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 parse_warning(const char *message)
206 parser_print_prefix_pos(token.source_position);
207 fprintf(stderr, "warning: %s\n", message);
210 static void parse_error_expected(const char *message, ...)
215 if(message != NULL) {
216 parser_print_error_prefix();
217 fprintf(stderr, "%s\n", message);
219 parser_print_error_prefix();
220 fputs("Parse error: got ", stderr);
221 print_token(stderr, &token);
222 fputs(", expected ", stderr);
224 va_start(args, message);
225 token_type_t token_type = va_arg(args, token_type_t);
226 while(token_type != 0) {
230 fprintf(stderr, ", ");
232 print_token_type(stderr, token_type);
233 token_type = va_arg(args, token_type_t);
236 fprintf(stderr, "\n");
239 static void type_error(const char *msg, const source_position_t source_position,
242 parser_print_error_prefix_pos(source_position);
243 fprintf(stderr, "%s, but found type ", msg);
249 static void type_error_incompatible(const char *msg,
250 const source_position_t source_position, type_t *type1, type_t *type2)
252 parser_print_error_prefix_pos(source_position);
253 fprintf(stderr, "%s, incompatible types: ", msg);
255 fprintf(stderr, " - ");
257 fprintf(stderr, ")\n");
261 static void eat_block(void)
263 if(token.type == '{')
266 while(token.type != '}') {
267 if(token.type == T_EOF)
269 if(token.type == '{') {
278 static void eat_statement(void)
280 while(token.type != ';') {
281 if(token.type == T_EOF)
283 if(token.type == '}')
285 if(token.type == '{') {
294 static void eat_brace(void)
296 if(token.type == '(')
299 while(token.type != ')') {
300 if(token.type == T_EOF)
302 if(token.type == ')' || token.type == ';' || token.type == '}') {
305 if(token.type == '(') {
309 if(token.type == '{') {
318 #define expect(expected) \
319 if(UNLIKELY(token.type != (expected))) { \
320 parse_error_expected(NULL, (expected), 0); \
326 #define expect_void(expected) \
327 if(UNLIKELY(token.type != (expected))) { \
328 parse_error_expected(NULL, (expected), 0); \
334 static void set_context(context_t *new_context)
336 context = new_context;
338 last_declaration = new_context->declarations;
339 if(last_declaration != NULL) {
340 while(last_declaration->next != NULL) {
341 last_declaration = last_declaration->next;
347 * called when we find a 2nd declarator for an identifier we already have a
350 static bool is_compatible_declaration (declaration_t *declaration,
351 declaration_t *previous)
353 /* TODO: not correct yet */
354 return declaration->type == previous->type;
357 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
359 declaration_t *declaration = symbol->declaration;
360 for( ; declaration != NULL; declaration = declaration->symbol_next) {
361 if(declaration->namespace == namespace)
368 static const char *get_namespace_prefix(namespace_t namespace)
371 case NAMESPACE_NORMAL:
373 case NAMESPACE_UNION:
375 case NAMESPACE_STRUCT:
379 case NAMESPACE_LABEL:
382 panic("invalid namespace found");
386 * pushs an environment_entry on the environment stack and links the
387 * corresponding symbol to the new entry
389 static declaration_t *stack_push(stack_entry_t **stack_ptr,
390 declaration_t *declaration,
391 context_t *parent_context)
393 symbol_t *symbol = declaration->symbol;
394 namespace_t namespace = declaration->namespace;
396 /* a declaration should be only pushed once */
397 assert(declaration->parent_context == NULL);
398 declaration->parent_context = parent_context;
400 declaration_t *previous_declaration = get_declaration(symbol, namespace);
401 assert(declaration != previous_declaration);
402 if(previous_declaration != NULL
403 && previous_declaration->parent_context == context) {
404 if(!is_compatible_declaration(declaration, previous_declaration)) {
405 parser_print_error_prefix_pos(declaration->source_position);
406 fprintf(stderr, "definition of symbol %s%s with type ",
407 get_namespace_prefix(namespace), symbol->string);
409 print_type(declaration->type);
411 parser_print_error_prefix_pos(
412 previous_declaration->source_position);
413 fprintf(stderr, "is incompatible with previous declaration "
415 print_type(previous_declaration->type);
418 return previous_declaration;
421 /* remember old declaration */
423 entry.symbol = symbol;
424 entry.old_declaration = symbol->declaration;
425 entry.namespace = namespace;
426 ARR_APP1(*stack_ptr, entry);
428 /* replace/add declaration into declaration list of the symbol */
429 if(symbol->declaration == NULL) {
430 symbol->declaration = declaration;
432 declaration_t *iter_last = NULL;
433 declaration_t *iter = symbol->declaration;
434 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
435 /* replace an entry? */
436 if(iter->namespace == namespace) {
437 if(iter_last == NULL) {
438 symbol->declaration = declaration;
440 iter_last->symbol_next = declaration;
442 declaration->symbol_next = iter->symbol_next;
447 assert(iter_last->symbol_next == NULL);
448 iter_last->symbol_next = declaration;
455 static declaration_t *environment_push(declaration_t *declaration)
457 assert(declaration->source_position.input_name != NULL);
458 return stack_push(&environment_stack, declaration, context);
461 static declaration_t *label_push(declaration_t *declaration)
463 return stack_push(&label_stack, declaration, ¤t_function->context);
467 * pops symbols from the environment stack until @p new_top is the top element
469 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
471 stack_entry_t *stack = *stack_ptr;
472 size_t top = ARR_LEN(stack);
475 assert(new_top <= top);
479 for(i = top; i > new_top; --i) {
480 stack_entry_t *entry = & stack[i - 1];
482 declaration_t *old_declaration = entry->old_declaration;
483 symbol_t *symbol = entry->symbol;
484 namespace_t namespace = entry->namespace;
486 /* replace/remove declaration */
487 declaration_t *declaration = symbol->declaration;
488 assert(declaration != NULL);
489 if(declaration->namespace == namespace) {
490 if(old_declaration == NULL) {
491 symbol->declaration = declaration->symbol_next;
493 symbol->declaration = old_declaration;
494 assert(old_declaration->symbol_next ==
495 declaration->symbol_next);
498 for(; declaration != NULL; declaration = declaration->symbol_next) {
499 declaration_t *symbol_next = declaration->symbol_next;
500 if(symbol_next->namespace == namespace) {
501 declaration->symbol_next = old_declaration;
502 assert(old_declaration->symbol_next
503 == symbol_next->symbol_next);
507 assert(declaration != NULL);
511 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
514 static void environment_pop_to(size_t new_top)
516 stack_pop_to(&environment_stack, new_top);
519 static void label_pop_to(size_t new_top)
521 stack_pop_to(&label_stack, new_top);
525 static int get_rank(const type_t *type)
527 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
528 * and esp. footnote 108). However we can't fold constants (yet), so we
529 * can't decide wether unsigned int is possible, while int always works.
530 * (unsigned int would be preferable when possible... for stuff like
531 * struct { enum { ... } bla : 4; } ) */
532 if(type->type == TYPE_ENUM)
533 return ATOMIC_TYPE_INT;
535 assert(type->type == TYPE_ATOMIC);
536 atomic_type_t *atomic_type = (atomic_type_t*) type;
537 atomic_type_type_t atype = atomic_type->atype;
541 static type_t *promote_integer(type_t *type)
543 if(get_rank(type) < ATOMIC_TYPE_INT)
549 static expression_t *create_cast_expression(expression_t *expression,
552 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
554 cast->expression.type = EXPR_UNARY;
555 cast->type = UNEXPR_CAST;
556 cast->value = expression;
557 cast->expression.datatype = dest_type;
559 return (expression_t*) cast;
562 static expression_t *create_implicit_cast(expression_t *expression,
565 type_t *source_type = expression->datatype;
567 if(source_type == NULL)
570 source_type = skip_typeref(source_type);
571 dest_type = skip_typeref(dest_type);
573 if(source_type == dest_type)
576 if(dest_type->type == TYPE_ATOMIC) {
577 if(source_type->type != TYPE_ATOMIC)
578 panic("casting of non-atomic types not implemented yet");
580 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
581 type_error_incompatible("can't cast types",
582 expression->source_position,
583 source_type, dest_type);
587 return create_cast_expression(expression, dest_type);
589 if(dest_type->type == TYPE_POINTER) {
590 if(source_type->type == TYPE_POINTER) {
591 if(!pointers_compatible(source_type, dest_type)) {
592 type_error_incompatible("can't implicitely cast types",
593 expression->source_position,
594 source_type, dest_type);
596 return create_cast_expression(expression, dest_type);
601 panic("casting of non-atomic types not implemented yet");
604 static void semantic_assign(type_t *orig_type_left, expression_t **right,
607 type_t *orig_type_right = (*right)->datatype;
609 if(orig_type_right == NULL)
612 type_t *type_left = skip_typeref(orig_type_left);
613 type_t *type_right = skip_typeref(orig_type_right);
615 if(type_left == type_right) {
617 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
618 *right = create_implicit_cast(*right, type_left);
619 } else if(type_left->type == TYPE_POINTER
620 && type_right->type == TYPE_POINTER) {
623 /* TODO: improve error message */
624 parser_print_error_prefix();
625 fprintf(stderr, "incompatible types in %s\n", context);
626 parser_print_error_prefix();
627 print_type(type_left);
628 fputs(" <- ", stderr);
629 print_type(type_right);
635 static expression_t *parse_constant_expression(void)
637 /* start parsing at precedence 7 (conditional expression) */
638 return parse_sub_expression(7);
641 static expression_t *parse_assignment_expression(void)
643 /* start parsing at precedence 2 (assignment expression) */
644 return parse_sub_expression(2);
647 static void parse_compound_type_entries(void);
648 static declaration_t *parse_declarator(storage_class_t storage_class,
649 type_t *type, int may_be_abstract);
650 static declaration_t *record_declaration(declaration_t *declaration);
652 typedef struct declaration_specifiers_t declaration_specifiers_t;
653 struct declaration_specifiers_t {
654 storage_class_t storage_class;
658 static const char *parse_string_literals(void)
660 assert(token.type == T_STRING_LITERAL);
661 const char *result = token.v.string;
665 while(token.type == T_STRING_LITERAL) {
666 result = concat_strings(result, token.v.string);
673 static void parse_attributes(void)
677 case T___attribute__:
685 parse_error("EOF while parsing attribute");
703 if(token.type != T_STRING_LITERAL) {
704 parse_error_expected("while parsing assembler attribute",
709 parse_string_literals();
714 goto attributes_finished;
722 static designator_t *parse_designation(void)
724 if(token.type != '[' && token.type != '.')
727 designator_t *result = NULL;
728 designator_t *last = NULL;
731 designator_t *designator;
734 designator = allocate_ast_zero(sizeof(designator[0]));
736 designator->array_access = parse_constant_expression();
740 designator = allocate_ast_zero(sizeof(designator[0]));
742 if(token.type != T_IDENTIFIER) {
743 parse_error_expected("problem while parsing designator",
747 designator->symbol = token.v.symbol;
755 assert(designator != NULL);
757 last->next = designator;
765 static initializer_t *parse_initializer_list(type_t *type);
767 static initializer_t *parse_initializer(type_t *type)
769 designator_t *designator = parse_designation();
771 initializer_t *result;
772 if(token.type == '{') {
773 result = parse_initializer_list(type);
775 result = allocate_ast_zero(sizeof(result[0]));
776 result->type = INITIALIZER_VALUE;
777 result->v.value = parse_assignment_expression();
780 semantic_assign(type, &result->v.value, "initializer");
783 result->designator = designator;
788 static initializer_t *parse_initializer_list(type_t *type)
795 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
796 result->type = INITIALIZER_LIST;
798 initializer_t *last = NULL;
800 initializer_t *initializer = parse_initializer(NULL);
802 last->next = initializer;
804 result->v.list = initializer;
808 if(token.type == '}')
811 if(token.type != ',') {
812 parse_error_expected("problem while parsing initializer list",
819 if(token.type == '}')
828 static declaration_t *parse_compound_type_specifier(bool is_struct)
836 symbol_t *symbol = NULL;
837 declaration_t *declaration = NULL;
839 if(token.type == T_IDENTIFIER) {
840 symbol = token.v.symbol;
844 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
846 declaration = get_declaration(symbol, NAMESPACE_UNION);
848 } else if(token.type != '{') {
850 parse_error_expected("problem while parsing struct type specifier",
851 T_IDENTIFIER, '{', 0);
853 parse_error_expected("problem while parsing union type specifier",
854 T_IDENTIFIER, '{', 0);
860 if(declaration == NULL) {
861 declaration = allocate_type_zero(sizeof(declaration[0]));
864 declaration->namespace = NAMESPACE_STRUCT;
866 declaration->namespace = NAMESPACE_UNION;
868 declaration->source_position = token.source_position;
869 declaration->symbol = symbol;
872 if(token.type == '{') {
873 if(declaration->init.is_defined) {
874 assert(symbol != NULL);
875 parser_print_error_prefix();
876 fprintf(stderr, "multiple definition of %s %s\n",
877 is_struct ? "struct" : "union", symbol->string);
878 declaration->context.declarations = NULL;
880 record_declaration(declaration);
881 declaration->init.is_defined = true;
883 int top = environment_top();
884 context_t *last_context = context;
885 set_context(& declaration->context);
887 parse_compound_type_entries();
890 assert(context == & declaration->context);
891 set_context(last_context);
892 environment_pop_to(top);
898 static void parse_enum_entries(void)
902 if(token.type == '}') {
904 parse_error("empty enum not allowed");
909 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
911 if(token.type != T_IDENTIFIER) {
912 parse_error_expected("problem while parsing enum entry",
917 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
918 entry->symbol = token.v.symbol;
919 entry->source_position = token.source_position;
922 if(token.type == '=') {
924 entry->init.initializer = parse_initializer(type_int);
927 record_declaration(entry);
929 if(token.type != ',')
932 } while(token.type != '}');
937 static declaration_t *parse_enum_specifier(void)
941 declaration_t *declaration;
944 if(token.type == T_IDENTIFIER) {
945 symbol = token.v.symbol;
948 declaration = get_declaration(symbol, NAMESPACE_ENUM);
949 } else if(token.type != '{') {
950 parse_error_expected("problem while parsing enum type specifier",
951 T_IDENTIFIER, '{', 0);
958 if(declaration == NULL) {
959 declaration = allocate_type_zero(sizeof(declaration[0]));
961 declaration->namespace = NAMESPACE_ENUM;
962 declaration->source_position = token.source_position;
963 declaration->symbol = symbol;
966 if(token.type == '{') {
967 if(declaration->init.is_defined) {
968 parser_print_error_prefix();
969 fprintf(stderr, "multiple definitions of enum %s\n",
972 record_declaration(declaration);
973 declaration->init.is_defined = 1;
975 parse_enum_entries();
983 * if a symbol is a typedef to another type, return true
985 static bool is_typedef_symbol(symbol_t *symbol)
987 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
988 if(declaration == NULL
989 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
995 static type_t *parse_typeof(void)
1003 expression_t *expression = NULL;
1006 switch(token.type) {
1007 case T___extension__:
1008 /* this can be a prefix to a typename or an expression */
1009 /* we simply eat it now. */
1012 } while(token.type == T___extension__);
1016 if(is_typedef_symbol(token.v.symbol)) {
1017 type = parse_typename();
1019 expression = parse_expression();
1020 type = expression->datatype;
1025 type = parse_typename();
1029 expression = parse_expression();
1030 type = expression->datatype;
1036 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1037 typeof->type.type = TYPE_TYPEOF;
1038 typeof->expression = expression;
1039 typeof->typeof_type = type;
1041 return (type_t*) typeof;
1045 SPECIFIER_SIGNED = 1 << 0,
1046 SPECIFIER_UNSIGNED = 1 << 1,
1047 SPECIFIER_LONG = 1 << 2,
1048 SPECIFIER_INT = 1 << 3,
1049 SPECIFIER_DOUBLE = 1 << 4,
1050 SPECIFIER_CHAR = 1 << 5,
1051 SPECIFIER_SHORT = 1 << 6,
1052 SPECIFIER_LONG_LONG = 1 << 7,
1053 SPECIFIER_FLOAT = 1 << 8,
1054 SPECIFIER_BOOL = 1 << 9,
1055 SPECIFIER_VOID = 1 << 10,
1056 #ifdef PROVIDE_COMPLEX
1057 SPECIFIER_COMPLEX = 1 << 11,
1059 #ifdef PROVIDE_IMAGINARY
1060 SPECIFIER_IMAGINARY = 1 << 12,
1064 static type_t *create_builtin_type(symbol_t *symbol)
1066 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1067 type->type.type = TYPE_BUILTIN;
1068 type->symbol = symbol;
1070 type->real_type = type_int;
1072 return (type_t*) type;
1075 static type_t *get_typedef_type(symbol_t *symbol)
1077 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1078 if(declaration == NULL
1079 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1082 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1083 typedef_type->type.type = TYPE_TYPEDEF;
1084 typedef_type->declaration = declaration;
1086 return (type_t*) typedef_type;
1089 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1091 type_t *type = NULL;
1092 unsigned type_qualifiers = 0;
1093 unsigned type_specifiers = 0;
1097 switch(token.type) {
1100 #define MATCH_STORAGE_CLASS(token, class) \
1102 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1103 parse_error("multiple storage classes in declaration " \
1106 specifiers->storage_class = class; \
1110 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1111 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1112 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1113 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1114 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1116 /* type qualifiers */
1117 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1119 type_qualifiers |= qualifier; \
1123 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1124 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1125 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1126 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1128 case T___extension__:
1133 /* type specifiers */
1134 #define MATCH_SPECIFIER(token, specifier, name) \
1137 if(type_specifiers & specifier) { \
1138 parse_error("multiple " name " type specifiers given"); \
1140 type_specifiers |= specifier; \
1144 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1145 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1146 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1147 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1148 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1149 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1150 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1151 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1152 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1153 #ifdef PROVIDE_COMPLEX
1154 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1156 #ifdef PROVIDE_IMAGINARY
1157 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1161 if(type_specifiers & SPECIFIER_LONG_LONG) {
1162 parse_error("multiple type specifiers given");
1163 } else if(type_specifiers & SPECIFIER_LONG) {
1164 type_specifiers |= SPECIFIER_LONG_LONG;
1166 type_specifiers |= SPECIFIER_LONG;
1170 /* TODO: if type != NULL for the following rules should issue
1173 compound_type_t *compound_type
1174 = allocate_type_zero(sizeof(compound_type[0]));
1175 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1176 compound_type->declaration = parse_compound_type_specifier(true);
1178 type = (type_t*) compound_type;
1182 compound_type_t *compound_type
1183 = allocate_type_zero(sizeof(compound_type[0]));
1184 compound_type->type.type = TYPE_COMPOUND_UNION;
1185 compound_type->declaration = parse_compound_type_specifier(false);
1187 type = (type_t*) compound_type;
1191 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1192 enum_type->type.type = TYPE_ENUM;
1193 enum_type->declaration = parse_enum_specifier();
1195 type = (type_t*) enum_type;
1199 type = parse_typeof();
1201 case T___builtin_va_list:
1202 type = create_builtin_type(token.v.symbol);
1206 case T___attribute__:
1211 case T_IDENTIFIER: {
1212 type_t *typedef_type = get_typedef_type(token.v.symbol);
1214 if(typedef_type == NULL)
1215 goto finish_specifiers;
1218 type = typedef_type;
1222 /* function specifier */
1224 goto finish_specifiers;
1231 atomic_type_type_t atomic_type;
1233 /* match valid basic types */
1234 switch(type_specifiers) {
1235 case SPECIFIER_VOID:
1236 atomic_type = ATOMIC_TYPE_VOID;
1238 case SPECIFIER_CHAR:
1239 atomic_type = ATOMIC_TYPE_CHAR;
1241 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1242 atomic_type = ATOMIC_TYPE_SCHAR;
1244 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1245 atomic_type = ATOMIC_TYPE_UCHAR;
1247 case SPECIFIER_SHORT:
1248 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1249 case SPECIFIER_SHORT | SPECIFIER_INT:
1250 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1251 atomic_type = ATOMIC_TYPE_SHORT;
1253 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1254 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1255 atomic_type = ATOMIC_TYPE_USHORT;
1258 case SPECIFIER_SIGNED:
1259 case SPECIFIER_SIGNED | SPECIFIER_INT:
1260 atomic_type = ATOMIC_TYPE_INT;
1262 case SPECIFIER_UNSIGNED:
1263 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1264 atomic_type = ATOMIC_TYPE_UINT;
1266 case SPECIFIER_LONG:
1267 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1268 case SPECIFIER_LONG | SPECIFIER_INT:
1269 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1270 atomic_type = ATOMIC_TYPE_LONG;
1272 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1273 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1274 atomic_type = ATOMIC_TYPE_ULONG;
1276 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1277 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1278 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1279 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1281 atomic_type = ATOMIC_TYPE_LONGLONG;
1283 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1284 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1286 atomic_type = ATOMIC_TYPE_ULONGLONG;
1288 case SPECIFIER_FLOAT:
1289 atomic_type = ATOMIC_TYPE_FLOAT;
1291 case SPECIFIER_DOUBLE:
1292 atomic_type = ATOMIC_TYPE_DOUBLE;
1294 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1295 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1297 case SPECIFIER_BOOL:
1298 atomic_type = ATOMIC_TYPE_BOOL;
1300 #ifdef PROVIDE_COMPLEX
1301 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1302 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1304 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1305 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1307 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1308 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1311 #ifdef PROVIDE_IMAGINARY
1312 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1313 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1315 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1316 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1318 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1319 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1323 /* invalid specifier combination, give an error message */
1324 if(type_specifiers == 0) {
1326 parse_warning("no type specifiers in declaration (using int)");
1327 atomic_type = ATOMIC_TYPE_INT;
1330 parse_error("no type specifiers given in declaration");
1332 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1333 (type_specifiers & SPECIFIER_UNSIGNED)) {
1334 parse_error("signed and unsigned specifiers gives");
1335 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1336 parse_error("only integer types can be signed or unsigned");
1338 parse_error("multiple datatypes in declaration");
1340 atomic_type = ATOMIC_TYPE_INVALID;
1343 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1344 atype->type.type = TYPE_ATOMIC;
1345 atype->atype = atomic_type;
1348 type = (type_t*) atype;
1350 if(type_specifiers != 0) {
1351 parse_error("multiple datatypes in declaration");
1355 type->qualifiers = type_qualifiers;
1357 type_t *result = typehash_insert(type);
1358 if(newtype && result != (type_t*) type) {
1362 specifiers->type = result;
1365 static type_qualifier_t parse_type_qualifiers(void)
1367 type_qualifier_t type_qualifiers = 0;
1370 switch(token.type) {
1371 /* type qualifiers */
1372 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1373 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1374 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1375 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1378 return type_qualifiers;
1383 static void parse_identifier_list(void)
1386 if(token.type != T_IDENTIFIER) {
1387 parse_error_expected("problem while parsing parameter identifier "
1388 "list", T_IDENTIFIER, 0);
1392 if(token.type != ',')
1398 static declaration_t *parse_parameter(void)
1400 declaration_specifiers_t specifiers;
1401 memset(&specifiers, 0, sizeof(specifiers));
1403 parse_declaration_specifiers(&specifiers);
1405 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1406 specifiers.type, 1);
1408 /* TODO check declaration constraints for parameters */
1409 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1410 parse_error("typedef not allowed in parameter list");
1416 static declaration_t *parse_parameters(function_type_t *type)
1418 if(token.type == T_IDENTIFIER) {
1419 symbol_t *symbol = token.v.symbol;
1420 if(!is_typedef_symbol(symbol)) {
1422 parse_identifier_list();
1427 if(token.type == ')') {
1428 type->unspecified_parameters = 1;
1431 if(token.type == T_void && look_ahead(1)->type == ')') {
1436 declaration_t *declarations = NULL;
1437 declaration_t *declaration;
1438 declaration_t *last_declaration = NULL;
1439 function_parameter_t *parameter;
1440 function_parameter_t *last_parameter = NULL;
1443 switch(token.type) {
1447 return declarations;
1450 case T___extension__:
1452 declaration = parse_parameter();
1454 parameter = allocate_type_zero(sizeof(parameter[0]));
1455 parameter->type = declaration->type;
1457 if(last_parameter != NULL) {
1458 last_declaration->next = declaration;
1459 last_parameter->next = parameter;
1461 type->parameters = parameter;
1462 declarations = declaration;
1464 last_parameter = parameter;
1465 last_declaration = declaration;
1469 return declarations;
1471 if(token.type != ',')
1472 return declarations;
1481 } construct_type_type_t;
1483 typedef struct construct_type_t construct_type_t;
1484 struct construct_type_t {
1485 construct_type_type_t type;
1486 construct_type_t *next;
1489 typedef struct parsed_pointer_t parsed_pointer_t;
1490 struct parsed_pointer_t {
1491 construct_type_t construct_type;
1492 type_qualifier_t type_qualifiers;
1495 typedef struct construct_function_type_t construct_function_type_t;
1496 struct construct_function_type_t {
1497 construct_type_t construct_type;
1498 function_type_t *function_type;
1501 typedef struct parsed_array_t parsed_array_t;
1502 struct parsed_array_t {
1503 construct_type_t construct_type;
1504 type_qualifier_t type_qualifiers;
1510 typedef struct construct_base_type_t construct_base_type_t;
1511 struct construct_base_type_t {
1512 construct_type_t construct_type;
1516 static construct_type_t *parse_pointer_declarator(void)
1520 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1521 memset(pointer, 0, sizeof(pointer[0]));
1522 pointer->type_qualifiers = parse_type_qualifiers();
1524 return (construct_type_t*) pointer;
1527 static construct_type_t *parse_array_declarator(void)
1531 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1532 memset(array, 0, sizeof(array[0]));
1534 if(token.type == T_static) {
1535 array->is_static = true;
1539 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1540 if(type_qualifiers != 0) {
1541 if(token.type == T_static) {
1542 array->is_static = true;
1546 array->type_qualifiers = type_qualifiers;
1548 if(token.type == '*' && look_ahead(1)->type == ']') {
1549 array->is_variable = true;
1551 } else if(token.type != ']') {
1552 array->size = parse_assignment_expression();
1557 return (construct_type_t*) array;
1560 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1564 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1565 type->type.type = TYPE_FUNCTION;
1567 declaration_t *parameters = parse_parameters(type);
1568 if(declaration != NULL) {
1569 declaration->context.declarations = parameters;
1572 construct_function_type_t *construct_function_type =
1573 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1574 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1575 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1576 construct_function_type->function_type = type;
1580 return (construct_type_t*) construct_function_type;
1583 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1584 int may_be_abstract)
1586 construct_type_t *result = NULL;
1587 construct_type_t *last = NULL;
1589 while(token.type == '*') {
1590 construct_type_t *type = parse_pointer_declarator();
1599 /* TODO: find out if this is correct */
1602 construct_type_t *inner_types = NULL;
1604 switch(token.type) {
1606 if(declaration == NULL) {
1607 parse_error("no identifier expected in typename");
1609 declaration->symbol = token.v.symbol;
1610 declaration->source_position = token.source_position;
1616 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1622 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1627 construct_type_t *type;
1628 switch(token.type) {
1630 type = parse_function_declarator(declaration);
1633 type = parse_array_declarator();
1636 goto declarator_finished;
1647 declarator_finished:
1650 if(inner_types != NULL) {
1652 last->next = inner_types;
1654 result = inner_types;
1662 static type_t *construct_declarator_type(construct_type_t *construct_list,
1665 construct_type_t *iter = construct_list;
1666 for( ; iter != NULL; iter = iter->next) {
1667 parsed_pointer_t *parsed_pointer;
1668 parsed_array_t *parsed_array;
1669 construct_function_type_t *construct_function_type;
1670 function_type_t *function_type;
1671 pointer_type_t *pointer_type;
1672 array_type_t *array_type;
1674 switch(iter->type) {
1675 case CONSTRUCT_FUNCTION:
1676 construct_function_type = (construct_function_type_t*) iter;
1677 function_type = construct_function_type->function_type;
1679 function_type->result_type = type;
1680 type = (type_t*) function_type;
1683 case CONSTRUCT_POINTER:
1684 parsed_pointer = (parsed_pointer_t*) iter;
1685 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1687 pointer_type->type.type = TYPE_POINTER;
1688 pointer_type->points_to = type;
1689 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1690 type = (type_t*) pointer_type;
1693 case CONSTRUCT_ARRAY:
1694 parsed_array = (parsed_array_t*) iter;
1695 array_type = allocate_type_zero(sizeof(array_type[0]));
1697 array_type->type.type = TYPE_ARRAY;
1698 array_type->element_type = type;
1699 array_type->type.qualifiers = parsed_array->type_qualifiers;
1700 array_type->is_static = parsed_array->is_static;
1701 array_type->is_variable = parsed_array->is_variable;
1702 array_type->size = parsed_array->size;
1703 type = (type_t*) array_type;
1707 type_t *hashed_type = typehash_insert((type_t*) type);
1708 if(hashed_type != type) {
1717 static declaration_t *parse_declarator(storage_class_t storage_class,
1718 type_t *type, int may_be_abstract)
1720 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1721 declaration->storage_class = storage_class;
1723 construct_type_t *construct_type
1724 = parse_inner_declarator(declaration, may_be_abstract);
1725 declaration->type = construct_declarator_type(construct_type, type);
1727 if(construct_type != NULL) {
1728 obstack_free(&temp_obst, construct_type);
1734 static type_t *parse_abstract_declarator(type_t *base_type)
1736 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1738 type_t *result = construct_declarator_type(construct_type, base_type);
1739 if(construct_type != NULL) {
1740 obstack_free(&temp_obst, construct_type);
1746 static declaration_t *record_declaration(declaration_t *declaration)
1748 assert(context != NULL);
1750 symbol_t *symbol = declaration->symbol;
1751 if(symbol != NULL) {
1752 declaration_t *alias = environment_push(declaration);
1753 if(alias != declaration)
1756 declaration->parent_context = context;
1759 if(last_declaration != NULL) {
1760 last_declaration->next = declaration;
1762 context->declarations = declaration;
1764 last_declaration = declaration;
1769 static void parser_error_multiple_definition(declaration_t *previous,
1770 declaration_t *declaration)
1772 parser_print_error_prefix_pos(declaration->source_position);
1773 fprintf(stderr, "multiple definition of symbol '%s'\n",
1774 declaration->symbol->string);
1775 parser_print_error_prefix_pos(previous->source_position);
1776 fprintf(stderr, "this is the location of the previous "
1781 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1784 declaration_t *ndeclaration
1785 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1787 declaration_t *declaration = record_declaration(ndeclaration);
1788 if(token.type == '=') {
1791 /* TODO: check that this is an allowed type (no function type) */
1793 if(declaration->init.initializer != NULL) {
1794 parser_error_multiple_definition(declaration, ndeclaration);
1797 ndeclaration->init.initializer = parse_initializer(declaration->type);
1798 } else if(token.type == '{') {
1799 if(declaration->type->type != TYPE_FUNCTION) {
1800 parser_print_error_prefix();
1801 fprintf(stderr, "Declarator ");
1802 print_type_ext(declaration->type, declaration->symbol, NULL);
1803 fprintf(stderr, " has a body but is not a function type.\n");
1808 if(declaration->init.statement != NULL) {
1809 parser_error_multiple_definition(declaration, ndeclaration);
1811 if(ndeclaration != declaration) {
1812 memcpy(&declaration->context, &ndeclaration->context,
1813 sizeof(declaration->context));
1816 int top = environment_top();
1817 context_t *last_context = context;
1818 set_context(&declaration->context);
1820 /* push function parameters */
1821 declaration_t *parameter = declaration->context.declarations;
1822 for( ; parameter != NULL; parameter = parameter->next) {
1823 environment_push(parameter);
1826 int label_stack_top = label_top();
1827 declaration_t *old_current_function = current_function;
1828 current_function = declaration;
1830 statement_t *statement = parse_compound_statement();
1832 assert(current_function == declaration);
1833 current_function = old_current_function;
1834 label_pop_to(label_stack_top);
1836 assert(context == &declaration->context);
1837 set_context(last_context);
1838 environment_pop_to(top);
1840 declaration->init.statement = statement;
1844 if(token.type != ',')
1851 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1854 if(token.type == ':') {
1856 parse_constant_expression();
1857 /* TODO (bitfields) */
1859 declaration_t *declaration
1860 = parse_declarator(specifiers->storage_class,
1861 specifiers->type, 1);
1863 /* TODO: check constraints for struct declarations */
1864 /* TODO: check for doubled fields */
1865 record_declaration(declaration);
1867 if(token.type == ':') {
1869 parse_constant_expression();
1870 /* TODO (bitfields) */
1874 if(token.type != ',')
1881 static void parse_compound_type_entries(void)
1885 while(token.type != '}' && token.type != T_EOF) {
1886 declaration_specifiers_t specifiers;
1887 memset(&specifiers, 0, sizeof(specifiers));
1888 parse_declaration_specifiers(&specifiers);
1890 parse_struct_declarators(&specifiers);
1892 if(token.type == T_EOF) {
1893 parse_error("unexpected error while parsing struct");
1898 static void parse_declaration(void)
1900 source_position_t source_position = token.source_position;
1902 declaration_specifiers_t specifiers;
1903 memset(&specifiers, 0, sizeof(specifiers));
1904 parse_declaration_specifiers(&specifiers);
1906 if(token.type == ';') {
1909 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1911 declaration->type = specifiers.type;
1912 declaration->storage_class = specifiers.storage_class;
1913 declaration->source_position = source_position;
1914 record_declaration(declaration);
1917 parse_init_declarators(&specifiers);
1920 static type_t *parse_typename(void)
1922 declaration_specifiers_t specifiers;
1923 memset(&specifiers, 0, sizeof(specifiers));
1924 parse_declaration_specifiers(&specifiers);
1925 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1926 /* TODO: improve error message, user does probably not know what a
1927 * storage class is...
1929 parse_error("typename may not have a storage class");
1932 type_t *result = parse_abstract_declarator(specifiers.type);
1940 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1941 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1942 expression_t *left);
1944 typedef struct expression_parser_function_t expression_parser_function_t;
1945 struct expression_parser_function_t {
1946 unsigned precedence;
1947 parse_expression_function parser;
1948 unsigned infix_precedence;
1949 parse_expression_infix_function infix_parser;
1952 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1954 static expression_t *expected_expression_error(void)
1956 parser_print_error_prefix();
1957 fprintf(stderr, "expected expression, got token ");
1958 print_token(stderr, & token);
1959 fprintf(stderr, "\n");
1961 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1962 expression->type = EXPR_INVALID;
1968 static expression_t *parse_string_const(void)
1970 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1972 cnst->expression.type = EXPR_STRING_LITERAL;
1973 cnst->expression.datatype = type_string;
1974 cnst->value = parse_string_literals();
1976 return (expression_t*) cnst;
1979 static expression_t *parse_int_const(void)
1981 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1983 cnst->expression.type = EXPR_CONST;
1984 cnst->expression.datatype = type_int;
1985 cnst->v.int_value = token.v.intvalue;
1989 return (expression_t*) cnst;
1992 static expression_t *parse_float_const(void)
1994 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1996 cnst->expression.type = EXPR_CONST;
1997 cnst->expression.datatype = type_double;
1998 cnst->v.float_value = token.v.floatvalue;
2002 return (expression_t*) cnst;
2005 static declaration_t *create_implicit_function(symbol_t *symbol,
2006 const source_position_t source_position)
2008 function_type_t *function_type
2009 = allocate_type_zero(sizeof(function_type[0]));
2011 function_type->type.type = TYPE_FUNCTION;
2012 function_type->result_type = type_int;
2013 function_type->unspecified_parameters = true;
2015 type_t *type = typehash_insert((type_t*) function_type);
2016 if(type != (type_t*) function_type) {
2017 free_type(function_type);
2020 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2022 declaration->storage_class = STORAGE_CLASS_EXTERN;
2023 declaration->type = type;
2024 declaration->symbol = symbol;
2025 declaration->source_position = source_position;
2027 /* prepend the implicit definition to the global context
2028 * this is safe since the symbol wasn't declared as anything else yet
2030 assert(symbol->declaration == NULL);
2032 context_t *last_context = context;
2033 context = global_context;
2035 environment_push(declaration);
2036 declaration->next = context->declarations;
2037 context->declarations = declaration;
2039 context = last_context;
2044 static expression_t *parse_reference(void)
2046 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2048 ref->expression.type = EXPR_REFERENCE;
2049 ref->symbol = token.v.symbol;
2051 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2053 source_position_t source_position = token.source_position;
2056 if(declaration == NULL) {
2058 /* an implicitely defined function */
2059 if(token.type == '(') {
2060 parser_print_prefix_pos(token.source_position);
2061 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2062 ref->symbol->string);
2064 declaration = create_implicit_function(ref->symbol,
2069 parser_print_error_prefix();
2070 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2071 return (expression_t*) ref;
2075 ref->declaration = declaration;
2076 ref->expression.datatype = declaration->type;
2078 return (expression_t*) ref;
2081 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2085 /* TODO check if explicit cast is allowed and issue warnings/errors */
2088 static expression_t *parse_cast(void)
2090 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2092 cast->expression.type = EXPR_UNARY;
2093 cast->type = UNEXPR_CAST;
2094 cast->expression.source_position = token.source_position;
2096 type_t *type = parse_typename();
2099 expression_t *value = parse_sub_expression(20);
2101 check_cast_allowed(value, type);
2103 cast->expression.datatype = type;
2104 cast->value = value;
2106 return (expression_t*) cast;
2109 static expression_t *parse_statement_expression(void)
2111 statement_expression_t *expression
2112 = allocate_ast_zero(sizeof(expression[0]));
2113 expression->expression.type = EXPR_STATEMENT;
2115 statement_t *statement = parse_compound_statement();
2116 expression->statement = statement;
2117 if(statement == NULL) {
2122 assert(statement->type == STATEMENT_COMPOUND);
2123 compound_statement_t *compound_statement
2124 = (compound_statement_t*) statement;
2126 /* find last statement and use it's type */
2127 const statement_t *last_statement = NULL;
2128 const statement_t *iter = compound_statement->statements;
2129 for( ; iter != NULL; iter = iter->next) {
2130 last_statement = iter;
2133 if(last_statement->type == STATEMENT_EXPRESSION) {
2134 const expression_statement_t *expression_statement =
2135 (const expression_statement_t*) last_statement;
2136 expression->expression.datatype
2137 = expression_statement->expression->datatype;
2139 expression->expression.datatype = type_void;
2144 return (expression_t*) expression;
2147 static expression_t *parse_brace_expression(void)
2151 switch(token.type) {
2153 /* gcc extension: a stement expression */
2154 return parse_statement_expression();
2158 return parse_cast();
2160 if(is_typedef_symbol(token.v.symbol)) {
2161 return parse_cast();
2165 expression_t *result = parse_expression();
2171 static expression_t *parse_function_keyword(void)
2173 eat(T___FUNCTION__);
2176 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2177 expression->expression.type = EXPR_FUNCTION;
2178 expression->expression.datatype = type_string;
2179 expression->value = "TODO: FUNCTION";
2181 return (expression_t*) expression;
2184 static expression_t *parse_pretty_function_keyword(void)
2186 eat(T___PRETTY_FUNCTION__);
2189 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2190 expression->expression.type = EXPR_PRETTY_FUNCTION;
2191 expression->expression.datatype = type_string;
2192 expression->value = "TODO: PRETTY FUNCTION";
2194 return (expression_t*) expression;
2197 static designator_t *parse_designator(void)
2199 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2201 if(token.type != T_IDENTIFIER) {
2202 parse_error_expected("problem while parsing member designator",
2207 result->symbol = token.v.symbol;
2210 designator_t *last_designator = result;
2212 if(token.type == '.') {
2214 if(token.type != T_IDENTIFIER) {
2215 parse_error_expected("problem while parsing member designator",
2220 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2221 designator->symbol = token.v.symbol;
2224 last_designator->next = designator;
2225 last_designator = designator;
2228 if(token.type == '[') {
2230 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2231 designator->array_access = parse_expression();
2232 if(designator->array_access == NULL) {
2238 last_designator->next = designator;
2239 last_designator = designator;
2248 static expression_t *parse_offsetof(void)
2250 eat(T___builtin_offsetof);
2252 offsetof_expression_t *expression
2253 = allocate_ast_zero(sizeof(expression[0]));
2254 expression->expression.type = EXPR_OFFSETOF;
2255 expression->expression.datatype = type_size_t;
2258 expression->type = parse_typename();
2260 expression->designator = parse_designator();
2263 return (expression_t*) expression;
2266 static expression_t *parse_va_arg(void)
2268 eat(T___builtin_va_arg);
2270 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2271 expression->expression.type = EXPR_VA_ARG;
2274 expression->arg = parse_assignment_expression();
2276 expression->expression.datatype = parse_typename();
2279 return (expression_t*) expression;
2282 static expression_t *parse_builtin_symbol(void)
2284 builtin_symbol_expression_t *expression
2285 = allocate_ast_zero(sizeof(expression[0]));
2286 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2288 /* TODO: set datatype */
2290 expression->symbol = token.v.symbol;
2294 return (expression_t*) expression;
2297 static expression_t *parse_primary_expression(void)
2299 switch(token.type) {
2301 return parse_int_const();
2302 case T_FLOATINGPOINT:
2303 return parse_float_const();
2304 case T_STRING_LITERAL:
2305 return parse_string_const();
2307 return parse_reference();
2308 case T___FUNCTION__:
2309 return parse_function_keyword();
2310 case T___PRETTY_FUNCTION__:
2311 return parse_pretty_function_keyword();
2312 case T___builtin_offsetof:
2313 return parse_offsetof();
2314 case T___builtin_va_arg:
2315 return parse_va_arg();
2316 case T___builtin_expect:
2317 case T___builtin_va_start:
2318 case T___builtin_va_end:
2319 return parse_builtin_symbol();
2322 return parse_brace_expression();
2325 parser_print_error_prefix();
2326 fprintf(stderr, "unexpected token ");
2327 print_token(stderr, &token);
2328 fprintf(stderr, "\n");
2331 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2332 expression->type = EXPR_INVALID;
2333 expression->datatype = type_void;
2338 static expression_t *parse_array_expression(unsigned precedence,
2339 expression_t *array_ref)
2345 array_access_expression_t *array_access
2346 = allocate_ast_zero(sizeof(array_access[0]));
2348 array_access->expression.type = EXPR_ARRAY_ACCESS;
2349 array_access->array_ref = array_ref;
2350 array_access->index = parse_expression();
2352 type_t *array_type = array_ref->datatype;
2353 if(array_type != NULL) {
2354 if(array_type->type == TYPE_POINTER) {
2355 pointer_type_t *pointer = (pointer_type_t*) array_type;
2356 array_access->expression.datatype = pointer->points_to;
2358 parser_print_error_prefix();
2359 fprintf(stderr, "array access on object with non-pointer type ");
2360 print_type(array_type);
2361 fprintf(stderr, "\n");
2365 if(token.type != ']') {
2366 parse_error_expected("Problem while parsing array access", ']', 0);
2367 return (expression_t*) array_access;
2371 return (expression_t*) array_access;
2374 static bool is_declaration_specifier(const token_t *token,
2375 bool only_type_specifiers)
2377 switch(token->type) {
2381 return is_typedef_symbol(token->v.symbol);
2384 if(only_type_specifiers)
2393 static expression_t *parse_sizeof(unsigned precedence)
2397 sizeof_expression_t *sizeof_expression
2398 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2399 sizeof_expression->expression.type = EXPR_SIZEOF;
2400 sizeof_expression->expression.datatype = type_size_t;
2402 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2404 sizeof_expression->type = parse_typename();
2407 expression_t *expression = parse_sub_expression(precedence);
2408 sizeof_expression->type = expression->datatype;
2409 sizeof_expression->size_expression = expression;
2412 return (expression_t*) sizeof_expression;
2415 static expression_t *parse_select_expression(unsigned precedence,
2416 expression_t *compound)
2420 assert(token.type == '.' || token.type == T_MINUSGREATER);
2423 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2425 select->expression.type = EXPR_SELECT;
2426 select->compound = compound;
2428 /* TODO: datatype */
2430 if(token.type != T_IDENTIFIER) {
2431 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2432 return (expression_t*) select;
2434 select->symbol = token.v.symbol;
2437 return (expression_t*) select;
2440 static expression_t *parse_call_expression(unsigned precedence,
2441 expression_t *expression)
2444 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2445 call->expression.type = EXPR_CALL;
2446 call->function = expression;
2448 function_type_t *function_type;
2449 type_t *type = expression->datatype;
2450 if(type->type != TYPE_FUNCTION) {
2451 /* TODO calling pointers to functions is ok */
2452 parser_print_error_prefix();
2453 fputs("called object '", stderr);
2454 print_expression(expression);
2455 fputs("' (type ", stderr);
2457 fputs("is not a function\n", stderr);
2459 function_type = NULL;
2460 call->expression.datatype = NULL;
2462 function_type = (function_type_t*) type;
2463 call->expression.datatype = function_type->result_type;
2466 /* parse arguments */
2469 if(token.type != ')') {
2470 call_argument_t *last_argument = NULL;
2473 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2475 argument->expression = parse_assignment_expression();
2476 if(last_argument == NULL) {
2477 call->arguments = argument;
2479 last_argument->next = argument;
2481 last_argument = argument;
2483 if(token.type != ',')
2490 if(function_type != NULL) {
2491 function_parameter_t *parameter = function_type->parameters;
2492 call_argument_t *argument = call->arguments;
2493 for( ; parameter != NULL && argument != NULL;
2494 parameter = parameter->next, argument = argument->next) {
2495 type_t *expected_type = parameter->type;
2496 /* TODO report context in error messages */
2497 argument->expression = create_implicit_cast(argument->expression,
2500 /* too few parameters */
2501 if(parameter != NULL) {
2502 parser_print_error_prefix();
2503 fprintf(stderr, "too few arguments to function '");
2504 print_expression(expression);
2505 fprintf(stderr, "'\n");
2506 } else if(argument != NULL) {
2507 /* too many parameters */
2508 if(!function_type->variadic
2509 && !function_type->unspecified_parameters) {
2510 parser_print_error_prefix();
2511 fprintf(stderr, "too many arguments to function '");
2512 print_expression(expression);
2513 fprintf(stderr, "'\n");
2515 /* do default promotion */
2516 for( ; argument != NULL; argument = argument->next) {
2517 type_t *type = argument->expression->datatype;
2522 if(is_type_integer(type)) {
2523 type = promote_integer(type);
2524 } else if(type == type_float) {
2527 argument->expression
2528 = create_implicit_cast(argument->expression, type);
2534 return (expression_t*) call;
2537 static type_t *get_type_after_conversion(const type_t *type1,
2538 const type_t *type2)
2542 return (type_t*) type1;
2545 static expression_t *parse_conditional_expression(unsigned precedence,
2546 expression_t *expression)
2550 conditional_expression_t *conditional
2551 = allocate_ast_zero(sizeof(conditional[0]));
2552 conditional->expression.type = EXPR_CONDITIONAL;
2553 conditional->condition = expression;
2556 type_t *condition_type = conditional->condition->datatype;
2557 if(condition_type != NULL) {
2558 if(!is_type_scalar(condition_type)) {
2559 type_error("expected a scalar type", expression->source_position,
2564 conditional->true_expression = parse_expression();
2566 conditional->false_expression = parse_sub_expression(precedence);
2568 type_t *true_type = conditional->true_expression->datatype;
2569 if(true_type == NULL)
2570 return (expression_t*) conditional;
2571 type_t *false_type = conditional->false_expression->datatype;
2572 if(false_type == NULL)
2573 return (expression_t*) conditional;
2576 if(true_type == false_type) {
2577 conditional->expression.datatype = true_type;
2578 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2579 type_t *result = get_type_after_conversion(true_type, false_type);
2580 /* TODO: create implicit convs if necessary */
2581 conditional->expression.datatype = result;
2582 } else if(true_type->type == TYPE_POINTER &&
2583 false_type->type == TYPE_POINTER &&
2584 true /* TODO compatible points_to types */) {
2586 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2587 || (is_null_ptr_const(false_type) &&
2588 true_type->type == TYPE_POINTER) TODO*/ false) {
2590 } else if(/* 1 is pointer to object type, other is void* */ false) {
2593 type_error_incompatible("problem while parsing conditional",
2594 expression->source_position, true_type,
2598 return (expression_t*) conditional;
2601 static expression_t *parse_extension(unsigned precedence)
2603 eat(T___extension__);
2605 /* TODO enable extensions */
2607 return parse_sub_expression(precedence);
2610 static void semantic_incdec(unary_expression_t *expression)
2612 type_t *orig_type = expression->value->datatype;
2613 if(orig_type == NULL)
2616 type_t *type = skip_typeref(orig_type);
2617 if(!is_type_arithmetic(type) && type->type != TYPE_POINTER) {
2618 /* TODO: improve error message */
2619 parser_print_error_prefix();
2620 fprintf(stderr, "operation needs an arithmetic or pointer type\n");
2624 expression->expression.datatype = orig_type;
2627 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
2629 type_t *orig_type = expression->value->datatype;
2630 if(orig_type == NULL)
2633 type_t *type = skip_typeref(orig_type);
2634 if(!is_type_arithmetic(type)) {
2635 /* TODO: improve error message */
2636 parser_print_error_prefix();
2637 fprintf(stderr, "operation needs an arithmetic type\n");
2641 expression->expression.datatype = orig_type;
2644 static void semantic_dereference(unary_expression_t *expression)
2646 type_t *orig_type = expression->value->datatype;
2647 if(orig_type == NULL)
2650 type_t *type = skip_typeref(orig_type);
2651 if(type->type != TYPE_POINTER) {
2652 /* TODO: improve error message */
2653 parser_print_error_prefix();
2654 fprintf(stderr, "operation needs a pointer type\n");
2658 pointer_type_t *pointer_type = (pointer_type_t*) type;
2659 expression->expression.datatype = pointer_type->points_to;
2662 static void semantic_take_addr(unary_expression_t *expression)
2664 type_t *orig_type = expression->value->datatype;
2665 if(orig_type == NULL)
2668 expression->expression.datatype = make_pointer_type(orig_type, 0);
2671 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
2672 static expression_t *parse_##unexpression_type(unsigned precedence) \
2676 unary_expression_t *unary_expression \
2677 = allocate_ast_zero(sizeof(unary_expression[0])); \
2678 unary_expression->expression.type = EXPR_UNARY; \
2679 unary_expression->type = unexpression_type; \
2680 unary_expression->value = parse_sub_expression(precedence); \
2682 sfunc(unary_expression); \
2684 return (expression_t*) unary_expression; \
2687 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, semantic_unexpr_arithmetic)
2688 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, semantic_unexpr_arithmetic)
2689 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, semantic_unexpr_arithmetic)
2690 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE, semantic_dereference)
2691 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS, semantic_take_addr)
2692 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2693 semantic_unexpr_arithmetic)
2694 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2696 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2699 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2701 static expression_t *parse_##unexpression_type(unsigned precedence, \
2702 expression_t *left) \
2704 (void) precedence; \
2707 unary_expression_t *unary_expression \
2708 = allocate_ast_zero(sizeof(unary_expression[0])); \
2709 unary_expression->expression.type = EXPR_UNARY; \
2710 unary_expression->type = unexpression_type; \
2711 unary_expression->value = left; \
2713 sfunc(unary_expression); \
2715 return (expression_t*) unary_expression; \
2718 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2720 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2723 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2725 /* TODO: handle complex + imaginary types */
2727 /* § 6.3.1.8 Usual arithmetic conversions */
2728 if(type_left == type_long_double || type_right == type_long_double) {
2729 return type_long_double;
2730 } else if(type_left == type_double || type_right == type_double) {
2732 } else if(type_left == type_float || type_right == type_float) {
2736 type_right = promote_integer(type_right);
2737 type_left = promote_integer(type_left);
2739 if(type_left == type_right)
2742 bool signed_left = is_type_signed(type_left);
2743 bool signed_right = is_type_signed(type_right);
2744 if(get_rank(type_left) < get_rank(type_right)) {
2745 if(signed_left == signed_right || !signed_right) {
2751 if(signed_left == signed_right || !signed_left) {
2759 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2761 expression_t *left = expression->left;
2762 expression_t *right = expression->right;
2763 type_t *orig_type_left = left->datatype;
2764 type_t *orig_type_right = right->datatype;
2766 if(orig_type_left == NULL || orig_type_right == NULL)
2769 type_t *type_left = skip_typeref(orig_type_left);
2770 type_t *type_right = skip_typeref(orig_type_right);
2772 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2773 /* TODO: improve error message */
2774 parser_print_error_prefix();
2775 fprintf(stderr, "operation needs arithmetic types\n");
2779 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2780 expression->left = create_implicit_cast(left, arithmetic_type);
2781 expression->right = create_implicit_cast(right, arithmetic_type);
2782 expression->expression.datatype = arithmetic_type;
2785 static void semantic_add(binary_expression_t *expression)
2787 expression_t *left = expression->left;
2788 expression_t *right = expression->right;
2789 type_t *orig_type_left = left->datatype;
2790 type_t *orig_type_right = right->datatype;
2792 if(orig_type_left == NULL || orig_type_right == NULL)
2795 type_t *type_left = skip_typeref(orig_type_left);
2796 type_t *type_right = skip_typeref(orig_type_right);
2799 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2800 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2801 expression->left = create_implicit_cast(left, arithmetic_type);
2802 expression->right = create_implicit_cast(right, arithmetic_type);
2803 expression->expression.datatype = arithmetic_type;
2805 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2806 expression->expression.datatype = type_left;
2807 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2808 expression->expression.datatype = type_right;
2810 parser_print_error_prefix();
2811 fprintf(stderr, "invalid operands to binary + (");
2812 print_type(orig_type_left);
2813 fprintf(stderr, ", ");
2814 print_type(orig_type_right);
2815 fprintf(stderr, ")\n");
2819 static void semantic_sub(binary_expression_t *expression)
2821 expression_t *left = expression->left;
2822 expression_t *right = expression->right;
2823 type_t *orig_type_left = left->datatype;
2824 type_t *orig_type_right = right->datatype;
2826 if(orig_type_left == NULL || orig_type_right == NULL)
2829 type_t *type_left = skip_typeref(orig_type_left);
2830 type_t *type_right = skip_typeref(orig_type_right);
2833 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2834 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2835 expression->left = create_implicit_cast(left, arithmetic_type);
2836 expression->right = create_implicit_cast(right, arithmetic_type);
2837 expression->expression.datatype = arithmetic_type;
2839 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2840 expression->expression.datatype = type_left;
2841 } else if(type_left->type == TYPE_POINTER &&
2842 type_right->type == TYPE_POINTER) {
2843 if(!pointers_compatible(type_left, type_right)) {
2844 parser_print_error_prefix();
2845 fprintf(stderr, "pointers to incompatible objects to binary - (");
2846 print_type(orig_type_left);
2847 fprintf(stderr, ", ");
2848 print_type(orig_type_right);
2849 fprintf(stderr, ")\n");
2851 expression->expression.datatype = type_ptrdiff_t;
2854 parser_print_error_prefix();
2855 fprintf(stderr, "invalid operands to binary - (");
2856 print_type(orig_type_left);
2857 fprintf(stderr, ", ");
2858 print_type(orig_type_right);
2859 fprintf(stderr, ")\n");
2863 static void semantic_comparison(binary_expression_t *expression)
2865 expression_t *left = expression->left;
2866 expression_t *right = expression->right;
2867 type_t *orig_type_left = left->datatype;
2868 type_t *orig_type_right = right->datatype;
2870 if(orig_type_left == NULL || orig_type_right == NULL)
2873 type_t *type_left = skip_typeref(orig_type_left);
2874 type_t *type_right = skip_typeref(orig_type_right);
2876 /* TODO non-arithmetic types */
2877 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2878 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2879 expression->left = create_implicit_cast(left, arithmetic_type);
2880 expression->right = create_implicit_cast(right, arithmetic_type);
2881 expression->expression.datatype = arithmetic_type;
2883 expression->expression.datatype = type_int;
2886 static void semantic_arithmetic_assign(binary_expression_t *expression)
2888 expression_t *left = expression->left;
2889 expression_t *right = expression->right;
2890 type_t *orig_type_left = left->datatype;
2891 type_t *orig_type_right = right->datatype;
2893 if(orig_type_left == NULL || orig_type_right == NULL)
2896 type_t *type_left = skip_typeref(orig_type_left);
2897 type_t *type_right = skip_typeref(orig_type_right);
2899 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2900 /* TODO: improve error message */
2901 parser_print_error_prefix();
2902 fprintf(stderr, "operation needs arithmetic types\n");
2906 /* combined instructions are tricky. We can't create an implicit cast on
2907 * the left side, because we need the uncasted form for the store.
2908 * The ast2firm pass has to know that left_type must be right_type
2909 * for the arithmeitc operation and create a cast by itself */
2910 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2911 expression->right = create_implicit_cast(right, arithmetic_type);
2912 expression->expression.datatype = type_left;
2915 static void semantic_logical_op(binary_expression_t *expression)
2917 expression_t *left = expression->left;
2918 expression_t *right = expression->right;
2919 type_t *orig_type_left = left->datatype;
2920 type_t *orig_type_right = right->datatype;
2922 if(orig_type_left == NULL || orig_type_right == NULL)
2925 type_t *type_left = skip_typeref(orig_type_left);
2926 type_t *type_right = skip_typeref(orig_type_right);
2928 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2929 /* TODO: improve error message */
2930 parser_print_error_prefix();
2931 fprintf(stderr, "operation needs arithmetic types\n");
2935 expression->expression.datatype = type_int;
2938 static void semantic_binexpr_assign(binary_expression_t *expression)
2940 expression_t *left = expression->left;
2941 type_t *type_left = left->datatype;
2943 if(type_left != NULL) {
2944 semantic_assign(type_left, &expression->right, "assignment");
2947 expression->expression.datatype = type_left;
2950 static void semantic_comma(binary_expression_t *expression)
2952 expression->expression.datatype = expression->right->datatype;
2955 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2956 static expression_t *parse_##binexpression_type(unsigned precedence, \
2957 expression_t *left) \
2961 expression_t *right = parse_sub_expression(precedence + lr); \
2963 binary_expression_t *binexpr \
2964 = allocate_ast_zero(sizeof(binexpr[0])); \
2965 binexpr->expression.type = EXPR_BINARY; \
2966 binexpr->type = binexpression_type; \
2967 binexpr->left = left; \
2968 binexpr->right = right; \
2971 return (expression_t*) binexpr; \
2974 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
2975 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
2976 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
2977 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
2978 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
2979 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
2980 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
2981 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
2982 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
2983 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
2984 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2985 semantic_comparison, 1)
2986 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
2987 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2988 semantic_comparison, 1)
2989 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
2990 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
2991 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
2992 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
2993 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
2994 /* TODO shift has a bit special semantic */
2995 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2996 semantic_binexpr_arithmetic, 1)
2997 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2998 semantic_binexpr_arithmetic, 1)
2999 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
3000 semantic_arithmetic_assign, 0)
3001 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
3002 semantic_arithmetic_assign, 0)
3003 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
3004 semantic_arithmetic_assign, 0)
3005 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
3006 semantic_arithmetic_assign, 0)
3007 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
3008 semantic_arithmetic_assign, 0)
3009 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
3010 semantic_arithmetic_assign, 0)
3011 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
3012 semantic_arithmetic_assign, 0)
3013 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
3014 semantic_arithmetic_assign, 0)
3015 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
3016 semantic_arithmetic_assign, 0)
3017 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
3018 semantic_arithmetic_assign, 0)
3020 static expression_t *parse_sub_expression(unsigned precedence)
3022 if(token.type < 0) {
3023 return expected_expression_error();
3026 expression_parser_function_t *parser
3027 = &expression_parsers[token.type];
3028 source_position_t source_position = token.source_position;
3031 if(parser->parser != NULL) {
3032 left = parser->parser(parser->precedence);
3034 left = parse_primary_expression();
3036 assert(left != NULL);
3037 left->source_position = source_position;
3040 if(token.type < 0) {
3041 return expected_expression_error();
3044 parser = &expression_parsers[token.type];
3045 if(parser->infix_parser == NULL)
3047 if(parser->infix_precedence < precedence)
3050 left = parser->infix_parser(parser->infix_precedence, left);
3052 assert(left != NULL);
3053 assert(left->type != EXPR_INVALID);
3054 left->source_position = source_position;
3060 static expression_t *parse_expression(void)
3062 return parse_sub_expression(1);
3067 static void register_expression_parser(parse_expression_function parser,
3068 int token_type, unsigned precedence)
3070 expression_parser_function_t *entry = &expression_parsers[token_type];
3072 if(entry->parser != NULL) {
3073 fprintf(stderr, "for token ");
3074 print_token_type(stderr, token_type);
3075 fprintf(stderr, "\n");
3076 panic("trying to register multiple expression parsers for a token");
3078 entry->parser = parser;
3079 entry->precedence = precedence;
3082 static void register_expression_infix_parser(
3083 parse_expression_infix_function parser, int token_type,
3084 unsigned precedence)
3086 expression_parser_function_t *entry = &expression_parsers[token_type];
3088 if(entry->infix_parser != NULL) {
3089 fprintf(stderr, "for token ");
3090 print_token_type(stderr, token_type);
3091 fprintf(stderr, "\n");
3092 panic("trying to register multiple infix expression parsers for a "
3095 entry->infix_parser = parser;
3096 entry->infix_precedence = precedence;
3099 static void init_expression_parsers(void)
3101 memset(&expression_parsers, 0, sizeof(expression_parsers));
3103 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
3104 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3105 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3106 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3107 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3108 T_GREATERGREATER, 16);
3109 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3110 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3111 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3112 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3113 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3114 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3115 T_GREATEREQUAL, 14);
3116 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3117 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3118 T_EXCLAMATIONMARKEQUAL, 13);
3119 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3120 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3121 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3122 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3123 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3124 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3125 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3126 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3127 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3128 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3129 T_ASTERISKEQUAL, 2);
3130 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3131 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3133 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3134 T_LESSLESSEQUAL, 2);
3135 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3136 T_GREATERGREATEREQUAL, 2);
3137 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3139 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3141 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3144 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3146 register_expression_infix_parser(parse_array_expression, '[', 30);
3147 register_expression_infix_parser(parse_call_expression, '(', 30);
3148 register_expression_infix_parser(parse_select_expression, '.', 30);
3149 register_expression_infix_parser(parse_select_expression,
3150 T_MINUSGREATER, 30);
3151 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3153 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3156 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3157 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3158 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3159 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3160 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3161 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3162 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3163 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3164 register_expression_parser(parse_sizeof, T_sizeof, 25);
3165 register_expression_parser(parse_extension, T___extension__, 25);
3169 static statement_t *parse_case_statement(void)
3172 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3173 label->statement.type = STATEMENT_CASE_LABEL;
3174 label->statement.source_position = token.source_position;
3176 label->expression = parse_expression();
3179 label->statement.next = parse_statement();
3181 return (statement_t*) label;
3184 static statement_t *parse_default_statement(void)
3188 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3189 label->statement.type = STATEMENT_CASE_LABEL;
3190 label->statement.source_position = token.source_position;
3193 label->statement.next = parse_statement();
3195 return (statement_t*) label;
3198 static declaration_t *get_label(symbol_t *symbol)
3200 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3201 assert(current_function != NULL);
3202 /* if we found a label in the same function, then we already created the
3204 if(candidate != NULL
3205 && candidate->parent_context == ¤t_function->context) {
3209 /* otherwise we need to create a new one */
3210 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3211 declaration->namespace = NAMESPACE_LABEL;
3212 declaration->symbol = symbol;
3214 label_push(declaration);
3219 static statement_t *parse_label_statement(void)
3221 assert(token.type == T_IDENTIFIER);
3222 symbol_t *symbol = token.v.symbol;
3225 declaration_t *label = get_label(symbol);
3227 /* if source position is already set then the label is defined twice,
3228 * otherwise it was just mentioned in a goto so far */
3229 if(label->source_position.input_name != NULL) {
3230 parser_print_error_prefix();
3231 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3232 parser_print_error_prefix_pos(label->source_position);
3233 fprintf(stderr, "previous definition of '%s' was here\n",
3236 label->source_position = token.source_position;
3239 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3241 label_statement->statement.type = STATEMENT_LABEL;
3242 label_statement->statement.source_position = token.source_position;
3243 label_statement->label = label;
3247 if(token.type == '}') {
3248 parse_error("label at end of compound statement");
3249 return (statement_t*) label_statement;
3251 label_statement->label_statement = parse_statement();
3254 return (statement_t*) label_statement;
3257 static statement_t *parse_if(void)
3261 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3262 statement->statement.type = STATEMENT_IF;
3263 statement->statement.source_position = token.source_position;
3266 statement->condition = parse_expression();
3269 statement->true_statement = parse_statement();
3270 if(token.type == T_else) {
3272 statement->false_statement = parse_statement();
3275 return (statement_t*) statement;
3278 static statement_t *parse_switch(void)
3282 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3283 statement->statement.type = STATEMENT_SWITCH;
3284 statement->statement.source_position = token.source_position;
3287 statement->expression = parse_expression();
3289 statement->body = parse_statement();
3291 return (statement_t*) statement;
3294 static statement_t *parse_while(void)
3298 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3299 statement->statement.type = STATEMENT_WHILE;
3300 statement->statement.source_position = token.source_position;
3303 statement->condition = parse_expression();
3305 statement->body = parse_statement();
3307 return (statement_t*) statement;
3310 static statement_t *parse_do(void)
3314 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3315 statement->statement.type = STATEMENT_DO_WHILE;
3316 statement->statement.source_position = token.source_position;
3318 statement->body = parse_statement();
3321 statement->condition = parse_expression();
3325 return (statement_t*) statement;
3328 static statement_t *parse_for(void)
3332 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3333 statement->statement.type = STATEMENT_FOR;
3334 statement->statement.source_position = token.source_position;
3338 int top = environment_top();
3339 context_t *last_context = context;
3340 set_context(&statement->context);
3342 if(token.type != ';') {
3343 if(is_declaration_specifier(&token, false)) {
3344 parse_declaration();
3346 statement->initialisation = parse_expression();
3353 if(token.type != ';') {
3354 statement->condition = parse_expression();
3357 if(token.type != ')') {
3358 statement->step = parse_expression();
3361 statement->body = parse_statement();
3363 assert(context == &statement->context);
3364 set_context(last_context);
3365 environment_pop_to(top);
3367 return (statement_t*) statement;
3370 static statement_t *parse_goto(void)
3374 if(token.type != T_IDENTIFIER) {
3375 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3379 symbol_t *symbol = token.v.symbol;
3382 declaration_t *label = get_label(symbol);
3384 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3386 statement->statement.type = STATEMENT_GOTO;
3387 statement->statement.source_position = token.source_position;
3389 statement->label = label;
3393 return (statement_t*) statement;
3396 static statement_t *parse_continue(void)
3401 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3402 statement->type = STATEMENT_CONTINUE;
3403 statement->source_position = token.source_position;
3408 static statement_t *parse_break(void)
3413 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3414 statement->type = STATEMENT_BREAK;
3415 statement->source_position = token.source_position;
3420 static statement_t *parse_return(void)
3424 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3426 statement->statement.type = STATEMENT_RETURN;
3427 statement->statement.source_position = token.source_position;
3429 assert(current_function->type->type == TYPE_FUNCTION);
3430 function_type_t *function_type = (function_type_t*) current_function->type;
3431 type_t *return_type = function_type->result_type;
3433 expression_t *return_value;
3434 if(token.type != ';') {
3435 return_value = parse_expression();
3437 if(return_type == type_void && return_value->datatype != type_void) {
3438 parse_warning("'return' with a value, in function returning void");
3439 return_value = NULL;
3441 if(return_type != NULL) {
3442 semantic_assign(return_type, &return_value, "'return'");
3446 return_value = NULL;
3447 if(return_type != type_void) {
3448 parse_warning("'return' without value, in function returning "
3452 statement->return_value = return_value;
3456 return (statement_t*) statement;
3459 static statement_t *parse_declaration_statement(void)
3461 declaration_t *before = last_declaration;
3463 declaration_statement_t *statement
3464 = allocate_ast_zero(sizeof(statement[0]));
3465 statement->statement.type = STATEMENT_DECLARATION;
3466 statement->statement.source_position = token.source_position;
3468 declaration_specifiers_t specifiers;
3469 memset(&specifiers, 0, sizeof(specifiers));
3470 parse_declaration_specifiers(&specifiers);
3472 if(token.type == ';') {
3475 parse_init_declarators(&specifiers);
3478 if(before == NULL) {
3479 statement->declarations_begin = context->declarations;
3481 statement->declarations_begin = before->next;
3483 statement->declarations_end = last_declaration;
3485 return (statement_t*) statement;
3488 static statement_t *parse_expression_statement(void)
3490 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3491 statement->statement.type = STATEMENT_EXPRESSION;
3492 statement->statement.source_position = token.source_position;
3494 statement->expression = parse_expression();
3498 return (statement_t*) statement;
3501 static statement_t *parse_statement(void)
3503 statement_t *statement = NULL;
3505 /* declaration or statement */
3506 switch(token.type) {
3508 statement = parse_case_statement();
3512 statement = parse_default_statement();
3516 statement = parse_compound_statement();
3520 statement = parse_if();
3524 statement = parse_switch();
3528 statement = parse_while();
3532 statement = parse_do();
3536 statement = parse_for();
3540 statement = parse_goto();
3544 statement = parse_continue();
3548 statement = parse_break();
3552 statement = parse_return();
3561 if(look_ahead(1)->type == ':') {
3562 statement = parse_label_statement();
3566 if(is_typedef_symbol(token.v.symbol)) {
3567 statement = parse_declaration_statement();
3571 statement = parse_expression_statement();
3574 case T___extension__:
3575 /* this can be a prefix to a declaration or an expression statement */
3576 /* we simply eat it now and parse the rest with tail recursion */
3579 } while(token.type == T___extension__);
3580 statement = parse_statement();
3584 statement = parse_declaration_statement();
3588 statement = parse_expression_statement();
3592 assert(statement == NULL || statement->source_position.input_name != NULL);
3597 static statement_t *parse_compound_statement(void)
3599 compound_statement_t *compound_statement
3600 = allocate_ast_zero(sizeof(compound_statement[0]));
3601 compound_statement->statement.type = STATEMENT_COMPOUND;
3602 compound_statement->statement.source_position = token.source_position;
3606 int top = environment_top();
3607 context_t *last_context = context;
3608 set_context(&compound_statement->context);
3610 statement_t *last_statement = NULL;
3612 while(token.type != '}' && token.type != T_EOF) {
3613 statement_t *statement = parse_statement();
3614 if(statement == NULL)
3617 if(last_statement != NULL) {
3618 last_statement->next = statement;
3620 compound_statement->statements = statement;
3623 while(statement->next != NULL)
3624 statement = statement->next;
3626 last_statement = statement;
3629 if(token.type != '}') {
3630 parser_print_error_prefix_pos(
3631 compound_statement->statement.source_position);
3632 fprintf(stderr, "end of file while looking for closing '}'\n");
3636 assert(context == &compound_statement->context);
3637 set_context(last_context);
3638 environment_pop_to(top);
3640 return (statement_t*) compound_statement;
3643 static translation_unit_t *parse_translation_unit(void)
3645 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3647 assert(global_context == NULL);
3648 global_context = &unit->context;
3650 assert(context == NULL);
3651 set_context(&unit->context);
3653 while(token.type != T_EOF) {
3654 parse_declaration();
3657 assert(context == &unit->context);
3659 last_declaration = NULL;
3661 assert(global_context == &unit->context);
3662 global_context = NULL;
3667 translation_unit_t *parse(void)
3669 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3670 label_stack = NEW_ARR_F(stack_entry_t, 0);
3671 found_error = false;
3673 type_set_output(stderr);
3674 ast_set_output(stderr);
3676 lookahead_bufpos = 0;
3677 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3680 translation_unit_t *unit = parse_translation_unit();
3682 DEL_ARR_F(environment_stack);
3683 DEL_ARR_F(label_stack);
3691 void init_parser(void)
3693 init_expression_parsers();
3694 obstack_init(&temp_obst);
3696 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3697 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3698 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3699 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3700 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3701 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3702 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3703 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3704 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3705 type_string = make_pointer_type(type_const_char, 0);
3708 void exit_parser(void)
3710 obstack_free(&temp_obst, NULL);