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 /** special symbol used for anonymous entities. */
149 static const symbol_t *sym_anonymous = NULL;
151 /* symbols for Microsoft extended-decl-modifier */
152 static const symbol_t *sym_align = NULL;
153 static const symbol_t *sym_allocate = NULL;
154 static const symbol_t *sym_dllimport = NULL;
155 static const symbol_t *sym_dllexport = NULL;
156 static const symbol_t *sym_naked = NULL;
157 static const symbol_t *sym_noinline = NULL;
158 static const symbol_t *sym_noreturn = NULL;
159 static const symbol_t *sym_nothrow = NULL;
160 static const symbol_t *sym_novtable = NULL;
161 static const symbol_t *sym_property = NULL;
162 static const symbol_t *sym_get = NULL;
163 static const symbol_t *sym_put = NULL;
164 static const symbol_t *sym_selectany = NULL;
165 static const symbol_t *sym_thread = NULL;
166 static const symbol_t *sym_uuid = NULL;
167 static const symbol_t *sym_deprecated = NULL;
168 static const symbol_t *sym_restrict = NULL;
169 static const symbol_t *sym_noalias = NULL;
171 /** The token anchor set */
172 static unsigned char token_anchor_set[T_LAST_TOKEN];
174 /** The current source position. */
175 #define HERE (&token.source_position)
177 /** true if we are in GCC mode. */
178 #define GNU_MODE ((c_mode & _GNUC) || in_gcc_extension)
180 static type_t *type_valist;
182 static statement_t *parse_compound_statement(bool inside_expression_statement);
183 static statement_t *parse_statement(void);
185 static expression_t *parse_sub_expression(precedence_t);
186 static expression_t *parse_expression(void);
187 static type_t *parse_typename(void);
189 static void parse_compound_type_entries(declaration_t *compound_declaration);
190 static declaration_t *parse_declarator(
191 const declaration_specifiers_t *specifiers, bool may_be_abstract);
192 static declaration_t *record_declaration(declaration_t *declaration, bool is_definition);
194 static void semantic_comparison(binary_expression_t *expression);
196 #define STORAGE_CLASSES \
204 #define TYPE_QUALIFIERS \
209 case T__forceinline: \
210 case T___attribute__:
212 #ifdef PROVIDE_COMPLEX
213 #define COMPLEX_SPECIFIERS \
215 #define IMAGINARY_SPECIFIERS \
218 #define COMPLEX_SPECIFIERS
219 #define IMAGINARY_SPECIFIERS
222 #define TYPE_SPECIFIERS \
237 case T___builtin_va_list: \
242 #define DECLARATION_START \
247 #define TYPENAME_START \
252 * Allocate an AST node with given size and
253 * initialize all fields with zero.
255 static void *allocate_ast_zero(size_t size)
257 void *res = allocate_ast(size);
258 memset(res, 0, size);
262 static declaration_t *allocate_declaration_zero(void)
264 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
265 declaration->type = type_error_type;
266 declaration->alignment = 0;
271 * Returns the size of a statement node.
273 * @param kind the statement kind
275 static size_t get_statement_struct_size(statement_kind_t kind)
277 static const size_t sizes[] = {
278 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
279 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
280 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
281 [STATEMENT_RETURN] = sizeof(return_statement_t),
282 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
283 [STATEMENT_IF] = sizeof(if_statement_t),
284 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
285 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
286 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
287 [STATEMENT_BREAK] = sizeof(statement_base_t),
288 [STATEMENT_GOTO] = sizeof(goto_statement_t),
289 [STATEMENT_LABEL] = sizeof(label_statement_t),
290 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
291 [STATEMENT_WHILE] = sizeof(while_statement_t),
292 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
293 [STATEMENT_FOR] = sizeof(for_statement_t),
294 [STATEMENT_ASM] = sizeof(asm_statement_t),
295 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
296 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
298 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
299 assert(sizes[kind] != 0);
304 * Returns the size of an expression node.
306 * @param kind the expression kind
308 static size_t get_expression_struct_size(expression_kind_t kind)
310 static const size_t sizes[] = {
311 [EXPR_INVALID] = sizeof(expression_base_t),
312 [EXPR_REFERENCE] = sizeof(reference_expression_t),
313 [EXPR_CONST] = sizeof(const_expression_t),
314 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
315 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
316 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
317 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
318 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
319 [EXPR_CALL] = sizeof(call_expression_t),
320 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
321 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
322 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
323 [EXPR_SELECT] = sizeof(select_expression_t),
324 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
325 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
326 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
327 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
328 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
329 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
330 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
331 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
332 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
333 [EXPR_VA_START] = sizeof(va_start_expression_t),
334 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
335 [EXPR_STATEMENT] = sizeof(statement_expression_t),
336 [EXPR_LABEL_ADDRESS] = sizeof(label_address_expression_t),
338 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
339 return sizes[EXPR_UNARY_FIRST];
341 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
342 return sizes[EXPR_BINARY_FIRST];
344 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
345 assert(sizes[kind] != 0);
350 * Allocate a statement node of given kind and initialize all
353 static statement_t *allocate_statement_zero(statement_kind_t kind)
355 size_t size = get_statement_struct_size(kind);
356 statement_t *res = allocate_ast_zero(size);
358 res->base.kind = kind;
359 res->base.parent = current_parent;
360 res->base.source_position = token.source_position;
365 * Allocate an expression node of given kind and initialize all
368 static expression_t *allocate_expression_zero(expression_kind_t kind)
370 size_t size = get_expression_struct_size(kind);
371 expression_t *res = allocate_ast_zero(size);
373 res->base.kind = kind;
374 res->base.type = type_error_type;
379 * Creates a new invalid expression.
381 static expression_t *create_invalid_expression(void)
383 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
384 expression->base.source_position = token.source_position;
389 * Creates a new invalid statement.
391 static statement_t *create_invalid_statement(void)
393 return allocate_statement_zero(STATEMENT_INVALID);
397 * Allocate a new empty statement.
399 static statement_t *create_empty_statement(void)
401 return allocate_statement_zero(STATEMENT_EMPTY);
405 * Returns the size of a type node.
407 * @param kind the type kind
409 static size_t get_type_struct_size(type_kind_t kind)
411 static const size_t sizes[] = {
412 [TYPE_ATOMIC] = sizeof(atomic_type_t),
413 [TYPE_COMPLEX] = sizeof(complex_type_t),
414 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
415 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
416 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
417 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
418 [TYPE_ENUM] = sizeof(enum_type_t),
419 [TYPE_FUNCTION] = sizeof(function_type_t),
420 [TYPE_POINTER] = sizeof(pointer_type_t),
421 [TYPE_ARRAY] = sizeof(array_type_t),
422 [TYPE_BUILTIN] = sizeof(builtin_type_t),
423 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
424 [TYPE_TYPEOF] = sizeof(typeof_type_t),
426 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
427 assert(kind <= TYPE_TYPEOF);
428 assert(sizes[kind] != 0);
433 * Allocate a type node of given kind and initialize all
436 * @param kind type kind to allocate
438 static type_t *allocate_type_zero(type_kind_t kind)
440 size_t size = get_type_struct_size(kind);
441 type_t *res = obstack_alloc(type_obst, size);
442 memset(res, 0, size);
443 res->base.kind = kind;
449 * Returns the size of an initializer node.
451 * @param kind the initializer kind
453 static size_t get_initializer_size(initializer_kind_t kind)
455 static const size_t sizes[] = {
456 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
457 [INITIALIZER_STRING] = sizeof(initializer_string_t),
458 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
459 [INITIALIZER_LIST] = sizeof(initializer_list_t),
460 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
462 assert(kind < sizeof(sizes) / sizeof(*sizes));
463 assert(sizes[kind] != 0);
468 * Allocate an initializer node of given kind and initialize all
471 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
473 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
480 * Free a type from the type obstack.
482 static void free_type(void *type)
484 obstack_free(type_obst, type);
488 * Returns the index of the top element of the environment stack.
490 static size_t environment_top(void)
492 return ARR_LEN(environment_stack);
496 * Returns the index of the top element of the global label stack.
498 static size_t label_top(void)
500 return ARR_LEN(label_stack);
504 * Returns the index of the top element of the local label stack.
506 static size_t local_label_top(void)
508 return ARR_LEN(local_label_stack);
512 * Return the next token.
514 static inline void next_token(void)
516 token = lookahead_buffer[lookahead_bufpos];
517 lookahead_buffer[lookahead_bufpos] = lexer_token;
520 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
523 print_token(stderr, &token);
524 fprintf(stderr, "\n");
529 * Return the next token with a given lookahead.
531 static inline const token_t *look_ahead(int num)
533 assert(num > 0 && num <= MAX_LOOKAHEAD);
534 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
535 return &lookahead_buffer[pos];
539 * Adds a token to the token anchor set (a multi-set).
541 static void add_anchor_token(int token_type)
543 assert(0 <= token_type && token_type < T_LAST_TOKEN);
544 ++token_anchor_set[token_type];
547 static int save_and_reset_anchor_state(int token_type)
549 assert(0 <= token_type && token_type < T_LAST_TOKEN);
550 int count = token_anchor_set[token_type];
551 token_anchor_set[token_type] = 0;
555 static void restore_anchor_state(int token_type, int count)
557 assert(0 <= token_type && token_type < T_LAST_TOKEN);
558 token_anchor_set[token_type] = count;
562 * Remove a token from the token anchor set (a multi-set).
564 static void rem_anchor_token(int token_type)
566 assert(0 <= token_type && token_type < T_LAST_TOKEN);
567 assert(token_anchor_set[token_type] != 0);
568 --token_anchor_set[token_type];
571 static bool at_anchor(void)
575 return token_anchor_set[token.type];
579 * Eat tokens until a matching token is found.
581 static void eat_until_matching_token(int type)
585 case '(': end_token = ')'; break;
586 case '{': end_token = '}'; break;
587 case '[': end_token = ']'; break;
588 default: end_token = type; break;
591 unsigned parenthesis_count = 0;
592 unsigned brace_count = 0;
593 unsigned bracket_count = 0;
594 while (token.type != end_token ||
595 parenthesis_count != 0 ||
597 bracket_count != 0) {
598 switch (token.type) {
600 case '(': ++parenthesis_count; break;
601 case '{': ++brace_count; break;
602 case '[': ++bracket_count; break;
605 if (parenthesis_count > 0)
615 if (bracket_count > 0)
618 if (token.type == end_token &&
619 parenthesis_count == 0 &&
633 * Eat input tokens until an anchor is found.
635 static void eat_until_anchor(void)
637 while (token_anchor_set[token.type] == 0) {
638 if (token.type == '(' || token.type == '{' || token.type == '[')
639 eat_until_matching_token(token.type);
644 static void eat_block(void)
646 eat_until_matching_token('{');
647 if (token.type == '}')
651 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
654 * Report a parse error because an expected token was not found.
657 #if defined __GNUC__ && __GNUC__ >= 4
658 __attribute__((sentinel))
660 void parse_error_expected(const char *message, ...)
662 if (message != NULL) {
663 errorf(HERE, "%s", message);
666 va_start(ap, message);
667 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
672 * Report a type error.
674 static void type_error(const char *msg, const source_position_t *source_position,
677 errorf(source_position, "%s, but found type '%T'", msg, type);
681 * Report an incompatible type.
683 static void type_error_incompatible(const char *msg,
684 const source_position_t *source_position, type_t *type1, type_t *type2)
686 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
691 * Expect the the current token is the expected token.
692 * If not, generate an error, eat the current statement,
693 * and goto the end_error label.
695 #define expect(expected) \
697 if (UNLIKELY(token.type != (expected))) { \
698 parse_error_expected(NULL, (expected), NULL); \
699 add_anchor_token(expected); \
700 eat_until_anchor(); \
701 if (token.type == expected) \
703 rem_anchor_token(expected); \
709 static void scope_push(scope_t *new_scope)
712 scope->last_declaration = last_declaration;
713 new_scope->depth = scope->depth + 1;
715 new_scope->parent = scope;
718 last_declaration = new_scope->last_declaration;
721 static void scope_pop(void)
723 scope->last_declaration = last_declaration;
724 scope = scope->parent;
725 last_declaration = scope->last_declaration;
729 * Search a symbol in a given namespace and returns its declaration or
730 * NULL if this symbol was not found.
732 static declaration_t *get_declaration(const symbol_t *const symbol,
733 const namespace_t namespc)
735 declaration_t *declaration = symbol->declaration;
736 for( ; declaration != NULL; declaration = declaration->symbol_next) {
737 if (declaration->namespc == namespc)
745 * pushs an environment_entry on the environment stack and links the
746 * corresponding symbol to the new entry
748 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
750 symbol_t *symbol = declaration->symbol;
751 namespace_t namespc = (namespace_t) declaration->namespc;
753 /* replace/add declaration into declaration list of the symbol */
754 declaration_t **anchor;
756 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
761 /* replace an entry? */
762 if (iter->namespc == namespc) {
763 declaration->symbol_next = iter->symbol_next;
767 *anchor = declaration;
769 /* remember old declaration */
771 entry.symbol = symbol;
772 entry.old_declaration = iter;
773 entry.namespc = (unsigned short) namespc;
774 ARR_APP1(stack_entry_t, *stack_ptr, entry);
778 * Push a declaration on the environment stack.
780 * @param declaration the declaration
782 static void environment_push(declaration_t *declaration)
784 assert(declaration->source_position.input_name != NULL);
785 assert(declaration->parent_scope != NULL);
786 stack_push(&environment_stack, declaration);
790 * Push a declaration on the global label stack.
792 * @param declaration the declaration
794 static void label_push(declaration_t *declaration)
796 declaration->parent_scope = ¤t_function->scope;
797 stack_push(&label_stack, declaration);
801 * Push a declaration of the local label stack.
803 * @param declaration the declaration
805 static void local_label_push(declaration_t *declaration)
807 assert(declaration->parent_scope != NULL);
808 stack_push(&local_label_stack, declaration);
812 * pops symbols from the environment stack until @p new_top is the top element
814 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
816 stack_entry_t *stack = *stack_ptr;
817 size_t top = ARR_LEN(stack);
820 assert(new_top <= top);
824 for(i = top; i > new_top; --i) {
825 stack_entry_t *entry = &stack[i - 1];
827 declaration_t *old_declaration = entry->old_declaration;
828 symbol_t *symbol = entry->symbol;
829 namespace_t namespc = (namespace_t)entry->namespc;
831 /* replace/remove declaration */
832 declaration_t **anchor;
834 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
836 assert(iter != NULL);
837 /* replace an entry? */
838 if (iter->namespc == namespc)
842 /* Not all declarations adhere scopes (e.g. jump labels), so this
843 * correction is necessary */
844 if (old_declaration != NULL) {
845 old_declaration->symbol_next = iter->symbol_next;
846 *anchor = old_declaration;
848 *anchor = iter->symbol_next;
852 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
856 * Pop all entries from the environment stack until the new_top
859 * @param new_top the new stack top
861 static void environment_pop_to(size_t new_top)
863 stack_pop_to(&environment_stack, new_top);
867 * Pop all entries from the global label stack until the new_top
870 * @param new_top the new stack top
872 static void label_pop_to(size_t new_top)
874 stack_pop_to(&label_stack, new_top);
878 * Pop all entries from the local label stack until the new_top
881 * @param new_top the new stack top
883 static void local_label_pop_to(size_t new_top)
885 stack_pop_to(&local_label_stack, new_top);
889 static int get_akind_rank(atomic_type_kind_t akind)
894 static int get_rank(const type_t *type)
896 assert(!is_typeref(type));
897 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
898 * and esp. footnote 108). However we can't fold constants (yet), so we
899 * can't decide whether unsigned int is possible, while int always works.
900 * (unsigned int would be preferable when possible... for stuff like
901 * struct { enum { ... } bla : 4; } ) */
902 if (type->kind == TYPE_ENUM)
903 return get_akind_rank(ATOMIC_TYPE_INT);
905 assert(type->kind == TYPE_ATOMIC);
906 return get_akind_rank(type->atomic.akind);
909 static type_t *promote_integer(type_t *type)
911 if (type->kind == TYPE_BITFIELD)
912 type = type->bitfield.base_type;
914 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
921 * Create a cast expression.
923 * @param expression the expression to cast
924 * @param dest_type the destination type
926 static expression_t *create_cast_expression(expression_t *expression,
929 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
931 cast->unary.value = expression;
932 cast->base.type = dest_type;
938 * Check if a given expression represents the 0 pointer constant.
940 static bool is_null_pointer_constant(const expression_t *expression)
942 /* skip void* cast */
943 if (expression->kind == EXPR_UNARY_CAST
944 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
945 expression = expression->unary.value;
948 /* TODO: not correct yet, should be any constant integer expression
949 * which evaluates to 0 */
950 if (expression->kind != EXPR_CONST)
953 type_t *const type = skip_typeref(expression->base.type);
954 if (!is_type_integer(type))
957 return expression->conste.v.int_value == 0;
961 * Create an implicit cast expression.
963 * @param expression the expression to cast
964 * @param dest_type the destination type
966 static expression_t *create_implicit_cast(expression_t *expression,
969 type_t *const source_type = expression->base.type;
971 if (source_type == dest_type)
974 return create_cast_expression(expression, dest_type);
977 typedef enum assign_error_t {
979 ASSIGN_ERROR_INCOMPATIBLE,
980 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
981 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
982 ASSIGN_WARNING_POINTER_FROM_INT,
983 ASSIGN_WARNING_INT_FROM_POINTER
986 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
987 const expression_t *const right,
989 const source_position_t *source_position)
991 type_t *const orig_type_right = right->base.type;
992 type_t *const type_left = skip_typeref(orig_type_left);
993 type_t *const type_right = skip_typeref(orig_type_right);
998 case ASSIGN_ERROR_INCOMPATIBLE:
999 errorf(source_position,
1000 "destination type '%T' in %s is incompatible with type '%T'",
1001 orig_type_left, context, orig_type_right);
1004 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
1005 if (warning.other) {
1006 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
1007 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
1009 /* the left type has all qualifiers from the right type */
1010 unsigned missing_qualifiers
1011 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1012 warningf(source_position,
1013 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
1014 orig_type_left, context, orig_type_right, missing_qualifiers);
1019 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
1020 if (warning.other) {
1021 warningf(source_position,
1022 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
1023 orig_type_left, context, right, orig_type_right);
1027 case ASSIGN_WARNING_POINTER_FROM_INT:
1028 if (warning.other) {
1029 warningf(source_position,
1030 "%s makes pointer '%T' from integer '%T' without a cast",
1031 context, orig_type_left, orig_type_right);
1035 case ASSIGN_WARNING_INT_FROM_POINTER:
1036 if (warning.other) {
1037 warningf(source_position,
1038 "%s makes integer '%T' from pointer '%T' without a cast",
1039 context, orig_type_left, orig_type_right);
1044 panic("invalid error value");
1048 /** Implements the rules from § 6.5.16.1 */
1049 static assign_error_t semantic_assign(type_t *orig_type_left,
1050 const expression_t *const right)
1052 type_t *const orig_type_right = right->base.type;
1053 type_t *const type_left = skip_typeref(orig_type_left);
1054 type_t *const type_right = skip_typeref(orig_type_right);
1056 if (is_type_pointer(type_left)) {
1057 if (is_null_pointer_constant(right)) {
1058 return ASSIGN_SUCCESS;
1059 } else if (is_type_pointer(type_right)) {
1060 type_t *points_to_left
1061 = skip_typeref(type_left->pointer.points_to);
1062 type_t *points_to_right
1063 = skip_typeref(type_right->pointer.points_to);
1064 assign_error_t res = ASSIGN_SUCCESS;
1066 /* the left type has all qualifiers from the right type */
1067 unsigned missing_qualifiers
1068 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1069 if (missing_qualifiers != 0) {
1070 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1073 points_to_left = get_unqualified_type(points_to_left);
1074 points_to_right = get_unqualified_type(points_to_right);
1076 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1077 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1081 if (!types_compatible(points_to_left, points_to_right)) {
1082 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1086 } else if (is_type_integer(type_right)) {
1087 return ASSIGN_WARNING_POINTER_FROM_INT;
1089 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1090 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1091 && is_type_pointer(type_right))) {
1092 return ASSIGN_SUCCESS;
1093 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1094 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1095 type_t *const unqual_type_left = get_unqualified_type(type_left);
1096 type_t *const unqual_type_right = get_unqualified_type(type_right);
1097 if (types_compatible(unqual_type_left, unqual_type_right)) {
1098 return ASSIGN_SUCCESS;
1100 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1101 return ASSIGN_WARNING_INT_FROM_POINTER;
1104 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1105 return ASSIGN_SUCCESS;
1107 return ASSIGN_ERROR_INCOMPATIBLE;
1110 static expression_t *parse_constant_expression(void)
1112 expression_t *result = parse_sub_expression(PREC_CONDITIONAL);
1114 if (!is_constant_expression(result)) {
1115 errorf(&result->base.source_position,
1116 "expression '%E' is not constant\n", result);
1122 static expression_t *parse_assignment_expression(void)
1124 return parse_sub_expression(PREC_ASSIGNMENT);
1127 static type_t *make_global_typedef(const char *name, type_t *type)
1129 symbol_t *const symbol = symbol_table_insert(name);
1131 declaration_t *const declaration = allocate_declaration_zero();
1132 declaration->namespc = NAMESPACE_NORMAL;
1133 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1134 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1135 declaration->type = type;
1136 declaration->symbol = symbol;
1137 declaration->source_position = builtin_source_position;
1138 declaration->implicit = true;
1140 record_declaration(declaration, false);
1142 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
1143 typedef_type->typedeft.declaration = declaration;
1145 return typedef_type;
1148 static string_t parse_string_literals(void)
1150 assert(token.type == T_STRING_LITERAL);
1151 string_t result = token.v.string;
1155 while (token.type == T_STRING_LITERAL) {
1156 result = concat_strings(&result, &token.v.string);
1163 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1164 [GNU_AK_CONST] = "const",
1165 [GNU_AK_VOLATILE] = "volatile",
1166 [GNU_AK_CDECL] = "cdecl",
1167 [GNU_AK_STDCALL] = "stdcall",
1168 [GNU_AK_FASTCALL] = "fastcall",
1169 [GNU_AK_DEPRECATED] = "deprecated",
1170 [GNU_AK_NOINLINE] = "noinline",
1171 [GNU_AK_NORETURN] = "noreturn",
1172 [GNU_AK_NAKED] = "naked",
1173 [GNU_AK_PURE] = "pure",
1174 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1175 [GNU_AK_MALLOC] = "malloc",
1176 [GNU_AK_WEAK] = "weak",
1177 [GNU_AK_CONSTRUCTOR] = "constructor",
1178 [GNU_AK_DESTRUCTOR] = "destructor",
1179 [GNU_AK_NOTHROW] = "nothrow",
1180 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1181 [GNU_AK_COMMON] = "common",
1182 [GNU_AK_NOCOMMON] = "nocommon",
1183 [GNU_AK_PACKED] = "packed",
1184 [GNU_AK_SHARED] = "shared",
1185 [GNU_AK_NOTSHARED] = "notshared",
1186 [GNU_AK_USED] = "used",
1187 [GNU_AK_UNUSED] = "unused",
1188 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1189 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1190 [GNU_AK_LONGCALL] = "longcall",
1191 [GNU_AK_SHORTCALL] = "shortcall",
1192 [GNU_AK_LONG_CALL] = "long_call",
1193 [GNU_AK_SHORT_CALL] = "short_call",
1194 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1195 [GNU_AK_INTERRUPT] = "interrupt",
1196 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1197 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1198 [GNU_AK_NESTING] = "nesting",
1199 [GNU_AK_NEAR] = "near",
1200 [GNU_AK_FAR] = "far",
1201 [GNU_AK_SIGNAL] = "signal",
1202 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1203 [GNU_AK_TINY_DATA] = "tiny_data",
1204 [GNU_AK_SAVEALL] = "saveall",
1205 [GNU_AK_FLATTEN] = "flatten",
1206 [GNU_AK_SSEREGPARM] = "sseregparm",
1207 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1208 [GNU_AK_RETURN_TWICE] = "return_twice",
1209 [GNU_AK_MAY_ALIAS] = "may_alias",
1210 [GNU_AK_MS_STRUCT] = "ms_struct",
1211 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1212 [GNU_AK_DLLIMPORT] = "dllimport",
1213 [GNU_AK_DLLEXPORT] = "dllexport",
1214 [GNU_AK_ALIGNED] = "aligned",
1215 [GNU_AK_ALIAS] = "alias",
1216 [GNU_AK_SECTION] = "section",
1217 [GNU_AK_FORMAT] = "format",
1218 [GNU_AK_FORMAT_ARG] = "format_arg",
1219 [GNU_AK_WEAKREF] = "weakref",
1220 [GNU_AK_NONNULL] = "nonnull",
1221 [GNU_AK_TLS_MODEL] = "tls_model",
1222 [GNU_AK_VISIBILITY] = "visibility",
1223 [GNU_AK_REGPARM] = "regparm",
1224 [GNU_AK_MODE] = "mode",
1225 [GNU_AK_MODEL] = "model",
1226 [GNU_AK_TRAP_EXIT] = "trap_exit",
1227 [GNU_AK_SP_SWITCH] = "sp_switch",
1228 [GNU_AK_SENTINEL] = "sentinel"
1232 * compare two string, ignoring double underscores on the second.
1234 static int strcmp_underscore(const char *s1, const char *s2)
1236 if (s2[0] == '_' && s2[1] == '_') {
1237 size_t len2 = strlen(s2);
1238 size_t len1 = strlen(s1);
1239 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1240 return strncmp(s1, s2+2, len2-4);
1244 return strcmp(s1, s2);
1248 * Allocate a new gnu temporal attribute.
1250 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1252 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1253 attribute->kind = kind;
1254 attribute->next = NULL;
1255 attribute->invalid = false;
1256 attribute->have_arguments = false;
1262 * parse one constant expression argument.
1264 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1266 expression_t *expression;
1267 add_anchor_token(')');
1268 expression = parse_constant_expression();
1269 rem_anchor_token(')');
1271 attribute->u.argument = fold_constant(expression);
1274 attribute->invalid = true;
1278 * parse a list of constant expressions arguments.
1280 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1282 argument_list_t **list = &attribute->u.arguments;
1283 argument_list_t *entry;
1284 expression_t *expression;
1285 add_anchor_token(')');
1286 add_anchor_token(',');
1288 expression = parse_constant_expression();
1289 entry = obstack_alloc(&temp_obst, sizeof(entry));
1290 entry->argument = fold_constant(expression);
1293 list = &entry->next;
1294 if (token.type != ',')
1298 rem_anchor_token(',');
1299 rem_anchor_token(')');
1303 attribute->invalid = true;
1307 * parse one string literal argument.
1309 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1312 add_anchor_token('(');
1313 if (token.type != T_STRING_LITERAL) {
1314 parse_error_expected("while parsing attribute directive",
1315 T_STRING_LITERAL, NULL);
1318 *string = parse_string_literals();
1319 rem_anchor_token('(');
1323 attribute->invalid = true;
1327 * parse one tls model.
1329 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1331 static const char *const tls_models[] = {
1337 string_t string = { NULL, 0 };
1338 parse_gnu_attribute_string_arg(attribute, &string);
1339 if (string.begin != NULL) {
1340 for(size_t i = 0; i < 4; ++i) {
1341 if (strcmp(tls_models[i], string.begin) == 0) {
1342 attribute->u.value = i;
1346 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1348 attribute->invalid = true;
1352 * parse one tls model.
1354 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1356 static const char *const visibilities[] = {
1362 string_t string = { NULL, 0 };
1363 parse_gnu_attribute_string_arg(attribute, &string);
1364 if (string.begin != NULL) {
1365 for(size_t i = 0; i < 4; ++i) {
1366 if (strcmp(visibilities[i], string.begin) == 0) {
1367 attribute->u.value = i;
1371 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1373 attribute->invalid = true;
1377 * parse one (code) model.
1379 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1381 static const char *const visibilities[] = {
1386 string_t string = { NULL, 0 };
1387 parse_gnu_attribute_string_arg(attribute, &string);
1388 if (string.begin != NULL) {
1389 for(int i = 0; i < 3; ++i) {
1390 if (strcmp(visibilities[i], string.begin) == 0) {
1391 attribute->u.value = i;
1395 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1397 attribute->invalid = true;
1400 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1402 /* TODO: find out what is allowed here... */
1404 /* at least: byte, word, pointer, list of machine modes
1405 * __XXX___ is interpreted as XXX */
1406 add_anchor_token(')');
1408 if (token.type != T_IDENTIFIER) {
1409 expect(T_IDENTIFIER);
1412 /* This isn't really correct, the backend should provide a list of machine
1413 * specific modes (according to gcc philosophy that is...) */
1414 const char *symbol_str = token.v.symbol->string;
1415 if (strcmp_underscore("QI", symbol_str) == 0 ||
1416 strcmp_underscore("byte", symbol_str) == 0) {
1417 attribute->u.akind = ATOMIC_TYPE_CHAR;
1418 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1419 attribute->u.akind = ATOMIC_TYPE_SHORT;
1420 } else if (strcmp_underscore("SI", symbol_str) == 0
1421 || strcmp_underscore("word", symbol_str) == 0
1422 || strcmp_underscore("pointer", symbol_str) == 0) {
1423 attribute->u.akind = ATOMIC_TYPE_INT;
1424 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1425 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1428 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1429 attribute->invalid = true;
1433 rem_anchor_token(')');
1437 attribute->invalid = true;
1441 * parse one interrupt argument.
1443 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1445 static const char *const interrupts[] = {
1452 string_t string = { NULL, 0 };
1453 parse_gnu_attribute_string_arg(attribute, &string);
1454 if (string.begin != NULL) {
1455 for(size_t i = 0; i < 5; ++i) {
1456 if (strcmp(interrupts[i], string.begin) == 0) {
1457 attribute->u.value = i;
1461 errorf(HERE, "'%s' is not an interrupt", string.begin);
1463 attribute->invalid = true;
1467 * parse ( identifier, const expression, const expression )
1469 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1471 static const char *const format_names[] = {
1479 if (token.type != T_IDENTIFIER) {
1480 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1483 const char *name = token.v.symbol->string;
1484 for(i = 0; i < 4; ++i) {
1485 if (strcmp_underscore(format_names[i], name) == 0)
1489 if (warning.attribute)
1490 warningf(HERE, "'%s' is an unrecognized format function type", name);
1495 add_anchor_token(')');
1496 add_anchor_token(',');
1497 parse_constant_expression();
1498 rem_anchor_token(',');
1499 rem_anchor_token(')');
1502 add_anchor_token(')');
1503 parse_constant_expression();
1504 rem_anchor_token(')');
1508 attribute->u.value = true;
1511 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1513 if (!attribute->have_arguments)
1516 /* should have no arguments */
1517 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1518 eat_until_matching_token('(');
1519 /* we have already consumed '(', so we stop before ')', eat it */
1521 attribute->invalid = true;
1525 * Parse one GNU attribute.
1527 * Note that attribute names can be specified WITH or WITHOUT
1528 * double underscores, ie const or __const__.
1530 * The following attributes are parsed without arguments
1555 * no_instrument_function
1556 * warn_unused_result
1573 * externally_visible
1581 * The following attributes are parsed with arguments
1582 * aligned( const expression )
1583 * alias( string literal )
1584 * section( string literal )
1585 * format( identifier, const expression, const expression )
1586 * format_arg( const expression )
1587 * tls_model( string literal )
1588 * visibility( string literal )
1589 * regparm( const expression )
1590 * model( string leteral )
1591 * trap_exit( const expression )
1592 * sp_switch( string literal )
1594 * The following attributes might have arguments
1595 * weak_ref( string literal )
1596 * non_null( const expression // ',' )
1597 * interrupt( string literal )
1598 * sentinel( constant expression )
1600 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1602 gnu_attribute_t *head = *attributes;
1603 gnu_attribute_t *last = *attributes;
1604 decl_modifiers_t modifiers = 0;
1605 gnu_attribute_t *attribute;
1607 eat(T___attribute__);
1611 if (token.type != ')') {
1612 /* find the end of the list */
1614 while (last->next != NULL)
1618 /* non-empty attribute list */
1621 if (token.type == T_const) {
1623 } else if (token.type == T_volatile) {
1625 } else if (token.type == T_cdecl) {
1626 /* __attribute__((cdecl)), WITH ms mode */
1628 } else if (token.type == T_IDENTIFIER) {
1629 const symbol_t *sym = token.v.symbol;
1632 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1639 for(i = 0; i < GNU_AK_LAST; ++i) {
1640 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1643 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1646 if (kind == GNU_AK_LAST) {
1647 if (warning.attribute)
1648 warningf(HERE, "'%s' attribute directive ignored", name);
1650 /* skip possible arguments */
1651 if (token.type == '(') {
1652 eat_until_matching_token(')');
1655 /* check for arguments */
1656 attribute = allocate_gnu_attribute(kind);
1657 if (token.type == '(') {
1659 if (token.type == ')') {
1660 /* empty args are allowed */
1663 attribute->have_arguments = true;
1667 case GNU_AK_VOLATILE:
1672 case GNU_AK_NOCOMMON:
1674 case GNU_AK_NOTSHARED:
1675 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1676 case GNU_AK_WARN_UNUSED_RESULT:
1677 case GNU_AK_LONGCALL:
1678 case GNU_AK_SHORTCALL:
1679 case GNU_AK_LONG_CALL:
1680 case GNU_AK_SHORT_CALL:
1681 case GNU_AK_FUNCTION_VECTOR:
1682 case GNU_AK_INTERRUPT_HANDLER:
1683 case GNU_AK_NMI_HANDLER:
1684 case GNU_AK_NESTING:
1688 case GNU_AK_EIGTHBIT_DATA:
1689 case GNU_AK_TINY_DATA:
1690 case GNU_AK_SAVEALL:
1691 case GNU_AK_FLATTEN:
1692 case GNU_AK_SSEREGPARM:
1693 case GNU_AK_EXTERNALLY_VISIBLE:
1694 case GNU_AK_RETURN_TWICE:
1695 case GNU_AK_MAY_ALIAS:
1696 case GNU_AK_MS_STRUCT:
1697 case GNU_AK_GCC_STRUCT:
1700 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1701 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1702 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1703 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1704 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1705 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1706 case GNU_AK_CONST: modifiers |= DM_CONST; goto no_arg;
1707 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1708 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1709 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1710 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1711 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1712 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1713 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1714 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1715 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1716 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1717 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1719 case GNU_AK_ALIGNED:
1720 /* __align__ may be used without an argument */
1721 if (attribute->have_arguments) {
1722 parse_gnu_attribute_const_arg(attribute);
1726 case GNU_AK_FORMAT_ARG:
1727 case GNU_AK_REGPARM:
1728 case GNU_AK_TRAP_EXIT:
1729 if (!attribute->have_arguments) {
1730 /* should have arguments */
1731 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1732 attribute->invalid = true;
1734 parse_gnu_attribute_const_arg(attribute);
1737 case GNU_AK_SECTION:
1738 case GNU_AK_SP_SWITCH:
1739 if (!attribute->have_arguments) {
1740 /* should have arguments */
1741 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1742 attribute->invalid = true;
1744 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1747 if (!attribute->have_arguments) {
1748 /* should have arguments */
1749 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1750 attribute->invalid = true;
1752 parse_gnu_attribute_format_args(attribute);
1754 case GNU_AK_WEAKREF:
1755 /* may have one string argument */
1756 if (attribute->have_arguments)
1757 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1759 case GNU_AK_NONNULL:
1760 if (attribute->have_arguments)
1761 parse_gnu_attribute_const_arg_list(attribute);
1763 case GNU_AK_TLS_MODEL:
1764 if (!attribute->have_arguments) {
1765 /* should have arguments */
1766 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1768 parse_gnu_attribute_tls_model_arg(attribute);
1770 case GNU_AK_VISIBILITY:
1771 if (!attribute->have_arguments) {
1772 /* should have arguments */
1773 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1775 parse_gnu_attribute_visibility_arg(attribute);
1778 if (!attribute->have_arguments) {
1779 /* should have arguments */
1780 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1782 parse_gnu_attribute_model_arg(attribute);
1786 if (!attribute->have_arguments) {
1787 /* should have arguments */
1788 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1790 parse_gnu_attribute_mode_arg(attribute);
1793 case GNU_AK_INTERRUPT:
1794 /* may have one string argument */
1795 if (attribute->have_arguments)
1796 parse_gnu_attribute_interrupt_arg(attribute);
1798 case GNU_AK_SENTINEL:
1799 /* may have one string argument */
1800 if (attribute->have_arguments)
1801 parse_gnu_attribute_const_arg(attribute);
1804 /* already handled */
1808 check_no_argument(attribute, name);
1811 if (attribute != NULL) {
1813 last->next = attribute;
1816 head = last = attribute;
1820 if (token.type != ',')
1834 * Parse GNU attributes.
1836 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1838 decl_modifiers_t modifiers = 0;
1841 switch(token.type) {
1842 case T___attribute__:
1843 modifiers |= parse_gnu_attribute(attributes);
1849 if (token.type != T_STRING_LITERAL) {
1850 parse_error_expected("while parsing assembler attribute",
1851 T_STRING_LITERAL, NULL);
1852 eat_until_matching_token('(');
1855 parse_string_literals();
1860 case T_cdecl: modifiers |= DM_CDECL; break;
1861 case T__fastcall: modifiers |= DM_FASTCALL; break;
1862 case T__stdcall: modifiers |= DM_STDCALL; break;
1865 /* TODO record modifier */
1867 warningf(HERE, "Ignoring declaration modifier %K", &token);
1871 default: return modifiers;
1878 static void mark_decls_read(expression_t *expr, declaration_t *lhs_decl);
1880 static declaration_t *determine_lhs_decl(expression_t *const expr, declaration_t *lhs_decl)
1882 switch (expr->kind) {
1883 case EXPR_REFERENCE: {
1884 declaration_t *const decl = expr->reference.declaration;
1888 case EXPR_ARRAY_ACCESS: {
1889 expression_t *const ref = expr->array_access.array_ref;
1890 declaration_t * decl = NULL;
1891 if (is_type_array(skip_typeref(revert_automatic_type_conversion(ref)))) {
1892 decl = determine_lhs_decl(ref, lhs_decl);
1895 mark_decls_read(expr->select.compound, lhs_decl);
1897 mark_decls_read(expr->array_access.index, lhs_decl);
1902 if (is_type_compound(skip_typeref(expr->base.type))) {
1903 return determine_lhs_decl(expr->select.compound, lhs_decl);
1905 mark_decls_read(expr->select.compound, lhs_decl);
1910 case EXPR_UNARY_DEREFERENCE: {
1911 expression_t *const val = expr->unary.value;
1912 if (val->kind == EXPR_UNARY_TAKE_ADDRESS) {
1914 return determine_lhs_decl(val->unary.value, lhs_decl);
1916 mark_decls_read(val, NULL);
1922 mark_decls_read(expr, NULL);
1927 #define DECL_ANY ((declaration_t*)-1)
1930 * Mark declarations, which are read. This is used to deted variables, which
1934 * x is not marked as "read", because it is only read to calculate its own new
1938 * x and y are not detected as "not read", because multiple variables are
1941 static void mark_decls_read(expression_t *const expr, declaration_t *lhs_decl)
1943 switch (expr->kind) {
1944 case EXPR_REFERENCE: {
1945 declaration_t *const decl = expr->reference.declaration;
1946 if (lhs_decl != decl && lhs_decl != DECL_ANY)
1952 // TODO respect pure/const
1953 mark_decls_read(expr->call.function, NULL);
1954 for (call_argument_t *arg = expr->call.arguments; arg != NULL; arg = arg->next) {
1955 mark_decls_read(arg->expression, NULL);
1959 case EXPR_CONDITIONAL:
1960 // TODO lhs_decl should depend on whether true/false have an effect
1961 mark_decls_read(expr->conditional.condition, NULL);
1962 if (expr->conditional.true_expression != NULL)
1963 mark_decls_read(expr->conditional.true_expression, lhs_decl);
1964 mark_decls_read(expr->conditional.false_expression, lhs_decl);
1968 if (lhs_decl == DECL_ANY && !is_type_compound(skip_typeref(expr->base.type)))
1970 mark_decls_read(expr->select.compound, lhs_decl);
1973 case EXPR_ARRAY_ACCESS: {
1974 expression_t *const ref = expr->array_access.array_ref;
1975 mark_decls_read(ref, lhs_decl);
1976 lhs_decl = determine_lhs_decl(ref, lhs_decl);
1977 mark_decls_read(expr->array_access.index, lhs_decl);
1982 mark_decls_read(expr->va_arge.ap, lhs_decl);
1985 case EXPR_UNARY_CAST:
1986 /* Special case: Use void cast to mark a variable as "read" */
1987 if (is_type_atomic(skip_typeref(expr->base.type), ATOMIC_TYPE_VOID))
1991 case EXPR_UNARY_DEREFERENCE:
1992 if (lhs_decl == DECL_ANY)
1996 case EXPR_UNARY_NEGATE:
1997 case EXPR_UNARY_PLUS:
1998 case EXPR_UNARY_BITWISE_NEGATE:
1999 case EXPR_UNARY_NOT:
2000 case EXPR_UNARY_TAKE_ADDRESS:
2001 case EXPR_UNARY_POSTFIX_INCREMENT:
2002 case EXPR_UNARY_POSTFIX_DECREMENT:
2003 case EXPR_UNARY_PREFIX_INCREMENT:
2004 case EXPR_UNARY_PREFIX_DECREMENT:
2005 case EXPR_UNARY_CAST_IMPLICIT:
2006 case EXPR_UNARY_ASSUME:
2008 mark_decls_read(expr->unary.value, lhs_decl);
2011 case EXPR_BINARY_ADD:
2012 case EXPR_BINARY_SUB:
2013 case EXPR_BINARY_MUL:
2014 case EXPR_BINARY_DIV:
2015 case EXPR_BINARY_MOD:
2016 case EXPR_BINARY_EQUAL:
2017 case EXPR_BINARY_NOTEQUAL:
2018 case EXPR_BINARY_LESS:
2019 case EXPR_BINARY_LESSEQUAL:
2020 case EXPR_BINARY_GREATER:
2021 case EXPR_BINARY_GREATEREQUAL:
2022 case EXPR_BINARY_BITWISE_AND:
2023 case EXPR_BINARY_BITWISE_OR:
2024 case EXPR_BINARY_BITWISE_XOR:
2025 case EXPR_BINARY_LOGICAL_AND:
2026 case EXPR_BINARY_LOGICAL_OR:
2027 case EXPR_BINARY_SHIFTLEFT:
2028 case EXPR_BINARY_SHIFTRIGHT:
2029 case EXPR_BINARY_COMMA:
2030 case EXPR_BINARY_ISGREATER:
2031 case EXPR_BINARY_ISGREATEREQUAL:
2032 case EXPR_BINARY_ISLESS:
2033 case EXPR_BINARY_ISLESSEQUAL:
2034 case EXPR_BINARY_ISLESSGREATER:
2035 case EXPR_BINARY_ISUNORDERED:
2036 mark_decls_read(expr->binary.left, lhs_decl);
2037 mark_decls_read(expr->binary.right, lhs_decl);
2040 case EXPR_BINARY_ASSIGN:
2041 case EXPR_BINARY_MUL_ASSIGN:
2042 case EXPR_BINARY_DIV_ASSIGN:
2043 case EXPR_BINARY_MOD_ASSIGN:
2044 case EXPR_BINARY_ADD_ASSIGN:
2045 case EXPR_BINARY_SUB_ASSIGN:
2046 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2047 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2048 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2049 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2050 case EXPR_BINARY_BITWISE_OR_ASSIGN: {
2051 if (lhs_decl == DECL_ANY)
2053 lhs_decl = determine_lhs_decl(expr->binary.left, lhs_decl);
2054 mark_decls_read(expr->binary.right, lhs_decl);
2059 determine_lhs_decl(expr->va_starte.ap, lhs_decl);
2065 case EXPR_CHARACTER_CONSTANT:
2066 case EXPR_WIDE_CHARACTER_CONSTANT:
2067 case EXPR_STRING_LITERAL:
2068 case EXPR_WIDE_STRING_LITERAL:
2069 case EXPR_COMPOUND_LITERAL: // TODO init?
2071 case EXPR_CLASSIFY_TYPE:
2074 case EXPR_BUILTIN_SYMBOL:
2075 case EXPR_BUILTIN_CONSTANT_P:
2076 case EXPR_BUILTIN_PREFETCH:
2078 case EXPR_STATEMENT: // TODO
2079 case EXPR_LABEL_ADDRESS:
2080 case EXPR_BINARY_BUILTIN_EXPECT:
2084 panic("unhandled expression");
2087 static designator_t *parse_designation(void)
2089 designator_t *result = NULL;
2090 designator_t *last = NULL;
2093 designator_t *designator;
2094 switch(token.type) {
2096 designator = allocate_ast_zero(sizeof(designator[0]));
2097 designator->source_position = token.source_position;
2099 add_anchor_token(']');
2100 designator->array_index = parse_constant_expression();
2101 rem_anchor_token(']');
2105 designator = allocate_ast_zero(sizeof(designator[0]));
2106 designator->source_position = token.source_position;
2108 if (token.type != T_IDENTIFIER) {
2109 parse_error_expected("while parsing designator",
2110 T_IDENTIFIER, NULL);
2113 designator->symbol = token.v.symbol;
2121 assert(designator != NULL);
2123 last->next = designator;
2125 result = designator;
2133 static initializer_t *initializer_from_string(array_type_t *type,
2134 const string_t *const string)
2136 /* TODO: check len vs. size of array type */
2139 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
2140 initializer->string.string = *string;
2145 static initializer_t *initializer_from_wide_string(array_type_t *const type,
2146 wide_string_t *const string)
2148 /* TODO: check len vs. size of array type */
2151 initializer_t *const initializer =
2152 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
2153 initializer->wide_string.string = *string;
2159 * Build an initializer from a given expression.
2161 static initializer_t *initializer_from_expression(type_t *orig_type,
2162 expression_t *expression)
2164 /* TODO check that expression is a constant expression */
2166 /* § 6.7.8.14/15 char array may be initialized by string literals */
2167 type_t *type = skip_typeref(orig_type);
2168 type_t *expr_type_orig = expression->base.type;
2169 type_t *expr_type = skip_typeref(expr_type_orig);
2170 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
2171 array_type_t *const array_type = &type->array;
2172 type_t *const element_type = skip_typeref(array_type->element_type);
2174 if (element_type->kind == TYPE_ATOMIC) {
2175 atomic_type_kind_t akind = element_type->atomic.akind;
2176 switch (expression->kind) {
2177 case EXPR_STRING_LITERAL:
2178 if (akind == ATOMIC_TYPE_CHAR
2179 || akind == ATOMIC_TYPE_SCHAR
2180 || akind == ATOMIC_TYPE_UCHAR) {
2181 return initializer_from_string(array_type,
2182 &expression->string.value);
2185 case EXPR_WIDE_STRING_LITERAL: {
2186 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
2187 if (get_unqualified_type(element_type) == bare_wchar_type) {
2188 return initializer_from_wide_string(array_type,
2189 &expression->wide_string.value);
2199 assign_error_t error = semantic_assign(type, expression);
2200 if (error == ASSIGN_ERROR_INCOMPATIBLE)
2202 report_assign_error(error, type, expression, "initializer",
2203 &expression->base.source_position);
2205 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
2207 if (type->kind == TYPE_BITFIELD) {
2208 type = type->bitfield.base_type;
2211 result->value.value = create_implicit_cast(expression, type);
2217 * Checks if a given expression can be used as an constant initializer.
2219 static bool is_initializer_constant(const expression_t *expression)
2221 return is_constant_expression(expression)
2222 || is_address_constant(expression);
2226 * Parses an scalar initializer.
2228 * § 6.7.8.11; eat {} without warning
2230 static initializer_t *parse_scalar_initializer(type_t *type,
2231 bool must_be_constant)
2233 /* there might be extra {} hierarchies */
2235 if (token.type == '{') {
2237 warningf(HERE, "extra curly braces around scalar initializer");
2241 } while (token.type == '{');
2244 expression_t *expression = parse_assignment_expression();
2245 mark_decls_read(expression, NULL);
2246 if (must_be_constant && !is_initializer_constant(expression)) {
2247 errorf(&expression->base.source_position,
2248 "Initialisation expression '%E' is not constant\n",
2252 initializer_t *initializer = initializer_from_expression(type, expression);
2254 if (initializer == NULL) {
2255 errorf(&expression->base.source_position,
2256 "expression '%E' (type '%T') doesn't match expected type '%T'",
2257 expression, expression->base.type, type);
2262 bool additional_warning_displayed = false;
2263 while (braces > 0) {
2264 if (token.type == ',') {
2267 if (token.type != '}') {
2268 if (!additional_warning_displayed && warning.other) {
2269 warningf(HERE, "additional elements in scalar initializer");
2270 additional_warning_displayed = true;
2281 * An entry in the type path.
2283 typedef struct type_path_entry_t type_path_entry_t;
2284 struct type_path_entry_t {
2285 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2287 size_t index; /**< For array types: the current index. */
2288 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2293 * A type path expression a position inside compound or array types.
2295 typedef struct type_path_t type_path_t;
2296 struct type_path_t {
2297 type_path_entry_t *path; /**< An flexible array containing the current path. */
2298 type_t *top_type; /**< type of the element the path points */
2299 size_t max_index; /**< largest index in outermost array */
2303 * Prints a type path for debugging.
2305 static __attribute__((unused)) void debug_print_type_path(
2306 const type_path_t *path)
2308 size_t len = ARR_LEN(path->path);
2310 for(size_t i = 0; i < len; ++i) {
2311 const type_path_entry_t *entry = & path->path[i];
2313 type_t *type = skip_typeref(entry->type);
2314 if (is_type_compound(type)) {
2315 /* in gcc mode structs can have no members */
2316 if (entry->v.compound_entry == NULL) {
2320 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2321 } else if (is_type_array(type)) {
2322 fprintf(stderr, "[%zu]", entry->v.index);
2324 fprintf(stderr, "-INVALID-");
2327 if (path->top_type != NULL) {
2328 fprintf(stderr, " (");
2329 print_type(path->top_type);
2330 fprintf(stderr, ")");
2335 * Return the top type path entry, ie. in a path
2336 * (type).a.b returns the b.
2338 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2340 size_t len = ARR_LEN(path->path);
2342 return &path->path[len-1];
2346 * Enlarge the type path by an (empty) element.
2348 static type_path_entry_t *append_to_type_path(type_path_t *path)
2350 size_t len = ARR_LEN(path->path);
2351 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2353 type_path_entry_t *result = & path->path[len];
2354 memset(result, 0, sizeof(result[0]));
2359 * Descending into a sub-type. Enter the scope of the current
2362 static void descend_into_subtype(type_path_t *path)
2364 type_t *orig_top_type = path->top_type;
2365 type_t *top_type = skip_typeref(orig_top_type);
2367 type_path_entry_t *top = append_to_type_path(path);
2368 top->type = top_type;
2370 if (is_type_compound(top_type)) {
2371 declaration_t *declaration = top_type->compound.declaration;
2372 declaration_t *entry = declaration->scope.declarations;
2373 top->v.compound_entry = entry;
2375 if (entry != NULL) {
2376 path->top_type = entry->type;
2378 path->top_type = NULL;
2380 } else if (is_type_array(top_type)) {
2382 path->top_type = top_type->array.element_type;
2384 assert(!is_type_valid(top_type));
2389 * Pop an entry from the given type path, ie. returning from
2390 * (type).a.b to (type).a
2392 static void ascend_from_subtype(type_path_t *path)
2394 type_path_entry_t *top = get_type_path_top(path);
2396 path->top_type = top->type;
2398 size_t len = ARR_LEN(path->path);
2399 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2403 * Pop entries from the given type path until the given
2404 * path level is reached.
2406 static void ascend_to(type_path_t *path, size_t top_path_level)
2408 size_t len = ARR_LEN(path->path);
2410 while (len > top_path_level) {
2411 ascend_from_subtype(path);
2412 len = ARR_LEN(path->path);
2416 static bool walk_designator(type_path_t *path, const designator_t *designator,
2417 bool used_in_offsetof)
2419 for( ; designator != NULL; designator = designator->next) {
2420 type_path_entry_t *top = get_type_path_top(path);
2421 type_t *orig_type = top->type;
2423 type_t *type = skip_typeref(orig_type);
2425 if (designator->symbol != NULL) {
2426 symbol_t *symbol = designator->symbol;
2427 if (!is_type_compound(type)) {
2428 if (is_type_valid(type)) {
2429 errorf(&designator->source_position,
2430 "'.%Y' designator used for non-compound type '%T'",
2434 top->type = type_error_type;
2435 top->v.compound_entry = NULL;
2436 orig_type = type_error_type;
2438 declaration_t *declaration = type->compound.declaration;
2439 declaration_t *iter = declaration->scope.declarations;
2440 for( ; iter != NULL; iter = iter->next) {
2441 if (iter->symbol == symbol) {
2446 errorf(&designator->source_position,
2447 "'%T' has no member named '%Y'", orig_type, symbol);
2450 if (used_in_offsetof) {
2451 type_t *real_type = skip_typeref(iter->type);
2452 if (real_type->kind == TYPE_BITFIELD) {
2453 errorf(&designator->source_position,
2454 "offsetof designator '%Y' may not specify bitfield",
2460 top->type = orig_type;
2461 top->v.compound_entry = iter;
2462 orig_type = iter->type;
2465 expression_t *array_index = designator->array_index;
2466 assert(designator->array_index != NULL);
2468 if (!is_type_array(type)) {
2469 if (is_type_valid(type)) {
2470 errorf(&designator->source_position,
2471 "[%E] designator used for non-array type '%T'",
2472 array_index, orig_type);
2477 long index = fold_constant(array_index);
2478 if (!used_in_offsetof) {
2480 errorf(&designator->source_position,
2481 "array index [%E] must be positive", array_index);
2482 } else if (type->array.size_constant) {
2483 long array_size = type->array.size;
2484 if (index >= array_size) {
2485 errorf(&designator->source_position,
2486 "designator [%E] (%d) exceeds array size %d",
2487 array_index, index, array_size);
2492 top->type = orig_type;
2493 top->v.index = (size_t) index;
2494 orig_type = type->array.element_type;
2496 path->top_type = orig_type;
2498 if (designator->next != NULL) {
2499 descend_into_subtype(path);
2508 static void advance_current_object(type_path_t *path, size_t top_path_level)
2510 type_path_entry_t *top = get_type_path_top(path);
2512 type_t *type = skip_typeref(top->type);
2513 if (is_type_union(type)) {
2514 /* in unions only the first element is initialized */
2515 top->v.compound_entry = NULL;
2516 } else if (is_type_struct(type)) {
2517 declaration_t *entry = top->v.compound_entry;
2519 entry = entry->next;
2520 top->v.compound_entry = entry;
2521 if (entry != NULL) {
2522 path->top_type = entry->type;
2525 } else if (is_type_array(type)) {
2526 assert(is_type_array(type));
2530 if (!type->array.size_constant || top->v.index < type->array.size) {
2534 assert(!is_type_valid(type));
2538 /* we're past the last member of the current sub-aggregate, try if we
2539 * can ascend in the type hierarchy and continue with another subobject */
2540 size_t len = ARR_LEN(path->path);
2542 if (len > top_path_level) {
2543 ascend_from_subtype(path);
2544 advance_current_object(path, top_path_level);
2546 path->top_type = NULL;
2551 * skip until token is found.
2553 static void skip_until(int type)
2555 while (token.type != type) {
2556 if (token.type == T_EOF)
2563 * skip any {...} blocks until a closing bracket is reached.
2565 static void skip_initializers(void)
2567 if (token.type == '{')
2570 while (token.type != '}') {
2571 if (token.type == T_EOF)
2573 if (token.type == '{') {
2581 static initializer_t *create_empty_initializer(void)
2583 static initializer_t empty_initializer
2584 = { .list = { { INITIALIZER_LIST }, 0 } };
2585 return &empty_initializer;
2589 * Parse a part of an initialiser for a struct or union,
2591 static initializer_t *parse_sub_initializer(type_path_t *path,
2592 type_t *outer_type, size_t top_path_level,
2593 parse_initializer_env_t *env)
2595 if (token.type == '}') {
2596 /* empty initializer */
2597 return create_empty_initializer();
2600 type_t *orig_type = path->top_type;
2601 type_t *type = NULL;
2603 if (orig_type == NULL) {
2604 /* We are initializing an empty compound. */
2606 type = skip_typeref(orig_type);
2609 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2612 designator_t *designator = NULL;
2613 if (token.type == '.' || token.type == '[') {
2614 designator = parse_designation();
2615 goto finish_designator;
2616 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2617 /* GNU-style designator ("identifier: value") */
2618 designator = allocate_ast_zero(sizeof(designator[0]));
2619 designator->source_position = token.source_position;
2620 designator->symbol = token.v.symbol;
2625 /* reset path to toplevel, evaluate designator from there */
2626 ascend_to(path, top_path_level);
2627 if (!walk_designator(path, designator, false)) {
2628 /* can't continue after designation error */
2632 initializer_t *designator_initializer
2633 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2634 designator_initializer->designator.designator = designator;
2635 ARR_APP1(initializer_t*, initializers, designator_initializer);
2637 orig_type = path->top_type;
2638 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2643 if (token.type == '{') {
2644 if (type != NULL && is_type_scalar(type)) {
2645 sub = parse_scalar_initializer(type, env->must_be_constant);
2649 if (env->declaration != NULL) {
2650 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2651 env->declaration->symbol);
2653 errorf(HERE, "extra brace group at end of initializer");
2656 descend_into_subtype(path);
2658 add_anchor_token('}');
2659 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2661 rem_anchor_token('}');
2664 ascend_from_subtype(path);
2668 goto error_parse_next;
2672 /* must be an expression */
2673 expression_t *expression = parse_assignment_expression();
2675 if (env->must_be_constant && !is_initializer_constant(expression)) {
2676 errorf(&expression->base.source_position,
2677 "Initialisation expression '%E' is not constant\n",
2682 /* we are already outside, ... */
2683 type_t *const outer_type_skip = skip_typeref(outer_type);
2684 if (is_type_compound(outer_type_skip) &&
2685 !outer_type_skip->compound.declaration->init.complete) {
2686 goto error_parse_next;
2691 /* handle { "string" } special case */
2692 if ((expression->kind == EXPR_STRING_LITERAL
2693 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2694 && outer_type != NULL) {
2695 sub = initializer_from_expression(outer_type, expression);
2697 if (token.type == ',') {
2700 if (token.type != '}' && warning.other) {
2701 warningf(HERE, "excessive elements in initializer for type '%T'",
2704 /* TODO: eat , ... */
2709 /* descend into subtypes until expression matches type */
2711 orig_type = path->top_type;
2712 type = skip_typeref(orig_type);
2714 sub = initializer_from_expression(orig_type, expression);
2718 if (!is_type_valid(type)) {
2721 if (is_type_scalar(type)) {
2722 errorf(&expression->base.source_position,
2723 "expression '%E' doesn't match expected type '%T'",
2724 expression, orig_type);
2728 descend_into_subtype(path);
2732 /* update largest index of top array */
2733 const type_path_entry_t *first = &path->path[0];
2734 type_t *first_type = first->type;
2735 first_type = skip_typeref(first_type);
2736 if (is_type_array(first_type)) {
2737 size_t index = first->v.index;
2738 if (index > path->max_index)
2739 path->max_index = index;
2743 /* append to initializers list */
2744 ARR_APP1(initializer_t*, initializers, sub);
2747 if (warning.other) {
2748 if (env->declaration != NULL) {
2749 warningf(HERE, "excess elements in struct initializer for '%Y'",
2750 env->declaration->symbol);
2752 warningf(HERE, "excess elements in struct initializer");
2758 if (token.type == '}') {
2762 if (token.type == '}') {
2767 /* advance to the next declaration if we are not at the end */
2768 advance_current_object(path, top_path_level);
2769 orig_type = path->top_type;
2770 if (orig_type != NULL)
2771 type = skip_typeref(orig_type);
2777 size_t len = ARR_LEN(initializers);
2778 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2779 initializer_t *result = allocate_ast_zero(size);
2780 result->kind = INITIALIZER_LIST;
2781 result->list.len = len;
2782 memcpy(&result->list.initializers, initializers,
2783 len * sizeof(initializers[0]));
2785 DEL_ARR_F(initializers);
2786 ascend_to(path, top_path_level+1);
2791 skip_initializers();
2792 DEL_ARR_F(initializers);
2793 ascend_to(path, top_path_level+1);
2798 * Parses an initializer. Parsers either a compound literal
2799 * (env->declaration == NULL) or an initializer of a declaration.
2801 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2803 type_t *type = skip_typeref(env->type);
2804 initializer_t *result = NULL;
2807 if (is_type_scalar(type)) {
2808 result = parse_scalar_initializer(type, env->must_be_constant);
2809 } else if (token.type == '{') {
2813 memset(&path, 0, sizeof(path));
2814 path.top_type = env->type;
2815 path.path = NEW_ARR_F(type_path_entry_t, 0);
2817 descend_into_subtype(&path);
2819 add_anchor_token('}');
2820 result = parse_sub_initializer(&path, env->type, 1, env);
2821 rem_anchor_token('}');
2823 max_index = path.max_index;
2824 DEL_ARR_F(path.path);
2828 /* parse_scalar_initializer() also works in this case: we simply
2829 * have an expression without {} around it */
2830 result = parse_scalar_initializer(type, env->must_be_constant);
2833 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2834 * the array type size */
2835 if (is_type_array(type) && type->array.size_expression == NULL
2836 && result != NULL) {
2838 switch (result->kind) {
2839 case INITIALIZER_LIST:
2840 size = max_index + 1;
2843 case INITIALIZER_STRING:
2844 size = result->string.string.size;
2847 case INITIALIZER_WIDE_STRING:
2848 size = result->wide_string.string.size;
2851 case INITIALIZER_DESIGNATOR:
2852 case INITIALIZER_VALUE:
2853 /* can happen for parse errors */
2858 internal_errorf(HERE, "invalid initializer type");
2861 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2862 cnst->base.type = type_size_t;
2863 cnst->conste.v.int_value = size;
2865 type_t *new_type = duplicate_type(type);
2867 new_type->array.size_expression = cnst;
2868 new_type->array.size_constant = true;
2869 new_type->array.size = size;
2870 env->type = new_type;
2878 static declaration_t *append_declaration(declaration_t *declaration);
2880 static declaration_t *parse_compound_type_specifier(bool is_struct)
2882 gnu_attribute_t *attributes = NULL;
2883 decl_modifiers_t modifiers = 0;
2890 symbol_t *symbol = NULL;
2891 declaration_t *declaration = NULL;
2893 if (token.type == T___attribute__) {
2894 modifiers |= parse_attributes(&attributes);
2897 if (token.type == T_IDENTIFIER) {
2898 symbol = token.v.symbol;
2901 namespace_t const namespc =
2902 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2903 declaration = get_declaration(symbol, namespc);
2904 if (declaration != NULL) {
2905 if (declaration->parent_scope != scope &&
2906 (token.type == '{' || token.type == ';')) {
2908 } else if (declaration->init.complete &&
2909 token.type == '{') {
2910 assert(symbol != NULL);
2911 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2912 is_struct ? "struct" : "union", symbol,
2913 &declaration->source_position);
2914 declaration->scope.declarations = NULL;
2917 } else if (token.type != '{') {
2919 parse_error_expected("while parsing struct type specifier",
2920 T_IDENTIFIER, '{', NULL);
2922 parse_error_expected("while parsing union type specifier",
2923 T_IDENTIFIER, '{', NULL);
2929 if (declaration == NULL) {
2930 declaration = allocate_declaration_zero();
2931 declaration->namespc =
2932 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2933 declaration->source_position = token.source_position;
2934 declaration->symbol = symbol;
2935 declaration->parent_scope = scope;
2936 if (symbol != NULL) {
2937 environment_push(declaration);
2939 append_declaration(declaration);
2942 if (token.type == '{') {
2943 declaration->init.complete = true;
2945 parse_compound_type_entries(declaration);
2946 modifiers |= parse_attributes(&attributes);
2949 declaration->modifiers |= modifiers;
2953 static void parse_enum_entries(type_t *const enum_type)
2957 if (token.type == '}') {
2959 errorf(HERE, "empty enum not allowed");
2963 add_anchor_token('}');
2965 if (token.type != T_IDENTIFIER) {
2966 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2968 rem_anchor_token('}');
2972 declaration_t *const entry = allocate_declaration_zero();
2973 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2974 entry->type = enum_type;
2975 entry->symbol = token.v.symbol;
2976 entry->source_position = token.source_position;
2979 if (token.type == '=') {
2981 expression_t *value = parse_constant_expression();
2983 value = create_implicit_cast(value, enum_type);
2984 entry->init.enum_value = value;
2989 record_declaration(entry, false);
2991 if (token.type != ',')
2994 } while (token.type != '}');
2995 rem_anchor_token('}');
3003 static type_t *parse_enum_specifier(void)
3005 gnu_attribute_t *attributes = NULL;
3006 declaration_t *declaration;
3010 if (token.type == T_IDENTIFIER) {
3011 symbol = token.v.symbol;
3014 declaration = get_declaration(symbol, NAMESPACE_ENUM);
3015 } else if (token.type != '{') {
3016 parse_error_expected("while parsing enum type specifier",
3017 T_IDENTIFIER, '{', NULL);
3024 if (declaration == NULL) {
3025 declaration = allocate_declaration_zero();
3026 declaration->namespc = NAMESPACE_ENUM;
3027 declaration->source_position = token.source_position;
3028 declaration->symbol = symbol;
3029 declaration->parent_scope = scope;
3032 type_t *const type = allocate_type_zero(TYPE_ENUM);
3033 type->enumt.declaration = declaration;
3035 if (token.type == '{') {
3036 if (declaration->init.complete) {
3037 errorf(HERE, "multiple definitions of enum %Y", symbol);
3039 if (symbol != NULL) {
3040 environment_push(declaration);
3042 append_declaration(declaration);
3043 declaration->init.complete = true;
3045 parse_enum_entries(type);
3046 parse_attributes(&attributes);
3053 * if a symbol is a typedef to another type, return true
3055 static bool is_typedef_symbol(symbol_t *symbol)
3057 const declaration_t *const declaration =
3058 get_declaration(symbol, NAMESPACE_NORMAL);
3060 declaration != NULL &&
3061 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
3064 static type_t *parse_typeof(void)
3071 add_anchor_token(')');
3073 expression_t *expression = NULL;
3075 bool old_type_prop = in_type_prop;
3076 bool old_gcc_extension = in_gcc_extension;
3077 in_type_prop = true;
3079 while (token.type == T___extension__) {
3080 /* This can be a prefix to a typename or an expression. */
3082 in_gcc_extension = true;
3084 switch (token.type) {
3086 if (is_typedef_symbol(token.v.symbol)) {
3087 type = parse_typename();
3089 expression = parse_expression();
3090 type = expression->base.type;
3095 type = parse_typename();
3099 expression = parse_expression();
3100 type = expression->base.type;
3103 in_type_prop = old_type_prop;
3104 in_gcc_extension = old_gcc_extension;
3106 rem_anchor_token(')');
3109 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
3110 typeof_type->typeoft.expression = expression;
3111 typeof_type->typeoft.typeof_type = type;
3118 typedef enum specifiers_t {
3119 SPECIFIER_SIGNED = 1 << 0,
3120 SPECIFIER_UNSIGNED = 1 << 1,
3121 SPECIFIER_LONG = 1 << 2,
3122 SPECIFIER_INT = 1 << 3,
3123 SPECIFIER_DOUBLE = 1 << 4,
3124 SPECIFIER_CHAR = 1 << 5,
3125 SPECIFIER_SHORT = 1 << 6,
3126 SPECIFIER_LONG_LONG = 1 << 7,
3127 SPECIFIER_FLOAT = 1 << 8,
3128 SPECIFIER_BOOL = 1 << 9,
3129 SPECIFIER_VOID = 1 << 10,
3130 SPECIFIER_INT8 = 1 << 11,
3131 SPECIFIER_INT16 = 1 << 12,
3132 SPECIFIER_INT32 = 1 << 13,
3133 SPECIFIER_INT64 = 1 << 14,
3134 SPECIFIER_INT128 = 1 << 15,
3135 SPECIFIER_COMPLEX = 1 << 16,
3136 SPECIFIER_IMAGINARY = 1 << 17,
3139 static type_t *create_builtin_type(symbol_t *const symbol,
3140 type_t *const real_type)
3142 type_t *type = allocate_type_zero(TYPE_BUILTIN);
3143 type->builtin.symbol = symbol;
3144 type->builtin.real_type = real_type;
3146 type_t *result = typehash_insert(type);
3147 if (type != result) {
3154 static type_t *get_typedef_type(symbol_t *symbol)
3156 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
3157 if (declaration == NULL ||
3158 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
3161 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
3162 type->typedeft.declaration = declaration;
3168 * check for the allowed MS alignment values.
3170 static bool check_alignment_value(long long intvalue)
3172 if (intvalue < 1 || intvalue > 8192) {
3173 errorf(HERE, "illegal alignment value");
3176 unsigned v = (unsigned)intvalue;
3177 for (unsigned i = 1; i <= 8192; i += i) {
3181 errorf(HERE, "alignment must be power of two");
3185 #define DET_MOD(name, tag) do { \
3186 if (*modifiers & tag && warning.other) warningf(HERE, #name " used more than once"); \
3187 *modifiers |= tag; \
3190 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
3192 decl_modifiers_t *modifiers = &specifiers->modifiers;
3195 if (token.type == T_restrict) {
3197 DET_MOD(restrict, DM_RESTRICT);
3199 } else if (token.type != T_IDENTIFIER)
3201 symbol_t *symbol = token.v.symbol;
3202 if (symbol == sym_align) {
3205 if (token.type != T_INTEGER)
3207 if (check_alignment_value(token.v.intvalue)) {
3208 if (specifiers->alignment != 0 && warning.other)
3209 warningf(HERE, "align used more than once");
3210 specifiers->alignment = (unsigned char)token.v.intvalue;
3214 } else if (symbol == sym_allocate) {
3217 if (token.type != T_IDENTIFIER)
3219 (void)token.v.symbol;
3221 } else if (symbol == sym_dllimport) {
3223 DET_MOD(dllimport, DM_DLLIMPORT);
3224 } else if (symbol == sym_dllexport) {
3226 DET_MOD(dllexport, DM_DLLEXPORT);
3227 } else if (symbol == sym_thread) {
3229 DET_MOD(thread, DM_THREAD);
3230 } else if (symbol == sym_naked) {
3232 DET_MOD(naked, DM_NAKED);
3233 } else if (symbol == sym_noinline) {
3235 DET_MOD(noinline, DM_NOINLINE);
3236 } else if (symbol == sym_noreturn) {
3238 DET_MOD(noreturn, DM_NORETURN);
3239 } else if (symbol == sym_nothrow) {
3241 DET_MOD(nothrow, DM_NOTHROW);
3242 } else if (symbol == sym_novtable) {
3244 DET_MOD(novtable, DM_NOVTABLE);
3245 } else if (symbol == sym_property) {
3249 bool is_get = false;
3250 if (token.type != T_IDENTIFIER)
3252 if (token.v.symbol == sym_get) {
3254 } else if (token.v.symbol == sym_put) {
3256 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
3261 if (token.type != T_IDENTIFIER)
3264 if (specifiers->get_property_sym != NULL) {
3265 errorf(HERE, "get property name already specified");
3267 specifiers->get_property_sym = token.v.symbol;
3270 if (specifiers->put_property_sym != NULL) {
3271 errorf(HERE, "put property name already specified");
3273 specifiers->put_property_sym = token.v.symbol;
3277 if (token.type == ',') {
3284 } else if (symbol == sym_selectany) {
3286 DET_MOD(selectany, DM_SELECTANY);
3287 } else if (symbol == sym_uuid) {
3290 if (token.type != T_STRING_LITERAL)
3294 } else if (symbol == sym_deprecated) {
3296 if (specifiers->deprecated != 0 && warning.other)
3297 warningf(HERE, "deprecated used more than once");
3298 specifiers->deprecated = 1;
3299 if (token.type == '(') {
3301 if (token.type == T_STRING_LITERAL) {
3302 specifiers->deprecated_string = token.v.string.begin;
3305 errorf(HERE, "string literal expected");
3309 } else if (symbol == sym_noalias) {
3311 DET_MOD(noalias, DM_NOALIAS);
3314 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3316 if (token.type == '(')
3320 if (token.type == ',')
3327 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3329 declaration_t *const decl = allocate_declaration_zero();
3330 decl->source_position = *HERE;
3331 decl->declared_storage_class = storage_class;
3332 decl->storage_class =
3333 storage_class != STORAGE_CLASS_NONE || scope == file_scope ?
3334 storage_class : STORAGE_CLASS_AUTO;
3335 decl->symbol = symbol;
3336 decl->implicit = true;
3337 record_declaration(decl, false);
3342 * Finish the construction of a struct type by calculating
3343 * its size, offsets, alignment.
3345 static void finish_struct_type(compound_type_t *type) {
3346 if (type->declaration == NULL)
3348 declaration_t *struct_decl = type->declaration;
3349 if (! struct_decl->init.complete)
3354 il_alignment_t alignment = 1;
3355 bool need_pad = false;
3357 declaration_t *entry = struct_decl->scope.declarations;
3358 for (; entry != NULL; entry = entry->next) {
3359 if (entry->namespc != NAMESPACE_NORMAL)
3362 type_t *m_type = skip_typeref(entry->type);
3363 if (! is_type_valid(m_type)) {
3364 /* simply ignore errors here */
3367 il_alignment_t m_alignment = m_type->base.alignment;
3368 if (m_alignment > alignment)
3369 alignment = m_alignment;
3371 offset = (size + m_alignment - 1) & -m_alignment;
3375 entry->offset = offset;
3376 size = offset + m_type->base.size;
3378 if (type->base.alignment != 0) {
3379 alignment = type->base.alignment;
3382 offset = (size + alignment - 1) & -alignment;
3386 if (warning.padded && need_pad) {
3387 warningf(&struct_decl->source_position,
3388 "'%#T' needs padding", type, struct_decl->symbol);
3390 if (warning.packed && !need_pad) {
3391 warningf(&struct_decl->source_position,
3392 "superfluous packed attribute on '%#T'",
3393 type, struct_decl->symbol);
3396 type->base.size = offset;
3397 type->base.alignment = alignment;
3401 * Finish the construction of an union type by calculating
3402 * its size and alignment.
3404 static void finish_union_type(compound_type_t *type) {
3405 if (type->declaration == NULL)
3407 declaration_t *union_decl = type->declaration;
3408 if (! union_decl->init.complete)
3412 il_alignment_t alignment = 1;
3414 declaration_t *entry = union_decl->scope.declarations;
3415 for (; entry != NULL; entry = entry->next) {
3416 if (entry->namespc != NAMESPACE_NORMAL)
3419 type_t *m_type = skip_typeref(entry->type);
3420 if (! is_type_valid(m_type))
3424 if (m_type->base.size > size)
3425 size = m_type->base.size;
3426 if (m_type->base.alignment > alignment)
3427 alignment = m_type->base.alignment;
3429 if (type->base.alignment != 0) {
3430 alignment = type->base.alignment;
3432 size = (size + alignment - 1) & -alignment;
3433 type->base.size = size;
3434 type->base.alignment = alignment;
3437 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3439 type_t *type = NULL;
3440 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3441 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3442 unsigned type_specifiers = 0;
3443 bool newtype = false;
3444 bool saw_error = false;
3445 bool old_gcc_extension = in_gcc_extension;
3447 specifiers->source_position = token.source_position;
3450 specifiers->modifiers
3451 |= parse_attributes(&specifiers->gnu_attributes);
3452 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3453 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3455 switch (token.type) {
3458 #define MATCH_STORAGE_CLASS(token, class) \
3460 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3461 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3463 specifiers->declared_storage_class = class; \
3467 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3468 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3469 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3470 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3471 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3476 add_anchor_token(')');
3477 parse_microsoft_extended_decl_modifier(specifiers);
3478 rem_anchor_token(')');
3483 switch (specifiers->declared_storage_class) {
3484 case STORAGE_CLASS_NONE:
3485 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3488 case STORAGE_CLASS_EXTERN:
3489 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3492 case STORAGE_CLASS_STATIC:
3493 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3497 errorf(HERE, "multiple storage classes in declaration specifiers");
3503 /* type qualifiers */
3504 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3506 qualifiers |= qualifier; \
3510 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3511 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3512 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3513 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3514 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3515 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3516 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3517 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3519 case T___extension__:
3521 in_gcc_extension = true;
3524 /* type specifiers */
3525 #define MATCH_SPECIFIER(token, specifier, name) \
3528 if (type_specifiers & specifier) { \
3529 errorf(HERE, "multiple " name " type specifiers given"); \
3531 type_specifiers |= specifier; \
3535 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3536 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3537 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3538 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3539 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3540 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3541 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3542 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3543 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3544 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3545 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3546 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3547 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3548 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3549 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3550 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3552 case T__forceinline:
3553 /* only in microsoft mode */
3554 specifiers->modifiers |= DM_FORCEINLINE;
3559 specifiers->is_inline = true;
3564 if (type_specifiers & SPECIFIER_LONG_LONG) {
3565 errorf(HERE, "multiple type specifiers given");
3566 } else if (type_specifiers & SPECIFIER_LONG) {
3567 type_specifiers |= SPECIFIER_LONG_LONG;
3569 type_specifiers |= SPECIFIER_LONG;
3574 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
3576 type->compound.declaration = parse_compound_type_specifier(true);
3577 finish_struct_type(&type->compound);
3581 type = allocate_type_zero(TYPE_COMPOUND_UNION);
3582 type->compound.declaration = parse_compound_type_specifier(false);
3583 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3584 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3585 finish_union_type(&type->compound);
3589 type = parse_enum_specifier();
3592 type = parse_typeof();
3594 case T___builtin_va_list:
3595 type = duplicate_type(type_valist);
3599 case T_IDENTIFIER: {
3600 /* only parse identifier if we haven't found a type yet */
3601 if (type != NULL || type_specifiers != 0) {
3602 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3603 * declaration, so it doesn't generate errors about expecting '(' or
3605 switch (look_ahead(1)->type) {
3612 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3615 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3620 goto finish_specifiers;
3624 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3625 if (typedef_type == NULL) {
3626 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3627 * declaration, so it doesn't generate 'implicit int' followed by more
3628 * errors later on. */
3629 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3634 errorf(HERE, "%K does not name a type", &token);
3636 declaration_t *const decl =
3637 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3639 type = allocate_type_zero(TYPE_TYPEDEF);
3640 type->typedeft.declaration = decl;
3644 if (la1_type == '*')
3645 goto finish_specifiers;
3650 goto finish_specifiers;
3655 type = typedef_type;
3659 /* function specifier */
3661 goto finish_specifiers;
3666 in_gcc_extension = old_gcc_extension;
3668 if (type == NULL || (saw_error && type_specifiers != 0)) {
3669 atomic_type_kind_t atomic_type;
3671 /* match valid basic types */
3672 switch(type_specifiers) {
3673 case SPECIFIER_VOID:
3674 atomic_type = ATOMIC_TYPE_VOID;
3676 case SPECIFIER_CHAR:
3677 atomic_type = ATOMIC_TYPE_CHAR;
3679 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3680 atomic_type = ATOMIC_TYPE_SCHAR;
3682 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3683 atomic_type = ATOMIC_TYPE_UCHAR;
3685 case SPECIFIER_SHORT:
3686 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3687 case SPECIFIER_SHORT | SPECIFIER_INT:
3688 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3689 atomic_type = ATOMIC_TYPE_SHORT;
3691 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3692 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3693 atomic_type = ATOMIC_TYPE_USHORT;
3696 case SPECIFIER_SIGNED:
3697 case SPECIFIER_SIGNED | SPECIFIER_INT:
3698 atomic_type = ATOMIC_TYPE_INT;
3700 case SPECIFIER_UNSIGNED:
3701 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3702 atomic_type = ATOMIC_TYPE_UINT;
3704 case SPECIFIER_LONG:
3705 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3706 case SPECIFIER_LONG | SPECIFIER_INT:
3707 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3708 atomic_type = ATOMIC_TYPE_LONG;
3710 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3712 atomic_type = ATOMIC_TYPE_ULONG;
3715 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3716 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3717 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3718 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3720 atomic_type = ATOMIC_TYPE_LONGLONG;
3721 goto warn_about_long_long;
3723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3724 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3726 atomic_type = ATOMIC_TYPE_ULONGLONG;
3727 warn_about_long_long:
3728 if (warning.long_long) {
3729 warningf(&specifiers->source_position,
3730 "ISO C90 does not support 'long long'");
3734 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3735 atomic_type = unsigned_int8_type_kind;
3738 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3739 atomic_type = unsigned_int16_type_kind;
3742 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3743 atomic_type = unsigned_int32_type_kind;
3746 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3747 atomic_type = unsigned_int64_type_kind;
3750 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3751 atomic_type = unsigned_int128_type_kind;
3754 case SPECIFIER_INT8:
3755 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3756 atomic_type = int8_type_kind;
3759 case SPECIFIER_INT16:
3760 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3761 atomic_type = int16_type_kind;
3764 case SPECIFIER_INT32:
3765 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3766 atomic_type = int32_type_kind;
3769 case SPECIFIER_INT64:
3770 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3771 atomic_type = int64_type_kind;
3774 case SPECIFIER_INT128:
3775 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3776 atomic_type = int128_type_kind;
3779 case SPECIFIER_FLOAT:
3780 atomic_type = ATOMIC_TYPE_FLOAT;
3782 case SPECIFIER_DOUBLE:
3783 atomic_type = ATOMIC_TYPE_DOUBLE;
3785 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3786 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3788 case SPECIFIER_BOOL:
3789 atomic_type = ATOMIC_TYPE_BOOL;
3791 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3792 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3793 atomic_type = ATOMIC_TYPE_FLOAT;
3795 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3796 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3797 atomic_type = ATOMIC_TYPE_DOUBLE;
3799 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3800 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3801 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3804 /* invalid specifier combination, give an error message */
3805 if (type_specifiers == 0) {
3810 if (warning.implicit_int) {
3811 warningf(HERE, "no type specifiers in declaration, using 'int'");
3813 atomic_type = ATOMIC_TYPE_INT;
3816 errorf(HERE, "no type specifiers given in declaration");
3818 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3819 (type_specifiers & SPECIFIER_UNSIGNED)) {
3820 errorf(HERE, "signed and unsigned specifiers given");
3821 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3822 errorf(HERE, "only integer types can be signed or unsigned");
3824 errorf(HERE, "multiple datatypes in declaration");
3829 if (type_specifiers & SPECIFIER_COMPLEX) {
3830 type = allocate_type_zero(TYPE_COMPLEX);
3831 type->complex.akind = atomic_type;
3832 } else if (type_specifiers & SPECIFIER_IMAGINARY) {
3833 type = allocate_type_zero(TYPE_IMAGINARY);
3834 type->imaginary.akind = atomic_type;
3836 type = allocate_type_zero(TYPE_ATOMIC);
3837 type->atomic.akind = atomic_type;
3840 } else if (type_specifiers != 0) {
3841 errorf(HERE, "multiple datatypes in declaration");
3844 /* FIXME: check type qualifiers here */
3846 type->base.qualifiers = qualifiers;
3847 type->base.modifiers = modifiers;
3849 type_t *result = typehash_insert(type);
3850 if (newtype && result != type) {
3854 specifiers->type = result;
3858 specifiers->type = type_error_type;
3862 static type_qualifiers_t parse_type_qualifiers(void)
3864 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3867 switch(token.type) {
3868 /* type qualifiers */
3869 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3870 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3871 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3872 /* microsoft extended type modifiers */
3873 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3874 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3875 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3876 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3877 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3886 * Parses an K&R identifier list and return a list of declarations.
3888 * @param last points to the last declaration in the list
3889 * @return the list of declarations
3891 static declaration_t *parse_identifier_list(declaration_t **last)
3893 declaration_t *declarations = NULL;
3894 declaration_t *last_declaration = NULL;
3896 declaration_t *const declaration = allocate_declaration_zero();
3897 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3898 declaration->source_position = token.source_position;
3899 declaration->symbol = token.v.symbol;
3902 if (last_declaration != NULL) {
3903 last_declaration->next = declaration;
3905 declarations = declaration;
3907 last_declaration = declaration;
3909 if (token.type != ',') {
3913 } while (token.type == T_IDENTIFIER);
3915 *last = last_declaration;
3916 return declarations;
3919 static type_t *automatic_type_conversion(type_t *orig_type);
3921 static void semantic_parameter(declaration_t *declaration)
3923 /* TODO: improve error messages */
3924 source_position_t const* const pos = &declaration->source_position;
3926 switch (declaration->declared_storage_class) {
3927 case STORAGE_CLASS_TYPEDEF:
3928 errorf(pos, "typedef not allowed in parameter list");
3931 /* Allowed storage classes */
3932 case STORAGE_CLASS_NONE:
3933 case STORAGE_CLASS_REGISTER:
3937 errorf(pos, "parameter may only have none or register storage class");
3941 type_t *const orig_type = declaration->type;
3942 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3943 * sugar. Turn it into a pointer.
3944 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3945 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3947 type_t *const type = automatic_type_conversion(orig_type);
3948 declaration->type = type;
3950 if (is_type_incomplete(skip_typeref(type))) {
3951 errorf(pos, "parameter '%#T' is of incomplete type",
3952 orig_type, declaration->symbol);
3956 static declaration_t *parse_parameter(void)
3958 declaration_specifiers_t specifiers;
3959 memset(&specifiers, 0, sizeof(specifiers));
3961 parse_declaration_specifiers(&specifiers);
3963 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3969 * Parses a function type parameter list and return a list of declarations.
3971 * @param last point to the last element of the list
3972 * @return the parameter list
3974 static declaration_t *parse_parameters(function_type_t *type, declaration_t **last)
3976 declaration_t *declarations = NULL;
3979 add_anchor_token(')');
3980 int saved_comma_state = save_and_reset_anchor_state(',');
3982 if (token.type == T_IDENTIFIER &&
3983 !is_typedef_symbol(token.v.symbol)) {
3984 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
3985 if (la1_type == ',' || la1_type == ')') {
3986 type->kr_style_parameters = true;
3987 declarations = parse_identifier_list(last);
3988 goto parameters_finished;
3992 if (token.type == ')') {
3993 type->unspecified_parameters = 1;
3994 goto parameters_finished;
3997 declaration_t *declaration;
3998 declaration_t *last_declaration = NULL;
3999 function_parameter_t *parameter;
4000 function_parameter_t *last_parameter = NULL;
4003 switch(token.type) {
4007 goto parameters_finished;
4010 case T___extension__:
4012 declaration = parse_parameter();
4014 /* func(void) is not a parameter */
4015 if (last_parameter == NULL
4016 && token.type == ')'
4017 && declaration->symbol == NULL
4018 && skip_typeref(declaration->type) == type_void) {
4019 goto parameters_finished;
4021 semantic_parameter(declaration);
4023 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
4024 memset(parameter, 0, sizeof(parameter[0]));
4025 parameter->type = declaration->type;
4027 if (last_parameter != NULL) {
4028 last_declaration->next = declaration;
4029 last_parameter->next = parameter;
4031 type->parameters = parameter;
4032 declarations = declaration;
4034 last_parameter = parameter;
4035 last_declaration = declaration;
4039 goto parameters_finished;
4041 if (token.type != ',') {
4042 goto parameters_finished;
4048 parameters_finished:
4049 rem_anchor_token(')');
4052 restore_anchor_state(',', saved_comma_state);
4053 *last = last_declaration;
4054 return declarations;
4057 restore_anchor_state(',', saved_comma_state);
4062 typedef enum construct_type_kind_t {
4067 } construct_type_kind_t;
4069 typedef struct construct_type_t construct_type_t;
4070 struct construct_type_t {
4071 construct_type_kind_t kind;
4072 construct_type_t *next;
4075 typedef struct parsed_pointer_t parsed_pointer_t;
4076 struct parsed_pointer_t {
4077 construct_type_t construct_type;
4078 type_qualifiers_t type_qualifiers;
4081 typedef struct construct_function_type_t construct_function_type_t;
4082 struct construct_function_type_t {
4083 construct_type_t construct_type;
4084 type_t *function_type;
4087 typedef struct parsed_array_t parsed_array_t;
4088 struct parsed_array_t {
4089 construct_type_t construct_type;
4090 type_qualifiers_t type_qualifiers;
4096 typedef struct construct_base_type_t construct_base_type_t;
4097 struct construct_base_type_t {
4098 construct_type_t construct_type;
4102 static construct_type_t *parse_pointer_declarator(void)
4106 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
4107 memset(pointer, 0, sizeof(pointer[0]));
4108 pointer->construct_type.kind = CONSTRUCT_POINTER;
4109 pointer->type_qualifiers = parse_type_qualifiers();
4111 return (construct_type_t*) pointer;
4114 static construct_type_t *parse_array_declarator(void)
4117 add_anchor_token(']');
4119 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
4120 memset(array, 0, sizeof(array[0]));
4121 array->construct_type.kind = CONSTRUCT_ARRAY;
4123 if (token.type == T_static) {
4124 array->is_static = true;
4128 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
4129 if (type_qualifiers != 0) {
4130 if (token.type == T_static) {
4131 array->is_static = true;
4135 array->type_qualifiers = type_qualifiers;
4137 if (token.type == '*' && look_ahead(1)->type == ']') {
4138 array->is_variable = true;
4140 } else if (token.type != ']') {
4141 array->size = parse_assignment_expression();
4144 rem_anchor_token(']');
4148 return (construct_type_t*) array;
4151 static construct_type_t *parse_function_declarator(declaration_t *declaration)
4154 if (declaration != NULL) {
4155 type = allocate_type_zero(TYPE_FUNCTION);
4157 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
4159 if (mask & (mask-1)) {
4160 const char *first = NULL, *second = NULL;
4162 /* more than one calling convention set */
4163 if (declaration->modifiers & DM_CDECL) {
4164 if (first == NULL) first = "cdecl";
4165 else if (second == NULL) second = "cdecl";
4167 if (declaration->modifiers & DM_STDCALL) {
4168 if (first == NULL) first = "stdcall";
4169 else if (second == NULL) second = "stdcall";
4171 if (declaration->modifiers & DM_FASTCALL) {
4172 if (first == NULL) first = "fastcall";
4173 else if (second == NULL) second = "fastcall";
4175 if (declaration->modifiers & DM_THISCALL) {
4176 if (first == NULL) first = "thiscall";
4177 else if (second == NULL) second = "thiscall";
4179 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
4182 if (declaration->modifiers & DM_CDECL)
4183 type->function.calling_convention = CC_CDECL;
4184 else if (declaration->modifiers & DM_STDCALL)
4185 type->function.calling_convention = CC_STDCALL;
4186 else if (declaration->modifiers & DM_FASTCALL)
4187 type->function.calling_convention = CC_FASTCALL;
4188 else if (declaration->modifiers & DM_THISCALL)
4189 type->function.calling_convention = CC_THISCALL;
4191 type = allocate_type_zero(TYPE_FUNCTION);
4194 declaration_t *last;
4195 declaration_t *parameters = parse_parameters(&type->function, &last);
4196 if (declaration != NULL) {
4197 declaration->scope.declarations = parameters;
4198 declaration->scope.last_declaration = last;
4201 construct_function_type_t *construct_function_type =
4202 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
4203 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
4204 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
4205 construct_function_type->function_type = type;
4207 return &construct_function_type->construct_type;
4210 static void fix_declaration_type(declaration_t *declaration)
4212 decl_modifiers_t declaration_modifiers = declaration->modifiers;
4213 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
4215 if (declaration_modifiers & DM_TRANSPARENT_UNION)
4216 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4218 if (declaration->type->base.modifiers == type_modifiers)
4221 type_t *copy = duplicate_type(declaration->type);
4222 copy->base.modifiers = type_modifiers;
4224 type_t *result = typehash_insert(copy);
4225 if (result != copy) {
4226 obstack_free(type_obst, copy);
4229 declaration->type = result;
4232 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
4233 bool may_be_abstract)
4235 /* construct a single linked list of construct_type_t's which describe
4236 * how to construct the final declarator type */
4237 construct_type_t *first = NULL;
4238 construct_type_t *last = NULL;
4239 gnu_attribute_t *attributes = NULL;
4241 decl_modifiers_t modifiers = parse_attributes(&attributes);
4244 while (token.type == '*') {
4245 construct_type_t *type = parse_pointer_declarator();
4255 /* TODO: find out if this is correct */
4256 modifiers |= parse_attributes(&attributes);
4259 if (declaration != NULL)
4260 declaration->modifiers |= modifiers;
4262 construct_type_t *inner_types = NULL;
4264 switch(token.type) {
4266 if (declaration == NULL) {
4267 errorf(HERE, "no identifier expected in typename");
4269 declaration->symbol = token.v.symbol;
4270 declaration->source_position = token.source_position;
4276 add_anchor_token(')');
4277 inner_types = parse_inner_declarator(declaration, may_be_abstract);
4278 if (inner_types != NULL) {
4279 /* All later declarators only modify the return type, not declaration */
4282 rem_anchor_token(')');
4286 if (may_be_abstract)
4288 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
4293 construct_type_t *p = last;
4296 construct_type_t *type;
4297 switch(token.type) {
4299 type = parse_function_declarator(declaration);
4302 type = parse_array_declarator();
4305 goto declarator_finished;
4308 /* insert in the middle of the list (behind p) */
4310 type->next = p->next;
4321 declarator_finished:
4322 /* append inner_types at the end of the list, we don't to set last anymore
4323 * as it's not needed anymore */
4325 assert(first == NULL);
4326 first = inner_types;
4328 last->next = inner_types;
4336 static void parse_declaration_attributes(declaration_t *declaration)
4338 gnu_attribute_t *attributes = NULL;
4339 decl_modifiers_t modifiers = parse_attributes(&attributes);
4341 if (declaration == NULL)
4344 declaration->modifiers |= modifiers;
4345 /* check if we have these stupid mode attributes... */
4346 type_t *old_type = declaration->type;
4347 if (old_type == NULL)
4350 gnu_attribute_t *attribute = attributes;
4351 for ( ; attribute != NULL; attribute = attribute->next) {
4352 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
4355 atomic_type_kind_t akind = attribute->u.akind;
4356 if (!is_type_signed(old_type)) {
4358 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
4359 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
4360 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
4361 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
4363 panic("invalid akind in mode attribute");
4367 = make_atomic_type(akind, old_type->base.qualifiers);
4371 static type_t *construct_declarator_type(construct_type_t *construct_list,
4374 construct_type_t *iter = construct_list;
4375 for( ; iter != NULL; iter = iter->next) {
4376 switch(iter->kind) {
4377 case CONSTRUCT_INVALID:
4378 internal_errorf(HERE, "invalid type construction found");
4379 case CONSTRUCT_FUNCTION: {
4380 construct_function_type_t *construct_function_type
4381 = (construct_function_type_t*) iter;
4383 type_t *function_type = construct_function_type->function_type;
4385 function_type->function.return_type = type;
4387 type_t *skipped_return_type = skip_typeref(type);
4389 if (is_type_function(skipped_return_type)) {
4390 errorf(HERE, "function returning function is not allowed");
4391 } else if (is_type_array(skipped_return_type)) {
4392 errorf(HERE, "function returning array is not allowed");
4394 if (skipped_return_type->base.qualifiers != 0 && warning.other) {
4396 "type qualifiers in return type of function type are meaningless");
4400 type = function_type;
4404 case CONSTRUCT_POINTER: {
4405 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4406 type = make_pointer_type(type, parsed_pointer->type_qualifiers);
4410 case CONSTRUCT_ARRAY: {
4411 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4412 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
4414 expression_t *size_expression = parsed_array->size;
4415 if (size_expression != NULL) {
4417 = create_implicit_cast(size_expression, type_size_t);
4420 array_type->base.qualifiers = parsed_array->type_qualifiers;
4421 array_type->array.element_type = type;
4422 array_type->array.is_static = parsed_array->is_static;
4423 array_type->array.is_variable = parsed_array->is_variable;
4424 array_type->array.size_expression = size_expression;
4426 if (size_expression != NULL) {
4427 if (is_constant_expression(size_expression)) {
4428 array_type->array.size_constant = true;
4429 array_type->array.size
4430 = fold_constant(size_expression);
4432 array_type->array.is_vla = true;
4436 type_t *skipped_type = skip_typeref(type);
4438 if (is_type_incomplete(skipped_type)) {
4439 errorf(HERE, "array of incomplete type '%T' is not allowed", type);
4440 } else if (is_type_function(skipped_type)) {
4441 errorf(HERE, "array of functions is not allowed");
4448 type_t *hashed_type = typehash_insert(type);
4449 if (hashed_type != type) {
4450 /* the function type was constructed earlier freeing it here will
4451 * destroy other types... */
4452 if (iter->kind != CONSTRUCT_FUNCTION) {
4462 static declaration_t *parse_declarator(
4463 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4465 declaration_t *const declaration = allocate_declaration_zero();
4466 declaration->source_position = specifiers->source_position;
4467 declaration->declared_storage_class = specifiers->declared_storage_class;
4468 declaration->modifiers = specifiers->modifiers;
4469 declaration->deprecated_string = specifiers->deprecated_string;
4470 declaration->get_property_sym = specifiers->get_property_sym;
4471 declaration->put_property_sym = specifiers->put_property_sym;
4472 declaration->is_inline = specifiers->is_inline;
4474 declaration->storage_class = specifiers->declared_storage_class;
4475 if (declaration->storage_class == STORAGE_CLASS_NONE &&
4476 scope != file_scope) {
4477 declaration->storage_class = STORAGE_CLASS_AUTO;
4480 if (specifiers->alignment != 0) {
4481 /* TODO: add checks here */
4482 declaration->alignment = specifiers->alignment;
4485 construct_type_t *construct_type
4486 = parse_inner_declarator(declaration, may_be_abstract);
4487 type_t *const type = specifiers->type;
4488 declaration->type = construct_declarator_type(construct_type, type);
4490 parse_declaration_attributes(declaration);
4492 fix_declaration_type(declaration);
4494 if (construct_type != NULL) {
4495 obstack_free(&temp_obst, construct_type);
4501 static type_t *parse_abstract_declarator(type_t *base_type)
4503 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4505 type_t *result = construct_declarator_type(construct_type, base_type);
4506 if (construct_type != NULL) {
4507 obstack_free(&temp_obst, construct_type);
4513 static declaration_t *append_declaration(declaration_t* const declaration)
4515 if (last_declaration != NULL) {
4516 last_declaration->next = declaration;
4518 scope->declarations = declaration;
4520 last_declaration = declaration;
4525 * Check if the declaration of main is suspicious. main should be a
4526 * function with external linkage, returning int, taking either zero
4527 * arguments, two, or three arguments of appropriate types, ie.
4529 * int main([ int argc, char **argv [, char **env ] ]).
4531 * @param decl the declaration to check
4532 * @param type the function type of the declaration
4534 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4536 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4537 warningf(&decl->source_position,
4538 "'main' is normally a non-static function");
4540 if (!types_compatible(skip_typeref(func_type->return_type), type_int)) {
4541 warningf(&decl->source_position,
4542 "return type of 'main' should be 'int', but is '%T'",
4543 func_type->return_type);
4545 const function_parameter_t *parm = func_type->parameters;
4547 type_t *const first_type = parm->type;
4548 if (!types_compatible(skip_typeref(first_type), type_int)) {
4549 warningf(&decl->source_position,
4550 "first argument of 'main' should be 'int', but is '%T'", first_type);
4554 type_t *const second_type = parm->type;
4555 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4556 warningf(&decl->source_position,
4557 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4561 type_t *const third_type = parm->type;
4562 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4563 warningf(&decl->source_position,
4564 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4568 goto warn_arg_count;
4572 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4578 * Check if a symbol is the equal to "main".
4580 static bool is_sym_main(const symbol_t *const sym)
4582 return strcmp(sym->string, "main") == 0;
4585 static declaration_t *record_declaration(
4586 declaration_t *const declaration,
4587 const bool is_definition)
4589 const symbol_t *const symbol = declaration->symbol;
4590 const namespace_t namespc = (namespace_t)declaration->namespc;
4592 assert(symbol != NULL);
4593 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4595 type_t *const orig_type = declaration->type;
4596 type_t *const type = skip_typeref(orig_type);
4597 if (is_type_function(type) &&
4598 type->function.unspecified_parameters &&
4599 warning.strict_prototypes &&
4600 previous_declaration == NULL) {
4601 warningf(&declaration->source_position,
4602 "function declaration '%#T' is not a prototype",
4606 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4607 check_type_of_main(declaration, &type->function);
4610 if (warning.nested_externs &&
4611 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4612 scope != file_scope) {
4613 warningf(&declaration->source_position,
4614 "nested extern declaration of '%#T'", declaration->type, symbol);
4617 assert(declaration != previous_declaration);
4618 if (previous_declaration != NULL &&
4619 previous_declaration->parent_scope == ¤t_function->scope &&
4620 scope->depth == previous_declaration->parent_scope->depth + 1) {
4621 errorf(&declaration->source_position,
4622 "declaration '%#T' redeclares the parameter '%#T' (declared %P)",
4623 orig_type, symbol, previous_declaration->type, symbol,
4624 &previous_declaration->source_position);
4627 if (previous_declaration != NULL &&
4628 previous_declaration->parent_scope == scope) {
4629 /* can happen for K&R style declarations */
4630 if (previous_declaration->type == NULL) {
4631 previous_declaration->type = declaration->type;
4634 const type_t *prev_type = skip_typeref(previous_declaration->type);
4635 if (!types_compatible(type, prev_type)) {
4636 errorf(&declaration->source_position,
4637 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4638 orig_type, symbol, previous_declaration->type, symbol,
4639 &previous_declaration->source_position);
4641 unsigned old_storage_class = previous_declaration->storage_class;
4642 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4643 errorf(&declaration->source_position,
4644 "redeclaration of enum entry '%Y' (declared %P)",
4645 symbol, &previous_declaration->source_position);
4646 return previous_declaration;
4649 if (warning.redundant_decls &&
4651 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4652 !(previous_declaration->modifiers & DM_USED) &&
4653 !previous_declaration->used) {
4654 warningf(&previous_declaration->source_position,
4655 "unnecessary static forward declaration for '%#T'",
4656 previous_declaration->type, symbol);
4659 unsigned new_storage_class = declaration->storage_class;
4661 if (is_type_incomplete(prev_type)) {
4662 previous_declaration->type = type;
4666 /* pretend no storage class means extern for function
4667 * declarations (except if the previous declaration is neither
4668 * none nor extern) */
4669 if (is_type_function(type)) {
4670 if (prev_type->function.unspecified_parameters) {
4671 previous_declaration->type = type;
4675 switch (old_storage_class) {
4676 case STORAGE_CLASS_NONE:
4677 old_storage_class = STORAGE_CLASS_EXTERN;
4680 case STORAGE_CLASS_EXTERN:
4681 if (is_definition) {
4682 if (warning.missing_prototypes &&
4683 prev_type->function.unspecified_parameters &&
4684 !is_sym_main(symbol)) {
4685 warningf(&declaration->source_position,
4686 "no previous prototype for '%#T'",
4689 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4690 new_storage_class = STORAGE_CLASS_EXTERN;
4699 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4700 new_storage_class == STORAGE_CLASS_EXTERN) {
4701 warn_redundant_declaration:
4702 if (!is_definition &&
4703 warning.redundant_decls &&
4704 is_type_valid(prev_type) &&
4705 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4706 warningf(&declaration->source_position,
4707 "redundant declaration for '%Y' (declared %P)",
4708 symbol, &previous_declaration->source_position);
4710 } else if (current_function == NULL) {
4711 if (old_storage_class != STORAGE_CLASS_STATIC &&
4712 new_storage_class == STORAGE_CLASS_STATIC) {
4713 errorf(&declaration->source_position,
4714 "static declaration of '%Y' follows non-static declaration (declared %P)",
4715 symbol, &previous_declaration->source_position);
4716 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4717 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4718 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4720 goto warn_redundant_declaration;
4722 } else if (is_type_valid(prev_type)) {
4723 if (old_storage_class == new_storage_class) {
4724 errorf(&declaration->source_position,
4725 "redeclaration of '%Y' (declared %P)",
4726 symbol, &previous_declaration->source_position);
4728 errorf(&declaration->source_position,
4729 "redeclaration of '%Y' with different linkage (declared %P)",
4730 symbol, &previous_declaration->source_position);
4735 previous_declaration->modifiers |= declaration->modifiers;
4736 previous_declaration->is_inline |= declaration->is_inline;
4737 return previous_declaration;
4738 } else if (is_type_function(type)) {
4739 if (is_definition &&
4740 declaration->storage_class != STORAGE_CLASS_STATIC) {
4741 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4742 warningf(&declaration->source_position,
4743 "no previous prototype for '%#T'", orig_type, symbol);
4744 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4745 warningf(&declaration->source_position,
4746 "no previous declaration for '%#T'", orig_type,
4751 if (warning.missing_declarations &&
4752 scope == file_scope && (
4753 declaration->storage_class == STORAGE_CLASS_NONE ||
4754 declaration->storage_class == STORAGE_CLASS_THREAD
4756 warningf(&declaration->source_position,
4757 "no previous declaration for '%#T'", orig_type, symbol);
4761 assert(declaration->parent_scope == NULL);
4762 assert(scope != NULL);
4764 declaration->parent_scope = scope;
4766 environment_push(declaration);
4767 return append_declaration(declaration);
4770 static void parser_error_multiple_definition(declaration_t *declaration,
4771 const source_position_t *source_position)
4773 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4774 declaration->symbol, &declaration->source_position);
4777 static bool is_declaration_specifier(const token_t *token,
4778 bool only_specifiers_qualifiers)
4780 switch (token->type) {
4785 return is_typedef_symbol(token->v.symbol);
4787 case T___extension__:
4789 return !only_specifiers_qualifiers;
4796 static void parse_init_declarator_rest(declaration_t *declaration)
4800 type_t *orig_type = declaration->type;
4801 type_t *type = skip_typeref(orig_type);
4803 if (declaration->init.initializer != NULL) {
4804 parser_error_multiple_definition(declaration, HERE);
4807 bool must_be_constant = false;
4808 if (declaration->storage_class == STORAGE_CLASS_STATIC ||
4809 declaration->storage_class == STORAGE_CLASS_THREAD_STATIC ||
4810 declaration->parent_scope == file_scope) {
4811 must_be_constant = true;
4814 if (is_type_function(type)) {
4815 errorf(&declaration->source_position,
4816 "function '%#T' is initialized like a variable",
4817 orig_type, declaration->symbol);
4818 orig_type = type_error_type;
4821 parse_initializer_env_t env;
4822 env.type = orig_type;
4823 env.must_be_constant = must_be_constant;
4824 env.declaration = current_init_decl = declaration;
4826 initializer_t *initializer = parse_initializer(&env);
4827 current_init_decl = NULL;
4829 if (!is_type_function(type)) {
4830 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
4831 * the array type size */
4832 declaration->type = env.type;
4833 declaration->init.initializer = initializer;
4837 /* parse rest of a declaration without any declarator */
4838 static void parse_anonymous_declaration_rest(
4839 const declaration_specifiers_t *specifiers)
4843 if (warning.other) {
4844 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) {
4845 warningf(&specifiers->source_position,
4846 "useless storage class in empty declaration");
4849 type_t *type = specifiers->type;
4850 switch (type->kind) {
4851 case TYPE_COMPOUND_STRUCT:
4852 case TYPE_COMPOUND_UNION: {
4853 if (type->compound.declaration->symbol == NULL) {
4854 warningf(&specifiers->source_position,
4855 "unnamed struct/union that defines no instances");
4864 warningf(&specifiers->source_position, "empty declaration");
4869 #ifdef RECORD_EMPTY_DECLARATIONS
4870 declaration_t *const declaration = allocate_declaration_zero();
4871 declaration->type = specifiers->type;
4872 declaration->declared_storage_class = specifiers->declared_storage_class;
4873 declaration->source_position = specifiers->source_position;
4874 declaration->modifiers = specifiers->modifiers;
4875 declaration->storage_class = STORAGE_CLASS_NONE;
4877 append_declaration(declaration);
4881 static void parse_declaration_rest(declaration_t *ndeclaration,
4882 const declaration_specifiers_t *specifiers,
4883 parsed_declaration_func finished_declaration)
4885 add_anchor_token(';');
4886 add_anchor_token(',');
4888 declaration_t *declaration =
4889 finished_declaration(ndeclaration, token.type == '=');
4891 type_t *orig_type = declaration->type;
4892 type_t *type = skip_typeref(orig_type);
4894 if (warning.other &&
4895 type->kind != TYPE_FUNCTION &&
4896 declaration->is_inline &&
4897 is_type_valid(type)) {
4898 warningf(&declaration->source_position,
4899 "variable '%Y' declared 'inline'\n", declaration->symbol);
4902 if (token.type == '=') {
4903 parse_init_declarator_rest(declaration);
4906 if (token.type != ',')
4910 add_anchor_token('=');
4911 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4912 rem_anchor_token('=');
4917 rem_anchor_token(';');
4918 rem_anchor_token(',');
4921 static declaration_t *finished_kr_declaration(declaration_t *declaration, bool is_definition)
4923 symbol_t *symbol = declaration->symbol;
4924 if (symbol == NULL) {
4925 errorf(HERE, "anonymous declaration not valid as function parameter");
4928 namespace_t namespc = (namespace_t) declaration->namespc;
4929 if (namespc != NAMESPACE_NORMAL) {
4930 return record_declaration(declaration, false);
4933 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4934 if (previous_declaration == NULL ||
4935 previous_declaration->parent_scope != scope) {
4936 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4941 if (is_definition) {
4942 errorf(HERE, "parameter %Y is initialised", declaration->symbol);
4945 if (previous_declaration->type == NULL) {
4946 previous_declaration->type = declaration->type;
4947 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4948 previous_declaration->storage_class = declaration->storage_class;
4949 previous_declaration->parent_scope = scope;
4950 return previous_declaration;
4952 return record_declaration(declaration, false);
4956 static void parse_declaration(parsed_declaration_func finished_declaration)
4958 declaration_specifiers_t specifiers;
4959 memset(&specifiers, 0, sizeof(specifiers));
4961 add_anchor_token(';');
4962 parse_declaration_specifiers(&specifiers);
4963 rem_anchor_token(';');
4965 if (token.type == ';') {
4966 parse_anonymous_declaration_rest(&specifiers);
4968 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4969 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4973 static type_t *get_default_promoted_type(type_t *orig_type)
4975 type_t *result = orig_type;
4977 type_t *type = skip_typeref(orig_type);
4978 if (is_type_integer(type)) {
4979 result = promote_integer(type);
4980 } else if (type == type_float) {
4981 result = type_double;
4987 static void parse_kr_declaration_list(declaration_t *declaration)
4989 type_t *type = skip_typeref(declaration->type);
4990 if (!is_type_function(type))
4993 if (!type->function.kr_style_parameters)
4996 add_anchor_token('{');
4998 /* push function parameters */
4999 size_t const top = environment_top();
5000 scope_push(&declaration->scope);
5002 declaration_t *parameter = declaration->scope.declarations;
5003 for ( ; parameter != NULL; parameter = parameter->next) {
5004 assert(parameter->parent_scope == NULL);
5005 parameter->parent_scope = scope;
5006 environment_push(parameter);
5009 /* parse declaration list */
5010 while (is_declaration_specifier(&token, false)) {
5011 parse_declaration(finished_kr_declaration);
5014 /* pop function parameters */
5015 assert(scope == &declaration->scope);
5017 environment_pop_to(top);
5019 /* update function type */
5020 type_t *new_type = duplicate_type(type);
5022 function_parameter_t *parameters = NULL;
5023 function_parameter_t *last_parameter = NULL;
5025 declaration_t *parameter_declaration = declaration->scope.declarations;
5026 for( ; parameter_declaration != NULL;
5027 parameter_declaration = parameter_declaration->next) {
5028 type_t *parameter_type = parameter_declaration->type;
5029 if (parameter_type == NULL) {
5031 errorf(HERE, "no type specified for function parameter '%Y'",
5032 parameter_declaration->symbol);
5034 if (warning.implicit_int) {
5035 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
5036 parameter_declaration->symbol);
5038 parameter_type = type_int;
5039 parameter_declaration->type = parameter_type;
5043 semantic_parameter(parameter_declaration);
5044 parameter_type = parameter_declaration->type;
5047 * we need the default promoted types for the function type
5049 parameter_type = get_default_promoted_type(parameter_type);
5051 function_parameter_t *function_parameter
5052 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
5053 memset(function_parameter, 0, sizeof(function_parameter[0]));
5055 function_parameter->type = parameter_type;
5056 if (last_parameter != NULL) {
5057 last_parameter->next = function_parameter;
5059 parameters = function_parameter;
5061 last_parameter = function_parameter;
5064 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
5066 new_type->function.parameters = parameters;
5067 new_type->function.unspecified_parameters = true;
5069 type = typehash_insert(new_type);
5070 if (type != new_type) {
5071 obstack_free(type_obst, new_type);
5074 declaration->type = type;
5076 rem_anchor_token('{');
5079 static bool first_err = true;
5082 * When called with first_err set, prints the name of the current function,
5085 static void print_in_function(void)
5089 diagnosticf("%s: In function '%Y':\n",
5090 current_function->source_position.input_name,
5091 current_function->symbol);
5096 * Check if all labels are defined in the current function.
5097 * Check if all labels are used in the current function.
5099 static void check_labels(void)
5101 for (const goto_statement_t *goto_statement = goto_first;
5102 goto_statement != NULL;
5103 goto_statement = goto_statement->next) {
5104 /* skip computed gotos */
5105 if (goto_statement->expression != NULL)
5108 declaration_t *label = goto_statement->label;
5111 if (label->source_position.input_name == NULL) {
5112 print_in_function();
5113 errorf(&goto_statement->base.source_position,
5114 "label '%Y' used but not defined", label->symbol);
5117 goto_first = goto_last = NULL;
5119 if (warning.unused_label) {
5120 for (const label_statement_t *label_statement = label_first;
5121 label_statement != NULL;
5122 label_statement = label_statement->next) {
5123 const declaration_t *label = label_statement->label;
5125 if (! label->used) {
5126 print_in_function();
5127 warningf(&label_statement->base.source_position,
5128 "label '%Y' defined but not used", label->symbol);
5132 label_first = label_last = NULL;
5135 static void warn_unused_decl(declaration_t *decl, declaration_t *end, char const *const what)
5137 for (; decl != NULL; decl = decl->next) {
5142 print_in_function();
5143 warningf(&decl->source_position, "%s '%Y' is unused", what, decl->symbol);
5144 } else if (!decl->read) {
5145 print_in_function();
5146 warningf(&decl->source_position, "%s '%Y' is never read", what, decl->symbol);
5154 static void check_unused_variables(statement_t *const stmt, void *const env)
5158 switch (stmt->kind) {
5159 case STATEMENT_DECLARATION: {
5160 declaration_statement_t const *const decls = &stmt->declaration;
5161 warn_unused_decl(decls->declarations_begin, decls->declarations_end, "variable");
5166 warn_unused_decl(stmt->fors.scope.declarations, NULL, "variable");
5175 * Check declarations of current_function for unused entities.
5177 static void check_declarations(void)
5179 if (warning.unused_parameter) {
5180 const scope_t *scope = ¤t_function->scope;
5182 /* do not issue unused warnings for main */
5183 if (!is_sym_main(current_function->symbol)) {
5184 warn_unused_decl(scope->declarations, NULL, "parameter");
5187 if (warning.unused_variable) {
5188 walk_statements(current_function->init.statement, check_unused_variables, NULL);
5192 static int determine_truth(expression_t const* const cond)
5195 !is_constant_expression(cond) ? 0 :
5196 fold_constant(cond) != 0 ? 1 :
5200 static bool noreturn_candidate;
5202 static void check_reachable(statement_t *const stmt)
5204 if (stmt->base.reachable)
5206 if (stmt->kind != STATEMENT_DO_WHILE)
5207 stmt->base.reachable = true;
5209 statement_t *last = stmt;
5211 switch (stmt->kind) {
5212 case STATEMENT_INVALID:
5213 case STATEMENT_EMPTY:
5214 case STATEMENT_DECLARATION:
5216 next = stmt->base.next;
5219 case STATEMENT_COMPOUND:
5220 next = stmt->compound.statements;
5223 case STATEMENT_RETURN:
5224 noreturn_candidate = false;
5227 case STATEMENT_IF: {
5228 if_statement_t const* const ifs = &stmt->ifs;
5229 int const val = determine_truth(ifs->condition);
5232 check_reachable(ifs->true_statement);
5237 if (ifs->false_statement != NULL) {
5238 check_reachable(ifs->false_statement);
5242 next = stmt->base.next;
5246 case STATEMENT_SWITCH: {
5247 switch_statement_t const *const switchs = &stmt->switchs;
5248 expression_t const *const expr = switchs->expression;
5250 if (is_constant_expression(expr)) {
5251 long const val = fold_constant(expr);
5252 case_label_statement_t * defaults = NULL;
5253 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5254 if (i->expression == NULL) {
5259 if (i->first_case <= val && val <= i->last_case) {
5260 check_reachable((statement_t*)i);
5265 if (defaults != NULL) {
5266 check_reachable((statement_t*)defaults);
5270 bool has_default = false;
5271 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5272 if (i->expression == NULL)
5275 check_reachable((statement_t*)i);
5282 next = stmt->base.next;
5286 case STATEMENT_EXPRESSION: {
5287 /* Check for noreturn function call */
5288 expression_t const *const expr = stmt->expression.expression;
5289 if (expr->kind == EXPR_CALL) {
5290 expression_t const *const func = expr->call.function;
5291 if (func->kind == EXPR_REFERENCE) {
5292 declaration_t const *const decl = func->reference.declaration;
5293 if (decl != NULL && decl->modifiers & DM_NORETURN) {
5299 next = stmt->base.next;
5303 case STATEMENT_CONTINUE: {
5304 statement_t *parent = stmt;
5306 parent = parent->base.parent;
5307 if (parent == NULL) /* continue not within loop */
5311 switch (parent->kind) {
5312 case STATEMENT_WHILE: goto continue_while;
5313 case STATEMENT_DO_WHILE: goto continue_do_while;
5314 case STATEMENT_FOR: goto continue_for;
5321 case STATEMENT_BREAK: {
5322 statement_t *parent = stmt;
5324 parent = parent->base.parent;
5325 if (parent == NULL) /* break not within loop/switch */
5328 switch (parent->kind) {
5329 case STATEMENT_SWITCH:
5330 case STATEMENT_WHILE:
5331 case STATEMENT_DO_WHILE:
5334 next = parent->base.next;
5335 goto found_break_parent;
5344 case STATEMENT_GOTO:
5345 if (stmt->gotos.expression) {
5346 statement_t *parent = stmt->base.parent;
5347 if (parent == NULL) /* top level goto */
5351 next = stmt->gotos.label->init.statement;
5352 if (next == NULL) /* missing label */
5357 case STATEMENT_LABEL:
5358 next = stmt->label.statement;
5361 case STATEMENT_CASE_LABEL:
5362 next = stmt->case_label.statement;
5365 case STATEMENT_WHILE: {
5366 while_statement_t const *const whiles = &stmt->whiles;
5367 int const val = determine_truth(whiles->condition);
5370 check_reachable(whiles->body);
5375 next = stmt->base.next;
5379 case STATEMENT_DO_WHILE:
5380 next = stmt->do_while.body;
5383 case STATEMENT_FOR: {
5384 for_statement_t *const fors = &stmt->fors;
5386 if (fors->condition_reachable)
5388 fors->condition_reachable = true;
5390 expression_t const *const cond = fors->condition;
5392 cond == NULL ? 1 : determine_truth(cond);
5395 check_reachable(fors->body);
5400 next = stmt->base.next;
5404 case STATEMENT_MS_TRY: {
5405 ms_try_statement_t const *const ms_try = &stmt->ms_try;
5406 check_reachable(ms_try->try_statement);
5407 next = ms_try->final_statement;
5411 case STATEMENT_LEAVE: {
5412 statement_t *parent = stmt;
5414 parent = parent->base.parent;
5415 if (parent == NULL) /* __leave not within __try */
5418 if (parent->kind == STATEMENT_MS_TRY) {
5420 next = parent->ms_try.final_statement;
5428 while (next == NULL) {
5429 next = last->base.parent;
5431 noreturn_candidate = false;
5433 type_t *const type = current_function->type;
5434 assert(is_type_function(type));
5435 type_t *const ret = skip_typeref(type->function.return_type);
5436 if (warning.return_type &&
5437 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
5438 is_type_valid(ret) &&
5439 !is_sym_main(current_function->symbol)) {
5440 warningf(&stmt->base.source_position,
5441 "control reaches end of non-void function");
5446 switch (next->kind) {
5447 case STATEMENT_INVALID:
5448 case STATEMENT_EMPTY:
5449 case STATEMENT_DECLARATION:
5450 case STATEMENT_EXPRESSION:
5452 case STATEMENT_RETURN:
5453 case STATEMENT_CONTINUE:
5454 case STATEMENT_BREAK:
5455 case STATEMENT_GOTO:
5456 case STATEMENT_LEAVE:
5457 panic("invalid control flow in function");
5459 case STATEMENT_COMPOUND:
5461 case STATEMENT_SWITCH:
5462 case STATEMENT_LABEL:
5463 case STATEMENT_CASE_LABEL:
5465 next = next->base.next;
5468 case STATEMENT_WHILE: {
5470 if (next->base.reachable)
5472 next->base.reachable = true;
5474 while_statement_t const *const whiles = &next->whiles;
5475 int const val = determine_truth(whiles->condition);
5478 check_reachable(whiles->body);
5484 next = next->base.next;
5488 case STATEMENT_DO_WHILE: {
5490 if (next->base.reachable)
5492 next->base.reachable = true;
5494 do_while_statement_t const *const dw = &next->do_while;
5495 int const val = determine_truth(dw->condition);
5498 check_reachable(dw->body);
5504 next = next->base.next;
5508 case STATEMENT_FOR: {
5510 for_statement_t *const fors = &next->fors;
5512 fors->step_reachable = true;
5514 if (fors->condition_reachable)
5516 fors->condition_reachable = true;
5518 expression_t const *const cond = fors->condition;
5520 cond == NULL ? 1 : determine_truth(cond);
5523 check_reachable(fors->body);
5529 next = next->base.next;
5533 case STATEMENT_MS_TRY:
5535 next = next->ms_try.final_statement;
5540 check_reachable(next);
5543 static void check_unreachable(statement_t* const stmt, void *const env)
5547 switch (stmt->kind) {
5548 case STATEMENT_DO_WHILE:
5549 if (!stmt->base.reachable) {
5550 expression_t const *const cond = stmt->do_while.condition;
5551 if (determine_truth(cond) >= 0) {
5552 warningf(&cond->base.source_position,
5553 "condition of do-while-loop is unreachable");
5558 case STATEMENT_FOR: {
5559 for_statement_t const* const fors = &stmt->fors;
5561 // if init and step are unreachable, cond is unreachable, too
5562 if (!stmt->base.reachable && !fors->step_reachable) {
5563 warningf(&stmt->base.source_position, "statement is unreachable");
5565 if (!stmt->base.reachable && fors->initialisation != NULL) {
5566 warningf(&fors->initialisation->base.source_position,
5567 "initialisation of for-statement is unreachable");
5570 if (!fors->condition_reachable && fors->condition != NULL) {
5571 warningf(&fors->condition->base.source_position,
5572 "condition of for-statement is unreachable");
5575 if (!fors->step_reachable && fors->step != NULL) {
5576 warningf(&fors->step->base.source_position,
5577 "step of for-statement is unreachable");
5583 case STATEMENT_COMPOUND:
5584 if (stmt->compound.statements != NULL)
5589 if (!stmt->base.reachable)
5590 warningf(&stmt->base.source_position, "statement is unreachable");
5595 static void parse_external_declaration(void)
5597 /* function-definitions and declarations both start with declaration
5599 declaration_specifiers_t specifiers;
5600 memset(&specifiers, 0, sizeof(specifiers));
5602 add_anchor_token(';');
5603 parse_declaration_specifiers(&specifiers);
5604 rem_anchor_token(';');
5606 /* must be a declaration */
5607 if (token.type == ';') {
5608 parse_anonymous_declaration_rest(&specifiers);
5612 add_anchor_token(',');
5613 add_anchor_token('=');
5614 add_anchor_token(';');
5615 add_anchor_token('{');
5617 /* declarator is common to both function-definitions and declarations */
5618 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5620 rem_anchor_token('{');
5621 rem_anchor_token(';');
5622 rem_anchor_token('=');
5623 rem_anchor_token(',');
5625 /* must be a declaration */
5626 switch (token.type) {
5630 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5634 /* must be a function definition */
5635 parse_kr_declaration_list(ndeclaration);
5637 if (token.type != '{') {
5638 parse_error_expected("while parsing function definition", '{', NULL);
5639 eat_until_matching_token(';');
5643 type_t *type = ndeclaration->type;
5645 /* note that we don't skip typerefs: the standard doesn't allow them here
5646 * (so we can't use is_type_function here) */
5647 if (type->kind != TYPE_FUNCTION) {
5648 if (is_type_valid(type)) {
5649 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5650 type, ndeclaration->symbol);
5656 if (warning.aggregate_return &&
5657 is_type_compound(skip_typeref(type->function.return_type))) {
5658 warningf(HERE, "function '%Y' returns an aggregate",
5659 ndeclaration->symbol);
5661 if (warning.traditional && !type->function.unspecified_parameters) {
5662 warningf(HERE, "traditional C rejects ISO C style function definition of function '%Y'",
5663 ndeclaration->symbol);
5665 if (warning.old_style_definition && type->function.unspecified_parameters) {
5666 warningf(HERE, "old-style function definition '%Y'",
5667 ndeclaration->symbol);
5670 /* § 6.7.5.3 (14) a function definition with () means no
5671 * parameters (and not unspecified parameters) */
5672 if (type->function.unspecified_parameters
5673 && type->function.parameters == NULL
5674 && !type->function.kr_style_parameters) {
5675 type_t *duplicate = duplicate_type(type);
5676 duplicate->function.unspecified_parameters = false;
5678 type = typehash_insert(duplicate);
5679 if (type != duplicate) {
5680 obstack_free(type_obst, duplicate);
5682 ndeclaration->type = type;
5685 declaration_t *const declaration = record_declaration(ndeclaration, true);
5686 if (ndeclaration != declaration) {
5687 declaration->scope = ndeclaration->scope;
5689 type = skip_typeref(declaration->type);
5691 /* push function parameters and switch scope */
5692 size_t const top = environment_top();
5693 scope_push(&declaration->scope);
5695 declaration_t *parameter = declaration->scope.declarations;
5696 for( ; parameter != NULL; parameter = parameter->next) {
5697 if (parameter->parent_scope == &ndeclaration->scope) {
5698 parameter->parent_scope = scope;
5700 assert(parameter->parent_scope == NULL
5701 || parameter->parent_scope == scope);
5702 parameter->parent_scope = scope;
5703 if (parameter->symbol == NULL) {
5704 errorf(¶meter->source_position, "parameter name omitted");
5707 environment_push(parameter);
5710 if (declaration->init.statement != NULL) {
5711 parser_error_multiple_definition(declaration, HERE);
5714 /* parse function body */
5715 int label_stack_top = label_top();
5716 declaration_t *old_current_function = current_function;
5717 current_function = declaration;
5718 current_parent = NULL;
5720 statement_t *const body = parse_compound_statement(false);
5721 declaration->init.statement = body;
5724 check_declarations();
5725 if (warning.return_type ||
5726 warning.unreachable_code ||
5727 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5728 noreturn_candidate = true;
5729 check_reachable(body);
5730 if (warning.unreachable_code)
5731 walk_statements(body, check_unreachable, NULL);
5732 if (warning.missing_noreturn &&
5733 noreturn_candidate &&
5734 !(declaration->modifiers & DM_NORETURN)) {
5735 warningf(&body->base.source_position,
5736 "function '%#T' is candidate for attribute 'noreturn'",
5737 type, declaration->symbol);
5741 assert(current_parent == NULL);
5742 assert(current_function == declaration);
5743 current_function = old_current_function;
5744 label_pop_to(label_stack_top);
5747 assert(scope == &declaration->scope);
5749 environment_pop_to(top);
5752 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5753 source_position_t *source_position,
5754 const symbol_t *symbol)
5756 type_t *type = allocate_type_zero(TYPE_BITFIELD);
5758 type->bitfield.base_type = base_type;
5759 type->bitfield.size_expression = size;
5762 type_t *skipped_type = skip_typeref(base_type);
5763 if (!is_type_integer(skipped_type)) {
5764 errorf(HERE, "bitfield base type '%T' is not an integer type",
5768 bit_size = skipped_type->base.size * 8;
5771 if (is_constant_expression(size)) {
5772 long v = fold_constant(size);
5775 errorf(source_position, "negative width in bit-field '%Y'",
5777 } else if (v == 0) {
5778 errorf(source_position, "zero width for bit-field '%Y'",
5780 } else if (bit_size > 0 && (il_size_t)v > bit_size) {
5781 errorf(source_position, "width of '%Y' exceeds its type",
5784 type->bitfield.bit_size = v;
5791 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5794 declaration_t *iter = compound_declaration->scope.declarations;
5795 for( ; iter != NULL; iter = iter->next) {
5796 if (iter->namespc != NAMESPACE_NORMAL)
5799 if (iter->symbol == NULL) {
5800 type_t *type = skip_typeref(iter->type);
5801 if (is_type_compound(type)) {
5802 declaration_t *result
5803 = find_compound_entry(type->compound.declaration, symbol);
5810 if (iter->symbol == symbol) {
5818 static void parse_compound_declarators(declaration_t *struct_declaration,
5819 const declaration_specifiers_t *specifiers)
5821 declaration_t *last_declaration = struct_declaration->scope.declarations;
5822 if (last_declaration != NULL) {
5823 while (last_declaration->next != NULL) {
5824 last_declaration = last_declaration->next;
5829 declaration_t *declaration;
5831 if (token.type == ':') {
5832 source_position_t source_position = *HERE;
5835 type_t *base_type = specifiers->type;
5836 expression_t *size = parse_constant_expression();
5838 type_t *type = make_bitfield_type(base_type, size,
5839 &source_position, sym_anonymous);
5841 declaration = allocate_declaration_zero();
5842 declaration->namespc = NAMESPACE_NORMAL;
5843 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5844 declaration->storage_class = STORAGE_CLASS_NONE;
5845 declaration->source_position = source_position;
5846 declaration->modifiers = specifiers->modifiers;
5847 declaration->type = type;
5849 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5851 type_t *orig_type = declaration->type;
5852 type_t *type = skip_typeref(orig_type);
5854 if (token.type == ':') {
5855 source_position_t source_position = *HERE;
5857 expression_t *size = parse_constant_expression();
5859 type_t *bitfield_type = make_bitfield_type(orig_type, size,
5860 &source_position, declaration->symbol);
5861 declaration->type = bitfield_type;
5863 /* TODO we ignore arrays for now... what is missing is a check
5864 * that they're at the end of the struct */
5865 if (is_type_incomplete(type) && !is_type_array(type)) {
5867 "compound member '%Y' has incomplete type '%T'",
5868 declaration->symbol, orig_type);
5869 } else if (is_type_function(type)) {
5870 errorf(HERE, "compound member '%Y' must not have function type '%T'",
5871 declaration->symbol, orig_type);
5876 /* make sure we don't define a symbol multiple times */
5877 symbol_t *symbol = declaration->symbol;
5878 if (symbol != NULL) {
5879 declaration_t *prev_decl
5880 = find_compound_entry(struct_declaration, symbol);
5882 if (prev_decl != NULL) {
5883 assert(prev_decl->symbol == symbol);
5884 errorf(&declaration->source_position,
5885 "multiple declarations of symbol '%Y' (declared %P)",
5886 symbol, &prev_decl->source_position);
5890 /* append declaration */
5891 if (last_declaration != NULL) {
5892 last_declaration->next = declaration;
5894 struct_declaration->scope.declarations = declaration;
5896 last_declaration = declaration;
5898 if (token.type != ',')
5908 static void parse_compound_type_entries(declaration_t *compound_declaration)
5911 add_anchor_token('}');
5913 while (token.type != '}') {
5914 if (token.type == T_EOF) {
5915 errorf(HERE, "EOF while parsing struct");
5918 declaration_specifiers_t specifiers;
5919 memset(&specifiers, 0, sizeof(specifiers));
5920 parse_declaration_specifiers(&specifiers);
5922 parse_compound_declarators(compound_declaration, &specifiers);
5924 rem_anchor_token('}');
5928 static type_t *parse_typename(void)
5930 declaration_specifiers_t specifiers;
5931 memset(&specifiers, 0, sizeof(specifiers));
5932 parse_declaration_specifiers(&specifiers);
5933 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5934 /* TODO: improve error message, user does probably not know what a
5935 * storage class is...
5937 errorf(HERE, "typename may not have a storage class");
5940 type_t *result = parse_abstract_declarator(specifiers.type);
5948 typedef expression_t* (*parse_expression_function)(void);
5949 typedef expression_t* (*parse_expression_infix_function)(expression_t *left);
5951 typedef struct expression_parser_function_t expression_parser_function_t;
5952 struct expression_parser_function_t {
5953 parse_expression_function parser;
5954 unsigned infix_precedence;
5955 parse_expression_infix_function infix_parser;
5958 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5961 * Prints an error message if an expression was expected but not read
5963 static expression_t *expected_expression_error(void)
5965 /* skip the error message if the error token was read */
5966 if (token.type != T_ERROR) {
5967 errorf(HERE, "expected expression, got token '%K'", &token);
5971 return create_invalid_expression();
5975 * Parse a string constant.
5977 static expression_t *parse_string_const(void)
5980 if (token.type == T_STRING_LITERAL) {
5981 string_t res = token.v.string;
5983 while (token.type == T_STRING_LITERAL) {
5984 res = concat_strings(&res, &token.v.string);
5987 if (token.type != T_WIDE_STRING_LITERAL) {
5988 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
5989 /* note: that we use type_char_ptr here, which is already the
5990 * automatic converted type. revert_automatic_type_conversion
5991 * will construct the array type */
5992 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
5993 cnst->string.value = res;
5997 wres = concat_string_wide_string(&res, &token.v.wide_string);
5999 wres = token.v.wide_string;
6004 switch (token.type) {
6005 case T_WIDE_STRING_LITERAL:
6006 wres = concat_wide_strings(&wres, &token.v.wide_string);
6009 case T_STRING_LITERAL:
6010 wres = concat_wide_string_string(&wres, &token.v.string);
6014 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
6015 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
6016 cnst->wide_string.value = wres;
6025 * Parse an integer constant.
6027 static expression_t *parse_int_const(void)
6029 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6030 cnst->base.source_position = *HERE;
6031 cnst->base.type = token.datatype;
6032 cnst->conste.v.int_value = token.v.intvalue;
6040 * Parse a character constant.
6042 static expression_t *parse_character_constant(void)
6044 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
6046 cnst->base.source_position = *HERE;
6047 cnst->base.type = token.datatype;
6048 cnst->conste.v.character = token.v.string;
6050 if (cnst->conste.v.character.size != 1) {
6051 if (warning.multichar && GNU_MODE) {
6052 warningf(HERE, "multi-character character constant");
6054 errorf(HERE, "more than 1 characters in character constant");
6063 * Parse a wide character constant.
6065 static expression_t *parse_wide_character_constant(void)
6067 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
6069 cnst->base.source_position = *HERE;
6070 cnst->base.type = token.datatype;
6071 cnst->conste.v.wide_character = token.v.wide_string;
6073 if (cnst->conste.v.wide_character.size != 1) {
6074 if (warning.multichar && GNU_MODE) {
6075 warningf(HERE, "multi-character character constant");
6077 errorf(HERE, "more than 1 characters in character constant");
6086 * Parse a float constant.
6088 static expression_t *parse_float_const(void)
6090 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6091 cnst->base.type = token.datatype;
6092 cnst->conste.v.float_value = token.v.floatvalue;
6099 static declaration_t *create_implicit_function(symbol_t *symbol,
6100 const source_position_t *source_position)
6102 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
6103 ntype->function.return_type = type_int;
6104 ntype->function.unspecified_parameters = true;
6106 type_t *type = typehash_insert(ntype);
6107 if (type != ntype) {
6111 declaration_t *const declaration = allocate_declaration_zero();
6112 declaration->storage_class = STORAGE_CLASS_EXTERN;
6113 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
6114 declaration->type = type;
6115 declaration->symbol = symbol;
6116 declaration->source_position = *source_position;
6117 declaration->implicit = true;
6119 bool strict_prototypes_old = warning.strict_prototypes;
6120 warning.strict_prototypes = false;
6121 record_declaration(declaration, false);
6122 warning.strict_prototypes = strict_prototypes_old;
6128 * Creates a return_type (func)(argument_type) function type if not
6131 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
6132 type_t *argument_type2)
6134 function_parameter_t *parameter2
6135 = obstack_alloc(type_obst, sizeof(parameter2[0]));
6136 memset(parameter2, 0, sizeof(parameter2[0]));
6137 parameter2->type = argument_type2;
6139 function_parameter_t *parameter1
6140 = obstack_alloc(type_obst, sizeof(parameter1[0]));
6141 memset(parameter1, 0, sizeof(parameter1[0]));
6142 parameter1->type = argument_type1;
6143 parameter1->next = parameter2;
6145 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6146 type->function.return_type = return_type;
6147 type->function.parameters = parameter1;
6149 type_t *result = typehash_insert(type);
6150 if (result != type) {
6158 * Creates a return_type (func)(argument_type) function type if not
6161 * @param return_type the return type
6162 * @param argument_type the argument type
6164 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
6166 function_parameter_t *parameter
6167 = obstack_alloc(type_obst, sizeof(parameter[0]));
6168 memset(parameter, 0, sizeof(parameter[0]));
6169 parameter->type = argument_type;
6171 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6172 type->function.return_type = return_type;
6173 type->function.parameters = parameter;
6175 type_t *result = typehash_insert(type);
6176 if (result != type) {
6183 static type_t *make_function_0_type(type_t *return_type)
6185 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6186 type->function.return_type = return_type;
6187 type->function.parameters = NULL;
6189 type_t *result = typehash_insert(type);
6190 if (result != type) {
6198 * Creates a function type for some function like builtins.
6200 * @param symbol the symbol describing the builtin
6202 static type_t *get_builtin_symbol_type(symbol_t *symbol)
6204 switch(symbol->ID) {
6205 case T___builtin_alloca:
6206 return make_function_1_type(type_void_ptr, type_size_t);
6207 case T___builtin_huge_val:
6208 return make_function_0_type(type_double);
6209 case T___builtin_inf:
6210 return make_function_0_type(type_double);
6211 case T___builtin_inff:
6212 return make_function_0_type(type_float);
6213 case T___builtin_infl:
6214 return make_function_0_type(type_long_double);
6215 case T___builtin_nan:
6216 return make_function_1_type(type_double, type_char_ptr);
6217 case T___builtin_nanf:
6218 return make_function_1_type(type_float, type_char_ptr);
6219 case T___builtin_nanl:
6220 return make_function_1_type(type_long_double, type_char_ptr);
6221 case T___builtin_va_end:
6222 return make_function_1_type(type_void, type_valist);
6223 case T___builtin_expect:
6224 return make_function_2_type(type_long, type_long, type_long);
6226 internal_errorf(HERE, "not implemented builtin symbol found");
6231 * Performs automatic type cast as described in § 6.3.2.1.
6233 * @param orig_type the original type
6235 static type_t *automatic_type_conversion(type_t *orig_type)
6237 type_t *type = skip_typeref(orig_type);
6238 if (is_type_array(type)) {
6239 array_type_t *array_type = &type->array;
6240 type_t *element_type = array_type->element_type;
6241 unsigned qualifiers = array_type->base.qualifiers;
6243 return make_pointer_type(element_type, qualifiers);
6246 if (is_type_function(type)) {
6247 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6254 * reverts the automatic casts of array to pointer types and function
6255 * to function-pointer types as defined § 6.3.2.1
6257 type_t *revert_automatic_type_conversion(const expression_t *expression)
6259 switch (expression->kind) {
6260 case EXPR_REFERENCE: return expression->reference.declaration->type;
6263 return get_qualified_type(expression->select.compound_entry->type,
6264 expression->base.type->base.qualifiers);
6266 case EXPR_UNARY_DEREFERENCE: {
6267 const expression_t *const value = expression->unary.value;
6268 type_t *const type = skip_typeref(value->base.type);
6269 assert(is_type_pointer(type));
6270 return type->pointer.points_to;
6273 case EXPR_BUILTIN_SYMBOL:
6274 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
6276 case EXPR_ARRAY_ACCESS: {
6277 const expression_t *array_ref = expression->array_access.array_ref;
6278 type_t *type_left = skip_typeref(array_ref->base.type);
6279 if (!is_type_valid(type_left))
6281 assert(is_type_pointer(type_left));
6282 return type_left->pointer.points_to;
6285 case EXPR_STRING_LITERAL: {
6286 size_t size = expression->string.value.size;
6287 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
6290 case EXPR_WIDE_STRING_LITERAL: {
6291 size_t size = expression->wide_string.value.size;
6292 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
6295 case EXPR_COMPOUND_LITERAL:
6296 return expression->compound_literal.type;
6301 return expression->base.type;
6304 static expression_t *parse_reference(void)
6306 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
6308 reference_expression_t *ref = &expression->reference;
6309 symbol_t *const symbol = token.v.symbol;
6311 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
6313 if (declaration == NULL) {
6314 if (!strict_mode && look_ahead(1)->type == '(') {
6315 /* an implicitly declared function */
6316 if (warning.implicit_function_declaration) {
6317 warningf(HERE, "implicit declaration of function '%Y'",
6321 declaration = create_implicit_function(symbol, HERE);
6323 errorf(HERE, "unknown symbol '%Y' found.", symbol);
6324 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
6328 type_t *orig_type = declaration->type;
6330 /* we always do the auto-type conversions; the & and sizeof parser contains
6331 * code to revert this! */
6332 type_t *type = automatic_type_conversion(orig_type);
6334 ref->declaration = declaration;
6335 ref->base.type = type;
6337 /* this declaration is used */
6338 declaration->used = true;
6340 if (declaration->parent_scope != file_scope &&
6341 declaration->parent_scope->depth < current_function->scope.depth &&
6342 is_type_valid(orig_type) && !is_type_function(orig_type)) {
6343 /* access of a variable from an outer function */
6344 declaration->address_taken = true;
6345 current_function->need_closure = true;
6348 /* check for deprecated functions */
6349 if (warning.deprecated_declarations &&
6350 declaration->modifiers & DM_DEPRECATED) {
6351 char const *const prefix = is_type_function(declaration->type) ?
6352 "function" : "variable";
6354 if (declaration->deprecated_string != NULL) {
6355 warningf(HERE, "%s '%Y' is deprecated (declared %P): \"%s\"",
6356 prefix, declaration->symbol, &declaration->source_position,
6357 declaration->deprecated_string);
6359 warningf(HERE, "%s '%Y' is deprecated (declared %P)", prefix,
6360 declaration->symbol, &declaration->source_position);
6363 if (warning.init_self && declaration == current_init_decl && !in_type_prop) {
6364 current_init_decl = NULL;
6365 warningf(HERE, "variable '%#T' is initialized by itself",
6366 declaration->type, declaration->symbol);
6373 static bool semantic_cast(expression_t *cast)
6375 expression_t *expression = cast->unary.value;
6376 type_t *orig_dest_type = cast->base.type;
6377 type_t *orig_type_right = expression->base.type;
6378 type_t const *dst_type = skip_typeref(orig_dest_type);
6379 type_t const *src_type = skip_typeref(orig_type_right);
6380 source_position_t const *pos = &cast->base.source_position;
6382 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
6383 if (dst_type == type_void)
6386 /* only integer and pointer can be casted to pointer */
6387 if (is_type_pointer(dst_type) &&
6388 !is_type_pointer(src_type) &&
6389 !is_type_integer(src_type) &&
6390 is_type_valid(src_type)) {
6391 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
6395 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
6396 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
6400 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
6401 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
6405 if (warning.cast_qual &&
6406 is_type_pointer(src_type) &&
6407 is_type_pointer(dst_type)) {
6408 type_t *src = skip_typeref(src_type->pointer.points_to);
6409 type_t *dst = skip_typeref(dst_type->pointer.points_to);
6410 unsigned missing_qualifiers =
6411 src->base.qualifiers & ~dst->base.qualifiers;
6412 if (missing_qualifiers != 0) {
6414 "cast discards qualifiers '%Q' in pointer target type of '%T'",
6415 missing_qualifiers, orig_type_right);
6421 static expression_t *parse_compound_literal(type_t *type)
6423 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
6425 parse_initializer_env_t env;
6427 env.declaration = NULL;
6428 env.must_be_constant = false;
6429 initializer_t *initializer = parse_initializer(&env);
6432 expression->compound_literal.initializer = initializer;
6433 expression->compound_literal.type = type;
6434 expression->base.type = automatic_type_conversion(type);
6440 * Parse a cast expression.
6442 static expression_t *parse_cast(void)
6444 add_anchor_token(')');
6446 source_position_t source_position = token.source_position;
6448 type_t *type = parse_typename();
6450 rem_anchor_token(')');
6453 if (token.type == '{') {
6454 return parse_compound_literal(type);
6457 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6458 cast->base.source_position = source_position;
6460 expression_t *value = parse_sub_expression(PREC_CAST);
6461 cast->base.type = type;
6462 cast->unary.value = value;
6464 if (! semantic_cast(cast)) {
6465 /* TODO: record the error in the AST. else it is impossible to detect it */
6470 return create_invalid_expression();
6474 * Parse a statement expression.
6476 static expression_t *parse_statement_expression(void)
6478 add_anchor_token(')');
6480 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6482 statement_t *statement = parse_compound_statement(true);
6483 expression->statement.statement = statement;
6484 expression->base.source_position = statement->base.source_position;
6486 /* find last statement and use its type */
6487 type_t *type = type_void;
6488 const statement_t *stmt = statement->compound.statements;
6490 while (stmt->base.next != NULL)
6491 stmt = stmt->base.next;
6493 if (stmt->kind == STATEMENT_EXPRESSION) {
6494 type = stmt->expression.expression->base.type;
6496 } else if (warning.other) {
6497 warningf(&expression->base.source_position, "empty statement expression ({})");
6499 expression->base.type = type;
6501 rem_anchor_token(')');
6509 * Parse a parenthesized expression.
6511 static expression_t *parse_parenthesized_expression(void)
6515 switch(token.type) {
6517 /* gcc extension: a statement expression */
6518 return parse_statement_expression();
6522 return parse_cast();
6524 if (is_typedef_symbol(token.v.symbol)) {
6525 return parse_cast();
6529 add_anchor_token(')');
6530 expression_t *result = parse_expression();
6531 rem_anchor_token(')');
6538 static expression_t *parse_function_keyword(void)
6543 if (current_function == NULL) {
6544 errorf(HERE, "'__func__' used outside of a function");
6547 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6548 expression->base.type = type_char_ptr;
6549 expression->funcname.kind = FUNCNAME_FUNCTION;
6554 static expression_t *parse_pretty_function_keyword(void)
6556 eat(T___PRETTY_FUNCTION__);
6558 if (current_function == NULL) {
6559 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6562 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6563 expression->base.type = type_char_ptr;
6564 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6569 static expression_t *parse_funcsig_keyword(void)
6573 if (current_function == NULL) {
6574 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6577 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6578 expression->base.type = type_char_ptr;
6579 expression->funcname.kind = FUNCNAME_FUNCSIG;
6584 static expression_t *parse_funcdname_keyword(void)
6586 eat(T___FUNCDNAME__);
6588 if (current_function == NULL) {
6589 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6592 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6593 expression->base.type = type_char_ptr;
6594 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6599 static designator_t *parse_designator(void)
6601 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6602 result->source_position = *HERE;
6604 if (token.type != T_IDENTIFIER) {
6605 parse_error_expected("while parsing member designator",
6606 T_IDENTIFIER, NULL);
6609 result->symbol = token.v.symbol;
6612 designator_t *last_designator = result;
6614 if (token.type == '.') {
6616 if (token.type != T_IDENTIFIER) {
6617 parse_error_expected("while parsing member designator",
6618 T_IDENTIFIER, NULL);
6621 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6622 designator->source_position = *HERE;
6623 designator->symbol = token.v.symbol;
6626 last_designator->next = designator;
6627 last_designator = designator;
6630 if (token.type == '[') {
6632 add_anchor_token(']');
6633 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6634 designator->source_position = *HERE;
6635 designator->array_index = parse_expression();
6636 rem_anchor_token(']');
6638 if (designator->array_index == NULL) {
6642 last_designator->next = designator;
6643 last_designator = designator;
6655 * Parse the __builtin_offsetof() expression.
6657 static expression_t *parse_offsetof(void)
6659 eat(T___builtin_offsetof);
6661 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6662 expression->base.type = type_size_t;
6665 add_anchor_token(',');
6666 type_t *type = parse_typename();
6667 rem_anchor_token(',');
6669 add_anchor_token(')');
6670 designator_t *designator = parse_designator();
6671 rem_anchor_token(')');
6674 expression->offsetofe.type = type;
6675 expression->offsetofe.designator = designator;
6678 memset(&path, 0, sizeof(path));
6679 path.top_type = type;
6680 path.path = NEW_ARR_F(type_path_entry_t, 0);
6682 descend_into_subtype(&path);
6684 if (!walk_designator(&path, designator, true)) {
6685 return create_invalid_expression();
6688 DEL_ARR_F(path.path);
6692 return create_invalid_expression();
6696 * Parses a _builtin_va_start() expression.
6698 static expression_t *parse_va_start(void)
6700 eat(T___builtin_va_start);
6702 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6705 add_anchor_token(',');
6706 expression->va_starte.ap = parse_assignment_expression();
6707 rem_anchor_token(',');
6709 expression_t *const expr = parse_assignment_expression();
6710 if (expr->kind == EXPR_REFERENCE) {
6711 declaration_t *const decl = expr->reference.declaration;
6712 if (decl->parent_scope != ¤t_function->scope || decl->next != NULL) {
6713 errorf(&expr->base.source_position,
6714 "second argument of 'va_start' must be last parameter of the current function");
6716 expression->va_starte.parameter = decl;
6722 return create_invalid_expression();
6726 * Parses a _builtin_va_arg() expression.
6728 static expression_t *parse_va_arg(void)
6730 eat(T___builtin_va_arg);
6732 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6735 expression->va_arge.ap = parse_assignment_expression();
6737 expression->base.type = parse_typename();
6742 return create_invalid_expression();
6745 static expression_t *parse_builtin_symbol(void)
6747 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6749 symbol_t *symbol = token.v.symbol;
6751 expression->builtin_symbol.symbol = symbol;
6754 type_t *type = get_builtin_symbol_type(symbol);
6755 type = automatic_type_conversion(type);
6757 expression->base.type = type;
6762 * Parses a __builtin_constant() expression.
6764 static expression_t *parse_builtin_constant(void)
6766 eat(T___builtin_constant_p);
6768 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6771 add_anchor_token(')');
6772 expression->builtin_constant.value = parse_assignment_expression();
6773 rem_anchor_token(')');
6775 expression->base.type = type_int;
6779 return create_invalid_expression();
6783 * Parses a __builtin_prefetch() expression.
6785 static expression_t *parse_builtin_prefetch(void)
6787 eat(T___builtin_prefetch);
6789 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6792 add_anchor_token(')');
6793 expression->builtin_prefetch.adr = parse_assignment_expression();
6794 if (token.type == ',') {
6796 expression->builtin_prefetch.rw = parse_assignment_expression();
6798 if (token.type == ',') {
6800 expression->builtin_prefetch.locality = parse_assignment_expression();
6802 rem_anchor_token(')');
6804 expression->base.type = type_void;
6808 return create_invalid_expression();
6812 * Parses a __builtin_is_*() compare expression.
6814 static expression_t *parse_compare_builtin(void)
6816 expression_t *expression;
6818 switch(token.type) {
6819 case T___builtin_isgreater:
6820 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6822 case T___builtin_isgreaterequal:
6823 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6825 case T___builtin_isless:
6826 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6828 case T___builtin_islessequal:
6829 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6831 case T___builtin_islessgreater:
6832 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6834 case T___builtin_isunordered:
6835 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6838 internal_errorf(HERE, "invalid compare builtin found");
6840 expression->base.source_position = *HERE;
6844 expression->binary.left = parse_assignment_expression();
6846 expression->binary.right = parse_assignment_expression();
6849 type_t *const orig_type_left = expression->binary.left->base.type;
6850 type_t *const orig_type_right = expression->binary.right->base.type;
6852 type_t *const type_left = skip_typeref(orig_type_left);
6853 type_t *const type_right = skip_typeref(orig_type_right);
6854 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6855 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6856 type_error_incompatible("invalid operands in comparison",
6857 &expression->base.source_position, orig_type_left, orig_type_right);
6860 semantic_comparison(&expression->binary);
6865 return create_invalid_expression();
6870 * Parses a __builtin_expect() expression.
6872 static expression_t *parse_builtin_expect(void)
6874 eat(T___builtin_expect);
6876 expression_t *expression
6877 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6880 expression->binary.left = parse_assignment_expression();
6882 expression->binary.right = parse_constant_expression();
6885 expression->base.type = expression->binary.left->base.type;
6889 return create_invalid_expression();
6894 * Parses a MS assume() expression.
6896 static expression_t *parse_assume(void)
6900 expression_t *expression
6901 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6904 add_anchor_token(')');
6905 expression->unary.value = parse_assignment_expression();
6906 rem_anchor_token(')');
6909 expression->base.type = type_void;
6912 return create_invalid_expression();
6916 * Return the declaration for a given label symbol or create a new one.
6918 * @param symbol the symbol of the label
6920 static declaration_t *get_label(symbol_t *symbol)
6922 declaration_t *candidate;
6923 assert(current_function != NULL);
6925 candidate = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
6926 /* if we found a local label, we already created the declaration */
6927 if (candidate != NULL) {
6928 if (candidate->parent_scope != scope) {
6929 assert(candidate->parent_scope->depth < scope->depth);
6930 current_function->goto_to_outer = true;
6935 candidate = get_declaration(symbol, NAMESPACE_LABEL);
6936 /* if we found a label in the same function, then we already created the
6938 if (candidate != NULL
6939 && candidate->parent_scope == ¤t_function->scope) {
6943 /* otherwise we need to create a new one */
6944 declaration_t *const declaration = allocate_declaration_zero();
6945 declaration->namespc = NAMESPACE_LABEL;
6946 declaration->symbol = symbol;
6948 label_push(declaration);
6954 * Parses a GNU && label address expression.
6956 static expression_t *parse_label_address(void)
6958 source_position_t source_position = token.source_position;
6960 if (token.type != T_IDENTIFIER) {
6961 parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
6964 symbol_t *symbol = token.v.symbol;
6967 declaration_t *label = get_label(symbol);
6970 label->address_taken = true;
6972 expression_t *expression = allocate_expression_zero(EXPR_LABEL_ADDRESS);
6973 expression->base.source_position = source_position;
6975 /* label address is threaten as a void pointer */
6976 expression->base.type = type_void_ptr;
6977 expression->label_address.declaration = label;
6980 return create_invalid_expression();
6984 * Parse a microsoft __noop expression.
6986 static expression_t *parse_noop_expression(void)
6988 source_position_t source_position = *HERE;
6991 if (token.type == '(') {
6992 /* parse arguments */
6994 add_anchor_token(')');
6995 add_anchor_token(',');
6997 if (token.type != ')') {
6999 (void)parse_assignment_expression();
7000 if (token.type != ',')
7006 rem_anchor_token(',');
7007 rem_anchor_token(')');
7010 /* the result is a (int)0 */
7011 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
7012 cnst->base.source_position = source_position;
7013 cnst->base.type = type_int;
7014 cnst->conste.v.int_value = 0;
7015 cnst->conste.is_ms_noop = true;
7020 return create_invalid_expression();
7024 * Parses a primary expression.
7026 static expression_t *parse_primary_expression(void)
7028 switch (token.type) {
7029 case T_INTEGER: return parse_int_const();
7030 case T_CHARACTER_CONSTANT: return parse_character_constant();
7031 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
7032 case T_FLOATINGPOINT: return parse_float_const();
7033 case T_STRING_LITERAL:
7034 case T_WIDE_STRING_LITERAL: return parse_string_const();
7035 case T_IDENTIFIER: return parse_reference();
7036 case T___FUNCTION__:
7037 case T___func__: return parse_function_keyword();
7038 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
7039 case T___FUNCSIG__: return parse_funcsig_keyword();
7040 case T___FUNCDNAME__: return parse_funcdname_keyword();
7041 case T___builtin_offsetof: return parse_offsetof();
7042 case T___builtin_va_start: return parse_va_start();
7043 case T___builtin_va_arg: return parse_va_arg();
7044 case T___builtin_expect:
7045 case T___builtin_alloca:
7046 case T___builtin_inf:
7047 case T___builtin_inff:
7048 case T___builtin_infl:
7049 case T___builtin_nan:
7050 case T___builtin_nanf:
7051 case T___builtin_nanl:
7052 case T___builtin_huge_val:
7053 case T___builtin_va_end: return parse_builtin_symbol();
7054 case T___builtin_isgreater:
7055 case T___builtin_isgreaterequal:
7056 case T___builtin_isless:
7057 case T___builtin_islessequal:
7058 case T___builtin_islessgreater:
7059 case T___builtin_isunordered: return parse_compare_builtin();
7060 case T___builtin_constant_p: return parse_builtin_constant();
7061 case T___builtin_prefetch: return parse_builtin_prefetch();
7062 case T__assume: return parse_assume();
7065 return parse_label_address();
7068 case '(': return parse_parenthesized_expression();
7069 case T___noop: return parse_noop_expression();
7072 errorf(HERE, "unexpected token %K, expected an expression", &token);
7073 return create_invalid_expression();
7077 * Check if the expression has the character type and issue a warning then.
7079 static void check_for_char_index_type(const expression_t *expression)
7081 type_t *const type = expression->base.type;
7082 const type_t *const base_type = skip_typeref(type);
7084 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
7085 warning.char_subscripts) {
7086 warningf(&expression->base.source_position,
7087 "array subscript has type '%T'", type);
7091 static expression_t *parse_array_expression(expression_t *left)
7094 add_anchor_token(']');
7096 expression_t *inside = parse_expression();
7098 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
7100 array_access_expression_t *array_access = &expression->array_access;
7102 type_t *const orig_type_left = left->base.type;
7103 type_t *const orig_type_inside = inside->base.type;
7105 type_t *const type_left = skip_typeref(orig_type_left);
7106 type_t *const type_inside = skip_typeref(orig_type_inside);
7108 type_t *return_type;
7109 if (is_type_pointer(type_left)) {
7110 return_type = type_left->pointer.points_to;
7111 array_access->array_ref = left;
7112 array_access->index = inside;
7113 check_for_char_index_type(inside);
7114 } else if (is_type_pointer(type_inside)) {
7115 return_type = type_inside->pointer.points_to;
7116 array_access->array_ref = inside;
7117 array_access->index = left;
7118 array_access->flipped = true;
7119 check_for_char_index_type(left);
7121 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
7123 "array access on object with non-pointer types '%T', '%T'",
7124 orig_type_left, orig_type_inside);
7126 return_type = type_error_type;
7127 array_access->array_ref = left;
7128 array_access->index = inside;
7131 expression->base.type = automatic_type_conversion(return_type);
7133 rem_anchor_token(']');
7134 if (token.type == ']') {
7137 parse_error_expected("Problem while parsing array access", ']', NULL);
7142 static expression_t *parse_typeprop(expression_kind_t const kind,
7143 source_position_t const pos)
7145 expression_t *tp_expression = allocate_expression_zero(kind);
7146 tp_expression->base.type = type_size_t;
7147 tp_expression->base.source_position = pos;
7149 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
7151 /* we only refer to a type property, mark this case */
7152 bool old = in_type_prop;
7153 in_type_prop = true;
7154 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
7156 add_anchor_token(')');
7157 type_t* const orig_type = parse_typename();
7158 tp_expression->typeprop.type = orig_type;
7160 type_t const* const type = skip_typeref(orig_type);
7161 char const* const wrong_type =
7162 is_type_incomplete(type) ? "incomplete" :
7163 type->kind == TYPE_FUNCTION ? "function designator" :
7164 type->kind == TYPE_BITFIELD ? "bitfield" :
7166 if (wrong_type != NULL) {
7167 errorf(&pos, "operand of %s expression must not be %s type '%T'",
7168 what, wrong_type, type);
7171 rem_anchor_token(')');
7174 expression_t *expression = parse_sub_expression(PREC_UNARY);
7176 type_t* const orig_type = revert_automatic_type_conversion(expression);
7177 expression->base.type = orig_type;
7179 type_t const* const type = skip_typeref(orig_type);
7180 char const* const wrong_type =
7181 is_type_incomplete(type) ? "incomplete" :
7182 type->kind == TYPE_FUNCTION ? "function designator" :
7183 type->kind == TYPE_BITFIELD ? "bitfield" :
7185 if (wrong_type != NULL) {
7186 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
7189 tp_expression->typeprop.type = expression->base.type;
7190 tp_expression->typeprop.tp_expression = expression;
7195 return tp_expression;
7198 static expression_t *parse_sizeof(void)
7200 source_position_t pos = *HERE;
7202 return parse_typeprop(EXPR_SIZEOF, pos);
7205 static expression_t *parse_alignof(void)
7207 source_position_t pos = *HERE;
7209 return parse_typeprop(EXPR_ALIGNOF, pos);
7212 static expression_t *parse_select_expression(expression_t *compound)
7214 assert(token.type == '.' || token.type == T_MINUSGREATER);
7216 bool is_pointer = (token.type == T_MINUSGREATER);
7219 expression_t *select = allocate_expression_zero(EXPR_SELECT);
7220 select->select.compound = compound;
7222 if (token.type != T_IDENTIFIER) {
7223 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
7226 symbol_t *symbol = token.v.symbol;
7229 type_t *const orig_type = compound->base.type;
7230 type_t *const type = skip_typeref(orig_type);
7233 bool saw_error = false;
7234 if (is_type_pointer(type)) {
7237 "request for member '%Y' in something not a struct or union, but '%T'",
7241 type_left = skip_typeref(type->pointer.points_to);
7243 if (is_pointer && is_type_valid(type)) {
7244 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
7250 declaration_t *entry;
7251 if (type_left->kind == TYPE_COMPOUND_STRUCT ||
7252 type_left->kind == TYPE_COMPOUND_UNION) {
7253 declaration_t *const declaration = type_left->compound.declaration;
7255 if (!declaration->init.complete) {
7256 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
7258 goto create_error_entry;
7261 entry = find_compound_entry(declaration, symbol);
7262 if (entry == NULL) {
7263 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
7264 goto create_error_entry;
7267 if (is_type_valid(type_left) && !saw_error) {
7269 "request for member '%Y' in something not a struct or union, but '%T'",
7273 entry = allocate_declaration_zero();
7274 entry->symbol = symbol;
7277 select->select.compound_entry = entry;
7279 type_t *const res_type =
7280 get_qualified_type(entry->type, type_left->base.qualifiers);
7282 /* we always do the auto-type conversions; the & and sizeof parser contains
7283 * code to revert this! */
7284 select->base.type = automatic_type_conversion(res_type);
7286 type_t *skipped = skip_typeref(res_type);
7287 if (skipped->kind == TYPE_BITFIELD) {
7288 select->base.type = skipped->bitfield.base_type;
7294 static void check_call_argument(const function_parameter_t *parameter,
7295 call_argument_t *argument, unsigned pos)
7297 type_t *expected_type = parameter->type;
7298 type_t *expected_type_skip = skip_typeref(expected_type);
7299 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
7300 expression_t *arg_expr = argument->expression;
7301 type_t *arg_type = skip_typeref(arg_expr->base.type);
7303 /* handle transparent union gnu extension */
7304 if (is_type_union(expected_type_skip)
7305 && (expected_type_skip->base.modifiers
7306 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
7307 declaration_t *union_decl = expected_type_skip->compound.declaration;
7309 declaration_t *declaration = union_decl->scope.declarations;
7310 type_t *best_type = NULL;
7311 for ( ; declaration != NULL; declaration = declaration->next) {
7312 type_t *decl_type = declaration->type;
7313 error = semantic_assign(decl_type, arg_expr);
7314 if (error == ASSIGN_ERROR_INCOMPATIBLE
7315 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
7318 if (error == ASSIGN_SUCCESS) {
7319 best_type = decl_type;
7320 } else if (best_type == NULL) {
7321 best_type = decl_type;
7325 if (best_type != NULL) {
7326 expected_type = best_type;
7330 error = semantic_assign(expected_type, arg_expr);
7331 argument->expression = create_implicit_cast(argument->expression,
7334 if (error != ASSIGN_SUCCESS) {
7335 /* report exact scope in error messages (like "in argument 3") */
7337 snprintf(buf, sizeof(buf), "call argument %u", pos);
7338 report_assign_error(error, expected_type, arg_expr, buf,
7339 &arg_expr->base.source_position);
7340 } else if (warning.traditional || warning.conversion) {
7341 type_t *const promoted_type = get_default_promoted_type(arg_type);
7342 if (!types_compatible(expected_type_skip, promoted_type) &&
7343 !types_compatible(expected_type_skip, type_void_ptr) &&
7344 !types_compatible(type_void_ptr, promoted_type)) {
7345 /* Deliberately show the skipped types in this warning */
7346 warningf(&arg_expr->base.source_position,
7347 "passing call argument %u as '%T' rather than '%T' due to prototype",
7348 pos, expected_type_skip, promoted_type);
7354 * Parse a call expression, ie. expression '( ... )'.
7356 * @param expression the function address
7358 static expression_t *parse_call_expression(expression_t *expression)
7360 expression_t *result = allocate_expression_zero(EXPR_CALL);
7361 result->base.source_position = expression->base.source_position;
7363 call_expression_t *call = &result->call;
7364 call->function = expression;
7366 type_t *const orig_type = expression->base.type;
7367 type_t *const type = skip_typeref(orig_type);
7369 function_type_t *function_type = NULL;
7370 if (is_type_pointer(type)) {
7371 type_t *const to_type = skip_typeref(type->pointer.points_to);
7373 if (is_type_function(to_type)) {
7374 function_type = &to_type->function;
7375 call->base.type = function_type->return_type;
7379 if (function_type == NULL && is_type_valid(type)) {
7380 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
7383 /* parse arguments */
7385 add_anchor_token(')');
7386 add_anchor_token(',');
7388 if (token.type != ')') {
7389 call_argument_t *last_argument = NULL;
7392 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7394 argument->expression = parse_assignment_expression();
7395 if (last_argument == NULL) {
7396 call->arguments = argument;
7398 last_argument->next = argument;
7400 last_argument = argument;
7402 if (token.type != ',')
7407 rem_anchor_token(',');
7408 rem_anchor_token(')');
7411 if (function_type == NULL)
7414 function_parameter_t *parameter = function_type->parameters;
7415 call_argument_t *argument = call->arguments;
7416 if (!function_type->unspecified_parameters) {
7417 for (unsigned pos = 0; parameter != NULL && argument != NULL;
7418 parameter = parameter->next, argument = argument->next) {
7419 check_call_argument(parameter, argument, ++pos);
7422 if (parameter != NULL) {
7423 errorf(HERE, "too few arguments to function '%E'", expression);
7424 } else if (argument != NULL && !function_type->variadic) {
7425 errorf(HERE, "too many arguments to function '%E'", expression);
7429 /* do default promotion */
7430 for( ; argument != NULL; argument = argument->next) {
7431 type_t *type = argument->expression->base.type;
7433 type = get_default_promoted_type(type);
7435 argument->expression
7436 = create_implicit_cast(argument->expression, type);
7439 check_format(&result->call);
7441 if (warning.aggregate_return &&
7442 is_type_compound(skip_typeref(function_type->return_type))) {
7443 warningf(&result->base.source_position,
7444 "function call has aggregate value");
7451 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
7453 static bool same_compound_type(const type_t *type1, const type_t *type2)
7456 is_type_compound(type1) &&
7457 type1->kind == type2->kind &&
7458 type1->compound.declaration == type2->compound.declaration;
7462 * Parse a conditional expression, ie. 'expression ? ... : ...'.
7464 * @param expression the conditional expression
7466 static expression_t *parse_conditional_expression(expression_t *expression)
7468 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
7470 conditional_expression_t *conditional = &result->conditional;
7471 conditional->base.source_position = *HERE;
7472 conditional->condition = expression;
7475 add_anchor_token(':');
7478 type_t *const condition_type_orig = expression->base.type;
7479 type_t *const condition_type = skip_typeref(condition_type_orig);
7480 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
7481 type_error("expected a scalar type in conditional condition",
7482 &expression->base.source_position, condition_type_orig);
7485 expression_t *true_expression = expression;
7486 bool gnu_cond = false;
7487 if (GNU_MODE && token.type == ':') {
7490 true_expression = parse_expression();
7491 rem_anchor_token(':');
7493 expression_t *false_expression =
7494 parse_sub_expression(c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL);
7496 type_t *const orig_true_type = true_expression->base.type;
7497 type_t *const orig_false_type = false_expression->base.type;
7498 type_t *const true_type = skip_typeref(orig_true_type);
7499 type_t *const false_type = skip_typeref(orig_false_type);
7502 type_t *result_type;
7503 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
7504 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7505 if (warning.other && (
7506 !is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
7507 !is_type_atomic(false_type, ATOMIC_TYPE_VOID)
7509 warningf(&conditional->base.source_position,
7510 "ISO C forbids conditional expression with only one void side");
7512 result_type = type_void;
7513 } else if (is_type_arithmetic(true_type)
7514 && is_type_arithmetic(false_type)) {
7515 result_type = semantic_arithmetic(true_type, false_type);
7517 true_expression = create_implicit_cast(true_expression, result_type);
7518 false_expression = create_implicit_cast(false_expression, result_type);
7520 conditional->true_expression = true_expression;
7521 conditional->false_expression = false_expression;
7522 conditional->base.type = result_type;
7523 } else if (same_compound_type(true_type, false_type)) {
7524 /* just take 1 of the 2 types */
7525 result_type = true_type;
7526 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
7527 type_t *pointer_type;
7529 expression_t *other_expression;
7530 if (is_type_pointer(true_type) &&
7531 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
7532 pointer_type = true_type;
7533 other_type = false_type;
7534 other_expression = false_expression;
7536 pointer_type = false_type;
7537 other_type = true_type;
7538 other_expression = true_expression;
7541 if (is_null_pointer_constant(other_expression)) {
7542 result_type = pointer_type;
7543 } else if (is_type_pointer(other_type)) {
7544 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7545 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7548 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7549 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7551 } else if (types_compatible(get_unqualified_type(to1),
7552 get_unqualified_type(to2))) {
7555 if (warning.other) {
7556 warningf(&conditional->base.source_position,
7557 "pointer types '%T' and '%T' in conditional expression are incompatible",
7558 true_type, false_type);
7563 type_t *const type =
7564 get_qualified_type(to, to1->base.qualifiers | to2->base.qualifiers);
7565 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7566 } else if (is_type_integer(other_type)) {
7567 if (warning.other) {
7568 warningf(&conditional->base.source_position,
7569 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7571 result_type = pointer_type;
7573 if (is_type_valid(other_type)) {
7574 type_error_incompatible("while parsing conditional",
7575 &expression->base.source_position, true_type, false_type);
7577 result_type = type_error_type;
7580 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7581 type_error_incompatible("while parsing conditional",
7582 &conditional->base.source_position, true_type,
7585 result_type = type_error_type;
7588 conditional->true_expression
7589 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7590 conditional->false_expression
7591 = create_implicit_cast(false_expression, result_type);
7592 conditional->base.type = result_type;
7595 return create_invalid_expression();
7599 * Parse an extension expression.
7601 static expression_t *parse_extension(void)
7603 eat(T___extension__);
7605 bool old_gcc_extension = in_gcc_extension;
7606 in_gcc_extension = true;
7607 expression_t *expression = parse_sub_expression(PREC_UNARY);
7608 in_gcc_extension = old_gcc_extension;
7613 * Parse a __builtin_classify_type() expression.
7615 static expression_t *parse_builtin_classify_type(void)
7617 eat(T___builtin_classify_type);
7619 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7620 result->base.type = type_int;
7623 add_anchor_token(')');
7624 expression_t *expression = parse_expression();
7625 rem_anchor_token(')');
7627 result->classify_type.type_expression = expression;
7631 return create_invalid_expression();
7634 static bool check_pointer_arithmetic(const source_position_t *source_position,
7635 type_t *pointer_type,
7636 type_t *orig_pointer_type)
7638 type_t *points_to = pointer_type->pointer.points_to;
7639 points_to = skip_typeref(points_to);
7641 if (is_type_incomplete(points_to)) {
7642 if (!GNU_MODE || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7643 errorf(source_position,
7644 "arithmetic with pointer to incomplete type '%T' not allowed",
7647 } else if (warning.pointer_arith) {
7648 warningf(source_position,
7649 "pointer of type '%T' used in arithmetic",
7652 } else if (is_type_function(points_to)) {
7654 errorf(source_position,
7655 "arithmetic with pointer to function type '%T' not allowed",
7658 } else if (warning.pointer_arith) {
7659 warningf(source_position,
7660 "pointer to a function '%T' used in arithmetic",
7667 static bool is_lvalue(const expression_t *expression)
7669 switch (expression->kind) {
7670 case EXPR_REFERENCE:
7671 case EXPR_ARRAY_ACCESS:
7673 case EXPR_UNARY_DEREFERENCE:
7677 /* Claim it is an lvalue, if the type is invalid. There was a parse
7678 * error before, which maybe prevented properly recognizing it as
7680 return !is_type_valid(skip_typeref(expression->base.type));
7684 static void semantic_incdec(unary_expression_t *expression)
7686 type_t *const orig_type = expression->value->base.type;
7687 type_t *const type = skip_typeref(orig_type);
7688 if (is_type_pointer(type)) {
7689 if (!check_pointer_arithmetic(&expression->base.source_position,
7693 } else if (!is_type_real(type) && is_type_valid(type)) {
7694 /* TODO: improve error message */
7695 errorf(&expression->base.source_position,
7696 "operation needs an arithmetic or pointer type");
7699 if (!is_lvalue(expression->value)) {
7700 /* TODO: improve error message */
7701 errorf(&expression->base.source_position, "lvalue required as operand");
7703 expression->base.type = orig_type;
7706 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7708 type_t *const orig_type = expression->value->base.type;
7709 type_t *const type = skip_typeref(orig_type);
7710 if (!is_type_arithmetic(type)) {
7711 if (is_type_valid(type)) {
7712 /* TODO: improve error message */
7713 errorf(&expression->base.source_position,
7714 "operation needs an arithmetic type");
7719 expression->base.type = orig_type;
7722 static void semantic_unexpr_plus(unary_expression_t *expression)
7724 semantic_unexpr_arithmetic(expression);
7725 if (warning.traditional)
7726 warningf(&expression->base.source_position,
7727 "traditional C rejects the unary plus operator");
7730 static expression_t const *get_reference_address(expression_t const *expr)
7732 bool regular_take_address = true;
7734 if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
7735 expr = expr->unary.value;
7737 regular_take_address = false;
7740 if (expr->kind != EXPR_UNARY_DEREFERENCE)
7743 expr = expr->unary.value;
7746 if (expr->kind != EXPR_REFERENCE)
7749 if (!regular_take_address &&
7750 !is_type_function(skip_typeref(expr->reference.declaration->type))) {
7757 static void warn_function_address_as_bool(expression_t const* expr)
7759 if (!warning.address)
7762 expr = get_reference_address(expr);
7764 warningf(&expr->base.source_position,
7765 "the address of '%Y' will always evaluate as 'true'",
7766 expr->reference.declaration->symbol);
7770 static void semantic_not(unary_expression_t *expression)
7772 type_t *const orig_type = expression->value->base.type;
7773 type_t *const type = skip_typeref(orig_type);
7774 if (!is_type_scalar(type) && is_type_valid(type)) {
7775 errorf(&expression->base.source_position,
7776 "operand of ! must be of scalar type");
7779 warn_function_address_as_bool(expression->value);
7781 expression->base.type = type_int;
7784 static void semantic_unexpr_integer(unary_expression_t *expression)
7786 type_t *const orig_type = expression->value->base.type;
7787 type_t *const type = skip_typeref(orig_type);
7788 if (!is_type_integer(type)) {
7789 if (is_type_valid(type)) {
7790 errorf(&expression->base.source_position,
7791 "operand of ~ must be of integer type");
7796 expression->base.type = orig_type;
7799 static void semantic_dereference(unary_expression_t *expression)
7801 type_t *const orig_type = expression->value->base.type;
7802 type_t *const type = skip_typeref(orig_type);
7803 if (!is_type_pointer(type)) {
7804 if (is_type_valid(type)) {
7805 errorf(&expression->base.source_position,
7806 "Unary '*' needs pointer or array type, but type '%T' given", orig_type);
7811 type_t *result_type = type->pointer.points_to;
7812 result_type = automatic_type_conversion(result_type);
7813 expression->base.type = result_type;
7817 * Record that an address is taken (expression represents an lvalue).
7819 * @param expression the expression
7820 * @param may_be_register if true, the expression might be an register
7822 static void set_address_taken(expression_t *expression, bool may_be_register)
7824 if (expression->kind != EXPR_REFERENCE)
7827 declaration_t *const declaration = expression->reference.declaration;
7828 /* happens for parse errors */
7829 if (declaration == NULL)
7832 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7833 errorf(&expression->base.source_position,
7834 "address of register variable '%Y' requested",
7835 declaration->symbol);
7837 declaration->address_taken = 1;
7842 * Check the semantic of the address taken expression.
7844 static void semantic_take_addr(unary_expression_t *expression)
7846 expression_t *value = expression->value;
7847 value->base.type = revert_automatic_type_conversion(value);
7849 type_t *orig_type = value->base.type;
7850 if (!is_type_valid(skip_typeref(orig_type)))
7853 set_address_taken(value, false);
7855 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7858 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7859 static expression_t *parse_##unexpression_type(void) \
7861 expression_t *unary_expression \
7862 = allocate_expression_zero(unexpression_type); \
7863 unary_expression->base.source_position = *HERE; \
7865 unary_expression->unary.value = parse_sub_expression(PREC_UNARY); \
7867 sfunc(&unary_expression->unary); \
7869 return unary_expression; \
7872 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7873 semantic_unexpr_arithmetic)
7874 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7875 semantic_unexpr_plus)
7876 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7878 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7879 semantic_dereference)
7880 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7882 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7883 semantic_unexpr_integer)
7884 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7886 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7889 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7891 static expression_t *parse_##unexpression_type(expression_t *left) \
7893 expression_t *unary_expression \
7894 = allocate_expression_zero(unexpression_type); \
7895 unary_expression->base.source_position = *HERE; \
7897 unary_expression->unary.value = left; \
7899 sfunc(&unary_expression->unary); \
7901 return unary_expression; \
7904 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7905 EXPR_UNARY_POSTFIX_INCREMENT,
7907 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7908 EXPR_UNARY_POSTFIX_DECREMENT,
7911 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7913 /* TODO: handle complex + imaginary types */
7915 type_left = get_unqualified_type(type_left);
7916 type_right = get_unqualified_type(type_right);
7918 /* § 6.3.1.8 Usual arithmetic conversions */
7919 if (type_left == type_long_double || type_right == type_long_double) {
7920 return type_long_double;
7921 } else if (type_left == type_double || type_right == type_double) {
7923 } else if (type_left == type_float || type_right == type_float) {
7927 type_left = promote_integer(type_left);
7928 type_right = promote_integer(type_right);
7930 if (type_left == type_right)
7933 bool const signed_left = is_type_signed(type_left);
7934 bool const signed_right = is_type_signed(type_right);
7935 int const rank_left = get_rank(type_left);
7936 int const rank_right = get_rank(type_right);
7938 if (signed_left == signed_right)
7939 return rank_left >= rank_right ? type_left : type_right;
7948 u_rank = rank_right;
7949 u_type = type_right;
7951 s_rank = rank_right;
7952 s_type = type_right;
7957 if (u_rank >= s_rank)
7960 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7962 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7963 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7967 case ATOMIC_TYPE_INT: return type_unsigned_int;
7968 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7969 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7971 default: panic("invalid atomic type");
7976 * Check the semantic restrictions for a binary expression.
7978 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7980 expression_t *const left = expression->left;
7981 expression_t *const right = expression->right;
7982 type_t *const orig_type_left = left->base.type;
7983 type_t *const orig_type_right = right->base.type;
7984 type_t *const type_left = skip_typeref(orig_type_left);
7985 type_t *const type_right = skip_typeref(orig_type_right);
7987 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7988 /* TODO: improve error message */
7989 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7990 errorf(&expression->base.source_position,
7991 "operation needs arithmetic types");
7996 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7997 expression->left = create_implicit_cast(left, arithmetic_type);
7998 expression->right = create_implicit_cast(right, arithmetic_type);
7999 expression->base.type = arithmetic_type;
8002 static void warn_div_by_zero(binary_expression_t const *const expression)
8004 if (!warning.div_by_zero ||
8005 !is_type_integer(expression->base.type))
8008 expression_t const *const right = expression->right;
8009 /* The type of the right operand can be different for /= */
8010 if (is_type_integer(right->base.type) &&
8011 is_constant_expression(right) &&
8012 fold_constant(right) == 0) {
8013 warningf(&expression->base.source_position, "division by zero");
8018 * Check the semantic restrictions for a div/mod expression.
8020 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
8021 semantic_binexpr_arithmetic(expression);
8022 warn_div_by_zero(expression);
8025 static void semantic_shift_op(binary_expression_t *expression)
8027 expression_t *const left = expression->left;
8028 expression_t *const right = expression->right;
8029 type_t *const orig_type_left = left->base.type;
8030 type_t *const orig_type_right = right->base.type;
8031 type_t * type_left = skip_typeref(orig_type_left);
8032 type_t * type_right = skip_typeref(orig_type_right);
8034 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
8035 /* TODO: improve error message */
8036 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8037 errorf(&expression->base.source_position,
8038 "operands of shift operation must have integer types");
8043 type_left = promote_integer(type_left);
8044 type_right = promote_integer(type_right);
8046 expression->left = create_implicit_cast(left, type_left);
8047 expression->right = create_implicit_cast(right, type_right);
8048 expression->base.type = type_left;
8051 static void semantic_add(binary_expression_t *expression)
8053 expression_t *const left = expression->left;
8054 expression_t *const right = expression->right;
8055 type_t *const orig_type_left = left->base.type;
8056 type_t *const orig_type_right = right->base.type;
8057 type_t *const type_left = skip_typeref(orig_type_left);
8058 type_t *const type_right = skip_typeref(orig_type_right);
8061 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8062 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8063 expression->left = create_implicit_cast(left, arithmetic_type);
8064 expression->right = create_implicit_cast(right, arithmetic_type);
8065 expression->base.type = arithmetic_type;
8067 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8068 check_pointer_arithmetic(&expression->base.source_position,
8069 type_left, orig_type_left);
8070 expression->base.type = type_left;
8071 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
8072 check_pointer_arithmetic(&expression->base.source_position,
8073 type_right, orig_type_right);
8074 expression->base.type = type_right;
8075 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8076 errorf(&expression->base.source_position,
8077 "invalid operands to binary + ('%T', '%T')",
8078 orig_type_left, orig_type_right);
8082 static void semantic_sub(binary_expression_t *expression)
8084 expression_t *const left = expression->left;
8085 expression_t *const right = expression->right;
8086 type_t *const orig_type_left = left->base.type;
8087 type_t *const orig_type_right = right->base.type;
8088 type_t *const type_left = skip_typeref(orig_type_left);
8089 type_t *const type_right = skip_typeref(orig_type_right);
8090 source_position_t const *const pos = &expression->base.source_position;
8093 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8094 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8095 expression->left = create_implicit_cast(left, arithmetic_type);
8096 expression->right = create_implicit_cast(right, arithmetic_type);
8097 expression->base.type = arithmetic_type;
8099 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8100 check_pointer_arithmetic(&expression->base.source_position,
8101 type_left, orig_type_left);
8102 expression->base.type = type_left;
8103 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8104 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
8105 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
8106 if (!types_compatible(unqual_left, unqual_right)) {
8108 "subtracting pointers to incompatible types '%T' and '%T'",
8109 orig_type_left, orig_type_right);
8110 } else if (!is_type_object(unqual_left)) {
8111 if (!is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
8112 errorf(pos, "subtracting pointers to non-object types '%T'",
8114 } else if (warning.other) {
8115 warningf(pos, "subtracting pointers to void");
8118 expression->base.type = type_ptrdiff_t;
8119 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8120 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
8121 orig_type_left, orig_type_right);
8125 static void warn_string_literal_address(expression_t const* expr)
8127 while (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
8128 expr = expr->unary.value;
8129 if (expr->kind != EXPR_UNARY_DEREFERENCE)
8131 expr = expr->unary.value;
8134 if (expr->kind == EXPR_STRING_LITERAL ||
8135 expr->kind == EXPR_WIDE_STRING_LITERAL) {
8136 warningf(&expr->base.source_position,
8137 "comparison with string literal results in unspecified behaviour");
8142 * Check the semantics of comparison expressions.
8144 * @param expression The expression to check.
8146 static void semantic_comparison(binary_expression_t *expression)
8148 expression_t *left = expression->left;
8149 expression_t *right = expression->right;
8151 if (warning.address) {
8152 warn_string_literal_address(left);
8153 warn_string_literal_address(right);
8155 expression_t const* const func_left = get_reference_address(left);
8156 if (func_left != NULL && is_null_pointer_constant(right)) {
8157 warningf(&expression->base.source_position,
8158 "the address of '%Y' will never be NULL",
8159 func_left->reference.declaration->symbol);
8162 expression_t const* const func_right = get_reference_address(right);
8163 if (func_right != NULL && is_null_pointer_constant(right)) {
8164 warningf(&expression->base.source_position,
8165 "the address of '%Y' will never be NULL",
8166 func_right->reference.declaration->symbol);
8170 type_t *orig_type_left = left->base.type;
8171 type_t *orig_type_right = right->base.type;
8172 type_t *type_left = skip_typeref(orig_type_left);
8173 type_t *type_right = skip_typeref(orig_type_right);
8175 /* TODO non-arithmetic types */
8176 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8177 /* test for signed vs unsigned compares */
8178 if (warning.sign_compare &&
8179 (expression->base.kind != EXPR_BINARY_EQUAL &&
8180 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
8181 (is_type_signed(type_left) != is_type_signed(type_right))) {
8183 /* check if 1 of the operands is a constant, in this case we just
8184 * check wether we can safely represent the resulting constant in
8185 * the type of the other operand. */
8186 expression_t *const_expr = NULL;
8187 expression_t *other_expr = NULL;
8189 if (is_constant_expression(left)) {
8192 } else if (is_constant_expression(right)) {
8197 if (const_expr != NULL) {
8198 type_t *other_type = skip_typeref(other_expr->base.type);
8199 long val = fold_constant(const_expr);
8200 /* TODO: check if val can be represented by other_type */
8204 warningf(&expression->base.source_position,
8205 "comparison between signed and unsigned");
8207 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8208 expression->left = create_implicit_cast(left, arithmetic_type);
8209 expression->right = create_implicit_cast(right, arithmetic_type);
8210 expression->base.type = arithmetic_type;
8211 if (warning.float_equal &&
8212 (expression->base.kind == EXPR_BINARY_EQUAL ||
8213 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
8214 is_type_float(arithmetic_type)) {
8215 warningf(&expression->base.source_position,
8216 "comparing floating point with == or != is unsafe");
8218 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8219 /* TODO check compatibility */
8220 } else if (is_type_pointer(type_left)) {
8221 expression->right = create_implicit_cast(right, type_left);
8222 } else if (is_type_pointer(type_right)) {
8223 expression->left = create_implicit_cast(left, type_right);
8224 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8225 type_error_incompatible("invalid operands in comparison",
8226 &expression->base.source_position,
8227 type_left, type_right);
8229 expression->base.type = type_int;
8233 * Checks if a compound type has constant fields.
8235 static bool has_const_fields(const compound_type_t *type)
8237 const scope_t *scope = &type->declaration->scope;
8238 const declaration_t *declaration = scope->declarations;
8240 for (; declaration != NULL; declaration = declaration->next) {
8241 if (declaration->namespc != NAMESPACE_NORMAL)
8244 const type_t *decl_type = skip_typeref(declaration->type);
8245 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
8252 static bool is_valid_assignment_lhs(expression_t const* const left)
8254 type_t *const orig_type_left = revert_automatic_type_conversion(left);
8255 type_t *const type_left = skip_typeref(orig_type_left);
8257 if (!is_lvalue(left)) {
8258 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
8263 if (is_type_array(type_left)) {
8264 errorf(HERE, "cannot assign to arrays ('%E')", left);
8267 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
8268 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
8272 if (is_type_incomplete(type_left)) {
8273 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
8274 left, orig_type_left);
8277 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
8278 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
8279 left, orig_type_left);
8286 static void semantic_arithmetic_assign(binary_expression_t *expression)
8288 expression_t *left = expression->left;
8289 expression_t *right = expression->right;
8290 type_t *orig_type_left = left->base.type;
8291 type_t *orig_type_right = right->base.type;
8293 if (!is_valid_assignment_lhs(left))
8296 type_t *type_left = skip_typeref(orig_type_left);
8297 type_t *type_right = skip_typeref(orig_type_right);
8299 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
8300 /* TODO: improve error message */
8301 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8302 errorf(&expression->base.source_position,
8303 "operation needs arithmetic types");
8308 /* combined instructions are tricky. We can't create an implicit cast on
8309 * the left side, because we need the uncasted form for the store.
8310 * The ast2firm pass has to know that left_type must be right_type
8311 * for the arithmetic operation and create a cast by itself */
8312 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8313 expression->right = create_implicit_cast(right, arithmetic_type);
8314 expression->base.type = type_left;
8317 static void semantic_divmod_assign(binary_expression_t *expression)
8319 semantic_arithmetic_assign(expression);
8320 warn_div_by_zero(expression);
8323 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
8325 expression_t *const left = expression->left;
8326 expression_t *const right = expression->right;
8327 type_t *const orig_type_left = left->base.type;
8328 type_t *const orig_type_right = right->base.type;
8329 type_t *const type_left = skip_typeref(orig_type_left);
8330 type_t *const type_right = skip_typeref(orig_type_right);
8332 if (!is_valid_assignment_lhs(left))
8335 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8336 /* combined instructions are tricky. We can't create an implicit cast on
8337 * the left side, because we need the uncasted form for the store.
8338 * The ast2firm pass has to know that left_type must be right_type
8339 * for the arithmetic operation and create a cast by itself */
8340 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
8341 expression->right = create_implicit_cast(right, arithmetic_type);
8342 expression->base.type = type_left;
8343 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8344 check_pointer_arithmetic(&expression->base.source_position,
8345 type_left, orig_type_left);
8346 expression->base.type = type_left;
8347 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8348 errorf(&expression->base.source_position,
8349 "incompatible types '%T' and '%T' in assignment",
8350 orig_type_left, orig_type_right);
8355 * Check the semantic restrictions of a logical expression.
8357 static void semantic_logical_op(binary_expression_t *expression)
8359 expression_t *const left = expression->left;
8360 expression_t *const right = expression->right;
8361 type_t *const orig_type_left = left->base.type;
8362 type_t *const orig_type_right = right->base.type;
8363 type_t *const type_left = skip_typeref(orig_type_left);
8364 type_t *const type_right = skip_typeref(orig_type_right);
8366 warn_function_address_as_bool(left);
8367 warn_function_address_as_bool(right);
8369 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
8370 /* TODO: improve error message */
8371 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8372 errorf(&expression->base.source_position,
8373 "operation needs scalar types");
8378 expression->base.type = type_int;
8382 * Check the semantic restrictions of a binary assign expression.
8384 static void semantic_binexpr_assign(binary_expression_t *expression)
8386 expression_t *left = expression->left;
8387 type_t *orig_type_left = left->base.type;
8389 if (!is_valid_assignment_lhs(left))
8392 assign_error_t error = semantic_assign(orig_type_left, expression->right);
8393 report_assign_error(error, orig_type_left, expression->right,
8394 "assignment", &left->base.source_position);
8395 expression->right = create_implicit_cast(expression->right, orig_type_left);
8396 expression->base.type = orig_type_left;
8400 * Determine if the outermost operation (or parts thereof) of the given
8401 * expression has no effect in order to generate a warning about this fact.
8402 * Therefore in some cases this only examines some of the operands of the
8403 * expression (see comments in the function and examples below).
8405 * f() + 23; // warning, because + has no effect
8406 * x || f(); // no warning, because x controls execution of f()
8407 * x ? y : f(); // warning, because y has no effect
8408 * (void)x; // no warning to be able to suppress the warning
8409 * This function can NOT be used for an "expression has definitely no effect"-
8411 static bool expression_has_effect(const expression_t *const expr)
8413 switch (expr->kind) {
8414 case EXPR_UNKNOWN: break;
8415 case EXPR_INVALID: return true; /* do NOT warn */
8416 case EXPR_REFERENCE: return false;
8417 /* suppress the warning for microsoft __noop operations */
8418 case EXPR_CONST: return expr->conste.is_ms_noop;
8419 case EXPR_CHARACTER_CONSTANT: return false;
8420 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
8421 case EXPR_STRING_LITERAL: return false;
8422 case EXPR_WIDE_STRING_LITERAL: return false;
8423 case EXPR_LABEL_ADDRESS: return false;
8426 const call_expression_t *const call = &expr->call;
8427 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
8430 switch (call->function->builtin_symbol.symbol->ID) {
8431 case T___builtin_va_end: return true;
8432 default: return false;
8436 /* Generate the warning if either the left or right hand side of a
8437 * conditional expression has no effect */
8438 case EXPR_CONDITIONAL: {
8439 const conditional_expression_t *const cond = &expr->conditional;
8441 expression_has_effect(cond->true_expression) &&
8442 expression_has_effect(cond->false_expression);
8445 case EXPR_SELECT: return false;
8446 case EXPR_ARRAY_ACCESS: return false;
8447 case EXPR_SIZEOF: return false;
8448 case EXPR_CLASSIFY_TYPE: return false;
8449 case EXPR_ALIGNOF: return false;
8451 case EXPR_FUNCNAME: return false;
8452 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
8453 case EXPR_BUILTIN_CONSTANT_P: return false;
8454 case EXPR_BUILTIN_PREFETCH: return true;
8455 case EXPR_OFFSETOF: return false;
8456 case EXPR_VA_START: return true;
8457 case EXPR_VA_ARG: return true;
8458 case EXPR_STATEMENT: return true; // TODO
8459 case EXPR_COMPOUND_LITERAL: return false;
8461 case EXPR_UNARY_NEGATE: return false;
8462 case EXPR_UNARY_PLUS: return false;
8463 case EXPR_UNARY_BITWISE_NEGATE: return false;
8464 case EXPR_UNARY_NOT: return false;
8465 case EXPR_UNARY_DEREFERENCE: return false;
8466 case EXPR_UNARY_TAKE_ADDRESS: return false;
8467 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
8468 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
8469 case EXPR_UNARY_PREFIX_INCREMENT: return true;
8470 case EXPR_UNARY_PREFIX_DECREMENT: return true;
8472 /* Treat void casts as if they have an effect in order to being able to
8473 * suppress the warning */
8474 case EXPR_UNARY_CAST: {
8475 type_t *const type = skip_typeref(expr->base.type);
8476 return is_type_atomic(type, ATOMIC_TYPE_VOID);
8479 case EXPR_UNARY_CAST_IMPLICIT: return true;
8480 case EXPR_UNARY_ASSUME: return true;
8482 case EXPR_BINARY_ADD: return false;
8483 case EXPR_BINARY_SUB: return false;
8484 case EXPR_BINARY_MUL: return false;
8485 case EXPR_BINARY_DIV: return false;
8486 case EXPR_BINARY_MOD: return false;
8487 case EXPR_BINARY_EQUAL: return false;
8488 case EXPR_BINARY_NOTEQUAL: return false;
8489 case EXPR_BINARY_LESS: return false;
8490 case EXPR_BINARY_LESSEQUAL: return false;
8491 case EXPR_BINARY_GREATER: return false;
8492 case EXPR_BINARY_GREATEREQUAL: return false;
8493 case EXPR_BINARY_BITWISE_AND: return false;
8494 case EXPR_BINARY_BITWISE_OR: return false;
8495 case EXPR_BINARY_BITWISE_XOR: return false;
8496 case EXPR_BINARY_SHIFTLEFT: return false;
8497 case EXPR_BINARY_SHIFTRIGHT: return false;
8498 case EXPR_BINARY_ASSIGN: return true;
8499 case EXPR_BINARY_MUL_ASSIGN: return true;
8500 case EXPR_BINARY_DIV_ASSIGN: return true;
8501 case EXPR_BINARY_MOD_ASSIGN: return true;
8502 case EXPR_BINARY_ADD_ASSIGN: return true;
8503 case EXPR_BINARY_SUB_ASSIGN: return true;
8504 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
8505 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
8506 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
8507 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
8508 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
8510 /* Only examine the right hand side of && and ||, because the left hand
8511 * side already has the effect of controlling the execution of the right
8513 case EXPR_BINARY_LOGICAL_AND:
8514 case EXPR_BINARY_LOGICAL_OR:
8515 /* Only examine the right hand side of a comma expression, because the left
8516 * hand side has a separate warning */
8517 case EXPR_BINARY_COMMA:
8518 return expression_has_effect(expr->binary.right);
8520 case EXPR_BINARY_BUILTIN_EXPECT: return true;
8521 case EXPR_BINARY_ISGREATER: return false;
8522 case EXPR_BINARY_ISGREATEREQUAL: return false;
8523 case EXPR_BINARY_ISLESS: return false;
8524 case EXPR_BINARY_ISLESSEQUAL: return false;
8525 case EXPR_BINARY_ISLESSGREATER: return false;
8526 case EXPR_BINARY_ISUNORDERED: return false;
8529 internal_errorf(HERE, "unexpected expression");
8532 static void semantic_comma(binary_expression_t *expression)
8534 if (warning.unused_value) {
8535 const expression_t *const left = expression->left;
8536 if (!expression_has_effect(left)) {
8537 warningf(&left->base.source_position,
8538 "left-hand operand of comma expression has no effect");
8541 expression->base.type = expression->right->base.type;
8545 * @param prec_r precedence of the right operand
8547 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, prec_r, sfunc) \
8548 static expression_t *parse_##binexpression_type(expression_t *left) \
8550 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
8551 binexpr->base.source_position = *HERE; \
8552 binexpr->binary.left = left; \
8555 expression_t *right = parse_sub_expression(prec_r); \
8557 binexpr->binary.right = right; \
8558 sfunc(&binexpr->binary); \
8563 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, PREC_CAST, semantic_binexpr_arithmetic)
8564 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, PREC_CAST, semantic_divmod_arithmetic)
8565 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, PREC_CAST, semantic_divmod_arithmetic)
8566 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, PREC_MULTIPLICATIVE, semantic_add)
8567 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, PREC_MULTIPLICATIVE, semantic_sub)
8568 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT, PREC_ADDITIVE, semantic_shift_op)
8569 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT, PREC_ADDITIVE, semantic_shift_op)
8570 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, PREC_SHIFT, semantic_comparison)
8571 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, PREC_SHIFT, semantic_comparison)
8572 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL, PREC_SHIFT, semantic_comparison)
8573 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL, PREC_SHIFT, semantic_comparison)
8574 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL, PREC_RELATIONAL, semantic_comparison)
8575 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL, PREC_RELATIONAL, semantic_comparison)
8576 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND, PREC_EQUALITY, semantic_binexpr_arithmetic)
8577 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR, PREC_AND, semantic_binexpr_arithmetic)
8578 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR, PREC_XOR, semantic_binexpr_arithmetic)
8579 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND, PREC_OR, semantic_logical_op)
8580 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR, PREC_LOGICAL_AND, semantic_logical_op)
8581 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, PREC_ASSIGNMENT, semantic_binexpr_assign)
8582 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8583 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8584 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8585 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8586 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8587 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8588 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8589 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8590 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8591 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8592 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, PREC_ASSIGNMENT, semantic_comma)
8595 static expression_t *parse_sub_expression(precedence_t precedence)
8597 if (token.type < 0) {
8598 return expected_expression_error();
8601 expression_parser_function_t *parser
8602 = &expression_parsers[token.type];
8603 source_position_t source_position = token.source_position;
8606 if (parser->parser != NULL) {
8607 left = parser->parser();
8609 left = parse_primary_expression();
8611 assert(left != NULL);
8612 left->base.source_position = source_position;
8615 if (token.type < 0) {
8616 return expected_expression_error();
8619 parser = &expression_parsers[token.type];
8620 if (parser->infix_parser == NULL)
8622 if (parser->infix_precedence < precedence)
8625 left = parser->infix_parser(left);
8627 assert(left != NULL);
8628 assert(left->kind != EXPR_UNKNOWN);
8629 left->base.source_position = source_position;
8636 * Parse an expression.
8638 static expression_t *parse_expression(void)
8640 return parse_sub_expression(PREC_EXPRESSION);
8644 * Register a parser for a prefix-like operator.
8646 * @param parser the parser function
8647 * @param token_type the token type of the prefix token
8649 static void register_expression_parser(parse_expression_function parser,
8652 expression_parser_function_t *entry = &expression_parsers[token_type];
8654 if (entry->parser != NULL) {
8655 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8656 panic("trying to register multiple expression parsers for a token");
8658 entry->parser = parser;
8662 * Register a parser for an infix operator with given precedence.
8664 * @param parser the parser function
8665 * @param token_type the token type of the infix operator
8666 * @param precedence the precedence of the operator
8668 static void register_infix_parser(parse_expression_infix_function parser,
8669 int token_type, unsigned precedence)
8671 expression_parser_function_t *entry = &expression_parsers[token_type];
8673 if (entry->infix_parser != NULL) {
8674 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8675 panic("trying to register multiple infix expression parsers for a "
8678 entry->infix_parser = parser;
8679 entry->infix_precedence = precedence;
8683 * Initialize the expression parsers.
8685 static void init_expression_parsers(void)
8687 memset(&expression_parsers, 0, sizeof(expression_parsers));
8689 register_infix_parser(parse_array_expression, '[', PREC_POSTFIX);
8690 register_infix_parser(parse_call_expression, '(', PREC_POSTFIX);
8691 register_infix_parser(parse_select_expression, '.', PREC_POSTFIX);
8692 register_infix_parser(parse_select_expression, T_MINUSGREATER, PREC_POSTFIX);
8693 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT, T_PLUSPLUS, PREC_POSTFIX);
8694 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT, T_MINUSMINUS, PREC_POSTFIX);
8695 register_infix_parser(parse_EXPR_BINARY_MUL, '*', PREC_MULTIPLICATIVE);
8696 register_infix_parser(parse_EXPR_BINARY_DIV, '/', PREC_MULTIPLICATIVE);
8697 register_infix_parser(parse_EXPR_BINARY_MOD, '%', PREC_MULTIPLICATIVE);
8698 register_infix_parser(parse_EXPR_BINARY_ADD, '+', PREC_ADDITIVE);
8699 register_infix_parser(parse_EXPR_BINARY_SUB, '-', PREC_ADDITIVE);
8700 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, PREC_SHIFT);
8701 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, PREC_SHIFT);
8702 register_infix_parser(parse_EXPR_BINARY_LESS, '<', PREC_RELATIONAL);
8703 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', PREC_RELATIONAL);
8704 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, PREC_RELATIONAL);
8705 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, PREC_RELATIONAL);
8706 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, PREC_EQUALITY);
8707 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL, T_EXCLAMATIONMARKEQUAL, PREC_EQUALITY);
8708 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', PREC_AND);
8709 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', PREC_XOR);
8710 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', PREC_OR);
8711 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, PREC_LOGICAL_AND);
8712 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, PREC_LOGICAL_OR);
8713 register_infix_parser(parse_conditional_expression, '?', PREC_CONDITIONAL);
8714 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', PREC_ASSIGNMENT);
8715 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, PREC_ASSIGNMENT);
8716 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, PREC_ASSIGNMENT);
8717 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, PREC_ASSIGNMENT);
8718 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, PREC_ASSIGNMENT);
8719 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, PREC_ASSIGNMENT);
8720 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN, T_LESSLESSEQUAL, PREC_ASSIGNMENT);
8721 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN, T_GREATERGREATEREQUAL, PREC_ASSIGNMENT);
8722 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN, T_ANDEQUAL, PREC_ASSIGNMENT);
8723 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN, T_PIPEEQUAL, PREC_ASSIGNMENT);
8724 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN, T_CARETEQUAL, PREC_ASSIGNMENT);
8725 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', PREC_EXPRESSION);
8727 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-');
8728 register_expression_parser(parse_EXPR_UNARY_PLUS, '+');
8729 register_expression_parser(parse_EXPR_UNARY_NOT, '!');
8730 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~');
8731 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*');
8732 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&');
8733 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT, T_PLUSPLUS);
8734 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT, T_MINUSMINUS);
8735 register_expression_parser(parse_sizeof, T_sizeof);
8736 register_expression_parser(parse_alignof, T___alignof__);
8737 register_expression_parser(parse_extension, T___extension__);
8738 register_expression_parser(parse_builtin_classify_type, T___builtin_classify_type);
8742 * Parse a asm statement arguments specification.
8744 static asm_argument_t *parse_asm_arguments(bool is_out)
8746 asm_argument_t *result = NULL;
8747 asm_argument_t *last = NULL;
8749 while (token.type == T_STRING_LITERAL || token.type == '[') {
8750 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8751 memset(argument, 0, sizeof(argument[0]));
8753 if (token.type == '[') {
8755 if (token.type != T_IDENTIFIER) {
8756 parse_error_expected("while parsing asm argument",
8757 T_IDENTIFIER, NULL);
8760 argument->symbol = token.v.symbol;
8765 argument->constraints = parse_string_literals();
8767 add_anchor_token(')');
8768 expression_t *expression = parse_expression();
8769 rem_anchor_token(')');
8771 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8772 * change size or type representation (e.g. int -> long is ok, but
8773 * int -> float is not) */
8774 if (expression->kind == EXPR_UNARY_CAST) {
8775 type_t *const type = expression->base.type;
8776 type_kind_t const kind = type->kind;
8777 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8780 if (kind == TYPE_ATOMIC) {
8781 atomic_type_kind_t const akind = type->atomic.akind;
8782 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8783 size = get_atomic_type_size(akind);
8785 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8786 size = get_atomic_type_size(get_intptr_kind());
8790 expression_t *const value = expression->unary.value;
8791 type_t *const value_type = value->base.type;
8792 type_kind_t const value_kind = value_type->kind;
8794 unsigned value_flags;
8795 unsigned value_size;
8796 if (value_kind == TYPE_ATOMIC) {
8797 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8798 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8799 value_size = get_atomic_type_size(value_akind);
8800 } else if (value_kind == TYPE_POINTER) {
8801 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8802 value_size = get_atomic_type_size(get_intptr_kind());
8807 if (value_flags != flags || value_size != size)
8811 } while (expression->kind == EXPR_UNARY_CAST);
8815 if (!is_lvalue(expression)) {
8816 errorf(&expression->base.source_position,
8817 "asm output argument is not an lvalue");
8820 if (argument->constraints.begin[0] == '+')
8821 mark_decls_read(expression, NULL);
8823 mark_decls_read(expression, NULL);
8825 argument->expression = expression;
8828 set_address_taken(expression, true);
8831 last->next = argument;
8837 if (token.type != ',')
8848 * Parse a asm statement clobber specification.
8850 static asm_clobber_t *parse_asm_clobbers(void)
8852 asm_clobber_t *result = NULL;
8853 asm_clobber_t *last = NULL;
8855 while(token.type == T_STRING_LITERAL) {
8856 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8857 clobber->clobber = parse_string_literals();
8860 last->next = clobber;
8866 if (token.type != ',')
8875 * Parse an asm statement.
8877 static statement_t *parse_asm_statement(void)
8879 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8880 asm_statement_t *asm_statement = &statement->asms;
8884 if (token.type == T_volatile) {
8886 asm_statement->is_volatile = true;
8890 add_anchor_token(')');
8891 add_anchor_token(':');
8892 asm_statement->asm_text = parse_string_literals();
8894 if (token.type != ':') {
8895 rem_anchor_token(':');
8900 asm_statement->outputs = parse_asm_arguments(true);
8901 if (token.type != ':') {
8902 rem_anchor_token(':');
8907 asm_statement->inputs = parse_asm_arguments(false);
8908 if (token.type != ':') {
8909 rem_anchor_token(':');
8912 rem_anchor_token(':');
8915 asm_statement->clobbers = parse_asm_clobbers();
8918 rem_anchor_token(')');
8922 if (asm_statement->outputs == NULL) {
8923 /* GCC: An 'asm' instruction without any output operands will be treated
8924 * identically to a volatile 'asm' instruction. */
8925 asm_statement->is_volatile = true;
8930 return create_invalid_statement();
8934 * Parse a case statement.
8936 static statement_t *parse_case_statement(void)
8938 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8939 source_position_t *const pos = &statement->base.source_position;
8943 expression_t *const expression = parse_expression();
8944 statement->case_label.expression = expression;
8945 if (!is_constant_expression(expression)) {
8946 /* This check does not prevent the error message in all cases of an
8947 * prior error while parsing the expression. At least it catches the
8948 * common case of a mistyped enum entry. */
8949 if (is_type_valid(skip_typeref(expression->base.type))) {
8950 errorf(pos, "case label does not reduce to an integer constant");
8952 statement->case_label.is_bad = true;
8954 long const val = fold_constant(expression);
8955 statement->case_label.first_case = val;
8956 statement->case_label.last_case = val;
8960 if (token.type == T_DOTDOTDOT) {
8962 expression_t *const end_range = parse_expression();
8963 statement->case_label.end_range = end_range;
8964 if (!is_constant_expression(end_range)) {
8965 /* This check does not prevent the error message in all cases of an
8966 * prior error while parsing the expression. At least it catches the
8967 * common case of a mistyped enum entry. */
8968 if (is_type_valid(skip_typeref(end_range->base.type))) {
8969 errorf(pos, "case range does not reduce to an integer constant");
8971 statement->case_label.is_bad = true;
8973 long const val = fold_constant(end_range);
8974 statement->case_label.last_case = val;
8976 if (warning.other && val < statement->case_label.first_case) {
8977 statement->case_label.is_empty_range = true;
8978 warningf(pos, "empty range specified");
8984 PUSH_PARENT(statement);
8988 if (current_switch != NULL) {
8989 if (! statement->case_label.is_bad) {
8990 /* Check for duplicate case values */
8991 case_label_statement_t *c = &statement->case_label;
8992 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
8993 if (l->is_bad || l->is_empty_range || l->expression == NULL)
8996 if (c->last_case < l->first_case || c->first_case > l->last_case)
8999 errorf(pos, "duplicate case value (previously used %P)",
9000 &l->base.source_position);
9004 /* link all cases into the switch statement */
9005 if (current_switch->last_case == NULL) {
9006 current_switch->first_case = &statement->case_label;
9008 current_switch->last_case->next = &statement->case_label;
9010 current_switch->last_case = &statement->case_label;
9012 errorf(pos, "case label not within a switch statement");
9015 statement_t *const inner_stmt = parse_statement();
9016 statement->case_label.statement = inner_stmt;
9017 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9018 errorf(&inner_stmt->base.source_position, "declaration after case label");
9025 return create_invalid_statement();
9029 * Parse a default statement.
9031 static statement_t *parse_default_statement(void)
9033 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9037 PUSH_PARENT(statement);
9040 if (current_switch != NULL) {
9041 const case_label_statement_t *def_label = current_switch->default_label;
9042 if (def_label != NULL) {
9043 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
9044 &def_label->base.source_position);
9046 current_switch->default_label = &statement->case_label;
9048 /* link all cases into the switch statement */
9049 if (current_switch->last_case == NULL) {
9050 current_switch->first_case = &statement->case_label;
9052 current_switch->last_case->next = &statement->case_label;
9054 current_switch->last_case = &statement->case_label;
9057 errorf(&statement->base.source_position,
9058 "'default' label not within a switch statement");
9061 statement_t *const inner_stmt = parse_statement();
9062 statement->case_label.statement = inner_stmt;
9063 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9064 errorf(&inner_stmt->base.source_position, "declaration after default label");
9071 return create_invalid_statement();
9075 * Parse a label statement.
9077 static statement_t *parse_label_statement(void)
9079 assert(token.type == T_IDENTIFIER);
9080 symbol_t *symbol = token.v.symbol;
9081 declaration_t *label = get_label(symbol);
9083 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
9084 statement->label.label = label;
9088 PUSH_PARENT(statement);
9090 /* if statement is already set then the label is defined twice,
9091 * otherwise it was just mentioned in a goto/local label declaration so far */
9092 if (label->init.statement != NULL) {
9093 errorf(HERE, "duplicate label '%Y' (declared %P)",
9094 symbol, &label->source_position);
9096 label->source_position = token.source_position;
9097 label->init.statement = statement;
9102 if (token.type == '}') {
9103 /* TODO only warn? */
9104 if (warning.other && false) {
9105 warningf(HERE, "label at end of compound statement");
9106 statement->label.statement = create_empty_statement();
9108 errorf(HERE, "label at end of compound statement");
9109 statement->label.statement = create_invalid_statement();
9111 } else if (token.type == ';') {
9112 /* Eat an empty statement here, to avoid the warning about an empty
9113 * statement after a label. label:; is commonly used to have a label
9114 * before a closing brace. */
9115 statement->label.statement = create_empty_statement();
9118 statement_t *const inner_stmt = parse_statement();
9119 statement->label.statement = inner_stmt;
9120 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9121 errorf(&inner_stmt->base.source_position, "declaration after label");
9125 /* remember the labels in a list for later checking */
9126 if (label_last == NULL) {
9127 label_first = &statement->label;
9129 label_last->next = &statement->label;
9131 label_last = &statement->label;
9138 * Parse an if statement.
9140 static statement_t *parse_if(void)
9142 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
9146 PUSH_PARENT(statement);
9148 add_anchor_token('{');
9151 add_anchor_token(')');
9152 expression_t *const expr = parse_expression();
9153 statement->ifs.condition = expr;
9154 mark_decls_read(expr, NULL);
9155 rem_anchor_token(')');
9159 rem_anchor_token('{');
9161 add_anchor_token(T_else);
9162 statement->ifs.true_statement = parse_statement();
9163 rem_anchor_token(T_else);
9165 if (token.type == T_else) {
9167 statement->ifs.false_statement = parse_statement();
9175 * Check that all enums are handled in a switch.
9177 * @param statement the switch statement to check
9179 static void check_enum_cases(const switch_statement_t *statement) {
9180 const type_t *type = skip_typeref(statement->expression->base.type);
9181 if (! is_type_enum(type))
9183 const enum_type_t *enumt = &type->enumt;
9185 /* if we have a default, no warnings */
9186 if (statement->default_label != NULL)
9189 /* FIXME: calculation of value should be done while parsing */
9190 const declaration_t *declaration;
9191 long last_value = -1;
9192 for (declaration = enumt->declaration->next;
9193 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
9194 declaration = declaration->next) {
9195 const expression_t *expression = declaration->init.enum_value;
9196 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
9198 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
9199 if (l->expression == NULL)
9201 if (l->first_case <= value && value <= l->last_case) {
9207 warningf(&statement->base.source_position,
9208 "enumeration value '%Y' not handled in switch", declaration->symbol);
9215 * Parse a switch statement.
9217 static statement_t *parse_switch(void)
9219 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
9223 PUSH_PARENT(statement);
9226 add_anchor_token(')');
9227 expression_t *const expr = parse_expression();
9228 mark_decls_read(expr, NULL);
9229 type_t * type = skip_typeref(expr->base.type);
9230 if (is_type_integer(type)) {
9231 type = promote_integer(type);
9232 if (warning.traditional) {
9233 if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
9234 warningf(&expr->base.source_position,
9235 "'%T' switch expression not converted to '%T' in ISO C",
9239 } else if (is_type_valid(type)) {
9240 errorf(&expr->base.source_position,
9241 "switch quantity is not an integer, but '%T'", type);
9242 type = type_error_type;
9244 statement->switchs.expression = create_implicit_cast(expr, type);
9246 rem_anchor_token(')');
9248 switch_statement_t *rem = current_switch;
9249 current_switch = &statement->switchs;
9250 statement->switchs.body = parse_statement();
9251 current_switch = rem;
9253 if (warning.switch_default &&
9254 statement->switchs.default_label == NULL) {
9255 warningf(&statement->base.source_position, "switch has no default case");
9257 if (warning.switch_enum)
9258 check_enum_cases(&statement->switchs);
9264 return create_invalid_statement();
9267 static statement_t *parse_loop_body(statement_t *const loop)
9269 statement_t *const rem = current_loop;
9270 current_loop = loop;
9272 statement_t *const body = parse_statement();
9279 * Parse a while statement.
9281 static statement_t *parse_while(void)
9283 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
9287 PUSH_PARENT(statement);
9290 add_anchor_token(')');
9291 expression_t *const cond = parse_expression();
9292 statement->whiles.condition = cond;
9293 mark_decls_read(cond, NULL);
9294 rem_anchor_token(')');
9297 statement->whiles.body = parse_loop_body(statement);
9303 return create_invalid_statement();
9307 * Parse a do statement.
9309 static statement_t *parse_do(void)
9311 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
9315 PUSH_PARENT(statement);
9317 add_anchor_token(T_while);
9318 statement->do_while.body = parse_loop_body(statement);
9319 rem_anchor_token(T_while);
9323 add_anchor_token(')');
9324 expression_t *const cond = parse_expression();
9325 statement->do_while.condition = cond;
9326 mark_decls_read(cond, NULL);
9327 rem_anchor_token(')');
9335 return create_invalid_statement();
9339 * Parse a for statement.
9341 static statement_t *parse_for(void)
9343 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
9347 PUSH_PARENT(statement);
9349 size_t const top = environment_top();
9350 scope_push(&statement->fors.scope);
9353 add_anchor_token(')');
9355 if (token.type != ';') {
9356 if (is_declaration_specifier(&token, false)) {
9357 parse_declaration(record_declaration);
9359 add_anchor_token(';');
9360 expression_t *const init = parse_expression();
9361 statement->fors.initialisation = init;
9362 mark_decls_read(init, DECL_ANY);
9363 if (warning.unused_value && !expression_has_effect(init)) {
9364 warningf(&init->base.source_position,
9365 "initialisation of 'for'-statement has no effect");
9367 rem_anchor_token(';');
9374 if (token.type != ';') {
9375 add_anchor_token(';');
9376 expression_t *const cond = parse_expression();
9377 statement->fors.condition = cond;
9378 mark_decls_read(cond, NULL);
9379 rem_anchor_token(';');
9382 if (token.type != ')') {
9383 expression_t *const step = parse_expression();
9384 statement->fors.step = step;
9385 mark_decls_read(step, DECL_ANY);
9386 if (warning.unused_value && !expression_has_effect(step)) {
9387 warningf(&step->base.source_position,
9388 "step of 'for'-statement has no effect");
9391 rem_anchor_token(')');
9393 statement->fors.body = parse_loop_body(statement);
9395 assert(scope == &statement->fors.scope);
9397 environment_pop_to(top);
9404 rem_anchor_token(')');
9405 assert(scope == &statement->fors.scope);
9407 environment_pop_to(top);
9409 return create_invalid_statement();
9413 * Parse a goto statement.
9415 static statement_t *parse_goto(void)
9417 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
9420 if (GNU_MODE && token.type == '*') {
9422 expression_t *expression = parse_expression();
9423 mark_decls_read(expression, NULL);
9425 /* Argh: although documentation say the expression must be of type void *,
9426 * gcc excepts anything that can be casted into void * without error */
9427 type_t *type = expression->base.type;
9429 if (type != type_error_type) {
9430 if (!is_type_pointer(type) && !is_type_integer(type)) {
9431 errorf(&expression->base.source_position,
9432 "cannot convert to a pointer type");
9433 } else if (warning.other && type != type_void_ptr) {
9434 warningf(&expression->base.source_position,
9435 "type of computed goto expression should be 'void*' not '%T'", type);
9437 expression = create_implicit_cast(expression, type_void_ptr);
9440 statement->gotos.expression = expression;
9442 if (token.type != T_IDENTIFIER) {
9444 parse_error_expected("while parsing goto", T_IDENTIFIER, '*', NULL);
9446 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
9450 symbol_t *symbol = token.v.symbol;
9453 statement->gotos.label = get_label(symbol);
9456 /* remember the goto's in a list for later checking */
9457 if (goto_last == NULL) {
9458 goto_first = &statement->gotos;
9460 goto_last->next = &statement->gotos;
9462 goto_last = &statement->gotos;
9468 return create_invalid_statement();
9472 * Parse a continue statement.
9474 static statement_t *parse_continue(void)
9476 if (current_loop == NULL) {
9477 errorf(HERE, "continue statement not within loop");
9480 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
9490 * Parse a break statement.
9492 static statement_t *parse_break(void)
9494 if (current_switch == NULL && current_loop == NULL) {
9495 errorf(HERE, "break statement not within loop or switch");
9498 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
9508 * Parse a __leave statement.
9510 static statement_t *parse_leave_statement(void)
9512 if (current_try == NULL) {
9513 errorf(HERE, "__leave statement not within __try");
9516 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
9526 * Check if a given declaration represents a local variable.
9528 static bool is_local_var_declaration(const declaration_t *declaration)
9530 switch ((storage_class_tag_t) declaration->storage_class) {
9531 case STORAGE_CLASS_AUTO:
9532 case STORAGE_CLASS_REGISTER: {
9533 const type_t *type = skip_typeref(declaration->type);
9534 if (is_type_function(type)) {
9546 * Check if a given declaration represents a variable.
9548 static bool is_var_declaration(const declaration_t *declaration)
9550 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
9553 const type_t *type = skip_typeref(declaration->type);
9554 return !is_type_function(type);
9558 * Check if a given expression represents a local variable.
9560 static bool is_local_variable(const expression_t *expression)
9562 if (expression->base.kind != EXPR_REFERENCE) {
9565 const declaration_t *declaration = expression->reference.declaration;
9566 return is_local_var_declaration(declaration);
9570 * Check if a given expression represents a local variable and
9571 * return its declaration then, else return NULL.
9573 declaration_t *expr_is_variable(const expression_t *expression)
9575 if (expression->base.kind != EXPR_REFERENCE) {
9578 declaration_t *declaration = expression->reference.declaration;
9579 if (is_var_declaration(declaration))
9585 * Parse a return statement.
9587 static statement_t *parse_return(void)
9591 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
9593 expression_t *return_value = NULL;
9594 if (token.type != ';') {
9595 return_value = parse_expression();
9596 mark_decls_read(return_value, NULL);
9599 const type_t *const func_type = current_function->type;
9600 assert(is_type_function(func_type));
9601 type_t *const return_type = skip_typeref(func_type->function.return_type);
9603 if (return_value != NULL) {
9604 type_t *return_value_type = skip_typeref(return_value->base.type);
9606 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID) &&
9607 !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
9608 if (warning.other) {
9609 warningf(&statement->base.source_position,
9610 "'return' with a value, in function returning void");
9612 return_value = NULL;
9614 assign_error_t error = semantic_assign(return_type, return_value);
9615 report_assign_error(error, return_type, return_value, "'return'",
9616 &statement->base.source_position);
9617 return_value = create_implicit_cast(return_value, return_type);
9619 /* check for returning address of a local var */
9620 if (warning.other &&
9621 return_value != NULL &&
9622 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9623 const expression_t *expression = return_value->unary.value;
9624 if (is_local_variable(expression)) {
9625 warningf(&statement->base.source_position,
9626 "function returns address of local variable");
9629 } else if (warning.other && !is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9630 warningf(&statement->base.source_position,
9631 "'return' without value, in function returning non-void");
9633 statement->returns.value = return_value;
9642 * Parse a declaration statement.
9644 static statement_t *parse_declaration_statement(void)
9646 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9648 declaration_t *before = last_declaration;
9650 parse_external_declaration();
9652 parse_declaration(record_declaration);
9654 if (before == NULL) {
9655 statement->declaration.declarations_begin = scope->declarations;
9657 statement->declaration.declarations_begin = before->next;
9659 statement->declaration.declarations_end = last_declaration;
9665 * Parse an expression statement, ie. expr ';'.
9667 static statement_t *parse_expression_statement(void)
9669 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9671 expression_t *const expr = parse_expression();
9672 statement->expression.expression = expr;
9673 mark_decls_read(expr, DECL_ANY);
9682 * Parse a microsoft __try { } __finally { } or
9683 * __try{ } __except() { }
9685 static statement_t *parse_ms_try_statment(void)
9687 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9690 PUSH_PARENT(statement);
9692 ms_try_statement_t *rem = current_try;
9693 current_try = &statement->ms_try;
9694 statement->ms_try.try_statement = parse_compound_statement(false);
9699 if (token.type == T___except) {
9702 add_anchor_token(')');
9703 expression_t *const expr = parse_expression();
9704 mark_decls_read(expr, NULL);
9705 type_t * type = skip_typeref(expr->base.type);
9706 if (is_type_integer(type)) {
9707 type = promote_integer(type);
9708 } else if (is_type_valid(type)) {
9709 errorf(&expr->base.source_position,
9710 "__expect expression is not an integer, but '%T'", type);
9711 type = type_error_type;
9713 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9714 rem_anchor_token(')');
9716 statement->ms_try.final_statement = parse_compound_statement(false);
9717 } else if (token.type == T__finally) {
9719 statement->ms_try.final_statement = parse_compound_statement(false);
9721 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9722 return create_invalid_statement();
9726 return create_invalid_statement();
9729 static statement_t *parse_empty_statement(void)
9731 if (warning.empty_statement) {
9732 warningf(HERE, "statement is empty");
9734 statement_t *const statement = create_empty_statement();
9739 static statement_t *parse_local_label_declaration(void) {
9740 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9744 declaration_t *begin = NULL, *end = NULL;
9747 if (token.type != T_IDENTIFIER) {
9748 parse_error_expected("while parsing local label declaration",
9749 T_IDENTIFIER, NULL);
9752 symbol_t *symbol = token.v.symbol;
9753 declaration_t *declaration = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
9754 if (declaration != NULL) {
9755 errorf(HERE, "multiple definitions of '__label__ %Y' (previous definition at %P)",
9756 symbol, &declaration->source_position);
9758 declaration = allocate_declaration_zero();
9759 declaration->namespc = NAMESPACE_LOCAL_LABEL;
9760 declaration->source_position = token.source_position;
9761 declaration->symbol = symbol;
9762 declaration->parent_scope = scope;
9763 declaration->init.statement = NULL;
9766 end->next = declaration;
9769 begin = declaration;
9771 local_label_push(declaration);
9775 if (token.type != ',')
9781 statement->declaration.declarations_begin = begin;
9782 statement->declaration.declarations_end = end;
9787 * Parse a statement.
9788 * There's also parse_statement() which additionally checks for
9789 * "statement has no effect" warnings
9791 static statement_t *intern_parse_statement(void)
9793 statement_t *statement = NULL;
9795 /* declaration or statement */
9796 add_anchor_token(';');
9797 switch (token.type) {
9798 case T_IDENTIFIER: {
9799 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9800 if (la1_type == ':') {
9801 statement = parse_label_statement();
9802 } else if (is_typedef_symbol(token.v.symbol)) {
9803 statement = parse_declaration_statement();
9804 } else switch (la1_type) {
9806 if (get_declaration(token.v.symbol, NAMESPACE_NORMAL) != NULL)
9807 goto expression_statment;
9812 statement = parse_declaration_statement();
9816 expression_statment:
9817 statement = parse_expression_statement();
9823 case T___extension__:
9824 /* This can be a prefix to a declaration or an expression statement.
9825 * We simply eat it now and parse the rest with tail recursion. */
9828 } while (token.type == T___extension__);
9829 bool old_gcc_extension = in_gcc_extension;
9830 in_gcc_extension = true;
9831 statement = parse_statement();
9832 in_gcc_extension = old_gcc_extension;
9836 statement = parse_declaration_statement();
9840 statement = parse_local_label_declaration();
9843 case ';': statement = parse_empty_statement(); break;
9844 case '{': statement = parse_compound_statement(false); break;
9845 case T___leave: statement = parse_leave_statement(); break;
9846 case T___try: statement = parse_ms_try_statment(); break;
9847 case T_asm: statement = parse_asm_statement(); break;
9848 case T_break: statement = parse_break(); break;
9849 case T_case: statement = parse_case_statement(); break;
9850 case T_continue: statement = parse_continue(); break;
9851 case T_default: statement = parse_default_statement(); break;
9852 case T_do: statement = parse_do(); break;
9853 case T_for: statement = parse_for(); break;
9854 case T_goto: statement = parse_goto(); break;
9855 case T_if: statement = parse_if(); break;
9856 case T_return: statement = parse_return(); break;
9857 case T_switch: statement = parse_switch(); break;
9858 case T_while: statement = parse_while(); break;
9868 case T_CHARACTER_CONSTANT:
9869 case T_FLOATINGPOINT:
9873 case T_STRING_LITERAL:
9874 case T_WIDE_CHARACTER_CONSTANT:
9875 case T_WIDE_STRING_LITERAL:
9876 case T___FUNCDNAME__:
9878 case T___FUNCTION__:
9879 case T___PRETTY_FUNCTION__:
9881 case T___builtin_alloca:
9882 case T___builtin_classify_type:
9883 case T___builtin_constant_p:
9884 case T___builtin_expect:
9885 case T___builtin_huge_val:
9886 case T___builtin_inf:
9887 case T___builtin_inff:
9888 case T___builtin_infl:
9889 case T___builtin_isgreater:
9890 case T___builtin_isgreaterequal:
9891 case T___builtin_isless:
9892 case T___builtin_islessequal:
9893 case T___builtin_islessgreater:
9894 case T___builtin_isunordered:
9895 case T___builtin_nan:
9896 case T___builtin_nanf:
9897 case T___builtin_nanl:
9898 case T___builtin_offsetof:
9899 case T___builtin_prefetch:
9900 case T___builtin_va_arg:
9901 case T___builtin_va_end:
9902 case T___builtin_va_start:
9907 statement = parse_expression_statement();
9911 errorf(HERE, "unexpected token %K while parsing statement", &token);
9912 statement = create_invalid_statement();
9917 rem_anchor_token(';');
9919 assert(statement != NULL
9920 && statement->base.source_position.input_name != NULL);
9926 * parse a statement and emits "statement has no effect" warning if needed
9927 * (This is really a wrapper around intern_parse_statement with check for 1
9928 * single warning. It is needed, because for statement expressions we have
9929 * to avoid the warning on the last statement)
9931 static statement_t *parse_statement(void)
9933 statement_t *statement = intern_parse_statement();
9935 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9936 expression_t *expression = statement->expression.expression;
9937 if (!expression_has_effect(expression)) {
9938 warningf(&expression->base.source_position,
9939 "statement has no effect");
9947 * Parse a compound statement.
9949 static statement_t *parse_compound_statement(bool inside_expression_statement)
9951 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
9953 PUSH_PARENT(statement);
9956 add_anchor_token('}');
9958 size_t const top = environment_top();
9959 size_t const top_local = local_label_top();
9960 scope_push(&statement->compound.scope);
9962 statement_t **anchor = &statement->compound.statements;
9963 bool only_decls_so_far = true;
9964 while (token.type != '}') {
9965 if (token.type == T_EOF) {
9966 errorf(&statement->base.source_position,
9967 "EOF while parsing compound statement");
9970 statement_t *sub_statement = intern_parse_statement();
9971 if (is_invalid_statement(sub_statement)) {
9972 /* an error occurred. if we are at an anchor, return */
9978 if (warning.declaration_after_statement) {
9979 if (sub_statement->kind != STATEMENT_DECLARATION) {
9980 only_decls_so_far = false;
9981 } else if (!only_decls_so_far) {
9982 warningf(&sub_statement->base.source_position,
9983 "ISO C90 forbids mixed declarations and code");
9987 *anchor = sub_statement;
9989 while (sub_statement->base.next != NULL)
9990 sub_statement = sub_statement->base.next;
9992 anchor = &sub_statement->base.next;
9996 /* look over all statements again to produce no effect warnings */
9997 if (warning.unused_value) {
9998 statement_t *sub_statement = statement->compound.statements;
9999 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
10000 if (sub_statement->kind != STATEMENT_EXPRESSION)
10002 /* don't emit a warning for the last expression in an expression
10003 * statement as it has always an effect */
10004 if (inside_expression_statement && sub_statement->base.next == NULL)
10007 expression_t *expression = sub_statement->expression.expression;
10008 if (!expression_has_effect(expression)) {
10009 warningf(&expression->base.source_position,
10010 "statement has no effect");
10016 rem_anchor_token('}');
10017 assert(scope == &statement->compound.scope);
10019 environment_pop_to(top);
10020 local_label_pop_to(top_local);
10027 * Initialize builtin types.
10029 static void initialize_builtin_types(void)
10031 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
10032 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
10033 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
10034 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
10035 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
10036 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
10037 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
10038 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
10040 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
10041 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
10042 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
10043 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
10045 /* const version of wchar_t */
10046 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF);
10047 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
10048 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
10050 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
10054 * Check for unused global static functions and variables
10056 static void check_unused_globals(void)
10058 if (!warning.unused_function && !warning.unused_variable)
10061 for (const declaration_t *decl = file_scope->declarations; decl != NULL; decl = decl->next) {
10063 decl->modifiers & DM_UNUSED ||
10064 decl->modifiers & DM_USED ||
10065 decl->storage_class != STORAGE_CLASS_STATIC)
10068 type_t *const type = decl->type;
10070 if (is_type_function(skip_typeref(type))) {
10071 if (!warning.unused_function || decl->is_inline)
10074 s = (decl->init.statement != NULL ? "defined" : "declared");
10076 if (!warning.unused_variable)
10082 warningf(&decl->source_position, "'%#T' %s but not used",
10083 type, decl->symbol, s);
10087 static void parse_global_asm(void)
10089 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
10094 statement->asms.asm_text = parse_string_literals();
10095 statement->base.next = unit->global_asm;
10096 unit->global_asm = statement;
10105 * Parse a translation unit.
10107 static void parse_translation_unit(void)
10109 add_anchor_token(T_EOF);
10112 unsigned char token_anchor_copy[T_LAST_TOKEN];
10113 memcpy(token_anchor_copy, token_anchor_set, sizeof(token_anchor_copy));
10117 bool anchor_leak = false;
10118 for (int i = 0; i != T_LAST_TOKEN; ++i) {
10119 unsigned char count = token_anchor_set[i] - token_anchor_copy[i];
10121 errorf(HERE, "Leaked anchor token %k %d times", i, count);
10122 anchor_leak = true;
10125 if (in_gcc_extension) {
10126 errorf(HERE, "Leaked __extension__");
10127 anchor_leak = true;
10134 switch (token.type) {
10137 case T___extension__:
10138 parse_external_declaration();
10142 parse_global_asm();
10146 rem_anchor_token(T_EOF);
10150 if (!strict_mode) {
10152 warningf(HERE, "stray ';' outside of function");
10159 errorf(HERE, "stray %K outside of function", &token);
10160 if (token.type == '(' || token.type == '{' || token.type == '[')
10161 eat_until_matching_token(token.type);
10171 * @return the translation unit or NULL if errors occurred.
10173 void start_parsing(void)
10175 environment_stack = NEW_ARR_F(stack_entry_t, 0);
10176 label_stack = NEW_ARR_F(stack_entry_t, 0);
10177 local_label_stack = NEW_ARR_F(stack_entry_t, 0);
10178 diagnostic_count = 0;
10182 type_set_output(stderr);
10183 ast_set_output(stderr);
10185 assert(unit == NULL);
10186 unit = allocate_ast_zero(sizeof(unit[0]));
10188 assert(file_scope == NULL);
10189 file_scope = &unit->scope;
10191 assert(scope == NULL);
10192 scope_push(&unit->scope);
10194 initialize_builtin_types();
10197 translation_unit_t *finish_parsing(void)
10199 /* do NOT use scope_pop() here, this will crash, will it by hand */
10200 assert(scope == &unit->scope);
10202 last_declaration = NULL;
10204 assert(file_scope == &unit->scope);
10205 check_unused_globals();
10208 DEL_ARR_F(environment_stack);
10209 DEL_ARR_F(label_stack);
10210 DEL_ARR_F(local_label_stack);
10212 translation_unit_t *result = unit;
10219 lookahead_bufpos = 0;
10220 for (int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
10223 parse_translation_unit();
10227 * Initialize the parser.
10229 void init_parser(void)
10231 sym_anonymous = symbol_table_insert("<anonymous>");
10233 if (c_mode & _MS) {
10234 /* add predefined symbols for extended-decl-modifier */
10235 sym_align = symbol_table_insert("align");
10236 sym_allocate = symbol_table_insert("allocate");
10237 sym_dllimport = symbol_table_insert("dllimport");
10238 sym_dllexport = symbol_table_insert("dllexport");
10239 sym_naked = symbol_table_insert("naked");
10240 sym_noinline = symbol_table_insert("noinline");
10241 sym_noreturn = symbol_table_insert("noreturn");
10242 sym_nothrow = symbol_table_insert("nothrow");
10243 sym_novtable = symbol_table_insert("novtable");
10244 sym_property = symbol_table_insert("property");
10245 sym_get = symbol_table_insert("get");
10246 sym_put = symbol_table_insert("put");
10247 sym_selectany = symbol_table_insert("selectany");
10248 sym_thread = symbol_table_insert("thread");
10249 sym_uuid = symbol_table_insert("uuid");
10250 sym_deprecated = symbol_table_insert("deprecated");
10251 sym_restrict = symbol_table_insert("restrict");
10252 sym_noalias = symbol_table_insert("noalias");
10254 memset(token_anchor_set, 0, sizeof(token_anchor_set));
10256 init_expression_parsers();
10257 obstack_init(&temp_obst);
10259 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
10260 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
10264 * Terminate the parser.
10266 void exit_parser(void)
10268 obstack_free(&temp_obst, NULL);