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
22 struct environment_entry_t {
24 declaration_t *old_declaration;
25 const void *old_context;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static struct obstack environment_obstack;
32 static environment_entry_t **environment_stack = NULL;
33 static context_t *global_context = NULL;
34 static context_t *context = NULL;
35 static declaration_t *last_declaration = NULL;
36 static struct obstack temp_obst;
38 static type_t *type_int = NULL;
39 static type_t *type_double = NULL;
40 static type_t *type_const_char = NULL;
41 static type_t *type_string = NULL;
42 static type_t *type_void = NULL;
43 static type_t *type_size_t = NULL;
45 static statement_t *parse_compound_statement(void);
46 static statement_t *parse_statement(void);
48 static expression_t *parse_sub_expression(unsigned precedence);
49 static expression_t *parse_expression(void);
50 static type_t *parse_typename(void);
52 #define STORAGE_CLASSES \
59 #define TYPE_QUALIFIERS \
65 #ifdef PROVIDE_COMPLEX
66 #define COMPLEX_SPECIFIERS \
69 #define COMPLEX_SPECIFIERS
72 #ifdef PROVIDE_IMAGINARY
73 #define IMAGINARY_SPECIFIERS \
76 #define IMAGINARY_SPECIFIERS
79 #define TYPE_SPECIFIERS \
97 #define DECLARATION_START \
102 #define TYPENAME_START \
106 static inline void *allocate_ast_zero(size_t size)
108 void *res = allocate_ast(size);
109 memset(res, 0, size);
113 static inline void *allocate_type_zero(size_t size)
115 void *res = obstack_alloc(type_obst, size);
116 memset(res, 0, size);
120 static inline void free_type(void *type)
122 obstack_free(type_obst, type);
126 * returns the top element of the environment stack
128 static inline size_t environment_top(void)
130 return ARR_LEN(environment_stack);
135 static inline void next_token(void)
137 token = lookahead_buffer[lookahead_bufpos];
138 lookahead_buffer[lookahead_bufpos] = lexer_token;
141 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
144 print_token(stderr, &token);
145 fprintf(stderr, "\n");
149 static inline const token_t *look_ahead(int num)
151 assert(num > 0 && num <= MAX_LOOKAHEAD);
152 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
153 return & lookahead_buffer[pos];
156 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
160 #ifdef ABORT_ON_ERROR
165 void parser_print_prefix_pos(const source_position_t source_position)
167 fputs(source_position.input_name, stderr);
169 fprintf(stderr, "%d", source_position.linenr);
173 void parser_print_error_prefix_pos(const source_position_t source_position)
175 parser_print_prefix_pos(source_position);
176 fputs("error: ", stderr);
180 void parser_print_error_prefix(void)
182 parser_print_prefix_pos(token.source_position);
186 static void parse_error(const char *message)
188 parser_print_error_prefix();
189 fprintf(stderr, "parse error: %s\n", message);
192 __attribute__((unused))
193 static void parse_warning(const char *message)
195 parser_print_prefix_pos(token.source_position);
196 fprintf(stderr, "warning: %s\n", message);
199 static void parse_error_expected(const char *message, ...)
204 if(message != NULL) {
205 parser_print_error_prefix();
206 fprintf(stderr, "%s\n", message);
208 parser_print_error_prefix();
209 fputs("Parse error: got ", stderr);
210 print_token(stderr, &token);
211 fputs(", expected ", stderr);
213 va_start(args, message);
214 token_type_t token_type = va_arg(args, token_type_t);
215 while(token_type != 0) {
219 fprintf(stderr, ", ");
221 print_token_type(stderr, token_type);
222 token_type = va_arg(args, token_type_t);
225 fprintf(stderr, "\n");
228 static void eat_block(void)
230 if(token.type == '{')
233 while(token.type != '}') {
234 if(token.type == T_EOF)
236 if(token.type == '{') {
245 static void eat_statement(void)
247 while(token.type != ';') {
248 if(token.type == T_EOF)
250 if(token.type == '}')
252 if(token.type == '{') {
261 static void eat_brace(void)
263 if(token.type == '(')
266 while(token.type != ')') {
267 if(token.type == T_EOF)
269 if(token.type == ')' || token.type == ';' || token.type == '}') {
272 if(token.type == '(') {
276 if(token.type == '{') {
285 #define expect(expected) \
286 if(UNLIKELY(token.type != (expected))) { \
287 parse_error_expected(NULL, (expected), 0); \
293 #define expect_void(expected) \
294 if(UNLIKELY(token.type != (expected))) { \
295 parse_error_expected(NULL, (expected), 0); \
301 static void set_context(context_t *new_context)
303 context = new_context;
305 declaration_t *declaration = new_context->declarations;
306 if(declaration != NULL) {
308 if(declaration->next == NULL)
310 declaration = declaration->next;
314 last_declaration = declaration;
318 * called when we find a 2nd declarator for an identifier we already have a
321 static bool is_compatible_declaration (declaration_t *declaration,
322 declaration_t *previous)
324 /* TODO: not correct yet */
325 return declaration->type == previous->type;
329 * pushs an environment_entry on the environment stack and links the
330 * corresponding symbol to the new entry
332 static inline declaration_t *environment_push(declaration_t *declaration,
335 symbol_t *symbol = declaration->symbol;
336 assert(declaration != symbol->declaration);
337 assert(declaration->source_position.input_name != NULL);
339 if(symbol->context == context) {
340 declaration_t *previous_declaration = symbol->declaration;
341 if(symbol->declaration != NULL) {
342 if(!is_compatible_declaration(declaration, previous_declaration)) {
343 parser_print_error_prefix_pos(declaration->source_position);
344 fprintf(stderr, "definition of symbol '%s' with type ",
345 declaration->symbol->string);
347 print_type(declaration->type);
349 parser_print_error_prefix_pos(
350 previous_declaration->source_position);
351 fprintf(stderr, "is incompatible with previous declaration "
353 print_type(previous_declaration->type);
356 return previous_declaration;
360 environment_entry_t *entry
361 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
362 memset(entry, 0, sizeof(entry[0]));
364 int top = ARR_LEN(environment_stack);
365 ARR_RESIZE(environment_stack, top + 1);
366 environment_stack[top] = entry;
368 entry->old_declaration = symbol->declaration;
369 entry->old_context = symbol->context;
370 entry->symbol = symbol;
371 symbol->declaration = declaration;
372 symbol->context = context;
378 * pops symbols from the environment stack until @p new_top is the top element
380 static inline void environment_pop_to(size_t new_top)
382 environment_entry_t *entry = NULL;
383 size_t top = ARR_LEN(environment_stack);
389 assert(new_top < top);
392 entry = environment_stack[i - 1];
394 symbol_t *symbol = entry->symbol;
396 symbol->declaration = entry->old_declaration;
397 symbol->context = entry->old_context;
400 } while(i != new_top);
401 obstack_free(&environment_obstack, entry);
403 ARR_SHRINKLEN(environment_stack, (int) new_top);
408 static expression_t *parse_constant_expression(void)
410 /* start parsing at precedence 7 (conditional expression) */
411 return parse_sub_expression(7);
414 static expression_t *parse_assignment_expression(void)
416 /* start parsing at precedence 2 (assignment expression) */
417 return parse_sub_expression(2);
420 static void parse_compound_type_entries(void);
421 static void parse_declarator(declaration_t *declaration,
422 storage_class_t storage_class, type_t *type,
423 int may_be_abstract);
424 static declaration_t *record_declaration(declaration_t *declaration);
426 typedef struct declaration_specifiers_t declaration_specifiers_t;
427 struct declaration_specifiers_t {
428 storage_class_t storage_class;
432 static const char *parse_string_literals(void)
434 assert(token.type == T_STRING_LITERAL);
435 const char *result = token.v.string;
439 while(token.type == T_STRING_LITERAL) {
440 result = concat_strings(result, token.v.string);
447 static void parse_attributes(void)
451 case T___attribute__:
459 parse_error("EOF while parsing attribute");
477 if(token.type != T_STRING_LITERAL) {
478 parse_error_expected("while parsing assembler attribute",
483 parse_string_literals();
488 goto attributes_finished;
496 static designator_t *parse_designation(void)
498 if(token.type != '[' && token.type != '.')
501 designator_t *result = NULL;
502 designator_t *last = NULL;
505 designator_t *designator;
508 designator = allocate_ast_zero(sizeof(designator[0]));
510 designator->array_access = parse_constant_expression();
514 designator = allocate_ast_zero(sizeof(designator[0]));
516 if(token.type != T_IDENTIFIER) {
517 parse_error_expected("problem while parsing designator",
521 designator->symbol = token.v.symbol;
529 assert(designator != NULL);
531 last->next = designator;
539 static initializer_t *parse_initializer_list(void);
541 static initializer_t *parse_initializer(void)
543 designator_t *designator = parse_designation();
545 initializer_t *result;
546 if(token.type == '{') {
547 result = parse_initializer_list();
549 result = allocate_ast_zero(sizeof(result[0]));
550 result->type = INITIALIZER_VALUE;
551 result->v.value = parse_assignment_expression();
553 result->designator = designator;
558 static initializer_t *parse_initializer_list(void)
562 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
563 result->type = INITIALIZER_LIST;
565 initializer_t *last = NULL;
567 initializer_t *initializer = parse_initializer();
569 last->next = initializer;
571 result->v.list = initializer;
575 if(token.type == '}')
578 if(token.type != ',') {
579 parse_error_expected("problem while parsing initializer list",
586 if(token.type == '}')
595 static compound_type_t *find_compound_type(compound_type_t *types,
596 const symbol_t *symbol)
598 compound_type_t *type = types;
599 for( ; type != NULL; type = type->next) {
600 if(type->symbol == symbol)
607 static type_t *parse_compound_type_specifier(bool is_struct)
615 symbol_t *symbol = NULL;
616 compound_type_t *compound_type = NULL;
618 if(token.type == T_IDENTIFIER) {
619 symbol = token.v.symbol;
622 if(context != NULL) {
624 compound_type = find_compound_type(context->structs, symbol);
626 compound_type = find_compound_type(context->unions, symbol);
629 } else if(token.type != '{') {
631 parse_error_expected("problem while parsing struct type specifier",
632 T_IDENTIFIER, '{', 0);
634 parse_error_expected("problem while parsing union type specifier",
635 T_IDENTIFIER, '{', 0);
641 if(compound_type == NULL) {
642 compound_type = allocate_type_zero(sizeof(compound_type[0]));
645 compound_type->type.type = TYPE_COMPOUND_STRUCT;
647 compound_type->type.type = TYPE_COMPOUND_UNION;
649 compound_type->source_position = token.source_position;
650 compound_type->symbol = symbol;
653 if(token.type == '{') {
654 if(compound_type->defined) {
655 parser_print_error_prefix();
656 fprintf(stderr, "multiple definition of %s %s\n",
657 is_struct ? "struct" : "union", symbol->string);
658 compound_type->context.declarations = NULL;
660 compound_type->defined = 1;
662 int top = environment_top();
663 context_t *last_context = context;
664 set_context(&compound_type->context);
666 parse_compound_type_entries();
669 assert(context == &compound_type->context);
670 set_context(last_context);
671 environment_pop_to(top);
674 return (type_t*) compound_type;
677 static void parse_enum_entries(void)
681 if(token.type == '}') {
683 parse_error("empty enum not allowed");
688 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
690 if(token.type != T_IDENTIFIER) {
691 parse_error_expected("problem while parsing enum entry",
696 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
697 entry->symbol = token.v.symbol;
698 entry->source_position = token.source_position;
701 if(token.type == '=') {
703 entry->initializer = parse_initializer();
706 record_declaration(entry);
708 if(token.type != ',')
711 } while(token.type != '}');
716 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
718 enum_type_t *type = types;
719 for( ; type != NULL; type = type->next) {
720 if(type->symbol == symbol)
727 static type_t *parse_enum_specifier(void)
731 symbol_t *symbol = NULL;
732 enum_type_t *enum_type = NULL;
734 if(token.type == T_IDENTIFIER) {
735 symbol = token.v.symbol;
738 if(context != NULL) {
739 enum_type = find_enum_type(context->enums, symbol);
741 } else if(token.type != '{') {
742 parse_error_expected("problem while parsing enum type specifier",
743 T_IDENTIFIER, '{', 0);
747 if(enum_type == NULL) {
748 enum_type = allocate_type_zero(sizeof(enum_type[0]));
749 enum_type->type.type = TYPE_ENUM;
750 enum_type->source_position = token.source_position;
751 enum_type->symbol = symbol;
754 if(token.type == '{') {
755 if(enum_type->defined) {
756 parser_print_error_prefix();
757 fprintf(stderr, "multiple definitions of enum %s\n",
759 enum_type->entries_begin = NULL;
760 enum_type->entries_end = NULL;
762 enum_type->defined = 1;
764 declaration_t *before = last_declaration;
766 parse_enum_entries();
770 enum_type->entries_begin = context->declarations;
772 enum_type->entries_begin = before->next;
774 enum_type->entries_end = last_declaration;
777 return (type_t*) enum_type;
780 static type_t *parse_typeof(void)
788 declaration_t *declaration;
789 expression_t *expression;
793 case T___extension__:
794 /* this can be a prefix to a typename or an expression */
795 /* we simply eat it now. */
798 } while(token.type == T___extension__);
802 declaration = token.v.symbol->declaration;
803 if(declaration != NULL
804 && declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
805 result = parse_typename();
808 expression = parse_expression();
809 result = expression->datatype;
813 result = parse_typename();
817 expression = parse_expression();
818 result = expression->datatype;
828 SPECIFIER_SIGNED = 1 << 0,
829 SPECIFIER_UNSIGNED = 1 << 1,
830 SPECIFIER_LONG = 1 << 2,
831 SPECIFIER_INT = 1 << 3,
832 SPECIFIER_DOUBLE = 1 << 4,
833 SPECIFIER_CHAR = 1 << 5,
834 SPECIFIER_SHORT = 1 << 6,
835 SPECIFIER_LONG_LONG = 1 << 7,
836 SPECIFIER_FLOAT = 1 << 8,
837 SPECIFIER_BOOL = 1 << 9,
838 SPECIFIER_VOID = 1 << 10,
839 #ifdef PROVIDE_COMPLEX
840 SPECIFIER_COMPLEX = 1 << 11,
842 #ifdef PROVIDE_IMAGINARY
843 SPECIFIER_IMAGINARY = 1 << 12,
847 static type_t *create_builtin_type(symbol_t *symbol)
849 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
850 type->type.type = TYPE_BUILTIN;
851 type->symbol = symbol;
853 type_t *result = typehash_insert((type_t*) type);
854 if(result != (type_t*) type) {
861 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
863 declaration_t *declaration;
865 unsigned type_qualifiers = 0;
866 unsigned type_specifiers = 0;
873 #define MATCH_STORAGE_CLASS(token, class) \
875 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
876 parse_error("multiple storage classes in declaration " \
879 specifiers->storage_class = class; \
883 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
884 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
885 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
886 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
887 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
889 /* type qualifiers */
890 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
892 type_qualifiers |= qualifier; \
896 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
897 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
898 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
899 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
901 case T___extension__:
906 /* type specifiers */
907 #define MATCH_SPECIFIER(token, specifier, name) \
910 if(type_specifiers & specifier) { \
911 parse_error("multiple " name " type specifiers given"); \
913 type_specifiers |= specifier; \
917 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
918 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
919 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
920 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
921 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
922 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
923 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
924 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
925 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
926 #ifdef PROVIDE_COMPLEX
927 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
929 #ifdef PROVIDE_IMAGINARY
930 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
934 if(type_specifiers & SPECIFIER_LONG_LONG) {
935 parse_error("multiple type specifiers given");
936 } else if(type_specifiers & SPECIFIER_LONG) {
937 type_specifiers |= SPECIFIER_LONG_LONG;
939 type_specifiers |= SPECIFIER_LONG;
943 /* TODO: if type != NULL for the following rules issue an error */
945 type = parse_compound_type_specifier(true);
948 type = parse_compound_type_specifier(false);
951 type = parse_enum_specifier();
954 type = parse_typeof();
956 case T___builtin_va_list:
957 type = create_builtin_type(token.v.symbol);
961 case T___attribute__:
967 declaration = token.v.symbol->declaration;
968 if(declaration == NULL ||
969 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
970 goto finish_specifiers;
973 type = declaration->type;
974 assert(type != NULL);
978 /* function specifier */
980 goto finish_specifiers;
987 atomic_type_type_t atomic_type;
989 /* match valid basic types */
990 switch(type_specifiers) {
992 atomic_type = ATOMIC_TYPE_VOID;
995 atomic_type = ATOMIC_TYPE_CHAR;
997 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
998 atomic_type = ATOMIC_TYPE_SCHAR;
1000 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1001 atomic_type = ATOMIC_TYPE_UCHAR;
1003 case SPECIFIER_SHORT:
1004 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1005 case SPECIFIER_SHORT | SPECIFIER_INT:
1006 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1007 atomic_type = ATOMIC_TYPE_SHORT;
1009 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1010 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1011 atomic_type = ATOMIC_TYPE_USHORT;
1014 case SPECIFIER_SIGNED:
1015 case SPECIFIER_SIGNED | SPECIFIER_INT:
1016 atomic_type = ATOMIC_TYPE_INT;
1018 case SPECIFIER_UNSIGNED:
1019 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1020 atomic_type = ATOMIC_TYPE_UINT;
1022 case SPECIFIER_LONG:
1023 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1024 case SPECIFIER_LONG | SPECIFIER_INT:
1025 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1026 atomic_type = ATOMIC_TYPE_LONG;
1028 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1029 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1030 atomic_type = ATOMIC_TYPE_ULONG;
1032 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1033 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1034 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1035 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1037 atomic_type = ATOMIC_TYPE_LONGLONG;
1039 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1040 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1042 atomic_type = ATOMIC_TYPE_ULONGLONG;
1044 case SPECIFIER_FLOAT:
1045 atomic_type = ATOMIC_TYPE_FLOAT;
1047 case SPECIFIER_DOUBLE:
1048 atomic_type = ATOMIC_TYPE_DOUBLE;
1050 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1051 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1053 case SPECIFIER_BOOL:
1054 atomic_type = ATOMIC_TYPE_BOOL;
1056 #ifdef PROVIDE_COMPLEX
1057 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1058 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1060 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1061 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1063 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1064 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1067 #ifdef PROVIDE_IMAGINARY
1068 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1069 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1071 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1072 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1074 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1075 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1079 /* invalid specifier combination, give an error message */
1080 if(type_specifiers == 0) {
1082 parse_warning("no type specifiers in declaration (using int)");
1083 atomic_type = ATOMIC_TYPE_INT;
1086 parse_error("no type specifiers given in declaration");
1088 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1089 (type_specifiers & SPECIFIER_UNSIGNED)) {
1090 parse_error("signed and unsigned specifiers gives");
1091 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1092 parse_error("only integer types can be signed or unsigned");
1094 parse_error("multiple datatypes in declaration");
1096 atomic_type = ATOMIC_TYPE_INVALID;
1099 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1100 atype->type.type = TYPE_ATOMIC;
1101 atype->atype = atomic_type;
1104 type = (type_t*) atype;
1106 if(type_specifiers != 0) {
1107 parse_error("multiple datatypes in declaration");
1111 type->qualifiers = type_qualifiers;
1113 type_t *result = typehash_insert(type);
1114 if(newtype && result != (type_t*) type) {
1118 specifiers->type = result;
1121 static type_qualifier_t parse_type_qualifiers(void)
1123 type_qualifier_t type_qualifiers = 0;
1126 switch(token.type) {
1127 /* type qualifiers */
1128 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1129 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1130 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1131 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1134 return type_qualifiers;
1139 static void parse_identifier_list(void)
1142 if(token.type != T_IDENTIFIER) {
1143 parse_error_expected("problem while parsing parameter identifier "
1144 "list", T_IDENTIFIER, 0);
1148 if(token.type != ',')
1154 static declaration_t *parse_parameter(void)
1156 declaration_specifiers_t specifiers;
1157 memset(&specifiers, 0, sizeof(specifiers));
1159 parse_declaration_specifiers(&specifiers);
1161 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1162 parse_declarator(declaration, specifiers.storage_class,
1163 specifiers.type, 1);
1168 static declaration_t *parse_parameters(method_type_t *type)
1170 if(token.type == T_IDENTIFIER) {
1171 symbol_t *symbol = token.v.symbol;
1172 declaration_t *declaration = symbol->declaration;
1173 if(declaration == NULL
1174 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1176 parse_identifier_list();
1181 if(token.type == ')') {
1182 type->unspecified_parameters = 1;
1185 if(token.type == T_void && look_ahead(1)->type == ')') {
1190 declaration_t *declarations = NULL;
1191 declaration_t *declaration;
1192 declaration_t *last_declaration = NULL;
1193 method_parameter_t *parameter;
1194 method_parameter_t *last_parameter = NULL;
1197 switch(token.type) {
1201 return declarations;
1204 case T___extension__:
1206 declaration = parse_parameter();
1208 parameter = allocate_type_zero(sizeof(parameter[0]));
1209 parameter->type = declaration->type;
1211 if(last_parameter != NULL) {
1212 last_declaration->next = declaration;
1213 last_parameter->next = parameter;
1215 type->parameters = parameter;
1216 declarations = declaration;
1218 last_parameter = parameter;
1219 last_declaration = declaration;
1223 return declarations;
1225 if(token.type != ',')
1226 return declarations;
1235 } construct_type_type_t;
1237 typedef struct construct_type_t construct_type_t;
1238 struct construct_type_t {
1239 construct_type_type_t type;
1240 construct_type_t *next;
1243 typedef struct parsed_pointer_t parsed_pointer_t;
1244 struct parsed_pointer_t {
1245 construct_type_t construct_type;
1246 type_qualifier_t type_qualifiers;
1249 typedef struct construct_method_type_t construct_method_type_t;
1250 struct construct_method_type_t {
1251 construct_type_t construct_type;
1252 method_type_t *method_type;
1255 typedef struct parsed_array_t parsed_array_t;
1256 struct parsed_array_t {
1257 construct_type_t construct_type;
1258 type_qualifier_t type_qualifiers;
1264 typedef struct construct_base_type_t construct_base_type_t;
1265 struct construct_base_type_t {
1266 construct_type_t construct_type;
1270 static construct_type_t *parse_pointer_declarator(void)
1274 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1275 memset(pointer, 0, sizeof(pointer[0]));
1276 pointer->type_qualifiers = parse_type_qualifiers();
1278 return (construct_type_t*) pointer;
1281 static construct_type_t *parse_array_declarator(void)
1285 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1286 memset(array, 0, sizeof(array[0]));
1288 if(token.type == T_static) {
1289 array->is_static = true;
1293 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1294 if(type_qualifiers != 0) {
1295 if(token.type == T_static) {
1296 array->is_static = true;
1300 array->type_qualifiers = type_qualifiers;
1302 if(token.type == '*' && look_ahead(1)->type == ']') {
1303 array->is_variable = true;
1305 } else if(token.type != ']') {
1306 array->size = parse_assignment_expression();
1311 return (construct_type_t*) array;
1314 static construct_type_t *parse_method_declarator(declaration_t *declaration)
1318 method_type_t *method_type
1319 = allocate_type_zero(sizeof(method_type[0]));
1320 method_type->type.type = TYPE_METHOD;
1322 declaration_t *parameters = parse_parameters(method_type);
1323 if(declaration != NULL) {
1324 declaration->context.declarations = parameters;
1327 construct_method_type_t *construct_method_type =
1328 obstack_alloc(&temp_obst, sizeof(construct_method_type[0]));
1329 memset(construct_method_type, 0, sizeof(construct_method_type[0]));
1330 construct_method_type->construct_type.type = CONSTRUCT_METHOD;
1331 construct_method_type->method_type = method_type;
1335 return (construct_type_t*) construct_method_type;
1338 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1339 int may_be_abstract)
1341 construct_type_t *result = NULL;
1342 construct_type_t *last = NULL;
1344 while(token.type == '*') {
1345 construct_type_t *type = parse_pointer_declarator();
1354 /* TODO: find out if this is correct */
1357 construct_type_t *inner_types = NULL;
1359 switch(token.type) {
1361 if(declaration == NULL) {
1362 parse_error("no identifier expected in typename");
1364 declaration->symbol = token.v.symbol;
1365 declaration->source_position = token.source_position;
1371 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1377 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1382 construct_type_t *type;
1383 switch(token.type) {
1385 type = parse_method_declarator(declaration);
1388 type = parse_array_declarator();
1391 goto declarator_finished;
1402 declarator_finished:
1405 if(inner_types != NULL) {
1407 last->next = inner_types;
1409 result = inner_types;
1417 static type_t *construct_declarator_type(construct_type_t *construct_list,
1420 construct_type_t *iter = construct_list;
1421 for( ; iter != NULL; iter = iter->next) {
1422 parsed_pointer_t *parsed_pointer;
1423 parsed_array_t *parsed_array;
1424 construct_method_type_t *construct_method_type;
1425 method_type_t *method_type;
1426 pointer_type_t *pointer_type;
1427 array_type_t *array_type;
1429 switch(iter->type) {
1430 case CONSTRUCT_METHOD:
1431 construct_method_type = (construct_method_type_t*) iter;
1432 method_type = construct_method_type->method_type;
1434 method_type->result_type = type;
1435 type = (type_t*) method_type;
1438 case CONSTRUCT_POINTER:
1439 parsed_pointer = (parsed_pointer_t*) iter;
1440 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1442 pointer_type->type.type = TYPE_POINTER;
1443 pointer_type->points_to = type;
1444 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1445 type = (type_t*) pointer_type;
1448 case CONSTRUCT_ARRAY:
1449 parsed_array = (parsed_array_t*) iter;
1450 array_type = allocate_type_zero(sizeof(array_type[0]));
1452 array_type->type.type = TYPE_ARRAY;
1453 array_type->element_type = type;
1454 array_type->type.qualifiers = parsed_array->type_qualifiers;
1455 array_type->is_static = parsed_array->is_static;
1456 array_type->is_variable = parsed_array->is_variable;
1457 array_type->size = parsed_array->size;
1458 type = (type_t*) array_type;
1462 type_t *hashed_type = typehash_insert((type_t*) type);
1463 if(hashed_type != type) {
1472 static void parse_declarator(declaration_t *declaration,
1473 storage_class_t storage_class, type_t *type,
1474 int may_be_abstract)
1476 construct_type_t *construct_type
1477 = parse_inner_declarator(declaration, may_be_abstract);
1479 declaration->type = construct_declarator_type(construct_type, type);
1480 declaration->storage_class = storage_class;
1481 if(construct_type != NULL) {
1482 obstack_free(&temp_obst, construct_type);
1486 static type_t *parse_abstract_declarator(type_t *base_type)
1488 construct_type_t *construct_type
1489 = parse_inner_declarator(NULL, 1);
1491 if(construct_type == NULL)
1494 type_t *result = construct_declarator_type(construct_type, base_type);
1495 obstack_free(&temp_obst, construct_type);
1500 static declaration_t *record_declaration(declaration_t *declaration)
1505 symbol_t *symbol = declaration->symbol;
1506 if(symbol != NULL) {
1507 declaration_t *alias = environment_push(declaration, context);
1508 if(alias != declaration)
1512 if(last_declaration != NULL) {
1513 last_declaration->next = declaration;
1515 context->declarations = declaration;
1517 last_declaration = declaration;
1522 static void parser_error_multiple_definition(declaration_t *previous,
1523 declaration_t *declaration)
1525 parser_print_error_prefix_pos(declaration->source_position);
1526 fprintf(stderr, "multiple definition of symbol '%s'\n",
1527 declaration->symbol->string);
1528 parser_print_error_prefix_pos(previous->source_position);
1529 fprintf(stderr, "this is the location of the previous "
1534 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1537 declaration_t *ndeclaration
1538 = allocate_ast_zero(sizeof(ndeclaration[0]));
1540 parse_declarator(ndeclaration, specifiers->storage_class,
1541 specifiers->type, 0);
1542 declaration_t *declaration = record_declaration(ndeclaration);
1543 if(token.type == '=') {
1546 /* TODO: check that this is an allowed type (esp. no method type) */
1548 if(declaration->initializer != NULL) {
1549 parser_error_multiple_definition(declaration, ndeclaration);
1552 ndeclaration->initializer = parse_initializer();
1553 } else if(token.type == '{') {
1554 if(declaration->type->type != TYPE_METHOD) {
1555 parser_print_error_prefix();
1556 fprintf(stderr, "Declarator ");
1557 print_type_ext(declaration->type, declaration->symbol, NULL);
1558 fprintf(stderr, " is not a method type.\n");
1561 if(declaration->initializer != NULL) {
1562 parser_error_multiple_definition(declaration, ndeclaration);
1564 if(ndeclaration != declaration) {
1565 memcpy(&declaration->context, &ndeclaration->context,
1566 sizeof(declaration->context));
1569 int top = environment_top();
1570 context_t *last_context = context;
1571 set_context(&declaration->context);
1573 /* push function parameters */
1574 declaration_t *parameter = declaration->context.declarations;
1575 for( ; parameter != NULL; parameter = parameter->next) {
1576 environment_push(parameter, context);
1579 statement_t *statement = parse_compound_statement();
1581 assert(context == &declaration->context);
1582 set_context(last_context);
1583 environment_pop_to(top);
1585 declaration->statement = statement;
1589 if(token.type != ',')
1596 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1599 if(token.type == ':') {
1601 parse_constant_expression();
1602 /* TODO (bitfields) */
1604 declaration_t *declaration
1605 = allocate_ast_zero(sizeof(declaration[0]));
1606 parse_declarator(declaration, specifiers->storage_class,
1607 specifiers->type, 1);
1609 /* TODO: check for doubled fields */
1610 record_declaration(declaration);
1612 if(token.type == ':') {
1614 parse_constant_expression();
1615 /* TODO (bitfields) */
1619 if(token.type != ',')
1626 static void parse_compound_type_entries(void)
1630 while(token.type != '}' && token.type != T_EOF) {
1631 declaration_specifiers_t specifiers;
1632 memset(&specifiers, 0, sizeof(specifiers));
1633 /* TODO not correct as this allows storage class stuff... but only
1634 * specifiers and qualifiers sould be allowed here */
1635 parse_declaration_specifiers(&specifiers);
1637 parse_struct_declarators(&specifiers);
1639 if(token.type == T_EOF) {
1640 parse_error("unexpected error while parsing struct");
1645 static void parse_declaration(void)
1647 declaration_specifiers_t specifiers;
1648 memset(&specifiers, 0, sizeof(specifiers));
1649 parse_declaration_specifiers(&specifiers);
1651 if(token.type == ';') {
1655 parse_init_declarators(&specifiers);
1658 static type_t *parse_typename(void)
1660 declaration_specifiers_t specifiers;
1661 memset(&specifiers, 0, sizeof(specifiers));
1662 parse_declaration_specifiers(&specifiers);
1663 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1664 /* TODO: improve error message, user does probably not know what a
1665 * storage class is...
1667 parse_error("typename may not have a storage class");
1670 type_t *result = parse_abstract_declarator(specifiers.type);
1678 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1679 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1680 expression_t *left);
1682 typedef struct expression_parser_function_t expression_parser_function_t;
1683 struct expression_parser_function_t {
1684 unsigned precedence;
1685 parse_expression_function parser;
1686 unsigned infix_precedence;
1687 parse_expression_infix_function infix_parser;
1690 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1692 static expression_t *expected_expression_error(void)
1694 parser_print_error_prefix();
1695 fprintf(stderr, "expected expression, got token ");
1696 print_token(stderr, & token);
1697 fprintf(stderr, "\n");
1699 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1700 expression->type = EXPR_INVALID;
1706 static expression_t *parse_string_const(void)
1708 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1710 cnst->expression.type = EXPR_STRING_LITERAL;
1711 cnst->expression.datatype = type_string;
1712 cnst->value = parse_string_literals();
1714 return (expression_t*) cnst;
1717 static expression_t *parse_int_const(void)
1719 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1721 cnst->expression.type = EXPR_CONST;
1722 cnst->expression.datatype = type_int;
1723 cnst->v.int_value = token.v.intvalue;
1727 return (expression_t*) cnst;
1730 static expression_t *parse_float_const(void)
1732 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1734 cnst->expression.type = EXPR_CONST;
1735 cnst->expression.datatype = type_double;
1736 cnst->v.float_value = token.v.floatvalue;
1740 return (expression_t*) cnst;
1743 static declaration_t *create_implicit_function(symbol_t *symbol,
1744 const source_position_t source_position)
1746 method_type_t *method_type = allocate_type_zero(sizeof(method_type));
1748 method_type->type.type = TYPE_METHOD;
1749 method_type->result_type = type_int;
1750 method_type->unspecified_parameters = true;
1752 type_t *type = typehash_insert((type_t*) method_type);
1753 if(type != (type_t*) method_type) {
1754 free_type(method_type);
1757 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1759 declaration->storage_class = STORAGE_CLASS_EXTERN;
1760 declaration->type = type;
1761 declaration->symbol = symbol;
1762 declaration->source_position = source_position;
1764 /* we have to violate the environment push/pop rules here and assign
1765 * the declaration directly to its symbol and append it to the
1767 assert(symbol->declaration == NULL);
1768 symbol->declaration = declaration;
1769 declaration->next = global_context->declarations;
1770 global_context->declarations = declaration;
1775 static expression_t *parse_reference(void)
1777 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1779 ref->expression.type = EXPR_REFERENCE;
1780 ref->symbol = token.v.symbol;
1782 declaration_t *declaration = ref->symbol->declaration;
1783 source_position_t source_position = token.source_position;
1786 if(declaration == NULL) {
1788 /* an implicitely defined function */
1789 if(token.type == '(') {
1790 parser_print_prefix_pos(token.source_position);
1791 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1792 ref->symbol->string);
1794 declaration = create_implicit_function(ref->symbol,
1799 parser_print_error_prefix();
1800 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1801 return (expression_t*) ref;
1805 ref->declaration = declaration;
1806 ref->expression.datatype = declaration->type;
1808 return (expression_t*) ref;
1811 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1815 /* TODO check if cast is allowed and issue warnings/errors */
1818 static expression_t *parse_cast(void)
1820 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1822 cast->expression.type = EXPR_UNARY;
1823 cast->type = UNEXPR_CAST;
1824 cast->expression.source_position = token.source_position;
1826 type_t *type = parse_typename();
1829 expression_t *value = parse_sub_expression(20);
1831 check_cast_allowed(value, type);
1833 cast->expression.datatype = type;
1834 cast->value = value;
1836 return (expression_t*) cast;
1839 static expression_t *parse_statement_expression(void)
1841 statement_expression_t *expression
1842 = allocate_ast_zero(sizeof(expression[0]));
1843 expression->expression.type = EXPR_STATEMENT;
1844 expression->statement = parse_compound_statement();
1846 /* find last statement and use it's type */
1847 const statement_t *last_statement = NULL;
1848 const statement_t *statement = expression->statement;
1849 for( ; statement != NULL; statement = statement->next) {
1850 last_statement = statement;
1853 if(last_statement->type == STATEMENT_EXPRESSION) {
1854 const expression_statement_t *expression_statement =
1855 (const expression_statement_t*) last_statement;
1856 expression->expression.datatype
1857 = expression_statement->expression->datatype;
1859 expression->expression.datatype = type_void;
1864 return (expression_t*) expression;
1867 static expression_t *parse_brace_expression(void)
1871 declaration_t *declaration;
1872 switch(token.type) {
1874 /* gcc extension: a stement expression */
1875 return parse_statement_expression();
1879 return parse_cast();
1881 declaration = token.v.symbol->declaration;
1882 if(declaration != NULL &&
1883 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1884 return parse_cast();
1888 expression_t *result = parse_expression();
1894 static expression_t *parse_function_keyword(void)
1896 eat(T___FUNCTION__);
1899 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1900 expression->expression.type = EXPR_FUNCTION;
1901 expression->expression.datatype = type_string;
1902 expression->value = "TODO: FUNCTION";
1904 return (expression_t*) expression;
1907 static expression_t *parse_pretty_function_keyword(void)
1909 eat(T___PRETTY_FUNCTION__);
1912 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1913 expression->expression.type = EXPR_PRETTY_FUNCTION;
1914 expression->expression.datatype = type_string;
1915 expression->value = "TODO: PRETTY FUNCTION";
1917 return (expression_t*) expression;
1920 static designator_t *parse_designator(void)
1922 designator_t *result = allocate_ast_zero(sizeof(result[0]));
1924 if(token.type != T_IDENTIFIER) {
1925 parse_error_expected("problem while parsing member designator",
1930 result->symbol = token.v.symbol;
1933 designator_t *last_designator = result;
1935 if(token.type == '.') {
1937 if(token.type != T_IDENTIFIER) {
1938 parse_error_expected("problem while parsing member designator",
1943 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
1944 designator->symbol = token.v.symbol;
1947 last_designator->next = designator;
1948 last_designator = designator;
1951 if(token.type == '[') {
1953 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
1954 designator->array_access = parse_expression();
1955 if(designator->array_access == NULL) {
1961 last_designator->next = designator;
1962 last_designator = designator;
1971 static expression_t *parse_offsetof(void)
1973 eat(T___builtin_offsetof);
1975 offsetof_expression_t *expression
1976 = allocate_ast_zero(sizeof(expression[0]));
1977 expression->expression.type = EXPR_OFFSETOF;
1978 expression->expression.datatype = type_size_t;
1981 expression->type = parse_typename();
1983 expression->designator = parse_designator();
1986 return (expression_t*) expression;
1989 static expression_t *parse_va_arg(void)
1991 eat(T___builtin_va_arg);
1993 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1994 expression->expression.type = EXPR_VA_ARG;
1997 expression->arg = parse_assignment_expression();
1999 expression->expression.datatype = parse_typename();
2002 return (expression_t*) expression;
2005 static expression_t *parse_builtin_symbol(void)
2007 builtin_symbol_expression_t *expression
2008 = allocate_ast_zero(sizeof(expression[0]));
2009 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2011 /* TODO: set datatype */
2013 expression->symbol = token.v.symbol;
2017 return (expression_t*) expression;
2020 static expression_t *parse_primary_expression(void)
2022 switch(token.type) {
2024 return parse_int_const();
2025 case T_FLOATINGPOINT:
2026 return parse_float_const();
2027 case T_STRING_LITERAL:
2028 return parse_string_const();
2030 return parse_reference();
2031 case T___FUNCTION__:
2032 return parse_function_keyword();
2033 case T___PRETTY_FUNCTION__:
2034 return parse_pretty_function_keyword();
2035 case T___builtin_offsetof:
2036 return parse_offsetof();
2037 case T___builtin_va_arg:
2038 return parse_va_arg();
2039 case T___builtin_expect:
2040 case T___builtin_va_start:
2041 case T___builtin_va_end:
2042 return parse_builtin_symbol();
2045 return parse_brace_expression();
2048 parser_print_error_prefix();
2049 fprintf(stderr, "unexpected token ");
2050 print_token(stderr, &token);
2051 fprintf(stderr, "\n");
2054 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2055 expression->type = EXPR_INVALID;
2056 expression->datatype = type_void;
2061 static expression_t *parse_array_expression(unsigned precedence,
2062 expression_t *array_ref)
2068 array_access_expression_t *array_access
2069 = allocate_ast_zero(sizeof(array_access[0]));
2071 array_access->expression.type = EXPR_ARRAY_ACCESS;
2072 array_access->array_ref = array_ref;
2073 array_access->index = parse_expression();
2075 type_t *array_type = array_ref->datatype;
2076 if(array_type != NULL) {
2077 if(array_type->type == TYPE_POINTER) {
2078 pointer_type_t *pointer = (pointer_type_t*) array_type;
2079 array_access->expression.datatype = pointer->points_to;
2081 parser_print_error_prefix();
2082 fprintf(stderr, "array access on object with non-pointer type ");
2083 print_type(array_type);
2084 fprintf(stderr, "\n");
2088 if(token.type != ']') {
2089 parse_error_expected("Problem while parsing array access", ']', 0);
2090 return (expression_t*) array_access;
2094 return (expression_t*) array_access;
2097 static bool is_declaration_specifier(const token_t *token,
2098 bool only_type_specifiers)
2100 declaration_t *declaration;
2102 switch(token->type) {
2106 declaration = token->v.symbol->declaration;
2107 if(declaration == NULL)
2109 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2114 if(only_type_specifiers)
2123 static expression_t *parse_sizeof(unsigned precedence)
2127 sizeof_expression_t *sizeof_expression
2128 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2129 sizeof_expression->expression.type = EXPR_SIZEOF;
2130 sizeof_expression->expression.datatype = type_size_t;
2132 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2134 sizeof_expression->type = parse_typename();
2137 expression_t *expression = parse_sub_expression(precedence);
2138 sizeof_expression->type = expression->datatype;
2139 sizeof_expression->size_expression = expression;
2142 return (expression_t*) sizeof_expression;
2145 static expression_t *parse_select_expression(unsigned precedence,
2146 expression_t *compound)
2150 assert(token.type == '.' || token.type == T_MINUSGREATER);
2153 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2155 select->expression.type = EXPR_SELECT;
2156 select->compound = compound;
2158 /* TODO: datatype */
2160 if(token.type != T_IDENTIFIER) {
2161 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2162 return (expression_t*) select;
2164 select->symbol = token.v.symbol;
2167 return (expression_t*) select;
2170 static expression_t *parse_call_expression(unsigned precedence,
2171 expression_t *expression)
2174 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2176 call->expression.type = EXPR_CALL;
2177 call->method = expression;
2179 /* parse arguments */
2182 if(token.type != ')') {
2183 call_argument_t *last_argument = NULL;
2186 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2188 argument->expression = parse_assignment_expression();
2189 if(last_argument == NULL) {
2190 call->arguments = argument;
2192 last_argument->next = argument;
2194 last_argument = argument;
2196 if(token.type != ',')
2203 type_t *type = expression->datatype;
2205 /* we can call pointer to function */
2206 if(type->type == TYPE_POINTER) {
2207 pointer_type_t *pointer = (pointer_type_t*) type;
2208 type = pointer->points_to;
2211 if(type == NULL || type->type != TYPE_METHOD) {
2212 parser_print_error_prefix();
2213 fprintf(stderr, "expected a method type for call but found type ");
2214 print_type(expression->datatype);
2215 fprintf(stderr, "\n");
2217 method_type_t *method_type = (method_type_t*) type;
2218 call->expression.datatype = method_type->result_type;
2222 return (expression_t*) call;
2225 static void type_error(const char *msg, const source_position_t source_position,
2228 parser_print_error_prefix_pos(source_position);
2229 fprintf(stderr, "%s, but found type ", msg);
2231 fputc('\n', stderr);
2235 static void type_error_incompatible(const char *msg,
2236 const source_position_t source_position, const type_t *type1,
2237 const type_t *type2)
2239 parser_print_error_prefix_pos(source_position);
2240 fprintf(stderr, "%s, incompatible types: ", msg);
2242 fprintf(stderr, " - ");
2244 fprintf(stderr, ")\n");
2248 static type_t *get_type_after_conversion(const type_t *type1,
2249 const type_t *type2)
2253 return (type_t*) type1;
2256 static expression_t *parse_conditional_expression(unsigned precedence,
2257 expression_t *expression)
2261 conditional_expression_t *conditional
2262 = allocate_ast_zero(sizeof(conditional[0]));
2263 conditional->expression.type = EXPR_CONDITIONAL;
2264 conditional->condition = expression;
2267 type_t *condition_type = conditional->condition->datatype;
2268 if(condition_type != NULL) {
2269 if(!is_type_scalar(condition_type)) {
2270 type_error("expected a scalar type", expression->source_position,
2275 conditional->true_expression = parse_expression();
2277 conditional->false_expression = parse_sub_expression(precedence);
2279 type_t *true_type = conditional->true_expression->datatype;
2280 if(true_type == NULL)
2281 return (expression_t*) conditional;
2282 type_t *false_type = conditional->false_expression->datatype;
2283 if(false_type == NULL)
2284 return (expression_t*) conditional;
2287 if(true_type == false_type) {
2288 conditional->expression.datatype = true_type;
2289 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2290 type_t *result = get_type_after_conversion(true_type, false_type);
2291 /* TODO: create implicit convs if necessary */
2292 conditional->expression.datatype = result;
2293 } else if(true_type->type == TYPE_POINTER &&
2294 false_type->type == TYPE_POINTER &&
2295 true /* TODO compatible points_to types */) {
2297 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2298 || (is_null_ptr_const(false_type) &&
2299 true_type->type == TYPE_POINTER) TODO*/ false) {
2301 } else if(/* 1 is pointer to object type, other is void* */ false) {
2304 type_error_incompatible("problem while parsing conditional",
2305 expression->source_position, true_type,
2309 return (expression_t*) conditional;
2312 static expression_t *parse_extension(unsigned precedence)
2314 eat(T___extension__);
2316 /* TODO enable extensions */
2318 return parse_sub_expression(precedence);
2321 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
2323 expression_t *parse_##unexpression_type(unsigned precedence) \
2327 unary_expression_t *unary_expression \
2328 = allocate_ast_zero(sizeof(unary_expression[0])); \
2329 unary_expression->expression.type = EXPR_UNARY; \
2330 unary_expression->type = unexpression_type; \
2331 unary_expression->value = parse_sub_expression(precedence); \
2333 return (expression_t*) unary_expression; \
2336 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
2337 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
2338 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
2339 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
2340 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
2341 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
2342 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
2343 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
2345 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
2347 expression_t *parse_##unexpression_type(unsigned precedence, \
2348 expression_t *left) \
2350 (void) precedence; \
2353 unary_expression_t *unary_expression \
2354 = allocate_ast_zero(sizeof(unary_expression[0])); \
2355 unary_expression->expression.type = EXPR_UNARY; \
2356 unary_expression->type = unexpression_type; \
2357 unary_expression->value = left; \
2359 return (expression_t*) unary_expression; \
2362 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
2363 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
2365 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
2367 expression_t *parse_##binexpression_type(unsigned precedence, \
2368 expression_t *left) \
2372 expression_t *right = parse_sub_expression(precedence); \
2374 binary_expression_t *binexpr \
2375 = allocate_ast_zero(sizeof(binexpr[0])); \
2376 binexpr->expression.type = EXPR_BINARY; \
2377 binexpr->type = binexpression_type; \
2378 binexpr->left = left; \
2379 binexpr->right = right; \
2381 return (expression_t*) binexpr; \
2384 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
2385 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
2386 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
2387 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
2388 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
2389 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
2390 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
2391 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
2392 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
2393 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
2394 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
2395 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
2396 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
2397 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
2398 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
2399 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
2400 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
2401 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
2402 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
2403 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
2404 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
2405 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
2406 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
2407 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
2408 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
2409 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
2410 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
2411 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
2412 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
2413 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
2415 static expression_t *parse_sub_expression(unsigned precedence)
2417 if(token.type < 0) {
2418 return expected_expression_error();
2421 expression_parser_function_t *parser
2422 = &expression_parsers[token.type];
2423 source_position_t source_position = token.source_position;
2426 if(parser->parser != NULL) {
2427 left = parser->parser(parser->precedence);
2429 left = parse_primary_expression();
2431 assert(left != NULL);
2432 left->source_position = source_position;
2435 if(token.type < 0) {
2436 return expected_expression_error();
2439 parser = &expression_parsers[token.type];
2440 if(parser->infix_parser == NULL)
2442 if(parser->infix_precedence < precedence)
2445 left = parser->infix_parser(parser->infix_precedence, left);
2447 assert(left != NULL);
2448 assert(left->type != EXPR_INVALID);
2449 left->source_position = source_position;
2455 static expression_t *parse_expression(void)
2457 return parse_sub_expression(1);
2462 void register_expression_parser(parse_expression_function parser,
2463 int token_type, unsigned precedence)
2465 expression_parser_function_t *entry = &expression_parsers[token_type];
2467 if(entry->parser != NULL) {
2468 fprintf(stderr, "for token ");
2469 print_token_type(stderr, token_type);
2470 fprintf(stderr, "\n");
2471 panic("trying to register multiple expression parsers for a token");
2473 entry->parser = parser;
2474 entry->precedence = precedence;
2477 void register_expression_infix_parser(parse_expression_infix_function parser,
2478 int token_type, unsigned precedence)
2480 expression_parser_function_t *entry = &expression_parsers[token_type];
2482 if(entry->infix_parser != NULL) {
2483 fprintf(stderr, "for token ");
2484 print_token_type(stderr, token_type);
2485 fprintf(stderr, "\n");
2486 panic("trying to register multiple infix expression parsers for a "
2489 entry->infix_parser = parser;
2490 entry->infix_precedence = precedence;
2493 static void init_expression_parsers(void)
2495 memset(&expression_parsers, 0, sizeof(expression_parsers));
2497 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2498 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2499 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2500 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2501 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2502 T_GREATERGREATER, 16);
2503 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2504 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2505 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2506 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2507 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2508 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2509 T_GREATEREQUAL, 14);
2510 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2511 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2512 T_EXCLAMATIONMARKEQUAL, 13);
2513 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2514 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2515 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2516 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2517 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2518 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2519 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2520 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2521 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2522 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2523 T_ASTERISKEQUAL, 2);
2524 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2525 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2527 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2528 T_LESSLESSEQUAL, 2);
2529 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2530 T_GREATERGREATEREQUAL, 2);
2531 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2533 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2535 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2538 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2540 register_expression_infix_parser(parse_array_expression, '[', 30);
2541 register_expression_infix_parser(parse_call_expression, '(', 30);
2542 register_expression_infix_parser(parse_select_expression, '.', 30);
2543 register_expression_infix_parser(parse_select_expression,
2544 T_MINUSGREATER, 30);
2545 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2547 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2550 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2551 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2552 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2553 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2554 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2555 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2556 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2557 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2558 register_expression_parser(parse_sizeof, T_sizeof, 25);
2559 register_expression_parser(parse_extension, T___extension__, 25);
2563 static statement_t *parse_case_statement(void)
2566 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2567 label->statement.type = STATEMENT_CASE_LABEL;
2568 label->statement.source_position = token.source_position;
2570 label->expression = parse_expression();
2573 label->statement.next = parse_statement();
2575 return (statement_t*) label;
2578 static statement_t *parse_default_statement(void)
2582 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2583 label->statement.type = STATEMENT_CASE_LABEL;
2584 label->statement.source_position = token.source_position;
2587 label->statement.next = parse_statement();
2589 return (statement_t*) label;
2592 static statement_t *parse_label_statement(void)
2601 static statement_t *parse_if(void)
2605 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2606 statement->statement.type = STATEMENT_IF;
2607 statement->statement.source_position = token.source_position;
2610 statement->condition = parse_expression();
2613 statement->true_statement = parse_statement();
2614 if(token.type == T_else) {
2616 statement->false_statement = parse_statement();
2619 return (statement_t*) statement;
2622 static statement_t *parse_switch(void)
2626 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2627 statement->statement.type = STATEMENT_SWITCH;
2628 statement->statement.source_position = token.source_position;
2631 statement->expression = parse_expression();
2633 statement->body = parse_statement();
2635 return (statement_t*) statement;
2638 static statement_t *parse_while(void)
2642 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2643 statement->statement.type = STATEMENT_WHILE;
2644 statement->statement.source_position = token.source_position;
2647 statement->condition = parse_expression();
2649 statement->body = parse_statement();
2651 return (statement_t*) statement;
2654 static statement_t *parse_do(void)
2658 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2659 statement->statement.type = STATEMENT_DO_WHILE;
2660 statement->statement.source_position = token.source_position;
2662 statement->body = parse_statement();
2665 statement->condition = parse_expression();
2669 return (statement_t*) statement;
2672 static statement_t *parse_for(void)
2676 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2677 statement->statement.type = STATEMENT_FOR;
2678 statement->statement.source_position = token.source_position;
2682 int top = environment_top();
2683 context_t *last_context = context;
2684 set_context(&statement->context);
2686 if(token.type != ';') {
2687 if(is_declaration_specifier(&token, false)) {
2688 parse_declaration();
2690 statement->initialisation = parse_expression();
2697 if(token.type != ';') {
2698 statement->condition = parse_expression();
2701 if(token.type != ')') {
2702 statement->step = parse_expression();
2705 statement->body = parse_statement();
2707 assert(context == &statement->context);
2708 set_context(last_context);
2709 environment_pop_to(top);
2711 return (statement_t*) statement;
2714 static statement_t *parse_goto(void)
2717 expect(T_IDENTIFIER);
2723 static statement_t *parse_continue(void)
2728 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2729 statement->source_position = token.source_position;
2730 statement->type = STATEMENT_CONTINUE;
2735 static statement_t *parse_break(void)
2740 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2741 statement->source_position = token.source_position;
2742 statement->type = STATEMENT_BREAK;
2747 static statement_t *parse_return(void)
2751 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2753 statement->statement.type = STATEMENT_RETURN;
2754 statement->statement.source_position = token.source_position;
2755 if(token.type != ';') {
2756 statement->return_value = parse_expression();
2760 return (statement_t*) statement;
2763 static statement_t *parse_declaration_statement(void)
2765 declaration_t *before = last_declaration;
2767 declaration_statement_t *statement
2768 = allocate_ast_zero(sizeof(statement[0]));
2769 statement->statement.type = STATEMENT_DECLARATION;
2770 statement->statement.source_position = token.source_position;
2772 declaration_specifiers_t specifiers;
2773 memset(&specifiers, 0, sizeof(specifiers));
2774 parse_declaration_specifiers(&specifiers);
2776 if(token.type == ';') {
2779 parse_init_declarators(&specifiers);
2782 if(before == NULL) {
2783 statement->declarations_begin = context->declarations;
2785 statement->declarations_begin = before->next;
2787 statement->declarations_end = last_declaration;
2789 return (statement_t*) statement;
2792 static statement_t *parse_expression_statement(void)
2794 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2795 statement->statement.type = STATEMENT_EXPRESSION;
2796 statement->statement.source_position = token.source_position;
2798 statement->expression = parse_expression();
2802 return (statement_t*) statement;
2805 static statement_t *parse_statement(void)
2807 declaration_t *declaration;
2808 statement_t *statement = NULL;
2810 /* declaration or statement */
2811 switch(token.type) {
2813 statement = parse_case_statement();
2817 statement = parse_default_statement();
2821 statement = parse_compound_statement();
2825 statement = parse_if();
2829 statement = parse_switch();
2833 statement = parse_while();
2837 statement = parse_do();
2841 statement = parse_for();
2845 statement = parse_goto();
2849 statement = parse_continue();
2853 statement = parse_break();
2857 statement = parse_return();
2866 if(look_ahead(1)->type == ':') {
2867 statement = parse_label_statement();
2871 declaration = token.v.symbol->declaration;
2872 if(declaration != NULL &&
2873 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2874 statement = parse_declaration_statement();
2878 statement = parse_expression_statement();
2881 case T___extension__:
2882 /* this can be a prefix to a declaration or an expression statement */
2883 /* we simply eat it now and parse the rest with tail recursion */
2886 } while(token.type == T___extension__);
2887 statement = parse_statement();
2891 statement = parse_declaration_statement();
2895 statement = parse_expression_statement();
2899 assert(statement == NULL || statement->source_position.input_name != NULL);
2904 static statement_t *parse_compound_statement(void)
2908 compound_statement_t *compound_statement
2909 = allocate_ast_zero(sizeof(compound_statement[0]));
2910 compound_statement->statement.type = STATEMENT_COMPOUND;
2911 compound_statement->statement.source_position = token.source_position;
2913 int top = environment_top();
2914 context_t *last_context = context;
2915 set_context(&compound_statement->context);
2917 statement_t *last_statement = NULL;
2919 while(token.type != '}' && token.type != T_EOF) {
2920 statement_t *statement = parse_statement();
2921 if(statement == NULL)
2924 if(last_statement != NULL) {
2925 last_statement->next = statement;
2927 compound_statement->statements = statement;
2930 while(statement->next != NULL)
2931 statement = statement->next;
2933 last_statement = statement;
2936 assert(context == &compound_statement->context);
2937 set_context(last_context);
2938 environment_pop_to(top);
2942 return (statement_t*) compound_statement;
2945 static translation_unit_t *parse_translation_unit(void)
2947 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2949 assert(global_context == NULL);
2950 global_context = &unit->context;
2952 assert(context == NULL);
2953 set_context(&unit->context);
2955 while(token.type != T_EOF) {
2956 parse_declaration();
2959 assert(context == &unit->context);
2961 last_declaration = NULL;
2963 assert(global_context == &unit->context);
2964 global_context = NULL;
2969 translation_unit_t *parse(void)
2971 obstack_init(&environment_obstack);
2972 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2974 type_set_output(stderr);
2976 lookahead_bufpos = 0;
2977 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2980 translation_unit_t *unit = parse_translation_unit();
2982 DEL_ARR_F(environment_stack);
2983 obstack_free(&environment_obstack, NULL);
2988 void init_parser(void)
2990 init_expression_parsers();
2991 obstack_init(&temp_obst);
2993 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
2994 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
2995 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
2996 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
2997 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
2998 type_string = make_pointer_type(type_const_char, 0);
3001 void exit_parser(void)
3003 obstack_free(&temp_obst, NULL);