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 *parse_call_expression(unsigned precedence,
2254 expression_t *expression)
2257 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2258 call->expression.type = EXPR_CALL;
2259 call->function = expression;
2261 type_t *type = expression->datatype;
2262 if(type->type != TYPE_FUNCTION) {
2263 /* TODO calling pointers to functions is ok */
2264 parser_print_error_prefix();
2265 fputs("called object '", stderr);
2266 print_expression(expression);
2267 fputs("' (type ", stderr);
2269 fputs("is not a function\n", stderr);
2271 call->expression.datatype = NULL;
2273 function_type_t *function_type = (function_type_t*) type;
2274 call->expression.datatype = function_type->result_type;
2277 /* parse arguments */
2280 if(token.type != ')') {
2281 call_argument_t *last_argument = NULL;
2284 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2286 argument->expression = parse_assignment_expression();
2287 if(last_argument == NULL) {
2288 call->arguments = argument;
2290 last_argument->next = argument;
2292 last_argument = argument;
2294 if(token.type != ',')
2301 return (expression_t*) call;
2304 static void type_error(const char *msg, const source_position_t source_position,
2307 parser_print_error_prefix_pos(source_position);
2308 fprintf(stderr, "%s, but found type ", msg);
2310 fputc('\n', stderr);
2314 static void type_error_incompatible(const char *msg,
2315 const source_position_t source_position, type_t *type1, type_t *type2)
2317 parser_print_error_prefix_pos(source_position);
2318 fprintf(stderr, "%s, incompatible types: ", msg);
2320 fprintf(stderr, " - ");
2322 fprintf(stderr, ")\n");
2326 static type_t *get_type_after_conversion(const type_t *type1,
2327 const type_t *type2)
2331 return (type_t*) type1;
2334 static expression_t *create_cast_expression(expression_t *expression,
2337 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2339 cast->expression.type = EXPR_UNARY;
2340 cast->type = UNEXPR_CAST;
2341 cast->value = expression;
2342 cast->expression.datatype = dest_type;
2344 return (expression_t*) cast;
2347 static expression_t *create_cast(expression_t *expression, type_t *dest_type)
2349 assert(expression->datatype != NULL);
2350 type_t *source_type = expression->datatype;
2352 if(expression->datatype == dest_type)
2355 if(dest_type->type == TYPE_ATOMIC) {
2356 if(source_type->type != TYPE_ATOMIC)
2357 panic("casting of non-atomic types not implemented yet");
2359 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
2360 type_error_incompatible("can't cast types",
2361 expression->source_position,
2362 source_type, dest_type);
2366 return create_cast_expression(expression, dest_type);
2369 panic("casting of non-atomic types not implemented yet");
2372 static expression_t *parse_conditional_expression(unsigned precedence,
2373 expression_t *expression)
2377 conditional_expression_t *conditional
2378 = allocate_ast_zero(sizeof(conditional[0]));
2379 conditional->expression.type = EXPR_CONDITIONAL;
2380 conditional->condition = expression;
2383 type_t *condition_type = conditional->condition->datatype;
2384 if(condition_type != NULL) {
2385 if(!is_type_scalar(condition_type)) {
2386 type_error("expected a scalar type", expression->source_position,
2391 conditional->true_expression = parse_expression();
2393 conditional->false_expression = parse_sub_expression(precedence);
2395 type_t *true_type = conditional->true_expression->datatype;
2396 if(true_type == NULL)
2397 return (expression_t*) conditional;
2398 type_t *false_type = conditional->false_expression->datatype;
2399 if(false_type == NULL)
2400 return (expression_t*) conditional;
2403 if(true_type == false_type) {
2404 conditional->expression.datatype = true_type;
2405 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2406 type_t *result = get_type_after_conversion(true_type, false_type);
2407 /* TODO: create implicit convs if necessary */
2408 conditional->expression.datatype = result;
2409 } else if(true_type->type == TYPE_POINTER &&
2410 false_type->type == TYPE_POINTER &&
2411 true /* TODO compatible points_to types */) {
2413 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2414 || (is_null_ptr_const(false_type) &&
2415 true_type->type == TYPE_POINTER) TODO*/ false) {
2417 } else if(/* 1 is pointer to object type, other is void* */ false) {
2420 type_error_incompatible("problem while parsing conditional",
2421 expression->source_position, true_type,
2425 return (expression_t*) conditional;
2428 static expression_t *parse_extension(unsigned precedence)
2430 eat(T___extension__);
2432 /* TODO enable extensions */
2434 return parse_sub_expression(precedence);
2437 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2440 return expression->datatype;
2443 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2445 type_t *expression_type = expression->datatype;
2447 if(expression_type->type == TYPE_POINTER) {
2448 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2449 return pointer_type->points_to;
2451 panic("deref TODO...");
2455 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2457 type_t *type = expression->datatype;
2458 return make_pointer_type(type, 0);
2461 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2462 static expression_t *parse_##unexpression_type(unsigned precedence) \
2466 unary_expression_t *unary_expression \
2467 = allocate_ast_zero(sizeof(unary_expression[0])); \
2468 unary_expression->expression.type = EXPR_UNARY; \
2469 unary_expression->type = unexpression_type; \
2470 unary_expression->value = parse_sub_expression(precedence); \
2471 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2473 return (expression_t*) unary_expression; \
2476 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2477 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2478 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2479 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2480 get_unexpr_dereference_type)
2481 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2482 get_unexpr_take_addr_type)
2483 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2484 get_unexpr_arithmetic_type)
2485 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2486 get_unexpr_arithmetic_type)
2487 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2488 get_unexpr_arithmetic_type)
2490 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2492 static expression_t *parse_##unexpression_type(unsigned precedence, \
2493 expression_t *left) \
2495 (void) precedence; \
2498 unary_expression_t *unary_expression \
2499 = allocate_ast_zero(sizeof(unary_expression[0])); \
2500 unary_expression->expression.type = EXPR_UNARY; \
2501 unary_expression->type = unexpression_type; \
2502 unary_expression->value = left; \
2503 unary_expression->expression.datatype = tfunc(left); \
2505 return (expression_t*) unary_expression; \
2508 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2509 get_unexpr_arithmetic_type)
2510 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2511 get_unexpr_arithmetic_type)
2513 static int get_rank(const type_t *type)
2515 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
2516 * and esp. footnote 108). However we can't fold constants (yet), so we
2517 * can't decide wether unsigned int is possible, while int always works.
2518 * (unsigned int would be preferable when possible... for stuff like
2519 * struct { enum { ... } bla : 4; } ) */
2520 if(type->type == TYPE_ENUM)
2521 return ATOMIC_TYPE_INT;
2523 assert(type->type == TYPE_ATOMIC);
2524 atomic_type_t *atomic_type = (atomic_type_t*) type;
2525 atomic_type_type_t atype = atomic_type->atype;
2529 static void semantic_arithmetic(expression_t **left, expression_t **right)
2531 type_t *type_left = (*left)->datatype;
2532 type_t *type_right = (*right)->datatype;
2533 type_left = skip_typeref(type_left);
2534 type_right = skip_typeref(type_right);
2536 /* TODO: handle complex + imaginary types */
2538 /* § 6.3.1.8 Usual arithmetic conversions */
2539 if(type_left == type_long_double || type_right == type_long_double) {
2540 type_left = type_long_double;
2541 type_right = type_long_double;
2543 } else if(type_left == type_double || type_right == type_double) {
2544 type_left = type_double;
2545 type_right = type_double;
2547 } else if(type_left == type_float || type_right == type_float) {
2548 type_left = type_float;
2549 type_right = type_float;
2553 /* integer promotion */
2554 if(get_rank(type_left) < ATOMIC_TYPE_INT)
2555 type_left = type_int;
2556 if(get_rank(type_right) < ATOMIC_TYPE_INT)
2557 type_right = type_int;
2559 if(type_left == type_right)
2562 bool signed_left = is_type_signed(type_left);
2563 bool signed_right = is_type_signed(type_right);
2564 if(get_rank(type_left) < get_rank(type_right)) {
2565 if(signed_left == signed_right || !signed_right) {
2566 type_left = type_right;
2568 type_right = type_left;
2571 if(signed_left == signed_right || !signed_left) {
2572 type_right = type_left;
2574 type_left = type_right;
2579 assert(type_left == type_right);
2580 *left = create_cast(*left, type_left);
2581 *right = create_cast(*right, type_right);
2584 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2586 expression_t *left = expression->left;
2587 expression_t *right = expression->right;
2588 type_t *type_left = skip_typeref(left->datatype);
2589 type_t *type_right = skip_typeref(right->datatype);
2591 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2592 /* TODO: improve error message */
2593 parser_print_error_prefix();
2594 fprintf(stderr, "operation needs arithmetic types\n");
2598 semantic_arithmetic(&expression->left, &expression->right);
2599 expression->expression.datatype = expression->left->datatype;
2602 static void semantic_add(binary_expression_t *expression)
2604 expression_t *left = expression->left;
2605 expression_t *right = expression->right;
2606 type_t *orig_type_left = left->datatype;
2607 type_t *orig_type_right = right->datatype;
2608 type_t *type_left = skip_typeref(orig_type_left);
2609 type_t *type_right = skip_typeref(orig_type_right);
2612 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2613 semantic_arithmetic(&expression->left, &expression->right);
2614 expression->expression.datatype = expression->left->datatype;
2616 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2617 expression->expression.datatype = type_left;
2618 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2619 expression->expression.datatype = type_right;
2621 parser_print_error_prefix();
2622 fprintf(stderr, "invalid operands to binary + (");
2623 print_type(orig_type_left);
2624 fprintf(stderr, ", ");
2625 print_type(orig_type_right);
2626 fprintf(stderr, ")\n");
2630 static void semantic_sub(binary_expression_t *expression)
2632 expression_t *left = expression->left;
2633 expression_t *right = expression->right;
2634 type_t *orig_type_left = left->datatype;
2635 type_t *orig_type_right = right->datatype;
2636 type_t *type_left = skip_typeref(orig_type_left);
2637 type_t *type_right = skip_typeref(orig_type_right);
2640 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2641 semantic_arithmetic(&expression->left, &expression->right);
2642 expression->expression.datatype = expression->left->datatype;
2644 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2645 expression->expression.datatype = type_left;
2646 } else if(type_left->type == TYPE_POINTER &&
2647 type_right->type == TYPE_POINTER) {
2648 if(!pointers_compatible(type_left, type_right)) {
2649 parser_print_error_prefix();
2650 fprintf(stderr, "pointers to incompatible objects to binary - (");
2651 print_type(orig_type_left);
2652 fprintf(stderr, ", ");
2653 print_type(orig_type_right);
2654 fprintf(stderr, ")\n");
2656 expression->expression.datatype = type_ptrdiff_t;
2659 parser_print_error_prefix();
2660 fprintf(stderr, "invalid operands to binary - (");
2661 print_type(orig_type_left);
2662 fprintf(stderr, ", ");
2663 print_type(orig_type_right);
2664 fprintf(stderr, ")\n");
2668 static void semantic_comparison(binary_expression_t *expression)
2670 expression_t *left = expression->left;
2671 expression_t *right = expression->right;
2672 type_t *type_left = left->datatype;
2673 type_t *type_right = right->datatype;
2675 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2676 semantic_arithmetic(&expression->left, &expression->right);
2678 expression->expression.datatype = type_int;
2681 static void semantic_arithmetic_assign(binary_expression_t *expression)
2683 expression_t *left = expression->left;
2684 expression_t *right = expression->right;
2685 type_t *type_left = left->datatype;
2686 type_t *type_right = right->datatype;
2688 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2689 /* TODO: improve error message */
2690 parser_print_error_prefix();
2691 fprintf(stderr, "operation needs arithmetic types\n");
2695 semantic_arithmetic(&expression->left, &expression->right);
2696 /* note that we assign the original type_left before casting */
2697 expression->expression.datatype = type_left;
2700 static void semantic_logical_op(binary_expression_t *expression)
2703 expression->expression.datatype = type_int;
2706 static void semantic_assign(type_t *orig_type_left, expression_t **right,
2709 type_t *orig_type_right = (*right)->datatype;
2710 type_t *type_left = skip_typeref(orig_type_left);
2711 type_t *type_right = skip_typeref(orig_type_right);
2713 if(type_left == type_right) {
2715 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2716 *right = create_cast(*right, type_left);
2717 } else if(type_left->type == TYPE_POINTER
2718 && type_right->type == TYPE_POINTER) {
2721 /* TODO: improve error message */
2722 parser_print_error_prefix();
2723 fprintf(stderr, "incompatible types in %s\n",
2724 is_return ? "'return'" : "assignment");
2725 parser_print_error_prefix();
2726 print_type(type_left);
2727 fputs(" <- ", stderr);
2728 print_type(type_right);
2729 fputs("\n", stderr);
2734 static void semantic_binexpr_assign(binary_expression_t *expression)
2736 expression_t *left = expression->left;
2737 type_t *type_left = left->datatype;
2739 semantic_assign(type_left, &expression->right, false);
2741 expression->expression.datatype = type_left;
2744 static void semantic_comma(binary_expression_t *expression)
2746 expression->expression.datatype = expression->right->datatype;
2749 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc) \
2750 static expression_t *parse_##binexpression_type(unsigned precedence, \
2751 expression_t *left) \
2755 expression_t *right = parse_sub_expression(precedence); \
2757 binary_expression_t *binexpr \
2758 = allocate_ast_zero(sizeof(binexpr[0])); \
2759 binexpr->expression.type = EXPR_BINARY; \
2760 binexpr->type = binexpression_type; \
2761 binexpr->left = left; \
2762 binexpr->right = right; \
2765 return (expression_t*) binexpr; \
2768 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma)
2769 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic)
2770 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic)
2771 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic)
2772 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add)
2773 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub)
2774 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison)
2775 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison)
2776 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign)
2777 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison)
2778 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2779 semantic_comparison)
2780 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison)
2781 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2782 semantic_comparison)
2783 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic)
2784 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic)
2785 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic)
2786 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op)
2787 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op)
2788 /* TODO shift has a bit special semantic */
2789 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2790 semantic_binexpr_arithmetic)
2791 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2792 semantic_binexpr_arithmetic)
2793 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2794 semantic_arithmetic_assign)
2795 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2796 semantic_arithmetic_assign)
2797 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2798 semantic_arithmetic_assign)
2799 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2800 semantic_arithmetic_assign)
2801 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2802 semantic_arithmetic_assign)
2803 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2804 semantic_arithmetic_assign)
2805 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2806 semantic_arithmetic_assign)
2807 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2808 semantic_arithmetic_assign)
2809 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2810 semantic_arithmetic_assign)
2811 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2812 semantic_arithmetic_assign)
2814 static expression_t *parse_sub_expression(unsigned precedence)
2816 if(token.type < 0) {
2817 return expected_expression_error();
2820 expression_parser_function_t *parser
2821 = &expression_parsers[token.type];
2822 source_position_t source_position = token.source_position;
2825 if(parser->parser != NULL) {
2826 left = parser->parser(parser->precedence);
2828 left = parse_primary_expression();
2830 assert(left != NULL);
2831 left->source_position = source_position;
2834 if(token.type < 0) {
2835 return expected_expression_error();
2838 parser = &expression_parsers[token.type];
2839 if(parser->infix_parser == NULL)
2841 if(parser->infix_precedence < precedence)
2844 left = parser->infix_parser(parser->infix_precedence, left);
2846 assert(left != NULL);
2847 assert(left->type != EXPR_INVALID);
2848 left->source_position = source_position;
2854 static expression_t *parse_expression(void)
2856 return parse_sub_expression(1);
2861 static void register_expression_parser(parse_expression_function parser,
2862 int token_type, unsigned precedence)
2864 expression_parser_function_t *entry = &expression_parsers[token_type];
2866 if(entry->parser != NULL) {
2867 fprintf(stderr, "for token ");
2868 print_token_type(stderr, token_type);
2869 fprintf(stderr, "\n");
2870 panic("trying to register multiple expression parsers for a token");
2872 entry->parser = parser;
2873 entry->precedence = precedence;
2876 static void register_expression_infix_parser(
2877 parse_expression_infix_function parser, int token_type,
2878 unsigned precedence)
2880 expression_parser_function_t *entry = &expression_parsers[token_type];
2882 if(entry->infix_parser != NULL) {
2883 fprintf(stderr, "for token ");
2884 print_token_type(stderr, token_type);
2885 fprintf(stderr, "\n");
2886 panic("trying to register multiple infix expression parsers for a "
2889 entry->infix_parser = parser;
2890 entry->infix_precedence = precedence;
2893 static void init_expression_parsers(void)
2895 memset(&expression_parsers, 0, sizeof(expression_parsers));
2897 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2898 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2899 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2900 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2901 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2902 T_GREATERGREATER, 16);
2903 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2904 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2905 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2906 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2907 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2908 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2909 T_GREATEREQUAL, 14);
2910 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2911 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2912 T_EXCLAMATIONMARKEQUAL, 13);
2913 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2914 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2915 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2916 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2917 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2918 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2919 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2920 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2921 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2922 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2923 T_ASTERISKEQUAL, 2);
2924 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2925 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2927 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2928 T_LESSLESSEQUAL, 2);
2929 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2930 T_GREATERGREATEREQUAL, 2);
2931 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2933 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2935 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2938 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2940 register_expression_infix_parser(parse_array_expression, '[', 30);
2941 register_expression_infix_parser(parse_call_expression, '(', 30);
2942 register_expression_infix_parser(parse_select_expression, '.', 30);
2943 register_expression_infix_parser(parse_select_expression,
2944 T_MINUSGREATER, 30);
2945 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2947 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2950 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2951 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2952 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2953 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2954 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2955 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2956 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2957 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2958 register_expression_parser(parse_sizeof, T_sizeof, 25);
2959 register_expression_parser(parse_extension, T___extension__, 25);
2963 static statement_t *parse_case_statement(void)
2966 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2967 label->statement.type = STATEMENT_CASE_LABEL;
2968 label->statement.source_position = token.source_position;
2970 label->expression = parse_expression();
2973 label->statement.next = parse_statement();
2975 return (statement_t*) label;
2978 static statement_t *parse_default_statement(void)
2982 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2983 label->statement.type = STATEMENT_CASE_LABEL;
2984 label->statement.source_position = token.source_position;
2987 label->statement.next = parse_statement();
2989 return (statement_t*) label;
2992 static statement_t *parse_label_statement(void)
3001 static statement_t *parse_if(void)
3005 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3006 statement->statement.type = STATEMENT_IF;
3007 statement->statement.source_position = token.source_position;
3010 statement->condition = parse_expression();
3013 statement->true_statement = parse_statement();
3014 if(token.type == T_else) {
3016 statement->false_statement = parse_statement();
3019 return (statement_t*) statement;
3022 static statement_t *parse_switch(void)
3026 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3027 statement->statement.type = STATEMENT_SWITCH;
3028 statement->statement.source_position = token.source_position;
3031 statement->expression = parse_expression();
3033 statement->body = parse_statement();
3035 return (statement_t*) statement;
3038 static statement_t *parse_while(void)
3042 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3043 statement->statement.type = STATEMENT_WHILE;
3044 statement->statement.source_position = token.source_position;
3047 statement->condition = parse_expression();
3049 statement->body = parse_statement();
3051 return (statement_t*) statement;
3054 static statement_t *parse_do(void)
3058 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3059 statement->statement.type = STATEMENT_DO_WHILE;
3060 statement->statement.source_position = token.source_position;
3062 statement->body = parse_statement();
3065 statement->condition = parse_expression();
3069 return (statement_t*) statement;
3072 static statement_t *parse_for(void)
3076 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3077 statement->statement.type = STATEMENT_FOR;
3078 statement->statement.source_position = token.source_position;
3082 int top = environment_top();
3083 context_t *last_context = context;
3084 set_context(&statement->context);
3086 if(token.type != ';') {
3087 if(is_declaration_specifier(&token, false)) {
3088 parse_declaration();
3090 statement->initialisation = parse_expression();
3097 if(token.type != ';') {
3098 statement->condition = parse_expression();
3101 if(token.type != ')') {
3102 statement->step = parse_expression();
3105 statement->body = parse_statement();
3107 assert(context == &statement->context);
3108 set_context(last_context);
3109 environment_pop_to(top);
3111 return (statement_t*) statement;
3114 static statement_t *parse_goto(void)
3117 expect(T_IDENTIFIER);
3123 static statement_t *parse_continue(void)
3128 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3129 statement->source_position = token.source_position;
3130 statement->type = STATEMENT_CONTINUE;
3135 static statement_t *parse_break(void)
3140 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3141 statement->source_position = token.source_position;
3142 statement->type = STATEMENT_BREAK;
3147 static statement_t *parse_return(void)
3151 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3153 statement->statement.type = STATEMENT_RETURN;
3154 statement->statement.source_position = token.source_position;
3156 assert(current_function->type->type == TYPE_FUNCTION);
3157 function_type_t *function_type = (function_type_t*) current_function->type;
3158 type_t *return_type = function_type->result_type;
3160 expression_t *return_value;
3161 if(token.type != ';') {
3162 return_value = parse_expression();
3164 if(return_type == type_void && return_value->datatype != type_void) {
3165 parse_warning("'return' with a value, in function returning void");
3166 return_value = NULL;
3168 semantic_assign(return_type, &return_value, true);
3171 return_value = NULL;
3172 parse_warning("'return' without value, in function retruning non-void");
3174 statement->return_value = return_value;
3178 return (statement_t*) statement;
3181 static statement_t *parse_declaration_statement(void)
3183 declaration_t *before = last_declaration;
3185 declaration_statement_t *statement
3186 = allocate_ast_zero(sizeof(statement[0]));
3187 statement->statement.type = STATEMENT_DECLARATION;
3188 statement->statement.source_position = token.source_position;
3190 declaration_specifiers_t specifiers;
3191 memset(&specifiers, 0, sizeof(specifiers));
3192 parse_declaration_specifiers(&specifiers);
3194 if(token.type == ';') {
3197 parse_init_declarators(&specifiers);
3200 if(before == NULL) {
3201 statement->declarations_begin = context->declarations;
3203 statement->declarations_begin = before->next;
3205 statement->declarations_end = last_declaration;
3207 return (statement_t*) statement;
3210 static statement_t *parse_expression_statement(void)
3212 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3213 statement->statement.type = STATEMENT_EXPRESSION;
3214 statement->statement.source_position = token.source_position;
3216 statement->expression = parse_expression();
3220 return (statement_t*) statement;
3223 static statement_t *parse_statement(void)
3225 statement_t *statement = NULL;
3227 /* declaration or statement */
3228 switch(token.type) {
3230 statement = parse_case_statement();
3234 statement = parse_default_statement();
3238 statement = parse_compound_statement();
3242 statement = parse_if();
3246 statement = parse_switch();
3250 statement = parse_while();
3254 statement = parse_do();
3258 statement = parse_for();
3262 statement = parse_goto();
3266 statement = parse_continue();
3270 statement = parse_break();
3274 statement = parse_return();
3283 if(look_ahead(1)->type == ':') {
3284 statement = parse_label_statement();
3288 if(is_typedef_symbol(token.v.symbol)) {
3289 statement = parse_declaration_statement();
3293 statement = parse_expression_statement();
3296 case T___extension__:
3297 /* this can be a prefix to a declaration or an expression statement */
3298 /* we simply eat it now and parse the rest with tail recursion */
3301 } while(token.type == T___extension__);
3302 statement = parse_statement();
3306 statement = parse_declaration_statement();
3310 statement = parse_expression_statement();
3314 assert(statement == NULL || statement->source_position.input_name != NULL);
3319 static statement_t *parse_compound_statement(void)
3323 compound_statement_t *compound_statement
3324 = allocate_ast_zero(sizeof(compound_statement[0]));
3325 compound_statement->statement.type = STATEMENT_COMPOUND;
3326 compound_statement->statement.source_position = token.source_position;
3328 int top = environment_top();
3329 context_t *last_context = context;
3330 set_context(&compound_statement->context);
3332 statement_t *last_statement = NULL;
3334 while(token.type != '}' && token.type != T_EOF) {
3335 statement_t *statement = parse_statement();
3336 if(statement == NULL)
3339 if(last_statement != NULL) {
3340 last_statement->next = statement;
3342 compound_statement->statements = statement;
3345 while(statement->next != NULL)
3346 statement = statement->next;
3348 last_statement = statement;
3351 assert(context == &compound_statement->context);
3352 set_context(last_context);
3353 environment_pop_to(top);
3357 return (statement_t*) compound_statement;
3360 static translation_unit_t *parse_translation_unit(void)
3362 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3364 assert(global_context == NULL);
3365 global_context = &unit->context;
3367 assert(context == NULL);
3368 set_context(&unit->context);
3370 while(token.type != T_EOF) {
3371 parse_declaration();
3374 assert(context == &unit->context);
3376 last_declaration = NULL;
3378 assert(global_context == &unit->context);
3379 global_context = NULL;
3384 translation_unit_t *parse(void)
3386 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3387 found_error = false;
3389 type_set_output(stderr);
3390 ast_set_output(stderr);
3392 lookahead_bufpos = 0;
3393 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3396 translation_unit_t *unit = parse_translation_unit();
3398 DEL_ARR_F(environment_stack);
3406 void init_parser(void)
3408 init_expression_parsers();
3409 obstack_init(&temp_obst);
3411 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3412 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3413 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3414 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3415 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3416 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3417 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3418 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3419 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3420 type_string = make_pointer_type(type_const_char, 0);
3423 void exit_parser(void)
3425 obstack_free(&temp_obst, NULL);