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 *la(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");
156 void eat_until(int token_type)
158 while(token.type != token_type) {
159 if(token.type == T_EOF)
166 #define expect(expected) \
167 if(UNLIKELY(token.type != (expected))) { \
168 parse_error_expected(NULL, (expected), 0); \
174 #define expect_void(expected) \
175 if(UNLIKELY(token.type != (expected))) { \
176 parse_error_expected(NULL, (expected), 0); \
182 static void set_context(context_t *new_context)
184 context = new_context;
186 declaration_t *declaration = new_context->declarations;
187 if(declaration != NULL) {
189 if(declaration->next == NULL)
191 declaration = declaration->next;
195 last_declaration = declaration;
199 * pushs an environment_entry on the environment stack and links the
200 * corresponding symbol to the new entry
203 void environment_push(declaration_t *declaration, const void *context)
205 environment_entry_t *entry
206 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
207 memset(entry, 0, sizeof(entry[0]));
209 int top = ARR_LEN(environment_stack);
210 ARR_RESIZE(environment_stack, top + 1);
211 environment_stack[top] = entry;
213 assert(declaration->source_position.input_name != NULL);
215 symbol_t *symbol = declaration->symbol;
216 assert(declaration != symbol->declaration);
218 if(symbol->context == context) {
219 if(symbol->declaration != NULL) {
220 assert(symbol->declaration != NULL);
221 parser_print_error_prefix_pos(declaration->source_position);
222 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
224 parser_print_error_prefix_pos(symbol->declaration->source_position);
225 fprintf(stderr, "this is the location of the previous declaration.\n");
229 entry->old_declaration = symbol->declaration;
230 entry->old_context = symbol->context;
231 entry->symbol = symbol;
232 symbol->declaration = declaration;
233 symbol->context = context;
237 * pops symbols from the environment stack until @p new_top is the top element
240 void environment_pop_to(size_t new_top)
242 environment_entry_t *entry = NULL;
243 size_t top = ARR_LEN(environment_stack);
249 assert(new_top < top);
252 entry = environment_stack[i - 1];
254 symbol_t *symbol = entry->symbol;
256 symbol->declaration = entry->old_declaration;
257 symbol->context = entry->old_context;
260 } while(i != new_top);
261 obstack_free(&environment_obstack, entry);
263 ARR_SHRINKLEN(environment_stack, (int) new_top);
268 static expression_t *parse_constant_expression(void)
270 /* TODO: not correct yet */
271 return parse_expression();
274 static expression_t *parse_assignment_expression(void)
276 /* TODO: not correct yet */
277 return parse_expression();
280 static void parse_compound_type_entries(void);
281 static void parse_declarator(declaration_t *declaration,
282 storage_class_t storage_class, type_t *type,
283 int may_be_abstract);
284 static void maybe_push_declaration(declaration_t *declaration);
285 static void record_declaration(declaration_t *declaration);
287 typedef struct declaration_specifiers_t declaration_specifiers_t;
288 struct declaration_specifiers_t {
289 storage_class_t storage_class;
293 static type_t *parse_struct_specifier(void)
297 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
298 struct_type->type.type = TYPE_COMPOUND_STRUCT;
299 struct_type->source_position = token.source_position;
301 int top = environment_top();
302 context_t *last_context = context;
303 set_context(&struct_type->context);
305 if(token.type == T_IDENTIFIER) {
307 if(token.type == '{') {
308 parse_compound_type_entries();
310 } else if(token.type == '{') {
311 parse_compound_type_entries();
313 parse_error_expected("problem while parsing struct type specifiers",
314 T_IDENTIFIER, '{', 0);
318 assert(context == &struct_type->context);
319 set_context(last_context);
320 environment_pop_to(top);
322 return (type_t*) struct_type;
325 static type_t *parse_union_specifier(void)
329 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
330 union_type->type.type = TYPE_COMPOUND_UNION;
331 union_type->source_position = token.source_position;
333 int top = environment_top();
334 context_t *last_context = context;
335 set_context(&union_type->context);
337 if(token.type == T_IDENTIFIER) {
338 union_type->symbol = token.v.symbol;
340 if(token.type == '{') {
341 parse_compound_type_entries();
343 } else if(token.type == '{') {
344 parse_compound_type_entries();
346 parse_error_expected("problem while parsing union type specifiers",
351 assert(context == &union_type->context);
352 set_context(last_context);
353 environment_pop_to(top);
355 return (type_t*) union_type;
358 static enum_entry_t *parse_enum_type_entries(void)
362 if(token.type == '}') {
364 parse_error("empty enum not allowed");
368 enum_entry_t *result = NULL;
369 enum_entry_t *last_entry = NULL;
371 enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
372 if(token.type != T_IDENTIFIER) {
373 parse_error_expected("problem while parsing enum entry",
378 entry->symbol = token.v.symbol;
381 if(token.type == '=') {
383 entry->value = parse_constant_expression();
386 if(last_entry != NULL) {
387 last_entry->next = entry;
393 if(token.type != ',')
396 } while(token.type != '}');
402 static type_t *parse_enum_specifier(void)
406 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
407 enum_type->type.type = TYPE_ENUM;
408 enum_type->source_position = token.source_position;
410 if(token.type == T_IDENTIFIER) {
411 enum_type->symbol = token.v.symbol;
413 if(token.type == '{') {
414 enum_type->entries = parse_enum_type_entries();
416 } else if(token.type == '{') {
417 enum_type->entries = parse_enum_type_entries();
419 parse_error_expected("problem while parsing enum type specifiers",
423 return (type_t*) enum_type;
427 const char *parse_string_literals(void)
429 assert(token.type == T_STRING_LITERAL);
430 const char *result = token.v.string;
434 while(token.type == T_STRING_LITERAL) {
435 result = concat_strings(result, token.v.string);
443 void parse_attributes(void)
447 case T___attribute__:
455 parse_error("EOF while parsing attribute");
473 if(token.type != T_STRING_LITERAL) {
474 parse_error_expected("while parsing assembler attribute",
479 parse_string_literals();
484 goto attributes_finished;
493 SPECIFIER_SIGNED = 1 << 0,
494 SPECIFIER_UNSIGNED = 1 << 1,
495 SPECIFIER_LONG = 1 << 2,
496 SPECIFIER_INT = 1 << 3,
497 SPECIFIER_DOUBLE = 1 << 4,
498 SPECIFIER_CHAR = 1 << 5,
499 SPECIFIER_SHORT = 1 << 6,
500 SPECIFIER_LONG_LONG = 1 << 7,
501 SPECIFIER_FLOAT = 1 << 8,
502 SPECIFIER_BOOL = 1 << 9,
503 SPECIFIER_VOID = 1 << 10,
504 #ifdef PROVIDE_COMPLEX
505 SPECIFIER_COMPLEX = 1 << 11,
507 #ifdef PROVIDE_IMAGINARY
508 SPECIFIER_IMAGINARY = 1 << 12,
512 #define STORAGE_CLASSES \
519 #define TYPE_QUALIFIERS \
524 case T___extension__:
526 #ifdef PROVIDE_COMPLEX
527 #define COMPLEX_SPECIFIERS \
530 #define COMPLEX_SPECIFIERS
533 #ifdef PROVIDE_IMAGINARY
534 #define IMAGINARY_SPECIFIERS \
537 #define IMAGINARY_SPECIFIERS
540 #define TYPE_SPECIFIERS \
557 #define DECLARATION_START \
563 type_t *create_builtin_type(symbol_t *symbol)
565 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
566 type->type.type = TYPE_BUILTIN;
567 type->symbol = symbol;
569 type_t *result = typehash_insert((type_t*) type);
570 if(result != (type_t*) type) {
571 obstack_free(type_obst, type);
578 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
580 declaration_t *declaration;
582 unsigned type_qualifiers = 0;
583 unsigned type_specifiers = 0;
590 #define MATCH_STORAGE_CLASS(token, class) \
592 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
593 parse_error("multiple storage classes in declaration " \
596 specifiers->storage_class = class; \
600 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
601 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
602 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
603 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
604 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
606 /* type qualifiers */
607 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
609 type_qualifiers |= qualifier; \
613 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
614 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
615 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
616 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
618 case T___extension__:
623 /* type specifiers */
624 #define MATCH_SPECIFIER(token, specifier, name) \
627 if(type_specifiers & specifier) { \
628 parse_error("multiple " name " type specifiers given"); \
630 type_specifiers |= specifier; \
634 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
635 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
636 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
637 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
638 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
639 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
640 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
641 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
642 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
643 #ifdef PROVIDE_COMPLEX
644 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
646 #ifdef PROVIDE_IMAGINARY
647 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
651 if(type_specifiers & SPECIFIER_LONG_LONG) {
652 parse_error("multiple type specifiers given");
653 } else if(type_specifiers & SPECIFIER_LONG) {
654 type_specifiers |= SPECIFIER_LONG_LONG;
656 type_specifiers |= SPECIFIER_LONG;
660 /* TODO: if type != NULL for the following rules issue an error */
662 type = parse_struct_specifier();
665 type = parse_union_specifier();
668 type = parse_enum_specifier();
670 case T___builtin_va_list:
671 type = create_builtin_type(token.v.symbol);
675 case T___attribute__:
681 declaration = token.v.symbol->declaration;
682 if(declaration == NULL ||
683 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
684 goto finish_specifiers;
687 type = declaration->type;
688 assert(type != NULL);
692 /* function specifier */
694 goto finish_specifiers;
701 atomic_type_type_t atomic_type;
703 /* match valid basic types */
704 switch(type_specifiers) {
706 atomic_type = ATOMIC_TYPE_VOID;
709 atomic_type = ATOMIC_TYPE_CHAR;
711 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
712 atomic_type = ATOMIC_TYPE_SCHAR;
714 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
715 atomic_type = ATOMIC_TYPE_UCHAR;
717 case SPECIFIER_SHORT:
718 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
719 case SPECIFIER_SHORT | SPECIFIER_INT:
720 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
721 atomic_type = ATOMIC_TYPE_SHORT;
723 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
724 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
725 atomic_type = ATOMIC_TYPE_USHORT;
728 case SPECIFIER_SIGNED:
729 case SPECIFIER_SIGNED | SPECIFIER_INT:
730 atomic_type = ATOMIC_TYPE_INT;
732 case SPECIFIER_UNSIGNED:
733 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
734 atomic_type = ATOMIC_TYPE_UINT;
737 case SPECIFIER_SIGNED | SPECIFIER_LONG:
738 case SPECIFIER_LONG | SPECIFIER_INT:
739 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
740 atomic_type = ATOMIC_TYPE_LONG;
742 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
743 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
744 atomic_type = ATOMIC_TYPE_ULONG;
746 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
747 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
748 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
749 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
751 atomic_type = ATOMIC_TYPE_LONGLONG;
753 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
754 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
756 atomic_type = ATOMIC_TYPE_ULONGLONG;
758 case SPECIFIER_FLOAT:
759 atomic_type = ATOMIC_TYPE_FLOAT;
761 case SPECIFIER_DOUBLE:
762 atomic_type = ATOMIC_TYPE_DOUBLE;
764 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
765 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
768 atomic_type = ATOMIC_TYPE_BOOL;
770 #ifdef PROVIDE_COMPLEX
771 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
772 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
774 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
775 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
777 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
778 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
781 #ifdef PROVIDE_IMAGINARY
782 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
783 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
785 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
786 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
788 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
789 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
793 /* invalid specifier combination, give an error message */
794 if(type_specifiers == 0) {
795 parse_error("no type specifiers given in declaration");
796 } else if((type_specifiers & SPECIFIER_SIGNED) &&
797 (type_specifiers & SPECIFIER_UNSIGNED)) {
798 parse_error("signed and unsigned specifiers gives");
799 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
800 parse_error("only integer types can be signed or unsigned");
802 parse_error("multiple datatypes in declaration");
804 atomic_type = ATOMIC_TYPE_INVALID;
807 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
808 atype->type.type = TYPE_ATOMIC;
809 atype->atype = atomic_type;
812 type = (type_t*) atype;
814 if(type_specifiers != 0) {
815 parse_error("multiple datatypes in declaration");
819 type->qualifiers = type_qualifiers;
821 type_t *result = typehash_insert(type);
822 if(newtype && result != (type_t*) type) {
823 obstack_free(type_obst, type);
826 specifiers->type = result;
830 unsigned parse_type_qualifiers(void)
832 unsigned type_qualifiers = 0;
836 /* type qualifiers */
837 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
838 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
839 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
840 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
843 return type_qualifiers;
848 typedef struct parsed_pointer_t parsed_pointer_t;
849 struct parsed_pointer_t {
850 unsigned type_qualifiers;
851 parsed_pointer_t *next;
855 parsed_pointer_t *parse_pointers(void)
857 parsed_pointer_t *result = NULL;
858 parsed_pointer_t *last_pointer = NULL;
860 while(token.type == '*') {
862 parsed_pointer_t *pointer
863 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
864 pointer->type_qualifiers = parse_type_qualifiers();
866 if(last_pointer != NULL) {
867 last_pointer->next = pointer;
871 last_pointer = pointer;
878 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
880 for( ; pointer != NULL; pointer = pointer->next) {
881 pointer_type_t *pointer_type
882 = allocate_type_zero(sizeof(pointer_type[0]));
883 pointer_type->type.type = TYPE_POINTER;
884 pointer_type->points_to = type;
885 pointer_type->type.qualifiers = pointer->type_qualifiers;
887 type_t *result = typehash_insert((type_t*) pointer_type);
888 if(result != (type_t*) pointer_type) {
889 obstack_free(type_obst, pointer_type);
899 void parse_identifier_list(void)
902 if(token.type != T_IDENTIFIER) {
903 parse_error_expected("problem while parsing parameter identifier "
904 "list", T_IDENTIFIER, 0);
908 if(token.type != ',')
915 declaration_t *parse_parameter(void)
917 declaration_specifiers_t specifiers;
918 memset(&specifiers, 0, sizeof(specifiers));
920 parse_declaration_specifiers(&specifiers);
922 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
923 parse_declarator(declaration, specifiers.storage_class,
930 void parse_parameters(method_type_t *type)
932 if(token.type == T_IDENTIFIER) {
933 symbol_t *symbol = token.v.symbol;
934 declaration_t *declaration = symbol->declaration;
935 if(declaration == NULL
936 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
937 parse_identifier_list();
942 if(token.type == ')') {
943 type->unspecified_parameters = 1;
946 if(token.type == T_void && la(1)->type == ')') {
951 declaration_t *parameter;
952 declaration_t *last_parameter = NULL;
963 parameter = parse_parameter();
965 if(last_parameter != NULL) {
966 last_parameter->next = parameter;
968 type->parameters = parameter;
970 last_parameter = parameter;
976 if(token.type != ',')
982 typedef struct declarator_part declarator_part;
983 struct declarator_part {
984 parsed_pointer_t *pointers;
985 method_type_t *method_type;
986 declarator_part *inner;
991 declarator_part *parse_inner_declarator(declaration_t *declaration,
994 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
995 memset(part, 0, sizeof(part[0]));
997 part->pointers = parse_pointers();
999 /* TODO: find out if this is correct */
1002 switch(token.type) {
1004 if(declaration == NULL) {
1005 parse_error("no identifier expected in typename");
1007 declaration->symbol = token.v.symbol;
1008 declaration->source_position = token.source_position;
1014 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1020 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1025 switch(token.type) {
1029 method_type_t *method_type
1030 = allocate_type_zero(sizeof(method_type[0]));
1031 method_type->type.type = TYPE_METHOD;
1033 parse_parameters(method_type);
1035 part->method_type = method_type;
1042 if(token.type == T_static) {
1046 unsigned type_qualifiers = parse_type_qualifiers();
1047 if(type_qualifiers != 0) {
1048 if(token.type == T_static) {
1055 if(token.type == '*' && la(1)->type == ']') {
1057 } else if(token.type != ']') {
1058 parse_assignment_expression();
1064 goto declarator_finished;
1068 declarator_finished:
1075 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1078 type = make_pointers(type, part->pointers);
1080 method_type_t *method_type = part->method_type;
1081 if(method_type != NULL) {
1082 method_type->result_type = type;
1084 type = (type_t*) method_type;
1088 } while(part != NULL);
1094 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1095 type_t *type, int may_be_abstract)
1097 declarator_part *part
1098 = parse_inner_declarator(declaration, may_be_abstract);
1101 declaration->type = construct_declarator_type(part, type);
1102 declaration->storage_class = storage_class;
1103 obstack_free(&temp_obst, part);
1108 type_t *parse_abstract_declarator(type_t *base_type)
1110 declarator_part *part = parse_inner_declarator(NULL, 1);
1112 type_t *result = construct_declarator_type(part, base_type);
1113 obstack_free(&temp_obst, part);
1118 static void record_declaration(declaration_t *declaration)
1120 if(last_declaration != NULL) {
1121 last_declaration->next = declaration;
1124 context->declarations = declaration;
1126 last_declaration = declaration;
1130 void maybe_push_declaration(declaration_t *declaration)
1132 symbol_t *symbol = declaration->symbol;
1134 if(symbol != NULL) {
1135 environment_push(declaration, context);
1140 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1145 parse_declarator(declaration, specifiers->storage_class,
1146 specifiers->type, 0);
1147 maybe_push_declaration(declaration);
1148 record_declaration(declaration);
1149 if(token.type == '=') {
1151 if(token.type == '{') {
1155 parse_assignment_expression();
1157 } else if(token.type == '{') {
1158 statement_t *statement = parse_compound_statement();
1159 declaration->statement = statement;
1163 if(token.type != ',')
1171 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1174 if(token.type == ':') {
1176 parse_constant_expression();
1177 /* TODO (bitfields) */
1179 declaration_t *declaration
1180 = allocate_ast_zero(sizeof(declaration[0]));
1181 parse_declarator(declaration, specifiers->storage_class,
1182 specifiers->type, 0);
1183 maybe_push_declaration(declaration);
1184 record_declaration(declaration);
1186 if(token.type == ':') {
1188 parse_constant_expression();
1189 /* TODO (bitfields) */
1193 if(token.type != ',')
1200 static void parse_compound_type_entries(void)
1204 while(token.type != '}' && token.type != T_EOF) {
1205 declaration_specifiers_t specifiers;
1206 memset(&specifiers, 0, sizeof(specifiers));
1207 /* TODO not correct as this allows storage class stuff... but only
1208 * specifiers and qualifiers sould be allowed here */
1209 parse_declaration_specifiers(&specifiers);
1211 parse_struct_declarators(&specifiers);
1213 if(token.type == T_EOF) {
1214 parse_error("unexpected error while parsing struct");
1219 void parse_declaration(void)
1221 declaration_specifiers_t specifiers;
1222 memset(&specifiers, 0, sizeof(specifiers));
1223 parse_declaration_specifiers(&specifiers);
1225 if(token.type == ';') {
1229 parse_init_declarators(&specifiers);
1232 type_t *parse_typename(void)
1234 declaration_specifiers_t specifiers;
1235 memset(&specifiers, 0, sizeof(specifiers));
1236 parse_declaration_specifiers(&specifiers);
1237 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1238 /* TODO: improve error message, user does probably not know what a
1239 * storage class is...
1241 parse_error("typename may not have a storage class");
1244 type_t *result = parse_abstract_declarator(specifiers.type);
1252 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1253 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1254 expression_t *left);
1256 typedef struct expression_parser_function_t expression_parser_function_t;
1257 struct expression_parser_function_t {
1258 unsigned precedence;
1259 parse_expression_function parser;
1260 unsigned infix_precedence;
1261 parse_expression_infix_function infix_parser;
1264 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1267 expression_t *expected_expression_error(void)
1269 parser_print_error_prefix();
1270 fprintf(stderr, "expected expression, got token ");
1271 print_token(stderr, & token);
1272 fprintf(stderr, "\n");
1274 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1275 expression->type = EXPR_INVALID;
1282 expression_t *parse_string_const(void)
1284 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1286 cnst->expression.type = EXPR_STRING_LITERAL;
1287 cnst->value = parse_string_literals();
1289 return (expression_t*) cnst;
1293 expression_t *parse_int_const(void)
1295 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1297 cnst->expression.type = EXPR_CONST;
1298 cnst->value = token.v.intvalue;
1302 return (expression_t*) cnst;
1306 expression_t *parse_reference(void)
1308 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1310 ref->expression.type = EXPR_REFERENCE;
1311 ref->symbol = token.v.symbol;
1315 return (expression_t*) ref;
1319 expression_t *parse_cast(void)
1321 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1323 cast->expression.type = EXPR_UNARY;
1324 cast->type = UNEXPR_CAST;
1325 cast->expression.source_position = token.source_position;
1327 type_t *type = parse_typename();
1330 expression_t *value = parse_sub_expression(20);
1332 cast->expression.datatype = type;
1333 cast->value = value;
1335 return (expression_t*) cast;
1339 expression_t *parse_brace_expression(void)
1343 declaration_t *declaration;
1344 switch(token.type) {
1347 return parse_cast();
1349 declaration = token.v.symbol->declaration;
1350 if(declaration != NULL &&
1351 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1352 return parse_cast();
1356 expression_t *result = parse_expression();
1363 expression_t *parse_primary_expression(void)
1365 switch(token.type) {
1367 return parse_int_const();
1368 case T_STRING_LITERAL:
1369 return parse_string_const();
1371 return parse_reference();
1373 return parse_brace_expression();
1376 /* TODO: error message */
1381 expression_t *parse_array_expression(unsigned precedence,
1382 expression_t *array_ref)
1388 array_access_expression_t *array_access
1389 = allocate_ast_zero(sizeof(array_access[0]));
1391 array_access->expression.type = EXPR_ARRAY_ACCESS;
1392 array_access->array_ref = array_ref;
1393 array_access->index = parse_expression();
1395 if(token.type != ']') {
1396 parse_error_expected("Problem while parsing array access", ']', 0);
1401 return (expression_t*) array_access;
1405 type_t *get_expression_type(const expression_t *expression)
1413 expression_t *parse_sizeof(unsigned precedence)
1417 sizeof_expression_t *sizeof_expression
1418 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1419 sizeof_expression->expression.type = EXPR_SIZEOF;
1421 if(token.type == '(' /* && LA1 is type_specifier */) {
1423 sizeof_expression->type = parse_typename();
1426 expression_t *expression = parse_sub_expression(precedence);
1427 sizeof_expression->type = get_expression_type(expression);
1430 return (expression_t*) sizeof_expression;
1434 expression_t *parse_select_expression(unsigned precedence,
1435 expression_t *compound)
1439 assert(token.type == '.' || token.type == T_SELECT);
1442 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1444 select->expression.type = EXPR_SELECT;
1445 select->compound = compound;
1447 if(token.type != T_IDENTIFIER) {
1448 parse_error_expected("Problem while parsing compound select",
1452 select->symbol = token.v.symbol;
1455 return (expression_t*) select;
1459 expression_t *parse_call_expression(unsigned precedence,
1460 expression_t *expression)
1463 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1465 call->expression.type = EXPR_CALL;
1466 call->method = expression;
1468 /* parse arguments */
1471 if(token.type != ')') {
1472 call_argument_t *last_argument = NULL;
1475 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1477 argument->expression = parse_expression();
1478 if(last_argument == NULL) {
1479 call->arguments = argument;
1481 last_argument->next = argument;
1483 last_argument = argument;
1485 if(token.type != ',')
1492 return (expression_t*) call;
1496 expression_t *parse_conditional_expression(unsigned precedence,
1497 expression_t *expression)
1501 conditional_expression_t *conditional
1502 = allocate_ast_zero(sizeof(conditional[0]));
1503 conditional->condition = expression;
1505 conditional->true_expression = parse_expression();
1507 conditional->false_expression = parse_sub_expression(precedence);
1509 return (expression_t*) conditional;
1512 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1514 expression_t *parse_##unexpression_type(unsigned precedence) \
1518 unary_expression_t *unary_expression \
1519 = allocate_ast_zero(sizeof(unary_expression[0])); \
1520 unary_expression->expression.type = EXPR_UNARY; \
1521 unary_expression->type = unexpression_type; \
1522 unary_expression->value = parse_sub_expression(precedence); \
1524 return (expression_t*) unary_expression; \
1527 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1528 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1529 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1530 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1531 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1532 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1533 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1534 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1536 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1538 expression_t *parse_##unexpression_type(unsigned precedence, \
1539 expression_t *left) \
1541 (void) precedence; \
1544 unary_expression_t *unary_expression \
1545 = allocate_ast_zero(sizeof(unary_expression[0])); \
1546 unary_expression->expression.type = EXPR_UNARY; \
1547 unary_expression->type = unexpression_type; \
1548 unary_expression->value = left; \
1550 return (expression_t*) unary_expression; \
1553 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1554 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1556 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1558 expression_t *parse_##binexpression_type(unsigned precedence, \
1559 expression_t *left) \
1563 expression_t *right = parse_sub_expression(precedence); \
1565 binary_expression_t *binexpr \
1566 = allocate_ast_zero(sizeof(binexpr[0])); \
1567 binexpr->expression.type = EXPR_BINARY; \
1568 binexpr->type = binexpression_type; \
1569 binexpr->left = left; \
1570 binexpr->right = right; \
1572 return (expression_t*) binexpr; \
1575 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1576 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1577 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1578 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1579 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1580 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1581 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1582 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1583 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1584 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1585 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1586 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1587 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1588 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1589 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1590 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1591 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1592 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1595 expression_t *parse_sub_expression(unsigned precedence)
1597 if(token.type < 0) {
1598 return expected_expression_error();
1601 expression_parser_function_t *parser
1602 = &expression_parsers[token.type];
1603 source_position_t source_position = token.source_position;
1606 if(parser->parser != NULL) {
1607 left = parser->parser(parser->precedence);
1609 left = parse_primary_expression();
1612 left->source_position = source_position;
1615 if(token.type < 0) {
1616 return expected_expression_error();
1619 parser = &expression_parsers[token.type];
1620 if(parser->infix_parser == NULL)
1622 if(parser->infix_precedence < precedence)
1625 left = parser->infix_parser(parser->infix_precedence, left);
1627 left->source_position = source_position;
1634 expression_t *parse_expression(void)
1636 return parse_sub_expression(1);
1641 void register_expression_parser(parse_expression_function parser,
1642 int token_type, unsigned precedence)
1644 expression_parser_function_t *entry = &expression_parsers[token_type];
1646 if(entry->parser != NULL) {
1647 fprintf(stderr, "for token ");
1648 print_token_type(stderr, token_type);
1649 fprintf(stderr, "\n");
1650 panic("trying to register multiple expression parsers for a token");
1652 entry->parser = parser;
1653 entry->precedence = precedence;
1656 void register_expression_infix_parser(parse_expression_infix_function parser,
1657 int token_type, unsigned precedence)
1659 expression_parser_function_t *entry = &expression_parsers[token_type];
1661 if(entry->infix_parser != NULL) {
1662 fprintf(stderr, "for token ");
1663 print_token_type(stderr, token_type);
1664 fprintf(stderr, "\n");
1665 panic("trying to register multiple infix expression parsers for a "
1668 entry->infix_parser = parser;
1669 entry->infix_precedence = precedence;
1673 void init_expression_parsers(void)
1675 memset(&expression_parsers, 0, sizeof(expression_parsers));
1677 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1678 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1679 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1681 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1682 T_GREATERGREATER, 16);
1683 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1684 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1685 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1686 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1687 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1688 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1689 T_GREATEREQUAL, 14);
1690 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1691 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1692 T_EXCLAMATIONMARKEQUAL, 13);
1693 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1694 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1695 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1696 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1697 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1698 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1699 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1701 register_expression_infix_parser(parse_array_expression, '[', 30);
1702 register_expression_infix_parser(parse_call_expression, '(', 30);
1703 register_expression_infix_parser(parse_select_expression, '.', 30);
1704 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1705 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1707 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1710 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1711 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1712 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1713 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1714 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1715 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1716 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1717 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1718 register_expression_parser(parse_sizeof, T_sizeof, 25);
1723 statement_t *parse_case_statement(void)
1734 statement_t *parse_default_statement(void)
1744 statement_t *parse_label_statement(void)
1754 statement_t *parse_if(void)
1762 if(token.type == T_else) {
1771 statement_t *parse_switch(void)
1783 statement_t *parse_while(void)
1795 statement_t *parse_do(void)
1808 statement_t *parse_for(void)
1812 if(token.type != ';') {
1813 /* TODO not correct... this could also be a declaration */
1817 if(token.type != ';') {
1821 if(token.type != ')') {
1831 statement_t *parse_goto(void)
1834 expect(T_IDENTIFIER);
1841 statement_t *parse_continue(void)
1846 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1847 statement->source_position = token.source_position;
1848 statement->type = STATEMENT_CONTINUE;
1854 statement_t *parse_break(void)
1863 statement_t *parse_return(void)
1867 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1868 statement->statement.type = STATEMENT_RETURN;
1869 if(token.type != ';') {
1870 statement->return_value = parse_expression();
1874 return (statement_t*) statement;
1878 statement_t *parse_declaration_statement(void)
1880 parse_declaration();
1885 statement_t *parse_expression_statement(void)
1892 statement_t *parse_statement(void)
1894 declaration_t *declaration;
1895 statement_t *statement = NULL;
1897 /* declaration or statement */
1898 switch(token.type) {
1900 statement = parse_case_statement();
1904 statement = parse_default_statement();
1908 statement = parse_compound_statement();
1912 statement = parse_if();
1916 statement = parse_switch();
1920 statement = parse_while();
1924 statement = parse_do();
1928 statement = parse_for();
1932 statement = parse_goto();
1936 statement = parse_continue();
1940 statement = parse_break();
1944 statement = parse_return();
1952 if(la(1)->type == ':') {
1953 statement = parse_label_statement();
1957 declaration = token.v.symbol->declaration;
1958 if(declaration != NULL &&
1959 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1960 statement = parse_declaration_statement();
1964 statement = parse_expression_statement();
1968 statement = parse_declaration_statement();
1976 statement_t *parse_compound_statement(void)
1980 compound_statement_t *compound_statement
1981 = allocate_ast_zero(sizeof(compound_statement[0]));
1982 compound_statement->statement.type = STATEMENT_COMPOUND;
1984 int top = environment_top();
1985 context_t *last_context = context;
1986 set_context(&compound_statement->context);
1988 statement_t *last_statement = NULL;
1990 while(token.type != '}' && token.type != T_EOF) {
1991 statement_t *statement = parse_statement();
1993 if(last_statement != NULL) {
1994 last_statement->next = statement;
1996 compound_statement->statements = statement;
1998 last_statement = statement;
2001 assert(context == &compound_statement->context);
2002 set_context(last_context);
2003 environment_pop_to(top);
2007 return (statement_t*) compound_statement;
2011 translation_unit_t *parse_translation_unit(void)
2013 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2015 assert(context == NULL);
2016 set_context(&unit->context);
2018 while(token.type != T_EOF) {
2019 parse_declaration();
2022 assert(context == &unit->context);
2024 last_declaration = NULL;
2029 translation_unit_t *parse(void)
2031 obstack_init(&environment_obstack);
2032 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2034 lookahead_bufpos = 0;
2035 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2038 translation_unit_t *unit = parse_translation_unit();
2040 DEL_ARR_F(environment_stack);
2041 obstack_free(&environment_obstack, NULL);
2046 void init_parser(void)
2048 init_expression_parsers();
2049 obstack_init(&temp_obst);
2052 void exit_parser(void)
2054 obstack_free(&temp_obst, NULL);