11 #include "type_hash.h"
13 #include "adt/bitfiddle.h"
14 #include "adt/error.h"
15 #include "adt/array.h"
17 //#define PRINT_TOKENS
18 //#define ABORT_ON_ERROR
19 #define MAX_LOOKAHEAD 2
21 struct environment_entry_t {
23 declaration_t *old_declaration;
24 const void *old_context;
28 static token_t lookahead_buffer[MAX_LOOKAHEAD];
29 static int lookahead_bufpos;
30 static struct obstack environment_obstack;
31 static environment_entry_t **environment_stack = NULL;
32 static context_t *context = NULL;
33 static declaration_t *last_declaration = NULL;
34 static struct obstack temp_obst;
36 static statement_t *parse_compound_statement(void);
37 static statement_t *parse_statement(void);
39 static expression_t *parse_sub_expression(unsigned precedence);
40 static expression_t *parse_expression(void);
41 static type_t *parse_typename(void);
43 #define STORAGE_CLASSES \
50 #define TYPE_QUALIFIERS \
56 #ifdef PROVIDE_COMPLEX
57 #define COMPLEX_SPECIFIERS \
60 #define COMPLEX_SPECIFIERS
63 #ifdef PROVIDE_IMAGINARY
64 #define IMAGINARY_SPECIFIERS \
67 #define IMAGINARY_SPECIFIERS
70 #define TYPE_SPECIFIERS \
88 #define DECLARATION_START \
93 #define TYPENAME_START \
97 static inline void *allocate_ast_zero(size_t size)
99 void *res = allocate_ast(size);
100 memset(res, 0, size);
104 static inline void *allocate_type_zero(size_t size)
106 void *res = obstack_alloc(type_obst, size);
107 memset(res, 0, size);
112 * returns the top element of the environment stack
114 static inline size_t environment_top(void)
116 return ARR_LEN(environment_stack);
121 static inline void next_token(void)
123 token = lookahead_buffer[lookahead_bufpos];
124 lookahead_buffer[lookahead_bufpos] = lexer_token;
127 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
130 print_token(stderr, &token);
131 fprintf(stderr, "\n");
135 static inline const token_t *look_ahead(int num)
137 assert(num > 0 && num <= MAX_LOOKAHEAD);
138 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
139 return & lookahead_buffer[pos];
142 static inline void eat(token_type_t type)
144 assert(token.type == type);
148 void parser_print_error_prefix_pos(const source_position_t source_position)
150 fputs(source_position.input_name, stderr);
152 fprintf(stderr, "%d", source_position.linenr);
153 fputs(": error: ", stderr);
154 #ifdef ABORT_ON_ERROR
159 void parser_print_error_prefix(void)
161 parser_print_error_prefix_pos(token.source_position);
164 static void parse_error(const char *message)
166 parser_print_error_prefix();
167 fprintf(stderr, "parse error: %s\n", message);
170 static void parse_error_expected(const char *message, ...)
175 if(message != NULL) {
176 parser_print_error_prefix();
177 fprintf(stderr, "%s\n", message);
179 parser_print_error_prefix();
180 fputs("Parse error: got ", stderr);
181 print_token(stderr, &token);
182 fputs(", expected ", stderr);
184 va_start(args, message);
185 token_type_t token_type = va_arg(args, token_type_t);
186 while(token_type != 0) {
190 fprintf(stderr, ", ");
192 print_token_type(stderr, token_type);
193 token_type = va_arg(args, token_type_t);
196 fprintf(stderr, "\n");
199 static void eat_block(void)
201 if(token.type == '{')
204 while(token.type != '}') {
205 if(token.type == T_EOF)
207 if(token.type == '{') {
216 static void eat_statement(void)
218 while(token.type != ';') {
219 if(token.type == T_EOF)
221 if(token.type == '}')
223 if(token.type == '{') {
232 static void eat_brace(void)
234 if(token.type == '(')
237 while(token.type != ')') {
238 if(token.type == T_EOF)
240 if(token.type == '{') {
249 #define expect(expected) \
250 if(UNLIKELY(token.type != (expected))) { \
251 parse_error_expected(NULL, (expected), 0); \
257 #define expect_void(expected) \
258 if(UNLIKELY(token.type != (expected))) { \
259 parse_error_expected(NULL, (expected), 0); \
265 static void set_context(context_t *new_context)
267 context = new_context;
269 declaration_t *declaration = new_context->declarations;
270 if(declaration != NULL) {
272 if(declaration->next == NULL)
274 declaration = declaration->next;
278 last_declaration = declaration;
282 * called when we find a 2nd declarator for an identifier we already have a
285 static int is_compatible_declaration (declaration_t *declaration,
286 declaration_t *previous)
288 /* TODO: not correct yet */
289 return declaration->type == previous->type;
293 * pushs an environment_entry on the environment stack and links the
294 * corresponding symbol to the new entry
296 static inline declaration_t *environment_push(declaration_t *declaration,
299 environment_entry_t *entry
300 = obstack_alloc(&environment_obstack, sizeof(entry[0]));
301 memset(entry, 0, sizeof(entry[0]));
303 int top = ARR_LEN(environment_stack);
304 ARR_RESIZE(environment_stack, top + 1);
305 environment_stack[top] = entry;
307 assert(declaration->source_position.input_name != NULL);
309 symbol_t *symbol = declaration->symbol;
310 assert(declaration != symbol->declaration);
312 if(symbol->context == context) {
313 declaration_t *previous_declaration = symbol->declaration;
314 if(symbol->declaration != NULL) {
315 if(!is_compatible_declaration(declaration, previous_declaration)) {
316 parser_print_error_prefix_pos(declaration->source_position);
317 fprintf(stderr, "definition of symbol '%s' with type ",
318 declaration->symbol->string);
319 print_type(declaration->type);
321 parser_print_error_prefix_pos(
322 previous_declaration->source_position);
323 fprintf(stderr, "is incompatible with previous declaration "
325 print_type(previous_declaration->type);
328 return previous_declaration;
332 entry->old_declaration = symbol->declaration;
333 entry->old_context = symbol->context;
334 entry->symbol = symbol;
335 symbol->declaration = declaration;
336 symbol->context = context;
342 * pops symbols from the environment stack until @p new_top is the top element
344 static inline void environment_pop_to(size_t new_top)
346 environment_entry_t *entry = NULL;
347 size_t top = ARR_LEN(environment_stack);
353 assert(new_top < top);
356 entry = environment_stack[i - 1];
358 symbol_t *symbol = entry->symbol;
360 symbol->declaration = entry->old_declaration;
361 symbol->context = entry->old_context;
364 } while(i != new_top);
365 obstack_free(&environment_obstack, entry);
367 ARR_SHRINKLEN(environment_stack, (int) new_top);
372 static expression_t *parse_constant_expression(void)
374 /* start parsing at precedence 7 (conditional expression) */
375 return parse_sub_expression(7);
378 static expression_t *parse_assignment_expression(void)
380 /* start parsing at precedence 2 (assignment expression) */
381 return parse_sub_expression(2);
384 static void parse_compound_type_entries(void);
385 static void parse_declarator(declaration_t *declaration,
386 storage_class_t storage_class, type_t *type,
387 int may_be_abstract);
388 static declaration_t *record_declaration(declaration_t *declaration);
390 typedef struct declaration_specifiers_t declaration_specifiers_t;
391 struct declaration_specifiers_t {
392 storage_class_t storage_class;
396 static compound_type_t *find_compound_type(compound_type_t *types,
397 const symbol_t *symbol)
399 compound_type_t *type = types;
400 for( ; type != NULL; type = type->next) {
401 if(type->symbol == symbol)
408 static type_t *parse_compound_type_specifier(int is_struct)
416 symbol_t *symbol = NULL;
417 compound_type_t *compound_type = NULL;
419 if(token.type == T_IDENTIFIER) {
420 symbol = token.v.symbol;
423 if(context != NULL) {
425 compound_type = find_compound_type(context->structs, symbol);
427 compound_type = find_compound_type(context->unions, symbol);
430 } else if(token.type != '{') {
432 parse_error_expected("problem while parsing struct type specifier",
433 T_IDENTIFIER, '{', 0);
435 parse_error_expected("problem while parsing union type specifier",
436 T_IDENTIFIER, '{', 0);
442 if(compound_type == NULL) {
443 compound_type = allocate_type_zero(sizeof(compound_type[0]));
446 compound_type->type.type = TYPE_COMPOUND_STRUCT;
448 compound_type->type.type = TYPE_COMPOUND_UNION;
450 compound_type->source_position = token.source_position;
451 compound_type->symbol = symbol;
454 if(token.type == '{') {
455 if(compound_type->defined) {
456 parser_print_error_prefix();
457 fprintf(stderr, "multiple definition of %s %s\n",
458 is_struct ? "struct" : "union", symbol->string);
459 compound_type->context.declarations = NULL;
461 compound_type->defined = 1;
463 int top = environment_top();
464 context_t *last_context = context;
465 set_context(&compound_type->context);
467 parse_compound_type_entries();
469 assert(context == &compound_type->context);
470 set_context(last_context);
471 environment_pop_to(top);
474 return (type_t*) compound_type;
477 static void parse_enum_entries(void)
481 if(token.type == '}') {
483 parse_error("empty enum not allowed");
488 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
490 if(token.type != T_IDENTIFIER) {
491 parse_error_expected("problem while parsing enum entry",
496 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
497 entry->symbol = token.v.symbol;
498 entry->source_position = token.source_position;
501 if(token.type == '=') {
503 entry->initializer = parse_constant_expression();
506 record_declaration(entry);
508 if(token.type != ',')
511 } while(token.type != '}');
516 static enum_type_t *find_enum_type(enum_type_t *types, const symbol_t *symbol)
518 enum_type_t *type = types;
519 for( ; type != NULL; type = type->next) {
520 if(type->symbol == symbol)
527 static type_t *parse_enum_specifier(void)
531 symbol_t *symbol = NULL;
532 enum_type_t *enum_type = NULL;
534 if(token.type == T_IDENTIFIER) {
535 symbol = token.v.symbol;
538 if(context != NULL) {
539 enum_type = find_enum_type(context->enums, symbol);
541 } else if(token.type != '{') {
542 parse_error_expected("problem while parsing enum type specifier",
543 T_IDENTIFIER, '{', 0);
547 if(enum_type == NULL) {
548 enum_type = allocate_type_zero(sizeof(enum_type[0]));
549 enum_type->type.type = TYPE_ENUM;
550 enum_type->source_position = token.source_position;
551 enum_type->symbol = symbol;
554 if(token.type == '{') {
555 if(enum_type->defined) {
556 parser_print_error_prefix();
557 fprintf(stderr, "multiple definitions of enum %s\n",
559 enum_type->entries_begin = NULL;
560 enum_type->entries_end = NULL;
562 enum_type->defined = 1;
564 declaration_t *before = last_declaration;
566 parse_enum_entries();
569 enum_type->entries_begin = context->declarations;
571 enum_type->entries_begin = before->next;
573 enum_type->entries_end = last_declaration;
576 return (type_t*) enum_type;
579 static type_t *parse_typeof(void)
587 declaration_t *declaration;
588 expression_t *expression;
592 case T___extension__:
593 /* this can be a prefix to a typename or an expression */
594 /* we simply eat it now. */
597 } while(token.type == T___extension__);
601 declaration = token.v.symbol->declaration;
602 if(declaration != NULL
603 && declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
604 result = parse_typename();
607 expression = parse_expression();
608 result = expression->datatype;
612 result = parse_typename();
616 expression = parse_expression();
617 result = expression->datatype;
626 static const char *parse_string_literals(void)
628 assert(token.type == T_STRING_LITERAL);
629 const char *result = token.v.string;
633 while(token.type == T_STRING_LITERAL) {
634 result = concat_strings(result, token.v.string);
641 static void parse_attributes(void)
645 case T___attribute__:
653 parse_error("EOF while parsing attribute");
671 if(token.type != T_STRING_LITERAL) {
672 parse_error_expected("while parsing assembler attribute",
677 parse_string_literals();
682 goto attributes_finished;
691 SPECIFIER_SIGNED = 1 << 0,
692 SPECIFIER_UNSIGNED = 1 << 1,
693 SPECIFIER_LONG = 1 << 2,
694 SPECIFIER_INT = 1 << 3,
695 SPECIFIER_DOUBLE = 1 << 4,
696 SPECIFIER_CHAR = 1 << 5,
697 SPECIFIER_SHORT = 1 << 6,
698 SPECIFIER_LONG_LONG = 1 << 7,
699 SPECIFIER_FLOAT = 1 << 8,
700 SPECIFIER_BOOL = 1 << 9,
701 SPECIFIER_VOID = 1 << 10,
702 #ifdef PROVIDE_COMPLEX
703 SPECIFIER_COMPLEX = 1 << 11,
705 #ifdef PROVIDE_IMAGINARY
706 SPECIFIER_IMAGINARY = 1 << 12,
710 static type_t *create_builtin_type(symbol_t *symbol)
712 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
713 type->type.type = TYPE_BUILTIN;
714 type->symbol = symbol;
716 type_t *result = typehash_insert((type_t*) type);
717 if(result != (type_t*) type) {
718 obstack_free(type_obst, type);
724 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
726 declaration_t *declaration;
728 unsigned type_qualifiers = 0;
729 unsigned type_specifiers = 0;
736 #define MATCH_STORAGE_CLASS(token, class) \
738 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
739 parse_error("multiple storage classes in declaration " \
742 specifiers->storage_class = class; \
746 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
747 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
748 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
749 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
750 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
752 /* type qualifiers */
753 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
755 type_qualifiers |= qualifier; \
759 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
760 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
761 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
762 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
764 case T___extension__:
769 /* type specifiers */
770 #define MATCH_SPECIFIER(token, specifier, name) \
773 if(type_specifiers & specifier) { \
774 parse_error("multiple " name " type specifiers given"); \
776 type_specifiers |= specifier; \
780 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
781 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
782 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
783 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
784 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
785 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
786 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
787 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
788 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
789 #ifdef PROVIDE_COMPLEX
790 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
792 #ifdef PROVIDE_IMAGINARY
793 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
797 if(type_specifiers & SPECIFIER_LONG_LONG) {
798 parse_error("multiple type specifiers given");
799 } else if(type_specifiers & SPECIFIER_LONG) {
800 type_specifiers |= SPECIFIER_LONG_LONG;
802 type_specifiers |= SPECIFIER_LONG;
806 /* TODO: if type != NULL for the following rules issue an error */
808 type = parse_compound_type_specifier(1);
811 type = parse_compound_type_specifier(0);
814 type = parse_enum_specifier();
817 type = parse_typeof();
819 case T___builtin_va_list:
820 type = create_builtin_type(token.v.symbol);
824 case T___attribute__:
830 declaration = token.v.symbol->declaration;
831 if(declaration == NULL ||
832 declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
833 goto finish_specifiers;
836 type = declaration->type;
837 assert(type != NULL);
841 /* function specifier */
843 goto finish_specifiers;
850 atomic_type_type_t atomic_type;
852 /* match valid basic types */
853 switch(type_specifiers) {
855 atomic_type = ATOMIC_TYPE_VOID;
858 atomic_type = ATOMIC_TYPE_CHAR;
860 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
861 atomic_type = ATOMIC_TYPE_SCHAR;
863 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
864 atomic_type = ATOMIC_TYPE_UCHAR;
866 case SPECIFIER_SHORT:
867 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
868 case SPECIFIER_SHORT | SPECIFIER_INT:
869 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
870 atomic_type = ATOMIC_TYPE_SHORT;
872 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
873 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
874 atomic_type = ATOMIC_TYPE_USHORT;
877 case SPECIFIER_SIGNED:
878 case SPECIFIER_SIGNED | SPECIFIER_INT:
879 atomic_type = ATOMIC_TYPE_INT;
881 case SPECIFIER_UNSIGNED:
882 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
883 atomic_type = ATOMIC_TYPE_UINT;
886 case SPECIFIER_SIGNED | SPECIFIER_LONG:
887 case SPECIFIER_LONG | SPECIFIER_INT:
888 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
889 atomic_type = ATOMIC_TYPE_LONG;
891 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
892 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
893 atomic_type = ATOMIC_TYPE_ULONG;
895 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
896 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
897 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
898 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
900 atomic_type = ATOMIC_TYPE_LONGLONG;
902 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
903 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
905 atomic_type = ATOMIC_TYPE_ULONGLONG;
907 case SPECIFIER_FLOAT:
908 atomic_type = ATOMIC_TYPE_FLOAT;
910 case SPECIFIER_DOUBLE:
911 atomic_type = ATOMIC_TYPE_DOUBLE;
913 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
914 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
917 atomic_type = ATOMIC_TYPE_BOOL;
919 #ifdef PROVIDE_COMPLEX
920 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
921 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
923 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
924 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
926 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
927 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
930 #ifdef PROVIDE_IMAGINARY
931 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
932 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
934 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
935 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
937 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
938 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
942 /* invalid specifier combination, give an error message */
943 if(type_specifiers == 0) {
944 parse_error("no type specifiers given in declaration");
945 } else if((type_specifiers & SPECIFIER_SIGNED) &&
946 (type_specifiers & SPECIFIER_UNSIGNED)) {
947 parse_error("signed and unsigned specifiers gives");
948 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
949 parse_error("only integer types can be signed or unsigned");
951 parse_error("multiple datatypes in declaration");
953 atomic_type = ATOMIC_TYPE_INVALID;
956 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
957 atype->type.type = TYPE_ATOMIC;
958 atype->atype = atomic_type;
961 type = (type_t*) atype;
963 if(type_specifiers != 0) {
964 parse_error("multiple datatypes in declaration");
968 type->qualifiers = type_qualifiers;
970 type_t *result = typehash_insert(type);
971 if(newtype && result != (type_t*) type) {
972 obstack_free(type_obst, type);
975 specifiers->type = result;
978 static unsigned parse_type_qualifiers(void)
980 unsigned type_qualifiers = 0;
984 /* type qualifiers */
985 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
986 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
987 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
988 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
991 return type_qualifiers;
996 typedef struct parsed_pointer_t parsed_pointer_t;
997 struct parsed_pointer_t {
998 unsigned type_qualifiers;
999 parsed_pointer_t *next;
1002 static parsed_pointer_t *parse_pointers(void)
1004 parsed_pointer_t *result = NULL;
1005 parsed_pointer_t *last_pointer = NULL;
1007 while(token.type == '*') {
1009 parsed_pointer_t *pointer
1010 = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1011 pointer->type_qualifiers = parse_type_qualifiers();
1013 if(last_pointer != NULL) {
1014 last_pointer->next = pointer;
1018 last_pointer = pointer;
1024 static type_t *make_pointers(type_t *type, parsed_pointer_t *pointer)
1026 for( ; pointer != NULL; pointer = pointer->next) {
1027 pointer_type_t *pointer_type
1028 = allocate_type_zero(sizeof(pointer_type[0]));
1029 pointer_type->type.type = TYPE_POINTER;
1030 pointer_type->points_to = type;
1031 pointer_type->type.qualifiers = pointer->type_qualifiers;
1033 type_t *result = typehash_insert((type_t*) pointer_type);
1034 if(result != (type_t*) pointer_type) {
1035 obstack_free(type_obst, pointer_type);
1044 static void parse_identifier_list(void)
1047 if(token.type != T_IDENTIFIER) {
1048 parse_error_expected("problem while parsing parameter identifier "
1049 "list", T_IDENTIFIER, 0);
1053 if(token.type != ',')
1059 static declaration_t *parse_parameter(void)
1061 declaration_specifiers_t specifiers;
1062 memset(&specifiers, 0, sizeof(specifiers));
1064 parse_declaration_specifiers(&specifiers);
1066 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1067 parse_declarator(declaration, specifiers.storage_class,
1068 specifiers.type, 1);
1073 static declaration_t *parse_parameters(method_type_t *type)
1075 if(token.type == T_IDENTIFIER) {
1076 symbol_t *symbol = token.v.symbol;
1077 declaration_t *declaration = symbol->declaration;
1078 if(declaration == NULL
1079 || declaration->storage_class != STORAGE_CLASS_TYPEDEF) {
1081 parse_identifier_list();
1086 if(token.type == ')') {
1087 type->unspecified_parameters = 1;
1090 if(token.type == T_void && look_ahead(1)->type == ')') {
1095 declaration_t *declarations = NULL;
1096 declaration_t *declaration;
1097 declaration_t *last_declaration = NULL;
1098 method_parameter_t *parameter;
1099 method_parameter_t *last_parameter = NULL;
1102 switch(token.type) {
1106 return declarations;
1109 case T___extension__:
1111 declaration = parse_parameter();
1113 parameter = allocate_type_zero(sizeof(parameter[0]));
1114 parameter->type = declaration->type;
1116 if(last_parameter != NULL) {
1117 last_declaration->next = declaration;
1118 last_parameter->next = parameter;
1120 type->parameters = parameter;
1121 declarations = declaration;
1123 last_parameter = parameter;
1124 last_declaration = declaration;
1128 return declarations;
1130 if(token.type != ',')
1131 return declarations;
1136 typedef struct declarator_part declarator_part;
1137 struct declarator_part {
1138 parsed_pointer_t *pointers;
1139 method_type_t *method_type;
1140 declarator_part *inner;
1144 static declarator_part *parse_inner_declarator(declaration_t *declaration,
1145 int may_be_abstract)
1147 declarator_part *part = obstack_alloc(&temp_obst, sizeof(part[0]));
1148 memset(part, 0, sizeof(part[0]));
1150 part->pointers = parse_pointers();
1152 /* TODO: find out if this is correct */
1155 switch(token.type) {
1157 if(declaration == NULL) {
1158 parse_error("no identifier expected in typename");
1160 declaration->symbol = token.v.symbol;
1161 declaration->source_position = token.source_position;
1167 part->inner = parse_inner_declarator(declaration, may_be_abstract);
1173 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1178 switch(token.type) {
1182 method_type_t *method_type
1183 = allocate_type_zero(sizeof(method_type[0]));
1184 method_type->type.type = TYPE_METHOD;
1186 declaration_t *parameters = parse_parameters(method_type);
1187 if(declaration != NULL) {
1188 declaration->context.declarations = parameters;
1191 part->method_type = method_type;
1198 if(token.type == T_static) {
1202 unsigned type_qualifiers = parse_type_qualifiers();
1203 if(type_qualifiers != 0) {
1204 if(token.type == T_static) {
1211 if(token.type == '*' && look_ahead(1)->type == ']') {
1213 } else if(token.type != ']') {
1214 parse_assignment_expression();
1220 goto declarator_finished;
1224 declarator_finished:
1230 static type_t *construct_declarator_type(declarator_part *part, type_t *type)
1233 type = make_pointers(type, part->pointers);
1235 method_type_t *method_type = part->method_type;
1236 if(method_type != NULL) {
1237 method_type->result_type = type;
1239 type_t *result = typehash_insert((type_t*) method_type);
1240 if(result != (type_t*) method_type) {
1241 obstack_free(type_obst, method_type);
1247 } while(part != NULL);
1252 static void parse_declarator(declaration_t *declaration,
1253 storage_class_t storage_class, type_t *type,
1254 int may_be_abstract)
1256 declarator_part *part
1257 = parse_inner_declarator(declaration, may_be_abstract);
1260 declaration->type = construct_declarator_type(part, type);
1261 declaration->storage_class = storage_class;
1262 obstack_free(&temp_obst, part);
1266 static type_t *parse_abstract_declarator(type_t *base_type)
1268 declarator_part *part = parse_inner_declarator(NULL, 1);
1273 type_t *result = construct_declarator_type(part, base_type);
1274 obstack_free(&temp_obst, part);
1279 static declaration_t *record_declaration(declaration_t *declaration)
1284 symbol_t *symbol = declaration->symbol;
1285 if(symbol != NULL) {
1286 declaration_t *alias = environment_push(declaration, context);
1287 if(alias != declaration)
1291 if(last_declaration != NULL) {
1292 last_declaration->next = declaration;
1294 context->declarations = declaration;
1296 last_declaration = declaration;
1301 static void parser_error_multiple_definition(declaration_t *previous,
1302 declaration_t *declaration)
1304 parser_print_error_prefix_pos(declaration->source_position);
1305 fprintf(stderr, "multiple definition of symbol '%s'\n",
1306 declaration->symbol->string);
1307 parser_print_error_prefix_pos(previous->source_position);
1308 fprintf(stderr, "this is the location of the previous "
1312 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1315 declaration_t *ndeclaration
1316 = allocate_ast_zero(sizeof(ndeclaration[0]));
1318 parse_declarator(ndeclaration, specifiers->storage_class,
1319 specifiers->type, 0);
1320 declaration_t *declaration = record_declaration(ndeclaration);
1321 if(token.type == '=') {
1324 /* TODO: check that this is an allowed type (esp. no method type) */
1326 if(declaration->initializer != NULL) {
1327 parser_error_multiple_definition(declaration, ndeclaration);
1330 if(token.type == '{') {
1334 declaration->initializer = parse_assignment_expression();
1336 } else if(token.type == '{') {
1337 if(declaration->type->type != TYPE_METHOD) {
1338 parser_print_error_prefix();
1339 fprintf(stderr, "Declarator ");
1340 print_type_ext(declaration->type, declaration->symbol, NULL);
1341 fprintf(stderr, " is not a method type.\n");
1344 if(declaration->initializer != NULL) {
1345 parser_error_multiple_definition(declaration, ndeclaration);
1347 if(ndeclaration != declaration) {
1348 memcpy(&declaration->context, &ndeclaration->context,
1349 sizeof(declaration->context));
1352 int top = environment_top();
1353 context_t *last_context = context;
1354 set_context(&declaration->context);
1356 /* push function parameters */
1357 declaration_t *parameter = declaration->context.declarations;
1358 for( ; parameter != NULL; parameter = parameter->next) {
1359 environment_push(parameter, context);
1362 statement_t *statement = parse_compound_statement();
1364 assert(context == &declaration->context);
1365 set_context(last_context);
1366 environment_pop_to(top);
1368 declaration->statement = statement;
1372 if(token.type != ',')
1379 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1382 if(token.type == ':') {
1384 parse_constant_expression();
1385 /* TODO (bitfields) */
1387 declaration_t *declaration
1388 = allocate_ast_zero(sizeof(declaration[0]));
1389 parse_declarator(declaration, specifiers->storage_class,
1390 specifiers->type, 1);
1392 /* TODO: check for doubled fields */
1393 record_declaration(declaration);
1395 if(token.type == ':') {
1397 parse_constant_expression();
1398 /* TODO (bitfields) */
1402 if(token.type != ',')
1409 static void parse_compound_type_entries(void)
1413 while(token.type != '}' && token.type != T_EOF) {
1414 declaration_specifiers_t specifiers;
1415 memset(&specifiers, 0, sizeof(specifiers));
1416 /* TODO not correct as this allows storage class stuff... but only
1417 * specifiers and qualifiers sould be allowed here */
1418 parse_declaration_specifiers(&specifiers);
1420 parse_struct_declarators(&specifiers);
1422 if(token.type == T_EOF) {
1423 parse_error("unexpected error while parsing struct");
1428 static void parse_declaration(void)
1430 declaration_specifiers_t specifiers;
1431 memset(&specifiers, 0, sizeof(specifiers));
1432 parse_declaration_specifiers(&specifiers);
1434 if(token.type == ';') {
1438 parse_init_declarators(&specifiers);
1441 static type_t *parse_typename(void)
1443 declaration_specifiers_t specifiers;
1444 memset(&specifiers, 0, sizeof(specifiers));
1445 parse_declaration_specifiers(&specifiers);
1446 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1447 /* TODO: improve error message, user does probably not know what a
1448 * storage class is...
1450 parse_error("typename may not have a storage class");
1453 type_t *result = parse_abstract_declarator(specifiers.type);
1461 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1462 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1463 expression_t *left);
1465 typedef struct expression_parser_function_t expression_parser_function_t;
1466 struct expression_parser_function_t {
1467 unsigned precedence;
1468 parse_expression_function parser;
1469 unsigned infix_precedence;
1470 parse_expression_infix_function infix_parser;
1473 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1475 static expression_t *expected_expression_error(void)
1477 parser_print_error_prefix();
1478 fprintf(stderr, "expected expression, got token ");
1479 print_token(stderr, & token);
1480 fprintf(stderr, "\n");
1482 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1483 expression->type = EXPR_INVALID;
1489 static expression_t *parse_string_const(void)
1491 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1493 cnst->expression.type = EXPR_STRING_LITERAL;
1494 cnst->value = parse_string_literals();
1496 return (expression_t*) cnst;
1499 static expression_t *parse_int_const(void)
1501 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1503 cnst->expression.type = EXPR_CONST;
1504 cnst->value = token.v.intvalue;
1508 return (expression_t*) cnst;
1511 static expression_t *parse_reference(void)
1513 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1515 ref->expression.type = EXPR_REFERENCE;
1516 ref->symbol = token.v.symbol;
1518 if(ref->symbol->declaration == NULL) {
1519 parser_print_error_prefix();
1520 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1522 ref->declaration = ref->symbol->declaration;
1526 return (expression_t*) ref;
1529 static expression_t *parse_cast(void)
1531 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1533 cast->expression.type = EXPR_UNARY;
1534 cast->type = UNEXPR_CAST;
1535 cast->expression.source_position = token.source_position;
1537 type_t *type = parse_typename();
1540 expression_t *value = parse_sub_expression(20);
1542 cast->expression.datatype = type;
1543 cast->value = value;
1545 return (expression_t*) cast;
1548 static expression_t *parse_statement_expression(void)
1550 statement_expression_t *expression
1551 = allocate_ast_zero(sizeof(expression[0]));
1552 expression->expression.type = EXPR_STATEMENT;
1553 expression->statement = parse_compound_statement();
1557 return (expression_t*) expression;
1560 static expression_t *parse_brace_expression(void)
1564 declaration_t *declaration;
1565 switch(token.type) {
1567 /* gcc extension: a stement expression */
1568 return parse_statement_expression();
1572 return parse_cast();
1574 declaration = token.v.symbol->declaration;
1575 if(declaration != NULL &&
1576 (declaration->storage_class == STORAGE_CLASS_TYPEDEF)) {
1577 return parse_cast();
1581 expression_t *result = parse_expression();
1587 static expression_t *parse_function_keyword(void)
1589 eat(T___FUNCTION__);
1592 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1593 expression->expression.type = EXPR_FUNCTION;
1594 expression->value = "TODO: FUNCTION";
1596 return (expression_t*) expression;
1599 static expression_t *parse_pretty_function_keyword(void)
1601 eat(T___PRETTY_FUNCTION__);
1604 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1605 expression->expression.type = EXPR_PRETTY_FUNCTION;
1606 expression->value = "TODO: PRETTY FUNCTION";
1608 return (expression_t*) expression;
1611 static member_designator_t *parse_member_designators(void)
1613 member_designator_t *result = allocate_ast_zero(sizeof(result[0]));
1615 if(token.type != T_IDENTIFIER) {
1616 parse_error_expected("problem while parsing member designator",
1621 result->symbol = token.v.symbol;
1624 member_designator_t *last_designator = result;
1626 if(token.type == '.') {
1628 if(token.type != T_IDENTIFIER) {
1629 parse_error_expected("problem while parsing member designator",
1634 member_designator_t *designator
1635 = allocate_ast_zero(sizeof(result[0]));
1636 designator->symbol = token.v.symbol;
1639 last_designator->next = designator;
1640 last_designator = designator;
1643 if(token.type == '[') {
1645 member_designator_t *designator
1646 = allocate_ast_zero(sizeof(result[0]));
1647 designator->array_access = parse_expression();
1648 if(designator->array_access == NULL) {
1654 last_designator->next = designator;
1655 last_designator = designator;
1664 static expression_t *parse_offsetof(void)
1666 eat(T___builtin_offsetof);
1668 offsetof_expression_t *expression
1669 = allocate_ast_zero(sizeof(expression[0]));
1670 expression->expression.type = EXPR_OFFSETOF;
1673 expression->type = parse_typename();
1675 expression->member_designators = parse_member_designators();
1678 return (expression_t*) expression;
1681 static expression_t *parse_builtin_symbol(void)
1683 builtin_symbol_expression_t *expression
1684 = allocate_ast_zero(sizeof(expression[0]));
1685 expression->expression.type = EXPR_BUILTIN_SYMBOL;
1687 expression->symbol = token.v.symbol;
1691 return (expression_t*) expression;
1694 static expression_t *parse_primary_expression(void)
1696 switch(token.type) {
1698 return parse_int_const();
1699 case T_STRING_LITERAL:
1700 return parse_string_const();
1702 return parse_reference();
1703 case T___FUNCTION__:
1704 return parse_function_keyword();
1705 case T___PRETTY_FUNCTION__:
1706 return parse_pretty_function_keyword();
1707 case T___builtin_offsetof:
1708 return parse_offsetof();
1709 case T___builtin_expect:
1710 case T___builtin_va_start:
1711 case T___builtin_va_arg:
1712 case T___builtin_va_end:
1713 return parse_builtin_symbol();
1716 return parse_brace_expression();
1719 parser_print_error_prefix();
1720 fprintf(stderr, "unexpected token ");
1721 print_token(stderr, &token);
1722 fprintf(stderr, "\n");
1727 static expression_t *parse_array_expression(unsigned precedence,
1728 expression_t *array_ref)
1734 array_access_expression_t *array_access
1735 = allocate_ast_zero(sizeof(array_access[0]));
1737 array_access->expression.type = EXPR_ARRAY_ACCESS;
1738 array_access->array_ref = array_ref;
1739 array_access->index = parse_expression();
1741 if(token.type != ']') {
1742 parse_error_expected("Problem while parsing array access", ']', 0);
1747 return (expression_t*) array_access;
1750 static type_t *get_expression_type(const expression_t *expression)
1757 static int is_declaration_specifier(const token_t *token,
1758 int only_type_specifiers)
1760 declaration_t *declaration;
1762 switch(token->type) {
1766 declaration = token->v.symbol->declaration;
1767 if(declaration == NULL)
1769 if(declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1774 if(only_type_specifiers)
1783 static expression_t *parse_sizeof(unsigned precedence)
1787 sizeof_expression_t *sizeof_expression
1788 = allocate_ast_zero(sizeof(sizeof_expression[0]));
1789 sizeof_expression->expression.type = EXPR_SIZEOF;
1791 if(token.type == '(' && is_declaration_specifier(look_ahead(1), 1)) {
1793 sizeof_expression->type = parse_typename();
1796 expression_t *expression = parse_sub_expression(precedence);
1797 sizeof_expression->type = get_expression_type(expression);
1798 sizeof_expression->size_expression = expression;
1801 return (expression_t*) sizeof_expression;
1804 static expression_t *parse_select_expression(unsigned precedence,
1805 expression_t *compound)
1809 assert(token.type == '.' || token.type == T_MINUSGREATER);
1812 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
1814 select->expression.type = EXPR_SELECT;
1815 select->compound = compound;
1817 if(token.type != T_IDENTIFIER) {
1818 parse_error_expected("Problem while parsing compound select",
1822 select->symbol = token.v.symbol;
1825 return (expression_t*) select;
1828 static expression_t *parse_call_expression(unsigned precedence,
1829 expression_t *expression)
1832 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
1834 call->expression.type = EXPR_CALL;
1835 call->method = expression;
1837 /* parse arguments */
1840 if(token.type != ')') {
1841 call_argument_t *last_argument = NULL;
1844 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
1846 argument->expression = parse_assignment_expression();
1847 if(last_argument == NULL) {
1848 call->arguments = argument;
1850 last_argument->next = argument;
1852 last_argument = argument;
1854 if(token.type != ',')
1861 return (expression_t*) call;
1864 static expression_t *parse_conditional_expression(unsigned precedence,
1865 expression_t *expression)
1869 conditional_expression_t *conditional
1870 = allocate_ast_zero(sizeof(conditional[0]));
1871 conditional->condition = expression;
1873 conditional->true_expression = parse_expression();
1875 conditional->false_expression = parse_sub_expression(precedence);
1877 return (expression_t*) conditional;
1880 static expression_t *parse_extension(unsigned precedence)
1882 eat(T___extension__);
1884 /* TODO enable extensions */
1886 return parse_sub_expression(precedence);
1889 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type) \
1891 expression_t *parse_##unexpression_type(unsigned precedence) \
1895 unary_expression_t *unary_expression \
1896 = allocate_ast_zero(sizeof(unary_expression[0])); \
1897 unary_expression->expression.type = EXPR_UNARY; \
1898 unary_expression->type = unexpression_type; \
1899 unary_expression->value = parse_sub_expression(precedence); \
1901 return (expression_t*) unary_expression; \
1904 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE)
1905 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS)
1906 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT)
1907 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE)
1908 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS)
1909 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE)
1910 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT)
1911 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT)
1913 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type) \
1915 expression_t *parse_##unexpression_type(unsigned precedence, \
1916 expression_t *left) \
1918 (void) precedence; \
1921 unary_expression_t *unary_expression \
1922 = allocate_ast_zero(sizeof(unary_expression[0])); \
1923 unary_expression->expression.type = EXPR_UNARY; \
1924 unary_expression->type = unexpression_type; \
1925 unary_expression->value = left; \
1927 return (expression_t*) unary_expression; \
1930 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT)
1931 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT)
1933 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type) \
1935 expression_t *parse_##binexpression_type(unsigned precedence, \
1936 expression_t *left) \
1940 expression_t *right = parse_sub_expression(precedence); \
1942 binary_expression_t *binexpr \
1943 = allocate_ast_zero(sizeof(binexpr[0])); \
1944 binexpr->expression.type = EXPR_BINARY; \
1945 binexpr->type = binexpression_type; \
1946 binexpr->left = left; \
1947 binexpr->right = right; \
1949 return (expression_t*) binexpr; \
1952 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA)
1953 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL)
1954 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV)
1955 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD)
1956 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD)
1957 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB)
1958 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS)
1959 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER)
1960 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN)
1961 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL)
1962 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL)
1963 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL)
1964 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL)
1965 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND)
1966 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR)
1967 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR)
1968 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND)
1969 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR)
1970 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT)
1971 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT)
1972 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN)
1973 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN)
1974 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN)
1975 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN)
1976 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN)
1977 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN)
1978 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN)
1979 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN)
1980 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN)
1981 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN)
1983 static expression_t *parse_sub_expression(unsigned precedence)
1985 if(token.type < 0) {
1986 return expected_expression_error();
1989 expression_parser_function_t *parser
1990 = &expression_parsers[token.type];
1991 source_position_t source_position = token.source_position;
1994 if(parser->parser != NULL) {
1995 left = parser->parser(parser->precedence);
1997 left = parse_primary_expression();
2000 left->source_position = source_position;
2003 if(token.type < 0) {
2004 return expected_expression_error();
2007 parser = &expression_parsers[token.type];
2008 if(parser->infix_parser == NULL)
2010 if(parser->infix_precedence < precedence)
2013 left = parser->infix_parser(parser->infix_precedence, left);
2015 left->source_position = source_position;
2021 static expression_t *parse_expression(void)
2023 return parse_sub_expression(1);
2028 void register_expression_parser(parse_expression_function parser,
2029 int token_type, unsigned precedence)
2031 expression_parser_function_t *entry = &expression_parsers[token_type];
2033 if(entry->parser != NULL) {
2034 fprintf(stderr, "for token ");
2035 print_token_type(stderr, token_type);
2036 fprintf(stderr, "\n");
2037 panic("trying to register multiple expression parsers for a token");
2039 entry->parser = parser;
2040 entry->precedence = precedence;
2043 void register_expression_infix_parser(parse_expression_infix_function parser,
2044 int token_type, unsigned precedence)
2046 expression_parser_function_t *entry = &expression_parsers[token_type];
2048 if(entry->infix_parser != NULL) {
2049 fprintf(stderr, "for token ");
2050 print_token_type(stderr, token_type);
2051 fprintf(stderr, "\n");
2052 panic("trying to register multiple infix expression parsers for a "
2055 entry->infix_parser = parser;
2056 entry->infix_precedence = precedence;
2059 static void init_expression_parsers(void)
2061 memset(&expression_parsers, 0, sizeof(expression_parsers));
2063 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2064 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2065 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2066 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2067 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2068 T_GREATERGREATER, 16);
2069 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2070 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2071 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2072 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2073 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2074 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2075 T_GREATEREQUAL, 14);
2076 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2077 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2078 T_EXCLAMATIONMARKEQUAL, 13);
2079 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2080 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2081 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2082 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2083 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2084 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2085 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2086 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2087 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2088 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2089 T_ASTERISKEQUAL, 2);
2090 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2091 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2093 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2094 T_LESSLESSEQUAL, 2);
2095 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2096 T_GREATERGREATEREQUAL, 2);
2097 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2099 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2101 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2104 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2106 register_expression_infix_parser(parse_array_expression, '[', 30);
2107 register_expression_infix_parser(parse_call_expression, '(', 30);
2108 register_expression_infix_parser(parse_select_expression, '.', 30);
2109 register_expression_infix_parser(parse_select_expression,
2110 T_MINUSGREATER, 30);
2111 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2113 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2116 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2117 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2118 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2119 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2120 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2121 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2122 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2123 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2124 register_expression_parser(parse_sizeof, T_sizeof, 25);
2125 register_expression_parser(parse_extension, T___extension__, 25);
2129 static statement_t *parse_case_statement(void)
2132 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2133 label->statement.type = STATEMENT_CASE_LABEL;
2134 label->statement.source_position = token.source_position;
2136 label->expression = parse_expression();
2139 label->statement.next = parse_statement();
2141 return (statement_t*) label;
2144 static statement_t *parse_default_statement(void)
2148 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2149 label->statement.type = STATEMENT_CASE_LABEL;
2150 label->statement.source_position = token.source_position;
2153 label->statement.next = parse_statement();
2155 return (statement_t*) label;
2158 static statement_t *parse_label_statement(void)
2167 static statement_t *parse_if(void)
2171 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2172 statement->statement.type = STATEMENT_IF;
2173 statement->statement.source_position = token.source_position;
2176 statement->condition = parse_expression();
2179 statement->true_statement = parse_statement();
2180 if(token.type == T_else) {
2182 statement->false_statement = parse_statement();
2185 return (statement_t*) statement;
2188 static statement_t *parse_switch(void)
2192 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2193 statement->statement.type = STATEMENT_SWITCH;
2194 statement->statement.source_position = token.source_position;
2197 statement->expression = parse_expression();
2199 statement->body = parse_statement();
2201 return (statement_t*) statement;
2204 static statement_t *parse_while(void)
2208 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2209 statement->statement.type = STATEMENT_WHILE;
2210 statement->statement.source_position = token.source_position;
2213 statement->condition = parse_expression();
2215 statement->body = parse_statement();
2217 return (statement_t*) statement;
2220 static statement_t *parse_do(void)
2224 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2225 statement->statement.type = STATEMENT_DO_WHILE;
2226 statement->statement.source_position = token.source_position;
2228 statement->body = parse_statement();
2231 statement->condition = parse_expression();
2235 return (statement_t*) statement;
2238 static statement_t *parse_for(void)
2242 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2243 statement->statement.type = STATEMENT_FOR;
2244 statement->statement.source_position = token.source_position;
2248 int top = environment_top();
2249 context_t *last_context = context;
2250 set_context(&statement->context);
2252 if(token.type != ';') {
2253 if(is_declaration_specifier(&token, 0)) {
2254 parse_declaration();
2256 statement->initialisation = parse_expression();
2263 if(token.type != ';') {
2264 statement->condition = parse_expression();
2267 if(token.type != ')') {
2268 statement->step = parse_expression();
2271 statement->body = parse_statement();
2273 assert(context == &statement->context);
2274 set_context(last_context);
2275 environment_pop_to(top);
2277 return (statement_t*) statement;
2280 static statement_t *parse_goto(void)
2283 expect(T_IDENTIFIER);
2289 static statement_t *parse_continue(void)
2294 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2295 statement->source_position = token.source_position;
2296 statement->type = STATEMENT_CONTINUE;
2301 static statement_t *parse_break(void)
2306 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2307 statement->source_position = token.source_position;
2308 statement->type = STATEMENT_BREAK;
2313 static statement_t *parse_return(void)
2317 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2319 statement->statement.type = STATEMENT_RETURN;
2320 statement->statement.source_position = token.source_position;
2321 if(token.type != ';') {
2322 statement->return_value = parse_expression();
2326 return (statement_t*) statement;
2329 static statement_t *parse_declaration_statement(void)
2331 declaration_t *before = last_declaration;
2333 declaration_statement_t *statement
2334 = allocate_ast_zero(sizeof(statement[0]));
2335 statement->statement.type = STATEMENT_DECLARATION;
2336 statement->statement.source_position = token.source_position;
2338 declaration_specifiers_t specifiers;
2339 memset(&specifiers, 0, sizeof(specifiers));
2340 parse_declaration_specifiers(&specifiers);
2342 if(token.type == ';') {
2345 parse_init_declarators(&specifiers);
2348 if(before == NULL) {
2349 statement->declarations_begin = context->declarations;
2351 statement->declarations_begin = before->next;
2353 statement->declarations_end = last_declaration;
2355 return (statement_t*) statement;
2358 static statement_t *parse_expression_statement(void)
2360 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2361 statement->statement.type = STATEMENT_EXPRESSION;
2362 statement->statement.source_position = token.source_position;
2364 statement->expression = parse_expression();
2368 return (statement_t*) statement;
2371 static statement_t *parse_statement(void)
2373 declaration_t *declaration;
2374 statement_t *statement = NULL;
2376 /* declaration or statement */
2377 switch(token.type) {
2379 statement = parse_case_statement();
2383 statement = parse_default_statement();
2387 statement = parse_compound_statement();
2391 statement = parse_if();
2395 statement = parse_switch();
2399 statement = parse_while();
2403 statement = parse_do();
2407 statement = parse_for();
2411 statement = parse_goto();
2415 statement = parse_continue();
2419 statement = parse_break();
2423 statement = parse_return();
2432 if(look_ahead(1)->type == ':') {
2433 statement = parse_label_statement();
2437 declaration = token.v.symbol->declaration;
2438 if(declaration != NULL &&
2439 declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2440 statement = parse_declaration_statement();
2444 statement = parse_expression_statement();
2447 case T___extension__:
2448 /* this can be a prefix to a declaration or an expression statement */
2449 /* we simply eat it now and parse the rest with tail recursion */
2452 } while(token.type == T___extension__);
2453 statement = parse_statement();
2457 statement = parse_declaration_statement();
2461 statement = parse_expression_statement();
2465 assert(statement == NULL || statement->source_position.input_name != NULL);
2470 static statement_t *parse_compound_statement(void)
2474 compound_statement_t *compound_statement
2475 = allocate_ast_zero(sizeof(compound_statement[0]));
2476 compound_statement->statement.type = STATEMENT_COMPOUND;
2477 compound_statement->statement.source_position = token.source_position;
2479 int top = environment_top();
2480 context_t *last_context = context;
2481 set_context(&compound_statement->context);
2483 statement_t *last_statement = NULL;
2485 while(token.type != '}' && token.type != T_EOF) {
2486 statement_t *statement = parse_statement();
2487 if(statement == NULL)
2490 if(last_statement != NULL) {
2491 last_statement->next = statement;
2493 compound_statement->statements = statement;
2496 while(statement->next != NULL)
2497 statement = statement->next;
2499 last_statement = statement;
2502 assert(context == &compound_statement->context);
2503 set_context(last_context);
2504 environment_pop_to(top);
2508 return (statement_t*) compound_statement;
2511 static translation_unit_t *parse_translation_unit(void)
2513 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
2515 assert(context == NULL);
2516 set_context(&unit->context);
2518 while(token.type != T_EOF) {
2519 parse_declaration();
2522 assert(context == &unit->context);
2524 last_declaration = NULL;
2529 translation_unit_t *parse(void)
2531 obstack_init(&environment_obstack);
2532 environment_stack = NEW_ARR_F(environment_entry_t*, 0);
2534 type_set_output(stderr);
2536 lookahead_bufpos = 0;
2537 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
2540 translation_unit_t *unit = parse_translation_unit();
2542 DEL_ARR_F(environment_stack);
2543 obstack_free(&environment_obstack, NULL);
2548 void init_parser(void)
2550 init_expression_parsers();
2551 obstack_init(&temp_obst);
2554 void exit_parser(void)
2556 obstack_free(&temp_obst, NULL);