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);
1318 if(ndeclaration != declaration) {
1319 memcpy(&declaration->context, &ndeclaration->context,
1320 sizeof(declaration->context));
1323 int top = environment_top();
1324 context_t *last_context = context;
1325 set_context(&declaration->context);
1327 /* push function parameters */
1328 declaration_t *parameter = declaration->context.declarations;
1329 for( ; parameter != NULL; parameter = parameter->next) {
1330 environment_push(parameter, context);
1333 statement_t *statement = parse_compound_statement();
1335 assert(context == &declaration->context);
1336 set_context(last_context);
1337 environment_pop_to(top);
1339 declaration->statement = statement;
1343 if(token.type != ',')
1351 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1354 if(token.type == ':') {
1356 parse_constant_expression();
1357 /* TODO (bitfields) */
1359 declaration_t *declaration
1360 = allocate_ast_zero(sizeof(declaration[0]));
1361 parse_declarator(declaration, specifiers->storage_class,
1362 specifiers->type, 1);
1364 /* TODO: check for doubled fields */
1365 record_declaration(declaration);
1367 if(token.type == ':') {
1369 parse_constant_expression();
1370 /* TODO (bitfields) */
1374 if(token.type != ',')
1381 static void parse_compound_type_entries(void)
1385 while(token.type != '}' && token.type != T_EOF) {
1386 declaration_specifiers_t specifiers;
1387 memset(&specifiers, 0, sizeof(specifiers));
1388 /* TODO not correct as this allows storage class stuff... but only
1389 * specifiers and qualifiers sould be allowed here */
1390 parse_declaration_specifiers(&specifiers);
1392 parse_struct_declarators(&specifiers);
1394 if(token.type == T_EOF) {
1395 parse_error("unexpected error while parsing struct");
1400 void parse_declaration(void)
1402 declaration_specifiers_t specifiers;
1403 memset(&specifiers, 0, sizeof(specifiers));
1404 parse_declaration_specifiers(&specifiers);
1406 if(token.type == ';') {
1410 parse_init_declarators(&specifiers);
1413 type_t *parse_typename(void)
1415 declaration_specifiers_t specifiers;
1416 memset(&specifiers, 0, sizeof(specifiers));
1417 parse_declaration_specifiers(&specifiers);
1418 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1419 /* TODO: improve error message, user does probably not know what a
1420 * storage class is...
1422 parse_error("typename may not have a storage class");
1425 type_t *result = parse_abstract_declarator(specifiers.type);
1433 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1434 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1435 expression_t *left);
1437 typedef struct expression_parser_function_t expression_parser_function_t;
1438 struct expression_parser_function_t {
1439 unsigned precedence;
1440 parse_expression_function parser;
1441 unsigned infix_precedence;
1442 parse_expression_infix_function infix_parser;
1445 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1448 expression_t *expected_expression_error(void)
1450 parser_print_error_prefix();
1451 fprintf(stderr, "expected expression, got token ");
1452 print_token(stderr, & token);
1453 fprintf(stderr, "\n");
1455 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1456 expression->type = EXPR_INVALID;
1463 expression_t *parse_string_const(void)
1465 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1467 cnst->expression.type = EXPR_STRING_LITERAL;
1468 cnst->value = parse_string_literals();
1470 return (expression_t*) cnst;
1474 expression_t *parse_int_const(void)
1476 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1478 cnst->expression.type = EXPR_CONST;
1479 cnst->value = token.v.intvalue;
1483 return (expression_t*) cnst;
1487 expression_t *parse_reference(void)
1489 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1491 ref->expression.type = EXPR_REFERENCE;
1492 ref->symbol = token.v.symbol;
1494 if(ref->symbol->declaration == NULL) {
1495 parser_print_error_prefix();
1496 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1498 ref->declaration = ref->symbol->declaration;
1502 return (expression_t*) ref;
1506 expression_t *parse_cast(void)
1508 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1510 cast->expression.type = EXPR_UNARY;
1511 cast->type = UNEXPR_CAST;
1512 cast->expression.source_position = token.source_position;
1514 type_t *type = parse_typename();
1517 expression_t *value = parse_sub_expression(20);
1519 cast->expression.datatype = type;
1520 cast->value = value;
1522 return (expression_t*) cast;
1526 expression_t *parse_statement_expression(void)
1528 statement_expression_t *expression
1529 = allocate_ast_zero(sizeof(expression[0]));
1530 expression->expression.type = EXPR_STATEMENT;
1531 expression->statement = parse_compound_statement();
1535 return (expression_t*) expression;
1539 expression_t *parse_brace_expression(void)
1543 declaration_t *declaration;
1544 switch(token.type) {
1546 /* gcc extension: a stement expression */
1547 return parse_statement_expression();
1551 return parse_cast();
1553 declaration = token.v.symbol->declaration;
1554 if(declaration != NULL &&
1555 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1556 return parse_cast();
1560 expression_t *result = parse_expression();
1567 expression_t *parse_function_keyword(void)
1569 eat(T___FUNCTION__);
1572 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1573 expression->expression.type = EXPR_FUNCTION;
1574 expression->value = "TODO: FUNCTION";
1576 return (expression_t*) expression;
1580 expression_t *parse_pretty_function_keyword(void)
1582 eat(T___PRETTY_FUNCTION__);
1585 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1586 expression->expression.type = EXPR_PRETTY_FUNCTION;
1587 expression->value = "TODO: PRETTY FUNCTION";
1589 return (expression_t*) expression;
1593 expression_t *parse_primary_expression(void)
1595 switch(token.type) {
1597 return parse_int_const();
1598 case T_STRING_LITERAL:
1599 return parse_string_const();
1601 return parse_reference();
1602 case T___FUNCTION__:
1603 return parse_function_keyword();
1604 case T___PRETTY_FUNCTION__:
1605 return parse_pretty_function_keyword();
1607 return parse_brace_expression();
1610 parser_print_error_prefix();
1611 fprintf(stderr, "unexpected token ");
1612 print_token(stderr, &token);
1613 fprintf(stderr, "\n");
1619 expression_t *parse_array_expression(unsigned precedence,
1620 expression_t *array_ref)
1626 array_access_expression_t *array_access
1627 = allocate_ast_zero(sizeof(array_access[0]));
1629 array_access->expression.type = EXPR_ARRAY_ACCESS;
1630 array_access->array_ref = array_ref;
1631 array_access->index = parse_expression();
1633 if(token.type != ']') {
1634 parse_error_expected("Problem while parsing array access", ']', 0);
1639 return (expression_t*) array_access;
1643 type_t *get_expression_type(const expression_t *expression)
1651 int is_declaration_specifier(const token_t *token, int only_type_specifiers)
1653 declaration_t *declaration;
1655 switch(token->type) {
1659 declaration = token->v.symbol->declaration;
1660 if(declaration == NULL)
1662 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1667 if(only_type_specifiers)
1677 expression_t *parse_sizeof(unsigned precedence)
1681 sizeof_expression_t *sizeof_expression
1682 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1683 sizeof_expression->expression.type = EXPR_SIZEOF;
1685 if(token.type == '(' && is_declaration_specifier(look_ahead(1), 1)) {
1687 sizeof_expression->type = parse_typename();
1690 expression_t *expression = parse_sub_expression(precedence);
1691 sizeof_expression->type = get_expression_type(expression);
1692 sizeof_expression->size_expression = expression;
1695 return (expression_t*) sizeof_expression;
1699 expression_t *parse_select_expression(unsigned precedence,
1700 expression_t *compound)
1704 assert(token.type == '.' || token.type == T_MINUSGREATER);
1707 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1709 select->expression.type = EXPR_SELECT;
1710 select->compound = compound;
1712 if(token.type != T_IDENTIFIER) {
1713 parse_error_expected("Problem while parsing compound select",
1717 select->symbol = token.v.symbol;
1720 return (expression_t*) select;
1724 expression_t *parse_call_expression(unsigned precedence,
1725 expression_t *expression)
1728 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1730 call->expression.type = EXPR_CALL;
1731 call->method = expression;
1733 /* parse arguments */
1736 if(token.type != ')') {
1737 call_argument_t *last_argument = NULL;
1740 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1742 /* we start parsing at precedence 2 so we don't get comma operators
1744 argument->expression = parse_sub_expression(2);
1745 if(last_argument == NULL) {
1746 call->arguments = argument;
1748 last_argument->next = argument;
1750 last_argument = argument;
1752 if(token.type != ',')
1759 return (expression_t*) call;
1763 expression_t *parse_conditional_expression(unsigned precedence,
1764 expression_t *expression)
1768 conditional_expression_t *conditional
1769 = allocate_ast_zero(sizeof(conditional[0]));
1770 conditional->condition = expression;
1772 conditional->true_expression = parse_expression();
1774 conditional->false_expression = parse_sub_expression(precedence);
1776 return (expression_t*) conditional;
1779 static expression_t *parse_extension(unsigned precedence)
1781 eat(T___extension__);
1783 /* TODO enable extensions */
1785 return parse_sub_expression(precedence);
1788 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1790 expression_t *parse_##unexpression_type(unsigned precedence) \
1794 unary_expression_t *unary_expression \
1795 = allocate_ast_zero(sizeof(unary_expression[0])); \
1796 unary_expression->expression.type = EXPR_UNARY; \
1797 unary_expression->type = unexpression_type; \
1798 unary_expression->value = parse_sub_expression(precedence); \
1800 return (expression_t*) unary_expression; \
1803 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1804 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1805 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1806 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1807 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1808 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1809 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1810 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1812 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1814 expression_t *parse_##unexpression_type(unsigned precedence, \
1815 expression_t *left) \
1817 (void) precedence; \
1820 unary_expression_t *unary_expression \
1821 = allocate_ast_zero(sizeof(unary_expression[0])); \
1822 unary_expression->expression.type = EXPR_UNARY; \
1823 unary_expression->type = unexpression_type; \
1824 unary_expression->value = left; \
1826 return (expression_t*) unary_expression; \
1829 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1830 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1832 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1834 expression_t *parse_##binexpression_type(unsigned precedence, \
1835 expression_t *left) \
1839 expression_t *right = parse_sub_expression(precedence); \
1841 binary_expression_t *binexpr \
1842 = allocate_ast_zero(sizeof(binexpr[0])); \
1843 binexpr->expression.type = EXPR_BINARY; \
1844 binexpr->type = binexpression_type; \
1845 binexpr->left = left; \
1846 binexpr->right = right; \
1848 return (expression_t*) binexpr; \
1851 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
1852 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1853 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1854 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1855 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1856 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1857 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1858 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1859 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1860 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
1861 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1862 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1863 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1864 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1865 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1866 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1867 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1868 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1869 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1870 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
1871 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
1872 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
1873 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
1874 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
1875 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
1876 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
1877 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
1878 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
1879 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
1882 expression_t *parse_sub_expression(unsigned precedence)
1884 if(token.type < 0) {
1885 return expected_expression_error();
1888 expression_parser_function_t *parser
1889 = &expression_parsers[token.type];
1890 source_position_t source_position = token.source_position;
1893 if(parser->parser != NULL) {
1894 left = parser->parser(parser->precedence);
1896 left = parse_primary_expression();
1899 left->source_position = source_position;
1902 if(token.type < 0) {
1903 return expected_expression_error();
1906 parser = &expression_parsers[token.type];
1907 if(parser->infix_parser == NULL)
1909 if(parser->infix_precedence < precedence)
1912 left = parser->infix_parser(parser->infix_precedence, left);
1914 left->source_position = source_position;
1921 expression_t *parse_expression(void)
1923 return parse_sub_expression(1);
1928 void register_expression_parser(parse_expression_function parser,
1929 int token_type, unsigned precedence)
1931 expression_parser_function_t *entry = &expression_parsers[token_type];
1933 if(entry->parser != NULL) {
1934 fprintf(stderr, "for token ");
1935 print_token_type(stderr, token_type);
1936 fprintf(stderr, "\n");
1937 panic("trying to register multiple expression parsers for a token");
1939 entry->parser = parser;
1940 entry->precedence = precedence;
1943 void register_expression_infix_parser(parse_expression_infix_function parser,
1944 int token_type, unsigned precedence)
1946 expression_parser_function_t *entry = &expression_parsers[token_type];
1948 if(entry->infix_parser != NULL) {
1949 fprintf(stderr, "for token ");
1950 print_token_type(stderr, token_type);
1951 fprintf(stderr, "\n");
1952 panic("trying to register multiple infix expression parsers for a "
1955 entry->infix_parser = parser;
1956 entry->infix_precedence = precedence;
1960 void init_expression_parsers(void)
1962 memset(&expression_parsers, 0, sizeof(expression_parsers));
1964 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1965 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1966 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1968 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1969 T_GREATERGREATER, 16);
1970 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1971 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1972 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1973 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1974 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1975 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1976 T_GREATEREQUAL, 14);
1977 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1978 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1979 T_EXCLAMATIONMARKEQUAL, 13);
1980 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1981 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1982 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1983 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1984 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1985 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1986 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
1987 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
1988 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
1989 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
1990 T_ASTERISKEQUAL, 2);
1991 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
1992 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
1994 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
1995 T_LESSLESSEQUAL, 2);
1996 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
1997 T_GREATERGREATEREQUAL, 2);
1998 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2000 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2002 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2005 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2007 register_expression_infix_parser(parse_array_expression, '[', 30);
2008 register_expression_infix_parser(parse_call_expression, '(', 30);
2009 register_expression_infix_parser(parse_select_expression, '.', 30);
2010 register_expression_infix_parser(parse_select_expression,
2011 T_MINUSGREATER, 30);
2012 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2014 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2017 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2018 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2019 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2020 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2021 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2022 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2023 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2024 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2025 register_expression_parser(parse_sizeof, T_sizeof, 25);
2026 register_expression_parser(parse_extension, T___extension__, 25);
2031 statement_t *parse_case_statement(void)
2034 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2035 label->statement.type = STATEMENT_CASE_LABEL;
2036 label->statement.source_position = token.source_position;
2038 label->expression = parse_expression();
2041 label->statement.next = parse_statement();
2043 return (statement_t*) label;
2047 statement_t *parse_default_statement(void)
2051 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2052 label->statement.type = STATEMENT_CASE_LABEL;
2053 label->statement.source_position = token.source_position;
2056 label->statement.next = parse_statement();
2058 return (statement_t*) label;
2062 statement_t *parse_label_statement(void)
2072 statement_t *parse_if(void)
2076 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2077 statement->statement.type = STATEMENT_IF;
2078 statement->statement.source_position = token.source_position;
2081 statement->condition = parse_expression();
2084 statement->true_statement = parse_statement();
2085 if(token.type == T_else) {
2087 statement->false_statement = parse_statement();
2090 return (statement_t*) statement;
2094 statement_t *parse_switch(void)
2098 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2099 statement->statement.type = STATEMENT_SWITCH;
2100 statement->statement.source_position = token.source_position;
2103 statement->expression = parse_expression();
2105 statement->body = parse_statement();
2107 return (statement_t*) statement;
2111 statement_t *parse_while(void)
2115 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2116 statement->statement.type = STATEMENT_WHILE;
2117 statement->statement.source_position = token.source_position;
2120 statement->condition = parse_expression();
2122 statement->body = parse_statement();
2124 return (statement_t*) statement;
2128 statement_t *parse_do(void)
2132 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2133 statement->statement.type = STATEMENT_DO_WHILE;
2134 statement->statement.source_position = token.source_position;
2136 statement->body = parse_statement();
2139 statement->condition = parse_expression();
2143 return (statement_t*) statement;
2147 statement_t *parse_for(void)
2151 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2152 statement->statement.type = STATEMENT_FOR;
2153 statement->statement.source_position = token.source_position;
2157 int top = environment_top();
2158 context_t *last_context = context;
2159 set_context(&statement->context);
2161 if(token.type != ';') {
2162 if(is_declaration_specifier(&token, 0)) {
2163 parse_declaration();
2165 statement->initialisation = parse_expression();
2172 if(token.type != ';') {
2173 statement->condition = parse_expression();
2176 if(token.type != ')') {
2177 statement->step = parse_expression();
2180 statement->body = parse_statement();
2182 assert(context == &statement->context);
2183 set_context(last_context);
2184 environment_pop_to(top);
2186 return (statement_t*) statement;
2190 statement_t *parse_goto(void)
2193 expect(T_IDENTIFIER);
2200 statement_t *parse_continue(void)
2205 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2206 statement->source_position = token.source_position;
2207 statement->type = STATEMENT_CONTINUE;
2213 statement_t *parse_break(void)
2218 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2219 statement->source_position = token.source_position;
2220 statement->type = STATEMENT_BREAK;
2226 statement_t *parse_return(void)
2230 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2232 statement->statement.type = STATEMENT_RETURN;
2233 statement->statement.source_position = token.source_position;
2234 if(token.type != ';') {
2235 statement->return_value = parse_expression();
2239 return (statement_t*) statement;
2243 statement_t *parse_declaration_statement(void)
2245 declaration_t *before = last_declaration;
2247 declaration_statement_t *statement
2248 = allocate_ast_zero(sizeof(statement[0]));
2249 statement->statement.type = STATEMENT_DECLARATION;
2250 statement->statement.source_position = token.source_position;
2252 declaration_specifiers_t specifiers;
2253 memset(&specifiers, 0, sizeof(specifiers));
2254 parse_declaration_specifiers(&specifiers);
2256 if(token.type == ';') {
2259 parse_init_declarators(&specifiers);
2262 if(before == NULL) {
2263 statement->declarations_begin = context->declarations;
2265 statement->declarations_begin = before->next;
2267 statement->declarations_end = last_declaration;
2269 return (statement_t*) statement;
2273 statement_t *parse_expression_statement(void)
2275 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2276 statement->statement.type = STATEMENT_EXPRESSION;
2277 statement->statement.source_position = token.source_position;
2279 statement->expression = parse_expression();
2283 return (statement_t*) statement;
2287 statement_t *parse_statement(void)
2289 declaration_t *declaration;
2290 statement_t *statement = NULL;
2292 /* declaration or statement */
2293 switch(token.type) {
2295 statement = parse_case_statement();
2299 statement = parse_default_statement();
2303 statement = parse_compound_statement();
2307 statement = parse_if();
2311 statement = parse_switch();
2315 statement = parse_while();
2319 statement = parse_do();
2323 statement = parse_for();
2327 statement = parse_goto();
2331 statement = parse_continue();
2335 statement = parse_break();
2339 statement = parse_return();
2348 if(look_ahead(1)->type == ':') {
2349 statement = parse_label_statement();
2353 declaration = token.v.symbol->declaration;
2354 if(declaration != NULL &&
2355 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2356 statement = parse_declaration_statement();
2360 statement = parse_expression_statement();
2363 case T___extension__:
2364 /* this can be a prefix to a declaration or an expression statement */
2365 /* we simply eat it now and parse the rest with tail recursion */
2368 } while(token.type == T___extension__);
2369 statement = parse_statement();
2373 statement = parse_declaration_statement();
2377 statement = parse_expression_statement();
2381 assert(statement == NULL || statement->source_position.input_name != NULL);
2387 statement_t *parse_compound_statement(void)
2391 compound_statement_t *compound_statement
2392 = allocate_ast_zero(sizeof(compound_statement[0]));
2393 compound_statement->statement.type = STATEMENT_COMPOUND;
2394 compound_statement->statement.source_position = token.source_position;
2396 int top = environment_top();
2397 context_t *last_context = context;
2398 set_context(&compound_statement->context);
2400 statement_t *last_statement = NULL;
2402 while(token.type != '}' && token.type != T_EOF) {
2403 statement_t *statement = parse_statement();
2404 if(statement == NULL)
2407 if(last_statement != NULL) {
2408 last_statement->next = statement;
2410 compound_statement->statements = statement;
2413 while(statement->next != NULL)
2414 statement = statement->next;
2416 last_statement = statement;
2419 assert(context == &compound_statement->context);
2420 set_context(last_context);
2421 environment_pop_to(top);
2425 return (statement_t*) compound_statement;
2429 translation_unit_t *parse_translation_unit(void)
2431 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2433 assert(context == NULL);
2434 set_context(&unit->context);
2436 while(token.type != T_EOF) {
2437 parse_declaration();
2440 assert(context == &unit->context);
2442 last_declaration = NULL;
2447 translation_unit_t *parse(void)
2449 obstack_init(&environment_obstack);
2450 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2452 type_set_output(stderr);
2454 lookahead_bufpos = 0;
2455 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2458 translation_unit_t *unit = parse_translation_unit();
2460 DEL_ARR_F(environment_stack);
2461 obstack_free(&environment_obstack, NULL);
2466 void init_parser(void)
2468 init_expression_parsers();
2469 obstack_init(&temp_obst);
2472 void exit_parser(void)
2474 obstack_free(&temp_obst, NULL);