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"
37 #include "walk_statements.h"
39 #include "adt/bitfiddle.h"
40 #include "adt/error.h"
41 #include "adt/array.h"
43 /** if wchar_t is equal to unsigned short. */
44 bool opt_short_wchar_t =
51 //#define PRINT_TOKENS
52 #define MAX_LOOKAHEAD 2
55 declaration_t *old_declaration;
57 unsigned short namespc;
60 typedef struct argument_list_t argument_list_t;
61 struct argument_list_t {
63 argument_list_t *next;
66 typedef struct gnu_attribute_t gnu_attribute_t;
67 struct gnu_attribute_t {
68 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
69 gnu_attribute_t *next;
70 bool invalid; /**< Set if this attribute had argument errors, */
71 bool have_arguments; /**< True, if this attribute has arguments. */
75 atomic_type_kind_t akind;
76 long argument; /**< Single argument. */
77 argument_list_t *arguments; /**< List of argument expressions. */
81 typedef struct declaration_specifiers_t declaration_specifiers_t;
82 struct declaration_specifiers_t {
83 source_position_t source_position;
84 unsigned char declared_storage_class;
85 unsigned char alignment; /**< Alignment, 0 if not set. */
86 unsigned int is_inline : 1;
87 unsigned int deprecated : 1;
88 decl_modifiers_t modifiers; /**< declaration modifiers */
89 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
90 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
91 symbol_t *get_property_sym; /**< the name of the get property if set. */
92 symbol_t *put_property_sym; /**< the name of the put property if set. */
97 * An environment for parsing initializers (and compound literals).
99 typedef struct parse_initializer_env_t {
100 type_t *type; /**< the type of the initializer. In case of an
101 array type with unspecified size this gets
102 adjusted to the actual size. */
103 declaration_t *declaration; /**< the declaration that is initialized if any */
104 bool must_be_constant;
105 } parse_initializer_env_t;
107 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration, bool is_definition);
109 /** The current token. */
110 static token_t token;
111 /** The lookahead ring-buffer. */
112 static token_t lookahead_buffer[MAX_LOOKAHEAD];
113 /** Position of the next token in the lookahead buffer. */
114 static int lookahead_bufpos;
115 static stack_entry_t *environment_stack = NULL;
116 static stack_entry_t *label_stack = NULL;
117 static stack_entry_t *local_label_stack = NULL;
118 /** The global file scope. */
119 static scope_t *file_scope = NULL;
120 /** The current scope. */
121 static scope_t *scope = NULL;
122 static declaration_t *last_declaration = NULL;
123 /** Point to the current function declaration if inside a function. */
124 static declaration_t *current_function = NULL;
125 static declaration_t *current_init_decl = NULL;
126 static switch_statement_t *current_switch = NULL;
127 static statement_t *current_loop = NULL;
128 static statement_t *current_parent = NULL;
129 static ms_try_statement_t *current_try = NULL;
130 static goto_statement_t *goto_first = NULL;
131 static goto_statement_t *goto_last = NULL;
132 static label_statement_t *label_first = NULL;
133 static label_statement_t *label_last = NULL;
134 /** current translation unit. */
135 static translation_unit_t *unit = NULL;
136 /** true if we are in a type property context (evaluation only for type. */
137 static bool in_type_prop = false;
138 /** true in we are in a __extension__ context. */
139 static bool in_gcc_extension = false;
140 static struct obstack temp_obst;
143 #define PUSH_PARENT(stmt) \
144 statement_t *const prev_parent = current_parent; \
145 ((void)(current_parent = (stmt)))
146 #define POP_PARENT ((void)(current_parent = prev_parent))
148 static source_position_t null_position = { NULL, 0 };
150 /** special symbol used for anonymous entities. */
151 static const symbol_t *sym_anonymous = NULL;
153 /* symbols for Microsoft extended-decl-modifier */
154 static const symbol_t *sym_align = NULL;
155 static const symbol_t *sym_allocate = NULL;
156 static const symbol_t *sym_dllimport = NULL;
157 static const symbol_t *sym_dllexport = NULL;
158 static const symbol_t *sym_naked = NULL;
159 static const symbol_t *sym_noinline = NULL;
160 static const symbol_t *sym_noreturn = NULL;
161 static const symbol_t *sym_nothrow = NULL;
162 static const symbol_t *sym_novtable = NULL;
163 static const symbol_t *sym_property = NULL;
164 static const symbol_t *sym_get = NULL;
165 static const symbol_t *sym_put = NULL;
166 static const symbol_t *sym_selectany = NULL;
167 static const symbol_t *sym_thread = NULL;
168 static const symbol_t *sym_uuid = NULL;
169 static const symbol_t *sym_deprecated = NULL;
170 static const symbol_t *sym_restrict = NULL;
171 static const symbol_t *sym_noalias = NULL;
173 /** The token anchor set */
174 static unsigned char token_anchor_set[T_LAST_TOKEN];
176 /** The current source position. */
177 #define HERE (&token.source_position)
179 /** true if we are in GCC mode. */
180 #define GNU_MODE ((c_mode & _GNUC) || in_gcc_extension)
182 static type_t *type_valist;
184 static statement_t *parse_compound_statement(bool inside_expression_statement);
185 static statement_t *parse_statement(void);
187 typedef enum precedence_t {
207 static expression_t *parse_sub_expression(precedence_t);
208 static expression_t *parse_expression(void);
209 static type_t *parse_typename(void);
211 static void parse_compound_type_entries(declaration_t *compound_declaration);
212 static declaration_t *parse_declarator(
213 const declaration_specifiers_t *specifiers, bool may_be_abstract);
214 static declaration_t *record_declaration(declaration_t *declaration, bool is_definition);
216 static void semantic_comparison(binary_expression_t *expression);
218 #define STORAGE_CLASSES \
226 #define TYPE_QUALIFIERS \
231 case T__forceinline: \
232 case T___attribute__:
234 #ifdef PROVIDE_COMPLEX
235 #define COMPLEX_SPECIFIERS \
237 #define IMAGINARY_SPECIFIERS \
240 #define COMPLEX_SPECIFIERS
241 #define IMAGINARY_SPECIFIERS
244 #define TYPE_SPECIFIERS \
259 case T___builtin_va_list: \
264 #define DECLARATION_START \
269 #define TYPENAME_START \
274 * Allocate an AST node with given size and
275 * initialize all fields with zero.
277 static void *allocate_ast_zero(size_t size)
279 void *res = allocate_ast(size);
280 memset(res, 0, size);
284 static declaration_t *allocate_declaration_zero(void)
286 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
287 declaration->type = type_error_type;
288 declaration->alignment = 0;
293 * Returns the size of a statement node.
295 * @param kind the statement kind
297 static size_t get_statement_struct_size(statement_kind_t kind)
299 static const size_t sizes[] = {
300 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
301 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
302 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
303 [STATEMENT_RETURN] = sizeof(return_statement_t),
304 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
305 [STATEMENT_IF] = sizeof(if_statement_t),
306 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
307 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
308 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
309 [STATEMENT_BREAK] = sizeof(statement_base_t),
310 [STATEMENT_GOTO] = sizeof(goto_statement_t),
311 [STATEMENT_LABEL] = sizeof(label_statement_t),
312 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
313 [STATEMENT_WHILE] = sizeof(while_statement_t),
314 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
315 [STATEMENT_FOR] = sizeof(for_statement_t),
316 [STATEMENT_ASM] = sizeof(asm_statement_t),
317 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
318 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
320 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
321 assert(sizes[kind] != 0);
326 * Returns the size of an expression node.
328 * @param kind the expression kind
330 static size_t get_expression_struct_size(expression_kind_t kind)
332 static const size_t sizes[] = {
333 [EXPR_INVALID] = sizeof(expression_base_t),
334 [EXPR_REFERENCE] = sizeof(reference_expression_t),
335 [EXPR_CONST] = sizeof(const_expression_t),
336 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
337 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
338 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
339 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
340 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
341 [EXPR_CALL] = sizeof(call_expression_t),
342 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
343 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
344 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
345 [EXPR_SELECT] = sizeof(select_expression_t),
346 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
347 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
348 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
349 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
350 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
351 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
352 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
353 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
354 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
355 [EXPR_VA_START] = sizeof(va_start_expression_t),
356 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
357 [EXPR_STATEMENT] = sizeof(statement_expression_t),
358 [EXPR_LABEL_ADDRESS] = sizeof(label_address_expression_t),
360 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
361 return sizes[EXPR_UNARY_FIRST];
363 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
364 return sizes[EXPR_BINARY_FIRST];
366 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
367 assert(sizes[kind] != 0);
372 * Allocate a statement node of given kind and initialize all
375 static statement_t *allocate_statement_zero(statement_kind_t kind)
377 size_t size = get_statement_struct_size(kind);
378 statement_t *res = allocate_ast_zero(size);
380 res->base.kind = kind;
381 res->base.parent = current_parent;
382 res->base.source_position = token.source_position;
387 * Allocate an expression node of given kind and initialize all
390 static expression_t *allocate_expression_zero(expression_kind_t kind)
392 size_t size = get_expression_struct_size(kind);
393 expression_t *res = allocate_ast_zero(size);
395 res->base.kind = kind;
396 res->base.type = type_error_type;
401 * Creates a new invalid expression.
403 static expression_t *create_invalid_expression(void)
405 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
406 expression->base.source_position = token.source_position;
411 * Creates a new invalid statement.
413 static statement_t *create_invalid_statement(void)
415 return allocate_statement_zero(STATEMENT_INVALID);
419 * Allocate a new empty statement.
421 static statement_t *create_empty_statement(void)
423 return allocate_statement_zero(STATEMENT_EMPTY);
427 * Returns the size of a type node.
429 * @param kind the type kind
431 static size_t get_type_struct_size(type_kind_t kind)
433 static const size_t sizes[] = {
434 [TYPE_ATOMIC] = sizeof(atomic_type_t),
435 [TYPE_COMPLEX] = sizeof(complex_type_t),
436 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
437 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
438 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
439 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
440 [TYPE_ENUM] = sizeof(enum_type_t),
441 [TYPE_FUNCTION] = sizeof(function_type_t),
442 [TYPE_POINTER] = sizeof(pointer_type_t),
443 [TYPE_ARRAY] = sizeof(array_type_t),
444 [TYPE_BUILTIN] = sizeof(builtin_type_t),
445 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
446 [TYPE_TYPEOF] = sizeof(typeof_type_t),
448 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
449 assert(kind <= TYPE_TYPEOF);
450 assert(sizes[kind] != 0);
455 * Allocate a type node of given kind and initialize all
458 * @param kind type kind to allocate
459 * @param source_position the source position of the type definition
461 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
463 size_t size = get_type_struct_size(kind);
464 type_t *res = obstack_alloc(type_obst, size);
465 memset(res, 0, size);
467 res->base.kind = kind;
468 res->base.source_position = *source_position;
473 * Returns the size of an initializer node.
475 * @param kind the initializer kind
477 static size_t get_initializer_size(initializer_kind_t kind)
479 static const size_t sizes[] = {
480 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
481 [INITIALIZER_STRING] = sizeof(initializer_string_t),
482 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
483 [INITIALIZER_LIST] = sizeof(initializer_list_t),
484 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
486 assert(kind < sizeof(sizes) / sizeof(*sizes));
487 assert(sizes[kind] != 0);
492 * Allocate an initializer node of given kind and initialize all
495 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
497 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
504 * Free a type from the type obstack.
506 static void free_type(void *type)
508 obstack_free(type_obst, type);
512 * Returns the index of the top element of the environment stack.
514 static size_t environment_top(void)
516 return ARR_LEN(environment_stack);
520 * Returns the index of the top element of the global label stack.
522 static size_t label_top(void)
524 return ARR_LEN(label_stack);
528 * Returns the index of the top element of the local label stack.
530 static size_t local_label_top(void)
532 return ARR_LEN(local_label_stack);
536 * Return the next token.
538 static inline void next_token(void)
540 token = lookahead_buffer[lookahead_bufpos];
541 lookahead_buffer[lookahead_bufpos] = lexer_token;
544 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
547 print_token(stderr, &token);
548 fprintf(stderr, "\n");
553 * Return the next token with a given lookahead.
555 static inline const token_t *look_ahead(int num)
557 assert(num > 0 && num <= MAX_LOOKAHEAD);
558 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
559 return &lookahead_buffer[pos];
563 * Adds a token to the token anchor set (a multi-set).
565 static void add_anchor_token(int token_type)
567 assert(0 <= token_type && token_type < T_LAST_TOKEN);
568 ++token_anchor_set[token_type];
571 static int save_and_reset_anchor_state(int token_type)
573 assert(0 <= token_type && token_type < T_LAST_TOKEN);
574 int count = token_anchor_set[token_type];
575 token_anchor_set[token_type] = 0;
579 static void restore_anchor_state(int token_type, int count)
581 assert(0 <= token_type && token_type < T_LAST_TOKEN);
582 token_anchor_set[token_type] = count;
586 * Remove a token from the token anchor set (a multi-set).
588 static void rem_anchor_token(int token_type)
590 assert(0 <= token_type && token_type < T_LAST_TOKEN);
591 assert(token_anchor_set[token_type] != 0);
592 --token_anchor_set[token_type];
595 static bool at_anchor(void)
599 return token_anchor_set[token.type];
603 * Eat tokens until a matching token is found.
605 static void eat_until_matching_token(int type)
609 case '(': end_token = ')'; break;
610 case '{': end_token = '}'; break;
611 case '[': end_token = ']'; break;
612 default: end_token = type; break;
615 unsigned parenthesis_count = 0;
616 unsigned brace_count = 0;
617 unsigned bracket_count = 0;
618 while (token.type != end_token ||
619 parenthesis_count != 0 ||
621 bracket_count != 0) {
622 switch (token.type) {
624 case '(': ++parenthesis_count; break;
625 case '{': ++brace_count; break;
626 case '[': ++bracket_count; break;
629 if (parenthesis_count > 0)
639 if (bracket_count > 0)
642 if (token.type == end_token &&
643 parenthesis_count == 0 &&
657 * Eat input tokens until an anchor is found.
659 static void eat_until_anchor(void)
661 while (token_anchor_set[token.type] == 0) {
662 if (token.type == '(' || token.type == '{' || token.type == '[')
663 eat_until_matching_token(token.type);
668 static void eat_block(void)
670 eat_until_matching_token('{');
671 if (token.type == '}')
675 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
678 * Report a parse error because an expected token was not found.
681 #if defined __GNUC__ && __GNUC__ >= 4
682 __attribute__((sentinel))
684 void parse_error_expected(const char *message, ...)
686 if (message != NULL) {
687 errorf(HERE, "%s", message);
690 va_start(ap, message);
691 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
696 * Report a type error.
698 static void type_error(const char *msg, const source_position_t *source_position,
701 errorf(source_position, "%s, but found type '%T'", msg, type);
705 * Report an incompatible type.
707 static void type_error_incompatible(const char *msg,
708 const source_position_t *source_position, type_t *type1, type_t *type2)
710 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
715 * Expect the the current token is the expected token.
716 * If not, generate an error, eat the current statement,
717 * and goto the end_error label.
719 #define expect(expected) \
721 if (UNLIKELY(token.type != (expected))) { \
722 parse_error_expected(NULL, (expected), NULL); \
723 add_anchor_token(expected); \
724 eat_until_anchor(); \
725 if (token.type == expected) \
727 rem_anchor_token(expected); \
733 static void scope_push(scope_t *new_scope)
736 scope->last_declaration = last_declaration;
737 new_scope->depth = scope->depth + 1;
739 new_scope->parent = scope;
742 last_declaration = new_scope->last_declaration;
745 static void scope_pop(void)
747 scope->last_declaration = last_declaration;
748 scope = scope->parent;
749 last_declaration = scope->last_declaration;
753 * Search a symbol in a given namespace and returns its declaration or
754 * NULL if this symbol was not found.
756 static declaration_t *get_declaration(const symbol_t *const symbol,
757 const namespace_t namespc)
759 declaration_t *declaration = symbol->declaration;
760 for( ; declaration != NULL; declaration = declaration->symbol_next) {
761 if (declaration->namespc == namespc)
769 * pushs an environment_entry on the environment stack and links the
770 * corresponding symbol to the new entry
772 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
774 symbol_t *symbol = declaration->symbol;
775 namespace_t namespc = (namespace_t) declaration->namespc;
777 /* replace/add declaration into declaration list of the symbol */
778 declaration_t **anchor;
780 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
785 /* replace an entry? */
786 if (iter->namespc == namespc) {
787 declaration->symbol_next = iter->symbol_next;
791 *anchor = declaration;
793 /* remember old declaration */
795 entry.symbol = symbol;
796 entry.old_declaration = iter;
797 entry.namespc = (unsigned short) namespc;
798 ARR_APP1(stack_entry_t, *stack_ptr, entry);
802 * Push a declaration on the environment stack.
804 * @param declaration the declaration
806 static void environment_push(declaration_t *declaration)
808 assert(declaration->source_position.input_name != NULL);
809 assert(declaration->parent_scope != NULL);
810 stack_push(&environment_stack, declaration);
814 * Push a declaration on the global label stack.
816 * @param declaration the declaration
818 static void label_push(declaration_t *declaration)
820 declaration->parent_scope = ¤t_function->scope;
821 stack_push(&label_stack, declaration);
825 * Push a declaration of the local label stack.
827 * @param declaration the declaration
829 static void local_label_push(declaration_t *declaration)
831 assert(declaration->parent_scope != NULL);
832 stack_push(&local_label_stack, declaration);
836 * pops symbols from the environment stack until @p new_top is the top element
838 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
840 stack_entry_t *stack = *stack_ptr;
841 size_t top = ARR_LEN(stack);
844 assert(new_top <= top);
848 for(i = top; i > new_top; --i) {
849 stack_entry_t *entry = &stack[i - 1];
851 declaration_t *old_declaration = entry->old_declaration;
852 symbol_t *symbol = entry->symbol;
853 namespace_t namespc = (namespace_t)entry->namespc;
855 /* replace/remove declaration */
856 declaration_t **anchor;
858 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
860 assert(iter != NULL);
861 /* replace an entry? */
862 if (iter->namespc == namespc)
866 /* Not all declarations adhere scopes (e.g. jump labels), so this
867 * correction is necessary */
868 if (old_declaration != NULL) {
869 old_declaration->symbol_next = iter->symbol_next;
870 *anchor = old_declaration;
872 *anchor = iter->symbol_next;
876 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
880 * Pop all entries from the environment stack until the new_top
883 * @param new_top the new stack top
885 static void environment_pop_to(size_t new_top)
887 stack_pop_to(&environment_stack, new_top);
891 * Pop all entries from the global label stack until the new_top
894 * @param new_top the new stack top
896 static void label_pop_to(size_t new_top)
898 stack_pop_to(&label_stack, new_top);
902 * Pop all entries from the local label stack until the new_top
905 * @param new_top the new stack top
907 static void local_label_pop_to(size_t new_top)
909 stack_pop_to(&local_label_stack, new_top);
913 static int get_akind_rank(atomic_type_kind_t akind)
918 static int get_rank(const type_t *type)
920 assert(!is_typeref(type));
921 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
922 * and esp. footnote 108). However we can't fold constants (yet), so we
923 * can't decide whether unsigned int is possible, while int always works.
924 * (unsigned int would be preferable when possible... for stuff like
925 * struct { enum { ... } bla : 4; } ) */
926 if (type->kind == TYPE_ENUM)
927 return get_akind_rank(ATOMIC_TYPE_INT);
929 assert(type->kind == TYPE_ATOMIC);
930 return get_akind_rank(type->atomic.akind);
933 static type_t *promote_integer(type_t *type)
935 if (type->kind == TYPE_BITFIELD)
936 type = type->bitfield.base_type;
938 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
945 * Create a cast expression.
947 * @param expression the expression to cast
948 * @param dest_type the destination type
950 static expression_t *create_cast_expression(expression_t *expression,
953 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
955 cast->unary.value = expression;
956 cast->base.type = dest_type;
962 * Check if a given expression represents the 0 pointer constant.
964 static bool is_null_pointer_constant(const expression_t *expression)
966 /* skip void* cast */
967 if (expression->kind == EXPR_UNARY_CAST
968 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
969 expression = expression->unary.value;
972 /* TODO: not correct yet, should be any constant integer expression
973 * which evaluates to 0 */
974 if (expression->kind != EXPR_CONST)
977 type_t *const type = skip_typeref(expression->base.type);
978 if (!is_type_integer(type))
981 return expression->conste.v.int_value == 0;
985 * Create an implicit cast expression.
987 * @param expression the expression to cast
988 * @param dest_type the destination type
990 static expression_t *create_implicit_cast(expression_t *expression,
993 type_t *const source_type = expression->base.type;
995 if (source_type == dest_type)
998 return create_cast_expression(expression, dest_type);
1001 typedef enum assign_error_t {
1003 ASSIGN_ERROR_INCOMPATIBLE,
1004 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
1005 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
1006 ASSIGN_WARNING_POINTER_FROM_INT,
1007 ASSIGN_WARNING_INT_FROM_POINTER
1010 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
1011 const expression_t *const right,
1012 const char *context,
1013 const source_position_t *source_position)
1015 type_t *const orig_type_right = right->base.type;
1016 type_t *const type_left = skip_typeref(orig_type_left);
1017 type_t *const type_right = skip_typeref(orig_type_right);
1020 case ASSIGN_SUCCESS:
1022 case ASSIGN_ERROR_INCOMPATIBLE:
1023 errorf(source_position,
1024 "destination type '%T' in %s is incompatible with type '%T'",
1025 orig_type_left, context, orig_type_right);
1028 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
1029 type_t *points_to_left
1030 = skip_typeref(type_left->pointer.points_to);
1031 type_t *points_to_right
1032 = skip_typeref(type_right->pointer.points_to);
1034 /* the left type has all qualifiers from the right type */
1035 unsigned missing_qualifiers
1036 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1037 warningf(source_position,
1038 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
1039 orig_type_left, context, orig_type_right, missing_qualifiers);
1043 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
1044 warningf(source_position,
1045 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
1046 orig_type_left, context, right, orig_type_right);
1049 case ASSIGN_WARNING_POINTER_FROM_INT:
1050 warningf(source_position,
1051 "%s makes pointer '%T' from integer '%T' without a cast",
1052 context, orig_type_left, orig_type_right);
1055 case ASSIGN_WARNING_INT_FROM_POINTER:
1056 warningf(source_position,
1057 "%s makes integer '%T' from pointer '%T' without a cast",
1058 context, orig_type_left, orig_type_right);
1062 panic("invalid error value");
1066 /** Implements the rules from § 6.5.16.1 */
1067 static assign_error_t semantic_assign(type_t *orig_type_left,
1068 const expression_t *const right)
1070 type_t *const orig_type_right = right->base.type;
1071 type_t *const type_left = skip_typeref(orig_type_left);
1072 type_t *const type_right = skip_typeref(orig_type_right);
1074 if (is_type_pointer(type_left)) {
1075 if (is_null_pointer_constant(right)) {
1076 return ASSIGN_SUCCESS;
1077 } else if (is_type_pointer(type_right)) {
1078 type_t *points_to_left
1079 = skip_typeref(type_left->pointer.points_to);
1080 type_t *points_to_right
1081 = skip_typeref(type_right->pointer.points_to);
1082 assign_error_t res = ASSIGN_SUCCESS;
1084 /* the left type has all qualifiers from the right type */
1085 unsigned missing_qualifiers
1086 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1087 if (missing_qualifiers != 0) {
1088 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1091 points_to_left = get_unqualified_type(points_to_left);
1092 points_to_right = get_unqualified_type(points_to_right);
1094 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1095 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1099 if (!types_compatible(points_to_left, points_to_right)) {
1100 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1104 } else if (is_type_integer(type_right)) {
1105 return ASSIGN_WARNING_POINTER_FROM_INT;
1107 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1108 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1109 && is_type_pointer(type_right))) {
1110 return ASSIGN_SUCCESS;
1111 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1112 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1113 type_t *const unqual_type_left = get_unqualified_type(type_left);
1114 type_t *const unqual_type_right = get_unqualified_type(type_right);
1115 if (types_compatible(unqual_type_left, unqual_type_right)) {
1116 return ASSIGN_SUCCESS;
1118 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1119 return ASSIGN_WARNING_INT_FROM_POINTER;
1122 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1123 return ASSIGN_SUCCESS;
1125 return ASSIGN_ERROR_INCOMPATIBLE;
1128 static expression_t *parse_constant_expression(void)
1130 expression_t *result = parse_sub_expression(PREC_CONDITIONAL);
1132 if (!is_constant_expression(result)) {
1133 errorf(&result->base.source_position,
1134 "expression '%E' is not constant\n", result);
1140 static expression_t *parse_assignment_expression(void)
1142 return parse_sub_expression(PREC_ASSIGNMENT);
1145 static type_t *make_global_typedef(const char *name, type_t *type)
1147 symbol_t *const symbol = symbol_table_insert(name);
1149 declaration_t *const declaration = allocate_declaration_zero();
1150 declaration->namespc = NAMESPACE_NORMAL;
1151 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1152 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1153 declaration->type = type;
1154 declaration->symbol = symbol;
1155 declaration->source_position = builtin_source_position;
1156 declaration->implicit = true;
1158 record_declaration(declaration, false);
1160 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1161 typedef_type->typedeft.declaration = declaration;
1163 return typedef_type;
1166 static string_t parse_string_literals(void)
1168 assert(token.type == T_STRING_LITERAL);
1169 string_t result = token.v.string;
1173 while (token.type == T_STRING_LITERAL) {
1174 result = concat_strings(&result, &token.v.string);
1181 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1182 [GNU_AK_CONST] = "const",
1183 [GNU_AK_VOLATILE] = "volatile",
1184 [GNU_AK_CDECL] = "cdecl",
1185 [GNU_AK_STDCALL] = "stdcall",
1186 [GNU_AK_FASTCALL] = "fastcall",
1187 [GNU_AK_DEPRECATED] = "deprecated",
1188 [GNU_AK_NOINLINE] = "noinline",
1189 [GNU_AK_NORETURN] = "noreturn",
1190 [GNU_AK_NAKED] = "naked",
1191 [GNU_AK_PURE] = "pure",
1192 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1193 [GNU_AK_MALLOC] = "malloc",
1194 [GNU_AK_WEAK] = "weak",
1195 [GNU_AK_CONSTRUCTOR] = "constructor",
1196 [GNU_AK_DESTRUCTOR] = "destructor",
1197 [GNU_AK_NOTHROW] = "nothrow",
1198 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1199 [GNU_AK_COMMON] = "common",
1200 [GNU_AK_NOCOMMON] = "nocommon",
1201 [GNU_AK_PACKED] = "packed",
1202 [GNU_AK_SHARED] = "shared",
1203 [GNU_AK_NOTSHARED] = "notshared",
1204 [GNU_AK_USED] = "used",
1205 [GNU_AK_UNUSED] = "unused",
1206 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1207 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1208 [GNU_AK_LONGCALL] = "longcall",
1209 [GNU_AK_SHORTCALL] = "shortcall",
1210 [GNU_AK_LONG_CALL] = "long_call",
1211 [GNU_AK_SHORT_CALL] = "short_call",
1212 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1213 [GNU_AK_INTERRUPT] = "interrupt",
1214 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1215 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1216 [GNU_AK_NESTING] = "nesting",
1217 [GNU_AK_NEAR] = "near",
1218 [GNU_AK_FAR] = "far",
1219 [GNU_AK_SIGNAL] = "signal",
1220 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1221 [GNU_AK_TINY_DATA] = "tiny_data",
1222 [GNU_AK_SAVEALL] = "saveall",
1223 [GNU_AK_FLATTEN] = "flatten",
1224 [GNU_AK_SSEREGPARM] = "sseregparm",
1225 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1226 [GNU_AK_RETURN_TWICE] = "return_twice",
1227 [GNU_AK_MAY_ALIAS] = "may_alias",
1228 [GNU_AK_MS_STRUCT] = "ms_struct",
1229 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1230 [GNU_AK_DLLIMPORT] = "dllimport",
1231 [GNU_AK_DLLEXPORT] = "dllexport",
1232 [GNU_AK_ALIGNED] = "aligned",
1233 [GNU_AK_ALIAS] = "alias",
1234 [GNU_AK_SECTION] = "section",
1235 [GNU_AK_FORMAT] = "format",
1236 [GNU_AK_FORMAT_ARG] = "format_arg",
1237 [GNU_AK_WEAKREF] = "weakref",
1238 [GNU_AK_NONNULL] = "nonnull",
1239 [GNU_AK_TLS_MODEL] = "tls_model",
1240 [GNU_AK_VISIBILITY] = "visibility",
1241 [GNU_AK_REGPARM] = "regparm",
1242 [GNU_AK_MODE] = "mode",
1243 [GNU_AK_MODEL] = "model",
1244 [GNU_AK_TRAP_EXIT] = "trap_exit",
1245 [GNU_AK_SP_SWITCH] = "sp_switch",
1246 [GNU_AK_SENTINEL] = "sentinel"
1250 * compare two string, ignoring double underscores on the second.
1252 static int strcmp_underscore(const char *s1, const char *s2)
1254 if (s2[0] == '_' && s2[1] == '_') {
1255 size_t len2 = strlen(s2);
1256 size_t len1 = strlen(s1);
1257 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1258 return strncmp(s1, s2+2, len2-4);
1262 return strcmp(s1, s2);
1266 * Allocate a new gnu temporal attribute.
1268 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1270 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1271 attribute->kind = kind;
1272 attribute->next = NULL;
1273 attribute->invalid = false;
1274 attribute->have_arguments = false;
1280 * parse one constant expression argument.
1282 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1284 expression_t *expression;
1285 add_anchor_token(')');
1286 expression = parse_constant_expression();
1287 rem_anchor_token(')');
1289 attribute->u.argument = fold_constant(expression);
1292 attribute->invalid = true;
1296 * parse a list of constant expressions arguments.
1298 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1300 argument_list_t **list = &attribute->u.arguments;
1301 argument_list_t *entry;
1302 expression_t *expression;
1303 add_anchor_token(')');
1304 add_anchor_token(',');
1306 expression = parse_constant_expression();
1307 entry = obstack_alloc(&temp_obst, sizeof(entry));
1308 entry->argument = fold_constant(expression);
1311 list = &entry->next;
1312 if (token.type != ',')
1316 rem_anchor_token(',');
1317 rem_anchor_token(')');
1321 attribute->invalid = true;
1325 * parse one string literal argument.
1327 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1330 add_anchor_token('(');
1331 if (token.type != T_STRING_LITERAL) {
1332 parse_error_expected("while parsing attribute directive",
1333 T_STRING_LITERAL, NULL);
1336 *string = parse_string_literals();
1337 rem_anchor_token('(');
1341 attribute->invalid = true;
1345 * parse one tls model.
1347 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1349 static const char *const tls_models[] = {
1355 string_t string = { NULL, 0 };
1356 parse_gnu_attribute_string_arg(attribute, &string);
1357 if (string.begin != NULL) {
1358 for(size_t i = 0; i < 4; ++i) {
1359 if (strcmp(tls_models[i], string.begin) == 0) {
1360 attribute->u.value = i;
1364 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1366 attribute->invalid = true;
1370 * parse one tls model.
1372 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1374 static const char *const visibilities[] = {
1380 string_t string = { NULL, 0 };
1381 parse_gnu_attribute_string_arg(attribute, &string);
1382 if (string.begin != NULL) {
1383 for(size_t i = 0; i < 4; ++i) {
1384 if (strcmp(visibilities[i], string.begin) == 0) {
1385 attribute->u.value = i;
1389 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1391 attribute->invalid = true;
1395 * parse one (code) model.
1397 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1399 static const char *const visibilities[] = {
1404 string_t string = { NULL, 0 };
1405 parse_gnu_attribute_string_arg(attribute, &string);
1406 if (string.begin != NULL) {
1407 for(int i = 0; i < 3; ++i) {
1408 if (strcmp(visibilities[i], string.begin) == 0) {
1409 attribute->u.value = i;
1413 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1415 attribute->invalid = true;
1418 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1420 /* TODO: find out what is allowed here... */
1422 /* at least: byte, word, pointer, list of machine modes
1423 * __XXX___ is interpreted as XXX */
1424 add_anchor_token(')');
1426 if (token.type != T_IDENTIFIER) {
1427 expect(T_IDENTIFIER);
1430 /* This isn't really correct, the backend should provide a list of machine
1431 * specific modes (according to gcc philosophy that is...) */
1432 const char *symbol_str = token.v.symbol->string;
1433 if (strcmp_underscore("QI", symbol_str) == 0 ||
1434 strcmp_underscore("byte", symbol_str) == 0) {
1435 attribute->u.akind = ATOMIC_TYPE_CHAR;
1436 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1437 attribute->u.akind = ATOMIC_TYPE_SHORT;
1438 } else if (strcmp_underscore("SI", symbol_str) == 0
1439 || strcmp_underscore("word", symbol_str) == 0
1440 || strcmp_underscore("pointer", symbol_str) == 0) {
1441 attribute->u.akind = ATOMIC_TYPE_INT;
1442 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1443 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1445 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1446 attribute->invalid = true;
1450 rem_anchor_token(')');
1454 attribute->invalid = true;
1458 * parse one interrupt argument.
1460 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1462 static const char *const interrupts[] = {
1469 string_t string = { NULL, 0 };
1470 parse_gnu_attribute_string_arg(attribute, &string);
1471 if (string.begin != NULL) {
1472 for(size_t i = 0; i < 5; ++i) {
1473 if (strcmp(interrupts[i], string.begin) == 0) {
1474 attribute->u.value = i;
1478 errorf(HERE, "'%s' is not an interrupt", string.begin);
1480 attribute->invalid = true;
1484 * parse ( identifier, const expression, const expression )
1486 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1488 static const char *const format_names[] = {
1496 if (token.type != T_IDENTIFIER) {
1497 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1500 const char *name = token.v.symbol->string;
1501 for(i = 0; i < 4; ++i) {
1502 if (strcmp_underscore(format_names[i], name) == 0)
1506 if (warning.attribute)
1507 warningf(HERE, "'%s' is an unrecognized format function type", name);
1512 add_anchor_token(')');
1513 add_anchor_token(',');
1514 parse_constant_expression();
1515 rem_anchor_token(',');
1516 rem_anchor_token(')');
1519 add_anchor_token(')');
1520 parse_constant_expression();
1521 rem_anchor_token(')');
1525 attribute->u.value = true;
1528 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1530 if (!attribute->have_arguments)
1533 /* should have no arguments */
1534 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1535 eat_until_matching_token('(');
1536 /* we have already consumed '(', so we stop before ')', eat it */
1538 attribute->invalid = true;
1542 * Parse one GNU attribute.
1544 * Note that attribute names can be specified WITH or WITHOUT
1545 * double underscores, ie const or __const__.
1547 * The following attributes are parsed without arguments
1572 * no_instrument_function
1573 * warn_unused_result
1590 * externally_visible
1598 * The following attributes are parsed with arguments
1599 * aligned( const expression )
1600 * alias( string literal )
1601 * section( string literal )
1602 * format( identifier, const expression, const expression )
1603 * format_arg( const expression )
1604 * tls_model( string literal )
1605 * visibility( string literal )
1606 * regparm( const expression )
1607 * model( string leteral )
1608 * trap_exit( const expression )
1609 * sp_switch( string literal )
1611 * The following attributes might have arguments
1612 * weak_ref( string literal )
1613 * non_null( const expression // ',' )
1614 * interrupt( string literal )
1615 * sentinel( constant expression )
1617 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1619 gnu_attribute_t *head = *attributes;
1620 gnu_attribute_t *last = *attributes;
1621 decl_modifiers_t modifiers = 0;
1622 gnu_attribute_t *attribute;
1624 eat(T___attribute__);
1628 if (token.type != ')') {
1629 /* find the end of the list */
1631 while (last->next != NULL)
1635 /* non-empty attribute list */
1638 if (token.type == T_const) {
1640 } else if (token.type == T_volatile) {
1642 } else if (token.type == T_cdecl) {
1643 /* __attribute__((cdecl)), WITH ms mode */
1645 } else if (token.type == T_IDENTIFIER) {
1646 const symbol_t *sym = token.v.symbol;
1649 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1656 for(i = 0; i < GNU_AK_LAST; ++i) {
1657 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1660 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1663 if (kind == GNU_AK_LAST) {
1664 if (warning.attribute)
1665 warningf(HERE, "'%s' attribute directive ignored", name);
1667 /* skip possible arguments */
1668 if (token.type == '(') {
1669 eat_until_matching_token(')');
1672 /* check for arguments */
1673 attribute = allocate_gnu_attribute(kind);
1674 if (token.type == '(') {
1676 if (token.type == ')') {
1677 /* empty args are allowed */
1680 attribute->have_arguments = true;
1684 case GNU_AK_VOLATILE:
1689 case GNU_AK_NOCOMMON:
1691 case GNU_AK_NOTSHARED:
1692 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1693 case GNU_AK_WARN_UNUSED_RESULT:
1694 case GNU_AK_LONGCALL:
1695 case GNU_AK_SHORTCALL:
1696 case GNU_AK_LONG_CALL:
1697 case GNU_AK_SHORT_CALL:
1698 case GNU_AK_FUNCTION_VECTOR:
1699 case GNU_AK_INTERRUPT_HANDLER:
1700 case GNU_AK_NMI_HANDLER:
1701 case GNU_AK_NESTING:
1705 case GNU_AK_EIGTHBIT_DATA:
1706 case GNU_AK_TINY_DATA:
1707 case GNU_AK_SAVEALL:
1708 case GNU_AK_FLATTEN:
1709 case GNU_AK_SSEREGPARM:
1710 case GNU_AK_EXTERNALLY_VISIBLE:
1711 case GNU_AK_RETURN_TWICE:
1712 case GNU_AK_MAY_ALIAS:
1713 case GNU_AK_MS_STRUCT:
1714 case GNU_AK_GCC_STRUCT:
1717 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1718 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1719 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1720 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1721 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1722 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1723 case GNU_AK_CONST: modifiers |= DM_CONST; goto no_arg;
1724 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1725 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1726 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1727 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1728 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1729 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1730 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1731 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1732 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1733 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1734 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1736 case GNU_AK_ALIGNED:
1737 /* __align__ may be used without an argument */
1738 if (attribute->have_arguments) {
1739 parse_gnu_attribute_const_arg(attribute);
1743 case GNU_AK_FORMAT_ARG:
1744 case GNU_AK_REGPARM:
1745 case GNU_AK_TRAP_EXIT:
1746 if (!attribute->have_arguments) {
1747 /* should have arguments */
1748 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1749 attribute->invalid = true;
1751 parse_gnu_attribute_const_arg(attribute);
1754 case GNU_AK_SECTION:
1755 case GNU_AK_SP_SWITCH:
1756 if (!attribute->have_arguments) {
1757 /* should have arguments */
1758 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1759 attribute->invalid = true;
1761 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1764 if (!attribute->have_arguments) {
1765 /* should have arguments */
1766 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1767 attribute->invalid = true;
1769 parse_gnu_attribute_format_args(attribute);
1771 case GNU_AK_WEAKREF:
1772 /* may have one string argument */
1773 if (attribute->have_arguments)
1774 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1776 case GNU_AK_NONNULL:
1777 if (attribute->have_arguments)
1778 parse_gnu_attribute_const_arg_list(attribute);
1780 case GNU_AK_TLS_MODEL:
1781 if (!attribute->have_arguments) {
1782 /* should have arguments */
1783 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1785 parse_gnu_attribute_tls_model_arg(attribute);
1787 case GNU_AK_VISIBILITY:
1788 if (!attribute->have_arguments) {
1789 /* should have arguments */
1790 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1792 parse_gnu_attribute_visibility_arg(attribute);
1795 if (!attribute->have_arguments) {
1796 /* should have arguments */
1797 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1799 parse_gnu_attribute_model_arg(attribute);
1803 if (!attribute->have_arguments) {
1804 /* should have arguments */
1805 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1807 parse_gnu_attribute_mode_arg(attribute);
1810 case GNU_AK_INTERRUPT:
1811 /* may have one string argument */
1812 if (attribute->have_arguments)
1813 parse_gnu_attribute_interrupt_arg(attribute);
1815 case GNU_AK_SENTINEL:
1816 /* may have one string argument */
1817 if (attribute->have_arguments)
1818 parse_gnu_attribute_const_arg(attribute);
1821 /* already handled */
1825 check_no_argument(attribute, name);
1828 if (attribute != NULL) {
1830 last->next = attribute;
1833 head = last = attribute;
1837 if (token.type != ',')
1851 * Parse GNU attributes.
1853 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1855 decl_modifiers_t modifiers = 0;
1858 switch(token.type) {
1859 case T___attribute__:
1860 modifiers |= parse_gnu_attribute(attributes);
1866 if (token.type != T_STRING_LITERAL) {
1867 parse_error_expected("while parsing assembler attribute",
1868 T_STRING_LITERAL, NULL);
1869 eat_until_matching_token('(');
1872 parse_string_literals();
1877 case T_cdecl: modifiers |= DM_CDECL; break;
1878 case T__fastcall: modifiers |= DM_FASTCALL; break;
1879 case T__stdcall: modifiers |= DM_STDCALL; break;
1882 /* TODO record modifier */
1883 warningf(HERE, "Ignoring declaration modifier %K", &token);
1887 default: return modifiers;
1894 static void mark_decls_read(expression_t *expr, declaration_t *lhs_decl);
1896 static declaration_t *determine_lhs_decl(expression_t *const expr, declaration_t *lhs_decl)
1898 switch (expr->kind) {
1899 case EXPR_REFERENCE: {
1900 declaration_t *const decl = expr->reference.declaration;
1904 case EXPR_ARRAY_ACCESS: {
1905 expression_t *const ref = expr->array_access.array_ref;
1906 declaration_t * decl = NULL;
1907 if (is_type_array(skip_typeref(revert_automatic_type_conversion(ref)))) {
1908 decl = determine_lhs_decl(ref, lhs_decl);
1911 mark_decls_read(expr->select.compound, lhs_decl);
1913 mark_decls_read(expr->array_access.index, lhs_decl);
1918 if (is_type_compound(skip_typeref(expr->base.type))) {
1919 return determine_lhs_decl(expr->select.compound, lhs_decl);
1921 mark_decls_read(expr->select.compound, lhs_decl);
1926 case EXPR_UNARY_DEREFERENCE: {
1927 expression_t *const val = expr->unary.value;
1928 if (val->kind == EXPR_UNARY_TAKE_ADDRESS) {
1930 return determine_lhs_decl(val->unary.value, lhs_decl);
1932 mark_decls_read(val, NULL);
1938 mark_decls_read(expr, NULL);
1943 #define DECL_ANY ((declaration_t*)-1)
1946 * Mark declarations, which are read. This is used to deted variables, which
1950 * x is not marked as "read", because it is only read to calculate its own new
1954 * x and y are not detected as "not read", because multiple variables are
1957 static void mark_decls_read(expression_t *const expr, declaration_t *lhs_decl)
1959 switch (expr->kind) {
1960 case EXPR_REFERENCE: {
1961 declaration_t *const decl = expr->reference.declaration;
1962 if (lhs_decl != decl && lhs_decl != DECL_ANY)
1968 // TODO respect pure/const
1969 mark_decls_read(expr->call.function, NULL);
1970 for (call_argument_t *arg = expr->call.arguments; arg != NULL; arg = arg->next) {
1971 mark_decls_read(arg->expression, NULL);
1975 case EXPR_CONDITIONAL:
1976 // TODO lhs_decl should depend on whether true/false have an effect
1977 mark_decls_read(expr->conditional.condition, NULL);
1978 if (expr->conditional.true_expression != NULL)
1979 mark_decls_read(expr->conditional.true_expression, lhs_decl);
1980 mark_decls_read(expr->conditional.false_expression, lhs_decl);
1984 if (lhs_decl == DECL_ANY && !is_type_compound(skip_typeref(expr->base.type)))
1986 mark_decls_read(expr->select.compound, lhs_decl);
1989 case EXPR_ARRAY_ACCESS: {
1990 expression_t *const ref = expr->array_access.array_ref;
1991 mark_decls_read(ref, lhs_decl);
1992 lhs_decl = determine_lhs_decl(ref, lhs_decl);
1993 mark_decls_read(expr->array_access.index, lhs_decl);
1998 mark_decls_read(expr->va_arge.ap, lhs_decl);
2001 case EXPR_UNARY_CAST:
2002 /* Special case: Use void cast to mark a variable as "read" */
2003 if (is_type_atomic(skip_typeref(expr->base.type), ATOMIC_TYPE_VOID))
2007 case EXPR_UNARY_DEREFERENCE:
2008 if (lhs_decl == DECL_ANY)
2012 case EXPR_UNARY_NEGATE:
2013 case EXPR_UNARY_PLUS:
2014 case EXPR_UNARY_BITWISE_NEGATE:
2015 case EXPR_UNARY_NOT:
2016 case EXPR_UNARY_TAKE_ADDRESS:
2017 case EXPR_UNARY_POSTFIX_INCREMENT:
2018 case EXPR_UNARY_POSTFIX_DECREMENT:
2019 case EXPR_UNARY_PREFIX_INCREMENT:
2020 case EXPR_UNARY_PREFIX_DECREMENT:
2021 case EXPR_UNARY_CAST_IMPLICIT:
2022 case EXPR_UNARY_ASSUME:
2024 mark_decls_read(expr->unary.value, lhs_decl);
2027 case EXPR_BINARY_ADD:
2028 case EXPR_BINARY_SUB:
2029 case EXPR_BINARY_MUL:
2030 case EXPR_BINARY_DIV:
2031 case EXPR_BINARY_MOD:
2032 case EXPR_BINARY_EQUAL:
2033 case EXPR_BINARY_NOTEQUAL:
2034 case EXPR_BINARY_LESS:
2035 case EXPR_BINARY_LESSEQUAL:
2036 case EXPR_BINARY_GREATER:
2037 case EXPR_BINARY_GREATEREQUAL:
2038 case EXPR_BINARY_BITWISE_AND:
2039 case EXPR_BINARY_BITWISE_OR:
2040 case EXPR_BINARY_BITWISE_XOR:
2041 case EXPR_BINARY_LOGICAL_AND:
2042 case EXPR_BINARY_LOGICAL_OR:
2043 case EXPR_BINARY_SHIFTLEFT:
2044 case EXPR_BINARY_SHIFTRIGHT:
2045 case EXPR_BINARY_COMMA:
2046 case EXPR_BINARY_ISGREATER:
2047 case EXPR_BINARY_ISGREATEREQUAL:
2048 case EXPR_BINARY_ISLESS:
2049 case EXPR_BINARY_ISLESSEQUAL:
2050 case EXPR_BINARY_ISLESSGREATER:
2051 case EXPR_BINARY_ISUNORDERED:
2052 mark_decls_read(expr->binary.left, lhs_decl);
2053 mark_decls_read(expr->binary.right, lhs_decl);
2056 case EXPR_BINARY_ASSIGN:
2057 case EXPR_BINARY_MUL_ASSIGN:
2058 case EXPR_BINARY_DIV_ASSIGN:
2059 case EXPR_BINARY_MOD_ASSIGN:
2060 case EXPR_BINARY_ADD_ASSIGN:
2061 case EXPR_BINARY_SUB_ASSIGN:
2062 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2063 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2064 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2065 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2066 case EXPR_BINARY_BITWISE_OR_ASSIGN: {
2067 if (lhs_decl == DECL_ANY)
2069 lhs_decl = determine_lhs_decl(expr->binary.left, lhs_decl);
2070 mark_decls_read(expr->binary.right, lhs_decl);
2075 determine_lhs_decl(expr->va_starte.ap, lhs_decl);
2081 case EXPR_CHARACTER_CONSTANT:
2082 case EXPR_WIDE_CHARACTER_CONSTANT:
2083 case EXPR_STRING_LITERAL:
2084 case EXPR_WIDE_STRING_LITERAL:
2085 case EXPR_COMPOUND_LITERAL: // TODO init?
2087 case EXPR_CLASSIFY_TYPE:
2090 case EXPR_BUILTIN_SYMBOL:
2091 case EXPR_BUILTIN_CONSTANT_P:
2092 case EXPR_BUILTIN_PREFETCH:
2094 case EXPR_STATEMENT: // TODO
2095 case EXPR_LABEL_ADDRESS:
2096 case EXPR_BINARY_BUILTIN_EXPECT:
2100 panic("unhandled expression");
2103 static designator_t *parse_designation(void)
2105 designator_t *result = NULL;
2106 designator_t *last = NULL;
2109 designator_t *designator;
2110 switch(token.type) {
2112 designator = allocate_ast_zero(sizeof(designator[0]));
2113 designator->source_position = token.source_position;
2115 add_anchor_token(']');
2116 designator->array_index = parse_constant_expression();
2117 rem_anchor_token(']');
2121 designator = allocate_ast_zero(sizeof(designator[0]));
2122 designator->source_position = token.source_position;
2124 if (token.type != T_IDENTIFIER) {
2125 parse_error_expected("while parsing designator",
2126 T_IDENTIFIER, NULL);
2129 designator->symbol = token.v.symbol;
2137 assert(designator != NULL);
2139 last->next = designator;
2141 result = designator;
2149 static initializer_t *initializer_from_string(array_type_t *type,
2150 const string_t *const string)
2152 /* TODO: check len vs. size of array type */
2155 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
2156 initializer->string.string = *string;
2161 static initializer_t *initializer_from_wide_string(array_type_t *const type,
2162 wide_string_t *const string)
2164 /* TODO: check len vs. size of array type */
2167 initializer_t *const initializer =
2168 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
2169 initializer->wide_string.string = *string;
2175 * Build an initializer from a given expression.
2177 static initializer_t *initializer_from_expression(type_t *orig_type,
2178 expression_t *expression)
2180 /* TODO check that expression is a constant expression */
2182 /* § 6.7.8.14/15 char array may be initialized by string literals */
2183 type_t *type = skip_typeref(orig_type);
2184 type_t *expr_type_orig = expression->base.type;
2185 type_t *expr_type = skip_typeref(expr_type_orig);
2186 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
2187 array_type_t *const array_type = &type->array;
2188 type_t *const element_type = skip_typeref(array_type->element_type);
2190 if (element_type->kind == TYPE_ATOMIC) {
2191 atomic_type_kind_t akind = element_type->atomic.akind;
2192 switch (expression->kind) {
2193 case EXPR_STRING_LITERAL:
2194 if (akind == ATOMIC_TYPE_CHAR
2195 || akind == ATOMIC_TYPE_SCHAR
2196 || akind == ATOMIC_TYPE_UCHAR) {
2197 return initializer_from_string(array_type,
2198 &expression->string.value);
2201 case EXPR_WIDE_STRING_LITERAL: {
2202 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
2203 if (get_unqualified_type(element_type) == bare_wchar_type) {
2204 return initializer_from_wide_string(array_type,
2205 &expression->wide_string.value);
2215 assign_error_t error = semantic_assign(type, expression);
2216 if (error == ASSIGN_ERROR_INCOMPATIBLE)
2218 report_assign_error(error, type, expression, "initializer",
2219 &expression->base.source_position);
2221 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
2223 if (type->kind == TYPE_BITFIELD) {
2224 type = type->bitfield.base_type;
2227 result->value.value = create_implicit_cast(expression, type);
2233 * Checks if a given expression can be used as an constant initializer.
2235 static bool is_initializer_constant(const expression_t *expression)
2237 return is_constant_expression(expression)
2238 || is_address_constant(expression);
2242 * Parses an scalar initializer.
2244 * § 6.7.8.11; eat {} without warning
2246 static initializer_t *parse_scalar_initializer(type_t *type,
2247 bool must_be_constant)
2249 /* there might be extra {} hierarchies */
2251 if (token.type == '{') {
2252 warningf(HERE, "extra curly braces around scalar initializer");
2256 } while (token.type == '{');
2259 expression_t *expression = parse_assignment_expression();
2260 mark_decls_read(expression, NULL);
2261 if (must_be_constant && !is_initializer_constant(expression)) {
2262 errorf(&expression->base.source_position,
2263 "Initialisation expression '%E' is not constant\n",
2267 initializer_t *initializer = initializer_from_expression(type, expression);
2269 if (initializer == NULL) {
2270 errorf(&expression->base.source_position,
2271 "expression '%E' (type '%T') doesn't match expected type '%T'",
2272 expression, expression->base.type, type);
2277 bool additional_warning_displayed = false;
2278 while (braces > 0) {
2279 if (token.type == ',') {
2282 if (token.type != '}') {
2283 if (!additional_warning_displayed) {
2284 warningf(HERE, "additional elements in scalar initializer");
2285 additional_warning_displayed = true;
2296 * An entry in the type path.
2298 typedef struct type_path_entry_t type_path_entry_t;
2299 struct type_path_entry_t {
2300 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2302 size_t index; /**< For array types: the current index. */
2303 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2308 * A type path expression a position inside compound or array types.
2310 typedef struct type_path_t type_path_t;
2311 struct type_path_t {
2312 type_path_entry_t *path; /**< An flexible array containing the current path. */
2313 type_t *top_type; /**< type of the element the path points */
2314 size_t max_index; /**< largest index in outermost array */
2318 * Prints a type path for debugging.
2320 static __attribute__((unused)) void debug_print_type_path(
2321 const type_path_t *path)
2323 size_t len = ARR_LEN(path->path);
2325 for(size_t i = 0; i < len; ++i) {
2326 const type_path_entry_t *entry = & path->path[i];
2328 type_t *type = skip_typeref(entry->type);
2329 if (is_type_compound(type)) {
2330 /* in gcc mode structs can have no members */
2331 if (entry->v.compound_entry == NULL) {
2335 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2336 } else if (is_type_array(type)) {
2337 fprintf(stderr, "[%zu]", entry->v.index);
2339 fprintf(stderr, "-INVALID-");
2342 if (path->top_type != NULL) {
2343 fprintf(stderr, " (");
2344 print_type(path->top_type);
2345 fprintf(stderr, ")");
2350 * Return the top type path entry, ie. in a path
2351 * (type).a.b returns the b.
2353 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2355 size_t len = ARR_LEN(path->path);
2357 return &path->path[len-1];
2361 * Enlarge the type path by an (empty) element.
2363 static type_path_entry_t *append_to_type_path(type_path_t *path)
2365 size_t len = ARR_LEN(path->path);
2366 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2368 type_path_entry_t *result = & path->path[len];
2369 memset(result, 0, sizeof(result[0]));
2374 * Descending into a sub-type. Enter the scope of the current
2377 static void descend_into_subtype(type_path_t *path)
2379 type_t *orig_top_type = path->top_type;
2380 type_t *top_type = skip_typeref(orig_top_type);
2382 type_path_entry_t *top = append_to_type_path(path);
2383 top->type = top_type;
2385 if (is_type_compound(top_type)) {
2386 declaration_t *declaration = top_type->compound.declaration;
2387 declaration_t *entry = declaration->scope.declarations;
2388 top->v.compound_entry = entry;
2390 if (entry != NULL) {
2391 path->top_type = entry->type;
2393 path->top_type = NULL;
2395 } else if (is_type_array(top_type)) {
2397 path->top_type = top_type->array.element_type;
2399 assert(!is_type_valid(top_type));
2404 * Pop an entry from the given type path, ie. returning from
2405 * (type).a.b to (type).a
2407 static void ascend_from_subtype(type_path_t *path)
2409 type_path_entry_t *top = get_type_path_top(path);
2411 path->top_type = top->type;
2413 size_t len = ARR_LEN(path->path);
2414 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2418 * Pop entries from the given type path until the given
2419 * path level is reached.
2421 static void ascend_to(type_path_t *path, size_t top_path_level)
2423 size_t len = ARR_LEN(path->path);
2425 while (len > top_path_level) {
2426 ascend_from_subtype(path);
2427 len = ARR_LEN(path->path);
2431 static bool walk_designator(type_path_t *path, const designator_t *designator,
2432 bool used_in_offsetof)
2434 for( ; designator != NULL; designator = designator->next) {
2435 type_path_entry_t *top = get_type_path_top(path);
2436 type_t *orig_type = top->type;
2438 type_t *type = skip_typeref(orig_type);
2440 if (designator->symbol != NULL) {
2441 symbol_t *symbol = designator->symbol;
2442 if (!is_type_compound(type)) {
2443 if (is_type_valid(type)) {
2444 errorf(&designator->source_position,
2445 "'.%Y' designator used for non-compound type '%T'",
2449 top->type = type_error_type;
2450 top->v.compound_entry = NULL;
2451 orig_type = type_error_type;
2453 declaration_t *declaration = type->compound.declaration;
2454 declaration_t *iter = declaration->scope.declarations;
2455 for( ; iter != NULL; iter = iter->next) {
2456 if (iter->symbol == symbol) {
2461 errorf(&designator->source_position,
2462 "'%T' has no member named '%Y'", orig_type, symbol);
2465 if (used_in_offsetof) {
2466 type_t *real_type = skip_typeref(iter->type);
2467 if (real_type->kind == TYPE_BITFIELD) {
2468 errorf(&designator->source_position,
2469 "offsetof designator '%Y' may not specify bitfield",
2475 top->type = orig_type;
2476 top->v.compound_entry = iter;
2477 orig_type = iter->type;
2480 expression_t *array_index = designator->array_index;
2481 assert(designator->array_index != NULL);
2483 if (!is_type_array(type)) {
2484 if (is_type_valid(type)) {
2485 errorf(&designator->source_position,
2486 "[%E] designator used for non-array type '%T'",
2487 array_index, orig_type);
2492 long index = fold_constant(array_index);
2493 if (!used_in_offsetof) {
2495 errorf(&designator->source_position,
2496 "array index [%E] must be positive", array_index);
2497 } else if (type->array.size_constant) {
2498 long array_size = type->array.size;
2499 if (index >= array_size) {
2500 errorf(&designator->source_position,
2501 "designator [%E] (%d) exceeds array size %d",
2502 array_index, index, array_size);
2507 top->type = orig_type;
2508 top->v.index = (size_t) index;
2509 orig_type = type->array.element_type;
2511 path->top_type = orig_type;
2513 if (designator->next != NULL) {
2514 descend_into_subtype(path);
2523 static void advance_current_object(type_path_t *path, size_t top_path_level)
2525 type_path_entry_t *top = get_type_path_top(path);
2527 type_t *type = skip_typeref(top->type);
2528 if (is_type_union(type)) {
2529 /* in unions only the first element is initialized */
2530 top->v.compound_entry = NULL;
2531 } else if (is_type_struct(type)) {
2532 declaration_t *entry = top->v.compound_entry;
2534 entry = entry->next;
2535 top->v.compound_entry = entry;
2536 if (entry != NULL) {
2537 path->top_type = entry->type;
2540 } else if (is_type_array(type)) {
2541 assert(is_type_array(type));
2545 if (!type->array.size_constant || top->v.index < type->array.size) {
2549 assert(!is_type_valid(type));
2553 /* we're past the last member of the current sub-aggregate, try if we
2554 * can ascend in the type hierarchy and continue with another subobject */
2555 size_t len = ARR_LEN(path->path);
2557 if (len > top_path_level) {
2558 ascend_from_subtype(path);
2559 advance_current_object(path, top_path_level);
2561 path->top_type = NULL;
2566 * skip until token is found.
2568 static void skip_until(int type)
2570 while (token.type != type) {
2571 if (token.type == T_EOF)
2578 * skip any {...} blocks until a closing bracket is reached.
2580 static void skip_initializers(void)
2582 if (token.type == '{')
2585 while (token.type != '}') {
2586 if (token.type == T_EOF)
2588 if (token.type == '{') {
2596 static initializer_t *create_empty_initializer(void)
2598 static initializer_t empty_initializer
2599 = { .list = { { INITIALIZER_LIST }, 0 } };
2600 return &empty_initializer;
2604 * Parse a part of an initialiser for a struct or union,
2606 static initializer_t *parse_sub_initializer(type_path_t *path,
2607 type_t *outer_type, size_t top_path_level,
2608 parse_initializer_env_t *env)
2610 if (token.type == '}') {
2611 /* empty initializer */
2612 return create_empty_initializer();
2615 type_t *orig_type = path->top_type;
2616 type_t *type = NULL;
2618 if (orig_type == NULL) {
2619 /* We are initializing an empty compound. */
2621 type = skip_typeref(orig_type);
2624 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2627 designator_t *designator = NULL;
2628 if (token.type == '.' || token.type == '[') {
2629 designator = parse_designation();
2630 goto finish_designator;
2631 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2632 /* GNU-style designator ("identifier: value") */
2633 designator = allocate_ast_zero(sizeof(designator[0]));
2634 designator->source_position = token.source_position;
2635 designator->symbol = token.v.symbol;
2640 /* reset path to toplevel, evaluate designator from there */
2641 ascend_to(path, top_path_level);
2642 if (!walk_designator(path, designator, false)) {
2643 /* can't continue after designation error */
2647 initializer_t *designator_initializer
2648 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2649 designator_initializer->designator.designator = designator;
2650 ARR_APP1(initializer_t*, initializers, designator_initializer);
2652 orig_type = path->top_type;
2653 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2658 if (token.type == '{') {
2659 if (type != NULL && is_type_scalar(type)) {
2660 sub = parse_scalar_initializer(type, env->must_be_constant);
2664 if (env->declaration != NULL) {
2665 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2666 env->declaration->symbol);
2668 errorf(HERE, "extra brace group at end of initializer");
2671 descend_into_subtype(path);
2673 add_anchor_token('}');
2674 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2676 rem_anchor_token('}');
2679 ascend_from_subtype(path);
2683 goto error_parse_next;
2687 /* must be an expression */
2688 expression_t *expression = parse_assignment_expression();
2690 if (env->must_be_constant && !is_initializer_constant(expression)) {
2691 errorf(&expression->base.source_position,
2692 "Initialisation expression '%E' is not constant\n",
2697 /* we are already outside, ... */
2698 type_t *const outer_type_skip = skip_typeref(outer_type);
2699 if (is_type_compound(outer_type_skip) &&
2700 !outer_type_skip->compound.declaration->init.complete) {
2701 goto error_parse_next;
2706 /* handle { "string" } special case */
2707 if ((expression->kind == EXPR_STRING_LITERAL
2708 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2709 && outer_type != NULL) {
2710 sub = initializer_from_expression(outer_type, expression);
2712 if (token.type == ',') {
2715 if (token.type != '}') {
2716 warningf(HERE, "excessive elements in initializer for type '%T'",
2719 /* TODO: eat , ... */
2724 /* descend into subtypes until expression matches type */
2726 orig_type = path->top_type;
2727 type = skip_typeref(orig_type);
2729 sub = initializer_from_expression(orig_type, expression);
2733 if (!is_type_valid(type)) {
2736 if (is_type_scalar(type)) {
2737 errorf(&expression->base.source_position,
2738 "expression '%E' doesn't match expected type '%T'",
2739 expression, orig_type);
2743 descend_into_subtype(path);
2747 /* update largest index of top array */
2748 const type_path_entry_t *first = &path->path[0];
2749 type_t *first_type = first->type;
2750 first_type = skip_typeref(first_type);
2751 if (is_type_array(first_type)) {
2752 size_t index = first->v.index;
2753 if (index > path->max_index)
2754 path->max_index = index;
2758 /* append to initializers list */
2759 ARR_APP1(initializer_t*, initializers, sub);
2762 if (env->declaration != NULL)
2763 warningf(HERE, "excess elements in struct initializer for '%Y'",
2764 env->declaration->symbol);
2766 warningf(HERE, "excess elements in struct initializer");
2770 if (token.type == '}') {
2774 if (token.type == '}') {
2779 /* advance to the next declaration if we are not at the end */
2780 advance_current_object(path, top_path_level);
2781 orig_type = path->top_type;
2782 if (orig_type != NULL)
2783 type = skip_typeref(orig_type);
2789 size_t len = ARR_LEN(initializers);
2790 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2791 initializer_t *result = allocate_ast_zero(size);
2792 result->kind = INITIALIZER_LIST;
2793 result->list.len = len;
2794 memcpy(&result->list.initializers, initializers,
2795 len * sizeof(initializers[0]));
2797 DEL_ARR_F(initializers);
2798 ascend_to(path, top_path_level+1);
2803 skip_initializers();
2804 DEL_ARR_F(initializers);
2805 ascend_to(path, top_path_level+1);
2810 * Parses an initializer. Parsers either a compound literal
2811 * (env->declaration == NULL) or an initializer of a declaration.
2813 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2815 type_t *type = skip_typeref(env->type);
2816 initializer_t *result = NULL;
2819 if (is_type_scalar(type)) {
2820 result = parse_scalar_initializer(type, env->must_be_constant);
2821 } else if (token.type == '{') {
2825 memset(&path, 0, sizeof(path));
2826 path.top_type = env->type;
2827 path.path = NEW_ARR_F(type_path_entry_t, 0);
2829 descend_into_subtype(&path);
2831 add_anchor_token('}');
2832 result = parse_sub_initializer(&path, env->type, 1, env);
2833 rem_anchor_token('}');
2835 max_index = path.max_index;
2836 DEL_ARR_F(path.path);
2840 /* parse_scalar_initializer() also works in this case: we simply
2841 * have an expression without {} around it */
2842 result = parse_scalar_initializer(type, env->must_be_constant);
2845 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2846 * the array type size */
2847 if (is_type_array(type) && type->array.size_expression == NULL
2848 && result != NULL) {
2850 switch (result->kind) {
2851 case INITIALIZER_LIST:
2852 size = max_index + 1;
2855 case INITIALIZER_STRING:
2856 size = result->string.string.size;
2859 case INITIALIZER_WIDE_STRING:
2860 size = result->wide_string.string.size;
2863 case INITIALIZER_DESIGNATOR:
2864 case INITIALIZER_VALUE:
2865 /* can happen for parse errors */
2870 internal_errorf(HERE, "invalid initializer type");
2873 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2874 cnst->base.type = type_size_t;
2875 cnst->conste.v.int_value = size;
2877 type_t *new_type = duplicate_type(type);
2879 new_type->array.size_expression = cnst;
2880 new_type->array.size_constant = true;
2881 new_type->array.size = size;
2882 env->type = new_type;
2890 static declaration_t *append_declaration(declaration_t *declaration);
2892 static declaration_t *parse_compound_type_specifier(bool is_struct)
2894 gnu_attribute_t *attributes = NULL;
2895 decl_modifiers_t modifiers = 0;
2902 symbol_t *symbol = NULL;
2903 declaration_t *declaration = NULL;
2905 if (token.type == T___attribute__) {
2906 modifiers |= parse_attributes(&attributes);
2909 if (token.type == T_IDENTIFIER) {
2910 symbol = token.v.symbol;
2913 namespace_t const namespc =
2914 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2915 declaration = get_declaration(symbol, namespc);
2916 if (declaration != NULL) {
2917 if (declaration->parent_scope != scope &&
2918 (token.type == '{' || token.type == ';')) {
2920 } else if (declaration->init.complete &&
2921 token.type == '{') {
2922 assert(symbol != NULL);
2923 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2924 is_struct ? "struct" : "union", symbol,
2925 &declaration->source_position);
2926 declaration->scope.declarations = NULL;
2929 } else if (token.type != '{') {
2931 parse_error_expected("while parsing struct type specifier",
2932 T_IDENTIFIER, '{', NULL);
2934 parse_error_expected("while parsing union type specifier",
2935 T_IDENTIFIER, '{', NULL);
2941 if (declaration == NULL) {
2942 declaration = allocate_declaration_zero();
2943 declaration->namespc =
2944 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2945 declaration->source_position = token.source_position;
2946 declaration->symbol = symbol;
2947 declaration->parent_scope = scope;
2948 if (symbol != NULL) {
2949 environment_push(declaration);
2951 append_declaration(declaration);
2954 if (token.type == '{') {
2955 declaration->init.complete = true;
2957 parse_compound_type_entries(declaration);
2958 modifiers |= parse_attributes(&attributes);
2961 declaration->modifiers |= modifiers;
2965 static void parse_enum_entries(type_t *const enum_type)
2969 if (token.type == '}') {
2971 errorf(HERE, "empty enum not allowed");
2975 add_anchor_token('}');
2977 if (token.type != T_IDENTIFIER) {
2978 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2980 rem_anchor_token('}');
2984 declaration_t *const entry = allocate_declaration_zero();
2985 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2986 entry->type = enum_type;
2987 entry->symbol = token.v.symbol;
2988 entry->source_position = token.source_position;
2991 if (token.type == '=') {
2993 expression_t *value = parse_constant_expression();
2995 value = create_implicit_cast(value, enum_type);
2996 entry->init.enum_value = value;
3001 record_declaration(entry, false);
3003 if (token.type != ',')
3006 } while (token.type != '}');
3007 rem_anchor_token('}');
3015 static type_t *parse_enum_specifier(void)
3017 gnu_attribute_t *attributes = NULL;
3018 declaration_t *declaration;
3022 if (token.type == T_IDENTIFIER) {
3023 symbol = token.v.symbol;
3026 declaration = get_declaration(symbol, NAMESPACE_ENUM);
3027 } else if (token.type != '{') {
3028 parse_error_expected("while parsing enum type specifier",
3029 T_IDENTIFIER, '{', NULL);
3036 if (declaration == NULL) {
3037 declaration = allocate_declaration_zero();
3038 declaration->namespc = NAMESPACE_ENUM;
3039 declaration->source_position = token.source_position;
3040 declaration->symbol = symbol;
3041 declaration->parent_scope = scope;
3044 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
3045 type->enumt.declaration = declaration;
3047 if (token.type == '{') {
3048 if (declaration->init.complete) {
3049 errorf(HERE, "multiple definitions of enum %Y", symbol);
3051 if (symbol != NULL) {
3052 environment_push(declaration);
3054 append_declaration(declaration);
3055 declaration->init.complete = true;
3057 parse_enum_entries(type);
3058 parse_attributes(&attributes);
3065 * if a symbol is a typedef to another type, return true
3067 static bool is_typedef_symbol(symbol_t *symbol)
3069 const declaration_t *const declaration =
3070 get_declaration(symbol, NAMESPACE_NORMAL);
3072 declaration != NULL &&
3073 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
3076 static type_t *parse_typeof(void)
3083 add_anchor_token(')');
3085 expression_t *expression = NULL;
3087 bool old_type_prop = in_type_prop;
3088 bool old_gcc_extension = in_gcc_extension;
3089 in_type_prop = true;
3091 while (token.type == T___extension__) {
3092 /* This can be a prefix to a typename or an expression. */
3094 in_gcc_extension = true;
3096 switch (token.type) {
3098 if (is_typedef_symbol(token.v.symbol)) {
3099 type = parse_typename();
3101 expression = parse_expression();
3102 type = expression->base.type;
3107 type = parse_typename();
3111 expression = parse_expression();
3112 type = expression->base.type;
3115 in_type_prop = old_type_prop;
3116 in_gcc_extension = old_gcc_extension;
3118 rem_anchor_token(')');
3121 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
3122 typeof_type->typeoft.expression = expression;
3123 typeof_type->typeoft.typeof_type = type;
3130 typedef enum specifiers_t {
3131 SPECIFIER_SIGNED = 1 << 0,
3132 SPECIFIER_UNSIGNED = 1 << 1,
3133 SPECIFIER_LONG = 1 << 2,
3134 SPECIFIER_INT = 1 << 3,
3135 SPECIFIER_DOUBLE = 1 << 4,
3136 SPECIFIER_CHAR = 1 << 5,
3137 SPECIFIER_SHORT = 1 << 6,
3138 SPECIFIER_LONG_LONG = 1 << 7,
3139 SPECIFIER_FLOAT = 1 << 8,
3140 SPECIFIER_BOOL = 1 << 9,
3141 SPECIFIER_VOID = 1 << 10,
3142 SPECIFIER_INT8 = 1 << 11,
3143 SPECIFIER_INT16 = 1 << 12,
3144 SPECIFIER_INT32 = 1 << 13,
3145 SPECIFIER_INT64 = 1 << 14,
3146 SPECIFIER_INT128 = 1 << 15,
3147 SPECIFIER_COMPLEX = 1 << 16,
3148 SPECIFIER_IMAGINARY = 1 << 17,
3151 static type_t *create_builtin_type(symbol_t *const symbol,
3152 type_t *const real_type)
3154 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
3155 type->builtin.symbol = symbol;
3156 type->builtin.real_type = real_type;
3158 type_t *result = typehash_insert(type);
3159 if (type != result) {
3166 static type_t *get_typedef_type(symbol_t *symbol)
3168 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
3169 if (declaration == NULL ||
3170 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
3173 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
3174 type->typedeft.declaration = declaration;
3180 * check for the allowed MS alignment values.
3182 static bool check_alignment_value(long long intvalue)
3184 if (intvalue < 1 || intvalue > 8192) {
3185 errorf(HERE, "illegal alignment value");
3188 unsigned v = (unsigned)intvalue;
3189 for (unsigned i = 1; i <= 8192; i += i) {
3193 errorf(HERE, "alignment must be power of two");
3197 #define DET_MOD(name, tag) do { \
3198 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
3199 *modifiers |= tag; \
3202 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
3204 decl_modifiers_t *modifiers = &specifiers->modifiers;
3207 if (token.type == T_restrict) {
3209 DET_MOD(restrict, DM_RESTRICT);
3211 } else if (token.type != T_IDENTIFIER)
3213 symbol_t *symbol = token.v.symbol;
3214 if (symbol == sym_align) {
3217 if (token.type != T_INTEGER)
3219 if (check_alignment_value(token.v.intvalue)) {
3220 if (specifiers->alignment != 0)
3221 warningf(HERE, "align used more than once");
3222 specifiers->alignment = (unsigned char)token.v.intvalue;
3226 } else if (symbol == sym_allocate) {
3229 if (token.type != T_IDENTIFIER)
3231 (void)token.v.symbol;
3233 } else if (symbol == sym_dllimport) {
3235 DET_MOD(dllimport, DM_DLLIMPORT);
3236 } else if (symbol == sym_dllexport) {
3238 DET_MOD(dllexport, DM_DLLEXPORT);
3239 } else if (symbol == sym_thread) {
3241 DET_MOD(thread, DM_THREAD);
3242 } else if (symbol == sym_naked) {
3244 DET_MOD(naked, DM_NAKED);
3245 } else if (symbol == sym_noinline) {
3247 DET_MOD(noinline, DM_NOINLINE);
3248 } else if (symbol == sym_noreturn) {
3250 DET_MOD(noreturn, DM_NORETURN);
3251 } else if (symbol == sym_nothrow) {
3253 DET_MOD(nothrow, DM_NOTHROW);
3254 } else if (symbol == sym_novtable) {
3256 DET_MOD(novtable, DM_NOVTABLE);
3257 } else if (symbol == sym_property) {
3261 bool is_get = false;
3262 if (token.type != T_IDENTIFIER)
3264 if (token.v.symbol == sym_get) {
3266 } else if (token.v.symbol == sym_put) {
3268 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
3273 if (token.type != T_IDENTIFIER)
3276 if (specifiers->get_property_sym != NULL) {
3277 errorf(HERE, "get property name already specified");
3279 specifiers->get_property_sym = token.v.symbol;
3282 if (specifiers->put_property_sym != NULL) {
3283 errorf(HERE, "put property name already specified");
3285 specifiers->put_property_sym = token.v.symbol;
3289 if (token.type == ',') {
3296 } else if (symbol == sym_selectany) {
3298 DET_MOD(selectany, DM_SELECTANY);
3299 } else if (symbol == sym_uuid) {
3302 if (token.type != T_STRING_LITERAL)
3306 } else if (symbol == sym_deprecated) {
3308 if (specifiers->deprecated != 0)
3309 warningf(HERE, "deprecated used more than once");
3310 specifiers->deprecated = 1;
3311 if (token.type == '(') {
3313 if (token.type == T_STRING_LITERAL) {
3314 specifiers->deprecated_string = token.v.string.begin;
3317 errorf(HERE, "string literal expected");
3321 } else if (symbol == sym_noalias) {
3323 DET_MOD(noalias, DM_NOALIAS);
3325 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3327 if (token.type == '(')
3331 if (token.type == ',')
3338 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3340 declaration_t *const decl = allocate_declaration_zero();
3341 decl->source_position = *HERE;
3342 decl->declared_storage_class = storage_class;
3343 decl->storage_class =
3344 storage_class != STORAGE_CLASS_NONE || scope == file_scope ?
3345 storage_class : STORAGE_CLASS_AUTO;
3346 decl->symbol = symbol;
3347 decl->implicit = true;
3348 record_declaration(decl, false);
3353 * Finish the construction of a struct type by calculating
3354 * its size, offsets, alignment.
3356 static void finish_struct_type(compound_type_t *type) {
3357 if (type->declaration == NULL)
3359 declaration_t *struct_decl = type->declaration;
3360 if (! struct_decl->init.complete)
3365 il_alignment_t alignment = 1;
3366 bool need_pad = false;
3368 declaration_t *entry = struct_decl->scope.declarations;
3369 for (; entry != NULL; entry = entry->next) {
3370 if (entry->namespc != NAMESPACE_NORMAL)
3373 type_t *m_type = skip_typeref(entry->type);
3374 if (! is_type_valid(m_type)) {
3375 /* simply ignore errors here */
3378 il_alignment_t m_alignment = m_type->base.alignment;
3379 if (m_alignment > alignment)
3380 alignment = m_alignment;
3382 offset = (size + m_alignment - 1) & -m_alignment;
3386 entry->offset = offset;
3387 size = offset + m_type->base.size;
3389 if (type->base.alignment != 0) {
3390 alignment = type->base.alignment;
3393 offset = (size + alignment - 1) & -alignment;
3397 if (warning.padded && need_pad) {
3398 warningf(&struct_decl->source_position,
3399 "'%#T' needs padding", type, struct_decl->symbol);
3401 if (warning.packed && !need_pad) {
3402 warningf(&struct_decl->source_position,
3403 "superfluous packed attribute on '%#T'",
3404 type, struct_decl->symbol);
3407 type->base.size = offset;
3408 type->base.alignment = alignment;
3412 * Finish the construction of an union type by calculating
3413 * its size and alignment.
3415 static void finish_union_type(compound_type_t *type) {
3416 if (type->declaration == NULL)
3418 declaration_t *union_decl = type->declaration;
3419 if (! union_decl->init.complete)
3423 il_alignment_t alignment = 1;
3425 declaration_t *entry = union_decl->scope.declarations;
3426 for (; entry != NULL; entry = entry->next) {
3427 if (entry->namespc != NAMESPACE_NORMAL)
3430 type_t *m_type = skip_typeref(entry->type);
3431 if (! is_type_valid(m_type))
3435 if (m_type->base.size > size)
3436 size = m_type->base.size;
3437 if (m_type->base.alignment > alignment)
3438 alignment = m_type->base.alignment;
3440 if (type->base.alignment != 0) {
3441 alignment = type->base.alignment;
3443 size = (size + alignment - 1) & -alignment;
3444 type->base.size = size;
3445 type->base.alignment = alignment;
3448 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3450 type_t *type = NULL;
3451 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3452 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3453 unsigned type_specifiers = 0;
3454 bool newtype = false;
3455 bool saw_error = false;
3456 bool old_gcc_extension = in_gcc_extension;
3458 specifiers->source_position = token.source_position;
3461 specifiers->modifiers
3462 |= parse_attributes(&specifiers->gnu_attributes);
3463 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3464 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3466 switch (token.type) {
3469 #define MATCH_STORAGE_CLASS(token, class) \
3471 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3472 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3474 specifiers->declared_storage_class = class; \
3478 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3479 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3480 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3481 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3482 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3487 add_anchor_token(')');
3488 parse_microsoft_extended_decl_modifier(specifiers);
3489 rem_anchor_token(')');
3494 switch (specifiers->declared_storage_class) {
3495 case STORAGE_CLASS_NONE:
3496 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3499 case STORAGE_CLASS_EXTERN:
3500 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3503 case STORAGE_CLASS_STATIC:
3504 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3508 errorf(HERE, "multiple storage classes in declaration specifiers");
3514 /* type qualifiers */
3515 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3517 qualifiers |= qualifier; \
3521 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3522 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3523 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3524 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3525 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3526 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3527 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3528 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3530 case T___extension__:
3532 in_gcc_extension = true;
3535 /* type specifiers */
3536 #define MATCH_SPECIFIER(token, specifier, name) \
3539 if (type_specifiers & specifier) { \
3540 errorf(HERE, "multiple " name " type specifiers given"); \
3542 type_specifiers |= specifier; \
3546 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3547 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3548 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3549 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3550 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3551 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3552 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3553 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3554 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3555 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3556 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3557 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3558 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3559 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3560 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3561 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3563 case T__forceinline:
3564 /* only in microsoft mode */
3565 specifiers->modifiers |= DM_FORCEINLINE;
3570 specifiers->is_inline = true;
3575 if (type_specifiers & SPECIFIER_LONG_LONG) {
3576 errorf(HERE, "multiple type specifiers given");
3577 } else if (type_specifiers & SPECIFIER_LONG) {
3578 type_specifiers |= SPECIFIER_LONG_LONG;
3580 type_specifiers |= SPECIFIER_LONG;
3585 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3587 type->compound.declaration = parse_compound_type_specifier(true);
3588 finish_struct_type(&type->compound);
3592 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3593 type->compound.declaration = parse_compound_type_specifier(false);
3594 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3595 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3596 finish_union_type(&type->compound);
3600 type = parse_enum_specifier();
3603 type = parse_typeof();
3605 case T___builtin_va_list:
3606 type = duplicate_type(type_valist);
3610 case T_IDENTIFIER: {
3611 /* only parse identifier if we haven't found a type yet */
3612 if (type != NULL || type_specifiers != 0) {
3613 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3614 * declaration, so it doesn't generate errors about expecting '(' or
3616 switch (look_ahead(1)->type) {
3623 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3626 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3631 goto finish_specifiers;
3635 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3636 if (typedef_type == NULL) {
3637 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3638 * declaration, so it doesn't generate 'implicit int' followed by more
3639 * errors later on. */
3640 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3645 errorf(HERE, "%K does not name a type", &token);
3647 declaration_t *const decl =
3648 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3650 type = allocate_type_zero(TYPE_TYPEDEF, HERE);
3651 type->typedeft.declaration = decl;
3655 if (la1_type == '*')
3656 goto finish_specifiers;
3661 goto finish_specifiers;
3666 type = typedef_type;
3670 /* function specifier */
3672 goto finish_specifiers;
3677 in_gcc_extension = old_gcc_extension;
3679 if (type == NULL || (saw_error && type_specifiers != 0)) {
3680 atomic_type_kind_t atomic_type;
3682 /* match valid basic types */
3683 switch(type_specifiers) {
3684 case SPECIFIER_VOID:
3685 atomic_type = ATOMIC_TYPE_VOID;
3687 case SPECIFIER_CHAR:
3688 atomic_type = ATOMIC_TYPE_CHAR;
3690 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3691 atomic_type = ATOMIC_TYPE_SCHAR;
3693 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3694 atomic_type = ATOMIC_TYPE_UCHAR;
3696 case SPECIFIER_SHORT:
3697 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3698 case SPECIFIER_SHORT | SPECIFIER_INT:
3699 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3700 atomic_type = ATOMIC_TYPE_SHORT;
3702 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3703 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3704 atomic_type = ATOMIC_TYPE_USHORT;
3707 case SPECIFIER_SIGNED:
3708 case SPECIFIER_SIGNED | SPECIFIER_INT:
3709 atomic_type = ATOMIC_TYPE_INT;
3711 case SPECIFIER_UNSIGNED:
3712 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3713 atomic_type = ATOMIC_TYPE_UINT;
3715 case SPECIFIER_LONG:
3716 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3717 case SPECIFIER_LONG | SPECIFIER_INT:
3718 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3719 atomic_type = ATOMIC_TYPE_LONG;
3721 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3722 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3723 atomic_type = ATOMIC_TYPE_ULONG;
3726 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3727 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3728 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3729 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3731 atomic_type = ATOMIC_TYPE_LONGLONG;
3732 goto warn_about_long_long;
3734 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3735 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3737 atomic_type = ATOMIC_TYPE_ULONGLONG;
3738 warn_about_long_long:
3739 if (warning.long_long) {
3740 warningf(&specifiers->source_position,
3741 "ISO C90 does not support 'long long'");
3745 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3746 atomic_type = unsigned_int8_type_kind;
3749 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3750 atomic_type = unsigned_int16_type_kind;
3753 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3754 atomic_type = unsigned_int32_type_kind;
3757 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3758 atomic_type = unsigned_int64_type_kind;
3761 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3762 atomic_type = unsigned_int128_type_kind;
3765 case SPECIFIER_INT8:
3766 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3767 atomic_type = int8_type_kind;
3770 case SPECIFIER_INT16:
3771 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3772 atomic_type = int16_type_kind;
3775 case SPECIFIER_INT32:
3776 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3777 atomic_type = int32_type_kind;
3780 case SPECIFIER_INT64:
3781 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3782 atomic_type = int64_type_kind;
3785 case SPECIFIER_INT128:
3786 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3787 atomic_type = int128_type_kind;
3790 case SPECIFIER_FLOAT:
3791 atomic_type = ATOMIC_TYPE_FLOAT;
3793 case SPECIFIER_DOUBLE:
3794 atomic_type = ATOMIC_TYPE_DOUBLE;
3796 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3797 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3799 case SPECIFIER_BOOL:
3800 atomic_type = ATOMIC_TYPE_BOOL;
3802 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3803 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3804 atomic_type = ATOMIC_TYPE_FLOAT;
3806 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3807 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3808 atomic_type = ATOMIC_TYPE_DOUBLE;
3810 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3811 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3812 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3815 /* invalid specifier combination, give an error message */
3816 if (type_specifiers == 0) {
3821 if (warning.implicit_int) {
3822 warningf(HERE, "no type specifiers in declaration, using 'int'");
3824 atomic_type = ATOMIC_TYPE_INT;
3827 errorf(HERE, "no type specifiers given in declaration");
3829 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3830 (type_specifiers & SPECIFIER_UNSIGNED)) {
3831 errorf(HERE, "signed and unsigned specifiers given");
3832 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3833 errorf(HERE, "only integer types can be signed or unsigned");
3835 errorf(HERE, "multiple datatypes in declaration");
3840 if (type_specifiers & SPECIFIER_COMPLEX) {
3841 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3842 type->complex.akind = atomic_type;
3843 } else if (type_specifiers & SPECIFIER_IMAGINARY) {
3844 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3845 type->imaginary.akind = atomic_type;
3847 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3848 type->atomic.akind = atomic_type;
3851 } else if (type_specifiers != 0) {
3852 errorf(HERE, "multiple datatypes in declaration");
3855 /* FIXME: check type qualifiers here */
3857 type->base.qualifiers = qualifiers;
3858 type->base.modifiers = modifiers;
3860 type_t *result = typehash_insert(type);
3861 if (newtype && result != type) {
3865 specifiers->type = result;
3869 specifiers->type = type_error_type;
3873 static type_qualifiers_t parse_type_qualifiers(void)
3875 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3878 switch(token.type) {
3879 /* type qualifiers */
3880 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3881 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3882 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3883 /* microsoft extended type modifiers */
3884 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3885 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3886 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3887 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3888 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3897 * Parses an K&R identifier list and return a list of declarations.
3899 * @param last points to the last declaration in the list
3900 * @return the list of declarations
3902 static declaration_t *parse_identifier_list(declaration_t **last)
3904 declaration_t *declarations = NULL;
3905 declaration_t *last_declaration = NULL;
3907 declaration_t *const declaration = allocate_declaration_zero();
3908 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3909 declaration->source_position = token.source_position;
3910 declaration->symbol = token.v.symbol;
3913 if (last_declaration != NULL) {
3914 last_declaration->next = declaration;
3916 declarations = declaration;
3918 last_declaration = declaration;
3920 if (token.type != ',') {
3924 } while (token.type == T_IDENTIFIER);
3926 *last = last_declaration;
3927 return declarations;
3930 static type_t *automatic_type_conversion(type_t *orig_type);
3932 static void semantic_parameter(declaration_t *declaration)
3934 /* TODO: improve error messages */
3935 source_position_t const* const pos = &declaration->source_position;
3937 switch (declaration->declared_storage_class) {
3938 case STORAGE_CLASS_TYPEDEF:
3939 errorf(pos, "typedef not allowed in parameter list");
3942 /* Allowed storage classes */
3943 case STORAGE_CLASS_NONE:
3944 case STORAGE_CLASS_REGISTER:
3948 errorf(pos, "parameter may only have none or register storage class");
3952 type_t *const orig_type = declaration->type;
3953 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3954 * sugar. Turn it into a pointer.
3955 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3956 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3958 type_t *const type = automatic_type_conversion(orig_type);
3959 declaration->type = type;
3961 if (is_type_incomplete(skip_typeref(type))) {
3962 errorf(pos, "parameter '%#T' is of incomplete type",
3963 orig_type, declaration->symbol);
3967 static declaration_t *parse_parameter(void)
3969 declaration_specifiers_t specifiers;
3970 memset(&specifiers, 0, sizeof(specifiers));
3972 parse_declaration_specifiers(&specifiers);
3974 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3980 * Parses a function type parameter list and return a list of declarations.
3982 * @param last point to the last element of the list
3983 * @return the parameter list
3985 static declaration_t *parse_parameters(function_type_t *type, declaration_t **last)
3987 declaration_t *declarations = NULL;
3990 add_anchor_token(')');
3991 int saved_comma_state = save_and_reset_anchor_state(',');
3993 if (token.type == T_IDENTIFIER &&
3994 !is_typedef_symbol(token.v.symbol)) {
3995 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
3996 if (la1_type == ',' || la1_type == ')') {
3997 type->kr_style_parameters = true;
3998 declarations = parse_identifier_list(last);
3999 goto parameters_finished;
4003 if (token.type == ')') {
4004 type->unspecified_parameters = 1;
4005 goto parameters_finished;
4008 declaration_t *declaration;
4009 declaration_t *last_declaration = NULL;
4010 function_parameter_t *parameter;
4011 function_parameter_t *last_parameter = NULL;
4014 switch(token.type) {
4018 goto parameters_finished;
4021 case T___extension__:
4023 declaration = parse_parameter();
4025 /* func(void) is not a parameter */
4026 if (last_parameter == NULL
4027 && token.type == ')'
4028 && declaration->symbol == NULL
4029 && skip_typeref(declaration->type) == type_void) {
4030 goto parameters_finished;
4032 semantic_parameter(declaration);
4034 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
4035 memset(parameter, 0, sizeof(parameter[0]));
4036 parameter->type = declaration->type;
4038 if (last_parameter != NULL) {
4039 last_declaration->next = declaration;
4040 last_parameter->next = parameter;
4042 type->parameters = parameter;
4043 declarations = declaration;
4045 last_parameter = parameter;
4046 last_declaration = declaration;
4050 goto parameters_finished;
4052 if (token.type != ',') {
4053 goto parameters_finished;
4059 parameters_finished:
4060 rem_anchor_token(')');
4063 restore_anchor_state(',', saved_comma_state);
4064 *last = last_declaration;
4065 return declarations;
4068 restore_anchor_state(',', saved_comma_state);
4073 typedef enum construct_type_kind_t {
4078 } construct_type_kind_t;
4080 typedef struct construct_type_t construct_type_t;
4081 struct construct_type_t {
4082 construct_type_kind_t kind;
4083 construct_type_t *next;
4086 typedef struct parsed_pointer_t parsed_pointer_t;
4087 struct parsed_pointer_t {
4088 construct_type_t construct_type;
4089 type_qualifiers_t type_qualifiers;
4092 typedef struct construct_function_type_t construct_function_type_t;
4093 struct construct_function_type_t {
4094 construct_type_t construct_type;
4095 type_t *function_type;
4098 typedef struct parsed_array_t parsed_array_t;
4099 struct parsed_array_t {
4100 construct_type_t construct_type;
4101 type_qualifiers_t type_qualifiers;
4107 typedef struct construct_base_type_t construct_base_type_t;
4108 struct construct_base_type_t {
4109 construct_type_t construct_type;
4113 static construct_type_t *parse_pointer_declarator(void)
4117 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
4118 memset(pointer, 0, sizeof(pointer[0]));
4119 pointer->construct_type.kind = CONSTRUCT_POINTER;
4120 pointer->type_qualifiers = parse_type_qualifiers();
4122 return (construct_type_t*) pointer;
4125 static construct_type_t *parse_array_declarator(void)
4128 add_anchor_token(']');
4130 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
4131 memset(array, 0, sizeof(array[0]));
4132 array->construct_type.kind = CONSTRUCT_ARRAY;
4134 if (token.type == T_static) {
4135 array->is_static = true;
4139 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
4140 if (type_qualifiers != 0) {
4141 if (token.type == T_static) {
4142 array->is_static = true;
4146 array->type_qualifiers = type_qualifiers;
4148 if (token.type == '*' && look_ahead(1)->type == ']') {
4149 array->is_variable = true;
4151 } else if (token.type != ']') {
4152 array->size = parse_assignment_expression();
4155 rem_anchor_token(']');
4159 return (construct_type_t*) array;
4162 static construct_type_t *parse_function_declarator(declaration_t *declaration)
4165 if (declaration != NULL) {
4166 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
4168 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
4170 if (mask & (mask-1)) {
4171 const char *first = NULL, *second = NULL;
4173 /* more than one calling convention set */
4174 if (declaration->modifiers & DM_CDECL) {
4175 if (first == NULL) first = "cdecl";
4176 else if (second == NULL) second = "cdecl";
4178 if (declaration->modifiers & DM_STDCALL) {
4179 if (first == NULL) first = "stdcall";
4180 else if (second == NULL) second = "stdcall";
4182 if (declaration->modifiers & DM_FASTCALL) {
4183 if (first == NULL) first = "fastcall";
4184 else if (second == NULL) second = "fastcall";
4186 if (declaration->modifiers & DM_THISCALL) {
4187 if (first == NULL) first = "thiscall";
4188 else if (second == NULL) second = "thiscall";
4190 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
4193 if (declaration->modifiers & DM_CDECL)
4194 type->function.calling_convention = CC_CDECL;
4195 else if (declaration->modifiers & DM_STDCALL)
4196 type->function.calling_convention = CC_STDCALL;
4197 else if (declaration->modifiers & DM_FASTCALL)
4198 type->function.calling_convention = CC_FASTCALL;
4199 else if (declaration->modifiers & DM_THISCALL)
4200 type->function.calling_convention = CC_THISCALL;
4202 type = allocate_type_zero(TYPE_FUNCTION, HERE);
4205 declaration_t *last;
4206 declaration_t *parameters = parse_parameters(&type->function, &last);
4207 if (declaration != NULL) {
4208 declaration->scope.declarations = parameters;
4209 declaration->scope.last_declaration = last;
4212 construct_function_type_t *construct_function_type =
4213 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
4214 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
4215 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
4216 construct_function_type->function_type = type;
4218 return &construct_function_type->construct_type;
4221 static void fix_declaration_type(declaration_t *declaration)
4223 decl_modifiers_t declaration_modifiers = declaration->modifiers;
4224 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
4226 if (declaration_modifiers & DM_TRANSPARENT_UNION)
4227 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4229 if (declaration->type->base.modifiers == type_modifiers)
4232 type_t *copy = duplicate_type(declaration->type);
4233 copy->base.modifiers = type_modifiers;
4235 type_t *result = typehash_insert(copy);
4236 if (result != copy) {
4237 obstack_free(type_obst, copy);
4240 declaration->type = result;
4243 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
4244 bool may_be_abstract)
4246 /* construct a single linked list of construct_type_t's which describe
4247 * how to construct the final declarator type */
4248 construct_type_t *first = NULL;
4249 construct_type_t *last = NULL;
4250 gnu_attribute_t *attributes = NULL;
4252 decl_modifiers_t modifiers = parse_attributes(&attributes);
4255 while (token.type == '*') {
4256 construct_type_t *type = parse_pointer_declarator();
4266 /* TODO: find out if this is correct */
4267 modifiers |= parse_attributes(&attributes);
4270 if (declaration != NULL)
4271 declaration->modifiers |= modifiers;
4273 construct_type_t *inner_types = NULL;
4275 switch(token.type) {
4277 if (declaration == NULL) {
4278 errorf(HERE, "no identifier expected in typename");
4280 declaration->symbol = token.v.symbol;
4281 declaration->source_position = token.source_position;
4287 add_anchor_token(')');
4288 inner_types = parse_inner_declarator(declaration, may_be_abstract);
4289 if (inner_types != NULL) {
4290 /* All later declarators only modify the return type, not declaration */
4293 rem_anchor_token(')');
4297 if (may_be_abstract)
4299 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
4304 construct_type_t *p = last;
4307 construct_type_t *type;
4308 switch(token.type) {
4310 type = parse_function_declarator(declaration);
4313 type = parse_array_declarator();
4316 goto declarator_finished;
4319 /* insert in the middle of the list (behind p) */
4321 type->next = p->next;
4332 declarator_finished:
4333 /* append inner_types at the end of the list, we don't to set last anymore
4334 * as it's not needed anymore */
4336 assert(first == NULL);
4337 first = inner_types;
4339 last->next = inner_types;
4347 static void parse_declaration_attributes(declaration_t *declaration)
4349 gnu_attribute_t *attributes = NULL;
4350 decl_modifiers_t modifiers = parse_attributes(&attributes);
4352 if (declaration == NULL)
4355 declaration->modifiers |= modifiers;
4356 /* check if we have these stupid mode attributes... */
4357 type_t *old_type = declaration->type;
4358 if (old_type == NULL)
4361 gnu_attribute_t *attribute = attributes;
4362 for ( ; attribute != NULL; attribute = attribute->next) {
4363 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
4366 atomic_type_kind_t akind = attribute->u.akind;
4367 if (!is_type_signed(old_type)) {
4369 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
4370 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
4371 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
4372 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
4374 panic("invalid akind in mode attribute");
4378 = make_atomic_type(akind, old_type->base.qualifiers);
4382 static type_t *construct_declarator_type(construct_type_t *construct_list,
4385 construct_type_t *iter = construct_list;
4386 for( ; iter != NULL; iter = iter->next) {
4387 switch(iter->kind) {
4388 case CONSTRUCT_INVALID:
4389 internal_errorf(HERE, "invalid type construction found");
4390 case CONSTRUCT_FUNCTION: {
4391 construct_function_type_t *construct_function_type
4392 = (construct_function_type_t*) iter;
4394 type_t *function_type = construct_function_type->function_type;
4396 function_type->function.return_type = type;
4398 type_t *skipped_return_type = skip_typeref(type);
4400 if (is_type_function(skipped_return_type)) {
4401 errorf(HERE, "function returning function is not allowed");
4402 } else if (is_type_array(skipped_return_type)) {
4403 errorf(HERE, "function returning array is not allowed");
4405 if (skipped_return_type->base.qualifiers != 0) {
4407 "type qualifiers in return type of function type are meaningless");
4411 type = function_type;
4415 case CONSTRUCT_POINTER: {
4416 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4417 type = make_pointer_type(type, parsed_pointer->type_qualifiers);
4421 case CONSTRUCT_ARRAY: {
4422 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4423 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
4425 expression_t *size_expression = parsed_array->size;
4426 if (size_expression != NULL) {
4428 = create_implicit_cast(size_expression, type_size_t);
4431 array_type->base.qualifiers = parsed_array->type_qualifiers;
4432 array_type->array.element_type = type;
4433 array_type->array.is_static = parsed_array->is_static;
4434 array_type->array.is_variable = parsed_array->is_variable;
4435 array_type->array.size_expression = size_expression;
4437 if (size_expression != NULL) {
4438 if (is_constant_expression(size_expression)) {
4439 array_type->array.size_constant = true;
4440 array_type->array.size
4441 = fold_constant(size_expression);
4443 array_type->array.is_vla = true;
4447 type_t *skipped_type = skip_typeref(type);
4449 if (is_type_incomplete(skipped_type)) {
4450 errorf(HERE, "array of incomplete type '%T' is not allowed", type);
4451 } else if (is_type_function(skipped_type)) {
4452 errorf(HERE, "array of functions is not allowed");
4459 type_t *hashed_type = typehash_insert(type);
4460 if (hashed_type != type) {
4461 /* the function type was constructed earlier freeing it here will
4462 * destroy other types... */
4463 if (iter->kind != CONSTRUCT_FUNCTION) {
4473 static declaration_t *parse_declarator(
4474 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4476 declaration_t *const declaration = allocate_declaration_zero();
4477 declaration->source_position = specifiers->source_position;
4478 declaration->declared_storage_class = specifiers->declared_storage_class;
4479 declaration->modifiers = specifiers->modifiers;
4480 declaration->deprecated_string = specifiers->deprecated_string;
4481 declaration->get_property_sym = specifiers->get_property_sym;
4482 declaration->put_property_sym = specifiers->put_property_sym;
4483 declaration->is_inline = specifiers->is_inline;
4485 declaration->storage_class = specifiers->declared_storage_class;
4486 if (declaration->storage_class == STORAGE_CLASS_NONE &&
4487 scope != file_scope) {
4488 declaration->storage_class = STORAGE_CLASS_AUTO;
4491 if (specifiers->alignment != 0) {
4492 /* TODO: add checks here */
4493 declaration->alignment = specifiers->alignment;
4496 construct_type_t *construct_type
4497 = parse_inner_declarator(declaration, may_be_abstract);
4498 type_t *const type = specifiers->type;
4499 declaration->type = construct_declarator_type(construct_type, type);
4501 parse_declaration_attributes(declaration);
4503 fix_declaration_type(declaration);
4505 if (construct_type != NULL) {
4506 obstack_free(&temp_obst, construct_type);
4512 static type_t *parse_abstract_declarator(type_t *base_type)
4514 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4516 type_t *result = construct_declarator_type(construct_type, base_type);
4517 if (construct_type != NULL) {
4518 obstack_free(&temp_obst, construct_type);
4524 static declaration_t *append_declaration(declaration_t* const declaration)
4526 if (last_declaration != NULL) {
4527 last_declaration->next = declaration;
4529 scope->declarations = declaration;
4531 last_declaration = declaration;
4536 * Check if the declaration of main is suspicious. main should be a
4537 * function with external linkage, returning int, taking either zero
4538 * arguments, two, or three arguments of appropriate types, ie.
4540 * int main([ int argc, char **argv [, char **env ] ]).
4542 * @param decl the declaration to check
4543 * @param type the function type of the declaration
4545 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4547 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4548 warningf(&decl->source_position,
4549 "'main' is normally a non-static function");
4551 if (!types_compatible(skip_typeref(func_type->return_type), type_int)) {
4552 warningf(&decl->source_position,
4553 "return type of 'main' should be 'int', but is '%T'",
4554 func_type->return_type);
4556 const function_parameter_t *parm = func_type->parameters;
4558 type_t *const first_type = parm->type;
4559 if (!types_compatible(skip_typeref(first_type), type_int)) {
4560 warningf(&decl->source_position,
4561 "first argument of 'main' should be 'int', but is '%T'", first_type);
4565 type_t *const second_type = parm->type;
4566 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4567 warningf(&decl->source_position,
4568 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4572 type_t *const third_type = parm->type;
4573 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4574 warningf(&decl->source_position,
4575 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4579 goto warn_arg_count;
4583 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4589 * Check if a symbol is the equal to "main".
4591 static bool is_sym_main(const symbol_t *const sym)
4593 return strcmp(sym->string, "main") == 0;
4596 static declaration_t *record_declaration(
4597 declaration_t *const declaration,
4598 const bool is_definition)
4600 const symbol_t *const symbol = declaration->symbol;
4601 const namespace_t namespc = (namespace_t)declaration->namespc;
4603 assert(symbol != NULL);
4604 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4606 type_t *const orig_type = declaration->type;
4607 type_t *const type = skip_typeref(orig_type);
4608 if (is_type_function(type) &&
4609 type->function.unspecified_parameters &&
4610 warning.strict_prototypes &&
4611 previous_declaration == NULL) {
4612 warningf(&declaration->source_position,
4613 "function declaration '%#T' is not a prototype",
4617 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4618 check_type_of_main(declaration, &type->function);
4621 if (warning.nested_externs &&
4622 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4623 scope != file_scope) {
4624 warningf(&declaration->source_position,
4625 "nested extern declaration of '%#T'", declaration->type, symbol);
4628 assert(declaration != previous_declaration);
4629 if (previous_declaration != NULL &&
4630 previous_declaration->parent_scope == ¤t_function->scope &&
4631 scope->depth == previous_declaration->parent_scope->depth + 1) {
4632 errorf(&declaration->source_position,
4633 "declaration '%#T' redeclares the parameter '%#T' (declared %P)",
4634 orig_type, symbol, previous_declaration->type, symbol,
4635 &previous_declaration->source_position);
4638 if (previous_declaration != NULL &&
4639 previous_declaration->parent_scope == scope) {
4640 /* can happen for K&R style declarations */
4641 if (previous_declaration->type == NULL) {
4642 previous_declaration->type = declaration->type;
4645 const type_t *prev_type = skip_typeref(previous_declaration->type);
4646 if (!types_compatible(type, prev_type)) {
4647 errorf(&declaration->source_position,
4648 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4649 orig_type, symbol, previous_declaration->type, symbol,
4650 &previous_declaration->source_position);
4652 unsigned old_storage_class = previous_declaration->storage_class;
4653 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4654 errorf(&declaration->source_position,
4655 "redeclaration of enum entry '%Y' (declared %P)",
4656 symbol, &previous_declaration->source_position);
4657 return previous_declaration;
4660 if (warning.redundant_decls &&
4662 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4663 !(previous_declaration->modifiers & DM_USED) &&
4664 !previous_declaration->used) {
4665 warningf(&previous_declaration->source_position,
4666 "unnecessary static forward declaration for '%#T'",
4667 previous_declaration->type, symbol);
4670 unsigned new_storage_class = declaration->storage_class;
4672 if (is_type_incomplete(prev_type)) {
4673 previous_declaration->type = type;
4677 /* pretend no storage class means extern for function
4678 * declarations (except if the previous declaration is neither
4679 * none nor extern) */
4680 if (is_type_function(type)) {
4681 if (prev_type->function.unspecified_parameters) {
4682 previous_declaration->type = type;
4686 switch (old_storage_class) {
4687 case STORAGE_CLASS_NONE:
4688 old_storage_class = STORAGE_CLASS_EXTERN;
4691 case STORAGE_CLASS_EXTERN:
4692 if (is_definition) {
4693 if (warning.missing_prototypes &&
4694 prev_type->function.unspecified_parameters &&
4695 !is_sym_main(symbol)) {
4696 warningf(&declaration->source_position,
4697 "no previous prototype for '%#T'",
4700 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4701 new_storage_class = STORAGE_CLASS_EXTERN;
4710 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4711 new_storage_class == STORAGE_CLASS_EXTERN) {
4712 warn_redundant_declaration:
4713 if (!is_definition &&
4714 warning.redundant_decls &&
4715 is_type_valid(prev_type) &&
4716 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4717 warningf(&declaration->source_position,
4718 "redundant declaration for '%Y' (declared %P)",
4719 symbol, &previous_declaration->source_position);
4721 } else if (current_function == NULL) {
4722 if (old_storage_class != STORAGE_CLASS_STATIC &&
4723 new_storage_class == STORAGE_CLASS_STATIC) {
4724 errorf(&declaration->source_position,
4725 "static declaration of '%Y' follows non-static declaration (declared %P)",
4726 symbol, &previous_declaration->source_position);
4727 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4728 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4729 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4731 goto warn_redundant_declaration;
4733 } else if (is_type_valid(prev_type)) {
4734 if (old_storage_class == new_storage_class) {
4735 errorf(&declaration->source_position,
4736 "redeclaration of '%Y' (declared %P)",
4737 symbol, &previous_declaration->source_position);
4739 errorf(&declaration->source_position,
4740 "redeclaration of '%Y' with different linkage (declared %P)",
4741 symbol, &previous_declaration->source_position);
4746 previous_declaration->modifiers |= declaration->modifiers;
4747 previous_declaration->is_inline |= declaration->is_inline;
4748 return previous_declaration;
4749 } else if (is_type_function(type)) {
4750 if (is_definition &&
4751 declaration->storage_class != STORAGE_CLASS_STATIC) {
4752 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4753 warningf(&declaration->source_position,
4754 "no previous prototype for '%#T'", orig_type, symbol);
4755 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4756 warningf(&declaration->source_position,
4757 "no previous declaration for '%#T'", orig_type,
4762 if (warning.missing_declarations &&
4763 scope == file_scope && (
4764 declaration->storage_class == STORAGE_CLASS_NONE ||
4765 declaration->storage_class == STORAGE_CLASS_THREAD
4767 warningf(&declaration->source_position,
4768 "no previous declaration for '%#T'", orig_type, symbol);
4772 assert(declaration->parent_scope == NULL);
4773 assert(scope != NULL);
4775 declaration->parent_scope = scope;
4777 environment_push(declaration);
4778 return append_declaration(declaration);
4781 static void parser_error_multiple_definition(declaration_t *declaration,
4782 const source_position_t *source_position)
4784 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4785 declaration->symbol, &declaration->source_position);
4788 static bool is_declaration_specifier(const token_t *token,
4789 bool only_specifiers_qualifiers)
4791 switch (token->type) {
4796 return is_typedef_symbol(token->v.symbol);
4798 case T___extension__:
4800 return !only_specifiers_qualifiers;
4807 static void parse_init_declarator_rest(declaration_t *declaration)
4811 type_t *orig_type = declaration->type;
4812 type_t *type = skip_typeref(orig_type);
4814 if (declaration->init.initializer != NULL) {
4815 parser_error_multiple_definition(declaration, HERE);
4818 bool must_be_constant = false;
4819 if (declaration->storage_class == STORAGE_CLASS_STATIC ||
4820 declaration->storage_class == STORAGE_CLASS_THREAD_STATIC ||
4821 declaration->parent_scope == file_scope) {
4822 must_be_constant = true;
4825 if (is_type_function(type)) {
4826 errorf(&declaration->source_position,
4827 "function '%#T' is initialized like a variable",
4828 orig_type, declaration->symbol);
4829 orig_type = type_error_type;
4832 parse_initializer_env_t env;
4833 env.type = orig_type;
4834 env.must_be_constant = must_be_constant;
4835 env.declaration = current_init_decl = declaration;
4837 initializer_t *initializer = parse_initializer(&env);
4838 current_init_decl = NULL;
4840 if (!is_type_function(type)) {
4841 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
4842 * the array type size */
4843 declaration->type = env.type;
4844 declaration->init.initializer = initializer;
4848 /* parse rest of a declaration without any declarator */
4849 static void parse_anonymous_declaration_rest(
4850 const declaration_specifiers_t *specifiers)
4854 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) {
4855 warningf(&specifiers->source_position,
4856 "useless storage class in empty declaration");
4859 type_t *type = specifiers->type;
4860 switch (type->kind) {
4861 case TYPE_COMPOUND_STRUCT:
4862 case TYPE_COMPOUND_UNION: {
4863 if (type->compound.declaration->symbol == NULL) {
4864 warningf(&specifiers->source_position,
4865 "unnamed struct/union that defines no instances");
4874 warningf(&specifiers->source_position, "empty declaration");
4878 #ifdef RECORD_EMPTY_DECLARATIONS
4879 declaration_t *const declaration = allocate_declaration_zero();
4880 declaration->type = specifiers->type;
4881 declaration->declared_storage_class = specifiers->declared_storage_class;
4882 declaration->source_position = specifiers->source_position;
4883 declaration->modifiers = specifiers->modifiers;
4884 declaration->storage_class = STORAGE_CLASS_NONE;
4886 append_declaration(declaration);
4890 static void parse_declaration_rest(declaration_t *ndeclaration,
4891 const declaration_specifiers_t *specifiers,
4892 parsed_declaration_func finished_declaration)
4894 add_anchor_token(';');
4895 add_anchor_token(',');
4897 declaration_t *declaration =
4898 finished_declaration(ndeclaration, token.type == '=');
4900 type_t *orig_type = declaration->type;
4901 type_t *type = skip_typeref(orig_type);
4903 if (type->kind != TYPE_FUNCTION &&
4904 declaration->is_inline &&
4905 is_type_valid(type)) {
4906 warningf(&declaration->source_position,
4907 "variable '%Y' declared 'inline'\n", declaration->symbol);
4910 if (token.type == '=') {
4911 parse_init_declarator_rest(declaration);
4914 if (token.type != ',')
4918 add_anchor_token('=');
4919 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4920 rem_anchor_token('=');
4925 rem_anchor_token(';');
4926 rem_anchor_token(',');
4929 static declaration_t *finished_kr_declaration(declaration_t *declaration, bool is_definition)
4931 symbol_t *symbol = declaration->symbol;
4932 if (symbol == NULL) {
4933 errorf(HERE, "anonymous declaration not valid as function parameter");
4936 namespace_t namespc = (namespace_t) declaration->namespc;
4937 if (namespc != NAMESPACE_NORMAL) {
4938 return record_declaration(declaration, false);
4941 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4942 if (previous_declaration == NULL ||
4943 previous_declaration->parent_scope != scope) {
4944 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4949 if (is_definition) {
4950 errorf(HERE, "parameter %Y is initialised", declaration->symbol);
4953 if (previous_declaration->type == NULL) {
4954 previous_declaration->type = declaration->type;
4955 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4956 previous_declaration->storage_class = declaration->storage_class;
4957 previous_declaration->parent_scope = scope;
4958 return previous_declaration;
4960 return record_declaration(declaration, false);
4964 static void parse_declaration(parsed_declaration_func finished_declaration)
4966 declaration_specifiers_t specifiers;
4967 memset(&specifiers, 0, sizeof(specifiers));
4969 add_anchor_token(';');
4970 parse_declaration_specifiers(&specifiers);
4971 rem_anchor_token(';');
4973 if (token.type == ';') {
4974 parse_anonymous_declaration_rest(&specifiers);
4976 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4977 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4981 static type_t *get_default_promoted_type(type_t *orig_type)
4983 type_t *result = orig_type;
4985 type_t *type = skip_typeref(orig_type);
4986 if (is_type_integer(type)) {
4987 result = promote_integer(type);
4988 } else if (type == type_float) {
4989 result = type_double;
4995 static void parse_kr_declaration_list(declaration_t *declaration)
4997 type_t *type = skip_typeref(declaration->type);
4998 if (!is_type_function(type))
5001 if (!type->function.kr_style_parameters)
5004 add_anchor_token('{');
5006 /* push function parameters */
5007 size_t const top = environment_top();
5008 scope_push(&declaration->scope);
5010 declaration_t *parameter = declaration->scope.declarations;
5011 for ( ; parameter != NULL; parameter = parameter->next) {
5012 assert(parameter->parent_scope == NULL);
5013 parameter->parent_scope = scope;
5014 environment_push(parameter);
5017 /* parse declaration list */
5018 while (is_declaration_specifier(&token, false)) {
5019 parse_declaration(finished_kr_declaration);
5022 /* pop function parameters */
5023 assert(scope == &declaration->scope);
5025 environment_pop_to(top);
5027 /* update function type */
5028 type_t *new_type = duplicate_type(type);
5030 function_parameter_t *parameters = NULL;
5031 function_parameter_t *last_parameter = NULL;
5033 declaration_t *parameter_declaration = declaration->scope.declarations;
5034 for( ; parameter_declaration != NULL;
5035 parameter_declaration = parameter_declaration->next) {
5036 type_t *parameter_type = parameter_declaration->type;
5037 if (parameter_type == NULL) {
5039 errorf(HERE, "no type specified for function parameter '%Y'",
5040 parameter_declaration->symbol);
5042 if (warning.implicit_int) {
5043 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
5044 parameter_declaration->symbol);
5046 parameter_type = type_int;
5047 parameter_declaration->type = parameter_type;
5051 semantic_parameter(parameter_declaration);
5052 parameter_type = parameter_declaration->type;
5055 * we need the default promoted types for the function type
5057 parameter_type = get_default_promoted_type(parameter_type);
5059 function_parameter_t *function_parameter
5060 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
5061 memset(function_parameter, 0, sizeof(function_parameter[0]));
5063 function_parameter->type = parameter_type;
5064 if (last_parameter != NULL) {
5065 last_parameter->next = function_parameter;
5067 parameters = function_parameter;
5069 last_parameter = function_parameter;
5072 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
5074 new_type->function.parameters = parameters;
5075 new_type->function.unspecified_parameters = true;
5077 type = typehash_insert(new_type);
5078 if (type != new_type) {
5079 obstack_free(type_obst, new_type);
5082 declaration->type = type;
5084 rem_anchor_token('{');
5087 static bool first_err = true;
5090 * When called with first_err set, prints the name of the current function,
5093 static void print_in_function(void)
5097 diagnosticf("%s: In function '%Y':\n",
5098 current_function->source_position.input_name,
5099 current_function->symbol);
5104 * Check if all labels are defined in the current function.
5105 * Check if all labels are used in the current function.
5107 static void check_labels(void)
5109 for (const goto_statement_t *goto_statement = goto_first;
5110 goto_statement != NULL;
5111 goto_statement = goto_statement->next) {
5112 /* skip computed gotos */
5113 if (goto_statement->expression != NULL)
5116 declaration_t *label = goto_statement->label;
5119 if (label->source_position.input_name == NULL) {
5120 print_in_function();
5121 errorf(&goto_statement->base.source_position,
5122 "label '%Y' used but not defined", label->symbol);
5125 goto_first = goto_last = NULL;
5127 if (warning.unused_label) {
5128 for (const label_statement_t *label_statement = label_first;
5129 label_statement != NULL;
5130 label_statement = label_statement->next) {
5131 const declaration_t *label = label_statement->label;
5133 if (! label->used) {
5134 print_in_function();
5135 warningf(&label_statement->base.source_position,
5136 "label '%Y' defined but not used", label->symbol);
5140 label_first = label_last = NULL;
5143 static void warn_unused_decl(declaration_t *decl, declaration_t *end, char const *const what)
5145 for (; decl != NULL; decl = decl->next) {
5150 print_in_function();
5151 warningf(&decl->source_position, "%s '%Y' is unused", what, decl->symbol);
5152 } else if (!decl->read) {
5153 print_in_function();
5154 warningf(&decl->source_position, "%s '%Y' is never read", what, decl->symbol);
5162 static void check_unused_variables(statement_t *const stmt, void *const env)
5166 switch (stmt->kind) {
5167 case STATEMENT_DECLARATION: {
5168 declaration_statement_t const *const decls = &stmt->declaration;
5169 warn_unused_decl(decls->declarations_begin, decls->declarations_end, "variable");
5174 warn_unused_decl(stmt->fors.scope.declarations, NULL, "variable");
5183 * Check declarations of current_function for unused entities.
5185 static void check_declarations(void)
5187 if (warning.unused_parameter) {
5188 const scope_t *scope = ¤t_function->scope;
5190 /* do not issue unused warnings for main */
5191 if (!is_sym_main(current_function->symbol)) {
5192 warn_unused_decl(scope->declarations, NULL, "parameter");
5195 if (warning.unused_variable) {
5196 walk_statements(current_function->init.statement, check_unused_variables, NULL);
5200 static int determine_truth(expression_t const* const cond)
5203 !is_constant_expression(cond) ? 0 :
5204 fold_constant(cond) != 0 ? 1 :
5208 static bool noreturn_candidate;
5210 static void check_reachable(statement_t *const stmt)
5212 if (stmt->base.reachable)
5214 if (stmt->kind != STATEMENT_DO_WHILE)
5215 stmt->base.reachable = true;
5217 statement_t *last = stmt;
5219 switch (stmt->kind) {
5220 case STATEMENT_INVALID:
5221 case STATEMENT_EMPTY:
5222 case STATEMENT_DECLARATION:
5224 next = stmt->base.next;
5227 case STATEMENT_COMPOUND:
5228 next = stmt->compound.statements;
5231 case STATEMENT_RETURN:
5232 noreturn_candidate = false;
5235 case STATEMENT_IF: {
5236 if_statement_t const* const ifs = &stmt->ifs;
5237 int const val = determine_truth(ifs->condition);
5240 check_reachable(ifs->true_statement);
5245 if (ifs->false_statement != NULL) {
5246 check_reachable(ifs->false_statement);
5250 next = stmt->base.next;
5254 case STATEMENT_SWITCH: {
5255 switch_statement_t const *const switchs = &stmt->switchs;
5256 expression_t const *const expr = switchs->expression;
5258 if (is_constant_expression(expr)) {
5259 long const val = fold_constant(expr);
5260 case_label_statement_t * defaults = NULL;
5261 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5262 if (i->expression == NULL) {
5267 if (i->first_case <= val && val <= i->last_case) {
5268 check_reachable((statement_t*)i);
5273 if (defaults != NULL) {
5274 check_reachable((statement_t*)defaults);
5278 bool has_default = false;
5279 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5280 if (i->expression == NULL)
5283 check_reachable((statement_t*)i);
5290 next = stmt->base.next;
5294 case STATEMENT_EXPRESSION: {
5295 /* Check for noreturn function call */
5296 expression_t const *const expr = stmt->expression.expression;
5297 if (expr->kind == EXPR_CALL) {
5298 expression_t const *const func = expr->call.function;
5299 if (func->kind == EXPR_REFERENCE) {
5300 declaration_t const *const decl = func->reference.declaration;
5301 if (decl != NULL && decl->modifiers & DM_NORETURN) {
5307 next = stmt->base.next;
5311 case STATEMENT_CONTINUE: {
5312 statement_t *parent = stmt;
5314 parent = parent->base.parent;
5315 if (parent == NULL) /* continue not within loop */
5319 switch (parent->kind) {
5320 case STATEMENT_WHILE: goto continue_while;
5321 case STATEMENT_DO_WHILE: goto continue_do_while;
5322 case STATEMENT_FOR: goto continue_for;
5329 case STATEMENT_BREAK: {
5330 statement_t *parent = stmt;
5332 parent = parent->base.parent;
5333 if (parent == NULL) /* break not within loop/switch */
5336 switch (parent->kind) {
5337 case STATEMENT_SWITCH:
5338 case STATEMENT_WHILE:
5339 case STATEMENT_DO_WHILE:
5342 next = parent->base.next;
5343 goto found_break_parent;
5352 case STATEMENT_GOTO:
5353 if (stmt->gotos.expression) {
5354 statement_t *parent = stmt->base.parent;
5355 if (parent == NULL) /* top level goto */
5359 next = stmt->gotos.label->init.statement;
5360 if (next == NULL) /* missing label */
5365 case STATEMENT_LABEL:
5366 next = stmt->label.statement;
5369 case STATEMENT_CASE_LABEL:
5370 next = stmt->case_label.statement;
5373 case STATEMENT_WHILE: {
5374 while_statement_t const *const whiles = &stmt->whiles;
5375 int const val = determine_truth(whiles->condition);
5378 check_reachable(whiles->body);
5383 next = stmt->base.next;
5387 case STATEMENT_DO_WHILE:
5388 next = stmt->do_while.body;
5391 case STATEMENT_FOR: {
5392 for_statement_t *const fors = &stmt->fors;
5394 if (fors->condition_reachable)
5396 fors->condition_reachable = true;
5398 expression_t const *const cond = fors->condition;
5400 cond == NULL ? 1 : determine_truth(cond);
5403 check_reachable(fors->body);
5408 next = stmt->base.next;
5412 case STATEMENT_MS_TRY: {
5413 ms_try_statement_t const *const ms_try = &stmt->ms_try;
5414 check_reachable(ms_try->try_statement);
5415 next = ms_try->final_statement;
5419 case STATEMENT_LEAVE: {
5420 statement_t *parent = stmt;
5422 parent = parent->base.parent;
5423 if (parent == NULL) /* __leave not within __try */
5426 if (parent->kind == STATEMENT_MS_TRY) {
5428 next = parent->ms_try.final_statement;
5436 while (next == NULL) {
5437 next = last->base.parent;
5439 noreturn_candidate = false;
5441 type_t *const type = current_function->type;
5442 assert(is_type_function(type));
5443 type_t *const ret = skip_typeref(type->function.return_type);
5444 if (warning.return_type &&
5445 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
5446 is_type_valid(ret) &&
5447 !is_sym_main(current_function->symbol)) {
5448 warningf(&stmt->base.source_position,
5449 "control reaches end of non-void function");
5454 switch (next->kind) {
5455 case STATEMENT_INVALID:
5456 case STATEMENT_EMPTY:
5457 case STATEMENT_DECLARATION:
5458 case STATEMENT_EXPRESSION:
5460 case STATEMENT_RETURN:
5461 case STATEMENT_CONTINUE:
5462 case STATEMENT_BREAK:
5463 case STATEMENT_GOTO:
5464 case STATEMENT_LEAVE:
5465 panic("invalid control flow in function");
5467 case STATEMENT_COMPOUND:
5469 case STATEMENT_SWITCH:
5470 case STATEMENT_LABEL:
5471 case STATEMENT_CASE_LABEL:
5473 next = next->base.next;
5476 case STATEMENT_WHILE: {
5478 if (next->base.reachable)
5480 next->base.reachable = true;
5482 while_statement_t const *const whiles = &next->whiles;
5483 int const val = determine_truth(whiles->condition);
5486 check_reachable(whiles->body);
5492 next = next->base.next;
5496 case STATEMENT_DO_WHILE: {
5498 if (next->base.reachable)
5500 next->base.reachable = true;
5502 do_while_statement_t const *const dw = &next->do_while;
5503 int const val = determine_truth(dw->condition);
5506 check_reachable(dw->body);
5512 next = next->base.next;
5516 case STATEMENT_FOR: {
5518 for_statement_t *const fors = &next->fors;
5520 fors->step_reachable = true;
5522 if (fors->condition_reachable)
5524 fors->condition_reachable = true;
5526 expression_t const *const cond = fors->condition;
5528 cond == NULL ? 1 : determine_truth(cond);
5531 check_reachable(fors->body);
5537 next = next->base.next;
5541 case STATEMENT_MS_TRY:
5543 next = next->ms_try.final_statement;
5549 next = stmt->base.parent;
5551 warningf(&stmt->base.source_position,
5552 "control reaches end of non-void function");
5556 check_reachable(next);
5559 static void check_unreachable(statement_t* const stmt, void *const env)
5563 switch (stmt->kind) {
5564 case STATEMENT_DO_WHILE:
5565 if (!stmt->base.reachable) {
5566 expression_t const *const cond = stmt->do_while.condition;
5567 if (determine_truth(cond) >= 0) {
5568 warningf(&cond->base.source_position,
5569 "condition of do-while-loop is unreachable");
5574 case STATEMENT_FOR: {
5575 for_statement_t const* const fors = &stmt->fors;
5577 // if init and step are unreachable, cond is unreachable, too
5578 if (!stmt->base.reachable && !fors->step_reachable) {
5579 warningf(&stmt->base.source_position, "statement is unreachable");
5581 if (!stmt->base.reachable && fors->initialisation != NULL) {
5582 warningf(&fors->initialisation->base.source_position,
5583 "initialisation of for-statement is unreachable");
5586 if (!fors->condition_reachable && fors->condition != NULL) {
5587 warningf(&fors->condition->base.source_position,
5588 "condition of for-statement is unreachable");
5591 if (!fors->step_reachable && fors->step != NULL) {
5592 warningf(&fors->step->base.source_position,
5593 "step of for-statement is unreachable");
5599 case STATEMENT_COMPOUND:
5600 if (stmt->compound.statements != NULL)
5605 if (!stmt->base.reachable)
5606 warningf(&stmt->base.source_position, "statement is unreachable");
5611 static void parse_external_declaration(void)
5613 /* function-definitions and declarations both start with declaration
5615 declaration_specifiers_t specifiers;
5616 memset(&specifiers, 0, sizeof(specifiers));
5618 add_anchor_token(';');
5619 parse_declaration_specifiers(&specifiers);
5620 rem_anchor_token(';');
5622 /* must be a declaration */
5623 if (token.type == ';') {
5624 parse_anonymous_declaration_rest(&specifiers);
5628 add_anchor_token(',');
5629 add_anchor_token('=');
5630 add_anchor_token(';');
5631 add_anchor_token('{');
5633 /* declarator is common to both function-definitions and declarations */
5634 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5636 rem_anchor_token('{');
5637 rem_anchor_token(';');
5638 rem_anchor_token('=');
5639 rem_anchor_token(',');
5641 /* must be a declaration */
5642 switch (token.type) {
5646 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5650 /* must be a function definition */
5651 parse_kr_declaration_list(ndeclaration);
5653 if (token.type != '{') {
5654 parse_error_expected("while parsing function definition", '{', NULL);
5655 eat_until_matching_token(';');
5659 type_t *type = ndeclaration->type;
5661 /* note that we don't skip typerefs: the standard doesn't allow them here
5662 * (so we can't use is_type_function here) */
5663 if (type->kind != TYPE_FUNCTION) {
5664 if (is_type_valid(type)) {
5665 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5666 type, ndeclaration->symbol);
5672 if (warning.aggregate_return &&
5673 is_type_compound(skip_typeref(type->function.return_type))) {
5674 warningf(HERE, "function '%Y' returns an aggregate",
5675 ndeclaration->symbol);
5677 if (warning.traditional && !type->function.unspecified_parameters) {
5678 warningf(HERE, "traditional C rejects ISO C style function definition of function '%Y'",
5679 ndeclaration->symbol);
5681 if (warning.old_style_definition && type->function.unspecified_parameters) {
5682 warningf(HERE, "old-style function definition '%Y'",
5683 ndeclaration->symbol);
5686 /* § 6.7.5.3 (14) a function definition with () means no
5687 * parameters (and not unspecified parameters) */
5688 if (type->function.unspecified_parameters
5689 && type->function.parameters == NULL
5690 && !type->function.kr_style_parameters) {
5691 type_t *duplicate = duplicate_type(type);
5692 duplicate->function.unspecified_parameters = false;
5694 type = typehash_insert(duplicate);
5695 if (type != duplicate) {
5696 obstack_free(type_obst, duplicate);
5698 ndeclaration->type = type;
5701 declaration_t *const declaration = record_declaration(ndeclaration, true);
5702 if (ndeclaration != declaration) {
5703 declaration->scope = ndeclaration->scope;
5705 type = skip_typeref(declaration->type);
5707 /* push function parameters and switch scope */
5708 size_t const top = environment_top();
5709 scope_push(&declaration->scope);
5711 declaration_t *parameter = declaration->scope.declarations;
5712 for( ; parameter != NULL; parameter = parameter->next) {
5713 if (parameter->parent_scope == &ndeclaration->scope) {
5714 parameter->parent_scope = scope;
5716 assert(parameter->parent_scope == NULL
5717 || parameter->parent_scope == scope);
5718 parameter->parent_scope = scope;
5719 if (parameter->symbol == NULL) {
5720 errorf(¶meter->source_position, "parameter name omitted");
5723 environment_push(parameter);
5726 if (declaration->init.statement != NULL) {
5727 parser_error_multiple_definition(declaration, HERE);
5730 /* parse function body */
5731 int label_stack_top = label_top();
5732 declaration_t *old_current_function = current_function;
5733 current_function = declaration;
5734 current_parent = NULL;
5736 statement_t *const body = parse_compound_statement(false);
5737 declaration->init.statement = body;
5740 check_declarations();
5741 if (warning.return_type ||
5742 warning.unreachable_code ||
5743 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5744 noreturn_candidate = true;
5745 check_reachable(body);
5746 if (warning.unreachable_code)
5747 walk_statements(body, check_unreachable, NULL);
5748 if (warning.missing_noreturn &&
5749 noreturn_candidate &&
5750 !(declaration->modifiers & DM_NORETURN)) {
5751 warningf(&body->base.source_position,
5752 "function '%#T' is candidate for attribute 'noreturn'",
5753 type, declaration->symbol);
5757 assert(current_parent == NULL);
5758 assert(current_function == declaration);
5759 current_function = old_current_function;
5760 label_pop_to(label_stack_top);
5763 assert(scope == &declaration->scope);
5765 environment_pop_to(top);
5768 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5769 source_position_t *source_position,
5770 const symbol_t *symbol)
5772 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
5774 type->bitfield.base_type = base_type;
5775 type->bitfield.size_expression = size;
5778 type_t *skipped_type = skip_typeref(base_type);
5779 if (!is_type_integer(skipped_type)) {
5780 errorf(HERE, "bitfield base type '%T' is not an integer type",
5784 bit_size = skipped_type->base.size * 8;
5787 if (is_constant_expression(size)) {
5788 long v = fold_constant(size);
5791 errorf(source_position, "negative width in bit-field '%Y'",
5793 } else if (v == 0) {
5794 errorf(source_position, "zero width for bit-field '%Y'",
5796 } else if (bit_size > 0 && (il_size_t)v > bit_size) {
5797 errorf(source_position, "width of '%Y' exceeds its type",
5800 type->bitfield.bit_size = v;
5807 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5810 declaration_t *iter = compound_declaration->scope.declarations;
5811 for( ; iter != NULL; iter = iter->next) {
5812 if (iter->namespc != NAMESPACE_NORMAL)
5815 if (iter->symbol == NULL) {
5816 type_t *type = skip_typeref(iter->type);
5817 if (is_type_compound(type)) {
5818 declaration_t *result
5819 = find_compound_entry(type->compound.declaration, symbol);
5826 if (iter->symbol == symbol) {
5834 static void parse_compound_declarators(declaration_t *struct_declaration,
5835 const declaration_specifiers_t *specifiers)
5837 declaration_t *last_declaration = struct_declaration->scope.declarations;
5838 if (last_declaration != NULL) {
5839 while (last_declaration->next != NULL) {
5840 last_declaration = last_declaration->next;
5845 declaration_t *declaration;
5847 if (token.type == ':') {
5848 source_position_t source_position = *HERE;
5851 type_t *base_type = specifiers->type;
5852 expression_t *size = parse_constant_expression();
5854 type_t *type = make_bitfield_type(base_type, size,
5855 &source_position, sym_anonymous);
5857 declaration = allocate_declaration_zero();
5858 declaration->namespc = NAMESPACE_NORMAL;
5859 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5860 declaration->storage_class = STORAGE_CLASS_NONE;
5861 declaration->source_position = source_position;
5862 declaration->modifiers = specifiers->modifiers;
5863 declaration->type = type;
5865 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5867 type_t *orig_type = declaration->type;
5868 type_t *type = skip_typeref(orig_type);
5870 if (token.type == ':') {
5871 source_position_t source_position = *HERE;
5873 expression_t *size = parse_constant_expression();
5875 type_t *bitfield_type = make_bitfield_type(orig_type, size,
5876 &source_position, declaration->symbol);
5877 declaration->type = bitfield_type;
5879 /* TODO we ignore arrays for now... what is missing is a check
5880 * that they're at the end of the struct */
5881 if (is_type_incomplete(type) && !is_type_array(type)) {
5883 "compound member '%Y' has incomplete type '%T'",
5884 declaration->symbol, orig_type);
5885 } else if (is_type_function(type)) {
5886 errorf(HERE, "compound member '%Y' must not have function type '%T'",
5887 declaration->symbol, orig_type);
5892 /* make sure we don't define a symbol multiple times */
5893 symbol_t *symbol = declaration->symbol;
5894 if (symbol != NULL) {
5895 declaration_t *prev_decl
5896 = find_compound_entry(struct_declaration, symbol);
5898 if (prev_decl != NULL) {
5899 assert(prev_decl->symbol == symbol);
5900 errorf(&declaration->source_position,
5901 "multiple declarations of symbol '%Y' (declared %P)",
5902 symbol, &prev_decl->source_position);
5906 /* append declaration */
5907 if (last_declaration != NULL) {
5908 last_declaration->next = declaration;
5910 struct_declaration->scope.declarations = declaration;
5912 last_declaration = declaration;
5914 if (token.type != ',')
5924 static void parse_compound_type_entries(declaration_t *compound_declaration)
5927 add_anchor_token('}');
5929 while (token.type != '}') {
5930 if (token.type == T_EOF) {
5931 errorf(HERE, "EOF while parsing struct");
5934 declaration_specifiers_t specifiers;
5935 memset(&specifiers, 0, sizeof(specifiers));
5936 parse_declaration_specifiers(&specifiers);
5938 parse_compound_declarators(compound_declaration, &specifiers);
5940 rem_anchor_token('}');
5944 static type_t *parse_typename(void)
5946 declaration_specifiers_t specifiers;
5947 memset(&specifiers, 0, sizeof(specifiers));
5948 parse_declaration_specifiers(&specifiers);
5949 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5950 /* TODO: improve error message, user does probably not know what a
5951 * storage class is...
5953 errorf(HERE, "typename may not have a storage class");
5956 type_t *result = parse_abstract_declarator(specifiers.type);
5964 typedef expression_t* (*parse_expression_function)(void);
5965 typedef expression_t* (*parse_expression_infix_function)(expression_t *left);
5967 typedef struct expression_parser_function_t expression_parser_function_t;
5968 struct expression_parser_function_t {
5969 unsigned precedence;
5970 parse_expression_function parser;
5971 unsigned infix_precedence;
5972 parse_expression_infix_function infix_parser;
5975 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5978 * Prints an error message if an expression was expected but not read
5980 static expression_t *expected_expression_error(void)
5982 /* skip the error message if the error token was read */
5983 if (token.type != T_ERROR) {
5984 errorf(HERE, "expected expression, got token '%K'", &token);
5988 return create_invalid_expression();
5992 * Parse a string constant.
5994 static expression_t *parse_string_const(void)
5997 if (token.type == T_STRING_LITERAL) {
5998 string_t res = token.v.string;
6000 while (token.type == T_STRING_LITERAL) {
6001 res = concat_strings(&res, &token.v.string);
6004 if (token.type != T_WIDE_STRING_LITERAL) {
6005 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
6006 /* note: that we use type_char_ptr here, which is already the
6007 * automatic converted type. revert_automatic_type_conversion
6008 * will construct the array type */
6009 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
6010 cnst->string.value = res;
6014 wres = concat_string_wide_string(&res, &token.v.wide_string);
6016 wres = token.v.wide_string;
6021 switch (token.type) {
6022 case T_WIDE_STRING_LITERAL:
6023 wres = concat_wide_strings(&wres, &token.v.wide_string);
6026 case T_STRING_LITERAL:
6027 wres = concat_wide_string_string(&wres, &token.v.string);
6031 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
6032 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
6033 cnst->wide_string.value = wres;
6042 * Parse an integer constant.
6044 static expression_t *parse_int_const(void)
6046 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6047 cnst->base.source_position = *HERE;
6048 cnst->base.type = token.datatype;
6049 cnst->conste.v.int_value = token.v.intvalue;
6057 * Parse a character constant.
6059 static expression_t *parse_character_constant(void)
6061 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
6063 cnst->base.source_position = *HERE;
6064 cnst->base.type = token.datatype;
6065 cnst->conste.v.character = token.v.string;
6067 if (cnst->conste.v.character.size != 1) {
6068 if (warning.multichar && GNU_MODE) {
6069 warningf(HERE, "multi-character character constant");
6071 errorf(HERE, "more than 1 characters in character constant");
6080 * Parse a wide character constant.
6082 static expression_t *parse_wide_character_constant(void)
6084 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
6086 cnst->base.source_position = *HERE;
6087 cnst->base.type = token.datatype;
6088 cnst->conste.v.wide_character = token.v.wide_string;
6090 if (cnst->conste.v.wide_character.size != 1) {
6091 if (warning.multichar && GNU_MODE) {
6092 warningf(HERE, "multi-character character constant");
6094 errorf(HERE, "more than 1 characters in character constant");
6103 * Parse a float constant.
6105 static expression_t *parse_float_const(void)
6107 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6108 cnst->base.type = token.datatype;
6109 cnst->conste.v.float_value = token.v.floatvalue;
6116 static declaration_t *create_implicit_function(symbol_t *symbol,
6117 const source_position_t *source_position)
6119 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
6120 ntype->function.return_type = type_int;
6121 ntype->function.unspecified_parameters = true;
6123 type_t *type = typehash_insert(ntype);
6124 if (type != ntype) {
6128 declaration_t *const declaration = allocate_declaration_zero();
6129 declaration->storage_class = STORAGE_CLASS_EXTERN;
6130 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
6131 declaration->type = type;
6132 declaration->symbol = symbol;
6133 declaration->source_position = *source_position;
6134 declaration->implicit = true;
6136 bool strict_prototypes_old = warning.strict_prototypes;
6137 warning.strict_prototypes = false;
6138 record_declaration(declaration, false);
6139 warning.strict_prototypes = strict_prototypes_old;
6145 * Creates a return_type (func)(argument_type) function type if not
6148 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
6149 type_t *argument_type2)
6151 function_parameter_t *parameter2
6152 = obstack_alloc(type_obst, sizeof(parameter2[0]));
6153 memset(parameter2, 0, sizeof(parameter2[0]));
6154 parameter2->type = argument_type2;
6156 function_parameter_t *parameter1
6157 = obstack_alloc(type_obst, sizeof(parameter1[0]));
6158 memset(parameter1, 0, sizeof(parameter1[0]));
6159 parameter1->type = argument_type1;
6160 parameter1->next = parameter2;
6162 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6163 type->function.return_type = return_type;
6164 type->function.parameters = parameter1;
6166 type_t *result = typehash_insert(type);
6167 if (result != type) {
6175 * Creates a return_type (func)(argument_type) function type if not
6178 * @param return_type the return type
6179 * @param argument_type the argument type
6181 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
6183 function_parameter_t *parameter
6184 = obstack_alloc(type_obst, sizeof(parameter[0]));
6185 memset(parameter, 0, sizeof(parameter[0]));
6186 parameter->type = argument_type;
6188 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6189 type->function.return_type = return_type;
6190 type->function.parameters = parameter;
6192 type_t *result = typehash_insert(type);
6193 if (result != type) {
6200 static type_t *make_function_0_type(type_t *return_type)
6202 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6203 type->function.return_type = return_type;
6204 type->function.parameters = NULL;
6206 type_t *result = typehash_insert(type);
6207 if (result != type) {
6215 * Creates a function type for some function like builtins.
6217 * @param symbol the symbol describing the builtin
6219 static type_t *get_builtin_symbol_type(symbol_t *symbol)
6221 switch(symbol->ID) {
6222 case T___builtin_alloca:
6223 return make_function_1_type(type_void_ptr, type_size_t);
6224 case T___builtin_huge_val:
6225 return make_function_0_type(type_double);
6226 case T___builtin_inf:
6227 return make_function_0_type(type_double);
6228 case T___builtin_inff:
6229 return make_function_0_type(type_float);
6230 case T___builtin_infl:
6231 return make_function_0_type(type_long_double);
6232 case T___builtin_nan:
6233 return make_function_1_type(type_double, type_char_ptr);
6234 case T___builtin_nanf:
6235 return make_function_1_type(type_float, type_char_ptr);
6236 case T___builtin_nanl:
6237 return make_function_1_type(type_long_double, type_char_ptr);
6238 case T___builtin_va_end:
6239 return make_function_1_type(type_void, type_valist);
6240 case T___builtin_expect:
6241 return make_function_2_type(type_long, type_long, type_long);
6243 internal_errorf(HERE, "not implemented builtin symbol found");
6248 * Performs automatic type cast as described in § 6.3.2.1.
6250 * @param orig_type the original type
6252 static type_t *automatic_type_conversion(type_t *orig_type)
6254 type_t *type = skip_typeref(orig_type);
6255 if (is_type_array(type)) {
6256 array_type_t *array_type = &type->array;
6257 type_t *element_type = array_type->element_type;
6258 unsigned qualifiers = array_type->base.qualifiers;
6260 return make_pointer_type(element_type, qualifiers);
6263 if (is_type_function(type)) {
6264 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6271 * reverts the automatic casts of array to pointer types and function
6272 * to function-pointer types as defined § 6.3.2.1
6274 type_t *revert_automatic_type_conversion(const expression_t *expression)
6276 switch (expression->kind) {
6277 case EXPR_REFERENCE: return expression->reference.declaration->type;
6280 return get_qualified_type(expression->select.compound_entry->type,
6281 expression->base.type->base.qualifiers);
6283 case EXPR_UNARY_DEREFERENCE: {
6284 const expression_t *const value = expression->unary.value;
6285 type_t *const type = skip_typeref(value->base.type);
6286 assert(is_type_pointer(type));
6287 return type->pointer.points_to;
6290 case EXPR_BUILTIN_SYMBOL:
6291 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
6293 case EXPR_ARRAY_ACCESS: {
6294 const expression_t *array_ref = expression->array_access.array_ref;
6295 type_t *type_left = skip_typeref(array_ref->base.type);
6296 if (!is_type_valid(type_left))
6298 assert(is_type_pointer(type_left));
6299 return type_left->pointer.points_to;
6302 case EXPR_STRING_LITERAL: {
6303 size_t size = expression->string.value.size;
6304 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
6307 case EXPR_WIDE_STRING_LITERAL: {
6308 size_t size = expression->wide_string.value.size;
6309 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
6312 case EXPR_COMPOUND_LITERAL:
6313 return expression->compound_literal.type;
6318 return expression->base.type;
6321 static expression_t *parse_reference(void)
6323 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
6325 reference_expression_t *ref = &expression->reference;
6326 symbol_t *const symbol = token.v.symbol;
6328 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
6330 if (declaration == NULL) {
6331 if (!strict_mode && look_ahead(1)->type == '(') {
6332 /* an implicitly declared function */
6333 if (warning.implicit_function_declaration) {
6334 warningf(HERE, "implicit declaration of function '%Y'",
6338 declaration = create_implicit_function(symbol, HERE);
6340 errorf(HERE, "unknown symbol '%Y' found.", symbol);
6341 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
6345 type_t *orig_type = declaration->type;
6347 /* we always do the auto-type conversions; the & and sizeof parser contains
6348 * code to revert this! */
6349 type_t *type = automatic_type_conversion(orig_type);
6351 ref->declaration = declaration;
6352 ref->base.type = type;
6354 /* this declaration is used */
6355 declaration->used = true;
6357 if (declaration->parent_scope != file_scope &&
6358 declaration->parent_scope->depth < current_function->scope.depth &&
6359 is_type_valid(orig_type) && !is_type_function(orig_type)) {
6360 /* access of a variable from an outer function */
6361 declaration->address_taken = true;
6362 current_function->need_closure = true;
6365 /* check for deprecated functions */
6366 if (warning.deprecated_declarations &&
6367 declaration->modifiers & DM_DEPRECATED) {
6368 char const *const prefix = is_type_function(declaration->type) ?
6369 "function" : "variable";
6371 if (declaration->deprecated_string != NULL) {
6372 warningf(HERE, "%s '%Y' is deprecated (declared %P): \"%s\"",
6373 prefix, declaration->symbol, &declaration->source_position,
6374 declaration->deprecated_string);
6376 warningf(HERE, "%s '%Y' is deprecated (declared %P)", prefix,
6377 declaration->symbol, &declaration->source_position);
6380 if (warning.init_self && declaration == current_init_decl && !in_type_prop) {
6381 current_init_decl = NULL;
6382 warningf(HERE, "variable '%#T' is initialized by itself",
6383 declaration->type, declaration->symbol);
6390 static bool semantic_cast(expression_t *cast)
6392 expression_t *expression = cast->unary.value;
6393 type_t *orig_dest_type = cast->base.type;
6394 type_t *orig_type_right = expression->base.type;
6395 type_t const *dst_type = skip_typeref(orig_dest_type);
6396 type_t const *src_type = skip_typeref(orig_type_right);
6397 source_position_t const *pos = &cast->base.source_position;
6399 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
6400 if (dst_type == type_void)
6403 /* only integer and pointer can be casted to pointer */
6404 if (is_type_pointer(dst_type) &&
6405 !is_type_pointer(src_type) &&
6406 !is_type_integer(src_type) &&
6407 is_type_valid(src_type)) {
6408 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
6412 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
6413 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
6417 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
6418 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
6422 if (warning.cast_qual &&
6423 is_type_pointer(src_type) &&
6424 is_type_pointer(dst_type)) {
6425 type_t *src = skip_typeref(src_type->pointer.points_to);
6426 type_t *dst = skip_typeref(dst_type->pointer.points_to);
6427 unsigned missing_qualifiers =
6428 src->base.qualifiers & ~dst->base.qualifiers;
6429 if (missing_qualifiers != 0) {
6431 "cast discards qualifiers '%Q' in pointer target type of '%T'",
6432 missing_qualifiers, orig_type_right);
6438 static expression_t *parse_compound_literal(type_t *type)
6440 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
6442 parse_initializer_env_t env;
6444 env.declaration = NULL;
6445 env.must_be_constant = false;
6446 initializer_t *initializer = parse_initializer(&env);
6449 expression->compound_literal.initializer = initializer;
6450 expression->compound_literal.type = type;
6451 expression->base.type = automatic_type_conversion(type);
6457 * Parse a cast expression.
6459 static expression_t *parse_cast(void)
6461 add_anchor_token(')');
6463 source_position_t source_position = token.source_position;
6465 type_t *type = parse_typename();
6467 rem_anchor_token(')');
6470 if (token.type == '{') {
6471 return parse_compound_literal(type);
6474 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6475 cast->base.source_position = source_position;
6477 expression_t *value = parse_sub_expression(PREC_CAST);
6478 cast->base.type = type;
6479 cast->unary.value = value;
6481 if (! semantic_cast(cast)) {
6482 /* TODO: record the error in the AST. else it is impossible to detect it */
6487 return create_invalid_expression();
6491 * Parse a statement expression.
6493 static expression_t *parse_statement_expression(void)
6495 add_anchor_token(')');
6497 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6499 statement_t *statement = parse_compound_statement(true);
6500 expression->statement.statement = statement;
6501 expression->base.source_position = statement->base.source_position;
6503 /* find last statement and use its type */
6504 type_t *type = type_void;
6505 const statement_t *stmt = statement->compound.statements;
6507 while (stmt->base.next != NULL)
6508 stmt = stmt->base.next;
6510 if (stmt->kind == STATEMENT_EXPRESSION) {
6511 type = stmt->expression.expression->base.type;
6514 warningf(&expression->base.source_position, "empty statement expression ({})");
6516 expression->base.type = type;
6518 rem_anchor_token(')');
6526 * Parse a parenthesized expression.
6528 static expression_t *parse_parenthesized_expression(void)
6532 switch(token.type) {
6534 /* gcc extension: a statement expression */
6535 return parse_statement_expression();
6539 return parse_cast();
6541 if (is_typedef_symbol(token.v.symbol)) {
6542 return parse_cast();
6546 add_anchor_token(')');
6547 expression_t *result = parse_expression();
6548 rem_anchor_token(')');
6555 static expression_t *parse_function_keyword(void)
6560 if (current_function == NULL) {
6561 errorf(HERE, "'__func__' used outside of a function");
6564 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6565 expression->base.type = type_char_ptr;
6566 expression->funcname.kind = FUNCNAME_FUNCTION;
6571 static expression_t *parse_pretty_function_keyword(void)
6573 eat(T___PRETTY_FUNCTION__);
6575 if (current_function == NULL) {
6576 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6579 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6580 expression->base.type = type_char_ptr;
6581 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6586 static expression_t *parse_funcsig_keyword(void)
6590 if (current_function == NULL) {
6591 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6594 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6595 expression->base.type = type_char_ptr;
6596 expression->funcname.kind = FUNCNAME_FUNCSIG;
6601 static expression_t *parse_funcdname_keyword(void)
6603 eat(T___FUNCDNAME__);
6605 if (current_function == NULL) {
6606 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6609 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6610 expression->base.type = type_char_ptr;
6611 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6616 static designator_t *parse_designator(void)
6618 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6619 result->source_position = *HERE;
6621 if (token.type != T_IDENTIFIER) {
6622 parse_error_expected("while parsing member designator",
6623 T_IDENTIFIER, NULL);
6626 result->symbol = token.v.symbol;
6629 designator_t *last_designator = result;
6631 if (token.type == '.') {
6633 if (token.type != T_IDENTIFIER) {
6634 parse_error_expected("while parsing member designator",
6635 T_IDENTIFIER, NULL);
6638 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6639 designator->source_position = *HERE;
6640 designator->symbol = token.v.symbol;
6643 last_designator->next = designator;
6644 last_designator = designator;
6647 if (token.type == '[') {
6649 add_anchor_token(']');
6650 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6651 designator->source_position = *HERE;
6652 designator->array_index = parse_expression();
6653 rem_anchor_token(']');
6655 if (designator->array_index == NULL) {
6659 last_designator->next = designator;
6660 last_designator = designator;
6672 * Parse the __builtin_offsetof() expression.
6674 static expression_t *parse_offsetof(void)
6676 eat(T___builtin_offsetof);
6678 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6679 expression->base.type = type_size_t;
6682 add_anchor_token(',');
6683 type_t *type = parse_typename();
6684 rem_anchor_token(',');
6686 add_anchor_token(')');
6687 designator_t *designator = parse_designator();
6688 rem_anchor_token(')');
6691 expression->offsetofe.type = type;
6692 expression->offsetofe.designator = designator;
6695 memset(&path, 0, sizeof(path));
6696 path.top_type = type;
6697 path.path = NEW_ARR_F(type_path_entry_t, 0);
6699 descend_into_subtype(&path);
6701 if (!walk_designator(&path, designator, true)) {
6702 return create_invalid_expression();
6705 DEL_ARR_F(path.path);
6709 return create_invalid_expression();
6713 * Parses a _builtin_va_start() expression.
6715 static expression_t *parse_va_start(void)
6717 eat(T___builtin_va_start);
6719 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6722 add_anchor_token(',');
6723 expression->va_starte.ap = parse_assignment_expression();
6724 rem_anchor_token(',');
6726 expression_t *const expr = parse_assignment_expression();
6727 if (expr->kind == EXPR_REFERENCE) {
6728 declaration_t *const decl = expr->reference.declaration;
6729 if (decl->parent_scope != ¤t_function->scope || decl->next != NULL) {
6730 errorf(&expr->base.source_position,
6731 "second argument of 'va_start' must be last parameter of the current function");
6733 expression->va_starte.parameter = decl;
6739 return create_invalid_expression();
6743 * Parses a _builtin_va_arg() expression.
6745 static expression_t *parse_va_arg(void)
6747 eat(T___builtin_va_arg);
6749 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6752 expression->va_arge.ap = parse_assignment_expression();
6754 expression->base.type = parse_typename();
6759 return create_invalid_expression();
6762 static expression_t *parse_builtin_symbol(void)
6764 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6766 symbol_t *symbol = token.v.symbol;
6768 expression->builtin_symbol.symbol = symbol;
6771 type_t *type = get_builtin_symbol_type(symbol);
6772 type = automatic_type_conversion(type);
6774 expression->base.type = type;
6779 * Parses a __builtin_constant() expression.
6781 static expression_t *parse_builtin_constant(void)
6783 eat(T___builtin_constant_p);
6785 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6788 add_anchor_token(')');
6789 expression->builtin_constant.value = parse_assignment_expression();
6790 rem_anchor_token(')');
6792 expression->base.type = type_int;
6796 return create_invalid_expression();
6800 * Parses a __builtin_prefetch() expression.
6802 static expression_t *parse_builtin_prefetch(void)
6804 eat(T___builtin_prefetch);
6806 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6809 add_anchor_token(')');
6810 expression->builtin_prefetch.adr = parse_assignment_expression();
6811 if (token.type == ',') {
6813 expression->builtin_prefetch.rw = parse_assignment_expression();
6815 if (token.type == ',') {
6817 expression->builtin_prefetch.locality = parse_assignment_expression();
6819 rem_anchor_token(')');
6821 expression->base.type = type_void;
6825 return create_invalid_expression();
6829 * Parses a __builtin_is_*() compare expression.
6831 static expression_t *parse_compare_builtin(void)
6833 expression_t *expression;
6835 switch(token.type) {
6836 case T___builtin_isgreater:
6837 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6839 case T___builtin_isgreaterequal:
6840 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6842 case T___builtin_isless:
6843 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6845 case T___builtin_islessequal:
6846 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6848 case T___builtin_islessgreater:
6849 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6851 case T___builtin_isunordered:
6852 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6855 internal_errorf(HERE, "invalid compare builtin found");
6857 expression->base.source_position = *HERE;
6861 expression->binary.left = parse_assignment_expression();
6863 expression->binary.right = parse_assignment_expression();
6866 type_t *const orig_type_left = expression->binary.left->base.type;
6867 type_t *const orig_type_right = expression->binary.right->base.type;
6869 type_t *const type_left = skip_typeref(orig_type_left);
6870 type_t *const type_right = skip_typeref(orig_type_right);
6871 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6872 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6873 type_error_incompatible("invalid operands in comparison",
6874 &expression->base.source_position, orig_type_left, orig_type_right);
6877 semantic_comparison(&expression->binary);
6882 return create_invalid_expression();
6887 * Parses a __builtin_expect() expression.
6889 static expression_t *parse_builtin_expect(void)
6891 eat(T___builtin_expect);
6893 expression_t *expression
6894 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6897 expression->binary.left = parse_assignment_expression();
6899 expression->binary.right = parse_constant_expression();
6902 expression->base.type = expression->binary.left->base.type;
6906 return create_invalid_expression();
6911 * Parses a MS assume() expression.
6913 static expression_t *parse_assume(void)
6917 expression_t *expression
6918 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6921 add_anchor_token(')');
6922 expression->unary.value = parse_assignment_expression();
6923 rem_anchor_token(')');
6926 expression->base.type = type_void;
6929 return create_invalid_expression();
6933 * Return the declaration for a given label symbol or create a new one.
6935 * @param symbol the symbol of the label
6937 static declaration_t *get_label(symbol_t *symbol)
6939 declaration_t *candidate;
6940 assert(current_function != NULL);
6942 candidate = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
6943 /* if we found a local label, we already created the declaration */
6944 if (candidate != NULL) {
6945 if (candidate->parent_scope != scope) {
6946 assert(candidate->parent_scope->depth < scope->depth);
6947 current_function->goto_to_outer = true;
6952 candidate = get_declaration(symbol, NAMESPACE_LABEL);
6953 /* if we found a label in the same function, then we already created the
6955 if (candidate != NULL
6956 && candidate->parent_scope == ¤t_function->scope) {
6960 /* otherwise we need to create a new one */
6961 declaration_t *const declaration = allocate_declaration_zero();
6962 declaration->namespc = NAMESPACE_LABEL;
6963 declaration->symbol = symbol;
6965 label_push(declaration);
6971 * Parses a GNU && label address expression.
6973 static expression_t *parse_label_address(void)
6975 source_position_t source_position = token.source_position;
6977 if (token.type != T_IDENTIFIER) {
6978 parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
6981 symbol_t *symbol = token.v.symbol;
6984 declaration_t *label = get_label(symbol);
6987 label->address_taken = true;
6989 expression_t *expression = allocate_expression_zero(EXPR_LABEL_ADDRESS);
6990 expression->base.source_position = source_position;
6992 /* label address is threaten as a void pointer */
6993 expression->base.type = type_void_ptr;
6994 expression->label_address.declaration = label;
6997 return create_invalid_expression();
7001 * Parse a microsoft __noop expression.
7003 static expression_t *parse_noop_expression(void)
7005 source_position_t source_position = *HERE;
7008 if (token.type == '(') {
7009 /* parse arguments */
7011 add_anchor_token(')');
7012 add_anchor_token(',');
7014 if (token.type != ')') {
7016 (void)parse_assignment_expression();
7017 if (token.type != ',')
7023 rem_anchor_token(',');
7024 rem_anchor_token(')');
7027 /* the result is a (int)0 */
7028 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
7029 cnst->base.source_position = source_position;
7030 cnst->base.type = type_int;
7031 cnst->conste.v.int_value = 0;
7032 cnst->conste.is_ms_noop = true;
7037 return create_invalid_expression();
7041 * Parses a primary expression.
7043 static expression_t *parse_primary_expression(void)
7045 switch (token.type) {
7046 case T_INTEGER: return parse_int_const();
7047 case T_CHARACTER_CONSTANT: return parse_character_constant();
7048 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
7049 case T_FLOATINGPOINT: return parse_float_const();
7050 case T_STRING_LITERAL:
7051 case T_WIDE_STRING_LITERAL: return parse_string_const();
7052 case T_IDENTIFIER: return parse_reference();
7053 case T___FUNCTION__:
7054 case T___func__: return parse_function_keyword();
7055 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
7056 case T___FUNCSIG__: return parse_funcsig_keyword();
7057 case T___FUNCDNAME__: return parse_funcdname_keyword();
7058 case T___builtin_offsetof: return parse_offsetof();
7059 case T___builtin_va_start: return parse_va_start();
7060 case T___builtin_va_arg: return parse_va_arg();
7061 case T___builtin_expect:
7062 case T___builtin_alloca:
7063 case T___builtin_inf:
7064 case T___builtin_inff:
7065 case T___builtin_infl:
7066 case T___builtin_nan:
7067 case T___builtin_nanf:
7068 case T___builtin_nanl:
7069 case T___builtin_huge_val:
7070 case T___builtin_va_end: return parse_builtin_symbol();
7071 case T___builtin_isgreater:
7072 case T___builtin_isgreaterequal:
7073 case T___builtin_isless:
7074 case T___builtin_islessequal:
7075 case T___builtin_islessgreater:
7076 case T___builtin_isunordered: return parse_compare_builtin();
7077 case T___builtin_constant_p: return parse_builtin_constant();
7078 case T___builtin_prefetch: return parse_builtin_prefetch();
7079 case T__assume: return parse_assume();
7082 return parse_label_address();
7085 case '(': return parse_parenthesized_expression();
7086 case T___noop: return parse_noop_expression();
7089 errorf(HERE, "unexpected token %K, expected an expression", &token);
7090 return create_invalid_expression();
7094 * Check if the expression has the character type and issue a warning then.
7096 static void check_for_char_index_type(const expression_t *expression)
7098 type_t *const type = expression->base.type;
7099 const type_t *const base_type = skip_typeref(type);
7101 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
7102 warning.char_subscripts) {
7103 warningf(&expression->base.source_position,
7104 "array subscript has type '%T'", type);
7108 static expression_t *parse_array_expression(expression_t *left)
7111 add_anchor_token(']');
7113 expression_t *inside = parse_expression();
7115 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
7117 array_access_expression_t *array_access = &expression->array_access;
7119 type_t *const orig_type_left = left->base.type;
7120 type_t *const orig_type_inside = inside->base.type;
7122 type_t *const type_left = skip_typeref(orig_type_left);
7123 type_t *const type_inside = skip_typeref(orig_type_inside);
7125 type_t *return_type;
7126 if (is_type_pointer(type_left)) {
7127 return_type = type_left->pointer.points_to;
7128 array_access->array_ref = left;
7129 array_access->index = inside;
7130 check_for_char_index_type(inside);
7131 } else if (is_type_pointer(type_inside)) {
7132 return_type = type_inside->pointer.points_to;
7133 array_access->array_ref = inside;
7134 array_access->index = left;
7135 array_access->flipped = true;
7136 check_for_char_index_type(left);
7138 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
7140 "array access on object with non-pointer types '%T', '%T'",
7141 orig_type_left, orig_type_inside);
7143 return_type = type_error_type;
7144 array_access->array_ref = left;
7145 array_access->index = inside;
7148 expression->base.type = automatic_type_conversion(return_type);
7150 rem_anchor_token(']');
7151 if (token.type == ']') {
7154 parse_error_expected("Problem while parsing array access", ']', NULL);
7159 static expression_t *parse_typeprop(expression_kind_t const kind,
7160 source_position_t const pos)
7162 expression_t *tp_expression = allocate_expression_zero(kind);
7163 tp_expression->base.type = type_size_t;
7164 tp_expression->base.source_position = pos;
7166 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
7168 /* we only refer to a type property, mark this case */
7169 bool old = in_type_prop;
7170 in_type_prop = true;
7171 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
7173 add_anchor_token(')');
7174 type_t* const orig_type = parse_typename();
7175 tp_expression->typeprop.type = orig_type;
7177 type_t const* const type = skip_typeref(orig_type);
7178 char const* const wrong_type =
7179 is_type_incomplete(type) ? "incomplete" :
7180 type->kind == TYPE_FUNCTION ? "function designator" :
7181 type->kind == TYPE_BITFIELD ? "bitfield" :
7183 if (wrong_type != NULL) {
7184 errorf(&pos, "operand of %s expression must not be %s type '%T'",
7185 what, wrong_type, type);
7188 rem_anchor_token(')');
7191 expression_t *expression = parse_sub_expression(PREC_UNARY);
7193 type_t* const orig_type = revert_automatic_type_conversion(expression);
7194 expression->base.type = orig_type;
7196 type_t const* const type = skip_typeref(orig_type);
7197 char const* const wrong_type =
7198 is_type_incomplete(type) ? "incomplete" :
7199 type->kind == TYPE_FUNCTION ? "function designator" :
7200 type->kind == TYPE_BITFIELD ? "bitfield" :
7202 if (wrong_type != NULL) {
7203 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
7206 tp_expression->typeprop.type = expression->base.type;
7207 tp_expression->typeprop.tp_expression = expression;
7212 return tp_expression;
7215 static expression_t *parse_sizeof(void)
7217 source_position_t pos = *HERE;
7219 return parse_typeprop(EXPR_SIZEOF, pos);
7222 static expression_t *parse_alignof(void)
7224 source_position_t pos = *HERE;
7226 return parse_typeprop(EXPR_ALIGNOF, pos);
7229 static expression_t *parse_select_expression(expression_t *compound)
7231 assert(token.type == '.' || token.type == T_MINUSGREATER);
7233 bool is_pointer = (token.type == T_MINUSGREATER);
7236 expression_t *select = allocate_expression_zero(EXPR_SELECT);
7237 select->select.compound = compound;
7239 if (token.type != T_IDENTIFIER) {
7240 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
7243 symbol_t *symbol = token.v.symbol;
7246 type_t *const orig_type = compound->base.type;
7247 type_t *const type = skip_typeref(orig_type);
7250 bool saw_error = false;
7251 if (is_type_pointer(type)) {
7254 "request for member '%Y' in something not a struct or union, but '%T'",
7258 type_left = skip_typeref(type->pointer.points_to);
7260 if (is_pointer && is_type_valid(type)) {
7261 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
7267 declaration_t *entry;
7268 if (type_left->kind == TYPE_COMPOUND_STRUCT ||
7269 type_left->kind == TYPE_COMPOUND_UNION) {
7270 declaration_t *const declaration = type_left->compound.declaration;
7272 if (!declaration->init.complete) {
7273 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
7275 goto create_error_entry;
7278 entry = find_compound_entry(declaration, symbol);
7279 if (entry == NULL) {
7280 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
7281 goto create_error_entry;
7284 if (is_type_valid(type_left) && !saw_error) {
7286 "request for member '%Y' in something not a struct or union, but '%T'",
7290 entry = allocate_declaration_zero();
7291 entry->symbol = symbol;
7294 select->select.compound_entry = entry;
7296 type_t *const res_type =
7297 get_qualified_type(entry->type, type_left->base.qualifiers);
7299 /* we always do the auto-type conversions; the & and sizeof parser contains
7300 * code to revert this! */
7301 select->base.type = automatic_type_conversion(res_type);
7303 type_t *skipped = skip_typeref(res_type);
7304 if (skipped->kind == TYPE_BITFIELD) {
7305 select->base.type = skipped->bitfield.base_type;
7311 static void check_call_argument(const function_parameter_t *parameter,
7312 call_argument_t *argument, unsigned pos)
7314 type_t *expected_type = parameter->type;
7315 type_t *expected_type_skip = skip_typeref(expected_type);
7316 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
7317 expression_t *arg_expr = argument->expression;
7318 type_t *arg_type = skip_typeref(arg_expr->base.type);
7320 /* handle transparent union gnu extension */
7321 if (is_type_union(expected_type_skip)
7322 && (expected_type_skip->base.modifiers
7323 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
7324 declaration_t *union_decl = expected_type_skip->compound.declaration;
7326 declaration_t *declaration = union_decl->scope.declarations;
7327 type_t *best_type = NULL;
7328 for ( ; declaration != NULL; declaration = declaration->next) {
7329 type_t *decl_type = declaration->type;
7330 error = semantic_assign(decl_type, arg_expr);
7331 if (error == ASSIGN_ERROR_INCOMPATIBLE
7332 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
7335 if (error == ASSIGN_SUCCESS) {
7336 best_type = decl_type;
7337 } else if (best_type == NULL) {
7338 best_type = decl_type;
7342 if (best_type != NULL) {
7343 expected_type = best_type;
7347 error = semantic_assign(expected_type, arg_expr);
7348 argument->expression = create_implicit_cast(argument->expression,
7351 if (error != ASSIGN_SUCCESS) {
7352 /* report exact scope in error messages (like "in argument 3") */
7354 snprintf(buf, sizeof(buf), "call argument %u", pos);
7355 report_assign_error(error, expected_type, arg_expr, buf,
7356 &arg_expr->base.source_position);
7357 } else if (warning.traditional || warning.conversion) {
7358 type_t *const promoted_type = get_default_promoted_type(arg_type);
7359 if (!types_compatible(expected_type_skip, promoted_type) &&
7360 !types_compatible(expected_type_skip, type_void_ptr) &&
7361 !types_compatible(type_void_ptr, promoted_type)) {
7362 /* Deliberately show the skipped types in this warning */
7363 warningf(&arg_expr->base.source_position,
7364 "passing call argument %u as '%T' rather than '%T' due to prototype",
7365 pos, expected_type_skip, promoted_type);
7371 * Parse a call expression, ie. expression '( ... )'.
7373 * @param expression the function address
7375 static expression_t *parse_call_expression(expression_t *expression)
7377 expression_t *result = allocate_expression_zero(EXPR_CALL);
7378 result->base.source_position = expression->base.source_position;
7380 call_expression_t *call = &result->call;
7381 call->function = expression;
7383 type_t *const orig_type = expression->base.type;
7384 type_t *const type = skip_typeref(orig_type);
7386 function_type_t *function_type = NULL;
7387 if (is_type_pointer(type)) {
7388 type_t *const to_type = skip_typeref(type->pointer.points_to);
7390 if (is_type_function(to_type)) {
7391 function_type = &to_type->function;
7392 call->base.type = function_type->return_type;
7396 if (function_type == NULL && is_type_valid(type)) {
7397 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
7400 /* parse arguments */
7402 add_anchor_token(')');
7403 add_anchor_token(',');
7405 if (token.type != ')') {
7406 call_argument_t *last_argument = NULL;
7409 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7411 argument->expression = parse_assignment_expression();
7412 if (last_argument == NULL) {
7413 call->arguments = argument;
7415 last_argument->next = argument;
7417 last_argument = argument;
7419 if (token.type != ',')
7424 rem_anchor_token(',');
7425 rem_anchor_token(')');
7428 if (function_type == NULL)
7431 function_parameter_t *parameter = function_type->parameters;
7432 call_argument_t *argument = call->arguments;
7433 if (!function_type->unspecified_parameters) {
7434 for (unsigned pos = 0; parameter != NULL && argument != NULL;
7435 parameter = parameter->next, argument = argument->next) {
7436 check_call_argument(parameter, argument, ++pos);
7439 if (parameter != NULL) {
7440 errorf(HERE, "too few arguments to function '%E'", expression);
7441 } else if (argument != NULL && !function_type->variadic) {
7442 errorf(HERE, "too many arguments to function '%E'", expression);
7446 /* do default promotion */
7447 for( ; argument != NULL; argument = argument->next) {
7448 type_t *type = argument->expression->base.type;
7450 type = get_default_promoted_type(type);
7452 argument->expression
7453 = create_implicit_cast(argument->expression, type);
7456 check_format(&result->call);
7458 if (warning.aggregate_return &&
7459 is_type_compound(skip_typeref(function_type->return_type))) {
7460 warningf(&result->base.source_position,
7461 "function call has aggregate value");
7468 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
7470 static bool same_compound_type(const type_t *type1, const type_t *type2)
7473 is_type_compound(type1) &&
7474 type1->kind == type2->kind &&
7475 type1->compound.declaration == type2->compound.declaration;
7479 * Parse a conditional expression, ie. 'expression ? ... : ...'.
7481 * @param expression the conditional expression
7483 static expression_t *parse_conditional_expression(expression_t *expression)
7485 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
7487 conditional_expression_t *conditional = &result->conditional;
7488 conditional->base.source_position = *HERE;
7489 conditional->condition = expression;
7492 add_anchor_token(':');
7495 type_t *const condition_type_orig = expression->base.type;
7496 type_t *const condition_type = skip_typeref(condition_type_orig);
7497 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
7498 type_error("expected a scalar type in conditional condition",
7499 &expression->base.source_position, condition_type_orig);
7502 expression_t *true_expression = expression;
7503 bool gnu_cond = false;
7504 if (GNU_MODE && token.type == ':') {
7507 true_expression = parse_expression();
7508 rem_anchor_token(':');
7510 expression_t *false_expression = parse_sub_expression(PREC_CONDITIONAL);
7512 type_t *const orig_true_type = true_expression->base.type;
7513 type_t *const orig_false_type = false_expression->base.type;
7514 type_t *const true_type = skip_typeref(orig_true_type);
7515 type_t *const false_type = skip_typeref(orig_false_type);
7518 type_t *result_type;
7519 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
7520 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7521 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
7522 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7523 warningf(&conditional->base.source_position,
7524 "ISO C forbids conditional expression with only one void side");
7526 result_type = type_void;
7527 } else if (is_type_arithmetic(true_type)
7528 && is_type_arithmetic(false_type)) {
7529 result_type = semantic_arithmetic(true_type, false_type);
7531 true_expression = create_implicit_cast(true_expression, result_type);
7532 false_expression = create_implicit_cast(false_expression, result_type);
7534 conditional->true_expression = true_expression;
7535 conditional->false_expression = false_expression;
7536 conditional->base.type = result_type;
7537 } else if (same_compound_type(true_type, false_type)) {
7538 /* just take 1 of the 2 types */
7539 result_type = true_type;
7540 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
7541 type_t *pointer_type;
7543 expression_t *other_expression;
7544 if (is_type_pointer(true_type) &&
7545 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
7546 pointer_type = true_type;
7547 other_type = false_type;
7548 other_expression = false_expression;
7550 pointer_type = false_type;
7551 other_type = true_type;
7552 other_expression = true_expression;
7555 if (is_null_pointer_constant(other_expression)) {
7556 result_type = pointer_type;
7557 } else if (is_type_pointer(other_type)) {
7558 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7559 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7562 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7563 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7565 } else if (types_compatible(get_unqualified_type(to1),
7566 get_unqualified_type(to2))) {
7569 warningf(&conditional->base.source_position,
7570 "pointer types '%T' and '%T' in conditional expression are incompatible",
7571 true_type, false_type);
7575 type_t *const type =
7576 get_qualified_type(to, to1->base.qualifiers | to2->base.qualifiers);
7577 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7578 } else if (is_type_integer(other_type)) {
7579 warningf(&conditional->base.source_position,
7580 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7581 result_type = pointer_type;
7583 if (is_type_valid(other_type)) {
7584 type_error_incompatible("while parsing conditional",
7585 &expression->base.source_position, true_type, false_type);
7587 result_type = type_error_type;
7590 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7591 type_error_incompatible("while parsing conditional",
7592 &conditional->base.source_position, true_type,
7595 result_type = type_error_type;
7598 conditional->true_expression
7599 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7600 conditional->false_expression
7601 = create_implicit_cast(false_expression, result_type);
7602 conditional->base.type = result_type;
7605 return create_invalid_expression();
7609 * Parse an extension expression.
7611 static expression_t *parse_extension(void)
7613 eat(T___extension__);
7615 bool old_gcc_extension = in_gcc_extension;
7616 in_gcc_extension = true;
7617 expression_t *expression = parse_sub_expression(PREC_UNARY);
7618 in_gcc_extension = old_gcc_extension;
7623 * Parse a __builtin_classify_type() expression.
7625 static expression_t *parse_builtin_classify_type(void)
7627 eat(T___builtin_classify_type);
7629 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7630 result->base.type = type_int;
7633 add_anchor_token(')');
7634 expression_t *expression = parse_expression();
7635 rem_anchor_token(')');
7637 result->classify_type.type_expression = expression;
7641 return create_invalid_expression();
7644 static bool check_pointer_arithmetic(const source_position_t *source_position,
7645 type_t *pointer_type,
7646 type_t *orig_pointer_type)
7648 type_t *points_to = pointer_type->pointer.points_to;
7649 points_to = skip_typeref(points_to);
7651 if (is_type_incomplete(points_to)) {
7652 if (!GNU_MODE || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7653 errorf(source_position,
7654 "arithmetic with pointer to incomplete type '%T' not allowed",
7657 } else if (warning.pointer_arith) {
7658 warningf(source_position,
7659 "pointer of type '%T' used in arithmetic",
7662 } else if (is_type_function(points_to)) {
7664 errorf(source_position,
7665 "arithmetic with pointer to function type '%T' not allowed",
7668 } else if (warning.pointer_arith) {
7669 warningf(source_position,
7670 "pointer to a function '%T' used in arithmetic",
7677 static bool is_lvalue(const expression_t *expression)
7679 switch (expression->kind) {
7680 case EXPR_REFERENCE:
7681 case EXPR_ARRAY_ACCESS:
7683 case EXPR_UNARY_DEREFERENCE:
7687 /* Claim it is an lvalue, if the type is invalid. There was a parse
7688 * error before, which maybe prevented properly recognizing it as
7690 return !is_type_valid(skip_typeref(expression->base.type));
7694 static void semantic_incdec(unary_expression_t *expression)
7696 type_t *const orig_type = expression->value->base.type;
7697 type_t *const type = skip_typeref(orig_type);
7698 if (is_type_pointer(type)) {
7699 if (!check_pointer_arithmetic(&expression->base.source_position,
7703 } else if (!is_type_real(type) && is_type_valid(type)) {
7704 /* TODO: improve error message */
7705 errorf(&expression->base.source_position,
7706 "operation needs an arithmetic or pointer type");
7709 if (!is_lvalue(expression->value)) {
7710 /* TODO: improve error message */
7711 errorf(&expression->base.source_position, "lvalue required as operand");
7713 expression->base.type = orig_type;
7716 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7718 type_t *const orig_type = expression->value->base.type;
7719 type_t *const type = skip_typeref(orig_type);
7720 if (!is_type_arithmetic(type)) {
7721 if (is_type_valid(type)) {
7722 /* TODO: improve error message */
7723 errorf(&expression->base.source_position,
7724 "operation needs an arithmetic type");
7729 expression->base.type = orig_type;
7732 static void semantic_unexpr_plus(unary_expression_t *expression)
7734 semantic_unexpr_arithmetic(expression);
7735 if (warning.traditional)
7736 warningf(&expression->base.source_position,
7737 "traditional C rejects the unary plus operator");
7740 static expression_t const *get_reference_address(expression_t const *expr)
7742 bool regular_take_address = true;
7744 if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
7745 expr = expr->unary.value;
7747 regular_take_address = false;
7750 if (expr->kind != EXPR_UNARY_DEREFERENCE)
7753 expr = expr->unary.value;
7756 if (expr->kind != EXPR_REFERENCE)
7759 if (!regular_take_address &&
7760 !is_type_function(skip_typeref(expr->reference.declaration->type))) {
7767 static void warn_function_address_as_bool(expression_t const* expr)
7769 if (!warning.address)
7772 expr = get_reference_address(expr);
7774 warningf(&expr->base.source_position,
7775 "the address of '%Y' will always evaluate as 'true'",
7776 expr->reference.declaration->symbol);
7780 static void semantic_not(unary_expression_t *expression)
7782 type_t *const orig_type = expression->value->base.type;
7783 type_t *const type = skip_typeref(orig_type);
7784 if (!is_type_scalar(type) && is_type_valid(type)) {
7785 errorf(&expression->base.source_position,
7786 "operand of ! must be of scalar type");
7789 warn_function_address_as_bool(expression->value);
7791 expression->base.type = type_int;
7794 static void semantic_unexpr_integer(unary_expression_t *expression)
7796 type_t *const orig_type = expression->value->base.type;
7797 type_t *const type = skip_typeref(orig_type);
7798 if (!is_type_integer(type)) {
7799 if (is_type_valid(type)) {
7800 errorf(&expression->base.source_position,
7801 "operand of ~ must be of integer type");
7806 expression->base.type = orig_type;
7809 static void semantic_dereference(unary_expression_t *expression)
7811 type_t *const orig_type = expression->value->base.type;
7812 type_t *const type = skip_typeref(orig_type);
7813 if (!is_type_pointer(type)) {
7814 if (is_type_valid(type)) {
7815 errorf(&expression->base.source_position,
7816 "Unary '*' needs pointer or array type, but type '%T' given", orig_type);
7821 type_t *result_type = type->pointer.points_to;
7822 result_type = automatic_type_conversion(result_type);
7823 expression->base.type = result_type;
7827 * Record that an address is taken (expression represents an lvalue).
7829 * @param expression the expression
7830 * @param may_be_register if true, the expression might be an register
7832 static void set_address_taken(expression_t *expression, bool may_be_register)
7834 if (expression->kind != EXPR_REFERENCE)
7837 declaration_t *const declaration = expression->reference.declaration;
7838 /* happens for parse errors */
7839 if (declaration == NULL)
7842 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7843 errorf(&expression->base.source_position,
7844 "address of register variable '%Y' requested",
7845 declaration->symbol);
7847 declaration->address_taken = 1;
7852 * Check the semantic of the address taken expression.
7854 static void semantic_take_addr(unary_expression_t *expression)
7856 expression_t *value = expression->value;
7857 value->base.type = revert_automatic_type_conversion(value);
7859 type_t *orig_type = value->base.type;
7860 if (!is_type_valid(skip_typeref(orig_type)))
7863 set_address_taken(value, false);
7865 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7868 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7869 static expression_t *parse_##unexpression_type(void) \
7871 expression_t *unary_expression \
7872 = allocate_expression_zero(unexpression_type); \
7873 unary_expression->base.source_position = *HERE; \
7875 unary_expression->unary.value = parse_sub_expression(PREC_UNARY); \
7877 sfunc(&unary_expression->unary); \
7879 return unary_expression; \
7882 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7883 semantic_unexpr_arithmetic)
7884 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7885 semantic_unexpr_plus)
7886 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7888 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7889 semantic_dereference)
7890 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7892 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7893 semantic_unexpr_integer)
7894 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7896 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7899 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7901 static expression_t *parse_##unexpression_type(expression_t *left) \
7903 expression_t *unary_expression \
7904 = allocate_expression_zero(unexpression_type); \
7905 unary_expression->base.source_position = *HERE; \
7907 unary_expression->unary.value = left; \
7909 sfunc(&unary_expression->unary); \
7911 return unary_expression; \
7914 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7915 EXPR_UNARY_POSTFIX_INCREMENT,
7917 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7918 EXPR_UNARY_POSTFIX_DECREMENT,
7921 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7923 /* TODO: handle complex + imaginary types */
7925 type_left = get_unqualified_type(type_left);
7926 type_right = get_unqualified_type(type_right);
7928 /* § 6.3.1.8 Usual arithmetic conversions */
7929 if (type_left == type_long_double || type_right == type_long_double) {
7930 return type_long_double;
7931 } else if (type_left == type_double || type_right == type_double) {
7933 } else if (type_left == type_float || type_right == type_float) {
7937 type_left = promote_integer(type_left);
7938 type_right = promote_integer(type_right);
7940 if (type_left == type_right)
7943 bool const signed_left = is_type_signed(type_left);
7944 bool const signed_right = is_type_signed(type_right);
7945 int const rank_left = get_rank(type_left);
7946 int const rank_right = get_rank(type_right);
7948 if (signed_left == signed_right)
7949 return rank_left >= rank_right ? type_left : type_right;
7958 u_rank = rank_right;
7959 u_type = type_right;
7961 s_rank = rank_right;
7962 s_type = type_right;
7967 if (u_rank >= s_rank)
7970 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7972 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7973 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7977 case ATOMIC_TYPE_INT: return type_unsigned_int;
7978 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7979 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7981 default: panic("invalid atomic type");
7986 * Check the semantic restrictions for a binary expression.
7988 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7990 expression_t *const left = expression->left;
7991 expression_t *const right = expression->right;
7992 type_t *const orig_type_left = left->base.type;
7993 type_t *const orig_type_right = right->base.type;
7994 type_t *const type_left = skip_typeref(orig_type_left);
7995 type_t *const type_right = skip_typeref(orig_type_right);
7997 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7998 /* TODO: improve error message */
7999 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8000 errorf(&expression->base.source_position,
8001 "operation needs arithmetic types");
8006 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8007 expression->left = create_implicit_cast(left, arithmetic_type);
8008 expression->right = create_implicit_cast(right, arithmetic_type);
8009 expression->base.type = arithmetic_type;
8012 static void warn_div_by_zero(binary_expression_t const *const expression)
8014 if (!warning.div_by_zero ||
8015 !is_type_integer(expression->base.type))
8018 expression_t const *const right = expression->right;
8019 /* The type of the right operand can be different for /= */
8020 if (is_type_integer(right->base.type) &&
8021 is_constant_expression(right) &&
8022 fold_constant(right) == 0) {
8023 warningf(&expression->base.source_position, "division by zero");
8028 * Check the semantic restrictions for a div/mod expression.
8030 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
8031 semantic_binexpr_arithmetic(expression);
8032 warn_div_by_zero(expression);
8035 static void semantic_shift_op(binary_expression_t *expression)
8037 expression_t *const left = expression->left;
8038 expression_t *const right = expression->right;
8039 type_t *const orig_type_left = left->base.type;
8040 type_t *const orig_type_right = right->base.type;
8041 type_t * type_left = skip_typeref(orig_type_left);
8042 type_t * type_right = skip_typeref(orig_type_right);
8044 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
8045 /* TODO: improve error message */
8046 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8047 errorf(&expression->base.source_position,
8048 "operands of shift operation must have integer types");
8053 type_left = promote_integer(type_left);
8054 type_right = promote_integer(type_right);
8056 expression->left = create_implicit_cast(left, type_left);
8057 expression->right = create_implicit_cast(right, type_right);
8058 expression->base.type = type_left;
8061 static void semantic_add(binary_expression_t *expression)
8063 expression_t *const left = expression->left;
8064 expression_t *const right = expression->right;
8065 type_t *const orig_type_left = left->base.type;
8066 type_t *const orig_type_right = right->base.type;
8067 type_t *const type_left = skip_typeref(orig_type_left);
8068 type_t *const type_right = skip_typeref(orig_type_right);
8071 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8072 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8073 expression->left = create_implicit_cast(left, arithmetic_type);
8074 expression->right = create_implicit_cast(right, arithmetic_type);
8075 expression->base.type = arithmetic_type;
8077 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8078 check_pointer_arithmetic(&expression->base.source_position,
8079 type_left, orig_type_left);
8080 expression->base.type = type_left;
8081 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
8082 check_pointer_arithmetic(&expression->base.source_position,
8083 type_right, orig_type_right);
8084 expression->base.type = type_right;
8085 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8086 errorf(&expression->base.source_position,
8087 "invalid operands to binary + ('%T', '%T')",
8088 orig_type_left, orig_type_right);
8092 static void semantic_sub(binary_expression_t *expression)
8094 expression_t *const left = expression->left;
8095 expression_t *const right = expression->right;
8096 type_t *const orig_type_left = left->base.type;
8097 type_t *const orig_type_right = right->base.type;
8098 type_t *const type_left = skip_typeref(orig_type_left);
8099 type_t *const type_right = skip_typeref(orig_type_right);
8100 source_position_t const *const pos = &expression->base.source_position;
8103 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8104 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8105 expression->left = create_implicit_cast(left, arithmetic_type);
8106 expression->right = create_implicit_cast(right, arithmetic_type);
8107 expression->base.type = arithmetic_type;
8109 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8110 check_pointer_arithmetic(&expression->base.source_position,
8111 type_left, orig_type_left);
8112 expression->base.type = type_left;
8113 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8114 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
8115 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
8116 if (!types_compatible(unqual_left, unqual_right)) {
8118 "subtracting pointers to incompatible types '%T' and '%T'",
8119 orig_type_left, orig_type_right);
8120 } else if (!is_type_object(unqual_left)) {
8121 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
8122 warningf(pos, "subtracting pointers to void");
8124 errorf(pos, "subtracting pointers to non-object types '%T'",
8128 expression->base.type = type_ptrdiff_t;
8129 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8130 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
8131 orig_type_left, orig_type_right);
8135 static void warn_string_literal_address(expression_t const* expr)
8137 while (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
8138 expr = expr->unary.value;
8139 if (expr->kind != EXPR_UNARY_DEREFERENCE)
8141 expr = expr->unary.value;
8144 if (expr->kind == EXPR_STRING_LITERAL ||
8145 expr->kind == EXPR_WIDE_STRING_LITERAL) {
8146 warningf(&expr->base.source_position,
8147 "comparison with string literal results in unspecified behaviour");
8152 * Check the semantics of comparison expressions.
8154 * @param expression The expression to check.
8156 static void semantic_comparison(binary_expression_t *expression)
8158 expression_t *left = expression->left;
8159 expression_t *right = expression->right;
8161 if (warning.address) {
8162 warn_string_literal_address(left);
8163 warn_string_literal_address(right);
8165 expression_t const* const func_left = get_reference_address(left);
8166 if (func_left != NULL && is_null_pointer_constant(right)) {
8167 warningf(&expression->base.source_position,
8168 "the address of '%Y' will never be NULL",
8169 func_left->reference.declaration->symbol);
8172 expression_t const* const func_right = get_reference_address(right);
8173 if (func_right != NULL && is_null_pointer_constant(right)) {
8174 warningf(&expression->base.source_position,
8175 "the address of '%Y' will never be NULL",
8176 func_right->reference.declaration->symbol);
8180 type_t *orig_type_left = left->base.type;
8181 type_t *orig_type_right = right->base.type;
8182 type_t *type_left = skip_typeref(orig_type_left);
8183 type_t *type_right = skip_typeref(orig_type_right);
8185 /* TODO non-arithmetic types */
8186 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8187 /* test for signed vs unsigned compares */
8188 if (warning.sign_compare &&
8189 (expression->base.kind != EXPR_BINARY_EQUAL &&
8190 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
8191 (is_type_signed(type_left) != is_type_signed(type_right))) {
8193 /* check if 1 of the operands is a constant, in this case we just
8194 * check wether we can safely represent the resulting constant in
8195 * the type of the other operand. */
8196 expression_t *const_expr = NULL;
8197 expression_t *other_expr = NULL;
8199 if (is_constant_expression(left)) {
8202 } else if (is_constant_expression(right)) {
8207 if (const_expr != NULL) {
8208 type_t *other_type = skip_typeref(other_expr->base.type);
8209 long val = fold_constant(const_expr);
8210 /* TODO: check if val can be represented by other_type */
8214 warningf(&expression->base.source_position,
8215 "comparison between signed and unsigned");
8217 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8218 expression->left = create_implicit_cast(left, arithmetic_type);
8219 expression->right = create_implicit_cast(right, arithmetic_type);
8220 expression->base.type = arithmetic_type;
8221 if (warning.float_equal &&
8222 (expression->base.kind == EXPR_BINARY_EQUAL ||
8223 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
8224 is_type_float(arithmetic_type)) {
8225 warningf(&expression->base.source_position,
8226 "comparing floating point with == or != is unsafe");
8228 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8229 /* TODO check compatibility */
8230 } else if (is_type_pointer(type_left)) {
8231 expression->right = create_implicit_cast(right, type_left);
8232 } else if (is_type_pointer(type_right)) {
8233 expression->left = create_implicit_cast(left, type_right);
8234 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8235 type_error_incompatible("invalid operands in comparison",
8236 &expression->base.source_position,
8237 type_left, type_right);
8239 expression->base.type = type_int;
8243 * Checks if a compound type has constant fields.
8245 static bool has_const_fields(const compound_type_t *type)
8247 const scope_t *scope = &type->declaration->scope;
8248 const declaration_t *declaration = scope->declarations;
8250 for (; declaration != NULL; declaration = declaration->next) {
8251 if (declaration->namespc != NAMESPACE_NORMAL)
8254 const type_t *decl_type = skip_typeref(declaration->type);
8255 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
8262 static bool is_valid_assignment_lhs(expression_t const* const left)
8264 type_t *const orig_type_left = revert_automatic_type_conversion(left);
8265 type_t *const type_left = skip_typeref(orig_type_left);
8267 if (!is_lvalue(left)) {
8268 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
8273 if (is_type_array(type_left)) {
8274 errorf(HERE, "cannot assign to arrays ('%E')", left);
8277 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
8278 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
8282 if (is_type_incomplete(type_left)) {
8283 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
8284 left, orig_type_left);
8287 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
8288 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
8289 left, orig_type_left);
8296 static void semantic_arithmetic_assign(binary_expression_t *expression)
8298 expression_t *left = expression->left;
8299 expression_t *right = expression->right;
8300 type_t *orig_type_left = left->base.type;
8301 type_t *orig_type_right = right->base.type;
8303 if (!is_valid_assignment_lhs(left))
8306 type_t *type_left = skip_typeref(orig_type_left);
8307 type_t *type_right = skip_typeref(orig_type_right);
8309 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
8310 /* TODO: improve error message */
8311 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8312 errorf(&expression->base.source_position,
8313 "operation needs arithmetic types");
8318 /* combined instructions are tricky. We can't create an implicit cast on
8319 * the left side, because we need the uncasted form for the store.
8320 * The ast2firm pass has to know that left_type must be right_type
8321 * for the arithmetic operation and create a cast by itself */
8322 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8323 expression->right = create_implicit_cast(right, arithmetic_type);
8324 expression->base.type = type_left;
8327 static void semantic_divmod_assign(binary_expression_t *expression)
8329 semantic_arithmetic_assign(expression);
8330 warn_div_by_zero(expression);
8333 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
8335 expression_t *const left = expression->left;
8336 expression_t *const right = expression->right;
8337 type_t *const orig_type_left = left->base.type;
8338 type_t *const orig_type_right = right->base.type;
8339 type_t *const type_left = skip_typeref(orig_type_left);
8340 type_t *const type_right = skip_typeref(orig_type_right);
8342 if (!is_valid_assignment_lhs(left))
8345 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8346 /* combined instructions are tricky. We can't create an implicit cast on
8347 * the left side, because we need the uncasted form for the store.
8348 * The ast2firm pass has to know that left_type must be right_type
8349 * for the arithmetic operation and create a cast by itself */
8350 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
8351 expression->right = create_implicit_cast(right, arithmetic_type);
8352 expression->base.type = type_left;
8353 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8354 check_pointer_arithmetic(&expression->base.source_position,
8355 type_left, orig_type_left);
8356 expression->base.type = type_left;
8357 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8358 errorf(&expression->base.source_position,
8359 "incompatible types '%T' and '%T' in assignment",
8360 orig_type_left, orig_type_right);
8365 * Check the semantic restrictions of a logical expression.
8367 static void semantic_logical_op(binary_expression_t *expression)
8369 expression_t *const left = expression->left;
8370 expression_t *const right = expression->right;
8371 type_t *const orig_type_left = left->base.type;
8372 type_t *const orig_type_right = right->base.type;
8373 type_t *const type_left = skip_typeref(orig_type_left);
8374 type_t *const type_right = skip_typeref(orig_type_right);
8376 warn_function_address_as_bool(left);
8377 warn_function_address_as_bool(right);
8379 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
8380 /* TODO: improve error message */
8381 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8382 errorf(&expression->base.source_position,
8383 "operation needs scalar types");
8388 expression->base.type = type_int;
8392 * Check the semantic restrictions of a binary assign expression.
8394 static void semantic_binexpr_assign(binary_expression_t *expression)
8396 expression_t *left = expression->left;
8397 type_t *orig_type_left = left->base.type;
8399 if (!is_valid_assignment_lhs(left))
8402 assign_error_t error = semantic_assign(orig_type_left, expression->right);
8403 report_assign_error(error, orig_type_left, expression->right,
8404 "assignment", &left->base.source_position);
8405 expression->right = create_implicit_cast(expression->right, orig_type_left);
8406 expression->base.type = orig_type_left;
8410 * Determine if the outermost operation (or parts thereof) of the given
8411 * expression has no effect in order to generate a warning about this fact.
8412 * Therefore in some cases this only examines some of the operands of the
8413 * expression (see comments in the function and examples below).
8415 * f() + 23; // warning, because + has no effect
8416 * x || f(); // no warning, because x controls execution of f()
8417 * x ? y : f(); // warning, because y has no effect
8418 * (void)x; // no warning to be able to suppress the warning
8419 * This function can NOT be used for an "expression has definitely no effect"-
8421 static bool expression_has_effect(const expression_t *const expr)
8423 switch (expr->kind) {
8424 case EXPR_UNKNOWN: break;
8425 case EXPR_INVALID: return true; /* do NOT warn */
8426 case EXPR_REFERENCE: return false;
8427 /* suppress the warning for microsoft __noop operations */
8428 case EXPR_CONST: return expr->conste.is_ms_noop;
8429 case EXPR_CHARACTER_CONSTANT: return false;
8430 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
8431 case EXPR_STRING_LITERAL: return false;
8432 case EXPR_WIDE_STRING_LITERAL: return false;
8433 case EXPR_LABEL_ADDRESS: return false;
8436 const call_expression_t *const call = &expr->call;
8437 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
8440 switch (call->function->builtin_symbol.symbol->ID) {
8441 case T___builtin_va_end: return true;
8442 default: return false;
8446 /* Generate the warning if either the left or right hand side of a
8447 * conditional expression has no effect */
8448 case EXPR_CONDITIONAL: {
8449 const conditional_expression_t *const cond = &expr->conditional;
8451 expression_has_effect(cond->true_expression) &&
8452 expression_has_effect(cond->false_expression);
8455 case EXPR_SELECT: return false;
8456 case EXPR_ARRAY_ACCESS: return false;
8457 case EXPR_SIZEOF: return false;
8458 case EXPR_CLASSIFY_TYPE: return false;
8459 case EXPR_ALIGNOF: return false;
8461 case EXPR_FUNCNAME: return false;
8462 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
8463 case EXPR_BUILTIN_CONSTANT_P: return false;
8464 case EXPR_BUILTIN_PREFETCH: return true;
8465 case EXPR_OFFSETOF: return false;
8466 case EXPR_VA_START: return true;
8467 case EXPR_VA_ARG: return true;
8468 case EXPR_STATEMENT: return true; // TODO
8469 case EXPR_COMPOUND_LITERAL: return false;
8471 case EXPR_UNARY_NEGATE: return false;
8472 case EXPR_UNARY_PLUS: return false;
8473 case EXPR_UNARY_BITWISE_NEGATE: return false;
8474 case EXPR_UNARY_NOT: return false;
8475 case EXPR_UNARY_DEREFERENCE: return false;
8476 case EXPR_UNARY_TAKE_ADDRESS: return false;
8477 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
8478 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
8479 case EXPR_UNARY_PREFIX_INCREMENT: return true;
8480 case EXPR_UNARY_PREFIX_DECREMENT: return true;
8482 /* Treat void casts as if they have an effect in order to being able to
8483 * suppress the warning */
8484 case EXPR_UNARY_CAST: {
8485 type_t *const type = skip_typeref(expr->base.type);
8486 return is_type_atomic(type, ATOMIC_TYPE_VOID);
8489 case EXPR_UNARY_CAST_IMPLICIT: return true;
8490 case EXPR_UNARY_ASSUME: return true;
8492 case EXPR_BINARY_ADD: return false;
8493 case EXPR_BINARY_SUB: return false;
8494 case EXPR_BINARY_MUL: return false;
8495 case EXPR_BINARY_DIV: return false;
8496 case EXPR_BINARY_MOD: return false;
8497 case EXPR_BINARY_EQUAL: return false;
8498 case EXPR_BINARY_NOTEQUAL: return false;
8499 case EXPR_BINARY_LESS: return false;
8500 case EXPR_BINARY_LESSEQUAL: return false;
8501 case EXPR_BINARY_GREATER: return false;
8502 case EXPR_BINARY_GREATEREQUAL: return false;
8503 case EXPR_BINARY_BITWISE_AND: return false;
8504 case EXPR_BINARY_BITWISE_OR: return false;
8505 case EXPR_BINARY_BITWISE_XOR: return false;
8506 case EXPR_BINARY_SHIFTLEFT: return false;
8507 case EXPR_BINARY_SHIFTRIGHT: return false;
8508 case EXPR_BINARY_ASSIGN: return true;
8509 case EXPR_BINARY_MUL_ASSIGN: return true;
8510 case EXPR_BINARY_DIV_ASSIGN: return true;
8511 case EXPR_BINARY_MOD_ASSIGN: return true;
8512 case EXPR_BINARY_ADD_ASSIGN: return true;
8513 case EXPR_BINARY_SUB_ASSIGN: return true;
8514 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
8515 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
8516 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
8517 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
8518 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
8520 /* Only examine the right hand side of && and ||, because the left hand
8521 * side already has the effect of controlling the execution of the right
8523 case EXPR_BINARY_LOGICAL_AND:
8524 case EXPR_BINARY_LOGICAL_OR:
8525 /* Only examine the right hand side of a comma expression, because the left
8526 * hand side has a separate warning */
8527 case EXPR_BINARY_COMMA:
8528 return expression_has_effect(expr->binary.right);
8530 case EXPR_BINARY_BUILTIN_EXPECT: return true;
8531 case EXPR_BINARY_ISGREATER: return false;
8532 case EXPR_BINARY_ISGREATEREQUAL: return false;
8533 case EXPR_BINARY_ISLESS: return false;
8534 case EXPR_BINARY_ISLESSEQUAL: return false;
8535 case EXPR_BINARY_ISLESSGREATER: return false;
8536 case EXPR_BINARY_ISUNORDERED: return false;
8539 internal_errorf(HERE, "unexpected expression");
8542 static void semantic_comma(binary_expression_t *expression)
8544 if (warning.unused_value) {
8545 const expression_t *const left = expression->left;
8546 if (!expression_has_effect(left)) {
8547 warningf(&left->base.source_position,
8548 "left-hand operand of comma expression has no effect");
8551 expression->base.type = expression->right->base.type;
8555 * @param prec_r precedence of the right operand
8557 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, prec_r, sfunc) \
8558 static expression_t *parse_##binexpression_type(expression_t *left) \
8560 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
8561 binexpr->base.source_position = *HERE; \
8562 binexpr->binary.left = left; \
8565 expression_t *right = parse_sub_expression(prec_r); \
8567 binexpr->binary.right = right; \
8568 sfunc(&binexpr->binary); \
8573 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, PREC_CAST, semantic_binexpr_arithmetic)
8574 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, PREC_CAST, semantic_divmod_arithmetic)
8575 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, PREC_CAST, semantic_divmod_arithmetic)
8576 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, PREC_MULTIPLICATIVE, semantic_add)
8577 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, PREC_MULTIPLICATIVE, semantic_sub)
8578 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT, PREC_ADDITIVE, semantic_shift_op)
8579 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT, PREC_ADDITIVE, semantic_shift_op)
8580 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, PREC_SHIFT, semantic_comparison)
8581 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, PREC_SHIFT, semantic_comparison)
8582 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL, PREC_SHIFT, semantic_comparison)
8583 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL, PREC_SHIFT, semantic_comparison)
8584 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL, PREC_RELATIONAL, semantic_comparison)
8585 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL, PREC_RELATIONAL, semantic_comparison)
8586 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND, PREC_EQUALITY, semantic_binexpr_arithmetic)
8587 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR, PREC_AND, semantic_binexpr_arithmetic)
8588 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR, PREC_XOR, semantic_binexpr_arithmetic)
8589 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND, PREC_OR, semantic_logical_op)
8590 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR, PREC_LOGICAL_AND, semantic_logical_op)
8591 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, PREC_ASSIGNMENT, semantic_binexpr_assign)
8592 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8593 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8594 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8595 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8596 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8597 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8598 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8599 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8600 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8601 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8602 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, PREC_ASSIGNMENT, semantic_comma)
8605 static expression_t *parse_sub_expression(precedence_t precedence)
8607 if (token.type < 0) {
8608 return expected_expression_error();
8611 expression_parser_function_t *parser
8612 = &expression_parsers[token.type];
8613 source_position_t source_position = token.source_position;
8616 if (parser->parser != NULL) {
8617 left = parser->parser();
8619 left = parse_primary_expression();
8621 assert(left != NULL);
8622 left->base.source_position = source_position;
8625 if (token.type < 0) {
8626 return expected_expression_error();
8629 parser = &expression_parsers[token.type];
8630 if (parser->infix_parser == NULL)
8632 if (parser->infix_precedence < precedence)
8635 left = parser->infix_parser(left);
8637 assert(left != NULL);
8638 assert(left->kind != EXPR_UNKNOWN);
8639 left->base.source_position = source_position;
8646 * Parse an expression.
8648 static expression_t *parse_expression(void)
8650 return parse_sub_expression(PREC_EXPRESSION);
8654 * Register a parser for a prefix-like operator with given precedence.
8656 * @param parser the parser function
8657 * @param token_type the token type of the prefix token
8658 * @param precedence the precedence of the operator
8660 static void register_expression_parser(parse_expression_function parser,
8661 int token_type, unsigned precedence)
8663 expression_parser_function_t *entry = &expression_parsers[token_type];
8665 if (entry->parser != NULL) {
8666 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8667 panic("trying to register multiple expression parsers for a token");
8669 entry->parser = parser;
8670 entry->precedence = precedence;
8674 * Register a parser for an infix operator with given precedence.
8676 * @param parser the parser function
8677 * @param token_type the token type of the infix operator
8678 * @param precedence the precedence of the operator
8680 static void register_infix_parser(parse_expression_infix_function parser,
8681 int token_type, unsigned precedence)
8683 expression_parser_function_t *entry = &expression_parsers[token_type];
8685 if (entry->infix_parser != NULL) {
8686 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8687 panic("trying to register multiple infix expression parsers for a "
8690 entry->infix_parser = parser;
8691 entry->infix_precedence = precedence;
8695 * Initialize the expression parsers.
8697 static void init_expression_parsers(void)
8699 memset(&expression_parsers, 0, sizeof(expression_parsers));
8701 register_infix_parser(parse_array_expression, '[', PREC_POSTFIX);
8702 register_infix_parser(parse_call_expression, '(', PREC_POSTFIX);
8703 register_infix_parser(parse_select_expression, '.', PREC_POSTFIX);
8704 register_infix_parser(parse_select_expression, T_MINUSGREATER, PREC_POSTFIX);
8705 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT, T_PLUSPLUS, PREC_POSTFIX);
8706 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT, T_MINUSMINUS, PREC_POSTFIX);
8707 register_infix_parser(parse_EXPR_BINARY_MUL, '*', PREC_MULTIPLICATIVE);
8708 register_infix_parser(parse_EXPR_BINARY_DIV, '/', PREC_MULTIPLICATIVE);
8709 register_infix_parser(parse_EXPR_BINARY_MOD, '%', PREC_MULTIPLICATIVE);
8710 register_infix_parser(parse_EXPR_BINARY_ADD, '+', PREC_ADDITIVE);
8711 register_infix_parser(parse_EXPR_BINARY_SUB, '-', PREC_ADDITIVE);
8712 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, PREC_SHIFT);
8713 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, PREC_SHIFT);
8714 register_infix_parser(parse_EXPR_BINARY_LESS, '<', PREC_RELATIONAL);
8715 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', PREC_RELATIONAL);
8716 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, PREC_RELATIONAL);
8717 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, PREC_RELATIONAL);
8718 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, PREC_EQUALITY);
8719 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL, T_EXCLAMATIONMARKEQUAL, PREC_EQUALITY);
8720 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', PREC_AND);
8721 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', PREC_XOR);
8722 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', PREC_OR);
8723 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, PREC_LOGICAL_AND);
8724 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, PREC_LOGICAL_OR);
8725 register_infix_parser(parse_conditional_expression, '?', PREC_CONDITIONAL);
8726 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', PREC_ASSIGNMENT);
8727 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, PREC_ASSIGNMENT);
8728 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, PREC_ASSIGNMENT);
8729 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, PREC_ASSIGNMENT);
8730 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, PREC_ASSIGNMENT);
8731 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, PREC_ASSIGNMENT);
8732 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN, T_LESSLESSEQUAL, PREC_ASSIGNMENT);
8733 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN, T_GREATERGREATEREQUAL, PREC_ASSIGNMENT);
8734 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN, T_ANDEQUAL, PREC_ASSIGNMENT);
8735 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN, T_PIPEEQUAL, PREC_ASSIGNMENT);
8736 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN, T_CARETEQUAL, PREC_ASSIGNMENT);
8737 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', PREC_EXPRESSION);
8739 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', PREC_UNARY);
8740 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', PREC_UNARY);
8741 register_expression_parser(parse_EXPR_UNARY_NOT, '!', PREC_UNARY);
8742 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', PREC_UNARY);
8743 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', PREC_UNARY);
8744 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', PREC_UNARY);
8745 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT, T_PLUSPLUS, PREC_UNARY);
8746 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT, T_MINUSMINUS, PREC_UNARY);
8747 register_expression_parser(parse_sizeof, T_sizeof, PREC_UNARY);
8748 register_expression_parser(parse_alignof, T___alignof__, PREC_UNARY);
8749 register_expression_parser(parse_extension, T___extension__, PREC_UNARY);
8750 register_expression_parser(parse_builtin_classify_type, T___builtin_classify_type, PREC_UNARY);
8754 * Parse a asm statement arguments specification.
8756 static asm_argument_t *parse_asm_arguments(bool is_out)
8758 asm_argument_t *result = NULL;
8759 asm_argument_t *last = NULL;
8761 while (token.type == T_STRING_LITERAL || token.type == '[') {
8762 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8763 memset(argument, 0, sizeof(argument[0]));
8765 if (token.type == '[') {
8767 if (token.type != T_IDENTIFIER) {
8768 parse_error_expected("while parsing asm argument",
8769 T_IDENTIFIER, NULL);
8772 argument->symbol = token.v.symbol;
8777 argument->constraints = parse_string_literals();
8779 add_anchor_token(')');
8780 expression_t *expression = parse_expression();
8781 rem_anchor_token(')');
8783 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8784 * change size or type representation (e.g. int -> long is ok, but
8785 * int -> float is not) */
8786 if (expression->kind == EXPR_UNARY_CAST) {
8787 type_t *const type = expression->base.type;
8788 type_kind_t const kind = type->kind;
8789 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8792 if (kind == TYPE_ATOMIC) {
8793 atomic_type_kind_t const akind = type->atomic.akind;
8794 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8795 size = get_atomic_type_size(akind);
8797 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8798 size = get_atomic_type_size(get_intptr_kind());
8802 expression_t *const value = expression->unary.value;
8803 type_t *const value_type = value->base.type;
8804 type_kind_t const value_kind = value_type->kind;
8806 unsigned value_flags;
8807 unsigned value_size;
8808 if (value_kind == TYPE_ATOMIC) {
8809 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8810 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8811 value_size = get_atomic_type_size(value_akind);
8812 } else if (value_kind == TYPE_POINTER) {
8813 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8814 value_size = get_atomic_type_size(get_intptr_kind());
8819 if (value_flags != flags || value_size != size)
8823 } while (expression->kind == EXPR_UNARY_CAST);
8827 if (!is_lvalue(expression)) {
8828 errorf(&expression->base.source_position,
8829 "asm output argument is not an lvalue");
8832 if (argument->constraints.begin[0] == '+')
8833 mark_decls_read(expression, NULL);
8835 mark_decls_read(expression, NULL);
8837 argument->expression = expression;
8840 set_address_taken(expression, true);
8843 last->next = argument;
8849 if (token.type != ',')
8860 * Parse a asm statement clobber specification.
8862 static asm_clobber_t *parse_asm_clobbers(void)
8864 asm_clobber_t *result = NULL;
8865 asm_clobber_t *last = NULL;
8867 while(token.type == T_STRING_LITERAL) {
8868 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8869 clobber->clobber = parse_string_literals();
8872 last->next = clobber;
8878 if (token.type != ',')
8887 * Parse an asm statement.
8889 static statement_t *parse_asm_statement(void)
8891 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8892 asm_statement_t *asm_statement = &statement->asms;
8896 if (token.type == T_volatile) {
8898 asm_statement->is_volatile = true;
8902 add_anchor_token(')');
8903 add_anchor_token(':');
8904 asm_statement->asm_text = parse_string_literals();
8906 if (token.type != ':') {
8907 rem_anchor_token(':');
8912 asm_statement->outputs = parse_asm_arguments(true);
8913 if (token.type != ':') {
8914 rem_anchor_token(':');
8919 asm_statement->inputs = parse_asm_arguments(false);
8920 if (token.type != ':') {
8921 rem_anchor_token(':');
8924 rem_anchor_token(':');
8927 asm_statement->clobbers = parse_asm_clobbers();
8930 rem_anchor_token(')');
8934 if (asm_statement->outputs == NULL) {
8935 /* GCC: An 'asm' instruction without any output operands will be treated
8936 * identically to a volatile 'asm' instruction. */
8937 asm_statement->is_volatile = true;
8942 return create_invalid_statement();
8946 * Parse a case statement.
8948 static statement_t *parse_case_statement(void)
8950 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8951 source_position_t *const pos = &statement->base.source_position;
8955 expression_t *const expression = parse_expression();
8956 statement->case_label.expression = expression;
8957 if (!is_constant_expression(expression)) {
8958 /* This check does not prevent the error message in all cases of an
8959 * prior error while parsing the expression. At least it catches the
8960 * common case of a mistyped enum entry. */
8961 if (is_type_valid(skip_typeref(expression->base.type))) {
8962 errorf(pos, "case label does not reduce to an integer constant");
8964 statement->case_label.is_bad = true;
8966 long const val = fold_constant(expression);
8967 statement->case_label.first_case = val;
8968 statement->case_label.last_case = val;
8972 if (token.type == T_DOTDOTDOT) {
8974 expression_t *const end_range = parse_expression();
8975 statement->case_label.end_range = end_range;
8976 if (!is_constant_expression(end_range)) {
8977 /* This check does not prevent the error message in all cases of an
8978 * prior error while parsing the expression. At least it catches the
8979 * common case of a mistyped enum entry. */
8980 if (is_type_valid(skip_typeref(end_range->base.type))) {
8981 errorf(pos, "case range does not reduce to an integer constant");
8983 statement->case_label.is_bad = true;
8985 long const val = fold_constant(end_range);
8986 statement->case_label.last_case = val;
8988 if (val < statement->case_label.first_case) {
8989 statement->case_label.is_empty_range = true;
8990 warningf(pos, "empty range specified");
8996 PUSH_PARENT(statement);
9000 if (current_switch != NULL) {
9001 if (! statement->case_label.is_bad) {
9002 /* Check for duplicate case values */
9003 case_label_statement_t *c = &statement->case_label;
9004 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
9005 if (l->is_bad || l->is_empty_range || l->expression == NULL)
9008 if (c->last_case < l->first_case || c->first_case > l->last_case)
9011 errorf(pos, "duplicate case value (previously used %P)",
9012 &l->base.source_position);
9016 /* link all cases into the switch statement */
9017 if (current_switch->last_case == NULL) {
9018 current_switch->first_case = &statement->case_label;
9020 current_switch->last_case->next = &statement->case_label;
9022 current_switch->last_case = &statement->case_label;
9024 errorf(pos, "case label not within a switch statement");
9027 statement_t *const inner_stmt = parse_statement();
9028 statement->case_label.statement = inner_stmt;
9029 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9030 errorf(&inner_stmt->base.source_position, "declaration after case label");
9037 return create_invalid_statement();
9041 * Parse a default statement.
9043 static statement_t *parse_default_statement(void)
9045 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9049 PUSH_PARENT(statement);
9052 if (current_switch != NULL) {
9053 const case_label_statement_t *def_label = current_switch->default_label;
9054 if (def_label != NULL) {
9055 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
9056 &def_label->base.source_position);
9058 current_switch->default_label = &statement->case_label;
9060 /* link all cases into the switch statement */
9061 if (current_switch->last_case == NULL) {
9062 current_switch->first_case = &statement->case_label;
9064 current_switch->last_case->next = &statement->case_label;
9066 current_switch->last_case = &statement->case_label;
9069 errorf(&statement->base.source_position,
9070 "'default' label not within a switch statement");
9073 statement_t *const inner_stmt = parse_statement();
9074 statement->case_label.statement = inner_stmt;
9075 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9076 errorf(&inner_stmt->base.source_position, "declaration after default label");
9083 return create_invalid_statement();
9087 * Parse a label statement.
9089 static statement_t *parse_label_statement(void)
9091 assert(token.type == T_IDENTIFIER);
9092 symbol_t *symbol = token.v.symbol;
9093 declaration_t *label = get_label(symbol);
9095 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
9096 statement->label.label = label;
9100 PUSH_PARENT(statement);
9102 /* if statement is already set then the label is defined twice,
9103 * otherwise it was just mentioned in a goto/local label declaration so far */
9104 if (label->init.statement != NULL) {
9105 errorf(HERE, "duplicate label '%Y' (declared %P)",
9106 symbol, &label->source_position);
9108 label->source_position = token.source_position;
9109 label->init.statement = statement;
9114 if (token.type == '}') {
9115 /* TODO only warn? */
9117 warningf(HERE, "label at end of compound statement");
9118 statement->label.statement = create_empty_statement();
9120 errorf(HERE, "label at end of compound statement");
9121 statement->label.statement = create_invalid_statement();
9123 } else if (token.type == ';') {
9124 /* Eat an empty statement here, to avoid the warning about an empty
9125 * statement after a label. label:; is commonly used to have a label
9126 * before a closing brace. */
9127 statement->label.statement = create_empty_statement();
9130 statement_t *const inner_stmt = parse_statement();
9131 statement->label.statement = inner_stmt;
9132 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9133 errorf(&inner_stmt->base.source_position, "declaration after label");
9137 /* remember the labels in a list for later checking */
9138 if (label_last == NULL) {
9139 label_first = &statement->label;
9141 label_last->next = &statement->label;
9143 label_last = &statement->label;
9150 * Parse an if statement.
9152 static statement_t *parse_if(void)
9154 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
9158 PUSH_PARENT(statement);
9160 add_anchor_token('{');
9163 add_anchor_token(')');
9164 expression_t *const expr = parse_expression();
9165 statement->ifs.condition = expr;
9166 mark_decls_read(expr, NULL);
9167 rem_anchor_token(')');
9171 rem_anchor_token('{');
9173 add_anchor_token(T_else);
9174 statement->ifs.true_statement = parse_statement();
9175 rem_anchor_token(T_else);
9177 if (token.type == T_else) {
9179 statement->ifs.false_statement = parse_statement();
9187 * Check that all enums are handled in a switch.
9189 * @param statement the switch statement to check
9191 static void check_enum_cases(const switch_statement_t *statement) {
9192 const type_t *type = skip_typeref(statement->expression->base.type);
9193 if (! is_type_enum(type))
9195 const enum_type_t *enumt = &type->enumt;
9197 /* if we have a default, no warnings */
9198 if (statement->default_label != NULL)
9201 /* FIXME: calculation of value should be done while parsing */
9202 const declaration_t *declaration;
9203 long last_value = -1;
9204 for (declaration = enumt->declaration->next;
9205 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
9206 declaration = declaration->next) {
9207 const expression_t *expression = declaration->init.enum_value;
9208 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
9210 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
9211 if (l->expression == NULL)
9213 if (l->first_case <= value && value <= l->last_case) {
9219 warningf(&statement->base.source_position,
9220 "enumeration value '%Y' not handled in switch", declaration->symbol);
9227 * Parse a switch statement.
9229 static statement_t *parse_switch(void)
9231 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
9235 PUSH_PARENT(statement);
9238 add_anchor_token(')');
9239 expression_t *const expr = parse_expression();
9240 mark_decls_read(expr, NULL);
9241 type_t * type = skip_typeref(expr->base.type);
9242 if (is_type_integer(type)) {
9243 type = promote_integer(type);
9244 if (warning.traditional) {
9245 if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
9246 warningf(&expr->base.source_position,
9247 "'%T' switch expression not converted to '%T' in ISO C",
9251 } else if (is_type_valid(type)) {
9252 errorf(&expr->base.source_position,
9253 "switch quantity is not an integer, but '%T'", type);
9254 type = type_error_type;
9256 statement->switchs.expression = create_implicit_cast(expr, type);
9258 rem_anchor_token(')');
9260 switch_statement_t *rem = current_switch;
9261 current_switch = &statement->switchs;
9262 statement->switchs.body = parse_statement();
9263 current_switch = rem;
9265 if (warning.switch_default &&
9266 statement->switchs.default_label == NULL) {
9267 warningf(&statement->base.source_position, "switch has no default case");
9269 if (warning.switch_enum)
9270 check_enum_cases(&statement->switchs);
9276 return create_invalid_statement();
9279 static statement_t *parse_loop_body(statement_t *const loop)
9281 statement_t *const rem = current_loop;
9282 current_loop = loop;
9284 statement_t *const body = parse_statement();
9291 * Parse a while statement.
9293 static statement_t *parse_while(void)
9295 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
9299 PUSH_PARENT(statement);
9302 add_anchor_token(')');
9303 expression_t *const cond = parse_expression();
9304 statement->whiles.condition = cond;
9305 mark_decls_read(cond, NULL);
9306 rem_anchor_token(')');
9309 statement->whiles.body = parse_loop_body(statement);
9315 return create_invalid_statement();
9319 * Parse a do statement.
9321 static statement_t *parse_do(void)
9323 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
9327 PUSH_PARENT(statement);
9329 add_anchor_token(T_while);
9330 statement->do_while.body = parse_loop_body(statement);
9331 rem_anchor_token(T_while);
9335 add_anchor_token(')');
9336 expression_t *const cond = parse_expression();
9337 statement->do_while.condition = cond;
9338 mark_decls_read(cond, NULL);
9339 rem_anchor_token(')');
9347 return create_invalid_statement();
9351 * Parse a for statement.
9353 static statement_t *parse_for(void)
9355 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
9359 PUSH_PARENT(statement);
9361 size_t const top = environment_top();
9362 scope_push(&statement->fors.scope);
9365 add_anchor_token(')');
9367 if (token.type != ';') {
9368 if (is_declaration_specifier(&token, false)) {
9369 parse_declaration(record_declaration);
9371 add_anchor_token(';');
9372 expression_t *const init = parse_expression();
9373 statement->fors.initialisation = init;
9374 mark_decls_read(init, DECL_ANY);
9375 if (warning.unused_value && !expression_has_effect(init)) {
9376 warningf(&init->base.source_position,
9377 "initialisation of 'for'-statement has no effect");
9379 rem_anchor_token(';');
9386 if (token.type != ';') {
9387 add_anchor_token(';');
9388 expression_t *const cond = parse_expression();
9389 statement->fors.condition = cond;
9390 mark_decls_read(cond, NULL);
9391 rem_anchor_token(';');
9394 if (token.type != ')') {
9395 expression_t *const step = parse_expression();
9396 statement->fors.step = step;
9397 mark_decls_read(step, DECL_ANY);
9398 if (warning.unused_value && !expression_has_effect(step)) {
9399 warningf(&step->base.source_position,
9400 "step of 'for'-statement has no effect");
9403 rem_anchor_token(')');
9405 statement->fors.body = parse_loop_body(statement);
9407 assert(scope == &statement->fors.scope);
9409 environment_pop_to(top);
9416 rem_anchor_token(')');
9417 assert(scope == &statement->fors.scope);
9419 environment_pop_to(top);
9421 return create_invalid_statement();
9425 * Parse a goto statement.
9427 static statement_t *parse_goto(void)
9429 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
9432 if (GNU_MODE && token.type == '*') {
9434 expression_t *expression = parse_expression();
9435 mark_decls_read(expression, NULL);
9437 /* Argh: although documentation say the expression must be of type void *,
9438 * gcc excepts anything that can be casted into void * without error */
9439 type_t *type = expression->base.type;
9441 if (type != type_error_type) {
9442 if (!is_type_pointer(type) && !is_type_integer(type)) {
9443 errorf(&expression->base.source_position,
9444 "cannot convert to a pointer type");
9445 } else if (type != type_void_ptr) {
9446 warningf(&expression->base.source_position,
9447 "type of computed goto expression should be 'void*' not '%T'", type);
9449 expression = create_implicit_cast(expression, type_void_ptr);
9452 statement->gotos.expression = expression;
9454 if (token.type != T_IDENTIFIER) {
9456 parse_error_expected("while parsing goto", T_IDENTIFIER, '*', NULL);
9458 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
9462 symbol_t *symbol = token.v.symbol;
9465 statement->gotos.label = get_label(symbol);
9468 /* remember the goto's in a list for later checking */
9469 if (goto_last == NULL) {
9470 goto_first = &statement->gotos;
9472 goto_last->next = &statement->gotos;
9474 goto_last = &statement->gotos;
9480 return create_invalid_statement();
9484 * Parse a continue statement.
9486 static statement_t *parse_continue(void)
9488 if (current_loop == NULL) {
9489 errorf(HERE, "continue statement not within loop");
9492 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
9502 * Parse a break statement.
9504 static statement_t *parse_break(void)
9506 if (current_switch == NULL && current_loop == NULL) {
9507 errorf(HERE, "break statement not within loop or switch");
9510 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
9520 * Parse a __leave statement.
9522 static statement_t *parse_leave_statement(void)
9524 if (current_try == NULL) {
9525 errorf(HERE, "__leave statement not within __try");
9528 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
9538 * Check if a given declaration represents a local variable.
9540 static bool is_local_var_declaration(const declaration_t *declaration)
9542 switch ((storage_class_tag_t) declaration->storage_class) {
9543 case STORAGE_CLASS_AUTO:
9544 case STORAGE_CLASS_REGISTER: {
9545 const type_t *type = skip_typeref(declaration->type);
9546 if (is_type_function(type)) {
9558 * Check if a given declaration represents a variable.
9560 static bool is_var_declaration(const declaration_t *declaration)
9562 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
9565 const type_t *type = skip_typeref(declaration->type);
9566 return !is_type_function(type);
9570 * Check if a given expression represents a local variable.
9572 static bool is_local_variable(const expression_t *expression)
9574 if (expression->base.kind != EXPR_REFERENCE) {
9577 const declaration_t *declaration = expression->reference.declaration;
9578 return is_local_var_declaration(declaration);
9582 * Check if a given expression represents a local variable and
9583 * return its declaration then, else return NULL.
9585 declaration_t *expr_is_variable(const expression_t *expression)
9587 if (expression->base.kind != EXPR_REFERENCE) {
9590 declaration_t *declaration = expression->reference.declaration;
9591 if (is_var_declaration(declaration))
9597 * Parse a return statement.
9599 static statement_t *parse_return(void)
9603 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
9605 expression_t *return_value = NULL;
9606 if (token.type != ';') {
9607 return_value = parse_expression();
9608 mark_decls_read(return_value, NULL);
9611 const type_t *const func_type = current_function->type;
9612 assert(is_type_function(func_type));
9613 type_t *const return_type = skip_typeref(func_type->function.return_type);
9615 if (return_value != NULL) {
9616 type_t *return_value_type = skip_typeref(return_value->base.type);
9618 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
9619 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
9620 warningf(&statement->base.source_position,
9621 "'return' with a value, in function returning void");
9622 return_value = NULL;
9624 assign_error_t error = semantic_assign(return_type, return_value);
9625 report_assign_error(error, return_type, return_value, "'return'",
9626 &statement->base.source_position);
9627 return_value = create_implicit_cast(return_value, return_type);
9629 /* check for returning address of a local var */
9630 if (return_value != NULL &&
9631 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9632 const expression_t *expression = return_value->unary.value;
9633 if (is_local_variable(expression)) {
9634 warningf(&statement->base.source_position,
9635 "function returns address of local variable");
9639 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9640 warningf(&statement->base.source_position,
9641 "'return' without value, in function returning non-void");
9644 statement->returns.value = return_value;
9653 * Parse a declaration statement.
9655 static statement_t *parse_declaration_statement(void)
9657 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9659 declaration_t *before = last_declaration;
9661 parse_external_declaration();
9663 parse_declaration(record_declaration);
9665 if (before == NULL) {
9666 statement->declaration.declarations_begin = scope->declarations;
9668 statement->declaration.declarations_begin = before->next;
9670 statement->declaration.declarations_end = last_declaration;
9676 * Parse an expression statement, ie. expr ';'.
9678 static statement_t *parse_expression_statement(void)
9680 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9682 expression_t *const expr = parse_expression();
9683 statement->expression.expression = expr;
9684 mark_decls_read(expr, DECL_ANY);
9693 * Parse a microsoft __try { } __finally { } or
9694 * __try{ } __except() { }
9696 static statement_t *parse_ms_try_statment(void)
9698 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9701 PUSH_PARENT(statement);
9703 ms_try_statement_t *rem = current_try;
9704 current_try = &statement->ms_try;
9705 statement->ms_try.try_statement = parse_compound_statement(false);
9710 if (token.type == T___except) {
9713 add_anchor_token(')');
9714 expression_t *const expr = parse_expression();
9715 mark_decls_read(expr, NULL);
9716 type_t * type = skip_typeref(expr->base.type);
9717 if (is_type_integer(type)) {
9718 type = promote_integer(type);
9719 } else if (is_type_valid(type)) {
9720 errorf(&expr->base.source_position,
9721 "__expect expression is not an integer, but '%T'", type);
9722 type = type_error_type;
9724 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9725 rem_anchor_token(')');
9727 statement->ms_try.final_statement = parse_compound_statement(false);
9728 } else if (token.type == T__finally) {
9730 statement->ms_try.final_statement = parse_compound_statement(false);
9732 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9733 return create_invalid_statement();
9737 return create_invalid_statement();
9740 static statement_t *parse_empty_statement(void)
9742 if (warning.empty_statement) {
9743 warningf(HERE, "statement is empty");
9745 statement_t *const statement = create_empty_statement();
9750 static statement_t *parse_local_label_declaration(void) {
9751 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9755 declaration_t *begin = NULL, *end = NULL;
9758 if (token.type != T_IDENTIFIER) {
9759 parse_error_expected("while parsing local label declaration",
9760 T_IDENTIFIER, NULL);
9763 symbol_t *symbol = token.v.symbol;
9764 declaration_t *declaration = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
9765 if (declaration != NULL) {
9766 errorf(HERE, "multiple definitions of '__label__ %Y' (previous definition at %P)",
9767 symbol, &declaration->source_position);
9769 declaration = allocate_declaration_zero();
9770 declaration->namespc = NAMESPACE_LOCAL_LABEL;
9771 declaration->source_position = token.source_position;
9772 declaration->symbol = symbol;
9773 declaration->parent_scope = scope;
9774 declaration->init.statement = NULL;
9777 end->next = declaration;
9780 begin = declaration;
9782 local_label_push(declaration);
9786 if (token.type != ',')
9792 statement->declaration.declarations_begin = begin;
9793 statement->declaration.declarations_end = end;
9798 * Parse a statement.
9799 * There's also parse_statement() which additionally checks for
9800 * "statement has no effect" warnings
9802 static statement_t *intern_parse_statement(void)
9804 statement_t *statement = NULL;
9806 /* declaration or statement */
9807 add_anchor_token(';');
9808 switch (token.type) {
9809 case T_IDENTIFIER: {
9810 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9811 if (la1_type == ':') {
9812 statement = parse_label_statement();
9813 } else if (is_typedef_symbol(token.v.symbol)) {
9814 statement = parse_declaration_statement();
9815 } else switch (la1_type) {
9817 if (get_declaration(token.v.symbol, NAMESPACE_NORMAL) != NULL)
9818 goto expression_statment;
9823 statement = parse_declaration_statement();
9827 expression_statment:
9828 statement = parse_expression_statement();
9834 case T___extension__:
9835 /* This can be a prefix to a declaration or an expression statement.
9836 * We simply eat it now and parse the rest with tail recursion. */
9839 } while (token.type == T___extension__);
9840 bool old_gcc_extension = in_gcc_extension;
9841 in_gcc_extension = true;
9842 statement = parse_statement();
9843 in_gcc_extension = old_gcc_extension;
9847 statement = parse_declaration_statement();
9851 statement = parse_local_label_declaration();
9854 case ';': statement = parse_empty_statement(); break;
9855 case '{': statement = parse_compound_statement(false); break;
9856 case T___leave: statement = parse_leave_statement(); break;
9857 case T___try: statement = parse_ms_try_statment(); break;
9858 case T_asm: statement = parse_asm_statement(); break;
9859 case T_break: statement = parse_break(); break;
9860 case T_case: statement = parse_case_statement(); break;
9861 case T_continue: statement = parse_continue(); break;
9862 case T_default: statement = parse_default_statement(); break;
9863 case T_do: statement = parse_do(); break;
9864 case T_for: statement = parse_for(); break;
9865 case T_goto: statement = parse_goto(); break;
9866 case T_if: statement = parse_if(); break;
9867 case T_return: statement = parse_return(); break;
9868 case T_switch: statement = parse_switch(); break;
9869 case T_while: statement = parse_while(); break;
9879 case T_CHARACTER_CONSTANT:
9880 case T_FLOATINGPOINT:
9884 case T_STRING_LITERAL:
9885 case T_WIDE_CHARACTER_CONSTANT:
9886 case T_WIDE_STRING_LITERAL:
9887 case T___FUNCDNAME__:
9889 case T___FUNCTION__:
9890 case T___PRETTY_FUNCTION__:
9891 case T___builtin_alloca:
9892 case T___builtin_classify_type:
9893 case T___builtin_constant_p:
9894 case T___builtin_expect:
9895 case T___builtin_huge_val:
9896 case T___builtin_isgreater:
9897 case T___builtin_isgreaterequal:
9898 case T___builtin_isless:
9899 case T___builtin_islessequal:
9900 case T___builtin_islessgreater:
9901 case T___builtin_isunordered:
9902 case T___builtin_inf:
9903 case T___builtin_inff:
9904 case T___builtin_infl:
9905 case T___builtin_nan:
9906 case T___builtin_nanf:
9907 case T___builtin_nanl:
9908 case T___builtin_offsetof:
9909 case T___builtin_prefetch:
9910 case T___builtin_va_arg:
9911 case T___builtin_va_end:
9912 case T___builtin_va_start:
9916 statement = parse_expression_statement();
9920 errorf(HERE, "unexpected token %K while parsing statement", &token);
9921 statement = create_invalid_statement();
9926 rem_anchor_token(';');
9928 assert(statement != NULL
9929 && statement->base.source_position.input_name != NULL);
9935 * parse a statement and emits "statement has no effect" warning if needed
9936 * (This is really a wrapper around intern_parse_statement with check for 1
9937 * single warning. It is needed, because for statement expressions we have
9938 * to avoid the warning on the last statement)
9940 static statement_t *parse_statement(void)
9942 statement_t *statement = intern_parse_statement();
9944 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9945 expression_t *expression = statement->expression.expression;
9946 if (!expression_has_effect(expression)) {
9947 warningf(&expression->base.source_position,
9948 "statement has no effect");
9956 * Parse a compound statement.
9958 static statement_t *parse_compound_statement(bool inside_expression_statement)
9960 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
9962 PUSH_PARENT(statement);
9965 add_anchor_token('}');
9967 size_t const top = environment_top();
9968 size_t const top_local = local_label_top();
9969 scope_push(&statement->compound.scope);
9971 statement_t **anchor = &statement->compound.statements;
9972 bool only_decls_so_far = true;
9973 while (token.type != '}') {
9974 if (token.type == T_EOF) {
9975 errorf(&statement->base.source_position,
9976 "EOF while parsing compound statement");
9979 statement_t *sub_statement = intern_parse_statement();
9980 if (is_invalid_statement(sub_statement)) {
9981 /* an error occurred. if we are at an anchor, return */
9987 if (warning.declaration_after_statement) {
9988 if (sub_statement->kind != STATEMENT_DECLARATION) {
9989 only_decls_so_far = false;
9990 } else if (!only_decls_so_far) {
9991 warningf(&sub_statement->base.source_position,
9992 "ISO C90 forbids mixed declarations and code");
9996 *anchor = sub_statement;
9998 while (sub_statement->base.next != NULL)
9999 sub_statement = sub_statement->base.next;
10001 anchor = &sub_statement->base.next;
10005 /* look over all statements again to produce no effect warnings */
10006 if (warning.unused_value) {
10007 statement_t *sub_statement = statement->compound.statements;
10008 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
10009 if (sub_statement->kind != STATEMENT_EXPRESSION)
10011 /* don't emit a warning for the last expression in an expression
10012 * statement as it has always an effect */
10013 if (inside_expression_statement && sub_statement->base.next == NULL)
10016 expression_t *expression = sub_statement->expression.expression;
10017 if (!expression_has_effect(expression)) {
10018 warningf(&expression->base.source_position,
10019 "statement has no effect");
10025 rem_anchor_token('}');
10026 assert(scope == &statement->compound.scope);
10028 environment_pop_to(top);
10029 local_label_pop_to(top_local);
10036 * Initialize builtin types.
10038 static void initialize_builtin_types(void)
10040 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
10041 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
10042 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
10043 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
10044 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
10045 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
10046 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
10047 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
10049 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
10050 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
10051 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
10052 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
10054 /* const version of wchar_t */
10055 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
10056 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
10057 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
10059 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
10063 * Check for unused global static functions and variables
10065 static void check_unused_globals(void)
10067 if (!warning.unused_function && !warning.unused_variable)
10070 for (const declaration_t *decl = file_scope->declarations; decl != NULL; decl = decl->next) {
10072 decl->modifiers & DM_UNUSED ||
10073 decl->modifiers & DM_USED ||
10074 decl->storage_class != STORAGE_CLASS_STATIC)
10077 type_t *const type = decl->type;
10079 if (is_type_function(skip_typeref(type))) {
10080 if (!warning.unused_function || decl->is_inline)
10083 s = (decl->init.statement != NULL ? "defined" : "declared");
10085 if (!warning.unused_variable)
10091 warningf(&decl->source_position, "'%#T' %s but not used",
10092 type, decl->symbol, s);
10096 static void parse_global_asm(void)
10098 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
10103 statement->asms.asm_text = parse_string_literals();
10104 statement->base.next = unit->global_asm;
10105 unit->global_asm = statement;
10114 * Parse a translation unit.
10116 static void parse_translation_unit(void)
10118 add_anchor_token(T_EOF);
10121 unsigned char token_anchor_copy[T_LAST_TOKEN];
10122 memcpy(token_anchor_copy, token_anchor_set, sizeof(token_anchor_copy));
10126 bool anchor_leak = false;
10127 for (int i = 0; i != T_LAST_TOKEN; ++i) {
10128 unsigned char count = token_anchor_set[i] - token_anchor_copy[i];
10130 errorf(HERE, "Leaked anchor token %k %d times", i, count);
10131 anchor_leak = true;
10134 if (in_gcc_extension) {
10135 errorf(HERE, "Leaked __extension__");
10136 anchor_leak = true;
10143 switch (token.type) {
10146 case T___extension__:
10147 parse_external_declaration();
10151 parse_global_asm();
10155 rem_anchor_token(T_EOF);
10159 if (!strict_mode) {
10160 warningf(HERE, "stray ';' outside of function");
10167 errorf(HERE, "stray %K outside of function", &token);
10168 if (token.type == '(' || token.type == '{' || token.type == '[')
10169 eat_until_matching_token(token.type);
10179 * @return the translation unit or NULL if errors occurred.
10181 void start_parsing(void)
10183 environment_stack = NEW_ARR_F(stack_entry_t, 0);
10184 label_stack = NEW_ARR_F(stack_entry_t, 0);
10185 local_label_stack = NEW_ARR_F(stack_entry_t, 0);
10186 diagnostic_count = 0;
10190 type_set_output(stderr);
10191 ast_set_output(stderr);
10193 assert(unit == NULL);
10194 unit = allocate_ast_zero(sizeof(unit[0]));
10196 assert(file_scope == NULL);
10197 file_scope = &unit->scope;
10199 assert(scope == NULL);
10200 scope_push(&unit->scope);
10202 initialize_builtin_types();
10205 translation_unit_t *finish_parsing(void)
10207 /* do NOT use scope_pop() here, this will crash, will it by hand */
10208 assert(scope == &unit->scope);
10210 last_declaration = NULL;
10212 assert(file_scope == &unit->scope);
10213 check_unused_globals();
10216 DEL_ARR_F(environment_stack);
10217 DEL_ARR_F(label_stack);
10218 DEL_ARR_F(local_label_stack);
10220 translation_unit_t *result = unit;
10227 lookahead_bufpos = 0;
10228 for (int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
10231 parse_translation_unit();
10235 * Initialize the parser.
10237 void init_parser(void)
10239 sym_anonymous = symbol_table_insert("<anonymous>");
10241 if (c_mode & _MS) {
10242 /* add predefined symbols for extended-decl-modifier */
10243 sym_align = symbol_table_insert("align");
10244 sym_allocate = symbol_table_insert("allocate");
10245 sym_dllimport = symbol_table_insert("dllimport");
10246 sym_dllexport = symbol_table_insert("dllexport");
10247 sym_naked = symbol_table_insert("naked");
10248 sym_noinline = symbol_table_insert("noinline");
10249 sym_noreturn = symbol_table_insert("noreturn");
10250 sym_nothrow = symbol_table_insert("nothrow");
10251 sym_novtable = symbol_table_insert("novtable");
10252 sym_property = symbol_table_insert("property");
10253 sym_get = symbol_table_insert("get");
10254 sym_put = symbol_table_insert("put");
10255 sym_selectany = symbol_table_insert("selectany");
10256 sym_thread = symbol_table_insert("thread");
10257 sym_uuid = symbol_table_insert("uuid");
10258 sym_deprecated = symbol_table_insert("deprecated");
10259 sym_restrict = symbol_table_insert("restrict");
10260 sym_noalias = symbol_table_insert("noalias");
10262 memset(token_anchor_set, 0, sizeof(token_anchor_set));
10264 init_expression_parsers();
10265 obstack_init(&temp_obst);
10267 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
10268 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
10272 * Terminate the parser.
10274 void exit_parser(void)
10276 obstack_free(&temp_obst, NULL);