7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
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 declared_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 scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(declaration_t *compound_declaration);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_CHAR_CONST] = sizeof(const_expression_t),
202 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
203 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
204 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
205 [EXPR_CALL] = sizeof(call_expression_t),
206 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
207 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
208 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
209 [EXPR_SELECT] = sizeof(select_expression_t),
210 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
211 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
212 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
213 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
214 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
215 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
216 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
217 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
218 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
219 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
220 [EXPR_VA_START] = sizeof(va_start_expression_t),
221 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
222 [EXPR_STATEMENT] = sizeof(statement_expression_t),
224 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
225 return sizes[EXPR_UNARY_FIRST];
227 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
228 return sizes[EXPR_BINARY_FIRST];
230 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
231 assert(sizes[kind] != 0);
236 * Allocate an expression node of given kind and initialize all
239 static expression_t *allocate_expression_zero(expression_kind_t kind)
241 size_t size = get_expression_struct_size(kind);
242 expression_t *res = allocate_ast_zero(size);
244 res->base.kind = kind;
245 res->base.type = type_error_type;
250 * Returns the size of a type node.
252 * @param kind the type kind
254 static size_t get_type_struct_size(type_kind_t kind)
256 static const size_t sizes[] = {
257 [TYPE_ATOMIC] = sizeof(atomic_type_t),
258 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
259 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
260 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
261 [TYPE_ENUM] = sizeof(enum_type_t),
262 [TYPE_FUNCTION] = sizeof(function_type_t),
263 [TYPE_POINTER] = sizeof(pointer_type_t),
264 [TYPE_ARRAY] = sizeof(array_type_t),
265 [TYPE_BUILTIN] = sizeof(builtin_type_t),
266 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
267 [TYPE_TYPEOF] = sizeof(typeof_type_t),
269 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
270 assert(kind <= TYPE_TYPEOF);
271 assert(sizes[kind] != 0);
276 * Allocate a type node of given kind and initialize all
279 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
281 size_t size = get_type_struct_size(kind);
282 type_t *res = obstack_alloc(type_obst, size);
283 memset(res, 0, size);
285 res->base.kind = kind;
286 res->base.source_position = source_position;
291 * Returns the size of an initializer node.
293 * @param kind the initializer kind
295 static size_t get_initializer_size(initializer_kind_t kind)
297 static const size_t sizes[] = {
298 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
299 [INITIALIZER_STRING] = sizeof(initializer_string_t),
300 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
301 [INITIALIZER_LIST] = sizeof(initializer_list_t),
302 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
304 assert(kind < sizeof(sizes) / sizeof(*sizes));
305 assert(sizes[kind] != 0);
310 * Allocate an initializer node of given kind and initialize all
313 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
315 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
322 * Free a type from the type obstack.
324 static void free_type(void *type)
326 obstack_free(type_obst, type);
330 * Returns the index of the top element of the environment stack.
332 static size_t environment_top(void)
334 return ARR_LEN(environment_stack);
338 * Returns the index of the top element of the label stack.
340 static size_t label_top(void)
342 return ARR_LEN(label_stack);
347 * Return the next token.
349 static inline void next_token(void)
351 token = lookahead_buffer[lookahead_bufpos];
352 lookahead_buffer[lookahead_bufpos] = lexer_token;
355 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
358 print_token(stderr, &token);
359 fprintf(stderr, "\n");
364 * Return the next token with a given lookahead.
366 static inline const token_t *look_ahead(int num)
368 assert(num > 0 && num <= MAX_LOOKAHEAD);
369 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
370 return &lookahead_buffer[pos];
373 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
376 * Report a parse error because an expected token was not found.
378 static void parse_error_expected(const char *message, ...)
380 if(message != NULL) {
381 errorf(HERE, "%s", message);
384 va_start(ap, message);
385 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
390 * Report a type error.
392 static void type_error(const char *msg, const source_position_t source_position,
395 errorf(source_position, "%s, but found type '%T'", msg, type);
399 * Report an incompatible type.
401 static void type_error_incompatible(const char *msg,
402 const source_position_t source_position, type_t *type1, type_t *type2)
404 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
408 * Eat an complete block, ie. '{ ... }'.
410 static void eat_block(void)
412 if(token.type == '{')
415 while(token.type != '}') {
416 if(token.type == T_EOF)
418 if(token.type == '{') {
428 * Eat a statement until an ';' token.
430 static void eat_statement(void)
432 while(token.type != ';') {
433 if(token.type == T_EOF)
435 if(token.type == '}')
437 if(token.type == '{') {
447 * Eat a parenthesed term, ie. '( ... )'.
449 static void eat_paren(void)
451 if(token.type == '(')
454 while(token.type != ')') {
455 if(token.type == T_EOF)
457 if(token.type == ')' || token.type == ';' || token.type == '}') {
460 if(token.type == '(') {
464 if(token.type == '{') {
473 #define expect(expected) \
475 if(UNLIKELY(token.type != (expected))) { \
476 parse_error_expected(NULL, (expected), 0); \
483 #define expect_block(expected) \
485 if(UNLIKELY(token.type != (expected))) { \
486 parse_error_expected(NULL, (expected), 0); \
493 #define expect_void(expected) \
495 if(UNLIKELY(token.type != (expected))) { \
496 parse_error_expected(NULL, (expected), 0); \
503 static void set_scope(scope_t *new_scope)
506 scope->last_declaration = last_declaration;
510 last_declaration = new_scope->last_declaration;
514 * Search a symbol in a given namespace and returns its declaration or
515 * NULL if this symbol was not found.
517 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
519 declaration_t *declaration = symbol->declaration;
520 for( ; declaration != NULL; declaration = declaration->symbol_next) {
521 if(declaration->namespc == namespc)
529 * pushs an environment_entry on the environment stack and links the
530 * corresponding symbol to the new entry
532 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
534 symbol_t *symbol = declaration->symbol;
535 namespace_t namespc = (namespace_t) declaration->namespc;
537 /* replace/add declaration into declaration list of the symbol */
538 declaration_t *iter = symbol->declaration;
540 symbol->declaration = declaration;
542 declaration_t *iter_last = NULL;
543 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
544 /* replace an entry? */
545 if(iter->namespc == namespc) {
546 if(iter_last == NULL) {
547 symbol->declaration = declaration;
549 iter_last->symbol_next = declaration;
551 declaration->symbol_next = iter->symbol_next;
556 assert(iter_last->symbol_next == NULL);
557 iter_last->symbol_next = declaration;
561 /* remember old declaration */
563 entry.symbol = symbol;
564 entry.old_declaration = iter;
565 entry.namespc = (unsigned short) namespc;
566 ARR_APP1(stack_entry_t, *stack_ptr, entry);
569 static void environment_push(declaration_t *declaration)
571 assert(declaration->source_position.input_name != NULL);
572 assert(declaration->parent_scope != NULL);
573 stack_push(&environment_stack, declaration);
576 static void label_push(declaration_t *declaration)
578 declaration->parent_scope = ¤t_function->scope;
579 stack_push(&label_stack, declaration);
583 * pops symbols from the environment stack until @p new_top is the top element
585 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
587 stack_entry_t *stack = *stack_ptr;
588 size_t top = ARR_LEN(stack);
591 assert(new_top <= top);
595 for(i = top; i > new_top; --i) {
596 stack_entry_t *entry = &stack[i - 1];
598 declaration_t *old_declaration = entry->old_declaration;
599 symbol_t *symbol = entry->symbol;
600 namespace_t namespc = (namespace_t)entry->namespc;
602 /* replace/remove declaration */
603 declaration_t *declaration = symbol->declaration;
604 assert(declaration != NULL);
605 if(declaration->namespc == namespc) {
606 if(old_declaration == NULL) {
607 symbol->declaration = declaration->symbol_next;
609 symbol->declaration = old_declaration;
612 declaration_t *iter_last = declaration;
613 declaration_t *iter = declaration->symbol_next;
614 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
615 /* replace an entry? */
616 if(iter->namespc == namespc) {
617 assert(iter_last != NULL);
618 iter_last->symbol_next = old_declaration;
619 if(old_declaration != NULL) {
620 old_declaration->symbol_next = iter->symbol_next;
625 assert(iter != NULL);
629 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
632 static void environment_pop_to(size_t new_top)
634 stack_pop_to(&environment_stack, new_top);
637 static void label_pop_to(size_t new_top)
639 stack_pop_to(&label_stack, new_top);
643 static int get_rank(const type_t *type)
645 assert(!is_typeref(type));
646 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
647 * and esp. footnote 108). However we can't fold constants (yet), so we
648 * can't decide whether unsigned int is possible, while int always works.
649 * (unsigned int would be preferable when possible... for stuff like
650 * struct { enum { ... } bla : 4; } ) */
651 if(type->kind == TYPE_ENUM)
652 return ATOMIC_TYPE_INT;
654 assert(type->kind == TYPE_ATOMIC);
655 return type->atomic.akind;
658 static type_t *promote_integer(type_t *type)
660 if(type->kind == TYPE_BITFIELD)
661 type = type->bitfield.base;
663 if(get_rank(type) < ATOMIC_TYPE_INT)
670 * Create a cast expression.
672 * @param expression the expression to cast
673 * @param dest_type the destination type
675 static expression_t *create_cast_expression(expression_t *expression,
678 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
680 cast->unary.value = expression;
681 cast->base.type = dest_type;
687 * Check if a given expression represents the 0 pointer constant.
689 static bool is_null_pointer_constant(const expression_t *expression)
691 /* skip void* cast */
692 if(expression->kind == EXPR_UNARY_CAST
693 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
694 expression = expression->unary.value;
697 /* TODO: not correct yet, should be any constant integer expression
698 * which evaluates to 0 */
699 if (expression->kind != EXPR_CONST)
702 type_t *const type = skip_typeref(expression->base.type);
703 if (!is_type_integer(type))
706 return expression->conste.v.int_value == 0;
710 * Create an implicit cast expression.
712 * @param expression the expression to cast
713 * @param dest_type the destination type
715 static expression_t *create_implicit_cast(expression_t *expression,
718 type_t *const source_type = expression->base.type;
720 if (source_type == dest_type)
723 return create_cast_expression(expression, dest_type);
726 /** Implements the rules from § 6.5.16.1 */
727 static type_t *semantic_assign(type_t *orig_type_left,
728 const expression_t *const right,
731 type_t *const orig_type_right = right->base.type;
732 type_t *const type_left = skip_typeref(orig_type_left);
733 type_t *const type_right = skip_typeref(orig_type_right);
735 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
736 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
737 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
738 && is_type_pointer(type_right))) {
739 return orig_type_left;
742 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
743 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
744 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
746 /* the left type has all qualifiers from the right type */
747 unsigned missing_qualifiers
748 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
749 if(missing_qualifiers != 0) {
750 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
751 return orig_type_left;
754 points_to_left = get_unqualified_type(points_to_left);
755 points_to_right = get_unqualified_type(points_to_right);
757 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
758 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
759 return orig_type_left;
762 if (!types_compatible(points_to_left, points_to_right)) {
763 warningf(right->base.source_position,
764 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
765 orig_type_left, context, right, orig_type_right);
768 return orig_type_left;
771 if ((is_type_compound(type_left) && is_type_compound(type_right))
772 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
773 type_t *const unqual_type_left = get_unqualified_type(type_left);
774 type_t *const unqual_type_right = get_unqualified_type(type_right);
775 if (types_compatible(unqual_type_left, unqual_type_right)) {
776 return orig_type_left;
780 if (!is_type_valid(type_left))
783 if (!is_type_valid(type_right))
784 return orig_type_right;
789 static expression_t *parse_constant_expression(void)
791 /* start parsing at precedence 7 (conditional expression) */
792 expression_t *result = parse_sub_expression(7);
794 if(!is_constant_expression(result)) {
795 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
801 static expression_t *parse_assignment_expression(void)
803 /* start parsing at precedence 2 (assignment expression) */
804 return parse_sub_expression(2);
807 static type_t *make_global_typedef(const char *name, type_t *type)
809 symbol_t *const symbol = symbol_table_insert(name);
811 declaration_t *const declaration = allocate_declaration_zero();
812 declaration->namespc = NAMESPACE_NORMAL;
813 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
814 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
815 declaration->type = type;
816 declaration->symbol = symbol;
817 declaration->source_position = builtin_source_position;
819 record_declaration(declaration);
821 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
822 typedef_type->typedeft.declaration = declaration;
827 static string_t parse_string_literals(void)
829 assert(token.type == T_STRING_LITERAL);
830 string_t result = token.v.string;
834 while (token.type == T_STRING_LITERAL) {
835 result = concat_strings(&result, &token.v.string);
842 static void parse_attributes(void)
846 case T___attribute__: {
854 errorf(HERE, "EOF while parsing attribute");
873 if(token.type != T_STRING_LITERAL) {
874 parse_error_expected("while parsing assembler attribute",
879 parse_string_literals();
884 goto attributes_finished;
892 static designator_t *parse_designation(void)
894 designator_t *result = NULL;
895 designator_t *last = NULL;
898 designator_t *designator;
901 designator = allocate_ast_zero(sizeof(designator[0]));
902 designator->source_position = token.source_position;
904 designator->array_index = parse_constant_expression();
908 designator = allocate_ast_zero(sizeof(designator[0]));
909 designator->source_position = token.source_position;
911 if(token.type != T_IDENTIFIER) {
912 parse_error_expected("while parsing designator",
916 designator->symbol = token.v.symbol;
924 assert(designator != NULL);
926 last->next = designator;
934 static initializer_t *initializer_from_string(array_type_t *type,
935 const string_t *const string)
937 /* TODO: check len vs. size of array type */
940 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
941 initializer->string.string = *string;
946 static initializer_t *initializer_from_wide_string(array_type_t *const type,
947 wide_string_t *const string)
949 /* TODO: check len vs. size of array type */
952 initializer_t *const initializer =
953 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
954 initializer->wide_string.string = *string;
959 static initializer_t *initializer_from_expression(type_t *orig_type,
960 expression_t *expression)
962 /* TODO check that expression is a constant expression */
964 /* § 6.7.8.14/15 char array may be initialized by string literals */
965 type_t *type = skip_typeref(orig_type);
966 type_t *expr_type_orig = expression->base.type;
967 type_t *expr_type = skip_typeref(expr_type_orig);
968 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
969 array_type_t *const array_type = &type->array;
970 type_t *const element_type = skip_typeref(array_type->element_type);
972 if (element_type->kind == TYPE_ATOMIC) {
973 atomic_type_kind_t akind = element_type->atomic.akind;
974 switch (expression->kind) {
975 case EXPR_STRING_LITERAL:
976 if (akind == ATOMIC_TYPE_CHAR
977 || akind == ATOMIC_TYPE_SCHAR
978 || akind == ATOMIC_TYPE_UCHAR) {
979 return initializer_from_string(array_type,
980 &expression->string.value);
983 case EXPR_WIDE_STRING_LITERAL: {
984 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
985 if (get_unqualified_type(element_type) == bare_wchar_type) {
986 return initializer_from_wide_string(array_type,
987 &expression->wide_string.value);
997 type_t *const res_type = semantic_assign(type, expression, "initializer");
998 if (res_type == NULL)
1001 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1002 result->value.value = create_implicit_cast(expression, res_type);
1007 static bool is_initializer_constant(const expression_t *expression)
1009 return is_constant_expression(expression)
1010 || is_address_constant(expression);
1013 static initializer_t *parse_scalar_initializer(type_t *type,
1014 bool must_be_constant)
1016 /* there might be extra {} hierarchies */
1018 while(token.type == '{') {
1021 warningf(HERE, "extra curly braces around scalar initializer");
1026 expression_t *expression = parse_assignment_expression();
1027 if(must_be_constant && !is_initializer_constant(expression)) {
1028 errorf(expression->base.source_position,
1029 "Initialisation expression '%E' is not constant\n",
1033 initializer_t *initializer = initializer_from_expression(type, expression);
1035 if(initializer == NULL) {
1036 errorf(expression->base.source_position,
1037 "expression '%E' doesn't match expected type '%T'",
1043 bool additional_warning_displayed = false;
1045 if(token.type == ',') {
1048 if(token.type != '}') {
1049 if(!additional_warning_displayed) {
1050 warningf(HERE, "additional elements in scalar initializer");
1051 additional_warning_displayed = true;
1061 typedef struct type_path_entry_t type_path_entry_t;
1062 struct type_path_entry_t {
1066 declaration_t *compound_entry;
1070 typedef struct type_path_t type_path_t;
1071 struct type_path_t {
1072 type_path_entry_t *path;
1073 type_t *top_type; /**< type of the element the path points */
1074 size_t max_index; /**< largest index in outermost array */
1078 static __attribute__((unused)) void debug_print_type_path(
1079 const type_path_t *path)
1081 size_t len = ARR_LEN(path->path);
1084 fprintf(stderr, "invalid path");
1088 for(size_t i = 0; i < len; ++i) {
1089 const type_path_entry_t *entry = & path->path[i];
1091 type_t *type = skip_typeref(entry->type);
1092 if(is_type_compound(type)) {
1093 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1094 } else if(is_type_array(type)) {
1095 fprintf(stderr, "[%u]", entry->v.index);
1097 fprintf(stderr, "-INVALID-");
1100 fprintf(stderr, " (");
1101 print_type(path->top_type);
1102 fprintf(stderr, ")");
1105 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1107 size_t len = ARR_LEN(path->path);
1109 return & path->path[len-1];
1112 static type_path_entry_t *append_to_type_path(type_path_t *path)
1114 size_t len = ARR_LEN(path->path);
1115 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1117 type_path_entry_t *result = & path->path[len];
1118 memset(result, 0, sizeof(result[0]));
1122 static void descend_into_subtype(type_path_t *path)
1124 type_t *orig_top_type = path->top_type;
1125 type_t *top_type = skip_typeref(orig_top_type);
1127 assert(is_type_compound(top_type) || is_type_array(top_type));
1129 type_path_entry_t *top = append_to_type_path(path);
1130 top->type = top_type;
1132 if(is_type_compound(top_type)) {
1133 declaration_t *declaration = top_type->compound.declaration;
1134 declaration_t *entry = declaration->scope.declarations;
1136 top->v.compound_entry = entry;
1137 path->top_type = entry->type;
1139 assert(is_type_array(top_type));
1142 path->top_type = top_type->array.element_type;
1146 static void ascend_from_subtype(type_path_t *path)
1148 type_path_entry_t *top = get_type_path_top(path);
1150 path->top_type = top->type;
1152 size_t len = ARR_LEN(path->path);
1153 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1156 static void ascend_to(type_path_t *path, size_t top_path_level)
1158 size_t len = ARR_LEN(path->path);
1159 assert(len >= top_path_level);
1161 while(len > top_path_level) {
1162 ascend_from_subtype(path);
1163 len = ARR_LEN(path->path);
1167 static bool walk_designator(type_path_t *path, const designator_t *designator,
1168 bool used_in_offsetof)
1170 for( ; designator != NULL; designator = designator->next) {
1171 type_path_entry_t *top = get_type_path_top(path);
1172 type_t *orig_type = top->type;
1174 type_t *type = skip_typeref(orig_type);
1176 if(designator->symbol != NULL) {
1177 symbol_t *symbol = designator->symbol;
1178 if(!is_type_compound(type)) {
1179 if(is_type_valid(type)) {
1180 errorf(designator->source_position,
1181 "'.%Y' designator used for non-compound type '%T'",
1187 declaration_t *declaration = type->compound.declaration;
1188 declaration_t *iter = declaration->scope.declarations;
1189 for( ; iter != NULL; iter = iter->next) {
1190 if(iter->symbol == symbol) {
1195 errorf(designator->source_position,
1196 "'%T' has no member named '%Y'", orig_type, symbol);
1199 if(used_in_offsetof) {
1200 type_t *real_type = skip_typeref(iter->type);
1201 if(real_type->kind == TYPE_BITFIELD) {
1202 errorf(designator->source_position,
1203 "offsetof designator '%Y' may not specify bitfield",
1209 top->type = orig_type;
1210 top->v.compound_entry = iter;
1211 orig_type = iter->type;
1213 expression_t *array_index = designator->array_index;
1214 assert(designator->array_index != NULL);
1216 if(!is_type_array(type)) {
1217 if(is_type_valid(type)) {
1218 errorf(designator->source_position,
1219 "[%E] designator used for non-array type '%T'",
1220 array_index, orig_type);
1224 if(!is_type_valid(array_index->base.type)) {
1228 long index = fold_constant(array_index);
1229 if(!used_in_offsetof) {
1231 errorf(designator->source_position,
1232 "array index [%E] must be positive", array_index);
1235 if(type->array.size_constant == true) {
1236 long array_size = type->array.size;
1237 if(index >= array_size) {
1238 errorf(designator->source_position,
1239 "designator [%E] (%d) exceeds array size %d",
1240 array_index, index, array_size);
1246 top->type = orig_type;
1247 top->v.index = (size_t) index;
1248 orig_type = type->array.element_type;
1250 path->top_type = orig_type;
1252 if(designator->next != NULL) {
1253 descend_into_subtype(path);
1257 path->invalid = false;
1264 static void advance_current_object(type_path_t *path, size_t top_path_level)
1269 type_path_entry_t *top = get_type_path_top(path);
1271 type_t *type = skip_typeref(top->type);
1272 if(is_type_union(type)) {
1273 /* in unions only the first element is initialized */
1274 top->v.compound_entry = NULL;
1275 } else if(is_type_struct(type)) {
1276 declaration_t *entry = top->v.compound_entry;
1278 entry = entry->next;
1279 top->v.compound_entry = entry;
1281 path->top_type = entry->type;
1285 assert(is_type_array(type));
1289 if(!type->array.size_constant || top->v.index < type->array.size) {
1294 /* we're past the last member of the current sub-aggregate, try if we
1295 * can ascend in the type hierarchy and continue with another subobject */
1296 size_t len = ARR_LEN(path->path);
1298 if(len > top_path_level) {
1299 ascend_from_subtype(path);
1300 advance_current_object(path, top_path_level);
1302 path->invalid = true;
1306 static void skip_initializers(void)
1308 if(token.type == '{')
1311 while(token.type != '}') {
1312 if(token.type == T_EOF)
1314 if(token.type == '{') {
1322 static initializer_t *parse_sub_initializer(type_path_t *path,
1323 type_t *outer_type, size_t top_path_level, bool must_be_constant)
1325 type_t *orig_type = path->top_type;
1326 type_t *type = skip_typeref(orig_type);
1328 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1329 * initializers in this case. */
1330 if(!is_type_valid(type)) {
1331 skip_initializers();
1335 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1338 designator_t *designator = NULL;
1339 if(token.type == '.' || token.type == '[') {
1340 designator = parse_designation();
1342 /* reset path to toplevel, evaluate designator from there */
1343 ascend_to(path, top_path_level);
1344 if(!walk_designator(path, designator, false)) {
1345 /* can't continue after designation error */
1349 initializer_t *designator_initializer
1350 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1351 designator_initializer->designator.designator = designator;
1352 ARR_APP1(initializer_t*, initializers, designator_initializer);
1357 if(token.type == '{') {
1358 if(is_type_scalar(type)) {
1359 sub = parse_scalar_initializer(type, must_be_constant);
1362 descend_into_subtype(path);
1364 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1367 ascend_from_subtype(path);
1372 /* must be an expression */
1373 expression_t *expression = parse_assignment_expression();
1375 if(must_be_constant && !is_initializer_constant(expression)) {
1376 errorf(expression->base.source_position,
1377 "Initialisation expression '%E' is not constant\n",
1381 /* handle { "string" } special case */
1382 if((expression->kind == EXPR_STRING_LITERAL
1383 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1384 && outer_type != NULL) {
1385 sub = initializer_from_expression(outer_type, expression);
1387 if(token.type == ',') {
1390 if(token.type != '}') {
1391 warningf(HERE, "excessive elements in initializer for type '%T'",
1394 /* TODO: eat , ... */
1399 /* descend into subtypes until expression matches type */
1401 orig_type = path->top_type;
1402 type = skip_typeref(orig_type);
1404 sub = initializer_from_expression(orig_type, expression);
1408 if(!is_type_valid(type)) {
1411 if(is_type_scalar(type)) {
1412 errorf(expression->base.source_position,
1413 "expression '%E' doesn't match expected type '%T'",
1414 expression, orig_type);
1418 descend_into_subtype(path);
1422 /* update largest index of top array */
1423 const type_path_entry_t *first = &path->path[0];
1424 type_t *first_type = first->type;
1425 first_type = skip_typeref(first_type);
1426 if(is_type_array(first_type)) {
1427 size_t index = first->v.index;
1428 if(index > path->max_index)
1429 path->max_index = index;
1432 /* append to initializers list */
1433 ARR_APP1(initializer_t*, initializers, sub);
1435 if(token.type == '}') {
1439 if(token.type == '}') {
1443 advance_current_object(path, top_path_level);
1444 orig_type = path->top_type;
1445 type = skip_typeref(orig_type);
1448 size_t len = ARR_LEN(initializers);
1449 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1450 initializer_t *result = allocate_ast_zero(size);
1451 result->kind = INITIALIZER_LIST;
1452 result->list.len = len;
1453 memcpy(&result->list.initializers, initializers,
1454 len * sizeof(initializers[0]));
1456 ascend_to(path, top_path_level);
1461 skip_initializers();
1462 DEL_ARR_F(initializers);
1463 ascend_to(path, top_path_level);
1467 typedef struct parse_initializer_env_t {
1468 type_t *type; /* the type of the initializer. In case of an
1469 array type with unspecified size this gets
1470 adjusted to the actual size. */
1471 initializer_t *initializer; /* initializer will be filled in here */
1472 bool must_be_constant;
1473 } parse_initializer_env_t;
1475 static void parse_initializer(parse_initializer_env_t *env)
1477 type_t *type = skip_typeref(env->type);
1478 initializer_t *result = NULL;
1481 if(is_type_scalar(type)) {
1482 /* TODO: § 6.7.8.11; eat {} without warning */
1483 result = parse_scalar_initializer(type, env->must_be_constant);
1484 } else if(token.type == '{') {
1488 memset(&path, 0, sizeof(path));
1489 path.top_type = env->type;
1490 path.path = NEW_ARR_F(type_path_entry_t, 0);
1492 descend_into_subtype(&path);
1494 result = parse_sub_initializer(&path, env->type, 1,
1495 env->must_be_constant);
1497 max_index = path.max_index;
1498 DEL_ARR_F(path.path);
1502 /* parse_scalar_initializer also works in this case: we simply
1503 * have an expression without {} around it */
1504 result = parse_scalar_initializer(type, env->must_be_constant);
1507 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1508 * the array type size */
1509 if(is_type_array(type) && type->array.size_expression == NULL
1510 && result != NULL) {
1512 switch (result->kind) {
1513 case INITIALIZER_LIST:
1514 size = max_index + 1;
1517 case INITIALIZER_STRING:
1518 size = result->string.string.size;
1521 case INITIALIZER_WIDE_STRING:
1522 size = result->wide_string.string.size;
1526 panic("invalid initializer type");
1529 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1530 cnst->base.type = type_size_t;
1531 cnst->conste.v.int_value = size;
1533 type_t *new_type = duplicate_type(type);
1535 new_type->array.size_expression = cnst;
1536 new_type->array.size_constant = true;
1537 new_type->array.size = size;
1538 env->type = new_type;
1541 env->initializer = result;
1544 static declaration_t *append_declaration(declaration_t *declaration);
1546 static declaration_t *parse_compound_type_specifier(bool is_struct)
1554 symbol_t *symbol = NULL;
1555 declaration_t *declaration = NULL;
1557 if (token.type == T___attribute__) {
1562 if(token.type == T_IDENTIFIER) {
1563 symbol = token.v.symbol;
1567 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1569 declaration = get_declaration(symbol, NAMESPACE_UNION);
1571 } else if(token.type != '{') {
1573 parse_error_expected("while parsing struct type specifier",
1574 T_IDENTIFIER, '{', 0);
1576 parse_error_expected("while parsing union type specifier",
1577 T_IDENTIFIER, '{', 0);
1583 if(declaration == NULL) {
1584 declaration = allocate_declaration_zero();
1585 declaration->namespc =
1586 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1587 declaration->source_position = token.source_position;
1588 declaration->symbol = symbol;
1589 declaration->parent_scope = scope;
1590 if (symbol != NULL) {
1591 environment_push(declaration);
1593 append_declaration(declaration);
1596 if(token.type == '{') {
1597 if(declaration->init.is_defined) {
1598 assert(symbol != NULL);
1599 errorf(HERE, "multiple definitions of '%s %Y'",
1600 is_struct ? "struct" : "union", symbol);
1601 declaration->scope.declarations = NULL;
1603 declaration->init.is_defined = true;
1605 parse_compound_type_entries(declaration);
1612 static void parse_enum_entries(type_t *const enum_type)
1616 if(token.type == '}') {
1618 errorf(HERE, "empty enum not allowed");
1623 if(token.type != T_IDENTIFIER) {
1624 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1629 declaration_t *const entry = allocate_declaration_zero();
1630 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1631 entry->type = enum_type;
1632 entry->symbol = token.v.symbol;
1633 entry->source_position = token.source_position;
1636 if(token.type == '=') {
1638 expression_t *value = parse_constant_expression();
1640 value = create_implicit_cast(value, enum_type);
1641 entry->init.enum_value = value;
1646 record_declaration(entry);
1648 if(token.type != ',')
1651 } while(token.type != '}');
1656 static type_t *parse_enum_specifier(void)
1660 declaration_t *declaration;
1663 if(token.type == T_IDENTIFIER) {
1664 symbol = token.v.symbol;
1667 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1668 } else if(token.type != '{') {
1669 parse_error_expected("while parsing enum type specifier",
1670 T_IDENTIFIER, '{', 0);
1677 if(declaration == NULL) {
1678 declaration = allocate_declaration_zero();
1679 declaration->namespc = NAMESPACE_ENUM;
1680 declaration->source_position = token.source_position;
1681 declaration->symbol = symbol;
1682 declaration->parent_scope = scope;
1685 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1686 type->enumt.declaration = declaration;
1688 if(token.type == '{') {
1689 if(declaration->init.is_defined) {
1690 errorf(HERE, "multiple definitions of enum %Y", symbol);
1692 if (symbol != NULL) {
1693 environment_push(declaration);
1695 append_declaration(declaration);
1696 declaration->init.is_defined = 1;
1698 parse_enum_entries(type);
1706 * if a symbol is a typedef to another type, return true
1708 static bool is_typedef_symbol(symbol_t *symbol)
1710 const declaration_t *const declaration =
1711 get_declaration(symbol, NAMESPACE_NORMAL);
1713 declaration != NULL &&
1714 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1717 static type_t *parse_typeof(void)
1725 expression_t *expression = NULL;
1728 switch(token.type) {
1729 case T___extension__:
1730 /* this can be a prefix to a typename or an expression */
1731 /* we simply eat it now. */
1734 } while(token.type == T___extension__);
1738 if(is_typedef_symbol(token.v.symbol)) {
1739 type = parse_typename();
1741 expression = parse_expression();
1742 type = expression->base.type;
1747 type = parse_typename();
1751 expression = parse_expression();
1752 type = expression->base.type;
1758 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1759 typeof_type->typeoft.expression = expression;
1760 typeof_type->typeoft.typeof_type = type;
1766 SPECIFIER_SIGNED = 1 << 0,
1767 SPECIFIER_UNSIGNED = 1 << 1,
1768 SPECIFIER_LONG = 1 << 2,
1769 SPECIFIER_INT = 1 << 3,
1770 SPECIFIER_DOUBLE = 1 << 4,
1771 SPECIFIER_CHAR = 1 << 5,
1772 SPECIFIER_SHORT = 1 << 6,
1773 SPECIFIER_LONG_LONG = 1 << 7,
1774 SPECIFIER_FLOAT = 1 << 8,
1775 SPECIFIER_BOOL = 1 << 9,
1776 SPECIFIER_VOID = 1 << 10,
1777 #ifdef PROVIDE_COMPLEX
1778 SPECIFIER_COMPLEX = 1 << 11,
1779 SPECIFIER_IMAGINARY = 1 << 12,
1783 static type_t *create_builtin_type(symbol_t *const symbol,
1784 type_t *const real_type)
1786 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1787 type->builtin.symbol = symbol;
1788 type->builtin.real_type = real_type;
1790 type_t *result = typehash_insert(type);
1791 if (type != result) {
1798 static type_t *get_typedef_type(symbol_t *symbol)
1800 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1801 if(declaration == NULL
1802 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1805 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1806 type->typedeft.declaration = declaration;
1811 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1813 type_t *type = NULL;
1814 unsigned type_qualifiers = 0;
1815 unsigned type_specifiers = 0;
1818 specifiers->source_position = token.source_position;
1821 switch(token.type) {
1824 #define MATCH_STORAGE_CLASS(token, class) \
1826 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
1827 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1829 specifiers->declared_storage_class = class; \
1833 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1834 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1835 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1836 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1837 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1840 switch (specifiers->declared_storage_class) {
1841 case STORAGE_CLASS_NONE:
1842 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
1845 case STORAGE_CLASS_EXTERN:
1846 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
1849 case STORAGE_CLASS_STATIC:
1850 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
1854 errorf(HERE, "multiple storage classes in declaration specifiers");
1860 /* type qualifiers */
1861 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1863 type_qualifiers |= qualifier; \
1867 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1868 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1869 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1871 case T___extension__:
1876 /* type specifiers */
1877 #define MATCH_SPECIFIER(token, specifier, name) \
1880 if(type_specifiers & specifier) { \
1881 errorf(HERE, "multiple " name " type specifiers given"); \
1883 type_specifiers |= specifier; \
1887 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1888 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1889 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1890 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1891 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1892 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1893 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1894 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1895 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1896 #ifdef PROVIDE_COMPLEX
1897 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1898 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1901 /* only in microsoft mode */
1902 specifiers->decl_modifiers |= DM_FORCEINLINE;
1906 specifiers->is_inline = true;
1911 if(type_specifiers & SPECIFIER_LONG_LONG) {
1912 errorf(HERE, "multiple type specifiers given");
1913 } else if(type_specifiers & SPECIFIER_LONG) {
1914 type_specifiers |= SPECIFIER_LONG_LONG;
1916 type_specifiers |= SPECIFIER_LONG;
1921 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1923 type->compound.declaration = parse_compound_type_specifier(true);
1927 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1929 type->compound.declaration = parse_compound_type_specifier(false);
1933 type = parse_enum_specifier();
1936 type = parse_typeof();
1938 case T___builtin_va_list:
1939 type = duplicate_type(type_valist);
1943 case T___attribute__:
1947 case T_IDENTIFIER: {
1948 /* only parse identifier if we haven't found a type yet */
1949 if(type != NULL || type_specifiers != 0)
1950 goto finish_specifiers;
1952 type_t *typedef_type = get_typedef_type(token.v.symbol);
1954 if(typedef_type == NULL)
1955 goto finish_specifiers;
1958 type = typedef_type;
1962 /* function specifier */
1964 goto finish_specifiers;
1971 atomic_type_kind_t atomic_type;
1973 /* match valid basic types */
1974 switch(type_specifiers) {
1975 case SPECIFIER_VOID:
1976 atomic_type = ATOMIC_TYPE_VOID;
1978 case SPECIFIER_CHAR:
1979 atomic_type = ATOMIC_TYPE_CHAR;
1981 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1982 atomic_type = ATOMIC_TYPE_SCHAR;
1984 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1985 atomic_type = ATOMIC_TYPE_UCHAR;
1987 case SPECIFIER_SHORT:
1988 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1989 case SPECIFIER_SHORT | SPECIFIER_INT:
1990 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1991 atomic_type = ATOMIC_TYPE_SHORT;
1993 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1994 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1995 atomic_type = ATOMIC_TYPE_USHORT;
1998 case SPECIFIER_SIGNED:
1999 case SPECIFIER_SIGNED | SPECIFIER_INT:
2000 atomic_type = ATOMIC_TYPE_INT;
2002 case SPECIFIER_UNSIGNED:
2003 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2004 atomic_type = ATOMIC_TYPE_UINT;
2006 case SPECIFIER_LONG:
2007 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2008 case SPECIFIER_LONG | SPECIFIER_INT:
2009 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2010 atomic_type = ATOMIC_TYPE_LONG;
2012 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2013 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2014 atomic_type = ATOMIC_TYPE_ULONG;
2016 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2017 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2018 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2019 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2021 atomic_type = ATOMIC_TYPE_LONGLONG;
2023 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2024 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2026 atomic_type = ATOMIC_TYPE_ULONGLONG;
2028 case SPECIFIER_FLOAT:
2029 atomic_type = ATOMIC_TYPE_FLOAT;
2031 case SPECIFIER_DOUBLE:
2032 atomic_type = ATOMIC_TYPE_DOUBLE;
2034 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2035 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2037 case SPECIFIER_BOOL:
2038 atomic_type = ATOMIC_TYPE_BOOL;
2040 #ifdef PROVIDE_COMPLEX
2041 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2042 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2044 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2045 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2047 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2048 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2050 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2051 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2053 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2054 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2056 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2057 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2061 /* invalid specifier combination, give an error message */
2062 if(type_specifiers == 0) {
2063 if (! strict_mode) {
2064 if (warning.implicit_int) {
2065 warningf(HERE, "no type specifiers in declaration, using 'int'");
2067 atomic_type = ATOMIC_TYPE_INT;
2070 errorf(HERE, "no type specifiers given in declaration");
2072 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2073 (type_specifiers & SPECIFIER_UNSIGNED)) {
2074 errorf(HERE, "signed and unsigned specifiers gives");
2075 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2076 errorf(HERE, "only integer types can be signed or unsigned");
2078 errorf(HERE, "multiple datatypes in declaration");
2080 atomic_type = ATOMIC_TYPE_INVALID;
2083 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2084 type->atomic.akind = atomic_type;
2087 if(type_specifiers != 0) {
2088 errorf(HERE, "multiple datatypes in declaration");
2092 type->base.qualifiers = type_qualifiers;
2094 type_t *result = typehash_insert(type);
2095 if(newtype && result != type) {
2099 specifiers->type = result;
2102 static type_qualifiers_t parse_type_qualifiers(void)
2104 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2107 switch(token.type) {
2108 /* type qualifiers */
2109 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2110 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2111 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2114 return type_qualifiers;
2119 static declaration_t *parse_identifier_list(void)
2121 declaration_t *declarations = NULL;
2122 declaration_t *last_declaration = NULL;
2124 declaration_t *const declaration = allocate_declaration_zero();
2125 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2126 declaration->source_position = token.source_position;
2127 declaration->symbol = token.v.symbol;
2130 if(last_declaration != NULL) {
2131 last_declaration->next = declaration;
2133 declarations = declaration;
2135 last_declaration = declaration;
2137 if(token.type != ',')
2140 } while(token.type == T_IDENTIFIER);
2142 return declarations;
2145 static void semantic_parameter(declaration_t *declaration)
2147 /* TODO: improve error messages */
2149 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2150 errorf(HERE, "typedef not allowed in parameter list");
2151 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2152 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2153 errorf(HERE, "parameter may only have none or register storage class");
2156 type_t *const orig_type = declaration->type;
2157 type_t * type = skip_typeref(orig_type);
2159 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2160 * into a pointer. § 6.7.5.3 (7) */
2161 if (is_type_array(type)) {
2162 type_t *const element_type = type->array.element_type;
2164 type = make_pointer_type(element_type, type->base.qualifiers);
2166 declaration->type = type;
2169 if(is_type_incomplete(type)) {
2170 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2171 orig_type, declaration->symbol);
2175 static declaration_t *parse_parameter(void)
2177 declaration_specifiers_t specifiers;
2178 memset(&specifiers, 0, sizeof(specifiers));
2180 parse_declaration_specifiers(&specifiers);
2182 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2184 semantic_parameter(declaration);
2189 static declaration_t *parse_parameters(function_type_t *type)
2191 if(token.type == T_IDENTIFIER) {
2192 symbol_t *symbol = token.v.symbol;
2193 if(!is_typedef_symbol(symbol)) {
2194 type->kr_style_parameters = true;
2195 return parse_identifier_list();
2199 if(token.type == ')') {
2200 type->unspecified_parameters = 1;
2203 if(token.type == T_void && look_ahead(1)->type == ')') {
2208 declaration_t *declarations = NULL;
2209 declaration_t *declaration;
2210 declaration_t *last_declaration = NULL;
2211 function_parameter_t *parameter;
2212 function_parameter_t *last_parameter = NULL;
2215 switch(token.type) {
2219 return declarations;
2222 case T___extension__:
2224 declaration = parse_parameter();
2226 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2227 memset(parameter, 0, sizeof(parameter[0]));
2228 parameter->type = declaration->type;
2230 if(last_parameter != NULL) {
2231 last_declaration->next = declaration;
2232 last_parameter->next = parameter;
2234 type->parameters = parameter;
2235 declarations = declaration;
2237 last_parameter = parameter;
2238 last_declaration = declaration;
2242 return declarations;
2244 if(token.type != ',')
2245 return declarations;
2255 } construct_type_kind_t;
2257 typedef struct construct_type_t construct_type_t;
2258 struct construct_type_t {
2259 construct_type_kind_t kind;
2260 construct_type_t *next;
2263 typedef struct parsed_pointer_t parsed_pointer_t;
2264 struct parsed_pointer_t {
2265 construct_type_t construct_type;
2266 type_qualifiers_t type_qualifiers;
2269 typedef struct construct_function_type_t construct_function_type_t;
2270 struct construct_function_type_t {
2271 construct_type_t construct_type;
2272 type_t *function_type;
2275 typedef struct parsed_array_t parsed_array_t;
2276 struct parsed_array_t {
2277 construct_type_t construct_type;
2278 type_qualifiers_t type_qualifiers;
2284 typedef struct construct_base_type_t construct_base_type_t;
2285 struct construct_base_type_t {
2286 construct_type_t construct_type;
2290 static construct_type_t *parse_pointer_declarator(void)
2294 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2295 memset(pointer, 0, sizeof(pointer[0]));
2296 pointer->construct_type.kind = CONSTRUCT_POINTER;
2297 pointer->type_qualifiers = parse_type_qualifiers();
2299 return (construct_type_t*) pointer;
2302 static construct_type_t *parse_array_declarator(void)
2306 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2307 memset(array, 0, sizeof(array[0]));
2308 array->construct_type.kind = CONSTRUCT_ARRAY;
2310 if(token.type == T_static) {
2311 array->is_static = true;
2315 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2316 if(type_qualifiers != 0) {
2317 if(token.type == T_static) {
2318 array->is_static = true;
2322 array->type_qualifiers = type_qualifiers;
2324 if(token.type == '*' && look_ahead(1)->type == ']') {
2325 array->is_variable = true;
2327 } else if(token.type != ']') {
2328 array->size = parse_assignment_expression();
2333 return (construct_type_t*) array;
2336 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2341 if(declaration != NULL) {
2342 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2344 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2347 declaration_t *parameters = parse_parameters(&type->function);
2348 if(declaration != NULL) {
2349 declaration->scope.declarations = parameters;
2352 construct_function_type_t *construct_function_type =
2353 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2354 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2355 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2356 construct_function_type->function_type = type;
2360 return (construct_type_t*) construct_function_type;
2363 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2364 bool may_be_abstract)
2366 /* construct a single linked list of construct_type_t's which describe
2367 * how to construct the final declarator type */
2368 construct_type_t *first = NULL;
2369 construct_type_t *last = NULL;
2372 while(token.type == '*') {
2373 construct_type_t *type = parse_pointer_declarator();
2384 /* TODO: find out if this is correct */
2387 construct_type_t *inner_types = NULL;
2389 switch(token.type) {
2391 if(declaration == NULL) {
2392 errorf(HERE, "no identifier expected in typename");
2394 declaration->symbol = token.v.symbol;
2395 declaration->source_position = token.source_position;
2401 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2407 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2408 /* avoid a loop in the outermost scope, because eat_statement doesn't
2410 if(token.type == '}' && current_function == NULL) {
2418 construct_type_t *p = last;
2421 construct_type_t *type;
2422 switch(token.type) {
2424 type = parse_function_declarator(declaration);
2427 type = parse_array_declarator();
2430 goto declarator_finished;
2433 /* insert in the middle of the list (behind p) */
2435 type->next = p->next;
2446 declarator_finished:
2449 /* append inner_types at the end of the list, we don't to set last anymore
2450 * as it's not needed anymore */
2452 assert(first == NULL);
2453 first = inner_types;
2455 last->next = inner_types;
2461 static type_t *construct_declarator_type(construct_type_t *construct_list,
2464 construct_type_t *iter = construct_list;
2465 for( ; iter != NULL; iter = iter->next) {
2466 switch(iter->kind) {
2467 case CONSTRUCT_INVALID:
2468 panic("invalid type construction found");
2469 case CONSTRUCT_FUNCTION: {
2470 construct_function_type_t *construct_function_type
2471 = (construct_function_type_t*) iter;
2473 type_t *function_type = construct_function_type->function_type;
2475 function_type->function.return_type = type;
2477 type_t *skipped_return_type = skip_typeref(type);
2478 if (is_type_function(skipped_return_type)) {
2479 errorf(HERE, "function returning function is not allowed");
2480 type = type_error_type;
2481 } else if (is_type_array(skipped_return_type)) {
2482 errorf(HERE, "function returning array is not allowed");
2483 type = type_error_type;
2485 type = function_type;
2490 case CONSTRUCT_POINTER: {
2491 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2492 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2493 pointer_type->pointer.points_to = type;
2494 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2496 type = pointer_type;
2500 case CONSTRUCT_ARRAY: {
2501 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2502 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2504 expression_t *size_expression
2505 = create_implicit_cast(parsed_array->size, type_size_t);
2507 array_type->base.qualifiers = parsed_array->type_qualifiers;
2508 array_type->array.element_type = type;
2509 array_type->array.is_static = parsed_array->is_static;
2510 array_type->array.is_variable = parsed_array->is_variable;
2511 array_type->array.size_expression = size_expression;
2513 if(size_expression != NULL) {
2514 if(is_constant_expression(size_expression)) {
2515 array_type->array.size_constant = true;
2516 array_type->array.size
2517 = fold_constant(size_expression);
2519 array_type->array.is_vla = true;
2523 type_t *skipped_type = skip_typeref(type);
2524 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2525 errorf(HERE, "array of void is not allowed");
2526 type = type_error_type;
2534 type_t *hashed_type = typehash_insert(type);
2535 if(hashed_type != type) {
2536 /* the function type was constructed earlier freeing it here will
2537 * destroy other types... */
2538 if(iter->kind != CONSTRUCT_FUNCTION) {
2548 static declaration_t *parse_declarator(
2549 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2551 declaration_t *const declaration = allocate_declaration_zero();
2552 declaration->declared_storage_class = specifiers->declared_storage_class;
2553 declaration->modifiers = specifiers->decl_modifiers;
2554 declaration->is_inline = specifiers->is_inline;
2556 declaration->storage_class = specifiers->declared_storage_class;
2557 if(declaration->storage_class == STORAGE_CLASS_NONE
2558 && scope != global_scope) {
2559 declaration->storage_class = STORAGE_CLASS_AUTO;
2562 construct_type_t *construct_type
2563 = parse_inner_declarator(declaration, may_be_abstract);
2564 type_t *const type = specifiers->type;
2565 declaration->type = construct_declarator_type(construct_type, type);
2567 if(construct_type != NULL) {
2568 obstack_free(&temp_obst, construct_type);
2574 static type_t *parse_abstract_declarator(type_t *base_type)
2576 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2578 type_t *result = construct_declarator_type(construct_type, base_type);
2579 if(construct_type != NULL) {
2580 obstack_free(&temp_obst, construct_type);
2586 static declaration_t *append_declaration(declaration_t* const declaration)
2588 if (last_declaration != NULL) {
2589 last_declaration->next = declaration;
2591 scope->declarations = declaration;
2593 last_declaration = declaration;
2598 * Check if the declaration of main is suspicious. main should be a
2599 * function with external linkage, returning int, taking either zero
2600 * arguments, two, or three arguments of appropriate types, ie.
2602 * int main([ int argc, char **argv [, char **env ] ]).
2604 * @param decl the declaration to check
2605 * @param type the function type of the declaration
2607 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2609 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2610 warningf(decl->source_position, "'main' is normally a non-static function");
2612 if (skip_typeref(func_type->return_type) != type_int) {
2613 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2615 const function_parameter_t *parm = func_type->parameters;
2617 type_t *const first_type = parm->type;
2618 if (!types_compatible(skip_typeref(first_type), type_int)) {
2619 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2623 type_t *const second_type = parm->type;
2624 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2625 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2629 type_t *const third_type = parm->type;
2630 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2631 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2635 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2639 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2645 * Check if a symbol is the equal to "main".
2647 static bool is_sym_main(const symbol_t *const sym)
2649 return strcmp(sym->string, "main") == 0;
2652 static declaration_t *internal_record_declaration(
2653 declaration_t *const declaration,
2654 const bool is_function_definition)
2656 const symbol_t *const symbol = declaration->symbol;
2657 const namespace_t namespc = (namespace_t)declaration->namespc;
2659 type_t *const orig_type = declaration->type;
2660 type_t *const type = skip_typeref(orig_type);
2661 if (is_type_function(type) &&
2662 type->function.unspecified_parameters &&
2663 warning.strict_prototypes) {
2664 warningf(declaration->source_position,
2665 "function declaration '%#T' is not a prototype",
2666 orig_type, declaration->symbol);
2669 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2670 check_type_of_main(declaration, &type->function);
2673 assert(declaration->symbol != NULL);
2674 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2676 assert(declaration != previous_declaration);
2677 if (previous_declaration != NULL) {
2678 if (previous_declaration->parent_scope == scope) {
2679 /* can happen for K&R style declarations */
2680 if(previous_declaration->type == NULL) {
2681 previous_declaration->type = declaration->type;
2684 const type_t *prev_type = skip_typeref(previous_declaration->type);
2685 if (!types_compatible(type, prev_type)) {
2686 errorf(declaration->source_position,
2687 "declaration '%#T' is incompatible with "
2688 "previous declaration '%#T'",
2689 orig_type, symbol, previous_declaration->type, symbol);
2690 errorf(previous_declaration->source_position,
2691 "previous declaration of '%Y' was here", symbol);
2693 unsigned old_storage_class
2694 = previous_declaration->storage_class;
2695 unsigned new_storage_class = declaration->storage_class;
2697 if(is_type_incomplete(prev_type)) {
2698 previous_declaration->type = type;
2702 /* pretend no storage class means extern for function
2703 * declarations (except if the previous declaration is neither
2704 * none nor extern) */
2705 if (is_type_function(type)) {
2706 switch (old_storage_class) {
2707 case STORAGE_CLASS_NONE:
2708 old_storage_class = STORAGE_CLASS_EXTERN;
2710 case STORAGE_CLASS_EXTERN:
2711 if (is_function_definition) {
2712 if (warning.missing_prototypes &&
2713 prev_type->function.unspecified_parameters &&
2714 !is_sym_main(symbol)) {
2715 warningf(declaration->source_position,
2716 "no previous prototype for '%#T'",
2719 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2720 new_storage_class = STORAGE_CLASS_EXTERN;
2728 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2729 new_storage_class == STORAGE_CLASS_EXTERN) {
2730 warn_redundant_declaration:
2731 if (warning.redundant_decls) {
2732 warningf(declaration->source_position,
2733 "redundant declaration for '%Y'", symbol);
2734 warningf(previous_declaration->source_position,
2735 "previous declaration of '%Y' was here",
2738 } else if (current_function == NULL) {
2739 if (old_storage_class != STORAGE_CLASS_STATIC &&
2740 new_storage_class == STORAGE_CLASS_STATIC) {
2741 errorf(declaration->source_position,
2742 "static declaration of '%Y' follows non-static declaration",
2744 errorf(previous_declaration->source_position,
2745 "previous declaration of '%Y' was here", symbol);
2747 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2748 goto warn_redundant_declaration;
2750 if (new_storage_class == STORAGE_CLASS_NONE) {
2751 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2752 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
2756 if (old_storage_class == new_storage_class) {
2757 errorf(declaration->source_position,
2758 "redeclaration of '%Y'", symbol);
2760 errorf(declaration->source_position,
2761 "redeclaration of '%Y' with different linkage",
2764 errorf(previous_declaration->source_position,
2765 "previous declaration of '%Y' was here", symbol);
2768 return previous_declaration;
2770 } else if (is_function_definition) {
2771 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2772 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2773 warningf(declaration->source_position,
2774 "no previous prototype for '%#T'", orig_type, symbol);
2775 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2776 warningf(declaration->source_position,
2777 "no previous declaration for '%#T'", orig_type,
2781 } else if (warning.missing_declarations &&
2782 scope == global_scope &&
2783 !is_type_function(type) && (
2784 declaration->storage_class == STORAGE_CLASS_NONE ||
2785 declaration->storage_class == STORAGE_CLASS_THREAD
2787 warningf(declaration->source_position,
2788 "no previous declaration for '%#T'", orig_type, symbol);
2791 assert(declaration->parent_scope == NULL);
2792 assert(scope != NULL);
2794 declaration->parent_scope = scope;
2796 environment_push(declaration);
2797 return append_declaration(declaration);
2800 static declaration_t *record_declaration(declaration_t *declaration)
2802 return internal_record_declaration(declaration, false);
2805 static declaration_t *record_function_definition(declaration_t *declaration)
2807 return internal_record_declaration(declaration, true);
2810 static void parser_error_multiple_definition(declaration_t *declaration,
2811 const source_position_t source_position)
2813 errorf(source_position, "multiple definition of symbol '%Y'",
2814 declaration->symbol);
2815 errorf(declaration->source_position,
2816 "this is the location of the previous definition.");
2819 static bool is_declaration_specifier(const token_t *token,
2820 bool only_type_specifiers)
2822 switch(token->type) {
2826 return is_typedef_symbol(token->v.symbol);
2828 case T___extension__:
2831 return !only_type_specifiers;
2838 static void parse_init_declarator_rest(declaration_t *declaration)
2842 type_t *orig_type = declaration->type;
2843 type_t *type = skip_typeref(orig_type);
2845 if(declaration->init.initializer != NULL) {
2846 parser_error_multiple_definition(declaration, token.source_position);
2849 bool must_be_constant = false;
2850 if(declaration->storage_class == STORAGE_CLASS_STATIC
2851 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
2852 || declaration->parent_scope == global_scope) {
2853 must_be_constant = true;
2856 parse_initializer_env_t env;
2857 env.type = orig_type;
2858 env.must_be_constant = must_be_constant;
2859 parse_initializer(&env);
2861 if(env.type != orig_type) {
2862 orig_type = env.type;
2863 type = skip_typeref(orig_type);
2864 declaration->type = env.type;
2867 if(is_type_function(type)) {
2868 errorf(declaration->source_position,
2869 "initializers not allowed for function types at declator '%Y' (type '%T')",
2870 declaration->symbol, orig_type);
2872 declaration->init.initializer = env.initializer;
2876 /* parse rest of a declaration without any declarator */
2877 static void parse_anonymous_declaration_rest(
2878 const declaration_specifiers_t *specifiers,
2879 parsed_declaration_func finished_declaration)
2883 declaration_t *const declaration = allocate_declaration_zero();
2884 declaration->type = specifiers->type;
2885 declaration->declared_storage_class = specifiers->declared_storage_class;
2886 declaration->source_position = specifiers->source_position;
2888 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
2889 warningf(declaration->source_position, "useless storage class in empty declaration");
2891 declaration->storage_class = STORAGE_CLASS_NONE;
2893 type_t *type = declaration->type;
2894 switch (type->kind) {
2895 case TYPE_COMPOUND_STRUCT:
2896 case TYPE_COMPOUND_UNION: {
2897 if (type->compound.declaration->symbol == NULL) {
2898 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2907 warningf(declaration->source_position, "empty declaration");
2911 finished_declaration(declaration);
2914 static void parse_declaration_rest(declaration_t *ndeclaration,
2915 const declaration_specifiers_t *specifiers,
2916 parsed_declaration_func finished_declaration)
2919 declaration_t *declaration = finished_declaration(ndeclaration);
2921 type_t *orig_type = declaration->type;
2922 type_t *type = skip_typeref(orig_type);
2924 if (type->kind != TYPE_FUNCTION &&
2925 declaration->is_inline &&
2926 is_type_valid(type)) {
2927 warningf(declaration->source_position,
2928 "variable '%Y' declared 'inline'\n", declaration->symbol);
2931 if(token.type == '=') {
2932 parse_init_declarator_rest(declaration);
2935 if(token.type != ',')
2939 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2944 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2946 symbol_t *symbol = declaration->symbol;
2947 if(symbol == NULL) {
2948 errorf(HERE, "anonymous declaration not valid as function parameter");
2951 namespace_t namespc = (namespace_t) declaration->namespc;
2952 if(namespc != NAMESPACE_NORMAL) {
2953 return record_declaration(declaration);
2956 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2957 if(previous_declaration == NULL ||
2958 previous_declaration->parent_scope != scope) {
2959 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2964 if(previous_declaration->type == NULL) {
2965 previous_declaration->type = declaration->type;
2966 previous_declaration->declared_storage_class = declaration->declared_storage_class;
2967 previous_declaration->storage_class = declaration->storage_class;
2968 previous_declaration->parent_scope = scope;
2969 return previous_declaration;
2971 return record_declaration(declaration);
2975 static void parse_declaration(parsed_declaration_func finished_declaration)
2977 declaration_specifiers_t specifiers;
2978 memset(&specifiers, 0, sizeof(specifiers));
2979 parse_declaration_specifiers(&specifiers);
2981 if(token.type == ';') {
2982 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2984 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2985 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2989 static void parse_kr_declaration_list(declaration_t *declaration)
2991 type_t *type = skip_typeref(declaration->type);
2992 if(!is_type_function(type))
2995 if(!type->function.kr_style_parameters)
2998 /* push function parameters */
2999 int top = environment_top();
3000 scope_t *last_scope = scope;
3001 set_scope(&declaration->scope);
3003 declaration_t *parameter = declaration->scope.declarations;
3004 for( ; parameter != NULL; parameter = parameter->next) {
3005 assert(parameter->parent_scope == NULL);
3006 parameter->parent_scope = scope;
3007 environment_push(parameter);
3010 /* parse declaration list */
3011 while(is_declaration_specifier(&token, false)) {
3012 parse_declaration(finished_kr_declaration);
3015 /* pop function parameters */
3016 assert(scope == &declaration->scope);
3017 set_scope(last_scope);
3018 environment_pop_to(top);
3020 /* update function type */
3021 type_t *new_type = duplicate_type(type);
3022 new_type->function.kr_style_parameters = false;
3024 function_parameter_t *parameters = NULL;
3025 function_parameter_t *last_parameter = NULL;
3027 declaration_t *parameter_declaration = declaration->scope.declarations;
3028 for( ; parameter_declaration != NULL;
3029 parameter_declaration = parameter_declaration->next) {
3030 type_t *parameter_type = parameter_declaration->type;
3031 if(parameter_type == NULL) {
3033 errorf(HERE, "no type specified for function parameter '%Y'",
3034 parameter_declaration->symbol);
3036 if (warning.implicit_int) {
3037 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3038 parameter_declaration->symbol);
3040 parameter_type = type_int;
3041 parameter_declaration->type = parameter_type;
3045 semantic_parameter(parameter_declaration);
3046 parameter_type = parameter_declaration->type;
3048 function_parameter_t *function_parameter
3049 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3050 memset(function_parameter, 0, sizeof(function_parameter[0]));
3052 function_parameter->type = parameter_type;
3053 if(last_parameter != NULL) {
3054 last_parameter->next = function_parameter;
3056 parameters = function_parameter;
3058 last_parameter = function_parameter;
3060 new_type->function.parameters = parameters;
3062 type = typehash_insert(new_type);
3063 if(type != new_type) {
3064 obstack_free(type_obst, new_type);
3067 declaration->type = type;
3070 static bool first_err = true;
3073 * When called with first_err set, prints the name of the current function,
3076 static void print_in_function(void) {
3079 diagnosticf("%s: In function '%Y':\n",
3080 current_function->source_position.input_name,
3081 current_function->symbol);
3086 * Check if all labels are defined in the current function.
3087 * Check if all labels are used in the current function.
3089 static void check_labels(void)
3091 for (const goto_statement_t *goto_statement = goto_first;
3092 goto_statement != NULL;
3093 goto_statement = goto_statement->next) {
3094 declaration_t *label = goto_statement->label;
3097 if (label->source_position.input_name == NULL) {
3098 print_in_function();
3099 errorf(goto_statement->base.source_position,
3100 "label '%Y' used but not defined", label->symbol);
3103 goto_first = goto_last = NULL;
3105 if (warning.unused_label) {
3106 for (const label_statement_t *label_statement = label_first;
3107 label_statement != NULL;
3108 label_statement = label_statement->next) {
3109 const declaration_t *label = label_statement->label;
3111 if (! label->used) {
3112 print_in_function();
3113 warningf(label_statement->base.source_position,
3114 "label '%Y' defined but not used", label->symbol);
3118 label_first = label_last = NULL;
3122 * Check declarations of current_function for unused entities.
3124 static void check_declarations(void)
3126 if (warning.unused_parameter) {
3127 const scope_t *scope = ¤t_function->scope;
3129 const declaration_t *parameter = scope->declarations;
3130 for (; parameter != NULL; parameter = parameter->next) {
3131 if (! parameter->used) {
3132 print_in_function();
3133 warningf(parameter->source_position,
3134 "unused parameter '%Y'", parameter->symbol);
3138 if (warning.unused_variable) {
3142 static void parse_external_declaration(void)
3144 /* function-definitions and declarations both start with declaration
3146 declaration_specifiers_t specifiers;
3147 memset(&specifiers, 0, sizeof(specifiers));
3148 parse_declaration_specifiers(&specifiers);
3150 /* must be a declaration */
3151 if(token.type == ';') {
3152 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3156 /* declarator is common to both function-definitions and declarations */
3157 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3159 /* must be a declaration */
3160 if(token.type == ',' || token.type == '=' || token.type == ';') {
3161 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3165 /* must be a function definition */
3166 parse_kr_declaration_list(ndeclaration);
3168 if(token.type != '{') {
3169 parse_error_expected("while parsing function definition", '{', 0);
3174 type_t *type = ndeclaration->type;
3176 /* note that we don't skip typerefs: the standard doesn't allow them here
3177 * (so we can't use is_type_function here) */
3178 if(type->kind != TYPE_FUNCTION) {
3179 if (is_type_valid(type)) {
3180 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3181 type, ndeclaration->symbol);
3187 /* § 6.7.5.3 (14) a function definition with () means no
3188 * parameters (and not unspecified parameters) */
3189 if(type->function.unspecified_parameters) {
3190 type_t *duplicate = duplicate_type(type);
3191 duplicate->function.unspecified_parameters = false;
3193 type = typehash_insert(duplicate);
3194 if(type != duplicate) {
3195 obstack_free(type_obst, duplicate);
3197 ndeclaration->type = type;
3200 declaration_t *const declaration = record_function_definition(ndeclaration);
3201 if(ndeclaration != declaration) {
3202 declaration->scope = ndeclaration->scope;
3204 type = skip_typeref(declaration->type);
3206 /* push function parameters and switch scope */
3207 int top = environment_top();
3208 scope_t *last_scope = scope;
3209 set_scope(&declaration->scope);
3211 declaration_t *parameter = declaration->scope.declarations;
3212 for( ; parameter != NULL; parameter = parameter->next) {
3213 if(parameter->parent_scope == &ndeclaration->scope) {
3214 parameter->parent_scope = scope;
3216 assert(parameter->parent_scope == NULL
3217 || parameter->parent_scope == scope);
3218 parameter->parent_scope = scope;
3219 environment_push(parameter);
3222 if(declaration->init.statement != NULL) {
3223 parser_error_multiple_definition(declaration, token.source_position);
3225 goto end_of_parse_external_declaration;
3227 /* parse function body */
3228 int label_stack_top = label_top();
3229 declaration_t *old_current_function = current_function;
3230 current_function = declaration;
3232 declaration->init.statement = parse_compound_statement();
3235 check_declarations();
3237 assert(current_function == declaration);
3238 current_function = old_current_function;
3239 label_pop_to(label_stack_top);
3242 end_of_parse_external_declaration:
3243 assert(scope == &declaration->scope);
3244 set_scope(last_scope);
3245 environment_pop_to(top);
3248 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3249 source_position_t source_position)
3251 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3252 type->bitfield.base = base;
3253 type->bitfield.size = size;
3258 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3261 declaration_t *iter = compound_declaration->scope.declarations;
3262 for( ; iter != NULL; iter = iter->next) {
3263 if(iter->namespc != NAMESPACE_NORMAL)
3266 if(iter->symbol == NULL) {
3267 type_t *type = skip_typeref(iter->type);
3268 if(is_type_compound(type)) {
3269 declaration_t *result
3270 = find_compound_entry(type->compound.declaration, symbol);
3277 if(iter->symbol == symbol) {
3285 static void parse_compound_declarators(declaration_t *struct_declaration,
3286 const declaration_specifiers_t *specifiers)
3288 declaration_t *last_declaration = struct_declaration->scope.declarations;
3289 if(last_declaration != NULL) {
3290 while(last_declaration->next != NULL) {
3291 last_declaration = last_declaration->next;
3296 declaration_t *declaration;
3298 if(token.type == ':') {
3299 source_position_t source_position = HERE;
3302 type_t *base_type = specifiers->type;
3303 expression_t *size = parse_constant_expression();
3305 if(!is_type_integer(skip_typeref(base_type))) {
3306 errorf(HERE, "bitfield base type '%T' is not an integer type",
3310 type_t *type = make_bitfield_type(base_type, size, source_position);
3312 declaration = allocate_declaration_zero();
3313 declaration->namespc = NAMESPACE_NORMAL;
3314 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3315 declaration->storage_class = STORAGE_CLASS_NONE;
3316 declaration->source_position = source_position;
3317 declaration->modifiers = specifiers->decl_modifiers;
3318 declaration->type = type;
3320 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3322 type_t *orig_type = declaration->type;
3323 type_t *type = skip_typeref(orig_type);
3325 if(token.type == ':') {
3326 source_position_t source_position = HERE;
3328 expression_t *size = parse_constant_expression();
3330 if(!is_type_integer(type)) {
3331 errorf(HERE, "bitfield base type '%T' is not an "
3332 "integer type", orig_type);
3335 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3336 declaration->type = bitfield_type;
3338 /* TODO we ignore arrays for now... what is missing is a check
3339 * that they're at the end of the struct */
3340 if(is_type_incomplete(type) && !is_type_array(type)) {
3342 "compound member '%Y' has incomplete type '%T'",
3343 declaration->symbol, orig_type);
3344 } else if(is_type_function(type)) {
3345 errorf(HERE, "compound member '%Y' must not have function "
3346 "type '%T'", declaration->symbol, orig_type);
3351 /* make sure we don't define a symbol multiple times */
3352 symbol_t *symbol = declaration->symbol;
3353 if(symbol != NULL) {
3354 declaration_t *prev_decl
3355 = find_compound_entry(struct_declaration, symbol);
3357 if(prev_decl != NULL) {
3358 assert(prev_decl->symbol == symbol);
3359 errorf(declaration->source_position,
3360 "multiple declarations of symbol '%Y'", symbol);
3361 errorf(prev_decl->source_position,
3362 "previous declaration of '%Y' was here", symbol);
3366 /* append declaration */
3367 if(last_declaration != NULL) {
3368 last_declaration->next = declaration;
3370 struct_declaration->scope.declarations = declaration;
3372 last_declaration = declaration;
3374 if(token.type != ',')
3381 static void parse_compound_type_entries(declaration_t *compound_declaration)
3385 while(token.type != '}' && token.type != T_EOF) {
3386 declaration_specifiers_t specifiers;
3387 memset(&specifiers, 0, sizeof(specifiers));
3388 parse_declaration_specifiers(&specifiers);
3390 parse_compound_declarators(compound_declaration, &specifiers);
3392 if(token.type == T_EOF) {
3393 errorf(HERE, "EOF while parsing struct");
3398 static type_t *parse_typename(void)
3400 declaration_specifiers_t specifiers;
3401 memset(&specifiers, 0, sizeof(specifiers));
3402 parse_declaration_specifiers(&specifiers);
3403 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3404 /* TODO: improve error message, user does probably not know what a
3405 * storage class is...
3407 errorf(HERE, "typename may not have a storage class");
3410 type_t *result = parse_abstract_declarator(specifiers.type);
3418 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3419 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3420 expression_t *left);
3422 typedef struct expression_parser_function_t expression_parser_function_t;
3423 struct expression_parser_function_t {
3424 unsigned precedence;
3425 parse_expression_function parser;
3426 unsigned infix_precedence;
3427 parse_expression_infix_function infix_parser;
3430 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3433 * Creates a new invalid expression.
3435 static expression_t *create_invalid_expression(void)
3437 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3438 expression->base.source_position = token.source_position;
3443 * Prints an error message if an expression was expected but not read
3445 static expression_t *expected_expression_error(void)
3447 /* skip the error message if the error token was read */
3448 if (token.type != T_ERROR) {
3449 errorf(HERE, "expected expression, got token '%K'", &token);
3453 return create_invalid_expression();
3457 * Parse a string constant.
3459 static expression_t *parse_string_const(void)
3462 if (token.type == T_STRING_LITERAL) {
3463 string_t res = token.v.string;
3465 while (token.type == T_STRING_LITERAL) {
3466 res = concat_strings(&res, &token.v.string);
3469 if (token.type != T_WIDE_STRING_LITERAL) {
3470 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3471 /* note: that we use type_char_ptr here, which is already the
3472 * automatic converted type. revert_automatic_type_conversion
3473 * will construct the array type */
3474 cnst->base.type = type_char_ptr;
3475 cnst->string.value = res;
3479 wres = concat_string_wide_string(&res, &token.v.wide_string);
3481 wres = token.v.wide_string;
3486 switch (token.type) {
3487 case T_WIDE_STRING_LITERAL:
3488 wres = concat_wide_strings(&wres, &token.v.wide_string);
3491 case T_STRING_LITERAL:
3492 wres = concat_wide_string_string(&wres, &token.v.string);
3496 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3497 cnst->base.type = type_wchar_t_ptr;
3498 cnst->wide_string.value = wres;
3507 * Parse an integer constant.
3509 static expression_t *parse_int_const(void)
3511 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3512 cnst->base.source_position = HERE;
3513 cnst->base.type = token.datatype;
3514 cnst->conste.v.int_value = token.v.intvalue;
3522 * Parse a character constant.
3524 static expression_t *parse_char_const(void)
3526 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3527 cnst->base.source_position = HERE;
3528 cnst->base.type = token.datatype;
3529 cnst->conste.v.chars.begin = token.v.string.begin;
3530 cnst->conste.v.chars.size = token.v.string.size;
3532 if (cnst->conste.v.chars.size != 1) {
3533 if (warning.multichar && (c_mode & _GNUC)) {
3535 warningf(HERE, "multi-character character constant");
3537 errorf(HERE, "more than 1 characters in character constant");
3546 * Parse a float constant.
3548 static expression_t *parse_float_const(void)
3550 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3551 cnst->base.type = token.datatype;
3552 cnst->conste.v.float_value = token.v.floatvalue;
3559 static declaration_t *create_implicit_function(symbol_t *symbol,
3560 const source_position_t source_position)
3562 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3563 ntype->function.return_type = type_int;
3564 ntype->function.unspecified_parameters = true;
3566 type_t *type = typehash_insert(ntype);
3571 declaration_t *const declaration = allocate_declaration_zero();
3572 declaration->storage_class = STORAGE_CLASS_EXTERN;
3573 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3574 declaration->type = type;
3575 declaration->symbol = symbol;
3576 declaration->source_position = source_position;
3577 declaration->parent_scope = global_scope;
3579 scope_t *old_scope = scope;
3580 set_scope(global_scope);
3582 environment_push(declaration);
3583 /* prepends the declaration to the global declarations list */
3584 declaration->next = scope->declarations;
3585 scope->declarations = declaration;
3587 assert(scope == global_scope);
3588 set_scope(old_scope);
3594 * Creates a return_type (func)(argument_type) function type if not
3597 * @param return_type the return type
3598 * @param argument_type the argument type
3600 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3602 function_parameter_t *parameter
3603 = obstack_alloc(type_obst, sizeof(parameter[0]));
3604 memset(parameter, 0, sizeof(parameter[0]));
3605 parameter->type = argument_type;
3607 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3608 type->function.return_type = return_type;
3609 type->function.parameters = parameter;
3611 type_t *result = typehash_insert(type);
3612 if(result != type) {
3620 * Creates a function type for some function like builtins.
3622 * @param symbol the symbol describing the builtin
3624 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3626 switch(symbol->ID) {
3627 case T___builtin_alloca:
3628 return make_function_1_type(type_void_ptr, type_size_t);
3629 case T___builtin_nan:
3630 return make_function_1_type(type_double, type_char_ptr);
3631 case T___builtin_nanf:
3632 return make_function_1_type(type_float, type_char_ptr);
3633 case T___builtin_nand:
3634 return make_function_1_type(type_long_double, type_char_ptr);
3635 case T___builtin_va_end:
3636 return make_function_1_type(type_void, type_valist);
3638 panic("not implemented builtin symbol found");
3643 * Performs automatic type cast as described in § 6.3.2.1.
3645 * @param orig_type the original type
3647 static type_t *automatic_type_conversion(type_t *orig_type)
3649 type_t *type = skip_typeref(orig_type);
3650 if(is_type_array(type)) {
3651 array_type_t *array_type = &type->array;
3652 type_t *element_type = array_type->element_type;
3653 unsigned qualifiers = array_type->type.qualifiers;
3655 return make_pointer_type(element_type, qualifiers);
3658 if(is_type_function(type)) {
3659 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3666 * reverts the automatic casts of array to pointer types and function
3667 * to function-pointer types as defined § 6.3.2.1
3669 type_t *revert_automatic_type_conversion(const expression_t *expression)
3671 switch (expression->kind) {
3672 case EXPR_REFERENCE: return expression->reference.declaration->type;
3673 case EXPR_SELECT: return expression->select.compound_entry->type;
3675 case EXPR_UNARY_DEREFERENCE: {
3676 const expression_t *const value = expression->unary.value;
3677 type_t *const type = skip_typeref(value->base.type);
3678 assert(is_type_pointer(type));
3679 return type->pointer.points_to;
3682 case EXPR_BUILTIN_SYMBOL:
3683 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3685 case EXPR_ARRAY_ACCESS: {
3686 const expression_t *array_ref = expression->array_access.array_ref;
3687 type_t *type_left = skip_typeref(array_ref->base.type);
3688 if (!is_type_valid(type_left))
3690 assert(is_type_pointer(type_left));
3691 return type_left->pointer.points_to;
3694 case EXPR_STRING_LITERAL: {
3695 size_t size = expression->string.value.size;
3696 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
3699 case EXPR_WIDE_STRING_LITERAL: {
3700 size_t size = expression->wide_string.value.size;
3701 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
3704 case EXPR_COMPOUND_LITERAL:
3705 return expression->compound_literal.type;
3710 return expression->base.type;
3713 static expression_t *parse_reference(void)
3715 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3717 reference_expression_t *ref = &expression->reference;
3718 ref->symbol = token.v.symbol;
3720 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3722 source_position_t source_position = token.source_position;
3725 if(declaration == NULL) {
3726 if (! strict_mode && token.type == '(') {
3727 /* an implicitly defined function */
3728 if (warning.implicit_function_declaration) {
3729 warningf(HERE, "implicit declaration of function '%Y'",
3733 declaration = create_implicit_function(ref->symbol,
3736 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3737 return create_invalid_expression();
3741 type_t *type = declaration->type;
3743 /* we always do the auto-type conversions; the & and sizeof parser contains
3744 * code to revert this! */
3745 type = automatic_type_conversion(type);
3747 ref->declaration = declaration;
3748 ref->base.type = type;
3750 /* this declaration is used */
3751 declaration->used = true;
3756 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3760 /* TODO check if explicit cast is allowed and issue warnings/errors */
3763 static expression_t *parse_compound_literal(type_t *type)
3765 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3767 parse_initializer_env_t env;
3769 env.must_be_constant = false;
3770 parse_initializer(&env);
3773 expression->compound_literal.type = type;
3774 expression->compound_literal.initializer = env.initializer;
3775 expression->base.type = automatic_type_conversion(type);
3780 static expression_t *parse_cast(void)
3782 source_position_t source_position = token.source_position;
3784 type_t *type = parse_typename();
3788 if(token.type == '{') {
3789 return parse_compound_literal(type);
3792 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3793 cast->base.source_position = source_position;
3795 expression_t *value = parse_sub_expression(20);
3797 check_cast_allowed(value, type);
3799 cast->base.type = type;
3800 cast->unary.value = value;
3805 static expression_t *parse_statement_expression(void)
3807 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3809 statement_t *statement = parse_compound_statement();
3810 expression->statement.statement = statement;
3811 expression->base.source_position = statement->base.source_position;
3813 /* find last statement and use its type */
3814 type_t *type = type_void;
3815 const statement_t *stmt = statement->compound.statements;
3817 while (stmt->base.next != NULL)
3818 stmt = stmt->base.next;
3820 if (stmt->kind == STATEMENT_EXPRESSION) {
3821 type = stmt->expression.expression->base.type;
3824 warningf(expression->base.source_position, "empty statement expression ({})");
3826 expression->base.type = type;
3833 static expression_t *parse_brace_expression(void)
3837 switch(token.type) {
3839 /* gcc extension: a statement expression */
3840 return parse_statement_expression();
3844 return parse_cast();
3846 if(is_typedef_symbol(token.v.symbol)) {
3847 return parse_cast();
3851 expression_t *result = parse_expression();
3857 static expression_t *parse_function_keyword(void)
3862 if (current_function == NULL) {
3863 errorf(HERE, "'__func__' used outside of a function");
3866 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3867 expression->base.type = type_char_ptr;
3872 static expression_t *parse_pretty_function_keyword(void)
3874 eat(T___PRETTY_FUNCTION__);
3877 if (current_function == NULL) {
3878 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3881 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3882 expression->base.type = type_char_ptr;
3887 static designator_t *parse_designator(void)
3889 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3890 result->source_position = HERE;
3892 if(token.type != T_IDENTIFIER) {
3893 parse_error_expected("while parsing member designator",
3898 result->symbol = token.v.symbol;
3901 designator_t *last_designator = result;
3903 if(token.type == '.') {
3905 if(token.type != T_IDENTIFIER) {
3906 parse_error_expected("while parsing member designator",
3911 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3912 designator->source_position = HERE;
3913 designator->symbol = token.v.symbol;
3916 last_designator->next = designator;
3917 last_designator = designator;
3920 if(token.type == '[') {
3922 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3923 designator->source_position = HERE;
3924 designator->array_index = parse_expression();
3925 if(designator->array_index == NULL) {
3931 last_designator->next = designator;
3932 last_designator = designator;
3941 static expression_t *parse_offsetof(void)
3943 eat(T___builtin_offsetof);
3945 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3946 expression->base.type = type_size_t;
3949 type_t *type = parse_typename();
3951 designator_t *designator = parse_designator();
3954 expression->offsetofe.type = type;
3955 expression->offsetofe.designator = designator;
3958 memset(&path, 0, sizeof(path));
3959 path.top_type = type;
3960 path.path = NEW_ARR_F(type_path_entry_t, 0);
3962 descend_into_subtype(&path);
3964 if(!walk_designator(&path, designator, true)) {
3965 return create_invalid_expression();
3968 DEL_ARR_F(path.path);
3973 static expression_t *parse_va_start(void)
3975 eat(T___builtin_va_start);
3977 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3980 expression->va_starte.ap = parse_assignment_expression();
3982 expression_t *const expr = parse_assignment_expression();
3983 if (expr->kind == EXPR_REFERENCE) {
3984 declaration_t *const decl = expr->reference.declaration;
3986 return create_invalid_expression();
3987 if (decl->parent_scope == ¤t_function->scope &&
3988 decl->next == NULL) {
3989 expression->va_starte.parameter = decl;
3994 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3996 return create_invalid_expression();
3999 static expression_t *parse_va_arg(void)
4001 eat(T___builtin_va_arg);
4003 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4006 expression->va_arge.ap = parse_assignment_expression();
4008 expression->base.type = parse_typename();
4014 static expression_t *parse_builtin_symbol(void)
4016 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4018 symbol_t *symbol = token.v.symbol;
4020 expression->builtin_symbol.symbol = symbol;
4023 type_t *type = get_builtin_symbol_type(symbol);
4024 type = automatic_type_conversion(type);
4026 expression->base.type = type;
4030 static expression_t *parse_builtin_constant(void)
4032 eat(T___builtin_constant_p);
4034 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4037 expression->builtin_constant.value = parse_assignment_expression();
4039 expression->base.type = type_int;
4044 static expression_t *parse_builtin_prefetch(void)
4046 eat(T___builtin_prefetch);
4048 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4051 expression->builtin_prefetch.adr = parse_assignment_expression();
4052 if (token.type == ',') {
4054 expression->builtin_prefetch.rw = parse_assignment_expression();
4056 if (token.type == ',') {
4058 expression->builtin_prefetch.locality = parse_assignment_expression();
4061 expression->base.type = type_void;
4066 static expression_t *parse_compare_builtin(void)
4068 expression_t *expression;
4070 switch(token.type) {
4071 case T___builtin_isgreater:
4072 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4074 case T___builtin_isgreaterequal:
4075 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4077 case T___builtin_isless:
4078 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4080 case T___builtin_islessequal:
4081 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4083 case T___builtin_islessgreater:
4084 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4086 case T___builtin_isunordered:
4087 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4090 panic("invalid compare builtin found");
4093 expression->base.source_position = HERE;
4097 expression->binary.left = parse_assignment_expression();
4099 expression->binary.right = parse_assignment_expression();
4102 type_t *const orig_type_left = expression->binary.left->base.type;
4103 type_t *const orig_type_right = expression->binary.right->base.type;
4105 type_t *const type_left = skip_typeref(orig_type_left);
4106 type_t *const type_right = skip_typeref(orig_type_right);
4107 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4108 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4109 type_error_incompatible("invalid operands in comparison",
4110 expression->base.source_position, orig_type_left, orig_type_right);
4113 semantic_comparison(&expression->binary);
4119 static expression_t *parse_builtin_expect(void)
4121 eat(T___builtin_expect);
4123 expression_t *expression
4124 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4127 expression->binary.left = parse_assignment_expression();
4129 expression->binary.right = parse_constant_expression();
4132 expression->base.type = expression->binary.left->base.type;
4137 static expression_t *parse_assume(void) {
4140 expression_t *expression
4141 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4144 expression->unary.value = parse_assignment_expression();
4147 expression->base.type = type_void;
4151 static expression_t *parse_primary_expression(void)
4153 switch (token.type) {
4154 case T_INTEGER: return parse_int_const();
4155 case T_CHARS: return parse_char_const();
4156 case T_FLOATINGPOINT: return parse_float_const();
4157 case T_STRING_LITERAL:
4158 case T_WIDE_STRING_LITERAL: return parse_string_const();
4159 case T_IDENTIFIER: return parse_reference();
4160 case T___FUNCTION__:
4161 case T___func__: return parse_function_keyword();
4162 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4163 case T___builtin_offsetof: return parse_offsetof();
4164 case T___builtin_va_start: return parse_va_start();
4165 case T___builtin_va_arg: return parse_va_arg();
4166 case T___builtin_expect: return parse_builtin_expect();
4167 case T___builtin_alloca:
4168 case T___builtin_nan:
4169 case T___builtin_nand:
4170 case T___builtin_nanf:
4171 case T___builtin_va_end: return parse_builtin_symbol();
4172 case T___builtin_isgreater:
4173 case T___builtin_isgreaterequal:
4174 case T___builtin_isless:
4175 case T___builtin_islessequal:
4176 case T___builtin_islessgreater:
4177 case T___builtin_isunordered: return parse_compare_builtin();
4178 case T___builtin_constant_p: return parse_builtin_constant();
4179 case T___builtin_prefetch: return parse_builtin_prefetch();
4180 case T_assume: return parse_assume();
4182 case '(': return parse_brace_expression();
4185 errorf(HERE, "unexpected token %K, expected an expression", &token);
4188 return create_invalid_expression();
4192 * Check if the expression has the character type and issue a warning then.
4194 static void check_for_char_index_type(const expression_t *expression) {
4195 type_t *const type = expression->base.type;
4196 const type_t *const base_type = skip_typeref(type);
4198 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4199 warning.char_subscripts) {
4200 warningf(expression->base.source_position,
4201 "array subscript has type '%T'", type);
4205 static expression_t *parse_array_expression(unsigned precedence,
4212 expression_t *inside = parse_expression();
4214 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4216 array_access_expression_t *array_access = &expression->array_access;
4218 type_t *const orig_type_left = left->base.type;
4219 type_t *const orig_type_inside = inside->base.type;
4221 type_t *const type_left = skip_typeref(orig_type_left);
4222 type_t *const type_inside = skip_typeref(orig_type_inside);
4224 type_t *return_type;
4225 if (is_type_pointer(type_left)) {
4226 return_type = type_left->pointer.points_to;
4227 array_access->array_ref = left;
4228 array_access->index = inside;
4229 check_for_char_index_type(inside);
4230 } else if (is_type_pointer(type_inside)) {
4231 return_type = type_inside->pointer.points_to;
4232 array_access->array_ref = inside;
4233 array_access->index = left;
4234 array_access->flipped = true;
4235 check_for_char_index_type(left);
4237 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4239 "array access on object with non-pointer types '%T', '%T'",
4240 orig_type_left, orig_type_inside);
4242 return_type = type_error_type;
4243 array_access->array_ref = create_invalid_expression();
4246 if(token.type != ']') {
4247 parse_error_expected("Problem while parsing array access", ']', 0);
4252 return_type = automatic_type_conversion(return_type);
4253 expression->base.type = return_type;
4258 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4260 expression_t *tp_expression = allocate_expression_zero(kind);
4261 tp_expression->base.type = type_size_t;
4263 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4265 tp_expression->typeprop.type = parse_typename();
4268 expression_t *expression = parse_sub_expression(precedence);
4269 expression->base.type = revert_automatic_type_conversion(expression);
4271 tp_expression->typeprop.type = expression->base.type;
4272 tp_expression->typeprop.tp_expression = expression;
4275 return tp_expression;
4278 static expression_t *parse_sizeof(unsigned precedence)
4281 return parse_typeprop(EXPR_SIZEOF, precedence);
4284 static expression_t *parse_alignof(unsigned precedence)
4287 return parse_typeprop(EXPR_SIZEOF, precedence);
4290 static expression_t *parse_select_expression(unsigned precedence,
4291 expression_t *compound)
4294 assert(token.type == '.' || token.type == T_MINUSGREATER);
4296 bool is_pointer = (token.type == T_MINUSGREATER);
4299 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4300 select->select.compound = compound;
4302 if(token.type != T_IDENTIFIER) {
4303 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4306 symbol_t *symbol = token.v.symbol;
4307 select->select.symbol = symbol;
4310 type_t *const orig_type = compound->base.type;
4311 type_t *const type = skip_typeref(orig_type);
4313 type_t *type_left = type;
4315 if (!is_type_pointer(type)) {
4316 if (is_type_valid(type)) {
4317 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4319 return create_invalid_expression();
4321 type_left = type->pointer.points_to;
4323 type_left = skip_typeref(type_left);
4325 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4326 type_left->kind != TYPE_COMPOUND_UNION) {
4327 if (is_type_valid(type_left)) {
4328 errorf(HERE, "request for member '%Y' in something not a struct or "
4329 "union, but '%T'", symbol, type_left);
4331 return create_invalid_expression();
4334 declaration_t *const declaration = type_left->compound.declaration;
4336 if(!declaration->init.is_defined) {
4337 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4339 return create_invalid_expression();
4342 declaration_t *iter = find_compound_entry(declaration, symbol);
4344 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4345 return create_invalid_expression();
4348 /* we always do the auto-type conversions; the & and sizeof parser contains
4349 * code to revert this! */
4350 type_t *expression_type = automatic_type_conversion(iter->type);
4352 select->select.compound_entry = iter;
4353 select->base.type = expression_type;
4355 if(expression_type->kind == TYPE_BITFIELD) {
4356 expression_t *extract
4357 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4358 extract->unary.value = select;
4359 extract->base.type = expression_type->bitfield.base;
4368 * Parse a call expression, ie. expression '( ... )'.
4370 * @param expression the function address
4372 static expression_t *parse_call_expression(unsigned precedence,
4373 expression_t *expression)
4376 expression_t *result = allocate_expression_zero(EXPR_CALL);
4378 call_expression_t *call = &result->call;
4379 call->function = expression;
4381 type_t *const orig_type = expression->base.type;
4382 type_t *const type = skip_typeref(orig_type);
4384 function_type_t *function_type = NULL;
4385 if (is_type_pointer(type)) {
4386 type_t *const to_type = skip_typeref(type->pointer.points_to);
4388 if (is_type_function(to_type)) {
4389 function_type = &to_type->function;
4390 call->base.type = function_type->return_type;
4394 if (function_type == NULL && is_type_valid(type)) {
4395 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4398 /* parse arguments */
4401 if(token.type != ')') {
4402 call_argument_t *last_argument = NULL;
4405 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4407 argument->expression = parse_assignment_expression();
4408 if(last_argument == NULL) {
4409 call->arguments = argument;
4411 last_argument->next = argument;
4413 last_argument = argument;
4415 if(token.type != ',')
4422 if(function_type != NULL) {
4423 function_parameter_t *parameter = function_type->parameters;
4424 call_argument_t *argument = call->arguments;
4425 for( ; parameter != NULL && argument != NULL;
4426 parameter = parameter->next, argument = argument->next) {
4427 type_t *expected_type = parameter->type;
4428 /* TODO report scope in error messages */
4429 expression_t *const arg_expr = argument->expression;
4430 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4431 if (res_type == NULL) {
4432 /* TODO improve error message */
4433 errorf(arg_expr->base.source_position,
4434 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4435 arg_expr, arg_expr->base.type, expected_type);
4437 argument->expression = create_implicit_cast(argument->expression, expected_type);
4440 /* too few parameters */
4441 if(parameter != NULL) {
4442 errorf(HERE, "too few arguments to function '%E'", expression);
4443 } else if(argument != NULL) {
4444 /* too many parameters */
4445 if(!function_type->variadic
4446 && !function_type->unspecified_parameters) {
4447 errorf(HERE, "too many arguments to function '%E'", expression);
4449 /* do default promotion */
4450 for( ; argument != NULL; argument = argument->next) {
4451 type_t *type = argument->expression->base.type;
4453 type = skip_typeref(type);
4454 if(is_type_integer(type)) {
4455 type = promote_integer(type);
4456 } else if(type == type_float) {
4460 argument->expression
4461 = create_implicit_cast(argument->expression, type);
4464 check_format(&result->call);
4467 check_format(&result->call);
4474 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4476 static bool same_compound_type(const type_t *type1, const type_t *type2)
4479 is_type_compound(type1) &&
4480 type1->kind == type2->kind &&
4481 type1->compound.declaration == type2->compound.declaration;
4485 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4487 * @param expression the conditional expression
4489 static expression_t *parse_conditional_expression(unsigned precedence,
4490 expression_t *expression)
4494 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4496 conditional_expression_t *conditional = &result->conditional;
4497 conditional->condition = expression;
4500 type_t *const condition_type_orig = expression->base.type;
4501 type_t *const condition_type = skip_typeref(condition_type_orig);
4502 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4503 type_error("expected a scalar type in conditional condition",
4504 expression->base.source_position, condition_type_orig);
4507 expression_t *true_expression = parse_expression();
4509 expression_t *false_expression = parse_sub_expression(precedence);
4511 type_t *const orig_true_type = true_expression->base.type;
4512 type_t *const orig_false_type = false_expression->base.type;
4513 type_t *const true_type = skip_typeref(orig_true_type);
4514 type_t *const false_type = skip_typeref(orig_false_type);
4517 type_t *result_type;
4518 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4519 result_type = semantic_arithmetic(true_type, false_type);
4521 true_expression = create_implicit_cast(true_expression, result_type);
4522 false_expression = create_implicit_cast(false_expression, result_type);
4524 conditional->true_expression = true_expression;
4525 conditional->false_expression = false_expression;
4526 conditional->base.type = result_type;
4527 } else if (same_compound_type(true_type, false_type) || (
4528 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4529 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4531 /* just take 1 of the 2 types */
4532 result_type = true_type;
4533 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4534 && pointers_compatible(true_type, false_type)) {
4536 result_type = true_type;
4537 } else if (is_type_pointer(true_type)
4538 && is_null_pointer_constant(false_expression)) {
4539 result_type = true_type;
4540 } else if (is_type_pointer(false_type)
4541 && is_null_pointer_constant(true_expression)) {
4542 result_type = false_type;
4544 /* TODO: one pointer to void*, other some pointer */
4546 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4547 type_error_incompatible("while parsing conditional",
4548 expression->base.source_position, true_type,
4551 result_type = type_error_type;
4554 conditional->true_expression
4555 = create_implicit_cast(true_expression, result_type);
4556 conditional->false_expression
4557 = create_implicit_cast(false_expression, result_type);
4558 conditional->base.type = result_type;
4563 * Parse an extension expression.
4565 static expression_t *parse_extension(unsigned precedence)
4567 eat(T___extension__);
4569 /* TODO enable extensions */
4570 expression_t *expression = parse_sub_expression(precedence);
4571 /* TODO disable extensions */
4575 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4577 eat(T___builtin_classify_type);
4579 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4580 result->base.type = type_int;
4583 expression_t *expression = parse_sub_expression(precedence);
4585 result->classify_type.type_expression = expression;
4590 static void semantic_incdec(unary_expression_t *expression)
4592 type_t *const orig_type = expression->value->base.type;
4593 type_t *const type = skip_typeref(orig_type);
4594 /* TODO !is_type_real && !is_type_pointer */
4595 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4596 if (is_type_valid(type)) {
4597 /* TODO: improve error message */
4598 errorf(HERE, "operation needs an arithmetic or pointer type");
4603 expression->base.type = orig_type;
4606 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4608 type_t *const orig_type = expression->value->base.type;
4609 type_t *const type = skip_typeref(orig_type);
4610 if(!is_type_arithmetic(type)) {
4611 if (is_type_valid(type)) {
4612 /* TODO: improve error message */
4613 errorf(HERE, "operation needs an arithmetic type");
4618 expression->base.type = orig_type;
4621 static void semantic_unexpr_scalar(unary_expression_t *expression)
4623 type_t *const orig_type = expression->value->base.type;
4624 type_t *const type = skip_typeref(orig_type);
4625 if (!is_type_scalar(type)) {
4626 if (is_type_valid(type)) {
4627 errorf(HERE, "operand of ! must be of scalar type");
4632 expression->base.type = orig_type;
4635 static void semantic_unexpr_integer(unary_expression_t *expression)
4637 type_t *const orig_type = expression->value->base.type;
4638 type_t *const type = skip_typeref(orig_type);
4639 if (!is_type_integer(type)) {
4640 if (is_type_valid(type)) {
4641 errorf(HERE, "operand of ~ must be of integer type");
4646 expression->base.type = orig_type;
4649 static void semantic_dereference(unary_expression_t *expression)
4651 type_t *const orig_type = expression->value->base.type;
4652 type_t *const type = skip_typeref(orig_type);
4653 if(!is_type_pointer(type)) {
4654 if (is_type_valid(type)) {
4655 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4660 type_t *result_type = type->pointer.points_to;
4661 result_type = automatic_type_conversion(result_type);
4662 expression->base.type = result_type;
4666 * Check the semantic of the address taken expression.
4668 static void semantic_take_addr(unary_expression_t *expression)
4670 expression_t *value = expression->value;
4671 value->base.type = revert_automatic_type_conversion(value);
4673 type_t *orig_type = value->base.type;
4674 if(!is_type_valid(orig_type))
4677 if(value->kind == EXPR_REFERENCE) {
4678 declaration_t *const declaration = value->reference.declaration;
4679 if(declaration != NULL) {
4680 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4681 errorf(expression->base.source_position,
4682 "address of register variable '%Y' requested",
4683 declaration->symbol);
4685 declaration->address_taken = 1;
4689 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4692 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4693 static expression_t *parse_##unexpression_type(unsigned precedence) \
4697 expression_t *unary_expression \
4698 = allocate_expression_zero(unexpression_type); \
4699 unary_expression->base.source_position = HERE; \
4700 unary_expression->unary.value = parse_sub_expression(precedence); \
4702 sfunc(&unary_expression->unary); \
4704 return unary_expression; \
4707 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4708 semantic_unexpr_arithmetic)
4709 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4710 semantic_unexpr_arithmetic)
4711 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4712 semantic_unexpr_scalar)
4713 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4714 semantic_dereference)
4715 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4717 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4718 semantic_unexpr_integer)
4719 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4721 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4724 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4726 static expression_t *parse_##unexpression_type(unsigned precedence, \
4727 expression_t *left) \
4729 (void) precedence; \
4732 expression_t *unary_expression \
4733 = allocate_expression_zero(unexpression_type); \
4734 unary_expression->unary.value = left; \
4736 sfunc(&unary_expression->unary); \
4738 return unary_expression; \
4741 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4742 EXPR_UNARY_POSTFIX_INCREMENT,
4744 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4745 EXPR_UNARY_POSTFIX_DECREMENT,
4748 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4750 /* TODO: handle complex + imaginary types */
4752 /* § 6.3.1.8 Usual arithmetic conversions */
4753 if(type_left == type_long_double || type_right == type_long_double) {
4754 return type_long_double;
4755 } else if(type_left == type_double || type_right == type_double) {
4757 } else if(type_left == type_float || type_right == type_float) {
4761 type_right = promote_integer(type_right);
4762 type_left = promote_integer(type_left);
4764 if(type_left == type_right)
4767 bool signed_left = is_type_signed(type_left);
4768 bool signed_right = is_type_signed(type_right);
4769 int rank_left = get_rank(type_left);
4770 int rank_right = get_rank(type_right);
4771 if(rank_left < rank_right) {
4772 if(signed_left == signed_right || !signed_right) {
4778 if(signed_left == signed_right || !signed_left) {
4787 * Check the semantic restrictions for a binary expression.
4789 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4791 expression_t *const left = expression->left;
4792 expression_t *const right = expression->right;
4793 type_t *const orig_type_left = left->base.type;
4794 type_t *const orig_type_right = right->base.type;
4795 type_t *const type_left = skip_typeref(orig_type_left);
4796 type_t *const type_right = skip_typeref(orig_type_right);
4798 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4799 /* TODO: improve error message */
4800 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4801 errorf(HERE, "operation needs arithmetic types");
4806 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4807 expression->left = create_implicit_cast(left, arithmetic_type);
4808 expression->right = create_implicit_cast(right, arithmetic_type);
4809 expression->base.type = arithmetic_type;
4812 static void semantic_shift_op(binary_expression_t *expression)
4814 expression_t *const left = expression->left;
4815 expression_t *const right = expression->right;
4816 type_t *const orig_type_left = left->base.type;
4817 type_t *const orig_type_right = right->base.type;
4818 type_t * type_left = skip_typeref(orig_type_left);
4819 type_t * type_right = skip_typeref(orig_type_right);
4821 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4822 /* TODO: improve error message */
4823 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4824 errorf(HERE, "operation needs integer types");
4829 type_left = promote_integer(type_left);
4830 type_right = promote_integer(type_right);
4832 expression->left = create_implicit_cast(left, type_left);
4833 expression->right = create_implicit_cast(right, type_right);
4834 expression->base.type = type_left;
4837 static void semantic_add(binary_expression_t *expression)
4839 expression_t *const left = expression->left;
4840 expression_t *const right = expression->right;
4841 type_t *const orig_type_left = left->base.type;
4842 type_t *const orig_type_right = right->base.type;
4843 type_t *const type_left = skip_typeref(orig_type_left);
4844 type_t *const type_right = skip_typeref(orig_type_right);
4847 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4848 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4849 expression->left = create_implicit_cast(left, arithmetic_type);
4850 expression->right = create_implicit_cast(right, arithmetic_type);
4851 expression->base.type = arithmetic_type;
4853 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4854 expression->base.type = type_left;
4855 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4856 expression->base.type = type_right;
4857 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4858 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4862 static void semantic_sub(binary_expression_t *expression)
4864 expression_t *const left = expression->left;
4865 expression_t *const right = expression->right;
4866 type_t *const orig_type_left = left->base.type;
4867 type_t *const orig_type_right = right->base.type;
4868 type_t *const type_left = skip_typeref(orig_type_left);
4869 type_t *const type_right = skip_typeref(orig_type_right);
4872 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4873 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4874 expression->left = create_implicit_cast(left, arithmetic_type);
4875 expression->right = create_implicit_cast(right, arithmetic_type);
4876 expression->base.type = arithmetic_type;
4878 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4879 expression->base.type = type_left;
4880 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4881 if(!pointers_compatible(type_left, type_right)) {
4883 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4884 orig_type_left, orig_type_right);
4886 expression->base.type = type_ptrdiff_t;
4888 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4889 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4890 orig_type_left, orig_type_right);
4895 * Check the semantics of comparison expressions.
4897 * @param expression The expression to check.
4899 static void semantic_comparison(binary_expression_t *expression)
4901 expression_t *left = expression->left;
4902 expression_t *right = expression->right;
4903 type_t *orig_type_left = left->base.type;
4904 type_t *orig_type_right = right->base.type;
4906 type_t *type_left = skip_typeref(orig_type_left);
4907 type_t *type_right = skip_typeref(orig_type_right);
4909 /* TODO non-arithmetic types */
4910 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4911 if (warning.sign_compare &&
4912 (expression->base.kind != EXPR_BINARY_EQUAL &&
4913 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4914 (is_type_signed(type_left) != is_type_signed(type_right))) {
4915 warningf(expression->base.source_position,
4916 "comparison between signed and unsigned");
4918 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4919 expression->left = create_implicit_cast(left, arithmetic_type);
4920 expression->right = create_implicit_cast(right, arithmetic_type);
4921 expression->base.type = arithmetic_type;
4922 if (warning.float_equal &&
4923 (expression->base.kind == EXPR_BINARY_EQUAL ||
4924 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4925 is_type_float(arithmetic_type)) {
4926 warningf(expression->base.source_position,
4927 "comparing floating point with == or != is unsafe");
4929 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4930 /* TODO check compatibility */
4931 } else if (is_type_pointer(type_left)) {
4932 expression->right = create_implicit_cast(right, type_left);
4933 } else if (is_type_pointer(type_right)) {
4934 expression->left = create_implicit_cast(left, type_right);
4935 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4936 type_error_incompatible("invalid operands in comparison",
4937 expression->base.source_position,
4938 type_left, type_right);
4940 expression->base.type = type_int;
4943 static void semantic_arithmetic_assign(binary_expression_t *expression)
4945 expression_t *left = expression->left;
4946 expression_t *right = expression->right;
4947 type_t *orig_type_left = left->base.type;
4948 type_t *orig_type_right = right->base.type;
4950 type_t *type_left = skip_typeref(orig_type_left);
4951 type_t *type_right = skip_typeref(orig_type_right);
4953 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4954 /* TODO: improve error message */
4955 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4956 errorf(HERE, "operation needs arithmetic types");
4961 /* combined instructions are tricky. We can't create an implicit cast on
4962 * the left side, because we need the uncasted form for the store.
4963 * The ast2firm pass has to know that left_type must be right_type
4964 * for the arithmetic operation and create a cast by itself */
4965 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4966 expression->right = create_implicit_cast(right, arithmetic_type);
4967 expression->base.type = type_left;
4970 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4972 expression_t *const left = expression->left;
4973 expression_t *const right = expression->right;
4974 type_t *const orig_type_left = left->base.type;
4975 type_t *const orig_type_right = right->base.type;
4976 type_t *const type_left = skip_typeref(orig_type_left);
4977 type_t *const type_right = skip_typeref(orig_type_right);
4979 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4980 /* combined instructions are tricky. We can't create an implicit cast on
4981 * the left side, because we need the uncasted form for the store.
4982 * The ast2firm pass has to know that left_type must be right_type
4983 * for the arithmetic operation and create a cast by itself */
4984 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4985 expression->right = create_implicit_cast(right, arithmetic_type);
4986 expression->base.type = type_left;
4987 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4988 expression->base.type = type_left;
4989 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4990 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4995 * Check the semantic restrictions of a logical expression.
4997 static void semantic_logical_op(binary_expression_t *expression)
4999 expression_t *const left = expression->left;
5000 expression_t *const right = expression->right;
5001 type_t *const orig_type_left = left->base.type;
5002 type_t *const orig_type_right = right->base.type;
5003 type_t *const type_left = skip_typeref(orig_type_left);
5004 type_t *const type_right = skip_typeref(orig_type_right);
5006 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5007 /* TODO: improve error message */
5008 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5009 errorf(HERE, "operation needs scalar types");
5014 expression->base.type = type_int;
5018 * Checks if a compound type has constant fields.
5020 static bool has_const_fields(const compound_type_t *type)
5022 const scope_t *scope = &type->declaration->scope;
5023 const declaration_t *declaration = scope->declarations;
5025 for (; declaration != NULL; declaration = declaration->next) {
5026 if (declaration->namespc != NAMESPACE_NORMAL)
5029 const type_t *decl_type = skip_typeref(declaration->type);
5030 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5038 * Check the semantic restrictions of a binary assign expression.
5040 static void semantic_binexpr_assign(binary_expression_t *expression)
5042 expression_t *left = expression->left;
5043 type_t *orig_type_left = left->base.type;
5045 type_t *type_left = revert_automatic_type_conversion(left);
5046 type_left = skip_typeref(orig_type_left);
5048 /* must be a modifiable lvalue */
5049 if (is_type_array(type_left)) {
5050 errorf(HERE, "cannot assign to arrays ('%E')", left);
5053 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5054 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5058 if(is_type_incomplete(type_left)) {
5060 "left-hand side of assignment '%E' has incomplete type '%T'",
5061 left, orig_type_left);
5064 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5065 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5066 left, orig_type_left);
5070 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5072 if (res_type == NULL) {
5073 errorf(expression->base.source_position,
5074 "cannot assign to '%T' from '%T'",
5075 orig_type_left, expression->right->base.type);
5077 expression->right = create_implicit_cast(expression->right, res_type);
5080 expression->base.type = orig_type_left;
5083 static bool expression_has_effect(const expression_t *const expr)
5085 switch (expr->kind) {
5086 case EXPR_UNKNOWN: break;
5087 case EXPR_INVALID: break;
5088 case EXPR_REFERENCE: return false;
5089 case EXPR_CONST: return false;
5090 case EXPR_CHAR_CONST: return false;
5091 case EXPR_STRING_LITERAL: return false;
5092 case EXPR_WIDE_STRING_LITERAL: return false;
5094 const call_expression_t *const call = &expr->call;
5095 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5098 switch (call->function->builtin_symbol.symbol->ID) {
5099 case T___builtin_va_end: return true;
5100 default: return false;
5103 case EXPR_CONDITIONAL: {
5104 const conditional_expression_t *const cond = &expr->conditional;
5106 expression_has_effect(cond->true_expression) &&
5107 expression_has_effect(cond->false_expression);
5109 case EXPR_SELECT: return false;
5110 case EXPR_ARRAY_ACCESS: return false;
5111 case EXPR_SIZEOF: return false;
5112 case EXPR_CLASSIFY_TYPE: return false;
5113 case EXPR_ALIGNOF: return false;
5115 case EXPR_FUNCTION: return false;
5116 case EXPR_PRETTY_FUNCTION: return false;
5117 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5118 case EXPR_BUILTIN_CONSTANT_P: return false;
5119 case EXPR_BUILTIN_PREFETCH: return true;
5120 case EXPR_OFFSETOF: return false;
5121 case EXPR_VA_START: return true;
5122 case EXPR_VA_ARG: return true;
5123 case EXPR_STATEMENT: return true; // TODO
5124 case EXPR_COMPOUND_LITERAL: return false;
5126 case EXPR_UNARY_NEGATE: return false;
5127 case EXPR_UNARY_PLUS: return false;
5128 case EXPR_UNARY_BITWISE_NEGATE: return false;
5129 case EXPR_UNARY_NOT: return false;
5130 case EXPR_UNARY_DEREFERENCE: return false;
5131 case EXPR_UNARY_TAKE_ADDRESS: return false;
5132 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5133 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5134 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5135 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5136 case EXPR_UNARY_CAST: {
5137 type_t *type = skip_typeref(expr->base.type);
5138 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5140 case EXPR_UNARY_CAST_IMPLICIT: return true;
5141 case EXPR_UNARY_ASSUME: return true;
5142 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5144 case EXPR_BINARY_ADD: return false;
5145 case EXPR_BINARY_SUB: return false;
5146 case EXPR_BINARY_MUL: return false;
5147 case EXPR_BINARY_DIV: return false;
5148 case EXPR_BINARY_MOD: return false;
5149 case EXPR_BINARY_EQUAL: return false;
5150 case EXPR_BINARY_NOTEQUAL: return false;
5151 case EXPR_BINARY_LESS: return false;
5152 case EXPR_BINARY_LESSEQUAL: return false;
5153 case EXPR_BINARY_GREATER: return false;
5154 case EXPR_BINARY_GREATEREQUAL: return false;
5155 case EXPR_BINARY_BITWISE_AND: return false;
5156 case EXPR_BINARY_BITWISE_OR: return false;
5157 case EXPR_BINARY_BITWISE_XOR: return false;
5158 case EXPR_BINARY_SHIFTLEFT: return false;
5159 case EXPR_BINARY_SHIFTRIGHT: return false;
5160 case EXPR_BINARY_ASSIGN: return true;
5161 case EXPR_BINARY_MUL_ASSIGN: return true;
5162 case EXPR_BINARY_DIV_ASSIGN: return true;
5163 case EXPR_BINARY_MOD_ASSIGN: return true;
5164 case EXPR_BINARY_ADD_ASSIGN: return true;
5165 case EXPR_BINARY_SUB_ASSIGN: return true;
5166 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5167 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5168 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5169 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5170 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5171 case EXPR_BINARY_LOGICAL_AND:
5172 case EXPR_BINARY_LOGICAL_OR:
5173 case EXPR_BINARY_COMMA:
5174 return expression_has_effect(expr->binary.right);
5176 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5177 case EXPR_BINARY_ISGREATER: return false;
5178 case EXPR_BINARY_ISGREATEREQUAL: return false;
5179 case EXPR_BINARY_ISLESS: return false;
5180 case EXPR_BINARY_ISLESSEQUAL: return false;
5181 case EXPR_BINARY_ISLESSGREATER: return false;
5182 case EXPR_BINARY_ISUNORDERED: return false;
5185 panic("unexpected statement");
5188 static void semantic_comma(binary_expression_t *expression)
5190 if (warning.unused_value) {
5191 const expression_t *const left = expression->left;
5192 if (!expression_has_effect(left)) {
5193 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5196 expression->base.type = expression->right->base.type;
5199 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5200 static expression_t *parse_##binexpression_type(unsigned precedence, \
5201 expression_t *left) \
5204 source_position_t pos = HERE; \
5206 expression_t *right = parse_sub_expression(precedence + lr); \
5208 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5209 binexpr->base.source_position = pos; \
5210 binexpr->binary.left = left; \
5211 binexpr->binary.right = right; \
5212 sfunc(&binexpr->binary); \
5217 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5218 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5219 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5220 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5221 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5222 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5223 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5224 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5225 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5227 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5228 semantic_comparison, 1)
5229 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5230 semantic_comparison, 1)
5231 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5232 semantic_comparison, 1)
5233 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5234 semantic_comparison, 1)
5236 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5237 semantic_binexpr_arithmetic, 1)
5238 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5239 semantic_binexpr_arithmetic, 1)
5240 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5241 semantic_binexpr_arithmetic, 1)
5242 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5243 semantic_logical_op, 1)
5244 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5245 semantic_logical_op, 1)
5246 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5247 semantic_shift_op, 1)
5248 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5249 semantic_shift_op, 1)
5250 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5251 semantic_arithmetic_addsubb_assign, 0)
5252 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5253 semantic_arithmetic_addsubb_assign, 0)
5254 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5255 semantic_arithmetic_assign, 0)
5256 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5257 semantic_arithmetic_assign, 0)
5258 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5259 semantic_arithmetic_assign, 0)
5260 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5261 semantic_arithmetic_assign, 0)
5262 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5263 semantic_arithmetic_assign, 0)
5264 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5265 semantic_arithmetic_assign, 0)
5266 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5267 semantic_arithmetic_assign, 0)
5268 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5269 semantic_arithmetic_assign, 0)
5271 static expression_t *parse_sub_expression(unsigned precedence)
5273 if(token.type < 0) {
5274 return expected_expression_error();
5277 expression_parser_function_t *parser
5278 = &expression_parsers[token.type];
5279 source_position_t source_position = token.source_position;
5282 if(parser->parser != NULL) {
5283 left = parser->parser(parser->precedence);
5285 left = parse_primary_expression();
5287 assert(left != NULL);
5288 left->base.source_position = source_position;
5291 if(token.type < 0) {
5292 return expected_expression_error();
5295 parser = &expression_parsers[token.type];
5296 if(parser->infix_parser == NULL)
5298 if(parser->infix_precedence < precedence)
5301 left = parser->infix_parser(parser->infix_precedence, left);
5303 assert(left != NULL);
5304 assert(left->kind != EXPR_UNKNOWN);
5305 left->base.source_position = source_position;
5312 * Parse an expression.
5314 static expression_t *parse_expression(void)
5316 return parse_sub_expression(1);
5320 * Register a parser for a prefix-like operator with given precedence.
5322 * @param parser the parser function
5323 * @param token_type the token type of the prefix token
5324 * @param precedence the precedence of the operator
5326 static void register_expression_parser(parse_expression_function parser,
5327 int token_type, unsigned precedence)
5329 expression_parser_function_t *entry = &expression_parsers[token_type];
5331 if(entry->parser != NULL) {
5332 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5333 panic("trying to register multiple expression parsers for a token");
5335 entry->parser = parser;
5336 entry->precedence = precedence;
5340 * Register a parser for an infix operator with given precedence.
5342 * @param parser the parser function
5343 * @param token_type the token type of the infix operator
5344 * @param precedence the precedence of the operator
5346 static void register_infix_parser(parse_expression_infix_function parser,
5347 int token_type, unsigned precedence)
5349 expression_parser_function_t *entry = &expression_parsers[token_type];
5351 if(entry->infix_parser != NULL) {
5352 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5353 panic("trying to register multiple infix expression parsers for a "
5356 entry->infix_parser = parser;
5357 entry->infix_precedence = precedence;
5361 * Initialize the expression parsers.
5363 static void init_expression_parsers(void)
5365 memset(&expression_parsers, 0, sizeof(expression_parsers));
5367 register_infix_parser(parse_array_expression, '[', 30);
5368 register_infix_parser(parse_call_expression, '(', 30);
5369 register_infix_parser(parse_select_expression, '.', 30);
5370 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5371 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5373 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5376 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5377 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5378 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5379 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5380 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5381 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5382 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5383 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5384 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5385 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5386 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5387 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5388 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5389 T_EXCLAMATIONMARKEQUAL, 13);
5390 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5391 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5392 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5393 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5394 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5395 register_infix_parser(parse_conditional_expression, '?', 7);
5396 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5397 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5398 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5399 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5400 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5401 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5402 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5403 T_LESSLESSEQUAL, 2);
5404 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5405 T_GREATERGREATEREQUAL, 2);
5406 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5408 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5410 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5413 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5415 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5416 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5417 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5418 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5419 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5420 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5421 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5423 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5425 register_expression_parser(parse_sizeof, T_sizeof, 25);
5426 register_expression_parser(parse_alignof, T___alignof__, 25);
5427 register_expression_parser(parse_extension, T___extension__, 25);
5428 register_expression_parser(parse_builtin_classify_type,
5429 T___builtin_classify_type, 25);
5433 * Parse a asm statement constraints specification.
5435 static asm_constraint_t *parse_asm_constraints(void)
5437 asm_constraint_t *result = NULL;
5438 asm_constraint_t *last = NULL;
5440 while(token.type == T_STRING_LITERAL || token.type == '[') {
5441 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5442 memset(constraint, 0, sizeof(constraint[0]));
5444 if(token.type == '[') {
5446 if(token.type != T_IDENTIFIER) {
5447 parse_error_expected("while parsing asm constraint",
5451 constraint->symbol = token.v.symbol;
5456 constraint->constraints = parse_string_literals();
5458 constraint->expression = parse_expression();
5462 last->next = constraint;
5464 result = constraint;
5468 if(token.type != ',')
5477 * Parse a asm statement clobber specification.
5479 static asm_clobber_t *parse_asm_clobbers(void)
5481 asm_clobber_t *result = NULL;
5482 asm_clobber_t *last = NULL;
5484 while(token.type == T_STRING_LITERAL) {
5485 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5486 clobber->clobber = parse_string_literals();
5489 last->next = clobber;
5495 if(token.type != ',')
5504 * Parse an asm statement.
5506 static statement_t *parse_asm_statement(void)
5510 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5511 statement->base.source_position = token.source_position;
5513 asm_statement_t *asm_statement = &statement->asms;
5515 if(token.type == T_volatile) {
5517 asm_statement->is_volatile = true;
5521 asm_statement->asm_text = parse_string_literals();
5523 if(token.type != ':')
5527 asm_statement->inputs = parse_asm_constraints();
5528 if(token.type != ':')
5532 asm_statement->outputs = parse_asm_constraints();
5533 if(token.type != ':')
5537 asm_statement->clobbers = parse_asm_clobbers();
5546 * Parse a case statement.
5548 static statement_t *parse_case_statement(void)
5552 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5554 statement->base.source_position = token.source_position;
5555 statement->case_label.expression = parse_expression();
5557 if (c_mode & _GNUC) {
5558 if (token.type == T_DOTDOTDOT) {
5560 statement->case_label.end_range = parse_expression();
5566 if (! is_constant_expression(statement->case_label.expression)) {
5567 errorf(statement->base.source_position,
5568 "case label does not reduce to an integer constant");
5570 /* TODO: check if the case label is already known */
5571 if (current_switch != NULL) {
5572 /* link all cases into the switch statement */
5573 if (current_switch->last_case == NULL) {
5574 current_switch->first_case =
5575 current_switch->last_case = &statement->case_label;
5577 current_switch->last_case->next = &statement->case_label;
5580 errorf(statement->base.source_position,
5581 "case label not within a switch statement");
5584 statement->case_label.statement = parse_statement();
5590 * Finds an existing default label of a switch statement.
5592 static case_label_statement_t *
5593 find_default_label(const switch_statement_t *statement)
5595 case_label_statement_t *label = statement->first_case;
5596 for ( ; label != NULL; label = label->next) {
5597 if (label->expression == NULL)
5604 * Parse a default statement.
5606 static statement_t *parse_default_statement(void)
5610 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5612 statement->base.source_position = token.source_position;
5615 if (current_switch != NULL) {
5616 const case_label_statement_t *def_label = find_default_label(current_switch);
5617 if (def_label != NULL) {
5618 errorf(HERE, "multiple default labels in one switch");
5619 errorf(def_label->base.source_position,
5620 "this is the first default label");
5622 /* link all cases into the switch statement */
5623 if (current_switch->last_case == NULL) {
5624 current_switch->first_case =
5625 current_switch->last_case = &statement->case_label;
5627 current_switch->last_case->next = &statement->case_label;
5631 errorf(statement->base.source_position,
5632 "'default' label not within a switch statement");
5634 statement->case_label.statement = parse_statement();
5640 * Return the declaration for a given label symbol or create a new one.
5642 static declaration_t *get_label(symbol_t *symbol)
5644 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5645 assert(current_function != NULL);
5646 /* if we found a label in the same function, then we already created the
5648 if(candidate != NULL
5649 && candidate->parent_scope == ¤t_function->scope) {
5653 /* otherwise we need to create a new one */
5654 declaration_t *const declaration = allocate_declaration_zero();
5655 declaration->namespc = NAMESPACE_LABEL;
5656 declaration->symbol = symbol;
5658 label_push(declaration);
5664 * Parse a label statement.
5666 static statement_t *parse_label_statement(void)
5668 assert(token.type == T_IDENTIFIER);
5669 symbol_t *symbol = token.v.symbol;
5672 declaration_t *label = get_label(symbol);
5674 /* if source position is already set then the label is defined twice,
5675 * otherwise it was just mentioned in a goto so far */
5676 if(label->source_position.input_name != NULL) {
5677 errorf(HERE, "duplicate label '%Y'", symbol);
5678 errorf(label->source_position, "previous definition of '%Y' was here",
5681 label->source_position = token.source_position;
5684 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5686 statement->base.source_position = token.source_position;
5687 statement->label.label = label;
5691 if(token.type == '}') {
5692 /* TODO only warn? */
5693 errorf(HERE, "label at end of compound statement");
5696 if (token.type == ';') {
5697 /* eat an empty statement here, to avoid the warning about an empty
5698 * after a label. label:; is commonly used to have a label before
5702 statement->label.statement = parse_statement();
5706 /* remember the labels's in a list for later checking */
5707 if (label_last == NULL) {
5708 label_first = &statement->label;
5710 label_last->next = &statement->label;
5712 label_last = &statement->label;
5718 * Parse an if statement.
5720 static statement_t *parse_if(void)
5724 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5725 statement->base.source_position = token.source_position;
5728 statement->ifs.condition = parse_expression();
5731 statement->ifs.true_statement = parse_statement();
5732 if(token.type == T_else) {
5734 statement->ifs.false_statement = parse_statement();
5741 * Parse a switch statement.
5743 static statement_t *parse_switch(void)
5747 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5748 statement->base.source_position = token.source_position;
5751 expression_t *const expr = parse_expression();
5752 type_t * type = skip_typeref(expr->base.type);
5753 if (is_type_integer(type)) {
5754 type = promote_integer(type);
5755 } else if (is_type_valid(type)) {
5756 errorf(expr->base.source_position,
5757 "switch quantity is not an integer, but '%T'", type);
5758 type = type_error_type;
5760 statement->switchs.expression = create_implicit_cast(expr, type);
5763 switch_statement_t *rem = current_switch;
5764 current_switch = &statement->switchs;
5765 statement->switchs.body = parse_statement();
5766 current_switch = rem;
5768 if (warning.switch_default
5769 && find_default_label(&statement->switchs) == NULL) {
5770 warningf(statement->base.source_position, "switch has no default case");
5776 static statement_t *parse_loop_body(statement_t *const loop)
5778 statement_t *const rem = current_loop;
5779 current_loop = loop;
5781 statement_t *const body = parse_statement();
5788 * Parse a while statement.
5790 static statement_t *parse_while(void)
5794 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5795 statement->base.source_position = token.source_position;
5798 statement->whiles.condition = parse_expression();
5801 statement->whiles.body = parse_loop_body(statement);
5807 * Parse a do statement.
5809 static statement_t *parse_do(void)
5813 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5815 statement->base.source_position = token.source_position;
5817 statement->do_while.body = parse_loop_body(statement);
5821 statement->do_while.condition = parse_expression();
5829 * Parse a for statement.
5831 static statement_t *parse_for(void)
5835 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5836 statement->base.source_position = token.source_position;
5840 int top = environment_top();
5841 scope_t *last_scope = scope;
5842 set_scope(&statement->fors.scope);
5844 if(token.type != ';') {
5845 if(is_declaration_specifier(&token, false)) {
5846 parse_declaration(record_declaration);
5848 expression_t *const init = parse_expression();
5849 statement->fors.initialisation = init;
5850 if (warning.unused_value && !expression_has_effect(init)) {
5851 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5859 if(token.type != ';') {
5860 statement->fors.condition = parse_expression();
5863 if(token.type != ')') {
5864 expression_t *const step = parse_expression();
5865 statement->fors.step = step;
5866 if (warning.unused_value && !expression_has_effect(step)) {
5867 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5871 statement->fors.body = parse_loop_body(statement);
5873 assert(scope == &statement->fors.scope);
5874 set_scope(last_scope);
5875 environment_pop_to(top);
5881 * Parse a goto statement.
5883 static statement_t *parse_goto(void)
5887 if(token.type != T_IDENTIFIER) {
5888 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5892 symbol_t *symbol = token.v.symbol;
5895 declaration_t *label = get_label(symbol);
5897 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5898 statement->base.source_position = token.source_position;
5900 statement->gotos.label = label;
5902 /* remember the goto's in a list for later checking */
5903 if (goto_last == NULL) {
5904 goto_first = &statement->gotos;
5906 goto_last->next = &statement->gotos;
5908 goto_last = &statement->gotos;
5916 * Parse a continue statement.
5918 static statement_t *parse_continue(void)
5920 statement_t *statement;
5921 if (current_loop == NULL) {
5922 errorf(HERE, "continue statement not within loop");
5925 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5927 statement->base.source_position = token.source_position;
5937 * Parse a break statement.
5939 static statement_t *parse_break(void)
5941 statement_t *statement;
5942 if (current_switch == NULL && current_loop == NULL) {
5943 errorf(HERE, "break statement not within loop or switch");
5946 statement = allocate_statement_zero(STATEMENT_BREAK);
5948 statement->base.source_position = token.source_position;
5958 * Check if a given declaration represents a local variable.
5960 static bool is_local_var_declaration(const declaration_t *declaration) {
5961 switch ((storage_class_tag_t) declaration->storage_class) {
5962 case STORAGE_CLASS_AUTO:
5963 case STORAGE_CLASS_REGISTER: {
5964 const type_t *type = skip_typeref(declaration->type);
5965 if(is_type_function(type)) {
5977 * Check if a given declaration represents a variable.
5979 static bool is_var_declaration(const declaration_t *declaration) {
5980 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
5983 const type_t *type = skip_typeref(declaration->type);
5984 return !is_type_function(type);
5988 * Check if a given expression represents a local variable.
5990 static bool is_local_variable(const expression_t *expression)
5992 if (expression->base.kind != EXPR_REFERENCE) {
5995 const declaration_t *declaration = expression->reference.declaration;
5996 return is_local_var_declaration(declaration);
6000 * Check if a given expression represents a local variable and
6001 * return its declaration then, else return NULL.
6003 declaration_t *expr_is_variable(const expression_t *expression)
6005 if (expression->base.kind != EXPR_REFERENCE) {
6008 declaration_t *declaration = expression->reference.declaration;
6009 if (is_var_declaration(declaration))
6015 * Parse a return statement.
6017 static statement_t *parse_return(void)
6021 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6022 statement->base.source_position = token.source_position;
6024 expression_t *return_value = NULL;
6025 if(token.type != ';') {
6026 return_value = parse_expression();
6030 const type_t *const func_type = current_function->type;
6031 assert(is_type_function(func_type));
6032 type_t *const return_type = skip_typeref(func_type->function.return_type);
6034 if(return_value != NULL) {
6035 type_t *return_value_type = skip_typeref(return_value->base.type);
6037 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6038 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6039 warningf(statement->base.source_position,
6040 "'return' with a value, in function returning void");
6041 return_value = NULL;
6043 type_t *const res_type = semantic_assign(return_type,
6044 return_value, "'return'");
6045 if (res_type == NULL) {
6046 errorf(statement->base.source_position,
6047 "cannot return something of type '%T' in function returning '%T'",
6048 return_value->base.type, return_type);
6050 return_value = create_implicit_cast(return_value, res_type);
6053 /* check for returning address of a local var */
6054 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6055 const expression_t *expression = return_value->unary.value;
6056 if (is_local_variable(expression)) {
6057 warningf(statement->base.source_position,
6058 "function returns address of local variable");
6062 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6063 warningf(statement->base.source_position,
6064 "'return' without value, in function returning non-void");
6067 statement->returns.value = return_value;
6073 * Parse a declaration statement.
6075 static statement_t *parse_declaration_statement(void)
6077 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6079 statement->base.source_position = token.source_position;
6081 declaration_t *before = last_declaration;
6082 parse_declaration(record_declaration);
6084 if(before == NULL) {
6085 statement->declaration.declarations_begin = scope->declarations;
6087 statement->declaration.declarations_begin = before->next;
6089 statement->declaration.declarations_end = last_declaration;
6095 * Parse an expression statement, ie. expr ';'.
6097 static statement_t *parse_expression_statement(void)
6099 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6101 statement->base.source_position = token.source_position;
6102 expression_t *const expr = parse_expression();
6103 statement->expression.expression = expr;
6105 if (warning.unused_value && !expression_has_effect(expr)) {
6106 warningf(expr->base.source_position, "statement has no effect");
6115 * Parse a statement.
6117 static statement_t *parse_statement(void)
6119 statement_t *statement = NULL;
6121 /* declaration or statement */
6122 switch(token.type) {
6124 statement = parse_asm_statement();
6128 statement = parse_case_statement();
6132 statement = parse_default_statement();
6136 statement = parse_compound_statement();
6140 statement = parse_if();
6144 statement = parse_switch();
6148 statement = parse_while();
6152 statement = parse_do();
6156 statement = parse_for();
6160 statement = parse_goto();
6164 statement = parse_continue();
6168 statement = parse_break();
6172 statement = parse_return();
6176 if (warning.empty_statement) {
6177 warningf(HERE, "statement is empty");
6184 if(look_ahead(1)->type == ':') {
6185 statement = parse_label_statement();
6189 if(is_typedef_symbol(token.v.symbol)) {
6190 statement = parse_declaration_statement();
6194 statement = parse_expression_statement();
6197 case T___extension__:
6198 /* this can be a prefix to a declaration or an expression statement */
6199 /* we simply eat it now and parse the rest with tail recursion */
6202 } while(token.type == T___extension__);
6203 statement = parse_statement();
6207 statement = parse_declaration_statement();
6211 statement = parse_expression_statement();
6215 assert(statement == NULL
6216 || statement->base.source_position.input_name != NULL);
6222 * Parse a compound statement.
6224 static statement_t *parse_compound_statement(void)
6226 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6228 statement->base.source_position = token.source_position;
6232 int top = environment_top();
6233 scope_t *last_scope = scope;
6234 set_scope(&statement->compound.scope);
6236 statement_t *last_statement = NULL;
6238 while(token.type != '}' && token.type != T_EOF) {
6239 statement_t *sub_statement = parse_statement();
6240 if(sub_statement == NULL)
6243 if(last_statement != NULL) {
6244 last_statement->base.next = sub_statement;
6246 statement->compound.statements = sub_statement;
6249 while(sub_statement->base.next != NULL)
6250 sub_statement = sub_statement->base.next;
6252 last_statement = sub_statement;
6255 if(token.type == '}') {
6258 errorf(statement->base.source_position,
6259 "end of file while looking for closing '}'");
6262 assert(scope == &statement->compound.scope);
6263 set_scope(last_scope);
6264 environment_pop_to(top);
6270 * Initialize builtin types.
6272 static void initialize_builtin_types(void)
6274 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6275 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6276 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6277 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6278 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6279 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6280 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6281 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6283 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6284 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6285 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6286 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6290 * Check for unused global static functions and variables
6292 static void check_unused_globals(void)
6294 if (!warning.unused_function && !warning.unused_variable)
6297 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6298 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6301 type_t *const type = decl->type;
6303 if (is_type_function(skip_typeref(type))) {
6304 if (!warning.unused_function || decl->is_inline)
6307 s = (decl->init.statement != NULL ? "defined" : "declared");
6309 if (!warning.unused_variable)
6315 warningf(decl->source_position, "'%#T' %s but not used",
6316 type, decl->symbol, s);
6321 * Parse a translation unit.
6323 static translation_unit_t *parse_translation_unit(void)
6325 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6327 assert(global_scope == NULL);
6328 global_scope = &unit->scope;
6330 assert(scope == NULL);
6331 set_scope(&unit->scope);
6333 initialize_builtin_types();
6335 while(token.type != T_EOF) {
6336 if (token.type == ';') {
6337 /* TODO error in strict mode */
6338 warningf(HERE, "stray ';' outside of function");
6341 parse_external_declaration();
6345 assert(scope == &unit->scope);
6347 last_declaration = NULL;
6349 assert(global_scope == &unit->scope);
6350 check_unused_globals();
6351 global_scope = NULL;
6359 * @return the translation unit or NULL if errors occurred.
6361 translation_unit_t *parse(void)
6363 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6364 label_stack = NEW_ARR_F(stack_entry_t, 0);
6365 diagnostic_count = 0;
6369 type_set_output(stderr);
6370 ast_set_output(stderr);
6372 lookahead_bufpos = 0;
6373 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6376 translation_unit_t *unit = parse_translation_unit();
6378 DEL_ARR_F(environment_stack);
6379 DEL_ARR_F(label_stack);
6388 * Initialize the parser.
6390 void init_parser(void)
6392 init_expression_parsers();
6393 obstack_init(&temp_obst);
6395 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6396 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6400 * Terminate the parser.
6402 void exit_parser(void)
6404 obstack_free(&temp_obst, NULL);