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->context_next != NULL) {
307 last_declaration = last_declaration->context_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->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->next) {
397 declaration_t *next = iter->next;
399 iter->next = declaration;
400 assert(declaration->next == NULL);
403 if(next->namespace == namespace) {
404 iter->next = declaration;
405 declaration->next = next->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->next;
440 symbol->declaration = old_declaration;
441 assert(old_declaration->next == declaration->next);
444 for( ; declaration != NULL; declaration = declaration->next) {
445 declaration_t *next = declaration->next;
446 if(next->namespace == namespace) {
447 declaration->next = old_declaration;
448 assert(old_declaration->next == next->next);
452 assert(declaration != NULL);
456 ARR_SHRINKLEN(environment_stack, (int) new_top);
461 static expression_t *parse_constant_expression(void)
463 /* start parsing at precedence 7 (conditional expression) */
464 return parse_sub_expression(7);
467 static expression_t *parse_assignment_expression(void)
469 /* start parsing at precedence 2 (assignment expression) */
470 return parse_sub_expression(2);
473 static void parse_compound_type_entries(void);
474 static declaration_t *parse_declarator(storage_class_t storage_class,
475 type_t *type, int may_be_abstract);
476 static declaration_t *record_declaration(declaration_t *declaration);
478 typedef struct declaration_specifiers_t declaration_specifiers_t;
479 struct declaration_specifiers_t {
480 storage_class_t storage_class;
484 static const char *parse_string_literals(void)
486 assert(token.type == T_STRING_LITERAL);
487 const char *result = token.v.string;
491 while(token.type == T_STRING_LITERAL) {
492 result = concat_strings(result, token.v.string);
499 static void parse_attributes(void)
503 case T___attribute__:
511 parse_error("EOF while parsing attribute");
529 if(token.type != T_STRING_LITERAL) {
530 parse_error_expected("while parsing assembler attribute",
535 parse_string_literals();
540 goto attributes_finished;
548 static designator_t *parse_designation(void)
550 if(token.type != '[' && token.type != '.')
553 designator_t *result = NULL;
554 designator_t *last = NULL;
557 designator_t *designator;
560 designator = allocate_ast_zero(sizeof(designator[0]));
562 designator->array_access = parse_constant_expression();
566 designator = allocate_ast_zero(sizeof(designator[0]));
568 if(token.type != T_IDENTIFIER) {
569 parse_error_expected("problem while parsing designator",
573 designator->symbol = token.v.symbol;
581 assert(designator != NULL);
583 last->next = designator;
591 static initializer_t *parse_initializer_list(void);
593 static initializer_t *parse_initializer(void)
595 designator_t *designator = parse_designation();
597 initializer_t *result;
598 if(token.type == '{') {
599 result = parse_initializer_list();
601 result = allocate_ast_zero(sizeof(result[0]));
602 result->type = INITIALIZER_VALUE;
603 result->v.value = parse_assignment_expression();
605 result->designator = designator;
610 static initializer_t *parse_initializer_list(void)
614 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
615 result->type = INITIALIZER_LIST;
617 initializer_t *last = NULL;
619 initializer_t *initializer = parse_initializer();
621 last->next = initializer;
623 result->v.list = initializer;
627 if(token.type == '}')
630 if(token.type != ',') {
631 parse_error_expected("problem while parsing initializer list",
638 if(token.type == '}')
647 static declaration_t *parse_compound_type_specifier(bool is_struct)
655 symbol_t *symbol = NULL;
656 declaration_t *declaration = NULL;
658 if(token.type == T_IDENTIFIER) {
659 symbol = token.v.symbol;
663 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
665 declaration = get_declaration(symbol, NAMESPACE_UNION);
667 } else if(token.type != '{') {
669 parse_error_expected("problem while parsing struct type specifier",
670 T_IDENTIFIER, '{', 0);
672 parse_error_expected("problem while parsing union type specifier",
673 T_IDENTIFIER, '{', 0);
679 if(declaration == NULL) {
680 declaration = allocate_type_zero(sizeof(declaration[0]));
683 declaration->namespace = NAMESPACE_STRUCT;
685 declaration->namespace = NAMESPACE_UNION;
687 declaration->source_position = token.source_position;
688 declaration->symbol = symbol;
691 if(token.type == '{') {
692 if(declaration->init.is_defined) {
693 assert(symbol != NULL);
694 parser_print_error_prefix();
695 fprintf(stderr, "multiple definition of %s %s\n",
696 is_struct ? "struct" : "union", symbol->string);
697 declaration->context.declarations = NULL;
699 record_declaration(declaration);
700 declaration->init.is_defined = true;
702 int top = environment_top();
703 context_t *last_context = context;
704 set_context(& declaration->context);
706 parse_compound_type_entries();
709 assert(context == & declaration->context);
710 set_context(last_context);
711 environment_pop_to(top);
717 static void parse_enum_entries(void)
721 if(token.type == '}') {
723 parse_error("empty enum not allowed");
728 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
730 if(token.type != T_IDENTIFIER) {
731 parse_error_expected("problem while parsing enum entry",
736 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
737 entry->symbol = token.v.symbol;
738 entry->source_position = token.source_position;
741 if(token.type == '=') {
743 entry->init.initializer = parse_initializer();
746 record_declaration(entry);
748 if(token.type != ',')
751 } while(token.type != '}');
756 static declaration_t *parse_enum_specifier(void)
760 declaration_t *declaration;
763 if(token.type == T_IDENTIFIER) {
764 symbol = token.v.symbol;
767 declaration = get_declaration(symbol, NAMESPACE_ENUM);
768 } else if(token.type != '{') {
769 parse_error_expected("problem while parsing enum type specifier",
770 T_IDENTIFIER, '{', 0);
777 if(declaration == NULL) {
778 declaration = allocate_type_zero(sizeof(declaration[0]));
780 declaration->namespace = NAMESPACE_ENUM;
781 declaration->source_position = token.source_position;
782 declaration->symbol = symbol;
785 if(token.type == '{') {
786 if(declaration->init.is_defined) {
787 parser_print_error_prefix();
788 fprintf(stderr, "multiple definitions of enum %s\n",
791 record_declaration(declaration);
792 declaration->init.is_defined = 1;
794 parse_enum_entries();
802 * if a symbol is a typedef to another type, return true
804 static bool is_typedef_symbol(symbol_t *symbol)
806 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
807 if(declaration == NULL
808 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
814 static type_t *parse_typeof(void)
822 expression_t *expression = NULL;
826 case T___extension__:
827 /* this can be a prefix to a typename or an expression */
828 /* we simply eat it now. */
831 } while(token.type == T___extension__);
835 if(is_typedef_symbol(token.v.symbol)) {
836 type = parse_typename();
838 expression = parse_expression();
839 type = expression->datatype;
844 type = parse_typename();
848 expression = parse_expression();
849 type = expression->datatype;
855 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
856 typeof->type.type = TYPE_TYPEOF;
857 typeof->expression = expression;
858 typeof->typeof_type = type;
860 return (type_t*) typeof;
864 SPECIFIER_SIGNED = 1 << 0,
865 SPECIFIER_UNSIGNED = 1 << 1,
866 SPECIFIER_LONG = 1 << 2,
867 SPECIFIER_INT = 1 << 3,
868 SPECIFIER_DOUBLE = 1 << 4,
869 SPECIFIER_CHAR = 1 << 5,
870 SPECIFIER_SHORT = 1 << 6,
871 SPECIFIER_LONG_LONG = 1 << 7,
872 SPECIFIER_FLOAT = 1 << 8,
873 SPECIFIER_BOOL = 1 << 9,
874 SPECIFIER_VOID = 1 << 10,
875 #ifdef PROVIDE_COMPLEX
876 SPECIFIER_COMPLEX = 1 << 11,
878 #ifdef PROVIDE_IMAGINARY
879 SPECIFIER_IMAGINARY = 1 << 12,
883 static type_t *create_builtin_type(symbol_t *symbol)
885 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
886 type->type.type = TYPE_BUILTIN;
887 type->symbol = symbol;
889 return (type_t*) type;
892 static type_t *get_typedef_type(symbol_t *symbol)
894 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
895 if(declaration == NULL
896 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
899 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
900 typedef_type->type.type = TYPE_TYPEDEF;
901 typedef_type->declaration = declaration;
903 return (type_t*) typedef_type;
906 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
909 unsigned type_qualifiers = 0;
910 unsigned type_specifiers = 0;
917 #define MATCH_STORAGE_CLASS(token, class) \
919 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
920 parse_error("multiple storage classes in declaration " \
923 specifiers->storage_class = class; \
927 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
928 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
929 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
930 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
931 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
933 /* type qualifiers */
934 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
936 type_qualifiers |= qualifier; \
940 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
941 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
942 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
943 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
945 case T___extension__:
950 /* type specifiers */
951 #define MATCH_SPECIFIER(token, specifier, name) \
954 if(type_specifiers & specifier) { \
955 parse_error("multiple " name " type specifiers given"); \
957 type_specifiers |= specifier; \
961 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
962 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
963 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
964 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
965 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
966 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
967 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
968 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
969 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
970 #ifdef PROVIDE_COMPLEX
971 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
973 #ifdef PROVIDE_IMAGINARY
974 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
978 if(type_specifiers & SPECIFIER_LONG_LONG) {
979 parse_error("multiple type specifiers given");
980 } else if(type_specifiers & SPECIFIER_LONG) {
981 type_specifiers |= SPECIFIER_LONG_LONG;
983 type_specifiers |= SPECIFIER_LONG;
987 /* TODO: if type != NULL for the following rules should issue
990 compound_type_t *compound_type
991 = allocate_type_zero(sizeof(compound_type[0]));
992 compound_type->type.type = TYPE_COMPOUND_STRUCT;
993 compound_type->declaration = parse_compound_type_specifier(true);
995 type = (type_t*) compound_type;
999 compound_type_t *compound_type
1000 = allocate_type_zero(sizeof(compound_type[0]));
1001 compound_type->type.type = TYPE_COMPOUND_UNION;
1002 compound_type->declaration = parse_compound_type_specifier(false);
1004 type = (type_t*) compound_type;
1008 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1009 enum_type->type.type = TYPE_ENUM;
1010 enum_type->declaration = parse_enum_specifier();
1012 type = (type_t*) enum_type;
1016 type = parse_typeof();
1018 case T___builtin_va_list:
1019 type = create_builtin_type(token.v.symbol);
1023 case T___attribute__:
1028 case T_IDENTIFIER: {
1029 type_t *typedef_type = get_typedef_type(token.v.symbol);
1031 if(typedef_type == NULL)
1032 goto finish_specifiers;
1035 type = typedef_type;
1039 /* function specifier */
1041 goto finish_specifiers;
1048 atomic_type_type_t atomic_type;
1050 /* match valid basic types */
1051 switch(type_specifiers) {
1052 case SPECIFIER_VOID:
1053 atomic_type = ATOMIC_TYPE_VOID;
1055 case SPECIFIER_CHAR:
1056 atomic_type = ATOMIC_TYPE_CHAR;
1058 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1059 atomic_type = ATOMIC_TYPE_SCHAR;
1061 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1062 atomic_type = ATOMIC_TYPE_UCHAR;
1064 case SPECIFIER_SHORT:
1065 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1066 case SPECIFIER_SHORT | SPECIFIER_INT:
1067 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1068 atomic_type = ATOMIC_TYPE_SHORT;
1070 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1071 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1072 atomic_type = ATOMIC_TYPE_USHORT;
1075 case SPECIFIER_SIGNED:
1076 case SPECIFIER_SIGNED | SPECIFIER_INT:
1077 atomic_type = ATOMIC_TYPE_INT;
1079 case SPECIFIER_UNSIGNED:
1080 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1081 atomic_type = ATOMIC_TYPE_UINT;
1083 case SPECIFIER_LONG:
1084 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1085 case SPECIFIER_LONG | SPECIFIER_INT:
1086 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1087 atomic_type = ATOMIC_TYPE_LONG;
1089 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1090 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1091 atomic_type = ATOMIC_TYPE_ULONG;
1093 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1094 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1095 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1096 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1098 atomic_type = ATOMIC_TYPE_LONGLONG;
1100 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1101 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1103 atomic_type = ATOMIC_TYPE_ULONGLONG;
1105 case SPECIFIER_FLOAT:
1106 atomic_type = ATOMIC_TYPE_FLOAT;
1108 case SPECIFIER_DOUBLE:
1109 atomic_type = ATOMIC_TYPE_DOUBLE;
1111 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1112 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1114 case SPECIFIER_BOOL:
1115 atomic_type = ATOMIC_TYPE_BOOL;
1117 #ifdef PROVIDE_COMPLEX
1118 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1119 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1121 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1122 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1124 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1125 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1128 #ifdef PROVIDE_IMAGINARY
1129 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1130 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1132 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1133 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1135 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1136 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1140 /* invalid specifier combination, give an error message */
1141 if(type_specifiers == 0) {
1143 parse_warning("no type specifiers in declaration (using int)");
1144 atomic_type = ATOMIC_TYPE_INT;
1147 parse_error("no type specifiers given in declaration");
1149 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1150 (type_specifiers & SPECIFIER_UNSIGNED)) {
1151 parse_error("signed and unsigned specifiers gives");
1152 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1153 parse_error("only integer types can be signed or unsigned");
1155 parse_error("multiple datatypes in declaration");
1157 atomic_type = ATOMIC_TYPE_INVALID;
1160 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1161 atype->type.type = TYPE_ATOMIC;
1162 atype->atype = atomic_type;
1165 type = (type_t*) atype;
1167 if(type_specifiers != 0) {
1168 parse_error("multiple datatypes in declaration");
1172 type->qualifiers = type_qualifiers;
1174 type_t *result = typehash_insert(type);
1175 if(newtype && result != (type_t*) type) {
1179 specifiers->type = result;
1182 static type_qualifier_t parse_type_qualifiers(void)
1184 type_qualifier_t type_qualifiers = 0;
1187 switch(token.type) {
1188 /* type qualifiers */
1189 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1190 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1191 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1192 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1195 return type_qualifiers;
1200 static void parse_identifier_list(void)
1203 if(token.type != T_IDENTIFIER) {
1204 parse_error_expected("problem while parsing parameter identifier "
1205 "list", T_IDENTIFIER, 0);
1209 if(token.type != ',')
1215 static declaration_t *parse_parameter(void)
1217 declaration_specifiers_t specifiers;
1218 memset(&specifiers, 0, sizeof(specifiers));
1220 parse_declaration_specifiers(&specifiers);
1222 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1223 specifiers.type, 1);
1225 /* TODO check declaration constraints for parameters */
1226 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1227 parse_error("typedef not allowed in parameter list");
1233 static declaration_t *parse_parameters(method_type_t *type)
1235 if(token.type == T_IDENTIFIER) {
1236 symbol_t *symbol = token.v.symbol;
1237 if(!is_typedef_symbol(symbol)) {
1239 parse_identifier_list();
1244 if(token.type == ')') {
1245 type->unspecified_parameters = 1;
1248 if(token.type == T_void && look_ahead(1)->type == ')') {
1253 declaration_t *declarations = NULL;
1254 declaration_t *declaration;
1255 declaration_t *last_declaration = NULL;
1256 method_parameter_t *parameter;
1257 method_parameter_t *last_parameter = NULL;
1260 switch(token.type) {
1264 return declarations;
1267 case T___extension__:
1269 declaration = parse_parameter();
1271 parameter = allocate_type_zero(sizeof(parameter[0]));
1272 parameter->type = declaration->type;
1274 if(last_parameter != NULL) {
1275 last_declaration->context_next = declaration;
1276 last_parameter->next = parameter;
1278 type->parameters = parameter;
1279 declarations = declaration;
1281 last_parameter = parameter;
1282 last_declaration = declaration;
1286 return declarations;
1288 if(token.type != ',')
1289 return declarations;
1298 } construct_type_type_t;
1300 typedef struct construct_type_t construct_type_t;
1301 struct construct_type_t {
1302 construct_type_type_t type;
1303 construct_type_t *next;
1306 typedef struct parsed_pointer_t parsed_pointer_t;
1307 struct parsed_pointer_t {
1308 construct_type_t construct_type;
1309 type_qualifier_t type_qualifiers;
1312 typedef struct construct_method_type_t construct_method_type_t;
1313 struct construct_method_type_t {
1314 construct_type_t construct_type;
1315 method_type_t *method_type;
1318 typedef struct parsed_array_t parsed_array_t;
1319 struct parsed_array_t {
1320 construct_type_t construct_type;
1321 type_qualifier_t type_qualifiers;
1327 typedef struct construct_base_type_t construct_base_type_t;
1328 struct construct_base_type_t {
1329 construct_type_t construct_type;
1333 static construct_type_t *parse_pointer_declarator(void)
1337 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1338 memset(pointer, 0, sizeof(pointer[0]));
1339 pointer->type_qualifiers = parse_type_qualifiers();
1341 return (construct_type_t*) pointer;
1344 static construct_type_t *parse_array_declarator(void)
1348 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1349 memset(array, 0, sizeof(array[0]));
1351 if(token.type == T_static) {
1352 array->is_static = true;
1356 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1357 if(type_qualifiers != 0) {
1358 if(token.type == T_static) {
1359 array->is_static = true;
1363 array->type_qualifiers = type_qualifiers;
1365 if(token.type == '*' && look_ahead(1)->type == ']') {
1366 array->is_variable = true;
1368 } else if(token.type != ']') {
1369 array->size = parse_assignment_expression();
1374 return (construct_type_t*) array;
1377 static construct_type_t *parse_method_declarator(declaration_t *declaration)
1381 method_type_t *method_type
1382 = allocate_type_zero(sizeof(method_type[0]));
1383 method_type->type.type = TYPE_METHOD;
1385 declaration_t *parameters = parse_parameters(method_type);
1386 if(declaration != NULL) {
1387 declaration->context.declarations = parameters;
1390 construct_method_type_t *construct_method_type =
1391 obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
1392 memset(construct_method_type, 0, sizeof(construct_method_type[0]));
1393 construct_method_type->construct_type.type = CONSTRUCT_METHOD;
1394 construct_method_type->method_type = method_type;
1398 return (construct_type_t*) construct_method_type;
1401 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1402 int may_be_abstract)
1404 construct_type_t *result = NULL;
1405 construct_type_t *last = NULL;
1407 while(token.type == '*') {
1408 construct_type_t *type = parse_pointer_declarator();
1417 /* TODO: find out if this is correct */
1420 construct_type_t *inner_types = NULL;
1422 switch(token.type) {
1424 if(declaration == NULL) {
1425 parse_error("no identifier expected in typename");
1427 declaration->symbol = token.v.symbol;
1428 declaration->source_position = token.source_position;
1434 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1440 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1445 construct_type_t *type;
1446 switch(token.type) {
1448 type = parse_method_declarator(declaration);
1451 type = parse_array_declarator();
1454 goto declarator_finished;
1465 declarator_finished:
1468 if(inner_types != NULL) {
1470 last->next = inner_types;
1472 result = inner_types;
1480 static type_t *construct_declarator_type(construct_type_t *construct_list,
1483 construct_type_t *iter = construct_list;
1484 for( ; iter != NULL; iter = iter->next) {
1485 parsed_pointer_t *parsed_pointer;
1486 parsed_array_t *parsed_array;
1487 construct_method_type_t *construct_method_type;
1488 method_type_t *method_type;
1489 pointer_type_t *pointer_type;
1490 array_type_t *array_type;
1492 switch(iter->type) {
1493 case CONSTRUCT_METHOD:
1494 construct_method_type = (construct_method_type_t*) iter;
1495 method_type = construct_method_type->method_type;
1497 method_type->result_type = type;
1498 type = (type_t*) method_type;
1501 case CONSTRUCT_POINTER:
1502 parsed_pointer = (parsed_pointer_t*) iter;
1503 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1505 pointer_type->type.type = TYPE_POINTER;
1506 pointer_type->points_to = type;
1507 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1508 type = (type_t*) pointer_type;
1511 case CONSTRUCT_ARRAY:
1512 parsed_array = (parsed_array_t*) iter;
1513 array_type = allocate_type_zero(sizeof(array_type[0]));
1515 array_type->type.type = TYPE_ARRAY;
1516 array_type->element_type = type;
1517 array_type->type.qualifiers = parsed_array->type_qualifiers;
1518 array_type->is_static = parsed_array->is_static;
1519 array_type->is_variable = parsed_array->is_variable;
1520 array_type->size = parsed_array->size;
1521 type = (type_t*) array_type;
1525 type_t *hashed_type = typehash_insert((type_t*) type);
1526 if(hashed_type != type) {
1535 static declaration_t *parse_declarator(storage_class_t storage_class,
1536 type_t *type, int may_be_abstract)
1538 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1539 declaration->storage_class = storage_class;
1541 construct_type_t *construct_type
1542 = parse_inner_declarator(declaration, may_be_abstract);
1543 declaration->type = construct_declarator_type(construct_type, type);
1545 if(construct_type != NULL) {
1546 obstack_free(&temp_obst, construct_type);
1552 static type_t *parse_abstract_declarator(type_t *base_type)
1554 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1556 type_t *result = construct_declarator_type(construct_type, base_type);
1557 if(construct_type != NULL) {
1558 obstack_free(&temp_obst, construct_type);
1564 static declaration_t *record_declaration(declaration_t *declaration)
1566 assert(context != NULL);
1568 symbol_t *symbol = declaration->symbol;
1569 if(symbol != NULL) {
1570 declaration_t *alias = environment_push(declaration);
1571 if(alias != declaration)
1574 declaration->parent_context = context;
1577 if(last_declaration != NULL) {
1578 last_declaration->context_next = declaration;
1580 context->declarations = declaration;
1582 last_declaration = declaration;
1587 static void parser_error_multiple_definition(declaration_t *previous,
1588 declaration_t *declaration)
1590 parser_print_error_prefix_pos(declaration->source_position);
1591 fprintf(stderr, "multiple definition of symbol '%s'\n",
1592 declaration->symbol->string);
1593 parser_print_error_prefix_pos(previous->source_position);
1594 fprintf(stderr, "this is the location of the previous "
1599 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1602 declaration_t *ndeclaration
1603 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1605 declaration_t *declaration = record_declaration(ndeclaration);
1606 if(token.type == '=') {
1609 /* TODO: check that this is an allowed type (esp. no method type) */
1611 if(declaration->init.initializer != NULL) {
1612 parser_error_multiple_definition(declaration, ndeclaration);
1615 ndeclaration->init.initializer = parse_initializer();
1616 } else if(token.type == '{') {
1617 if(declaration->type->type != TYPE_METHOD) {
1618 parser_print_error_prefix();
1619 fprintf(stderr, "Declarator ");
1620 print_type_ext(declaration->type, declaration->symbol, NULL);
1621 fprintf(stderr, " is not a method type.\n");
1626 if(declaration->init.statement != NULL) {
1627 parser_error_multiple_definition(declaration, ndeclaration);
1629 if(ndeclaration != declaration) {
1630 memcpy(&declaration->context, &ndeclaration->context,
1631 sizeof(declaration->context));
1634 int top = environment_top();
1635 context_t *last_context = context;
1636 set_context(&declaration->context);
1638 /* push function parameters */
1639 declaration_t *parameter = declaration->context.declarations;
1640 for( ; parameter != NULL; parameter = parameter->context_next) {
1641 environment_push(parameter);
1644 statement_t *statement = parse_compound_statement();
1646 assert(context == &declaration->context);
1647 set_context(last_context);
1648 environment_pop_to(top);
1650 declaration->init.statement = statement;
1654 if(token.type != ',')
1661 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1664 if(token.type == ':') {
1666 parse_constant_expression();
1667 /* TODO (bitfields) */
1669 declaration_t *declaration
1670 = parse_declarator(specifiers->storage_class,
1671 specifiers->type, 1);
1673 /* TODO: check constraints for struct declarations */
1674 /* TODO: check for doubled fields */
1675 record_declaration(declaration);
1677 if(token.type == ':') {
1679 parse_constant_expression();
1680 /* TODO (bitfields) */
1684 if(token.type != ',')
1691 static void parse_compound_type_entries(void)
1695 while(token.type != '}' && token.type != T_EOF) {
1696 declaration_specifiers_t specifiers;
1697 memset(&specifiers, 0, sizeof(specifiers));
1698 parse_declaration_specifiers(&specifiers);
1700 parse_struct_declarators(&specifiers);
1702 if(token.type == T_EOF) {
1703 parse_error("unexpected error while parsing struct");
1708 static void parse_declaration(void)
1710 source_position_t source_position = token.source_position;
1712 declaration_specifiers_t specifiers;
1713 memset(&specifiers, 0, sizeof(specifiers));
1714 parse_declaration_specifiers(&specifiers);
1716 if(token.type == ';') {
1719 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1721 declaration->type = specifiers.type;
1722 declaration->storage_class = specifiers.storage_class;
1723 declaration->source_position = source_position;
1724 record_declaration(declaration);
1727 parse_init_declarators(&specifiers);
1730 static type_t *parse_typename(void)
1732 declaration_specifiers_t specifiers;
1733 memset(&specifiers, 0, sizeof(specifiers));
1734 parse_declaration_specifiers(&specifiers);
1735 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1736 /* TODO: improve error message, user does probably not know what a
1737 * storage class is...
1739 parse_error("typename may not have a storage class");
1742 type_t *result = parse_abstract_declarator(specifiers.type);
1750 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1751 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1752 expression_t *left);
1754 typedef struct expression_parser_function_t expression_parser_function_t;
1755 struct expression_parser_function_t {
1756 unsigned precedence;
1757 parse_expression_function parser;
1758 unsigned infix_precedence;
1759 parse_expression_infix_function infix_parser;
1762 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1764 static expression_t *expected_expression_error(void)
1766 parser_print_error_prefix();
1767 fprintf(stderr, "expected expression, got token ");
1768 print_token(stderr, & token);
1769 fprintf(stderr, "\n");
1771 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1772 expression->type = EXPR_INVALID;
1778 static expression_t *parse_string_const(void)
1780 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1782 cnst->expression.type = EXPR_STRING_LITERAL;
1783 cnst->expression.datatype = type_string;
1784 cnst->value = parse_string_literals();
1786 return (expression_t*) cnst;
1789 static expression_t *parse_int_const(void)
1791 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1793 cnst->expression.type = EXPR_CONST;
1794 cnst->expression.datatype = type_int;
1795 cnst->v.int_value = token.v.intvalue;
1799 return (expression_t*) cnst;
1802 static expression_t *parse_float_const(void)
1804 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1806 cnst->expression.type = EXPR_CONST;
1807 cnst->expression.datatype = type_double;
1808 cnst->v.float_value = token.v.floatvalue;
1812 return (expression_t*) cnst;
1815 static declaration_t *create_implicit_function(symbol_t *symbol,
1816 const source_position_t source_position)
1818 method_type_t *method_type = allocate_type_zero(sizeof(method_type));
1820 method_type->type.type = TYPE_METHOD;
1821 method_type->result_type = type_int;
1822 method_type->unspecified_parameters = true;
1824 type_t *type = typehash_insert((type_t*) method_type);
1825 if(type != (type_t*) method_type) {
1826 free_type(method_type);
1829 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1831 declaration->storage_class = STORAGE_CLASS_EXTERN;
1832 declaration->type = type;
1833 declaration->symbol = symbol;
1834 declaration->source_position = source_position;
1836 /* prepend the implicit definition to the global context
1837 * this is safe since the symbol wasn't declared as anything else yet
1839 assert(symbol->declaration == NULL);
1841 context_t *last_context = context;
1842 context = global_context;
1844 environment_push(declaration);
1845 declaration->context_next = context->declarations;
1846 context->declarations = declaration;
1848 context = last_context;
1853 static expression_t *parse_reference(void)
1855 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1857 ref->expression.type = EXPR_REFERENCE;
1858 ref->symbol = token.v.symbol;
1860 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
1862 source_position_t source_position = token.source_position;
1865 if(declaration == NULL) {
1867 /* an implicitely defined function */
1868 if(token.type == '(') {
1869 parser_print_prefix_pos(token.source_position);
1870 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1871 ref->symbol->string);
1873 declaration = create_implicit_function(ref->symbol,
1878 parser_print_error_prefix();
1879 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1880 return (expression_t*) ref;
1884 ref->declaration = declaration;
1885 ref->expression.datatype = declaration->type;
1887 return (expression_t*) ref;
1890 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1894 /* TODO check if cast is allowed and issue warnings/errors */
1897 static expression_t *parse_cast(void)
1899 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1901 cast->expression.type = EXPR_UNARY;
1902 cast->type = UNEXPR_CAST;
1903 cast->expression.source_position = token.source_position;
1905 type_t *type = parse_typename();
1908 expression_t *value = parse_sub_expression(20);
1910 check_cast_allowed(value, type);
1912 cast->expression.datatype = type;
1913 cast->value = value;
1915 return (expression_t*) cast;
1918 static expression_t *parse_statement_expression(void)
1920 statement_expression_t *expression
1921 = allocate_ast_zero(sizeof(expression[0]));
1922 expression->expression.type = EXPR_STATEMENT;
1923 expression->statement = parse_compound_statement();
1925 /* find last statement and use it's type */
1926 const statement_t *last_statement = NULL;
1927 const statement_t *statement = expression->statement;
1928 for( ; statement != NULL; statement = statement->next) {
1929 last_statement = statement;
1932 if(last_statement->type == STATEMENT_EXPRESSION) {
1933 const expression_statement_t *expression_statement =
1934 (const expression_statement_t*) last_statement;
1935 expression->expression.datatype
1936 = expression_statement->expression->datatype;
1938 expression->expression.datatype = type_void;
1943 return (expression_t*) expression;
1946 static expression_t *parse_brace_expression(void)
1950 switch(token.type) {
1952 /* gcc extension: a stement expression */
1953 return parse_statement_expression();
1957 return parse_cast();
1959 if(is_typedef_symbol(token.v.symbol)) {
1960 return parse_cast();
1964 expression_t *result = parse_expression();
1970 static expression_t *parse_function_keyword(void)
1972 eat(T___FUNCTION__);
1975 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1976 expression->expression.type = EXPR_FUNCTION;
1977 expression->expression.datatype = type_string;
1978 expression->value = "TODO: FUNCTION";
1980 return (expression_t*) expression;
1983 static expression_t *parse_pretty_function_keyword(void)
1985 eat(T___PRETTY_FUNCTION__);
1988 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1989 expression->expression.type = EXPR_PRETTY_FUNCTION;
1990 expression->expression.datatype = type_string;
1991 expression->value = "TODO: PRETTY FUNCTION";
1993 return (expression_t*) expression;
1996 static designator_t *parse_designator(void)
1998 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2000 if(token.type != T_IDENTIFIER) {
2001 parse_error_expected("problem while parsing member designator",
2006 result->symbol = token.v.symbol;
2009 designator_t *last_designator = result;
2011 if(token.type == '.') {
2013 if(token.type != T_IDENTIFIER) {
2014 parse_error_expected("problem while parsing member designator",
2019 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2020 designator->symbol = token.v.symbol;
2023 last_designator->next = designator;
2024 last_designator = designator;
2027 if(token.type == '[') {
2029 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2030 designator->array_access = parse_expression();
2031 if(designator->array_access == NULL) {
2037 last_designator->next = designator;
2038 last_designator = designator;
2047 static expression_t *parse_offsetof(void)
2049 eat(T___builtin_offsetof);
2051 offsetof_expression_t *expression
2052 = allocate_ast_zero(sizeof(expression[0]));
2053 expression->expression.type = EXPR_OFFSETOF;
2054 expression->expression.datatype = type_size_t;
2057 expression->type = parse_typename();
2059 expression->designator = parse_designator();
2062 return (expression_t*) expression;
2065 static expression_t *parse_va_arg(void)
2067 eat(T___builtin_va_arg);
2069 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2070 expression->expression.type = EXPR_VA_ARG;
2073 expression->arg = parse_assignment_expression();
2075 expression->expression.datatype = parse_typename();
2078 return (expression_t*) expression;
2081 static expression_t *parse_builtin_symbol(void)
2083 builtin_symbol_expression_t *expression
2084 = allocate_ast_zero(sizeof(expression[0]));
2085 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2087 /* TODO: set datatype */
2089 expression->symbol = token.v.symbol;
2093 return (expression_t*) expression;
2096 static expression_t *parse_primary_expression(void)
2098 switch(token.type) {
2100 return parse_int_const();
2101 case T_FLOATINGPOINT:
2102 return parse_float_const();
2103 case T_STRING_LITERAL:
2104 return parse_string_const();
2106 return parse_reference();
2107 case T___FUNCTION__:
2108 return parse_function_keyword();
2109 case T___PRETTY_FUNCTION__:
2110 return parse_pretty_function_keyword();
2111 case T___builtin_offsetof:
2112 return parse_offsetof();
2113 case T___builtin_va_arg:
2114 return parse_va_arg();
2115 case T___builtin_expect:
2116 case T___builtin_va_start:
2117 case T___builtin_va_end:
2118 return parse_builtin_symbol();
2121 return parse_brace_expression();
2124 parser_print_error_prefix();
2125 fprintf(stderr, "unexpected token ");
2126 print_token(stderr, &token);
2127 fprintf(stderr, "\n");
2130 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2131 expression->type = EXPR_INVALID;
2132 expression->datatype = type_void;
2137 static expression_t *parse_array_expression(unsigned precedence,
2138 expression_t *array_ref)
2144 array_access_expression_t *array_access
2145 = allocate_ast_zero(sizeof(array_access[0]));
2147 array_access->expression.type = EXPR_ARRAY_ACCESS;
2148 array_access->array_ref = array_ref;
2149 array_access->index = parse_expression();
2151 type_t *array_type = array_ref->datatype;
2152 if(array_type != NULL) {
2153 if(array_type->type == TYPE_POINTER) {
2154 pointer_type_t *pointer = (pointer_type_t*) array_type;
2155 array_access->expression.datatype = pointer->points_to;
2157 parser_print_error_prefix();
2158 fprintf(stderr, "array access on object with non-pointer type ");
2159 print_type(array_type);
2160 fprintf(stderr, "\n");
2164 if(token.type != ']') {
2165 parse_error_expected("Problem while parsing array access", ']', 0);
2166 return (expression_t*) array_access;
2170 return (expression_t*) array_access;
2173 static bool is_declaration_specifier(const token_t *token,
2174 bool only_type_specifiers)
2176 switch(token->type) {
2180 return is_typedef_symbol(token->v.symbol);
2183 if(only_type_specifiers)
2192 static expression_t *parse_sizeof(unsigned precedence)
2196 sizeof_expression_t *sizeof_expression
2197 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2198 sizeof_expression->expression.type = EXPR_SIZEOF;
2199 sizeof_expression->expression.datatype = type_size_t;
2201 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2203 sizeof_expression->type = parse_typename();
2206 expression_t *expression = parse_sub_expression(precedence);
2207 sizeof_expression->type = expression->datatype;
2208 sizeof_expression->size_expression = expression;
2211 return (expression_t*) sizeof_expression;
2214 static expression_t *parse_select_expression(unsigned precedence,
2215 expression_t *compound)
2219 assert(token.type == '.' || token.type == T_MINUSGREATER);
2222 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2224 select->expression.type = EXPR_SELECT;
2225 select->compound = compound;
2227 /* TODO: datatype */
2229 if(token.type != T_IDENTIFIER) {
2230 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2231 return (expression_t*) select;
2233 select->symbol = token.v.symbol;
2236 return (expression_t*) select;
2239 static expression_t *parse_call_expression(unsigned precedence,
2240 expression_t *expression)
2243 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2245 call->expression.type = EXPR_CALL;
2246 call->method = expression;
2248 /* parse arguments */
2251 if(token.type != ')') {
2252 call_argument_t *last_argument = NULL;
2255 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2257 argument->expression = parse_assignment_expression();
2258 if(last_argument == NULL) {
2259 call->arguments = argument;
2261 last_argument->next = argument;
2263 last_argument = argument;
2265 if(token.type != ',')
2272 type_t *type = expression->datatype;
2274 /* we can call pointer to function */
2275 if(type->type == TYPE_POINTER) {
2276 pointer_type_t *pointer = (pointer_type_t*) type;
2277 type = pointer->points_to;
2280 if(type == NULL || type->type != TYPE_METHOD) {
2281 parser_print_error_prefix();
2282 fprintf(stderr, "expected a method type for call but found type ");
2283 print_type(expression->datatype);
2284 fprintf(stderr, "\n");
2286 method_type_t *method_type = (method_type_t*) type;
2287 call->expression.datatype = method_type->result_type;
2291 return (expression_t*) call;
2294 static void type_error(const char *msg, const source_position_t source_position,
2297 parser_print_error_prefix_pos(source_position);
2298 fprintf(stderr, "%s, but found type ", msg);
2300 fputc('\n', stderr);
2304 static void type_error_incompatible(const char *msg,
2305 const source_position_t source_position, type_t *type1, type_t *type2)
2307 parser_print_error_prefix_pos(source_position);
2308 fprintf(stderr, "%s, incompatible types: ", msg);
2310 fprintf(stderr, " - ");
2312 fprintf(stderr, ")\n");
2316 static type_t *get_type_after_conversion(const type_t *type1,
2317 const type_t *type2)
2321 return (type_t*) type1;
2324 static expression_t *parse_conditional_expression(unsigned precedence,
2325 expression_t *expression)
2329 conditional_expression_t *conditional
2330 = allocate_ast_zero(sizeof(conditional[0]));
2331 conditional->expression.type = EXPR_CONDITIONAL;
2332 conditional->condition = expression;
2335 type_t *condition_type = conditional->condition->datatype;
2336 if(condition_type != NULL) {
2337 if(!is_type_scalar(condition_type)) {
2338 type_error("expected a scalar type", expression->source_position,
2343 conditional->true_expression = parse_expression();
2345 conditional->false_expression = parse_sub_expression(precedence);
2347 type_t *true_type = conditional->true_expression->datatype;
2348 if(true_type == NULL)
2349 return (expression_t*) conditional;
2350 type_t *false_type = conditional->false_expression->datatype;
2351 if(false_type == NULL)
2352 return (expression_t*) conditional;
2355 if(true_type == false_type) {
2356 conditional->expression.datatype = true_type;
2357 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2358 type_t *result = get_type_after_conversion(true_type, false_type);
2359 /* TODO: create implicit convs if necessary */
2360 conditional->expression.datatype = result;
2361 } else if(true_type->type == TYPE_POINTER &&
2362 false_type->type == TYPE_POINTER &&
2363 true /* TODO compatible points_to types */) {
2365 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2366 || (is_null_ptr_const(false_type) &&
2367 true_type->type == TYPE_POINTER) TODO*/ false) {
2369 } else if(/* 1 is pointer to object type, other is void* */ false) {
2372 type_error_incompatible("problem while parsing conditional",
2373 expression->source_position, true_type,
2377 return (expression_t*) conditional;
2380 static expression_t *parse_extension(unsigned precedence)
2382 eat(T___extension__);
2384 /* TODO enable extensions */
2386 return parse_sub_expression(precedence);
2389 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
2391 expression_t *parse_##unexpression_type(unsigned precedence) \
2395 unary_expression_t *unary_expression \
2396 = allocate_ast_zero(sizeof(unary_expression[0])); \
2397 unary_expression->expression.type = EXPR_UNARY; \
2398 unary_expression->type = unexpression_type; \
2399 unary_expression->value = parse_sub_expression(precedence); \
2401 return (expression_t*) unary_expression; \
2404 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
2405 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
2406 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
2407 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
2408 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
2409 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
2410 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
2411 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
2413 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
2415 expression_t *parse_##unexpression_type(unsigned precedence, \
2416 expression_t *left) \
2418 (void) precedence; \
2421 unary_expression_t *unary_expression \
2422 = allocate_ast_zero(sizeof(unary_expression[0])); \
2423 unary_expression->expression.type = EXPR_UNARY; \
2424 unary_expression->type = unexpression_type; \
2425 unary_expression->value = left; \
2427 return (expression_t*) unary_expression; \
2430 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
2431 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
2433 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
2435 expression_t *parse_##binexpression_type(unsigned precedence, \
2436 expression_t *left) \
2440 expression_t *right = parse_sub_expression(precedence); \
2442 binary_expression_t *binexpr \
2443 = allocate_ast_zero(sizeof(binexpr[0])); \
2444 binexpr->expression.type = EXPR_BINARY; \
2445 binexpr->type = binexpression_type; \
2446 binexpr->left = left; \
2447 binexpr->right = right; \
2449 return (expression_t*) binexpr; \
2452 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2453 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2454 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2455 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2456 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2457 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2458 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2459 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2460 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2461 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2462 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2463 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2464 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2465 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2466 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2467 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2468 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2469 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2470 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2471 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2472 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2473 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2474 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2475 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2476 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2477 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2478 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2479 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2480 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2481 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2483 static expression_t *parse_sub_expression(unsigned precedence)
2485 if(token.type < 0) {
2486 return expected_expression_error();
2489 expression_parser_function_t *parser
2490 = &expression_parsers[token.type];
2491 source_position_t source_position = token.source_position;
2494 if(parser->parser != NULL) {
2495 left = parser->parser(parser->precedence);
2497 left = parse_primary_expression();
2499 assert(left != NULL);
2500 left->source_position = source_position;
2503 if(token.type < 0) {
2504 return expected_expression_error();
2507 parser = &expression_parsers[token.type];
2508 if(parser->infix_parser == NULL)
2510 if(parser->infix_precedence < precedence)
2513 left = parser->infix_parser(parser->infix_precedence, left);
2515 assert(left != NULL);
2516 assert(left->type != EXPR_INVALID);
2517 left->source_position = source_position;
2523 static expression_t *parse_expression(void)
2525 return parse_sub_expression(1);
2530 void register_expression_parser(parse_expression_function parser,
2531 int token_type, unsigned precedence)
2533 expression_parser_function_t *entry = &expression_parsers[token_type];
2535 if(entry->parser != NULL) {
2536 fprintf(stderr, "for token ");
2537 print_token_type(stderr, token_type);
2538 fprintf(stderr, "\n");
2539 panic("trying to register multiple expression parsers for a token");
2541 entry->parser = parser;
2542 entry->precedence = precedence;
2545 void register_expression_infix_parser(parse_expression_infix_function parser,
2546 int token_type, unsigned precedence)
2548 expression_parser_function_t *entry = &expression_parsers[token_type];
2550 if(entry->infix_parser != NULL) {
2551 fprintf(stderr, "for token ");
2552 print_token_type(stderr, token_type);
2553 fprintf(stderr, "\n");
2554 panic("trying to register multiple infix expression parsers for a "
2557 entry->infix_parser = parser;
2558 entry->infix_precedence = precedence;
2561 static void init_expression_parsers(void)
2563 memset(&expression_parsers, 0, sizeof(expression_parsers));
2565 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2566 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2567 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2568 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2569 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2570 T_GREATERGREATER, 16);
2571 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2572 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2573 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2574 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2575 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2576 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2577 T_GREATEREQUAL, 14);
2578 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2579 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2580 T_EXCLAMATIONMARKEQUAL, 13);
2581 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2582 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2583 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2584 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2585 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2586 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2587 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2588 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2589 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2590 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2591 T_ASTERISKEQUAL, 2);
2592 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2593 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2595 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2596 T_LESSLESSEQUAL, 2);
2597 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2598 T_GREATERGREATEREQUAL, 2);
2599 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2601 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2603 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2606 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2608 register_expression_infix_parser(parse_array_expression, '[', 30);
2609 register_expression_infix_parser(parse_call_expression, '(', 30);
2610 register_expression_infix_parser(parse_select_expression, '.', 30);
2611 register_expression_infix_parser(parse_select_expression,
2612 T_MINUSGREATER, 30);
2613 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2615 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2618 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2619 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2620 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2621 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2622 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2623 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2624 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2625 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2626 register_expression_parser(parse_sizeof, T_sizeof, 25);
2627 register_expression_parser(parse_extension, T___extension__, 25);
2631 static statement_t *parse_case_statement(void)
2634 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2635 label->statement.type = STATEMENT_CASE_LABEL;
2636 label->statement.source_position = token.source_position;
2638 label->expression = parse_expression();
2641 label->statement.next = parse_statement();
2643 return (statement_t*) label;
2646 static statement_t *parse_default_statement(void)
2650 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2651 label->statement.type = STATEMENT_CASE_LABEL;
2652 label->statement.source_position = token.source_position;
2655 label->statement.next = parse_statement();
2657 return (statement_t*) label;
2660 static statement_t *parse_label_statement(void)
2669 static statement_t *parse_if(void)
2673 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2674 statement->statement.type = STATEMENT_IF;
2675 statement->statement.source_position = token.source_position;
2678 statement->condition = parse_expression();
2681 statement->true_statement = parse_statement();
2682 if(token.type == T_else) {
2684 statement->false_statement = parse_statement();
2687 return (statement_t*) statement;
2690 static statement_t *parse_switch(void)
2694 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2695 statement->statement.type = STATEMENT_SWITCH;
2696 statement->statement.source_position = token.source_position;
2699 statement->expression = parse_expression();
2701 statement->body = parse_statement();
2703 return (statement_t*) statement;
2706 static statement_t *parse_while(void)
2710 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2711 statement->statement.type = STATEMENT_WHILE;
2712 statement->statement.source_position = token.source_position;
2715 statement->condition = parse_expression();
2717 statement->body = parse_statement();
2719 return (statement_t*) statement;
2722 static statement_t *parse_do(void)
2726 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2727 statement->statement.type = STATEMENT_DO_WHILE;
2728 statement->statement.source_position = token.source_position;
2730 statement->body = parse_statement();
2733 statement->condition = parse_expression();
2737 return (statement_t*) statement;
2740 static statement_t *parse_for(void)
2744 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2745 statement->statement.type = STATEMENT_FOR;
2746 statement->statement.source_position = token.source_position;
2750 int top = environment_top();
2751 context_t *last_context = context;
2752 set_context(&statement->context);
2754 if(token.type != ';') {
2755 if(is_declaration_specifier(&token, false)) {
2756 parse_declaration();
2758 statement->initialisation = parse_expression();
2765 if(token.type != ';') {
2766 statement->condition = parse_expression();
2769 if(token.type != ')') {
2770 statement->step = parse_expression();
2773 statement->body = parse_statement();
2775 assert(context == &statement->context);
2776 set_context(last_context);
2777 environment_pop_to(top);
2779 return (statement_t*) statement;
2782 static statement_t *parse_goto(void)
2785 expect(T_IDENTIFIER);
2791 static statement_t *parse_continue(void)
2796 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2797 statement->source_position = token.source_position;
2798 statement->type = STATEMENT_CONTINUE;
2803 static statement_t *parse_break(void)
2808 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2809 statement->source_position = token.source_position;
2810 statement->type = STATEMENT_BREAK;
2815 static statement_t *parse_return(void)
2819 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2821 statement->statement.type = STATEMENT_RETURN;
2822 statement->statement.source_position = token.source_position;
2823 if(token.type != ';') {
2824 statement->return_value = parse_expression();
2828 return (statement_t*) statement;
2831 static statement_t *parse_declaration_statement(void)
2833 declaration_t *before = last_declaration;
2835 declaration_statement_t *statement
2836 = allocate_ast_zero(sizeof(statement[0]));
2837 statement->statement.type = STATEMENT_DECLARATION;
2838 statement->statement.source_position = token.source_position;
2840 declaration_specifiers_t specifiers;
2841 memset(&specifiers, 0, sizeof(specifiers));
2842 parse_declaration_specifiers(&specifiers);
2844 if(token.type == ';') {
2847 parse_init_declarators(&specifiers);
2850 if(before == NULL) {
2851 statement->declarations_begin = context->declarations;
2853 statement->declarations_begin = before->context_next;
2855 statement->declarations_end = last_declaration;
2857 return (statement_t*) statement;
2860 static statement_t *parse_expression_statement(void)
2862 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2863 statement->statement.type = STATEMENT_EXPRESSION;
2864 statement->statement.source_position = token.source_position;
2866 statement->expression = parse_expression();
2870 return (statement_t*) statement;
2873 static statement_t *parse_statement(void)
2875 statement_t *statement = NULL;
2877 /* declaration or statement */
2878 switch(token.type) {
2880 statement = parse_case_statement();
2884 statement = parse_default_statement();
2888 statement = parse_compound_statement();
2892 statement = parse_if();
2896 statement = parse_switch();
2900 statement = parse_while();
2904 statement = parse_do();
2908 statement = parse_for();
2912 statement = parse_goto();
2916 statement = parse_continue();
2920 statement = parse_break();
2924 statement = parse_return();
2933 if(look_ahead(1)->type == ':') {
2934 statement = parse_label_statement();
2938 if(is_typedef_symbol(token.v.symbol)) {
2939 statement = parse_declaration_statement();
2943 statement = parse_expression_statement();
2946 case T___extension__:
2947 /* this can be a prefix to a declaration or an expression statement */
2948 /* we simply eat it now and parse the rest with tail recursion */
2951 } while(token.type == T___extension__);
2952 statement = parse_statement();
2956 statement = parse_declaration_statement();
2960 statement = parse_expression_statement();
2964 assert(statement == NULL || statement->source_position.input_name != NULL);
2969 static statement_t *parse_compound_statement(void)
2973 compound_statement_t *compound_statement
2974 = allocate_ast_zero(sizeof(compound_statement[0]));
2975 compound_statement->statement.type = STATEMENT_COMPOUND;
2976 compound_statement->statement.source_position = token.source_position;
2978 int top = environment_top();
2979 context_t *last_context = context;
2980 set_context(&compound_statement->context);
2982 statement_t *last_statement = NULL;
2984 while(token.type != '}' && token.type != T_EOF) {
2985 statement_t *statement = parse_statement();
2986 if(statement == NULL)
2989 if(last_statement != NULL) {
2990 last_statement->next = statement;
2992 compound_statement->statements = statement;
2995 while(statement->next != NULL)
2996 statement = statement->next;
2998 last_statement = statement;
3001 assert(context == &compound_statement->context);
3002 set_context(last_context);
3003 environment_pop_to(top);
3007 return (statement_t*) compound_statement;
3010 static translation_unit_t *parse_translation_unit(void)
3012 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3014 assert(global_context == NULL);
3015 global_context = &unit->context;
3017 assert(context == NULL);
3018 set_context(&unit->context);
3020 while(token.type != T_EOF) {
3021 parse_declaration();
3024 assert(context == &unit->context);
3026 last_declaration = NULL;
3028 assert(global_context == &unit->context);
3029 global_context = NULL;
3034 translation_unit_t *parse(void)
3036 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3038 type_set_output(stderr);
3040 lookahead_bufpos = 0;
3041 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3044 translation_unit_t *unit = parse_translation_unit();
3046 DEL_ARR_F(environment_stack);
3051 void init_parser(void)
3053 init_expression_parsers();
3054 obstack_init(&temp_obst);
3056 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3057 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3058 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3059 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3060 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3061 type_string = make_pointer_type(type_const_char, 0);
3064 void exit_parser(void)
3066 obstack_free(&temp_obst, NULL);