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;
395 assert(declaration->source_position.input_name != NULL);
397 /* a declaration should be only pushed once */
398 assert(declaration->parent_context == NULL);
399 declaration->parent_context = parent_context;
401 declaration_t *previous_declaration = get_declaration(symbol, namespace);
402 assert(declaration != previous_declaration);
403 if(previous_declaration != NULL
404 && previous_declaration->parent_context == context) {
405 if(!is_compatible_declaration(declaration, previous_declaration)) {
406 parser_print_error_prefix_pos(declaration->source_position);
407 fprintf(stderr, "definition of symbol %s%s with type ",
408 get_namespace_prefix(namespace), symbol->string);
410 print_type(declaration->type);
412 parser_print_error_prefix_pos(
413 previous_declaration->source_position);
414 fprintf(stderr, "is incompatible with previous declaration "
416 print_type(previous_declaration->type);
419 return previous_declaration;
422 /* remember old declaration */
424 entry.symbol = symbol;
425 entry.old_declaration = symbol->declaration;
426 entry.namespace = namespace;
427 ARR_APP1(*stack_ptr, entry);
429 /* replace/add declaration into declaration list of the symbol */
430 if(symbol->declaration == NULL) {
431 symbol->declaration = declaration;
433 declaration_t *iter = symbol->declaration;
434 for( ; iter != NULL; iter = iter->symbol_next) {
435 declaration_t *symbol_next = iter->symbol_next;
436 if(symbol_next == NULL) {
437 iter->symbol_next = declaration;
438 assert(declaration->symbol_next == NULL);
441 if(symbol_next->namespace == namespace) {
442 iter->symbol_next = declaration;
443 declaration->symbol_next = symbol_next->symbol_next;
452 static declaration_t *environment_push(declaration_t *declaration)
454 return stack_push(&environment_stack, declaration, context);
457 static declaration_t *label_push(declaration_t *declaration)
459 return stack_push(&label_stack, declaration, ¤t_function->context);
463 * pops symbols from the environment stack until @p new_top is the top element
465 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
467 stack_entry_t *stack = *stack_ptr;
468 size_t top = ARR_LEN(stack);
471 assert(new_top <= top);
475 for(i = top; i > new_top; --i) {
476 stack_entry_t *entry = & stack[i - 1];
478 declaration_t *old_declaration = entry->old_declaration;
479 symbol_t *symbol = entry->symbol;
480 namespace_t namespace = entry->namespace;
482 /* replace/remove declaration */
483 declaration_t *declaration = symbol->declaration;
484 assert(declaration != NULL);
485 if(declaration->namespace == namespace) {
486 if(old_declaration == NULL) {
487 symbol->declaration = declaration->symbol_next;
489 symbol->declaration = old_declaration;
490 assert(old_declaration->symbol_next ==
491 declaration->symbol_next);
494 for(; declaration != NULL; declaration = declaration->symbol_next) {
495 declaration_t *symbol_next = declaration->symbol_next;
496 if(symbol_next->namespace == namespace) {
497 declaration->symbol_next = old_declaration;
498 assert(old_declaration->symbol_next
499 == symbol_next->symbol_next);
503 assert(declaration != NULL);
507 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
510 static void environment_pop_to(size_t new_top)
512 stack_pop_to(&environment_stack, new_top);
515 static void label_pop_to(size_t new_top)
517 stack_pop_to(&label_stack, new_top);
521 static int get_rank(const type_t *type)
523 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
524 * and esp. footnote 108). However we can't fold constants (yet), so we
525 * can't decide wether unsigned int is possible, while int always works.
526 * (unsigned int would be preferable when possible... for stuff like
527 * struct { enum { ... } bla : 4; } ) */
528 if(type->type == TYPE_ENUM)
529 return ATOMIC_TYPE_INT;
531 assert(type->type == TYPE_ATOMIC);
532 atomic_type_t *atomic_type = (atomic_type_t*) type;
533 atomic_type_type_t atype = atomic_type->atype;
537 static type_t *promote_integer(type_t *type)
539 if(get_rank(type) < ATOMIC_TYPE_INT)
545 static expression_t *create_cast_expression(expression_t *expression,
548 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
550 cast->expression.type = EXPR_UNARY;
551 cast->type = UNEXPR_CAST;
552 cast->value = expression;
553 cast->expression.datatype = dest_type;
555 return (expression_t*) cast;
558 static expression_t *create_implicit_cast(expression_t *expression,
561 assert(expression->datatype != NULL);
562 type_t *source_type = expression->datatype;
564 source_type = skip_typeref(source_type);
565 dest_type = skip_typeref(dest_type);
567 if(expression->datatype == dest_type)
570 if(dest_type->type == TYPE_ATOMIC) {
571 if(source_type->type != TYPE_ATOMIC)
572 panic("casting of non-atomic types not implemented yet");
574 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
575 type_error_incompatible("can't cast types",
576 expression->source_position,
577 source_type, dest_type);
581 return create_cast_expression(expression, dest_type);
583 if(dest_type->type == TYPE_POINTER) {
584 if(source_type->type == TYPE_POINTER) {
585 if(!pointers_compatible(source_type, dest_type)) {
586 type_error_incompatible("can't implicitely cast types",
587 expression->source_position,
588 source_type, dest_type);
590 return create_cast_expression(expression, dest_type);
595 panic("casting of non-atomic types not implemented yet");
598 static void semantic_assign(type_t *orig_type_left, expression_t **right,
601 type_t *orig_type_right = (*right)->datatype;
602 type_t *type_left = skip_typeref(orig_type_left);
603 type_t *type_right = skip_typeref(orig_type_right);
605 if(type_left == type_right) {
607 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
608 *right = create_implicit_cast(*right, type_left);
609 } else if(type_left->type == TYPE_POINTER
610 && type_right->type == TYPE_POINTER) {
613 /* TODO: improve error message */
614 parser_print_error_prefix();
615 fprintf(stderr, "incompatible types in %s\n", context);
616 parser_print_error_prefix();
617 print_type(type_left);
618 fputs(" <- ", stderr);
619 print_type(type_right);
625 static expression_t *parse_constant_expression(void)
627 /* start parsing at precedence 7 (conditional expression) */
628 return parse_sub_expression(7);
631 static expression_t *parse_assignment_expression(void)
633 /* start parsing at precedence 2 (assignment expression) */
634 return parse_sub_expression(2);
637 static void parse_compound_type_entries(void);
638 static declaration_t *parse_declarator(storage_class_t storage_class,
639 type_t *type, int may_be_abstract);
640 static declaration_t *record_declaration(declaration_t *declaration);
642 typedef struct declaration_specifiers_t declaration_specifiers_t;
643 struct declaration_specifiers_t {
644 storage_class_t storage_class;
648 static const char *parse_string_literals(void)
650 assert(token.type == T_STRING_LITERAL);
651 const char *result = token.v.string;
655 while(token.type == T_STRING_LITERAL) {
656 result = concat_strings(result, token.v.string);
663 static void parse_attributes(void)
667 case T___attribute__:
675 parse_error("EOF while parsing attribute");
693 if(token.type != T_STRING_LITERAL) {
694 parse_error_expected("while parsing assembler attribute",
699 parse_string_literals();
704 goto attributes_finished;
712 static designator_t *parse_designation(void)
714 if(token.type != '[' && token.type != '.')
717 designator_t *result = NULL;
718 designator_t *last = NULL;
721 designator_t *designator;
724 designator = allocate_ast_zero(sizeof(designator[0]));
726 designator->array_access = parse_constant_expression();
730 designator = allocate_ast_zero(sizeof(designator[0]));
732 if(token.type != T_IDENTIFIER) {
733 parse_error_expected("problem while parsing designator",
737 designator->symbol = token.v.symbol;
745 assert(designator != NULL);
747 last->next = designator;
755 static initializer_t *parse_initializer_list(type_t *type);
757 static initializer_t *parse_initializer(type_t *type)
759 designator_t *designator = parse_designation();
761 initializer_t *result;
762 if(token.type == '{') {
763 result = parse_initializer_list(type);
765 result = allocate_ast_zero(sizeof(result[0]));
766 result->type = INITIALIZER_VALUE;
767 result->v.value = parse_assignment_expression();
770 semantic_assign(type, &result->v.value, "initializer");
773 result->designator = designator;
778 static initializer_t *parse_initializer_list(type_t *type)
785 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
786 result->type = INITIALIZER_LIST;
788 initializer_t *last = NULL;
790 initializer_t *initializer = parse_initializer(NULL);
792 last->next = initializer;
794 result->v.list = initializer;
798 if(token.type == '}')
801 if(token.type != ',') {
802 parse_error_expected("problem while parsing initializer list",
809 if(token.type == '}')
818 static declaration_t *parse_compound_type_specifier(bool is_struct)
826 symbol_t *symbol = NULL;
827 declaration_t *declaration = NULL;
829 if(token.type == T_IDENTIFIER) {
830 symbol = token.v.symbol;
834 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
836 declaration = get_declaration(symbol, NAMESPACE_UNION);
838 } else if(token.type != '{') {
840 parse_error_expected("problem while parsing struct type specifier",
841 T_IDENTIFIER, '{', 0);
843 parse_error_expected("problem while parsing union type specifier",
844 T_IDENTIFIER, '{', 0);
850 if(declaration == NULL) {
851 declaration = allocate_type_zero(sizeof(declaration[0]));
854 declaration->namespace = NAMESPACE_STRUCT;
856 declaration->namespace = NAMESPACE_UNION;
858 declaration->source_position = token.source_position;
859 declaration->symbol = symbol;
862 if(token.type == '{') {
863 if(declaration->init.is_defined) {
864 assert(symbol != NULL);
865 parser_print_error_prefix();
866 fprintf(stderr, "multiple definition of %s %s\n",
867 is_struct ? "struct" : "union", symbol->string);
868 declaration->context.declarations = NULL;
870 record_declaration(declaration);
871 declaration->init.is_defined = true;
873 int top = environment_top();
874 context_t *last_context = context;
875 set_context(& declaration->context);
877 parse_compound_type_entries();
880 assert(context == & declaration->context);
881 set_context(last_context);
882 environment_pop_to(top);
888 static void parse_enum_entries(void)
892 if(token.type == '}') {
894 parse_error("empty enum not allowed");
899 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
901 if(token.type != T_IDENTIFIER) {
902 parse_error_expected("problem while parsing enum entry",
907 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
908 entry->symbol = token.v.symbol;
909 entry->source_position = token.source_position;
912 if(token.type == '=') {
914 entry->init.initializer = parse_initializer(type_int);
917 record_declaration(entry);
919 if(token.type != ',')
922 } while(token.type != '}');
927 static declaration_t *parse_enum_specifier(void)
931 declaration_t *declaration;
934 if(token.type == T_IDENTIFIER) {
935 symbol = token.v.symbol;
938 declaration = get_declaration(symbol, NAMESPACE_ENUM);
939 } else if(token.type != '{') {
940 parse_error_expected("problem while parsing enum type specifier",
941 T_IDENTIFIER, '{', 0);
948 if(declaration == NULL) {
949 declaration = allocate_type_zero(sizeof(declaration[0]));
951 declaration->namespace = NAMESPACE_ENUM;
952 declaration->source_position = token.source_position;
953 declaration->symbol = symbol;
956 if(token.type == '{') {
957 if(declaration->init.is_defined) {
958 parser_print_error_prefix();
959 fprintf(stderr, "multiple definitions of enum %s\n",
962 record_declaration(declaration);
963 declaration->init.is_defined = 1;
965 parse_enum_entries();
973 * if a symbol is a typedef to another type, return true
975 static bool is_typedef_symbol(symbol_t *symbol)
977 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
978 if(declaration == NULL
979 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
985 static type_t *parse_typeof(void)
993 expression_t *expression = NULL;
997 case T___extension__:
998 /* this can be a prefix to a typename or an expression */
999 /* we simply eat it now. */
1002 } while(token.type == T___extension__);
1006 if(is_typedef_symbol(token.v.symbol)) {
1007 type = parse_typename();
1009 expression = parse_expression();
1010 type = expression->datatype;
1015 type = parse_typename();
1019 expression = parse_expression();
1020 type = expression->datatype;
1026 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
1027 typeof->type.type = TYPE_TYPEOF;
1028 typeof->expression = expression;
1029 typeof->typeof_type = type;
1031 return (type_t*) typeof;
1035 SPECIFIER_SIGNED = 1 << 0,
1036 SPECIFIER_UNSIGNED = 1 << 1,
1037 SPECIFIER_LONG = 1 << 2,
1038 SPECIFIER_INT = 1 << 3,
1039 SPECIFIER_DOUBLE = 1 << 4,
1040 SPECIFIER_CHAR = 1 << 5,
1041 SPECIFIER_SHORT = 1 << 6,
1042 SPECIFIER_LONG_LONG = 1 << 7,
1043 SPECIFIER_FLOAT = 1 << 8,
1044 SPECIFIER_BOOL = 1 << 9,
1045 SPECIFIER_VOID = 1 << 10,
1046 #ifdef PROVIDE_COMPLEX
1047 SPECIFIER_COMPLEX = 1 << 11,
1049 #ifdef PROVIDE_IMAGINARY
1050 SPECIFIER_IMAGINARY = 1 << 12,
1054 static type_t *create_builtin_type(symbol_t *symbol)
1056 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1057 type->type.type = TYPE_BUILTIN;
1058 type->symbol = symbol;
1060 type->real_type = type_int;
1062 return (type_t*) type;
1065 static type_t *get_typedef_type(symbol_t *symbol)
1067 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1068 if(declaration == NULL
1069 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1072 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1073 typedef_type->type.type = TYPE_TYPEDEF;
1074 typedef_type->declaration = declaration;
1076 return (type_t*) typedef_type;
1079 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1081 type_t *type = NULL;
1082 unsigned type_qualifiers = 0;
1083 unsigned type_specifiers = 0;
1087 switch(token.type) {
1090 #define MATCH_STORAGE_CLASS(token, class) \
1092 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1093 parse_error("multiple storage classes in declaration " \
1096 specifiers->storage_class = class; \
1100 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1101 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1102 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1103 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1104 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1106 /* type qualifiers */
1107 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1109 type_qualifiers |= qualifier; \
1113 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1114 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1115 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1116 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1118 case T___extension__:
1123 /* type specifiers */
1124 #define MATCH_SPECIFIER(token, specifier, name) \
1127 if(type_specifiers & specifier) { \
1128 parse_error("multiple " name " type specifiers given"); \
1130 type_specifiers |= specifier; \
1134 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1135 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1136 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1137 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1138 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1139 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1140 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1141 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1142 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1143 #ifdef PROVIDE_COMPLEX
1144 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1146 #ifdef PROVIDE_IMAGINARY
1147 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1151 if(type_specifiers & SPECIFIER_LONG_LONG) {
1152 parse_error("multiple type specifiers given");
1153 } else if(type_specifiers & SPECIFIER_LONG) {
1154 type_specifiers |= SPECIFIER_LONG_LONG;
1156 type_specifiers |= SPECIFIER_LONG;
1160 /* TODO: if type != NULL for the following rules should issue
1163 compound_type_t *compound_type
1164 = allocate_type_zero(sizeof(compound_type[0]));
1165 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1166 compound_type->declaration = parse_compound_type_specifier(true);
1168 type = (type_t*) compound_type;
1172 compound_type_t *compound_type
1173 = allocate_type_zero(sizeof(compound_type[0]));
1174 compound_type->type.type = TYPE_COMPOUND_UNION;
1175 compound_type->declaration = parse_compound_type_specifier(false);
1177 type = (type_t*) compound_type;
1181 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1182 enum_type->type.type = TYPE_ENUM;
1183 enum_type->declaration = parse_enum_specifier();
1185 type = (type_t*) enum_type;
1189 type = parse_typeof();
1191 case T___builtin_va_list:
1192 type = create_builtin_type(token.v.symbol);
1196 case T___attribute__:
1201 case T_IDENTIFIER: {
1202 type_t *typedef_type = get_typedef_type(token.v.symbol);
1204 if(typedef_type == NULL)
1205 goto finish_specifiers;
1208 type = typedef_type;
1212 /* function specifier */
1214 goto finish_specifiers;
1221 atomic_type_type_t atomic_type;
1223 /* match valid basic types */
1224 switch(type_specifiers) {
1225 case SPECIFIER_VOID:
1226 atomic_type = ATOMIC_TYPE_VOID;
1228 case SPECIFIER_CHAR:
1229 atomic_type = ATOMIC_TYPE_CHAR;
1231 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1232 atomic_type = ATOMIC_TYPE_SCHAR;
1234 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1235 atomic_type = ATOMIC_TYPE_UCHAR;
1237 case SPECIFIER_SHORT:
1238 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1239 case SPECIFIER_SHORT | SPECIFIER_INT:
1240 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1241 atomic_type = ATOMIC_TYPE_SHORT;
1243 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1244 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1245 atomic_type = ATOMIC_TYPE_USHORT;
1248 case SPECIFIER_SIGNED:
1249 case SPECIFIER_SIGNED | SPECIFIER_INT:
1250 atomic_type = ATOMIC_TYPE_INT;
1252 case SPECIFIER_UNSIGNED:
1253 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1254 atomic_type = ATOMIC_TYPE_UINT;
1256 case SPECIFIER_LONG:
1257 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1258 case SPECIFIER_LONG | SPECIFIER_INT:
1259 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1260 atomic_type = ATOMIC_TYPE_LONG;
1262 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1263 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1264 atomic_type = ATOMIC_TYPE_ULONG;
1266 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1267 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1268 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1269 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1271 atomic_type = ATOMIC_TYPE_LONGLONG;
1273 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1274 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1276 atomic_type = ATOMIC_TYPE_ULONGLONG;
1278 case SPECIFIER_FLOAT:
1279 atomic_type = ATOMIC_TYPE_FLOAT;
1281 case SPECIFIER_DOUBLE:
1282 atomic_type = ATOMIC_TYPE_DOUBLE;
1284 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1285 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1287 case SPECIFIER_BOOL:
1288 atomic_type = ATOMIC_TYPE_BOOL;
1290 #ifdef PROVIDE_COMPLEX
1291 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1292 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1294 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1295 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1297 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1298 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1301 #ifdef PROVIDE_IMAGINARY
1302 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1303 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1305 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1306 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1308 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1309 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1313 /* invalid specifier combination, give an error message */
1314 if(type_specifiers == 0) {
1316 parse_warning("no type specifiers in declaration (using int)");
1317 atomic_type = ATOMIC_TYPE_INT;
1320 parse_error("no type specifiers given in declaration");
1322 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1323 (type_specifiers & SPECIFIER_UNSIGNED)) {
1324 parse_error("signed and unsigned specifiers gives");
1325 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1326 parse_error("only integer types can be signed or unsigned");
1328 parse_error("multiple datatypes in declaration");
1330 atomic_type = ATOMIC_TYPE_INVALID;
1333 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1334 atype->type.type = TYPE_ATOMIC;
1335 atype->atype = atomic_type;
1338 type = (type_t*) atype;
1340 if(type_specifiers != 0) {
1341 parse_error("multiple datatypes in declaration");
1345 type->qualifiers = type_qualifiers;
1347 type_t *result = typehash_insert(type);
1348 if(newtype && result != (type_t*) type) {
1352 specifiers->type = result;
1355 static type_qualifier_t parse_type_qualifiers(void)
1357 type_qualifier_t type_qualifiers = 0;
1360 switch(token.type) {
1361 /* type qualifiers */
1362 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1363 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1364 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1365 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1368 return type_qualifiers;
1373 static void parse_identifier_list(void)
1376 if(token.type != T_IDENTIFIER) {
1377 parse_error_expected("problem while parsing parameter identifier "
1378 "list", T_IDENTIFIER, 0);
1382 if(token.type != ',')
1388 static declaration_t *parse_parameter(void)
1390 declaration_specifiers_t specifiers;
1391 memset(&specifiers, 0, sizeof(specifiers));
1393 parse_declaration_specifiers(&specifiers);
1395 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1396 specifiers.type, 1);
1398 /* TODO check declaration constraints for parameters */
1399 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1400 parse_error("typedef not allowed in parameter list");
1406 static declaration_t *parse_parameters(function_type_t *type)
1408 if(token.type == T_IDENTIFIER) {
1409 symbol_t *symbol = token.v.symbol;
1410 if(!is_typedef_symbol(symbol)) {
1412 parse_identifier_list();
1417 if(token.type == ')') {
1418 type->unspecified_parameters = 1;
1421 if(token.type == T_void && look_ahead(1)->type == ')') {
1426 declaration_t *declarations = NULL;
1427 declaration_t *declaration;
1428 declaration_t *last_declaration = NULL;
1429 function_parameter_t *parameter;
1430 function_parameter_t *last_parameter = NULL;
1433 switch(token.type) {
1437 return declarations;
1440 case T___extension__:
1442 declaration = parse_parameter();
1444 parameter = allocate_type_zero(sizeof(parameter[0]));
1445 parameter->type = declaration->type;
1447 if(last_parameter != NULL) {
1448 last_declaration->next = declaration;
1449 last_parameter->next = parameter;
1451 type->parameters = parameter;
1452 declarations = declaration;
1454 last_parameter = parameter;
1455 last_declaration = declaration;
1459 return declarations;
1461 if(token.type != ',')
1462 return declarations;
1471 } construct_type_type_t;
1473 typedef struct construct_type_t construct_type_t;
1474 struct construct_type_t {
1475 construct_type_type_t type;
1476 construct_type_t *next;
1479 typedef struct parsed_pointer_t parsed_pointer_t;
1480 struct parsed_pointer_t {
1481 construct_type_t construct_type;
1482 type_qualifier_t type_qualifiers;
1485 typedef struct construct_function_type_t construct_function_type_t;
1486 struct construct_function_type_t {
1487 construct_type_t construct_type;
1488 function_type_t *function_type;
1491 typedef struct parsed_array_t parsed_array_t;
1492 struct parsed_array_t {
1493 construct_type_t construct_type;
1494 type_qualifier_t type_qualifiers;
1500 typedef struct construct_base_type_t construct_base_type_t;
1501 struct construct_base_type_t {
1502 construct_type_t construct_type;
1506 static construct_type_t *parse_pointer_declarator(void)
1510 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1511 memset(pointer, 0, sizeof(pointer[0]));
1512 pointer->type_qualifiers = parse_type_qualifiers();
1514 return (construct_type_t*) pointer;
1517 static construct_type_t *parse_array_declarator(void)
1521 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1522 memset(array, 0, sizeof(array[0]));
1524 if(token.type == T_static) {
1525 array->is_static = true;
1529 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1530 if(type_qualifiers != 0) {
1531 if(token.type == T_static) {
1532 array->is_static = true;
1536 array->type_qualifiers = type_qualifiers;
1538 if(token.type == '*' && look_ahead(1)->type == ']') {
1539 array->is_variable = true;
1541 } else if(token.type != ']') {
1542 array->size = parse_assignment_expression();
1547 return (construct_type_t*) array;
1550 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1554 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1555 type->type.type = TYPE_FUNCTION;
1557 declaration_t *parameters = parse_parameters(type);
1558 if(declaration != NULL) {
1559 declaration->context.declarations = parameters;
1562 construct_function_type_t *construct_function_type =
1563 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1564 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1565 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1566 construct_function_type->function_type = type;
1570 return (construct_type_t*) construct_function_type;
1573 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1574 int may_be_abstract)
1576 construct_type_t *result = NULL;
1577 construct_type_t *last = NULL;
1579 while(token.type == '*') {
1580 construct_type_t *type = parse_pointer_declarator();
1589 /* TODO: find out if this is correct */
1592 construct_type_t *inner_types = NULL;
1594 switch(token.type) {
1596 if(declaration == NULL) {
1597 parse_error("no identifier expected in typename");
1599 declaration->symbol = token.v.symbol;
1600 declaration->source_position = token.source_position;
1606 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1612 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1617 construct_type_t *type;
1618 switch(token.type) {
1620 type = parse_function_declarator(declaration);
1623 type = parse_array_declarator();
1626 goto declarator_finished;
1637 declarator_finished:
1640 if(inner_types != NULL) {
1642 last->next = inner_types;
1644 result = inner_types;
1652 static type_t *construct_declarator_type(construct_type_t *construct_list,
1655 construct_type_t *iter = construct_list;
1656 for( ; iter != NULL; iter = iter->next) {
1657 parsed_pointer_t *parsed_pointer;
1658 parsed_array_t *parsed_array;
1659 construct_function_type_t *construct_function_type;
1660 function_type_t *function_type;
1661 pointer_type_t *pointer_type;
1662 array_type_t *array_type;
1664 switch(iter->type) {
1665 case CONSTRUCT_FUNCTION:
1666 construct_function_type = (construct_function_type_t*) iter;
1667 function_type = construct_function_type->function_type;
1669 function_type->result_type = type;
1670 type = (type_t*) function_type;
1673 case CONSTRUCT_POINTER:
1674 parsed_pointer = (parsed_pointer_t*) iter;
1675 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1677 pointer_type->type.type = TYPE_POINTER;
1678 pointer_type->points_to = type;
1679 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1680 type = (type_t*) pointer_type;
1683 case CONSTRUCT_ARRAY:
1684 parsed_array = (parsed_array_t*) iter;
1685 array_type = allocate_type_zero(sizeof(array_type[0]));
1687 array_type->type.type = TYPE_ARRAY;
1688 array_type->element_type = type;
1689 array_type->type.qualifiers = parsed_array->type_qualifiers;
1690 array_type->is_static = parsed_array->is_static;
1691 array_type->is_variable = parsed_array->is_variable;
1692 array_type->size = parsed_array->size;
1693 type = (type_t*) array_type;
1697 type_t *hashed_type = typehash_insert((type_t*) type);
1698 if(hashed_type != type) {
1707 static declaration_t *parse_declarator(storage_class_t storage_class,
1708 type_t *type, int may_be_abstract)
1710 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1711 declaration->storage_class = storage_class;
1713 construct_type_t *construct_type
1714 = parse_inner_declarator(declaration, may_be_abstract);
1715 declaration->type = construct_declarator_type(construct_type, type);
1717 if(construct_type != NULL) {
1718 obstack_free(&temp_obst, construct_type);
1724 static type_t *parse_abstract_declarator(type_t *base_type)
1726 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1728 type_t *result = construct_declarator_type(construct_type, base_type);
1729 if(construct_type != NULL) {
1730 obstack_free(&temp_obst, construct_type);
1736 static declaration_t *record_declaration(declaration_t *declaration)
1738 assert(context != NULL);
1740 symbol_t *symbol = declaration->symbol;
1741 if(symbol != NULL) {
1742 declaration_t *alias = environment_push(declaration);
1743 if(alias != declaration)
1746 declaration->parent_context = context;
1749 if(last_declaration != NULL) {
1750 last_declaration->next = declaration;
1752 context->declarations = declaration;
1754 last_declaration = declaration;
1759 static void parser_error_multiple_definition(declaration_t *previous,
1760 declaration_t *declaration)
1762 parser_print_error_prefix_pos(declaration->source_position);
1763 fprintf(stderr, "multiple definition of symbol '%s'\n",
1764 declaration->symbol->string);
1765 parser_print_error_prefix_pos(previous->source_position);
1766 fprintf(stderr, "this is the location of the previous "
1771 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1774 declaration_t *ndeclaration
1775 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1777 declaration_t *declaration = record_declaration(ndeclaration);
1778 if(token.type == '=') {
1781 /* TODO: check that this is an allowed type (no function type) */
1783 if(declaration->init.initializer != NULL) {
1784 parser_error_multiple_definition(declaration, ndeclaration);
1787 ndeclaration->init.initializer = parse_initializer(declaration->type);
1788 } else if(token.type == '{') {
1789 if(declaration->type->type != TYPE_FUNCTION) {
1790 parser_print_error_prefix();
1791 fprintf(stderr, "Declarator ");
1792 print_type_ext(declaration->type, declaration->symbol, NULL);
1793 fprintf(stderr, " has a body but is not a function type.\n");
1798 if(declaration->init.statement != NULL) {
1799 parser_error_multiple_definition(declaration, ndeclaration);
1801 if(ndeclaration != declaration) {
1802 memcpy(&declaration->context, &ndeclaration->context,
1803 sizeof(declaration->context));
1806 int top = environment_top();
1807 context_t *last_context = context;
1808 set_context(&declaration->context);
1810 /* push function parameters */
1811 declaration_t *parameter = declaration->context.declarations;
1812 for( ; parameter != NULL; parameter = parameter->next) {
1813 environment_push(parameter);
1816 int label_stack_top = label_top();
1817 declaration_t *old_current_function = current_function;
1818 current_function = declaration;
1820 statement_t *statement = parse_compound_statement();
1822 assert(current_function == declaration);
1823 current_function = old_current_function;
1824 label_pop_to(label_stack_top);
1826 assert(context == &declaration->context);
1827 set_context(last_context);
1828 environment_pop_to(top);
1830 declaration->init.statement = statement;
1834 if(token.type != ',')
1841 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1844 if(token.type == ':') {
1846 parse_constant_expression();
1847 /* TODO (bitfields) */
1849 declaration_t *declaration
1850 = parse_declarator(specifiers->storage_class,
1851 specifiers->type, 1);
1853 /* TODO: check constraints for struct declarations */
1854 /* TODO: check for doubled fields */
1855 record_declaration(declaration);
1857 if(token.type == ':') {
1859 parse_constant_expression();
1860 /* TODO (bitfields) */
1864 if(token.type != ',')
1871 static void parse_compound_type_entries(void)
1875 while(token.type != '}' && token.type != T_EOF) {
1876 declaration_specifiers_t specifiers;
1877 memset(&specifiers, 0, sizeof(specifiers));
1878 parse_declaration_specifiers(&specifiers);
1880 parse_struct_declarators(&specifiers);
1882 if(token.type == T_EOF) {
1883 parse_error("unexpected error while parsing struct");
1888 static void parse_declaration(void)
1890 source_position_t source_position = token.source_position;
1892 declaration_specifiers_t specifiers;
1893 memset(&specifiers, 0, sizeof(specifiers));
1894 parse_declaration_specifiers(&specifiers);
1896 if(token.type == ';') {
1899 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1901 declaration->type = specifiers.type;
1902 declaration->storage_class = specifiers.storage_class;
1903 declaration->source_position = source_position;
1904 record_declaration(declaration);
1907 parse_init_declarators(&specifiers);
1910 static type_t *parse_typename(void)
1912 declaration_specifiers_t specifiers;
1913 memset(&specifiers, 0, sizeof(specifiers));
1914 parse_declaration_specifiers(&specifiers);
1915 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1916 /* TODO: improve error message, user does probably not know what a
1917 * storage class is...
1919 parse_error("typename may not have a storage class");
1922 type_t *result = parse_abstract_declarator(specifiers.type);
1930 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1931 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1932 expression_t *left);
1934 typedef struct expression_parser_function_t expression_parser_function_t;
1935 struct expression_parser_function_t {
1936 unsigned precedence;
1937 parse_expression_function parser;
1938 unsigned infix_precedence;
1939 parse_expression_infix_function infix_parser;
1942 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1944 static expression_t *expected_expression_error(void)
1946 parser_print_error_prefix();
1947 fprintf(stderr, "expected expression, got token ");
1948 print_token(stderr, & token);
1949 fprintf(stderr, "\n");
1951 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1952 expression->type = EXPR_INVALID;
1958 static expression_t *parse_string_const(void)
1960 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1962 cnst->expression.type = EXPR_STRING_LITERAL;
1963 cnst->expression.datatype = type_string;
1964 cnst->value = parse_string_literals();
1966 return (expression_t*) cnst;
1969 static expression_t *parse_int_const(void)
1971 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1973 cnst->expression.type = EXPR_CONST;
1974 cnst->expression.datatype = type_int;
1975 cnst->v.int_value = token.v.intvalue;
1979 return (expression_t*) cnst;
1982 static expression_t *parse_float_const(void)
1984 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1986 cnst->expression.type = EXPR_CONST;
1987 cnst->expression.datatype = type_double;
1988 cnst->v.float_value = token.v.floatvalue;
1992 return (expression_t*) cnst;
1995 static declaration_t *create_implicit_function(symbol_t *symbol,
1996 const source_position_t source_position)
1998 function_type_t *function_type
1999 = allocate_type_zero(sizeof(function_type[0]));
2001 function_type->type.type = TYPE_FUNCTION;
2002 function_type->result_type = type_int;
2003 function_type->unspecified_parameters = true;
2005 type_t *type = typehash_insert((type_t*) function_type);
2006 if(type != (type_t*) function_type) {
2007 free_type(function_type);
2010 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2012 declaration->storage_class = STORAGE_CLASS_EXTERN;
2013 declaration->type = type;
2014 declaration->symbol = symbol;
2015 declaration->source_position = source_position;
2017 /* prepend the implicit definition to the global context
2018 * this is safe since the symbol wasn't declared as anything else yet
2020 assert(symbol->declaration == NULL);
2022 context_t *last_context = context;
2023 context = global_context;
2025 environment_push(declaration);
2026 declaration->next = context->declarations;
2027 context->declarations = declaration;
2029 context = last_context;
2034 static expression_t *parse_reference(void)
2036 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2038 ref->expression.type = EXPR_REFERENCE;
2039 ref->symbol = token.v.symbol;
2041 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2043 source_position_t source_position = token.source_position;
2046 if(declaration == NULL) {
2048 /* an implicitely defined function */
2049 if(token.type == '(') {
2050 parser_print_prefix_pos(token.source_position);
2051 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2052 ref->symbol->string);
2054 declaration = create_implicit_function(ref->symbol,
2059 parser_print_error_prefix();
2060 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2061 return (expression_t*) ref;
2065 ref->declaration = declaration;
2066 ref->expression.datatype = declaration->type;
2068 return (expression_t*) ref;
2071 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2075 /* TODO check if explicit cast is allowed and issue warnings/errors */
2078 static expression_t *parse_cast(void)
2080 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2082 cast->expression.type = EXPR_UNARY;
2083 cast->type = UNEXPR_CAST;
2084 cast->expression.source_position = token.source_position;
2086 type_t *type = parse_typename();
2089 expression_t *value = parse_sub_expression(20);
2091 check_cast_allowed(value, type);
2093 cast->expression.datatype = type;
2094 cast->value = value;
2096 return (expression_t*) cast;
2099 static expression_t *parse_statement_expression(void)
2101 statement_expression_t *expression
2102 = allocate_ast_zero(sizeof(expression[0]));
2103 expression->expression.type = EXPR_STATEMENT;
2104 expression->statement = parse_compound_statement();
2106 /* find last statement and use it's type */
2107 const statement_t *last_statement = NULL;
2108 const statement_t *statement = expression->statement;
2109 for( ; statement != NULL; statement = statement->next) {
2110 last_statement = statement;
2113 if(last_statement->type == STATEMENT_EXPRESSION) {
2114 const expression_statement_t *expression_statement =
2115 (const expression_statement_t*) last_statement;
2116 expression->expression.datatype
2117 = expression_statement->expression->datatype;
2119 expression->expression.datatype = type_void;
2124 return (expression_t*) expression;
2127 static expression_t *parse_brace_expression(void)
2131 switch(token.type) {
2133 /* gcc extension: a stement expression */
2134 return parse_statement_expression();
2138 return parse_cast();
2140 if(is_typedef_symbol(token.v.symbol)) {
2141 return parse_cast();
2145 expression_t *result = parse_expression();
2151 static expression_t *parse_function_keyword(void)
2153 eat(T___FUNCTION__);
2156 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2157 expression->expression.type = EXPR_FUNCTION;
2158 expression->expression.datatype = type_string;
2159 expression->value = "TODO: FUNCTION";
2161 return (expression_t*) expression;
2164 static expression_t *parse_pretty_function_keyword(void)
2166 eat(T___PRETTY_FUNCTION__);
2169 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2170 expression->expression.type = EXPR_PRETTY_FUNCTION;
2171 expression->expression.datatype = type_string;
2172 expression->value = "TODO: PRETTY FUNCTION";
2174 return (expression_t*) expression;
2177 static designator_t *parse_designator(void)
2179 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2181 if(token.type != T_IDENTIFIER) {
2182 parse_error_expected("problem while parsing member designator",
2187 result->symbol = token.v.symbol;
2190 designator_t *last_designator = result;
2192 if(token.type == '.') {
2194 if(token.type != T_IDENTIFIER) {
2195 parse_error_expected("problem while parsing member designator",
2200 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2201 designator->symbol = token.v.symbol;
2204 last_designator->next = designator;
2205 last_designator = designator;
2208 if(token.type == '[') {
2210 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2211 designator->array_access = parse_expression();
2212 if(designator->array_access == NULL) {
2218 last_designator->next = designator;
2219 last_designator = designator;
2228 static expression_t *parse_offsetof(void)
2230 eat(T___builtin_offsetof);
2232 offsetof_expression_t *expression
2233 = allocate_ast_zero(sizeof(expression[0]));
2234 expression->expression.type = EXPR_OFFSETOF;
2235 expression->expression.datatype = type_size_t;
2238 expression->type = parse_typename();
2240 expression->designator = parse_designator();
2243 return (expression_t*) expression;
2246 static expression_t *parse_va_arg(void)
2248 eat(T___builtin_va_arg);
2250 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2251 expression->expression.type = EXPR_VA_ARG;
2254 expression->arg = parse_assignment_expression();
2256 expression->expression.datatype = parse_typename();
2259 return (expression_t*) expression;
2262 static expression_t *parse_builtin_symbol(void)
2264 builtin_symbol_expression_t *expression
2265 = allocate_ast_zero(sizeof(expression[0]));
2266 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2268 /* TODO: set datatype */
2270 expression->symbol = token.v.symbol;
2274 return (expression_t*) expression;
2277 static expression_t *parse_primary_expression(void)
2279 switch(token.type) {
2281 return parse_int_const();
2282 case T_FLOATINGPOINT:
2283 return parse_float_const();
2284 case T_STRING_LITERAL:
2285 return parse_string_const();
2287 return parse_reference();
2288 case T___FUNCTION__:
2289 return parse_function_keyword();
2290 case T___PRETTY_FUNCTION__:
2291 return parse_pretty_function_keyword();
2292 case T___builtin_offsetof:
2293 return parse_offsetof();
2294 case T___builtin_va_arg:
2295 return parse_va_arg();
2296 case T___builtin_expect:
2297 case T___builtin_va_start:
2298 case T___builtin_va_end:
2299 return parse_builtin_symbol();
2302 return parse_brace_expression();
2305 parser_print_error_prefix();
2306 fprintf(stderr, "unexpected token ");
2307 print_token(stderr, &token);
2308 fprintf(stderr, "\n");
2311 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2312 expression->type = EXPR_INVALID;
2313 expression->datatype = type_void;
2318 static expression_t *parse_array_expression(unsigned precedence,
2319 expression_t *array_ref)
2325 array_access_expression_t *array_access
2326 = allocate_ast_zero(sizeof(array_access[0]));
2328 array_access->expression.type = EXPR_ARRAY_ACCESS;
2329 array_access->array_ref = array_ref;
2330 array_access->index = parse_expression();
2332 type_t *array_type = array_ref->datatype;
2333 if(array_type != NULL) {
2334 if(array_type->type == TYPE_POINTER) {
2335 pointer_type_t *pointer = (pointer_type_t*) array_type;
2336 array_access->expression.datatype = pointer->points_to;
2338 parser_print_error_prefix();
2339 fprintf(stderr, "array access on object with non-pointer type ");
2340 print_type(array_type);
2341 fprintf(stderr, "\n");
2345 if(token.type != ']') {
2346 parse_error_expected("Problem while parsing array access", ']', 0);
2347 return (expression_t*) array_access;
2351 return (expression_t*) array_access;
2354 static bool is_declaration_specifier(const token_t *token,
2355 bool only_type_specifiers)
2357 switch(token->type) {
2361 return is_typedef_symbol(token->v.symbol);
2364 if(only_type_specifiers)
2373 static expression_t *parse_sizeof(unsigned precedence)
2377 sizeof_expression_t *sizeof_expression
2378 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2379 sizeof_expression->expression.type = EXPR_SIZEOF;
2380 sizeof_expression->expression.datatype = type_size_t;
2382 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2384 sizeof_expression->type = parse_typename();
2387 expression_t *expression = parse_sub_expression(precedence);
2388 sizeof_expression->type = expression->datatype;
2389 sizeof_expression->size_expression = expression;
2392 return (expression_t*) sizeof_expression;
2395 static expression_t *parse_select_expression(unsigned precedence,
2396 expression_t *compound)
2400 assert(token.type == '.' || token.type == T_MINUSGREATER);
2403 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2405 select->expression.type = EXPR_SELECT;
2406 select->compound = compound;
2408 /* TODO: datatype */
2410 if(token.type != T_IDENTIFIER) {
2411 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2412 return (expression_t*) select;
2414 select->symbol = token.v.symbol;
2417 return (expression_t*) select;
2420 static expression_t *parse_call_expression(unsigned precedence,
2421 expression_t *expression)
2424 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2425 call->expression.type = EXPR_CALL;
2426 call->function = expression;
2428 function_type_t *function_type;
2429 type_t *type = expression->datatype;
2430 if(type->type != TYPE_FUNCTION) {
2431 /* TODO calling pointers to functions is ok */
2432 parser_print_error_prefix();
2433 fputs("called object '", stderr);
2434 print_expression(expression);
2435 fputs("' (type ", stderr);
2437 fputs("is not a function\n", stderr);
2439 function_type = NULL;
2440 call->expression.datatype = NULL;
2442 function_type = (function_type_t*) type;
2443 call->expression.datatype = function_type->result_type;
2446 /* parse arguments */
2449 if(token.type != ')') {
2450 call_argument_t *last_argument = NULL;
2453 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2455 argument->expression = parse_assignment_expression();
2456 if(last_argument == NULL) {
2457 call->arguments = argument;
2459 last_argument->next = argument;
2461 last_argument = argument;
2463 if(token.type != ',')
2470 if(function_type != NULL) {
2471 function_parameter_t *parameter = function_type->parameters;
2472 call_argument_t *argument = call->arguments;
2473 for( ; parameter != NULL && argument != NULL;
2474 parameter = parameter->next, argument = argument->next) {
2475 type_t *expected_type = parameter->type;
2476 /* TODO report context in error messages */
2477 argument->expression = create_implicit_cast(argument->expression,
2480 /* too few parameters */
2481 if(parameter != NULL) {
2482 parser_print_error_prefix();
2483 fprintf(stderr, "too few arguments to function '");
2484 print_expression(expression);
2485 fprintf(stderr, "'\n");
2486 } else if(argument != NULL) {
2487 /* too many parameters */
2488 if(!function_type->variadic
2489 && !function_type->unspecified_parameters) {
2490 parser_print_error_prefix();
2491 fprintf(stderr, "too many arguments to function '");
2492 print_expression(expression);
2493 fprintf(stderr, "'\n");
2495 /* do default promotion */
2496 for( ; argument != NULL; argument = argument->next) {
2497 type_t *type = argument->expression->datatype;
2499 if(is_type_integer(type)) {
2500 type = promote_integer(type);
2501 } else if(type == type_float) {
2504 argument->expression
2505 = create_implicit_cast(argument->expression, type);
2511 return (expression_t*) call;
2514 static type_t *get_type_after_conversion(const type_t *type1,
2515 const type_t *type2)
2519 return (type_t*) type1;
2522 static expression_t *parse_conditional_expression(unsigned precedence,
2523 expression_t *expression)
2527 conditional_expression_t *conditional
2528 = allocate_ast_zero(sizeof(conditional[0]));
2529 conditional->expression.type = EXPR_CONDITIONAL;
2530 conditional->condition = expression;
2533 type_t *condition_type = conditional->condition->datatype;
2534 if(condition_type != NULL) {
2535 if(!is_type_scalar(condition_type)) {
2536 type_error("expected a scalar type", expression->source_position,
2541 conditional->true_expression = parse_expression();
2543 conditional->false_expression = parse_sub_expression(precedence);
2545 type_t *true_type = conditional->true_expression->datatype;
2546 if(true_type == NULL)
2547 return (expression_t*) conditional;
2548 type_t *false_type = conditional->false_expression->datatype;
2549 if(false_type == NULL)
2550 return (expression_t*) conditional;
2553 if(true_type == false_type) {
2554 conditional->expression.datatype = true_type;
2555 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2556 type_t *result = get_type_after_conversion(true_type, false_type);
2557 /* TODO: create implicit convs if necessary */
2558 conditional->expression.datatype = result;
2559 } else if(true_type->type == TYPE_POINTER &&
2560 false_type->type == TYPE_POINTER &&
2561 true /* TODO compatible points_to types */) {
2563 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2564 || (is_null_ptr_const(false_type) &&
2565 true_type->type == TYPE_POINTER) TODO*/ false) {
2567 } else if(/* 1 is pointer to object type, other is void* */ false) {
2570 type_error_incompatible("problem while parsing conditional",
2571 expression->source_position, true_type,
2575 return (expression_t*) conditional;
2578 static expression_t *parse_extension(unsigned precedence)
2580 eat(T___extension__);
2582 /* TODO enable extensions */
2584 return parse_sub_expression(precedence);
2587 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2590 return expression->datatype;
2593 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2595 type_t *expression_type = expression->datatype;
2597 if(expression_type->type == TYPE_POINTER) {
2598 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2599 return pointer_type->points_to;
2601 panic("deref TODO...");
2605 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2607 type_t *type = expression->datatype;
2608 return make_pointer_type(type, 0);
2611 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2612 static expression_t *parse_##unexpression_type(unsigned precedence) \
2616 unary_expression_t *unary_expression \
2617 = allocate_ast_zero(sizeof(unary_expression[0])); \
2618 unary_expression->expression.type = EXPR_UNARY; \
2619 unary_expression->type = unexpression_type; \
2620 unary_expression->value = parse_sub_expression(precedence); \
2621 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2623 return (expression_t*) unary_expression; \
2626 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2627 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2628 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2629 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2630 get_unexpr_dereference_type)
2631 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2632 get_unexpr_take_addr_type)
2633 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2634 get_unexpr_arithmetic_type)
2635 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2636 get_unexpr_arithmetic_type)
2637 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2638 get_unexpr_arithmetic_type)
2640 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2642 static expression_t *parse_##unexpression_type(unsigned precedence, \
2643 expression_t *left) \
2645 (void) precedence; \
2648 unary_expression_t *unary_expression \
2649 = allocate_ast_zero(sizeof(unary_expression[0])); \
2650 unary_expression->expression.type = EXPR_UNARY; \
2651 unary_expression->type = unexpression_type; \
2652 unary_expression->value = left; \
2653 unary_expression->expression.datatype = tfunc(left); \
2655 return (expression_t*) unary_expression; \
2658 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2659 get_unexpr_arithmetic_type)
2660 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2661 get_unexpr_arithmetic_type)
2663 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2665 /* TODO: handle complex + imaginary types */
2667 /* § 6.3.1.8 Usual arithmetic conversions */
2668 if(type_left == type_long_double || type_right == type_long_double) {
2669 return type_long_double;
2670 } else if(type_left == type_double || type_right == type_double) {
2672 } else if(type_left == type_float || type_right == type_float) {
2676 type_right = promote_integer(type_right);
2677 type_left = promote_integer(type_left);
2679 if(type_left == type_right)
2682 bool signed_left = is_type_signed(type_left);
2683 bool signed_right = is_type_signed(type_right);
2684 if(get_rank(type_left) < get_rank(type_right)) {
2685 if(signed_left == signed_right || !signed_right) {
2691 if(signed_left == signed_right || !signed_left) {
2699 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2701 expression_t *left = expression->left;
2702 expression_t *right = expression->right;
2703 type_t *type_left = skip_typeref(left->datatype);
2704 type_t *type_right = skip_typeref(right->datatype);
2706 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2707 /* TODO: improve error message */
2708 parser_print_error_prefix();
2709 fprintf(stderr, "operation needs arithmetic types\n");
2713 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2714 expression->left = create_implicit_cast(left, arithmetic_type);
2715 expression->right = create_implicit_cast(right, arithmetic_type);
2716 expression->expression.datatype = arithmetic_type;
2719 static void semantic_add(binary_expression_t *expression)
2721 expression_t *left = expression->left;
2722 expression_t *right = expression->right;
2723 type_t *orig_type_left = left->datatype;
2724 type_t *orig_type_right = right->datatype;
2725 type_t *type_left = skip_typeref(orig_type_left);
2726 type_t *type_right = skip_typeref(orig_type_right);
2729 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2730 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2731 expression->left = create_implicit_cast(left, arithmetic_type);
2732 expression->right = create_implicit_cast(right, arithmetic_type);
2733 expression->expression.datatype = arithmetic_type;
2735 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2736 expression->expression.datatype = type_left;
2737 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2738 expression->expression.datatype = type_right;
2740 parser_print_error_prefix();
2741 fprintf(stderr, "invalid operands to binary + (");
2742 print_type(orig_type_left);
2743 fprintf(stderr, ", ");
2744 print_type(orig_type_right);
2745 fprintf(stderr, ")\n");
2749 static void semantic_sub(binary_expression_t *expression)
2751 expression_t *left = expression->left;
2752 expression_t *right = expression->right;
2753 type_t *orig_type_left = left->datatype;
2754 type_t *orig_type_right = right->datatype;
2755 type_t *type_left = skip_typeref(orig_type_left);
2756 type_t *type_right = skip_typeref(orig_type_right);
2759 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2760 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2761 expression->left = create_implicit_cast(left, arithmetic_type);
2762 expression->right = create_implicit_cast(right, arithmetic_type);
2763 expression->expression.datatype = arithmetic_type;
2765 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2766 expression->expression.datatype = type_left;
2767 } else if(type_left->type == TYPE_POINTER &&
2768 type_right->type == TYPE_POINTER) {
2769 if(!pointers_compatible(type_left, type_right)) {
2770 parser_print_error_prefix();
2771 fprintf(stderr, "pointers to incompatible objects to binary - (");
2772 print_type(orig_type_left);
2773 fprintf(stderr, ", ");
2774 print_type(orig_type_right);
2775 fprintf(stderr, ")\n");
2777 expression->expression.datatype = type_ptrdiff_t;
2780 parser_print_error_prefix();
2781 fprintf(stderr, "invalid operands to binary - (");
2782 print_type(orig_type_left);
2783 fprintf(stderr, ", ");
2784 print_type(orig_type_right);
2785 fprintf(stderr, ")\n");
2789 static void semantic_comparison(binary_expression_t *expression)
2791 expression_t *left = expression->left;
2792 expression_t *right = expression->right;
2793 type_t *type_left = left->datatype;
2794 type_t *type_right = right->datatype;
2796 /* TODO non-arithmetic types */
2797 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2798 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2799 expression->left = create_implicit_cast(left, arithmetic_type);
2800 expression->right = create_implicit_cast(right, arithmetic_type);
2801 expression->expression.datatype = arithmetic_type;
2803 expression->expression.datatype = type_int;
2806 static void semantic_arithmetic_assign(binary_expression_t *expression)
2808 expression_t *left = expression->left;
2809 expression_t *right = expression->right;
2810 type_t *type_left = left->datatype;
2811 type_t *type_right = right->datatype;
2813 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2814 /* TODO: improve error message */
2815 parser_print_error_prefix();
2816 fprintf(stderr, "operation needs arithmetic types\n");
2820 /* combined instructions are tricky. We can't create an implicit cast on
2821 * the left side, because we need the uncasted form for the store.
2822 * The ast2firm pass has to know that left_type must be right_type
2823 * for the arithmeitc operation and create a cast by itself */
2824 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2825 expression->right = create_implicit_cast(right, arithmetic_type);
2826 expression->expression.datatype = type_left;
2829 static void semantic_logical_op(binary_expression_t *expression)
2832 expression->expression.datatype = type_int;
2835 static void semantic_binexpr_assign(binary_expression_t *expression)
2837 expression_t *left = expression->left;
2838 type_t *type_left = left->datatype;
2840 semantic_assign(type_left, &expression->right, "assignment");
2842 expression->expression.datatype = type_left;
2845 static void semantic_comma(binary_expression_t *expression)
2847 expression->expression.datatype = expression->right->datatype;
2850 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2851 static expression_t *parse_##binexpression_type(unsigned precedence, \
2852 expression_t *left) \
2856 expression_t *right = parse_sub_expression(precedence + lr); \
2858 binary_expression_t *binexpr \
2859 = allocate_ast_zero(sizeof(binexpr[0])); \
2860 binexpr->expression.type = EXPR_BINARY; \
2861 binexpr->type = binexpression_type; \
2862 binexpr->left = left; \
2863 binexpr->right = right; \
2866 return (expression_t*) binexpr; \
2869 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
2870 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
2871 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
2872 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
2873 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
2874 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
2875 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
2876 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
2877 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
2878 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
2879 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2880 semantic_comparison, 1)
2881 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
2882 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2883 semantic_comparison, 1)
2884 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
2885 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
2886 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
2887 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
2888 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
2889 /* TODO shift has a bit special semantic */
2890 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2891 semantic_binexpr_arithmetic, 1)
2892 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2893 semantic_binexpr_arithmetic, 1)
2894 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2895 semantic_arithmetic_assign, 0)
2896 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2897 semantic_arithmetic_assign, 0)
2898 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2899 semantic_arithmetic_assign, 0)
2900 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2901 semantic_arithmetic_assign, 0)
2902 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2903 semantic_arithmetic_assign, 0)
2904 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2905 semantic_arithmetic_assign, 0)
2906 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2907 semantic_arithmetic_assign, 0)
2908 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2909 semantic_arithmetic_assign, 0)
2910 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2911 semantic_arithmetic_assign, 0)
2912 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2913 semantic_arithmetic_assign, 0)
2915 static expression_t *parse_sub_expression(unsigned precedence)
2917 if(token.type < 0) {
2918 return expected_expression_error();
2921 expression_parser_function_t *parser
2922 = &expression_parsers[token.type];
2923 source_position_t source_position = token.source_position;
2926 if(parser->parser != NULL) {
2927 left = parser->parser(parser->precedence);
2929 left = parse_primary_expression();
2931 assert(left != NULL);
2932 left->source_position = source_position;
2935 if(token.type < 0) {
2936 return expected_expression_error();
2939 parser = &expression_parsers[token.type];
2940 if(parser->infix_parser == NULL)
2942 if(parser->infix_precedence < precedence)
2945 left = parser->infix_parser(parser->infix_precedence, left);
2947 assert(left != NULL);
2948 assert(left->type != EXPR_INVALID);
2949 left->source_position = source_position;
2955 static expression_t *parse_expression(void)
2957 return parse_sub_expression(1);
2962 static void register_expression_parser(parse_expression_function parser,
2963 int token_type, unsigned precedence)
2965 expression_parser_function_t *entry = &expression_parsers[token_type];
2967 if(entry->parser != NULL) {
2968 fprintf(stderr, "for token ");
2969 print_token_type(stderr, token_type);
2970 fprintf(stderr, "\n");
2971 panic("trying to register multiple expression parsers for a token");
2973 entry->parser = parser;
2974 entry->precedence = precedence;
2977 static void register_expression_infix_parser(
2978 parse_expression_infix_function parser, int token_type,
2979 unsigned precedence)
2981 expression_parser_function_t *entry = &expression_parsers[token_type];
2983 if(entry->infix_parser != NULL) {
2984 fprintf(stderr, "for token ");
2985 print_token_type(stderr, token_type);
2986 fprintf(stderr, "\n");
2987 panic("trying to register multiple infix expression parsers for a "
2990 entry->infix_parser = parser;
2991 entry->infix_precedence = precedence;
2994 static void init_expression_parsers(void)
2996 memset(&expression_parsers, 0, sizeof(expression_parsers));
2998 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2999 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
3000 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
3001 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
3002 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
3003 T_GREATERGREATER, 16);
3004 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
3005 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
3006 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
3007 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
3008 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
3009 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
3010 T_GREATEREQUAL, 14);
3011 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
3012 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
3013 T_EXCLAMATIONMARKEQUAL, 13);
3014 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
3015 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
3016 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
3017 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
3018 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
3019 register_expression_infix_parser(parse_conditional_expression, '?', 7);
3020 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
3021 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
3022 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
3023 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
3024 T_ASTERISKEQUAL, 2);
3025 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
3026 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
3028 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
3029 T_LESSLESSEQUAL, 2);
3030 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
3031 T_GREATERGREATEREQUAL, 2);
3032 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3034 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3036 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3039 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3041 register_expression_infix_parser(parse_array_expression, '[', 30);
3042 register_expression_infix_parser(parse_call_expression, '(', 30);
3043 register_expression_infix_parser(parse_select_expression, '.', 30);
3044 register_expression_infix_parser(parse_select_expression,
3045 T_MINUSGREATER, 30);
3046 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3048 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3051 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3052 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3053 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3054 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3055 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3056 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3057 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3058 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3059 register_expression_parser(parse_sizeof, T_sizeof, 25);
3060 register_expression_parser(parse_extension, T___extension__, 25);
3064 static statement_t *parse_case_statement(void)
3067 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3068 label->statement.type = STATEMENT_CASE_LABEL;
3069 label->statement.source_position = token.source_position;
3071 label->expression = parse_expression();
3074 label->statement.next = parse_statement();
3076 return (statement_t*) label;
3079 static statement_t *parse_default_statement(void)
3083 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3084 label->statement.type = STATEMENT_CASE_LABEL;
3085 label->statement.source_position = token.source_position;
3088 label->statement.next = parse_statement();
3090 return (statement_t*) label;
3093 static declaration_t *get_label(symbol_t *symbol)
3095 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
3096 assert(current_function != NULL);
3097 /* if we found a label in the same function, then we already created the
3099 if(candidate != NULL
3100 && candidate->parent_context == ¤t_function->context) {
3104 /* otherwise we need to create a new one */
3105 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
3106 declaration->namespace = NAMESPACE_LABEL;
3107 declaration->symbol = symbol;
3109 label_push(declaration);
3114 static statement_t *parse_label_statement(void)
3116 assert(token.type == T_IDENTIFIER);
3117 symbol_t *symbol = token.v.symbol;
3119 declaration_t *label = get_label(symbol);
3121 /* if source position is already set then the label is defined twice,
3122 * otherwise it was just mentioned in a goto so far */
3123 if(label->source_position.input_name != NULL) {
3124 parser_print_error_prefix();
3125 fprintf(stderr, "duplicate label '%s'\n", symbol->string);
3126 parser_print_error_prefix_pos(label->source_position);
3127 fprintf(stderr, "previous definition of '%s' was here\n",
3130 label->source_position = token.source_position;
3133 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
3135 label_statement->statement.type = STATEMENT_LABEL;
3136 label_statement->statement.source_position = token.source_position;
3140 if(token.type == '}') {
3141 parse_error("label at end of compound statement");
3142 return (statement_t*) label_statement;
3144 label_statement->label_statement = parse_statement();
3147 return (statement_t*) label_statement;
3150 static statement_t *parse_if(void)
3154 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3155 statement->statement.type = STATEMENT_IF;
3156 statement->statement.source_position = token.source_position;
3159 statement->condition = parse_expression();
3162 statement->true_statement = parse_statement();
3163 if(token.type == T_else) {
3165 statement->false_statement = parse_statement();
3168 return (statement_t*) statement;
3171 static statement_t *parse_switch(void)
3175 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3176 statement->statement.type = STATEMENT_SWITCH;
3177 statement->statement.source_position = token.source_position;
3180 statement->expression = parse_expression();
3182 statement->body = parse_statement();
3184 return (statement_t*) statement;
3187 static statement_t *parse_while(void)
3191 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3192 statement->statement.type = STATEMENT_WHILE;
3193 statement->statement.source_position = token.source_position;
3196 statement->condition = parse_expression();
3198 statement->body = parse_statement();
3200 return (statement_t*) statement;
3203 static statement_t *parse_do(void)
3207 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3208 statement->statement.type = STATEMENT_DO_WHILE;
3209 statement->statement.source_position = token.source_position;
3211 statement->body = parse_statement();
3214 statement->condition = parse_expression();
3218 return (statement_t*) statement;
3221 static statement_t *parse_for(void)
3225 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3226 statement->statement.type = STATEMENT_FOR;
3227 statement->statement.source_position = token.source_position;
3231 int top = environment_top();
3232 context_t *last_context = context;
3233 set_context(&statement->context);
3235 if(token.type != ';') {
3236 if(is_declaration_specifier(&token, false)) {
3237 parse_declaration();
3239 statement->initialisation = parse_expression();
3246 if(token.type != ';') {
3247 statement->condition = parse_expression();
3250 if(token.type != ')') {
3251 statement->step = parse_expression();
3254 statement->body = parse_statement();
3256 assert(context == &statement->context);
3257 set_context(last_context);
3258 environment_pop_to(top);
3260 return (statement_t*) statement;
3263 static statement_t *parse_goto(void)
3267 if(token.type != T_IDENTIFIER) {
3268 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
3272 symbol_t *symbol = token.v.symbol;
3274 declaration_t *label = get_label(symbol);
3276 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3278 statement->statement.type = STATEMENT_GOTO;
3279 statement->statement.source_position = token.source_position;
3281 statement->label = label;
3288 static statement_t *parse_continue(void)
3293 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3294 statement->type = STATEMENT_CONTINUE;
3295 statement->source_position = token.source_position;
3300 static statement_t *parse_break(void)
3305 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3306 statement->type = STATEMENT_BREAK;
3307 statement->source_position = token.source_position;
3312 static statement_t *parse_return(void)
3316 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3318 statement->statement.type = STATEMENT_RETURN;
3319 statement->statement.source_position = token.source_position;
3321 assert(current_function->type->type == TYPE_FUNCTION);
3322 function_type_t *function_type = (function_type_t*) current_function->type;
3323 type_t *return_type = function_type->result_type;
3325 expression_t *return_value;
3326 if(token.type != ';') {
3327 return_value = parse_expression();
3329 if(return_type == type_void && return_value->datatype != type_void) {
3330 parse_warning("'return' with a value, in function returning void");
3331 return_value = NULL;
3333 semantic_assign(return_type, &return_value, "'return'");
3336 return_value = NULL;
3337 if(return_type != type_void) {
3338 parse_warning("'return' without value, in function returning "
3342 statement->return_value = return_value;
3346 return (statement_t*) statement;
3349 static statement_t *parse_declaration_statement(void)
3351 declaration_t *before = last_declaration;
3353 declaration_statement_t *statement
3354 = allocate_ast_zero(sizeof(statement[0]));
3355 statement->statement.type = STATEMENT_DECLARATION;
3356 statement->statement.source_position = token.source_position;
3358 declaration_specifiers_t specifiers;
3359 memset(&specifiers, 0, sizeof(specifiers));
3360 parse_declaration_specifiers(&specifiers);
3362 if(token.type == ';') {
3365 parse_init_declarators(&specifiers);
3368 if(before == NULL) {
3369 statement->declarations_begin = context->declarations;
3371 statement->declarations_begin = before->next;
3373 statement->declarations_end = last_declaration;
3375 return (statement_t*) statement;
3378 static statement_t *parse_expression_statement(void)
3380 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3381 statement->statement.type = STATEMENT_EXPRESSION;
3382 statement->statement.source_position = token.source_position;
3384 statement->expression = parse_expression();
3388 return (statement_t*) statement;
3391 static statement_t *parse_statement(void)
3393 statement_t *statement = NULL;
3395 /* declaration or statement */
3396 switch(token.type) {
3398 statement = parse_case_statement();
3402 statement = parse_default_statement();
3406 statement = parse_compound_statement();
3410 statement = parse_if();
3414 statement = parse_switch();
3418 statement = parse_while();
3422 statement = parse_do();
3426 statement = parse_for();
3430 statement = parse_goto();
3434 statement = parse_continue();
3438 statement = parse_break();
3442 statement = parse_return();
3451 if(look_ahead(1)->type == ':') {
3452 statement = parse_label_statement();
3456 if(is_typedef_symbol(token.v.symbol)) {
3457 statement = parse_declaration_statement();
3461 statement = parse_expression_statement();
3464 case T___extension__:
3465 /* this can be a prefix to a declaration or an expression statement */
3466 /* we simply eat it now and parse the rest with tail recursion */
3469 } while(token.type == T___extension__);
3470 statement = parse_statement();
3474 statement = parse_declaration_statement();
3478 statement = parse_expression_statement();
3482 assert(statement == NULL || statement->source_position.input_name != NULL);
3487 static statement_t *parse_compound_statement(void)
3489 compound_statement_t *compound_statement
3490 = allocate_ast_zero(sizeof(compound_statement[0]));
3491 compound_statement->statement.type = STATEMENT_COMPOUND;
3492 compound_statement->statement.source_position = token.source_position;
3496 int top = environment_top();
3497 context_t *last_context = context;
3498 set_context(&compound_statement->context);
3500 statement_t *last_statement = NULL;
3502 while(token.type != '}' && token.type != T_EOF) {
3503 statement_t *statement = parse_statement();
3504 if(statement == NULL)
3507 if(last_statement != NULL) {
3508 last_statement->next = statement;
3510 compound_statement->statements = statement;
3513 while(statement->next != NULL)
3514 statement = statement->next;
3516 last_statement = statement;
3519 if(token.type != '}') {
3520 parser_print_error_prefix_pos(
3521 compound_statement->statement.source_position);
3522 fprintf(stderr, "end of file while looking for closing '}'\n");
3526 assert(context == &compound_statement->context);
3527 set_context(last_context);
3528 environment_pop_to(top);
3530 return (statement_t*) compound_statement;
3533 static translation_unit_t *parse_translation_unit(void)
3535 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3537 assert(global_context == NULL);
3538 global_context = &unit->context;
3540 assert(context == NULL);
3541 set_context(&unit->context);
3543 while(token.type != T_EOF) {
3544 parse_declaration();
3547 assert(context == &unit->context);
3549 last_declaration = NULL;
3551 assert(global_context == &unit->context);
3552 global_context = NULL;
3557 translation_unit_t *parse(void)
3559 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3560 label_stack = NEW_ARR_F(stack_entry_t, 0);
3561 found_error = false;
3563 type_set_output(stderr);
3564 ast_set_output(stderr);
3566 lookahead_bufpos = 0;
3567 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3570 translation_unit_t *unit = parse_translation_unit();
3572 DEL_ARR_F(environment_stack);
3573 DEL_ARR_F(label_stack);
3581 void init_parser(void)
3583 init_expression_parsers();
3584 obstack_init(&temp_obst);
3586 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3587 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3588 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3589 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3590 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3591 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3592 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3593 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3594 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3595 type_string = make_pointer_type(type_const_char, 0);
3598 void exit_parser(void)
3600 obstack_free(&temp_obst, NULL);