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 inline void *allocate_ast_zero(size_t size)
111 void *res = allocate_ast(size);
112 memset(res, 0, size);
116 static inline void *allocate_type_zero(size_t size)
118 void *res = obstack_alloc(type_obst, size);
119 memset(res, 0, size);
123 static inline size_t get_initializer_size(initializer_type_t type)
125 static const size_t size[] = {
126 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
127 [INITIALIZER_STRING] = sizeof(initializer_string_t),
128 [INITIALIZER_LIST] = sizeof(initializer_list_t)
130 assert(type < INITIALIZER_COUNT);
131 assert(size[type] != 0);
135 static inline initializer_t *allocate_initializer(initializer_type_t type)
137 initializer_t *result = allocate_ast_zero(get_initializer_size(type));
143 static inline void free_type(void *type)
145 obstack_free(type_obst, type);
149 * returns the top element of the environment stack
151 static inline size_t environment_top(void)
153 return ARR_LEN(environment_stack);
156 static inline size_t label_top(void)
158 return ARR_LEN(label_stack);
163 static inline void next_token(void)
165 token = lookahead_buffer[lookahead_bufpos];
166 lookahead_buffer[lookahead_bufpos] = lexer_token;
169 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
172 print_token(stderr, &token);
173 fprintf(stderr, "\n");
177 static inline const token_t *look_ahead(int num)
179 assert(num > 0 && num <= MAX_LOOKAHEAD);
180 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
181 return & lookahead_buffer[pos];
184 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
186 static void error(void)
189 #ifdef ABORT_ON_ERROR
194 static void parser_print_prefix_pos(const source_position_t source_position)
196 fputs(source_position.input_name, stderr);
198 fprintf(stderr, "%d", source_position.linenr);
202 static void parser_print_error_prefix_pos(
203 const source_position_t source_position)
205 parser_print_prefix_pos(source_position);
206 fputs("error: ", stderr);
210 static void parser_print_error_prefix(void)
212 parser_print_error_prefix_pos(token.source_position);
215 static void parse_error(const char *message)
217 parser_print_error_prefix();
218 fprintf(stderr, "parse error: %s\n", message);
221 static void parser_print_warning_prefix_pos(
222 const source_position_t source_position)
224 parser_print_prefix_pos(source_position);
225 fputs("warning: ", stderr);
228 static void parse_warning_pos(const source_position_t source_position,
229 const char *const message)
231 parser_print_prefix_pos(source_position);
232 fprintf(stderr, "warning: %s\n", message);
235 static void parse_warning(const char *message)
237 parse_warning_pos(token.source_position, message);
240 static void parse_error_expected(const char *message, ...)
245 if(message != NULL) {
246 parser_print_error_prefix();
247 fprintf(stderr, "%s\n", message);
249 parser_print_error_prefix();
250 fputs("Parse error: got ", stderr);
251 print_token(stderr, &token);
252 fputs(", expected ", stderr);
254 va_start(args, message);
255 token_type_t token_type = va_arg(args, token_type_t);
256 while(token_type != 0) {
260 fprintf(stderr, ", ");
262 print_token_type(stderr, token_type);
263 token_type = va_arg(args, token_type_t);
266 fprintf(stderr, "\n");
269 static void print_type_quoted(type_t *type)
276 static void type_error(const char *msg, const source_position_t source_position,
279 parser_print_error_prefix_pos(source_position);
280 fprintf(stderr, "%s, but found type ", msg);
281 print_type_quoted(type);
285 static void type_error_incompatible(const char *msg,
286 const source_position_t source_position, type_t *type1, type_t *type2)
288 parser_print_error_prefix_pos(source_position);
289 fprintf(stderr, "%s, incompatible types: ", msg);
290 print_type_quoted(type1);
291 fprintf(stderr, " - ");
292 print_type_quoted(type2);
293 fprintf(stderr, ")\n");
296 static void eat_block(void)
298 if(token.type == '{')
301 while(token.type != '}') {
302 if(token.type == T_EOF)
304 if(token.type == '{') {
313 static void eat_statement(void)
315 while(token.type != ';') {
316 if(token.type == T_EOF)
318 if(token.type == '}')
320 if(token.type == '{') {
329 static void eat_brace(void)
331 if(token.type == '(')
334 while(token.type != ')') {
335 if(token.type == T_EOF)
337 if(token.type == ')' || token.type == ';' || token.type == '}') {
340 if(token.type == '(') {
344 if(token.type == '{') {
353 #define expect(expected) \
354 if(UNLIKELY(token.type != (expected))) { \
355 parse_error_expected(NULL, (expected), 0); \
361 #define expect_block(expected) \
362 if(UNLIKELY(token.type != (expected))) { \
363 parse_error_expected(NULL, (expected), 0); \
369 #define expect_void(expected) \
370 if(UNLIKELY(token.type != (expected))) { \
371 parse_error_expected(NULL, (expected), 0); \
377 static void set_context(context_t *new_context)
379 context = new_context;
381 last_declaration = new_context->declarations;
382 if(last_declaration != NULL) {
383 while(last_declaration->next != NULL) {
384 last_declaration = last_declaration->next;
390 * called when we find a 2nd declarator for an identifier we already have a
393 static bool is_compatible_declaration (declaration_t *declaration,
394 declaration_t *previous)
396 /* TODO: not correct yet */
397 return declaration->type == previous->type;
400 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespc)
402 declaration_t *declaration = symbol->declaration;
403 for( ; declaration != NULL; declaration = declaration->symbol_next) {
404 if(declaration->namespc == namespc)
411 static const char *get_namespace_prefix(namespace_t namespc)
414 case NAMESPACE_NORMAL:
416 case NAMESPACE_UNION:
418 case NAMESPACE_STRUCT:
422 case NAMESPACE_LABEL:
425 panic("invalid namespace found");
429 * pushs an environment_entry on the environment stack and links the
430 * corresponding symbol to the new entry
432 static declaration_t *stack_push(stack_entry_t **stack_ptr,
433 declaration_t *declaration,
434 context_t *parent_context)
436 symbol_t *symbol = declaration->symbol;
437 namespace_t namespc = (namespace_t)declaration->namespc;
439 /* a declaration should be only pushed once */
440 assert(declaration->parent_context == NULL);
441 declaration->parent_context = parent_context;
443 declaration_t *previous_declaration = get_declaration(symbol, namespc);
444 assert(declaration != previous_declaration);
445 if(previous_declaration != NULL
446 && previous_declaration->parent_context == context) {
447 if(!is_compatible_declaration(declaration, previous_declaration)) {
448 parser_print_error_prefix_pos(declaration->source_position);
449 fprintf(stderr, "definition of symbol %s%s with type ",
450 get_namespace_prefix(namespc), symbol->string);
451 print_type_quoted(declaration->type);
453 parser_print_error_prefix_pos(
454 previous_declaration->source_position);
455 fprintf(stderr, "is incompatible with previous declaration "
457 print_type_quoted(previous_declaration->type);
460 const storage_class_t old_storage = previous_declaration->storage_class;
461 const storage_class_t new_storage = declaration->storage_class;
462 if (current_function == NULL) {
463 if (old_storage != STORAGE_CLASS_STATIC &&
464 new_storage == STORAGE_CLASS_STATIC) {
465 parser_print_error_prefix_pos(declaration->source_position);
467 "static declaration of '%s' follows non-static declaration\n",
469 parser_print_error_prefix_pos(previous_declaration->source_position);
470 fprintf(stderr, "previous declaration of '%s' was here\n",
473 if (old_storage == STORAGE_CLASS_EXTERN) {
474 if (new_storage == STORAGE_CLASS_NONE) {
475 previous_declaration->storage_class = STORAGE_CLASS_NONE;
478 parser_print_warning_prefix_pos(declaration->source_position);
479 fprintf(stderr, "redundant declaration for '%s'\n",
481 parser_print_warning_prefix_pos(previous_declaration->source_position);
482 fprintf(stderr, "previous declaration of '%s' was here\n",
487 if (old_storage == STORAGE_CLASS_EXTERN &&
488 new_storage == STORAGE_CLASS_EXTERN) {
489 parser_print_warning_prefix_pos(declaration->source_position);
490 fprintf(stderr, "redundant extern declaration for '%s'\n",
492 parser_print_warning_prefix_pos(previous_declaration->source_position);
493 fprintf(stderr, "previous declaration of '%s' was here\n",
496 parser_print_error_prefix_pos(declaration->source_position);
497 if (old_storage == new_storage) {
498 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
500 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
502 parser_print_error_prefix_pos(previous_declaration->source_position);
503 fprintf(stderr, "previous declaration of '%s' was here\n",
508 return previous_declaration;
511 /* remember old declaration */
513 entry.symbol = symbol;
514 entry.old_declaration = symbol->declaration;
515 entry.namespc = namespc;
516 ARR_APP1(stack_entry_t, *stack_ptr, entry);
518 /* replace/add declaration into declaration list of the symbol */
519 if(symbol->declaration == NULL) {
520 symbol->declaration = declaration;
522 declaration_t *iter_last = NULL;
523 declaration_t *iter = symbol->declaration;
524 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
525 /* replace an entry? */
526 if(iter->namespc == namespc) {
527 if(iter_last == NULL) {
528 symbol->declaration = declaration;
530 iter_last->symbol_next = declaration;
532 declaration->symbol_next = iter->symbol_next;
537 assert(iter_last->symbol_next == NULL);
538 iter_last->symbol_next = declaration;
545 static declaration_t *environment_push(declaration_t *declaration)
547 assert(declaration->source_position.input_name != NULL);
548 return stack_push(&environment_stack, declaration, context);
551 static declaration_t *label_push(declaration_t *declaration)
553 return stack_push(&label_stack, declaration, ¤t_function->context);
557 * pops symbols from the environment stack until @p new_top is the top element
559 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
561 stack_entry_t *stack = *stack_ptr;
562 size_t top = ARR_LEN(stack);
565 assert(new_top <= top);
569 for(i = top; i > new_top; --i) {
570 stack_entry_t *entry = & stack[i - 1];
572 declaration_t *old_declaration = entry->old_declaration;
573 symbol_t *symbol = entry->symbol;
574 namespace_t namespc = (namespace_t)entry->namespc;
576 /* replace/remove declaration */
577 declaration_t *declaration = symbol->declaration;
578 assert(declaration != NULL);
579 if(declaration->namespc == namespc) {
580 if(old_declaration == NULL) {
581 symbol->declaration = declaration->symbol_next;
583 symbol->declaration = old_declaration;
586 declaration_t *iter_last = declaration;
587 declaration_t *iter = declaration->symbol_next;
588 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
589 /* replace an entry? */
590 if(iter->namespc == namespc) {
591 assert(iter_last != NULL);
592 iter_last->symbol_next = old_declaration;
593 old_declaration->symbol_next = iter->symbol_next;
597 assert(iter != NULL);
601 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
604 static void environment_pop_to(size_t new_top)
606 stack_pop_to(&environment_stack, new_top);
609 static void label_pop_to(size_t new_top)
611 stack_pop_to(&label_stack, new_top);
615 static int get_rank(const type_t *type)
617 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
618 * and esp. footnote 108). However we can't fold constants (yet), so we
619 * can't decide wether unsigned int is possible, while int always works.
620 * (unsigned int would be preferable when possible... for stuff like
621 * struct { enum { ... } bla : 4; } ) */
622 if(type->type == TYPE_ENUM)
623 return ATOMIC_TYPE_INT;
625 assert(type->type == TYPE_ATOMIC);
626 const atomic_type_t *atomic_type = &type->atomic;
627 atomic_type_type_t atype = atomic_type->atype;
631 static type_t *promote_integer(type_t *type)
633 if(get_rank(type) < ATOMIC_TYPE_INT)
639 static expression_t *create_cast_expression(expression_t *expression,
642 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
644 cast->expression.type = EXPR_UNARY;
645 cast->type = UNEXPR_CAST;
646 cast->value = expression;
647 cast->expression.datatype = dest_type;
649 return (expression_t*) cast;
652 static bool is_null_expression(const expression_t *const expr)
654 if (expr->type != EXPR_CONST) return false;
656 type_t *const type = skip_typeref(expr->datatype);
657 if (!is_type_integer(type)) return false;
659 const const_t *const const_expr = (const const_t*)expr;
660 return const_expr->v.int_value == 0;
663 static expression_t *create_implicit_cast(expression_t *expression,
666 type_t *source_type = expression->datatype;
668 if(source_type == NULL)
671 source_type = skip_typeref(source_type);
672 dest_type = skip_typeref(dest_type);
674 if(source_type == dest_type)
677 switch (dest_type->type) {
679 /* TODO warning for implicitly converting to enum */
681 if (source_type->type != TYPE_ATOMIC &&
682 source_type->type != TYPE_ENUM) {
683 panic("casting of non-atomic types not implemented yet");
686 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
687 type_error_incompatible("can't cast types",
688 expression->source_position, source_type, dest_type);
692 return create_cast_expression(expression, dest_type);
695 switch (source_type->type) {
697 if (is_null_expression(expression)) {
698 return create_cast_expression(expression, dest_type);
703 if (pointers_compatible(source_type, dest_type)) {
704 return create_cast_expression(expression, dest_type);
709 array_type_t *array_type = &source_type->array;
710 pointer_type_t *pointer_type = &dest_type->pointer;
711 if (types_compatible(array_type->element_type,
712 pointer_type->points_to)) {
713 return create_cast_expression(expression, dest_type);
719 panic("casting of non-atomic types not implemented yet");
722 type_error_incompatible("can't implicitly cast types",
723 expression->source_position, source_type, dest_type);
727 panic("casting of non-atomic types not implemented yet");
731 /** Implements the rules from § 6.5.16.1 */
732 static void semantic_assign(type_t *orig_type_left, expression_t **right,
735 type_t *orig_type_right = (*right)->datatype;
737 if(orig_type_right == NULL)
740 type_t *const type_left = skip_typeref(orig_type_left);
741 type_t *const type_right = skip_typeref(orig_type_right);
743 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
744 (is_type_pointer(type_left) && is_null_expression(*right)) ||
745 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
746 && is_type_pointer(type_right))) {
747 *right = create_implicit_cast(*right, type_left);
751 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
752 pointer_type_t *pointer_type_left = (pointer_type_t*) type_left;
753 pointer_type_t *pointer_type_right = (pointer_type_t*) type_right;
754 type_t *points_to_left = pointer_type_left->points_to;
755 type_t *points_to_right = pointer_type_right->points_to;
757 points_to_left = skip_typeref(points_to_left);
758 points_to_right = skip_typeref(points_to_right);
760 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
761 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
762 && !types_compatible(points_to_left, points_to_right)) {
763 goto incompatible_assign_types;
766 /* the left type has all qualifiers from the right type */
767 unsigned missing_qualifiers
768 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
769 if(missing_qualifiers != 0) {
770 parser_print_error_prefix();
771 fprintf(stderr, "destination type ");
772 print_type_quoted(type_left);
773 fprintf(stderr, " in %s from type ", context);
774 print_type_quoted(type_right);
775 fprintf(stderr, " lacks qualifiers '");
776 print_type_qualifiers(missing_qualifiers);
777 fprintf(stderr, "' in pointed-to type\n");
781 *right = create_implicit_cast(*right, type_left);
785 if (is_type_compound(type_left)
786 && types_compatible(type_left, type_right)) {
787 *right = create_implicit_cast(*right, type_left);
791 incompatible_assign_types:
792 /* TODO: improve error message */
793 parser_print_error_prefix();
794 fprintf(stderr, "incompatible types in %s\n", context);
795 parser_print_error_prefix();
796 print_type_quoted(type_left);
797 fputs(" <- ", stderr);
798 print_type_quoted(type_right);
802 static expression_t *parse_constant_expression(void)
804 /* start parsing at precedence 7 (conditional expression) */
805 return parse_sub_expression(7);
808 static expression_t *parse_assignment_expression(void)
810 /* start parsing at precedence 2 (assignment expression) */
811 return parse_sub_expression(2);
814 typedef struct declaration_specifiers_t declaration_specifiers_t;
815 struct declaration_specifiers_t {
816 storage_class_t storage_class;
821 static void parse_compound_type_entries(void);
822 static declaration_t *parse_declarator(
823 const declaration_specifiers_t *specifiers, type_t *type,
824 bool may_be_abstract);
825 static declaration_t *record_declaration(declaration_t *declaration);
827 static const char *parse_string_literals(void)
829 assert(token.type == T_STRING_LITERAL);
830 const char *result = token.v.string;
834 while(token.type == T_STRING_LITERAL) {
835 result = concat_strings(result, token.v.string);
842 static void parse_attributes(void)
846 case T___attribute__:
854 parse_error("EOF while parsing attribute");
872 if(token.type != T_STRING_LITERAL) {
873 parse_error_expected("while parsing assembler attribute",
878 parse_string_literals();
883 goto attributes_finished;
892 static designator_t *parse_designation(void)
894 if(token.type != '[' && token.type != '.')
897 designator_t *result = NULL;
898 designator_t *last = NULL;
901 designator_t *designator;
904 designator = allocate_ast_zero(sizeof(designator[0]));
906 designator->array_access = parse_constant_expression();
910 designator = allocate_ast_zero(sizeof(designator[0]));
912 if(token.type != T_IDENTIFIER) {
913 parse_error_expected("while parsing designator",
917 designator->symbol = token.v.symbol;
925 assert(designator != NULL);
927 last->next = designator;
936 static initializer_t *initializer_from_string(array_type_t *type,
939 /* TODO: check len vs. size of array type */
942 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
943 initializer->string.string = string;
948 static initializer_t *initializer_from_expression(type_t *type,
949 expression_t *expression)
951 /* TODO check that expression is a constant expression */
953 /* § 6.7.8.14/15 char array may be initialized by string literals */
954 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
955 array_type_t *array_type = &type->array;
956 type_t *element_type = array_type->element_type;
958 if(element_type->type == TYPE_ATOMIC) {
959 atomic_type_t *atomic_type = &element_type->atomic;
960 atomic_type_type_t atype = atomic_type->atype;
962 /* TODO handle wide strings */
963 if(atype == ATOMIC_TYPE_CHAR
964 || atype == ATOMIC_TYPE_SCHAR
965 || atype == ATOMIC_TYPE_UCHAR) {
967 string_literal_t *literal = (string_literal_t*) expression;
968 return initializer_from_string(array_type, literal->value);
973 semantic_assign(type, &expression, "initializer");
975 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
976 result->value.value = expression;
981 static initializer_t *parse_sub_initializer(type_t *type,
982 expression_t *expression,
983 type_t *expression_type);
985 static initializer_t *parse_sub_initializer_elem(type_t *type)
987 if(token.type == '{') {
988 return parse_sub_initializer(type, NULL, NULL);
991 expression_t *expression = parse_assignment_expression();
992 type_t *expression_type = skip_typeref(expression->datatype);
994 return parse_sub_initializer(type, expression, expression_type);
997 static bool had_initializer_brace_warning;
999 static initializer_t *parse_sub_initializer(type_t *type,
1000 expression_t *expression,
1001 type_t *expression_type)
1003 if(is_type_scalar(type)) {
1004 /* there might be extra {} hierarchies */
1005 if(token.type == '{') {
1007 if(!had_initializer_brace_warning) {
1008 parse_warning("braces around scalar initializer");
1009 had_initializer_brace_warning = true;
1011 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1012 if(token.type == ',') {
1014 /* TODO: warn about excessive elements */
1020 if(expression == NULL) {
1021 expression = parse_assignment_expression();
1023 return initializer_from_expression(type, expression);
1026 /* TODO: ignore qualifiers, comparing pointers is probably
1028 if(expression != NULL && expression_type == type) {
1029 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1032 semantic_assign(type, &expression, "initializer");
1034 result->value.value = expression;
1039 bool read_paren = false;
1040 if(token.type == '{') {
1045 /* descend into subtype */
1046 initializer_t *result = NULL;
1047 initializer_t **elems;
1048 if(type->type == TYPE_ARRAY) {
1049 array_type_t *array_type = (array_type_t*) type;
1050 type_t *element_type = array_type->element_type;
1051 element_type = skip_typeref(element_type);
1054 had_initializer_brace_warning = false;
1055 if(expression == NULL) {
1056 sub = parse_sub_initializer_elem(element_type);
1058 sub = parse_sub_initializer(element_type, expression,
1062 /* didn't match the subtypes -> try the parent type */
1064 assert(!read_paren);
1068 elems = NEW_ARR_F(initializer_t*, 0);
1069 ARR_APP1(initializer_t*, elems, sub);
1072 if(token.type == '}')
1075 if(token.type == '}')
1079 = parse_sub_initializer(element_type, NULL, NULL);
1081 /* TODO error, do nicer cleanup */
1082 parse_error("member initializer didn't match");
1086 ARR_APP1(initializer_t*, elems, sub);
1089 assert(type->type == TYPE_COMPOUND_STRUCT
1090 || type->type == TYPE_COMPOUND_UNION);
1091 compound_type_t *compound_type = (compound_type_t*) type;
1092 context_t *context = & compound_type->declaration->context;
1094 declaration_t *first = context->declarations;
1097 type_t *first_type = first->type;
1098 first_type = skip_typeref(first_type);
1101 had_initializer_brace_warning = false;
1102 if(expression == NULL) {
1103 sub = parse_sub_initializer_elem(first_type);
1105 sub = parse_sub_initializer(first_type, expression,expression_type);
1108 /* didn't match the subtypes -> try our parent type */
1110 assert(!read_paren);
1114 elems = NEW_ARR_F(initializer_t*, 0);
1115 ARR_APP1(initializer_t*, elems, sub);
1117 declaration_t *iter = first->next;
1118 for( ; iter != NULL; iter = iter->next) {
1119 if(iter->symbol == NULL)
1121 if(iter->namespc != NAMESPACE_NORMAL)
1124 if(token.type == '}')
1127 if(token.type == '}')
1130 type_t *iter_type = iter->type;
1131 iter_type = skip_typeref(iter_type);
1133 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1135 /* TODO error, do nicer cleanup*/
1136 parse_error("member initializer didn't match");
1140 ARR_APP1(initializer_t*, elems, sub);
1144 int len = ARR_LEN(elems);
1145 size_t elems_size = sizeof(initializer_t*) * len;
1147 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1149 init->initializer.type = INITIALIZER_LIST;
1151 memcpy(init->initializers, elems, elems_size);
1154 result = (initializer_t*) init;
1157 if(token.type == ',')
1164 static initializer_t *parse_initializer(type_t *type)
1166 initializer_t *result;
1168 type = skip_typeref(type);
1170 if(token.type != '{') {
1171 expression_t *expression = parse_assignment_expression();
1172 return initializer_from_expression(type, expression);
1175 if(is_type_scalar(type)) {
1179 expression_t *expression = parse_assignment_expression();
1180 result = initializer_from_expression(type, expression);
1182 if(token.type == ',')
1188 result = parse_sub_initializer(type, NULL, NULL);
1196 static declaration_t *parse_compound_type_specifier(bool is_struct)
1204 symbol_t *symbol = NULL;
1205 declaration_t *declaration = NULL;
1207 if (token.type == T___attribute__) {
1212 if(token.type == T_IDENTIFIER) {
1213 symbol = token.v.symbol;
1217 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1219 declaration = get_declaration(symbol, NAMESPACE_UNION);
1221 } else if(token.type != '{') {
1223 parse_error_expected("while parsing struct type specifier",
1224 T_IDENTIFIER, '{', 0);
1226 parse_error_expected("while parsing union type specifier",
1227 T_IDENTIFIER, '{', 0);
1233 if(declaration == NULL) {
1234 declaration = allocate_type_zero(sizeof(declaration[0]));
1237 declaration->namespc = NAMESPACE_STRUCT;
1239 declaration->namespc = NAMESPACE_UNION;
1241 declaration->source_position = token.source_position;
1242 declaration->symbol = symbol;
1243 record_declaration(declaration);
1246 if(token.type == '{') {
1247 if(declaration->init.is_defined) {
1248 assert(symbol != NULL);
1249 parser_print_error_prefix();
1250 fprintf(stderr, "multiple definition of %s %s\n",
1251 is_struct ? "struct" : "union", symbol->string);
1252 declaration->context.declarations = NULL;
1254 declaration->init.is_defined = true;
1256 int top = environment_top();
1257 context_t *last_context = context;
1258 set_context(& declaration->context);
1260 parse_compound_type_entries();
1263 assert(context == & declaration->context);
1264 set_context(last_context);
1265 environment_pop_to(top);
1271 static void parse_enum_entries(enum_type_t *const enum_type)
1275 if(token.type == '}') {
1277 parse_error("empty enum not allowed");
1282 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1284 if(token.type != T_IDENTIFIER) {
1285 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1289 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1290 entry->type = (type_t*) enum_type;
1291 entry->symbol = token.v.symbol;
1292 entry->source_position = token.source_position;
1295 if(token.type == '=') {
1297 entry->init.enum_value = parse_constant_expression();
1302 record_declaration(entry);
1304 if(token.type != ',')
1307 } while(token.type != '}');
1312 static type_t *parse_enum_specifier(void)
1316 declaration_t *declaration;
1319 if(token.type == T_IDENTIFIER) {
1320 symbol = token.v.symbol;
1323 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1324 } else if(token.type != '{') {
1325 parse_error_expected("while parsing enum type specifier",
1326 T_IDENTIFIER, '{', 0);
1333 if(declaration == NULL) {
1334 declaration = allocate_type_zero(sizeof(declaration[0]));
1336 declaration->namespc = NAMESPACE_ENUM;
1337 declaration->source_position = token.source_position;
1338 declaration->symbol = symbol;
1341 enum_type_t *const enum_type = allocate_type_zero(sizeof(enum_type[0]));
1342 enum_type->type.type = TYPE_ENUM;
1343 enum_type->declaration = declaration;
1345 if(token.type == '{') {
1346 if(declaration->init.is_defined) {
1347 parser_print_error_prefix();
1348 fprintf(stderr, "multiple definitions of enum %s\n",
1351 record_declaration(declaration);
1352 declaration->init.is_defined = 1;
1354 parse_enum_entries(enum_type);
1358 return (type_t*) enum_type;
1362 * if a symbol is a typedef to another type, return true
1364 static bool is_typedef_symbol(symbol_t *symbol)
1366 const declaration_t *const declaration =
1367 get_declaration(symbol, NAMESPACE_NORMAL);
1369 declaration != NULL &&
1370 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1373 static type_t *parse_typeof(void)
1381 expression_t *expression = NULL;
1384 switch(token.type) {
1385 case T___extension__:
1386 /* this can be a prefix to a typename or an expression */
1387 /* we simply eat it now. */
1390 } while(token.type == T___extension__);
1394 if(is_typedef_symbol(token.v.symbol)) {
1395 type = parse_typename();
1397 expression = parse_expression();
1398 type = expression->datatype;
1403 type = parse_typename();
1407 expression = parse_expression();
1408 type = expression->datatype;
1414 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1415 typeof->type.type = TYPE_TYPEOF;
1416 typeof->expression = expression;
1417 typeof->typeof_type = type;
1419 return (type_t*) typeof;
1423 SPECIFIER_SIGNED = 1 << 0,
1424 SPECIFIER_UNSIGNED = 1 << 1,
1425 SPECIFIER_LONG = 1 << 2,
1426 SPECIFIER_INT = 1 << 3,
1427 SPECIFIER_DOUBLE = 1 << 4,
1428 SPECIFIER_CHAR = 1 << 5,
1429 SPECIFIER_SHORT = 1 << 6,
1430 SPECIFIER_LONG_LONG = 1 << 7,
1431 SPECIFIER_FLOAT = 1 << 8,
1432 SPECIFIER_BOOL = 1 << 9,
1433 SPECIFIER_VOID = 1 << 10,
1434 #ifdef PROVIDE_COMPLEX
1435 SPECIFIER_COMPLEX = 1 << 11,
1436 SPECIFIER_IMAGINARY = 1 << 12,
1440 static type_t *create_builtin_type(symbol_t *symbol)
1442 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1443 type->type.type = TYPE_BUILTIN;
1444 type->symbol = symbol;
1446 type->real_type = type_int;
1448 return (type_t*) type;
1451 static type_t *get_typedef_type(symbol_t *symbol)
1453 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1454 if(declaration == NULL
1455 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1458 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1459 typedef_type->type.type = TYPE_TYPEDEF;
1460 typedef_type->declaration = declaration;
1462 return (type_t*) typedef_type;
1465 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1467 type_t *type = NULL;
1468 unsigned type_qualifiers = 0;
1469 unsigned type_specifiers = 0;
1473 switch(token.type) {
1476 #define MATCH_STORAGE_CLASS(token, class) \
1478 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1479 parse_error("multiple storage classes in declaration " \
1482 specifiers->storage_class = class; \
1486 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1487 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1488 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1489 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1490 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1492 /* type qualifiers */
1493 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1495 type_qualifiers |= qualifier; \
1499 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1500 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1501 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1503 case T___extension__:
1508 /* type specifiers */
1509 #define MATCH_SPECIFIER(token, specifier, name) \
1512 if(type_specifiers & specifier) { \
1513 parse_error("multiple " name " type specifiers given"); \
1515 type_specifiers |= specifier; \
1519 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1520 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1521 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1522 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1523 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1524 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1525 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1526 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1527 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1528 #ifdef PROVIDE_COMPLEX
1529 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1530 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1534 specifiers->is_inline = true;
1539 if(type_specifiers & SPECIFIER_LONG_LONG) {
1540 parse_error("multiple type specifiers given");
1541 } else if(type_specifiers & SPECIFIER_LONG) {
1542 type_specifiers |= SPECIFIER_LONG_LONG;
1544 type_specifiers |= SPECIFIER_LONG;
1548 /* TODO: if type != NULL for the following rules should issue
1551 type = allocate_type_zero(sizeof(struct compound_type_t));
1553 compound_type_t *compound_type = (compound_type_t*) type;
1554 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1555 compound_type->declaration = parse_compound_type_specifier(true);
1559 type = allocate_type_zero(sizeof(compound_type_t));
1561 compound_type_t *compound_type = (compound_type_t*) type;
1562 compound_type->type.type = TYPE_COMPOUND_UNION;
1563 compound_type->declaration = parse_compound_type_specifier(false);
1567 type = parse_enum_specifier();
1570 type = parse_typeof();
1572 case T___builtin_va_list:
1573 type = create_builtin_type(token.v.symbol);
1577 case T___attribute__:
1582 case T_IDENTIFIER: {
1583 type_t *typedef_type = get_typedef_type(token.v.symbol);
1585 if(typedef_type == NULL)
1586 goto finish_specifiers;
1589 type = typedef_type;
1593 /* function specifier */
1595 goto finish_specifiers;
1602 atomic_type_type_t atomic_type;
1604 /* match valid basic types */
1605 switch(type_specifiers) {
1606 case SPECIFIER_VOID:
1607 atomic_type = ATOMIC_TYPE_VOID;
1609 case SPECIFIER_CHAR:
1610 atomic_type = ATOMIC_TYPE_CHAR;
1612 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1613 atomic_type = ATOMIC_TYPE_SCHAR;
1615 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1616 atomic_type = ATOMIC_TYPE_UCHAR;
1618 case SPECIFIER_SHORT:
1619 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1620 case SPECIFIER_SHORT | SPECIFIER_INT:
1621 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1622 atomic_type = ATOMIC_TYPE_SHORT;
1624 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1625 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1626 atomic_type = ATOMIC_TYPE_USHORT;
1629 case SPECIFIER_SIGNED:
1630 case SPECIFIER_SIGNED | SPECIFIER_INT:
1631 atomic_type = ATOMIC_TYPE_INT;
1633 case SPECIFIER_UNSIGNED:
1634 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1635 atomic_type = ATOMIC_TYPE_UINT;
1637 case SPECIFIER_LONG:
1638 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1639 case SPECIFIER_LONG | SPECIFIER_INT:
1640 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1641 atomic_type = ATOMIC_TYPE_LONG;
1643 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1644 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1645 atomic_type = ATOMIC_TYPE_ULONG;
1647 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1648 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1649 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1650 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1652 atomic_type = ATOMIC_TYPE_LONGLONG;
1654 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1655 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1657 atomic_type = ATOMIC_TYPE_ULONGLONG;
1659 case SPECIFIER_FLOAT:
1660 atomic_type = ATOMIC_TYPE_FLOAT;
1662 case SPECIFIER_DOUBLE:
1663 atomic_type = ATOMIC_TYPE_DOUBLE;
1665 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1666 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1668 case SPECIFIER_BOOL:
1669 atomic_type = ATOMIC_TYPE_BOOL;
1671 #ifdef PROVIDE_COMPLEX
1672 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1673 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1675 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1676 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1678 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1679 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1681 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1682 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1684 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1685 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1687 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1688 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1692 /* invalid specifier combination, give an error message */
1693 if(type_specifiers == 0) {
1695 parse_warning("no type specifiers in declaration (using int)");
1696 atomic_type = ATOMIC_TYPE_INT;
1699 parse_error("no type specifiers given in declaration");
1701 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1702 (type_specifiers & SPECIFIER_UNSIGNED)) {
1703 parse_error("signed and unsigned specifiers gives");
1704 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1705 parse_error("only integer types can be signed or unsigned");
1707 parse_error("multiple datatypes in declaration");
1709 atomic_type = ATOMIC_TYPE_INVALID;
1712 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1713 atype->type.type = TYPE_ATOMIC;
1714 atype->atype = atomic_type;
1717 type = (type_t*) atype;
1719 if(type_specifiers != 0) {
1720 parse_error("multiple datatypes in declaration");
1724 type->base.qualifiers = (type_qualifier_t)type_qualifiers;
1726 type_t *result = typehash_insert(type);
1727 if(newtype && result != (type_t*) type) {
1731 specifiers->type = result;
1734 static type_qualifiers_t parse_type_qualifiers(void)
1736 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1739 switch(token.type) {
1740 /* type qualifiers */
1741 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1742 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1743 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1746 return type_qualifiers;
1751 static void parse_identifier_list(void)
1754 if(token.type != T_IDENTIFIER) {
1755 parse_error_expected("while parsing parameter identifier list",
1760 if(token.type != ',')
1766 static declaration_t *parse_parameter(void)
1768 declaration_specifiers_t specifiers;
1769 memset(&specifiers, 0, sizeof(specifiers));
1771 parse_declaration_specifiers(&specifiers);
1773 declaration_t *declaration
1774 = parse_declarator(&specifiers, specifiers.type, true);
1776 /* TODO check declaration constraints for parameters */
1777 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1778 parse_error("typedef not allowed in parameter list");
1781 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1783 if (declaration->type->type == TYPE_ARRAY) {
1784 const array_type_t *const arr_type =
1785 (const array_type_t*)declaration->type;
1786 type_t *element_type = arr_type->element_type;
1787 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1793 static declaration_t *parse_parameters(function_type_t *type)
1795 if(token.type == T_IDENTIFIER) {
1796 symbol_t *symbol = token.v.symbol;
1797 if(!is_typedef_symbol(symbol)) {
1798 /* TODO: K&R style C parameters */
1799 parse_identifier_list();
1804 if(token.type == ')') {
1805 type->unspecified_parameters = 1;
1808 if(token.type == T_void && look_ahead(1)->type == ')') {
1813 declaration_t *declarations = NULL;
1814 declaration_t *declaration;
1815 declaration_t *last_declaration = NULL;
1816 function_parameter_t *parameter;
1817 function_parameter_t *last_parameter = NULL;
1820 switch(token.type) {
1824 return declarations;
1827 case T___extension__:
1829 declaration = parse_parameter();
1831 parameter = allocate_type_zero(sizeof(parameter[0]));
1832 parameter->type = declaration->type;
1834 if(last_parameter != NULL) {
1835 last_declaration->next = declaration;
1836 last_parameter->next = parameter;
1838 type->parameters = parameter;
1839 declarations = declaration;
1841 last_parameter = parameter;
1842 last_declaration = declaration;
1846 return declarations;
1848 if(token.type != ',')
1849 return declarations;
1859 } construct_type_type_t;
1861 typedef struct construct_type_t construct_type_t;
1862 struct construct_type_t {
1863 construct_type_type_t type;
1864 construct_type_t *next;
1867 typedef struct parsed_pointer_t parsed_pointer_t;
1868 struct parsed_pointer_t {
1869 construct_type_t construct_type;
1870 type_qualifiers_t type_qualifiers;
1873 typedef struct construct_function_type_t construct_function_type_t;
1874 struct construct_function_type_t {
1875 construct_type_t construct_type;
1876 function_type_t *function_type;
1879 typedef struct parsed_array_t parsed_array_t;
1880 struct parsed_array_t {
1881 construct_type_t construct_type;
1882 type_qualifiers_t type_qualifiers;
1888 typedef struct construct_base_type_t construct_base_type_t;
1889 struct construct_base_type_t {
1890 construct_type_t construct_type;
1894 static construct_type_t *parse_pointer_declarator(void)
1898 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1899 memset(pointer, 0, sizeof(pointer[0]));
1900 pointer->construct_type.type = CONSTRUCT_POINTER;
1901 pointer->type_qualifiers = parse_type_qualifiers();
1903 return (construct_type_t*) pointer;
1906 static construct_type_t *parse_array_declarator(void)
1910 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1911 memset(array, 0, sizeof(array[0]));
1912 array->construct_type.type = CONSTRUCT_ARRAY;
1914 if(token.type == T_static) {
1915 array->is_static = true;
1919 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1920 if(type_qualifiers != 0) {
1921 if(token.type == T_static) {
1922 array->is_static = true;
1926 array->type_qualifiers = type_qualifiers;
1928 if(token.type == '*' && look_ahead(1)->type == ']') {
1929 array->is_variable = true;
1931 } else if(token.type != ']') {
1932 array->size = parse_assignment_expression();
1937 return (construct_type_t*) array;
1940 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1944 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1945 type->type.type = TYPE_FUNCTION;
1947 declaration_t *parameters = parse_parameters(type);
1948 if(declaration != NULL) {
1949 declaration->context.declarations = parameters;
1952 construct_function_type_t *construct_function_type =
1953 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1954 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1955 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1956 construct_function_type->function_type = type;
1960 return (construct_type_t*) construct_function_type;
1963 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1964 bool may_be_abstract)
1966 /* construct a single linked list of construct_type_t's which describe
1967 * how to construct the final declarator type */
1968 construct_type_t *first = NULL;
1969 construct_type_t *last = NULL;
1972 while(token.type == '*') {
1973 construct_type_t *type = parse_pointer_declarator();
1984 /* TODO: find out if this is correct */
1987 construct_type_t *inner_types = NULL;
1989 switch(token.type) {
1991 if(declaration == NULL) {
1992 parse_error("no identifier expected in typename");
1994 declaration->symbol = token.v.symbol;
1995 declaration->source_position = token.source_position;
2001 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2007 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2008 /* avoid a loop in the outermost scope, because eat_statement doesn't
2010 if(token.type == '}' && current_function == NULL) {
2018 construct_type_t *p = last;
2021 construct_type_t *type;
2022 switch(token.type) {
2024 type = parse_function_declarator(declaration);
2027 type = parse_array_declarator();
2030 goto declarator_finished;
2033 /* insert in the middle of the list (behind p) */
2035 type->next = p->next;
2046 declarator_finished:
2049 /* append inner_types at the end of the list, we don't to set last anymore
2050 * as it's not needed anymore */
2052 assert(first == NULL);
2053 first = inner_types;
2055 last->next = inner_types;
2061 static type_t *construct_declarator_type(construct_type_t *construct_list,
2064 construct_type_t *iter = construct_list;
2065 for( ; iter != NULL; iter = iter->next) {
2066 parsed_pointer_t *parsed_pointer;
2067 parsed_array_t *parsed_array;
2068 construct_function_type_t *construct_function_type;
2069 function_type_t *function_type;
2070 pointer_type_t *pointer_type;
2071 array_type_t *array_type;
2073 switch(iter->type) {
2074 case CONSTRUCT_INVALID:
2075 panic("invalid type construction found");
2076 case CONSTRUCT_FUNCTION:
2077 construct_function_type = (construct_function_type_t*) iter;
2078 function_type = construct_function_type->function_type;
2080 function_type->result_type = type;
2081 type = (type_t*) function_type;
2084 case CONSTRUCT_POINTER:
2085 parsed_pointer = (parsed_pointer_t*) iter;
2086 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2088 pointer_type->type.type = TYPE_POINTER;
2089 pointer_type->points_to = type;
2090 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2091 type = (type_t*) pointer_type;
2094 case CONSTRUCT_ARRAY:
2095 parsed_array = (parsed_array_t*) iter;
2096 array_type = allocate_type_zero(sizeof(array_type[0]));
2098 array_type->type.type = TYPE_ARRAY;
2099 array_type->element_type = type;
2100 array_type->type.qualifiers = parsed_array->type_qualifiers;
2101 array_type->is_static = parsed_array->is_static;
2102 array_type->is_variable = parsed_array->is_variable;
2103 array_type->size = parsed_array->size;
2104 type = (type_t*) array_type;
2108 type_t *hashed_type = typehash_insert((type_t*) type);
2109 if(hashed_type != type) {
2110 /* the function type was constructed earlier freeing it here will
2111 * destroy other types... */
2112 if(iter->type != CONSTRUCT_FUNCTION) {
2122 static declaration_t *parse_declarator(
2123 const declaration_specifiers_t *specifiers,
2124 type_t *type, bool may_be_abstract)
2126 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2127 declaration->storage_class = specifiers->storage_class;
2128 declaration->is_inline = specifiers->is_inline;
2130 construct_type_t *construct_type
2131 = parse_inner_declarator(declaration, may_be_abstract);
2132 declaration->type = construct_declarator_type(construct_type, type);
2134 if(construct_type != NULL) {
2135 obstack_free(&temp_obst, construct_type);
2141 static type_t *parse_abstract_declarator(type_t *base_type)
2143 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2145 type_t *result = construct_declarator_type(construct_type, base_type);
2146 if(construct_type != NULL) {
2147 obstack_free(&temp_obst, construct_type);
2153 static declaration_t *record_declaration(declaration_t *declaration)
2155 assert(context != NULL);
2157 symbol_t *symbol = declaration->symbol;
2158 if(symbol != NULL) {
2159 declaration_t *alias = environment_push(declaration);
2160 if(alias != declaration)
2163 declaration->parent_context = context;
2166 if(last_declaration != NULL) {
2167 last_declaration->next = declaration;
2169 context->declarations = declaration;
2171 last_declaration = declaration;
2176 static void parser_error_multiple_definition(declaration_t *previous,
2177 declaration_t *declaration)
2179 parser_print_error_prefix_pos(declaration->source_position);
2180 fprintf(stderr, "multiple definition of symbol '%s'\n",
2181 declaration->symbol->string);
2182 parser_print_error_prefix_pos(previous->source_position);
2183 fprintf(stderr, "this is the location of the previous definition.\n");
2186 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2189 declaration_t *ndeclaration
2190 = parse_declarator(specifiers, specifiers->type, false);
2192 declaration_t *declaration = record_declaration(ndeclaration);
2194 type_t *orig_type = declaration->type;
2195 type_t *type = skip_typeref(orig_type);
2196 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2197 parser_print_warning_prefix_pos(declaration->source_position);
2198 fprintf(stderr, "variable '%s' declared 'inline'\n",
2199 declaration->symbol->string);
2202 if(token.type == '=') {
2205 /* TODO: check that this is an allowed type (no function type) */
2207 if(declaration->init.initializer != NULL) {
2208 parser_error_multiple_definition(declaration, ndeclaration);
2211 initializer_t *initializer = parse_initializer(type);
2213 if(type->type == TYPE_ARRAY && initializer != NULL) {
2214 array_type_t *array_type = (array_type_t*) type;
2216 if(array_type->size == NULL) {
2217 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2219 cnst->expression.type = EXPR_CONST;
2220 cnst->expression.datatype = type_size_t;
2222 if(initializer->type == INITIALIZER_LIST) {
2223 initializer_list_t *list = &initializer->list;
2224 cnst->v.int_value = list->len;
2226 assert(initializer->type == INITIALIZER_STRING);
2227 initializer_string_t *string = &initializer->string;
2228 cnst->v.int_value = strlen(string->string) + 1;
2231 array_type->size = (expression_t*) cnst;
2236 ndeclaration->init.initializer = initializer;
2237 } else if(token.type == '{') {
2238 if(type->type != TYPE_FUNCTION) {
2239 parser_print_error_prefix();
2240 fprintf(stderr, "declarator '");
2241 print_type_ext(orig_type, declaration->symbol, NULL);
2242 fprintf(stderr, "' has a body but is not a function type.\n");
2247 if(declaration->init.statement != NULL) {
2248 parser_error_multiple_definition(declaration, ndeclaration);
2250 if(ndeclaration != declaration) {
2251 memcpy(&declaration->context, &ndeclaration->context,
2252 sizeof(declaration->context));
2255 int top = environment_top();
2256 context_t *last_context = context;
2257 set_context(&declaration->context);
2259 /* push function parameters */
2260 declaration_t *parameter = declaration->context.declarations;
2261 for( ; parameter != NULL; parameter = parameter->next) {
2262 environment_push(parameter);
2265 int label_stack_top = label_top();
2266 declaration_t *old_current_function = current_function;
2267 current_function = declaration;
2269 statement_t *statement = parse_compound_statement();
2271 assert(current_function == declaration);
2272 current_function = old_current_function;
2273 label_pop_to(label_stack_top);
2275 assert(context == &declaration->context);
2276 set_context(last_context);
2277 environment_pop_to(top);
2279 declaration->init.statement = statement;
2283 if(token.type != ',')
2290 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2293 if(token.type == ':') {
2295 parse_constant_expression();
2296 /* TODO (bitfields) */
2298 declaration_t *declaration
2299 = parse_declarator(specifiers, specifiers->type, true);
2301 /* TODO: check constraints for struct declarations */
2302 /* TODO: check for doubled fields */
2303 record_declaration(declaration);
2305 if(token.type == ':') {
2307 parse_constant_expression();
2308 /* TODO (bitfields) */
2312 if(token.type != ',')
2319 static void parse_compound_type_entries(void)
2323 while(token.type != '}' && token.type != T_EOF) {
2324 declaration_specifiers_t specifiers;
2325 memset(&specifiers, 0, sizeof(specifiers));
2326 parse_declaration_specifiers(&specifiers);
2328 parse_struct_declarators(&specifiers);
2330 if(token.type == T_EOF) {
2331 parse_error("unexpected error while parsing struct");
2336 static void parse_declaration(void)
2338 source_position_t source_position = token.source_position;
2340 declaration_specifiers_t specifiers;
2341 memset(&specifiers, 0, sizeof(specifiers));
2342 parse_declaration_specifiers(&specifiers);
2344 if(token.type == ';') {
2345 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2346 parse_warning_pos(source_position,
2347 "useless keyword in empty declaration");
2349 switch (specifiers.type->type) {
2350 case TYPE_COMPOUND_STRUCT:
2351 case TYPE_COMPOUND_UNION: {
2352 const compound_type_t *const comp_type =
2353 (const compound_type_t*)specifiers.type;
2354 if (comp_type->declaration->symbol == NULL) {
2355 parse_warning_pos(source_position,
2356 "unnamed struct/union that defines no instances");
2361 case TYPE_ENUM: break;
2364 parse_warning_pos(source_position, "empty declaration");
2370 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2372 declaration->type = specifiers.type;
2373 declaration->storage_class = specifiers.storage_class;
2374 declaration->source_position = source_position;
2375 record_declaration(declaration);
2378 parse_init_declarators(&specifiers);
2381 static type_t *parse_typename(void)
2383 declaration_specifiers_t specifiers;
2384 memset(&specifiers, 0, sizeof(specifiers));
2385 parse_declaration_specifiers(&specifiers);
2386 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2387 /* TODO: improve error message, user does probably not know what a
2388 * storage class is...
2390 parse_error("typename may not have a storage class");
2393 type_t *result = parse_abstract_declarator(specifiers.type);
2401 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2402 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2403 expression_t *left);
2405 typedef struct expression_parser_function_t expression_parser_function_t;
2406 struct expression_parser_function_t {
2407 unsigned precedence;
2408 parse_expression_function parser;
2409 unsigned infix_precedence;
2410 parse_expression_infix_function infix_parser;
2413 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2415 static expression_t *make_invalid_expression(void)
2417 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2418 expression->type = EXPR_INVALID;
2419 expression->source_position = token.source_position;
2423 static expression_t *expected_expression_error(void)
2425 parser_print_error_prefix();
2426 fprintf(stderr, "expected expression, got token ");
2427 print_token(stderr, & token);
2428 fprintf(stderr, "\n");
2432 return make_invalid_expression();
2435 static expression_t *parse_string_const(void)
2437 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2439 cnst->expression.type = EXPR_STRING_LITERAL;
2440 cnst->expression.datatype = type_string;
2441 cnst->value = parse_string_literals();
2443 return (expression_t*) cnst;
2446 static expression_t *parse_int_const(void)
2448 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2450 cnst->expression.type = EXPR_CONST;
2451 cnst->expression.datatype = token.datatype;
2452 cnst->v.int_value = token.v.intvalue;
2456 return (expression_t*) cnst;
2459 static expression_t *parse_float_const(void)
2461 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2463 cnst->expression.type = EXPR_CONST;
2464 cnst->expression.datatype = token.datatype;
2465 cnst->v.float_value = token.v.floatvalue;
2469 return (expression_t*) cnst;
2472 static declaration_t *create_implicit_function(symbol_t *symbol,
2473 const source_position_t source_position)
2475 function_type_t *function_type
2476 = allocate_type_zero(sizeof(function_type[0]));
2478 function_type->type.type = TYPE_FUNCTION;
2479 function_type->result_type = type_int;
2480 function_type->unspecified_parameters = true;
2482 type_t *type = typehash_insert((type_t*) function_type);
2483 if(type != (type_t*) function_type) {
2484 free_type(function_type);
2487 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2489 declaration->storage_class = STORAGE_CLASS_EXTERN;
2490 declaration->type = type;
2491 declaration->symbol = symbol;
2492 declaration->source_position = source_position;
2494 /* prepend the implicit definition to the global context
2495 * this is safe since the symbol wasn't declared as anything else yet
2497 assert(symbol->declaration == NULL);
2499 context_t *last_context = context;
2500 context = global_context;
2502 environment_push(declaration);
2503 declaration->next = context->declarations;
2504 context->declarations = declaration;
2506 context = last_context;
2511 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2513 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2514 parameter->type = argument_type;
2516 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2517 type->type.type = TYPE_FUNCTION;
2518 type->result_type = result_type;
2519 type->parameters = parameter;
2521 type_t *result = typehash_insert((type_t*) type);
2522 if(result != (type_t*) type) {
2529 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2531 switch(symbol->ID) {
2532 case T___builtin_alloca:
2533 return make_function_1_type(type_void_ptr, type_size_t);
2535 panic("not implemented builtin symbol found");
2540 * performs automatic type cast as described in § 6.3.2.1
2542 static type_t *automatic_type_conversion(type_t *type)
2547 if(type->type == TYPE_ARRAY) {
2548 array_type_t *array_type = (array_type_t*) type;
2549 type_t *element_type = array_type->element_type;
2550 unsigned qualifiers = array_type->type.qualifiers;
2552 return make_pointer_type(element_type, qualifiers);
2555 if(type->type == TYPE_FUNCTION) {
2556 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2563 * reverts the automatic casts of array to pointer types and function
2564 * to function-pointer types as defined § 6.3.2.1
2566 type_t *revert_automatic_type_conversion(const expression_t *expression)
2568 if(expression->datatype == NULL)
2571 switch(expression->type) {
2572 case EXPR_REFERENCE: {
2573 const reference_expression_t *ref
2574 = (const reference_expression_t*) expression;
2575 return ref->declaration->type;
2578 const select_expression_t *select
2579 = (const select_expression_t*) expression;
2580 return select->compound_entry->type;
2583 const unary_expression_t *unary
2584 = (const unary_expression_t*) expression;
2585 if(unary->type == UNEXPR_DEREFERENCE) {
2586 expression_t *value = unary->value;
2587 type_t *type = skip_typeref(value->datatype);
2588 pointer_type_t *pointer_type = (pointer_type_t*) type;
2590 return pointer_type->points_to;
2594 case EXPR_BUILTIN_SYMBOL: {
2595 const builtin_symbol_expression_t *builtin
2596 = (const builtin_symbol_expression_t*) expression;
2597 return get_builtin_symbol_type(builtin->symbol);
2599 case EXPR_ARRAY_ACCESS: {
2600 const array_access_expression_t *array_access
2601 = (const array_access_expression_t*) expression;
2602 type_t *type_left = skip_typeref(array_access->array_ref->datatype);
2603 assert(is_type_pointer(type_left));
2604 pointer_type_t *pointer_type = (pointer_type_t*) type_left;
2605 return pointer_type->points_to;
2612 return expression->datatype;
2615 static expression_t *parse_reference(void)
2617 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2619 ref->expression.type = EXPR_REFERENCE;
2620 ref->symbol = token.v.symbol;
2622 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2624 source_position_t source_position = token.source_position;
2627 if(declaration == NULL) {
2629 /* an implicitly defined function */
2630 if(token.type == '(') {
2631 parser_print_prefix_pos(token.source_position);
2632 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2633 ref->symbol->string);
2635 declaration = create_implicit_function(ref->symbol,
2640 parser_print_error_prefix();
2641 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2642 return (expression_t*) ref;
2646 type_t *type = declaration->type;
2647 /* we always do the auto-type conversions; the & and sizeof parser contains
2648 * code to revert this! */
2649 type = automatic_type_conversion(type);
2651 ref->declaration = declaration;
2652 ref->expression.datatype = type;
2654 return (expression_t*) ref;
2657 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2661 /* TODO check if explicit cast is allowed and issue warnings/errors */
2664 static expression_t *parse_cast(void)
2666 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2668 cast->expression.type = EXPR_UNARY;
2669 cast->type = UNEXPR_CAST;
2670 cast->expression.source_position = token.source_position;
2672 type_t *type = parse_typename();
2675 expression_t *value = parse_sub_expression(20);
2677 check_cast_allowed(value, type);
2679 cast->expression.datatype = type;
2680 cast->value = value;
2682 return (expression_t*) cast;
2685 static expression_t *parse_statement_expression(void)
2687 statement_expression_t *expression
2688 = allocate_ast_zero(sizeof(expression[0]));
2689 expression->expression.type = EXPR_STATEMENT;
2691 statement_t *statement = parse_compound_statement();
2692 expression->statement = statement;
2693 if(statement == NULL) {
2698 assert(statement->type == STATEMENT_COMPOUND);
2699 compound_statement_t *compound_statement
2700 = (compound_statement_t*) statement;
2702 /* find last statement and use it's type */
2703 const statement_t *last_statement = NULL;
2704 const statement_t *iter = compound_statement->statements;
2705 for( ; iter != NULL; iter = iter->next) {
2706 last_statement = iter;
2709 if(last_statement->type == STATEMENT_EXPRESSION) {
2710 const expression_statement_t *expression_statement =
2711 (const expression_statement_t*) last_statement;
2712 expression->expression.datatype
2713 = expression_statement->expression->datatype;
2715 expression->expression.datatype = type_void;
2720 return (expression_t*) expression;
2723 static expression_t *parse_brace_expression(void)
2727 switch(token.type) {
2729 /* gcc extension: a stement expression */
2730 return parse_statement_expression();
2734 return parse_cast();
2736 if(is_typedef_symbol(token.v.symbol)) {
2737 return parse_cast();
2741 expression_t *result = parse_expression();
2747 static expression_t *parse_function_keyword(void)
2752 if (current_function == NULL) {
2753 parse_error("'__func__' used outside of a function");
2756 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2757 expression->expression.type = EXPR_FUNCTION;
2758 expression->expression.datatype = type_string;
2759 expression->value = "TODO: FUNCTION";
2761 return (expression_t*) expression;
2764 static expression_t *parse_pretty_function_keyword(void)
2766 eat(T___PRETTY_FUNCTION__);
2769 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2770 expression->expression.type = EXPR_PRETTY_FUNCTION;
2771 expression->expression.datatype = type_string;
2772 expression->value = "TODO: PRETTY FUNCTION";
2774 return (expression_t*) expression;
2777 static designator_t *parse_designator(void)
2779 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2781 if(token.type != T_IDENTIFIER) {
2782 parse_error_expected("while parsing member designator",
2787 result->symbol = token.v.symbol;
2790 designator_t *last_designator = result;
2792 if(token.type == '.') {
2794 if(token.type != T_IDENTIFIER) {
2795 parse_error_expected("while parsing member designator",
2800 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2801 designator->symbol = token.v.symbol;
2804 last_designator->next = designator;
2805 last_designator = designator;
2808 if(token.type == '[') {
2810 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2811 designator->array_access = parse_expression();
2812 if(designator->array_access == NULL) {
2818 last_designator->next = designator;
2819 last_designator = designator;
2828 static expression_t *parse_offsetof(void)
2830 eat(T___builtin_offsetof);
2832 offsetof_expression_t *expression
2833 = allocate_ast_zero(sizeof(expression[0]));
2834 expression->expression.type = EXPR_OFFSETOF;
2835 expression->expression.datatype = type_size_t;
2838 expression->type = parse_typename();
2840 expression->designator = parse_designator();
2843 return (expression_t*) expression;
2846 static expression_t *parse_va_arg(void)
2848 eat(T___builtin_va_arg);
2850 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2851 expression->expression.type = EXPR_VA_ARG;
2854 expression->arg = parse_assignment_expression();
2856 expression->expression.datatype = parse_typename();
2859 return (expression_t*) expression;
2862 static expression_t *parse_builtin_symbol(void)
2864 builtin_symbol_expression_t *expression
2865 = allocate_ast_zero(sizeof(expression[0]));
2866 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2868 expression->symbol = token.v.symbol;
2871 type_t *type = get_builtin_symbol_type(expression->symbol);
2872 type = automatic_type_conversion(type);
2874 expression->expression.datatype = type;
2875 return (expression_t*) expression;
2878 static expression_t *parse_primary_expression(void)
2880 switch(token.type) {
2882 return parse_int_const();
2883 case T_FLOATINGPOINT:
2884 return parse_float_const();
2885 case T_STRING_LITERAL:
2886 return parse_string_const();
2888 return parse_reference();
2889 case T___FUNCTION__:
2891 return parse_function_keyword();
2892 case T___PRETTY_FUNCTION__:
2893 return parse_pretty_function_keyword();
2894 case T___builtin_offsetof:
2895 return parse_offsetof();
2896 case T___builtin_va_arg:
2897 return parse_va_arg();
2898 case T___builtin_alloca:
2899 case T___builtin_expect:
2900 case T___builtin_va_start:
2901 case T___builtin_va_end:
2902 return parse_builtin_symbol();
2905 return parse_brace_expression();
2908 parser_print_error_prefix();
2909 fprintf(stderr, "unexpected token ");
2910 print_token(stderr, &token);
2911 fprintf(stderr, "\n");
2914 return make_invalid_expression();
2917 static expression_t *parse_array_expression(unsigned precedence,
2924 expression_t *inside = parse_expression();
2926 array_access_expression_t *array_access
2927 = allocate_ast_zero(sizeof(array_access[0]));
2929 array_access->expression.type = EXPR_ARRAY_ACCESS;
2931 type_t *type_left = skip_typeref(left->datatype);
2932 type_t *type_inside = skip_typeref(inside->datatype);
2933 type_t *result_type;
2935 if(type_left != NULL && type_inside != NULL) {
2936 if(is_type_pointer(type_left)) {
2937 pointer_type_t *pointer = (pointer_type_t*) type_left;
2938 result_type = pointer->points_to;
2939 array_access->array_ref = left;
2940 array_access->index = inside;
2941 } else if(is_type_pointer(type_inside)) {
2942 pointer_type_t *pointer = (pointer_type_t*) type_inside;
2943 result_type = pointer->points_to;
2944 array_access->array_ref = inside;
2945 array_access->index = left;
2946 array_access->flipped = true;
2948 parser_print_error_prefix();
2949 fprintf(stderr, "array access on object with non-pointer types ");
2950 print_type_quoted(type_left);
2951 fprintf(stderr, ", ");
2952 print_type_quoted(type_inside);
2953 fprintf(stderr, "\n");
2956 array_access->array_ref = left;
2957 array_access->index = inside;
2960 if(token.type != ']') {
2961 parse_error_expected("Problem while parsing array access", ']', 0);
2962 return (expression_t*) array_access;
2966 result_type = automatic_type_conversion(result_type);
2967 array_access->expression.datatype = result_type;
2969 return (expression_t*) array_access;
2972 static bool is_declaration_specifier(const token_t *token,
2973 bool only_type_specifiers)
2975 switch(token->type) {
2979 return is_typedef_symbol(token->v.symbol);
2982 if(only_type_specifiers)
2991 static expression_t *parse_sizeof(unsigned precedence)
2995 sizeof_expression_t *sizeof_expression
2996 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2997 sizeof_expression->expression.type = EXPR_SIZEOF;
2998 sizeof_expression->expression.datatype = type_size_t;
3000 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3002 sizeof_expression->type = parse_typename();
3005 expression_t *expression = parse_sub_expression(precedence);
3006 expression->datatype = revert_automatic_type_conversion(expression);
3008 sizeof_expression->type = expression->datatype;
3009 sizeof_expression->size_expression = expression;
3012 return (expression_t*) sizeof_expression;
3015 static expression_t *parse_select_expression(unsigned precedence,
3016 expression_t *compound)
3019 assert(token.type == '.' || token.type == T_MINUSGREATER);
3021 bool is_pointer = (token.type == T_MINUSGREATER);
3024 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
3026 select->expression.type = EXPR_SELECT;
3027 select->compound = compound;
3029 if(token.type != T_IDENTIFIER) {
3030 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3031 return (expression_t*) select;
3033 symbol_t *symbol = token.v.symbol;
3034 select->symbol = symbol;
3037 type_t *orig_type = compound->datatype;
3038 if(orig_type == NULL)
3039 return make_invalid_expression();
3041 type_t *type = skip_typeref(orig_type);
3043 type_t *type_left = type;
3045 if(type->type != TYPE_POINTER) {
3046 parser_print_error_prefix();
3047 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3048 print_type_quoted(orig_type);
3049 fputc('\n', stderr);
3050 return make_invalid_expression();
3052 pointer_type_t *pointer_type = (pointer_type_t*) type;
3053 type_left = pointer_type->points_to;
3055 type_left = skip_typeref(type_left);
3057 if(type_left->type != TYPE_COMPOUND_STRUCT
3058 && type_left->type != TYPE_COMPOUND_UNION) {
3059 parser_print_error_prefix();
3060 fprintf(stderr, "request for member '%s' in something not a struct or "
3061 "union, but ", symbol->string);
3062 print_type_quoted(type_left);
3063 fputc('\n', stderr);
3064 return make_invalid_expression();
3067 compound_type_t *compound_type = (compound_type_t*) type_left;
3068 declaration_t *declaration = compound_type->declaration;
3070 if(!declaration->init.is_defined) {
3071 parser_print_error_prefix();
3072 fprintf(stderr, "request for member '%s' of incomplete type ",
3074 print_type_quoted(type_left);
3075 fputc('\n', stderr);
3076 return make_invalid_expression();
3079 declaration_t *iter = declaration->context.declarations;
3080 for( ; iter != NULL; iter = iter->next) {
3081 if(iter->symbol == symbol) {
3086 parser_print_error_prefix();
3087 print_type_quoted(type_left);
3088 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3089 return make_invalid_expression();
3092 /* we always do the auto-type conversions; the & and sizeof parser contains
3093 * code to revert this! */
3094 type_t *expression_type = automatic_type_conversion(iter->type);
3096 select->compound_entry = iter;
3097 select->expression.datatype = expression_type;
3098 return (expression_t*) select;
3101 static expression_t *parse_call_expression(unsigned precedence,
3102 expression_t *expression)
3105 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3106 call->expression.type = EXPR_CALL;
3107 call->function = expression;
3109 function_type_t *function_type;
3110 type_t *orig_type = expression->datatype;
3111 if(orig_type != NULL) {
3112 function_type = NULL;
3113 type_t *type = skip_typeref(orig_type);
3115 if(is_type_pointer(type)) {
3116 pointer_type_t *pointer_type = (pointer_type_t*) type;
3118 type = skip_typeref(pointer_type->points_to);
3120 if (type->type == TYPE_FUNCTION) {
3121 function_type = (function_type_t*) type;
3122 call->expression.datatype = function_type->result_type;
3125 if(function_type == NULL) {
3126 parser_print_error_prefix();
3127 fputs("called object '", stderr);
3128 print_expression(expression);
3129 fputs("' (type ", stderr);
3130 print_type_quoted(orig_type);
3131 fputs(") is not a pointer to a function\n", stderr);
3133 function_type = NULL;
3134 call->expression.datatype = NULL;
3138 /* parse arguments */
3141 if(token.type != ')') {
3142 call_argument_t *last_argument = NULL;
3145 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3147 argument->expression = parse_assignment_expression();
3148 if(last_argument == NULL) {
3149 call->arguments = argument;
3151 last_argument->next = argument;
3153 last_argument = argument;
3155 if(token.type != ',')
3162 if(function_type != NULL) {
3163 function_parameter_t *parameter = function_type->parameters;
3164 call_argument_t *argument = call->arguments;
3165 for( ; parameter != NULL && argument != NULL;
3166 parameter = parameter->next, argument = argument->next) {
3167 type_t *expected_type = parameter->type;
3168 /* TODO report context in error messages */
3169 argument->expression = create_implicit_cast(argument->expression,
3172 /* too few parameters */
3173 if(parameter != NULL) {
3174 parser_print_error_prefix();
3175 fprintf(stderr, "too few arguments to function '");
3176 print_expression(expression);
3177 fprintf(stderr, "'\n");
3178 } else if(argument != NULL) {
3179 /* too many parameters */
3180 if(!function_type->variadic
3181 && !function_type->unspecified_parameters) {
3182 parser_print_error_prefix();
3183 fprintf(stderr, "too many arguments to function '");
3184 print_expression(expression);
3185 fprintf(stderr, "'\n");
3187 /* do default promotion */
3188 for( ; argument != NULL; argument = argument->next) {
3189 type_t *type = argument->expression->datatype;
3190 type = skip_typeref(type);
3195 if(is_type_integer(type)) {
3196 type = promote_integer(type);
3197 } else if(type == type_float) {
3201 argument->expression
3202 = create_implicit_cast(argument->expression, type);
3208 return (expression_t*) call;
3211 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3213 static expression_t *parse_conditional_expression(unsigned precedence,
3214 expression_t *expression)
3218 conditional_expression_t *conditional
3219 = allocate_ast_zero(sizeof(conditional[0]));
3220 conditional->expression.type = EXPR_CONDITIONAL;
3221 conditional->condition = expression;
3224 type_t *condition_type_orig = conditional->condition->datatype;
3225 if(condition_type_orig != NULL) {
3226 type_t *condition_type = skip_typeref(condition_type_orig);
3227 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3228 type_error("expected a scalar type", expression->source_position,
3229 condition_type_orig);
3233 expression_t *const t_expr = parse_expression();
3234 conditional->true_expression = t_expr;
3236 expression_t *const f_expr = parse_sub_expression(precedence);
3237 conditional->false_expression = f_expr;
3239 type_t *const true_type = t_expr->datatype;
3240 if(true_type == NULL)
3241 return (expression_t*) conditional;
3242 type_t *const false_type = f_expr->datatype;
3243 if(false_type == NULL)
3244 return (expression_t*) conditional;
3246 type_t *const skipped_true_type = skip_typeref(true_type);
3247 type_t *const skipped_false_type = skip_typeref(false_type);
3250 if (skipped_true_type == skipped_false_type) {
3251 conditional->expression.datatype = skipped_true_type;
3252 } else if (is_type_arithmetic(skipped_true_type) &&
3253 is_type_arithmetic(skipped_false_type)) {
3254 type_t *const result = semantic_arithmetic(skipped_true_type,
3255 skipped_false_type);
3256 conditional->true_expression = create_implicit_cast(t_expr, result);
3257 conditional->false_expression = create_implicit_cast(f_expr, result);
3258 conditional->expression.datatype = result;
3259 } else if (skipped_true_type->type == TYPE_POINTER &&
3260 skipped_false_type->type == TYPE_POINTER &&
3261 true /* TODO compatible points_to types */) {
3263 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3264 skipped_false_type->type == TYPE_POINTER)
3265 || (is_null_ptr_const(skipped_false_type) &&
3266 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3268 } else if(/* 1 is pointer to object type, other is void* */ false) {
3271 type_error_incompatible("while parsing conditional",
3272 expression->source_position, true_type,
3273 skipped_false_type);
3276 return (expression_t*) conditional;
3279 static expression_t *parse_extension(unsigned precedence)
3281 eat(T___extension__);
3283 /* TODO enable extensions */
3285 return parse_sub_expression(precedence);
3288 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3290 eat(T___builtin_classify_type);
3292 classify_type_expression_t *const classify_type_expr =
3293 allocate_ast_zero(sizeof(classify_type_expr[0]));
3294 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3295 classify_type_expr->expression.datatype = type_int;
3298 expression_t *const expression = parse_sub_expression(precedence);
3300 classify_type_expr->type_expression = expression;
3302 return (expression_t*)classify_type_expr;
3305 static void semantic_incdec(unary_expression_t *expression)
3307 type_t *orig_type = expression->value->datatype;
3308 if(orig_type == NULL)
3311 type_t *type = skip_typeref(orig_type);
3312 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3313 /* TODO: improve error message */
3314 parser_print_error_prefix();
3315 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3319 expression->expression.datatype = orig_type;
3322 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3324 type_t *orig_type = expression->value->datatype;
3325 if(orig_type == NULL)
3328 type_t *type = skip_typeref(orig_type);
3329 if(!is_type_arithmetic(type)) {
3330 /* TODO: improve error message */
3331 parser_print_error_prefix();
3332 fprintf(stderr, "operation needs an arithmetic type\n");
3336 expression->expression.datatype = orig_type;
3339 static void semantic_unexpr_scalar(unary_expression_t *expression)
3341 type_t *orig_type = expression->value->datatype;
3342 if(orig_type == NULL)
3345 type_t *type = skip_typeref(orig_type);
3346 if (!is_type_scalar(type)) {
3347 parse_error("operand of ! must be of scalar type\n");
3351 expression->expression.datatype = orig_type;
3354 static void semantic_unexpr_integer(unary_expression_t *expression)
3356 type_t *orig_type = expression->value->datatype;
3357 if(orig_type == NULL)
3360 type_t *type = skip_typeref(orig_type);
3361 if (!is_type_integer(type)) {
3362 parse_error("operand of ~ must be of integer type\n");
3366 expression->expression.datatype = orig_type;
3369 static void semantic_dereference(unary_expression_t *expression)
3371 type_t *orig_type = expression->value->datatype;
3372 if(orig_type == NULL)
3375 type_t *type = skip_typeref(orig_type);
3376 if(!is_type_pointer(type)) {
3377 parser_print_error_prefix();
3378 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3379 print_type_quoted(orig_type);
3380 fputs(" given.\n", stderr);
3384 pointer_type_t *pointer_type = (pointer_type_t*)type;
3385 type_t *result_type = pointer_type->points_to;
3387 result_type = automatic_type_conversion(result_type);
3388 expression->expression.datatype = result_type;
3391 static void semantic_take_addr(unary_expression_t *expression)
3393 expression_t *value = expression->value;
3394 value->datatype = revert_automatic_type_conversion(value);
3396 type_t *orig_type = value->datatype;
3397 if(orig_type == NULL)
3400 if(value->type == EXPR_REFERENCE) {
3401 reference_expression_t *reference = (reference_expression_t*) value;
3402 declaration_t *declaration = reference->declaration;
3403 if(declaration != NULL) {
3404 declaration->address_taken = 1;
3408 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3411 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3412 static expression_t *parse_##unexpression_type(unsigned precedence) \
3416 unary_expression_t *unary_expression \
3417 = allocate_ast_zero(sizeof(unary_expression[0])); \
3418 unary_expression->expression.type = EXPR_UNARY; \
3419 unary_expression->type = unexpression_type; \
3420 unary_expression->value = parse_sub_expression(precedence); \
3422 sfunc(unary_expression); \
3424 return (expression_t*) unary_expression; \
3427 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3428 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3429 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3430 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3431 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3432 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3433 semantic_unexpr_integer)
3434 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3436 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3439 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3441 static expression_t *parse_##unexpression_type(unsigned precedence, \
3442 expression_t *left) \
3444 (void) precedence; \
3447 unary_expression_t *unary_expression \
3448 = allocate_ast_zero(sizeof(unary_expression[0])); \
3449 unary_expression->expression.type = EXPR_UNARY; \
3450 unary_expression->type = unexpression_type; \
3451 unary_expression->value = left; \
3453 sfunc(unary_expression); \
3455 return (expression_t*) unary_expression; \
3458 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3460 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3463 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3465 /* TODO: handle complex + imaginary types */
3467 /* § 6.3.1.8 Usual arithmetic conversions */
3468 if(type_left == type_long_double || type_right == type_long_double) {
3469 return type_long_double;
3470 } else if(type_left == type_double || type_right == type_double) {
3472 } else if(type_left == type_float || type_right == type_float) {
3476 type_right = promote_integer(type_right);
3477 type_left = promote_integer(type_left);
3479 if(type_left == type_right)
3482 bool signed_left = is_type_signed(type_left);
3483 bool signed_right = is_type_signed(type_right);
3484 if(get_rank(type_left) < get_rank(type_right)) {
3485 if(signed_left == signed_right || !signed_right) {
3491 if(signed_left == signed_right || !signed_left) {
3499 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3501 expression_t *left = expression->left;
3502 expression_t *right = expression->right;
3503 type_t *orig_type_left = left->datatype;
3504 type_t *orig_type_right = right->datatype;
3506 if(orig_type_left == NULL || orig_type_right == NULL)
3509 type_t *type_left = skip_typeref(orig_type_left);
3510 type_t *type_right = skip_typeref(orig_type_right);
3512 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3513 /* TODO: improve error message */
3514 parser_print_error_prefix();
3515 fprintf(stderr, "operation needs arithmetic types\n");
3519 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3520 expression->left = create_implicit_cast(left, arithmetic_type);
3521 expression->right = create_implicit_cast(right, arithmetic_type);
3522 expression->expression.datatype = arithmetic_type;
3525 static void semantic_shift_op(binary_expression_t *expression)
3527 expression_t *left = expression->left;
3528 expression_t *right = expression->right;
3529 type_t *orig_type_left = left->datatype;
3530 type_t *orig_type_right = right->datatype;
3532 if(orig_type_left == NULL || orig_type_right == NULL)
3535 type_t *type_left = skip_typeref(orig_type_left);
3536 type_t *type_right = skip_typeref(orig_type_right);
3538 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3539 /* TODO: improve error message */
3540 parser_print_error_prefix();
3541 fprintf(stderr, "operation needs integer types\n");
3545 type_left = promote_integer(type_left);
3546 type_right = promote_integer(type_right);
3548 expression->left = create_implicit_cast(left, type_left);
3549 expression->right = create_implicit_cast(right, type_right);
3550 expression->expression.datatype = type_left;
3553 static void semantic_add(binary_expression_t *expression)
3555 expression_t *left = expression->left;
3556 expression_t *right = expression->right;
3557 type_t *orig_type_left = left->datatype;
3558 type_t *orig_type_right = right->datatype;
3560 if(orig_type_left == NULL || orig_type_right == NULL)
3563 type_t *type_left = skip_typeref(orig_type_left);
3564 type_t *type_right = skip_typeref(orig_type_right);
3567 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3568 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3569 expression->left = create_implicit_cast(left, arithmetic_type);
3570 expression->right = create_implicit_cast(right, arithmetic_type);
3571 expression->expression.datatype = arithmetic_type;
3573 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3574 expression->expression.datatype = type_left;
3575 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3576 expression->expression.datatype = type_right;
3578 parser_print_error_prefix();
3579 fprintf(stderr, "invalid operands to binary + (");
3580 print_type_quoted(orig_type_left);
3581 fprintf(stderr, ", ");
3582 print_type_quoted(orig_type_right);
3583 fprintf(stderr, ")\n");
3587 static void semantic_sub(binary_expression_t *expression)
3589 expression_t *left = expression->left;
3590 expression_t *right = expression->right;
3591 type_t *orig_type_left = left->datatype;
3592 type_t *orig_type_right = right->datatype;
3594 if(orig_type_left == NULL || orig_type_right == NULL)
3597 type_t *type_left = skip_typeref(orig_type_left);
3598 type_t *type_right = skip_typeref(orig_type_right);
3601 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3602 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3603 expression->left = create_implicit_cast(left, arithmetic_type);
3604 expression->right = create_implicit_cast(right, arithmetic_type);
3605 expression->expression.datatype = arithmetic_type;
3607 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3608 expression->expression.datatype = type_left;
3609 } else if(type_left->type == TYPE_POINTER &&
3610 type_right->type == TYPE_POINTER) {
3611 if(!pointers_compatible(type_left, type_right)) {
3612 parser_print_error_prefix();
3613 fprintf(stderr, "pointers to incompatible objects to binary - (");
3614 print_type_quoted(orig_type_left);
3615 fprintf(stderr, ", ");
3616 print_type_quoted(orig_type_right);
3617 fprintf(stderr, ")\n");
3619 expression->expression.datatype = type_ptrdiff_t;
3622 parser_print_error_prefix();
3623 fprintf(stderr, "invalid operands to binary - (");
3624 print_type_quoted(orig_type_left);
3625 fprintf(stderr, ", ");
3626 print_type_quoted(orig_type_right);
3627 fprintf(stderr, ")\n");
3631 static void semantic_comparison(binary_expression_t *expression)
3633 expression_t *left = expression->left;
3634 expression_t *right = expression->right;
3635 type_t *orig_type_left = left->datatype;
3636 type_t *orig_type_right = right->datatype;
3638 if(orig_type_left == NULL || orig_type_right == NULL)
3641 type_t *type_left = skip_typeref(orig_type_left);
3642 type_t *type_right = skip_typeref(orig_type_right);
3644 /* TODO non-arithmetic types */
3645 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3646 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3647 expression->left = create_implicit_cast(left, arithmetic_type);
3648 expression->right = create_implicit_cast(right, arithmetic_type);
3649 expression->expression.datatype = arithmetic_type;
3650 } else if (type_left->type == TYPE_POINTER &&
3651 type_right->type == TYPE_POINTER) {
3652 /* TODO check compatibility */
3653 } else if (type_left->type == TYPE_POINTER) {
3654 expression->right = create_implicit_cast(right, type_left);
3655 } else if (type_right->type == TYPE_POINTER) {
3656 expression->left = create_implicit_cast(left, type_right);
3658 type_error_incompatible("invalid operands in comparison",
3659 token.source_position, type_left, type_right);
3661 expression->expression.datatype = type_int;
3664 static void semantic_arithmetic_assign(binary_expression_t *expression)
3666 expression_t *left = expression->left;
3667 expression_t *right = expression->right;
3668 type_t *orig_type_left = left->datatype;
3669 type_t *orig_type_right = right->datatype;
3671 if(orig_type_left == NULL || orig_type_right == NULL)
3674 type_t *type_left = skip_typeref(orig_type_left);
3675 type_t *type_right = skip_typeref(orig_type_right);
3677 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3678 /* TODO: improve error message */
3679 parser_print_error_prefix();
3680 fprintf(stderr, "operation needs arithmetic types\n");
3684 /* combined instructions are tricky. We can't create an implicit cast on
3685 * the left side, because we need the uncasted form for the store.
3686 * The ast2firm pass has to know that left_type must be right_type
3687 * for the arithmeitc operation and create a cast by itself */
3688 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3689 expression->right = create_implicit_cast(right, arithmetic_type);
3690 expression->expression.datatype = type_left;
3693 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3695 expression_t *left = expression->left;
3696 expression_t *right = expression->right;
3697 type_t *orig_type_left = left->datatype;
3698 type_t *orig_type_right = right->datatype;
3700 if(orig_type_left == NULL || orig_type_right == NULL)
3703 type_t *type_left = skip_typeref(orig_type_left);
3704 type_t *type_right = skip_typeref(orig_type_right);
3706 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3707 /* combined instructions are tricky. We can't create an implicit cast on
3708 * the left side, because we need the uncasted form for the store.
3709 * The ast2firm pass has to know that left_type must be right_type
3710 * for the arithmeitc operation and create a cast by itself */
3711 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3712 expression->right = create_implicit_cast(right, arithmetic_type);
3713 expression->expression.datatype = type_left;
3714 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3715 expression->expression.datatype = type_left;
3717 parser_print_error_prefix();
3718 fputs("Incompatible types ", stderr);
3719 print_type_quoted(orig_type_left);
3720 fputs(" and ", stderr);
3721 print_type_quoted(orig_type_right);
3722 fputs(" in assignment\n", stderr);
3727 static void semantic_logical_op(binary_expression_t *expression)
3729 expression_t *left = expression->left;
3730 expression_t *right = expression->right;
3731 type_t *orig_type_left = left->datatype;
3732 type_t *orig_type_right = right->datatype;
3734 if(orig_type_left == NULL || orig_type_right == NULL)
3737 type_t *type_left = skip_typeref(orig_type_left);
3738 type_t *type_right = skip_typeref(orig_type_right);
3740 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3741 /* TODO: improve error message */
3742 parser_print_error_prefix();
3743 fprintf(stderr, "operation needs scalar types\n");
3747 expression->expression.datatype = type_int;
3750 static bool has_const_fields(type_t *type)
3757 static void semantic_binexpr_assign(binary_expression_t *expression)
3759 expression_t *left = expression->left;
3760 type_t *orig_type_left = left->datatype;
3762 if(orig_type_left == NULL)
3765 type_t *type_left = revert_automatic_type_conversion(left);
3766 type_left = skip_typeref(orig_type_left);
3768 /* must be a modifiable lvalue */
3769 if (type_left->type == TYPE_ARRAY) {
3770 parser_print_error_prefix();
3771 fprintf(stderr, "Cannot assign to arrays ('");
3772 print_expression(left);
3773 fprintf(stderr, "')\n");
3776 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
3777 parser_print_error_prefix();
3778 fprintf(stderr, "assignment to readonly location '");
3779 print_expression(left);
3780 fprintf(stderr, "' (type ");
3781 print_type_quoted(orig_type_left);
3782 fprintf(stderr, ")\n");
3785 if(is_type_incomplete(type_left)) {
3786 parser_print_error_prefix();
3787 fprintf(stderr, "left-hand side of assignment '");
3788 print_expression(left);
3789 fprintf(stderr, "' has incomplete type ");
3790 print_type_quoted(orig_type_left);
3791 fprintf(stderr, "\n");
3794 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3795 parser_print_error_prefix();
3796 fprintf(stderr, "can't assign to '");
3797 print_expression(left);
3798 fprintf(stderr, "' because compound type ");
3799 print_type_quoted(orig_type_left);
3800 fprintf(stderr, " has readonly fields\n");
3804 semantic_assign(orig_type_left, &expression->right, "assignment");
3806 expression->expression.datatype = orig_type_left;
3809 static void semantic_comma(binary_expression_t *expression)
3811 expression->expression.datatype = expression->right->datatype;
3814 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3815 static expression_t *parse_##binexpression_type(unsigned precedence, \
3816 expression_t *left) \
3820 expression_t *right = parse_sub_expression(precedence + lr); \
3822 binary_expression_t *binexpr \
3823 = allocate_ast_zero(sizeof(binexpr[0])); \
3824 binexpr->expression.type = EXPR_BINARY; \
3825 binexpr->type = binexpression_type; \
3826 binexpr->left = left; \
3827 binexpr->right = right; \
3830 return (expression_t*) binexpr; \
3833 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3834 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3835 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3836 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3837 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3838 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3839 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3840 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3841 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3842 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3843 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3844 semantic_comparison, 1)
3845 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3846 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3847 semantic_comparison, 1)
3848 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3849 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3850 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3851 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3852 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3853 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3854 semantic_shift_op, 1)
3855 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3856 semantic_shift_op, 1)
3857 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3858 semantic_arithmetic_addsubb_assign, 0)
3859 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3860 semantic_arithmetic_addsubb_assign, 0)
3861 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3862 semantic_arithmetic_assign, 0)
3863 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3864 semantic_arithmetic_assign, 0)
3865 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3866 semantic_arithmetic_assign, 0)
3867 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3868 semantic_arithmetic_assign, 0)
3869 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3870 semantic_arithmetic_assign, 0)
3871 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3872 semantic_arithmetic_assign, 0)
3873 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3874 semantic_arithmetic_assign, 0)
3875 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3876 semantic_arithmetic_assign, 0)
3878 static expression_t *parse_sub_expression(unsigned precedence)
3880 if(token.type < 0) {
3881 return expected_expression_error();
3884 expression_parser_function_t *parser
3885 = &expression_parsers[token.type];
3886 source_position_t source_position = token.source_position;
3889 if(parser->parser != NULL) {
3890 left = parser->parser(parser->precedence);
3892 left = parse_primary_expression();
3894 assert(left != NULL);
3895 left->source_position = source_position;
3898 if(token.type < 0) {
3899 return expected_expression_error();
3902 parser = &expression_parsers[token.type];
3903 if(parser->infix_parser == NULL)
3905 if(parser->infix_precedence < precedence)
3908 left = parser->infix_parser(parser->infix_precedence, left);
3910 assert(left != NULL);
3911 assert(left->type != EXPR_UNKNOWN);
3912 left->source_position = source_position;
3918 static expression_t *parse_expression(void)
3920 return parse_sub_expression(1);
3925 static void register_expression_parser(parse_expression_function parser,
3926 int token_type, unsigned precedence)
3928 expression_parser_function_t *entry = &expression_parsers[token_type];
3930 if(entry->parser != NULL) {
3931 fprintf(stderr, "for token ");
3932 print_token_type(stderr, token_type);
3933 fprintf(stderr, "\n");
3934 panic("trying to register multiple expression parsers for a token");
3936 entry->parser = parser;
3937 entry->precedence = precedence;
3940 static void register_expression_infix_parser(
3941 parse_expression_infix_function parser, int token_type,
3942 unsigned precedence)
3944 expression_parser_function_t *entry = &expression_parsers[token_type];
3946 if(entry->infix_parser != NULL) {
3947 fprintf(stderr, "for token ");
3948 print_token_type(stderr, token_type);
3949 fprintf(stderr, "\n");
3950 panic("trying to register multiple infix expression parsers for a "
3953 entry->infix_parser = parser;
3954 entry->infix_precedence = precedence;
3957 static void init_expression_parsers(void)
3959 memset(&expression_parsers, 0, sizeof(expression_parsers));
3961 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3962 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3963 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3964 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3965 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3966 T_GREATERGREATER, 16);
3967 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3968 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3969 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3970 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3971 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3972 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3973 T_GREATEREQUAL, 14);
3974 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3975 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3976 T_EXCLAMATIONMARKEQUAL, 13);
3977 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3978 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3979 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3980 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3981 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3982 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3983 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3984 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3985 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3986 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3987 T_ASTERISKEQUAL, 2);
3988 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3989 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3991 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3992 T_LESSLESSEQUAL, 2);
3993 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3994 T_GREATERGREATEREQUAL, 2);
3995 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3997 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3999 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4002 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4004 register_expression_infix_parser(parse_array_expression, '[', 30);
4005 register_expression_infix_parser(parse_call_expression, '(', 30);
4006 register_expression_infix_parser(parse_select_expression, '.', 30);
4007 register_expression_infix_parser(parse_select_expression,
4008 T_MINUSGREATER, 30);
4009 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4011 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4014 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4015 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4016 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4017 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4018 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4019 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4020 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4021 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4022 register_expression_parser(parse_sizeof, T_sizeof, 25);
4023 register_expression_parser(parse_extension, T___extension__, 25);
4024 register_expression_parser(parse_builtin_classify_type,
4025 T___builtin_classify_type, 25);
4029 static statement_t *parse_case_statement(void)
4032 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4033 label->statement.type = STATEMENT_CASE_LABEL;
4034 label->statement.source_position = token.source_position;
4036 label->expression = parse_expression();
4039 label->label_statement = parse_statement();
4041 return (statement_t*) label;
4044 static statement_t *parse_default_statement(void)
4048 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4049 label->statement.type = STATEMENT_CASE_LABEL;
4050 label->statement.source_position = token.source_position;
4053 label->label_statement = parse_statement();
4055 return (statement_t*) label;
4058 static declaration_t *get_label(symbol_t *symbol)
4060 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4061 assert(current_function != NULL);
4062 /* if we found a label in the same function, then we already created the
4064 if(candidate != NULL
4065 && candidate->parent_context == ¤t_function->context) {
4069 /* otherwise we need to create a new one */
4070 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4071 declaration->namespc = NAMESPACE_LABEL;
4072 declaration->symbol = symbol;
4074 label_push(declaration);
4079 static statement_t *parse_label_statement(void)
4081 assert(token.type == T_IDENTIFIER);
4082 symbol_t *symbol = token.v.symbol;
4085 declaration_t *label = get_label(symbol);
4087 /* if source position is already set then the label is defined twice,
4088 * otherwise it was just mentioned in a goto so far */
4089 if(label->source_position.input_name != NULL) {
4090 parser_print_error_prefix();
4091 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4092 parser_print_error_prefix_pos(label->source_position);
4093 fprintf(stderr, "previous definition of '%s' was here\n",
4096 label->source_position = token.source_position;
4099 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4101 label_statement->statement.type = STATEMENT_LABEL;
4102 label_statement->statement.source_position = token.source_position;
4103 label_statement->label = label;
4107 if(token.type == '}') {
4108 parse_error("label at end of compound statement");
4109 return (statement_t*) label_statement;
4111 label_statement->label_statement = parse_statement();
4114 return (statement_t*) label_statement;
4117 static statement_t *parse_if(void)
4121 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4122 statement->statement.type = STATEMENT_IF;
4123 statement->statement.source_position = token.source_position;
4126 statement->condition = parse_expression();
4129 statement->true_statement = parse_statement();
4130 if(token.type == T_else) {
4132 statement->false_statement = parse_statement();
4135 return (statement_t*) statement;
4138 static statement_t *parse_switch(void)
4142 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4143 statement->statement.type = STATEMENT_SWITCH;
4144 statement->statement.source_position = token.source_position;
4147 statement->expression = parse_expression();
4149 statement->body = parse_statement();
4151 return (statement_t*) statement;
4154 static statement_t *parse_while(void)
4158 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4159 statement->statement.type = STATEMENT_WHILE;
4160 statement->statement.source_position = token.source_position;
4163 statement->condition = parse_expression();
4165 statement->body = parse_statement();
4167 return (statement_t*) statement;
4170 static statement_t *parse_do(void)
4174 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4175 statement->statement.type = STATEMENT_DO_WHILE;
4176 statement->statement.source_position = token.source_position;
4178 statement->body = parse_statement();
4181 statement->condition = parse_expression();
4185 return (statement_t*) statement;
4188 static statement_t *parse_for(void)
4192 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4193 statement->statement.type = STATEMENT_FOR;
4194 statement->statement.source_position = token.source_position;
4198 int top = environment_top();
4199 context_t *last_context = context;
4200 set_context(&statement->context);
4202 if(token.type != ';') {
4203 if(is_declaration_specifier(&token, false)) {
4204 parse_declaration();
4206 statement->initialisation = parse_expression();
4213 if(token.type != ';') {
4214 statement->condition = parse_expression();
4217 if(token.type != ')') {
4218 statement->step = parse_expression();
4221 statement->body = parse_statement();
4223 assert(context == &statement->context);
4224 set_context(last_context);
4225 environment_pop_to(top);
4227 return (statement_t*) statement;
4230 static statement_t *parse_goto(void)
4234 if(token.type != T_IDENTIFIER) {
4235 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4239 symbol_t *symbol = token.v.symbol;
4242 declaration_t *label = get_label(symbol);
4244 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4246 statement->statement.type = STATEMENT_GOTO;
4247 statement->statement.source_position = token.source_position;
4249 statement->label = label;
4253 return (statement_t*) statement;
4256 static statement_t *parse_continue(void)
4261 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4262 statement->type = STATEMENT_CONTINUE;
4263 statement->source_position = token.source_position;
4268 static statement_t *parse_break(void)
4273 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4274 statement->type = STATEMENT_BREAK;
4275 statement->source_position = token.source_position;
4280 static statement_t *parse_return(void)
4284 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4286 statement->statement.type = STATEMENT_RETURN;
4287 statement->statement.source_position = token.source_position;
4289 assert(current_function->type->type == TYPE_FUNCTION);
4290 function_type_t *function_type = (function_type_t*) current_function->type;
4291 type_t *return_type = function_type->result_type;
4293 expression_t *return_value;
4294 if(token.type != ';') {
4295 return_value = parse_expression();
4297 if(return_type == type_void && return_value->datatype != type_void) {
4298 parse_warning("'return' with a value, in function returning void");
4299 return_value = NULL;
4301 if(return_type != NULL) {
4302 semantic_assign(return_type, &return_value, "'return'");
4306 return_value = NULL;
4307 if(return_type != type_void) {
4308 parse_warning("'return' without value, in function returning "
4312 statement->return_value = return_value;
4316 return (statement_t*) statement;
4319 static statement_t *parse_declaration_statement(void)
4321 declaration_t *before = last_declaration;
4323 declaration_statement_t *statement
4324 = allocate_ast_zero(sizeof(statement[0]));
4325 statement->statement.type = STATEMENT_DECLARATION;
4326 statement->statement.source_position = token.source_position;
4328 declaration_specifiers_t specifiers;
4329 memset(&specifiers, 0, sizeof(specifiers));
4330 parse_declaration_specifiers(&specifiers);
4332 if(token.type == ';') {
4335 parse_init_declarators(&specifiers);
4338 if(before == NULL) {
4339 statement->declarations_begin = context->declarations;
4341 statement->declarations_begin = before->next;
4343 statement->declarations_end = last_declaration;
4345 return (statement_t*) statement;
4348 static statement_t *parse_expression_statement(void)
4350 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4351 statement->statement.type = STATEMENT_EXPRESSION;
4352 statement->statement.source_position = token.source_position;
4354 statement->expression = parse_expression();
4358 return (statement_t*) statement;
4361 static statement_t *parse_statement(void)
4363 statement_t *statement = NULL;
4365 /* declaration or statement */
4366 switch(token.type) {
4368 statement = parse_case_statement();
4372 statement = parse_default_statement();
4376 statement = parse_compound_statement();
4380 statement = parse_if();
4384 statement = parse_switch();
4388 statement = parse_while();
4392 statement = parse_do();
4396 statement = parse_for();
4400 statement = parse_goto();
4404 statement = parse_continue();
4408 statement = parse_break();
4412 statement = parse_return();
4421 if(look_ahead(1)->type == ':') {
4422 statement = parse_label_statement();
4426 if(is_typedef_symbol(token.v.symbol)) {
4427 statement = parse_declaration_statement();
4431 statement = parse_expression_statement();
4434 case T___extension__:
4435 /* this can be a prefix to a declaration or an expression statement */
4436 /* we simply eat it now and parse the rest with tail recursion */
4439 } while(token.type == T___extension__);
4440 statement = parse_statement();
4444 statement = parse_declaration_statement();
4448 statement = parse_expression_statement();
4452 assert(statement == NULL || statement->source_position.input_name != NULL);
4457 static statement_t *parse_compound_statement(void)
4459 compound_statement_t *compound_statement
4460 = allocate_ast_zero(sizeof(compound_statement[0]));
4461 compound_statement->statement.type = STATEMENT_COMPOUND;
4462 compound_statement->statement.source_position = token.source_position;
4466 int top = environment_top();
4467 context_t *last_context = context;
4468 set_context(&compound_statement->context);
4470 statement_t *last_statement = NULL;
4472 while(token.type != '}' && token.type != T_EOF) {
4473 statement_t *statement = parse_statement();
4474 if(statement == NULL)
4477 if(last_statement != NULL) {
4478 last_statement->next = statement;
4480 compound_statement->statements = statement;
4483 while(statement->next != NULL)
4484 statement = statement->next;
4486 last_statement = statement;
4489 if(token.type != '}') {
4490 parser_print_error_prefix_pos(
4491 compound_statement->statement.source_position);
4492 fprintf(stderr, "end of file while looking for closing '}'\n");
4496 assert(context == &compound_statement->context);
4497 set_context(last_context);
4498 environment_pop_to(top);
4500 return (statement_t*) compound_statement;
4503 static translation_unit_t *parse_translation_unit(void)
4505 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4507 assert(global_context == NULL);
4508 global_context = &unit->context;
4510 assert(context == NULL);
4511 set_context(&unit->context);
4513 while(token.type != T_EOF) {
4514 parse_declaration();
4517 assert(context == &unit->context);
4519 last_declaration = NULL;
4521 assert(global_context == &unit->context);
4522 global_context = NULL;
4527 translation_unit_t *parse(void)
4529 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4530 label_stack = NEW_ARR_F(stack_entry_t, 0);
4531 found_error = false;
4533 type_set_output(stderr);
4534 ast_set_output(stderr);
4536 lookahead_bufpos = 0;
4537 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4540 translation_unit_t *unit = parse_translation_unit();
4542 DEL_ARR_F(environment_stack);
4543 DEL_ARR_F(label_stack);
4551 void init_parser(void)
4553 init_expression_parsers();
4554 obstack_init(&temp_obst);
4556 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4557 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4558 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4559 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4560 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4561 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4562 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4563 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4564 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4565 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4566 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4569 void exit_parser(void)
4571 obstack_free(&temp_obst, NULL);