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 MAX_LOOKAHEAD 2
19 struct environment_entry_t {
21 declaration_t *old_declaration;
22 const void *old_context;
26 static token_t lookahead_buffer[MAX_LOOKAHEAD];
27 static int lookahead_bufpos;
28 static struct obstack environment_obstack;
29 static environment_entry_t **environment_stack = NULL;
30 static context_t *context = NULL;
31 static declaration_t *last_declaration = NULL;
34 statement_t *parse_compound_statement(void);
36 statement_t *parse_statement(void);
39 expression_t *parse_sub_expression(unsigned precedence);
41 expression_t *parse_expression(void);
44 void *allocate_ast_zero(size_t size)
46 void *res = allocate_ast(size);
52 void *allocate_type_zero(size_t size)
54 void *res = obstack_alloc(type_obst, size);
60 * returns the top element of the environment stack
63 size_t environment_top()
65 return ARR_LEN(environment_stack);
73 token = lookahead_buffer[lookahead_bufpos];
74 lookahead_buffer[lookahead_bufpos] = lexer_token;
77 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
80 print_token(stderr, &token);
81 fprintf(stderr, "\n");
86 const token_t *la(int num)
88 assert(num > 0 && num <= MAX_LOOKAHEAD);
89 int pos = (num-1) % MAX_LOOKAHEAD;
90 return & lookahead_buffer[pos];
94 void eat(token_type_t type)
96 assert(token.type == type);
100 void parser_print_error_prefix_pos(const source_position_t source_position)
102 fputs(source_position.input_name, stderr);
104 fprintf(stderr, "%d", source_position.linenr);
105 fputs(": error: ", stderr);
108 void parser_print_error_prefix(void)
110 parser_print_error_prefix_pos(token.source_position);
114 void parse_error(const char *message)
116 parser_print_error_prefix();
117 fprintf(stderr, "parse error: %s\n", message);
121 void parse_error_expected(const char *message, ...)
126 if(message != NULL) {
127 parser_print_error_prefix();
128 fprintf(stderr, "%s\n", message);
130 parser_print_error_prefix();
131 fputs("Parse error: got ", stderr);
132 print_token(stderr, &token);
133 fputs(", expected ", stderr);
135 va_start(args, message);
136 token_type_t token_type = va_arg(args, token_type_t);
137 while(token_type != 0) {
141 fprintf(stderr, ", ");
143 print_token_type(stderr, token_type);
144 token_type = va_arg(args, token_type_t);
147 fprintf(stderr, "\n");
151 void eat_until(int token_type)
153 while(token.type != token_type) {
154 if(token.type == T_EOF)
161 #define expect(expected) \
162 if(UNLIKELY(token.type != (expected))) { \
163 parse_error_expected(NULL, (expected), 0); \
169 #define expect_void(expected) \
170 if(UNLIKELY(token.type != (expected))) { \
171 parse_error_expected(NULL, (expected), 0); \
177 static void set_context(context_t *new_context)
179 context = new_context;
181 declaration_t *declaration = new_context->declarations;
182 if(declaration != NULL) {
184 if(declaration->next == NULL)
186 declaration = declaration->next;
190 last_declaration = declaration;
194 * pushs an environment_entry on the environment stack and links the
195 * corresponding symbol to the new entry
198 void environment_push(declaration_t *declaration, const void *context)
200 environment_entry_t *entry
201 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
202 memset(entry, 0, sizeof(entry[0]));
204 int top = ARR_LEN(environment_stack);
205 ARR_RESIZE(environment_stack, top + 1);
206 environment_stack[top] = entry;
208 assert(declaration->source_position.input_name != NULL);
210 symbol_t *symbol = declaration->symbol;
211 assert(declaration != symbol->declaration);
213 if(symbol->context == context) {
214 if(symbol->declaration != NULL) {
215 assert(symbol->declaration != NULL);
216 parser_print_error_prefix_pos(declaration->source_position);
217 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
219 parser_print_error_prefix_pos(symbol->declaration->source_position);
220 fprintf(stderr, "this is the location of the previous declaration.\n");
224 entry->old_declaration = symbol->declaration;
225 entry->old_context = symbol->context;
226 entry->symbol = symbol;
227 symbol->declaration = declaration;
228 symbol->context = context;
232 * pops symbols from the environment stack until @p new_top is the top element
235 void environment_pop_to(size_t new_top)
237 environment_entry_t *entry = NULL;
238 size_t top = ARR_LEN(environment_stack);
244 assert(new_top < top);
247 entry = environment_stack[i - 1];
249 symbol_t *symbol = entry->symbol;
251 symbol->declaration = entry->old_declaration;
252 symbol->context = entry->old_context;
255 } while(i != new_top);
256 obstack_free(&environment_obstack, entry);
258 ARR_SHRINKLEN(environment_stack, (int) new_top);
263 static expression_t *parse_constant_expression(void)
265 /* TODO: not correct yet */
266 return parse_expression();
269 static expression_t *parse_assignment_expression(void)
271 /* TODO: not correct yet */
272 return parse_expression();
275 static void parse_compound_type_entries(void);
276 static void parse_declarator(declaration_t *declaration,
277 storage_class_t storage_class, type_t *type,
278 int may_omit_identifier);
279 static void maybe_push_declaration(declaration_t *declaration);
280 static void record_declaration(declaration_t *declaration);
282 typedef struct declaration_specifiers_t declaration_specifiers_t;
283 struct declaration_specifiers_t {
284 storage_class_t storage_class;
288 static type_t *parse_struct_specifier(void)
292 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
293 struct_type->type.type = TYPE_COMPOUND_STRUCT;
294 struct_type->source_position = token.source_position;
296 int top = environment_top();
297 context_t *last_context = context;
298 set_context(&struct_type->context);
300 if(token.type == T_IDENTIFIER) {
302 if(token.type == '{') {
303 parse_compound_type_entries();
305 } else if(token.type == '{') {
306 parse_compound_type_entries();
308 parse_error_expected("problem while parsing struct type specifiers",
309 T_IDENTIFIER, '{', 0);
313 assert(context == &struct_type->context);
314 set_context(last_context);
315 environment_pop_to(top);
317 return (type_t*) struct_type;
320 static type_t *parse_union_specifier(void)
324 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
325 union_type->type.type = TYPE_COMPOUND_UNION;
326 union_type->source_position = token.source_position;
328 int top = environment_top();
329 context_t *last_context = context;
330 set_context(&union_type->context);
332 if(token.type == T_IDENTIFIER) {
333 union_type->symbol = token.v.symbol;
335 if(token.type == '{') {
336 parse_compound_type_entries();
338 } else if(token.type == '{') {
339 parse_compound_type_entries();
341 parse_error_expected("problem while parsing union type specifiers",
346 assert(context == &union_type->context);
347 set_context(last_context);
348 environment_pop_to(top);
350 return (type_t*) union_type;
353 static void parse_enum_type_entries()
357 if(token.type == '}') {
359 parse_error("empty enum not allowed");
364 if(token.type != T_IDENTIFIER) {
365 parse_error_expected("problem while parsing enum entry",
372 if(token.type == '=') {
373 parse_constant_expression();
376 if(token.type != ',')
379 } while(token.type != '}');
384 static type_t *parse_enum_specifier(void)
388 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
389 enum_type->type.type = TYPE_ENUM;
390 enum_type->source_position = token.source_position;
392 if(token.type == T_IDENTIFIER) {
393 enum_type->symbol = token.v.symbol;
395 if(token.type == '{') {
396 parse_enum_type_entries();
398 } else if(token.type == '{') {
399 parse_enum_type_entries();
401 parse_error_expected("problem while parsing enum type specifiers",
405 return (type_t*) enum_type;
409 SPECIFIER_SIGNED = 1 << 0,
410 SPECIFIER_UNSIGNED = 1 << 1,
411 SPECIFIER_LONG = 1 << 2,
412 SPECIFIER_INT = 1 << 3,
413 SPECIFIER_DOUBLE = 1 << 4,
414 SPECIFIER_CHAR = 1 << 5,
415 SPECIFIER_SHORT = 1 << 6,
416 SPECIFIER_LONG_LONG = 1 << 7,
417 SPECIFIER_FLOAT = 1 << 8,
418 SPECIFIER_BOOL = 1 << 9,
419 SPECIFIER_VOID = 1 << 10,
420 #ifdef PROVIDE_COMPLEX
421 SPECIFIER_COMPLEX = 1 << 11,
423 #ifdef PROVIDE_IMAGINARY
424 SPECIFIER_IMAGINARY = 1 << 12,
428 #define STORAGE_CLASSES \
435 #define TYPE_QUALIFIERS \
440 case T___extension__:
442 #ifdef PROVIDE_COMPLEX
443 #define COMPLEX_SPECIFIERS \
446 #define COMPLEX_SPECIFIERS
449 #ifdef PROVIDE_IMAGINARY
450 #define IMAGINARY_SPECIFIERS \
453 #define IMAGINARY_SPECIFIERS
456 #define TYPE_SPECIFIERS \
473 #define DECLARATION_START \
479 type_t *create_builtin_type(symbol_t *symbol)
481 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
482 type->type.type = TYPE_BUILTIN;
483 type->symbol = symbol;
485 type_t *result = typehash_insert((type_t*) type);
486 if(result != (type_t*) type) {
487 obstack_free(type_obst, type);
494 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
496 declaration_t *declaration;
498 unsigned type_qualifiers = 0;
499 unsigned type_specifiers = 0;
506 #define MATCH_STORAGE_CLASS(token, class) \
508 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
509 parse_error("multiple storage classes in declaration " \
512 specifiers->storage_class = class; \
516 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
517 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
518 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
519 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
520 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
522 /* type qualifiers */
523 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
525 type_qualifiers |= qualifier; \
529 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
530 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
531 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
532 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
534 case T___extension__:
539 /* type specifiers */
540 #define MATCH_SPECIFIER(token, specifier, name) \
543 if(type_specifiers & specifier) { \
544 parse_error("multiple " name " type specifiers given"); \
546 type_specifiers |= specifier; \
550 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
551 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
552 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
553 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
554 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
555 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
556 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
557 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
558 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
559 #ifdef PROVIDE_COMPLEX
560 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
562 #ifdef PROVIDE_IMAGINARY
563 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
567 if(type_specifiers & SPECIFIER_LONG_LONG) {
568 parse_error("multiple type specifiers given");
569 } else if(type_specifiers & SPECIFIER_LONG) {
570 type_specifiers |= SPECIFIER_LONG_LONG;
572 type_specifiers |= SPECIFIER_LONG;
576 /* TODO: if type != NULL for the following rules issue an error */
578 type = parse_struct_specifier();
581 type = parse_union_specifier();
584 type = parse_enum_specifier();
586 case T___builtin_va_list:
587 type = create_builtin_type(token.v.symbol);
592 declaration = token.v.symbol->declaration;
593 if(declaration == NULL ||
594 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
595 goto finish_specifiers;
598 type = declaration->type;
599 assert(type != NULL);
603 /* function specifier */
605 goto finish_specifiers;
612 atomic_type_type_t atomic_type;
614 /* match valid basic types */
615 switch(type_specifiers) {
617 atomic_type = ATOMIC_TYPE_VOID;
620 atomic_type = ATOMIC_TYPE_CHAR;
622 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
623 atomic_type = ATOMIC_TYPE_SCHAR;
625 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
626 atomic_type = ATOMIC_TYPE_UCHAR;
628 case SPECIFIER_SHORT:
629 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
630 case SPECIFIER_SHORT | SPECIFIER_INT:
631 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
632 atomic_type = ATOMIC_TYPE_SHORT;
634 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
635 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
636 atomic_type = ATOMIC_TYPE_USHORT;
639 case SPECIFIER_SIGNED:
640 case SPECIFIER_SIGNED | SPECIFIER_INT:
641 atomic_type = ATOMIC_TYPE_INT;
643 case SPECIFIER_UNSIGNED:
644 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
645 atomic_type = ATOMIC_TYPE_UINT;
648 case SPECIFIER_SIGNED | SPECIFIER_LONG:
649 case SPECIFIER_LONG | SPECIFIER_INT:
650 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
651 atomic_type = ATOMIC_TYPE_LONG;
653 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
654 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
655 atomic_type = ATOMIC_TYPE_ULONG;
657 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
658 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
659 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
660 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
662 atomic_type = ATOMIC_TYPE_LONGLONG;
664 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
665 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
667 atomic_type = ATOMIC_TYPE_ULONGLONG;
669 case SPECIFIER_FLOAT:
670 atomic_type = ATOMIC_TYPE_FLOAT;
672 case SPECIFIER_DOUBLE:
673 atomic_type = ATOMIC_TYPE_DOUBLE;
675 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
676 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
679 atomic_type = ATOMIC_TYPE_BOOL;
681 #ifdef PROVIDE_COMPLEX
682 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
683 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
685 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
686 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
688 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
689 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
692 #ifdef PROVIDE_IMAGINARY
693 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
694 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
696 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
697 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
699 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
700 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
704 /* invalid specifier combination, give an error message */
705 if(type_specifiers == 0) {
706 parse_error("no type specifiers given in declaration");
707 } else if((type_specifiers & SPECIFIER_SIGNED) &&
708 (type_specifiers & SPECIFIER_UNSIGNED)) {
709 parse_error("signed and unsigned specifiers gives");
710 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
711 parse_error("only integer types can be signed or unsigned");
713 parse_error("multiple datatypes in declaration");
715 atomic_type = ATOMIC_TYPE_INVALID;
718 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
719 atype->type.type = TYPE_ATOMIC;
720 atype->atype = atomic_type;
723 type = (type_t*) atype;
725 if(type_specifiers != 0) {
726 parse_error("multiple datatypes in declaration");
730 type->qualifiers = type_qualifiers;
732 type_t *result = typehash_insert(type);
733 if(newtype && result != (type_t*) type) {
734 obstack_free(type_obst, type);
737 specifiers->type = result;
741 unsigned parse_type_qualifiers()
743 unsigned type_qualifiers = 0;
747 /* type qualifiers */
748 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
749 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
750 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
751 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
754 return type_qualifiers;
760 int parse_pointers(void)
764 while(token.type == '*') {
773 type_t *make_pointers(type_t *type, int count)
775 for(int i = 0; i < count; ++i) {
776 pointer_type_t *pointer_type
777 = allocate_type_zero(sizeof(pointer_type[0]));
778 pointer_type->type.type = TYPE_POINTER;
779 pointer_type->points_to = type;
781 pointer_type->type.qualifiers = parse_type_qualifiers();
783 type_t *result = typehash_insert((type_t*) pointer_type);
784 if(result != (type_t*) pointer_type) {
785 obstack_free(type_obst, pointer_type);
795 void parse_identifier_list(void)
798 if(token.type != T_IDENTIFIER) {
799 parse_error_expected("problem while parsing parameter identifier "
800 "list", T_IDENTIFIER, 0);
804 if(token.type != ',')
811 declaration_t *parse_parameter(void)
813 declaration_specifiers_t specifiers;
814 memset(&specifiers, 0, sizeof(specifiers));
816 parse_declaration_specifiers(&specifiers);
818 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
819 parse_declarator(declaration, specifiers.storage_class,
823 if(declaration->symbol != NULL) {
824 maybe_push_declaration(declaration);
825 record_declaration(declaration);
833 void parse_parameters(method_type_t *type)
835 if(token.type == T_IDENTIFIER) {
836 symbol_t *symbol = token.v.symbol;
837 declaration_t *declaration = symbol->declaration;
838 if(declaration == NULL
839 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
840 parse_identifier_list();
845 if(token.type == ')') {
846 type->unspecified_parameters = 1;
849 if(token.type == T_void && la(1)->type == ')') {
854 declaration_t *declaration;
855 method_parameter_type_t *parameter_type;
856 method_parameter_type_t *last_parameter_type = NULL;
867 declaration = parse_parameter();
869 parameter_type = allocate_type_zero(sizeof(parameter_type[0]));
870 parameter_type->type = declaration->type;
871 parameter_type->symbol = declaration->symbol;
873 if(last_parameter_type != NULL) {
874 last_parameter_type->next = parameter_type;
876 type->parameter_types = parameter_type;
878 last_parameter_type = parameter_type;
884 if(token.type != ',')
891 void parse_attributes(void)
893 while(token.type == T___attribute__) {
901 parse_error("EOF while parsing attribute");
918 typedef struct declarator_part declarator_part;
919 struct declarator_part {
921 method_type_t *method_type;
922 declarator_part *inner;
925 static struct obstack temp_obst;
928 declarator_part *parse_inner_declarator(declaration_t *declaration)
930 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
931 memset(part, 0, sizeof(part[0]));
933 part->pointers = parse_pointers();
937 declaration->symbol = token.v.symbol;
938 declaration->source_position = token.source_position;
943 part->inner = parse_inner_declarator(declaration);
947 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
957 int top = environment_top();
958 context_t *last_context = context;
959 set_context(&declaration->context);
962 method_type_t *method_type
963 = allocate_type_zero(sizeof(method_type[0]));
964 method_type->type.type = TYPE_METHOD;
966 parse_parameters(method_type);
969 assert(context == &declaration->context);
970 set_context(last_context);
971 environment_pop_to(top);
974 part->method_type = method_type;
981 if(token.type == T_static) {
985 unsigned type_qualifiers = parse_type_qualifiers();
986 if(type_qualifiers != 0) {
987 if(token.type == T_static) {
994 if(token.type == '*' && la(1)->type == ']') {
996 } else if(token.type != ']') {
997 parse_assignment_expression();
1003 goto declarator_finished;
1007 declarator_finished:
1014 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1017 type = make_pointers(type, part->pointers);
1019 method_type_t *method_type = part->method_type;
1020 if(method_type != NULL) {
1021 method_type->result_type = type;
1023 type = (type_t*) method_type;
1027 } while(part != NULL);
1033 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1034 type_t *type, int may_omit_identifier)
1036 (void) may_omit_identifier;
1037 declarator_part *part = parse_inner_declarator(declaration);
1039 declaration->type = construct_declarator_type(part, type);
1040 declaration->storage_class = storage_class;
1041 obstack_free(&temp_obst, part);
1046 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1047 type_t *type, int may_omit_identifier)
1049 ir_type *outer_type = parse_pointers(type);
1051 declaration->storage_class = storage_class;
1052 declaration->type = outer_type;
1054 switch(token.type) {
1056 declaration->symbol = token.v.symbol;
1057 declaration->source_position = token.source_position;
1062 parse_declarator(declaration, storage_class, type, 0);
1066 if(may_omit_identifier)
1067 goto declarator_finished;
1068 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1073 switch(token.type) {
1077 int top = environment_top();
1078 context_t *last_context = context;
1079 set_context(&declaration->context);
1081 method_type_t *method_type
1082 = allocate_type_zero(sizeof(method_type[0]));
1083 method_type->type.type = TYPE_METHOD;
1084 method_type->result_type = outer_type;
1086 parse_parameters(method_type);
1088 assert(context == &declaration->context);
1089 set_context(last_context);
1090 environment_pop_to(top);
1092 replace_type(&declaration->type, outer_type,
1100 if(token.type == T_static) {
1104 unsigned type_qualifiers = parse_type_qualifiers();
1105 if(type_qualifiers != 0) {
1106 if(token.type == T_static) {
1111 if(token.type == '*' && la(1)->type == ']') {
1113 } else if(token.type != ']') {
1114 parse_assignment_expression();
1120 goto declarator_finished;
1124 declarator_finished:
1129 static void record_declaration(declaration_t *declaration)
1131 if(last_declaration != NULL) {
1132 last_declaration->next = declaration;
1135 context->declarations = declaration;
1137 last_declaration = declaration;
1141 void maybe_push_declaration(declaration_t *declaration)
1143 symbol_t *symbol = declaration->symbol;
1145 if(symbol != NULL) {
1146 environment_push(declaration, context);
1151 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1154 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1156 parse_declarator(declaration, specifiers->storage_class,
1157 specifiers->type, 0);
1158 maybe_push_declaration(declaration);
1159 record_declaration(declaration);
1160 if(token.type == '=') {
1162 if(token.type == '{') {
1166 parse_assignment_expression();
1168 } else if(token.type == '{') {
1169 statement_t *statement = parse_compound_statement();
1170 declaration->statement = statement;
1174 if(token.type != ',')
1182 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1185 if(token.type == ':') {
1187 parse_constant_expression();
1188 /* TODO (bitfields) */
1190 declaration_t *declaration
1191 = allocate_ast_zero(sizeof(declaration[0]));
1192 parse_declarator(declaration, specifiers->storage_class,
1193 specifiers->type, 0);
1194 maybe_push_declaration(declaration);
1195 record_declaration(declaration);
1197 if(token.type == ':') {
1199 parse_constant_expression();
1200 /* TODO (bitfields) */
1204 if(token.type != ',')
1211 static void parse_compound_type_entries(void)
1215 while(token.type != '}' && token.type != T_EOF) {
1216 declaration_specifiers_t specifiers;
1217 memset(&specifiers, 0, sizeof(specifiers));
1218 /* TODO not correct as this allows storage class stuff... but only
1219 * specifiers and qualifiers sould be allowed here */
1220 parse_declaration_specifiers(&specifiers);
1222 parse_struct_declarators(&specifiers);
1224 if(token.type == T_EOF) {
1225 parse_error("unexpected error while parsing struct");
1230 void parse_declaration(void)
1232 declaration_specifiers_t specifiers;
1233 memset(&specifiers, 0, sizeof(specifiers));
1234 parse_declaration_specifiers(&specifiers);
1236 if(token.type == ';') {
1240 parse_init_declarators(&specifiers);
1243 type_t *parse_typename(void)
1245 declaration_specifiers_t specifiers;
1246 memset(&specifiers, 0, sizeof(specifiers));
1247 /* TODO not correct storage class elements are not allowed here */
1248 parse_declaration_specifiers(&specifiers);
1250 int pointers = parse_pointers();
1251 specifiers.type = make_pointers(specifiers.type, pointers);
1253 return specifiers.type;
1259 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1260 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1261 expression_t *left);
1263 typedef struct expression_parser_function_t expression_parser_function_t;
1264 struct expression_parser_function_t {
1265 unsigned precedence;
1266 parse_expression_function parser;
1267 unsigned infix_precedence;
1268 parse_expression_infix_function infix_parser;
1271 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1274 expression_t *expected_expression_error(void)
1276 parser_print_error_prefix();
1277 fprintf(stderr, "expected expression, got token ");
1278 print_token(stderr, & token);
1279 fprintf(stderr, "\n");
1281 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1282 expression->type = EXPR_INVALID;
1289 expression_t *parse_string_const(void)
1291 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1293 cnst->expression.type = EXPR_STRING_LITERAL;
1294 cnst->value = token.v.string;
1298 return (expression_t*) cnst;
1302 expression_t *parse_int_const(void)
1304 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1306 cnst->expression.type = EXPR_CONST;
1307 cnst->value = token.v.intvalue;
1311 return (expression_t*) cnst;
1315 expression_t *parse_reference(void)
1317 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1319 ref->expression.type = EXPR_REFERENCE;
1320 ref->symbol = token.v.symbol;
1324 return (expression_t*) ref;
1328 expression_t *parse_brace_expression(void)
1332 expression_t *result = parse_expression();
1339 expression_t *parse_primary_expression(void)
1341 switch(token.type) {
1343 return parse_int_const();
1344 case T_STRING_LITERAL:
1345 return parse_string_const();
1347 return parse_reference();
1349 return parse_brace_expression();
1352 /* TODO: error message */
1357 expression_t *parse_array_expression(unsigned precedence,
1358 expression_t *array_ref)
1364 array_access_expression_t *array_access
1365 = allocate_ast_zero(sizeof(array_access[0]));
1367 array_access->expression.type = EXPR_ARRAY_ACCESS;
1368 array_access->array_ref = array_ref;
1369 array_access->index = parse_expression();
1371 if(token.type != ']') {
1372 parse_error_expected("Problem while parsing array access", ']', 0);
1377 return (expression_t*) array_access;
1381 type_t *get_expression_type(const expression_t *expression)
1389 expression_t *parse_sizeof(unsigned precedence)
1393 sizeof_expression_t *sizeof_expression
1394 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1395 sizeof_expression->expression.type = EXPR_SIZEOF;
1397 if(token.type == '(' /* && LA1 is type_specifier */) {
1399 sizeof_expression->type = parse_typename();
1402 expression_t *expression = parse_sub_expression(precedence);
1403 sizeof_expression->type = get_expression_type(expression);
1406 return (expression_t*) sizeof_expression;
1410 expression_t *parse_select_expression(unsigned precedence,
1411 expression_t *compound)
1415 assert(token.type == '.' || token.type == T_SELECT);
1418 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1420 select->expression.type = EXPR_SELECT;
1421 select->compound = compound;
1423 if(token.type != T_IDENTIFIER) {
1424 parse_error_expected("Problem while parsing compound select",
1428 select->symbol = token.v.symbol;
1431 return (expression_t*) select;
1435 expression_t *parse_call_expression(unsigned precedence,
1436 expression_t *expression)
1439 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1441 call->expression.type = EXPR_CALL;
1442 call->method = expression;
1444 /* parse arguments */
1447 if(token.type != ')') {
1448 call_argument_t *last_argument = NULL;
1451 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1453 argument->expression = parse_expression();
1454 if(last_argument == NULL) {
1455 call->arguments = argument;
1457 last_argument->next = argument;
1459 last_argument = argument;
1461 if(token.type != ',')
1468 return (expression_t*) call;
1471 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1473 expression_t *parse_##unexpression_type(unsigned precedence) \
1477 unary_expression_t *unary_expression \
1478 = allocate_ast_zero(sizeof(unary_expression[0])); \
1479 unary_expression->expression.type = EXPR_UNARY; \
1480 unary_expression->type = unexpression_type; \
1481 unary_expression->value = parse_sub_expression(precedence); \
1483 return (expression_t*) unary_expression; \
1486 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1487 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1488 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1489 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1490 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1491 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1492 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1493 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1495 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1497 expression_t *parse_##unexpression_type(unsigned precedence, \
1498 expression_t *left) \
1500 (void) precedence; \
1503 unary_expression_t *unary_expression \
1504 = allocate_ast_zero(sizeof(unary_expression[0])); \
1505 unary_expression->expression.type = EXPR_UNARY; \
1506 unary_expression->type = unexpression_type; \
1507 unary_expression->value = left; \
1509 return (expression_t*) unary_expression; \
1512 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1513 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1515 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1517 expression_t *parse_##binexpression_type(unsigned precedence, \
1518 expression_t *left) \
1522 expression_t *right = parse_sub_expression(precedence); \
1524 binary_expression_t *binexpr \
1525 = allocate_ast_zero(sizeof(binexpr[0])); \
1526 binexpr->expression.type = EXPR_BINARY; \
1527 binexpr->type = binexpression_type; \
1528 binexpr->left = left; \
1529 binexpr->right = right; \
1531 return (expression_t*) binexpr; \
1534 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1535 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1536 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1537 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1538 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1539 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1540 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1541 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1542 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1543 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1544 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1545 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1546 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1547 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1548 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1549 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1552 expression_t *parse_sub_expression(unsigned precedence)
1554 if(token.type < 0) {
1555 return expected_expression_error();
1558 expression_parser_function_t *parser
1559 = &expression_parsers[token.type];
1560 source_position_t source_position = token.source_position;
1563 if(parser->parser != NULL) {
1564 left = parser->parser(parser->precedence);
1566 left = parse_primary_expression();
1569 left->source_position = source_position;
1572 if(token.type < 0) {
1573 return expected_expression_error();
1576 parser = &expression_parsers[token.type];
1577 if(parser->infix_parser == NULL)
1579 if(parser->infix_precedence < precedence)
1582 left = parser->infix_parser(parser->infix_precedence, left);
1584 left->source_position = source_position;
1591 expression_t *parse_expression(void)
1593 return parse_sub_expression(1);
1598 void register_expression_parser(parse_expression_function parser,
1599 int token_type, unsigned precedence)
1601 expression_parser_function_t *entry = &expression_parsers[token_type];
1603 if(entry->parser != NULL) {
1604 fprintf(stderr, "for token ");
1605 print_token_type(stderr, token_type);
1606 fprintf(stderr, "\n");
1607 panic("trying to register multiple expression parsers for a token");
1609 entry->parser = parser;
1610 entry->precedence = precedence;
1613 void register_expression_infix_parser(parse_expression_infix_function parser,
1614 int token_type, unsigned precedence)
1616 expression_parser_function_t *entry = &expression_parsers[token_type];
1618 if(entry->infix_parser != NULL) {
1619 fprintf(stderr, "for token ");
1620 print_token_type(stderr, token_type);
1621 fprintf(stderr, "\n");
1622 panic("trying to register multiple infix expression parsers for a "
1625 entry->infix_parser = parser;
1626 entry->infix_precedence = precedence;
1630 void init_expression_parsers(void)
1632 memset(&expression_parsers, 0, sizeof(expression_parsers));
1634 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1635 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1636 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1638 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1639 T_GREATERGREATER, 16);
1640 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1641 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1642 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1643 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1644 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1645 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1646 T_GREATEREQUAL, 14);
1647 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1648 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1649 T_EXCLAMATIONMARKEQUAL, 13);
1650 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1651 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1652 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1653 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1655 register_expression_infix_parser(parse_array_expression, '[', 30);
1656 register_expression_infix_parser(parse_call_expression, '(', 30);
1657 register_expression_infix_parser(parse_select_expression, '.', 30);
1658 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1659 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1661 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1664 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1665 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1666 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1667 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1668 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1669 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1670 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1671 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1672 register_expression_parser(parse_sizeof, T_sizeof, 25);
1677 statement_t *parse_case_statement(void)
1688 statement_t *parse_default_statement(void)
1698 statement_t *parse_label_statement(void)
1708 statement_t *parse_if(void)
1716 if(token.type == T_else) {
1725 statement_t *parse_switch(void)
1737 statement_t *parse_while(void)
1749 statement_t *parse_do(void)
1762 statement_t *parse_for(void)
1766 if(token.type != ';') {
1767 /* TODO not correct... this could also be a declaration */
1771 if(token.type != ';') {
1775 if(token.type != ')') {
1785 statement_t *parse_goto(void)
1788 expect(T_IDENTIFIER);
1795 statement_t *parse_continue(void)
1804 statement_t *parse_break(void)
1813 statement_t *parse_return(void)
1823 statement_t *parse_declaration_statement(void)
1825 parse_declaration();
1830 statement_t *parse_expression_statement(void)
1837 statement_t *parse_statement(void)
1839 declaration_t *declaration;
1840 statement_t *statement = NULL;
1842 /* declaration or statement */
1843 switch(token.type) {
1845 statement = parse_case_statement();
1849 statement = parse_default_statement();
1853 statement = parse_compound_statement();
1857 statement = parse_if();
1861 statement = parse_switch();
1865 statement = parse_while();
1869 statement = parse_do();
1873 statement = parse_for();
1877 statement = parse_goto();
1881 statement = parse_continue();
1885 statement = parse_break();
1889 statement = parse_return();
1897 if(la(1)->type == ':') {
1898 statement = parse_label_statement();
1902 declaration = token.v.symbol->declaration;
1903 if(declaration != NULL &&
1904 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1905 statement = parse_declaration_statement();
1909 statement = parse_expression_statement();
1913 statement = parse_declaration_statement();
1921 statement_t *parse_compound_statement(void)
1925 compound_statement_t *compound_statement
1926 = allocate_ast_zero(sizeof(compound_statement[0]));
1927 compound_statement->statement.type = STATEMENT_COMPOUND;
1929 int top = environment_top();
1930 context_t *last_context = context;
1931 set_context(&compound_statement->context);
1933 while(token.type != '}') {
1937 assert(context == &compound_statement->context);
1938 set_context(last_context);
1939 environment_pop_to(top);
1943 return (statement_t*) compound_statement;
1947 translation_unit_t *parse_translation_unit(void)
1949 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1951 assert(context == NULL);
1952 set_context(&unit->context);
1954 while(token.type != T_EOF) {
1955 parse_declaration();
1958 assert(context == &unit->context);
1960 last_declaration = NULL;
1965 translation_unit_t *parse(void)
1967 obstack_init(&environment_obstack);
1968 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1970 lookahead_bufpos = 0;
1971 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1974 translation_unit_t *unit = parse_translation_unit();
1976 DEL_ARR_F(environment_stack);
1977 obstack_free(&environment_obstack, NULL);
1982 void init_parser(void)
1984 init_expression_parsers();
1985 obstack_init(&temp_obst);
1988 void exit_parser(void)
1990 obstack_free(&temp_obst, NULL);