2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
37 #include "walk_statements.h"
39 #include "adt/bitfiddle.h"
40 #include "adt/error.h"
41 #include "adt/array.h"
43 /** if wchar_t is equal to unsigned short. */
44 bool opt_short_wchar_t =
51 //#define PRINT_TOKENS
52 #define MAX_LOOKAHEAD 2
55 declaration_t *old_declaration;
57 unsigned short namespc;
60 typedef struct argument_list_t argument_list_t;
61 struct argument_list_t {
63 argument_list_t *next;
66 typedef struct gnu_attribute_t gnu_attribute_t;
67 struct gnu_attribute_t {
68 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
69 gnu_attribute_t *next;
70 bool invalid; /**< Set if this attribute had argument errors, */
71 bool have_arguments; /**< True, if this attribute has arguments. */
75 atomic_type_kind_t akind;
76 long argument; /**< Single argument. */
77 argument_list_t *arguments; /**< List of argument expressions. */
81 typedef struct declaration_specifiers_t declaration_specifiers_t;
82 struct declaration_specifiers_t {
83 source_position_t source_position;
84 unsigned char declared_storage_class;
85 unsigned char alignment; /**< Alignment, 0 if not set. */
86 unsigned int is_inline : 1;
87 unsigned int deprecated : 1;
88 decl_modifiers_t modifiers; /**< declaration modifiers */
89 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
90 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
91 symbol_t *get_property_sym; /**< the name of the get property if set. */
92 symbol_t *put_property_sym; /**< the name of the put property if set. */
97 * An environment for parsing initializers (and compound literals).
99 typedef struct parse_initializer_env_t {
100 type_t *type; /**< the type of the initializer. In case of an
101 array type with unspecified size this gets
102 adjusted to the actual size. */
103 declaration_t *declaration; /**< the declaration that is initialized if any */
104 bool must_be_constant;
105 } parse_initializer_env_t;
107 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration, bool is_definition);
109 /** The current token. */
110 static token_t token;
111 /** The lookahead ring-buffer. */
112 static token_t lookahead_buffer[MAX_LOOKAHEAD];
113 /** Position of the next token in the lookahead buffer. */
114 static int lookahead_bufpos;
115 static stack_entry_t *environment_stack = NULL;
116 static stack_entry_t *label_stack = NULL;
117 static stack_entry_t *local_label_stack = NULL;
118 /** The global file scope. */
119 static scope_t *file_scope = NULL;
120 /** The current scope. */
121 static scope_t *scope = NULL;
122 static declaration_t *last_declaration = NULL;
123 /** Point to the current function declaration if inside a function. */
124 static declaration_t *current_function = NULL;
125 static declaration_t *current_init_decl = NULL;
126 static switch_statement_t *current_switch = NULL;
127 static statement_t *current_loop = NULL;
128 static statement_t *current_parent = NULL;
129 static ms_try_statement_t *current_try = NULL;
130 static goto_statement_t *goto_first = NULL;
131 static goto_statement_t *goto_last = NULL;
132 static label_statement_t *label_first = NULL;
133 static label_statement_t *label_last = NULL;
134 /** current translation unit. */
135 static translation_unit_t *unit = NULL;
136 /** true if we are in a type property context (evaluation only for type. */
137 static bool in_type_prop = false;
138 /** true in we are in a __extension__ context. */
139 static bool in_gcc_extension = false;
140 static struct obstack temp_obst;
143 #define PUSH_PARENT(stmt) \
144 statement_t *const prev_parent = current_parent; \
145 ((void)(current_parent = (stmt)))
146 #define POP_PARENT ((void)(current_parent = prev_parent))
148 static source_position_t null_position = { NULL, 0 };
150 /** special symbol used for anonymous entities. */
151 static const symbol_t *sym_anonymous = NULL;
153 /* symbols for Microsoft extended-decl-modifier */
154 static const symbol_t *sym_align = NULL;
155 static const symbol_t *sym_allocate = NULL;
156 static const symbol_t *sym_dllimport = NULL;
157 static const symbol_t *sym_dllexport = NULL;
158 static const symbol_t *sym_naked = NULL;
159 static const symbol_t *sym_noinline = NULL;
160 static const symbol_t *sym_noreturn = NULL;
161 static const symbol_t *sym_nothrow = NULL;
162 static const symbol_t *sym_novtable = NULL;
163 static const symbol_t *sym_property = NULL;
164 static const symbol_t *sym_get = NULL;
165 static const symbol_t *sym_put = NULL;
166 static const symbol_t *sym_selectany = NULL;
167 static const symbol_t *sym_thread = NULL;
168 static const symbol_t *sym_uuid = NULL;
169 static const symbol_t *sym_deprecated = NULL;
170 static const symbol_t *sym_restrict = NULL;
171 static const symbol_t *sym_noalias = NULL;
173 /** The token anchor set */
174 static unsigned char token_anchor_set[T_LAST_TOKEN];
176 /** The current source position. */
177 #define HERE (&token.source_position)
179 /** true if we are in GCC mode. */
180 #define GNU_MODE ((c_mode & _GNUC) || in_gcc_extension)
182 static type_t *type_valist;
184 static statement_t *parse_compound_statement(bool inside_expression_statement);
185 static statement_t *parse_statement(void);
187 static expression_t *parse_sub_expression(precedence_t);
188 static expression_t *parse_expression(void);
189 static type_t *parse_typename(void);
191 static void parse_compound_type_entries(declaration_t *compound_declaration);
192 static declaration_t *parse_declarator(
193 const declaration_specifiers_t *specifiers, bool may_be_abstract);
194 static declaration_t *record_declaration(declaration_t *declaration, bool is_definition);
196 static void semantic_comparison(binary_expression_t *expression);
198 #define STORAGE_CLASSES \
206 #define TYPE_QUALIFIERS \
211 case T__forceinline: \
212 case T___attribute__:
214 #ifdef PROVIDE_COMPLEX
215 #define COMPLEX_SPECIFIERS \
217 #define IMAGINARY_SPECIFIERS \
220 #define COMPLEX_SPECIFIERS
221 #define IMAGINARY_SPECIFIERS
224 #define TYPE_SPECIFIERS \
239 case T___builtin_va_list: \
244 #define DECLARATION_START \
249 #define TYPENAME_START \
254 * Allocate an AST node with given size and
255 * initialize all fields with zero.
257 static void *allocate_ast_zero(size_t size)
259 void *res = allocate_ast(size);
260 memset(res, 0, size);
264 static declaration_t *allocate_declaration_zero(void)
266 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
267 declaration->type = type_error_type;
268 declaration->alignment = 0;
273 * Returns the size of a statement node.
275 * @param kind the statement kind
277 static size_t get_statement_struct_size(statement_kind_t kind)
279 static const size_t sizes[] = {
280 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
281 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
282 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
283 [STATEMENT_RETURN] = sizeof(return_statement_t),
284 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
285 [STATEMENT_IF] = sizeof(if_statement_t),
286 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
287 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
288 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
289 [STATEMENT_BREAK] = sizeof(statement_base_t),
290 [STATEMENT_GOTO] = sizeof(goto_statement_t),
291 [STATEMENT_LABEL] = sizeof(label_statement_t),
292 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
293 [STATEMENT_WHILE] = sizeof(while_statement_t),
294 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
295 [STATEMENT_FOR] = sizeof(for_statement_t),
296 [STATEMENT_ASM] = sizeof(asm_statement_t),
297 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
298 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
300 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
301 assert(sizes[kind] != 0);
306 * Returns the size of an expression node.
308 * @param kind the expression kind
310 static size_t get_expression_struct_size(expression_kind_t kind)
312 static const size_t sizes[] = {
313 [EXPR_INVALID] = sizeof(expression_base_t),
314 [EXPR_REFERENCE] = sizeof(reference_expression_t),
315 [EXPR_CONST] = sizeof(const_expression_t),
316 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
317 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
318 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
319 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
320 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
321 [EXPR_CALL] = sizeof(call_expression_t),
322 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
323 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
324 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
325 [EXPR_SELECT] = sizeof(select_expression_t),
326 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
327 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
328 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
329 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
330 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
331 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
332 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
333 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
334 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
335 [EXPR_VA_START] = sizeof(va_start_expression_t),
336 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
337 [EXPR_STATEMENT] = sizeof(statement_expression_t),
338 [EXPR_LABEL_ADDRESS] = sizeof(label_address_expression_t),
340 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
341 return sizes[EXPR_UNARY_FIRST];
343 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
344 return sizes[EXPR_BINARY_FIRST];
346 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
347 assert(sizes[kind] != 0);
352 * Allocate a statement node of given kind and initialize all
355 static statement_t *allocate_statement_zero(statement_kind_t kind)
357 size_t size = get_statement_struct_size(kind);
358 statement_t *res = allocate_ast_zero(size);
360 res->base.kind = kind;
361 res->base.parent = current_parent;
362 res->base.source_position = token.source_position;
367 * Allocate an expression node of given kind and initialize all
370 static expression_t *allocate_expression_zero(expression_kind_t kind)
372 size_t size = get_expression_struct_size(kind);
373 expression_t *res = allocate_ast_zero(size);
375 res->base.kind = kind;
376 res->base.type = type_error_type;
381 * Creates a new invalid expression.
383 static expression_t *create_invalid_expression(void)
385 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
386 expression->base.source_position = token.source_position;
391 * Creates a new invalid statement.
393 static statement_t *create_invalid_statement(void)
395 return allocate_statement_zero(STATEMENT_INVALID);
399 * Allocate a new empty statement.
401 static statement_t *create_empty_statement(void)
403 return allocate_statement_zero(STATEMENT_EMPTY);
407 * Returns the size of a type node.
409 * @param kind the type kind
411 static size_t get_type_struct_size(type_kind_t kind)
413 static const size_t sizes[] = {
414 [TYPE_ATOMIC] = sizeof(atomic_type_t),
415 [TYPE_COMPLEX] = sizeof(complex_type_t),
416 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
417 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
418 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
419 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
420 [TYPE_ENUM] = sizeof(enum_type_t),
421 [TYPE_FUNCTION] = sizeof(function_type_t),
422 [TYPE_POINTER] = sizeof(pointer_type_t),
423 [TYPE_ARRAY] = sizeof(array_type_t),
424 [TYPE_BUILTIN] = sizeof(builtin_type_t),
425 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
426 [TYPE_TYPEOF] = sizeof(typeof_type_t),
428 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
429 assert(kind <= TYPE_TYPEOF);
430 assert(sizes[kind] != 0);
435 * Allocate a type node of given kind and initialize all
438 * @param kind type kind to allocate
439 * @param source_position the source position of the type definition
441 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
443 size_t size = get_type_struct_size(kind);
444 type_t *res = obstack_alloc(type_obst, size);
445 memset(res, 0, size);
447 res->base.kind = kind;
448 res->base.source_position = *source_position;
453 * Returns the size of an initializer node.
455 * @param kind the initializer kind
457 static size_t get_initializer_size(initializer_kind_t kind)
459 static const size_t sizes[] = {
460 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
461 [INITIALIZER_STRING] = sizeof(initializer_string_t),
462 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
463 [INITIALIZER_LIST] = sizeof(initializer_list_t),
464 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
466 assert(kind < sizeof(sizes) / sizeof(*sizes));
467 assert(sizes[kind] != 0);
472 * Allocate an initializer node of given kind and initialize all
475 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
477 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
484 * Free a type from the type obstack.
486 static void free_type(void *type)
488 obstack_free(type_obst, type);
492 * Returns the index of the top element of the environment stack.
494 static size_t environment_top(void)
496 return ARR_LEN(environment_stack);
500 * Returns the index of the top element of the global label stack.
502 static size_t label_top(void)
504 return ARR_LEN(label_stack);
508 * Returns the index of the top element of the local label stack.
510 static size_t local_label_top(void)
512 return ARR_LEN(local_label_stack);
516 * Return the next token.
518 static inline void next_token(void)
520 token = lookahead_buffer[lookahead_bufpos];
521 lookahead_buffer[lookahead_bufpos] = lexer_token;
524 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
527 print_token(stderr, &token);
528 fprintf(stderr, "\n");
533 * Return the next token with a given lookahead.
535 static inline const token_t *look_ahead(int num)
537 assert(num > 0 && num <= MAX_LOOKAHEAD);
538 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
539 return &lookahead_buffer[pos];
543 * Adds a token to the token anchor set (a multi-set).
545 static void add_anchor_token(int token_type)
547 assert(0 <= token_type && token_type < T_LAST_TOKEN);
548 ++token_anchor_set[token_type];
551 static int save_and_reset_anchor_state(int token_type)
553 assert(0 <= token_type && token_type < T_LAST_TOKEN);
554 int count = token_anchor_set[token_type];
555 token_anchor_set[token_type] = 0;
559 static void restore_anchor_state(int token_type, int count)
561 assert(0 <= token_type && token_type < T_LAST_TOKEN);
562 token_anchor_set[token_type] = count;
566 * Remove a token from the token anchor set (a multi-set).
568 static void rem_anchor_token(int token_type)
570 assert(0 <= token_type && token_type < T_LAST_TOKEN);
571 assert(token_anchor_set[token_type] != 0);
572 --token_anchor_set[token_type];
575 static bool at_anchor(void)
579 return token_anchor_set[token.type];
583 * Eat tokens until a matching token is found.
585 static void eat_until_matching_token(int type)
589 case '(': end_token = ')'; break;
590 case '{': end_token = '}'; break;
591 case '[': end_token = ']'; break;
592 default: end_token = type; break;
595 unsigned parenthesis_count = 0;
596 unsigned brace_count = 0;
597 unsigned bracket_count = 0;
598 while (token.type != end_token ||
599 parenthesis_count != 0 ||
601 bracket_count != 0) {
602 switch (token.type) {
604 case '(': ++parenthesis_count; break;
605 case '{': ++brace_count; break;
606 case '[': ++bracket_count; break;
609 if (parenthesis_count > 0)
619 if (bracket_count > 0)
622 if (token.type == end_token &&
623 parenthesis_count == 0 &&
637 * Eat input tokens until an anchor is found.
639 static void eat_until_anchor(void)
641 while (token_anchor_set[token.type] == 0) {
642 if (token.type == '(' || token.type == '{' || token.type == '[')
643 eat_until_matching_token(token.type);
648 static void eat_block(void)
650 eat_until_matching_token('{');
651 if (token.type == '}')
655 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
658 * Report a parse error because an expected token was not found.
661 #if defined __GNUC__ && __GNUC__ >= 4
662 __attribute__((sentinel))
664 void parse_error_expected(const char *message, ...)
666 if (message != NULL) {
667 errorf(HERE, "%s", message);
670 va_start(ap, message);
671 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
676 * Report a type error.
678 static void type_error(const char *msg, const source_position_t *source_position,
681 errorf(source_position, "%s, but found type '%T'", msg, type);
685 * Report an incompatible type.
687 static void type_error_incompatible(const char *msg,
688 const source_position_t *source_position, type_t *type1, type_t *type2)
690 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
695 * Expect the the current token is the expected token.
696 * If not, generate an error, eat the current statement,
697 * and goto the end_error label.
699 #define expect(expected) \
701 if (UNLIKELY(token.type != (expected))) { \
702 parse_error_expected(NULL, (expected), NULL); \
703 add_anchor_token(expected); \
704 eat_until_anchor(); \
705 if (token.type == expected) \
707 rem_anchor_token(expected); \
713 static void scope_push(scope_t *new_scope)
716 scope->last_declaration = last_declaration;
717 new_scope->depth = scope->depth + 1;
719 new_scope->parent = scope;
722 last_declaration = new_scope->last_declaration;
725 static void scope_pop(void)
727 scope->last_declaration = last_declaration;
728 scope = scope->parent;
729 last_declaration = scope->last_declaration;
733 * Search a symbol in a given namespace and returns its declaration or
734 * NULL if this symbol was not found.
736 static declaration_t *get_declaration(const symbol_t *const symbol,
737 const namespace_t namespc)
739 declaration_t *declaration = symbol->declaration;
740 for( ; declaration != NULL; declaration = declaration->symbol_next) {
741 if (declaration->namespc == namespc)
749 * pushs an environment_entry on the environment stack and links the
750 * corresponding symbol to the new entry
752 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
754 symbol_t *symbol = declaration->symbol;
755 namespace_t namespc = (namespace_t) declaration->namespc;
757 /* replace/add declaration into declaration list of the symbol */
758 declaration_t **anchor;
760 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
765 /* replace an entry? */
766 if (iter->namespc == namespc) {
767 declaration->symbol_next = iter->symbol_next;
771 *anchor = declaration;
773 /* remember old declaration */
775 entry.symbol = symbol;
776 entry.old_declaration = iter;
777 entry.namespc = (unsigned short) namespc;
778 ARR_APP1(stack_entry_t, *stack_ptr, entry);
782 * Push a declaration on the environment stack.
784 * @param declaration the declaration
786 static void environment_push(declaration_t *declaration)
788 assert(declaration->source_position.input_name != NULL);
789 assert(declaration->parent_scope != NULL);
790 stack_push(&environment_stack, declaration);
794 * Push a declaration on the global label stack.
796 * @param declaration the declaration
798 static void label_push(declaration_t *declaration)
800 declaration->parent_scope = ¤t_function->scope;
801 stack_push(&label_stack, declaration);
805 * Push a declaration of the local label stack.
807 * @param declaration the declaration
809 static void local_label_push(declaration_t *declaration)
811 assert(declaration->parent_scope != NULL);
812 stack_push(&local_label_stack, declaration);
816 * pops symbols from the environment stack until @p new_top is the top element
818 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
820 stack_entry_t *stack = *stack_ptr;
821 size_t top = ARR_LEN(stack);
824 assert(new_top <= top);
828 for(i = top; i > new_top; --i) {
829 stack_entry_t *entry = &stack[i - 1];
831 declaration_t *old_declaration = entry->old_declaration;
832 symbol_t *symbol = entry->symbol;
833 namespace_t namespc = (namespace_t)entry->namespc;
835 /* replace/remove declaration */
836 declaration_t **anchor;
838 for (anchor = &symbol->declaration;; anchor = &iter->symbol_next) {
840 assert(iter != NULL);
841 /* replace an entry? */
842 if (iter->namespc == namespc)
846 /* Not all declarations adhere scopes (e.g. jump labels), so this
847 * correction is necessary */
848 if (old_declaration != NULL) {
849 old_declaration->symbol_next = iter->symbol_next;
850 *anchor = old_declaration;
852 *anchor = iter->symbol_next;
856 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
860 * Pop all entries from the environment stack until the new_top
863 * @param new_top the new stack top
865 static void environment_pop_to(size_t new_top)
867 stack_pop_to(&environment_stack, new_top);
871 * Pop all entries from the global label stack until the new_top
874 * @param new_top the new stack top
876 static void label_pop_to(size_t new_top)
878 stack_pop_to(&label_stack, new_top);
882 * Pop all entries from the local label stack until the new_top
885 * @param new_top the new stack top
887 static void local_label_pop_to(size_t new_top)
889 stack_pop_to(&local_label_stack, new_top);
893 static int get_akind_rank(atomic_type_kind_t akind)
898 static int get_rank(const type_t *type)
900 assert(!is_typeref(type));
901 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
902 * and esp. footnote 108). However we can't fold constants (yet), so we
903 * can't decide whether unsigned int is possible, while int always works.
904 * (unsigned int would be preferable when possible... for stuff like
905 * struct { enum { ... } bla : 4; } ) */
906 if (type->kind == TYPE_ENUM)
907 return get_akind_rank(ATOMIC_TYPE_INT);
909 assert(type->kind == TYPE_ATOMIC);
910 return get_akind_rank(type->atomic.akind);
913 static type_t *promote_integer(type_t *type)
915 if (type->kind == TYPE_BITFIELD)
916 type = type->bitfield.base_type;
918 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
925 * Create a cast expression.
927 * @param expression the expression to cast
928 * @param dest_type the destination type
930 static expression_t *create_cast_expression(expression_t *expression,
933 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
935 cast->unary.value = expression;
936 cast->base.type = dest_type;
942 * Check if a given expression represents the 0 pointer constant.
944 static bool is_null_pointer_constant(const expression_t *expression)
946 /* skip void* cast */
947 if (expression->kind == EXPR_UNARY_CAST
948 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
949 expression = expression->unary.value;
952 /* TODO: not correct yet, should be any constant integer expression
953 * which evaluates to 0 */
954 if (expression->kind != EXPR_CONST)
957 type_t *const type = skip_typeref(expression->base.type);
958 if (!is_type_integer(type))
961 return expression->conste.v.int_value == 0;
965 * Create an implicit cast expression.
967 * @param expression the expression to cast
968 * @param dest_type the destination type
970 static expression_t *create_implicit_cast(expression_t *expression,
973 type_t *const source_type = expression->base.type;
975 if (source_type == dest_type)
978 return create_cast_expression(expression, dest_type);
981 typedef enum assign_error_t {
983 ASSIGN_ERROR_INCOMPATIBLE,
984 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
985 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
986 ASSIGN_WARNING_POINTER_FROM_INT,
987 ASSIGN_WARNING_INT_FROM_POINTER
990 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
991 const expression_t *const right,
993 const source_position_t *source_position)
995 type_t *const orig_type_right = right->base.type;
996 type_t *const type_left = skip_typeref(orig_type_left);
997 type_t *const type_right = skip_typeref(orig_type_right);
1000 case ASSIGN_SUCCESS:
1002 case ASSIGN_ERROR_INCOMPATIBLE:
1003 errorf(source_position,
1004 "destination type '%T' in %s is incompatible with type '%T'",
1005 orig_type_left, context, orig_type_right);
1008 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
1009 type_t *points_to_left
1010 = skip_typeref(type_left->pointer.points_to);
1011 type_t *points_to_right
1012 = skip_typeref(type_right->pointer.points_to);
1014 /* the left type has all qualifiers from the right type */
1015 unsigned missing_qualifiers
1016 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1017 warningf(source_position,
1018 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
1019 orig_type_left, context, orig_type_right, missing_qualifiers);
1023 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
1024 warningf(source_position,
1025 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
1026 orig_type_left, context, right, orig_type_right);
1029 case ASSIGN_WARNING_POINTER_FROM_INT:
1030 warningf(source_position,
1031 "%s makes pointer '%T' from integer '%T' without a cast",
1032 context, orig_type_left, orig_type_right);
1035 case ASSIGN_WARNING_INT_FROM_POINTER:
1036 warningf(source_position,
1037 "%s makes integer '%T' from pointer '%T' without a cast",
1038 context, orig_type_left, orig_type_right);
1042 panic("invalid error value");
1046 /** Implements the rules from § 6.5.16.1 */
1047 static assign_error_t semantic_assign(type_t *orig_type_left,
1048 const expression_t *const right)
1050 type_t *const orig_type_right = right->base.type;
1051 type_t *const type_left = skip_typeref(orig_type_left);
1052 type_t *const type_right = skip_typeref(orig_type_right);
1054 if (is_type_pointer(type_left)) {
1055 if (is_null_pointer_constant(right)) {
1056 return ASSIGN_SUCCESS;
1057 } else if (is_type_pointer(type_right)) {
1058 type_t *points_to_left
1059 = skip_typeref(type_left->pointer.points_to);
1060 type_t *points_to_right
1061 = skip_typeref(type_right->pointer.points_to);
1062 assign_error_t res = ASSIGN_SUCCESS;
1064 /* the left type has all qualifiers from the right type */
1065 unsigned missing_qualifiers
1066 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1067 if (missing_qualifiers != 0) {
1068 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1071 points_to_left = get_unqualified_type(points_to_left);
1072 points_to_right = get_unqualified_type(points_to_right);
1074 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1075 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1079 if (!types_compatible(points_to_left, points_to_right)) {
1080 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1084 } else if (is_type_integer(type_right)) {
1085 return ASSIGN_WARNING_POINTER_FROM_INT;
1087 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1088 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1089 && is_type_pointer(type_right))) {
1090 return ASSIGN_SUCCESS;
1091 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1092 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1093 type_t *const unqual_type_left = get_unqualified_type(type_left);
1094 type_t *const unqual_type_right = get_unqualified_type(type_right);
1095 if (types_compatible(unqual_type_left, unqual_type_right)) {
1096 return ASSIGN_SUCCESS;
1098 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1099 return ASSIGN_WARNING_INT_FROM_POINTER;
1102 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1103 return ASSIGN_SUCCESS;
1105 return ASSIGN_ERROR_INCOMPATIBLE;
1108 static expression_t *parse_constant_expression(void)
1110 expression_t *result = parse_sub_expression(PREC_CONDITIONAL);
1112 if (!is_constant_expression(result)) {
1113 errorf(&result->base.source_position,
1114 "expression '%E' is not constant\n", result);
1120 static expression_t *parse_assignment_expression(void)
1122 return parse_sub_expression(PREC_ASSIGNMENT);
1125 static type_t *make_global_typedef(const char *name, type_t *type)
1127 symbol_t *const symbol = symbol_table_insert(name);
1129 declaration_t *const declaration = allocate_declaration_zero();
1130 declaration->namespc = NAMESPACE_NORMAL;
1131 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1132 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1133 declaration->type = type;
1134 declaration->symbol = symbol;
1135 declaration->source_position = builtin_source_position;
1136 declaration->implicit = true;
1138 record_declaration(declaration, false);
1140 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1141 typedef_type->typedeft.declaration = declaration;
1143 return typedef_type;
1146 static string_t parse_string_literals(void)
1148 assert(token.type == T_STRING_LITERAL);
1149 string_t result = token.v.string;
1153 while (token.type == T_STRING_LITERAL) {
1154 result = concat_strings(&result, &token.v.string);
1161 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1162 [GNU_AK_CONST] = "const",
1163 [GNU_AK_VOLATILE] = "volatile",
1164 [GNU_AK_CDECL] = "cdecl",
1165 [GNU_AK_STDCALL] = "stdcall",
1166 [GNU_AK_FASTCALL] = "fastcall",
1167 [GNU_AK_DEPRECATED] = "deprecated",
1168 [GNU_AK_NOINLINE] = "noinline",
1169 [GNU_AK_NORETURN] = "noreturn",
1170 [GNU_AK_NAKED] = "naked",
1171 [GNU_AK_PURE] = "pure",
1172 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1173 [GNU_AK_MALLOC] = "malloc",
1174 [GNU_AK_WEAK] = "weak",
1175 [GNU_AK_CONSTRUCTOR] = "constructor",
1176 [GNU_AK_DESTRUCTOR] = "destructor",
1177 [GNU_AK_NOTHROW] = "nothrow",
1178 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1179 [GNU_AK_COMMON] = "common",
1180 [GNU_AK_NOCOMMON] = "nocommon",
1181 [GNU_AK_PACKED] = "packed",
1182 [GNU_AK_SHARED] = "shared",
1183 [GNU_AK_NOTSHARED] = "notshared",
1184 [GNU_AK_USED] = "used",
1185 [GNU_AK_UNUSED] = "unused",
1186 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1187 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1188 [GNU_AK_LONGCALL] = "longcall",
1189 [GNU_AK_SHORTCALL] = "shortcall",
1190 [GNU_AK_LONG_CALL] = "long_call",
1191 [GNU_AK_SHORT_CALL] = "short_call",
1192 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1193 [GNU_AK_INTERRUPT] = "interrupt",
1194 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1195 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1196 [GNU_AK_NESTING] = "nesting",
1197 [GNU_AK_NEAR] = "near",
1198 [GNU_AK_FAR] = "far",
1199 [GNU_AK_SIGNAL] = "signal",
1200 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1201 [GNU_AK_TINY_DATA] = "tiny_data",
1202 [GNU_AK_SAVEALL] = "saveall",
1203 [GNU_AK_FLATTEN] = "flatten",
1204 [GNU_AK_SSEREGPARM] = "sseregparm",
1205 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1206 [GNU_AK_RETURN_TWICE] = "return_twice",
1207 [GNU_AK_MAY_ALIAS] = "may_alias",
1208 [GNU_AK_MS_STRUCT] = "ms_struct",
1209 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1210 [GNU_AK_DLLIMPORT] = "dllimport",
1211 [GNU_AK_DLLEXPORT] = "dllexport",
1212 [GNU_AK_ALIGNED] = "aligned",
1213 [GNU_AK_ALIAS] = "alias",
1214 [GNU_AK_SECTION] = "section",
1215 [GNU_AK_FORMAT] = "format",
1216 [GNU_AK_FORMAT_ARG] = "format_arg",
1217 [GNU_AK_WEAKREF] = "weakref",
1218 [GNU_AK_NONNULL] = "nonnull",
1219 [GNU_AK_TLS_MODEL] = "tls_model",
1220 [GNU_AK_VISIBILITY] = "visibility",
1221 [GNU_AK_REGPARM] = "regparm",
1222 [GNU_AK_MODE] = "mode",
1223 [GNU_AK_MODEL] = "model",
1224 [GNU_AK_TRAP_EXIT] = "trap_exit",
1225 [GNU_AK_SP_SWITCH] = "sp_switch",
1226 [GNU_AK_SENTINEL] = "sentinel"
1230 * compare two string, ignoring double underscores on the second.
1232 static int strcmp_underscore(const char *s1, const char *s2)
1234 if (s2[0] == '_' && s2[1] == '_') {
1235 size_t len2 = strlen(s2);
1236 size_t len1 = strlen(s1);
1237 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1238 return strncmp(s1, s2+2, len2-4);
1242 return strcmp(s1, s2);
1246 * Allocate a new gnu temporal attribute.
1248 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1250 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1251 attribute->kind = kind;
1252 attribute->next = NULL;
1253 attribute->invalid = false;
1254 attribute->have_arguments = false;
1260 * parse one constant expression argument.
1262 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1264 expression_t *expression;
1265 add_anchor_token(')');
1266 expression = parse_constant_expression();
1267 rem_anchor_token(')');
1269 attribute->u.argument = fold_constant(expression);
1272 attribute->invalid = true;
1276 * parse a list of constant expressions arguments.
1278 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1280 argument_list_t **list = &attribute->u.arguments;
1281 argument_list_t *entry;
1282 expression_t *expression;
1283 add_anchor_token(')');
1284 add_anchor_token(',');
1286 expression = parse_constant_expression();
1287 entry = obstack_alloc(&temp_obst, sizeof(entry));
1288 entry->argument = fold_constant(expression);
1291 list = &entry->next;
1292 if (token.type != ',')
1296 rem_anchor_token(',');
1297 rem_anchor_token(')');
1301 attribute->invalid = true;
1305 * parse one string literal argument.
1307 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1310 add_anchor_token('(');
1311 if (token.type != T_STRING_LITERAL) {
1312 parse_error_expected("while parsing attribute directive",
1313 T_STRING_LITERAL, NULL);
1316 *string = parse_string_literals();
1317 rem_anchor_token('(');
1321 attribute->invalid = true;
1325 * parse one tls model.
1327 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1329 static const char *const tls_models[] = {
1335 string_t string = { NULL, 0 };
1336 parse_gnu_attribute_string_arg(attribute, &string);
1337 if (string.begin != NULL) {
1338 for(size_t i = 0; i < 4; ++i) {
1339 if (strcmp(tls_models[i], string.begin) == 0) {
1340 attribute->u.value = i;
1344 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1346 attribute->invalid = true;
1350 * parse one tls model.
1352 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1354 static const char *const visibilities[] = {
1360 string_t string = { NULL, 0 };
1361 parse_gnu_attribute_string_arg(attribute, &string);
1362 if (string.begin != NULL) {
1363 for(size_t i = 0; i < 4; ++i) {
1364 if (strcmp(visibilities[i], string.begin) == 0) {
1365 attribute->u.value = i;
1369 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1371 attribute->invalid = true;
1375 * parse one (code) model.
1377 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1379 static const char *const visibilities[] = {
1384 string_t string = { NULL, 0 };
1385 parse_gnu_attribute_string_arg(attribute, &string);
1386 if (string.begin != NULL) {
1387 for(int i = 0; i < 3; ++i) {
1388 if (strcmp(visibilities[i], string.begin) == 0) {
1389 attribute->u.value = i;
1393 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1395 attribute->invalid = true;
1398 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1400 /* TODO: find out what is allowed here... */
1402 /* at least: byte, word, pointer, list of machine modes
1403 * __XXX___ is interpreted as XXX */
1404 add_anchor_token(')');
1406 if (token.type != T_IDENTIFIER) {
1407 expect(T_IDENTIFIER);
1410 /* This isn't really correct, the backend should provide a list of machine
1411 * specific modes (according to gcc philosophy that is...) */
1412 const char *symbol_str = token.v.symbol->string;
1413 if (strcmp_underscore("QI", symbol_str) == 0 ||
1414 strcmp_underscore("byte", symbol_str) == 0) {
1415 attribute->u.akind = ATOMIC_TYPE_CHAR;
1416 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1417 attribute->u.akind = ATOMIC_TYPE_SHORT;
1418 } else if (strcmp_underscore("SI", symbol_str) == 0
1419 || strcmp_underscore("word", symbol_str) == 0
1420 || strcmp_underscore("pointer", symbol_str) == 0) {
1421 attribute->u.akind = ATOMIC_TYPE_INT;
1422 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1423 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1425 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1426 attribute->invalid = true;
1430 rem_anchor_token(')');
1434 attribute->invalid = true;
1438 * parse one interrupt argument.
1440 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1442 static const char *const interrupts[] = {
1449 string_t string = { NULL, 0 };
1450 parse_gnu_attribute_string_arg(attribute, &string);
1451 if (string.begin != NULL) {
1452 for(size_t i = 0; i < 5; ++i) {
1453 if (strcmp(interrupts[i], string.begin) == 0) {
1454 attribute->u.value = i;
1458 errorf(HERE, "'%s' is not an interrupt", string.begin);
1460 attribute->invalid = true;
1464 * parse ( identifier, const expression, const expression )
1466 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1468 static const char *const format_names[] = {
1476 if (token.type != T_IDENTIFIER) {
1477 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1480 const char *name = token.v.symbol->string;
1481 for(i = 0; i < 4; ++i) {
1482 if (strcmp_underscore(format_names[i], name) == 0)
1486 if (warning.attribute)
1487 warningf(HERE, "'%s' is an unrecognized format function type", name);
1492 add_anchor_token(')');
1493 add_anchor_token(',');
1494 parse_constant_expression();
1495 rem_anchor_token(',');
1496 rem_anchor_token(')');
1499 add_anchor_token(')');
1500 parse_constant_expression();
1501 rem_anchor_token(')');
1505 attribute->u.value = true;
1508 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1510 if (!attribute->have_arguments)
1513 /* should have no arguments */
1514 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1515 eat_until_matching_token('(');
1516 /* we have already consumed '(', so we stop before ')', eat it */
1518 attribute->invalid = true;
1522 * Parse one GNU attribute.
1524 * Note that attribute names can be specified WITH or WITHOUT
1525 * double underscores, ie const or __const__.
1527 * The following attributes are parsed without arguments
1552 * no_instrument_function
1553 * warn_unused_result
1570 * externally_visible
1578 * The following attributes are parsed with arguments
1579 * aligned( const expression )
1580 * alias( string literal )
1581 * section( string literal )
1582 * format( identifier, const expression, const expression )
1583 * format_arg( const expression )
1584 * tls_model( string literal )
1585 * visibility( string literal )
1586 * regparm( const expression )
1587 * model( string leteral )
1588 * trap_exit( const expression )
1589 * sp_switch( string literal )
1591 * The following attributes might have arguments
1592 * weak_ref( string literal )
1593 * non_null( const expression // ',' )
1594 * interrupt( string literal )
1595 * sentinel( constant expression )
1597 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1599 gnu_attribute_t *head = *attributes;
1600 gnu_attribute_t *last = *attributes;
1601 decl_modifiers_t modifiers = 0;
1602 gnu_attribute_t *attribute;
1604 eat(T___attribute__);
1608 if (token.type != ')') {
1609 /* find the end of the list */
1611 while (last->next != NULL)
1615 /* non-empty attribute list */
1618 if (token.type == T_const) {
1620 } else if (token.type == T_volatile) {
1622 } else if (token.type == T_cdecl) {
1623 /* __attribute__((cdecl)), WITH ms mode */
1625 } else if (token.type == T_IDENTIFIER) {
1626 const symbol_t *sym = token.v.symbol;
1629 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1636 for(i = 0; i < GNU_AK_LAST; ++i) {
1637 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1640 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1643 if (kind == GNU_AK_LAST) {
1644 if (warning.attribute)
1645 warningf(HERE, "'%s' attribute directive ignored", name);
1647 /* skip possible arguments */
1648 if (token.type == '(') {
1649 eat_until_matching_token(')');
1652 /* check for arguments */
1653 attribute = allocate_gnu_attribute(kind);
1654 if (token.type == '(') {
1656 if (token.type == ')') {
1657 /* empty args are allowed */
1660 attribute->have_arguments = true;
1664 case GNU_AK_VOLATILE:
1669 case GNU_AK_NOCOMMON:
1671 case GNU_AK_NOTSHARED:
1672 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1673 case GNU_AK_WARN_UNUSED_RESULT:
1674 case GNU_AK_LONGCALL:
1675 case GNU_AK_SHORTCALL:
1676 case GNU_AK_LONG_CALL:
1677 case GNU_AK_SHORT_CALL:
1678 case GNU_AK_FUNCTION_VECTOR:
1679 case GNU_AK_INTERRUPT_HANDLER:
1680 case GNU_AK_NMI_HANDLER:
1681 case GNU_AK_NESTING:
1685 case GNU_AK_EIGTHBIT_DATA:
1686 case GNU_AK_TINY_DATA:
1687 case GNU_AK_SAVEALL:
1688 case GNU_AK_FLATTEN:
1689 case GNU_AK_SSEREGPARM:
1690 case GNU_AK_EXTERNALLY_VISIBLE:
1691 case GNU_AK_RETURN_TWICE:
1692 case GNU_AK_MAY_ALIAS:
1693 case GNU_AK_MS_STRUCT:
1694 case GNU_AK_GCC_STRUCT:
1697 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1698 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1699 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1700 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1701 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1702 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1703 case GNU_AK_CONST: modifiers |= DM_CONST; goto no_arg;
1704 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1705 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1706 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1707 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1708 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1709 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1710 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1711 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1712 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1713 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1714 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1716 case GNU_AK_ALIGNED:
1717 /* __align__ may be used without an argument */
1718 if (attribute->have_arguments) {
1719 parse_gnu_attribute_const_arg(attribute);
1723 case GNU_AK_FORMAT_ARG:
1724 case GNU_AK_REGPARM:
1725 case GNU_AK_TRAP_EXIT:
1726 if (!attribute->have_arguments) {
1727 /* should have arguments */
1728 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1729 attribute->invalid = true;
1731 parse_gnu_attribute_const_arg(attribute);
1734 case GNU_AK_SECTION:
1735 case GNU_AK_SP_SWITCH:
1736 if (!attribute->have_arguments) {
1737 /* should have arguments */
1738 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1739 attribute->invalid = true;
1741 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1744 if (!attribute->have_arguments) {
1745 /* should have arguments */
1746 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1747 attribute->invalid = true;
1749 parse_gnu_attribute_format_args(attribute);
1751 case GNU_AK_WEAKREF:
1752 /* may have one string argument */
1753 if (attribute->have_arguments)
1754 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1756 case GNU_AK_NONNULL:
1757 if (attribute->have_arguments)
1758 parse_gnu_attribute_const_arg_list(attribute);
1760 case GNU_AK_TLS_MODEL:
1761 if (!attribute->have_arguments) {
1762 /* should have arguments */
1763 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1765 parse_gnu_attribute_tls_model_arg(attribute);
1767 case GNU_AK_VISIBILITY:
1768 if (!attribute->have_arguments) {
1769 /* should have arguments */
1770 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1772 parse_gnu_attribute_visibility_arg(attribute);
1775 if (!attribute->have_arguments) {
1776 /* should have arguments */
1777 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1779 parse_gnu_attribute_model_arg(attribute);
1783 if (!attribute->have_arguments) {
1784 /* should have arguments */
1785 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1787 parse_gnu_attribute_mode_arg(attribute);
1790 case GNU_AK_INTERRUPT:
1791 /* may have one string argument */
1792 if (attribute->have_arguments)
1793 parse_gnu_attribute_interrupt_arg(attribute);
1795 case GNU_AK_SENTINEL:
1796 /* may have one string argument */
1797 if (attribute->have_arguments)
1798 parse_gnu_attribute_const_arg(attribute);
1801 /* already handled */
1805 check_no_argument(attribute, name);
1808 if (attribute != NULL) {
1810 last->next = attribute;
1813 head = last = attribute;
1817 if (token.type != ',')
1831 * Parse GNU attributes.
1833 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1835 decl_modifiers_t modifiers = 0;
1838 switch(token.type) {
1839 case T___attribute__:
1840 modifiers |= parse_gnu_attribute(attributes);
1846 if (token.type != T_STRING_LITERAL) {
1847 parse_error_expected("while parsing assembler attribute",
1848 T_STRING_LITERAL, NULL);
1849 eat_until_matching_token('(');
1852 parse_string_literals();
1857 case T_cdecl: modifiers |= DM_CDECL; break;
1858 case T__fastcall: modifiers |= DM_FASTCALL; break;
1859 case T__stdcall: modifiers |= DM_STDCALL; break;
1862 /* TODO record modifier */
1863 warningf(HERE, "Ignoring declaration modifier %K", &token);
1867 default: return modifiers;
1874 static void mark_decls_read(expression_t *expr, declaration_t *lhs_decl);
1876 static declaration_t *determine_lhs_decl(expression_t *const expr, declaration_t *lhs_decl)
1878 switch (expr->kind) {
1879 case EXPR_REFERENCE: {
1880 declaration_t *const decl = expr->reference.declaration;
1884 case EXPR_ARRAY_ACCESS: {
1885 expression_t *const ref = expr->array_access.array_ref;
1886 declaration_t * decl = NULL;
1887 if (is_type_array(skip_typeref(revert_automatic_type_conversion(ref)))) {
1888 decl = determine_lhs_decl(ref, lhs_decl);
1891 mark_decls_read(expr->select.compound, lhs_decl);
1893 mark_decls_read(expr->array_access.index, lhs_decl);
1898 if (is_type_compound(skip_typeref(expr->base.type))) {
1899 return determine_lhs_decl(expr->select.compound, lhs_decl);
1901 mark_decls_read(expr->select.compound, lhs_decl);
1906 case EXPR_UNARY_DEREFERENCE: {
1907 expression_t *const val = expr->unary.value;
1908 if (val->kind == EXPR_UNARY_TAKE_ADDRESS) {
1910 return determine_lhs_decl(val->unary.value, lhs_decl);
1912 mark_decls_read(val, NULL);
1918 mark_decls_read(expr, NULL);
1923 #define DECL_ANY ((declaration_t*)-1)
1926 * Mark declarations, which are read. This is used to deted variables, which
1930 * x is not marked as "read", because it is only read to calculate its own new
1934 * x and y are not detected as "not read", because multiple variables are
1937 static void mark_decls_read(expression_t *const expr, declaration_t *lhs_decl)
1939 switch (expr->kind) {
1940 case EXPR_REFERENCE: {
1941 declaration_t *const decl = expr->reference.declaration;
1942 if (lhs_decl != decl && lhs_decl != DECL_ANY)
1948 // TODO respect pure/const
1949 mark_decls_read(expr->call.function, NULL);
1950 for (call_argument_t *arg = expr->call.arguments; arg != NULL; arg = arg->next) {
1951 mark_decls_read(arg->expression, NULL);
1955 case EXPR_CONDITIONAL:
1956 // TODO lhs_decl should depend on whether true/false have an effect
1957 mark_decls_read(expr->conditional.condition, NULL);
1958 if (expr->conditional.true_expression != NULL)
1959 mark_decls_read(expr->conditional.true_expression, lhs_decl);
1960 mark_decls_read(expr->conditional.false_expression, lhs_decl);
1964 if (lhs_decl == DECL_ANY && !is_type_compound(skip_typeref(expr->base.type)))
1966 mark_decls_read(expr->select.compound, lhs_decl);
1969 case EXPR_ARRAY_ACCESS: {
1970 expression_t *const ref = expr->array_access.array_ref;
1971 mark_decls_read(ref, lhs_decl);
1972 lhs_decl = determine_lhs_decl(ref, lhs_decl);
1973 mark_decls_read(expr->array_access.index, lhs_decl);
1978 mark_decls_read(expr->va_arge.ap, lhs_decl);
1981 case EXPR_UNARY_CAST:
1982 /* Special case: Use void cast to mark a variable as "read" */
1983 if (is_type_atomic(skip_typeref(expr->base.type), ATOMIC_TYPE_VOID))
1987 case EXPR_UNARY_DEREFERENCE:
1988 if (lhs_decl == DECL_ANY)
1992 case EXPR_UNARY_NEGATE:
1993 case EXPR_UNARY_PLUS:
1994 case EXPR_UNARY_BITWISE_NEGATE:
1995 case EXPR_UNARY_NOT:
1996 case EXPR_UNARY_TAKE_ADDRESS:
1997 case EXPR_UNARY_POSTFIX_INCREMENT:
1998 case EXPR_UNARY_POSTFIX_DECREMENT:
1999 case EXPR_UNARY_PREFIX_INCREMENT:
2000 case EXPR_UNARY_PREFIX_DECREMENT:
2001 case EXPR_UNARY_CAST_IMPLICIT:
2002 case EXPR_UNARY_ASSUME:
2004 mark_decls_read(expr->unary.value, lhs_decl);
2007 case EXPR_BINARY_ADD:
2008 case EXPR_BINARY_SUB:
2009 case EXPR_BINARY_MUL:
2010 case EXPR_BINARY_DIV:
2011 case EXPR_BINARY_MOD:
2012 case EXPR_BINARY_EQUAL:
2013 case EXPR_BINARY_NOTEQUAL:
2014 case EXPR_BINARY_LESS:
2015 case EXPR_BINARY_LESSEQUAL:
2016 case EXPR_BINARY_GREATER:
2017 case EXPR_BINARY_GREATEREQUAL:
2018 case EXPR_BINARY_BITWISE_AND:
2019 case EXPR_BINARY_BITWISE_OR:
2020 case EXPR_BINARY_BITWISE_XOR:
2021 case EXPR_BINARY_LOGICAL_AND:
2022 case EXPR_BINARY_LOGICAL_OR:
2023 case EXPR_BINARY_SHIFTLEFT:
2024 case EXPR_BINARY_SHIFTRIGHT:
2025 case EXPR_BINARY_COMMA:
2026 case EXPR_BINARY_ISGREATER:
2027 case EXPR_BINARY_ISGREATEREQUAL:
2028 case EXPR_BINARY_ISLESS:
2029 case EXPR_BINARY_ISLESSEQUAL:
2030 case EXPR_BINARY_ISLESSGREATER:
2031 case EXPR_BINARY_ISUNORDERED:
2032 mark_decls_read(expr->binary.left, lhs_decl);
2033 mark_decls_read(expr->binary.right, lhs_decl);
2036 case EXPR_BINARY_ASSIGN:
2037 case EXPR_BINARY_MUL_ASSIGN:
2038 case EXPR_BINARY_DIV_ASSIGN:
2039 case EXPR_BINARY_MOD_ASSIGN:
2040 case EXPR_BINARY_ADD_ASSIGN:
2041 case EXPR_BINARY_SUB_ASSIGN:
2042 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2043 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2044 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2045 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2046 case EXPR_BINARY_BITWISE_OR_ASSIGN: {
2047 if (lhs_decl == DECL_ANY)
2049 lhs_decl = determine_lhs_decl(expr->binary.left, lhs_decl);
2050 mark_decls_read(expr->binary.right, lhs_decl);
2055 determine_lhs_decl(expr->va_starte.ap, lhs_decl);
2061 case EXPR_CHARACTER_CONSTANT:
2062 case EXPR_WIDE_CHARACTER_CONSTANT:
2063 case EXPR_STRING_LITERAL:
2064 case EXPR_WIDE_STRING_LITERAL:
2065 case EXPR_COMPOUND_LITERAL: // TODO init?
2067 case EXPR_CLASSIFY_TYPE:
2070 case EXPR_BUILTIN_SYMBOL:
2071 case EXPR_BUILTIN_CONSTANT_P:
2072 case EXPR_BUILTIN_PREFETCH:
2074 case EXPR_STATEMENT: // TODO
2075 case EXPR_LABEL_ADDRESS:
2076 case EXPR_BINARY_BUILTIN_EXPECT:
2080 panic("unhandled expression");
2083 static designator_t *parse_designation(void)
2085 designator_t *result = NULL;
2086 designator_t *last = NULL;
2089 designator_t *designator;
2090 switch(token.type) {
2092 designator = allocate_ast_zero(sizeof(designator[0]));
2093 designator->source_position = token.source_position;
2095 add_anchor_token(']');
2096 designator->array_index = parse_constant_expression();
2097 rem_anchor_token(']');
2101 designator = allocate_ast_zero(sizeof(designator[0]));
2102 designator->source_position = token.source_position;
2104 if (token.type != T_IDENTIFIER) {
2105 parse_error_expected("while parsing designator",
2106 T_IDENTIFIER, NULL);
2109 designator->symbol = token.v.symbol;
2117 assert(designator != NULL);
2119 last->next = designator;
2121 result = designator;
2129 static initializer_t *initializer_from_string(array_type_t *type,
2130 const string_t *const string)
2132 /* TODO: check len vs. size of array type */
2135 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
2136 initializer->string.string = *string;
2141 static initializer_t *initializer_from_wide_string(array_type_t *const type,
2142 wide_string_t *const string)
2144 /* TODO: check len vs. size of array type */
2147 initializer_t *const initializer =
2148 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
2149 initializer->wide_string.string = *string;
2155 * Build an initializer from a given expression.
2157 static initializer_t *initializer_from_expression(type_t *orig_type,
2158 expression_t *expression)
2160 /* TODO check that expression is a constant expression */
2162 /* § 6.7.8.14/15 char array may be initialized by string literals */
2163 type_t *type = skip_typeref(orig_type);
2164 type_t *expr_type_orig = expression->base.type;
2165 type_t *expr_type = skip_typeref(expr_type_orig);
2166 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
2167 array_type_t *const array_type = &type->array;
2168 type_t *const element_type = skip_typeref(array_type->element_type);
2170 if (element_type->kind == TYPE_ATOMIC) {
2171 atomic_type_kind_t akind = element_type->atomic.akind;
2172 switch (expression->kind) {
2173 case EXPR_STRING_LITERAL:
2174 if (akind == ATOMIC_TYPE_CHAR
2175 || akind == ATOMIC_TYPE_SCHAR
2176 || akind == ATOMIC_TYPE_UCHAR) {
2177 return initializer_from_string(array_type,
2178 &expression->string.value);
2181 case EXPR_WIDE_STRING_LITERAL: {
2182 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
2183 if (get_unqualified_type(element_type) == bare_wchar_type) {
2184 return initializer_from_wide_string(array_type,
2185 &expression->wide_string.value);
2195 assign_error_t error = semantic_assign(type, expression);
2196 if (error == ASSIGN_ERROR_INCOMPATIBLE)
2198 report_assign_error(error, type, expression, "initializer",
2199 &expression->base.source_position);
2201 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
2203 if (type->kind == TYPE_BITFIELD) {
2204 type = type->bitfield.base_type;
2207 result->value.value = create_implicit_cast(expression, type);
2213 * Checks if a given expression can be used as an constant initializer.
2215 static bool is_initializer_constant(const expression_t *expression)
2217 return is_constant_expression(expression)
2218 || is_address_constant(expression);
2222 * Parses an scalar initializer.
2224 * § 6.7.8.11; eat {} without warning
2226 static initializer_t *parse_scalar_initializer(type_t *type,
2227 bool must_be_constant)
2229 /* there might be extra {} hierarchies */
2231 if (token.type == '{') {
2232 warningf(HERE, "extra curly braces around scalar initializer");
2236 } while (token.type == '{');
2239 expression_t *expression = parse_assignment_expression();
2240 mark_decls_read(expression, NULL);
2241 if (must_be_constant && !is_initializer_constant(expression)) {
2242 errorf(&expression->base.source_position,
2243 "Initialisation expression '%E' is not constant\n",
2247 initializer_t *initializer = initializer_from_expression(type, expression);
2249 if (initializer == NULL) {
2250 errorf(&expression->base.source_position,
2251 "expression '%E' (type '%T') doesn't match expected type '%T'",
2252 expression, expression->base.type, type);
2257 bool additional_warning_displayed = false;
2258 while (braces > 0) {
2259 if (token.type == ',') {
2262 if (token.type != '}') {
2263 if (!additional_warning_displayed) {
2264 warningf(HERE, "additional elements in scalar initializer");
2265 additional_warning_displayed = true;
2276 * An entry in the type path.
2278 typedef struct type_path_entry_t type_path_entry_t;
2279 struct type_path_entry_t {
2280 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2282 size_t index; /**< For array types: the current index. */
2283 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2288 * A type path expression a position inside compound or array types.
2290 typedef struct type_path_t type_path_t;
2291 struct type_path_t {
2292 type_path_entry_t *path; /**< An flexible array containing the current path. */
2293 type_t *top_type; /**< type of the element the path points */
2294 size_t max_index; /**< largest index in outermost array */
2298 * Prints a type path for debugging.
2300 static __attribute__((unused)) void debug_print_type_path(
2301 const type_path_t *path)
2303 size_t len = ARR_LEN(path->path);
2305 for(size_t i = 0; i < len; ++i) {
2306 const type_path_entry_t *entry = & path->path[i];
2308 type_t *type = skip_typeref(entry->type);
2309 if (is_type_compound(type)) {
2310 /* in gcc mode structs can have no members */
2311 if (entry->v.compound_entry == NULL) {
2315 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2316 } else if (is_type_array(type)) {
2317 fprintf(stderr, "[%zu]", entry->v.index);
2319 fprintf(stderr, "-INVALID-");
2322 if (path->top_type != NULL) {
2323 fprintf(stderr, " (");
2324 print_type(path->top_type);
2325 fprintf(stderr, ")");
2330 * Return the top type path entry, ie. in a path
2331 * (type).a.b returns the b.
2333 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2335 size_t len = ARR_LEN(path->path);
2337 return &path->path[len-1];
2341 * Enlarge the type path by an (empty) element.
2343 static type_path_entry_t *append_to_type_path(type_path_t *path)
2345 size_t len = ARR_LEN(path->path);
2346 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2348 type_path_entry_t *result = & path->path[len];
2349 memset(result, 0, sizeof(result[0]));
2354 * Descending into a sub-type. Enter the scope of the current
2357 static void descend_into_subtype(type_path_t *path)
2359 type_t *orig_top_type = path->top_type;
2360 type_t *top_type = skip_typeref(orig_top_type);
2362 type_path_entry_t *top = append_to_type_path(path);
2363 top->type = top_type;
2365 if (is_type_compound(top_type)) {
2366 declaration_t *declaration = top_type->compound.declaration;
2367 declaration_t *entry = declaration->scope.declarations;
2368 top->v.compound_entry = entry;
2370 if (entry != NULL) {
2371 path->top_type = entry->type;
2373 path->top_type = NULL;
2375 } else if (is_type_array(top_type)) {
2377 path->top_type = top_type->array.element_type;
2379 assert(!is_type_valid(top_type));
2384 * Pop an entry from the given type path, ie. returning from
2385 * (type).a.b to (type).a
2387 static void ascend_from_subtype(type_path_t *path)
2389 type_path_entry_t *top = get_type_path_top(path);
2391 path->top_type = top->type;
2393 size_t len = ARR_LEN(path->path);
2394 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2398 * Pop entries from the given type path until the given
2399 * path level is reached.
2401 static void ascend_to(type_path_t *path, size_t top_path_level)
2403 size_t len = ARR_LEN(path->path);
2405 while (len > top_path_level) {
2406 ascend_from_subtype(path);
2407 len = ARR_LEN(path->path);
2411 static bool walk_designator(type_path_t *path, const designator_t *designator,
2412 bool used_in_offsetof)
2414 for( ; designator != NULL; designator = designator->next) {
2415 type_path_entry_t *top = get_type_path_top(path);
2416 type_t *orig_type = top->type;
2418 type_t *type = skip_typeref(orig_type);
2420 if (designator->symbol != NULL) {
2421 symbol_t *symbol = designator->symbol;
2422 if (!is_type_compound(type)) {
2423 if (is_type_valid(type)) {
2424 errorf(&designator->source_position,
2425 "'.%Y' designator used for non-compound type '%T'",
2429 top->type = type_error_type;
2430 top->v.compound_entry = NULL;
2431 orig_type = type_error_type;
2433 declaration_t *declaration = type->compound.declaration;
2434 declaration_t *iter = declaration->scope.declarations;
2435 for( ; iter != NULL; iter = iter->next) {
2436 if (iter->symbol == symbol) {
2441 errorf(&designator->source_position,
2442 "'%T' has no member named '%Y'", orig_type, symbol);
2445 if (used_in_offsetof) {
2446 type_t *real_type = skip_typeref(iter->type);
2447 if (real_type->kind == TYPE_BITFIELD) {
2448 errorf(&designator->source_position,
2449 "offsetof designator '%Y' may not specify bitfield",
2455 top->type = orig_type;
2456 top->v.compound_entry = iter;
2457 orig_type = iter->type;
2460 expression_t *array_index = designator->array_index;
2461 assert(designator->array_index != NULL);
2463 if (!is_type_array(type)) {
2464 if (is_type_valid(type)) {
2465 errorf(&designator->source_position,
2466 "[%E] designator used for non-array type '%T'",
2467 array_index, orig_type);
2472 long index = fold_constant(array_index);
2473 if (!used_in_offsetof) {
2475 errorf(&designator->source_position,
2476 "array index [%E] must be positive", array_index);
2477 } else if (type->array.size_constant) {
2478 long array_size = type->array.size;
2479 if (index >= array_size) {
2480 errorf(&designator->source_position,
2481 "designator [%E] (%d) exceeds array size %d",
2482 array_index, index, array_size);
2487 top->type = orig_type;
2488 top->v.index = (size_t) index;
2489 orig_type = type->array.element_type;
2491 path->top_type = orig_type;
2493 if (designator->next != NULL) {
2494 descend_into_subtype(path);
2503 static void advance_current_object(type_path_t *path, size_t top_path_level)
2505 type_path_entry_t *top = get_type_path_top(path);
2507 type_t *type = skip_typeref(top->type);
2508 if (is_type_union(type)) {
2509 /* in unions only the first element is initialized */
2510 top->v.compound_entry = NULL;
2511 } else if (is_type_struct(type)) {
2512 declaration_t *entry = top->v.compound_entry;
2514 entry = entry->next;
2515 top->v.compound_entry = entry;
2516 if (entry != NULL) {
2517 path->top_type = entry->type;
2520 } else if (is_type_array(type)) {
2521 assert(is_type_array(type));
2525 if (!type->array.size_constant || top->v.index < type->array.size) {
2529 assert(!is_type_valid(type));
2533 /* we're past the last member of the current sub-aggregate, try if we
2534 * can ascend in the type hierarchy and continue with another subobject */
2535 size_t len = ARR_LEN(path->path);
2537 if (len > top_path_level) {
2538 ascend_from_subtype(path);
2539 advance_current_object(path, top_path_level);
2541 path->top_type = NULL;
2546 * skip until token is found.
2548 static void skip_until(int type)
2550 while (token.type != type) {
2551 if (token.type == T_EOF)
2558 * skip any {...} blocks until a closing bracket is reached.
2560 static void skip_initializers(void)
2562 if (token.type == '{')
2565 while (token.type != '}') {
2566 if (token.type == T_EOF)
2568 if (token.type == '{') {
2576 static initializer_t *create_empty_initializer(void)
2578 static initializer_t empty_initializer
2579 = { .list = { { INITIALIZER_LIST }, 0 } };
2580 return &empty_initializer;
2584 * Parse a part of an initialiser for a struct or union,
2586 static initializer_t *parse_sub_initializer(type_path_t *path,
2587 type_t *outer_type, size_t top_path_level,
2588 parse_initializer_env_t *env)
2590 if (token.type == '}') {
2591 /* empty initializer */
2592 return create_empty_initializer();
2595 type_t *orig_type = path->top_type;
2596 type_t *type = NULL;
2598 if (orig_type == NULL) {
2599 /* We are initializing an empty compound. */
2601 type = skip_typeref(orig_type);
2604 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2607 designator_t *designator = NULL;
2608 if (token.type == '.' || token.type == '[') {
2609 designator = parse_designation();
2610 goto finish_designator;
2611 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2612 /* GNU-style designator ("identifier: value") */
2613 designator = allocate_ast_zero(sizeof(designator[0]));
2614 designator->source_position = token.source_position;
2615 designator->symbol = token.v.symbol;
2620 /* reset path to toplevel, evaluate designator from there */
2621 ascend_to(path, top_path_level);
2622 if (!walk_designator(path, designator, false)) {
2623 /* can't continue after designation error */
2627 initializer_t *designator_initializer
2628 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2629 designator_initializer->designator.designator = designator;
2630 ARR_APP1(initializer_t*, initializers, designator_initializer);
2632 orig_type = path->top_type;
2633 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2638 if (token.type == '{') {
2639 if (type != NULL && is_type_scalar(type)) {
2640 sub = parse_scalar_initializer(type, env->must_be_constant);
2644 if (env->declaration != NULL) {
2645 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2646 env->declaration->symbol);
2648 errorf(HERE, "extra brace group at end of initializer");
2651 descend_into_subtype(path);
2653 add_anchor_token('}');
2654 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2656 rem_anchor_token('}');
2659 ascend_from_subtype(path);
2663 goto error_parse_next;
2667 /* must be an expression */
2668 expression_t *expression = parse_assignment_expression();
2670 if (env->must_be_constant && !is_initializer_constant(expression)) {
2671 errorf(&expression->base.source_position,
2672 "Initialisation expression '%E' is not constant\n",
2677 /* we are already outside, ... */
2678 type_t *const outer_type_skip = skip_typeref(outer_type);
2679 if (is_type_compound(outer_type_skip) &&
2680 !outer_type_skip->compound.declaration->init.complete) {
2681 goto error_parse_next;
2686 /* handle { "string" } special case */
2687 if ((expression->kind == EXPR_STRING_LITERAL
2688 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2689 && outer_type != NULL) {
2690 sub = initializer_from_expression(outer_type, expression);
2692 if (token.type == ',') {
2695 if (token.type != '}') {
2696 warningf(HERE, "excessive elements in initializer for type '%T'",
2699 /* TODO: eat , ... */
2704 /* descend into subtypes until expression matches type */
2706 orig_type = path->top_type;
2707 type = skip_typeref(orig_type);
2709 sub = initializer_from_expression(orig_type, expression);
2713 if (!is_type_valid(type)) {
2716 if (is_type_scalar(type)) {
2717 errorf(&expression->base.source_position,
2718 "expression '%E' doesn't match expected type '%T'",
2719 expression, orig_type);
2723 descend_into_subtype(path);
2727 /* update largest index of top array */
2728 const type_path_entry_t *first = &path->path[0];
2729 type_t *first_type = first->type;
2730 first_type = skip_typeref(first_type);
2731 if (is_type_array(first_type)) {
2732 size_t index = first->v.index;
2733 if (index > path->max_index)
2734 path->max_index = index;
2738 /* append to initializers list */
2739 ARR_APP1(initializer_t*, initializers, sub);
2742 if (env->declaration != NULL)
2743 warningf(HERE, "excess elements in struct initializer for '%Y'",
2744 env->declaration->symbol);
2746 warningf(HERE, "excess elements in struct initializer");
2750 if (token.type == '}') {
2754 if (token.type == '}') {
2759 /* advance to the next declaration if we are not at the end */
2760 advance_current_object(path, top_path_level);
2761 orig_type = path->top_type;
2762 if (orig_type != NULL)
2763 type = skip_typeref(orig_type);
2769 size_t len = ARR_LEN(initializers);
2770 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2771 initializer_t *result = allocate_ast_zero(size);
2772 result->kind = INITIALIZER_LIST;
2773 result->list.len = len;
2774 memcpy(&result->list.initializers, initializers,
2775 len * sizeof(initializers[0]));
2777 DEL_ARR_F(initializers);
2778 ascend_to(path, top_path_level+1);
2783 skip_initializers();
2784 DEL_ARR_F(initializers);
2785 ascend_to(path, top_path_level+1);
2790 * Parses an initializer. Parsers either a compound literal
2791 * (env->declaration == NULL) or an initializer of a declaration.
2793 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2795 type_t *type = skip_typeref(env->type);
2796 initializer_t *result = NULL;
2799 if (is_type_scalar(type)) {
2800 result = parse_scalar_initializer(type, env->must_be_constant);
2801 } else if (token.type == '{') {
2805 memset(&path, 0, sizeof(path));
2806 path.top_type = env->type;
2807 path.path = NEW_ARR_F(type_path_entry_t, 0);
2809 descend_into_subtype(&path);
2811 add_anchor_token('}');
2812 result = parse_sub_initializer(&path, env->type, 1, env);
2813 rem_anchor_token('}');
2815 max_index = path.max_index;
2816 DEL_ARR_F(path.path);
2820 /* parse_scalar_initializer() also works in this case: we simply
2821 * have an expression without {} around it */
2822 result = parse_scalar_initializer(type, env->must_be_constant);
2825 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2826 * the array type size */
2827 if (is_type_array(type) && type->array.size_expression == NULL
2828 && result != NULL) {
2830 switch (result->kind) {
2831 case INITIALIZER_LIST:
2832 size = max_index + 1;
2835 case INITIALIZER_STRING:
2836 size = result->string.string.size;
2839 case INITIALIZER_WIDE_STRING:
2840 size = result->wide_string.string.size;
2843 case INITIALIZER_DESIGNATOR:
2844 case INITIALIZER_VALUE:
2845 /* can happen for parse errors */
2850 internal_errorf(HERE, "invalid initializer type");
2853 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2854 cnst->base.type = type_size_t;
2855 cnst->conste.v.int_value = size;
2857 type_t *new_type = duplicate_type(type);
2859 new_type->array.size_expression = cnst;
2860 new_type->array.size_constant = true;
2861 new_type->array.size = size;
2862 env->type = new_type;
2870 static declaration_t *append_declaration(declaration_t *declaration);
2872 static declaration_t *parse_compound_type_specifier(bool is_struct)
2874 gnu_attribute_t *attributes = NULL;
2875 decl_modifiers_t modifiers = 0;
2882 symbol_t *symbol = NULL;
2883 declaration_t *declaration = NULL;
2885 if (token.type == T___attribute__) {
2886 modifiers |= parse_attributes(&attributes);
2889 if (token.type == T_IDENTIFIER) {
2890 symbol = token.v.symbol;
2893 namespace_t const namespc =
2894 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2895 declaration = get_declaration(symbol, namespc);
2896 if (declaration != NULL) {
2897 if (declaration->parent_scope != scope &&
2898 (token.type == '{' || token.type == ';')) {
2900 } else if (declaration->init.complete &&
2901 token.type == '{') {
2902 assert(symbol != NULL);
2903 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2904 is_struct ? "struct" : "union", symbol,
2905 &declaration->source_position);
2906 declaration->scope.declarations = NULL;
2909 } else if (token.type != '{') {
2911 parse_error_expected("while parsing struct type specifier",
2912 T_IDENTIFIER, '{', NULL);
2914 parse_error_expected("while parsing union type specifier",
2915 T_IDENTIFIER, '{', NULL);
2921 if (declaration == NULL) {
2922 declaration = allocate_declaration_zero();
2923 declaration->namespc =
2924 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2925 declaration->source_position = token.source_position;
2926 declaration->symbol = symbol;
2927 declaration->parent_scope = scope;
2928 if (symbol != NULL) {
2929 environment_push(declaration);
2931 append_declaration(declaration);
2934 if (token.type == '{') {
2935 declaration->init.complete = true;
2937 parse_compound_type_entries(declaration);
2938 modifiers |= parse_attributes(&attributes);
2941 declaration->modifiers |= modifiers;
2945 static void parse_enum_entries(type_t *const enum_type)
2949 if (token.type == '}') {
2951 errorf(HERE, "empty enum not allowed");
2955 add_anchor_token('}');
2957 if (token.type != T_IDENTIFIER) {
2958 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2960 rem_anchor_token('}');
2964 declaration_t *const entry = allocate_declaration_zero();
2965 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2966 entry->type = enum_type;
2967 entry->symbol = token.v.symbol;
2968 entry->source_position = token.source_position;
2971 if (token.type == '=') {
2973 expression_t *value = parse_constant_expression();
2975 value = create_implicit_cast(value, enum_type);
2976 entry->init.enum_value = value;
2981 record_declaration(entry, false);
2983 if (token.type != ',')
2986 } while (token.type != '}');
2987 rem_anchor_token('}');
2995 static type_t *parse_enum_specifier(void)
2997 gnu_attribute_t *attributes = NULL;
2998 declaration_t *declaration;
3002 if (token.type == T_IDENTIFIER) {
3003 symbol = token.v.symbol;
3006 declaration = get_declaration(symbol, NAMESPACE_ENUM);
3007 } else if (token.type != '{') {
3008 parse_error_expected("while parsing enum type specifier",
3009 T_IDENTIFIER, '{', NULL);
3016 if (declaration == NULL) {
3017 declaration = allocate_declaration_zero();
3018 declaration->namespc = NAMESPACE_ENUM;
3019 declaration->source_position = token.source_position;
3020 declaration->symbol = symbol;
3021 declaration->parent_scope = scope;
3024 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
3025 type->enumt.declaration = declaration;
3027 if (token.type == '{') {
3028 if (declaration->init.complete) {
3029 errorf(HERE, "multiple definitions of enum %Y", symbol);
3031 if (symbol != NULL) {
3032 environment_push(declaration);
3034 append_declaration(declaration);
3035 declaration->init.complete = true;
3037 parse_enum_entries(type);
3038 parse_attributes(&attributes);
3045 * if a symbol is a typedef to another type, return true
3047 static bool is_typedef_symbol(symbol_t *symbol)
3049 const declaration_t *const declaration =
3050 get_declaration(symbol, NAMESPACE_NORMAL);
3052 declaration != NULL &&
3053 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
3056 static type_t *parse_typeof(void)
3063 add_anchor_token(')');
3065 expression_t *expression = NULL;
3067 bool old_type_prop = in_type_prop;
3068 bool old_gcc_extension = in_gcc_extension;
3069 in_type_prop = true;
3071 while (token.type == T___extension__) {
3072 /* This can be a prefix to a typename or an expression. */
3074 in_gcc_extension = true;
3076 switch (token.type) {
3078 if (is_typedef_symbol(token.v.symbol)) {
3079 type = parse_typename();
3081 expression = parse_expression();
3082 type = expression->base.type;
3087 type = parse_typename();
3091 expression = parse_expression();
3092 type = expression->base.type;
3095 in_type_prop = old_type_prop;
3096 in_gcc_extension = old_gcc_extension;
3098 rem_anchor_token(')');
3101 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
3102 typeof_type->typeoft.expression = expression;
3103 typeof_type->typeoft.typeof_type = type;
3110 typedef enum specifiers_t {
3111 SPECIFIER_SIGNED = 1 << 0,
3112 SPECIFIER_UNSIGNED = 1 << 1,
3113 SPECIFIER_LONG = 1 << 2,
3114 SPECIFIER_INT = 1 << 3,
3115 SPECIFIER_DOUBLE = 1 << 4,
3116 SPECIFIER_CHAR = 1 << 5,
3117 SPECIFIER_SHORT = 1 << 6,
3118 SPECIFIER_LONG_LONG = 1 << 7,
3119 SPECIFIER_FLOAT = 1 << 8,
3120 SPECIFIER_BOOL = 1 << 9,
3121 SPECIFIER_VOID = 1 << 10,
3122 SPECIFIER_INT8 = 1 << 11,
3123 SPECIFIER_INT16 = 1 << 12,
3124 SPECIFIER_INT32 = 1 << 13,
3125 SPECIFIER_INT64 = 1 << 14,
3126 SPECIFIER_INT128 = 1 << 15,
3127 SPECIFIER_COMPLEX = 1 << 16,
3128 SPECIFIER_IMAGINARY = 1 << 17,
3131 static type_t *create_builtin_type(symbol_t *const symbol,
3132 type_t *const real_type)
3134 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
3135 type->builtin.symbol = symbol;
3136 type->builtin.real_type = real_type;
3138 type_t *result = typehash_insert(type);
3139 if (type != result) {
3146 static type_t *get_typedef_type(symbol_t *symbol)
3148 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
3149 if (declaration == NULL ||
3150 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
3153 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
3154 type->typedeft.declaration = declaration;
3160 * check for the allowed MS alignment values.
3162 static bool check_alignment_value(long long intvalue)
3164 if (intvalue < 1 || intvalue > 8192) {
3165 errorf(HERE, "illegal alignment value");
3168 unsigned v = (unsigned)intvalue;
3169 for (unsigned i = 1; i <= 8192; i += i) {
3173 errorf(HERE, "alignment must be power of two");
3177 #define DET_MOD(name, tag) do { \
3178 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
3179 *modifiers |= tag; \
3182 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
3184 decl_modifiers_t *modifiers = &specifiers->modifiers;
3187 if (token.type == T_restrict) {
3189 DET_MOD(restrict, DM_RESTRICT);
3191 } else if (token.type != T_IDENTIFIER)
3193 symbol_t *symbol = token.v.symbol;
3194 if (symbol == sym_align) {
3197 if (token.type != T_INTEGER)
3199 if (check_alignment_value(token.v.intvalue)) {
3200 if (specifiers->alignment != 0)
3201 warningf(HERE, "align used more than once");
3202 specifiers->alignment = (unsigned char)token.v.intvalue;
3206 } else if (symbol == sym_allocate) {
3209 if (token.type != T_IDENTIFIER)
3211 (void)token.v.symbol;
3213 } else if (symbol == sym_dllimport) {
3215 DET_MOD(dllimport, DM_DLLIMPORT);
3216 } else if (symbol == sym_dllexport) {
3218 DET_MOD(dllexport, DM_DLLEXPORT);
3219 } else if (symbol == sym_thread) {
3221 DET_MOD(thread, DM_THREAD);
3222 } else if (symbol == sym_naked) {
3224 DET_MOD(naked, DM_NAKED);
3225 } else if (symbol == sym_noinline) {
3227 DET_MOD(noinline, DM_NOINLINE);
3228 } else if (symbol == sym_noreturn) {
3230 DET_MOD(noreturn, DM_NORETURN);
3231 } else if (symbol == sym_nothrow) {
3233 DET_MOD(nothrow, DM_NOTHROW);
3234 } else if (symbol == sym_novtable) {
3236 DET_MOD(novtable, DM_NOVTABLE);
3237 } else if (symbol == sym_property) {
3241 bool is_get = false;
3242 if (token.type != T_IDENTIFIER)
3244 if (token.v.symbol == sym_get) {
3246 } else if (token.v.symbol == sym_put) {
3248 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
3253 if (token.type != T_IDENTIFIER)
3256 if (specifiers->get_property_sym != NULL) {
3257 errorf(HERE, "get property name already specified");
3259 specifiers->get_property_sym = token.v.symbol;
3262 if (specifiers->put_property_sym != NULL) {
3263 errorf(HERE, "put property name already specified");
3265 specifiers->put_property_sym = token.v.symbol;
3269 if (token.type == ',') {
3276 } else if (symbol == sym_selectany) {
3278 DET_MOD(selectany, DM_SELECTANY);
3279 } else if (symbol == sym_uuid) {
3282 if (token.type != T_STRING_LITERAL)
3286 } else if (symbol == sym_deprecated) {
3288 if (specifiers->deprecated != 0)
3289 warningf(HERE, "deprecated used more than once");
3290 specifiers->deprecated = 1;
3291 if (token.type == '(') {
3293 if (token.type == T_STRING_LITERAL) {
3294 specifiers->deprecated_string = token.v.string.begin;
3297 errorf(HERE, "string literal expected");
3301 } else if (symbol == sym_noalias) {
3303 DET_MOD(noalias, DM_NOALIAS);
3305 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3307 if (token.type == '(')
3311 if (token.type == ',')
3318 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3320 declaration_t *const decl = allocate_declaration_zero();
3321 decl->source_position = *HERE;
3322 decl->declared_storage_class = storage_class;
3323 decl->storage_class =
3324 storage_class != STORAGE_CLASS_NONE || scope == file_scope ?
3325 storage_class : STORAGE_CLASS_AUTO;
3326 decl->symbol = symbol;
3327 decl->implicit = true;
3328 record_declaration(decl, false);
3333 * Finish the construction of a struct type by calculating
3334 * its size, offsets, alignment.
3336 static void finish_struct_type(compound_type_t *type) {
3337 if (type->declaration == NULL)
3339 declaration_t *struct_decl = type->declaration;
3340 if (! struct_decl->init.complete)
3345 il_alignment_t alignment = 1;
3346 bool need_pad = false;
3348 declaration_t *entry = struct_decl->scope.declarations;
3349 for (; entry != NULL; entry = entry->next) {
3350 if (entry->namespc != NAMESPACE_NORMAL)
3353 type_t *m_type = skip_typeref(entry->type);
3354 if (! is_type_valid(m_type)) {
3355 /* simply ignore errors here */
3358 il_alignment_t m_alignment = m_type->base.alignment;
3359 if (m_alignment > alignment)
3360 alignment = m_alignment;
3362 offset = (size + m_alignment - 1) & -m_alignment;
3366 entry->offset = offset;
3367 size = offset + m_type->base.size;
3369 if (type->base.alignment != 0) {
3370 alignment = type->base.alignment;
3373 offset = (size + alignment - 1) & -alignment;
3377 if (warning.padded && need_pad) {
3378 warningf(&struct_decl->source_position,
3379 "'%#T' needs padding", type, struct_decl->symbol);
3381 if (warning.packed && !need_pad) {
3382 warningf(&struct_decl->source_position,
3383 "superfluous packed attribute on '%#T'",
3384 type, struct_decl->symbol);
3387 type->base.size = offset;
3388 type->base.alignment = alignment;
3392 * Finish the construction of an union type by calculating
3393 * its size and alignment.
3395 static void finish_union_type(compound_type_t *type) {
3396 if (type->declaration == NULL)
3398 declaration_t *union_decl = type->declaration;
3399 if (! union_decl->init.complete)
3403 il_alignment_t alignment = 1;
3405 declaration_t *entry = union_decl->scope.declarations;
3406 for (; entry != NULL; entry = entry->next) {
3407 if (entry->namespc != NAMESPACE_NORMAL)
3410 type_t *m_type = skip_typeref(entry->type);
3411 if (! is_type_valid(m_type))
3415 if (m_type->base.size > size)
3416 size = m_type->base.size;
3417 if (m_type->base.alignment > alignment)
3418 alignment = m_type->base.alignment;
3420 if (type->base.alignment != 0) {
3421 alignment = type->base.alignment;
3423 size = (size + alignment - 1) & -alignment;
3424 type->base.size = size;
3425 type->base.alignment = alignment;
3428 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3430 type_t *type = NULL;
3431 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3432 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3433 unsigned type_specifiers = 0;
3434 bool newtype = false;
3435 bool saw_error = false;
3436 bool old_gcc_extension = in_gcc_extension;
3438 specifiers->source_position = token.source_position;
3441 specifiers->modifiers
3442 |= parse_attributes(&specifiers->gnu_attributes);
3443 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3444 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3446 switch (token.type) {
3449 #define MATCH_STORAGE_CLASS(token, class) \
3451 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3452 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3454 specifiers->declared_storage_class = class; \
3458 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3459 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3460 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3461 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3462 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3467 add_anchor_token(')');
3468 parse_microsoft_extended_decl_modifier(specifiers);
3469 rem_anchor_token(')');
3474 switch (specifiers->declared_storage_class) {
3475 case STORAGE_CLASS_NONE:
3476 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3479 case STORAGE_CLASS_EXTERN:
3480 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3483 case STORAGE_CLASS_STATIC:
3484 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3488 errorf(HERE, "multiple storage classes in declaration specifiers");
3494 /* type qualifiers */
3495 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3497 qualifiers |= qualifier; \
3501 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3502 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3503 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3504 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3505 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3506 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3507 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3508 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3510 case T___extension__:
3512 in_gcc_extension = true;
3515 /* type specifiers */
3516 #define MATCH_SPECIFIER(token, specifier, name) \
3519 if (type_specifiers & specifier) { \
3520 errorf(HERE, "multiple " name " type specifiers given"); \
3522 type_specifiers |= specifier; \
3526 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3527 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3528 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3529 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3530 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3531 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3532 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3533 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3534 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3535 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3536 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3537 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3538 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3539 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3540 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3541 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3543 case T__forceinline:
3544 /* only in microsoft mode */
3545 specifiers->modifiers |= DM_FORCEINLINE;
3550 specifiers->is_inline = true;
3555 if (type_specifiers & SPECIFIER_LONG_LONG) {
3556 errorf(HERE, "multiple type specifiers given");
3557 } else if (type_specifiers & SPECIFIER_LONG) {
3558 type_specifiers |= SPECIFIER_LONG_LONG;
3560 type_specifiers |= SPECIFIER_LONG;
3565 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3567 type->compound.declaration = parse_compound_type_specifier(true);
3568 finish_struct_type(&type->compound);
3572 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3573 type->compound.declaration = parse_compound_type_specifier(false);
3574 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3575 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3576 finish_union_type(&type->compound);
3580 type = parse_enum_specifier();
3583 type = parse_typeof();
3585 case T___builtin_va_list:
3586 type = duplicate_type(type_valist);
3590 case T_IDENTIFIER: {
3591 /* only parse identifier if we haven't found a type yet */
3592 if (type != NULL || type_specifiers != 0) {
3593 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3594 * declaration, so it doesn't generate errors about expecting '(' or
3596 switch (look_ahead(1)->type) {
3603 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3606 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3611 goto finish_specifiers;
3615 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3616 if (typedef_type == NULL) {
3617 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3618 * declaration, so it doesn't generate 'implicit int' followed by more
3619 * errors later on. */
3620 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3625 errorf(HERE, "%K does not name a type", &token);
3627 declaration_t *const decl =
3628 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3630 type = allocate_type_zero(TYPE_TYPEDEF, HERE);
3631 type->typedeft.declaration = decl;
3635 if (la1_type == '*')
3636 goto finish_specifiers;
3641 goto finish_specifiers;
3646 type = typedef_type;
3650 /* function specifier */
3652 goto finish_specifiers;
3657 in_gcc_extension = old_gcc_extension;
3659 if (type == NULL || (saw_error && type_specifiers != 0)) {
3660 atomic_type_kind_t atomic_type;
3662 /* match valid basic types */
3663 switch(type_specifiers) {
3664 case SPECIFIER_VOID:
3665 atomic_type = ATOMIC_TYPE_VOID;
3667 case SPECIFIER_CHAR:
3668 atomic_type = ATOMIC_TYPE_CHAR;
3670 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3671 atomic_type = ATOMIC_TYPE_SCHAR;
3673 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3674 atomic_type = ATOMIC_TYPE_UCHAR;
3676 case SPECIFIER_SHORT:
3677 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3678 case SPECIFIER_SHORT | SPECIFIER_INT:
3679 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3680 atomic_type = ATOMIC_TYPE_SHORT;
3682 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3683 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3684 atomic_type = ATOMIC_TYPE_USHORT;
3687 case SPECIFIER_SIGNED:
3688 case SPECIFIER_SIGNED | SPECIFIER_INT:
3689 atomic_type = ATOMIC_TYPE_INT;
3691 case SPECIFIER_UNSIGNED:
3692 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3693 atomic_type = ATOMIC_TYPE_UINT;
3695 case SPECIFIER_LONG:
3696 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3697 case SPECIFIER_LONG | SPECIFIER_INT:
3698 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3699 atomic_type = ATOMIC_TYPE_LONG;
3701 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3702 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3703 atomic_type = ATOMIC_TYPE_ULONG;
3706 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3707 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3708 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3709 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3711 atomic_type = ATOMIC_TYPE_LONGLONG;
3712 goto warn_about_long_long;
3714 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3715 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3717 atomic_type = ATOMIC_TYPE_ULONGLONG;
3718 warn_about_long_long:
3719 if (warning.long_long) {
3720 warningf(&specifiers->source_position,
3721 "ISO C90 does not support 'long long'");
3725 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3726 atomic_type = unsigned_int8_type_kind;
3729 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3730 atomic_type = unsigned_int16_type_kind;
3733 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3734 atomic_type = unsigned_int32_type_kind;
3737 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3738 atomic_type = unsigned_int64_type_kind;
3741 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3742 atomic_type = unsigned_int128_type_kind;
3745 case SPECIFIER_INT8:
3746 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3747 atomic_type = int8_type_kind;
3750 case SPECIFIER_INT16:
3751 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3752 atomic_type = int16_type_kind;
3755 case SPECIFIER_INT32:
3756 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3757 atomic_type = int32_type_kind;
3760 case SPECIFIER_INT64:
3761 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3762 atomic_type = int64_type_kind;
3765 case SPECIFIER_INT128:
3766 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3767 atomic_type = int128_type_kind;
3770 case SPECIFIER_FLOAT:
3771 atomic_type = ATOMIC_TYPE_FLOAT;
3773 case SPECIFIER_DOUBLE:
3774 atomic_type = ATOMIC_TYPE_DOUBLE;
3776 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3777 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3779 case SPECIFIER_BOOL:
3780 atomic_type = ATOMIC_TYPE_BOOL;
3782 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3783 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3784 atomic_type = ATOMIC_TYPE_FLOAT;
3786 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3787 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3788 atomic_type = ATOMIC_TYPE_DOUBLE;
3790 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3791 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3792 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3795 /* invalid specifier combination, give an error message */
3796 if (type_specifiers == 0) {
3801 if (warning.implicit_int) {
3802 warningf(HERE, "no type specifiers in declaration, using 'int'");
3804 atomic_type = ATOMIC_TYPE_INT;
3807 errorf(HERE, "no type specifiers given in declaration");
3809 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3810 (type_specifiers & SPECIFIER_UNSIGNED)) {
3811 errorf(HERE, "signed and unsigned specifiers given");
3812 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3813 errorf(HERE, "only integer types can be signed or unsigned");
3815 errorf(HERE, "multiple datatypes in declaration");
3820 if (type_specifiers & SPECIFIER_COMPLEX) {
3821 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3822 type->complex.akind = atomic_type;
3823 } else if (type_specifiers & SPECIFIER_IMAGINARY) {
3824 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3825 type->imaginary.akind = atomic_type;
3827 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3828 type->atomic.akind = atomic_type;
3831 } else if (type_specifiers != 0) {
3832 errorf(HERE, "multiple datatypes in declaration");
3835 /* FIXME: check type qualifiers here */
3837 type->base.qualifiers = qualifiers;
3838 type->base.modifiers = modifiers;
3840 type_t *result = typehash_insert(type);
3841 if (newtype && result != type) {
3845 specifiers->type = result;
3849 specifiers->type = type_error_type;
3853 static type_qualifiers_t parse_type_qualifiers(void)
3855 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3858 switch(token.type) {
3859 /* type qualifiers */
3860 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3861 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3862 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3863 /* microsoft extended type modifiers */
3864 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3865 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3866 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3867 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3868 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3877 * Parses an K&R identifier list and return a list of declarations.
3879 * @param last points to the last declaration in the list
3880 * @return the list of declarations
3882 static declaration_t *parse_identifier_list(declaration_t **last)
3884 declaration_t *declarations = NULL;
3885 declaration_t *last_declaration = NULL;
3887 declaration_t *const declaration = allocate_declaration_zero();
3888 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3889 declaration->source_position = token.source_position;
3890 declaration->symbol = token.v.symbol;
3893 if (last_declaration != NULL) {
3894 last_declaration->next = declaration;
3896 declarations = declaration;
3898 last_declaration = declaration;
3900 if (token.type != ',') {
3904 } while (token.type == T_IDENTIFIER);
3906 *last = last_declaration;
3907 return declarations;
3910 static type_t *automatic_type_conversion(type_t *orig_type);
3912 static void semantic_parameter(declaration_t *declaration)
3914 /* TODO: improve error messages */
3915 source_position_t const* const pos = &declaration->source_position;
3917 switch (declaration->declared_storage_class) {
3918 case STORAGE_CLASS_TYPEDEF:
3919 errorf(pos, "typedef not allowed in parameter list");
3922 /* Allowed storage classes */
3923 case STORAGE_CLASS_NONE:
3924 case STORAGE_CLASS_REGISTER:
3928 errorf(pos, "parameter may only have none or register storage class");
3932 type_t *const orig_type = declaration->type;
3933 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3934 * sugar. Turn it into a pointer.
3935 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3936 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3938 type_t *const type = automatic_type_conversion(orig_type);
3939 declaration->type = type;
3941 if (is_type_incomplete(skip_typeref(type))) {
3942 errorf(pos, "parameter '%#T' is of incomplete type",
3943 orig_type, declaration->symbol);
3947 static declaration_t *parse_parameter(void)
3949 declaration_specifiers_t specifiers;
3950 memset(&specifiers, 0, sizeof(specifiers));
3952 parse_declaration_specifiers(&specifiers);
3954 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3960 * Parses a function type parameter list and return a list of declarations.
3962 * @param last point to the last element of the list
3963 * @return the parameter list
3965 static declaration_t *parse_parameters(function_type_t *type, declaration_t **last)
3967 declaration_t *declarations = NULL;
3970 add_anchor_token(')');
3971 int saved_comma_state = save_and_reset_anchor_state(',');
3973 if (token.type == T_IDENTIFIER &&
3974 !is_typedef_symbol(token.v.symbol)) {
3975 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
3976 if (la1_type == ',' || la1_type == ')') {
3977 type->kr_style_parameters = true;
3978 declarations = parse_identifier_list(last);
3979 goto parameters_finished;
3983 if (token.type == ')') {
3984 type->unspecified_parameters = 1;
3985 goto parameters_finished;
3988 declaration_t *declaration;
3989 declaration_t *last_declaration = NULL;
3990 function_parameter_t *parameter;
3991 function_parameter_t *last_parameter = NULL;
3994 switch(token.type) {
3998 goto parameters_finished;
4001 case T___extension__:
4003 declaration = parse_parameter();
4005 /* func(void) is not a parameter */
4006 if (last_parameter == NULL
4007 && token.type == ')'
4008 && declaration->symbol == NULL
4009 && skip_typeref(declaration->type) == type_void) {
4010 goto parameters_finished;
4012 semantic_parameter(declaration);
4014 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
4015 memset(parameter, 0, sizeof(parameter[0]));
4016 parameter->type = declaration->type;
4018 if (last_parameter != NULL) {
4019 last_declaration->next = declaration;
4020 last_parameter->next = parameter;
4022 type->parameters = parameter;
4023 declarations = declaration;
4025 last_parameter = parameter;
4026 last_declaration = declaration;
4030 goto parameters_finished;
4032 if (token.type != ',') {
4033 goto parameters_finished;
4039 parameters_finished:
4040 rem_anchor_token(')');
4043 restore_anchor_state(',', saved_comma_state);
4044 *last = last_declaration;
4045 return declarations;
4048 restore_anchor_state(',', saved_comma_state);
4053 typedef enum construct_type_kind_t {
4058 } construct_type_kind_t;
4060 typedef struct construct_type_t construct_type_t;
4061 struct construct_type_t {
4062 construct_type_kind_t kind;
4063 construct_type_t *next;
4066 typedef struct parsed_pointer_t parsed_pointer_t;
4067 struct parsed_pointer_t {
4068 construct_type_t construct_type;
4069 type_qualifiers_t type_qualifiers;
4072 typedef struct construct_function_type_t construct_function_type_t;
4073 struct construct_function_type_t {
4074 construct_type_t construct_type;
4075 type_t *function_type;
4078 typedef struct parsed_array_t parsed_array_t;
4079 struct parsed_array_t {
4080 construct_type_t construct_type;
4081 type_qualifiers_t type_qualifiers;
4087 typedef struct construct_base_type_t construct_base_type_t;
4088 struct construct_base_type_t {
4089 construct_type_t construct_type;
4093 static construct_type_t *parse_pointer_declarator(void)
4097 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
4098 memset(pointer, 0, sizeof(pointer[0]));
4099 pointer->construct_type.kind = CONSTRUCT_POINTER;
4100 pointer->type_qualifiers = parse_type_qualifiers();
4102 return (construct_type_t*) pointer;
4105 static construct_type_t *parse_array_declarator(void)
4108 add_anchor_token(']');
4110 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
4111 memset(array, 0, sizeof(array[0]));
4112 array->construct_type.kind = CONSTRUCT_ARRAY;
4114 if (token.type == T_static) {
4115 array->is_static = true;
4119 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
4120 if (type_qualifiers != 0) {
4121 if (token.type == T_static) {
4122 array->is_static = true;
4126 array->type_qualifiers = type_qualifiers;
4128 if (token.type == '*' && look_ahead(1)->type == ']') {
4129 array->is_variable = true;
4131 } else if (token.type != ']') {
4132 array->size = parse_assignment_expression();
4135 rem_anchor_token(']');
4139 return (construct_type_t*) array;
4142 static construct_type_t *parse_function_declarator(declaration_t *declaration)
4145 if (declaration != NULL) {
4146 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
4148 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
4150 if (mask & (mask-1)) {
4151 const char *first = NULL, *second = NULL;
4153 /* more than one calling convention set */
4154 if (declaration->modifiers & DM_CDECL) {
4155 if (first == NULL) first = "cdecl";
4156 else if (second == NULL) second = "cdecl";
4158 if (declaration->modifiers & DM_STDCALL) {
4159 if (first == NULL) first = "stdcall";
4160 else if (second == NULL) second = "stdcall";
4162 if (declaration->modifiers & DM_FASTCALL) {
4163 if (first == NULL) first = "fastcall";
4164 else if (second == NULL) second = "fastcall";
4166 if (declaration->modifiers & DM_THISCALL) {
4167 if (first == NULL) first = "thiscall";
4168 else if (second == NULL) second = "thiscall";
4170 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
4173 if (declaration->modifiers & DM_CDECL)
4174 type->function.calling_convention = CC_CDECL;
4175 else if (declaration->modifiers & DM_STDCALL)
4176 type->function.calling_convention = CC_STDCALL;
4177 else if (declaration->modifiers & DM_FASTCALL)
4178 type->function.calling_convention = CC_FASTCALL;
4179 else if (declaration->modifiers & DM_THISCALL)
4180 type->function.calling_convention = CC_THISCALL;
4182 type = allocate_type_zero(TYPE_FUNCTION, HERE);
4185 declaration_t *last;
4186 declaration_t *parameters = parse_parameters(&type->function, &last);
4187 if (declaration != NULL) {
4188 declaration->scope.declarations = parameters;
4189 declaration->scope.last_declaration = last;
4192 construct_function_type_t *construct_function_type =
4193 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
4194 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
4195 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
4196 construct_function_type->function_type = type;
4198 return &construct_function_type->construct_type;
4201 static void fix_declaration_type(declaration_t *declaration)
4203 decl_modifiers_t declaration_modifiers = declaration->modifiers;
4204 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
4206 if (declaration_modifiers & DM_TRANSPARENT_UNION)
4207 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4209 if (declaration->type->base.modifiers == type_modifiers)
4212 type_t *copy = duplicate_type(declaration->type);
4213 copy->base.modifiers = type_modifiers;
4215 type_t *result = typehash_insert(copy);
4216 if (result != copy) {
4217 obstack_free(type_obst, copy);
4220 declaration->type = result;
4223 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
4224 bool may_be_abstract)
4226 /* construct a single linked list of construct_type_t's which describe
4227 * how to construct the final declarator type */
4228 construct_type_t *first = NULL;
4229 construct_type_t *last = NULL;
4230 gnu_attribute_t *attributes = NULL;
4232 decl_modifiers_t modifiers = parse_attributes(&attributes);
4235 while (token.type == '*') {
4236 construct_type_t *type = parse_pointer_declarator();
4246 /* TODO: find out if this is correct */
4247 modifiers |= parse_attributes(&attributes);
4250 if (declaration != NULL)
4251 declaration->modifiers |= modifiers;
4253 construct_type_t *inner_types = NULL;
4255 switch(token.type) {
4257 if (declaration == NULL) {
4258 errorf(HERE, "no identifier expected in typename");
4260 declaration->symbol = token.v.symbol;
4261 declaration->source_position = token.source_position;
4267 add_anchor_token(')');
4268 inner_types = parse_inner_declarator(declaration, may_be_abstract);
4269 if (inner_types != NULL) {
4270 /* All later declarators only modify the return type, not declaration */
4273 rem_anchor_token(')');
4277 if (may_be_abstract)
4279 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
4284 construct_type_t *p = last;
4287 construct_type_t *type;
4288 switch(token.type) {
4290 type = parse_function_declarator(declaration);
4293 type = parse_array_declarator();
4296 goto declarator_finished;
4299 /* insert in the middle of the list (behind p) */
4301 type->next = p->next;
4312 declarator_finished:
4313 /* append inner_types at the end of the list, we don't to set last anymore
4314 * as it's not needed anymore */
4316 assert(first == NULL);
4317 first = inner_types;
4319 last->next = inner_types;
4327 static void parse_declaration_attributes(declaration_t *declaration)
4329 gnu_attribute_t *attributes = NULL;
4330 decl_modifiers_t modifiers = parse_attributes(&attributes);
4332 if (declaration == NULL)
4335 declaration->modifiers |= modifiers;
4336 /* check if we have these stupid mode attributes... */
4337 type_t *old_type = declaration->type;
4338 if (old_type == NULL)
4341 gnu_attribute_t *attribute = attributes;
4342 for ( ; attribute != NULL; attribute = attribute->next) {
4343 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
4346 atomic_type_kind_t akind = attribute->u.akind;
4347 if (!is_type_signed(old_type)) {
4349 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
4350 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
4351 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
4352 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
4354 panic("invalid akind in mode attribute");
4358 = make_atomic_type(akind, old_type->base.qualifiers);
4362 static type_t *construct_declarator_type(construct_type_t *construct_list,
4365 construct_type_t *iter = construct_list;
4366 for( ; iter != NULL; iter = iter->next) {
4367 switch(iter->kind) {
4368 case CONSTRUCT_INVALID:
4369 internal_errorf(HERE, "invalid type construction found");
4370 case CONSTRUCT_FUNCTION: {
4371 construct_function_type_t *construct_function_type
4372 = (construct_function_type_t*) iter;
4374 type_t *function_type = construct_function_type->function_type;
4376 function_type->function.return_type = type;
4378 type_t *skipped_return_type = skip_typeref(type);
4380 if (is_type_function(skipped_return_type)) {
4381 errorf(HERE, "function returning function is not allowed");
4382 } else if (is_type_array(skipped_return_type)) {
4383 errorf(HERE, "function returning array is not allowed");
4385 if (skipped_return_type->base.qualifiers != 0) {
4387 "type qualifiers in return type of function type are meaningless");
4391 type = function_type;
4395 case CONSTRUCT_POINTER: {
4396 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4397 type = make_pointer_type(type, parsed_pointer->type_qualifiers);
4401 case CONSTRUCT_ARRAY: {
4402 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4403 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
4405 expression_t *size_expression = parsed_array->size;
4406 if (size_expression != NULL) {
4408 = create_implicit_cast(size_expression, type_size_t);
4411 array_type->base.qualifiers = parsed_array->type_qualifiers;
4412 array_type->array.element_type = type;
4413 array_type->array.is_static = parsed_array->is_static;
4414 array_type->array.is_variable = parsed_array->is_variable;
4415 array_type->array.size_expression = size_expression;
4417 if (size_expression != NULL) {
4418 if (is_constant_expression(size_expression)) {
4419 array_type->array.size_constant = true;
4420 array_type->array.size
4421 = fold_constant(size_expression);
4423 array_type->array.is_vla = true;
4427 type_t *skipped_type = skip_typeref(type);
4429 if (is_type_incomplete(skipped_type)) {
4430 errorf(HERE, "array of incomplete type '%T' is not allowed", type);
4431 } else if (is_type_function(skipped_type)) {
4432 errorf(HERE, "array of functions is not allowed");
4439 type_t *hashed_type = typehash_insert(type);
4440 if (hashed_type != type) {
4441 /* the function type was constructed earlier freeing it here will
4442 * destroy other types... */
4443 if (iter->kind != CONSTRUCT_FUNCTION) {
4453 static declaration_t *parse_declarator(
4454 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4456 declaration_t *const declaration = allocate_declaration_zero();
4457 declaration->source_position = specifiers->source_position;
4458 declaration->declared_storage_class = specifiers->declared_storage_class;
4459 declaration->modifiers = specifiers->modifiers;
4460 declaration->deprecated_string = specifiers->deprecated_string;
4461 declaration->get_property_sym = specifiers->get_property_sym;
4462 declaration->put_property_sym = specifiers->put_property_sym;
4463 declaration->is_inline = specifiers->is_inline;
4465 declaration->storage_class = specifiers->declared_storage_class;
4466 if (declaration->storage_class == STORAGE_CLASS_NONE &&
4467 scope != file_scope) {
4468 declaration->storage_class = STORAGE_CLASS_AUTO;
4471 if (specifiers->alignment != 0) {
4472 /* TODO: add checks here */
4473 declaration->alignment = specifiers->alignment;
4476 construct_type_t *construct_type
4477 = parse_inner_declarator(declaration, may_be_abstract);
4478 type_t *const type = specifiers->type;
4479 declaration->type = construct_declarator_type(construct_type, type);
4481 parse_declaration_attributes(declaration);
4483 fix_declaration_type(declaration);
4485 if (construct_type != NULL) {
4486 obstack_free(&temp_obst, construct_type);
4492 static type_t *parse_abstract_declarator(type_t *base_type)
4494 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4496 type_t *result = construct_declarator_type(construct_type, base_type);
4497 if (construct_type != NULL) {
4498 obstack_free(&temp_obst, construct_type);
4504 static declaration_t *append_declaration(declaration_t* const declaration)
4506 if (last_declaration != NULL) {
4507 last_declaration->next = declaration;
4509 scope->declarations = declaration;
4511 last_declaration = declaration;
4516 * Check if the declaration of main is suspicious. main should be a
4517 * function with external linkage, returning int, taking either zero
4518 * arguments, two, or three arguments of appropriate types, ie.
4520 * int main([ int argc, char **argv [, char **env ] ]).
4522 * @param decl the declaration to check
4523 * @param type the function type of the declaration
4525 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4527 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4528 warningf(&decl->source_position,
4529 "'main' is normally a non-static function");
4531 if (!types_compatible(skip_typeref(func_type->return_type), type_int)) {
4532 warningf(&decl->source_position,
4533 "return type of 'main' should be 'int', but is '%T'",
4534 func_type->return_type);
4536 const function_parameter_t *parm = func_type->parameters;
4538 type_t *const first_type = parm->type;
4539 if (!types_compatible(skip_typeref(first_type), type_int)) {
4540 warningf(&decl->source_position,
4541 "first argument of 'main' should be 'int', but is '%T'", first_type);
4545 type_t *const second_type = parm->type;
4546 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4547 warningf(&decl->source_position,
4548 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4552 type_t *const third_type = parm->type;
4553 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4554 warningf(&decl->source_position,
4555 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4559 goto warn_arg_count;
4563 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4569 * Check if a symbol is the equal to "main".
4571 static bool is_sym_main(const symbol_t *const sym)
4573 return strcmp(sym->string, "main") == 0;
4576 static declaration_t *record_declaration(
4577 declaration_t *const declaration,
4578 const bool is_definition)
4580 const symbol_t *const symbol = declaration->symbol;
4581 const namespace_t namespc = (namespace_t)declaration->namespc;
4583 assert(symbol != NULL);
4584 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4586 type_t *const orig_type = declaration->type;
4587 type_t *const type = skip_typeref(orig_type);
4588 if (is_type_function(type) &&
4589 type->function.unspecified_parameters &&
4590 warning.strict_prototypes &&
4591 previous_declaration == NULL) {
4592 warningf(&declaration->source_position,
4593 "function declaration '%#T' is not a prototype",
4597 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4598 check_type_of_main(declaration, &type->function);
4601 if (warning.nested_externs &&
4602 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4603 scope != file_scope) {
4604 warningf(&declaration->source_position,
4605 "nested extern declaration of '%#T'", declaration->type, symbol);
4608 assert(declaration != previous_declaration);
4609 if (previous_declaration != NULL &&
4610 previous_declaration->parent_scope == ¤t_function->scope &&
4611 scope->depth == previous_declaration->parent_scope->depth + 1) {
4612 errorf(&declaration->source_position,
4613 "declaration '%#T' redeclares the parameter '%#T' (declared %P)",
4614 orig_type, symbol, previous_declaration->type, symbol,
4615 &previous_declaration->source_position);
4618 if (previous_declaration != NULL &&
4619 previous_declaration->parent_scope == scope) {
4620 /* can happen for K&R style declarations */
4621 if (previous_declaration->type == NULL) {
4622 previous_declaration->type = declaration->type;
4625 const type_t *prev_type = skip_typeref(previous_declaration->type);
4626 if (!types_compatible(type, prev_type)) {
4627 errorf(&declaration->source_position,
4628 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4629 orig_type, symbol, previous_declaration->type, symbol,
4630 &previous_declaration->source_position);
4632 unsigned old_storage_class = previous_declaration->storage_class;
4633 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4634 errorf(&declaration->source_position,
4635 "redeclaration of enum entry '%Y' (declared %P)",
4636 symbol, &previous_declaration->source_position);
4637 return previous_declaration;
4640 if (warning.redundant_decls &&
4642 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4643 !(previous_declaration->modifiers & DM_USED) &&
4644 !previous_declaration->used) {
4645 warningf(&previous_declaration->source_position,
4646 "unnecessary static forward declaration for '%#T'",
4647 previous_declaration->type, symbol);
4650 unsigned new_storage_class = declaration->storage_class;
4652 if (is_type_incomplete(prev_type)) {
4653 previous_declaration->type = type;
4657 /* pretend no storage class means extern for function
4658 * declarations (except if the previous declaration is neither
4659 * none nor extern) */
4660 if (is_type_function(type)) {
4661 if (prev_type->function.unspecified_parameters) {
4662 previous_declaration->type = type;
4666 switch (old_storage_class) {
4667 case STORAGE_CLASS_NONE:
4668 old_storage_class = STORAGE_CLASS_EXTERN;
4671 case STORAGE_CLASS_EXTERN:
4672 if (is_definition) {
4673 if (warning.missing_prototypes &&
4674 prev_type->function.unspecified_parameters &&
4675 !is_sym_main(symbol)) {
4676 warningf(&declaration->source_position,
4677 "no previous prototype for '%#T'",
4680 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4681 new_storage_class = STORAGE_CLASS_EXTERN;
4690 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4691 new_storage_class == STORAGE_CLASS_EXTERN) {
4692 warn_redundant_declaration:
4693 if (!is_definition &&
4694 warning.redundant_decls &&
4695 is_type_valid(prev_type) &&
4696 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4697 warningf(&declaration->source_position,
4698 "redundant declaration for '%Y' (declared %P)",
4699 symbol, &previous_declaration->source_position);
4701 } else if (current_function == NULL) {
4702 if (old_storage_class != STORAGE_CLASS_STATIC &&
4703 new_storage_class == STORAGE_CLASS_STATIC) {
4704 errorf(&declaration->source_position,
4705 "static declaration of '%Y' follows non-static declaration (declared %P)",
4706 symbol, &previous_declaration->source_position);
4707 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4708 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4709 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4711 goto warn_redundant_declaration;
4713 } else if (is_type_valid(prev_type)) {
4714 if (old_storage_class == new_storage_class) {
4715 errorf(&declaration->source_position,
4716 "redeclaration of '%Y' (declared %P)",
4717 symbol, &previous_declaration->source_position);
4719 errorf(&declaration->source_position,
4720 "redeclaration of '%Y' with different linkage (declared %P)",
4721 symbol, &previous_declaration->source_position);
4726 previous_declaration->modifiers |= declaration->modifiers;
4727 previous_declaration->is_inline |= declaration->is_inline;
4728 return previous_declaration;
4729 } else if (is_type_function(type)) {
4730 if (is_definition &&
4731 declaration->storage_class != STORAGE_CLASS_STATIC) {
4732 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4733 warningf(&declaration->source_position,
4734 "no previous prototype for '%#T'", orig_type, symbol);
4735 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4736 warningf(&declaration->source_position,
4737 "no previous declaration for '%#T'", orig_type,
4742 if (warning.missing_declarations &&
4743 scope == file_scope && (
4744 declaration->storage_class == STORAGE_CLASS_NONE ||
4745 declaration->storage_class == STORAGE_CLASS_THREAD
4747 warningf(&declaration->source_position,
4748 "no previous declaration for '%#T'", orig_type, symbol);
4752 assert(declaration->parent_scope == NULL);
4753 assert(scope != NULL);
4755 declaration->parent_scope = scope;
4757 environment_push(declaration);
4758 return append_declaration(declaration);
4761 static void parser_error_multiple_definition(declaration_t *declaration,
4762 const source_position_t *source_position)
4764 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4765 declaration->symbol, &declaration->source_position);
4768 static bool is_declaration_specifier(const token_t *token,
4769 bool only_specifiers_qualifiers)
4771 switch (token->type) {
4776 return is_typedef_symbol(token->v.symbol);
4778 case T___extension__:
4780 return !only_specifiers_qualifiers;
4787 static void parse_init_declarator_rest(declaration_t *declaration)
4791 type_t *orig_type = declaration->type;
4792 type_t *type = skip_typeref(orig_type);
4794 if (declaration->init.initializer != NULL) {
4795 parser_error_multiple_definition(declaration, HERE);
4798 bool must_be_constant = false;
4799 if (declaration->storage_class == STORAGE_CLASS_STATIC ||
4800 declaration->storage_class == STORAGE_CLASS_THREAD_STATIC ||
4801 declaration->parent_scope == file_scope) {
4802 must_be_constant = true;
4805 if (is_type_function(type)) {
4806 errorf(&declaration->source_position,
4807 "function '%#T' is initialized like a variable",
4808 orig_type, declaration->symbol);
4809 orig_type = type_error_type;
4812 parse_initializer_env_t env;
4813 env.type = orig_type;
4814 env.must_be_constant = must_be_constant;
4815 env.declaration = current_init_decl = declaration;
4817 initializer_t *initializer = parse_initializer(&env);
4818 current_init_decl = NULL;
4820 if (!is_type_function(type)) {
4821 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
4822 * the array type size */
4823 declaration->type = env.type;
4824 declaration->init.initializer = initializer;
4828 /* parse rest of a declaration without any declarator */
4829 static void parse_anonymous_declaration_rest(
4830 const declaration_specifiers_t *specifiers)
4834 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) {
4835 warningf(&specifiers->source_position,
4836 "useless storage class in empty declaration");
4839 type_t *type = specifiers->type;
4840 switch (type->kind) {
4841 case TYPE_COMPOUND_STRUCT:
4842 case TYPE_COMPOUND_UNION: {
4843 if (type->compound.declaration->symbol == NULL) {
4844 warningf(&specifiers->source_position,
4845 "unnamed struct/union that defines no instances");
4854 warningf(&specifiers->source_position, "empty declaration");
4858 #ifdef RECORD_EMPTY_DECLARATIONS
4859 declaration_t *const declaration = allocate_declaration_zero();
4860 declaration->type = specifiers->type;
4861 declaration->declared_storage_class = specifiers->declared_storage_class;
4862 declaration->source_position = specifiers->source_position;
4863 declaration->modifiers = specifiers->modifiers;
4864 declaration->storage_class = STORAGE_CLASS_NONE;
4866 append_declaration(declaration);
4870 static void parse_declaration_rest(declaration_t *ndeclaration,
4871 const declaration_specifiers_t *specifiers,
4872 parsed_declaration_func finished_declaration)
4874 add_anchor_token(';');
4875 add_anchor_token(',');
4877 declaration_t *declaration =
4878 finished_declaration(ndeclaration, token.type == '=');
4880 type_t *orig_type = declaration->type;
4881 type_t *type = skip_typeref(orig_type);
4883 if (type->kind != TYPE_FUNCTION &&
4884 declaration->is_inline &&
4885 is_type_valid(type)) {
4886 warningf(&declaration->source_position,
4887 "variable '%Y' declared 'inline'\n", declaration->symbol);
4890 if (token.type == '=') {
4891 parse_init_declarator_rest(declaration);
4894 if (token.type != ',')
4898 add_anchor_token('=');
4899 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4900 rem_anchor_token('=');
4905 rem_anchor_token(';');
4906 rem_anchor_token(',');
4909 static declaration_t *finished_kr_declaration(declaration_t *declaration, bool is_definition)
4911 symbol_t *symbol = declaration->symbol;
4912 if (symbol == NULL) {
4913 errorf(HERE, "anonymous declaration not valid as function parameter");
4916 namespace_t namespc = (namespace_t) declaration->namespc;
4917 if (namespc != NAMESPACE_NORMAL) {
4918 return record_declaration(declaration, false);
4921 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4922 if (previous_declaration == NULL ||
4923 previous_declaration->parent_scope != scope) {
4924 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4929 if (is_definition) {
4930 errorf(HERE, "parameter %Y is initialised", declaration->symbol);
4933 if (previous_declaration->type == NULL) {
4934 previous_declaration->type = declaration->type;
4935 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4936 previous_declaration->storage_class = declaration->storage_class;
4937 previous_declaration->parent_scope = scope;
4938 return previous_declaration;
4940 return record_declaration(declaration, false);
4944 static void parse_declaration(parsed_declaration_func finished_declaration)
4946 declaration_specifiers_t specifiers;
4947 memset(&specifiers, 0, sizeof(specifiers));
4949 add_anchor_token(';');
4950 parse_declaration_specifiers(&specifiers);
4951 rem_anchor_token(';');
4953 if (token.type == ';') {
4954 parse_anonymous_declaration_rest(&specifiers);
4956 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4957 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4961 static type_t *get_default_promoted_type(type_t *orig_type)
4963 type_t *result = orig_type;
4965 type_t *type = skip_typeref(orig_type);
4966 if (is_type_integer(type)) {
4967 result = promote_integer(type);
4968 } else if (type == type_float) {
4969 result = type_double;
4975 static void parse_kr_declaration_list(declaration_t *declaration)
4977 type_t *type = skip_typeref(declaration->type);
4978 if (!is_type_function(type))
4981 if (!type->function.kr_style_parameters)
4984 add_anchor_token('{');
4986 /* push function parameters */
4987 size_t const top = environment_top();
4988 scope_push(&declaration->scope);
4990 declaration_t *parameter = declaration->scope.declarations;
4991 for ( ; parameter != NULL; parameter = parameter->next) {
4992 assert(parameter->parent_scope == NULL);
4993 parameter->parent_scope = scope;
4994 environment_push(parameter);
4997 /* parse declaration list */
4998 while (is_declaration_specifier(&token, false)) {
4999 parse_declaration(finished_kr_declaration);
5002 /* pop function parameters */
5003 assert(scope == &declaration->scope);
5005 environment_pop_to(top);
5007 /* update function type */
5008 type_t *new_type = duplicate_type(type);
5010 function_parameter_t *parameters = NULL;
5011 function_parameter_t *last_parameter = NULL;
5013 declaration_t *parameter_declaration = declaration->scope.declarations;
5014 for( ; parameter_declaration != NULL;
5015 parameter_declaration = parameter_declaration->next) {
5016 type_t *parameter_type = parameter_declaration->type;
5017 if (parameter_type == NULL) {
5019 errorf(HERE, "no type specified for function parameter '%Y'",
5020 parameter_declaration->symbol);
5022 if (warning.implicit_int) {
5023 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
5024 parameter_declaration->symbol);
5026 parameter_type = type_int;
5027 parameter_declaration->type = parameter_type;
5031 semantic_parameter(parameter_declaration);
5032 parameter_type = parameter_declaration->type;
5035 * we need the default promoted types for the function type
5037 parameter_type = get_default_promoted_type(parameter_type);
5039 function_parameter_t *function_parameter
5040 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
5041 memset(function_parameter, 0, sizeof(function_parameter[0]));
5043 function_parameter->type = parameter_type;
5044 if (last_parameter != NULL) {
5045 last_parameter->next = function_parameter;
5047 parameters = function_parameter;
5049 last_parameter = function_parameter;
5052 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
5054 new_type->function.parameters = parameters;
5055 new_type->function.unspecified_parameters = true;
5057 type = typehash_insert(new_type);
5058 if (type != new_type) {
5059 obstack_free(type_obst, new_type);
5062 declaration->type = type;
5064 rem_anchor_token('{');
5067 static bool first_err = true;
5070 * When called with first_err set, prints the name of the current function,
5073 static void print_in_function(void)
5077 diagnosticf("%s: In function '%Y':\n",
5078 current_function->source_position.input_name,
5079 current_function->symbol);
5084 * Check if all labels are defined in the current function.
5085 * Check if all labels are used in the current function.
5087 static void check_labels(void)
5089 for (const goto_statement_t *goto_statement = goto_first;
5090 goto_statement != NULL;
5091 goto_statement = goto_statement->next) {
5092 /* skip computed gotos */
5093 if (goto_statement->expression != NULL)
5096 declaration_t *label = goto_statement->label;
5099 if (label->source_position.input_name == NULL) {
5100 print_in_function();
5101 errorf(&goto_statement->base.source_position,
5102 "label '%Y' used but not defined", label->symbol);
5105 goto_first = goto_last = NULL;
5107 if (warning.unused_label) {
5108 for (const label_statement_t *label_statement = label_first;
5109 label_statement != NULL;
5110 label_statement = label_statement->next) {
5111 const declaration_t *label = label_statement->label;
5113 if (! label->used) {
5114 print_in_function();
5115 warningf(&label_statement->base.source_position,
5116 "label '%Y' defined but not used", label->symbol);
5120 label_first = label_last = NULL;
5123 static void warn_unused_decl(declaration_t *decl, declaration_t *end, char const *const what)
5125 for (; decl != NULL; decl = decl->next) {
5130 print_in_function();
5131 warningf(&decl->source_position, "%s '%Y' is unused", what, decl->symbol);
5132 } else if (!decl->read) {
5133 print_in_function();
5134 warningf(&decl->source_position, "%s '%Y' is never read", what, decl->symbol);
5142 static void check_unused_variables(statement_t *const stmt, void *const env)
5146 switch (stmt->kind) {
5147 case STATEMENT_DECLARATION: {
5148 declaration_statement_t const *const decls = &stmt->declaration;
5149 warn_unused_decl(decls->declarations_begin, decls->declarations_end, "variable");
5154 warn_unused_decl(stmt->fors.scope.declarations, NULL, "variable");
5163 * Check declarations of current_function for unused entities.
5165 static void check_declarations(void)
5167 if (warning.unused_parameter) {
5168 const scope_t *scope = ¤t_function->scope;
5170 /* do not issue unused warnings for main */
5171 if (!is_sym_main(current_function->symbol)) {
5172 warn_unused_decl(scope->declarations, NULL, "parameter");
5175 if (warning.unused_variable) {
5176 walk_statements(current_function->init.statement, check_unused_variables, NULL);
5180 static int determine_truth(expression_t const* const cond)
5183 !is_constant_expression(cond) ? 0 :
5184 fold_constant(cond) != 0 ? 1 :
5188 static bool noreturn_candidate;
5190 static void check_reachable(statement_t *const stmt)
5192 if (stmt->base.reachable)
5194 if (stmt->kind != STATEMENT_DO_WHILE)
5195 stmt->base.reachable = true;
5197 statement_t *last = stmt;
5199 switch (stmt->kind) {
5200 case STATEMENT_INVALID:
5201 case STATEMENT_EMPTY:
5202 case STATEMENT_DECLARATION:
5204 next = stmt->base.next;
5207 case STATEMENT_COMPOUND:
5208 next = stmt->compound.statements;
5211 case STATEMENT_RETURN:
5212 noreturn_candidate = false;
5215 case STATEMENT_IF: {
5216 if_statement_t const* const ifs = &stmt->ifs;
5217 int const val = determine_truth(ifs->condition);
5220 check_reachable(ifs->true_statement);
5225 if (ifs->false_statement != NULL) {
5226 check_reachable(ifs->false_statement);
5230 next = stmt->base.next;
5234 case STATEMENT_SWITCH: {
5235 switch_statement_t const *const switchs = &stmt->switchs;
5236 expression_t const *const expr = switchs->expression;
5238 if (is_constant_expression(expr)) {
5239 long const val = fold_constant(expr);
5240 case_label_statement_t * defaults = NULL;
5241 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5242 if (i->expression == NULL) {
5247 if (i->first_case <= val && val <= i->last_case) {
5248 check_reachable((statement_t*)i);
5253 if (defaults != NULL) {
5254 check_reachable((statement_t*)defaults);
5258 bool has_default = false;
5259 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5260 if (i->expression == NULL)
5263 check_reachable((statement_t*)i);
5270 next = stmt->base.next;
5274 case STATEMENT_EXPRESSION: {
5275 /* Check for noreturn function call */
5276 expression_t const *const expr = stmt->expression.expression;
5277 if (expr->kind == EXPR_CALL) {
5278 expression_t const *const func = expr->call.function;
5279 if (func->kind == EXPR_REFERENCE) {
5280 declaration_t const *const decl = func->reference.declaration;
5281 if (decl != NULL && decl->modifiers & DM_NORETURN) {
5287 next = stmt->base.next;
5291 case STATEMENT_CONTINUE: {
5292 statement_t *parent = stmt;
5294 parent = parent->base.parent;
5295 if (parent == NULL) /* continue not within loop */
5299 switch (parent->kind) {
5300 case STATEMENT_WHILE: goto continue_while;
5301 case STATEMENT_DO_WHILE: goto continue_do_while;
5302 case STATEMENT_FOR: goto continue_for;
5309 case STATEMENT_BREAK: {
5310 statement_t *parent = stmt;
5312 parent = parent->base.parent;
5313 if (parent == NULL) /* break not within loop/switch */
5316 switch (parent->kind) {
5317 case STATEMENT_SWITCH:
5318 case STATEMENT_WHILE:
5319 case STATEMENT_DO_WHILE:
5322 next = parent->base.next;
5323 goto found_break_parent;
5332 case STATEMENT_GOTO:
5333 if (stmt->gotos.expression) {
5334 statement_t *parent = stmt->base.parent;
5335 if (parent == NULL) /* top level goto */
5339 next = stmt->gotos.label->init.statement;
5340 if (next == NULL) /* missing label */
5345 case STATEMENT_LABEL:
5346 next = stmt->label.statement;
5349 case STATEMENT_CASE_LABEL:
5350 next = stmt->case_label.statement;
5353 case STATEMENT_WHILE: {
5354 while_statement_t const *const whiles = &stmt->whiles;
5355 int const val = determine_truth(whiles->condition);
5358 check_reachable(whiles->body);
5363 next = stmt->base.next;
5367 case STATEMENT_DO_WHILE:
5368 next = stmt->do_while.body;
5371 case STATEMENT_FOR: {
5372 for_statement_t *const fors = &stmt->fors;
5374 if (fors->condition_reachable)
5376 fors->condition_reachable = true;
5378 expression_t const *const cond = fors->condition;
5380 cond == NULL ? 1 : determine_truth(cond);
5383 check_reachable(fors->body);
5388 next = stmt->base.next;
5392 case STATEMENT_MS_TRY: {
5393 ms_try_statement_t const *const ms_try = &stmt->ms_try;
5394 check_reachable(ms_try->try_statement);
5395 next = ms_try->final_statement;
5399 case STATEMENT_LEAVE: {
5400 statement_t *parent = stmt;
5402 parent = parent->base.parent;
5403 if (parent == NULL) /* __leave not within __try */
5406 if (parent->kind == STATEMENT_MS_TRY) {
5408 next = parent->ms_try.final_statement;
5416 while (next == NULL) {
5417 next = last->base.parent;
5419 noreturn_candidate = false;
5421 type_t *const type = current_function->type;
5422 assert(is_type_function(type));
5423 type_t *const ret = skip_typeref(type->function.return_type);
5424 if (warning.return_type &&
5425 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
5426 is_type_valid(ret) &&
5427 !is_sym_main(current_function->symbol)) {
5428 warningf(&stmt->base.source_position,
5429 "control reaches end of non-void function");
5434 switch (next->kind) {
5435 case STATEMENT_INVALID:
5436 case STATEMENT_EMPTY:
5437 case STATEMENT_DECLARATION:
5438 case STATEMENT_EXPRESSION:
5440 case STATEMENT_RETURN:
5441 case STATEMENT_CONTINUE:
5442 case STATEMENT_BREAK:
5443 case STATEMENT_GOTO:
5444 case STATEMENT_LEAVE:
5445 panic("invalid control flow in function");
5447 case STATEMENT_COMPOUND:
5449 case STATEMENT_SWITCH:
5450 case STATEMENT_LABEL:
5451 case STATEMENT_CASE_LABEL:
5453 next = next->base.next;
5456 case STATEMENT_WHILE: {
5458 if (next->base.reachable)
5460 next->base.reachable = true;
5462 while_statement_t const *const whiles = &next->whiles;
5463 int const val = determine_truth(whiles->condition);
5466 check_reachable(whiles->body);
5472 next = next->base.next;
5476 case STATEMENT_DO_WHILE: {
5478 if (next->base.reachable)
5480 next->base.reachable = true;
5482 do_while_statement_t const *const dw = &next->do_while;
5483 int const val = determine_truth(dw->condition);
5486 check_reachable(dw->body);
5492 next = next->base.next;
5496 case STATEMENT_FOR: {
5498 for_statement_t *const fors = &next->fors;
5500 fors->step_reachable = true;
5502 if (fors->condition_reachable)
5504 fors->condition_reachable = true;
5506 expression_t const *const cond = fors->condition;
5508 cond == NULL ? 1 : determine_truth(cond);
5511 check_reachable(fors->body);
5517 next = next->base.next;
5521 case STATEMENT_MS_TRY:
5523 next = next->ms_try.final_statement;
5529 next = stmt->base.parent;
5531 warningf(&stmt->base.source_position,
5532 "control reaches end of non-void function");
5536 check_reachable(next);
5539 static void check_unreachable(statement_t* const stmt, void *const env)
5543 switch (stmt->kind) {
5544 case STATEMENT_DO_WHILE:
5545 if (!stmt->base.reachable) {
5546 expression_t const *const cond = stmt->do_while.condition;
5547 if (determine_truth(cond) >= 0) {
5548 warningf(&cond->base.source_position,
5549 "condition of do-while-loop is unreachable");
5554 case STATEMENT_FOR: {
5555 for_statement_t const* const fors = &stmt->fors;
5557 // if init and step are unreachable, cond is unreachable, too
5558 if (!stmt->base.reachable && !fors->step_reachable) {
5559 warningf(&stmt->base.source_position, "statement is unreachable");
5561 if (!stmt->base.reachable && fors->initialisation != NULL) {
5562 warningf(&fors->initialisation->base.source_position,
5563 "initialisation of for-statement is unreachable");
5566 if (!fors->condition_reachable && fors->condition != NULL) {
5567 warningf(&fors->condition->base.source_position,
5568 "condition of for-statement is unreachable");
5571 if (!fors->step_reachable && fors->step != NULL) {
5572 warningf(&fors->step->base.source_position,
5573 "step of for-statement is unreachable");
5579 case STATEMENT_COMPOUND:
5580 if (stmt->compound.statements != NULL)
5585 if (!stmt->base.reachable)
5586 warningf(&stmt->base.source_position, "statement is unreachable");
5591 static void parse_external_declaration(void)
5593 /* function-definitions and declarations both start with declaration
5595 declaration_specifiers_t specifiers;
5596 memset(&specifiers, 0, sizeof(specifiers));
5598 add_anchor_token(';');
5599 parse_declaration_specifiers(&specifiers);
5600 rem_anchor_token(';');
5602 /* must be a declaration */
5603 if (token.type == ';') {
5604 parse_anonymous_declaration_rest(&specifiers);
5608 add_anchor_token(',');
5609 add_anchor_token('=');
5610 add_anchor_token(';');
5611 add_anchor_token('{');
5613 /* declarator is common to both function-definitions and declarations */
5614 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5616 rem_anchor_token('{');
5617 rem_anchor_token(';');
5618 rem_anchor_token('=');
5619 rem_anchor_token(',');
5621 /* must be a declaration */
5622 switch (token.type) {
5626 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5630 /* must be a function definition */
5631 parse_kr_declaration_list(ndeclaration);
5633 if (token.type != '{') {
5634 parse_error_expected("while parsing function definition", '{', NULL);
5635 eat_until_matching_token(';');
5639 type_t *type = ndeclaration->type;
5641 /* note that we don't skip typerefs: the standard doesn't allow them here
5642 * (so we can't use is_type_function here) */
5643 if (type->kind != TYPE_FUNCTION) {
5644 if (is_type_valid(type)) {
5645 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5646 type, ndeclaration->symbol);
5652 if (warning.aggregate_return &&
5653 is_type_compound(skip_typeref(type->function.return_type))) {
5654 warningf(HERE, "function '%Y' returns an aggregate",
5655 ndeclaration->symbol);
5657 if (warning.traditional && !type->function.unspecified_parameters) {
5658 warningf(HERE, "traditional C rejects ISO C style function definition of function '%Y'",
5659 ndeclaration->symbol);
5661 if (warning.old_style_definition && type->function.unspecified_parameters) {
5662 warningf(HERE, "old-style function definition '%Y'",
5663 ndeclaration->symbol);
5666 /* § 6.7.5.3 (14) a function definition with () means no
5667 * parameters (and not unspecified parameters) */
5668 if (type->function.unspecified_parameters
5669 && type->function.parameters == NULL
5670 && !type->function.kr_style_parameters) {
5671 type_t *duplicate = duplicate_type(type);
5672 duplicate->function.unspecified_parameters = false;
5674 type = typehash_insert(duplicate);
5675 if (type != duplicate) {
5676 obstack_free(type_obst, duplicate);
5678 ndeclaration->type = type;
5681 declaration_t *const declaration = record_declaration(ndeclaration, true);
5682 if (ndeclaration != declaration) {
5683 declaration->scope = ndeclaration->scope;
5685 type = skip_typeref(declaration->type);
5687 /* push function parameters and switch scope */
5688 size_t const top = environment_top();
5689 scope_push(&declaration->scope);
5691 declaration_t *parameter = declaration->scope.declarations;
5692 for( ; parameter != NULL; parameter = parameter->next) {
5693 if (parameter->parent_scope == &ndeclaration->scope) {
5694 parameter->parent_scope = scope;
5696 assert(parameter->parent_scope == NULL
5697 || parameter->parent_scope == scope);
5698 parameter->parent_scope = scope;
5699 if (parameter->symbol == NULL) {
5700 errorf(¶meter->source_position, "parameter name omitted");
5703 environment_push(parameter);
5706 if (declaration->init.statement != NULL) {
5707 parser_error_multiple_definition(declaration, HERE);
5710 /* parse function body */
5711 int label_stack_top = label_top();
5712 declaration_t *old_current_function = current_function;
5713 current_function = declaration;
5714 current_parent = NULL;
5716 statement_t *const body = parse_compound_statement(false);
5717 declaration->init.statement = body;
5720 check_declarations();
5721 if (warning.return_type ||
5722 warning.unreachable_code ||
5723 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5724 noreturn_candidate = true;
5725 check_reachable(body);
5726 if (warning.unreachable_code)
5727 walk_statements(body, check_unreachable, NULL);
5728 if (warning.missing_noreturn &&
5729 noreturn_candidate &&
5730 !(declaration->modifiers & DM_NORETURN)) {
5731 warningf(&body->base.source_position,
5732 "function '%#T' is candidate for attribute 'noreturn'",
5733 type, declaration->symbol);
5737 assert(current_parent == NULL);
5738 assert(current_function == declaration);
5739 current_function = old_current_function;
5740 label_pop_to(label_stack_top);
5743 assert(scope == &declaration->scope);
5745 environment_pop_to(top);
5748 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5749 source_position_t *source_position,
5750 const symbol_t *symbol)
5752 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
5754 type->bitfield.base_type = base_type;
5755 type->bitfield.size_expression = size;
5758 type_t *skipped_type = skip_typeref(base_type);
5759 if (!is_type_integer(skipped_type)) {
5760 errorf(HERE, "bitfield base type '%T' is not an integer type",
5764 bit_size = skipped_type->base.size * 8;
5767 if (is_constant_expression(size)) {
5768 long v = fold_constant(size);
5771 errorf(source_position, "negative width in bit-field '%Y'",
5773 } else if (v == 0) {
5774 errorf(source_position, "zero width for bit-field '%Y'",
5776 } else if (bit_size > 0 && (il_size_t)v > bit_size) {
5777 errorf(source_position, "width of '%Y' exceeds its type",
5780 type->bitfield.bit_size = v;
5787 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5790 declaration_t *iter = compound_declaration->scope.declarations;
5791 for( ; iter != NULL; iter = iter->next) {
5792 if (iter->namespc != NAMESPACE_NORMAL)
5795 if (iter->symbol == NULL) {
5796 type_t *type = skip_typeref(iter->type);
5797 if (is_type_compound(type)) {
5798 declaration_t *result
5799 = find_compound_entry(type->compound.declaration, symbol);
5806 if (iter->symbol == symbol) {
5814 static void parse_compound_declarators(declaration_t *struct_declaration,
5815 const declaration_specifiers_t *specifiers)
5817 declaration_t *last_declaration = struct_declaration->scope.declarations;
5818 if (last_declaration != NULL) {
5819 while (last_declaration->next != NULL) {
5820 last_declaration = last_declaration->next;
5825 declaration_t *declaration;
5827 if (token.type == ':') {
5828 source_position_t source_position = *HERE;
5831 type_t *base_type = specifiers->type;
5832 expression_t *size = parse_constant_expression();
5834 type_t *type = make_bitfield_type(base_type, size,
5835 &source_position, sym_anonymous);
5837 declaration = allocate_declaration_zero();
5838 declaration->namespc = NAMESPACE_NORMAL;
5839 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5840 declaration->storage_class = STORAGE_CLASS_NONE;
5841 declaration->source_position = source_position;
5842 declaration->modifiers = specifiers->modifiers;
5843 declaration->type = type;
5845 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5847 type_t *orig_type = declaration->type;
5848 type_t *type = skip_typeref(orig_type);
5850 if (token.type == ':') {
5851 source_position_t source_position = *HERE;
5853 expression_t *size = parse_constant_expression();
5855 type_t *bitfield_type = make_bitfield_type(orig_type, size,
5856 &source_position, declaration->symbol);
5857 declaration->type = bitfield_type;
5859 /* TODO we ignore arrays for now... what is missing is a check
5860 * that they're at the end of the struct */
5861 if (is_type_incomplete(type) && !is_type_array(type)) {
5863 "compound member '%Y' has incomplete type '%T'",
5864 declaration->symbol, orig_type);
5865 } else if (is_type_function(type)) {
5866 errorf(HERE, "compound member '%Y' must not have function type '%T'",
5867 declaration->symbol, orig_type);
5872 /* make sure we don't define a symbol multiple times */
5873 symbol_t *symbol = declaration->symbol;
5874 if (symbol != NULL) {
5875 declaration_t *prev_decl
5876 = find_compound_entry(struct_declaration, symbol);
5878 if (prev_decl != NULL) {
5879 assert(prev_decl->symbol == symbol);
5880 errorf(&declaration->source_position,
5881 "multiple declarations of symbol '%Y' (declared %P)",
5882 symbol, &prev_decl->source_position);
5886 /* append declaration */
5887 if (last_declaration != NULL) {
5888 last_declaration->next = declaration;
5890 struct_declaration->scope.declarations = declaration;
5892 last_declaration = declaration;
5894 if (token.type != ',')
5904 static void parse_compound_type_entries(declaration_t *compound_declaration)
5907 add_anchor_token('}');
5909 while (token.type != '}') {
5910 if (token.type == T_EOF) {
5911 errorf(HERE, "EOF while parsing struct");
5914 declaration_specifiers_t specifiers;
5915 memset(&specifiers, 0, sizeof(specifiers));
5916 parse_declaration_specifiers(&specifiers);
5918 parse_compound_declarators(compound_declaration, &specifiers);
5920 rem_anchor_token('}');
5924 static type_t *parse_typename(void)
5926 declaration_specifiers_t specifiers;
5927 memset(&specifiers, 0, sizeof(specifiers));
5928 parse_declaration_specifiers(&specifiers);
5929 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5930 /* TODO: improve error message, user does probably not know what a
5931 * storage class is...
5933 errorf(HERE, "typename may not have a storage class");
5936 type_t *result = parse_abstract_declarator(specifiers.type);
5944 typedef expression_t* (*parse_expression_function)(void);
5945 typedef expression_t* (*parse_expression_infix_function)(expression_t *left);
5947 typedef struct expression_parser_function_t expression_parser_function_t;
5948 struct expression_parser_function_t {
5949 parse_expression_function parser;
5950 unsigned infix_precedence;
5951 parse_expression_infix_function infix_parser;
5954 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5957 * Prints an error message if an expression was expected but not read
5959 static expression_t *expected_expression_error(void)
5961 /* skip the error message if the error token was read */
5962 if (token.type != T_ERROR) {
5963 errorf(HERE, "expected expression, got token '%K'", &token);
5967 return create_invalid_expression();
5971 * Parse a string constant.
5973 static expression_t *parse_string_const(void)
5976 if (token.type == T_STRING_LITERAL) {
5977 string_t res = token.v.string;
5979 while (token.type == T_STRING_LITERAL) {
5980 res = concat_strings(&res, &token.v.string);
5983 if (token.type != T_WIDE_STRING_LITERAL) {
5984 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
5985 /* note: that we use type_char_ptr here, which is already the
5986 * automatic converted type. revert_automatic_type_conversion
5987 * will construct the array type */
5988 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
5989 cnst->string.value = res;
5993 wres = concat_string_wide_string(&res, &token.v.wide_string);
5995 wres = token.v.wide_string;
6000 switch (token.type) {
6001 case T_WIDE_STRING_LITERAL:
6002 wres = concat_wide_strings(&wres, &token.v.wide_string);
6005 case T_STRING_LITERAL:
6006 wres = concat_wide_string_string(&wres, &token.v.string);
6010 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
6011 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
6012 cnst->wide_string.value = wres;
6021 * Parse an integer constant.
6023 static expression_t *parse_int_const(void)
6025 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6026 cnst->base.source_position = *HERE;
6027 cnst->base.type = token.datatype;
6028 cnst->conste.v.int_value = token.v.intvalue;
6036 * Parse a character constant.
6038 static expression_t *parse_character_constant(void)
6040 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
6042 cnst->base.source_position = *HERE;
6043 cnst->base.type = token.datatype;
6044 cnst->conste.v.character = token.v.string;
6046 if (cnst->conste.v.character.size != 1) {
6047 if (warning.multichar && GNU_MODE) {
6048 warningf(HERE, "multi-character character constant");
6050 errorf(HERE, "more than 1 characters in character constant");
6059 * Parse a wide character constant.
6061 static expression_t *parse_wide_character_constant(void)
6063 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
6065 cnst->base.source_position = *HERE;
6066 cnst->base.type = token.datatype;
6067 cnst->conste.v.wide_character = token.v.wide_string;
6069 if (cnst->conste.v.wide_character.size != 1) {
6070 if (warning.multichar && GNU_MODE) {
6071 warningf(HERE, "multi-character character constant");
6073 errorf(HERE, "more than 1 characters in character constant");
6082 * Parse a float constant.
6084 static expression_t *parse_float_const(void)
6086 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6087 cnst->base.type = token.datatype;
6088 cnst->conste.v.float_value = token.v.floatvalue;
6095 static declaration_t *create_implicit_function(symbol_t *symbol,
6096 const source_position_t *source_position)
6098 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
6099 ntype->function.return_type = type_int;
6100 ntype->function.unspecified_parameters = true;
6102 type_t *type = typehash_insert(ntype);
6103 if (type != ntype) {
6107 declaration_t *const declaration = allocate_declaration_zero();
6108 declaration->storage_class = STORAGE_CLASS_EXTERN;
6109 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
6110 declaration->type = type;
6111 declaration->symbol = symbol;
6112 declaration->source_position = *source_position;
6113 declaration->implicit = true;
6115 bool strict_prototypes_old = warning.strict_prototypes;
6116 warning.strict_prototypes = false;
6117 record_declaration(declaration, false);
6118 warning.strict_prototypes = strict_prototypes_old;
6124 * Creates a return_type (func)(argument_type) function type if not
6127 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
6128 type_t *argument_type2)
6130 function_parameter_t *parameter2
6131 = obstack_alloc(type_obst, sizeof(parameter2[0]));
6132 memset(parameter2, 0, sizeof(parameter2[0]));
6133 parameter2->type = argument_type2;
6135 function_parameter_t *parameter1
6136 = obstack_alloc(type_obst, sizeof(parameter1[0]));
6137 memset(parameter1, 0, sizeof(parameter1[0]));
6138 parameter1->type = argument_type1;
6139 parameter1->next = parameter2;
6141 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6142 type->function.return_type = return_type;
6143 type->function.parameters = parameter1;
6145 type_t *result = typehash_insert(type);
6146 if (result != type) {
6154 * Creates a return_type (func)(argument_type) function type if not
6157 * @param return_type the return type
6158 * @param argument_type the argument type
6160 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
6162 function_parameter_t *parameter
6163 = obstack_alloc(type_obst, sizeof(parameter[0]));
6164 memset(parameter, 0, sizeof(parameter[0]));
6165 parameter->type = argument_type;
6167 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6168 type->function.return_type = return_type;
6169 type->function.parameters = parameter;
6171 type_t *result = typehash_insert(type);
6172 if (result != type) {
6179 static type_t *make_function_0_type(type_t *return_type)
6181 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6182 type->function.return_type = return_type;
6183 type->function.parameters = NULL;
6185 type_t *result = typehash_insert(type);
6186 if (result != type) {
6194 * Creates a function type for some function like builtins.
6196 * @param symbol the symbol describing the builtin
6198 static type_t *get_builtin_symbol_type(symbol_t *symbol)
6200 switch(symbol->ID) {
6201 case T___builtin_alloca:
6202 return make_function_1_type(type_void_ptr, type_size_t);
6203 case T___builtin_huge_val:
6204 return make_function_0_type(type_double);
6205 case T___builtin_inf:
6206 return make_function_0_type(type_double);
6207 case T___builtin_inff:
6208 return make_function_0_type(type_float);
6209 case T___builtin_infl:
6210 return make_function_0_type(type_long_double);
6211 case T___builtin_nan:
6212 return make_function_1_type(type_double, type_char_ptr);
6213 case T___builtin_nanf:
6214 return make_function_1_type(type_float, type_char_ptr);
6215 case T___builtin_nanl:
6216 return make_function_1_type(type_long_double, type_char_ptr);
6217 case T___builtin_va_end:
6218 return make_function_1_type(type_void, type_valist);
6219 case T___builtin_expect:
6220 return make_function_2_type(type_long, type_long, type_long);
6222 internal_errorf(HERE, "not implemented builtin symbol found");
6227 * Performs automatic type cast as described in § 6.3.2.1.
6229 * @param orig_type the original type
6231 static type_t *automatic_type_conversion(type_t *orig_type)
6233 type_t *type = skip_typeref(orig_type);
6234 if (is_type_array(type)) {
6235 array_type_t *array_type = &type->array;
6236 type_t *element_type = array_type->element_type;
6237 unsigned qualifiers = array_type->base.qualifiers;
6239 return make_pointer_type(element_type, qualifiers);
6242 if (is_type_function(type)) {
6243 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6250 * reverts the automatic casts of array to pointer types and function
6251 * to function-pointer types as defined § 6.3.2.1
6253 type_t *revert_automatic_type_conversion(const expression_t *expression)
6255 switch (expression->kind) {
6256 case EXPR_REFERENCE: return expression->reference.declaration->type;
6259 return get_qualified_type(expression->select.compound_entry->type,
6260 expression->base.type->base.qualifiers);
6262 case EXPR_UNARY_DEREFERENCE: {
6263 const expression_t *const value = expression->unary.value;
6264 type_t *const type = skip_typeref(value->base.type);
6265 assert(is_type_pointer(type));
6266 return type->pointer.points_to;
6269 case EXPR_BUILTIN_SYMBOL:
6270 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
6272 case EXPR_ARRAY_ACCESS: {
6273 const expression_t *array_ref = expression->array_access.array_ref;
6274 type_t *type_left = skip_typeref(array_ref->base.type);
6275 if (!is_type_valid(type_left))
6277 assert(is_type_pointer(type_left));
6278 return type_left->pointer.points_to;
6281 case EXPR_STRING_LITERAL: {
6282 size_t size = expression->string.value.size;
6283 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
6286 case EXPR_WIDE_STRING_LITERAL: {
6287 size_t size = expression->wide_string.value.size;
6288 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
6291 case EXPR_COMPOUND_LITERAL:
6292 return expression->compound_literal.type;
6297 return expression->base.type;
6300 static expression_t *parse_reference(void)
6302 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
6304 reference_expression_t *ref = &expression->reference;
6305 symbol_t *const symbol = token.v.symbol;
6307 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
6309 if (declaration == NULL) {
6310 if (!strict_mode && look_ahead(1)->type == '(') {
6311 /* an implicitly declared function */
6312 if (warning.implicit_function_declaration) {
6313 warningf(HERE, "implicit declaration of function '%Y'",
6317 declaration = create_implicit_function(symbol, HERE);
6319 errorf(HERE, "unknown symbol '%Y' found.", symbol);
6320 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
6324 type_t *orig_type = declaration->type;
6326 /* we always do the auto-type conversions; the & and sizeof parser contains
6327 * code to revert this! */
6328 type_t *type = automatic_type_conversion(orig_type);
6330 ref->declaration = declaration;
6331 ref->base.type = type;
6333 /* this declaration is used */
6334 declaration->used = true;
6336 if (declaration->parent_scope != file_scope &&
6337 declaration->parent_scope->depth < current_function->scope.depth &&
6338 is_type_valid(orig_type) && !is_type_function(orig_type)) {
6339 /* access of a variable from an outer function */
6340 declaration->address_taken = true;
6341 current_function->need_closure = true;
6344 /* check for deprecated functions */
6345 if (warning.deprecated_declarations &&
6346 declaration->modifiers & DM_DEPRECATED) {
6347 char const *const prefix = is_type_function(declaration->type) ?
6348 "function" : "variable";
6350 if (declaration->deprecated_string != NULL) {
6351 warningf(HERE, "%s '%Y' is deprecated (declared %P): \"%s\"",
6352 prefix, declaration->symbol, &declaration->source_position,
6353 declaration->deprecated_string);
6355 warningf(HERE, "%s '%Y' is deprecated (declared %P)", prefix,
6356 declaration->symbol, &declaration->source_position);
6359 if (warning.init_self && declaration == current_init_decl && !in_type_prop) {
6360 current_init_decl = NULL;
6361 warningf(HERE, "variable '%#T' is initialized by itself",
6362 declaration->type, declaration->symbol);
6369 static bool semantic_cast(expression_t *cast)
6371 expression_t *expression = cast->unary.value;
6372 type_t *orig_dest_type = cast->base.type;
6373 type_t *orig_type_right = expression->base.type;
6374 type_t const *dst_type = skip_typeref(orig_dest_type);
6375 type_t const *src_type = skip_typeref(orig_type_right);
6376 source_position_t const *pos = &cast->base.source_position;
6378 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
6379 if (dst_type == type_void)
6382 /* only integer and pointer can be casted to pointer */
6383 if (is_type_pointer(dst_type) &&
6384 !is_type_pointer(src_type) &&
6385 !is_type_integer(src_type) &&
6386 is_type_valid(src_type)) {
6387 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
6391 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
6392 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
6396 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
6397 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
6401 if (warning.cast_qual &&
6402 is_type_pointer(src_type) &&
6403 is_type_pointer(dst_type)) {
6404 type_t *src = skip_typeref(src_type->pointer.points_to);
6405 type_t *dst = skip_typeref(dst_type->pointer.points_to);
6406 unsigned missing_qualifiers =
6407 src->base.qualifiers & ~dst->base.qualifiers;
6408 if (missing_qualifiers != 0) {
6410 "cast discards qualifiers '%Q' in pointer target type of '%T'",
6411 missing_qualifiers, orig_type_right);
6417 static expression_t *parse_compound_literal(type_t *type)
6419 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
6421 parse_initializer_env_t env;
6423 env.declaration = NULL;
6424 env.must_be_constant = false;
6425 initializer_t *initializer = parse_initializer(&env);
6428 expression->compound_literal.initializer = initializer;
6429 expression->compound_literal.type = type;
6430 expression->base.type = automatic_type_conversion(type);
6436 * Parse a cast expression.
6438 static expression_t *parse_cast(void)
6440 add_anchor_token(')');
6442 source_position_t source_position = token.source_position;
6444 type_t *type = parse_typename();
6446 rem_anchor_token(')');
6449 if (token.type == '{') {
6450 return parse_compound_literal(type);
6453 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6454 cast->base.source_position = source_position;
6456 expression_t *value = parse_sub_expression(PREC_CAST);
6457 cast->base.type = type;
6458 cast->unary.value = value;
6460 if (! semantic_cast(cast)) {
6461 /* TODO: record the error in the AST. else it is impossible to detect it */
6466 return create_invalid_expression();
6470 * Parse a statement expression.
6472 static expression_t *parse_statement_expression(void)
6474 add_anchor_token(')');
6476 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6478 statement_t *statement = parse_compound_statement(true);
6479 expression->statement.statement = statement;
6480 expression->base.source_position = statement->base.source_position;
6482 /* find last statement and use its type */
6483 type_t *type = type_void;
6484 const statement_t *stmt = statement->compound.statements;
6486 while (stmt->base.next != NULL)
6487 stmt = stmt->base.next;
6489 if (stmt->kind == STATEMENT_EXPRESSION) {
6490 type = stmt->expression.expression->base.type;
6493 warningf(&expression->base.source_position, "empty statement expression ({})");
6495 expression->base.type = type;
6497 rem_anchor_token(')');
6505 * Parse a parenthesized expression.
6507 static expression_t *parse_parenthesized_expression(void)
6511 switch(token.type) {
6513 /* gcc extension: a statement expression */
6514 return parse_statement_expression();
6518 return parse_cast();
6520 if (is_typedef_symbol(token.v.symbol)) {
6521 return parse_cast();
6525 add_anchor_token(')');
6526 expression_t *result = parse_expression();
6527 rem_anchor_token(')');
6534 static expression_t *parse_function_keyword(void)
6539 if (current_function == NULL) {
6540 errorf(HERE, "'__func__' used outside of a function");
6543 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6544 expression->base.type = type_char_ptr;
6545 expression->funcname.kind = FUNCNAME_FUNCTION;
6550 static expression_t *parse_pretty_function_keyword(void)
6552 eat(T___PRETTY_FUNCTION__);
6554 if (current_function == NULL) {
6555 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6558 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6559 expression->base.type = type_char_ptr;
6560 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6565 static expression_t *parse_funcsig_keyword(void)
6569 if (current_function == NULL) {
6570 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6573 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6574 expression->base.type = type_char_ptr;
6575 expression->funcname.kind = FUNCNAME_FUNCSIG;
6580 static expression_t *parse_funcdname_keyword(void)
6582 eat(T___FUNCDNAME__);
6584 if (current_function == NULL) {
6585 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6588 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6589 expression->base.type = type_char_ptr;
6590 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6595 static designator_t *parse_designator(void)
6597 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6598 result->source_position = *HERE;
6600 if (token.type != T_IDENTIFIER) {
6601 parse_error_expected("while parsing member designator",
6602 T_IDENTIFIER, NULL);
6605 result->symbol = token.v.symbol;
6608 designator_t *last_designator = result;
6610 if (token.type == '.') {
6612 if (token.type != T_IDENTIFIER) {
6613 parse_error_expected("while parsing member designator",
6614 T_IDENTIFIER, NULL);
6617 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6618 designator->source_position = *HERE;
6619 designator->symbol = token.v.symbol;
6622 last_designator->next = designator;
6623 last_designator = designator;
6626 if (token.type == '[') {
6628 add_anchor_token(']');
6629 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6630 designator->source_position = *HERE;
6631 designator->array_index = parse_expression();
6632 rem_anchor_token(']');
6634 if (designator->array_index == NULL) {
6638 last_designator->next = designator;
6639 last_designator = designator;
6651 * Parse the __builtin_offsetof() expression.
6653 static expression_t *parse_offsetof(void)
6655 eat(T___builtin_offsetof);
6657 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6658 expression->base.type = type_size_t;
6661 add_anchor_token(',');
6662 type_t *type = parse_typename();
6663 rem_anchor_token(',');
6665 add_anchor_token(')');
6666 designator_t *designator = parse_designator();
6667 rem_anchor_token(')');
6670 expression->offsetofe.type = type;
6671 expression->offsetofe.designator = designator;
6674 memset(&path, 0, sizeof(path));
6675 path.top_type = type;
6676 path.path = NEW_ARR_F(type_path_entry_t, 0);
6678 descend_into_subtype(&path);
6680 if (!walk_designator(&path, designator, true)) {
6681 return create_invalid_expression();
6684 DEL_ARR_F(path.path);
6688 return create_invalid_expression();
6692 * Parses a _builtin_va_start() expression.
6694 static expression_t *parse_va_start(void)
6696 eat(T___builtin_va_start);
6698 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6701 add_anchor_token(',');
6702 expression->va_starte.ap = parse_assignment_expression();
6703 rem_anchor_token(',');
6705 expression_t *const expr = parse_assignment_expression();
6706 if (expr->kind == EXPR_REFERENCE) {
6707 declaration_t *const decl = expr->reference.declaration;
6708 if (decl->parent_scope != ¤t_function->scope || decl->next != NULL) {
6709 errorf(&expr->base.source_position,
6710 "second argument of 'va_start' must be last parameter of the current function");
6712 expression->va_starte.parameter = decl;
6718 return create_invalid_expression();
6722 * Parses a _builtin_va_arg() expression.
6724 static expression_t *parse_va_arg(void)
6726 eat(T___builtin_va_arg);
6728 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6731 expression->va_arge.ap = parse_assignment_expression();
6733 expression->base.type = parse_typename();
6738 return create_invalid_expression();
6741 static expression_t *parse_builtin_symbol(void)
6743 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6745 symbol_t *symbol = token.v.symbol;
6747 expression->builtin_symbol.symbol = symbol;
6750 type_t *type = get_builtin_symbol_type(symbol);
6751 type = automatic_type_conversion(type);
6753 expression->base.type = type;
6758 * Parses a __builtin_constant() expression.
6760 static expression_t *parse_builtin_constant(void)
6762 eat(T___builtin_constant_p);
6764 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6767 add_anchor_token(')');
6768 expression->builtin_constant.value = parse_assignment_expression();
6769 rem_anchor_token(')');
6771 expression->base.type = type_int;
6775 return create_invalid_expression();
6779 * Parses a __builtin_prefetch() expression.
6781 static expression_t *parse_builtin_prefetch(void)
6783 eat(T___builtin_prefetch);
6785 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6788 add_anchor_token(')');
6789 expression->builtin_prefetch.adr = parse_assignment_expression();
6790 if (token.type == ',') {
6792 expression->builtin_prefetch.rw = parse_assignment_expression();
6794 if (token.type == ',') {
6796 expression->builtin_prefetch.locality = parse_assignment_expression();
6798 rem_anchor_token(')');
6800 expression->base.type = type_void;
6804 return create_invalid_expression();
6808 * Parses a __builtin_is_*() compare expression.
6810 static expression_t *parse_compare_builtin(void)
6812 expression_t *expression;
6814 switch(token.type) {
6815 case T___builtin_isgreater:
6816 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6818 case T___builtin_isgreaterequal:
6819 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6821 case T___builtin_isless:
6822 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6824 case T___builtin_islessequal:
6825 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6827 case T___builtin_islessgreater:
6828 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6830 case T___builtin_isunordered:
6831 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6834 internal_errorf(HERE, "invalid compare builtin found");
6836 expression->base.source_position = *HERE;
6840 expression->binary.left = parse_assignment_expression();
6842 expression->binary.right = parse_assignment_expression();
6845 type_t *const orig_type_left = expression->binary.left->base.type;
6846 type_t *const orig_type_right = expression->binary.right->base.type;
6848 type_t *const type_left = skip_typeref(orig_type_left);
6849 type_t *const type_right = skip_typeref(orig_type_right);
6850 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6851 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6852 type_error_incompatible("invalid operands in comparison",
6853 &expression->base.source_position, orig_type_left, orig_type_right);
6856 semantic_comparison(&expression->binary);
6861 return create_invalid_expression();
6866 * Parses a __builtin_expect() expression.
6868 static expression_t *parse_builtin_expect(void)
6870 eat(T___builtin_expect);
6872 expression_t *expression
6873 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6876 expression->binary.left = parse_assignment_expression();
6878 expression->binary.right = parse_constant_expression();
6881 expression->base.type = expression->binary.left->base.type;
6885 return create_invalid_expression();
6890 * Parses a MS assume() expression.
6892 static expression_t *parse_assume(void)
6896 expression_t *expression
6897 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6900 add_anchor_token(')');
6901 expression->unary.value = parse_assignment_expression();
6902 rem_anchor_token(')');
6905 expression->base.type = type_void;
6908 return create_invalid_expression();
6912 * Return the declaration for a given label symbol or create a new one.
6914 * @param symbol the symbol of the label
6916 static declaration_t *get_label(symbol_t *symbol)
6918 declaration_t *candidate;
6919 assert(current_function != NULL);
6921 candidate = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
6922 /* if we found a local label, we already created the declaration */
6923 if (candidate != NULL) {
6924 if (candidate->parent_scope != scope) {
6925 assert(candidate->parent_scope->depth < scope->depth);
6926 current_function->goto_to_outer = true;
6931 candidate = get_declaration(symbol, NAMESPACE_LABEL);
6932 /* if we found a label in the same function, then we already created the
6934 if (candidate != NULL
6935 && candidate->parent_scope == ¤t_function->scope) {
6939 /* otherwise we need to create a new one */
6940 declaration_t *const declaration = allocate_declaration_zero();
6941 declaration->namespc = NAMESPACE_LABEL;
6942 declaration->symbol = symbol;
6944 label_push(declaration);
6950 * Parses a GNU && label address expression.
6952 static expression_t *parse_label_address(void)
6954 source_position_t source_position = token.source_position;
6956 if (token.type != T_IDENTIFIER) {
6957 parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
6960 symbol_t *symbol = token.v.symbol;
6963 declaration_t *label = get_label(symbol);
6966 label->address_taken = true;
6968 expression_t *expression = allocate_expression_zero(EXPR_LABEL_ADDRESS);
6969 expression->base.source_position = source_position;
6971 /* label address is threaten as a void pointer */
6972 expression->base.type = type_void_ptr;
6973 expression->label_address.declaration = label;
6976 return create_invalid_expression();
6980 * Parse a microsoft __noop expression.
6982 static expression_t *parse_noop_expression(void)
6984 source_position_t source_position = *HERE;
6987 if (token.type == '(') {
6988 /* parse arguments */
6990 add_anchor_token(')');
6991 add_anchor_token(',');
6993 if (token.type != ')') {
6995 (void)parse_assignment_expression();
6996 if (token.type != ',')
7002 rem_anchor_token(',');
7003 rem_anchor_token(')');
7006 /* the result is a (int)0 */
7007 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
7008 cnst->base.source_position = source_position;
7009 cnst->base.type = type_int;
7010 cnst->conste.v.int_value = 0;
7011 cnst->conste.is_ms_noop = true;
7016 return create_invalid_expression();
7020 * Parses a primary expression.
7022 static expression_t *parse_primary_expression(void)
7024 switch (token.type) {
7025 case T_INTEGER: return parse_int_const();
7026 case T_CHARACTER_CONSTANT: return parse_character_constant();
7027 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
7028 case T_FLOATINGPOINT: return parse_float_const();
7029 case T_STRING_LITERAL:
7030 case T_WIDE_STRING_LITERAL: return parse_string_const();
7031 case T_IDENTIFIER: return parse_reference();
7032 case T___FUNCTION__:
7033 case T___func__: return parse_function_keyword();
7034 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
7035 case T___FUNCSIG__: return parse_funcsig_keyword();
7036 case T___FUNCDNAME__: return parse_funcdname_keyword();
7037 case T___builtin_offsetof: return parse_offsetof();
7038 case T___builtin_va_start: return parse_va_start();
7039 case T___builtin_va_arg: return parse_va_arg();
7040 case T___builtin_expect:
7041 case T___builtin_alloca:
7042 case T___builtin_inf:
7043 case T___builtin_inff:
7044 case T___builtin_infl:
7045 case T___builtin_nan:
7046 case T___builtin_nanf:
7047 case T___builtin_nanl:
7048 case T___builtin_huge_val:
7049 case T___builtin_va_end: return parse_builtin_symbol();
7050 case T___builtin_isgreater:
7051 case T___builtin_isgreaterequal:
7052 case T___builtin_isless:
7053 case T___builtin_islessequal:
7054 case T___builtin_islessgreater:
7055 case T___builtin_isunordered: return parse_compare_builtin();
7056 case T___builtin_constant_p: return parse_builtin_constant();
7057 case T___builtin_prefetch: return parse_builtin_prefetch();
7058 case T__assume: return parse_assume();
7061 return parse_label_address();
7064 case '(': return parse_parenthesized_expression();
7065 case T___noop: return parse_noop_expression();
7068 errorf(HERE, "unexpected token %K, expected an expression", &token);
7069 return create_invalid_expression();
7073 * Check if the expression has the character type and issue a warning then.
7075 static void check_for_char_index_type(const expression_t *expression)
7077 type_t *const type = expression->base.type;
7078 const type_t *const base_type = skip_typeref(type);
7080 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
7081 warning.char_subscripts) {
7082 warningf(&expression->base.source_position,
7083 "array subscript has type '%T'", type);
7087 static expression_t *parse_array_expression(expression_t *left)
7090 add_anchor_token(']');
7092 expression_t *inside = parse_expression();
7094 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
7096 array_access_expression_t *array_access = &expression->array_access;
7098 type_t *const orig_type_left = left->base.type;
7099 type_t *const orig_type_inside = inside->base.type;
7101 type_t *const type_left = skip_typeref(orig_type_left);
7102 type_t *const type_inside = skip_typeref(orig_type_inside);
7104 type_t *return_type;
7105 if (is_type_pointer(type_left)) {
7106 return_type = type_left->pointer.points_to;
7107 array_access->array_ref = left;
7108 array_access->index = inside;
7109 check_for_char_index_type(inside);
7110 } else if (is_type_pointer(type_inside)) {
7111 return_type = type_inside->pointer.points_to;
7112 array_access->array_ref = inside;
7113 array_access->index = left;
7114 array_access->flipped = true;
7115 check_for_char_index_type(left);
7117 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
7119 "array access on object with non-pointer types '%T', '%T'",
7120 orig_type_left, orig_type_inside);
7122 return_type = type_error_type;
7123 array_access->array_ref = left;
7124 array_access->index = inside;
7127 expression->base.type = automatic_type_conversion(return_type);
7129 rem_anchor_token(']');
7130 if (token.type == ']') {
7133 parse_error_expected("Problem while parsing array access", ']', NULL);
7138 static expression_t *parse_typeprop(expression_kind_t const kind,
7139 source_position_t const pos)
7141 expression_t *tp_expression = allocate_expression_zero(kind);
7142 tp_expression->base.type = type_size_t;
7143 tp_expression->base.source_position = pos;
7145 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
7147 /* we only refer to a type property, mark this case */
7148 bool old = in_type_prop;
7149 in_type_prop = true;
7150 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
7152 add_anchor_token(')');
7153 type_t* const orig_type = parse_typename();
7154 tp_expression->typeprop.type = orig_type;
7156 type_t const* const type = skip_typeref(orig_type);
7157 char const* const wrong_type =
7158 is_type_incomplete(type) ? "incomplete" :
7159 type->kind == TYPE_FUNCTION ? "function designator" :
7160 type->kind == TYPE_BITFIELD ? "bitfield" :
7162 if (wrong_type != NULL) {
7163 errorf(&pos, "operand of %s expression must not be %s type '%T'",
7164 what, wrong_type, type);
7167 rem_anchor_token(')');
7170 expression_t *expression = parse_sub_expression(PREC_UNARY);
7172 type_t* const orig_type = revert_automatic_type_conversion(expression);
7173 expression->base.type = orig_type;
7175 type_t const* const type = skip_typeref(orig_type);
7176 char const* const wrong_type =
7177 is_type_incomplete(type) ? "incomplete" :
7178 type->kind == TYPE_FUNCTION ? "function designator" :
7179 type->kind == TYPE_BITFIELD ? "bitfield" :
7181 if (wrong_type != NULL) {
7182 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
7185 tp_expression->typeprop.type = expression->base.type;
7186 tp_expression->typeprop.tp_expression = expression;
7191 return tp_expression;
7194 static expression_t *parse_sizeof(void)
7196 source_position_t pos = *HERE;
7198 return parse_typeprop(EXPR_SIZEOF, pos);
7201 static expression_t *parse_alignof(void)
7203 source_position_t pos = *HERE;
7205 return parse_typeprop(EXPR_ALIGNOF, pos);
7208 static expression_t *parse_select_expression(expression_t *compound)
7210 assert(token.type == '.' || token.type == T_MINUSGREATER);
7212 bool is_pointer = (token.type == T_MINUSGREATER);
7215 expression_t *select = allocate_expression_zero(EXPR_SELECT);
7216 select->select.compound = compound;
7218 if (token.type != T_IDENTIFIER) {
7219 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
7222 symbol_t *symbol = token.v.symbol;
7225 type_t *const orig_type = compound->base.type;
7226 type_t *const type = skip_typeref(orig_type);
7229 bool saw_error = false;
7230 if (is_type_pointer(type)) {
7233 "request for member '%Y' in something not a struct or union, but '%T'",
7237 type_left = skip_typeref(type->pointer.points_to);
7239 if (is_pointer && is_type_valid(type)) {
7240 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
7246 declaration_t *entry;
7247 if (type_left->kind == TYPE_COMPOUND_STRUCT ||
7248 type_left->kind == TYPE_COMPOUND_UNION) {
7249 declaration_t *const declaration = type_left->compound.declaration;
7251 if (!declaration->init.complete) {
7252 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
7254 goto create_error_entry;
7257 entry = find_compound_entry(declaration, symbol);
7258 if (entry == NULL) {
7259 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
7260 goto create_error_entry;
7263 if (is_type_valid(type_left) && !saw_error) {
7265 "request for member '%Y' in something not a struct or union, but '%T'",
7269 entry = allocate_declaration_zero();
7270 entry->symbol = symbol;
7273 select->select.compound_entry = entry;
7275 type_t *const res_type =
7276 get_qualified_type(entry->type, type_left->base.qualifiers);
7278 /* we always do the auto-type conversions; the & and sizeof parser contains
7279 * code to revert this! */
7280 select->base.type = automatic_type_conversion(res_type);
7282 type_t *skipped = skip_typeref(res_type);
7283 if (skipped->kind == TYPE_BITFIELD) {
7284 select->base.type = skipped->bitfield.base_type;
7290 static void check_call_argument(const function_parameter_t *parameter,
7291 call_argument_t *argument, unsigned pos)
7293 type_t *expected_type = parameter->type;
7294 type_t *expected_type_skip = skip_typeref(expected_type);
7295 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
7296 expression_t *arg_expr = argument->expression;
7297 type_t *arg_type = skip_typeref(arg_expr->base.type);
7299 /* handle transparent union gnu extension */
7300 if (is_type_union(expected_type_skip)
7301 && (expected_type_skip->base.modifiers
7302 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
7303 declaration_t *union_decl = expected_type_skip->compound.declaration;
7305 declaration_t *declaration = union_decl->scope.declarations;
7306 type_t *best_type = NULL;
7307 for ( ; declaration != NULL; declaration = declaration->next) {
7308 type_t *decl_type = declaration->type;
7309 error = semantic_assign(decl_type, arg_expr);
7310 if (error == ASSIGN_ERROR_INCOMPATIBLE
7311 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
7314 if (error == ASSIGN_SUCCESS) {
7315 best_type = decl_type;
7316 } else if (best_type == NULL) {
7317 best_type = decl_type;
7321 if (best_type != NULL) {
7322 expected_type = best_type;
7326 error = semantic_assign(expected_type, arg_expr);
7327 argument->expression = create_implicit_cast(argument->expression,
7330 if (error != ASSIGN_SUCCESS) {
7331 /* report exact scope in error messages (like "in argument 3") */
7333 snprintf(buf, sizeof(buf), "call argument %u", pos);
7334 report_assign_error(error, expected_type, arg_expr, buf,
7335 &arg_expr->base.source_position);
7336 } else if (warning.traditional || warning.conversion) {
7337 type_t *const promoted_type = get_default_promoted_type(arg_type);
7338 if (!types_compatible(expected_type_skip, promoted_type) &&
7339 !types_compatible(expected_type_skip, type_void_ptr) &&
7340 !types_compatible(type_void_ptr, promoted_type)) {
7341 /* Deliberately show the skipped types in this warning */
7342 warningf(&arg_expr->base.source_position,
7343 "passing call argument %u as '%T' rather than '%T' due to prototype",
7344 pos, expected_type_skip, promoted_type);
7350 * Parse a call expression, ie. expression '( ... )'.
7352 * @param expression the function address
7354 static expression_t *parse_call_expression(expression_t *expression)
7356 expression_t *result = allocate_expression_zero(EXPR_CALL);
7357 result->base.source_position = expression->base.source_position;
7359 call_expression_t *call = &result->call;
7360 call->function = expression;
7362 type_t *const orig_type = expression->base.type;
7363 type_t *const type = skip_typeref(orig_type);
7365 function_type_t *function_type = NULL;
7366 if (is_type_pointer(type)) {
7367 type_t *const to_type = skip_typeref(type->pointer.points_to);
7369 if (is_type_function(to_type)) {
7370 function_type = &to_type->function;
7371 call->base.type = function_type->return_type;
7375 if (function_type == NULL && is_type_valid(type)) {
7376 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
7379 /* parse arguments */
7381 add_anchor_token(')');
7382 add_anchor_token(',');
7384 if (token.type != ')') {
7385 call_argument_t *last_argument = NULL;
7388 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7390 argument->expression = parse_assignment_expression();
7391 if (last_argument == NULL) {
7392 call->arguments = argument;
7394 last_argument->next = argument;
7396 last_argument = argument;
7398 if (token.type != ',')
7403 rem_anchor_token(',');
7404 rem_anchor_token(')');
7407 if (function_type == NULL)
7410 function_parameter_t *parameter = function_type->parameters;
7411 call_argument_t *argument = call->arguments;
7412 if (!function_type->unspecified_parameters) {
7413 for (unsigned pos = 0; parameter != NULL && argument != NULL;
7414 parameter = parameter->next, argument = argument->next) {
7415 check_call_argument(parameter, argument, ++pos);
7418 if (parameter != NULL) {
7419 errorf(HERE, "too few arguments to function '%E'", expression);
7420 } else if (argument != NULL && !function_type->variadic) {
7421 errorf(HERE, "too many arguments to function '%E'", expression);
7425 /* do default promotion */
7426 for( ; argument != NULL; argument = argument->next) {
7427 type_t *type = argument->expression->base.type;
7429 type = get_default_promoted_type(type);
7431 argument->expression
7432 = create_implicit_cast(argument->expression, type);
7435 check_format(&result->call);
7437 if (warning.aggregate_return &&
7438 is_type_compound(skip_typeref(function_type->return_type))) {
7439 warningf(&result->base.source_position,
7440 "function call has aggregate value");
7447 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
7449 static bool same_compound_type(const type_t *type1, const type_t *type2)
7452 is_type_compound(type1) &&
7453 type1->kind == type2->kind &&
7454 type1->compound.declaration == type2->compound.declaration;
7458 * Parse a conditional expression, ie. 'expression ? ... : ...'.
7460 * @param expression the conditional expression
7462 static expression_t *parse_conditional_expression(expression_t *expression)
7464 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
7466 conditional_expression_t *conditional = &result->conditional;
7467 conditional->base.source_position = *HERE;
7468 conditional->condition = expression;
7471 add_anchor_token(':');
7474 type_t *const condition_type_orig = expression->base.type;
7475 type_t *const condition_type = skip_typeref(condition_type_orig);
7476 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
7477 type_error("expected a scalar type in conditional condition",
7478 &expression->base.source_position, condition_type_orig);
7481 expression_t *true_expression = expression;
7482 bool gnu_cond = false;
7483 if (GNU_MODE && token.type == ':') {
7486 true_expression = parse_expression();
7487 rem_anchor_token(':');
7489 expression_t *false_expression = parse_sub_expression(PREC_CONDITIONAL);
7491 type_t *const orig_true_type = true_expression->base.type;
7492 type_t *const orig_false_type = false_expression->base.type;
7493 type_t *const true_type = skip_typeref(orig_true_type);
7494 type_t *const false_type = skip_typeref(orig_false_type);
7497 type_t *result_type;
7498 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
7499 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7500 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
7501 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7502 warningf(&conditional->base.source_position,
7503 "ISO C forbids conditional expression with only one void side");
7505 result_type = type_void;
7506 } else if (is_type_arithmetic(true_type)
7507 && is_type_arithmetic(false_type)) {
7508 result_type = semantic_arithmetic(true_type, false_type);
7510 true_expression = create_implicit_cast(true_expression, result_type);
7511 false_expression = create_implicit_cast(false_expression, result_type);
7513 conditional->true_expression = true_expression;
7514 conditional->false_expression = false_expression;
7515 conditional->base.type = result_type;
7516 } else if (same_compound_type(true_type, false_type)) {
7517 /* just take 1 of the 2 types */
7518 result_type = true_type;
7519 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
7520 type_t *pointer_type;
7522 expression_t *other_expression;
7523 if (is_type_pointer(true_type) &&
7524 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
7525 pointer_type = true_type;
7526 other_type = false_type;
7527 other_expression = false_expression;
7529 pointer_type = false_type;
7530 other_type = true_type;
7531 other_expression = true_expression;
7534 if (is_null_pointer_constant(other_expression)) {
7535 result_type = pointer_type;
7536 } else if (is_type_pointer(other_type)) {
7537 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7538 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7541 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7542 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7544 } else if (types_compatible(get_unqualified_type(to1),
7545 get_unqualified_type(to2))) {
7548 warningf(&conditional->base.source_position,
7549 "pointer types '%T' and '%T' in conditional expression are incompatible",
7550 true_type, false_type);
7554 type_t *const type =
7555 get_qualified_type(to, to1->base.qualifiers | to2->base.qualifiers);
7556 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7557 } else if (is_type_integer(other_type)) {
7558 warningf(&conditional->base.source_position,
7559 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7560 result_type = pointer_type;
7562 if (is_type_valid(other_type)) {
7563 type_error_incompatible("while parsing conditional",
7564 &expression->base.source_position, true_type, false_type);
7566 result_type = type_error_type;
7569 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7570 type_error_incompatible("while parsing conditional",
7571 &conditional->base.source_position, true_type,
7574 result_type = type_error_type;
7577 conditional->true_expression
7578 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7579 conditional->false_expression
7580 = create_implicit_cast(false_expression, result_type);
7581 conditional->base.type = result_type;
7584 return create_invalid_expression();
7588 * Parse an extension expression.
7590 static expression_t *parse_extension(void)
7592 eat(T___extension__);
7594 bool old_gcc_extension = in_gcc_extension;
7595 in_gcc_extension = true;
7596 expression_t *expression = parse_sub_expression(PREC_UNARY);
7597 in_gcc_extension = old_gcc_extension;
7602 * Parse a __builtin_classify_type() expression.
7604 static expression_t *parse_builtin_classify_type(void)
7606 eat(T___builtin_classify_type);
7608 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7609 result->base.type = type_int;
7612 add_anchor_token(')');
7613 expression_t *expression = parse_expression();
7614 rem_anchor_token(')');
7616 result->classify_type.type_expression = expression;
7620 return create_invalid_expression();
7623 static bool check_pointer_arithmetic(const source_position_t *source_position,
7624 type_t *pointer_type,
7625 type_t *orig_pointer_type)
7627 type_t *points_to = pointer_type->pointer.points_to;
7628 points_to = skip_typeref(points_to);
7630 if (is_type_incomplete(points_to)) {
7631 if (!GNU_MODE || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7632 errorf(source_position,
7633 "arithmetic with pointer to incomplete type '%T' not allowed",
7636 } else if (warning.pointer_arith) {
7637 warningf(source_position,
7638 "pointer of type '%T' used in arithmetic",
7641 } else if (is_type_function(points_to)) {
7643 errorf(source_position,
7644 "arithmetic with pointer to function type '%T' not allowed",
7647 } else if (warning.pointer_arith) {
7648 warningf(source_position,
7649 "pointer to a function '%T' used in arithmetic",
7656 static bool is_lvalue(const expression_t *expression)
7658 switch (expression->kind) {
7659 case EXPR_REFERENCE:
7660 case EXPR_ARRAY_ACCESS:
7662 case EXPR_UNARY_DEREFERENCE:
7666 /* Claim it is an lvalue, if the type is invalid. There was a parse
7667 * error before, which maybe prevented properly recognizing it as
7669 return !is_type_valid(skip_typeref(expression->base.type));
7673 static void semantic_incdec(unary_expression_t *expression)
7675 type_t *const orig_type = expression->value->base.type;
7676 type_t *const type = skip_typeref(orig_type);
7677 if (is_type_pointer(type)) {
7678 if (!check_pointer_arithmetic(&expression->base.source_position,
7682 } else if (!is_type_real(type) && is_type_valid(type)) {
7683 /* TODO: improve error message */
7684 errorf(&expression->base.source_position,
7685 "operation needs an arithmetic or pointer type");
7688 if (!is_lvalue(expression->value)) {
7689 /* TODO: improve error message */
7690 errorf(&expression->base.source_position, "lvalue required as operand");
7692 expression->base.type = orig_type;
7695 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7697 type_t *const orig_type = expression->value->base.type;
7698 type_t *const type = skip_typeref(orig_type);
7699 if (!is_type_arithmetic(type)) {
7700 if (is_type_valid(type)) {
7701 /* TODO: improve error message */
7702 errorf(&expression->base.source_position,
7703 "operation needs an arithmetic type");
7708 expression->base.type = orig_type;
7711 static void semantic_unexpr_plus(unary_expression_t *expression)
7713 semantic_unexpr_arithmetic(expression);
7714 if (warning.traditional)
7715 warningf(&expression->base.source_position,
7716 "traditional C rejects the unary plus operator");
7719 static expression_t const *get_reference_address(expression_t const *expr)
7721 bool regular_take_address = true;
7723 if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
7724 expr = expr->unary.value;
7726 regular_take_address = false;
7729 if (expr->kind != EXPR_UNARY_DEREFERENCE)
7732 expr = expr->unary.value;
7735 if (expr->kind != EXPR_REFERENCE)
7738 if (!regular_take_address &&
7739 !is_type_function(skip_typeref(expr->reference.declaration->type))) {
7746 static void warn_function_address_as_bool(expression_t const* expr)
7748 if (!warning.address)
7751 expr = get_reference_address(expr);
7753 warningf(&expr->base.source_position,
7754 "the address of '%Y' will always evaluate as 'true'",
7755 expr->reference.declaration->symbol);
7759 static void semantic_not(unary_expression_t *expression)
7761 type_t *const orig_type = expression->value->base.type;
7762 type_t *const type = skip_typeref(orig_type);
7763 if (!is_type_scalar(type) && is_type_valid(type)) {
7764 errorf(&expression->base.source_position,
7765 "operand of ! must be of scalar type");
7768 warn_function_address_as_bool(expression->value);
7770 expression->base.type = type_int;
7773 static void semantic_unexpr_integer(unary_expression_t *expression)
7775 type_t *const orig_type = expression->value->base.type;
7776 type_t *const type = skip_typeref(orig_type);
7777 if (!is_type_integer(type)) {
7778 if (is_type_valid(type)) {
7779 errorf(&expression->base.source_position,
7780 "operand of ~ must be of integer type");
7785 expression->base.type = orig_type;
7788 static void semantic_dereference(unary_expression_t *expression)
7790 type_t *const orig_type = expression->value->base.type;
7791 type_t *const type = skip_typeref(orig_type);
7792 if (!is_type_pointer(type)) {
7793 if (is_type_valid(type)) {
7794 errorf(&expression->base.source_position,
7795 "Unary '*' needs pointer or array type, but type '%T' given", orig_type);
7800 type_t *result_type = type->pointer.points_to;
7801 result_type = automatic_type_conversion(result_type);
7802 expression->base.type = result_type;
7806 * Record that an address is taken (expression represents an lvalue).
7808 * @param expression the expression
7809 * @param may_be_register if true, the expression might be an register
7811 static void set_address_taken(expression_t *expression, bool may_be_register)
7813 if (expression->kind != EXPR_REFERENCE)
7816 declaration_t *const declaration = expression->reference.declaration;
7817 /* happens for parse errors */
7818 if (declaration == NULL)
7821 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7822 errorf(&expression->base.source_position,
7823 "address of register variable '%Y' requested",
7824 declaration->symbol);
7826 declaration->address_taken = 1;
7831 * Check the semantic of the address taken expression.
7833 static void semantic_take_addr(unary_expression_t *expression)
7835 expression_t *value = expression->value;
7836 value->base.type = revert_automatic_type_conversion(value);
7838 type_t *orig_type = value->base.type;
7839 if (!is_type_valid(skip_typeref(orig_type)))
7842 set_address_taken(value, false);
7844 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7847 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7848 static expression_t *parse_##unexpression_type(void) \
7850 expression_t *unary_expression \
7851 = allocate_expression_zero(unexpression_type); \
7852 unary_expression->base.source_position = *HERE; \
7854 unary_expression->unary.value = parse_sub_expression(PREC_UNARY); \
7856 sfunc(&unary_expression->unary); \
7858 return unary_expression; \
7861 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7862 semantic_unexpr_arithmetic)
7863 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7864 semantic_unexpr_plus)
7865 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7867 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7868 semantic_dereference)
7869 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7871 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7872 semantic_unexpr_integer)
7873 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7875 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7878 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7880 static expression_t *parse_##unexpression_type(expression_t *left) \
7882 expression_t *unary_expression \
7883 = allocate_expression_zero(unexpression_type); \
7884 unary_expression->base.source_position = *HERE; \
7886 unary_expression->unary.value = left; \
7888 sfunc(&unary_expression->unary); \
7890 return unary_expression; \
7893 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7894 EXPR_UNARY_POSTFIX_INCREMENT,
7896 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7897 EXPR_UNARY_POSTFIX_DECREMENT,
7900 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7902 /* TODO: handle complex + imaginary types */
7904 type_left = get_unqualified_type(type_left);
7905 type_right = get_unqualified_type(type_right);
7907 /* § 6.3.1.8 Usual arithmetic conversions */
7908 if (type_left == type_long_double || type_right == type_long_double) {
7909 return type_long_double;
7910 } else if (type_left == type_double || type_right == type_double) {
7912 } else if (type_left == type_float || type_right == type_float) {
7916 type_left = promote_integer(type_left);
7917 type_right = promote_integer(type_right);
7919 if (type_left == type_right)
7922 bool const signed_left = is_type_signed(type_left);
7923 bool const signed_right = is_type_signed(type_right);
7924 int const rank_left = get_rank(type_left);
7925 int const rank_right = get_rank(type_right);
7927 if (signed_left == signed_right)
7928 return rank_left >= rank_right ? type_left : type_right;
7937 u_rank = rank_right;
7938 u_type = type_right;
7940 s_rank = rank_right;
7941 s_type = type_right;
7946 if (u_rank >= s_rank)
7949 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7951 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7952 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7956 case ATOMIC_TYPE_INT: return type_unsigned_int;
7957 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7958 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7960 default: panic("invalid atomic type");
7965 * Check the semantic restrictions for a binary expression.
7967 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7969 expression_t *const left = expression->left;
7970 expression_t *const right = expression->right;
7971 type_t *const orig_type_left = left->base.type;
7972 type_t *const orig_type_right = right->base.type;
7973 type_t *const type_left = skip_typeref(orig_type_left);
7974 type_t *const type_right = skip_typeref(orig_type_right);
7976 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7977 /* TODO: improve error message */
7978 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7979 errorf(&expression->base.source_position,
7980 "operation needs arithmetic types");
7985 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7986 expression->left = create_implicit_cast(left, arithmetic_type);
7987 expression->right = create_implicit_cast(right, arithmetic_type);
7988 expression->base.type = arithmetic_type;
7991 static void warn_div_by_zero(binary_expression_t const *const expression)
7993 if (!warning.div_by_zero ||
7994 !is_type_integer(expression->base.type))
7997 expression_t const *const right = expression->right;
7998 /* The type of the right operand can be different for /= */
7999 if (is_type_integer(right->base.type) &&
8000 is_constant_expression(right) &&
8001 fold_constant(right) == 0) {
8002 warningf(&expression->base.source_position, "division by zero");
8007 * Check the semantic restrictions for a div/mod expression.
8009 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
8010 semantic_binexpr_arithmetic(expression);
8011 warn_div_by_zero(expression);
8014 static void semantic_shift_op(binary_expression_t *expression)
8016 expression_t *const left = expression->left;
8017 expression_t *const right = expression->right;
8018 type_t *const orig_type_left = left->base.type;
8019 type_t *const orig_type_right = right->base.type;
8020 type_t * type_left = skip_typeref(orig_type_left);
8021 type_t * type_right = skip_typeref(orig_type_right);
8023 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
8024 /* TODO: improve error message */
8025 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8026 errorf(&expression->base.source_position,
8027 "operands of shift operation must have integer types");
8032 type_left = promote_integer(type_left);
8033 type_right = promote_integer(type_right);
8035 expression->left = create_implicit_cast(left, type_left);
8036 expression->right = create_implicit_cast(right, type_right);
8037 expression->base.type = type_left;
8040 static void semantic_add(binary_expression_t *expression)
8042 expression_t *const left = expression->left;
8043 expression_t *const right = expression->right;
8044 type_t *const orig_type_left = left->base.type;
8045 type_t *const orig_type_right = right->base.type;
8046 type_t *const type_left = skip_typeref(orig_type_left);
8047 type_t *const type_right = skip_typeref(orig_type_right);
8050 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8051 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8052 expression->left = create_implicit_cast(left, arithmetic_type);
8053 expression->right = create_implicit_cast(right, arithmetic_type);
8054 expression->base.type = arithmetic_type;
8056 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8057 check_pointer_arithmetic(&expression->base.source_position,
8058 type_left, orig_type_left);
8059 expression->base.type = type_left;
8060 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
8061 check_pointer_arithmetic(&expression->base.source_position,
8062 type_right, orig_type_right);
8063 expression->base.type = type_right;
8064 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8065 errorf(&expression->base.source_position,
8066 "invalid operands to binary + ('%T', '%T')",
8067 orig_type_left, orig_type_right);
8071 static void semantic_sub(binary_expression_t *expression)
8073 expression_t *const left = expression->left;
8074 expression_t *const right = expression->right;
8075 type_t *const orig_type_left = left->base.type;
8076 type_t *const orig_type_right = right->base.type;
8077 type_t *const type_left = skip_typeref(orig_type_left);
8078 type_t *const type_right = skip_typeref(orig_type_right);
8079 source_position_t const *const pos = &expression->base.source_position;
8082 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8083 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8084 expression->left = create_implicit_cast(left, arithmetic_type);
8085 expression->right = create_implicit_cast(right, arithmetic_type);
8086 expression->base.type = arithmetic_type;
8088 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8089 check_pointer_arithmetic(&expression->base.source_position,
8090 type_left, orig_type_left);
8091 expression->base.type = type_left;
8092 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8093 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
8094 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
8095 if (!types_compatible(unqual_left, unqual_right)) {
8097 "subtracting pointers to incompatible types '%T' and '%T'",
8098 orig_type_left, orig_type_right);
8099 } else if (!is_type_object(unqual_left)) {
8100 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
8101 warningf(pos, "subtracting pointers to void");
8103 errorf(pos, "subtracting pointers to non-object types '%T'",
8107 expression->base.type = type_ptrdiff_t;
8108 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8109 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
8110 orig_type_left, orig_type_right);
8114 static void warn_string_literal_address(expression_t const* expr)
8116 while (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
8117 expr = expr->unary.value;
8118 if (expr->kind != EXPR_UNARY_DEREFERENCE)
8120 expr = expr->unary.value;
8123 if (expr->kind == EXPR_STRING_LITERAL ||
8124 expr->kind == EXPR_WIDE_STRING_LITERAL) {
8125 warningf(&expr->base.source_position,
8126 "comparison with string literal results in unspecified behaviour");
8131 * Check the semantics of comparison expressions.
8133 * @param expression The expression to check.
8135 static void semantic_comparison(binary_expression_t *expression)
8137 expression_t *left = expression->left;
8138 expression_t *right = expression->right;
8140 if (warning.address) {
8141 warn_string_literal_address(left);
8142 warn_string_literal_address(right);
8144 expression_t const* const func_left = get_reference_address(left);
8145 if (func_left != NULL && is_null_pointer_constant(right)) {
8146 warningf(&expression->base.source_position,
8147 "the address of '%Y' will never be NULL",
8148 func_left->reference.declaration->symbol);
8151 expression_t const* const func_right = get_reference_address(right);
8152 if (func_right != NULL && is_null_pointer_constant(right)) {
8153 warningf(&expression->base.source_position,
8154 "the address of '%Y' will never be NULL",
8155 func_right->reference.declaration->symbol);
8159 type_t *orig_type_left = left->base.type;
8160 type_t *orig_type_right = right->base.type;
8161 type_t *type_left = skip_typeref(orig_type_left);
8162 type_t *type_right = skip_typeref(orig_type_right);
8164 /* TODO non-arithmetic types */
8165 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8166 /* test for signed vs unsigned compares */
8167 if (warning.sign_compare &&
8168 (expression->base.kind != EXPR_BINARY_EQUAL &&
8169 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
8170 (is_type_signed(type_left) != is_type_signed(type_right))) {
8172 /* check if 1 of the operands is a constant, in this case we just
8173 * check wether we can safely represent the resulting constant in
8174 * the type of the other operand. */
8175 expression_t *const_expr = NULL;
8176 expression_t *other_expr = NULL;
8178 if (is_constant_expression(left)) {
8181 } else if (is_constant_expression(right)) {
8186 if (const_expr != NULL) {
8187 type_t *other_type = skip_typeref(other_expr->base.type);
8188 long val = fold_constant(const_expr);
8189 /* TODO: check if val can be represented by other_type */
8193 warningf(&expression->base.source_position,
8194 "comparison between signed and unsigned");
8196 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8197 expression->left = create_implicit_cast(left, arithmetic_type);
8198 expression->right = create_implicit_cast(right, arithmetic_type);
8199 expression->base.type = arithmetic_type;
8200 if (warning.float_equal &&
8201 (expression->base.kind == EXPR_BINARY_EQUAL ||
8202 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
8203 is_type_float(arithmetic_type)) {
8204 warningf(&expression->base.source_position,
8205 "comparing floating point with == or != is unsafe");
8207 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8208 /* TODO check compatibility */
8209 } else if (is_type_pointer(type_left)) {
8210 expression->right = create_implicit_cast(right, type_left);
8211 } else if (is_type_pointer(type_right)) {
8212 expression->left = create_implicit_cast(left, type_right);
8213 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8214 type_error_incompatible("invalid operands in comparison",
8215 &expression->base.source_position,
8216 type_left, type_right);
8218 expression->base.type = type_int;
8222 * Checks if a compound type has constant fields.
8224 static bool has_const_fields(const compound_type_t *type)
8226 const scope_t *scope = &type->declaration->scope;
8227 const declaration_t *declaration = scope->declarations;
8229 for (; declaration != NULL; declaration = declaration->next) {
8230 if (declaration->namespc != NAMESPACE_NORMAL)
8233 const type_t *decl_type = skip_typeref(declaration->type);
8234 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
8241 static bool is_valid_assignment_lhs(expression_t const* const left)
8243 type_t *const orig_type_left = revert_automatic_type_conversion(left);
8244 type_t *const type_left = skip_typeref(orig_type_left);
8246 if (!is_lvalue(left)) {
8247 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
8252 if (is_type_array(type_left)) {
8253 errorf(HERE, "cannot assign to arrays ('%E')", left);
8256 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
8257 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
8261 if (is_type_incomplete(type_left)) {
8262 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
8263 left, orig_type_left);
8266 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
8267 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
8268 left, orig_type_left);
8275 static void semantic_arithmetic_assign(binary_expression_t *expression)
8277 expression_t *left = expression->left;
8278 expression_t *right = expression->right;
8279 type_t *orig_type_left = left->base.type;
8280 type_t *orig_type_right = right->base.type;
8282 if (!is_valid_assignment_lhs(left))
8285 type_t *type_left = skip_typeref(orig_type_left);
8286 type_t *type_right = skip_typeref(orig_type_right);
8288 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
8289 /* TODO: improve error message */
8290 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8291 errorf(&expression->base.source_position,
8292 "operation needs arithmetic types");
8297 /* combined instructions are tricky. We can't create an implicit cast on
8298 * the left side, because we need the uncasted form for the store.
8299 * The ast2firm pass has to know that left_type must be right_type
8300 * for the arithmetic operation and create a cast by itself */
8301 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8302 expression->right = create_implicit_cast(right, arithmetic_type);
8303 expression->base.type = type_left;
8306 static void semantic_divmod_assign(binary_expression_t *expression)
8308 semantic_arithmetic_assign(expression);
8309 warn_div_by_zero(expression);
8312 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
8314 expression_t *const left = expression->left;
8315 expression_t *const right = expression->right;
8316 type_t *const orig_type_left = left->base.type;
8317 type_t *const orig_type_right = right->base.type;
8318 type_t *const type_left = skip_typeref(orig_type_left);
8319 type_t *const type_right = skip_typeref(orig_type_right);
8321 if (!is_valid_assignment_lhs(left))
8324 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8325 /* combined instructions are tricky. We can't create an implicit cast on
8326 * the left side, because we need the uncasted form for the store.
8327 * The ast2firm pass has to know that left_type must be right_type
8328 * for the arithmetic operation and create a cast by itself */
8329 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
8330 expression->right = create_implicit_cast(right, arithmetic_type);
8331 expression->base.type = type_left;
8332 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8333 check_pointer_arithmetic(&expression->base.source_position,
8334 type_left, orig_type_left);
8335 expression->base.type = type_left;
8336 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8337 errorf(&expression->base.source_position,
8338 "incompatible types '%T' and '%T' in assignment",
8339 orig_type_left, orig_type_right);
8344 * Check the semantic restrictions of a logical expression.
8346 static void semantic_logical_op(binary_expression_t *expression)
8348 expression_t *const left = expression->left;
8349 expression_t *const right = expression->right;
8350 type_t *const orig_type_left = left->base.type;
8351 type_t *const orig_type_right = right->base.type;
8352 type_t *const type_left = skip_typeref(orig_type_left);
8353 type_t *const type_right = skip_typeref(orig_type_right);
8355 warn_function_address_as_bool(left);
8356 warn_function_address_as_bool(right);
8358 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
8359 /* TODO: improve error message */
8360 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8361 errorf(&expression->base.source_position,
8362 "operation needs scalar types");
8367 expression->base.type = type_int;
8371 * Check the semantic restrictions of a binary assign expression.
8373 static void semantic_binexpr_assign(binary_expression_t *expression)
8375 expression_t *left = expression->left;
8376 type_t *orig_type_left = left->base.type;
8378 if (!is_valid_assignment_lhs(left))
8381 assign_error_t error = semantic_assign(orig_type_left, expression->right);
8382 report_assign_error(error, orig_type_left, expression->right,
8383 "assignment", &left->base.source_position);
8384 expression->right = create_implicit_cast(expression->right, orig_type_left);
8385 expression->base.type = orig_type_left;
8389 * Determine if the outermost operation (or parts thereof) of the given
8390 * expression has no effect in order to generate a warning about this fact.
8391 * Therefore in some cases this only examines some of the operands of the
8392 * expression (see comments in the function and examples below).
8394 * f() + 23; // warning, because + has no effect
8395 * x || f(); // no warning, because x controls execution of f()
8396 * x ? y : f(); // warning, because y has no effect
8397 * (void)x; // no warning to be able to suppress the warning
8398 * This function can NOT be used for an "expression has definitely no effect"-
8400 static bool expression_has_effect(const expression_t *const expr)
8402 switch (expr->kind) {
8403 case EXPR_UNKNOWN: break;
8404 case EXPR_INVALID: return true; /* do NOT warn */
8405 case EXPR_REFERENCE: return false;
8406 /* suppress the warning for microsoft __noop operations */
8407 case EXPR_CONST: return expr->conste.is_ms_noop;
8408 case EXPR_CHARACTER_CONSTANT: return false;
8409 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
8410 case EXPR_STRING_LITERAL: return false;
8411 case EXPR_WIDE_STRING_LITERAL: return false;
8412 case EXPR_LABEL_ADDRESS: return false;
8415 const call_expression_t *const call = &expr->call;
8416 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
8419 switch (call->function->builtin_symbol.symbol->ID) {
8420 case T___builtin_va_end: return true;
8421 default: return false;
8425 /* Generate the warning if either the left or right hand side of a
8426 * conditional expression has no effect */
8427 case EXPR_CONDITIONAL: {
8428 const conditional_expression_t *const cond = &expr->conditional;
8430 expression_has_effect(cond->true_expression) &&
8431 expression_has_effect(cond->false_expression);
8434 case EXPR_SELECT: return false;
8435 case EXPR_ARRAY_ACCESS: return false;
8436 case EXPR_SIZEOF: return false;
8437 case EXPR_CLASSIFY_TYPE: return false;
8438 case EXPR_ALIGNOF: return false;
8440 case EXPR_FUNCNAME: return false;
8441 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
8442 case EXPR_BUILTIN_CONSTANT_P: return false;
8443 case EXPR_BUILTIN_PREFETCH: return true;
8444 case EXPR_OFFSETOF: return false;
8445 case EXPR_VA_START: return true;
8446 case EXPR_VA_ARG: return true;
8447 case EXPR_STATEMENT: return true; // TODO
8448 case EXPR_COMPOUND_LITERAL: return false;
8450 case EXPR_UNARY_NEGATE: return false;
8451 case EXPR_UNARY_PLUS: return false;
8452 case EXPR_UNARY_BITWISE_NEGATE: return false;
8453 case EXPR_UNARY_NOT: return false;
8454 case EXPR_UNARY_DEREFERENCE: return false;
8455 case EXPR_UNARY_TAKE_ADDRESS: return false;
8456 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
8457 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
8458 case EXPR_UNARY_PREFIX_INCREMENT: return true;
8459 case EXPR_UNARY_PREFIX_DECREMENT: return true;
8461 /* Treat void casts as if they have an effect in order to being able to
8462 * suppress the warning */
8463 case EXPR_UNARY_CAST: {
8464 type_t *const type = skip_typeref(expr->base.type);
8465 return is_type_atomic(type, ATOMIC_TYPE_VOID);
8468 case EXPR_UNARY_CAST_IMPLICIT: return true;
8469 case EXPR_UNARY_ASSUME: return true;
8471 case EXPR_BINARY_ADD: return false;
8472 case EXPR_BINARY_SUB: return false;
8473 case EXPR_BINARY_MUL: return false;
8474 case EXPR_BINARY_DIV: return false;
8475 case EXPR_BINARY_MOD: return false;
8476 case EXPR_BINARY_EQUAL: return false;
8477 case EXPR_BINARY_NOTEQUAL: return false;
8478 case EXPR_BINARY_LESS: return false;
8479 case EXPR_BINARY_LESSEQUAL: return false;
8480 case EXPR_BINARY_GREATER: return false;
8481 case EXPR_BINARY_GREATEREQUAL: return false;
8482 case EXPR_BINARY_BITWISE_AND: return false;
8483 case EXPR_BINARY_BITWISE_OR: return false;
8484 case EXPR_BINARY_BITWISE_XOR: return false;
8485 case EXPR_BINARY_SHIFTLEFT: return false;
8486 case EXPR_BINARY_SHIFTRIGHT: return false;
8487 case EXPR_BINARY_ASSIGN: return true;
8488 case EXPR_BINARY_MUL_ASSIGN: return true;
8489 case EXPR_BINARY_DIV_ASSIGN: return true;
8490 case EXPR_BINARY_MOD_ASSIGN: return true;
8491 case EXPR_BINARY_ADD_ASSIGN: return true;
8492 case EXPR_BINARY_SUB_ASSIGN: return true;
8493 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
8494 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
8495 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
8496 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
8497 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
8499 /* Only examine the right hand side of && and ||, because the left hand
8500 * side already has the effect of controlling the execution of the right
8502 case EXPR_BINARY_LOGICAL_AND:
8503 case EXPR_BINARY_LOGICAL_OR:
8504 /* Only examine the right hand side of a comma expression, because the left
8505 * hand side has a separate warning */
8506 case EXPR_BINARY_COMMA:
8507 return expression_has_effect(expr->binary.right);
8509 case EXPR_BINARY_BUILTIN_EXPECT: return true;
8510 case EXPR_BINARY_ISGREATER: return false;
8511 case EXPR_BINARY_ISGREATEREQUAL: return false;
8512 case EXPR_BINARY_ISLESS: return false;
8513 case EXPR_BINARY_ISLESSEQUAL: return false;
8514 case EXPR_BINARY_ISLESSGREATER: return false;
8515 case EXPR_BINARY_ISUNORDERED: return false;
8518 internal_errorf(HERE, "unexpected expression");
8521 static void semantic_comma(binary_expression_t *expression)
8523 if (warning.unused_value) {
8524 const expression_t *const left = expression->left;
8525 if (!expression_has_effect(left)) {
8526 warningf(&left->base.source_position,
8527 "left-hand operand of comma expression has no effect");
8530 expression->base.type = expression->right->base.type;
8534 * @param prec_r precedence of the right operand
8536 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, prec_r, sfunc) \
8537 static expression_t *parse_##binexpression_type(expression_t *left) \
8539 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
8540 binexpr->base.source_position = *HERE; \
8541 binexpr->binary.left = left; \
8544 expression_t *right = parse_sub_expression(prec_r); \
8546 binexpr->binary.right = right; \
8547 sfunc(&binexpr->binary); \
8552 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, PREC_CAST, semantic_binexpr_arithmetic)
8553 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, PREC_CAST, semantic_divmod_arithmetic)
8554 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, PREC_CAST, semantic_divmod_arithmetic)
8555 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, PREC_MULTIPLICATIVE, semantic_add)
8556 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, PREC_MULTIPLICATIVE, semantic_sub)
8557 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT, PREC_ADDITIVE, semantic_shift_op)
8558 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT, PREC_ADDITIVE, semantic_shift_op)
8559 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, PREC_SHIFT, semantic_comparison)
8560 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, PREC_SHIFT, semantic_comparison)
8561 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL, PREC_SHIFT, semantic_comparison)
8562 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL, PREC_SHIFT, semantic_comparison)
8563 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL, PREC_RELATIONAL, semantic_comparison)
8564 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL, PREC_RELATIONAL, semantic_comparison)
8565 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND, PREC_EQUALITY, semantic_binexpr_arithmetic)
8566 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR, PREC_AND, semantic_binexpr_arithmetic)
8567 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR, PREC_XOR, semantic_binexpr_arithmetic)
8568 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND, PREC_OR, semantic_logical_op)
8569 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR, PREC_LOGICAL_AND, semantic_logical_op)
8570 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, PREC_ASSIGNMENT, semantic_binexpr_assign)
8571 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8572 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
8573 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8574 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8575 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
8576 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8577 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8578 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8579 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8580 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
8581 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, PREC_ASSIGNMENT, semantic_comma)
8584 static expression_t *parse_sub_expression(precedence_t precedence)
8586 if (token.type < 0) {
8587 return expected_expression_error();
8590 expression_parser_function_t *parser
8591 = &expression_parsers[token.type];
8592 source_position_t source_position = token.source_position;
8595 if (parser->parser != NULL) {
8596 left = parser->parser();
8598 left = parse_primary_expression();
8600 assert(left != NULL);
8601 left->base.source_position = source_position;
8604 if (token.type < 0) {
8605 return expected_expression_error();
8608 parser = &expression_parsers[token.type];
8609 if (parser->infix_parser == NULL)
8611 if (parser->infix_precedence < precedence)
8614 left = parser->infix_parser(left);
8616 assert(left != NULL);
8617 assert(left->kind != EXPR_UNKNOWN);
8618 left->base.source_position = source_position;
8625 * Parse an expression.
8627 static expression_t *parse_expression(void)
8629 return parse_sub_expression(PREC_EXPRESSION);
8633 * Register a parser for a prefix-like operator.
8635 * @param parser the parser function
8636 * @param token_type the token type of the prefix token
8638 static void register_expression_parser(parse_expression_function parser,
8641 expression_parser_function_t *entry = &expression_parsers[token_type];
8643 if (entry->parser != NULL) {
8644 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8645 panic("trying to register multiple expression parsers for a token");
8647 entry->parser = parser;
8651 * Register a parser for an infix operator with given precedence.
8653 * @param parser the parser function
8654 * @param token_type the token type of the infix operator
8655 * @param precedence the precedence of the operator
8657 static void register_infix_parser(parse_expression_infix_function parser,
8658 int token_type, unsigned precedence)
8660 expression_parser_function_t *entry = &expression_parsers[token_type];
8662 if (entry->infix_parser != NULL) {
8663 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8664 panic("trying to register multiple infix expression parsers for a "
8667 entry->infix_parser = parser;
8668 entry->infix_precedence = precedence;
8672 * Initialize the expression parsers.
8674 static void init_expression_parsers(void)
8676 memset(&expression_parsers, 0, sizeof(expression_parsers));
8678 register_infix_parser(parse_array_expression, '[', PREC_POSTFIX);
8679 register_infix_parser(parse_call_expression, '(', PREC_POSTFIX);
8680 register_infix_parser(parse_select_expression, '.', PREC_POSTFIX);
8681 register_infix_parser(parse_select_expression, T_MINUSGREATER, PREC_POSTFIX);
8682 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT, T_PLUSPLUS, PREC_POSTFIX);
8683 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT, T_MINUSMINUS, PREC_POSTFIX);
8684 register_infix_parser(parse_EXPR_BINARY_MUL, '*', PREC_MULTIPLICATIVE);
8685 register_infix_parser(parse_EXPR_BINARY_DIV, '/', PREC_MULTIPLICATIVE);
8686 register_infix_parser(parse_EXPR_BINARY_MOD, '%', PREC_MULTIPLICATIVE);
8687 register_infix_parser(parse_EXPR_BINARY_ADD, '+', PREC_ADDITIVE);
8688 register_infix_parser(parse_EXPR_BINARY_SUB, '-', PREC_ADDITIVE);
8689 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, PREC_SHIFT);
8690 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, PREC_SHIFT);
8691 register_infix_parser(parse_EXPR_BINARY_LESS, '<', PREC_RELATIONAL);
8692 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', PREC_RELATIONAL);
8693 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, PREC_RELATIONAL);
8694 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, PREC_RELATIONAL);
8695 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, PREC_EQUALITY);
8696 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL, T_EXCLAMATIONMARKEQUAL, PREC_EQUALITY);
8697 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', PREC_AND);
8698 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', PREC_XOR);
8699 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', PREC_OR);
8700 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, PREC_LOGICAL_AND);
8701 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, PREC_LOGICAL_OR);
8702 register_infix_parser(parse_conditional_expression, '?', PREC_CONDITIONAL);
8703 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', PREC_ASSIGNMENT);
8704 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, PREC_ASSIGNMENT);
8705 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, PREC_ASSIGNMENT);
8706 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, PREC_ASSIGNMENT);
8707 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, PREC_ASSIGNMENT);
8708 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, PREC_ASSIGNMENT);
8709 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN, T_LESSLESSEQUAL, PREC_ASSIGNMENT);
8710 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN, T_GREATERGREATEREQUAL, PREC_ASSIGNMENT);
8711 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN, T_ANDEQUAL, PREC_ASSIGNMENT);
8712 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN, T_PIPEEQUAL, PREC_ASSIGNMENT);
8713 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN, T_CARETEQUAL, PREC_ASSIGNMENT);
8714 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', PREC_EXPRESSION);
8716 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-');
8717 register_expression_parser(parse_EXPR_UNARY_PLUS, '+');
8718 register_expression_parser(parse_EXPR_UNARY_NOT, '!');
8719 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~');
8720 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*');
8721 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&');
8722 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT, T_PLUSPLUS);
8723 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT, T_MINUSMINUS);
8724 register_expression_parser(parse_sizeof, T_sizeof);
8725 register_expression_parser(parse_alignof, T___alignof__);
8726 register_expression_parser(parse_extension, T___extension__);
8727 register_expression_parser(parse_builtin_classify_type, T___builtin_classify_type);
8731 * Parse a asm statement arguments specification.
8733 static asm_argument_t *parse_asm_arguments(bool is_out)
8735 asm_argument_t *result = NULL;
8736 asm_argument_t *last = NULL;
8738 while (token.type == T_STRING_LITERAL || token.type == '[') {
8739 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8740 memset(argument, 0, sizeof(argument[0]));
8742 if (token.type == '[') {
8744 if (token.type != T_IDENTIFIER) {
8745 parse_error_expected("while parsing asm argument",
8746 T_IDENTIFIER, NULL);
8749 argument->symbol = token.v.symbol;
8754 argument->constraints = parse_string_literals();
8756 add_anchor_token(')');
8757 expression_t *expression = parse_expression();
8758 rem_anchor_token(')');
8760 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8761 * change size or type representation (e.g. int -> long is ok, but
8762 * int -> float is not) */
8763 if (expression->kind == EXPR_UNARY_CAST) {
8764 type_t *const type = expression->base.type;
8765 type_kind_t const kind = type->kind;
8766 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8769 if (kind == TYPE_ATOMIC) {
8770 atomic_type_kind_t const akind = type->atomic.akind;
8771 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8772 size = get_atomic_type_size(akind);
8774 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8775 size = get_atomic_type_size(get_intptr_kind());
8779 expression_t *const value = expression->unary.value;
8780 type_t *const value_type = value->base.type;
8781 type_kind_t const value_kind = value_type->kind;
8783 unsigned value_flags;
8784 unsigned value_size;
8785 if (value_kind == TYPE_ATOMIC) {
8786 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8787 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8788 value_size = get_atomic_type_size(value_akind);
8789 } else if (value_kind == TYPE_POINTER) {
8790 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8791 value_size = get_atomic_type_size(get_intptr_kind());
8796 if (value_flags != flags || value_size != size)
8800 } while (expression->kind == EXPR_UNARY_CAST);
8804 if (!is_lvalue(expression)) {
8805 errorf(&expression->base.source_position,
8806 "asm output argument is not an lvalue");
8809 if (argument->constraints.begin[0] == '+')
8810 mark_decls_read(expression, NULL);
8812 mark_decls_read(expression, NULL);
8814 argument->expression = expression;
8817 set_address_taken(expression, true);
8820 last->next = argument;
8826 if (token.type != ',')
8837 * Parse a asm statement clobber specification.
8839 static asm_clobber_t *parse_asm_clobbers(void)
8841 asm_clobber_t *result = NULL;
8842 asm_clobber_t *last = NULL;
8844 while(token.type == T_STRING_LITERAL) {
8845 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8846 clobber->clobber = parse_string_literals();
8849 last->next = clobber;
8855 if (token.type != ',')
8864 * Parse an asm statement.
8866 static statement_t *parse_asm_statement(void)
8868 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8869 asm_statement_t *asm_statement = &statement->asms;
8873 if (token.type == T_volatile) {
8875 asm_statement->is_volatile = true;
8879 add_anchor_token(')');
8880 add_anchor_token(':');
8881 asm_statement->asm_text = parse_string_literals();
8883 if (token.type != ':') {
8884 rem_anchor_token(':');
8889 asm_statement->outputs = parse_asm_arguments(true);
8890 if (token.type != ':') {
8891 rem_anchor_token(':');
8896 asm_statement->inputs = parse_asm_arguments(false);
8897 if (token.type != ':') {
8898 rem_anchor_token(':');
8901 rem_anchor_token(':');
8904 asm_statement->clobbers = parse_asm_clobbers();
8907 rem_anchor_token(')');
8911 if (asm_statement->outputs == NULL) {
8912 /* GCC: An 'asm' instruction without any output operands will be treated
8913 * identically to a volatile 'asm' instruction. */
8914 asm_statement->is_volatile = true;
8919 return create_invalid_statement();
8923 * Parse a case statement.
8925 static statement_t *parse_case_statement(void)
8927 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8928 source_position_t *const pos = &statement->base.source_position;
8932 expression_t *const expression = parse_expression();
8933 statement->case_label.expression = expression;
8934 if (!is_constant_expression(expression)) {
8935 /* This check does not prevent the error message in all cases of an
8936 * prior error while parsing the expression. At least it catches the
8937 * common case of a mistyped enum entry. */
8938 if (is_type_valid(skip_typeref(expression->base.type))) {
8939 errorf(pos, "case label does not reduce to an integer constant");
8941 statement->case_label.is_bad = true;
8943 long const val = fold_constant(expression);
8944 statement->case_label.first_case = val;
8945 statement->case_label.last_case = val;
8949 if (token.type == T_DOTDOTDOT) {
8951 expression_t *const end_range = parse_expression();
8952 statement->case_label.end_range = end_range;
8953 if (!is_constant_expression(end_range)) {
8954 /* This check does not prevent the error message in all cases of an
8955 * prior error while parsing the expression. At least it catches the
8956 * common case of a mistyped enum entry. */
8957 if (is_type_valid(skip_typeref(end_range->base.type))) {
8958 errorf(pos, "case range does not reduce to an integer constant");
8960 statement->case_label.is_bad = true;
8962 long const val = fold_constant(end_range);
8963 statement->case_label.last_case = val;
8965 if (val < statement->case_label.first_case) {
8966 statement->case_label.is_empty_range = true;
8967 warningf(pos, "empty range specified");
8973 PUSH_PARENT(statement);
8977 if (current_switch != NULL) {
8978 if (! statement->case_label.is_bad) {
8979 /* Check for duplicate case values */
8980 case_label_statement_t *c = &statement->case_label;
8981 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
8982 if (l->is_bad || l->is_empty_range || l->expression == NULL)
8985 if (c->last_case < l->first_case || c->first_case > l->last_case)
8988 errorf(pos, "duplicate case value (previously used %P)",
8989 &l->base.source_position);
8993 /* link all cases into the switch statement */
8994 if (current_switch->last_case == NULL) {
8995 current_switch->first_case = &statement->case_label;
8997 current_switch->last_case->next = &statement->case_label;
8999 current_switch->last_case = &statement->case_label;
9001 errorf(pos, "case label not within a switch statement");
9004 statement_t *const inner_stmt = parse_statement();
9005 statement->case_label.statement = inner_stmt;
9006 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9007 errorf(&inner_stmt->base.source_position, "declaration after case label");
9014 return create_invalid_statement();
9018 * Parse a default statement.
9020 static statement_t *parse_default_statement(void)
9022 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9026 PUSH_PARENT(statement);
9029 if (current_switch != NULL) {
9030 const case_label_statement_t *def_label = current_switch->default_label;
9031 if (def_label != NULL) {
9032 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
9033 &def_label->base.source_position);
9035 current_switch->default_label = &statement->case_label;
9037 /* link all cases into the switch statement */
9038 if (current_switch->last_case == NULL) {
9039 current_switch->first_case = &statement->case_label;
9041 current_switch->last_case->next = &statement->case_label;
9043 current_switch->last_case = &statement->case_label;
9046 errorf(&statement->base.source_position,
9047 "'default' label not within a switch statement");
9050 statement_t *const inner_stmt = parse_statement();
9051 statement->case_label.statement = inner_stmt;
9052 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9053 errorf(&inner_stmt->base.source_position, "declaration after default label");
9060 return create_invalid_statement();
9064 * Parse a label statement.
9066 static statement_t *parse_label_statement(void)
9068 assert(token.type == T_IDENTIFIER);
9069 symbol_t *symbol = token.v.symbol;
9070 declaration_t *label = get_label(symbol);
9072 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
9073 statement->label.label = label;
9077 PUSH_PARENT(statement);
9079 /* if statement is already set then the label is defined twice,
9080 * otherwise it was just mentioned in a goto/local label declaration so far */
9081 if (label->init.statement != NULL) {
9082 errorf(HERE, "duplicate label '%Y' (declared %P)",
9083 symbol, &label->source_position);
9085 label->source_position = token.source_position;
9086 label->init.statement = statement;
9091 if (token.type == '}') {
9092 /* TODO only warn? */
9094 warningf(HERE, "label at end of compound statement");
9095 statement->label.statement = create_empty_statement();
9097 errorf(HERE, "label at end of compound statement");
9098 statement->label.statement = create_invalid_statement();
9100 } else if (token.type == ';') {
9101 /* Eat an empty statement here, to avoid the warning about an empty
9102 * statement after a label. label:; is commonly used to have a label
9103 * before a closing brace. */
9104 statement->label.statement = create_empty_statement();
9107 statement_t *const inner_stmt = parse_statement();
9108 statement->label.statement = inner_stmt;
9109 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9110 errorf(&inner_stmt->base.source_position, "declaration after label");
9114 /* remember the labels in a list for later checking */
9115 if (label_last == NULL) {
9116 label_first = &statement->label;
9118 label_last->next = &statement->label;
9120 label_last = &statement->label;
9127 * Parse an if statement.
9129 static statement_t *parse_if(void)
9131 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
9135 PUSH_PARENT(statement);
9137 add_anchor_token('{');
9140 add_anchor_token(')');
9141 expression_t *const expr = parse_expression();
9142 statement->ifs.condition = expr;
9143 mark_decls_read(expr, NULL);
9144 rem_anchor_token(')');
9148 rem_anchor_token('{');
9150 add_anchor_token(T_else);
9151 statement->ifs.true_statement = parse_statement();
9152 rem_anchor_token(T_else);
9154 if (token.type == T_else) {
9156 statement->ifs.false_statement = parse_statement();
9164 * Check that all enums are handled in a switch.
9166 * @param statement the switch statement to check
9168 static void check_enum_cases(const switch_statement_t *statement) {
9169 const type_t *type = skip_typeref(statement->expression->base.type);
9170 if (! is_type_enum(type))
9172 const enum_type_t *enumt = &type->enumt;
9174 /* if we have a default, no warnings */
9175 if (statement->default_label != NULL)
9178 /* FIXME: calculation of value should be done while parsing */
9179 const declaration_t *declaration;
9180 long last_value = -1;
9181 for (declaration = enumt->declaration->next;
9182 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
9183 declaration = declaration->next) {
9184 const expression_t *expression = declaration->init.enum_value;
9185 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
9187 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
9188 if (l->expression == NULL)
9190 if (l->first_case <= value && value <= l->last_case) {
9196 warningf(&statement->base.source_position,
9197 "enumeration value '%Y' not handled in switch", declaration->symbol);
9204 * Parse a switch statement.
9206 static statement_t *parse_switch(void)
9208 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
9212 PUSH_PARENT(statement);
9215 add_anchor_token(')');
9216 expression_t *const expr = parse_expression();
9217 mark_decls_read(expr, NULL);
9218 type_t * type = skip_typeref(expr->base.type);
9219 if (is_type_integer(type)) {
9220 type = promote_integer(type);
9221 if (warning.traditional) {
9222 if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
9223 warningf(&expr->base.source_position,
9224 "'%T' switch expression not converted to '%T' in ISO C",
9228 } else if (is_type_valid(type)) {
9229 errorf(&expr->base.source_position,
9230 "switch quantity is not an integer, but '%T'", type);
9231 type = type_error_type;
9233 statement->switchs.expression = create_implicit_cast(expr, type);
9235 rem_anchor_token(')');
9237 switch_statement_t *rem = current_switch;
9238 current_switch = &statement->switchs;
9239 statement->switchs.body = parse_statement();
9240 current_switch = rem;
9242 if (warning.switch_default &&
9243 statement->switchs.default_label == NULL) {
9244 warningf(&statement->base.source_position, "switch has no default case");
9246 if (warning.switch_enum)
9247 check_enum_cases(&statement->switchs);
9253 return create_invalid_statement();
9256 static statement_t *parse_loop_body(statement_t *const loop)
9258 statement_t *const rem = current_loop;
9259 current_loop = loop;
9261 statement_t *const body = parse_statement();
9268 * Parse a while statement.
9270 static statement_t *parse_while(void)
9272 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
9276 PUSH_PARENT(statement);
9279 add_anchor_token(')');
9280 expression_t *const cond = parse_expression();
9281 statement->whiles.condition = cond;
9282 mark_decls_read(cond, NULL);
9283 rem_anchor_token(')');
9286 statement->whiles.body = parse_loop_body(statement);
9292 return create_invalid_statement();
9296 * Parse a do statement.
9298 static statement_t *parse_do(void)
9300 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
9304 PUSH_PARENT(statement);
9306 add_anchor_token(T_while);
9307 statement->do_while.body = parse_loop_body(statement);
9308 rem_anchor_token(T_while);
9312 add_anchor_token(')');
9313 expression_t *const cond = parse_expression();
9314 statement->do_while.condition = cond;
9315 mark_decls_read(cond, NULL);
9316 rem_anchor_token(')');
9324 return create_invalid_statement();
9328 * Parse a for statement.
9330 static statement_t *parse_for(void)
9332 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
9336 PUSH_PARENT(statement);
9338 size_t const top = environment_top();
9339 scope_push(&statement->fors.scope);
9342 add_anchor_token(')');
9344 if (token.type != ';') {
9345 if (is_declaration_specifier(&token, false)) {
9346 parse_declaration(record_declaration);
9348 add_anchor_token(';');
9349 expression_t *const init = parse_expression();
9350 statement->fors.initialisation = init;
9351 mark_decls_read(init, DECL_ANY);
9352 if (warning.unused_value && !expression_has_effect(init)) {
9353 warningf(&init->base.source_position,
9354 "initialisation of 'for'-statement has no effect");
9356 rem_anchor_token(';');
9363 if (token.type != ';') {
9364 add_anchor_token(';');
9365 expression_t *const cond = parse_expression();
9366 statement->fors.condition = cond;
9367 mark_decls_read(cond, NULL);
9368 rem_anchor_token(';');
9371 if (token.type != ')') {
9372 expression_t *const step = parse_expression();
9373 statement->fors.step = step;
9374 mark_decls_read(step, DECL_ANY);
9375 if (warning.unused_value && !expression_has_effect(step)) {
9376 warningf(&step->base.source_position,
9377 "step of 'for'-statement has no effect");
9380 rem_anchor_token(')');
9382 statement->fors.body = parse_loop_body(statement);
9384 assert(scope == &statement->fors.scope);
9386 environment_pop_to(top);
9393 rem_anchor_token(')');
9394 assert(scope == &statement->fors.scope);
9396 environment_pop_to(top);
9398 return create_invalid_statement();
9402 * Parse a goto statement.
9404 static statement_t *parse_goto(void)
9406 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
9409 if (GNU_MODE && token.type == '*') {
9411 expression_t *expression = parse_expression();
9412 mark_decls_read(expression, NULL);
9414 /* Argh: although documentation say the expression must be of type void *,
9415 * gcc excepts anything that can be casted into void * without error */
9416 type_t *type = expression->base.type;
9418 if (type != type_error_type) {
9419 if (!is_type_pointer(type) && !is_type_integer(type)) {
9420 errorf(&expression->base.source_position,
9421 "cannot convert to a pointer type");
9422 } else if (type != type_void_ptr) {
9423 warningf(&expression->base.source_position,
9424 "type of computed goto expression should be 'void*' not '%T'", type);
9426 expression = create_implicit_cast(expression, type_void_ptr);
9429 statement->gotos.expression = expression;
9431 if (token.type != T_IDENTIFIER) {
9433 parse_error_expected("while parsing goto", T_IDENTIFIER, '*', NULL);
9435 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
9439 symbol_t *symbol = token.v.symbol;
9442 statement->gotos.label = get_label(symbol);
9445 /* remember the goto's in a list for later checking */
9446 if (goto_last == NULL) {
9447 goto_first = &statement->gotos;
9449 goto_last->next = &statement->gotos;
9451 goto_last = &statement->gotos;
9457 return create_invalid_statement();
9461 * Parse a continue statement.
9463 static statement_t *parse_continue(void)
9465 if (current_loop == NULL) {
9466 errorf(HERE, "continue statement not within loop");
9469 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
9479 * Parse a break statement.
9481 static statement_t *parse_break(void)
9483 if (current_switch == NULL && current_loop == NULL) {
9484 errorf(HERE, "break statement not within loop or switch");
9487 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
9497 * Parse a __leave statement.
9499 static statement_t *parse_leave_statement(void)
9501 if (current_try == NULL) {
9502 errorf(HERE, "__leave statement not within __try");
9505 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
9515 * Check if a given declaration represents a local variable.
9517 static bool is_local_var_declaration(const declaration_t *declaration)
9519 switch ((storage_class_tag_t) declaration->storage_class) {
9520 case STORAGE_CLASS_AUTO:
9521 case STORAGE_CLASS_REGISTER: {
9522 const type_t *type = skip_typeref(declaration->type);
9523 if (is_type_function(type)) {
9535 * Check if a given declaration represents a variable.
9537 static bool is_var_declaration(const declaration_t *declaration)
9539 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
9542 const type_t *type = skip_typeref(declaration->type);
9543 return !is_type_function(type);
9547 * Check if a given expression represents a local variable.
9549 static bool is_local_variable(const expression_t *expression)
9551 if (expression->base.kind != EXPR_REFERENCE) {
9554 const declaration_t *declaration = expression->reference.declaration;
9555 return is_local_var_declaration(declaration);
9559 * Check if a given expression represents a local variable and
9560 * return its declaration then, else return NULL.
9562 declaration_t *expr_is_variable(const expression_t *expression)
9564 if (expression->base.kind != EXPR_REFERENCE) {
9567 declaration_t *declaration = expression->reference.declaration;
9568 if (is_var_declaration(declaration))
9574 * Parse a return statement.
9576 static statement_t *parse_return(void)
9580 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
9582 expression_t *return_value = NULL;
9583 if (token.type != ';') {
9584 return_value = parse_expression();
9585 mark_decls_read(return_value, NULL);
9588 const type_t *const func_type = current_function->type;
9589 assert(is_type_function(func_type));
9590 type_t *const return_type = skip_typeref(func_type->function.return_type);
9592 if (return_value != NULL) {
9593 type_t *return_value_type = skip_typeref(return_value->base.type);
9595 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
9596 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
9597 warningf(&statement->base.source_position,
9598 "'return' with a value, in function returning void");
9599 return_value = NULL;
9601 assign_error_t error = semantic_assign(return_type, return_value);
9602 report_assign_error(error, return_type, return_value, "'return'",
9603 &statement->base.source_position);
9604 return_value = create_implicit_cast(return_value, return_type);
9606 /* check for returning address of a local var */
9607 if (return_value != NULL &&
9608 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9609 const expression_t *expression = return_value->unary.value;
9610 if (is_local_variable(expression)) {
9611 warningf(&statement->base.source_position,
9612 "function returns address of local variable");
9616 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9617 warningf(&statement->base.source_position,
9618 "'return' without value, in function returning non-void");
9621 statement->returns.value = return_value;
9630 * Parse a declaration statement.
9632 static statement_t *parse_declaration_statement(void)
9634 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9636 declaration_t *before = last_declaration;
9638 parse_external_declaration();
9640 parse_declaration(record_declaration);
9642 if (before == NULL) {
9643 statement->declaration.declarations_begin = scope->declarations;
9645 statement->declaration.declarations_begin = before->next;
9647 statement->declaration.declarations_end = last_declaration;
9653 * Parse an expression statement, ie. expr ';'.
9655 static statement_t *parse_expression_statement(void)
9657 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9659 expression_t *const expr = parse_expression();
9660 statement->expression.expression = expr;
9661 mark_decls_read(expr, DECL_ANY);
9670 * Parse a microsoft __try { } __finally { } or
9671 * __try{ } __except() { }
9673 static statement_t *parse_ms_try_statment(void)
9675 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9678 PUSH_PARENT(statement);
9680 ms_try_statement_t *rem = current_try;
9681 current_try = &statement->ms_try;
9682 statement->ms_try.try_statement = parse_compound_statement(false);
9687 if (token.type == T___except) {
9690 add_anchor_token(')');
9691 expression_t *const expr = parse_expression();
9692 mark_decls_read(expr, NULL);
9693 type_t * type = skip_typeref(expr->base.type);
9694 if (is_type_integer(type)) {
9695 type = promote_integer(type);
9696 } else if (is_type_valid(type)) {
9697 errorf(&expr->base.source_position,
9698 "__expect expression is not an integer, but '%T'", type);
9699 type = type_error_type;
9701 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9702 rem_anchor_token(')');
9704 statement->ms_try.final_statement = parse_compound_statement(false);
9705 } else if (token.type == T__finally) {
9707 statement->ms_try.final_statement = parse_compound_statement(false);
9709 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9710 return create_invalid_statement();
9714 return create_invalid_statement();
9717 static statement_t *parse_empty_statement(void)
9719 if (warning.empty_statement) {
9720 warningf(HERE, "statement is empty");
9722 statement_t *const statement = create_empty_statement();
9727 static statement_t *parse_local_label_declaration(void) {
9728 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9732 declaration_t *begin = NULL, *end = NULL;
9735 if (token.type != T_IDENTIFIER) {
9736 parse_error_expected("while parsing local label declaration",
9737 T_IDENTIFIER, NULL);
9740 symbol_t *symbol = token.v.symbol;
9741 declaration_t *declaration = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
9742 if (declaration != NULL) {
9743 errorf(HERE, "multiple definitions of '__label__ %Y' (previous definition at %P)",
9744 symbol, &declaration->source_position);
9746 declaration = allocate_declaration_zero();
9747 declaration->namespc = NAMESPACE_LOCAL_LABEL;
9748 declaration->source_position = token.source_position;
9749 declaration->symbol = symbol;
9750 declaration->parent_scope = scope;
9751 declaration->init.statement = NULL;
9754 end->next = declaration;
9757 begin = declaration;
9759 local_label_push(declaration);
9763 if (token.type != ',')
9769 statement->declaration.declarations_begin = begin;
9770 statement->declaration.declarations_end = end;
9775 * Parse a statement.
9776 * There's also parse_statement() which additionally checks for
9777 * "statement has no effect" warnings
9779 static statement_t *intern_parse_statement(void)
9781 statement_t *statement = NULL;
9783 /* declaration or statement */
9784 add_anchor_token(';');
9785 switch (token.type) {
9786 case T_IDENTIFIER: {
9787 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9788 if (la1_type == ':') {
9789 statement = parse_label_statement();
9790 } else if (is_typedef_symbol(token.v.symbol)) {
9791 statement = parse_declaration_statement();
9792 } else switch (la1_type) {
9794 if (get_declaration(token.v.symbol, NAMESPACE_NORMAL) != NULL)
9795 goto expression_statment;
9800 statement = parse_declaration_statement();
9804 expression_statment:
9805 statement = parse_expression_statement();
9811 case T___extension__:
9812 /* This can be a prefix to a declaration or an expression statement.
9813 * We simply eat it now and parse the rest with tail recursion. */
9816 } while (token.type == T___extension__);
9817 bool old_gcc_extension = in_gcc_extension;
9818 in_gcc_extension = true;
9819 statement = parse_statement();
9820 in_gcc_extension = old_gcc_extension;
9824 statement = parse_declaration_statement();
9828 statement = parse_local_label_declaration();
9831 case ';': statement = parse_empty_statement(); break;
9832 case '{': statement = parse_compound_statement(false); break;
9833 case T___leave: statement = parse_leave_statement(); break;
9834 case T___try: statement = parse_ms_try_statment(); break;
9835 case T_asm: statement = parse_asm_statement(); break;
9836 case T_break: statement = parse_break(); break;
9837 case T_case: statement = parse_case_statement(); break;
9838 case T_continue: statement = parse_continue(); break;
9839 case T_default: statement = parse_default_statement(); break;
9840 case T_do: statement = parse_do(); break;
9841 case T_for: statement = parse_for(); break;
9842 case T_goto: statement = parse_goto(); break;
9843 case T_if: statement = parse_if(); break;
9844 case T_return: statement = parse_return(); break;
9845 case T_switch: statement = parse_switch(); break;
9846 case T_while: statement = parse_while(); break;
9856 case T_CHARACTER_CONSTANT:
9857 case T_FLOATINGPOINT:
9861 case T_STRING_LITERAL:
9862 case T_WIDE_CHARACTER_CONSTANT:
9863 case T_WIDE_STRING_LITERAL:
9864 case T___FUNCDNAME__:
9866 case T___FUNCTION__:
9867 case T___PRETTY_FUNCTION__:
9868 case T___builtin_alloca:
9869 case T___builtin_classify_type:
9870 case T___builtin_constant_p:
9871 case T___builtin_expect:
9872 case T___builtin_huge_val:
9873 case T___builtin_isgreater:
9874 case T___builtin_isgreaterequal:
9875 case T___builtin_isless:
9876 case T___builtin_islessequal:
9877 case T___builtin_islessgreater:
9878 case T___builtin_isunordered:
9879 case T___builtin_inf:
9880 case T___builtin_inff:
9881 case T___builtin_infl:
9882 case T___builtin_nan:
9883 case T___builtin_nanf:
9884 case T___builtin_nanl:
9885 case T___builtin_offsetof:
9886 case T___builtin_prefetch:
9887 case T___builtin_va_arg:
9888 case T___builtin_va_end:
9889 case T___builtin_va_start:
9893 statement = parse_expression_statement();
9897 errorf(HERE, "unexpected token %K while parsing statement", &token);
9898 statement = create_invalid_statement();
9903 rem_anchor_token(';');
9905 assert(statement != NULL
9906 && statement->base.source_position.input_name != NULL);
9912 * parse a statement and emits "statement has no effect" warning if needed
9913 * (This is really a wrapper around intern_parse_statement with check for 1
9914 * single warning. It is needed, because for statement expressions we have
9915 * to avoid the warning on the last statement)
9917 static statement_t *parse_statement(void)
9919 statement_t *statement = intern_parse_statement();
9921 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9922 expression_t *expression = statement->expression.expression;
9923 if (!expression_has_effect(expression)) {
9924 warningf(&expression->base.source_position,
9925 "statement has no effect");
9933 * Parse a compound statement.
9935 static statement_t *parse_compound_statement(bool inside_expression_statement)
9937 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
9939 PUSH_PARENT(statement);
9942 add_anchor_token('}');
9944 size_t const top = environment_top();
9945 size_t const top_local = local_label_top();
9946 scope_push(&statement->compound.scope);
9948 statement_t **anchor = &statement->compound.statements;
9949 bool only_decls_so_far = true;
9950 while (token.type != '}') {
9951 if (token.type == T_EOF) {
9952 errorf(&statement->base.source_position,
9953 "EOF while parsing compound statement");
9956 statement_t *sub_statement = intern_parse_statement();
9957 if (is_invalid_statement(sub_statement)) {
9958 /* an error occurred. if we are at an anchor, return */
9964 if (warning.declaration_after_statement) {
9965 if (sub_statement->kind != STATEMENT_DECLARATION) {
9966 only_decls_so_far = false;
9967 } else if (!only_decls_so_far) {
9968 warningf(&sub_statement->base.source_position,
9969 "ISO C90 forbids mixed declarations and code");
9973 *anchor = sub_statement;
9975 while (sub_statement->base.next != NULL)
9976 sub_statement = sub_statement->base.next;
9978 anchor = &sub_statement->base.next;
9982 /* look over all statements again to produce no effect warnings */
9983 if (warning.unused_value) {
9984 statement_t *sub_statement = statement->compound.statements;
9985 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
9986 if (sub_statement->kind != STATEMENT_EXPRESSION)
9988 /* don't emit a warning for the last expression in an expression
9989 * statement as it has always an effect */
9990 if (inside_expression_statement && sub_statement->base.next == NULL)
9993 expression_t *expression = sub_statement->expression.expression;
9994 if (!expression_has_effect(expression)) {
9995 warningf(&expression->base.source_position,
9996 "statement has no effect");
10002 rem_anchor_token('}');
10003 assert(scope == &statement->compound.scope);
10005 environment_pop_to(top);
10006 local_label_pop_to(top_local);
10013 * Initialize builtin types.
10015 static void initialize_builtin_types(void)
10017 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
10018 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
10019 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
10020 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
10021 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
10022 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
10023 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
10024 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
10026 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
10027 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
10028 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
10029 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
10031 /* const version of wchar_t */
10032 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
10033 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
10034 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
10036 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
10040 * Check for unused global static functions and variables
10042 static void check_unused_globals(void)
10044 if (!warning.unused_function && !warning.unused_variable)
10047 for (const declaration_t *decl = file_scope->declarations; decl != NULL; decl = decl->next) {
10049 decl->modifiers & DM_UNUSED ||
10050 decl->modifiers & DM_USED ||
10051 decl->storage_class != STORAGE_CLASS_STATIC)
10054 type_t *const type = decl->type;
10056 if (is_type_function(skip_typeref(type))) {
10057 if (!warning.unused_function || decl->is_inline)
10060 s = (decl->init.statement != NULL ? "defined" : "declared");
10062 if (!warning.unused_variable)
10068 warningf(&decl->source_position, "'%#T' %s but not used",
10069 type, decl->symbol, s);
10073 static void parse_global_asm(void)
10075 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
10080 statement->asms.asm_text = parse_string_literals();
10081 statement->base.next = unit->global_asm;
10082 unit->global_asm = statement;
10091 * Parse a translation unit.
10093 static void parse_translation_unit(void)
10095 add_anchor_token(T_EOF);
10098 unsigned char token_anchor_copy[T_LAST_TOKEN];
10099 memcpy(token_anchor_copy, token_anchor_set, sizeof(token_anchor_copy));
10103 bool anchor_leak = false;
10104 for (int i = 0; i != T_LAST_TOKEN; ++i) {
10105 unsigned char count = token_anchor_set[i] - token_anchor_copy[i];
10107 errorf(HERE, "Leaked anchor token %k %d times", i, count);
10108 anchor_leak = true;
10111 if (in_gcc_extension) {
10112 errorf(HERE, "Leaked __extension__");
10113 anchor_leak = true;
10120 switch (token.type) {
10123 case T___extension__:
10124 parse_external_declaration();
10128 parse_global_asm();
10132 rem_anchor_token(T_EOF);
10136 if (!strict_mode) {
10137 warningf(HERE, "stray ';' outside of function");
10144 errorf(HERE, "stray %K outside of function", &token);
10145 if (token.type == '(' || token.type == '{' || token.type == '[')
10146 eat_until_matching_token(token.type);
10156 * @return the translation unit or NULL if errors occurred.
10158 void start_parsing(void)
10160 environment_stack = NEW_ARR_F(stack_entry_t, 0);
10161 label_stack = NEW_ARR_F(stack_entry_t, 0);
10162 local_label_stack = NEW_ARR_F(stack_entry_t, 0);
10163 diagnostic_count = 0;
10167 type_set_output(stderr);
10168 ast_set_output(stderr);
10170 assert(unit == NULL);
10171 unit = allocate_ast_zero(sizeof(unit[0]));
10173 assert(file_scope == NULL);
10174 file_scope = &unit->scope;
10176 assert(scope == NULL);
10177 scope_push(&unit->scope);
10179 initialize_builtin_types();
10182 translation_unit_t *finish_parsing(void)
10184 /* do NOT use scope_pop() here, this will crash, will it by hand */
10185 assert(scope == &unit->scope);
10187 last_declaration = NULL;
10189 assert(file_scope == &unit->scope);
10190 check_unused_globals();
10193 DEL_ARR_F(environment_stack);
10194 DEL_ARR_F(label_stack);
10195 DEL_ARR_F(local_label_stack);
10197 translation_unit_t *result = unit;
10204 lookahead_bufpos = 0;
10205 for (int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
10208 parse_translation_unit();
10212 * Initialize the parser.
10214 void init_parser(void)
10216 sym_anonymous = symbol_table_insert("<anonymous>");
10218 if (c_mode & _MS) {
10219 /* add predefined symbols for extended-decl-modifier */
10220 sym_align = symbol_table_insert("align");
10221 sym_allocate = symbol_table_insert("allocate");
10222 sym_dllimport = symbol_table_insert("dllimport");
10223 sym_dllexport = symbol_table_insert("dllexport");
10224 sym_naked = symbol_table_insert("naked");
10225 sym_noinline = symbol_table_insert("noinline");
10226 sym_noreturn = symbol_table_insert("noreturn");
10227 sym_nothrow = symbol_table_insert("nothrow");
10228 sym_novtable = symbol_table_insert("novtable");
10229 sym_property = symbol_table_insert("property");
10230 sym_get = symbol_table_insert("get");
10231 sym_put = symbol_table_insert("put");
10232 sym_selectany = symbol_table_insert("selectany");
10233 sym_thread = symbol_table_insert("thread");
10234 sym_uuid = symbol_table_insert("uuid");
10235 sym_deprecated = symbol_table_insert("deprecated");
10236 sym_restrict = symbol_table_insert("restrict");
10237 sym_noalias = symbol_table_insert("noalias");
10239 memset(token_anchor_set, 0, sizeof(token_anchor_set));
10241 init_expression_parsers();
10242 obstack_init(&temp_obst);
10244 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
10245 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
10249 * Terminate the parser.
10251 void exit_parser(void)
10253 obstack_free(&temp_obst, NULL);