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 struct obstack temp_obst;
37 static type_t *type_int = NULL;
38 static type_t *type_double = NULL;
39 static type_t *type_const_char = NULL;
40 static type_t *type_string = NULL;
41 static type_t *type_void = NULL;
42 static type_t *type_size_t = NULL;
44 static statement_t *parse_compound_statement(void);
45 static statement_t *parse_statement(void);
47 static expression_t *parse_sub_expression(unsigned precedence);
48 static expression_t *parse_expression(void);
49 static type_t *parse_typename(void);
51 #define STORAGE_CLASSES \
58 #define TYPE_QUALIFIERS \
64 #ifdef PROVIDE_COMPLEX
65 #define COMPLEX_SPECIFIERS \
68 #define COMPLEX_SPECIFIERS
71 #ifdef PROVIDE_IMAGINARY
72 #define IMAGINARY_SPECIFIERS \
75 #define IMAGINARY_SPECIFIERS
78 #define TYPE_SPECIFIERS \
96 #define DECLARATION_START \
101 #define TYPENAME_START \
105 static inline void *allocate_ast_zero(size_t size)
107 void *res = allocate_ast(size);
108 memset(res, 0, size);
112 static inline void *allocate_type_zero(size_t size)
114 void *res = obstack_alloc(type_obst, size);
115 memset(res, 0, size);
119 static inline void free_type(void *type)
121 obstack_free(type_obst, type);
125 * returns the top element of the environment stack
127 static inline size_t environment_top(void)
129 return ARR_LEN(environment_stack);
134 static inline void next_token(void)
136 token = lookahead_buffer[lookahead_bufpos];
137 lookahead_buffer[lookahead_bufpos] = lexer_token;
140 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
143 print_token(stderr, &token);
144 fprintf(stderr, "\n");
148 static inline const token_t *look_ahead(int num)
150 assert(num > 0 && num <= MAX_LOOKAHEAD);
151 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
152 return & lookahead_buffer[pos];
155 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
159 #ifdef ABORT_ON_ERROR
164 void parser_print_prefix_pos(const source_position_t source_position)
166 fputs(source_position.input_name, stderr);
168 fprintf(stderr, "%d", source_position.linenr);
172 void parser_print_error_prefix_pos(const source_position_t source_position)
174 parser_print_prefix_pos(source_position);
175 fputs("error: ", stderr);
179 void parser_print_error_prefix(void)
181 parser_print_prefix_pos(token.source_position);
185 static void parse_error(const char *message)
187 parser_print_error_prefix();
188 fprintf(stderr, "parse error: %s\n", message);
191 __attribute__((unused))
192 static void parse_warning(const char *message)
194 parser_print_prefix_pos(token.source_position);
195 fprintf(stderr, "warning: %s\n", message);
198 static void parse_error_expected(const char *message, ...)
203 if(message != NULL) {
204 parser_print_error_prefix();
205 fprintf(stderr, "%s\n", message);
207 parser_print_error_prefix();
208 fputs("Parse error: got ", stderr);
209 print_token(stderr, &token);
210 fputs(", expected ", stderr);
212 va_start(args, message);
213 token_type_t token_type = va_arg(args, token_type_t);
214 while(token_type != 0) {
218 fprintf(stderr, ", ");
220 print_token_type(stderr, token_type);
221 token_type = va_arg(args, token_type_t);
224 fprintf(stderr, "\n");
227 static void eat_block(void)
229 if(token.type == '{')
232 while(token.type != '}') {
233 if(token.type == T_EOF)
235 if(token.type == '{') {
244 static void eat_statement(void)
246 while(token.type != ';') {
247 if(token.type == T_EOF)
249 if(token.type == '}')
251 if(token.type == '{') {
260 static void eat_brace(void)
262 if(token.type == '(')
265 while(token.type != ')') {
266 if(token.type == T_EOF)
268 if(token.type == ')' || token.type == ';' || token.type == '}') {
271 if(token.type == '(') {
275 if(token.type == '{') {
284 #define expect(expected) \
285 if(UNLIKELY(token.type != (expected))) { \
286 parse_error_expected(NULL, (expected), 0); \
292 #define expect_void(expected) \
293 if(UNLIKELY(token.type != (expected))) { \
294 parse_error_expected(NULL, (expected), 0); \
300 static void set_context(context_t *new_context)
302 context = new_context;
304 last_declaration = new_context->declarations;
305 if(last_declaration != NULL) {
306 while(last_declaration->next != NULL) {
307 last_declaration = last_declaration->next;
313 * called when we find a 2nd declarator for an identifier we already have a
316 static bool is_compatible_declaration (declaration_t *declaration,
317 declaration_t *previous)
319 /* TODO: not correct yet */
320 return declaration->type == previous->type;
323 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
325 declaration_t *declaration = symbol->declaration;
326 for( ; declaration != NULL; declaration = declaration->symbol_next) {
327 if(declaration->namespace == namespace)
334 static const char *get_namespace_prefix(namespace_t namespace)
337 case NAMESPACE_NORMAL:
339 case NAMESPACE_UNION:
341 case NAMESPACE_STRUCT:
346 panic("invalid namespace found");
350 * pushs an environment_entry on the environment stack and links the
351 * corresponding symbol to the new entry
353 static declaration_t *environment_push(declaration_t *declaration)
355 symbol_t *symbol = declaration->symbol;
356 namespace_t namespace = declaration->namespace;
357 assert(declaration->source_position.input_name != NULL);
359 /* a declaration should be only pushed once */
360 assert(declaration->parent_context == NULL);
361 declaration->parent_context = context;
363 declaration_t *previous_declaration = get_declaration(symbol, namespace);
364 assert(declaration != previous_declaration);
365 if(previous_declaration != NULL
366 && previous_declaration->parent_context == context) {
367 if(!is_compatible_declaration(declaration, previous_declaration)) {
368 parser_print_error_prefix_pos(declaration->source_position);
369 fprintf(stderr, "definition of symbol %s%s with type ",
370 get_namespace_prefix(namespace), symbol->string);
372 print_type(declaration->type);
374 parser_print_error_prefix_pos(
375 previous_declaration->source_position);
376 fprintf(stderr, "is incompatible with previous declaration "
378 print_type(previous_declaration->type);
381 return previous_declaration;
384 /* remember old declaration */
386 entry.symbol = symbol;
387 entry.old_declaration = symbol->declaration;
388 entry.namespace = namespace;
389 ARR_APP1(environment_stack, entry);
391 /* replace/add declaration into declaration list of the symbol */
392 if(symbol->declaration == NULL) {
393 symbol->declaration = declaration;
395 declaration_t *iter = symbol->declaration;
396 for( ; iter != NULL; iter = iter->symbol_next) {
397 declaration_t *symbol_next = iter->symbol_next;
398 if(symbol_next == NULL) {
399 iter->symbol_next = declaration;
400 assert(declaration->symbol_next == NULL);
403 if(symbol_next->namespace == namespace) {
404 iter->symbol_next = declaration;
405 declaration->symbol_next = symbol_next->symbol_next;
415 * pops symbols from the environment stack until @p new_top is the top element
417 static void environment_pop_to(size_t new_top)
419 size_t top = ARR_LEN(environment_stack);
422 assert(new_top <= top);
426 for(i = top; i > new_top; --i) {
427 stack_entry_t *entry = & environment_stack[i - 1];
429 declaration_t *old_declaration = entry->old_declaration;
430 symbol_t *symbol = entry->symbol;
431 namespace_t namespace = entry->namespace;
433 /* replace/remove declaration */
434 declaration_t *declaration = symbol->declaration;
435 assert(declaration != NULL);
436 if(declaration->namespace == namespace) {
437 if(old_declaration == NULL) {
438 symbol->declaration = declaration->symbol_next;
440 symbol->declaration = old_declaration;
441 assert(old_declaration->symbol_next ==
442 declaration->symbol_next);
445 for(; declaration != NULL; declaration = declaration->symbol_next) {
446 declaration_t *symbol_next = declaration->symbol_next;
447 if(symbol_next->namespace == namespace) {
448 declaration->symbol_next = old_declaration;
449 assert(old_declaration->symbol_next
450 == symbol_next->symbol_next);
454 assert(declaration != NULL);
458 ARR_SHRINKLEN(environment_stack, (int) new_top);
463 static expression_t *parse_constant_expression(void)
465 /* start parsing at precedence 7 (conditional expression) */
466 return parse_sub_expression(7);
469 static expression_t *parse_assignment_expression(void)
471 /* start parsing at precedence 2 (assignment expression) */
472 return parse_sub_expression(2);
475 static void parse_compound_type_entries(void);
476 static declaration_t *parse_declarator(storage_class_t storage_class,
477 type_t *type, int may_be_abstract);
478 static declaration_t *record_declaration(declaration_t *declaration);
480 typedef struct declaration_specifiers_t declaration_specifiers_t;
481 struct declaration_specifiers_t {
482 storage_class_t storage_class;
486 static const char *parse_string_literals(void)
488 assert(token.type == T_STRING_LITERAL);
489 const char *result = token.v.string;
493 while(token.type == T_STRING_LITERAL) {
494 result = concat_strings(result, token.v.string);
501 static void parse_attributes(void)
505 case T___attribute__:
513 parse_error("EOF while parsing attribute");
531 if(token.type != T_STRING_LITERAL) {
532 parse_error_expected("while parsing assembler attribute",
537 parse_string_literals();
542 goto attributes_finished;
550 static designator_t *parse_designation(void)
552 if(token.type != '[' && token.type != '.')
555 designator_t *result = NULL;
556 designator_t *last = NULL;
559 designator_t *designator;
562 designator = allocate_ast_zero(sizeof(designator[0]));
564 designator->array_access = parse_constant_expression();
568 designator = allocate_ast_zero(sizeof(designator[0]));
570 if(token.type != T_IDENTIFIER) {
571 parse_error_expected("problem while parsing designator",
575 designator->symbol = token.v.symbol;
583 assert(designator != NULL);
585 last->next = designator;
593 static initializer_t *parse_initializer_list(void);
595 static initializer_t *parse_initializer(void)
597 designator_t *designator = parse_designation();
599 initializer_t *result;
600 if(token.type == '{') {
601 result = parse_initializer_list();
603 result = allocate_ast_zero(sizeof(result[0]));
604 result->type = INITIALIZER_VALUE;
605 result->v.value = parse_assignment_expression();
607 result->designator = designator;
612 static initializer_t *parse_initializer_list(void)
616 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
617 result->type = INITIALIZER_LIST;
619 initializer_t *last = NULL;
621 initializer_t *initializer = parse_initializer();
623 last->next = initializer;
625 result->v.list = initializer;
629 if(token.type == '}')
632 if(token.type != ',') {
633 parse_error_expected("problem while parsing initializer list",
640 if(token.type == '}')
649 static declaration_t *parse_compound_type_specifier(bool is_struct)
657 symbol_t *symbol = NULL;
658 declaration_t *declaration = NULL;
660 if(token.type == T_IDENTIFIER) {
661 symbol = token.v.symbol;
665 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
667 declaration = get_declaration(symbol, NAMESPACE_UNION);
669 } else if(token.type != '{') {
671 parse_error_expected("problem while parsing struct type specifier",
672 T_IDENTIFIER, '{', 0);
674 parse_error_expected("problem while parsing union type specifier",
675 T_IDENTIFIER, '{', 0);
681 if(declaration == NULL) {
682 declaration = allocate_type_zero(sizeof(declaration[0]));
685 declaration->namespace = NAMESPACE_STRUCT;
687 declaration->namespace = NAMESPACE_UNION;
689 declaration->source_position = token.source_position;
690 declaration->symbol = symbol;
693 if(token.type == '{') {
694 if(declaration->init.is_defined) {
695 assert(symbol != NULL);
696 parser_print_error_prefix();
697 fprintf(stderr, "multiple definition of %s %s\n",
698 is_struct ? "struct" : "union", symbol->string);
699 declaration->context.declarations = NULL;
701 record_declaration(declaration);
702 declaration->init.is_defined = true;
704 int top = environment_top();
705 context_t *last_context = context;
706 set_context(& declaration->context);
708 parse_compound_type_entries();
711 assert(context == & declaration->context);
712 set_context(last_context);
713 environment_pop_to(top);
719 static void parse_enum_entries(void)
723 if(token.type == '}') {
725 parse_error("empty enum not allowed");
730 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
732 if(token.type != T_IDENTIFIER) {
733 parse_error_expected("problem while parsing enum entry",
738 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
739 entry->symbol = token.v.symbol;
740 entry->source_position = token.source_position;
743 if(token.type == '=') {
745 entry->init.initializer = parse_initializer();
748 record_declaration(entry);
750 if(token.type != ',')
753 } while(token.type != '}');
758 static declaration_t *parse_enum_specifier(void)
762 declaration_t *declaration;
765 if(token.type == T_IDENTIFIER) {
766 symbol = token.v.symbol;
769 declaration = get_declaration(symbol, NAMESPACE_ENUM);
770 } else if(token.type != '{') {
771 parse_error_expected("problem while parsing enum type specifier",
772 T_IDENTIFIER, '{', 0);
779 if(declaration == NULL) {
780 declaration = allocate_type_zero(sizeof(declaration[0]));
782 declaration->namespace = NAMESPACE_ENUM;
783 declaration->source_position = token.source_position;
784 declaration->symbol = symbol;
787 if(token.type == '{') {
788 if(declaration->init.is_defined) {
789 parser_print_error_prefix();
790 fprintf(stderr, "multiple definitions of enum %s\n",
793 record_declaration(declaration);
794 declaration->init.is_defined = 1;
796 parse_enum_entries();
804 * if a symbol is a typedef to another type, return true
806 static bool is_typedef_symbol(symbol_t *symbol)
808 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
809 if(declaration == NULL
810 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
816 static type_t *parse_typeof(void)
824 expression_t *expression = NULL;
828 case T___extension__:
829 /* this can be a prefix to a typename or an expression */
830 /* we simply eat it now. */
833 } while(token.type == T___extension__);
837 if(is_typedef_symbol(token.v.symbol)) {
838 type = parse_typename();
840 expression = parse_expression();
841 type = expression->datatype;
846 type = parse_typename();
850 expression = parse_expression();
851 type = expression->datatype;
857 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
858 typeof->type.type = TYPE_TYPEOF;
859 typeof->expression = expression;
860 typeof->typeof_type = type;
862 return (type_t*) typeof;
866 SPECIFIER_SIGNED = 1 << 0,
867 SPECIFIER_UNSIGNED = 1 << 1,
868 SPECIFIER_LONG = 1 << 2,
869 SPECIFIER_INT = 1 << 3,
870 SPECIFIER_DOUBLE = 1 << 4,
871 SPECIFIER_CHAR = 1 << 5,
872 SPECIFIER_SHORT = 1 << 6,
873 SPECIFIER_LONG_LONG = 1 << 7,
874 SPECIFIER_FLOAT = 1 << 8,
875 SPECIFIER_BOOL = 1 << 9,
876 SPECIFIER_VOID = 1 << 10,
877 #ifdef PROVIDE_COMPLEX
878 SPECIFIER_COMPLEX = 1 << 11,
880 #ifdef PROVIDE_IMAGINARY
881 SPECIFIER_IMAGINARY = 1 << 12,
885 static type_t *create_builtin_type(symbol_t *symbol)
887 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
888 type->type.type = TYPE_BUILTIN;
889 type->symbol = symbol;
891 return (type_t*) type;
894 static type_t *get_typedef_type(symbol_t *symbol)
896 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
897 if(declaration == NULL
898 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
901 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
902 typedef_type->type.type = TYPE_TYPEDEF;
903 typedef_type->declaration = declaration;
905 return (type_t*) typedef_type;
908 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
911 unsigned type_qualifiers = 0;
912 unsigned type_specifiers = 0;
919 #define MATCH_STORAGE_CLASS(token, class) \
921 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
922 parse_error("multiple storage classes in declaration " \
925 specifiers->storage_class = class; \
929 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
930 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
931 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
932 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
933 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
935 /* type qualifiers */
936 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
938 type_qualifiers |= qualifier; \
942 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
943 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
944 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
945 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
947 case T___extension__:
952 /* type specifiers */
953 #define MATCH_SPECIFIER(token, specifier, name) \
956 if(type_specifiers & specifier) { \
957 parse_error("multiple " name " type specifiers given"); \
959 type_specifiers |= specifier; \
963 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
964 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
965 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
966 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
967 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
968 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
969 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
970 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
971 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
972 #ifdef PROVIDE_COMPLEX
973 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
975 #ifdef PROVIDE_IMAGINARY
976 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
980 if(type_specifiers & SPECIFIER_LONG_LONG) {
981 parse_error("multiple type specifiers given");
982 } else if(type_specifiers & SPECIFIER_LONG) {
983 type_specifiers |= SPECIFIER_LONG_LONG;
985 type_specifiers |= SPECIFIER_LONG;
989 /* TODO: if type != NULL for the following rules should issue
992 compound_type_t *compound_type
993 = allocate_type_zero(sizeof(compound_type[0]));
994 compound_type->type.type = TYPE_COMPOUND_STRUCT;
995 compound_type->declaration = parse_compound_type_specifier(true);
997 type = (type_t*) compound_type;
1001 compound_type_t *compound_type
1002 = allocate_type_zero(sizeof(compound_type[0]));
1003 compound_type->type.type = TYPE_COMPOUND_UNION;
1004 compound_type->declaration = parse_compound_type_specifier(false);
1006 type = (type_t*) compound_type;
1010 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1011 enum_type->type.type = TYPE_ENUM;
1012 enum_type->declaration = parse_enum_specifier();
1014 type = (type_t*) enum_type;
1018 type = parse_typeof();
1020 case T___builtin_va_list:
1021 type = create_builtin_type(token.v.symbol);
1025 case T___attribute__:
1030 case T_IDENTIFIER: {
1031 type_t *typedef_type = get_typedef_type(token.v.symbol);
1033 if(typedef_type == NULL)
1034 goto finish_specifiers;
1037 type = typedef_type;
1041 /* function specifier */
1043 goto finish_specifiers;
1050 atomic_type_type_t atomic_type;
1052 /* match valid basic types */
1053 switch(type_specifiers) {
1054 case SPECIFIER_VOID:
1055 atomic_type = ATOMIC_TYPE_VOID;
1057 case SPECIFIER_CHAR:
1058 atomic_type = ATOMIC_TYPE_CHAR;
1060 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1061 atomic_type = ATOMIC_TYPE_SCHAR;
1063 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1064 atomic_type = ATOMIC_TYPE_UCHAR;
1066 case SPECIFIER_SHORT:
1067 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1068 case SPECIFIER_SHORT | SPECIFIER_INT:
1069 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1070 atomic_type = ATOMIC_TYPE_SHORT;
1072 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1073 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1074 atomic_type = ATOMIC_TYPE_USHORT;
1077 case SPECIFIER_SIGNED:
1078 case SPECIFIER_SIGNED | SPECIFIER_INT:
1079 atomic_type = ATOMIC_TYPE_INT;
1081 case SPECIFIER_UNSIGNED:
1082 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1083 atomic_type = ATOMIC_TYPE_UINT;
1085 case SPECIFIER_LONG:
1086 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1087 case SPECIFIER_LONG | SPECIFIER_INT:
1088 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1089 atomic_type = ATOMIC_TYPE_LONG;
1091 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1092 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1093 atomic_type = ATOMIC_TYPE_ULONG;
1095 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1096 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1097 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1098 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1100 atomic_type = ATOMIC_TYPE_LONGLONG;
1102 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1103 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1105 atomic_type = ATOMIC_TYPE_ULONGLONG;
1107 case SPECIFIER_FLOAT:
1108 atomic_type = ATOMIC_TYPE_FLOAT;
1110 case SPECIFIER_DOUBLE:
1111 atomic_type = ATOMIC_TYPE_DOUBLE;
1113 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1114 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1116 case SPECIFIER_BOOL:
1117 atomic_type = ATOMIC_TYPE_BOOL;
1119 #ifdef PROVIDE_COMPLEX
1120 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1121 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1123 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1124 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1126 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1127 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1130 #ifdef PROVIDE_IMAGINARY
1131 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1132 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1134 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1135 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1137 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1138 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1142 /* invalid specifier combination, give an error message */
1143 if(type_specifiers == 0) {
1145 parse_warning("no type specifiers in declaration (using int)");
1146 atomic_type = ATOMIC_TYPE_INT;
1149 parse_error("no type specifiers given in declaration");
1151 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1152 (type_specifiers & SPECIFIER_UNSIGNED)) {
1153 parse_error("signed and unsigned specifiers gives");
1154 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1155 parse_error("only integer types can be signed or unsigned");
1157 parse_error("multiple datatypes in declaration");
1159 atomic_type = ATOMIC_TYPE_INVALID;
1162 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1163 atype->type.type = TYPE_ATOMIC;
1164 atype->atype = atomic_type;
1167 type = (type_t*) atype;
1169 if(type_specifiers != 0) {
1170 parse_error("multiple datatypes in declaration");
1174 type->qualifiers = type_qualifiers;
1176 type_t *result = typehash_insert(type);
1177 if(newtype && result != (type_t*) type) {
1181 specifiers->type = result;
1184 static type_qualifier_t parse_type_qualifiers(void)
1186 type_qualifier_t type_qualifiers = 0;
1189 switch(token.type) {
1190 /* type qualifiers */
1191 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1192 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1193 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1194 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1197 return type_qualifiers;
1202 static void parse_identifier_list(void)
1205 if(token.type != T_IDENTIFIER) {
1206 parse_error_expected("problem while parsing parameter identifier "
1207 "list", T_IDENTIFIER, 0);
1211 if(token.type != ',')
1217 static declaration_t *parse_parameter(void)
1219 declaration_specifiers_t specifiers;
1220 memset(&specifiers, 0, sizeof(specifiers));
1222 parse_declaration_specifiers(&specifiers);
1224 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1225 specifiers.type, 1);
1227 /* TODO check declaration constraints for parameters */
1228 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1229 parse_error("typedef not allowed in parameter list");
1235 static declaration_t *parse_parameters(function_type_t *type)
1237 if(token.type == T_IDENTIFIER) {
1238 symbol_t *symbol = token.v.symbol;
1239 if(!is_typedef_symbol(symbol)) {
1241 parse_identifier_list();
1246 if(token.type == ')') {
1247 type->unspecified_parameters = 1;
1250 if(token.type == T_void && look_ahead(1)->type == ')') {
1255 declaration_t *declarations = NULL;
1256 declaration_t *declaration;
1257 declaration_t *last_declaration = NULL;
1258 function_parameter_t *parameter;
1259 function_parameter_t *last_parameter = NULL;
1262 switch(token.type) {
1266 return declarations;
1269 case T___extension__:
1271 declaration = parse_parameter();
1273 parameter = allocate_type_zero(sizeof(parameter[0]));
1274 parameter->type = declaration->type;
1276 if(last_parameter != NULL) {
1277 last_declaration->next = declaration;
1278 last_parameter->next = parameter;
1280 type->parameters = parameter;
1281 declarations = declaration;
1283 last_parameter = parameter;
1284 last_declaration = declaration;
1288 return declarations;
1290 if(token.type != ',')
1291 return declarations;
1300 } construct_type_type_t;
1302 typedef struct construct_type_t construct_type_t;
1303 struct construct_type_t {
1304 construct_type_type_t type;
1305 construct_type_t *next;
1308 typedef struct parsed_pointer_t parsed_pointer_t;
1309 struct parsed_pointer_t {
1310 construct_type_t construct_type;
1311 type_qualifier_t type_qualifiers;
1314 typedef struct construct_function_type_t construct_function_type_t;
1315 struct construct_function_type_t {
1316 construct_type_t construct_type;
1317 function_type_t *function_type;
1320 typedef struct parsed_array_t parsed_array_t;
1321 struct parsed_array_t {
1322 construct_type_t construct_type;
1323 type_qualifier_t type_qualifiers;
1329 typedef struct construct_base_type_t construct_base_type_t;
1330 struct construct_base_type_t {
1331 construct_type_t construct_type;
1335 static construct_type_t *parse_pointer_declarator(void)
1339 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1340 memset(pointer, 0, sizeof(pointer[0]));
1341 pointer->type_qualifiers = parse_type_qualifiers();
1343 return (construct_type_t*) pointer;
1346 static construct_type_t *parse_array_declarator(void)
1350 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1351 memset(array, 0, sizeof(array[0]));
1353 if(token.type == T_static) {
1354 array->is_static = true;
1358 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1359 if(type_qualifiers != 0) {
1360 if(token.type == T_static) {
1361 array->is_static = true;
1365 array->type_qualifiers = type_qualifiers;
1367 if(token.type == '*' && look_ahead(1)->type == ']') {
1368 array->is_variable = true;
1370 } else if(token.type != ']') {
1371 array->size = parse_assignment_expression();
1376 return (construct_type_t*) array;
1379 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1383 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1384 type->type.type = TYPE_FUNCTION;
1386 declaration_t *parameters = parse_parameters(type);
1387 if(declaration != NULL) {
1388 declaration->context.declarations = parameters;
1391 construct_function_type_t *construct_function_type =
1392 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1393 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1394 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1395 construct_function_type->function_type = type;
1399 return (construct_type_t*) construct_function_type;
1402 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1403 int may_be_abstract)
1405 construct_type_t *result = NULL;
1406 construct_type_t *last = NULL;
1408 while(token.type == '*') {
1409 construct_type_t *type = parse_pointer_declarator();
1418 /* TODO: find out if this is correct */
1421 construct_type_t *inner_types = NULL;
1423 switch(token.type) {
1425 if(declaration == NULL) {
1426 parse_error("no identifier expected in typename");
1428 declaration->symbol = token.v.symbol;
1429 declaration->source_position = token.source_position;
1435 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1441 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1446 construct_type_t *type;
1447 switch(token.type) {
1449 type = parse_function_declarator(declaration);
1452 type = parse_array_declarator();
1455 goto declarator_finished;
1466 declarator_finished:
1469 if(inner_types != NULL) {
1471 last->next = inner_types;
1473 result = inner_types;
1481 static type_t *construct_declarator_type(construct_type_t *construct_list,
1484 construct_type_t *iter = construct_list;
1485 for( ; iter != NULL; iter = iter->next) {
1486 parsed_pointer_t *parsed_pointer;
1487 parsed_array_t *parsed_array;
1488 construct_function_type_t *construct_function_type;
1489 function_type_t *function_type;
1490 pointer_type_t *pointer_type;
1491 array_type_t *array_type;
1493 switch(iter->type) {
1494 case CONSTRUCT_FUNCTION:
1495 construct_function_type = (construct_function_type_t*) iter;
1496 function_type = construct_function_type->function_type;
1498 function_type->result_type = type;
1499 type = (type_t*) function_type;
1502 case CONSTRUCT_POINTER:
1503 parsed_pointer = (parsed_pointer_t*) iter;
1504 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1506 pointer_type->type.type = TYPE_POINTER;
1507 pointer_type->points_to = type;
1508 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1509 type = (type_t*) pointer_type;
1512 case CONSTRUCT_ARRAY:
1513 parsed_array = (parsed_array_t*) iter;
1514 array_type = allocate_type_zero(sizeof(array_type[0]));
1516 array_type->type.type = TYPE_ARRAY;
1517 array_type->element_type = type;
1518 array_type->type.qualifiers = parsed_array->type_qualifiers;
1519 array_type->is_static = parsed_array->is_static;
1520 array_type->is_variable = parsed_array->is_variable;
1521 array_type->size = parsed_array->size;
1522 type = (type_t*) array_type;
1526 type_t *hashed_type = typehash_insert((type_t*) type);
1527 if(hashed_type != type) {
1536 static declaration_t *parse_declarator(storage_class_t storage_class,
1537 type_t *type, int may_be_abstract)
1539 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1540 declaration->storage_class = storage_class;
1542 construct_type_t *construct_type
1543 = parse_inner_declarator(declaration, may_be_abstract);
1544 declaration->type = construct_declarator_type(construct_type, type);
1546 if(construct_type != NULL) {
1547 obstack_free(&temp_obst, construct_type);
1553 static type_t *parse_abstract_declarator(type_t *base_type)
1555 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1557 type_t *result = construct_declarator_type(construct_type, base_type);
1558 if(construct_type != NULL) {
1559 obstack_free(&temp_obst, construct_type);
1565 static declaration_t *record_declaration(declaration_t *declaration)
1567 assert(context != NULL);
1569 symbol_t *symbol = declaration->symbol;
1570 if(symbol != NULL) {
1571 declaration_t *alias = environment_push(declaration);
1572 if(alias != declaration)
1575 declaration->parent_context = context;
1578 if(last_declaration != NULL) {
1579 last_declaration->next = declaration;
1581 context->declarations = declaration;
1583 last_declaration = declaration;
1588 static void parser_error_multiple_definition(declaration_t *previous,
1589 declaration_t *declaration)
1591 parser_print_error_prefix_pos(declaration->source_position);
1592 fprintf(stderr, "multiple definition of symbol '%s'\n",
1593 declaration->symbol->string);
1594 parser_print_error_prefix_pos(previous->source_position);
1595 fprintf(stderr, "this is the location of the previous "
1600 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1603 declaration_t *ndeclaration
1604 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1606 declaration_t *declaration = record_declaration(ndeclaration);
1607 if(token.type == '=') {
1610 /* TODO: check that this is an allowed type (no function type) */
1612 if(declaration->init.initializer != NULL) {
1613 parser_error_multiple_definition(declaration, ndeclaration);
1616 ndeclaration->init.initializer = parse_initializer();
1617 } else if(token.type == '{') {
1618 if(declaration->type->type != TYPE_FUNCTION) {
1619 parser_print_error_prefix();
1620 fprintf(stderr, "Declarator ");
1621 print_type_ext(declaration->type, declaration->symbol, NULL);
1622 fprintf(stderr, " has a body but is not a function type.\n");
1627 if(declaration->init.statement != NULL) {
1628 parser_error_multiple_definition(declaration, ndeclaration);
1630 if(ndeclaration != declaration) {
1631 memcpy(&declaration->context, &ndeclaration->context,
1632 sizeof(declaration->context));
1635 int top = environment_top();
1636 context_t *last_context = context;
1637 set_context(&declaration->context);
1639 /* push function parameters */
1640 declaration_t *parameter = declaration->context.declarations;
1641 for( ; parameter != NULL; parameter = parameter->next) {
1642 environment_push(parameter);
1645 statement_t *statement = parse_compound_statement();
1647 assert(context == &declaration->context);
1648 set_context(last_context);
1649 environment_pop_to(top);
1651 declaration->init.statement = statement;
1655 if(token.type != ',')
1662 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1665 if(token.type == ':') {
1667 parse_constant_expression();
1668 /* TODO (bitfields) */
1670 declaration_t *declaration
1671 = parse_declarator(specifiers->storage_class,
1672 specifiers->type, 1);
1674 /* TODO: check constraints for struct declarations */
1675 /* TODO: check for doubled fields */
1676 record_declaration(declaration);
1678 if(token.type == ':') {
1680 parse_constant_expression();
1681 /* TODO (bitfields) */
1685 if(token.type != ',')
1692 static void parse_compound_type_entries(void)
1696 while(token.type != '}' && token.type != T_EOF) {
1697 declaration_specifiers_t specifiers;
1698 memset(&specifiers, 0, sizeof(specifiers));
1699 parse_declaration_specifiers(&specifiers);
1701 parse_struct_declarators(&specifiers);
1703 if(token.type == T_EOF) {
1704 parse_error("unexpected error while parsing struct");
1709 static void parse_declaration(void)
1711 source_position_t source_position = token.source_position;
1713 declaration_specifiers_t specifiers;
1714 memset(&specifiers, 0, sizeof(specifiers));
1715 parse_declaration_specifiers(&specifiers);
1717 if(token.type == ';') {
1720 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1722 declaration->type = specifiers.type;
1723 declaration->storage_class = specifiers.storage_class;
1724 declaration->source_position = source_position;
1725 record_declaration(declaration);
1728 parse_init_declarators(&specifiers);
1731 static type_t *parse_typename(void)
1733 declaration_specifiers_t specifiers;
1734 memset(&specifiers, 0, sizeof(specifiers));
1735 parse_declaration_specifiers(&specifiers);
1736 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1737 /* TODO: improve error message, user does probably not know what a
1738 * storage class is...
1740 parse_error("typename may not have a storage class");
1743 type_t *result = parse_abstract_declarator(specifiers.type);
1751 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1752 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1753 expression_t *left);
1755 typedef struct expression_parser_function_t expression_parser_function_t;
1756 struct expression_parser_function_t {
1757 unsigned precedence;
1758 parse_expression_function parser;
1759 unsigned infix_precedence;
1760 parse_expression_infix_function infix_parser;
1763 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1765 static expression_t *expected_expression_error(void)
1767 parser_print_error_prefix();
1768 fprintf(stderr, "expected expression, got token ");
1769 print_token(stderr, & token);
1770 fprintf(stderr, "\n");
1772 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1773 expression->type = EXPR_INVALID;
1779 static expression_t *parse_string_const(void)
1781 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1783 cnst->expression.type = EXPR_STRING_LITERAL;
1784 cnst->expression.datatype = type_string;
1785 cnst->value = parse_string_literals();
1787 return (expression_t*) cnst;
1790 static expression_t *parse_int_const(void)
1792 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1794 cnst->expression.type = EXPR_CONST;
1795 cnst->expression.datatype = type_int;
1796 cnst->v.int_value = token.v.intvalue;
1800 return (expression_t*) cnst;
1803 static expression_t *parse_float_const(void)
1805 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1807 cnst->expression.type = EXPR_CONST;
1808 cnst->expression.datatype = type_double;
1809 cnst->v.float_value = token.v.floatvalue;
1813 return (expression_t*) cnst;
1816 static declaration_t *create_implicit_function(symbol_t *symbol,
1817 const source_position_t source_position)
1819 function_type_t *function_type = allocate_type_zero(sizeof(function_type));
1821 function_type->type.type = TYPE_FUNCTION;
1822 function_type->result_type = type_int;
1823 function_type->unspecified_parameters = true;
1825 type_t *type = typehash_insert((type_t*) function_type);
1826 if(type != (type_t*) function_type) {
1827 free_type(function_type);
1830 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1832 declaration->storage_class = STORAGE_CLASS_EXTERN;
1833 declaration->type = type;
1834 declaration->symbol = symbol;
1835 declaration->source_position = source_position;
1837 /* prepend the implicit definition to the global context
1838 * this is safe since the symbol wasn't declared as anything else yet
1840 assert(symbol->declaration == NULL);
1842 context_t *last_context = context;
1843 context = global_context;
1845 environment_push(declaration);
1846 declaration->next = context->declarations;
1847 context->declarations = declaration;
1849 context = last_context;
1854 static expression_t *parse_reference(void)
1856 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1858 ref->expression.type = EXPR_REFERENCE;
1859 ref->symbol = token.v.symbol;
1861 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
1863 source_position_t source_position = token.source_position;
1866 if(declaration == NULL) {
1868 /* an implicitely defined function */
1869 if(token.type == '(') {
1870 parser_print_prefix_pos(token.source_position);
1871 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1872 ref->symbol->string);
1874 declaration = create_implicit_function(ref->symbol,
1879 parser_print_error_prefix();
1880 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1881 return (expression_t*) ref;
1885 ref->declaration = declaration;
1886 ref->expression.datatype = declaration->type;
1888 return (expression_t*) ref;
1891 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1895 /* TODO check if cast is allowed and issue warnings/errors */
1898 static expression_t *parse_cast(void)
1900 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1902 cast->expression.type = EXPR_UNARY;
1903 cast->type = UNEXPR_CAST;
1904 cast->expression.source_position = token.source_position;
1906 type_t *type = parse_typename();
1909 expression_t *value = parse_sub_expression(20);
1911 check_cast_allowed(value, type);
1913 cast->expression.datatype = type;
1914 cast->value = value;
1916 return (expression_t*) cast;
1919 static expression_t *parse_statement_expression(void)
1921 statement_expression_t *expression
1922 = allocate_ast_zero(sizeof(expression[0]));
1923 expression->expression.type = EXPR_STATEMENT;
1924 expression->statement = parse_compound_statement();
1926 /* find last statement and use it's type */
1927 const statement_t *last_statement = NULL;
1928 const statement_t *statement = expression->statement;
1929 for( ; statement != NULL; statement = statement->next) {
1930 last_statement = statement;
1933 if(last_statement->type == STATEMENT_EXPRESSION) {
1934 const expression_statement_t *expression_statement =
1935 (const expression_statement_t*) last_statement;
1936 expression->expression.datatype
1937 = expression_statement->expression->datatype;
1939 expression->expression.datatype = type_void;
1944 return (expression_t*) expression;
1947 static expression_t *parse_brace_expression(void)
1951 switch(token.type) {
1953 /* gcc extension: a stement expression */
1954 return parse_statement_expression();
1958 return parse_cast();
1960 if(is_typedef_symbol(token.v.symbol)) {
1961 return parse_cast();
1965 expression_t *result = parse_expression();
1971 static expression_t *parse_function_keyword(void)
1973 eat(T___FUNCTION__);
1976 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1977 expression->expression.type = EXPR_FUNCTION;
1978 expression->expression.datatype = type_string;
1979 expression->value = "TODO: FUNCTION";
1981 return (expression_t*) expression;
1984 static expression_t *parse_pretty_function_keyword(void)
1986 eat(T___PRETTY_FUNCTION__);
1989 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1990 expression->expression.type = EXPR_PRETTY_FUNCTION;
1991 expression->expression.datatype = type_string;
1992 expression->value = "TODO: PRETTY FUNCTION";
1994 return (expression_t*) expression;
1997 static designator_t *parse_designator(void)
1999 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2001 if(token.type != T_IDENTIFIER) {
2002 parse_error_expected("problem while parsing member designator",
2007 result->symbol = token.v.symbol;
2010 designator_t *last_designator = result;
2012 if(token.type == '.') {
2014 if(token.type != T_IDENTIFIER) {
2015 parse_error_expected("problem while parsing member designator",
2020 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2021 designator->symbol = token.v.symbol;
2024 last_designator->next = designator;
2025 last_designator = designator;
2028 if(token.type == '[') {
2030 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2031 designator->array_access = parse_expression();
2032 if(designator->array_access == NULL) {
2038 last_designator->next = designator;
2039 last_designator = designator;
2048 static expression_t *parse_offsetof(void)
2050 eat(T___builtin_offsetof);
2052 offsetof_expression_t *expression
2053 = allocate_ast_zero(sizeof(expression[0]));
2054 expression->expression.type = EXPR_OFFSETOF;
2055 expression->expression.datatype = type_size_t;
2058 expression->type = parse_typename();
2060 expression->designator = parse_designator();
2063 return (expression_t*) expression;
2066 static expression_t *parse_va_arg(void)
2068 eat(T___builtin_va_arg);
2070 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2071 expression->expression.type = EXPR_VA_ARG;
2074 expression->arg = parse_assignment_expression();
2076 expression->expression.datatype = parse_typename();
2079 return (expression_t*) expression;
2082 static expression_t *parse_builtin_symbol(void)
2084 builtin_symbol_expression_t *expression
2085 = allocate_ast_zero(sizeof(expression[0]));
2086 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2088 /* TODO: set datatype */
2090 expression->symbol = token.v.symbol;
2094 return (expression_t*) expression;
2097 static expression_t *parse_primary_expression(void)
2099 switch(token.type) {
2101 return parse_int_const();
2102 case T_FLOATINGPOINT:
2103 return parse_float_const();
2104 case T_STRING_LITERAL:
2105 return parse_string_const();
2107 return parse_reference();
2108 case T___FUNCTION__:
2109 return parse_function_keyword();
2110 case T___PRETTY_FUNCTION__:
2111 return parse_pretty_function_keyword();
2112 case T___builtin_offsetof:
2113 return parse_offsetof();
2114 case T___builtin_va_arg:
2115 return parse_va_arg();
2116 case T___builtin_expect:
2117 case T___builtin_va_start:
2118 case T___builtin_va_end:
2119 return parse_builtin_symbol();
2122 return parse_brace_expression();
2125 parser_print_error_prefix();
2126 fprintf(stderr, "unexpected token ");
2127 print_token(stderr, &token);
2128 fprintf(stderr, "\n");
2131 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2132 expression->type = EXPR_INVALID;
2133 expression->datatype = type_void;
2138 static expression_t *parse_array_expression(unsigned precedence,
2139 expression_t *array_ref)
2145 array_access_expression_t *array_access
2146 = allocate_ast_zero(sizeof(array_access[0]));
2148 array_access->expression.type = EXPR_ARRAY_ACCESS;
2149 array_access->array_ref = array_ref;
2150 array_access->index = parse_expression();
2152 type_t *array_type = array_ref->datatype;
2153 if(array_type != NULL) {
2154 if(array_type->type == TYPE_POINTER) {
2155 pointer_type_t *pointer = (pointer_type_t*) array_type;
2156 array_access->expression.datatype = pointer->points_to;
2158 parser_print_error_prefix();
2159 fprintf(stderr, "array access on object with non-pointer type ");
2160 print_type(array_type);
2161 fprintf(stderr, "\n");
2165 if(token.type != ']') {
2166 parse_error_expected("Problem while parsing array access", ']', 0);
2167 return (expression_t*) array_access;
2171 return (expression_t*) array_access;
2174 static bool is_declaration_specifier(const token_t *token,
2175 bool only_type_specifiers)
2177 switch(token->type) {
2181 return is_typedef_symbol(token->v.symbol);
2184 if(only_type_specifiers)
2193 static expression_t *parse_sizeof(unsigned precedence)
2197 sizeof_expression_t *sizeof_expression
2198 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2199 sizeof_expression->expression.type = EXPR_SIZEOF;
2200 sizeof_expression->expression.datatype = type_size_t;
2202 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2204 sizeof_expression->type = parse_typename();
2207 expression_t *expression = parse_sub_expression(precedence);
2208 sizeof_expression->type = expression->datatype;
2209 sizeof_expression->size_expression = expression;
2212 return (expression_t*) sizeof_expression;
2215 static expression_t *parse_select_expression(unsigned precedence,
2216 expression_t *compound)
2220 assert(token.type == '.' || token.type == T_MINUSGREATER);
2223 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2225 select->expression.type = EXPR_SELECT;
2226 select->compound = compound;
2228 /* TODO: datatype */
2230 if(token.type != T_IDENTIFIER) {
2231 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2232 return (expression_t*) select;
2234 select->symbol = token.v.symbol;
2237 return (expression_t*) select;
2240 static expression_t *parse_call_expression(unsigned precedence,
2241 expression_t *expression)
2244 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2246 call->expression.type = EXPR_CALL;
2247 call->function = expression;
2249 /* parse arguments */
2252 if(token.type != ')') {
2253 call_argument_t *last_argument = NULL;
2256 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2258 argument->expression = parse_assignment_expression();
2259 if(last_argument == NULL) {
2260 call->arguments = argument;
2262 last_argument->next = argument;
2264 last_argument = argument;
2266 if(token.type != ',')
2273 type_t *type = expression->datatype;
2275 /* we can call pointer to function */
2276 if(type->type == TYPE_POINTER) {
2277 pointer_type_t *pointer = (pointer_type_t*) type;
2278 type = pointer->points_to;
2281 if(type == NULL || type->type != TYPE_FUNCTION) {
2282 parser_print_error_prefix();
2283 fprintf(stderr, "expected a function type for call but found "
2285 print_type(expression->datatype);
2286 fprintf(stderr, "\n");
2288 function_type_t *function_type = (function_type_t*) type;
2289 call->expression.datatype = function_type->result_type;
2293 return (expression_t*) call;
2296 static void type_error(const char *msg, const source_position_t source_position,
2299 parser_print_error_prefix_pos(source_position);
2300 fprintf(stderr, "%s, but found type ", msg);
2302 fputc('\n', stderr);
2306 static void type_error_incompatible(const char *msg,
2307 const source_position_t source_position, type_t *type1, type_t *type2)
2309 parser_print_error_prefix_pos(source_position);
2310 fprintf(stderr, "%s, incompatible types: ", msg);
2312 fprintf(stderr, " - ");
2314 fprintf(stderr, ")\n");
2318 static type_t *get_type_after_conversion(const type_t *type1,
2319 const type_t *type2)
2323 return (type_t*) type1;
2326 static expression_t *parse_conditional_expression(unsigned precedence,
2327 expression_t *expression)
2331 conditional_expression_t *conditional
2332 = allocate_ast_zero(sizeof(conditional[0]));
2333 conditional->expression.type = EXPR_CONDITIONAL;
2334 conditional->condition = expression;
2337 type_t *condition_type = conditional->condition->datatype;
2338 if(condition_type != NULL) {
2339 if(!is_type_scalar(condition_type)) {
2340 type_error("expected a scalar type", expression->source_position,
2345 conditional->true_expression = parse_expression();
2347 conditional->false_expression = parse_sub_expression(precedence);
2349 type_t *true_type = conditional->true_expression->datatype;
2350 if(true_type == NULL)
2351 return (expression_t*) conditional;
2352 type_t *false_type = conditional->false_expression->datatype;
2353 if(false_type == NULL)
2354 return (expression_t*) conditional;
2357 if(true_type == false_type) {
2358 conditional->expression.datatype = true_type;
2359 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2360 type_t *result = get_type_after_conversion(true_type, false_type);
2361 /* TODO: create implicit convs if necessary */
2362 conditional->expression.datatype = result;
2363 } else if(true_type->type == TYPE_POINTER &&
2364 false_type->type == TYPE_POINTER &&
2365 true /* TODO compatible points_to types */) {
2367 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2368 || (is_null_ptr_const(false_type) &&
2369 true_type->type == TYPE_POINTER) TODO*/ false) {
2371 } else if(/* 1 is pointer to object type, other is void* */ false) {
2374 type_error_incompatible("problem while parsing conditional",
2375 expression->source_position, true_type,
2379 return (expression_t*) conditional;
2382 static expression_t *parse_extension(unsigned precedence)
2384 eat(T___extension__);
2386 /* TODO enable extensions */
2388 return parse_sub_expression(precedence);
2391 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2394 return expression->datatype;
2397 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2404 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2406 type_t *type = expression->datatype;
2407 return make_pointer_type(type, 0);
2410 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2411 static expression_t *parse_##unexpression_type(unsigned precedence) \
2415 unary_expression_t *unary_expression \
2416 = allocate_ast_zero(sizeof(unary_expression[0])); \
2417 unary_expression->expression.type = EXPR_UNARY; \
2418 unary_expression->type = unexpression_type; \
2419 unary_expression->value = parse_sub_expression(precedence); \
2420 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2422 return (expression_t*) unary_expression; \
2425 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2426 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2427 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2428 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2429 get_unexpr_dereference_type)
2430 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2431 get_unexpr_take_addr_type)
2432 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2433 get_unexpr_arithmetic_type)
2434 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2435 get_unexpr_arithmetic_type)
2436 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2437 get_unexpr_arithmetic_type)
2439 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2441 static expression_t *parse_##unexpression_type(unsigned precedence, \
2442 expression_t *left) \
2444 (void) precedence; \
2447 unary_expression_t *unary_expression \
2448 = allocate_ast_zero(sizeof(unary_expression[0])); \
2449 unary_expression->expression.type = EXPR_UNARY; \
2450 unary_expression->type = unexpression_type; \
2451 unary_expression->value = left; \
2452 unary_expression->expression.datatype = tfunc(left); \
2454 return (expression_t*) unary_expression; \
2457 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2458 get_unexpr_arithmetic_type)
2459 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2460 get_unexpr_arithmetic_type)
2462 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
2464 expression_t *parse_##binexpression_type(unsigned precedence, \
2465 expression_t *left) \
2469 expression_t *right = parse_sub_expression(precedence); \
2471 binary_expression_t *binexpr \
2472 = allocate_ast_zero(sizeof(binexpr[0])); \
2473 binexpr->expression.type = EXPR_BINARY; \
2474 binexpr->type = binexpression_type; \
2475 binexpr->left = left; \
2476 binexpr->right = right; \
2478 return (expression_t*) binexpr; \
2481 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2482 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2483 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2484 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2485 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2486 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2487 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2488 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2489 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2490 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2491 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2492 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2493 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2494 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2495 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2496 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2497 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2498 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2499 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2500 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2501 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2502 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2503 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2504 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2505 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2506 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2507 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2508 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2509 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2510 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2512 static expression_t *parse_sub_expression(unsigned precedence)
2514 if(token.type < 0) {
2515 return expected_expression_error();
2518 expression_parser_function_t *parser
2519 = &expression_parsers[token.type];
2520 source_position_t source_position = token.source_position;
2523 if(parser->parser != NULL) {
2524 left = parser->parser(parser->precedence);
2526 left = parse_primary_expression();
2528 assert(left != NULL);
2529 left->source_position = source_position;
2532 if(token.type < 0) {
2533 return expected_expression_error();
2536 parser = &expression_parsers[token.type];
2537 if(parser->infix_parser == NULL)
2539 if(parser->infix_precedence < precedence)
2542 left = parser->infix_parser(parser->infix_precedence, left);
2544 assert(left != NULL);
2545 assert(left->type != EXPR_INVALID);
2546 left->source_position = source_position;
2552 static expression_t *parse_expression(void)
2554 return parse_sub_expression(1);
2559 void register_expression_parser(parse_expression_function parser,
2560 int token_type, unsigned precedence)
2562 expression_parser_function_t *entry = &expression_parsers[token_type];
2564 if(entry->parser != NULL) {
2565 fprintf(stderr, "for token ");
2566 print_token_type(stderr, token_type);
2567 fprintf(stderr, "\n");
2568 panic("trying to register multiple expression parsers for a token");
2570 entry->parser = parser;
2571 entry->precedence = precedence;
2574 void register_expression_infix_parser(parse_expression_infix_function parser,
2575 int token_type, unsigned precedence)
2577 expression_parser_function_t *entry = &expression_parsers[token_type];
2579 if(entry->infix_parser != NULL) {
2580 fprintf(stderr, "for token ");
2581 print_token_type(stderr, token_type);
2582 fprintf(stderr, "\n");
2583 panic("trying to register multiple infix expression parsers for a "
2586 entry->infix_parser = parser;
2587 entry->infix_precedence = precedence;
2590 static void init_expression_parsers(void)
2592 memset(&expression_parsers, 0, sizeof(expression_parsers));
2594 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2595 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2596 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2597 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2598 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2599 T_GREATERGREATER, 16);
2600 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2601 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2602 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2603 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2604 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2605 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2606 T_GREATEREQUAL, 14);
2607 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2608 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2609 T_EXCLAMATIONMARKEQUAL, 13);
2610 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2611 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2612 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2613 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2614 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2615 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2616 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2617 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2618 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2619 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2620 T_ASTERISKEQUAL, 2);
2621 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2622 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2624 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2625 T_LESSLESSEQUAL, 2);
2626 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2627 T_GREATERGREATEREQUAL, 2);
2628 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2630 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2632 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2635 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2637 register_expression_infix_parser(parse_array_expression, '[', 30);
2638 register_expression_infix_parser(parse_call_expression, '(', 30);
2639 register_expression_infix_parser(parse_select_expression, '.', 30);
2640 register_expression_infix_parser(parse_select_expression,
2641 T_MINUSGREATER, 30);
2642 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2644 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2647 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2648 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2649 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2650 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2651 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2652 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2653 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2654 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2655 register_expression_parser(parse_sizeof, T_sizeof, 25);
2656 register_expression_parser(parse_extension, T___extension__, 25);
2660 static statement_t *parse_case_statement(void)
2663 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2664 label->statement.type = STATEMENT_CASE_LABEL;
2665 label->statement.source_position = token.source_position;
2667 label->expression = parse_expression();
2670 label->statement.next = parse_statement();
2672 return (statement_t*) label;
2675 static statement_t *parse_default_statement(void)
2679 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2680 label->statement.type = STATEMENT_CASE_LABEL;
2681 label->statement.source_position = token.source_position;
2684 label->statement.next = parse_statement();
2686 return (statement_t*) label;
2689 static statement_t *parse_label_statement(void)
2698 static statement_t *parse_if(void)
2702 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2703 statement->statement.type = STATEMENT_IF;
2704 statement->statement.source_position = token.source_position;
2707 statement->condition = parse_expression();
2710 statement->true_statement = parse_statement();
2711 if(token.type == T_else) {
2713 statement->false_statement = parse_statement();
2716 return (statement_t*) statement;
2719 static statement_t *parse_switch(void)
2723 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2724 statement->statement.type = STATEMENT_SWITCH;
2725 statement->statement.source_position = token.source_position;
2728 statement->expression = parse_expression();
2730 statement->body = parse_statement();
2732 return (statement_t*) statement;
2735 static statement_t *parse_while(void)
2739 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2740 statement->statement.type = STATEMENT_WHILE;
2741 statement->statement.source_position = token.source_position;
2744 statement->condition = parse_expression();
2746 statement->body = parse_statement();
2748 return (statement_t*) statement;
2751 static statement_t *parse_do(void)
2755 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2756 statement->statement.type = STATEMENT_DO_WHILE;
2757 statement->statement.source_position = token.source_position;
2759 statement->body = parse_statement();
2762 statement->condition = parse_expression();
2766 return (statement_t*) statement;
2769 static statement_t *parse_for(void)
2773 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2774 statement->statement.type = STATEMENT_FOR;
2775 statement->statement.source_position = token.source_position;
2779 int top = environment_top();
2780 context_t *last_context = context;
2781 set_context(&statement->context);
2783 if(token.type != ';') {
2784 if(is_declaration_specifier(&token, false)) {
2785 parse_declaration();
2787 statement->initialisation = parse_expression();
2794 if(token.type != ';') {
2795 statement->condition = parse_expression();
2798 if(token.type != ')') {
2799 statement->step = parse_expression();
2802 statement->body = parse_statement();
2804 assert(context == &statement->context);
2805 set_context(last_context);
2806 environment_pop_to(top);
2808 return (statement_t*) statement;
2811 static statement_t *parse_goto(void)
2814 expect(T_IDENTIFIER);
2820 static statement_t *parse_continue(void)
2825 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2826 statement->source_position = token.source_position;
2827 statement->type = STATEMENT_CONTINUE;
2832 static statement_t *parse_break(void)
2837 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2838 statement->source_position = token.source_position;
2839 statement->type = STATEMENT_BREAK;
2844 static statement_t *parse_return(void)
2848 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2850 statement->statement.type = STATEMENT_RETURN;
2851 statement->statement.source_position = token.source_position;
2852 if(token.type != ';') {
2853 statement->return_value = parse_expression();
2857 return (statement_t*) statement;
2860 static statement_t *parse_declaration_statement(void)
2862 declaration_t *before = last_declaration;
2864 declaration_statement_t *statement
2865 = allocate_ast_zero(sizeof(statement[0]));
2866 statement->statement.type = STATEMENT_DECLARATION;
2867 statement->statement.source_position = token.source_position;
2869 declaration_specifiers_t specifiers;
2870 memset(&specifiers, 0, sizeof(specifiers));
2871 parse_declaration_specifiers(&specifiers);
2873 if(token.type == ';') {
2876 parse_init_declarators(&specifiers);
2879 if(before == NULL) {
2880 statement->declarations_begin = context->declarations;
2882 statement->declarations_begin = before->next;
2884 statement->declarations_end = last_declaration;
2886 return (statement_t*) statement;
2889 static statement_t *parse_expression_statement(void)
2891 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2892 statement->statement.type = STATEMENT_EXPRESSION;
2893 statement->statement.source_position = token.source_position;
2895 statement->expression = parse_expression();
2899 return (statement_t*) statement;
2902 static statement_t *parse_statement(void)
2904 statement_t *statement = NULL;
2906 /* declaration or statement */
2907 switch(token.type) {
2909 statement = parse_case_statement();
2913 statement = parse_default_statement();
2917 statement = parse_compound_statement();
2921 statement = parse_if();
2925 statement = parse_switch();
2929 statement = parse_while();
2933 statement = parse_do();
2937 statement = parse_for();
2941 statement = parse_goto();
2945 statement = parse_continue();
2949 statement = parse_break();
2953 statement = parse_return();
2962 if(look_ahead(1)->type == ':') {
2963 statement = parse_label_statement();
2967 if(is_typedef_symbol(token.v.symbol)) {
2968 statement = parse_declaration_statement();
2972 statement = parse_expression_statement();
2975 case T___extension__:
2976 /* this can be a prefix to a declaration or an expression statement */
2977 /* we simply eat it now and parse the rest with tail recursion */
2980 } while(token.type == T___extension__);
2981 statement = parse_statement();
2985 statement = parse_declaration_statement();
2989 statement = parse_expression_statement();
2993 assert(statement == NULL || statement->source_position.input_name != NULL);
2998 static statement_t *parse_compound_statement(void)
3002 compound_statement_t *compound_statement
3003 = allocate_ast_zero(sizeof(compound_statement[0]));
3004 compound_statement->statement.type = STATEMENT_COMPOUND;
3005 compound_statement->statement.source_position = token.source_position;
3007 int top = environment_top();
3008 context_t *last_context = context;
3009 set_context(&compound_statement->context);
3011 statement_t *last_statement = NULL;
3013 while(token.type != '}' && token.type != T_EOF) {
3014 statement_t *statement = parse_statement();
3015 if(statement == NULL)
3018 if(last_statement != NULL) {
3019 last_statement->next = statement;
3021 compound_statement->statements = statement;
3024 while(statement->next != NULL)
3025 statement = statement->next;
3027 last_statement = statement;
3030 assert(context == &compound_statement->context);
3031 set_context(last_context);
3032 environment_pop_to(top);
3036 return (statement_t*) compound_statement;
3039 static translation_unit_t *parse_translation_unit(void)
3041 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3043 assert(global_context == NULL);
3044 global_context = &unit->context;
3046 assert(context == NULL);
3047 set_context(&unit->context);
3049 while(token.type != T_EOF) {
3050 parse_declaration();
3053 assert(context == &unit->context);
3055 last_declaration = NULL;
3057 assert(global_context == &unit->context);
3058 global_context = NULL;
3063 translation_unit_t *parse(void)
3065 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3067 type_set_output(stderr);
3069 lookahead_bufpos = 0;
3070 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3073 translation_unit_t *unit = parse_translation_unit();
3075 DEL_ARR_F(environment_stack);
3080 void init_parser(void)
3082 init_expression_parsers();
3083 obstack_init(&temp_obst);
3085 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3086 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3087 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3088 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3089 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3090 type_string = make_pointer_type(type_const_char, 0);
3093 void exit_parser(void)
3095 obstack_free(&temp_obst, NULL);