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;
32 static struct obstack temp_obst;
35 statement_t *parse_compound_statement(void);
37 statement_t *parse_statement(void);
40 expression_t *parse_sub_expression(unsigned precedence);
42 expression_t *parse_expression(void);
45 void *allocate_ast_zero(size_t size)
47 void *res = allocate_ast(size);
53 void *allocate_type_zero(size_t size)
55 void *res = obstack_alloc(type_obst, size);
61 * returns the top element of the environment stack
64 size_t environment_top(void)
66 return ARR_LEN(environment_stack);
74 token = lookahead_buffer[lookahead_bufpos];
75 lookahead_buffer[lookahead_bufpos] = lexer_token;
78 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
81 print_token(stderr, &token);
82 fprintf(stderr, "\n");
87 const token_t *la(int num)
89 assert(num > 0 && num <= MAX_LOOKAHEAD);
90 int pos = (num-1) % MAX_LOOKAHEAD;
91 return & lookahead_buffer[pos];
95 void eat(token_type_t type)
97 assert(token.type == type);
101 void parser_print_error_prefix_pos(const source_position_t source_position)
103 fputs(source_position.input_name, stderr);
105 fprintf(stderr, "%d", source_position.linenr);
106 fputs(": error: ", stderr);
109 void parser_print_error_prefix(void)
111 parser_print_error_prefix_pos(token.source_position);
115 void parse_error(const char *message)
117 parser_print_error_prefix();
118 fprintf(stderr, "parse error: %s\n", message);
122 void parse_error_expected(const char *message, ...)
127 if(message != NULL) {
128 parser_print_error_prefix();
129 fprintf(stderr, "%s\n", message);
131 parser_print_error_prefix();
132 fputs("Parse error: got ", stderr);
133 print_token(stderr, &token);
134 fputs(", expected ", stderr);
136 va_start(args, message);
137 token_type_t token_type = va_arg(args, token_type_t);
138 while(token_type != 0) {
142 fprintf(stderr, ", ");
144 print_token_type(stderr, token_type);
145 token_type = va_arg(args, token_type_t);
148 fprintf(stderr, "\n");
152 void eat_until(int token_type)
154 while(token.type != token_type) {
155 if(token.type == T_EOF)
162 #define expect(expected) \
163 if(UNLIKELY(token.type != (expected))) { \
164 parse_error_expected(NULL, (expected), 0); \
170 #define expect_void(expected) \
171 if(UNLIKELY(token.type != (expected))) { \
172 parse_error_expected(NULL, (expected), 0); \
178 static void set_context(context_t *new_context)
180 context = new_context;
182 declaration_t *declaration = new_context->declarations;
183 if(declaration != NULL) {
185 if(declaration->next == NULL)
187 declaration = declaration->next;
191 last_declaration = declaration;
195 * pushs an environment_entry on the environment stack and links the
196 * corresponding symbol to the new entry
199 void environment_push(declaration_t *declaration, const void *context)
201 environment_entry_t *entry
202 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
203 memset(entry, 0, sizeof(entry[0]));
205 int top = ARR_LEN(environment_stack);
206 ARR_RESIZE(environment_stack, top + 1);
207 environment_stack[top] = entry;
209 assert(declaration->source_position.input_name != NULL);
211 symbol_t *symbol = declaration->symbol;
212 assert(declaration != symbol->declaration);
214 if(symbol->context == context) {
215 if(symbol->declaration != NULL) {
216 assert(symbol->declaration != NULL);
217 parser_print_error_prefix_pos(declaration->source_position);
218 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
220 parser_print_error_prefix_pos(symbol->declaration->source_position);
221 fprintf(stderr, "this is the location of the previous declaration.\n");
225 entry->old_declaration = symbol->declaration;
226 entry->old_context = symbol->context;
227 entry->symbol = symbol;
228 symbol->declaration = declaration;
229 symbol->context = context;
233 * pops symbols from the environment stack until @p new_top is the top element
236 void environment_pop_to(size_t new_top)
238 environment_entry_t *entry = NULL;
239 size_t top = ARR_LEN(environment_stack);
245 assert(new_top < top);
248 entry = environment_stack[i - 1];
250 symbol_t *symbol = entry->symbol;
252 symbol->declaration = entry->old_declaration;
253 symbol->context = entry->old_context;
256 } while(i != new_top);
257 obstack_free(&environment_obstack, entry);
259 ARR_SHRINKLEN(environment_stack, (int) new_top);
264 static expression_t *parse_constant_expression(void)
266 /* TODO: not correct yet */
267 return parse_expression();
270 static expression_t *parse_assignment_expression(void)
272 /* TODO: not correct yet */
273 return parse_expression();
276 static void parse_compound_type_entries(void);
277 static void parse_declarator(declaration_t *declaration,
278 storage_class_t storage_class, type_t *type,
279 int may_be_abstract);
280 static void maybe_push_declaration(declaration_t *declaration);
281 static void record_declaration(declaration_t *declaration);
283 typedef struct declaration_specifiers_t declaration_specifiers_t;
284 struct declaration_specifiers_t {
285 storage_class_t storage_class;
289 static type_t *parse_struct_specifier(void)
293 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
294 struct_type->type.type = TYPE_COMPOUND_STRUCT;
295 struct_type->source_position = token.source_position;
297 int top = environment_top();
298 context_t *last_context = context;
299 set_context(&struct_type->context);
301 if(token.type == T_IDENTIFIER) {
303 if(token.type == '{') {
304 parse_compound_type_entries();
306 } else if(token.type == '{') {
307 parse_compound_type_entries();
309 parse_error_expected("problem while parsing struct type specifiers",
310 T_IDENTIFIER, '{', 0);
314 assert(context == &struct_type->context);
315 set_context(last_context);
316 environment_pop_to(top);
318 return (type_t*) struct_type;
321 static type_t *parse_union_specifier(void)
325 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
326 union_type->type.type = TYPE_COMPOUND_UNION;
327 union_type->source_position = token.source_position;
329 int top = environment_top();
330 context_t *last_context = context;
331 set_context(&union_type->context);
333 if(token.type == T_IDENTIFIER) {
334 union_type->symbol = token.v.symbol;
336 if(token.type == '{') {
337 parse_compound_type_entries();
339 } else if(token.type == '{') {
340 parse_compound_type_entries();
342 parse_error_expected("problem while parsing union type specifiers",
347 assert(context == &union_type->context);
348 set_context(last_context);
349 environment_pop_to(top);
351 return (type_t*) union_type;
354 static void parse_enum_type_entries(void)
358 if(token.type == '}') {
360 parse_error("empty enum not allowed");
365 if(token.type != T_IDENTIFIER) {
366 parse_error_expected("problem while parsing enum entry",
373 if(token.type == '=') {
374 parse_constant_expression();
377 if(token.type != ',')
380 } while(token.type != '}');
385 static type_t *parse_enum_specifier(void)
389 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
390 enum_type->type.type = TYPE_ENUM;
391 enum_type->source_position = token.source_position;
393 if(token.type == T_IDENTIFIER) {
394 enum_type->symbol = token.v.symbol;
396 if(token.type == '{') {
397 parse_enum_type_entries();
399 } else if(token.type == '{') {
400 parse_enum_type_entries();
402 parse_error_expected("problem while parsing enum type specifiers",
406 return (type_t*) enum_type;
410 SPECIFIER_SIGNED = 1 << 0,
411 SPECIFIER_UNSIGNED = 1 << 1,
412 SPECIFIER_LONG = 1 << 2,
413 SPECIFIER_INT = 1 << 3,
414 SPECIFIER_DOUBLE = 1 << 4,
415 SPECIFIER_CHAR = 1 << 5,
416 SPECIFIER_SHORT = 1 << 6,
417 SPECIFIER_LONG_LONG = 1 << 7,
418 SPECIFIER_FLOAT = 1 << 8,
419 SPECIFIER_BOOL = 1 << 9,
420 SPECIFIER_VOID = 1 << 10,
421 #ifdef PROVIDE_COMPLEX
422 SPECIFIER_COMPLEX = 1 << 11,
424 #ifdef PROVIDE_IMAGINARY
425 SPECIFIER_IMAGINARY = 1 << 12,
429 #define STORAGE_CLASSES \
436 #define TYPE_QUALIFIERS \
441 case T___extension__:
443 #ifdef PROVIDE_COMPLEX
444 #define COMPLEX_SPECIFIERS \
447 #define COMPLEX_SPECIFIERS
450 #ifdef PROVIDE_IMAGINARY
451 #define IMAGINARY_SPECIFIERS \
454 #define IMAGINARY_SPECIFIERS
457 #define TYPE_SPECIFIERS \
474 #define DECLARATION_START \
480 type_t *create_builtin_type(symbol_t *symbol)
482 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
483 type->type.type = TYPE_BUILTIN;
484 type->symbol = symbol;
486 type_t *result = typehash_insert((type_t*) type);
487 if(result != (type_t*) type) {
488 obstack_free(type_obst, type);
495 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
497 declaration_t *declaration;
499 unsigned type_qualifiers = 0;
500 unsigned type_specifiers = 0;
507 #define MATCH_STORAGE_CLASS(token, class) \
509 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
510 parse_error("multiple storage classes in declaration " \
513 specifiers->storage_class = class; \
517 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
518 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
519 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
520 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
521 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
523 /* type qualifiers */
524 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
526 type_qualifiers |= qualifier; \
530 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
531 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
532 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
533 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
535 case T___extension__:
540 /* type specifiers */
541 #define MATCH_SPECIFIER(token, specifier, name) \
544 if(type_specifiers & specifier) { \
545 parse_error("multiple " name " type specifiers given"); \
547 type_specifiers |= specifier; \
551 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
552 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
553 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
554 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
555 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
556 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
557 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
558 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
559 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
560 #ifdef PROVIDE_COMPLEX
561 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
563 #ifdef PROVIDE_IMAGINARY
564 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
568 if(type_specifiers & SPECIFIER_LONG_LONG) {
569 parse_error("multiple type specifiers given");
570 } else if(type_specifiers & SPECIFIER_LONG) {
571 type_specifiers |= SPECIFIER_LONG_LONG;
573 type_specifiers |= SPECIFIER_LONG;
577 /* TODO: if type != NULL for the following rules issue an error */
579 type = parse_struct_specifier();
582 type = parse_union_specifier();
585 type = parse_enum_specifier();
587 case T___builtin_va_list:
588 type = create_builtin_type(token.v.symbol);
593 declaration = token.v.symbol->declaration;
594 if(declaration == NULL ||
595 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
596 goto finish_specifiers;
599 type = declaration->type;
600 assert(type != NULL);
604 /* function specifier */
606 goto finish_specifiers;
613 atomic_type_type_t atomic_type;
615 /* match valid basic types */
616 switch(type_specifiers) {
618 atomic_type = ATOMIC_TYPE_VOID;
621 atomic_type = ATOMIC_TYPE_CHAR;
623 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
624 atomic_type = ATOMIC_TYPE_SCHAR;
626 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
627 atomic_type = ATOMIC_TYPE_UCHAR;
629 case SPECIFIER_SHORT:
630 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
631 case SPECIFIER_SHORT | SPECIFIER_INT:
632 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
633 atomic_type = ATOMIC_TYPE_SHORT;
635 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
636 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
637 atomic_type = ATOMIC_TYPE_USHORT;
640 case SPECIFIER_SIGNED:
641 case SPECIFIER_SIGNED | SPECIFIER_INT:
642 atomic_type = ATOMIC_TYPE_INT;
644 case SPECIFIER_UNSIGNED:
645 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
646 atomic_type = ATOMIC_TYPE_UINT;
649 case SPECIFIER_SIGNED | SPECIFIER_LONG:
650 case SPECIFIER_LONG | SPECIFIER_INT:
651 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
652 atomic_type = ATOMIC_TYPE_LONG;
654 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
655 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
656 atomic_type = ATOMIC_TYPE_ULONG;
658 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
659 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
660 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
661 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
663 atomic_type = ATOMIC_TYPE_LONGLONG;
665 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
666 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
668 atomic_type = ATOMIC_TYPE_ULONGLONG;
670 case SPECIFIER_FLOAT:
671 atomic_type = ATOMIC_TYPE_FLOAT;
673 case SPECIFIER_DOUBLE:
674 atomic_type = ATOMIC_TYPE_DOUBLE;
676 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
677 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
680 atomic_type = ATOMIC_TYPE_BOOL;
682 #ifdef PROVIDE_COMPLEX
683 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
684 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
686 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
687 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
689 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
690 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
693 #ifdef PROVIDE_IMAGINARY
694 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
695 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
697 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
698 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
700 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
701 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
705 /* invalid specifier combination, give an error message */
706 if(type_specifiers == 0) {
707 parse_error("no type specifiers given in declaration");
708 } else if((type_specifiers & SPECIFIER_SIGNED) &&
709 (type_specifiers & SPECIFIER_UNSIGNED)) {
710 parse_error("signed and unsigned specifiers gives");
711 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
712 parse_error("only integer types can be signed or unsigned");
714 parse_error("multiple datatypes in declaration");
716 atomic_type = ATOMIC_TYPE_INVALID;
719 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
720 atype->type.type = TYPE_ATOMIC;
721 atype->atype = atomic_type;
724 type = (type_t*) atype;
726 if(type_specifiers != 0) {
727 parse_error("multiple datatypes in declaration");
731 type->qualifiers = type_qualifiers;
733 type_t *result = typehash_insert(type);
734 if(newtype && result != (type_t*) type) {
735 obstack_free(type_obst, type);
738 specifiers->type = result;
742 unsigned parse_type_qualifiers(void)
744 unsigned type_qualifiers = 0;
748 /* type qualifiers */
749 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
750 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
751 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
752 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
755 return type_qualifiers;
760 typedef struct parsed_pointer_t parsed_pointer_t;
761 struct parsed_pointer_t {
762 unsigned type_qualifiers;
763 parsed_pointer_t *next;
767 parsed_pointer_t *parse_pointers(void)
769 parsed_pointer_t *result = NULL;
770 parsed_pointer_t *last_pointer = NULL;
772 while(token.type == '*') {
774 parsed_pointer_t *pointer
775 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
776 pointer->type_qualifiers = parse_type_qualifiers();
778 if(last_pointer != NULL) {
779 last_pointer->next = pointer;
783 last_pointer = pointer;
790 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
792 for( ; pointer != NULL; pointer = pointer->next) {
793 pointer_type_t *pointer_type
794 = allocate_type_zero(sizeof(pointer_type[0]));
795 pointer_type->type.type = TYPE_POINTER;
796 pointer_type->points_to = type;
797 pointer_type->type.qualifiers = pointer->type_qualifiers;
799 type_t *result = typehash_insert((type_t*) pointer_type);
800 if(result != (type_t*) pointer_type) {
801 obstack_free(type_obst, pointer_type);
811 void parse_identifier_list(void)
814 if(token.type != T_IDENTIFIER) {
815 parse_error_expected("problem while parsing parameter identifier "
816 "list", T_IDENTIFIER, 0);
820 if(token.type != ',')
827 declaration_t *parse_parameter(void)
829 declaration_specifiers_t specifiers;
830 memset(&specifiers, 0, sizeof(specifiers));
832 parse_declaration_specifiers(&specifiers);
834 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
835 parse_declarator(declaration, specifiers.storage_class,
842 void parse_parameters(method_type_t *type)
844 if(token.type == T_IDENTIFIER) {
845 symbol_t *symbol = token.v.symbol;
846 declaration_t *declaration = symbol->declaration;
847 if(declaration == NULL
848 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
849 parse_identifier_list();
854 if(token.type == ')') {
855 type->unspecified_parameters = 1;
858 if(token.type == T_void && la(1)->type == ')') {
863 declaration_t *declaration;
864 method_parameter_type_t *parameter_type;
865 method_parameter_type_t *last_parameter_type = NULL;
876 declaration = parse_parameter();
878 parameter_type = allocate_type_zero(sizeof(parameter_type[0]));
879 parameter_type->type = declaration->type;
880 parameter_type->symbol = declaration->symbol;
882 if(last_parameter_type != NULL) {
883 last_parameter_type->next = parameter_type;
885 type->parameter_types = parameter_type;
887 last_parameter_type = parameter_type;
893 if(token.type != ',')
900 void parse_attributes(void)
902 while(token.type == T___attribute__) {
910 parse_error("EOF while parsing attribute");
927 typedef struct declarator_part declarator_part;
928 struct declarator_part {
929 parsed_pointer_t *pointers;
930 method_type_t *method_type;
931 declarator_part *inner;
936 declarator_part *parse_inner_declarator(declaration_t *declaration,
939 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
940 memset(part, 0, sizeof(part[0]));
942 part->pointers = parse_pointers();
946 if(declaration == NULL) {
947 parse_error("no identifier expected in typename");
949 declaration->symbol = token.v.symbol;
950 declaration->source_position = token.source_position;
956 part->inner = parse_inner_declarator(declaration, may_be_abstract);
962 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
971 method_type_t *method_type
972 = allocate_type_zero(sizeof(method_type[0]));
973 method_type->type.type = TYPE_METHOD;
975 parse_parameters(method_type);
977 part->method_type = method_type;
984 if(token.type == T_static) {
988 unsigned type_qualifiers = parse_type_qualifiers();
989 if(type_qualifiers != 0) {
990 if(token.type == T_static) {
997 if(token.type == '*' && la(1)->type == ']') {
999 } else if(token.type != ']') {
1000 parse_assignment_expression();
1006 goto declarator_finished;
1010 declarator_finished:
1017 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1020 type = make_pointers(type, part->pointers);
1022 method_type_t *method_type = part->method_type;
1023 if(method_type != NULL) {
1024 method_type->result_type = type;
1026 type = (type_t*) method_type;
1030 } while(part != NULL);
1036 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1037 type_t *type, int may_be_abstract)
1039 declarator_part *part
1040 = parse_inner_declarator(declaration, may_be_abstract);
1043 declaration->type = construct_declarator_type(part, type);
1044 declaration->storage_class = storage_class;
1045 obstack_free(&temp_obst, part);
1050 type_t *parse_abstract_declarator(type_t *base_type)
1052 declarator_part *part = parse_inner_declarator(NULL, 1);
1054 type_t *result = construct_declarator_type(part, base_type);
1055 obstack_free(&temp_obst, part);
1060 static void record_declaration(declaration_t *declaration)
1062 if(last_declaration != NULL) {
1063 last_declaration->next = declaration;
1066 context->declarations = declaration;
1068 last_declaration = declaration;
1072 void maybe_push_declaration(declaration_t *declaration)
1074 symbol_t *symbol = declaration->symbol;
1076 if(symbol != NULL) {
1077 environment_push(declaration, context);
1082 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1085 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1087 parse_declarator(declaration, specifiers->storage_class,
1088 specifiers->type, 0);
1089 maybe_push_declaration(declaration);
1090 record_declaration(declaration);
1091 if(token.type == '=') {
1093 if(token.type == '{') {
1097 parse_assignment_expression();
1099 } else if(token.type == '{') {
1100 statement_t *statement = parse_compound_statement();
1101 declaration->statement = statement;
1105 if(token.type != ',')
1113 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1116 if(token.type == ':') {
1118 parse_constant_expression();
1119 /* TODO (bitfields) */
1121 declaration_t *declaration
1122 = allocate_ast_zero(sizeof(declaration[0]));
1123 parse_declarator(declaration, specifiers->storage_class,
1124 specifiers->type, 0);
1125 maybe_push_declaration(declaration);
1126 record_declaration(declaration);
1128 if(token.type == ':') {
1130 parse_constant_expression();
1131 /* TODO (bitfields) */
1135 if(token.type != ',')
1142 static void parse_compound_type_entries(void)
1146 while(token.type != '}' && token.type != T_EOF) {
1147 declaration_specifiers_t specifiers;
1148 memset(&specifiers, 0, sizeof(specifiers));
1149 /* TODO not correct as this allows storage class stuff... but only
1150 * specifiers and qualifiers sould be allowed here */
1151 parse_declaration_specifiers(&specifiers);
1153 parse_struct_declarators(&specifiers);
1155 if(token.type == T_EOF) {
1156 parse_error("unexpected error while parsing struct");
1161 void parse_declaration(void)
1163 declaration_specifiers_t specifiers;
1164 memset(&specifiers, 0, sizeof(specifiers));
1165 parse_declaration_specifiers(&specifiers);
1167 if(token.type == ';') {
1171 parse_init_declarators(&specifiers);
1174 type_t *parse_typename(void)
1176 declaration_specifiers_t specifiers;
1177 memset(&specifiers, 0, sizeof(specifiers));
1178 parse_declaration_specifiers(&specifiers);
1179 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1180 /* TODO: improve error message, user does probably not know what a
1181 * storage class is...
1183 parse_error("typename may not have a storage class");
1186 type_t *result = parse_abstract_declarator(specifiers.type);
1194 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1195 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1196 expression_t *left);
1198 typedef struct expression_parser_function_t expression_parser_function_t;
1199 struct expression_parser_function_t {
1200 unsigned precedence;
1201 parse_expression_function parser;
1202 unsigned infix_precedence;
1203 parse_expression_infix_function infix_parser;
1206 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1209 expression_t *expected_expression_error(void)
1211 parser_print_error_prefix();
1212 fprintf(stderr, "expected expression, got token ");
1213 print_token(stderr, & token);
1214 fprintf(stderr, "\n");
1216 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1217 expression->type = EXPR_INVALID;
1224 expression_t *parse_string_const(void)
1226 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1228 cnst->expression.type = EXPR_STRING_LITERAL;
1229 cnst->value = token.v.string;
1233 return (expression_t*) cnst;
1237 expression_t *parse_int_const(void)
1239 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1241 cnst->expression.type = EXPR_CONST;
1242 cnst->value = token.v.intvalue;
1246 return (expression_t*) cnst;
1250 expression_t *parse_reference(void)
1252 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1254 ref->expression.type = EXPR_REFERENCE;
1255 ref->symbol = token.v.symbol;
1259 return (expression_t*) ref;
1263 expression_t *parse_brace_expression(void)
1267 expression_t *result = parse_expression();
1274 expression_t *parse_primary_expression(void)
1276 switch(token.type) {
1278 return parse_int_const();
1279 case T_STRING_LITERAL:
1280 return parse_string_const();
1282 return parse_reference();
1284 return parse_brace_expression();
1287 /* TODO: error message */
1292 expression_t *parse_array_expression(unsigned precedence,
1293 expression_t *array_ref)
1299 array_access_expression_t *array_access
1300 = allocate_ast_zero(sizeof(array_access[0]));
1302 array_access->expression.type = EXPR_ARRAY_ACCESS;
1303 array_access->array_ref = array_ref;
1304 array_access->index = parse_expression();
1306 if(token.type != ']') {
1307 parse_error_expected("Problem while parsing array access", ']', 0);
1312 return (expression_t*) array_access;
1316 type_t *get_expression_type(const expression_t *expression)
1324 expression_t *parse_sizeof(unsigned precedence)
1328 sizeof_expression_t *sizeof_expression
1329 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1330 sizeof_expression->expression.type = EXPR_SIZEOF;
1332 if(token.type == '(' /* && LA1 is type_specifier */) {
1334 sizeof_expression->type = parse_typename();
1337 expression_t *expression = parse_sub_expression(precedence);
1338 sizeof_expression->type = get_expression_type(expression);
1341 return (expression_t*) sizeof_expression;
1345 expression_t *parse_select_expression(unsigned precedence,
1346 expression_t *compound)
1350 assert(token.type == '.' || token.type == T_SELECT);
1353 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1355 select->expression.type = EXPR_SELECT;
1356 select->compound = compound;
1358 if(token.type != T_IDENTIFIER) {
1359 parse_error_expected("Problem while parsing compound select",
1363 select->symbol = token.v.symbol;
1366 return (expression_t*) select;
1370 expression_t *parse_call_expression(unsigned precedence,
1371 expression_t *expression)
1374 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1376 call->expression.type = EXPR_CALL;
1377 call->method = expression;
1379 /* parse arguments */
1382 if(token.type != ')') {
1383 call_argument_t *last_argument = NULL;
1386 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1388 argument->expression = parse_expression();
1389 if(last_argument == NULL) {
1390 call->arguments = argument;
1392 last_argument->next = argument;
1394 last_argument = argument;
1396 if(token.type != ',')
1403 return (expression_t*) call;
1407 expression_t *parse_conditional_expression(unsigned precedence,
1408 expression_t *expression)
1412 conditional_expression_t *conditional
1413 = allocate_ast_zero(sizeof(conditional[0]));
1414 conditional->condition = expression;
1416 conditional->true_expression = parse_expression();
1418 conditional->false_expression = parse_sub_expression(precedence);
1420 return (expression_t*) conditional;
1423 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1425 expression_t *parse_##unexpression_type(unsigned precedence) \
1429 unary_expression_t *unary_expression \
1430 = allocate_ast_zero(sizeof(unary_expression[0])); \
1431 unary_expression->expression.type = EXPR_UNARY; \
1432 unary_expression->type = unexpression_type; \
1433 unary_expression->value = parse_sub_expression(precedence); \
1435 return (expression_t*) unary_expression; \
1438 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1439 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1440 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1441 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1442 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1443 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1444 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1445 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1447 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1449 expression_t *parse_##unexpression_type(unsigned precedence, \
1450 expression_t *left) \
1452 (void) precedence; \
1455 unary_expression_t *unary_expression \
1456 = allocate_ast_zero(sizeof(unary_expression[0])); \
1457 unary_expression->expression.type = EXPR_UNARY; \
1458 unary_expression->type = unexpression_type; \
1459 unary_expression->value = left; \
1461 return (expression_t*) unary_expression; \
1464 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1465 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1467 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1469 expression_t *parse_##binexpression_type(unsigned precedence, \
1470 expression_t *left) \
1474 expression_t *right = parse_sub_expression(precedence); \
1476 binary_expression_t *binexpr \
1477 = allocate_ast_zero(sizeof(binexpr[0])); \
1478 binexpr->expression.type = EXPR_BINARY; \
1479 binexpr->type = binexpression_type; \
1480 binexpr->left = left; \
1481 binexpr->right = right; \
1483 return (expression_t*) binexpr; \
1486 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1487 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1488 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1489 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1490 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1491 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1492 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1493 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1494 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1495 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1496 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1497 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1498 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1499 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1500 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1501 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1502 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1503 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1506 expression_t *parse_sub_expression(unsigned precedence)
1508 if(token.type < 0) {
1509 return expected_expression_error();
1512 expression_parser_function_t *parser
1513 = &expression_parsers[token.type];
1514 source_position_t source_position = token.source_position;
1517 if(parser->parser != NULL) {
1518 left = parser->parser(parser->precedence);
1520 left = parse_primary_expression();
1523 left->source_position = source_position;
1526 if(token.type < 0) {
1527 return expected_expression_error();
1530 parser = &expression_parsers[token.type];
1531 if(parser->infix_parser == NULL)
1533 if(parser->infix_precedence < precedence)
1536 left = parser->infix_parser(parser->infix_precedence, left);
1538 left->source_position = source_position;
1545 expression_t *parse_expression(void)
1547 return parse_sub_expression(1);
1552 void register_expression_parser(parse_expression_function parser,
1553 int token_type, unsigned precedence)
1555 expression_parser_function_t *entry = &expression_parsers[token_type];
1557 if(entry->parser != NULL) {
1558 fprintf(stderr, "for token ");
1559 print_token_type(stderr, token_type);
1560 fprintf(stderr, "\n");
1561 panic("trying to register multiple expression parsers for a token");
1563 entry->parser = parser;
1564 entry->precedence = precedence;
1567 void register_expression_infix_parser(parse_expression_infix_function parser,
1568 int token_type, unsigned precedence)
1570 expression_parser_function_t *entry = &expression_parsers[token_type];
1572 if(entry->infix_parser != NULL) {
1573 fprintf(stderr, "for token ");
1574 print_token_type(stderr, token_type);
1575 fprintf(stderr, "\n");
1576 panic("trying to register multiple infix expression parsers for a "
1579 entry->infix_parser = parser;
1580 entry->infix_precedence = precedence;
1584 void init_expression_parsers(void)
1586 memset(&expression_parsers, 0, sizeof(expression_parsers));
1588 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1589 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1590 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1592 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1593 T_GREATERGREATER, 16);
1594 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1595 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1596 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1597 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1598 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1599 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1600 T_GREATEREQUAL, 14);
1601 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1602 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1603 T_EXCLAMATIONMARKEQUAL, 13);
1604 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1605 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1606 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1607 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1608 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1609 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1610 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1612 register_expression_infix_parser(parse_array_expression, '[', 30);
1613 register_expression_infix_parser(parse_call_expression, '(', 30);
1614 register_expression_infix_parser(parse_select_expression, '.', 30);
1615 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1616 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1618 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1621 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1622 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1623 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1624 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1625 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1626 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1627 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1628 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1629 register_expression_parser(parse_sizeof, T_sizeof, 25);
1634 statement_t *parse_case_statement(void)
1645 statement_t *parse_default_statement(void)
1655 statement_t *parse_label_statement(void)
1665 statement_t *parse_if(void)
1673 if(token.type == T_else) {
1682 statement_t *parse_switch(void)
1694 statement_t *parse_while(void)
1706 statement_t *parse_do(void)
1719 statement_t *parse_for(void)
1723 if(token.type != ';') {
1724 /* TODO not correct... this could also be a declaration */
1728 if(token.type != ';') {
1732 if(token.type != ')') {
1742 statement_t *parse_goto(void)
1745 expect(T_IDENTIFIER);
1752 statement_t *parse_continue(void)
1757 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1758 statement->source_position = token.source_position;
1759 statement->type = STATEMENT_CONTINUE;
1765 statement_t *parse_break(void)
1774 statement_t *parse_return(void)
1778 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1779 statement->statement.type = STATEMENT_RETURN;
1780 if(token.type != ';') {
1781 statement->return_value = parse_expression();
1785 return (statement_t*) statement;
1789 statement_t *parse_declaration_statement(void)
1791 parse_declaration();
1796 statement_t *parse_expression_statement(void)
1803 statement_t *parse_statement(void)
1805 declaration_t *declaration;
1806 statement_t *statement = NULL;
1808 /* declaration or statement */
1809 switch(token.type) {
1811 statement = parse_case_statement();
1815 statement = parse_default_statement();
1819 statement = parse_compound_statement();
1823 statement = parse_if();
1827 statement = parse_switch();
1831 statement = parse_while();
1835 statement = parse_do();
1839 statement = parse_for();
1843 statement = parse_goto();
1847 statement = parse_continue();
1851 statement = parse_break();
1855 statement = parse_return();
1863 if(la(1)->type == ':') {
1864 statement = parse_label_statement();
1868 declaration = token.v.symbol->declaration;
1869 if(declaration != NULL &&
1870 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1871 statement = parse_declaration_statement();
1875 statement = parse_expression_statement();
1879 statement = parse_declaration_statement();
1887 statement_t *parse_compound_statement(void)
1891 compound_statement_t *compound_statement
1892 = allocate_ast_zero(sizeof(compound_statement[0]));
1893 compound_statement->statement.type = STATEMENT_COMPOUND;
1895 int top = environment_top();
1896 context_t *last_context = context;
1897 set_context(&compound_statement->context);
1899 statement_t *last_statement = NULL;
1901 while(token.type != '}') {
1902 statement_t *statement = parse_statement();
1904 if(last_statement != NULL) {
1905 last_statement->next = statement;
1907 compound_statement->statements = statement;
1909 last_statement = statement;
1912 assert(context == &compound_statement->context);
1913 set_context(last_context);
1914 environment_pop_to(top);
1918 return (statement_t*) compound_statement;
1922 translation_unit_t *parse_translation_unit(void)
1924 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1926 assert(context == NULL);
1927 set_context(&unit->context);
1929 while(token.type != T_EOF) {
1930 parse_declaration();
1933 assert(context == &unit->context);
1935 last_declaration = NULL;
1940 translation_unit_t *parse(void)
1942 obstack_init(&environment_obstack);
1943 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1945 lookahead_bufpos = 0;
1946 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1949 translation_unit_t *unit = parse_translation_unit();
1951 DEL_ARR_F(environment_stack);
1952 obstack_free(&environment_obstack, NULL);
1957 void init_parser(void)
1959 init_expression_parsers();
1960 obstack_init(&temp_obst);
1963 void exit_parser(void)
1965 obstack_free(&temp_obst, NULL);