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);
2334 if(dest_type->type == TYPE_POINTER) {
2335 if(source_type->type == TYPE_POINTER) {
2336 if(!pointers_compatible(source_type, dest_type)) {
2337 type_error_incompatible("can't implicitely cast types",
2338 expression->source_position,
2339 source_type, dest_type);
2341 return create_cast_expression(expression, dest_type);
2346 panic("casting of non-atomic types not implemented yet");
2349 static expression_t *parse_call_expression(unsigned precedence,
2350 expression_t *expression)
2353 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2354 call->expression.type = EXPR_CALL;
2355 call->function = expression;
2357 function_type_t *function_type;
2358 type_t *type = expression->datatype;
2359 if(type->type != TYPE_FUNCTION) {
2360 /* TODO calling pointers to functions is ok */
2361 parser_print_error_prefix();
2362 fputs("called object '", stderr);
2363 print_expression(expression);
2364 fputs("' (type ", stderr);
2366 fputs("is not a function\n", stderr);
2368 function_type = NULL;
2369 call->expression.datatype = NULL;
2371 function_type = (function_type_t*) type;
2372 call->expression.datatype = function_type->result_type;
2375 /* parse arguments */
2378 if(token.type != ')') {
2379 call_argument_t *last_argument = NULL;
2382 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2384 argument->expression = parse_assignment_expression();
2385 if(last_argument == NULL) {
2386 call->arguments = argument;
2388 last_argument->next = argument;
2390 last_argument = argument;
2392 if(token.type != ',')
2399 if(function_type != NULL) {
2400 function_parameter_t *parameter = function_type->parameters;
2401 call_argument_t *argument = call->arguments;
2402 for( ; parameter != NULL && argument != NULL;
2403 parameter = parameter->next, argument = argument->next) {
2404 type_t *expected_type = parameter->type;
2405 /* TODO report context in error messages */
2406 argument->expression = create_implicit_cast(argument->expression,
2409 /* too few parameters */
2410 if(parameter != NULL) {
2411 parser_print_error_prefix();
2412 fprintf(stderr, "too few arguments to function '");
2413 print_expression(expression);
2414 fprintf(stderr, "'\n");
2415 } else if(argument != NULL) {
2416 /* too many parameters */
2417 if(!function_type->variadic
2418 && !function_type->unspecified_parameters) {
2419 parser_print_error_prefix();
2420 fprintf(stderr, "too many arguments to function '");
2421 print_expression(expression);
2422 fprintf(stderr, "'\n");
2424 /* do default promotion */
2425 for( ; argument != NULL; argument = argument->next) {
2426 type_t *type = argument->expression->datatype;
2428 if(is_type_integer(type)) {
2429 type = promote_integer(type);
2430 } else if(type == type_float) {
2433 argument->expression
2434 = create_implicit_cast(argument->expression, type);
2440 return (expression_t*) call;
2443 static type_t *get_type_after_conversion(const type_t *type1,
2444 const type_t *type2)
2448 return (type_t*) type1;
2451 static expression_t *parse_conditional_expression(unsigned precedence,
2452 expression_t *expression)
2456 conditional_expression_t *conditional
2457 = allocate_ast_zero(sizeof(conditional[0]));
2458 conditional->expression.type = EXPR_CONDITIONAL;
2459 conditional->condition = expression;
2462 type_t *condition_type = conditional->condition->datatype;
2463 if(condition_type != NULL) {
2464 if(!is_type_scalar(condition_type)) {
2465 type_error("expected a scalar type", expression->source_position,
2470 conditional->true_expression = parse_expression();
2472 conditional->false_expression = parse_sub_expression(precedence);
2474 type_t *true_type = conditional->true_expression->datatype;
2475 if(true_type == NULL)
2476 return (expression_t*) conditional;
2477 type_t *false_type = conditional->false_expression->datatype;
2478 if(false_type == NULL)
2479 return (expression_t*) conditional;
2482 if(true_type == false_type) {
2483 conditional->expression.datatype = true_type;
2484 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2485 type_t *result = get_type_after_conversion(true_type, false_type);
2486 /* TODO: create implicit convs if necessary */
2487 conditional->expression.datatype = result;
2488 } else if(true_type->type == TYPE_POINTER &&
2489 false_type->type == TYPE_POINTER &&
2490 true /* TODO compatible points_to types */) {
2492 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2493 || (is_null_ptr_const(false_type) &&
2494 true_type->type == TYPE_POINTER) TODO*/ false) {
2496 } else if(/* 1 is pointer to object type, other is void* */ false) {
2499 type_error_incompatible("problem while parsing conditional",
2500 expression->source_position, true_type,
2504 return (expression_t*) conditional;
2507 static expression_t *parse_extension(unsigned precedence)
2509 eat(T___extension__);
2511 /* TODO enable extensions */
2513 return parse_sub_expression(precedence);
2516 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2519 return expression->datatype;
2522 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2524 type_t *expression_type = expression->datatype;
2526 if(expression_type->type == TYPE_POINTER) {
2527 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2528 return pointer_type->points_to;
2530 panic("deref TODO...");
2534 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2536 type_t *type = expression->datatype;
2537 return make_pointer_type(type, 0);
2540 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2541 static expression_t *parse_##unexpression_type(unsigned precedence) \
2545 unary_expression_t *unary_expression \
2546 = allocate_ast_zero(sizeof(unary_expression[0])); \
2547 unary_expression->expression.type = EXPR_UNARY; \
2548 unary_expression->type = unexpression_type; \
2549 unary_expression->value = parse_sub_expression(precedence); \
2550 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2552 return (expression_t*) unary_expression; \
2555 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2556 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2557 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2558 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2559 get_unexpr_dereference_type)
2560 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2561 get_unexpr_take_addr_type)
2562 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2563 get_unexpr_arithmetic_type)
2564 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2565 get_unexpr_arithmetic_type)
2566 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2567 get_unexpr_arithmetic_type)
2569 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2571 static expression_t *parse_##unexpression_type(unsigned precedence, \
2572 expression_t *left) \
2574 (void) precedence; \
2577 unary_expression_t *unary_expression \
2578 = allocate_ast_zero(sizeof(unary_expression[0])); \
2579 unary_expression->expression.type = EXPR_UNARY; \
2580 unary_expression->type = unexpression_type; \
2581 unary_expression->value = left; \
2582 unary_expression->expression.datatype = tfunc(left); \
2584 return (expression_t*) unary_expression; \
2587 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2588 get_unexpr_arithmetic_type)
2589 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2590 get_unexpr_arithmetic_type)
2592 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2594 /* TODO: handle complex + imaginary types */
2596 /* § 6.3.1.8 Usual arithmetic conversions */
2597 if(type_left == type_long_double || type_right == type_long_double) {
2598 return type_long_double;
2599 } else if(type_left == type_double || type_right == type_double) {
2601 } else if(type_left == type_float || type_right == type_float) {
2605 type_right = promote_integer(type_right);
2606 type_left = promote_integer(type_left);
2608 if(type_left == type_right)
2611 bool signed_left = is_type_signed(type_left);
2612 bool signed_right = is_type_signed(type_right);
2613 if(get_rank(type_left) < get_rank(type_right)) {
2614 if(signed_left == signed_right || !signed_right) {
2620 if(signed_left == signed_right || !signed_left) {
2628 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2630 expression_t *left = expression->left;
2631 expression_t *right = expression->right;
2632 type_t *type_left = skip_typeref(left->datatype);
2633 type_t *type_right = skip_typeref(right->datatype);
2635 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2636 /* TODO: improve error message */
2637 parser_print_error_prefix();
2638 fprintf(stderr, "operation needs arithmetic types\n");
2642 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2643 expression->left = create_implicit_cast(left, arithmetic_type);
2644 expression->right = create_implicit_cast(right, arithmetic_type);
2645 expression->expression.datatype = arithmetic_type;
2648 static void semantic_add(binary_expression_t *expression)
2650 expression_t *left = expression->left;
2651 expression_t *right = expression->right;
2652 type_t *orig_type_left = left->datatype;
2653 type_t *orig_type_right = right->datatype;
2654 type_t *type_left = skip_typeref(orig_type_left);
2655 type_t *type_right = skip_typeref(orig_type_right);
2658 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2659 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2660 expression->left = create_implicit_cast(left, arithmetic_type);
2661 expression->right = create_implicit_cast(right, arithmetic_type);
2662 expression->expression.datatype = arithmetic_type;
2664 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2665 expression->expression.datatype = type_left;
2666 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2667 expression->expression.datatype = type_right;
2669 parser_print_error_prefix();
2670 fprintf(stderr, "invalid operands to binary + (");
2671 print_type(orig_type_left);
2672 fprintf(stderr, ", ");
2673 print_type(orig_type_right);
2674 fprintf(stderr, ")\n");
2678 static void semantic_sub(binary_expression_t *expression)
2680 expression_t *left = expression->left;
2681 expression_t *right = expression->right;
2682 type_t *orig_type_left = left->datatype;
2683 type_t *orig_type_right = right->datatype;
2684 type_t *type_left = skip_typeref(orig_type_left);
2685 type_t *type_right = skip_typeref(orig_type_right);
2688 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2689 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2690 expression->left = create_implicit_cast(left, arithmetic_type);
2691 expression->right = create_implicit_cast(right, arithmetic_type);
2692 expression->expression.datatype = arithmetic_type;
2694 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2695 expression->expression.datatype = type_left;
2696 } else if(type_left->type == TYPE_POINTER &&
2697 type_right->type == TYPE_POINTER) {
2698 if(!pointers_compatible(type_left, type_right)) {
2699 parser_print_error_prefix();
2700 fprintf(stderr, "pointers to incompatible objects to binary - (");
2701 print_type(orig_type_left);
2702 fprintf(stderr, ", ");
2703 print_type(orig_type_right);
2704 fprintf(stderr, ")\n");
2706 expression->expression.datatype = type_ptrdiff_t;
2709 parser_print_error_prefix();
2710 fprintf(stderr, "invalid operands to binary - (");
2711 print_type(orig_type_left);
2712 fprintf(stderr, ", ");
2713 print_type(orig_type_right);
2714 fprintf(stderr, ")\n");
2718 static void semantic_comparison(binary_expression_t *expression)
2720 expression_t *left = expression->left;
2721 expression_t *right = expression->right;
2722 type_t *type_left = left->datatype;
2723 type_t *type_right = right->datatype;
2725 /* TODO non-arithmetic types */
2726 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2727 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2728 expression->left = create_implicit_cast(left, arithmetic_type);
2729 expression->right = create_implicit_cast(right, arithmetic_type);
2730 expression->expression.datatype = arithmetic_type;
2732 expression->expression.datatype = type_int;
2735 static void semantic_arithmetic_assign(binary_expression_t *expression)
2737 expression_t *left = expression->left;
2738 expression_t *right = expression->right;
2739 type_t *type_left = left->datatype;
2740 type_t *type_right = right->datatype;
2742 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2743 /* TODO: improve error message */
2744 parser_print_error_prefix();
2745 fprintf(stderr, "operation needs arithmetic types\n");
2749 /* combined instructions are tricky. We can't create an implicit cast on
2750 * the left side, because we need the uncasted form for the store.
2751 * The ast2firm pass has to know that left_type must be right_type
2752 * for the arithmeitc operation and create a cast by itself */
2753 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2754 expression->right = create_implicit_cast(right, arithmetic_type);
2755 expression->expression.datatype = type_left;
2758 static void semantic_logical_op(binary_expression_t *expression)
2761 expression->expression.datatype = type_int;
2764 static void semantic_assign(type_t *orig_type_left, expression_t **right,
2767 type_t *orig_type_right = (*right)->datatype;
2768 type_t *type_left = skip_typeref(orig_type_left);
2769 type_t *type_right = skip_typeref(orig_type_right);
2771 if(type_left == type_right) {
2773 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2774 *right = create_implicit_cast(*right, type_left);
2775 } else if(type_left->type == TYPE_POINTER
2776 && type_right->type == TYPE_POINTER) {
2779 /* TODO: improve error message */
2780 parser_print_error_prefix();
2781 fprintf(stderr, "incompatible types in %s\n",
2782 is_return ? "'return'" : "assignment");
2783 parser_print_error_prefix();
2784 print_type(type_left);
2785 fputs(" <- ", stderr);
2786 print_type(type_right);
2787 fputs("\n", stderr);
2792 static void semantic_binexpr_assign(binary_expression_t *expression)
2794 expression_t *left = expression->left;
2795 type_t *type_left = left->datatype;
2797 semantic_assign(type_left, &expression->right, false);
2799 expression->expression.datatype = type_left;
2802 static void semantic_comma(binary_expression_t *expression)
2804 expression->expression.datatype = expression->right->datatype;
2807 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2808 static expression_t *parse_##binexpression_type(unsigned precedence, \
2809 expression_t *left) \
2813 expression_t *right = parse_sub_expression(precedence + lr); \
2815 binary_expression_t *binexpr \
2816 = allocate_ast_zero(sizeof(binexpr[0])); \
2817 binexpr->expression.type = EXPR_BINARY; \
2818 binexpr->type = binexpression_type; \
2819 binexpr->left = left; \
2820 binexpr->right = right; \
2823 return (expression_t*) binexpr; \
2826 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
2827 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
2828 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
2829 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
2830 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
2831 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
2832 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
2833 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
2834 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
2835 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
2836 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2837 semantic_comparison, 1)
2838 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
2839 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2840 semantic_comparison, 1)
2841 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
2842 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
2843 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
2844 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
2845 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
2846 /* TODO shift has a bit special semantic */
2847 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2848 semantic_binexpr_arithmetic, 1)
2849 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2850 semantic_binexpr_arithmetic, 1)
2851 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2852 semantic_arithmetic_assign, 0)
2853 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2854 semantic_arithmetic_assign, 0)
2855 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2856 semantic_arithmetic_assign, 0)
2857 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2858 semantic_arithmetic_assign, 0)
2859 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2860 semantic_arithmetic_assign, 0)
2861 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2862 semantic_arithmetic_assign, 0)
2863 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2864 semantic_arithmetic_assign, 0)
2865 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2866 semantic_arithmetic_assign, 0)
2867 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2868 semantic_arithmetic_assign, 0)
2869 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2870 semantic_arithmetic_assign, 0)
2872 static expression_t *parse_sub_expression(unsigned precedence)
2874 if(token.type < 0) {
2875 return expected_expression_error();
2878 expression_parser_function_t *parser
2879 = &expression_parsers[token.type];
2880 source_position_t source_position = token.source_position;
2883 if(parser->parser != NULL) {
2884 left = parser->parser(parser->precedence);
2886 left = parse_primary_expression();
2888 assert(left != NULL);
2889 left->source_position = source_position;
2892 if(token.type < 0) {
2893 return expected_expression_error();
2896 parser = &expression_parsers[token.type];
2897 if(parser->infix_parser == NULL)
2899 if(parser->infix_precedence < precedence)
2902 left = parser->infix_parser(parser->infix_precedence, left);
2904 assert(left != NULL);
2905 assert(left->type != EXPR_INVALID);
2906 left->source_position = source_position;
2912 static expression_t *parse_expression(void)
2914 return parse_sub_expression(1);
2919 static void register_expression_parser(parse_expression_function parser,
2920 int token_type, unsigned precedence)
2922 expression_parser_function_t *entry = &expression_parsers[token_type];
2924 if(entry->parser != NULL) {
2925 fprintf(stderr, "for token ");
2926 print_token_type(stderr, token_type);
2927 fprintf(stderr, "\n");
2928 panic("trying to register multiple expression parsers for a token");
2930 entry->parser = parser;
2931 entry->precedence = precedence;
2934 static void register_expression_infix_parser(
2935 parse_expression_infix_function parser, int token_type,
2936 unsigned precedence)
2938 expression_parser_function_t *entry = &expression_parsers[token_type];
2940 if(entry->infix_parser != NULL) {
2941 fprintf(stderr, "for token ");
2942 print_token_type(stderr, token_type);
2943 fprintf(stderr, "\n");
2944 panic("trying to register multiple infix expression parsers for a "
2947 entry->infix_parser = parser;
2948 entry->infix_precedence = precedence;
2951 static void init_expression_parsers(void)
2953 memset(&expression_parsers, 0, sizeof(expression_parsers));
2955 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2956 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2957 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2958 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2959 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2960 T_GREATERGREATER, 16);
2961 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2962 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2963 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2964 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2965 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2966 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2967 T_GREATEREQUAL, 14);
2968 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2969 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2970 T_EXCLAMATIONMARKEQUAL, 13);
2971 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2972 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2973 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2974 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2975 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2976 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2977 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2978 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2979 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2980 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2981 T_ASTERISKEQUAL, 2);
2982 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2983 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2985 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2986 T_LESSLESSEQUAL, 2);
2987 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2988 T_GREATERGREATEREQUAL, 2);
2989 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2991 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2993 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2996 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2998 register_expression_infix_parser(parse_array_expression, '[', 30);
2999 register_expression_infix_parser(parse_call_expression, '(', 30);
3000 register_expression_infix_parser(parse_select_expression, '.', 30);
3001 register_expression_infix_parser(parse_select_expression,
3002 T_MINUSGREATER, 30);
3003 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3005 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3008 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3009 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3010 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3011 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3012 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3013 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3014 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3015 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3016 register_expression_parser(parse_sizeof, T_sizeof, 25);
3017 register_expression_parser(parse_extension, T___extension__, 25);
3021 static statement_t *parse_case_statement(void)
3024 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3025 label->statement.type = STATEMENT_CASE_LABEL;
3026 label->statement.source_position = token.source_position;
3028 label->expression = parse_expression();
3031 label->statement.next = parse_statement();
3033 return (statement_t*) label;
3036 static statement_t *parse_default_statement(void)
3040 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3041 label->statement.type = STATEMENT_CASE_LABEL;
3042 label->statement.source_position = token.source_position;
3045 label->statement.next = parse_statement();
3047 return (statement_t*) label;
3050 static statement_t *parse_label_statement(void)
3059 static statement_t *parse_if(void)
3063 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3064 statement->statement.type = STATEMENT_IF;
3065 statement->statement.source_position = token.source_position;
3068 statement->condition = parse_expression();
3071 statement->true_statement = parse_statement();
3072 if(token.type == T_else) {
3074 statement->false_statement = parse_statement();
3077 return (statement_t*) statement;
3080 static statement_t *parse_switch(void)
3084 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3085 statement->statement.type = STATEMENT_SWITCH;
3086 statement->statement.source_position = token.source_position;
3089 statement->expression = parse_expression();
3091 statement->body = parse_statement();
3093 return (statement_t*) statement;
3096 static statement_t *parse_while(void)
3100 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3101 statement->statement.type = STATEMENT_WHILE;
3102 statement->statement.source_position = token.source_position;
3105 statement->condition = parse_expression();
3107 statement->body = parse_statement();
3109 return (statement_t*) statement;
3112 static statement_t *parse_do(void)
3116 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3117 statement->statement.type = STATEMENT_DO_WHILE;
3118 statement->statement.source_position = token.source_position;
3120 statement->body = parse_statement();
3123 statement->condition = parse_expression();
3127 return (statement_t*) statement;
3130 static statement_t *parse_for(void)
3134 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3135 statement->statement.type = STATEMENT_FOR;
3136 statement->statement.source_position = token.source_position;
3140 int top = environment_top();
3141 context_t *last_context = context;
3142 set_context(&statement->context);
3144 if(token.type != ';') {
3145 if(is_declaration_specifier(&token, false)) {
3146 parse_declaration();
3148 statement->initialisation = parse_expression();
3155 if(token.type != ';') {
3156 statement->condition = parse_expression();
3159 if(token.type != ')') {
3160 statement->step = parse_expression();
3163 statement->body = parse_statement();
3165 assert(context == &statement->context);
3166 set_context(last_context);
3167 environment_pop_to(top);
3169 return (statement_t*) statement;
3172 static statement_t *parse_goto(void)
3175 expect(T_IDENTIFIER);
3181 static statement_t *parse_continue(void)
3186 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3187 statement->source_position = token.source_position;
3188 statement->type = STATEMENT_CONTINUE;
3193 static statement_t *parse_break(void)
3198 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3199 statement->source_position = token.source_position;
3200 statement->type = STATEMENT_BREAK;
3205 static statement_t *parse_return(void)
3209 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3211 statement->statement.type = STATEMENT_RETURN;
3212 statement->statement.source_position = token.source_position;
3214 assert(current_function->type->type == TYPE_FUNCTION);
3215 function_type_t *function_type = (function_type_t*) current_function->type;
3216 type_t *return_type = function_type->result_type;
3218 expression_t *return_value;
3219 if(token.type != ';') {
3220 return_value = parse_expression();
3222 if(return_type == type_void && return_value->datatype != type_void) {
3223 parse_warning("'return' with a value, in function returning void");
3224 return_value = NULL;
3226 semantic_assign(return_type, &return_value, true);
3229 return_value = NULL;
3230 if(return_type != type_void) {
3231 parse_warning("'return' without value, in function returning "
3235 statement->return_value = return_value;
3239 return (statement_t*) statement;
3242 static statement_t *parse_declaration_statement(void)
3244 declaration_t *before = last_declaration;
3246 declaration_statement_t *statement
3247 = allocate_ast_zero(sizeof(statement[0]));
3248 statement->statement.type = STATEMENT_DECLARATION;
3249 statement->statement.source_position = token.source_position;
3251 declaration_specifiers_t specifiers;
3252 memset(&specifiers, 0, sizeof(specifiers));
3253 parse_declaration_specifiers(&specifiers);
3255 if(token.type == ';') {
3258 parse_init_declarators(&specifiers);
3261 if(before == NULL) {
3262 statement->declarations_begin = context->declarations;
3264 statement->declarations_begin = before->next;
3266 statement->declarations_end = last_declaration;
3268 return (statement_t*) statement;
3271 static statement_t *parse_expression_statement(void)
3273 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3274 statement->statement.type = STATEMENT_EXPRESSION;
3275 statement->statement.source_position = token.source_position;
3277 statement->expression = parse_expression();
3281 return (statement_t*) statement;
3284 static statement_t *parse_statement(void)
3286 statement_t *statement = NULL;
3288 /* declaration or statement */
3289 switch(token.type) {
3291 statement = parse_case_statement();
3295 statement = parse_default_statement();
3299 statement = parse_compound_statement();
3303 statement = parse_if();
3307 statement = parse_switch();
3311 statement = parse_while();
3315 statement = parse_do();
3319 statement = parse_for();
3323 statement = parse_goto();
3327 statement = parse_continue();
3331 statement = parse_break();
3335 statement = parse_return();
3344 if(look_ahead(1)->type == ':') {
3345 statement = parse_label_statement();
3349 if(is_typedef_symbol(token.v.symbol)) {
3350 statement = parse_declaration_statement();
3354 statement = parse_expression_statement();
3357 case T___extension__:
3358 /* this can be a prefix to a declaration or an expression statement */
3359 /* we simply eat it now and parse the rest with tail recursion */
3362 } while(token.type == T___extension__);
3363 statement = parse_statement();
3367 statement = parse_declaration_statement();
3371 statement = parse_expression_statement();
3375 assert(statement == NULL || statement->source_position.input_name != NULL);
3380 static statement_t *parse_compound_statement(void)
3384 compound_statement_t *compound_statement
3385 = allocate_ast_zero(sizeof(compound_statement[0]));
3386 compound_statement->statement.type = STATEMENT_COMPOUND;
3387 compound_statement->statement.source_position = token.source_position;
3389 int top = environment_top();
3390 context_t *last_context = context;
3391 set_context(&compound_statement->context);
3393 statement_t *last_statement = NULL;
3395 while(token.type != '}' && token.type != T_EOF) {
3396 statement_t *statement = parse_statement();
3397 if(statement == NULL)
3400 if(last_statement != NULL) {
3401 last_statement->next = statement;
3403 compound_statement->statements = statement;
3406 while(statement->next != NULL)
3407 statement = statement->next;
3409 last_statement = statement;
3412 assert(context == &compound_statement->context);
3413 set_context(last_context);
3414 environment_pop_to(top);
3418 return (statement_t*) compound_statement;
3421 static translation_unit_t *parse_translation_unit(void)
3423 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3425 assert(global_context == NULL);
3426 global_context = &unit->context;
3428 assert(context == NULL);
3429 set_context(&unit->context);
3431 while(token.type != T_EOF) {
3432 parse_declaration();
3435 assert(context == &unit->context);
3437 last_declaration = NULL;
3439 assert(global_context == &unit->context);
3440 global_context = NULL;
3445 translation_unit_t *parse(void)
3447 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3448 found_error = false;
3450 type_set_output(stderr);
3451 ast_set_output(stderr);
3453 lookahead_bufpos = 0;
3454 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3457 translation_unit_t *unit = parse_translation_unit();
3459 DEL_ARR_F(environment_stack);
3467 void init_parser(void)
3469 init_expression_parsers();
3470 obstack_init(&temp_obst);
3472 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3473 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3474 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3475 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3476 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3477 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3478 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3479 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3480 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3481 type_string = make_pointer_type(type_const_char, 0);
3484 void exit_parser(void)
3486 obstack_free(&temp_obst, NULL);