11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespc;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static stack_entry_t *label_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static declaration_t *current_function = NULL;
37 static struct obstack temp_obst;
38 static bool found_error;
40 static type_t *type_int = NULL;
41 static type_t *type_uint = NULL;
42 static type_t *type_long_double = NULL;
43 static type_t *type_double = NULL;
44 static type_t *type_float = NULL;
45 static type_t *type_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_void_ptr = NULL;
49 static type_t *type_size_t = NULL;
50 static type_t *type_ptrdiff_t = NULL;
52 static statement_t *parse_compound_statement(void);
53 static statement_t *parse_statement(void);
55 static expression_t *parse_sub_expression(unsigned precedence);
56 static expression_t *parse_expression(void);
57 static type_t *parse_typename(void);
59 #define STORAGE_CLASSES \
66 #define TYPE_QUALIFIERS \
72 #ifdef PROVIDE_COMPLEX
73 #define COMPLEX_SPECIFIERS \
75 #define IMAGINARY_SPECIFIERS \
78 #define COMPLEX_SPECIFIERS
79 #define IMAGINARY_SPECIFIERS
82 #define TYPE_SPECIFIERS \
100 #define DECLARATION_START \
105 #define TYPENAME_START \
109 static void *allocate_ast_zero(size_t size)
111 void *res = allocate_ast(size);
112 memset(res, 0, size);
116 static size_t get_type_struct_size(type_type_t type)
118 static const size_t sizes[] = {
119 [TYPE_ATOMIC] = sizeof(atomic_type_t),
120 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
121 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
122 [TYPE_ENUM] = sizeof(enum_type_t),
123 [TYPE_FUNCTION] = sizeof(function_type_t),
124 [TYPE_POINTER] = sizeof(pointer_type_t),
125 [TYPE_ARRAY] = sizeof(array_type_t),
126 [TYPE_BUILTIN] = sizeof(builtin_type_t),
127 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
128 [TYPE_TYPEOF] = sizeof(typeof_type_t),
130 assert(type < TYPE_COUNT);
131 assert(sizes[type] != 0);
135 static type_t *allocate_type_zero(type_type_t type)
137 size_t size = get_type_struct_size(type);
138 type_t *res = obstack_alloc(type_obst, size);
139 memset(res, 0, size);
141 res->base.type = type;
145 static size_t get_initializer_size(initializer_type_t type)
147 static const size_t sizes[] = {
148 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
149 [INITIALIZER_STRING] = sizeof(initializer_string_t),
150 [INITIALIZER_LIST] = sizeof(initializer_list_t)
152 assert(type < INITIALIZER_COUNT);
153 assert(sizes[type] != 0);
157 static initializer_t *allocate_initializer(initializer_type_t type)
159 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
165 static void free_type(void *type)
167 obstack_free(type_obst, type);
171 * returns the top element of the environment stack
173 static size_t environment_top(void)
175 return ARR_LEN(environment_stack);
178 static size_t label_top(void)
180 return ARR_LEN(label_stack);
185 static inline void next_token(void)
187 token = lookahead_buffer[lookahead_bufpos];
188 lookahead_buffer[lookahead_bufpos] = lexer_token;
191 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
194 print_token(stderr, &token);
195 fprintf(stderr, "\n");
199 static inline const token_t *look_ahead(int num)
201 assert(num > 0 && num <= MAX_LOOKAHEAD);
202 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
203 return & lookahead_buffer[pos];
206 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
208 static void error(void)
211 #ifdef ABORT_ON_ERROR
216 static void parser_print_prefix_pos(const source_position_t source_position)
218 fputs(source_position.input_name, stderr);
220 fprintf(stderr, "%d", source_position.linenr);
224 static void parser_print_error_prefix_pos(
225 const source_position_t source_position)
227 parser_print_prefix_pos(source_position);
228 fputs("error: ", stderr);
232 static void parser_print_error_prefix(void)
234 parser_print_error_prefix_pos(token.source_position);
237 static void parse_error(const char *message)
239 parser_print_error_prefix();
240 fprintf(stderr, "parse error: %s\n", message);
243 static void parser_print_warning_prefix_pos(
244 const source_position_t source_position)
246 parser_print_prefix_pos(source_position);
247 fputs("warning: ", stderr);
250 static void parse_warning_pos(const source_position_t source_position,
251 const char *const message)
253 parser_print_prefix_pos(source_position);
254 fprintf(stderr, "warning: %s\n", message);
257 static void parse_warning(const char *message)
259 parse_warning_pos(token.source_position, message);
262 static void parse_error_expected(const char *message, ...)
267 if(message != NULL) {
268 parser_print_error_prefix();
269 fprintf(stderr, "%s\n", message);
271 parser_print_error_prefix();
272 fputs("Parse error: got ", stderr);
273 print_token(stderr, &token);
274 fputs(", expected ", stderr);
276 va_start(args, message);
277 token_type_t token_type = va_arg(args, token_type_t);
278 while(token_type != 0) {
282 fprintf(stderr, ", ");
284 print_token_type(stderr, token_type);
285 token_type = va_arg(args, token_type_t);
288 fprintf(stderr, "\n");
291 static void print_type_quoted(type_t *type)
298 static void type_error(const char *msg, const source_position_t source_position,
301 parser_print_error_prefix_pos(source_position);
302 fprintf(stderr, "%s, but found type ", msg);
303 print_type_quoted(type);
307 static void type_error_incompatible(const char *msg,
308 const source_position_t source_position, type_t *type1, type_t *type2)
310 parser_print_error_prefix_pos(source_position);
311 fprintf(stderr, "%s, incompatible types: ", msg);
312 print_type_quoted(type1);
313 fprintf(stderr, " - ");
314 print_type_quoted(type2);
315 fprintf(stderr, ")\n");
318 static void eat_block(void)
320 if(token.type == '{')
323 while(token.type != '}') {
324 if(token.type == T_EOF)
326 if(token.type == '{') {
335 static void eat_statement(void)
337 while(token.type != ';') {
338 if(token.type == T_EOF)
340 if(token.type == '}')
342 if(token.type == '{') {
351 static void eat_brace(void)
353 if(token.type == '(')
356 while(token.type != ')') {
357 if(token.type == T_EOF)
359 if(token.type == ')' || token.type == ';' || token.type == '}') {
362 if(token.type == '(') {
366 if(token.type == '{') {
375 #define expect(expected) \
376 if(UNLIKELY(token.type != (expected))) { \
377 parse_error_expected(NULL, (expected), 0); \
383 #define expect_block(expected) \
384 if(UNLIKELY(token.type != (expected))) { \
385 parse_error_expected(NULL, (expected), 0); \
391 #define expect_void(expected) \
392 if(UNLIKELY(token.type != (expected))) { \
393 parse_error_expected(NULL, (expected), 0); \
399 static void set_context(context_t *new_context)
401 context = new_context;
403 last_declaration = new_context->declarations;
404 if(last_declaration != NULL) {
405 while(last_declaration->next != NULL) {
406 last_declaration = last_declaration->next;
412 * called when we find a 2nd declarator for an identifier we already have a
415 static bool is_compatible_declaration (declaration_t *declaration,
416 declaration_t *previous)
418 /* TODO: not correct yet */
419 return declaration->type == previous->type;
422 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
424 declaration_t *declaration = symbol->declaration;
425 for( ; declaration != NULL; declaration = declaration->symbol_next) {
426 if(declaration->namespc == namespc)
433 static const char *get_namespace_prefix(namespace_t namespc)
436 case NAMESPACE_NORMAL:
438 case NAMESPACE_UNION:
440 case NAMESPACE_STRUCT:
444 case NAMESPACE_LABEL:
447 panic("invalid namespace found");
451 * pushs an environment_entry on the environment stack and links the
452 * corresponding symbol to the new entry
454 static declaration_t *stack_push(stack_entry_t **stack_ptr,
455 declaration_t *declaration,
456 context_t *parent_context)
458 symbol_t *symbol = declaration->symbol;
459 namespace_t namespc = (namespace_t)declaration->namespc;
461 /* a declaration should be only pushed once */
462 assert(declaration->parent_context == NULL);
463 declaration->parent_context = parent_context;
465 declaration_t *previous_declaration = get_declaration(symbol, namespc);
466 assert(declaration != previous_declaration);
467 if(previous_declaration != NULL
468 && previous_declaration->parent_context == context) {
469 if(!is_compatible_declaration(declaration, previous_declaration)) {
470 parser_print_error_prefix_pos(declaration->source_position);
471 fprintf(stderr, "definition of symbol %s%s with type ",
472 get_namespace_prefix(namespc), symbol->string);
473 print_type_quoted(declaration->type);
475 parser_print_error_prefix_pos(
476 previous_declaration->source_position);
477 fprintf(stderr, "is incompatible with previous declaration "
479 print_type_quoted(previous_declaration->type);
482 const storage_class_t old_storage = previous_declaration->storage_class;
483 const storage_class_t new_storage = declaration->storage_class;
484 if (current_function == NULL) {
485 if (old_storage != STORAGE_CLASS_STATIC &&
486 new_storage == STORAGE_CLASS_STATIC) {
487 parser_print_error_prefix_pos(declaration->source_position);
489 "static declaration of '%s' follows non-static declaration\n",
491 parser_print_error_prefix_pos(previous_declaration->source_position);
492 fprintf(stderr, "previous declaration of '%s' was here\n",
495 if (old_storage == STORAGE_CLASS_EXTERN) {
496 if (new_storage == STORAGE_CLASS_NONE) {
497 previous_declaration->storage_class = STORAGE_CLASS_NONE;
500 parser_print_warning_prefix_pos(declaration->source_position);
501 fprintf(stderr, "redundant declaration for '%s'\n",
503 parser_print_warning_prefix_pos(previous_declaration->source_position);
504 fprintf(stderr, "previous declaration of '%s' was here\n",
509 if (old_storage == STORAGE_CLASS_EXTERN &&
510 new_storage == STORAGE_CLASS_EXTERN) {
511 parser_print_warning_prefix_pos(declaration->source_position);
512 fprintf(stderr, "redundant extern declaration for '%s'\n",
514 parser_print_warning_prefix_pos(previous_declaration->source_position);
515 fprintf(stderr, "previous declaration of '%s' was here\n",
518 parser_print_error_prefix_pos(declaration->source_position);
519 if (old_storage == new_storage) {
520 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
522 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
524 parser_print_error_prefix_pos(previous_declaration->source_position);
525 fprintf(stderr, "previous declaration of '%s' was here\n",
530 return previous_declaration;
533 /* remember old declaration */
535 entry.symbol = symbol;
536 entry.old_declaration = symbol->declaration;
537 entry.namespc = namespc;
538 ARR_APP1(stack_entry_t, *stack_ptr, entry);
540 /* replace/add declaration into declaration list of the symbol */
541 if(symbol->declaration == NULL) {
542 symbol->declaration = declaration;
544 declaration_t *iter_last = NULL;
545 declaration_t *iter = symbol->declaration;
546 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
547 /* replace an entry? */
548 if(iter->namespc == namespc) {
549 if(iter_last == NULL) {
550 symbol->declaration = declaration;
552 iter_last->symbol_next = declaration;
554 declaration->symbol_next = iter->symbol_next;
559 assert(iter_last->symbol_next == NULL);
560 iter_last->symbol_next = declaration;
567 static declaration_t *environment_push(declaration_t *declaration)
569 assert(declaration->source_position.input_name != NULL);
570 return stack_push(&environment_stack, declaration, context);
573 static declaration_t *label_push(declaration_t *declaration)
575 return stack_push(&label_stack, declaration, ¤t_function->context);
579 * pops symbols from the environment stack until @p new_top is the top element
581 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
583 stack_entry_t *stack = *stack_ptr;
584 size_t top = ARR_LEN(stack);
587 assert(new_top <= top);
591 for(i = top; i > new_top; --i) {
592 stack_entry_t *entry = & stack[i - 1];
594 declaration_t *old_declaration = entry->old_declaration;
595 symbol_t *symbol = entry->symbol;
596 namespace_t namespc = (namespace_t)entry->namespc;
598 /* replace/remove declaration */
599 declaration_t *declaration = symbol->declaration;
600 assert(declaration != NULL);
601 if(declaration->namespc == namespc) {
602 if(old_declaration == NULL) {
603 symbol->declaration = declaration->symbol_next;
605 symbol->declaration = old_declaration;
608 declaration_t *iter_last = declaration;
609 declaration_t *iter = declaration->symbol_next;
610 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
611 /* replace an entry? */
612 if(iter->namespc == namespc) {
613 assert(iter_last != NULL);
614 iter_last->symbol_next = old_declaration;
615 old_declaration->symbol_next = iter->symbol_next;
619 assert(iter != NULL);
623 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
626 static void environment_pop_to(size_t new_top)
628 stack_pop_to(&environment_stack, new_top);
631 static void label_pop_to(size_t new_top)
633 stack_pop_to(&label_stack, new_top);
637 static int get_rank(const type_t *type)
639 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
640 * and esp. footnote 108). However we can't fold constants (yet), so we
641 * can't decide wether unsigned int is possible, while int always works.
642 * (unsigned int would be preferable when possible... for stuff like
643 * struct { enum { ... } bla : 4; } ) */
644 if(type->type == TYPE_ENUM)
645 return ATOMIC_TYPE_INT;
647 assert(type->type == TYPE_ATOMIC);
648 const atomic_type_t *atomic_type = &type->atomic;
649 atomic_type_type_t atype = atomic_type->atype;
653 static type_t *promote_integer(type_t *type)
655 if(get_rank(type) < ATOMIC_TYPE_INT)
661 static expression_t *create_cast_expression(expression_t *expression,
664 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
666 cast->expression.type = EXPR_UNARY;
667 cast->type = UNEXPR_CAST;
668 cast->value = expression;
669 cast->expression.datatype = dest_type;
671 return (expression_t*) cast;
674 static bool is_null_expression(const expression_t *const expr)
676 if (expr->type != EXPR_CONST) return false;
678 type_t *const type = skip_typeref(expr->datatype);
679 if (!is_type_integer(type)) return false;
681 const const_t *const const_expr = (const const_t*)expr;
682 return const_expr->v.int_value == 0;
685 static expression_t *create_implicit_cast(expression_t *expression,
688 type_t *source_type = expression->datatype;
690 if(source_type == NULL)
693 source_type = skip_typeref(source_type);
694 dest_type = skip_typeref(dest_type);
696 if(source_type == dest_type)
699 switch (dest_type->type) {
701 /* TODO warning for implicitly converting to enum */
703 if (source_type->type != TYPE_ATOMIC &&
704 source_type->type != TYPE_ENUM) {
705 panic("casting of non-atomic types not implemented yet");
708 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
709 type_error_incompatible("can't cast types",
710 expression->source_position, source_type, dest_type);
714 return create_cast_expression(expression, dest_type);
717 switch (source_type->type) {
719 if (is_null_expression(expression)) {
720 return create_cast_expression(expression, dest_type);
725 if (pointers_compatible(source_type, dest_type)) {
726 return create_cast_expression(expression, dest_type);
731 array_type_t *array_type = &source_type->array;
732 pointer_type_t *pointer_type = &dest_type->pointer;
733 if (types_compatible(array_type->element_type,
734 pointer_type->points_to)) {
735 return create_cast_expression(expression, dest_type);
741 panic("casting of non-atomic types not implemented yet");
744 type_error_incompatible("can't implicitly cast types",
745 expression->source_position, source_type, dest_type);
749 panic("casting of non-atomic types not implemented yet");
753 /** Implements the rules from § 6.5.16.1 */
754 static void semantic_assign(type_t *orig_type_left, expression_t **right,
757 type_t *orig_type_right = (*right)->datatype;
759 if(orig_type_right == NULL)
762 type_t *const type_left = skip_typeref(orig_type_left);
763 type_t *const type_right = skip_typeref(orig_type_right);
765 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
766 (is_type_pointer(type_left) && is_null_expression(*right)) ||
767 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
768 && is_type_pointer(type_right))) {
769 *right = create_implicit_cast(*right, type_left);
773 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
774 pointer_type_t *pointer_type_left = (pointer_type_t*) type_left;
775 pointer_type_t *pointer_type_right = (pointer_type_t*) type_right;
776 type_t *points_to_left = pointer_type_left->points_to;
777 type_t *points_to_right = pointer_type_right->points_to;
779 points_to_left = skip_typeref(points_to_left);
780 points_to_right = skip_typeref(points_to_right);
782 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
783 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
784 && !types_compatible(points_to_left, points_to_right)) {
785 goto incompatible_assign_types;
788 /* the left type has all qualifiers from the right type */
789 unsigned missing_qualifiers
790 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
791 if(missing_qualifiers != 0) {
792 parser_print_error_prefix();
793 fprintf(stderr, "destination type ");
794 print_type_quoted(type_left);
795 fprintf(stderr, " in %s from type ", context);
796 print_type_quoted(type_right);
797 fprintf(stderr, " lacks qualifiers '");
798 print_type_qualifiers(missing_qualifiers);
799 fprintf(stderr, "' in pointed-to type\n");
803 *right = create_implicit_cast(*right, type_left);
807 if (is_type_compound(type_left)
808 && types_compatible(type_left, type_right)) {
809 *right = create_implicit_cast(*right, type_left);
813 incompatible_assign_types:
814 /* TODO: improve error message */
815 parser_print_error_prefix();
816 fprintf(stderr, "incompatible types in %s\n", context);
817 parser_print_error_prefix();
818 print_type_quoted(type_left);
819 fputs(" <- ", stderr);
820 print_type_quoted(type_right);
824 static expression_t *parse_constant_expression(void)
826 /* start parsing at precedence 7 (conditional expression) */
827 return parse_sub_expression(7);
830 static expression_t *parse_assignment_expression(void)
832 /* start parsing at precedence 2 (assignment expression) */
833 return parse_sub_expression(2);
836 typedef struct declaration_specifiers_t declaration_specifiers_t;
837 struct declaration_specifiers_t {
838 storage_class_t storage_class;
843 static void parse_compound_type_entries(void);
844 static declaration_t *parse_declarator(
845 const declaration_specifiers_t *specifiers, type_t *type,
846 bool may_be_abstract);
847 static declaration_t *record_declaration(declaration_t *declaration);
849 static const char *parse_string_literals(void)
851 assert(token.type == T_STRING_LITERAL);
852 const char *result = token.v.string;
856 while(token.type == T_STRING_LITERAL) {
857 result = concat_strings(result, token.v.string);
864 static void parse_attributes(void)
868 case T___attribute__:
876 parse_error("EOF while parsing attribute");
894 if(token.type != T_STRING_LITERAL) {
895 parse_error_expected("while parsing assembler attribute",
900 parse_string_literals();
905 goto attributes_finished;
914 static designator_t *parse_designation(void)
916 if(token.type != '[' && token.type != '.')
919 designator_t *result = NULL;
920 designator_t *last = NULL;
923 designator_t *designator;
926 designator = allocate_ast_zero(sizeof(designator[0]));
928 designator->array_access = parse_constant_expression();
932 designator = allocate_ast_zero(sizeof(designator[0]));
934 if(token.type != T_IDENTIFIER) {
935 parse_error_expected("while parsing designator",
939 designator->symbol = token.v.symbol;
947 assert(designator != NULL);
949 last->next = designator;
958 static initializer_t *initializer_from_string(array_type_t *type,
961 /* TODO: check len vs. size of array type */
964 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
965 initializer->string.string = string;
970 static initializer_t *initializer_from_expression(type_t *type,
971 expression_t *expression)
973 /* TODO check that expression is a constant expression */
975 /* § 6.7.8.14/15 char array may be initialized by string literals */
976 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
977 array_type_t *array_type = &type->array;
978 type_t *element_type = array_type->element_type;
980 if(element_type->type == TYPE_ATOMIC) {
981 atomic_type_t *atomic_type = &element_type->atomic;
982 atomic_type_type_t atype = atomic_type->atype;
984 /* TODO handle wide strings */
985 if(atype == ATOMIC_TYPE_CHAR
986 || atype == ATOMIC_TYPE_SCHAR
987 || atype == ATOMIC_TYPE_UCHAR) {
989 string_literal_t *literal = (string_literal_t*) expression;
990 return initializer_from_string(array_type, literal->value);
995 semantic_assign(type, &expression, "initializer");
997 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
998 result->value.value = expression;
1003 static initializer_t *parse_sub_initializer(type_t *type,
1004 expression_t *expression,
1005 type_t *expression_type);
1007 static initializer_t *parse_sub_initializer_elem(type_t *type)
1009 if(token.type == '{') {
1010 return parse_sub_initializer(type, NULL, NULL);
1013 expression_t *expression = parse_assignment_expression();
1014 type_t *expression_type = skip_typeref(expression->datatype);
1016 return parse_sub_initializer(type, expression, expression_type);
1019 static bool had_initializer_brace_warning;
1021 static initializer_t *parse_sub_initializer(type_t *type,
1022 expression_t *expression,
1023 type_t *expression_type)
1025 if(is_type_scalar(type)) {
1026 /* there might be extra {} hierarchies */
1027 if(token.type == '{') {
1029 if(!had_initializer_brace_warning) {
1030 parse_warning("braces around scalar initializer");
1031 had_initializer_brace_warning = true;
1033 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1034 if(token.type == ',') {
1036 /* TODO: warn about excessive elements */
1042 if(expression == NULL) {
1043 expression = parse_assignment_expression();
1045 return initializer_from_expression(type, expression);
1048 /* TODO: ignore qualifiers, comparing pointers is probably
1050 if(expression != NULL && expression_type == type) {
1051 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1054 semantic_assign(type, &expression, "initializer");
1056 result->value.value = expression;
1061 bool read_paren = false;
1062 if(token.type == '{') {
1067 /* descend into subtype */
1068 initializer_t *result = NULL;
1069 initializer_t **elems;
1070 if(type->type == TYPE_ARRAY) {
1071 array_type_t *array_type = (array_type_t*) type;
1072 type_t *element_type = array_type->element_type;
1073 element_type = skip_typeref(element_type);
1076 had_initializer_brace_warning = false;
1077 if(expression == NULL) {
1078 sub = parse_sub_initializer_elem(element_type);
1080 sub = parse_sub_initializer(element_type, expression,
1084 /* didn't match the subtypes -> try the parent type */
1086 assert(!read_paren);
1090 elems = NEW_ARR_F(initializer_t*, 0);
1091 ARR_APP1(initializer_t*, elems, sub);
1094 if(token.type == '}')
1097 if(token.type == '}')
1101 = parse_sub_initializer(element_type, NULL, NULL);
1103 /* TODO error, do nicer cleanup */
1104 parse_error("member initializer didn't match");
1108 ARR_APP1(initializer_t*, elems, sub);
1111 assert(type->type == TYPE_COMPOUND_STRUCT
1112 || type->type == TYPE_COMPOUND_UNION);
1113 compound_type_t *compound_type = (compound_type_t*) type;
1114 context_t *context = & compound_type->declaration->context;
1116 declaration_t *first = context->declarations;
1119 type_t *first_type = first->type;
1120 first_type = skip_typeref(first_type);
1123 had_initializer_brace_warning = false;
1124 if(expression == NULL) {
1125 sub = parse_sub_initializer_elem(first_type);
1127 sub = parse_sub_initializer(first_type, expression,expression_type);
1130 /* didn't match the subtypes -> try our parent type */
1132 assert(!read_paren);
1136 elems = NEW_ARR_F(initializer_t*, 0);
1137 ARR_APP1(initializer_t*, elems, sub);
1139 declaration_t *iter = first->next;
1140 for( ; iter != NULL; iter = iter->next) {
1141 if(iter->symbol == NULL)
1143 if(iter->namespc != NAMESPACE_NORMAL)
1146 if(token.type == '}')
1149 if(token.type == '}')
1152 type_t *iter_type = iter->type;
1153 iter_type = skip_typeref(iter_type);
1155 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1157 /* TODO error, do nicer cleanup*/
1158 parse_error("member initializer didn't match");
1162 ARR_APP1(initializer_t*, elems, sub);
1166 int len = ARR_LEN(elems);
1167 size_t elems_size = sizeof(initializer_t*) * len;
1169 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1171 init->initializer.type = INITIALIZER_LIST;
1173 memcpy(init->initializers, elems, elems_size);
1176 result = (initializer_t*) init;
1179 if(token.type == ',')
1186 static initializer_t *parse_initializer(type_t *type)
1188 initializer_t *result;
1190 type = skip_typeref(type);
1192 if(token.type != '{') {
1193 expression_t *expression = parse_assignment_expression();
1194 return initializer_from_expression(type, expression);
1197 if(is_type_scalar(type)) {
1201 expression_t *expression = parse_assignment_expression();
1202 result = initializer_from_expression(type, expression);
1204 if(token.type == ',')
1210 result = parse_sub_initializer(type, NULL, NULL);
1218 static declaration_t *parse_compound_type_specifier(bool is_struct)
1226 symbol_t *symbol = NULL;
1227 declaration_t *declaration = NULL;
1229 if (token.type == T___attribute__) {
1234 if(token.type == T_IDENTIFIER) {
1235 symbol = token.v.symbol;
1239 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1241 declaration = get_declaration(symbol, NAMESPACE_UNION);
1243 } else if(token.type != '{') {
1245 parse_error_expected("while parsing struct type specifier",
1246 T_IDENTIFIER, '{', 0);
1248 parse_error_expected("while parsing union type specifier",
1249 T_IDENTIFIER, '{', 0);
1255 if(declaration == NULL) {
1256 declaration = allocate_ast_zero(sizeof(declaration[0]));
1259 declaration->namespc = NAMESPACE_STRUCT;
1261 declaration->namespc = NAMESPACE_UNION;
1263 declaration->source_position = token.source_position;
1264 declaration->symbol = symbol;
1265 record_declaration(declaration);
1268 if(token.type == '{') {
1269 if(declaration->init.is_defined) {
1270 assert(symbol != NULL);
1271 parser_print_error_prefix();
1272 fprintf(stderr, "multiple definition of %s %s\n",
1273 is_struct ? "struct" : "union", symbol->string);
1274 declaration->context.declarations = NULL;
1276 declaration->init.is_defined = true;
1278 int top = environment_top();
1279 context_t *last_context = context;
1280 set_context(& declaration->context);
1282 parse_compound_type_entries();
1285 assert(context == & declaration->context);
1286 set_context(last_context);
1287 environment_pop_to(top);
1293 static void parse_enum_entries(enum_type_t *const enum_type)
1297 if(token.type == '}') {
1299 parse_error("empty enum not allowed");
1304 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1306 if(token.type != T_IDENTIFIER) {
1307 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1311 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1312 entry->type = (type_t*) enum_type;
1313 entry->symbol = token.v.symbol;
1314 entry->source_position = token.source_position;
1317 if(token.type == '=') {
1319 entry->init.enum_value = parse_constant_expression();
1324 record_declaration(entry);
1326 if(token.type != ',')
1329 } while(token.type != '}');
1334 static type_t *parse_enum_specifier(void)
1338 declaration_t *declaration;
1341 if(token.type == T_IDENTIFIER) {
1342 symbol = token.v.symbol;
1345 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1346 } else if(token.type != '{') {
1347 parse_error_expected("while parsing enum type specifier",
1348 T_IDENTIFIER, '{', 0);
1355 if(declaration == NULL) {
1356 declaration = allocate_ast_zero(sizeof(declaration[0]));
1358 declaration->namespc = NAMESPACE_ENUM;
1359 declaration->source_position = token.source_position;
1360 declaration->symbol = symbol;
1363 type_t *const type = allocate_type_zero(TYPE_ENUM);
1364 type->enumt.declaration = declaration;
1366 if(token.type == '{') {
1367 if(declaration->init.is_defined) {
1368 parser_print_error_prefix();
1369 fprintf(stderr, "multiple definitions of enum %s\n",
1372 record_declaration(declaration);
1373 declaration->init.is_defined = 1;
1375 parse_enum_entries(&type->enumt);
1383 * if a symbol is a typedef to another type, return true
1385 static bool is_typedef_symbol(symbol_t *symbol)
1387 const declaration_t *const declaration =
1388 get_declaration(symbol, NAMESPACE_NORMAL);
1390 declaration != NULL &&
1391 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1394 static type_t *parse_typeof(void)
1402 expression_t *expression = NULL;
1405 switch(token.type) {
1406 case T___extension__:
1407 /* this can be a prefix to a typename or an expression */
1408 /* we simply eat it now. */
1411 } while(token.type == T___extension__);
1415 if(is_typedef_symbol(token.v.symbol)) {
1416 type = parse_typename();
1418 expression = parse_expression();
1419 type = expression->datatype;
1424 type = parse_typename();
1428 expression = parse_expression();
1429 type = expression->datatype;
1435 type_t *typeof = allocate_type_zero(TYPE_TYPEOF);
1436 typeof->typeoft.expression = expression;
1437 typeof->typeoft.typeof_type = type;
1443 SPECIFIER_SIGNED = 1 << 0,
1444 SPECIFIER_UNSIGNED = 1 << 1,
1445 SPECIFIER_LONG = 1 << 2,
1446 SPECIFIER_INT = 1 << 3,
1447 SPECIFIER_DOUBLE = 1 << 4,
1448 SPECIFIER_CHAR = 1 << 5,
1449 SPECIFIER_SHORT = 1 << 6,
1450 SPECIFIER_LONG_LONG = 1 << 7,
1451 SPECIFIER_FLOAT = 1 << 8,
1452 SPECIFIER_BOOL = 1 << 9,
1453 SPECIFIER_VOID = 1 << 10,
1454 #ifdef PROVIDE_COMPLEX
1455 SPECIFIER_COMPLEX = 1 << 11,
1456 SPECIFIER_IMAGINARY = 1 << 12,
1460 static type_t *create_builtin_type(symbol_t *symbol)
1462 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1463 type->builtin.symbol = symbol;
1465 type->builtin.real_type = type_int;
1470 static type_t *get_typedef_type(symbol_t *symbol)
1472 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1473 if(declaration == NULL
1474 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1477 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1478 type->typedeft.declaration = declaration;
1483 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1485 type_t *type = NULL;
1486 unsigned type_qualifiers = 0;
1487 unsigned type_specifiers = 0;
1491 switch(token.type) {
1494 #define MATCH_STORAGE_CLASS(token, class) \
1496 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1497 parse_error("multiple storage classes in declaration " \
1500 specifiers->storage_class = class; \
1504 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1505 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1506 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1507 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1508 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1510 /* type qualifiers */
1511 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1513 type_qualifiers |= qualifier; \
1517 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1518 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1519 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1521 case T___extension__:
1526 /* type specifiers */
1527 #define MATCH_SPECIFIER(token, specifier, name) \
1530 if(type_specifiers & specifier) { \
1531 parse_error("multiple " name " type specifiers given"); \
1533 type_specifiers |= specifier; \
1537 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1538 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1539 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1540 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1541 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1542 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1543 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1544 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1545 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1546 #ifdef PROVIDE_COMPLEX
1547 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1548 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1552 specifiers->is_inline = true;
1557 if(type_specifiers & SPECIFIER_LONG_LONG) {
1558 parse_error("multiple type specifiers given");
1559 } else if(type_specifiers & SPECIFIER_LONG) {
1560 type_specifiers |= SPECIFIER_LONG_LONG;
1562 type_specifiers |= SPECIFIER_LONG;
1566 /* TODO: if type != NULL for the following rules should issue
1569 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1571 type->compound.declaration = parse_compound_type_specifier(true);
1575 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1577 type->compound.declaration = parse_compound_type_specifier(false);
1581 type = parse_enum_specifier();
1584 type = parse_typeof();
1586 case T___builtin_va_list:
1587 type = create_builtin_type(token.v.symbol);
1591 case T___attribute__:
1596 case T_IDENTIFIER: {
1597 type_t *typedef_type = get_typedef_type(token.v.symbol);
1599 if(typedef_type == NULL)
1600 goto finish_specifiers;
1603 type = typedef_type;
1607 /* function specifier */
1609 goto finish_specifiers;
1616 atomic_type_type_t atomic_type;
1618 /* match valid basic types */
1619 switch(type_specifiers) {
1620 case SPECIFIER_VOID:
1621 atomic_type = ATOMIC_TYPE_VOID;
1623 case SPECIFIER_CHAR:
1624 atomic_type = ATOMIC_TYPE_CHAR;
1626 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1627 atomic_type = ATOMIC_TYPE_SCHAR;
1629 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1630 atomic_type = ATOMIC_TYPE_UCHAR;
1632 case SPECIFIER_SHORT:
1633 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1634 case SPECIFIER_SHORT | SPECIFIER_INT:
1635 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1636 atomic_type = ATOMIC_TYPE_SHORT;
1638 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1639 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1640 atomic_type = ATOMIC_TYPE_USHORT;
1643 case SPECIFIER_SIGNED:
1644 case SPECIFIER_SIGNED | SPECIFIER_INT:
1645 atomic_type = ATOMIC_TYPE_INT;
1647 case SPECIFIER_UNSIGNED:
1648 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1649 atomic_type = ATOMIC_TYPE_UINT;
1651 case SPECIFIER_LONG:
1652 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1653 case SPECIFIER_LONG | SPECIFIER_INT:
1654 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1655 atomic_type = ATOMIC_TYPE_LONG;
1657 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1658 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1659 atomic_type = ATOMIC_TYPE_ULONG;
1661 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1662 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1663 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1664 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1666 atomic_type = ATOMIC_TYPE_LONGLONG;
1668 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1669 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1671 atomic_type = ATOMIC_TYPE_ULONGLONG;
1673 case SPECIFIER_FLOAT:
1674 atomic_type = ATOMIC_TYPE_FLOAT;
1676 case SPECIFIER_DOUBLE:
1677 atomic_type = ATOMIC_TYPE_DOUBLE;
1679 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1680 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1682 case SPECIFIER_BOOL:
1683 atomic_type = ATOMIC_TYPE_BOOL;
1685 #ifdef PROVIDE_COMPLEX
1686 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1687 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1689 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1690 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1692 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1693 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1695 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1696 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1698 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1699 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1701 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1702 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1706 /* invalid specifier combination, give an error message */
1707 if(type_specifiers == 0) {
1709 parse_warning("no type specifiers in declaration (using int)");
1710 atomic_type = ATOMIC_TYPE_INT;
1713 parse_error("no type specifiers given in declaration");
1715 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1716 (type_specifiers & SPECIFIER_UNSIGNED)) {
1717 parse_error("signed and unsigned specifiers gives");
1718 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1719 parse_error("only integer types can be signed or unsigned");
1721 parse_error("multiple datatypes in declaration");
1723 atomic_type = ATOMIC_TYPE_INVALID;
1726 type = allocate_type_zero(TYPE_ATOMIC);
1727 type->atomic.atype = atomic_type;
1730 if(type_specifiers != 0) {
1731 parse_error("multiple datatypes in declaration");
1735 type->base.qualifiers = type_qualifiers;
1737 type_t *result = typehash_insert(type);
1738 if(newtype && result != type) {
1742 specifiers->type = result;
1745 static type_qualifiers_t parse_type_qualifiers(void)
1747 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1750 switch(token.type) {
1751 /* type qualifiers */
1752 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1753 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1754 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1757 return type_qualifiers;
1762 static void parse_identifier_list(void)
1765 if(token.type != T_IDENTIFIER) {
1766 parse_error_expected("while parsing parameter identifier list",
1771 if(token.type != ',')
1777 static declaration_t *parse_parameter(void)
1779 declaration_specifiers_t specifiers;
1780 memset(&specifiers, 0, sizeof(specifiers));
1782 parse_declaration_specifiers(&specifiers);
1784 declaration_t *declaration
1785 = parse_declarator(&specifiers, specifiers.type, true);
1787 /* TODO check declaration constraints for parameters */
1788 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1789 parse_error("typedef not allowed in parameter list");
1792 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1794 if (declaration->type->type == TYPE_ARRAY) {
1795 const array_type_t *const arr_type =
1796 (const array_type_t*)declaration->type;
1797 type_t *element_type = arr_type->element_type;
1798 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1804 static declaration_t *parse_parameters(function_type_t *type)
1806 if(token.type == T_IDENTIFIER) {
1807 symbol_t *symbol = token.v.symbol;
1808 if(!is_typedef_symbol(symbol)) {
1809 /* TODO: K&R style C parameters */
1810 parse_identifier_list();
1815 if(token.type == ')') {
1816 type->unspecified_parameters = 1;
1819 if(token.type == T_void && look_ahead(1)->type == ')') {
1824 declaration_t *declarations = NULL;
1825 declaration_t *declaration;
1826 declaration_t *last_declaration = NULL;
1827 function_parameter_t *parameter;
1828 function_parameter_t *last_parameter = NULL;
1831 switch(token.type) {
1835 return declarations;
1838 case T___extension__:
1840 declaration = parse_parameter();
1842 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1843 memset(parameter, 0, sizeof(parameter[0]));
1844 parameter->type = declaration->type;
1846 if(last_parameter != NULL) {
1847 last_declaration->next = declaration;
1848 last_parameter->next = parameter;
1850 type->parameters = parameter;
1851 declarations = declaration;
1853 last_parameter = parameter;
1854 last_declaration = declaration;
1858 return declarations;
1860 if(token.type != ',')
1861 return declarations;
1871 } construct_type_type_t;
1873 typedef struct construct_type_t construct_type_t;
1874 struct construct_type_t {
1875 construct_type_type_t type;
1876 construct_type_t *next;
1879 typedef struct parsed_pointer_t parsed_pointer_t;
1880 struct parsed_pointer_t {
1881 construct_type_t construct_type;
1882 type_qualifiers_t type_qualifiers;
1885 typedef struct construct_function_type_t construct_function_type_t;
1886 struct construct_function_type_t {
1887 construct_type_t construct_type;
1888 type_t *function_type;
1891 typedef struct parsed_array_t parsed_array_t;
1892 struct parsed_array_t {
1893 construct_type_t construct_type;
1894 type_qualifiers_t type_qualifiers;
1900 typedef struct construct_base_type_t construct_base_type_t;
1901 struct construct_base_type_t {
1902 construct_type_t construct_type;
1906 static construct_type_t *parse_pointer_declarator(void)
1910 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1911 memset(pointer, 0, sizeof(pointer[0]));
1912 pointer->construct_type.type = CONSTRUCT_POINTER;
1913 pointer->type_qualifiers = parse_type_qualifiers();
1915 return (construct_type_t*) pointer;
1918 static construct_type_t *parse_array_declarator(void)
1922 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1923 memset(array, 0, sizeof(array[0]));
1924 array->construct_type.type = CONSTRUCT_ARRAY;
1926 if(token.type == T_static) {
1927 array->is_static = true;
1931 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1932 if(type_qualifiers != 0) {
1933 if(token.type == T_static) {
1934 array->is_static = true;
1938 array->type_qualifiers = type_qualifiers;
1940 if(token.type == '*' && look_ahead(1)->type == ']') {
1941 array->is_variable = true;
1943 } else if(token.type != ']') {
1944 array->size = parse_assignment_expression();
1949 return (construct_type_t*) array;
1952 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1956 type_t *type = allocate_type_zero(TYPE_FUNCTION);
1958 declaration_t *parameters = parse_parameters(&type->function);
1959 if(declaration != NULL) {
1960 declaration->context.declarations = parameters;
1963 construct_function_type_t *construct_function_type =
1964 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1965 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1966 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1967 construct_function_type->function_type = type;
1971 return (construct_type_t*) construct_function_type;
1974 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1975 bool may_be_abstract)
1977 /* construct a single linked list of construct_type_t's which describe
1978 * how to construct the final declarator type */
1979 construct_type_t *first = NULL;
1980 construct_type_t *last = NULL;
1983 while(token.type == '*') {
1984 construct_type_t *type = parse_pointer_declarator();
1995 /* TODO: find out if this is correct */
1998 construct_type_t *inner_types = NULL;
2000 switch(token.type) {
2002 if(declaration == NULL) {
2003 parse_error("no identifier expected in typename");
2005 declaration->symbol = token.v.symbol;
2006 declaration->source_position = token.source_position;
2012 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2018 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2019 /* avoid a loop in the outermost scope, because eat_statement doesn't
2021 if(token.type == '}' && current_function == NULL) {
2029 construct_type_t *p = last;
2032 construct_type_t *type;
2033 switch(token.type) {
2035 type = parse_function_declarator(declaration);
2038 type = parse_array_declarator();
2041 goto declarator_finished;
2044 /* insert in the middle of the list (behind p) */
2046 type->next = p->next;
2057 declarator_finished:
2060 /* append inner_types at the end of the list, we don't to set last anymore
2061 * as it's not needed anymore */
2063 assert(first == NULL);
2064 first = inner_types;
2066 last->next = inner_types;
2072 static type_t *construct_declarator_type(construct_type_t *construct_list,
2075 construct_type_t *iter = construct_list;
2076 for( ; iter != NULL; iter = iter->next) {
2077 switch(iter->type) {
2078 case CONSTRUCT_INVALID:
2079 panic("invalid type construction found");
2080 case CONSTRUCT_FUNCTION: {
2081 construct_function_type_t *construct_function_type
2082 = (construct_function_type_t*) iter;
2084 type_t *function_type = construct_function_type->function_type;
2086 function_type->function.result_type = type;
2088 type = function_type;
2092 case CONSTRUCT_POINTER: {
2093 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2094 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2095 pointer_type->pointer.points_to = type;
2096 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2098 type = pointer_type;
2102 case CONSTRUCT_ARRAY: {
2103 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2104 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2106 array_type->base.qualifiers = parsed_array->type_qualifiers;
2107 array_type->array.element_type = type;
2108 array_type->array.is_static = parsed_array->is_static;
2109 array_type->array.is_variable = parsed_array->is_variable;
2110 array_type->array.size = parsed_array->size;
2117 type_t *hashed_type = typehash_insert(type);
2118 if(hashed_type != type) {
2119 /* the function type was constructed earlier freeing it here will
2120 * destroy other types... */
2121 if(iter->type != CONSTRUCT_FUNCTION) {
2131 static declaration_t *parse_declarator(
2132 const declaration_specifiers_t *specifiers,
2133 type_t *type, bool may_be_abstract)
2135 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2136 declaration->storage_class = specifiers->storage_class;
2137 declaration->is_inline = specifiers->is_inline;
2139 construct_type_t *construct_type
2140 = parse_inner_declarator(declaration, may_be_abstract);
2141 declaration->type = construct_declarator_type(construct_type, type);
2143 if(construct_type != NULL) {
2144 obstack_free(&temp_obst, construct_type);
2150 static type_t *parse_abstract_declarator(type_t *base_type)
2152 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2154 type_t *result = construct_declarator_type(construct_type, base_type);
2155 if(construct_type != NULL) {
2156 obstack_free(&temp_obst, construct_type);
2162 static declaration_t *record_declaration(declaration_t *declaration)
2164 assert(context != NULL);
2166 symbol_t *symbol = declaration->symbol;
2167 if(symbol != NULL) {
2168 declaration_t *alias = environment_push(declaration);
2169 if(alias != declaration)
2172 declaration->parent_context = context;
2175 if(last_declaration != NULL) {
2176 last_declaration->next = declaration;
2178 context->declarations = declaration;
2180 last_declaration = declaration;
2185 static void parser_error_multiple_definition(declaration_t *previous,
2186 declaration_t *declaration)
2188 parser_print_error_prefix_pos(declaration->source_position);
2189 fprintf(stderr, "multiple definition of symbol '%s'\n",
2190 declaration->symbol->string);
2191 parser_print_error_prefix_pos(previous->source_position);
2192 fprintf(stderr, "this is the location of the previous definition.\n");
2195 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2198 declaration_t *ndeclaration
2199 = parse_declarator(specifiers, specifiers->type, false);
2201 declaration_t *declaration = record_declaration(ndeclaration);
2203 type_t *orig_type = declaration->type;
2204 type_t *type = skip_typeref(orig_type);
2205 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2206 parser_print_warning_prefix_pos(declaration->source_position);
2207 fprintf(stderr, "variable '%s' declared 'inline'\n",
2208 declaration->symbol->string);
2211 if(token.type == '=') {
2214 /* TODO: check that this is an allowed type (no function type) */
2216 if(declaration->init.initializer != NULL) {
2217 parser_error_multiple_definition(declaration, ndeclaration);
2220 initializer_t *initializer = parse_initializer(type);
2222 if(type->type == TYPE_ARRAY && initializer != NULL) {
2223 array_type_t *array_type = (array_type_t*) type;
2225 if(array_type->size == NULL) {
2226 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2228 cnst->expression.type = EXPR_CONST;
2229 cnst->expression.datatype = type_size_t;
2231 if(initializer->type == INITIALIZER_LIST) {
2232 initializer_list_t *list = &initializer->list;
2233 cnst->v.int_value = list->len;
2235 assert(initializer->type == INITIALIZER_STRING);
2236 initializer_string_t *string = &initializer->string;
2237 cnst->v.int_value = strlen(string->string) + 1;
2240 array_type->size = (expression_t*) cnst;
2245 ndeclaration->init.initializer = initializer;
2246 } else if(token.type == '{') {
2247 if(type->type != TYPE_FUNCTION) {
2248 parser_print_error_prefix();
2249 fprintf(stderr, "declarator '");
2250 print_type_ext(orig_type, declaration->symbol, NULL);
2251 fprintf(stderr, "' has a body but is not a function type.\n");
2256 if(declaration->init.statement != NULL) {
2257 parser_error_multiple_definition(declaration, ndeclaration);
2259 if(ndeclaration != declaration) {
2260 memcpy(&declaration->context, &ndeclaration->context,
2261 sizeof(declaration->context));
2264 int top = environment_top();
2265 context_t *last_context = context;
2266 set_context(&declaration->context);
2268 /* push function parameters */
2269 declaration_t *parameter = declaration->context.declarations;
2270 for( ; parameter != NULL; parameter = parameter->next) {
2271 environment_push(parameter);
2274 int label_stack_top = label_top();
2275 declaration_t *old_current_function = current_function;
2276 current_function = declaration;
2278 statement_t *statement = parse_compound_statement();
2280 assert(current_function == declaration);
2281 current_function = old_current_function;
2282 label_pop_to(label_stack_top);
2284 assert(context == &declaration->context);
2285 set_context(last_context);
2286 environment_pop_to(top);
2288 declaration->init.statement = statement;
2292 if(token.type != ',')
2299 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2302 if(token.type == ':') {
2304 parse_constant_expression();
2305 /* TODO (bitfields) */
2307 declaration_t *declaration
2308 = parse_declarator(specifiers, specifiers->type, true);
2310 /* TODO: check constraints for struct declarations */
2311 /* TODO: check for doubled fields */
2312 record_declaration(declaration);
2314 if(token.type == ':') {
2316 parse_constant_expression();
2317 /* TODO (bitfields) */
2321 if(token.type != ',')
2328 static void parse_compound_type_entries(void)
2332 while(token.type != '}' && token.type != T_EOF) {
2333 declaration_specifiers_t specifiers;
2334 memset(&specifiers, 0, sizeof(specifiers));
2335 parse_declaration_specifiers(&specifiers);
2337 parse_struct_declarators(&specifiers);
2339 if(token.type == T_EOF) {
2340 parse_error("unexpected error while parsing struct");
2345 static void parse_declaration(void)
2347 source_position_t source_position = token.source_position;
2349 declaration_specifiers_t specifiers;
2350 memset(&specifiers, 0, sizeof(specifiers));
2351 parse_declaration_specifiers(&specifiers);
2353 if(token.type == ';') {
2354 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2355 parse_warning_pos(source_position,
2356 "useless keyword in empty declaration");
2358 switch (specifiers.type->type) {
2359 case TYPE_COMPOUND_STRUCT:
2360 case TYPE_COMPOUND_UNION: {
2361 const compound_type_t *const comp_type =
2362 (const compound_type_t*)specifiers.type;
2363 if (comp_type->declaration->symbol == NULL) {
2364 parse_warning_pos(source_position,
2365 "unnamed struct/union that defines no instances");
2370 case TYPE_ENUM: break;
2373 parse_warning_pos(source_position, "empty declaration");
2379 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2381 declaration->type = specifiers.type;
2382 declaration->storage_class = specifiers.storage_class;
2383 declaration->source_position = source_position;
2384 record_declaration(declaration);
2387 parse_init_declarators(&specifiers);
2390 static type_t *parse_typename(void)
2392 declaration_specifiers_t specifiers;
2393 memset(&specifiers, 0, sizeof(specifiers));
2394 parse_declaration_specifiers(&specifiers);
2395 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2396 /* TODO: improve error message, user does probably not know what a
2397 * storage class is...
2399 parse_error("typename may not have a storage class");
2402 type_t *result = parse_abstract_declarator(specifiers.type);
2410 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2411 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2412 expression_t *left);
2414 typedef struct expression_parser_function_t expression_parser_function_t;
2415 struct expression_parser_function_t {
2416 unsigned precedence;
2417 parse_expression_function parser;
2418 unsigned infix_precedence;
2419 parse_expression_infix_function infix_parser;
2422 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2424 static expression_t *make_invalid_expression(void)
2426 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2427 expression->type = EXPR_INVALID;
2428 expression->source_position = token.source_position;
2432 static expression_t *expected_expression_error(void)
2434 parser_print_error_prefix();
2435 fprintf(stderr, "expected expression, got token ");
2436 print_token(stderr, & token);
2437 fprintf(stderr, "\n");
2441 return make_invalid_expression();
2444 static expression_t *parse_string_const(void)
2446 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2448 cnst->expression.type = EXPR_STRING_LITERAL;
2449 cnst->expression.datatype = type_string;
2450 cnst->value = parse_string_literals();
2452 return (expression_t*) cnst;
2455 static expression_t *parse_int_const(void)
2457 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2459 cnst->expression.type = EXPR_CONST;
2460 cnst->expression.datatype = token.datatype;
2461 cnst->v.int_value = token.v.intvalue;
2465 return (expression_t*) cnst;
2468 static expression_t *parse_float_const(void)
2470 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2472 cnst->expression.type = EXPR_CONST;
2473 cnst->expression.datatype = token.datatype;
2474 cnst->v.float_value = token.v.floatvalue;
2478 return (expression_t*) cnst;
2481 static declaration_t *create_implicit_function(symbol_t *symbol,
2482 const source_position_t source_position)
2484 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2485 ntype->function.result_type = type_int;
2486 ntype->function.unspecified_parameters = true;
2488 type_t *type = typehash_insert(ntype);
2493 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2495 declaration->storage_class = STORAGE_CLASS_EXTERN;
2496 declaration->type = type;
2497 declaration->symbol = symbol;
2498 declaration->source_position = source_position;
2500 /* prepend the implicit definition to the global context
2501 * this is safe since the symbol wasn't declared as anything else yet
2503 assert(symbol->declaration == NULL);
2505 context_t *last_context = context;
2506 context = global_context;
2508 environment_push(declaration);
2509 declaration->next = context->declarations;
2510 context->declarations = declaration;
2512 context = last_context;
2517 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2519 function_parameter_t *parameter
2520 = obstack_alloc(type_obst, sizeof(parameter[0]));
2521 memset(parameter, 0, sizeof(parameter[0]));
2522 parameter->type = argument_type;
2524 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2525 type->function.result_type = result_type;
2526 type->function.parameters = parameter;
2528 type_t *result = typehash_insert(type);
2529 if(result != type) {
2536 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2538 switch(symbol->ID) {
2539 case T___builtin_alloca:
2540 return make_function_1_type(type_void_ptr, type_size_t);
2542 panic("not implemented builtin symbol found");
2547 * performs automatic type cast as described in § 6.3.2.1
2549 static type_t *automatic_type_conversion(type_t *type)
2554 if(type->type == TYPE_ARRAY) {
2555 array_type_t *array_type = (array_type_t*) type;
2556 type_t *element_type = array_type->element_type;
2557 unsigned qualifiers = array_type->type.qualifiers;
2559 return make_pointer_type(element_type, qualifiers);
2562 if(type->type == TYPE_FUNCTION) {
2563 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2570 * reverts the automatic casts of array to pointer types and function
2571 * to function-pointer types as defined § 6.3.2.1
2573 type_t *revert_automatic_type_conversion(const expression_t *expression)
2575 if(expression->datatype == NULL)
2578 switch(expression->type) {
2579 case EXPR_REFERENCE: {
2580 const reference_expression_t *ref
2581 = (const reference_expression_t*) expression;
2582 return ref->declaration->type;
2585 const select_expression_t *select
2586 = (const select_expression_t*) expression;
2587 return select->compound_entry->type;
2590 const unary_expression_t *unary
2591 = (const unary_expression_t*) expression;
2592 if(unary->type == UNEXPR_DEREFERENCE) {
2593 expression_t *value = unary->value;
2594 type_t *type = skip_typeref(value->datatype);
2595 pointer_type_t *pointer_type = (pointer_type_t*) type;
2597 return pointer_type->points_to;
2601 case EXPR_BUILTIN_SYMBOL: {
2602 const builtin_symbol_expression_t *builtin
2603 = (const builtin_symbol_expression_t*) expression;
2604 return get_builtin_symbol_type(builtin->symbol);
2606 case EXPR_ARRAY_ACCESS: {
2607 const array_access_expression_t *array_access
2608 = (const array_access_expression_t*) expression;
2609 type_t *type_left = skip_typeref(array_access->array_ref->datatype);
2610 assert(is_type_pointer(type_left));
2611 pointer_type_t *pointer_type = (pointer_type_t*) type_left;
2612 return pointer_type->points_to;
2619 return expression->datatype;
2622 static expression_t *parse_reference(void)
2624 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2626 ref->expression.type = EXPR_REFERENCE;
2627 ref->symbol = token.v.symbol;
2629 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2631 source_position_t source_position = token.source_position;
2634 if(declaration == NULL) {
2636 /* an implicitly defined function */
2637 if(token.type == '(') {
2638 parser_print_prefix_pos(token.source_position);
2639 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2640 ref->symbol->string);
2642 declaration = create_implicit_function(ref->symbol,
2647 parser_print_error_prefix();
2648 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2649 return (expression_t*) ref;
2653 type_t *type = declaration->type;
2654 /* we always do the auto-type conversions; the & and sizeof parser contains
2655 * code to revert this! */
2656 type = automatic_type_conversion(type);
2658 ref->declaration = declaration;
2659 ref->expression.datatype = type;
2661 return (expression_t*) ref;
2664 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2668 /* TODO check if explicit cast is allowed and issue warnings/errors */
2671 static expression_t *parse_cast(void)
2673 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2675 cast->expression.type = EXPR_UNARY;
2676 cast->type = UNEXPR_CAST;
2677 cast->expression.source_position = token.source_position;
2679 type_t *type = parse_typename();
2682 expression_t *value = parse_sub_expression(20);
2684 check_cast_allowed(value, type);
2686 cast->expression.datatype = type;
2687 cast->value = value;
2689 return (expression_t*) cast;
2692 static expression_t *parse_statement_expression(void)
2694 statement_expression_t *expression
2695 = allocate_ast_zero(sizeof(expression[0]));
2696 expression->expression.type = EXPR_STATEMENT;
2698 statement_t *statement = parse_compound_statement();
2699 expression->statement = statement;
2700 if(statement == NULL) {
2705 assert(statement->type == STATEMENT_COMPOUND);
2706 compound_statement_t *compound_statement
2707 = (compound_statement_t*) statement;
2709 /* find last statement and use it's type */
2710 const statement_t *last_statement = NULL;
2711 const statement_t *iter = compound_statement->statements;
2712 for( ; iter != NULL; iter = iter->next) {
2713 last_statement = iter;
2716 if(last_statement->type == STATEMENT_EXPRESSION) {
2717 const expression_statement_t *expression_statement =
2718 (const expression_statement_t*) last_statement;
2719 expression->expression.datatype
2720 = expression_statement->expression->datatype;
2722 expression->expression.datatype = type_void;
2727 return (expression_t*) expression;
2730 static expression_t *parse_brace_expression(void)
2734 switch(token.type) {
2736 /* gcc extension: a stement expression */
2737 return parse_statement_expression();
2741 return parse_cast();
2743 if(is_typedef_symbol(token.v.symbol)) {
2744 return parse_cast();
2748 expression_t *result = parse_expression();
2754 static expression_t *parse_function_keyword(void)
2759 if (current_function == NULL) {
2760 parse_error("'__func__' used outside of a function");
2763 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2764 expression->expression.type = EXPR_FUNCTION;
2765 expression->expression.datatype = type_string;
2766 expression->value = "TODO: FUNCTION";
2768 return (expression_t*) expression;
2771 static expression_t *parse_pretty_function_keyword(void)
2773 eat(T___PRETTY_FUNCTION__);
2776 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2777 expression->expression.type = EXPR_PRETTY_FUNCTION;
2778 expression->expression.datatype = type_string;
2779 expression->value = "TODO: PRETTY FUNCTION";
2781 return (expression_t*) expression;
2784 static designator_t *parse_designator(void)
2786 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2788 if(token.type != T_IDENTIFIER) {
2789 parse_error_expected("while parsing member designator",
2794 result->symbol = token.v.symbol;
2797 designator_t *last_designator = result;
2799 if(token.type == '.') {
2801 if(token.type != T_IDENTIFIER) {
2802 parse_error_expected("while parsing member designator",
2807 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2808 designator->symbol = token.v.symbol;
2811 last_designator->next = designator;
2812 last_designator = designator;
2815 if(token.type == '[') {
2817 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2818 designator->array_access = parse_expression();
2819 if(designator->array_access == NULL) {
2825 last_designator->next = designator;
2826 last_designator = designator;
2835 static expression_t *parse_offsetof(void)
2837 eat(T___builtin_offsetof);
2839 offsetof_expression_t *expression
2840 = allocate_ast_zero(sizeof(expression[0]));
2841 expression->expression.type = EXPR_OFFSETOF;
2842 expression->expression.datatype = type_size_t;
2845 expression->type = parse_typename();
2847 expression->designator = parse_designator();
2850 return (expression_t*) expression;
2853 static expression_t *parse_va_arg(void)
2855 eat(T___builtin_va_arg);
2857 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2858 expression->expression.type = EXPR_VA_ARG;
2861 expression->arg = parse_assignment_expression();
2863 expression->expression.datatype = parse_typename();
2866 return (expression_t*) expression;
2869 static expression_t *parse_builtin_symbol(void)
2871 builtin_symbol_expression_t *expression
2872 = allocate_ast_zero(sizeof(expression[0]));
2873 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2875 expression->symbol = token.v.symbol;
2878 type_t *type = get_builtin_symbol_type(expression->symbol);
2879 type = automatic_type_conversion(type);
2881 expression->expression.datatype = type;
2882 return (expression_t*) expression;
2885 static expression_t *parse_primary_expression(void)
2887 switch(token.type) {
2889 return parse_int_const();
2890 case T_FLOATINGPOINT:
2891 return parse_float_const();
2892 case T_STRING_LITERAL:
2893 return parse_string_const();
2895 return parse_reference();
2896 case T___FUNCTION__:
2898 return parse_function_keyword();
2899 case T___PRETTY_FUNCTION__:
2900 return parse_pretty_function_keyword();
2901 case T___builtin_offsetof:
2902 return parse_offsetof();
2903 case T___builtin_va_arg:
2904 return parse_va_arg();
2905 case T___builtin_alloca:
2906 case T___builtin_expect:
2907 case T___builtin_va_start:
2908 case T___builtin_va_end:
2909 return parse_builtin_symbol();
2912 return parse_brace_expression();
2915 parser_print_error_prefix();
2916 fprintf(stderr, "unexpected token ");
2917 print_token(stderr, &token);
2918 fprintf(stderr, "\n");
2921 return make_invalid_expression();
2924 static expression_t *parse_array_expression(unsigned precedence,
2931 expression_t *inside = parse_expression();
2933 array_access_expression_t *array_access
2934 = allocate_ast_zero(sizeof(array_access[0]));
2936 array_access->expression.type = EXPR_ARRAY_ACCESS;
2938 type_t *type_left = skip_typeref(left->datatype);
2939 type_t *type_inside = skip_typeref(inside->datatype);
2940 type_t *result_type;
2942 if(type_left != NULL && type_inside != NULL) {
2943 if(is_type_pointer(type_left)) {
2944 pointer_type_t *pointer = (pointer_type_t*) type_left;
2945 result_type = pointer->points_to;
2946 array_access->array_ref = left;
2947 array_access->index = inside;
2948 } else if(is_type_pointer(type_inside)) {
2949 pointer_type_t *pointer = (pointer_type_t*) type_inside;
2950 result_type = pointer->points_to;
2951 array_access->array_ref = inside;
2952 array_access->index = left;
2953 array_access->flipped = true;
2955 parser_print_error_prefix();
2956 fprintf(stderr, "array access on object with non-pointer types ");
2957 print_type_quoted(type_left);
2958 fprintf(stderr, ", ");
2959 print_type_quoted(type_inside);
2960 fprintf(stderr, "\n");
2963 array_access->array_ref = left;
2964 array_access->index = inside;
2967 if(token.type != ']') {
2968 parse_error_expected("Problem while parsing array access", ']', 0);
2969 return (expression_t*) array_access;
2973 result_type = automatic_type_conversion(result_type);
2974 array_access->expression.datatype = result_type;
2976 return (expression_t*) array_access;
2979 static bool is_declaration_specifier(const token_t *token,
2980 bool only_type_specifiers)
2982 switch(token->type) {
2986 return is_typedef_symbol(token->v.symbol);
2989 if(only_type_specifiers)
2998 static expression_t *parse_sizeof(unsigned precedence)
3002 sizeof_expression_t *sizeof_expression
3003 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3004 sizeof_expression->expression.type = EXPR_SIZEOF;
3005 sizeof_expression->expression.datatype = type_size_t;
3007 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3009 sizeof_expression->type = parse_typename();
3012 expression_t *expression = parse_sub_expression(precedence);
3013 expression->datatype = revert_automatic_type_conversion(expression);
3015 sizeof_expression->type = expression->datatype;
3016 sizeof_expression->size_expression = expression;
3019 return (expression_t*) sizeof_expression;
3022 static expression_t *parse_select_expression(unsigned precedence,
3023 expression_t *compound)
3026 assert(token.type == '.' || token.type == T_MINUSGREATER);
3028 bool is_pointer = (token.type == T_MINUSGREATER);
3031 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
3033 select->expression.type = EXPR_SELECT;
3034 select->compound = compound;
3036 if(token.type != T_IDENTIFIER) {
3037 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3038 return (expression_t*) select;
3040 symbol_t *symbol = token.v.symbol;
3041 select->symbol = symbol;
3044 type_t *orig_type = compound->datatype;
3045 if(orig_type == NULL)
3046 return make_invalid_expression();
3048 type_t *type = skip_typeref(orig_type);
3050 type_t *type_left = type;
3052 if(type->type != TYPE_POINTER) {
3053 parser_print_error_prefix();
3054 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3055 print_type_quoted(orig_type);
3056 fputc('\n', stderr);
3057 return make_invalid_expression();
3059 pointer_type_t *pointer_type = (pointer_type_t*) type;
3060 type_left = pointer_type->points_to;
3062 type_left = skip_typeref(type_left);
3064 if(type_left->type != TYPE_COMPOUND_STRUCT
3065 && type_left->type != TYPE_COMPOUND_UNION) {
3066 parser_print_error_prefix();
3067 fprintf(stderr, "request for member '%s' in something not a struct or "
3068 "union, but ", symbol->string);
3069 print_type_quoted(type_left);
3070 fputc('\n', stderr);
3071 return make_invalid_expression();
3074 compound_type_t *compound_type = (compound_type_t*) type_left;
3075 declaration_t *declaration = compound_type->declaration;
3077 if(!declaration->init.is_defined) {
3078 parser_print_error_prefix();
3079 fprintf(stderr, "request for member '%s' of incomplete type ",
3081 print_type_quoted(type_left);
3082 fputc('\n', stderr);
3083 return make_invalid_expression();
3086 declaration_t *iter = declaration->context.declarations;
3087 for( ; iter != NULL; iter = iter->next) {
3088 if(iter->symbol == symbol) {
3093 parser_print_error_prefix();
3094 print_type_quoted(type_left);
3095 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3096 return make_invalid_expression();
3099 /* we always do the auto-type conversions; the & and sizeof parser contains
3100 * code to revert this! */
3101 type_t *expression_type = automatic_type_conversion(iter->type);
3103 select->compound_entry = iter;
3104 select->expression.datatype = expression_type;
3105 return (expression_t*) select;
3108 static expression_t *parse_call_expression(unsigned precedence,
3109 expression_t *expression)
3112 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3113 call->expression.type = EXPR_CALL;
3114 call->function = expression;
3116 function_type_t *function_type;
3117 type_t *orig_type = expression->datatype;
3118 if(orig_type != NULL) {
3119 function_type = NULL;
3120 type_t *type = skip_typeref(orig_type);
3122 if(is_type_pointer(type)) {
3123 pointer_type_t *pointer_type = (pointer_type_t*) type;
3125 type = skip_typeref(pointer_type->points_to);
3127 if (type->type == TYPE_FUNCTION) {
3128 function_type = (function_type_t*) type;
3129 call->expression.datatype = function_type->result_type;
3132 if(function_type == NULL) {
3133 parser_print_error_prefix();
3134 fputs("called object '", stderr);
3135 print_expression(expression);
3136 fputs("' (type ", stderr);
3137 print_type_quoted(orig_type);
3138 fputs(") is not a pointer to a function\n", stderr);
3140 function_type = NULL;
3141 call->expression.datatype = NULL;
3145 /* parse arguments */
3148 if(token.type != ')') {
3149 call_argument_t *last_argument = NULL;
3152 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3154 argument->expression = parse_assignment_expression();
3155 if(last_argument == NULL) {
3156 call->arguments = argument;
3158 last_argument->next = argument;
3160 last_argument = argument;
3162 if(token.type != ',')
3169 if(function_type != NULL) {
3170 function_parameter_t *parameter = function_type->parameters;
3171 call_argument_t *argument = call->arguments;
3172 for( ; parameter != NULL && argument != NULL;
3173 parameter = parameter->next, argument = argument->next) {
3174 type_t *expected_type = parameter->type;
3175 /* TODO report context in error messages */
3176 argument->expression = create_implicit_cast(argument->expression,
3179 /* too few parameters */
3180 if(parameter != NULL) {
3181 parser_print_error_prefix();
3182 fprintf(stderr, "too few arguments to function '");
3183 print_expression(expression);
3184 fprintf(stderr, "'\n");
3185 } else if(argument != NULL) {
3186 /* too many parameters */
3187 if(!function_type->variadic
3188 && !function_type->unspecified_parameters) {
3189 parser_print_error_prefix();
3190 fprintf(stderr, "too many arguments to function '");
3191 print_expression(expression);
3192 fprintf(stderr, "'\n");
3194 /* do default promotion */
3195 for( ; argument != NULL; argument = argument->next) {
3196 type_t *type = argument->expression->datatype;
3197 type = skip_typeref(type);
3202 if(is_type_integer(type)) {
3203 type = promote_integer(type);
3204 } else if(type == type_float) {
3208 argument->expression
3209 = create_implicit_cast(argument->expression, type);
3215 return (expression_t*) call;
3218 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3220 static expression_t *parse_conditional_expression(unsigned precedence,
3221 expression_t *expression)
3225 conditional_expression_t *conditional
3226 = allocate_ast_zero(sizeof(conditional[0]));
3227 conditional->expression.type = EXPR_CONDITIONAL;
3228 conditional->condition = expression;
3231 type_t *condition_type_orig = conditional->condition->datatype;
3232 if(condition_type_orig != NULL) {
3233 type_t *condition_type = skip_typeref(condition_type_orig);
3234 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3235 type_error("expected a scalar type", expression->source_position,
3236 condition_type_orig);
3240 expression_t *const t_expr = parse_expression();
3241 conditional->true_expression = t_expr;
3243 expression_t *const f_expr = parse_sub_expression(precedence);
3244 conditional->false_expression = f_expr;
3246 type_t *const true_type = t_expr->datatype;
3247 if(true_type == NULL)
3248 return (expression_t*) conditional;
3249 type_t *const false_type = f_expr->datatype;
3250 if(false_type == NULL)
3251 return (expression_t*) conditional;
3253 type_t *const skipped_true_type = skip_typeref(true_type);
3254 type_t *const skipped_false_type = skip_typeref(false_type);
3257 if (skipped_true_type == skipped_false_type) {
3258 conditional->expression.datatype = skipped_true_type;
3259 } else if (is_type_arithmetic(skipped_true_type) &&
3260 is_type_arithmetic(skipped_false_type)) {
3261 type_t *const result = semantic_arithmetic(skipped_true_type,
3262 skipped_false_type);
3263 conditional->true_expression = create_implicit_cast(t_expr, result);
3264 conditional->false_expression = create_implicit_cast(f_expr, result);
3265 conditional->expression.datatype = result;
3266 } else if (skipped_true_type->type == TYPE_POINTER &&
3267 skipped_false_type->type == TYPE_POINTER &&
3268 true /* TODO compatible points_to types */) {
3270 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3271 skipped_false_type->type == TYPE_POINTER)
3272 || (is_null_ptr_const(skipped_false_type) &&
3273 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3275 } else if(/* 1 is pointer to object type, other is void* */ false) {
3278 type_error_incompatible("while parsing conditional",
3279 expression->source_position, true_type,
3280 skipped_false_type);
3283 return (expression_t*) conditional;
3286 static expression_t *parse_extension(unsigned precedence)
3288 eat(T___extension__);
3290 /* TODO enable extensions */
3292 return parse_sub_expression(precedence);
3295 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3297 eat(T___builtin_classify_type);
3299 classify_type_expression_t *const classify_type_expr =
3300 allocate_ast_zero(sizeof(classify_type_expr[0]));
3301 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3302 classify_type_expr->expression.datatype = type_int;
3305 expression_t *const expression = parse_sub_expression(precedence);
3307 classify_type_expr->type_expression = expression;
3309 return (expression_t*)classify_type_expr;
3312 static void semantic_incdec(unary_expression_t *expression)
3314 type_t *orig_type = expression->value->datatype;
3315 if(orig_type == NULL)
3318 type_t *type = skip_typeref(orig_type);
3319 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3320 /* TODO: improve error message */
3321 parser_print_error_prefix();
3322 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3326 expression->expression.datatype = orig_type;
3329 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3331 type_t *orig_type = expression->value->datatype;
3332 if(orig_type == NULL)
3335 type_t *type = skip_typeref(orig_type);
3336 if(!is_type_arithmetic(type)) {
3337 /* TODO: improve error message */
3338 parser_print_error_prefix();
3339 fprintf(stderr, "operation needs an arithmetic type\n");
3343 expression->expression.datatype = orig_type;
3346 static void semantic_unexpr_scalar(unary_expression_t *expression)
3348 type_t *orig_type = expression->value->datatype;
3349 if(orig_type == NULL)
3352 type_t *type = skip_typeref(orig_type);
3353 if (!is_type_scalar(type)) {
3354 parse_error("operand of ! must be of scalar type\n");
3358 expression->expression.datatype = orig_type;
3361 static void semantic_unexpr_integer(unary_expression_t *expression)
3363 type_t *orig_type = expression->value->datatype;
3364 if(orig_type == NULL)
3367 type_t *type = skip_typeref(orig_type);
3368 if (!is_type_integer(type)) {
3369 parse_error("operand of ~ must be of integer type\n");
3373 expression->expression.datatype = orig_type;
3376 static void semantic_dereference(unary_expression_t *expression)
3378 type_t *orig_type = expression->value->datatype;
3379 if(orig_type == NULL)
3382 type_t *type = skip_typeref(orig_type);
3383 if(!is_type_pointer(type)) {
3384 parser_print_error_prefix();
3385 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3386 print_type_quoted(orig_type);
3387 fputs(" given.\n", stderr);
3391 pointer_type_t *pointer_type = (pointer_type_t*)type;
3392 type_t *result_type = pointer_type->points_to;
3394 result_type = automatic_type_conversion(result_type);
3395 expression->expression.datatype = result_type;
3398 static void semantic_take_addr(unary_expression_t *expression)
3400 expression_t *value = expression->value;
3401 value->datatype = revert_automatic_type_conversion(value);
3403 type_t *orig_type = value->datatype;
3404 if(orig_type == NULL)
3407 if(value->type == EXPR_REFERENCE) {
3408 reference_expression_t *reference = (reference_expression_t*) value;
3409 declaration_t *declaration = reference->declaration;
3410 if(declaration != NULL) {
3411 declaration->address_taken = 1;
3415 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3418 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3419 static expression_t *parse_##unexpression_type(unsigned precedence) \
3423 unary_expression_t *unary_expression \
3424 = allocate_ast_zero(sizeof(unary_expression[0])); \
3425 unary_expression->expression.type = EXPR_UNARY; \
3426 unary_expression->type = unexpression_type; \
3427 unary_expression->value = parse_sub_expression(precedence); \
3429 sfunc(unary_expression); \
3431 return (expression_t*) unary_expression; \
3434 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3435 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3436 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3437 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3438 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3439 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3440 semantic_unexpr_integer)
3441 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3443 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3446 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3448 static expression_t *parse_##unexpression_type(unsigned precedence, \
3449 expression_t *left) \
3451 (void) precedence; \
3454 unary_expression_t *unary_expression \
3455 = allocate_ast_zero(sizeof(unary_expression[0])); \
3456 unary_expression->expression.type = EXPR_UNARY; \
3457 unary_expression->type = unexpression_type; \
3458 unary_expression->value = left; \
3460 sfunc(unary_expression); \
3462 return (expression_t*) unary_expression; \
3465 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3467 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3470 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3472 /* TODO: handle complex + imaginary types */
3474 /* § 6.3.1.8 Usual arithmetic conversions */
3475 if(type_left == type_long_double || type_right == type_long_double) {
3476 return type_long_double;
3477 } else if(type_left == type_double || type_right == type_double) {
3479 } else if(type_left == type_float || type_right == type_float) {
3483 type_right = promote_integer(type_right);
3484 type_left = promote_integer(type_left);
3486 if(type_left == type_right)
3489 bool signed_left = is_type_signed(type_left);
3490 bool signed_right = is_type_signed(type_right);
3491 if(get_rank(type_left) < get_rank(type_right)) {
3492 if(signed_left == signed_right || !signed_right) {
3498 if(signed_left == signed_right || !signed_left) {
3506 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3508 expression_t *left = expression->left;
3509 expression_t *right = expression->right;
3510 type_t *orig_type_left = left->datatype;
3511 type_t *orig_type_right = right->datatype;
3513 if(orig_type_left == NULL || orig_type_right == NULL)
3516 type_t *type_left = skip_typeref(orig_type_left);
3517 type_t *type_right = skip_typeref(orig_type_right);
3519 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3520 /* TODO: improve error message */
3521 parser_print_error_prefix();
3522 fprintf(stderr, "operation needs arithmetic types\n");
3526 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3527 expression->left = create_implicit_cast(left, arithmetic_type);
3528 expression->right = create_implicit_cast(right, arithmetic_type);
3529 expression->expression.datatype = arithmetic_type;
3532 static void semantic_shift_op(binary_expression_t *expression)
3534 expression_t *left = expression->left;
3535 expression_t *right = expression->right;
3536 type_t *orig_type_left = left->datatype;
3537 type_t *orig_type_right = right->datatype;
3539 if(orig_type_left == NULL || orig_type_right == NULL)
3542 type_t *type_left = skip_typeref(orig_type_left);
3543 type_t *type_right = skip_typeref(orig_type_right);
3545 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3546 /* TODO: improve error message */
3547 parser_print_error_prefix();
3548 fprintf(stderr, "operation needs integer types\n");
3552 type_left = promote_integer(type_left);
3553 type_right = promote_integer(type_right);
3555 expression->left = create_implicit_cast(left, type_left);
3556 expression->right = create_implicit_cast(right, type_right);
3557 expression->expression.datatype = type_left;
3560 static void semantic_add(binary_expression_t *expression)
3562 expression_t *left = expression->left;
3563 expression_t *right = expression->right;
3564 type_t *orig_type_left = left->datatype;
3565 type_t *orig_type_right = right->datatype;
3567 if(orig_type_left == NULL || orig_type_right == NULL)
3570 type_t *type_left = skip_typeref(orig_type_left);
3571 type_t *type_right = skip_typeref(orig_type_right);
3574 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3575 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3576 expression->left = create_implicit_cast(left, arithmetic_type);
3577 expression->right = create_implicit_cast(right, arithmetic_type);
3578 expression->expression.datatype = arithmetic_type;
3580 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3581 expression->expression.datatype = type_left;
3582 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3583 expression->expression.datatype = type_right;
3585 parser_print_error_prefix();
3586 fprintf(stderr, "invalid operands to binary + (");
3587 print_type_quoted(orig_type_left);
3588 fprintf(stderr, ", ");
3589 print_type_quoted(orig_type_right);
3590 fprintf(stderr, ")\n");
3594 static void semantic_sub(binary_expression_t *expression)
3596 expression_t *left = expression->left;
3597 expression_t *right = expression->right;
3598 type_t *orig_type_left = left->datatype;
3599 type_t *orig_type_right = right->datatype;
3601 if(orig_type_left == NULL || orig_type_right == NULL)
3604 type_t *type_left = skip_typeref(orig_type_left);
3605 type_t *type_right = skip_typeref(orig_type_right);
3608 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3609 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3610 expression->left = create_implicit_cast(left, arithmetic_type);
3611 expression->right = create_implicit_cast(right, arithmetic_type);
3612 expression->expression.datatype = arithmetic_type;
3614 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3615 expression->expression.datatype = type_left;
3616 } else if(type_left->type == TYPE_POINTER &&
3617 type_right->type == TYPE_POINTER) {
3618 if(!pointers_compatible(type_left, type_right)) {
3619 parser_print_error_prefix();
3620 fprintf(stderr, "pointers to incompatible objects to binary - (");
3621 print_type_quoted(orig_type_left);
3622 fprintf(stderr, ", ");
3623 print_type_quoted(orig_type_right);
3624 fprintf(stderr, ")\n");
3626 expression->expression.datatype = type_ptrdiff_t;
3629 parser_print_error_prefix();
3630 fprintf(stderr, "invalid operands to binary - (");
3631 print_type_quoted(orig_type_left);
3632 fprintf(stderr, ", ");
3633 print_type_quoted(orig_type_right);
3634 fprintf(stderr, ")\n");
3638 static void semantic_comparison(binary_expression_t *expression)
3640 expression_t *left = expression->left;
3641 expression_t *right = expression->right;
3642 type_t *orig_type_left = left->datatype;
3643 type_t *orig_type_right = right->datatype;
3645 if(orig_type_left == NULL || orig_type_right == NULL)
3648 type_t *type_left = skip_typeref(orig_type_left);
3649 type_t *type_right = skip_typeref(orig_type_right);
3651 /* TODO non-arithmetic types */
3652 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3653 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3654 expression->left = create_implicit_cast(left, arithmetic_type);
3655 expression->right = create_implicit_cast(right, arithmetic_type);
3656 expression->expression.datatype = arithmetic_type;
3657 } else if (type_left->type == TYPE_POINTER &&
3658 type_right->type == TYPE_POINTER) {
3659 /* TODO check compatibility */
3660 } else if (type_left->type == TYPE_POINTER) {
3661 expression->right = create_implicit_cast(right, type_left);
3662 } else if (type_right->type == TYPE_POINTER) {
3663 expression->left = create_implicit_cast(left, type_right);
3665 type_error_incompatible("invalid operands in comparison",
3666 token.source_position, type_left, type_right);
3668 expression->expression.datatype = type_int;
3671 static void semantic_arithmetic_assign(binary_expression_t *expression)
3673 expression_t *left = expression->left;
3674 expression_t *right = expression->right;
3675 type_t *orig_type_left = left->datatype;
3676 type_t *orig_type_right = right->datatype;
3678 if(orig_type_left == NULL || orig_type_right == NULL)
3681 type_t *type_left = skip_typeref(orig_type_left);
3682 type_t *type_right = skip_typeref(orig_type_right);
3684 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3685 /* TODO: improve error message */
3686 parser_print_error_prefix();
3687 fprintf(stderr, "operation needs arithmetic types\n");
3691 /* combined instructions are tricky. We can't create an implicit cast on
3692 * the left side, because we need the uncasted form for the store.
3693 * The ast2firm pass has to know that left_type must be right_type
3694 * for the arithmeitc operation and create a cast by itself */
3695 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3696 expression->right = create_implicit_cast(right, arithmetic_type);
3697 expression->expression.datatype = type_left;
3700 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3702 expression_t *left = expression->left;
3703 expression_t *right = expression->right;
3704 type_t *orig_type_left = left->datatype;
3705 type_t *orig_type_right = right->datatype;
3707 if(orig_type_left == NULL || orig_type_right == NULL)
3710 type_t *type_left = skip_typeref(orig_type_left);
3711 type_t *type_right = skip_typeref(orig_type_right);
3713 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3714 /* combined instructions are tricky. We can't create an implicit cast on
3715 * the left side, because we need the uncasted form for the store.
3716 * The ast2firm pass has to know that left_type must be right_type
3717 * for the arithmeitc operation and create a cast by itself */
3718 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3719 expression->right = create_implicit_cast(right, arithmetic_type);
3720 expression->expression.datatype = type_left;
3721 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3722 expression->expression.datatype = type_left;
3724 parser_print_error_prefix();
3725 fputs("Incompatible types ", stderr);
3726 print_type_quoted(orig_type_left);
3727 fputs(" and ", stderr);
3728 print_type_quoted(orig_type_right);
3729 fputs(" in assignment\n", stderr);
3734 static void semantic_logical_op(binary_expression_t *expression)
3736 expression_t *left = expression->left;
3737 expression_t *right = expression->right;
3738 type_t *orig_type_left = left->datatype;
3739 type_t *orig_type_right = right->datatype;
3741 if(orig_type_left == NULL || orig_type_right == NULL)
3744 type_t *type_left = skip_typeref(orig_type_left);
3745 type_t *type_right = skip_typeref(orig_type_right);
3747 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3748 /* TODO: improve error message */
3749 parser_print_error_prefix();
3750 fprintf(stderr, "operation needs scalar types\n");
3754 expression->expression.datatype = type_int;
3757 static bool has_const_fields(type_t *type)
3764 static void semantic_binexpr_assign(binary_expression_t *expression)
3766 expression_t *left = expression->left;
3767 type_t *orig_type_left = left->datatype;
3769 if(orig_type_left == NULL)
3772 type_t *type_left = revert_automatic_type_conversion(left);
3773 type_left = skip_typeref(orig_type_left);
3775 /* must be a modifiable lvalue */
3776 if (type_left->type == TYPE_ARRAY) {
3777 parser_print_error_prefix();
3778 fprintf(stderr, "Cannot assign to arrays ('");
3779 print_expression(left);
3780 fprintf(stderr, "')\n");
3783 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3784 parser_print_error_prefix();
3785 fprintf(stderr, "assignment to readonly location '");
3786 print_expression(left);
3787 fprintf(stderr, "' (type ");
3788 print_type_quoted(orig_type_left);
3789 fprintf(stderr, ")\n");
3792 if(is_type_incomplete(type_left)) {
3793 parser_print_error_prefix();
3794 fprintf(stderr, "left-hand side of assignment '");
3795 print_expression(left);
3796 fprintf(stderr, "' has incomplete type ");
3797 print_type_quoted(orig_type_left);
3798 fprintf(stderr, "\n");
3801 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3802 parser_print_error_prefix();
3803 fprintf(stderr, "can't assign to '");
3804 print_expression(left);
3805 fprintf(stderr, "' because compound type ");
3806 print_type_quoted(orig_type_left);
3807 fprintf(stderr, " has readonly fields\n");
3811 semantic_assign(orig_type_left, &expression->right, "assignment");
3813 expression->expression.datatype = orig_type_left;
3816 static void semantic_comma(binary_expression_t *expression)
3818 expression->expression.datatype = expression->right->datatype;
3821 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3822 static expression_t *parse_##binexpression_type(unsigned precedence, \
3823 expression_t *left) \
3827 expression_t *right = parse_sub_expression(precedence + lr); \
3829 binary_expression_t *binexpr \
3830 = allocate_ast_zero(sizeof(binexpr[0])); \
3831 binexpr->expression.type = EXPR_BINARY; \
3832 binexpr->type = binexpression_type; \
3833 binexpr->left = left; \
3834 binexpr->right = right; \
3837 return (expression_t*) binexpr; \
3840 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3841 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3842 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3843 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3844 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3845 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3846 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3847 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3848 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3849 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3850 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3851 semantic_comparison, 1)
3852 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3853 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3854 semantic_comparison, 1)
3855 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3856 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3857 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3858 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3859 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3860 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3861 semantic_shift_op, 1)
3862 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3863 semantic_shift_op, 1)
3864 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3865 semantic_arithmetic_addsubb_assign, 0)
3866 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3867 semantic_arithmetic_addsubb_assign, 0)
3868 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3869 semantic_arithmetic_assign, 0)
3870 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3871 semantic_arithmetic_assign, 0)
3872 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3873 semantic_arithmetic_assign, 0)
3874 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3875 semantic_arithmetic_assign, 0)
3876 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3877 semantic_arithmetic_assign, 0)
3878 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3879 semantic_arithmetic_assign, 0)
3880 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3881 semantic_arithmetic_assign, 0)
3882 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3883 semantic_arithmetic_assign, 0)
3885 static expression_t *parse_sub_expression(unsigned precedence)
3887 if(token.type < 0) {
3888 return expected_expression_error();
3891 expression_parser_function_t *parser
3892 = &expression_parsers[token.type];
3893 source_position_t source_position = token.source_position;
3896 if(parser->parser != NULL) {
3897 left = parser->parser(parser->precedence);
3899 left = parse_primary_expression();
3901 assert(left != NULL);
3902 left->source_position = source_position;
3905 if(token.type < 0) {
3906 return expected_expression_error();
3909 parser = &expression_parsers[token.type];
3910 if(parser->infix_parser == NULL)
3912 if(parser->infix_precedence < precedence)
3915 left = parser->infix_parser(parser->infix_precedence, left);
3917 assert(left != NULL);
3918 assert(left->type != EXPR_UNKNOWN);
3919 left->source_position = source_position;
3925 static expression_t *parse_expression(void)
3927 return parse_sub_expression(1);
3932 static void register_expression_parser(parse_expression_function parser,
3933 int token_type, unsigned precedence)
3935 expression_parser_function_t *entry = &expression_parsers[token_type];
3937 if(entry->parser != NULL) {
3938 fprintf(stderr, "for token ");
3939 print_token_type(stderr, token_type);
3940 fprintf(stderr, "\n");
3941 panic("trying to register multiple expression parsers for a token");
3943 entry->parser = parser;
3944 entry->precedence = precedence;
3947 static void register_expression_infix_parser(
3948 parse_expression_infix_function parser, int token_type,
3949 unsigned precedence)
3951 expression_parser_function_t *entry = &expression_parsers[token_type];
3953 if(entry->infix_parser != NULL) {
3954 fprintf(stderr, "for token ");
3955 print_token_type(stderr, token_type);
3956 fprintf(stderr, "\n");
3957 panic("trying to register multiple infix expression parsers for a "
3960 entry->infix_parser = parser;
3961 entry->infix_precedence = precedence;
3964 static void init_expression_parsers(void)
3966 memset(&expression_parsers, 0, sizeof(expression_parsers));
3968 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3969 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3970 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3971 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3972 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3973 T_GREATERGREATER, 16);
3974 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3975 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3976 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3977 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3978 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3979 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3980 T_GREATEREQUAL, 14);
3981 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3982 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3983 T_EXCLAMATIONMARKEQUAL, 13);
3984 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3985 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3986 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3987 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3988 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3989 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3990 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3991 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3992 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3993 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3994 T_ASTERISKEQUAL, 2);
3995 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3996 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3998 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3999 T_LESSLESSEQUAL, 2);
4000 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
4001 T_GREATERGREATEREQUAL, 2);
4002 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
4004 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
4006 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4009 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4011 register_expression_infix_parser(parse_array_expression, '[', 30);
4012 register_expression_infix_parser(parse_call_expression, '(', 30);
4013 register_expression_infix_parser(parse_select_expression, '.', 30);
4014 register_expression_infix_parser(parse_select_expression,
4015 T_MINUSGREATER, 30);
4016 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4018 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4021 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4022 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4023 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4024 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4025 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4026 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4027 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4028 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4029 register_expression_parser(parse_sizeof, T_sizeof, 25);
4030 register_expression_parser(parse_extension, T___extension__, 25);
4031 register_expression_parser(parse_builtin_classify_type,
4032 T___builtin_classify_type, 25);
4036 static statement_t *parse_case_statement(void)
4039 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4040 label->statement.type = STATEMENT_CASE_LABEL;
4041 label->statement.source_position = token.source_position;
4043 label->expression = parse_expression();
4046 label->label_statement = parse_statement();
4048 return (statement_t*) label;
4051 static statement_t *parse_default_statement(void)
4055 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4056 label->statement.type = STATEMENT_CASE_LABEL;
4057 label->statement.source_position = token.source_position;
4060 label->label_statement = parse_statement();
4062 return (statement_t*) label;
4065 static declaration_t *get_label(symbol_t *symbol)
4067 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4068 assert(current_function != NULL);
4069 /* if we found a label in the same function, then we already created the
4071 if(candidate != NULL
4072 && candidate->parent_context == ¤t_function->context) {
4076 /* otherwise we need to create a new one */
4077 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4078 declaration->namespc = NAMESPACE_LABEL;
4079 declaration->symbol = symbol;
4081 label_push(declaration);
4086 static statement_t *parse_label_statement(void)
4088 assert(token.type == T_IDENTIFIER);
4089 symbol_t *symbol = token.v.symbol;
4092 declaration_t *label = get_label(symbol);
4094 /* if source position is already set then the label is defined twice,
4095 * otherwise it was just mentioned in a goto so far */
4096 if(label->source_position.input_name != NULL) {
4097 parser_print_error_prefix();
4098 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4099 parser_print_error_prefix_pos(label->source_position);
4100 fprintf(stderr, "previous definition of '%s' was here\n",
4103 label->source_position = token.source_position;
4106 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4108 label_statement->statement.type = STATEMENT_LABEL;
4109 label_statement->statement.source_position = token.source_position;
4110 label_statement->label = label;
4114 if(token.type == '}') {
4115 parse_error("label at end of compound statement");
4116 return (statement_t*) label_statement;
4118 label_statement->label_statement = parse_statement();
4121 return (statement_t*) label_statement;
4124 static statement_t *parse_if(void)
4128 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4129 statement->statement.type = STATEMENT_IF;
4130 statement->statement.source_position = token.source_position;
4133 statement->condition = parse_expression();
4136 statement->true_statement = parse_statement();
4137 if(token.type == T_else) {
4139 statement->false_statement = parse_statement();
4142 return (statement_t*) statement;
4145 static statement_t *parse_switch(void)
4149 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4150 statement->statement.type = STATEMENT_SWITCH;
4151 statement->statement.source_position = token.source_position;
4154 statement->expression = parse_expression();
4156 statement->body = parse_statement();
4158 return (statement_t*) statement;
4161 static statement_t *parse_while(void)
4165 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4166 statement->statement.type = STATEMENT_WHILE;
4167 statement->statement.source_position = token.source_position;
4170 statement->condition = parse_expression();
4172 statement->body = parse_statement();
4174 return (statement_t*) statement;
4177 static statement_t *parse_do(void)
4181 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4182 statement->statement.type = STATEMENT_DO_WHILE;
4183 statement->statement.source_position = token.source_position;
4185 statement->body = parse_statement();
4188 statement->condition = parse_expression();
4192 return (statement_t*) statement;
4195 static statement_t *parse_for(void)
4199 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4200 statement->statement.type = STATEMENT_FOR;
4201 statement->statement.source_position = token.source_position;
4205 int top = environment_top();
4206 context_t *last_context = context;
4207 set_context(&statement->context);
4209 if(token.type != ';') {
4210 if(is_declaration_specifier(&token, false)) {
4211 parse_declaration();
4213 statement->initialisation = parse_expression();
4220 if(token.type != ';') {
4221 statement->condition = parse_expression();
4224 if(token.type != ')') {
4225 statement->step = parse_expression();
4228 statement->body = parse_statement();
4230 assert(context == &statement->context);
4231 set_context(last_context);
4232 environment_pop_to(top);
4234 return (statement_t*) statement;
4237 static statement_t *parse_goto(void)
4241 if(token.type != T_IDENTIFIER) {
4242 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4246 symbol_t *symbol = token.v.symbol;
4249 declaration_t *label = get_label(symbol);
4251 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4253 statement->statement.type = STATEMENT_GOTO;
4254 statement->statement.source_position = token.source_position;
4256 statement->label = label;
4260 return (statement_t*) statement;
4263 static statement_t *parse_continue(void)
4268 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4269 statement->type = STATEMENT_CONTINUE;
4270 statement->source_position = token.source_position;
4275 static statement_t *parse_break(void)
4280 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4281 statement->type = STATEMENT_BREAK;
4282 statement->source_position = token.source_position;
4287 static statement_t *parse_return(void)
4291 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4293 statement->statement.type = STATEMENT_RETURN;
4294 statement->statement.source_position = token.source_position;
4296 assert(current_function->type->type == TYPE_FUNCTION);
4297 function_type_t *function_type = (function_type_t*) current_function->type;
4298 type_t *return_type = function_type->result_type;
4300 expression_t *return_value;
4301 if(token.type != ';') {
4302 return_value = parse_expression();
4304 if(return_type == type_void && return_value->datatype != type_void) {
4305 parse_warning("'return' with a value, in function returning void");
4306 return_value = NULL;
4308 if(return_type != NULL) {
4309 semantic_assign(return_type, &return_value, "'return'");
4313 return_value = NULL;
4314 if(return_type != type_void) {
4315 parse_warning("'return' without value, in function returning "
4319 statement->return_value = return_value;
4323 return (statement_t*) statement;
4326 static statement_t *parse_declaration_statement(void)
4328 declaration_t *before = last_declaration;
4330 declaration_statement_t *statement
4331 = allocate_ast_zero(sizeof(statement[0]));
4332 statement->statement.type = STATEMENT_DECLARATION;
4333 statement->statement.source_position = token.source_position;
4335 declaration_specifiers_t specifiers;
4336 memset(&specifiers, 0, sizeof(specifiers));
4337 parse_declaration_specifiers(&specifiers);
4339 if(token.type == ';') {
4342 parse_init_declarators(&specifiers);
4345 if(before == NULL) {
4346 statement->declarations_begin = context->declarations;
4348 statement->declarations_begin = before->next;
4350 statement->declarations_end = last_declaration;
4352 return (statement_t*) statement;
4355 static statement_t *parse_expression_statement(void)
4357 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4358 statement->statement.type = STATEMENT_EXPRESSION;
4359 statement->statement.source_position = token.source_position;
4361 statement->expression = parse_expression();
4365 return (statement_t*) statement;
4368 static statement_t *parse_statement(void)
4370 statement_t *statement = NULL;
4372 /* declaration or statement */
4373 switch(token.type) {
4375 statement = parse_case_statement();
4379 statement = parse_default_statement();
4383 statement = parse_compound_statement();
4387 statement = parse_if();
4391 statement = parse_switch();
4395 statement = parse_while();
4399 statement = parse_do();
4403 statement = parse_for();
4407 statement = parse_goto();
4411 statement = parse_continue();
4415 statement = parse_break();
4419 statement = parse_return();
4428 if(look_ahead(1)->type == ':') {
4429 statement = parse_label_statement();
4433 if(is_typedef_symbol(token.v.symbol)) {
4434 statement = parse_declaration_statement();
4438 statement = parse_expression_statement();
4441 case T___extension__:
4442 /* this can be a prefix to a declaration or an expression statement */
4443 /* we simply eat it now and parse the rest with tail recursion */
4446 } while(token.type == T___extension__);
4447 statement = parse_statement();
4451 statement = parse_declaration_statement();
4455 statement = parse_expression_statement();
4459 assert(statement == NULL || statement->source_position.input_name != NULL);
4464 static statement_t *parse_compound_statement(void)
4466 compound_statement_t *compound_statement
4467 = allocate_ast_zero(sizeof(compound_statement[0]));
4468 compound_statement->statement.type = STATEMENT_COMPOUND;
4469 compound_statement->statement.source_position = token.source_position;
4473 int top = environment_top();
4474 context_t *last_context = context;
4475 set_context(&compound_statement->context);
4477 statement_t *last_statement = NULL;
4479 while(token.type != '}' && token.type != T_EOF) {
4480 statement_t *statement = parse_statement();
4481 if(statement == NULL)
4484 if(last_statement != NULL) {
4485 last_statement->next = statement;
4487 compound_statement->statements = statement;
4490 while(statement->next != NULL)
4491 statement = statement->next;
4493 last_statement = statement;
4496 if(token.type != '}') {
4497 parser_print_error_prefix_pos(
4498 compound_statement->statement.source_position);
4499 fprintf(stderr, "end of file while looking for closing '}'\n");
4503 assert(context == &compound_statement->context);
4504 set_context(last_context);
4505 environment_pop_to(top);
4507 return (statement_t*) compound_statement;
4510 static translation_unit_t *parse_translation_unit(void)
4512 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4514 assert(global_context == NULL);
4515 global_context = &unit->context;
4517 assert(context == NULL);
4518 set_context(&unit->context);
4520 while(token.type != T_EOF) {
4521 parse_declaration();
4524 assert(context == &unit->context);
4526 last_declaration = NULL;
4528 assert(global_context == &unit->context);
4529 global_context = NULL;
4534 translation_unit_t *parse(void)
4536 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4537 label_stack = NEW_ARR_F(stack_entry_t, 0);
4538 found_error = false;
4540 type_set_output(stderr);
4541 ast_set_output(stderr);
4543 lookahead_bufpos = 0;
4544 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4547 translation_unit_t *unit = parse_translation_unit();
4549 DEL_ARR_F(environment_stack);
4550 DEL_ARR_F(label_stack);
4558 void init_parser(void)
4560 init_expression_parsers();
4561 obstack_init(&temp_obst);
4563 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4564 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4565 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4566 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4567 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4568 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4569 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4570 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4571 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4572 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4573 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4576 void exit_parser(void)
4578 obstack_free(&temp_obst, NULL);