10 #include "type_hash.h"
12 #include "adt/bitfiddle.h"
13 #include "adt/error.h"
14 #include "adt/array.h"
16 //#define PRINT_TOKENS
17 //#define ABORT_ON_ERROR
18 #define MAX_LOOKAHEAD 2
20 struct environment_entry_t {
22 declaration_t *old_declaration;
23 const void *old_context;
27 static token_t lookahead_buffer[MAX_LOOKAHEAD];
28 static int lookahead_bufpos;
29 static struct obstack environment_obstack;
30 static environment_entry_t **environment_stack = NULL;
31 static context_t *context = NULL;
32 static declaration_t *last_declaration = NULL;
33 static struct obstack temp_obst;
36 statement_t *parse_compound_statement(void);
38 statement_t *parse_statement(void);
41 expression_t *parse_sub_expression(unsigned precedence);
43 expression_t *parse_expression(void);
46 void *allocate_ast_zero(size_t size)
48 void *res = allocate_ast(size);
54 void *allocate_type_zero(size_t size)
56 void *res = obstack_alloc(type_obst, size);
62 * returns the top element of the environment stack
65 size_t environment_top(void)
67 return ARR_LEN(environment_stack);
75 token = lookahead_buffer[lookahead_bufpos];
76 lookahead_buffer[lookahead_bufpos] = lexer_token;
79 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
82 print_token(stderr, &token);
83 fprintf(stderr, "\n");
88 const token_t *la(int num)
90 assert(num > 0 && num <= MAX_LOOKAHEAD);
91 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
92 return & lookahead_buffer[pos];
96 void eat(token_type_t type)
98 assert(token.type == type);
102 void parser_print_error_prefix_pos(const source_position_t source_position)
104 fputs(source_position.input_name, stderr);
106 fprintf(stderr, "%d", source_position.linenr);
107 fputs(": error: ", stderr);
108 #ifdef ABORT_ON_ERROR
113 void parser_print_error_prefix(void)
115 parser_print_error_prefix_pos(token.source_position);
119 void parse_error(const char *message)
121 parser_print_error_prefix();
122 fprintf(stderr, "parse error: %s\n", message);
126 void parse_error_expected(const char *message, ...)
131 if(message != NULL) {
132 parser_print_error_prefix();
133 fprintf(stderr, "%s\n", message);
135 parser_print_error_prefix();
136 fputs("Parse error: got ", stderr);
137 print_token(stderr, &token);
138 fputs(", expected ", stderr);
140 va_start(args, message);
141 token_type_t token_type = va_arg(args, token_type_t);
142 while(token_type != 0) {
146 fprintf(stderr, ", ");
148 print_token_type(stderr, token_type);
149 token_type = va_arg(args, token_type_t);
152 fprintf(stderr, "\n");
156 void eat_until(int token_type)
158 while(token.type != token_type) {
159 if(token.type == T_EOF)
166 #define expect(expected) \
167 if(UNLIKELY(token.type != (expected))) { \
168 parse_error_expected(NULL, (expected), 0); \
174 #define expect_void(expected) \
175 if(UNLIKELY(token.type != (expected))) { \
176 parse_error_expected(NULL, (expected), 0); \
182 static void set_context(context_t *new_context)
184 context = new_context;
186 declaration_t *declaration = new_context->declarations;
187 if(declaration != NULL) {
189 if(declaration->next == NULL)
191 declaration = declaration->next;
195 last_declaration = declaration;
199 * pushs an environment_entry on the environment stack and links the
200 * corresponding symbol to the new entry
203 void environment_push(declaration_t *declaration, const void *context)
205 environment_entry_t *entry
206 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
207 memset(entry, 0, sizeof(entry[0]));
209 int top = ARR_LEN(environment_stack);
210 ARR_RESIZE(environment_stack, top + 1);
211 environment_stack[top] = entry;
213 assert(declaration->source_position.input_name != NULL);
215 symbol_t *symbol = declaration->symbol;
216 assert(declaration != symbol->declaration);
218 if(symbol->context == context) {
219 if(symbol->declaration != NULL) {
220 assert(symbol->declaration != NULL);
221 parser_print_error_prefix_pos(declaration->source_position);
222 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
224 parser_print_error_prefix_pos(symbol->declaration->source_position);
225 fprintf(stderr, "this is the location of the previous declaration.\n");
229 entry->old_declaration = symbol->declaration;
230 entry->old_context = symbol->context;
231 entry->symbol = symbol;
232 symbol->declaration = declaration;
233 symbol->context = context;
237 * pops symbols from the environment stack until @p new_top is the top element
240 void environment_pop_to(size_t new_top)
242 environment_entry_t *entry = NULL;
243 size_t top = ARR_LEN(environment_stack);
249 assert(new_top < top);
252 entry = environment_stack[i - 1];
254 symbol_t *symbol = entry->symbol;
256 symbol->declaration = entry->old_declaration;
257 symbol->context = entry->old_context;
260 } while(i != new_top);
261 obstack_free(&environment_obstack, entry);
263 ARR_SHRINKLEN(environment_stack, (int) new_top);
268 static expression_t *parse_constant_expression(void)
270 /* TODO: not correct yet */
271 return parse_expression();
274 static expression_t *parse_assignment_expression(void)
276 /* TODO: not correct yet */
277 return parse_expression();
280 static void parse_compound_type_entries(void);
281 static void parse_declarator(declaration_t *declaration,
282 storage_class_t storage_class, type_t *type,
283 int may_be_abstract);
284 static void maybe_push_declaration(declaration_t *declaration);
285 static void record_declaration(declaration_t *declaration);
287 typedef struct declaration_specifiers_t declaration_specifiers_t;
288 struct declaration_specifiers_t {
289 storage_class_t storage_class;
293 static type_t *parse_struct_specifier(void)
297 compound_type_t *struct_type = allocate_type_zero(sizeof(struct_type[0]));
298 struct_type->type.type = TYPE_COMPOUND_STRUCT;
299 struct_type->source_position = token.source_position;
301 int top = environment_top();
302 context_t *last_context = context;
303 set_context(&struct_type->context);
305 if(token.type == T_IDENTIFIER) {
307 if(token.type == '{') {
308 parse_compound_type_entries();
310 } else if(token.type == '{') {
311 parse_compound_type_entries();
313 parse_error_expected("problem while parsing struct type specifiers",
314 T_IDENTIFIER, '{', 0);
318 assert(context == &struct_type->context);
319 set_context(last_context);
320 environment_pop_to(top);
322 return (type_t*) struct_type;
325 static type_t *parse_union_specifier(void)
329 compound_type_t *union_type = allocate_type_zero(sizeof(union_type[0]));
330 union_type->type.type = TYPE_COMPOUND_UNION;
331 union_type->source_position = token.source_position;
333 int top = environment_top();
334 context_t *last_context = context;
335 set_context(&union_type->context);
337 if(token.type == T_IDENTIFIER) {
338 union_type->symbol = token.v.symbol;
340 if(token.type == '{') {
341 parse_compound_type_entries();
343 } else if(token.type == '{') {
344 parse_compound_type_entries();
346 parse_error_expected("problem while parsing union type specifiers",
351 assert(context == &union_type->context);
352 set_context(last_context);
353 environment_pop_to(top);
355 return (type_t*) union_type;
358 static void parse_enum_type_entries(void)
362 if(token.type == '}') {
364 parse_error("empty enum not allowed");
369 if(token.type != T_IDENTIFIER) {
370 parse_error_expected("problem while parsing enum entry",
377 if(token.type == '=') {
378 parse_constant_expression();
381 if(token.type != ',')
384 } while(token.type != '}');
389 static type_t *parse_enum_specifier(void)
393 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
394 enum_type->type.type = TYPE_ENUM;
395 enum_type->source_position = token.source_position;
397 if(token.type == T_IDENTIFIER) {
398 enum_type->symbol = token.v.symbol;
400 if(token.type == '{') {
401 parse_enum_type_entries();
403 } else if(token.type == '{') {
404 parse_enum_type_entries();
406 parse_error_expected("problem while parsing enum type specifiers",
410 return (type_t*) enum_type;
414 SPECIFIER_SIGNED = 1 << 0,
415 SPECIFIER_UNSIGNED = 1 << 1,
416 SPECIFIER_LONG = 1 << 2,
417 SPECIFIER_INT = 1 << 3,
418 SPECIFIER_DOUBLE = 1 << 4,
419 SPECIFIER_CHAR = 1 << 5,
420 SPECIFIER_SHORT = 1 << 6,
421 SPECIFIER_LONG_LONG = 1 << 7,
422 SPECIFIER_FLOAT = 1 << 8,
423 SPECIFIER_BOOL = 1 << 9,
424 SPECIFIER_VOID = 1 << 10,
425 #ifdef PROVIDE_COMPLEX
426 SPECIFIER_COMPLEX = 1 << 11,
428 #ifdef PROVIDE_IMAGINARY
429 SPECIFIER_IMAGINARY = 1 << 12,
433 #define STORAGE_CLASSES \
440 #define TYPE_QUALIFIERS \
445 case T___extension__:
447 #ifdef PROVIDE_COMPLEX
448 #define COMPLEX_SPECIFIERS \
451 #define COMPLEX_SPECIFIERS
454 #ifdef PROVIDE_IMAGINARY
455 #define IMAGINARY_SPECIFIERS \
458 #define IMAGINARY_SPECIFIERS
461 #define TYPE_SPECIFIERS \
478 #define DECLARATION_START \
484 type_t *create_builtin_type(symbol_t *symbol)
486 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
487 type->type.type = TYPE_BUILTIN;
488 type->symbol = symbol;
490 type_t *result = typehash_insert((type_t*) type);
491 if(result != (type_t*) type) {
492 obstack_free(type_obst, type);
499 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
501 declaration_t *declaration;
503 unsigned type_qualifiers = 0;
504 unsigned type_specifiers = 0;
511 #define MATCH_STORAGE_CLASS(token, class) \
513 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
514 parse_error("multiple storage classes in declaration " \
517 specifiers->storage_class = class; \
521 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
522 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
523 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
524 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
525 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
527 /* type qualifiers */
528 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
530 type_qualifiers |= qualifier; \
534 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
535 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
536 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
537 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
539 case T___extension__:
544 /* type specifiers */
545 #define MATCH_SPECIFIER(token, specifier, name) \
548 if(type_specifiers & specifier) { \
549 parse_error("multiple " name " type specifiers given"); \
551 type_specifiers |= specifier; \
555 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
556 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
557 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
558 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
559 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
560 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
561 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
562 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
563 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
564 #ifdef PROVIDE_COMPLEX
565 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
567 #ifdef PROVIDE_IMAGINARY
568 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
572 if(type_specifiers & SPECIFIER_LONG_LONG) {
573 parse_error("multiple type specifiers given");
574 } else if(type_specifiers & SPECIFIER_LONG) {
575 type_specifiers |= SPECIFIER_LONG_LONG;
577 type_specifiers |= SPECIFIER_LONG;
581 /* TODO: if type != NULL for the following rules issue an error */
583 type = parse_struct_specifier();
586 type = parse_union_specifier();
589 type = parse_enum_specifier();
591 case T___builtin_va_list:
592 type = create_builtin_type(token.v.symbol);
597 declaration = token.v.symbol->declaration;
598 if(declaration == NULL ||
599 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
600 goto finish_specifiers;
603 type = declaration->type;
604 assert(type != NULL);
608 /* function specifier */
610 goto finish_specifiers;
617 atomic_type_type_t atomic_type;
619 /* match valid basic types */
620 switch(type_specifiers) {
622 atomic_type = ATOMIC_TYPE_VOID;
625 atomic_type = ATOMIC_TYPE_CHAR;
627 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
628 atomic_type = ATOMIC_TYPE_SCHAR;
630 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
631 atomic_type = ATOMIC_TYPE_UCHAR;
633 case SPECIFIER_SHORT:
634 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
635 case SPECIFIER_SHORT | SPECIFIER_INT:
636 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
637 atomic_type = ATOMIC_TYPE_SHORT;
639 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
640 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
641 atomic_type = ATOMIC_TYPE_USHORT;
644 case SPECIFIER_SIGNED:
645 case SPECIFIER_SIGNED | SPECIFIER_INT:
646 atomic_type = ATOMIC_TYPE_INT;
648 case SPECIFIER_UNSIGNED:
649 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
650 atomic_type = ATOMIC_TYPE_UINT;
653 case SPECIFIER_SIGNED | SPECIFIER_LONG:
654 case SPECIFIER_LONG | SPECIFIER_INT:
655 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
656 atomic_type = ATOMIC_TYPE_LONG;
658 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
659 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
660 atomic_type = ATOMIC_TYPE_ULONG;
662 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
663 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
664 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
665 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
667 atomic_type = ATOMIC_TYPE_LONGLONG;
669 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
670 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
672 atomic_type = ATOMIC_TYPE_ULONGLONG;
674 case SPECIFIER_FLOAT:
675 atomic_type = ATOMIC_TYPE_FLOAT;
677 case SPECIFIER_DOUBLE:
678 atomic_type = ATOMIC_TYPE_DOUBLE;
680 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
681 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
684 atomic_type = ATOMIC_TYPE_BOOL;
686 #ifdef PROVIDE_COMPLEX
687 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
688 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
690 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
691 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
693 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
694 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
697 #ifdef PROVIDE_IMAGINARY
698 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
699 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
701 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
702 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
704 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
705 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
709 /* invalid specifier combination, give an error message */
710 if(type_specifiers == 0) {
711 parse_error("no type specifiers given in declaration");
712 } else if((type_specifiers & SPECIFIER_SIGNED) &&
713 (type_specifiers & SPECIFIER_UNSIGNED)) {
714 parse_error("signed and unsigned specifiers gives");
715 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
716 parse_error("only integer types can be signed or unsigned");
718 parse_error("multiple datatypes in declaration");
720 atomic_type = ATOMIC_TYPE_INVALID;
723 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
724 atype->type.type = TYPE_ATOMIC;
725 atype->atype = atomic_type;
728 type = (type_t*) atype;
730 if(type_specifiers != 0) {
731 parse_error("multiple datatypes in declaration");
735 type->qualifiers = type_qualifiers;
737 type_t *result = typehash_insert(type);
738 if(newtype && result != (type_t*) type) {
739 obstack_free(type_obst, type);
742 specifiers->type = result;
746 unsigned parse_type_qualifiers(void)
748 unsigned type_qualifiers = 0;
752 /* type qualifiers */
753 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
754 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
755 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
756 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
759 return type_qualifiers;
764 typedef struct parsed_pointer_t parsed_pointer_t;
765 struct parsed_pointer_t {
766 unsigned type_qualifiers;
767 parsed_pointer_t *next;
771 parsed_pointer_t *parse_pointers(void)
773 parsed_pointer_t *result = NULL;
774 parsed_pointer_t *last_pointer = NULL;
776 while(token.type == '*') {
778 parsed_pointer_t *pointer
779 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
780 pointer->type_qualifiers = parse_type_qualifiers();
782 if(last_pointer != NULL) {
783 last_pointer->next = pointer;
787 last_pointer = pointer;
794 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
796 for( ; pointer != NULL; pointer = pointer->next) {
797 pointer_type_t *pointer_type
798 = allocate_type_zero(sizeof(pointer_type[0]));
799 pointer_type->type.type = TYPE_POINTER;
800 pointer_type->points_to = type;
801 pointer_type->type.qualifiers = pointer->type_qualifiers;
803 type_t *result = typehash_insert((type_t*) pointer_type);
804 if(result != (type_t*) pointer_type) {
805 obstack_free(type_obst, pointer_type);
815 void parse_identifier_list(void)
818 if(token.type != T_IDENTIFIER) {
819 parse_error_expected("problem while parsing parameter identifier "
820 "list", T_IDENTIFIER, 0);
824 if(token.type != ',')
831 declaration_t *parse_parameter(void)
833 declaration_specifiers_t specifiers;
834 memset(&specifiers, 0, sizeof(specifiers));
836 parse_declaration_specifiers(&specifiers);
838 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
839 parse_declarator(declaration, specifiers.storage_class,
846 void parse_parameters(method_type_t *type)
848 if(token.type == T_IDENTIFIER) {
849 symbol_t *symbol = token.v.symbol;
850 declaration_t *declaration = symbol->declaration;
851 if(declaration == NULL
852 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
853 parse_identifier_list();
858 if(token.type == ')') {
859 type->unspecified_parameters = 1;
862 if(token.type == T_void && la(1)->type == ')') {
867 declaration_t *declaration;
868 method_parameter_type_t *parameter_type;
869 method_parameter_type_t *last_parameter_type = NULL;
880 declaration = parse_parameter();
882 parameter_type = allocate_type_zero(sizeof(parameter_type[0]));
883 parameter_type->type = declaration->type;
884 parameter_type->symbol = declaration->symbol;
886 if(last_parameter_type != NULL) {
887 last_parameter_type->next = parameter_type;
889 type->parameter_types = parameter_type;
891 last_parameter_type = parameter_type;
897 if(token.type != ',')
904 const char *parse_string_literals(void)
906 assert(token.type == T_STRING_LITERAL);
907 const char *result = token.v.string;
911 while(token.type == T_STRING_LITERAL) {
912 result = concat_strings(result, token.v.string);
920 void parse_attributes(void)
924 case T___attribute__:
932 parse_error("EOF while parsing attribute");
950 if(token.type != T_STRING_LITERAL) {
951 parse_error_expected("while parsing assembler attribute",
956 parse_string_literals();
961 goto attributes_finished;
969 typedef struct declarator_part declarator_part;
970 struct declarator_part {
971 parsed_pointer_t *pointers;
972 method_type_t *method_type;
973 declarator_part *inner;
978 declarator_part *parse_inner_declarator(declaration_t *declaration,
981 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
982 memset(part, 0, sizeof(part[0]));
984 part->pointers = parse_pointers();
986 /* TODO: find out if this is correct */
991 if(declaration == NULL) {
992 parse_error("no identifier expected in typename");
994 declaration->symbol = token.v.symbol;
995 declaration->source_position = token.source_position;
1001 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1007 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1012 switch(token.type) {
1016 method_type_t *method_type
1017 = allocate_type_zero(sizeof(method_type[0]));
1018 method_type->type.type = TYPE_METHOD;
1020 parse_parameters(method_type);
1022 part->method_type = method_type;
1029 if(token.type == T_static) {
1033 unsigned type_qualifiers = parse_type_qualifiers();
1034 if(type_qualifiers != 0) {
1035 if(token.type == T_static) {
1042 if(token.type == '*' && la(1)->type == ']') {
1044 } else if(token.type != ']') {
1045 parse_assignment_expression();
1051 goto declarator_finished;
1055 declarator_finished:
1062 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1065 type = make_pointers(type, part->pointers);
1067 method_type_t *method_type = part->method_type;
1068 if(method_type != NULL) {
1069 method_type->result_type = type;
1071 type = (type_t*) method_type;
1075 } while(part != NULL);
1081 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1082 type_t *type, int may_be_abstract)
1084 declarator_part *part
1085 = parse_inner_declarator(declaration, may_be_abstract);
1088 declaration->type = construct_declarator_type(part, type);
1089 declaration->storage_class = storage_class;
1090 obstack_free(&temp_obst, part);
1095 type_t *parse_abstract_declarator(type_t *base_type)
1097 declarator_part *part = parse_inner_declarator(NULL, 1);
1099 type_t *result = construct_declarator_type(part, base_type);
1100 obstack_free(&temp_obst, part);
1105 static void record_declaration(declaration_t *declaration)
1107 if(last_declaration != NULL) {
1108 last_declaration->next = declaration;
1111 context->declarations = declaration;
1113 last_declaration = declaration;
1117 void maybe_push_declaration(declaration_t *declaration)
1119 symbol_t *symbol = declaration->symbol;
1121 if(symbol != NULL) {
1122 environment_push(declaration, context);
1127 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1130 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1132 parse_declarator(declaration, specifiers->storage_class,
1133 specifiers->type, 0);
1134 maybe_push_declaration(declaration);
1135 record_declaration(declaration);
1136 if(token.type == '=') {
1138 if(token.type == '{') {
1142 parse_assignment_expression();
1144 } else if(token.type == '{') {
1145 statement_t *statement = parse_compound_statement();
1146 declaration->statement = statement;
1150 if(token.type != ',')
1158 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1161 if(token.type == ':') {
1163 parse_constant_expression();
1164 /* TODO (bitfields) */
1166 declaration_t *declaration
1167 = allocate_ast_zero(sizeof(declaration[0]));
1168 parse_declarator(declaration, specifiers->storage_class,
1169 specifiers->type, 0);
1170 maybe_push_declaration(declaration);
1171 record_declaration(declaration);
1173 if(token.type == ':') {
1175 parse_constant_expression();
1176 /* TODO (bitfields) */
1180 if(token.type != ',')
1187 static void parse_compound_type_entries(void)
1191 while(token.type != '}' && token.type != T_EOF) {
1192 declaration_specifiers_t specifiers;
1193 memset(&specifiers, 0, sizeof(specifiers));
1194 /* TODO not correct as this allows storage class stuff... but only
1195 * specifiers and qualifiers sould be allowed here */
1196 parse_declaration_specifiers(&specifiers);
1198 parse_struct_declarators(&specifiers);
1200 if(token.type == T_EOF) {
1201 parse_error("unexpected error while parsing struct");
1206 void parse_declaration(void)
1208 declaration_specifiers_t specifiers;
1209 memset(&specifiers, 0, sizeof(specifiers));
1210 parse_declaration_specifiers(&specifiers);
1212 if(token.type == ';') {
1216 parse_init_declarators(&specifiers);
1219 type_t *parse_typename(void)
1221 declaration_specifiers_t specifiers;
1222 memset(&specifiers, 0, sizeof(specifiers));
1223 parse_declaration_specifiers(&specifiers);
1224 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1225 /* TODO: improve error message, user does probably not know what a
1226 * storage class is...
1228 parse_error("typename may not have a storage class");
1231 type_t *result = parse_abstract_declarator(specifiers.type);
1239 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1240 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1241 expression_t *left);
1243 typedef struct expression_parser_function_t expression_parser_function_t;
1244 struct expression_parser_function_t {
1245 unsigned precedence;
1246 parse_expression_function parser;
1247 unsigned infix_precedence;
1248 parse_expression_infix_function infix_parser;
1251 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1254 expression_t *expected_expression_error(void)
1256 parser_print_error_prefix();
1257 fprintf(stderr, "expected expression, got token ");
1258 print_token(stderr, & token);
1259 fprintf(stderr, "\n");
1261 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1262 expression->type = EXPR_INVALID;
1269 expression_t *parse_string_const(void)
1271 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1273 cnst->expression.type = EXPR_STRING_LITERAL;
1274 cnst->value = parse_string_literals();
1276 return (expression_t*) cnst;
1280 expression_t *parse_int_const(void)
1282 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1284 cnst->expression.type = EXPR_CONST;
1285 cnst->value = token.v.intvalue;
1289 return (expression_t*) cnst;
1293 expression_t *parse_reference(void)
1295 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1297 ref->expression.type = EXPR_REFERENCE;
1298 ref->symbol = token.v.symbol;
1302 return (expression_t*) ref;
1306 expression_t *parse_brace_expression(void)
1310 expression_t *result = parse_expression();
1317 expression_t *parse_primary_expression(void)
1319 switch(token.type) {
1321 return parse_int_const();
1322 case T_STRING_LITERAL:
1323 return parse_string_const();
1325 return parse_reference();
1327 return parse_brace_expression();
1330 /* TODO: error message */
1335 expression_t *parse_array_expression(unsigned precedence,
1336 expression_t *array_ref)
1342 array_access_expression_t *array_access
1343 = allocate_ast_zero(sizeof(array_access[0]));
1345 array_access->expression.type = EXPR_ARRAY_ACCESS;
1346 array_access->array_ref = array_ref;
1347 array_access->index = parse_expression();
1349 if(token.type != ']') {
1350 parse_error_expected("Problem while parsing array access", ']', 0);
1355 return (expression_t*) array_access;
1359 type_t *get_expression_type(const expression_t *expression)
1367 expression_t *parse_sizeof(unsigned precedence)
1371 sizeof_expression_t *sizeof_expression
1372 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1373 sizeof_expression->expression.type = EXPR_SIZEOF;
1375 if(token.type == '(' /* && LA1 is type_specifier */) {
1377 sizeof_expression->type = parse_typename();
1380 expression_t *expression = parse_sub_expression(precedence);
1381 sizeof_expression->type = get_expression_type(expression);
1384 return (expression_t*) sizeof_expression;
1388 expression_t *parse_select_expression(unsigned precedence,
1389 expression_t *compound)
1393 assert(token.type == '.' || token.type == T_SELECT);
1396 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1398 select->expression.type = EXPR_SELECT;
1399 select->compound = compound;
1401 if(token.type != T_IDENTIFIER) {
1402 parse_error_expected("Problem while parsing compound select",
1406 select->symbol = token.v.symbol;
1409 return (expression_t*) select;
1413 expression_t *parse_call_expression(unsigned precedence,
1414 expression_t *expression)
1417 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1419 call->expression.type = EXPR_CALL;
1420 call->method = expression;
1422 /* parse arguments */
1425 if(token.type != ')') {
1426 call_argument_t *last_argument = NULL;
1429 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1431 argument->expression = parse_expression();
1432 if(last_argument == NULL) {
1433 call->arguments = argument;
1435 last_argument->next = argument;
1437 last_argument = argument;
1439 if(token.type != ',')
1446 return (expression_t*) call;
1450 expression_t *parse_conditional_expression(unsigned precedence,
1451 expression_t *expression)
1455 conditional_expression_t *conditional
1456 = allocate_ast_zero(sizeof(conditional[0]));
1457 conditional->condition = expression;
1459 conditional->true_expression = parse_expression();
1461 conditional->false_expression = parse_sub_expression(precedence);
1463 return (expression_t*) conditional;
1466 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1468 expression_t *parse_##unexpression_type(unsigned precedence) \
1472 unary_expression_t *unary_expression \
1473 = allocate_ast_zero(sizeof(unary_expression[0])); \
1474 unary_expression->expression.type = EXPR_UNARY; \
1475 unary_expression->type = unexpression_type; \
1476 unary_expression->value = parse_sub_expression(precedence); \
1478 return (expression_t*) unary_expression; \
1481 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1482 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1483 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1484 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1485 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1486 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1487 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1488 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1490 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1492 expression_t *parse_##unexpression_type(unsigned precedence, \
1493 expression_t *left) \
1495 (void) precedence; \
1498 unary_expression_t *unary_expression \
1499 = allocate_ast_zero(sizeof(unary_expression[0])); \
1500 unary_expression->expression.type = EXPR_UNARY; \
1501 unary_expression->type = unexpression_type; \
1502 unary_expression->value = left; \
1504 return (expression_t*) unary_expression; \
1507 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1508 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1510 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1512 expression_t *parse_##binexpression_type(unsigned precedence, \
1513 expression_t *left) \
1517 expression_t *right = parse_sub_expression(precedence); \
1519 binary_expression_t *binexpr \
1520 = allocate_ast_zero(sizeof(binexpr[0])); \
1521 binexpr->expression.type = EXPR_BINARY; \
1522 binexpr->type = binexpression_type; \
1523 binexpr->left = left; \
1524 binexpr->right = right; \
1526 return (expression_t*) binexpr; \
1529 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1530 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1531 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1532 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1533 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1534 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1535 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1536 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1537 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1538 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1539 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1540 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1541 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1542 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1543 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1544 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1545 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1546 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1549 expression_t *parse_sub_expression(unsigned precedence)
1551 if(token.type < 0) {
1552 return expected_expression_error();
1555 expression_parser_function_t *parser
1556 = &expression_parsers[token.type];
1557 source_position_t source_position = token.source_position;
1560 if(parser->parser != NULL) {
1561 left = parser->parser(parser->precedence);
1563 left = parse_primary_expression();
1566 left->source_position = source_position;
1569 if(token.type < 0) {
1570 return expected_expression_error();
1573 parser = &expression_parsers[token.type];
1574 if(parser->infix_parser == NULL)
1576 if(parser->infix_precedence < precedence)
1579 left = parser->infix_parser(parser->infix_precedence, left);
1581 left->source_position = source_position;
1588 expression_t *parse_expression(void)
1590 return parse_sub_expression(1);
1595 void register_expression_parser(parse_expression_function parser,
1596 int token_type, unsigned precedence)
1598 expression_parser_function_t *entry = &expression_parsers[token_type];
1600 if(entry->parser != NULL) {
1601 fprintf(stderr, "for token ");
1602 print_token_type(stderr, token_type);
1603 fprintf(stderr, "\n");
1604 panic("trying to register multiple expression parsers for a token");
1606 entry->parser = parser;
1607 entry->precedence = precedence;
1610 void register_expression_infix_parser(parse_expression_infix_function parser,
1611 int token_type, unsigned precedence)
1613 expression_parser_function_t *entry = &expression_parsers[token_type];
1615 if(entry->infix_parser != NULL) {
1616 fprintf(stderr, "for token ");
1617 print_token_type(stderr, token_type);
1618 fprintf(stderr, "\n");
1619 panic("trying to register multiple infix expression parsers for a "
1622 entry->infix_parser = parser;
1623 entry->infix_precedence = precedence;
1627 void init_expression_parsers(void)
1629 memset(&expression_parsers, 0, sizeof(expression_parsers));
1631 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1632 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1633 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1635 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1636 T_GREATERGREATER, 16);
1637 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1638 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1639 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1640 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1641 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1642 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1643 T_GREATEREQUAL, 14);
1644 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1645 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1646 T_EXCLAMATIONMARKEQUAL, 13);
1647 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1648 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1649 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1650 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1651 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1652 register_expression_infix_parser(parse_conditional_expression, '?', 7);
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)
1800 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1801 statement->source_position = token.source_position;
1802 statement->type = STATEMENT_CONTINUE;
1808 statement_t *parse_break(void)
1817 statement_t *parse_return(void)
1821 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1822 statement->statement.type = STATEMENT_RETURN;
1823 if(token.type != ';') {
1824 statement->return_value = parse_expression();
1828 return (statement_t*) statement;
1832 statement_t *parse_declaration_statement(void)
1834 parse_declaration();
1839 statement_t *parse_expression_statement(void)
1846 statement_t *parse_statement(void)
1848 declaration_t *declaration;
1849 statement_t *statement = NULL;
1851 /* declaration or statement */
1852 switch(token.type) {
1854 statement = parse_case_statement();
1858 statement = parse_default_statement();
1862 statement = parse_compound_statement();
1866 statement = parse_if();
1870 statement = parse_switch();
1874 statement = parse_while();
1878 statement = parse_do();
1882 statement = parse_for();
1886 statement = parse_goto();
1890 statement = parse_continue();
1894 statement = parse_break();
1898 statement = parse_return();
1906 if(la(1)->type == ':') {
1907 statement = parse_label_statement();
1911 declaration = token.v.symbol->declaration;
1912 if(declaration != NULL &&
1913 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1914 statement = parse_declaration_statement();
1918 statement = parse_expression_statement();
1922 statement = parse_declaration_statement();
1930 statement_t *parse_compound_statement(void)
1934 compound_statement_t *compound_statement
1935 = allocate_ast_zero(sizeof(compound_statement[0]));
1936 compound_statement->statement.type = STATEMENT_COMPOUND;
1938 int top = environment_top();
1939 context_t *last_context = context;
1940 set_context(&compound_statement->context);
1942 statement_t *last_statement = NULL;
1944 while(token.type != '}' && token.type != T_EOF) {
1945 statement_t *statement = parse_statement();
1947 if(last_statement != NULL) {
1948 last_statement->next = statement;
1950 compound_statement->statements = statement;
1952 last_statement = statement;
1955 assert(context == &compound_statement->context);
1956 set_context(last_context);
1957 environment_pop_to(top);
1961 return (statement_t*) compound_statement;
1965 translation_unit_t *parse_translation_unit(void)
1967 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
1969 assert(context == NULL);
1970 set_context(&unit->context);
1972 while(token.type != T_EOF) {
1973 parse_declaration();
1976 assert(context == &unit->context);
1978 last_declaration = NULL;
1983 translation_unit_t *parse(void)
1985 obstack_init(&environment_obstack);
1986 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
1988 lookahead_bufpos = 0;
1989 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
1992 translation_unit_t *unit = parse_translation_unit();
1994 DEL_ARR_F(environment_stack);
1995 obstack_free(&environment_obstack, NULL);
2000 void init_parser(void)
2002 init_expression_parsers();
2003 obstack_init(&temp_obst);
2006 void exit_parser(void)
2008 obstack_free(&temp_obst, NULL);