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);
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);
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 /* start parsing at precedence 7 (conditional expression) */
332 return parse_sub_expression(7);
335 static expression_t *parse_assignment_expression(void)
337 /* start parsing at precedence 2 (assignment expression) */
338 return parse_sub_expression(2);
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 specifier",
390 T_IDENTIFIER, '{', 0);
392 parse_error_expected("problem while parsing union type specifier",
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 void parse_enum_entries(void)
438 if(token.type == '}') {
440 parse_error("empty enum not allowed");
445 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
447 if(token.type != T_IDENTIFIER) {
448 parse_error_expected("problem while parsing enum entry",
453 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
454 entry->symbol = token.v.symbol;
455 entry->source_position = token.source_position;
458 if(token.type == '=') {
460 entry->initializer = parse_constant_expression();
463 record_declaration(entry);
465 if(token.type != ',')
468 } while(token.type != '}');
473 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
475 enum_type_t *type = types;
476 for( ; type != NULL; type = type->next) {
477 if(type->symbol == symbol)
484 static type_t *parse_enum_specifier(void)
488 symbol_t *symbol = NULL;
489 enum_type_t *enum_type = NULL;
491 if(token.type == T_IDENTIFIER) {
492 symbol = token.v.symbol;
495 if(context != NULL) {
496 enum_type = find_enum_type(context->enums, symbol);
498 } else if(token.type != '{') {
499 parse_error_expected("problem while parsing enum type specifier",
500 T_IDENTIFIER, '{', 0);
504 if(enum_type == NULL) {
505 enum_type = allocate_type_zero(sizeof(enum_type[0]));
506 enum_type->type.type = TYPE_ENUM;
507 enum_type->source_position = token.source_position;
508 enum_type->symbol = symbol;
511 if(token.type == '{') {
512 if(enum_type->defined) {
513 parser_print_error_prefix();
514 fprintf(stderr, "multiple definitions of enum %s\n",
516 enum_type->entries_begin = NULL;
517 enum_type->entries_end = NULL;
519 enum_type->defined = 1;
521 declaration_t *before = last_declaration;
523 parse_enum_entries();
526 enum_type->entries_begin = context->declarations;
528 enum_type->entries_begin = before->next;
530 enum_type->entries_end = last_declaration;
533 return (type_t*) enum_type;
537 const char *parse_string_literals(void)
539 assert(token.type == T_STRING_LITERAL);
540 const char *result = token.v.string;
544 while(token.type == T_STRING_LITERAL) {
545 result = concat_strings(result, token.v.string);
553 void parse_attributes(void)
557 case T___attribute__:
565 parse_error("EOF while parsing attribute");
583 if(token.type != T_STRING_LITERAL) {
584 parse_error_expected("while parsing assembler attribute",
589 parse_string_literals();
594 goto attributes_finished;
603 SPECIFIER_SIGNED = 1 << 0,
604 SPECIFIER_UNSIGNED = 1 << 1,
605 SPECIFIER_LONG = 1 << 2,
606 SPECIFIER_INT = 1 << 3,
607 SPECIFIER_DOUBLE = 1 << 4,
608 SPECIFIER_CHAR = 1 << 5,
609 SPECIFIER_SHORT = 1 << 6,
610 SPECIFIER_LONG_LONG = 1 << 7,
611 SPECIFIER_FLOAT = 1 << 8,
612 SPECIFIER_BOOL = 1 << 9,
613 SPECIFIER_VOID = 1 << 10,
614 #ifdef PROVIDE_COMPLEX
615 SPECIFIER_COMPLEX = 1 << 11,
617 #ifdef PROVIDE_IMAGINARY
618 SPECIFIER_IMAGINARY = 1 << 12,
622 #define STORAGE_CLASSES \
629 #define TYPE_QUALIFIERS \
635 #ifdef PROVIDE_COMPLEX
636 #define COMPLEX_SPECIFIERS \
639 #define COMPLEX_SPECIFIERS
642 #ifdef PROVIDE_IMAGINARY
643 #define IMAGINARY_SPECIFIERS \
646 #define IMAGINARY_SPECIFIERS
649 #define TYPE_SPECIFIERS \
666 #define DECLARATION_START \
672 type_t *create_builtin_type(symbol_t *symbol)
674 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
675 type->type.type = TYPE_BUILTIN;
676 type->symbol = symbol;
678 type_t *result = typehash_insert((type_t*) type);
679 if(result != (type_t*) type) {
680 obstack_free(type_obst, type);
687 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
689 declaration_t *declaration;
691 unsigned type_qualifiers = 0;
692 unsigned type_specifiers = 0;
699 #define MATCH_STORAGE_CLASS(token, class) \
701 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
702 parse_error("multiple storage classes in declaration " \
705 specifiers->storage_class = class; \
709 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
710 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
711 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
712 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
713 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
715 /* type qualifiers */
716 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
718 type_qualifiers |= qualifier; \
722 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
723 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
724 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
725 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
727 case T___extension__:
732 /* type specifiers */
733 #define MATCH_SPECIFIER(token, specifier, name) \
736 if(type_specifiers & specifier) { \
737 parse_error("multiple " name " type specifiers given"); \
739 type_specifiers |= specifier; \
743 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
744 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
745 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
746 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
747 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
748 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
749 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
750 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
751 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
752 #ifdef PROVIDE_COMPLEX
753 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
755 #ifdef PROVIDE_IMAGINARY
756 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
760 if(type_specifiers & SPECIFIER_LONG_LONG) {
761 parse_error("multiple type specifiers given");
762 } else if(type_specifiers & SPECIFIER_LONG) {
763 type_specifiers |= SPECIFIER_LONG_LONG;
765 type_specifiers |= SPECIFIER_LONG;
769 /* TODO: if type != NULL for the following rules issue an error */
771 type = parse_compound_type_specifier(1);
774 type = parse_compound_type_specifier(0);
777 type = parse_enum_specifier();
779 case T___builtin_va_list:
780 type = create_builtin_type(token.v.symbol);
784 case T___attribute__:
790 declaration = token.v.symbol->declaration;
791 if(declaration == NULL ||
792 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
793 goto finish_specifiers;
796 type = declaration->type;
797 assert(type != NULL);
801 /* function specifier */
803 goto finish_specifiers;
810 atomic_type_type_t atomic_type;
812 /* match valid basic types */
813 switch(type_specifiers) {
815 atomic_type = ATOMIC_TYPE_VOID;
818 atomic_type = ATOMIC_TYPE_CHAR;
820 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
821 atomic_type = ATOMIC_TYPE_SCHAR;
823 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
824 atomic_type = ATOMIC_TYPE_UCHAR;
826 case SPECIFIER_SHORT:
827 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
828 case SPECIFIER_SHORT | SPECIFIER_INT:
829 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
830 atomic_type = ATOMIC_TYPE_SHORT;
832 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
833 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
834 atomic_type = ATOMIC_TYPE_USHORT;
837 case SPECIFIER_SIGNED:
838 case SPECIFIER_SIGNED | SPECIFIER_INT:
839 atomic_type = ATOMIC_TYPE_INT;
841 case SPECIFIER_UNSIGNED:
842 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
843 atomic_type = ATOMIC_TYPE_UINT;
846 case SPECIFIER_SIGNED | SPECIFIER_LONG:
847 case SPECIFIER_LONG | SPECIFIER_INT:
848 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
849 atomic_type = ATOMIC_TYPE_LONG;
851 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
852 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
853 atomic_type = ATOMIC_TYPE_ULONG;
855 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
856 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
857 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
858 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
860 atomic_type = ATOMIC_TYPE_LONGLONG;
862 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
863 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
865 atomic_type = ATOMIC_TYPE_ULONGLONG;
867 case SPECIFIER_FLOAT:
868 atomic_type = ATOMIC_TYPE_FLOAT;
870 case SPECIFIER_DOUBLE:
871 atomic_type = ATOMIC_TYPE_DOUBLE;
873 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
874 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
877 atomic_type = ATOMIC_TYPE_BOOL;
879 #ifdef PROVIDE_COMPLEX
880 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
881 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
883 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
884 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
886 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
887 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
890 #ifdef PROVIDE_IMAGINARY
891 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
892 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
894 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
895 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
897 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
898 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
902 /* invalid specifier combination, give an error message */
903 if(type_specifiers == 0) {
904 parse_error("no type specifiers given in declaration");
905 } else if((type_specifiers & SPECIFIER_SIGNED) &&
906 (type_specifiers & SPECIFIER_UNSIGNED)) {
907 parse_error("signed and unsigned specifiers gives");
908 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
909 parse_error("only integer types can be signed or unsigned");
911 parse_error("multiple datatypes in declaration");
913 atomic_type = ATOMIC_TYPE_INVALID;
916 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
917 atype->type.type = TYPE_ATOMIC;
918 atype->atype = atomic_type;
921 type = (type_t*) atype;
923 if(type_specifiers != 0) {
924 parse_error("multiple datatypes in declaration");
928 type->qualifiers = type_qualifiers;
930 type_t *result = typehash_insert(type);
931 if(newtype && result != (type_t*) type) {
932 obstack_free(type_obst, type);
935 specifiers->type = result;
939 unsigned parse_type_qualifiers(void)
941 unsigned type_qualifiers = 0;
945 /* type qualifiers */
946 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
947 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
948 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
949 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
952 return type_qualifiers;
957 typedef struct parsed_pointer_t parsed_pointer_t;
958 struct parsed_pointer_t {
959 unsigned type_qualifiers;
960 parsed_pointer_t *next;
964 parsed_pointer_t *parse_pointers(void)
966 parsed_pointer_t *result = NULL;
967 parsed_pointer_t *last_pointer = NULL;
969 while(token.type == '*') {
971 parsed_pointer_t *pointer
972 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
973 pointer->type_qualifiers = parse_type_qualifiers();
975 if(last_pointer != NULL) {
976 last_pointer->next = pointer;
980 last_pointer = pointer;
987 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
989 for( ; pointer != NULL; pointer = pointer->next) {
990 pointer_type_t *pointer_type
991 = allocate_type_zero(sizeof(pointer_type[0]));
992 pointer_type->type.type = TYPE_POINTER;
993 pointer_type->points_to = type;
994 pointer_type->type.qualifiers = pointer->type_qualifiers;
996 type_t *result = typehash_insert((type_t*) pointer_type);
997 if(result != (type_t*) pointer_type) {
998 obstack_free(type_obst, pointer_type);
1008 void parse_identifier_list(void)
1011 if(token.type != T_IDENTIFIER) {
1012 parse_error_expected("problem while parsing parameter identifier "
1013 "list", T_IDENTIFIER, 0);
1017 if(token.type != ',')
1024 declaration_t *parse_parameter(void)
1026 declaration_specifiers_t specifiers;
1027 memset(&specifiers, 0, sizeof(specifiers));
1029 parse_declaration_specifiers(&specifiers);
1031 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1032 parse_declarator(declaration, specifiers.storage_class,
1033 specifiers.type, 1);
1039 declaration_t *parse_parameters(method_type_t *type)
1041 if(token.type == T_IDENTIFIER) {
1042 symbol_t *symbol = token.v.symbol;
1043 declaration_t *declaration = symbol->declaration;
1044 if(declaration == NULL
1045 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1047 parse_identifier_list();
1052 if(token.type == ')') {
1053 type->unspecified_parameters = 1;
1056 if(token.type == T_void && look_ahead(1)->type == ')') {
1061 declaration_t *declarations = NULL;
1062 declaration_t *declaration;
1063 declaration_t *last_declaration = NULL;
1064 method_parameter_t *parameter;
1065 method_parameter_t *last_parameter = NULL;
1068 switch(token.type) {
1072 return declarations;
1075 case T___extension__:
1077 declaration = parse_parameter();
1079 parameter = allocate_type_zero(sizeof(parameter[0]));
1080 parameter->type = declaration->type;
1082 if(last_parameter != NULL) {
1083 last_declaration->next = declaration;
1084 last_parameter->next = parameter;
1086 type->parameters = parameter;
1087 declarations = declaration;
1089 last_parameter = parameter;
1090 last_declaration = declaration;
1094 return declarations;
1096 if(token.type != ',')
1097 return declarations;
1102 typedef struct declarator_part declarator_part;
1103 struct declarator_part {
1104 parsed_pointer_t *pointers;
1105 method_type_t *method_type;
1106 declarator_part *inner;
1111 declarator_part *parse_inner_declarator(declaration_t *declaration,
1112 int may_be_abstract)
1114 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1115 memset(part, 0, sizeof(part[0]));
1117 part->pointers = parse_pointers();
1119 /* TODO: find out if this is correct */
1122 switch(token.type) {
1124 if(declaration == NULL) {
1125 parse_error("no identifier expected in typename");
1127 declaration->symbol = token.v.symbol;
1128 declaration->source_position = token.source_position;
1134 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1140 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1145 switch(token.type) {
1149 method_type_t *method_type
1150 = allocate_type_zero(sizeof(method_type[0]));
1151 method_type->type.type = TYPE_METHOD;
1153 declaration_t *parameters = parse_parameters(method_type);
1154 if(declaration != NULL) {
1155 declaration->context.declarations = parameters;
1158 part->method_type = method_type;
1165 if(token.type == T_static) {
1169 unsigned type_qualifiers = parse_type_qualifiers();
1170 if(type_qualifiers != 0) {
1171 if(token.type == T_static) {
1178 if(token.type == '*' && look_ahead(1)->type == ']') {
1180 } else if(token.type != ']') {
1181 parse_assignment_expression();
1187 goto declarator_finished;
1191 declarator_finished:
1198 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1201 type = make_pointers(type, part->pointers);
1203 method_type_t *method_type = part->method_type;
1204 if(method_type != NULL) {
1205 method_type->result_type = type;
1207 type_t *result = typehash_insert((type_t*) method_type);
1208 if(result != (type_t*) method_type) {
1209 obstack_free(type_obst, method_type);
1215 } while(part != NULL);
1221 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1222 type_t *type, int may_be_abstract)
1224 declarator_part *part
1225 = parse_inner_declarator(declaration, may_be_abstract);
1228 declaration->type = construct_declarator_type(part, type);
1229 declaration->storage_class = storage_class;
1230 obstack_free(&temp_obst, part);
1235 type_t *parse_abstract_declarator(type_t *base_type)
1237 declarator_part *part = parse_inner_declarator(NULL, 1);
1242 type_t *result = construct_declarator_type(part, base_type);
1243 obstack_free(&temp_obst, part);
1248 static declaration_t *record_declaration(declaration_t *declaration)
1253 symbol_t *symbol = declaration->symbol;
1254 if(symbol != NULL) {
1255 declaration_t *alias = environment_push(declaration, context);
1256 if(alias != declaration)
1260 if(last_declaration != NULL) {
1261 last_declaration->next = declaration;
1263 context->declarations = declaration;
1265 last_declaration = declaration;
1271 void parser_error_multiple_definition(declaration_t *previous,
1272 declaration_t *declaration)
1274 parser_print_error_prefix_pos(declaration->source_position);
1275 fprintf(stderr, "multiple definition of symbol '%s'\n",
1276 declaration->symbol->string);
1277 parser_print_error_prefix_pos(previous->source_position);
1278 fprintf(stderr, "this is the location of the previous "
1283 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1286 declaration_t *ndeclaration
1287 = allocate_ast_zero(sizeof(ndeclaration[0]));
1289 parse_declarator(ndeclaration, specifiers->storage_class,
1290 specifiers->type, 0);
1291 declaration_t *declaration = record_declaration(ndeclaration);
1292 if(token.type == '=') {
1295 /* TODO: check that this is an allowed type (esp. no method type) */
1297 if(declaration->initializer != NULL) {
1298 parser_error_multiple_definition(declaration, ndeclaration);
1301 if(token.type == '{') {
1305 declaration->initializer = parse_assignment_expression();
1307 } else if(token.type == '{') {
1308 if(declaration->type->type != TYPE_METHOD) {
1309 parser_print_error_prefix();
1310 fprintf(stderr, "Declarator ");
1311 print_type_ext(declaration->type, declaration->symbol, NULL);
1312 fprintf(stderr, " is not a method type.\n");
1315 if(declaration->initializer != NULL) {
1316 parser_error_multiple_definition(declaration, ndeclaration);
1319 int top = environment_top();
1320 context_t *last_context = context;
1321 set_context(&declaration->context);
1323 /* push function parameters */
1324 declaration_t *parameter = declaration->context.declarations;
1325 for( ; parameter != NULL; parameter = parameter->next) {
1326 environment_push(parameter, context);
1329 statement_t *statement = parse_compound_statement();
1331 assert(context == &declaration->context);
1332 set_context(last_context);
1333 environment_pop_to(top);
1335 declaration->statement = statement;
1339 if(token.type != ',')
1347 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1350 if(token.type == ':') {
1352 parse_constant_expression();
1353 /* TODO (bitfields) */
1355 declaration_t *declaration
1356 = allocate_ast_zero(sizeof(declaration[0]));
1357 parse_declarator(declaration, specifiers->storage_class,
1358 specifiers->type, 1);
1360 /* TODO: check for doubled fields */
1361 record_declaration(declaration);
1363 if(token.type == ':') {
1365 parse_constant_expression();
1366 /* TODO (bitfields) */
1370 if(token.type != ',')
1377 static void parse_compound_type_entries(void)
1381 while(token.type != '}' && token.type != T_EOF) {
1382 declaration_specifiers_t specifiers;
1383 memset(&specifiers, 0, sizeof(specifiers));
1384 /* TODO not correct as this allows storage class stuff... but only
1385 * specifiers and qualifiers sould be allowed here */
1386 parse_declaration_specifiers(&specifiers);
1388 parse_struct_declarators(&specifiers);
1390 if(token.type == T_EOF) {
1391 parse_error("unexpected error while parsing struct");
1396 void parse_declaration(void)
1398 declaration_specifiers_t specifiers;
1399 memset(&specifiers, 0, sizeof(specifiers));
1400 parse_declaration_specifiers(&specifiers);
1402 if(token.type == ';') {
1406 parse_init_declarators(&specifiers);
1409 type_t *parse_typename(void)
1411 declaration_specifiers_t specifiers;
1412 memset(&specifiers, 0, sizeof(specifiers));
1413 parse_declaration_specifiers(&specifiers);
1414 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1415 /* TODO: improve error message, user does probably not know what a
1416 * storage class is...
1418 parse_error("typename may not have a storage class");
1421 type_t *result = parse_abstract_declarator(specifiers.type);
1429 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1430 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1431 expression_t *left);
1433 typedef struct expression_parser_function_t expression_parser_function_t;
1434 struct expression_parser_function_t {
1435 unsigned precedence;
1436 parse_expression_function parser;
1437 unsigned infix_precedence;
1438 parse_expression_infix_function infix_parser;
1441 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1444 expression_t *expected_expression_error(void)
1446 parser_print_error_prefix();
1447 fprintf(stderr, "expected expression, got token ");
1448 print_token(stderr, & token);
1449 fprintf(stderr, "\n");
1451 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1452 expression->type = EXPR_INVALID;
1459 expression_t *parse_string_const(void)
1461 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1463 cnst->expression.type = EXPR_STRING_LITERAL;
1464 cnst->value = parse_string_literals();
1466 return (expression_t*) cnst;
1470 expression_t *parse_int_const(void)
1472 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1474 cnst->expression.type = EXPR_CONST;
1475 cnst->value = token.v.intvalue;
1479 return (expression_t*) cnst;
1483 expression_t *parse_reference(void)
1485 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1487 ref->expression.type = EXPR_REFERENCE;
1488 ref->symbol = token.v.symbol;
1490 if(ref->symbol->declaration == NULL) {
1491 parser_print_error_prefix();
1492 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1494 ref->declaration = ref->symbol->declaration;
1498 return (expression_t*) ref;
1502 expression_t *parse_cast(void)
1504 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1506 cast->expression.type = EXPR_UNARY;
1507 cast->type = UNEXPR_CAST;
1508 cast->expression.source_position = token.source_position;
1510 type_t *type = parse_typename();
1513 expression_t *value = parse_sub_expression(20);
1515 cast->expression.datatype = type;
1516 cast->value = value;
1518 return (expression_t*) cast;
1522 expression_t *parse_statement_expression(void)
1524 statement_expression_t *expression
1525 = allocate_ast_zero(sizeof(expression[0]));
1526 expression->expression.type = EXPR_STATEMENT;
1527 expression->statement = parse_compound_statement();
1531 return (expression_t*) expression;
1535 expression_t *parse_brace_expression(void)
1539 declaration_t *declaration;
1540 switch(token.type) {
1542 /* gcc extension: a stement expression */
1543 return parse_statement_expression();
1547 return parse_cast();
1549 declaration = token.v.symbol->declaration;
1550 if(declaration != NULL &&
1551 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1552 return parse_cast();
1556 expression_t *result = parse_expression();
1563 expression_t *parse_function_keyword(void)
1565 eat(T___FUNCTION__);
1568 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1569 expression->expression.type = EXPR_FUNCTION;
1570 expression->value = "TODO: FUNCTION";
1572 return (expression_t*) expression;
1576 expression_t *parse_pretty_function_keyword(void)
1578 eat(T___PRETTY_FUNCTION__);
1581 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1582 expression->expression.type = EXPR_PRETTY_FUNCTION;
1583 expression->value = "TODO: PRETTY FUNCTION";
1585 return (expression_t*) expression;
1589 expression_t *parse_primary_expression(void)
1591 switch(token.type) {
1593 return parse_int_const();
1594 case T_STRING_LITERAL:
1595 return parse_string_const();
1597 return parse_reference();
1598 case T___FUNCTION__:
1599 return parse_function_keyword();
1600 case T___PRETTY_FUNCTION__:
1601 return parse_pretty_function_keyword();
1603 return parse_brace_expression();
1606 parser_print_error_prefix();
1607 fprintf(stderr, "unexpected token ");
1608 print_token(stderr, &token);
1609 fprintf(stderr, "\n");
1615 expression_t *parse_array_expression(unsigned precedence,
1616 expression_t *array_ref)
1622 array_access_expression_t *array_access
1623 = allocate_ast_zero(sizeof(array_access[0]));
1625 array_access->expression.type = EXPR_ARRAY_ACCESS;
1626 array_access->array_ref = array_ref;
1627 array_access->index = parse_expression();
1629 if(token.type != ']') {
1630 parse_error_expected("Problem while parsing array access", ']', 0);
1635 return (expression_t*) array_access;
1639 type_t *get_expression_type(const expression_t *expression)
1647 int is_declaration_specifier(const token_t *token, int only_type_specifiers)
1649 declaration_t *declaration;
1651 switch(token->type) {
1655 declaration = token->v.symbol->declaration;
1656 if(declaration == NULL)
1658 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1663 if(only_type_specifiers)
1673 expression_t *parse_sizeof(unsigned precedence)
1677 sizeof_expression_t *sizeof_expression
1678 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1679 sizeof_expression->expression.type = EXPR_SIZEOF;
1681 if(token.type == '(' && is_declaration_specifier(look_ahead(1), 1)) {
1683 sizeof_expression->type = parse_typename();
1686 expression_t *expression = parse_sub_expression(precedence);
1687 sizeof_expression->type = get_expression_type(expression);
1688 sizeof_expression->size_expression = expression;
1691 return (expression_t*) sizeof_expression;
1695 expression_t *parse_select_expression(unsigned precedence,
1696 expression_t *compound)
1700 assert(token.type == '.' || token.type == T_MINUSGREATER);
1703 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1705 select->expression.type = EXPR_SELECT;
1706 select->compound = compound;
1708 if(token.type != T_IDENTIFIER) {
1709 parse_error_expected("Problem while parsing compound select",
1713 select->symbol = token.v.symbol;
1716 return (expression_t*) select;
1720 expression_t *parse_call_expression(unsigned precedence,
1721 expression_t *expression)
1724 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1726 call->expression.type = EXPR_CALL;
1727 call->method = expression;
1729 /* parse arguments */
1732 if(token.type != ')') {
1733 call_argument_t *last_argument = NULL;
1736 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1738 /* we start parsing at precedence 2 so we don't get comma operators
1740 argument->expression = parse_sub_expression(2);
1741 if(last_argument == NULL) {
1742 call->arguments = argument;
1744 last_argument->next = argument;
1746 last_argument = argument;
1748 if(token.type != ',')
1755 return (expression_t*) call;
1759 expression_t *parse_conditional_expression(unsigned precedence,
1760 expression_t *expression)
1764 conditional_expression_t *conditional
1765 = allocate_ast_zero(sizeof(conditional[0]));
1766 conditional->condition = expression;
1768 conditional->true_expression = parse_expression();
1770 conditional->false_expression = parse_sub_expression(precedence);
1772 return (expression_t*) conditional;
1775 static expression_t *parse_extension(unsigned precedence)
1777 eat(T___extension__);
1779 /* TODO enable extensions */
1781 return parse_sub_expression(precedence);
1784 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1786 expression_t *parse_##unexpression_type(unsigned precedence) \
1790 unary_expression_t *unary_expression \
1791 = allocate_ast_zero(sizeof(unary_expression[0])); \
1792 unary_expression->expression.type = EXPR_UNARY; \
1793 unary_expression->type = unexpression_type; \
1794 unary_expression->value = parse_sub_expression(precedence); \
1796 return (expression_t*) unary_expression; \
1799 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1800 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1801 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1802 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1803 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1804 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1805 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1806 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1808 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1810 expression_t *parse_##unexpression_type(unsigned precedence, \
1811 expression_t *left) \
1813 (void) precedence; \
1816 unary_expression_t *unary_expression \
1817 = allocate_ast_zero(sizeof(unary_expression[0])); \
1818 unary_expression->expression.type = EXPR_UNARY; \
1819 unary_expression->type = unexpression_type; \
1820 unary_expression->value = left; \
1822 return (expression_t*) unary_expression; \
1825 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1826 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1828 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1830 expression_t *parse_##binexpression_type(unsigned precedence, \
1831 expression_t *left) \
1835 expression_t *right = parse_sub_expression(precedence); \
1837 binary_expression_t *binexpr \
1838 = allocate_ast_zero(sizeof(binexpr[0])); \
1839 binexpr->expression.type = EXPR_BINARY; \
1840 binexpr->type = binexpression_type; \
1841 binexpr->left = left; \
1842 binexpr->right = right; \
1844 return (expression_t*) binexpr; \
1847 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
1848 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1849 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1850 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1851 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1852 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1853 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1854 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1855 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1856 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
1857 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1858 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1859 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1860 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1861 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1862 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1863 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1864 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1865 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1866 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
1867 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
1868 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
1869 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
1870 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
1871 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
1872 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
1873 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
1874 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
1875 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
1878 expression_t *parse_sub_expression(unsigned precedence)
1880 if(token.type < 0) {
1881 return expected_expression_error();
1884 expression_parser_function_t *parser
1885 = &expression_parsers[token.type];
1886 source_position_t source_position = token.source_position;
1889 if(parser->parser != NULL) {
1890 left = parser->parser(parser->precedence);
1892 left = parse_primary_expression();
1895 left->source_position = source_position;
1898 if(token.type < 0) {
1899 return expected_expression_error();
1902 parser = &expression_parsers[token.type];
1903 if(parser->infix_parser == NULL)
1905 if(parser->infix_precedence < precedence)
1908 left = parser->infix_parser(parser->infix_precedence, left);
1910 left->source_position = source_position;
1917 expression_t *parse_expression(void)
1919 return parse_sub_expression(1);
1924 void register_expression_parser(parse_expression_function parser,
1925 int token_type, unsigned precedence)
1927 expression_parser_function_t *entry = &expression_parsers[token_type];
1929 if(entry->parser != NULL) {
1930 fprintf(stderr, "for token ");
1931 print_token_type(stderr, token_type);
1932 fprintf(stderr, "\n");
1933 panic("trying to register multiple expression parsers for a token");
1935 entry->parser = parser;
1936 entry->precedence = precedence;
1939 void register_expression_infix_parser(parse_expression_infix_function parser,
1940 int token_type, unsigned precedence)
1942 expression_parser_function_t *entry = &expression_parsers[token_type];
1944 if(entry->infix_parser != NULL) {
1945 fprintf(stderr, "for token ");
1946 print_token_type(stderr, token_type);
1947 fprintf(stderr, "\n");
1948 panic("trying to register multiple infix expression parsers for a "
1951 entry->infix_parser = parser;
1952 entry->infix_precedence = precedence;
1956 void init_expression_parsers(void)
1958 memset(&expression_parsers, 0, sizeof(expression_parsers));
1960 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1961 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1962 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1964 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1965 T_GREATERGREATER, 16);
1966 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1967 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1968 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1969 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1970 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1971 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1972 T_GREATEREQUAL, 14);
1973 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1974 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1975 T_EXCLAMATIONMARKEQUAL, 13);
1976 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1977 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1978 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1979 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1980 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1981 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1982 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
1983 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
1984 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
1985 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
1986 T_ASTERISKEQUAL, 2);
1987 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
1988 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
1990 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
1991 T_LESSLESSEQUAL, 2);
1992 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
1993 T_GREATERGREATEREQUAL, 2);
1994 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
1996 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
1998 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2001 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2003 register_expression_infix_parser(parse_array_expression, '[', 30);
2004 register_expression_infix_parser(parse_call_expression, '(', 30);
2005 register_expression_infix_parser(parse_select_expression, '.', 30);
2006 register_expression_infix_parser(parse_select_expression,
2007 T_MINUSGREATER, 30);
2008 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2010 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2013 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2014 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2015 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2016 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2017 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2018 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2019 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2020 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2021 register_expression_parser(parse_sizeof, T_sizeof, 25);
2022 register_expression_parser(parse_extension, T___extension__, 25);
2027 statement_t *parse_case_statement(void)
2030 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2031 label->statement.type = STATEMENT_CASE_LABEL;
2032 label->statement.source_position = token.source_position;
2034 label->expression = parse_expression();
2037 label->statement.next = parse_statement();
2039 return (statement_t*) label;
2043 statement_t *parse_default_statement(void)
2047 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2048 label->statement.type = STATEMENT_CASE_LABEL;
2049 label->statement.source_position = token.source_position;
2052 label->statement.next = parse_statement();
2054 return (statement_t*) label;
2058 statement_t *parse_label_statement(void)
2068 statement_t *parse_if(void)
2072 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2073 statement->statement.type = STATEMENT_IF;
2074 statement->statement.source_position = token.source_position;
2077 statement->condition = parse_expression();
2080 statement->true_statement = parse_statement();
2081 if(token.type == T_else) {
2083 statement->false_statement = parse_statement();
2086 return (statement_t*) statement;
2090 statement_t *parse_switch(void)
2094 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2095 statement->statement.type = STATEMENT_SWITCH;
2096 statement->statement.source_position = token.source_position;
2099 statement->expression = parse_expression();
2101 statement->body = parse_statement();
2103 return (statement_t*) statement;
2107 statement_t *parse_while(void)
2111 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2112 statement->statement.type = STATEMENT_WHILE;
2113 statement->statement.source_position = token.source_position;
2116 statement->condition = parse_expression();
2118 statement->body = parse_statement();
2120 return (statement_t*) statement;
2124 statement_t *parse_do(void)
2128 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2129 statement->statement.type = STATEMENT_DO_WHILE;
2130 statement->statement.source_position = token.source_position;
2132 statement->body = parse_statement();
2135 statement->condition = parse_expression();
2139 return (statement_t*) statement;
2143 statement_t *parse_for(void)
2147 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2148 statement->statement.type = STATEMENT_FOR;
2149 statement->statement.source_position = token.source_position;
2153 int top = environment_top();
2154 context_t *last_context = context;
2155 set_context(&statement->context);
2157 if(token.type != ';') {
2158 if(is_declaration_specifier(&token, 0)) {
2159 parse_declaration();
2161 statement->initialisation = parse_expression();
2168 if(token.type != ';') {
2169 statement->condition = parse_expression();
2172 if(token.type != ')') {
2173 statement->step = parse_expression();
2176 statement->body = parse_statement();
2178 assert(context == &statement->context);
2179 set_context(last_context);
2180 environment_pop_to(top);
2182 return (statement_t*) statement;
2186 statement_t *parse_goto(void)
2189 expect(T_IDENTIFIER);
2196 statement_t *parse_continue(void)
2201 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2202 statement->source_position = token.source_position;
2203 statement->type = STATEMENT_CONTINUE;
2209 statement_t *parse_break(void)
2214 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2215 statement->source_position = token.source_position;
2216 statement->type = STATEMENT_BREAK;
2222 statement_t *parse_return(void)
2226 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2228 statement->statement.type = STATEMENT_RETURN;
2229 statement->statement.source_position = token.source_position;
2230 if(token.type != ';') {
2231 statement->return_value = parse_expression();
2235 return (statement_t*) statement;
2239 statement_t *parse_declaration_statement(void)
2241 declaration_t *before = last_declaration;
2243 declaration_statement_t *statement
2244 = allocate_ast_zero(sizeof(statement[0]));
2245 statement->statement.type = STATEMENT_DECLARATION;
2246 statement->statement.source_position = token.source_position;
2248 declaration_specifiers_t specifiers;
2249 memset(&specifiers, 0, sizeof(specifiers));
2250 parse_declaration_specifiers(&specifiers);
2252 if(token.type == ';') {
2255 parse_init_declarators(&specifiers);
2258 if(before == NULL) {
2259 statement->declarations_begin = context->declarations;
2261 statement->declarations_begin = before->next;
2263 statement->declarations_end = last_declaration;
2265 return (statement_t*) statement;
2269 statement_t *parse_expression_statement(void)
2271 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2272 statement->statement.type = STATEMENT_EXPRESSION;
2273 statement->statement.source_position = token.source_position;
2275 statement->expression = parse_expression();
2279 return (statement_t*) statement;
2283 statement_t *parse_statement(void)
2285 declaration_t *declaration;
2286 statement_t *statement = NULL;
2288 /* declaration or statement */
2289 switch(token.type) {
2291 statement = parse_case_statement();
2295 statement = parse_default_statement();
2299 statement = parse_compound_statement();
2303 statement = parse_if();
2307 statement = parse_switch();
2311 statement = parse_while();
2315 statement = parse_do();
2319 statement = parse_for();
2323 statement = parse_goto();
2327 statement = parse_continue();
2331 statement = parse_break();
2335 statement = parse_return();
2344 if(look_ahead(1)->type == ':') {
2345 statement = parse_label_statement();
2349 declaration = token.v.symbol->declaration;
2350 if(declaration != NULL &&
2351 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2352 statement = parse_declaration_statement();
2356 statement = parse_expression_statement();
2359 case T___extension__:
2360 /* this can be a prefix to a declaration or an expression statement */
2361 /* we simply eat it now and parse the rest with tail recursion */
2364 } while(token.type == T___extension__);
2365 statement = parse_statement();
2369 statement = parse_declaration_statement();
2373 statement = parse_expression_statement();
2377 assert(statement == NULL || statement->source_position.input_name != NULL);
2383 statement_t *parse_compound_statement(void)
2387 compound_statement_t *compound_statement
2388 = allocate_ast_zero(sizeof(compound_statement[0]));
2389 compound_statement->statement.type = STATEMENT_COMPOUND;
2390 compound_statement->statement.source_position = token.source_position;
2392 int top = environment_top();
2393 context_t *last_context = context;
2394 set_context(&compound_statement->context);
2396 statement_t *last_statement = NULL;
2398 while(token.type != '}' && token.type != T_EOF) {
2399 statement_t *statement = parse_statement();
2400 if(statement == NULL)
2403 if(last_statement != NULL) {
2404 last_statement->next = statement;
2406 compound_statement->statements = statement;
2409 while(statement->next != NULL)
2410 statement = statement->next;
2412 last_statement = statement;
2415 assert(context == &compound_statement->context);
2416 set_context(last_context);
2417 environment_pop_to(top);
2421 return (statement_t*) compound_statement;
2425 translation_unit_t *parse_translation_unit(void)
2427 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2429 assert(context == NULL);
2430 set_context(&unit->context);
2432 while(token.type != T_EOF) {
2433 parse_declaration();
2436 assert(context == &unit->context);
2438 last_declaration = NULL;
2443 translation_unit_t *parse(void)
2445 obstack_init(&environment_obstack);
2446 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2448 type_set_output(stderr);
2450 lookahead_bufpos = 0;
2451 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2454 translation_unit_t *unit = parse_translation_unit();
2456 DEL_ARR_F(environment_stack);
2457 obstack_free(&environment_obstack, NULL);
2462 void init_parser(void)
2464 init_expression_parsers();
2465 obstack_init(&temp_obst);
2468 void exit_parser(void)
2470 obstack_free(&temp_obst, NULL);