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
22 struct environment_entry_t {
24 declaration_t *old_declaration;
25 const void *old_context;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static struct obstack environment_obstack;
32 static environment_entry_t **environment_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static struct obstack temp_obst;
38 static type_t *type_int = NULL;
39 static type_t *type_double = NULL;
40 static type_t *type_const_char = NULL;
41 static type_t *type_string = NULL;
42 static type_t *type_void = NULL;
43 static type_t *type_size_t = NULL;
45 static statement_t *parse_compound_statement(void);
46 static statement_t *parse_statement(void);
48 static expression_t *parse_sub_expression(unsigned precedence);
49 static expression_t *parse_expression(void);
50 static type_t *parse_typename(void);
52 #define STORAGE_CLASSES \
59 #define TYPE_QUALIFIERS \
65 #ifdef PROVIDE_COMPLEX
66 #define COMPLEX_SPECIFIERS \
69 #define COMPLEX_SPECIFIERS
72 #ifdef PROVIDE_IMAGINARY
73 #define IMAGINARY_SPECIFIERS \
76 #define IMAGINARY_SPECIFIERS
79 #define TYPE_SPECIFIERS \
97 #define DECLARATION_START \
102 #define TYPENAME_START \
106 static inline void *allocate_ast_zero(size_t size)
108 void *res = allocate_ast(size);
109 memset(res, 0, size);
113 static inline void *allocate_type_zero(size_t size)
115 void *res = obstack_alloc(type_obst, size);
116 memset(res, 0, size);
121 * returns the top element of the environment stack
123 static inline size_t environment_top(void)
125 return ARR_LEN(environment_stack);
130 static inline void next_token(void)
132 token = lookahead_buffer[lookahead_bufpos];
133 lookahead_buffer[lookahead_bufpos] = lexer_token;
136 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
139 print_token(stderr, &token);
140 fprintf(stderr, "\n");
144 static inline const token_t *look_ahead(int num)
146 assert(num > 0 && num <= MAX_LOOKAHEAD);
147 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
148 return & lookahead_buffer[pos];
151 static inline void eat(token_type_t type)
153 assert(token.type == type);
159 #ifdef ABORT_ON_ERROR
164 void parser_print_prefix_pos(const source_position_t source_position)
166 fputs(source_position.input_name, stderr);
168 fprintf(stderr, "%d", source_position.linenr);
172 void parser_print_error_prefix_pos(const source_position_t source_position)
174 parser_print_prefix_pos(source_position);
175 fputs("error: ", stderr);
179 void parser_print_error_prefix(void)
181 parser_print_prefix_pos(token.source_position);
185 static void parse_error(const char *message)
187 parser_print_error_prefix();
188 fprintf(stderr, "parse error: %s\n", message);
191 __attribute__((unused))
192 static void parse_warning(const char *message)
194 parser_print_prefix_pos(token.source_position);
195 fprintf(stderr, "warning: %s\n", message);
198 static void parse_error_expected(const char *message, ...)
203 if(message != NULL) {
204 parser_print_error_prefix();
205 fprintf(stderr, "%s\n", message);
207 parser_print_error_prefix();
208 fputs("Parse error: got ", stderr);
209 print_token(stderr, &token);
210 fputs(", expected ", stderr);
212 va_start(args, message);
213 token_type_t token_type = va_arg(args, token_type_t);
214 while(token_type != 0) {
218 fprintf(stderr, ", ");
220 print_token_type(stderr, token_type);
221 token_type = va_arg(args, token_type_t);
224 fprintf(stderr, "\n");
227 static void eat_block(void)
229 if(token.type == '{')
232 while(token.type != '}') {
233 if(token.type == T_EOF)
235 if(token.type == '{') {
244 static void eat_statement(void)
246 while(token.type != ';') {
247 if(token.type == T_EOF)
249 if(token.type == '}')
251 if(token.type == '{') {
260 static void eat_brace(void)
262 if(token.type == '(')
265 while(token.type != ')') {
266 if(token.type == T_EOF)
268 if(token.type == ')' || token.type == ';' || token.type == '}') {
271 if(token.type == '(') {
275 if(token.type == '{') {
284 #define expect(expected) \
285 if(UNLIKELY(token.type != (expected))) { \
286 parse_error_expected(NULL, (expected), 0); \
292 #define expect_void(expected) \
293 if(UNLIKELY(token.type != (expected))) { \
294 parse_error_expected(NULL, (expected), 0); \
300 static void set_context(context_t *new_context)
302 context = new_context;
304 declaration_t *declaration = new_context->declarations;
305 if(declaration != NULL) {
307 if(declaration->next == NULL)
309 declaration = declaration->next;
313 last_declaration = declaration;
317 * called when we find a 2nd declarator for an identifier we already have a
320 static bool is_compatible_declaration (declaration_t *declaration,
321 declaration_t *previous)
323 /* TODO: not correct yet */
324 return declaration->type == previous->type;
328 * pushs an environment_entry on the environment stack and links the
329 * corresponding symbol to the new entry
331 static inline declaration_t *environment_push(declaration_t *declaration,
334 symbol_t *symbol = declaration->symbol;
335 assert(declaration != symbol->declaration);
336 assert(declaration->source_position.input_name != NULL);
338 if(symbol->context == context) {
339 declaration_t *previous_declaration = symbol->declaration;
340 if(symbol->declaration != NULL) {
341 if(!is_compatible_declaration(declaration, previous_declaration)) {
342 parser_print_error_prefix_pos(declaration->source_position);
343 fprintf(stderr, "definition of symbol '%s' with type ",
344 declaration->symbol->string);
346 print_type(declaration->type);
348 parser_print_error_prefix_pos(
349 previous_declaration->source_position);
350 fprintf(stderr, "is incompatible with previous declaration "
352 print_type(previous_declaration->type);
355 return previous_declaration;
359 environment_entry_t *entry
360 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
361 memset(entry, 0, sizeof(entry[0]));
363 int top = ARR_LEN(environment_stack);
364 ARR_RESIZE(environment_stack, top + 1);
365 environment_stack[top] = entry;
367 entry->old_declaration = symbol->declaration;
368 entry->old_context = symbol->context;
369 entry->symbol = symbol;
370 symbol->declaration = declaration;
371 symbol->context = context;
377 * pops symbols from the environment stack until @p new_top is the top element
379 static inline void environment_pop_to(size_t new_top)
381 environment_entry_t *entry = NULL;
382 size_t top = ARR_LEN(environment_stack);
388 assert(new_top < top);
391 entry = environment_stack[i - 1];
393 symbol_t *symbol = entry->symbol;
395 symbol->declaration = entry->old_declaration;
396 symbol->context = entry->old_context;
399 } while(i != new_top);
400 obstack_free(&environment_obstack, entry);
402 ARR_SHRINKLEN(environment_stack, (int) new_top);
407 static expression_t *parse_constant_expression(void)
409 /* start parsing at precedence 7 (conditional expression) */
410 return parse_sub_expression(7);
413 static expression_t *parse_assignment_expression(void)
415 /* start parsing at precedence 2 (assignment expression) */
416 return parse_sub_expression(2);
419 static void parse_compound_type_entries(void);
420 static void parse_declarator(declaration_t *declaration,
421 storage_class_t storage_class, type_t *type,
422 int may_be_abstract);
423 static declaration_t *record_declaration(declaration_t *declaration);
425 typedef struct declaration_specifiers_t declaration_specifiers_t;
426 struct declaration_specifiers_t {
427 storage_class_t storage_class;
431 static compound_type_t *find_compound_type(compound_type_t *types,
432 const symbol_t *symbol)
434 compound_type_t *type = types;
435 for( ; type != NULL; type = type->next) {
436 if(type->symbol == symbol)
443 static type_t *parse_compound_type_specifier(bool is_struct)
451 symbol_t *symbol = NULL;
452 compound_type_t *compound_type = NULL;
454 if(token.type == T_IDENTIFIER) {
455 symbol = token.v.symbol;
458 if(context != NULL) {
460 compound_type = find_compound_type(context->structs, symbol);
462 compound_type = find_compound_type(context->unions, symbol);
465 } else if(token.type != '{') {
467 parse_error_expected("problem while parsing struct type specifier",
468 T_IDENTIFIER, '{', 0);
470 parse_error_expected("problem while parsing union type specifier",
471 T_IDENTIFIER, '{', 0);
477 if(compound_type == NULL) {
478 compound_type = allocate_type_zero(sizeof(compound_type[0]));
481 compound_type->type.type = TYPE_COMPOUND_STRUCT;
483 compound_type->type.type = TYPE_COMPOUND_UNION;
485 compound_type->source_position = token.source_position;
486 compound_type->symbol = symbol;
489 if(token.type == '{') {
490 if(compound_type->defined) {
491 parser_print_error_prefix();
492 fprintf(stderr, "multiple definition of %s %s\n",
493 is_struct ? "struct" : "union", symbol->string);
494 compound_type->context.declarations = NULL;
496 compound_type->defined = 1;
498 int top = environment_top();
499 context_t *last_context = context;
500 set_context(&compound_type->context);
502 parse_compound_type_entries();
504 assert(context == &compound_type->context);
505 set_context(last_context);
506 environment_pop_to(top);
509 return (type_t*) compound_type;
512 static void parse_enum_entries(void)
516 if(token.type == '}') {
518 parse_error("empty enum not allowed");
523 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
525 if(token.type != T_IDENTIFIER) {
526 parse_error_expected("problem while parsing enum entry",
531 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
532 entry->symbol = token.v.symbol;
533 entry->source_position = token.source_position;
536 if(token.type == '=') {
538 entry->initializer = parse_constant_expression();
541 record_declaration(entry);
543 if(token.type != ',')
546 } while(token.type != '}');
551 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
553 enum_type_t *type = types;
554 for( ; type != NULL; type = type->next) {
555 if(type->symbol == symbol)
562 static type_t *parse_enum_specifier(void)
566 symbol_t *symbol = NULL;
567 enum_type_t *enum_type = NULL;
569 if(token.type == T_IDENTIFIER) {
570 symbol = token.v.symbol;
573 if(context != NULL) {
574 enum_type = find_enum_type(context->enums, symbol);
576 } else if(token.type != '{') {
577 parse_error_expected("problem while parsing enum type specifier",
578 T_IDENTIFIER, '{', 0);
582 if(enum_type == NULL) {
583 enum_type = allocate_type_zero(sizeof(enum_type[0]));
584 enum_type->type.type = TYPE_ENUM;
585 enum_type->source_position = token.source_position;
586 enum_type->symbol = symbol;
589 if(token.type == '{') {
590 if(enum_type->defined) {
591 parser_print_error_prefix();
592 fprintf(stderr, "multiple definitions of enum %s\n",
594 enum_type->entries_begin = NULL;
595 enum_type->entries_end = NULL;
597 enum_type->defined = 1;
599 declaration_t *before = last_declaration;
601 parse_enum_entries();
604 enum_type->entries_begin = context->declarations;
606 enum_type->entries_begin = before->next;
608 enum_type->entries_end = last_declaration;
611 return (type_t*) enum_type;
614 static type_t *parse_typeof(void)
622 declaration_t *declaration;
623 expression_t *expression;
627 case T___extension__:
628 /* this can be a prefix to a typename or an expression */
629 /* we simply eat it now. */
632 } while(token.type == T___extension__);
636 declaration = token.v.symbol->declaration;
637 if(declaration != NULL
638 && declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
639 result = parse_typename();
642 expression = parse_expression();
643 result = expression->datatype;
647 result = parse_typename();
651 expression = parse_expression();
652 result = expression->datatype;
661 static const char *parse_string_literals(void)
663 assert(token.type == T_STRING_LITERAL);
664 const char *result = token.v.string;
668 while(token.type == T_STRING_LITERAL) {
669 result = concat_strings(result, token.v.string);
676 static void parse_attributes(void)
680 case T___attribute__:
688 parse_error("EOF while parsing attribute");
706 if(token.type != T_STRING_LITERAL) {
707 parse_error_expected("while parsing assembler attribute",
712 parse_string_literals();
717 goto attributes_finished;
726 SPECIFIER_SIGNED = 1 << 0,
727 SPECIFIER_UNSIGNED = 1 << 1,
728 SPECIFIER_LONG = 1 << 2,
729 SPECIFIER_INT = 1 << 3,
730 SPECIFIER_DOUBLE = 1 << 4,
731 SPECIFIER_CHAR = 1 << 5,
732 SPECIFIER_SHORT = 1 << 6,
733 SPECIFIER_LONG_LONG = 1 << 7,
734 SPECIFIER_FLOAT = 1 << 8,
735 SPECIFIER_BOOL = 1 << 9,
736 SPECIFIER_VOID = 1 << 10,
737 #ifdef PROVIDE_COMPLEX
738 SPECIFIER_COMPLEX = 1 << 11,
740 #ifdef PROVIDE_IMAGINARY
741 SPECIFIER_IMAGINARY = 1 << 12,
745 static type_t *create_builtin_type(symbol_t *symbol)
747 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
748 type->type.type = TYPE_BUILTIN;
749 type->symbol = symbol;
751 type_t *result = typehash_insert((type_t*) type);
752 if(result != (type_t*) type) {
753 obstack_free(type_obst, type);
759 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
761 declaration_t *declaration;
763 unsigned type_qualifiers = 0;
764 unsigned type_specifiers = 0;
771 #define MATCH_STORAGE_CLASS(token, class) \
773 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
774 parse_error("multiple storage classes in declaration " \
777 specifiers->storage_class = class; \
781 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
782 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
783 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
784 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
785 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
787 /* type qualifiers */
788 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
790 type_qualifiers |= qualifier; \
794 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
795 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
796 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
797 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
799 case T___extension__:
804 /* type specifiers */
805 #define MATCH_SPECIFIER(token, specifier, name) \
808 if(type_specifiers & specifier) { \
809 parse_error("multiple " name " type specifiers given"); \
811 type_specifiers |= specifier; \
815 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
816 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
817 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
818 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
819 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
820 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
821 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
822 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
823 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
824 #ifdef PROVIDE_COMPLEX
825 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
827 #ifdef PROVIDE_IMAGINARY
828 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
832 if(type_specifiers & SPECIFIER_LONG_LONG) {
833 parse_error("multiple type specifiers given");
834 } else if(type_specifiers & SPECIFIER_LONG) {
835 type_specifiers |= SPECIFIER_LONG_LONG;
837 type_specifiers |= SPECIFIER_LONG;
841 /* TODO: if type != NULL for the following rules issue an error */
843 type = parse_compound_type_specifier(true);
846 type = parse_compound_type_specifier(false);
849 type = parse_enum_specifier();
852 type = parse_typeof();
854 case T___builtin_va_list:
855 type = create_builtin_type(token.v.symbol);
859 case T___attribute__:
865 declaration = token.v.symbol->declaration;
866 if(declaration == NULL ||
867 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
868 goto finish_specifiers;
871 type = declaration->type;
872 assert(type != NULL);
876 /* function specifier */
878 goto finish_specifiers;
885 atomic_type_type_t atomic_type;
887 /* match valid basic types */
888 switch(type_specifiers) {
890 atomic_type = ATOMIC_TYPE_VOID;
893 atomic_type = ATOMIC_TYPE_CHAR;
895 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
896 atomic_type = ATOMIC_TYPE_SCHAR;
898 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
899 atomic_type = ATOMIC_TYPE_UCHAR;
901 case SPECIFIER_SHORT:
902 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
903 case SPECIFIER_SHORT | SPECIFIER_INT:
904 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
905 atomic_type = ATOMIC_TYPE_SHORT;
907 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
908 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
909 atomic_type = ATOMIC_TYPE_USHORT;
912 case SPECIFIER_SIGNED:
913 case SPECIFIER_SIGNED | SPECIFIER_INT:
914 atomic_type = ATOMIC_TYPE_INT;
916 case SPECIFIER_UNSIGNED:
917 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
918 atomic_type = ATOMIC_TYPE_UINT;
921 case SPECIFIER_SIGNED | SPECIFIER_LONG:
922 case SPECIFIER_LONG | SPECIFIER_INT:
923 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
924 atomic_type = ATOMIC_TYPE_LONG;
926 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
927 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
928 atomic_type = ATOMIC_TYPE_ULONG;
930 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
931 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
932 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
933 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
935 atomic_type = ATOMIC_TYPE_LONGLONG;
937 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
938 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
940 atomic_type = ATOMIC_TYPE_ULONGLONG;
942 case SPECIFIER_FLOAT:
943 atomic_type = ATOMIC_TYPE_FLOAT;
945 case SPECIFIER_DOUBLE:
946 atomic_type = ATOMIC_TYPE_DOUBLE;
948 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
949 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
952 atomic_type = ATOMIC_TYPE_BOOL;
954 #ifdef PROVIDE_COMPLEX
955 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
956 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
958 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
959 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
961 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
962 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
965 #ifdef PROVIDE_IMAGINARY
966 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
967 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
969 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
970 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
972 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
973 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
977 /* invalid specifier combination, give an error message */
978 if(type_specifiers == 0) {
980 parse_warning("no type specifiers in declaration (using int)");
981 atomic_type = ATOMIC_TYPE_INT;
984 parse_error("no type specifiers given in declaration");
986 } else if((type_specifiers & SPECIFIER_SIGNED) &&
987 (type_specifiers & SPECIFIER_UNSIGNED)) {
988 parse_error("signed and unsigned specifiers gives");
989 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
990 parse_error("only integer types can be signed or unsigned");
992 parse_error("multiple datatypes in declaration");
994 atomic_type = ATOMIC_TYPE_INVALID;
997 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
998 atype->type.type = TYPE_ATOMIC;
999 atype->atype = atomic_type;
1002 type = (type_t*) atype;
1004 if(type_specifiers != 0) {
1005 parse_error("multiple datatypes in declaration");
1009 type->qualifiers = type_qualifiers;
1011 type_t *result = typehash_insert(type);
1012 if(newtype && result != (type_t*) type) {
1013 obstack_free(type_obst, type);
1016 specifiers->type = result;
1019 static type_qualifier_t parse_type_qualifiers(void)
1021 type_qualifier_t type_qualifiers = 0;
1024 switch(token.type) {
1025 /* type qualifiers */
1026 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1027 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1028 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1029 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1032 return type_qualifiers;
1037 static void parse_identifier_list(void)
1040 if(token.type != T_IDENTIFIER) {
1041 parse_error_expected("problem while parsing parameter identifier "
1042 "list", T_IDENTIFIER, 0);
1046 if(token.type != ',')
1052 static declaration_t *parse_parameter(void)
1054 declaration_specifiers_t specifiers;
1055 memset(&specifiers, 0, sizeof(specifiers));
1057 parse_declaration_specifiers(&specifiers);
1059 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1060 parse_declarator(declaration, specifiers.storage_class,
1061 specifiers.type, 1);
1066 static declaration_t *parse_parameters(method_type_t *type)
1068 if(token.type == T_IDENTIFIER) {
1069 symbol_t *symbol = token.v.symbol;
1070 declaration_t *declaration = symbol->declaration;
1071 if(declaration == NULL
1072 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1074 parse_identifier_list();
1079 if(token.type == ')') {
1080 type->unspecified_parameters = 1;
1083 if(token.type == T_void && look_ahead(1)->type == ')') {
1088 declaration_t *declarations = NULL;
1089 declaration_t *declaration;
1090 declaration_t *last_declaration = NULL;
1091 method_parameter_t *parameter;
1092 method_parameter_t *last_parameter = NULL;
1095 switch(token.type) {
1099 return declarations;
1102 case T___extension__:
1104 declaration = parse_parameter();
1106 parameter = allocate_type_zero(sizeof(parameter[0]));
1107 parameter->type = declaration->type;
1109 if(last_parameter != NULL) {
1110 last_declaration->next = declaration;
1111 last_parameter->next = parameter;
1113 type->parameters = parameter;
1114 declarations = declaration;
1116 last_parameter = parameter;
1117 last_declaration = declaration;
1121 return declarations;
1123 if(token.type != ',')
1124 return declarations;
1133 } construct_type_type_t;
1135 typedef struct construct_type_t construct_type_t;
1136 struct construct_type_t {
1137 construct_type_type_t type;
1138 construct_type_t *next;
1141 typedef struct parsed_pointer_t parsed_pointer_t;
1142 struct parsed_pointer_t {
1143 construct_type_t construct_type;
1144 type_qualifier_t type_qualifiers;
1147 typedef struct construct_method_type_t construct_method_type_t;
1148 struct construct_method_type_t {
1149 construct_type_t construct_type;
1150 method_type_t *method_type;
1153 typedef struct parsed_array_t parsed_array_t;
1154 struct parsed_array_t {
1155 construct_type_t construct_type;
1156 type_qualifier_t type_qualifiers;
1162 typedef struct construct_base_type_t construct_base_type_t;
1163 struct construct_base_type_t {
1164 construct_type_t construct_type;
1168 static construct_type_t *parse_pointer_declarator(void)
1172 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1173 memset(pointer, 0, sizeof(pointer[0]));
1174 pointer->type_qualifiers = parse_type_qualifiers();
1176 return (construct_type_t*) pointer;
1179 static construct_type_t *parse_array_declarator(void)
1183 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1184 memset(array, 0, sizeof(array[0]));
1186 if(token.type == T_static) {
1187 array->is_static = true;
1191 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1192 if(type_qualifiers != 0) {
1193 if(token.type == T_static) {
1194 array->is_static = true;
1198 array->type_qualifiers = type_qualifiers;
1200 if(token.type == '*' && look_ahead(1)->type == ']') {
1201 array->is_variable = true;
1203 } else if(token.type != ']') {
1204 array->size = parse_assignment_expression();
1209 return (construct_type_t*) array;
1212 static construct_type_t *parse_method_declarator(declaration_t *declaration)
1216 method_type_t *method_type
1217 = allocate_type_zero(sizeof(method_type[0]));
1218 method_type->type.type = TYPE_METHOD;
1220 declaration_t *parameters = parse_parameters(method_type);
1221 if(declaration != NULL) {
1222 declaration->context.declarations = parameters;
1225 construct_method_type_t *construct_method_type =
1226 obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
1227 memset(construct_method_type, 0, sizeof(construct_method_type[0]));
1228 construct_method_type->construct_type.type = CONSTRUCT_METHOD;
1229 construct_method_type->method_type = method_type;
1233 return (construct_type_t*) construct_method_type;
1236 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1237 int may_be_abstract)
1239 construct_type_t *result = NULL;
1240 construct_type_t *last = NULL;
1242 while(token.type == '*') {
1243 construct_type_t *type = parse_pointer_declarator();
1252 /* TODO: find out if this is correct */
1255 construct_type_t *inner_types = NULL;
1257 switch(token.type) {
1259 if(declaration == NULL) {
1260 parse_error("no identifier expected in typename");
1262 declaration->symbol = token.v.symbol;
1263 declaration->source_position = token.source_position;
1269 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1275 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1280 construct_type_t *type;
1281 switch(token.type) {
1283 type = parse_method_declarator(declaration);
1286 type = parse_array_declarator();
1289 goto declarator_finished;
1300 declarator_finished:
1303 if(inner_types != NULL) {
1305 last->next = inner_types;
1307 result = inner_types;
1315 static type_t *construct_declarator_type(construct_type_t *construct_list,
1318 construct_type_t *iter = construct_list;
1319 for( ; iter != NULL; iter = iter->next) {
1320 parsed_pointer_t *parsed_pointer;
1321 parsed_array_t *parsed_array;
1322 construct_method_type_t *construct_method_type;
1323 method_type_t *method_type;
1324 pointer_type_t *pointer_type;
1325 array_type_t *array_type;
1327 switch(iter->type) {
1328 case CONSTRUCT_METHOD:
1329 construct_method_type = (construct_method_type_t*) iter;
1330 method_type = construct_method_type->method_type;
1332 method_type->result_type = type;
1333 type = (type_t*) method_type;
1336 case CONSTRUCT_POINTER:
1337 parsed_pointer = (parsed_pointer_t*) iter;
1338 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1340 pointer_type->type.type = TYPE_POINTER;
1341 pointer_type->points_to = type;
1342 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1343 type = (type_t*) pointer_type;
1346 case CONSTRUCT_ARRAY:
1347 parsed_array = (parsed_array_t*) iter;
1348 array_type = allocate_type_zero(sizeof(array_type[0]));
1350 array_type->type.type = TYPE_ARRAY;
1351 array_type->element_type = type;
1352 array_type->type.qualifiers = parsed_array->type_qualifiers;
1353 array_type->is_static = parsed_array->is_static;
1354 array_type->is_variable = parsed_array->is_variable;
1355 array_type->size = parsed_array->size;
1356 type = (type_t*) array_type;
1360 type_t *hashed_type = typehash_insert((type_t*) type);
1361 if(hashed_type != type) {
1362 obstack_free(type_obst, type);
1370 static void parse_declarator(declaration_t *declaration,
1371 storage_class_t storage_class, type_t *type,
1372 int may_be_abstract)
1374 construct_type_t *construct_type
1375 = parse_inner_declarator(declaration, may_be_abstract);
1377 declaration->type = construct_declarator_type(construct_type, type);
1378 declaration->storage_class = storage_class;
1379 if(construct_type != NULL) {
1380 obstack_free(&temp_obst, construct_type);
1384 static type_t *parse_abstract_declarator(type_t *base_type)
1386 construct_type_t *construct_type
1387 = parse_inner_declarator(NULL, 1);
1389 if(construct_type == NULL)
1392 type_t *result = construct_declarator_type(construct_type, base_type);
1393 obstack_free(&temp_obst, construct_type);
1398 static declaration_t *record_declaration(declaration_t *declaration)
1403 symbol_t *symbol = declaration->symbol;
1404 if(symbol != NULL) {
1405 declaration_t *alias = environment_push(declaration, context);
1406 if(alias != declaration)
1410 if(last_declaration != NULL) {
1411 last_declaration->next = declaration;
1413 context->declarations = declaration;
1415 last_declaration = declaration;
1420 static void parser_error_multiple_definition(declaration_t *previous,
1421 declaration_t *declaration)
1423 parser_print_error_prefix_pos(declaration->source_position);
1424 fprintf(stderr, "multiple definition of symbol '%s'\n",
1425 declaration->symbol->string);
1426 parser_print_error_prefix_pos(previous->source_position);
1427 fprintf(stderr, "this is the location of the previous "
1432 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1435 declaration_t *ndeclaration
1436 = allocate_ast_zero(sizeof(ndeclaration[0]));
1438 parse_declarator(ndeclaration, specifiers->storage_class,
1439 specifiers->type, 0);
1440 declaration_t *declaration = record_declaration(ndeclaration);
1441 if(token.type == '=') {
1444 /* TODO: check that this is an allowed type (esp. no method type) */
1446 if(declaration->initializer != NULL) {
1447 parser_error_multiple_definition(declaration, ndeclaration);
1450 if(token.type == '{') {
1454 declaration->initializer = parse_assignment_expression();
1456 } else if(token.type == '{') {
1457 if(declaration->type->type != TYPE_METHOD) {
1458 parser_print_error_prefix();
1459 fprintf(stderr, "Declarator ");
1460 print_type_ext(declaration->type, declaration->symbol, NULL);
1461 fprintf(stderr, " is not a method type.\n");
1464 if(declaration->initializer != NULL) {
1465 parser_error_multiple_definition(declaration, ndeclaration);
1467 if(ndeclaration != declaration) {
1468 memcpy(&declaration->context, &ndeclaration->context,
1469 sizeof(declaration->context));
1472 int top = environment_top();
1473 context_t *last_context = context;
1474 set_context(&declaration->context);
1476 /* push function parameters */
1477 declaration_t *parameter = declaration->context.declarations;
1478 for( ; parameter != NULL; parameter = parameter->next) {
1479 environment_push(parameter, context);
1482 statement_t *statement = parse_compound_statement();
1484 assert(context == &declaration->context);
1485 set_context(last_context);
1486 environment_pop_to(top);
1488 declaration->statement = statement;
1492 if(token.type != ',')
1499 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1502 if(token.type == ':') {
1504 parse_constant_expression();
1505 /* TODO (bitfields) */
1507 declaration_t *declaration
1508 = allocate_ast_zero(sizeof(declaration[0]));
1509 parse_declarator(declaration, specifiers->storage_class,
1510 specifiers->type, 1);
1512 /* TODO: check for doubled fields */
1513 record_declaration(declaration);
1515 if(token.type == ':') {
1517 parse_constant_expression();
1518 /* TODO (bitfields) */
1522 if(token.type != ',')
1529 static void parse_compound_type_entries(void)
1533 while(token.type != '}' && token.type != T_EOF) {
1534 declaration_specifiers_t specifiers;
1535 memset(&specifiers, 0, sizeof(specifiers));
1536 /* TODO not correct as this allows storage class stuff... but only
1537 * specifiers and qualifiers sould be allowed here */
1538 parse_declaration_specifiers(&specifiers);
1540 parse_struct_declarators(&specifiers);
1542 if(token.type == T_EOF) {
1543 parse_error("unexpected error while parsing struct");
1548 static void parse_declaration(void)
1550 declaration_specifiers_t specifiers;
1551 memset(&specifiers, 0, sizeof(specifiers));
1552 parse_declaration_specifiers(&specifiers);
1554 if(token.type == ';') {
1558 parse_init_declarators(&specifiers);
1561 static type_t *parse_typename(void)
1563 declaration_specifiers_t specifiers;
1564 memset(&specifiers, 0, sizeof(specifiers));
1565 parse_declaration_specifiers(&specifiers);
1566 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1567 /* TODO: improve error message, user does probably not know what a
1568 * storage class is...
1570 parse_error("typename may not have a storage class");
1573 type_t *result = parse_abstract_declarator(specifiers.type);
1581 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1582 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1583 expression_t *left);
1585 typedef struct expression_parser_function_t expression_parser_function_t;
1586 struct expression_parser_function_t {
1587 unsigned precedence;
1588 parse_expression_function parser;
1589 unsigned infix_precedence;
1590 parse_expression_infix_function infix_parser;
1593 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1595 static expression_t *expected_expression_error(void)
1597 parser_print_error_prefix();
1598 fprintf(stderr, "expected expression, got token ");
1599 print_token(stderr, & token);
1600 fprintf(stderr, "\n");
1602 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1603 expression->type = EXPR_INVALID;
1609 static expression_t *parse_string_const(void)
1611 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1613 cnst->expression.type = EXPR_STRING_LITERAL;
1614 cnst->expression.datatype = type_string;
1615 cnst->value = parse_string_literals();
1617 return (expression_t*) cnst;
1620 static expression_t *parse_int_const(void)
1622 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1624 cnst->expression.type = EXPR_CONST;
1625 cnst->expression.datatype = type_int;
1626 cnst->v.int_value = token.v.intvalue;
1630 return (expression_t*) cnst;
1633 static expression_t *parse_float_const(void)
1635 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1637 cnst->expression.type = EXPR_CONST;
1638 cnst->expression.datatype = type_double;
1639 cnst->v.float_value = token.v.floatvalue;
1643 return (expression_t*) cnst;
1646 static declaration_t *create_implicit_function(symbol_t *symbol,
1647 const source_position_t source_position)
1649 method_type_t *method_type = allocate_type_zero(sizeof(method_type));
1651 method_type->type.type = TYPE_METHOD;
1652 method_type->result_type = type_int;
1653 method_type->unspecified_parameters = true;
1655 type_t *type = typehash_insert((type_t*) method_type);
1656 if(type != (type_t*) method_type) {
1657 obstack_free(type_obst, method_type);
1660 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1662 declaration->storage_class = STORAGE_CLASS_EXTERN;
1663 declaration->type = type;
1664 declaration->symbol = symbol;
1665 declaration->source_position = source_position;
1667 /* we have to violate the environment push/pop rules here and assign
1668 * the declaration directly to its symbol and append it to the
1670 assert(symbol->declaration == NULL);
1671 symbol->declaration = declaration;
1672 declaration->next = global_context->declarations;
1673 global_context->declarations = declaration;
1678 static expression_t *parse_reference(void)
1680 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1682 ref->expression.type = EXPR_REFERENCE;
1683 ref->symbol = token.v.symbol;
1685 declaration_t *declaration = ref->symbol->declaration;
1686 source_position_t source_position = token.source_position;
1689 if(declaration == NULL) {
1691 /* an implicitely defined function */
1692 if(token.type == '(') {
1693 parser_print_prefix_pos(token.source_position);
1694 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1695 ref->symbol->string);
1697 declaration = create_implicit_function(ref->symbol,
1702 parser_print_error_prefix();
1703 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1704 return (expression_t*) ref;
1708 ref->declaration = declaration;
1709 ref->expression.datatype = declaration->type;
1711 return (expression_t*) ref;
1714 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1718 /* TODO check if cast is allowed and issue warnings/errors */
1721 static expression_t *parse_cast(void)
1723 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1725 cast->expression.type = EXPR_UNARY;
1726 cast->type = UNEXPR_CAST;
1727 cast->expression.source_position = token.source_position;
1729 type_t *type = parse_typename();
1732 expression_t *value = parse_sub_expression(20);
1734 check_cast_allowed(value, type);
1736 cast->expression.datatype = type;
1737 cast->value = value;
1739 return (expression_t*) cast;
1742 static expression_t *parse_statement_expression(void)
1744 statement_expression_t *expression
1745 = allocate_ast_zero(sizeof(expression[0]));
1746 expression->expression.type = EXPR_STATEMENT;
1747 expression->statement = parse_compound_statement();
1749 /* find last statement and use it's type */
1750 const statement_t *last_statement = NULL;
1751 const statement_t *statement = expression->statement;
1752 for( ; statement != NULL; statement = statement->next) {
1753 last_statement = statement;
1756 if(last_statement->type == STATEMENT_EXPRESSION) {
1757 const expression_statement_t *expression_statement =
1758 (const expression_statement_t*) last_statement;
1759 expression->expression.datatype
1760 = expression_statement->expression->datatype;
1762 expression->expression.datatype = type_void;
1767 return (expression_t*) expression;
1770 static expression_t *parse_brace_expression(void)
1774 declaration_t *declaration;
1775 switch(token.type) {
1777 /* gcc extension: a stement expression */
1778 return parse_statement_expression();
1782 return parse_cast();
1784 declaration = token.v.symbol->declaration;
1785 if(declaration != NULL &&
1786 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1787 return parse_cast();
1791 expression_t *result = parse_expression();
1797 static expression_t *parse_function_keyword(void)
1799 eat(T___FUNCTION__);
1802 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1803 expression->expression.type = EXPR_FUNCTION;
1804 expression->expression.datatype = type_string;
1805 expression->value = "TODO: FUNCTION";
1807 return (expression_t*) expression;
1810 static expression_t *parse_pretty_function_keyword(void)
1812 eat(T___PRETTY_FUNCTION__);
1815 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1816 expression->expression.type = EXPR_PRETTY_FUNCTION;
1817 expression->expression.datatype = type_string;
1818 expression->value = "TODO: PRETTY FUNCTION";
1820 return (expression_t*) expression;
1823 static member_designator_t *parse_member_designators(void)
1825 member_designator_t *result = allocate_ast_zero(sizeof(result[0]));
1827 if(token.type != T_IDENTIFIER) {
1828 parse_error_expected("problem while parsing member designator",
1833 result->symbol = token.v.symbol;
1836 member_designator_t *last_designator = result;
1838 if(token.type == '.') {
1840 if(token.type != T_IDENTIFIER) {
1841 parse_error_expected("problem while parsing member designator",
1846 member_designator_t *designator
1847 = allocate_ast_zero(sizeof(result[0]));
1848 designator->symbol = token.v.symbol;
1851 last_designator->next = designator;
1852 last_designator = designator;
1855 if(token.type == '[') {
1857 member_designator_t *designator
1858 = allocate_ast_zero(sizeof(result[0]));
1859 designator->array_access = parse_expression();
1860 if(designator->array_access == NULL) {
1866 last_designator->next = designator;
1867 last_designator = designator;
1876 static expression_t *parse_offsetof(void)
1878 eat(T___builtin_offsetof);
1880 offsetof_expression_t *expression
1881 = allocate_ast_zero(sizeof(expression[0]));
1882 expression->expression.type = EXPR_OFFSETOF;
1883 expression->expression.datatype = type_size_t;
1886 expression->type = parse_typename();
1888 expression->member_designators = parse_member_designators();
1891 return (expression_t*) expression;
1894 static expression_t *parse_builtin_symbol(void)
1896 builtin_symbol_expression_t *expression
1897 = allocate_ast_zero(sizeof(expression[0]));
1898 expression->expression.type = EXPR_BUILTIN_SYMBOL;
1900 /* TODO: set datatype */
1902 expression->symbol = token.v.symbol;
1906 return (expression_t*) expression;
1909 static expression_t *parse_primary_expression(void)
1911 switch(token.type) {
1913 return parse_int_const();
1914 case T_FLOATINGPOINT:
1915 return parse_float_const();
1916 case T_STRING_LITERAL:
1917 return parse_string_const();
1919 return parse_reference();
1920 case T___FUNCTION__:
1921 return parse_function_keyword();
1922 case T___PRETTY_FUNCTION__:
1923 return parse_pretty_function_keyword();
1924 case T___builtin_offsetof:
1925 return parse_offsetof();
1926 case T___builtin_expect:
1927 case T___builtin_va_start:
1928 case T___builtin_va_arg:
1929 case T___builtin_va_end:
1930 return parse_builtin_symbol();
1933 return parse_brace_expression();
1936 parser_print_error_prefix();
1937 fprintf(stderr, "unexpected token ");
1938 print_token(stderr, &token);
1939 fprintf(stderr, "\n");
1942 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1943 expression->type = EXPR_INVALID;
1944 expression->datatype = type_void;
1949 static expression_t *parse_array_expression(unsigned precedence,
1950 expression_t *array_ref)
1956 array_access_expression_t *array_access
1957 = allocate_ast_zero(sizeof(array_access[0]));
1959 array_access->expression.type = EXPR_ARRAY_ACCESS;
1960 array_access->array_ref = array_ref;
1961 array_access->index = parse_expression();
1963 type_t *array_type = array_ref->datatype;
1964 if(array_type != NULL) {
1965 if(array_type->type == TYPE_POINTER) {
1966 pointer_type_t *pointer = (pointer_type_t*) array_type;
1967 array_access->expression.datatype = pointer->points_to;
1969 parser_print_error_prefix();
1970 fprintf(stderr, "array access on object with non-pointer type ");
1971 print_type(array_type);
1972 fprintf(stderr, "\n");
1976 if(token.type != ']') {
1977 parse_error_expected("Problem while parsing array access", ']', 0);
1978 return (expression_t*) array_access;
1982 return (expression_t*) array_access;
1985 static bool is_declaration_specifier(const token_t *token,
1986 bool only_type_specifiers)
1988 declaration_t *declaration;
1990 switch(token->type) {
1994 declaration = token->v.symbol->declaration;
1995 if(declaration == NULL)
1997 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2002 if(only_type_specifiers)
2011 static expression_t *parse_sizeof(unsigned precedence)
2015 sizeof_expression_t *sizeof_expression
2016 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2017 sizeof_expression->expression.type = EXPR_SIZEOF;
2018 sizeof_expression->expression.datatype = type_size_t;
2020 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2022 sizeof_expression->type = parse_typename();
2025 expression_t *expression = parse_sub_expression(precedence);
2026 sizeof_expression->type = expression->datatype;
2027 sizeof_expression->size_expression = expression;
2030 return (expression_t*) sizeof_expression;
2033 static expression_t *parse_select_expression(unsigned precedence,
2034 expression_t *compound)
2038 assert(token.type == '.' || token.type == T_MINUSGREATER);
2041 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2043 select->expression.type = EXPR_SELECT;
2044 select->compound = compound;
2046 /* TODO: datatype */
2048 if(token.type != T_IDENTIFIER) {
2049 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2050 return (expression_t*) select;
2052 select->symbol = token.v.symbol;
2055 return (expression_t*) select;
2058 static expression_t *parse_call_expression(unsigned precedence,
2059 expression_t *expression)
2062 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2064 call->expression.type = EXPR_CALL;
2065 call->method = expression;
2067 /* parse arguments */
2070 if(token.type != ')') {
2071 call_argument_t *last_argument = NULL;
2074 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2076 argument->expression = parse_assignment_expression();
2077 if(last_argument == NULL) {
2078 call->arguments = argument;
2080 last_argument->next = argument;
2082 last_argument = argument;
2084 if(token.type != ',')
2091 type_t *type = expression->datatype;
2093 /* we can call pointer to function */
2094 if(type->type == TYPE_POINTER) {
2095 pointer_type_t *pointer = (pointer_type_t*) type;
2096 type = pointer->points_to;
2099 if(type == NULL || type->type != TYPE_METHOD) {
2100 parser_print_error_prefix();
2101 fprintf(stderr, "expected a method type for call but found type ");
2102 print_type(expression->datatype);
2103 fprintf(stderr, "\n");
2105 method_type_t *method_type = (method_type_t*) type;
2106 call->expression.datatype = method_type->result_type;
2110 return (expression_t*) call;
2113 static void type_error(const char *msg, const source_position_t source_position,
2116 parser_print_error_prefix_pos(source_position);
2117 fprintf(stderr, "%s, but found type ", msg);
2119 fputc('\n', stderr);
2123 static void type_error_incompatible(const char *msg,
2124 const source_position_t source_position, const type_t *type1,
2125 const type_t *type2)
2127 parser_print_error_prefix_pos(source_position);
2128 fprintf(stderr, "%s, incompatible types: ", msg);
2130 fprintf(stderr, " - ");
2132 fprintf(stderr, ")\n");
2136 static type_t *get_type_after_conversion(const type_t *type1,
2137 const type_t *type2)
2141 return (type_t*) type1;
2144 static expression_t *parse_conditional_expression(unsigned precedence,
2145 expression_t *expression)
2149 conditional_expression_t *conditional
2150 = allocate_ast_zero(sizeof(conditional[0]));
2151 conditional->expression.type = EXPR_CONDITIONAL;
2152 conditional->condition = expression;
2155 type_t *condition_type = conditional->condition->datatype;
2156 if(condition_type != NULL) {
2157 if(!is_type_scalar(condition_type)) {
2158 type_error("expected a scalar type", expression->source_position,
2163 conditional->true_expression = parse_expression();
2165 conditional->false_expression = parse_sub_expression(precedence);
2167 type_t *true_type = conditional->true_expression->datatype;
2168 if(true_type == NULL)
2169 return (expression_t*) conditional;
2170 type_t *false_type = conditional->false_expression->datatype;
2171 if(false_type == NULL)
2172 return (expression_t*) conditional;
2175 if(true_type == false_type) {
2176 conditional->expression.datatype = true_type;
2177 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2178 type_t *result = get_type_after_conversion(true_type, false_type);
2179 /* TODO: create implicit convs if necessary */
2180 conditional->expression.datatype = result;
2181 } else if(true_type->type == TYPE_POINTER &&
2182 false_type->type == TYPE_POINTER &&
2183 true /* TODO compatible points_to types */) {
2185 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2186 || (is_null_ptr_const(false_type) &&
2187 true_type->type == TYPE_POINTER) TODO*/ false) {
2189 } else if(/* 1 is pointer to object type, other is void* */ false) {
2192 type_error_incompatible("problem while parsing conditional",
2193 expression->source_position, true_type,
2197 return (expression_t*) conditional;
2200 static expression_t *parse_extension(unsigned precedence)
2202 eat(T___extension__);
2204 /* TODO enable extensions */
2206 return parse_sub_expression(precedence);
2209 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
2211 expression_t *parse_##unexpression_type(unsigned precedence) \
2215 unary_expression_t *unary_expression \
2216 = allocate_ast_zero(sizeof(unary_expression[0])); \
2217 unary_expression->expression.type = EXPR_UNARY; \
2218 unary_expression->type = unexpression_type; \
2219 unary_expression->value = parse_sub_expression(precedence); \
2221 return (expression_t*) unary_expression; \
2224 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
2225 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
2226 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
2227 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
2228 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
2229 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
2230 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
2231 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
2233 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
2235 expression_t *parse_##unexpression_type(unsigned precedence, \
2236 expression_t *left) \
2238 (void) precedence; \
2241 unary_expression_t *unary_expression \
2242 = allocate_ast_zero(sizeof(unary_expression[0])); \
2243 unary_expression->expression.type = EXPR_UNARY; \
2244 unary_expression->type = unexpression_type; \
2245 unary_expression->value = left; \
2247 return (expression_t*) unary_expression; \
2250 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
2251 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
2253 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
2255 expression_t *parse_##binexpression_type(unsigned precedence, \
2256 expression_t *left) \
2260 expression_t *right = parse_sub_expression(precedence); \
2262 binary_expression_t *binexpr \
2263 = allocate_ast_zero(sizeof(binexpr[0])); \
2264 binexpr->expression.type = EXPR_BINARY; \
2265 binexpr->type = binexpression_type; \
2266 binexpr->left = left; \
2267 binexpr->right = right; \
2269 return (expression_t*) binexpr; \
2272 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2273 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2274 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2275 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2276 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2277 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2278 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2279 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2280 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2281 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2282 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2283 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2284 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2285 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2286 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2287 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2288 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2289 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2290 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2291 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2292 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2293 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2294 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2295 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2296 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2297 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2298 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2299 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2300 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2301 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2303 static expression_t *parse_sub_expression(unsigned precedence)
2305 if(token.type < 0) {
2306 return expected_expression_error();
2309 expression_parser_function_t *parser
2310 = &expression_parsers[token.type];
2311 source_position_t source_position = token.source_position;
2314 if(parser->parser != NULL) {
2315 left = parser->parser(parser->precedence);
2317 left = parse_primary_expression();
2319 assert(left != NULL);
2320 left->source_position = source_position;
2323 if(token.type < 0) {
2324 return expected_expression_error();
2327 parser = &expression_parsers[token.type];
2328 if(parser->infix_parser == NULL)
2330 if(parser->infix_precedence < precedence)
2333 left = parser->infix_parser(parser->infix_precedence, left);
2335 assert(left != NULL);
2336 assert(left->type != EXPR_INVALID);
2337 left->source_position = source_position;
2343 static expression_t *parse_expression(void)
2345 return parse_sub_expression(1);
2350 void register_expression_parser(parse_expression_function parser,
2351 int token_type, unsigned precedence)
2353 expression_parser_function_t *entry = &expression_parsers[token_type];
2355 if(entry->parser != NULL) {
2356 fprintf(stderr, "for token ");
2357 print_token_type(stderr, token_type);
2358 fprintf(stderr, "\n");
2359 panic("trying to register multiple expression parsers for a token");
2361 entry->parser = parser;
2362 entry->precedence = precedence;
2365 void register_expression_infix_parser(parse_expression_infix_function parser,
2366 int token_type, unsigned precedence)
2368 expression_parser_function_t *entry = &expression_parsers[token_type];
2370 if(entry->infix_parser != NULL) {
2371 fprintf(stderr, "for token ");
2372 print_token_type(stderr, token_type);
2373 fprintf(stderr, "\n");
2374 panic("trying to register multiple infix expression parsers for a "
2377 entry->infix_parser = parser;
2378 entry->infix_precedence = precedence;
2381 static void init_expression_parsers(void)
2383 memset(&expression_parsers, 0, sizeof(expression_parsers));
2385 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2386 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2387 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2388 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2389 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2390 T_GREATERGREATER, 16);
2391 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2392 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2393 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2394 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2395 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2396 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2397 T_GREATEREQUAL, 14);
2398 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2399 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2400 T_EXCLAMATIONMARKEQUAL, 13);
2401 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2402 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2403 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2404 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2405 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2406 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2407 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2408 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2409 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2410 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2411 T_ASTERISKEQUAL, 2);
2412 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2413 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2415 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2416 T_LESSLESSEQUAL, 2);
2417 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2418 T_GREATERGREATEREQUAL, 2);
2419 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2421 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2423 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2426 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2428 register_expression_infix_parser(parse_array_expression, '[', 30);
2429 register_expression_infix_parser(parse_call_expression, '(', 30);
2430 register_expression_infix_parser(parse_select_expression, '.', 30);
2431 register_expression_infix_parser(parse_select_expression,
2432 T_MINUSGREATER, 30);
2433 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2435 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2438 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2439 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2440 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2441 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2442 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2443 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2444 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2445 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2446 register_expression_parser(parse_sizeof, T_sizeof, 25);
2447 register_expression_parser(parse_extension, T___extension__, 25);
2451 static statement_t *parse_case_statement(void)
2454 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2455 label->statement.type = STATEMENT_CASE_LABEL;
2456 label->statement.source_position = token.source_position;
2458 label->expression = parse_expression();
2461 label->statement.next = parse_statement();
2463 return (statement_t*) label;
2466 static statement_t *parse_default_statement(void)
2470 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2471 label->statement.type = STATEMENT_CASE_LABEL;
2472 label->statement.source_position = token.source_position;
2475 label->statement.next = parse_statement();
2477 return (statement_t*) label;
2480 static statement_t *parse_label_statement(void)
2489 static statement_t *parse_if(void)
2493 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2494 statement->statement.type = STATEMENT_IF;
2495 statement->statement.source_position = token.source_position;
2498 statement->condition = parse_expression();
2501 statement->true_statement = parse_statement();
2502 if(token.type == T_else) {
2504 statement->false_statement = parse_statement();
2507 return (statement_t*) statement;
2510 static statement_t *parse_switch(void)
2514 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2515 statement->statement.type = STATEMENT_SWITCH;
2516 statement->statement.source_position = token.source_position;
2519 statement->expression = parse_expression();
2521 statement->body = parse_statement();
2523 return (statement_t*) statement;
2526 static statement_t *parse_while(void)
2530 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2531 statement->statement.type = STATEMENT_WHILE;
2532 statement->statement.source_position = token.source_position;
2535 statement->condition = parse_expression();
2537 statement->body = parse_statement();
2539 return (statement_t*) statement;
2542 static statement_t *parse_do(void)
2546 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2547 statement->statement.type = STATEMENT_DO_WHILE;
2548 statement->statement.source_position = token.source_position;
2550 statement->body = parse_statement();
2553 statement->condition = parse_expression();
2557 return (statement_t*) statement;
2560 static statement_t *parse_for(void)
2564 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2565 statement->statement.type = STATEMENT_FOR;
2566 statement->statement.source_position = token.source_position;
2570 int top = environment_top();
2571 context_t *last_context = context;
2572 set_context(&statement->context);
2574 if(token.type != ';') {
2575 if(is_declaration_specifier(&token, false)) {
2576 parse_declaration();
2578 statement->initialisation = parse_expression();
2585 if(token.type != ';') {
2586 statement->condition = parse_expression();
2589 if(token.type != ')') {
2590 statement->step = parse_expression();
2593 statement->body = parse_statement();
2595 assert(context == &statement->context);
2596 set_context(last_context);
2597 environment_pop_to(top);
2599 return (statement_t*) statement;
2602 static statement_t *parse_goto(void)
2605 expect(T_IDENTIFIER);
2611 static statement_t *parse_continue(void)
2616 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2617 statement->source_position = token.source_position;
2618 statement->type = STATEMENT_CONTINUE;
2623 static statement_t *parse_break(void)
2628 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2629 statement->source_position = token.source_position;
2630 statement->type = STATEMENT_BREAK;
2635 static statement_t *parse_return(void)
2639 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2641 statement->statement.type = STATEMENT_RETURN;
2642 statement->statement.source_position = token.source_position;
2643 if(token.type != ';') {
2644 statement->return_value = parse_expression();
2648 return (statement_t*) statement;
2651 static statement_t *parse_declaration_statement(void)
2653 declaration_t *before = last_declaration;
2655 declaration_statement_t *statement
2656 = allocate_ast_zero(sizeof(statement[0]));
2657 statement->statement.type = STATEMENT_DECLARATION;
2658 statement->statement.source_position = token.source_position;
2660 declaration_specifiers_t specifiers;
2661 memset(&specifiers, 0, sizeof(specifiers));
2662 parse_declaration_specifiers(&specifiers);
2664 if(token.type == ';') {
2667 parse_init_declarators(&specifiers);
2670 if(before == NULL) {
2671 statement->declarations_begin = context->declarations;
2673 statement->declarations_begin = before->next;
2675 statement->declarations_end = last_declaration;
2677 return (statement_t*) statement;
2680 static statement_t *parse_expression_statement(void)
2682 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2683 statement->statement.type = STATEMENT_EXPRESSION;
2684 statement->statement.source_position = token.source_position;
2686 statement->expression = parse_expression();
2690 return (statement_t*) statement;
2693 static statement_t *parse_statement(void)
2695 declaration_t *declaration;
2696 statement_t *statement = NULL;
2698 /* declaration or statement */
2699 switch(token.type) {
2701 statement = parse_case_statement();
2705 statement = parse_default_statement();
2709 statement = parse_compound_statement();
2713 statement = parse_if();
2717 statement = parse_switch();
2721 statement = parse_while();
2725 statement = parse_do();
2729 statement = parse_for();
2733 statement = parse_goto();
2737 statement = parse_continue();
2741 statement = parse_break();
2745 statement = parse_return();
2754 if(look_ahead(1)->type == ':') {
2755 statement = parse_label_statement();
2759 declaration = token.v.symbol->declaration;
2760 if(declaration != NULL &&
2761 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2762 statement = parse_declaration_statement();
2766 statement = parse_expression_statement();
2769 case T___extension__:
2770 /* this can be a prefix to a declaration or an expression statement */
2771 /* we simply eat it now and parse the rest with tail recursion */
2774 } while(token.type == T___extension__);
2775 statement = parse_statement();
2779 statement = parse_declaration_statement();
2783 statement = parse_expression_statement();
2787 assert(statement == NULL || statement->source_position.input_name != NULL);
2792 static statement_t *parse_compound_statement(void)
2796 compound_statement_t *compound_statement
2797 = allocate_ast_zero(sizeof(compound_statement[0]));
2798 compound_statement->statement.type = STATEMENT_COMPOUND;
2799 compound_statement->statement.source_position = token.source_position;
2801 int top = environment_top();
2802 context_t *last_context = context;
2803 set_context(&compound_statement->context);
2805 statement_t *last_statement = NULL;
2807 while(token.type != '}' && token.type != T_EOF) {
2808 statement_t *statement = parse_statement();
2809 if(statement == NULL)
2812 if(last_statement != NULL) {
2813 last_statement->next = statement;
2815 compound_statement->statements = statement;
2818 while(statement->next != NULL)
2819 statement = statement->next;
2821 last_statement = statement;
2824 assert(context == &compound_statement->context);
2825 set_context(last_context);
2826 environment_pop_to(top);
2830 return (statement_t*) compound_statement;
2833 static translation_unit_t *parse_translation_unit(void)
2835 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2837 assert(global_context == NULL);
2838 global_context = &unit->context;
2840 assert(context == NULL);
2841 set_context(&unit->context);
2843 while(token.type != T_EOF) {
2844 parse_declaration();
2847 assert(context == &unit->context);
2849 last_declaration = NULL;
2851 assert(global_context == &unit->context);
2852 global_context = NULL;
2857 translation_unit_t *parse(void)
2859 obstack_init(&environment_obstack);
2860 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2862 type_set_output(stderr);
2864 lookahead_bufpos = 0;
2865 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2868 translation_unit_t *unit = parse_translation_unit();
2870 DEL_ARR_F(environment_stack);
2871 obstack_free(&environment_obstack, NULL);
2876 void init_parser(void)
2878 init_expression_parsers();
2879 obstack_init(&temp_obst);
2881 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
2882 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
2883 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
2884 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
2885 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
2886 type_string = make_pointer_type(type_const_char, 0);
2889 void exit_parser(void)
2891 obstack_free(&temp_obst, NULL);