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;
200 * called when we find a 2nd declarator for an identifier we already have a
203 static void multiple_occurence(declaration_t *declaration,
204 declaration_t *previous)
206 if(declaration->type != previous->type) {
213 * pushs an environment_entry on the environment stack and links the
214 * corresponding symbol to the new entry
217 void environment_push(declaration_t *declaration, const void *context)
219 environment_entry_t *entry
220 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
221 memset(entry, 0, sizeof(entry[0]));
223 int top = ARR_LEN(environment_stack);
224 ARR_RESIZE(environment_stack, top + 1);
225 environment_stack[top] = entry;
227 assert(declaration->source_position.input_name != NULL);
229 symbol_t *symbol = declaration->symbol;
230 assert(declaration != symbol->declaration);
232 if(symbol->context == context) {
233 if(symbol->declaration != NULL) {
234 assert(symbol->declaration != NULL);
235 parser_print_error_prefix_pos(declaration->source_position);
236 fprintf(stderr, "multiple definitions for symbol '%s'.\n",
238 parser_print_error_prefix_pos(symbol->declaration->source_position);
239 fprintf(stderr, "this is the location of the previous declaration.\n");
243 entry->old_declaration = symbol->declaration;
244 entry->old_context = symbol->context;
245 entry->symbol = symbol;
246 symbol->declaration = declaration;
247 symbol->context = context;
251 * pops symbols from the environment stack until @p new_top is the top element
254 void environment_pop_to(size_t new_top)
256 environment_entry_t *entry = NULL;
257 size_t top = ARR_LEN(environment_stack);
263 assert(new_top < top);
266 entry = environment_stack[i - 1];
268 symbol_t *symbol = entry->symbol;
270 symbol->declaration = entry->old_declaration;
271 symbol->context = entry->old_context;
274 } while(i != new_top);
275 obstack_free(&environment_obstack, entry);
277 ARR_SHRINKLEN(environment_stack, (int) new_top);
282 static expression_t *parse_constant_expression(void)
284 /* TODO: not correct yet */
285 return parse_expression();
288 static expression_t *parse_assignment_expression(void)
290 /* TODO: not correct yet */
291 return parse_expression();
294 static void parse_compound_type_entries(void);
295 static void parse_declarator(declaration_t *declaration,
296 storage_class_t storage_class, type_t *type,
297 int may_be_abstract);
298 static void maybe_push_declaration(declaration_t *declaration);
299 static void record_declaration(declaration_t *declaration);
301 typedef struct declaration_specifiers_t declaration_specifiers_t;
302 struct declaration_specifiers_t {
303 storage_class_t storage_class;
307 static compound_type_t *find_compound_type(compound_type_t *types,
308 const symbol_t *symbol)
310 compound_type_t *type = types;
311 for( ; type != NULL; type = type->next) {
312 if(type->symbol == symbol)
319 static type_t *parse_compound_type_specifier(int is_struct)
327 symbol_t *symbol = NULL;
328 compound_type_t *compound_type = NULL;
330 if(token.type == T_IDENTIFIER) {
331 symbol = token.v.symbol;
334 if(context != NULL) {
336 compound_type = find_compound_type(context->structs, symbol);
338 compound_type = find_compound_type(context->unions, symbol);
341 } else if(token.type != '{') {
343 parse_error_expected("problem while parsing struct type specifiers",
344 T_IDENTIFIER, '{', 0);
346 parse_error_expected("problem while parsing union type specifiers",
347 T_IDENTIFIER, '{', 0);
353 if(compound_type == NULL) {
354 compound_type = allocate_type_zero(sizeof(compound_type[0]));
357 compound_type->type.type = TYPE_COMPOUND_STRUCT;
359 compound_type->type.type = TYPE_COMPOUND_UNION;
361 compound_type->source_position = token.source_position;
362 compound_type->symbol = symbol;
365 if(token.type == '{') {
366 if(compound_type->defined) {
367 parser_print_error_prefix();
368 fprintf(stderr, "multiple definition of %s %s\n",
369 is_struct ? "struct" : "union", symbol->string);
370 compound_type->context.declarations = NULL;
372 compound_type->defined = 1;
374 int top = environment_top();
375 context_t *last_context = context;
376 set_context(&compound_type->context);
378 parse_compound_type_entries();
380 assert(context == &compound_type->context);
381 set_context(last_context);
382 environment_pop_to(top);
385 return (type_t*) compound_type;
388 static enum_entry_t *parse_enum_type_entries(void)
392 if(token.type == '}') {
394 parse_error("empty enum not allowed");
398 enum_entry_t *result = NULL;
399 enum_entry_t *last_entry = NULL;
401 enum_entry_t *entry = allocate_ast_zero(sizeof(entry[0]));
402 if(token.type != T_IDENTIFIER) {
403 parse_error_expected("problem while parsing enum entry",
408 entry->symbol = token.v.symbol;
411 if(token.type == '=') {
413 entry->value = parse_constant_expression();
416 if(last_entry != NULL) {
417 last_entry->next = entry;
423 if(token.type != ',')
426 } while(token.type != '}');
432 static type_t *parse_enum_specifier(void)
436 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
437 enum_type->type.type = TYPE_ENUM;
438 enum_type->source_position = token.source_position;
440 if(token.type == T_IDENTIFIER) {
441 enum_type->symbol = token.v.symbol;
443 if(token.type == '{') {
444 enum_type->entries = parse_enum_type_entries();
446 } else if(token.type == '{') {
447 enum_type->entries = parse_enum_type_entries();
449 parse_error_expected("problem while parsing enum type specifiers",
453 return (type_t*) enum_type;
457 const char *parse_string_literals(void)
459 assert(token.type == T_STRING_LITERAL);
460 const char *result = token.v.string;
464 while(token.type == T_STRING_LITERAL) {
465 result = concat_strings(result, token.v.string);
473 void parse_attributes(void)
477 case T___attribute__:
485 parse_error("EOF while parsing attribute");
503 if(token.type != T_STRING_LITERAL) {
504 parse_error_expected("while parsing assembler attribute",
509 parse_string_literals();
514 goto attributes_finished;
523 SPECIFIER_SIGNED = 1 << 0,
524 SPECIFIER_UNSIGNED = 1 << 1,
525 SPECIFIER_LONG = 1 << 2,
526 SPECIFIER_INT = 1 << 3,
527 SPECIFIER_DOUBLE = 1 << 4,
528 SPECIFIER_CHAR = 1 << 5,
529 SPECIFIER_SHORT = 1 << 6,
530 SPECIFIER_LONG_LONG = 1 << 7,
531 SPECIFIER_FLOAT = 1 << 8,
532 SPECIFIER_BOOL = 1 << 9,
533 SPECIFIER_VOID = 1 << 10,
534 #ifdef PROVIDE_COMPLEX
535 SPECIFIER_COMPLEX = 1 << 11,
537 #ifdef PROVIDE_IMAGINARY
538 SPECIFIER_IMAGINARY = 1 << 12,
542 #define STORAGE_CLASSES \
549 #define TYPE_QUALIFIERS \
554 case T___extension__:
556 #ifdef PROVIDE_COMPLEX
557 #define COMPLEX_SPECIFIERS \
560 #define COMPLEX_SPECIFIERS
563 #ifdef PROVIDE_IMAGINARY
564 #define IMAGINARY_SPECIFIERS \
567 #define IMAGINARY_SPECIFIERS
570 #define TYPE_SPECIFIERS \
587 #define DECLARATION_START \
593 type_t *create_builtin_type(symbol_t *symbol)
595 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
596 type->type.type = TYPE_BUILTIN;
597 type->symbol = symbol;
599 type_t *result = typehash_insert((type_t*) type);
600 if(result != (type_t*) type) {
601 obstack_free(type_obst, type);
608 void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
610 declaration_t *declaration;
612 unsigned type_qualifiers = 0;
613 unsigned type_specifiers = 0;
620 #define MATCH_STORAGE_CLASS(token, class) \
622 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
623 parse_error("multiple storage classes in declaration " \
626 specifiers->storage_class = class; \
630 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
631 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
632 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
633 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
634 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
636 /* type qualifiers */
637 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
639 type_qualifiers |= qualifier; \
643 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
644 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
645 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
646 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
648 case T___extension__:
653 /* type specifiers */
654 #define MATCH_SPECIFIER(token, specifier, name) \
657 if(type_specifiers & specifier) { \
658 parse_error("multiple " name " type specifiers given"); \
660 type_specifiers |= specifier; \
664 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
665 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
666 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
667 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
668 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
669 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
670 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
671 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
672 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
673 #ifdef PROVIDE_COMPLEX
674 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
676 #ifdef PROVIDE_IMAGINARY
677 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
681 if(type_specifiers & SPECIFIER_LONG_LONG) {
682 parse_error("multiple type specifiers given");
683 } else if(type_specifiers & SPECIFIER_LONG) {
684 type_specifiers |= SPECIFIER_LONG_LONG;
686 type_specifiers |= SPECIFIER_LONG;
690 /* TODO: if type != NULL for the following rules issue an error */
692 type = parse_compound_type_specifier(1);
695 type = parse_compound_type_specifier(0);
698 type = parse_enum_specifier();
700 case T___builtin_va_list:
701 type = create_builtin_type(token.v.symbol);
705 case T___attribute__:
711 declaration = token.v.symbol->declaration;
712 if(declaration == NULL ||
713 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
714 goto finish_specifiers;
717 type = declaration->type;
718 assert(type != NULL);
722 /* function specifier */
724 goto finish_specifiers;
731 atomic_type_type_t atomic_type;
733 /* match valid basic types */
734 switch(type_specifiers) {
736 atomic_type = ATOMIC_TYPE_VOID;
739 atomic_type = ATOMIC_TYPE_CHAR;
741 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
742 atomic_type = ATOMIC_TYPE_SCHAR;
744 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
745 atomic_type = ATOMIC_TYPE_UCHAR;
747 case SPECIFIER_SHORT:
748 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
749 case SPECIFIER_SHORT | SPECIFIER_INT:
750 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
751 atomic_type = ATOMIC_TYPE_SHORT;
753 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
754 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
755 atomic_type = ATOMIC_TYPE_USHORT;
758 case SPECIFIER_SIGNED:
759 case SPECIFIER_SIGNED | SPECIFIER_INT:
760 atomic_type = ATOMIC_TYPE_INT;
762 case SPECIFIER_UNSIGNED:
763 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
764 atomic_type = ATOMIC_TYPE_UINT;
767 case SPECIFIER_SIGNED | SPECIFIER_LONG:
768 case SPECIFIER_LONG | SPECIFIER_INT:
769 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
770 atomic_type = ATOMIC_TYPE_LONG;
772 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
773 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
774 atomic_type = ATOMIC_TYPE_ULONG;
776 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
777 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
778 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
779 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
781 atomic_type = ATOMIC_TYPE_LONGLONG;
783 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
784 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
786 atomic_type = ATOMIC_TYPE_ULONGLONG;
788 case SPECIFIER_FLOAT:
789 atomic_type = ATOMIC_TYPE_FLOAT;
791 case SPECIFIER_DOUBLE:
792 atomic_type = ATOMIC_TYPE_DOUBLE;
794 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
795 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
798 atomic_type = ATOMIC_TYPE_BOOL;
800 #ifdef PROVIDE_COMPLEX
801 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
802 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
804 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
805 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
807 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
808 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
811 #ifdef PROVIDE_IMAGINARY
812 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
813 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
815 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
816 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
818 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
819 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
823 /* invalid specifier combination, give an error message */
824 if(type_specifiers == 0) {
825 parse_error("no type specifiers given in declaration");
826 } else if((type_specifiers & SPECIFIER_SIGNED) &&
827 (type_specifiers & SPECIFIER_UNSIGNED)) {
828 parse_error("signed and unsigned specifiers gives");
829 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
830 parse_error("only integer types can be signed or unsigned");
832 parse_error("multiple datatypes in declaration");
834 atomic_type = ATOMIC_TYPE_INVALID;
837 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
838 atype->type.type = TYPE_ATOMIC;
839 atype->atype = atomic_type;
842 type = (type_t*) atype;
844 if(type_specifiers != 0) {
845 parse_error("multiple datatypes in declaration");
849 type->qualifiers = type_qualifiers;
851 type_t *result = typehash_insert(type);
852 if(newtype && result != (type_t*) type) {
853 obstack_free(type_obst, type);
856 specifiers->type = result;
860 unsigned parse_type_qualifiers(void)
862 unsigned type_qualifiers = 0;
866 /* type qualifiers */
867 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
868 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
869 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
870 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
873 return type_qualifiers;
878 typedef struct parsed_pointer_t parsed_pointer_t;
879 struct parsed_pointer_t {
880 unsigned type_qualifiers;
881 parsed_pointer_t *next;
885 parsed_pointer_t *parse_pointers(void)
887 parsed_pointer_t *result = NULL;
888 parsed_pointer_t *last_pointer = NULL;
890 while(token.type == '*') {
892 parsed_pointer_t *pointer
893 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
894 pointer->type_qualifiers = parse_type_qualifiers();
896 if(last_pointer != NULL) {
897 last_pointer->next = pointer;
901 last_pointer = pointer;
908 type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
910 for( ; pointer != NULL; pointer = pointer->next) {
911 pointer_type_t *pointer_type
912 = allocate_type_zero(sizeof(pointer_type[0]));
913 pointer_type->type.type = TYPE_POINTER;
914 pointer_type->points_to = type;
915 pointer_type->type.qualifiers = pointer->type_qualifiers;
917 type_t *result = typehash_insert((type_t*) pointer_type);
918 if(result != (type_t*) pointer_type) {
919 obstack_free(type_obst, pointer_type);
929 void parse_identifier_list(void)
932 if(token.type != T_IDENTIFIER) {
933 parse_error_expected("problem while parsing parameter identifier "
934 "list", T_IDENTIFIER, 0);
938 if(token.type != ',')
945 declaration_t *parse_parameter(void)
947 declaration_specifiers_t specifiers;
948 memset(&specifiers, 0, sizeof(specifiers));
950 parse_declaration_specifiers(&specifiers);
952 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
953 parse_declarator(declaration, specifiers.storage_class,
960 declaration_t *parse_parameters(method_type_t *type)
962 if(token.type == T_IDENTIFIER) {
963 symbol_t *symbol = token.v.symbol;
964 declaration_t *declaration = symbol->declaration;
965 if(declaration == NULL
966 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
968 parse_identifier_list();
973 if(token.type == ')') {
974 type->unspecified_parameters = 1;
977 if(token.type == T_void && la(1)->type == ')') {
982 declaration_t *declarations = NULL;
983 declaration_t *declaration;
984 declaration_t *last_declaration = NULL;
985 method_parameter_t *parameter;
986 method_parameter_t *last_parameter = NULL;
997 declaration = parse_parameter();
999 parameter = allocate_type_zero(sizeof(parameter[0]));
1000 parameter->type = declaration->type;
1002 if(last_parameter != NULL) {
1003 last_declaration->next = declaration;
1004 last_parameter->next = parameter;
1006 type->parameters = parameter;
1007 declarations = declaration;
1009 last_parameter = parameter;
1010 last_declaration = declaration;
1014 return declarations;
1016 if(token.type != ',')
1017 return declarations;
1022 typedef struct declarator_part declarator_part;
1023 struct declarator_part {
1024 parsed_pointer_t *pointers;
1025 method_type_t *method_type;
1026 declarator_part *inner;
1031 declarator_part *parse_inner_declarator(declaration_t *declaration,
1032 int may_be_abstract)
1034 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1035 memset(part, 0, sizeof(part[0]));
1037 part->pointers = parse_pointers();
1039 /* TODO: find out if this is correct */
1042 switch(token.type) {
1044 if(declaration == NULL) {
1045 parse_error("no identifier expected in typename");
1047 declaration->symbol = token.v.symbol;
1048 declaration->source_position = token.source_position;
1054 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1060 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1065 switch(token.type) {
1069 method_type_t *method_type
1070 = allocate_type_zero(sizeof(method_type[0]));
1071 method_type->type.type = TYPE_METHOD;
1073 declaration->context.declarations = parse_parameters(method_type);
1075 part->method_type = method_type;
1082 if(token.type == T_static) {
1086 unsigned type_qualifiers = parse_type_qualifiers();
1087 if(type_qualifiers != 0) {
1088 if(token.type == T_static) {
1095 if(token.type == '*' && la(1)->type == ']') {
1097 } else if(token.type != ']') {
1098 parse_assignment_expression();
1104 goto declarator_finished;
1108 declarator_finished:
1115 type_t *construct_declarator_type(declarator_part *part, type_t *type)
1118 type = make_pointers(type, part->pointers);
1120 method_type_t *method_type = part->method_type;
1121 if(method_type != NULL) {
1122 method_type->result_type = type;
1124 type = (type_t*) method_type;
1128 } while(part != NULL);
1134 void parse_declarator(declaration_t *declaration, storage_class_t storage_class,
1135 type_t *type, int may_be_abstract)
1137 declarator_part *part
1138 = parse_inner_declarator(declaration, may_be_abstract);
1141 declaration->type = construct_declarator_type(part, type);
1142 declaration->storage_class = storage_class;
1143 obstack_free(&temp_obst, part);
1148 type_t *parse_abstract_declarator(type_t *base_type)
1150 declarator_part *part = parse_inner_declarator(NULL, 1);
1152 type_t *result = construct_declarator_type(part, base_type);
1153 obstack_free(&temp_obst, part);
1158 static void record_declaration(declaration_t *declaration)
1160 if(last_declaration != NULL) {
1161 last_declaration->next = declaration;
1164 context->declarations = declaration;
1166 last_declaration = declaration;
1170 void maybe_push_declaration(declaration_t *declaration)
1172 symbol_t *symbol = declaration->symbol;
1174 if(symbol != NULL) {
1175 environment_push(declaration, context);
1180 void parse_init_declarators(const declaration_specifiers_t *specifiers)
1183 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1185 parse_declarator(declaration, specifiers->storage_class,
1186 specifiers->type, 0);
1187 maybe_push_declaration(declaration);
1188 record_declaration(declaration);
1189 if(token.type == '=') {
1191 if(token.type == '{') {
1195 declaration->initializer = parse_assignment_expression();
1197 } else if(token.type == '{') {
1198 statement_t *statement = parse_compound_statement();
1199 declaration->statement = statement;
1203 if(token.type != ',')
1211 void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1214 if(token.type == ':') {
1216 parse_constant_expression();
1217 /* TODO (bitfields) */
1219 declaration_t *declaration
1220 = allocate_ast_zero(sizeof(declaration[0]));
1221 parse_declarator(declaration, specifiers->storage_class,
1222 specifiers->type, 0);
1223 maybe_push_declaration(declaration);
1224 record_declaration(declaration);
1226 if(token.type == ':') {
1228 parse_constant_expression();
1229 /* TODO (bitfields) */
1233 if(token.type != ',')
1240 static void parse_compound_type_entries(void)
1244 while(token.type != '}' && token.type != T_EOF) {
1245 declaration_specifiers_t specifiers;
1246 memset(&specifiers, 0, sizeof(specifiers));
1247 /* TODO not correct as this allows storage class stuff... but only
1248 * specifiers and qualifiers sould be allowed here */
1249 parse_declaration_specifiers(&specifiers);
1251 parse_struct_declarators(&specifiers);
1253 if(token.type == T_EOF) {
1254 parse_error("unexpected error while parsing struct");
1259 void parse_declaration(void)
1261 declaration_specifiers_t specifiers;
1262 memset(&specifiers, 0, sizeof(specifiers));
1263 parse_declaration_specifiers(&specifiers);
1265 if(token.type == ';') {
1269 parse_init_declarators(&specifiers);
1272 type_t *parse_typename(void)
1274 declaration_specifiers_t specifiers;
1275 memset(&specifiers, 0, sizeof(specifiers));
1276 parse_declaration_specifiers(&specifiers);
1277 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1278 /* TODO: improve error message, user does probably not know what a
1279 * storage class is...
1281 parse_error("typename may not have a storage class");
1284 type_t *result = parse_abstract_declarator(specifiers.type);
1292 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1293 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1294 expression_t *left);
1296 typedef struct expression_parser_function_t expression_parser_function_t;
1297 struct expression_parser_function_t {
1298 unsigned precedence;
1299 parse_expression_function parser;
1300 unsigned infix_precedence;
1301 parse_expression_infix_function infix_parser;
1304 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1307 expression_t *expected_expression_error(void)
1309 parser_print_error_prefix();
1310 fprintf(stderr, "expected expression, got token ");
1311 print_token(stderr, & token);
1312 fprintf(stderr, "\n");
1314 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1315 expression->type = EXPR_INVALID;
1322 expression_t *parse_string_const(void)
1324 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1326 cnst->expression.type = EXPR_STRING_LITERAL;
1327 cnst->value = parse_string_literals();
1329 return (expression_t*) cnst;
1333 expression_t *parse_int_const(void)
1335 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1337 cnst->expression.type = EXPR_CONST;
1338 cnst->value = token.v.intvalue;
1342 return (expression_t*) cnst;
1346 expression_t *parse_reference(void)
1348 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1350 ref->expression.type = EXPR_REFERENCE;
1351 ref->symbol = token.v.symbol;
1355 return (expression_t*) ref;
1359 expression_t *parse_cast(void)
1361 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1363 cast->expression.type = EXPR_UNARY;
1364 cast->type = UNEXPR_CAST;
1365 cast->expression.source_position = token.source_position;
1367 type_t *type = parse_typename();
1370 expression_t *value = parse_sub_expression(20);
1372 cast->expression.datatype = type;
1373 cast->value = value;
1375 return (expression_t*) cast;
1379 expression_t *parse_brace_expression(void)
1383 declaration_t *declaration;
1384 switch(token.type) {
1387 return parse_cast();
1389 declaration = token.v.symbol->declaration;
1390 if(declaration != NULL &&
1391 (declaration->storage_class & STORAGE_CLASS_TYPEDEF)) {
1392 return parse_cast();
1396 expression_t *result = parse_expression();
1403 expression_t *parse_primary_expression(void)
1405 switch(token.type) {
1407 return parse_int_const();
1408 case T_STRING_LITERAL:
1409 return parse_string_const();
1411 return parse_reference();
1413 return parse_brace_expression();
1416 /* TODO: error message */
1421 expression_t *parse_array_expression(unsigned precedence,
1422 expression_t *array_ref)
1428 array_access_expression_t *array_access
1429 = allocate_ast_zero(sizeof(array_access[0]));
1431 array_access->expression.type = EXPR_ARRAY_ACCESS;
1432 array_access->array_ref = array_ref;
1433 array_access->index = parse_expression();
1435 if(token.type != ']') {
1436 parse_error_expected("Problem while parsing array access", ']', 0);
1441 return (expression_t*) array_access;
1445 type_t *get_expression_type(const expression_t *expression)
1453 expression_t *parse_sizeof(unsigned precedence)
1457 sizeof_expression_t *sizeof_expression
1458 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1459 sizeof_expression->expression.type = EXPR_SIZEOF;
1461 if(token.type == '(' /* && LA1 is type_specifier */) {
1463 sizeof_expression->type = parse_typename();
1466 expression_t *expression = parse_sub_expression(precedence);
1467 sizeof_expression->type = get_expression_type(expression);
1470 return (expression_t*) sizeof_expression;
1474 expression_t *parse_select_expression(unsigned precedence,
1475 expression_t *compound)
1479 assert(token.type == '.' || token.type == T_SELECT);
1482 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1484 select->expression.type = EXPR_SELECT;
1485 select->compound = compound;
1487 if(token.type != T_IDENTIFIER) {
1488 parse_error_expected("Problem while parsing compound select",
1492 select->symbol = token.v.symbol;
1495 return (expression_t*) select;
1499 expression_t *parse_call_expression(unsigned precedence,
1500 expression_t *expression)
1503 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1505 call->expression.type = EXPR_CALL;
1506 call->method = expression;
1508 /* parse arguments */
1511 if(token.type != ')') {
1512 call_argument_t *last_argument = NULL;
1515 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1517 argument->expression = parse_expression();
1518 if(last_argument == NULL) {
1519 call->arguments = argument;
1521 last_argument->next = argument;
1523 last_argument = argument;
1525 if(token.type != ',')
1532 return (expression_t*) call;
1536 expression_t *parse_conditional_expression(unsigned precedence,
1537 expression_t *expression)
1541 conditional_expression_t *conditional
1542 = allocate_ast_zero(sizeof(conditional[0]));
1543 conditional->condition = expression;
1545 conditional->true_expression = parse_expression();
1547 conditional->false_expression = parse_sub_expression(precedence);
1549 return (expression_t*) conditional;
1552 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1554 expression_t *parse_##unexpression_type(unsigned precedence) \
1558 unary_expression_t *unary_expression \
1559 = allocate_ast_zero(sizeof(unary_expression[0])); \
1560 unary_expression->expression.type = EXPR_UNARY; \
1561 unary_expression->type = unexpression_type; \
1562 unary_expression->value = parse_sub_expression(precedence); \
1564 return (expression_t*) unary_expression; \
1567 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1568 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1569 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1570 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1571 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1572 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1573 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1574 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1576 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1578 expression_t *parse_##unexpression_type(unsigned precedence, \
1579 expression_t *left) \
1581 (void) precedence; \
1584 unary_expression_t *unary_expression \
1585 = allocate_ast_zero(sizeof(unary_expression[0])); \
1586 unary_expression->expression.type = EXPR_UNARY; \
1587 unary_expression->type = unexpression_type; \
1588 unary_expression->value = left; \
1590 return (expression_t*) unary_expression; \
1593 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1594 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1596 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1598 expression_t *parse_##binexpression_type(unsigned precedence, \
1599 expression_t *left) \
1603 expression_t *right = parse_sub_expression(precedence); \
1605 binary_expression_t *binexpr \
1606 = allocate_ast_zero(sizeof(binexpr[0])); \
1607 binexpr->expression.type = EXPR_BINARY; \
1608 binexpr->type = binexpression_type; \
1609 binexpr->left = left; \
1610 binexpr->right = right; \
1612 return (expression_t*) binexpr; \
1615 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1616 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1617 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1618 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1619 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1620 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1621 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1622 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1623 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_NOTEQUAL)
1624 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1625 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1626 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1627 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1628 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1629 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1630 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1631 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1632 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1635 expression_t *parse_sub_expression(unsigned precedence)
1637 if(token.type < 0) {
1638 return expected_expression_error();
1641 expression_parser_function_t *parser
1642 = &expression_parsers[token.type];
1643 source_position_t source_position = token.source_position;
1646 if(parser->parser != NULL) {
1647 left = parser->parser(parser->precedence);
1649 left = parse_primary_expression();
1652 left->source_position = source_position;
1655 if(token.type < 0) {
1656 return expected_expression_error();
1659 parser = &expression_parsers[token.type];
1660 if(parser->infix_parser == NULL)
1662 if(parser->infix_precedence < precedence)
1665 left = parser->infix_parser(parser->infix_precedence, left);
1667 left->source_position = source_position;
1674 expression_t *parse_expression(void)
1676 return parse_sub_expression(1);
1681 void register_expression_parser(parse_expression_function parser,
1682 int token_type, unsigned precedence)
1684 expression_parser_function_t *entry = &expression_parsers[token_type];
1686 if(entry->parser != NULL) {
1687 fprintf(stderr, "for token ");
1688 print_token_type(stderr, token_type);
1689 fprintf(stderr, "\n");
1690 panic("trying to register multiple expression parsers for a token");
1692 entry->parser = parser;
1693 entry->precedence = precedence;
1696 void register_expression_infix_parser(parse_expression_infix_function parser,
1697 int token_type, unsigned precedence)
1699 expression_parser_function_t *entry = &expression_parsers[token_type];
1701 if(entry->infix_parser != NULL) {
1702 fprintf(stderr, "for token ");
1703 print_token_type(stderr, token_type);
1704 fprintf(stderr, "\n");
1705 panic("trying to register multiple infix expression parsers for a "
1708 entry->infix_parser = parser;
1709 entry->infix_precedence = precedence;
1713 void init_expression_parsers(void)
1715 memset(&expression_parsers, 0, sizeof(expression_parsers));
1717 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
1718 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
1719 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT,
1721 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
1722 T_GREATERGREATER, 16);
1723 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
1724 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
1725 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
1726 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
1727 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
1728 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
1729 T_GREATEREQUAL, 14);
1730 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
1731 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
1732 T_EXCLAMATIONMARKEQUAL, 13);
1733 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
1734 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
1735 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
1736 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
1737 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
1738 register_expression_infix_parser(parse_conditional_expression, '?', 7);
1739 register_expression_infix_parser(parse_BINEXPR_ASSIGN, T_EQUAL, 2);
1741 register_expression_infix_parser(parse_array_expression, '[', 30);
1742 register_expression_infix_parser(parse_call_expression, '(', 30);
1743 register_expression_infix_parser(parse_select_expression, '.', 30);
1744 register_expression_infix_parser(parse_select_expression, T_SELECT, 30);
1745 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
1747 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
1750 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
1751 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
1752 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
1753 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
1754 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
1755 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
1756 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
1757 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
1758 register_expression_parser(parse_sizeof, T_sizeof, 25);
1763 statement_t *parse_case_statement(void)
1774 statement_t *parse_default_statement(void)
1784 statement_t *parse_label_statement(void)
1794 statement_t *parse_if(void)
1802 if(token.type == T_else) {
1811 statement_t *parse_switch(void)
1823 statement_t *parse_while(void)
1835 statement_t *parse_do(void)
1848 statement_t *parse_for(void)
1852 if(token.type != ';') {
1853 /* TODO not correct... this could also be a declaration */
1857 if(token.type != ';') {
1861 if(token.type != ')') {
1871 statement_t *parse_goto(void)
1874 expect(T_IDENTIFIER);
1881 statement_t *parse_continue(void)
1886 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1887 statement->source_position = token.source_position;
1888 statement->type = STATEMENT_CONTINUE;
1894 statement_t *parse_break(void)
1903 statement_t *parse_return(void)
1907 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
1908 statement->statement.type = STATEMENT_RETURN;
1909 if(token.type != ';') {
1910 statement->return_value = parse_expression();
1914 return (statement_t*) statement;
1918 statement_t *parse_declaration_statement(void)
1920 parse_declaration();
1925 statement_t *parse_expression_statement(void)
1932 statement_t *parse_statement(void)
1934 declaration_t *declaration;
1935 statement_t *statement = NULL;
1937 /* declaration or statement */
1938 switch(token.type) {
1940 statement = parse_case_statement();
1944 statement = parse_default_statement();
1948 statement = parse_compound_statement();
1952 statement = parse_if();
1956 statement = parse_switch();
1960 statement = parse_while();
1964 statement = parse_do();
1968 statement = parse_for();
1972 statement = parse_goto();
1976 statement = parse_continue();
1980 statement = parse_break();
1984 statement = parse_return();
1992 if(la(1)->type == ':') {
1993 statement = parse_label_statement();
1997 declaration = token.v.symbol->declaration;
1998 if(declaration != NULL &&
1999 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2000 statement = parse_declaration_statement();
2004 statement = parse_expression_statement();
2008 statement = parse_declaration_statement();
2016 statement_t *parse_compound_statement(void)
2020 compound_statement_t *compound_statement
2021 = allocate_ast_zero(sizeof(compound_statement[0]));
2022 compound_statement->statement.type = STATEMENT_COMPOUND;
2024 int top = environment_top();
2025 context_t *last_context = context;
2026 set_context(&compound_statement->context);
2028 statement_t *last_statement = NULL;
2030 while(token.type != '}' && token.type != T_EOF) {
2031 statement_t *statement = parse_statement();
2033 if(last_statement != NULL) {
2034 last_statement->next = statement;
2036 compound_statement->statements = statement;
2038 last_statement = statement;
2041 assert(context == &compound_statement->context);
2042 set_context(last_context);
2043 environment_pop_to(top);
2047 return (statement_t*) compound_statement;
2051 translation_unit_t *parse_translation_unit(void)
2053 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2055 assert(context == NULL);
2056 set_context(&unit->context);
2058 while(token.type != T_EOF) {
2059 parse_declaration();
2062 assert(context == &unit->context);
2064 last_declaration = NULL;
2069 translation_unit_t *parse(void)
2071 obstack_init(&environment_obstack);
2072 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2074 lookahead_bufpos = 0;
2075 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2078 translation_unit_t *unit = parse_translation_unit();
2080 DEL_ARR_F(environment_stack);
2081 obstack_free(&environment_obstack, NULL);
2086 void init_parser(void)
2088 init_expression_parsers();
2089 obstack_init(&temp_obst);
2092 void exit_parser(void)
2094 obstack_free(&temp_obst, NULL);