2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct gnu_attribute_t gnu_attribute_t;
52 struct gnu_attribute_t {
53 gnu_attribute_kind_t kind;
54 gnu_attribute_t *next;
63 typedef struct declaration_specifiers_t declaration_specifiers_t;
64 struct declaration_specifiers_t {
65 source_position_t source_position;
66 unsigned char declared_storage_class;
67 unsigned char alignment; /**< Alignment, 0 if not set. */
68 unsigned int is_inline : 1;
69 unsigned int deprecated : 1;
70 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
71 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
72 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
73 symbol_t *get_property_sym; /**< the name of the get property if set. */
74 symbol_t *put_property_sym; /**< the name of the put property if set. */
79 * An environment for parsing initializers (and compound literals).
81 typedef struct parse_initializer_env_t {
82 type_t *type; /**< the type of the initializer. In case of an
83 array type with unspecified size this gets
84 adjusted to the actual size. */
85 declaration_t *declaration; /**< the declaration that is initialized if any */
86 bool must_be_constant;
87 } parse_initializer_env_t;
89 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
92 static token_t lookahead_buffer[MAX_LOOKAHEAD];
93 static int lookahead_bufpos;
94 static stack_entry_t *environment_stack = NULL;
95 static stack_entry_t *label_stack = NULL;
96 static scope_t *global_scope = NULL;
97 static scope_t *scope = NULL;
98 static declaration_t *last_declaration = NULL;
99 static declaration_t *current_function = NULL;
100 static switch_statement_t *current_switch = NULL;
101 static statement_t *current_loop = NULL;
102 static ms_try_statement_t *current_try = NULL;
103 static goto_statement_t *goto_first = NULL;
104 static goto_statement_t *goto_last = NULL;
105 static label_statement_t *label_first = NULL;
106 static label_statement_t *label_last = NULL;
107 static struct obstack temp_obst;
109 static source_position_t null_position = { NULL, 0 };
111 /* symbols for Microsoft extended-decl-modifier */
112 static const symbol_t *sym_align = NULL;
113 static const symbol_t *sym_allocate = NULL;
114 static const symbol_t *sym_dllimport = NULL;
115 static const symbol_t *sym_dllexport = NULL;
116 static const symbol_t *sym_naked = NULL;
117 static const symbol_t *sym_noinline = NULL;
118 static const symbol_t *sym_noreturn = NULL;
119 static const symbol_t *sym_nothrow = NULL;
120 static const symbol_t *sym_novtable = NULL;
121 static const symbol_t *sym_property = NULL;
122 static const symbol_t *sym_get = NULL;
123 static const symbol_t *sym_put = NULL;
124 static const symbol_t *sym_selectany = NULL;
125 static const symbol_t *sym_thread = NULL;
126 static const symbol_t *sym_uuid = NULL;
127 static const symbol_t *sym_deprecated = NULL;
128 static const symbol_t *sym_restrict = NULL;
129 static const symbol_t *sym_noalias = NULL;
131 /** The token anchor set */
132 static unsigned char token_anchor_set[T_LAST_TOKEN];
134 /** The current source position. */
135 #define HERE &token.source_position
137 static type_t *type_valist;
139 static statement_t *parse_compound_statement(bool inside_expression_statement);
140 static statement_t *parse_statement(void);
142 static expression_t *parse_sub_expression(unsigned precedence);
143 static expression_t *parse_expression(void);
144 static type_t *parse_typename(void);
146 static void parse_compound_type_entries(declaration_t *compound_declaration);
147 static declaration_t *parse_declarator(
148 const declaration_specifiers_t *specifiers, bool may_be_abstract);
149 static declaration_t *record_declaration(declaration_t *declaration);
151 static void semantic_comparison(binary_expression_t *expression);
153 #define STORAGE_CLASSES \
160 #define TYPE_QUALIFIERS \
167 #ifdef PROVIDE_COMPLEX
168 #define COMPLEX_SPECIFIERS \
170 #define IMAGINARY_SPECIFIERS \
173 #define COMPLEX_SPECIFIERS
174 #define IMAGINARY_SPECIFIERS
177 #define TYPE_SPECIFIERS \
192 case T___builtin_va_list: \
197 #define DECLARATION_START \
202 #define TYPENAME_START \
207 * Allocate an AST node with given size and
208 * initialize all fields with zero.
210 static void *allocate_ast_zero(size_t size)
212 void *res = allocate_ast(size);
213 memset(res, 0, size);
217 static declaration_t *allocate_declaration_zero(void)
219 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
220 declaration->type = type_error_type;
221 declaration->alignment = 0;
226 * Returns the size of a statement node.
228 * @param kind the statement kind
230 static size_t get_statement_struct_size(statement_kind_t kind)
232 static const size_t sizes[] = {
233 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
234 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
235 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
236 [STATEMENT_RETURN] = sizeof(return_statement_t),
237 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
238 [STATEMENT_IF] = sizeof(if_statement_t),
239 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
240 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
241 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
242 [STATEMENT_BREAK] = sizeof(statement_base_t),
243 [STATEMENT_GOTO] = sizeof(goto_statement_t),
244 [STATEMENT_LABEL] = sizeof(label_statement_t),
245 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
246 [STATEMENT_WHILE] = sizeof(while_statement_t),
247 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
248 [STATEMENT_FOR] = sizeof(for_statement_t),
249 [STATEMENT_ASM] = sizeof(asm_statement_t),
250 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
251 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
253 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
254 assert(sizes[kind] != 0);
259 * Returns the size of an expression node.
261 * @param kind the expression kind
263 static size_t get_expression_struct_size(expression_kind_t kind)
265 static const size_t sizes[] = {
266 [EXPR_INVALID] = sizeof(expression_base_t),
267 [EXPR_REFERENCE] = sizeof(reference_expression_t),
268 [EXPR_CONST] = sizeof(const_expression_t),
269 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
270 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
271 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
272 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
273 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
274 [EXPR_CALL] = sizeof(call_expression_t),
275 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
276 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
277 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
278 [EXPR_SELECT] = sizeof(select_expression_t),
279 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
280 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
281 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
282 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
283 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
284 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
285 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
286 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
287 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
288 [EXPR_VA_START] = sizeof(va_start_expression_t),
289 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
290 [EXPR_STATEMENT] = sizeof(statement_expression_t),
292 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
293 return sizes[EXPR_UNARY_FIRST];
295 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
296 return sizes[EXPR_BINARY_FIRST];
298 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
299 assert(sizes[kind] != 0);
304 * Allocate a statement node of given kind and initialize all
307 static statement_t *allocate_statement_zero(statement_kind_t kind)
309 size_t size = get_statement_struct_size(kind);
310 statement_t *res = allocate_ast_zero(size);
312 res->base.kind = kind;
317 * Allocate an expression node of given kind and initialize all
320 static expression_t *allocate_expression_zero(expression_kind_t kind)
322 size_t size = get_expression_struct_size(kind);
323 expression_t *res = allocate_ast_zero(size);
325 res->base.kind = kind;
326 res->base.type = type_error_type;
331 * Creates a new invalid expression.
333 static expression_t *create_invalid_expression(void)
335 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
336 expression->base.source_position = token.source_position;
341 * Creates a new invalid statement.
343 static statement_t *create_invalid_statement(void)
345 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
346 statement->base.source_position = token.source_position;
351 * Allocate a new empty statement.
353 static statement_t *create_empty_statement(void)
355 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
356 statement->base.source_position = token.source_position;
361 * Returns the size of a type node.
363 * @param kind the type kind
365 static size_t get_type_struct_size(type_kind_t kind)
367 static const size_t sizes[] = {
368 [TYPE_ATOMIC] = sizeof(atomic_type_t),
369 [TYPE_COMPLEX] = sizeof(complex_type_t),
370 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
371 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
372 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
373 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
374 [TYPE_ENUM] = sizeof(enum_type_t),
375 [TYPE_FUNCTION] = sizeof(function_type_t),
376 [TYPE_POINTER] = sizeof(pointer_type_t),
377 [TYPE_ARRAY] = sizeof(array_type_t),
378 [TYPE_BUILTIN] = sizeof(builtin_type_t),
379 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
380 [TYPE_TYPEOF] = sizeof(typeof_type_t),
382 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
383 assert(kind <= TYPE_TYPEOF);
384 assert(sizes[kind] != 0);
389 * Allocate a type node of given kind and initialize all
392 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
394 size_t size = get_type_struct_size(kind);
395 type_t *res = obstack_alloc(type_obst, size);
396 memset(res, 0, size);
398 res->base.kind = kind;
399 res->base.source_position = *source_position;
404 * Returns the size of an initializer node.
406 * @param kind the initializer kind
408 static size_t get_initializer_size(initializer_kind_t kind)
410 static const size_t sizes[] = {
411 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
412 [INITIALIZER_STRING] = sizeof(initializer_string_t),
413 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
414 [INITIALIZER_LIST] = sizeof(initializer_list_t),
415 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
417 assert(kind < sizeof(sizes) / sizeof(*sizes));
418 assert(sizes[kind] != 0);
423 * Allocate an initializer node of given kind and initialize all
426 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
428 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
435 * Free a type from the type obstack.
437 static void free_type(void *type)
439 obstack_free(type_obst, type);
443 * Returns the index of the top element of the environment stack.
445 static size_t environment_top(void)
447 return ARR_LEN(environment_stack);
451 * Returns the index of the top element of the label stack.
453 static size_t label_top(void)
455 return ARR_LEN(label_stack);
459 * Return the next token.
461 static inline void next_token(void)
463 token = lookahead_buffer[lookahead_bufpos];
464 lookahead_buffer[lookahead_bufpos] = lexer_token;
467 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
470 print_token(stderr, &token);
471 fprintf(stderr, "\n");
476 * Return the next token with a given lookahead.
478 static inline const token_t *look_ahead(int num)
480 assert(num > 0 && num <= MAX_LOOKAHEAD);
481 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
482 return &lookahead_buffer[pos];
486 * Adds a token to the token anchor set (a multi-set).
488 static void add_anchor_token(int token_type) {
489 assert(0 <= token_type && token_type < T_LAST_TOKEN);
490 ++token_anchor_set[token_type];
493 static int save_and_reset_anchor_state(int token_type) {
494 assert(0 <= token_type && token_type < T_LAST_TOKEN);
495 int count = token_anchor_set[token_type];
496 token_anchor_set[token_type] = 0;
500 static void restore_anchor_state(int token_type, int count) {
501 assert(0 <= token_type && token_type < T_LAST_TOKEN);
502 token_anchor_set[token_type] = count;
506 * Remove a token from the token anchor set (a multi-set).
508 static void rem_anchor_token(int token_type) {
509 assert(0 <= token_type && token_type < T_LAST_TOKEN);
510 --token_anchor_set[token_type];
513 static bool at_anchor(void) {
516 return token_anchor_set[token.type];
520 * Eat tokens until a matching token is found.
522 static void eat_until_matching_token(int type) {
523 unsigned parenthesis_count = 0;
524 unsigned brace_count = 0;
525 unsigned bracket_count = 0;
526 int end_token = type;
535 while(token.type != end_token ||
536 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
540 case '(': ++parenthesis_count; break;
541 case '{': ++brace_count; break;
542 case '[': ++bracket_count; break;
544 if(parenthesis_count > 0)
552 if(bracket_count > 0)
563 * Eat input tokens until an anchor is found.
565 static void eat_until_anchor(void) {
566 if(token.type == T_EOF)
568 while(token_anchor_set[token.type] == 0) {
569 if(token.type == '(' || token.type == '{' || token.type == '[')
570 eat_until_matching_token(token.type);
571 if(token.type == T_EOF)
577 static void eat_block(void) {
578 eat_until_matching_token('{');
579 if(token.type == '}')
584 * eat all token until a ';' is reached or a stop token is found.
586 static void eat_statement(void) {
587 eat_until_matching_token(';');
588 if(token.type == ';')
592 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
595 * Report a parse error because an expected token was not found.
598 #if defined __GNUC__ && __GNUC__ >= 4
599 __attribute__((sentinel))
601 void parse_error_expected(const char *message, ...)
603 if(message != NULL) {
604 errorf(HERE, "%s", message);
607 va_start(ap, message);
608 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
613 * Report a type error.
615 static void type_error(const char *msg, const source_position_t *source_position,
618 errorf(source_position, "%s, but found type '%T'", msg, type);
622 * Report an incompatible type.
624 static void type_error_incompatible(const char *msg,
625 const source_position_t *source_position, type_t *type1, type_t *type2)
627 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
632 * Expect the the current token is the expected token.
633 * If not, generate an error, eat the current statement,
634 * and goto the end_error label.
636 #define expect(expected) \
638 if(UNLIKELY(token.type != (expected))) { \
639 parse_error_expected(NULL, (expected), NULL); \
640 add_anchor_token(expected); \
641 eat_until_anchor(); \
642 if (token.type == expected) \
644 rem_anchor_token(expected); \
650 static void set_scope(scope_t *new_scope)
653 scope->last_declaration = last_declaration;
657 last_declaration = new_scope->last_declaration;
661 * Search a symbol in a given namespace and returns its declaration or
662 * NULL if this symbol was not found.
664 static declaration_t *get_declaration(const symbol_t *const symbol,
665 const namespace_t namespc)
667 declaration_t *declaration = symbol->declaration;
668 for( ; declaration != NULL; declaration = declaration->symbol_next) {
669 if(declaration->namespc == namespc)
677 * pushs an environment_entry on the environment stack and links the
678 * corresponding symbol to the new entry
680 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
682 symbol_t *symbol = declaration->symbol;
683 namespace_t namespc = (namespace_t) declaration->namespc;
685 /* replace/add declaration into declaration list of the symbol */
686 declaration_t *iter = symbol->declaration;
688 symbol->declaration = declaration;
690 declaration_t *iter_last = NULL;
691 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
692 /* replace an entry? */
693 if(iter->namespc == namespc) {
694 if(iter_last == NULL) {
695 symbol->declaration = declaration;
697 iter_last->symbol_next = declaration;
699 declaration->symbol_next = iter->symbol_next;
704 assert(iter_last->symbol_next == NULL);
705 iter_last->symbol_next = declaration;
709 /* remember old declaration */
711 entry.symbol = symbol;
712 entry.old_declaration = iter;
713 entry.namespc = (unsigned short) namespc;
714 ARR_APP1(stack_entry_t, *stack_ptr, entry);
717 static void environment_push(declaration_t *declaration)
719 assert(declaration->source_position.input_name != NULL);
720 assert(declaration->parent_scope != NULL);
721 stack_push(&environment_stack, declaration);
724 static void label_push(declaration_t *declaration)
726 declaration->parent_scope = ¤t_function->scope;
727 stack_push(&label_stack, declaration);
731 * pops symbols from the environment stack until @p new_top is the top element
733 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
735 stack_entry_t *stack = *stack_ptr;
736 size_t top = ARR_LEN(stack);
739 assert(new_top <= top);
743 for(i = top; i > new_top; --i) {
744 stack_entry_t *entry = &stack[i - 1];
746 declaration_t *old_declaration = entry->old_declaration;
747 symbol_t *symbol = entry->symbol;
748 namespace_t namespc = (namespace_t)entry->namespc;
750 /* replace/remove declaration */
751 declaration_t *declaration = symbol->declaration;
752 assert(declaration != NULL);
753 if(declaration->namespc == namespc) {
754 if(old_declaration == NULL) {
755 symbol->declaration = declaration->symbol_next;
757 symbol->declaration = old_declaration;
760 declaration_t *iter_last = declaration;
761 declaration_t *iter = declaration->symbol_next;
762 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
763 /* replace an entry? */
764 if(iter->namespc == namespc) {
765 assert(iter_last != NULL);
766 iter_last->symbol_next = old_declaration;
767 if(old_declaration != NULL) {
768 old_declaration->symbol_next = iter->symbol_next;
773 assert(iter != NULL);
777 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
780 static void environment_pop_to(size_t new_top)
782 stack_pop_to(&environment_stack, new_top);
785 static void label_pop_to(size_t new_top)
787 stack_pop_to(&label_stack, new_top);
791 static int get_rank(const type_t *type)
793 assert(!is_typeref(type));
794 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
795 * and esp. footnote 108). However we can't fold constants (yet), so we
796 * can't decide whether unsigned int is possible, while int always works.
797 * (unsigned int would be preferable when possible... for stuff like
798 * struct { enum { ... } bla : 4; } ) */
799 if(type->kind == TYPE_ENUM)
800 return ATOMIC_TYPE_INT;
802 assert(type->kind == TYPE_ATOMIC);
803 return type->atomic.akind;
806 static type_t *promote_integer(type_t *type)
808 if(type->kind == TYPE_BITFIELD)
809 type = type->bitfield.base_type;
811 if(get_rank(type) < ATOMIC_TYPE_INT)
818 * Create a cast expression.
820 * @param expression the expression to cast
821 * @param dest_type the destination type
823 static expression_t *create_cast_expression(expression_t *expression,
826 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
828 cast->unary.value = expression;
829 cast->base.type = dest_type;
835 * Check if a given expression represents the 0 pointer constant.
837 static bool is_null_pointer_constant(const expression_t *expression)
839 /* skip void* cast */
840 if(expression->kind == EXPR_UNARY_CAST
841 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
842 expression = expression->unary.value;
845 /* TODO: not correct yet, should be any constant integer expression
846 * which evaluates to 0 */
847 if (expression->kind != EXPR_CONST)
850 type_t *const type = skip_typeref(expression->base.type);
851 if (!is_type_integer(type))
854 return expression->conste.v.int_value == 0;
858 * Create an implicit cast expression.
860 * @param expression the expression to cast
861 * @param dest_type the destination type
863 static expression_t *create_implicit_cast(expression_t *expression,
866 type_t *const source_type = expression->base.type;
868 if (source_type == dest_type)
871 return create_cast_expression(expression, dest_type);
874 /** Implements the rules from § 6.5.16.1 */
875 static type_t *semantic_assign(type_t *orig_type_left,
876 const expression_t *const right,
878 const source_position_t *source_position)
880 type_t *const orig_type_right = right->base.type;
881 type_t *const type_left = skip_typeref(orig_type_left);
882 type_t *const type_right = skip_typeref(orig_type_right);
884 if(is_type_pointer(type_left)) {
885 if(is_null_pointer_constant(right)) {
886 return orig_type_left;
887 } else if(is_type_pointer(type_right)) {
888 type_t *points_to_left
889 = skip_typeref(type_left->pointer.points_to);
890 type_t *points_to_right
891 = skip_typeref(type_right->pointer.points_to);
893 /* the left type has all qualifiers from the right type */
894 unsigned missing_qualifiers
895 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
896 if(missing_qualifiers != 0) {
897 errorf(source_position,
898 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
899 return orig_type_left;
902 points_to_left = get_unqualified_type(points_to_left);
903 points_to_right = get_unqualified_type(points_to_right);
905 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
906 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
907 return orig_type_left;
910 if (!types_compatible(points_to_left, points_to_right)) {
911 warningf(source_position,
912 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
913 orig_type_left, context, right, orig_type_right);
916 return orig_type_left;
917 } else if(is_type_integer(type_right)) {
918 warningf(source_position,
919 "%s makes pointer '%T' from integer '%T' without a cast",
920 context, orig_type_left, orig_type_right);
921 return orig_type_left;
923 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
924 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
925 && is_type_pointer(type_right))) {
926 return orig_type_left;
927 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
928 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
929 type_t *const unqual_type_left = get_unqualified_type(type_left);
930 type_t *const unqual_type_right = get_unqualified_type(type_right);
931 if (types_compatible(unqual_type_left, unqual_type_right)) {
932 return orig_type_left;
934 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
935 warningf(source_position,
936 "%s makes integer '%T' from pointer '%T' without a cast",
937 context, orig_type_left, orig_type_right);
938 return orig_type_left;
941 if (!is_type_valid(type_left))
944 if (!is_type_valid(type_right))
945 return orig_type_right;
950 static expression_t *parse_constant_expression(void)
952 /* start parsing at precedence 7 (conditional expression) */
953 expression_t *result = parse_sub_expression(7);
955 if(!is_constant_expression(result)) {
956 errorf(&result->base.source_position,
957 "expression '%E' is not constant\n", result);
963 static expression_t *parse_assignment_expression(void)
965 /* start parsing at precedence 2 (assignment expression) */
966 return parse_sub_expression(2);
969 static type_t *make_global_typedef(const char *name, type_t *type)
971 symbol_t *const symbol = symbol_table_insert(name);
973 declaration_t *const declaration = allocate_declaration_zero();
974 declaration->namespc = NAMESPACE_NORMAL;
975 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
976 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
977 declaration->type = type;
978 declaration->symbol = symbol;
979 declaration->source_position = builtin_source_position;
981 record_declaration(declaration);
983 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
984 typedef_type->typedeft.declaration = declaration;
989 static string_t parse_string_literals(void)
991 assert(token.type == T_STRING_LITERAL);
992 string_t result = token.v.string;
996 while (token.type == T_STRING_LITERAL) {
997 result = concat_strings(&result, &token.v.string);
1004 static const char *gnu_attribute_names[GNU_AK_LAST] = {
1005 [GNU_AK_CONST] = "const",
1006 [GNU_AK_VOLATILE] = "volatile",
1007 [GNU_AK_CDECL] = "cdecl",
1008 [GNU_AK_STDCALL] = "stdcall",
1009 [GNU_AK_FASTCALL] = "fastcall",
1010 [GNU_AK_DEPRECATED] = "deprecated",
1011 [GNU_AK_NOINLINE] = "noinline",
1012 [GNU_AK_NORETURN] = "noreturn",
1013 [GNU_AK_NAKED] = "naked",
1014 [GNU_AK_PURE] = "pure",
1015 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1016 [GNU_AK_MALLOC] = "malloc",
1017 [GNU_AK_WEAK] = "weak",
1018 [GNU_AK_CONSTRUCTOR] = "constructor",
1019 [GNU_AK_DESTRUCTOR] = "destructor",
1020 [GNU_AK_NOTHROW] = "nothrow",
1021 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1022 [GNU_AK_COMMON] = "coommon",
1023 [GNU_AK_NOCOMMON] = "nocommon",
1024 [GNU_AK_PACKED] = "packed",
1025 [GNU_AK_SHARED] = "shared",
1026 [GNU_AK_NOTSHARED] = "notshared",
1027 [GNU_AK_USED] = "used",
1028 [GNU_AK_UNUSED] = "unused",
1029 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1030 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1031 [GNU_AK_LONGCALL] = "longcall",
1032 [GNU_AK_SHORTCALL] = "shortcall",
1033 [GNU_AK_LONG_CALL] = "long_call",
1034 [GNU_AK_SHORT_CALL] = "short_call",
1035 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1036 [GNU_AK_INTERRUPT] = "interrupt",
1037 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1038 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1039 [GNU_AK_NESTING] = "nesting",
1040 [GNU_AK_NEAR] = "near",
1041 [GNU_AK_FAR] = "far",
1042 [GNU_AK_SIGNAL] = "signal",
1043 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1044 [GNU_AK_TINY_DATA] = "tiny_data",
1045 [GNU_AK_SAVEALL] = "saveall",
1046 [GNU_AK_FLATTEN] = "flatten",
1047 [GNU_AK_SSEREGPARM] = "sseregparm",
1048 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1049 [GNU_AK_RETURN_TWICE] = "return_twice",
1050 [GNU_AK_MAY_ALIAS] = "may_alias",
1051 [GNU_AK_MS_STRUCT] = "ms_struct",
1052 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1053 [GNU_AK_DLLIMPORT] = "dllimport",
1054 [GNU_AK_DLLEXPORT] = "dllexport",
1055 [GNU_AK_ALIGNED] = "aligned",
1056 [GNU_AK_ALIAS] = "alias",
1057 [GNU_AK_SECTION] = "section",
1058 [GNU_AK_FORMAT] = "format",
1059 [GNU_AK_FORMAT_ARG] = "format_arg",
1060 [GNU_AK_WEAKREF] = "weakref",
1061 [GNU_AK_NONNULL] = "nonnull",
1062 [GNU_AK_TLS_MODEL] = "tls_model",
1063 [GNU_AK_VISIBILITY] = "visibility",
1064 [GNU_AK_REGPARM] = "regparm",
1065 [GNU_AK_MODE] = "mode",
1066 [GNU_AK_MODEL] = "model",
1067 [GNU_AK_TRAP_EXIT] = "trap_exit",
1068 [GNU_AK_SP_SWITCH] = "sp_switch",
1069 [GNU_AK_SENTINEL] = "sentinel"
1073 * compare two string, ignoring double underscores on the second.
1075 static int strcmp_underscore(const char *s1, const char *s2) {
1076 if(s2[0] == '_' && s2[1] == '_') {
1078 size_t l1 = strlen(s1);
1079 if(l1 + 2 != strlen(s2)) {
1083 return strncmp(s1, s2, l1);
1085 return strcmp(s1, s2);
1089 * Allocate a new gnu temporal attribute.
1091 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1092 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1093 attribute->kind = kind;
1094 attribute->next = NULL;
1095 attribute->invalid = false;
1096 attribute->have_arguments = false;
1103 * parse one constant expression argument.
1105 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1106 expression_t *expression;
1107 add_anchor_token(')');
1108 expression = parse_constant_expression();
1109 rem_anchor_token(')');
1114 attribute->invalid = true;
1118 * parse a list of constant expressions arguments.
1120 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1121 expression_t *expression;
1122 add_anchor_token(')');
1123 add_anchor_token(',');
1125 expression = parse_constant_expression();
1126 if(token.type != ',')
1130 rem_anchor_token(',');
1131 rem_anchor_token(')');
1136 attribute->invalid = true;
1140 * parse one string literal argument.
1142 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1145 add_anchor_token('(');
1146 if(token.type != T_STRING_LITERAL) {
1147 parse_error_expected("while parsing attribute directive",
1148 T_STRING_LITERAL, NULL);
1151 *string = parse_string_literals();
1152 rem_anchor_token('(');
1156 attribute->invalid = true;
1160 * parse one tls model.
1162 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1163 static const char *tls_models[] = {
1169 string_t string = { NULL, 0 };
1170 parse_gnu_attribute_string_arg(attribute, &string);
1171 if(string.begin != NULL) {
1172 for(size_t i = 0; i < 4; ++i) {
1173 if(strcmp(tls_models[i], string.begin) == 0) {
1174 attribute->u.value = i;
1179 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1180 attribute->invalid = true;
1184 * parse one tls model.
1186 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1187 static const char *visibilities[] = {
1193 string_t string = { NULL, 0 };
1194 parse_gnu_attribute_string_arg(attribute, &string);
1195 if(string.begin != NULL) {
1196 for(size_t i = 0; i < 4; ++i) {
1197 if(strcmp(visibilities[i], string.begin) == 0) {
1198 attribute->u.value = i;
1203 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1204 attribute->invalid = true;
1208 * parse one (code) model.
1210 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1211 static const char *visibilities[] = {
1216 string_t string = { NULL, 0 };
1217 parse_gnu_attribute_string_arg(attribute, &string);
1218 if(string.begin != NULL) {
1219 for(int i = 0; i < 3; ++i) {
1220 if(strcmp(visibilities[i], string.begin) == 0) {
1221 attribute->u.value = i;
1226 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1227 attribute->invalid = true;
1230 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1232 /* TODO: find out what is allowed here... */
1234 /* at least: byte, word, pointer, list of machine modes
1235 * __XXX___ is interpreted as XXX */
1236 add_anchor_token(')');
1237 expect(T_IDENTIFIER);
1238 rem_anchor_token(')');
1242 attribute->invalid = true;
1246 * parse one interrupt argument.
1248 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1249 static const char *interrupts[] = {
1256 string_t string = { NULL, 0 };
1257 parse_gnu_attribute_string_arg(attribute, &string);
1258 if(string.begin != NULL) {
1259 for(size_t i = 0; i < 5; ++i) {
1260 if(strcmp(interrupts[i], string.begin) == 0) {
1261 attribute->u.value = i;
1266 errorf(HERE, "'%s' is an interrupt", string.begin);
1267 attribute->invalid = true;
1271 * parse ( identifier, const expression, const expression )
1273 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1274 static const char *format_names[] = {
1282 if(token.type != T_IDENTIFIER) {
1283 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1286 const char *name = token.v.symbol->string;
1287 for(i = 0; i < 4; ++i) {
1288 if(strcmp_underscore(format_names[i], name) == 0)
1292 if(warning.attribute)
1293 warningf(HERE, "'%s' is an unrecognized format function type", name);
1298 add_anchor_token(')');
1299 add_anchor_token(',');
1300 parse_constant_expression();
1301 rem_anchor_token(',');
1302 rem_anchor_token('(');
1305 add_anchor_token(')');
1306 parse_constant_expression();
1307 rem_anchor_token('(');
1311 attribute->u.value = true;
1315 * Parse one GNU attribute.
1317 * Note that attribute names can be specified WITH or WITHOUT
1318 * double underscores, ie const or __const__.
1320 * The following attributes are parsed without arguments
1345 * no_instrument_function
1346 * warn_unused_result
1363 * externally_visible
1371 * The following attributes are parsed with arguments
1372 * aligned( const expression )
1373 * alias( string literal )
1374 * section( string literal )
1375 * format( identifier, const expression, const expression )
1376 * format_arg( const expression )
1377 * tls_model( string literal )
1378 * visibility( string literal )
1379 * regparm( const expression )
1380 * model( string leteral )
1381 * trap_exit( const expression )
1382 * sp_switch( string literal )
1384 * The following attributes might have arguments
1385 * weak_ref( string literal )
1386 * non_null( const expression // ',' )
1387 * interrupt( string literal )
1388 * sentinel( constant expression )
1390 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1392 gnu_attribute_t *head = *attributes;
1393 gnu_attribute_t *last = *attributes;
1394 gnu_attribute_t *attribute;
1396 eat(T___attribute__);
1400 if(token.type != ')') {
1401 /* find the end of the list */
1403 while(last->next != NULL)
1407 /* non-empty attribute list */
1410 if(token.type == T_const) {
1412 } else if(token.type == T_volatile) {
1414 } else if(token.type == T_cdecl) {
1415 /* __attribute__((cdecl)), WITH ms mode */
1417 } else if(token.type != T_IDENTIFIER) {
1418 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1421 const symbol_t *sym = token.v.symbol;
1426 for(i = 0; i < GNU_AK_LAST; ++i) {
1427 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1430 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1433 if(kind == GNU_AK_LAST) {
1434 if(warning.attribute)
1435 warningf(HERE, "'%s' attribute directive ignored", name);
1437 /* skip possible arguments */
1438 if(token.type == '(') {
1439 eat_until_matching_token(')');
1442 /* check for arguments */
1443 attribute = allocate_gnu_attribute(kind);
1444 if(token.type == '(') {
1446 if(token.type == ')') {
1447 /* empty args are allowed */
1450 attribute->have_arguments = true;
1455 case GNU_AK_VOLATILE:
1457 case GNU_AK_STDCALL:
1458 case GNU_AK_FASTCALL:
1459 case GNU_AK_DEPRECATED:
1460 case GNU_AK_NOINLINE:
1461 case GNU_AK_NORETURN:
1464 case GNU_AK_ALWAYS_INLINE:
1467 case GNU_AK_CONSTRUCTOR:
1468 case GNU_AK_DESTRUCTOR:
1469 case GNU_AK_NOTHROW:
1470 case GNU_AK_TRANSPARENT_UNION:
1472 case GNU_AK_NOCOMMON:
1475 case GNU_AK_NOTSHARED:
1478 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1479 case GNU_AK_WARN_UNUSED_RESULT:
1480 case GNU_AK_LONGCALL:
1481 case GNU_AK_SHORTCALL:
1482 case GNU_AK_LONG_CALL:
1483 case GNU_AK_SHORT_CALL:
1484 case GNU_AK_FUNCTION_VECTOR:
1485 case GNU_AK_INTERRUPT_HANDLER:
1486 case GNU_AK_NMI_HANDLER:
1487 case GNU_AK_NESTING:
1491 case GNU_AK_EIGTHBIT_DATA:
1492 case GNU_AK_TINY_DATA:
1493 case GNU_AK_SAVEALL:
1494 case GNU_AK_FLATTEN:
1495 case GNU_AK_SSEREGPARM:
1496 case GNU_AK_EXTERNALLY_VISIBLE:
1497 case GNU_AK_RETURN_TWICE:
1498 case GNU_AK_MAY_ALIAS:
1499 case GNU_AK_MS_STRUCT:
1500 case GNU_AK_GCC_STRUCT:
1501 case GNU_AK_DLLIMPORT:
1502 case GNU_AK_DLLEXPORT:
1503 if(attribute->have_arguments) {
1504 /* should have no arguments */
1505 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1506 eat_until_matching_token('(');
1507 /* we have already consumed '(', so we stop before ')', eat it */
1509 attribute->invalid = true;
1513 case GNU_AK_ALIGNED:
1514 case GNU_AK_FORMAT_ARG:
1515 case GNU_AK_REGPARM:
1516 case GNU_AK_TRAP_EXIT:
1517 if(!attribute->have_arguments) {
1518 /* should have arguments */
1519 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1520 attribute->invalid = true;
1522 parse_gnu_attribute_const_arg(attribute);
1525 case GNU_AK_SECTION:
1526 case GNU_AK_SP_SWITCH:
1527 if(!attribute->have_arguments) {
1528 /* should have arguments */
1529 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1530 attribute->invalid = true;
1532 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1535 if(!attribute->have_arguments) {
1536 /* should have arguments */
1537 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1538 attribute->invalid = true;
1540 parse_gnu_attribute_format_args(attribute);
1542 case GNU_AK_WEAKREF:
1543 /* may have one string argument */
1544 if(attribute->have_arguments)
1545 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1547 case GNU_AK_NONNULL:
1548 if(attribute->have_arguments)
1549 parse_gnu_attribute_const_arg_list(attribute);
1551 case GNU_AK_TLS_MODEL:
1552 if(!attribute->have_arguments) {
1553 /* should have arguments */
1554 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1556 parse_gnu_attribute_tls_model_arg(attribute);
1558 case GNU_AK_VISIBILITY:
1559 if(!attribute->have_arguments) {
1560 /* should have arguments */
1561 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1563 parse_gnu_attribute_visibility_arg(attribute);
1566 if(!attribute->have_arguments) {
1567 /* should have arguments */
1568 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1570 parse_gnu_attribute_model_arg(attribute);
1574 if(!attribute->have_arguments) {
1575 /* should have arguments */
1576 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1578 parse_gnu_attribute_mode_arg(attribute);
1581 case GNU_AK_INTERRUPT:
1582 /* may have one string argument */
1583 if(attribute->have_arguments)
1584 parse_gnu_attribute_interrupt_arg(attribute);
1586 case GNU_AK_SENTINEL:
1587 /* may have one string argument */
1588 if(attribute->have_arguments)
1589 parse_gnu_attribute_const_arg(attribute);
1592 /* already handled */
1596 if(attribute != NULL) {
1598 last->next = attribute;
1601 head = last = attribute;
1605 if(token.type != ',')
1617 * Parse GNU attributes.
1619 static void parse_attributes(gnu_attribute_t **attributes)
1622 switch(token.type) {
1623 case T___attribute__: {
1624 parse_gnu_attribute(attributes);
1630 if(token.type != T_STRING_LITERAL) {
1631 parse_error_expected("while parsing assembler attribute",
1632 T_STRING_LITERAL, NULL);
1633 eat_until_matching_token('(');
1636 parse_string_literals();
1641 goto attributes_finished;
1645 attributes_finished:
1650 static designator_t *parse_designation(void)
1652 designator_t *result = NULL;
1653 designator_t *last = NULL;
1656 designator_t *designator;
1657 switch(token.type) {
1659 designator = allocate_ast_zero(sizeof(designator[0]));
1660 designator->source_position = token.source_position;
1662 add_anchor_token(']');
1663 designator->array_index = parse_constant_expression();
1664 rem_anchor_token(']');
1668 designator = allocate_ast_zero(sizeof(designator[0]));
1669 designator->source_position = token.source_position;
1671 if(token.type != T_IDENTIFIER) {
1672 parse_error_expected("while parsing designator",
1673 T_IDENTIFIER, NULL);
1676 designator->symbol = token.v.symbol;
1684 assert(designator != NULL);
1686 last->next = designator;
1688 result = designator;
1696 static initializer_t *initializer_from_string(array_type_t *type,
1697 const string_t *const string)
1699 /* TODO: check len vs. size of array type */
1702 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1703 initializer->string.string = *string;
1708 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1709 wide_string_t *const string)
1711 /* TODO: check len vs. size of array type */
1714 initializer_t *const initializer =
1715 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1716 initializer->wide_string.string = *string;
1722 * Build an initializer from a given expression.
1724 static initializer_t *initializer_from_expression(type_t *orig_type,
1725 expression_t *expression)
1727 /* TODO check that expression is a constant expression */
1729 /* § 6.7.8.14/15 char array may be initialized by string literals */
1730 type_t *type = skip_typeref(orig_type);
1731 type_t *expr_type_orig = expression->base.type;
1732 type_t *expr_type = skip_typeref(expr_type_orig);
1733 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1734 array_type_t *const array_type = &type->array;
1735 type_t *const element_type = skip_typeref(array_type->element_type);
1737 if (element_type->kind == TYPE_ATOMIC) {
1738 atomic_type_kind_t akind = element_type->atomic.akind;
1739 switch (expression->kind) {
1740 case EXPR_STRING_LITERAL:
1741 if (akind == ATOMIC_TYPE_CHAR
1742 || akind == ATOMIC_TYPE_SCHAR
1743 || akind == ATOMIC_TYPE_UCHAR) {
1744 return initializer_from_string(array_type,
1745 &expression->string.value);
1748 case EXPR_WIDE_STRING_LITERAL: {
1749 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1750 if (get_unqualified_type(element_type) == bare_wchar_type) {
1751 return initializer_from_wide_string(array_type,
1752 &expression->wide_string.value);
1762 type_t *const res_type = semantic_assign(type, expression, "initializer",
1763 &expression->base.source_position);
1764 if (res_type == NULL)
1767 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1768 result->value.value = create_implicit_cast(expression, res_type);
1774 * Checks if a given expression can be used as an constant initializer.
1776 static bool is_initializer_constant(const expression_t *expression)
1778 return is_constant_expression(expression)
1779 || is_address_constant(expression);
1783 * Parses an scalar initializer.
1785 * § 6.7.8.11; eat {} without warning
1787 static initializer_t *parse_scalar_initializer(type_t *type,
1788 bool must_be_constant)
1790 /* there might be extra {} hierarchies */
1792 while(token.type == '{') {
1795 warningf(HERE, "extra curly braces around scalar initializer");
1800 expression_t *expression = parse_assignment_expression();
1801 if(must_be_constant && !is_initializer_constant(expression)) {
1802 errorf(&expression->base.source_position,
1803 "Initialisation expression '%E' is not constant\n",
1807 initializer_t *initializer = initializer_from_expression(type, expression);
1809 if(initializer == NULL) {
1810 errorf(&expression->base.source_position,
1811 "expression '%E' (type '%T') doesn't match expected type '%T'",
1812 expression, expression->base.type, type);
1817 bool additional_warning_displayed = false;
1819 if(token.type == ',') {
1822 if(token.type != '}') {
1823 if(!additional_warning_displayed) {
1824 warningf(HERE, "additional elements in scalar initializer");
1825 additional_warning_displayed = true;
1836 * An entry in the type path.
1838 typedef struct type_path_entry_t type_path_entry_t;
1839 struct type_path_entry_t {
1840 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1842 size_t index; /**< For array types: the current index. */
1843 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1848 * A type path expression a position inside compound or array types.
1850 typedef struct type_path_t type_path_t;
1851 struct type_path_t {
1852 type_path_entry_t *path; /**< An flexible array containing the current path. */
1853 type_t *top_type; /**< type of the element the path points */
1854 size_t max_index; /**< largest index in outermost array */
1858 * Prints a type path for debugging.
1860 static __attribute__((unused)) void debug_print_type_path(
1861 const type_path_t *path)
1863 size_t len = ARR_LEN(path->path);
1865 for(size_t i = 0; i < len; ++i) {
1866 const type_path_entry_t *entry = & path->path[i];
1868 type_t *type = skip_typeref(entry->type);
1869 if(is_type_compound(type)) {
1870 /* in gcc mode structs can have no members */
1871 if(entry->v.compound_entry == NULL) {
1875 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1876 } else if(is_type_array(type)) {
1877 fprintf(stderr, "[%zd]", entry->v.index);
1879 fprintf(stderr, "-INVALID-");
1882 if(path->top_type != NULL) {
1883 fprintf(stderr, " (");
1884 print_type(path->top_type);
1885 fprintf(stderr, ")");
1890 * Return the top type path entry, ie. in a path
1891 * (type).a.b returns the b.
1893 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1895 size_t len = ARR_LEN(path->path);
1897 return &path->path[len-1];
1901 * Enlarge the type path by an (empty) element.
1903 static type_path_entry_t *append_to_type_path(type_path_t *path)
1905 size_t len = ARR_LEN(path->path);
1906 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1908 type_path_entry_t *result = & path->path[len];
1909 memset(result, 0, sizeof(result[0]));
1914 * Descending into a sub-type. Enter the scope of the current
1917 static void descend_into_subtype(type_path_t *path)
1919 type_t *orig_top_type = path->top_type;
1920 type_t *top_type = skip_typeref(orig_top_type);
1922 assert(is_type_compound(top_type) || is_type_array(top_type));
1924 type_path_entry_t *top = append_to_type_path(path);
1925 top->type = top_type;
1927 if(is_type_compound(top_type)) {
1928 declaration_t *declaration = top_type->compound.declaration;
1929 declaration_t *entry = declaration->scope.declarations;
1930 top->v.compound_entry = entry;
1933 path->top_type = entry->type;
1935 path->top_type = NULL;
1938 assert(is_type_array(top_type));
1941 path->top_type = top_type->array.element_type;
1946 * Pop an entry from the given type path, ie. returning from
1947 * (type).a.b to (type).a
1949 static void ascend_from_subtype(type_path_t *path)
1951 type_path_entry_t *top = get_type_path_top(path);
1953 path->top_type = top->type;
1955 size_t len = ARR_LEN(path->path);
1956 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1960 * Pop entries from the given type path until the given
1961 * path level is reached.
1963 static void ascend_to(type_path_t *path, size_t top_path_level)
1965 size_t len = ARR_LEN(path->path);
1967 while(len > top_path_level) {
1968 ascend_from_subtype(path);
1969 len = ARR_LEN(path->path);
1973 static bool walk_designator(type_path_t *path, const designator_t *designator,
1974 bool used_in_offsetof)
1976 for( ; designator != NULL; designator = designator->next) {
1977 type_path_entry_t *top = get_type_path_top(path);
1978 type_t *orig_type = top->type;
1980 type_t *type = skip_typeref(orig_type);
1982 if(designator->symbol != NULL) {
1983 symbol_t *symbol = designator->symbol;
1984 if(!is_type_compound(type)) {
1985 if(is_type_valid(type)) {
1986 errorf(&designator->source_position,
1987 "'.%Y' designator used for non-compound type '%T'",
1993 declaration_t *declaration = type->compound.declaration;
1994 declaration_t *iter = declaration->scope.declarations;
1995 for( ; iter != NULL; iter = iter->next) {
1996 if(iter->symbol == symbol) {
2001 errorf(&designator->source_position,
2002 "'%T' has no member named '%Y'", orig_type, symbol);
2005 if(used_in_offsetof) {
2006 type_t *real_type = skip_typeref(iter->type);
2007 if(real_type->kind == TYPE_BITFIELD) {
2008 errorf(&designator->source_position,
2009 "offsetof designator '%Y' may not specify bitfield",
2015 top->type = orig_type;
2016 top->v.compound_entry = iter;
2017 orig_type = iter->type;
2019 expression_t *array_index = designator->array_index;
2020 assert(designator->array_index != NULL);
2022 if(!is_type_array(type)) {
2023 if(is_type_valid(type)) {
2024 errorf(&designator->source_position,
2025 "[%E] designator used for non-array type '%T'",
2026 array_index, orig_type);
2030 if(!is_type_valid(array_index->base.type)) {
2034 long index = fold_constant(array_index);
2035 if(!used_in_offsetof) {
2037 errorf(&designator->source_position,
2038 "array index [%E] must be positive", array_index);
2041 if(type->array.size_constant == true) {
2042 long array_size = type->array.size;
2043 if(index >= array_size) {
2044 errorf(&designator->source_position,
2045 "designator [%E] (%d) exceeds array size %d",
2046 array_index, index, array_size);
2052 top->type = orig_type;
2053 top->v.index = (size_t) index;
2054 orig_type = type->array.element_type;
2056 path->top_type = orig_type;
2058 if(designator->next != NULL) {
2059 descend_into_subtype(path);
2068 static void advance_current_object(type_path_t *path, size_t top_path_level)
2070 type_path_entry_t *top = get_type_path_top(path);
2072 type_t *type = skip_typeref(top->type);
2073 if(is_type_union(type)) {
2074 /* in unions only the first element is initialized */
2075 top->v.compound_entry = NULL;
2076 } else if(is_type_struct(type)) {
2077 declaration_t *entry = top->v.compound_entry;
2079 entry = entry->next;
2080 top->v.compound_entry = entry;
2082 path->top_type = entry->type;
2086 assert(is_type_array(type));
2090 if(!type->array.size_constant || top->v.index < type->array.size) {
2095 /* we're past the last member of the current sub-aggregate, try if we
2096 * can ascend in the type hierarchy and continue with another subobject */
2097 size_t len = ARR_LEN(path->path);
2099 if(len > top_path_level) {
2100 ascend_from_subtype(path);
2101 advance_current_object(path, top_path_level);
2103 path->top_type = NULL;
2108 * skip until token is found.
2110 static void skip_until(int type) {
2111 while(token.type != type) {
2112 if(token.type == T_EOF)
2119 * skip any {...} blocks until a closing bracket is reached.
2121 static void skip_initializers(void)
2123 if(token.type == '{')
2126 while(token.type != '}') {
2127 if(token.type == T_EOF)
2129 if(token.type == '{') {
2137 static initializer_t *create_empty_initializer(void)
2139 static initializer_t empty_initializer
2140 = { .list = { { INITIALIZER_LIST }, 0 } };
2141 return &empty_initializer;
2145 * Parse a part of an initialiser for a struct or union,
2147 static initializer_t *parse_sub_initializer(type_path_t *path,
2148 type_t *outer_type, size_t top_path_level,
2149 parse_initializer_env_t *env)
2151 if(token.type == '}') {
2152 /* empty initializer */
2153 return create_empty_initializer();
2156 type_t *orig_type = path->top_type;
2157 type_t *type = NULL;
2159 if (orig_type == NULL) {
2160 /* We are initializing an empty compound. */
2162 type = skip_typeref(orig_type);
2164 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2165 * initializers in this case. */
2166 if(!is_type_valid(type)) {
2167 skip_initializers();
2168 return create_empty_initializer();
2172 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2175 designator_t *designator = NULL;
2176 if(token.type == '.' || token.type == '[') {
2177 designator = parse_designation();
2179 /* reset path to toplevel, evaluate designator from there */
2180 ascend_to(path, top_path_level);
2181 if(!walk_designator(path, designator, false)) {
2182 /* can't continue after designation error */
2186 initializer_t *designator_initializer
2187 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2188 designator_initializer->designator.designator = designator;
2189 ARR_APP1(initializer_t*, initializers, designator_initializer);
2194 if(token.type == '{') {
2195 if(type != NULL && is_type_scalar(type)) {
2196 sub = parse_scalar_initializer(type, env->must_be_constant);
2200 if (env->declaration != NULL)
2201 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2202 env->declaration->symbol);
2204 errorf(HERE, "extra brace group at end of initializer");
2206 descend_into_subtype(path);
2208 add_anchor_token('}');
2209 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2211 rem_anchor_token('}');
2214 ascend_from_subtype(path);
2218 goto error_parse_next;
2222 /* must be an expression */
2223 expression_t *expression = parse_assignment_expression();
2225 if(env->must_be_constant && !is_initializer_constant(expression)) {
2226 errorf(&expression->base.source_position,
2227 "Initialisation expression '%E' is not constant\n",
2232 /* we are already outside, ... */
2236 /* handle { "string" } special case */
2237 if((expression->kind == EXPR_STRING_LITERAL
2238 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2239 && outer_type != NULL) {
2240 sub = initializer_from_expression(outer_type, expression);
2242 if(token.type == ',') {
2245 if(token.type != '}') {
2246 warningf(HERE, "excessive elements in initializer for type '%T'",
2249 /* TODO: eat , ... */
2254 /* descend into subtypes until expression matches type */
2256 orig_type = path->top_type;
2257 type = skip_typeref(orig_type);
2259 sub = initializer_from_expression(orig_type, expression);
2263 if(!is_type_valid(type)) {
2266 if(is_type_scalar(type)) {
2267 errorf(&expression->base.source_position,
2268 "expression '%E' doesn't match expected type '%T'",
2269 expression, orig_type);
2273 descend_into_subtype(path);
2277 /* update largest index of top array */
2278 const type_path_entry_t *first = &path->path[0];
2279 type_t *first_type = first->type;
2280 first_type = skip_typeref(first_type);
2281 if(is_type_array(first_type)) {
2282 size_t index = first->v.index;
2283 if(index > path->max_index)
2284 path->max_index = index;
2288 /* append to initializers list */
2289 ARR_APP1(initializer_t*, initializers, sub);
2292 if(env->declaration != NULL)
2293 warningf(HERE, "excess elements in struct initializer for '%Y'",
2294 env->declaration->symbol);
2296 warningf(HERE, "excess elements in struct initializer");
2300 if(token.type == '}') {
2304 if(token.type == '}') {
2309 /* advance to the next declaration if we are not at the end */
2310 advance_current_object(path, top_path_level);
2311 orig_type = path->top_type;
2312 if(orig_type != NULL)
2313 type = skip_typeref(orig_type);
2319 size_t len = ARR_LEN(initializers);
2320 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2321 initializer_t *result = allocate_ast_zero(size);
2322 result->kind = INITIALIZER_LIST;
2323 result->list.len = len;
2324 memcpy(&result->list.initializers, initializers,
2325 len * sizeof(initializers[0]));
2327 DEL_ARR_F(initializers);
2328 ascend_to(path, top_path_level);
2333 skip_initializers();
2334 DEL_ARR_F(initializers);
2335 ascend_to(path, top_path_level);
2340 * Parses an initializer. Parsers either a compound literal
2341 * (env->declaration == NULL) or an initializer of a declaration.
2343 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2345 type_t *type = skip_typeref(env->type);
2346 initializer_t *result = NULL;
2349 if(is_type_scalar(type)) {
2350 result = parse_scalar_initializer(type, env->must_be_constant);
2351 } else if(token.type == '{') {
2355 memset(&path, 0, sizeof(path));
2356 path.top_type = env->type;
2357 path.path = NEW_ARR_F(type_path_entry_t, 0);
2359 descend_into_subtype(&path);
2361 add_anchor_token('}');
2362 result = parse_sub_initializer(&path, env->type, 1, env);
2363 rem_anchor_token('}');
2365 max_index = path.max_index;
2366 DEL_ARR_F(path.path);
2370 /* parse_scalar_initializer() also works in this case: we simply
2371 * have an expression without {} around it */
2372 result = parse_scalar_initializer(type, env->must_be_constant);
2375 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2376 * the array type size */
2377 if(is_type_array(type) && type->array.size_expression == NULL
2378 && result != NULL) {
2380 switch (result->kind) {
2381 case INITIALIZER_LIST:
2382 size = max_index + 1;
2385 case INITIALIZER_STRING:
2386 size = result->string.string.size;
2389 case INITIALIZER_WIDE_STRING:
2390 size = result->wide_string.string.size;
2394 internal_errorf(HERE, "invalid initializer type");
2397 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2398 cnst->base.type = type_size_t;
2399 cnst->conste.v.int_value = size;
2401 type_t *new_type = duplicate_type(type);
2403 new_type->array.size_expression = cnst;
2404 new_type->array.size_constant = true;
2405 new_type->array.size = size;
2406 env->type = new_type;
2414 static declaration_t *append_declaration(declaration_t *declaration);
2416 static declaration_t *parse_compound_type_specifier(bool is_struct)
2418 gnu_attribute_t *attributes = NULL;
2425 symbol_t *symbol = NULL;
2426 declaration_t *declaration = NULL;
2428 if (token.type == T___attribute__) {
2429 parse_attributes(&attributes);
2432 if(token.type == T_IDENTIFIER) {
2433 symbol = token.v.symbol;
2437 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2439 declaration = get_declaration(symbol, NAMESPACE_UNION);
2441 } else if(token.type != '{') {
2443 parse_error_expected("while parsing struct type specifier",
2444 T_IDENTIFIER, '{', NULL);
2446 parse_error_expected("while parsing union type specifier",
2447 T_IDENTIFIER, '{', NULL);
2453 if(declaration == NULL) {
2454 declaration = allocate_declaration_zero();
2455 declaration->namespc =
2456 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2457 declaration->source_position = token.source_position;
2458 declaration->symbol = symbol;
2459 declaration->parent_scope = scope;
2460 if (symbol != NULL) {
2461 environment_push(declaration);
2463 append_declaration(declaration);
2466 if(token.type == '{') {
2467 if (declaration->init.complete) {
2468 assert(symbol != NULL);
2469 errorf(HERE, "multiple definitions of '%s %Y'",
2470 is_struct ? "struct" : "union", symbol);
2471 declaration->scope.declarations = NULL;
2473 declaration->init.complete = true;
2475 parse_compound_type_entries(declaration);
2476 parse_attributes(&attributes);
2482 static void parse_enum_entries(type_t *const enum_type)
2486 if(token.type == '}') {
2488 errorf(HERE, "empty enum not allowed");
2492 add_anchor_token('}');
2494 if(token.type != T_IDENTIFIER) {
2495 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2497 rem_anchor_token('}');
2501 declaration_t *const entry = allocate_declaration_zero();
2502 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2503 entry->type = enum_type;
2504 entry->symbol = token.v.symbol;
2505 entry->source_position = token.source_position;
2508 if(token.type == '=') {
2510 expression_t *value = parse_constant_expression();
2512 value = create_implicit_cast(value, enum_type);
2513 entry->init.enum_value = value;
2518 record_declaration(entry);
2520 if(token.type != ',')
2523 } while(token.type != '}');
2524 rem_anchor_token('}');
2532 static type_t *parse_enum_specifier(void)
2534 gnu_attribute_t *attributes = NULL;
2535 declaration_t *declaration;
2539 if(token.type == T_IDENTIFIER) {
2540 symbol = token.v.symbol;
2543 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2544 } else if(token.type != '{') {
2545 parse_error_expected("while parsing enum type specifier",
2546 T_IDENTIFIER, '{', NULL);
2553 if(declaration == NULL) {
2554 declaration = allocate_declaration_zero();
2555 declaration->namespc = NAMESPACE_ENUM;
2556 declaration->source_position = token.source_position;
2557 declaration->symbol = symbol;
2558 declaration->parent_scope = scope;
2561 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2562 type->enumt.declaration = declaration;
2564 if(token.type == '{') {
2565 if(declaration->init.complete) {
2566 errorf(HERE, "multiple definitions of enum %Y", symbol);
2568 if (symbol != NULL) {
2569 environment_push(declaration);
2571 append_declaration(declaration);
2572 declaration->init.complete = true;
2574 parse_enum_entries(type);
2575 parse_attributes(&attributes);
2582 * if a symbol is a typedef to another type, return true
2584 static bool is_typedef_symbol(symbol_t *symbol)
2586 const declaration_t *const declaration =
2587 get_declaration(symbol, NAMESPACE_NORMAL);
2589 declaration != NULL &&
2590 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2593 static type_t *parse_typeof(void)
2600 add_anchor_token(')');
2602 expression_t *expression = NULL;
2605 switch(token.type) {
2606 case T___extension__:
2607 /* this can be a prefix to a typename or an expression */
2608 /* we simply eat it now. */
2611 } while(token.type == T___extension__);
2615 if(is_typedef_symbol(token.v.symbol)) {
2616 type = parse_typename();
2618 expression = parse_expression();
2619 type = expression->base.type;
2624 type = parse_typename();
2628 expression = parse_expression();
2629 type = expression->base.type;
2633 rem_anchor_token(')');
2636 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2637 typeof_type->typeoft.expression = expression;
2638 typeof_type->typeoft.typeof_type = type;
2646 SPECIFIER_SIGNED = 1 << 0,
2647 SPECIFIER_UNSIGNED = 1 << 1,
2648 SPECIFIER_LONG = 1 << 2,
2649 SPECIFIER_INT = 1 << 3,
2650 SPECIFIER_DOUBLE = 1 << 4,
2651 SPECIFIER_CHAR = 1 << 5,
2652 SPECIFIER_SHORT = 1 << 6,
2653 SPECIFIER_LONG_LONG = 1 << 7,
2654 SPECIFIER_FLOAT = 1 << 8,
2655 SPECIFIER_BOOL = 1 << 9,
2656 SPECIFIER_VOID = 1 << 10,
2657 SPECIFIER_INT8 = 1 << 11,
2658 SPECIFIER_INT16 = 1 << 12,
2659 SPECIFIER_INT32 = 1 << 13,
2660 SPECIFIER_INT64 = 1 << 14,
2661 SPECIFIER_INT128 = 1 << 15,
2662 SPECIFIER_COMPLEX = 1 << 16,
2663 SPECIFIER_IMAGINARY = 1 << 17,
2666 static type_t *create_builtin_type(symbol_t *const symbol,
2667 type_t *const real_type)
2669 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2670 type->builtin.symbol = symbol;
2671 type->builtin.real_type = real_type;
2673 type_t *result = typehash_insert(type);
2674 if(type != result) {
2681 static type_t *get_typedef_type(symbol_t *symbol)
2683 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2684 if(declaration == NULL ||
2685 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2688 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2689 type->typedeft.declaration = declaration;
2695 * check for the allowed MS alignment values.
2697 static bool check_elignment_value(long long intvalue) {
2698 if(intvalue < 1 || intvalue > 8192) {
2699 errorf(HERE, "illegal alignment value");
2702 unsigned v = (unsigned)intvalue;
2703 for(unsigned i = 1; i <= 8192; i += i) {
2707 errorf(HERE, "alignment must be power of two");
2711 #define DET_MOD(name, tag) do { \
2712 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2713 *modifiers |= tag; \
2716 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2718 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2721 if(token.type == T_restrict) {
2723 DET_MOD(restrict, DM_RESTRICT);
2725 } else if(token.type != T_IDENTIFIER)
2727 symbol_t *symbol = token.v.symbol;
2728 if(symbol == sym_align) {
2731 if(token.type != T_INTEGER)
2733 if(check_elignment_value(token.v.intvalue)) {
2734 if(specifiers->alignment != 0)
2735 warningf(HERE, "align used more than once");
2736 specifiers->alignment = (unsigned char)token.v.intvalue;
2740 } else if(symbol == sym_allocate) {
2743 if(token.type != T_IDENTIFIER)
2745 (void)token.v.symbol;
2747 } else if(symbol == sym_dllimport) {
2749 DET_MOD(dllimport, DM_DLLIMPORT);
2750 } else if(symbol == sym_dllexport) {
2752 DET_MOD(dllexport, DM_DLLEXPORT);
2753 } else if(symbol == sym_thread) {
2755 DET_MOD(thread, DM_THREAD);
2756 } else if(symbol == sym_naked) {
2758 DET_MOD(naked, DM_NAKED);
2759 } else if(symbol == sym_noinline) {
2761 DET_MOD(noinline, DM_NOINLINE);
2762 } else if(symbol == sym_noreturn) {
2764 DET_MOD(noreturn, DM_NORETURN);
2765 } else if(symbol == sym_nothrow) {
2767 DET_MOD(nothrow, DM_NOTHROW);
2768 } else if(symbol == sym_novtable) {
2770 DET_MOD(novtable, DM_NOVTABLE);
2771 } else if(symbol == sym_property) {
2775 bool is_get = false;
2776 if(token.type != T_IDENTIFIER)
2778 if(token.v.symbol == sym_get) {
2780 } else if(token.v.symbol == sym_put) {
2782 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2787 if(token.type != T_IDENTIFIER)
2790 if(specifiers->get_property_sym != NULL) {
2791 errorf(HERE, "get property name already specified");
2793 specifiers->get_property_sym = token.v.symbol;
2796 if(specifiers->put_property_sym != NULL) {
2797 errorf(HERE, "put property name already specified");
2799 specifiers->put_property_sym = token.v.symbol;
2803 if(token.type == ',') {
2810 } else if(symbol == sym_selectany) {
2812 DET_MOD(selectany, DM_SELECTANY);
2813 } else if(symbol == sym_uuid) {
2816 if(token.type != T_STRING_LITERAL)
2820 } else if(symbol == sym_deprecated) {
2822 if(specifiers->deprecated != 0)
2823 warningf(HERE, "deprecated used more than once");
2824 specifiers->deprecated = 1;
2825 if(token.type == '(') {
2827 if(token.type == T_STRING_LITERAL) {
2828 specifiers->deprecated_string = token.v.string.begin;
2831 errorf(HERE, "string literal expected");
2835 } else if(symbol == sym_noalias) {
2837 DET_MOD(noalias, DM_NOALIAS);
2839 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2841 if(token.type == '(')
2845 if (token.type == ',')
2852 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2854 type_t *type = NULL;
2855 unsigned type_qualifiers = 0;
2856 unsigned type_specifiers = 0;
2859 specifiers->source_position = token.source_position;
2862 switch(token.type) {
2865 #define MATCH_STORAGE_CLASS(token, class) \
2867 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2868 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2870 specifiers->declared_storage_class = class; \
2874 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2875 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2876 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2877 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2878 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2883 add_anchor_token(')');
2884 parse_microsoft_extended_decl_modifier(specifiers);
2885 rem_anchor_token(')');
2890 switch (specifiers->declared_storage_class) {
2891 case STORAGE_CLASS_NONE:
2892 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2895 case STORAGE_CLASS_EXTERN:
2896 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2899 case STORAGE_CLASS_STATIC:
2900 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2904 errorf(HERE, "multiple storage classes in declaration specifiers");
2910 /* type qualifiers */
2911 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2913 type_qualifiers |= qualifier; \
2917 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2918 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2919 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2920 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2921 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2922 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2923 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2924 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2926 case T___extension__:
2931 /* type specifiers */
2932 #define MATCH_SPECIFIER(token, specifier, name) \
2935 if(type_specifiers & specifier) { \
2936 errorf(HERE, "multiple " name " type specifiers given"); \
2938 type_specifiers |= specifier; \
2942 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2943 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2944 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2945 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2946 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2947 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2948 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2949 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2950 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2951 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2952 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2953 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2954 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2955 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2956 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2957 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2959 case T__forceinline:
2960 /* only in microsoft mode */
2961 specifiers->decl_modifiers |= DM_FORCEINLINE;
2965 specifiers->is_inline = true;
2970 if(type_specifiers & SPECIFIER_LONG_LONG) {
2971 errorf(HERE, "multiple type specifiers given");
2972 } else if(type_specifiers & SPECIFIER_LONG) {
2973 type_specifiers |= SPECIFIER_LONG_LONG;
2975 type_specifiers |= SPECIFIER_LONG;
2980 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2982 type->compound.declaration = parse_compound_type_specifier(true);
2986 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2988 type->compound.declaration = parse_compound_type_specifier(false);
2992 type = parse_enum_specifier();
2995 type = parse_typeof();
2997 case T___builtin_va_list:
2998 type = duplicate_type(type_valist);
3002 case T___attribute__:
3003 parse_attributes(&specifiers->gnu_attributes);
3006 case T_IDENTIFIER: {
3007 /* only parse identifier if we haven't found a type yet */
3008 if(type != NULL || type_specifiers != 0)
3009 goto finish_specifiers;
3011 type_t *typedef_type = get_typedef_type(token.v.symbol);
3013 if(typedef_type == NULL)
3014 goto finish_specifiers;
3017 type = typedef_type;
3021 /* function specifier */
3023 goto finish_specifiers;
3030 atomic_type_kind_t atomic_type;
3032 /* match valid basic types */
3033 switch(type_specifiers) {
3034 case SPECIFIER_VOID:
3035 atomic_type = ATOMIC_TYPE_VOID;
3037 case SPECIFIER_CHAR:
3038 atomic_type = ATOMIC_TYPE_CHAR;
3040 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3041 atomic_type = ATOMIC_TYPE_SCHAR;
3043 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3044 atomic_type = ATOMIC_TYPE_UCHAR;
3046 case SPECIFIER_SHORT:
3047 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3048 case SPECIFIER_SHORT | SPECIFIER_INT:
3049 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3050 atomic_type = ATOMIC_TYPE_SHORT;
3052 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3053 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3054 atomic_type = ATOMIC_TYPE_USHORT;
3057 case SPECIFIER_SIGNED:
3058 case SPECIFIER_SIGNED | SPECIFIER_INT:
3059 atomic_type = ATOMIC_TYPE_INT;
3061 case SPECIFIER_UNSIGNED:
3062 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3063 atomic_type = ATOMIC_TYPE_UINT;
3065 case SPECIFIER_LONG:
3066 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3067 case SPECIFIER_LONG | SPECIFIER_INT:
3068 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3069 atomic_type = ATOMIC_TYPE_LONG;
3071 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3072 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3073 atomic_type = ATOMIC_TYPE_ULONG;
3075 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3076 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3077 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3078 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3080 atomic_type = ATOMIC_TYPE_LONGLONG;
3082 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3083 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3085 atomic_type = ATOMIC_TYPE_ULONGLONG;
3088 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3089 atomic_type = unsigned_int8_type_kind;
3092 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3093 atomic_type = unsigned_int16_type_kind;
3096 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3097 atomic_type = unsigned_int32_type_kind;
3100 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3101 atomic_type = unsigned_int64_type_kind;
3104 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3105 atomic_type = unsigned_int128_type_kind;
3108 case SPECIFIER_INT8:
3109 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3110 atomic_type = int8_type_kind;
3113 case SPECIFIER_INT16:
3114 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3115 atomic_type = int16_type_kind;
3118 case SPECIFIER_INT32:
3119 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3120 atomic_type = int32_type_kind;
3123 case SPECIFIER_INT64:
3124 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3125 atomic_type = int64_type_kind;
3128 case SPECIFIER_INT128:
3129 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3130 atomic_type = int128_type_kind;
3133 case SPECIFIER_FLOAT:
3134 atomic_type = ATOMIC_TYPE_FLOAT;
3136 case SPECIFIER_DOUBLE:
3137 atomic_type = ATOMIC_TYPE_DOUBLE;
3139 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3140 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3142 case SPECIFIER_BOOL:
3143 atomic_type = ATOMIC_TYPE_BOOL;
3145 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3146 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3147 atomic_type = ATOMIC_TYPE_FLOAT;
3149 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3150 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3151 atomic_type = ATOMIC_TYPE_DOUBLE;
3153 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3154 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3155 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3158 /* invalid specifier combination, give an error message */
3159 if(type_specifiers == 0) {
3160 if (! strict_mode) {
3161 if (warning.implicit_int) {
3162 warningf(HERE, "no type specifiers in declaration, using 'int'");
3164 atomic_type = ATOMIC_TYPE_INT;
3167 errorf(HERE, "no type specifiers given in declaration");
3169 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3170 (type_specifiers & SPECIFIER_UNSIGNED)) {
3171 errorf(HERE, "signed and unsigned specifiers gives");
3172 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3173 errorf(HERE, "only integer types can be signed or unsigned");
3175 errorf(HERE, "multiple datatypes in declaration");
3177 atomic_type = ATOMIC_TYPE_INVALID;
3180 if(type_specifiers & SPECIFIER_COMPLEX &&
3181 atomic_type != ATOMIC_TYPE_INVALID) {
3182 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3183 type->complex.akind = atomic_type;
3184 } else if(type_specifiers & SPECIFIER_IMAGINARY &&
3185 atomic_type != ATOMIC_TYPE_INVALID) {
3186 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3187 type->imaginary.akind = atomic_type;
3189 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3190 type->atomic.akind = atomic_type;
3194 if(type_specifiers != 0) {
3195 errorf(HERE, "multiple datatypes in declaration");
3199 type->base.qualifiers = type_qualifiers;
3200 /* FIXME: check type qualifiers here */
3202 type_t *result = typehash_insert(type);
3203 if(newtype && result != type) {
3207 specifiers->type = result;
3212 static type_qualifiers_t parse_type_qualifiers(void)
3214 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3217 switch(token.type) {
3218 /* type qualifiers */
3219 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3220 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3221 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3222 /* microsoft extended type modifiers */
3223 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3224 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3225 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3226 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3227 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3230 return type_qualifiers;
3235 static declaration_t *parse_identifier_list(void)
3237 declaration_t *declarations = NULL;
3238 declaration_t *last_declaration = NULL;
3240 declaration_t *const declaration = allocate_declaration_zero();
3241 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3242 declaration->source_position = token.source_position;
3243 declaration->symbol = token.v.symbol;
3246 if(last_declaration != NULL) {
3247 last_declaration->next = declaration;
3249 declarations = declaration;
3251 last_declaration = declaration;
3253 if (token.type != ',') {
3257 } while(token.type == T_IDENTIFIER);
3259 return declarations;
3262 static void semantic_parameter(declaration_t *declaration)
3264 /* TODO: improve error messages */
3266 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3267 errorf(HERE, "typedef not allowed in parameter list");
3268 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3269 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3270 errorf(HERE, "parameter may only have none or register storage class");
3273 type_t *const orig_type = declaration->type;
3274 type_t * type = skip_typeref(orig_type);
3276 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3277 * into a pointer. § 6.7.5.3 (7) */
3278 if (is_type_array(type)) {
3279 type_t *const element_type = type->array.element_type;
3281 type = make_pointer_type(element_type, type->base.qualifiers);
3283 declaration->type = type;
3286 if(is_type_incomplete(type)) {
3287 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3288 orig_type, declaration->symbol);
3292 static declaration_t *parse_parameter(void)
3294 declaration_specifiers_t specifiers;
3295 memset(&specifiers, 0, sizeof(specifiers));
3297 parse_declaration_specifiers(&specifiers);
3299 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3301 semantic_parameter(declaration);
3306 static declaration_t *parse_parameters(function_type_t *type)
3308 declaration_t *declarations = NULL;
3311 add_anchor_token(')');
3312 int saved_comma_state = save_and_reset_anchor_state(',');
3314 if(token.type == T_IDENTIFIER) {
3315 symbol_t *symbol = token.v.symbol;
3316 if(!is_typedef_symbol(symbol)) {
3317 type->kr_style_parameters = true;
3318 declarations = parse_identifier_list();
3319 goto parameters_finished;
3323 if(token.type == ')') {
3324 type->unspecified_parameters = 1;
3325 goto parameters_finished;
3327 if(token.type == T_void && look_ahead(1)->type == ')') {
3329 goto parameters_finished;
3332 declaration_t *declaration;
3333 declaration_t *last_declaration = NULL;
3334 function_parameter_t *parameter;
3335 function_parameter_t *last_parameter = NULL;
3338 switch(token.type) {
3342 goto parameters_finished;
3345 case T___extension__:
3347 declaration = parse_parameter();
3349 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3350 memset(parameter, 0, sizeof(parameter[0]));
3351 parameter->type = declaration->type;
3353 if(last_parameter != NULL) {
3354 last_declaration->next = declaration;
3355 last_parameter->next = parameter;
3357 type->parameters = parameter;
3358 declarations = declaration;
3360 last_parameter = parameter;
3361 last_declaration = declaration;
3365 goto parameters_finished;
3367 if (token.type != ',') {
3368 goto parameters_finished;
3374 parameters_finished:
3375 rem_anchor_token(')');
3378 restore_anchor_state(',', saved_comma_state);
3379 return declarations;
3382 restore_anchor_state(',', saved_comma_state);
3391 } construct_type_kind_t;
3393 typedef struct construct_type_t construct_type_t;
3394 struct construct_type_t {
3395 construct_type_kind_t kind;
3396 construct_type_t *next;
3399 typedef struct parsed_pointer_t parsed_pointer_t;
3400 struct parsed_pointer_t {
3401 construct_type_t construct_type;
3402 type_qualifiers_t type_qualifiers;
3405 typedef struct construct_function_type_t construct_function_type_t;
3406 struct construct_function_type_t {
3407 construct_type_t construct_type;
3408 type_t *function_type;
3411 typedef struct parsed_array_t parsed_array_t;
3412 struct parsed_array_t {
3413 construct_type_t construct_type;
3414 type_qualifiers_t type_qualifiers;
3420 typedef struct construct_base_type_t construct_base_type_t;
3421 struct construct_base_type_t {
3422 construct_type_t construct_type;
3426 static construct_type_t *parse_pointer_declarator(void)
3430 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3431 memset(pointer, 0, sizeof(pointer[0]));
3432 pointer->construct_type.kind = CONSTRUCT_POINTER;
3433 pointer->type_qualifiers = parse_type_qualifiers();
3435 return (construct_type_t*) pointer;
3438 static construct_type_t *parse_array_declarator(void)
3441 add_anchor_token(']');
3443 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3444 memset(array, 0, sizeof(array[0]));
3445 array->construct_type.kind = CONSTRUCT_ARRAY;
3447 if(token.type == T_static) {
3448 array->is_static = true;
3452 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3453 if(type_qualifiers != 0) {
3454 if(token.type == T_static) {
3455 array->is_static = true;
3459 array->type_qualifiers = type_qualifiers;
3461 if(token.type == '*' && look_ahead(1)->type == ']') {
3462 array->is_variable = true;
3464 } else if(token.type != ']') {
3465 array->size = parse_assignment_expression();
3468 rem_anchor_token(']');
3471 return (construct_type_t*) array;
3476 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3479 if(declaration != NULL) {
3480 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3482 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3485 declaration_t *parameters = parse_parameters(&type->function);
3486 if(declaration != NULL) {
3487 declaration->scope.declarations = parameters;
3490 construct_function_type_t *construct_function_type =
3491 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3492 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3493 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3494 construct_function_type->function_type = type;
3496 return (construct_type_t*) construct_function_type;
3499 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3500 bool may_be_abstract)
3502 /* construct a single linked list of construct_type_t's which describe
3503 * how to construct the final declarator type */
3504 construct_type_t *first = NULL;
3505 construct_type_t *last = NULL;
3506 gnu_attribute_t *attributes = NULL;
3509 while(token.type == '*') {
3510 construct_type_t *type = parse_pointer_declarator();
3521 /* TODO: find out if this is correct */
3522 parse_attributes(&attributes);
3524 construct_type_t *inner_types = NULL;
3526 switch(token.type) {
3528 if(declaration == NULL) {
3529 errorf(HERE, "no identifier expected in typename");
3531 declaration->symbol = token.v.symbol;
3532 declaration->source_position = token.source_position;
3538 add_anchor_token(')');
3539 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3540 rem_anchor_token(')');
3546 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3547 /* avoid a loop in the outermost scope, because eat_statement doesn't
3549 if(token.type == '}' && current_function == NULL) {
3557 construct_type_t *p = last;
3560 construct_type_t *type;
3561 switch(token.type) {
3563 type = parse_function_declarator(declaration);
3566 type = parse_array_declarator();
3569 goto declarator_finished;
3572 /* insert in the middle of the list (behind p) */
3574 type->next = p->next;
3585 declarator_finished:
3586 parse_attributes(&attributes);
3588 /* append inner_types at the end of the list, we don't to set last anymore
3589 * as it's not needed anymore */
3591 assert(first == NULL);
3592 first = inner_types;
3594 last->next = inner_types;
3602 static type_t *construct_declarator_type(construct_type_t *construct_list,
3605 construct_type_t *iter = construct_list;
3606 for( ; iter != NULL; iter = iter->next) {
3607 switch(iter->kind) {
3608 case CONSTRUCT_INVALID:
3609 internal_errorf(HERE, "invalid type construction found");
3610 case CONSTRUCT_FUNCTION: {
3611 construct_function_type_t *construct_function_type
3612 = (construct_function_type_t*) iter;
3614 type_t *function_type = construct_function_type->function_type;
3616 function_type->function.return_type = type;
3618 type_t *skipped_return_type = skip_typeref(type);
3619 if (is_type_function(skipped_return_type)) {
3620 errorf(HERE, "function returning function is not allowed");
3621 type = type_error_type;
3622 } else if (is_type_array(skipped_return_type)) {
3623 errorf(HERE, "function returning array is not allowed");
3624 type = type_error_type;
3626 type = function_type;
3631 case CONSTRUCT_POINTER: {
3632 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3633 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3634 pointer_type->pointer.points_to = type;
3635 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3637 type = pointer_type;
3641 case CONSTRUCT_ARRAY: {
3642 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3643 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3645 expression_t *size_expression = parsed_array->size;
3646 if(size_expression != NULL) {
3648 = create_implicit_cast(size_expression, type_size_t);
3651 array_type->base.qualifiers = parsed_array->type_qualifiers;
3652 array_type->array.element_type = type;
3653 array_type->array.is_static = parsed_array->is_static;
3654 array_type->array.is_variable = parsed_array->is_variable;
3655 array_type->array.size_expression = size_expression;
3657 if(size_expression != NULL) {
3658 if(is_constant_expression(size_expression)) {
3659 array_type->array.size_constant = true;
3660 array_type->array.size
3661 = fold_constant(size_expression);
3663 array_type->array.is_vla = true;
3667 type_t *skipped_type = skip_typeref(type);
3668 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3669 errorf(HERE, "array of void is not allowed");
3670 type = type_error_type;
3678 type_t *hashed_type = typehash_insert(type);
3679 if(hashed_type != type) {
3680 /* the function type was constructed earlier freeing it here will
3681 * destroy other types... */
3682 if(iter->kind != CONSTRUCT_FUNCTION) {
3692 static declaration_t *parse_declarator(
3693 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3695 declaration_t *const declaration = allocate_declaration_zero();
3696 declaration->declared_storage_class = specifiers->declared_storage_class;
3697 declaration->decl_modifiers = specifiers->decl_modifiers;
3698 declaration->deprecated = specifiers->deprecated;
3699 declaration->deprecated_string = specifiers->deprecated_string;
3700 declaration->get_property_sym = specifiers->get_property_sym;
3701 declaration->put_property_sym = specifiers->put_property_sym;
3702 declaration->is_inline = specifiers->is_inline;
3704 declaration->storage_class = specifiers->declared_storage_class;
3705 if(declaration->storage_class == STORAGE_CLASS_NONE
3706 && scope != global_scope) {
3707 declaration->storage_class = STORAGE_CLASS_AUTO;
3710 if(specifiers->alignment != 0) {
3711 /* TODO: add checks here */
3712 declaration->alignment = specifiers->alignment;
3715 construct_type_t *construct_type
3716 = parse_inner_declarator(declaration, may_be_abstract);
3717 type_t *const type = specifiers->type;
3718 declaration->type = construct_declarator_type(construct_type, type);
3720 if(construct_type != NULL) {
3721 obstack_free(&temp_obst, construct_type);
3727 static type_t *parse_abstract_declarator(type_t *base_type)
3729 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3731 type_t *result = construct_declarator_type(construct_type, base_type);
3732 if(construct_type != NULL) {
3733 obstack_free(&temp_obst, construct_type);
3739 static declaration_t *append_declaration(declaration_t* const declaration)
3741 if (last_declaration != NULL) {
3742 last_declaration->next = declaration;
3744 scope->declarations = declaration;
3746 last_declaration = declaration;
3751 * Check if the declaration of main is suspicious. main should be a
3752 * function with external linkage, returning int, taking either zero
3753 * arguments, two, or three arguments of appropriate types, ie.
3755 * int main([ int argc, char **argv [, char **env ] ]).
3757 * @param decl the declaration to check
3758 * @param type the function type of the declaration
3760 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3762 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3763 warningf(&decl->source_position,
3764 "'main' is normally a non-static function");
3766 if (skip_typeref(func_type->return_type) != type_int) {
3767 warningf(&decl->source_position,
3768 "return type of 'main' should be 'int', but is '%T'",
3769 func_type->return_type);
3771 const function_parameter_t *parm = func_type->parameters;
3773 type_t *const first_type = parm->type;
3774 if (!types_compatible(skip_typeref(first_type), type_int)) {
3775 warningf(&decl->source_position,
3776 "first argument of 'main' should be 'int', but is '%T'", first_type);
3780 type_t *const second_type = parm->type;
3781 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3782 warningf(&decl->source_position,
3783 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3787 type_t *const third_type = parm->type;
3788 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3789 warningf(&decl->source_position,
3790 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3794 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3798 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3804 * Check if a symbol is the equal to "main".
3806 static bool is_sym_main(const symbol_t *const sym)
3808 return strcmp(sym->string, "main") == 0;
3811 static declaration_t *internal_record_declaration(
3812 declaration_t *const declaration,
3813 const bool is_function_definition)
3815 const symbol_t *const symbol = declaration->symbol;
3816 const namespace_t namespc = (namespace_t)declaration->namespc;
3818 type_t *const orig_type = declaration->type;
3819 type_t *const type = skip_typeref(orig_type);
3820 if (is_type_function(type) &&
3821 type->function.unspecified_parameters &&
3822 warning.strict_prototypes) {
3823 warningf(&declaration->source_position,
3824 "function declaration '%#T' is not a prototype",
3825 orig_type, declaration->symbol);
3828 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3829 check_type_of_main(declaration, &type->function);
3832 assert(declaration->symbol != NULL);
3833 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3835 assert(declaration != previous_declaration);
3836 if (previous_declaration != NULL) {
3837 if (previous_declaration->parent_scope == scope) {
3838 /* can happen for K&R style declarations */
3839 if(previous_declaration->type == NULL) {
3840 previous_declaration->type = declaration->type;
3843 const type_t *prev_type = skip_typeref(previous_declaration->type);
3844 if (!types_compatible(type, prev_type)) {
3845 errorf(&declaration->source_position,
3846 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3847 orig_type, symbol, previous_declaration->type, symbol,
3848 &previous_declaration->source_position);
3850 unsigned old_storage_class = previous_declaration->storage_class;
3851 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3852 errorf(&declaration->source_position,
3853 "redeclaration of enum entry '%Y' (declared %P)",
3854 symbol, &previous_declaration->source_position);
3855 return previous_declaration;
3858 unsigned new_storage_class = declaration->storage_class;
3860 if(is_type_incomplete(prev_type)) {
3861 previous_declaration->type = type;
3865 /* pretend no storage class means extern for function
3866 * declarations (except if the previous declaration is neither
3867 * none nor extern) */
3868 if (is_type_function(type)) {
3869 switch (old_storage_class) {
3870 case STORAGE_CLASS_NONE:
3871 old_storage_class = STORAGE_CLASS_EXTERN;
3873 case STORAGE_CLASS_EXTERN:
3874 if (is_function_definition) {
3875 if (warning.missing_prototypes &&
3876 prev_type->function.unspecified_parameters &&
3877 !is_sym_main(symbol)) {
3878 warningf(&declaration->source_position,
3879 "no previous prototype for '%#T'",
3882 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3883 new_storage_class = STORAGE_CLASS_EXTERN;
3891 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3892 new_storage_class == STORAGE_CLASS_EXTERN) {
3893 warn_redundant_declaration:
3894 if (warning.redundant_decls) {
3895 warningf(&declaration->source_position,
3896 "redundant declaration for '%Y' (declared %P)",
3897 symbol, &previous_declaration->source_position);
3899 } else if (current_function == NULL) {
3900 if (old_storage_class != STORAGE_CLASS_STATIC &&
3901 new_storage_class == STORAGE_CLASS_STATIC) {
3902 errorf(&declaration->source_position,
3903 "static declaration of '%Y' follows non-static declaration (declared %P)",
3904 symbol, &previous_declaration->source_position);
3906 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3907 goto warn_redundant_declaration;
3909 if (new_storage_class == STORAGE_CLASS_NONE) {
3910 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3911 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3915 if (old_storage_class == new_storage_class) {
3916 errorf(&declaration->source_position,
3917 "redeclaration of '%Y' (declared %P)",
3918 symbol, &previous_declaration->source_position);
3920 errorf(&declaration->source_position,
3921 "redeclaration of '%Y' with different linkage (declared %P)",
3922 symbol, &previous_declaration->source_position);
3926 return previous_declaration;
3928 } else if (is_function_definition) {
3929 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3930 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3931 warningf(&declaration->source_position,
3932 "no previous prototype for '%#T'", orig_type, symbol);
3933 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3934 warningf(&declaration->source_position,
3935 "no previous declaration for '%#T'", orig_type,
3939 } else if (warning.missing_declarations &&
3940 scope == global_scope &&
3941 !is_type_function(type) && (
3942 declaration->storage_class == STORAGE_CLASS_NONE ||
3943 declaration->storage_class == STORAGE_CLASS_THREAD
3945 warningf(&declaration->source_position,
3946 "no previous declaration for '%#T'", orig_type, symbol);
3949 assert(declaration->parent_scope == NULL);
3950 assert(scope != NULL);
3952 declaration->parent_scope = scope;
3954 environment_push(declaration);
3955 return append_declaration(declaration);
3958 static declaration_t *record_declaration(declaration_t *declaration)
3960 return internal_record_declaration(declaration, false);
3963 static declaration_t *record_function_definition(declaration_t *declaration)
3965 return internal_record_declaration(declaration, true);
3968 static void parser_error_multiple_definition(declaration_t *declaration,
3969 const source_position_t *source_position)
3971 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3972 declaration->symbol, &declaration->source_position);
3975 static bool is_declaration_specifier(const token_t *token,
3976 bool only_type_specifiers)
3978 switch(token->type) {
3982 return is_typedef_symbol(token->v.symbol);
3984 case T___extension__:
3987 return !only_type_specifiers;
3994 static void parse_init_declarator_rest(declaration_t *declaration)
3998 type_t *orig_type = declaration->type;
3999 type_t *type = skip_typeref(orig_type);
4001 if(declaration->init.initializer != NULL) {
4002 parser_error_multiple_definition(declaration, HERE);
4005 bool must_be_constant = false;
4006 if(declaration->storage_class == STORAGE_CLASS_STATIC
4007 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4008 || declaration->parent_scope == global_scope) {
4009 must_be_constant = true;
4012 parse_initializer_env_t env;
4013 env.type = orig_type;
4014 env.must_be_constant = must_be_constant;
4015 env.declaration = declaration;
4017 initializer_t *initializer = parse_initializer(&env);
4019 if(env.type != orig_type) {
4020 orig_type = env.type;
4021 type = skip_typeref(orig_type);
4022 declaration->type = env.type;
4025 if(is_type_function(type)) {
4026 errorf(&declaration->source_position,
4027 "initializers not allowed for function types at declator '%Y' (type '%T')",
4028 declaration->symbol, orig_type);
4030 declaration->init.initializer = initializer;
4034 /* parse rest of a declaration without any declarator */
4035 static void parse_anonymous_declaration_rest(
4036 const declaration_specifiers_t *specifiers,
4037 parsed_declaration_func finished_declaration)
4041 declaration_t *const declaration = allocate_declaration_zero();
4042 declaration->type = specifiers->type;
4043 declaration->declared_storage_class = specifiers->declared_storage_class;
4044 declaration->source_position = specifiers->source_position;
4045 declaration->decl_modifiers = specifiers->decl_modifiers;
4047 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4048 warningf(&declaration->source_position,
4049 "useless storage class in empty declaration");
4051 declaration->storage_class = STORAGE_CLASS_NONE;
4053 type_t *type = declaration->type;
4054 switch (type->kind) {
4055 case TYPE_COMPOUND_STRUCT:
4056 case TYPE_COMPOUND_UNION: {
4057 if (type->compound.declaration->symbol == NULL) {
4058 warningf(&declaration->source_position,
4059 "unnamed struct/union that defines no instances");
4068 warningf(&declaration->source_position, "empty declaration");
4072 finished_declaration(declaration);
4075 static void parse_declaration_rest(declaration_t *ndeclaration,
4076 const declaration_specifiers_t *specifiers,
4077 parsed_declaration_func finished_declaration)
4079 add_anchor_token(';');
4080 add_anchor_token('=');
4081 add_anchor_token(',');
4083 declaration_t *declaration = finished_declaration(ndeclaration);
4085 type_t *orig_type = declaration->type;
4086 type_t *type = skip_typeref(orig_type);
4088 if (type->kind != TYPE_FUNCTION &&
4089 declaration->is_inline &&
4090 is_type_valid(type)) {
4091 warningf(&declaration->source_position,
4092 "variable '%Y' declared 'inline'\n", declaration->symbol);
4095 if(token.type == '=') {
4096 parse_init_declarator_rest(declaration);
4099 if(token.type != ',')
4103 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4108 rem_anchor_token(';');
4109 rem_anchor_token('=');
4110 rem_anchor_token(',');
4113 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4115 symbol_t *symbol = declaration->symbol;
4116 if(symbol == NULL) {
4117 errorf(HERE, "anonymous declaration not valid as function parameter");
4120 namespace_t namespc = (namespace_t) declaration->namespc;
4121 if(namespc != NAMESPACE_NORMAL) {
4122 return record_declaration(declaration);
4125 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4126 if(previous_declaration == NULL ||
4127 previous_declaration->parent_scope != scope) {
4128 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4133 if(previous_declaration->type == NULL) {
4134 previous_declaration->type = declaration->type;
4135 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4136 previous_declaration->storage_class = declaration->storage_class;
4137 previous_declaration->parent_scope = scope;
4138 return previous_declaration;
4140 return record_declaration(declaration);
4144 static void parse_declaration(parsed_declaration_func finished_declaration)
4146 declaration_specifiers_t specifiers;
4147 memset(&specifiers, 0, sizeof(specifiers));
4148 parse_declaration_specifiers(&specifiers);
4150 if(token.type == ';') {
4151 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4153 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4154 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4158 static void parse_kr_declaration_list(declaration_t *declaration)
4160 type_t *type = skip_typeref(declaration->type);
4161 if (!is_type_function(type))
4164 if (!type->function.kr_style_parameters)
4167 /* push function parameters */
4168 int top = environment_top();
4169 scope_t *last_scope = scope;
4170 set_scope(&declaration->scope);
4172 declaration_t *parameter = declaration->scope.declarations;
4173 for( ; parameter != NULL; parameter = parameter->next) {
4174 assert(parameter->parent_scope == NULL);
4175 parameter->parent_scope = scope;
4176 environment_push(parameter);
4179 /* parse declaration list */
4180 while(is_declaration_specifier(&token, false)) {
4181 parse_declaration(finished_kr_declaration);
4184 /* pop function parameters */
4185 assert(scope == &declaration->scope);
4186 set_scope(last_scope);
4187 environment_pop_to(top);
4189 /* update function type */
4190 type_t *new_type = duplicate_type(type);
4191 new_type->function.kr_style_parameters = false;
4193 function_parameter_t *parameters = NULL;
4194 function_parameter_t *last_parameter = NULL;
4196 declaration_t *parameter_declaration = declaration->scope.declarations;
4197 for( ; parameter_declaration != NULL;
4198 parameter_declaration = parameter_declaration->next) {
4199 type_t *parameter_type = parameter_declaration->type;
4200 if(parameter_type == NULL) {
4202 errorf(HERE, "no type specified for function parameter '%Y'",
4203 parameter_declaration->symbol);
4205 if (warning.implicit_int) {
4206 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4207 parameter_declaration->symbol);
4209 parameter_type = type_int;
4210 parameter_declaration->type = parameter_type;
4214 semantic_parameter(parameter_declaration);
4215 parameter_type = parameter_declaration->type;
4217 function_parameter_t *function_parameter
4218 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4219 memset(function_parameter, 0, sizeof(function_parameter[0]));
4221 function_parameter->type = parameter_type;
4222 if(last_parameter != NULL) {
4223 last_parameter->next = function_parameter;
4225 parameters = function_parameter;
4227 last_parameter = function_parameter;
4229 new_type->function.parameters = parameters;
4231 type = typehash_insert(new_type);
4232 if(type != new_type) {
4233 obstack_free(type_obst, new_type);
4236 declaration->type = type;
4239 static bool first_err = true;
4242 * When called with first_err set, prints the name of the current function,
4245 static void print_in_function(void) {
4248 diagnosticf("%s: In function '%Y':\n",
4249 current_function->source_position.input_name,
4250 current_function->symbol);
4255 * Check if all labels are defined in the current function.
4256 * Check if all labels are used in the current function.
4258 static void check_labels(void)
4260 for (const goto_statement_t *goto_statement = goto_first;
4261 goto_statement != NULL;
4262 goto_statement = goto_statement->next) {
4263 declaration_t *label = goto_statement->label;
4266 if (label->source_position.input_name == NULL) {
4267 print_in_function();
4268 errorf(&goto_statement->base.source_position,
4269 "label '%Y' used but not defined", label->symbol);
4272 goto_first = goto_last = NULL;
4274 if (warning.unused_label) {
4275 for (const label_statement_t *label_statement = label_first;
4276 label_statement != NULL;
4277 label_statement = label_statement->next) {
4278 const declaration_t *label = label_statement->label;
4280 if (! label->used) {
4281 print_in_function();
4282 warningf(&label_statement->base.source_position,
4283 "label '%Y' defined but not used", label->symbol);
4287 label_first = label_last = NULL;
4291 * Check declarations of current_function for unused entities.
4293 static void check_declarations(void)
4295 if (warning.unused_parameter) {
4296 const scope_t *scope = ¤t_function->scope;
4298 const declaration_t *parameter = scope->declarations;
4299 for (; parameter != NULL; parameter = parameter->next) {
4300 if (! parameter->used) {
4301 print_in_function();
4302 warningf(¶meter->source_position,
4303 "unused parameter '%Y'", parameter->symbol);
4307 if (warning.unused_variable) {
4311 static void parse_external_declaration(void)
4313 /* function-definitions and declarations both start with declaration
4315 declaration_specifiers_t specifiers;
4316 memset(&specifiers, 0, sizeof(specifiers));
4318 add_anchor_token(';');
4319 parse_declaration_specifiers(&specifiers);
4320 rem_anchor_token(';');
4322 /* must be a declaration */
4323 if(token.type == ';') {
4324 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4328 add_anchor_token(',');
4329 add_anchor_token('=');
4330 rem_anchor_token(';');
4332 /* declarator is common to both function-definitions and declarations */
4333 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4335 rem_anchor_token(',');
4336 rem_anchor_token('=');
4337 rem_anchor_token(';');
4339 /* must be a declaration */
4340 if(token.type == ',' || token.type == '=' || token.type == ';') {
4341 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4345 /* must be a function definition */
4346 parse_kr_declaration_list(ndeclaration);
4348 if(token.type != '{') {
4349 parse_error_expected("while parsing function definition", '{', NULL);
4350 eat_until_matching_token(';');
4354 type_t *type = ndeclaration->type;
4356 /* note that we don't skip typerefs: the standard doesn't allow them here
4357 * (so we can't use is_type_function here) */
4358 if(type->kind != TYPE_FUNCTION) {
4359 if (is_type_valid(type)) {
4360 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4361 type, ndeclaration->symbol);
4367 /* § 6.7.5.3 (14) a function definition with () means no
4368 * parameters (and not unspecified parameters) */
4369 if(type->function.unspecified_parameters) {
4370 type_t *duplicate = duplicate_type(type);
4371 duplicate->function.unspecified_parameters = false;
4373 type = typehash_insert(duplicate);
4374 if(type != duplicate) {
4375 obstack_free(type_obst, duplicate);
4377 ndeclaration->type = type;
4380 declaration_t *const declaration = record_function_definition(ndeclaration);
4381 if(ndeclaration != declaration) {
4382 declaration->scope = ndeclaration->scope;
4384 type = skip_typeref(declaration->type);
4386 /* push function parameters and switch scope */
4387 int top = environment_top();
4388 scope_t *last_scope = scope;
4389 set_scope(&declaration->scope);
4391 declaration_t *parameter = declaration->scope.declarations;
4392 for( ; parameter != NULL; parameter = parameter->next) {
4393 if(parameter->parent_scope == &ndeclaration->scope) {
4394 parameter->parent_scope = scope;
4396 assert(parameter->parent_scope == NULL
4397 || parameter->parent_scope == scope);
4398 parameter->parent_scope = scope;
4399 environment_push(parameter);
4402 if(declaration->init.statement != NULL) {
4403 parser_error_multiple_definition(declaration, HERE);
4405 goto end_of_parse_external_declaration;
4407 /* parse function body */
4408 int label_stack_top = label_top();
4409 declaration_t *old_current_function = current_function;
4410 current_function = declaration;
4412 declaration->init.statement = parse_compound_statement(false);
4415 check_declarations();
4417 assert(current_function == declaration);
4418 current_function = old_current_function;
4419 label_pop_to(label_stack_top);
4422 end_of_parse_external_declaration:
4423 assert(scope == &declaration->scope);
4424 set_scope(last_scope);
4425 environment_pop_to(top);
4428 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4429 source_position_t *source_position)
4431 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4433 type->bitfield.base_type = base_type;
4434 type->bitfield.size = size;
4439 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4442 declaration_t *iter = compound_declaration->scope.declarations;
4443 for( ; iter != NULL; iter = iter->next) {
4444 if(iter->namespc != NAMESPACE_NORMAL)
4447 if(iter->symbol == NULL) {
4448 type_t *type = skip_typeref(iter->type);
4449 if(is_type_compound(type)) {
4450 declaration_t *result
4451 = find_compound_entry(type->compound.declaration, symbol);
4458 if(iter->symbol == symbol) {
4466 static void parse_compound_declarators(declaration_t *struct_declaration,
4467 const declaration_specifiers_t *specifiers)
4469 declaration_t *last_declaration = struct_declaration->scope.declarations;
4470 if(last_declaration != NULL) {
4471 while(last_declaration->next != NULL) {
4472 last_declaration = last_declaration->next;
4477 declaration_t *declaration;
4479 if(token.type == ':') {
4480 source_position_t source_position = *HERE;
4483 type_t *base_type = specifiers->type;
4484 expression_t *size = parse_constant_expression();
4486 if(!is_type_integer(skip_typeref(base_type))) {
4487 errorf(HERE, "bitfield base type '%T' is not an integer type",
4491 type_t *type = make_bitfield_type(base_type, size, &source_position);
4493 declaration = allocate_declaration_zero();
4494 declaration->namespc = NAMESPACE_NORMAL;
4495 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4496 declaration->storage_class = STORAGE_CLASS_NONE;
4497 declaration->source_position = source_position;
4498 declaration->decl_modifiers = specifiers->decl_modifiers;
4499 declaration->type = type;
4501 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4503 type_t *orig_type = declaration->type;
4504 type_t *type = skip_typeref(orig_type);
4506 if(token.type == ':') {
4507 source_position_t source_position = *HERE;
4509 expression_t *size = parse_constant_expression();
4511 if(!is_type_integer(type)) {
4512 errorf(HERE, "bitfield base type '%T' is not an "
4513 "integer type", orig_type);
4516 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4517 declaration->type = bitfield_type;
4519 /* TODO we ignore arrays for now... what is missing is a check
4520 * that they're at the end of the struct */
4521 if(is_type_incomplete(type) && !is_type_array(type)) {
4523 "compound member '%Y' has incomplete type '%T'",
4524 declaration->symbol, orig_type);
4525 } else if(is_type_function(type)) {
4526 errorf(HERE, "compound member '%Y' must not have function "
4527 "type '%T'", declaration->symbol, orig_type);
4532 /* make sure we don't define a symbol multiple times */
4533 symbol_t *symbol = declaration->symbol;
4534 if(symbol != NULL) {
4535 declaration_t *prev_decl
4536 = find_compound_entry(struct_declaration, symbol);
4538 if(prev_decl != NULL) {
4539 assert(prev_decl->symbol == symbol);
4540 errorf(&declaration->source_position,
4541 "multiple declarations of symbol '%Y' (declared %P)",
4542 symbol, &prev_decl->source_position);
4546 /* append declaration */
4547 if(last_declaration != NULL) {
4548 last_declaration->next = declaration;
4550 struct_declaration->scope.declarations = declaration;
4552 last_declaration = declaration;
4554 if(token.type != ',')
4564 static void parse_compound_type_entries(declaration_t *compound_declaration)
4567 add_anchor_token('}');
4569 while(token.type != '}' && token.type != T_EOF) {
4570 declaration_specifiers_t specifiers;
4571 memset(&specifiers, 0, sizeof(specifiers));
4572 parse_declaration_specifiers(&specifiers);
4574 parse_compound_declarators(compound_declaration, &specifiers);
4576 rem_anchor_token('}');
4578 if(token.type == T_EOF) {
4579 errorf(HERE, "EOF while parsing struct");
4584 static type_t *parse_typename(void)
4586 declaration_specifiers_t specifiers;
4587 memset(&specifiers, 0, sizeof(specifiers));
4588 parse_declaration_specifiers(&specifiers);
4589 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4590 /* TODO: improve error message, user does probably not know what a
4591 * storage class is...
4593 errorf(HERE, "typename may not have a storage class");
4596 type_t *result = parse_abstract_declarator(specifiers.type);
4604 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4605 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4606 expression_t *left);
4608 typedef struct expression_parser_function_t expression_parser_function_t;
4609 struct expression_parser_function_t {
4610 unsigned precedence;
4611 parse_expression_function parser;
4612 unsigned infix_precedence;
4613 parse_expression_infix_function infix_parser;
4616 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4619 * Prints an error message if an expression was expected but not read
4621 static expression_t *expected_expression_error(void)
4623 /* skip the error message if the error token was read */
4624 if (token.type != T_ERROR) {
4625 errorf(HERE, "expected expression, got token '%K'", &token);
4629 return create_invalid_expression();
4633 * Parse a string constant.
4635 static expression_t *parse_string_const(void)
4638 if (token.type == T_STRING_LITERAL) {
4639 string_t res = token.v.string;
4641 while (token.type == T_STRING_LITERAL) {
4642 res = concat_strings(&res, &token.v.string);
4645 if (token.type != T_WIDE_STRING_LITERAL) {
4646 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4647 /* note: that we use type_char_ptr here, which is already the
4648 * automatic converted type. revert_automatic_type_conversion
4649 * will construct the array type */
4650 cnst->base.type = type_char_ptr;
4651 cnst->string.value = res;
4655 wres = concat_string_wide_string(&res, &token.v.wide_string);
4657 wres = token.v.wide_string;
4662 switch (token.type) {
4663 case T_WIDE_STRING_LITERAL:
4664 wres = concat_wide_strings(&wres, &token.v.wide_string);
4667 case T_STRING_LITERAL:
4668 wres = concat_wide_string_string(&wres, &token.v.string);
4672 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4673 cnst->base.type = type_wchar_t_ptr;
4674 cnst->wide_string.value = wres;
4683 * Parse an integer constant.
4685 static expression_t *parse_int_const(void)
4687 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4688 cnst->base.source_position = *HERE;
4689 cnst->base.type = token.datatype;
4690 cnst->conste.v.int_value = token.v.intvalue;
4698 * Parse a character constant.
4700 static expression_t *parse_character_constant(void)
4702 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4704 cnst->base.source_position = *HERE;
4705 cnst->base.type = token.datatype;
4706 cnst->conste.v.character = token.v.string;
4708 if (cnst->conste.v.character.size != 1) {
4709 if (warning.multichar && (c_mode & _GNUC)) {
4711 warningf(HERE, "multi-character character constant");
4713 errorf(HERE, "more than 1 characters in character constant");
4722 * Parse a wide character constant.
4724 static expression_t *parse_wide_character_constant(void)
4726 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4728 cnst->base.source_position = *HERE;
4729 cnst->base.type = token.datatype;
4730 cnst->conste.v.wide_character = token.v.wide_string;
4732 if (cnst->conste.v.wide_character.size != 1) {
4733 if (warning.multichar && (c_mode & _GNUC)) {
4735 warningf(HERE, "multi-character character constant");
4737 errorf(HERE, "more than 1 characters in character constant");
4746 * Parse a float constant.
4748 static expression_t *parse_float_const(void)
4750 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4751 cnst->base.type = token.datatype;
4752 cnst->conste.v.float_value = token.v.floatvalue;
4759 static declaration_t *create_implicit_function(symbol_t *symbol,
4760 const source_position_t *source_position)
4762 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4763 ntype->function.return_type = type_int;
4764 ntype->function.unspecified_parameters = true;
4766 type_t *type = typehash_insert(ntype);
4771 declaration_t *const declaration = allocate_declaration_zero();
4772 declaration->storage_class = STORAGE_CLASS_EXTERN;
4773 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4774 declaration->type = type;
4775 declaration->symbol = symbol;
4776 declaration->source_position = *source_position;
4777 declaration->parent_scope = global_scope;
4779 scope_t *old_scope = scope;
4780 set_scope(global_scope);
4782 environment_push(declaration);
4783 /* prepends the declaration to the global declarations list */
4784 declaration->next = scope->declarations;
4785 scope->declarations = declaration;
4787 assert(scope == global_scope);
4788 set_scope(old_scope);
4794 * Creates a return_type (func)(argument_type) function type if not
4797 * @param return_type the return type
4798 * @param argument_type the argument type
4800 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4802 function_parameter_t *parameter
4803 = obstack_alloc(type_obst, sizeof(parameter[0]));
4804 memset(parameter, 0, sizeof(parameter[0]));
4805 parameter->type = argument_type;
4807 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4808 type->function.return_type = return_type;
4809 type->function.parameters = parameter;
4811 type_t *result = typehash_insert(type);
4812 if(result != type) {
4820 * Creates a function type for some function like builtins.
4822 * @param symbol the symbol describing the builtin
4824 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4826 switch(symbol->ID) {
4827 case T___builtin_alloca:
4828 return make_function_1_type(type_void_ptr, type_size_t);
4829 case T___builtin_nan:
4830 return make_function_1_type(type_double, type_char_ptr);
4831 case T___builtin_nanf:
4832 return make_function_1_type(type_float, type_char_ptr);
4833 case T___builtin_nand:
4834 return make_function_1_type(type_long_double, type_char_ptr);
4835 case T___builtin_va_end:
4836 return make_function_1_type(type_void, type_valist);
4838 internal_errorf(HERE, "not implemented builtin symbol found");
4843 * Performs automatic type cast as described in § 6.3.2.1.
4845 * @param orig_type the original type
4847 static type_t *automatic_type_conversion(type_t *orig_type)
4849 type_t *type = skip_typeref(orig_type);
4850 if(is_type_array(type)) {
4851 array_type_t *array_type = &type->array;
4852 type_t *element_type = array_type->element_type;
4853 unsigned qualifiers = array_type->base.qualifiers;
4855 return make_pointer_type(element_type, qualifiers);
4858 if(is_type_function(type)) {
4859 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4866 * reverts the automatic casts of array to pointer types and function
4867 * to function-pointer types as defined § 6.3.2.1
4869 type_t *revert_automatic_type_conversion(const expression_t *expression)
4871 switch (expression->kind) {
4872 case EXPR_REFERENCE: return expression->reference.declaration->type;
4873 case EXPR_SELECT: return expression->select.compound_entry->type;
4875 case EXPR_UNARY_DEREFERENCE: {
4876 const expression_t *const value = expression->unary.value;
4877 type_t *const type = skip_typeref(value->base.type);
4878 assert(is_type_pointer(type));
4879 return type->pointer.points_to;
4882 case EXPR_BUILTIN_SYMBOL:
4883 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4885 case EXPR_ARRAY_ACCESS: {
4886 const expression_t *array_ref = expression->array_access.array_ref;
4887 type_t *type_left = skip_typeref(array_ref->base.type);
4888 if (!is_type_valid(type_left))
4890 assert(is_type_pointer(type_left));
4891 return type_left->pointer.points_to;
4894 case EXPR_STRING_LITERAL: {
4895 size_t size = expression->string.value.size;
4896 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4899 case EXPR_WIDE_STRING_LITERAL: {
4900 size_t size = expression->wide_string.value.size;
4901 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4904 case EXPR_COMPOUND_LITERAL:
4905 return expression->compound_literal.type;
4910 return expression->base.type;
4913 static expression_t *parse_reference(void)
4915 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4917 reference_expression_t *ref = &expression->reference;
4918 symbol_t *const symbol = token.v.symbol;
4920 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
4922 source_position_t source_position = token.source_position;
4925 if(declaration == NULL) {
4926 if (! strict_mode && token.type == '(') {
4927 /* an implicitly defined function */
4928 if (warning.implicit_function_declaration) {
4929 warningf(HERE, "implicit declaration of function '%Y'",
4933 declaration = create_implicit_function(symbol,
4936 errorf(HERE, "unknown symbol '%Y' found.", symbol);
4937 return create_invalid_expression();
4941 type_t *type = declaration->type;
4943 /* we always do the auto-type conversions; the & and sizeof parser contains
4944 * code to revert this! */
4945 type = automatic_type_conversion(type);
4947 ref->declaration = declaration;
4948 ref->base.type = type;
4950 /* this declaration is used */
4951 declaration->used = true;
4953 /* check for deprecated functions */
4954 if(declaration->deprecated != 0) {
4955 const char *prefix = "";
4956 if (is_type_function(declaration->type))
4957 prefix = "function ";
4959 if (declaration->deprecated_string != NULL) {
4960 warningf(&source_position,
4961 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4962 declaration->deprecated_string);
4964 warningf(&source_position,
4965 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4972 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4976 /* TODO check if explicit cast is allowed and issue warnings/errors */
4979 static expression_t *parse_compound_literal(type_t *type)
4981 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4983 parse_initializer_env_t env;
4985 env.declaration = NULL;
4986 env.must_be_constant = false;
4987 initializer_t *initializer = parse_initializer(&env);
4990 expression->compound_literal.initializer = initializer;
4991 expression->compound_literal.type = type;
4992 expression->base.type = automatic_type_conversion(type);
4998 * Parse a cast expression.
5000 static expression_t *parse_cast(void)
5002 source_position_t source_position = token.source_position;
5004 type_t *type = parse_typename();
5006 /* matching add_anchor_token() is at call site */
5007 rem_anchor_token(')');
5010 if(token.type == '{') {
5011 return parse_compound_literal(type);
5014 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5015 cast->base.source_position = source_position;
5017 expression_t *value = parse_sub_expression(20);
5019 check_cast_allowed(value, type);
5021 cast->base.type = type;
5022 cast->unary.value = value;
5026 return create_invalid_expression();
5030 * Parse a statement expression.
5032 static expression_t *parse_statement_expression(void)
5034 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5036 statement_t *statement = parse_compound_statement(true);
5037 expression->statement.statement = statement;
5038 expression->base.source_position = statement->base.source_position;
5040 /* find last statement and use its type */
5041 type_t *type = type_void;
5042 const statement_t *stmt = statement->compound.statements;
5044 while (stmt->base.next != NULL)
5045 stmt = stmt->base.next;
5047 if (stmt->kind == STATEMENT_EXPRESSION) {
5048 type = stmt->expression.expression->base.type;
5051 warningf(&expression->base.source_position, "empty statement expression ({})");
5053 expression->base.type = type;
5059 return create_invalid_expression();
5063 * Parse a braced expression.
5065 static expression_t *parse_brace_expression(void)
5068 add_anchor_token(')');
5070 switch(token.type) {
5072 /* gcc extension: a statement expression */
5073 return parse_statement_expression();
5077 return parse_cast();
5079 if(is_typedef_symbol(token.v.symbol)) {
5080 return parse_cast();
5084 expression_t *result = parse_expression();
5085 rem_anchor_token(')');
5090 return create_invalid_expression();
5093 static expression_t *parse_function_keyword(void)
5098 if (current_function == NULL) {
5099 errorf(HERE, "'__func__' used outside of a function");
5102 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5103 expression->base.type = type_char_ptr;
5104 expression->funcname.kind = FUNCNAME_FUNCTION;
5109 static expression_t *parse_pretty_function_keyword(void)
5111 eat(T___PRETTY_FUNCTION__);
5113 if (current_function == NULL) {
5114 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5117 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5118 expression->base.type = type_char_ptr;
5119 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5124 static expression_t *parse_funcsig_keyword(void)
5128 if (current_function == NULL) {
5129 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5132 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5133 expression->base.type = type_char_ptr;
5134 expression->funcname.kind = FUNCNAME_FUNCSIG;
5139 static expression_t *parse_funcdname_keyword(void)
5141 eat(T___FUNCDNAME__);
5143 if (current_function == NULL) {
5144 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5147 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5148 expression->base.type = type_char_ptr;
5149 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5154 static designator_t *parse_designator(void)
5156 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5157 result->source_position = *HERE;
5159 if(token.type != T_IDENTIFIER) {
5160 parse_error_expected("while parsing member designator",
5161 T_IDENTIFIER, NULL);
5164 result->symbol = token.v.symbol;
5167 designator_t *last_designator = result;
5169 if(token.type == '.') {
5171 if(token.type != T_IDENTIFIER) {
5172 parse_error_expected("while parsing member designator",
5173 T_IDENTIFIER, NULL);
5176 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5177 designator->source_position = *HERE;
5178 designator->symbol = token.v.symbol;
5181 last_designator->next = designator;
5182 last_designator = designator;
5185 if(token.type == '[') {
5187 add_anchor_token(']');
5188 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5189 designator->source_position = *HERE;
5190 designator->array_index = parse_expression();
5191 rem_anchor_token(']');
5193 if(designator->array_index == NULL) {
5197 last_designator->next = designator;
5198 last_designator = designator;
5210 * Parse the __builtin_offsetof() expression.
5212 static expression_t *parse_offsetof(void)
5214 eat(T___builtin_offsetof);
5216 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5217 expression->base.type = type_size_t;
5220 add_anchor_token(',');
5221 type_t *type = parse_typename();
5222 rem_anchor_token(',');
5224 add_anchor_token(')');
5225 designator_t *designator = parse_designator();
5226 rem_anchor_token(')');
5229 expression->offsetofe.type = type;
5230 expression->offsetofe.designator = designator;
5233 memset(&path, 0, sizeof(path));
5234 path.top_type = type;
5235 path.path = NEW_ARR_F(type_path_entry_t, 0);
5237 descend_into_subtype(&path);
5239 if(!walk_designator(&path, designator, true)) {
5240 return create_invalid_expression();
5243 DEL_ARR_F(path.path);
5247 return create_invalid_expression();
5251 * Parses a _builtin_va_start() expression.
5253 static expression_t *parse_va_start(void)
5255 eat(T___builtin_va_start);
5257 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5260 add_anchor_token(',');
5261 expression->va_starte.ap = parse_assignment_expression();
5262 rem_anchor_token(',');
5264 expression_t *const expr = parse_assignment_expression();
5265 if (expr->kind == EXPR_REFERENCE) {
5266 declaration_t *const decl = expr->reference.declaration;
5268 return create_invalid_expression();
5269 if (decl->parent_scope == ¤t_function->scope &&
5270 decl->next == NULL) {
5271 expression->va_starte.parameter = decl;
5276 errorf(&expr->base.source_position,
5277 "second argument of 'va_start' must be last parameter of the current function");
5279 return create_invalid_expression();
5283 * Parses a _builtin_va_arg() expression.
5285 static expression_t *parse_va_arg(void)
5287 eat(T___builtin_va_arg);
5289 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5292 expression->va_arge.ap = parse_assignment_expression();
5294 expression->base.type = parse_typename();
5299 return create_invalid_expression();
5302 static expression_t *parse_builtin_symbol(void)
5304 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5306 symbol_t *symbol = token.v.symbol;
5308 expression->builtin_symbol.symbol = symbol;
5311 type_t *type = get_builtin_symbol_type(symbol);
5312 type = automatic_type_conversion(type);
5314 expression->base.type = type;
5319 * Parses a __builtin_constant() expression.
5321 static expression_t *parse_builtin_constant(void)
5323 eat(T___builtin_constant_p);
5325 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5328 add_anchor_token(')');
5329 expression->builtin_constant.value = parse_assignment_expression();
5330 rem_anchor_token(')');
5332 expression->base.type = type_int;
5336 return create_invalid_expression();
5340 * Parses a __builtin_prefetch() expression.
5342 static expression_t *parse_builtin_prefetch(void)
5344 eat(T___builtin_prefetch);
5346 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5349 add_anchor_token(')');
5350 expression->builtin_prefetch.adr = parse_assignment_expression();
5351 if (token.type == ',') {
5353 expression->builtin_prefetch.rw = parse_assignment_expression();
5355 if (token.type == ',') {
5357 expression->builtin_prefetch.locality = parse_assignment_expression();
5359 rem_anchor_token(')');
5361 expression->base.type = type_void;
5365 return create_invalid_expression();
5369 * Parses a __builtin_is_*() compare expression.
5371 static expression_t *parse_compare_builtin(void)
5373 expression_t *expression;
5375 switch(token.type) {
5376 case T___builtin_isgreater:
5377 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5379 case T___builtin_isgreaterequal:
5380 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5382 case T___builtin_isless:
5383 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5385 case T___builtin_islessequal:
5386 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5388 case T___builtin_islessgreater:
5389 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5391 case T___builtin_isunordered:
5392 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5395 internal_errorf(HERE, "invalid compare builtin found");
5398 expression->base.source_position = *HERE;
5402 expression->binary.left = parse_assignment_expression();
5404 expression->binary.right = parse_assignment_expression();
5407 type_t *const orig_type_left = expression->binary.left->base.type;
5408 type_t *const orig_type_right = expression->binary.right->base.type;
5410 type_t *const type_left = skip_typeref(orig_type_left);
5411 type_t *const type_right = skip_typeref(orig_type_right);
5412 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5413 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5414 type_error_incompatible("invalid operands in comparison",
5415 &expression->base.source_position, orig_type_left, orig_type_right);
5418 semantic_comparison(&expression->binary);
5423 return create_invalid_expression();
5427 * Parses a __builtin_expect() expression.
5429 static expression_t *parse_builtin_expect(void)
5431 eat(T___builtin_expect);
5433 expression_t *expression
5434 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5437 expression->binary.left = parse_assignment_expression();
5439 expression->binary.right = parse_constant_expression();
5442 expression->base.type = expression->binary.left->base.type;
5446 return create_invalid_expression();
5450 * Parses a MS assume() expression.
5452 static expression_t *parse_assume(void) {
5455 expression_t *expression
5456 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5459 add_anchor_token(')');
5460 expression->unary.value = parse_assignment_expression();
5461 rem_anchor_token(')');
5464 expression->base.type = type_void;
5467 return create_invalid_expression();
5471 * Parse a microsoft __noop expression.
5473 static expression_t *parse_noop_expression(void) {
5474 source_position_t source_position = *HERE;
5477 if (token.type == '(') {
5478 /* parse arguments */
5480 add_anchor_token(')');
5481 add_anchor_token(',');
5483 if(token.type != ')') {
5485 (void)parse_assignment_expression();
5486 if(token.type != ',')
5492 rem_anchor_token(',');
5493 rem_anchor_token(')');
5496 /* the result is a (int)0 */
5497 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5498 cnst->base.source_position = source_position;
5499 cnst->base.type = type_int;
5500 cnst->conste.v.int_value = 0;
5501 cnst->conste.is_ms_noop = true;
5506 return create_invalid_expression();
5510 * Parses a primary expression.
5512 static expression_t *parse_primary_expression(void)
5514 switch (token.type) {
5515 case T_INTEGER: return parse_int_const();
5516 case T_CHARACTER_CONSTANT: return parse_character_constant();
5517 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5518 case T_FLOATINGPOINT: return parse_float_const();
5519 case T_STRING_LITERAL:
5520 case T_WIDE_STRING_LITERAL: return parse_string_const();
5521 case T_IDENTIFIER: return parse_reference();
5522 case T___FUNCTION__:
5523 case T___func__: return parse_function_keyword();
5524 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5525 case T___FUNCSIG__: return parse_funcsig_keyword();
5526 case T___FUNCDNAME__: return parse_funcdname_keyword();
5527 case T___builtin_offsetof: return parse_offsetof();
5528 case T___builtin_va_start: return parse_va_start();
5529 case T___builtin_va_arg: return parse_va_arg();
5530 case T___builtin_expect: return parse_builtin_expect();
5531 case T___builtin_alloca:
5532 case T___builtin_nan:
5533 case T___builtin_nand:
5534 case T___builtin_nanf:
5535 case T___builtin_va_end: return parse_builtin_symbol();
5536 case T___builtin_isgreater:
5537 case T___builtin_isgreaterequal:
5538 case T___builtin_isless:
5539 case T___builtin_islessequal:
5540 case T___builtin_islessgreater:
5541 case T___builtin_isunordered: return parse_compare_builtin();
5542 case T___builtin_constant_p: return parse_builtin_constant();
5543 case T___builtin_prefetch: return parse_builtin_prefetch();
5544 case T__assume: return parse_assume();
5546 case '(': return parse_brace_expression();
5547 case T___noop: return parse_noop_expression();
5550 errorf(HERE, "unexpected token %K, expected an expression", &token);
5551 return create_invalid_expression();
5555 * Check if the expression has the character type and issue a warning then.
5557 static void check_for_char_index_type(const expression_t *expression) {
5558 type_t *const type = expression->base.type;
5559 const type_t *const base_type = skip_typeref(type);
5561 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5562 warning.char_subscripts) {
5563 warningf(&expression->base.source_position,
5564 "array subscript has type '%T'", type);
5568 static expression_t *parse_array_expression(unsigned precedence,
5574 add_anchor_token(']');
5576 expression_t *inside = parse_expression();
5578 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5580 array_access_expression_t *array_access = &expression->array_access;
5582 type_t *const orig_type_left = left->base.type;
5583 type_t *const orig_type_inside = inside->base.type;
5585 type_t *const type_left = skip_typeref(orig_type_left);
5586 type_t *const type_inside = skip_typeref(orig_type_inside);
5588 type_t *return_type;
5589 if (is_type_pointer(type_left)) {
5590 return_type = type_left->pointer.points_to;
5591 array_access->array_ref = left;
5592 array_access->index = inside;
5593 check_for_char_index_type(inside);
5594 } else if (is_type_pointer(type_inside)) {
5595 return_type = type_inside->pointer.points_to;
5596 array_access->array_ref = inside;
5597 array_access->index = left;
5598 array_access->flipped = true;
5599 check_for_char_index_type(left);
5601 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5603 "array access on object with non-pointer types '%T', '%T'",
5604 orig_type_left, orig_type_inside);
5606 return_type = type_error_type;
5607 array_access->array_ref = create_invalid_expression();
5610 rem_anchor_token(']');
5611 if(token.type != ']') {
5612 parse_error_expected("Problem while parsing array access", ']', NULL);
5617 return_type = automatic_type_conversion(return_type);
5618 expression->base.type = return_type;
5623 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5625 expression_t *tp_expression = allocate_expression_zero(kind);
5626 tp_expression->base.type = type_size_t;
5628 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5630 add_anchor_token(')');
5631 tp_expression->typeprop.type = parse_typename();
5632 rem_anchor_token(')');
5635 expression_t *expression = parse_sub_expression(precedence);
5636 expression->base.type = revert_automatic_type_conversion(expression);
5638 tp_expression->typeprop.type = expression->base.type;
5639 tp_expression->typeprop.tp_expression = expression;
5642 return tp_expression;
5644 return create_invalid_expression();
5647 static expression_t *parse_sizeof(unsigned precedence)
5650 return parse_typeprop(EXPR_SIZEOF, precedence);
5653 static expression_t *parse_alignof(unsigned precedence)
5656 return parse_typeprop(EXPR_SIZEOF, precedence);
5659 static expression_t *parse_select_expression(unsigned precedence,
5660 expression_t *compound)
5663 assert(token.type == '.' || token.type == T_MINUSGREATER);
5665 bool is_pointer = (token.type == T_MINUSGREATER);
5668 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5669 select->select.compound = compound;
5671 if(token.type != T_IDENTIFIER) {
5672 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
5675 symbol_t *symbol = token.v.symbol;
5676 select->select.symbol = symbol;
5679 type_t *const orig_type = compound->base.type;
5680 type_t *const type = skip_typeref(orig_type);
5682 type_t *type_left = type;
5684 if (!is_type_pointer(type)) {
5685 if (is_type_valid(type)) {
5686 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5688 return create_invalid_expression();
5690 type_left = type->pointer.points_to;
5692 type_left = skip_typeref(type_left);
5694 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5695 type_left->kind != TYPE_COMPOUND_UNION) {
5696 if (is_type_valid(type_left)) {
5697 errorf(HERE, "request for member '%Y' in something not a struct or "
5698 "union, but '%T'", symbol, type_left);
5700 return create_invalid_expression();
5703 declaration_t *const declaration = type_left->compound.declaration;
5705 if(!declaration->init.complete) {
5706 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5708 return create_invalid_expression();
5711 declaration_t *iter = find_compound_entry(declaration, symbol);
5713 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5714 return create_invalid_expression();
5717 /* we always do the auto-type conversions; the & and sizeof parser contains
5718 * code to revert this! */
5719 type_t *expression_type = automatic_type_conversion(iter->type);
5721 select->select.compound_entry = iter;
5722 select->base.type = expression_type;
5724 if(expression_type->kind == TYPE_BITFIELD) {
5725 expression_t *extract
5726 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5727 extract->unary.value = select;
5728 extract->base.type = expression_type->bitfield.base_type;
5737 * Parse a call expression, ie. expression '( ... )'.
5739 * @param expression the function address
5741 static expression_t *parse_call_expression(unsigned precedence,
5742 expression_t *expression)
5745 expression_t *result = allocate_expression_zero(EXPR_CALL);
5746 result->base.source_position = expression->base.source_position;
5748 call_expression_t *call = &result->call;
5749 call->function = expression;
5751 type_t *const orig_type = expression->base.type;
5752 type_t *const type = skip_typeref(orig_type);
5754 function_type_t *function_type = NULL;
5755 if (is_type_pointer(type)) {
5756 type_t *const to_type = skip_typeref(type->pointer.points_to);
5758 if (is_type_function(to_type)) {
5759 function_type = &to_type->function;
5760 call->base.type = function_type->return_type;
5764 if (function_type == NULL && is_type_valid(type)) {
5765 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5768 /* parse arguments */
5770 add_anchor_token(')');
5771 add_anchor_token(',');
5773 if(token.type != ')') {
5774 call_argument_t *last_argument = NULL;
5777 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5779 argument->expression = parse_assignment_expression();
5780 if(last_argument == NULL) {
5781 call->arguments = argument;
5783 last_argument->next = argument;
5785 last_argument = argument;
5787 if(token.type != ',')
5792 rem_anchor_token(',');
5793 rem_anchor_token(')');
5796 if(function_type != NULL) {
5797 function_parameter_t *parameter = function_type->parameters;
5798 call_argument_t *argument = call->arguments;
5799 for( ; parameter != NULL && argument != NULL;
5800 parameter = parameter->next, argument = argument->next) {
5801 type_t *expected_type = parameter->type;
5802 /* TODO report scope in error messages */
5803 expression_t *const arg_expr = argument->expression;
5804 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5806 &arg_expr->base.source_position);
5807 if (res_type == NULL) {
5808 /* TODO improve error message */
5809 errorf(&arg_expr->base.source_position,
5810 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5811 arg_expr, arg_expr->base.type, expected_type);
5813 argument->expression = create_implicit_cast(argument->expression, expected_type);
5816 /* too few parameters */
5817 if(parameter != NULL) {
5818 errorf(HERE, "too few arguments to function '%E'", expression);
5819 } else if(argument != NULL) {
5820 /* too many parameters */
5821 if(!function_type->variadic
5822 && !function_type->unspecified_parameters) {
5823 errorf(HERE, "too many arguments to function '%E'", expression);
5825 /* do default promotion */
5826 for( ; argument != NULL; argument = argument->next) {
5827 type_t *type = argument->expression->base.type;
5829 type = skip_typeref(type);
5830 if(is_type_integer(type)) {
5831 type = promote_integer(type);
5832 } else if(type == type_float) {
5836 argument->expression
5837 = create_implicit_cast(argument->expression, type);
5840 check_format(&result->call);
5843 check_format(&result->call);
5849 return create_invalid_expression();
5852 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5854 static bool same_compound_type(const type_t *type1, const type_t *type2)
5857 is_type_compound(type1) &&
5858 type1->kind == type2->kind &&
5859 type1->compound.declaration == type2->compound.declaration;
5863 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5865 * @param expression the conditional expression
5867 static expression_t *parse_conditional_expression(unsigned precedence,
5868 expression_t *expression)
5871 add_anchor_token(':');
5873 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5875 conditional_expression_t *conditional = &result->conditional;
5876 conditional->condition = expression;
5879 type_t *const condition_type_orig = expression->base.type;
5880 type_t *const condition_type = skip_typeref(condition_type_orig);
5881 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5882 type_error("expected a scalar type in conditional condition",
5883 &expression->base.source_position, condition_type_orig);
5886 expression_t *true_expression = parse_expression();
5887 rem_anchor_token(':');
5889 expression_t *false_expression = parse_sub_expression(precedence);
5891 type_t *const orig_true_type = true_expression->base.type;
5892 type_t *const orig_false_type = false_expression->base.type;
5893 type_t *const true_type = skip_typeref(orig_true_type);
5894 type_t *const false_type = skip_typeref(orig_false_type);
5897 type_t *result_type;
5898 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5899 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5900 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5901 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5902 warningf(&expression->base.source_position,
5903 "ISO C forbids conditional expression with only one void side");
5905 result_type = type_void;
5906 } else if (is_type_arithmetic(true_type)
5907 && is_type_arithmetic(false_type)) {
5908 result_type = semantic_arithmetic(true_type, false_type);
5910 true_expression = create_implicit_cast(true_expression, result_type);
5911 false_expression = create_implicit_cast(false_expression, result_type);
5913 conditional->true_expression = true_expression;
5914 conditional->false_expression = false_expression;
5915 conditional->base.type = result_type;
5916 } else if (same_compound_type(true_type, false_type)) {
5917 /* just take 1 of the 2 types */
5918 result_type = true_type;
5919 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5920 type_t *pointer_type;
5922 expression_t *other_expression;
5923 if (is_type_pointer(true_type)) {
5924 pointer_type = true_type;
5925 other_type = false_type;
5926 other_expression = false_expression;
5928 pointer_type = false_type;
5929 other_type = true_type;
5930 other_expression = true_expression;
5933 if(is_type_pointer(other_type)) {
5934 if(!pointers_compatible(true_type, false_type)) {
5935 warningf(&expression->base.source_position,
5936 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5938 result_type = true_type;
5939 } else if(is_null_pointer_constant(other_expression)) {
5940 result_type = pointer_type;
5941 } else if(is_type_integer(other_type)) {
5942 warningf(&expression->base.source_position,
5943 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5944 result_type = pointer_type;
5946 type_error_incompatible("while parsing conditional",
5947 &expression->base.source_position, true_type, false_type);
5948 result_type = type_error_type;
5951 /* TODO: one pointer to void*, other some pointer */
5953 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5954 type_error_incompatible("while parsing conditional",
5955 &expression->base.source_position, true_type,
5958 result_type = type_error_type;
5961 conditional->true_expression
5962 = create_implicit_cast(true_expression, result_type);
5963 conditional->false_expression
5964 = create_implicit_cast(false_expression, result_type);
5965 conditional->base.type = result_type;
5968 return create_invalid_expression();
5972 * Parse an extension expression.
5974 static expression_t *parse_extension(unsigned precedence)
5976 eat(T___extension__);
5978 /* TODO enable extensions */
5979 expression_t *expression = parse_sub_expression(precedence);
5980 /* TODO disable extensions */
5985 * Parse a __builtin_classify_type() expression.
5987 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5989 eat(T___builtin_classify_type);
5991 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5992 result->base.type = type_int;
5995 add_anchor_token(')');
5996 expression_t *expression = parse_sub_expression(precedence);
5997 rem_anchor_token(')');
5999 result->classify_type.type_expression = expression;
6003 return create_invalid_expression();
6006 static void semantic_incdec(unary_expression_t *expression)
6008 type_t *const orig_type = expression->value->base.type;
6009 type_t *const type = skip_typeref(orig_type);
6010 /* TODO !is_type_real && !is_type_pointer */
6011 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
6012 if (is_type_valid(type)) {
6013 /* TODO: improve error message */
6014 errorf(HERE, "operation needs an arithmetic or pointer type");
6019 expression->base.type = orig_type;
6022 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6024 type_t *const orig_type = expression->value->base.type;
6025 type_t *const type = skip_typeref(orig_type);
6026 if(!is_type_arithmetic(type)) {
6027 if (is_type_valid(type)) {
6028 /* TODO: improve error message */
6029 errorf(HERE, "operation needs an arithmetic type");
6034 expression->base.type = orig_type;
6037 static void semantic_unexpr_scalar(unary_expression_t *expression)
6039 type_t *const orig_type = expression->value->base.type;
6040 type_t *const type = skip_typeref(orig_type);
6041 if (!is_type_scalar(type)) {
6042 if (is_type_valid(type)) {
6043 errorf(HERE, "operand of ! must be of scalar type");
6048 expression->base.type = orig_type;
6051 static void semantic_unexpr_integer(unary_expression_t *expression)
6053 type_t *const orig_type = expression->value->base.type;
6054 type_t *const type = skip_typeref(orig_type);
6055 if (!is_type_integer(type)) {
6056 if (is_type_valid(type)) {
6057 errorf(HERE, "operand of ~ must be of integer type");
6062 expression->base.type = orig_type;
6065 static void semantic_dereference(unary_expression_t *expression)
6067 type_t *const orig_type = expression->value->base.type;
6068 type_t *const type = skip_typeref(orig_type);
6069 if(!is_type_pointer(type)) {
6070 if (is_type_valid(type)) {
6071 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6076 type_t *result_type = type->pointer.points_to;
6077 result_type = automatic_type_conversion(result_type);
6078 expression->base.type = result_type;
6082 * Check the semantic of the address taken expression.
6084 static void semantic_take_addr(unary_expression_t *expression)
6086 expression_t *value = expression->value;
6087 value->base.type = revert_automatic_type_conversion(value);
6089 type_t *orig_type = value->base.type;
6090 if(!is_type_valid(orig_type))
6093 if(value->kind == EXPR_REFERENCE) {
6094 declaration_t *const declaration = value->reference.declaration;
6095 if(declaration != NULL) {
6096 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6097 errorf(&expression->base.source_position,
6098 "address of register variable '%Y' requested",
6099 declaration->symbol);
6101 declaration->address_taken = 1;
6105 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6108 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6109 static expression_t *parse_##unexpression_type(unsigned precedence) \
6113 expression_t *unary_expression \
6114 = allocate_expression_zero(unexpression_type); \
6115 unary_expression->base.source_position = *HERE; \
6116 unary_expression->unary.value = parse_sub_expression(precedence); \
6118 sfunc(&unary_expression->unary); \
6120 return unary_expression; \
6123 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6124 semantic_unexpr_arithmetic)
6125 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6126 semantic_unexpr_arithmetic)
6127 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6128 semantic_unexpr_scalar)
6129 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6130 semantic_dereference)
6131 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6133 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6134 semantic_unexpr_integer)
6135 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6137 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6140 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6142 static expression_t *parse_##unexpression_type(unsigned precedence, \
6143 expression_t *left) \
6145 (void) precedence; \
6148 expression_t *unary_expression \
6149 = allocate_expression_zero(unexpression_type); \
6150 unary_expression->unary.value = left; \
6152 sfunc(&unary_expression->unary); \
6154 return unary_expression; \
6157 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6158 EXPR_UNARY_POSTFIX_INCREMENT,
6160 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6161 EXPR_UNARY_POSTFIX_DECREMENT,
6164 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6166 /* TODO: handle complex + imaginary types */
6168 /* § 6.3.1.8 Usual arithmetic conversions */
6169 if(type_left == type_long_double || type_right == type_long_double) {
6170 return type_long_double;
6171 } else if(type_left == type_double || type_right == type_double) {
6173 } else if(type_left == type_float || type_right == type_float) {
6177 type_right = promote_integer(type_right);
6178 type_left = promote_integer(type_left);
6180 if(type_left == type_right)
6183 bool signed_left = is_type_signed(type_left);
6184 bool signed_right = is_type_signed(type_right);
6185 int rank_left = get_rank(type_left);
6186 int rank_right = get_rank(type_right);
6187 if(rank_left < rank_right) {
6188 if(signed_left == signed_right || !signed_right) {
6194 if(signed_left == signed_right || !signed_left) {
6203 * Check the semantic restrictions for a binary expression.
6205 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6207 expression_t *const left = expression->left;
6208 expression_t *const right = expression->right;
6209 type_t *const orig_type_left = left->base.type;
6210 type_t *const orig_type_right = right->base.type;
6211 type_t *const type_left = skip_typeref(orig_type_left);
6212 type_t *const type_right = skip_typeref(orig_type_right);
6214 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6215 /* TODO: improve error message */
6216 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6217 errorf(HERE, "operation needs arithmetic types");
6222 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6223 expression->left = create_implicit_cast(left, arithmetic_type);
6224 expression->right = create_implicit_cast(right, arithmetic_type);
6225 expression->base.type = arithmetic_type;
6228 static void semantic_shift_op(binary_expression_t *expression)
6230 expression_t *const left = expression->left;
6231 expression_t *const right = expression->right;
6232 type_t *const orig_type_left = left->base.type;
6233 type_t *const orig_type_right = right->base.type;
6234 type_t * type_left = skip_typeref(orig_type_left);
6235 type_t * type_right = skip_typeref(orig_type_right);
6237 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6238 /* TODO: improve error message */
6239 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6240 errorf(HERE, "operation needs integer types");
6245 type_left = promote_integer(type_left);
6246 type_right = promote_integer(type_right);
6248 expression->left = create_implicit_cast(left, type_left);
6249 expression->right = create_implicit_cast(right, type_right);
6250 expression->base.type = type_left;
6253 static void semantic_add(binary_expression_t *expression)
6255 expression_t *const left = expression->left;
6256 expression_t *const right = expression->right;
6257 type_t *const orig_type_left = left->base.type;
6258 type_t *const orig_type_right = right->base.type;
6259 type_t *const type_left = skip_typeref(orig_type_left);
6260 type_t *const type_right = skip_typeref(orig_type_right);
6263 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6264 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6265 expression->left = create_implicit_cast(left, arithmetic_type);
6266 expression->right = create_implicit_cast(right, arithmetic_type);
6267 expression->base.type = arithmetic_type;
6269 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6270 expression->base.type = type_left;
6271 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6272 expression->base.type = type_right;
6273 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6274 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6278 static void semantic_sub(binary_expression_t *expression)
6280 expression_t *const left = expression->left;
6281 expression_t *const right = expression->right;
6282 type_t *const orig_type_left = left->base.type;
6283 type_t *const orig_type_right = right->base.type;
6284 type_t *const type_left = skip_typeref(orig_type_left);
6285 type_t *const type_right = skip_typeref(orig_type_right);
6288 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6289 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6290 expression->left = create_implicit_cast(left, arithmetic_type);
6291 expression->right = create_implicit_cast(right, arithmetic_type);
6292 expression->base.type = arithmetic_type;
6294 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6295 expression->base.type = type_left;
6296 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6297 if(!pointers_compatible(type_left, type_right)) {
6299 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6300 orig_type_left, orig_type_right);
6302 expression->base.type = type_ptrdiff_t;
6304 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6305 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6306 orig_type_left, orig_type_right);
6311 * Check the semantics of comparison expressions.
6313 * @param expression The expression to check.
6315 static void semantic_comparison(binary_expression_t *expression)
6317 expression_t *left = expression->left;
6318 expression_t *right = expression->right;
6319 type_t *orig_type_left = left->base.type;
6320 type_t *orig_type_right = right->base.type;
6322 type_t *type_left = skip_typeref(orig_type_left);
6323 type_t *type_right = skip_typeref(orig_type_right);
6325 /* TODO non-arithmetic types */
6326 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6327 /* test for signed vs unsigned compares */
6328 if (warning.sign_compare &&
6329 (expression->base.kind != EXPR_BINARY_EQUAL &&
6330 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6331 (is_type_signed(type_left) != is_type_signed(type_right))) {
6333 /* check if 1 of the operands is a constant, in this case we just
6334 * check wether we can safely represent the resulting constant in
6335 * the type of the other operand. */
6336 expression_t *const_expr = NULL;
6337 expression_t *other_expr = NULL;
6339 if(is_constant_expression(left)) {
6342 } else if(is_constant_expression(right)) {
6347 type_t *other_type = skip_typeref(other_expr->base.type);
6348 if(const_expr != NULL) {
6349 long val = fold_constant(const_expr);
6350 /* TODO: check if val can be represented by other_type */
6354 warningf(&expression->base.source_position,
6355 "comparison between signed and unsigned");
6357 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6358 expression->left = create_implicit_cast(left, arithmetic_type);
6359 expression->right = create_implicit_cast(right, arithmetic_type);
6360 expression->base.type = arithmetic_type;
6361 if (warning.float_equal &&
6362 (expression->base.kind == EXPR_BINARY_EQUAL ||
6363 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6364 is_type_float(arithmetic_type)) {
6365 warningf(&expression->base.source_position,
6366 "comparing floating point with == or != is unsafe");
6368 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6369 /* TODO check compatibility */
6370 } else if (is_type_pointer(type_left)) {
6371 expression->right = create_implicit_cast(right, type_left);
6372 } else if (is_type_pointer(type_right)) {
6373 expression->left = create_implicit_cast(left, type_right);
6374 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6375 type_error_incompatible("invalid operands in comparison",
6376 &expression->base.source_position,
6377 type_left, type_right);
6379 expression->base.type = type_int;
6382 static void semantic_arithmetic_assign(binary_expression_t *expression)
6384 expression_t *left = expression->left;
6385 expression_t *right = expression->right;
6386 type_t *orig_type_left = left->base.type;
6387 type_t *orig_type_right = right->base.type;
6389 type_t *type_left = skip_typeref(orig_type_left);
6390 type_t *type_right = skip_typeref(orig_type_right);
6392 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6393 /* TODO: improve error message */
6394 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6395 errorf(HERE, "operation needs arithmetic types");
6400 /* combined instructions are tricky. We can't create an implicit cast on
6401 * the left side, because we need the uncasted form for the store.
6402 * The ast2firm pass has to know that left_type must be right_type
6403 * for the arithmetic operation and create a cast by itself */
6404 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6405 expression->right = create_implicit_cast(right, arithmetic_type);
6406 expression->base.type = type_left;
6409 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6411 expression_t *const left = expression->left;
6412 expression_t *const right = expression->right;
6413 type_t *const orig_type_left = left->base.type;
6414 type_t *const orig_type_right = right->base.type;
6415 type_t *const type_left = skip_typeref(orig_type_left);
6416 type_t *const type_right = skip_typeref(orig_type_right);
6418 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6419 /* combined instructions are tricky. We can't create an implicit cast on
6420 * the left side, because we need the uncasted form for the store.
6421 * The ast2firm pass has to know that left_type must be right_type
6422 * for the arithmetic operation and create a cast by itself */
6423 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6424 expression->right = create_implicit_cast(right, arithmetic_type);
6425 expression->base.type = type_left;
6426 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6427 expression->base.type = type_left;
6428 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6429 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6434 * Check the semantic restrictions of a logical expression.
6436 static void semantic_logical_op(binary_expression_t *expression)
6438 expression_t *const left = expression->left;
6439 expression_t *const right = expression->right;
6440 type_t *const orig_type_left = left->base.type;
6441 type_t *const orig_type_right = right->base.type;
6442 type_t *const type_left = skip_typeref(orig_type_left);
6443 type_t *const type_right = skip_typeref(orig_type_right);
6445 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6446 /* TODO: improve error message */
6447 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6448 errorf(HERE, "operation needs scalar types");
6453 expression->base.type = type_int;
6457 * Checks if a compound type has constant fields.
6459 static bool has_const_fields(const compound_type_t *type)
6461 const scope_t *scope = &type->declaration->scope;
6462 const declaration_t *declaration = scope->declarations;
6464 for (; declaration != NULL; declaration = declaration->next) {
6465 if (declaration->namespc != NAMESPACE_NORMAL)
6468 const type_t *decl_type = skip_typeref(declaration->type);
6469 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6477 * Check the semantic restrictions of a binary assign expression.
6479 static void semantic_binexpr_assign(binary_expression_t *expression)
6481 expression_t *left = expression->left;
6482 type_t *orig_type_left = left->base.type;
6484 type_t *type_left = revert_automatic_type_conversion(left);
6485 type_left = skip_typeref(orig_type_left);
6487 /* must be a modifiable lvalue */
6488 if (is_type_array(type_left)) {
6489 errorf(HERE, "cannot assign to arrays ('%E')", left);
6492 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6493 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6497 if(is_type_incomplete(type_left)) {
6499 "left-hand side of assignment '%E' has incomplete type '%T'",
6500 left, orig_type_left);
6503 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6504 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6505 left, orig_type_left);
6509 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6510 "assignment", &left->base.source_position);
6511 if (res_type == NULL) {
6512 errorf(&expression->base.source_position,
6513 "cannot assign to '%T' from '%T'",
6514 orig_type_left, expression->right->base.type);
6516 expression->right = create_implicit_cast(expression->right, res_type);
6519 expression->base.type = orig_type_left;
6523 * Determine if the outermost operation (or parts thereof) of the given
6524 * expression has no effect in order to generate a warning about this fact.
6525 * Therefore in some cases this only examines some of the operands of the
6526 * expression (see comments in the function and examples below).
6528 * f() + 23; // warning, because + has no effect
6529 * x || f(); // no warning, because x controls execution of f()
6530 * x ? y : f(); // warning, because y has no effect
6531 * (void)x; // no warning to be able to suppress the warning
6532 * This function can NOT be used for an "expression has definitely no effect"-
6534 static bool expression_has_effect(const expression_t *const expr)
6536 switch (expr->kind) {
6537 case EXPR_UNKNOWN: break;
6538 case EXPR_INVALID: return true; /* do NOT warn */
6539 case EXPR_REFERENCE: return false;
6540 /* suppress the warning for microsoft __noop operations */
6541 case EXPR_CONST: return expr->conste.is_ms_noop;
6542 case EXPR_CHARACTER_CONSTANT: return false;
6543 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6544 case EXPR_STRING_LITERAL: return false;
6545 case EXPR_WIDE_STRING_LITERAL: return false;
6548 const call_expression_t *const call = &expr->call;
6549 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6552 switch (call->function->builtin_symbol.symbol->ID) {
6553 case T___builtin_va_end: return true;
6554 default: return false;
6558 /* Generate the warning if either the left or right hand side of a
6559 * conditional expression has no effect */
6560 case EXPR_CONDITIONAL: {
6561 const conditional_expression_t *const cond = &expr->conditional;
6563 expression_has_effect(cond->true_expression) &&
6564 expression_has_effect(cond->false_expression);
6567 case EXPR_SELECT: return false;
6568 case EXPR_ARRAY_ACCESS: return false;
6569 case EXPR_SIZEOF: return false;
6570 case EXPR_CLASSIFY_TYPE: return false;
6571 case EXPR_ALIGNOF: return false;
6573 case EXPR_FUNCNAME: return false;
6574 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6575 case EXPR_BUILTIN_CONSTANT_P: return false;
6576 case EXPR_BUILTIN_PREFETCH: return true;
6577 case EXPR_OFFSETOF: return false;
6578 case EXPR_VA_START: return true;
6579 case EXPR_VA_ARG: return true;
6580 case EXPR_STATEMENT: return true; // TODO
6581 case EXPR_COMPOUND_LITERAL: return false;
6583 case EXPR_UNARY_NEGATE: return false;
6584 case EXPR_UNARY_PLUS: return false;
6585 case EXPR_UNARY_BITWISE_NEGATE: return false;
6586 case EXPR_UNARY_NOT: return false;
6587 case EXPR_UNARY_DEREFERENCE: return false;
6588 case EXPR_UNARY_TAKE_ADDRESS: return false;
6589 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6590 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6591 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6592 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6594 /* Treat void casts as if they have an effect in order to being able to
6595 * suppress the warning */
6596 case EXPR_UNARY_CAST: {
6597 type_t *const type = skip_typeref(expr->base.type);
6598 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6601 case EXPR_UNARY_CAST_IMPLICIT: return true;
6602 case EXPR_UNARY_ASSUME: return true;
6603 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6605 case EXPR_BINARY_ADD: return false;
6606 case EXPR_BINARY_SUB: return false;
6607 case EXPR_BINARY_MUL: return false;
6608 case EXPR_BINARY_DIV: return false;
6609 case EXPR_BINARY_MOD: return false;
6610 case EXPR_BINARY_EQUAL: return false;
6611 case EXPR_BINARY_NOTEQUAL: return false;
6612 case EXPR_BINARY_LESS: return false;
6613 case EXPR_BINARY_LESSEQUAL: return false;
6614 case EXPR_BINARY_GREATER: return false;
6615 case EXPR_BINARY_GREATEREQUAL: return false;
6616 case EXPR_BINARY_BITWISE_AND: return false;
6617 case EXPR_BINARY_BITWISE_OR: return false;
6618 case EXPR_BINARY_BITWISE_XOR: return false;
6619 case EXPR_BINARY_SHIFTLEFT: return false;
6620 case EXPR_BINARY_SHIFTRIGHT: return false;
6621 case EXPR_BINARY_ASSIGN: return true;
6622 case EXPR_BINARY_MUL_ASSIGN: return true;
6623 case EXPR_BINARY_DIV_ASSIGN: return true;
6624 case EXPR_BINARY_MOD_ASSIGN: return true;
6625 case EXPR_BINARY_ADD_ASSIGN: return true;
6626 case EXPR_BINARY_SUB_ASSIGN: return true;
6627 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6628 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6629 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6630 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6631 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6633 /* Only examine the right hand side of && and ||, because the left hand
6634 * side already has the effect of controlling the execution of the right
6636 case EXPR_BINARY_LOGICAL_AND:
6637 case EXPR_BINARY_LOGICAL_OR:
6638 /* Only examine the right hand side of a comma expression, because the left
6639 * hand side has a separate warning */
6640 case EXPR_BINARY_COMMA:
6641 return expression_has_effect(expr->binary.right);
6643 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6644 case EXPR_BINARY_ISGREATER: return false;
6645 case EXPR_BINARY_ISGREATEREQUAL: return false;
6646 case EXPR_BINARY_ISLESS: return false;
6647 case EXPR_BINARY_ISLESSEQUAL: return false;
6648 case EXPR_BINARY_ISLESSGREATER: return false;
6649 case EXPR_BINARY_ISUNORDERED: return false;
6652 internal_errorf(HERE, "unexpected expression");
6655 static void semantic_comma(binary_expression_t *expression)
6657 if (warning.unused_value) {
6658 const expression_t *const left = expression->left;
6659 if (!expression_has_effect(left)) {
6660 warningf(&left->base.source_position,
6661 "left-hand operand of comma expression has no effect");
6664 expression->base.type = expression->right->base.type;
6667 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6668 static expression_t *parse_##binexpression_type(unsigned precedence, \
6669 expression_t *left) \
6672 source_position_t pos = *HERE; \
6674 expression_t *right = parse_sub_expression(precedence + lr); \
6676 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6677 binexpr->base.source_position = pos; \
6678 binexpr->binary.left = left; \
6679 binexpr->binary.right = right; \
6680 sfunc(&binexpr->binary); \
6685 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6686 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6687 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6688 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6689 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6690 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6691 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6692 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6693 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6695 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6696 semantic_comparison, 1)
6697 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6698 semantic_comparison, 1)
6699 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6700 semantic_comparison, 1)
6701 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6702 semantic_comparison, 1)
6704 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6705 semantic_binexpr_arithmetic, 1)
6706 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6707 semantic_binexpr_arithmetic, 1)
6708 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6709 semantic_binexpr_arithmetic, 1)
6710 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6711 semantic_logical_op, 1)
6712 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6713 semantic_logical_op, 1)
6714 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6715 semantic_shift_op, 1)
6716 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6717 semantic_shift_op, 1)
6718 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6719 semantic_arithmetic_addsubb_assign, 0)
6720 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6721 semantic_arithmetic_addsubb_assign, 0)
6722 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6723 semantic_arithmetic_assign, 0)
6724 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6725 semantic_arithmetic_assign, 0)
6726 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6727 semantic_arithmetic_assign, 0)
6728 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6729 semantic_arithmetic_assign, 0)
6730 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6731 semantic_arithmetic_assign, 0)
6732 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6733 semantic_arithmetic_assign, 0)
6734 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6735 semantic_arithmetic_assign, 0)
6736 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6737 semantic_arithmetic_assign, 0)
6739 static expression_t *parse_sub_expression(unsigned precedence)
6741 if(token.type < 0) {
6742 return expected_expression_error();
6745 expression_parser_function_t *parser
6746 = &expression_parsers[token.type];
6747 source_position_t source_position = token.source_position;
6750 if(parser->parser != NULL) {
6751 left = parser->parser(parser->precedence);
6753 left = parse_primary_expression();
6755 assert(left != NULL);
6756 left->base.source_position = source_position;
6759 if(token.type < 0) {
6760 return expected_expression_error();
6763 parser = &expression_parsers[token.type];
6764 if(parser->infix_parser == NULL)
6766 if(parser->infix_precedence < precedence)
6769 left = parser->infix_parser(parser->infix_precedence, left);
6771 assert(left != NULL);
6772 assert(left->kind != EXPR_UNKNOWN);
6773 left->base.source_position = source_position;
6780 * Parse an expression.
6782 static expression_t *parse_expression(void)
6784 return parse_sub_expression(1);
6788 * Register a parser for a prefix-like operator with given precedence.
6790 * @param parser the parser function
6791 * @param token_type the token type of the prefix token
6792 * @param precedence the precedence of the operator
6794 static void register_expression_parser(parse_expression_function parser,
6795 int token_type, unsigned precedence)
6797 expression_parser_function_t *entry = &expression_parsers[token_type];
6799 if(entry->parser != NULL) {
6800 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6801 panic("trying to register multiple expression parsers for a token");
6803 entry->parser = parser;
6804 entry->precedence = precedence;
6808 * Register a parser for an infix operator with given precedence.
6810 * @param parser the parser function
6811 * @param token_type the token type of the infix operator
6812 * @param precedence the precedence of the operator
6814 static void register_infix_parser(parse_expression_infix_function parser,
6815 int token_type, unsigned precedence)
6817 expression_parser_function_t *entry = &expression_parsers[token_type];
6819 if(entry->infix_parser != NULL) {
6820 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6821 panic("trying to register multiple infix expression parsers for a "
6824 entry->infix_parser = parser;
6825 entry->infix_precedence = precedence;
6829 * Initialize the expression parsers.
6831 static void init_expression_parsers(void)
6833 memset(&expression_parsers, 0, sizeof(expression_parsers));
6835 register_infix_parser(parse_array_expression, '[', 30);
6836 register_infix_parser(parse_call_expression, '(', 30);
6837 register_infix_parser(parse_select_expression, '.', 30);
6838 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6839 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6841 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6844 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6845 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6846 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6847 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6848 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6849 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6850 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6851 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6852 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6853 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6854 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6855 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6856 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6857 T_EXCLAMATIONMARKEQUAL, 13);
6858 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6859 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6860 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6861 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6862 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6863 register_infix_parser(parse_conditional_expression, '?', 7);
6864 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6865 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6866 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6867 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6868 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6869 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6870 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6871 T_LESSLESSEQUAL, 2);
6872 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6873 T_GREATERGREATEREQUAL, 2);
6874 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6876 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6878 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6881 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6883 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6884 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6885 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6886 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6887 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6888 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6889 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6891 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6893 register_expression_parser(parse_sizeof, T_sizeof, 25);
6894 register_expression_parser(parse_alignof, T___alignof__, 25);
6895 register_expression_parser(parse_extension, T___extension__, 25);
6896 register_expression_parser(parse_builtin_classify_type,
6897 T___builtin_classify_type, 25);
6901 * Parse a asm statement constraints specification.
6903 static asm_constraint_t *parse_asm_constraints(void)
6905 asm_constraint_t *result = NULL;
6906 asm_constraint_t *last = NULL;
6908 while(token.type == T_STRING_LITERAL || token.type == '[') {
6909 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6910 memset(constraint, 0, sizeof(constraint[0]));
6912 if(token.type == '[') {
6914 if(token.type != T_IDENTIFIER) {
6915 parse_error_expected("while parsing asm constraint",
6916 T_IDENTIFIER, NULL);
6919 constraint->symbol = token.v.symbol;
6924 constraint->constraints = parse_string_literals();
6926 constraint->expression = parse_expression();
6930 last->next = constraint;
6932 result = constraint;
6936 if(token.type != ',')
6947 * Parse a asm statement clobber specification.
6949 static asm_clobber_t *parse_asm_clobbers(void)
6951 asm_clobber_t *result = NULL;
6952 asm_clobber_t *last = NULL;
6954 while(token.type == T_STRING_LITERAL) {
6955 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6956 clobber->clobber = parse_string_literals();
6959 last->next = clobber;
6965 if(token.type != ',')
6974 * Parse an asm statement.
6976 static statement_t *parse_asm_statement(void)
6980 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6981 statement->base.source_position = token.source_position;
6983 asm_statement_t *asm_statement = &statement->asms;
6985 if(token.type == T_volatile) {
6987 asm_statement->is_volatile = true;
6991 add_anchor_token(')');
6992 add_anchor_token(':');
6993 asm_statement->asm_text = parse_string_literals();
6995 if(token.type != ':') {
6996 rem_anchor_token(':');
7001 asm_statement->inputs = parse_asm_constraints();
7002 if(token.type != ':') {
7003 rem_anchor_token(':');
7008 asm_statement->outputs = parse_asm_constraints();
7009 if(token.type != ':') {
7010 rem_anchor_token(':');
7013 rem_anchor_token(':');
7016 asm_statement->clobbers = parse_asm_clobbers();
7019 rem_anchor_token(')');
7024 return create_invalid_statement();
7028 * Parse a case statement.
7030 static statement_t *parse_case_statement(void)
7034 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7036 statement->base.source_position = token.source_position;
7037 statement->case_label.expression = parse_expression();
7039 if (c_mode & _GNUC) {
7040 if (token.type == T_DOTDOTDOT) {
7042 statement->case_label.end_range = parse_expression();
7048 if (! is_constant_expression(statement->case_label.expression)) {
7049 errorf(&statement->base.source_position,
7050 "case label does not reduce to an integer constant");
7052 /* TODO: check if the case label is already known */
7053 if (current_switch != NULL) {
7054 /* link all cases into the switch statement */
7055 if (current_switch->last_case == NULL) {
7056 current_switch->first_case =
7057 current_switch->last_case = &statement->case_label;
7059 current_switch->last_case->next = &statement->case_label;
7062 errorf(&statement->base.source_position,
7063 "case label not within a switch statement");
7066 statement->case_label.statement = parse_statement();
7070 return create_invalid_statement();
7074 * Finds an existing default label of a switch statement.
7076 static case_label_statement_t *
7077 find_default_label(const switch_statement_t *statement)
7079 case_label_statement_t *label = statement->first_case;
7080 for ( ; label != NULL; label = label->next) {
7081 if (label->expression == NULL)
7088 * Parse a default statement.
7090 static statement_t *parse_default_statement(void)
7094 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7096 statement->base.source_position = token.source_position;
7099 if (current_switch != NULL) {
7100 const case_label_statement_t *def_label = find_default_label(current_switch);
7101 if (def_label != NULL) {
7102 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7103 &def_label->base.source_position);
7105 /* link all cases into the switch statement */
7106 if (current_switch->last_case == NULL) {
7107 current_switch->first_case =
7108 current_switch->last_case = &statement->case_label;
7110 current_switch->last_case->next = &statement->case_label;
7114 errorf(&statement->base.source_position,
7115 "'default' label not within a switch statement");
7117 statement->case_label.statement = parse_statement();
7121 return create_invalid_statement();
7125 * Return the declaration for a given label symbol or create a new one.
7127 static declaration_t *get_label(symbol_t *symbol)
7129 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7130 assert(current_function != NULL);
7131 /* if we found a label in the same function, then we already created the
7133 if(candidate != NULL
7134 && candidate->parent_scope == ¤t_function->scope) {
7138 /* otherwise we need to create a new one */
7139 declaration_t *const declaration = allocate_declaration_zero();
7140 declaration->namespc = NAMESPACE_LABEL;
7141 declaration->symbol = symbol;
7143 label_push(declaration);
7149 * Parse a label statement.
7151 static statement_t *parse_label_statement(void)
7153 assert(token.type == T_IDENTIFIER);
7154 symbol_t *symbol = token.v.symbol;
7157 declaration_t *label = get_label(symbol);
7159 /* if source position is already set then the label is defined twice,
7160 * otherwise it was just mentioned in a goto so far */
7161 if(label->source_position.input_name != NULL) {
7162 errorf(HERE, "duplicate label '%Y' (declared %P)",
7163 symbol, &label->source_position);
7165 label->source_position = token.source_position;
7168 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7170 statement->base.source_position = token.source_position;
7171 statement->label.label = label;
7175 if(token.type == '}') {
7176 /* TODO only warn? */
7178 warningf(HERE, "label at end of compound statement");
7179 statement->label.statement = create_empty_statement();
7181 errorf(HERE, "label at end of compound statement");
7182 statement->label.statement = create_invalid_statement();
7186 if (token.type == ';') {
7187 /* eat an empty statement here, to avoid the warning about an empty
7188 * after a label. label:; is commonly used to have a label before
7190 statement->label.statement = create_empty_statement();
7193 statement->label.statement = parse_statement();
7197 /* remember the labels's in a list for later checking */
7198 if (label_last == NULL) {
7199 label_first = &statement->label;
7201 label_last->next = &statement->label;
7203 label_last = &statement->label;
7209 * Parse an if statement.
7211 static statement_t *parse_if(void)
7215 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7216 statement->base.source_position = token.source_position;
7219 add_anchor_token(')');
7220 statement->ifs.condition = parse_expression();
7221 rem_anchor_token(')');
7224 add_anchor_token(T_else);
7225 statement->ifs.true_statement = parse_statement();
7226 rem_anchor_token(T_else);
7228 if(token.type == T_else) {
7230 statement->ifs.false_statement = parse_statement();
7235 return create_invalid_statement();
7239 * Parse a switch statement.
7241 static statement_t *parse_switch(void)
7245 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7246 statement->base.source_position = token.source_position;
7249 expression_t *const expr = parse_expression();
7250 type_t * type = skip_typeref(expr->base.type);
7251 if (is_type_integer(type)) {
7252 type = promote_integer(type);
7253 } else if (is_type_valid(type)) {
7254 errorf(&expr->base.source_position,
7255 "switch quantity is not an integer, but '%T'", type);
7256 type = type_error_type;
7258 statement->switchs.expression = create_implicit_cast(expr, type);
7261 switch_statement_t *rem = current_switch;
7262 current_switch = &statement->switchs;
7263 statement->switchs.body = parse_statement();
7264 current_switch = rem;
7266 if(warning.switch_default &&
7267 find_default_label(&statement->switchs) == NULL) {
7268 warningf(&statement->base.source_position, "switch has no default case");
7273 return create_invalid_statement();
7276 static statement_t *parse_loop_body(statement_t *const loop)
7278 statement_t *const rem = current_loop;
7279 current_loop = loop;
7281 statement_t *const body = parse_statement();
7288 * Parse a while statement.
7290 static statement_t *parse_while(void)
7294 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7295 statement->base.source_position = token.source_position;
7298 add_anchor_token(')');
7299 statement->whiles.condition = parse_expression();
7300 rem_anchor_token(')');
7303 statement->whiles.body = parse_loop_body(statement);
7307 return create_invalid_statement();
7311 * Parse a do statement.
7313 static statement_t *parse_do(void)
7317 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7319 statement->base.source_position = token.source_position;
7321 add_anchor_token(T_while);
7322 statement->do_while.body = parse_loop_body(statement);
7323 rem_anchor_token(T_while);
7327 add_anchor_token(')');
7328 statement->do_while.condition = parse_expression();
7329 rem_anchor_token(')');
7335 return create_invalid_statement();
7339 * Parse a for statement.
7341 static statement_t *parse_for(void)
7345 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7346 statement->base.source_position = token.source_position;
7348 int top = environment_top();
7349 scope_t *last_scope = scope;
7350 set_scope(&statement->fors.scope);
7353 add_anchor_token(')');
7355 if(token.type != ';') {
7356 if(is_declaration_specifier(&token, false)) {
7357 parse_declaration(record_declaration);
7359 add_anchor_token(';');
7360 expression_t *const init = parse_expression();
7361 statement->fors.initialisation = init;
7362 if (warning.unused_value && !expression_has_effect(init)) {
7363 warningf(&init->base.source_position,
7364 "initialisation of 'for'-statement has no effect");
7366 rem_anchor_token(';');
7373 if(token.type != ';') {
7374 add_anchor_token(';');
7375 statement->fors.condition = parse_expression();
7376 rem_anchor_token(';');
7379 if(token.type != ')') {
7380 expression_t *const step = parse_expression();
7381 statement->fors.step = step;
7382 if (warning.unused_value && !expression_has_effect(step)) {
7383 warningf(&step->base.source_position,
7384 "step of 'for'-statement has no effect");
7387 rem_anchor_token(')');
7389 statement->fors.body = parse_loop_body(statement);
7391 assert(scope == &statement->fors.scope);
7392 set_scope(last_scope);
7393 environment_pop_to(top);
7398 rem_anchor_token(')');
7399 assert(scope == &statement->fors.scope);
7400 set_scope(last_scope);
7401 environment_pop_to(top);
7403 return create_invalid_statement();
7407 * Parse a goto statement.
7409 static statement_t *parse_goto(void)
7413 if(token.type != T_IDENTIFIER) {
7414 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
7418 symbol_t *symbol = token.v.symbol;
7421 declaration_t *label = get_label(symbol);
7423 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7424 statement->base.source_position = token.source_position;
7426 statement->gotos.label = label;
7428 /* remember the goto's in a list for later checking */
7429 if (goto_last == NULL) {
7430 goto_first = &statement->gotos;
7432 goto_last->next = &statement->gotos;
7434 goto_last = &statement->gotos;
7440 return create_invalid_statement();
7444 * Parse a continue statement.
7446 static statement_t *parse_continue(void)
7448 statement_t *statement;
7449 if (current_loop == NULL) {
7450 errorf(HERE, "continue statement not within loop");
7451 statement = create_invalid_statement();
7453 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7455 statement->base.source_position = token.source_position;
7463 return create_invalid_statement();
7467 * Parse a break statement.
7469 static statement_t *parse_break(void)
7471 statement_t *statement;
7472 if (current_switch == NULL && current_loop == NULL) {
7473 errorf(HERE, "break statement not within loop or switch");
7474 statement = create_invalid_statement();
7476 statement = allocate_statement_zero(STATEMENT_BREAK);
7478 statement->base.source_position = token.source_position;
7486 return create_invalid_statement();
7490 * Parse a __leave statement.
7492 static statement_t *parse_leave(void)
7494 statement_t *statement;
7495 if (current_try == NULL) {
7496 errorf(HERE, "__leave statement not within __try");
7497 statement = create_invalid_statement();
7499 statement = allocate_statement_zero(STATEMENT_LEAVE);
7501 statement->base.source_position = token.source_position;
7509 return create_invalid_statement();
7513 * Check if a given declaration represents a local variable.
7515 static bool is_local_var_declaration(const declaration_t *declaration) {
7516 switch ((storage_class_tag_t) declaration->storage_class) {
7517 case STORAGE_CLASS_AUTO:
7518 case STORAGE_CLASS_REGISTER: {
7519 const type_t *type = skip_typeref(declaration->type);
7520 if(is_type_function(type)) {
7532 * Check if a given declaration represents a variable.
7534 static bool is_var_declaration(const declaration_t *declaration) {
7535 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7538 const type_t *type = skip_typeref(declaration->type);
7539 return !is_type_function(type);
7543 * Check if a given expression represents a local variable.
7545 static bool is_local_variable(const expression_t *expression)
7547 if (expression->base.kind != EXPR_REFERENCE) {
7550 const declaration_t *declaration = expression->reference.declaration;
7551 return is_local_var_declaration(declaration);
7555 * Check if a given expression represents a local variable and
7556 * return its declaration then, else return NULL.
7558 declaration_t *expr_is_variable(const expression_t *expression)
7560 if (expression->base.kind != EXPR_REFERENCE) {
7563 declaration_t *declaration = expression->reference.declaration;
7564 if (is_var_declaration(declaration))
7570 * Parse a return statement.
7572 static statement_t *parse_return(void)
7574 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7575 statement->base.source_position = token.source_position;
7579 expression_t *return_value = NULL;
7580 if(token.type != ';') {
7581 return_value = parse_expression();
7585 const type_t *const func_type = current_function->type;
7586 assert(is_type_function(func_type));
7587 type_t *const return_type = skip_typeref(func_type->function.return_type);
7589 if(return_value != NULL) {
7590 type_t *return_value_type = skip_typeref(return_value->base.type);
7592 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7593 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7594 warningf(&statement->base.source_position,
7595 "'return' with a value, in function returning void");
7596 return_value = NULL;
7598 type_t *const res_type = semantic_assign(return_type,
7599 return_value, "'return'", &statement->base.source_position);
7600 if (res_type == NULL) {
7601 errorf(&statement->base.source_position,
7602 "cannot return something of type '%T' in function returning '%T'",
7603 return_value->base.type, return_type);
7605 return_value = create_implicit_cast(return_value, res_type);
7608 /* check for returning address of a local var */
7609 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7610 const expression_t *expression = return_value->unary.value;
7611 if (is_local_variable(expression)) {
7612 warningf(&statement->base.source_position,
7613 "function returns address of local variable");
7617 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7618 warningf(&statement->base.source_position,
7619 "'return' without value, in function returning non-void");
7622 statement->returns.value = return_value;
7626 return create_invalid_statement();
7630 * Parse a declaration statement.
7632 static statement_t *parse_declaration_statement(void)
7634 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7636 statement->base.source_position = token.source_position;
7638 declaration_t *before = last_declaration;
7639 parse_declaration(record_declaration);
7641 if(before == NULL) {
7642 statement->declaration.declarations_begin = scope->declarations;
7644 statement->declaration.declarations_begin = before->next;
7646 statement->declaration.declarations_end = last_declaration;
7652 * Parse an expression statement, ie. expr ';'.
7654 static statement_t *parse_expression_statement(void)
7656 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7658 statement->base.source_position = token.source_position;
7659 expression_t *const expr = parse_expression();
7660 statement->expression.expression = expr;
7666 return create_invalid_statement();
7670 * Parse a microsoft __try { } __finally { } or
7671 * __try{ } __except() { }
7673 static statement_t *parse_ms_try_statment(void) {
7674 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
7676 statement->base.source_position = token.source_position;
7679 ms_try_statement_t *rem = current_try;
7680 current_try = &statement->ms_try;
7681 statement->ms_try.try_statement = parse_compound_statement(false);
7684 if(token.type == T___except) {
7687 add_anchor_token(')');
7688 expression_t *const expr = parse_expression();
7689 type_t * type = skip_typeref(expr->base.type);
7690 if (is_type_integer(type)) {
7691 type = promote_integer(type);
7692 } else if (is_type_valid(type)) {
7693 errorf(&expr->base.source_position,
7694 "__expect expression is not an integer, but '%T'", type);
7695 type = type_error_type;
7697 statement->ms_try.except_expression = create_implicit_cast(expr, type);
7698 rem_anchor_token(')');
7700 statement->ms_try.final_statement = parse_compound_statement(false);
7701 } else if(token.type == T__finally) {
7703 statement->ms_try.final_statement = parse_compound_statement(false);
7705 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
7706 return create_invalid_statement();
7710 return create_invalid_statement();
7714 * Parse a statement.
7715 * There's also parse_statement() which additionally checks for
7716 * "statement has no effect" warnings
7718 static statement_t *intern_parse_statement(void)
7720 statement_t *statement = NULL;
7722 /* declaration or statement */
7723 add_anchor_token(';');
7724 switch(token.type) {
7726 statement = parse_asm_statement();
7730 statement = parse_case_statement();
7734 statement = parse_default_statement();
7738 statement = parse_compound_statement(false);
7742 statement = parse_if();
7746 statement = parse_switch();
7750 statement = parse_while();
7754 statement = parse_do();
7758 statement = parse_for();
7762 statement = parse_goto();
7766 statement = parse_continue();
7770 statement = parse_break();
7774 statement = parse_leave();
7778 statement = parse_return();
7782 if(warning.empty_statement) {
7783 warningf(HERE, "statement is empty");
7785 statement = create_empty_statement();
7790 if(look_ahead(1)->type == ':') {
7791 statement = parse_label_statement();
7795 if(is_typedef_symbol(token.v.symbol)) {
7796 statement = parse_declaration_statement();
7800 statement = parse_expression_statement();
7803 case T___extension__:
7804 /* this can be a prefix to a declaration or an expression statement */
7805 /* we simply eat it now and parse the rest with tail recursion */
7808 } while(token.type == T___extension__);
7809 statement = parse_statement();
7813 statement = parse_declaration_statement();
7817 statement = parse_ms_try_statment();
7821 statement = parse_expression_statement();
7824 rem_anchor_token(';');
7826 assert(statement != NULL
7827 && statement->base.source_position.input_name != NULL);
7833 * parse a statement and emits "statement has no effect" warning if needed
7834 * (This is really a wrapper around intern_parse_statement with check for 1
7835 * single warning. It is needed, because for statement expressions we have
7836 * to avoid the warning on the last statement)
7838 static statement_t *parse_statement(void)
7840 statement_t *statement = intern_parse_statement();
7842 if(statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
7843 expression_t *expression = statement->expression.expression;
7844 if(!expression_has_effect(expression)) {
7845 warningf(&expression->base.source_position,
7846 "statement has no effect");
7854 * Parse a compound statement.
7856 static statement_t *parse_compound_statement(bool inside_expression_statement)
7858 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7860 statement->base.source_position = token.source_position;
7863 add_anchor_token('}');
7865 int top = environment_top();
7866 scope_t *last_scope = scope;
7867 set_scope(&statement->compound.scope);
7869 statement_t *last_statement = NULL;
7871 while(token.type != '}' && token.type != T_EOF) {
7872 statement_t *sub_statement = intern_parse_statement();
7873 if(is_invalid_statement(sub_statement)) {
7874 /* an error occurred. if we are at an anchor, return */
7880 if(last_statement != NULL) {
7881 last_statement->base.next = sub_statement;
7883 statement->compound.statements = sub_statement;
7886 while(sub_statement->base.next != NULL)
7887 sub_statement = sub_statement->base.next;
7889 last_statement = sub_statement;
7892 if(token.type == '}') {
7895 errorf(&statement->base.source_position,
7896 "end of file while looking for closing '}'");
7899 /* look over all statements again to produce no effect warnings */
7900 if(warning.unused_value) {
7901 statement_t *sub_statement = statement->compound.statements;
7902 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
7903 if(sub_statement->kind != STATEMENT_EXPRESSION)
7905 /* don't emit a warning for the last expression in an expression
7906 * statement as it has always an effect */
7907 if(inside_expression_statement && sub_statement->base.next == NULL)
7910 expression_t *expression = sub_statement->expression.expression;
7911 if(!expression_has_effect(expression)) {
7912 warningf(&expression->base.source_position,
7913 "statement has no effect");
7919 rem_anchor_token('}');
7920 assert(scope == &statement->compound.scope);
7921 set_scope(last_scope);
7922 environment_pop_to(top);
7928 * Initialize builtin types.
7930 static void initialize_builtin_types(void)
7932 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7933 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7934 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7935 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7936 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7937 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7938 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7939 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7941 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7942 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7943 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7944 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7948 * Check for unused global static functions and variables
7950 static void check_unused_globals(void)
7952 if (!warning.unused_function && !warning.unused_variable)
7955 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7956 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7959 type_t *const type = decl->type;
7961 if (is_type_function(skip_typeref(type))) {
7962 if (!warning.unused_function || decl->is_inline)
7965 s = (decl->init.statement != NULL ? "defined" : "declared");
7967 if (!warning.unused_variable)
7973 warningf(&decl->source_position, "'%#T' %s but not used",
7974 type, decl->symbol, s);
7979 * Parse a translation unit.
7981 static translation_unit_t *parse_translation_unit(void)
7983 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7985 assert(global_scope == NULL);
7986 global_scope = &unit->scope;
7988 assert(scope == NULL);
7989 set_scope(&unit->scope);
7991 initialize_builtin_types();
7993 while(token.type != T_EOF) {
7994 if (token.type == ';') {
7995 /* TODO error in strict mode */
7996 warningf(HERE, "stray ';' outside of function");
7999 parse_external_declaration();
8003 assert(scope == &unit->scope);
8005 last_declaration = NULL;
8007 assert(global_scope == &unit->scope);
8008 check_unused_globals();
8009 global_scope = NULL;
8017 * @return the translation unit or NULL if errors occurred.
8019 translation_unit_t *parse(void)
8021 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8022 label_stack = NEW_ARR_F(stack_entry_t, 0);
8023 diagnostic_count = 0;
8027 type_set_output(stderr);
8028 ast_set_output(stderr);
8030 lookahead_bufpos = 0;
8031 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8034 translation_unit_t *unit = parse_translation_unit();
8036 DEL_ARR_F(environment_stack);
8037 DEL_ARR_F(label_stack);
8043 * Initialize the parser.
8045 void init_parser(void)
8048 /* add predefined symbols for extended-decl-modifier */
8049 sym_align = symbol_table_insert("align");
8050 sym_allocate = symbol_table_insert("allocate");
8051 sym_dllimport = symbol_table_insert("dllimport");
8052 sym_dllexport = symbol_table_insert("dllexport");
8053 sym_naked = symbol_table_insert("naked");
8054 sym_noinline = symbol_table_insert("noinline");
8055 sym_noreturn = symbol_table_insert("noreturn");
8056 sym_nothrow = symbol_table_insert("nothrow");
8057 sym_novtable = symbol_table_insert("novtable");
8058 sym_property = symbol_table_insert("property");
8059 sym_get = symbol_table_insert("get");
8060 sym_put = symbol_table_insert("put");
8061 sym_selectany = symbol_table_insert("selectany");
8062 sym_thread = symbol_table_insert("thread");
8063 sym_uuid = symbol_table_insert("uuid");
8064 sym_deprecated = symbol_table_insert("deprecated");
8065 sym_restrict = symbol_table_insert("restrict");
8066 sym_noalias = symbol_table_insert("noalias");
8068 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8070 init_expression_parsers();
8071 obstack_init(&temp_obst);
8073 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8074 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8078 * Terminate the parser.
8080 void exit_parser(void)
8082 obstack_free(&temp_obst, NULL);