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 declaration_t *current_function = NULL;
36 static struct obstack temp_obst;
37 static bool found_error;
39 static type_t *type_int = NULL;
40 static type_t *type_uint = NULL;
41 static type_t *type_long_double = NULL;
42 static type_t *type_double = NULL;
43 static type_t *type_float = NULL;
44 static type_t *type_const_char = NULL;
45 static type_t *type_string = NULL;
46 static type_t *type_void = NULL;
47 static type_t *type_size_t = NULL;
48 static type_t *type_ptrdiff_t = NULL;
50 static statement_t *parse_compound_statement(void);
51 static statement_t *parse_statement(void);
53 static expression_t *parse_sub_expression(unsigned precedence);
54 static expression_t *parse_expression(void);
55 static type_t *parse_typename(void);
57 #define STORAGE_CLASSES \
64 #define TYPE_QUALIFIERS \
70 #ifdef PROVIDE_COMPLEX
71 #define COMPLEX_SPECIFIERS \
74 #define COMPLEX_SPECIFIERS
77 #ifdef PROVIDE_IMAGINARY
78 #define IMAGINARY_SPECIFIERS \
81 #define IMAGINARY_SPECIFIERS
84 #define TYPE_SPECIFIERS \
102 #define DECLARATION_START \
107 #define TYPENAME_START \
111 static inline void *allocate_ast_zero(size_t size)
113 void *res = allocate_ast(size);
114 memset(res, 0, size);
118 static inline void *allocate_type_zero(size_t size)
120 void *res = obstack_alloc(type_obst, size);
121 memset(res, 0, size);
125 static inline void free_type(void *type)
127 obstack_free(type_obst, type);
131 * returns the top element of the environment stack
133 static inline size_t environment_top(void)
135 return ARR_LEN(environment_stack);
140 static inline void next_token(void)
142 token = lookahead_buffer[lookahead_bufpos];
143 lookahead_buffer[lookahead_bufpos] = lexer_token;
146 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
149 print_token(stderr, &token);
150 fprintf(stderr, "\n");
154 static inline const token_t *look_ahead(int num)
156 assert(num > 0 && num <= MAX_LOOKAHEAD);
157 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
158 return & lookahead_buffer[pos];
161 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
163 static void error(void)
166 #ifdef ABORT_ON_ERROR
171 static void parser_print_prefix_pos(const source_position_t source_position)
173 fputs(source_position.input_name, stderr);
175 fprintf(stderr, "%d", source_position.linenr);
179 static void parser_print_error_prefix_pos(
180 const source_position_t source_position)
182 parser_print_prefix_pos(source_position);
183 fputs("error: ", stderr);
187 static void parser_print_error_prefix(void)
189 parser_print_error_prefix_pos(token.source_position);
192 static void parse_error(const char *message)
194 parser_print_error_prefix();
195 fprintf(stderr, "parse error: %s\n", message);
198 static void parse_warning(const char *message)
200 parser_print_prefix_pos(token.source_position);
201 fprintf(stderr, "warning: %s\n", message);
204 static void parse_error_expected(const char *message, ...)
209 if(message != NULL) {
210 parser_print_error_prefix();
211 fprintf(stderr, "%s\n", message);
213 parser_print_error_prefix();
214 fputs("Parse error: got ", stderr);
215 print_token(stderr, &token);
216 fputs(", expected ", stderr);
218 va_start(args, message);
219 token_type_t token_type = va_arg(args, token_type_t);
220 while(token_type != 0) {
224 fprintf(stderr, ", ");
226 print_token_type(stderr, token_type);
227 token_type = va_arg(args, token_type_t);
230 fprintf(stderr, "\n");
233 static void type_error(const char *msg, const source_position_t source_position,
236 parser_print_error_prefix_pos(source_position);
237 fprintf(stderr, "%s, but found type ", msg);
243 static void type_error_incompatible(const char *msg,
244 const source_position_t source_position, type_t *type1, type_t *type2)
246 parser_print_error_prefix_pos(source_position);
247 fprintf(stderr, "%s, incompatible types: ", msg);
249 fprintf(stderr, " - ");
251 fprintf(stderr, ")\n");
255 static void eat_block(void)
257 if(token.type == '{')
260 while(token.type != '}') {
261 if(token.type == T_EOF)
263 if(token.type == '{') {
272 static void eat_statement(void)
274 while(token.type != ';') {
275 if(token.type == T_EOF)
277 if(token.type == '}')
279 if(token.type == '{') {
288 static void eat_brace(void)
290 if(token.type == '(')
293 while(token.type != ')') {
294 if(token.type == T_EOF)
296 if(token.type == ')' || token.type == ';' || token.type == '}') {
299 if(token.type == '(') {
303 if(token.type == '{') {
312 #define expect(expected) \
313 if(UNLIKELY(token.type != (expected))) { \
314 parse_error_expected(NULL, (expected), 0); \
320 #define expect_void(expected) \
321 if(UNLIKELY(token.type != (expected))) { \
322 parse_error_expected(NULL, (expected), 0); \
328 static void set_context(context_t *new_context)
330 context = new_context;
332 last_declaration = new_context->declarations;
333 if(last_declaration != NULL) {
334 while(last_declaration->next != NULL) {
335 last_declaration = last_declaration->next;
341 * called when we find a 2nd declarator for an identifier we already have a
344 static bool is_compatible_declaration (declaration_t *declaration,
345 declaration_t *previous)
347 /* TODO: not correct yet */
348 return declaration->type == previous->type;
351 static declaration_t *get_declaration(symbol_t *symbol, namespace_t namespace)
353 declaration_t *declaration = symbol->declaration;
354 for( ; declaration != NULL; declaration = declaration->symbol_next) {
355 if(declaration->namespace == namespace)
362 static const char *get_namespace_prefix(namespace_t namespace)
365 case NAMESPACE_NORMAL:
367 case NAMESPACE_UNION:
369 case NAMESPACE_STRUCT:
374 panic("invalid namespace found");
378 * pushs an environment_entry on the environment stack and links the
379 * corresponding symbol to the new entry
381 static declaration_t *environment_push(declaration_t *declaration)
383 symbol_t *symbol = declaration->symbol;
384 namespace_t namespace = declaration->namespace;
385 assert(declaration->source_position.input_name != NULL);
387 /* a declaration should be only pushed once */
388 assert(declaration->parent_context == NULL);
389 declaration->parent_context = context;
391 declaration_t *previous_declaration = get_declaration(symbol, namespace);
392 assert(declaration != previous_declaration);
393 if(previous_declaration != NULL
394 && previous_declaration->parent_context == context) {
395 if(!is_compatible_declaration(declaration, previous_declaration)) {
396 parser_print_error_prefix_pos(declaration->source_position);
397 fprintf(stderr, "definition of symbol %s%s with type ",
398 get_namespace_prefix(namespace), symbol->string);
400 print_type(declaration->type);
402 parser_print_error_prefix_pos(
403 previous_declaration->source_position);
404 fprintf(stderr, "is incompatible with previous declaration "
406 print_type(previous_declaration->type);
409 return previous_declaration;
412 /* remember old declaration */
414 entry.symbol = symbol;
415 entry.old_declaration = symbol->declaration;
416 entry.namespace = namespace;
417 ARR_APP1(environment_stack, entry);
419 /* replace/add declaration into declaration list of the symbol */
420 if(symbol->declaration == NULL) {
421 symbol->declaration = declaration;
423 declaration_t *iter = symbol->declaration;
424 for( ; iter != NULL; iter = iter->symbol_next) {
425 declaration_t *symbol_next = iter->symbol_next;
426 if(symbol_next == NULL) {
427 iter->symbol_next = declaration;
428 assert(declaration->symbol_next == NULL);
431 if(symbol_next->namespace == namespace) {
432 iter->symbol_next = declaration;
433 declaration->symbol_next = symbol_next->symbol_next;
443 * pops symbols from the environment stack until @p new_top is the top element
445 static void environment_pop_to(size_t new_top)
447 size_t top = ARR_LEN(environment_stack);
450 assert(new_top <= top);
454 for(i = top; i > new_top; --i) {
455 stack_entry_t *entry = & environment_stack[i - 1];
457 declaration_t *old_declaration = entry->old_declaration;
458 symbol_t *symbol = entry->symbol;
459 namespace_t namespace = entry->namespace;
461 /* replace/remove declaration */
462 declaration_t *declaration = symbol->declaration;
463 assert(declaration != NULL);
464 if(declaration->namespace == namespace) {
465 if(old_declaration == NULL) {
466 symbol->declaration = declaration->symbol_next;
468 symbol->declaration = old_declaration;
469 assert(old_declaration->symbol_next ==
470 declaration->symbol_next);
473 for(; declaration != NULL; declaration = declaration->symbol_next) {
474 declaration_t *symbol_next = declaration->symbol_next;
475 if(symbol_next->namespace == namespace) {
476 declaration->symbol_next = old_declaration;
477 assert(old_declaration->symbol_next
478 == symbol_next->symbol_next);
482 assert(declaration != NULL);
486 ARR_SHRINKLEN(environment_stack, (int) new_top);
490 static int get_rank(const type_t *type)
492 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
493 * and esp. footnote 108). However we can't fold constants (yet), so we
494 * can't decide wether unsigned int is possible, while int always works.
495 * (unsigned int would be preferable when possible... for stuff like
496 * struct { enum { ... } bla : 4; } ) */
497 if(type->type == TYPE_ENUM)
498 return ATOMIC_TYPE_INT;
500 assert(type->type == TYPE_ATOMIC);
501 atomic_type_t *atomic_type = (atomic_type_t*) type;
502 atomic_type_type_t atype = atomic_type->atype;
506 static type_t *promote_integer(type_t *type)
508 if(get_rank(type) < ATOMIC_TYPE_INT)
514 static expression_t *create_cast_expression(expression_t *expression,
517 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
519 cast->expression.type = EXPR_UNARY;
520 cast->type = UNEXPR_CAST;
521 cast->value = expression;
522 cast->expression.datatype = dest_type;
524 return (expression_t*) cast;
527 static expression_t *create_implicit_cast(expression_t *expression,
530 assert(expression->datatype != NULL);
531 type_t *source_type = expression->datatype;
533 source_type = skip_typeref(source_type);
534 dest_type = skip_typeref(dest_type);
536 if(expression->datatype == dest_type)
539 if(dest_type->type == TYPE_ATOMIC) {
540 if(source_type->type != TYPE_ATOMIC)
541 panic("casting of non-atomic types not implemented yet");
543 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
544 type_error_incompatible("can't cast types",
545 expression->source_position,
546 source_type, dest_type);
550 return create_cast_expression(expression, dest_type);
552 if(dest_type->type == TYPE_POINTER) {
553 if(source_type->type == TYPE_POINTER) {
554 if(!pointers_compatible(source_type, dest_type)) {
555 type_error_incompatible("can't implicitely cast types",
556 expression->source_position,
557 source_type, dest_type);
559 return create_cast_expression(expression, dest_type);
564 panic("casting of non-atomic types not implemented yet");
567 static void semantic_assign(type_t *orig_type_left, expression_t **right,
570 type_t *orig_type_right = (*right)->datatype;
571 type_t *type_left = skip_typeref(orig_type_left);
572 type_t *type_right = skip_typeref(orig_type_right);
574 if(type_left == type_right) {
576 } else if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
577 *right = create_implicit_cast(*right, type_left);
578 } else if(type_left->type == TYPE_POINTER
579 && type_right->type == TYPE_POINTER) {
582 /* TODO: improve error message */
583 parser_print_error_prefix();
584 fprintf(stderr, "incompatible types in %s\n", context);
585 parser_print_error_prefix();
586 print_type(type_left);
587 fputs(" <- ", stderr);
588 print_type(type_right);
594 static expression_t *parse_constant_expression(void)
596 /* start parsing at precedence 7 (conditional expression) */
597 return parse_sub_expression(7);
600 static expression_t *parse_assignment_expression(void)
602 /* start parsing at precedence 2 (assignment expression) */
603 return parse_sub_expression(2);
606 static void parse_compound_type_entries(void);
607 static declaration_t *parse_declarator(storage_class_t storage_class,
608 type_t *type, int may_be_abstract);
609 static declaration_t *record_declaration(declaration_t *declaration);
611 typedef struct declaration_specifiers_t declaration_specifiers_t;
612 struct declaration_specifiers_t {
613 storage_class_t storage_class;
617 static const char *parse_string_literals(void)
619 assert(token.type == T_STRING_LITERAL);
620 const char *result = token.v.string;
624 while(token.type == T_STRING_LITERAL) {
625 result = concat_strings(result, token.v.string);
632 static void parse_attributes(void)
636 case T___attribute__:
644 parse_error("EOF while parsing attribute");
662 if(token.type != T_STRING_LITERAL) {
663 parse_error_expected("while parsing assembler attribute",
668 parse_string_literals();
673 goto attributes_finished;
681 static designator_t *parse_designation(void)
683 if(token.type != '[' && token.type != '.')
686 designator_t *result = NULL;
687 designator_t *last = NULL;
690 designator_t *designator;
693 designator = allocate_ast_zero(sizeof(designator[0]));
695 designator->array_access = parse_constant_expression();
699 designator = allocate_ast_zero(sizeof(designator[0]));
701 if(token.type != T_IDENTIFIER) {
702 parse_error_expected("problem while parsing designator",
706 designator->symbol = token.v.symbol;
714 assert(designator != NULL);
716 last->next = designator;
724 static initializer_t *parse_initializer_list(type_t *type);
726 static initializer_t *parse_initializer(type_t *type)
728 designator_t *designator = parse_designation();
730 initializer_t *result;
731 if(token.type == '{') {
732 result = parse_initializer_list(type);
734 result = allocate_ast_zero(sizeof(result[0]));
735 result->type = INITIALIZER_VALUE;
736 result->v.value = parse_assignment_expression();
739 semantic_assign(type, &result->v.value, "initializer");
742 result->designator = designator;
747 static initializer_t *parse_initializer_list(type_t *type)
754 initializer_t *result = allocate_ast_zero(sizeof(result[0]));
755 result->type = INITIALIZER_LIST;
757 initializer_t *last = NULL;
759 initializer_t *initializer = parse_initializer(NULL);
761 last->next = initializer;
763 result->v.list = initializer;
767 if(token.type == '}')
770 if(token.type != ',') {
771 parse_error_expected("problem while parsing initializer list",
778 if(token.type == '}')
787 static declaration_t *parse_compound_type_specifier(bool is_struct)
795 symbol_t *symbol = NULL;
796 declaration_t *declaration = NULL;
798 if(token.type == T_IDENTIFIER) {
799 symbol = token.v.symbol;
803 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
805 declaration = get_declaration(symbol, NAMESPACE_UNION);
807 } else if(token.type != '{') {
809 parse_error_expected("problem while parsing struct type specifier",
810 T_IDENTIFIER, '{', 0);
812 parse_error_expected("problem while parsing union type specifier",
813 T_IDENTIFIER, '{', 0);
819 if(declaration == NULL) {
820 declaration = allocate_type_zero(sizeof(declaration[0]));
823 declaration->namespace = NAMESPACE_STRUCT;
825 declaration->namespace = NAMESPACE_UNION;
827 declaration->source_position = token.source_position;
828 declaration->symbol = symbol;
831 if(token.type == '{') {
832 if(declaration->init.is_defined) {
833 assert(symbol != NULL);
834 parser_print_error_prefix();
835 fprintf(stderr, "multiple definition of %s %s\n",
836 is_struct ? "struct" : "union", symbol->string);
837 declaration->context.declarations = NULL;
839 record_declaration(declaration);
840 declaration->init.is_defined = true;
842 int top = environment_top();
843 context_t *last_context = context;
844 set_context(& declaration->context);
846 parse_compound_type_entries();
849 assert(context == & declaration->context);
850 set_context(last_context);
851 environment_pop_to(top);
857 static void parse_enum_entries(void)
861 if(token.type == '}') {
863 parse_error("empty enum not allowed");
868 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
870 if(token.type != T_IDENTIFIER) {
871 parse_error_expected("problem while parsing enum entry",
876 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
877 entry->symbol = token.v.symbol;
878 entry->source_position = token.source_position;
881 if(token.type == '=') {
883 entry->init.initializer = parse_initializer(type_int);
886 record_declaration(entry);
888 if(token.type != ',')
891 } while(token.type != '}');
896 static declaration_t *parse_enum_specifier(void)
900 declaration_t *declaration;
903 if(token.type == T_IDENTIFIER) {
904 symbol = token.v.symbol;
907 declaration = get_declaration(symbol, NAMESPACE_ENUM);
908 } else if(token.type != '{') {
909 parse_error_expected("problem while parsing enum type specifier",
910 T_IDENTIFIER, '{', 0);
917 if(declaration == NULL) {
918 declaration = allocate_type_zero(sizeof(declaration[0]));
920 declaration->namespace = NAMESPACE_ENUM;
921 declaration->source_position = token.source_position;
922 declaration->symbol = symbol;
925 if(token.type == '{') {
926 if(declaration->init.is_defined) {
927 parser_print_error_prefix();
928 fprintf(stderr, "multiple definitions of enum %s\n",
931 record_declaration(declaration);
932 declaration->init.is_defined = 1;
934 parse_enum_entries();
942 * if a symbol is a typedef to another type, return true
944 static bool is_typedef_symbol(symbol_t *symbol)
946 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
947 if(declaration == NULL
948 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
954 static type_t *parse_typeof(void)
962 expression_t *expression = NULL;
966 case T___extension__:
967 /* this can be a prefix to a typename or an expression */
968 /* we simply eat it now. */
971 } while(token.type == T___extension__);
975 if(is_typedef_symbol(token.v.symbol)) {
976 type = parse_typename();
978 expression = parse_expression();
979 type = expression->datatype;
984 type = parse_typename();
988 expression = parse_expression();
989 type = expression->datatype;
995 typeof_type_t *typeof = allocate_type_zero(sizeof(typeof[0]));
996 typeof->type.type = TYPE_TYPEOF;
997 typeof->expression = expression;
998 typeof->typeof_type = type;
1000 return (type_t*) typeof;
1004 SPECIFIER_SIGNED = 1 << 0,
1005 SPECIFIER_UNSIGNED = 1 << 1,
1006 SPECIFIER_LONG = 1 << 2,
1007 SPECIFIER_INT = 1 << 3,
1008 SPECIFIER_DOUBLE = 1 << 4,
1009 SPECIFIER_CHAR = 1 << 5,
1010 SPECIFIER_SHORT = 1 << 6,
1011 SPECIFIER_LONG_LONG = 1 << 7,
1012 SPECIFIER_FLOAT = 1 << 8,
1013 SPECIFIER_BOOL = 1 << 9,
1014 SPECIFIER_VOID = 1 << 10,
1015 #ifdef PROVIDE_COMPLEX
1016 SPECIFIER_COMPLEX = 1 << 11,
1018 #ifdef PROVIDE_IMAGINARY
1019 SPECIFIER_IMAGINARY = 1 << 12,
1023 static type_t *create_builtin_type(symbol_t *symbol)
1025 builtin_type_t *type = allocate_type_zero(sizeof(type[0]));
1026 type->type.type = TYPE_BUILTIN;
1027 type->symbol = symbol;
1029 type->real_type = type_int;
1031 return (type_t*) type;
1034 static type_t *get_typedef_type(symbol_t *symbol)
1036 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1037 if(declaration == NULL
1038 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1041 typedef_type_t *typedef_type = allocate_type_zero(sizeof(typedef_type[0]));
1042 typedef_type->type.type = TYPE_TYPEDEF;
1043 typedef_type->declaration = declaration;
1045 return (type_t*) typedef_type;
1048 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1050 type_t *type = NULL;
1051 unsigned type_qualifiers = 0;
1052 unsigned type_specifiers = 0;
1056 switch(token.type) {
1059 #define MATCH_STORAGE_CLASS(token, class) \
1061 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1062 parse_error("multiple storage classes in declaration " \
1065 specifiers->storage_class = class; \
1069 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1070 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1071 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1072 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1073 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1075 /* type qualifiers */
1076 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1078 type_qualifiers |= qualifier; \
1082 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1083 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1084 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1085 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1087 case T___extension__:
1092 /* type specifiers */
1093 #define MATCH_SPECIFIER(token, specifier, name) \
1096 if(type_specifiers & specifier) { \
1097 parse_error("multiple " name " type specifiers given"); \
1099 type_specifiers |= specifier; \
1103 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1104 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1105 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1106 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1107 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1108 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1109 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1110 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1111 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1112 #ifdef PROVIDE_COMPLEX
1113 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1115 #ifdef PROVIDE_IMAGINARY
1116 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1120 if(type_specifiers & SPECIFIER_LONG_LONG) {
1121 parse_error("multiple type specifiers given");
1122 } else if(type_specifiers & SPECIFIER_LONG) {
1123 type_specifiers |= SPECIFIER_LONG_LONG;
1125 type_specifiers |= SPECIFIER_LONG;
1129 /* TODO: if type != NULL for the following rules should issue
1132 compound_type_t *compound_type
1133 = allocate_type_zero(sizeof(compound_type[0]));
1134 compound_type->type.type = TYPE_COMPOUND_STRUCT;
1135 compound_type->declaration = parse_compound_type_specifier(true);
1137 type = (type_t*) compound_type;
1141 compound_type_t *compound_type
1142 = allocate_type_zero(sizeof(compound_type[0]));
1143 compound_type->type.type = TYPE_COMPOUND_UNION;
1144 compound_type->declaration = parse_compound_type_specifier(false);
1146 type = (type_t*) compound_type;
1150 enum_type_t *enum_type = allocate_type_zero(sizeof(enum_type[0]));
1151 enum_type->type.type = TYPE_ENUM;
1152 enum_type->declaration = parse_enum_specifier();
1154 type = (type_t*) enum_type;
1158 type = parse_typeof();
1160 case T___builtin_va_list:
1161 type = create_builtin_type(token.v.symbol);
1165 case T___attribute__:
1170 case T_IDENTIFIER: {
1171 type_t *typedef_type = get_typedef_type(token.v.symbol);
1173 if(typedef_type == NULL)
1174 goto finish_specifiers;
1177 type = typedef_type;
1181 /* function specifier */
1183 goto finish_specifiers;
1190 atomic_type_type_t atomic_type;
1192 /* match valid basic types */
1193 switch(type_specifiers) {
1194 case SPECIFIER_VOID:
1195 atomic_type = ATOMIC_TYPE_VOID;
1197 case SPECIFIER_CHAR:
1198 atomic_type = ATOMIC_TYPE_CHAR;
1200 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1201 atomic_type = ATOMIC_TYPE_SCHAR;
1203 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1204 atomic_type = ATOMIC_TYPE_UCHAR;
1206 case SPECIFIER_SHORT:
1207 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1208 case SPECIFIER_SHORT | SPECIFIER_INT:
1209 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1210 atomic_type = ATOMIC_TYPE_SHORT;
1212 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1213 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1214 atomic_type = ATOMIC_TYPE_USHORT;
1217 case SPECIFIER_SIGNED:
1218 case SPECIFIER_SIGNED | SPECIFIER_INT:
1219 atomic_type = ATOMIC_TYPE_INT;
1221 case SPECIFIER_UNSIGNED:
1222 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1223 atomic_type = ATOMIC_TYPE_UINT;
1225 case SPECIFIER_LONG:
1226 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1227 case SPECIFIER_LONG | SPECIFIER_INT:
1228 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1229 atomic_type = ATOMIC_TYPE_LONG;
1231 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1232 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1233 atomic_type = ATOMIC_TYPE_ULONG;
1235 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1236 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1237 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1238 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1240 atomic_type = ATOMIC_TYPE_LONGLONG;
1242 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1243 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1245 atomic_type = ATOMIC_TYPE_ULONGLONG;
1247 case SPECIFIER_FLOAT:
1248 atomic_type = ATOMIC_TYPE_FLOAT;
1250 case SPECIFIER_DOUBLE:
1251 atomic_type = ATOMIC_TYPE_DOUBLE;
1253 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1254 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1256 case SPECIFIER_BOOL:
1257 atomic_type = ATOMIC_TYPE_BOOL;
1259 #ifdef PROVIDE_COMPLEX
1260 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1261 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1263 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1264 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1266 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1267 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1270 #ifdef PROVIDE_IMAGINARY
1271 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1272 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1274 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1275 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1277 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1278 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1282 /* invalid specifier combination, give an error message */
1283 if(type_specifiers == 0) {
1285 parse_warning("no type specifiers in declaration (using int)");
1286 atomic_type = ATOMIC_TYPE_INT;
1289 parse_error("no type specifiers given in declaration");
1291 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1292 (type_specifiers & SPECIFIER_UNSIGNED)) {
1293 parse_error("signed and unsigned specifiers gives");
1294 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1295 parse_error("only integer types can be signed or unsigned");
1297 parse_error("multiple datatypes in declaration");
1299 atomic_type = ATOMIC_TYPE_INVALID;
1302 atomic_type_t *atype = allocate_type_zero(sizeof(atype[0]));
1303 atype->type.type = TYPE_ATOMIC;
1304 atype->atype = atomic_type;
1307 type = (type_t*) atype;
1309 if(type_specifiers != 0) {
1310 parse_error("multiple datatypes in declaration");
1314 type->qualifiers = type_qualifiers;
1316 type_t *result = typehash_insert(type);
1317 if(newtype && result != (type_t*) type) {
1321 specifiers->type = result;
1324 static type_qualifier_t parse_type_qualifiers(void)
1326 type_qualifier_t type_qualifiers = 0;
1329 switch(token.type) {
1330 /* type qualifiers */
1331 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1332 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1333 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1334 MATCH_TYPE_QUALIFIER(T_inline, TYPE_QUALIFIER_INLINE);
1337 return type_qualifiers;
1342 static void parse_identifier_list(void)
1345 if(token.type != T_IDENTIFIER) {
1346 parse_error_expected("problem while parsing parameter identifier "
1347 "list", T_IDENTIFIER, 0);
1351 if(token.type != ',')
1357 static declaration_t *parse_parameter(void)
1359 declaration_specifiers_t specifiers;
1360 memset(&specifiers, 0, sizeof(specifiers));
1362 parse_declaration_specifiers(&specifiers);
1364 declaration_t *declaration = parse_declarator(specifiers.storage_class,
1365 specifiers.type, 1);
1367 /* TODO check declaration constraints for parameters */
1368 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1369 parse_error("typedef not allowed in parameter list");
1375 static declaration_t *parse_parameters(function_type_t *type)
1377 if(token.type == T_IDENTIFIER) {
1378 symbol_t *symbol = token.v.symbol;
1379 if(!is_typedef_symbol(symbol)) {
1381 parse_identifier_list();
1386 if(token.type == ')') {
1387 type->unspecified_parameters = 1;
1390 if(token.type == T_void && look_ahead(1)->type == ')') {
1395 declaration_t *declarations = NULL;
1396 declaration_t *declaration;
1397 declaration_t *last_declaration = NULL;
1398 function_parameter_t *parameter;
1399 function_parameter_t *last_parameter = NULL;
1402 switch(token.type) {
1406 return declarations;
1409 case T___extension__:
1411 declaration = parse_parameter();
1413 parameter = allocate_type_zero(sizeof(parameter[0]));
1414 parameter->type = declaration->type;
1416 if(last_parameter != NULL) {
1417 last_declaration->next = declaration;
1418 last_parameter->next = parameter;
1420 type->parameters = parameter;
1421 declarations = declaration;
1423 last_parameter = parameter;
1424 last_declaration = declaration;
1428 return declarations;
1430 if(token.type != ',')
1431 return declarations;
1440 } construct_type_type_t;
1442 typedef struct construct_type_t construct_type_t;
1443 struct construct_type_t {
1444 construct_type_type_t type;
1445 construct_type_t *next;
1448 typedef struct parsed_pointer_t parsed_pointer_t;
1449 struct parsed_pointer_t {
1450 construct_type_t construct_type;
1451 type_qualifier_t type_qualifiers;
1454 typedef struct construct_function_type_t construct_function_type_t;
1455 struct construct_function_type_t {
1456 construct_type_t construct_type;
1457 function_type_t *function_type;
1460 typedef struct parsed_array_t parsed_array_t;
1461 struct parsed_array_t {
1462 construct_type_t construct_type;
1463 type_qualifier_t type_qualifiers;
1469 typedef struct construct_base_type_t construct_base_type_t;
1470 struct construct_base_type_t {
1471 construct_type_t construct_type;
1475 static construct_type_t *parse_pointer_declarator(void)
1479 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1480 memset(pointer, 0, sizeof(pointer[0]));
1481 pointer->type_qualifiers = parse_type_qualifiers();
1483 return (construct_type_t*) pointer;
1486 static construct_type_t *parse_array_declarator(void)
1490 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1491 memset(array, 0, sizeof(array[0]));
1493 if(token.type == T_static) {
1494 array->is_static = true;
1498 type_qualifier_t type_qualifiers = parse_type_qualifiers();
1499 if(type_qualifiers != 0) {
1500 if(token.type == T_static) {
1501 array->is_static = true;
1505 array->type_qualifiers = type_qualifiers;
1507 if(token.type == '*' && look_ahead(1)->type == ']') {
1508 array->is_variable = true;
1510 } else if(token.type != ']') {
1511 array->size = parse_assignment_expression();
1516 return (construct_type_t*) array;
1519 static construct_type_t *parse_function_declarator(declaration_t *declaration)
1523 function_type_t *type = allocate_type_zero(sizeof(type[0]));
1524 type->type.type = TYPE_FUNCTION;
1526 declaration_t *parameters = parse_parameters(type);
1527 if(declaration != NULL) {
1528 declaration->context.declarations = parameters;
1531 construct_function_type_t *construct_function_type =
1532 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
1533 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
1534 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
1535 construct_function_type->function_type = type;
1539 return (construct_type_t*) construct_function_type;
1542 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
1543 int may_be_abstract)
1545 construct_type_t *result = NULL;
1546 construct_type_t *last = NULL;
1548 while(token.type == '*') {
1549 construct_type_t *type = parse_pointer_declarator();
1558 /* TODO: find out if this is correct */
1561 construct_type_t *inner_types = NULL;
1563 switch(token.type) {
1565 if(declaration == NULL) {
1566 parse_error("no identifier expected in typename");
1568 declaration->symbol = token.v.symbol;
1569 declaration->source_position = token.source_position;
1575 inner_types = parse_inner_declarator(declaration, may_be_abstract);
1581 parse_error_expected("problem while parsing declarator", T_IDENTIFIER,
1586 construct_type_t *type;
1587 switch(token.type) {
1589 type = parse_function_declarator(declaration);
1592 type = parse_array_declarator();
1595 goto declarator_finished;
1606 declarator_finished:
1609 if(inner_types != NULL) {
1611 last->next = inner_types;
1613 result = inner_types;
1621 static type_t *construct_declarator_type(construct_type_t *construct_list,
1624 construct_type_t *iter = construct_list;
1625 for( ; iter != NULL; iter = iter->next) {
1626 parsed_pointer_t *parsed_pointer;
1627 parsed_array_t *parsed_array;
1628 construct_function_type_t *construct_function_type;
1629 function_type_t *function_type;
1630 pointer_type_t *pointer_type;
1631 array_type_t *array_type;
1633 switch(iter->type) {
1634 case CONSTRUCT_FUNCTION:
1635 construct_function_type = (construct_function_type_t*) iter;
1636 function_type = construct_function_type->function_type;
1638 function_type->result_type = type;
1639 type = (type_t*) function_type;
1642 case CONSTRUCT_POINTER:
1643 parsed_pointer = (parsed_pointer_t*) iter;
1644 pointer_type = allocate_type_zero(sizeof(pointer_type[0]));
1646 pointer_type->type.type = TYPE_POINTER;
1647 pointer_type->points_to = type;
1648 pointer_type->type.qualifiers = parsed_pointer->type_qualifiers;
1649 type = (type_t*) pointer_type;
1652 case CONSTRUCT_ARRAY:
1653 parsed_array = (parsed_array_t*) iter;
1654 array_type = allocate_type_zero(sizeof(array_type[0]));
1656 array_type->type.type = TYPE_ARRAY;
1657 array_type->element_type = type;
1658 array_type->type.qualifiers = parsed_array->type_qualifiers;
1659 array_type->is_static = parsed_array->is_static;
1660 array_type->is_variable = parsed_array->is_variable;
1661 array_type->size = parsed_array->size;
1662 type = (type_t*) array_type;
1666 type_t *hashed_type = typehash_insert((type_t*) type);
1667 if(hashed_type != type) {
1676 static declaration_t *parse_declarator(storage_class_t storage_class,
1677 type_t *type, int may_be_abstract)
1679 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1680 declaration->storage_class = storage_class;
1682 construct_type_t *construct_type
1683 = parse_inner_declarator(declaration, may_be_abstract);
1684 declaration->type = construct_declarator_type(construct_type, type);
1686 if(construct_type != NULL) {
1687 obstack_free(&temp_obst, construct_type);
1693 static type_t *parse_abstract_declarator(type_t *base_type)
1695 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
1697 type_t *result = construct_declarator_type(construct_type, base_type);
1698 if(construct_type != NULL) {
1699 obstack_free(&temp_obst, construct_type);
1705 static declaration_t *record_declaration(declaration_t *declaration)
1707 assert(context != NULL);
1709 symbol_t *symbol = declaration->symbol;
1710 if(symbol != NULL) {
1711 declaration_t *alias = environment_push(declaration);
1712 if(alias != declaration)
1715 declaration->parent_context = context;
1718 if(last_declaration != NULL) {
1719 last_declaration->next = declaration;
1721 context->declarations = declaration;
1723 last_declaration = declaration;
1728 static void parser_error_multiple_definition(declaration_t *previous,
1729 declaration_t *declaration)
1731 parser_print_error_prefix_pos(declaration->source_position);
1732 fprintf(stderr, "multiple definition of symbol '%s'\n",
1733 declaration->symbol->string);
1734 parser_print_error_prefix_pos(previous->source_position);
1735 fprintf(stderr, "this is the location of the previous "
1740 static void parse_init_declarators(const declaration_specifiers_t *specifiers)
1743 declaration_t *ndeclaration
1744 = parse_declarator(specifiers->storage_class, specifiers->type, 0);
1746 declaration_t *declaration = record_declaration(ndeclaration);
1747 if(token.type == '=') {
1750 /* TODO: check that this is an allowed type (no function type) */
1752 if(declaration->init.initializer != NULL) {
1753 parser_error_multiple_definition(declaration, ndeclaration);
1756 ndeclaration->init.initializer = parse_initializer(declaration->type);
1757 } else if(token.type == '{') {
1758 if(declaration->type->type != TYPE_FUNCTION) {
1759 parser_print_error_prefix();
1760 fprintf(stderr, "Declarator ");
1761 print_type_ext(declaration->type, declaration->symbol, NULL);
1762 fprintf(stderr, " has a body but is not a function type.\n");
1767 if(declaration->init.statement != NULL) {
1768 parser_error_multiple_definition(declaration, ndeclaration);
1770 if(ndeclaration != declaration) {
1771 memcpy(&declaration->context, &ndeclaration->context,
1772 sizeof(declaration->context));
1775 int top = environment_top();
1776 context_t *last_context = context;
1777 set_context(&declaration->context);
1779 /* push function parameters */
1780 declaration_t *parameter = declaration->context.declarations;
1781 for( ; parameter != NULL; parameter = parameter->next) {
1782 environment_push(parameter);
1784 declaration_t *old_current_function = current_function;
1785 current_function = declaration;
1787 statement_t *statement = parse_compound_statement();
1789 assert(current_function == declaration);
1790 old_current_function = current_function;
1792 assert(context == &declaration->context);
1793 set_context(last_context);
1794 environment_pop_to(top);
1796 declaration->init.statement = statement;
1800 if(token.type != ',')
1807 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
1810 if(token.type == ':') {
1812 parse_constant_expression();
1813 /* TODO (bitfields) */
1815 declaration_t *declaration
1816 = parse_declarator(specifiers->storage_class,
1817 specifiers->type, 1);
1819 /* TODO: check constraints for struct declarations */
1820 /* TODO: check for doubled fields */
1821 record_declaration(declaration);
1823 if(token.type == ':') {
1825 parse_constant_expression();
1826 /* TODO (bitfields) */
1830 if(token.type != ',')
1837 static void parse_compound_type_entries(void)
1841 while(token.type != '}' && token.type != T_EOF) {
1842 declaration_specifiers_t specifiers;
1843 memset(&specifiers, 0, sizeof(specifiers));
1844 parse_declaration_specifiers(&specifiers);
1846 parse_struct_declarators(&specifiers);
1848 if(token.type == T_EOF) {
1849 parse_error("unexpected error while parsing struct");
1854 static void parse_declaration(void)
1856 source_position_t source_position = token.source_position;
1858 declaration_specifiers_t specifiers;
1859 memset(&specifiers, 0, sizeof(specifiers));
1860 parse_declaration_specifiers(&specifiers);
1862 if(token.type == ';') {
1865 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1867 declaration->type = specifiers.type;
1868 declaration->storage_class = specifiers.storage_class;
1869 declaration->source_position = source_position;
1870 record_declaration(declaration);
1873 parse_init_declarators(&specifiers);
1876 static type_t *parse_typename(void)
1878 declaration_specifiers_t specifiers;
1879 memset(&specifiers, 0, sizeof(specifiers));
1880 parse_declaration_specifiers(&specifiers);
1881 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
1882 /* TODO: improve error message, user does probably not know what a
1883 * storage class is...
1885 parse_error("typename may not have a storage class");
1888 type_t *result = parse_abstract_declarator(specifiers.type);
1896 typedef expression_t* (*parse_expression_function) (unsigned precedence);
1897 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
1898 expression_t *left);
1900 typedef struct expression_parser_function_t expression_parser_function_t;
1901 struct expression_parser_function_t {
1902 unsigned precedence;
1903 parse_expression_function parser;
1904 unsigned infix_precedence;
1905 parse_expression_infix_function infix_parser;
1908 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
1910 static expression_t *expected_expression_error(void)
1912 parser_print_error_prefix();
1913 fprintf(stderr, "expected expression, got token ");
1914 print_token(stderr, & token);
1915 fprintf(stderr, "\n");
1917 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
1918 expression->type = EXPR_INVALID;
1924 static expression_t *parse_string_const(void)
1926 string_literal_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1928 cnst->expression.type = EXPR_STRING_LITERAL;
1929 cnst->expression.datatype = type_string;
1930 cnst->value = parse_string_literals();
1932 return (expression_t*) cnst;
1935 static expression_t *parse_int_const(void)
1937 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1939 cnst->expression.type = EXPR_CONST;
1940 cnst->expression.datatype = type_int;
1941 cnst->v.int_value = token.v.intvalue;
1945 return (expression_t*) cnst;
1948 static expression_t *parse_float_const(void)
1950 const_t *cnst = allocate_ast_zero(sizeof(cnst[0]));
1952 cnst->expression.type = EXPR_CONST;
1953 cnst->expression.datatype = type_double;
1954 cnst->v.float_value = token.v.floatvalue;
1958 return (expression_t*) cnst;
1961 static declaration_t *create_implicit_function(symbol_t *symbol,
1962 const source_position_t source_position)
1964 function_type_t *function_type
1965 = allocate_type_zero(sizeof(function_type[0]));
1967 function_type->type.type = TYPE_FUNCTION;
1968 function_type->result_type = type_int;
1969 function_type->unspecified_parameters = true;
1971 type_t *type = typehash_insert((type_t*) function_type);
1972 if(type != (type_t*) function_type) {
1973 free_type(function_type);
1976 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1978 declaration->storage_class = STORAGE_CLASS_EXTERN;
1979 declaration->type = type;
1980 declaration->symbol = symbol;
1981 declaration->source_position = source_position;
1983 /* prepend the implicit definition to the global context
1984 * this is safe since the symbol wasn't declared as anything else yet
1986 assert(symbol->declaration == NULL);
1988 context_t *last_context = context;
1989 context = global_context;
1991 environment_push(declaration);
1992 declaration->next = context->declarations;
1993 context->declarations = declaration;
1995 context = last_context;
2000 static expression_t *parse_reference(void)
2002 reference_expression_t *ref = allocate_ast_zero(sizeof(ref[0]));
2004 ref->expression.type = EXPR_REFERENCE;
2005 ref->symbol = token.v.symbol;
2007 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
2009 source_position_t source_position = token.source_position;
2012 if(declaration == NULL) {
2014 /* an implicitely defined function */
2015 if(token.type == '(') {
2016 parser_print_prefix_pos(token.source_position);
2017 fprintf(stderr, "warning: implicit declaration of function '%s'\n",
2018 ref->symbol->string);
2020 declaration = create_implicit_function(ref->symbol,
2025 parser_print_error_prefix();
2026 fprintf(stderr, "unknown symbol '%s' found.\n", ref->symbol->string);
2027 return (expression_t*) ref;
2031 ref->declaration = declaration;
2032 ref->expression.datatype = declaration->type;
2034 return (expression_t*) ref;
2037 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
2041 /* TODO check if explicit cast is allowed and issue warnings/errors */
2044 static expression_t *parse_cast(void)
2046 unary_expression_t *cast = allocate_ast_zero(sizeof(cast[0]));
2048 cast->expression.type = EXPR_UNARY;
2049 cast->type = UNEXPR_CAST;
2050 cast->expression.source_position = token.source_position;
2052 type_t *type = parse_typename();
2055 expression_t *value = parse_sub_expression(20);
2057 check_cast_allowed(value, type);
2059 cast->expression.datatype = type;
2060 cast->value = value;
2062 return (expression_t*) cast;
2065 static expression_t *parse_statement_expression(void)
2067 statement_expression_t *expression
2068 = allocate_ast_zero(sizeof(expression[0]));
2069 expression->expression.type = EXPR_STATEMENT;
2070 expression->statement = parse_compound_statement();
2072 /* find last statement and use it's type */
2073 const statement_t *last_statement = NULL;
2074 const statement_t *statement = expression->statement;
2075 for( ; statement != NULL; statement = statement->next) {
2076 last_statement = statement;
2079 if(last_statement->type == STATEMENT_EXPRESSION) {
2080 const expression_statement_t *expression_statement =
2081 (const expression_statement_t*) last_statement;
2082 expression->expression.datatype
2083 = expression_statement->expression->datatype;
2085 expression->expression.datatype = type_void;
2090 return (expression_t*) expression;
2093 static expression_t *parse_brace_expression(void)
2097 switch(token.type) {
2099 /* gcc extension: a stement expression */
2100 return parse_statement_expression();
2104 return parse_cast();
2106 if(is_typedef_symbol(token.v.symbol)) {
2107 return parse_cast();
2111 expression_t *result = parse_expression();
2117 static expression_t *parse_function_keyword(void)
2119 eat(T___FUNCTION__);
2122 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2123 expression->expression.type = EXPR_FUNCTION;
2124 expression->expression.datatype = type_string;
2125 expression->value = "TODO: FUNCTION";
2127 return (expression_t*) expression;
2130 static expression_t *parse_pretty_function_keyword(void)
2132 eat(T___PRETTY_FUNCTION__);
2135 string_literal_t *expression = allocate_ast_zero(sizeof(expression[0]));
2136 expression->expression.type = EXPR_PRETTY_FUNCTION;
2137 expression->expression.datatype = type_string;
2138 expression->value = "TODO: PRETTY FUNCTION";
2140 return (expression_t*) expression;
2143 static designator_t *parse_designator(void)
2145 designator_t *result = allocate_ast_zero(sizeof(result[0]));
2147 if(token.type != T_IDENTIFIER) {
2148 parse_error_expected("problem while parsing member designator",
2153 result->symbol = token.v.symbol;
2156 designator_t *last_designator = result;
2158 if(token.type == '.') {
2160 if(token.type != T_IDENTIFIER) {
2161 parse_error_expected("problem while parsing member designator",
2166 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2167 designator->symbol = token.v.symbol;
2170 last_designator->next = designator;
2171 last_designator = designator;
2174 if(token.type == '[') {
2176 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
2177 designator->array_access = parse_expression();
2178 if(designator->array_access == NULL) {
2184 last_designator->next = designator;
2185 last_designator = designator;
2194 static expression_t *parse_offsetof(void)
2196 eat(T___builtin_offsetof);
2198 offsetof_expression_t *expression
2199 = allocate_ast_zero(sizeof(expression[0]));
2200 expression->expression.type = EXPR_OFFSETOF;
2201 expression->expression.datatype = type_size_t;
2204 expression->type = parse_typename();
2206 expression->designator = parse_designator();
2209 return (expression_t*) expression;
2212 static expression_t *parse_va_arg(void)
2214 eat(T___builtin_va_arg);
2216 va_arg_expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2217 expression->expression.type = EXPR_VA_ARG;
2220 expression->arg = parse_assignment_expression();
2222 expression->expression.datatype = parse_typename();
2225 return (expression_t*) expression;
2228 static expression_t *parse_builtin_symbol(void)
2230 builtin_symbol_expression_t *expression
2231 = allocate_ast_zero(sizeof(expression[0]));
2232 expression->expression.type = EXPR_BUILTIN_SYMBOL;
2234 /* TODO: set datatype */
2236 expression->symbol = token.v.symbol;
2240 return (expression_t*) expression;
2243 static expression_t *parse_primary_expression(void)
2245 switch(token.type) {
2247 return parse_int_const();
2248 case T_FLOATINGPOINT:
2249 return parse_float_const();
2250 case T_STRING_LITERAL:
2251 return parse_string_const();
2253 return parse_reference();
2254 case T___FUNCTION__:
2255 return parse_function_keyword();
2256 case T___PRETTY_FUNCTION__:
2257 return parse_pretty_function_keyword();
2258 case T___builtin_offsetof:
2259 return parse_offsetof();
2260 case T___builtin_va_arg:
2261 return parse_va_arg();
2262 case T___builtin_expect:
2263 case T___builtin_va_start:
2264 case T___builtin_va_end:
2265 return parse_builtin_symbol();
2268 return parse_brace_expression();
2271 parser_print_error_prefix();
2272 fprintf(stderr, "unexpected token ");
2273 print_token(stderr, &token);
2274 fprintf(stderr, "\n");
2277 expression_t *expression = allocate_ast_zero(sizeof(expression[0]));
2278 expression->type = EXPR_INVALID;
2279 expression->datatype = type_void;
2284 static expression_t *parse_array_expression(unsigned precedence,
2285 expression_t *array_ref)
2291 array_access_expression_t *array_access
2292 = allocate_ast_zero(sizeof(array_access[0]));
2294 array_access->expression.type = EXPR_ARRAY_ACCESS;
2295 array_access->array_ref = array_ref;
2296 array_access->index = parse_expression();
2298 type_t *array_type = array_ref->datatype;
2299 if(array_type != NULL) {
2300 if(array_type->type == TYPE_POINTER) {
2301 pointer_type_t *pointer = (pointer_type_t*) array_type;
2302 array_access->expression.datatype = pointer->points_to;
2304 parser_print_error_prefix();
2305 fprintf(stderr, "array access on object with non-pointer type ");
2306 print_type(array_type);
2307 fprintf(stderr, "\n");
2311 if(token.type != ']') {
2312 parse_error_expected("Problem while parsing array access", ']', 0);
2313 return (expression_t*) array_access;
2317 return (expression_t*) array_access;
2320 static bool is_declaration_specifier(const token_t *token,
2321 bool only_type_specifiers)
2323 switch(token->type) {
2327 return is_typedef_symbol(token->v.symbol);
2330 if(only_type_specifiers)
2339 static expression_t *parse_sizeof(unsigned precedence)
2343 sizeof_expression_t *sizeof_expression
2344 = allocate_ast_zero(sizeof(sizeof_expression[0]));
2345 sizeof_expression->expression.type = EXPR_SIZEOF;
2346 sizeof_expression->expression.datatype = type_size_t;
2348 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
2350 sizeof_expression->type = parse_typename();
2353 expression_t *expression = parse_sub_expression(precedence);
2354 sizeof_expression->type = expression->datatype;
2355 sizeof_expression->size_expression = expression;
2358 return (expression_t*) sizeof_expression;
2361 static expression_t *parse_select_expression(unsigned precedence,
2362 expression_t *compound)
2366 assert(token.type == '.' || token.type == T_MINUSGREATER);
2369 select_expression_t *select = allocate_ast_zero(sizeof(select[0]));
2371 select->expression.type = EXPR_SELECT;
2372 select->compound = compound;
2374 /* TODO: datatype */
2376 if(token.type != T_IDENTIFIER) {
2377 parse_error_expected("Problem while parsing select", T_IDENTIFIER, 0);
2378 return (expression_t*) select;
2380 select->symbol = token.v.symbol;
2383 return (expression_t*) select;
2386 static expression_t *parse_call_expression(unsigned precedence,
2387 expression_t *expression)
2390 call_expression_t *call = allocate_ast_zero(sizeof(call[0]));
2391 call->expression.type = EXPR_CALL;
2392 call->function = expression;
2394 function_type_t *function_type;
2395 type_t *type = expression->datatype;
2396 if(type->type != TYPE_FUNCTION) {
2397 /* TODO calling pointers to functions is ok */
2398 parser_print_error_prefix();
2399 fputs("called object '", stderr);
2400 print_expression(expression);
2401 fputs("' (type ", stderr);
2403 fputs("is not a function\n", stderr);
2405 function_type = NULL;
2406 call->expression.datatype = NULL;
2408 function_type = (function_type_t*) type;
2409 call->expression.datatype = function_type->result_type;
2412 /* parse arguments */
2415 if(token.type != ')') {
2416 call_argument_t *last_argument = NULL;
2419 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
2421 argument->expression = parse_assignment_expression();
2422 if(last_argument == NULL) {
2423 call->arguments = argument;
2425 last_argument->next = argument;
2427 last_argument = argument;
2429 if(token.type != ',')
2436 if(function_type != NULL) {
2437 function_parameter_t *parameter = function_type->parameters;
2438 call_argument_t *argument = call->arguments;
2439 for( ; parameter != NULL && argument != NULL;
2440 parameter = parameter->next, argument = argument->next) {
2441 type_t *expected_type = parameter->type;
2442 /* TODO report context in error messages */
2443 argument->expression = create_implicit_cast(argument->expression,
2446 /* too few parameters */
2447 if(parameter != NULL) {
2448 parser_print_error_prefix();
2449 fprintf(stderr, "too few arguments to function '");
2450 print_expression(expression);
2451 fprintf(stderr, "'\n");
2452 } else if(argument != NULL) {
2453 /* too many parameters */
2454 if(!function_type->variadic
2455 && !function_type->unspecified_parameters) {
2456 parser_print_error_prefix();
2457 fprintf(stderr, "too many arguments to function '");
2458 print_expression(expression);
2459 fprintf(stderr, "'\n");
2461 /* do default promotion */
2462 for( ; argument != NULL; argument = argument->next) {
2463 type_t *type = argument->expression->datatype;
2465 if(is_type_integer(type)) {
2466 type = promote_integer(type);
2467 } else if(type == type_float) {
2470 argument->expression
2471 = create_implicit_cast(argument->expression, type);
2477 return (expression_t*) call;
2480 static type_t *get_type_after_conversion(const type_t *type1,
2481 const type_t *type2)
2485 return (type_t*) type1;
2488 static expression_t *parse_conditional_expression(unsigned precedence,
2489 expression_t *expression)
2493 conditional_expression_t *conditional
2494 = allocate_ast_zero(sizeof(conditional[0]));
2495 conditional->expression.type = EXPR_CONDITIONAL;
2496 conditional->condition = expression;
2499 type_t *condition_type = conditional->condition->datatype;
2500 if(condition_type != NULL) {
2501 if(!is_type_scalar(condition_type)) {
2502 type_error("expected a scalar type", expression->source_position,
2507 conditional->true_expression = parse_expression();
2509 conditional->false_expression = parse_sub_expression(precedence);
2511 type_t *true_type = conditional->true_expression->datatype;
2512 if(true_type == NULL)
2513 return (expression_t*) conditional;
2514 type_t *false_type = conditional->false_expression->datatype;
2515 if(false_type == NULL)
2516 return (expression_t*) conditional;
2519 if(true_type == false_type) {
2520 conditional->expression.datatype = true_type;
2521 } else if(is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
2522 type_t *result = get_type_after_conversion(true_type, false_type);
2523 /* TODO: create implicit convs if necessary */
2524 conditional->expression.datatype = result;
2525 } else if(true_type->type == TYPE_POINTER &&
2526 false_type->type == TYPE_POINTER &&
2527 true /* TODO compatible points_to types */) {
2529 } else if(/* (is_null_ptr_const(true_type) && false_type->type == TYPE_POINTER)
2530 || (is_null_ptr_const(false_type) &&
2531 true_type->type == TYPE_POINTER) TODO*/ false) {
2533 } else if(/* 1 is pointer to object type, other is void* */ false) {
2536 type_error_incompatible("problem while parsing conditional",
2537 expression->source_position, true_type,
2541 return (expression_t*) conditional;
2544 static expression_t *parse_extension(unsigned precedence)
2546 eat(T___extension__);
2548 /* TODO enable extensions */
2550 return parse_sub_expression(precedence);
2553 static type_t *get_unexpr_arithmetic_type(const expression_t *expression)
2556 return expression->datatype;
2559 static type_t *get_unexpr_dereference_type(const expression_t *expression)
2561 type_t *expression_type = expression->datatype;
2563 if(expression_type->type == TYPE_POINTER) {
2564 pointer_type_t *pointer_type = (pointer_type_t*) expression_type;
2565 return pointer_type->points_to;
2567 panic("deref TODO...");
2571 static type_t *get_unexpr_take_addr_type(const expression_t *expression)
2573 type_t *type = expression->datatype;
2574 return make_pointer_type(type, 0);
2577 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, tfunc) \
2578 static expression_t *parse_##unexpression_type(unsigned precedence) \
2582 unary_expression_t *unary_expression \
2583 = allocate_ast_zero(sizeof(unary_expression[0])); \
2584 unary_expression->expression.type = EXPR_UNARY; \
2585 unary_expression->type = unexpression_type; \
2586 unary_expression->value = parse_sub_expression(precedence); \
2587 unary_expression->expression.datatype = tfunc(unary_expression->value); \
2589 return (expression_t*) unary_expression; \
2592 CREATE_UNARY_EXPRESSION_PARSER('-', UNEXPR_NEGATE, get_unexpr_arithmetic_type)
2593 CREATE_UNARY_EXPRESSION_PARSER('+', UNEXPR_PLUS, get_unexpr_arithmetic_type)
2594 CREATE_UNARY_EXPRESSION_PARSER('!', UNEXPR_NOT, get_unexpr_arithmetic_type)
2595 CREATE_UNARY_EXPRESSION_PARSER('*', UNEXPR_DEREFERENCE,
2596 get_unexpr_dereference_type)
2597 CREATE_UNARY_EXPRESSION_PARSER('&', UNEXPR_TAKE_ADDRESS,
2598 get_unexpr_take_addr_type)
2599 CREATE_UNARY_EXPRESSION_PARSER('~', UNEXPR_BITWISE_NEGATE,
2600 get_unexpr_arithmetic_type)
2601 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_PREFIX_INCREMENT,
2602 get_unexpr_arithmetic_type)
2603 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_PREFIX_DECREMENT,
2604 get_unexpr_arithmetic_type)
2606 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
2608 static expression_t *parse_##unexpression_type(unsigned precedence, \
2609 expression_t *left) \
2611 (void) precedence; \
2614 unary_expression_t *unary_expression \
2615 = allocate_ast_zero(sizeof(unary_expression[0])); \
2616 unary_expression->expression.type = EXPR_UNARY; \
2617 unary_expression->type = unexpression_type; \
2618 unary_expression->value = left; \
2619 unary_expression->expression.datatype = tfunc(left); \
2621 return (expression_t*) unary_expression; \
2624 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS, UNEXPR_POSTFIX_INCREMENT,
2625 get_unexpr_arithmetic_type)
2626 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS, UNEXPR_POSTFIX_DECREMENT,
2627 get_unexpr_arithmetic_type)
2629 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
2631 /* TODO: handle complex + imaginary types */
2633 /* § 6.3.1.8 Usual arithmetic conversions */
2634 if(type_left == type_long_double || type_right == type_long_double) {
2635 return type_long_double;
2636 } else if(type_left == type_double || type_right == type_double) {
2638 } else if(type_left == type_float || type_right == type_float) {
2642 type_right = promote_integer(type_right);
2643 type_left = promote_integer(type_left);
2645 if(type_left == type_right)
2648 bool signed_left = is_type_signed(type_left);
2649 bool signed_right = is_type_signed(type_right);
2650 if(get_rank(type_left) < get_rank(type_right)) {
2651 if(signed_left == signed_right || !signed_right) {
2657 if(signed_left == signed_right || !signed_left) {
2665 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
2667 expression_t *left = expression->left;
2668 expression_t *right = expression->right;
2669 type_t *type_left = skip_typeref(left->datatype);
2670 type_t *type_right = skip_typeref(right->datatype);
2672 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2673 /* TODO: improve error message */
2674 parser_print_error_prefix();
2675 fprintf(stderr, "operation needs arithmetic types\n");
2679 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2680 expression->left = create_implicit_cast(left, arithmetic_type);
2681 expression->right = create_implicit_cast(right, arithmetic_type);
2682 expression->expression.datatype = arithmetic_type;
2685 static void semantic_add(binary_expression_t *expression)
2687 expression_t *left = expression->left;
2688 expression_t *right = expression->right;
2689 type_t *orig_type_left = left->datatype;
2690 type_t *orig_type_right = right->datatype;
2691 type_t *type_left = skip_typeref(orig_type_left);
2692 type_t *type_right = skip_typeref(orig_type_right);
2695 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2696 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2697 expression->left = create_implicit_cast(left, arithmetic_type);
2698 expression->right = create_implicit_cast(right, arithmetic_type);
2699 expression->expression.datatype = arithmetic_type;
2701 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2702 expression->expression.datatype = type_left;
2703 } else if(type_right->type == TYPE_POINTER && is_type_integer(type_left)) {
2704 expression->expression.datatype = type_right;
2706 parser_print_error_prefix();
2707 fprintf(stderr, "invalid operands to binary + (");
2708 print_type(orig_type_left);
2709 fprintf(stderr, ", ");
2710 print_type(orig_type_right);
2711 fprintf(stderr, ")\n");
2715 static void semantic_sub(binary_expression_t *expression)
2717 expression_t *left = expression->left;
2718 expression_t *right = expression->right;
2719 type_t *orig_type_left = left->datatype;
2720 type_t *orig_type_right = right->datatype;
2721 type_t *type_left = skip_typeref(orig_type_left);
2722 type_t *type_right = skip_typeref(orig_type_right);
2725 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2726 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2727 expression->left = create_implicit_cast(left, arithmetic_type);
2728 expression->right = create_implicit_cast(right, arithmetic_type);
2729 expression->expression.datatype = arithmetic_type;
2731 } else if(type_left->type == TYPE_POINTER && is_type_integer(type_right)) {
2732 expression->expression.datatype = type_left;
2733 } else if(type_left->type == TYPE_POINTER &&
2734 type_right->type == TYPE_POINTER) {
2735 if(!pointers_compatible(type_left, type_right)) {
2736 parser_print_error_prefix();
2737 fprintf(stderr, "pointers to incompatible objects to binary - (");
2738 print_type(orig_type_left);
2739 fprintf(stderr, ", ");
2740 print_type(orig_type_right);
2741 fprintf(stderr, ")\n");
2743 expression->expression.datatype = type_ptrdiff_t;
2746 parser_print_error_prefix();
2747 fprintf(stderr, "invalid operands to binary - (");
2748 print_type(orig_type_left);
2749 fprintf(stderr, ", ");
2750 print_type(orig_type_right);
2751 fprintf(stderr, ")\n");
2755 static void semantic_comparison(binary_expression_t *expression)
2757 expression_t *left = expression->left;
2758 expression_t *right = expression->right;
2759 type_t *type_left = left->datatype;
2760 type_t *type_right = right->datatype;
2762 /* TODO non-arithmetic types */
2763 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
2764 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2765 expression->left = create_implicit_cast(left, arithmetic_type);
2766 expression->right = create_implicit_cast(right, arithmetic_type);
2767 expression->expression.datatype = arithmetic_type;
2769 expression->expression.datatype = type_int;
2772 static void semantic_arithmetic_assign(binary_expression_t *expression)
2774 expression_t *left = expression->left;
2775 expression_t *right = expression->right;
2776 type_t *type_left = left->datatype;
2777 type_t *type_right = right->datatype;
2779 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
2780 /* TODO: improve error message */
2781 parser_print_error_prefix();
2782 fprintf(stderr, "operation needs arithmetic types\n");
2786 /* combined instructions are tricky. We can't create an implicit cast on
2787 * the left side, because we need the uncasted form for the store.
2788 * The ast2firm pass has to know that left_type must be right_type
2789 * for the arithmeitc operation and create a cast by itself */
2790 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
2791 expression->right = create_implicit_cast(right, arithmetic_type);
2792 expression->expression.datatype = type_left;
2795 static void semantic_logical_op(binary_expression_t *expression)
2798 expression->expression.datatype = type_int;
2801 static void semantic_binexpr_assign(binary_expression_t *expression)
2803 expression_t *left = expression->left;
2804 type_t *type_left = left->datatype;
2806 semantic_assign(type_left, &expression->right, "assignment");
2808 expression->expression.datatype = type_left;
2811 static void semantic_comma(binary_expression_t *expression)
2813 expression->expression.datatype = expression->right->datatype;
2816 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
2817 static expression_t *parse_##binexpression_type(unsigned precedence, \
2818 expression_t *left) \
2822 expression_t *right = parse_sub_expression(precedence + lr); \
2824 binary_expression_t *binexpr \
2825 = allocate_ast_zero(sizeof(binexpr[0])); \
2826 binexpr->expression.type = EXPR_BINARY; \
2827 binexpr->type = binexpression_type; \
2828 binexpr->left = left; \
2829 binexpr->right = right; \
2832 return (expression_t*) binexpr; \
2835 CREATE_BINEXPR_PARSER(',', BINEXPR_COMMA, semantic_comma, 1)
2836 CREATE_BINEXPR_PARSER('*', BINEXPR_MUL, semantic_binexpr_arithmetic, 1)
2837 CREATE_BINEXPR_PARSER('/', BINEXPR_DIV, semantic_binexpr_arithmetic, 1)
2838 CREATE_BINEXPR_PARSER('%', BINEXPR_MOD, semantic_binexpr_arithmetic, 1)
2839 CREATE_BINEXPR_PARSER('+', BINEXPR_ADD, semantic_add, 1)
2840 CREATE_BINEXPR_PARSER('-', BINEXPR_SUB, semantic_sub, 1)
2841 CREATE_BINEXPR_PARSER('<', BINEXPR_LESS, semantic_comparison, 1)
2842 CREATE_BINEXPR_PARSER('>', BINEXPR_GREATER, semantic_comparison, 1)
2843 CREATE_BINEXPR_PARSER('=', BINEXPR_ASSIGN, semantic_binexpr_assign, 0)
2844 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, BINEXPR_EQUAL, semantic_comparison, 1)
2845 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, BINEXPR_NOTEQUAL,
2846 semantic_comparison, 1)
2847 CREATE_BINEXPR_PARSER(T_LESSEQUAL, BINEXPR_LESSEQUAL, semantic_comparison, 1)
2848 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, BINEXPR_GREATEREQUAL,
2849 semantic_comparison, 1)
2850 CREATE_BINEXPR_PARSER('&', BINEXPR_BITWISE_AND, semantic_binexpr_arithmetic, 1)
2851 CREATE_BINEXPR_PARSER('|', BINEXPR_BITWISE_OR, semantic_binexpr_arithmetic, 1)
2852 CREATE_BINEXPR_PARSER('^', BINEXPR_BITWISE_XOR, semantic_binexpr_arithmetic, 1)
2853 CREATE_BINEXPR_PARSER(T_ANDAND, BINEXPR_LOGICAL_AND, semantic_logical_op, 1)
2854 CREATE_BINEXPR_PARSER(T_PIPEPIPE, BINEXPR_LOGICAL_OR, semantic_logical_op, 1)
2855 /* TODO shift has a bit special semantic */
2856 CREATE_BINEXPR_PARSER(T_LESSLESS, BINEXPR_SHIFTLEFT,
2857 semantic_binexpr_arithmetic, 1)
2858 CREATE_BINEXPR_PARSER(T_GREATERGREATER, BINEXPR_SHIFTRIGHT,
2859 semantic_binexpr_arithmetic, 1)
2860 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, BINEXPR_ADD_ASSIGN,
2861 semantic_arithmetic_assign, 0)
2862 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, BINEXPR_SUB_ASSIGN,
2863 semantic_arithmetic_assign, 0)
2864 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, BINEXPR_MUL_ASSIGN,
2865 semantic_arithmetic_assign, 0)
2866 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, BINEXPR_DIV_ASSIGN,
2867 semantic_arithmetic_assign, 0)
2868 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, BINEXPR_MOD_ASSIGN,
2869 semantic_arithmetic_assign, 0)
2870 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, BINEXPR_SHIFTLEFT_ASSIGN,
2871 semantic_arithmetic_assign, 0)
2872 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, BINEXPR_SHIFTRIGHT_ASSIGN,
2873 semantic_arithmetic_assign, 0)
2874 CREATE_BINEXPR_PARSER(T_ANDEQUAL, BINEXPR_BITWISE_AND_ASSIGN,
2875 semantic_arithmetic_assign, 0)
2876 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, BINEXPR_BITWISE_OR_ASSIGN,
2877 semantic_arithmetic_assign, 0)
2878 CREATE_BINEXPR_PARSER(T_CARETEQUAL, BINEXPR_BITWISE_XOR_ASSIGN,
2879 semantic_arithmetic_assign, 0)
2881 static expression_t *parse_sub_expression(unsigned precedence)
2883 if(token.type < 0) {
2884 return expected_expression_error();
2887 expression_parser_function_t *parser
2888 = &expression_parsers[token.type];
2889 source_position_t source_position = token.source_position;
2892 if(parser->parser != NULL) {
2893 left = parser->parser(parser->precedence);
2895 left = parse_primary_expression();
2897 assert(left != NULL);
2898 left->source_position = source_position;
2901 if(token.type < 0) {
2902 return expected_expression_error();
2905 parser = &expression_parsers[token.type];
2906 if(parser->infix_parser == NULL)
2908 if(parser->infix_precedence < precedence)
2911 left = parser->infix_parser(parser->infix_precedence, left);
2913 assert(left != NULL);
2914 assert(left->type != EXPR_INVALID);
2915 left->source_position = source_position;
2921 static expression_t *parse_expression(void)
2923 return parse_sub_expression(1);
2928 static void register_expression_parser(parse_expression_function parser,
2929 int token_type, unsigned precedence)
2931 expression_parser_function_t *entry = &expression_parsers[token_type];
2933 if(entry->parser != NULL) {
2934 fprintf(stderr, "for token ");
2935 print_token_type(stderr, token_type);
2936 fprintf(stderr, "\n");
2937 panic("trying to register multiple expression parsers for a token");
2939 entry->parser = parser;
2940 entry->precedence = precedence;
2943 static void register_expression_infix_parser(
2944 parse_expression_infix_function parser, int token_type,
2945 unsigned precedence)
2947 expression_parser_function_t *entry = &expression_parsers[token_type];
2949 if(entry->infix_parser != NULL) {
2950 fprintf(stderr, "for token ");
2951 print_token_type(stderr, token_type);
2952 fprintf(stderr, "\n");
2953 panic("trying to register multiple infix expression parsers for a "
2956 entry->infix_parser = parser;
2957 entry->infix_precedence = precedence;
2960 static void init_expression_parsers(void)
2962 memset(&expression_parsers, 0, sizeof(expression_parsers));
2964 register_expression_infix_parser(parse_BINEXPR_MUL, '*', 16);
2965 register_expression_infix_parser(parse_BINEXPR_DIV, '/', 16);
2966 register_expression_infix_parser(parse_BINEXPR_MOD, '%', 16);
2967 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT, T_LESSLESS, 16);
2968 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT,
2969 T_GREATERGREATER, 16);
2970 register_expression_infix_parser(parse_BINEXPR_ADD, '+', 15);
2971 register_expression_infix_parser(parse_BINEXPR_SUB, '-', 15);
2972 register_expression_infix_parser(parse_BINEXPR_LESS, '<', 14);
2973 register_expression_infix_parser(parse_BINEXPR_GREATER, '>', 14);
2974 register_expression_infix_parser(parse_BINEXPR_LESSEQUAL, T_LESSEQUAL, 14);
2975 register_expression_infix_parser(parse_BINEXPR_GREATEREQUAL,
2976 T_GREATEREQUAL, 14);
2977 register_expression_infix_parser(parse_BINEXPR_EQUAL, T_EQUALEQUAL, 13);
2978 register_expression_infix_parser(parse_BINEXPR_NOTEQUAL,
2979 T_EXCLAMATIONMARKEQUAL, 13);
2980 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND, '&', 12);
2981 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR, '^', 11);
2982 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR, '|', 10);
2983 register_expression_infix_parser(parse_BINEXPR_LOGICAL_AND, T_ANDAND, 9);
2984 register_expression_infix_parser(parse_BINEXPR_LOGICAL_OR, T_PIPEPIPE, 8);
2985 register_expression_infix_parser(parse_conditional_expression, '?', 7);
2986 register_expression_infix_parser(parse_BINEXPR_ASSIGN, '=', 2);
2987 register_expression_infix_parser(parse_BINEXPR_ADD_ASSIGN, T_PLUSEQUAL, 2);
2988 register_expression_infix_parser(parse_BINEXPR_SUB_ASSIGN, T_MINUSEQUAL, 2);
2989 register_expression_infix_parser(parse_BINEXPR_MUL_ASSIGN,
2990 T_ASTERISKEQUAL, 2);
2991 register_expression_infix_parser(parse_BINEXPR_DIV_ASSIGN, T_SLASHEQUAL, 2);
2992 register_expression_infix_parser(parse_BINEXPR_MOD_ASSIGN,
2994 register_expression_infix_parser(parse_BINEXPR_SHIFTLEFT_ASSIGN,
2995 T_LESSLESSEQUAL, 2);
2996 register_expression_infix_parser(parse_BINEXPR_SHIFTRIGHT_ASSIGN,
2997 T_GREATERGREATEREQUAL, 2);
2998 register_expression_infix_parser(parse_BINEXPR_BITWISE_AND_ASSIGN,
3000 register_expression_infix_parser(parse_BINEXPR_BITWISE_OR_ASSIGN,
3002 register_expression_infix_parser(parse_BINEXPR_BITWISE_XOR_ASSIGN,
3005 register_expression_infix_parser(parse_BINEXPR_COMMA, ',', 1);
3007 register_expression_infix_parser(parse_array_expression, '[', 30);
3008 register_expression_infix_parser(parse_call_expression, '(', 30);
3009 register_expression_infix_parser(parse_select_expression, '.', 30);
3010 register_expression_infix_parser(parse_select_expression,
3011 T_MINUSGREATER, 30);
3012 register_expression_infix_parser(parse_UNEXPR_POSTFIX_INCREMENT,
3014 register_expression_infix_parser(parse_UNEXPR_POSTFIX_DECREMENT,
3017 register_expression_parser(parse_UNEXPR_NEGATE, '-', 25);
3018 register_expression_parser(parse_UNEXPR_PLUS, '+', 25);
3019 register_expression_parser(parse_UNEXPR_NOT, '!', 25);
3020 register_expression_parser(parse_UNEXPR_BITWISE_NEGATE, '~', 25);
3021 register_expression_parser(parse_UNEXPR_DEREFERENCE, '*', 25);
3022 register_expression_parser(parse_UNEXPR_TAKE_ADDRESS, '&', 25);
3023 register_expression_parser(parse_UNEXPR_PREFIX_INCREMENT, T_PLUSPLUS, 25);
3024 register_expression_parser(parse_UNEXPR_PREFIX_DECREMENT, T_MINUSMINUS, 25);
3025 register_expression_parser(parse_sizeof, T_sizeof, 25);
3026 register_expression_parser(parse_extension, T___extension__, 25);
3030 static statement_t *parse_case_statement(void)
3033 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3034 label->statement.type = STATEMENT_CASE_LABEL;
3035 label->statement.source_position = token.source_position;
3037 label->expression = parse_expression();
3040 label->statement.next = parse_statement();
3042 return (statement_t*) label;
3045 static statement_t *parse_default_statement(void)
3049 case_label_statement_t *label = allocate_ast_zero(sizeof(label[0]));
3050 label->statement.type = STATEMENT_CASE_LABEL;
3051 label->statement.source_position = token.source_position;
3054 label->statement.next = parse_statement();
3056 return (statement_t*) label;
3059 static statement_t *parse_label_statement(void)
3068 static statement_t *parse_if(void)
3072 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3073 statement->statement.type = STATEMENT_IF;
3074 statement->statement.source_position = token.source_position;
3077 statement->condition = parse_expression();
3080 statement->true_statement = parse_statement();
3081 if(token.type == T_else) {
3083 statement->false_statement = parse_statement();
3086 return (statement_t*) statement;
3089 static statement_t *parse_switch(void)
3093 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3094 statement->statement.type = STATEMENT_SWITCH;
3095 statement->statement.source_position = token.source_position;
3098 statement->expression = parse_expression();
3100 statement->body = parse_statement();
3102 return (statement_t*) statement;
3105 static statement_t *parse_while(void)
3109 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3110 statement->statement.type = STATEMENT_WHILE;
3111 statement->statement.source_position = token.source_position;
3114 statement->condition = parse_expression();
3116 statement->body = parse_statement();
3118 return (statement_t*) statement;
3121 static statement_t *parse_do(void)
3125 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3126 statement->statement.type = STATEMENT_DO_WHILE;
3127 statement->statement.source_position = token.source_position;
3129 statement->body = parse_statement();
3132 statement->condition = parse_expression();
3136 return (statement_t*) statement;
3139 static statement_t *parse_for(void)
3143 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3144 statement->statement.type = STATEMENT_FOR;
3145 statement->statement.source_position = token.source_position;
3149 int top = environment_top();
3150 context_t *last_context = context;
3151 set_context(&statement->context);
3153 if(token.type != ';') {
3154 if(is_declaration_specifier(&token, false)) {
3155 parse_declaration();
3157 statement->initialisation = parse_expression();
3164 if(token.type != ';') {
3165 statement->condition = parse_expression();
3168 if(token.type != ')') {
3169 statement->step = parse_expression();
3172 statement->body = parse_statement();
3174 assert(context == &statement->context);
3175 set_context(last_context);
3176 environment_pop_to(top);
3178 return (statement_t*) statement;
3181 static statement_t *parse_goto(void)
3184 expect(T_IDENTIFIER);
3190 static statement_t *parse_continue(void)
3195 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3196 statement->source_position = token.source_position;
3197 statement->type = STATEMENT_CONTINUE;
3202 static statement_t *parse_break(void)
3207 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3208 statement->source_position = token.source_position;
3209 statement->type = STATEMENT_BREAK;
3214 static statement_t *parse_return(void)
3218 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3220 statement->statement.type = STATEMENT_RETURN;
3221 statement->statement.source_position = token.source_position;
3223 assert(current_function->type->type == TYPE_FUNCTION);
3224 function_type_t *function_type = (function_type_t*) current_function->type;
3225 type_t *return_type = function_type->result_type;
3227 expression_t *return_value;
3228 if(token.type != ';') {
3229 return_value = parse_expression();
3231 if(return_type == type_void && return_value->datatype != type_void) {
3232 parse_warning("'return' with a value, in function returning void");
3233 return_value = NULL;
3235 semantic_assign(return_type, &return_value, "'return'");
3238 return_value = NULL;
3239 if(return_type != type_void) {
3240 parse_warning("'return' without value, in function returning "
3244 statement->return_value = return_value;
3248 return (statement_t*) statement;
3251 static statement_t *parse_declaration_statement(void)
3253 declaration_t *before = last_declaration;
3255 declaration_statement_t *statement
3256 = allocate_ast_zero(sizeof(statement[0]));
3257 statement->statement.type = STATEMENT_DECLARATION;
3258 statement->statement.source_position = token.source_position;
3260 declaration_specifiers_t specifiers;
3261 memset(&specifiers, 0, sizeof(specifiers));
3262 parse_declaration_specifiers(&specifiers);
3264 if(token.type == ';') {
3267 parse_init_declarators(&specifiers);
3270 if(before == NULL) {
3271 statement->declarations_begin = context->declarations;
3273 statement->declarations_begin = before->next;
3275 statement->declarations_end = last_declaration;
3277 return (statement_t*) statement;
3280 static statement_t *parse_expression_statement(void)
3282 expression_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
3283 statement->statement.type = STATEMENT_EXPRESSION;
3284 statement->statement.source_position = token.source_position;
3286 statement->expression = parse_expression();
3290 return (statement_t*) statement;
3293 static statement_t *parse_statement(void)
3295 statement_t *statement = NULL;
3297 /* declaration or statement */
3298 switch(token.type) {
3300 statement = parse_case_statement();
3304 statement = parse_default_statement();
3308 statement = parse_compound_statement();
3312 statement = parse_if();
3316 statement = parse_switch();
3320 statement = parse_while();
3324 statement = parse_do();
3328 statement = parse_for();
3332 statement = parse_goto();
3336 statement = parse_continue();
3340 statement = parse_break();
3344 statement = parse_return();
3353 if(look_ahead(1)->type == ':') {
3354 statement = parse_label_statement();
3358 if(is_typedef_symbol(token.v.symbol)) {
3359 statement = parse_declaration_statement();
3363 statement = parse_expression_statement();
3366 case T___extension__:
3367 /* this can be a prefix to a declaration or an expression statement */
3368 /* we simply eat it now and parse the rest with tail recursion */
3371 } while(token.type == T___extension__);
3372 statement = parse_statement();
3376 statement = parse_declaration_statement();
3380 statement = parse_expression_statement();
3384 assert(statement == NULL || statement->source_position.input_name != NULL);
3389 static statement_t *parse_compound_statement(void)
3393 compound_statement_t *compound_statement
3394 = allocate_ast_zero(sizeof(compound_statement[0]));
3395 compound_statement->statement.type = STATEMENT_COMPOUND;
3396 compound_statement->statement.source_position = token.source_position;
3398 int top = environment_top();
3399 context_t *last_context = context;
3400 set_context(&compound_statement->context);
3402 statement_t *last_statement = NULL;
3404 while(token.type != '}' && token.type != T_EOF) {
3405 statement_t *statement = parse_statement();
3406 if(statement == NULL)
3409 if(last_statement != NULL) {
3410 last_statement->next = statement;
3412 compound_statement->statements = statement;
3415 while(statement->next != NULL)
3416 statement = statement->next;
3418 last_statement = statement;
3421 assert(context == &compound_statement->context);
3422 set_context(last_context);
3423 environment_pop_to(top);
3427 return (statement_t*) compound_statement;
3430 static translation_unit_t *parse_translation_unit(void)
3432 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
3434 assert(global_context == NULL);
3435 global_context = &unit->context;
3437 assert(context == NULL);
3438 set_context(&unit->context);
3440 while(token.type != T_EOF) {
3441 parse_declaration();
3444 assert(context == &unit->context);
3446 last_declaration = NULL;
3448 assert(global_context == &unit->context);
3449 global_context = NULL;
3454 translation_unit_t *parse(void)
3456 environment_stack = NEW_ARR_F(stack_entry_t, 0);
3457 found_error = false;
3459 type_set_output(stderr);
3460 ast_set_output(stderr);
3462 lookahead_bufpos = 0;
3463 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
3466 translation_unit_t *unit = parse_translation_unit();
3468 DEL_ARR_F(environment_stack);
3476 void init_parser(void)
3478 init_expression_parsers();
3479 obstack_init(&temp_obst);
3481 type_int = make_atomic_type(ATOMIC_TYPE_INT, 0);
3482 type_uint = make_atomic_type(ATOMIC_TYPE_UINT, 0);
3483 type_long_double = make_atomic_type(ATOMIC_TYPE_LONG_DOUBLE, 0);
3484 type_double = make_atomic_type(ATOMIC_TYPE_DOUBLE, 0);
3485 type_float = make_atomic_type(ATOMIC_TYPE_FLOAT, 0);
3486 type_size_t = make_atomic_type(ATOMIC_TYPE_ULONG, 0);
3487 type_ptrdiff_t = make_atomic_type(ATOMIC_TYPE_LONG, 0);
3488 type_const_char = make_atomic_type(ATOMIC_TYPE_CHAR, TYPE_QUALIFIER_CONST);
3489 type_void = make_atomic_type(ATOMIC_TYPE_VOID, 0);
3490 type_string = make_pointer_type(type_const_char, 0);
3493 void exit_parser(void)
3495 obstack_free(&temp_obst, NULL);