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 atomic_type_t *atomic_type = (atomic_type_t*) type;
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 = (array_type_t*) source_type;
710 pointer_type_t *pointer_type
711 = (pointer_type_t*) dest_type;
712 if (types_compatible(array_type->element_type,
713 pointer_type->points_to)) {
714 return create_cast_expression(expression, dest_type);
720 panic("casting of non-atomic types not implemented yet");
723 type_error_incompatible("can't implicitly cast types",
724 expression->source_position, source_type, dest_type);
728 panic("casting of non-atomic types not implemented yet");
732 /** Implements the rules from § 6.5.16.1 */
733 static void semantic_assign(type_t *orig_type_left, expression_t **right,
736 type_t *orig_type_right = (*right)->datatype;
738 if(orig_type_right == NULL)
741 type_t *const type_left = skip_typeref(orig_type_left);
742 type_t *const type_right = skip_typeref(orig_type_right);
744 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
745 (is_type_pointer(type_left) && is_null_expression(*right)) ||
746 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
747 && is_type_pointer(type_right))) {
748 *right = create_implicit_cast(*right, type_left);
752 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
753 pointer_type_t *pointer_type_left = (pointer_type_t*) type_left;
754 pointer_type_t *pointer_type_right = (pointer_type_t*) type_right;
755 type_t *points_to_left = pointer_type_left->points_to;
756 type_t *points_to_right = pointer_type_right->points_to;
758 points_to_left = skip_typeref(points_to_left);
759 points_to_right = skip_typeref(points_to_right);
761 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
762 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
763 && !types_compatible(points_to_left, points_to_right)) {
764 goto incompatible_assign_types;
767 /* the left type has all qualifiers from the right type */
768 unsigned missing_qualifiers
769 = points_to_right->qualifiers & ~points_to_left->qualifiers;
770 if(missing_qualifiers != 0) {
771 parser_print_error_prefix();
772 fprintf(stderr, "destination type ");
773 print_type_quoted(type_left);
774 fprintf(stderr, " in %s from type ", context);
775 print_type_quoted(type_right);
776 fprintf(stderr, " lacks qualifiers '");
777 print_type_qualifiers(missing_qualifiers);
778 fprintf(stderr, "' in pointed-to type\n");
782 *right = create_implicit_cast(*right, type_left);
786 if (is_type_compound(type_left)
787 && types_compatible(type_left, type_right)) {
788 *right = create_implicit_cast(*right, type_left);
792 incompatible_assign_types:
793 /* TODO: improve error message */
794 parser_print_error_prefix();
795 fprintf(stderr, "incompatible types in %s\n", context);
796 parser_print_error_prefix();
797 print_type_quoted(type_left);
798 fputs(" <- ", stderr);
799 print_type_quoted(type_right);
803 static expression_t *parse_constant_expression(void)
805 /* start parsing at precedence 7 (conditional expression) */
806 return parse_sub_expression(7);
809 static expression_t *parse_assignment_expression(void)
811 /* start parsing at precedence 2 (assignment expression) */
812 return parse_sub_expression(2);
815 typedef struct declaration_specifiers_t declaration_specifiers_t;
816 struct declaration_specifiers_t {
817 storage_class_t storage_class;
822 static void parse_compound_type_entries(void);
823 static declaration_t *parse_declarator(
824 const declaration_specifiers_t *specifiers, type_t *type,
825 bool may_be_abstract);
826 static declaration_t *record_declaration(declaration_t *declaration);
828 static const char *parse_string_literals(void)
830 assert(token.type == T_STRING_LITERAL);
831 const char *result = token.v.string;
835 while(token.type == T_STRING_LITERAL) {
836 result = concat_strings(result, token.v.string);
843 static void parse_attributes(void)
847 case T___attribute__:
855 parse_error("EOF while parsing attribute");
873 if(token.type != T_STRING_LITERAL) {
874 parse_error_expected("while parsing assembler attribute",
879 parse_string_literals();
884 goto attributes_finished;
893 static designator_t *parse_designation(void)
895 if(token.type != '[' && token.type != '.')
898 designator_t *result = NULL;
899 designator_t *last = NULL;
902 designator_t *designator;
905 designator = allocate_ast_zero(sizeof(designator[0]));
907 designator->array_access = parse_constant_expression();
911 designator = allocate_ast_zero(sizeof(designator[0]));
913 if(token.type != T_IDENTIFIER) {
914 parse_error_expected("while parsing designator",
918 designator->symbol = token.v.symbol;
926 assert(designator != NULL);
928 last->next = designator;
937 static initializer_t *initializer_from_string(array_type_t *type,
940 /* TODO: check len vs. size of array type */
943 initializer_t *initializer = allocate_initializer(INITIALIZER_STRING);
944 initializer->string.string = string;
949 static initializer_t *initializer_from_expression(type_t *type,
950 expression_t *expression)
952 /* TODO check that expression is a constant expression */
954 /* § 6.7.8.14/15 char array may be initialized by string literals */
955 if(type->type == TYPE_ARRAY && expression->type == EXPR_STRING_LITERAL) {
956 array_type_t *array_type = (array_type_t*) type;
957 type_t *element_type = array_type->element_type;
959 if(element_type->type == TYPE_ATOMIC) {
960 atomic_type_t *atomic_type = (atomic_type_t*) element_type;
961 atomic_type_type_t atype = atomic_type->atype;
963 /* TODO handle wide strings */
964 if(atype == ATOMIC_TYPE_CHAR
965 || atype == ATOMIC_TYPE_SCHAR
966 || atype == ATOMIC_TYPE_UCHAR) {
968 string_literal_t *literal = (string_literal_t*) expression;
969 return initializer_from_string(array_type, literal->value);
974 semantic_assign(type, &expression, "initializer");
976 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
977 result->value.value = expression;
982 static initializer_t *parse_sub_initializer(type_t *type,
983 expression_t *expression,
984 type_t *expression_type);
986 static initializer_t *parse_sub_initializer_elem(type_t *type)
988 if(token.type == '{') {
989 return parse_sub_initializer(type, NULL, NULL);
992 expression_t *expression = parse_assignment_expression();
993 type_t *expression_type = skip_typeref(expression->datatype);
995 return parse_sub_initializer(type, expression, expression_type);
998 static bool had_initializer_brace_warning;
1000 static initializer_t *parse_sub_initializer(type_t *type,
1001 expression_t *expression,
1002 type_t *expression_type)
1004 if(is_type_scalar(type)) {
1005 /* there might be extra {} hierarchies */
1006 if(token.type == '{') {
1008 if(!had_initializer_brace_warning) {
1009 parse_warning("braces around scalar initializer");
1010 had_initializer_brace_warning = true;
1012 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1013 if(token.type == ',') {
1015 /* TODO: warn about excessive elements */
1021 if(expression == NULL) {
1022 expression = parse_assignment_expression();
1024 return initializer_from_expression(type, expression);
1027 /* TODO: ignore qualifiers, comparing pointers is probably
1029 if(expression != NULL && expression_type == type) {
1030 initializer_t *result = allocate_initializer(INITIALIZER_VALUE);
1033 semantic_assign(type, &expression, "initializer");
1035 result->value.value = expression;
1040 bool read_paren = false;
1041 if(token.type == '{') {
1046 /* descend into subtype */
1047 initializer_t *result = NULL;
1048 initializer_t **elems;
1049 if(type->type == TYPE_ARRAY) {
1050 array_type_t *array_type = (array_type_t*) type;
1051 type_t *element_type = array_type->element_type;
1052 element_type = skip_typeref(element_type);
1055 had_initializer_brace_warning = false;
1056 if(expression == NULL) {
1057 sub = parse_sub_initializer_elem(element_type);
1059 sub = parse_sub_initializer(element_type, expression,
1063 /* didn't match the subtypes -> try the parent type */
1065 assert(!read_paren);
1069 elems = NEW_ARR_F(initializer_t*, 0);
1070 ARR_APP1(initializer_t*, elems, sub);
1073 if(token.type == '}')
1076 if(token.type == '}')
1080 = parse_sub_initializer(element_type, NULL, NULL);
1082 /* TODO error, do nicer cleanup */
1083 parse_error("member initializer didn't match");
1087 ARR_APP1(initializer_t*, elems, sub);
1090 assert(type->type == TYPE_COMPOUND_STRUCT
1091 || type->type == TYPE_COMPOUND_UNION);
1092 compound_type_t *compound_type = (compound_type_t*) type;
1093 context_t *context = & compound_type->declaration->context;
1095 declaration_t *first = context->declarations;
1098 type_t *first_type = first->type;
1099 first_type = skip_typeref(first_type);
1102 had_initializer_brace_warning = false;
1103 if(expression == NULL) {
1104 sub = parse_sub_initializer_elem(first_type);
1106 sub = parse_sub_initializer(first_type, expression,expression_type);
1109 /* didn't match the subtypes -> try our parent type */
1111 assert(!read_paren);
1115 elems = NEW_ARR_F(initializer_t*, 0);
1116 ARR_APP1(initializer_t*, elems, sub);
1118 declaration_t *iter = first->next;
1119 for( ; iter != NULL; iter = iter->next) {
1120 if(iter->symbol == NULL)
1122 if(iter->namespc != NAMESPACE_NORMAL)
1125 if(token.type == '}')
1128 if(token.type == '}')
1131 type_t *iter_type = iter->type;
1132 iter_type = skip_typeref(iter_type);
1134 initializer_t *sub = parse_sub_initializer(iter_type, NULL, NULL);
1136 /* TODO error, do nicer cleanup*/
1137 parse_error("member initializer didn't match");
1141 ARR_APP1(initializer_t*, elems, sub);
1145 int len = ARR_LEN(elems);
1146 size_t elems_size = sizeof(initializer_t*) * len;
1148 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1150 init->initializer.type = INITIALIZER_LIST;
1152 memcpy(init->initializers, elems, elems_size);
1155 result = (initializer_t*) init;
1158 if(token.type == ',')
1165 static initializer_t *parse_initializer(type_t *type)
1167 initializer_t *result;
1169 type = skip_typeref(type);
1171 if(token.type != '{') {
1172 expression_t *expression = parse_assignment_expression();
1173 return initializer_from_expression(type, expression);
1176 if(is_type_scalar(type)) {
1180 expression_t *expression = parse_assignment_expression();
1181 result = initializer_from_expression(type, expression);
1183 if(token.type == ',')
1189 result = parse_sub_initializer(type, NULL, NULL);
1197 static declaration_t *parse_compound_type_specifier(bool is_struct)
1205 symbol_t *symbol = NULL;
1206 declaration_t *declaration = NULL;
1208 if (token.type == T___attribute__) {
1213 if(token.type == T_IDENTIFIER) {
1214 symbol = token.v.symbol;
1218 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1220 declaration = get_declaration(symbol, NAMESPACE_UNION);
1222 } else if(token.type != '{') {
1224 parse_error_expected("while parsing struct type specifier",
1225 T_IDENTIFIER, '{', 0);
1227 parse_error_expected("while parsing union type specifier",
1228 T_IDENTIFIER, '{', 0);
1234 if(declaration == NULL) {
1235 declaration = allocate_type_zero(sizeof(declaration[0]));
1238 declaration->namespc = NAMESPACE_STRUCT;
1240 declaration->namespc = NAMESPACE_UNION;
1242 declaration->source_position = token.source_position;
1243 declaration->symbol = symbol;
1244 record_declaration(declaration);
1247 if(token.type == '{') {
1248 if(declaration->init.is_defined) {
1249 assert(symbol != NULL);
1250 parser_print_error_prefix();
1251 fprintf(stderr, "multiple definition of %s %s\n",
1252 is_struct ? "struct" : "union", symbol->string);
1253 declaration->context.declarations = NULL;
1255 declaration->init.is_defined = true;
1257 int top = environment_top();
1258 context_t *last_context = context;
1259 set_context(& declaration->context);
1261 parse_compound_type_entries();
1264 assert(context == & declaration->context);
1265 set_context(last_context);
1266 environment_pop_to(top);
1272 static void parse_enum_entries(enum_type_t *const enum_type)
1276 if(token.type == '}') {
1278 parse_error("empty enum not allowed");
1283 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1285 if(token.type != T_IDENTIFIER) {
1286 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1290 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1291 entry->type = (type_t*) enum_type;
1292 entry->symbol = token.v.symbol;
1293 entry->source_position = token.source_position;
1296 if(token.type == '=') {
1298 entry->init.enum_value = parse_constant_expression();
1303 record_declaration(entry);
1305 if(token.type != ',')
1308 } while(token.type != '}');
1313 static type_t *parse_enum_specifier(void)
1317 declaration_t *declaration;
1320 if(token.type == T_IDENTIFIER) {
1321 symbol = token.v.symbol;
1324 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1325 } else if(token.type != '{') {
1326 parse_error_expected("while parsing enum type specifier",
1327 T_IDENTIFIER, '{', 0);
1334 if(declaration == NULL) {
1335 declaration = allocate_type_zero(sizeof(declaration[0]));
1337 declaration->namespc = NAMESPACE_ENUM;
1338 declaration->source_position = token.source_position;
1339 declaration->symbol = symbol;
1342 enum_type_t *const enum_type = allocate_type_zero(sizeof(enum_type[0]));
1343 enum_type->type.type = TYPE_ENUM;
1344 enum_type->declaration = declaration;
1346 if(token.type == '{') {
1347 if(declaration->init.is_defined) {
1348 parser_print_error_prefix();
1349 fprintf(stderr, "multiple definitions of enum %s\n",
1352 record_declaration(declaration);
1353 declaration->init.is_defined = 1;
1355 parse_enum_entries(enum_type);
1359 return (type_t*) enum_type;
1363 * if a symbol is a typedef to another type, return true
1365 static bool is_typedef_symbol(symbol_t *symbol)
1367 const declaration_t *const declaration =
1368 get_declaration(symbol, NAMESPACE_NORMAL);
1370 declaration != NULL &&
1371 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1374 static type_t *parse_typeof(void)
1382 expression_t *expression = NULL;
1385 switch(token.type) {
1386 case T___extension__:
1387 /* this can be a prefix to a typename or an expression */
1388 /* we simply eat it now. */
1391 } while(token.type == T___extension__);
1395 if(is_typedef_symbol(token.v.symbol)) {
1396 type = parse_typename();
1398 expression = parse_expression();
1399 type = expression->datatype;
1404 type = parse_typename();
1408 expression = parse_expression();
1409 type = expression->datatype;
1415 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1416 typeof->type.type = TYPE_TYPEOF;
1417 typeof->expression = expression;
1418 typeof->typeof_type = type;
1420 return (type_t*) typeof;
1424 SPECIFIER_SIGNED = 1 << 0,
1425 SPECIFIER_UNSIGNED = 1 << 1,
1426 SPECIFIER_LONG = 1 << 2,
1427 SPECIFIER_INT = 1 << 3,
1428 SPECIFIER_DOUBLE = 1 << 4,
1429 SPECIFIER_CHAR = 1 << 5,
1430 SPECIFIER_SHORT = 1 << 6,
1431 SPECIFIER_LONG_LONG = 1 << 7,
1432 SPECIFIER_FLOAT = 1 << 8,
1433 SPECIFIER_BOOL = 1 << 9,
1434 SPECIFIER_VOID = 1 << 10,
1435 #ifdef PROVIDE_COMPLEX
1436 SPECIFIER_COMPLEX = 1 << 11,
1437 SPECIFIER_IMAGINARY = 1 << 12,
1441 static type_t *create_builtin_type(symbol_t *symbol)
1443 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1444 type->type.type = TYPE_BUILTIN;
1445 type->symbol = symbol;
1447 type->real_type = type_int;
1449 return (type_t*) type;
1452 static type_t *get_typedef_type(symbol_t *symbol)
1454 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1455 if(declaration == NULL
1456 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1459 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1460 typedef_type->type.type = TYPE_TYPEDEF;
1461 typedef_type->declaration = declaration;
1463 return (type_t*) typedef_type;
1466 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1468 type_t *type = NULL;
1469 unsigned type_qualifiers = 0;
1470 unsigned type_specifiers = 0;
1474 switch(token.type) {
1477 #define MATCH_STORAGE_CLASS(token, class) \
1479 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1480 parse_error("multiple storage classes in declaration " \
1483 specifiers->storage_class = class; \
1487 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1488 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1489 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1490 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1491 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1493 /* type qualifiers */
1494 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1496 type_qualifiers |= qualifier; \
1500 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1501 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1502 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1504 case T___extension__:
1509 /* type specifiers */
1510 #define MATCH_SPECIFIER(token, specifier, name) \
1513 if(type_specifiers & specifier) { \
1514 parse_error("multiple " name " type specifiers given"); \
1516 type_specifiers |= specifier; \
1520 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1521 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1522 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1523 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1524 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1525 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1526 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1527 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1528 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1529 #ifdef PROVIDE_COMPLEX
1530 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1531 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1535 specifiers->is_inline = true;
1540 if(type_specifiers & SPECIFIER_LONG_LONG) {
1541 parse_error("multiple type specifiers given");
1542 } else if(type_specifiers & SPECIFIER_LONG) {
1543 type_specifiers |= SPECIFIER_LONG_LONG;
1545 type_specifiers |= SPECIFIER_LONG;
1549 /* TODO: if type != NULL for the following rules should issue
1552 type = allocate_type_zero(sizeof(struct compound_type_t));
1554 compound_type_t *compound_type = (compound_type_t*) type;
1555 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1556 compound_type->declaration = parse_compound_type_specifier(true);
1560 type = allocate_type_zero(sizeof(compound_type_t));
1562 compound_type_t *compound_type = (compound_type_t*) type;
1563 compound_type->type.type = TYPE_COMPOUND_UNION;
1564 compound_type->declaration = parse_compound_type_specifier(false);
1568 type = parse_enum_specifier();
1571 type = parse_typeof();
1573 case T___builtin_va_list:
1574 type = create_builtin_type(token.v.symbol);
1578 case T___attribute__:
1583 case T_IDENTIFIER: {
1584 type_t *typedef_type = get_typedef_type(token.v.symbol);
1586 if(typedef_type == NULL)
1587 goto finish_specifiers;
1590 type = typedef_type;
1594 /* function specifier */
1596 goto finish_specifiers;
1603 atomic_type_type_t atomic_type;
1605 /* match valid basic types */
1606 switch(type_specifiers) {
1607 case SPECIFIER_VOID:
1608 atomic_type = ATOMIC_TYPE_VOID;
1610 case SPECIFIER_CHAR:
1611 atomic_type = ATOMIC_TYPE_CHAR;
1613 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1614 atomic_type = ATOMIC_TYPE_SCHAR;
1616 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1617 atomic_type = ATOMIC_TYPE_UCHAR;
1619 case SPECIFIER_SHORT:
1620 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1621 case SPECIFIER_SHORT | SPECIFIER_INT:
1622 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1623 atomic_type = ATOMIC_TYPE_SHORT;
1625 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1626 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1627 atomic_type = ATOMIC_TYPE_USHORT;
1630 case SPECIFIER_SIGNED:
1631 case SPECIFIER_SIGNED | SPECIFIER_INT:
1632 atomic_type = ATOMIC_TYPE_INT;
1634 case SPECIFIER_UNSIGNED:
1635 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1636 atomic_type = ATOMIC_TYPE_UINT;
1638 case SPECIFIER_LONG:
1639 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1640 case SPECIFIER_LONG | SPECIFIER_INT:
1641 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1642 atomic_type = ATOMIC_TYPE_LONG;
1644 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1645 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1646 atomic_type = ATOMIC_TYPE_ULONG;
1648 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1649 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1650 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1651 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1653 atomic_type = ATOMIC_TYPE_LONGLONG;
1655 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1656 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1658 atomic_type = ATOMIC_TYPE_ULONGLONG;
1660 case SPECIFIER_FLOAT:
1661 atomic_type = ATOMIC_TYPE_FLOAT;
1663 case SPECIFIER_DOUBLE:
1664 atomic_type = ATOMIC_TYPE_DOUBLE;
1666 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1667 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1669 case SPECIFIER_BOOL:
1670 atomic_type = ATOMIC_TYPE_BOOL;
1672 #ifdef PROVIDE_COMPLEX
1673 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1674 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1676 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1677 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1679 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1680 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1682 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1683 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1685 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1686 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1688 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1689 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1693 /* invalid specifier combination, give an error message */
1694 if(type_specifiers == 0) {
1696 parse_warning("no type specifiers in declaration (using int)");
1697 atomic_type = ATOMIC_TYPE_INT;
1700 parse_error("no type specifiers given in declaration");
1702 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1703 (type_specifiers & SPECIFIER_UNSIGNED)) {
1704 parse_error("signed and unsigned specifiers gives");
1705 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1706 parse_error("only integer types can be signed or unsigned");
1708 parse_error("multiple datatypes in declaration");
1710 atomic_type = ATOMIC_TYPE_INVALID;
1713 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1714 atype->type.type = TYPE_ATOMIC;
1715 atype->atype = atomic_type;
1718 type = (type_t*) atype;
1720 if(type_specifiers != 0) {
1721 parse_error("multiple datatypes in declaration");
1725 type->qualifiers = (type_qualifier_t)type_qualifiers;
1727 type_t *result = typehash_insert(type);
1728 if(newtype && result != (type_t*) type) {
1732 specifiers->type = result;
1735 static type_qualifiers_t parse_type_qualifiers(void)
1737 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1740 switch(token.type) {
1741 /* type qualifiers */
1742 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1743 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1744 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1747 return type_qualifiers;
1752 static void parse_identifier_list(void)
1755 if(token.type != T_IDENTIFIER) {
1756 parse_error_expected("while parsing parameter identifier list",
1761 if(token.type != ',')
1767 static declaration_t *parse_parameter(void)
1769 declaration_specifiers_t specifiers;
1770 memset(&specifiers, 0, sizeof(specifiers));
1772 parse_declaration_specifiers(&specifiers);
1774 declaration_t *declaration
1775 = parse_declarator(&specifiers, specifiers.type, true);
1777 /* TODO check declaration constraints for parameters */
1778 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1779 parse_error("typedef not allowed in parameter list");
1782 /* Array as last part of a paramter type is just syntactic sugar. Turn it
1784 if (declaration->type->type == TYPE_ARRAY) {
1785 const array_type_t *const arr_type =
1786 (const array_type_t*)declaration->type;
1787 type_t *element_type = arr_type->element_type;
1788 declaration->type = make_pointer_type(element_type, TYPE_QUALIFIER_NONE);
1794 static declaration_t *parse_parameters(function_type_t *type)
1796 if(token.type == T_IDENTIFIER) {
1797 symbol_t *symbol = token.v.symbol;
1798 if(!is_typedef_symbol(symbol)) {
1799 /* TODO: K&R style C parameters */
1800 parse_identifier_list();
1805 if(token.type == ')') {
1806 type->unspecified_parameters = 1;
1809 if(token.type == T_void && look_ahead(1)->type == ')') {
1814 declaration_t *declarations = NULL;
1815 declaration_t *declaration;
1816 declaration_t *last_declaration = NULL;
1817 function_parameter_t *parameter;
1818 function_parameter_t *last_parameter = NULL;
1821 switch(token.type) {
1825 return declarations;
1828 case T___extension__:
1830 declaration = parse_parameter();
1832 parameter = allocate_type_zero(sizeof(parameter[0]));
1833 parameter->type = declaration->type;
1835 if(last_parameter != NULL) {
1836 last_declaration->next = declaration;
1837 last_parameter->next = parameter;
1839 type->parameters = parameter;
1840 declarations = declaration;
1842 last_parameter = parameter;
1843 last_declaration = declaration;
1847 return declarations;
1849 if(token.type != ',')
1850 return declarations;
1860 } construct_type_type_t;
1862 typedef struct construct_type_t construct_type_t;
1863 struct construct_type_t {
1864 construct_type_type_t type;
1865 construct_type_t *next;
1868 typedef struct parsed_pointer_t parsed_pointer_t;
1869 struct parsed_pointer_t {
1870 construct_type_t construct_type;
1871 type_qualifiers_t type_qualifiers;
1874 typedef struct construct_function_type_t construct_function_type_t;
1875 struct construct_function_type_t {
1876 construct_type_t construct_type;
1877 function_type_t *function_type;
1880 typedef struct parsed_array_t parsed_array_t;
1881 struct parsed_array_t {
1882 construct_type_t construct_type;
1883 type_qualifiers_t type_qualifiers;
1889 typedef struct construct_base_type_t construct_base_type_t;
1890 struct construct_base_type_t {
1891 construct_type_t construct_type;
1895 static construct_type_t *parse_pointer_declarator(void)
1899 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1900 memset(pointer, 0, sizeof(pointer[0]));
1901 pointer->construct_type.type = CONSTRUCT_POINTER;
1902 pointer->type_qualifiers = parse_type_qualifiers();
1904 return (construct_type_t*) pointer;
1907 static construct_type_t *parse_array_declarator(void)
1911 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1912 memset(array, 0, sizeof(array[0]));
1913 array->construct_type.type = CONSTRUCT_ARRAY;
1915 if(token.type == T_static) {
1916 array->is_static = true;
1920 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1921 if(type_qualifiers != 0) {
1922 if(token.type == T_static) {
1923 array->is_static = true;
1927 array->type_qualifiers = type_qualifiers;
1929 if(token.type == '*' && look_ahead(1)->type == ']') {
1930 array->is_variable = true;
1932 } else if(token.type != ']') {
1933 array->size = parse_assignment_expression();
1938 return (construct_type_t*) array;
1941 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1945 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1946 type->type.type = TYPE_FUNCTION;
1948 declaration_t *parameters = parse_parameters(type);
1949 if(declaration != NULL) {
1950 declaration->context.declarations = parameters;
1953 construct_function_type_t *construct_function_type =
1954 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1955 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1956 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1957 construct_function_type->function_type = type;
1961 return (construct_type_t*) construct_function_type;
1964 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1965 bool may_be_abstract)
1967 /* construct a single linked list of construct_type_t's which describe
1968 * how to construct the final declarator type */
1969 construct_type_t *first = NULL;
1970 construct_type_t *last = NULL;
1973 while(token.type == '*') {
1974 construct_type_t *type = parse_pointer_declarator();
1985 /* TODO: find out if this is correct */
1988 construct_type_t *inner_types = NULL;
1990 switch(token.type) {
1992 if(declaration == NULL) {
1993 parse_error("no identifier expected in typename");
1995 declaration->symbol = token.v.symbol;
1996 declaration->source_position = token.source_position;
2002 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2008 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2009 /* avoid a loop in the outermost scope, because eat_statement doesn't
2011 if(token.type == '}' && current_function == NULL) {
2019 construct_type_t *p = last;
2022 construct_type_t *type;
2023 switch(token.type) {
2025 type = parse_function_declarator(declaration);
2028 type = parse_array_declarator();
2031 goto declarator_finished;
2034 /* insert in the middle of the list (behind p) */
2036 type->next = p->next;
2047 declarator_finished:
2050 /* append inner_types at the end of the list, we don't to set last anymore
2051 * as it's not needed anymore */
2053 assert(first == NULL);
2054 first = inner_types;
2056 last->next = inner_types;
2062 static type_t *construct_declarator_type(construct_type_t *construct_list,
2065 construct_type_t *iter = construct_list;
2066 for( ; iter != NULL; iter = iter->next) {
2067 parsed_pointer_t *parsed_pointer;
2068 parsed_array_t *parsed_array;
2069 construct_function_type_t *construct_function_type;
2070 function_type_t *function_type;
2071 pointer_type_t *pointer_type;
2072 array_type_t *array_type;
2074 switch(iter->type) {
2075 case CONSTRUCT_INVALID:
2076 panic("invalid type construction found");
2077 case CONSTRUCT_FUNCTION:
2078 construct_function_type = (construct_function_type_t*) iter;
2079 function_type = construct_function_type->function_type;
2081 function_type->result_type = type;
2082 type = (type_t*) function_type;
2085 case CONSTRUCT_POINTER:
2086 parsed_pointer = (parsed_pointer_t*) iter;
2087 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
2089 pointer_type->type.type = TYPE_POINTER;
2090 pointer_type->points_to = type;
2091 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
2092 type = (type_t*) pointer_type;
2095 case CONSTRUCT_ARRAY:
2096 parsed_array = (parsed_array_t*) iter;
2097 array_type = allocate_type_zero(sizeof(array_type[0]));
2099 array_type->type.type = TYPE_ARRAY;
2100 array_type->element_type = type;
2101 array_type->type.qualifiers = parsed_array->type_qualifiers;
2102 array_type->is_static = parsed_array->is_static;
2103 array_type->is_variable = parsed_array->is_variable;
2104 array_type->size = parsed_array->size;
2105 type = (type_t*) array_type;
2109 type_t *hashed_type = typehash_insert((type_t*) type);
2110 if(hashed_type != type) {
2111 /* the function type was constructed earlier freeing it here will
2112 * destroy other types... */
2113 if(iter->type != CONSTRUCT_FUNCTION) {
2123 static declaration_t *parse_declarator(
2124 const declaration_specifiers_t *specifiers,
2125 type_t *type, bool may_be_abstract)
2127 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2128 declaration->storage_class = specifiers->storage_class;
2129 declaration->is_inline = specifiers->is_inline;
2131 construct_type_t *construct_type
2132 = parse_inner_declarator(declaration, may_be_abstract);
2133 declaration->type = construct_declarator_type(construct_type, type);
2135 if(construct_type != NULL) {
2136 obstack_free(&temp_obst, construct_type);
2142 static type_t *parse_abstract_declarator(type_t *base_type)
2144 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2146 type_t *result = construct_declarator_type(construct_type, base_type);
2147 if(construct_type != NULL) {
2148 obstack_free(&temp_obst, construct_type);
2154 static declaration_t *record_declaration(declaration_t *declaration)
2156 assert(context != NULL);
2158 symbol_t *symbol = declaration->symbol;
2159 if(symbol != NULL) {
2160 declaration_t *alias = environment_push(declaration);
2161 if(alias != declaration)
2164 declaration->parent_context = context;
2167 if(last_declaration != NULL) {
2168 last_declaration->next = declaration;
2170 context->declarations = declaration;
2172 last_declaration = declaration;
2177 static void parser_error_multiple_definition(declaration_t *previous,
2178 declaration_t *declaration)
2180 parser_print_error_prefix_pos(declaration->source_position);
2181 fprintf(stderr, "multiple definition of symbol '%s'\n",
2182 declaration->symbol->string);
2183 parser_print_error_prefix_pos(previous->source_position);
2184 fprintf(stderr, "this is the location of the previous definition.\n");
2187 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
2190 declaration_t *ndeclaration
2191 = parse_declarator(specifiers, specifiers->type, false);
2193 declaration_t *declaration = record_declaration(ndeclaration);
2195 type_t *orig_type = declaration->type;
2196 type_t *type = skip_typeref(orig_type);
2197 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
2198 parser_print_warning_prefix_pos(declaration->source_position);
2199 fprintf(stderr, "variable '%s' declared 'inline'\n",
2200 declaration->symbol->string);
2203 if(token.type == '=') {
2206 /* TODO: check that this is an allowed type (no function type) */
2208 if(declaration->init.initializer != NULL) {
2209 parser_error_multiple_definition(declaration, ndeclaration);
2212 initializer_t *initializer = parse_initializer(type);
2214 if(type->type == TYPE_ARRAY && initializer != NULL) {
2215 array_type_t *array_type = (array_type_t*) type;
2217 if(array_type->size == NULL) {
2218 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2220 cnst->expression.type = EXPR_CONST;
2221 cnst->expression.datatype = type_size_t;
2223 if(initializer->type == INITIALIZER_LIST) {
2224 initializer_list_t *list = &initializer->list;
2225 cnst->v.int_value = list->len;
2227 assert(initializer->type == INITIALIZER_STRING);
2228 initializer_string_t *string = &initializer->string;
2229 cnst->v.int_value = strlen(string->string) + 1;
2232 array_type->size = (expression_t*) cnst;
2237 ndeclaration->init.initializer = initializer;
2238 } else if(token.type == '{') {
2239 if(type->type != TYPE_FUNCTION) {
2240 parser_print_error_prefix();
2241 fprintf(stderr, "declarator '");
2242 print_type_ext(orig_type, declaration->symbol, NULL);
2243 fprintf(stderr, "' has a body but is not a function type.\n");
2248 if(declaration->init.statement != NULL) {
2249 parser_error_multiple_definition(declaration, ndeclaration);
2251 if(ndeclaration != declaration) {
2252 memcpy(&declaration->context, &ndeclaration->context,
2253 sizeof(declaration->context));
2256 int top = environment_top();
2257 context_t *last_context = context;
2258 set_context(&declaration->context);
2260 /* push function parameters */
2261 declaration_t *parameter = declaration->context.declarations;
2262 for( ; parameter != NULL; parameter = parameter->next) {
2263 environment_push(parameter);
2266 int label_stack_top = label_top();
2267 declaration_t *old_current_function = current_function;
2268 current_function = declaration;
2270 statement_t *statement = parse_compound_statement();
2272 assert(current_function == declaration);
2273 current_function = old_current_function;
2274 label_pop_to(label_stack_top);
2276 assert(context == &declaration->context);
2277 set_context(last_context);
2278 environment_pop_to(top);
2280 declaration->init.statement = statement;
2284 if(token.type != ',')
2291 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2294 if(token.type == ':') {
2296 parse_constant_expression();
2297 /* TODO (bitfields) */
2299 declaration_t *declaration
2300 = parse_declarator(specifiers, specifiers->type, true);
2302 /* TODO: check constraints for struct declarations */
2303 /* TODO: check for doubled fields */
2304 record_declaration(declaration);
2306 if(token.type == ':') {
2308 parse_constant_expression();
2309 /* TODO (bitfields) */
2313 if(token.type != ',')
2320 static void parse_compound_type_entries(void)
2324 while(token.type != '}' && token.type != T_EOF) {
2325 declaration_specifiers_t specifiers;
2326 memset(&specifiers, 0, sizeof(specifiers));
2327 parse_declaration_specifiers(&specifiers);
2329 parse_struct_declarators(&specifiers);
2331 if(token.type == T_EOF) {
2332 parse_error("unexpected error while parsing struct");
2337 static void parse_declaration(void)
2339 source_position_t source_position = token.source_position;
2341 declaration_specifiers_t specifiers;
2342 memset(&specifiers, 0, sizeof(specifiers));
2343 parse_declaration_specifiers(&specifiers);
2345 if(token.type == ';') {
2346 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2347 parse_warning_pos(source_position,
2348 "useless keyword in empty declaration");
2350 switch (specifiers.type->type) {
2351 case TYPE_COMPOUND_STRUCT:
2352 case TYPE_COMPOUND_UNION: {
2353 const compound_type_t *const comp_type =
2354 (const compound_type_t*)specifiers.type;
2355 if (comp_type->declaration->symbol == NULL) {
2356 parse_warning_pos(source_position,
2357 "unnamed struct/union that defines no instances");
2362 case TYPE_ENUM: break;
2365 parse_warning_pos(source_position, "empty declaration");
2371 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2373 declaration->type = specifiers.type;
2374 declaration->storage_class = specifiers.storage_class;
2375 declaration->source_position = source_position;
2376 record_declaration(declaration);
2379 parse_init_declarators(&specifiers);
2382 static type_t *parse_typename(void)
2384 declaration_specifiers_t specifiers;
2385 memset(&specifiers, 0, sizeof(specifiers));
2386 parse_declaration_specifiers(&specifiers);
2387 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2388 /* TODO: improve error message, user does probably not know what a
2389 * storage class is...
2391 parse_error("typename may not have a storage class");
2394 type_t *result = parse_abstract_declarator(specifiers.type);
2402 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2403 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2404 expression_t *left);
2406 typedef struct expression_parser_function_t expression_parser_function_t;
2407 struct expression_parser_function_t {
2408 unsigned precedence;
2409 parse_expression_function parser;
2410 unsigned infix_precedence;
2411 parse_expression_infix_function infix_parser;
2414 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2416 static expression_t *make_invalid_expression(void)
2418 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2419 expression->type = EXPR_INVALID;
2420 expression->source_position = token.source_position;
2424 static expression_t *expected_expression_error(void)
2426 parser_print_error_prefix();
2427 fprintf(stderr, "expected expression, got token ");
2428 print_token(stderr, & token);
2429 fprintf(stderr, "\n");
2433 return make_invalid_expression();
2436 static expression_t *parse_string_const(void)
2438 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2440 cnst->expression.type = EXPR_STRING_LITERAL;
2441 cnst->expression.datatype = type_string;
2442 cnst->value = parse_string_literals();
2444 return (expression_t*) cnst;
2447 static expression_t *parse_int_const(void)
2449 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2451 cnst->expression.type = EXPR_CONST;
2452 cnst->expression.datatype = token.datatype;
2453 cnst->v.int_value = token.v.intvalue;
2457 return (expression_t*) cnst;
2460 static expression_t *parse_float_const(void)
2462 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2464 cnst->expression.type = EXPR_CONST;
2465 cnst->expression.datatype = token.datatype;
2466 cnst->v.float_value = token.v.floatvalue;
2470 return (expression_t*) cnst;
2473 static declaration_t *create_implicit_function(symbol_t *symbol,
2474 const source_position_t source_position)
2476 function_type_t *function_type
2477 = allocate_type_zero(sizeof(function_type[0]));
2479 function_type->type.type = TYPE_FUNCTION;
2480 function_type->result_type = type_int;
2481 function_type->unspecified_parameters = true;
2483 type_t *type = typehash_insert((type_t*) function_type);
2484 if(type != (type_t*) function_type) {
2485 free_type(function_type);
2488 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2490 declaration->storage_class = STORAGE_CLASS_EXTERN;
2491 declaration->type = type;
2492 declaration->symbol = symbol;
2493 declaration->source_position = source_position;
2495 /* prepend the implicit definition to the global context
2496 * this is safe since the symbol wasn't declared as anything else yet
2498 assert(symbol->declaration == NULL);
2500 context_t *last_context = context;
2501 context = global_context;
2503 environment_push(declaration);
2504 declaration->next = context->declarations;
2505 context->declarations = declaration;
2507 context = last_context;
2512 static type_t *make_function_1_type(type_t *result_type, type_t *argument_type)
2514 function_parameter_t *parameter = allocate_type_zero(sizeof(parameter[0]));
2515 parameter->type = argument_type;
2517 function_type_t *type = allocate_type_zero(sizeof(type[0]));
2518 type->type.type = TYPE_FUNCTION;
2519 type->result_type = result_type;
2520 type->parameters = parameter;
2522 type_t *result = typehash_insert((type_t*) type);
2523 if(result != (type_t*) type) {
2530 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2532 switch(symbol->ID) {
2533 case T___builtin_alloca:
2534 return make_function_1_type(type_void_ptr, type_size_t);
2536 panic("not implemented builtin symbol found");
2541 * performs automatic type cast as described in § 6.3.2.1
2543 static type_t *automatic_type_conversion(type_t *type)
2548 if(type->type == TYPE_ARRAY) {
2549 array_type_t *array_type = (array_type_t*) type;
2550 type_t *element_type = array_type->element_type;
2551 unsigned qualifiers = array_type->type.qualifiers;
2553 return make_pointer_type(element_type, qualifiers);
2556 if(type->type == TYPE_FUNCTION) {
2557 return make_pointer_type(type, TYPE_QUALIFIER_NONE);
2564 * reverts the automatic casts of array to pointer types and function
2565 * to function-pointer types as defined § 6.3.2.1
2567 type_t *revert_automatic_type_conversion(const expression_t *expression)
2569 if(expression->datatype == NULL)
2572 switch(expression->type) {
2573 case EXPR_REFERENCE: {
2574 const reference_expression_t *ref
2575 = (const reference_expression_t*) expression;
2576 return ref->declaration->type;
2579 const select_expression_t *select
2580 = (const select_expression_t*) expression;
2581 return select->compound_entry->type;
2584 const unary_expression_t *unary
2585 = (const unary_expression_t*) expression;
2586 if(unary->type == UNEXPR_DEREFERENCE) {
2587 expression_t *value = unary->value;
2588 type_t *type = skip_typeref(value->datatype);
2589 pointer_type_t *pointer_type = (pointer_type_t*) type;
2591 return pointer_type->points_to;
2595 case EXPR_BUILTIN_SYMBOL: {
2596 const builtin_symbol_expression_t *builtin
2597 = (const builtin_symbol_expression_t*) expression;
2598 return get_builtin_symbol_type(builtin->symbol);
2600 case EXPR_ARRAY_ACCESS: {
2601 const array_access_expression_t *array_access
2602 = (const array_access_expression_t*) expression;
2603 type_t *type_left = skip_typeref(array_access->array_ref->datatype);
2604 assert(is_type_pointer(type_left));
2605 pointer_type_t *pointer_type = (pointer_type_t*) type_left;
2606 return pointer_type->points_to;
2613 return expression->datatype;
2616 static expression_t *parse_reference(void)
2618 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2620 ref->expression.type = EXPR_REFERENCE;
2621 ref->symbol = token.v.symbol;
2623 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2625 source_position_t source_position = token.source_position;
2628 if(declaration == NULL) {
2630 /* an implicitly defined function */
2631 if(token.type == '(') {
2632 parser_print_prefix_pos(token.source_position);
2633 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2634 ref->symbol->string);
2636 declaration = create_implicit_function(ref->symbol,
2641 parser_print_error_prefix();
2642 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2643 return (expression_t*) ref;
2647 type_t *type = declaration->type;
2648 /* we always do the auto-type conversions; the & and sizeof parser contains
2649 * code to revert this! */
2650 type = automatic_type_conversion(type);
2652 ref->declaration = declaration;
2653 ref->expression.datatype = type;
2655 return (expression_t*) ref;
2658 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2662 /* TODO check if explicit cast is allowed and issue warnings/errors */
2665 static expression_t *parse_cast(void)
2667 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2669 cast->expression.type = EXPR_UNARY;
2670 cast->type = UNEXPR_CAST;
2671 cast->expression.source_position = token.source_position;
2673 type_t *type = parse_typename();
2676 expression_t *value = parse_sub_expression(20);
2678 check_cast_allowed(value, type);
2680 cast->expression.datatype = type;
2681 cast->value = value;
2683 return (expression_t*) cast;
2686 static expression_t *parse_statement_expression(void)
2688 statement_expression_t *expression
2689 = allocate_ast_zero(sizeof(expression[0]));
2690 expression->expression.type = EXPR_STATEMENT;
2692 statement_t *statement = parse_compound_statement();
2693 expression->statement = statement;
2694 if(statement == NULL) {
2699 assert(statement->type == STATEMENT_COMPOUND);
2700 compound_statement_t *compound_statement
2701 = (compound_statement_t*) statement;
2703 /* find last statement and use it's type */
2704 const statement_t *last_statement = NULL;
2705 const statement_t *iter = compound_statement->statements;
2706 for( ; iter != NULL; iter = iter->next) {
2707 last_statement = iter;
2710 if(last_statement->type == STATEMENT_EXPRESSION) {
2711 const expression_statement_t *expression_statement =
2712 (const expression_statement_t*) last_statement;
2713 expression->expression.datatype
2714 = expression_statement->expression->datatype;
2716 expression->expression.datatype = type_void;
2721 return (expression_t*) expression;
2724 static expression_t *parse_brace_expression(void)
2728 switch(token.type) {
2730 /* gcc extension: a stement expression */
2731 return parse_statement_expression();
2735 return parse_cast();
2737 if(is_typedef_symbol(token.v.symbol)) {
2738 return parse_cast();
2742 expression_t *result = parse_expression();
2748 static expression_t *parse_function_keyword(void)
2753 if (current_function == NULL) {
2754 parse_error("'__func__' used outside of a function");
2757 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2758 expression->expression.type = EXPR_FUNCTION;
2759 expression->expression.datatype = type_string;
2760 expression->value = "TODO: FUNCTION";
2762 return (expression_t*) expression;
2765 static expression_t *parse_pretty_function_keyword(void)
2767 eat(T___PRETTY_FUNCTION__);
2770 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2771 expression->expression.type = EXPR_PRETTY_FUNCTION;
2772 expression->expression.datatype = type_string;
2773 expression->value = "TODO: PRETTY FUNCTION";
2775 return (expression_t*) expression;
2778 static designator_t *parse_designator(void)
2780 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2782 if(token.type != T_IDENTIFIER) {
2783 parse_error_expected("while parsing member designator",
2788 result->symbol = token.v.symbol;
2791 designator_t *last_designator = result;
2793 if(token.type == '.') {
2795 if(token.type != T_IDENTIFIER) {
2796 parse_error_expected("while parsing member designator",
2801 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2802 designator->symbol = token.v.symbol;
2805 last_designator->next = designator;
2806 last_designator = designator;
2809 if(token.type == '[') {
2811 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2812 designator->array_access = parse_expression();
2813 if(designator->array_access == NULL) {
2819 last_designator->next = designator;
2820 last_designator = designator;
2829 static expression_t *parse_offsetof(void)
2831 eat(T___builtin_offsetof);
2833 offsetof_expression_t *expression
2834 = allocate_ast_zero(sizeof(expression[0]));
2835 expression->expression.type = EXPR_OFFSETOF;
2836 expression->expression.datatype = type_size_t;
2839 expression->type = parse_typename();
2841 expression->designator = parse_designator();
2844 return (expression_t*) expression;
2847 static expression_t *parse_va_arg(void)
2849 eat(T___builtin_va_arg);
2851 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2852 expression->expression.type = EXPR_VA_ARG;
2855 expression->arg = parse_assignment_expression();
2857 expression->expression.datatype = parse_typename();
2860 return (expression_t*) expression;
2863 static expression_t *parse_builtin_symbol(void)
2865 builtin_symbol_expression_t *expression
2866 = allocate_ast_zero(sizeof(expression[0]));
2867 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2869 expression->symbol = token.v.symbol;
2872 type_t *type = get_builtin_symbol_type(expression->symbol);
2873 type = automatic_type_conversion(type);
2875 expression->expression.datatype = type;
2876 return (expression_t*) expression;
2879 static expression_t *parse_primary_expression(void)
2881 switch(token.type) {
2883 return parse_int_const();
2884 case T_FLOATINGPOINT:
2885 return parse_float_const();
2886 case T_STRING_LITERAL:
2887 return parse_string_const();
2889 return parse_reference();
2890 case T___FUNCTION__:
2892 return parse_function_keyword();
2893 case T___PRETTY_FUNCTION__:
2894 return parse_pretty_function_keyword();
2895 case T___builtin_offsetof:
2896 return parse_offsetof();
2897 case T___builtin_va_arg:
2898 return parse_va_arg();
2899 case T___builtin_alloca:
2900 case T___builtin_expect:
2901 case T___builtin_va_start:
2902 case T___builtin_va_end:
2903 return parse_builtin_symbol();
2906 return parse_brace_expression();
2909 parser_print_error_prefix();
2910 fprintf(stderr, "unexpected token ");
2911 print_token(stderr, &token);
2912 fprintf(stderr, "\n");
2915 return make_invalid_expression();
2918 static expression_t *parse_array_expression(unsigned precedence,
2925 expression_t *inside = parse_expression();
2927 array_access_expression_t *array_access
2928 = allocate_ast_zero(sizeof(array_access[0]));
2930 array_access->expression.type = EXPR_ARRAY_ACCESS;
2932 type_t *type_left = skip_typeref(left->datatype);
2933 type_t *type_inside = skip_typeref(inside->datatype);
2934 type_t *result_type;
2936 if(type_left != NULL && type_inside != NULL) {
2937 if(is_type_pointer(type_left)) {
2938 pointer_type_t *pointer = (pointer_type_t*) type_left;
2939 result_type = pointer->points_to;
2940 array_access->array_ref = left;
2941 array_access->index = inside;
2942 } else if(is_type_pointer(type_inside)) {
2943 pointer_type_t *pointer = (pointer_type_t*) type_inside;
2944 result_type = pointer->points_to;
2945 array_access->array_ref = inside;
2946 array_access->index = left;
2947 array_access->flipped = true;
2949 parser_print_error_prefix();
2950 fprintf(stderr, "array access on object with non-pointer types ");
2951 print_type_quoted(type_left);
2952 fprintf(stderr, ", ");
2953 print_type_quoted(type_inside);
2954 fprintf(stderr, "\n");
2957 array_access->array_ref = left;
2958 array_access->index = inside;
2961 if(token.type != ']') {
2962 parse_error_expected("Problem while parsing array access", ']', 0);
2963 return (expression_t*) array_access;
2967 result_type = automatic_type_conversion(result_type);
2968 array_access->expression.datatype = result_type;
2970 return (expression_t*) array_access;
2973 static bool is_declaration_specifier(const token_t *token,
2974 bool only_type_specifiers)
2976 switch(token->type) {
2980 return is_typedef_symbol(token->v.symbol);
2983 if(only_type_specifiers)
2992 static expression_t *parse_sizeof(unsigned precedence)
2996 sizeof_expression_t *sizeof_expression
2997 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2998 sizeof_expression->expression.type = EXPR_SIZEOF;
2999 sizeof_expression->expression.datatype = type_size_t;
3001 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3003 sizeof_expression->type = parse_typename();
3006 expression_t *expression = parse_sub_expression(precedence);
3007 expression->datatype = revert_automatic_type_conversion(expression);
3009 sizeof_expression->type = expression->datatype;
3010 sizeof_expression->size_expression = expression;
3013 return (expression_t*) sizeof_expression;
3016 static expression_t *parse_select_expression(unsigned precedence,
3017 expression_t *compound)
3020 assert(token.type == '.' || token.type == T_MINUSGREATER);
3022 bool is_pointer = (token.type == T_MINUSGREATER);
3025 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
3027 select->expression.type = EXPR_SELECT;
3028 select->compound = compound;
3030 if(token.type != T_IDENTIFIER) {
3031 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3032 return (expression_t*) select;
3034 symbol_t *symbol = token.v.symbol;
3035 select->symbol = symbol;
3038 type_t *orig_type = compound->datatype;
3039 if(orig_type == NULL)
3040 return make_invalid_expression();
3042 type_t *type = skip_typeref(orig_type);
3044 type_t *type_left = type;
3046 if(type->type != TYPE_POINTER) {
3047 parser_print_error_prefix();
3048 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
3049 print_type_quoted(orig_type);
3050 fputc('\n', stderr);
3051 return make_invalid_expression();
3053 pointer_type_t *pointer_type = (pointer_type_t*) type;
3054 type_left = pointer_type->points_to;
3056 type_left = skip_typeref(type_left);
3058 if(type_left->type != TYPE_COMPOUND_STRUCT
3059 && type_left->type != TYPE_COMPOUND_UNION) {
3060 parser_print_error_prefix();
3061 fprintf(stderr, "request for member '%s' in something not a struct or "
3062 "union, but ", symbol->string);
3063 print_type_quoted(type_left);
3064 fputc('\n', stderr);
3065 return make_invalid_expression();
3068 compound_type_t *compound_type = (compound_type_t*) type_left;
3069 declaration_t *declaration = compound_type->declaration;
3071 if(!declaration->init.is_defined) {
3072 parser_print_error_prefix();
3073 fprintf(stderr, "request for member '%s' of incomplete type ",
3075 print_type_quoted(type_left);
3076 fputc('\n', stderr);
3077 return make_invalid_expression();
3080 declaration_t *iter = declaration->context.declarations;
3081 for( ; iter != NULL; iter = iter->next) {
3082 if(iter->symbol == symbol) {
3087 parser_print_error_prefix();
3088 print_type_quoted(type_left);
3089 fprintf(stderr, " has no member named '%s'\n", symbol->string);
3090 return make_invalid_expression();
3093 /* we always do the auto-type conversions; the & and sizeof parser contains
3094 * code to revert this! */
3095 type_t *expression_type = automatic_type_conversion(iter->type);
3097 select->compound_entry = iter;
3098 select->expression.datatype = expression_type;
3099 return (expression_t*) select;
3102 static expression_t *parse_call_expression(unsigned precedence,
3103 expression_t *expression)
3106 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
3107 call->expression.type = EXPR_CALL;
3108 call->function = expression;
3110 function_type_t *function_type;
3111 type_t *orig_type = expression->datatype;
3112 if(orig_type != NULL) {
3113 function_type = NULL;
3114 type_t *type = skip_typeref(orig_type);
3116 if(is_type_pointer(type)) {
3117 pointer_type_t *pointer_type = (pointer_type_t*) type;
3119 type = skip_typeref(pointer_type->points_to);
3121 if (type->type == TYPE_FUNCTION) {
3122 function_type = (function_type_t*) type;
3123 call->expression.datatype = function_type->result_type;
3126 if(function_type == NULL) {
3127 parser_print_error_prefix();
3128 fputs("called object '", stderr);
3129 print_expression(expression);
3130 fputs("' (type ", stderr);
3131 print_type_quoted(orig_type);
3132 fputs(") is not a pointer to a function\n", stderr);
3134 function_type = NULL;
3135 call->expression.datatype = NULL;
3139 /* parse arguments */
3142 if(token.type != ')') {
3143 call_argument_t *last_argument = NULL;
3146 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3148 argument->expression = parse_assignment_expression();
3149 if(last_argument == NULL) {
3150 call->arguments = argument;
3152 last_argument->next = argument;
3154 last_argument = argument;
3156 if(token.type != ',')
3163 if(function_type != NULL) {
3164 function_parameter_t *parameter = function_type->parameters;
3165 call_argument_t *argument = call->arguments;
3166 for( ; parameter != NULL && argument != NULL;
3167 parameter = parameter->next, argument = argument->next) {
3168 type_t *expected_type = parameter->type;
3169 /* TODO report context in error messages */
3170 argument->expression = create_implicit_cast(argument->expression,
3173 /* too few parameters */
3174 if(parameter != NULL) {
3175 parser_print_error_prefix();
3176 fprintf(stderr, "too few arguments to function '");
3177 print_expression(expression);
3178 fprintf(stderr, "'\n");
3179 } else if(argument != NULL) {
3180 /* too many parameters */
3181 if(!function_type->variadic
3182 && !function_type->unspecified_parameters) {
3183 parser_print_error_prefix();
3184 fprintf(stderr, "too many arguments to function '");
3185 print_expression(expression);
3186 fprintf(stderr, "'\n");
3188 /* do default promotion */
3189 for( ; argument != NULL; argument = argument->next) {
3190 type_t *type = argument->expression->datatype;
3195 if(is_type_integer(type)) {
3196 type = promote_integer(type);
3197 } else if(type == type_float) {
3200 argument->expression
3201 = create_implicit_cast(argument->expression, type);
3207 return (expression_t*) call;
3210 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3212 static expression_t *parse_conditional_expression(unsigned precedence,
3213 expression_t *expression)
3217 conditional_expression_t *conditional
3218 = allocate_ast_zero(sizeof(conditional[0]));
3219 conditional->expression.type = EXPR_CONDITIONAL;
3220 conditional->condition = expression;
3223 type_t *condition_type_orig = conditional->condition->datatype;
3224 if(condition_type_orig != NULL) {
3225 type_t *condition_type = skip_typeref(condition_type_orig);
3226 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3227 type_error("expected a scalar type", expression->source_position,
3228 condition_type_orig);
3232 expression_t *const t_expr = parse_expression();
3233 conditional->true_expression = t_expr;
3235 expression_t *const f_expr = parse_sub_expression(precedence);
3236 conditional->false_expression = f_expr;
3238 type_t *const true_type = t_expr->datatype;
3239 if(true_type == NULL)
3240 return (expression_t*) conditional;
3241 type_t *const false_type = f_expr->datatype;
3242 if(false_type == NULL)
3243 return (expression_t*) conditional;
3245 type_t *const skipped_true_type = skip_typeref(true_type);
3246 type_t *const skipped_false_type = skip_typeref(false_type);
3249 if (skipped_true_type == skipped_false_type) {
3250 conditional->expression.datatype = skipped_true_type;
3251 } else if (is_type_arithmetic(skipped_true_type) &&
3252 is_type_arithmetic(skipped_false_type)) {
3253 type_t *const result = semantic_arithmetic(skipped_true_type,
3254 skipped_false_type);
3255 conditional->true_expression = create_implicit_cast(t_expr, result);
3256 conditional->false_expression = create_implicit_cast(f_expr, result);
3257 conditional->expression.datatype = result;
3258 } else if (skipped_true_type->type == TYPE_POINTER &&
3259 skipped_false_type->type == TYPE_POINTER &&
3260 true /* TODO compatible points_to types */) {
3262 } else if(/* (is_null_ptr_const(skipped_true_type) &&
3263 skipped_false_type->type == TYPE_POINTER)
3264 || (is_null_ptr_const(skipped_false_type) &&
3265 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
3267 } else if(/* 1 is pointer to object type, other is void* */ false) {
3270 type_error_incompatible("while parsing conditional",
3271 expression->source_position, true_type,
3272 skipped_false_type);
3275 return (expression_t*) conditional;
3278 static expression_t *parse_extension(unsigned precedence)
3280 eat(T___extension__);
3282 /* TODO enable extensions */
3284 return parse_sub_expression(precedence);
3287 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3289 eat(T___builtin_classify_type);
3291 classify_type_expression_t *const classify_type_expr =
3292 allocate_ast_zero(sizeof(classify_type_expr[0]));
3293 classify_type_expr->expression.type = EXPR_CLASSIFY_TYPE;
3294 classify_type_expr->expression.datatype = type_int;
3297 expression_t *const expression = parse_sub_expression(precedence);
3299 classify_type_expr->type_expression = expression;
3301 return (expression_t*)classify_type_expr;
3304 static void semantic_incdec(unary_expression_t *expression)
3306 type_t *orig_type = expression->value->datatype;
3307 if(orig_type == NULL)
3310 type_t *type = skip_typeref(orig_type);
3311 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
3312 /* TODO: improve error message */
3313 parser_print_error_prefix();
3314 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
3318 expression->expression.datatype = orig_type;
3321 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3323 type_t *orig_type = expression->value->datatype;
3324 if(orig_type == NULL)
3327 type_t *type = skip_typeref(orig_type);
3328 if(!is_type_arithmetic(type)) {
3329 /* TODO: improve error message */
3330 parser_print_error_prefix();
3331 fprintf(stderr, "operation needs an arithmetic type\n");
3335 expression->expression.datatype = orig_type;
3338 static void semantic_unexpr_scalar(unary_expression_t *expression)
3340 type_t *orig_type = expression->value->datatype;
3341 if(orig_type == NULL)
3344 type_t *type = skip_typeref(orig_type);
3345 if (!is_type_scalar(type)) {
3346 parse_error("operand of ! must be of scalar type\n");
3350 expression->expression.datatype = orig_type;
3353 static void semantic_unexpr_integer(unary_expression_t *expression)
3355 type_t *orig_type = expression->value->datatype;
3356 if(orig_type == NULL)
3359 type_t *type = skip_typeref(orig_type);
3360 if (!is_type_integer(type)) {
3361 parse_error("operand of ~ must be of integer type\n");
3365 expression->expression.datatype = orig_type;
3368 static void semantic_dereference(unary_expression_t *expression)
3370 type_t *orig_type = expression->value->datatype;
3371 if(orig_type == NULL)
3374 type_t *type = skip_typeref(orig_type);
3375 if(!is_type_pointer(type)) {
3376 parser_print_error_prefix();
3377 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
3378 print_type_quoted(orig_type);
3379 fputs(" given.\n", stderr);
3383 pointer_type_t *pointer_type = (pointer_type_t*)type;
3384 type_t *result_type = pointer_type->points_to;
3386 result_type = automatic_type_conversion(result_type);
3387 expression->expression.datatype = result_type;
3390 static void semantic_take_addr(unary_expression_t *expression)
3392 expression_t *value = expression->value;
3393 value->datatype = revert_automatic_type_conversion(value);
3395 type_t *orig_type = value->datatype;
3396 if(orig_type == NULL)
3399 if(value->type == EXPR_REFERENCE) {
3400 reference_expression_t *reference = (reference_expression_t*) value;
3401 declaration_t *declaration = reference->declaration;
3402 if(declaration != NULL) {
3403 declaration->address_taken = 1;
3407 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3410 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
3411 static expression_t *parse_##unexpression_type(unsigned precedence) \
3415 unary_expression_t *unary_expression \
3416 = allocate_ast_zero(sizeof(unary_expression[0])); \
3417 unary_expression->expression.type = EXPR_UNARY; \
3418 unary_expression->type = unexpression_type; \
3419 unary_expression->value = parse_sub_expression(precedence); \
3421 sfunc(unary_expression); \
3423 return (expression_t*) unary_expression; \
3426 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
3427 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
3428 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_scalar)
3429 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
3430 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
3431 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
3432 semantic_unexpr_integer)
3433 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
3435 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
3438 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
3440 static expression_t *parse_##unexpression_type(unsigned precedence, \
3441 expression_t *left) \
3443 (void) precedence; \
3446 unary_expression_t *unary_expression \
3447 = allocate_ast_zero(sizeof(unary_expression[0])); \
3448 unary_expression->expression.type = EXPR_UNARY; \
3449 unary_expression->type = unexpression_type; \
3450 unary_expression->value = left; \
3452 sfunc(unary_expression); \
3454 return (expression_t*) unary_expression; \
3457 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
3459 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
3462 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
3464 /* TODO: handle complex + imaginary types */
3466 /* § 6.3.1.8 Usual arithmetic conversions */
3467 if(type_left == type_long_double || type_right == type_long_double) {
3468 return type_long_double;
3469 } else if(type_left == type_double || type_right == type_double) {
3471 } else if(type_left == type_float || type_right == type_float) {
3475 type_right = promote_integer(type_right);
3476 type_left = promote_integer(type_left);
3478 if(type_left == type_right)
3481 bool signed_left = is_type_signed(type_left);
3482 bool signed_right = is_type_signed(type_right);
3483 if(get_rank(type_left) < get_rank(type_right)) {
3484 if(signed_left == signed_right || !signed_right) {
3490 if(signed_left == signed_right || !signed_left) {
3498 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
3500 expression_t *left = expression->left;
3501 expression_t *right = expression->right;
3502 type_t *orig_type_left = left->datatype;
3503 type_t *orig_type_right = right->datatype;
3505 if(orig_type_left == NULL || orig_type_right == NULL)
3508 type_t *type_left = skip_typeref(orig_type_left);
3509 type_t *type_right = skip_typeref(orig_type_right);
3511 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3512 /* TODO: improve error message */
3513 parser_print_error_prefix();
3514 fprintf(stderr, "operation needs arithmetic types\n");
3518 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3519 expression->left = create_implicit_cast(left, arithmetic_type);
3520 expression->right = create_implicit_cast(right, arithmetic_type);
3521 expression->expression.datatype = arithmetic_type;
3524 static void semantic_shift_op(binary_expression_t *expression)
3526 expression_t *left = expression->left;
3527 expression_t *right = expression->right;
3528 type_t *orig_type_left = left->datatype;
3529 type_t *orig_type_right = right->datatype;
3531 if(orig_type_left == NULL || orig_type_right == NULL)
3534 type_t *type_left = skip_typeref(orig_type_left);
3535 type_t *type_right = skip_typeref(orig_type_right);
3537 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3538 /* TODO: improve error message */
3539 parser_print_error_prefix();
3540 fprintf(stderr, "operation needs integer types\n");
3544 type_left = promote_integer(type_left);
3545 type_right = promote_integer(type_right);
3547 expression->left = create_implicit_cast(left, type_left);
3548 expression->right = create_implicit_cast(right, type_right);
3549 expression->expression.datatype = type_left;
3552 static void semantic_add(binary_expression_t *expression)
3554 expression_t *left = expression->left;
3555 expression_t *right = expression->right;
3556 type_t *orig_type_left = left->datatype;
3557 type_t *orig_type_right = right->datatype;
3559 if(orig_type_left == NULL || orig_type_right == NULL)
3562 type_t *type_left = skip_typeref(orig_type_left);
3563 type_t *type_right = skip_typeref(orig_type_right);
3566 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3567 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3568 expression->left = create_implicit_cast(left, arithmetic_type);
3569 expression->right = create_implicit_cast(right, arithmetic_type);
3570 expression->expression.datatype = arithmetic_type;
3572 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
3573 expression->expression.datatype = type_left;
3574 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
3575 expression->expression.datatype = type_right;
3577 parser_print_error_prefix();
3578 fprintf(stderr, "invalid operands to binary + (");
3579 print_type_quoted(orig_type_left);
3580 fprintf(stderr, ", ");
3581 print_type_quoted(orig_type_right);
3582 fprintf(stderr, ")\n");
3586 static void semantic_sub(binary_expression_t *expression)
3588 expression_t *left = expression->left;
3589 expression_t *right = expression->right;
3590 type_t *orig_type_left = left->datatype;
3591 type_t *orig_type_right = right->datatype;
3593 if(orig_type_left == NULL || orig_type_right == NULL)
3596 type_t *type_left = skip_typeref(orig_type_left);
3597 type_t *type_right = skip_typeref(orig_type_right);
3600 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3601 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3602 expression->left = create_implicit_cast(left, arithmetic_type);
3603 expression->right = create_implicit_cast(right, arithmetic_type);
3604 expression->expression.datatype = arithmetic_type;
3606 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3607 expression->expression.datatype = type_left;
3608 } else if(type_left->type == TYPE_POINTER &&
3609 type_right->type == TYPE_POINTER) {
3610 if(!pointers_compatible(type_left, type_right)) {
3611 parser_print_error_prefix();
3612 fprintf(stderr, "pointers to incompatible objects to binary - (");
3613 print_type_quoted(orig_type_left);
3614 fprintf(stderr, ", ");
3615 print_type_quoted(orig_type_right);
3616 fprintf(stderr, ")\n");
3618 expression->expression.datatype = type_ptrdiff_t;
3621 parser_print_error_prefix();
3622 fprintf(stderr, "invalid operands to binary - (");
3623 print_type_quoted(orig_type_left);
3624 fprintf(stderr, ", ");
3625 print_type_quoted(orig_type_right);
3626 fprintf(stderr, ")\n");
3630 static void semantic_comparison(binary_expression_t *expression)
3632 expression_t *left = expression->left;
3633 expression_t *right = expression->right;
3634 type_t *orig_type_left = left->datatype;
3635 type_t *orig_type_right = right->datatype;
3637 if(orig_type_left == NULL || orig_type_right == NULL)
3640 type_t *type_left = skip_typeref(orig_type_left);
3641 type_t *type_right = skip_typeref(orig_type_right);
3643 /* TODO non-arithmetic types */
3644 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3645 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3646 expression->left = create_implicit_cast(left, arithmetic_type);
3647 expression->right = create_implicit_cast(right, arithmetic_type);
3648 expression->expression.datatype = arithmetic_type;
3649 } else if (type_left->type == TYPE_POINTER &&
3650 type_right->type == TYPE_POINTER) {
3651 /* TODO check compatibility */
3652 } else if (type_left->type == TYPE_POINTER) {
3653 expression->right = create_implicit_cast(right, type_left);
3654 } else if (type_right->type == TYPE_POINTER) {
3655 expression->left = create_implicit_cast(left, type_right);
3657 type_error_incompatible("invalid operands in comparison",
3658 token.source_position, type_left, type_right);
3660 expression->expression.datatype = type_int;
3663 static void semantic_arithmetic_assign(binary_expression_t *expression)
3665 expression_t *left = expression->left;
3666 expression_t *right = expression->right;
3667 type_t *orig_type_left = left->datatype;
3668 type_t *orig_type_right = right->datatype;
3670 if(orig_type_left == NULL || orig_type_right == NULL)
3673 type_t *type_left = skip_typeref(orig_type_left);
3674 type_t *type_right = skip_typeref(orig_type_right);
3676 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3677 /* TODO: improve error message */
3678 parser_print_error_prefix();
3679 fprintf(stderr, "operation needs arithmetic types\n");
3683 /* combined instructions are tricky. We can't create an implicit cast on
3684 * the left side, because we need the uncasted form for the store.
3685 * The ast2firm pass has to know that left_type must be right_type
3686 * for the arithmeitc operation and create a cast by itself */
3687 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3688 expression->right = create_implicit_cast(right, arithmetic_type);
3689 expression->expression.datatype = type_left;
3692 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3694 expression_t *left = expression->left;
3695 expression_t *right = expression->right;
3696 type_t *orig_type_left = left->datatype;
3697 type_t *orig_type_right = right->datatype;
3699 if(orig_type_left == NULL || orig_type_right == NULL)
3702 type_t *type_left = skip_typeref(orig_type_left);
3703 type_t *type_right = skip_typeref(orig_type_right);
3705 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3706 /* combined instructions are tricky. We can't create an implicit cast on
3707 * the left side, because we need the uncasted form for the store.
3708 * The ast2firm pass has to know that left_type must be right_type
3709 * for the arithmeitc operation and create a cast by itself */
3710 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3711 expression->right = create_implicit_cast(right, arithmetic_type);
3712 expression->expression.datatype = type_left;
3713 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3714 expression->expression.datatype = type_left;
3716 parser_print_error_prefix();
3717 fputs("Incompatible types ", stderr);
3718 print_type_quoted(orig_type_left);
3719 fputs(" and ", stderr);
3720 print_type_quoted(orig_type_right);
3721 fputs(" in assignment\n", stderr);
3726 static void semantic_logical_op(binary_expression_t *expression)
3728 expression_t *left = expression->left;
3729 expression_t *right = expression->right;
3730 type_t *orig_type_left = left->datatype;
3731 type_t *orig_type_right = right->datatype;
3733 if(orig_type_left == NULL || orig_type_right == NULL)
3736 type_t *type_left = skip_typeref(orig_type_left);
3737 type_t *type_right = skip_typeref(orig_type_right);
3739 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3740 /* TODO: improve error message */
3741 parser_print_error_prefix();
3742 fprintf(stderr, "operation needs scalar types\n");
3746 expression->expression.datatype = type_int;
3749 static bool has_const_fields(type_t *type)
3756 static void semantic_binexpr_assign(binary_expression_t *expression)
3758 expression_t *left = expression->left;
3759 type_t *orig_type_left = left->datatype;
3761 if(orig_type_left == NULL)
3764 type_t *type_left = revert_automatic_type_conversion(left);
3765 type_left = skip_typeref(orig_type_left);
3767 /* must be a modifiable lvalue */
3768 if (type_left->type == TYPE_ARRAY) {
3769 parser_print_error_prefix();
3770 fprintf(stderr, "Cannot assign to arrays ('");
3771 print_expression(left);
3772 fprintf(stderr, "')\n");
3775 if(type_left->qualifiers & TYPE_QUALIFIER_CONST) {
3776 parser_print_error_prefix();
3777 fprintf(stderr, "assignment to readonly location '");
3778 print_expression(left);
3779 fprintf(stderr, "' (type ");
3780 print_type_quoted(orig_type_left);
3781 fprintf(stderr, ")\n");
3784 if(is_type_incomplete(type_left)) {
3785 parser_print_error_prefix();
3786 fprintf(stderr, "left-hand side of assignment '");
3787 print_expression(left);
3788 fprintf(stderr, "' has incomplete type ");
3789 print_type_quoted(orig_type_left);
3790 fprintf(stderr, "\n");
3793 if(is_type_compound(type_left) && has_const_fields(type_left)) {
3794 parser_print_error_prefix();
3795 fprintf(stderr, "can't assign to '");
3796 print_expression(left);
3797 fprintf(stderr, "' because compound type ");
3798 print_type_quoted(orig_type_left);
3799 fprintf(stderr, " has readonly fields\n");
3803 semantic_assign(orig_type_left, &expression->right, "assignment");
3805 expression->expression.datatype = orig_type_left;
3808 static void semantic_comma(binary_expression_t *expression)
3810 expression->expression.datatype = expression->right->datatype;
3813 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3814 static expression_t *parse_##binexpression_type(unsigned precedence, \
3815 expression_t *left) \
3819 expression_t *right = parse_sub_expression(precedence + lr); \
3821 binary_expression_t *binexpr \
3822 = allocate_ast_zero(sizeof(binexpr[0])); \
3823 binexpr->expression.type = EXPR_BINARY; \
3824 binexpr->type = binexpression_type; \
3825 binexpr->left = left; \
3826 binexpr->right = right; \
3829 return (expression_t*) binexpr; \
3832 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3833 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3834 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3835 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3836 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3837 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3838 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3839 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3840 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3841 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3842 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3843 semantic_comparison, 1)
3844 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3845 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3846 semantic_comparison, 1)
3847 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3848 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3849 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3850 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3851 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3852 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3853 semantic_shift_op, 1)
3854 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3855 semantic_shift_op, 1)
3856 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3857 semantic_arithmetic_addsubb_assign, 0)
3858 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3859 semantic_arithmetic_addsubb_assign, 0)
3860 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3861 semantic_arithmetic_assign, 0)
3862 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3863 semantic_arithmetic_assign, 0)
3864 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3865 semantic_arithmetic_assign, 0)
3866 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3867 semantic_arithmetic_assign, 0)
3868 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3869 semantic_arithmetic_assign, 0)
3870 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3871 semantic_arithmetic_assign, 0)
3872 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3873 semantic_arithmetic_assign, 0)
3874 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3875 semantic_arithmetic_assign, 0)
3877 static expression_t *parse_sub_expression(unsigned precedence)
3879 if(token.type < 0) {
3880 return expected_expression_error();
3883 expression_parser_function_t *parser
3884 = &expression_parsers[token.type];
3885 source_position_t source_position = token.source_position;
3888 if(parser->parser != NULL) {
3889 left = parser->parser(parser->precedence);
3891 left = parse_primary_expression();
3893 assert(left != NULL);
3894 left->source_position = source_position;
3897 if(token.type < 0) {
3898 return expected_expression_error();
3901 parser = &expression_parsers[token.type];
3902 if(parser->infix_parser == NULL)
3904 if(parser->infix_precedence < precedence)
3907 left = parser->infix_parser(parser->infix_precedence, left);
3909 assert(left != NULL);
3910 assert(left->type != EXPR_UNKNOWN);
3911 left->source_position = source_position;
3917 static expression_t *parse_expression(void)
3919 return parse_sub_expression(1);
3924 static void register_expression_parser(parse_expression_function parser,
3925 int token_type, unsigned precedence)
3927 expression_parser_function_t *entry = &expression_parsers[token_type];
3929 if(entry->parser != NULL) {
3930 fprintf(stderr, "for token ");
3931 print_token_type(stderr, token_type);
3932 fprintf(stderr, "\n");
3933 panic("trying to register multiple expression parsers for a token");
3935 entry->parser = parser;
3936 entry->precedence = precedence;
3939 static void register_expression_infix_parser(
3940 parse_expression_infix_function parser, int token_type,
3941 unsigned precedence)
3943 expression_parser_function_t *entry = &expression_parsers[token_type];
3945 if(entry->infix_parser != NULL) {
3946 fprintf(stderr, "for token ");
3947 print_token_type(stderr, token_type);
3948 fprintf(stderr, "\n");
3949 panic("trying to register multiple infix expression parsers for a "
3952 entry->infix_parser = parser;
3953 entry->infix_precedence = precedence;
3956 static void init_expression_parsers(void)
3958 memset(&expression_parsers, 0, sizeof(expression_parsers));
3960 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3961 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3962 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3963 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3964 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3965 T_GREATERGREATER, 16);
3966 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3967 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3968 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3969 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3970 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3971 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3972 T_GREATEREQUAL, 14);
3973 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3974 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3975 T_EXCLAMATIONMARKEQUAL, 13);
3976 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3977 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3978 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3979 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3980 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3981 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3982 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3983 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3984 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3985 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3986 T_ASTERISKEQUAL, 2);
3987 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3988 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3990 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3991 T_LESSLESSEQUAL, 2);
3992 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3993 T_GREATERGREATEREQUAL, 2);
3994 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3996 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3998 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
4001 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
4003 register_expression_infix_parser(parse_array_expression, '[', 30);
4004 register_expression_infix_parser(parse_call_expression, '(', 30);
4005 register_expression_infix_parser(parse_select_expression, '.', 30);
4006 register_expression_infix_parser(parse_select_expression,
4007 T_MINUSGREATER, 30);
4008 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
4010 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
4013 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
4014 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
4015 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
4016 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
4017 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
4018 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
4019 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
4020 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
4021 register_expression_parser(parse_sizeof, T_sizeof, 25);
4022 register_expression_parser(parse_extension, T___extension__, 25);
4023 register_expression_parser(parse_builtin_classify_type,
4024 T___builtin_classify_type, 25);
4028 static statement_t *parse_case_statement(void)
4031 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4032 label->statement.type = STATEMENT_CASE_LABEL;
4033 label->statement.source_position = token.source_position;
4035 label->expression = parse_expression();
4038 label->label_statement = parse_statement();
4040 return (statement_t*) label;
4043 static statement_t *parse_default_statement(void)
4047 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
4048 label->statement.type = STATEMENT_CASE_LABEL;
4049 label->statement.source_position = token.source_position;
4052 label->label_statement = parse_statement();
4054 return (statement_t*) label;
4057 static declaration_t *get_label(symbol_t *symbol)
4059 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4060 assert(current_function != NULL);
4061 /* if we found a label in the same function, then we already created the
4063 if(candidate != NULL
4064 && candidate->parent_context == ¤t_function->context) {
4068 /* otherwise we need to create a new one */
4069 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4070 declaration->namespc = NAMESPACE_LABEL;
4071 declaration->symbol = symbol;
4073 label_push(declaration);
4078 static statement_t *parse_label_statement(void)
4080 assert(token.type == T_IDENTIFIER);
4081 symbol_t *symbol = token.v.symbol;
4084 declaration_t *label = get_label(symbol);
4086 /* if source position is already set then the label is defined twice,
4087 * otherwise it was just mentioned in a goto so far */
4088 if(label->source_position.input_name != NULL) {
4089 parser_print_error_prefix();
4090 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
4091 parser_print_error_prefix_pos(label->source_position);
4092 fprintf(stderr, "previous definition of '%s' was here\n",
4095 label->source_position = token.source_position;
4098 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4100 label_statement->statement.type = STATEMENT_LABEL;
4101 label_statement->statement.source_position = token.source_position;
4102 label_statement->label = label;
4106 if(token.type == '}') {
4107 parse_error("label at end of compound statement");
4108 return (statement_t*) label_statement;
4110 label_statement->label_statement = parse_statement();
4113 return (statement_t*) label_statement;
4116 static statement_t *parse_if(void)
4120 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4121 statement->statement.type = STATEMENT_IF;
4122 statement->statement.source_position = token.source_position;
4125 statement->condition = parse_expression();
4128 statement->true_statement = parse_statement();
4129 if(token.type == T_else) {
4131 statement->false_statement = parse_statement();
4134 return (statement_t*) statement;
4137 static statement_t *parse_switch(void)
4141 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4142 statement->statement.type = STATEMENT_SWITCH;
4143 statement->statement.source_position = token.source_position;
4146 statement->expression = parse_expression();
4148 statement->body = parse_statement();
4150 return (statement_t*) statement;
4153 static statement_t *parse_while(void)
4157 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4158 statement->statement.type = STATEMENT_WHILE;
4159 statement->statement.source_position = token.source_position;
4162 statement->condition = parse_expression();
4164 statement->body = parse_statement();
4166 return (statement_t*) statement;
4169 static statement_t *parse_do(void)
4173 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4174 statement->statement.type = STATEMENT_DO_WHILE;
4175 statement->statement.source_position = token.source_position;
4177 statement->body = parse_statement();
4180 statement->condition = parse_expression();
4184 return (statement_t*) statement;
4187 static statement_t *parse_for(void)
4191 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4192 statement->statement.type = STATEMENT_FOR;
4193 statement->statement.source_position = token.source_position;
4197 int top = environment_top();
4198 context_t *last_context = context;
4199 set_context(&statement->context);
4201 if(token.type != ';') {
4202 if(is_declaration_specifier(&token, false)) {
4203 parse_declaration();
4205 statement->initialisation = parse_expression();
4212 if(token.type != ';') {
4213 statement->condition = parse_expression();
4216 if(token.type != ')') {
4217 statement->step = parse_expression();
4220 statement->body = parse_statement();
4222 assert(context == &statement->context);
4223 set_context(last_context);
4224 environment_pop_to(top);
4226 return (statement_t*) statement;
4229 static statement_t *parse_goto(void)
4233 if(token.type != T_IDENTIFIER) {
4234 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
4238 symbol_t *symbol = token.v.symbol;
4241 declaration_t *label = get_label(symbol);
4243 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4245 statement->statement.type = STATEMENT_GOTO;
4246 statement->statement.source_position = token.source_position;
4248 statement->label = label;
4252 return (statement_t*) statement;
4255 static statement_t *parse_continue(void)
4260 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4261 statement->type = STATEMENT_CONTINUE;
4262 statement->source_position = token.source_position;
4267 static statement_t *parse_break(void)
4272 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4273 statement->type = STATEMENT_BREAK;
4274 statement->source_position = token.source_position;
4279 static statement_t *parse_return(void)
4283 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4285 statement->statement.type = STATEMENT_RETURN;
4286 statement->statement.source_position = token.source_position;
4288 assert(current_function->type->type == TYPE_FUNCTION);
4289 function_type_t *function_type = (function_type_t*) current_function->type;
4290 type_t *return_type = function_type->result_type;
4292 expression_t *return_value;
4293 if(token.type != ';') {
4294 return_value = parse_expression();
4296 if(return_type == type_void && return_value->datatype != type_void) {
4297 parse_warning("'return' with a value, in function returning void");
4298 return_value = NULL;
4300 if(return_type != NULL) {
4301 semantic_assign(return_type, &return_value, "'return'");
4305 return_value = NULL;
4306 if(return_type != type_void) {
4307 parse_warning("'return' without value, in function returning "
4311 statement->return_value = return_value;
4315 return (statement_t*) statement;
4318 static statement_t *parse_declaration_statement(void)
4320 declaration_t *before = last_declaration;
4322 declaration_statement_t *statement
4323 = allocate_ast_zero(sizeof(statement[0]));
4324 statement->statement.type = STATEMENT_DECLARATION;
4325 statement->statement.source_position = token.source_position;
4327 declaration_specifiers_t specifiers;
4328 memset(&specifiers, 0, sizeof(specifiers));
4329 parse_declaration_specifiers(&specifiers);
4331 if(token.type == ';') {
4334 parse_init_declarators(&specifiers);
4337 if(before == NULL) {
4338 statement->declarations_begin = context->declarations;
4340 statement->declarations_begin = before->next;
4342 statement->declarations_end = last_declaration;
4344 return (statement_t*) statement;
4347 static statement_t *parse_expression_statement(void)
4349 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4350 statement->statement.type = STATEMENT_EXPRESSION;
4351 statement->statement.source_position = token.source_position;
4353 statement->expression = parse_expression();
4357 return (statement_t*) statement;
4360 static statement_t *parse_statement(void)
4362 statement_t *statement = NULL;
4364 /* declaration or statement */
4365 switch(token.type) {
4367 statement = parse_case_statement();
4371 statement = parse_default_statement();
4375 statement = parse_compound_statement();
4379 statement = parse_if();
4383 statement = parse_switch();
4387 statement = parse_while();
4391 statement = parse_do();
4395 statement = parse_for();
4399 statement = parse_goto();
4403 statement = parse_continue();
4407 statement = parse_break();
4411 statement = parse_return();
4420 if(look_ahead(1)->type == ':') {
4421 statement = parse_label_statement();
4425 if(is_typedef_symbol(token.v.symbol)) {
4426 statement = parse_declaration_statement();
4430 statement = parse_expression_statement();
4433 case T___extension__:
4434 /* this can be a prefix to a declaration or an expression statement */
4435 /* we simply eat it now and parse the rest with tail recursion */
4438 } while(token.type == T___extension__);
4439 statement = parse_statement();
4443 statement = parse_declaration_statement();
4447 statement = parse_expression_statement();
4451 assert(statement == NULL || statement->source_position.input_name != NULL);
4456 static statement_t *parse_compound_statement(void)
4458 compound_statement_t *compound_statement
4459 = allocate_ast_zero(sizeof(compound_statement[0]));
4460 compound_statement->statement.type = STATEMENT_COMPOUND;
4461 compound_statement->statement.source_position = token.source_position;
4465 int top = environment_top();
4466 context_t *last_context = context;
4467 set_context(&compound_statement->context);
4469 statement_t *last_statement = NULL;
4471 while(token.type != '}' && token.type != T_EOF) {
4472 statement_t *statement = parse_statement();
4473 if(statement == NULL)
4476 if(last_statement != NULL) {
4477 last_statement->next = statement;
4479 compound_statement->statements = statement;
4482 while(statement->next != NULL)
4483 statement = statement->next;
4485 last_statement = statement;
4488 if(token.type != '}') {
4489 parser_print_error_prefix_pos(
4490 compound_statement->statement.source_position);
4491 fprintf(stderr, "end of file while looking for closing '}'\n");
4495 assert(context == &compound_statement->context);
4496 set_context(last_context);
4497 environment_pop_to(top);
4499 return (statement_t*) compound_statement;
4502 static translation_unit_t *parse_translation_unit(void)
4504 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
4506 assert(global_context == NULL);
4507 global_context = &unit->context;
4509 assert(context == NULL);
4510 set_context(&unit->context);
4512 while(token.type != T_EOF) {
4513 parse_declaration();
4516 assert(context == &unit->context);
4518 last_declaration = NULL;
4520 assert(global_context == &unit->context);
4521 global_context = NULL;
4526 translation_unit_t *parse(void)
4528 environment_stack = NEW_ARR_F(stack_entry_t, 0);
4529 label_stack = NEW_ARR_F(stack_entry_t, 0);
4530 found_error = false;
4532 type_set_output(stderr);
4533 ast_set_output(stderr);
4535 lookahead_bufpos = 0;
4536 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
4539 translation_unit_t *unit = parse_translation_unit();
4541 DEL_ARR_F(environment_stack);
4542 DEL_ARR_F(label_stack);
4550 void init_parser(void)
4552 init_expression_parsers();
4553 obstack_init(&temp_obst);
4555 type_int = make_atomic_type(ATOMIC_TYPE_INT, TYPE_QUALIFIER_NONE);
4556 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, TYPE_QUALIFIER_NONE);
4557 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, TYPE_QUALIFIER_NONE);
4558 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, TYPE_QUALIFIER_NONE);
4559 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, TYPE_QUALIFIER_NONE);
4560 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, TYPE_QUALIFIER_NONE);
4561 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, TYPE_QUALIFIER_NONE);
4562 type_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_NONE);
4563 type_void = make_atomic_type(ATOMIC_TYPE_VOID, TYPE_QUALIFIER_NONE);
4564 type_void_ptr = make_pointer_type(type_void, TYPE_QUALIFIER_NONE);
4565 type_string = make_pointer_type(type_char, TYPE_QUALIFIER_NONE);
4568 void exit_parser(void)
4570 obstack_free(&temp_obst, NULL);