10 #include "type_hash.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
16 //#define PRINT_TOKENS
17 //#define ABORT_ON_ERROR
18 #define MAX_LOOKAHEAD 2
20 struct environment_entry_t {
22 declaration_t *old_declaration;
23 const void *old_context;
27 static token_t lookahead_buffer[MAX_LOOKAHEAD];
28 static int lookahead_bufpos;
29 static struct obstack environment_obstack;
30 static environment_entry_t **environment_stack = NULL;
31 static context_t *context = NULL;
32 static declaration_t *last_declaration = NULL;
33 static struct obstack temp_obst;
36 statement_t *parse_compound_statement(void);
38 statement_t *parse_statement(void);
41 expression_t *parse_sub_expression(unsigned precedence);
43 expression_t *parse_expression(void);
46 void *allocate_ast_zero(size_t size)
48 void *res = allocate_ast(size);
54 void *allocate_type_zero(size_t size)
56 void *res = obstack_alloc(type_obst, size);
62 * returns the top element of the environment stack
65 size_t environment_top(void)
67 return ARR_LEN(environment_stack);
75 token = lookahead_buffer[lookahead_bufpos];
76 lookahead_buffer[lookahead_bufpos] = lexer_token;
79 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
82 print_token(stderr, &token);
83 fprintf(stderr, "\n");
88 const token_t *look_ahead(int num)
90 assert(num > 0 && num <= MAX_LOOKAHEAD);
91 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
92 return & lookahead_buffer[pos];
96 void eat(token_type_t type)
98 assert(token.type == type);
102 void parser_print_error_prefix_pos(const source_position_t source_position)
104 fputs(source_position.input_name, stderr);
106 fprintf(stderr, "%d", source_position.linenr);
107 fputs(": error: ", stderr);
108 #ifdef ABORT_ON_ERROR
113 void parser_print_error_prefix(void)
115 parser_print_error_prefix_pos(token.source_position);
119 void parse_error(const char *message)
121 parser_print_error_prefix();
122 fprintf(stderr, "parse error: %s\n", message);
126 void parse_error_expected(const char *message, ...)
131 if(message != NULL) {
132 parser_print_error_prefix();
133 fprintf(stderr, "%s\n", message);
135 parser_print_error_prefix();
136 fputs("Parse error: got ", stderr);
137 print_token(stderr, &token);
138 fputs(", expected ", stderr);
140 va_start(args, message);
141 token_type_t token_type = va_arg(args, token_type_t);
142 while(token_type != 0) {
146 fprintf(stderr, ", ");
148 print_token_type(stderr, token_type);
149 token_type = va_arg(args, token_type_t);
152 fprintf(stderr, "\n");
155 static void eat_block(void)
157 if(token.type == '{')
160 while(token.type != '}') {
161 if(token.type == T_EOF)
163 if(token.type == '{') {
172 static void eat_statement(void)
174 while(token.type != ';') {
175 if(token.type == T_EOF)
177 if(token.type == '}')
179 if(token.type == '{') {
188 static void eat_brace(void)
190 if(token.type == '(')
193 while(token.type != ')') {
194 if(token.type == T_EOF)
196 if(token.type == '{') {
205 #define expect(expected) \
206 if(UNLIKELY(token.type != (expected))) { \
207 parse_error_expected(NULL, (expected), 0); \
213 #define expect_void(expected) \
214 if(UNLIKELY(token.type != (expected))) { \
215 parse_error_expected(NULL, (expected), 0); \
221 static void set_context(context_t *new_context)
223 context = new_context;
225 declaration_t *declaration = new_context->declarations;
226 if(declaration != NULL) {
228 if(declaration->next == NULL)
230 declaration = declaration->next;
234 last_declaration = declaration;
238 * called when we find a 2nd declarator for an identifier we already have a
241 static int is_compatible_declaration (declaration_t *declaration,
242 declaration_t *previous)
244 /* TODO: not correct yet */
245 return declaration->type == previous->type;
249 * pushs an environment_entry on the environment stack and links the
250 * corresponding symbol to the new entry
253 declaration_t *environment_push(declaration_t *declaration, const void *context)
255 environment_entry_t *entry
256 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
257 memset(entry, 0, sizeof(entry[0]));
259 int top = ARR_LEN(environment_stack);
260 ARR_RESIZE(environment_stack, top + 1);
261 environment_stack[top] = entry;
263 assert(declaration->source_position.input_name != NULL);
265 symbol_t *symbol = declaration->symbol;
266 assert(declaration != symbol->declaration);
268 if(symbol->context == context) {
269 declaration_t *previous_declaration = symbol->declaration;
270 if(symbol->declaration != NULL) {
271 if(!is_compatible_declaration(declaration, previous_declaration)) {
272 parser_print_error_prefix_pos(declaration->source_position);
273 fprintf(stderr, "definition of symbol '%s' with type ",
274 declaration->symbol->string);
275 print_type(declaration->type, NULL);
277 parser_print_error_prefix_pos(
278 previous_declaration->source_position);
279 fprintf(stderr, "is incompatible with previous declaration "
281 print_type(previous_declaration->type, NULL);
284 return previous_declaration;
288 entry->old_declaration = symbol->declaration;
289 entry->old_context = symbol->context;
290 entry->symbol = symbol;
291 symbol->declaration = declaration;
292 symbol->context = context;
298 * pops symbols from the environment stack until @p new_top is the top element
301 void environment_pop_to(size_t new_top)
303 environment_entry_t *entry = NULL;
304 size_t top = ARR_LEN(environment_stack);
310 assert(new_top < top);
313 entry = environment_stack[i - 1];
315 symbol_t *symbol = entry->symbol;
317 symbol->declaration = entry->old_declaration;
318 symbol->context = entry->old_context;
321 } while(i != new_top);
322 obstack_free(&environment_obstack, entry);
324 ARR_SHRINKLEN(environment_stack, (int) new_top);
329 static expression_t *parse_constant_expression(void)
331 /* TODO: not correct yet */
332 return parse_expression();
335 static expression_t *parse_assignment_expression(void)
337 /* TODO: not correct yet */
338 return parse_expression();
341 static void parse_compound_type_entries(void);
342 static void parse_declarator(declaration_t *declaration,
343 storage_class_t storage_class, type_t *type,
344 int may_be_abstract);
345 static declaration_t *record_declaration(declaration_t *declaration);
347 typedef struct declaration_specifiers_t declaration_specifiers_t;
348 struct declaration_specifiers_t {
349 storage_class_t storage_class;
353 static compound_type_t *find_compound_type(compound_type_t *types,
354 const symbol_t *symbol)
356 compound_type_t *type = types;
357 for( ; type != NULL; type = type->next) {
358 if(type->symbol == symbol)
365 static type_t *parse_compound_type_specifier(int is_struct)
373 symbol_t *symbol = NULL;
374 compound_type_t *compound_type = NULL;
376 if(token.type == T_IDENTIFIER) {
377 symbol = token.v.symbol;
380 if(context != NULL) {
382 compound_type = find_compound_type(context->structs, symbol);
384 compound_type = find_compound_type(context->unions, symbol);
387 } else if(token.type != '{') {
389 parse_error_expected("problem while parsing struct type specifiers",
390 T_IDENTIFIER, '{', 0);
392 parse_error_expected("problem while parsing union type specifiers",
393 T_IDENTIFIER, '{', 0);
399 if(compound_type == NULL) {
400 compound_type = allocate_type_zero(sizeof(compound_type[0]));
403 compound_type->type.type = TYPE_COMPOUND_STRUCT;
405 compound_type->type.type = TYPE_COMPOUND_UNION;
407 compound_type->source_position = token.source_position;
408 compound_type->symbol = symbol;
411 if(token.type == '{') {
412 if(compound_type->defined) {
413 parser_print_error_prefix();
414 fprintf(stderr, "multiple definition of %s %s\n",
415 is_struct ? "struct" : "union", symbol->string);
416 compound_type->context.declarations = NULL;
418 compound_type->defined = 1;
420 int top = environment_top();
421 context_t *last_context = context;
422 set_context(&compound_type->context);
424 parse_compound_type_entries();
426 assert(context == &compound_type->context);
427 set_context(last_context);
428 environment_pop_to(top);
431 return (type_t*) compound_type;
434 static enum_entry_t *parse_enum_type_entries(void)
438 if(token.type == '}') {
440 parse_error("empty enum not allowed");
444 enum_entry_t *result = NULL;
445 enum_entry_t *last_entry = NULL;
447 enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
448 if(token.type != T_IDENTIFIER) {
449 parse_error_expected("problem while parsing enum entry",
454 entry->symbol = token.v.symbol;
457 if(token.type == '=') {
459 entry->value = parse_constant_expression();
462 if(last_entry != NULL) {
463 last_entry->next = entry;
469 if(token.type != ',')
472 } while(token.type != '}');
478 static type_t *parse_enum_specifier(void)
482 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
483 enum_type->type.type = TYPE_ENUM;
484 enum_type->source_position = token.source_position;
486 /* TODO: rewrite to the same style as struct/union above to handle
487 * type identities correctly
490 if(token.type == T_IDENTIFIER) {
491 enum_type->symbol = token.v.symbol;
493 if(token.type == '{') {
494 enum_type->entries = parse_enum_type_entries();
496 } else if(token.type == '{') {
497 enum_type->entries = parse_enum_type_entries();
499 parse_error_expected("problem while parsing enum type specifiers",
503 return (type_t*) enum_type;
507 const char *parse_string_literals(void)
509 assert(token.type == T_STRING_LITERAL);
510 const char *result = token.v.string;
514 while(token.type == T_STRING_LITERAL) {
515 result = concat_strings(result, token.v.string);
523 void parse_attributes(void)
527 case T___attribute__:
535 parse_error("EOF while parsing attribute");
553 if(token.type != T_STRING_LITERAL) {
554 parse_error_expected("while parsing assembler attribute",
559 parse_string_literals();
564 goto attributes_finished;
573 SPECIFIER_SIGNED = 1 << 0,
574 SPECIFIER_UNSIGNED = 1 << 1,
575 SPECIFIER_LONG = 1 << 2,
576 SPECIFIER_INT = 1 << 3,
577 SPECIFIER_DOUBLE = 1 << 4,
578 SPECIFIER_CHAR = 1 << 5,
579 SPECIFIER_SHORT = 1 << 6,
580 SPECIFIER_LONG_LONG = 1 << 7,
581 SPECIFIER_FLOAT = 1 << 8,
582 SPECIFIER_BOOL = 1 << 9,
583 SPECIFIER_VOID = 1 << 10,
584 #ifdef PROVIDE_COMPLEX
585 SPECIFIER_COMPLEX = 1 << 11,
587 #ifdef PROVIDE_IMAGINARY
588 SPECIFIER_IMAGINARY = 1 << 12,
592 #define STORAGE_CLASSES \
599 #define TYPE_QUALIFIERS \
604 case T___extension__:
606 #ifdef PROVIDE_COMPLEX
607 #define COMPLEX_SPECIFIERS \
610 #define COMPLEX_SPECIFIERS
613 #ifdef PROVIDE_IMAGINARY
614 #define IMAGINARY_SPECIFIERS \
617 #define IMAGINARY_SPECIFIERS
620 #define TYPE_SPECIFIERS \
637 #define DECLARATION_START \
643 type_t *create_builtin_type(symbol_t *symbol)
645 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
646 type->type.type = TYPE_BUILTIN;
647 type->symbol = symbol;
649 type_t *result = typehash_insert((type_t*) type);
650 if(result != (type_t*) type) {
651 obstack_free(type_obst, type);
658 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
660 declaration_t *declaration;
662 unsigned type_qualifiers = 0;
663 unsigned type_specifiers = 0;
670 #define MATCH_STORAGE_CLASS(token, class) \
672 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
673 parse_error("multiple storage classes in declaration " \
676 specifiers->storage_class = class; \
680 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
681 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
682 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
683 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
684 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
686 /* type qualifiers */
687 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
689 type_qualifiers |= qualifier; \
693 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
694 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
695 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
696 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
698 case T___extension__:
703 /* type specifiers */
704 #define MATCH_SPECIFIER(token, specifier, name) \
707 if(type_specifiers & specifier) { \
708 parse_error("multiple " name " type specifiers given"); \
710 type_specifiers |= specifier; \
714 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
715 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
716 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
717 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
718 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
719 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
720 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
721 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
722 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
723 #ifdef PROVIDE_COMPLEX
724 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
726 #ifdef PROVIDE_IMAGINARY
727 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
731 if(type_specifiers & SPECIFIER_LONG_LONG) {
732 parse_error("multiple type specifiers given");
733 } else if(type_specifiers & SPECIFIER_LONG) {
734 type_specifiers |= SPECIFIER_LONG_LONG;
736 type_specifiers |= SPECIFIER_LONG;
740 /* TODO: if type != NULL for the following rules issue an error */
742 type = parse_compound_type_specifier(1);
745 type = parse_compound_type_specifier(0);
748 type = parse_enum_specifier();
750 case T___builtin_va_list:
751 type = create_builtin_type(token.v.symbol);
755 case T___attribute__:
761 declaration = token.v.symbol->declaration;
762 if(declaration == NULL ||
763 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
764 goto finish_specifiers;
767 type = declaration->type;
768 assert(type != NULL);
772 /* function specifier */
774 goto finish_specifiers;
781 atomic_type_type_t atomic_type;
783 /* match valid basic types */
784 switch(type_specifiers) {
786 atomic_type = ATOMIC_TYPE_VOID;
789 atomic_type = ATOMIC_TYPE_CHAR;
791 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
792 atomic_type = ATOMIC_TYPE_SCHAR;
794 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
795 atomic_type = ATOMIC_TYPE_UCHAR;
797 case SPECIFIER_SHORT:
798 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
799 case SPECIFIER_SHORT | SPECIFIER_INT:
800 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
801 atomic_type = ATOMIC_TYPE_SHORT;
803 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
804 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
805 atomic_type = ATOMIC_TYPE_USHORT;
808 case SPECIFIER_SIGNED:
809 case SPECIFIER_SIGNED | SPECIFIER_INT:
810 atomic_type = ATOMIC_TYPE_INT;
812 case SPECIFIER_UNSIGNED:
813 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
814 atomic_type = ATOMIC_TYPE_UINT;
817 case SPECIFIER_SIGNED | SPECIFIER_LONG:
818 case SPECIFIER_LONG | SPECIFIER_INT:
819 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
820 atomic_type = ATOMIC_TYPE_LONG;
822 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
823 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
824 atomic_type = ATOMIC_TYPE_ULONG;
826 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
827 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
828 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
829 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
831 atomic_type = ATOMIC_TYPE_LONGLONG;
833 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
834 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
836 atomic_type = ATOMIC_TYPE_ULONGLONG;
838 case SPECIFIER_FLOAT:
839 atomic_type = ATOMIC_TYPE_FLOAT;
841 case SPECIFIER_DOUBLE:
842 atomic_type = ATOMIC_TYPE_DOUBLE;
844 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
845 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
848 atomic_type = ATOMIC_TYPE_BOOL;
850 #ifdef PROVIDE_COMPLEX
851 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
852 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
854 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
855 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
857 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
858 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
861 #ifdef PROVIDE_IMAGINARY
862 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
863 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
865 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
866 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
868 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
869 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
873 /* invalid specifier combination, give an error message */
874 if(type_specifiers == 0) {
875 parse_error("no type specifiers given in declaration");
876 } else if((type_specifiers & SPECIFIER_SIGNED) &&
877 (type_specifiers & SPECIFIER_UNSIGNED)) {
878 parse_error("signed and unsigned specifiers gives");
879 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
880 parse_error("only integer types can be signed or unsigned");
882 parse_error("multiple datatypes in declaration");
884 atomic_type = ATOMIC_TYPE_INVALID;
887 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
888 atype->type.type = TYPE_ATOMIC;
889 atype->atype = atomic_type;
892 type = (type_t*) atype;
894 if(type_specifiers != 0) {
895 parse_error("multiple datatypes in declaration");
899 type->qualifiers = type_qualifiers;
901 type_t *result = typehash_insert(type);
902 if(newtype && result != (type_t*) type) {
903 obstack_free(type_obst, type);
906 specifiers->type = result;
910 unsigned parse_type_qualifiers(void)
912 unsigned type_qualifiers = 0;
916 /* type qualifiers */
917 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
918 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
919 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
920 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
923 return type_qualifiers;
928 typedef struct parsed_pointer_t parsed_pointer_t;
929 struct parsed_pointer_t {
930 unsigned type_qualifiers;
931 parsed_pointer_t *next;
935 parsed_pointer_t *parse_pointers(void)
937 parsed_pointer_t *result = NULL;
938 parsed_pointer_t *last_pointer = NULL;
940 while(token.type == '*') {
942 parsed_pointer_t *pointer
943 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
944 pointer->type_qualifiers = parse_type_qualifiers();
946 if(last_pointer != NULL) {
947 last_pointer->next = pointer;
951 last_pointer = pointer;
958 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
960 for( ; pointer != NULL; pointer = pointer->next) {
961 pointer_type_t *pointer_type
962 = allocate_type_zero(sizeof(pointer_type[0]));
963 pointer_type->type.type = TYPE_POINTER;
964 pointer_type->points_to = type;
965 pointer_type->type.qualifiers = pointer->type_qualifiers;
967 type_t *result = typehash_insert((type_t*) pointer_type);
968 if(result != (type_t*) pointer_type) {
969 obstack_free(type_obst, pointer_type);
979 void parse_identifier_list(void)
982 if(token.type != T_IDENTIFIER) {
983 parse_error_expected("problem while parsing parameter identifier "
984 "list", T_IDENTIFIER, 0);
988 if(token.type != ',')
995 declaration_t *parse_parameter(void)
997 declaration_specifiers_t specifiers;
998 memset(&specifiers, 0, sizeof(specifiers));
1000 parse_declaration_specifiers(&specifiers);
1002 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1003 parse_declarator(declaration, specifiers.storage_class,
1004 specifiers.type, 1);
1010 declaration_t *parse_parameters(method_type_t *type)
1012 if(token.type == T_IDENTIFIER) {
1013 symbol_t *symbol = token.v.symbol;
1014 declaration_t *declaration = symbol->declaration;
1015 if(declaration == NULL
1016 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1018 parse_identifier_list();
1023 if(token.type == ')') {
1024 type->unspecified_parameters = 1;
1027 if(token.type == T_void && look_ahead(1)->type == ')') {
1032 declaration_t *declarations = NULL;
1033 declaration_t *declaration;
1034 declaration_t *last_declaration = NULL;
1035 method_parameter_t *parameter;
1036 method_parameter_t *last_parameter = NULL;
1039 switch(token.type) {
1043 return declarations;
1047 declaration = parse_parameter();
1049 parameter = allocate_type_zero(sizeof(parameter[0]));
1050 parameter->type = declaration->type;
1052 if(last_parameter != NULL) {
1053 last_declaration->next = declaration;
1054 last_parameter->next = parameter;
1056 type->parameters = parameter;
1057 declarations = declaration;
1059 last_parameter = parameter;
1060 last_declaration = declaration;
1064 return declarations;
1066 if(token.type != ',')
1067 return declarations;
1072 typedef struct declarator_part declarator_part;
1073 struct declarator_part {
1074 parsed_pointer_t *pointers;
1075 method_type_t *method_type;
1076 declarator_part *inner;
1081 declarator_part *parse_inner_declarator(declaration_t *declaration,
1082 int may_be_abstract)
1084 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1085 memset(part, 0, sizeof(part[0]));
1087 part->pointers = parse_pointers();
1089 /* TODO: find out if this is correct */
1092 switch(token.type) {
1094 if(declaration == NULL) {
1095 parse_error("no identifier expected in typename");
1097 declaration->symbol = token.v.symbol;
1098 declaration->source_position = token.source_position;
1104 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1110 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1115 switch(token.type) {
1119 method_type_t *method_type
1120 = allocate_type_zero(sizeof(method_type[0]));
1121 method_type->type.type = TYPE_METHOD;
1123 declaration_t *parameters = parse_parameters(method_type);
1124 if(declaration != NULL) {
1125 declaration->context.declarations = parameters;
1128 part->method_type = method_type;
1135 if(token.type == T_static) {
1139 unsigned type_qualifiers = parse_type_qualifiers();
1140 if(type_qualifiers != 0) {
1141 if(token.type == T_static) {
1148 if(token.type == '*' && look_ahead(1)->type == ']') {
1150 } else if(token.type != ']') {
1151 parse_assignment_expression();
1157 goto declarator_finished;
1161 declarator_finished:
1168 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1171 type = make_pointers(type, part->pointers);
1173 method_type_t *method_type = part->method_type;
1174 if(method_type != NULL) {
1175 method_type->result_type = type;
1177 type_t *result = typehash_insert((type_t*) method_type);
1178 if(result != (type_t*) method_type) {
1179 obstack_free(type_obst, method_type);
1185 } while(part != NULL);
1191 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1192 type_t *type, int may_be_abstract)
1194 declarator_part *part
1195 = parse_inner_declarator(declaration, may_be_abstract);
1198 declaration->type = construct_declarator_type(part, type);
1199 declaration->storage_class = storage_class;
1200 obstack_free(&temp_obst, part);
1205 type_t *parse_abstract_declarator(type_t *base_type)
1207 declarator_part *part = parse_inner_declarator(NULL, 1);
1212 type_t *result = construct_declarator_type(part, base_type);
1213 obstack_free(&temp_obst, part);
1218 static declaration_t *record_declaration(declaration_t *declaration)
1223 symbol_t *symbol = declaration->symbol;
1224 if(symbol != NULL) {
1225 declaration_t *alias = environment_push(declaration, context);
1226 if(alias != declaration)
1230 if(last_declaration != NULL) {
1231 last_declaration->next = declaration;
1233 context->declarations = declaration;
1235 last_declaration = declaration;
1241 void parser_error_multiple_definition(declaration_t *previous,
1242 declaration_t *declaration)
1244 parser_print_error_prefix_pos(declaration->source_position);
1245 fprintf(stderr, "multiple definition of symbol '%s'\n",
1246 declaration->symbol->string);
1247 parser_print_error_prefix_pos(previous->source_position);
1248 fprintf(stderr, "this is the location of the previous "
1253 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1256 declaration_t *ndeclaration
1257 = allocate_ast_zero(sizeof(ndeclaration[0]));
1259 parse_declarator(ndeclaration, specifiers->storage_class,
1260 specifiers->type, 0);
1261 declaration_t *declaration = record_declaration(ndeclaration);
1262 if(token.type == '=') {
1265 /* TODO: check that this is an allowed type (esp. no method type) */
1267 if(declaration->initializer != NULL) {
1268 parser_error_multiple_definition(declaration, ndeclaration);
1271 if(token.type == '{') {
1275 declaration->initializer = parse_assignment_expression();
1277 } else if(token.type == '{') {
1278 if(declaration->type->type != TYPE_METHOD) {
1279 parser_print_error_prefix();
1280 fprintf(stderr, "Declarator ");
1281 print_type(declaration->type, declaration->symbol);
1282 fprintf(stderr, " is not a method type.\n");
1285 if(declaration->initializer != NULL) {
1286 parser_error_multiple_definition(declaration, ndeclaration);
1289 int top = environment_top();
1290 context_t *last_context = context;
1291 set_context(&declaration->context);
1293 /* push function parameters */
1294 declaration_t *parameter = declaration->context.declarations;
1295 for( ; parameter != NULL; parameter = parameter->next) {
1296 environment_push(parameter, context);
1299 statement_t *statement = parse_compound_statement();
1301 assert(context == &declaration->context);
1302 set_context(last_context);
1303 environment_pop_to(top);
1305 declaration->statement = statement;
1309 if(token.type != ',')
1317 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1320 if(token.type == ':') {
1322 parse_constant_expression();
1323 /* TODO (bitfields) */
1325 declaration_t *declaration
1326 = allocate_ast_zero(sizeof(declaration[0]));
1327 parse_declarator(declaration, specifiers->storage_class,
1328 specifiers->type, 1);
1330 /* TODO: check for doubled fields */
1331 record_declaration(declaration);
1333 if(token.type == ':') {
1335 parse_constant_expression();
1336 /* TODO (bitfields) */
1340 if(token.type != ',')
1347 static void parse_compound_type_entries(void)
1351 while(token.type != '}' && token.type != T_EOF) {
1352 declaration_specifiers_t specifiers;
1353 memset(&specifiers, 0, sizeof(specifiers));
1354 /* TODO not correct as this allows storage class stuff... but only
1355 * specifiers and qualifiers sould be allowed here */
1356 parse_declaration_specifiers(&specifiers);
1358 parse_struct_declarators(&specifiers);
1360 if(token.type == T_EOF) {
1361 parse_error("unexpected error while parsing struct");
1366 void parse_declaration(void)
1368 declaration_specifiers_t specifiers;
1369 memset(&specifiers, 0, sizeof(specifiers));
1370 parse_declaration_specifiers(&specifiers);
1372 if(token.type == ';') {
1376 parse_init_declarators(&specifiers);
1379 type_t *parse_typename(void)
1381 declaration_specifiers_t specifiers;
1382 memset(&specifiers, 0, sizeof(specifiers));
1383 parse_declaration_specifiers(&specifiers);
1384 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1385 /* TODO: improve error message, user does probably not know what a
1386 * storage class is...
1388 parse_error("typename may not have a storage class");
1391 type_t *result = parse_abstract_declarator(specifiers.type);
1399 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1400 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1401 expression_t *left);
1403 typedef struct expression_parser_function_t expression_parser_function_t;
1404 struct expression_parser_function_t {
1405 unsigned precedence;
1406 parse_expression_function parser;
1407 unsigned infix_precedence;
1408 parse_expression_infix_function infix_parser;
1411 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1414 expression_t *expected_expression_error(void)
1416 parser_print_error_prefix();
1417 fprintf(stderr, "expected expression, got token ");
1418 print_token(stderr, & token);
1419 fprintf(stderr, "\n");
1421 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1422 expression->type = EXPR_INVALID;
1429 expression_t *parse_string_const(void)
1431 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1433 cnst->expression.type = EXPR_STRING_LITERAL;
1434 cnst->value = parse_string_literals();
1436 return (expression_t*) cnst;
1440 expression_t *parse_int_const(void)
1442 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1444 cnst->expression.type = EXPR_CONST;
1445 cnst->value = token.v.intvalue;
1449 return (expression_t*) cnst;
1453 expression_t *parse_reference(void)
1455 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1457 ref->expression.type = EXPR_REFERENCE;
1458 ref->symbol = token.v.symbol;
1460 if(ref->symbol->declaration == NULL) {
1461 parser_print_error_prefix();
1462 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1464 ref->declaration = ref->symbol->declaration;
1468 return (expression_t*) ref;
1472 expression_t *parse_cast(void)
1474 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1476 cast->expression.type = EXPR_UNARY;
1477 cast->type = UNEXPR_CAST;
1478 cast->expression.source_position = token.source_position;
1480 type_t *type = parse_typename();
1483 expression_t *value = parse_sub_expression(20);
1485 cast->expression.datatype = type;
1486 cast->value = value;
1488 return (expression_t*) cast;
1492 expression_t *parse_brace_expression(void)
1496 declaration_t *declaration;
1497 switch(token.type) {
1500 return parse_cast();
1502 declaration = token.v.symbol->declaration;
1503 if(declaration != NULL &&
1504 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1505 return parse_cast();
1509 expression_t *result = parse_expression();
1516 expression_t *parse_primary_expression(void)
1518 switch(token.type) {
1520 return parse_int_const();
1521 case T_STRING_LITERAL:
1522 return parse_string_const();
1524 return parse_reference();
1526 return parse_brace_expression();
1529 parser_print_error_prefix();
1530 fprintf(stderr, "unexpected token ");
1531 print_token(stderr, &token);
1532 fprintf(stderr, "\n");
1538 expression_t *parse_array_expression(unsigned precedence,
1539 expression_t *array_ref)
1545 array_access_expression_t *array_access
1546 = allocate_ast_zero(sizeof(array_access[0]));
1548 array_access->expression.type = EXPR_ARRAY_ACCESS;
1549 array_access->array_ref = array_ref;
1550 array_access->index = parse_expression();
1552 if(token.type != ']') {
1553 parse_error_expected("Problem while parsing array access", ']', 0);
1558 return (expression_t*) array_access;
1562 type_t *get_expression_type(const expression_t *expression)
1570 int is_type_specifier(const token_t *token)
1572 declaration_t *declaration;
1574 switch(token->type) {
1578 declaration = token->v.symbol->declaration;
1579 if(declaration == NULL)
1581 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1590 expression_t *parse_sizeof(unsigned precedence)
1594 sizeof_expression_t *sizeof_expression
1595 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1596 sizeof_expression->expression.type = EXPR_SIZEOF;
1598 if(token.type == '(' && is_type_specifier(look_ahead(1))) {
1600 sizeof_expression->type = parse_typename();
1603 expression_t *expression = parse_sub_expression(precedence);
1604 sizeof_expression->type = get_expression_type(expression);
1607 return (expression_t*) sizeof_expression;
1611 expression_t *parse_select_expression(unsigned precedence,
1612 expression_t *compound)
1616 assert(token.type == '.' || token.type == T_SELECT);
1619 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1621 select->expression.type = EXPR_SELECT;
1622 select->compound = compound;
1624 if(token.type != T_IDENTIFIER) {
1625 parse_error_expected("Problem while parsing compound select",
1629 select->symbol = token.v.symbol;
1632 return (expression_t*) select;
1636 expression_t *parse_call_expression(unsigned precedence,
1637 expression_t *expression)
1640 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1642 call->expression.type = EXPR_CALL;
1643 call->method = expression;
1645 /* parse arguments */
1648 if(token.type != ')') {
1649 call_argument_t *last_argument = NULL;
1652 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1654 argument->expression = parse_expression();
1655 if(last_argument == NULL) {
1656 call->arguments = argument;
1658 last_argument->next = argument;
1660 last_argument = argument;
1662 if(token.type != ',')
1669 return (expression_t*) call;
1673 expression_t *parse_conditional_expression(unsigned precedence,
1674 expression_t *expression)
1678 conditional_expression_t *conditional
1679 = allocate_ast_zero(sizeof(conditional[0]));
1680 conditional->condition = expression;
1682 conditional->true_expression = parse_expression();
1684 conditional->false_expression = parse_sub_expression(precedence);
1686 return (expression_t*) conditional;
1689 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1691 expression_t *parse_##unexpression_type(unsigned precedence) \
1695 unary_expression_t *unary_expression \
1696 = allocate_ast_zero(sizeof(unary_expression[0])); \
1697 unary_expression->expression.type = EXPR_UNARY; \
1698 unary_expression->type = unexpression_type; \
1699 unary_expression->value = parse_sub_expression(precedence); \
1701 return (expression_t*) unary_expression; \
1704 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1705 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1706 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1707 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1708 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1709 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1710 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1711 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1713 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1715 expression_t *parse_##unexpression_type(unsigned precedence, \
1716 expression_t *left) \
1718 (void) precedence; \
1721 unary_expression_t *unary_expression \
1722 = allocate_ast_zero(sizeof(unary_expression[0])); \
1723 unary_expression->expression.type = EXPR_UNARY; \
1724 unary_expression->type = unexpression_type; \
1725 unary_expression->value = left; \
1727 return (expression_t*) unary_expression; \
1730 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1731 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1733 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1735 expression_t *parse_##binexpression_type(unsigned precedence, \
1736 expression_t *left) \
1740 expression_t *right = parse_sub_expression(precedence); \
1742 binary_expression_t *binexpr \
1743 = allocate_ast_zero(sizeof(binexpr[0])); \
1744 binexpr->expression.type = EXPR_BINARY; \
1745 binexpr->type = binexpression_type; \
1746 binexpr->left = left; \
1747 binexpr->right = right; \
1749 return (expression_t*) binexpr; \
1752 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1753 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1754 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1755 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1756 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1757 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1758 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1759 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1760 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
1761 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1762 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1763 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1764 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1765 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1766 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1767 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1768 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1769 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1772 expression_t *parse_sub_expression(unsigned precedence)
1774 if(token.type < 0) {
1775 return expected_expression_error();
1778 expression_parser_function_t *parser
1779 = &expression_parsers[token.type];
1780 source_position_t source_position = token.source_position;
1783 if(parser->parser != NULL) {
1784 left = parser->parser(parser->precedence);
1786 left = parse_primary_expression();
1789 left->source_position = source_position;
1792 if(token.type < 0) {
1793 return expected_expression_error();
1796 parser = &expression_parsers[token.type];
1797 if(parser->infix_parser == NULL)
1799 if(parser->infix_precedence < precedence)
1802 left = parser->infix_parser(parser->infix_precedence, left);
1804 left->source_position = source_position;
1811 expression_t *parse_expression(void)
1813 return parse_sub_expression(1);
1818 void register_expression_parser(parse_expression_function parser,
1819 int token_type, unsigned precedence)
1821 expression_parser_function_t *entry = &expression_parsers[token_type];
1823 if(entry->parser != NULL) {
1824 fprintf(stderr, "for token ");
1825 print_token_type(stderr, token_type);
1826 fprintf(stderr, "\n");
1827 panic("trying to register multiple expression parsers for a token");
1829 entry->parser = parser;
1830 entry->precedence = precedence;
1833 void register_expression_infix_parser(parse_expression_infix_function parser,
1834 int token_type, unsigned precedence)
1836 expression_parser_function_t *entry = &expression_parsers[token_type];
1838 if(entry->infix_parser != NULL) {
1839 fprintf(stderr, "for token ");
1840 print_token_type(stderr, token_type);
1841 fprintf(stderr, "\n");
1842 panic("trying to register multiple infix expression parsers for a "
1845 entry->infix_parser = parser;
1846 entry->infix_precedence = precedence;
1850 void init_expression_parsers(void)
1852 memset(&expression_parsers, 0, sizeof(expression_parsers));
1854 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1855 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1856 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1858 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1859 T_GREATERGREATER, 16);
1860 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1861 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1862 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1863 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1864 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1865 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1866 T_GREATEREQUAL, 14);
1867 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1868 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1869 T_EXCLAMATIONMARKEQUAL, 13);
1870 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1871 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1872 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1873 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1874 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1875 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1876 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1878 register_expression_infix_parser(parse_array_expression, '[', 30);
1879 register_expression_infix_parser(parse_call_expression, '(', 30);
1880 register_expression_infix_parser(parse_select_expression, '.', 30);
1881 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1882 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1884 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1887 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1888 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1889 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1890 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1891 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1892 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1893 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1894 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1895 register_expression_parser(parse_sizeof, T_sizeof, 25);
1900 statement_t *parse_case_statement(void)
1911 statement_t *parse_default_statement(void)
1921 statement_t *parse_label_statement(void)
1931 statement_t *parse_if(void)
1935 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1936 statement->statement.type = STATEMENT_IF;
1939 statement->condition = parse_expression();
1942 statement->true_statement = parse_statement();
1943 if(token.type == T_else) {
1945 statement->false_statement = parse_statement();
1948 return (statement_t*) statement;
1952 statement_t *parse_switch(void)
1964 statement_t *parse_while(void)
1976 statement_t *parse_do(void)
1989 statement_t *parse_for(void)
1993 if(token.type != ';') {
1994 /* TODO not correct... this could also be a declaration */
1998 if(token.type != ';') {
2002 if(token.type != ')') {
2012 statement_t *parse_goto(void)
2015 expect(T_IDENTIFIER);
2022 statement_t *parse_continue(void)
2027 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2028 statement->source_position = token.source_position;
2029 statement->type = STATEMENT_CONTINUE;
2035 statement_t *parse_break(void)
2044 statement_t *parse_return(void)
2048 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2049 statement->statement.type = STATEMENT_RETURN;
2050 if(token.type != ';') {
2051 statement->return_value = parse_expression();
2055 return (statement_t*) statement;
2059 statement_t *parse_declaration_statement(void)
2061 parse_declaration();
2066 statement_t *parse_expression_statement(void)
2073 statement_t *parse_statement(void)
2075 declaration_t *declaration;
2076 statement_t *statement = NULL;
2078 /* declaration or statement */
2079 switch(token.type) {
2081 statement = parse_case_statement();
2085 statement = parse_default_statement();
2089 statement = parse_compound_statement();
2093 statement = parse_if();
2097 statement = parse_switch();
2101 statement = parse_while();
2105 statement = parse_do();
2109 statement = parse_for();
2113 statement = parse_goto();
2117 statement = parse_continue();
2121 statement = parse_break();
2125 statement = parse_return();
2134 if(look_ahead(1)->type == ':') {
2135 statement = parse_label_statement();
2139 declaration = token.v.symbol->declaration;
2140 if(declaration != NULL &&
2141 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2142 statement = parse_declaration_statement();
2146 statement = parse_expression_statement();
2150 statement = parse_declaration_statement();
2154 statement = parse_expression_statement();
2162 statement_t *parse_compound_statement(void)
2166 compound_statement_t *compound_statement
2167 = allocate_ast_zero(sizeof(compound_statement[0]));
2168 compound_statement->statement.type = STATEMENT_COMPOUND;
2170 int top = environment_top();
2171 context_t *last_context = context;
2172 set_context(&compound_statement->context);
2174 statement_t *last_statement = NULL;
2176 while(token.type != '}' && token.type != T_EOF) {
2177 statement_t *statement = parse_statement();
2179 if(last_statement != NULL) {
2180 last_statement->next = statement;
2182 compound_statement->statements = statement;
2184 last_statement = statement;
2187 assert(context == &compound_statement->context);
2188 set_context(last_context);
2189 environment_pop_to(top);
2193 return (statement_t*) compound_statement;
2197 translation_unit_t *parse_translation_unit(void)
2199 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2201 assert(context == NULL);
2202 set_context(&unit->context);
2204 while(token.type != T_EOF) {
2205 parse_declaration();
2208 assert(context == &unit->context);
2210 last_declaration = NULL;
2215 translation_unit_t *parse(void)
2217 obstack_init(&environment_obstack);
2218 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2220 type_set_output(stderr);
2222 lookahead_bufpos = 0;
2223 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2226 translation_unit_t *unit = parse_translation_unit();
2228 DEL_ARR_F(environment_stack);
2229 obstack_free(&environment_obstack, NULL);
2234 void init_parser(void)
2236 init_expression_parsers();
2237 obstack_init(&temp_obst);
2240 void exit_parser(void)
2242 obstack_free(&temp_obst, NULL);