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
23 declaration_t *old_declaration;
25 unsigned short namespace;
29 static token_t lookahead_buffer[MAX_LOOKAHEAD];
30 static int lookahead_bufpos;
31 static stack_entry_t *environment_stack = NULL;
32 static context_t *global_context = NULL;
33 static context_t *context = NULL;
34 static declaration_t *last_declaration = NULL;
35 static struct obstack temp_obst;
37 static type_t *type_int = NULL;
38 static type_t *type_double = NULL;
39 static type_t *type_const_char = NULL;
40 static type_t *type_string = NULL;
41 static type_t *type_void = NULL;
42 static type_t *type_size_t = NULL;
44 static statement_t *parse_compound_statement(void);
45 static statement_t *parse_statement(void);
47 static expression_t *parse_sub_expression(unsigned precedence);
48 static expression_t *parse_expression(void);
49 static type_t *parse_typename(void);
51 #define STORAGE_CLASSES \
58 #define TYPE_QUALIFIERS \
64 #ifdef PROVIDE_COMPLEX
65 #define COMPLEX_SPECIFIERS \
68 #define COMPLEX_SPECIFIERS
71 #ifdef PROVIDE_IMAGINARY
72 #define IMAGINARY_SPECIFIERS \
75 #define IMAGINARY_SPECIFIERS
78 #define TYPE_SPECIFIERS \
96 #define DECLARATION_START \
101 #define TYPENAME_START \
105 static inline void *allocate_ast_zero(size_t size)
107 void *res = allocate_ast(size);
108 memset(res, 0, size);
112 static inline void *allocate_type_zero(size_t size)
114 void *res = obstack_alloc(type_obst, size);
115 memset(res, 0, size);
119 static inline void free_type(void *type)
121 obstack_free(type_obst, type);
125 * returns the top element of the environment stack
127 static inline size_t environment_top(void)
129 return ARR_LEN(environment_stack);
134 static inline void next_token(void)
136 token = lookahead_buffer[lookahead_bufpos];
137 lookahead_buffer[lookahead_bufpos] = lexer_token;
140 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
143 print_token(stderr, &token);
144 fprintf(stderr, "\n");
148 static inline const token_t *look_ahead(int num)
150 assert(num > 0 && num <= MAX_LOOKAHEAD);
151 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
152 return & lookahead_buffer[pos];
155 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
157 static void error(void)
159 #ifdef ABORT_ON_ERROR
164 static void parser_print_prefix_pos(const source_position_t source_position)
166 fputs(source_position.input_name, stderr);
168 fprintf(stderr, "%d", source_position.linenr);
172 static void parser_print_error_prefix_pos(
173 const source_position_t source_position)
175 parser_print_prefix_pos(source_position);
176 fputs("error: ", stderr);
180 static void parser_print_error_prefix(void)
182 parser_print_prefix_pos(token.source_position);
186 static void parse_error(const char *message)
188 parser_print_error_prefix();
189 fprintf(stderr, "parse error: %s\n", message);
192 __attribute__((unused))
193 static void parse_warning(const char *message)
195 parser_print_prefix_pos(token.source_position);
196 fprintf(stderr, "warning: %s\n", message);
199 static void parse_error_expected(const char *message, ...)
204 if(message != NULL) {
205 parser_print_error_prefix();
206 fprintf(stderr, "%s\n", message);
208 parser_print_error_prefix();
209 fputs("Parse error: got ", stderr);
210 print_token(stderr, &token);
211 fputs(", expected ", stderr);
213 va_start(args, message);
214 token_type_t token_type = va_arg(args, token_type_t);
215 while(token_type != 0) {
219 fprintf(stderr, ", ");
221 print_token_type(stderr, token_type);
222 token_type = va_arg(args, token_type_t);
225 fprintf(stderr, "\n");
228 static void eat_block(void)
230 if(token.type == '{')
233 while(token.type != '}') {
234 if(token.type == T_EOF)
236 if(token.type == '{') {
245 static void eat_statement(void)
247 while(token.type != ';') {
248 if(token.type == T_EOF)
250 if(token.type == '}')
252 if(token.type == '{') {
261 static void eat_brace(void)
263 if(token.type == '(')
266 while(token.type != ')') {
267 if(token.type == T_EOF)
269 if(token.type == ')' || token.type == ';' || token.type == '}') {
272 if(token.type == '(') {
276 if(token.type == '{') {
285 #define expect(expected) \
286 if(UNLIKELY(token.type != (expected))) { \
287 parse_error_expected(NULL, (expected), 0); \
293 #define expect_void(expected) \
294 if(UNLIKELY(token.type != (expected))) { \
295 parse_error_expected(NULL, (expected), 0); \
301 static void set_context(context_t *new_context)
303 context = new_context;
305 last_declaration = new_context->declarations;
306 if(last_declaration != NULL) {
307 while(last_declaration->next != NULL) {
308 last_declaration = last_declaration->next;
314 * called when we find a 2nd declarator for an identifier we already have a
317 static bool is_compatible_declaration (declaration_t *declaration,
318 declaration_t *previous)
320 /* TODO: not correct yet */
321 return declaration->type == previous->type;
324 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
326 declaration_t *declaration = symbol->declaration;
327 for( ; declaration != NULL; declaration = declaration->symbol_next) {
328 if(declaration->namespace == namespace)
335 static const char *get_namespace_prefix(namespace_t namespace)
338 case NAMESPACE_NORMAL:
340 case NAMESPACE_UNION:
342 case NAMESPACE_STRUCT:
347 panic("invalid namespace found");
351 * pushs an environment_entry on the environment stack and links the
352 * corresponding symbol to the new entry
354 static declaration_t *environment_push(declaration_t *declaration)
356 symbol_t *symbol = declaration->symbol;
357 namespace_t namespace = declaration->namespace;
358 assert(declaration->source_position.input_name != NULL);
360 /* a declaration should be only pushed once */
361 assert(declaration->parent_context == NULL);
362 declaration->parent_context = context;
364 declaration_t *previous_declaration = get_declaration(symbol, namespace);
365 assert(declaration != previous_declaration);
366 if(previous_declaration != NULL
367 && previous_declaration->parent_context == context) {
368 if(!is_compatible_declaration(declaration, previous_declaration)) {
369 parser_print_error_prefix_pos(declaration->source_position);
370 fprintf(stderr, "definition of symbol %s%s with type ",
371 get_namespace_prefix(namespace), symbol->string);
373 print_type(declaration->type);
375 parser_print_error_prefix_pos(
376 previous_declaration->source_position);
377 fprintf(stderr, "is incompatible with previous declaration "
379 print_type(previous_declaration->type);
382 return previous_declaration;
385 /* remember old declaration */
387 entry.symbol = symbol;
388 entry.old_declaration = symbol->declaration;
389 entry.namespace = namespace;
390 ARR_APP1(environment_stack, entry);
392 /* replace/add declaration into declaration list of the symbol */
393 if(symbol->declaration == NULL) {
394 symbol->declaration = declaration;
396 declaration_t *iter = symbol->declaration;
397 for( ; iter != NULL; iter = iter->symbol_next) {
398 declaration_t *symbol_next = iter->symbol_next;
399 if(symbol_next == NULL) {
400 iter->symbol_next = declaration;
401 assert(declaration->symbol_next == NULL);
404 if(symbol_next->namespace == namespace) {
405 iter->symbol_next = declaration;
406 declaration->symbol_next = symbol_next->symbol_next;
416 * pops symbols from the environment stack until @p new_top is the top element
418 static void environment_pop_to(size_t new_top)
420 size_t top = ARR_LEN(environment_stack);
423 assert(new_top <= top);
427 for(i = top; i > new_top; --i) {
428 stack_entry_t *entry = & environment_stack[i - 1];
430 declaration_t *old_declaration = entry->old_declaration;
431 symbol_t *symbol = entry->symbol;
432 namespace_t namespace = entry->namespace;
434 /* replace/remove declaration */
435 declaration_t *declaration = symbol->declaration;
436 assert(declaration != NULL);
437 if(declaration->namespace == namespace) {
438 if(old_declaration == NULL) {
439 symbol->declaration = declaration->symbol_next;
441 symbol->declaration = old_declaration;
442 assert(old_declaration->symbol_next ==
443 declaration->symbol_next);
446 for(; declaration != NULL; declaration = declaration->symbol_next) {
447 declaration_t *symbol_next = declaration->symbol_next;
448 if(symbol_next->namespace == namespace) {
449 declaration->symbol_next = old_declaration;
450 assert(old_declaration->symbol_next
451 == symbol_next->symbol_next);
455 assert(declaration != NULL);
459 ARR_SHRINKLEN(environment_stack, (int) new_top);
464 static expression_t *parse_constant_expression(void)
466 /* start parsing at precedence 7 (conditional expression) */
467 return parse_sub_expression(7);
470 static expression_t *parse_assignment_expression(void)
472 /* start parsing at precedence 2 (assignment expression) */
473 return parse_sub_expression(2);
476 static void parse_compound_type_entries(void);
477 static declaration_t *parse_declarator(storage_class_t storage_class,
478 type_t *type, int may_be_abstract);
479 static declaration_t *record_declaration(declaration_t *declaration);
481 typedef struct declaration_specifiers_t declaration_specifiers_t;
482 struct declaration_specifiers_t {
483 storage_class_t storage_class;
487 static const char *parse_string_literals(void)
489 assert(token.type == T_STRING_LITERAL);
490 const char *result = token.v.string;
494 while(token.type == T_STRING_LITERAL) {
495 result = concat_strings(result, token.v.string);
502 static void parse_attributes(void)
506 case T___attribute__:
514 parse_error("EOF while parsing attribute");
532 if(token.type != T_STRING_LITERAL) {
533 parse_error_expected("while parsing assembler attribute",
538 parse_string_literals();
543 goto attributes_finished;
551 static designator_t *parse_designation(void)
553 if(token.type != '[' && token.type != '.')
556 designator_t *result = NULL;
557 designator_t *last = NULL;
560 designator_t *designator;
563 designator = allocate_ast_zero(sizeof(designator[0]));
565 designator->array_access = parse_constant_expression();
569 designator = allocate_ast_zero(sizeof(designator[0]));
571 if(token.type != T_IDENTIFIER) {
572 parse_error_expected("problem while parsing designator",
576 designator->symbol = token.v.symbol;
584 assert(designator != NULL);
586 last->next = designator;
594 static initializer_t *parse_initializer_list(void);
596 static initializer_t *parse_initializer(void)
598 designator_t *designator = parse_designation();
600 initializer_t *result;
601 if(token.type == '{') {
602 result = parse_initializer_list();
604 result = allocate_ast_zero(sizeof(result[0]));
605 result->type = INITIALIZER_VALUE;
606 result->v.value = parse_assignment_expression();
608 result->designator = designator;
613 static initializer_t *parse_initializer_list(void)
617 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
618 result->type = INITIALIZER_LIST;
620 initializer_t *last = NULL;
622 initializer_t *initializer = parse_initializer();
624 last->next = initializer;
626 result->v.list = initializer;
630 if(token.type == '}')
633 if(token.type != ',') {
634 parse_error_expected("problem while parsing initializer list",
641 if(token.type == '}')
650 static declaration_t *parse_compound_type_specifier(bool is_struct)
658 symbol_t *symbol = NULL;
659 declaration_t *declaration = NULL;
661 if(token.type == T_IDENTIFIER) {
662 symbol = token.v.symbol;
666 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
668 declaration = get_declaration(symbol, NAMESPACE_UNION);
670 } else if(token.type != '{') {
672 parse_error_expected("problem while parsing struct type specifier",
673 T_IDENTIFIER, '{', 0);
675 parse_error_expected("problem while parsing union type specifier",
676 T_IDENTIFIER, '{', 0);
682 if(declaration == NULL) {
683 declaration = allocate_type_zero(sizeof(declaration[0]));
686 declaration->namespace = NAMESPACE_STRUCT;
688 declaration->namespace = NAMESPACE_UNION;
690 declaration->source_position = token.source_position;
691 declaration->symbol = symbol;
694 if(token.type == '{') {
695 if(declaration->init.is_defined) {
696 assert(symbol != NULL);
697 parser_print_error_prefix();
698 fprintf(stderr, "multiple definition of %s %s\n",
699 is_struct ? "struct" : "union", symbol->string);
700 declaration->context.declarations = NULL;
702 record_declaration(declaration);
703 declaration->init.is_defined = true;
705 int top = environment_top();
706 context_t *last_context = context;
707 set_context(& declaration->context);
709 parse_compound_type_entries();
712 assert(context == & declaration->context);
713 set_context(last_context);
714 environment_pop_to(top);
720 static void parse_enum_entries(void)
724 if(token.type == '}') {
726 parse_error("empty enum not allowed");
731 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
733 if(token.type != T_IDENTIFIER) {
734 parse_error_expected("problem while parsing enum entry",
739 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
740 entry->symbol = token.v.symbol;
741 entry->source_position = token.source_position;
744 if(token.type == '=') {
746 entry->init.initializer = parse_initializer();
749 record_declaration(entry);
751 if(token.type != ',')
754 } while(token.type != '}');
759 static declaration_t *parse_enum_specifier(void)
763 declaration_t *declaration;
766 if(token.type == T_IDENTIFIER) {
767 symbol = token.v.symbol;
770 declaration = get_declaration(symbol, NAMESPACE_ENUM);
771 } else if(token.type != '{') {
772 parse_error_expected("problem while parsing enum type specifier",
773 T_IDENTIFIER, '{', 0);
780 if(declaration == NULL) {
781 declaration = allocate_type_zero(sizeof(declaration[0]));
783 declaration->namespace = NAMESPACE_ENUM;
784 declaration->source_position = token.source_position;
785 declaration->symbol = symbol;
788 if(token.type == '{') {
789 if(declaration->init.is_defined) {
790 parser_print_error_prefix();
791 fprintf(stderr, "multiple definitions of enum %s\n",
794 record_declaration(declaration);
795 declaration->init.is_defined = 1;
797 parse_enum_entries();
805 * if a symbol is a typedef to another type, return true
807 static bool is_typedef_symbol(symbol_t *symbol)
809 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
810 if(declaration == NULL
811 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
817 static type_t *parse_typeof(void)
825 expression_t *expression = NULL;
829 case T___extension__:
830 /* this can be a prefix to a typename or an expression */
831 /* we simply eat it now. */
834 } while(token.type == T___extension__);
838 if(is_typedef_symbol(token.v.symbol)) {
839 type = parse_typename();
841 expression = parse_expression();
842 type = expression->datatype;
847 type = parse_typename();
851 expression = parse_expression();
852 type = expression->datatype;
858 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
859 typeof->type.type = TYPE_TYPEOF;
860 typeof->expression = expression;
861 typeof->typeof_type = type;
863 return (type_t*) typeof;
867 SPECIFIER_SIGNED = 1 << 0,
868 SPECIFIER_UNSIGNED = 1 << 1,
869 SPECIFIER_LONG = 1 << 2,
870 SPECIFIER_INT = 1 << 3,
871 SPECIFIER_DOUBLE = 1 << 4,
872 SPECIFIER_CHAR = 1 << 5,
873 SPECIFIER_SHORT = 1 << 6,
874 SPECIFIER_LONG_LONG = 1 << 7,
875 SPECIFIER_FLOAT = 1 << 8,
876 SPECIFIER_BOOL = 1 << 9,
877 SPECIFIER_VOID = 1 << 10,
878 #ifdef PROVIDE_COMPLEX
879 SPECIFIER_COMPLEX = 1 << 11,
881 #ifdef PROVIDE_IMAGINARY
882 SPECIFIER_IMAGINARY = 1 << 12,
886 static type_t *create_builtin_type(symbol_t *symbol)
888 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
889 type->type.type = TYPE_BUILTIN;
890 type->symbol = symbol;
892 return (type_t*) type;
895 static type_t *get_typedef_type(symbol_t *symbol)
897 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
898 if(declaration == NULL
899 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
902 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
903 typedef_type->type.type = TYPE_TYPEDEF;
904 typedef_type->declaration = declaration;
906 return (type_t*) typedef_type;
909 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
912 unsigned type_qualifiers = 0;
913 unsigned type_specifiers = 0;
920 #define MATCH_STORAGE_CLASS(token, class) \
922 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
923 parse_error("multiple storage classes in declaration " \
926 specifiers->storage_class = class; \
930 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
931 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
932 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
933 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
934 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
936 /* type qualifiers */
937 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
939 type_qualifiers |= qualifier; \
943 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
944 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
945 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
946 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
948 case T___extension__:
953 /* type specifiers */
954 #define MATCH_SPECIFIER(token, specifier, name) \
957 if(type_specifiers & specifier) { \
958 parse_error("multiple " name " type specifiers given"); \
960 type_specifiers |= specifier; \
964 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
965 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
966 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
967 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
968 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
969 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
970 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
971 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
972 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
973 #ifdef PROVIDE_COMPLEX
974 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
976 #ifdef PROVIDE_IMAGINARY
977 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
981 if(type_specifiers & SPECIFIER_LONG_LONG) {
982 parse_error("multiple type specifiers given");
983 } else if(type_specifiers & SPECIFIER_LONG) {
984 type_specifiers |= SPECIFIER_LONG_LONG;
986 type_specifiers |= SPECIFIER_LONG;
990 /* TODO: if type != NULL for the following rules should issue
993 compound_type_t *compound_type
994 = allocate_type_zero(sizeof(compound_type[0]));
995 compound_type->type.type = TYPE_COMPOUND_STRUCT;
996 compound_type->declaration = parse_compound_type_specifier(true);
998 type = (type_t*) compound_type;
1002 compound_type_t *compound_type
1003 = allocate_type_zero(sizeof(compound_type[0]));
1004 compound_type->type.type = TYPE_COMPOUND_UNION;
1005 compound_type->declaration = parse_compound_type_specifier(false);
1007 type = (type_t*) compound_type;
1011 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1012 enum_type->type.type = TYPE_ENUM;
1013 enum_type->declaration = parse_enum_specifier();
1015 type = (type_t*) enum_type;
1019 type = parse_typeof();
1021 case T___builtin_va_list:
1022 type = create_builtin_type(token.v.symbol);
1026 case T___attribute__:
1031 case T_IDENTIFIER: {
1032 type_t *typedef_type = get_typedef_type(token.v.symbol);
1034 if(typedef_type == NULL)
1035 goto finish_specifiers;
1038 type = typedef_type;
1042 /* function specifier */
1044 goto finish_specifiers;
1051 atomic_type_type_t atomic_type;
1053 /* match valid basic types */
1054 switch(type_specifiers) {
1055 case SPECIFIER_VOID:
1056 atomic_type = ATOMIC_TYPE_VOID;
1058 case SPECIFIER_CHAR:
1059 atomic_type = ATOMIC_TYPE_CHAR;
1061 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1062 atomic_type = ATOMIC_TYPE_SCHAR;
1064 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1065 atomic_type = ATOMIC_TYPE_UCHAR;
1067 case SPECIFIER_SHORT:
1068 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1069 case SPECIFIER_SHORT | SPECIFIER_INT:
1070 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1071 atomic_type = ATOMIC_TYPE_SHORT;
1073 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1074 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1075 atomic_type = ATOMIC_TYPE_USHORT;
1078 case SPECIFIER_SIGNED:
1079 case SPECIFIER_SIGNED | SPECIFIER_INT:
1080 atomic_type = ATOMIC_TYPE_INT;
1082 case SPECIFIER_UNSIGNED:
1083 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1084 atomic_type = ATOMIC_TYPE_UINT;
1086 case SPECIFIER_LONG:
1087 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1088 case SPECIFIER_LONG | SPECIFIER_INT:
1089 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1090 atomic_type = ATOMIC_TYPE_LONG;
1092 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1093 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1094 atomic_type = ATOMIC_TYPE_ULONG;
1096 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1097 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1098 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1099 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1101 atomic_type = ATOMIC_TYPE_LONGLONG;
1103 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1104 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1106 atomic_type = ATOMIC_TYPE_ULONGLONG;
1108 case SPECIFIER_FLOAT:
1109 atomic_type = ATOMIC_TYPE_FLOAT;
1111 case SPECIFIER_DOUBLE:
1112 atomic_type = ATOMIC_TYPE_DOUBLE;
1114 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1115 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1117 case SPECIFIER_BOOL:
1118 atomic_type = ATOMIC_TYPE_BOOL;
1120 #ifdef PROVIDE_COMPLEX
1121 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1122 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1124 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1125 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1127 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1128 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1131 #ifdef PROVIDE_IMAGINARY
1132 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1133 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1135 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1136 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1138 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1139 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1143 /* invalid specifier combination, give an error message */
1144 if(type_specifiers == 0) {
1146 parse_warning("no type specifiers in declaration (using int)");
1147 atomic_type = ATOMIC_TYPE_INT;
1150 parse_error("no type specifiers given in declaration");
1152 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1153 (type_specifiers & SPECIFIER_UNSIGNED)) {
1154 parse_error("signed and unsigned specifiers gives");
1155 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1156 parse_error("only integer types can be signed or unsigned");
1158 parse_error("multiple datatypes in declaration");
1160 atomic_type = ATOMIC_TYPE_INVALID;
1163 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1164 atype->type.type = TYPE_ATOMIC;
1165 atype->atype = atomic_type;
1168 type = (type_t*) atype;
1170 if(type_specifiers != 0) {
1171 parse_error("multiple datatypes in declaration");
1175 type->qualifiers = type_qualifiers;
1177 type_t *result = typehash_insert(type);
1178 if(newtype && result != (type_t*) type) {
1182 specifiers->type = result;
1185 static type_qualifier_t parse_type_qualifiers(void)
1187 type_qualifier_t type_qualifiers = 0;
1190 switch(token.type) {
1191 /* type qualifiers */
1192 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1193 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1194 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1195 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1198 return type_qualifiers;
1203 static void parse_identifier_list(void)
1206 if(token.type != T_IDENTIFIER) {
1207 parse_error_expected("problem while parsing parameter identifier "
1208 "list", T_IDENTIFIER, 0);
1212 if(token.type != ',')
1218 static declaration_t *parse_parameter(void)
1220 declaration_specifiers_t specifiers;
1221 memset(&specifiers, 0, sizeof(specifiers));
1223 parse_declaration_specifiers(&specifiers);
1225 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1226 specifiers.type, 1);
1228 /* TODO check declaration constraints for parameters */
1229 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1230 parse_error("typedef not allowed in parameter list");
1236 static declaration_t *parse_parameters(function_type_t *type)
1238 if(token.type == T_IDENTIFIER) {
1239 symbol_t *symbol = token.v.symbol;
1240 if(!is_typedef_symbol(symbol)) {
1242 parse_identifier_list();
1247 if(token.type == ')') {
1248 type->unspecified_parameters = 1;
1251 if(token.type == T_void && look_ahead(1)->type == ')') {
1256 declaration_t *declarations = NULL;
1257 declaration_t *declaration;
1258 declaration_t *last_declaration = NULL;
1259 function_parameter_t *parameter;
1260 function_parameter_t *last_parameter = NULL;
1263 switch(token.type) {
1267 return declarations;
1270 case T___extension__:
1272 declaration = parse_parameter();
1274 parameter = allocate_type_zero(sizeof(parameter[0]));
1275 parameter->type = declaration->type;
1277 if(last_parameter != NULL) {
1278 last_declaration->next = declaration;
1279 last_parameter->next = parameter;
1281 type->parameters = parameter;
1282 declarations = declaration;
1284 last_parameter = parameter;
1285 last_declaration = declaration;
1289 return declarations;
1291 if(token.type != ',')
1292 return declarations;
1301 } construct_type_type_t;
1303 typedef struct construct_type_t construct_type_t;
1304 struct construct_type_t {
1305 construct_type_type_t type;
1306 construct_type_t *next;
1309 typedef struct parsed_pointer_t parsed_pointer_t;
1310 struct parsed_pointer_t {
1311 construct_type_t construct_type;
1312 type_qualifier_t type_qualifiers;
1315 typedef struct construct_function_type_t construct_function_type_t;
1316 struct construct_function_type_t {
1317 construct_type_t construct_type;
1318 function_type_t *function_type;
1321 typedef struct parsed_array_t parsed_array_t;
1322 struct parsed_array_t {
1323 construct_type_t construct_type;
1324 type_qualifier_t type_qualifiers;
1330 typedef struct construct_base_type_t construct_base_type_t;
1331 struct construct_base_type_t {
1332 construct_type_t construct_type;
1336 static construct_type_t *parse_pointer_declarator(void)
1340 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1341 memset(pointer, 0, sizeof(pointer[0]));
1342 pointer->type_qualifiers = parse_type_qualifiers();
1344 return (construct_type_t*) pointer;
1347 static construct_type_t *parse_array_declarator(void)
1351 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1352 memset(array, 0, sizeof(array[0]));
1354 if(token.type == T_static) {
1355 array->is_static = true;
1359 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1360 if(type_qualifiers != 0) {
1361 if(token.type == T_static) {
1362 array->is_static = true;
1366 array->type_qualifiers = type_qualifiers;
1368 if(token.type == '*' && look_ahead(1)->type == ']') {
1369 array->is_variable = true;
1371 } else if(token.type != ']') {
1372 array->size = parse_assignment_expression();
1377 return (construct_type_t*) array;
1380 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1384 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1385 type->type.type = TYPE_FUNCTION;
1387 declaration_t *parameters = parse_parameters(type);
1388 if(declaration != NULL) {
1389 declaration->context.declarations = parameters;
1392 construct_function_type_t *construct_function_type =
1393 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1394 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1395 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1396 construct_function_type->function_type = type;
1400 return (construct_type_t*) construct_function_type;
1403 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1404 int may_be_abstract)
1406 construct_type_t *result = NULL;
1407 construct_type_t *last = NULL;
1409 while(token.type == '*') {
1410 construct_type_t *type = parse_pointer_declarator();
1419 /* TODO: find out if this is correct */
1422 construct_type_t *inner_types = NULL;
1424 switch(token.type) {
1426 if(declaration == NULL) {
1427 parse_error("no identifier expected in typename");
1429 declaration->symbol = token.v.symbol;
1430 declaration->source_position = token.source_position;
1436 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1442 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1447 construct_type_t *type;
1448 switch(token.type) {
1450 type = parse_function_declarator(declaration);
1453 type = parse_array_declarator();
1456 goto declarator_finished;
1467 declarator_finished:
1470 if(inner_types != NULL) {
1472 last->next = inner_types;
1474 result = inner_types;
1482 static type_t *construct_declarator_type(construct_type_t *construct_list,
1485 construct_type_t *iter = construct_list;
1486 for( ; iter != NULL; iter = iter->next) {
1487 parsed_pointer_t *parsed_pointer;
1488 parsed_array_t *parsed_array;
1489 construct_function_type_t *construct_function_type;
1490 function_type_t *function_type;
1491 pointer_type_t *pointer_type;
1492 array_type_t *array_type;
1494 switch(iter->type) {
1495 case CONSTRUCT_FUNCTION:
1496 construct_function_type = (construct_function_type_t*) iter;
1497 function_type = construct_function_type->function_type;
1499 function_type->result_type = type;
1500 type = (type_t*) function_type;
1503 case CONSTRUCT_POINTER:
1504 parsed_pointer = (parsed_pointer_t*) iter;
1505 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1507 pointer_type->type.type = TYPE_POINTER;
1508 pointer_type->points_to = type;
1509 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1510 type = (type_t*) pointer_type;
1513 case CONSTRUCT_ARRAY:
1514 parsed_array = (parsed_array_t*) iter;
1515 array_type = allocate_type_zero(sizeof(array_type[0]));
1517 array_type->type.type = TYPE_ARRAY;
1518 array_type->element_type = type;
1519 array_type->type.qualifiers = parsed_array->type_qualifiers;
1520 array_type->is_static = parsed_array->is_static;
1521 array_type->is_variable = parsed_array->is_variable;
1522 array_type->size = parsed_array->size;
1523 type = (type_t*) array_type;
1527 type_t *hashed_type = typehash_insert((type_t*) type);
1528 if(hashed_type != type) {
1537 static declaration_t *parse_declarator(storage_class_t storage_class,
1538 type_t *type, int may_be_abstract)
1540 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1541 declaration->storage_class = storage_class;
1543 construct_type_t *construct_type
1544 = parse_inner_declarator(declaration, may_be_abstract);
1545 declaration->type = construct_declarator_type(construct_type, type);
1547 if(construct_type != NULL) {
1548 obstack_free(&temp_obst, construct_type);
1554 static type_t *parse_abstract_declarator(type_t *base_type)
1556 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1558 type_t *result = construct_declarator_type(construct_type, base_type);
1559 if(construct_type != NULL) {
1560 obstack_free(&temp_obst, construct_type);
1566 static declaration_t *record_declaration(declaration_t *declaration)
1568 assert(context != NULL);
1570 symbol_t *symbol = declaration->symbol;
1571 if(symbol != NULL) {
1572 declaration_t *alias = environment_push(declaration);
1573 if(alias != declaration)
1576 declaration->parent_context = context;
1579 if(last_declaration != NULL) {
1580 last_declaration->next = declaration;
1582 context->declarations = declaration;
1584 last_declaration = declaration;
1589 static void parser_error_multiple_definition(declaration_t *previous,
1590 declaration_t *declaration)
1592 parser_print_error_prefix_pos(declaration->source_position);
1593 fprintf(stderr, "multiple definition of symbol '%s'\n",
1594 declaration->symbol->string);
1595 parser_print_error_prefix_pos(previous->source_position);
1596 fprintf(stderr, "this is the location of the previous "
1601 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1604 declaration_t *ndeclaration
1605 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1607 declaration_t *declaration = record_declaration(ndeclaration);
1608 if(token.type == '=') {
1611 /* TODO: check that this is an allowed type (no function type) */
1613 if(declaration->init.initializer != NULL) {
1614 parser_error_multiple_definition(declaration, ndeclaration);
1617 ndeclaration->init.initializer = parse_initializer();
1618 } else if(token.type == '{') {
1619 if(declaration->type->type != TYPE_FUNCTION) {
1620 parser_print_error_prefix();
1621 fprintf(stderr, "Declarator ");
1622 print_type_ext(declaration->type, declaration->symbol, NULL);
1623 fprintf(stderr, " has a body but is not a function type.\n");
1628 if(declaration->init.statement != NULL) {
1629 parser_error_multiple_definition(declaration, ndeclaration);
1631 if(ndeclaration != declaration) {
1632 memcpy(&declaration->context, &ndeclaration->context,
1633 sizeof(declaration->context));
1636 int top = environment_top();
1637 context_t *last_context = context;
1638 set_context(&declaration->context);
1640 /* push function parameters */
1641 declaration_t *parameter = declaration->context.declarations;
1642 for( ; parameter != NULL; parameter = parameter->next) {
1643 environment_push(parameter);
1646 statement_t *statement = parse_compound_statement();
1648 assert(context == &declaration->context);
1649 set_context(last_context);
1650 environment_pop_to(top);
1652 declaration->init.statement = statement;
1656 if(token.type != ',')
1663 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1666 if(token.type == ':') {
1668 parse_constant_expression();
1669 /* TODO (bitfields) */
1671 declaration_t *declaration
1672 = parse_declarator(specifiers->storage_class,
1673 specifiers->type, 1);
1675 /* TODO: check constraints for struct declarations */
1676 /* TODO: check for doubled fields */
1677 record_declaration(declaration);
1679 if(token.type == ':') {
1681 parse_constant_expression();
1682 /* TODO (bitfields) */
1686 if(token.type != ',')
1693 static void parse_compound_type_entries(void)
1697 while(token.type != '}' && token.type != T_EOF) {
1698 declaration_specifiers_t specifiers;
1699 memset(&specifiers, 0, sizeof(specifiers));
1700 parse_declaration_specifiers(&specifiers);
1702 parse_struct_declarators(&specifiers);
1704 if(token.type == T_EOF) {
1705 parse_error("unexpected error while parsing struct");
1710 static void parse_declaration(void)
1712 source_position_t source_position = token.source_position;
1714 declaration_specifiers_t specifiers;
1715 memset(&specifiers, 0, sizeof(specifiers));
1716 parse_declaration_specifiers(&specifiers);
1718 if(token.type == ';') {
1721 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1723 declaration->type = specifiers.type;
1724 declaration->storage_class = specifiers.storage_class;
1725 declaration->source_position = source_position;
1726 record_declaration(declaration);
1729 parse_init_declarators(&specifiers);
1732 static type_t *parse_typename(void)
1734 declaration_specifiers_t specifiers;
1735 memset(&specifiers, 0, sizeof(specifiers));
1736 parse_declaration_specifiers(&specifiers);
1737 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1738 /* TODO: improve error message, user does probably not know what a
1739 * storage class is...
1741 parse_error("typename may not have a storage class");
1744 type_t *result = parse_abstract_declarator(specifiers.type);
1752 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1753 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1754 expression_t *left);
1756 typedef struct expression_parser_function_t expression_parser_function_t;
1757 struct expression_parser_function_t {
1758 unsigned precedence;
1759 parse_expression_function parser;
1760 unsigned infix_precedence;
1761 parse_expression_infix_function infix_parser;
1764 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1766 static expression_t *expected_expression_error(void)
1768 parser_print_error_prefix();
1769 fprintf(stderr, "expected expression, got token ");
1770 print_token(stderr, & token);
1771 fprintf(stderr, "\n");
1773 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1774 expression->type = EXPR_INVALID;
1780 static expression_t *parse_string_const(void)
1782 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1784 cnst->expression.type = EXPR_STRING_LITERAL;
1785 cnst->expression.datatype = type_string;
1786 cnst->value = parse_string_literals();
1788 return (expression_t*) cnst;
1791 static expression_t *parse_int_const(void)
1793 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1795 cnst->expression.type = EXPR_CONST;
1796 cnst->expression.datatype = type_int;
1797 cnst->v.int_value = token.v.intvalue;
1801 return (expression_t*) cnst;
1804 static expression_t *parse_float_const(void)
1806 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1808 cnst->expression.type = EXPR_CONST;
1809 cnst->expression.datatype = type_double;
1810 cnst->v.float_value = token.v.floatvalue;
1814 return (expression_t*) cnst;
1817 static declaration_t *create_implicit_function(symbol_t *symbol,
1818 const source_position_t source_position)
1820 function_type_t *function_type = allocate_type_zero(sizeof(function_type));
1822 function_type->type.type = TYPE_FUNCTION;
1823 function_type->result_type = type_int;
1824 function_type->unspecified_parameters = true;
1826 type_t *type = typehash_insert((type_t*) function_type);
1827 if(type != (type_t*) function_type) {
1828 free_type(function_type);
1831 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1833 declaration->storage_class = STORAGE_CLASS_EXTERN;
1834 declaration->type = type;
1835 declaration->symbol = symbol;
1836 declaration->source_position = source_position;
1838 /* prepend the implicit definition to the global context
1839 * this is safe since the symbol wasn't declared as anything else yet
1841 assert(symbol->declaration == NULL);
1843 context_t *last_context = context;
1844 context = global_context;
1846 environment_push(declaration);
1847 declaration->next = context->declarations;
1848 context->declarations = declaration;
1850 context = last_context;
1855 static expression_t *parse_reference(void)
1857 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
1859 ref->expression.type = EXPR_REFERENCE;
1860 ref->symbol = token.v.symbol;
1862 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
1864 source_position_t source_position = token.source_position;
1867 if(declaration == NULL) {
1869 /* an implicitely defined function */
1870 if(token.type == '(') {
1871 parser_print_prefix_pos(token.source_position);
1872 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
1873 ref->symbol->string);
1875 declaration = create_implicit_function(ref->symbol,
1880 parser_print_error_prefix();
1881 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
1882 return (expression_t*) ref;
1886 ref->declaration = declaration;
1887 ref->expression.datatype = declaration->type;
1889 return (expression_t*) ref;
1892 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
1896 /* TODO check if cast is allowed and issue warnings/errors */
1899 static expression_t *parse_cast(void)
1901 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
1903 cast->expression.type = EXPR_UNARY;
1904 cast->type = UNEXPR_CAST;
1905 cast->expression.source_position = token.source_position;
1907 type_t *type = parse_typename();
1910 expression_t *value = parse_sub_expression(20);
1912 check_cast_allowed(value, type);
1914 cast->expression.datatype = type;
1915 cast->value = value;
1917 return (expression_t*) cast;
1920 static expression_t *parse_statement_expression(void)
1922 statement_expression_t *expression
1923 = allocate_ast_zero(sizeof(expression[0]));
1924 expression->expression.type = EXPR_STATEMENT;
1925 expression->statement = parse_compound_statement();
1927 /* find last statement and use it's type */
1928 const statement_t *last_statement = NULL;
1929 const statement_t *statement = expression->statement;
1930 for( ; statement != NULL; statement = statement->next) {
1931 last_statement = statement;
1934 if(last_statement->type == STATEMENT_EXPRESSION) {
1935 const expression_statement_t *expression_statement =
1936 (const expression_statement_t*) last_statement;
1937 expression->expression.datatype
1938 = expression_statement->expression->datatype;
1940 expression->expression.datatype = type_void;
1945 return (expression_t*) expression;
1948 static expression_t *parse_brace_expression(void)
1952 switch(token.type) {
1954 /* gcc extension: a stement expression */
1955 return parse_statement_expression();
1959 return parse_cast();
1961 if(is_typedef_symbol(token.v.symbol)) {
1962 return parse_cast();
1966 expression_t *result = parse_expression();
1972 static expression_t *parse_function_keyword(void)
1974 eat(T___FUNCTION__);
1977 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1978 expression->expression.type = EXPR_FUNCTION;
1979 expression->expression.datatype = type_string;
1980 expression->value = "TODO: FUNCTION";
1982 return (expression_t*) expression;
1985 static expression_t *parse_pretty_function_keyword(void)
1987 eat(T___PRETTY_FUNCTION__);
1990 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
1991 expression->expression.type = EXPR_PRETTY_FUNCTION;
1992 expression->expression.datatype = type_string;
1993 expression->value = "TODO: PRETTY FUNCTION";
1995 return (expression_t*) expression;
1998 static designator_t *parse_designator(void)
2000 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2002 if(token.type != T_IDENTIFIER) {
2003 parse_error_expected("problem while parsing member designator",
2008 result->symbol = token.v.symbol;
2011 designator_t *last_designator = result;
2013 if(token.type == '.') {
2015 if(token.type != T_IDENTIFIER) {
2016 parse_error_expected("problem while parsing member designator",
2021 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2022 designator->symbol = token.v.symbol;
2025 last_designator->next = designator;
2026 last_designator = designator;
2029 if(token.type == '[') {
2031 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2032 designator->array_access = parse_expression();
2033 if(designator->array_access == NULL) {
2039 last_designator->next = designator;
2040 last_designator = designator;
2049 static expression_t *parse_offsetof(void)
2051 eat(T___builtin_offsetof);
2053 offsetof_expression_t *expression
2054 = allocate_ast_zero(sizeof(expression[0]));
2055 expression->expression.type = EXPR_OFFSETOF;
2056 expression->expression.datatype = type_size_t;
2059 expression->type = parse_typename();
2061 expression->designator = parse_designator();
2064 return (expression_t*) expression;
2067 static expression_t *parse_va_arg(void)
2069 eat(T___builtin_va_arg);
2071 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2072 expression->expression.type = EXPR_VA_ARG;
2075 expression->arg = parse_assignment_expression();
2077 expression->expression.datatype = parse_typename();
2080 return (expression_t*) expression;
2083 static expression_t *parse_builtin_symbol(void)
2085 builtin_symbol_expression_t *expression
2086 = allocate_ast_zero(sizeof(expression[0]));
2087 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2089 /* TODO: set datatype */
2091 expression->symbol = token.v.symbol;
2095 return (expression_t*) expression;
2098 static expression_t *parse_primary_expression(void)
2100 switch(token.type) {
2102 return parse_int_const();
2103 case T_FLOATINGPOINT:
2104 return parse_float_const();
2105 case T_STRING_LITERAL:
2106 return parse_string_const();
2108 return parse_reference();
2109 case T___FUNCTION__:
2110 return parse_function_keyword();
2111 case T___PRETTY_FUNCTION__:
2112 return parse_pretty_function_keyword();
2113 case T___builtin_offsetof:
2114 return parse_offsetof();
2115 case T___builtin_va_arg:
2116 return parse_va_arg();
2117 case T___builtin_expect:
2118 case T___builtin_va_start:
2119 case T___builtin_va_end:
2120 return parse_builtin_symbol();
2123 return parse_brace_expression();
2126 parser_print_error_prefix();
2127 fprintf(stderr, "unexpected token ");
2128 print_token(stderr, &token);
2129 fprintf(stderr, "\n");
2132 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2133 expression->type = EXPR_INVALID;
2134 expression->datatype = type_void;
2139 static expression_t *parse_array_expression(unsigned precedence,
2140 expression_t *array_ref)
2146 array_access_expression_t *array_access
2147 = allocate_ast_zero(sizeof(array_access[0]));
2149 array_access->expression.type = EXPR_ARRAY_ACCESS;
2150 array_access->array_ref = array_ref;
2151 array_access->index = parse_expression();
2153 type_t *array_type = array_ref->datatype;
2154 if(array_type != NULL) {
2155 if(array_type->type == TYPE_POINTER) {
2156 pointer_type_t *pointer = (pointer_type_t*) array_type;
2157 array_access->expression.datatype = pointer->points_to;
2159 parser_print_error_prefix();
2160 fprintf(stderr, "array access on object with non-pointer type ");
2161 print_type(array_type);
2162 fprintf(stderr, "\n");
2166 if(token.type != ']') {
2167 parse_error_expected("Problem while parsing array access", ']', 0);
2168 return (expression_t*) array_access;
2172 return (expression_t*) array_access;
2175 static bool is_declaration_specifier(const token_t *token,
2176 bool only_type_specifiers)
2178 switch(token->type) {
2182 return is_typedef_symbol(token->v.symbol);
2185 if(only_type_specifiers)
2194 static expression_t *parse_sizeof(unsigned precedence)
2198 sizeof_expression_t *sizeof_expression
2199 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2200 sizeof_expression->expression.type = EXPR_SIZEOF;
2201 sizeof_expression->expression.datatype = type_size_t;
2203 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2205 sizeof_expression->type = parse_typename();
2208 expression_t *expression = parse_sub_expression(precedence);
2209 sizeof_expression->type = expression->datatype;
2210 sizeof_expression->size_expression = expression;
2213 return (expression_t*) sizeof_expression;
2216 static expression_t *parse_select_expression(unsigned precedence,
2217 expression_t *compound)
2221 assert(token.type == '.' || token.type == T_MINUSGREATER);
2224 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2226 select->expression.type = EXPR_SELECT;
2227 select->compound = compound;
2229 /* TODO: datatype */
2231 if(token.type != T_IDENTIFIER) {
2232 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2233 return (expression_t*) select;
2235 select->symbol = token.v.symbol;
2238 return (expression_t*) select;
2241 static expression_t *parse_call_expression(unsigned precedence,
2242 expression_t *expression)
2245 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2247 call->expression.type = EXPR_CALL;
2248 call->function = expression;
2250 /* parse arguments */
2253 if(token.type != ')') {
2254 call_argument_t *last_argument = NULL;
2257 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2259 argument->expression = parse_assignment_expression();
2260 if(last_argument == NULL) {
2261 call->arguments = argument;
2263 last_argument->next = argument;
2265 last_argument = argument;
2267 if(token.type != ',')
2274 type_t *type = expression->datatype;
2276 /* we can call pointer to function */
2277 if(type->type == TYPE_POINTER) {
2278 pointer_type_t *pointer = (pointer_type_t*) type;
2279 type = pointer->points_to;
2282 if(type == NULL || type->type != TYPE_FUNCTION) {
2283 parser_print_error_prefix();
2284 fprintf(stderr, "expected a function type for call but found "
2286 print_type(expression->datatype);
2287 fprintf(stderr, "\n");
2289 function_type_t *function_type = (function_type_t*) type;
2290 call->expression.datatype = function_type->result_type;
2294 return (expression_t*) call;
2297 static void type_error(const char *msg, const source_position_t source_position,
2300 parser_print_error_prefix_pos(source_position);
2301 fprintf(stderr, "%s, but found type ", msg);
2303 fputc('\n', stderr);
2307 static void type_error_incompatible(const char *msg,
2308 const source_position_t source_position, type_t *type1, type_t *type2)
2310 parser_print_error_prefix_pos(source_position);
2311 fprintf(stderr, "%s, incompatible types: ", msg);
2313 fprintf(stderr, " - ");
2315 fprintf(stderr, ")\n");
2319 static type_t *get_type_after_conversion(const type_t *type1,
2320 const type_t *type2)
2324 return (type_t*) type1;
2327 static expression_t *parse_conditional_expression(unsigned precedence,
2328 expression_t *expression)
2332 conditional_expression_t *conditional
2333 = allocate_ast_zero(sizeof(conditional[0]));
2334 conditional->expression.type = EXPR_CONDITIONAL;
2335 conditional->condition = expression;
2338 type_t *condition_type = conditional->condition->datatype;
2339 if(condition_type != NULL) {
2340 if(!is_type_scalar(condition_type)) {
2341 type_error("expected a scalar type", expression->source_position,
2346 conditional->true_expression = parse_expression();
2348 conditional->false_expression = parse_sub_expression(precedence);
2350 type_t *true_type = conditional->true_expression->datatype;
2351 if(true_type == NULL)
2352 return (expression_t*) conditional;
2353 type_t *false_type = conditional->false_expression->datatype;
2354 if(false_type == NULL)
2355 return (expression_t*) conditional;
2358 if(true_type == false_type) {
2359 conditional->expression.datatype = true_type;
2360 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2361 type_t *result = get_type_after_conversion(true_type, false_type);
2362 /* TODO: create implicit convs if necessary */
2363 conditional->expression.datatype = result;
2364 } else if(true_type->type == TYPE_POINTER &&
2365 false_type->type == TYPE_POINTER &&
2366 true /* TODO compatible points_to types */) {
2368 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2369 || (is_null_ptr_const(false_type) &&
2370 true_type->type == TYPE_POINTER) TODO*/ false) {
2372 } else if(/* 1 is pointer to object type, other is void* */ false) {
2375 type_error_incompatible("problem while parsing conditional",
2376 expression->source_position, true_type,
2380 return (expression_t*) conditional;
2383 static expression_t *parse_extension(unsigned precedence)
2385 eat(T___extension__);
2387 /* TODO enable extensions */
2389 return parse_sub_expression(precedence);
2392 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2395 return expression->datatype;
2398 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2400 type_t *expression_type = expression->datatype;
2402 if(expression_type->type == TYPE_POINTER) {
2403 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2404 return pointer_type->points_to;
2406 panic("deref TODO...");
2410 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2412 type_t *type = expression->datatype;
2413 return make_pointer_type(type, 0);
2416 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2417 static expression_t *parse_##unexpression_type(unsigned precedence) \
2421 unary_expression_t *unary_expression \
2422 = allocate_ast_zero(sizeof(unary_expression[0])); \
2423 unary_expression->expression.type = EXPR_UNARY; \
2424 unary_expression->type = unexpression_type; \
2425 unary_expression->value = parse_sub_expression(precedence); \
2426 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2428 return (expression_t*) unary_expression; \
2431 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2432 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2433 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2434 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2435 get_unexpr_dereference_type)
2436 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2437 get_unexpr_take_addr_type)
2438 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2439 get_unexpr_arithmetic_type)
2440 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2441 get_unexpr_arithmetic_type)
2442 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2443 get_unexpr_arithmetic_type)
2445 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2447 static expression_t *parse_##unexpression_type(unsigned precedence, \
2448 expression_t *left) \
2450 (void) precedence; \
2453 unary_expression_t *unary_expression \
2454 = allocate_ast_zero(sizeof(unary_expression[0])); \
2455 unary_expression->expression.type = EXPR_UNARY; \
2456 unary_expression->type = unexpression_type; \
2457 unary_expression->value = left; \
2458 unary_expression->expression.datatype = tfunc(left); \
2460 return (expression_t*) unary_expression; \
2463 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2464 get_unexpr_arithmetic_type)
2465 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2466 get_unexpr_arithmetic_type)
2468 static type_t *get_binexpr_int_type(const expression_t *left,
2469 const expression_t *right)
2476 static type_t *get_binexpr_arithmetic_type(const expression_t *left,
2477 const expression_t *right)
2480 return left->datatype;
2483 static type_t *get_binexpr_arithmetic_assign_type(const expression_t *left,
2484 const expression_t *right)
2488 return left->datatype;
2491 static type_t *get_binexpr_right_type(const expression_t *left,
2492 const expression_t *right)
2495 return right->datatype;
2498 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, tfunc) \
2499 static expression_t *parse_##binexpression_type(unsigned precedence, \
2500 expression_t *left) \
2504 expression_t *right = parse_sub_expression(precedence); \
2506 binary_expression_t *binexpr \
2507 = allocate_ast_zero(sizeof(binexpr[0])); \
2508 binexpr->expression.type = EXPR_BINARY; \
2509 binexpr->type = binexpression_type; \
2510 binexpr->left = left; \
2511 binexpr->right = right; \
2512 binexpr->expression.datatype = tfunc(left, right); \
2514 return (expression_t*) binexpr; \
2517 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, get_binexpr_right_type)
2518 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, get_binexpr_arithmetic_type)
2519 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, get_binexpr_arithmetic_type)
2520 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, get_binexpr_arithmetic_type)
2521 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, get_binexpr_arithmetic_type)
2522 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, get_binexpr_arithmetic_type)
2523 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, get_binexpr_int_type)
2524 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, get_binexpr_int_type)
2525 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, get_binexpr_right_type)
2526 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL,
2527 get_binexpr_int_type)
2528 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2529 get_binexpr_int_type)
2530 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL,
2531 get_binexpr_int_type)
2532 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2533 get_binexpr_int_type)
2534 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, get_binexpr_arithmetic_type)
2535 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, get_binexpr_arithmetic_type)
2536 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, get_binexpr_arithmetic_type)
2537 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND,
2538 get_binexpr_int_type)
2539 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR,
2540 get_binexpr_int_type)
2541 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2542 get_binexpr_arithmetic_type)
2543 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2544 get_binexpr_arithmetic_type)
2545 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2546 get_binexpr_arithmetic_assign_type)
2547 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2548 get_binexpr_arithmetic_assign_type)
2549 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2550 get_binexpr_arithmetic_assign_type)
2551 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2552 get_binexpr_arithmetic_assign_type)
2553 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2554 get_binexpr_arithmetic_assign_type)
2555 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2556 get_binexpr_arithmetic_assign_type)
2557 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2558 get_binexpr_arithmetic_assign_type)
2559 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2560 get_binexpr_arithmetic_assign_type)
2561 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2562 get_binexpr_arithmetic_assign_type)
2563 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2564 get_binexpr_arithmetic_assign_type)
2566 static expression_t *parse_sub_expression(unsigned precedence)
2568 if(token.type < 0) {
2569 return expected_expression_error();
2572 expression_parser_function_t *parser
2573 = &expression_parsers[token.type];
2574 source_position_t source_position = token.source_position;
2577 if(parser->parser != NULL) {
2578 left = parser->parser(parser->precedence);
2580 left = parse_primary_expression();
2582 assert(left != NULL);
2583 left->source_position = source_position;
2586 if(token.type < 0) {
2587 return expected_expression_error();
2590 parser = &expression_parsers[token.type];
2591 if(parser->infix_parser == NULL)
2593 if(parser->infix_precedence < precedence)
2596 left = parser->infix_parser(parser->infix_precedence, left);
2598 assert(left != NULL);
2599 assert(left->type != EXPR_INVALID);
2600 left->source_position = source_position;
2606 static expression_t *parse_expression(void)
2608 return parse_sub_expression(1);
2613 static void register_expression_parser(parse_expression_function parser,
2614 int token_type, unsigned precedence)
2616 expression_parser_function_t *entry = &expression_parsers[token_type];
2618 if(entry->parser != NULL) {
2619 fprintf(stderr, "for token ");
2620 print_token_type(stderr, token_type);
2621 fprintf(stderr, "\n");
2622 panic("trying to register multiple expression parsers for a token");
2624 entry->parser = parser;
2625 entry->precedence = precedence;
2628 static void register_expression_infix_parser(
2629 parse_expression_infix_function parser, int token_type,
2630 unsigned precedence)
2632 expression_parser_function_t *entry = &expression_parsers[token_type];
2634 if(entry->infix_parser != NULL) {
2635 fprintf(stderr, "for token ");
2636 print_token_type(stderr, token_type);
2637 fprintf(stderr, "\n");
2638 panic("trying to register multiple infix expression parsers for a "
2641 entry->infix_parser = parser;
2642 entry->infix_precedence = precedence;
2645 static void init_expression_parsers(void)
2647 memset(&expression_parsers, 0, sizeof(expression_parsers));
2649 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2650 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2651 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2652 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2653 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2654 T_GREATERGREATER, 16);
2655 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2656 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2657 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2658 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2659 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2660 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2661 T_GREATEREQUAL, 14);
2662 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2663 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2664 T_EXCLAMATIONMARKEQUAL, 13);
2665 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2666 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2667 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2668 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2669 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2670 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2671 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2672 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2673 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2674 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2675 T_ASTERISKEQUAL, 2);
2676 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2677 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2679 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2680 T_LESSLESSEQUAL, 2);
2681 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2682 T_GREATERGREATEREQUAL, 2);
2683 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
2685 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
2687 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
2690 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
2692 register_expression_infix_parser(parse_array_expression, '[', 30);
2693 register_expression_infix_parser(parse_call_expression, '(', 30);
2694 register_expression_infix_parser(parse_select_expression, '.', 30);
2695 register_expression_infix_parser(parse_select_expression,
2696 T_MINUSGREATER, 30);
2697 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
2699 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
2702 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
2703 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
2704 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
2705 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
2706 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
2707 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
2708 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
2709 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
2710 register_expression_parser(parse_sizeof, T_sizeof, 25);
2711 register_expression_parser(parse_extension, T___extension__, 25);
2715 static statement_t *parse_case_statement(void)
2718 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2719 label->statement.type = STATEMENT_CASE_LABEL;
2720 label->statement.source_position = token.source_position;
2722 label->expression = parse_expression();
2725 label->statement.next = parse_statement();
2727 return (statement_t*) label;
2730 static statement_t *parse_default_statement(void)
2734 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
2735 label->statement.type = STATEMENT_CASE_LABEL;
2736 label->statement.source_position = token.source_position;
2739 label->statement.next = parse_statement();
2741 return (statement_t*) label;
2744 static statement_t *parse_label_statement(void)
2753 static statement_t *parse_if(void)
2757 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2758 statement->statement.type = STATEMENT_IF;
2759 statement->statement.source_position = token.source_position;
2762 statement->condition = parse_expression();
2765 statement->true_statement = parse_statement();
2766 if(token.type == T_else) {
2768 statement->false_statement = parse_statement();
2771 return (statement_t*) statement;
2774 static statement_t *parse_switch(void)
2778 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2779 statement->statement.type = STATEMENT_SWITCH;
2780 statement->statement.source_position = token.source_position;
2783 statement->expression = parse_expression();
2785 statement->body = parse_statement();
2787 return (statement_t*) statement;
2790 static statement_t *parse_while(void)
2794 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2795 statement->statement.type = STATEMENT_WHILE;
2796 statement->statement.source_position = token.source_position;
2799 statement->condition = parse_expression();
2801 statement->body = parse_statement();
2803 return (statement_t*) statement;
2806 static statement_t *parse_do(void)
2810 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2811 statement->statement.type = STATEMENT_DO_WHILE;
2812 statement->statement.source_position = token.source_position;
2814 statement->body = parse_statement();
2817 statement->condition = parse_expression();
2821 return (statement_t*) statement;
2824 static statement_t *parse_for(void)
2828 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2829 statement->statement.type = STATEMENT_FOR;
2830 statement->statement.source_position = token.source_position;
2834 int top = environment_top();
2835 context_t *last_context = context;
2836 set_context(&statement->context);
2838 if(token.type != ';') {
2839 if(is_declaration_specifier(&token, false)) {
2840 parse_declaration();
2842 statement->initialisation = parse_expression();
2849 if(token.type != ';') {
2850 statement->condition = parse_expression();
2853 if(token.type != ')') {
2854 statement->step = parse_expression();
2857 statement->body = parse_statement();
2859 assert(context == &statement->context);
2860 set_context(last_context);
2861 environment_pop_to(top);
2863 return (statement_t*) statement;
2866 static statement_t *parse_goto(void)
2869 expect(T_IDENTIFIER);
2875 static statement_t *parse_continue(void)
2880 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2881 statement->source_position = token.source_position;
2882 statement->type = STATEMENT_CONTINUE;
2887 static statement_t *parse_break(void)
2892 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2893 statement->source_position = token.source_position;
2894 statement->type = STATEMENT_BREAK;
2899 static statement_t *parse_return(void)
2903 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2905 statement->statement.type = STATEMENT_RETURN;
2906 statement->statement.source_position = token.source_position;
2907 if(token.type != ';') {
2908 statement->return_value = parse_expression();
2912 return (statement_t*) statement;
2915 static statement_t *parse_declaration_statement(void)
2917 declaration_t *before = last_declaration;
2919 declaration_statement_t *statement
2920 = allocate_ast_zero(sizeof(statement[0]));
2921 statement->statement.type = STATEMENT_DECLARATION;
2922 statement->statement.source_position = token.source_position;
2924 declaration_specifiers_t specifiers;
2925 memset(&specifiers, 0, sizeof(specifiers));
2926 parse_declaration_specifiers(&specifiers);
2928 if(token.type == ';') {
2931 parse_init_declarators(&specifiers);
2934 if(before == NULL) {
2935 statement->declarations_begin = context->declarations;
2937 statement->declarations_begin = before->next;
2939 statement->declarations_end = last_declaration;
2941 return (statement_t*) statement;
2944 static statement_t *parse_expression_statement(void)
2946 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
2947 statement->statement.type = STATEMENT_EXPRESSION;
2948 statement->statement.source_position = token.source_position;
2950 statement->expression = parse_expression();
2954 return (statement_t*) statement;
2957 static statement_t *parse_statement(void)
2959 statement_t *statement = NULL;
2961 /* declaration or statement */
2962 switch(token.type) {
2964 statement = parse_case_statement();
2968 statement = parse_default_statement();
2972 statement = parse_compound_statement();
2976 statement = parse_if();
2980 statement = parse_switch();
2984 statement = parse_while();
2988 statement = parse_do();
2992 statement = parse_for();
2996 statement = parse_goto();
3000 statement = parse_continue();
3004 statement = parse_break();
3008 statement = parse_return();
3017 if(look_ahead(1)->type == ':') {
3018 statement = parse_label_statement();
3022 if(is_typedef_symbol(token.v.symbol)) {
3023 statement = parse_declaration_statement();
3027 statement = parse_expression_statement();
3030 case T___extension__:
3031 /* this can be a prefix to a declaration or an expression statement */
3032 /* we simply eat it now and parse the rest with tail recursion */
3035 } while(token.type == T___extension__);
3036 statement = parse_statement();
3040 statement = parse_declaration_statement();
3044 statement = parse_expression_statement();
3048 assert(statement == NULL || statement->source_position.input_name != NULL);
3053 static statement_t *parse_compound_statement(void)
3057 compound_statement_t *compound_statement
3058 = allocate_ast_zero(sizeof(compound_statement[0]));
3059 compound_statement->statement.type = STATEMENT_COMPOUND;
3060 compound_statement->statement.source_position = token.source_position;
3062 int top = environment_top();
3063 context_t *last_context = context;
3064 set_context(&compound_statement->context);
3066 statement_t *last_statement = NULL;
3068 while(token.type != '}' && token.type != T_EOF) {
3069 statement_t *statement = parse_statement();
3070 if(statement == NULL)
3073 if(last_statement != NULL) {
3074 last_statement->next = statement;
3076 compound_statement->statements = statement;
3079 while(statement->next != NULL)
3080 statement = statement->next;
3082 last_statement = statement;
3085 assert(context == &compound_statement->context);
3086 set_context(last_context);
3087 environment_pop_to(top);
3091 return (statement_t*) compound_statement;
3094 static translation_unit_t *parse_translation_unit(void)
3096 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3098 assert(global_context == NULL);
3099 global_context = &unit->context;
3101 assert(context == NULL);
3102 set_context(&unit->context);
3104 while(token.type != T_EOF) {
3105 parse_declaration();
3108 assert(context == &unit->context);
3110 last_declaration = NULL;
3112 assert(global_context == &unit->context);
3113 global_context = NULL;
3118 translation_unit_t *parse(void)
3120 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3122 type_set_output(stderr);
3124 lookahead_bufpos = 0;
3125 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3128 translation_unit_t *unit = parse_translation_unit();
3130 DEL_ARR_F(environment_stack);
3135 void init_parser(void)
3137 init_expression_parsers();
3138 obstack_init(&temp_obst);
3140 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3141 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3142 type_size_t = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3143 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3144 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3145 type_string = make_pointer_type(type_const_char, 0);
3148 void exit_parser(void)
3150 obstack_free(&temp_obst, NULL);