11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
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 *context = NULL;
34 static declaration_t *last_declaration = NULL;
35 static struct obstack temp_obst;
37 static type_t *type_int = NULL;
38 static type_t *type_const_char = NULL;
39 static type_t *type_string = NULL;
40 static type_t *type_void = NULL;
41 static type_t *type_size_t = NULL;
43 static statement_t *parse_compound_statement(void);
44 static statement_t *parse_statement(void);
46 static expression_t *parse_sub_expression(unsigned precedence);
47 static expression_t *parse_expression(void);
48 static type_t *parse_typename(void);
50 #define STORAGE_CLASSES \
57 #define TYPE_QUALIFIERS \
63 #ifdef PROVIDE_COMPLEX
64 #define COMPLEX_SPECIFIERS \
67 #define COMPLEX_SPECIFIERS
70 #ifdef PROVIDE_IMAGINARY
71 #define IMAGINARY_SPECIFIERS \
74 #define IMAGINARY_SPECIFIERS
77 #define TYPE_SPECIFIERS \
95 #define DECLARATION_START \
100 #define TYPENAME_START \
104 static inline void *allocate_ast_zero(size_t size)
106 void *res = allocate_ast(size);
107 memset(res, 0, size);
111 static inline void *allocate_type_zero(size_t size)
113 void *res = obstack_alloc(type_obst, size);
114 memset(res, 0, size);
119 * returns the top element of the environment stack
121 static inline size_t environment_top(void)
123 return ARR_LEN(environment_stack);
128 static inline void next_token(void)
130 token = lookahead_buffer[lookahead_bufpos];
131 lookahead_buffer[lookahead_bufpos] = lexer_token;
134 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
137 print_token(stderr, &token);
138 fprintf(stderr, "\n");
142 static inline const token_t *look_ahead(int num)
144 assert(num > 0 && num <= MAX_LOOKAHEAD);
145 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
146 return & lookahead_buffer[pos];
149 static inline void eat(token_type_t type)
151 assert(token.type == type);
157 #ifdef ABORT_ON_ERROR
162 void parser_print_prefix_pos(const source_position_t source_position)
164 fputs(source_position.input_name, stderr);
166 fprintf(stderr, "%d", source_position.linenr);
170 void parser_print_error_prefix_pos(const source_position_t source_position)
172 parser_print_prefix_pos(source_position);
173 fputs("error: ", stderr);
177 void parser_print_error_prefix(void)
179 parser_print_prefix_pos(token.source_position);
183 static void parse_error(const char *message)
185 parser_print_error_prefix();
186 fprintf(stderr, "parse error: %s\n", message);
189 __attribute__((unused))
190 static void parse_warning(const char *message)
192 parser_print_prefix_pos(token.source_position);
193 fprintf(stderr, "warning: %s\n", message);
196 static void parse_error_expected(const char *message, ...)
201 if(message != NULL) {
202 parser_print_error_prefix();
203 fprintf(stderr, "%s\n", message);
205 parser_print_error_prefix();
206 fputs("Parse error: got ", stderr);
207 print_token(stderr, &token);
208 fputs(", expected ", stderr);
210 va_start(args, message);
211 token_type_t token_type = va_arg(args, token_type_t);
212 while(token_type != 0) {
216 fprintf(stderr, ", ");
218 print_token_type(stderr, token_type);
219 token_type = va_arg(args, token_type_t);
222 fprintf(stderr, "\n");
225 static void eat_block(void)
227 if(token.type == '{')
230 while(token.type != '}') {
231 if(token.type == T_EOF)
233 if(token.type == '{') {
242 static void eat_statement(void)
244 while(token.type != ';') {
245 if(token.type == T_EOF)
247 if(token.type == '}')
249 if(token.type == '{') {
258 static void eat_brace(void)
260 if(token.type == '(')
263 while(token.type != ')') {
264 if(token.type == T_EOF)
266 if(token.type == ')' || token.type == ';' || token.type == '}') {
269 if(token.type == '(') {
273 if(token.type == '{') {
282 #define expect(expected) \
283 if(UNLIKELY(token.type != (expected))) { \
284 parse_error_expected(NULL, (expected), 0); \
290 #define expect_void(expected) \
291 if(UNLIKELY(token.type != (expected))) { \
292 parse_error_expected(NULL, (expected), 0); \
298 static void set_context(context_t *new_context)
300 context = new_context;
302 declaration_t *declaration = new_context->declarations;
303 if(declaration != NULL) {
305 if(declaration->next == NULL)
307 declaration = declaration->next;
311 last_declaration = declaration;
315 * called when we find a 2nd declarator for an identifier we already have a
318 static bool is_compatible_declaration (declaration_t *declaration,
319 declaration_t *previous)
321 /* TODO: not correct yet */
322 return declaration->type == previous->type;
326 * pushs an environment_entry on the environment stack and links the
327 * corresponding symbol to the new entry
329 static inline declaration_t *environment_push(declaration_t *declaration,
332 symbol_t *symbol = declaration->symbol;
333 assert(declaration != symbol->declaration);
334 assert(declaration->source_position.input_name != NULL);
336 if(symbol->context == context) {
337 declaration_t *previous_declaration = symbol->declaration;
338 if(symbol->declaration != NULL) {
339 if(!is_compatible_declaration(declaration, previous_declaration)) {
340 parser_print_error_prefix_pos(declaration->source_position);
341 fprintf(stderr, "definition of symbol '%s' with type ",
342 declaration->symbol->string);
344 print_type(declaration->type);
346 parser_print_error_prefix_pos(
347 previous_declaration->source_position);
348 fprintf(stderr, "is incompatible with previous declaration "
350 print_type(previous_declaration->type);
353 return previous_declaration;
357 environment_entry_t *entry
358 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
359 memset(entry, 0, sizeof(entry[0]));
361 int top = ARR_LEN(environment_stack);
362 ARR_RESIZE(environment_stack, top + 1);
363 environment_stack[top] = entry;
365 entry->old_declaration = symbol->declaration;
366 entry->old_context = symbol->context;
367 entry->symbol = symbol;
368 symbol->declaration = declaration;
369 symbol->context = context;
375 * pops symbols from the environment stack until @p new_top is the top element
377 static inline void environment_pop_to(size_t new_top)
379 environment_entry_t *entry = NULL;
380 size_t top = ARR_LEN(environment_stack);
386 assert(new_top < top);
389 entry = environment_stack[i - 1];
391 symbol_t *symbol = entry->symbol;
393 symbol->declaration = entry->old_declaration;
394 symbol->context = entry->old_context;
397 } while(i != new_top);
398 obstack_free(&environment_obstack, entry);
400 ARR_SHRINKLEN(environment_stack, (int) new_top);
405 static expression_t *parse_constant_expression(void)
407 /* start parsing at precedence 7 (conditional expression) */
408 return parse_sub_expression(7);
411 static expression_t *parse_assignment_expression(void)
413 /* start parsing at precedence 2 (assignment expression) */
414 return parse_sub_expression(2);
417 static void parse_compound_type_entries(void);
418 static void parse_declarator(declaration_t *declaration,
419 storage_class_t storage_class, type_t *type,
420 int may_be_abstract);
421 static declaration_t *record_declaration(declaration_t *declaration);
423 typedef struct declaration_specifiers_t declaration_specifiers_t;
424 struct declaration_specifiers_t {
425 storage_class_t storage_class;
429 static compound_type_t *find_compound_type(compound_type_t *types,
430 const symbol_t *symbol)
432 compound_type_t *type = types;
433 for( ; type != NULL; type = type->next) {
434 if(type->symbol == symbol)
441 static type_t *parse_compound_type_specifier(bool is_struct)
449 symbol_t *symbol = NULL;
450 compound_type_t *compound_type = NULL;
452 if(token.type == T_IDENTIFIER) {
453 symbol = token.v.symbol;
456 if(context != NULL) {
458 compound_type = find_compound_type(context->structs, symbol);
460 compound_type = find_compound_type(context->unions, symbol);
463 } else if(token.type != '{') {
465 parse_error_expected("problem while parsing struct type specifier",
466 T_IDENTIFIER, '{', 0);
468 parse_error_expected("problem while parsing union type specifier",
469 T_IDENTIFIER, '{', 0);
475 if(compound_type == NULL) {
476 compound_type = allocate_type_zero(sizeof(compound_type[0]));
479 compound_type->type.type = TYPE_COMPOUND_STRUCT;
481 compound_type->type.type = TYPE_COMPOUND_UNION;
483 compound_type->source_position = token.source_position;
484 compound_type->symbol = symbol;
487 if(token.type == '{') {
488 if(compound_type->defined) {
489 parser_print_error_prefix();
490 fprintf(stderr, "multiple definition of %s %s\n",
491 is_struct ? "struct" : "union", symbol->string);
492 compound_type->context.declarations = NULL;
494 compound_type->defined = 1;
496 int top = environment_top();
497 context_t *last_context = context;
498 set_context(&compound_type->context);
500 parse_compound_type_entries();
502 assert(context == &compound_type->context);
503 set_context(last_context);
504 environment_pop_to(top);
507 return (type_t*) compound_type;
510 static void parse_enum_entries(void)
514 if(token.type == '}') {
516 parse_error("empty enum not allowed");
521 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
523 if(token.type != T_IDENTIFIER) {
524 parse_error_expected("problem while parsing enum entry",
529 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
530 entry->symbol = token.v.symbol;
531 entry->source_position = token.source_position;
534 if(token.type == '=') {
536 entry->initializer = parse_constant_expression();
539 record_declaration(entry);
541 if(token.type != ',')
544 } while(token.type != '}');
549 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
551 enum_type_t *type = types;
552 for( ; type != NULL; type = type->next) {
553 if(type->symbol == symbol)
560 static type_t *parse_enum_specifier(void)
564 symbol_t *symbol = NULL;
565 enum_type_t *enum_type = NULL;
567 if(token.type == T_IDENTIFIER) {
568 symbol = token.v.symbol;
571 if(context != NULL) {
572 enum_type = find_enum_type(context->enums, symbol);
574 } else if(token.type != '{') {
575 parse_error_expected("problem while parsing enum type specifier",
576 T_IDENTIFIER, '{', 0);
580 if(enum_type == NULL) {
581 enum_type = allocate_type_zero(sizeof(enum_type[0]));
582 enum_type->type.type = TYPE_ENUM;
583 enum_type->source_position = token.source_position;
584 enum_type->symbol = symbol;
587 if(token.type == '{') {
588 if(enum_type->defined) {
589 parser_print_error_prefix();
590 fprintf(stderr, "multiple definitions of enum %s\n",
592 enum_type->entries_begin = NULL;
593 enum_type->entries_end = NULL;
595 enum_type->defined = 1;
597 declaration_t *before = last_declaration;
599 parse_enum_entries();
602 enum_type->entries_begin = context->declarations;
604 enum_type->entries_begin = before->next;
606 enum_type->entries_end = last_declaration;
609 return (type_t*) enum_type;
612 static type_t *parse_typeof(void)
620 declaration_t *declaration;
621 expression_t *expression;
625 case T___extension__:
626 /* this can be a prefix to a typename or an expression */
627 /* we simply eat it now. */
630 } while(token.type == T___extension__);
634 declaration = token.v.symbol->declaration;
635 if(declaration != NULL
636 && declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
637 result = parse_typename();
640 expression = parse_expression();
641 result = expression->datatype;
645 result = parse_typename();
649 expression = parse_expression();
650 result = expression->datatype;
659 static const char *parse_string_literals(void)
661 assert(token.type == T_STRING_LITERAL);
662 const char *result = token.v.string;
666 while(token.type == T_STRING_LITERAL) {
667 result = concat_strings(result, token.v.string);
674 static void parse_attributes(void)
678 case T___attribute__:
686 parse_error("EOF while parsing attribute");
704 if(token.type != T_STRING_LITERAL) {
705 parse_error_expected("while parsing assembler attribute",
710 parse_string_literals();
715 goto attributes_finished;
724 SPECIFIER_SIGNED = 1 << 0,
725 SPECIFIER_UNSIGNED = 1 << 1,
726 SPECIFIER_LONG = 1 << 2,
727 SPECIFIER_INT = 1 << 3,
728 SPECIFIER_DOUBLE = 1 << 4,
729 SPECIFIER_CHAR = 1 << 5,
730 SPECIFIER_SHORT = 1 << 6,
731 SPECIFIER_LONG_LONG = 1 << 7,
732 SPECIFIER_FLOAT = 1 << 8,
733 SPECIFIER_BOOL = 1 << 9,
734 SPECIFIER_VOID = 1 << 10,
735 #ifdef PROVIDE_COMPLEX
736 SPECIFIER_COMPLEX = 1 << 11,
738 #ifdef PROVIDE_IMAGINARY
739 SPECIFIER_IMAGINARY = 1 << 12,
743 static type_t *create_builtin_type(symbol_t *symbol)
745 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
746 type->type.type = TYPE_BUILTIN;
747 type->symbol = symbol;
749 type_t *result = typehash_insert((type_t*) type);
750 if(result != (type_t*) type) {
751 obstack_free(type_obst, type);
757 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
759 declaration_t *declaration;
761 unsigned type_qualifiers = 0;
762 unsigned type_specifiers = 0;
769 #define MATCH_STORAGE_CLASS(token, class) \
771 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
772 parse_error("multiple storage classes in declaration " \
775 specifiers->storage_class = class; \
779 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
780 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
781 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
782 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
783 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
785 /* type qualifiers */
786 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
788 type_qualifiers |= qualifier; \
792 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
793 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
794 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
795 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
797 case T___extension__:
802 /* type specifiers */
803 #define MATCH_SPECIFIER(token, specifier, name) \
806 if(type_specifiers & specifier) { \
807 parse_error("multiple " name " type specifiers given"); \
809 type_specifiers |= specifier; \
813 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
814 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
815 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
816 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
817 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
818 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
819 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
820 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
821 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
822 #ifdef PROVIDE_COMPLEX
823 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
825 #ifdef PROVIDE_IMAGINARY
826 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
830 if(type_specifiers & SPECIFIER_LONG_LONG) {
831 parse_error("multiple type specifiers given");
832 } else if(type_specifiers & SPECIFIER_LONG) {
833 type_specifiers |= SPECIFIER_LONG_LONG;
835 type_specifiers |= SPECIFIER_LONG;
839 /* TODO: if type != NULL for the following rules issue an error */
841 type = parse_compound_type_specifier(true);
844 type = parse_compound_type_specifier(false);
847 type = parse_enum_specifier();
850 type = parse_typeof();
852 case T___builtin_va_list:
853 type = create_builtin_type(token.v.symbol);
857 case T___attribute__:
863 declaration = token.v.symbol->declaration;
864 if(declaration == NULL ||
865 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
866 goto finish_specifiers;
869 type = declaration->type;
870 assert(type != NULL);
874 /* function specifier */
876 goto finish_specifiers;
883 atomic_type_type_t atomic_type;
885 /* match valid basic types */
886 switch(type_specifiers) {
888 atomic_type = ATOMIC_TYPE_VOID;
891 atomic_type = ATOMIC_TYPE_CHAR;
893 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
894 atomic_type = ATOMIC_TYPE_SCHAR;
896 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
897 atomic_type = ATOMIC_TYPE_UCHAR;
899 case SPECIFIER_SHORT:
900 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
901 case SPECIFIER_SHORT | SPECIFIER_INT:
902 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
903 atomic_type = ATOMIC_TYPE_SHORT;
905 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
906 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
907 atomic_type = ATOMIC_TYPE_USHORT;
910 case SPECIFIER_SIGNED:
911 case SPECIFIER_SIGNED | SPECIFIER_INT:
912 atomic_type = ATOMIC_TYPE_INT;
914 case SPECIFIER_UNSIGNED:
915 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
916 atomic_type = ATOMIC_TYPE_UINT;
919 case SPECIFIER_SIGNED | SPECIFIER_LONG:
920 case SPECIFIER_LONG | SPECIFIER_INT:
921 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
922 atomic_type = ATOMIC_TYPE_LONG;
924 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
925 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
926 atomic_type = ATOMIC_TYPE_ULONG;
928 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
929 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
930 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
931 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
933 atomic_type = ATOMIC_TYPE_LONGLONG;
935 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
936 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
938 atomic_type = ATOMIC_TYPE_ULONGLONG;
940 case SPECIFIER_FLOAT:
941 atomic_type = ATOMIC_TYPE_FLOAT;
943 case SPECIFIER_DOUBLE:
944 atomic_type = ATOMIC_TYPE_DOUBLE;
946 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
947 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
950 atomic_type = ATOMIC_TYPE_BOOL;
952 #ifdef PROVIDE_COMPLEX
953 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
954 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
956 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
957 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
959 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
960 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
963 #ifdef PROVIDE_IMAGINARY
964 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
965 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
967 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
968 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
970 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
971 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
975 /* invalid specifier combination, give an error message */
976 if(type_specifiers == 0) {
978 parse_warning("no type specifiers in declaration (using int)");
979 atomic_type = ATOMIC_TYPE_INT;
982 parse_error("no type specifiers given in declaration");
984 } else if((type_specifiers & SPECIFIER_SIGNED) &&
985 (type_specifiers & SPECIFIER_UNSIGNED)) {
986 parse_error("signed and unsigned specifiers gives");
987 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
988 parse_error("only integer types can be signed or unsigned");
990 parse_error("multiple datatypes in declaration");
992 atomic_type = ATOMIC_TYPE_INVALID;
995 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
996 atype->type.type = TYPE_ATOMIC;
997 atype->atype = atomic_type;
1000 type = (type_t*) atype;
1002 if(type_specifiers != 0) {
1003 parse_error("multiple datatypes in declaration");
1007 type->qualifiers = type_qualifiers;
1009 type_t *result = typehash_insert(type);
1010 if(newtype && result != (type_t*) type) {
1011 obstack_free(type_obst, type);
1014 specifiers->type = result;
1017 static type_qualifier_t parse_type_qualifiers(void)
1019 type_qualifier_t type_qualifiers = 0;
1022 switch(token.type) {
1023 /* type qualifiers */
1024 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1025 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1026 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1027 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1030 return type_qualifiers;
1035 static void parse_identifier_list(void)
1038 if(token.type != T_IDENTIFIER) {
1039 parse_error_expected("problem while parsing parameter identifier "
1040 "list", T_IDENTIFIER, 0);
1044 if(token.type != ',')
1050 static declaration_t *parse_parameter(void)
1052 declaration_specifiers_t specifiers;
1053 memset(&specifiers, 0, sizeof(specifiers));
1055 parse_declaration_specifiers(&specifiers);
1057 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1058 parse_declarator(declaration, specifiers.storage_class,
1059 specifiers.type, 1);
1064 static declaration_t *parse_parameters(method_type_t *type)
1066 if(token.type == T_IDENTIFIER) {
1067 symbol_t *symbol = token.v.symbol;
1068 declaration_t *declaration = symbol->declaration;
1069 if(declaration == NULL
1070 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1072 parse_identifier_list();
1077 if(token.type == ')') {
1078 type->unspecified_parameters = 1;
1081 if(token.type == T_void && look_ahead(1)->type == ')') {
1086 declaration_t *declarations = NULL;
1087 declaration_t *declaration;
1088 declaration_t *last_declaration = NULL;
1089 method_parameter_t *parameter;
1090 method_parameter_t *last_parameter = NULL;
1093 switch(token.type) {
1097 return declarations;
1100 case T___extension__:
1102 declaration = parse_parameter();
1104 parameter = allocate_type_zero(sizeof(parameter[0]));
1105 parameter->type = declaration->type;
1107 if(last_parameter != NULL) {
1108 last_declaration->next = declaration;
1109 last_parameter->next = parameter;
1111 type->parameters = parameter;
1112 declarations = declaration;
1114 last_parameter = parameter;
1115 last_declaration = declaration;
1119 return declarations;
1121 if(token.type != ',')
1122 return declarations;
1131 } construct_type_type_t;
1133 typedef struct construct_type_t construct_type_t;
1134 struct construct_type_t {
1135 construct_type_type_t type;
1136 construct_type_t *next;
1139 typedef struct parsed_pointer_t parsed_pointer_t;
1140 struct parsed_pointer_t {
1141 construct_type_t construct_type;
1142 type_qualifier_t type_qualifiers;
1145 typedef struct construct_method_type_t construct_method_type_t;
1146 struct construct_method_type_t {
1147 construct_type_t construct_type;
1148 method_type_t *method_type;
1151 typedef struct parsed_array_t parsed_array_t;
1152 struct parsed_array_t {
1153 construct_type_t construct_type;
1154 type_qualifier_t type_qualifiers;
1160 typedef struct construct_base_type_t construct_base_type_t;
1161 struct construct_base_type_t {
1162 construct_type_t construct_type;
1166 static construct_type_t *parse_pointer_declarator(void)
1170 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1171 memset(pointer, 0, sizeof(pointer[0]));
1172 pointer->type_qualifiers = parse_type_qualifiers();
1174 return (construct_type_t*) pointer;
1177 static construct_type_t *parse_array_declarator(void)
1181 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1182 memset(array, 0, sizeof(array[0]));
1184 if(token.type == T_static) {
1185 array->is_static = true;
1189 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1190 if(type_qualifiers != 0) {
1191 if(token.type == T_static) {
1192 array->is_static = true;
1196 array->type_qualifiers = type_qualifiers;
1198 if(token.type == '*' && look_ahead(1)->type == ']') {
1199 array->is_variable = true;
1201 } else if(token.type != ']') {
1202 array->size = parse_assignment_expression();
1207 return (construct_type_t*) array;
1210 static construct_type_t *parse_method_declarator(declaration_t *declaration)
1214 method_type_t *method_type
1215 = allocate_type_zero(sizeof(method_type[0]));
1216 method_type->type.type = TYPE_METHOD;
1218 declaration_t *parameters = parse_parameters(method_type);
1219 if(declaration != NULL) {
1220 declaration->context.declarations = parameters;
1223 construct_method_type_t *construct_method_type =
1224 obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
1225 memset(construct_method_type, 0, sizeof(construct_method_type[0]));
1226 construct_method_type->construct_type.type = CONSTRUCT_METHOD;
1227 construct_method_type->method_type = method_type;
1231 return (construct_type_t*) construct_method_type;
1234 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1235 int may_be_abstract)
1237 construct_type_t *result = NULL;
1238 construct_type_t *last = NULL;
1240 while(token.type == '*') {
1241 construct_type_t *type = parse_pointer_declarator();
1250 /* TODO: find out if this is correct */
1253 construct_type_t *inner_types = NULL;
1255 switch(token.type) {
1257 if(declaration == NULL) {
1258 parse_error("no identifier expected in typename");
1260 declaration->symbol = token.v.symbol;
1261 declaration->source_position = token.source_position;
1267 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1273 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1278 construct_type_t *type;
1279 switch(token.type) {
1281 type = parse_method_declarator(declaration);
1284 type = parse_array_declarator();
1287 goto declarator_finished;
1298 declarator_finished:
1301 if(inner_types != NULL) {
1303 last->next = inner_types;
1305 result = inner_types;
1313 static type_t *construct_declarator_type(construct_type_t *construct_list,
1316 construct_type_t *iter = construct_list;
1317 for( ; iter != NULL; iter = iter->next) {
1318 parsed_pointer_t *parsed_pointer;
1319 parsed_array_t *parsed_array;
1320 construct_method_type_t *construct_method_type;
1321 method_type_t *method_type;
1322 pointer_type_t *pointer_type;
1323 array_type_t *array_type;
1325 switch(iter->type) {
1326 case CONSTRUCT_METHOD:
1327 construct_method_type = (construct_method_type_t*) iter;
1328 method_type = construct_method_type->method_type;
1330 method_type->result_type = type;
1331 type = (type_t*) method_type;
1334 case CONSTRUCT_POINTER:
1335 parsed_pointer = (parsed_pointer_t*) iter;
1336 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1338 pointer_type->type.type = TYPE_POINTER;
1339 pointer_type->points_to = type;
1340 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1341 type = (type_t*) pointer_type;
1344 case CONSTRUCT_ARRAY:
1345 parsed_array = (parsed_array_t*) iter;
1346 array_type = allocate_type_zero(sizeof(array_type[0]));
1348 array_type->type.type = TYPE_ARRAY;
1349 array_type->element_type = type;
1350 array_type->type.qualifiers = parsed_array->type_qualifiers;
1351 array_type->is_static = parsed_array->is_static;
1352 array_type->is_variable = parsed_array->is_variable;
1353 array_type->size = parsed_array->size;
1354 type = (type_t*) array_type;
1358 type_t *hashed_type = typehash_insert((type_t*) type);
1359 if(hashed_type != type) {
1360 obstack_free(type_obst, type);
1368 static void parse_declarator(declaration_t *declaration,
1369 storage_class_t storage_class, type_t *type,
1370 int may_be_abstract)
1372 construct_type_t *construct_type
1373 = parse_inner_declarator(declaration, may_be_abstract);
1375 declaration->type = construct_declarator_type(construct_type, type);
1376 declaration->storage_class = storage_class;
1377 if(construct_type != NULL) {
1378 obstack_free(&temp_obst, construct_type);
1382 static type_t *parse_abstract_declarator(type_t *base_type)
1384 construct_type_t *construct_type
1385 = parse_inner_declarator(NULL, 1);
1387 if(construct_type == NULL)
1390 type_t *result = construct_declarator_type(construct_type, base_type);
1391 obstack_free(&temp_obst, construct_type);
1396 static declaration_t *record_declaration(declaration_t *declaration)
1401 symbol_t *symbol = declaration->symbol;
1402 if(symbol != NULL) {
1403 declaration_t *alias = environment_push(declaration, context);
1404 if(alias != declaration)
1408 if(last_declaration != NULL) {
1409 last_declaration->next = declaration;
1411 context->declarations = declaration;
1413 last_declaration = declaration;
1418 static void parser_error_multiple_definition(declaration_t *previous,
1419 declaration_t *declaration)
1421 parser_print_error_prefix_pos(declaration->source_position);
1422 fprintf(stderr, "multiple definition of symbol '%s'\n",
1423 declaration->symbol->string);
1424 parser_print_error_prefix_pos(previous->source_position);
1425 fprintf(stderr, "this is the location of the previous "
1430 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1433 declaration_t *ndeclaration
1434 = allocate_ast_zero(sizeof(ndeclaration[0]));
1436 parse_declarator(ndeclaration, specifiers->storage_class,
1437 specifiers->type, 0);
1438 declaration_t *declaration = record_declaration(ndeclaration);
1439 if(token.type == '=') {
1442 /* TODO: check that this is an allowed type (esp. no method type) */
1444 if(declaration->initializer != NULL) {
1445 parser_error_multiple_definition(declaration, ndeclaration);
1448 if(token.type == '{') {
1452 declaration->initializer = parse_assignment_expression();
1454 } else if(token.type == '{') {
1455 if(declaration->type->type != TYPE_METHOD) {
1456 parser_print_error_prefix();
1457 fprintf(stderr, "Declarator ");
1458 print_type_ext(declaration->type, declaration->symbol, NULL);
1459 fprintf(stderr, " is not a method type.\n");
1462 if(declaration->initializer != NULL) {
1463 parser_error_multiple_definition(declaration, ndeclaration);
1465 if(ndeclaration != declaration) {
1466 memcpy(&declaration->context, &ndeclaration->context,
1467 sizeof(declaration->context));
1470 int top = environment_top();
1471 context_t *last_context = context;
1472 set_context(&declaration->context);
1474 /* push function parameters */
1475 declaration_t *parameter = declaration->context.declarations;
1476 for( ; parameter != NULL; parameter = parameter->next) {
1477 environment_push(parameter, context);
1480 statement_t *statement = parse_compound_statement();
1482 assert(context == &declaration->context);
1483 set_context(last_context);
1484 environment_pop_to(top);
1486 declaration->statement = statement;
1490 if(token.type != ',')
1497 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1500 if(token.type == ':') {
1502 parse_constant_expression();
1503 /* TODO (bitfields) */
1505 declaration_t *declaration
1506 = allocate_ast_zero(sizeof(declaration[0]));
1507 parse_declarator(declaration, specifiers->storage_class,
1508 specifiers->type, 1);
1510 /* TODO: check for doubled fields */
1511 record_declaration(declaration);
1513 if(token.type == ':') {
1515 parse_constant_expression();
1516 /* TODO (bitfields) */
1520 if(token.type != ',')
1527 static void parse_compound_type_entries(void)
1531 while(token.type != '}' && token.type != T_EOF) {
1532 declaration_specifiers_t specifiers;
1533 memset(&specifiers, 0, sizeof(specifiers));
1534 /* TODO not correct as this allows storage class stuff... but only
1535 * specifiers and qualifiers sould be allowed here */
1536 parse_declaration_specifiers(&specifiers);
1538 parse_struct_declarators(&specifiers);
1540 if(token.type == T_EOF) {
1541 parse_error("unexpected error while parsing struct");
1546 static void parse_declaration(void)
1548 declaration_specifiers_t specifiers;
1549 memset(&specifiers, 0, sizeof(specifiers));
1550 parse_declaration_specifiers(&specifiers);
1552 if(token.type == ';') {
1556 parse_init_declarators(&specifiers);
1559 static type_t *parse_typename(void)
1561 declaration_specifiers_t specifiers;
1562 memset(&specifiers, 0, sizeof(specifiers));
1563 parse_declaration_specifiers(&specifiers);
1564 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1565 /* TODO: improve error message, user does probably not know what a
1566 * storage class is...
1568 parse_error("typename may not have a storage class");
1571 type_t *result = parse_abstract_declarator(specifiers.type);
1579 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1580 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1581 expression_t *left);
1583 typedef struct expression_parser_function_t expression_parser_function_t;
1584 struct expression_parser_function_t {
1585 unsigned precedence;
1586 parse_expression_function parser;
1587 unsigned infix_precedence;
1588 parse_expression_infix_function infix_parser;
1591 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1593 static expression_t *expected_expression_error(void)
1595 parser_print_error_prefix();
1596 fprintf(stderr, "expected expression, got token ");
1597 print_token(stderr, & token);
1598 fprintf(stderr, "\n");
1600 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1601 expression->type = EXPR_INVALID;
1607 static expression_t *parse_string_const(void)
1609 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1611 cnst->expression.type = EXPR_STRING_LITERAL;
1612 cnst->expression.datatype = type_string;
1613 cnst->value = parse_string_literals();
1615 return (expression_t*) cnst;
1618 static expression_t *parse_int_const(void)
1620 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1622 cnst->expression.type = EXPR_CONST;
1623 cnst->expression.datatype = type_int;
1624 cnst->v.int_value = token.v.intvalue;
1628 return (expression_t*) cnst;
1631 static expression_t *parse_float_const(void)
1633 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1635 cnst->expression.type = EXPR_CONST;
1636 cnst->expression.datatype = type_int;
1637 cnst->v.float_value = token.v.floatvalue;
1641 return (expression_t*) cnst;
1644 static expression_t *parse_reference(void)
1646 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1648 ref->expression.type = EXPR_REFERENCE;
1649 ref->symbol = token.v.symbol;
1651 declaration_t *declaration = ref->symbol->declaration;
1654 if(declaration == NULL) {
1656 /* is it an implicitely defined function */
1657 if(token.type == '(') {
1658 parser_print_prefix_pos(token.source_position);
1659 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1660 ref->symbol->string);
1661 /* TODO: do this correctly */
1662 return (expression_t*) ref;
1666 parser_print_error_prefix();
1667 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1669 ref->declaration = declaration;
1670 ref->expression.datatype = declaration->type;
1674 return (expression_t*) ref;
1677 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1681 /* TODO check if cast is allowed and issue warnings/errors */
1684 static expression_t *parse_cast(void)
1686 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1688 cast->expression.type = EXPR_UNARY;
1689 cast->type = UNEXPR_CAST;
1690 cast->expression.source_position = token.source_position;
1692 type_t *type = parse_typename();
1695 expression_t *value = parse_sub_expression(20);
1697 check_cast_allowed(value, type);
1699 cast->expression.datatype = type;
1700 cast->value = value;
1702 return (expression_t*) cast;
1705 static expression_t *parse_statement_expression(void)
1707 statement_expression_t *expression
1708 = allocate_ast_zero(sizeof(expression[0]));
1709 expression->expression.type = EXPR_STATEMENT;
1710 expression->statement = parse_compound_statement();
1712 /* find last statement and use it's type */
1713 const statement_t *last_statement = NULL;
1714 const statement_t *statement = expression->statement;
1715 for( ; statement != NULL; statement = statement->next) {
1716 last_statement = statement;
1719 if(last_statement->type == STATEMENT_EXPRESSION) {
1720 const expression_statement_t *expression_statement =
1721 (const expression_statement_t*) last_statement;
1722 expression->expression.datatype
1723 = expression_statement->expression->datatype;
1725 expression->expression.datatype = type_void;
1730 return (expression_t*) expression;
1733 static expression_t *parse_brace_expression(void)
1737 declaration_t *declaration;
1738 switch(token.type) {
1740 /* gcc extension: a stement expression */
1741 return parse_statement_expression();
1745 return parse_cast();
1747 declaration = token.v.symbol->declaration;
1748 if(declaration != NULL &&
1749 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1750 return parse_cast();
1754 expression_t *result = parse_expression();
1760 static expression_t *parse_function_keyword(void)
1762 eat(T___FUNCTION__);
1765 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1766 expression->expression.type = EXPR_FUNCTION;
1767 expression->expression.datatype = type_string;
1768 expression->value = "TODO: FUNCTION";
1770 return (expression_t*) expression;
1773 static expression_t *parse_pretty_function_keyword(void)
1775 eat(T___PRETTY_FUNCTION__);
1778 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1779 expression->expression.type = EXPR_PRETTY_FUNCTION;
1780 expression->expression.datatype = type_string;
1781 expression->value = "TODO: PRETTY FUNCTION";
1783 return (expression_t*) expression;
1786 static member_designator_t *parse_member_designators(void)
1788 member_designator_t *result = allocate_ast_zero(sizeof(result[0]));
1790 if(token.type != T_IDENTIFIER) {
1791 parse_error_expected("problem while parsing member designator",
1796 result->symbol = token.v.symbol;
1799 member_designator_t *last_designator = result;
1801 if(token.type == '.') {
1803 if(token.type != T_IDENTIFIER) {
1804 parse_error_expected("problem while parsing member designator",
1809 member_designator_t *designator
1810 = allocate_ast_zero(sizeof(result[0]));
1811 designator->symbol = token.v.symbol;
1814 last_designator->next = designator;
1815 last_designator = designator;
1818 if(token.type == '[') {
1820 member_designator_t *designator
1821 = allocate_ast_zero(sizeof(result[0]));
1822 designator->array_access = parse_expression();
1823 if(designator->array_access == NULL) {
1829 last_designator->next = designator;
1830 last_designator = designator;
1839 static expression_t *parse_offsetof(void)
1841 eat(T___builtin_offsetof);
1843 offsetof_expression_t *expression
1844 = allocate_ast_zero(sizeof(expression[0]));
1845 expression->expression.type = EXPR_OFFSETOF;
1846 expression->expression.datatype = type_size_t;
1849 expression->type = parse_typename();
1851 expression->member_designators = parse_member_designators();
1854 return (expression_t*) expression;
1857 static expression_t *parse_builtin_symbol(void)
1859 builtin_symbol_expression_t *expression
1860 = allocate_ast_zero(sizeof(expression[0]));
1861 expression->expression.type = EXPR_BUILTIN_SYMBOL;
1863 /* TODO: set datatype */
1865 expression->symbol = token.v.symbol;
1869 return (expression_t*) expression;
1872 static expression_t *parse_primary_expression(void)
1874 switch(token.type) {
1876 return parse_int_const();
1877 case T_FLOATINGPOINT:
1878 return parse_float_const();
1879 case T_STRING_LITERAL:
1880 return parse_string_const();
1882 return parse_reference();
1883 case T___FUNCTION__:
1884 return parse_function_keyword();
1885 case T___PRETTY_FUNCTION__:
1886 return parse_pretty_function_keyword();
1887 case T___builtin_offsetof:
1888 return parse_offsetof();
1889 case T___builtin_expect:
1890 case T___builtin_va_start:
1891 case T___builtin_va_arg:
1892 case T___builtin_va_end:
1893 return parse_builtin_symbol();
1896 return parse_brace_expression();
1899 parser_print_error_prefix();
1900 fprintf(stderr, "unexpected token ");
1901 print_token(stderr, &token);
1902 fprintf(stderr, "\n");
1905 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1906 expression->type = EXPR_INVALID;
1907 expression->datatype = type_void;
1912 static expression_t *parse_array_expression(unsigned precedence,
1913 expression_t *array_ref)
1919 array_access_expression_t *array_access
1920 = allocate_ast_zero(sizeof(array_access[0]));
1922 array_access->expression.type = EXPR_ARRAY_ACCESS;
1923 array_access->array_ref = array_ref;
1924 array_access->index = parse_expression();
1926 type_t *array_type = array_ref->datatype;
1927 if(array_type != NULL) {
1928 if(array_type->type == TYPE_POINTER) {
1929 pointer_type_t *pointer = (pointer_type_t*) array_type;
1930 array_access->expression.datatype = pointer->points_to;
1932 parser_print_error_prefix();
1933 fprintf(stderr, "array access on object with non-pointer type ");
1934 print_type(array_type);
1935 fprintf(stderr, "\n");
1939 if(token.type != ']') {
1940 parse_error_expected("Problem while parsing array access", ']', 0);
1941 return (expression_t*) array_access;
1945 return (expression_t*) array_access;
1948 static bool is_declaration_specifier(const token_t *token,
1949 bool only_type_specifiers)
1951 declaration_t *declaration;
1953 switch(token->type) {
1957 declaration = token->v.symbol->declaration;
1958 if(declaration == NULL)
1960 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1965 if(only_type_specifiers)
1974 static expression_t *parse_sizeof(unsigned precedence)
1978 sizeof_expression_t *sizeof_expression
1979 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1980 sizeof_expression->expression.type = EXPR_SIZEOF;
1981 sizeof_expression->expression.datatype = type_size_t;
1983 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
1985 sizeof_expression->type = parse_typename();
1988 expression_t *expression = parse_sub_expression(precedence);
1989 sizeof_expression->type = expression->datatype;
1990 sizeof_expression->size_expression = expression;
1993 return (expression_t*) sizeof_expression;
1996 static expression_t *parse_select_expression(unsigned precedence,
1997 expression_t *compound)
2001 assert(token.type == '.' || token.type == T_MINUSGREATER);
2004 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2006 select->expression.type = EXPR_SELECT;
2007 select->compound = compound;
2009 /* TODO: datatype */
2011 if(token.type != T_IDENTIFIER) {
2012 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2013 return (expression_t*) select;
2015 select->symbol = token.v.symbol;
2018 return (expression_t*) select;
2021 static expression_t *parse_call_expression(unsigned precedence,
2022 expression_t *expression)
2025 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2027 call->expression.type = EXPR_CALL;
2028 call->method = expression;
2030 /* parse arguments */
2033 if(token.type != ')') {
2034 call_argument_t *last_argument = NULL;
2037 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2039 argument->expression = parse_assignment_expression();
2040 if(last_argument == NULL) {
2041 call->arguments = argument;
2043 last_argument->next = argument;
2045 last_argument = argument;
2047 if(token.type != ',')
2054 type_t *type = expression->datatype;
2056 /* we can call pointer to function */
2057 if(type->type == TYPE_POINTER) {
2058 pointer_type_t *pointer = (pointer_type_t*) type;
2059 type = pointer->points_to;
2062 if(type == NULL || type->type != TYPE_METHOD) {
2063 parser_print_error_prefix();
2064 fprintf(stderr, "expected a method type for call but found type ");
2065 print_type(expression->datatype);
2066 fprintf(stderr, "\n");
2068 method_type_t *method_type = (method_type_t*) type;
2069 call->expression.datatype = method_type->result_type;
2073 return (expression_t*) call;
2076 static void type_error(const char *msg, const source_position_t source_position,
2079 parser_print_error_prefix_pos(source_position);
2080 fprintf(stderr, "%s, but found type ", msg);
2082 fputc('\n', stderr);
2086 static void type_error_incompatible(const char *msg,
2087 const source_position_t source_position, const type_t *type1,
2088 const type_t *type2)
2090 parser_print_error_prefix_pos(source_position);
2091 fprintf(stderr, "%s, incompatible types: ", msg);
2093 fprintf(stderr, " - ");
2095 fprintf(stderr, ")\n");
2099 static type_t *get_type_after_conversion(const type_t *type1,
2100 const type_t *type2)
2104 return (type_t*) type1;
2107 static expression_t *parse_conditional_expression(unsigned precedence,
2108 expression_t *expression)
2112 conditional_expression_t *conditional
2113 = allocate_ast_zero(sizeof(conditional[0]));
2114 conditional->expression.type = EXPR_CONDITIONAL;
2115 conditional->condition = expression;
2118 type_t *condition_type = conditional->condition->datatype;
2119 if(condition_type != NULL) {
2120 if(!is_type_scalar(condition_type)) {
2121 type_error("expected a scalar type", expression->source_position,
2126 conditional->true_expression = parse_expression();
2128 conditional->false_expression = parse_sub_expression(precedence);
2130 type_t *true_type = conditional->true_expression->datatype;
2131 if(true_type == NULL)
2132 return (expression_t*) conditional;
2133 type_t *false_type = conditional->false_expression->datatype;
2134 if(false_type == NULL)
2135 return (expression_t*) conditional;
2138 if(true_type == false_type) {
2139 conditional->expression.datatype = true_type;
2140 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2141 type_t *result = get_type_after_conversion(true_type, false_type);
2142 /* TODO: create implicit convs if necessary */
2143 conditional->expression.datatype = result;
2144 } else if(true_type->type == TYPE_POINTER &&
2145 false_type->type == TYPE_POINTER &&
2146 true /* TODO compatible points_to types */) {
2148 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2149 || (is_null_ptr_const(false_type) &&
2150 true_type->type == TYPE_POINTER) TODO*/ false) {
2152 } else if(/* 1 is pointer to object type, other is void* */ false) {
2155 type_error_incompatible("problem while parsing conditional",
2156 expression->source_position, true_type,
2160 return (expression_t*) conditional;
2163 static expression_t *parse_extension(unsigned precedence)
2165 eat(T___extension__);
2167 /* TODO enable extensions */
2169 return parse_sub_expression(precedence);
2172 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
2174 expression_t *parse_##unexpression_type(unsigned precedence) \
2178 unary_expression_t *unary_expression \
2179 = allocate_ast_zero(sizeof(unary_expression[0])); \
2180 unary_expression->expression.type = EXPR_UNARY; \
2181 unary_expression->type = unexpression_type; \
2182 unary_expression->value = parse_sub_expression(precedence); \
2184 return (expression_t*) unary_expression; \
2187 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
2188 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
2189 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
2190 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
2191 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
2192 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
2193 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
2194 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
2196 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
2198 expression_t *parse_##unexpression_type(unsigned precedence, \
2199 expression_t *left) \
2201 (void) precedence; \
2204 unary_expression_t *unary_expression \
2205 = allocate_ast_zero(sizeof(unary_expression[0])); \
2206 unary_expression->expression.type = EXPR_UNARY; \
2207 unary_expression->type = unexpression_type; \
2208 unary_expression->value = left; \
2210 return (expression_t*) unary_expression; \
2213 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
2214 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
2216 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
2218 expression_t *parse_##binexpression_type(unsigned precedence, \
2219 expression_t *left) \
2223 expression_t *right = parse_sub_expression(precedence); \
2225 binary_expression_t *binexpr \
2226 = allocate_ast_zero(sizeof(binexpr[0])); \
2227 binexpr->expression.type = EXPR_BINARY; \
2228 binexpr->type = binexpression_type; \
2229 binexpr->left = left; \
2230 binexpr->right = right; \
2232 return (expression_t*) binexpr; \
2235 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2236 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2237 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2238 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2239 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2240 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2241 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2242 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2243 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2244 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2245 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2246 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2247 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2248 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2249 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2250 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2251 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2252 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2253 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2254 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2255 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2256 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2257 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2258 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2259 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2260 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2261 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2262 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2263 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2264 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2266 static expression_t *parse_sub_expression(unsigned precedence)
2268 if(token.type < 0) {
2269 return expected_expression_error();
2272 expression_parser_function_t *parser
2273 = &expression_parsers[token.type];
2274 source_position_t source_position = token.source_position;
2277 if(parser->parser != NULL) {
2278 left = parser->parser(parser->precedence);
2280 left = parse_primary_expression();
2282 assert(left != NULL);
2283 left->source_position = source_position;
2286 if(token.type < 0) {
2287 return expected_expression_error();
2290 parser = &expression_parsers[token.type];
2291 if(parser->infix_parser == NULL)
2293 if(parser->infix_precedence < precedence)
2296 left = parser->infix_parser(parser->infix_precedence, left);
2298 assert(left != NULL);
2299 assert(left->type != EXPR_INVALID);
2300 left->source_position = source_position;
2306 static expression_t *parse_expression(void)
2308 return parse_sub_expression(1);
2313 void register_expression_parser(parse_expression_function parser,
2314 int token_type, unsigned precedence)
2316 expression_parser_function_t *entry = &expression_parsers[token_type];
2318 if(entry->parser != NULL) {
2319 fprintf(stderr, "for token ");
2320 print_token_type(stderr, token_type);
2321 fprintf(stderr, "\n");
2322 panic("trying to register multiple expression parsers for a token");
2324 entry->parser = parser;
2325 entry->precedence = precedence;
2328 void register_expression_infix_parser(parse_expression_infix_function parser,
2329 int token_type, unsigned precedence)
2331 expression_parser_function_t *entry = &expression_parsers[token_type];
2333 if(entry->infix_parser != NULL) {
2334 fprintf(stderr, "for token ");
2335 print_token_type(stderr, token_type);
2336 fprintf(stderr, "\n");
2337 panic("trying to register multiple infix expression parsers for a "
2340 entry->infix_parser = parser;
2341 entry->infix_precedence = precedence;
2344 static void init_expression_parsers(void)
2346 memset(&expression_parsers, 0, sizeof(expression_parsers));
2348 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2349 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2350 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2351 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2352 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2353 T_GREATERGREATER, 16);
2354 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2355 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2356 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2357 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2358 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2359 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2360 T_GREATEREQUAL, 14);
2361 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2362 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2363 T_EXCLAMATIONMARKEQUAL, 13);
2364 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2365 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2366 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2367 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2368 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2369 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2370 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2371 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2372 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2373 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2374 T_ASTERISKEQUAL, 2);
2375 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2376 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2378 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2379 T_LESSLESSEQUAL, 2);
2380 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2381 T_GREATERGREATEREQUAL, 2);
2382 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2384 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2386 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2389 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2391 register_expression_infix_parser(parse_array_expression, '[', 30);
2392 register_expression_infix_parser(parse_call_expression, '(', 30);
2393 register_expression_infix_parser(parse_select_expression, '.', 30);
2394 register_expression_infix_parser(parse_select_expression,
2395 T_MINUSGREATER, 30);
2396 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2398 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2401 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2402 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2403 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2404 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2405 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2406 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2407 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2408 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2409 register_expression_parser(parse_sizeof, T_sizeof, 25);
2410 register_expression_parser(parse_extension, T___extension__, 25);
2414 static statement_t *parse_case_statement(void)
2417 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2418 label->statement.type = STATEMENT_CASE_LABEL;
2419 label->statement.source_position = token.source_position;
2421 label->expression = parse_expression();
2424 label->statement.next = parse_statement();
2426 return (statement_t*) label;
2429 static statement_t *parse_default_statement(void)
2433 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2434 label->statement.type = STATEMENT_CASE_LABEL;
2435 label->statement.source_position = token.source_position;
2438 label->statement.next = parse_statement();
2440 return (statement_t*) label;
2443 static statement_t *parse_label_statement(void)
2452 static statement_t *parse_if(void)
2456 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2457 statement->statement.type = STATEMENT_IF;
2458 statement->statement.source_position = token.source_position;
2461 statement->condition = parse_expression();
2464 statement->true_statement = parse_statement();
2465 if(token.type == T_else) {
2467 statement->false_statement = parse_statement();
2470 return (statement_t*) statement;
2473 static statement_t *parse_switch(void)
2477 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2478 statement->statement.type = STATEMENT_SWITCH;
2479 statement->statement.source_position = token.source_position;
2482 statement->expression = parse_expression();
2484 statement->body = parse_statement();
2486 return (statement_t*) statement;
2489 static statement_t *parse_while(void)
2493 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2494 statement->statement.type = STATEMENT_WHILE;
2495 statement->statement.source_position = token.source_position;
2498 statement->condition = parse_expression();
2500 statement->body = parse_statement();
2502 return (statement_t*) statement;
2505 static statement_t *parse_do(void)
2509 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2510 statement->statement.type = STATEMENT_DO_WHILE;
2511 statement->statement.source_position = token.source_position;
2513 statement->body = parse_statement();
2516 statement->condition = parse_expression();
2520 return (statement_t*) statement;
2523 static statement_t *parse_for(void)
2527 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2528 statement->statement.type = STATEMENT_FOR;
2529 statement->statement.source_position = token.source_position;
2533 int top = environment_top();
2534 context_t *last_context = context;
2535 set_context(&statement->context);
2537 if(token.type != ';') {
2538 if(is_declaration_specifier(&token, false)) {
2539 parse_declaration();
2541 statement->initialisation = parse_expression();
2548 if(token.type != ';') {
2549 statement->condition = parse_expression();
2552 if(token.type != ')') {
2553 statement->step = parse_expression();
2556 statement->body = parse_statement();
2558 assert(context == &statement->context);
2559 set_context(last_context);
2560 environment_pop_to(top);
2562 return (statement_t*) statement;
2565 static statement_t *parse_goto(void)
2568 expect(T_IDENTIFIER);
2574 static statement_t *parse_continue(void)
2579 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2580 statement->source_position = token.source_position;
2581 statement->type = STATEMENT_CONTINUE;
2586 static statement_t *parse_break(void)
2591 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2592 statement->source_position = token.source_position;
2593 statement->type = STATEMENT_BREAK;
2598 static statement_t *parse_return(void)
2602 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2604 statement->statement.type = STATEMENT_RETURN;
2605 statement->statement.source_position = token.source_position;
2606 if(token.type != ';') {
2607 statement->return_value = parse_expression();
2611 return (statement_t*) statement;
2614 static statement_t *parse_declaration_statement(void)
2616 declaration_t *before = last_declaration;
2618 declaration_statement_t *statement
2619 = allocate_ast_zero(sizeof(statement[0]));
2620 statement->statement.type = STATEMENT_DECLARATION;
2621 statement->statement.source_position = token.source_position;
2623 declaration_specifiers_t specifiers;
2624 memset(&specifiers, 0, sizeof(specifiers));
2625 parse_declaration_specifiers(&specifiers);
2627 if(token.type == ';') {
2630 parse_init_declarators(&specifiers);
2633 if(before == NULL) {
2634 statement->declarations_begin = context->declarations;
2636 statement->declarations_begin = before->next;
2638 statement->declarations_end = last_declaration;
2640 return (statement_t*) statement;
2643 static statement_t *parse_expression_statement(void)
2645 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2646 statement->statement.type = STATEMENT_EXPRESSION;
2647 statement->statement.source_position = token.source_position;
2649 statement->expression = parse_expression();
2653 return (statement_t*) statement;
2656 static statement_t *parse_statement(void)
2658 declaration_t *declaration;
2659 statement_t *statement = NULL;
2661 /* declaration or statement */
2662 switch(token.type) {
2664 statement = parse_case_statement();
2668 statement = parse_default_statement();
2672 statement = parse_compound_statement();
2676 statement = parse_if();
2680 statement = parse_switch();
2684 statement = parse_while();
2688 statement = parse_do();
2692 statement = parse_for();
2696 statement = parse_goto();
2700 statement = parse_continue();
2704 statement = parse_break();
2708 statement = parse_return();
2717 if(look_ahead(1)->type == ':') {
2718 statement = parse_label_statement();
2722 declaration = token.v.symbol->declaration;
2723 if(declaration != NULL &&
2724 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2725 statement = parse_declaration_statement();
2729 statement = parse_expression_statement();
2732 case T___extension__:
2733 /* this can be a prefix to a declaration or an expression statement */
2734 /* we simply eat it now and parse the rest with tail recursion */
2737 } while(token.type == T___extension__);
2738 statement = parse_statement();
2742 statement = parse_declaration_statement();
2746 statement = parse_expression_statement();
2750 assert(statement == NULL || statement->source_position.input_name != NULL);
2755 static statement_t *parse_compound_statement(void)
2759 compound_statement_t *compound_statement
2760 = allocate_ast_zero(sizeof(compound_statement[0]));
2761 compound_statement->statement.type = STATEMENT_COMPOUND;
2762 compound_statement->statement.source_position = token.source_position;
2764 int top = environment_top();
2765 context_t *last_context = context;
2766 set_context(&compound_statement->context);
2768 statement_t *last_statement = NULL;
2770 while(token.type != '}' && token.type != T_EOF) {
2771 statement_t *statement = parse_statement();
2772 if(statement == NULL)
2775 if(last_statement != NULL) {
2776 last_statement->next = statement;
2778 compound_statement->statements = statement;
2781 while(statement->next != NULL)
2782 statement = statement->next;
2784 last_statement = statement;
2787 assert(context == &compound_statement->context);
2788 set_context(last_context);
2789 environment_pop_to(top);
2793 return (statement_t*) compound_statement;
2796 static translation_unit_t *parse_translation_unit(void)
2798 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2800 assert(context == NULL);
2801 set_context(&unit->context);
2803 while(token.type != T_EOF) {
2804 parse_declaration();
2807 assert(context == &unit->context);
2809 last_declaration = NULL;
2814 translation_unit_t *parse(void)
2816 obstack_init(&environment_obstack);
2817 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2819 type_set_output(stderr);
2821 lookahead_bufpos = 0;
2822 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2825 translation_unit_t *unit = parse_translation_unit();
2827 DEL_ARR_F(environment_stack);
2828 obstack_free(&environment_obstack, NULL);
2833 void init_parser(void)
2835 init_expression_parsers();
2836 obstack_init(&temp_obst);
2838 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
2839 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
2840 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
2841 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
2842 type_string = make_pointer_type(type_const_char, 0);
2845 void exit_parser(void)
2847 obstack_free(&temp_obst, NULL);