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(const char *message)
213 parser_print_prefix_pos(token.source_position);
214 fprintf(stderr, "warning: %s\n", message);
217 static void parse_error_expected(const char *message, ...)
222 if(message != NULL) {
223 parser_print_error_prefix();
224 fprintf(stderr, "%s\n", message);
226 parser_print_error_prefix();
227 fputs("Parse error: got ", stderr);
228 print_token(stderr, &token);
229 fputs(", expected ", stderr);
231 va_start(args, message);
232 token_type_t token_type = va_arg(args, token_type_t);
233 while(token_type != 0) {
237 fprintf(stderr, ", ");
239 print_token_type(stderr, token_type);
240 token_type = va_arg(args, token_type_t);
243 fprintf(stderr, "\n");
246 static void print_type_quoted(type_t *type)
253 static void type_error(const char *msg, const source_position_t source_position,
256 parser_print_error_prefix_pos(source_position);
257 fprintf(stderr, "%s, but found type ", msg);
258 print_type_quoted(type);
263 static void type_error_incompatible(const char *msg,
264 const source_position_t source_position, type_t *type1, type_t *type2)
266 parser_print_error_prefix_pos(source_position);
267 fprintf(stderr, "%s, incompatible types: ", msg);
268 print_type_quoted(type1);
269 fprintf(stderr, " - ");
270 print_type_quoted(type2);
271 fprintf(stderr, ")\n");
275 static void eat_block(void)
277 if(token.type == '{')
280 while(token.type != '}') {
281 if(token.type == T_EOF)
283 if(token.type == '{') {
292 static void eat_statement(void)
294 while(token.type != ';') {
295 if(token.type == T_EOF)
297 if(token.type == '}')
299 if(token.type == '{') {
308 static void eat_brace(void)
310 if(token.type == '(')
313 while(token.type != ')') {
314 if(token.type == T_EOF)
316 if(token.type == ')' || token.type == ';' || token.type == '}') {
319 if(token.type == '(') {
323 if(token.type == '{') {
332 #define expect(expected) \
333 if(UNLIKELY(token.type != (expected))) { \
334 parse_error_expected(NULL, (expected), 0); \
340 #define expect_void(expected) \
341 if(UNLIKELY(token.type != (expected))) { \
342 parse_error_expected(NULL, (expected), 0); \
348 static void set_context(context_t *new_context)
350 context = new_context;
352 last_declaration = new_context->declarations;
353 if(last_declaration != NULL) {
354 while(last_declaration->next != NULL) {
355 last_declaration = last_declaration->next;
361 * called when we find a 2nd declarator for an identifier we already have a
364 static bool is_compatible_declaration (declaration_t *declaration,
365 declaration_t *previous)
367 /* TODO: not correct yet */
368 return declaration->type == previous->type;
371 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
373 declaration_t *declaration = symbol->declaration;
374 for( ; declaration != NULL; declaration = declaration->symbol_next) {
375 if(declaration->namespace == namespace)
382 static const char *get_namespace_prefix(namespace_t namespace)
385 case NAMESPACE_NORMAL:
387 case NAMESPACE_UNION:
389 case NAMESPACE_STRUCT:
393 case NAMESPACE_LABEL:
396 panic("invalid namespace found");
400 * pushs an environment_entry on the environment stack and links the
401 * corresponding symbol to the new entry
403 static declaration_t *stack_push(stack_entry_t **stack_ptr,
404 declaration_t *declaration,
405 context_t *parent_context)
407 symbol_t *symbol = declaration->symbol;
408 namespace_t namespace = declaration->namespace;
410 /* a declaration should be only pushed once */
411 assert(declaration->parent_context == NULL);
412 declaration->parent_context = parent_context;
414 declaration_t *previous_declaration = get_declaration(symbol, namespace);
415 assert(declaration != previous_declaration);
416 if(previous_declaration != NULL
417 && previous_declaration->parent_context == context) {
418 if(!is_compatible_declaration(declaration, previous_declaration)) {
419 parser_print_error_prefix_pos(declaration->source_position);
420 fprintf(stderr, "definition of symbol %s%s with type ",
421 get_namespace_prefix(namespace), symbol->string);
423 print_type_quoted(declaration->type);
425 parser_print_error_prefix_pos(
426 previous_declaration->source_position);
427 fprintf(stderr, "is incompatible with previous declaration "
429 print_type_quoted(previous_declaration->type);
432 return previous_declaration;
435 /* remember old declaration */
437 entry.symbol = symbol;
438 entry.old_declaration = symbol->declaration;
439 entry.namespace = namespace;
440 ARR_APP1(*stack_ptr, entry);
442 /* replace/add declaration into declaration list of the symbol */
443 if(symbol->declaration == NULL) {
444 symbol->declaration = declaration;
446 declaration_t *iter_last = NULL;
447 declaration_t *iter = symbol->declaration;
448 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
449 /* replace an entry? */
450 if(iter->namespace == namespace) {
451 if(iter_last == NULL) {
452 symbol->declaration = declaration;
454 iter_last->symbol_next = declaration;
456 declaration->symbol_next = iter->symbol_next;
461 assert(iter_last->symbol_next == NULL);
462 iter_last->symbol_next = declaration;
469 static declaration_t *environment_push(declaration_t *declaration)
471 assert(declaration->source_position.input_name != NULL);
472 return stack_push(&environment_stack, declaration, context);
475 static declaration_t *label_push(declaration_t *declaration)
477 return stack_push(&label_stack, declaration, ¤t_function->context);
481 * pops symbols from the environment stack until @p new_top is the top element
483 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
485 stack_entry_t *stack = *stack_ptr;
486 size_t top = ARR_LEN(stack);
489 assert(new_top <= top);
493 for(i = top; i > new_top; --i) {
494 stack_entry_t *entry = & stack[i - 1];
496 declaration_t *old_declaration = entry->old_declaration;
497 symbol_t *symbol = entry->symbol;
498 namespace_t namespace = entry->namespace;
500 /* replace/remove declaration */
501 declaration_t *declaration = symbol->declaration;
502 assert(declaration != NULL);
503 if(declaration->namespace == namespace) {
504 if(old_declaration == NULL) {
505 symbol->declaration = declaration->symbol_next;
507 symbol->declaration = old_declaration;
508 assert(old_declaration->symbol_next ==
509 declaration->symbol_next);
512 declaration_t *iter_last = declaration;
513 declaration_t *iter = declaration->next;
514 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
515 /* replace an entry? */
516 if(iter->namespace == namespace) {
517 assert(iter_last != NULL);
518 iter_last->symbol_next = old_declaration;
519 assert(old_declaration->symbol_next ==
520 declaration->symbol_next);
524 assert(iter != NULL);
528 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
531 static void environment_pop_to(size_t new_top)
533 stack_pop_to(&environment_stack, new_top);
536 static void label_pop_to(size_t new_top)
538 stack_pop_to(&label_stack, new_top);
542 static int get_rank(const type_t *type)
544 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
545 * and esp. footnote 108). However we can't fold constants (yet), so we
546 * can't decide wether unsigned int is possible, while int always works.
547 * (unsigned int would be preferable when possible... for stuff like
548 * struct { enum { ... } bla : 4; } ) */
549 if(type->type == TYPE_ENUM)
550 return ATOMIC_TYPE_INT;
552 assert(type->type == TYPE_ATOMIC);
553 atomic_type_t *atomic_type = (atomic_type_t*) type;
554 atomic_type_type_t atype = atomic_type->atype;
558 static type_t *promote_integer(type_t *type)
560 if(get_rank(type) < ATOMIC_TYPE_INT)
566 static expression_t *create_cast_expression(expression_t *expression,
569 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
571 cast->expression.type = EXPR_UNARY;
572 cast->type = UNEXPR_CAST;
573 cast->value = expression;
574 cast->expression.datatype = dest_type;
576 return (expression_t*) cast;
579 static expression_t *create_implicit_cast(expression_t *expression,
582 type_t *source_type = expression->datatype;
584 if(source_type == NULL)
587 source_type = skip_typeref(source_type);
588 dest_type = skip_typeref(dest_type);
590 if(source_type == dest_type)
593 if(dest_type->type == TYPE_ATOMIC) {
594 if(source_type->type != TYPE_ATOMIC)
595 panic("casting of non-atomic types not implemented yet");
597 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
598 type_error_incompatible("can't cast types",
599 expression->source_position,
600 source_type, dest_type);
604 return create_cast_expression(expression, dest_type);
606 if(dest_type->type == TYPE_POINTER) {
607 pointer_type_t *pointer_type
608 = (pointer_type_t*) dest_type;
609 if(source_type->type == TYPE_POINTER) {
610 if(!pointers_compatible(source_type, dest_type)) {
611 type_error_incompatible("can't implicitely cast types",
612 expression->source_position,
613 source_type, dest_type);
616 return create_cast_expression(expression, dest_type);
618 } else if(source_type->type == TYPE_ARRAY) {
619 array_type_t *array_type = (array_type_t*) source_type;
620 if(!types_compatible(array_type->element_type,
621 pointer_type->points_to)) {
622 type_error_incompatible("can't implicitely cast types",
623 expression->source_position,
624 source_type, dest_type);
627 return create_cast_expression(expression, dest_type);
631 panic("casting of non-atomic types not implemented yet");
634 static void semantic_assign(type_t *orig_type_left, expression_t **right,
637 type_t *orig_type_right = (*right)->datatype;
639 if(orig_type_right == NULL)
642 type_t *type_left = skip_typeref(orig_type_left);
643 type_t *type_right = skip_typeref(orig_type_right);
645 if(type_left == type_right) {
647 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
648 *right = create_implicit_cast(*right, type_left);
649 } else if(type_left->type == TYPE_POINTER
650 && type_right->type == TYPE_POINTER) {
653 /* TODO: improve error message */
654 parser_print_error_prefix();
655 fprintf(stderr, "incompatible types in %s\n", context);
656 parser_print_error_prefix();
657 print_type_quoted(type_left);
658 fputs(" <- ", stderr);
659 print_type_quoted(type_right);
665 static expression_t *parse_constant_expression(void)
667 /* start parsing at precedence 7 (conditional expression) */
668 return parse_sub_expression(7);
671 static expression_t *parse_assignment_expression(void)
673 /* start parsing at precedence 2 (assignment expression) */
674 return parse_sub_expression(2);
677 typedef struct declaration_specifiers_t declaration_specifiers_t;
678 struct declaration_specifiers_t {
679 storage_class_t storage_class;
684 static void parse_compound_type_entries(void);
685 static declaration_t *parse_declarator(
686 const declaration_specifiers_t *specifiers, type_t *type,
687 bool may_be_abstract);
688 static declaration_t *record_declaration(declaration_t *declaration);
690 static const char *parse_string_literals(void)
692 assert(token.type == T_STRING_LITERAL);
693 const char *result = token.v.string;
697 while(token.type == T_STRING_LITERAL) {
698 result = concat_strings(result, token.v.string);
705 static void parse_attributes(void)
709 case T___attribute__:
717 parse_error("EOF while parsing attribute");
735 if(token.type != T_STRING_LITERAL) {
736 parse_error_expected("while parsing assembler attribute",
741 parse_string_literals();
746 goto attributes_finished;
754 static designator_t *parse_designation(void)
756 if(token.type != '[' && token.type != '.')
759 designator_t *result = NULL;
760 designator_t *last = NULL;
763 designator_t *designator;
766 designator = allocate_ast_zero(sizeof(designator[0]));
768 designator->array_access = parse_constant_expression();
772 designator = allocate_ast_zero(sizeof(designator[0]));
774 if(token.type != T_IDENTIFIER) {
775 parse_error_expected("while parsing designator",
779 designator->symbol = token.v.symbol;
787 assert(designator != NULL);
789 last->next = designator;
797 static initializer_t *parse_initializer_list(type_t *type);
799 static initializer_t *parse_initializer(type_t *type)
801 designator_t *designator = parse_designation();
803 initializer_t *result;
804 if(token.type == '{') {
805 result = parse_initializer_list(type);
807 result = allocate_ast_zero(sizeof(result[0]));
808 result->type = INITIALIZER_VALUE;
809 result->v.value = parse_assignment_expression();
812 semantic_assign(type, &result->v.value, "initializer");
815 result->designator = designator;
820 static initializer_t *parse_initializer_list(type_t *type)
827 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
828 result->type = INITIALIZER_LIST;
830 initializer_t *last = NULL;
832 initializer_t *initializer = parse_initializer(NULL);
834 last->next = initializer;
836 result->v.list = initializer;
840 if(token.type == '}')
843 if(token.type != ',') {
844 parse_error_expected("while parsing initializer list", ',', '}', 0);
850 if(token.type == '}')
859 static declaration_t *parse_compound_type_specifier(bool is_struct)
867 symbol_t *symbol = NULL;
868 declaration_t *declaration = NULL;
870 if(token.type == T_IDENTIFIER) {
871 symbol = token.v.symbol;
875 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
877 declaration = get_declaration(symbol, NAMESPACE_UNION);
879 } else if(token.type != '{') {
881 parse_error_expected("while parsing struct type specifier",
882 T_IDENTIFIER, '{', 0);
884 parse_error_expected("while parsing union type specifier",
885 T_IDENTIFIER, '{', 0);
891 if(declaration == NULL) {
892 declaration = allocate_type_zero(sizeof(declaration[0]));
895 declaration->namespace = NAMESPACE_STRUCT;
897 declaration->namespace = NAMESPACE_UNION;
899 declaration->source_position = token.source_position;
900 declaration->symbol = symbol;
901 record_declaration(declaration);
904 if(token.type == '{') {
905 if(declaration->init.is_defined) {
906 assert(symbol != NULL);
907 parser_print_error_prefix();
908 fprintf(stderr, "multiple definition of %s %s\n",
909 is_struct ? "struct" : "union", symbol->string);
910 declaration->context.declarations = NULL;
912 declaration->init.is_defined = true;
914 int top = environment_top();
915 context_t *last_context = context;
916 set_context(& declaration->context);
918 parse_compound_type_entries();
921 assert(context == & declaration->context);
922 set_context(last_context);
923 environment_pop_to(top);
929 static void parse_enum_entries(void)
933 if(token.type == '}') {
935 parse_error("empty enum not allowed");
940 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
942 if(token.type != T_IDENTIFIER) {
943 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
947 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
948 entry->symbol = token.v.symbol;
949 entry->source_position = token.source_position;
952 if(token.type == '=') {
954 entry->init.initializer = parse_initializer(type_int);
957 record_declaration(entry);
959 if(token.type != ',')
962 } while(token.type != '}');
967 static declaration_t *parse_enum_specifier(void)
971 declaration_t *declaration;
974 if(token.type == T_IDENTIFIER) {
975 symbol = token.v.symbol;
978 declaration = get_declaration(symbol, NAMESPACE_ENUM);
979 } else if(token.type != '{') {
980 parse_error_expected("while parsing enum type specifier",
981 T_IDENTIFIER, '{', 0);
988 if(declaration == NULL) {
989 declaration = allocate_type_zero(sizeof(declaration[0]));
991 declaration->namespace = NAMESPACE_ENUM;
992 declaration->source_position = token.source_position;
993 declaration->symbol = symbol;
996 if(token.type == '{') {
997 if(declaration->init.is_defined) {
998 parser_print_error_prefix();
999 fprintf(stderr, "multiple definitions of enum %s\n",
1002 record_declaration(declaration);
1003 declaration->init.is_defined = 1;
1005 parse_enum_entries();
1013 * if a symbol is a typedef to another type, return true
1015 static bool is_typedef_symbol(symbol_t *symbol)
1017 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1018 if(declaration == NULL
1019 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1025 static type_t *parse_typeof(void)
1033 expression_t *expression = NULL;
1036 switch(token.type) {
1037 case T___extension__:
1038 /* this can be a prefix to a typename or an expression */
1039 /* we simply eat it now. */
1042 } while(token.type == T___extension__);
1046 if(is_typedef_symbol(token.v.symbol)) {
1047 type = parse_typename();
1049 expression = parse_expression();
1050 type = expression->datatype;
1055 type = parse_typename();
1059 expression = parse_expression();
1060 type = expression->datatype;
1066 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1067 typeof->type.type = TYPE_TYPEOF;
1068 typeof->expression = expression;
1069 typeof->typeof_type = type;
1071 return (type_t*) typeof;
1075 SPECIFIER_SIGNED = 1 << 0,
1076 SPECIFIER_UNSIGNED = 1 << 1,
1077 SPECIFIER_LONG = 1 << 2,
1078 SPECIFIER_INT = 1 << 3,
1079 SPECIFIER_DOUBLE = 1 << 4,
1080 SPECIFIER_CHAR = 1 << 5,
1081 SPECIFIER_SHORT = 1 << 6,
1082 SPECIFIER_LONG_LONG = 1 << 7,
1083 SPECIFIER_FLOAT = 1 << 8,
1084 SPECIFIER_BOOL = 1 << 9,
1085 SPECIFIER_VOID = 1 << 10,
1086 #ifdef PROVIDE_COMPLEX
1087 SPECIFIER_COMPLEX = 1 << 11,
1089 #ifdef PROVIDE_IMAGINARY
1090 SPECIFIER_IMAGINARY = 1 << 12,
1094 static type_t *create_builtin_type(symbol_t *symbol)
1096 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1097 type->type.type = TYPE_BUILTIN;
1098 type->symbol = symbol;
1100 type->real_type = type_int;
1102 return (type_t*) type;
1105 static type_t *get_typedef_type(symbol_t *symbol)
1107 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1108 if(declaration == NULL
1109 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1112 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1113 typedef_type->type.type = TYPE_TYPEDEF;
1114 typedef_type->declaration = declaration;
1116 return (type_t*) typedef_type;
1119 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1121 type_t *type = NULL;
1122 unsigned type_qualifiers = 0;
1123 unsigned type_specifiers = 0;
1127 switch(token.type) {
1130 #define MATCH_STORAGE_CLASS(token, class) \
1132 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1133 parse_error("multiple storage classes in declaration " \
1136 specifiers->storage_class = class; \
1140 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1141 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1142 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1143 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1144 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1146 /* type qualifiers */
1147 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1149 type_qualifiers |= qualifier; \
1153 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1154 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1155 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1157 case T___extension__:
1162 /* type specifiers */
1163 #define MATCH_SPECIFIER(token, specifier, name) \
1166 if(type_specifiers & specifier) { \
1167 parse_error("multiple " name " type specifiers given"); \
1169 type_specifiers |= specifier; \
1173 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1174 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1175 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1176 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1177 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1178 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1179 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1180 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1181 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1182 #ifdef PROVIDE_COMPLEX
1183 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1185 #ifdef PROVIDE_IMAGINARY
1186 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1190 specifiers->is_inline = true;
1195 if(type_specifiers & SPECIFIER_LONG_LONG) {
1196 parse_error("multiple type specifiers given");
1197 } else if(type_specifiers & SPECIFIER_LONG) {
1198 type_specifiers |= SPECIFIER_LONG_LONG;
1200 type_specifiers |= SPECIFIER_LONG;
1204 /* TODO: if type != NULL for the following rules should issue
1207 compound_type_t *compound_type
1208 = allocate_type_zero(sizeof(compound_type[0]));
1209 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1210 compound_type->declaration = parse_compound_type_specifier(true);
1212 type = (type_t*) compound_type;
1216 compound_type_t *compound_type
1217 = allocate_type_zero(sizeof(compound_type[0]));
1218 compound_type->type.type = TYPE_COMPOUND_UNION;
1219 compound_type->declaration = parse_compound_type_specifier(false);
1221 type = (type_t*) compound_type;
1225 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1226 enum_type->type.type = TYPE_ENUM;
1227 enum_type->declaration = parse_enum_specifier();
1229 type = (type_t*) enum_type;
1233 type = parse_typeof();
1235 case T___builtin_va_list:
1236 type = create_builtin_type(token.v.symbol);
1240 case T___attribute__:
1245 case T_IDENTIFIER: {
1246 type_t *typedef_type = get_typedef_type(token.v.symbol);
1248 if(typedef_type == NULL)
1249 goto finish_specifiers;
1252 type = typedef_type;
1256 /* function specifier */
1258 goto finish_specifiers;
1265 atomic_type_type_t atomic_type;
1267 /* match valid basic types */
1268 switch(type_specifiers) {
1269 case SPECIFIER_VOID:
1270 atomic_type = ATOMIC_TYPE_VOID;
1272 case SPECIFIER_CHAR:
1273 atomic_type = ATOMIC_TYPE_CHAR;
1275 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1276 atomic_type = ATOMIC_TYPE_SCHAR;
1278 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1279 atomic_type = ATOMIC_TYPE_UCHAR;
1281 case SPECIFIER_SHORT:
1282 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1283 case SPECIFIER_SHORT | SPECIFIER_INT:
1284 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1285 atomic_type = ATOMIC_TYPE_SHORT;
1287 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1288 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1289 atomic_type = ATOMIC_TYPE_USHORT;
1292 case SPECIFIER_SIGNED:
1293 case SPECIFIER_SIGNED | SPECIFIER_INT:
1294 atomic_type = ATOMIC_TYPE_INT;
1296 case SPECIFIER_UNSIGNED:
1297 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1298 atomic_type = ATOMIC_TYPE_UINT;
1300 case SPECIFIER_LONG:
1301 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1302 case SPECIFIER_LONG | SPECIFIER_INT:
1303 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1304 atomic_type = ATOMIC_TYPE_LONG;
1306 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1307 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1308 atomic_type = ATOMIC_TYPE_ULONG;
1310 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1311 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1312 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1313 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1315 atomic_type = ATOMIC_TYPE_LONGLONG;
1317 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1318 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1320 atomic_type = ATOMIC_TYPE_ULONGLONG;
1322 case SPECIFIER_FLOAT:
1323 atomic_type = ATOMIC_TYPE_FLOAT;
1325 case SPECIFIER_DOUBLE:
1326 atomic_type = ATOMIC_TYPE_DOUBLE;
1328 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1329 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1331 case SPECIFIER_BOOL:
1332 atomic_type = ATOMIC_TYPE_BOOL;
1334 #ifdef PROVIDE_COMPLEX
1335 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1336 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1338 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1339 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1341 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1342 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1345 #ifdef PROVIDE_IMAGINARY
1346 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1347 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1349 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1350 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1352 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1353 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1357 /* invalid specifier combination, give an error message */
1358 if(type_specifiers == 0) {
1360 parse_warning("no type specifiers in declaration (using int)");
1361 atomic_type = ATOMIC_TYPE_INT;
1364 parse_error("no type specifiers given in declaration");
1366 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1367 (type_specifiers & SPECIFIER_UNSIGNED)) {
1368 parse_error("signed and unsigned specifiers gives");
1369 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1370 parse_error("only integer types can be signed or unsigned");
1372 parse_error("multiple datatypes in declaration");
1374 atomic_type = ATOMIC_TYPE_INVALID;
1377 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1378 atype->type.type = TYPE_ATOMIC;
1379 atype->atype = atomic_type;
1382 type = (type_t*) atype;
1384 if(type_specifiers != 0) {
1385 parse_error("multiple datatypes in declaration");
1389 type->qualifiers = type_qualifiers;
1391 type_t *result = typehash_insert(type);
1392 if(newtype && result != (type_t*) type) {
1396 specifiers->type = result;
1399 static type_qualifier_t parse_type_qualifiers(void)
1401 type_qualifier_t type_qualifiers = 0;
1404 switch(token.type) {
1405 /* type qualifiers */
1406 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1407 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1408 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1411 return type_qualifiers;
1416 static void parse_identifier_list(void)
1419 if(token.type != T_IDENTIFIER) {
1420 parse_error_expected("while parsing parameter identifier list",
1425 if(token.type != ',')
1431 static declaration_t *parse_parameter(void)
1433 declaration_specifiers_t specifiers;
1434 memset(&specifiers, 0, sizeof(specifiers));
1436 parse_declaration_specifiers(&specifiers);
1438 declaration_t *declaration
1439 = parse_declarator(&specifiers, specifiers.type, true);
1441 /* TODO check declaration constraints for parameters */
1442 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1443 parse_error("typedef not allowed in parameter list");
1449 static declaration_t *parse_parameters(function_type_t *type)
1451 if(token.type == T_IDENTIFIER) {
1452 symbol_t *symbol = token.v.symbol;
1453 if(!is_typedef_symbol(symbol)) {
1455 parse_identifier_list();
1460 if(token.type == ')') {
1461 type->unspecified_parameters = 1;
1464 if(token.type == T_void && look_ahead(1)->type == ')') {
1469 declaration_t *declarations = NULL;
1470 declaration_t *declaration;
1471 declaration_t *last_declaration = NULL;
1472 function_parameter_t *parameter;
1473 function_parameter_t *last_parameter = NULL;
1476 switch(token.type) {
1480 return declarations;
1483 case T___extension__:
1485 declaration = parse_parameter();
1487 parameter = allocate_type_zero(sizeof(parameter[0]));
1488 parameter->type = declaration->type;
1490 if(last_parameter != NULL) {
1491 last_declaration->next = declaration;
1492 last_parameter->next = parameter;
1494 type->parameters = parameter;
1495 declarations = declaration;
1497 last_parameter = parameter;
1498 last_declaration = declaration;
1502 return declarations;
1504 if(token.type != ',')
1505 return declarations;
1515 } construct_type_type_t;
1517 typedef struct construct_type_t construct_type_t;
1518 struct construct_type_t {
1519 construct_type_type_t type;
1520 construct_type_t *next;
1523 typedef struct parsed_pointer_t parsed_pointer_t;
1524 struct parsed_pointer_t {
1525 construct_type_t construct_type;
1526 type_qualifier_t type_qualifiers;
1529 typedef struct construct_function_type_t construct_function_type_t;
1530 struct construct_function_type_t {
1531 construct_type_t construct_type;
1532 function_type_t *function_type;
1535 typedef struct parsed_array_t parsed_array_t;
1536 struct parsed_array_t {
1537 construct_type_t construct_type;
1538 type_qualifier_t type_qualifiers;
1544 typedef struct construct_base_type_t construct_base_type_t;
1545 struct construct_base_type_t {
1546 construct_type_t construct_type;
1550 static construct_type_t *parse_pointer_declarator(void)
1554 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1555 memset(pointer, 0, sizeof(pointer[0]));
1556 pointer->construct_type.type = CONSTRUCT_POINTER;
1557 pointer->type_qualifiers = parse_type_qualifiers();
1559 return (construct_type_t*) pointer;
1562 static construct_type_t *parse_array_declarator(void)
1566 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1567 memset(array, 0, sizeof(array[0]));
1568 array->construct_type.type = CONSTRUCT_ARRAY;
1570 if(token.type == T_static) {
1571 array->is_static = true;
1575 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1576 if(type_qualifiers != 0) {
1577 if(token.type == T_static) {
1578 array->is_static = true;
1582 array->type_qualifiers = type_qualifiers;
1584 if(token.type == '*' && look_ahead(1)->type == ']') {
1585 array->is_variable = true;
1587 } else if(token.type != ']') {
1588 array->size = parse_assignment_expression();
1593 return (construct_type_t*) array;
1596 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1600 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1601 type->type.type = TYPE_FUNCTION;
1603 declaration_t *parameters = parse_parameters(type);
1604 if(declaration != NULL) {
1605 declaration->context.declarations = parameters;
1608 construct_function_type_t *construct_function_type =
1609 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1610 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1611 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1612 construct_function_type->function_type = type;
1616 return (construct_type_t*) construct_function_type;
1619 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1620 int may_be_abstract)
1622 construct_type_t *result = NULL;
1623 construct_type_t *last = NULL;
1625 while(token.type == '*') {
1626 construct_type_t *type = parse_pointer_declarator();
1635 /* TODO: find out if this is correct */
1638 construct_type_t *inner_types = NULL;
1640 switch(token.type) {
1642 if(declaration == NULL) {
1643 parse_error("no identifier expected in typename");
1645 declaration->symbol = token.v.symbol;
1646 declaration->source_position = token.source_position;
1652 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1658 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1662 construct_type_t *type;
1663 switch(token.type) {
1665 type = parse_function_declarator(declaration);
1668 type = parse_array_declarator();
1671 goto declarator_finished;
1682 declarator_finished:
1685 if(inner_types != NULL) {
1687 last->next = inner_types;
1689 result = inner_types;
1697 static type_t *construct_declarator_type(construct_type_t *construct_list,
1700 construct_type_t *iter = construct_list;
1701 for( ; iter != NULL; iter = iter->next) {
1702 parsed_pointer_t *parsed_pointer;
1703 parsed_array_t *parsed_array;
1704 construct_function_type_t *construct_function_type;
1705 function_type_t *function_type;
1706 pointer_type_t *pointer_type;
1707 array_type_t *array_type;
1709 switch(iter->type) {
1710 case CONSTRUCT_INVALID:
1711 panic("invalid type construction found");
1712 case CONSTRUCT_FUNCTION:
1713 construct_function_type = (construct_function_type_t*) iter;
1714 function_type = construct_function_type->function_type;
1716 function_type->result_type = type;
1717 type = (type_t*) function_type;
1720 case CONSTRUCT_POINTER:
1721 parsed_pointer = (parsed_pointer_t*) iter;
1722 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1724 pointer_type->type.type = TYPE_POINTER;
1725 pointer_type->points_to = type;
1726 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1727 type = (type_t*) pointer_type;
1730 case CONSTRUCT_ARRAY:
1731 parsed_array = (parsed_array_t*) iter;
1732 array_type = allocate_type_zero(sizeof(array_type[0]));
1734 array_type->type.type = TYPE_ARRAY;
1735 array_type->element_type = type;
1736 array_type->type.qualifiers = parsed_array->type_qualifiers;
1737 array_type->is_static = parsed_array->is_static;
1738 array_type->is_variable = parsed_array->is_variable;
1739 array_type->size = parsed_array->size;
1740 type = (type_t*) array_type;
1744 type_t *hashed_type = typehash_insert((type_t*) type);
1745 if(hashed_type != type) {
1754 static declaration_t *parse_declarator(
1755 const declaration_specifiers_t *specifiers,
1756 type_t *type, bool may_be_abstract)
1758 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1759 declaration->storage_class = specifiers->storage_class;
1760 declaration->is_inline = specifiers->is_inline;
1762 construct_type_t *construct_type
1763 = parse_inner_declarator(declaration, may_be_abstract);
1764 declaration->type = construct_declarator_type(construct_type, type);
1766 if(construct_type != NULL) {
1767 obstack_free(&temp_obst, construct_type);
1773 static type_t *parse_abstract_declarator(type_t *base_type)
1775 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1777 type_t *result = construct_declarator_type(construct_type, base_type);
1778 if(construct_type != NULL) {
1779 obstack_free(&temp_obst, construct_type);
1785 static declaration_t *record_declaration(declaration_t *declaration)
1787 assert(context != NULL);
1789 symbol_t *symbol = declaration->symbol;
1790 if(symbol != NULL) {
1791 declaration_t *alias = environment_push(declaration);
1792 if(alias != declaration)
1795 declaration->parent_context = context;
1798 if(last_declaration != NULL) {
1799 last_declaration->next = declaration;
1801 context->declarations = declaration;
1803 last_declaration = declaration;
1808 static void parser_error_multiple_definition(declaration_t *previous,
1809 declaration_t *declaration)
1811 parser_print_error_prefix_pos(declaration->source_position);
1812 fprintf(stderr, "multiple definition of symbol '%s'\n",
1813 declaration->symbol->string);
1814 parser_print_error_prefix_pos(previous->source_position);
1815 fprintf(stderr, "this is the location of the previous "
1820 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1823 declaration_t *ndeclaration
1824 = parse_declarator(specifiers, specifiers->type, false);
1826 declaration_t *declaration = record_declaration(ndeclaration);
1828 type_t *type = declaration->type;
1829 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
1830 parser_print_warning_prefix_pos(declaration->source_position);
1831 fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
1832 declaration->symbol->string);
1835 if(token.type == '=') {
1838 /* TODO: check that this is an allowed type (no function type) */
1840 if(declaration->init.initializer != NULL) {
1841 parser_error_multiple_definition(declaration, ndeclaration);
1844 ndeclaration->init.initializer = parse_initializer(declaration->type);
1845 } else if(token.type == '{') {
1846 if(declaration->type->type != TYPE_FUNCTION) {
1847 parser_print_error_prefix();
1848 fprintf(stderr, "Declarator ");
1849 print_type_ext(declaration->type, declaration->symbol, NULL);
1850 fprintf(stderr, " has a body but is not a function type.\n");
1855 if(declaration->init.statement != NULL) {
1856 parser_error_multiple_definition(declaration, ndeclaration);
1858 if(ndeclaration != declaration) {
1859 memcpy(&declaration->context, &ndeclaration->context,
1860 sizeof(declaration->context));
1863 int top = environment_top();
1864 context_t *last_context = context;
1865 set_context(&declaration->context);
1867 /* push function parameters */
1868 declaration_t *parameter = declaration->context.declarations;
1869 for( ; parameter != NULL; parameter = parameter->next) {
1870 environment_push(parameter);
1873 int label_stack_top = label_top();
1874 declaration_t *old_current_function = current_function;
1875 current_function = declaration;
1877 statement_t *statement = parse_compound_statement();
1879 assert(current_function == declaration);
1880 current_function = old_current_function;
1881 label_pop_to(label_stack_top);
1883 assert(context == &declaration->context);
1884 set_context(last_context);
1885 environment_pop_to(top);
1887 declaration->init.statement = statement;
1891 if(token.type != ',')
1898 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1901 if(token.type == ':') {
1903 parse_constant_expression();
1904 /* TODO (bitfields) */
1906 declaration_t *declaration
1907 = parse_declarator(specifiers, specifiers->type, true);
1909 /* TODO: check constraints for struct declarations */
1910 /* TODO: check for doubled fields */
1911 record_declaration(declaration);
1913 if(token.type == ':') {
1915 parse_constant_expression();
1916 /* TODO (bitfields) */
1920 if(token.type != ',')
1927 static void parse_compound_type_entries(void)
1931 while(token.type != '}' && token.type != T_EOF) {
1932 declaration_specifiers_t specifiers;
1933 memset(&specifiers, 0, sizeof(specifiers));
1934 parse_declaration_specifiers(&specifiers);
1936 parse_struct_declarators(&specifiers);
1938 if(token.type == T_EOF) {
1939 parse_error("unexpected error while parsing struct");
1944 static void parse_declaration(void)
1946 source_position_t source_position = token.source_position;
1948 declaration_specifiers_t specifiers;
1949 memset(&specifiers, 0, sizeof(specifiers));
1950 parse_declaration_specifiers(&specifiers);
1952 if(token.type == ';') {
1955 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1957 declaration->type = specifiers.type;
1958 declaration->storage_class = specifiers.storage_class;
1959 declaration->source_position = source_position;
1960 record_declaration(declaration);
1963 parse_init_declarators(&specifiers);
1966 static type_t *parse_typename(void)
1968 declaration_specifiers_t specifiers;
1969 memset(&specifiers, 0, sizeof(specifiers));
1970 parse_declaration_specifiers(&specifiers);
1971 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1972 /* TODO: improve error message, user does probably not know what a
1973 * storage class is...
1975 parse_error("typename may not have a storage class");
1978 type_t *result = parse_abstract_declarator(specifiers.type);
1986 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1987 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1988 expression_t *left);
1990 typedef struct expression_parser_function_t expression_parser_function_t;
1991 struct expression_parser_function_t {
1992 unsigned precedence;
1993 parse_expression_function parser;
1994 unsigned infix_precedence;
1995 parse_expression_infix_function infix_parser;
1998 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2000 static expression_t *make_invalid_expression(void)
2002 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2003 expression->type = EXPR_INVALID;
2004 expression->source_position = token.source_position;
2008 static expression_t *expected_expression_error(void)
2010 parser_print_error_prefix();
2011 fprintf(stderr, "expected expression, got token ");
2012 print_token(stderr, & token);
2013 fprintf(stderr, "\n");
2017 return make_invalid_expression();
2020 static expression_t *parse_string_const(void)
2022 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2024 cnst->expression.type = EXPR_STRING_LITERAL;
2025 cnst->expression.datatype = type_string;
2026 cnst->value = parse_string_literals();
2028 return (expression_t*) cnst;
2031 static expression_t *parse_int_const(void)
2033 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2035 cnst->expression.type = EXPR_CONST;
2036 cnst->expression.datatype = type_int;
2037 cnst->v.int_value = token.v.intvalue;
2041 return (expression_t*) cnst;
2044 static expression_t *parse_float_const(void)
2046 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2048 cnst->expression.type = EXPR_CONST;
2049 cnst->expression.datatype = type_double;
2050 cnst->v.float_value = token.v.floatvalue;
2054 return (expression_t*) cnst;
2057 static declaration_t *create_implicit_function(symbol_t *symbol,
2058 const source_position_t source_position)
2060 function_type_t *function_type
2061 = allocate_type_zero(sizeof(function_type[0]));
2063 function_type->type.type = TYPE_FUNCTION;
2064 function_type->result_type = type_int;
2065 function_type->unspecified_parameters = true;
2067 type_t *type = typehash_insert((type_t*) function_type);
2068 if(type != (type_t*) function_type) {
2069 free_type(function_type);
2072 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2074 declaration->storage_class = STORAGE_CLASS_EXTERN;
2075 declaration->type = type;
2076 declaration->symbol = symbol;
2077 declaration->source_position = source_position;
2079 /* prepend the implicit definition to the global context
2080 * this is safe since the symbol wasn't declared as anything else yet
2082 assert(symbol->declaration == NULL);
2084 context_t *last_context = context;
2085 context = global_context;
2087 environment_push(declaration);
2088 declaration->next = context->declarations;
2089 context->declarations = declaration;
2091 context = last_context;
2096 static expression_t *parse_reference(void)
2098 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2100 ref->expression.type = EXPR_REFERENCE;
2101 ref->symbol = token.v.symbol;
2103 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2105 source_position_t source_position = token.source_position;
2108 if(declaration == NULL) {
2110 /* an implicitely defined function */
2111 if(token.type == '(') {
2112 parser_print_prefix_pos(token.source_position);
2113 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2114 ref->symbol->string);
2116 declaration = create_implicit_function(ref->symbol,
2121 parser_print_error_prefix();
2122 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2123 return (expression_t*) ref;
2127 ref->declaration = declaration;
2128 ref->expression.datatype = declaration->type;
2130 return (expression_t*) ref;
2133 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2137 /* TODO check if explicit cast is allowed and issue warnings/errors */
2140 static expression_t *parse_cast(void)
2142 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2144 cast->expression.type = EXPR_UNARY;
2145 cast->type = UNEXPR_CAST;
2146 cast->expression.source_position = token.source_position;
2148 type_t *type = parse_typename();
2151 expression_t *value = parse_sub_expression(20);
2153 check_cast_allowed(value, type);
2155 cast->expression.datatype = type;
2156 cast->value = value;
2158 return (expression_t*) cast;
2161 static expression_t *parse_statement_expression(void)
2163 statement_expression_t *expression
2164 = allocate_ast_zero(sizeof(expression[0]));
2165 expression->expression.type = EXPR_STATEMENT;
2167 statement_t *statement = parse_compound_statement();
2168 expression->statement = statement;
2169 if(statement == NULL) {
2174 assert(statement->type == STATEMENT_COMPOUND);
2175 compound_statement_t *compound_statement
2176 = (compound_statement_t*) statement;
2178 /* find last statement and use it's type */
2179 const statement_t *last_statement = NULL;
2180 const statement_t *iter = compound_statement->statements;
2181 for( ; iter != NULL; iter = iter->next) {
2182 last_statement = iter;
2185 if(last_statement->type == STATEMENT_EXPRESSION) {
2186 const expression_statement_t *expression_statement =
2187 (const expression_statement_t*) last_statement;
2188 expression->expression.datatype
2189 = expression_statement->expression->datatype;
2191 expression->expression.datatype = type_void;
2196 return (expression_t*) expression;
2199 static expression_t *parse_brace_expression(void)
2203 switch(token.type) {
2205 /* gcc extension: a stement expression */
2206 return parse_statement_expression();
2210 return parse_cast();
2212 if(is_typedef_symbol(token.v.symbol)) {
2213 return parse_cast();
2217 expression_t *result = parse_expression();
2223 static expression_t *parse_function_keyword(void)
2225 eat(T___FUNCTION__);
2228 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2229 expression->expression.type = EXPR_FUNCTION;
2230 expression->expression.datatype = type_string;
2231 expression->value = "TODO: FUNCTION";
2233 return (expression_t*) expression;
2236 static expression_t *parse_pretty_function_keyword(void)
2238 eat(T___PRETTY_FUNCTION__);
2241 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2242 expression->expression.type = EXPR_PRETTY_FUNCTION;
2243 expression->expression.datatype = type_string;
2244 expression->value = "TODO: PRETTY FUNCTION";
2246 return (expression_t*) expression;
2249 static designator_t *parse_designator(void)
2251 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2253 if(token.type != T_IDENTIFIER) {
2254 parse_error_expected("while parsing member designator",
2259 result->symbol = token.v.symbol;
2262 designator_t *last_designator = result;
2264 if(token.type == '.') {
2266 if(token.type != T_IDENTIFIER) {
2267 parse_error_expected("while parsing member designator",
2272 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2273 designator->symbol = token.v.symbol;
2276 last_designator->next = designator;
2277 last_designator = designator;
2280 if(token.type == '[') {
2282 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2283 designator->array_access = parse_expression();
2284 if(designator->array_access == NULL) {
2290 last_designator->next = designator;
2291 last_designator = designator;
2300 static expression_t *parse_offsetof(void)
2302 eat(T___builtin_offsetof);
2304 offsetof_expression_t *expression
2305 = allocate_ast_zero(sizeof(expression[0]));
2306 expression->expression.type = EXPR_OFFSETOF;
2307 expression->expression.datatype = type_size_t;
2310 expression->type = parse_typename();
2312 expression->designator = parse_designator();
2315 return (expression_t*) expression;
2318 static expression_t *parse_va_arg(void)
2320 eat(T___builtin_va_arg);
2322 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2323 expression->expression.type = EXPR_VA_ARG;
2326 expression->arg = parse_assignment_expression();
2328 expression->expression.datatype = parse_typename();
2331 return (expression_t*) expression;
2334 static expression_t *parse_builtin_symbol(void)
2336 builtin_symbol_expression_t *expression
2337 = allocate_ast_zero(sizeof(expression[0]));
2338 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2340 /* TODO: set datatype */
2342 expression->symbol = token.v.symbol;
2346 return (expression_t*) expression;
2349 static expression_t *parse_primary_expression(void)
2351 switch(token.type) {
2353 return parse_int_const();
2354 case T_FLOATINGPOINT:
2355 return parse_float_const();
2356 case T_STRING_LITERAL:
2357 return parse_string_const();
2359 return parse_reference();
2360 case T___FUNCTION__:
2361 return parse_function_keyword();
2362 case T___PRETTY_FUNCTION__:
2363 return parse_pretty_function_keyword();
2364 case T___builtin_offsetof:
2365 return parse_offsetof();
2366 case T___builtin_va_arg:
2367 return parse_va_arg();
2368 case T___builtin_expect:
2369 case T___builtin_va_start:
2370 case T___builtin_va_end:
2371 return parse_builtin_symbol();
2374 return parse_brace_expression();
2377 parser_print_error_prefix();
2378 fprintf(stderr, "unexpected token ");
2379 print_token(stderr, &token);
2380 fprintf(stderr, "\n");
2383 return make_invalid_expression();
2386 static expression_t *parse_array_expression(unsigned precedence,
2387 expression_t *array_ref)
2393 array_access_expression_t *array_access
2394 = allocate_ast_zero(sizeof(array_access[0]));
2396 array_access->expression.type = EXPR_ARRAY_ACCESS;
2397 array_access->array_ref = array_ref;
2398 array_access->index = parse_expression();
2400 type_t *type = array_ref->datatype;
2402 if(type->type == TYPE_POINTER) {
2403 pointer_type_t *pointer = (pointer_type_t*) type;
2404 array_access->expression.datatype = pointer->points_to;
2405 } else if(type->type == TYPE_ARRAY) {
2406 array_type_t *array_type = (array_type_t*) type;
2407 array_access->expression.datatype = array_type->element_type;
2409 parser_print_error_prefix();
2410 fprintf(stderr, "array access on object with non-pointer type ");
2411 print_type_quoted(type);
2412 fprintf(stderr, "\n");
2416 if(token.type != ']') {
2417 parse_error_expected("Problem while parsing array access", ']', 0);
2418 return (expression_t*) array_access;
2422 return (expression_t*) array_access;
2425 static bool is_declaration_specifier(const token_t *token,
2426 bool only_type_specifiers)
2428 switch(token->type) {
2432 return is_typedef_symbol(token->v.symbol);
2435 if(only_type_specifiers)
2444 static expression_t *parse_sizeof(unsigned precedence)
2448 sizeof_expression_t *sizeof_expression
2449 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2450 sizeof_expression->expression.type = EXPR_SIZEOF;
2451 sizeof_expression->expression.datatype = type_size_t;
2453 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2455 sizeof_expression->type = parse_typename();
2458 expression_t *expression = parse_sub_expression(precedence);
2459 sizeof_expression->type = expression->datatype;
2460 sizeof_expression->size_expression = expression;
2463 return (expression_t*) sizeof_expression;
2466 static expression_t *parse_select_expression(unsigned precedence,
2467 expression_t *compound)
2470 assert(token.type == '.' || token.type == T_MINUSGREATER);
2472 bool is_pointer = (token.type == T_MINUSGREATER);
2475 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2477 select->expression.type = EXPR_SELECT;
2478 select->compound = compound;
2480 if(token.type != T_IDENTIFIER) {
2481 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2482 return (expression_t*) select;
2484 symbol_t *symbol = token.v.symbol;
2485 select->symbol = symbol;
2488 type_t *type = compound->datatype;
2490 return make_invalid_expression();
2492 type_t *type_left = type;
2494 if(type->type != TYPE_POINTER) {
2495 parser_print_error_prefix();
2496 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2497 print_type_quoted(type);
2498 fputc('\n', stderr);
2499 return make_invalid_expression();
2501 pointer_type_t *pointer_type = (pointer_type_t*) type;
2502 type_left = pointer_type->points_to;
2504 type_left = skip_typeref(type_left);
2506 if(type_left->type != TYPE_COMPOUND_STRUCT
2507 && type_left->type != TYPE_COMPOUND_UNION) {
2508 parser_print_error_prefix();
2509 fprintf(stderr, "request for member '%s' in something not a struct or "
2510 "union, but ", symbol->string);
2511 print_type_quoted(type_left);
2512 fputc('\n', stderr);
2513 return make_invalid_expression();
2516 compound_type_t *compound_type = (compound_type_t*) type_left;
2517 declaration_t *declaration = compound_type->declaration;
2519 if(!declaration->init.is_defined) {
2520 parser_print_error_prefix();
2521 fprintf(stderr, "request for member '%s' of incomplete type ",
2523 print_type_quoted(type_left);
2524 fputc('\n', stderr);
2525 return make_invalid_expression();
2528 declaration_t *iter = declaration->context.declarations;
2529 for( ; iter != NULL; iter = iter->next) {
2530 if(iter->symbol == symbol) {
2535 parser_print_error_prefix();
2536 print_type_quoted(type_left);
2537 fprintf(stderr, " has no memeber named '%s'\n", symbol->string);
2538 return make_invalid_expression();
2541 select->compound_entry = iter;
2542 select->expression.datatype = iter->type;
2543 return (expression_t*) select;
2546 static expression_t *parse_call_expression(unsigned precedence,
2547 expression_t *expression)
2550 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2551 call->expression.type = EXPR_CALL;
2552 call->function = expression;
2554 function_type_t *function_type;
2555 type_t *type = expression->datatype;
2556 if(type->type != TYPE_FUNCTION) {
2557 /* TODO calling pointers to functions is ok */
2558 parser_print_error_prefix();
2559 fputs("called object '", stderr);
2560 print_expression(expression);
2561 fputs("' (type ", stderr);
2562 print_type_quoted(type);
2563 fputs("is not a function\n", stderr);
2565 function_type = NULL;
2566 call->expression.datatype = NULL;
2568 function_type = (function_type_t*) type;
2569 call->expression.datatype = function_type->result_type;
2572 /* parse arguments */
2575 if(token.type != ')') {
2576 call_argument_t *last_argument = NULL;
2579 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2581 argument->expression = parse_assignment_expression();
2582 if(last_argument == NULL) {
2583 call->arguments = argument;
2585 last_argument->next = argument;
2587 last_argument = argument;
2589 if(token.type != ',')
2596 if(function_type != NULL) {
2597 function_parameter_t *parameter = function_type->parameters;
2598 call_argument_t *argument = call->arguments;
2599 for( ; parameter != NULL && argument != NULL;
2600 parameter = parameter->next, argument = argument->next) {
2601 type_t *expected_type = parameter->type;
2602 /* TODO report context in error messages */
2603 argument->expression = create_implicit_cast(argument->expression,
2606 /* too few parameters */
2607 if(parameter != NULL) {
2608 parser_print_error_prefix();
2609 fprintf(stderr, "too few arguments to function '");
2610 print_expression(expression);
2611 fprintf(stderr, "'\n");
2612 } else if(argument != NULL) {
2613 /* too many parameters */
2614 if(!function_type->variadic
2615 && !function_type->unspecified_parameters) {
2616 parser_print_error_prefix();
2617 fprintf(stderr, "too many arguments to function '");
2618 print_expression(expression);
2619 fprintf(stderr, "'\n");
2621 /* do default promotion */
2622 for( ; argument != NULL; argument = argument->next) {
2623 type_t *type = argument->expression->datatype;
2628 if(is_type_integer(type)) {
2629 type = promote_integer(type);
2630 } else if(type == type_float) {
2633 argument->expression
2634 = create_implicit_cast(argument->expression, type);
2640 return (expression_t*) call;
2643 static type_t *get_type_after_conversion(const type_t *type1,
2644 const type_t *type2)
2648 return (type_t*) type1;
2651 static expression_t *parse_conditional_expression(unsigned precedence,
2652 expression_t *expression)
2656 conditional_expression_t *conditional
2657 = allocate_ast_zero(sizeof(conditional[0]));
2658 conditional->expression.type = EXPR_CONDITIONAL;
2659 conditional->condition = expression;
2662 type_t *condition_type = conditional->condition->datatype;
2663 if(condition_type != NULL) {
2664 if(!is_type_scalar(condition_type)) {
2665 type_error("expected a scalar type", expression->source_position,
2670 conditional->true_expression = parse_expression();
2672 conditional->false_expression = parse_sub_expression(precedence);
2674 type_t *true_type = conditional->true_expression->datatype;
2675 if(true_type == NULL)
2676 return (expression_t*) conditional;
2677 type_t *false_type = conditional->false_expression->datatype;
2678 if(false_type == NULL)
2679 return (expression_t*) conditional;
2682 if(true_type == false_type) {
2683 conditional->expression.datatype = true_type;
2684 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2685 type_t *result = get_type_after_conversion(true_type, false_type);
2686 /* TODO: create implicit convs if necessary */
2687 conditional->expression.datatype = result;
2688 } else if(true_type->type == TYPE_POINTER &&
2689 false_type->type == TYPE_POINTER &&
2690 true /* TODO compatible points_to types */) {
2692 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2693 || (is_null_ptr_const(false_type) &&
2694 true_type->type == TYPE_POINTER) TODO*/ false) {
2696 } else if(/* 1 is pointer to object type, other is void* */ false) {
2699 type_error_incompatible("while parsing conditional",
2700 expression->source_position, true_type,
2704 return (expression_t*) conditional;
2707 static expression_t *parse_extension(unsigned precedence)
2709 eat(T___extension__);
2711 /* TODO enable extensions */
2713 return parse_sub_expression(precedence);
2716 static void semantic_incdec(unary_expression_t *expression)
2718 type_t *orig_type = expression->value->datatype;
2719 if(orig_type == NULL)
2722 type_t *type = skip_typeref(orig_type);
2723 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2724 /* TODO: improve error message */
2725 parser_print_error_prefix();
2726 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2730 expression->expression.datatype = orig_type;
2733 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2735 type_t *orig_type = expression->value->datatype;
2736 if(orig_type == NULL)
2739 type_t *type = skip_typeref(orig_type);
2740 if(!is_type_arithmetic(type)) {
2741 /* TODO: improve error message */
2742 parser_print_error_prefix();
2743 fprintf(stderr, "operation needs an arithmetic type\n");
2747 expression->expression.datatype = orig_type;
2750 static void semantic_dereference(unary_expression_t *expression)
2752 type_t *orig_type = expression->value->datatype;
2753 if(orig_type == NULL)
2756 type_t *type = skip_typeref(orig_type);
2757 if(type->type != TYPE_POINTER) {
2758 /* TODO: improve error message */
2759 parser_print_error_prefix();
2760 fprintf(stderr, "operation needs a pointer type\n");
2764 pointer_type_t *pointer_type = (pointer_type_t*) type;
2765 expression->expression.datatype = pointer_type->points_to;
2768 static void semantic_take_addr(unary_expression_t *expression)
2770 type_t *orig_type = expression->value->datatype;
2771 if(orig_type == NULL)
2774 expression->expression.datatype = make_pointer_type(orig_type, 0);
2777 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2778 static expression_t *parse_##unexpression_type(unsigned precedence) \
2782 unary_expression_t *unary_expression \
2783 = allocate_ast_zero(sizeof(unary_expression[0])); \
2784 unary_expression->expression.type = EXPR_UNARY; \
2785 unary_expression->type = unexpression_type; \
2786 unary_expression->value = parse_sub_expression(precedence); \
2788 sfunc(unary_expression); \
2790 return (expression_t*) unary_expression; \
2793 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2794 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2795 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2796 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2797 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2798 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2799 semantic_unexpr_arithmetic)
2800 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2802 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2805 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2807 static expression_t *parse_##unexpression_type(unsigned precedence, \
2808 expression_t *left) \
2810 (void) precedence; \
2813 unary_expression_t *unary_expression \
2814 = allocate_ast_zero(sizeof(unary_expression[0])); \
2815 unary_expression->expression.type = EXPR_UNARY; \
2816 unary_expression->type = unexpression_type; \
2817 unary_expression->value = left; \
2819 sfunc(unary_expression); \
2821 return (expression_t*) unary_expression; \
2824 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2826 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2829 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2831 /* TODO: handle complex + imaginary types */
2833 /* § 6.3.1.8 Usual arithmetic conversions */
2834 if(type_left == type_long_double || type_right == type_long_double) {
2835 return type_long_double;
2836 } else if(type_left == type_double || type_right == type_double) {
2838 } else if(type_left == type_float || type_right == type_float) {
2842 type_right = promote_integer(type_right);
2843 type_left = promote_integer(type_left);
2845 if(type_left == type_right)
2848 bool signed_left = is_type_signed(type_left);
2849 bool signed_right = is_type_signed(type_right);
2850 if(get_rank(type_left) < get_rank(type_right)) {
2851 if(signed_left == signed_right || !signed_right) {
2857 if(signed_left == signed_right || !signed_left) {
2865 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2867 expression_t *left = expression->left;
2868 expression_t *right = expression->right;
2869 type_t *orig_type_left = left->datatype;
2870 type_t *orig_type_right = right->datatype;
2872 if(orig_type_left == NULL || orig_type_right == NULL)
2875 type_t *type_left = skip_typeref(orig_type_left);
2876 type_t *type_right = skip_typeref(orig_type_right);
2878 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2879 /* TODO: improve error message */
2880 parser_print_error_prefix();
2881 fprintf(stderr, "operation needs arithmetic types\n");
2885 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2886 expression->left = create_implicit_cast(left, arithmetic_type);
2887 expression->right = create_implicit_cast(right, arithmetic_type);
2888 expression->expression.datatype = arithmetic_type;
2891 static void semantic_shift_op(binary_expression_t *expression)
2893 expression_t *left = expression->left;
2894 expression_t *right = expression->right;
2895 type_t *orig_type_left = left->datatype;
2896 type_t *orig_type_right = right->datatype;
2898 if(orig_type_left == NULL || orig_type_right == NULL)
2901 type_t *type_left = skip_typeref(orig_type_left);
2902 type_t *type_right = skip_typeref(orig_type_right);
2904 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
2905 /* TODO: improve error message */
2906 parser_print_error_prefix();
2907 fprintf(stderr, "operation needs integer types\n");
2911 type_left = promote_integer(type_left);
2912 type_right = promote_integer(type_right);
2914 expression->left = create_implicit_cast(left, type_left);
2915 expression->right = create_implicit_cast(right, type_right);
2916 expression->expression.datatype = type_left;
2919 static void semantic_add(binary_expression_t *expression)
2921 expression_t *left = expression->left;
2922 expression_t *right = expression->right;
2923 type_t *orig_type_left = left->datatype;
2924 type_t *orig_type_right = right->datatype;
2926 if(orig_type_left == NULL || orig_type_right == NULL)
2929 type_t *type_left = skip_typeref(orig_type_left);
2930 type_t *type_right = skip_typeref(orig_type_right);
2933 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2934 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2935 expression->left = create_implicit_cast(left, arithmetic_type);
2936 expression->right = create_implicit_cast(right, arithmetic_type);
2937 expression->expression.datatype = arithmetic_type;
2939 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2940 expression->expression.datatype = type_left;
2941 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2942 expression->expression.datatype = type_right;
2944 parser_print_error_prefix();
2945 fprintf(stderr, "invalid operands to binary + (");
2946 print_type_quoted(orig_type_left);
2947 fprintf(stderr, ", ");
2948 print_type_quoted(orig_type_right);
2949 fprintf(stderr, ")\n");
2953 static void semantic_sub(binary_expression_t *expression)
2955 expression_t *left = expression->left;
2956 expression_t *right = expression->right;
2957 type_t *orig_type_left = left->datatype;
2958 type_t *orig_type_right = right->datatype;
2960 if(orig_type_left == NULL || orig_type_right == NULL)
2963 type_t *type_left = skip_typeref(orig_type_left);
2964 type_t *type_right = skip_typeref(orig_type_right);
2967 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2968 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2969 expression->left = create_implicit_cast(left, arithmetic_type);
2970 expression->right = create_implicit_cast(right, arithmetic_type);
2971 expression->expression.datatype = arithmetic_type;
2973 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2974 expression->expression.datatype = type_left;
2975 } else if(type_left->type == TYPE_POINTER &&
2976 type_right->type == TYPE_POINTER) {
2977 if(!pointers_compatible(type_left, type_right)) {
2978 parser_print_error_prefix();
2979 fprintf(stderr, "pointers to incompatible objects to binary - (");
2980 print_type_quoted(orig_type_left);
2981 fprintf(stderr, ", ");
2982 print_type_quoted(orig_type_right);
2983 fprintf(stderr, ")\n");
2985 expression->expression.datatype = type_ptrdiff_t;
2988 parser_print_error_prefix();
2989 fprintf(stderr, "invalid operands to binary - (");
2990 print_type_quoted(orig_type_left);
2991 fprintf(stderr, ", ");
2992 print_type_quoted(orig_type_right);
2993 fprintf(stderr, ")\n");
2997 static void semantic_comparison(binary_expression_t *expression)
2999 expression_t *left = expression->left;
3000 expression_t *right = expression->right;
3001 type_t *orig_type_left = left->datatype;
3002 type_t *orig_type_right = right->datatype;
3004 if(orig_type_left == NULL || orig_type_right == NULL)
3007 type_t *type_left = skip_typeref(orig_type_left);
3008 type_t *type_right = skip_typeref(orig_type_right);
3010 /* TODO non-arithmetic types */
3011 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3012 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3013 expression->left = create_implicit_cast(left, arithmetic_type);
3014 expression->right = create_implicit_cast(right, arithmetic_type);
3015 expression->expression.datatype = arithmetic_type;
3017 expression->expression.datatype = type_int;
3020 static void semantic_arithmetic_assign(binary_expression_t *expression)
3022 expression_t *left = expression->left;
3023 expression_t *right = expression->right;
3024 type_t *orig_type_left = left->datatype;
3025 type_t *orig_type_right = right->datatype;
3027 if(orig_type_left == NULL || orig_type_right == NULL)
3030 type_t *type_left = skip_typeref(orig_type_left);
3031 type_t *type_right = skip_typeref(orig_type_right);
3033 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3034 /* TODO: improve error message */
3035 parser_print_error_prefix();
3036 fprintf(stderr, "operation needs arithmetic types\n");
3040 /* combined instructions are tricky. We can't create an implicit cast on
3041 * the left side, because we need the uncasted form for the store.
3042 * The ast2firm pass has to know that left_type must be right_type
3043 * for the arithmeitc operation and create a cast by itself */
3044 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3045 expression->right = create_implicit_cast(right, arithmetic_type);
3046 expression->expression.datatype = type_left;
3049 static void semantic_logical_op(binary_expression_t *expression)
3051 expression_t *left = expression->left;
3052 expression_t *right = expression->right;
3053 type_t *orig_type_left = left->datatype;
3054 type_t *orig_type_right = right->datatype;
3056 if(orig_type_left == NULL || orig_type_right == NULL)
3059 type_t *type_left = skip_typeref(orig_type_left);
3060 type_t *type_right = skip_typeref(orig_type_right);
3062 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3063 /* TODO: improve error message */
3064 parser_print_error_prefix();
3065 fprintf(stderr, "operation needs arithmetic types\n");
3069 expression->expression.datatype = type_int;
3072 static void semantic_binexpr_assign(binary_expression_t *expression)
3074 expression_t *left = expression->left;
3075 type_t *type_left = left->datatype;
3077 if(type_left != NULL) {
3078 semantic_assign(type_left, &expression->right, "assignment");
3081 expression->expression.datatype = type_left;
3084 static void semantic_comma(binary_expression_t *expression)
3086 expression->expression.datatype = expression->right->datatype;
3089 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3090 static expression_t *parse_##binexpression_type(unsigned precedence, \
3091 expression_t *left) \
3095 expression_t *right = parse_sub_expression(precedence + lr); \
3097 binary_expression_t *binexpr \
3098 = allocate_ast_zero(sizeof(binexpr[0])); \
3099 binexpr->expression.type = EXPR_BINARY; \
3100 binexpr->type = binexpression_type; \
3101 binexpr->left = left; \
3102 binexpr->right = right; \
3105 return (expression_t*) binexpr; \
3108 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3109 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3110 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3111 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3112 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3113 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3114 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3115 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3116 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3117 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3118 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3119 semantic_comparison, 1)
3120 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3121 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3122 semantic_comparison, 1)
3123 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3124 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3125 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3126 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3127 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3128 /* TODO shift has a bit special semantic */
3129 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3130 semantic_shift_op, 1)
3131 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3132 semantic_shift_op, 1)
3133 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3134 semantic_arithmetic_assign, 0)
3135 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3136 semantic_arithmetic_assign, 0)
3137 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3138 semantic_arithmetic_assign, 0)
3139 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3140 semantic_arithmetic_assign, 0)
3141 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3142 semantic_arithmetic_assign, 0)
3143 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3144 semantic_arithmetic_assign, 0)
3145 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3146 semantic_arithmetic_assign, 0)
3147 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3148 semantic_arithmetic_assign, 0)
3149 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3150 semantic_arithmetic_assign, 0)
3151 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3152 semantic_arithmetic_assign, 0)
3154 static expression_t *parse_sub_expression(unsigned precedence)
3156 if(token.type < 0) {
3157 return expected_expression_error();
3160 expression_parser_function_t *parser
3161 = &expression_parsers[token.type];
3162 source_position_t source_position = token.source_position;
3165 if(parser->parser != NULL) {
3166 left = parser->parser(parser->precedence);
3168 left = parse_primary_expression();
3170 assert(left != NULL);
3171 left->source_position = source_position;
3174 if(token.type < 0) {
3175 return expected_expression_error();
3178 parser = &expression_parsers[token.type];
3179 if(parser->infix_parser == NULL)
3181 if(parser->infix_precedence < precedence)
3184 left = parser->infix_parser(parser->infix_precedence, left);
3186 assert(left != NULL);
3187 assert(left->type != EXPR_UNKNOWN);
3188 left->source_position = source_position;
3194 static expression_t *parse_expression(void)
3196 return parse_sub_expression(1);
3201 static void register_expression_parser(parse_expression_function parser,
3202 int token_type, unsigned precedence)
3204 expression_parser_function_t *entry = &expression_parsers[token_type];
3206 if(entry->parser != NULL) {
3207 fprintf(stderr, "for token ");
3208 print_token_type(stderr, token_type);
3209 fprintf(stderr, "\n");
3210 panic("trying to register multiple expression parsers for a token");
3212 entry->parser = parser;
3213 entry->precedence = precedence;
3216 static void register_expression_infix_parser(
3217 parse_expression_infix_function parser, int token_type,
3218 unsigned precedence)
3220 expression_parser_function_t *entry = &expression_parsers[token_type];
3222 if(entry->infix_parser != NULL) {
3223 fprintf(stderr, "for token ");
3224 print_token_type(stderr, token_type);
3225 fprintf(stderr, "\n");
3226 panic("trying to register multiple infix expression parsers for a "
3229 entry->infix_parser = parser;
3230 entry->infix_precedence = precedence;
3233 static void init_expression_parsers(void)
3235 memset(&expression_parsers, 0, sizeof(expression_parsers));
3237 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3238 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3239 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3240 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3241 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3242 T_GREATERGREATER, 16);
3243 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3244 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3245 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3246 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3247 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3248 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3249 T_GREATEREQUAL, 14);
3250 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3251 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3252 T_EXCLAMATIONMARKEQUAL, 13);
3253 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3254 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3255 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3256 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3257 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3258 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3259 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3260 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3261 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3262 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3263 T_ASTERISKEQUAL, 2);
3264 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3265 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3267 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3268 T_LESSLESSEQUAL, 2);
3269 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3270 T_GREATERGREATEREQUAL, 2);
3271 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3273 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3275 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3278 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3280 register_expression_infix_parser(parse_array_expression, '[', 30);
3281 register_expression_infix_parser(parse_call_expression, '(', 30);
3282 register_expression_infix_parser(parse_select_expression, '.', 30);
3283 register_expression_infix_parser(parse_select_expression,
3284 T_MINUSGREATER, 30);
3285 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3287 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3290 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3291 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3292 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3293 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3294 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3295 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3296 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3297 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3298 register_expression_parser(parse_sizeof, T_sizeof, 25);
3299 register_expression_parser(parse_extension, T___extension__, 25);
3303 static statement_t *parse_case_statement(void)
3306 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3307 label->statement.type = STATEMENT_CASE_LABEL;
3308 label->statement.source_position = token.source_position;
3310 label->expression = parse_expression();
3313 label->statement.next = parse_statement();
3315 return (statement_t*) label;
3318 static statement_t *parse_default_statement(void)
3322 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3323 label->statement.type = STATEMENT_CASE_LABEL;
3324 label->statement.source_position = token.source_position;
3327 label->statement.next = parse_statement();
3329 return (statement_t*) label;
3332 static declaration_t *get_label(symbol_t *symbol)
3334 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3335 assert(current_function != NULL);
3336 /* if we found a label in the same function, then we already created the
3338 if(candidate != NULL
3339 && candidate->parent_context == ¤t_function->context) {
3343 /* otherwise we need to create a new one */
3344 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3345 declaration->namespace = NAMESPACE_LABEL;
3346 declaration->symbol = symbol;
3348 label_push(declaration);
3353 static statement_t *parse_label_statement(void)
3355 assert(token.type == T_IDENTIFIER);
3356 symbol_t *symbol = token.v.symbol;
3359 declaration_t *label = get_label(symbol);
3361 /* if source position is already set then the label is defined twice,
3362 * otherwise it was just mentioned in a goto so far */
3363 if(label->source_position.input_name != NULL) {
3364 parser_print_error_prefix();
3365 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3366 parser_print_error_prefix_pos(label->source_position);
3367 fprintf(stderr, "previous definition of '%s' was here\n",
3370 label->source_position = token.source_position;
3373 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3375 label_statement->statement.type = STATEMENT_LABEL;
3376 label_statement->statement.source_position = token.source_position;
3377 label_statement->label = label;
3381 if(token.type == '}') {
3382 parse_error("label at end of compound statement");
3383 return (statement_t*) label_statement;
3385 label_statement->label_statement = parse_statement();
3388 return (statement_t*) label_statement;
3391 static statement_t *parse_if(void)
3395 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3396 statement->statement.type = STATEMENT_IF;
3397 statement->statement.source_position = token.source_position;
3400 statement->condition = parse_expression();
3403 statement->true_statement = parse_statement();
3404 if(token.type == T_else) {
3406 statement->false_statement = parse_statement();
3409 return (statement_t*) statement;
3412 static statement_t *parse_switch(void)
3416 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3417 statement->statement.type = STATEMENT_SWITCH;
3418 statement->statement.source_position = token.source_position;
3421 statement->expression = parse_expression();
3423 statement->body = parse_statement();
3425 return (statement_t*) statement;
3428 static statement_t *parse_while(void)
3432 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3433 statement->statement.type = STATEMENT_WHILE;
3434 statement->statement.source_position = token.source_position;
3437 statement->condition = parse_expression();
3439 statement->body = parse_statement();
3441 return (statement_t*) statement;
3444 static statement_t *parse_do(void)
3448 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3449 statement->statement.type = STATEMENT_DO_WHILE;
3450 statement->statement.source_position = token.source_position;
3452 statement->body = parse_statement();
3455 statement->condition = parse_expression();
3459 return (statement_t*) statement;
3462 static statement_t *parse_for(void)
3466 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3467 statement->statement.type = STATEMENT_FOR;
3468 statement->statement.source_position = token.source_position;
3472 int top = environment_top();
3473 context_t *last_context = context;
3474 set_context(&statement->context);
3476 if(token.type != ';') {
3477 if(is_declaration_specifier(&token, false)) {
3478 parse_declaration();
3480 statement->initialisation = parse_expression();
3487 if(token.type != ';') {
3488 statement->condition = parse_expression();
3491 if(token.type != ')') {
3492 statement->step = parse_expression();
3495 statement->body = parse_statement();
3497 assert(context == &statement->context);
3498 set_context(last_context);
3499 environment_pop_to(top);
3501 return (statement_t*) statement;
3504 static statement_t *parse_goto(void)
3508 if(token.type != T_IDENTIFIER) {
3509 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3513 symbol_t *symbol = token.v.symbol;
3516 declaration_t *label = get_label(symbol);
3518 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3520 statement->statement.type = STATEMENT_GOTO;
3521 statement->statement.source_position = token.source_position;
3523 statement->label = label;
3527 return (statement_t*) statement;
3530 static statement_t *parse_continue(void)
3535 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3536 statement->type = STATEMENT_CONTINUE;
3537 statement->source_position = token.source_position;
3542 static statement_t *parse_break(void)
3547 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3548 statement->type = STATEMENT_BREAK;
3549 statement->source_position = token.source_position;
3554 static statement_t *parse_return(void)
3558 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3560 statement->statement.type = STATEMENT_RETURN;
3561 statement->statement.source_position = token.source_position;
3563 assert(current_function->type->type == TYPE_FUNCTION);
3564 function_type_t *function_type = (function_type_t*) current_function->type;
3565 type_t *return_type = function_type->result_type;
3567 expression_t *return_value;
3568 if(token.type != ';') {
3569 return_value = parse_expression();
3571 if(return_type == type_void && return_value->datatype != type_void) {
3572 parse_warning("'return' with a value, in function returning void");
3573 return_value = NULL;
3575 if(return_type != NULL) {
3576 semantic_assign(return_type, &return_value, "'return'");
3580 return_value = NULL;
3581 if(return_type != type_void) {
3582 parse_warning("'return' without value, in function returning "
3586 statement->return_value = return_value;
3590 return (statement_t*) statement;
3593 static statement_t *parse_declaration_statement(void)
3595 declaration_t *before = last_declaration;
3597 declaration_statement_t *statement
3598 = allocate_ast_zero(sizeof(statement[0]));
3599 statement->statement.type = STATEMENT_DECLARATION;
3600 statement->statement.source_position = token.source_position;
3602 declaration_specifiers_t specifiers;
3603 memset(&specifiers, 0, sizeof(specifiers));
3604 parse_declaration_specifiers(&specifiers);
3606 if(token.type == ';') {
3609 parse_init_declarators(&specifiers);
3612 if(before == NULL) {
3613 statement->declarations_begin = context->declarations;
3615 statement->declarations_begin = before->next;
3617 statement->declarations_end = last_declaration;
3619 return (statement_t*) statement;
3622 static statement_t *parse_expression_statement(void)
3624 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3625 statement->statement.type = STATEMENT_EXPRESSION;
3626 statement->statement.source_position = token.source_position;
3628 statement->expression = parse_expression();
3632 return (statement_t*) statement;
3635 static statement_t *parse_statement(void)
3637 statement_t *statement = NULL;
3639 /* declaration or statement */
3640 switch(token.type) {
3642 statement = parse_case_statement();
3646 statement = parse_default_statement();
3650 statement = parse_compound_statement();
3654 statement = parse_if();
3658 statement = parse_switch();
3662 statement = parse_while();
3666 statement = parse_do();
3670 statement = parse_for();
3674 statement = parse_goto();
3678 statement = parse_continue();
3682 statement = parse_break();
3686 statement = parse_return();
3695 if(look_ahead(1)->type == ':') {
3696 statement = parse_label_statement();
3700 if(is_typedef_symbol(token.v.symbol)) {
3701 statement = parse_declaration_statement();
3705 statement = parse_expression_statement();
3708 case T___extension__:
3709 /* this can be a prefix to a declaration or an expression statement */
3710 /* we simply eat it now and parse the rest with tail recursion */
3713 } while(token.type == T___extension__);
3714 statement = parse_statement();
3718 statement = parse_declaration_statement();
3722 statement = parse_expression_statement();
3726 assert(statement == NULL || statement->source_position.input_name != NULL);
3731 static statement_t *parse_compound_statement(void)
3733 compound_statement_t *compound_statement
3734 = allocate_ast_zero(sizeof(compound_statement[0]));
3735 compound_statement->statement.type = STATEMENT_COMPOUND;
3736 compound_statement->statement.source_position = token.source_position;
3740 int top = environment_top();
3741 context_t *last_context = context;
3742 set_context(&compound_statement->context);
3744 statement_t *last_statement = NULL;
3746 while(token.type != '}' && token.type != T_EOF) {
3747 statement_t *statement = parse_statement();
3748 if(statement == NULL)
3751 if(last_statement != NULL) {
3752 last_statement->next = statement;
3754 compound_statement->statements = statement;
3757 while(statement->next != NULL)
3758 statement = statement->next;
3760 last_statement = statement;
3763 if(token.type != '}') {
3764 parser_print_error_prefix_pos(
3765 compound_statement->statement.source_position);
3766 fprintf(stderr, "end of file while looking for closing '}'\n");
3770 assert(context == &compound_statement->context);
3771 set_context(last_context);
3772 environment_pop_to(top);
3774 return (statement_t*) compound_statement;
3777 static translation_unit_t *parse_translation_unit(void)
3779 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3781 assert(global_context == NULL);
3782 global_context = &unit->context;
3784 assert(context == NULL);
3785 set_context(&unit->context);
3787 while(token.type != T_EOF) {
3788 parse_declaration();
3791 assert(context == &unit->context);
3793 last_declaration = NULL;
3795 assert(global_context == &unit->context);
3796 global_context = NULL;
3801 translation_unit_t *parse(void)
3803 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3804 label_stack = NEW_ARR_F(stack_entry_t, 0);
3805 found_error = false;
3807 type_set_output(stderr);
3808 ast_set_output(stderr);
3810 lookahead_bufpos = 0;
3811 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3814 translation_unit_t *unit = parse_translation_unit();
3816 DEL_ARR_F(environment_stack);
3817 DEL_ARR_F(label_stack);
3825 void init_parser(void)
3827 init_expression_parsers();
3828 obstack_init(&temp_obst);
3830 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3831 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3832 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3833 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3834 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3835 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3836 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3837 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3838 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3839 type_string = make_pointer_type(type_const_char, 0);
3842 void exit_parser(void)
3844 obstack_free(&temp_obst, NULL);