7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
17 #include "adt/bitfiddle.h"
18 #include "adt/error.h"
19 #include "adt/array.h"
21 //#define PRINT_TOKENS
22 //#define ABORT_ON_ERROR
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static context_t *global_context = NULL;
48 static context_t *context = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static struct obstack temp_obst;
53 /** The current source position. */
54 #define HERE token.source_position
56 static type_t *type_valist;
58 static statement_t *parse_compound_statement(void);
59 static statement_t *parse_statement(void);
61 static expression_t *parse_sub_expression(unsigned precedence);
62 static expression_t *parse_expression(void);
63 static type_t *parse_typename(void);
65 static void parse_compound_type_entries(void);
66 static declaration_t *parse_declarator(
67 const declaration_specifiers_t *specifiers, bool may_be_abstract);
68 static declaration_t *record_declaration(declaration_t *declaration);
70 static void semantic_comparison(binary_expression_t *expression);
72 #define STORAGE_CLASSES \
79 #define TYPE_QUALIFIERS \
86 #ifdef PROVIDE_COMPLEX
87 #define COMPLEX_SPECIFIERS \
89 #define IMAGINARY_SPECIFIERS \
92 #define COMPLEX_SPECIFIERS
93 #define IMAGINARY_SPECIFIERS
96 #define TYPE_SPECIFIERS \
111 case T___builtin_va_list: \
115 #define DECLARATION_START \
120 #define TYPENAME_START \
125 * Allocate an AST node with given size and
126 * initialize all fields with zero.
128 static void *allocate_ast_zero(size_t size)
130 void *res = allocate_ast(size);
131 memset(res, 0, size);
136 * Returns the size of a statement node.
138 * @param kind the statement kind
140 static size_t get_statement_struct_size(statement_kind_t kind)
142 static const size_t sizes[] = {
143 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
144 [STATEMENT_RETURN] = sizeof(return_statement_t),
145 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
146 [STATEMENT_IF] = sizeof(if_statement_t),
147 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
148 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
149 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
150 [STATEMENT_BREAK] = sizeof(statement_base_t),
151 [STATEMENT_GOTO] = sizeof(goto_statement_t),
152 [STATEMENT_LABEL] = sizeof(label_statement_t),
153 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
154 [STATEMENT_WHILE] = sizeof(while_statement_t),
155 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
156 [STATEMENT_FOR] = sizeof(for_statement_t),
157 [STATEMENT_ASM] = sizeof(asm_statement_t)
159 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
160 assert(sizes[kind] != 0);
165 * Allocate a statement node of given kind and initialize all
168 static statement_t *allocate_statement_zero(statement_kind_t kind)
170 size_t size = get_statement_struct_size(kind);
171 statement_t *res = allocate_ast_zero(size);
173 res->base.kind = kind;
178 * Returns the size of an expression node.
180 * @param kind the expression kind
182 static size_t get_expression_struct_size(expression_kind_t kind)
184 static const size_t sizes[] = {
185 [EXPR_INVALID] = sizeof(expression_base_t),
186 [EXPR_REFERENCE] = sizeof(reference_expression_t),
187 [EXPR_CONST] = sizeof(const_expression_t),
188 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
189 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
190 [EXPR_CALL] = sizeof(call_expression_t),
191 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
192 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
193 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
194 [EXPR_SELECT] = sizeof(select_expression_t),
195 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
196 [EXPR_SIZEOF] = sizeof(sizeof_expression_t),
197 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
198 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
199 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
200 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
201 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
202 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
203 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
204 [EXPR_VA_START] = sizeof(va_start_expression_t),
205 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
206 [EXPR_STATEMENT] = sizeof(statement_expression_t),
208 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
209 return sizes[EXPR_UNARY_FIRST];
211 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
212 return sizes[EXPR_BINARY_FIRST];
214 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
215 assert(sizes[kind] != 0);
220 * Allocate an expression node of given kind and initialize all
223 static expression_t *allocate_expression_zero(expression_kind_t kind)
225 size_t size = get_expression_struct_size(kind);
226 expression_t *res = allocate_ast_zero(size);
228 res->base.kind = kind;
233 * Returns the size of a type node.
235 * @param kind the type kind
237 static size_t get_type_struct_size(type_kind_t kind)
239 static const size_t sizes[] = {
240 [TYPE_ATOMIC] = sizeof(atomic_type_t),
241 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
242 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
243 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
244 [TYPE_ENUM] = sizeof(enum_type_t),
245 [TYPE_FUNCTION] = sizeof(function_type_t),
246 [TYPE_POINTER] = sizeof(pointer_type_t),
247 [TYPE_ARRAY] = sizeof(array_type_t),
248 [TYPE_BUILTIN] = sizeof(builtin_type_t),
249 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
250 [TYPE_TYPEOF] = sizeof(typeof_type_t),
252 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
253 assert(kind <= TYPE_TYPEOF);
254 assert(sizes[kind] != 0);
259 * Allocate a type node of given kind and initialize all
262 static type_t *allocate_type_zero(type_kind_t kind)
264 size_t size = get_type_struct_size(kind);
265 type_t *res = obstack_alloc(type_obst, size);
266 memset(res, 0, size);
268 res->base.kind = kind;
273 * Returns the size of an initializer node.
275 * @param kind the initializer kind
277 static size_t get_initializer_size(initializer_kind_t kind)
279 static const size_t sizes[] = {
280 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
281 [INITIALIZER_STRING] = sizeof(initializer_string_t),
282 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
283 [INITIALIZER_LIST] = sizeof(initializer_list_t)
285 assert(kind < sizeof(sizes) / sizeof(*sizes));
286 assert(sizes[kind] != 0);
291 * Allocate an initializer node of given kind and initialize all
294 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
296 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
303 * Free a type from the type obstack.
305 static void free_type(void *type)
307 obstack_free(type_obst, type);
311 * Returns the index of the top element of the environment stack.
313 static size_t environment_top(void)
315 return ARR_LEN(environment_stack);
319 * Returns the index of the top element of the label stack.
321 static size_t label_top(void)
323 return ARR_LEN(label_stack);
328 * Return the next token.
330 static inline void next_token(void)
332 token = lookahead_buffer[lookahead_bufpos];
333 lookahead_buffer[lookahead_bufpos] = lexer_token;
336 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
339 print_token(stderr, &token);
340 fprintf(stderr, "\n");
345 * Return the next token with a given lookahead.
347 static inline const token_t *look_ahead(int num)
349 assert(num > 0 && num <= MAX_LOOKAHEAD);
350 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
351 return &lookahead_buffer[pos];
354 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
357 * Report a parse error because an expected token was not found.
359 static void parse_error_expected(const char *message, ...)
361 if(message != NULL) {
362 errorf(HERE, "%s", message);
365 va_start(ap, message);
366 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
371 * Report a type error.
373 static void type_error(const char *msg, const source_position_t source_position,
376 errorf(source_position, "%s, but found type '%T'", msg, type);
380 * Report an incompatible type.
382 static void type_error_incompatible(const char *msg,
383 const source_position_t source_position, type_t *type1, type_t *type2)
385 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
389 * Eat an complete block, ie. '{ ... }'.
391 static void eat_block(void)
393 if(token.type == '{')
396 while(token.type != '}') {
397 if(token.type == T_EOF)
399 if(token.type == '{') {
409 * Eat a statement until an ';' token.
411 static void eat_statement(void)
413 while(token.type != ';') {
414 if(token.type == T_EOF)
416 if(token.type == '}')
418 if(token.type == '{') {
428 * Eat a parenthesed term, ie. '( ... )'.
430 static void eat_paren(void)
432 if(token.type == '(')
435 while(token.type != ')') {
436 if(token.type == T_EOF)
438 if(token.type == ')' || token.type == ';' || token.type == '}') {
441 if(token.type == '(') {
445 if(token.type == '{') {
454 #define expect(expected) \
455 if(UNLIKELY(token.type != (expected))) { \
456 parse_error_expected(NULL, (expected), 0); \
462 #define expect_block(expected) \
463 if(UNLIKELY(token.type != (expected))) { \
464 parse_error_expected(NULL, (expected), 0); \
470 #define expect_void(expected) \
471 if(UNLIKELY(token.type != (expected))) { \
472 parse_error_expected(NULL, (expected), 0); \
478 static void set_context(context_t *new_context)
480 context = new_context;
482 last_declaration = new_context->declarations;
483 if(last_declaration != NULL) {
484 while(last_declaration->next != NULL) {
485 last_declaration = last_declaration->next;
491 * Search a symbol in a given namespace and returns its declaration or
492 * NULL if this symbol was not found.
494 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
496 declaration_t *declaration = symbol->declaration;
497 for( ; declaration != NULL; declaration = declaration->symbol_next) {
498 if(declaration->namespc == namespc)
506 * pushs an environment_entry on the environment stack and links the
507 * corresponding symbol to the new entry
509 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
511 symbol_t *symbol = declaration->symbol;
512 namespace_t namespc = (namespace_t)declaration->namespc;
514 /* remember old declaration */
516 entry.symbol = symbol;
517 entry.old_declaration = symbol->declaration;
518 entry.namespc = (unsigned short) namespc;
519 ARR_APP1(stack_entry_t, *stack_ptr, entry);
521 /* replace/add declaration into declaration list of the symbol */
522 if(symbol->declaration == NULL) {
523 symbol->declaration = declaration;
525 declaration_t *iter_last = NULL;
526 declaration_t *iter = symbol->declaration;
527 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
528 /* replace an entry? */
529 if(iter->namespc == namespc) {
530 if(iter_last == NULL) {
531 symbol->declaration = declaration;
533 iter_last->symbol_next = declaration;
535 declaration->symbol_next = iter->symbol_next;
540 assert(iter_last->symbol_next == NULL);
541 iter_last->symbol_next = declaration;
546 static void environment_push(declaration_t *declaration)
548 assert(declaration->source_position.input_name != NULL);
549 assert(declaration->parent_context != NULL);
550 stack_push(&environment_stack, declaration);
553 static void label_push(declaration_t *declaration)
555 declaration->parent_context = ¤t_function->context;
556 stack_push(&label_stack, declaration);
560 * pops symbols from the environment stack until @p new_top is the top element
562 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
564 stack_entry_t *stack = *stack_ptr;
565 size_t top = ARR_LEN(stack);
568 assert(new_top <= top);
572 for(i = top; i > new_top; --i) {
573 stack_entry_t *entry = &stack[i - 1];
575 declaration_t *old_declaration = entry->old_declaration;
576 symbol_t *symbol = entry->symbol;
577 namespace_t namespc = (namespace_t)entry->namespc;
579 /* replace/remove declaration */
580 declaration_t *declaration = symbol->declaration;
581 assert(declaration != NULL);
582 if(declaration->namespc == namespc) {
583 if(old_declaration == NULL) {
584 symbol->declaration = declaration->symbol_next;
586 symbol->declaration = old_declaration;
589 declaration_t *iter_last = declaration;
590 declaration_t *iter = declaration->symbol_next;
591 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
592 /* replace an entry? */
593 if(iter->namespc == namespc) {
594 assert(iter_last != NULL);
595 iter_last->symbol_next = old_declaration;
596 old_declaration->symbol_next = iter->symbol_next;
600 assert(iter != NULL);
604 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
607 static void environment_pop_to(size_t new_top)
609 stack_pop_to(&environment_stack, new_top);
612 static void label_pop_to(size_t new_top)
614 stack_pop_to(&label_stack, new_top);
618 static int get_rank(const type_t *type)
620 assert(!is_typeref(type));
621 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
622 * and esp. footnote 108). However we can't fold constants (yet), so we
623 * can't decide whether unsigned int is possible, while int always works.
624 * (unsigned int would be preferable when possible... for stuff like
625 * struct { enum { ... } bla : 4; } ) */
626 if(type->kind == TYPE_ENUM)
627 return ATOMIC_TYPE_INT;
629 assert(type->kind == TYPE_ATOMIC);
630 const atomic_type_t *atomic_type = &type->atomic;
631 atomic_type_kind_t atype = atomic_type->akind;
635 static type_t *promote_integer(type_t *type)
637 if(type->kind == TYPE_BITFIELD)
638 return promote_integer(type->bitfield.base);
640 if(get_rank(type) < ATOMIC_TYPE_INT)
647 * Create a cast expression.
649 * @param expression the expression to cast
650 * @param dest_type the destination type
652 static expression_t *create_cast_expression(expression_t *expression,
655 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
657 cast->unary.value = expression;
658 cast->base.datatype = dest_type;
664 * Check if a given expression represents the 0 pointer constant.
666 static bool is_null_pointer_constant(const expression_t *expression)
668 /* skip void* cast */
669 if(expression->kind == EXPR_UNARY_CAST
670 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
671 expression = expression->unary.value;
674 /* TODO: not correct yet, should be any constant integer expression
675 * which evaluates to 0 */
676 if (expression->kind != EXPR_CONST)
679 type_t *const type = skip_typeref(expression->base.datatype);
680 if (!is_type_integer(type))
683 return expression->conste.v.int_value == 0;
687 * Create an implicit cast expression.
689 * @param expression the expression to cast
690 * @param dest_type the destination type
692 static expression_t *create_implicit_cast(expression_t *expression,
695 type_t *source_type = expression->base.datatype;
697 if(source_type == NULL)
700 source_type = skip_typeref(source_type);
701 dest_type = skip_typeref(dest_type);
703 if(source_type == dest_type)
706 switch (dest_type->kind) {
708 /* TODO warning for implicitly converting to enum */
711 if (source_type->kind != TYPE_ATOMIC &&
712 source_type->kind != TYPE_ENUM &&
713 source_type->kind != TYPE_BITFIELD) {
714 panic("casting of non-atomic types not implemented yet");
717 if(is_type_floating(dest_type) && !is_type_scalar(source_type)) {
718 type_error_incompatible("can't cast types",
719 expression->base.source_position, source_type,
724 return create_cast_expression(expression, dest_type);
727 switch (source_type->kind) {
729 if (is_null_pointer_constant(expression)) {
730 return create_cast_expression(expression, dest_type);
735 if (pointers_compatible(source_type, dest_type)) {
736 return create_cast_expression(expression, dest_type);
741 array_type_t *array_type = &source_type->array;
742 pointer_type_t *pointer_type = &dest_type->pointer;
743 if (types_compatible(array_type->element_type,
744 pointer_type->points_to)) {
745 return create_cast_expression(expression, dest_type);
751 panic("casting of non-atomic types not implemented yet");
754 type_error_incompatible("can't implicitly cast types",
755 expression->base.source_position, source_type, dest_type);
759 panic("casting of non-atomic types not implemented yet");
763 /** Implements the rules from § 6.5.16.1 */
764 static void semantic_assign(type_t *orig_type_left, expression_t **right,
767 type_t *orig_type_right = (*right)->base.datatype;
769 if(orig_type_right == NULL)
772 type_t *const type_left = skip_typeref(orig_type_left);
773 type_t *const type_right = skip_typeref(orig_type_right);
775 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
776 (is_type_pointer(type_left) && is_null_pointer_constant(*right)) ||
777 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
778 && is_type_pointer(type_right))) {
779 *right = create_implicit_cast(*right, type_left);
783 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
784 pointer_type_t *pointer_type_left = &type_left->pointer;
785 pointer_type_t *pointer_type_right = &type_right->pointer;
786 type_t *points_to_left = pointer_type_left->points_to;
787 type_t *points_to_right = pointer_type_right->points_to;
789 points_to_left = skip_typeref(points_to_left);
790 points_to_right = skip_typeref(points_to_right);
792 /* the left type has all qualifiers from the right type */
793 unsigned missing_qualifiers
794 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
795 if(missing_qualifiers != 0) {
796 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
800 points_to_left = get_unqualified_type(points_to_left);
801 points_to_right = get_unqualified_type(points_to_right);
803 if(!is_type_atomic(points_to_left, ATOMIC_TYPE_VOID)
804 && !is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)
805 && !types_compatible(points_to_left, points_to_right)) {
806 goto incompatible_assign_types;
809 *right = create_implicit_cast(*right, type_left);
813 if (is_type_compound(type_left)
814 && types_compatible(type_left, type_right)) {
815 *right = create_implicit_cast(*right, type_left);
819 incompatible_assign_types:
820 /* TODO: improve error message */
821 errorf(HERE, "incompatible types in %s", context);
822 errorf(HERE, "'%T' <- '%T'", orig_type_left, orig_type_right);
825 static expression_t *parse_constant_expression(void)
827 /* start parsing at precedence 7 (conditional expression) */
828 expression_t *result = parse_sub_expression(7);
830 if(!is_constant_expression(result)) {
831 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
837 static expression_t *parse_assignment_expression(void)
839 /* start parsing at precedence 2 (assignment expression) */
840 return parse_sub_expression(2);
843 static type_t *make_global_typedef(const char *name, type_t *type)
845 symbol_t *const symbol = symbol_table_insert(name);
847 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
848 declaration->namespc = NAMESPACE_NORMAL;
849 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
850 declaration->type = type;
851 declaration->symbol = symbol;
852 declaration->source_position = builtin_source_position;
854 record_declaration(declaration);
856 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
857 typedef_type->typedeft.declaration = declaration;
862 static const char *parse_string_literals(void)
864 assert(token.type == T_STRING_LITERAL);
865 const char *result = token.v.string;
869 while(token.type == T_STRING_LITERAL) {
870 result = concat_strings(result, token.v.string);
877 static void parse_attributes(void)
881 case T___attribute__: {
889 errorf(HERE, "EOF while parsing attribute");
908 if(token.type != T_STRING_LITERAL) {
909 parse_error_expected("while parsing assembler attribute",
914 parse_string_literals();
919 goto attributes_finished;
928 static designator_t *parse_designation(void)
930 if(token.type != '[' && token.type != '.')
933 designator_t *result = NULL;
934 designator_t *last = NULL;
937 designator_t *designator;
940 designator = allocate_ast_zero(sizeof(designator[0]));
942 designator->array_access = parse_constant_expression();
946 designator = allocate_ast_zero(sizeof(designator[0]));
948 if(token.type != T_IDENTIFIER) {
949 parse_error_expected("while parsing designator",
953 designator->symbol = token.v.symbol;
961 assert(designator != NULL);
963 last->next = designator;
972 static initializer_t *initializer_from_string(array_type_t *type,
975 /* TODO: check len vs. size of array type */
978 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
979 initializer->string.string = string;
984 static initializer_t *initializer_from_wide_string(array_type_t *const type,
985 wide_string_t *const string)
987 /* TODO: check len vs. size of array type */
990 initializer_t *const initializer =
991 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
992 initializer->wide_string.string = *string;
997 static initializer_t *initializer_from_expression(type_t *type,
998 expression_t *expression)
1000 /* TODO check that expression is a constant expression */
1002 /* § 6.7.8.14/15 char array may be initialized by string literals */
1003 type_t *const expr_type = expression->base.datatype;
1004 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1005 array_type_t *const array_type = &type->array;
1006 type_t *const element_type = skip_typeref(array_type->element_type);
1008 if (element_type->kind == TYPE_ATOMIC) {
1009 switch (expression->kind) {
1010 case EXPR_STRING_LITERAL:
1011 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
1012 return initializer_from_string(array_type,
1013 expression->string.value);
1016 case EXPR_WIDE_STRING_LITERAL: {
1017 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1018 if (get_unqualified_type(element_type) == bare_wchar_type) {
1019 return initializer_from_wide_string(array_type,
1020 &expression->wide_string.value);
1030 type_t *expression_type = skip_typeref(expression->base.datatype);
1031 if(is_type_scalar(type) || types_compatible(type, expression_type)) {
1032 semantic_assign(type, &expression, "initializer");
1034 initializer_t *result = allocate_initializer_zero(INITIALIZER_VALUE);
1035 result->value.value = expression;
1043 static initializer_t *parse_sub_initializer(type_t *type,
1044 expression_t *expression,
1045 type_t *expression_type);
1047 static initializer_t *parse_sub_initializer_elem(type_t *type)
1049 if(token.type == '{') {
1050 return parse_sub_initializer(type, NULL, NULL);
1053 expression_t *expression = parse_assignment_expression();
1054 type_t *expression_type = skip_typeref(expression->base.datatype);
1056 return parse_sub_initializer(type, expression, expression_type);
1059 static bool had_initializer_brace_warning;
1061 static void skip_designator(void)
1064 if(token.type == '.') {
1066 if(token.type == T_IDENTIFIER)
1068 } else if(token.type == '[') {
1070 parse_constant_expression();
1071 if(token.type == ']')
1079 static initializer_t *parse_sub_initializer(type_t *type,
1080 expression_t *expression,
1081 type_t *expression_type)
1083 if(is_type_scalar(type)) {
1084 /* there might be extra {} hierarchies */
1085 if(token.type == '{') {
1087 if(!had_initializer_brace_warning) {
1088 warningf(HERE, "braces around scalar initializer");
1089 had_initializer_brace_warning = true;
1091 initializer_t *result = parse_sub_initializer(type, NULL, NULL);
1092 if(token.type == ',') {
1094 /* TODO: warn about excessive elements */
1100 if(expression == NULL) {
1101 expression = parse_assignment_expression();
1103 return initializer_from_expression(type, expression);
1106 /* does the expression match the currently looked at object to initialize */
1107 if(expression != NULL) {
1108 initializer_t *result = initializer_from_expression(type, expression);
1113 bool read_paren = false;
1114 if(token.type == '{') {
1119 /* descend into subtype */
1120 initializer_t *result = NULL;
1121 initializer_t **elems;
1122 if(is_type_array(type)) {
1123 array_type_t *array_type = &type->array;
1124 type_t *element_type = array_type->element_type;
1125 element_type = skip_typeref(element_type);
1127 if(token.type == '.') {
1129 "compound designator in initializer for array type '%T'",
1135 had_initializer_brace_warning = false;
1136 if(expression == NULL) {
1137 sub = parse_sub_initializer_elem(element_type);
1139 sub = parse_sub_initializer(element_type, expression,
1143 /* didn't match the subtypes -> try the parent type */
1145 assert(!read_paren);
1149 elems = NEW_ARR_F(initializer_t*, 0);
1150 ARR_APP1(initializer_t*, elems, sub);
1153 if(token.type == '}')
1156 if(token.type == '}')
1159 sub = parse_sub_initializer_elem(element_type);
1161 /* TODO error, do nicer cleanup */
1162 errorf(HERE, "member initializer didn't match");
1166 ARR_APP1(initializer_t*, elems, sub);
1169 assert(is_type_compound(type));
1170 compound_type_t *compound_type = &type->compound;
1171 context_t *context = &compound_type->declaration->context;
1173 if(token.type == '[') {
1175 "array designator in initializer for compound type '%T'",
1180 declaration_t *first = context->declarations;
1183 type_t *first_type = first->type;
1184 first_type = skip_typeref(first_type);
1187 had_initializer_brace_warning = false;
1188 if(expression == NULL) {
1189 sub = parse_sub_initializer_elem(first_type);
1191 sub = parse_sub_initializer(first_type, expression,expression_type);
1194 /* didn't match the subtypes -> try our parent type */
1196 assert(!read_paren);
1200 elems = NEW_ARR_F(initializer_t*, 0);
1201 ARR_APP1(initializer_t*, elems, sub);
1203 declaration_t *iter = first->next;
1204 for( ; iter != NULL; iter = iter->next) {
1205 if(iter->symbol == NULL)
1207 if(iter->namespc != NAMESPACE_NORMAL)
1210 if(token.type == '}')
1213 if(token.type == '}')
1216 type_t *iter_type = iter->type;
1217 iter_type = skip_typeref(iter_type);
1219 sub = parse_sub_initializer_elem(iter_type);
1221 /* TODO error, do nicer cleanup */
1222 errorf(HERE, "member initializer didn't match");
1226 ARR_APP1(initializer_t*, elems, sub);
1230 int len = ARR_LEN(elems);
1231 size_t elems_size = sizeof(initializer_t*) * len;
1233 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1235 init->initializer.kind = INITIALIZER_LIST;
1237 memcpy(init->initializers, elems, elems_size);
1240 result = (initializer_t*) init;
1243 if(token.type == ',')
1250 static initializer_t *parse_initializer(type_t *type)
1252 initializer_t *result;
1254 type = skip_typeref(type);
1256 if(token.type != '{') {
1257 expression_t *expression = parse_assignment_expression();
1258 initializer_t *initializer = initializer_from_expression(type, expression);
1259 if(initializer == NULL) {
1260 errorf(HERE, "initializer expression '%E', type '%T' is incompatible with type '%T'", expression, expression->base.datatype, type);
1265 if(is_type_scalar(type)) {
1269 expression_t *expression = parse_assignment_expression();
1270 result = initializer_from_expression(type, expression);
1272 if(token.type == ',')
1278 result = parse_sub_initializer(type, NULL, NULL);
1284 static declaration_t *append_declaration(declaration_t *declaration);
1286 static declaration_t *parse_compound_type_specifier(bool is_struct)
1294 symbol_t *symbol = NULL;
1295 declaration_t *declaration = NULL;
1297 if (token.type == T___attribute__) {
1302 if(token.type == T_IDENTIFIER) {
1303 symbol = token.v.symbol;
1307 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1309 declaration = get_declaration(symbol, NAMESPACE_UNION);
1311 } else if(token.type != '{') {
1313 parse_error_expected("while parsing struct type specifier",
1314 T_IDENTIFIER, '{', 0);
1316 parse_error_expected("while parsing union type specifier",
1317 T_IDENTIFIER, '{', 0);
1323 if(declaration == NULL) {
1324 declaration = allocate_ast_zero(sizeof(declaration[0]));
1327 declaration->namespc = NAMESPACE_STRUCT;
1329 declaration->namespc = NAMESPACE_UNION;
1331 declaration->source_position = token.source_position;
1332 declaration->symbol = symbol;
1333 declaration->parent_context = context;
1334 if (symbol != NULL) {
1335 environment_push(declaration);
1337 append_declaration(declaration);
1340 if(token.type == '{') {
1341 if(declaration->init.is_defined) {
1342 assert(symbol != NULL);
1343 errorf(HERE, "multiple definition of '%s %Y'",
1344 is_struct ? "struct" : "union", symbol);
1345 declaration->context.declarations = NULL;
1347 declaration->init.is_defined = true;
1349 int top = environment_top();
1350 context_t *last_context = context;
1351 set_context(&declaration->context);
1353 parse_compound_type_entries();
1356 assert(context == &declaration->context);
1357 set_context(last_context);
1358 environment_pop_to(top);
1364 static void parse_enum_entries(enum_type_t *const enum_type)
1368 if(token.type == '}') {
1370 errorf(HERE, "empty enum not allowed");
1375 declaration_t *entry = allocate_ast_zero(sizeof(entry[0]));
1377 if(token.type != T_IDENTIFIER) {
1378 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1382 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1383 entry->type = (type_t*) enum_type;
1384 entry->symbol = token.v.symbol;
1385 entry->source_position = token.source_position;
1388 if(token.type == '=') {
1390 entry->init.enum_value = parse_constant_expression();
1395 record_declaration(entry);
1397 if(token.type != ',')
1400 } while(token.type != '}');
1405 static type_t *parse_enum_specifier(void)
1409 declaration_t *declaration;
1412 if(token.type == T_IDENTIFIER) {
1413 symbol = token.v.symbol;
1416 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1417 } else if(token.type != '{') {
1418 parse_error_expected("while parsing enum type specifier",
1419 T_IDENTIFIER, '{', 0);
1426 if(declaration == NULL) {
1427 declaration = allocate_ast_zero(sizeof(declaration[0]));
1429 declaration->namespc = NAMESPACE_ENUM;
1430 declaration->source_position = token.source_position;
1431 declaration->symbol = symbol;
1432 declaration->parent_context = context;
1435 type_t *const type = allocate_type_zero(TYPE_ENUM);
1436 type->enumt.declaration = declaration;
1438 if(token.type == '{') {
1439 if(declaration->init.is_defined) {
1440 errorf(HERE, "multiple definitions of enum %Y", symbol);
1442 if (symbol != NULL) {
1443 environment_push(declaration);
1445 append_declaration(declaration);
1446 declaration->init.is_defined = 1;
1448 parse_enum_entries(&type->enumt);
1456 * if a symbol is a typedef to another type, return true
1458 static bool is_typedef_symbol(symbol_t *symbol)
1460 const declaration_t *const declaration =
1461 get_declaration(symbol, NAMESPACE_NORMAL);
1463 declaration != NULL &&
1464 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1467 static type_t *parse_typeof(void)
1475 expression_t *expression = NULL;
1478 switch(token.type) {
1479 case T___extension__:
1480 /* this can be a prefix to a typename or an expression */
1481 /* we simply eat it now. */
1484 } while(token.type == T___extension__);
1488 if(is_typedef_symbol(token.v.symbol)) {
1489 type = parse_typename();
1491 expression = parse_expression();
1492 type = expression->base.datatype;
1497 type = parse_typename();
1501 expression = parse_expression();
1502 type = expression->base.datatype;
1508 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1509 typeof_type->typeoft.expression = expression;
1510 typeof_type->typeoft.typeof_type = type;
1516 SPECIFIER_SIGNED = 1 << 0,
1517 SPECIFIER_UNSIGNED = 1 << 1,
1518 SPECIFIER_LONG = 1 << 2,
1519 SPECIFIER_INT = 1 << 3,
1520 SPECIFIER_DOUBLE = 1 << 4,
1521 SPECIFIER_CHAR = 1 << 5,
1522 SPECIFIER_SHORT = 1 << 6,
1523 SPECIFIER_LONG_LONG = 1 << 7,
1524 SPECIFIER_FLOAT = 1 << 8,
1525 SPECIFIER_BOOL = 1 << 9,
1526 SPECIFIER_VOID = 1 << 10,
1527 #ifdef PROVIDE_COMPLEX
1528 SPECIFIER_COMPLEX = 1 << 11,
1529 SPECIFIER_IMAGINARY = 1 << 12,
1533 static type_t *create_builtin_type(symbol_t *const symbol,
1534 type_t *const real_type)
1536 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1537 type->builtin.symbol = symbol;
1538 type->builtin.real_type = real_type;
1540 type_t *result = typehash_insert(type);
1541 if (type != result) {
1548 static type_t *get_typedef_type(symbol_t *symbol)
1550 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1551 if(declaration == NULL
1552 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1555 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1556 type->typedeft.declaration = declaration;
1561 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1563 type_t *type = NULL;
1564 unsigned type_qualifiers = 0;
1565 unsigned type_specifiers = 0;
1568 specifiers->source_position = token.source_position;
1571 switch(token.type) {
1574 #define MATCH_STORAGE_CLASS(token, class) \
1576 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1577 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1579 specifiers->storage_class = class; \
1583 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1584 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1585 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1586 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1587 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1590 switch (specifiers->storage_class) {
1591 case STORAGE_CLASS_NONE:
1592 specifiers->storage_class = STORAGE_CLASS_THREAD;
1595 case STORAGE_CLASS_EXTERN:
1596 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1599 case STORAGE_CLASS_STATIC:
1600 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1604 errorf(HERE, "multiple storage classes in declaration specifiers");
1610 /* type qualifiers */
1611 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1613 type_qualifiers |= qualifier; \
1617 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1618 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1619 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1621 case T___extension__:
1626 /* type specifiers */
1627 #define MATCH_SPECIFIER(token, specifier, name) \
1630 if(type_specifiers & specifier) { \
1631 errorf(HERE, "multiple " name " type specifiers given"); \
1633 type_specifiers |= specifier; \
1637 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1638 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1639 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1640 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1641 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1642 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1643 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1644 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1645 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1646 #ifdef PROVIDE_COMPLEX
1647 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1648 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1651 /* only in microsoft mode */
1652 specifiers->decl_modifiers |= DM_FORCEINLINE;
1656 specifiers->is_inline = true;
1661 if(type_specifiers & SPECIFIER_LONG_LONG) {
1662 errorf(HERE, "multiple type specifiers given");
1663 } else if(type_specifiers & SPECIFIER_LONG) {
1664 type_specifiers |= SPECIFIER_LONG_LONG;
1666 type_specifiers |= SPECIFIER_LONG;
1670 /* TODO: if type != NULL for the following rules should issue
1673 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1675 type->compound.declaration = parse_compound_type_specifier(true);
1679 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1681 type->compound.declaration = parse_compound_type_specifier(false);
1685 type = parse_enum_specifier();
1688 type = parse_typeof();
1690 case T___builtin_va_list:
1691 type = duplicate_type(type_valist);
1695 case T___attribute__:
1700 case T_IDENTIFIER: {
1701 type_t *typedef_type = get_typedef_type(token.v.symbol);
1703 if(typedef_type == NULL)
1704 goto finish_specifiers;
1707 type = typedef_type;
1711 /* function specifier */
1713 goto finish_specifiers;
1720 atomic_type_kind_t atomic_type;
1722 /* match valid basic types */
1723 switch(type_specifiers) {
1724 case SPECIFIER_VOID:
1725 atomic_type = ATOMIC_TYPE_VOID;
1727 case SPECIFIER_CHAR:
1728 atomic_type = ATOMIC_TYPE_CHAR;
1730 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1731 atomic_type = ATOMIC_TYPE_SCHAR;
1733 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1734 atomic_type = ATOMIC_TYPE_UCHAR;
1736 case SPECIFIER_SHORT:
1737 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1738 case SPECIFIER_SHORT | SPECIFIER_INT:
1739 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1740 atomic_type = ATOMIC_TYPE_SHORT;
1742 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1743 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1744 atomic_type = ATOMIC_TYPE_USHORT;
1747 case SPECIFIER_SIGNED:
1748 case SPECIFIER_SIGNED | SPECIFIER_INT:
1749 atomic_type = ATOMIC_TYPE_INT;
1751 case SPECIFIER_UNSIGNED:
1752 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1753 atomic_type = ATOMIC_TYPE_UINT;
1755 case SPECIFIER_LONG:
1756 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1757 case SPECIFIER_LONG | SPECIFIER_INT:
1758 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1759 atomic_type = ATOMIC_TYPE_LONG;
1761 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1762 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1763 atomic_type = ATOMIC_TYPE_ULONG;
1765 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1766 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1767 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1768 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1770 atomic_type = ATOMIC_TYPE_LONGLONG;
1772 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1773 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1775 atomic_type = ATOMIC_TYPE_ULONGLONG;
1777 case SPECIFIER_FLOAT:
1778 atomic_type = ATOMIC_TYPE_FLOAT;
1780 case SPECIFIER_DOUBLE:
1781 atomic_type = ATOMIC_TYPE_DOUBLE;
1783 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1784 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1786 case SPECIFIER_BOOL:
1787 atomic_type = ATOMIC_TYPE_BOOL;
1789 #ifdef PROVIDE_COMPLEX
1790 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1791 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1793 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1794 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1796 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1797 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1799 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1800 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1802 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1803 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1805 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1806 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1810 /* invalid specifier combination, give an error message */
1811 if(type_specifiers == 0) {
1812 if (! strict_mode) {
1813 warningf(HERE, "no type specifiers in declaration, using int");
1814 atomic_type = ATOMIC_TYPE_INT;
1817 errorf(HERE, "no type specifiers given in declaration");
1819 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1820 (type_specifiers & SPECIFIER_UNSIGNED)) {
1821 errorf(HERE, "signed and unsigned specifiers gives");
1822 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1823 errorf(HERE, "only integer types can be signed or unsigned");
1825 errorf(HERE, "multiple datatypes in declaration");
1827 atomic_type = ATOMIC_TYPE_INVALID;
1830 type = allocate_type_zero(TYPE_ATOMIC);
1831 type->atomic.akind = atomic_type;
1834 if(type_specifiers != 0) {
1835 errorf(HERE, "multiple datatypes in declaration");
1839 type->base.qualifiers = type_qualifiers;
1841 type_t *result = typehash_insert(type);
1842 if(newtype && result != type) {
1846 specifiers->type = result;
1849 static type_qualifiers_t parse_type_qualifiers(void)
1851 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1854 switch(token.type) {
1855 /* type qualifiers */
1856 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1857 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1858 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1861 return type_qualifiers;
1866 static declaration_t *parse_identifier_list(void)
1868 declaration_t *declarations = NULL;
1869 declaration_t *last_declaration = NULL;
1871 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
1873 declaration->source_position = token.source_position;
1874 declaration->symbol = token.v.symbol;
1877 if(last_declaration != NULL) {
1878 last_declaration->next = declaration;
1880 declarations = declaration;
1882 last_declaration = declaration;
1884 if(token.type != ',')
1887 } while(token.type == T_IDENTIFIER);
1889 return declarations;
1892 static void semantic_parameter(declaration_t *declaration)
1894 /* TODO: improve error messages */
1896 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1897 errorf(HERE, "typedef not allowed in parameter list");
1898 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1899 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1900 errorf(HERE, "parameter may only have none or register storage class");
1903 type_t *orig_type = declaration->type;
1904 if(orig_type == NULL)
1906 type_t *type = skip_typeref(orig_type);
1908 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1909 * into a pointer. § 6.7.5.3 (7) */
1910 if (is_type_array(type)) {
1911 const array_type_t *arr_type = &type->array;
1912 type_t *element_type = arr_type->element_type;
1914 type = make_pointer_type(element_type, type->base.qualifiers);
1916 declaration->type = type;
1919 if(is_type_incomplete(type)) {
1920 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1921 orig_type, declaration->symbol);
1925 static declaration_t *parse_parameter(void)
1927 declaration_specifiers_t specifiers;
1928 memset(&specifiers, 0, sizeof(specifiers));
1930 parse_declaration_specifiers(&specifiers);
1932 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1934 semantic_parameter(declaration);
1939 static declaration_t *parse_parameters(function_type_t *type)
1941 if(token.type == T_IDENTIFIER) {
1942 symbol_t *symbol = token.v.symbol;
1943 if(!is_typedef_symbol(symbol)) {
1944 type->kr_style_parameters = true;
1945 return parse_identifier_list();
1949 if(token.type == ')') {
1950 type->unspecified_parameters = 1;
1953 if(token.type == T_void && look_ahead(1)->type == ')') {
1958 declaration_t *declarations = NULL;
1959 declaration_t *declaration;
1960 declaration_t *last_declaration = NULL;
1961 function_parameter_t *parameter;
1962 function_parameter_t *last_parameter = NULL;
1965 switch(token.type) {
1969 return declarations;
1972 case T___extension__:
1974 declaration = parse_parameter();
1976 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1977 memset(parameter, 0, sizeof(parameter[0]));
1978 parameter->type = declaration->type;
1980 if(last_parameter != NULL) {
1981 last_declaration->next = declaration;
1982 last_parameter->next = parameter;
1984 type->parameters = parameter;
1985 declarations = declaration;
1987 last_parameter = parameter;
1988 last_declaration = declaration;
1992 return declarations;
1994 if(token.type != ',')
1995 return declarations;
2005 } construct_type_type_t;
2007 typedef struct construct_type_t construct_type_t;
2008 struct construct_type_t {
2009 construct_type_type_t type;
2010 construct_type_t *next;
2013 typedef struct parsed_pointer_t parsed_pointer_t;
2014 struct parsed_pointer_t {
2015 construct_type_t construct_type;
2016 type_qualifiers_t type_qualifiers;
2019 typedef struct construct_function_type_t construct_function_type_t;
2020 struct construct_function_type_t {
2021 construct_type_t construct_type;
2022 type_t *function_type;
2025 typedef struct parsed_array_t parsed_array_t;
2026 struct parsed_array_t {
2027 construct_type_t construct_type;
2028 type_qualifiers_t type_qualifiers;
2034 typedef struct construct_base_type_t construct_base_type_t;
2035 struct construct_base_type_t {
2036 construct_type_t construct_type;
2040 static construct_type_t *parse_pointer_declarator(void)
2044 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2045 memset(pointer, 0, sizeof(pointer[0]));
2046 pointer->construct_type.type = CONSTRUCT_POINTER;
2047 pointer->type_qualifiers = parse_type_qualifiers();
2049 return (construct_type_t*) pointer;
2052 static construct_type_t *parse_array_declarator(void)
2056 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2057 memset(array, 0, sizeof(array[0]));
2058 array->construct_type.type = CONSTRUCT_ARRAY;
2060 if(token.type == T_static) {
2061 array->is_static = true;
2065 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2066 if(type_qualifiers != 0) {
2067 if(token.type == T_static) {
2068 array->is_static = true;
2072 array->type_qualifiers = type_qualifiers;
2074 if(token.type == '*' && look_ahead(1)->type == ']') {
2075 array->is_variable = true;
2077 } else if(token.type != ']') {
2078 array->size = parse_assignment_expression();
2083 return (construct_type_t*) array;
2086 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2090 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2092 declaration_t *parameters = parse_parameters(&type->function);
2093 if(declaration != NULL) {
2094 declaration->context.declarations = parameters;
2097 construct_function_type_t *construct_function_type =
2098 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2099 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2100 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2101 construct_function_type->function_type = type;
2105 return (construct_type_t*) construct_function_type;
2108 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2109 bool may_be_abstract)
2111 /* construct a single linked list of construct_type_t's which describe
2112 * how to construct the final declarator type */
2113 construct_type_t *first = NULL;
2114 construct_type_t *last = NULL;
2117 while(token.type == '*') {
2118 construct_type_t *type = parse_pointer_declarator();
2129 /* TODO: find out if this is correct */
2132 construct_type_t *inner_types = NULL;
2134 switch(token.type) {
2136 if(declaration == NULL) {
2137 errorf(HERE, "no identifier expected in typename");
2139 declaration->symbol = token.v.symbol;
2140 declaration->source_position = token.source_position;
2146 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2152 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2153 /* avoid a loop in the outermost scope, because eat_statement doesn't
2155 if(token.type == '}' && current_function == NULL) {
2163 construct_type_t *p = last;
2166 construct_type_t *type;
2167 switch(token.type) {
2169 type = parse_function_declarator(declaration);
2172 type = parse_array_declarator();
2175 goto declarator_finished;
2178 /* insert in the middle of the list (behind p) */
2180 type->next = p->next;
2191 declarator_finished:
2194 /* append inner_types at the end of the list, we don't to set last anymore
2195 * as it's not needed anymore */
2197 assert(first == NULL);
2198 first = inner_types;
2200 last->next = inner_types;
2206 static type_t *construct_declarator_type(construct_type_t *construct_list,
2209 construct_type_t *iter = construct_list;
2210 for( ; iter != NULL; iter = iter->next) {
2211 switch(iter->type) {
2212 case CONSTRUCT_INVALID:
2213 panic("invalid type construction found");
2214 case CONSTRUCT_FUNCTION: {
2215 construct_function_type_t *construct_function_type
2216 = (construct_function_type_t*) iter;
2218 type_t *function_type = construct_function_type->function_type;
2220 function_type->function.return_type = type;
2222 type = function_type;
2226 case CONSTRUCT_POINTER: {
2227 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2228 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2229 pointer_type->pointer.points_to = type;
2230 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2232 type = pointer_type;
2236 case CONSTRUCT_ARRAY: {
2237 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2238 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2240 array_type->base.qualifiers = parsed_array->type_qualifiers;
2241 array_type->array.element_type = type;
2242 array_type->array.is_static = parsed_array->is_static;
2243 array_type->array.is_variable = parsed_array->is_variable;
2244 array_type->array.size = parsed_array->size;
2251 type_t *hashed_type = typehash_insert(type);
2252 if(hashed_type != type) {
2253 /* the function type was constructed earlier freeing it here will
2254 * destroy other types... */
2255 if(iter->type != CONSTRUCT_FUNCTION) {
2265 static declaration_t *parse_declarator(
2266 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2268 type_t *type = specifiers->type;
2269 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2270 declaration->storage_class = specifiers->storage_class;
2271 declaration->modifiers = specifiers->decl_modifiers;
2272 declaration->is_inline = specifiers->is_inline;
2274 construct_type_t *construct_type
2275 = parse_inner_declarator(declaration, may_be_abstract);
2276 declaration->type = construct_declarator_type(construct_type, type);
2278 if(construct_type != NULL) {
2279 obstack_free(&temp_obst, construct_type);
2285 static type_t *parse_abstract_declarator(type_t *base_type)
2287 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2289 type_t *result = construct_declarator_type(construct_type, base_type);
2290 if(construct_type != NULL) {
2291 obstack_free(&temp_obst, construct_type);
2297 static declaration_t *append_declaration(declaration_t* const declaration)
2299 if (last_declaration != NULL) {
2300 last_declaration->next = declaration;
2302 context->declarations = declaration;
2304 last_declaration = declaration;
2308 static declaration_t *internal_record_declaration(
2309 declaration_t *const declaration,
2310 const bool is_function_definition)
2312 const symbol_t *const symbol = declaration->symbol;
2313 const namespace_t namespc = (namespace_t)declaration->namespc;
2315 const type_t *const type = skip_typeref(declaration->type);
2316 if (is_type_function(type) && type->function.unspecified_parameters) {
2317 warningf(declaration->source_position,
2318 "function declaration '%#T' is not a prototype",
2319 type, declaration->symbol);
2322 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2323 assert(declaration != previous_declaration);
2324 if (previous_declaration != NULL
2325 && previous_declaration->parent_context == context) {
2326 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2327 if (!types_compatible(type, prev_type)) {
2328 errorf(declaration->source_position,
2329 "declaration '%#T' is incompatible with previous declaration '%#T'",
2330 type, symbol, previous_declaration->type, symbol);
2331 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2333 unsigned old_storage_class = previous_declaration->storage_class;
2334 unsigned new_storage_class = declaration->storage_class;
2336 /* pretend no storage class means extern for function declarations
2337 * (except if the previous declaration is neither none nor extern) */
2338 if (is_type_function(type)) {
2339 switch (old_storage_class) {
2340 case STORAGE_CLASS_NONE:
2341 old_storage_class = STORAGE_CLASS_EXTERN;
2343 case STORAGE_CLASS_EXTERN:
2344 if (new_storage_class == STORAGE_CLASS_NONE && !is_function_definition) {
2345 new_storage_class = STORAGE_CLASS_EXTERN;
2353 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2354 new_storage_class == STORAGE_CLASS_EXTERN) {
2355 warn_redundant_declaration:
2356 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2357 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2358 } else if (current_function == NULL) {
2359 if (old_storage_class != STORAGE_CLASS_STATIC &&
2360 new_storage_class == STORAGE_CLASS_STATIC) {
2361 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2362 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2364 if (old_storage_class != STORAGE_CLASS_EXTERN) {
2365 goto warn_redundant_declaration;
2367 if (new_storage_class == STORAGE_CLASS_NONE) {
2368 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2372 if (old_storage_class == new_storage_class) {
2373 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2375 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2377 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2380 return previous_declaration;
2383 assert(declaration->parent_context == NULL);
2384 assert(declaration->symbol != NULL);
2385 assert(context != NULL);
2387 declaration->parent_context = context;
2389 environment_push(declaration);
2390 return append_declaration(declaration);
2393 static declaration_t *record_declaration(declaration_t *declaration)
2395 return internal_record_declaration(declaration, false);
2398 static declaration_t *record_function_definition(declaration_t *const declaration)
2400 return internal_record_declaration(declaration, true);
2403 static void parser_error_multiple_definition(declaration_t *declaration,
2404 const source_position_t source_position)
2406 errorf(source_position, "multiple definition of symbol '%Y'",
2407 declaration->symbol);
2408 errorf(declaration->source_position,
2409 "this is the location of the previous definition.");
2412 static bool is_declaration_specifier(const token_t *token,
2413 bool only_type_specifiers)
2415 switch(token->type) {
2419 return is_typedef_symbol(token->v.symbol);
2421 case T___extension__:
2424 return !only_type_specifiers;
2431 static void parse_init_declarator_rest(declaration_t *declaration)
2435 type_t *orig_type = declaration->type;
2436 type_t *type = NULL;
2437 if(orig_type != NULL)
2438 type = skip_typeref(orig_type);
2440 if(declaration->init.initializer != NULL) {
2441 parser_error_multiple_definition(declaration, token.source_position);
2444 initializer_t *initializer = parse_initializer(type);
2446 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2447 * the array type size */
2448 if(type != NULL && is_type_array(type) && initializer != NULL) {
2449 array_type_t *array_type = &type->array;
2451 if(array_type->size == NULL) {
2452 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2454 cnst->base.datatype = type_size_t;
2456 switch (initializer->kind) {
2457 case INITIALIZER_LIST: {
2458 initializer_list_t *const list = &initializer->list;
2459 cnst->conste.v.int_value = list->len;
2463 case INITIALIZER_STRING: {
2464 initializer_string_t *const string = &initializer->string;
2465 cnst->conste.v.int_value = strlen(string->string) + 1;
2469 case INITIALIZER_WIDE_STRING: {
2470 initializer_wide_string_t *const string = &initializer->wide_string;
2471 cnst->conste.v.int_value = string->string.size;
2476 panic("invalid initializer type");
2479 array_type->size = cnst;
2483 if(type != NULL && is_type_function(type)) {
2484 errorf(declaration->source_position,
2485 "initializers not allowed for function types at declator '%Y' (type '%T')",
2486 declaration->symbol, orig_type);
2488 declaration->init.initializer = initializer;
2492 /* parse rest of a declaration without any declarator */
2493 static void parse_anonymous_declaration_rest(
2494 const declaration_specifiers_t *specifiers,
2495 parsed_declaration_func finished_declaration)
2499 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2501 declaration->type = specifiers->type;
2502 declaration->storage_class = specifiers->storage_class;
2503 declaration->source_position = specifiers->source_position;
2505 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2506 warningf(declaration->source_position, "useless storage class in empty declaration");
2509 type_t *type = declaration->type;
2510 switch (type->kind) {
2511 case TYPE_COMPOUND_STRUCT:
2512 case TYPE_COMPOUND_UNION: {
2513 const compound_type_t *compound_type = &type->compound;
2514 if (compound_type->declaration->symbol == NULL) {
2515 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2524 warningf(declaration->source_position, "empty declaration");
2528 finished_declaration(declaration);
2531 static void parse_declaration_rest(declaration_t *ndeclaration,
2532 const declaration_specifiers_t *specifiers,
2533 parsed_declaration_func finished_declaration)
2536 declaration_t *declaration = finished_declaration(ndeclaration);
2538 type_t *orig_type = declaration->type;
2539 type_t *type = skip_typeref(orig_type);
2541 if(type->kind != TYPE_FUNCTION && declaration->is_inline) {
2542 warningf(declaration->source_position,
2543 "variable '%Y' declared 'inline'\n", declaration->symbol);
2546 if(token.type == '=') {
2547 parse_init_declarator_rest(declaration);
2550 if(token.type != ',')
2554 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2559 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2561 /* TODO: check that it was actually a parameter that gets a type */
2563 /* we should have a declaration for the parameter in the current
2565 return record_declaration(declaration);
2568 static void parse_declaration(parsed_declaration_func finished_declaration)
2570 declaration_specifiers_t specifiers;
2571 memset(&specifiers, 0, sizeof(specifiers));
2572 parse_declaration_specifiers(&specifiers);
2574 if(token.type == ';') {
2575 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2577 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2578 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2582 static void parse_kr_declaration_list(declaration_t *declaration)
2584 type_t *type = skip_typeref(declaration->type);
2585 if(!is_type_function(type))
2588 if(!type->function.kr_style_parameters)
2591 /* push function parameters */
2592 int top = environment_top();
2593 context_t *last_context = context;
2594 set_context(&declaration->context);
2596 declaration_t *parameter = declaration->context.declarations;
2597 for( ; parameter != NULL; parameter = parameter->next) {
2598 assert(parameter->parent_context == NULL);
2599 parameter->parent_context = context;
2600 environment_push(parameter);
2603 /* parse declaration list */
2604 while(is_declaration_specifier(&token, false)) {
2605 parse_declaration(finished_kr_declaration);
2608 /* pop function parameters */
2609 assert(context == &declaration->context);
2610 set_context(last_context);
2611 environment_pop_to(top);
2613 /* update function type */
2614 type_t *new_type = duplicate_type(type);
2615 new_type->function.kr_style_parameters = false;
2617 function_parameter_t *parameters = NULL;
2618 function_parameter_t *last_parameter = NULL;
2620 declaration_t *parameter_declaration = declaration->context.declarations;
2621 for( ; parameter_declaration != NULL;
2622 parameter_declaration = parameter_declaration->next) {
2623 type_t *parameter_type = parameter_declaration->type;
2624 if(parameter_type == NULL) {
2626 errorf(HERE, "no type specified for function parameter '%Y'",
2627 parameter_declaration->symbol);
2629 warningf(HERE, "no type specified for function parameter '%Y', using int",
2630 parameter_declaration->symbol);
2631 parameter_type = type_int;
2632 parameter_declaration->type = parameter_type;
2636 semantic_parameter(parameter_declaration);
2637 parameter_type = parameter_declaration->type;
2639 function_parameter_t *function_parameter
2640 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2641 memset(function_parameter, 0, sizeof(function_parameter[0]));
2643 function_parameter->type = parameter_type;
2644 if(last_parameter != NULL) {
2645 last_parameter->next = function_parameter;
2647 parameters = function_parameter;
2649 last_parameter = function_parameter;
2651 new_type->function.parameters = parameters;
2653 type = typehash_insert(new_type);
2654 if(type != new_type) {
2655 obstack_free(type_obst, new_type);
2658 declaration->type = type;
2661 static void parse_external_declaration(void)
2663 /* function-definitions and declarations both start with declaration
2665 declaration_specifiers_t specifiers;
2666 memset(&specifiers, 0, sizeof(specifiers));
2667 parse_declaration_specifiers(&specifiers);
2669 /* must be a declaration */
2670 if(token.type == ';') {
2671 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2675 /* declarator is common to both function-definitions and declarations */
2676 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2678 /* must be a declaration */
2679 if(token.type == ',' || token.type == '=' || token.type == ';') {
2680 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2684 /* must be a function definition */
2685 parse_kr_declaration_list(ndeclaration);
2687 if(token.type != '{') {
2688 parse_error_expected("while parsing function definition", '{', 0);
2693 type_t *type = ndeclaration->type;
2699 /* note that we don't skip typerefs: the standard doesn't allow them here
2700 * (so we can't use is_type_function here) */
2701 if(type->kind != TYPE_FUNCTION) {
2702 errorf(HERE, "declarator '%#T' has a body but is not a function type", type, ndeclaration->symbol);
2707 /* § 6.7.5.3 (14) a function definition with () means no
2708 * parameters (and not unspecified parameters) */
2709 if(type->function.unspecified_parameters) {
2710 type_t *duplicate = duplicate_type(type);
2711 duplicate->function.unspecified_parameters = false;
2713 type = typehash_insert(duplicate);
2714 if(type != duplicate) {
2715 obstack_free(type_obst, duplicate);
2717 ndeclaration->type = type;
2720 declaration_t *const declaration = record_function_definition(ndeclaration);
2721 if(ndeclaration != declaration) {
2722 declaration->context = ndeclaration->context;
2724 type = skip_typeref(declaration->type);
2726 /* push function parameters and switch context */
2727 int top = environment_top();
2728 context_t *last_context = context;
2729 set_context(&declaration->context);
2731 declaration_t *parameter = declaration->context.declarations;
2732 for( ; parameter != NULL; parameter = parameter->next) {
2733 assert(parameter->parent_context == NULL || parameter->parent_context == context);
2734 parameter->parent_context = context;
2735 environment_push(parameter);
2738 if(declaration->init.statement != NULL) {
2739 parser_error_multiple_definition(declaration, token.source_position);
2741 goto end_of_parse_external_declaration;
2743 /* parse function body */
2744 int label_stack_top = label_top();
2745 declaration_t *old_current_function = current_function;
2746 current_function = declaration;
2748 declaration->init.statement = parse_compound_statement();
2750 assert(current_function == declaration);
2751 current_function = old_current_function;
2752 label_pop_to(label_stack_top);
2755 end_of_parse_external_declaration:
2756 assert(context == &declaration->context);
2757 set_context(last_context);
2758 environment_pop_to(top);
2761 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2763 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2764 type->bitfield.base = base;
2765 type->bitfield.size = size;
2770 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2772 /* TODO: check constraints for struct declarations (in specifiers) */
2774 declaration_t *declaration;
2776 if(token.type == ':') {
2779 type_t *base_type = specifiers->type;
2780 expression_t *size = parse_constant_expression();
2782 type_t *type = make_bitfield_type(base_type, size);
2784 declaration = allocate_ast_zero(sizeof(declaration[0]));
2786 declaration->namespc = NAMESPACE_NORMAL;
2787 declaration->storage_class = STORAGE_CLASS_NONE;
2788 declaration->source_position = token.source_position;
2789 declaration->modifiers = specifiers->decl_modifiers;
2790 declaration->type = type;
2792 record_declaration(declaration);
2794 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2796 if(token.type == ':') {
2798 expression_t *size = parse_constant_expression();
2800 type_t *type = make_bitfield_type(declaration->type, size);
2801 declaration->type = type;
2804 record_declaration(declaration);
2806 if(token.type != ',')
2813 static void parse_compound_type_entries(void)
2817 while(token.type != '}' && token.type != T_EOF) {
2818 declaration_specifiers_t specifiers;
2819 memset(&specifiers, 0, sizeof(specifiers));
2820 parse_declaration_specifiers(&specifiers);
2822 parse_struct_declarators(&specifiers);
2824 if(token.type == T_EOF) {
2825 errorf(HERE, "EOF while parsing struct");
2830 static type_t *parse_typename(void)
2832 declaration_specifiers_t specifiers;
2833 memset(&specifiers, 0, sizeof(specifiers));
2834 parse_declaration_specifiers(&specifiers);
2835 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2836 /* TODO: improve error message, user does probably not know what a
2837 * storage class is...
2839 errorf(HERE, "typename may not have a storage class");
2842 type_t *result = parse_abstract_declarator(specifiers.type);
2850 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2851 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2852 expression_t *left);
2854 typedef struct expression_parser_function_t expression_parser_function_t;
2855 struct expression_parser_function_t {
2856 unsigned precedence;
2857 parse_expression_function parser;
2858 unsigned infix_precedence;
2859 parse_expression_infix_function infix_parser;
2862 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2865 * Creates a new invalid expression.
2867 static expression_t *create_invalid_expression(void)
2869 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
2870 expression->base.source_position = token.source_position;
2874 static expression_t *expected_expression_error(void)
2876 errorf(HERE, "expected expression, got token '%K'", &token);
2880 return create_invalid_expression();
2884 * Parse a string constant.
2886 static expression_t *parse_string_const(void)
2888 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
2889 cnst->base.datatype = type_string;
2890 cnst->string.value = parse_string_literals();
2896 * Parse a wide string constant.
2898 static expression_t *parse_wide_string_const(void)
2900 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
2901 cnst->base.datatype = type_wchar_t_ptr;
2902 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
2908 * Parse an integer constant.
2910 static expression_t *parse_int_const(void)
2912 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2913 cnst->base.datatype = token.datatype;
2914 cnst->conste.v.int_value = token.v.intvalue;
2922 * Parse a float constant.
2924 static expression_t *parse_float_const(void)
2926 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2927 cnst->base.datatype = token.datatype;
2928 cnst->conste.v.float_value = token.v.floatvalue;
2935 static declaration_t *create_implicit_function(symbol_t *symbol,
2936 const source_position_t source_position)
2938 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
2939 ntype->function.return_type = type_int;
2940 ntype->function.unspecified_parameters = true;
2942 type_t *type = typehash_insert(ntype);
2947 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
2949 declaration->storage_class = STORAGE_CLASS_EXTERN;
2950 declaration->type = type;
2951 declaration->symbol = symbol;
2952 declaration->source_position = source_position;
2953 declaration->parent_context = global_context;
2955 environment_push(declaration);
2956 declaration->next = context->declarations;
2957 context->declarations = declaration;
2963 * Creates a return_type (func)(argument_type) function type if not
2966 * @param return_type the return type
2967 * @param argument_type the argument type
2969 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
2971 function_parameter_t *parameter
2972 = obstack_alloc(type_obst, sizeof(parameter[0]));
2973 memset(parameter, 0, sizeof(parameter[0]));
2974 parameter->type = argument_type;
2976 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2977 type->function.return_type = return_type;
2978 type->function.parameters = parameter;
2980 type_t *result = typehash_insert(type);
2981 if(result != type) {
2989 * Creates a function type for some function like builtins.
2991 * @param symbol the symbol describing the builtin
2993 static type_t *get_builtin_symbol_type(symbol_t *symbol)
2995 switch(symbol->ID) {
2996 case T___builtin_alloca:
2997 return make_function_1_type(type_void_ptr, type_size_t);
2998 case T___builtin_nan:
2999 return make_function_1_type(type_double, type_string);
3000 case T___builtin_nanf:
3001 return make_function_1_type(type_float, type_string);
3002 case T___builtin_nand:
3003 return make_function_1_type(type_long_double, type_string);
3004 case T___builtin_va_end:
3005 return make_function_1_type(type_void, type_valist);
3007 panic("not implemented builtin symbol found");
3012 * Performs automatic type cast as described in § 6.3.2.1.
3014 * @param orig_type the original type
3016 static type_t *automatic_type_conversion(type_t *orig_type)
3018 if(orig_type == NULL)
3021 type_t *type = skip_typeref(orig_type);
3022 if(is_type_array(type)) {
3023 array_type_t *array_type = &type->array;
3024 type_t *element_type = array_type->element_type;
3025 unsigned qualifiers = array_type->type.qualifiers;
3027 return make_pointer_type(element_type, qualifiers);
3030 if(is_type_function(type)) {
3031 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3038 * reverts the automatic casts of array to pointer types and function
3039 * to function-pointer types as defined § 6.3.2.1
3041 type_t *revert_automatic_type_conversion(const expression_t *expression)
3043 if(expression->base.datatype == NULL)
3046 switch(expression->kind) {
3047 case EXPR_REFERENCE: {
3048 const reference_expression_t *ref = &expression->reference;
3049 return ref->declaration->type;
3052 const select_expression_t *select = &expression->select;
3053 return select->compound_entry->type;
3055 case EXPR_UNARY_DEREFERENCE: {
3056 expression_t *value = expression->unary.value;
3057 type_t *type = skip_typeref(value->base.datatype);
3058 pointer_type_t *pointer_type = &type->pointer;
3060 return pointer_type->points_to;
3062 case EXPR_BUILTIN_SYMBOL: {
3063 const builtin_symbol_expression_t *builtin
3064 = &expression->builtin_symbol;
3065 return get_builtin_symbol_type(builtin->symbol);
3067 case EXPR_ARRAY_ACCESS: {
3068 const array_access_expression_t *array_access
3069 = &expression->array_access;
3070 const expression_t *array_ref = array_access->array_ref;
3071 type_t *type_left = skip_typeref(array_ref->base.datatype);
3072 assert(is_type_pointer(type_left));
3073 pointer_type_t *pointer_type = &type_left->pointer;
3074 return pointer_type->points_to;
3081 return expression->base.datatype;
3084 static expression_t *parse_reference(void)
3086 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3088 reference_expression_t *ref = &expression->reference;
3089 ref->symbol = token.v.symbol;
3091 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3093 source_position_t source_position = token.source_position;
3096 if(declaration == NULL) {
3097 if (! strict_mode && token.type == '(') {
3098 /* an implicitly defined function */
3099 warningf(HERE, "implicit declaration of function '%Y'",
3102 declaration = create_implicit_function(ref->symbol,
3105 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3110 type_t *type = declaration->type;
3112 /* we always do the auto-type conversions; the & and sizeof parser contains
3113 * code to revert this! */
3114 type = automatic_type_conversion(type);
3116 ref->declaration = declaration;
3117 ref->expression.datatype = type;
3122 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3126 /* TODO check if explicit cast is allowed and issue warnings/errors */
3129 static expression_t *parse_cast(void)
3131 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3133 cast->base.source_position = token.source_position;
3135 type_t *type = parse_typename();
3138 expression_t *value = parse_sub_expression(20);
3140 check_cast_allowed(value, type);
3142 cast->base.datatype = type;
3143 cast->unary.value = value;
3148 static expression_t *parse_statement_expression(void)
3150 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3152 statement_t *statement = parse_compound_statement();
3153 expression->statement.statement = statement;
3154 if(statement == NULL) {
3159 assert(statement->kind == STATEMENT_COMPOUND);
3160 compound_statement_t *compound_statement = &statement->compound;
3162 /* find last statement and use it's type */
3163 const statement_t *last_statement = NULL;
3164 const statement_t *iter = compound_statement->statements;
3165 for( ; iter != NULL; iter = iter->base.next) {
3166 last_statement = iter;
3169 if(last_statement->kind == STATEMENT_EXPRESSION) {
3170 const expression_statement_t *expression_statement
3171 = &last_statement->expression;
3172 expression->base.datatype
3173 = expression_statement->expression->base.datatype;
3175 expression->base.datatype = type_void;
3183 static expression_t *parse_brace_expression(void)
3187 switch(token.type) {
3189 /* gcc extension: a statement expression */
3190 return parse_statement_expression();
3194 return parse_cast();
3196 if(is_typedef_symbol(token.v.symbol)) {
3197 return parse_cast();
3201 expression_t *result = parse_expression();
3207 static expression_t *parse_function_keyword(void)
3212 if (current_function == NULL) {
3213 errorf(HERE, "'__func__' used outside of a function");
3216 string_literal_expression_t *expression
3217 = allocate_ast_zero(sizeof(expression[0]));
3219 expression->expression.kind = EXPR_FUNCTION;
3220 expression->expression.datatype = type_string;
3221 expression->value = current_function->symbol->string;
3223 return (expression_t*) expression;
3226 static expression_t *parse_pretty_function_keyword(void)
3228 eat(T___PRETTY_FUNCTION__);
3231 if (current_function == NULL) {
3232 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3235 string_literal_expression_t *expression
3236 = allocate_ast_zero(sizeof(expression[0]));
3238 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3239 expression->expression.datatype = type_string;
3240 expression->value = current_function->symbol->string;
3242 return (expression_t*) expression;
3245 static designator_t *parse_designator(void)
3247 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3249 if(token.type != T_IDENTIFIER) {
3250 parse_error_expected("while parsing member designator",
3255 result->symbol = token.v.symbol;
3258 designator_t *last_designator = result;
3260 if(token.type == '.') {
3262 if(token.type != T_IDENTIFIER) {
3263 parse_error_expected("while parsing member designator",
3268 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3269 designator->symbol = token.v.symbol;
3272 last_designator->next = designator;
3273 last_designator = designator;
3276 if(token.type == '[') {
3278 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3279 designator->array_access = parse_expression();
3280 if(designator->array_access == NULL) {
3286 last_designator->next = designator;
3287 last_designator = designator;
3296 static expression_t *parse_offsetof(void)
3298 eat(T___builtin_offsetof);
3300 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3301 expression->base.datatype = type_size_t;
3304 expression->offsetofe.type = parse_typename();
3306 expression->offsetofe.designator = parse_designator();
3312 static expression_t *parse_va_start(void)
3314 eat(T___builtin_va_start);
3316 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3319 expression->va_starte.ap = parse_assignment_expression();
3321 expression_t *const expr = parse_assignment_expression();
3322 if (expr->kind == EXPR_REFERENCE) {
3323 declaration_t *const decl = expr->reference.declaration;
3324 if (decl->parent_context == ¤t_function->context &&
3325 decl->next == NULL) {
3326 expression->va_starte.parameter = decl;
3331 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3333 return create_invalid_expression();
3336 static expression_t *parse_va_arg(void)
3338 eat(T___builtin_va_arg);
3340 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3343 expression->va_arge.ap = parse_assignment_expression();
3345 expression->base.datatype = parse_typename();
3351 static expression_t *parse_builtin_symbol(void)
3353 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3355 symbol_t *symbol = token.v.symbol;
3357 expression->builtin_symbol.symbol = symbol;
3360 type_t *type = get_builtin_symbol_type(symbol);
3361 type = automatic_type_conversion(type);
3363 expression->base.datatype = type;
3367 static expression_t *parse_builtin_constant(void)
3369 eat(T___builtin_constant_p);
3371 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3374 expression->builtin_constant.value = parse_assignment_expression();
3376 expression->base.datatype = type_int;
3381 static expression_t *parse_builtin_prefetch(void)
3383 eat(T___builtin_prefetch);
3385 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3388 expression->builtin_prefetch.adr = parse_assignment_expression();
3389 if (token.type == ',') {
3391 expression->builtin_prefetch.rw = parse_assignment_expression();
3393 if (token.type == ',') {
3395 expression->builtin_prefetch.locality = parse_assignment_expression();
3398 expression->base.datatype = type_void;
3403 static expression_t *parse_compare_builtin(void)
3405 expression_t *expression;
3407 switch(token.type) {
3408 case T___builtin_isgreater:
3409 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3411 case T___builtin_isgreaterequal:
3412 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3414 case T___builtin_isless:
3415 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3417 case T___builtin_islessequal:
3418 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3420 case T___builtin_islessgreater:
3421 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3423 case T___builtin_isunordered:
3424 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3427 panic("invalid compare builtin found");
3433 expression->binary.left = parse_assignment_expression();
3435 expression->binary.right = parse_assignment_expression();
3438 type_t *orig_type_left = expression->binary.left->base.datatype;
3439 type_t *orig_type_right = expression->binary.right->base.datatype;
3440 if(orig_type_left == NULL || orig_type_right == NULL)
3443 type_t *type_left = skip_typeref(orig_type_left);
3444 type_t *type_right = skip_typeref(orig_type_right);
3445 if(!is_type_floating(type_left) && !is_type_floating(type_right)) {
3446 type_error_incompatible("invalid operands in comparison",
3447 token.source_position, type_left, type_right);
3449 semantic_comparison(&expression->binary);
3455 static expression_t *parse_builtin_expect(void)
3457 eat(T___builtin_expect);
3459 expression_t *expression
3460 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3463 expression->binary.left = parse_assignment_expression();
3465 expression->binary.right = parse_constant_expression();
3468 expression->base.datatype = expression->binary.left->base.datatype;
3473 static expression_t *parse_assume(void) {
3476 expression_t *expression
3477 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3480 expression->unary.value = parse_assignment_expression();
3483 expression->base.datatype = type_void;
3487 static expression_t *parse_alignof(void) {
3490 expression_t *expression
3491 = allocate_expression_zero(EXPR_ALIGNOF);
3494 expression->alignofe.type = parse_typename();
3497 expression->base.datatype = type_size_t;
3501 static expression_t *parse_primary_expression(void)
3503 switch(token.type) {
3505 return parse_int_const();
3506 case T_FLOATINGPOINT:
3507 return parse_float_const();
3508 case T_STRING_LITERAL:
3509 return parse_string_const();
3510 case T_WIDE_STRING_LITERAL:
3511 return parse_wide_string_const();
3513 return parse_reference();
3514 case T___FUNCTION__:
3516 return parse_function_keyword();
3517 case T___PRETTY_FUNCTION__:
3518 return parse_pretty_function_keyword();
3519 case T___builtin_offsetof:
3520 return parse_offsetof();
3521 case T___builtin_va_start:
3522 return parse_va_start();
3523 case T___builtin_va_arg:
3524 return parse_va_arg();
3525 case T___builtin_expect:
3526 return parse_builtin_expect();
3527 case T___builtin_nanf:
3528 case T___builtin_alloca:
3529 case T___builtin_va_end:
3530 return parse_builtin_symbol();
3531 case T___builtin_isgreater:
3532 case T___builtin_isgreaterequal:
3533 case T___builtin_isless:
3534 case T___builtin_islessequal:
3535 case T___builtin_islessgreater:
3536 case T___builtin_isunordered:
3537 return parse_compare_builtin();
3538 case T___builtin_constant_p:
3539 return parse_builtin_constant();
3540 case T___builtin_prefetch:
3541 return parse_builtin_prefetch();
3543 return parse_alignof();
3545 return parse_assume();
3548 return parse_brace_expression();
3551 errorf(HERE, "unexpected token '%K'", &token);
3554 return create_invalid_expression();
3558 * Check if the expression has the character type and issue a warning then.
3560 static void check_for_char_index_type(const expression_t *expression) {
3561 type_t *type = expression->base.datatype;
3562 type_t *base_type = skip_typeref(type);
3564 if (base_type->base.kind == TYPE_ATOMIC) {
3565 if (base_type->atomic.akind == ATOMIC_TYPE_CHAR) {
3566 warningf(expression->base.source_position,
3567 "array subscript has type '%T'", type);
3572 static expression_t *parse_array_expression(unsigned precedence,
3579 expression_t *inside = parse_expression();
3581 array_access_expression_t *array_access
3582 = allocate_ast_zero(sizeof(array_access[0]));
3584 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3586 type_t *type_left = left->base.datatype;
3587 type_t *type_inside = inside->base.datatype;
3588 type_t *return_type = NULL;
3590 if(type_left != NULL && type_inside != NULL) {
3591 type_left = skip_typeref(type_left);
3592 type_inside = skip_typeref(type_inside);
3594 if(is_type_pointer(type_left)) {
3595 pointer_type_t *pointer = &type_left->pointer;
3596 return_type = pointer->points_to;
3597 array_access->array_ref = left;
3598 array_access->index = inside;
3599 check_for_char_index_type(inside);
3600 } else if(is_type_pointer(type_inside)) {
3601 pointer_type_t *pointer = &type_inside->pointer;
3602 return_type = pointer->points_to;
3603 array_access->array_ref = inside;
3604 array_access->index = left;
3605 array_access->flipped = true;
3606 check_for_char_index_type(left);
3608 errorf(HERE, "array access on object with non-pointer types '%T', '%T'", type_left, type_inside);
3611 array_access->array_ref = left;
3612 array_access->index = inside;
3615 if(token.type != ']') {
3616 parse_error_expected("Problem while parsing array access", ']', 0);
3617 return (expression_t*) array_access;
3621 return_type = automatic_type_conversion(return_type);
3622 array_access->expression.datatype = return_type;
3624 return (expression_t*) array_access;
3627 static expression_t *parse_sizeof(unsigned precedence)
3631 sizeof_expression_t *sizeof_expression
3632 = allocate_ast_zero(sizeof(sizeof_expression[0]));
3633 sizeof_expression->expression.kind = EXPR_SIZEOF;
3634 sizeof_expression->expression.datatype = type_size_t;
3636 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3638 sizeof_expression->type = parse_typename();
3641 expression_t *expression = parse_sub_expression(precedence);
3642 expression->base.datatype = revert_automatic_type_conversion(expression);
3644 sizeof_expression->type = expression->base.datatype;
3645 sizeof_expression->size_expression = expression;
3648 return (expression_t*) sizeof_expression;
3651 static expression_t *parse_select_expression(unsigned precedence,
3652 expression_t *compound)
3655 assert(token.type == '.' || token.type == T_MINUSGREATER);
3657 bool is_pointer = (token.type == T_MINUSGREATER);
3660 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3661 select->select.compound = compound;
3663 if(token.type != T_IDENTIFIER) {
3664 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3667 symbol_t *symbol = token.v.symbol;
3668 select->select.symbol = symbol;
3671 type_t *orig_type = compound->base.datatype;
3672 if(orig_type == NULL)
3673 return create_invalid_expression();
3675 type_t *type = skip_typeref(orig_type);
3677 type_t *type_left = type;
3679 if(type->kind != TYPE_POINTER) {
3680 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3681 return create_invalid_expression();
3683 pointer_type_t *pointer_type = &type->pointer;
3684 type_left = pointer_type->points_to;
3686 type_left = skip_typeref(type_left);
3688 if(type_left->kind != TYPE_COMPOUND_STRUCT
3689 && type_left->kind != TYPE_COMPOUND_UNION) {
3690 errorf(HERE, "request for member '%Y' in something not a struct or "
3691 "union, but '%T'", symbol, type_left);
3692 return create_invalid_expression();
3695 compound_type_t *compound_type = &type_left->compound;
3696 declaration_t *declaration = compound_type->declaration;
3698 if(!declaration->init.is_defined) {
3699 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3701 return create_invalid_expression();
3704 declaration_t *iter = declaration->context.declarations;
3705 for( ; iter != NULL; iter = iter->next) {
3706 if(iter->symbol == symbol) {
3711 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3712 return create_invalid_expression();
3715 /* we always do the auto-type conversions; the & and sizeof parser contains
3716 * code to revert this! */
3717 type_t *expression_type = automatic_type_conversion(iter->type);
3719 select->select.compound_entry = iter;
3720 select->base.datatype = expression_type;
3722 if(expression_type->kind == TYPE_BITFIELD) {
3723 expression_t *extract
3724 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3725 extract->unary.value = select;
3726 extract->base.datatype = expression_type->bitfield.base;
3735 * Parse a call expression, ie. expression '( ... )'.
3737 * @param expression the function address
3739 static expression_t *parse_call_expression(unsigned precedence,
3740 expression_t *expression)
3743 expression_t *result = allocate_expression_zero(EXPR_CALL);
3745 call_expression_t *call = &result->call;
3746 call->function = expression;
3748 function_type_t *function_type = NULL;
3749 type_t *orig_type = expression->base.datatype;
3750 if(orig_type != NULL) {
3751 type_t *type = skip_typeref(orig_type);
3753 if(is_type_pointer(type)) {
3754 pointer_type_t *pointer_type = &type->pointer;
3756 type = skip_typeref(pointer_type->points_to);
3758 if (is_type_function(type)) {
3759 function_type = &type->function;
3760 call->expression.datatype = function_type->return_type;
3763 if(function_type == NULL) {
3764 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3766 function_type = NULL;
3767 call->expression.datatype = NULL;
3771 /* parse arguments */
3774 if(token.type != ')') {
3775 call_argument_t *last_argument = NULL;
3778 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3780 argument->expression = parse_assignment_expression();
3781 if(last_argument == NULL) {
3782 call->arguments = argument;
3784 last_argument->next = argument;
3786 last_argument = argument;
3788 if(token.type != ',')
3795 if(function_type != NULL) {
3796 function_parameter_t *parameter = function_type->parameters;
3797 call_argument_t *argument = call->arguments;
3798 for( ; parameter != NULL && argument != NULL;
3799 parameter = parameter->next, argument = argument->next) {
3800 type_t *expected_type = parameter->type;
3801 /* TODO report context in error messages */
3802 argument->expression = create_implicit_cast(argument->expression,
3805 /* too few parameters */
3806 if(parameter != NULL) {
3807 errorf(HERE, "too few arguments to function '%E'", expression);
3808 } else if(argument != NULL) {
3809 /* too many parameters */
3810 if(!function_type->variadic
3811 && !function_type->unspecified_parameters) {
3812 errorf(HERE, "too many arguments to function '%E'", expression);
3814 /* do default promotion */
3815 for( ; argument != NULL; argument = argument->next) {
3816 type_t *type = argument->expression->base.datatype;
3821 type = skip_typeref(type);
3822 if(is_type_integer(type)) {
3823 type = promote_integer(type);
3824 } else if(type == type_float) {
3828 argument->expression
3829 = create_implicit_cast(argument->expression, type);
3832 check_format(&result->call);
3835 check_format(&result->call);
3842 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3844 static bool same_compound_type(const type_t *type1, const type_t *type2)
3846 if(!is_type_compound(type1))
3848 if(type1->kind != type2->kind)
3851 const compound_type_t *compound1 = &type1->compound;
3852 const compound_type_t *compound2 = &type2->compound;
3854 return compound1->declaration == compound2->declaration;
3858 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3860 * @param expression the conditional expression
3862 static expression_t *parse_conditional_expression(unsigned precedence,
3863 expression_t *expression)
3867 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3869 conditional_expression_t *conditional = &result->conditional;
3870 conditional->condition = expression;
3873 type_t *condition_type_orig = expression->base.datatype;
3874 if(condition_type_orig != NULL) {
3875 type_t *condition_type = skip_typeref(condition_type_orig);
3876 if(condition_type != NULL && !is_type_scalar(condition_type)) {
3877 type_error("expected a scalar type in conditional condition",
3878 expression->base.source_position, condition_type_orig);
3882 expression_t *true_expression = parse_expression();
3884 expression_t *false_expression = parse_sub_expression(precedence);
3886 conditional->true_expression = true_expression;
3887 conditional->false_expression = false_expression;
3889 type_t *orig_true_type = true_expression->base.datatype;
3890 type_t *orig_false_type = false_expression->base.datatype;
3891 if(orig_true_type == NULL || orig_false_type == NULL)
3894 type_t *true_type = skip_typeref(orig_true_type);
3895 type_t *false_type = skip_typeref(orig_false_type);
3898 type_t *result_type = NULL;
3899 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
3900 result_type = semantic_arithmetic(true_type, false_type);
3902 true_expression = create_implicit_cast(true_expression, result_type);
3903 false_expression = create_implicit_cast(false_expression, result_type);
3905 conditional->true_expression = true_expression;
3906 conditional->false_expression = false_expression;
3907 conditional->expression.datatype = result_type;
3908 } else if (same_compound_type(true_type, false_type)
3909 || (is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
3910 is_type_atomic(false_type, ATOMIC_TYPE_VOID))) {
3911 /* just take 1 of the 2 types */
3912 result_type = true_type;
3913 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
3914 && pointers_compatible(true_type, false_type)) {
3916 result_type = true_type;
3919 type_error_incompatible("while parsing conditional",
3920 expression->base.source_position, true_type,
3924 conditional->expression.datatype = result_type;
3929 * Parse an extension expression.
3931 static expression_t *parse_extension(unsigned precedence)
3933 eat(T___extension__);
3935 /* TODO enable extensions */
3936 expression_t *expression = parse_sub_expression(precedence);
3937 /* TODO disable extensions */
3941 static expression_t *parse_builtin_classify_type(const unsigned precedence)
3943 eat(T___builtin_classify_type);
3945 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
3946 result->base.datatype = type_int;
3949 expression_t *expression = parse_sub_expression(precedence);
3951 result->classify_type.type_expression = expression;
3956 static void semantic_incdec(unary_expression_t *expression)
3958 type_t *orig_type = expression->value->base.datatype;
3959 if(orig_type == NULL)
3962 type_t *type = skip_typeref(orig_type);
3963 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
3964 /* TODO: improve error message */
3965 errorf(HERE, "operation needs an arithmetic or pointer type");
3969 expression->expression.datatype = orig_type;
3972 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
3974 type_t *orig_type = expression->value->base.datatype;
3975 if(orig_type == NULL)
3978 type_t *type = skip_typeref(orig_type);
3979 if(!is_type_arithmetic(type)) {
3980 /* TODO: improve error message */
3981 errorf(HERE, "operation needs an arithmetic type");
3985 expression->expression.datatype = orig_type;
3988 static void semantic_unexpr_scalar(unary_expression_t *expression)
3990 type_t *orig_type = expression->value->base.datatype;
3991 if(orig_type == NULL)
3994 type_t *type = skip_typeref(orig_type);
3995 if (!is_type_scalar(type)) {
3996 errorf(HERE, "operand of ! must be of scalar type");
4000 expression->expression.datatype = orig_type;
4003 static void semantic_unexpr_integer(unary_expression_t *expression)
4005 type_t *orig_type = expression->value->base.datatype;
4006 if(orig_type == NULL)
4009 type_t *type = skip_typeref(orig_type);
4010 if (!is_type_integer(type)) {
4011 errorf(HERE, "operand of ~ must be of integer type");
4015 expression->expression.datatype = orig_type;
4018 static void semantic_dereference(unary_expression_t *expression)
4020 type_t *orig_type = expression->value->base.datatype;
4021 if(orig_type == NULL)
4024 type_t *type = skip_typeref(orig_type);
4025 if(!is_type_pointer(type)) {
4026 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4030 pointer_type_t *pointer_type = &type->pointer;
4031 type_t *result_type = pointer_type->points_to;
4033 result_type = automatic_type_conversion(result_type);
4034 expression->expression.datatype = result_type;
4038 * Check the semantic of the address taken expression.
4040 static void semantic_take_addr(unary_expression_t *expression)
4042 expression_t *value = expression->value;
4043 value->base.datatype = revert_automatic_type_conversion(value);
4045 type_t *orig_type = value->base.datatype;
4046 if(orig_type == NULL)
4049 if(value->kind == EXPR_REFERENCE) {
4050 reference_expression_t *reference = (reference_expression_t*) value;
4051 declaration_t *declaration = reference->declaration;
4052 if(declaration != NULL) {
4053 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4054 errorf(expression->expression.source_position,
4055 "address of register variable '%Y' requested",
4056 declaration->symbol);
4058 declaration->address_taken = 1;
4062 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4065 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4066 static expression_t *parse_##unexpression_type(unsigned precedence) \
4070 expression_t *unary_expression \
4071 = allocate_expression_zero(unexpression_type); \
4072 unary_expression->base.source_position = HERE; \
4073 unary_expression->unary.value = parse_sub_expression(precedence); \
4075 sfunc(&unary_expression->unary); \
4077 return unary_expression; \
4080 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4081 semantic_unexpr_arithmetic)
4082 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4083 semantic_unexpr_arithmetic)
4084 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4085 semantic_unexpr_scalar)
4086 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4087 semantic_dereference)
4088 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4090 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4091 semantic_unexpr_integer)
4092 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4094 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4097 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4099 static expression_t *parse_##unexpression_type(unsigned precedence, \
4100 expression_t *left) \
4102 (void) precedence; \
4105 expression_t *unary_expression \
4106 = allocate_expression_zero(unexpression_type); \
4107 unary_expression->unary.value = left; \
4109 sfunc(&unary_expression->unary); \
4111 return unary_expression; \
4114 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4115 EXPR_UNARY_POSTFIX_INCREMENT,
4117 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4118 EXPR_UNARY_POSTFIX_DECREMENT,
4121 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4123 /* TODO: handle complex + imaginary types */
4125 /* § 6.3.1.8 Usual arithmetic conversions */
4126 if(type_left == type_long_double || type_right == type_long_double) {
4127 return type_long_double;
4128 } else if(type_left == type_double || type_right == type_double) {
4130 } else if(type_left == type_float || type_right == type_float) {
4134 type_right = promote_integer(type_right);
4135 type_left = promote_integer(type_left);
4137 if(type_left == type_right)
4140 bool signed_left = is_type_signed(type_left);
4141 bool signed_right = is_type_signed(type_right);
4142 int rank_left = get_rank(type_left);
4143 int rank_right = get_rank(type_right);
4144 if(rank_left < rank_right) {
4145 if(signed_left == signed_right || !signed_right) {
4151 if(signed_left == signed_right || !signed_left) {
4160 * Check the semantic restrictions for a binary expression.
4162 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4164 expression_t *left = expression->left;
4165 expression_t *right = expression->right;
4166 type_t *orig_type_left = left->base.datatype;
4167 type_t *orig_type_right = right->base.datatype;
4169 if(orig_type_left == NULL || orig_type_right == NULL)
4172 type_t *type_left = skip_typeref(orig_type_left);
4173 type_t *type_right = skip_typeref(orig_type_right);
4175 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4176 /* TODO: improve error message */
4177 errorf(HERE, "operation needs arithmetic types");
4181 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4182 expression->left = create_implicit_cast(left, arithmetic_type);
4183 expression->right = create_implicit_cast(right, arithmetic_type);
4184 expression->expression.datatype = arithmetic_type;
4187 static void semantic_shift_op(binary_expression_t *expression)
4189 expression_t *left = expression->left;
4190 expression_t *right = expression->right;
4191 type_t *orig_type_left = left->base.datatype;
4192 type_t *orig_type_right = right->base.datatype;
4194 if(orig_type_left == NULL || orig_type_right == NULL)
4197 type_t *type_left = skip_typeref(orig_type_left);
4198 type_t *type_right = skip_typeref(orig_type_right);
4200 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4201 /* TODO: improve error message */
4202 errorf(HERE, "operation needs integer types");
4206 type_left = promote_integer(type_left);
4207 type_right = promote_integer(type_right);
4209 expression->left = create_implicit_cast(left, type_left);
4210 expression->right = create_implicit_cast(right, type_right);
4211 expression->expression.datatype = type_left;
4214 static void semantic_add(binary_expression_t *expression)
4216 expression_t *left = expression->left;
4217 expression_t *right = expression->right;
4218 type_t *orig_type_left = left->base.datatype;
4219 type_t *orig_type_right = right->base.datatype;
4221 if(orig_type_left == NULL || orig_type_right == NULL)
4224 type_t *type_left = skip_typeref(orig_type_left);
4225 type_t *type_right = skip_typeref(orig_type_right);
4228 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4229 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4230 expression->left = create_implicit_cast(left, arithmetic_type);
4231 expression->right = create_implicit_cast(right, arithmetic_type);
4232 expression->expression.datatype = arithmetic_type;
4234 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4235 expression->expression.datatype = type_left;
4236 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4237 expression->expression.datatype = type_right;
4239 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4243 static void semantic_sub(binary_expression_t *expression)
4245 expression_t *left = expression->left;
4246 expression_t *right = expression->right;
4247 type_t *orig_type_left = left->base.datatype;
4248 type_t *orig_type_right = right->base.datatype;
4250 if(orig_type_left == NULL || orig_type_right == NULL)
4253 type_t *type_left = skip_typeref(orig_type_left);
4254 type_t *type_right = skip_typeref(orig_type_right);
4257 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4258 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4259 expression->left = create_implicit_cast(left, arithmetic_type);
4260 expression->right = create_implicit_cast(right, arithmetic_type);
4261 expression->expression.datatype = arithmetic_type;
4263 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4264 expression->expression.datatype = type_left;
4265 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4266 if(!pointers_compatible(type_left, type_right)) {
4267 errorf(HERE, "pointers to incompatible objects to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4269 expression->expression.datatype = type_ptrdiff_t;
4272 errorf(HERE, "invalid operands to binary - ('%T', '%T')", orig_type_left, orig_type_right);
4276 static void semantic_comparison(binary_expression_t *expression)
4278 expression_t *left = expression->left;
4279 expression_t *right = expression->right;
4280 type_t *orig_type_left = left->base.datatype;
4281 type_t *orig_type_right = right->base.datatype;
4283 if(orig_type_left == NULL || orig_type_right == NULL)
4286 type_t *type_left = skip_typeref(orig_type_left);
4287 type_t *type_right = skip_typeref(orig_type_right);
4289 /* TODO non-arithmetic types */
4290 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4291 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4292 expression->left = create_implicit_cast(left, arithmetic_type);
4293 expression->right = create_implicit_cast(right, arithmetic_type);
4294 expression->expression.datatype = arithmetic_type;
4295 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4296 /* TODO check compatibility */
4297 } else if (is_type_pointer(type_left)) {
4298 expression->right = create_implicit_cast(right, type_left);
4299 } else if (is_type_pointer(type_right)) {
4300 expression->left = create_implicit_cast(left, type_right);
4302 type_error_incompatible("invalid operands in comparison",
4303 token.source_position, type_left, type_right);
4305 expression->expression.datatype = type_int;
4308 static void semantic_arithmetic_assign(binary_expression_t *expression)
4310 expression_t *left = expression->left;
4311 expression_t *right = expression->right;
4312 type_t *orig_type_left = left->base.datatype;
4313 type_t *orig_type_right = right->base.datatype;
4315 if(orig_type_left == NULL || orig_type_right == NULL)
4318 type_t *type_left = skip_typeref(orig_type_left);
4319 type_t *type_right = skip_typeref(orig_type_right);
4321 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4322 /* TODO: improve error message */
4323 errorf(HERE, "operation needs arithmetic types");
4327 /* combined instructions are tricky. We can't create an implicit cast on
4328 * the left side, because we need the uncasted form for the store.
4329 * The ast2firm pass has to know that left_type must be right_type
4330 * for the arithmetic operation and create a cast by itself */
4331 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4332 expression->right = create_implicit_cast(right, arithmetic_type);
4333 expression->expression.datatype = type_left;
4336 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4338 expression_t *left = expression->left;
4339 expression_t *right = expression->right;
4340 type_t *orig_type_left = left->base.datatype;
4341 type_t *orig_type_right = right->base.datatype;
4343 if(orig_type_left == NULL || orig_type_right == NULL)
4346 type_t *type_left = skip_typeref(orig_type_left);
4347 type_t *type_right = skip_typeref(orig_type_right);
4349 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4350 /* combined instructions are tricky. We can't create an implicit cast on
4351 * the left side, because we need the uncasted form for the store.
4352 * The ast2firm pass has to know that left_type must be right_type
4353 * for the arithmetic operation and create a cast by itself */
4354 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4355 expression->right = create_implicit_cast(right, arithmetic_type);
4356 expression->expression.datatype = type_left;
4357 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4358 expression->expression.datatype = type_left;
4360 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4366 * Check the semantic restrictions of a logical expression.
4368 static void semantic_logical_op(binary_expression_t *expression)
4370 expression_t *left = expression->left;
4371 expression_t *right = expression->right;
4372 type_t *orig_type_left = left->base.datatype;
4373 type_t *orig_type_right = right->base.datatype;
4375 if(orig_type_left == NULL || orig_type_right == NULL)
4378 type_t *type_left = skip_typeref(orig_type_left);
4379 type_t *type_right = skip_typeref(orig_type_right);
4381 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4382 /* TODO: improve error message */
4383 errorf(HERE, "operation needs scalar types");
4387 expression->expression.datatype = type_int;
4391 * Checks if a compound type has constant fields.
4393 static bool has_const_fields(const compound_type_t *type)
4395 const context_t *context = &type->declaration->context;
4396 const declaration_t *declaration = context->declarations;
4398 for (; declaration != NULL; declaration = declaration->next) {
4399 if (declaration->namespc != NAMESPACE_NORMAL)
4402 const type_t *decl_type = skip_typeref(declaration->type);
4403 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4411 * Check the semantic restrictions of a binary assign expression.
4413 static void semantic_binexpr_assign(binary_expression_t *expression)
4415 expression_t *left = expression->left;
4416 type_t *orig_type_left = left->base.datatype;
4418 if(orig_type_left == NULL)
4421 type_t *type_left = revert_automatic_type_conversion(left);
4422 type_left = skip_typeref(orig_type_left);
4424 /* must be a modifiable lvalue */
4425 if (is_type_array(type_left)) {
4426 errorf(HERE, "cannot assign to arrays ('%E')", left);
4429 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4430 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4434 if(is_type_incomplete(type_left)) {
4436 "left-hand side of assignment '%E' has incomplete type '%T'",
4437 left, orig_type_left);
4440 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4441 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4442 left, orig_type_left);
4446 semantic_assign(orig_type_left, &expression->right, "assignment");
4448 expression->expression.datatype = orig_type_left;
4451 static void semantic_comma(binary_expression_t *expression)
4453 expression->expression.datatype = expression->right->base.datatype;
4456 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4457 static expression_t *parse_##binexpression_type(unsigned precedence, \
4458 expression_t *left) \
4462 expression_t *right = parse_sub_expression(precedence + lr); \
4464 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4465 binexpr->binary.left = left; \
4466 binexpr->binary.right = right; \
4467 sfunc(&binexpr->binary); \
4472 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4473 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4474 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4475 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4476 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4477 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4478 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4479 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4480 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4482 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4483 semantic_comparison, 1)
4484 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4485 semantic_comparison, 1)
4486 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4487 semantic_comparison, 1)
4488 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4489 semantic_comparison, 1)
4491 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4492 semantic_binexpr_arithmetic, 1)
4493 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4494 semantic_binexpr_arithmetic, 1)
4495 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4496 semantic_binexpr_arithmetic, 1)
4497 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4498 semantic_logical_op, 1)
4499 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4500 semantic_logical_op, 1)
4501 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4502 semantic_shift_op, 1)
4503 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4504 semantic_shift_op, 1)
4505 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4506 semantic_arithmetic_addsubb_assign, 0)
4507 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4508 semantic_arithmetic_addsubb_assign, 0)
4509 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4510 semantic_arithmetic_assign, 0)
4511 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4512 semantic_arithmetic_assign, 0)
4513 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4514 semantic_arithmetic_assign, 0)
4515 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4516 semantic_arithmetic_assign, 0)
4517 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4518 semantic_arithmetic_assign, 0)
4519 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4520 semantic_arithmetic_assign, 0)
4521 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4522 semantic_arithmetic_assign, 0)
4523 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4524 semantic_arithmetic_assign, 0)
4526 static expression_t *parse_sub_expression(unsigned precedence)
4528 if(token.type < 0) {
4529 return expected_expression_error();
4532 expression_parser_function_t *parser
4533 = &expression_parsers[token.type];
4534 source_position_t source_position = token.source_position;
4537 if(parser->parser != NULL) {
4538 left = parser->parser(parser->precedence);
4540 left = parse_primary_expression();
4542 assert(left != NULL);
4543 left->base.source_position = source_position;
4546 if(token.type < 0) {
4547 return expected_expression_error();
4550 parser = &expression_parsers[token.type];
4551 if(parser->infix_parser == NULL)
4553 if(parser->infix_precedence < precedence)
4556 left = parser->infix_parser(parser->infix_precedence, left);
4558 assert(left != NULL);
4559 assert(left->kind != EXPR_UNKNOWN);
4560 left->base.source_position = source_position;
4567 * Parse an expression.
4569 static expression_t *parse_expression(void)
4571 return parse_sub_expression(1);
4575 * Register a parser for a prefix-like operator with given precedence.
4577 * @param parser the parser function
4578 * @param token_type the token type of the prefix token
4579 * @param precedence the precedence of the operator
4581 static void register_expression_parser(parse_expression_function parser,
4582 int token_type, unsigned precedence)
4584 expression_parser_function_t *entry = &expression_parsers[token_type];
4586 if(entry->parser != NULL) {
4587 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4588 panic("trying to register multiple expression parsers for a token");
4590 entry->parser = parser;
4591 entry->precedence = precedence;
4595 * Register a parser for an infix operator with given precedence.
4597 * @param parser the parser function
4598 * @param token_type the token type of the infix operator
4599 * @param precedence the precedence of the operator
4601 static void register_infix_parser(parse_expression_infix_function parser,
4602 int token_type, unsigned precedence)
4604 expression_parser_function_t *entry = &expression_parsers[token_type];
4606 if(entry->infix_parser != NULL) {
4607 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4608 panic("trying to register multiple infix expression parsers for a "
4611 entry->infix_parser = parser;
4612 entry->infix_precedence = precedence;
4616 * Initialize the expression parsers.
4618 static void init_expression_parsers(void)
4620 memset(&expression_parsers, 0, sizeof(expression_parsers));
4622 register_infix_parser(parse_array_expression, '[', 30);
4623 register_infix_parser(parse_call_expression, '(', 30);
4624 register_infix_parser(parse_select_expression, '.', 30);
4625 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4626 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4628 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4631 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4632 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4633 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4634 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4635 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4636 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4637 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4638 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4639 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4640 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4641 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4642 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4643 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4644 T_EXCLAMATIONMARKEQUAL, 13);
4645 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4646 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4647 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4648 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4649 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4650 register_infix_parser(parse_conditional_expression, '?', 7);
4651 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4652 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4653 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4654 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4655 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4656 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4657 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4658 T_LESSLESSEQUAL, 2);
4659 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4660 T_GREATERGREATEREQUAL, 2);
4661 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4663 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4665 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4668 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4670 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4671 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4672 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4673 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4674 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4675 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4676 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4678 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4680 register_expression_parser(parse_sizeof, T_sizeof, 25);
4681 register_expression_parser(parse_extension, T___extension__, 25);
4682 register_expression_parser(parse_builtin_classify_type,
4683 T___builtin_classify_type, 25);
4687 * Parse a asm statement constraints specification.
4689 static asm_constraint_t *parse_asm_constraints(void)
4691 asm_constraint_t *result = NULL;
4692 asm_constraint_t *last = NULL;
4694 while(token.type == T_STRING_LITERAL || token.type == '[') {
4695 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4696 memset(constraint, 0, sizeof(constraint[0]));
4698 if(token.type == '[') {
4700 if(token.type != T_IDENTIFIER) {
4701 parse_error_expected("while parsing asm constraint",
4705 constraint->symbol = token.v.symbol;
4710 constraint->constraints = parse_string_literals();
4712 constraint->expression = parse_expression();
4716 last->next = constraint;
4718 result = constraint;
4722 if(token.type != ',')
4731 * Parse a asm statement clobber specification.
4733 static asm_clobber_t *parse_asm_clobbers(void)
4735 asm_clobber_t *result = NULL;
4736 asm_clobber_t *last = NULL;
4738 while(token.type == T_STRING_LITERAL) {
4739 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4740 clobber->clobber = parse_string_literals();
4743 last->next = clobber;
4749 if(token.type != ',')
4758 * Parse an asm statement.
4760 static statement_t *parse_asm_statement(void)
4764 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4765 statement->base.source_position = token.source_position;
4767 asm_statement_t *asm_statement = &statement->asms;
4769 if(token.type == T_volatile) {
4771 asm_statement->is_volatile = true;
4775 asm_statement->asm_text = parse_string_literals();
4777 if(token.type != ':')
4781 asm_statement->inputs = parse_asm_constraints();
4782 if(token.type != ':')
4786 asm_statement->outputs = parse_asm_constraints();
4787 if(token.type != ':')
4791 asm_statement->clobbers = parse_asm_clobbers();
4800 * Parse a case statement.
4802 static statement_t *parse_case_statement(void)
4806 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4808 statement->base.source_position = token.source_position;
4809 statement->case_label.expression = parse_expression();
4812 statement->case_label.label_statement = parse_statement();
4818 * Parse a default statement.
4820 static statement_t *parse_default_statement(void)
4824 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
4826 statement->base.source_position = token.source_position;
4829 statement->label.label_statement = parse_statement();
4835 * Return the declaration for a given label symbol or create a new one.
4837 static declaration_t *get_label(symbol_t *symbol)
4839 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
4840 assert(current_function != NULL);
4841 /* if we found a label in the same function, then we already created the
4843 if(candidate != NULL
4844 && candidate->parent_context == ¤t_function->context) {
4848 /* otherwise we need to create a new one */
4849 declaration_t *declaration = allocate_ast_zero(sizeof(declaration[0]));
4850 declaration->namespc = NAMESPACE_LABEL;
4851 declaration->symbol = symbol;
4853 label_push(declaration);
4859 * Parse a label statement.
4861 static statement_t *parse_label_statement(void)
4863 assert(token.type == T_IDENTIFIER);
4864 symbol_t *symbol = token.v.symbol;
4867 declaration_t *label = get_label(symbol);
4869 /* if source position is already set then the label is defined twice,
4870 * otherwise it was just mentioned in a goto so far */
4871 if(label->source_position.input_name != NULL) {
4872 errorf(HERE, "duplicate label '%Y'", symbol);
4873 errorf(label->source_position, "previous definition of '%Y' was here",
4876 label->source_position = token.source_position;
4879 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
4881 label_statement->statement.kind = STATEMENT_LABEL;
4882 label_statement->statement.source_position = token.source_position;
4883 label_statement->label = label;
4887 if(token.type == '}') {
4888 /* TODO only warn? */
4889 errorf(HERE, "label at end of compound statement");
4890 return (statement_t*) label_statement;
4892 label_statement->label_statement = parse_statement();
4895 return (statement_t*) label_statement;
4899 * Parse an if statement.
4901 static statement_t *parse_if(void)
4905 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4906 statement->statement.kind = STATEMENT_IF;
4907 statement->statement.source_position = token.source_position;
4910 statement->condition = parse_expression();
4913 statement->true_statement = parse_statement();
4914 if(token.type == T_else) {
4916 statement->false_statement = parse_statement();
4919 return (statement_t*) statement;
4923 * Parse a switch statement.
4925 static statement_t *parse_switch(void)
4929 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4930 statement->statement.kind = STATEMENT_SWITCH;
4931 statement->statement.source_position = token.source_position;
4934 expression_t *const expr = parse_expression();
4935 type_t *const type = promote_integer(skip_typeref(expr->base.datatype));
4936 statement->expression = create_implicit_cast(expr, type);
4938 statement->body = parse_statement();
4940 return (statement_t*) statement;
4944 * Parse a while statement.
4946 static statement_t *parse_while(void)
4950 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4951 statement->statement.kind = STATEMENT_WHILE;
4952 statement->statement.source_position = token.source_position;
4955 statement->condition = parse_expression();
4957 statement->body = parse_statement();
4959 return (statement_t*) statement;
4963 * Parse a do statement.
4965 static statement_t *parse_do(void)
4969 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4970 statement->statement.kind = STATEMENT_DO_WHILE;
4971 statement->statement.source_position = token.source_position;
4973 statement->body = parse_statement();
4976 statement->condition = parse_expression();
4980 return (statement_t*) statement;
4984 * Parse a for statement.
4986 static statement_t *parse_for(void)
4990 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
4991 statement->statement.kind = STATEMENT_FOR;
4992 statement->statement.source_position = token.source_position;
4996 int top = environment_top();
4997 context_t *last_context = context;
4998 set_context(&statement->context);
5000 if(token.type != ';') {
5001 if(is_declaration_specifier(&token, false)) {
5002 parse_declaration(record_declaration);
5004 statement->initialisation = parse_expression();
5011 if(token.type != ';') {
5012 statement->condition = parse_expression();
5015 if(token.type != ')') {
5016 statement->step = parse_expression();
5019 statement->body = parse_statement();
5021 assert(context == &statement->context);
5022 set_context(last_context);
5023 environment_pop_to(top);
5025 return (statement_t*) statement;
5029 * Parse a goto statement.
5031 static statement_t *parse_goto(void)
5035 if(token.type != T_IDENTIFIER) {
5036 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5040 symbol_t *symbol = token.v.symbol;
5043 declaration_t *label = get_label(symbol);
5045 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5047 statement->statement.kind = STATEMENT_GOTO;
5048 statement->statement.source_position = token.source_position;
5050 statement->label = label;
5054 return (statement_t*) statement;
5058 * Parse a continue statement.
5060 static statement_t *parse_continue(void)
5065 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5066 statement->kind = STATEMENT_CONTINUE;
5067 statement->base.source_position = token.source_position;
5073 * Parse a break statement.
5075 static statement_t *parse_break(void)
5080 statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5081 statement->kind = STATEMENT_BREAK;
5082 statement->base.source_position = token.source_position;
5088 * Check if a given declaration represents a local variable.
5090 static bool is_local_var_declaration(const declaration_t *declaration) {
5091 switch ((storage_class_tag_t) declaration->storage_class) {
5092 case STORAGE_CLASS_NONE:
5093 case STORAGE_CLASS_AUTO:
5094 case STORAGE_CLASS_REGISTER: {
5095 const type_t *type = skip_typeref(declaration->type);
5096 if(is_type_function(type)) {
5108 * Check if a given expression represents a local variable.
5110 static bool is_local_variable(const expression_t *expression)
5112 if (expression->base.kind != EXPR_REFERENCE) {
5115 const declaration_t *declaration = expression->reference.declaration;
5116 return is_local_var_declaration(declaration);
5120 * Parse a return statement.
5122 static statement_t *parse_return(void)
5126 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5128 statement->statement.kind = STATEMENT_RETURN;
5129 statement->statement.source_position = token.source_position;
5131 assert(is_type_function(current_function->type));
5132 function_type_t *function_type = ¤t_function->type->function;
5133 type_t *return_type = function_type->return_type;
5135 expression_t *return_value = NULL;
5136 if(token.type != ';') {
5137 return_value = parse_expression();
5141 if(return_type == NULL)
5142 return (statement_t*) statement;
5143 if(return_value != NULL && return_value->base.datatype == NULL)
5144 return (statement_t*) statement;
5146 return_type = skip_typeref(return_type);
5148 if(return_value != NULL) {
5149 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5151 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5152 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5153 warningf(statement->statement.source_position,
5154 "'return' with a value, in function returning void");
5155 return_value = NULL;
5157 if(return_type != NULL) {
5158 semantic_assign(return_type, &return_value, "'return'");
5161 /* check for returning address of a local var */
5162 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5163 const expression_t *expression = return_value->unary.value;
5164 if (is_local_variable(expression)) {
5165 warningf(statement->statement.source_position,
5166 "function returns address of local variable");
5170 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5171 warningf(statement->statement.source_position,
5172 "'return' without value, in function returning non-void");
5175 statement->return_value = return_value;
5177 return (statement_t*) statement;
5181 * Parse a declaration statement.
5183 static statement_t *parse_declaration_statement(void)
5185 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5187 statement->base.source_position = token.source_position;
5189 declaration_t *before = last_declaration;
5190 parse_declaration(record_declaration);
5192 if(before == NULL) {
5193 statement->declaration.declarations_begin = context->declarations;
5195 statement->declaration.declarations_begin = before->next;
5197 statement->declaration.declarations_end = last_declaration;
5203 * Parse an expression statement, ie. expr ';'.
5205 static statement_t *parse_expression_statement(void)
5207 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5209 statement->base.source_position = token.source_position;
5210 statement->expression.expression = parse_expression();
5218 * Parse a statement.
5220 static statement_t *parse_statement(void)
5222 statement_t *statement = NULL;
5224 /* declaration or statement */
5225 switch(token.type) {
5227 statement = parse_asm_statement();
5231 statement = parse_case_statement();
5235 statement = parse_default_statement();
5239 statement = parse_compound_statement();
5243 statement = parse_if();
5247 statement = parse_switch();
5251 statement = parse_while();
5255 statement = parse_do();
5259 statement = parse_for();
5263 statement = parse_goto();
5267 statement = parse_continue();
5271 statement = parse_break();
5275 statement = parse_return();
5284 if(look_ahead(1)->type == ':') {
5285 statement = parse_label_statement();
5289 if(is_typedef_symbol(token.v.symbol)) {
5290 statement = parse_declaration_statement();
5294 statement = parse_expression_statement();
5297 case T___extension__:
5298 /* this can be a prefix to a declaration or an expression statement */
5299 /* we simply eat it now and parse the rest with tail recursion */
5302 } while(token.type == T___extension__);
5303 statement = parse_statement();
5307 statement = parse_declaration_statement();
5311 statement = parse_expression_statement();
5315 assert(statement == NULL
5316 || statement->base.source_position.input_name != NULL);
5322 * Parse a compound statement.
5324 static statement_t *parse_compound_statement(void)
5326 compound_statement_t *compound_statement
5327 = allocate_ast_zero(sizeof(compound_statement[0]));
5328 compound_statement->statement.kind = STATEMENT_COMPOUND;
5329 compound_statement->statement.source_position = token.source_position;
5333 int top = environment_top();
5334 context_t *last_context = context;
5335 set_context(&compound_statement->context);
5337 statement_t *last_statement = NULL;
5339 while(token.type != '}' && token.type != T_EOF) {
5340 statement_t *statement = parse_statement();
5341 if(statement == NULL)
5344 if(last_statement != NULL) {
5345 last_statement->base.next = statement;
5347 compound_statement->statements = statement;
5350 while(statement->base.next != NULL)
5351 statement = statement->base.next;
5353 last_statement = statement;
5356 if(token.type == '}') {
5359 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5362 assert(context == &compound_statement->context);
5363 set_context(last_context);
5364 environment_pop_to(top);
5366 return (statement_t*) compound_statement;
5370 * Initialize builtin types.
5372 static void initialize_builtin_types(void)
5374 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5375 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5376 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5377 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5378 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5379 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5380 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5381 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5383 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5384 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5385 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5386 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5390 * Parse a translation unit.
5392 static translation_unit_t *parse_translation_unit(void)
5394 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5396 assert(global_context == NULL);
5397 global_context = &unit->context;
5399 assert(context == NULL);
5400 set_context(&unit->context);
5402 initialize_builtin_types();
5404 while(token.type != T_EOF) {
5405 if (token.type == ';') {
5406 /* TODO error in strict mode */
5407 warningf(HERE, "stray ';' outside of function");
5410 parse_external_declaration();
5414 assert(context == &unit->context);
5416 last_declaration = NULL;
5418 assert(global_context == &unit->context);
5419 global_context = NULL;
5427 * @return the translation unit or NULL if errors occurred.
5429 translation_unit_t *parse(void)
5431 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5432 label_stack = NEW_ARR_F(stack_entry_t, 0);
5433 diagnostic_count = 0;
5437 type_set_output(stderr);
5438 ast_set_output(stderr);
5440 lookahead_bufpos = 0;
5441 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5444 translation_unit_t *unit = parse_translation_unit();
5446 DEL_ARR_F(environment_stack);
5447 DEL_ARR_F(label_stack);
5456 * Initialize the parser.
5458 void init_parser(void)
5460 init_expression_parsers();
5461 obstack_init(&temp_obst);
5463 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5464 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5468 * Terminate the parser.
5470 void exit_parser(void)
5472 obstack_free(&temp_obst, NULL);