11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
23 declaration_t *old_declaration;
25 unsigned short namespace;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static context_t *global_context = NULL;
33 static context_t *context = NULL;
34 static declaration_t *last_declaration = NULL;
35 static declaration_t *current_function = NULL;
36 static struct obstack temp_obst;
37 static bool found_error;
39 static type_t *type_int = NULL;
40 static type_t *type_uint = NULL;
41 static type_t *type_long_double = NULL;
42 static type_t *type_double = NULL;
43 static type_t *type_float = NULL;
44 static type_t *type_const_char = NULL;
45 static type_t *type_string = NULL;
46 static type_t *type_void = NULL;
47 static type_t *type_size_t = NULL;
48 static type_t *type_ptrdiff_t = NULL;
50 static statement_t *parse_compound_statement(void);
51 static statement_t *parse_statement(void);
53 static expression_t *parse_sub_expression(unsigned precedence);
54 static expression_t *parse_expression(void);
55 static type_t *parse_typename(void);
57 #define STORAGE_CLASSES \
64 #define TYPE_QUALIFIERS \
70 #ifdef PROVIDE_COMPLEX
71 #define COMPLEX_SPECIFIERS \
74 #define COMPLEX_SPECIFIERS
77 #ifdef PROVIDE_IMAGINARY
78 #define IMAGINARY_SPECIFIERS \
81 #define IMAGINARY_SPECIFIERS
84 #define TYPE_SPECIFIERS \
102 #define DECLARATION_START \
107 #define TYPENAME_START \
111 static inline void *allocate_ast_zero(size_t size)
113 void *res = allocate_ast(size);
114 memset(res, 0, size);
118 static inline void *allocate_type_zero(size_t size)
120 void *res = obstack_alloc(type_obst, size);
121 memset(res, 0, size);
125 static inline void free_type(void *type)
127 obstack_free(type_obst, type);
131 * returns the top element of the environment stack
133 static inline size_t environment_top(void)
135 return ARR_LEN(environment_stack);
140 static inline void next_token(void)
142 token = lookahead_buffer[lookahead_bufpos];
143 lookahead_buffer[lookahead_bufpos] = lexer_token;
146 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
149 print_token(stderr, &token);
150 fprintf(stderr, "\n");
154 static inline const token_t *look_ahead(int num)
156 assert(num > 0 && num <= MAX_LOOKAHEAD);
157 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
158 return & lookahead_buffer[pos];
161 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
163 static void error(void)
166 #ifdef ABORT_ON_ERROR
171 static void parser_print_prefix_pos(const source_position_t source_position)
173 fputs(source_position.input_name, stderr);
175 fprintf(stderr, "%d", source_position.linenr);
179 static void parser_print_error_prefix_pos(
180 const source_position_t source_position)
182 parser_print_prefix_pos(source_position);
183 fputs("error: ", stderr);
187 static void parser_print_error_prefix(void)
189 parser_print_error_prefix_pos(token.source_position);
192 static void parse_error(const char *message)
194 parser_print_error_prefix();
195 fprintf(stderr, "parse error: %s\n", message);
198 static void parse_warning(const char *message)
200 parser_print_prefix_pos(token.source_position);
201 fprintf(stderr, "warning: %s\n", message);
204 static void parse_error_expected(const char *message, ...)
209 if(message != NULL) {
210 parser_print_error_prefix();
211 fprintf(stderr, "%s\n", message);
213 parser_print_error_prefix();
214 fputs("Parse error: got ", stderr);
215 print_token(stderr, &token);
216 fputs(", expected ", stderr);
218 va_start(args, message);
219 token_type_t token_type = va_arg(args, token_type_t);
220 while(token_type != 0) {
224 fprintf(stderr, ", ");
226 print_token_type(stderr, token_type);
227 token_type = va_arg(args, token_type_t);
230 fprintf(stderr, "\n");
233 static void eat_block(void)
235 if(token.type == '{')
238 while(token.type != '}') {
239 if(token.type == T_EOF)
241 if(token.type == '{') {
250 static void eat_statement(void)
252 while(token.type != ';') {
253 if(token.type == T_EOF)
255 if(token.type == '}')
257 if(token.type == '{') {
266 static void eat_brace(void)
268 if(token.type == '(')
271 while(token.type != ')') {
272 if(token.type == T_EOF)
274 if(token.type == ')' || token.type == ';' || token.type == '}') {
277 if(token.type == '(') {
281 if(token.type == '{') {
290 #define expect(expected) \
291 if(UNLIKELY(token.type != (expected))) { \
292 parse_error_expected(NULL, (expected), 0); \
298 #define expect_void(expected) \
299 if(UNLIKELY(token.type != (expected))) { \
300 parse_error_expected(NULL, (expected), 0); \
306 static void set_context(context_t *new_context)
308 context = new_context;
310 last_declaration = new_context->declarations;
311 if(last_declaration != NULL) {
312 while(last_declaration->next != NULL) {
313 last_declaration = last_declaration->next;
319 * called when we find a 2nd declarator for an identifier we already have a
322 static bool is_compatible_declaration (declaration_t *declaration,
323 declaration_t *previous)
325 /* TODO: not correct yet */
326 return declaration->type == previous->type;
329 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
331 declaration_t *declaration = symbol->declaration;
332 for( ; declaration != NULL; declaration = declaration->symbol_next) {
333 if(declaration->namespace == namespace)
340 static const char *get_namespace_prefix(namespace_t namespace)
343 case NAMESPACE_NORMAL:
345 case NAMESPACE_UNION:
347 case NAMESPACE_STRUCT:
352 panic("invalid namespace found");
356 * pushs an environment_entry on the environment stack and links the
357 * corresponding symbol to the new entry
359 static declaration_t *environment_push(declaration_t *declaration)
361 symbol_t *symbol = declaration->symbol;
362 namespace_t namespace = declaration->namespace;
363 assert(declaration->source_position.input_name != NULL);
365 /* a declaration should be only pushed once */
366 assert(declaration->parent_context == NULL);
367 declaration->parent_context = context;
369 declaration_t *previous_declaration = get_declaration(symbol, namespace);
370 assert(declaration != previous_declaration);
371 if(previous_declaration != NULL
372 && previous_declaration->parent_context == context) {
373 if(!is_compatible_declaration(declaration, previous_declaration)) {
374 parser_print_error_prefix_pos(declaration->source_position);
375 fprintf(stderr, "definition of symbol %s%s with type ",
376 get_namespace_prefix(namespace), symbol->string);
378 print_type(declaration->type);
380 parser_print_error_prefix_pos(
381 previous_declaration->source_position);
382 fprintf(stderr, "is incompatible with previous declaration "
384 print_type(previous_declaration->type);
387 return previous_declaration;
390 /* remember old declaration */
392 entry.symbol = symbol;
393 entry.old_declaration = symbol->declaration;
394 entry.namespace = namespace;
395 ARR_APP1(environment_stack, entry);
397 /* replace/add declaration into declaration list of the symbol */
398 if(symbol->declaration == NULL) {
399 symbol->declaration = declaration;
401 declaration_t *iter = symbol->declaration;
402 for( ; iter != NULL; iter = iter->symbol_next) {
403 declaration_t *symbol_next = iter->symbol_next;
404 if(symbol_next == NULL) {
405 iter->symbol_next = declaration;
406 assert(declaration->symbol_next == NULL);
409 if(symbol_next->namespace == namespace) {
410 iter->symbol_next = declaration;
411 declaration->symbol_next = symbol_next->symbol_next;
421 * pops symbols from the environment stack until @p new_top is the top element
423 static void environment_pop_to(size_t new_top)
425 size_t top = ARR_LEN(environment_stack);
428 assert(new_top <= top);
432 for(i = top; i > new_top; --i) {
433 stack_entry_t *entry = & environment_stack[i - 1];
435 declaration_t *old_declaration = entry->old_declaration;
436 symbol_t *symbol = entry->symbol;
437 namespace_t namespace = entry->namespace;
439 /* replace/remove declaration */
440 declaration_t *declaration = symbol->declaration;
441 assert(declaration != NULL);
442 if(declaration->namespace == namespace) {
443 if(old_declaration == NULL) {
444 symbol->declaration = declaration->symbol_next;
446 symbol->declaration = old_declaration;
447 assert(old_declaration->symbol_next ==
448 declaration->symbol_next);
451 for(; declaration != NULL; declaration = declaration->symbol_next) {
452 declaration_t *symbol_next = declaration->symbol_next;
453 if(symbol_next->namespace == namespace) {
454 declaration->symbol_next = old_declaration;
455 assert(old_declaration->symbol_next
456 == symbol_next->symbol_next);
460 assert(declaration != NULL);
464 ARR_SHRINKLEN(environment_stack, (int) new_top);
469 static expression_t *parse_constant_expression(void)
471 /* start parsing at precedence 7 (conditional expression) */
472 return parse_sub_expression(7);
475 static expression_t *parse_assignment_expression(void)
477 /* start parsing at precedence 2 (assignment expression) */
478 return parse_sub_expression(2);
481 static void parse_compound_type_entries(void);
482 static declaration_t *parse_declarator(storage_class_t storage_class,
483 type_t *type, int may_be_abstract);
484 static declaration_t *record_declaration(declaration_t *declaration);
486 typedef struct declaration_specifiers_t declaration_specifiers_t;
487 struct declaration_specifiers_t {
488 storage_class_t storage_class;
492 static const char *parse_string_literals(void)
494 assert(token.type == T_STRING_LITERAL);
495 const char *result = token.v.string;
499 while(token.type == T_STRING_LITERAL) {
500 result = concat_strings(result, token.v.string);
507 static void parse_attributes(void)
511 case T___attribute__:
519 parse_error("EOF while parsing attribute");
537 if(token.type != T_STRING_LITERAL) {
538 parse_error_expected("while parsing assembler attribute",
543 parse_string_literals();
548 goto attributes_finished;
556 static designator_t *parse_designation(void)
558 if(token.type != '[' && token.type != '.')
561 designator_t *result = NULL;
562 designator_t *last = NULL;
565 designator_t *designator;
568 designator = allocate_ast_zero(sizeof(designator[0]));
570 designator->array_access = parse_constant_expression();
574 designator = allocate_ast_zero(sizeof(designator[0]));
576 if(token.type != T_IDENTIFIER) {
577 parse_error_expected("problem while parsing designator",
581 designator->symbol = token.v.symbol;
589 assert(designator != NULL);
591 last->next = designator;
599 static initializer_t *parse_initializer_list(void);
601 static initializer_t *parse_initializer(void)
603 designator_t *designator = parse_designation();
605 initializer_t *result;
606 if(token.type == '{') {
607 result = parse_initializer_list();
609 result = allocate_ast_zero(sizeof(result[0]));
610 result->type = INITIALIZER_VALUE;
611 result->v.value = parse_assignment_expression();
613 result->designator = designator;
618 static initializer_t *parse_initializer_list(void)
622 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
623 result->type = INITIALIZER_LIST;
625 initializer_t *last = NULL;
627 initializer_t *initializer = parse_initializer();
629 last->next = initializer;
631 result->v.list = initializer;
635 if(token.type == '}')
638 if(token.type != ',') {
639 parse_error_expected("problem while parsing initializer list",
646 if(token.type == '}')
655 static declaration_t *parse_compound_type_specifier(bool is_struct)
663 symbol_t *symbol = NULL;
664 declaration_t *declaration = NULL;
666 if(token.type == T_IDENTIFIER) {
667 symbol = token.v.symbol;
671 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
673 declaration = get_declaration(symbol, NAMESPACE_UNION);
675 } else if(token.type != '{') {
677 parse_error_expected("problem while parsing struct type specifier",
678 T_IDENTIFIER, '{', 0);
680 parse_error_expected("problem while parsing union type specifier",
681 T_IDENTIFIER, '{', 0);
687 if(declaration == NULL) {
688 declaration = allocate_type_zero(sizeof(declaration[0]));
691 declaration->namespace = NAMESPACE_STRUCT;
693 declaration->namespace = NAMESPACE_UNION;
695 declaration->source_position = token.source_position;
696 declaration->symbol = symbol;
699 if(token.type == '{') {
700 if(declaration->init.is_defined) {
701 assert(symbol != NULL);
702 parser_print_error_prefix();
703 fprintf(stderr, "multiple definition of %s %s\n",
704 is_struct ? "struct" : "union", symbol->string);
705 declaration->context.declarations = NULL;
707 record_declaration(declaration);
708 declaration->init.is_defined = true;
710 int top = environment_top();
711 context_t *last_context = context;
712 set_context(& declaration->context);
714 parse_compound_type_entries();
717 assert(context == & declaration->context);
718 set_context(last_context);
719 environment_pop_to(top);
725 static void parse_enum_entries(void)
729 if(token.type == '}') {
731 parse_error("empty enum not allowed");
736 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
738 if(token.type != T_IDENTIFIER) {
739 parse_error_expected("problem while parsing enum entry",
744 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
745 entry->symbol = token.v.symbol;
746 entry->source_position = token.source_position;
749 if(token.type == '=') {
751 entry->init.initializer = parse_initializer();
754 record_declaration(entry);
756 if(token.type != ',')
759 } while(token.type != '}');
764 static declaration_t *parse_enum_specifier(void)
768 declaration_t *declaration;
771 if(token.type == T_IDENTIFIER) {
772 symbol = token.v.symbol;
775 declaration = get_declaration(symbol, NAMESPACE_ENUM);
776 } else if(token.type != '{') {
777 parse_error_expected("problem while parsing enum type specifier",
778 T_IDENTIFIER, '{', 0);
785 if(declaration == NULL) {
786 declaration = allocate_type_zero(sizeof(declaration[0]));
788 declaration->namespace = NAMESPACE_ENUM;
789 declaration->source_position = token.source_position;
790 declaration->symbol = symbol;
793 if(token.type == '{') {
794 if(declaration->init.is_defined) {
795 parser_print_error_prefix();
796 fprintf(stderr, "multiple definitions of enum %s\n",
799 record_declaration(declaration);
800 declaration->init.is_defined = 1;
802 parse_enum_entries();
810 * if a symbol is a typedef to another type, return true
812 static bool is_typedef_symbol(symbol_t *symbol)
814 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
815 if(declaration == NULL
816 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
822 static type_t *parse_typeof(void)
830 expression_t *expression = NULL;
834 case T___extension__:
835 /* this can be a prefix to a typename or an expression */
836 /* we simply eat it now. */
839 } while(token.type == T___extension__);
843 if(is_typedef_symbol(token.v.symbol)) {
844 type = parse_typename();
846 expression = parse_expression();
847 type = expression->datatype;
852 type = parse_typename();
856 expression = parse_expression();
857 type = expression->datatype;
863 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
864 typeof->type.type = TYPE_TYPEOF;
865 typeof->expression = expression;
866 typeof->typeof_type = type;
868 return (type_t*) typeof;
872 SPECIFIER_SIGNED = 1 << 0,
873 SPECIFIER_UNSIGNED = 1 << 1,
874 SPECIFIER_LONG = 1 << 2,
875 SPECIFIER_INT = 1 << 3,
876 SPECIFIER_DOUBLE = 1 << 4,
877 SPECIFIER_CHAR = 1 << 5,
878 SPECIFIER_SHORT = 1 << 6,
879 SPECIFIER_LONG_LONG = 1 << 7,
880 SPECIFIER_FLOAT = 1 << 8,
881 SPECIFIER_BOOL = 1 << 9,
882 SPECIFIER_VOID = 1 << 10,
883 #ifdef PROVIDE_COMPLEX
884 SPECIFIER_COMPLEX = 1 << 11,
886 #ifdef PROVIDE_IMAGINARY
887 SPECIFIER_IMAGINARY = 1 << 12,
891 static type_t *create_builtin_type(symbol_t *symbol)
893 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
894 type->type.type = TYPE_BUILTIN;
895 type->symbol = symbol;
897 type->real_type = type_int;
899 return (type_t*) type;
902 static type_t *get_typedef_type(symbol_t *symbol)
904 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
905 if(declaration == NULL
906 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
909 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
910 typedef_type->type.type = TYPE_TYPEDEF;
911 typedef_type->declaration = declaration;
913 return (type_t*) typedef_type;
916 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
919 unsigned type_qualifiers = 0;
920 unsigned type_specifiers = 0;
927 #define MATCH_STORAGE_CLASS(token, class) \
929 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
930 parse_error("multiple storage classes in declaration " \
933 specifiers->storage_class = class; \
937 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
938 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
939 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
940 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
941 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
943 /* type qualifiers */
944 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
946 type_qualifiers |= qualifier; \
950 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
951 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
952 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
953 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
955 case T___extension__:
960 /* type specifiers */
961 #define MATCH_SPECIFIER(token, specifier, name) \
964 if(type_specifiers & specifier) { \
965 parse_error("multiple " name " type specifiers given"); \
967 type_specifiers |= specifier; \
971 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
972 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
973 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
974 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
975 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
976 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
977 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
978 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
979 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
980 #ifdef PROVIDE_COMPLEX
981 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
983 #ifdef PROVIDE_IMAGINARY
984 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
988 if(type_specifiers & SPECIFIER_LONG_LONG) {
989 parse_error("multiple type specifiers given");
990 } else if(type_specifiers & SPECIFIER_LONG) {
991 type_specifiers |= SPECIFIER_LONG_LONG;
993 type_specifiers |= SPECIFIER_LONG;
997 /* TODO: if type != NULL for the following rules should issue
1000 compound_type_t *compound_type
1001 = allocate_type_zero(sizeof(compound_type[0]));
1002 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1003 compound_type->declaration = parse_compound_type_specifier(true);
1005 type = (type_t*) compound_type;
1009 compound_type_t *compound_type
1010 = allocate_type_zero(sizeof(compound_type[0]));
1011 compound_type->type.type = TYPE_COMPOUND_UNION;
1012 compound_type->declaration = parse_compound_type_specifier(false);
1014 type = (type_t*) compound_type;
1018 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1019 enum_type->type.type = TYPE_ENUM;
1020 enum_type->declaration = parse_enum_specifier();
1022 type = (type_t*) enum_type;
1026 type = parse_typeof();
1028 case T___builtin_va_list:
1029 type = create_builtin_type(token.v.symbol);
1033 case T___attribute__:
1038 case T_IDENTIFIER: {
1039 type_t *typedef_type = get_typedef_type(token.v.symbol);
1041 if(typedef_type == NULL)
1042 goto finish_specifiers;
1045 type = typedef_type;
1049 /* function specifier */
1051 goto finish_specifiers;
1058 atomic_type_type_t atomic_type;
1060 /* match valid basic types */
1061 switch(type_specifiers) {
1062 case SPECIFIER_VOID:
1063 atomic_type = ATOMIC_TYPE_VOID;
1065 case SPECIFIER_CHAR:
1066 atomic_type = ATOMIC_TYPE_CHAR;
1068 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1069 atomic_type = ATOMIC_TYPE_SCHAR;
1071 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1072 atomic_type = ATOMIC_TYPE_UCHAR;
1074 case SPECIFIER_SHORT:
1075 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1076 case SPECIFIER_SHORT | SPECIFIER_INT:
1077 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1078 atomic_type = ATOMIC_TYPE_SHORT;
1080 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1081 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1082 atomic_type = ATOMIC_TYPE_USHORT;
1085 case SPECIFIER_SIGNED:
1086 case SPECIFIER_SIGNED | SPECIFIER_INT:
1087 atomic_type = ATOMIC_TYPE_INT;
1089 case SPECIFIER_UNSIGNED:
1090 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1091 atomic_type = ATOMIC_TYPE_UINT;
1093 case SPECIFIER_LONG:
1094 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1095 case SPECIFIER_LONG | SPECIFIER_INT:
1096 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1097 atomic_type = ATOMIC_TYPE_LONG;
1099 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1100 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1101 atomic_type = ATOMIC_TYPE_ULONG;
1103 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1104 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1105 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1106 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1108 atomic_type = ATOMIC_TYPE_LONGLONG;
1110 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1111 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1113 atomic_type = ATOMIC_TYPE_ULONGLONG;
1115 case SPECIFIER_FLOAT:
1116 atomic_type = ATOMIC_TYPE_FLOAT;
1118 case SPECIFIER_DOUBLE:
1119 atomic_type = ATOMIC_TYPE_DOUBLE;
1121 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1122 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1124 case SPECIFIER_BOOL:
1125 atomic_type = ATOMIC_TYPE_BOOL;
1127 #ifdef PROVIDE_COMPLEX
1128 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1129 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1131 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1132 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1134 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1135 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1138 #ifdef PROVIDE_IMAGINARY
1139 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1140 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1142 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1143 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1145 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1146 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1150 /* invalid specifier combination, give an error message */
1151 if(type_specifiers == 0) {
1153 parse_warning("no type specifiers in declaration (using int)");
1154 atomic_type = ATOMIC_TYPE_INT;
1157 parse_error("no type specifiers given in declaration");
1159 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1160 (type_specifiers & SPECIFIER_UNSIGNED)) {
1161 parse_error("signed and unsigned specifiers gives");
1162 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1163 parse_error("only integer types can be signed or unsigned");
1165 parse_error("multiple datatypes in declaration");
1167 atomic_type = ATOMIC_TYPE_INVALID;
1170 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1171 atype->type.type = TYPE_ATOMIC;
1172 atype->atype = atomic_type;
1175 type = (type_t*) atype;
1177 if(type_specifiers != 0) {
1178 parse_error("multiple datatypes in declaration");
1182 type->qualifiers = type_qualifiers;
1184 type_t *result = typehash_insert(type);
1185 if(newtype && result != (type_t*) type) {
1189 specifiers->type = result;
1192 static type_qualifier_t parse_type_qualifiers(void)
1194 type_qualifier_t type_qualifiers = 0;
1197 switch(token.type) {
1198 /* type qualifiers */
1199 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1200 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1201 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1202 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1205 return type_qualifiers;
1210 static void parse_identifier_list(void)
1213 if(token.type != T_IDENTIFIER) {
1214 parse_error_expected("problem while parsing parameter identifier "
1215 "list", T_IDENTIFIER, 0);
1219 if(token.type != ',')
1225 static declaration_t *parse_parameter(void)
1227 declaration_specifiers_t specifiers;
1228 memset(&specifiers, 0, sizeof(specifiers));
1230 parse_declaration_specifiers(&specifiers);
1232 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1233 specifiers.type, 1);
1235 /* TODO check declaration constraints for parameters */
1236 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1237 parse_error("typedef not allowed in parameter list");
1243 static declaration_t *parse_parameters(function_type_t *type)
1245 if(token.type == T_IDENTIFIER) {
1246 symbol_t *symbol = token.v.symbol;
1247 if(!is_typedef_symbol(symbol)) {
1249 parse_identifier_list();
1254 if(token.type == ')') {
1255 type->unspecified_parameters = 1;
1258 if(token.type == T_void && look_ahead(1)->type == ')') {
1263 declaration_t *declarations = NULL;
1264 declaration_t *declaration;
1265 declaration_t *last_declaration = NULL;
1266 function_parameter_t *parameter;
1267 function_parameter_t *last_parameter = NULL;
1270 switch(token.type) {
1274 return declarations;
1277 case T___extension__:
1279 declaration = parse_parameter();
1281 parameter = allocate_type_zero(sizeof(parameter[0]));
1282 parameter->type = declaration->type;
1284 if(last_parameter != NULL) {
1285 last_declaration->next = declaration;
1286 last_parameter->next = parameter;
1288 type->parameters = parameter;
1289 declarations = declaration;
1291 last_parameter = parameter;
1292 last_declaration = declaration;
1296 return declarations;
1298 if(token.type != ',')
1299 return declarations;
1308 } construct_type_type_t;
1310 typedef struct construct_type_t construct_type_t;
1311 struct construct_type_t {
1312 construct_type_type_t type;
1313 construct_type_t *next;
1316 typedef struct parsed_pointer_t parsed_pointer_t;
1317 struct parsed_pointer_t {
1318 construct_type_t construct_type;
1319 type_qualifier_t type_qualifiers;
1322 typedef struct construct_function_type_t construct_function_type_t;
1323 struct construct_function_type_t {
1324 construct_type_t construct_type;
1325 function_type_t *function_type;
1328 typedef struct parsed_array_t parsed_array_t;
1329 struct parsed_array_t {
1330 construct_type_t construct_type;
1331 type_qualifier_t type_qualifiers;
1337 typedef struct construct_base_type_t construct_base_type_t;
1338 struct construct_base_type_t {
1339 construct_type_t construct_type;
1343 static construct_type_t *parse_pointer_declarator(void)
1347 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1348 memset(pointer, 0, sizeof(pointer[0]));
1349 pointer->type_qualifiers = parse_type_qualifiers();
1351 return (construct_type_t*) pointer;
1354 static construct_type_t *parse_array_declarator(void)
1358 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1359 memset(array, 0, sizeof(array[0]));
1361 if(token.type == T_static) {
1362 array->is_static = true;
1366 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1367 if(type_qualifiers != 0) {
1368 if(token.type == T_static) {
1369 array->is_static = true;
1373 array->type_qualifiers = type_qualifiers;
1375 if(token.type == '*' && look_ahead(1)->type == ']') {
1376 array->is_variable = true;
1378 } else if(token.type != ']') {
1379 array->size = parse_assignment_expression();
1384 return (construct_type_t*) array;
1387 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1391 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1392 type->type.type = TYPE_FUNCTION;
1394 declaration_t *parameters = parse_parameters(type);
1395 if(declaration != NULL) {
1396 declaration->context.declarations = parameters;
1399 construct_function_type_t *construct_function_type =
1400 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1401 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1402 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1403 construct_function_type->function_type = type;
1407 return (construct_type_t*) construct_function_type;
1410 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1411 int may_be_abstract)
1413 construct_type_t *result = NULL;
1414 construct_type_t *last = NULL;
1416 while(token.type == '*') {
1417 construct_type_t *type = parse_pointer_declarator();
1426 /* TODO: find out if this is correct */
1429 construct_type_t *inner_types = NULL;
1431 switch(token.type) {
1433 if(declaration == NULL) {
1434 parse_error("no identifier expected in typename");
1436 declaration->symbol = token.v.symbol;
1437 declaration->source_position = token.source_position;
1443 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1449 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1454 construct_type_t *type;
1455 switch(token.type) {
1457 type = parse_function_declarator(declaration);
1460 type = parse_array_declarator();
1463 goto declarator_finished;
1474 declarator_finished:
1477 if(inner_types != NULL) {
1479 last->next = inner_types;
1481 result = inner_types;
1489 static type_t *construct_declarator_type(construct_type_t *construct_list,
1492 construct_type_t *iter = construct_list;
1493 for( ; iter != NULL; iter = iter->next) {
1494 parsed_pointer_t *parsed_pointer;
1495 parsed_array_t *parsed_array;
1496 construct_function_type_t *construct_function_type;
1497 function_type_t *function_type;
1498 pointer_type_t *pointer_type;
1499 array_type_t *array_type;
1501 switch(iter->type) {
1502 case CONSTRUCT_FUNCTION:
1503 construct_function_type = (construct_function_type_t*) iter;
1504 function_type = construct_function_type->function_type;
1506 function_type->result_type = type;
1507 type = (type_t*) function_type;
1510 case CONSTRUCT_POINTER:
1511 parsed_pointer = (parsed_pointer_t*) iter;
1512 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1514 pointer_type->type.type = TYPE_POINTER;
1515 pointer_type->points_to = type;
1516 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1517 type = (type_t*) pointer_type;
1520 case CONSTRUCT_ARRAY:
1521 parsed_array = (parsed_array_t*) iter;
1522 array_type = allocate_type_zero(sizeof(array_type[0]));
1524 array_type->type.type = TYPE_ARRAY;
1525 array_type->element_type = type;
1526 array_type->type.qualifiers = parsed_array->type_qualifiers;
1527 array_type->is_static = parsed_array->is_static;
1528 array_type->is_variable = parsed_array->is_variable;
1529 array_type->size = parsed_array->size;
1530 type = (type_t*) array_type;
1534 type_t *hashed_type = typehash_insert((type_t*) type);
1535 if(hashed_type != type) {
1544 static declaration_t *parse_declarator(storage_class_t storage_class,
1545 type_t *type, int may_be_abstract)
1547 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1548 declaration->storage_class = storage_class;
1550 construct_type_t *construct_type
1551 = parse_inner_declarator(declaration, may_be_abstract);
1552 declaration->type = construct_declarator_type(construct_type, type);
1554 if(construct_type != NULL) {
1555 obstack_free(&temp_obst, construct_type);
1561 static type_t *parse_abstract_declarator(type_t *base_type)
1563 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1565 type_t *result = construct_declarator_type(construct_type, base_type);
1566 if(construct_type != NULL) {
1567 obstack_free(&temp_obst, construct_type);
1573 static declaration_t *record_declaration(declaration_t *declaration)
1575 assert(context != NULL);
1577 symbol_t *symbol = declaration->symbol;
1578 if(symbol != NULL) {
1579 declaration_t *alias = environment_push(declaration);
1580 if(alias != declaration)
1583 declaration->parent_context = context;
1586 if(last_declaration != NULL) {
1587 last_declaration->next = declaration;
1589 context->declarations = declaration;
1591 last_declaration = declaration;
1596 static void parser_error_multiple_definition(declaration_t *previous,
1597 declaration_t *declaration)
1599 parser_print_error_prefix_pos(declaration->source_position);
1600 fprintf(stderr, "multiple definition of symbol '%s'\n",
1601 declaration->symbol->string);
1602 parser_print_error_prefix_pos(previous->source_position);
1603 fprintf(stderr, "this is the location of the previous "
1608 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1611 declaration_t *ndeclaration
1612 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1614 declaration_t *declaration = record_declaration(ndeclaration);
1615 if(token.type == '=') {
1618 /* TODO: check that this is an allowed type (no function type) */
1620 if(declaration->init.initializer != NULL) {
1621 parser_error_multiple_definition(declaration, ndeclaration);
1624 ndeclaration->init.initializer = parse_initializer();
1625 } else if(token.type == '{') {
1626 if(declaration->type->type != TYPE_FUNCTION) {
1627 parser_print_error_prefix();
1628 fprintf(stderr, "Declarator ");
1629 print_type_ext(declaration->type, declaration->symbol, NULL);
1630 fprintf(stderr, " has a body but is not a function type.\n");
1635 if(declaration->init.statement != NULL) {
1636 parser_error_multiple_definition(declaration, ndeclaration);
1638 if(ndeclaration != declaration) {
1639 memcpy(&declaration->context, &ndeclaration->context,
1640 sizeof(declaration->context));
1643 int top = environment_top();
1644 context_t *last_context = context;
1645 set_context(&declaration->context);
1647 /* push function parameters */
1648 declaration_t *parameter = declaration->context.declarations;
1649 for( ; parameter != NULL; parameter = parameter->next) {
1650 environment_push(parameter);
1652 declaration_t *old_current_function = current_function;
1653 current_function = declaration;
1655 statement_t *statement = parse_compound_statement();
1657 assert(current_function == declaration);
1658 old_current_function = current_function;
1660 assert(context == &declaration->context);
1661 set_context(last_context);
1662 environment_pop_to(top);
1664 declaration->init.statement = statement;
1668 if(token.type != ',')
1675 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1678 if(token.type == ':') {
1680 parse_constant_expression();
1681 /* TODO (bitfields) */
1683 declaration_t *declaration
1684 = parse_declarator(specifiers->storage_class,
1685 specifiers->type, 1);
1687 /* TODO: check constraints for struct declarations */
1688 /* TODO: check for doubled fields */
1689 record_declaration(declaration);
1691 if(token.type == ':') {
1693 parse_constant_expression();
1694 /* TODO (bitfields) */
1698 if(token.type != ',')
1705 static void parse_compound_type_entries(void)
1709 while(token.type != '}' && token.type != T_EOF) {
1710 declaration_specifiers_t specifiers;
1711 memset(&specifiers, 0, sizeof(specifiers));
1712 parse_declaration_specifiers(&specifiers);
1714 parse_struct_declarators(&specifiers);
1716 if(token.type == T_EOF) {
1717 parse_error("unexpected error while parsing struct");
1722 static void parse_declaration(void)
1724 source_position_t source_position = token.source_position;
1726 declaration_specifiers_t specifiers;
1727 memset(&specifiers, 0, sizeof(specifiers));
1728 parse_declaration_specifiers(&specifiers);
1730 if(token.type == ';') {
1733 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1735 declaration->type = specifiers.type;
1736 declaration->storage_class = specifiers.storage_class;
1737 declaration->source_position = source_position;
1738 record_declaration(declaration);
1741 parse_init_declarators(&specifiers);
1744 static type_t *parse_typename(void)
1746 declaration_specifiers_t specifiers;
1747 memset(&specifiers, 0, sizeof(specifiers));
1748 parse_declaration_specifiers(&specifiers);
1749 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1750 /* TODO: improve error message, user does probably not know what a
1751 * storage class is...
1753 parse_error("typename may not have a storage class");
1756 type_t *result = parse_abstract_declarator(specifiers.type);
1764 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1765 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1766 expression_t *left);
1768 typedef struct expression_parser_function_t expression_parser_function_t;
1769 struct expression_parser_function_t {
1770 unsigned precedence;
1771 parse_expression_function parser;
1772 unsigned infix_precedence;
1773 parse_expression_infix_function infix_parser;
1776 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1778 static expression_t *expected_expression_error(void)
1780 parser_print_error_prefix();
1781 fprintf(stderr, "expected expression, got token ");
1782 print_token(stderr, & token);
1783 fprintf(stderr, "\n");
1785 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1786 expression->type = EXPR_INVALID;
1792 static expression_t *parse_string_const(void)
1794 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1796 cnst->expression.type = EXPR_STRING_LITERAL;
1797 cnst->expression.datatype = type_string;
1798 cnst->value = parse_string_literals();
1800 return (expression_t*) cnst;
1803 static expression_t *parse_int_const(void)
1805 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1807 cnst->expression.type = EXPR_CONST;
1808 cnst->expression.datatype = type_int;
1809 cnst->v.int_value = token.v.intvalue;
1813 return (expression_t*) cnst;
1816 static expression_t *parse_float_const(void)
1818 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1820 cnst->expression.type = EXPR_CONST;
1821 cnst->expression.datatype = type_double;
1822 cnst->v.float_value = token.v.floatvalue;
1826 return (expression_t*) cnst;
1829 static declaration_t *create_implicit_function(symbol_t *symbol,
1830 const source_position_t source_position)
1832 function_type_t *function_type = allocate_type_zero(sizeof(function_type));
1834 function_type->type.type = TYPE_FUNCTION;
1835 function_type->result_type = type_int;
1836 function_type->unspecified_parameters = true;
1838 type_t *type = typehash_insert((type_t*) function_type);
1839 if(type != (type_t*) function_type) {
1840 free_type(function_type);
1843 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1845 declaration->storage_class = STORAGE_CLASS_EXTERN;
1846 declaration->type = type;
1847 declaration->symbol = symbol;
1848 declaration->source_position = source_position;
1850 /* prepend the implicit definition to the global context
1851 * this is safe since the symbol wasn't declared as anything else yet
1853 assert(symbol->declaration == NULL);
1855 context_t *last_context = context;
1856 context = global_context;
1858 environment_push(declaration);
1859 declaration->next = context->declarations;
1860 context->declarations = declaration;
1862 context = last_context;
1867 static expression_t *parse_reference(void)
1869 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1871 ref->expression.type = EXPR_REFERENCE;
1872 ref->symbol = token.v.symbol;
1874 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
1876 source_position_t source_position = token.source_position;
1879 if(declaration == NULL) {
1881 /* an implicitely defined function */
1882 if(token.type == '(') {
1883 parser_print_prefix_pos(token.source_position);
1884 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1885 ref->symbol->string);
1887 declaration = create_implicit_function(ref->symbol,
1892 parser_print_error_prefix();
1893 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1894 return (expression_t*) ref;
1898 ref->declaration = declaration;
1899 ref->expression.datatype = declaration->type;
1901 return (expression_t*) ref;
1904 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1908 /* TODO check if explicit cast is allowed and issue warnings/errors */
1911 static expression_t *parse_cast(void)
1913 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1915 cast->expression.type = EXPR_UNARY;
1916 cast->type = UNEXPR_CAST;
1917 cast->expression.source_position = token.source_position;
1919 type_t *type = parse_typename();
1922 expression_t *value = parse_sub_expression(20);
1924 check_cast_allowed(value, type);
1926 cast->expression.datatype = type;
1927 cast->value = value;
1929 return (expression_t*) cast;
1932 static expression_t *parse_statement_expression(void)
1934 statement_expression_t *expression
1935 = allocate_ast_zero(sizeof(expression[0]));
1936 expression->expression.type = EXPR_STATEMENT;
1937 expression->statement = parse_compound_statement();
1939 /* find last statement and use it's type */
1940 const statement_t *last_statement = NULL;
1941 const statement_t *statement = expression->statement;
1942 for( ; statement != NULL; statement = statement->next) {
1943 last_statement = statement;
1946 if(last_statement->type == STATEMENT_EXPRESSION) {
1947 const expression_statement_t *expression_statement =
1948 (const expression_statement_t*) last_statement;
1949 expression->expression.datatype
1950 = expression_statement->expression->datatype;
1952 expression->expression.datatype = type_void;
1957 return (expression_t*) expression;
1960 static expression_t *parse_brace_expression(void)
1964 switch(token.type) {
1966 /* gcc extension: a stement expression */
1967 return parse_statement_expression();
1971 return parse_cast();
1973 if(is_typedef_symbol(token.v.symbol)) {
1974 return parse_cast();
1978 expression_t *result = parse_expression();
1984 static expression_t *parse_function_keyword(void)
1986 eat(T___FUNCTION__);
1989 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1990 expression->expression.type = EXPR_FUNCTION;
1991 expression->expression.datatype = type_string;
1992 expression->value = "TODO: FUNCTION";
1994 return (expression_t*) expression;
1997 static expression_t *parse_pretty_function_keyword(void)
1999 eat(T___PRETTY_FUNCTION__);
2002 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2003 expression->expression.type = EXPR_PRETTY_FUNCTION;
2004 expression->expression.datatype = type_string;
2005 expression->value = "TODO: PRETTY FUNCTION";
2007 return (expression_t*) expression;
2010 static designator_t *parse_designator(void)
2012 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2014 if(token.type != T_IDENTIFIER) {
2015 parse_error_expected("problem while parsing member designator",
2020 result->symbol = token.v.symbol;
2023 designator_t *last_designator = result;
2025 if(token.type == '.') {
2027 if(token.type != T_IDENTIFIER) {
2028 parse_error_expected("problem while parsing member designator",
2033 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2034 designator->symbol = token.v.symbol;
2037 last_designator->next = designator;
2038 last_designator = designator;
2041 if(token.type == '[') {
2043 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2044 designator->array_access = parse_expression();
2045 if(designator->array_access == NULL) {
2051 last_designator->next = designator;
2052 last_designator = designator;
2061 static expression_t *parse_offsetof(void)
2063 eat(T___builtin_offsetof);
2065 offsetof_expression_t *expression
2066 = allocate_ast_zero(sizeof(expression[0]));
2067 expression->expression.type = EXPR_OFFSETOF;
2068 expression->expression.datatype = type_size_t;
2071 expression->type = parse_typename();
2073 expression->designator = parse_designator();
2076 return (expression_t*) expression;
2079 static expression_t *parse_va_arg(void)
2081 eat(T___builtin_va_arg);
2083 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2084 expression->expression.type = EXPR_VA_ARG;
2087 expression->arg = parse_assignment_expression();
2089 expression->expression.datatype = parse_typename();
2092 return (expression_t*) expression;
2095 static expression_t *parse_builtin_symbol(void)
2097 builtin_symbol_expression_t *expression
2098 = allocate_ast_zero(sizeof(expression[0]));
2099 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2101 /* TODO: set datatype */
2103 expression->symbol = token.v.symbol;
2107 return (expression_t*) expression;
2110 static expression_t *parse_primary_expression(void)
2112 switch(token.type) {
2114 return parse_int_const();
2115 case T_FLOATINGPOINT:
2116 return parse_float_const();
2117 case T_STRING_LITERAL:
2118 return parse_string_const();
2120 return parse_reference();
2121 case T___FUNCTION__:
2122 return parse_function_keyword();
2123 case T___PRETTY_FUNCTION__:
2124 return parse_pretty_function_keyword();
2125 case T___builtin_offsetof:
2126 return parse_offsetof();
2127 case T___builtin_va_arg:
2128 return parse_va_arg();
2129 case T___builtin_expect:
2130 case T___builtin_va_start:
2131 case T___builtin_va_end:
2132 return parse_builtin_symbol();
2135 return parse_brace_expression();
2138 parser_print_error_prefix();
2139 fprintf(stderr, "unexpected token ");
2140 print_token(stderr, &token);
2141 fprintf(stderr, "\n");
2144 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2145 expression->type = EXPR_INVALID;
2146 expression->datatype = type_void;
2151 static expression_t *parse_array_expression(unsigned precedence,
2152 expression_t *array_ref)
2158 array_access_expression_t *array_access
2159 = allocate_ast_zero(sizeof(array_access[0]));
2161 array_access->expression.type = EXPR_ARRAY_ACCESS;
2162 array_access->array_ref = array_ref;
2163 array_access->index = parse_expression();
2165 type_t *array_type = array_ref->datatype;
2166 if(array_type != NULL) {
2167 if(array_type->type == TYPE_POINTER) {
2168 pointer_type_t *pointer = (pointer_type_t*) array_type;
2169 array_access->expression.datatype = pointer->points_to;
2171 parser_print_error_prefix();
2172 fprintf(stderr, "array access on object with non-pointer type ");
2173 print_type(array_type);
2174 fprintf(stderr, "\n");
2178 if(token.type != ']') {
2179 parse_error_expected("Problem while parsing array access", ']', 0);
2180 return (expression_t*) array_access;
2184 return (expression_t*) array_access;
2187 static bool is_declaration_specifier(const token_t *token,
2188 bool only_type_specifiers)
2190 switch(token->type) {
2194 return is_typedef_symbol(token->v.symbol);
2197 if(only_type_specifiers)
2206 static expression_t *parse_sizeof(unsigned precedence)
2210 sizeof_expression_t *sizeof_expression
2211 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2212 sizeof_expression->expression.type = EXPR_SIZEOF;
2213 sizeof_expression->expression.datatype = type_size_t;
2215 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2217 sizeof_expression->type = parse_typename();
2220 expression_t *expression = parse_sub_expression(precedence);
2221 sizeof_expression->type = expression->datatype;
2222 sizeof_expression->size_expression = expression;
2225 return (expression_t*) sizeof_expression;
2228 static expression_t *parse_select_expression(unsigned precedence,
2229 expression_t *compound)
2233 assert(token.type == '.' || token.type == T_MINUSGREATER);
2236 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2238 select->expression.type = EXPR_SELECT;
2239 select->compound = compound;
2241 /* TODO: datatype */
2243 if(token.type != T_IDENTIFIER) {
2244 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2245 return (expression_t*) select;
2247 select->symbol = token.v.symbol;
2250 return (expression_t*) select;
2253 static expression_t *create_cast_expression(expression_t *expression,
2256 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2258 cast->expression.type = EXPR_UNARY;
2259 cast->type = UNEXPR_CAST;
2260 cast->value = expression;
2261 cast->expression.datatype = dest_type;
2263 return (expression_t*) cast;
2266 static void type_error(const char *msg, const source_position_t source_position,
2269 parser_print_error_prefix_pos(source_position);
2270 fprintf(stderr, "%s, but found type ", msg);
2272 fputc('\n', stderr);
2276 static void type_error_incompatible(const char *msg,
2277 const source_position_t source_position, type_t *type1, type_t *type2)
2279 parser_print_error_prefix_pos(source_position);
2280 fprintf(stderr, "%s, incompatible types: ", msg);
2282 fprintf(stderr, " - ");
2284 fprintf(stderr, ")\n");
2288 static int get_rank(const type_t *type)
2290 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
2291 * and esp. footnote 108). However we can't fold constants (yet), so we
2292 * can't decide wether unsigned int is possible, while int always works.
2293 * (unsigned int would be preferable when possible... for stuff like
2294 * struct { enum { ... } bla : 4; } ) */
2295 if(type->type == TYPE_ENUM)
2296 return ATOMIC_TYPE_INT;
2298 assert(type->type == TYPE_ATOMIC);
2299 atomic_type_t *atomic_type = (atomic_type_t*) type;
2300 atomic_type_type_t atype = atomic_type->atype;
2304 static type_t *promote_integer(type_t *type)
2306 if(get_rank(type) < ATOMIC_TYPE_INT)
2312 static expression_t *create_implicit_cast(expression_t *expression,
2315 assert(expression->datatype != NULL);
2316 type_t *source_type = expression->datatype;
2318 if(expression->datatype == dest_type)
2321 if(dest_type->type == TYPE_ATOMIC) {
2322 if(source_type->type != TYPE_ATOMIC)
2323 panic("casting of non-atomic types not implemented yet");
2325 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
2326 type_error_incompatible("can't cast types",
2327 expression->source_position,
2328 source_type, dest_type);
2332 return create_cast_expression(expression, dest_type);
2335 panic("casting of non-atomic types not implemented yet");
2338 static expression_t *parse_call_expression(unsigned precedence,
2339 expression_t *expression)
2342 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2343 call->expression.type = EXPR_CALL;
2344 call->function = expression;
2346 function_type_t *function_type;
2347 type_t *type = expression->datatype;
2348 if(type->type != TYPE_FUNCTION) {
2349 /* TODO calling pointers to functions is ok */
2350 parser_print_error_prefix();
2351 fputs("called object '", stderr);
2352 print_expression(expression);
2353 fputs("' (type ", stderr);
2355 fputs("is not a function\n", stderr);
2357 function_type = NULL;
2358 call->expression.datatype = NULL;
2360 function_type = (function_type_t*) type;
2361 call->expression.datatype = function_type->result_type;
2364 /* parse arguments */
2367 if(token.type != ')') {
2368 call_argument_t *last_argument = NULL;
2371 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2373 argument->expression = parse_assignment_expression();
2374 if(last_argument == NULL) {
2375 call->arguments = argument;
2377 last_argument->next = argument;
2379 last_argument = argument;
2381 if(token.type != ',')
2388 if(function_type != NULL) {
2389 function_parameter_t *parameter = function_type->parameters;
2390 call_argument_t *argument = call->arguments;
2391 for( ; parameter != NULL && argument != NULL;
2392 parameter = parameter->next, argument = argument->next) {
2393 type_t *expected_type = parameter->type;
2394 /* TODO report context in error messages */
2395 argument->expression = create_implicit_cast(argument->expression,
2398 /* too few parameters */
2399 if(parameter != NULL) {
2400 parser_print_error_prefix();
2401 fprintf(stderr, "too few arguments to function '");
2402 print_expression(expression);
2403 fprintf(stderr, "'\n");
2404 } else if(argument != NULL) {
2405 /* too many parameters */
2406 if(!function_type->variadic
2407 && !function_type->unspecified_parameters) {
2408 parser_print_error_prefix();
2409 fprintf(stderr, "too many arguments to function '");
2410 print_expression(expression);
2411 fprintf(stderr, "'\n");
2413 /* do default promotion */
2414 for( ; argument != NULL; argument = argument->next) {
2415 type_t *type = argument->expression->datatype;
2417 if(is_type_integer(type)) {
2418 type = promote_integer(type);
2419 } else if(type == type_float) {
2422 argument->expression
2423 = create_implicit_cast(argument->expression, type);
2429 return (expression_t*) call;
2432 static type_t *get_type_after_conversion(const type_t *type1,
2433 const type_t *type2)
2437 return (type_t*) type1;
2440 static expression_t *parse_conditional_expression(unsigned precedence,
2441 expression_t *expression)
2445 conditional_expression_t *conditional
2446 = allocate_ast_zero(sizeof(conditional[0]));
2447 conditional->expression.type = EXPR_CONDITIONAL;
2448 conditional->condition = expression;
2451 type_t *condition_type = conditional->condition->datatype;
2452 if(condition_type != NULL) {
2453 if(!is_type_scalar(condition_type)) {
2454 type_error("expected a scalar type", expression->source_position,
2459 conditional->true_expression = parse_expression();
2461 conditional->false_expression = parse_sub_expression(precedence);
2463 type_t *true_type = conditional->true_expression->datatype;
2464 if(true_type == NULL)
2465 return (expression_t*) conditional;
2466 type_t *false_type = conditional->false_expression->datatype;
2467 if(false_type == NULL)
2468 return (expression_t*) conditional;
2471 if(true_type == false_type) {
2472 conditional->expression.datatype = true_type;
2473 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2474 type_t *result = get_type_after_conversion(true_type, false_type);
2475 /* TODO: create implicit convs if necessary */
2476 conditional->expression.datatype = result;
2477 } else if(true_type->type == TYPE_POINTER &&
2478 false_type->type == TYPE_POINTER &&
2479 true /* TODO compatible points_to types */) {
2481 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2482 || (is_null_ptr_const(false_type) &&
2483 true_type->type == TYPE_POINTER) TODO*/ false) {
2485 } else if(/* 1 is pointer to object type, other is void* */ false) {
2488 type_error_incompatible("problem while parsing conditional",
2489 expression->source_position, true_type,
2493 return (expression_t*) conditional;
2496 static expression_t *parse_extension(unsigned precedence)
2498 eat(T___extension__);
2500 /* TODO enable extensions */
2502 return parse_sub_expression(precedence);
2505 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2508 return expression->datatype;
2511 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2513 type_t *expression_type = expression->datatype;
2515 if(expression_type->type == TYPE_POINTER) {
2516 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2517 return pointer_type->points_to;
2519 panic("deref TODO...");
2523 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2525 type_t *type = expression->datatype;
2526 return make_pointer_type(type, 0);
2529 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2530 static expression_t *parse_##unexpression_type(unsigned precedence) \
2534 unary_expression_t *unary_expression \
2535 = allocate_ast_zero(sizeof(unary_expression[0])); \
2536 unary_expression->expression.type = EXPR_UNARY; \
2537 unary_expression->type = unexpression_type; \
2538 unary_expression->value = parse_sub_expression(precedence); \
2539 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2541 return (expression_t*) unary_expression; \
2544 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2545 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2546 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2547 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2548 get_unexpr_dereference_type)
2549 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2550 get_unexpr_take_addr_type)
2551 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2552 get_unexpr_arithmetic_type)
2553 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2554 get_unexpr_arithmetic_type)
2555 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2556 get_unexpr_arithmetic_type)
2558 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2560 static expression_t *parse_##unexpression_type(unsigned precedence, \
2561 expression_t *left) \
2563 (void) precedence; \
2566 unary_expression_t *unary_expression \
2567 = allocate_ast_zero(sizeof(unary_expression[0])); \
2568 unary_expression->expression.type = EXPR_UNARY; \
2569 unary_expression->type = unexpression_type; \
2570 unary_expression->value = left; \
2571 unary_expression->expression.datatype = tfunc(left); \
2573 return (expression_t*) unary_expression; \
2576 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2577 get_unexpr_arithmetic_type)
2578 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2579 get_unexpr_arithmetic_type)
2581 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2583 /* TODO: handle complex + imaginary types */
2585 /* § 6.3.1.8 Usual arithmetic conversions */
2586 if(type_left == type_long_double || type_right == type_long_double) {
2587 return type_long_double;
2588 } else if(type_left == type_double || type_right == type_double) {
2590 } else if(type_left == type_float || type_right == type_float) {
2594 type_right = promote_integer(type_right);
2595 type_left = promote_integer(type_left);
2597 if(type_left == type_right)
2600 bool signed_left = is_type_signed(type_left);
2601 bool signed_right = is_type_signed(type_right);
2602 if(get_rank(type_left) < get_rank(type_right)) {
2603 if(signed_left == signed_right || !signed_right) {
2609 if(signed_left == signed_right || !signed_left) {
2617 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2619 expression_t *left = expression->left;
2620 expression_t *right = expression->right;
2621 type_t *type_left = skip_typeref(left->datatype);
2622 type_t *type_right = skip_typeref(right->datatype);
2624 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2625 /* TODO: improve error message */
2626 parser_print_error_prefix();
2627 fprintf(stderr, "operation needs arithmetic types\n");
2631 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2632 expression->left = create_implicit_cast(left, arithmetic_type);
2633 expression->right = create_implicit_cast(right, arithmetic_type);
2634 expression->expression.datatype = arithmetic_type;
2637 static void semantic_add(binary_expression_t *expression)
2639 expression_t *left = expression->left;
2640 expression_t *right = expression->right;
2641 type_t *orig_type_left = left->datatype;
2642 type_t *orig_type_right = right->datatype;
2643 type_t *type_left = skip_typeref(orig_type_left);
2644 type_t *type_right = skip_typeref(orig_type_right);
2647 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2648 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2649 expression->left = create_implicit_cast(left, arithmetic_type);
2650 expression->right = create_implicit_cast(right, arithmetic_type);
2651 expression->expression.datatype = arithmetic_type;
2653 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2654 expression->expression.datatype = type_left;
2655 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2656 expression->expression.datatype = type_right;
2658 parser_print_error_prefix();
2659 fprintf(stderr, "invalid operands to binary + (");
2660 print_type(orig_type_left);
2661 fprintf(stderr, ", ");
2662 print_type(orig_type_right);
2663 fprintf(stderr, ")\n");
2667 static void semantic_sub(binary_expression_t *expression)
2669 expression_t *left = expression->left;
2670 expression_t *right = expression->right;
2671 type_t *orig_type_left = left->datatype;
2672 type_t *orig_type_right = right->datatype;
2673 type_t *type_left = skip_typeref(orig_type_left);
2674 type_t *type_right = skip_typeref(orig_type_right);
2677 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2678 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2679 expression->left = create_implicit_cast(left, arithmetic_type);
2680 expression->right = create_implicit_cast(right, arithmetic_type);
2681 expression->expression.datatype = arithmetic_type;
2683 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2684 expression->expression.datatype = type_left;
2685 } else if(type_left->type == TYPE_POINTER &&
2686 type_right->type == TYPE_POINTER) {
2687 if(!pointers_compatible(type_left, type_right)) {
2688 parser_print_error_prefix();
2689 fprintf(stderr, "pointers to incompatible objects to binary - (");
2690 print_type(orig_type_left);
2691 fprintf(stderr, ", ");
2692 print_type(orig_type_right);
2693 fprintf(stderr, ")\n");
2695 expression->expression.datatype = type_ptrdiff_t;
2698 parser_print_error_prefix();
2699 fprintf(stderr, "invalid operands to binary - (");
2700 print_type(orig_type_left);
2701 fprintf(stderr, ", ");
2702 print_type(orig_type_right);
2703 fprintf(stderr, ")\n");
2707 static void semantic_comparison(binary_expression_t *expression)
2709 expression_t *left = expression->left;
2710 expression_t *right = expression->right;
2711 type_t *type_left = left->datatype;
2712 type_t *type_right = right->datatype;
2714 /* TODO non-arithmetic types */
2715 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2716 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2717 expression->left = create_implicit_cast(left, arithmetic_type);
2718 expression->right = create_implicit_cast(right, arithmetic_type);
2719 expression->expression.datatype = arithmetic_type;
2721 expression->expression.datatype = type_int;
2724 static void semantic_arithmetic_assign(binary_expression_t *expression)
2726 expression_t *left = expression->left;
2727 expression_t *right = expression->right;
2728 type_t *type_left = left->datatype;
2729 type_t *type_right = right->datatype;
2731 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2732 /* TODO: improve error message */
2733 parser_print_error_prefix();
2734 fprintf(stderr, "operation needs arithmetic types\n");
2738 /* combined instructions are tricky. We can't create an implicit cast on
2739 * the left side, because we need the uncasted form for the store.
2740 * The ast2firm pass has to know that left_type must be right_type
2741 * for the arithmeitc operation and create a cast by itself */
2742 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2743 expression->right = create_implicit_cast(right, arithmetic_type);
2744 expression->expression.datatype = type_left;
2747 static void semantic_logical_op(binary_expression_t *expression)
2750 expression->expression.datatype = type_int;
2753 static void semantic_assign(type_t *orig_type_left, expression_t **right,
2756 type_t *orig_type_right = (*right)->datatype;
2757 type_t *type_left = skip_typeref(orig_type_left);
2758 type_t *type_right = skip_typeref(orig_type_right);
2760 if(type_left == type_right) {
2762 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2763 *right = create_implicit_cast(*right, type_left);
2764 } else if(type_left->type == TYPE_POINTER
2765 && type_right->type == TYPE_POINTER) {
2768 /* TODO: improve error message */
2769 parser_print_error_prefix();
2770 fprintf(stderr, "incompatible types in %s\n",
2771 is_return ? "'return'" : "assignment");
2772 parser_print_error_prefix();
2773 print_type(type_left);
2774 fputs(" <- ", stderr);
2775 print_type(type_right);
2776 fputs("\n", stderr);
2781 static void semantic_binexpr_assign(binary_expression_t *expression)
2783 expression_t *left = expression->left;
2784 type_t *type_left = left->datatype;
2786 semantic_assign(type_left, &expression->right, false);
2788 expression->expression.datatype = type_left;
2791 static void semantic_comma(binary_expression_t *expression)
2793 expression->expression.datatype = expression->right->datatype;
2796 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc) \
2797 static expression_t *parse_##binexpression_type(unsigned precedence, \
2798 expression_t *left) \
2802 expression_t *right = parse_sub_expression(precedence); \
2804 binary_expression_t *binexpr \
2805 = allocate_ast_zero(sizeof(binexpr[0])); \
2806 binexpr->expression.type = EXPR_BINARY; \
2807 binexpr->type = binexpression_type; \
2808 binexpr->left = left; \
2809 binexpr->right = right; \
2812 return (expression_t*) binexpr; \
2815 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma)
2816 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic)
2817 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic)
2818 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic)
2819 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add)
2820 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub)
2821 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison)
2822 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison)
2823 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign)
2824 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison)
2825 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2826 semantic_comparison)
2827 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison)
2828 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2829 semantic_comparison)
2830 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic)
2831 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic)
2832 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic)
2833 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op)
2834 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op)
2835 /* TODO shift has a bit special semantic */
2836 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2837 semantic_binexpr_arithmetic)
2838 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2839 semantic_binexpr_arithmetic)
2840 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2841 semantic_arithmetic_assign)
2842 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2843 semantic_arithmetic_assign)
2844 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2845 semantic_arithmetic_assign)
2846 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2847 semantic_arithmetic_assign)
2848 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2849 semantic_arithmetic_assign)
2850 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2851 semantic_arithmetic_assign)
2852 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2853 semantic_arithmetic_assign)
2854 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2855 semantic_arithmetic_assign)
2856 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2857 semantic_arithmetic_assign)
2858 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2859 semantic_arithmetic_assign)
2861 static expression_t *parse_sub_expression(unsigned precedence)
2863 if(token.type < 0) {
2864 return expected_expression_error();
2867 expression_parser_function_t *parser
2868 = &expression_parsers[token.type];
2869 source_position_t source_position = token.source_position;
2872 if(parser->parser != NULL) {
2873 left = parser->parser(parser->precedence);
2875 left = parse_primary_expression();
2877 assert(left != NULL);
2878 left->source_position = source_position;
2881 if(token.type < 0) {
2882 return expected_expression_error();
2885 parser = &expression_parsers[token.type];
2886 if(parser->infix_parser == NULL)
2888 if(parser->infix_precedence < precedence)
2891 left = parser->infix_parser(parser->infix_precedence, left);
2893 assert(left != NULL);
2894 assert(left->type != EXPR_INVALID);
2895 left->source_position = source_position;
2901 static expression_t *parse_expression(void)
2903 return parse_sub_expression(1);
2908 static void register_expression_parser(parse_expression_function parser,
2909 int token_type, unsigned precedence)
2911 expression_parser_function_t *entry = &expression_parsers[token_type];
2913 if(entry->parser != NULL) {
2914 fprintf(stderr, "for token ");
2915 print_token_type(stderr, token_type);
2916 fprintf(stderr, "\n");
2917 panic("trying to register multiple expression parsers for a token");
2919 entry->parser = parser;
2920 entry->precedence = precedence;
2923 static void register_expression_infix_parser(
2924 parse_expression_infix_function parser, int token_type,
2925 unsigned precedence)
2927 expression_parser_function_t *entry = &expression_parsers[token_type];
2929 if(entry->infix_parser != NULL) {
2930 fprintf(stderr, "for token ");
2931 print_token_type(stderr, token_type);
2932 fprintf(stderr, "\n");
2933 panic("trying to register multiple infix expression parsers for a "
2936 entry->infix_parser = parser;
2937 entry->infix_precedence = precedence;
2940 static void init_expression_parsers(void)
2942 memset(&expression_parsers, 0, sizeof(expression_parsers));
2944 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2945 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2946 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2947 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2948 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2949 T_GREATERGREATER, 16);
2950 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2951 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2952 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2953 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2954 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2955 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2956 T_GREATEREQUAL, 14);
2957 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2958 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2959 T_EXCLAMATIONMARKEQUAL, 13);
2960 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2961 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2962 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2963 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2964 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2965 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2966 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2967 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2968 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2969 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2970 T_ASTERISKEQUAL, 2);
2971 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2972 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2974 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2975 T_LESSLESSEQUAL, 2);
2976 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2977 T_GREATERGREATEREQUAL, 2);
2978 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2980 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2982 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2985 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2987 register_expression_infix_parser(parse_array_expression, '[', 30);
2988 register_expression_infix_parser(parse_call_expression, '(', 30);
2989 register_expression_infix_parser(parse_select_expression, '.', 30);
2990 register_expression_infix_parser(parse_select_expression,
2991 T_MINUSGREATER, 30);
2992 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2994 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2997 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2998 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2999 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3000 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3001 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3002 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3003 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3004 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3005 register_expression_parser(parse_sizeof, T_sizeof, 25);
3006 register_expression_parser(parse_extension, T___extension__, 25);
3010 static statement_t *parse_case_statement(void)
3013 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3014 label->statement.type = STATEMENT_CASE_LABEL;
3015 label->statement.source_position = token.source_position;
3017 label->expression = parse_expression();
3020 label->statement.next = parse_statement();
3022 return (statement_t*) label;
3025 static statement_t *parse_default_statement(void)
3029 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3030 label->statement.type = STATEMENT_CASE_LABEL;
3031 label->statement.source_position = token.source_position;
3034 label->statement.next = parse_statement();
3036 return (statement_t*) label;
3039 static statement_t *parse_label_statement(void)
3048 static statement_t *parse_if(void)
3052 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3053 statement->statement.type = STATEMENT_IF;
3054 statement->statement.source_position = token.source_position;
3057 statement->condition = parse_expression();
3060 statement->true_statement = parse_statement();
3061 if(token.type == T_else) {
3063 statement->false_statement = parse_statement();
3066 return (statement_t*) statement;
3069 static statement_t *parse_switch(void)
3073 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3074 statement->statement.type = STATEMENT_SWITCH;
3075 statement->statement.source_position = token.source_position;
3078 statement->expression = parse_expression();
3080 statement->body = parse_statement();
3082 return (statement_t*) statement;
3085 static statement_t *parse_while(void)
3089 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3090 statement->statement.type = STATEMENT_WHILE;
3091 statement->statement.source_position = token.source_position;
3094 statement->condition = parse_expression();
3096 statement->body = parse_statement();
3098 return (statement_t*) statement;
3101 static statement_t *parse_do(void)
3105 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3106 statement->statement.type = STATEMENT_DO_WHILE;
3107 statement->statement.source_position = token.source_position;
3109 statement->body = parse_statement();
3112 statement->condition = parse_expression();
3116 return (statement_t*) statement;
3119 static statement_t *parse_for(void)
3123 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3124 statement->statement.type = STATEMENT_FOR;
3125 statement->statement.source_position = token.source_position;
3129 int top = environment_top();
3130 context_t *last_context = context;
3131 set_context(&statement->context);
3133 if(token.type != ';') {
3134 if(is_declaration_specifier(&token, false)) {
3135 parse_declaration();
3137 statement->initialisation = parse_expression();
3144 if(token.type != ';') {
3145 statement->condition = parse_expression();
3148 if(token.type != ')') {
3149 statement->step = parse_expression();
3152 statement->body = parse_statement();
3154 assert(context == &statement->context);
3155 set_context(last_context);
3156 environment_pop_to(top);
3158 return (statement_t*) statement;
3161 static statement_t *parse_goto(void)
3164 expect(T_IDENTIFIER);
3170 static statement_t *parse_continue(void)
3175 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3176 statement->source_position = token.source_position;
3177 statement->type = STATEMENT_CONTINUE;
3182 static statement_t *parse_break(void)
3187 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3188 statement->source_position = token.source_position;
3189 statement->type = STATEMENT_BREAK;
3194 static statement_t *parse_return(void)
3198 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3200 statement->statement.type = STATEMENT_RETURN;
3201 statement->statement.source_position = token.source_position;
3203 assert(current_function->type->type == TYPE_FUNCTION);
3204 function_type_t *function_type = (function_type_t*) current_function->type;
3205 type_t *return_type = function_type->result_type;
3207 expression_t *return_value;
3208 if(token.type != ';') {
3209 return_value = parse_expression();
3211 if(return_type == type_void && return_value->datatype != type_void) {
3212 parse_warning("'return' with a value, in function returning void");
3213 return_value = NULL;
3215 semantic_assign(return_type, &return_value, true);
3218 return_value = NULL;
3219 parse_warning("'return' without value, in function retruning non-void");
3221 statement->return_value = return_value;
3225 return (statement_t*) statement;
3228 static statement_t *parse_declaration_statement(void)
3230 declaration_t *before = last_declaration;
3232 declaration_statement_t *statement
3233 = allocate_ast_zero(sizeof(statement[0]));
3234 statement->statement.type = STATEMENT_DECLARATION;
3235 statement->statement.source_position = token.source_position;
3237 declaration_specifiers_t specifiers;
3238 memset(&specifiers, 0, sizeof(specifiers));
3239 parse_declaration_specifiers(&specifiers);
3241 if(token.type == ';') {
3244 parse_init_declarators(&specifiers);
3247 if(before == NULL) {
3248 statement->declarations_begin = context->declarations;
3250 statement->declarations_begin = before->next;
3252 statement->declarations_end = last_declaration;
3254 return (statement_t*) statement;
3257 static statement_t *parse_expression_statement(void)
3259 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3260 statement->statement.type = STATEMENT_EXPRESSION;
3261 statement->statement.source_position = token.source_position;
3263 statement->expression = parse_expression();
3267 return (statement_t*) statement;
3270 static statement_t *parse_statement(void)
3272 statement_t *statement = NULL;
3274 /* declaration or statement */
3275 switch(token.type) {
3277 statement = parse_case_statement();
3281 statement = parse_default_statement();
3285 statement = parse_compound_statement();
3289 statement = parse_if();
3293 statement = parse_switch();
3297 statement = parse_while();
3301 statement = parse_do();
3305 statement = parse_for();
3309 statement = parse_goto();
3313 statement = parse_continue();
3317 statement = parse_break();
3321 statement = parse_return();
3330 if(look_ahead(1)->type == ':') {
3331 statement = parse_label_statement();
3335 if(is_typedef_symbol(token.v.symbol)) {
3336 statement = parse_declaration_statement();
3340 statement = parse_expression_statement();
3343 case T___extension__:
3344 /* this can be a prefix to a declaration or an expression statement */
3345 /* we simply eat it now and parse the rest with tail recursion */
3348 } while(token.type == T___extension__);
3349 statement = parse_statement();
3353 statement = parse_declaration_statement();
3357 statement = parse_expression_statement();
3361 assert(statement == NULL || statement->source_position.input_name != NULL);
3366 static statement_t *parse_compound_statement(void)
3370 compound_statement_t *compound_statement
3371 = allocate_ast_zero(sizeof(compound_statement[0]));
3372 compound_statement->statement.type = STATEMENT_COMPOUND;
3373 compound_statement->statement.source_position = token.source_position;
3375 int top = environment_top();
3376 context_t *last_context = context;
3377 set_context(&compound_statement->context);
3379 statement_t *last_statement = NULL;
3381 while(token.type != '}' && token.type != T_EOF) {
3382 statement_t *statement = parse_statement();
3383 if(statement == NULL)
3386 if(last_statement != NULL) {
3387 last_statement->next = statement;
3389 compound_statement->statements = statement;
3392 while(statement->next != NULL)
3393 statement = statement->next;
3395 last_statement = statement;
3398 assert(context == &compound_statement->context);
3399 set_context(last_context);
3400 environment_pop_to(top);
3404 return (statement_t*) compound_statement;
3407 static translation_unit_t *parse_translation_unit(void)
3409 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3411 assert(global_context == NULL);
3412 global_context = &unit->context;
3414 assert(context == NULL);
3415 set_context(&unit->context);
3417 while(token.type != T_EOF) {
3418 parse_declaration();
3421 assert(context == &unit->context);
3423 last_declaration = NULL;
3425 assert(global_context == &unit->context);
3426 global_context = NULL;
3431 translation_unit_t *parse(void)
3433 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3434 found_error = false;
3436 type_set_output(stderr);
3437 ast_set_output(stderr);
3439 lookahead_bufpos = 0;
3440 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3443 translation_unit_t *unit = parse_translation_unit();
3445 DEL_ARR_F(environment_stack);
3453 void init_parser(void)
3455 init_expression_parsers();
3456 obstack_init(&temp_obst);
3458 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3459 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3460 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3461 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3462 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3463 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3464 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3465 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3466 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3467 type_string = make_pointer_type(type_const_char, 0);
3470 void exit_parser(void)
3472 obstack_free(&temp_obst, NULL);