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;
49 static statement_t *parse_compound_statement(void);
50 static statement_t *parse_statement(void);
52 static expression_t *parse_sub_expression(unsigned precedence);
53 static expression_t *parse_expression(void);
54 static type_t *parse_typename(void);
56 #define STORAGE_CLASSES \
63 #define TYPE_QUALIFIERS \
69 #ifdef PROVIDE_COMPLEX
70 #define COMPLEX_SPECIFIERS \
73 #define COMPLEX_SPECIFIERS
76 #ifdef PROVIDE_IMAGINARY
77 #define IMAGINARY_SPECIFIERS \
80 #define IMAGINARY_SPECIFIERS
83 #define TYPE_SPECIFIERS \
101 #define DECLARATION_START \
106 #define TYPENAME_START \
110 static inline void *allocate_ast_zero(size_t size)
112 void *res = allocate_ast(size);
113 memset(res, 0, size);
117 static inline void *allocate_type_zero(size_t size)
119 void *res = obstack_alloc(type_obst, size);
120 memset(res, 0, size);
124 static inline void free_type(void *type)
126 obstack_free(type_obst, type);
130 * returns the top element of the environment stack
132 static inline size_t environment_top(void)
134 return ARR_LEN(environment_stack);
139 static inline void next_token(void)
141 token = lookahead_buffer[lookahead_bufpos];
142 lookahead_buffer[lookahead_bufpos] = lexer_token;
145 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
148 print_token(stderr, &token);
149 fprintf(stderr, "\n");
153 static inline const token_t *look_ahead(int num)
155 assert(num > 0 && num <= MAX_LOOKAHEAD);
156 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
157 return & lookahead_buffer[pos];
160 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
162 static void error(void)
165 #ifdef ABORT_ON_ERROR
170 static void parser_print_prefix_pos(const source_position_t source_position)
172 fputs(source_position.input_name, stderr);
174 fprintf(stderr, "%d", source_position.linenr);
178 static void parser_print_error_prefix_pos(
179 const source_position_t source_position)
181 parser_print_prefix_pos(source_position);
182 fputs("error: ", stderr);
186 static void parser_print_error_prefix(void)
188 parser_print_error_prefix_pos(token.source_position);
191 static void parse_error(const char *message)
193 parser_print_error_prefix();
194 fprintf(stderr, "parse error: %s\n", message);
197 static void parse_warning(const char *message)
199 parser_print_prefix_pos(token.source_position);
200 fprintf(stderr, "warning: %s\n", message);
203 static void parse_error_expected(const char *message, ...)
208 if(message != NULL) {
209 parser_print_error_prefix();
210 fprintf(stderr, "%s\n", message);
212 parser_print_error_prefix();
213 fputs("Parse error: got ", stderr);
214 print_token(stderr, &token);
215 fputs(", expected ", stderr);
217 va_start(args, message);
218 token_type_t token_type = va_arg(args, token_type_t);
219 while(token_type != 0) {
223 fprintf(stderr, ", ");
225 print_token_type(stderr, token_type);
226 token_type = va_arg(args, token_type_t);
229 fprintf(stderr, "\n");
232 static void eat_block(void)
234 if(token.type == '{')
237 while(token.type != '}') {
238 if(token.type == T_EOF)
240 if(token.type == '{') {
249 static void eat_statement(void)
251 while(token.type != ';') {
252 if(token.type == T_EOF)
254 if(token.type == '}')
256 if(token.type == '{') {
265 static void eat_brace(void)
267 if(token.type == '(')
270 while(token.type != ')') {
271 if(token.type == T_EOF)
273 if(token.type == ')' || token.type == ';' || token.type == '}') {
276 if(token.type == '(') {
280 if(token.type == '{') {
289 #define expect(expected) \
290 if(UNLIKELY(token.type != (expected))) { \
291 parse_error_expected(NULL, (expected), 0); \
297 #define expect_void(expected) \
298 if(UNLIKELY(token.type != (expected))) { \
299 parse_error_expected(NULL, (expected), 0); \
305 static void set_context(context_t *new_context)
307 context = new_context;
309 last_declaration = new_context->declarations;
310 if(last_declaration != NULL) {
311 while(last_declaration->next != NULL) {
312 last_declaration = last_declaration->next;
318 * called when we find a 2nd declarator for an identifier we already have a
321 static bool is_compatible_declaration (declaration_t *declaration,
322 declaration_t *previous)
324 /* TODO: not correct yet */
325 return declaration->type == previous->type;
328 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
330 declaration_t *declaration = symbol->declaration;
331 for( ; declaration != NULL; declaration = declaration->symbol_next) {
332 if(declaration->namespace == namespace)
339 static const char *get_namespace_prefix(namespace_t namespace)
342 case NAMESPACE_NORMAL:
344 case NAMESPACE_UNION:
346 case NAMESPACE_STRUCT:
351 panic("invalid namespace found");
355 * pushs an environment_entry on the environment stack and links the
356 * corresponding symbol to the new entry
358 static declaration_t *environment_push(declaration_t *declaration)
360 symbol_t *symbol = declaration->symbol;
361 namespace_t namespace = declaration->namespace;
362 assert(declaration->source_position.input_name != NULL);
364 /* a declaration should be only pushed once */
365 assert(declaration->parent_context == NULL);
366 declaration->parent_context = context;
368 declaration_t *previous_declaration = get_declaration(symbol, namespace);
369 assert(declaration != previous_declaration);
370 if(previous_declaration != NULL
371 && previous_declaration->parent_context == context) {
372 if(!is_compatible_declaration(declaration, previous_declaration)) {
373 parser_print_error_prefix_pos(declaration->source_position);
374 fprintf(stderr, "definition of symbol %s%s with type ",
375 get_namespace_prefix(namespace), symbol->string);
377 print_type(declaration->type);
379 parser_print_error_prefix_pos(
380 previous_declaration->source_position);
381 fprintf(stderr, "is incompatible with previous declaration "
383 print_type(previous_declaration->type);
386 return previous_declaration;
389 /* remember old declaration */
391 entry.symbol = symbol;
392 entry.old_declaration = symbol->declaration;
393 entry.namespace = namespace;
394 ARR_APP1(environment_stack, entry);
396 /* replace/add declaration into declaration list of the symbol */
397 if(symbol->declaration == NULL) {
398 symbol->declaration = declaration;
400 declaration_t *iter = symbol->declaration;
401 for( ; iter != NULL; iter = iter->symbol_next) {
402 declaration_t *symbol_next = iter->symbol_next;
403 if(symbol_next == NULL) {
404 iter->symbol_next = declaration;
405 assert(declaration->symbol_next == NULL);
408 if(symbol_next->namespace == namespace) {
409 iter->symbol_next = declaration;
410 declaration->symbol_next = symbol_next->symbol_next;
420 * pops symbols from the environment stack until @p new_top is the top element
422 static void environment_pop_to(size_t new_top)
424 size_t top = ARR_LEN(environment_stack);
427 assert(new_top <= top);
431 for(i = top; i > new_top; --i) {
432 stack_entry_t *entry = & environment_stack[i - 1];
434 declaration_t *old_declaration = entry->old_declaration;
435 symbol_t *symbol = entry->symbol;
436 namespace_t namespace = entry->namespace;
438 /* replace/remove declaration */
439 declaration_t *declaration = symbol->declaration;
440 assert(declaration != NULL);
441 if(declaration->namespace == namespace) {
442 if(old_declaration == NULL) {
443 symbol->declaration = declaration->symbol_next;
445 symbol->declaration = old_declaration;
446 assert(old_declaration->symbol_next ==
447 declaration->symbol_next);
450 for(; declaration != NULL; declaration = declaration->symbol_next) {
451 declaration_t *symbol_next = declaration->symbol_next;
452 if(symbol_next->namespace == namespace) {
453 declaration->symbol_next = old_declaration;
454 assert(old_declaration->symbol_next
455 == symbol_next->symbol_next);
459 assert(declaration != NULL);
463 ARR_SHRINKLEN(environment_stack, (int) new_top);
468 static expression_t *parse_constant_expression(void)
470 /* start parsing at precedence 7 (conditional expression) */
471 return parse_sub_expression(7);
474 static expression_t *parse_assignment_expression(void)
476 /* start parsing at precedence 2 (assignment expression) */
477 return parse_sub_expression(2);
480 static void parse_compound_type_entries(void);
481 static declaration_t *parse_declarator(storage_class_t storage_class,
482 type_t *type, int may_be_abstract);
483 static declaration_t *record_declaration(declaration_t *declaration);
485 typedef struct declaration_specifiers_t declaration_specifiers_t;
486 struct declaration_specifiers_t {
487 storage_class_t storage_class;
491 static const char *parse_string_literals(void)
493 assert(token.type == T_STRING_LITERAL);
494 const char *result = token.v.string;
498 while(token.type == T_STRING_LITERAL) {
499 result = concat_strings(result, token.v.string);
506 static void parse_attributes(void)
510 case T___attribute__:
518 parse_error("EOF while parsing attribute");
536 if(token.type != T_STRING_LITERAL) {
537 parse_error_expected("while parsing assembler attribute",
542 parse_string_literals();
547 goto attributes_finished;
555 static designator_t *parse_designation(void)
557 if(token.type != '[' && token.type != '.')
560 designator_t *result = NULL;
561 designator_t *last = NULL;
564 designator_t *designator;
567 designator = allocate_ast_zero(sizeof(designator[0]));
569 designator->array_access = parse_constant_expression();
573 designator = allocate_ast_zero(sizeof(designator[0]));
575 if(token.type != T_IDENTIFIER) {
576 parse_error_expected("problem while parsing designator",
580 designator->symbol = token.v.symbol;
588 assert(designator != NULL);
590 last->next = designator;
598 static initializer_t *parse_initializer_list(void);
600 static initializer_t *parse_initializer(void)
602 designator_t *designator = parse_designation();
604 initializer_t *result;
605 if(token.type == '{') {
606 result = parse_initializer_list();
608 result = allocate_ast_zero(sizeof(result[0]));
609 result->type = INITIALIZER_VALUE;
610 result->v.value = parse_assignment_expression();
612 result->designator = designator;
617 static initializer_t *parse_initializer_list(void)
621 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
622 result->type = INITIALIZER_LIST;
624 initializer_t *last = NULL;
626 initializer_t *initializer = parse_initializer();
628 last->next = initializer;
630 result->v.list = initializer;
634 if(token.type == '}')
637 if(token.type != ',') {
638 parse_error_expected("problem while parsing initializer list",
645 if(token.type == '}')
654 static declaration_t *parse_compound_type_specifier(bool is_struct)
662 symbol_t *symbol = NULL;
663 declaration_t *declaration = NULL;
665 if(token.type == T_IDENTIFIER) {
666 symbol = token.v.symbol;
670 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
672 declaration = get_declaration(symbol, NAMESPACE_UNION);
674 } else if(token.type != '{') {
676 parse_error_expected("problem while parsing struct type specifier",
677 T_IDENTIFIER, '{', 0);
679 parse_error_expected("problem while parsing union type specifier",
680 T_IDENTIFIER, '{', 0);
686 if(declaration == NULL) {
687 declaration = allocate_type_zero(sizeof(declaration[0]));
690 declaration->namespace = NAMESPACE_STRUCT;
692 declaration->namespace = NAMESPACE_UNION;
694 declaration->source_position = token.source_position;
695 declaration->symbol = symbol;
698 if(token.type == '{') {
699 if(declaration->init.is_defined) {
700 assert(symbol != NULL);
701 parser_print_error_prefix();
702 fprintf(stderr, "multiple definition of %s %s\n",
703 is_struct ? "struct" : "union", symbol->string);
704 declaration->context.declarations = NULL;
706 record_declaration(declaration);
707 declaration->init.is_defined = true;
709 int top = environment_top();
710 context_t *last_context = context;
711 set_context(& declaration->context);
713 parse_compound_type_entries();
716 assert(context == & declaration->context);
717 set_context(last_context);
718 environment_pop_to(top);
724 static void parse_enum_entries(void)
728 if(token.type == '}') {
730 parse_error("empty enum not allowed");
735 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
737 if(token.type != T_IDENTIFIER) {
738 parse_error_expected("problem while parsing enum entry",
743 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
744 entry->symbol = token.v.symbol;
745 entry->source_position = token.source_position;
748 if(token.type == '=') {
750 entry->init.initializer = parse_initializer();
753 record_declaration(entry);
755 if(token.type != ',')
758 } while(token.type != '}');
763 static declaration_t *parse_enum_specifier(void)
767 declaration_t *declaration;
770 if(token.type == T_IDENTIFIER) {
771 symbol = token.v.symbol;
774 declaration = get_declaration(symbol, NAMESPACE_ENUM);
775 } else if(token.type != '{') {
776 parse_error_expected("problem while parsing enum type specifier",
777 T_IDENTIFIER, '{', 0);
784 if(declaration == NULL) {
785 declaration = allocate_type_zero(sizeof(declaration[0]));
787 declaration->namespace = NAMESPACE_ENUM;
788 declaration->source_position = token.source_position;
789 declaration->symbol = symbol;
792 if(token.type == '{') {
793 if(declaration->init.is_defined) {
794 parser_print_error_prefix();
795 fprintf(stderr, "multiple definitions of enum %s\n",
798 record_declaration(declaration);
799 declaration->init.is_defined = 1;
801 parse_enum_entries();
809 * if a symbol is a typedef to another type, return true
811 static bool is_typedef_symbol(symbol_t *symbol)
813 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
814 if(declaration == NULL
815 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
821 static type_t *parse_typeof(void)
829 expression_t *expression = NULL;
833 case T___extension__:
834 /* this can be a prefix to a typename or an expression */
835 /* we simply eat it now. */
838 } while(token.type == T___extension__);
842 if(is_typedef_symbol(token.v.symbol)) {
843 type = parse_typename();
845 expression = parse_expression();
846 type = expression->datatype;
851 type = parse_typename();
855 expression = parse_expression();
856 type = expression->datatype;
862 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
863 typeof->type.type = TYPE_TYPEOF;
864 typeof->expression = expression;
865 typeof->typeof_type = type;
867 return (type_t*) typeof;
871 SPECIFIER_SIGNED = 1 << 0,
872 SPECIFIER_UNSIGNED = 1 << 1,
873 SPECIFIER_LONG = 1 << 2,
874 SPECIFIER_INT = 1 << 3,
875 SPECIFIER_DOUBLE = 1 << 4,
876 SPECIFIER_CHAR = 1 << 5,
877 SPECIFIER_SHORT = 1 << 6,
878 SPECIFIER_LONG_LONG = 1 << 7,
879 SPECIFIER_FLOAT = 1 << 8,
880 SPECIFIER_BOOL = 1 << 9,
881 SPECIFIER_VOID = 1 << 10,
882 #ifdef PROVIDE_COMPLEX
883 SPECIFIER_COMPLEX = 1 << 11,
885 #ifdef PROVIDE_IMAGINARY
886 SPECIFIER_IMAGINARY = 1 << 12,
890 static type_t *create_builtin_type(symbol_t *symbol)
892 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
893 type->type.type = TYPE_BUILTIN;
894 type->symbol = symbol;
896 type->real_type = type_int;
898 return (type_t*) type;
901 static type_t *get_typedef_type(symbol_t *symbol)
903 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
904 if(declaration == NULL
905 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
908 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
909 typedef_type->type.type = TYPE_TYPEDEF;
910 typedef_type->declaration = declaration;
912 return (type_t*) typedef_type;
915 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
918 unsigned type_qualifiers = 0;
919 unsigned type_specifiers = 0;
926 #define MATCH_STORAGE_CLASS(token, class) \
928 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
929 parse_error("multiple storage classes in declaration " \
932 specifiers->storage_class = class; \
936 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
937 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
938 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
939 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
940 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
942 /* type qualifiers */
943 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
945 type_qualifiers |= qualifier; \
949 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
950 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
951 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
952 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
954 case T___extension__:
959 /* type specifiers */
960 #define MATCH_SPECIFIER(token, specifier, name) \
963 if(type_specifiers & specifier) { \
964 parse_error("multiple " name " type specifiers given"); \
966 type_specifiers |= specifier; \
970 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
971 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
972 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
973 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
974 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
975 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
976 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
977 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
978 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
979 #ifdef PROVIDE_COMPLEX
980 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
982 #ifdef PROVIDE_IMAGINARY
983 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
987 if(type_specifiers & SPECIFIER_LONG_LONG) {
988 parse_error("multiple type specifiers given");
989 } else if(type_specifiers & SPECIFIER_LONG) {
990 type_specifiers |= SPECIFIER_LONG_LONG;
992 type_specifiers |= SPECIFIER_LONG;
996 /* TODO: if type != NULL for the following rules should issue
999 compound_type_t *compound_type
1000 = allocate_type_zero(sizeof(compound_type[0]));
1001 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1002 compound_type->declaration = parse_compound_type_specifier(true);
1004 type = (type_t*) compound_type;
1008 compound_type_t *compound_type
1009 = allocate_type_zero(sizeof(compound_type[0]));
1010 compound_type->type.type = TYPE_COMPOUND_UNION;
1011 compound_type->declaration = parse_compound_type_specifier(false);
1013 type = (type_t*) compound_type;
1017 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1018 enum_type->type.type = TYPE_ENUM;
1019 enum_type->declaration = parse_enum_specifier();
1021 type = (type_t*) enum_type;
1025 type = parse_typeof();
1027 case T___builtin_va_list:
1028 type = create_builtin_type(token.v.symbol);
1032 case T___attribute__:
1037 case T_IDENTIFIER: {
1038 type_t *typedef_type = get_typedef_type(token.v.symbol);
1040 if(typedef_type == NULL)
1041 goto finish_specifiers;
1044 type = typedef_type;
1048 /* function specifier */
1050 goto finish_specifiers;
1057 atomic_type_type_t atomic_type;
1059 /* match valid basic types */
1060 switch(type_specifiers) {
1061 case SPECIFIER_VOID:
1062 atomic_type = ATOMIC_TYPE_VOID;
1064 case SPECIFIER_CHAR:
1065 atomic_type = ATOMIC_TYPE_CHAR;
1067 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1068 atomic_type = ATOMIC_TYPE_SCHAR;
1070 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1071 atomic_type = ATOMIC_TYPE_UCHAR;
1073 case SPECIFIER_SHORT:
1074 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1075 case SPECIFIER_SHORT | SPECIFIER_INT:
1076 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1077 atomic_type = ATOMIC_TYPE_SHORT;
1079 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1080 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1081 atomic_type = ATOMIC_TYPE_USHORT;
1084 case SPECIFIER_SIGNED:
1085 case SPECIFIER_SIGNED | SPECIFIER_INT:
1086 atomic_type = ATOMIC_TYPE_INT;
1088 case SPECIFIER_UNSIGNED:
1089 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1090 atomic_type = ATOMIC_TYPE_UINT;
1092 case SPECIFIER_LONG:
1093 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1094 case SPECIFIER_LONG | SPECIFIER_INT:
1095 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1096 atomic_type = ATOMIC_TYPE_LONG;
1098 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1099 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1100 atomic_type = ATOMIC_TYPE_ULONG;
1102 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1103 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1104 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1105 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1107 atomic_type = ATOMIC_TYPE_LONGLONG;
1109 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1110 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1112 atomic_type = ATOMIC_TYPE_ULONGLONG;
1114 case SPECIFIER_FLOAT:
1115 atomic_type = ATOMIC_TYPE_FLOAT;
1117 case SPECIFIER_DOUBLE:
1118 atomic_type = ATOMIC_TYPE_DOUBLE;
1120 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1121 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1123 case SPECIFIER_BOOL:
1124 atomic_type = ATOMIC_TYPE_BOOL;
1126 #ifdef PROVIDE_COMPLEX
1127 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1128 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1130 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1131 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1133 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1134 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1137 #ifdef PROVIDE_IMAGINARY
1138 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1139 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1141 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1142 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1144 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1145 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1149 /* invalid specifier combination, give an error message */
1150 if(type_specifiers == 0) {
1152 parse_warning("no type specifiers in declaration (using int)");
1153 atomic_type = ATOMIC_TYPE_INT;
1156 parse_error("no type specifiers given in declaration");
1158 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1159 (type_specifiers & SPECIFIER_UNSIGNED)) {
1160 parse_error("signed and unsigned specifiers gives");
1161 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1162 parse_error("only integer types can be signed or unsigned");
1164 parse_error("multiple datatypes in declaration");
1166 atomic_type = ATOMIC_TYPE_INVALID;
1169 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1170 atype->type.type = TYPE_ATOMIC;
1171 atype->atype = atomic_type;
1174 type = (type_t*) atype;
1176 if(type_specifiers != 0) {
1177 parse_error("multiple datatypes in declaration");
1181 type->qualifiers = type_qualifiers;
1183 type_t *result = typehash_insert(type);
1184 if(newtype && result != (type_t*) type) {
1188 specifiers->type = result;
1191 static type_qualifier_t parse_type_qualifiers(void)
1193 type_qualifier_t type_qualifiers = 0;
1196 switch(token.type) {
1197 /* type qualifiers */
1198 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1199 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1200 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1201 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1204 return type_qualifiers;
1209 static void parse_identifier_list(void)
1212 if(token.type != T_IDENTIFIER) {
1213 parse_error_expected("problem while parsing parameter identifier "
1214 "list", T_IDENTIFIER, 0);
1218 if(token.type != ',')
1224 static declaration_t *parse_parameter(void)
1226 declaration_specifiers_t specifiers;
1227 memset(&specifiers, 0, sizeof(specifiers));
1229 parse_declaration_specifiers(&specifiers);
1231 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1232 specifiers.type, 1);
1234 /* TODO check declaration constraints for parameters */
1235 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1236 parse_error("typedef not allowed in parameter list");
1242 static declaration_t *parse_parameters(function_type_t *type)
1244 if(token.type == T_IDENTIFIER) {
1245 symbol_t *symbol = token.v.symbol;
1246 if(!is_typedef_symbol(symbol)) {
1248 parse_identifier_list();
1253 if(token.type == ')') {
1254 type->unspecified_parameters = 1;
1257 if(token.type == T_void && look_ahead(1)->type == ')') {
1262 declaration_t *declarations = NULL;
1263 declaration_t *declaration;
1264 declaration_t *last_declaration = NULL;
1265 function_parameter_t *parameter;
1266 function_parameter_t *last_parameter = NULL;
1269 switch(token.type) {
1273 return declarations;
1276 case T___extension__:
1278 declaration = parse_parameter();
1280 parameter = allocate_type_zero(sizeof(parameter[0]));
1281 parameter->type = declaration->type;
1283 if(last_parameter != NULL) {
1284 last_declaration->next = declaration;
1285 last_parameter->next = parameter;
1287 type->parameters = parameter;
1288 declarations = declaration;
1290 last_parameter = parameter;
1291 last_declaration = declaration;
1295 return declarations;
1297 if(token.type != ',')
1298 return declarations;
1307 } construct_type_type_t;
1309 typedef struct construct_type_t construct_type_t;
1310 struct construct_type_t {
1311 construct_type_type_t type;
1312 construct_type_t *next;
1315 typedef struct parsed_pointer_t parsed_pointer_t;
1316 struct parsed_pointer_t {
1317 construct_type_t construct_type;
1318 type_qualifier_t type_qualifiers;
1321 typedef struct construct_function_type_t construct_function_type_t;
1322 struct construct_function_type_t {
1323 construct_type_t construct_type;
1324 function_type_t *function_type;
1327 typedef struct parsed_array_t parsed_array_t;
1328 struct parsed_array_t {
1329 construct_type_t construct_type;
1330 type_qualifier_t type_qualifiers;
1336 typedef struct construct_base_type_t construct_base_type_t;
1337 struct construct_base_type_t {
1338 construct_type_t construct_type;
1342 static construct_type_t *parse_pointer_declarator(void)
1346 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1347 memset(pointer, 0, sizeof(pointer[0]));
1348 pointer->type_qualifiers = parse_type_qualifiers();
1350 return (construct_type_t*) pointer;
1353 static construct_type_t *parse_array_declarator(void)
1357 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1358 memset(array, 0, sizeof(array[0]));
1360 if(token.type == T_static) {
1361 array->is_static = true;
1365 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1366 if(type_qualifiers != 0) {
1367 if(token.type == T_static) {
1368 array->is_static = true;
1372 array->type_qualifiers = type_qualifiers;
1374 if(token.type == '*' && look_ahead(1)->type == ']') {
1375 array->is_variable = true;
1377 } else if(token.type != ']') {
1378 array->size = parse_assignment_expression();
1383 return (construct_type_t*) array;
1386 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1390 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1391 type->type.type = TYPE_FUNCTION;
1393 declaration_t *parameters = parse_parameters(type);
1394 if(declaration != NULL) {
1395 declaration->context.declarations = parameters;
1398 construct_function_type_t *construct_function_type =
1399 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1400 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1401 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1402 construct_function_type->function_type = type;
1406 return (construct_type_t*) construct_function_type;
1409 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1410 int may_be_abstract)
1412 construct_type_t *result = NULL;
1413 construct_type_t *last = NULL;
1415 while(token.type == '*') {
1416 construct_type_t *type = parse_pointer_declarator();
1425 /* TODO: find out if this is correct */
1428 construct_type_t *inner_types = NULL;
1430 switch(token.type) {
1432 if(declaration == NULL) {
1433 parse_error("no identifier expected in typename");
1435 declaration->symbol = token.v.symbol;
1436 declaration->source_position = token.source_position;
1442 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1448 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1453 construct_type_t *type;
1454 switch(token.type) {
1456 type = parse_function_declarator(declaration);
1459 type = parse_array_declarator();
1462 goto declarator_finished;
1473 declarator_finished:
1476 if(inner_types != NULL) {
1478 last->next = inner_types;
1480 result = inner_types;
1488 static type_t *construct_declarator_type(construct_type_t *construct_list,
1491 construct_type_t *iter = construct_list;
1492 for( ; iter != NULL; iter = iter->next) {
1493 parsed_pointer_t *parsed_pointer;
1494 parsed_array_t *parsed_array;
1495 construct_function_type_t *construct_function_type;
1496 function_type_t *function_type;
1497 pointer_type_t *pointer_type;
1498 array_type_t *array_type;
1500 switch(iter->type) {
1501 case CONSTRUCT_FUNCTION:
1502 construct_function_type = (construct_function_type_t*) iter;
1503 function_type = construct_function_type->function_type;
1505 function_type->result_type = type;
1506 type = (type_t*) function_type;
1509 case CONSTRUCT_POINTER:
1510 parsed_pointer = (parsed_pointer_t*) iter;
1511 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1513 pointer_type->type.type = TYPE_POINTER;
1514 pointer_type->points_to = type;
1515 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1516 type = (type_t*) pointer_type;
1519 case CONSTRUCT_ARRAY:
1520 parsed_array = (parsed_array_t*) iter;
1521 array_type = allocate_type_zero(sizeof(array_type[0]));
1523 array_type->type.type = TYPE_ARRAY;
1524 array_type->element_type = type;
1525 array_type->type.qualifiers = parsed_array->type_qualifiers;
1526 array_type->is_static = parsed_array->is_static;
1527 array_type->is_variable = parsed_array->is_variable;
1528 array_type->size = parsed_array->size;
1529 type = (type_t*) array_type;
1533 type_t *hashed_type = typehash_insert((type_t*) type);
1534 if(hashed_type != type) {
1543 static declaration_t *parse_declarator(storage_class_t storage_class,
1544 type_t *type, int may_be_abstract)
1546 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1547 declaration->storage_class = storage_class;
1549 construct_type_t *construct_type
1550 = parse_inner_declarator(declaration, may_be_abstract);
1551 declaration->type = construct_declarator_type(construct_type, type);
1553 if(construct_type != NULL) {
1554 obstack_free(&temp_obst, construct_type);
1560 static type_t *parse_abstract_declarator(type_t *base_type)
1562 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1564 type_t *result = construct_declarator_type(construct_type, base_type);
1565 if(construct_type != NULL) {
1566 obstack_free(&temp_obst, construct_type);
1572 static declaration_t *record_declaration(declaration_t *declaration)
1574 assert(context != NULL);
1576 symbol_t *symbol = declaration->symbol;
1577 if(symbol != NULL) {
1578 declaration_t *alias = environment_push(declaration);
1579 if(alias != declaration)
1582 declaration->parent_context = context;
1585 if(last_declaration != NULL) {
1586 last_declaration->next = declaration;
1588 context->declarations = declaration;
1590 last_declaration = declaration;
1595 static void parser_error_multiple_definition(declaration_t *previous,
1596 declaration_t *declaration)
1598 parser_print_error_prefix_pos(declaration->source_position);
1599 fprintf(stderr, "multiple definition of symbol '%s'\n",
1600 declaration->symbol->string);
1601 parser_print_error_prefix_pos(previous->source_position);
1602 fprintf(stderr, "this is the location of the previous "
1607 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1610 declaration_t *ndeclaration
1611 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1613 declaration_t *declaration = record_declaration(ndeclaration);
1614 if(token.type == '=') {
1617 /* TODO: check that this is an allowed type (no function type) */
1619 if(declaration->init.initializer != NULL) {
1620 parser_error_multiple_definition(declaration, ndeclaration);
1623 ndeclaration->init.initializer = parse_initializer();
1624 } else if(token.type == '{') {
1625 if(declaration->type->type != TYPE_FUNCTION) {
1626 parser_print_error_prefix();
1627 fprintf(stderr, "Declarator ");
1628 print_type_ext(declaration->type, declaration->symbol, NULL);
1629 fprintf(stderr, " has a body but is not a function type.\n");
1634 if(declaration->init.statement != NULL) {
1635 parser_error_multiple_definition(declaration, ndeclaration);
1637 if(ndeclaration != declaration) {
1638 memcpy(&declaration->context, &ndeclaration->context,
1639 sizeof(declaration->context));
1642 int top = environment_top();
1643 context_t *last_context = context;
1644 set_context(&declaration->context);
1646 /* push function parameters */
1647 declaration_t *parameter = declaration->context.declarations;
1648 for( ; parameter != NULL; parameter = parameter->next) {
1649 environment_push(parameter);
1651 declaration_t *old_current_function = current_function;
1652 current_function = declaration;
1654 statement_t *statement = parse_compound_statement();
1656 assert(current_function == declaration);
1657 old_current_function = current_function;
1659 assert(context == &declaration->context);
1660 set_context(last_context);
1661 environment_pop_to(top);
1663 declaration->init.statement = statement;
1667 if(token.type != ',')
1674 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1677 if(token.type == ':') {
1679 parse_constant_expression();
1680 /* TODO (bitfields) */
1682 declaration_t *declaration
1683 = parse_declarator(specifiers->storage_class,
1684 specifiers->type, 1);
1686 /* TODO: check constraints for struct declarations */
1687 /* TODO: check for doubled fields */
1688 record_declaration(declaration);
1690 if(token.type == ':') {
1692 parse_constant_expression();
1693 /* TODO (bitfields) */
1697 if(token.type != ',')
1704 static void parse_compound_type_entries(void)
1708 while(token.type != '}' && token.type != T_EOF) {
1709 declaration_specifiers_t specifiers;
1710 memset(&specifiers, 0, sizeof(specifiers));
1711 parse_declaration_specifiers(&specifiers);
1713 parse_struct_declarators(&specifiers);
1715 if(token.type == T_EOF) {
1716 parse_error("unexpected error while parsing struct");
1721 static void parse_declaration(void)
1723 source_position_t source_position = token.source_position;
1725 declaration_specifiers_t specifiers;
1726 memset(&specifiers, 0, sizeof(specifiers));
1727 parse_declaration_specifiers(&specifiers);
1729 if(token.type == ';') {
1732 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1734 declaration->type = specifiers.type;
1735 declaration->storage_class = specifiers.storage_class;
1736 declaration->source_position = source_position;
1737 record_declaration(declaration);
1740 parse_init_declarators(&specifiers);
1743 static type_t *parse_typename(void)
1745 declaration_specifiers_t specifiers;
1746 memset(&specifiers, 0, sizeof(specifiers));
1747 parse_declaration_specifiers(&specifiers);
1748 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1749 /* TODO: improve error message, user does probably not know what a
1750 * storage class is...
1752 parse_error("typename may not have a storage class");
1755 type_t *result = parse_abstract_declarator(specifiers.type);
1763 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1764 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1765 expression_t *left);
1767 typedef struct expression_parser_function_t expression_parser_function_t;
1768 struct expression_parser_function_t {
1769 unsigned precedence;
1770 parse_expression_function parser;
1771 unsigned infix_precedence;
1772 parse_expression_infix_function infix_parser;
1775 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1777 static expression_t *expected_expression_error(void)
1779 parser_print_error_prefix();
1780 fprintf(stderr, "expected expression, got token ");
1781 print_token(stderr, & token);
1782 fprintf(stderr, "\n");
1784 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1785 expression->type = EXPR_INVALID;
1791 static expression_t *parse_string_const(void)
1793 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1795 cnst->expression.type = EXPR_STRING_LITERAL;
1796 cnst->expression.datatype = type_string;
1797 cnst->value = parse_string_literals();
1799 return (expression_t*) cnst;
1802 static expression_t *parse_int_const(void)
1804 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1806 cnst->expression.type = EXPR_CONST;
1807 cnst->expression.datatype = type_int;
1808 cnst->v.int_value = token.v.intvalue;
1812 return (expression_t*) cnst;
1815 static expression_t *parse_float_const(void)
1817 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1819 cnst->expression.type = EXPR_CONST;
1820 cnst->expression.datatype = type_double;
1821 cnst->v.float_value = token.v.floatvalue;
1825 return (expression_t*) cnst;
1828 static declaration_t *create_implicit_function(symbol_t *symbol,
1829 const source_position_t source_position)
1831 function_type_t *function_type = allocate_type_zero(sizeof(function_type));
1833 function_type->type.type = TYPE_FUNCTION;
1834 function_type->result_type = type_int;
1835 function_type->unspecified_parameters = true;
1837 type_t *type = typehash_insert((type_t*) function_type);
1838 if(type != (type_t*) function_type) {
1839 free_type(function_type);
1842 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1844 declaration->storage_class = STORAGE_CLASS_EXTERN;
1845 declaration->type = type;
1846 declaration->symbol = symbol;
1847 declaration->source_position = source_position;
1849 /* prepend the implicit definition to the global context
1850 * this is safe since the symbol wasn't declared as anything else yet
1852 assert(symbol->declaration == NULL);
1854 context_t *last_context = context;
1855 context = global_context;
1857 environment_push(declaration);
1858 declaration->next = context->declarations;
1859 context->declarations = declaration;
1861 context = last_context;
1866 static expression_t *parse_reference(void)
1868 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1870 ref->expression.type = EXPR_REFERENCE;
1871 ref->symbol = token.v.symbol;
1873 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
1875 source_position_t source_position = token.source_position;
1878 if(declaration == NULL) {
1880 /* an implicitely defined function */
1881 if(token.type == '(') {
1882 parser_print_prefix_pos(token.source_position);
1883 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1884 ref->symbol->string);
1886 declaration = create_implicit_function(ref->symbol,
1891 parser_print_error_prefix();
1892 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1893 return (expression_t*) ref;
1897 ref->declaration = declaration;
1898 ref->expression.datatype = declaration->type;
1900 return (expression_t*) ref;
1903 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1907 /* TODO check if explicit cast is allowed and issue warnings/errors */
1910 static expression_t *parse_cast(void)
1912 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1914 cast->expression.type = EXPR_UNARY;
1915 cast->type = UNEXPR_CAST;
1916 cast->expression.source_position = token.source_position;
1918 type_t *type = parse_typename();
1921 expression_t *value = parse_sub_expression(20);
1923 check_cast_allowed(value, type);
1925 cast->expression.datatype = type;
1926 cast->value = value;
1928 return (expression_t*) cast;
1931 static expression_t *parse_statement_expression(void)
1933 statement_expression_t *expression
1934 = allocate_ast_zero(sizeof(expression[0]));
1935 expression->expression.type = EXPR_STATEMENT;
1936 expression->statement = parse_compound_statement();
1938 /* find last statement and use it's type */
1939 const statement_t *last_statement = NULL;
1940 const statement_t *statement = expression->statement;
1941 for( ; statement != NULL; statement = statement->next) {
1942 last_statement = statement;
1945 if(last_statement->type == STATEMENT_EXPRESSION) {
1946 const expression_statement_t *expression_statement =
1947 (const expression_statement_t*) last_statement;
1948 expression->expression.datatype
1949 = expression_statement->expression->datatype;
1951 expression->expression.datatype = type_void;
1956 return (expression_t*) expression;
1959 static expression_t *parse_brace_expression(void)
1963 switch(token.type) {
1965 /* gcc extension: a stement expression */
1966 return parse_statement_expression();
1970 return parse_cast();
1972 if(is_typedef_symbol(token.v.symbol)) {
1973 return parse_cast();
1977 expression_t *result = parse_expression();
1983 static expression_t *parse_function_keyword(void)
1985 eat(T___FUNCTION__);
1988 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1989 expression->expression.type = EXPR_FUNCTION;
1990 expression->expression.datatype = type_string;
1991 expression->value = "TODO: FUNCTION";
1993 return (expression_t*) expression;
1996 static expression_t *parse_pretty_function_keyword(void)
1998 eat(T___PRETTY_FUNCTION__);
2001 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2002 expression->expression.type = EXPR_PRETTY_FUNCTION;
2003 expression->expression.datatype = type_string;
2004 expression->value = "TODO: PRETTY FUNCTION";
2006 return (expression_t*) expression;
2009 static designator_t *parse_designator(void)
2011 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2013 if(token.type != T_IDENTIFIER) {
2014 parse_error_expected("problem while parsing member designator",
2019 result->symbol = token.v.symbol;
2022 designator_t *last_designator = result;
2024 if(token.type == '.') {
2026 if(token.type != T_IDENTIFIER) {
2027 parse_error_expected("problem while parsing member designator",
2032 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2033 designator->symbol = token.v.symbol;
2036 last_designator->next = designator;
2037 last_designator = designator;
2040 if(token.type == '[') {
2042 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2043 designator->array_access = parse_expression();
2044 if(designator->array_access == NULL) {
2050 last_designator->next = designator;
2051 last_designator = designator;
2060 static expression_t *parse_offsetof(void)
2062 eat(T___builtin_offsetof);
2064 offsetof_expression_t *expression
2065 = allocate_ast_zero(sizeof(expression[0]));
2066 expression->expression.type = EXPR_OFFSETOF;
2067 expression->expression.datatype = type_size_t;
2070 expression->type = parse_typename();
2072 expression->designator = parse_designator();
2075 return (expression_t*) expression;
2078 static expression_t *parse_va_arg(void)
2080 eat(T___builtin_va_arg);
2082 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2083 expression->expression.type = EXPR_VA_ARG;
2086 expression->arg = parse_assignment_expression();
2088 expression->expression.datatype = parse_typename();
2091 return (expression_t*) expression;
2094 static expression_t *parse_builtin_symbol(void)
2096 builtin_symbol_expression_t *expression
2097 = allocate_ast_zero(sizeof(expression[0]));
2098 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2100 /* TODO: set datatype */
2102 expression->symbol = token.v.symbol;
2106 return (expression_t*) expression;
2109 static expression_t *parse_primary_expression(void)
2111 switch(token.type) {
2113 return parse_int_const();
2114 case T_FLOATINGPOINT:
2115 return parse_float_const();
2116 case T_STRING_LITERAL:
2117 return parse_string_const();
2119 return parse_reference();
2120 case T___FUNCTION__:
2121 return parse_function_keyword();
2122 case T___PRETTY_FUNCTION__:
2123 return parse_pretty_function_keyword();
2124 case T___builtin_offsetof:
2125 return parse_offsetof();
2126 case T___builtin_va_arg:
2127 return parse_va_arg();
2128 case T___builtin_expect:
2129 case T___builtin_va_start:
2130 case T___builtin_va_end:
2131 return parse_builtin_symbol();
2134 return parse_brace_expression();
2137 parser_print_error_prefix();
2138 fprintf(stderr, "unexpected token ");
2139 print_token(stderr, &token);
2140 fprintf(stderr, "\n");
2143 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2144 expression->type = EXPR_INVALID;
2145 expression->datatype = type_void;
2150 static expression_t *parse_array_expression(unsigned precedence,
2151 expression_t *array_ref)
2157 array_access_expression_t *array_access
2158 = allocate_ast_zero(sizeof(array_access[0]));
2160 array_access->expression.type = EXPR_ARRAY_ACCESS;
2161 array_access->array_ref = array_ref;
2162 array_access->index = parse_expression();
2164 type_t *array_type = array_ref->datatype;
2165 if(array_type != NULL) {
2166 if(array_type->type == TYPE_POINTER) {
2167 pointer_type_t *pointer = (pointer_type_t*) array_type;
2168 array_access->expression.datatype = pointer->points_to;
2170 parser_print_error_prefix();
2171 fprintf(stderr, "array access on object with non-pointer type ");
2172 print_type(array_type);
2173 fprintf(stderr, "\n");
2177 if(token.type != ']') {
2178 parse_error_expected("Problem while parsing array access", ']', 0);
2179 return (expression_t*) array_access;
2183 return (expression_t*) array_access;
2186 static bool is_declaration_specifier(const token_t *token,
2187 bool only_type_specifiers)
2189 switch(token->type) {
2193 return is_typedef_symbol(token->v.symbol);
2196 if(only_type_specifiers)
2205 static expression_t *parse_sizeof(unsigned precedence)
2209 sizeof_expression_t *sizeof_expression
2210 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2211 sizeof_expression->expression.type = EXPR_SIZEOF;
2212 sizeof_expression->expression.datatype = type_size_t;
2214 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2216 sizeof_expression->type = parse_typename();
2219 expression_t *expression = parse_sub_expression(precedence);
2220 sizeof_expression->type = expression->datatype;
2221 sizeof_expression->size_expression = expression;
2224 return (expression_t*) sizeof_expression;
2227 static expression_t *parse_select_expression(unsigned precedence,
2228 expression_t *compound)
2232 assert(token.type == '.' || token.type == T_MINUSGREATER);
2235 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2237 select->expression.type = EXPR_SELECT;
2238 select->compound = compound;
2240 /* TODO: datatype */
2242 if(token.type != T_IDENTIFIER) {
2243 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2244 return (expression_t*) select;
2246 select->symbol = token.v.symbol;
2249 return (expression_t*) select;
2252 static expression_t *parse_call_expression(unsigned precedence,
2253 expression_t *expression)
2256 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2257 call->expression.type = EXPR_CALL;
2258 call->function = expression;
2260 type_t *type = expression->datatype;
2261 if(type->type != TYPE_FUNCTION) {
2262 /* TODO calling pointers to functions is ok */
2263 parser_print_error_prefix();
2264 fputs("called object '", stderr);
2265 print_expression(expression);
2266 fputs("' (type ", stderr);
2268 fputs("is not a function\n", stderr);
2270 call->expression.datatype = NULL;
2272 function_type_t *function_type = (function_type_t*) type;
2273 call->expression.datatype = function_type->result_type;
2276 /* parse arguments */
2279 if(token.type != ')') {
2280 call_argument_t *last_argument = NULL;
2283 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2285 argument->expression = parse_assignment_expression();
2286 if(last_argument == NULL) {
2287 call->arguments = argument;
2289 last_argument->next = argument;
2291 last_argument = argument;
2293 if(token.type != ',')
2300 return (expression_t*) call;
2303 static void type_error(const char *msg, const source_position_t source_position,
2306 parser_print_error_prefix_pos(source_position);
2307 fprintf(stderr, "%s, but found type ", msg);
2309 fputc('\n', stderr);
2313 static void type_error_incompatible(const char *msg,
2314 const source_position_t source_position, type_t *type1, type_t *type2)
2316 parser_print_error_prefix_pos(source_position);
2317 fprintf(stderr, "%s, incompatible types: ", msg);
2319 fprintf(stderr, " - ");
2321 fprintf(stderr, ")\n");
2325 static type_t *get_type_after_conversion(const type_t *type1,
2326 const type_t *type2)
2330 return (type_t*) type1;
2333 static expression_t *create_cast_expression(expression_t *expression,
2336 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2338 cast->expression.type = EXPR_UNARY;
2339 cast->type = UNEXPR_CAST;
2340 cast->value = expression;
2341 cast->expression.datatype = dest_type;
2343 return (expression_t*) cast;
2346 static expression_t *create_cast(expression_t *expression, type_t *dest_type)
2348 assert(expression->datatype != NULL);
2349 type_t *source_type = expression->datatype;
2351 if(expression->datatype == dest_type)
2354 if(dest_type->type == TYPE_ATOMIC) {
2355 if(source_type->type != TYPE_ATOMIC)
2356 panic("casting of non-atomic types not implemented yet");
2358 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
2359 type_error_incompatible("can't cast types",
2360 expression->source_position,
2361 source_type, dest_type);
2365 return create_cast_expression(expression, dest_type);
2368 panic("casting of non-atomic types not implemented yet");
2371 static expression_t *parse_conditional_expression(unsigned precedence,
2372 expression_t *expression)
2376 conditional_expression_t *conditional
2377 = allocate_ast_zero(sizeof(conditional[0]));
2378 conditional->expression.type = EXPR_CONDITIONAL;
2379 conditional->condition = expression;
2382 type_t *condition_type = conditional->condition->datatype;
2383 if(condition_type != NULL) {
2384 if(!is_type_scalar(condition_type)) {
2385 type_error("expected a scalar type", expression->source_position,
2390 conditional->true_expression = parse_expression();
2392 conditional->false_expression = parse_sub_expression(precedence);
2394 type_t *true_type = conditional->true_expression->datatype;
2395 if(true_type == NULL)
2396 return (expression_t*) conditional;
2397 type_t *false_type = conditional->false_expression->datatype;
2398 if(false_type == NULL)
2399 return (expression_t*) conditional;
2402 if(true_type == false_type) {
2403 conditional->expression.datatype = true_type;
2404 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2405 type_t *result = get_type_after_conversion(true_type, false_type);
2406 /* TODO: create implicit convs if necessary */
2407 conditional->expression.datatype = result;
2408 } else if(true_type->type == TYPE_POINTER &&
2409 false_type->type == TYPE_POINTER &&
2410 true /* TODO compatible points_to types */) {
2412 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2413 || (is_null_ptr_const(false_type) &&
2414 true_type->type == TYPE_POINTER) TODO*/ false) {
2416 } else if(/* 1 is pointer to object type, other is void* */ false) {
2419 type_error_incompatible("problem while parsing conditional",
2420 expression->source_position, true_type,
2424 return (expression_t*) conditional;
2427 static expression_t *parse_extension(unsigned precedence)
2429 eat(T___extension__);
2431 /* TODO enable extensions */
2433 return parse_sub_expression(precedence);
2436 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2439 return expression->datatype;
2442 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2444 type_t *expression_type = expression->datatype;
2446 if(expression_type->type == TYPE_POINTER) {
2447 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2448 return pointer_type->points_to;
2450 panic("deref TODO...");
2454 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2456 type_t *type = expression->datatype;
2457 return make_pointer_type(type, 0);
2460 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2461 static expression_t *parse_##unexpression_type(unsigned precedence) \
2465 unary_expression_t *unary_expression \
2466 = allocate_ast_zero(sizeof(unary_expression[0])); \
2467 unary_expression->expression.type = EXPR_UNARY; \
2468 unary_expression->type = unexpression_type; \
2469 unary_expression->value = parse_sub_expression(precedence); \
2470 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2472 return (expression_t*) unary_expression; \
2475 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2476 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2477 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2478 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2479 get_unexpr_dereference_type)
2480 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2481 get_unexpr_take_addr_type)
2482 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2483 get_unexpr_arithmetic_type)
2484 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2485 get_unexpr_arithmetic_type)
2486 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2487 get_unexpr_arithmetic_type)
2489 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2491 static expression_t *parse_##unexpression_type(unsigned precedence, \
2492 expression_t *left) \
2494 (void) precedence; \
2497 unary_expression_t *unary_expression \
2498 = allocate_ast_zero(sizeof(unary_expression[0])); \
2499 unary_expression->expression.type = EXPR_UNARY; \
2500 unary_expression->type = unexpression_type; \
2501 unary_expression->value = left; \
2502 unary_expression->expression.datatype = tfunc(left); \
2504 return (expression_t*) unary_expression; \
2507 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2508 get_unexpr_arithmetic_type)
2509 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2510 get_unexpr_arithmetic_type)
2512 static int get_rank(const type_t *type)
2514 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
2515 * and esp. footnote 108). However we can't fold constants (yet), so we
2516 * can't decide wether unsigned int is possible, while int always works.
2517 * (unsigned int would be preferable when possible... for stuff like
2518 * struct { enum { ... } bla : 4; } ) */
2519 if(type->type == TYPE_ENUM)
2520 return ATOMIC_TYPE_INT;
2522 assert(type->type == TYPE_ATOMIC);
2523 atomic_type_t *atomic_type = (atomic_type_t*) type;
2524 atomic_type_type_t atype = atomic_type->atype;
2528 static void semantic_arithmetic(expression_t **left, expression_t **right)
2530 type_t *type_left = (*left)->datatype;
2531 type_t *type_right = (*right)->datatype;
2532 type_left = skip_typeref(type_left);
2533 type_right = skip_typeref(type_right);
2535 /* TODO: handle complex + imaginary types */
2537 /* § 6.3.1.8 Usual arithmetic conversions */
2538 if(type_left == type_long_double || type_right == type_long_double) {
2539 type_left = type_long_double;
2540 type_right = type_long_double;
2542 } else if(type_left == type_double || type_right == type_double) {
2543 type_left = type_double;
2544 type_right = type_double;
2546 } else if(type_left == type_float || type_right == type_float) {
2547 type_left = type_float;
2548 type_right = type_float;
2552 /* integer promotion */
2553 if(get_rank(type_left) < ATOMIC_TYPE_INT)
2554 type_left = type_int;
2555 if(get_rank(type_right) < ATOMIC_TYPE_INT)
2556 type_right = type_int;
2558 if(type_left == type_right)
2561 bool signed_left = is_type_signed(type_left);
2562 bool signed_right = is_type_signed(type_right);
2563 if(get_rank(type_left) < get_rank(type_right)) {
2564 if(signed_left == signed_right || !signed_right) {
2565 type_left = type_right;
2567 type_right = type_left;
2570 if(signed_left == signed_right || !signed_left) {
2571 type_right = type_left;
2573 type_left = type_right;
2578 assert(type_left == type_right);
2579 *left = create_cast(*left, type_left);
2580 *right = create_cast(*right, type_right);
2583 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2585 expression_t *left = expression->left;
2586 expression_t *right = expression->right;
2587 type_t *type_left = skip_typeref(left->datatype);
2588 type_t *type_right = skip_typeref(right->datatype);
2590 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2591 /* TODO: improve error message */
2592 parser_print_error_prefix();
2593 fprintf(stderr, "operation needs arithmetic types\n");
2597 semantic_arithmetic(&expression->left, &expression->right);
2598 expression->expression.datatype = expression->left->datatype;
2601 static void semantic_comparison(binary_expression_t *expression)
2603 expression_t *left = expression->left;
2604 expression_t *right = expression->right;
2605 type_t *type_left = left->datatype;
2606 type_t *type_right = right->datatype;
2608 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2609 semantic_arithmetic(&expression->left, &expression->right);
2611 expression->expression.datatype = type_int;
2614 static void semantic_arithmetic_assign(binary_expression_t *expression)
2616 expression_t *left = expression->left;
2617 expression_t *right = expression->right;
2618 type_t *type_left = left->datatype;
2619 type_t *type_right = right->datatype;
2621 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2622 /* TODO: improve error message */
2623 parser_print_error_prefix();
2624 fprintf(stderr, "operation needs arithmetic types\n");
2628 semantic_arithmetic(&expression->left, &expression->right);
2629 /* note that we assign the original type_left before casting */
2630 expression->expression.datatype = type_left;
2633 static void semantic_logical_op(binary_expression_t *expression)
2636 expression->expression.datatype = type_int;
2639 static void semantic_assign(type_t *orig_type_left, expression_t **right,
2642 type_t *orig_type_right = (*right)->datatype;
2643 type_t *type_left = skip_typeref(orig_type_left);
2644 type_t *type_right = skip_typeref(orig_type_right);
2646 if(type_left == type_right) {
2648 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2649 *right = create_cast(*right, type_left);
2650 } else if(type_left->type == TYPE_POINTER
2651 && type_right->type == TYPE_POINTER) {
2654 /* TODO: improve error message */
2655 parser_print_error_prefix();
2656 fprintf(stderr, "incompatible types in %s\n",
2657 is_return ? "'return'" : "assignment");
2658 parser_print_error_prefix();
2659 print_type(type_left);
2660 fputs(" <- ", stderr);
2661 print_type(type_right);
2662 fputs("\n", stderr);
2667 static void semantic_binexpr_assign(binary_expression_t *expression)
2669 expression_t *left = expression->left;
2670 type_t *type_left = left->datatype;
2672 semantic_assign(type_left, &expression->right, false);
2674 expression->expression.datatype = type_left;
2677 static void semantic_comma(binary_expression_t *expression)
2679 expression->expression.datatype = expression->right->datatype;
2682 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc) \
2683 static expression_t *parse_##binexpression_type(unsigned precedence, \
2684 expression_t *left) \
2688 expression_t *right = parse_sub_expression(precedence); \
2690 binary_expression_t *binexpr \
2691 = allocate_ast_zero(sizeof(binexpr[0])); \
2692 binexpr->expression.type = EXPR_BINARY; \
2693 binexpr->type = binexpression_type; \
2694 binexpr->left = left; \
2695 binexpr->right = right; \
2698 return (expression_t*) binexpr; \
2701 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma)
2702 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic)
2703 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic)
2704 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic)
2705 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_binexpr_arithmetic)
2706 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_binexpr_arithmetic)
2707 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison)
2708 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison)
2709 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign)
2710 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison)
2711 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2712 semantic_comparison)
2713 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison)
2714 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2715 semantic_comparison)
2716 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic)
2717 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic)
2718 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic)
2719 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op)
2720 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op)
2721 /* TODO shift has a bit special semantic */
2722 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2723 semantic_binexpr_arithmetic)
2724 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2725 semantic_binexpr_arithmetic)
2726 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2727 semantic_arithmetic_assign)
2728 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2729 semantic_arithmetic_assign)
2730 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2731 semantic_arithmetic_assign)
2732 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2733 semantic_arithmetic_assign)
2734 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2735 semantic_arithmetic_assign)
2736 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2737 semantic_arithmetic_assign)
2738 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2739 semantic_arithmetic_assign)
2740 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2741 semantic_arithmetic_assign)
2742 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2743 semantic_arithmetic_assign)
2744 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2745 semantic_arithmetic_assign)
2747 static expression_t *parse_sub_expression(unsigned precedence)
2749 if(token.type < 0) {
2750 return expected_expression_error();
2753 expression_parser_function_t *parser
2754 = &expression_parsers[token.type];
2755 source_position_t source_position = token.source_position;
2758 if(parser->parser != NULL) {
2759 left = parser->parser(parser->precedence);
2761 left = parse_primary_expression();
2763 assert(left != NULL);
2764 left->source_position = source_position;
2767 if(token.type < 0) {
2768 return expected_expression_error();
2771 parser = &expression_parsers[token.type];
2772 if(parser->infix_parser == NULL)
2774 if(parser->infix_precedence < precedence)
2777 left = parser->infix_parser(parser->infix_precedence, left);
2779 assert(left != NULL);
2780 assert(left->type != EXPR_INVALID);
2781 left->source_position = source_position;
2787 static expression_t *parse_expression(void)
2789 return parse_sub_expression(1);
2794 static void register_expression_parser(parse_expression_function parser,
2795 int token_type, unsigned precedence)
2797 expression_parser_function_t *entry = &expression_parsers[token_type];
2799 if(entry->parser != NULL) {
2800 fprintf(stderr, "for token ");
2801 print_token_type(stderr, token_type);
2802 fprintf(stderr, "\n");
2803 panic("trying to register multiple expression parsers for a token");
2805 entry->parser = parser;
2806 entry->precedence = precedence;
2809 static void register_expression_infix_parser(
2810 parse_expression_infix_function parser, int token_type,
2811 unsigned precedence)
2813 expression_parser_function_t *entry = &expression_parsers[token_type];
2815 if(entry->infix_parser != NULL) {
2816 fprintf(stderr, "for token ");
2817 print_token_type(stderr, token_type);
2818 fprintf(stderr, "\n");
2819 panic("trying to register multiple infix expression parsers for a "
2822 entry->infix_parser = parser;
2823 entry->infix_precedence = precedence;
2826 static void init_expression_parsers(void)
2828 memset(&expression_parsers, 0, sizeof(expression_parsers));
2830 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2831 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2832 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2833 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2834 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2835 T_GREATERGREATER, 16);
2836 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2837 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2838 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2839 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2840 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2841 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2842 T_GREATEREQUAL, 14);
2843 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2844 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2845 T_EXCLAMATIONMARKEQUAL, 13);
2846 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2847 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2848 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2849 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2850 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2851 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2852 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2853 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2854 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2855 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2856 T_ASTERISKEQUAL, 2);
2857 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2858 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2860 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2861 T_LESSLESSEQUAL, 2);
2862 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2863 T_GREATERGREATEREQUAL, 2);
2864 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2866 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2868 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2871 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2873 register_expression_infix_parser(parse_array_expression, '[', 30);
2874 register_expression_infix_parser(parse_call_expression, '(', 30);
2875 register_expression_infix_parser(parse_select_expression, '.', 30);
2876 register_expression_infix_parser(parse_select_expression,
2877 T_MINUSGREATER, 30);
2878 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2880 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2883 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2884 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2885 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2886 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2887 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2888 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2889 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2890 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2891 register_expression_parser(parse_sizeof, T_sizeof, 25);
2892 register_expression_parser(parse_extension, T___extension__, 25);
2896 static statement_t *parse_case_statement(void)
2899 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2900 label->statement.type = STATEMENT_CASE_LABEL;
2901 label->statement.source_position = token.source_position;
2903 label->expression = parse_expression();
2906 label->statement.next = parse_statement();
2908 return (statement_t*) label;
2911 static statement_t *parse_default_statement(void)
2915 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2916 label->statement.type = STATEMENT_CASE_LABEL;
2917 label->statement.source_position = token.source_position;
2920 label->statement.next = parse_statement();
2922 return (statement_t*) label;
2925 static statement_t *parse_label_statement(void)
2934 static statement_t *parse_if(void)
2938 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2939 statement->statement.type = STATEMENT_IF;
2940 statement->statement.source_position = token.source_position;
2943 statement->condition = parse_expression();
2946 statement->true_statement = parse_statement();
2947 if(token.type == T_else) {
2949 statement->false_statement = parse_statement();
2952 return (statement_t*) statement;
2955 static statement_t *parse_switch(void)
2959 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2960 statement->statement.type = STATEMENT_SWITCH;
2961 statement->statement.source_position = token.source_position;
2964 statement->expression = parse_expression();
2966 statement->body = parse_statement();
2968 return (statement_t*) statement;
2971 static statement_t *parse_while(void)
2975 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2976 statement->statement.type = STATEMENT_WHILE;
2977 statement->statement.source_position = token.source_position;
2980 statement->condition = parse_expression();
2982 statement->body = parse_statement();
2984 return (statement_t*) statement;
2987 static statement_t *parse_do(void)
2991 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2992 statement->statement.type = STATEMENT_DO_WHILE;
2993 statement->statement.source_position = token.source_position;
2995 statement->body = parse_statement();
2998 statement->condition = parse_expression();
3002 return (statement_t*) statement;
3005 static statement_t *parse_for(void)
3009 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3010 statement->statement.type = STATEMENT_FOR;
3011 statement->statement.source_position = token.source_position;
3015 int top = environment_top();
3016 context_t *last_context = context;
3017 set_context(&statement->context);
3019 if(token.type != ';') {
3020 if(is_declaration_specifier(&token, false)) {
3021 parse_declaration();
3023 statement->initialisation = parse_expression();
3030 if(token.type != ';') {
3031 statement->condition = parse_expression();
3034 if(token.type != ')') {
3035 statement->step = parse_expression();
3038 statement->body = parse_statement();
3040 assert(context == &statement->context);
3041 set_context(last_context);
3042 environment_pop_to(top);
3044 return (statement_t*) statement;
3047 static statement_t *parse_goto(void)
3050 expect(T_IDENTIFIER);
3056 static statement_t *parse_continue(void)
3061 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3062 statement->source_position = token.source_position;
3063 statement->type = STATEMENT_CONTINUE;
3068 static statement_t *parse_break(void)
3073 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3074 statement->source_position = token.source_position;
3075 statement->type = STATEMENT_BREAK;
3080 static statement_t *parse_return(void)
3084 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3086 statement->statement.type = STATEMENT_RETURN;
3087 statement->statement.source_position = token.source_position;
3089 assert(current_function->type->type == TYPE_FUNCTION);
3090 function_type_t *function_type = (function_type_t*) current_function->type;
3091 type_t *return_type = function_type->result_type;
3093 expression_t *return_value;
3094 if(token.type != ';') {
3095 return_value = parse_expression();
3097 if(return_type == type_void && return_value->datatype != type_void) {
3098 parse_warning("'return' with a value, in function returning void");
3099 return_value = NULL;
3101 semantic_assign(return_type, &return_value, true);
3104 return_value = NULL;
3105 parse_warning("'return' without value, in function retruning non-void");
3107 statement->return_value = return_value;
3111 return (statement_t*) statement;
3114 static statement_t *parse_declaration_statement(void)
3116 declaration_t *before = last_declaration;
3118 declaration_statement_t *statement
3119 = allocate_ast_zero(sizeof(statement[0]));
3120 statement->statement.type = STATEMENT_DECLARATION;
3121 statement->statement.source_position = token.source_position;
3123 declaration_specifiers_t specifiers;
3124 memset(&specifiers, 0, sizeof(specifiers));
3125 parse_declaration_specifiers(&specifiers);
3127 if(token.type == ';') {
3130 parse_init_declarators(&specifiers);
3133 if(before == NULL) {
3134 statement->declarations_begin = context->declarations;
3136 statement->declarations_begin = before->next;
3138 statement->declarations_end = last_declaration;
3140 return (statement_t*) statement;
3143 static statement_t *parse_expression_statement(void)
3145 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3146 statement->statement.type = STATEMENT_EXPRESSION;
3147 statement->statement.source_position = token.source_position;
3149 statement->expression = parse_expression();
3153 return (statement_t*) statement;
3156 static statement_t *parse_statement(void)
3158 statement_t *statement = NULL;
3160 /* declaration or statement */
3161 switch(token.type) {
3163 statement = parse_case_statement();
3167 statement = parse_default_statement();
3171 statement = parse_compound_statement();
3175 statement = parse_if();
3179 statement = parse_switch();
3183 statement = parse_while();
3187 statement = parse_do();
3191 statement = parse_for();
3195 statement = parse_goto();
3199 statement = parse_continue();
3203 statement = parse_break();
3207 statement = parse_return();
3216 if(look_ahead(1)->type == ':') {
3217 statement = parse_label_statement();
3221 if(is_typedef_symbol(token.v.symbol)) {
3222 statement = parse_declaration_statement();
3226 statement = parse_expression_statement();
3229 case T___extension__:
3230 /* this can be a prefix to a declaration or an expression statement */
3231 /* we simply eat it now and parse the rest with tail recursion */
3234 } while(token.type == T___extension__);
3235 statement = parse_statement();
3239 statement = parse_declaration_statement();
3243 statement = parse_expression_statement();
3247 assert(statement == NULL || statement->source_position.input_name != NULL);
3252 static statement_t *parse_compound_statement(void)
3256 compound_statement_t *compound_statement
3257 = allocate_ast_zero(sizeof(compound_statement[0]));
3258 compound_statement->statement.type = STATEMENT_COMPOUND;
3259 compound_statement->statement.source_position = token.source_position;
3261 int top = environment_top();
3262 context_t *last_context = context;
3263 set_context(&compound_statement->context);
3265 statement_t *last_statement = NULL;
3267 while(token.type != '}' && token.type != T_EOF) {
3268 statement_t *statement = parse_statement();
3269 if(statement == NULL)
3272 if(last_statement != NULL) {
3273 last_statement->next = statement;
3275 compound_statement->statements = statement;
3278 while(statement->next != NULL)
3279 statement = statement->next;
3281 last_statement = statement;
3284 assert(context == &compound_statement->context);
3285 set_context(last_context);
3286 environment_pop_to(top);
3290 return (statement_t*) compound_statement;
3293 static translation_unit_t *parse_translation_unit(void)
3295 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3297 assert(global_context == NULL);
3298 global_context = &unit->context;
3300 assert(context == NULL);
3301 set_context(&unit->context);
3303 while(token.type != T_EOF) {
3304 parse_declaration();
3307 assert(context == &unit->context);
3309 last_declaration = NULL;
3311 assert(global_context == &unit->context);
3312 global_context = NULL;
3317 translation_unit_t *parse(void)
3319 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3320 found_error = false;
3322 type_set_output(stderr);
3323 ast_set_output(stderr);
3325 lookahead_bufpos = 0;
3326 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3329 translation_unit_t *unit = parse_translation_unit();
3331 DEL_ARR_F(environment_stack);
3339 void init_parser(void)
3341 init_expression_parsers();
3342 obstack_init(&temp_obst);
3344 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3345 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3346 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3347 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3348 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3349 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3350 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3351 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3352 type_string = make_pointer_type(type_const_char, 0);
3355 void exit_parser(void)
3357 obstack_free(&temp_obst, NULL);