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 namespace;
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_const_char = NULL;
46 static type_t *type_string = NULL;
47 static type_t *type_void = NULL;
48 static type_t *type_size_t = NULL;
49 static type_t *type_ptrdiff_t = NULL;
51 static statement_t *parse_compound_statement(void);
52 static statement_t *parse_statement(void);
54 static expression_t *parse_sub_expression(unsigned precedence);
55 static expression_t *parse_expression(void);
56 static type_t *parse_typename(void);
58 #define STORAGE_CLASSES \
65 #define TYPE_QUALIFIERS \
71 #ifdef PROVIDE_COMPLEX
72 #define COMPLEX_SPECIFIERS \
75 #define COMPLEX_SPECIFIERS
78 #ifdef PROVIDE_IMAGINARY
79 #define IMAGINARY_SPECIFIERS \
82 #define IMAGINARY_SPECIFIERS
85 #define TYPE_SPECIFIERS \
103 #define DECLARATION_START \
108 #define TYPENAME_START \
112 static inline void *allocate_ast_zero(size_t size)
114 void *res = allocate_ast(size);
115 memset(res, 0, size);
119 static inline void *allocate_type_zero(size_t size)
121 void *res = obstack_alloc(type_obst, size);
122 memset(res, 0, size);
126 static inline void free_type(void *type)
128 obstack_free(type_obst, type);
132 * returns the top element of the environment stack
134 static inline size_t environment_top(void)
136 return ARR_LEN(environment_stack);
139 static inline size_t label_top(void)
141 return ARR_LEN(label_stack);
146 static inline void next_token(void)
148 token = lookahead_buffer[lookahead_bufpos];
149 lookahead_buffer[lookahead_bufpos] = lexer_token;
152 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
155 print_token(stderr, &token);
156 fprintf(stderr, "\n");
160 static inline const token_t *look_ahead(int num)
162 assert(num > 0 && num <= MAX_LOOKAHEAD);
163 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
164 return & lookahead_buffer[pos];
167 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
169 static void error(void)
172 #ifdef ABORT_ON_ERROR
177 static void parser_print_prefix_pos(const source_position_t source_position)
179 fputs(source_position.input_name, stderr);
181 fprintf(stderr, "%d", source_position.linenr);
185 static void parser_print_error_prefix_pos(
186 const source_position_t source_position)
188 parser_print_prefix_pos(source_position);
189 fputs("error: ", stderr);
193 static void parser_print_error_prefix(void)
195 parser_print_error_prefix_pos(token.source_position);
198 static void parse_error(const char *message)
200 parser_print_error_prefix();
201 fprintf(stderr, "parse error: %s\n", message);
204 static void parser_print_warning_prefix_pos(
205 const source_position_t source_position)
207 parser_print_prefix_pos(source_position);
208 fputs("warning: ", stderr);
211 static void parse_warning_pos(const source_position_t source_position,
212 const char *const message)
214 parser_print_prefix_pos(source_position);
215 fprintf(stderr, "warning: %s\n", message);
218 static void parse_warning(const char *message)
220 parse_warning_pos(token.source_position, message);
223 static void parse_error_expected(const char *message, ...)
228 if(message != NULL) {
229 parser_print_error_prefix();
230 fprintf(stderr, "%s\n", message);
232 parser_print_error_prefix();
233 fputs("Parse error: got ", stderr);
234 print_token(stderr, &token);
235 fputs(", expected ", stderr);
237 va_start(args, message);
238 token_type_t token_type = va_arg(args, token_type_t);
239 while(token_type != 0) {
243 fprintf(stderr, ", ");
245 print_token_type(stderr, token_type);
246 token_type = va_arg(args, token_type_t);
249 fprintf(stderr, "\n");
252 static void print_type_quoted(type_t *type)
259 static void type_error(const char *msg, const source_position_t source_position,
262 parser_print_error_prefix_pos(source_position);
263 fprintf(stderr, "%s, but found type ", msg);
264 print_type_quoted(type);
268 static void type_error_incompatible(const char *msg,
269 const source_position_t source_position, type_t *type1, type_t *type2)
271 parser_print_error_prefix_pos(source_position);
272 fprintf(stderr, "%s, incompatible types: ", msg);
273 print_type_quoted(type1);
274 fprintf(stderr, " - ");
275 print_type_quoted(type2);
276 fprintf(stderr, ")\n");
279 static void eat_block(void)
281 if(token.type == '{')
284 while(token.type != '}') {
285 if(token.type == T_EOF)
287 if(token.type == '{') {
296 static void eat_statement(void)
298 while(token.type != ';') {
299 if(token.type == T_EOF)
301 if(token.type == '}')
303 if(token.type == '{') {
312 static void eat_brace(void)
314 if(token.type == '(')
317 while(token.type != ')') {
318 if(token.type == T_EOF)
320 if(token.type == ')' || token.type == ';' || token.type == '}') {
323 if(token.type == '(') {
327 if(token.type == '{') {
336 #define expect(expected) \
337 if(UNLIKELY(token.type != (expected))) { \
338 parse_error_expected(NULL, (expected), 0); \
344 #define expect_void(expected) \
345 if(UNLIKELY(token.type != (expected))) { \
346 parse_error_expected(NULL, (expected), 0); \
352 static void set_context(context_t *new_context)
354 context = new_context;
356 last_declaration = new_context->declarations;
357 if(last_declaration != NULL) {
358 while(last_declaration->next != NULL) {
359 last_declaration = last_declaration->next;
365 * called when we find a 2nd declarator for an identifier we already have a
368 static bool is_compatible_declaration (declaration_t *declaration,
369 declaration_t *previous)
371 /* TODO: not correct yet */
372 return declaration->type == previous->type;
375 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
377 declaration_t *declaration = symbol->declaration;
378 for( ; declaration != NULL; declaration = declaration->symbol_next) {
379 if(declaration->namespace == namespace)
386 static const char *get_namespace_prefix(namespace_t namespace)
389 case NAMESPACE_NORMAL:
391 case NAMESPACE_UNION:
393 case NAMESPACE_STRUCT:
397 case NAMESPACE_LABEL:
400 panic("invalid namespace found");
404 * pushs an environment_entry on the environment stack and links the
405 * corresponding symbol to the new entry
407 static declaration_t *stack_push(stack_entry_t **stack_ptr,
408 declaration_t *declaration,
409 context_t *parent_context)
411 symbol_t *symbol = declaration->symbol;
412 namespace_t namespace = (namespace_t)declaration->namespace;
414 /* a declaration should be only pushed once */
415 assert(declaration->parent_context == NULL);
416 declaration->parent_context = parent_context;
418 declaration_t *previous_declaration = get_declaration(symbol, namespace);
419 assert(declaration != previous_declaration);
420 if(previous_declaration != NULL
421 && previous_declaration->parent_context == context) {
422 if(!is_compatible_declaration(declaration, previous_declaration)) {
423 parser_print_error_prefix_pos(declaration->source_position);
424 fprintf(stderr, "definition of symbol %s%s with type ",
425 get_namespace_prefix(namespace), symbol->string);
426 print_type_quoted(declaration->type);
428 parser_print_error_prefix_pos(
429 previous_declaration->source_position);
430 fprintf(stderr, "is incompatible with previous declaration "
432 print_type_quoted(previous_declaration->type);
435 const storage_class_t old_storage = previous_declaration->storage_class;
436 const storage_class_t new_storage = declaration->storage_class;
437 if (current_function == NULL) {
438 if (old_storage != STORAGE_CLASS_STATIC &&
439 new_storage == STORAGE_CLASS_STATIC) {
440 parser_print_error_prefix_pos(declaration->source_position);
442 "static declaration of '%s' follows non-static declaration\n",
444 parser_print_error_prefix_pos(previous_declaration->source_position);
445 fprintf(stderr, "previous declaration of '%s' was here\n",
448 if (old_storage == STORAGE_CLASS_EXTERN) {
449 if (new_storage == STORAGE_CLASS_NONE) {
450 previous_declaration->storage_class = STORAGE_CLASS_NONE;
453 parser_print_warning_prefix_pos(declaration->source_position);
454 fprintf(stderr, "redundant declaration for '%s'\n",
456 parser_print_warning_prefix_pos(previous_declaration->source_position);
457 fprintf(stderr, "previous declaration of '%s' was here\n",
462 if (old_storage == STORAGE_CLASS_EXTERN &&
463 new_storage == STORAGE_CLASS_EXTERN) {
464 parser_print_warning_prefix_pos(declaration->source_position);
465 fprintf(stderr, "redundant extern declaration for '%s'\n",
467 parser_print_warning_prefix_pos(previous_declaration->source_position);
468 fprintf(stderr, "previous declaration of '%s' was here\n",
471 parser_print_error_prefix_pos(declaration->source_position);
472 if (old_storage == new_storage) {
473 fprintf(stderr, "redeclaration of '%s'\n", symbol->string);
475 fprintf(stderr, "redeclaration of '%s' with different linkage\n", symbol->string);
477 parser_print_error_prefix_pos(previous_declaration->source_position);
478 fprintf(stderr, "previous declaration of '%s' was here\n",
483 return previous_declaration;
486 /* remember old declaration */
488 entry.symbol = symbol;
489 entry.old_declaration = symbol->declaration;
490 entry.namespace = namespace;
491 ARR_APP1(stack_entry_t, *stack_ptr, entry);
493 /* replace/add declaration into declaration list of the symbol */
494 if(symbol->declaration == NULL) {
495 symbol->declaration = declaration;
497 declaration_t *iter_last = NULL;
498 declaration_t *iter = symbol->declaration;
499 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
500 /* replace an entry? */
501 if(iter->namespace == namespace) {
502 if(iter_last == NULL) {
503 symbol->declaration = declaration;
505 iter_last->symbol_next = declaration;
507 declaration->symbol_next = iter->symbol_next;
512 assert(iter_last->symbol_next == NULL);
513 iter_last->symbol_next = declaration;
520 static declaration_t *environment_push(declaration_t *declaration)
522 assert(declaration->source_position.input_name != NULL);
523 return stack_push(&environment_stack, declaration, context);
526 static declaration_t *label_push(declaration_t *declaration)
528 return stack_push(&label_stack, declaration, ¤t_function->context);
532 * pops symbols from the environment stack until @p new_top is the top element
534 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
536 stack_entry_t *stack = *stack_ptr;
537 size_t top = ARR_LEN(stack);
540 assert(new_top <= top);
544 for(i = top; i > new_top; --i) {
545 stack_entry_t *entry = & stack[i - 1];
547 declaration_t *old_declaration = entry->old_declaration;
548 symbol_t *symbol = entry->symbol;
549 namespace_t namespace = (namespace_t)entry->namespace;
551 /* replace/remove declaration */
552 declaration_t *declaration = symbol->declaration;
553 assert(declaration != NULL);
554 if(declaration->namespace == namespace) {
555 if(old_declaration == NULL) {
556 symbol->declaration = declaration->symbol_next;
558 symbol->declaration = old_declaration;
561 declaration_t *iter_last = declaration;
562 declaration_t *iter = declaration->symbol_next;
563 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
564 /* replace an entry? */
565 if(iter->namespace == namespace) {
566 assert(iter_last != NULL);
567 iter_last->symbol_next = old_declaration;
568 old_declaration->symbol_next = iter->symbol_next;
572 assert(iter != NULL);
576 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
579 static void environment_pop_to(size_t new_top)
581 stack_pop_to(&environment_stack, new_top);
584 static void label_pop_to(size_t new_top)
586 stack_pop_to(&label_stack, new_top);
590 static int get_rank(const type_t *type)
592 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
593 * and esp. footnote 108). However we can't fold constants (yet), so we
594 * can't decide wether unsigned int is possible, while int always works.
595 * (unsigned int would be preferable when possible... for stuff like
596 * struct { enum { ... } bla : 4; } ) */
597 if(type->type == TYPE_ENUM)
598 return ATOMIC_TYPE_INT;
600 assert(type->type == TYPE_ATOMIC);
601 atomic_type_t *atomic_type = (atomic_type_t*) type;
602 atomic_type_type_t atype = atomic_type->atype;
606 static type_t *promote_integer(type_t *type)
608 if(get_rank(type) < ATOMIC_TYPE_INT)
614 static expression_t *create_cast_expression(expression_t *expression,
617 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
619 cast->expression.type = EXPR_UNARY;
620 cast->type = UNEXPR_CAST;
621 cast->value = expression;
622 cast->expression.datatype = dest_type;
624 return (expression_t*) cast;
627 static bool is_null_expression(const expression_t *const expr)
629 if (expr->type != EXPR_CONST) return false;
631 type_t *const type = skip_typeref(expr->datatype);
632 if (!is_type_integer(type)) return false;
634 const const_t *const const_expr = (const const_t*)expr;
635 return const_expr->v.int_value == 0;
638 static expression_t *create_implicit_cast(expression_t *expression,
641 type_t *source_type = expression->datatype;
643 if(source_type == NULL)
646 source_type = skip_typeref(source_type);
647 dest_type = skip_typeref(dest_type);
649 if(source_type == dest_type)
652 if(dest_type->type == TYPE_ATOMIC) {
653 if(source_type->type != TYPE_ATOMIC)
654 panic("casting of non-atomic types not implemented yet");
656 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
657 type_error_incompatible("can't cast types",
658 expression->source_position,
659 source_type, dest_type);
663 return create_cast_expression(expression, dest_type);
665 if(dest_type->type == TYPE_POINTER) {
666 pointer_type_t *pointer_type
667 = (pointer_type_t*) dest_type;
668 switch (source_type->type) {
670 if (is_null_expression(expression)) {
671 return create_cast_expression(expression, dest_type);
676 if (pointers_compatible(source_type, dest_type)) {
677 return create_cast_expression(expression, dest_type);
682 array_type_t *const array_type = (array_type_t*) source_type;
683 if (types_compatible(array_type->element_type,
684 pointer_type->points_to)) {
685 return create_cast_expression(expression, dest_type);
691 panic("casting of non-atomic types not implemented yet");
694 type_error_incompatible("can't implicitely cast types",
695 expression->source_position,
696 source_type, dest_type);
700 panic("casting of non-atomic types not implemented yet");
703 static void semantic_assign(type_t *orig_type_left, expression_t **right,
706 type_t *orig_type_right = (*right)->datatype;
708 if(orig_type_right == NULL)
711 type_t *const type_left = skip_typeref(orig_type_left);
712 type_t *const type_right = skip_typeref(orig_type_right);
714 if(type_left == type_right) {
716 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
717 (type_left->type == TYPE_POINTER && is_null_expression(*right)) ||
718 (type_left->type == TYPE_POINTER && type_right->type == TYPE_POINTER)) {
719 *right = create_implicit_cast(*right, type_left);
721 /* TODO: improve error message */
722 parser_print_error_prefix();
723 fprintf(stderr, "incompatible types in %s\n", context);
724 parser_print_error_prefix();
725 print_type_quoted(type_left);
726 fputs(" <- ", stderr);
727 print_type_quoted(type_right);
733 static expression_t *parse_constant_expression(void)
735 /* start parsing at precedence 7 (conditional expression) */
736 return parse_sub_expression(7);
739 static expression_t *parse_assignment_expression(void)
741 /* start parsing at precedence 2 (assignment expression) */
742 return parse_sub_expression(2);
745 typedef struct declaration_specifiers_t declaration_specifiers_t;
746 struct declaration_specifiers_t {
747 storage_class_t storage_class;
752 static void parse_compound_type_entries(void);
753 static declaration_t *parse_declarator(
754 const declaration_specifiers_t *specifiers, type_t *type,
755 bool may_be_abstract);
756 static declaration_t *record_declaration(declaration_t *declaration);
758 static const char *parse_string_literals(void)
760 assert(token.type == T_STRING_LITERAL);
761 const char *result = token.v.string;
765 while(token.type == T_STRING_LITERAL) {
766 result = concat_strings(result, token.v.string);
773 static void parse_attributes(void)
777 case T___attribute__:
785 parse_error("EOF while parsing attribute");
803 if(token.type != T_STRING_LITERAL) {
804 parse_error_expected("while parsing assembler attribute",
809 parse_string_literals();
814 goto attributes_finished;
822 static designator_t *parse_designation(void)
824 if(token.type != '[' && token.type != '.')
827 designator_t *result = NULL;
828 designator_t *last = NULL;
831 designator_t *designator;
834 designator = allocate_ast_zero(sizeof(designator[0]));
836 designator->array_access = parse_constant_expression();
840 designator = allocate_ast_zero(sizeof(designator[0]));
842 if(token.type != T_IDENTIFIER) {
843 parse_error_expected("while parsing designator",
847 designator->symbol = token.v.symbol;
855 assert(designator != NULL);
857 last->next = designator;
865 static initializer_t *parse_initializer_list(type_t *type);
867 static initializer_t *parse_initializer(type_t *type)
869 designator_t *designator = parse_designation();
871 initializer_t *result;
872 if(token.type == '{') {
873 result = parse_initializer_list(type);
875 result = allocate_ast_zero(sizeof(result[0]));
876 result->type = INITIALIZER_VALUE;
877 result->v.value = parse_assignment_expression();
880 semantic_assign(type, &result->v.value, "initializer");
883 result->designator = designator;
888 static initializer_t *parse_initializer_list(type_t *type)
895 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
896 result->type = INITIALIZER_LIST;
898 initializer_t *last = NULL;
900 initializer_t *initializer = parse_initializer(NULL);
902 last->next = initializer;
904 result->v.list = initializer;
908 if(token.type == '}')
911 if(token.type != ',') {
912 parse_error_expected("while parsing initializer list", ',', '}', 0);
918 if(token.type == '}')
927 static declaration_t *parse_compound_type_specifier(bool is_struct)
935 symbol_t *symbol = NULL;
936 declaration_t *declaration = NULL;
938 if(token.type == T_IDENTIFIER) {
939 symbol = token.v.symbol;
943 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
945 declaration = get_declaration(symbol, NAMESPACE_UNION);
947 } else if(token.type != '{') {
949 parse_error_expected("while parsing struct type specifier",
950 T_IDENTIFIER, '{', 0);
952 parse_error_expected("while parsing union type specifier",
953 T_IDENTIFIER, '{', 0);
959 if(declaration == NULL) {
960 declaration = allocate_type_zero(sizeof(declaration[0]));
963 declaration->namespace = NAMESPACE_STRUCT;
965 declaration->namespace = NAMESPACE_UNION;
967 declaration->source_position = token.source_position;
968 declaration->symbol = symbol;
969 record_declaration(declaration);
972 if(token.type == '{') {
973 if(declaration->init.is_defined) {
974 assert(symbol != NULL);
975 parser_print_error_prefix();
976 fprintf(stderr, "multiple definition of %s %s\n",
977 is_struct ? "struct" : "union", symbol->string);
978 declaration->context.declarations = NULL;
980 declaration->init.is_defined = true;
982 int top = environment_top();
983 context_t *last_context = context;
984 set_context(& declaration->context);
986 parse_compound_type_entries();
989 assert(context == & declaration->context);
990 set_context(last_context);
991 environment_pop_to(top);
997 static void parse_enum_entries(void)
1001 if(token.type == '}') {
1003 parse_error("empty enum not allowed");
1008 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1010 if(token.type != T_IDENTIFIER) {
1011 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1015 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1016 entry->symbol = token.v.symbol;
1017 entry->source_position = token.source_position;
1020 if(token.type == '=') {
1022 entry->init.initializer = parse_initializer(type_int);
1025 record_declaration(entry);
1027 if(token.type != ',')
1030 } while(token.type != '}');
1035 static declaration_t *parse_enum_specifier(void)
1039 declaration_t *declaration;
1042 if(token.type == T_IDENTIFIER) {
1043 symbol = token.v.symbol;
1046 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1047 } else if(token.type != '{') {
1048 parse_error_expected("while parsing enum type specifier",
1049 T_IDENTIFIER, '{', 0);
1056 if(declaration == NULL) {
1057 declaration = allocate_type_zero(sizeof(declaration[0]));
1059 declaration->namespace = NAMESPACE_ENUM;
1060 declaration->source_position = token.source_position;
1061 declaration->symbol = symbol;
1064 if(token.type == '{') {
1065 if(declaration->init.is_defined) {
1066 parser_print_error_prefix();
1067 fprintf(stderr, "multiple definitions of enum %s\n",
1070 record_declaration(declaration);
1071 declaration->init.is_defined = 1;
1073 parse_enum_entries();
1081 * if a symbol is a typedef to another type, return true
1083 static bool is_typedef_symbol(symbol_t *symbol)
1085 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1086 if(declaration == NULL
1087 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1093 static type_t *parse_typeof(void)
1101 expression_t *expression = NULL;
1104 switch(token.type) {
1105 case T___extension__:
1106 /* this can be a prefix to a typename or an expression */
1107 /* we simply eat it now. */
1110 } while(token.type == T___extension__);
1114 if(is_typedef_symbol(token.v.symbol)) {
1115 type = parse_typename();
1117 expression = parse_expression();
1118 type = expression->datatype;
1123 type = parse_typename();
1127 expression = parse_expression();
1128 type = expression->datatype;
1134 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1135 typeof->type.type = TYPE_TYPEOF;
1136 typeof->expression = expression;
1137 typeof->typeof_type = type;
1139 return (type_t*) typeof;
1143 SPECIFIER_SIGNED = 1 << 0,
1144 SPECIFIER_UNSIGNED = 1 << 1,
1145 SPECIFIER_LONG = 1 << 2,
1146 SPECIFIER_INT = 1 << 3,
1147 SPECIFIER_DOUBLE = 1 << 4,
1148 SPECIFIER_CHAR = 1 << 5,
1149 SPECIFIER_SHORT = 1 << 6,
1150 SPECIFIER_LONG_LONG = 1 << 7,
1151 SPECIFIER_FLOAT = 1 << 8,
1152 SPECIFIER_BOOL = 1 << 9,
1153 SPECIFIER_VOID = 1 << 10,
1154 #ifdef PROVIDE_COMPLEX
1155 SPECIFIER_COMPLEX = 1 << 11,
1157 #ifdef PROVIDE_IMAGINARY
1158 SPECIFIER_IMAGINARY = 1 << 12,
1162 static type_t *create_builtin_type(symbol_t *symbol)
1164 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1165 type->type.type = TYPE_BUILTIN;
1166 type->symbol = symbol;
1168 type->real_type = type_int;
1170 return (type_t*) type;
1173 static type_t *get_typedef_type(symbol_t *symbol)
1175 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1176 if(declaration == NULL
1177 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1180 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1181 typedef_type->type.type = TYPE_TYPEDEF;
1182 typedef_type->declaration = declaration;
1184 return (type_t*) typedef_type;
1187 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1189 type_t *type = NULL;
1190 unsigned type_qualifiers = 0;
1191 unsigned type_specifiers = 0;
1195 switch(token.type) {
1198 #define MATCH_STORAGE_CLASS(token, class) \
1200 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1201 parse_error("multiple storage classes in declaration " \
1204 specifiers->storage_class = class; \
1208 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1209 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1210 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1211 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1212 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1214 /* type qualifiers */
1215 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1217 type_qualifiers |= qualifier; \
1221 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1222 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1223 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1225 case T___extension__:
1230 /* type specifiers */
1231 #define MATCH_SPECIFIER(token, specifier, name) \
1234 if(type_specifiers & specifier) { \
1235 parse_error("multiple " name " type specifiers given"); \
1237 type_specifiers |= specifier; \
1241 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1242 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1243 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1244 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1245 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1246 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1247 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1248 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1249 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1250 #ifdef PROVIDE_COMPLEX
1251 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1253 #ifdef PROVIDE_IMAGINARY
1254 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1258 specifiers->is_inline = true;
1263 if(type_specifiers & SPECIFIER_LONG_LONG) {
1264 parse_error("multiple type specifiers given");
1265 } else if(type_specifiers & SPECIFIER_LONG) {
1266 type_specifiers |= SPECIFIER_LONG_LONG;
1268 type_specifiers |= SPECIFIER_LONG;
1272 /* TODO: if type != NULL for the following rules should issue
1275 compound_type_t *compound_type
1276 = allocate_type_zero(sizeof(compound_type[0]));
1277 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1278 compound_type->declaration = parse_compound_type_specifier(true);
1280 type = (type_t*) compound_type;
1284 compound_type_t *compound_type
1285 = allocate_type_zero(sizeof(compound_type[0]));
1286 compound_type->type.type = TYPE_COMPOUND_UNION;
1287 compound_type->declaration = parse_compound_type_specifier(false);
1289 type = (type_t*) compound_type;
1293 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1294 enum_type->type.type = TYPE_ENUM;
1295 enum_type->declaration = parse_enum_specifier();
1297 type = (type_t*) enum_type;
1301 type = parse_typeof();
1303 case T___builtin_va_list:
1304 type = create_builtin_type(token.v.symbol);
1308 case T___attribute__:
1313 case T_IDENTIFIER: {
1314 type_t *typedef_type = get_typedef_type(token.v.symbol);
1316 if(typedef_type == NULL)
1317 goto finish_specifiers;
1320 type = typedef_type;
1324 /* function specifier */
1326 goto finish_specifiers;
1333 atomic_type_type_t atomic_type;
1335 /* match valid basic types */
1336 switch(type_specifiers) {
1337 case SPECIFIER_VOID:
1338 atomic_type = ATOMIC_TYPE_VOID;
1340 case SPECIFIER_CHAR:
1341 atomic_type = ATOMIC_TYPE_CHAR;
1343 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1344 atomic_type = ATOMIC_TYPE_SCHAR;
1346 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1347 atomic_type = ATOMIC_TYPE_UCHAR;
1349 case SPECIFIER_SHORT:
1350 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1351 case SPECIFIER_SHORT | SPECIFIER_INT:
1352 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1353 atomic_type = ATOMIC_TYPE_SHORT;
1355 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1356 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1357 atomic_type = ATOMIC_TYPE_USHORT;
1360 case SPECIFIER_SIGNED:
1361 case SPECIFIER_SIGNED | SPECIFIER_INT:
1362 atomic_type = ATOMIC_TYPE_INT;
1364 case SPECIFIER_UNSIGNED:
1365 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1366 atomic_type = ATOMIC_TYPE_UINT;
1368 case SPECIFIER_LONG:
1369 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1370 case SPECIFIER_LONG | SPECIFIER_INT:
1371 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1372 atomic_type = ATOMIC_TYPE_LONG;
1374 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1375 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1376 atomic_type = ATOMIC_TYPE_ULONG;
1378 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1379 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1380 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1381 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1383 atomic_type = ATOMIC_TYPE_LONGLONG;
1385 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1386 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1388 atomic_type = ATOMIC_TYPE_ULONGLONG;
1390 case SPECIFIER_FLOAT:
1391 atomic_type = ATOMIC_TYPE_FLOAT;
1393 case SPECIFIER_DOUBLE:
1394 atomic_type = ATOMIC_TYPE_DOUBLE;
1396 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1397 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1399 case SPECIFIER_BOOL:
1400 atomic_type = ATOMIC_TYPE_BOOL;
1402 #ifdef PROVIDE_COMPLEX
1403 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1404 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1406 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1407 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1409 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1410 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1413 #ifdef PROVIDE_IMAGINARY
1414 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1415 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1417 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1418 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1420 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1421 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1425 /* invalid specifier combination, give an error message */
1426 if(type_specifiers == 0) {
1428 parse_warning("no type specifiers in declaration (using int)");
1429 atomic_type = ATOMIC_TYPE_INT;
1432 parse_error("no type specifiers given in declaration");
1434 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1435 (type_specifiers & SPECIFIER_UNSIGNED)) {
1436 parse_error("signed and unsigned specifiers gives");
1437 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1438 parse_error("only integer types can be signed or unsigned");
1440 parse_error("multiple datatypes in declaration");
1442 atomic_type = ATOMIC_TYPE_INVALID;
1445 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1446 atype->type.type = TYPE_ATOMIC;
1447 atype->atype = atomic_type;
1450 type = (type_t*) atype;
1452 if(type_specifiers != 0) {
1453 parse_error("multiple datatypes in declaration");
1457 type->qualifiers = (type_qualifier_t)type_qualifiers;
1459 type_t *result = typehash_insert(type);
1460 if(newtype && result != (type_t*) type) {
1464 specifiers->type = result;
1467 static unsigned parse_type_qualifiers(void)
1469 unsigned type_qualifiers = TYPE_QUALIFIER_NONE;
1472 switch(token.type) {
1473 /* type qualifiers */
1474 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1475 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1476 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1479 return type_qualifiers;
1484 static void parse_identifier_list(void)
1487 if(token.type != T_IDENTIFIER) {
1488 parse_error_expected("while parsing parameter identifier list",
1493 if(token.type != ',')
1499 static declaration_t *parse_parameter(void)
1501 declaration_specifiers_t specifiers;
1502 memset(&specifiers, 0, sizeof(specifiers));
1504 parse_declaration_specifiers(&specifiers);
1506 declaration_t *declaration
1507 = parse_declarator(&specifiers, specifiers.type, true);
1509 /* TODO check declaration constraints for parameters */
1510 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1511 parse_error("typedef not allowed in parameter list");
1517 static declaration_t *parse_parameters(function_type_t *type)
1519 if(token.type == T_IDENTIFIER) {
1520 symbol_t *symbol = token.v.symbol;
1521 if(!is_typedef_symbol(symbol)) {
1523 parse_identifier_list();
1528 if(token.type == ')') {
1529 type->unspecified_parameters = 1;
1532 if(token.type == T_void && look_ahead(1)->type == ')') {
1537 declaration_t *declarations = NULL;
1538 declaration_t *declaration;
1539 declaration_t *last_declaration = NULL;
1540 function_parameter_t *parameter;
1541 function_parameter_t *last_parameter = NULL;
1544 switch(token.type) {
1548 return declarations;
1551 case T___extension__:
1553 declaration = parse_parameter();
1555 parameter = allocate_type_zero(sizeof(parameter[0]));
1556 parameter->type = declaration->type;
1558 if(last_parameter != NULL) {
1559 last_declaration->next = declaration;
1560 last_parameter->next = parameter;
1562 type->parameters = parameter;
1563 declarations = declaration;
1565 last_parameter = parameter;
1566 last_declaration = declaration;
1570 return declarations;
1572 if(token.type != ',')
1573 return declarations;
1583 } construct_type_type_t;
1585 typedef struct construct_type_t construct_type_t;
1586 struct construct_type_t {
1587 construct_type_type_t type;
1588 construct_type_t *next;
1591 typedef struct parsed_pointer_t parsed_pointer_t;
1592 struct parsed_pointer_t {
1593 construct_type_t construct_type;
1594 type_qualifier_t type_qualifiers;
1597 typedef struct construct_function_type_t construct_function_type_t;
1598 struct construct_function_type_t {
1599 construct_type_t construct_type;
1600 function_type_t *function_type;
1603 typedef struct parsed_array_t parsed_array_t;
1604 struct parsed_array_t {
1605 construct_type_t construct_type;
1606 type_qualifier_t type_qualifiers;
1612 typedef struct construct_base_type_t construct_base_type_t;
1613 struct construct_base_type_t {
1614 construct_type_t construct_type;
1618 static construct_type_t *parse_pointer_declarator(void)
1622 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1623 memset(pointer, 0, sizeof(pointer[0]));
1624 pointer->construct_type.type = CONSTRUCT_POINTER;
1625 pointer->type_qualifiers = parse_type_qualifiers();
1627 return (construct_type_t*) pointer;
1630 static construct_type_t *parse_array_declarator(void)
1634 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1635 memset(array, 0, sizeof(array[0]));
1636 array->construct_type.type = CONSTRUCT_ARRAY;
1638 if(token.type == T_static) {
1639 array->is_static = true;
1643 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1644 if(type_qualifiers != 0) {
1645 if(token.type == T_static) {
1646 array->is_static = true;
1650 array->type_qualifiers = type_qualifiers;
1652 if(token.type == '*' && look_ahead(1)->type == ']') {
1653 array->is_variable = true;
1655 } else if(token.type != ']') {
1656 array->size = parse_assignment_expression();
1661 return (construct_type_t*) array;
1664 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1668 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1669 type->type.type = TYPE_FUNCTION;
1671 declaration_t *parameters = parse_parameters(type);
1672 if(declaration != NULL) {
1673 declaration->context.declarations = parameters;
1676 construct_function_type_t *construct_function_type =
1677 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1678 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1679 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1680 construct_function_type->function_type = type;
1684 return (construct_type_t*) construct_function_type;
1687 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1688 int may_be_abstract)
1690 construct_type_t *result = NULL;
1691 construct_type_t *last = NULL;
1693 while(token.type == '*') {
1694 construct_type_t *type = parse_pointer_declarator();
1703 /* TODO: find out if this is correct */
1706 construct_type_t *inner_types = NULL;
1708 switch(token.type) {
1710 if(declaration == NULL) {
1711 parse_error("no identifier expected in typename");
1713 declaration->symbol = token.v.symbol;
1714 declaration->source_position = token.source_position;
1720 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1726 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1730 construct_type_t *type;
1731 switch(token.type) {
1733 type = parse_function_declarator(declaration);
1736 type = parse_array_declarator();
1739 goto declarator_finished;
1750 declarator_finished:
1753 if(inner_types != NULL) {
1755 last->next = inner_types;
1757 result = inner_types;
1765 static type_t *construct_declarator_type(construct_type_t *construct_list,
1768 construct_type_t *iter = construct_list;
1769 for( ; iter != NULL; iter = iter->next) {
1770 parsed_pointer_t *parsed_pointer;
1771 parsed_array_t *parsed_array;
1772 construct_function_type_t *construct_function_type;
1773 function_type_t *function_type;
1774 pointer_type_t *pointer_type;
1775 array_type_t *array_type;
1777 switch(iter->type) {
1778 case CONSTRUCT_INVALID:
1779 panic("invalid type construction found");
1780 case CONSTRUCT_FUNCTION:
1781 construct_function_type = (construct_function_type_t*) iter;
1782 function_type = construct_function_type->function_type;
1784 function_type->result_type = type;
1785 type = (type_t*) function_type;
1788 case CONSTRUCT_POINTER:
1789 parsed_pointer = (parsed_pointer_t*) iter;
1790 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1792 pointer_type->type.type = TYPE_POINTER;
1793 pointer_type->points_to = type;
1794 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1795 type = (type_t*) pointer_type;
1798 case CONSTRUCT_ARRAY:
1799 parsed_array = (parsed_array_t*) iter;
1800 array_type = allocate_type_zero(sizeof(array_type[0]));
1802 array_type->type.type = TYPE_ARRAY;
1803 array_type->element_type = type;
1804 array_type->type.qualifiers = parsed_array->type_qualifiers;
1805 array_type->is_static = parsed_array->is_static;
1806 array_type->is_variable = parsed_array->is_variable;
1807 array_type->size = parsed_array->size;
1808 type = (type_t*) array_type;
1812 type_t *hashed_type = typehash_insert((type_t*) type);
1813 if(hashed_type != type) {
1822 static declaration_t *parse_declarator(
1823 const declaration_specifiers_t *specifiers,
1824 type_t *type, bool may_be_abstract)
1826 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1827 declaration->storage_class = specifiers->storage_class;
1828 declaration->is_inline = specifiers->is_inline;
1830 construct_type_t *construct_type
1831 = parse_inner_declarator(declaration, may_be_abstract);
1832 declaration->type = construct_declarator_type(construct_type, type);
1834 if(construct_type != NULL) {
1835 obstack_free(&temp_obst, construct_type);
1841 static type_t *parse_abstract_declarator(type_t *base_type)
1843 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1845 type_t *result = construct_declarator_type(construct_type, base_type);
1846 if(construct_type != NULL) {
1847 obstack_free(&temp_obst, construct_type);
1853 static declaration_t *record_declaration(declaration_t *declaration)
1855 assert(context != NULL);
1857 symbol_t *symbol = declaration->symbol;
1858 if(symbol != NULL) {
1859 declaration_t *alias = environment_push(declaration);
1860 if(alias != declaration)
1863 declaration->parent_context = context;
1866 if(last_declaration != NULL) {
1867 last_declaration->next = declaration;
1869 context->declarations = declaration;
1871 last_declaration = declaration;
1876 static void parser_error_multiple_definition(declaration_t *previous,
1877 declaration_t *declaration)
1879 parser_print_error_prefix_pos(declaration->source_position);
1880 fprintf(stderr, "multiple definition of symbol '%s'\n",
1881 declaration->symbol->string);
1882 parser_print_error_prefix_pos(previous->source_position);
1883 fprintf(stderr, "this is the location of the previous "
1887 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1890 declaration_t *ndeclaration
1891 = parse_declarator(specifiers, specifiers->type, false);
1893 declaration_t *declaration = record_declaration(ndeclaration);
1895 type_t *type = declaration->type;
1896 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
1897 parser_print_warning_prefix_pos(declaration->source_position);
1898 fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
1899 declaration->symbol->string);
1902 if(token.type == '=') {
1905 /* TODO: check that this is an allowed type (no function type) */
1907 if(declaration->init.initializer != NULL) {
1908 parser_error_multiple_definition(declaration, ndeclaration);
1911 ndeclaration->init.initializer = parse_initializer(declaration->type);
1912 } else if(token.type == '{') {
1913 if(declaration->type->type != TYPE_FUNCTION) {
1914 parser_print_error_prefix();
1915 fprintf(stderr, "Declarator ");
1916 print_type_ext(declaration->type, declaration->symbol, NULL);
1917 fprintf(stderr, " has a body but is not a function type.\n");
1922 if(declaration->init.statement != NULL) {
1923 parser_error_multiple_definition(declaration, ndeclaration);
1925 if(ndeclaration != declaration) {
1926 memcpy(&declaration->context, &ndeclaration->context,
1927 sizeof(declaration->context));
1930 int top = environment_top();
1931 context_t *last_context = context;
1932 set_context(&declaration->context);
1934 /* push function parameters */
1935 declaration_t *parameter = declaration->context.declarations;
1936 for( ; parameter != NULL; parameter = parameter->next) {
1937 environment_push(parameter);
1940 int label_stack_top = label_top();
1941 declaration_t *old_current_function = current_function;
1942 current_function = declaration;
1944 statement_t *statement = parse_compound_statement();
1946 assert(current_function == declaration);
1947 current_function = old_current_function;
1948 label_pop_to(label_stack_top);
1950 assert(context == &declaration->context);
1951 set_context(last_context);
1952 environment_pop_to(top);
1954 declaration->init.statement = statement;
1958 if(token.type != ',')
1965 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1968 if(token.type == ':') {
1970 parse_constant_expression();
1971 /* TODO (bitfields) */
1973 declaration_t *declaration
1974 = parse_declarator(specifiers, specifiers->type, true);
1976 /* TODO: check constraints for struct declarations */
1977 /* TODO: check for doubled fields */
1978 record_declaration(declaration);
1980 if(token.type == ':') {
1982 parse_constant_expression();
1983 /* TODO (bitfields) */
1987 if(token.type != ',')
1994 static void parse_compound_type_entries(void)
1998 while(token.type != '}' && token.type != T_EOF) {
1999 declaration_specifiers_t specifiers;
2000 memset(&specifiers, 0, sizeof(specifiers));
2001 parse_declaration_specifiers(&specifiers);
2003 parse_struct_declarators(&specifiers);
2005 if(token.type == T_EOF) {
2006 parse_error("unexpected error while parsing struct");
2011 static void parse_declaration(void)
2013 source_position_t source_position = token.source_position;
2015 declaration_specifiers_t specifiers;
2016 memset(&specifiers, 0, sizeof(specifiers));
2017 parse_declaration_specifiers(&specifiers);
2019 if(token.type == ';') {
2020 if (specifiers.storage_class != STORAGE_CLASS_NONE) {
2021 parse_warning_pos(source_position,
2022 "useless keyword in empty declaration");
2024 switch (specifiers.type->type) {
2025 case TYPE_COMPOUND_STRUCT:
2026 case TYPE_COMPOUND_UNION: {
2027 const compound_type_t *const comp_type =
2028 (const compound_type_t*)specifiers.type;
2029 if (comp_type->declaration->symbol == NULL) {
2030 parse_warning_pos(source_position,
2031 "unnamed struct/union that defines no instances");
2036 case TYPE_ENUM: break;
2039 parse_warning_pos(source_position, "empty declaration");
2045 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2047 declaration->type = specifiers.type;
2048 declaration->storage_class = specifiers.storage_class;
2049 declaration->source_position = source_position;
2050 record_declaration(declaration);
2053 parse_init_declarators(&specifiers);
2056 static type_t *parse_typename(void)
2058 declaration_specifiers_t specifiers;
2059 memset(&specifiers, 0, sizeof(specifiers));
2060 parse_declaration_specifiers(&specifiers);
2061 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2062 /* TODO: improve error message, user does probably not know what a
2063 * storage class is...
2065 parse_error("typename may not have a storage class");
2068 type_t *result = parse_abstract_declarator(specifiers.type);
2076 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2077 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2078 expression_t *left);
2080 typedef struct expression_parser_function_t expression_parser_function_t;
2081 struct expression_parser_function_t {
2082 unsigned precedence;
2083 parse_expression_function parser;
2084 unsigned infix_precedence;
2085 parse_expression_infix_function infix_parser;
2088 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2090 static expression_t *make_invalid_expression(void)
2092 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2093 expression->type = EXPR_INVALID;
2094 expression->source_position = token.source_position;
2098 static expression_t *expected_expression_error(void)
2100 parser_print_error_prefix();
2101 fprintf(stderr, "expected expression, got token ");
2102 print_token(stderr, & token);
2103 fprintf(stderr, "\n");
2107 return make_invalid_expression();
2110 static expression_t *parse_string_const(void)
2112 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2114 cnst->expression.type = EXPR_STRING_LITERAL;
2115 cnst->expression.datatype = type_string;
2116 cnst->value = parse_string_literals();
2118 return (expression_t*) cnst;
2121 static expression_t *parse_int_const(void)
2123 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2125 cnst->expression.type = EXPR_CONST;
2126 cnst->expression.datatype = type_int;
2127 cnst->v.int_value = token.v.intvalue;
2131 return (expression_t*) cnst;
2134 static expression_t *parse_float_const(void)
2136 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2138 cnst->expression.type = EXPR_CONST;
2139 cnst->expression.datatype = type_double;
2140 cnst->v.float_value = token.v.floatvalue;
2144 return (expression_t*) cnst;
2147 static declaration_t *create_implicit_function(symbol_t *symbol,
2148 const source_position_t source_position)
2150 function_type_t *function_type
2151 = allocate_type_zero(sizeof(function_type[0]));
2153 function_type->type.type = TYPE_FUNCTION;
2154 function_type->result_type = type_int;
2155 function_type->unspecified_parameters = true;
2157 type_t *type = typehash_insert((type_t*) function_type);
2158 if(type != (type_t*) function_type) {
2159 free_type(function_type);
2162 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2164 declaration->storage_class = STORAGE_CLASS_EXTERN;
2165 declaration->type = type;
2166 declaration->symbol = symbol;
2167 declaration->source_position = source_position;
2169 /* prepend the implicit definition to the global context
2170 * this is safe since the symbol wasn't declared as anything else yet
2172 assert(symbol->declaration == NULL);
2174 context_t *last_context = context;
2175 context = global_context;
2177 environment_push(declaration);
2178 declaration->next = context->declarations;
2179 context->declarations = declaration;
2181 context = last_context;
2186 static expression_t *parse_reference(void)
2188 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2190 ref->expression.type = EXPR_REFERENCE;
2191 ref->symbol = token.v.symbol;
2193 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2195 source_position_t source_position = token.source_position;
2198 if(declaration == NULL) {
2200 /* an implicitely defined function */
2201 if(token.type == '(') {
2202 parser_print_prefix_pos(token.source_position);
2203 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2204 ref->symbol->string);
2206 declaration = create_implicit_function(ref->symbol,
2211 parser_print_error_prefix();
2212 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2213 return (expression_t*) ref;
2217 ref->declaration = declaration;
2218 ref->expression.datatype = declaration->type;
2220 return (expression_t*) ref;
2223 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2227 /* TODO check if explicit cast is allowed and issue warnings/errors */
2230 static expression_t *parse_cast(void)
2232 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2234 cast->expression.type = EXPR_UNARY;
2235 cast->type = UNEXPR_CAST;
2236 cast->expression.source_position = token.source_position;
2238 type_t *type = parse_typename();
2241 expression_t *value = parse_sub_expression(20);
2243 check_cast_allowed(value, type);
2245 cast->expression.datatype = type;
2246 cast->value = value;
2248 return (expression_t*) cast;
2251 static expression_t *parse_statement_expression(void)
2253 statement_expression_t *expression
2254 = allocate_ast_zero(sizeof(expression[0]));
2255 expression->expression.type = EXPR_STATEMENT;
2257 statement_t *statement = parse_compound_statement();
2258 expression->statement = statement;
2259 if(statement == NULL) {
2264 assert(statement->type == STATEMENT_COMPOUND);
2265 compound_statement_t *compound_statement
2266 = (compound_statement_t*) statement;
2268 /* find last statement and use it's type */
2269 const statement_t *last_statement = NULL;
2270 const statement_t *iter = compound_statement->statements;
2271 for( ; iter != NULL; iter = iter->next) {
2272 last_statement = iter;
2275 if(last_statement->type == STATEMENT_EXPRESSION) {
2276 const expression_statement_t *expression_statement =
2277 (const expression_statement_t*) last_statement;
2278 expression->expression.datatype
2279 = expression_statement->expression->datatype;
2281 expression->expression.datatype = type_void;
2286 return (expression_t*) expression;
2289 static expression_t *parse_brace_expression(void)
2293 switch(token.type) {
2295 /* gcc extension: a stement expression */
2296 return parse_statement_expression();
2300 return parse_cast();
2302 if(is_typedef_symbol(token.v.symbol)) {
2303 return parse_cast();
2307 expression_t *result = parse_expression();
2313 static expression_t *parse_function_keyword(void)
2315 eat(T___FUNCTION__);
2318 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2319 expression->expression.type = EXPR_FUNCTION;
2320 expression->expression.datatype = type_string;
2321 expression->value = "TODO: FUNCTION";
2323 return (expression_t*) expression;
2326 static expression_t *parse_pretty_function_keyword(void)
2328 eat(T___PRETTY_FUNCTION__);
2331 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2332 expression->expression.type = EXPR_PRETTY_FUNCTION;
2333 expression->expression.datatype = type_string;
2334 expression->value = "TODO: PRETTY FUNCTION";
2336 return (expression_t*) expression;
2339 static designator_t *parse_designator(void)
2341 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2343 if(token.type != T_IDENTIFIER) {
2344 parse_error_expected("while parsing member designator",
2349 result->symbol = token.v.symbol;
2352 designator_t *last_designator = result;
2354 if(token.type == '.') {
2356 if(token.type != T_IDENTIFIER) {
2357 parse_error_expected("while parsing member designator",
2362 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2363 designator->symbol = token.v.symbol;
2366 last_designator->next = designator;
2367 last_designator = designator;
2370 if(token.type == '[') {
2372 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2373 designator->array_access = parse_expression();
2374 if(designator->array_access == NULL) {
2380 last_designator->next = designator;
2381 last_designator = designator;
2390 static expression_t *parse_offsetof(void)
2392 eat(T___builtin_offsetof);
2394 offsetof_expression_t *expression
2395 = allocate_ast_zero(sizeof(expression[0]));
2396 expression->expression.type = EXPR_OFFSETOF;
2397 expression->expression.datatype = type_size_t;
2400 expression->type = parse_typename();
2402 expression->designator = parse_designator();
2405 return (expression_t*) expression;
2408 static expression_t *parse_va_arg(void)
2410 eat(T___builtin_va_arg);
2412 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2413 expression->expression.type = EXPR_VA_ARG;
2416 expression->arg = parse_assignment_expression();
2418 expression->expression.datatype = parse_typename();
2421 return (expression_t*) expression;
2424 static expression_t *parse_builtin_symbol(void)
2426 builtin_symbol_expression_t *expression
2427 = allocate_ast_zero(sizeof(expression[0]));
2428 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2430 /* TODO: set datatype */
2432 expression->symbol = token.v.symbol;
2436 return (expression_t*) expression;
2439 static expression_t *parse_primary_expression(void)
2441 switch(token.type) {
2443 return parse_int_const();
2444 case T_FLOATINGPOINT:
2445 return parse_float_const();
2446 case T_STRING_LITERAL:
2447 return parse_string_const();
2449 return parse_reference();
2450 case T___FUNCTION__:
2451 return parse_function_keyword();
2452 case T___PRETTY_FUNCTION__:
2453 return parse_pretty_function_keyword();
2454 case T___builtin_offsetof:
2455 return parse_offsetof();
2456 case T___builtin_va_arg:
2457 return parse_va_arg();
2458 case T___builtin_expect:
2459 case T___builtin_va_start:
2460 case T___builtin_va_end:
2461 return parse_builtin_symbol();
2464 return parse_brace_expression();
2467 parser_print_error_prefix();
2468 fprintf(stderr, "unexpected token ");
2469 print_token(stderr, &token);
2470 fprintf(stderr, "\n");
2473 return make_invalid_expression();
2476 static expression_t *parse_array_expression(unsigned precedence,
2477 expression_t *array_ref)
2483 array_access_expression_t *array_access
2484 = allocate_ast_zero(sizeof(array_access[0]));
2486 array_access->expression.type = EXPR_ARRAY_ACCESS;
2487 array_access->array_ref = array_ref;
2488 array_access->index = parse_expression();
2490 type_t *type = array_ref->datatype;
2492 if(type->type == TYPE_POINTER) {
2493 pointer_type_t *pointer = (pointer_type_t*) type;
2494 array_access->expression.datatype = pointer->points_to;
2495 } else if(type->type == TYPE_ARRAY) {
2496 array_type_t *array_type = (array_type_t*) type;
2497 array_access->expression.datatype = array_type->element_type;
2499 parser_print_error_prefix();
2500 fprintf(stderr, "array access on object with non-pointer type ");
2501 print_type_quoted(type);
2502 fprintf(stderr, "\n");
2506 if(token.type != ']') {
2507 parse_error_expected("Problem while parsing array access", ']', 0);
2508 return (expression_t*) array_access;
2512 return (expression_t*) array_access;
2515 static bool is_declaration_specifier(const token_t *token,
2516 bool only_type_specifiers)
2518 switch(token->type) {
2522 return is_typedef_symbol(token->v.symbol);
2525 if(only_type_specifiers)
2534 static expression_t *parse_sizeof(unsigned precedence)
2538 sizeof_expression_t *sizeof_expression
2539 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2540 sizeof_expression->expression.type = EXPR_SIZEOF;
2541 sizeof_expression->expression.datatype = type_size_t;
2543 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2545 sizeof_expression->type = parse_typename();
2548 expression_t *expression = parse_sub_expression(precedence);
2549 sizeof_expression->type = expression->datatype;
2550 sizeof_expression->size_expression = expression;
2553 return (expression_t*) sizeof_expression;
2556 static expression_t *parse_select_expression(unsigned precedence,
2557 expression_t *compound)
2560 assert(token.type == '.' || token.type == T_MINUSGREATER);
2562 bool is_pointer = (token.type == T_MINUSGREATER);
2565 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2567 select->expression.type = EXPR_SELECT;
2568 select->compound = compound;
2570 if(token.type != T_IDENTIFIER) {
2571 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2572 return (expression_t*) select;
2574 symbol_t *symbol = token.v.symbol;
2575 select->symbol = symbol;
2578 type_t *type = compound->datatype;
2580 return make_invalid_expression();
2582 type_t *type_left = type;
2584 if(type->type != TYPE_POINTER) {
2585 parser_print_error_prefix();
2586 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2587 print_type_quoted(type);
2588 fputc('\n', stderr);
2589 return make_invalid_expression();
2591 pointer_type_t *pointer_type = (pointer_type_t*) type;
2592 type_left = pointer_type->points_to;
2594 type_left = skip_typeref(type_left);
2596 if(type_left->type != TYPE_COMPOUND_STRUCT
2597 && type_left->type != TYPE_COMPOUND_UNION) {
2598 parser_print_error_prefix();
2599 fprintf(stderr, "request for member '%s' in something not a struct or "
2600 "union, but ", symbol->string);
2601 print_type_quoted(type_left);
2602 fputc('\n', stderr);
2603 return make_invalid_expression();
2606 compound_type_t *compound_type = (compound_type_t*) type_left;
2607 declaration_t *declaration = compound_type->declaration;
2609 if(!declaration->init.is_defined) {
2610 parser_print_error_prefix();
2611 fprintf(stderr, "request for member '%s' of incomplete type ",
2613 print_type_quoted(type_left);
2614 fputc('\n', stderr);
2615 return make_invalid_expression();
2618 declaration_t *iter = declaration->context.declarations;
2619 for( ; iter != NULL; iter = iter->next) {
2620 if(iter->symbol == symbol) {
2625 parser_print_error_prefix();
2626 print_type_quoted(type_left);
2627 fprintf(stderr, " has no memeber named '%s'\n", symbol->string);
2628 return make_invalid_expression();
2631 select->compound_entry = iter;
2632 select->expression.datatype = iter->type;
2633 return (expression_t*) select;
2636 static expression_t *parse_call_expression(unsigned precedence,
2637 expression_t *expression)
2640 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2641 call->expression.type = EXPR_CALL;
2642 call->function = expression;
2644 function_type_t *function_type;
2645 type_t *type = expression->datatype;
2646 if (type->type == TYPE_FUNCTION) {
2647 function_type = (function_type_t*) type;
2648 call->expression.datatype = function_type->result_type;
2649 } else if (type->type == TYPE_POINTER &&
2650 ((pointer_type_t*)type)->points_to->type == TYPE_FUNCTION) {
2651 pointer_type_t *const ptr_type = (pointer_type_t*)type;
2652 function_type = (function_type_t*)ptr_type->points_to;
2653 call->expression.datatype = function_type->result_type;
2655 parser_print_error_prefix();
2656 fputs("called object '", stderr);
2657 print_expression(expression);
2658 fputs("' (type ", stderr);
2659 print_type_quoted(type);
2660 fputs(") is not a function\n", stderr);
2662 function_type = NULL;
2663 call->expression.datatype = NULL;
2666 /* parse arguments */
2669 if(token.type != ')') {
2670 call_argument_t *last_argument = NULL;
2673 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2675 argument->expression = parse_assignment_expression();
2676 if(last_argument == NULL) {
2677 call->arguments = argument;
2679 last_argument->next = argument;
2681 last_argument = argument;
2683 if(token.type != ',')
2690 if(function_type != NULL) {
2691 function_parameter_t *parameter = function_type->parameters;
2692 call_argument_t *argument = call->arguments;
2693 for( ; parameter != NULL && argument != NULL;
2694 parameter = parameter->next, argument = argument->next) {
2695 type_t *expected_type = parameter->type;
2696 /* TODO report context in error messages */
2697 argument->expression = create_implicit_cast(argument->expression,
2700 /* too few parameters */
2701 if(parameter != NULL) {
2702 parser_print_error_prefix();
2703 fprintf(stderr, "too few arguments to function '");
2704 print_expression(expression);
2705 fprintf(stderr, "'\n");
2706 } else if(argument != NULL) {
2707 /* too many parameters */
2708 if(!function_type->variadic
2709 && !function_type->unspecified_parameters) {
2710 parser_print_error_prefix();
2711 fprintf(stderr, "too many arguments to function '");
2712 print_expression(expression);
2713 fprintf(stderr, "'\n");
2715 /* do default promotion */
2716 for( ; argument != NULL; argument = argument->next) {
2717 type_t *type = argument->expression->datatype;
2722 if(is_type_integer(type)) {
2723 type = promote_integer(type);
2724 } else if(type == type_float) {
2727 argument->expression
2728 = create_implicit_cast(argument->expression, type);
2734 return (expression_t*) call;
2737 static type_t *get_type_after_conversion(const type_t *type1,
2738 const type_t *type2)
2742 return (type_t*) type1;
2745 static expression_t *parse_conditional_expression(unsigned precedence,
2746 expression_t *expression)
2750 conditional_expression_t *conditional
2751 = allocate_ast_zero(sizeof(conditional[0]));
2752 conditional->expression.type = EXPR_CONDITIONAL;
2753 conditional->condition = expression;
2756 type_t *condition_type = conditional->condition->datatype;
2757 if(condition_type != NULL) {
2758 if(!is_type_scalar(skip_typeref(condition_type))) {
2759 type_error("expected a scalar type", expression->source_position,
2764 conditional->true_expression = parse_expression();
2766 conditional->false_expression = parse_sub_expression(precedence);
2768 type_t *true_type = conditional->true_expression->datatype;
2769 if(true_type == NULL)
2770 return (expression_t*) conditional;
2771 type_t *false_type = conditional->false_expression->datatype;
2772 if(false_type == NULL)
2773 return (expression_t*) conditional;
2775 type_t *const skipped_true_type = skip_typeref(true_type);
2776 type_t *const skipped_false_type = skip_typeref(false_type);
2779 if (skipped_true_type == skipped_false_type) {
2780 conditional->expression.datatype = skipped_true_type;
2781 } else if (is_type_arithmetic(skipped_true_type) &&
2782 is_type_arithmetic(skipped_false_type)) {
2783 type_t *const result = get_type_after_conversion(skipped_true_type,
2784 skipped_false_type);
2785 /* TODO: create implicit convs if necessary */
2786 conditional->expression.datatype = result;
2787 } else if (skipped_true_type->type == TYPE_POINTER &&
2788 skipped_false_type->type == TYPE_POINTER &&
2789 true /* TODO compatible points_to types */) {
2791 } else if(/* (is_null_ptr_const(skipped_true_type) &&
2792 skipped_false_type->type == TYPE_POINTER)
2793 || (is_null_ptr_const(skipped_false_type) &&
2794 skipped_true_type->type == TYPE_POINTER) TODO*/ false) {
2796 } else if(/* 1 is pointer to object type, other is void* */ false) {
2799 type_error_incompatible("while parsing conditional",
2800 expression->source_position, true_type,
2801 skipped_false_type);
2804 return (expression_t*) conditional;
2807 static expression_t *parse_extension(unsigned precedence)
2809 eat(T___extension__);
2811 /* TODO enable extensions */
2813 return parse_sub_expression(precedence);
2816 static void semantic_incdec(unary_expression_t *expression)
2818 type_t *orig_type = expression->value->datatype;
2819 if(orig_type == NULL)
2822 type_t *type = skip_typeref(orig_type);
2823 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2824 /* TODO: improve error message */
2825 parser_print_error_prefix();
2826 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2830 expression->expression.datatype = orig_type;
2833 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2835 type_t *orig_type = expression->value->datatype;
2836 if(orig_type == NULL)
2839 type_t *type = skip_typeref(orig_type);
2840 if(!is_type_arithmetic(type)) {
2841 /* TODO: improve error message */
2842 parser_print_error_prefix();
2843 fprintf(stderr, "operation needs an arithmetic type\n");
2847 expression->expression.datatype = orig_type;
2850 static void semantic_dereference(unary_expression_t *expression)
2852 type_t *orig_type = expression->value->datatype;
2853 if(orig_type == NULL)
2856 type_t *type = skip_typeref(orig_type);
2857 switch (type->type) {
2859 array_type_t *const array_type = (array_type_t*)type;
2860 expression->expression.datatype = array_type->element_type;
2864 case TYPE_POINTER: {
2865 pointer_type_t *pointer_type = (pointer_type_t*)type;
2866 expression->expression.datatype = pointer_type->points_to;
2871 parser_print_error_prefix();
2872 fputs("'Unary *' needs pointer or arrray type, but type ", stderr);
2873 print_type_quoted(orig_type);
2874 fputs(" given.\n", stderr);
2879 static void semantic_take_addr(unary_expression_t *expression)
2881 type_t *orig_type = expression->value->datatype;
2882 if(orig_type == NULL)
2885 expression_t *value = expression->value;
2886 if(value->type == EXPR_REFERENCE) {
2887 reference_expression_t *reference = (reference_expression_t*) value;
2888 declaration_t *declaration = reference->declaration;
2889 if(declaration != NULL) {
2890 declaration->address_taken = 1;
2894 expression->expression.datatype = make_pointer_type(orig_type, 0);
2897 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2898 static expression_t *parse_##unexpression_type(unsigned precedence) \
2902 unary_expression_t *unary_expression \
2903 = allocate_ast_zero(sizeof(unary_expression[0])); \
2904 unary_expression->expression.type = EXPR_UNARY; \
2905 unary_expression->type = unexpression_type; \
2906 unary_expression->value = parse_sub_expression(precedence); \
2908 sfunc(unary_expression); \
2910 return (expression_t*) unary_expression; \
2913 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2914 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2915 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2916 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2917 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2918 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2919 semantic_unexpr_arithmetic)
2920 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2922 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2925 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2927 static expression_t *parse_##unexpression_type(unsigned precedence, \
2928 expression_t *left) \
2930 (void) precedence; \
2933 unary_expression_t *unary_expression \
2934 = allocate_ast_zero(sizeof(unary_expression[0])); \
2935 unary_expression->expression.type = EXPR_UNARY; \
2936 unary_expression->type = unexpression_type; \
2937 unary_expression->value = left; \
2939 sfunc(unary_expression); \
2941 return (expression_t*) unary_expression; \
2944 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2946 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2949 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2951 /* TODO: handle complex + imaginary types */
2953 /* § 6.3.1.8 Usual arithmetic conversions */
2954 if(type_left == type_long_double || type_right == type_long_double) {
2955 return type_long_double;
2956 } else if(type_left == type_double || type_right == type_double) {
2958 } else if(type_left == type_float || type_right == type_float) {
2962 type_right = promote_integer(type_right);
2963 type_left = promote_integer(type_left);
2965 if(type_left == type_right)
2968 bool signed_left = is_type_signed(type_left);
2969 bool signed_right = is_type_signed(type_right);
2970 if(get_rank(type_left) < get_rank(type_right)) {
2971 if(signed_left == signed_right || !signed_right) {
2977 if(signed_left == signed_right || !signed_left) {
2985 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2987 expression_t *left = expression->left;
2988 expression_t *right = expression->right;
2989 type_t *orig_type_left = left->datatype;
2990 type_t *orig_type_right = right->datatype;
2992 if(orig_type_left == NULL || orig_type_right == NULL)
2995 type_t *type_left = skip_typeref(orig_type_left);
2996 type_t *type_right = skip_typeref(orig_type_right);
2998 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2999 /* TODO: improve error message */
3000 parser_print_error_prefix();
3001 fprintf(stderr, "operation needs arithmetic types\n");
3005 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3006 expression->left = create_implicit_cast(left, arithmetic_type);
3007 expression->right = create_implicit_cast(right, arithmetic_type);
3008 expression->expression.datatype = arithmetic_type;
3011 static void semantic_shift_op(binary_expression_t *expression)
3013 expression_t *left = expression->left;
3014 expression_t *right = expression->right;
3015 type_t *orig_type_left = left->datatype;
3016 type_t *orig_type_right = right->datatype;
3018 if(orig_type_left == NULL || orig_type_right == NULL)
3021 type_t *type_left = skip_typeref(orig_type_left);
3022 type_t *type_right = skip_typeref(orig_type_right);
3024 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
3025 /* TODO: improve error message */
3026 parser_print_error_prefix();
3027 fprintf(stderr, "operation needs integer types\n");
3031 type_left = promote_integer(type_left);
3032 type_right = promote_integer(type_right);
3034 expression->left = create_implicit_cast(left, type_left);
3035 expression->right = create_implicit_cast(right, type_right);
3036 expression->expression.datatype = type_left;
3039 static void semantic_add(binary_expression_t *expression)
3041 expression_t *left = expression->left;
3042 expression_t *right = expression->right;
3043 type_t *orig_type_left = left->datatype;
3044 type_t *orig_type_right = right->datatype;
3046 if(orig_type_left == NULL || orig_type_right == NULL)
3049 type_t *type_left = skip_typeref(orig_type_left);
3050 type_t *type_right = skip_typeref(orig_type_right);
3053 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3054 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3055 expression->left = create_implicit_cast(left, arithmetic_type);
3056 expression->right = create_implicit_cast(right, arithmetic_type);
3057 expression->expression.datatype = arithmetic_type;
3059 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3060 expression->expression.datatype = type_left;
3061 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
3062 expression->expression.datatype = type_right;
3064 parser_print_error_prefix();
3065 fprintf(stderr, "invalid operands to binary + (");
3066 print_type_quoted(orig_type_left);
3067 fprintf(stderr, ", ");
3068 print_type_quoted(orig_type_right);
3069 fprintf(stderr, ")\n");
3073 static void semantic_sub(binary_expression_t *expression)
3075 expression_t *left = expression->left;
3076 expression_t *right = expression->right;
3077 type_t *orig_type_left = left->datatype;
3078 type_t *orig_type_right = right->datatype;
3080 if(orig_type_left == NULL || orig_type_right == NULL)
3083 type_t *type_left = skip_typeref(orig_type_left);
3084 type_t *type_right = skip_typeref(orig_type_right);
3087 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3088 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3089 expression->left = create_implicit_cast(left, arithmetic_type);
3090 expression->right = create_implicit_cast(right, arithmetic_type);
3091 expression->expression.datatype = arithmetic_type;
3093 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3094 expression->expression.datatype = type_left;
3095 } else if(type_left->type == TYPE_POINTER &&
3096 type_right->type == TYPE_POINTER) {
3097 if(!pointers_compatible(type_left, type_right)) {
3098 parser_print_error_prefix();
3099 fprintf(stderr, "pointers to incompatible objects to binary - (");
3100 print_type_quoted(orig_type_left);
3101 fprintf(stderr, ", ");
3102 print_type_quoted(orig_type_right);
3103 fprintf(stderr, ")\n");
3105 expression->expression.datatype = type_ptrdiff_t;
3108 parser_print_error_prefix();
3109 fprintf(stderr, "invalid operands to binary - (");
3110 print_type_quoted(orig_type_left);
3111 fprintf(stderr, ", ");
3112 print_type_quoted(orig_type_right);
3113 fprintf(stderr, ")\n");
3117 static void semantic_comparison(binary_expression_t *expression)
3119 expression_t *left = expression->left;
3120 expression_t *right = expression->right;
3121 type_t *orig_type_left = left->datatype;
3122 type_t *orig_type_right = right->datatype;
3124 if(orig_type_left == NULL || orig_type_right == NULL)
3127 type_t *type_left = skip_typeref(orig_type_left);
3128 type_t *type_right = skip_typeref(orig_type_right);
3130 /* TODO non-arithmetic types */
3131 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3132 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3133 expression->left = create_implicit_cast(left, arithmetic_type);
3134 expression->right = create_implicit_cast(right, arithmetic_type);
3135 expression->expression.datatype = arithmetic_type;
3136 } else if (type_left->type == TYPE_POINTER &&
3137 type_right->type == TYPE_POINTER) {
3138 /* TODO check compatibility */
3139 } else if (type_left->type == TYPE_POINTER) {
3140 expression->right = create_implicit_cast(right, type_left);
3141 } else if (type_right->type == TYPE_POINTER) {
3142 expression->left = create_implicit_cast(left, type_right);
3144 type_error_incompatible("invalid operands in comparison",
3145 expression->expression.source_position,
3146 type_left, type_right);
3148 expression->expression.datatype = type_int;
3151 static void semantic_arithmetic_assign(binary_expression_t *expression)
3153 expression_t *left = expression->left;
3154 expression_t *right = expression->right;
3155 type_t *orig_type_left = left->datatype;
3156 type_t *orig_type_right = right->datatype;
3158 if(orig_type_left == NULL || orig_type_right == NULL)
3161 type_t *type_left = skip_typeref(orig_type_left);
3162 type_t *type_right = skip_typeref(orig_type_right);
3164 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3165 /* TODO: improve error message */
3166 parser_print_error_prefix();
3167 fprintf(stderr, "operation needs arithmetic types\n");
3171 /* combined instructions are tricky. We can't create an implicit cast on
3172 * the left side, because we need the uncasted form for the store.
3173 * The ast2firm pass has to know that left_type must be right_type
3174 * for the arithmeitc operation and create a cast by itself */
3175 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3176 expression->right = create_implicit_cast(right, arithmetic_type);
3177 expression->expression.datatype = type_left;
3180 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
3182 expression_t *left = expression->left;
3183 expression_t *right = expression->right;
3184 type_t *orig_type_left = left->datatype;
3185 type_t *orig_type_right = right->datatype;
3187 if(orig_type_left == NULL || orig_type_right == NULL)
3190 type_t *type_left = skip_typeref(orig_type_left);
3191 type_t *type_right = skip_typeref(orig_type_right);
3193 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3194 /* combined instructions are tricky. We can't create an implicit cast on
3195 * the left side, because we need the uncasted form for the store.
3196 * The ast2firm pass has to know that left_type must be right_type
3197 * for the arithmeitc operation and create a cast by itself */
3198 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
3199 expression->right = create_implicit_cast(right, arithmetic_type);
3200 expression->expression.datatype = type_left;
3201 } else if (type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
3202 expression->expression.datatype = type_left;
3204 parser_print_error_prefix();
3205 fputs("Incompatible types ", stderr);
3206 print_type_quoted(orig_type_left);
3207 fputs(" and ", stderr);
3208 print_type_quoted(orig_type_right);
3209 fputs(" in assignment\n", stderr);
3214 static void semantic_logical_op(binary_expression_t *expression)
3216 expression_t *left = expression->left;
3217 expression_t *right = expression->right;
3218 type_t *orig_type_left = left->datatype;
3219 type_t *orig_type_right = right->datatype;
3221 if(orig_type_left == NULL || orig_type_right == NULL)
3224 type_t *type_left = skip_typeref(orig_type_left);
3225 type_t *type_right = skip_typeref(orig_type_right);
3227 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
3228 /* TODO: improve error message */
3229 parser_print_error_prefix();
3230 fprintf(stderr, "operation needs scalar types\n");
3234 expression->expression.datatype = type_int;
3237 static void semantic_binexpr_assign(binary_expression_t *expression)
3239 expression_t *left = expression->left;
3240 type_t *type_left = left->datatype;
3242 if (type_left->type == TYPE_ARRAY) {
3243 parse_error("Cannot assign to arrays.");
3244 } else if (type_left != NULL) {
3245 semantic_assign(type_left, &expression->right, "assignment");
3248 expression->expression.datatype = type_left;
3251 static void semantic_comma(binary_expression_t *expression)
3253 expression->expression.datatype = expression->right->datatype;
3256 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3257 static expression_t *parse_##binexpression_type(unsigned precedence, \
3258 expression_t *left) \
3262 expression_t *right = parse_sub_expression(precedence + lr); \
3264 binary_expression_t *binexpr \
3265 = allocate_ast_zero(sizeof(binexpr[0])); \
3266 binexpr->expression.type = EXPR_BINARY; \
3267 binexpr->type = binexpression_type; \
3268 binexpr->left = left; \
3269 binexpr->right = right; \
3272 return (expression_t*) binexpr; \
3275 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3276 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3277 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3278 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3279 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3280 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3281 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3282 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3283 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3284 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3285 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3286 semantic_comparison, 1)
3287 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3288 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3289 semantic_comparison, 1)
3290 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3291 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3292 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3293 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3294 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3295 /* TODO shift has a bit special semantic */
3296 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3297 semantic_shift_op, 1)
3298 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3299 semantic_shift_op, 1)
3300 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3301 semantic_arithmetic_addsubb_assign, 0)
3302 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3303 semantic_arithmetic_addsubb_assign, 0)
3304 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3305 semantic_arithmetic_assign, 0)
3306 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3307 semantic_arithmetic_assign, 0)
3308 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3309 semantic_arithmetic_assign, 0)
3310 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3311 semantic_arithmetic_assign, 0)
3312 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3313 semantic_arithmetic_assign, 0)
3314 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3315 semantic_arithmetic_assign, 0)
3316 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3317 semantic_arithmetic_assign, 0)
3318 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3319 semantic_arithmetic_assign, 0)
3321 static expression_t *parse_sub_expression(unsigned precedence)
3323 if(token.type < 0) {
3324 return expected_expression_error();
3327 expression_parser_function_t *parser
3328 = &expression_parsers[token.type];
3329 source_position_t source_position = token.source_position;
3332 if(parser->parser != NULL) {
3333 left = parser->parser(parser->precedence);
3335 left = parse_primary_expression();
3337 assert(left != NULL);
3338 left->source_position = source_position;
3341 if(token.type < 0) {
3342 return expected_expression_error();
3345 parser = &expression_parsers[token.type];
3346 if(parser->infix_parser == NULL)
3348 if(parser->infix_precedence < precedence)
3351 left = parser->infix_parser(parser->infix_precedence, left);
3353 assert(left != NULL);
3354 assert(left->type != EXPR_UNKNOWN);
3355 left->source_position = source_position;
3361 static expression_t *parse_expression(void)
3363 return parse_sub_expression(1);
3368 static void register_expression_parser(parse_expression_function parser,
3369 int token_type, unsigned precedence)
3371 expression_parser_function_t *entry = &expression_parsers[token_type];
3373 if(entry->parser != NULL) {
3374 fprintf(stderr, "for token ");
3375 print_token_type(stderr, token_type);
3376 fprintf(stderr, "\n");
3377 panic("trying to register multiple expression parsers for a token");
3379 entry->parser = parser;
3380 entry->precedence = precedence;
3383 static void register_expression_infix_parser(
3384 parse_expression_infix_function parser, int token_type,
3385 unsigned precedence)
3387 expression_parser_function_t *entry = &expression_parsers[token_type];
3389 if(entry->infix_parser != NULL) {
3390 fprintf(stderr, "for token ");
3391 print_token_type(stderr, token_type);
3392 fprintf(stderr, "\n");
3393 panic("trying to register multiple infix expression parsers for a "
3396 entry->infix_parser = parser;
3397 entry->infix_precedence = precedence;
3400 static void init_expression_parsers(void)
3402 memset(&expression_parsers, 0, sizeof(expression_parsers));
3404 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3405 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3406 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3407 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3408 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3409 T_GREATERGREATER, 16);
3410 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3411 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3412 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3413 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3414 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3415 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3416 T_GREATEREQUAL, 14);
3417 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3418 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3419 T_EXCLAMATIONMARKEQUAL, 13);
3420 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3421 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3422 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3423 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3424 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3425 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3426 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3427 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3428 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3429 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3430 T_ASTERISKEQUAL, 2);
3431 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3432 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3434 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3435 T_LESSLESSEQUAL, 2);
3436 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3437 T_GREATERGREATEREQUAL, 2);
3438 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3440 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3442 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3445 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3447 register_expression_infix_parser(parse_array_expression, '[', 30);
3448 register_expression_infix_parser(parse_call_expression, '(', 30);
3449 register_expression_infix_parser(parse_select_expression, '.', 30);
3450 register_expression_infix_parser(parse_select_expression,
3451 T_MINUSGREATER, 30);
3452 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3454 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3457 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3458 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3459 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3460 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3461 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3462 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3463 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3464 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3465 register_expression_parser(parse_sizeof, T_sizeof, 25);
3466 register_expression_parser(parse_extension, T___extension__, 25);
3470 static statement_t *parse_case_statement(void)
3473 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3474 label->statement.type = STATEMENT_CASE_LABEL;
3475 label->statement.source_position = token.source_position;
3477 label->expression = parse_expression();
3480 label->statement.next = parse_statement();
3482 return (statement_t*) label;
3485 static statement_t *parse_default_statement(void)
3489 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3490 label->statement.type = STATEMENT_CASE_LABEL;
3491 label->statement.source_position = token.source_position;
3494 label->statement.next = parse_statement();
3496 return (statement_t*) label;
3499 static declaration_t *get_label(symbol_t *symbol)
3501 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3502 assert(current_function != NULL);
3503 /* if we found a label in the same function, then we already created the
3505 if(candidate != NULL
3506 && candidate->parent_context == ¤t_function->context) {
3510 /* otherwise we need to create a new one */
3511 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3512 declaration->namespace = NAMESPACE_LABEL;
3513 declaration->symbol = symbol;
3515 label_push(declaration);
3520 static statement_t *parse_label_statement(void)
3522 assert(token.type == T_IDENTIFIER);
3523 symbol_t *symbol = token.v.symbol;
3526 declaration_t *label = get_label(symbol);
3528 /* if source position is already set then the label is defined twice,
3529 * otherwise it was just mentioned in a goto so far */
3530 if(label->source_position.input_name != NULL) {
3531 parser_print_error_prefix();
3532 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3533 parser_print_error_prefix_pos(label->source_position);
3534 fprintf(stderr, "previous definition of '%s' was here\n",
3537 label->source_position = token.source_position;
3540 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3542 label_statement->statement.type = STATEMENT_LABEL;
3543 label_statement->statement.source_position = token.source_position;
3544 label_statement->label = label;
3548 if(token.type == '}') {
3549 parse_error("label at end of compound statement");
3550 return (statement_t*) label_statement;
3552 label_statement->label_statement = parse_statement();
3555 return (statement_t*) label_statement;
3558 static statement_t *parse_if(void)
3562 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3563 statement->statement.type = STATEMENT_IF;
3564 statement->statement.source_position = token.source_position;
3567 statement->condition = parse_expression();
3570 statement->true_statement = parse_statement();
3571 if(token.type == T_else) {
3573 statement->false_statement = parse_statement();
3576 return (statement_t*) statement;
3579 static statement_t *parse_switch(void)
3583 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3584 statement->statement.type = STATEMENT_SWITCH;
3585 statement->statement.source_position = token.source_position;
3588 statement->expression = parse_expression();
3590 statement->body = parse_statement();
3592 return (statement_t*) statement;
3595 static statement_t *parse_while(void)
3599 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3600 statement->statement.type = STATEMENT_WHILE;
3601 statement->statement.source_position = token.source_position;
3604 statement->condition = parse_expression();
3606 statement->body = parse_statement();
3608 return (statement_t*) statement;
3611 static statement_t *parse_do(void)
3615 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3616 statement->statement.type = STATEMENT_DO_WHILE;
3617 statement->statement.source_position = token.source_position;
3619 statement->body = parse_statement();
3622 statement->condition = parse_expression();
3626 return (statement_t*) statement;
3629 static statement_t *parse_for(void)
3633 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3634 statement->statement.type = STATEMENT_FOR;
3635 statement->statement.source_position = token.source_position;
3639 int top = environment_top();
3640 context_t *last_context = context;
3641 set_context(&statement->context);
3643 if(token.type != ';') {
3644 if(is_declaration_specifier(&token, false)) {
3645 parse_declaration();
3647 statement->initialisation = parse_expression();
3654 if(token.type != ';') {
3655 statement->condition = parse_expression();
3658 if(token.type != ')') {
3659 statement->step = parse_expression();
3662 statement->body = parse_statement();
3664 assert(context == &statement->context);
3665 set_context(last_context);
3666 environment_pop_to(top);
3668 return (statement_t*) statement;
3671 static statement_t *parse_goto(void)
3675 if(token.type != T_IDENTIFIER) {
3676 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3680 symbol_t *symbol = token.v.symbol;
3683 declaration_t *label = get_label(symbol);
3685 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3687 statement->statement.type = STATEMENT_GOTO;
3688 statement->statement.source_position = token.source_position;
3690 statement->label = label;
3694 return (statement_t*) statement;
3697 static statement_t *parse_continue(void)
3702 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3703 statement->type = STATEMENT_CONTINUE;
3704 statement->source_position = token.source_position;
3709 static statement_t *parse_break(void)
3714 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3715 statement->type = STATEMENT_BREAK;
3716 statement->source_position = token.source_position;
3721 static statement_t *parse_return(void)
3725 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3727 statement->statement.type = STATEMENT_RETURN;
3728 statement->statement.source_position = token.source_position;
3730 assert(current_function->type->type == TYPE_FUNCTION);
3731 function_type_t *function_type = (function_type_t*) current_function->type;
3732 type_t *return_type = function_type->result_type;
3734 expression_t *return_value;
3735 if(token.type != ';') {
3736 return_value = parse_expression();
3738 if(return_type == type_void && return_value->datatype != type_void) {
3739 parse_warning("'return' with a value, in function returning void");
3740 return_value = NULL;
3742 if(return_type != NULL) {
3743 semantic_assign(return_type, &return_value, "'return'");
3747 return_value = NULL;
3748 if(return_type != type_void) {
3749 parse_warning("'return' without value, in function returning "
3753 statement->return_value = return_value;
3757 return (statement_t*) statement;
3760 static statement_t *parse_declaration_statement(void)
3762 declaration_t *before = last_declaration;
3764 declaration_statement_t *statement
3765 = allocate_ast_zero(sizeof(statement[0]));
3766 statement->statement.type = STATEMENT_DECLARATION;
3767 statement->statement.source_position = token.source_position;
3769 declaration_specifiers_t specifiers;
3770 memset(&specifiers, 0, sizeof(specifiers));
3771 parse_declaration_specifiers(&specifiers);
3773 if(token.type == ';') {
3776 parse_init_declarators(&specifiers);
3779 if(before == NULL) {
3780 statement->declarations_begin = context->declarations;
3782 statement->declarations_begin = before->next;
3784 statement->declarations_end = last_declaration;
3786 return (statement_t*) statement;
3789 static statement_t *parse_expression_statement(void)
3791 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3792 statement->statement.type = STATEMENT_EXPRESSION;
3793 statement->statement.source_position = token.source_position;
3795 statement->expression = parse_expression();
3799 return (statement_t*) statement;
3802 static statement_t *parse_statement(void)
3804 statement_t *statement = NULL;
3806 /* declaration or statement */
3807 switch(token.type) {
3809 statement = parse_case_statement();
3813 statement = parse_default_statement();
3817 statement = parse_compound_statement();
3821 statement = parse_if();
3825 statement = parse_switch();
3829 statement = parse_while();
3833 statement = parse_do();
3837 statement = parse_for();
3841 statement = parse_goto();
3845 statement = parse_continue();
3849 statement = parse_break();
3853 statement = parse_return();
3862 if(look_ahead(1)->type == ':') {
3863 statement = parse_label_statement();
3867 if(is_typedef_symbol(token.v.symbol)) {
3868 statement = parse_declaration_statement();
3872 statement = parse_expression_statement();
3875 case T___extension__:
3876 /* this can be a prefix to a declaration or an expression statement */
3877 /* we simply eat it now and parse the rest with tail recursion */
3880 } while(token.type == T___extension__);
3881 statement = parse_statement();
3885 statement = parse_declaration_statement();
3889 statement = parse_expression_statement();
3893 assert(statement == NULL || statement->source_position.input_name != NULL);
3898 static statement_t *parse_compound_statement(void)
3900 compound_statement_t *compound_statement
3901 = allocate_ast_zero(sizeof(compound_statement[0]));
3902 compound_statement->statement.type = STATEMENT_COMPOUND;
3903 compound_statement->statement.source_position = token.source_position;
3907 int top = environment_top();
3908 context_t *last_context = context;
3909 set_context(&compound_statement->context);
3911 statement_t *last_statement = NULL;
3913 while(token.type != '}' && token.type != T_EOF) {
3914 statement_t *statement = parse_statement();
3915 if(statement == NULL)
3918 if(last_statement != NULL) {
3919 last_statement->next = statement;
3921 compound_statement->statements = statement;
3924 while(statement->next != NULL)
3925 statement = statement->next;
3927 last_statement = statement;
3930 if(token.type != '}') {
3931 parser_print_error_prefix_pos(
3932 compound_statement->statement.source_position);
3933 fprintf(stderr, "end of file while looking for closing '}'\n");
3937 assert(context == &compound_statement->context);
3938 set_context(last_context);
3939 environment_pop_to(top);
3941 return (statement_t*) compound_statement;
3944 static translation_unit_t *parse_translation_unit(void)
3946 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3948 assert(global_context == NULL);
3949 global_context = &unit->context;
3951 assert(context == NULL);
3952 set_context(&unit->context);
3954 while(token.type != T_EOF) {
3955 parse_declaration();
3958 assert(context == &unit->context);
3960 last_declaration = NULL;
3962 assert(global_context == &unit->context);
3963 global_context = NULL;
3968 translation_unit_t *parse(void)
3970 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3971 label_stack = NEW_ARR_F(stack_entry_t, 0);
3972 found_error = false;
3974 type_set_output(stderr);
3975 ast_set_output(stderr);
3977 lookahead_bufpos = 0;
3978 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3981 translation_unit_t *unit = parse_translation_unit();
3983 DEL_ARR_F(environment_stack);
3984 DEL_ARR_F(label_stack);
3992 void init_parser(void)
3994 init_expression_parsers();
3995 obstack_init(&temp_obst);
3997 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3998 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3999 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
4000 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
4001 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
4002 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
4003 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
4004 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
4005 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
4006 type_string = make_pointer_type(type_const_char, 0);
4009 void exit_parser(void)
4011 obstack_free(&temp_obst, NULL);