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;
510 declaration_t *iter_last = declaration;
511 declaration_t *iter = declaration->symbol_next;
512 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
513 /* replace an entry? */
514 if(iter->namespace == namespace) {
515 assert(iter_last != NULL);
516 iter_last->symbol_next = old_declaration;
517 old_declaration->symbol_next = iter->symbol_next;
521 assert(iter != NULL);
525 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
528 static void environment_pop_to(size_t new_top)
530 stack_pop_to(&environment_stack, new_top);
533 static void label_pop_to(size_t new_top)
535 stack_pop_to(&label_stack, new_top);
539 static int get_rank(const type_t *type)
541 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
542 * and esp. footnote 108). However we can't fold constants (yet), so we
543 * can't decide wether unsigned int is possible, while int always works.
544 * (unsigned int would be preferable when possible... for stuff like
545 * struct { enum { ... } bla : 4; } ) */
546 if(type->type == TYPE_ENUM)
547 return ATOMIC_TYPE_INT;
549 assert(type->type == TYPE_ATOMIC);
550 atomic_type_t *atomic_type = (atomic_type_t*) type;
551 atomic_type_type_t atype = atomic_type->atype;
555 static type_t *promote_integer(type_t *type)
557 if(get_rank(type) < ATOMIC_TYPE_INT)
563 static expression_t *create_cast_expression(expression_t *expression,
566 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
568 cast->expression.type = EXPR_UNARY;
569 cast->type = UNEXPR_CAST;
570 cast->value = expression;
571 cast->expression.datatype = dest_type;
573 return (expression_t*) cast;
576 static expression_t *create_implicit_cast(expression_t *expression,
579 type_t *source_type = expression->datatype;
581 if(source_type == NULL)
584 source_type = skip_typeref(source_type);
585 dest_type = skip_typeref(dest_type);
587 if(source_type == dest_type)
590 if(dest_type->type == TYPE_ATOMIC) {
591 if(source_type->type != TYPE_ATOMIC)
592 panic("casting of non-atomic types not implemented yet");
594 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
595 type_error_incompatible("can't cast types",
596 expression->source_position,
597 source_type, dest_type);
601 return create_cast_expression(expression, dest_type);
603 if(dest_type->type == TYPE_POINTER) {
604 pointer_type_t *pointer_type
605 = (pointer_type_t*) dest_type;
606 if(source_type->type == TYPE_POINTER) {
607 if(!pointers_compatible(source_type, dest_type)) {
608 type_error_incompatible("can't implicitely cast types",
609 expression->source_position,
610 source_type, dest_type);
613 return create_cast_expression(expression, dest_type);
615 } else if(source_type->type == TYPE_ARRAY) {
616 array_type_t *array_type = (array_type_t*) source_type;
617 if(!types_compatible(array_type->element_type,
618 pointer_type->points_to)) {
619 type_error_incompatible("can't implicitely cast types",
620 expression->source_position,
621 source_type, dest_type);
624 return create_cast_expression(expression, dest_type);
628 panic("casting of non-atomic types not implemented yet");
631 static void semantic_assign(type_t *orig_type_left, expression_t **right,
634 type_t *orig_type_right = (*right)->datatype;
636 if(orig_type_right == NULL)
639 type_t *type_left = skip_typeref(orig_type_left);
640 type_t *type_right = skip_typeref(orig_type_right);
642 if(type_left == type_right) {
644 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
645 *right = create_implicit_cast(*right, type_left);
646 } else if(type_left->type == TYPE_POINTER
647 && type_right->type == TYPE_POINTER) {
650 /* TODO: improve error message */
651 parser_print_error_prefix();
652 fprintf(stderr, "incompatible types in %s\n", context);
653 parser_print_error_prefix();
654 print_type_quoted(type_left);
655 fputs(" <- ", stderr);
656 print_type_quoted(type_right);
662 static expression_t *parse_constant_expression(void)
664 /* start parsing at precedence 7 (conditional expression) */
665 return parse_sub_expression(7);
668 static expression_t *parse_assignment_expression(void)
670 /* start parsing at precedence 2 (assignment expression) */
671 return parse_sub_expression(2);
674 typedef struct declaration_specifiers_t declaration_specifiers_t;
675 struct declaration_specifiers_t {
676 storage_class_t storage_class;
681 static void parse_compound_type_entries(void);
682 static declaration_t *parse_declarator(
683 const declaration_specifiers_t *specifiers, type_t *type,
684 bool may_be_abstract);
685 static declaration_t *record_declaration(declaration_t *declaration);
687 static const char *parse_string_literals(void)
689 assert(token.type == T_STRING_LITERAL);
690 const char *result = token.v.string;
694 while(token.type == T_STRING_LITERAL) {
695 result = concat_strings(result, token.v.string);
702 static void parse_attributes(void)
706 case T___attribute__:
714 parse_error("EOF while parsing attribute");
732 if(token.type != T_STRING_LITERAL) {
733 parse_error_expected("while parsing assembler attribute",
738 parse_string_literals();
743 goto attributes_finished;
751 static designator_t *parse_designation(void)
753 if(token.type != '[' && token.type != '.')
756 designator_t *result = NULL;
757 designator_t *last = NULL;
760 designator_t *designator;
763 designator = allocate_ast_zero(sizeof(designator[0]));
765 designator->array_access = parse_constant_expression();
769 designator = allocate_ast_zero(sizeof(designator[0]));
771 if(token.type != T_IDENTIFIER) {
772 parse_error_expected("while parsing designator",
776 designator->symbol = token.v.symbol;
784 assert(designator != NULL);
786 last->next = designator;
794 static initializer_t *parse_initializer_list(type_t *type);
796 static initializer_t *parse_initializer(type_t *type)
798 designator_t *designator = parse_designation();
800 initializer_t *result;
801 if(token.type == '{') {
802 result = parse_initializer_list(type);
804 result = allocate_ast_zero(sizeof(result[0]));
805 result->type = INITIALIZER_VALUE;
806 result->v.value = parse_assignment_expression();
809 semantic_assign(type, &result->v.value, "initializer");
812 result->designator = designator;
817 static initializer_t *parse_initializer_list(type_t *type)
824 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
825 result->type = INITIALIZER_LIST;
827 initializer_t *last = NULL;
829 initializer_t *initializer = parse_initializer(NULL);
831 last->next = initializer;
833 result->v.list = initializer;
837 if(token.type == '}')
840 if(token.type != ',') {
841 parse_error_expected("while parsing initializer list", ',', '}', 0);
847 if(token.type == '}')
856 static declaration_t *parse_compound_type_specifier(bool is_struct)
864 symbol_t *symbol = NULL;
865 declaration_t *declaration = NULL;
867 if(token.type == T_IDENTIFIER) {
868 symbol = token.v.symbol;
872 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
874 declaration = get_declaration(symbol, NAMESPACE_UNION);
876 } else if(token.type != '{') {
878 parse_error_expected("while parsing struct type specifier",
879 T_IDENTIFIER, '{', 0);
881 parse_error_expected("while parsing union type specifier",
882 T_IDENTIFIER, '{', 0);
888 if(declaration == NULL) {
889 declaration = allocate_type_zero(sizeof(declaration[0]));
892 declaration->namespace = NAMESPACE_STRUCT;
894 declaration->namespace = NAMESPACE_UNION;
896 declaration->source_position = token.source_position;
897 declaration->symbol = symbol;
898 record_declaration(declaration);
901 if(token.type == '{') {
902 if(declaration->init.is_defined) {
903 assert(symbol != NULL);
904 parser_print_error_prefix();
905 fprintf(stderr, "multiple definition of %s %s\n",
906 is_struct ? "struct" : "union", symbol->string);
907 declaration->context.declarations = NULL;
909 declaration->init.is_defined = true;
911 int top = environment_top();
912 context_t *last_context = context;
913 set_context(& declaration->context);
915 parse_compound_type_entries();
918 assert(context == & declaration->context);
919 set_context(last_context);
920 environment_pop_to(top);
926 static void parse_enum_entries(void)
930 if(token.type == '}') {
932 parse_error("empty enum not allowed");
937 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
939 if(token.type != T_IDENTIFIER) {
940 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
944 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
945 entry->symbol = token.v.symbol;
946 entry->source_position = token.source_position;
949 if(token.type == '=') {
951 entry->init.initializer = parse_initializer(type_int);
954 record_declaration(entry);
956 if(token.type != ',')
959 } while(token.type != '}');
964 static declaration_t *parse_enum_specifier(void)
968 declaration_t *declaration;
971 if(token.type == T_IDENTIFIER) {
972 symbol = token.v.symbol;
975 declaration = get_declaration(symbol, NAMESPACE_ENUM);
976 } else if(token.type != '{') {
977 parse_error_expected("while parsing enum type specifier",
978 T_IDENTIFIER, '{', 0);
985 if(declaration == NULL) {
986 declaration = allocate_type_zero(sizeof(declaration[0]));
988 declaration->namespace = NAMESPACE_ENUM;
989 declaration->source_position = token.source_position;
990 declaration->symbol = symbol;
993 if(token.type == '{') {
994 if(declaration->init.is_defined) {
995 parser_print_error_prefix();
996 fprintf(stderr, "multiple definitions of enum %s\n",
999 record_declaration(declaration);
1000 declaration->init.is_defined = 1;
1002 parse_enum_entries();
1010 * if a symbol is a typedef to another type, return true
1012 static bool is_typedef_symbol(symbol_t *symbol)
1014 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1015 if(declaration == NULL
1016 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1022 static type_t *parse_typeof(void)
1030 expression_t *expression = NULL;
1033 switch(token.type) {
1034 case T___extension__:
1035 /* this can be a prefix to a typename or an expression */
1036 /* we simply eat it now. */
1039 } while(token.type == T___extension__);
1043 if(is_typedef_symbol(token.v.symbol)) {
1044 type = parse_typename();
1046 expression = parse_expression();
1047 type = expression->datatype;
1052 type = parse_typename();
1056 expression = parse_expression();
1057 type = expression->datatype;
1063 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1064 typeof->type.type = TYPE_TYPEOF;
1065 typeof->expression = expression;
1066 typeof->typeof_type = type;
1068 return (type_t*) typeof;
1072 SPECIFIER_SIGNED = 1 << 0,
1073 SPECIFIER_UNSIGNED = 1 << 1,
1074 SPECIFIER_LONG = 1 << 2,
1075 SPECIFIER_INT = 1 << 3,
1076 SPECIFIER_DOUBLE = 1 << 4,
1077 SPECIFIER_CHAR = 1 << 5,
1078 SPECIFIER_SHORT = 1 << 6,
1079 SPECIFIER_LONG_LONG = 1 << 7,
1080 SPECIFIER_FLOAT = 1 << 8,
1081 SPECIFIER_BOOL = 1 << 9,
1082 SPECIFIER_VOID = 1 << 10,
1083 #ifdef PROVIDE_COMPLEX
1084 SPECIFIER_COMPLEX = 1 << 11,
1086 #ifdef PROVIDE_IMAGINARY
1087 SPECIFIER_IMAGINARY = 1 << 12,
1091 static type_t *create_builtin_type(symbol_t *symbol)
1093 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1094 type->type.type = TYPE_BUILTIN;
1095 type->symbol = symbol;
1097 type->real_type = type_int;
1099 return (type_t*) type;
1102 static type_t *get_typedef_type(symbol_t *symbol)
1104 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1105 if(declaration == NULL
1106 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1109 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1110 typedef_type->type.type = TYPE_TYPEDEF;
1111 typedef_type->declaration = declaration;
1113 return (type_t*) typedef_type;
1116 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1118 type_t *type = NULL;
1119 unsigned type_qualifiers = 0;
1120 unsigned type_specifiers = 0;
1124 switch(token.type) {
1127 #define MATCH_STORAGE_CLASS(token, class) \
1129 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1130 parse_error("multiple storage classes in declaration " \
1133 specifiers->storage_class = class; \
1137 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1138 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1139 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1140 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1141 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1143 /* type qualifiers */
1144 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1146 type_qualifiers |= qualifier; \
1150 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1151 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1152 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1154 case T___extension__:
1159 /* type specifiers */
1160 #define MATCH_SPECIFIER(token, specifier, name) \
1163 if(type_specifiers & specifier) { \
1164 parse_error("multiple " name " type specifiers given"); \
1166 type_specifiers |= specifier; \
1170 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1171 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1172 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1173 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1174 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1175 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1176 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1177 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1178 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1179 #ifdef PROVIDE_COMPLEX
1180 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1182 #ifdef PROVIDE_IMAGINARY
1183 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1187 specifiers->is_inline = true;
1192 if(type_specifiers & SPECIFIER_LONG_LONG) {
1193 parse_error("multiple type specifiers given");
1194 } else if(type_specifiers & SPECIFIER_LONG) {
1195 type_specifiers |= SPECIFIER_LONG_LONG;
1197 type_specifiers |= SPECIFIER_LONG;
1201 /* TODO: if type != NULL for the following rules should issue
1204 compound_type_t *compound_type
1205 = allocate_type_zero(sizeof(compound_type[0]));
1206 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1207 compound_type->declaration = parse_compound_type_specifier(true);
1209 type = (type_t*) compound_type;
1213 compound_type_t *compound_type
1214 = allocate_type_zero(sizeof(compound_type[0]));
1215 compound_type->type.type = TYPE_COMPOUND_UNION;
1216 compound_type->declaration = parse_compound_type_specifier(false);
1218 type = (type_t*) compound_type;
1222 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1223 enum_type->type.type = TYPE_ENUM;
1224 enum_type->declaration = parse_enum_specifier();
1226 type = (type_t*) enum_type;
1230 type = parse_typeof();
1232 case T___builtin_va_list:
1233 type = create_builtin_type(token.v.symbol);
1237 case T___attribute__:
1242 case T_IDENTIFIER: {
1243 type_t *typedef_type = get_typedef_type(token.v.symbol);
1245 if(typedef_type == NULL)
1246 goto finish_specifiers;
1249 type = typedef_type;
1253 /* function specifier */
1255 goto finish_specifiers;
1262 atomic_type_type_t atomic_type;
1264 /* match valid basic types */
1265 switch(type_specifiers) {
1266 case SPECIFIER_VOID:
1267 atomic_type = ATOMIC_TYPE_VOID;
1269 case SPECIFIER_CHAR:
1270 atomic_type = ATOMIC_TYPE_CHAR;
1272 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1273 atomic_type = ATOMIC_TYPE_SCHAR;
1275 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1276 atomic_type = ATOMIC_TYPE_UCHAR;
1278 case SPECIFIER_SHORT:
1279 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1280 case SPECIFIER_SHORT | SPECIFIER_INT:
1281 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1282 atomic_type = ATOMIC_TYPE_SHORT;
1284 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1285 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1286 atomic_type = ATOMIC_TYPE_USHORT;
1289 case SPECIFIER_SIGNED:
1290 case SPECIFIER_SIGNED | SPECIFIER_INT:
1291 atomic_type = ATOMIC_TYPE_INT;
1293 case SPECIFIER_UNSIGNED:
1294 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1295 atomic_type = ATOMIC_TYPE_UINT;
1297 case SPECIFIER_LONG:
1298 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1299 case SPECIFIER_LONG | SPECIFIER_INT:
1300 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1301 atomic_type = ATOMIC_TYPE_LONG;
1303 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1304 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1305 atomic_type = ATOMIC_TYPE_ULONG;
1307 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1308 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1309 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1310 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1312 atomic_type = ATOMIC_TYPE_LONGLONG;
1314 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1315 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1317 atomic_type = ATOMIC_TYPE_ULONGLONG;
1319 case SPECIFIER_FLOAT:
1320 atomic_type = ATOMIC_TYPE_FLOAT;
1322 case SPECIFIER_DOUBLE:
1323 atomic_type = ATOMIC_TYPE_DOUBLE;
1325 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1326 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1328 case SPECIFIER_BOOL:
1329 atomic_type = ATOMIC_TYPE_BOOL;
1331 #ifdef PROVIDE_COMPLEX
1332 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1333 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1335 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1336 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1338 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1339 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1342 #ifdef PROVIDE_IMAGINARY
1343 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1344 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1346 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1347 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1349 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1350 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1354 /* invalid specifier combination, give an error message */
1355 if(type_specifiers == 0) {
1357 parse_warning("no type specifiers in declaration (using int)");
1358 atomic_type = ATOMIC_TYPE_INT;
1361 parse_error("no type specifiers given in declaration");
1363 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1364 (type_specifiers & SPECIFIER_UNSIGNED)) {
1365 parse_error("signed and unsigned specifiers gives");
1366 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1367 parse_error("only integer types can be signed or unsigned");
1369 parse_error("multiple datatypes in declaration");
1371 atomic_type = ATOMIC_TYPE_INVALID;
1374 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1375 atype->type.type = TYPE_ATOMIC;
1376 atype->atype = atomic_type;
1379 type = (type_t*) atype;
1381 if(type_specifiers != 0) {
1382 parse_error("multiple datatypes in declaration");
1386 type->qualifiers = type_qualifiers;
1388 type_t *result = typehash_insert(type);
1389 if(newtype && result != (type_t*) type) {
1393 specifiers->type = result;
1396 static type_qualifier_t parse_type_qualifiers(void)
1398 type_qualifier_t type_qualifiers = 0;
1401 switch(token.type) {
1402 /* type qualifiers */
1403 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1404 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1405 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1408 return type_qualifiers;
1413 static void parse_identifier_list(void)
1416 if(token.type != T_IDENTIFIER) {
1417 parse_error_expected("while parsing parameter identifier list",
1422 if(token.type != ',')
1428 static declaration_t *parse_parameter(void)
1430 declaration_specifiers_t specifiers;
1431 memset(&specifiers, 0, sizeof(specifiers));
1433 parse_declaration_specifiers(&specifiers);
1435 declaration_t *declaration
1436 = parse_declarator(&specifiers, specifiers.type, true);
1438 /* TODO check declaration constraints for parameters */
1439 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1440 parse_error("typedef not allowed in parameter list");
1446 static declaration_t *parse_parameters(function_type_t *type)
1448 if(token.type == T_IDENTIFIER) {
1449 symbol_t *symbol = token.v.symbol;
1450 if(!is_typedef_symbol(symbol)) {
1452 parse_identifier_list();
1457 if(token.type == ')') {
1458 type->unspecified_parameters = 1;
1461 if(token.type == T_void && look_ahead(1)->type == ')') {
1466 declaration_t *declarations = NULL;
1467 declaration_t *declaration;
1468 declaration_t *last_declaration = NULL;
1469 function_parameter_t *parameter;
1470 function_parameter_t *last_parameter = NULL;
1473 switch(token.type) {
1477 return declarations;
1480 case T___extension__:
1482 declaration = parse_parameter();
1484 parameter = allocate_type_zero(sizeof(parameter[0]));
1485 parameter->type = declaration->type;
1487 if(last_parameter != NULL) {
1488 last_declaration->next = declaration;
1489 last_parameter->next = parameter;
1491 type->parameters = parameter;
1492 declarations = declaration;
1494 last_parameter = parameter;
1495 last_declaration = declaration;
1499 return declarations;
1501 if(token.type != ',')
1502 return declarations;
1512 } construct_type_type_t;
1514 typedef struct construct_type_t construct_type_t;
1515 struct construct_type_t {
1516 construct_type_type_t type;
1517 construct_type_t *next;
1520 typedef struct parsed_pointer_t parsed_pointer_t;
1521 struct parsed_pointer_t {
1522 construct_type_t construct_type;
1523 type_qualifier_t type_qualifiers;
1526 typedef struct construct_function_type_t construct_function_type_t;
1527 struct construct_function_type_t {
1528 construct_type_t construct_type;
1529 function_type_t *function_type;
1532 typedef struct parsed_array_t parsed_array_t;
1533 struct parsed_array_t {
1534 construct_type_t construct_type;
1535 type_qualifier_t type_qualifiers;
1541 typedef struct construct_base_type_t construct_base_type_t;
1542 struct construct_base_type_t {
1543 construct_type_t construct_type;
1547 static construct_type_t *parse_pointer_declarator(void)
1551 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1552 memset(pointer, 0, sizeof(pointer[0]));
1553 pointer->construct_type.type = CONSTRUCT_POINTER;
1554 pointer->type_qualifiers = parse_type_qualifiers();
1556 return (construct_type_t*) pointer;
1559 static construct_type_t *parse_array_declarator(void)
1563 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1564 memset(array, 0, sizeof(array[0]));
1565 array->construct_type.type = CONSTRUCT_ARRAY;
1567 if(token.type == T_static) {
1568 array->is_static = true;
1572 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1573 if(type_qualifiers != 0) {
1574 if(token.type == T_static) {
1575 array->is_static = true;
1579 array->type_qualifiers = type_qualifiers;
1581 if(token.type == '*' && look_ahead(1)->type == ']') {
1582 array->is_variable = true;
1584 } else if(token.type != ']') {
1585 array->size = parse_assignment_expression();
1590 return (construct_type_t*) array;
1593 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1597 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1598 type->type.type = TYPE_FUNCTION;
1600 declaration_t *parameters = parse_parameters(type);
1601 if(declaration != NULL) {
1602 declaration->context.declarations = parameters;
1605 construct_function_type_t *construct_function_type =
1606 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1607 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1608 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1609 construct_function_type->function_type = type;
1613 return (construct_type_t*) construct_function_type;
1616 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1617 int may_be_abstract)
1619 construct_type_t *result = NULL;
1620 construct_type_t *last = NULL;
1622 while(token.type == '*') {
1623 construct_type_t *type = parse_pointer_declarator();
1632 /* TODO: find out if this is correct */
1635 construct_type_t *inner_types = NULL;
1637 switch(token.type) {
1639 if(declaration == NULL) {
1640 parse_error("no identifier expected in typename");
1642 declaration->symbol = token.v.symbol;
1643 declaration->source_position = token.source_position;
1649 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1655 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
1659 construct_type_t *type;
1660 switch(token.type) {
1662 type = parse_function_declarator(declaration);
1665 type = parse_array_declarator();
1668 goto declarator_finished;
1679 declarator_finished:
1682 if(inner_types != NULL) {
1684 last->next = inner_types;
1686 result = inner_types;
1694 static type_t *construct_declarator_type(construct_type_t *construct_list,
1697 construct_type_t *iter = construct_list;
1698 for( ; iter != NULL; iter = iter->next) {
1699 parsed_pointer_t *parsed_pointer;
1700 parsed_array_t *parsed_array;
1701 construct_function_type_t *construct_function_type;
1702 function_type_t *function_type;
1703 pointer_type_t *pointer_type;
1704 array_type_t *array_type;
1706 switch(iter->type) {
1707 case CONSTRUCT_INVALID:
1708 panic("invalid type construction found");
1709 case CONSTRUCT_FUNCTION:
1710 construct_function_type = (construct_function_type_t*) iter;
1711 function_type = construct_function_type->function_type;
1713 function_type->result_type = type;
1714 type = (type_t*) function_type;
1717 case CONSTRUCT_POINTER:
1718 parsed_pointer = (parsed_pointer_t*) iter;
1719 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1721 pointer_type->type.type = TYPE_POINTER;
1722 pointer_type->points_to = type;
1723 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1724 type = (type_t*) pointer_type;
1727 case CONSTRUCT_ARRAY:
1728 parsed_array = (parsed_array_t*) iter;
1729 array_type = allocate_type_zero(sizeof(array_type[0]));
1731 array_type->type.type = TYPE_ARRAY;
1732 array_type->element_type = type;
1733 array_type->type.qualifiers = parsed_array->type_qualifiers;
1734 array_type->is_static = parsed_array->is_static;
1735 array_type->is_variable = parsed_array->is_variable;
1736 array_type->size = parsed_array->size;
1737 type = (type_t*) array_type;
1741 type_t *hashed_type = typehash_insert((type_t*) type);
1742 if(hashed_type != type) {
1751 static declaration_t *parse_declarator(
1752 const declaration_specifiers_t *specifiers,
1753 type_t *type, bool may_be_abstract)
1755 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1756 declaration->storage_class = specifiers->storage_class;
1757 declaration->is_inline = specifiers->is_inline;
1759 construct_type_t *construct_type
1760 = parse_inner_declarator(declaration, may_be_abstract);
1761 declaration->type = construct_declarator_type(construct_type, type);
1763 if(construct_type != NULL) {
1764 obstack_free(&temp_obst, construct_type);
1770 static type_t *parse_abstract_declarator(type_t *base_type)
1772 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1774 type_t *result = construct_declarator_type(construct_type, base_type);
1775 if(construct_type != NULL) {
1776 obstack_free(&temp_obst, construct_type);
1782 static declaration_t *record_declaration(declaration_t *declaration)
1784 assert(context != NULL);
1786 symbol_t *symbol = declaration->symbol;
1787 if(symbol != NULL) {
1788 declaration_t *alias = environment_push(declaration);
1789 if(alias != declaration)
1792 declaration->parent_context = context;
1795 if(last_declaration != NULL) {
1796 last_declaration->next = declaration;
1798 context->declarations = declaration;
1800 last_declaration = declaration;
1805 static void parser_error_multiple_definition(declaration_t *previous,
1806 declaration_t *declaration)
1808 parser_print_error_prefix_pos(declaration->source_position);
1809 fprintf(stderr, "multiple definition of symbol '%s'\n",
1810 declaration->symbol->string);
1811 parser_print_error_prefix_pos(previous->source_position);
1812 fprintf(stderr, "this is the location of the previous "
1817 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1820 declaration_t *ndeclaration
1821 = parse_declarator(specifiers, specifiers->type, false);
1823 declaration_t *declaration = record_declaration(ndeclaration);
1825 type_t *type = declaration->type;
1826 if(type->type != TYPE_FUNCTION && declaration->is_inline) {
1827 parser_print_warning_prefix_pos(declaration->source_position);
1828 fprintf(stderr, "variable ‘%s’ declared ‘inline’\n",
1829 declaration->symbol->string);
1832 if(token.type == '=') {
1835 /* TODO: check that this is an allowed type (no function type) */
1837 if(declaration->init.initializer != NULL) {
1838 parser_error_multiple_definition(declaration, ndeclaration);
1841 ndeclaration->init.initializer = parse_initializer(declaration->type);
1842 } else if(token.type == '{') {
1843 if(declaration->type->type != TYPE_FUNCTION) {
1844 parser_print_error_prefix();
1845 fprintf(stderr, "Declarator ");
1846 print_type_ext(declaration->type, declaration->symbol, NULL);
1847 fprintf(stderr, " has a body but is not a function type.\n");
1852 if(declaration->init.statement != NULL) {
1853 parser_error_multiple_definition(declaration, ndeclaration);
1855 if(ndeclaration != declaration) {
1856 memcpy(&declaration->context, &ndeclaration->context,
1857 sizeof(declaration->context));
1860 int top = environment_top();
1861 context_t *last_context = context;
1862 set_context(&declaration->context);
1864 /* push function parameters */
1865 declaration_t *parameter = declaration->context.declarations;
1866 for( ; parameter != NULL; parameter = parameter->next) {
1867 environment_push(parameter);
1870 int label_stack_top = label_top();
1871 declaration_t *old_current_function = current_function;
1872 current_function = declaration;
1874 statement_t *statement = parse_compound_statement();
1876 assert(current_function == declaration);
1877 current_function = old_current_function;
1878 label_pop_to(label_stack_top);
1880 assert(context == &declaration->context);
1881 set_context(last_context);
1882 environment_pop_to(top);
1884 declaration->init.statement = statement;
1888 if(token.type != ',')
1895 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1898 if(token.type == ':') {
1900 parse_constant_expression();
1901 /* TODO (bitfields) */
1903 declaration_t *declaration
1904 = parse_declarator(specifiers, specifiers->type, true);
1906 /* TODO: check constraints for struct declarations */
1907 /* TODO: check for doubled fields */
1908 record_declaration(declaration);
1910 if(token.type == ':') {
1912 parse_constant_expression();
1913 /* TODO (bitfields) */
1917 if(token.type != ',')
1924 static void parse_compound_type_entries(void)
1928 while(token.type != '}' && token.type != T_EOF) {
1929 declaration_specifiers_t specifiers;
1930 memset(&specifiers, 0, sizeof(specifiers));
1931 parse_declaration_specifiers(&specifiers);
1933 parse_struct_declarators(&specifiers);
1935 if(token.type == T_EOF) {
1936 parse_error("unexpected error while parsing struct");
1941 static void parse_declaration(void)
1943 source_position_t source_position = token.source_position;
1945 declaration_specifiers_t specifiers;
1946 memset(&specifiers, 0, sizeof(specifiers));
1947 parse_declaration_specifiers(&specifiers);
1949 if(token.type == ';') {
1952 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1954 declaration->type = specifiers.type;
1955 declaration->storage_class = specifiers.storage_class;
1956 declaration->source_position = source_position;
1957 record_declaration(declaration);
1960 parse_init_declarators(&specifiers);
1963 static type_t *parse_typename(void)
1965 declaration_specifiers_t specifiers;
1966 memset(&specifiers, 0, sizeof(specifiers));
1967 parse_declaration_specifiers(&specifiers);
1968 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1969 /* TODO: improve error message, user does probably not know what a
1970 * storage class is...
1972 parse_error("typename may not have a storage class");
1975 type_t *result = parse_abstract_declarator(specifiers.type);
1983 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1984 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1985 expression_t *left);
1987 typedef struct expression_parser_function_t expression_parser_function_t;
1988 struct expression_parser_function_t {
1989 unsigned precedence;
1990 parse_expression_function parser;
1991 unsigned infix_precedence;
1992 parse_expression_infix_function infix_parser;
1995 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1997 static expression_t *make_invalid_expression(void)
1999 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2000 expression->type = EXPR_INVALID;
2001 expression->source_position = token.source_position;
2005 static expression_t *expected_expression_error(void)
2007 parser_print_error_prefix();
2008 fprintf(stderr, "expected expression, got token ");
2009 print_token(stderr, & token);
2010 fprintf(stderr, "\n");
2014 return make_invalid_expression();
2017 static expression_t *parse_string_const(void)
2019 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2021 cnst->expression.type = EXPR_STRING_LITERAL;
2022 cnst->expression.datatype = type_string;
2023 cnst->value = parse_string_literals();
2025 return (expression_t*) cnst;
2028 static expression_t *parse_int_const(void)
2030 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2032 cnst->expression.type = EXPR_CONST;
2033 cnst->expression.datatype = type_int;
2034 cnst->v.int_value = token.v.intvalue;
2038 return (expression_t*) cnst;
2041 static expression_t *parse_float_const(void)
2043 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
2045 cnst->expression.type = EXPR_CONST;
2046 cnst->expression.datatype = type_double;
2047 cnst->v.float_value = token.v.floatvalue;
2051 return (expression_t*) cnst;
2054 static declaration_t *create_implicit_function(symbol_t *symbol,
2055 const source_position_t source_position)
2057 function_type_t *function_type
2058 = allocate_type_zero(sizeof(function_type[0]));
2060 function_type->type.type = TYPE_FUNCTION;
2061 function_type->result_type = type_int;
2062 function_type->unspecified_parameters = true;
2064 type_t *type = typehash_insert((type_t*) function_type);
2065 if(type != (type_t*) function_type) {
2066 free_type(function_type);
2069 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2071 declaration->storage_class = STORAGE_CLASS_EXTERN;
2072 declaration->type = type;
2073 declaration->symbol = symbol;
2074 declaration->source_position = source_position;
2076 /* prepend the implicit definition to the global context
2077 * this is safe since the symbol wasn't declared as anything else yet
2079 assert(symbol->declaration == NULL);
2081 context_t *last_context = context;
2082 context = global_context;
2084 environment_push(declaration);
2085 declaration->next = context->declarations;
2086 context->declarations = declaration;
2088 context = last_context;
2093 static expression_t *parse_reference(void)
2095 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2097 ref->expression.type = EXPR_REFERENCE;
2098 ref->symbol = token.v.symbol;
2100 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2102 source_position_t source_position = token.source_position;
2105 if(declaration == NULL) {
2107 /* an implicitely defined function */
2108 if(token.type == '(') {
2109 parser_print_prefix_pos(token.source_position);
2110 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2111 ref->symbol->string);
2113 declaration = create_implicit_function(ref->symbol,
2118 parser_print_error_prefix();
2119 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2120 return (expression_t*) ref;
2124 ref->declaration = declaration;
2125 ref->expression.datatype = declaration->type;
2127 return (expression_t*) ref;
2130 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2134 /* TODO check if explicit cast is allowed and issue warnings/errors */
2137 static expression_t *parse_cast(void)
2139 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2141 cast->expression.type = EXPR_UNARY;
2142 cast->type = UNEXPR_CAST;
2143 cast->expression.source_position = token.source_position;
2145 type_t *type = parse_typename();
2148 expression_t *value = parse_sub_expression(20);
2150 check_cast_allowed(value, type);
2152 cast->expression.datatype = type;
2153 cast->value = value;
2155 return (expression_t*) cast;
2158 static expression_t *parse_statement_expression(void)
2160 statement_expression_t *expression
2161 = allocate_ast_zero(sizeof(expression[0]));
2162 expression->expression.type = EXPR_STATEMENT;
2164 statement_t *statement = parse_compound_statement();
2165 expression->statement = statement;
2166 if(statement == NULL) {
2171 assert(statement->type == STATEMENT_COMPOUND);
2172 compound_statement_t *compound_statement
2173 = (compound_statement_t*) statement;
2175 /* find last statement and use it's type */
2176 const statement_t *last_statement = NULL;
2177 const statement_t *iter = compound_statement->statements;
2178 for( ; iter != NULL; iter = iter->next) {
2179 last_statement = iter;
2182 if(last_statement->type == STATEMENT_EXPRESSION) {
2183 const expression_statement_t *expression_statement =
2184 (const expression_statement_t*) last_statement;
2185 expression->expression.datatype
2186 = expression_statement->expression->datatype;
2188 expression->expression.datatype = type_void;
2193 return (expression_t*) expression;
2196 static expression_t *parse_brace_expression(void)
2200 switch(token.type) {
2202 /* gcc extension: a stement expression */
2203 return parse_statement_expression();
2207 return parse_cast();
2209 if(is_typedef_symbol(token.v.symbol)) {
2210 return parse_cast();
2214 expression_t *result = parse_expression();
2220 static expression_t *parse_function_keyword(void)
2222 eat(T___FUNCTION__);
2225 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2226 expression->expression.type = EXPR_FUNCTION;
2227 expression->expression.datatype = type_string;
2228 expression->value = "TODO: FUNCTION";
2230 return (expression_t*) expression;
2233 static expression_t *parse_pretty_function_keyword(void)
2235 eat(T___PRETTY_FUNCTION__);
2238 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2239 expression->expression.type = EXPR_PRETTY_FUNCTION;
2240 expression->expression.datatype = type_string;
2241 expression->value = "TODO: PRETTY FUNCTION";
2243 return (expression_t*) expression;
2246 static designator_t *parse_designator(void)
2248 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2250 if(token.type != T_IDENTIFIER) {
2251 parse_error_expected("while parsing member designator",
2256 result->symbol = token.v.symbol;
2259 designator_t *last_designator = result;
2261 if(token.type == '.') {
2263 if(token.type != T_IDENTIFIER) {
2264 parse_error_expected("while parsing member designator",
2269 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2270 designator->symbol = token.v.symbol;
2273 last_designator->next = designator;
2274 last_designator = designator;
2277 if(token.type == '[') {
2279 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2280 designator->array_access = parse_expression();
2281 if(designator->array_access == NULL) {
2287 last_designator->next = designator;
2288 last_designator = designator;
2297 static expression_t *parse_offsetof(void)
2299 eat(T___builtin_offsetof);
2301 offsetof_expression_t *expression
2302 = allocate_ast_zero(sizeof(expression[0]));
2303 expression->expression.type = EXPR_OFFSETOF;
2304 expression->expression.datatype = type_size_t;
2307 expression->type = parse_typename();
2309 expression->designator = parse_designator();
2312 return (expression_t*) expression;
2315 static expression_t *parse_va_arg(void)
2317 eat(T___builtin_va_arg);
2319 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2320 expression->expression.type = EXPR_VA_ARG;
2323 expression->arg = parse_assignment_expression();
2325 expression->expression.datatype = parse_typename();
2328 return (expression_t*) expression;
2331 static expression_t *parse_builtin_symbol(void)
2333 builtin_symbol_expression_t *expression
2334 = allocate_ast_zero(sizeof(expression[0]));
2335 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2337 /* TODO: set datatype */
2339 expression->symbol = token.v.symbol;
2343 return (expression_t*) expression;
2346 static expression_t *parse_primary_expression(void)
2348 switch(token.type) {
2350 return parse_int_const();
2351 case T_FLOATINGPOINT:
2352 return parse_float_const();
2353 case T_STRING_LITERAL:
2354 return parse_string_const();
2356 return parse_reference();
2357 case T___FUNCTION__:
2358 return parse_function_keyword();
2359 case T___PRETTY_FUNCTION__:
2360 return parse_pretty_function_keyword();
2361 case T___builtin_offsetof:
2362 return parse_offsetof();
2363 case T___builtin_va_arg:
2364 return parse_va_arg();
2365 case T___builtin_expect:
2366 case T___builtin_va_start:
2367 case T___builtin_va_end:
2368 return parse_builtin_symbol();
2371 return parse_brace_expression();
2374 parser_print_error_prefix();
2375 fprintf(stderr, "unexpected token ");
2376 print_token(stderr, &token);
2377 fprintf(stderr, "\n");
2380 return make_invalid_expression();
2383 static expression_t *parse_array_expression(unsigned precedence,
2384 expression_t *array_ref)
2390 array_access_expression_t *array_access
2391 = allocate_ast_zero(sizeof(array_access[0]));
2393 array_access->expression.type = EXPR_ARRAY_ACCESS;
2394 array_access->array_ref = array_ref;
2395 array_access->index = parse_expression();
2397 type_t *type = array_ref->datatype;
2399 if(type->type == TYPE_POINTER) {
2400 pointer_type_t *pointer = (pointer_type_t*) type;
2401 array_access->expression.datatype = pointer->points_to;
2402 } else if(type->type == TYPE_ARRAY) {
2403 array_type_t *array_type = (array_type_t*) type;
2404 array_access->expression.datatype = array_type->element_type;
2406 parser_print_error_prefix();
2407 fprintf(stderr, "array access on object with non-pointer type ");
2408 print_type_quoted(type);
2409 fprintf(stderr, "\n");
2413 if(token.type != ']') {
2414 parse_error_expected("Problem while parsing array access", ']', 0);
2415 return (expression_t*) array_access;
2419 return (expression_t*) array_access;
2422 static bool is_declaration_specifier(const token_t *token,
2423 bool only_type_specifiers)
2425 switch(token->type) {
2429 return is_typedef_symbol(token->v.symbol);
2432 if(only_type_specifiers)
2441 static expression_t *parse_sizeof(unsigned precedence)
2445 sizeof_expression_t *sizeof_expression
2446 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2447 sizeof_expression->expression.type = EXPR_SIZEOF;
2448 sizeof_expression->expression.datatype = type_size_t;
2450 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2452 sizeof_expression->type = parse_typename();
2455 expression_t *expression = parse_sub_expression(precedence);
2456 sizeof_expression->type = expression->datatype;
2457 sizeof_expression->size_expression = expression;
2460 return (expression_t*) sizeof_expression;
2463 static expression_t *parse_select_expression(unsigned precedence,
2464 expression_t *compound)
2467 assert(token.type == '.' || token.type == T_MINUSGREATER);
2469 bool is_pointer = (token.type == T_MINUSGREATER);
2472 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2474 select->expression.type = EXPR_SELECT;
2475 select->compound = compound;
2477 if(token.type != T_IDENTIFIER) {
2478 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
2479 return (expression_t*) select;
2481 symbol_t *symbol = token.v.symbol;
2482 select->symbol = symbol;
2485 type_t *type = compound->datatype;
2487 return make_invalid_expression();
2489 type_t *type_left = type;
2491 if(type->type != TYPE_POINTER) {
2492 parser_print_error_prefix();
2493 fprintf(stderr, "left hand side of '->' is not a pointer, but ");
2494 print_type_quoted(type);
2495 fputc('\n', stderr);
2496 return make_invalid_expression();
2498 pointer_type_t *pointer_type = (pointer_type_t*) type;
2499 type_left = pointer_type->points_to;
2501 type_left = skip_typeref(type_left);
2503 if(type_left->type != TYPE_COMPOUND_STRUCT
2504 && type_left->type != TYPE_COMPOUND_UNION) {
2505 parser_print_error_prefix();
2506 fprintf(stderr, "request for member '%s' in something not a struct or "
2507 "union, but ", symbol->string);
2508 print_type_quoted(type_left);
2509 fputc('\n', stderr);
2510 return make_invalid_expression();
2513 compound_type_t *compound_type = (compound_type_t*) type_left;
2514 declaration_t *declaration = compound_type->declaration;
2516 if(!declaration->init.is_defined) {
2517 parser_print_error_prefix();
2518 fprintf(stderr, "request for member '%s' of incomplete type ",
2520 print_type_quoted(type_left);
2521 fputc('\n', stderr);
2522 return make_invalid_expression();
2525 declaration_t *iter = declaration->context.declarations;
2526 for( ; iter != NULL; iter = iter->next) {
2527 if(iter->symbol == symbol) {
2532 parser_print_error_prefix();
2533 print_type_quoted(type_left);
2534 fprintf(stderr, " has no memeber named '%s'\n", symbol->string);
2535 return make_invalid_expression();
2538 select->compound_entry = iter;
2539 select->expression.datatype = iter->type;
2540 return (expression_t*) select;
2543 static expression_t *parse_call_expression(unsigned precedence,
2544 expression_t *expression)
2547 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2548 call->expression.type = EXPR_CALL;
2549 call->function = expression;
2551 function_type_t *function_type;
2552 type_t *type = expression->datatype;
2553 if(type->type != TYPE_FUNCTION) {
2554 /* TODO calling pointers to functions is ok */
2555 parser_print_error_prefix();
2556 fputs("called object '", stderr);
2557 print_expression(expression);
2558 fputs("' (type ", stderr);
2559 print_type_quoted(type);
2560 fputs("is not a function\n", stderr);
2562 function_type = NULL;
2563 call->expression.datatype = NULL;
2565 function_type = (function_type_t*) type;
2566 call->expression.datatype = function_type->result_type;
2569 /* parse arguments */
2572 if(token.type != ')') {
2573 call_argument_t *last_argument = NULL;
2576 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2578 argument->expression = parse_assignment_expression();
2579 if(last_argument == NULL) {
2580 call->arguments = argument;
2582 last_argument->next = argument;
2584 last_argument = argument;
2586 if(token.type != ',')
2593 if(function_type != NULL) {
2594 function_parameter_t *parameter = function_type->parameters;
2595 call_argument_t *argument = call->arguments;
2596 for( ; parameter != NULL && argument != NULL;
2597 parameter = parameter->next, argument = argument->next) {
2598 type_t *expected_type = parameter->type;
2599 /* TODO report context in error messages */
2600 argument->expression = create_implicit_cast(argument->expression,
2603 /* too few parameters */
2604 if(parameter != NULL) {
2605 parser_print_error_prefix();
2606 fprintf(stderr, "too few arguments to function '");
2607 print_expression(expression);
2608 fprintf(stderr, "'\n");
2609 } else if(argument != NULL) {
2610 /* too many parameters */
2611 if(!function_type->variadic
2612 && !function_type->unspecified_parameters) {
2613 parser_print_error_prefix();
2614 fprintf(stderr, "too many arguments to function '");
2615 print_expression(expression);
2616 fprintf(stderr, "'\n");
2618 /* do default promotion */
2619 for( ; argument != NULL; argument = argument->next) {
2620 type_t *type = argument->expression->datatype;
2625 if(is_type_integer(type)) {
2626 type = promote_integer(type);
2627 } else if(type == type_float) {
2630 argument->expression
2631 = create_implicit_cast(argument->expression, type);
2637 return (expression_t*) call;
2640 static type_t *get_type_after_conversion(const type_t *type1,
2641 const type_t *type2)
2645 return (type_t*) type1;
2648 static expression_t *parse_conditional_expression(unsigned precedence,
2649 expression_t *expression)
2653 conditional_expression_t *conditional
2654 = allocate_ast_zero(sizeof(conditional[0]));
2655 conditional->expression.type = EXPR_CONDITIONAL;
2656 conditional->condition = expression;
2659 type_t *condition_type = conditional->condition->datatype;
2660 if(condition_type != NULL) {
2661 if(!is_type_scalar(condition_type)) {
2662 type_error("expected a scalar type", expression->source_position,
2667 conditional->true_expression = parse_expression();
2669 conditional->false_expression = parse_sub_expression(precedence);
2671 type_t *true_type = conditional->true_expression->datatype;
2672 if(true_type == NULL)
2673 return (expression_t*) conditional;
2674 type_t *false_type = conditional->false_expression->datatype;
2675 if(false_type == NULL)
2676 return (expression_t*) conditional;
2679 if(true_type == false_type) {
2680 conditional->expression.datatype = true_type;
2681 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2682 type_t *result = get_type_after_conversion(true_type, false_type);
2683 /* TODO: create implicit convs if necessary */
2684 conditional->expression.datatype = result;
2685 } else if(true_type->type == TYPE_POINTER &&
2686 false_type->type == TYPE_POINTER &&
2687 true /* TODO compatible points_to types */) {
2689 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2690 || (is_null_ptr_const(false_type) &&
2691 true_type->type == TYPE_POINTER) TODO*/ false) {
2693 } else if(/* 1 is pointer to object type, other is void* */ false) {
2696 type_error_incompatible("while parsing conditional",
2697 expression->source_position, true_type,
2701 return (expression_t*) conditional;
2704 static expression_t *parse_extension(unsigned precedence)
2706 eat(T___extension__);
2708 /* TODO enable extensions */
2710 return parse_sub_expression(precedence);
2713 static void semantic_incdec(unary_expression_t *expression)
2715 type_t *orig_type = expression->value->datatype;
2716 if(orig_type == NULL)
2719 type_t *type = skip_typeref(orig_type);
2720 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2721 /* TODO: improve error message */
2722 parser_print_error_prefix();
2723 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2727 expression->expression.datatype = orig_type;
2730 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2732 type_t *orig_type = expression->value->datatype;
2733 if(orig_type == NULL)
2736 type_t *type = skip_typeref(orig_type);
2737 if(!is_type_arithmetic(type)) {
2738 /* TODO: improve error message */
2739 parser_print_error_prefix();
2740 fprintf(stderr, "operation needs an arithmetic type\n");
2744 expression->expression.datatype = orig_type;
2747 static void semantic_dereference(unary_expression_t *expression)
2749 type_t *orig_type = expression->value->datatype;
2750 if(orig_type == NULL)
2753 type_t *type = skip_typeref(orig_type);
2754 if(type->type != TYPE_POINTER) {
2755 /* TODO: improve error message */
2756 parser_print_error_prefix();
2757 fprintf(stderr, "operation needs a pointer type\n");
2761 pointer_type_t *pointer_type = (pointer_type_t*) type;
2762 expression->expression.datatype = pointer_type->points_to;
2765 static void semantic_take_addr(unary_expression_t *expression)
2767 type_t *orig_type = expression->value->datatype;
2768 if(orig_type == NULL)
2771 expression_t *value = expression->value;
2772 if(value->type == EXPR_REFERENCE) {
2773 reference_expression_t *reference = (reference_expression_t*) value;
2774 declaration_t *declaration = reference->declaration;
2775 if(declaration != NULL) {
2776 declaration->address_taken = 1;
2780 expression->expression.datatype = make_pointer_type(orig_type, 0);
2783 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2784 static expression_t *parse_##unexpression_type(unsigned precedence) \
2788 unary_expression_t *unary_expression \
2789 = allocate_ast_zero(sizeof(unary_expression[0])); \
2790 unary_expression->expression.type = EXPR_UNARY; \
2791 unary_expression->type = unexpression_type; \
2792 unary_expression->value = parse_sub_expression(precedence); \
2794 sfunc(unary_expression); \
2796 return (expression_t*) unary_expression; \
2799 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2800 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2801 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2802 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2803 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2804 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2805 semantic_unexpr_arithmetic)
2806 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2808 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2811 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2813 static expression_t *parse_##unexpression_type(unsigned precedence, \
2814 expression_t *left) \
2816 (void) precedence; \
2819 unary_expression_t *unary_expression \
2820 = allocate_ast_zero(sizeof(unary_expression[0])); \
2821 unary_expression->expression.type = EXPR_UNARY; \
2822 unary_expression->type = unexpression_type; \
2823 unary_expression->value = left; \
2825 sfunc(unary_expression); \
2827 return (expression_t*) unary_expression; \
2830 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2832 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2835 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2837 /* TODO: handle complex + imaginary types */
2839 /* § 6.3.1.8 Usual arithmetic conversions */
2840 if(type_left == type_long_double || type_right == type_long_double) {
2841 return type_long_double;
2842 } else if(type_left == type_double || type_right == type_double) {
2844 } else if(type_left == type_float || type_right == type_float) {
2848 type_right = promote_integer(type_right);
2849 type_left = promote_integer(type_left);
2851 if(type_left == type_right)
2854 bool signed_left = is_type_signed(type_left);
2855 bool signed_right = is_type_signed(type_right);
2856 if(get_rank(type_left) < get_rank(type_right)) {
2857 if(signed_left == signed_right || !signed_right) {
2863 if(signed_left == signed_right || !signed_left) {
2871 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2873 expression_t *left = expression->left;
2874 expression_t *right = expression->right;
2875 type_t *orig_type_left = left->datatype;
2876 type_t *orig_type_right = right->datatype;
2878 if(orig_type_left == NULL || orig_type_right == NULL)
2881 type_t *type_left = skip_typeref(orig_type_left);
2882 type_t *type_right = skip_typeref(orig_type_right);
2884 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2885 /* TODO: improve error message */
2886 parser_print_error_prefix();
2887 fprintf(stderr, "operation needs arithmetic types\n");
2891 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2892 expression->left = create_implicit_cast(left, arithmetic_type);
2893 expression->right = create_implicit_cast(right, arithmetic_type);
2894 expression->expression.datatype = arithmetic_type;
2897 static void semantic_shift_op(binary_expression_t *expression)
2899 expression_t *left = expression->left;
2900 expression_t *right = expression->right;
2901 type_t *orig_type_left = left->datatype;
2902 type_t *orig_type_right = right->datatype;
2904 if(orig_type_left == NULL || orig_type_right == NULL)
2907 type_t *type_left = skip_typeref(orig_type_left);
2908 type_t *type_right = skip_typeref(orig_type_right);
2910 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
2911 /* TODO: improve error message */
2912 parser_print_error_prefix();
2913 fprintf(stderr, "operation needs integer types\n");
2917 type_left = promote_integer(type_left);
2918 type_right = promote_integer(type_right);
2920 expression->left = create_implicit_cast(left, type_left);
2921 expression->right = create_implicit_cast(right, type_right);
2922 expression->expression.datatype = type_left;
2925 static void semantic_add(binary_expression_t *expression)
2927 expression_t *left = expression->left;
2928 expression_t *right = expression->right;
2929 type_t *orig_type_left = left->datatype;
2930 type_t *orig_type_right = right->datatype;
2932 if(orig_type_left == NULL || orig_type_right == NULL)
2935 type_t *type_left = skip_typeref(orig_type_left);
2936 type_t *type_right = skip_typeref(orig_type_right);
2939 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2940 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2941 expression->left = create_implicit_cast(left, arithmetic_type);
2942 expression->right = create_implicit_cast(right, arithmetic_type);
2943 expression->expression.datatype = arithmetic_type;
2945 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2946 expression->expression.datatype = type_left;
2947 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2948 expression->expression.datatype = type_right;
2950 parser_print_error_prefix();
2951 fprintf(stderr, "invalid operands to binary + (");
2952 print_type_quoted(orig_type_left);
2953 fprintf(stderr, ", ");
2954 print_type_quoted(orig_type_right);
2955 fprintf(stderr, ")\n");
2959 static void semantic_sub(binary_expression_t *expression)
2961 expression_t *left = expression->left;
2962 expression_t *right = expression->right;
2963 type_t *orig_type_left = left->datatype;
2964 type_t *orig_type_right = right->datatype;
2966 if(orig_type_left == NULL || orig_type_right == NULL)
2969 type_t *type_left = skip_typeref(orig_type_left);
2970 type_t *type_right = skip_typeref(orig_type_right);
2973 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2974 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2975 expression->left = create_implicit_cast(left, arithmetic_type);
2976 expression->right = create_implicit_cast(right, arithmetic_type);
2977 expression->expression.datatype = arithmetic_type;
2979 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2980 expression->expression.datatype = type_left;
2981 } else if(type_left->type == TYPE_POINTER &&
2982 type_right->type == TYPE_POINTER) {
2983 if(!pointers_compatible(type_left, type_right)) {
2984 parser_print_error_prefix();
2985 fprintf(stderr, "pointers to incompatible objects to binary - (");
2986 print_type_quoted(orig_type_left);
2987 fprintf(stderr, ", ");
2988 print_type_quoted(orig_type_right);
2989 fprintf(stderr, ")\n");
2991 expression->expression.datatype = type_ptrdiff_t;
2994 parser_print_error_prefix();
2995 fprintf(stderr, "invalid operands to binary - (");
2996 print_type_quoted(orig_type_left);
2997 fprintf(stderr, ", ");
2998 print_type_quoted(orig_type_right);
2999 fprintf(stderr, ")\n");
3003 static void semantic_comparison(binary_expression_t *expression)
3005 expression_t *left = expression->left;
3006 expression_t *right = expression->right;
3007 type_t *orig_type_left = left->datatype;
3008 type_t *orig_type_right = right->datatype;
3010 if(orig_type_left == NULL || orig_type_right == NULL)
3013 type_t *type_left = skip_typeref(orig_type_left);
3014 type_t *type_right = skip_typeref(orig_type_right);
3016 /* TODO non-arithmetic types */
3017 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
3018 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3019 expression->left = create_implicit_cast(left, arithmetic_type);
3020 expression->right = create_implicit_cast(right, arithmetic_type);
3021 expression->expression.datatype = arithmetic_type;
3023 expression->expression.datatype = type_int;
3026 static void semantic_arithmetic_assign(binary_expression_t *expression)
3028 expression_t *left = expression->left;
3029 expression_t *right = expression->right;
3030 type_t *orig_type_left = left->datatype;
3031 type_t *orig_type_right = right->datatype;
3033 if(orig_type_left == NULL || orig_type_right == NULL)
3036 type_t *type_left = skip_typeref(orig_type_left);
3037 type_t *type_right = skip_typeref(orig_type_right);
3039 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3040 /* TODO: improve error message */
3041 parser_print_error_prefix();
3042 fprintf(stderr, "operation needs arithmetic types\n");
3046 /* combined instructions are tricky. We can't create an implicit cast on
3047 * the left side, because we need the uncasted form for the store.
3048 * The ast2firm pass has to know that left_type must be right_type
3049 * for the arithmeitc operation and create a cast by itself */
3050 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
3051 expression->right = create_implicit_cast(right, arithmetic_type);
3052 expression->expression.datatype = type_left;
3055 static void semantic_logical_op(binary_expression_t *expression)
3057 expression_t *left = expression->left;
3058 expression_t *right = expression->right;
3059 type_t *orig_type_left = left->datatype;
3060 type_t *orig_type_right = right->datatype;
3062 if(orig_type_left == NULL || orig_type_right == NULL)
3065 type_t *type_left = skip_typeref(orig_type_left);
3066 type_t *type_right = skip_typeref(orig_type_right);
3068 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
3069 /* TODO: improve error message */
3070 parser_print_error_prefix();
3071 fprintf(stderr, "operation needs arithmetic types\n");
3075 expression->expression.datatype = type_int;
3078 static void semantic_binexpr_assign(binary_expression_t *expression)
3080 expression_t *left = expression->left;
3081 type_t *type_left = left->datatype;
3083 if(type_left != NULL) {
3084 semantic_assign(type_left, &expression->right, "assignment");
3087 expression->expression.datatype = type_left;
3090 static void semantic_comma(binary_expression_t *expression)
3092 expression->expression.datatype = expression->right->datatype;
3095 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
3096 static expression_t *parse_##binexpression_type(unsigned precedence, \
3097 expression_t *left) \
3101 expression_t *right = parse_sub_expression(precedence + lr); \
3103 binary_expression_t *binexpr \
3104 = allocate_ast_zero(sizeof(binexpr[0])); \
3105 binexpr->expression.type = EXPR_BINARY; \
3106 binexpr->type = binexpression_type; \
3107 binexpr->left = left; \
3108 binexpr->right = right; \
3111 return (expression_t*) binexpr; \
3114 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
3115 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
3116 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
3117 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
3118 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
3119 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
3120 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
3121 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
3122 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
3123 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
3124 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
3125 semantic_comparison, 1)
3126 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
3127 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
3128 semantic_comparison, 1)
3129 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
3130 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
3131 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
3132 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
3133 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
3134 /* TODO shift has a bit special semantic */
3135 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
3136 semantic_shift_op, 1)
3137 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
3138 semantic_shift_op, 1)
3139 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3140 semantic_arithmetic_assign, 0)
3141 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3142 semantic_arithmetic_assign, 0)
3143 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3144 semantic_arithmetic_assign, 0)
3145 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3146 semantic_arithmetic_assign, 0)
3147 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3148 semantic_arithmetic_assign, 0)
3149 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3150 semantic_arithmetic_assign, 0)
3151 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3152 semantic_arithmetic_assign, 0)
3153 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3154 semantic_arithmetic_assign, 0)
3155 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3156 semantic_arithmetic_assign, 0)
3157 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3158 semantic_arithmetic_assign, 0)
3160 static expression_t *parse_sub_expression(unsigned precedence)
3162 if(token.type < 0) {
3163 return expected_expression_error();
3166 expression_parser_function_t *parser
3167 = &expression_parsers[token.type];
3168 source_position_t source_position = token.source_position;
3171 if(parser->parser != NULL) {
3172 left = parser->parser(parser->precedence);
3174 left = parse_primary_expression();
3176 assert(left != NULL);
3177 left->source_position = source_position;
3180 if(token.type < 0) {
3181 return expected_expression_error();
3184 parser = &expression_parsers[token.type];
3185 if(parser->infix_parser == NULL)
3187 if(parser->infix_precedence < precedence)
3190 left = parser->infix_parser(parser->infix_precedence, left);
3192 assert(left != NULL);
3193 assert(left->type != EXPR_UNKNOWN);
3194 left->source_position = source_position;
3200 static expression_t *parse_expression(void)
3202 return parse_sub_expression(1);
3207 static void register_expression_parser(parse_expression_function parser,
3208 int token_type, unsigned precedence)
3210 expression_parser_function_t *entry = &expression_parsers[token_type];
3212 if(entry->parser != NULL) {
3213 fprintf(stderr, "for token ");
3214 print_token_type(stderr, token_type);
3215 fprintf(stderr, "\n");
3216 panic("trying to register multiple expression parsers for a token");
3218 entry->parser = parser;
3219 entry->precedence = precedence;
3222 static void register_expression_infix_parser(
3223 parse_expression_infix_function parser, int token_type,
3224 unsigned precedence)
3226 expression_parser_function_t *entry = &expression_parsers[token_type];
3228 if(entry->infix_parser != NULL) {
3229 fprintf(stderr, "for token ");
3230 print_token_type(stderr, token_type);
3231 fprintf(stderr, "\n");
3232 panic("trying to register multiple infix expression parsers for a "
3235 entry->infix_parser = parser;
3236 entry->infix_precedence = precedence;
3239 static void init_expression_parsers(void)
3241 memset(&expression_parsers, 0, sizeof(expression_parsers));
3243 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3244 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3245 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3246 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3247 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3248 T_GREATERGREATER, 16);
3249 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3250 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3251 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3252 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3253 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3254 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3255 T_GREATEREQUAL, 14);
3256 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3257 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3258 T_EXCLAMATIONMARKEQUAL, 13);
3259 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3260 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3261 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3262 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3263 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3264 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3265 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3266 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3267 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3268 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3269 T_ASTERISKEQUAL, 2);
3270 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3271 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3273 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3274 T_LESSLESSEQUAL, 2);
3275 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3276 T_GREATERGREATEREQUAL, 2);
3277 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3279 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3281 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3284 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3286 register_expression_infix_parser(parse_array_expression, '[', 30);
3287 register_expression_infix_parser(parse_call_expression, '(', 30);
3288 register_expression_infix_parser(parse_select_expression, '.', 30);
3289 register_expression_infix_parser(parse_select_expression,
3290 T_MINUSGREATER, 30);
3291 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3293 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3296 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3297 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3298 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3299 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3300 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3301 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3302 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3303 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3304 register_expression_parser(parse_sizeof, T_sizeof, 25);
3305 register_expression_parser(parse_extension, T___extension__, 25);
3309 static statement_t *parse_case_statement(void)
3312 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3313 label->statement.type = STATEMENT_CASE_LABEL;
3314 label->statement.source_position = token.source_position;
3316 label->expression = parse_expression();
3319 label->statement.next = parse_statement();
3321 return (statement_t*) label;
3324 static statement_t *parse_default_statement(void)
3328 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3329 label->statement.type = STATEMENT_CASE_LABEL;
3330 label->statement.source_position = token.source_position;
3333 label->statement.next = parse_statement();
3335 return (statement_t*) label;
3338 static declaration_t *get_label(symbol_t *symbol)
3340 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3341 assert(current_function != NULL);
3342 /* if we found a label in the same function, then we already created the
3344 if(candidate != NULL
3345 && candidate->parent_context == ¤t_function->context) {
3349 /* otherwise we need to create a new one */
3350 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3351 declaration->namespace = NAMESPACE_LABEL;
3352 declaration->symbol = symbol;
3354 label_push(declaration);
3359 static statement_t *parse_label_statement(void)
3361 assert(token.type == T_IDENTIFIER);
3362 symbol_t *symbol = token.v.symbol;
3365 declaration_t *label = get_label(symbol);
3367 /* if source position is already set then the label is defined twice,
3368 * otherwise it was just mentioned in a goto so far */
3369 if(label->source_position.input_name != NULL) {
3370 parser_print_error_prefix();
3371 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3372 parser_print_error_prefix_pos(label->source_position);
3373 fprintf(stderr, "previous definition of '%s' was here\n",
3376 label->source_position = token.source_position;
3379 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3381 label_statement->statement.type = STATEMENT_LABEL;
3382 label_statement->statement.source_position = token.source_position;
3383 label_statement->label = label;
3387 if(token.type == '}') {
3388 parse_error("label at end of compound statement");
3389 return (statement_t*) label_statement;
3391 label_statement->label_statement = parse_statement();
3394 return (statement_t*) label_statement;
3397 static statement_t *parse_if(void)
3401 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3402 statement->statement.type = STATEMENT_IF;
3403 statement->statement.source_position = token.source_position;
3406 statement->condition = parse_expression();
3409 statement->true_statement = parse_statement();
3410 if(token.type == T_else) {
3412 statement->false_statement = parse_statement();
3415 return (statement_t*) statement;
3418 static statement_t *parse_switch(void)
3422 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3423 statement->statement.type = STATEMENT_SWITCH;
3424 statement->statement.source_position = token.source_position;
3427 statement->expression = parse_expression();
3429 statement->body = parse_statement();
3431 return (statement_t*) statement;
3434 static statement_t *parse_while(void)
3438 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3439 statement->statement.type = STATEMENT_WHILE;
3440 statement->statement.source_position = token.source_position;
3443 statement->condition = parse_expression();
3445 statement->body = parse_statement();
3447 return (statement_t*) statement;
3450 static statement_t *parse_do(void)
3454 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3455 statement->statement.type = STATEMENT_DO_WHILE;
3456 statement->statement.source_position = token.source_position;
3458 statement->body = parse_statement();
3461 statement->condition = parse_expression();
3465 return (statement_t*) statement;
3468 static statement_t *parse_for(void)
3472 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3473 statement->statement.type = STATEMENT_FOR;
3474 statement->statement.source_position = token.source_position;
3478 int top = environment_top();
3479 context_t *last_context = context;
3480 set_context(&statement->context);
3482 if(token.type != ';') {
3483 if(is_declaration_specifier(&token, false)) {
3484 parse_declaration();
3486 statement->initialisation = parse_expression();
3493 if(token.type != ';') {
3494 statement->condition = parse_expression();
3497 if(token.type != ')') {
3498 statement->step = parse_expression();
3501 statement->body = parse_statement();
3503 assert(context == &statement->context);
3504 set_context(last_context);
3505 environment_pop_to(top);
3507 return (statement_t*) statement;
3510 static statement_t *parse_goto(void)
3514 if(token.type != T_IDENTIFIER) {
3515 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3519 symbol_t *symbol = token.v.symbol;
3522 declaration_t *label = get_label(symbol);
3524 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3526 statement->statement.type = STATEMENT_GOTO;
3527 statement->statement.source_position = token.source_position;
3529 statement->label = label;
3533 return (statement_t*) statement;
3536 static statement_t *parse_continue(void)
3541 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3542 statement->type = STATEMENT_CONTINUE;
3543 statement->source_position = token.source_position;
3548 static statement_t *parse_break(void)
3553 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3554 statement->type = STATEMENT_BREAK;
3555 statement->source_position = token.source_position;
3560 static statement_t *parse_return(void)
3564 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3566 statement->statement.type = STATEMENT_RETURN;
3567 statement->statement.source_position = token.source_position;
3569 assert(current_function->type->type == TYPE_FUNCTION);
3570 function_type_t *function_type = (function_type_t*) current_function->type;
3571 type_t *return_type = function_type->result_type;
3573 expression_t *return_value;
3574 if(token.type != ';') {
3575 return_value = parse_expression();
3577 if(return_type == type_void && return_value->datatype != type_void) {
3578 parse_warning("'return' with a value, in function returning void");
3579 return_value = NULL;
3581 if(return_type != NULL) {
3582 semantic_assign(return_type, &return_value, "'return'");
3586 return_value = NULL;
3587 if(return_type != type_void) {
3588 parse_warning("'return' without value, in function returning "
3592 statement->return_value = return_value;
3596 return (statement_t*) statement;
3599 static statement_t *parse_declaration_statement(void)
3601 declaration_t *before = last_declaration;
3603 declaration_statement_t *statement
3604 = allocate_ast_zero(sizeof(statement[0]));
3605 statement->statement.type = STATEMENT_DECLARATION;
3606 statement->statement.source_position = token.source_position;
3608 declaration_specifiers_t specifiers;
3609 memset(&specifiers, 0, sizeof(specifiers));
3610 parse_declaration_specifiers(&specifiers);
3612 if(token.type == ';') {
3615 parse_init_declarators(&specifiers);
3618 if(before == NULL) {
3619 statement->declarations_begin = context->declarations;
3621 statement->declarations_begin = before->next;
3623 statement->declarations_end = last_declaration;
3625 return (statement_t*) statement;
3628 static statement_t *parse_expression_statement(void)
3630 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3631 statement->statement.type = STATEMENT_EXPRESSION;
3632 statement->statement.source_position = token.source_position;
3634 statement->expression = parse_expression();
3638 return (statement_t*) statement;
3641 static statement_t *parse_statement(void)
3643 statement_t *statement = NULL;
3645 /* declaration or statement */
3646 switch(token.type) {
3648 statement = parse_case_statement();
3652 statement = parse_default_statement();
3656 statement = parse_compound_statement();
3660 statement = parse_if();
3664 statement = parse_switch();
3668 statement = parse_while();
3672 statement = parse_do();
3676 statement = parse_for();
3680 statement = parse_goto();
3684 statement = parse_continue();
3688 statement = parse_break();
3692 statement = parse_return();
3701 if(look_ahead(1)->type == ':') {
3702 statement = parse_label_statement();
3706 if(is_typedef_symbol(token.v.symbol)) {
3707 statement = parse_declaration_statement();
3711 statement = parse_expression_statement();
3714 case T___extension__:
3715 /* this can be a prefix to a declaration or an expression statement */
3716 /* we simply eat it now and parse the rest with tail recursion */
3719 } while(token.type == T___extension__);
3720 statement = parse_statement();
3724 statement = parse_declaration_statement();
3728 statement = parse_expression_statement();
3732 assert(statement == NULL || statement->source_position.input_name != NULL);
3737 static statement_t *parse_compound_statement(void)
3739 compound_statement_t *compound_statement
3740 = allocate_ast_zero(sizeof(compound_statement[0]));
3741 compound_statement->statement.type = STATEMENT_COMPOUND;
3742 compound_statement->statement.source_position = token.source_position;
3746 int top = environment_top();
3747 context_t *last_context = context;
3748 set_context(&compound_statement->context);
3750 statement_t *last_statement = NULL;
3752 while(token.type != '}' && token.type != T_EOF) {
3753 statement_t *statement = parse_statement();
3754 if(statement == NULL)
3757 if(last_statement != NULL) {
3758 last_statement->next = statement;
3760 compound_statement->statements = statement;
3763 while(statement->next != NULL)
3764 statement = statement->next;
3766 last_statement = statement;
3769 if(token.type != '}') {
3770 parser_print_error_prefix_pos(
3771 compound_statement->statement.source_position);
3772 fprintf(stderr, "end of file while looking for closing '}'\n");
3776 assert(context == &compound_statement->context);
3777 set_context(last_context);
3778 environment_pop_to(top);
3780 return (statement_t*) compound_statement;
3783 static translation_unit_t *parse_translation_unit(void)
3785 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3787 assert(global_context == NULL);
3788 global_context = &unit->context;
3790 assert(context == NULL);
3791 set_context(&unit->context);
3793 while(token.type != T_EOF) {
3794 parse_declaration();
3797 assert(context == &unit->context);
3799 last_declaration = NULL;
3801 assert(global_context == &unit->context);
3802 global_context = NULL;
3807 translation_unit_t *parse(void)
3809 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3810 label_stack = NEW_ARR_F(stack_entry_t, 0);
3811 found_error = false;
3813 type_set_output(stderr);
3814 ast_set_output(stderr);
3816 lookahead_bufpos = 0;
3817 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3820 translation_unit_t *unit = parse_translation_unit();
3822 DEL_ARR_F(environment_stack);
3823 DEL_ARR_F(label_stack);
3831 void init_parser(void)
3833 init_expression_parsers();
3834 obstack_init(&temp_obst);
3836 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3837 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3838 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3839 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3840 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3841 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3842 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3843 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3844 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3845 type_string = make_pointer_type(type_const_char, 0);
3848 void exit_parser(void)
3850 obstack_free(&temp_obst, NULL);