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 context_t *global_context = NULL;
33 static context_t *context = NULL;
34 static declaration_t *last_declaration = NULL;
35 static declaration_t *current_function = NULL;
36 static struct obstack temp_obst;
37 static bool found_error;
39 static type_t *type_int = NULL;
40 static type_t *type_uint = NULL;
41 static type_t *type_long_double = NULL;
42 static type_t *type_double = NULL;
43 static type_t *type_float = NULL;
44 static type_t *type_const_char = NULL;
45 static type_t *type_string = NULL;
46 static type_t *type_void = NULL;
47 static type_t *type_size_t = NULL;
48 static type_t *type_ptrdiff_t = NULL;
50 static statement_t *parse_compound_statement(void);
51 static statement_t *parse_statement(void);
53 static expression_t *parse_sub_expression(unsigned precedence);
54 static expression_t *parse_expression(void);
55 static type_t *parse_typename(void);
57 #define STORAGE_CLASSES \
64 #define TYPE_QUALIFIERS \
70 #ifdef PROVIDE_COMPLEX
71 #define COMPLEX_SPECIFIERS \
74 #define COMPLEX_SPECIFIERS
77 #ifdef PROVIDE_IMAGINARY
78 #define IMAGINARY_SPECIFIERS \
81 #define IMAGINARY_SPECIFIERS
84 #define TYPE_SPECIFIERS \
102 #define DECLARATION_START \
107 #define TYPENAME_START \
111 static inline void *allocate_ast_zero(size_t size)
113 void *res = allocate_ast(size);
114 memset(res, 0, size);
118 static inline void *allocate_type_zero(size_t size)
120 void *res = obstack_alloc(type_obst, size);
121 memset(res, 0, size);
125 static inline void free_type(void *type)
127 obstack_free(type_obst, type);
131 * returns the top element of the environment stack
133 static inline size_t environment_top(void)
135 return ARR_LEN(environment_stack);
140 static inline void next_token(void)
142 token = lookahead_buffer[lookahead_bufpos];
143 lookahead_buffer[lookahead_bufpos] = lexer_token;
146 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
149 print_token(stderr, &token);
150 fprintf(stderr, "\n");
154 static inline const token_t *look_ahead(int num)
156 assert(num > 0 && num <= MAX_LOOKAHEAD);
157 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
158 return & lookahead_buffer[pos];
161 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
163 static void error(void)
166 #ifdef ABORT_ON_ERROR
171 static void parser_print_prefix_pos(const source_position_t source_position)
173 fputs(source_position.input_name, stderr);
175 fprintf(stderr, "%d", source_position.linenr);
179 static void parser_print_error_prefix_pos(
180 const source_position_t source_position)
182 parser_print_prefix_pos(source_position);
183 fputs("error: ", stderr);
187 static void parser_print_error_prefix(void)
189 parser_print_error_prefix_pos(token.source_position);
192 static void parse_error(const char *message)
194 parser_print_error_prefix();
195 fprintf(stderr, "parse error: %s\n", message);
198 static void parse_warning(const char *message)
200 parser_print_prefix_pos(token.source_position);
201 fprintf(stderr, "warning: %s\n", message);
204 static void parse_error_expected(const char *message, ...)
209 if(message != NULL) {
210 parser_print_error_prefix();
211 fprintf(stderr, "%s\n", message);
213 parser_print_error_prefix();
214 fputs("Parse error: got ", stderr);
215 print_token(stderr, &token);
216 fputs(", expected ", stderr);
218 va_start(args, message);
219 token_type_t token_type = va_arg(args, token_type_t);
220 while(token_type != 0) {
224 fprintf(stderr, ", ");
226 print_token_type(stderr, token_type);
227 token_type = va_arg(args, token_type_t);
230 fprintf(stderr, "\n");
233 static void type_error(const char *msg, const source_position_t source_position,
236 parser_print_error_prefix_pos(source_position);
237 fprintf(stderr, "%s, but found type ", msg);
243 static void type_error_incompatible(const char *msg,
244 const source_position_t source_position, type_t *type1, type_t *type2)
246 parser_print_error_prefix_pos(source_position);
247 fprintf(stderr, "%s, incompatible types: ", msg);
249 fprintf(stderr, " - ");
251 fprintf(stderr, ")\n");
255 static void eat_block(void)
257 if(token.type == '{')
260 while(token.type != '}') {
261 if(token.type == T_EOF)
263 if(token.type == '{') {
272 static void eat_statement(void)
274 while(token.type != ';') {
275 if(token.type == T_EOF)
277 if(token.type == '}')
279 if(token.type == '{') {
288 static void eat_brace(void)
290 if(token.type == '(')
293 while(token.type != ')') {
294 if(token.type == T_EOF)
296 if(token.type == ')' || token.type == ';' || token.type == '}') {
299 if(token.type == '(') {
303 if(token.type == '{') {
312 #define expect(expected) \
313 if(UNLIKELY(token.type != (expected))) { \
314 parse_error_expected(NULL, (expected), 0); \
320 #define expect_void(expected) \
321 if(UNLIKELY(token.type != (expected))) { \
322 parse_error_expected(NULL, (expected), 0); \
328 static void set_context(context_t *new_context)
330 context = new_context;
332 last_declaration = new_context->declarations;
333 if(last_declaration != NULL) {
334 while(last_declaration->next != NULL) {
335 last_declaration = last_declaration->next;
341 * called when we find a 2nd declarator for an identifier we already have a
344 static bool is_compatible_declaration (declaration_t *declaration,
345 declaration_t *previous)
347 /* TODO: not correct yet */
348 return declaration->type == previous->type;
351 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
353 declaration_t *declaration = symbol->declaration;
354 for( ; declaration != NULL; declaration = declaration->symbol_next) {
355 if(declaration->namespace == namespace)
362 static const char *get_namespace_prefix(namespace_t namespace)
365 case NAMESPACE_NORMAL:
367 case NAMESPACE_UNION:
369 case NAMESPACE_STRUCT:
374 panic("invalid namespace found");
378 * pushs an environment_entry on the environment stack and links the
379 * corresponding symbol to the new entry
381 static declaration_t *environment_push(declaration_t *declaration)
383 symbol_t *symbol = declaration->symbol;
384 namespace_t namespace = declaration->namespace;
385 assert(declaration->source_position.input_name != NULL);
387 /* a declaration should be only pushed once */
388 assert(declaration->parent_context == NULL);
389 declaration->parent_context = context;
391 declaration_t *previous_declaration = get_declaration(symbol, namespace);
392 assert(declaration != previous_declaration);
393 if(previous_declaration != NULL
394 && previous_declaration->parent_context == context) {
395 if(!is_compatible_declaration(declaration, previous_declaration)) {
396 parser_print_error_prefix_pos(declaration->source_position);
397 fprintf(stderr, "definition of symbol %s%s with type ",
398 get_namespace_prefix(namespace), symbol->string);
400 print_type(declaration->type);
402 parser_print_error_prefix_pos(
403 previous_declaration->source_position);
404 fprintf(stderr, "is incompatible with previous declaration "
406 print_type(previous_declaration->type);
409 return previous_declaration;
412 /* remember old declaration */
414 entry.symbol = symbol;
415 entry.old_declaration = symbol->declaration;
416 entry.namespace = namespace;
417 ARR_APP1(environment_stack, entry);
419 /* replace/add declaration into declaration list of the symbol */
420 if(symbol->declaration == NULL) {
421 symbol->declaration = declaration;
423 declaration_t *iter = symbol->declaration;
424 for( ; iter != NULL; iter = iter->symbol_next) {
425 declaration_t *symbol_next = iter->symbol_next;
426 if(symbol_next == NULL) {
427 iter->symbol_next = declaration;
428 assert(declaration->symbol_next == NULL);
431 if(symbol_next->namespace == namespace) {
432 iter->symbol_next = declaration;
433 declaration->symbol_next = symbol_next->symbol_next;
443 * pops symbols from the environment stack until @p new_top is the top element
445 static void environment_pop_to(size_t new_top)
447 size_t top = ARR_LEN(environment_stack);
450 assert(new_top <= top);
454 for(i = top; i > new_top; --i) {
455 stack_entry_t *entry = & environment_stack[i - 1];
457 declaration_t *old_declaration = entry->old_declaration;
458 symbol_t *symbol = entry->symbol;
459 namespace_t namespace = entry->namespace;
461 /* replace/remove declaration */
462 declaration_t *declaration = symbol->declaration;
463 assert(declaration != NULL);
464 if(declaration->namespace == namespace) {
465 if(old_declaration == NULL) {
466 symbol->declaration = declaration->symbol_next;
468 symbol->declaration = old_declaration;
469 assert(old_declaration->symbol_next ==
470 declaration->symbol_next);
473 for(; declaration != NULL; declaration = declaration->symbol_next) {
474 declaration_t *symbol_next = declaration->symbol_next;
475 if(symbol_next->namespace == namespace) {
476 declaration->symbol_next = old_declaration;
477 assert(old_declaration->symbol_next
478 == symbol_next->symbol_next);
482 assert(declaration != NULL);
486 ARR_SHRINKLEN(environment_stack, (int) new_top);
490 static int get_rank(const type_t *type)
492 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
493 * and esp. footnote 108). However we can't fold constants (yet), so we
494 * can't decide wether unsigned int is possible, while int always works.
495 * (unsigned int would be preferable when possible... for stuff like
496 * struct { enum { ... } bla : 4; } ) */
497 if(type->type == TYPE_ENUM)
498 return ATOMIC_TYPE_INT;
500 assert(type->type == TYPE_ATOMIC);
501 atomic_type_t *atomic_type = (atomic_type_t*) type;
502 atomic_type_type_t atype = atomic_type->atype;
506 static type_t *promote_integer(type_t *type)
508 if(get_rank(type) < ATOMIC_TYPE_INT)
514 static expression_t *create_cast_expression(expression_t *expression,
517 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
519 cast->expression.type = EXPR_UNARY;
520 cast->type = UNEXPR_CAST;
521 cast->value = expression;
522 cast->expression.datatype = dest_type;
524 return (expression_t*) cast;
527 static expression_t *create_implicit_cast(expression_t *expression,
530 assert(expression->datatype != NULL);
531 type_t *source_type = expression->datatype;
533 if(expression->datatype == dest_type)
536 if(dest_type->type == TYPE_ATOMIC) {
537 if(source_type->type != TYPE_ATOMIC)
538 panic("casting of non-atomic types not implemented yet");
540 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
541 type_error_incompatible("can't cast types",
542 expression->source_position,
543 source_type, dest_type);
547 return create_cast_expression(expression, dest_type);
549 if(dest_type->type == TYPE_POINTER) {
550 if(source_type->type == TYPE_POINTER) {
551 if(!pointers_compatible(source_type, dest_type)) {
552 type_error_incompatible("can't implicitely cast types",
553 expression->source_position,
554 source_type, dest_type);
556 return create_cast_expression(expression, dest_type);
561 panic("casting of non-atomic types not implemented yet");
564 static void semantic_assign(type_t *orig_type_left, expression_t **right,
567 type_t *orig_type_right = (*right)->datatype;
568 type_t *type_left = skip_typeref(orig_type_left);
569 type_t *type_right = skip_typeref(orig_type_right);
571 if(type_left == type_right) {
573 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
574 *right = create_implicit_cast(*right, type_left);
575 } else if(type_left->type == TYPE_POINTER
576 && type_right->type == TYPE_POINTER) {
579 /* TODO: improve error message */
580 parser_print_error_prefix();
581 fprintf(stderr, "incompatible types in %s\n", context);
582 parser_print_error_prefix();
583 print_type(type_left);
584 fputs(" <- ", stderr);
585 print_type(type_right);
591 static expression_t *parse_constant_expression(void)
593 /* start parsing at precedence 7 (conditional expression) */
594 return parse_sub_expression(7);
597 static expression_t *parse_assignment_expression(void)
599 /* start parsing at precedence 2 (assignment expression) */
600 return parse_sub_expression(2);
603 static void parse_compound_type_entries(void);
604 static declaration_t *parse_declarator(storage_class_t storage_class,
605 type_t *type, int may_be_abstract);
606 static declaration_t *record_declaration(declaration_t *declaration);
608 typedef struct declaration_specifiers_t declaration_specifiers_t;
609 struct declaration_specifiers_t {
610 storage_class_t storage_class;
614 static const char *parse_string_literals(void)
616 assert(token.type == T_STRING_LITERAL);
617 const char *result = token.v.string;
621 while(token.type == T_STRING_LITERAL) {
622 result = concat_strings(result, token.v.string);
629 static void parse_attributes(void)
633 case T___attribute__:
641 parse_error("EOF while parsing attribute");
659 if(token.type != T_STRING_LITERAL) {
660 parse_error_expected("while parsing assembler attribute",
665 parse_string_literals();
670 goto attributes_finished;
678 static designator_t *parse_designation(void)
680 if(token.type != '[' && token.type != '.')
683 designator_t *result = NULL;
684 designator_t *last = NULL;
687 designator_t *designator;
690 designator = allocate_ast_zero(sizeof(designator[0]));
692 designator->array_access = parse_constant_expression();
696 designator = allocate_ast_zero(sizeof(designator[0]));
698 if(token.type != T_IDENTIFIER) {
699 parse_error_expected("problem while parsing designator",
703 designator->symbol = token.v.symbol;
711 assert(designator != NULL);
713 last->next = designator;
721 static initializer_t *parse_initializer_list(type_t *type);
723 static initializer_t *parse_initializer(type_t *type)
725 designator_t *designator = parse_designation();
727 initializer_t *result;
728 if(token.type == '{') {
729 result = parse_initializer_list(type);
731 result = allocate_ast_zero(sizeof(result[0]));
732 result->type = INITIALIZER_VALUE;
733 result->v.value = parse_assignment_expression();
736 semantic_assign(type, &result->v.value, "initializer");
739 result->designator = designator;
744 static initializer_t *parse_initializer_list(type_t *type)
751 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
752 result->type = INITIALIZER_LIST;
754 initializer_t *last = NULL;
756 initializer_t *initializer = parse_initializer(NULL);
758 last->next = initializer;
760 result->v.list = initializer;
764 if(token.type == '}')
767 if(token.type != ',') {
768 parse_error_expected("problem while parsing initializer list",
775 if(token.type == '}')
784 static declaration_t *parse_compound_type_specifier(bool is_struct)
792 symbol_t *symbol = NULL;
793 declaration_t *declaration = NULL;
795 if(token.type == T_IDENTIFIER) {
796 symbol = token.v.symbol;
800 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
802 declaration = get_declaration(symbol, NAMESPACE_UNION);
804 } else if(token.type != '{') {
806 parse_error_expected("problem while parsing struct type specifier",
807 T_IDENTIFIER, '{', 0);
809 parse_error_expected("problem while parsing union type specifier",
810 T_IDENTIFIER, '{', 0);
816 if(declaration == NULL) {
817 declaration = allocate_type_zero(sizeof(declaration[0]));
820 declaration->namespace = NAMESPACE_STRUCT;
822 declaration->namespace = NAMESPACE_UNION;
824 declaration->source_position = token.source_position;
825 declaration->symbol = symbol;
828 if(token.type == '{') {
829 if(declaration->init.is_defined) {
830 assert(symbol != NULL);
831 parser_print_error_prefix();
832 fprintf(stderr, "multiple definition of %s %s\n",
833 is_struct ? "struct" : "union", symbol->string);
834 declaration->context.declarations = NULL;
836 record_declaration(declaration);
837 declaration->init.is_defined = true;
839 int top = environment_top();
840 context_t *last_context = context;
841 set_context(& declaration->context);
843 parse_compound_type_entries();
846 assert(context == & declaration->context);
847 set_context(last_context);
848 environment_pop_to(top);
854 static void parse_enum_entries(void)
858 if(token.type == '}') {
860 parse_error("empty enum not allowed");
865 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
867 if(token.type != T_IDENTIFIER) {
868 parse_error_expected("problem while parsing enum entry",
873 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
874 entry->symbol = token.v.symbol;
875 entry->source_position = token.source_position;
878 if(token.type == '=') {
880 entry->init.initializer = parse_initializer(type_int);
883 record_declaration(entry);
885 if(token.type != ',')
888 } while(token.type != '}');
893 static declaration_t *parse_enum_specifier(void)
897 declaration_t *declaration;
900 if(token.type == T_IDENTIFIER) {
901 symbol = token.v.symbol;
904 declaration = get_declaration(symbol, NAMESPACE_ENUM);
905 } else if(token.type != '{') {
906 parse_error_expected("problem while parsing enum type specifier",
907 T_IDENTIFIER, '{', 0);
914 if(declaration == NULL) {
915 declaration = allocate_type_zero(sizeof(declaration[0]));
917 declaration->namespace = NAMESPACE_ENUM;
918 declaration->source_position = token.source_position;
919 declaration->symbol = symbol;
922 if(token.type == '{') {
923 if(declaration->init.is_defined) {
924 parser_print_error_prefix();
925 fprintf(stderr, "multiple definitions of enum %s\n",
928 record_declaration(declaration);
929 declaration->init.is_defined = 1;
931 parse_enum_entries();
939 * if a symbol is a typedef to another type, return true
941 static bool is_typedef_symbol(symbol_t *symbol)
943 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
944 if(declaration == NULL
945 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
951 static type_t *parse_typeof(void)
959 expression_t *expression = NULL;
963 case T___extension__:
964 /* this can be a prefix to a typename or an expression */
965 /* we simply eat it now. */
968 } while(token.type == T___extension__);
972 if(is_typedef_symbol(token.v.symbol)) {
973 type = parse_typename();
975 expression = parse_expression();
976 type = expression->datatype;
981 type = parse_typename();
985 expression = parse_expression();
986 type = expression->datatype;
992 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
993 typeof->type.type = TYPE_TYPEOF;
994 typeof->expression = expression;
995 typeof->typeof_type = type;
997 return (type_t*) typeof;
1001 SPECIFIER_SIGNED = 1 << 0,
1002 SPECIFIER_UNSIGNED = 1 << 1,
1003 SPECIFIER_LONG = 1 << 2,
1004 SPECIFIER_INT = 1 << 3,
1005 SPECIFIER_DOUBLE = 1 << 4,
1006 SPECIFIER_CHAR = 1 << 5,
1007 SPECIFIER_SHORT = 1 << 6,
1008 SPECIFIER_LONG_LONG = 1 << 7,
1009 SPECIFIER_FLOAT = 1 << 8,
1010 SPECIFIER_BOOL = 1 << 9,
1011 SPECIFIER_VOID = 1 << 10,
1012 #ifdef PROVIDE_COMPLEX
1013 SPECIFIER_COMPLEX = 1 << 11,
1015 #ifdef PROVIDE_IMAGINARY
1016 SPECIFIER_IMAGINARY = 1 << 12,
1020 static type_t *create_builtin_type(symbol_t *symbol)
1022 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1023 type->type.type = TYPE_BUILTIN;
1024 type->symbol = symbol;
1026 type->real_type = type_int;
1028 return (type_t*) type;
1031 static type_t *get_typedef_type(symbol_t *symbol)
1033 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1034 if(declaration == NULL
1035 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1038 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1039 typedef_type->type.type = TYPE_TYPEDEF;
1040 typedef_type->declaration = declaration;
1042 return (type_t*) typedef_type;
1045 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1047 type_t *type = NULL;
1048 unsigned type_qualifiers = 0;
1049 unsigned type_specifiers = 0;
1053 switch(token.type) {
1056 #define MATCH_STORAGE_CLASS(token, class) \
1058 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1059 parse_error("multiple storage classes in declaration " \
1062 specifiers->storage_class = class; \
1066 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1067 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1068 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1069 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1070 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1072 /* type qualifiers */
1073 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1075 type_qualifiers |= qualifier; \
1079 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1080 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1081 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1082 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1084 case T___extension__:
1089 /* type specifiers */
1090 #define MATCH_SPECIFIER(token, specifier, name) \
1093 if(type_specifiers & specifier) { \
1094 parse_error("multiple " name " type specifiers given"); \
1096 type_specifiers |= specifier; \
1100 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1101 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1102 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1103 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1104 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1105 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1106 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1107 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1108 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1109 #ifdef PROVIDE_COMPLEX
1110 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1112 #ifdef PROVIDE_IMAGINARY
1113 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1117 if(type_specifiers & SPECIFIER_LONG_LONG) {
1118 parse_error("multiple type specifiers given");
1119 } else if(type_specifiers & SPECIFIER_LONG) {
1120 type_specifiers |= SPECIFIER_LONG_LONG;
1122 type_specifiers |= SPECIFIER_LONG;
1126 /* TODO: if type != NULL for the following rules should issue
1129 compound_type_t *compound_type
1130 = allocate_type_zero(sizeof(compound_type[0]));
1131 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1132 compound_type->declaration = parse_compound_type_specifier(true);
1134 type = (type_t*) compound_type;
1138 compound_type_t *compound_type
1139 = allocate_type_zero(sizeof(compound_type[0]));
1140 compound_type->type.type = TYPE_COMPOUND_UNION;
1141 compound_type->declaration = parse_compound_type_specifier(false);
1143 type = (type_t*) compound_type;
1147 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1148 enum_type->type.type = TYPE_ENUM;
1149 enum_type->declaration = parse_enum_specifier();
1151 type = (type_t*) enum_type;
1155 type = parse_typeof();
1157 case T___builtin_va_list:
1158 type = create_builtin_type(token.v.symbol);
1162 case T___attribute__:
1167 case T_IDENTIFIER: {
1168 type_t *typedef_type = get_typedef_type(token.v.symbol);
1170 if(typedef_type == NULL)
1171 goto finish_specifiers;
1174 type = typedef_type;
1178 /* function specifier */
1180 goto finish_specifiers;
1187 atomic_type_type_t atomic_type;
1189 /* match valid basic types */
1190 switch(type_specifiers) {
1191 case SPECIFIER_VOID:
1192 atomic_type = ATOMIC_TYPE_VOID;
1194 case SPECIFIER_CHAR:
1195 atomic_type = ATOMIC_TYPE_CHAR;
1197 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1198 atomic_type = ATOMIC_TYPE_SCHAR;
1200 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1201 atomic_type = ATOMIC_TYPE_UCHAR;
1203 case SPECIFIER_SHORT:
1204 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1205 case SPECIFIER_SHORT | SPECIFIER_INT:
1206 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1207 atomic_type = ATOMIC_TYPE_SHORT;
1209 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1210 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1211 atomic_type = ATOMIC_TYPE_USHORT;
1214 case SPECIFIER_SIGNED:
1215 case SPECIFIER_SIGNED | SPECIFIER_INT:
1216 atomic_type = ATOMIC_TYPE_INT;
1218 case SPECIFIER_UNSIGNED:
1219 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1220 atomic_type = ATOMIC_TYPE_UINT;
1222 case SPECIFIER_LONG:
1223 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1224 case SPECIFIER_LONG | SPECIFIER_INT:
1225 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1226 atomic_type = ATOMIC_TYPE_LONG;
1228 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1229 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1230 atomic_type = ATOMIC_TYPE_ULONG;
1232 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1233 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1234 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1235 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1237 atomic_type = ATOMIC_TYPE_LONGLONG;
1239 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1240 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1242 atomic_type = ATOMIC_TYPE_ULONGLONG;
1244 case SPECIFIER_FLOAT:
1245 atomic_type = ATOMIC_TYPE_FLOAT;
1247 case SPECIFIER_DOUBLE:
1248 atomic_type = ATOMIC_TYPE_DOUBLE;
1250 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1251 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1253 case SPECIFIER_BOOL:
1254 atomic_type = ATOMIC_TYPE_BOOL;
1256 #ifdef PROVIDE_COMPLEX
1257 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1258 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1260 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1261 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1263 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1264 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1267 #ifdef PROVIDE_IMAGINARY
1268 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1269 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1271 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1272 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1274 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1275 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1279 /* invalid specifier combination, give an error message */
1280 if(type_specifiers == 0) {
1282 parse_warning("no type specifiers in declaration (using int)");
1283 atomic_type = ATOMIC_TYPE_INT;
1286 parse_error("no type specifiers given in declaration");
1288 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1289 (type_specifiers & SPECIFIER_UNSIGNED)) {
1290 parse_error("signed and unsigned specifiers gives");
1291 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1292 parse_error("only integer types can be signed or unsigned");
1294 parse_error("multiple datatypes in declaration");
1296 atomic_type = ATOMIC_TYPE_INVALID;
1299 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1300 atype->type.type = TYPE_ATOMIC;
1301 atype->atype = atomic_type;
1304 type = (type_t*) atype;
1306 if(type_specifiers != 0) {
1307 parse_error("multiple datatypes in declaration");
1311 type->qualifiers = type_qualifiers;
1313 type_t *result = typehash_insert(type);
1314 if(newtype && result != (type_t*) type) {
1318 specifiers->type = result;
1321 static type_qualifier_t parse_type_qualifiers(void)
1323 type_qualifier_t type_qualifiers = 0;
1326 switch(token.type) {
1327 /* type qualifiers */
1328 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1329 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1330 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1331 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1334 return type_qualifiers;
1339 static void parse_identifier_list(void)
1342 if(token.type != T_IDENTIFIER) {
1343 parse_error_expected("problem while parsing parameter identifier "
1344 "list", T_IDENTIFIER, 0);
1348 if(token.type != ',')
1354 static declaration_t *parse_parameter(void)
1356 declaration_specifiers_t specifiers;
1357 memset(&specifiers, 0, sizeof(specifiers));
1359 parse_declaration_specifiers(&specifiers);
1361 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1362 specifiers.type, 1);
1364 /* TODO check declaration constraints for parameters */
1365 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1366 parse_error("typedef not allowed in parameter list");
1372 static declaration_t *parse_parameters(function_type_t *type)
1374 if(token.type == T_IDENTIFIER) {
1375 symbol_t *symbol = token.v.symbol;
1376 if(!is_typedef_symbol(symbol)) {
1378 parse_identifier_list();
1383 if(token.type == ')') {
1384 type->unspecified_parameters = 1;
1387 if(token.type == T_void && look_ahead(1)->type == ')') {
1392 declaration_t *declarations = NULL;
1393 declaration_t *declaration;
1394 declaration_t *last_declaration = NULL;
1395 function_parameter_t *parameter;
1396 function_parameter_t *last_parameter = NULL;
1399 switch(token.type) {
1403 return declarations;
1406 case T___extension__:
1408 declaration = parse_parameter();
1410 parameter = allocate_type_zero(sizeof(parameter[0]));
1411 parameter->type = declaration->type;
1413 if(last_parameter != NULL) {
1414 last_declaration->next = declaration;
1415 last_parameter->next = parameter;
1417 type->parameters = parameter;
1418 declarations = declaration;
1420 last_parameter = parameter;
1421 last_declaration = declaration;
1425 return declarations;
1427 if(token.type != ',')
1428 return declarations;
1437 } construct_type_type_t;
1439 typedef struct construct_type_t construct_type_t;
1440 struct construct_type_t {
1441 construct_type_type_t type;
1442 construct_type_t *next;
1445 typedef struct parsed_pointer_t parsed_pointer_t;
1446 struct parsed_pointer_t {
1447 construct_type_t construct_type;
1448 type_qualifier_t type_qualifiers;
1451 typedef struct construct_function_type_t construct_function_type_t;
1452 struct construct_function_type_t {
1453 construct_type_t construct_type;
1454 function_type_t *function_type;
1457 typedef struct parsed_array_t parsed_array_t;
1458 struct parsed_array_t {
1459 construct_type_t construct_type;
1460 type_qualifier_t type_qualifiers;
1466 typedef struct construct_base_type_t construct_base_type_t;
1467 struct construct_base_type_t {
1468 construct_type_t construct_type;
1472 static construct_type_t *parse_pointer_declarator(void)
1476 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1477 memset(pointer, 0, sizeof(pointer[0]));
1478 pointer->type_qualifiers = parse_type_qualifiers();
1480 return (construct_type_t*) pointer;
1483 static construct_type_t *parse_array_declarator(void)
1487 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1488 memset(array, 0, sizeof(array[0]));
1490 if(token.type == T_static) {
1491 array->is_static = true;
1495 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1496 if(type_qualifiers != 0) {
1497 if(token.type == T_static) {
1498 array->is_static = true;
1502 array->type_qualifiers = type_qualifiers;
1504 if(token.type == '*' && look_ahead(1)->type == ']') {
1505 array->is_variable = true;
1507 } else if(token.type != ']') {
1508 array->size = parse_assignment_expression();
1513 return (construct_type_t*) array;
1516 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1520 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1521 type->type.type = TYPE_FUNCTION;
1523 declaration_t *parameters = parse_parameters(type);
1524 if(declaration != NULL) {
1525 declaration->context.declarations = parameters;
1528 construct_function_type_t *construct_function_type =
1529 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1530 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1531 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1532 construct_function_type->function_type = type;
1536 return (construct_type_t*) construct_function_type;
1539 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1540 int may_be_abstract)
1542 construct_type_t *result = NULL;
1543 construct_type_t *last = NULL;
1545 while(token.type == '*') {
1546 construct_type_t *type = parse_pointer_declarator();
1555 /* TODO: find out if this is correct */
1558 construct_type_t *inner_types = NULL;
1560 switch(token.type) {
1562 if(declaration == NULL) {
1563 parse_error("no identifier expected in typename");
1565 declaration->symbol = token.v.symbol;
1566 declaration->source_position = token.source_position;
1572 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1578 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1583 construct_type_t *type;
1584 switch(token.type) {
1586 type = parse_function_declarator(declaration);
1589 type = parse_array_declarator();
1592 goto declarator_finished;
1603 declarator_finished:
1606 if(inner_types != NULL) {
1608 last->next = inner_types;
1610 result = inner_types;
1618 static type_t *construct_declarator_type(construct_type_t *construct_list,
1621 construct_type_t *iter = construct_list;
1622 for( ; iter != NULL; iter = iter->next) {
1623 parsed_pointer_t *parsed_pointer;
1624 parsed_array_t *parsed_array;
1625 construct_function_type_t *construct_function_type;
1626 function_type_t *function_type;
1627 pointer_type_t *pointer_type;
1628 array_type_t *array_type;
1630 switch(iter->type) {
1631 case CONSTRUCT_FUNCTION:
1632 construct_function_type = (construct_function_type_t*) iter;
1633 function_type = construct_function_type->function_type;
1635 function_type->result_type = type;
1636 type = (type_t*) function_type;
1639 case CONSTRUCT_POINTER:
1640 parsed_pointer = (parsed_pointer_t*) iter;
1641 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1643 pointer_type->type.type = TYPE_POINTER;
1644 pointer_type->points_to = type;
1645 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1646 type = (type_t*) pointer_type;
1649 case CONSTRUCT_ARRAY:
1650 parsed_array = (parsed_array_t*) iter;
1651 array_type = allocate_type_zero(sizeof(array_type[0]));
1653 array_type->type.type = TYPE_ARRAY;
1654 array_type->element_type = type;
1655 array_type->type.qualifiers = parsed_array->type_qualifiers;
1656 array_type->is_static = parsed_array->is_static;
1657 array_type->is_variable = parsed_array->is_variable;
1658 array_type->size = parsed_array->size;
1659 type = (type_t*) array_type;
1663 type_t *hashed_type = typehash_insert((type_t*) type);
1664 if(hashed_type != type) {
1673 static declaration_t *parse_declarator(storage_class_t storage_class,
1674 type_t *type, int may_be_abstract)
1676 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1677 declaration->storage_class = storage_class;
1679 construct_type_t *construct_type
1680 = parse_inner_declarator(declaration, may_be_abstract);
1681 declaration->type = construct_declarator_type(construct_type, type);
1683 if(construct_type != NULL) {
1684 obstack_free(&temp_obst, construct_type);
1690 static type_t *parse_abstract_declarator(type_t *base_type)
1692 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1694 type_t *result = construct_declarator_type(construct_type, base_type);
1695 if(construct_type != NULL) {
1696 obstack_free(&temp_obst, construct_type);
1702 static declaration_t *record_declaration(declaration_t *declaration)
1704 assert(context != NULL);
1706 symbol_t *symbol = declaration->symbol;
1707 if(symbol != NULL) {
1708 declaration_t *alias = environment_push(declaration);
1709 if(alias != declaration)
1712 declaration->parent_context = context;
1715 if(last_declaration != NULL) {
1716 last_declaration->next = declaration;
1718 context->declarations = declaration;
1720 last_declaration = declaration;
1725 static void parser_error_multiple_definition(declaration_t *previous,
1726 declaration_t *declaration)
1728 parser_print_error_prefix_pos(declaration->source_position);
1729 fprintf(stderr, "multiple definition of symbol '%s'\n",
1730 declaration->symbol->string);
1731 parser_print_error_prefix_pos(previous->source_position);
1732 fprintf(stderr, "this is the location of the previous "
1737 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1740 declaration_t *ndeclaration
1741 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1743 declaration_t *declaration = record_declaration(ndeclaration);
1744 if(token.type == '=') {
1747 /* TODO: check that this is an allowed type (no function type) */
1749 if(declaration->init.initializer != NULL) {
1750 parser_error_multiple_definition(declaration, ndeclaration);
1753 ndeclaration->init.initializer = parse_initializer(declaration->type);
1754 } else if(token.type == '{') {
1755 if(declaration->type->type != TYPE_FUNCTION) {
1756 parser_print_error_prefix();
1757 fprintf(stderr, "Declarator ");
1758 print_type_ext(declaration->type, declaration->symbol, NULL);
1759 fprintf(stderr, " has a body but is not a function type.\n");
1764 if(declaration->init.statement != NULL) {
1765 parser_error_multiple_definition(declaration, ndeclaration);
1767 if(ndeclaration != declaration) {
1768 memcpy(&declaration->context, &ndeclaration->context,
1769 sizeof(declaration->context));
1772 int top = environment_top();
1773 context_t *last_context = context;
1774 set_context(&declaration->context);
1776 /* push function parameters */
1777 declaration_t *parameter = declaration->context.declarations;
1778 for( ; parameter != NULL; parameter = parameter->next) {
1779 environment_push(parameter);
1781 declaration_t *old_current_function = current_function;
1782 current_function = declaration;
1784 statement_t *statement = parse_compound_statement();
1786 assert(current_function == declaration);
1787 old_current_function = current_function;
1789 assert(context == &declaration->context);
1790 set_context(last_context);
1791 environment_pop_to(top);
1793 declaration->init.statement = statement;
1797 if(token.type != ',')
1804 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1807 if(token.type == ':') {
1809 parse_constant_expression();
1810 /* TODO (bitfields) */
1812 declaration_t *declaration
1813 = parse_declarator(specifiers->storage_class,
1814 specifiers->type, 1);
1816 /* TODO: check constraints for struct declarations */
1817 /* TODO: check for doubled fields */
1818 record_declaration(declaration);
1820 if(token.type == ':') {
1822 parse_constant_expression();
1823 /* TODO (bitfields) */
1827 if(token.type != ',')
1834 static void parse_compound_type_entries(void)
1838 while(token.type != '}' && token.type != T_EOF) {
1839 declaration_specifiers_t specifiers;
1840 memset(&specifiers, 0, sizeof(specifiers));
1841 parse_declaration_specifiers(&specifiers);
1843 parse_struct_declarators(&specifiers);
1845 if(token.type == T_EOF) {
1846 parse_error("unexpected error while parsing struct");
1851 static void parse_declaration(void)
1853 source_position_t source_position = token.source_position;
1855 declaration_specifiers_t specifiers;
1856 memset(&specifiers, 0, sizeof(specifiers));
1857 parse_declaration_specifiers(&specifiers);
1859 if(token.type == ';') {
1862 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1864 declaration->type = specifiers.type;
1865 declaration->storage_class = specifiers.storage_class;
1866 declaration->source_position = source_position;
1867 record_declaration(declaration);
1870 parse_init_declarators(&specifiers);
1873 static type_t *parse_typename(void)
1875 declaration_specifiers_t specifiers;
1876 memset(&specifiers, 0, sizeof(specifiers));
1877 parse_declaration_specifiers(&specifiers);
1878 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1879 /* TODO: improve error message, user does probably not know what a
1880 * storage class is...
1882 parse_error("typename may not have a storage class");
1885 type_t *result = parse_abstract_declarator(specifiers.type);
1893 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1894 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1895 expression_t *left);
1897 typedef struct expression_parser_function_t expression_parser_function_t;
1898 struct expression_parser_function_t {
1899 unsigned precedence;
1900 parse_expression_function parser;
1901 unsigned infix_precedence;
1902 parse_expression_infix_function infix_parser;
1905 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1907 static expression_t *expected_expression_error(void)
1909 parser_print_error_prefix();
1910 fprintf(stderr, "expected expression, got token ");
1911 print_token(stderr, & token);
1912 fprintf(stderr, "\n");
1914 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1915 expression->type = EXPR_INVALID;
1921 static expression_t *parse_string_const(void)
1923 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1925 cnst->expression.type = EXPR_STRING_LITERAL;
1926 cnst->expression.datatype = type_string;
1927 cnst->value = parse_string_literals();
1929 return (expression_t*) cnst;
1932 static expression_t *parse_int_const(void)
1934 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1936 cnst->expression.type = EXPR_CONST;
1937 cnst->expression.datatype = type_int;
1938 cnst->v.int_value = token.v.intvalue;
1942 return (expression_t*) cnst;
1945 static expression_t *parse_float_const(void)
1947 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1949 cnst->expression.type = EXPR_CONST;
1950 cnst->expression.datatype = type_double;
1951 cnst->v.float_value = token.v.floatvalue;
1955 return (expression_t*) cnst;
1958 static declaration_t *create_implicit_function(symbol_t *symbol,
1959 const source_position_t source_position)
1961 function_type_t *function_type = allocate_type_zero(sizeof(function_type));
1963 function_type->type.type = TYPE_FUNCTION;
1964 function_type->result_type = type_int;
1965 function_type->unspecified_parameters = true;
1967 type_t *type = typehash_insert((type_t*) function_type);
1968 if(type != (type_t*) function_type) {
1969 free_type(function_type);
1972 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1974 declaration->storage_class = STORAGE_CLASS_EXTERN;
1975 declaration->type = type;
1976 declaration->symbol = symbol;
1977 declaration->source_position = source_position;
1979 /* prepend the implicit definition to the global context
1980 * this is safe since the symbol wasn't declared as anything else yet
1982 assert(symbol->declaration == NULL);
1984 context_t *last_context = context;
1985 context = global_context;
1987 environment_push(declaration);
1988 declaration->next = context->declarations;
1989 context->declarations = declaration;
1991 context = last_context;
1996 static expression_t *parse_reference(void)
1998 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2000 ref->expression.type = EXPR_REFERENCE;
2001 ref->symbol = token.v.symbol;
2003 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2005 source_position_t source_position = token.source_position;
2008 if(declaration == NULL) {
2010 /* an implicitely defined function */
2011 if(token.type == '(') {
2012 parser_print_prefix_pos(token.source_position);
2013 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2014 ref->symbol->string);
2016 declaration = create_implicit_function(ref->symbol,
2021 parser_print_error_prefix();
2022 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2023 return (expression_t*) ref;
2027 ref->declaration = declaration;
2028 ref->expression.datatype = declaration->type;
2030 return (expression_t*) ref;
2033 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2037 /* TODO check if explicit cast is allowed and issue warnings/errors */
2040 static expression_t *parse_cast(void)
2042 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2044 cast->expression.type = EXPR_UNARY;
2045 cast->type = UNEXPR_CAST;
2046 cast->expression.source_position = token.source_position;
2048 type_t *type = parse_typename();
2051 expression_t *value = parse_sub_expression(20);
2053 check_cast_allowed(value, type);
2055 cast->expression.datatype = type;
2056 cast->value = value;
2058 return (expression_t*) cast;
2061 static expression_t *parse_statement_expression(void)
2063 statement_expression_t *expression
2064 = allocate_ast_zero(sizeof(expression[0]));
2065 expression->expression.type = EXPR_STATEMENT;
2066 expression->statement = parse_compound_statement();
2068 /* find last statement and use it's type */
2069 const statement_t *last_statement = NULL;
2070 const statement_t *statement = expression->statement;
2071 for( ; statement != NULL; statement = statement->next) {
2072 last_statement = statement;
2075 if(last_statement->type == STATEMENT_EXPRESSION) {
2076 const expression_statement_t *expression_statement =
2077 (const expression_statement_t*) last_statement;
2078 expression->expression.datatype
2079 = expression_statement->expression->datatype;
2081 expression->expression.datatype = type_void;
2086 return (expression_t*) expression;
2089 static expression_t *parse_brace_expression(void)
2093 switch(token.type) {
2095 /* gcc extension: a stement expression */
2096 return parse_statement_expression();
2100 return parse_cast();
2102 if(is_typedef_symbol(token.v.symbol)) {
2103 return parse_cast();
2107 expression_t *result = parse_expression();
2113 static expression_t *parse_function_keyword(void)
2115 eat(T___FUNCTION__);
2118 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2119 expression->expression.type = EXPR_FUNCTION;
2120 expression->expression.datatype = type_string;
2121 expression->value = "TODO: FUNCTION";
2123 return (expression_t*) expression;
2126 static expression_t *parse_pretty_function_keyword(void)
2128 eat(T___PRETTY_FUNCTION__);
2131 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2132 expression->expression.type = EXPR_PRETTY_FUNCTION;
2133 expression->expression.datatype = type_string;
2134 expression->value = "TODO: PRETTY FUNCTION";
2136 return (expression_t*) expression;
2139 static designator_t *parse_designator(void)
2141 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2143 if(token.type != T_IDENTIFIER) {
2144 parse_error_expected("problem while parsing member designator",
2149 result->symbol = token.v.symbol;
2152 designator_t *last_designator = result;
2154 if(token.type == '.') {
2156 if(token.type != T_IDENTIFIER) {
2157 parse_error_expected("problem while parsing member designator",
2162 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2163 designator->symbol = token.v.symbol;
2166 last_designator->next = designator;
2167 last_designator = designator;
2170 if(token.type == '[') {
2172 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2173 designator->array_access = parse_expression();
2174 if(designator->array_access == NULL) {
2180 last_designator->next = designator;
2181 last_designator = designator;
2190 static expression_t *parse_offsetof(void)
2192 eat(T___builtin_offsetof);
2194 offsetof_expression_t *expression
2195 = allocate_ast_zero(sizeof(expression[0]));
2196 expression->expression.type = EXPR_OFFSETOF;
2197 expression->expression.datatype = type_size_t;
2200 expression->type = parse_typename();
2202 expression->designator = parse_designator();
2205 return (expression_t*) expression;
2208 static expression_t *parse_va_arg(void)
2210 eat(T___builtin_va_arg);
2212 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2213 expression->expression.type = EXPR_VA_ARG;
2216 expression->arg = parse_assignment_expression();
2218 expression->expression.datatype = parse_typename();
2221 return (expression_t*) expression;
2224 static expression_t *parse_builtin_symbol(void)
2226 builtin_symbol_expression_t *expression
2227 = allocate_ast_zero(sizeof(expression[0]));
2228 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2230 /* TODO: set datatype */
2232 expression->symbol = token.v.symbol;
2236 return (expression_t*) expression;
2239 static expression_t *parse_primary_expression(void)
2241 switch(token.type) {
2243 return parse_int_const();
2244 case T_FLOATINGPOINT:
2245 return parse_float_const();
2246 case T_STRING_LITERAL:
2247 return parse_string_const();
2249 return parse_reference();
2250 case T___FUNCTION__:
2251 return parse_function_keyword();
2252 case T___PRETTY_FUNCTION__:
2253 return parse_pretty_function_keyword();
2254 case T___builtin_offsetof:
2255 return parse_offsetof();
2256 case T___builtin_va_arg:
2257 return parse_va_arg();
2258 case T___builtin_expect:
2259 case T___builtin_va_start:
2260 case T___builtin_va_end:
2261 return parse_builtin_symbol();
2264 return parse_brace_expression();
2267 parser_print_error_prefix();
2268 fprintf(stderr, "unexpected token ");
2269 print_token(stderr, &token);
2270 fprintf(stderr, "\n");
2273 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2274 expression->type = EXPR_INVALID;
2275 expression->datatype = type_void;
2280 static expression_t *parse_array_expression(unsigned precedence,
2281 expression_t *array_ref)
2287 array_access_expression_t *array_access
2288 = allocate_ast_zero(sizeof(array_access[0]));
2290 array_access->expression.type = EXPR_ARRAY_ACCESS;
2291 array_access->array_ref = array_ref;
2292 array_access->index = parse_expression();
2294 type_t *array_type = array_ref->datatype;
2295 if(array_type != NULL) {
2296 if(array_type->type == TYPE_POINTER) {
2297 pointer_type_t *pointer = (pointer_type_t*) array_type;
2298 array_access->expression.datatype = pointer->points_to;
2300 parser_print_error_prefix();
2301 fprintf(stderr, "array access on object with non-pointer type ");
2302 print_type(array_type);
2303 fprintf(stderr, "\n");
2307 if(token.type != ']') {
2308 parse_error_expected("Problem while parsing array access", ']', 0);
2309 return (expression_t*) array_access;
2313 return (expression_t*) array_access;
2316 static bool is_declaration_specifier(const token_t *token,
2317 bool only_type_specifiers)
2319 switch(token->type) {
2323 return is_typedef_symbol(token->v.symbol);
2326 if(only_type_specifiers)
2335 static expression_t *parse_sizeof(unsigned precedence)
2339 sizeof_expression_t *sizeof_expression
2340 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2341 sizeof_expression->expression.type = EXPR_SIZEOF;
2342 sizeof_expression->expression.datatype = type_size_t;
2344 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2346 sizeof_expression->type = parse_typename();
2349 expression_t *expression = parse_sub_expression(precedence);
2350 sizeof_expression->type = expression->datatype;
2351 sizeof_expression->size_expression = expression;
2354 return (expression_t*) sizeof_expression;
2357 static expression_t *parse_select_expression(unsigned precedence,
2358 expression_t *compound)
2362 assert(token.type == '.' || token.type == T_MINUSGREATER);
2365 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2367 select->expression.type = EXPR_SELECT;
2368 select->compound = compound;
2370 /* TODO: datatype */
2372 if(token.type != T_IDENTIFIER) {
2373 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2374 return (expression_t*) select;
2376 select->symbol = token.v.symbol;
2379 return (expression_t*) select;
2382 static expression_t *parse_call_expression(unsigned precedence,
2383 expression_t *expression)
2386 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2387 call->expression.type = EXPR_CALL;
2388 call->function = expression;
2390 function_type_t *function_type;
2391 type_t *type = expression->datatype;
2392 if(type->type != TYPE_FUNCTION) {
2393 /* TODO calling pointers to functions is ok */
2394 parser_print_error_prefix();
2395 fputs("called object '", stderr);
2396 print_expression(expression);
2397 fputs("' (type ", stderr);
2399 fputs("is not a function\n", stderr);
2401 function_type = NULL;
2402 call->expression.datatype = NULL;
2404 function_type = (function_type_t*) type;
2405 call->expression.datatype = function_type->result_type;
2408 /* parse arguments */
2411 if(token.type != ')') {
2412 call_argument_t *last_argument = NULL;
2415 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2417 argument->expression = parse_assignment_expression();
2418 if(last_argument == NULL) {
2419 call->arguments = argument;
2421 last_argument->next = argument;
2423 last_argument = argument;
2425 if(token.type != ',')
2432 if(function_type != NULL) {
2433 function_parameter_t *parameter = function_type->parameters;
2434 call_argument_t *argument = call->arguments;
2435 for( ; parameter != NULL && argument != NULL;
2436 parameter = parameter->next, argument = argument->next) {
2437 type_t *expected_type = parameter->type;
2438 /* TODO report context in error messages */
2439 argument->expression = create_implicit_cast(argument->expression,
2442 /* too few parameters */
2443 if(parameter != NULL) {
2444 parser_print_error_prefix();
2445 fprintf(stderr, "too few arguments to function '");
2446 print_expression(expression);
2447 fprintf(stderr, "'\n");
2448 } else if(argument != NULL) {
2449 /* too many parameters */
2450 if(!function_type->variadic
2451 && !function_type->unspecified_parameters) {
2452 parser_print_error_prefix();
2453 fprintf(stderr, "too many arguments to function '");
2454 print_expression(expression);
2455 fprintf(stderr, "'\n");
2457 /* do default promotion */
2458 for( ; argument != NULL; argument = argument->next) {
2459 type_t *type = argument->expression->datatype;
2461 if(is_type_integer(type)) {
2462 type = promote_integer(type);
2463 } else if(type == type_float) {
2466 argument->expression
2467 = create_implicit_cast(argument->expression, type);
2473 return (expression_t*) call;
2476 static type_t *get_type_after_conversion(const type_t *type1,
2477 const type_t *type2)
2481 return (type_t*) type1;
2484 static expression_t *parse_conditional_expression(unsigned precedence,
2485 expression_t *expression)
2489 conditional_expression_t *conditional
2490 = allocate_ast_zero(sizeof(conditional[0]));
2491 conditional->expression.type = EXPR_CONDITIONAL;
2492 conditional->condition = expression;
2495 type_t *condition_type = conditional->condition->datatype;
2496 if(condition_type != NULL) {
2497 if(!is_type_scalar(condition_type)) {
2498 type_error("expected a scalar type", expression->source_position,
2503 conditional->true_expression = parse_expression();
2505 conditional->false_expression = parse_sub_expression(precedence);
2507 type_t *true_type = conditional->true_expression->datatype;
2508 if(true_type == NULL)
2509 return (expression_t*) conditional;
2510 type_t *false_type = conditional->false_expression->datatype;
2511 if(false_type == NULL)
2512 return (expression_t*) conditional;
2515 if(true_type == false_type) {
2516 conditional->expression.datatype = true_type;
2517 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2518 type_t *result = get_type_after_conversion(true_type, false_type);
2519 /* TODO: create implicit convs if necessary */
2520 conditional->expression.datatype = result;
2521 } else if(true_type->type == TYPE_POINTER &&
2522 false_type->type == TYPE_POINTER &&
2523 true /* TODO compatible points_to types */) {
2525 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2526 || (is_null_ptr_const(false_type) &&
2527 true_type->type == TYPE_POINTER) TODO*/ false) {
2529 } else if(/* 1 is pointer to object type, other is void* */ false) {
2532 type_error_incompatible("problem while parsing conditional",
2533 expression->source_position, true_type,
2537 return (expression_t*) conditional;
2540 static expression_t *parse_extension(unsigned precedence)
2542 eat(T___extension__);
2544 /* TODO enable extensions */
2546 return parse_sub_expression(precedence);
2549 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2552 return expression->datatype;
2555 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2557 type_t *expression_type = expression->datatype;
2559 if(expression_type->type == TYPE_POINTER) {
2560 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2561 return pointer_type->points_to;
2563 panic("deref TODO...");
2567 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2569 type_t *type = expression->datatype;
2570 return make_pointer_type(type, 0);
2573 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2574 static expression_t *parse_##unexpression_type(unsigned precedence) \
2578 unary_expression_t *unary_expression \
2579 = allocate_ast_zero(sizeof(unary_expression[0])); \
2580 unary_expression->expression.type = EXPR_UNARY; \
2581 unary_expression->type = unexpression_type; \
2582 unary_expression->value = parse_sub_expression(precedence); \
2583 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2585 return (expression_t*) unary_expression; \
2588 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2589 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2590 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2591 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2592 get_unexpr_dereference_type)
2593 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2594 get_unexpr_take_addr_type)
2595 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2596 get_unexpr_arithmetic_type)
2597 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2598 get_unexpr_arithmetic_type)
2599 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2600 get_unexpr_arithmetic_type)
2602 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2604 static expression_t *parse_##unexpression_type(unsigned precedence, \
2605 expression_t *left) \
2607 (void) precedence; \
2610 unary_expression_t *unary_expression \
2611 = allocate_ast_zero(sizeof(unary_expression[0])); \
2612 unary_expression->expression.type = EXPR_UNARY; \
2613 unary_expression->type = unexpression_type; \
2614 unary_expression->value = left; \
2615 unary_expression->expression.datatype = tfunc(left); \
2617 return (expression_t*) unary_expression; \
2620 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2621 get_unexpr_arithmetic_type)
2622 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2623 get_unexpr_arithmetic_type)
2625 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2627 /* TODO: handle complex + imaginary types */
2629 /* § 6.3.1.8 Usual arithmetic conversions */
2630 if(type_left == type_long_double || type_right == type_long_double) {
2631 return type_long_double;
2632 } else if(type_left == type_double || type_right == type_double) {
2634 } else if(type_left == type_float || type_right == type_float) {
2638 type_right = promote_integer(type_right);
2639 type_left = promote_integer(type_left);
2641 if(type_left == type_right)
2644 bool signed_left = is_type_signed(type_left);
2645 bool signed_right = is_type_signed(type_right);
2646 if(get_rank(type_left) < get_rank(type_right)) {
2647 if(signed_left == signed_right || !signed_right) {
2653 if(signed_left == signed_right || !signed_left) {
2661 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2663 expression_t *left = expression->left;
2664 expression_t *right = expression->right;
2665 type_t *type_left = skip_typeref(left->datatype);
2666 type_t *type_right = skip_typeref(right->datatype);
2668 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2669 /* TODO: improve error message */
2670 parser_print_error_prefix();
2671 fprintf(stderr, "operation needs arithmetic types\n");
2675 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2676 expression->left = create_implicit_cast(left, arithmetic_type);
2677 expression->right = create_implicit_cast(right, arithmetic_type);
2678 expression->expression.datatype = arithmetic_type;
2681 static void semantic_add(binary_expression_t *expression)
2683 expression_t *left = expression->left;
2684 expression_t *right = expression->right;
2685 type_t *orig_type_left = left->datatype;
2686 type_t *orig_type_right = right->datatype;
2687 type_t *type_left = skip_typeref(orig_type_left);
2688 type_t *type_right = skip_typeref(orig_type_right);
2691 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2692 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2693 expression->left = create_implicit_cast(left, arithmetic_type);
2694 expression->right = create_implicit_cast(right, arithmetic_type);
2695 expression->expression.datatype = arithmetic_type;
2697 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2698 expression->expression.datatype = type_left;
2699 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2700 expression->expression.datatype = type_right;
2702 parser_print_error_prefix();
2703 fprintf(stderr, "invalid operands to binary + (");
2704 print_type(orig_type_left);
2705 fprintf(stderr, ", ");
2706 print_type(orig_type_right);
2707 fprintf(stderr, ")\n");
2711 static void semantic_sub(binary_expression_t *expression)
2713 expression_t *left = expression->left;
2714 expression_t *right = expression->right;
2715 type_t *orig_type_left = left->datatype;
2716 type_t *orig_type_right = right->datatype;
2717 type_t *type_left = skip_typeref(orig_type_left);
2718 type_t *type_right = skip_typeref(orig_type_right);
2721 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2722 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2723 expression->left = create_implicit_cast(left, arithmetic_type);
2724 expression->right = create_implicit_cast(right, arithmetic_type);
2725 expression->expression.datatype = arithmetic_type;
2727 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2728 expression->expression.datatype = type_left;
2729 } else if(type_left->type == TYPE_POINTER &&
2730 type_right->type == TYPE_POINTER) {
2731 if(!pointers_compatible(type_left, type_right)) {
2732 parser_print_error_prefix();
2733 fprintf(stderr, "pointers to incompatible objects to binary - (");
2734 print_type(orig_type_left);
2735 fprintf(stderr, ", ");
2736 print_type(orig_type_right);
2737 fprintf(stderr, ")\n");
2739 expression->expression.datatype = type_ptrdiff_t;
2742 parser_print_error_prefix();
2743 fprintf(stderr, "invalid operands to binary - (");
2744 print_type(orig_type_left);
2745 fprintf(stderr, ", ");
2746 print_type(orig_type_right);
2747 fprintf(stderr, ")\n");
2751 static void semantic_comparison(binary_expression_t *expression)
2753 expression_t *left = expression->left;
2754 expression_t *right = expression->right;
2755 type_t *type_left = left->datatype;
2756 type_t *type_right = right->datatype;
2758 /* TODO non-arithmetic types */
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 expression->expression.datatype = type_int;
2768 static void semantic_arithmetic_assign(binary_expression_t *expression)
2770 expression_t *left = expression->left;
2771 expression_t *right = expression->right;
2772 type_t *type_left = left->datatype;
2773 type_t *type_right = right->datatype;
2775 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2776 /* TODO: improve error message */
2777 parser_print_error_prefix();
2778 fprintf(stderr, "operation needs arithmetic types\n");
2782 /* combined instructions are tricky. We can't create an implicit cast on
2783 * the left side, because we need the uncasted form for the store.
2784 * The ast2firm pass has to know that left_type must be right_type
2785 * for the arithmeitc operation and create a cast by itself */
2786 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2787 expression->right = create_implicit_cast(right, arithmetic_type);
2788 expression->expression.datatype = type_left;
2791 static void semantic_logical_op(binary_expression_t *expression)
2794 expression->expression.datatype = type_int;
2797 static void semantic_binexpr_assign(binary_expression_t *expression)
2799 expression_t *left = expression->left;
2800 type_t *type_left = left->datatype;
2802 semantic_assign(type_left, &expression->right, "assignment");
2804 expression->expression.datatype = type_left;
2807 static void semantic_comma(binary_expression_t *expression)
2809 expression->expression.datatype = expression->right->datatype;
2812 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2813 static expression_t *parse_##binexpression_type(unsigned precedence, \
2814 expression_t *left) \
2818 expression_t *right = parse_sub_expression(precedence + lr); \
2820 binary_expression_t *binexpr \
2821 = allocate_ast_zero(sizeof(binexpr[0])); \
2822 binexpr->expression.type = EXPR_BINARY; \
2823 binexpr->type = binexpression_type; \
2824 binexpr->left = left; \
2825 binexpr->right = right; \
2828 return (expression_t*) binexpr; \
2831 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
2832 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
2833 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
2834 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
2835 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
2836 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
2837 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
2838 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
2839 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
2840 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
2841 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2842 semantic_comparison, 1)
2843 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
2844 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2845 semantic_comparison, 1)
2846 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
2847 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
2848 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
2849 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
2850 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
2851 /* TODO shift has a bit special semantic */
2852 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2853 semantic_binexpr_arithmetic, 1)
2854 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2855 semantic_binexpr_arithmetic, 1)
2856 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2857 semantic_arithmetic_assign, 0)
2858 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2859 semantic_arithmetic_assign, 0)
2860 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2861 semantic_arithmetic_assign, 0)
2862 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2863 semantic_arithmetic_assign, 0)
2864 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2865 semantic_arithmetic_assign, 0)
2866 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2867 semantic_arithmetic_assign, 0)
2868 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2869 semantic_arithmetic_assign, 0)
2870 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2871 semantic_arithmetic_assign, 0)
2872 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2873 semantic_arithmetic_assign, 0)
2874 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2875 semantic_arithmetic_assign, 0)
2877 static expression_t *parse_sub_expression(unsigned precedence)
2879 if(token.type < 0) {
2880 return expected_expression_error();
2883 expression_parser_function_t *parser
2884 = &expression_parsers[token.type];
2885 source_position_t source_position = token.source_position;
2888 if(parser->parser != NULL) {
2889 left = parser->parser(parser->precedence);
2891 left = parse_primary_expression();
2893 assert(left != NULL);
2894 left->source_position = source_position;
2897 if(token.type < 0) {
2898 return expected_expression_error();
2901 parser = &expression_parsers[token.type];
2902 if(parser->infix_parser == NULL)
2904 if(parser->infix_precedence < precedence)
2907 left = parser->infix_parser(parser->infix_precedence, left);
2909 assert(left != NULL);
2910 assert(left->type != EXPR_INVALID);
2911 left->source_position = source_position;
2917 static expression_t *parse_expression(void)
2919 return parse_sub_expression(1);
2924 static void register_expression_parser(parse_expression_function parser,
2925 int token_type, unsigned precedence)
2927 expression_parser_function_t *entry = &expression_parsers[token_type];
2929 if(entry->parser != NULL) {
2930 fprintf(stderr, "for token ");
2931 print_token_type(stderr, token_type);
2932 fprintf(stderr, "\n");
2933 panic("trying to register multiple expression parsers for a token");
2935 entry->parser = parser;
2936 entry->precedence = precedence;
2939 static void register_expression_infix_parser(
2940 parse_expression_infix_function parser, int token_type,
2941 unsigned precedence)
2943 expression_parser_function_t *entry = &expression_parsers[token_type];
2945 if(entry->infix_parser != NULL) {
2946 fprintf(stderr, "for token ");
2947 print_token_type(stderr, token_type);
2948 fprintf(stderr, "\n");
2949 panic("trying to register multiple infix expression parsers for a "
2952 entry->infix_parser = parser;
2953 entry->infix_precedence = precedence;
2956 static void init_expression_parsers(void)
2958 memset(&expression_parsers, 0, sizeof(expression_parsers));
2960 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2961 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2962 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2963 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2964 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2965 T_GREATERGREATER, 16);
2966 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2967 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2968 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2969 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2970 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2971 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2972 T_GREATEREQUAL, 14);
2973 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2974 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2975 T_EXCLAMATIONMARKEQUAL, 13);
2976 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2977 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2978 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2979 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2980 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2981 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2982 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2983 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2984 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2985 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2986 T_ASTERISKEQUAL, 2);
2987 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2988 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2990 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2991 T_LESSLESSEQUAL, 2);
2992 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2993 T_GREATERGREATEREQUAL, 2);
2994 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2996 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2998 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3001 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3003 register_expression_infix_parser(parse_array_expression, '[', 30);
3004 register_expression_infix_parser(parse_call_expression, '(', 30);
3005 register_expression_infix_parser(parse_select_expression, '.', 30);
3006 register_expression_infix_parser(parse_select_expression,
3007 T_MINUSGREATER, 30);
3008 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3010 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3013 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3014 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3015 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3016 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3017 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3018 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3019 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3020 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3021 register_expression_parser(parse_sizeof, T_sizeof, 25);
3022 register_expression_parser(parse_extension, T___extension__, 25);
3026 static statement_t *parse_case_statement(void)
3029 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3030 label->statement.type = STATEMENT_CASE_LABEL;
3031 label->statement.source_position = token.source_position;
3033 label->expression = parse_expression();
3036 label->statement.next = parse_statement();
3038 return (statement_t*) label;
3041 static statement_t *parse_default_statement(void)
3045 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3046 label->statement.type = STATEMENT_CASE_LABEL;
3047 label->statement.source_position = token.source_position;
3050 label->statement.next = parse_statement();
3052 return (statement_t*) label;
3055 static statement_t *parse_label_statement(void)
3064 static statement_t *parse_if(void)
3068 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3069 statement->statement.type = STATEMENT_IF;
3070 statement->statement.source_position = token.source_position;
3073 statement->condition = parse_expression();
3076 statement->true_statement = parse_statement();
3077 if(token.type == T_else) {
3079 statement->false_statement = parse_statement();
3082 return (statement_t*) statement;
3085 static statement_t *parse_switch(void)
3089 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3090 statement->statement.type = STATEMENT_SWITCH;
3091 statement->statement.source_position = token.source_position;
3094 statement->expression = parse_expression();
3096 statement->body = parse_statement();
3098 return (statement_t*) statement;
3101 static statement_t *parse_while(void)
3105 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3106 statement->statement.type = STATEMENT_WHILE;
3107 statement->statement.source_position = token.source_position;
3110 statement->condition = parse_expression();
3112 statement->body = parse_statement();
3114 return (statement_t*) statement;
3117 static statement_t *parse_do(void)
3121 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3122 statement->statement.type = STATEMENT_DO_WHILE;
3123 statement->statement.source_position = token.source_position;
3125 statement->body = parse_statement();
3128 statement->condition = parse_expression();
3132 return (statement_t*) statement;
3135 static statement_t *parse_for(void)
3139 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3140 statement->statement.type = STATEMENT_FOR;
3141 statement->statement.source_position = token.source_position;
3145 int top = environment_top();
3146 context_t *last_context = context;
3147 set_context(&statement->context);
3149 if(token.type != ';') {
3150 if(is_declaration_specifier(&token, false)) {
3151 parse_declaration();
3153 statement->initialisation = parse_expression();
3160 if(token.type != ';') {
3161 statement->condition = parse_expression();
3164 if(token.type != ')') {
3165 statement->step = parse_expression();
3168 statement->body = parse_statement();
3170 assert(context == &statement->context);
3171 set_context(last_context);
3172 environment_pop_to(top);
3174 return (statement_t*) statement;
3177 static statement_t *parse_goto(void)
3180 expect(T_IDENTIFIER);
3186 static statement_t *parse_continue(void)
3191 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3192 statement->source_position = token.source_position;
3193 statement->type = STATEMENT_CONTINUE;
3198 static statement_t *parse_break(void)
3203 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3204 statement->source_position = token.source_position;
3205 statement->type = STATEMENT_BREAK;
3210 static statement_t *parse_return(void)
3214 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3216 statement->statement.type = STATEMENT_RETURN;
3217 statement->statement.source_position = token.source_position;
3219 assert(current_function->type->type == TYPE_FUNCTION);
3220 function_type_t *function_type = (function_type_t*) current_function->type;
3221 type_t *return_type = function_type->result_type;
3223 expression_t *return_value;
3224 if(token.type != ';') {
3225 return_value = parse_expression();
3227 if(return_type == type_void && return_value->datatype != type_void) {
3228 parse_warning("'return' with a value, in function returning void");
3229 return_value = NULL;
3231 semantic_assign(return_type, &return_value, "'return'");
3234 return_value = NULL;
3235 if(return_type != type_void) {
3236 parse_warning("'return' without value, in function returning "
3240 statement->return_value = return_value;
3244 return (statement_t*) statement;
3247 static statement_t *parse_declaration_statement(void)
3249 declaration_t *before = last_declaration;
3251 declaration_statement_t *statement
3252 = allocate_ast_zero(sizeof(statement[0]));
3253 statement->statement.type = STATEMENT_DECLARATION;
3254 statement->statement.source_position = token.source_position;
3256 declaration_specifiers_t specifiers;
3257 memset(&specifiers, 0, sizeof(specifiers));
3258 parse_declaration_specifiers(&specifiers);
3260 if(token.type == ';') {
3263 parse_init_declarators(&specifiers);
3266 if(before == NULL) {
3267 statement->declarations_begin = context->declarations;
3269 statement->declarations_begin = before->next;
3271 statement->declarations_end = last_declaration;
3273 return (statement_t*) statement;
3276 static statement_t *parse_expression_statement(void)
3278 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3279 statement->statement.type = STATEMENT_EXPRESSION;
3280 statement->statement.source_position = token.source_position;
3282 statement->expression = parse_expression();
3286 return (statement_t*) statement;
3289 static statement_t *parse_statement(void)
3291 statement_t *statement = NULL;
3293 /* declaration or statement */
3294 switch(token.type) {
3296 statement = parse_case_statement();
3300 statement = parse_default_statement();
3304 statement = parse_compound_statement();
3308 statement = parse_if();
3312 statement = parse_switch();
3316 statement = parse_while();
3320 statement = parse_do();
3324 statement = parse_for();
3328 statement = parse_goto();
3332 statement = parse_continue();
3336 statement = parse_break();
3340 statement = parse_return();
3349 if(look_ahead(1)->type == ':') {
3350 statement = parse_label_statement();
3354 if(is_typedef_symbol(token.v.symbol)) {
3355 statement = parse_declaration_statement();
3359 statement = parse_expression_statement();
3362 case T___extension__:
3363 /* this can be a prefix to a declaration or an expression statement */
3364 /* we simply eat it now and parse the rest with tail recursion */
3367 } while(token.type == T___extension__);
3368 statement = parse_statement();
3372 statement = parse_declaration_statement();
3376 statement = parse_expression_statement();
3380 assert(statement == NULL || statement->source_position.input_name != NULL);
3385 static statement_t *parse_compound_statement(void)
3389 compound_statement_t *compound_statement
3390 = allocate_ast_zero(sizeof(compound_statement[0]));
3391 compound_statement->statement.type = STATEMENT_COMPOUND;
3392 compound_statement->statement.source_position = token.source_position;
3394 int top = environment_top();
3395 context_t *last_context = context;
3396 set_context(&compound_statement->context);
3398 statement_t *last_statement = NULL;
3400 while(token.type != '}' && token.type != T_EOF) {
3401 statement_t *statement = parse_statement();
3402 if(statement == NULL)
3405 if(last_statement != NULL) {
3406 last_statement->next = statement;
3408 compound_statement->statements = statement;
3411 while(statement->next != NULL)
3412 statement = statement->next;
3414 last_statement = statement;
3417 assert(context == &compound_statement->context);
3418 set_context(last_context);
3419 environment_pop_to(top);
3423 return (statement_t*) compound_statement;
3426 static translation_unit_t *parse_translation_unit(void)
3428 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3430 assert(global_context == NULL);
3431 global_context = &unit->context;
3433 assert(context == NULL);
3434 set_context(&unit->context);
3436 while(token.type != T_EOF) {
3437 parse_declaration();
3440 assert(context == &unit->context);
3442 last_declaration = NULL;
3444 assert(global_context == &unit->context);
3445 global_context = NULL;
3450 translation_unit_t *parse(void)
3452 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3453 found_error = false;
3455 type_set_output(stderr);
3456 ast_set_output(stderr);
3458 lookahead_bufpos = 0;
3459 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3462 translation_unit_t *unit = parse_translation_unit();
3464 DEL_ARR_F(environment_stack);
3472 void init_parser(void)
3474 init_expression_parsers();
3475 obstack_init(&temp_obst);
3477 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3478 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3479 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3480 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3481 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3482 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3483 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3484 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3485 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3486 type_string = make_pointer_type(type_const_char, 0);
3489 void exit_parser(void)
3491 obstack_free(&temp_obst, NULL);