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 static void maybe_push_declaration(declaration_t *declaration);
279 static void record_declaration(declaration_t *declaration);
281 typedef struct declaration_specifiers_t declaration_specifiers_t;
282 struct declaration_specifiers_t {
283 storage_class_t storage_class;
287 static type_t *parse_struct_specifier(void)
291 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
292 struct_type->type.type = TYPE_COMPOUND_STRUCT;
293 struct_type->source_position = token.source_position;
295 int top = environment_top();
296 context_t *last_context = context;
297 set_context(&struct_type->context);
299 if(token.type == T_IDENTIFIER) {
301 if(token.type == '{') {
302 parse_compound_type_entries();
304 } else if(token.type == '{') {
305 parse_compound_type_entries();
307 parse_error_expected("problem while parsing struct type specifiers",
308 T_IDENTIFIER, '{', 0);
312 assert(context == &struct_type->context);
313 set_context(last_context);
314 environment_pop_to(top);
316 return (type_t*) struct_type;
319 static type_t *parse_union_specifier(void)
323 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
324 union_type->type.type = TYPE_COMPOUND_UNION;
325 union_type->source_position = token.source_position;
327 int top = environment_top();
328 context_t *last_context = context;
329 set_context(&union_type->context);
331 if(token.type == T_IDENTIFIER) {
332 union_type->symbol = token.v.symbol;
334 if(token.type == '{') {
335 parse_compound_type_entries();
337 } else if(token.type == '{') {
338 parse_compound_type_entries();
340 parse_error_expected("problem while parsing union type specifiers",
345 assert(context == &union_type->context);
346 set_context(last_context);
347 environment_pop_to(top);
349 return (type_t*) union_type;
352 static void parse_enum_type_entries()
356 if(token.type == '}') {
358 parse_error("empty enum not allowed");
363 if(token.type != T_IDENTIFIER) {
364 parse_error_expected("problem while parsing enum entry",
371 if(token.type == '=') {
372 parse_constant_expression();
375 if(token.type != ',')
378 } while(token.type != '}');
383 static type_t *parse_enum_specifier(void)
387 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
388 enum_type->type.type = TYPE_ENUM;
389 enum_type->source_position = token.source_position;
391 if(token.type == T_IDENTIFIER) {
392 enum_type->symbol = token.v.symbol;
394 if(token.type == '{') {
395 parse_enum_type_entries();
397 } else if(token.type == '{') {
398 parse_enum_type_entries();
400 parse_error_expected("problem while parsing enum type specifiers",
404 return (type_t*) enum_type;
408 SPECIFIER_SIGNED = 1 << 0,
409 SPECIFIER_UNSIGNED = 1 << 1,
410 SPECIFIER_LONG = 1 << 2,
411 SPECIFIER_INT = 1 << 3,
412 SPECIFIER_DOUBLE = 1 << 4,
413 SPECIFIER_CHAR = 1 << 5,
414 SPECIFIER_SHORT = 1 << 6,
415 SPECIFIER_LONG_LONG = 1 << 7,
416 SPECIFIER_FLOAT = 1 << 8,
417 SPECIFIER_BOOL = 1 << 9,
418 SPECIFIER_VOID = 1 << 10,
419 #ifdef PROVIDE_COMPLEX
420 SPECIFIER_COMPLEX = 1 << 11,
422 #ifdef PROVIDE_IMAGINARY
423 SPECIFIER_IMAGINARY = 1 << 12,
427 #define STORAGE_CLASSES \
434 #define TYPE_QUALIFIERS \
439 case T___extension__:
441 #ifdef PROVIDE_COMPLEX
442 #define COMPLEX_SPECIFIERS \
445 #define COMPLEX_SPECIFIERS
448 #ifdef PROVIDE_IMAGINARY
449 #define IMAGINARY_SPECIFIERS \
452 #define IMAGINARY_SPECIFIERS
455 #define TYPE_SPECIFIERS \
472 #define DECLARATION_START \
478 type_t *create_builtin_type(symbol_t *symbol)
480 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
481 type->type.type = TYPE_BUILTIN;
482 type->symbol = symbol;
484 type_t *result = typehash_insert((type_t*) type);
485 if(result != (type_t*) type) {
486 obstack_free(type_obst, type);
493 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
495 declaration_t *declaration;
497 unsigned type_qualifiers = 0;
498 unsigned type_specifiers = 0;
505 #define MATCH_STORAGE_CLASS(token, class) \
507 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
508 parse_error("multiple storage classes in declaration " \
511 specifiers->storage_class = class; \
515 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
516 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
517 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
518 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
519 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
521 /* type qualifiers */
522 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
524 type_qualifiers |= qualifier; \
528 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
529 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
530 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
531 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
533 case T___extension__:
538 /* type specifiers */
539 #define MATCH_SPECIFIER(token, specifier, name) \
542 if(type_specifiers & specifier) { \
543 parse_error("multiple " name " type specifiers given"); \
545 type_specifiers |= specifier; \
549 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
550 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
551 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
552 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
553 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
554 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
555 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
556 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
557 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
558 #ifdef PROVIDE_COMPLEX
559 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
561 #ifdef PROVIDE_IMAGINARY
562 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
566 if(type_specifiers & SPECIFIER_LONG_LONG) {
567 parse_error("multiple type specifiers given");
568 } else if(type_specifiers & SPECIFIER_LONG) {
569 type_specifiers |= SPECIFIER_LONG_LONG;
571 type_specifiers |= SPECIFIER_LONG;
575 /* TODO: if type != NULL for the following rules issue an error */
577 type = parse_struct_specifier();
580 type = parse_union_specifier();
583 type = parse_enum_specifier();
585 case T___builtin_va_list:
586 type = create_builtin_type(token.v.symbol);
591 declaration = token.v.symbol->declaration;
592 if(declaration == NULL ||
593 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
594 goto finish_specifiers;
597 type = declaration->type;
598 assert(type != NULL);
602 /* function specifier */
604 goto finish_specifiers;
611 atomic_type_type_t atomic_type;
613 /* match valid basic types */
614 switch(type_specifiers) {
616 atomic_type = ATOMIC_TYPE_VOID;
619 atomic_type = ATOMIC_TYPE_CHAR;
621 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
622 atomic_type = ATOMIC_TYPE_SCHAR;
624 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
625 atomic_type = ATOMIC_TYPE_UCHAR;
627 case SPECIFIER_SHORT:
628 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
629 case SPECIFIER_SHORT | SPECIFIER_INT:
630 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
631 atomic_type = ATOMIC_TYPE_SHORT;
633 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
634 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
635 atomic_type = ATOMIC_TYPE_USHORT;
638 case SPECIFIER_SIGNED:
639 case SPECIFIER_SIGNED | SPECIFIER_INT:
640 atomic_type = ATOMIC_TYPE_INT;
642 case SPECIFIER_UNSIGNED:
643 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
644 atomic_type = ATOMIC_TYPE_UINT;
647 case SPECIFIER_SIGNED | SPECIFIER_LONG:
648 case SPECIFIER_LONG | SPECIFIER_INT:
649 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
650 atomic_type = ATOMIC_TYPE_LONG;
652 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
653 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
654 atomic_type = ATOMIC_TYPE_ULONG;
656 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
657 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
658 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
659 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
661 atomic_type = ATOMIC_TYPE_LONGLONG;
663 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
664 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
666 atomic_type = ATOMIC_TYPE_ULONGLONG;
668 case SPECIFIER_FLOAT:
669 atomic_type = ATOMIC_TYPE_FLOAT;
671 case SPECIFIER_DOUBLE:
672 atomic_type = ATOMIC_TYPE_DOUBLE;
674 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
675 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
678 atomic_type = ATOMIC_TYPE_BOOL;
680 #ifdef PROVIDE_COMPLEX
681 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
682 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
684 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
685 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
687 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
688 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
691 #ifdef PROVIDE_IMAGINARY
692 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
693 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
695 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
696 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
698 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
699 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
703 /* invalid specifier combination, give an error message */
704 if(type_specifiers == 0) {
705 parse_error("no type specifiers given in declaration");
706 } else if((type_specifiers & SPECIFIER_SIGNED) &&
707 (type_specifiers & SPECIFIER_UNSIGNED)) {
708 parse_error("signed and unsigned specifiers gives");
709 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
710 parse_error("only integer types can be signed or unsigned");
712 parse_error("multiple datatypes in declaration");
714 atomic_type = ATOMIC_TYPE_INVALID;
717 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
718 atype->type.type = TYPE_ATOMIC;
719 atype->atype = atomic_type;
722 type = (type_t*) atype;
724 if(type_specifiers != 0) {
725 parse_error("multiple datatypes in declaration");
729 type->qualifiers = type_qualifiers;
731 type_t *result = typehash_insert(type);
732 if(newtype && result != (type_t*) type) {
733 obstack_free(type_obst, type);
736 specifiers->type = result;
740 unsigned parse_type_qualifiers()
742 unsigned type_qualifiers = 0;
746 /* type qualifiers */
747 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
748 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
749 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
750 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
753 return type_qualifiers;
759 type_t *parse_pointer(type_t *type)
761 while(token.type == '*') {
765 pointer_type_t *pointer_type
766 = allocate_type_zero(sizeof(pointer_type[0]));
767 pointer_type->type.type = TYPE_POINTER;
768 pointer_type->points_to = type;
770 pointer_type->type.qualifiers = parse_type_qualifiers();
772 type_t *result = typehash_insert((type_t*) pointer_type);
773 if(result != (type_t*) pointer_type) {
774 obstack_free(type_obst, pointer_type);
784 void parse_identifier_list()
787 if(token.type != T_IDENTIFIER) {
788 parse_error_expected("problem while parsing parameter identifier "
789 "list", T_IDENTIFIER, 0);
793 if(token.type != ',')
800 void parse_parameter()
802 if(token.type == T_DOTDOTDOT) {
807 declaration_specifiers_t specifiers;
808 memset(&specifiers, 0, sizeof(specifiers));
810 parse_declaration_specifiers(&specifiers);
811 specifiers.type = parse_pointer(specifiers.type);
813 if(token.type == '(' || token.type == T_IDENTIFIER) {
814 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
815 parse_declarator(declaration, specifiers.storage_class,
817 maybe_push_declaration(declaration);
818 record_declaration(declaration);
823 void parse_parameters()
825 if(token.type == T_IDENTIFIER) {
826 symbol_t *symbol = token.v.symbol;
827 declaration_t *declaration = symbol->declaration;
828 if(declaration == NULL
829 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
830 parse_identifier_list();
845 if(token.type != ',')
852 void parse_attributes(void)
854 while(token.type == T___attribute__) {
862 parse_error("EOF while parsing attribute");
880 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
883 type = parse_pointer(type);
884 declaration->storage_class = storage_class;
885 declaration->type = type;
889 declaration->symbol = token.v.symbol;
890 declaration->source_position = token.source_position;
895 parse_declarator(declaration, storage_class, type);
899 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
908 int top = environment_top();
909 context_t *last_context = context;
910 set_context(&declaration->context);
914 assert(context == &declaration->context);
915 set_context(last_context);
916 environment_pop_to(top);
923 if(token.type == T_static) {
927 unsigned type_qualifiers = parse_type_qualifiers();
928 if(type_qualifiers != 0) {
929 if(token.type == T_static) {
934 if(token.type == '*' && la(1)->type == ']') {
936 } else if(token.type != ']') {
937 parse_assignment_expression();
943 goto declarator_finished;
951 static void record_declaration(declaration_t *declaration)
953 if(last_declaration != NULL) {
954 last_declaration->next = declaration;
957 context->declarations = declaration;
959 last_declaration = declaration;
963 void maybe_push_declaration(declaration_t *declaration)
965 symbol_t *symbol = declaration->symbol;
968 environment_push(declaration, context);
973 void parse_init_declarators(const declaration_specifiers_t *specifiers)
976 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
978 parse_declarator(declaration, specifiers->storage_class,
980 maybe_push_declaration(declaration);
981 record_declaration(declaration);
982 if(token.type == '=') {
984 if(token.type == '{') {
988 parse_assignment_expression();
990 } else if(token.type == '{') {
991 parse_compound_statement();
995 if(token.type != ',')
1003 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1006 if(token.type == ':') {
1008 parse_constant_expression();
1009 /* TODO (bitfields) */
1011 declaration_t *declaration
1012 = allocate_ast_zero(sizeof(declaration[0]));
1013 parse_declarator(declaration, specifiers->storage_class,
1015 maybe_push_declaration(declaration);
1016 record_declaration(declaration);
1018 if(token.type == ':') {
1020 parse_constant_expression();
1021 /* TODO (bitfields) */
1025 if(token.type != ',')
1032 static void parse_compound_type_entries(void)
1036 while(token.type != '}' && token.type != T_EOF) {
1037 declaration_specifiers_t specifiers;
1038 memset(&specifiers, 0, sizeof(specifiers));
1039 /* TODO not correct as this allows storage class stuff... but only
1040 * specifiers and qualifiers sould be allowed here */
1041 parse_declaration_specifiers(&specifiers);
1043 parse_struct_declarators(&specifiers);
1045 if(token.type == T_EOF) {
1046 parse_error("unexpected error while parsing struct");
1051 void parse_declaration(void)
1053 declaration_specifiers_t specifiers;
1054 memset(&specifiers, 0, sizeof(specifiers));
1055 parse_declaration_specifiers(&specifiers);
1057 if(token.type == ';') {
1061 parse_init_declarators(&specifiers);
1064 type_t *parse_typename(void)
1066 declaration_specifiers_t specifiers;
1067 memset(&specifiers, 0, sizeof(specifiers));
1068 /* TODO not correct storage class elements are not allowed here */
1069 parse_declaration_specifiers(&specifiers);
1071 specifiers.type = parse_pointer(specifiers.type);
1073 return specifiers.type;
1079 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1080 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1081 expression_t *left);
1083 typedef struct expression_parser_function_t expression_parser_function_t;
1084 struct expression_parser_function_t {
1085 unsigned precedence;
1086 parse_expression_function parser;
1087 unsigned infix_precedence;
1088 parse_expression_infix_function infix_parser;
1091 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1094 expression_t *expected_expression_error(void)
1096 parser_print_error_prefix();
1097 fprintf(stderr, "expected expression, got token ");
1098 print_token(stderr, & token);
1099 fprintf(stderr, "\n");
1101 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1102 expression->type = EXPR_INVALID;
1109 expression_t *parse_string_const(void)
1111 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1113 cnst->expression.type = EXPR_STRING_LITERAL;
1114 cnst->value = token.v.string;
1118 return (expression_t*) cnst;
1122 expression_t *parse_int_const(void)
1124 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1126 cnst->expression.type = EXPR_CONST;
1127 cnst->value = token.v.intvalue;
1131 return (expression_t*) cnst;
1135 expression_t *parse_reference(void)
1137 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1139 ref->expression.type = EXPR_REFERENCE;
1140 ref->symbol = token.v.symbol;
1144 return (expression_t*) ref;
1148 expression_t *parse_brace_expression(void)
1152 expression_t *result = parse_expression();
1159 expression_t *parse_primary_expression(void)
1161 switch(token.type) {
1163 return parse_int_const();
1164 case T_STRING_LITERAL:
1165 return parse_string_const();
1167 return parse_reference();
1169 return parse_brace_expression();
1172 /* TODO: error message */
1177 expression_t *parse_array_expression(unsigned precedence,
1178 expression_t *array_ref)
1184 array_access_expression_t *array_access
1185 = allocate_ast_zero(sizeof(array_access[0]));
1187 array_access->expression.type = EXPR_ARRAY_ACCESS;
1188 array_access->array_ref = array_ref;
1189 array_access->index = parse_expression();
1191 if(token.type != ']') {
1192 parse_error_expected("Problem while parsing array access", ']', 0);
1197 return (expression_t*) array_access;
1201 type_t *get_expression_type(const expression_t *expression)
1209 expression_t *parse_sizeof(unsigned precedence)
1213 sizeof_expression_t *sizeof_expression
1214 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1215 sizeof_expression->expression.type = EXPR_SIZEOF;
1217 if(token.type == '(' /* && LA1 is type_specifier */) {
1219 sizeof_expression->type = parse_typename();
1222 expression_t *expression = parse_sub_expression(precedence);
1223 sizeof_expression->type = get_expression_type(expression);
1226 return (expression_t*) sizeof_expression;
1230 expression_t *parse_select_expression(unsigned precedence,
1231 expression_t *compound)
1235 assert(token.type == '.' || token.type == T_SELECT);
1238 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1240 select->expression.type = EXPR_SELECT;
1241 select->compound = compound;
1243 if(token.type != T_IDENTIFIER) {
1244 parse_error_expected("Problem while parsing compound select",
1248 select->symbol = token.v.symbol;
1251 return (expression_t*) select;
1255 expression_t *parse_call_expression(unsigned precedence,
1256 expression_t *expression)
1259 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1261 call->expression.type = EXPR_CALL;
1262 call->method = expression;
1264 /* parse arguments */
1267 if(token.type != ')') {
1268 call_argument_t *last_argument = NULL;
1271 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1273 argument->expression = parse_expression();
1274 if(last_argument == NULL) {
1275 call->arguments = argument;
1277 last_argument->next = argument;
1279 last_argument = argument;
1281 if(token.type != ',')
1288 return (expression_t*) call;
1291 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1293 expression_t *parse_##unexpression_type(unsigned precedence) \
1297 unary_expression_t *unary_expression \
1298 = allocate_ast_zero(sizeof(unary_expression[0])); \
1299 unary_expression->expression.type = EXPR_UNARY; \
1300 unary_expression->type = unexpression_type; \
1301 unary_expression->value = parse_sub_expression(precedence); \
1303 return (expression_t*) unary_expression; \
1306 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1307 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1308 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1309 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1310 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1311 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1312 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1313 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1315 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1317 expression_t *parse_##unexpression_type(unsigned precedence, \
1318 expression_t *left) \
1320 (void) precedence; \
1323 unary_expression_t *unary_expression \
1324 = allocate_ast_zero(sizeof(unary_expression[0])); \
1325 unary_expression->expression.type = EXPR_UNARY; \
1326 unary_expression->type = unexpression_type; \
1327 unary_expression->value = left; \
1329 return (expression_t*) unary_expression; \
1332 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1333 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1335 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1337 expression_t *parse_##binexpression_type(unsigned precedence, \
1338 expression_t *left) \
1342 expression_t *right = parse_sub_expression(precedence); \
1344 binary_expression_t *binexpr \
1345 = allocate_ast_zero(sizeof(binexpr[0])); \
1346 binexpr->expression.type = EXPR_BINARY; \
1347 binexpr->type = binexpression_type; \
1348 binexpr->left = left; \
1349 binexpr->right = right; \
1351 return (expression_t*) binexpr; \
1354 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1355 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1356 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1357 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1358 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1359 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1360 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1361 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1362 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1363 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1364 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1365 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1366 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1367 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1368 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1369 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1372 expression_t *parse_sub_expression(unsigned precedence)
1374 if(token.type < 0) {
1375 return expected_expression_error();
1378 expression_parser_function_t *parser
1379 = &expression_parsers[token.type];
1380 source_position_t source_position = token.source_position;
1383 if(parser->parser != NULL) {
1384 left = parser->parser(parser->precedence);
1386 left = parse_primary_expression();
1389 left->source_position = source_position;
1392 if(token.type < 0) {
1393 return expected_expression_error();
1396 parser = &expression_parsers[token.type];
1397 if(parser->infix_parser == NULL)
1399 if(parser->infix_precedence < precedence)
1402 left = parser->infix_parser(parser->infix_precedence, left);
1404 left->source_position = source_position;
1411 expression_t *parse_expression(void)
1413 return parse_sub_expression(1);
1418 void register_expression_parser(parse_expression_function parser,
1419 int token_type, unsigned precedence)
1421 expression_parser_function_t *entry = &expression_parsers[token_type];
1423 if(entry->parser != NULL) {
1424 fprintf(stderr, "for token ");
1425 print_token_type(stderr, token_type);
1426 fprintf(stderr, "\n");
1427 panic("trying to register multiple expression parsers for a token");
1429 entry->parser = parser;
1430 entry->precedence = precedence;
1433 void register_expression_infix_parser(parse_expression_infix_function parser,
1434 int token_type, unsigned precedence)
1436 expression_parser_function_t *entry = &expression_parsers[token_type];
1438 if(entry->infix_parser != NULL) {
1439 fprintf(stderr, "for token ");
1440 print_token_type(stderr, token_type);
1441 fprintf(stderr, "\n");
1442 panic("trying to register multiple infix expression parsers for a "
1445 entry->infix_parser = parser;
1446 entry->infix_precedence = precedence;
1450 void init_expression_parsers(void)
1452 memset(&expression_parsers, 0, sizeof(expression_parsers));
1454 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1455 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1456 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1458 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1459 T_GREATERGREATER, 16);
1460 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1461 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1462 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1463 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1464 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1465 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1466 T_GREATEREQUAL, 14);
1467 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1468 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1469 T_EXCLAMATIONMARKEQUAL, 13);
1470 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1471 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1472 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1473 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1475 register_expression_infix_parser(parse_array_expression, '[', 30);
1476 register_expression_infix_parser(parse_call_expression, '(', 30);
1477 register_expression_infix_parser(parse_select_expression, '.', 30);
1478 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1479 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1481 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1484 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1485 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1486 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1487 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1488 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1489 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1490 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1491 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1492 register_expression_parser(parse_sizeof, T_sizeof, 25);
1497 statement_t *parse_case_statement(void)
1508 statement_t *parse_default_statement(void)
1518 statement_t *parse_label_statement(void)
1528 statement_t *parse_if(void)
1536 if(token.type == T_else) {
1545 statement_t *parse_switch(void)
1557 statement_t *parse_while(void)
1569 statement_t *parse_do(void)
1582 statement_t *parse_for(void)
1586 if(token.type != ';') {
1587 /* TODO not correct... this could also be a declaration */
1591 if(token.type != ';') {
1595 if(token.type != ')') {
1605 statement_t *parse_goto(void)
1608 expect(T_IDENTIFIER);
1615 statement_t *parse_continue(void)
1624 statement_t *parse_break(void)
1633 statement_t *parse_return(void)
1643 statement_t *parse_declaration_statement(void)
1645 parse_declaration();
1650 statement_t *parse_expression_statement(void)
1657 statement_t *parse_statement(void)
1659 declaration_t *declaration;
1660 statement_t *statement = NULL;
1662 /* declaration or statement */
1663 switch(token.type) {
1665 statement = parse_case_statement();
1669 statement = parse_default_statement();
1673 statement = parse_compound_statement();
1677 statement = parse_if();
1681 statement = parse_switch();
1685 statement = parse_while();
1689 statement = parse_do();
1693 statement = parse_for();
1697 statement = parse_goto();
1701 statement = parse_continue();
1705 statement = parse_break();
1709 statement = parse_return();
1717 if(la(1)->type == ':') {
1718 statement = parse_label_statement();
1722 declaration = token.v.symbol->declaration;
1723 if(declaration != NULL &&
1724 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1725 statement = parse_declaration_statement();
1729 statement = parse_expression_statement();
1733 statement = parse_declaration_statement();
1741 statement_t *parse_compound_statement(void)
1745 compound_statement_t *compound_statement
1746 = allocate_ast_zero(sizeof(compound_statement[0]));
1747 compound_statement->statement.type = STATEMENT_COMPOUND;
1749 int top = environment_top();
1750 context_t *last_context = context;
1751 set_context(&compound_statement->context);
1753 while(token.type != '}') {
1757 assert(context == &compound_statement->context);
1758 set_context(last_context);
1759 environment_pop_to(top);
1767 translation_unit_t *parse_translation_unit(void)
1769 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1771 assert(context == NULL);
1772 set_context(&unit->context);
1774 while(token.type != T_EOF) {
1775 parse_declaration();
1778 assert(context == &unit->context);
1780 last_declaration = NULL;
1785 translation_unit_t *parse(void)
1787 obstack_init(&environment_obstack);
1788 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1790 lookahead_bufpos = 0;
1791 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1794 translation_unit_t *unit = parse_translation_unit();
1796 DEL_ARR_F(environment_stack);
1797 obstack_free(&environment_obstack, NULL);
1802 void init_parser(void)
1804 init_expression_parsers();
1807 void exit_parser(void)