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 *declaration;
952 method_parameter_type_t *parameter_type;
953 method_parameter_type_t *last_parameter_type = NULL;
964 declaration = parse_parameter();
966 parameter_type = allocate_type_zero(sizeof(parameter_type[0]));
967 parameter_type->type = declaration->type;
968 parameter_type->symbol = declaration->symbol;
970 if(last_parameter_type != NULL) {
971 last_parameter_type->next = parameter_type;
973 type->parameter_types = parameter_type;
975 last_parameter_type = parameter_type;
981 if(token.type != ',')
987 typedef struct declarator_part declarator_part;
988 struct declarator_part {
989 parsed_pointer_t *pointers;
990 method_type_t *method_type;
991 declarator_part *inner;
996 declarator_part *parse_inner_declarator(declaration_t *declaration,
999 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1000 memset(part, 0, sizeof(part[0]));
1002 part->pointers = parse_pointers();
1004 /* TODO: find out if this is correct */
1007 switch(token.type) {
1009 if(declaration == NULL) {
1010 parse_error("no identifier expected in typename");
1012 declaration->symbol = token.v.symbol;
1013 declaration->source_position = token.source_position;
1019 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1025 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1030 switch(token.type) {
1034 method_type_t *method_type
1035 = allocate_type_zero(sizeof(method_type[0]));
1036 method_type->type.type = TYPE_METHOD;
1038 parse_parameters(method_type);
1040 part->method_type = method_type;
1047 if(token.type == T_static) {
1051 unsigned type_qualifiers = parse_type_qualifiers();
1052 if(type_qualifiers != 0) {
1053 if(token.type == T_static) {
1060 if(token.type == '*' && la(1)->type == ']') {
1062 } else if(token.type != ']') {
1063 parse_assignment_expression();
1069 goto declarator_finished;
1073 declarator_finished:
1080 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1083 type = make_pointers(type, part->pointers);
1085 method_type_t *method_type = part->method_type;
1086 if(method_type != NULL) {
1087 method_type->result_type = type;
1089 type = (type_t*) method_type;
1093 } while(part != NULL);
1099 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1100 type_t *type, int may_be_abstract)
1102 declarator_part *part
1103 = parse_inner_declarator(declaration, may_be_abstract);
1106 declaration->type = construct_declarator_type(part, type);
1107 declaration->storage_class = storage_class;
1108 obstack_free(&temp_obst, part);
1113 type_t *parse_abstract_declarator(type_t *base_type)
1115 declarator_part *part = parse_inner_declarator(NULL, 1);
1117 type_t *result = construct_declarator_type(part, base_type);
1118 obstack_free(&temp_obst, part);
1123 static void record_declaration(declaration_t *declaration)
1125 if(last_declaration != NULL) {
1126 last_declaration->next = declaration;
1129 context->declarations = declaration;
1131 last_declaration = declaration;
1135 void maybe_push_declaration(declaration_t *declaration)
1137 symbol_t *symbol = declaration->symbol;
1139 if(symbol != NULL) {
1140 environment_push(declaration, context);
1145 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1148 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1150 parse_declarator(declaration, specifiers->storage_class,
1151 specifiers->type, 0);
1152 maybe_push_declaration(declaration);
1153 record_declaration(declaration);
1154 if(token.type == '=') {
1156 if(token.type == '{') {
1160 parse_assignment_expression();
1162 } else if(token.type == '{') {
1163 statement_t *statement = parse_compound_statement();
1164 declaration->statement = statement;
1168 if(token.type != ',')
1176 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1179 if(token.type == ':') {
1181 parse_constant_expression();
1182 /* TODO (bitfields) */
1184 declaration_t *declaration
1185 = allocate_ast_zero(sizeof(declaration[0]));
1186 parse_declarator(declaration, specifiers->storage_class,
1187 specifiers->type, 0);
1188 maybe_push_declaration(declaration);
1189 record_declaration(declaration);
1191 if(token.type == ':') {
1193 parse_constant_expression();
1194 /* TODO (bitfields) */
1198 if(token.type != ',')
1205 static void parse_compound_type_entries(void)
1209 while(token.type != '}' && token.type != T_EOF) {
1210 declaration_specifiers_t specifiers;
1211 memset(&specifiers, 0, sizeof(specifiers));
1212 /* TODO not correct as this allows storage class stuff... but only
1213 * specifiers and qualifiers sould be allowed here */
1214 parse_declaration_specifiers(&specifiers);
1216 parse_struct_declarators(&specifiers);
1218 if(token.type == T_EOF) {
1219 parse_error("unexpected error while parsing struct");
1224 void parse_declaration(void)
1226 declaration_specifiers_t specifiers;
1227 memset(&specifiers, 0, sizeof(specifiers));
1228 parse_declaration_specifiers(&specifiers);
1230 if(token.type == ';') {
1234 parse_init_declarators(&specifiers);
1237 type_t *parse_typename(void)
1239 declaration_specifiers_t specifiers;
1240 memset(&specifiers, 0, sizeof(specifiers));
1241 parse_declaration_specifiers(&specifiers);
1242 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1243 /* TODO: improve error message, user does probably not know what a
1244 * storage class is...
1246 parse_error("typename may not have a storage class");
1249 type_t *result = parse_abstract_declarator(specifiers.type);
1257 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1258 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1259 expression_t *left);
1261 typedef struct expression_parser_function_t expression_parser_function_t;
1262 struct expression_parser_function_t {
1263 unsigned precedence;
1264 parse_expression_function parser;
1265 unsigned infix_precedence;
1266 parse_expression_infix_function infix_parser;
1269 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1272 expression_t *expected_expression_error(void)
1274 parser_print_error_prefix();
1275 fprintf(stderr, "expected expression, got token ");
1276 print_token(stderr, & token);
1277 fprintf(stderr, "\n");
1279 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1280 expression->type = EXPR_INVALID;
1287 expression_t *parse_string_const(void)
1289 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1291 cnst->expression.type = EXPR_STRING_LITERAL;
1292 cnst->value = parse_string_literals();
1294 return (expression_t*) cnst;
1298 expression_t *parse_int_const(void)
1300 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1302 cnst->expression.type = EXPR_CONST;
1303 cnst->value = token.v.intvalue;
1307 return (expression_t*) cnst;
1311 expression_t *parse_reference(void)
1313 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1315 ref->expression.type = EXPR_REFERENCE;
1316 ref->symbol = token.v.symbol;
1320 return (expression_t*) ref;
1324 expression_t *parse_cast(void)
1326 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1328 cast->expression.type = EXPR_UNARY;
1329 cast->type = UNEXPR_CAST;
1330 cast->expression.source_position = token.source_position;
1332 type_t *type = parse_typename();
1335 expression_t *value = parse_sub_expression(20);
1337 cast->expression.datatype = type;
1338 cast->value = value;
1340 return (expression_t*) cast;
1344 expression_t *parse_brace_expression(void)
1348 declaration_t *declaration;
1349 switch(token.type) {
1352 return parse_cast();
1354 declaration = token.v.symbol->declaration;
1355 if(declaration != NULL &&
1356 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1357 return parse_cast();
1361 expression_t *result = parse_expression();
1368 expression_t *parse_primary_expression(void)
1370 switch(token.type) {
1372 return parse_int_const();
1373 case T_STRING_LITERAL:
1374 return parse_string_const();
1376 return parse_reference();
1378 return parse_brace_expression();
1381 /* TODO: error message */
1386 expression_t *parse_array_expression(unsigned precedence,
1387 expression_t *array_ref)
1393 array_access_expression_t *array_access
1394 = allocate_ast_zero(sizeof(array_access[0]));
1396 array_access->expression.type = EXPR_ARRAY_ACCESS;
1397 array_access->array_ref = array_ref;
1398 array_access->index = parse_expression();
1400 if(token.type != ']') {
1401 parse_error_expected("Problem while parsing array access", ']', 0);
1406 return (expression_t*) array_access;
1410 type_t *get_expression_type(const expression_t *expression)
1418 expression_t *parse_sizeof(unsigned precedence)
1422 sizeof_expression_t *sizeof_expression
1423 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1424 sizeof_expression->expression.type = EXPR_SIZEOF;
1426 if(token.type == '(' /* && LA1 is type_specifier */) {
1428 sizeof_expression->type = parse_typename();
1431 expression_t *expression = parse_sub_expression(precedence);
1432 sizeof_expression->type = get_expression_type(expression);
1435 return (expression_t*) sizeof_expression;
1439 expression_t *parse_select_expression(unsigned precedence,
1440 expression_t *compound)
1444 assert(token.type == '.' || token.type == T_SELECT);
1447 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1449 select->expression.type = EXPR_SELECT;
1450 select->compound = compound;
1452 if(token.type != T_IDENTIFIER) {
1453 parse_error_expected("Problem while parsing compound select",
1457 select->symbol = token.v.symbol;
1460 return (expression_t*) select;
1464 expression_t *parse_call_expression(unsigned precedence,
1465 expression_t *expression)
1468 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1470 call->expression.type = EXPR_CALL;
1471 call->method = expression;
1473 /* parse arguments */
1476 if(token.type != ')') {
1477 call_argument_t *last_argument = NULL;
1480 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1482 argument->expression = parse_expression();
1483 if(last_argument == NULL) {
1484 call->arguments = argument;
1486 last_argument->next = argument;
1488 last_argument = argument;
1490 if(token.type != ',')
1497 return (expression_t*) call;
1501 expression_t *parse_conditional_expression(unsigned precedence,
1502 expression_t *expression)
1506 conditional_expression_t *conditional
1507 = allocate_ast_zero(sizeof(conditional[0]));
1508 conditional->condition = expression;
1510 conditional->true_expression = parse_expression();
1512 conditional->false_expression = parse_sub_expression(precedence);
1514 return (expression_t*) conditional;
1517 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1519 expression_t *parse_##unexpression_type(unsigned precedence) \
1523 unary_expression_t *unary_expression \
1524 = allocate_ast_zero(sizeof(unary_expression[0])); \
1525 unary_expression->expression.type = EXPR_UNARY; \
1526 unary_expression->type = unexpression_type; \
1527 unary_expression->value = parse_sub_expression(precedence); \
1529 return (expression_t*) unary_expression; \
1532 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1533 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1534 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1535 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1536 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1537 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1538 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1539 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1541 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1543 expression_t *parse_##unexpression_type(unsigned precedence, \
1544 expression_t *left) \
1546 (void) precedence; \
1549 unary_expression_t *unary_expression \
1550 = allocate_ast_zero(sizeof(unary_expression[0])); \
1551 unary_expression->expression.type = EXPR_UNARY; \
1552 unary_expression->type = unexpression_type; \
1553 unary_expression->value = left; \
1555 return (expression_t*) unary_expression; \
1558 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1559 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1561 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1563 expression_t *parse_##binexpression_type(unsigned precedence, \
1564 expression_t *left) \
1568 expression_t *right = parse_sub_expression(precedence); \
1570 binary_expression_t *binexpr \
1571 = allocate_ast_zero(sizeof(binexpr[0])); \
1572 binexpr->expression.type = EXPR_BINARY; \
1573 binexpr->type = binexpression_type; \
1574 binexpr->left = left; \
1575 binexpr->right = right; \
1577 return (expression_t*) binexpr; \
1580 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1581 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1582 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1583 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1584 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1585 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1586 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1587 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1588 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1589 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1590 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1591 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1592 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1593 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1594 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1595 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1596 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1597 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1600 expression_t *parse_sub_expression(unsigned precedence)
1602 if(token.type < 0) {
1603 return expected_expression_error();
1606 expression_parser_function_t *parser
1607 = &expression_parsers[token.type];
1608 source_position_t source_position = token.source_position;
1611 if(parser->parser != NULL) {
1612 left = parser->parser(parser->precedence);
1614 left = parse_primary_expression();
1617 left->source_position = source_position;
1620 if(token.type < 0) {
1621 return expected_expression_error();
1624 parser = &expression_parsers[token.type];
1625 if(parser->infix_parser == NULL)
1627 if(parser->infix_precedence < precedence)
1630 left = parser->infix_parser(parser->infix_precedence, left);
1632 left->source_position = source_position;
1639 expression_t *parse_expression(void)
1641 return parse_sub_expression(1);
1646 void register_expression_parser(parse_expression_function parser,
1647 int token_type, unsigned precedence)
1649 expression_parser_function_t *entry = &expression_parsers[token_type];
1651 if(entry->parser != NULL) {
1652 fprintf(stderr, "for token ");
1653 print_token_type(stderr, token_type);
1654 fprintf(stderr, "\n");
1655 panic("trying to register multiple expression parsers for a token");
1657 entry->parser = parser;
1658 entry->precedence = precedence;
1661 void register_expression_infix_parser(parse_expression_infix_function parser,
1662 int token_type, unsigned precedence)
1664 expression_parser_function_t *entry = &expression_parsers[token_type];
1666 if(entry->infix_parser != NULL) {
1667 fprintf(stderr, "for token ");
1668 print_token_type(stderr, token_type);
1669 fprintf(stderr, "\n");
1670 panic("trying to register multiple infix expression parsers for a "
1673 entry->infix_parser = parser;
1674 entry->infix_precedence = precedence;
1678 void init_expression_parsers(void)
1680 memset(&expression_parsers, 0, sizeof(expression_parsers));
1682 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1683 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1684 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1686 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1687 T_GREATERGREATER, 16);
1688 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1689 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1690 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1691 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1692 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1693 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1694 T_GREATEREQUAL, 14);
1695 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1696 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1697 T_EXCLAMATIONMARKEQUAL, 13);
1698 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1699 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1700 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1701 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1702 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1703 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1704 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1706 register_expression_infix_parser(parse_array_expression, '[', 30);
1707 register_expression_infix_parser(parse_call_expression, '(', 30);
1708 register_expression_infix_parser(parse_select_expression, '.', 30);
1709 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1710 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1712 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1715 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1716 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1717 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1718 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1719 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1720 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1721 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1722 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1723 register_expression_parser(parse_sizeof, T_sizeof, 25);
1728 statement_t *parse_case_statement(void)
1739 statement_t *parse_default_statement(void)
1749 statement_t *parse_label_statement(void)
1759 statement_t *parse_if(void)
1767 if(token.type == T_else) {
1776 statement_t *parse_switch(void)
1788 statement_t *parse_while(void)
1800 statement_t *parse_do(void)
1813 statement_t *parse_for(void)
1817 if(token.type != ';') {
1818 /* TODO not correct... this could also be a declaration */
1822 if(token.type != ';') {
1826 if(token.type != ')') {
1836 statement_t *parse_goto(void)
1839 expect(T_IDENTIFIER);
1846 statement_t *parse_continue(void)
1851 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1852 statement->source_position = token.source_position;
1853 statement->type = STATEMENT_CONTINUE;
1859 statement_t *parse_break(void)
1868 statement_t *parse_return(void)
1872 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1873 statement->statement.type = STATEMENT_RETURN;
1874 if(token.type != ';') {
1875 statement->return_value = parse_expression();
1879 return (statement_t*) statement;
1883 statement_t *parse_declaration_statement(void)
1885 parse_declaration();
1890 statement_t *parse_expression_statement(void)
1897 statement_t *parse_statement(void)
1899 declaration_t *declaration;
1900 statement_t *statement = NULL;
1902 /* declaration or statement */
1903 switch(token.type) {
1905 statement = parse_case_statement();
1909 statement = parse_default_statement();
1913 statement = parse_compound_statement();
1917 statement = parse_if();
1921 statement = parse_switch();
1925 statement = parse_while();
1929 statement = parse_do();
1933 statement = parse_for();
1937 statement = parse_goto();
1941 statement = parse_continue();
1945 statement = parse_break();
1949 statement = parse_return();
1957 if(la(1)->type == ':') {
1958 statement = parse_label_statement();
1962 declaration = token.v.symbol->declaration;
1963 if(declaration != NULL &&
1964 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1965 statement = parse_declaration_statement();
1969 statement = parse_expression_statement();
1973 statement = parse_declaration_statement();
1981 statement_t *parse_compound_statement(void)
1985 compound_statement_t *compound_statement
1986 = allocate_ast_zero(sizeof(compound_statement[0]));
1987 compound_statement->statement.type = STATEMENT_COMPOUND;
1989 int top = environment_top();
1990 context_t *last_context = context;
1991 set_context(&compound_statement->context);
1993 statement_t *last_statement = NULL;
1995 while(token.type != '}' && token.type != T_EOF) {
1996 statement_t *statement = parse_statement();
1998 if(last_statement != NULL) {
1999 last_statement->next = statement;
2001 compound_statement->statements = statement;
2003 last_statement = statement;
2006 assert(context == &compound_statement->context);
2007 set_context(last_context);
2008 environment_pop_to(top);
2012 return (statement_t*) compound_statement;
2016 translation_unit_t *parse_translation_unit(void)
2018 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2020 assert(context == NULL);
2021 set_context(&unit->context);
2023 while(token.type != T_EOF) {
2024 parse_declaration();
2027 assert(context == &unit->context);
2029 last_declaration = NULL;
2034 translation_unit_t *parse(void)
2036 obstack_init(&environment_obstack);
2037 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2039 lookahead_bufpos = 0;
2040 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2043 translation_unit_t *unit = parse_translation_unit();
2045 DEL_ARR_F(environment_stack);
2046 obstack_free(&environment_obstack, NULL);
2051 void init_parser(void)
2053 init_expression_parsers();
2054 obstack_init(&temp_obst);
2057 void exit_parser(void)
2059 obstack_free(&temp_obst, NULL);