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(unsigned precedence);
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 /* start parsing at precedence 7 (conditional expression) */
1111 expression_t *result = parse_sub_expression(7);
1113 if (!is_constant_expression(result)) {
1114 errorf(&result->base.source_position,
1115 "expression '%E' is not constant\n", result);
1121 static expression_t *parse_assignment_expression(void)
1123 /* start parsing at precedence 2 (assignment expression) */
1124 return parse_sub_expression(2);
1127 static type_t *make_global_typedef(const char *name, type_t *type)
1129 symbol_t *const symbol = symbol_table_insert(name);
1131 declaration_t *const declaration = allocate_declaration_zero();
1132 declaration->namespc = NAMESPACE_NORMAL;
1133 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1134 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1135 declaration->type = type;
1136 declaration->symbol = symbol;
1137 declaration->source_position = builtin_source_position;
1138 declaration->implicit = true;
1140 record_declaration(declaration, false);
1142 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1143 typedef_type->typedeft.declaration = declaration;
1145 return typedef_type;
1148 static string_t parse_string_literals(void)
1150 assert(token.type == T_STRING_LITERAL);
1151 string_t result = token.v.string;
1155 while (token.type == T_STRING_LITERAL) {
1156 result = concat_strings(&result, &token.v.string);
1163 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1164 [GNU_AK_CONST] = "const",
1165 [GNU_AK_VOLATILE] = "volatile",
1166 [GNU_AK_CDECL] = "cdecl",
1167 [GNU_AK_STDCALL] = "stdcall",
1168 [GNU_AK_FASTCALL] = "fastcall",
1169 [GNU_AK_DEPRECATED] = "deprecated",
1170 [GNU_AK_NOINLINE] = "noinline",
1171 [GNU_AK_NORETURN] = "noreturn",
1172 [GNU_AK_NAKED] = "naked",
1173 [GNU_AK_PURE] = "pure",
1174 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1175 [GNU_AK_MALLOC] = "malloc",
1176 [GNU_AK_WEAK] = "weak",
1177 [GNU_AK_CONSTRUCTOR] = "constructor",
1178 [GNU_AK_DESTRUCTOR] = "destructor",
1179 [GNU_AK_NOTHROW] = "nothrow",
1180 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1181 [GNU_AK_COMMON] = "common",
1182 [GNU_AK_NOCOMMON] = "nocommon",
1183 [GNU_AK_PACKED] = "packed",
1184 [GNU_AK_SHARED] = "shared",
1185 [GNU_AK_NOTSHARED] = "notshared",
1186 [GNU_AK_USED] = "used",
1187 [GNU_AK_UNUSED] = "unused",
1188 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1189 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1190 [GNU_AK_LONGCALL] = "longcall",
1191 [GNU_AK_SHORTCALL] = "shortcall",
1192 [GNU_AK_LONG_CALL] = "long_call",
1193 [GNU_AK_SHORT_CALL] = "short_call",
1194 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1195 [GNU_AK_INTERRUPT] = "interrupt",
1196 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1197 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1198 [GNU_AK_NESTING] = "nesting",
1199 [GNU_AK_NEAR] = "near",
1200 [GNU_AK_FAR] = "far",
1201 [GNU_AK_SIGNAL] = "signal",
1202 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1203 [GNU_AK_TINY_DATA] = "tiny_data",
1204 [GNU_AK_SAVEALL] = "saveall",
1205 [GNU_AK_FLATTEN] = "flatten",
1206 [GNU_AK_SSEREGPARM] = "sseregparm",
1207 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1208 [GNU_AK_RETURN_TWICE] = "return_twice",
1209 [GNU_AK_MAY_ALIAS] = "may_alias",
1210 [GNU_AK_MS_STRUCT] = "ms_struct",
1211 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1212 [GNU_AK_DLLIMPORT] = "dllimport",
1213 [GNU_AK_DLLEXPORT] = "dllexport",
1214 [GNU_AK_ALIGNED] = "aligned",
1215 [GNU_AK_ALIAS] = "alias",
1216 [GNU_AK_SECTION] = "section",
1217 [GNU_AK_FORMAT] = "format",
1218 [GNU_AK_FORMAT_ARG] = "format_arg",
1219 [GNU_AK_WEAKREF] = "weakref",
1220 [GNU_AK_NONNULL] = "nonnull",
1221 [GNU_AK_TLS_MODEL] = "tls_model",
1222 [GNU_AK_VISIBILITY] = "visibility",
1223 [GNU_AK_REGPARM] = "regparm",
1224 [GNU_AK_MODE] = "mode",
1225 [GNU_AK_MODEL] = "model",
1226 [GNU_AK_TRAP_EXIT] = "trap_exit",
1227 [GNU_AK_SP_SWITCH] = "sp_switch",
1228 [GNU_AK_SENTINEL] = "sentinel"
1232 * compare two string, ignoring double underscores on the second.
1234 static int strcmp_underscore(const char *s1, const char *s2)
1236 if (s2[0] == '_' && s2[1] == '_') {
1237 size_t len2 = strlen(s2);
1238 size_t len1 = strlen(s1);
1239 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1240 return strncmp(s1, s2+2, len2-4);
1244 return strcmp(s1, s2);
1248 * Allocate a new gnu temporal attribute.
1250 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1252 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1253 attribute->kind = kind;
1254 attribute->next = NULL;
1255 attribute->invalid = false;
1256 attribute->have_arguments = false;
1262 * parse one constant expression argument.
1264 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1266 expression_t *expression;
1267 add_anchor_token(')');
1268 expression = parse_constant_expression();
1269 rem_anchor_token(')');
1271 attribute->u.argument = fold_constant(expression);
1274 attribute->invalid = true;
1278 * parse a list of constant expressions arguments.
1280 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1282 argument_list_t **list = &attribute->u.arguments;
1283 argument_list_t *entry;
1284 expression_t *expression;
1285 add_anchor_token(')');
1286 add_anchor_token(',');
1288 expression = parse_constant_expression();
1289 entry = obstack_alloc(&temp_obst, sizeof(entry));
1290 entry->argument = fold_constant(expression);
1293 list = &entry->next;
1294 if (token.type != ',')
1298 rem_anchor_token(',');
1299 rem_anchor_token(')');
1303 attribute->invalid = true;
1307 * parse one string literal argument.
1309 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1312 add_anchor_token('(');
1313 if (token.type != T_STRING_LITERAL) {
1314 parse_error_expected("while parsing attribute directive",
1315 T_STRING_LITERAL, NULL);
1318 *string = parse_string_literals();
1319 rem_anchor_token('(');
1323 attribute->invalid = true;
1327 * parse one tls model.
1329 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1331 static const char *const tls_models[] = {
1337 string_t string = { NULL, 0 };
1338 parse_gnu_attribute_string_arg(attribute, &string);
1339 if (string.begin != NULL) {
1340 for(size_t i = 0; i < 4; ++i) {
1341 if (strcmp(tls_models[i], string.begin) == 0) {
1342 attribute->u.value = i;
1346 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1348 attribute->invalid = true;
1352 * parse one tls model.
1354 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1356 static const char *const visibilities[] = {
1362 string_t string = { NULL, 0 };
1363 parse_gnu_attribute_string_arg(attribute, &string);
1364 if (string.begin != NULL) {
1365 for(size_t i = 0; i < 4; ++i) {
1366 if (strcmp(visibilities[i], string.begin) == 0) {
1367 attribute->u.value = i;
1371 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1373 attribute->invalid = true;
1377 * parse one (code) model.
1379 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1381 static const char *const visibilities[] = {
1386 string_t string = { NULL, 0 };
1387 parse_gnu_attribute_string_arg(attribute, &string);
1388 if (string.begin != NULL) {
1389 for(int i = 0; i < 3; ++i) {
1390 if (strcmp(visibilities[i], string.begin) == 0) {
1391 attribute->u.value = i;
1395 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1397 attribute->invalid = true;
1400 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1402 /* TODO: find out what is allowed here... */
1404 /* at least: byte, word, pointer, list of machine modes
1405 * __XXX___ is interpreted as XXX */
1406 add_anchor_token(')');
1408 if (token.type != T_IDENTIFIER) {
1409 expect(T_IDENTIFIER);
1412 /* This isn't really correct, the backend should provide a list of machine
1413 * specific modes (according to gcc philosophy that is...) */
1414 const char *symbol_str = token.v.symbol->string;
1415 if (strcmp_underscore("QI", symbol_str) == 0 ||
1416 strcmp_underscore("byte", symbol_str) == 0) {
1417 attribute->u.akind = ATOMIC_TYPE_CHAR;
1418 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1419 attribute->u.akind = ATOMIC_TYPE_SHORT;
1420 } else if (strcmp_underscore("SI", symbol_str) == 0
1421 || strcmp_underscore("word", symbol_str) == 0
1422 || strcmp_underscore("pointer", symbol_str) == 0) {
1423 attribute->u.akind = ATOMIC_TYPE_INT;
1424 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1425 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1427 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1428 attribute->invalid = true;
1432 rem_anchor_token(')');
1436 attribute->invalid = true;
1440 * parse one interrupt argument.
1442 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1444 static const char *const interrupts[] = {
1451 string_t string = { NULL, 0 };
1452 parse_gnu_attribute_string_arg(attribute, &string);
1453 if (string.begin != NULL) {
1454 for(size_t i = 0; i < 5; ++i) {
1455 if (strcmp(interrupts[i], string.begin) == 0) {
1456 attribute->u.value = i;
1460 errorf(HERE, "'%s' is not an interrupt", string.begin);
1462 attribute->invalid = true;
1466 * parse ( identifier, const expression, const expression )
1468 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1470 static const char *const format_names[] = {
1478 if (token.type != T_IDENTIFIER) {
1479 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1482 const char *name = token.v.symbol->string;
1483 for(i = 0; i < 4; ++i) {
1484 if (strcmp_underscore(format_names[i], name) == 0)
1488 if (warning.attribute)
1489 warningf(HERE, "'%s' is an unrecognized format function type", name);
1494 add_anchor_token(')');
1495 add_anchor_token(',');
1496 parse_constant_expression();
1497 rem_anchor_token(',');
1498 rem_anchor_token(')');
1501 add_anchor_token(')');
1502 parse_constant_expression();
1503 rem_anchor_token(')');
1507 attribute->u.value = true;
1510 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1512 if (!attribute->have_arguments)
1515 /* should have no arguments */
1516 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1517 eat_until_matching_token('(');
1518 /* we have already consumed '(', so we stop before ')', eat it */
1520 attribute->invalid = true;
1524 * Parse one GNU attribute.
1526 * Note that attribute names can be specified WITH or WITHOUT
1527 * double underscores, ie const or __const__.
1529 * The following attributes are parsed without arguments
1554 * no_instrument_function
1555 * warn_unused_result
1572 * externally_visible
1580 * The following attributes are parsed with arguments
1581 * aligned( const expression )
1582 * alias( string literal )
1583 * section( string literal )
1584 * format( identifier, const expression, const expression )
1585 * format_arg( const expression )
1586 * tls_model( string literal )
1587 * visibility( string literal )
1588 * regparm( const expression )
1589 * model( string leteral )
1590 * trap_exit( const expression )
1591 * sp_switch( string literal )
1593 * The following attributes might have arguments
1594 * weak_ref( string literal )
1595 * non_null( const expression // ',' )
1596 * interrupt( string literal )
1597 * sentinel( constant expression )
1599 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1601 gnu_attribute_t *head = *attributes;
1602 gnu_attribute_t *last = *attributes;
1603 decl_modifiers_t modifiers = 0;
1604 gnu_attribute_t *attribute;
1606 eat(T___attribute__);
1610 if (token.type != ')') {
1611 /* find the end of the list */
1613 while (last->next != NULL)
1617 /* non-empty attribute list */
1620 if (token.type == T_const) {
1622 } else if (token.type == T_volatile) {
1624 } else if (token.type == T_cdecl) {
1625 /* __attribute__((cdecl)), WITH ms mode */
1627 } else if (token.type == T_IDENTIFIER) {
1628 const symbol_t *sym = token.v.symbol;
1631 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1638 for(i = 0; i < GNU_AK_LAST; ++i) {
1639 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1642 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1645 if (kind == GNU_AK_LAST) {
1646 if (warning.attribute)
1647 warningf(HERE, "'%s' attribute directive ignored", name);
1649 /* skip possible arguments */
1650 if (token.type == '(') {
1651 eat_until_matching_token(')');
1654 /* check for arguments */
1655 attribute = allocate_gnu_attribute(kind);
1656 if (token.type == '(') {
1658 if (token.type == ')') {
1659 /* empty args are allowed */
1662 attribute->have_arguments = true;
1666 case GNU_AK_VOLATILE:
1671 case GNU_AK_NOCOMMON:
1673 case GNU_AK_NOTSHARED:
1674 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1675 case GNU_AK_WARN_UNUSED_RESULT:
1676 case GNU_AK_LONGCALL:
1677 case GNU_AK_SHORTCALL:
1678 case GNU_AK_LONG_CALL:
1679 case GNU_AK_SHORT_CALL:
1680 case GNU_AK_FUNCTION_VECTOR:
1681 case GNU_AK_INTERRUPT_HANDLER:
1682 case GNU_AK_NMI_HANDLER:
1683 case GNU_AK_NESTING:
1687 case GNU_AK_EIGTHBIT_DATA:
1688 case GNU_AK_TINY_DATA:
1689 case GNU_AK_SAVEALL:
1690 case GNU_AK_FLATTEN:
1691 case GNU_AK_SSEREGPARM:
1692 case GNU_AK_EXTERNALLY_VISIBLE:
1693 case GNU_AK_RETURN_TWICE:
1694 case GNU_AK_MAY_ALIAS:
1695 case GNU_AK_MS_STRUCT:
1696 case GNU_AK_GCC_STRUCT:
1699 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1700 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1701 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1702 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1703 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1704 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1705 case GNU_AK_CONST: modifiers |= DM_CONST; goto no_arg;
1706 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1707 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1708 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1709 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1710 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1711 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1712 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1713 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1714 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1715 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1716 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1718 case GNU_AK_ALIGNED:
1719 /* __align__ may be used without an argument */
1720 if (attribute->have_arguments) {
1721 parse_gnu_attribute_const_arg(attribute);
1725 case GNU_AK_FORMAT_ARG:
1726 case GNU_AK_REGPARM:
1727 case GNU_AK_TRAP_EXIT:
1728 if (!attribute->have_arguments) {
1729 /* should have arguments */
1730 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1731 attribute->invalid = true;
1733 parse_gnu_attribute_const_arg(attribute);
1736 case GNU_AK_SECTION:
1737 case GNU_AK_SP_SWITCH:
1738 if (!attribute->have_arguments) {
1739 /* should have arguments */
1740 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1741 attribute->invalid = true;
1743 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1746 if (!attribute->have_arguments) {
1747 /* should have arguments */
1748 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1749 attribute->invalid = true;
1751 parse_gnu_attribute_format_args(attribute);
1753 case GNU_AK_WEAKREF:
1754 /* may have one string argument */
1755 if (attribute->have_arguments)
1756 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1758 case GNU_AK_NONNULL:
1759 if (attribute->have_arguments)
1760 parse_gnu_attribute_const_arg_list(attribute);
1762 case GNU_AK_TLS_MODEL:
1763 if (!attribute->have_arguments) {
1764 /* should have arguments */
1765 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1767 parse_gnu_attribute_tls_model_arg(attribute);
1769 case GNU_AK_VISIBILITY:
1770 if (!attribute->have_arguments) {
1771 /* should have arguments */
1772 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1774 parse_gnu_attribute_visibility_arg(attribute);
1777 if (!attribute->have_arguments) {
1778 /* should have arguments */
1779 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1781 parse_gnu_attribute_model_arg(attribute);
1785 if (!attribute->have_arguments) {
1786 /* should have arguments */
1787 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1789 parse_gnu_attribute_mode_arg(attribute);
1792 case GNU_AK_INTERRUPT:
1793 /* may have one string argument */
1794 if (attribute->have_arguments)
1795 parse_gnu_attribute_interrupt_arg(attribute);
1797 case GNU_AK_SENTINEL:
1798 /* may have one string argument */
1799 if (attribute->have_arguments)
1800 parse_gnu_attribute_const_arg(attribute);
1803 /* already handled */
1807 check_no_argument(attribute, name);
1810 if (attribute != NULL) {
1812 last->next = attribute;
1815 head = last = attribute;
1819 if (token.type != ',')
1833 * Parse GNU attributes.
1835 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1837 decl_modifiers_t modifiers = 0;
1840 switch(token.type) {
1841 case T___attribute__:
1842 modifiers |= parse_gnu_attribute(attributes);
1848 if (token.type != T_STRING_LITERAL) {
1849 parse_error_expected("while parsing assembler attribute",
1850 T_STRING_LITERAL, NULL);
1851 eat_until_matching_token('(');
1854 parse_string_literals();
1859 case T_cdecl: modifiers |= DM_CDECL; break;
1860 case T__fastcall: modifiers |= DM_FASTCALL; break;
1861 case T__stdcall: modifiers |= DM_STDCALL; break;
1864 /* TODO record modifier */
1865 warningf(HERE, "Ignoring declaration modifier %K", &token);
1869 default: return modifiers;
1876 static void mark_decls_read(expression_t *expr, declaration_t *lhs_decl);
1878 static declaration_t *determine_lhs_decl(expression_t *const expr, declaration_t *lhs_decl)
1880 switch (expr->kind) {
1881 case EXPR_REFERENCE: {
1882 declaration_t *const decl = expr->reference.declaration;
1886 case EXPR_ARRAY_ACCESS: {
1887 expression_t *const ref = expr->array_access.array_ref;
1888 declaration_t * decl = NULL;
1889 if (is_type_array(revert_automatic_type_conversion(ref))) {
1890 decl = determine_lhs_decl(ref, lhs_decl);
1893 mark_decls_read(expr->select.compound, lhs_decl);
1895 mark_decls_read(expr->array_access.index, lhs_decl);
1900 if (is_type_compound(skip_typeref(expr->base.type))) {
1901 return determine_lhs_decl(expr->select.compound, lhs_decl);
1903 mark_decls_read(expr->select.compound, lhs_decl);
1908 case EXPR_UNARY_DEREFERENCE: {
1909 expression_t *const val = expr->unary.value;
1910 if (val->kind == EXPR_UNARY_TAKE_ADDRESS) {
1912 return determine_lhs_decl(val->unary.value, lhs_decl);
1914 mark_decls_read(val, NULL);
1920 mark_decls_read(expr, NULL);
1925 #define DECL_ANY ((declaration_t*)-1)
1928 * Mark declarations, which are read. This is used to deted variables, which
1932 * x is not marked as "read", because it is only read to calculate its own new
1936 * x and y are not detected as "not read", because multiple variables are
1939 static void mark_decls_read(expression_t *const expr, declaration_t *lhs_decl)
1941 switch (expr->kind) {
1942 case EXPR_REFERENCE: {
1943 declaration_t *const decl = expr->reference.declaration;
1944 if (lhs_decl != decl && lhs_decl != DECL_ANY)
1950 // TODO respect pure/const
1951 mark_decls_read(expr->call.function, NULL);
1952 for (call_argument_t *arg = expr->call.arguments; arg != NULL; arg = arg->next) {
1953 mark_decls_read(arg->expression, NULL);
1957 case EXPR_CONDITIONAL:
1958 // TODO lhs_decl should depend on whether true/false have an effect
1959 mark_decls_read(expr->conditional.condition, NULL);
1960 if (expr->conditional.true_expression != NULL)
1961 mark_decls_read(expr->conditional.true_expression, lhs_decl);
1962 mark_decls_read(expr->conditional.false_expression, lhs_decl);
1966 if (lhs_decl == DECL_ANY && !is_type_compound(skip_typeref(expr->base.type)))
1968 mark_decls_read(expr->select.compound, lhs_decl);
1971 case EXPR_ARRAY_ACCESS: {
1972 expression_t *const ref = expr->array_access.array_ref;
1973 mark_decls_read(ref, lhs_decl);
1974 lhs_decl = determine_lhs_decl(ref, lhs_decl);
1975 mark_decls_read(expr->array_access.index, lhs_decl);
1980 mark_decls_read(expr->va_arge.ap, lhs_decl);
1983 case EXPR_UNARY_CAST:
1984 /* Special case: Use void cast to mark a variable as "read" */
1985 if (is_type_atomic(skip_typeref(expr->base.type), ATOMIC_TYPE_VOID))
1989 case EXPR_UNARY_DEREFERENCE:
1990 if (lhs_decl == DECL_ANY)
1994 case EXPR_UNARY_NEGATE:
1995 case EXPR_UNARY_PLUS:
1996 case EXPR_UNARY_BITWISE_NEGATE:
1997 case EXPR_UNARY_NOT:
1998 case EXPR_UNARY_TAKE_ADDRESS:
1999 case EXPR_UNARY_POSTFIX_INCREMENT:
2000 case EXPR_UNARY_POSTFIX_DECREMENT:
2001 case EXPR_UNARY_PREFIX_INCREMENT:
2002 case EXPR_UNARY_PREFIX_DECREMENT:
2003 case EXPR_UNARY_CAST_IMPLICIT:
2004 case EXPR_UNARY_ASSUME:
2006 mark_decls_read(expr->unary.value, lhs_decl);
2009 case EXPR_BINARY_ADD:
2010 case EXPR_BINARY_SUB:
2011 case EXPR_BINARY_MUL:
2012 case EXPR_BINARY_DIV:
2013 case EXPR_BINARY_MOD:
2014 case EXPR_BINARY_EQUAL:
2015 case EXPR_BINARY_NOTEQUAL:
2016 case EXPR_BINARY_LESS:
2017 case EXPR_BINARY_LESSEQUAL:
2018 case EXPR_BINARY_GREATER:
2019 case EXPR_BINARY_GREATEREQUAL:
2020 case EXPR_BINARY_BITWISE_AND:
2021 case EXPR_BINARY_BITWISE_OR:
2022 case EXPR_BINARY_BITWISE_XOR:
2023 case EXPR_BINARY_LOGICAL_AND:
2024 case EXPR_BINARY_LOGICAL_OR:
2025 case EXPR_BINARY_SHIFTLEFT:
2026 case EXPR_BINARY_SHIFTRIGHT:
2027 case EXPR_BINARY_COMMA:
2028 case EXPR_BINARY_ISGREATER:
2029 case EXPR_BINARY_ISGREATEREQUAL:
2030 case EXPR_BINARY_ISLESS:
2031 case EXPR_BINARY_ISLESSEQUAL:
2032 case EXPR_BINARY_ISLESSGREATER:
2033 case EXPR_BINARY_ISUNORDERED:
2034 mark_decls_read(expr->binary.left, lhs_decl);
2035 mark_decls_read(expr->binary.right, lhs_decl);
2038 case EXPR_BINARY_ASSIGN:
2039 case EXPR_BINARY_MUL_ASSIGN:
2040 case EXPR_BINARY_DIV_ASSIGN:
2041 case EXPR_BINARY_MOD_ASSIGN:
2042 case EXPR_BINARY_ADD_ASSIGN:
2043 case EXPR_BINARY_SUB_ASSIGN:
2044 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2045 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2046 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2047 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2048 case EXPR_BINARY_BITWISE_OR_ASSIGN: {
2049 if (lhs_decl == DECL_ANY)
2051 lhs_decl = determine_lhs_decl(expr->binary.left, lhs_decl);
2052 mark_decls_read(expr->binary.right, lhs_decl);
2057 determine_lhs_decl(expr->va_starte.ap, lhs_decl);
2063 case EXPR_CHARACTER_CONSTANT:
2064 case EXPR_WIDE_CHARACTER_CONSTANT:
2065 case EXPR_STRING_LITERAL:
2066 case EXPR_WIDE_STRING_LITERAL:
2067 case EXPR_COMPOUND_LITERAL: // TODO init?
2069 case EXPR_CLASSIFY_TYPE:
2072 case EXPR_BUILTIN_SYMBOL:
2073 case EXPR_BUILTIN_CONSTANT_P:
2074 case EXPR_BUILTIN_PREFETCH:
2076 case EXPR_STATEMENT: // TODO
2077 case EXPR_LABEL_ADDRESS:
2078 case EXPR_BINARY_BUILTIN_EXPECT:
2082 panic("unhandled expression");
2085 static designator_t *parse_designation(void)
2087 designator_t *result = NULL;
2088 designator_t *last = NULL;
2091 designator_t *designator;
2092 switch(token.type) {
2094 designator = allocate_ast_zero(sizeof(designator[0]));
2095 designator->source_position = token.source_position;
2097 add_anchor_token(']');
2098 designator->array_index = parse_constant_expression();
2099 rem_anchor_token(']');
2103 designator = allocate_ast_zero(sizeof(designator[0]));
2104 designator->source_position = token.source_position;
2106 if (token.type != T_IDENTIFIER) {
2107 parse_error_expected("while parsing designator",
2108 T_IDENTIFIER, NULL);
2111 designator->symbol = token.v.symbol;
2119 assert(designator != NULL);
2121 last->next = designator;
2123 result = designator;
2131 static initializer_t *initializer_from_string(array_type_t *type,
2132 const string_t *const string)
2134 /* TODO: check len vs. size of array type */
2137 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
2138 initializer->string.string = *string;
2143 static initializer_t *initializer_from_wide_string(array_type_t *const type,
2144 wide_string_t *const string)
2146 /* TODO: check len vs. size of array type */
2149 initializer_t *const initializer =
2150 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
2151 initializer->wide_string.string = *string;
2157 * Build an initializer from a given expression.
2159 static initializer_t *initializer_from_expression(type_t *orig_type,
2160 expression_t *expression)
2162 /* TODO check that expression is a constant expression */
2164 /* § 6.7.8.14/15 char array may be initialized by string literals */
2165 type_t *type = skip_typeref(orig_type);
2166 type_t *expr_type_orig = expression->base.type;
2167 type_t *expr_type = skip_typeref(expr_type_orig);
2168 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
2169 array_type_t *const array_type = &type->array;
2170 type_t *const element_type = skip_typeref(array_type->element_type);
2172 if (element_type->kind == TYPE_ATOMIC) {
2173 atomic_type_kind_t akind = element_type->atomic.akind;
2174 switch (expression->kind) {
2175 case EXPR_STRING_LITERAL:
2176 if (akind == ATOMIC_TYPE_CHAR
2177 || akind == ATOMIC_TYPE_SCHAR
2178 || akind == ATOMIC_TYPE_UCHAR) {
2179 return initializer_from_string(array_type,
2180 &expression->string.value);
2183 case EXPR_WIDE_STRING_LITERAL: {
2184 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
2185 if (get_unqualified_type(element_type) == bare_wchar_type) {
2186 return initializer_from_wide_string(array_type,
2187 &expression->wide_string.value);
2197 assign_error_t error = semantic_assign(type, expression);
2198 if (error == ASSIGN_ERROR_INCOMPATIBLE)
2200 report_assign_error(error, type, expression, "initializer",
2201 &expression->base.source_position);
2203 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
2205 if (type->kind == TYPE_BITFIELD) {
2206 type = type->bitfield.base_type;
2209 result->value.value = create_implicit_cast(expression, type);
2215 * Checks if a given expression can be used as an constant initializer.
2217 static bool is_initializer_constant(const expression_t *expression)
2219 return is_constant_expression(expression)
2220 || is_address_constant(expression);
2224 * Parses an scalar initializer.
2226 * § 6.7.8.11; eat {} without warning
2228 static initializer_t *parse_scalar_initializer(type_t *type,
2229 bool must_be_constant)
2231 /* there might be extra {} hierarchies */
2233 if (token.type == '{') {
2234 warningf(HERE, "extra curly braces around scalar initializer");
2238 } while (token.type == '{');
2241 expression_t *expression = parse_assignment_expression();
2242 mark_decls_read(expression, NULL);
2243 if (must_be_constant && !is_initializer_constant(expression)) {
2244 errorf(&expression->base.source_position,
2245 "Initialisation expression '%E' is not constant\n",
2249 initializer_t *initializer = initializer_from_expression(type, expression);
2251 if (initializer == NULL) {
2252 errorf(&expression->base.source_position,
2253 "expression '%E' (type '%T') doesn't match expected type '%T'",
2254 expression, expression->base.type, type);
2259 bool additional_warning_displayed = false;
2260 while (braces > 0) {
2261 if (token.type == ',') {
2264 if (token.type != '}') {
2265 if (!additional_warning_displayed) {
2266 warningf(HERE, "additional elements in scalar initializer");
2267 additional_warning_displayed = true;
2278 * An entry in the type path.
2280 typedef struct type_path_entry_t type_path_entry_t;
2281 struct type_path_entry_t {
2282 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2284 size_t index; /**< For array types: the current index. */
2285 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2290 * A type path expression a position inside compound or array types.
2292 typedef struct type_path_t type_path_t;
2293 struct type_path_t {
2294 type_path_entry_t *path; /**< An flexible array containing the current path. */
2295 type_t *top_type; /**< type of the element the path points */
2296 size_t max_index; /**< largest index in outermost array */
2300 * Prints a type path for debugging.
2302 static __attribute__((unused)) void debug_print_type_path(
2303 const type_path_t *path)
2305 size_t len = ARR_LEN(path->path);
2307 for(size_t i = 0; i < len; ++i) {
2308 const type_path_entry_t *entry = & path->path[i];
2310 type_t *type = skip_typeref(entry->type);
2311 if (is_type_compound(type)) {
2312 /* in gcc mode structs can have no members */
2313 if (entry->v.compound_entry == NULL) {
2317 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2318 } else if (is_type_array(type)) {
2319 fprintf(stderr, "[%zu]", entry->v.index);
2321 fprintf(stderr, "-INVALID-");
2324 if (path->top_type != NULL) {
2325 fprintf(stderr, " (");
2326 print_type(path->top_type);
2327 fprintf(stderr, ")");
2332 * Return the top type path entry, ie. in a path
2333 * (type).a.b returns the b.
2335 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2337 size_t len = ARR_LEN(path->path);
2339 return &path->path[len-1];
2343 * Enlarge the type path by an (empty) element.
2345 static type_path_entry_t *append_to_type_path(type_path_t *path)
2347 size_t len = ARR_LEN(path->path);
2348 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2350 type_path_entry_t *result = & path->path[len];
2351 memset(result, 0, sizeof(result[0]));
2356 * Descending into a sub-type. Enter the scope of the current
2359 static void descend_into_subtype(type_path_t *path)
2361 type_t *orig_top_type = path->top_type;
2362 type_t *top_type = skip_typeref(orig_top_type);
2364 type_path_entry_t *top = append_to_type_path(path);
2365 top->type = top_type;
2367 if (is_type_compound(top_type)) {
2368 declaration_t *declaration = top_type->compound.declaration;
2369 declaration_t *entry = declaration->scope.declarations;
2370 top->v.compound_entry = entry;
2372 if (entry != NULL) {
2373 path->top_type = entry->type;
2375 path->top_type = NULL;
2377 } else if (is_type_array(top_type)) {
2379 path->top_type = top_type->array.element_type;
2381 assert(!is_type_valid(top_type));
2386 * Pop an entry from the given type path, ie. returning from
2387 * (type).a.b to (type).a
2389 static void ascend_from_subtype(type_path_t *path)
2391 type_path_entry_t *top = get_type_path_top(path);
2393 path->top_type = top->type;
2395 size_t len = ARR_LEN(path->path);
2396 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2400 * Pop entries from the given type path until the given
2401 * path level is reached.
2403 static void ascend_to(type_path_t *path, size_t top_path_level)
2405 size_t len = ARR_LEN(path->path);
2407 while (len > top_path_level) {
2408 ascend_from_subtype(path);
2409 len = ARR_LEN(path->path);
2413 static bool walk_designator(type_path_t *path, const designator_t *designator,
2414 bool used_in_offsetof)
2416 for( ; designator != NULL; designator = designator->next) {
2417 type_path_entry_t *top = get_type_path_top(path);
2418 type_t *orig_type = top->type;
2420 type_t *type = skip_typeref(orig_type);
2422 if (designator->symbol != NULL) {
2423 symbol_t *symbol = designator->symbol;
2424 if (!is_type_compound(type)) {
2425 if (is_type_valid(type)) {
2426 errorf(&designator->source_position,
2427 "'.%Y' designator used for non-compound type '%T'",
2431 top->type = type_error_type;
2432 top->v.compound_entry = NULL;
2433 orig_type = type_error_type;
2435 declaration_t *declaration = type->compound.declaration;
2436 declaration_t *iter = declaration->scope.declarations;
2437 for( ; iter != NULL; iter = iter->next) {
2438 if (iter->symbol == symbol) {
2443 errorf(&designator->source_position,
2444 "'%T' has no member named '%Y'", orig_type, symbol);
2447 if (used_in_offsetof) {
2448 type_t *real_type = skip_typeref(iter->type);
2449 if (real_type->kind == TYPE_BITFIELD) {
2450 errorf(&designator->source_position,
2451 "offsetof designator '%Y' may not specify bitfield",
2457 top->type = orig_type;
2458 top->v.compound_entry = iter;
2459 orig_type = iter->type;
2462 expression_t *array_index = designator->array_index;
2463 assert(designator->array_index != NULL);
2465 if (!is_type_array(type)) {
2466 if (is_type_valid(type)) {
2467 errorf(&designator->source_position,
2468 "[%E] designator used for non-array type '%T'",
2469 array_index, orig_type);
2474 long index = fold_constant(array_index);
2475 if (!used_in_offsetof) {
2477 errorf(&designator->source_position,
2478 "array index [%E] must be positive", array_index);
2479 } else if (type->array.size_constant) {
2480 long array_size = type->array.size;
2481 if (index >= array_size) {
2482 errorf(&designator->source_position,
2483 "designator [%E] (%d) exceeds array size %d",
2484 array_index, index, array_size);
2489 top->type = orig_type;
2490 top->v.index = (size_t) index;
2491 orig_type = type->array.element_type;
2493 path->top_type = orig_type;
2495 if (designator->next != NULL) {
2496 descend_into_subtype(path);
2505 static void advance_current_object(type_path_t *path, size_t top_path_level)
2507 type_path_entry_t *top = get_type_path_top(path);
2509 type_t *type = skip_typeref(top->type);
2510 if (is_type_union(type)) {
2511 /* in unions only the first element is initialized */
2512 top->v.compound_entry = NULL;
2513 } else if (is_type_struct(type)) {
2514 declaration_t *entry = top->v.compound_entry;
2516 entry = entry->next;
2517 top->v.compound_entry = entry;
2518 if (entry != NULL) {
2519 path->top_type = entry->type;
2522 } else if (is_type_array(type)) {
2523 assert(is_type_array(type));
2527 if (!type->array.size_constant || top->v.index < type->array.size) {
2531 assert(!is_type_valid(type));
2535 /* we're past the last member of the current sub-aggregate, try if we
2536 * can ascend in the type hierarchy and continue with another subobject */
2537 size_t len = ARR_LEN(path->path);
2539 if (len > top_path_level) {
2540 ascend_from_subtype(path);
2541 advance_current_object(path, top_path_level);
2543 path->top_type = NULL;
2548 * skip until token is found.
2550 static void skip_until(int type)
2552 while (token.type != type) {
2553 if (token.type == T_EOF)
2560 * skip any {...} blocks until a closing bracket is reached.
2562 static void skip_initializers(void)
2564 if (token.type == '{')
2567 while (token.type != '}') {
2568 if (token.type == T_EOF)
2570 if (token.type == '{') {
2578 static initializer_t *create_empty_initializer(void)
2580 static initializer_t empty_initializer
2581 = { .list = { { INITIALIZER_LIST }, 0 } };
2582 return &empty_initializer;
2586 * Parse a part of an initialiser for a struct or union,
2588 static initializer_t *parse_sub_initializer(type_path_t *path,
2589 type_t *outer_type, size_t top_path_level,
2590 parse_initializer_env_t *env)
2592 if (token.type == '}') {
2593 /* empty initializer */
2594 return create_empty_initializer();
2597 type_t *orig_type = path->top_type;
2598 type_t *type = NULL;
2600 if (orig_type == NULL) {
2601 /* We are initializing an empty compound. */
2603 type = skip_typeref(orig_type);
2606 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2609 designator_t *designator = NULL;
2610 if (token.type == '.' || token.type == '[') {
2611 designator = parse_designation();
2612 goto finish_designator;
2613 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2614 /* GNU-style designator ("identifier: value") */
2615 designator = allocate_ast_zero(sizeof(designator[0]));
2616 designator->source_position = token.source_position;
2617 designator->symbol = token.v.symbol;
2622 /* reset path to toplevel, evaluate designator from there */
2623 ascend_to(path, top_path_level);
2624 if (!walk_designator(path, designator, false)) {
2625 /* can't continue after designation error */
2629 initializer_t *designator_initializer
2630 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2631 designator_initializer->designator.designator = designator;
2632 ARR_APP1(initializer_t*, initializers, designator_initializer);
2634 orig_type = path->top_type;
2635 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2640 if (token.type == '{') {
2641 if (type != NULL && is_type_scalar(type)) {
2642 sub = parse_scalar_initializer(type, env->must_be_constant);
2646 if (env->declaration != NULL) {
2647 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2648 env->declaration->symbol);
2650 errorf(HERE, "extra brace group at end of initializer");
2653 descend_into_subtype(path);
2655 add_anchor_token('}');
2656 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2658 rem_anchor_token('}');
2661 ascend_from_subtype(path);
2665 goto error_parse_next;
2669 /* must be an expression */
2670 expression_t *expression = parse_assignment_expression();
2672 if (env->must_be_constant && !is_initializer_constant(expression)) {
2673 errorf(&expression->base.source_position,
2674 "Initialisation expression '%E' is not constant\n",
2679 /* we are already outside, ... */
2680 type_t *const outer_type_skip = skip_typeref(outer_type);
2681 if (is_type_compound(outer_type_skip) &&
2682 !outer_type_skip->compound.declaration->init.complete) {
2683 goto error_parse_next;
2688 /* handle { "string" } special case */
2689 if ((expression->kind == EXPR_STRING_LITERAL
2690 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2691 && outer_type != NULL) {
2692 sub = initializer_from_expression(outer_type, expression);
2694 if (token.type == ',') {
2697 if (token.type != '}') {
2698 warningf(HERE, "excessive elements in initializer for type '%T'",
2701 /* TODO: eat , ... */
2706 /* descend into subtypes until expression matches type */
2708 orig_type = path->top_type;
2709 type = skip_typeref(orig_type);
2711 sub = initializer_from_expression(orig_type, expression);
2715 if (!is_type_valid(type)) {
2718 if (is_type_scalar(type)) {
2719 errorf(&expression->base.source_position,
2720 "expression '%E' doesn't match expected type '%T'",
2721 expression, orig_type);
2725 descend_into_subtype(path);
2729 /* update largest index of top array */
2730 const type_path_entry_t *first = &path->path[0];
2731 type_t *first_type = first->type;
2732 first_type = skip_typeref(first_type);
2733 if (is_type_array(first_type)) {
2734 size_t index = first->v.index;
2735 if (index > path->max_index)
2736 path->max_index = index;
2740 /* append to initializers list */
2741 ARR_APP1(initializer_t*, initializers, sub);
2744 if (env->declaration != NULL)
2745 warningf(HERE, "excess elements in struct initializer for '%Y'",
2746 env->declaration->symbol);
2748 warningf(HERE, "excess elements in struct initializer");
2752 if (token.type == '}') {
2756 if (token.type == '}') {
2761 /* advance to the next declaration if we are not at the end */
2762 advance_current_object(path, top_path_level);
2763 orig_type = path->top_type;
2764 if (orig_type != NULL)
2765 type = skip_typeref(orig_type);
2771 size_t len = ARR_LEN(initializers);
2772 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2773 initializer_t *result = allocate_ast_zero(size);
2774 result->kind = INITIALIZER_LIST;
2775 result->list.len = len;
2776 memcpy(&result->list.initializers, initializers,
2777 len * sizeof(initializers[0]));
2779 DEL_ARR_F(initializers);
2780 ascend_to(path, top_path_level+1);
2785 skip_initializers();
2786 DEL_ARR_F(initializers);
2787 ascend_to(path, top_path_level+1);
2792 * Parses an initializer. Parsers either a compound literal
2793 * (env->declaration == NULL) or an initializer of a declaration.
2795 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2797 type_t *type = skip_typeref(env->type);
2798 initializer_t *result = NULL;
2801 if (is_type_scalar(type)) {
2802 result = parse_scalar_initializer(type, env->must_be_constant);
2803 } else if (token.type == '{') {
2807 memset(&path, 0, sizeof(path));
2808 path.top_type = env->type;
2809 path.path = NEW_ARR_F(type_path_entry_t, 0);
2811 descend_into_subtype(&path);
2813 add_anchor_token('}');
2814 result = parse_sub_initializer(&path, env->type, 1, env);
2815 rem_anchor_token('}');
2817 max_index = path.max_index;
2818 DEL_ARR_F(path.path);
2822 /* parse_scalar_initializer() also works in this case: we simply
2823 * have an expression without {} around it */
2824 result = parse_scalar_initializer(type, env->must_be_constant);
2827 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2828 * the array type size */
2829 if (is_type_array(type) && type->array.size_expression == NULL
2830 && result != NULL) {
2832 switch (result->kind) {
2833 case INITIALIZER_LIST:
2834 size = max_index + 1;
2837 case INITIALIZER_STRING:
2838 size = result->string.string.size;
2841 case INITIALIZER_WIDE_STRING:
2842 size = result->wide_string.string.size;
2845 case INITIALIZER_DESIGNATOR:
2846 case INITIALIZER_VALUE:
2847 /* can happen for parse errors */
2852 internal_errorf(HERE, "invalid initializer type");
2855 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2856 cnst->base.type = type_size_t;
2857 cnst->conste.v.int_value = size;
2859 type_t *new_type = duplicate_type(type);
2861 new_type->array.size_expression = cnst;
2862 new_type->array.size_constant = true;
2863 new_type->array.size = size;
2864 env->type = new_type;
2872 static declaration_t *append_declaration(declaration_t *declaration);
2874 static declaration_t *parse_compound_type_specifier(bool is_struct)
2876 gnu_attribute_t *attributes = NULL;
2877 decl_modifiers_t modifiers = 0;
2884 symbol_t *symbol = NULL;
2885 declaration_t *declaration = NULL;
2887 if (token.type == T___attribute__) {
2888 modifiers |= parse_attributes(&attributes);
2891 if (token.type == T_IDENTIFIER) {
2892 symbol = token.v.symbol;
2895 namespace_t const namespc =
2896 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2897 declaration = get_declaration(symbol, namespc);
2898 if (declaration != NULL) {
2899 if (declaration->parent_scope != scope &&
2900 (token.type == '{' || token.type == ';')) {
2902 } else if (declaration->init.complete &&
2903 token.type == '{') {
2904 assert(symbol != NULL);
2905 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2906 is_struct ? "struct" : "union", symbol,
2907 &declaration->source_position);
2908 declaration->scope.declarations = NULL;
2911 } else if (token.type != '{') {
2913 parse_error_expected("while parsing struct type specifier",
2914 T_IDENTIFIER, '{', NULL);
2916 parse_error_expected("while parsing union type specifier",
2917 T_IDENTIFIER, '{', NULL);
2923 if (declaration == NULL) {
2924 declaration = allocate_declaration_zero();
2925 declaration->namespc =
2926 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2927 declaration->source_position = token.source_position;
2928 declaration->symbol = symbol;
2929 declaration->parent_scope = scope;
2930 if (symbol != NULL) {
2931 environment_push(declaration);
2933 append_declaration(declaration);
2936 if (token.type == '{') {
2937 declaration->init.complete = true;
2939 parse_compound_type_entries(declaration);
2940 modifiers |= parse_attributes(&attributes);
2943 declaration->modifiers |= modifiers;
2947 static void parse_enum_entries(type_t *const enum_type)
2951 if (token.type == '}') {
2953 errorf(HERE, "empty enum not allowed");
2957 add_anchor_token('}');
2959 if (token.type != T_IDENTIFIER) {
2960 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2962 rem_anchor_token('}');
2966 declaration_t *const entry = allocate_declaration_zero();
2967 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2968 entry->type = enum_type;
2969 entry->symbol = token.v.symbol;
2970 entry->source_position = token.source_position;
2973 if (token.type == '=') {
2975 expression_t *value = parse_constant_expression();
2977 value = create_implicit_cast(value, enum_type);
2978 entry->init.enum_value = value;
2983 record_declaration(entry, false);
2985 if (token.type != ',')
2988 } while (token.type != '}');
2989 rem_anchor_token('}');
2997 static type_t *parse_enum_specifier(void)
2999 gnu_attribute_t *attributes = NULL;
3000 declaration_t *declaration;
3004 if (token.type == T_IDENTIFIER) {
3005 symbol = token.v.symbol;
3008 declaration = get_declaration(symbol, NAMESPACE_ENUM);
3009 } else if (token.type != '{') {
3010 parse_error_expected("while parsing enum type specifier",
3011 T_IDENTIFIER, '{', NULL);
3018 if (declaration == NULL) {
3019 declaration = allocate_declaration_zero();
3020 declaration->namespc = NAMESPACE_ENUM;
3021 declaration->source_position = token.source_position;
3022 declaration->symbol = symbol;
3023 declaration->parent_scope = scope;
3026 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
3027 type->enumt.declaration = declaration;
3029 if (token.type == '{') {
3030 if (declaration->init.complete) {
3031 errorf(HERE, "multiple definitions of enum %Y", symbol);
3033 if (symbol != NULL) {
3034 environment_push(declaration);
3036 append_declaration(declaration);
3037 declaration->init.complete = true;
3039 parse_enum_entries(type);
3040 parse_attributes(&attributes);
3047 * if a symbol is a typedef to another type, return true
3049 static bool is_typedef_symbol(symbol_t *symbol)
3051 const declaration_t *const declaration =
3052 get_declaration(symbol, NAMESPACE_NORMAL);
3054 declaration != NULL &&
3055 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
3058 static type_t *parse_typeof(void)
3065 add_anchor_token(')');
3067 expression_t *expression = NULL;
3069 bool old_type_prop = in_type_prop;
3070 bool old_gcc_extension = in_gcc_extension;
3071 in_type_prop = true;
3073 while (token.type == T___extension__) {
3074 /* This can be a prefix to a typename or an expression. */
3076 in_gcc_extension = true;
3078 switch (token.type) {
3080 if (is_typedef_symbol(token.v.symbol)) {
3081 type = parse_typename();
3083 expression = parse_expression();
3084 type = expression->base.type;
3089 type = parse_typename();
3093 expression = parse_expression();
3094 type = expression->base.type;
3097 in_type_prop = old_type_prop;
3098 in_gcc_extension = old_gcc_extension;
3100 rem_anchor_token(')');
3103 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
3104 typeof_type->typeoft.expression = expression;
3105 typeof_type->typeoft.typeof_type = type;
3112 typedef enum specifiers_t {
3113 SPECIFIER_SIGNED = 1 << 0,
3114 SPECIFIER_UNSIGNED = 1 << 1,
3115 SPECIFIER_LONG = 1 << 2,
3116 SPECIFIER_INT = 1 << 3,
3117 SPECIFIER_DOUBLE = 1 << 4,
3118 SPECIFIER_CHAR = 1 << 5,
3119 SPECIFIER_SHORT = 1 << 6,
3120 SPECIFIER_LONG_LONG = 1 << 7,
3121 SPECIFIER_FLOAT = 1 << 8,
3122 SPECIFIER_BOOL = 1 << 9,
3123 SPECIFIER_VOID = 1 << 10,
3124 SPECIFIER_INT8 = 1 << 11,
3125 SPECIFIER_INT16 = 1 << 12,
3126 SPECIFIER_INT32 = 1 << 13,
3127 SPECIFIER_INT64 = 1 << 14,
3128 SPECIFIER_INT128 = 1 << 15,
3129 SPECIFIER_COMPLEX = 1 << 16,
3130 SPECIFIER_IMAGINARY = 1 << 17,
3133 static type_t *create_builtin_type(symbol_t *const symbol,
3134 type_t *const real_type)
3136 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
3137 type->builtin.symbol = symbol;
3138 type->builtin.real_type = real_type;
3140 type_t *result = typehash_insert(type);
3141 if (type != result) {
3148 static type_t *get_typedef_type(symbol_t *symbol)
3150 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
3151 if (declaration == NULL ||
3152 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
3155 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
3156 type->typedeft.declaration = declaration;
3162 * check for the allowed MS alignment values.
3164 static bool check_alignment_value(long long intvalue)
3166 if (intvalue < 1 || intvalue > 8192) {
3167 errorf(HERE, "illegal alignment value");
3170 unsigned v = (unsigned)intvalue;
3171 for (unsigned i = 1; i <= 8192; i += i) {
3175 errorf(HERE, "alignment must be power of two");
3179 #define DET_MOD(name, tag) do { \
3180 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
3181 *modifiers |= tag; \
3184 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
3186 decl_modifiers_t *modifiers = &specifiers->modifiers;
3189 if (token.type == T_restrict) {
3191 DET_MOD(restrict, DM_RESTRICT);
3193 } else if (token.type != T_IDENTIFIER)
3195 symbol_t *symbol = token.v.symbol;
3196 if (symbol == sym_align) {
3199 if (token.type != T_INTEGER)
3201 if (check_alignment_value(token.v.intvalue)) {
3202 if (specifiers->alignment != 0)
3203 warningf(HERE, "align used more than once");
3204 specifiers->alignment = (unsigned char)token.v.intvalue;
3208 } else if (symbol == sym_allocate) {
3211 if (token.type != T_IDENTIFIER)
3213 (void)token.v.symbol;
3215 } else if (symbol == sym_dllimport) {
3217 DET_MOD(dllimport, DM_DLLIMPORT);
3218 } else if (symbol == sym_dllexport) {
3220 DET_MOD(dllexport, DM_DLLEXPORT);
3221 } else if (symbol == sym_thread) {
3223 DET_MOD(thread, DM_THREAD);
3224 } else if (symbol == sym_naked) {
3226 DET_MOD(naked, DM_NAKED);
3227 } else if (symbol == sym_noinline) {
3229 DET_MOD(noinline, DM_NOINLINE);
3230 } else if (symbol == sym_noreturn) {
3232 DET_MOD(noreturn, DM_NORETURN);
3233 } else if (symbol == sym_nothrow) {
3235 DET_MOD(nothrow, DM_NOTHROW);
3236 } else if (symbol == sym_novtable) {
3238 DET_MOD(novtable, DM_NOVTABLE);
3239 } else if (symbol == sym_property) {
3243 bool is_get = false;
3244 if (token.type != T_IDENTIFIER)
3246 if (token.v.symbol == sym_get) {
3248 } else if (token.v.symbol == sym_put) {
3250 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
3255 if (token.type != T_IDENTIFIER)
3258 if (specifiers->get_property_sym != NULL) {
3259 errorf(HERE, "get property name already specified");
3261 specifiers->get_property_sym = token.v.symbol;
3264 if (specifiers->put_property_sym != NULL) {
3265 errorf(HERE, "put property name already specified");
3267 specifiers->put_property_sym = token.v.symbol;
3271 if (token.type == ',') {
3278 } else if (symbol == sym_selectany) {
3280 DET_MOD(selectany, DM_SELECTANY);
3281 } else if (symbol == sym_uuid) {
3284 if (token.type != T_STRING_LITERAL)
3288 } else if (symbol == sym_deprecated) {
3290 if (specifiers->deprecated != 0)
3291 warningf(HERE, "deprecated used more than once");
3292 specifiers->deprecated = 1;
3293 if (token.type == '(') {
3295 if (token.type == T_STRING_LITERAL) {
3296 specifiers->deprecated_string = token.v.string.begin;
3299 errorf(HERE, "string literal expected");
3303 } else if (symbol == sym_noalias) {
3305 DET_MOD(noalias, DM_NOALIAS);
3307 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3309 if (token.type == '(')
3313 if (token.type == ',')
3320 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3322 declaration_t *const decl = allocate_declaration_zero();
3323 decl->source_position = *HERE;
3324 decl->declared_storage_class = storage_class;
3325 decl->storage_class =
3326 storage_class != STORAGE_CLASS_NONE || scope == file_scope ?
3327 storage_class : STORAGE_CLASS_AUTO;
3328 decl->symbol = symbol;
3329 decl->implicit = true;
3330 record_declaration(decl, false);
3335 * Finish the construction of a struct type by calculating
3336 * its size, offsets, alignment.
3338 static void finish_struct_type(compound_type_t *type) {
3339 if (type->declaration == NULL)
3341 declaration_t *struct_decl = type->declaration;
3342 if (! struct_decl->init.complete)
3347 il_alignment_t alignment = 1;
3348 bool need_pad = false;
3350 declaration_t *entry = struct_decl->scope.declarations;
3351 for (; entry != NULL; entry = entry->next) {
3352 if (entry->namespc != NAMESPACE_NORMAL)
3355 type_t *m_type = skip_typeref(entry->type);
3356 if (! is_type_valid(m_type)) {
3357 /* simply ignore errors here */
3360 il_alignment_t m_alignment = m_type->base.alignment;
3361 if (m_alignment > alignment)
3362 alignment = m_alignment;
3364 offset = (size + m_alignment - 1) & -m_alignment;
3368 entry->offset = offset;
3369 size = offset + m_type->base.size;
3371 if (type->base.alignment != 0) {
3372 alignment = type->base.alignment;
3375 offset = (size + alignment - 1) & -alignment;
3379 if (warning.padded && need_pad) {
3380 warningf(&struct_decl->source_position,
3381 "'%#T' needs padding", type, struct_decl->symbol);
3383 if (warning.packed && !need_pad) {
3384 warningf(&struct_decl->source_position,
3385 "superfluous packed attribute on '%#T'",
3386 type, struct_decl->symbol);
3389 type->base.size = offset;
3390 type->base.alignment = alignment;
3394 * Finish the construction of an union type by calculating
3395 * its size and alignment.
3397 static void finish_union_type(compound_type_t *type) {
3398 if (type->declaration == NULL)
3400 declaration_t *union_decl = type->declaration;
3401 if (! union_decl->init.complete)
3405 il_alignment_t alignment = 1;
3407 declaration_t *entry = union_decl->scope.declarations;
3408 for (; entry != NULL; entry = entry->next) {
3409 if (entry->namespc != NAMESPACE_NORMAL)
3412 type_t *m_type = skip_typeref(entry->type);
3413 if (! is_type_valid(m_type))
3417 if (m_type->base.size > size)
3418 size = m_type->base.size;
3419 if (m_type->base.alignment > alignment)
3420 alignment = m_type->base.alignment;
3422 if (type->base.alignment != 0) {
3423 alignment = type->base.alignment;
3425 size = (size + alignment - 1) & -alignment;
3426 type->base.size = size;
3427 type->base.alignment = alignment;
3430 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3432 type_t *type = NULL;
3433 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3434 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3435 unsigned type_specifiers = 0;
3436 bool newtype = false;
3437 bool saw_error = false;
3438 bool old_gcc_extension = in_gcc_extension;
3440 specifiers->source_position = token.source_position;
3443 specifiers->modifiers
3444 |= parse_attributes(&specifiers->gnu_attributes);
3445 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3446 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3448 switch (token.type) {
3451 #define MATCH_STORAGE_CLASS(token, class) \
3453 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3454 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3456 specifiers->declared_storage_class = class; \
3460 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3461 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3462 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3463 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3464 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3469 add_anchor_token(')');
3470 parse_microsoft_extended_decl_modifier(specifiers);
3471 rem_anchor_token(')');
3476 switch (specifiers->declared_storage_class) {
3477 case STORAGE_CLASS_NONE:
3478 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3481 case STORAGE_CLASS_EXTERN:
3482 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3485 case STORAGE_CLASS_STATIC:
3486 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3490 errorf(HERE, "multiple storage classes in declaration specifiers");
3496 /* type qualifiers */
3497 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3499 qualifiers |= qualifier; \
3503 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3504 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3505 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3506 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3507 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3508 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3509 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3510 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3512 case T___extension__:
3514 in_gcc_extension = true;
3517 /* type specifiers */
3518 #define MATCH_SPECIFIER(token, specifier, name) \
3521 if (type_specifiers & specifier) { \
3522 errorf(HERE, "multiple " name " type specifiers given"); \
3524 type_specifiers |= specifier; \
3528 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3529 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3530 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3531 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3532 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3533 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3534 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3535 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3536 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3537 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3538 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3539 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3540 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3541 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3542 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3543 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3545 case T__forceinline:
3546 /* only in microsoft mode */
3547 specifiers->modifiers |= DM_FORCEINLINE;
3552 specifiers->is_inline = true;
3557 if (type_specifiers & SPECIFIER_LONG_LONG) {
3558 errorf(HERE, "multiple type specifiers given");
3559 } else if (type_specifiers & SPECIFIER_LONG) {
3560 type_specifiers |= SPECIFIER_LONG_LONG;
3562 type_specifiers |= SPECIFIER_LONG;
3567 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3569 type->compound.declaration = parse_compound_type_specifier(true);
3570 finish_struct_type(&type->compound);
3574 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3575 type->compound.declaration = parse_compound_type_specifier(false);
3576 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3577 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3578 finish_union_type(&type->compound);
3582 type = parse_enum_specifier();
3585 type = parse_typeof();
3587 case T___builtin_va_list:
3588 type = duplicate_type(type_valist);
3592 case T_IDENTIFIER: {
3593 /* only parse identifier if we haven't found a type yet */
3594 if (type != NULL || type_specifiers != 0) {
3595 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3596 * declaration, so it doesn't generate errors about expecting '(' or
3598 switch (look_ahead(1)->type) {
3605 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3608 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3613 goto finish_specifiers;
3617 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3618 if (typedef_type == NULL) {
3619 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3620 * declaration, so it doesn't generate 'implicit int' followed by more
3621 * errors later on. */
3622 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3627 errorf(HERE, "%K does not name a type", &token);
3629 declaration_t *const decl =
3630 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3632 type = allocate_type_zero(TYPE_TYPEDEF, HERE);
3633 type->typedeft.declaration = decl;
3637 if (la1_type == '*')
3638 goto finish_specifiers;
3643 goto finish_specifiers;
3648 type = typedef_type;
3652 /* function specifier */
3654 goto finish_specifiers;
3659 in_gcc_extension = old_gcc_extension;
3661 if (type == NULL || (saw_error && type_specifiers != 0)) {
3662 atomic_type_kind_t atomic_type;
3664 /* match valid basic types */
3665 switch(type_specifiers) {
3666 case SPECIFIER_VOID:
3667 atomic_type = ATOMIC_TYPE_VOID;
3669 case SPECIFIER_CHAR:
3670 atomic_type = ATOMIC_TYPE_CHAR;
3672 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3673 atomic_type = ATOMIC_TYPE_SCHAR;
3675 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3676 atomic_type = ATOMIC_TYPE_UCHAR;
3678 case SPECIFIER_SHORT:
3679 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3680 case SPECIFIER_SHORT | SPECIFIER_INT:
3681 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3682 atomic_type = ATOMIC_TYPE_SHORT;
3684 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3685 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3686 atomic_type = ATOMIC_TYPE_USHORT;
3689 case SPECIFIER_SIGNED:
3690 case SPECIFIER_SIGNED | SPECIFIER_INT:
3691 atomic_type = ATOMIC_TYPE_INT;
3693 case SPECIFIER_UNSIGNED:
3694 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3695 atomic_type = ATOMIC_TYPE_UINT;
3697 case SPECIFIER_LONG:
3698 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3699 case SPECIFIER_LONG | SPECIFIER_INT:
3700 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3701 atomic_type = ATOMIC_TYPE_LONG;
3703 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3704 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3705 atomic_type = ATOMIC_TYPE_ULONG;
3708 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3709 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3710 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3711 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3713 atomic_type = ATOMIC_TYPE_LONGLONG;
3714 goto warn_about_long_long;
3716 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3717 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3719 atomic_type = ATOMIC_TYPE_ULONGLONG;
3720 warn_about_long_long:
3721 if (warning.long_long) {
3722 warningf(&specifiers->source_position,
3723 "ISO C90 does not support 'long long'");
3727 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3728 atomic_type = unsigned_int8_type_kind;
3731 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3732 atomic_type = unsigned_int16_type_kind;
3735 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3736 atomic_type = unsigned_int32_type_kind;
3739 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3740 atomic_type = unsigned_int64_type_kind;
3743 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3744 atomic_type = unsigned_int128_type_kind;
3747 case SPECIFIER_INT8:
3748 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3749 atomic_type = int8_type_kind;
3752 case SPECIFIER_INT16:
3753 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3754 atomic_type = int16_type_kind;
3757 case SPECIFIER_INT32:
3758 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3759 atomic_type = int32_type_kind;
3762 case SPECIFIER_INT64:
3763 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3764 atomic_type = int64_type_kind;
3767 case SPECIFIER_INT128:
3768 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3769 atomic_type = int128_type_kind;
3772 case SPECIFIER_FLOAT:
3773 atomic_type = ATOMIC_TYPE_FLOAT;
3775 case SPECIFIER_DOUBLE:
3776 atomic_type = ATOMIC_TYPE_DOUBLE;
3778 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3779 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3781 case SPECIFIER_BOOL:
3782 atomic_type = ATOMIC_TYPE_BOOL;
3784 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3785 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3786 atomic_type = ATOMIC_TYPE_FLOAT;
3788 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3789 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3790 atomic_type = ATOMIC_TYPE_DOUBLE;
3792 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3793 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3794 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3797 /* invalid specifier combination, give an error message */
3798 if (type_specifiers == 0) {
3803 if (warning.implicit_int) {
3804 warningf(HERE, "no type specifiers in declaration, using 'int'");
3806 atomic_type = ATOMIC_TYPE_INT;
3809 errorf(HERE, "no type specifiers given in declaration");
3811 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3812 (type_specifiers & SPECIFIER_UNSIGNED)) {
3813 errorf(HERE, "signed and unsigned specifiers given");
3814 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3815 errorf(HERE, "only integer types can be signed or unsigned");
3817 errorf(HERE, "multiple datatypes in declaration");
3822 if (type_specifiers & SPECIFIER_COMPLEX) {
3823 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3824 type->complex.akind = atomic_type;
3825 } else if (type_specifiers & SPECIFIER_IMAGINARY) {
3826 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3827 type->imaginary.akind = atomic_type;
3829 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3830 type->atomic.akind = atomic_type;
3833 } else if (type_specifiers != 0) {
3834 errorf(HERE, "multiple datatypes in declaration");
3837 /* FIXME: check type qualifiers here */
3839 type->base.qualifiers = qualifiers;
3840 type->base.modifiers = modifiers;
3842 type_t *result = typehash_insert(type);
3843 if (newtype && result != type) {
3847 specifiers->type = result;
3851 specifiers->type = type_error_type;
3855 static type_qualifiers_t parse_type_qualifiers(void)
3857 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3860 switch(token.type) {
3861 /* type qualifiers */
3862 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3863 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3864 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3865 /* microsoft extended type modifiers */
3866 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3867 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3868 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3869 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3870 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3879 * Parses an K&R identifier list and return a list of declarations.
3881 * @param last points to the last declaration in the list
3882 * @return the list of declarations
3884 static declaration_t *parse_identifier_list(declaration_t **last)
3886 declaration_t *declarations = NULL;
3887 declaration_t *last_declaration = NULL;
3889 declaration_t *const declaration = allocate_declaration_zero();
3890 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3891 declaration->source_position = token.source_position;
3892 declaration->symbol = token.v.symbol;
3895 if (last_declaration != NULL) {
3896 last_declaration->next = declaration;
3898 declarations = declaration;
3900 last_declaration = declaration;
3902 if (token.type != ',') {
3906 } while (token.type == T_IDENTIFIER);
3908 *last = last_declaration;
3909 return declarations;
3912 static type_t *automatic_type_conversion(type_t *orig_type);
3914 static void semantic_parameter(declaration_t *declaration)
3916 /* TODO: improve error messages */
3917 source_position_t const* const pos = &declaration->source_position;
3919 switch (declaration->declared_storage_class) {
3920 case STORAGE_CLASS_TYPEDEF:
3921 errorf(pos, "typedef not allowed in parameter list");
3924 /* Allowed storage classes */
3925 case STORAGE_CLASS_NONE:
3926 case STORAGE_CLASS_REGISTER:
3930 errorf(pos, "parameter may only have none or register storage class");
3934 type_t *const orig_type = declaration->type;
3935 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3936 * sugar. Turn it into a pointer.
3937 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3938 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3940 type_t *const type = automatic_type_conversion(orig_type);
3941 declaration->type = type;
3943 if (is_type_incomplete(skip_typeref(type))) {
3944 errorf(pos, "parameter '%#T' is of incomplete type",
3945 orig_type, declaration->symbol);
3949 static declaration_t *parse_parameter(void)
3951 declaration_specifiers_t specifiers;
3952 memset(&specifiers, 0, sizeof(specifiers));
3954 parse_declaration_specifiers(&specifiers);
3956 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3962 * Parses a function type parameter list and return a list of declarations.
3964 * @param last point to the last element of the list
3965 * @return the parameter list
3967 static declaration_t *parse_parameters(function_type_t *type, declaration_t **last)
3969 declaration_t *declarations = NULL;
3972 add_anchor_token(')');
3973 int saved_comma_state = save_and_reset_anchor_state(',');
3975 if (token.type == T_IDENTIFIER &&
3976 !is_typedef_symbol(token.v.symbol)) {
3977 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
3978 if (la1_type == ',' || la1_type == ')') {
3979 type->kr_style_parameters = true;
3980 declarations = parse_identifier_list(last);
3981 goto parameters_finished;
3985 if (token.type == ')') {
3986 type->unspecified_parameters = 1;
3987 goto parameters_finished;
3990 declaration_t *declaration;
3991 declaration_t *last_declaration = NULL;
3992 function_parameter_t *parameter;
3993 function_parameter_t *last_parameter = NULL;
3996 switch(token.type) {
4000 goto parameters_finished;
4003 case T___extension__:
4005 declaration = parse_parameter();
4007 /* func(void) is not a parameter */
4008 if (last_parameter == NULL
4009 && token.type == ')'
4010 && declaration->symbol == NULL
4011 && skip_typeref(declaration->type) == type_void) {
4012 goto parameters_finished;
4014 semantic_parameter(declaration);
4016 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
4017 memset(parameter, 0, sizeof(parameter[0]));
4018 parameter->type = declaration->type;
4020 if (last_parameter != NULL) {
4021 last_declaration->next = declaration;
4022 last_parameter->next = parameter;
4024 type->parameters = parameter;
4025 declarations = declaration;
4027 last_parameter = parameter;
4028 last_declaration = declaration;
4032 goto parameters_finished;
4034 if (token.type != ',') {
4035 goto parameters_finished;
4041 parameters_finished:
4042 rem_anchor_token(')');
4045 restore_anchor_state(',', saved_comma_state);
4046 *last = last_declaration;
4047 return declarations;
4050 restore_anchor_state(',', saved_comma_state);
4055 typedef enum construct_type_kind_t {
4060 } construct_type_kind_t;
4062 typedef struct construct_type_t construct_type_t;
4063 struct construct_type_t {
4064 construct_type_kind_t kind;
4065 construct_type_t *next;
4068 typedef struct parsed_pointer_t parsed_pointer_t;
4069 struct parsed_pointer_t {
4070 construct_type_t construct_type;
4071 type_qualifiers_t type_qualifiers;
4074 typedef struct construct_function_type_t construct_function_type_t;
4075 struct construct_function_type_t {
4076 construct_type_t construct_type;
4077 type_t *function_type;
4080 typedef struct parsed_array_t parsed_array_t;
4081 struct parsed_array_t {
4082 construct_type_t construct_type;
4083 type_qualifiers_t type_qualifiers;
4089 typedef struct construct_base_type_t construct_base_type_t;
4090 struct construct_base_type_t {
4091 construct_type_t construct_type;
4095 static construct_type_t *parse_pointer_declarator(void)
4099 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
4100 memset(pointer, 0, sizeof(pointer[0]));
4101 pointer->construct_type.kind = CONSTRUCT_POINTER;
4102 pointer->type_qualifiers = parse_type_qualifiers();
4104 return (construct_type_t*) pointer;
4107 static construct_type_t *parse_array_declarator(void)
4110 add_anchor_token(']');
4112 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
4113 memset(array, 0, sizeof(array[0]));
4114 array->construct_type.kind = CONSTRUCT_ARRAY;
4116 if (token.type == T_static) {
4117 array->is_static = true;
4121 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
4122 if (type_qualifiers != 0) {
4123 if (token.type == T_static) {
4124 array->is_static = true;
4128 array->type_qualifiers = type_qualifiers;
4130 if (token.type == '*' && look_ahead(1)->type == ']') {
4131 array->is_variable = true;
4133 } else if (token.type != ']') {
4134 array->size = parse_assignment_expression();
4137 rem_anchor_token(']');
4141 return (construct_type_t*) array;
4144 static construct_type_t *parse_function_declarator(declaration_t *declaration)
4147 if (declaration != NULL) {
4148 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
4150 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
4152 if (mask & (mask-1)) {
4153 const char *first = NULL, *second = NULL;
4155 /* more than one calling convention set */
4156 if (declaration->modifiers & DM_CDECL) {
4157 if (first == NULL) first = "cdecl";
4158 else if (second == NULL) second = "cdecl";
4160 if (declaration->modifiers & DM_STDCALL) {
4161 if (first == NULL) first = "stdcall";
4162 else if (second == NULL) second = "stdcall";
4164 if (declaration->modifiers & DM_FASTCALL) {
4165 if (first == NULL) first = "fastcall";
4166 else if (second == NULL) second = "fastcall";
4168 if (declaration->modifiers & DM_THISCALL) {
4169 if (first == NULL) first = "thiscall";
4170 else if (second == NULL) second = "thiscall";
4172 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
4175 if (declaration->modifiers & DM_CDECL)
4176 type->function.calling_convention = CC_CDECL;
4177 else if (declaration->modifiers & DM_STDCALL)
4178 type->function.calling_convention = CC_STDCALL;
4179 else if (declaration->modifiers & DM_FASTCALL)
4180 type->function.calling_convention = CC_FASTCALL;
4181 else if (declaration->modifiers & DM_THISCALL)
4182 type->function.calling_convention = CC_THISCALL;
4184 type = allocate_type_zero(TYPE_FUNCTION, HERE);
4187 declaration_t *last;
4188 declaration_t *parameters = parse_parameters(&type->function, &last);
4189 if (declaration != NULL) {
4190 declaration->scope.declarations = parameters;
4191 declaration->scope.last_declaration = last;
4192 declaration->scope.is_parameter = true;
4195 construct_function_type_t *construct_function_type =
4196 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
4197 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
4198 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
4199 construct_function_type->function_type = type;
4201 return &construct_function_type->construct_type;
4204 static void fix_declaration_type(declaration_t *declaration)
4206 decl_modifiers_t declaration_modifiers = declaration->modifiers;
4207 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
4209 if (declaration_modifiers & DM_TRANSPARENT_UNION)
4210 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4212 if (declaration->type->base.modifiers == type_modifiers)
4215 type_t *copy = duplicate_type(declaration->type);
4216 copy->base.modifiers = type_modifiers;
4218 type_t *result = typehash_insert(copy);
4219 if (result != copy) {
4220 obstack_free(type_obst, copy);
4223 declaration->type = result;
4226 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
4227 bool may_be_abstract)
4229 /* construct a single linked list of construct_type_t's which describe
4230 * how to construct the final declarator type */
4231 construct_type_t *first = NULL;
4232 construct_type_t *last = NULL;
4233 gnu_attribute_t *attributes = NULL;
4235 decl_modifiers_t modifiers = parse_attributes(&attributes);
4238 while (token.type == '*') {
4239 construct_type_t *type = parse_pointer_declarator();
4249 /* TODO: find out if this is correct */
4250 modifiers |= parse_attributes(&attributes);
4253 if (declaration != NULL)
4254 declaration->modifiers |= modifiers;
4256 construct_type_t *inner_types = NULL;
4258 switch(token.type) {
4260 if (declaration == NULL) {
4261 errorf(HERE, "no identifier expected in typename");
4263 declaration->symbol = token.v.symbol;
4264 declaration->source_position = token.source_position;
4270 add_anchor_token(')');
4271 inner_types = parse_inner_declarator(declaration, may_be_abstract);
4272 if (inner_types != NULL) {
4273 /* All later declarators only modify the return type, not declaration */
4276 rem_anchor_token(')');
4280 if (may_be_abstract)
4282 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
4287 construct_type_t *p = last;
4290 construct_type_t *type;
4291 switch(token.type) {
4293 type = parse_function_declarator(declaration);
4296 type = parse_array_declarator();
4299 goto declarator_finished;
4302 /* insert in the middle of the list (behind p) */
4304 type->next = p->next;
4315 declarator_finished:
4316 /* append inner_types at the end of the list, we don't to set last anymore
4317 * as it's not needed anymore */
4319 assert(first == NULL);
4320 first = inner_types;
4322 last->next = inner_types;
4330 static void parse_declaration_attributes(declaration_t *declaration)
4332 gnu_attribute_t *attributes = NULL;
4333 decl_modifiers_t modifiers = parse_attributes(&attributes);
4335 if (declaration == NULL)
4338 declaration->modifiers |= modifiers;
4339 /* check if we have these stupid mode attributes... */
4340 type_t *old_type = declaration->type;
4341 if (old_type == NULL)
4344 gnu_attribute_t *attribute = attributes;
4345 for ( ; attribute != NULL; attribute = attribute->next) {
4346 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
4349 atomic_type_kind_t akind = attribute->u.akind;
4350 if (!is_type_signed(old_type)) {
4352 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
4353 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
4354 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
4355 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
4357 panic("invalid akind in mode attribute");
4361 = make_atomic_type(akind, old_type->base.qualifiers);
4365 static type_t *construct_declarator_type(construct_type_t *construct_list,
4368 construct_type_t *iter = construct_list;
4369 for( ; iter != NULL; iter = iter->next) {
4370 switch(iter->kind) {
4371 case CONSTRUCT_INVALID:
4372 internal_errorf(HERE, "invalid type construction found");
4373 case CONSTRUCT_FUNCTION: {
4374 construct_function_type_t *construct_function_type
4375 = (construct_function_type_t*) iter;
4377 type_t *function_type = construct_function_type->function_type;
4379 function_type->function.return_type = type;
4381 type_t *skipped_return_type = skip_typeref(type);
4383 if (is_type_function(skipped_return_type)) {
4384 errorf(HERE, "function returning function is not allowed");
4385 } else if (is_type_array(skipped_return_type)) {
4386 errorf(HERE, "function returning array is not allowed");
4388 if (skipped_return_type->base.qualifiers != 0) {
4390 "type qualifiers in return type of function type are meaningless");
4394 type = function_type;
4398 case CONSTRUCT_POINTER: {
4399 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4400 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
4401 pointer_type->pointer.points_to = type;
4402 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
4404 type = pointer_type;
4408 case CONSTRUCT_ARRAY: {
4409 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4410 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
4412 expression_t *size_expression = parsed_array->size;
4413 if (size_expression != NULL) {
4415 = create_implicit_cast(size_expression, type_size_t);
4418 array_type->base.qualifiers = parsed_array->type_qualifiers;
4419 array_type->array.element_type = type;
4420 array_type->array.is_static = parsed_array->is_static;
4421 array_type->array.is_variable = parsed_array->is_variable;
4422 array_type->array.size_expression = size_expression;
4424 if (size_expression != NULL) {
4425 if (is_constant_expression(size_expression)) {
4426 array_type->array.size_constant = true;
4427 array_type->array.size
4428 = fold_constant(size_expression);
4430 array_type->array.is_vla = true;
4434 type_t *skipped_type = skip_typeref(type);
4436 if (is_type_incomplete(skipped_type)) {
4437 errorf(HERE, "array of incomplete type '%T' is not allowed", type);
4438 } else if (is_type_function(skipped_type)) {
4439 errorf(HERE, "array of functions is not allowed");
4446 type_t *hashed_type = typehash_insert(type);
4447 if (hashed_type != type) {
4448 /* the function type was constructed earlier freeing it here will
4449 * destroy other types... */
4450 if (iter->kind != CONSTRUCT_FUNCTION) {
4460 static declaration_t *parse_declarator(
4461 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4463 declaration_t *const declaration = allocate_declaration_zero();
4464 declaration->source_position = specifiers->source_position;
4465 declaration->declared_storage_class = specifiers->declared_storage_class;
4466 declaration->modifiers = specifiers->modifiers;
4467 declaration->deprecated_string = specifiers->deprecated_string;
4468 declaration->get_property_sym = specifiers->get_property_sym;
4469 declaration->put_property_sym = specifiers->put_property_sym;
4470 declaration->is_inline = specifiers->is_inline;
4472 declaration->storage_class = specifiers->declared_storage_class;
4473 if (declaration->storage_class == STORAGE_CLASS_NONE &&
4474 scope != file_scope) {
4475 declaration->storage_class = STORAGE_CLASS_AUTO;
4478 if (specifiers->alignment != 0) {
4479 /* TODO: add checks here */
4480 declaration->alignment = specifiers->alignment;
4483 construct_type_t *construct_type
4484 = parse_inner_declarator(declaration, may_be_abstract);
4485 type_t *const type = specifiers->type;
4486 declaration->type = construct_declarator_type(construct_type, type);
4488 parse_declaration_attributes(declaration);
4490 fix_declaration_type(declaration);
4492 if (construct_type != NULL) {
4493 obstack_free(&temp_obst, construct_type);
4499 static type_t *parse_abstract_declarator(type_t *base_type)
4501 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4503 type_t *result = construct_declarator_type(construct_type, base_type);
4504 if (construct_type != NULL) {
4505 obstack_free(&temp_obst, construct_type);
4511 static declaration_t *append_declaration(declaration_t* const declaration)
4513 if (last_declaration != NULL) {
4514 last_declaration->next = declaration;
4516 scope->declarations = declaration;
4518 last_declaration = declaration;
4523 * Check if the declaration of main is suspicious. main should be a
4524 * function with external linkage, returning int, taking either zero
4525 * arguments, two, or three arguments of appropriate types, ie.
4527 * int main([ int argc, char **argv [, char **env ] ]).
4529 * @param decl the declaration to check
4530 * @param type the function type of the declaration
4532 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4534 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4535 warningf(&decl->source_position,
4536 "'main' is normally a non-static function");
4538 if (!types_compatible(skip_typeref(func_type->return_type), type_int)) {
4539 warningf(&decl->source_position,
4540 "return type of 'main' should be 'int', but is '%T'",
4541 func_type->return_type);
4543 const function_parameter_t *parm = func_type->parameters;
4545 type_t *const first_type = parm->type;
4546 if (!types_compatible(skip_typeref(first_type), type_int)) {
4547 warningf(&decl->source_position,
4548 "first argument of 'main' should be 'int', but is '%T'", first_type);
4552 type_t *const second_type = parm->type;
4553 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4554 warningf(&decl->source_position,
4555 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4559 type_t *const third_type = parm->type;
4560 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4561 warningf(&decl->source_position,
4562 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4566 goto warn_arg_count;
4570 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4576 * Check if a symbol is the equal to "main".
4578 static bool is_sym_main(const symbol_t *const sym)
4580 return strcmp(sym->string, "main") == 0;
4583 static declaration_t *record_declaration(
4584 declaration_t *const declaration,
4585 const bool is_definition)
4587 const symbol_t *const symbol = declaration->symbol;
4588 const namespace_t namespc = (namespace_t)declaration->namespc;
4590 assert(symbol != NULL);
4591 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4593 type_t *const orig_type = declaration->type;
4594 type_t *const type = skip_typeref(orig_type);
4595 if (is_type_function(type) &&
4596 type->function.unspecified_parameters &&
4597 warning.strict_prototypes &&
4598 previous_declaration == NULL) {
4599 warningf(&declaration->source_position,
4600 "function declaration '%#T' is not a prototype",
4604 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4605 check_type_of_main(declaration, &type->function);
4608 if (warning.nested_externs &&
4609 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4610 scope != file_scope) {
4611 warningf(&declaration->source_position,
4612 "nested extern declaration of '%#T'", declaration->type, symbol);
4615 assert(declaration != previous_declaration);
4616 if (previous_declaration != NULL &&
4617 previous_declaration->parent_scope->is_parameter &&
4618 scope->depth == previous_declaration->parent_scope->depth + 1) {
4619 errorf(&declaration->source_position,
4620 "declaration '%#T' redeclares the parameter '%#T' (declared %P)",
4621 orig_type, symbol, previous_declaration->type, symbol,
4622 &previous_declaration->source_position);
4625 if (previous_declaration != NULL &&
4626 previous_declaration->parent_scope == scope) {
4627 /* can happen for K&R style declarations */
4628 if (previous_declaration->type == NULL) {
4629 previous_declaration->type = declaration->type;
4632 const type_t *prev_type = skip_typeref(previous_declaration->type);
4633 if (!types_compatible(type, prev_type)) {
4634 errorf(&declaration->source_position,
4635 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4636 orig_type, symbol, previous_declaration->type, symbol,
4637 &previous_declaration->source_position);
4639 unsigned old_storage_class = previous_declaration->storage_class;
4640 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4641 errorf(&declaration->source_position,
4642 "redeclaration of enum entry '%Y' (declared %P)",
4643 symbol, &previous_declaration->source_position);
4644 return previous_declaration;
4647 if (warning.redundant_decls &&
4649 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4650 !(previous_declaration->modifiers & DM_USED) &&
4651 !previous_declaration->used) {
4652 warningf(&previous_declaration->source_position,
4653 "unnecessary static forward declaration for '%#T'",
4654 previous_declaration->type, symbol);
4657 unsigned new_storage_class = declaration->storage_class;
4659 if (is_type_incomplete(prev_type)) {
4660 previous_declaration->type = type;
4664 /* pretend no storage class means extern for function
4665 * declarations (except if the previous declaration is neither
4666 * none nor extern) */
4667 if (is_type_function(type)) {
4668 if (prev_type->function.unspecified_parameters) {
4669 previous_declaration->type = type;
4673 switch (old_storage_class) {
4674 case STORAGE_CLASS_NONE:
4675 old_storage_class = STORAGE_CLASS_EXTERN;
4678 case STORAGE_CLASS_EXTERN:
4679 if (is_definition) {
4680 if (warning.missing_prototypes &&
4681 prev_type->function.unspecified_parameters &&
4682 !is_sym_main(symbol)) {
4683 warningf(&declaration->source_position,
4684 "no previous prototype for '%#T'",
4687 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4688 new_storage_class = STORAGE_CLASS_EXTERN;
4697 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4698 new_storage_class == STORAGE_CLASS_EXTERN) {
4699 warn_redundant_declaration:
4700 if (!is_definition &&
4701 warning.redundant_decls &&
4702 is_type_valid(prev_type) &&
4703 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4704 warningf(&declaration->source_position,
4705 "redundant declaration for '%Y' (declared %P)",
4706 symbol, &previous_declaration->source_position);
4708 } else if (current_function == NULL) {
4709 if (old_storage_class != STORAGE_CLASS_STATIC &&
4710 new_storage_class == STORAGE_CLASS_STATIC) {
4711 errorf(&declaration->source_position,
4712 "static declaration of '%Y' follows non-static declaration (declared %P)",
4713 symbol, &previous_declaration->source_position);
4714 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4715 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4716 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4718 goto warn_redundant_declaration;
4720 } else if (is_type_valid(prev_type)) {
4721 if (old_storage_class == new_storage_class) {
4722 errorf(&declaration->source_position,
4723 "redeclaration of '%Y' (declared %P)",
4724 symbol, &previous_declaration->source_position);
4726 errorf(&declaration->source_position,
4727 "redeclaration of '%Y' with different linkage (declared %P)",
4728 symbol, &previous_declaration->source_position);
4733 previous_declaration->modifiers |= declaration->modifiers;
4734 previous_declaration->is_inline |= declaration->is_inline;
4735 return previous_declaration;
4736 } else if (is_type_function(type)) {
4737 if (is_definition &&
4738 declaration->storage_class != STORAGE_CLASS_STATIC) {
4739 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4740 warningf(&declaration->source_position,
4741 "no previous prototype for '%#T'", orig_type, symbol);
4742 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4743 warningf(&declaration->source_position,
4744 "no previous declaration for '%#T'", orig_type,
4749 if (warning.missing_declarations &&
4750 scope == file_scope && (
4751 declaration->storage_class == STORAGE_CLASS_NONE ||
4752 declaration->storage_class == STORAGE_CLASS_THREAD
4754 warningf(&declaration->source_position,
4755 "no previous declaration for '%#T'", orig_type, symbol);
4759 assert(declaration->parent_scope == NULL);
4760 assert(scope != NULL);
4762 declaration->parent_scope = scope;
4764 environment_push(declaration);
4765 return append_declaration(declaration);
4768 static void parser_error_multiple_definition(declaration_t *declaration,
4769 const source_position_t *source_position)
4771 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4772 declaration->symbol, &declaration->source_position);
4775 static bool is_declaration_specifier(const token_t *token,
4776 bool only_specifiers_qualifiers)
4778 switch (token->type) {
4783 return is_typedef_symbol(token->v.symbol);
4785 case T___extension__:
4787 return !only_specifiers_qualifiers;
4794 static void parse_init_declarator_rest(declaration_t *declaration)
4798 type_t *orig_type = declaration->type;
4799 type_t *type = skip_typeref(orig_type);
4801 if (declaration->init.initializer != NULL) {
4802 parser_error_multiple_definition(declaration, HERE);
4805 bool must_be_constant = false;
4806 if (declaration->storage_class == STORAGE_CLASS_STATIC ||
4807 declaration->storage_class == STORAGE_CLASS_THREAD_STATIC ||
4808 declaration->parent_scope == file_scope) {
4809 must_be_constant = true;
4812 if (is_type_function(type)) {
4813 errorf(&declaration->source_position,
4814 "function '%#T' is initialized like a variable",
4815 orig_type, declaration->symbol);
4816 orig_type = type_error_type;
4819 parse_initializer_env_t env;
4820 env.type = orig_type;
4821 env.must_be_constant = must_be_constant;
4822 env.declaration = current_init_decl = declaration;
4824 initializer_t *initializer = parse_initializer(&env);
4825 current_init_decl = NULL;
4827 if (!is_type_function(type)) {
4828 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
4829 * the array type size */
4830 declaration->type = env.type;
4831 declaration->init.initializer = initializer;
4835 /* parse rest of a declaration without any declarator */
4836 static void parse_anonymous_declaration_rest(
4837 const declaration_specifiers_t *specifiers)
4841 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) {
4842 warningf(&specifiers->source_position,
4843 "useless storage class in empty declaration");
4846 type_t *type = specifiers->type;
4847 switch (type->kind) {
4848 case TYPE_COMPOUND_STRUCT:
4849 case TYPE_COMPOUND_UNION: {
4850 if (type->compound.declaration->symbol == NULL) {
4851 warningf(&specifiers->source_position,
4852 "unnamed struct/union that defines no instances");
4861 warningf(&specifiers->source_position, "empty declaration");
4865 #ifdef RECORD_EMPTY_DECLARATIONS
4866 declaration_t *const declaration = allocate_declaration_zero();
4867 declaration->type = specifiers->type;
4868 declaration->declared_storage_class = specifiers->declared_storage_class;
4869 declaration->source_position = specifiers->source_position;
4870 declaration->modifiers = specifiers->modifiers;
4871 declaration->storage_class = STORAGE_CLASS_NONE;
4873 append_declaration(declaration);
4877 static void parse_declaration_rest(declaration_t *ndeclaration,
4878 const declaration_specifiers_t *specifiers,
4879 parsed_declaration_func finished_declaration)
4881 add_anchor_token(';');
4882 add_anchor_token(',');
4884 declaration_t *declaration =
4885 finished_declaration(ndeclaration, token.type == '=');
4887 type_t *orig_type = declaration->type;
4888 type_t *type = skip_typeref(orig_type);
4890 if (type->kind != TYPE_FUNCTION &&
4891 declaration->is_inline &&
4892 is_type_valid(type)) {
4893 warningf(&declaration->source_position,
4894 "variable '%Y' declared 'inline'\n", declaration->symbol);
4897 if (token.type == '=') {
4898 parse_init_declarator_rest(declaration);
4901 if (token.type != ',')
4905 add_anchor_token('=');
4906 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4907 rem_anchor_token('=');
4912 rem_anchor_token(';');
4913 rem_anchor_token(',');
4916 static declaration_t *finished_kr_declaration(declaration_t *declaration, bool is_definition)
4918 symbol_t *symbol = declaration->symbol;
4919 if (symbol == NULL) {
4920 errorf(HERE, "anonymous declaration not valid as function parameter");
4923 namespace_t namespc = (namespace_t) declaration->namespc;
4924 if (namespc != NAMESPACE_NORMAL) {
4925 return record_declaration(declaration, false);
4928 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4929 if (previous_declaration == NULL ||
4930 previous_declaration->parent_scope != scope) {
4931 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4936 if (is_definition) {
4937 errorf(HERE, "parameter %Y is initialised", declaration->symbol);
4940 if (previous_declaration->type == NULL) {
4941 previous_declaration->type = declaration->type;
4942 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4943 previous_declaration->storage_class = declaration->storage_class;
4944 previous_declaration->parent_scope = scope;
4945 return previous_declaration;
4947 return record_declaration(declaration, false);
4951 static void parse_declaration(parsed_declaration_func finished_declaration)
4953 declaration_specifiers_t specifiers;
4954 memset(&specifiers, 0, sizeof(specifiers));
4956 add_anchor_token(';');
4957 parse_declaration_specifiers(&specifiers);
4958 rem_anchor_token(';');
4960 if (token.type == ';') {
4961 parse_anonymous_declaration_rest(&specifiers);
4963 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4964 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4968 static type_t *get_default_promoted_type(type_t *orig_type)
4970 type_t *result = orig_type;
4972 type_t *type = skip_typeref(orig_type);
4973 if (is_type_integer(type)) {
4974 result = promote_integer(type);
4975 } else if (type == type_float) {
4976 result = type_double;
4982 static void parse_kr_declaration_list(declaration_t *declaration)
4984 type_t *type = skip_typeref(declaration->type);
4985 if (!is_type_function(type))
4988 if (!type->function.kr_style_parameters)
4991 add_anchor_token('{');
4993 /* push function parameters */
4994 size_t const top = environment_top();
4995 scope_push(&declaration->scope);
4997 declaration_t *parameter = declaration->scope.declarations;
4998 for ( ; parameter != NULL; parameter = parameter->next) {
4999 assert(parameter->parent_scope == NULL);
5000 parameter->parent_scope = scope;
5001 environment_push(parameter);
5004 /* parse declaration list */
5005 while (is_declaration_specifier(&token, false)) {
5006 parse_declaration(finished_kr_declaration);
5009 /* pop function parameters */
5010 assert(scope == &declaration->scope);
5012 environment_pop_to(top);
5014 /* update function type */
5015 type_t *new_type = duplicate_type(type);
5017 function_parameter_t *parameters = NULL;
5018 function_parameter_t *last_parameter = NULL;
5020 declaration_t *parameter_declaration = declaration->scope.declarations;
5021 for( ; parameter_declaration != NULL;
5022 parameter_declaration = parameter_declaration->next) {
5023 type_t *parameter_type = parameter_declaration->type;
5024 if (parameter_type == NULL) {
5026 errorf(HERE, "no type specified for function parameter '%Y'",
5027 parameter_declaration->symbol);
5029 if (warning.implicit_int) {
5030 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
5031 parameter_declaration->symbol);
5033 parameter_type = type_int;
5034 parameter_declaration->type = parameter_type;
5038 semantic_parameter(parameter_declaration);
5039 parameter_type = parameter_declaration->type;
5042 * we need the default promoted types for the function type
5044 parameter_type = get_default_promoted_type(parameter_type);
5046 function_parameter_t *function_parameter
5047 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
5048 memset(function_parameter, 0, sizeof(function_parameter[0]));
5050 function_parameter->type = parameter_type;
5051 if (last_parameter != NULL) {
5052 last_parameter->next = function_parameter;
5054 parameters = function_parameter;
5056 last_parameter = function_parameter;
5059 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
5061 new_type->function.parameters = parameters;
5062 new_type->function.unspecified_parameters = true;
5064 type = typehash_insert(new_type);
5065 if (type != new_type) {
5066 obstack_free(type_obst, new_type);
5069 declaration->type = type;
5071 rem_anchor_token('{');
5074 static bool first_err = true;
5077 * When called with first_err set, prints the name of the current function,
5080 static void print_in_function(void)
5084 diagnosticf("%s: In function '%Y':\n",
5085 current_function->source_position.input_name,
5086 current_function->symbol);
5091 * Check if all labels are defined in the current function.
5092 * Check if all labels are used in the current function.
5094 static void check_labels(void)
5096 for (const goto_statement_t *goto_statement = goto_first;
5097 goto_statement != NULL;
5098 goto_statement = goto_statement->next) {
5099 /* skip computed gotos */
5100 if (goto_statement->expression != NULL)
5103 declaration_t *label = goto_statement->label;
5106 if (label->source_position.input_name == NULL) {
5107 print_in_function();
5108 errorf(&goto_statement->base.source_position,
5109 "label '%Y' used but not defined", label->symbol);
5112 goto_first = goto_last = NULL;
5114 if (warning.unused_label) {
5115 for (const label_statement_t *label_statement = label_first;
5116 label_statement != NULL;
5117 label_statement = label_statement->next) {
5118 const declaration_t *label = label_statement->label;
5120 if (! label->used) {
5121 print_in_function();
5122 warningf(&label_statement->base.source_position,
5123 "label '%Y' defined but not used", label->symbol);
5127 label_first = label_last = NULL;
5130 static void warn_unused_decl(declaration_t *decl, declaration_t *end, char const *const what)
5132 for (; decl != NULL; decl = decl->next) {
5137 print_in_function();
5138 warningf(&decl->source_position, "%s '%Y' is unused", what, decl->symbol);
5139 } else if (!decl->read) {
5140 print_in_function();
5141 warningf(&decl->source_position, "%s '%Y' is never read", what, decl->symbol);
5149 static void check_unused_variables(statement_t *const stmt, void *const env)
5153 switch (stmt->kind) {
5154 case STATEMENT_DECLARATION: {
5155 declaration_statement_t const *const decls = &stmt->declaration;
5156 warn_unused_decl(decls->declarations_begin, decls->declarations_end, "variable");
5161 warn_unused_decl(stmt->fors.scope.declarations, NULL, "variable");
5170 * Check declarations of current_function for unused entities.
5172 static void check_declarations(void)
5174 if (warning.unused_parameter) {
5175 const scope_t *scope = ¤t_function->scope;
5177 /* do not issue unused warnings for main */
5178 if (!is_sym_main(current_function->symbol)) {
5179 warn_unused_decl(scope->declarations, NULL, "parameter");
5182 if (warning.unused_variable) {
5183 walk_statements(current_function->init.statement, check_unused_variables, NULL);
5187 static int determine_truth(expression_t const* const cond)
5190 !is_constant_expression(cond) ? 0 :
5191 fold_constant(cond) != 0 ? 1 :
5195 static bool noreturn_candidate;
5197 static void check_reachable(statement_t *const stmt)
5199 if (stmt->base.reachable)
5201 if (stmt->kind != STATEMENT_DO_WHILE)
5202 stmt->base.reachable = true;
5204 statement_t *last = stmt;
5206 switch (stmt->kind) {
5207 case STATEMENT_INVALID:
5208 case STATEMENT_EMPTY:
5209 case STATEMENT_DECLARATION:
5211 next = stmt->base.next;
5214 case STATEMENT_COMPOUND:
5215 next = stmt->compound.statements;
5218 case STATEMENT_RETURN:
5219 noreturn_candidate = false;
5222 case STATEMENT_IF: {
5223 if_statement_t const* const ifs = &stmt->ifs;
5224 int const val = determine_truth(ifs->condition);
5227 check_reachable(ifs->true_statement);
5232 if (ifs->false_statement != NULL) {
5233 check_reachable(ifs->false_statement);
5237 next = stmt->base.next;
5241 case STATEMENT_SWITCH: {
5242 switch_statement_t const *const switchs = &stmt->switchs;
5243 expression_t const *const expr = switchs->expression;
5245 if (is_constant_expression(expr)) {
5246 long const val = fold_constant(expr);
5247 case_label_statement_t * defaults = NULL;
5248 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5249 if (i->expression == NULL) {
5254 if (i->first_case <= val && val <= i->last_case) {
5255 check_reachable((statement_t*)i);
5260 if (defaults != NULL) {
5261 check_reachable((statement_t*)defaults);
5265 bool has_default = false;
5266 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5267 if (i->expression == NULL)
5270 check_reachable((statement_t*)i);
5277 next = stmt->base.next;
5281 case STATEMENT_EXPRESSION: {
5282 /* Check for noreturn function call */
5283 expression_t const *const expr = stmt->expression.expression;
5284 if (expr->kind == EXPR_CALL) {
5285 expression_t const *const func = expr->call.function;
5286 if (func->kind == EXPR_REFERENCE) {
5287 declaration_t const *const decl = func->reference.declaration;
5288 if (decl != NULL && decl->modifiers & DM_NORETURN) {
5294 next = stmt->base.next;
5298 case STATEMENT_CONTINUE: {
5299 statement_t *parent = stmt;
5301 parent = parent->base.parent;
5302 if (parent == NULL) /* continue not within loop */
5306 switch (parent->kind) {
5307 case STATEMENT_WHILE: goto continue_while;
5308 case STATEMENT_DO_WHILE: goto continue_do_while;
5309 case STATEMENT_FOR: goto continue_for;
5316 case STATEMENT_BREAK: {
5317 statement_t *parent = stmt;
5319 parent = parent->base.parent;
5320 if (parent == NULL) /* break not within loop/switch */
5323 switch (parent->kind) {
5324 case STATEMENT_SWITCH:
5325 case STATEMENT_WHILE:
5326 case STATEMENT_DO_WHILE:
5329 next = parent->base.next;
5330 goto found_break_parent;
5339 case STATEMENT_GOTO:
5340 if (stmt->gotos.expression) {
5341 statement_t *parent = stmt->base.parent;
5342 if (parent == NULL) /* top level goto */
5346 next = stmt->gotos.label->init.statement;
5347 if (next == NULL) /* missing label */
5352 case STATEMENT_LABEL:
5353 next = stmt->label.statement;
5356 case STATEMENT_CASE_LABEL:
5357 next = stmt->case_label.statement;
5360 case STATEMENT_WHILE: {
5361 while_statement_t const *const whiles = &stmt->whiles;
5362 int const val = determine_truth(whiles->condition);
5365 check_reachable(whiles->body);
5370 next = stmt->base.next;
5374 case STATEMENT_DO_WHILE:
5375 next = stmt->do_while.body;
5378 case STATEMENT_FOR: {
5379 for_statement_t *const fors = &stmt->fors;
5381 if (fors->condition_reachable)
5383 fors->condition_reachable = true;
5385 expression_t const *const cond = fors->condition;
5387 cond == NULL ? 1 : determine_truth(cond);
5390 check_reachable(fors->body);
5395 next = stmt->base.next;
5399 case STATEMENT_MS_TRY: {
5400 ms_try_statement_t const *const ms_try = &stmt->ms_try;
5401 check_reachable(ms_try->try_statement);
5402 next = ms_try->final_statement;
5406 case STATEMENT_LEAVE: {
5407 statement_t *parent = stmt;
5409 parent = parent->base.parent;
5410 if (parent == NULL) /* __leave not within __try */
5413 if (parent->kind == STATEMENT_MS_TRY) {
5415 next = parent->ms_try.final_statement;
5423 while (next == NULL) {
5424 next = last->base.parent;
5426 noreturn_candidate = false;
5428 type_t *const type = current_function->type;
5429 assert(is_type_function(type));
5430 type_t *const ret = skip_typeref(type->function.return_type);
5431 if (warning.return_type &&
5432 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
5433 is_type_valid(ret) &&
5434 !is_sym_main(current_function->symbol)) {
5435 warningf(&stmt->base.source_position,
5436 "control reaches end of non-void function");
5441 switch (next->kind) {
5442 case STATEMENT_INVALID:
5443 case STATEMENT_EMPTY:
5444 case STATEMENT_DECLARATION:
5445 case STATEMENT_EXPRESSION:
5447 case STATEMENT_RETURN:
5448 case STATEMENT_CONTINUE:
5449 case STATEMENT_BREAK:
5450 case STATEMENT_GOTO:
5451 case STATEMENT_LEAVE:
5452 panic("invalid control flow in function");
5454 case STATEMENT_COMPOUND:
5456 case STATEMENT_SWITCH:
5457 case STATEMENT_LABEL:
5458 case STATEMENT_CASE_LABEL:
5460 next = next->base.next;
5463 case STATEMENT_WHILE: {
5465 if (next->base.reachable)
5467 next->base.reachable = true;
5469 while_statement_t const *const whiles = &next->whiles;
5470 int const val = determine_truth(whiles->condition);
5473 check_reachable(whiles->body);
5479 next = next->base.next;
5483 case STATEMENT_DO_WHILE: {
5485 if (next->base.reachable)
5487 next->base.reachable = true;
5489 do_while_statement_t const *const dw = &next->do_while;
5490 int const val = determine_truth(dw->condition);
5493 check_reachable(dw->body);
5499 next = next->base.next;
5503 case STATEMENT_FOR: {
5505 for_statement_t *const fors = &next->fors;
5507 fors->step_reachable = true;
5509 if (fors->condition_reachable)
5511 fors->condition_reachable = true;
5513 expression_t const *const cond = fors->condition;
5515 cond == NULL ? 1 : determine_truth(cond);
5518 check_reachable(fors->body);
5524 next = next->base.next;
5528 case STATEMENT_MS_TRY:
5530 next = next->ms_try.final_statement;
5536 next = stmt->base.parent;
5538 warningf(&stmt->base.source_position,
5539 "control reaches end of non-void function");
5543 check_reachable(next);
5546 static void check_unreachable(statement_t* const stmt, void *const env)
5550 switch (stmt->kind) {
5551 case STATEMENT_DO_WHILE:
5552 if (!stmt->base.reachable) {
5553 expression_t const *const cond = stmt->do_while.condition;
5554 if (determine_truth(cond) >= 0) {
5555 warningf(&cond->base.source_position,
5556 "condition of do-while-loop is unreachable");
5561 case STATEMENT_FOR: {
5562 for_statement_t const* const fors = &stmt->fors;
5564 // if init and step are unreachable, cond is unreachable, too
5565 if (!stmt->base.reachable && !fors->step_reachable) {
5566 warningf(&stmt->base.source_position, "statement is unreachable");
5568 if (!stmt->base.reachable && fors->initialisation != NULL) {
5569 warningf(&fors->initialisation->base.source_position,
5570 "initialisation of for-statement is unreachable");
5573 if (!fors->condition_reachable && fors->condition != NULL) {
5574 warningf(&fors->condition->base.source_position,
5575 "condition of for-statement is unreachable");
5578 if (!fors->step_reachable && fors->step != NULL) {
5579 warningf(&fors->step->base.source_position,
5580 "step of for-statement is unreachable");
5586 case STATEMENT_COMPOUND:
5587 if (stmt->compound.statements != NULL)
5592 if (!stmt->base.reachable)
5593 warningf(&stmt->base.source_position, "statement is unreachable");
5598 static void parse_external_declaration(void)
5600 /* function-definitions and declarations both start with declaration
5602 declaration_specifiers_t specifiers;
5603 memset(&specifiers, 0, sizeof(specifiers));
5605 add_anchor_token(';');
5606 parse_declaration_specifiers(&specifiers);
5607 rem_anchor_token(';');
5609 /* must be a declaration */
5610 if (token.type == ';') {
5611 parse_anonymous_declaration_rest(&specifiers);
5615 add_anchor_token(',');
5616 add_anchor_token('=');
5617 add_anchor_token(';');
5618 add_anchor_token('{');
5620 /* declarator is common to both function-definitions and declarations */
5621 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5623 rem_anchor_token('{');
5624 rem_anchor_token(';');
5625 rem_anchor_token('=');
5626 rem_anchor_token(',');
5628 /* must be a declaration */
5629 switch (token.type) {
5633 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5637 /* must be a function definition */
5638 parse_kr_declaration_list(ndeclaration);
5640 if (token.type != '{') {
5641 parse_error_expected("while parsing function definition", '{', NULL);
5642 eat_until_matching_token(';');
5646 type_t *type = ndeclaration->type;
5648 /* note that we don't skip typerefs: the standard doesn't allow them here
5649 * (so we can't use is_type_function here) */
5650 if (type->kind != TYPE_FUNCTION) {
5651 if (is_type_valid(type)) {
5652 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5653 type, ndeclaration->symbol);
5659 if (warning.aggregate_return &&
5660 is_type_compound(skip_typeref(type->function.return_type))) {
5661 warningf(HERE, "function '%Y' returns an aggregate",
5662 ndeclaration->symbol);
5664 if (warning.traditional && !type->function.unspecified_parameters) {
5665 warningf(HERE, "traditional C rejects ISO C style function definition of function '%Y'",
5666 ndeclaration->symbol);
5668 if (warning.old_style_definition && type->function.unspecified_parameters) {
5669 warningf(HERE, "old-style function definition '%Y'",
5670 ndeclaration->symbol);
5673 /* § 6.7.5.3 (14) a function definition with () means no
5674 * parameters (and not unspecified parameters) */
5675 if (type->function.unspecified_parameters
5676 && type->function.parameters == NULL
5677 && !type->function.kr_style_parameters) {
5678 type_t *duplicate = duplicate_type(type);
5679 duplicate->function.unspecified_parameters = false;
5681 type = typehash_insert(duplicate);
5682 if (type != duplicate) {
5683 obstack_free(type_obst, duplicate);
5685 ndeclaration->type = type;
5688 declaration_t *const declaration = record_declaration(ndeclaration, true);
5689 if (ndeclaration != declaration) {
5690 declaration->scope = ndeclaration->scope;
5692 type = skip_typeref(declaration->type);
5694 /* push function parameters and switch scope */
5695 size_t const top = environment_top();
5696 scope_push(&declaration->scope);
5698 declaration_t *parameter = declaration->scope.declarations;
5699 for( ; parameter != NULL; parameter = parameter->next) {
5700 if (parameter->parent_scope == &ndeclaration->scope) {
5701 parameter->parent_scope = scope;
5703 assert(parameter->parent_scope == NULL
5704 || parameter->parent_scope == scope);
5705 parameter->parent_scope = scope;
5706 if (parameter->symbol == NULL) {
5707 errorf(¶meter->source_position, "parameter name omitted");
5710 environment_push(parameter);
5713 if (declaration->init.statement != NULL) {
5714 parser_error_multiple_definition(declaration, HERE);
5717 /* parse function body */
5718 int label_stack_top = label_top();
5719 declaration_t *old_current_function = current_function;
5720 current_function = declaration;
5721 current_parent = NULL;
5723 statement_t *const body = parse_compound_statement(false);
5724 declaration->init.statement = body;
5727 check_declarations();
5728 if (warning.return_type ||
5729 warning.unreachable_code ||
5730 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5731 noreturn_candidate = true;
5732 check_reachable(body);
5733 if (warning.unreachable_code)
5734 walk_statements(body, check_unreachable, NULL);
5735 if (warning.missing_noreturn &&
5736 noreturn_candidate &&
5737 !(declaration->modifiers & DM_NORETURN)) {
5738 warningf(&body->base.source_position,
5739 "function '%#T' is candidate for attribute 'noreturn'",
5740 type, declaration->symbol);
5744 assert(current_parent == NULL);
5745 assert(current_function == declaration);
5746 current_function = old_current_function;
5747 label_pop_to(label_stack_top);
5750 assert(scope == &declaration->scope);
5752 environment_pop_to(top);
5755 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5756 source_position_t *source_position,
5757 const symbol_t *symbol)
5759 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
5761 type->bitfield.base_type = base_type;
5762 type->bitfield.size_expression = size;
5765 type_t *skipped_type = skip_typeref(base_type);
5766 if (!is_type_integer(skipped_type)) {
5767 errorf(HERE, "bitfield base type '%T' is not an integer type",
5771 bit_size = skipped_type->base.size * 8;
5774 if (is_constant_expression(size)) {
5775 long v = fold_constant(size);
5778 errorf(source_position, "negative width in bit-field '%Y'",
5780 } else if (v == 0) {
5781 errorf(source_position, "zero width for bit-field '%Y'",
5783 } else if (bit_size > 0 && (il_size_t)v > bit_size) {
5784 errorf(source_position, "width of '%Y' exceeds its type",
5787 type->bitfield.bit_size = v;
5794 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5797 declaration_t *iter = compound_declaration->scope.declarations;
5798 for( ; iter != NULL; iter = iter->next) {
5799 if (iter->namespc != NAMESPACE_NORMAL)
5802 if (iter->symbol == NULL) {
5803 type_t *type = skip_typeref(iter->type);
5804 if (is_type_compound(type)) {
5805 declaration_t *result
5806 = find_compound_entry(type->compound.declaration, symbol);
5813 if (iter->symbol == symbol) {
5821 static void parse_compound_declarators(declaration_t *struct_declaration,
5822 const declaration_specifiers_t *specifiers)
5824 declaration_t *last_declaration = struct_declaration->scope.declarations;
5825 if (last_declaration != NULL) {
5826 while (last_declaration->next != NULL) {
5827 last_declaration = last_declaration->next;
5832 declaration_t *declaration;
5834 if (token.type == ':') {
5835 source_position_t source_position = *HERE;
5838 type_t *base_type = specifiers->type;
5839 expression_t *size = parse_constant_expression();
5841 type_t *type = make_bitfield_type(base_type, size,
5842 &source_position, sym_anonymous);
5844 declaration = allocate_declaration_zero();
5845 declaration->namespc = NAMESPACE_NORMAL;
5846 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5847 declaration->storage_class = STORAGE_CLASS_NONE;
5848 declaration->source_position = source_position;
5849 declaration->modifiers = specifiers->modifiers;
5850 declaration->type = type;
5852 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5854 type_t *orig_type = declaration->type;
5855 type_t *type = skip_typeref(orig_type);
5857 if (token.type == ':') {
5858 source_position_t source_position = *HERE;
5860 expression_t *size = parse_constant_expression();
5862 type_t *bitfield_type = make_bitfield_type(orig_type, size,
5863 &source_position, declaration->symbol);
5864 declaration->type = bitfield_type;
5866 /* TODO we ignore arrays for now... what is missing is a check
5867 * that they're at the end of the struct */
5868 if (is_type_incomplete(type) && !is_type_array(type)) {
5870 "compound member '%Y' has incomplete type '%T'",
5871 declaration->symbol, orig_type);
5872 } else if (is_type_function(type)) {
5873 errorf(HERE, "compound member '%Y' must not have function type '%T'",
5874 declaration->symbol, orig_type);
5879 /* make sure we don't define a symbol multiple times */
5880 symbol_t *symbol = declaration->symbol;
5881 if (symbol != NULL) {
5882 declaration_t *prev_decl
5883 = find_compound_entry(struct_declaration, symbol);
5885 if (prev_decl != NULL) {
5886 assert(prev_decl->symbol == symbol);
5887 errorf(&declaration->source_position,
5888 "multiple declarations of symbol '%Y' (declared %P)",
5889 symbol, &prev_decl->source_position);
5893 /* append declaration */
5894 if (last_declaration != NULL) {
5895 last_declaration->next = declaration;
5897 struct_declaration->scope.declarations = declaration;
5899 last_declaration = declaration;
5901 if (token.type != ',')
5911 static void parse_compound_type_entries(declaration_t *compound_declaration)
5914 add_anchor_token('}');
5916 while (token.type != '}') {
5917 if (token.type == T_EOF) {
5918 errorf(HERE, "EOF while parsing struct");
5921 declaration_specifiers_t specifiers;
5922 memset(&specifiers, 0, sizeof(specifiers));
5923 parse_declaration_specifiers(&specifiers);
5925 parse_compound_declarators(compound_declaration, &specifiers);
5927 rem_anchor_token('}');
5931 static type_t *parse_typename(void)
5933 declaration_specifiers_t specifiers;
5934 memset(&specifiers, 0, sizeof(specifiers));
5935 parse_declaration_specifiers(&specifiers);
5936 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5937 /* TODO: improve error message, user does probably not know what a
5938 * storage class is...
5940 errorf(HERE, "typename may not have a storage class");
5943 type_t *result = parse_abstract_declarator(specifiers.type);
5951 typedef expression_t* (*parse_expression_function) (unsigned precedence);
5952 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
5953 expression_t *left);
5955 typedef struct expression_parser_function_t expression_parser_function_t;
5956 struct expression_parser_function_t {
5957 unsigned precedence;
5958 parse_expression_function parser;
5959 unsigned infix_precedence;
5960 parse_expression_infix_function infix_parser;
5963 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5966 * Prints an error message if an expression was expected but not read
5968 static expression_t *expected_expression_error(void)
5970 /* skip the error message if the error token was read */
5971 if (token.type != T_ERROR) {
5972 errorf(HERE, "expected expression, got token '%K'", &token);
5976 return create_invalid_expression();
5980 * Parse a string constant.
5982 static expression_t *parse_string_const(void)
5985 if (token.type == T_STRING_LITERAL) {
5986 string_t res = token.v.string;
5988 while (token.type == T_STRING_LITERAL) {
5989 res = concat_strings(&res, &token.v.string);
5992 if (token.type != T_WIDE_STRING_LITERAL) {
5993 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
5994 /* note: that we use type_char_ptr here, which is already the
5995 * automatic converted type. revert_automatic_type_conversion
5996 * will construct the array type */
5997 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
5998 cnst->string.value = res;
6002 wres = concat_string_wide_string(&res, &token.v.wide_string);
6004 wres = token.v.wide_string;
6009 switch (token.type) {
6010 case T_WIDE_STRING_LITERAL:
6011 wres = concat_wide_strings(&wres, &token.v.wide_string);
6014 case T_STRING_LITERAL:
6015 wres = concat_wide_string_string(&wres, &token.v.string);
6019 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
6020 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
6021 cnst->wide_string.value = wres;
6030 * Parse an integer constant.
6032 static expression_t *parse_int_const(void)
6034 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6035 cnst->base.source_position = *HERE;
6036 cnst->base.type = token.datatype;
6037 cnst->conste.v.int_value = token.v.intvalue;
6045 * Parse a character constant.
6047 static expression_t *parse_character_constant(void)
6049 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
6051 cnst->base.source_position = *HERE;
6052 cnst->base.type = token.datatype;
6053 cnst->conste.v.character = token.v.string;
6055 if (cnst->conste.v.character.size != 1) {
6056 if (warning.multichar && GNU_MODE) {
6057 warningf(HERE, "multi-character character constant");
6059 errorf(HERE, "more than 1 characters in character constant");
6068 * Parse a wide character constant.
6070 static expression_t *parse_wide_character_constant(void)
6072 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
6074 cnst->base.source_position = *HERE;
6075 cnst->base.type = token.datatype;
6076 cnst->conste.v.wide_character = token.v.wide_string;
6078 if (cnst->conste.v.wide_character.size != 1) {
6079 if (warning.multichar && GNU_MODE) {
6080 warningf(HERE, "multi-character character constant");
6082 errorf(HERE, "more than 1 characters in character constant");
6091 * Parse a float constant.
6093 static expression_t *parse_float_const(void)
6095 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6096 cnst->base.type = token.datatype;
6097 cnst->conste.v.float_value = token.v.floatvalue;
6104 static declaration_t *create_implicit_function(symbol_t *symbol,
6105 const source_position_t *source_position)
6107 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
6108 ntype->function.return_type = type_int;
6109 ntype->function.unspecified_parameters = true;
6111 type_t *type = typehash_insert(ntype);
6112 if (type != ntype) {
6116 declaration_t *const declaration = allocate_declaration_zero();
6117 declaration->storage_class = STORAGE_CLASS_EXTERN;
6118 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
6119 declaration->type = type;
6120 declaration->symbol = symbol;
6121 declaration->source_position = *source_position;
6122 declaration->implicit = true;
6124 bool strict_prototypes_old = warning.strict_prototypes;
6125 warning.strict_prototypes = false;
6126 record_declaration(declaration, false);
6127 warning.strict_prototypes = strict_prototypes_old;
6133 * Creates a return_type (func)(argument_type) function type if not
6136 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
6137 type_t *argument_type2)
6139 function_parameter_t *parameter2
6140 = obstack_alloc(type_obst, sizeof(parameter2[0]));
6141 memset(parameter2, 0, sizeof(parameter2[0]));
6142 parameter2->type = argument_type2;
6144 function_parameter_t *parameter1
6145 = obstack_alloc(type_obst, sizeof(parameter1[0]));
6146 memset(parameter1, 0, sizeof(parameter1[0]));
6147 parameter1->type = argument_type1;
6148 parameter1->next = parameter2;
6150 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6151 type->function.return_type = return_type;
6152 type->function.parameters = parameter1;
6154 type_t *result = typehash_insert(type);
6155 if (result != type) {
6163 * Creates a return_type (func)(argument_type) function type if not
6166 * @param return_type the return type
6167 * @param argument_type the argument type
6169 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
6171 function_parameter_t *parameter
6172 = obstack_alloc(type_obst, sizeof(parameter[0]));
6173 memset(parameter, 0, sizeof(parameter[0]));
6174 parameter->type = argument_type;
6176 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6177 type->function.return_type = return_type;
6178 type->function.parameters = parameter;
6180 type_t *result = typehash_insert(type);
6181 if (result != type) {
6188 static type_t *make_function_0_type(type_t *return_type)
6190 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
6191 type->function.return_type = return_type;
6192 type->function.parameters = NULL;
6194 type_t *result = typehash_insert(type);
6195 if (result != type) {
6203 * Creates a function type for some function like builtins.
6205 * @param symbol the symbol describing the builtin
6207 static type_t *get_builtin_symbol_type(symbol_t *symbol)
6209 switch(symbol->ID) {
6210 case T___builtin_alloca:
6211 return make_function_1_type(type_void_ptr, type_size_t);
6212 case T___builtin_huge_val:
6213 return make_function_0_type(type_double);
6214 case T___builtin_inf:
6215 return make_function_0_type(type_double);
6216 case T___builtin_inff:
6217 return make_function_0_type(type_float);
6218 case T___builtin_infl:
6219 return make_function_0_type(type_long_double);
6220 case T___builtin_nan:
6221 return make_function_1_type(type_double, type_char_ptr);
6222 case T___builtin_nanf:
6223 return make_function_1_type(type_float, type_char_ptr);
6224 case T___builtin_nanl:
6225 return make_function_1_type(type_long_double, type_char_ptr);
6226 case T___builtin_va_end:
6227 return make_function_1_type(type_void, type_valist);
6228 case T___builtin_expect:
6229 return make_function_2_type(type_long, type_long, type_long);
6231 internal_errorf(HERE, "not implemented builtin symbol found");
6236 * Performs automatic type cast as described in § 6.3.2.1.
6238 * @param orig_type the original type
6240 static type_t *automatic_type_conversion(type_t *orig_type)
6242 type_t *type = skip_typeref(orig_type);
6243 if (is_type_array(type)) {
6244 array_type_t *array_type = &type->array;
6245 type_t *element_type = array_type->element_type;
6246 unsigned qualifiers = array_type->base.qualifiers;
6248 return make_pointer_type(element_type, qualifiers);
6251 if (is_type_function(type)) {
6252 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6259 * reverts the automatic casts of array to pointer types and function
6260 * to function-pointer types as defined § 6.3.2.1
6262 type_t *revert_automatic_type_conversion(const expression_t *expression)
6264 switch (expression->kind) {
6265 case EXPR_REFERENCE: return expression->reference.declaration->type;
6268 return get_qualified_type(expression->select.compound_entry->type,
6269 expression->base.type->base.qualifiers);
6271 case EXPR_UNARY_DEREFERENCE: {
6272 const expression_t *const value = expression->unary.value;
6273 type_t *const type = skip_typeref(value->base.type);
6274 assert(is_type_pointer(type));
6275 return type->pointer.points_to;
6278 case EXPR_BUILTIN_SYMBOL:
6279 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
6281 case EXPR_ARRAY_ACCESS: {
6282 const expression_t *array_ref = expression->array_access.array_ref;
6283 type_t *type_left = skip_typeref(array_ref->base.type);
6284 if (!is_type_valid(type_left))
6286 assert(is_type_pointer(type_left));
6287 return type_left->pointer.points_to;
6290 case EXPR_STRING_LITERAL: {
6291 size_t size = expression->string.value.size;
6292 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
6295 case EXPR_WIDE_STRING_LITERAL: {
6296 size_t size = expression->wide_string.value.size;
6297 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
6300 case EXPR_COMPOUND_LITERAL:
6301 return expression->compound_literal.type;
6306 return expression->base.type;
6309 static expression_t *parse_reference(void)
6311 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
6313 reference_expression_t *ref = &expression->reference;
6314 symbol_t *const symbol = token.v.symbol;
6316 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
6318 if (declaration == NULL) {
6319 if (!strict_mode && look_ahead(1)->type == '(') {
6320 /* an implicitly declared function */
6321 if (warning.implicit_function_declaration) {
6322 warningf(HERE, "implicit declaration of function '%Y'",
6326 declaration = create_implicit_function(symbol, HERE);
6328 errorf(HERE, "unknown symbol '%Y' found.", symbol);
6329 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
6333 type_t *orig_type = declaration->type;
6335 /* we always do the auto-type conversions; the & and sizeof parser contains
6336 * code to revert this! */
6337 type_t *type = automatic_type_conversion(orig_type);
6339 ref->declaration = declaration;
6340 ref->base.type = type;
6342 /* this declaration is used */
6343 declaration->used = true;
6345 if (declaration->parent_scope != file_scope &&
6346 declaration->parent_scope->depth < current_function->scope.depth &&
6347 is_type_valid(orig_type) && !is_type_function(orig_type)) {
6348 /* access of a variable from an outer function */
6349 declaration->address_taken = true;
6350 ref->is_outer_ref = true;
6351 current_function->need_closure = true;
6354 /* check for deprecated functions */
6355 if (warning.deprecated_declarations &&
6356 declaration->modifiers & DM_DEPRECATED) {
6357 char const *const prefix = is_type_function(declaration->type) ?
6358 "function" : "variable";
6360 if (declaration->deprecated_string != NULL) {
6361 warningf(HERE, "%s '%Y' is deprecated (declared %P): \"%s\"",
6362 prefix, declaration->symbol, &declaration->source_position,
6363 declaration->deprecated_string);
6365 warningf(HERE, "%s '%Y' is deprecated (declared %P)", prefix,
6366 declaration->symbol, &declaration->source_position);
6369 if (warning.init_self && declaration == current_init_decl && !in_type_prop) {
6370 current_init_decl = NULL;
6371 warningf(HERE, "variable '%#T' is initialized by itself",
6372 declaration->type, declaration->symbol);
6379 static bool semantic_cast(expression_t *cast)
6381 expression_t *expression = cast->unary.value;
6382 type_t *orig_dest_type = cast->base.type;
6383 type_t *orig_type_right = expression->base.type;
6384 type_t const *dst_type = skip_typeref(orig_dest_type);
6385 type_t const *src_type = skip_typeref(orig_type_right);
6386 source_position_t const *pos = &cast->base.source_position;
6388 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
6389 if (dst_type == type_void)
6392 /* only integer and pointer can be casted to pointer */
6393 if (is_type_pointer(dst_type) &&
6394 !is_type_pointer(src_type) &&
6395 !is_type_integer(src_type) &&
6396 is_type_valid(src_type)) {
6397 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
6401 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
6402 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
6406 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
6407 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
6411 if (warning.cast_qual &&
6412 is_type_pointer(src_type) &&
6413 is_type_pointer(dst_type)) {
6414 type_t *src = skip_typeref(src_type->pointer.points_to);
6415 type_t *dst = skip_typeref(dst_type->pointer.points_to);
6416 unsigned missing_qualifiers =
6417 src->base.qualifiers & ~dst->base.qualifiers;
6418 if (missing_qualifiers != 0) {
6420 "cast discards qualifiers '%Q' in pointer target type of '%T'",
6421 missing_qualifiers, orig_type_right);
6427 static expression_t *parse_compound_literal(type_t *type)
6429 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
6431 parse_initializer_env_t env;
6433 env.declaration = NULL;
6434 env.must_be_constant = false;
6435 initializer_t *initializer = parse_initializer(&env);
6438 expression->compound_literal.initializer = initializer;
6439 expression->compound_literal.type = type;
6440 expression->base.type = automatic_type_conversion(type);
6446 * Parse a cast expression.
6448 static expression_t *parse_cast(void)
6450 add_anchor_token(')');
6452 source_position_t source_position = token.source_position;
6454 type_t *type = parse_typename();
6456 rem_anchor_token(')');
6459 if (token.type == '{') {
6460 return parse_compound_literal(type);
6463 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6464 cast->base.source_position = source_position;
6466 expression_t *value = parse_sub_expression(20);
6467 cast->base.type = type;
6468 cast->unary.value = value;
6470 if (! semantic_cast(cast)) {
6471 /* TODO: record the error in the AST. else it is impossible to detect it */
6476 return create_invalid_expression();
6480 * Parse a statement expression.
6482 static expression_t *parse_statement_expression(void)
6484 add_anchor_token(')');
6486 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6488 statement_t *statement = parse_compound_statement(true);
6489 expression->statement.statement = statement;
6490 expression->base.source_position = statement->base.source_position;
6492 /* find last statement and use its type */
6493 type_t *type = type_void;
6494 const statement_t *stmt = statement->compound.statements;
6496 while (stmt->base.next != NULL)
6497 stmt = stmt->base.next;
6499 if (stmt->kind == STATEMENT_EXPRESSION) {
6500 type = stmt->expression.expression->base.type;
6503 warningf(&expression->base.source_position, "empty statement expression ({})");
6505 expression->base.type = type;
6507 rem_anchor_token(')');
6515 * Parse a parenthesized expression.
6517 static expression_t *parse_parenthesized_expression(void)
6521 switch(token.type) {
6523 /* gcc extension: a statement expression */
6524 return parse_statement_expression();
6528 return parse_cast();
6530 if (is_typedef_symbol(token.v.symbol)) {
6531 return parse_cast();
6535 add_anchor_token(')');
6536 expression_t *result = parse_expression();
6537 rem_anchor_token(')');
6544 static expression_t *parse_function_keyword(void)
6549 if (current_function == NULL) {
6550 errorf(HERE, "'__func__' used outside of a function");
6553 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6554 expression->base.type = type_char_ptr;
6555 expression->funcname.kind = FUNCNAME_FUNCTION;
6560 static expression_t *parse_pretty_function_keyword(void)
6562 eat(T___PRETTY_FUNCTION__);
6564 if (current_function == NULL) {
6565 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6568 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6569 expression->base.type = type_char_ptr;
6570 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6575 static expression_t *parse_funcsig_keyword(void)
6579 if (current_function == NULL) {
6580 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6583 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6584 expression->base.type = type_char_ptr;
6585 expression->funcname.kind = FUNCNAME_FUNCSIG;
6590 static expression_t *parse_funcdname_keyword(void)
6592 eat(T___FUNCDNAME__);
6594 if (current_function == NULL) {
6595 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6598 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6599 expression->base.type = type_char_ptr;
6600 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6605 static designator_t *parse_designator(void)
6607 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6608 result->source_position = *HERE;
6610 if (token.type != T_IDENTIFIER) {
6611 parse_error_expected("while parsing member designator",
6612 T_IDENTIFIER, NULL);
6615 result->symbol = token.v.symbol;
6618 designator_t *last_designator = result;
6620 if (token.type == '.') {
6622 if (token.type != T_IDENTIFIER) {
6623 parse_error_expected("while parsing member designator",
6624 T_IDENTIFIER, NULL);
6627 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6628 designator->source_position = *HERE;
6629 designator->symbol = token.v.symbol;
6632 last_designator->next = designator;
6633 last_designator = designator;
6636 if (token.type == '[') {
6638 add_anchor_token(']');
6639 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6640 designator->source_position = *HERE;
6641 designator->array_index = parse_expression();
6642 rem_anchor_token(']');
6644 if (designator->array_index == NULL) {
6648 last_designator->next = designator;
6649 last_designator = designator;
6661 * Parse the __builtin_offsetof() expression.
6663 static expression_t *parse_offsetof(void)
6665 eat(T___builtin_offsetof);
6667 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6668 expression->base.type = type_size_t;
6671 add_anchor_token(',');
6672 type_t *type = parse_typename();
6673 rem_anchor_token(',');
6675 add_anchor_token(')');
6676 designator_t *designator = parse_designator();
6677 rem_anchor_token(')');
6680 expression->offsetofe.type = type;
6681 expression->offsetofe.designator = designator;
6684 memset(&path, 0, sizeof(path));
6685 path.top_type = type;
6686 path.path = NEW_ARR_F(type_path_entry_t, 0);
6688 descend_into_subtype(&path);
6690 if (!walk_designator(&path, designator, true)) {
6691 return create_invalid_expression();
6694 DEL_ARR_F(path.path);
6698 return create_invalid_expression();
6702 * Parses a _builtin_va_start() expression.
6704 static expression_t *parse_va_start(void)
6706 eat(T___builtin_va_start);
6708 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6711 add_anchor_token(',');
6712 expression->va_starte.ap = parse_assignment_expression();
6713 rem_anchor_token(',');
6715 expression_t *const expr = parse_assignment_expression();
6716 if (expr->kind == EXPR_REFERENCE) {
6717 declaration_t *const decl = expr->reference.declaration;
6718 if (decl->parent_scope != ¤t_function->scope || decl->next != NULL) {
6719 errorf(&expr->base.source_position,
6720 "second argument of 'va_start' must be last parameter of the current function");
6722 expression->va_starte.parameter = decl;
6728 return create_invalid_expression();
6732 * Parses a _builtin_va_arg() expression.
6734 static expression_t *parse_va_arg(void)
6736 eat(T___builtin_va_arg);
6738 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6741 expression->va_arge.ap = parse_assignment_expression();
6743 expression->base.type = parse_typename();
6748 return create_invalid_expression();
6751 static expression_t *parse_builtin_symbol(void)
6753 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6755 symbol_t *symbol = token.v.symbol;
6757 expression->builtin_symbol.symbol = symbol;
6760 type_t *type = get_builtin_symbol_type(symbol);
6761 type = automatic_type_conversion(type);
6763 expression->base.type = type;
6768 * Parses a __builtin_constant() expression.
6770 static expression_t *parse_builtin_constant(void)
6772 eat(T___builtin_constant_p);
6774 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6777 add_anchor_token(')');
6778 expression->builtin_constant.value = parse_assignment_expression();
6779 rem_anchor_token(')');
6781 expression->base.type = type_int;
6785 return create_invalid_expression();
6789 * Parses a __builtin_prefetch() expression.
6791 static expression_t *parse_builtin_prefetch(void)
6793 eat(T___builtin_prefetch);
6795 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6798 add_anchor_token(')');
6799 expression->builtin_prefetch.adr = parse_assignment_expression();
6800 if (token.type == ',') {
6802 expression->builtin_prefetch.rw = parse_assignment_expression();
6804 if (token.type == ',') {
6806 expression->builtin_prefetch.locality = parse_assignment_expression();
6808 rem_anchor_token(')');
6810 expression->base.type = type_void;
6814 return create_invalid_expression();
6818 * Parses a __builtin_is_*() compare expression.
6820 static expression_t *parse_compare_builtin(void)
6822 expression_t *expression;
6824 switch(token.type) {
6825 case T___builtin_isgreater:
6826 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6828 case T___builtin_isgreaterequal:
6829 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6831 case T___builtin_isless:
6832 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6834 case T___builtin_islessequal:
6835 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6837 case T___builtin_islessgreater:
6838 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6840 case T___builtin_isunordered:
6841 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6844 internal_errorf(HERE, "invalid compare builtin found");
6846 expression->base.source_position = *HERE;
6850 expression->binary.left = parse_assignment_expression();
6852 expression->binary.right = parse_assignment_expression();
6855 type_t *const orig_type_left = expression->binary.left->base.type;
6856 type_t *const orig_type_right = expression->binary.right->base.type;
6858 type_t *const type_left = skip_typeref(orig_type_left);
6859 type_t *const type_right = skip_typeref(orig_type_right);
6860 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6861 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6862 type_error_incompatible("invalid operands in comparison",
6863 &expression->base.source_position, orig_type_left, orig_type_right);
6866 semantic_comparison(&expression->binary);
6871 return create_invalid_expression();
6876 * Parses a __builtin_expect() expression.
6878 static expression_t *parse_builtin_expect(void)
6880 eat(T___builtin_expect);
6882 expression_t *expression
6883 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6886 expression->binary.left = parse_assignment_expression();
6888 expression->binary.right = parse_constant_expression();
6891 expression->base.type = expression->binary.left->base.type;
6895 return create_invalid_expression();
6900 * Parses a MS assume() expression.
6902 static expression_t *parse_assume(void)
6906 expression_t *expression
6907 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6910 add_anchor_token(')');
6911 expression->unary.value = parse_assignment_expression();
6912 rem_anchor_token(')');
6915 expression->base.type = type_void;
6918 return create_invalid_expression();
6922 * Return the declaration for a given label symbol or create a new one.
6924 * @param symbol the symbol of the label
6926 static declaration_t *get_label(symbol_t *symbol)
6928 declaration_t *candidate;
6929 assert(current_function != NULL);
6931 candidate = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
6932 /* if we found a local label, we already created the declaration */
6933 if (candidate != NULL) {
6934 if (candidate->parent_scope != scope) {
6935 assert(candidate->parent_scope->depth < scope->depth);
6936 current_function->goto_to_outer = true;
6941 candidate = get_declaration(symbol, NAMESPACE_LABEL);
6942 /* if we found a label in the same function, then we already created the
6944 if (candidate != NULL
6945 && candidate->parent_scope == ¤t_function->scope) {
6949 /* otherwise we need to create a new one */
6950 declaration_t *const declaration = allocate_declaration_zero();
6951 declaration->namespc = NAMESPACE_LABEL;
6952 declaration->symbol = symbol;
6954 label_push(declaration);
6960 * Parses a GNU && label address expression.
6962 static expression_t *parse_label_address(void)
6964 source_position_t source_position = token.source_position;
6966 if (token.type != T_IDENTIFIER) {
6967 parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
6970 symbol_t *symbol = token.v.symbol;
6973 declaration_t *label = get_label(symbol);
6976 label->address_taken = true;
6978 expression_t *expression = allocate_expression_zero(EXPR_LABEL_ADDRESS);
6979 expression->base.source_position = source_position;
6981 /* label address is threaten as a void pointer */
6982 expression->base.type = type_void_ptr;
6983 expression->label_address.declaration = label;
6986 return create_invalid_expression();
6990 * Parse a microsoft __noop expression.
6992 static expression_t *parse_noop_expression(void)
6994 source_position_t source_position = *HERE;
6997 if (token.type == '(') {
6998 /* parse arguments */
7000 add_anchor_token(')');
7001 add_anchor_token(',');
7003 if (token.type != ')') {
7005 (void)parse_assignment_expression();
7006 if (token.type != ',')
7012 rem_anchor_token(',');
7013 rem_anchor_token(')');
7016 /* the result is a (int)0 */
7017 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
7018 cnst->base.source_position = source_position;
7019 cnst->base.type = type_int;
7020 cnst->conste.v.int_value = 0;
7021 cnst->conste.is_ms_noop = true;
7026 return create_invalid_expression();
7030 * Parses a primary expression.
7032 static expression_t *parse_primary_expression(void)
7034 switch (token.type) {
7035 case T_INTEGER: return parse_int_const();
7036 case T_CHARACTER_CONSTANT: return parse_character_constant();
7037 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
7038 case T_FLOATINGPOINT: return parse_float_const();
7039 case T_STRING_LITERAL:
7040 case T_WIDE_STRING_LITERAL: return parse_string_const();
7041 case T_IDENTIFIER: return parse_reference();
7042 case T___FUNCTION__:
7043 case T___func__: return parse_function_keyword();
7044 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
7045 case T___FUNCSIG__: return parse_funcsig_keyword();
7046 case T___FUNCDNAME__: return parse_funcdname_keyword();
7047 case T___builtin_offsetof: return parse_offsetof();
7048 case T___builtin_va_start: return parse_va_start();
7049 case T___builtin_va_arg: return parse_va_arg();
7050 case T___builtin_expect:
7051 case T___builtin_alloca:
7052 case T___builtin_inf:
7053 case T___builtin_inff:
7054 case T___builtin_infl:
7055 case T___builtin_nan:
7056 case T___builtin_nanf:
7057 case T___builtin_nanl:
7058 case T___builtin_huge_val:
7059 case T___builtin_va_end: return parse_builtin_symbol();
7060 case T___builtin_isgreater:
7061 case T___builtin_isgreaterequal:
7062 case T___builtin_isless:
7063 case T___builtin_islessequal:
7064 case T___builtin_islessgreater:
7065 case T___builtin_isunordered: return parse_compare_builtin();
7066 case T___builtin_constant_p: return parse_builtin_constant();
7067 case T___builtin_prefetch: return parse_builtin_prefetch();
7068 case T__assume: return parse_assume();
7071 return parse_label_address();
7074 case '(': return parse_parenthesized_expression();
7075 case T___noop: return parse_noop_expression();
7078 errorf(HERE, "unexpected token %K, expected an expression", &token);
7079 return create_invalid_expression();
7083 * Check if the expression has the character type and issue a warning then.
7085 static void check_for_char_index_type(const expression_t *expression)
7087 type_t *const type = expression->base.type;
7088 const type_t *const base_type = skip_typeref(type);
7090 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
7091 warning.char_subscripts) {
7092 warningf(&expression->base.source_position,
7093 "array subscript has type '%T'", type);
7097 static expression_t *parse_array_expression(unsigned precedence,
7103 add_anchor_token(']');
7105 expression_t *inside = parse_expression();
7107 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
7109 array_access_expression_t *array_access = &expression->array_access;
7111 type_t *const orig_type_left = left->base.type;
7112 type_t *const orig_type_inside = inside->base.type;
7114 type_t *const type_left = skip_typeref(orig_type_left);
7115 type_t *const type_inside = skip_typeref(orig_type_inside);
7117 type_t *return_type;
7118 if (is_type_pointer(type_left)) {
7119 return_type = type_left->pointer.points_to;
7120 array_access->array_ref = left;
7121 array_access->index = inside;
7122 check_for_char_index_type(inside);
7123 } else if (is_type_pointer(type_inside)) {
7124 return_type = type_inside->pointer.points_to;
7125 array_access->array_ref = inside;
7126 array_access->index = left;
7127 array_access->flipped = true;
7128 check_for_char_index_type(left);
7130 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
7132 "array access on object with non-pointer types '%T', '%T'",
7133 orig_type_left, orig_type_inside);
7135 return_type = type_error_type;
7136 array_access->array_ref = left;
7137 array_access->index = inside;
7140 expression->base.type = automatic_type_conversion(return_type);
7142 rem_anchor_token(']');
7143 if (token.type == ']') {
7146 parse_error_expected("Problem while parsing array access", ']', NULL);
7151 static expression_t *parse_typeprop(expression_kind_t const kind,
7152 source_position_t const pos,
7153 unsigned const precedence)
7155 expression_t *tp_expression = allocate_expression_zero(kind);
7156 tp_expression->base.type = type_size_t;
7157 tp_expression->base.source_position = pos;
7159 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
7161 /* we only refer to a type property, mark this case */
7162 bool old = in_type_prop;
7163 in_type_prop = true;
7164 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
7166 add_anchor_token(')');
7167 type_t* const orig_type = parse_typename();
7168 tp_expression->typeprop.type = orig_type;
7170 type_t const* const type = skip_typeref(orig_type);
7171 char const* const wrong_type =
7172 is_type_incomplete(type) ? "incomplete" :
7173 type->kind == TYPE_FUNCTION ? "function designator" :
7174 type->kind == TYPE_BITFIELD ? "bitfield" :
7176 if (wrong_type != NULL) {
7177 errorf(&pos, "operand of %s expression must not be %s type '%T'",
7178 what, wrong_type, type);
7181 rem_anchor_token(')');
7184 expression_t *expression = parse_sub_expression(precedence);
7186 type_t* const orig_type = revert_automatic_type_conversion(expression);
7187 expression->base.type = orig_type;
7189 type_t const* const type = skip_typeref(orig_type);
7190 char const* const wrong_type =
7191 is_type_incomplete(type) ? "incomplete" :
7192 type->kind == TYPE_FUNCTION ? "function designator" :
7193 type->kind == TYPE_BITFIELD ? "bitfield" :
7195 if (wrong_type != NULL) {
7196 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
7199 tp_expression->typeprop.type = expression->base.type;
7200 tp_expression->typeprop.tp_expression = expression;
7205 return tp_expression;
7208 static expression_t *parse_sizeof(unsigned precedence)
7210 source_position_t pos = *HERE;
7212 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
7215 static expression_t *parse_alignof(unsigned precedence)
7217 source_position_t pos = *HERE;
7219 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
7222 static expression_t *parse_select_expression(unsigned precedence,
7223 expression_t *compound)
7226 assert(token.type == '.' || token.type == T_MINUSGREATER);
7228 bool is_pointer = (token.type == T_MINUSGREATER);
7231 expression_t *select = allocate_expression_zero(EXPR_SELECT);
7232 select->select.compound = compound;
7234 if (token.type != T_IDENTIFIER) {
7235 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
7238 symbol_t *symbol = token.v.symbol;
7241 type_t *const orig_type = compound->base.type;
7242 type_t *const type = skip_typeref(orig_type);
7245 bool saw_error = false;
7246 if (is_type_pointer(type)) {
7249 "request for member '%Y' in something not a struct or union, but '%T'",
7253 type_left = skip_typeref(type->pointer.points_to);
7255 if (is_pointer && is_type_valid(type)) {
7256 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
7262 declaration_t *entry;
7263 if (type_left->kind == TYPE_COMPOUND_STRUCT ||
7264 type_left->kind == TYPE_COMPOUND_UNION) {
7265 declaration_t *const declaration = type_left->compound.declaration;
7267 if (!declaration->init.complete) {
7268 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
7270 goto create_error_entry;
7273 entry = find_compound_entry(declaration, symbol);
7274 if (entry == NULL) {
7275 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
7276 goto create_error_entry;
7279 if (is_type_valid(type_left) && !saw_error) {
7281 "request for member '%Y' in something not a struct or union, but '%T'",
7285 entry = allocate_declaration_zero();
7286 entry->symbol = symbol;
7289 select->select.compound_entry = entry;
7291 type_t *const res_type =
7292 get_qualified_type(entry->type, type_left->base.qualifiers);
7294 /* we always do the auto-type conversions; the & and sizeof parser contains
7295 * code to revert this! */
7296 select->base.type = automatic_type_conversion(res_type);
7298 type_t *skipped = skip_typeref(res_type);
7299 if (skipped->kind == TYPE_BITFIELD) {
7300 select->base.type = skipped->bitfield.base_type;
7306 static void check_call_argument(const function_parameter_t *parameter,
7307 call_argument_t *argument, unsigned pos)
7309 type_t *expected_type = parameter->type;
7310 type_t *expected_type_skip = skip_typeref(expected_type);
7311 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
7312 expression_t *arg_expr = argument->expression;
7313 type_t *arg_type = skip_typeref(arg_expr->base.type);
7315 /* handle transparent union gnu extension */
7316 if (is_type_union(expected_type_skip)
7317 && (expected_type_skip->base.modifiers
7318 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
7319 declaration_t *union_decl = expected_type_skip->compound.declaration;
7321 declaration_t *declaration = union_decl->scope.declarations;
7322 type_t *best_type = NULL;
7323 for ( ; declaration != NULL; declaration = declaration->next) {
7324 type_t *decl_type = declaration->type;
7325 error = semantic_assign(decl_type, arg_expr);
7326 if (error == ASSIGN_ERROR_INCOMPATIBLE
7327 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
7330 if (error == ASSIGN_SUCCESS) {
7331 best_type = decl_type;
7332 } else if (best_type == NULL) {
7333 best_type = decl_type;
7337 if (best_type != NULL) {
7338 expected_type = best_type;
7342 error = semantic_assign(expected_type, arg_expr);
7343 argument->expression = create_implicit_cast(argument->expression,
7346 if (error != ASSIGN_SUCCESS) {
7347 /* report exact scope in error messages (like "in argument 3") */
7349 snprintf(buf, sizeof(buf), "call argument %u", pos);
7350 report_assign_error(error, expected_type, arg_expr, buf,
7351 &arg_expr->base.source_position);
7352 } else if (warning.traditional || warning.conversion) {
7353 type_t *const promoted_type = get_default_promoted_type(arg_type);
7354 if (!types_compatible(expected_type_skip, promoted_type) &&
7355 !types_compatible(expected_type_skip, type_void_ptr) &&
7356 !types_compatible(type_void_ptr, promoted_type)) {
7357 /* Deliberately show the skipped types in this warning */
7358 warningf(&arg_expr->base.source_position,
7359 "passing call argument %u as '%T' rather than '%T' due to prototype",
7360 pos, expected_type_skip, promoted_type);
7366 * Parse a call expression, ie. expression '( ... )'.
7368 * @param expression the function address
7370 static expression_t *parse_call_expression(unsigned precedence,
7371 expression_t *expression)
7374 expression_t *result = allocate_expression_zero(EXPR_CALL);
7375 result->base.source_position = expression->base.source_position;
7377 call_expression_t *call = &result->call;
7378 call->function = expression;
7380 type_t *const orig_type = expression->base.type;
7381 type_t *const type = skip_typeref(orig_type);
7383 function_type_t *function_type = NULL;
7384 if (is_type_pointer(type)) {
7385 type_t *const to_type = skip_typeref(type->pointer.points_to);
7387 if (is_type_function(to_type)) {
7388 function_type = &to_type->function;
7389 call->base.type = function_type->return_type;
7393 if (function_type == NULL && is_type_valid(type)) {
7394 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
7397 /* parse arguments */
7399 add_anchor_token(')');
7400 add_anchor_token(',');
7402 if (token.type != ')') {
7403 call_argument_t *last_argument = NULL;
7406 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7408 argument->expression = parse_assignment_expression();
7409 if (last_argument == NULL) {
7410 call->arguments = argument;
7412 last_argument->next = argument;
7414 last_argument = argument;
7416 if (token.type != ',')
7421 rem_anchor_token(',');
7422 rem_anchor_token(')');
7425 if (function_type == NULL)
7428 function_parameter_t *parameter = function_type->parameters;
7429 call_argument_t *argument = call->arguments;
7430 if (!function_type->unspecified_parameters) {
7431 for (unsigned pos = 0; parameter != NULL && argument != NULL;
7432 parameter = parameter->next, argument = argument->next) {
7433 check_call_argument(parameter, argument, ++pos);
7436 if (parameter != NULL) {
7437 errorf(HERE, "too few arguments to function '%E'", expression);
7438 } else if (argument != NULL && !function_type->variadic) {
7439 errorf(HERE, "too many arguments to function '%E'", expression);
7443 /* do default promotion */
7444 for( ; argument != NULL; argument = argument->next) {
7445 type_t *type = argument->expression->base.type;
7447 type = get_default_promoted_type(type);
7449 argument->expression
7450 = create_implicit_cast(argument->expression, type);
7453 check_format(&result->call);
7455 if (warning.aggregate_return &&
7456 is_type_compound(skip_typeref(function_type->return_type))) {
7457 warningf(&result->base.source_position,
7458 "function call has aggregate value");
7465 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
7467 static bool same_compound_type(const type_t *type1, const type_t *type2)
7470 is_type_compound(type1) &&
7471 type1->kind == type2->kind &&
7472 type1->compound.declaration == type2->compound.declaration;
7476 * Parse a conditional expression, ie. 'expression ? ... : ...'.
7478 * @param expression the conditional expression
7480 static expression_t *parse_conditional_expression(unsigned precedence,
7481 expression_t *expression)
7483 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
7485 conditional_expression_t *conditional = &result->conditional;
7486 conditional->base.source_position = *HERE;
7487 conditional->condition = expression;
7490 add_anchor_token(':');
7493 type_t *const condition_type_orig = expression->base.type;
7494 type_t *const condition_type = skip_typeref(condition_type_orig);
7495 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
7496 type_error("expected a scalar type in conditional condition",
7497 &expression->base.source_position, condition_type_orig);
7500 expression_t *true_expression = expression;
7501 bool gnu_cond = false;
7502 if (GNU_MODE && token.type == ':') {
7505 true_expression = parse_expression();
7506 rem_anchor_token(':');
7508 expression_t *false_expression = parse_sub_expression(precedence);
7510 type_t *const orig_true_type = true_expression->base.type;
7511 type_t *const orig_false_type = false_expression->base.type;
7512 type_t *const true_type = skip_typeref(orig_true_type);
7513 type_t *const false_type = skip_typeref(orig_false_type);
7516 type_t *result_type;
7517 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
7518 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7519 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
7520 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
7521 warningf(&conditional->base.source_position,
7522 "ISO C forbids conditional expression with only one void side");
7524 result_type = type_void;
7525 } else if (is_type_arithmetic(true_type)
7526 && is_type_arithmetic(false_type)) {
7527 result_type = semantic_arithmetic(true_type, false_type);
7529 true_expression = create_implicit_cast(true_expression, result_type);
7530 false_expression = create_implicit_cast(false_expression, result_type);
7532 conditional->true_expression = true_expression;
7533 conditional->false_expression = false_expression;
7534 conditional->base.type = result_type;
7535 } else if (same_compound_type(true_type, false_type)) {
7536 /* just take 1 of the 2 types */
7537 result_type = true_type;
7538 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
7539 type_t *pointer_type;
7541 expression_t *other_expression;
7542 if (is_type_pointer(true_type) &&
7543 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
7544 pointer_type = true_type;
7545 other_type = false_type;
7546 other_expression = false_expression;
7548 pointer_type = false_type;
7549 other_type = true_type;
7550 other_expression = true_expression;
7553 if (is_null_pointer_constant(other_expression)) {
7554 result_type = pointer_type;
7555 } else if (is_type_pointer(other_type)) {
7556 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7557 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7560 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7561 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7563 } else if (types_compatible(get_unqualified_type(to1),
7564 get_unqualified_type(to2))) {
7567 warningf(&conditional->base.source_position,
7568 "pointer types '%T' and '%T' in conditional expression are incompatible",
7569 true_type, false_type);
7573 type_t *const type =
7574 get_qualified_type(to, to1->base.qualifiers | to2->base.qualifiers);
7575 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7576 } else if (is_type_integer(other_type)) {
7577 warningf(&conditional->base.source_position,
7578 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7579 result_type = pointer_type;
7581 if (is_type_valid(other_type)) {
7582 type_error_incompatible("while parsing conditional",
7583 &expression->base.source_position, true_type, false_type);
7585 result_type = type_error_type;
7588 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7589 type_error_incompatible("while parsing conditional",
7590 &conditional->base.source_position, true_type,
7593 result_type = type_error_type;
7596 conditional->true_expression
7597 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7598 conditional->false_expression
7599 = create_implicit_cast(false_expression, result_type);
7600 conditional->base.type = result_type;
7603 return create_invalid_expression();
7607 * Parse an extension expression.
7609 static expression_t *parse_extension(unsigned precedence)
7611 eat(T___extension__);
7613 bool old_gcc_extension = in_gcc_extension;
7614 in_gcc_extension = true;
7615 expression_t *expression = parse_sub_expression(precedence);
7616 in_gcc_extension = old_gcc_extension;
7621 * Parse a __builtin_classify_type() expression.
7623 static expression_t *parse_builtin_classify_type(const unsigned precedence)
7625 eat(T___builtin_classify_type);
7627 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7628 result->base.type = type_int;
7631 add_anchor_token(')');
7632 expression_t *expression = parse_sub_expression(precedence);
7633 rem_anchor_token(')');
7635 result->classify_type.type_expression = expression;
7639 return create_invalid_expression();
7642 static bool check_pointer_arithmetic(const source_position_t *source_position,
7643 type_t *pointer_type,
7644 type_t *orig_pointer_type)
7646 type_t *points_to = pointer_type->pointer.points_to;
7647 points_to = skip_typeref(points_to);
7649 if (is_type_incomplete(points_to)) {
7650 if (!GNU_MODE || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7651 errorf(source_position,
7652 "arithmetic with pointer to incomplete type '%T' not allowed",
7655 } else if (warning.pointer_arith) {
7656 warningf(source_position,
7657 "pointer of type '%T' used in arithmetic",
7660 } else if (is_type_function(points_to)) {
7662 errorf(source_position,
7663 "arithmetic with pointer to function type '%T' not allowed",
7666 } else if (warning.pointer_arith) {
7667 warningf(source_position,
7668 "pointer to a function '%T' used in arithmetic",
7675 static bool is_lvalue(const expression_t *expression)
7677 switch (expression->kind) {
7678 case EXPR_REFERENCE:
7679 case EXPR_ARRAY_ACCESS:
7681 case EXPR_UNARY_DEREFERENCE:
7685 /* Claim it is an lvalue, if the type is invalid. There was a parse
7686 * error before, which maybe prevented properly recognizing it as
7688 return !is_type_valid(skip_typeref(expression->base.type));
7692 static void semantic_incdec(unary_expression_t *expression)
7694 type_t *const orig_type = expression->value->base.type;
7695 type_t *const type = skip_typeref(orig_type);
7696 if (is_type_pointer(type)) {
7697 if (!check_pointer_arithmetic(&expression->base.source_position,
7701 } else if (!is_type_real(type) && is_type_valid(type)) {
7702 /* TODO: improve error message */
7703 errorf(&expression->base.source_position,
7704 "operation needs an arithmetic or pointer type");
7707 if (!is_lvalue(expression->value)) {
7708 /* TODO: improve error message */
7709 errorf(&expression->base.source_position, "lvalue required as operand");
7711 expression->base.type = orig_type;
7714 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7716 type_t *const orig_type = expression->value->base.type;
7717 type_t *const type = skip_typeref(orig_type);
7718 if (!is_type_arithmetic(type)) {
7719 if (is_type_valid(type)) {
7720 /* TODO: improve error message */
7721 errorf(&expression->base.source_position,
7722 "operation needs an arithmetic type");
7727 expression->base.type = orig_type;
7730 static void semantic_unexpr_plus(unary_expression_t *expression)
7732 semantic_unexpr_arithmetic(expression);
7733 if (warning.traditional)
7734 warningf(&expression->base.source_position,
7735 "traditional C rejects the unary plus operator");
7738 static expression_t const *get_reference_address(expression_t const *expr)
7740 bool regular_take_address = true;
7742 if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
7743 expr = expr->unary.value;
7745 regular_take_address = false;
7748 if (expr->kind != EXPR_UNARY_DEREFERENCE)
7751 expr = expr->unary.value;
7754 if (expr->kind != EXPR_REFERENCE)
7757 if (!regular_take_address &&
7758 !is_type_function(skip_typeref(expr->reference.declaration->type))) {
7765 static void warn_function_address_as_bool(expression_t const* expr)
7767 if (!warning.address)
7770 expr = get_reference_address(expr);
7772 warningf(&expr->base.source_position,
7773 "the address of '%Y' will always evaluate as 'true'",
7774 expr->reference.declaration->symbol);
7778 static void semantic_not(unary_expression_t *expression)
7780 type_t *const orig_type = expression->value->base.type;
7781 type_t *const type = skip_typeref(orig_type);
7782 if (!is_type_scalar(type) && is_type_valid(type)) {
7783 errorf(&expression->base.source_position,
7784 "operand of ! must be of scalar type");
7787 warn_function_address_as_bool(expression->value);
7789 expression->base.type = type_int;
7792 static void semantic_unexpr_integer(unary_expression_t *expression)
7794 type_t *const orig_type = expression->value->base.type;
7795 type_t *const type = skip_typeref(orig_type);
7796 if (!is_type_integer(type)) {
7797 if (is_type_valid(type)) {
7798 errorf(&expression->base.source_position,
7799 "operand of ~ must be of integer type");
7804 expression->base.type = orig_type;
7807 static void semantic_dereference(unary_expression_t *expression)
7809 type_t *const orig_type = expression->value->base.type;
7810 type_t *const type = skip_typeref(orig_type);
7811 if (!is_type_pointer(type)) {
7812 if (is_type_valid(type)) {
7813 errorf(&expression->base.source_position,
7814 "Unary '*' needs pointer or array type, but type '%T' given", orig_type);
7819 type_t *result_type = type->pointer.points_to;
7820 result_type = automatic_type_conversion(result_type);
7821 expression->base.type = result_type;
7825 * Record that an address is taken (expression represents an lvalue).
7827 * @param expression the expression
7828 * @param may_be_register if true, the expression might be an register
7830 static void set_address_taken(expression_t *expression, bool may_be_register)
7832 if (expression->kind != EXPR_REFERENCE)
7835 declaration_t *const declaration = expression->reference.declaration;
7836 /* happens for parse errors */
7837 if (declaration == NULL)
7840 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7841 errorf(&expression->base.source_position,
7842 "address of register variable '%Y' requested",
7843 declaration->symbol);
7845 declaration->address_taken = 1;
7850 * Check the semantic of the address taken expression.
7852 static void semantic_take_addr(unary_expression_t *expression)
7854 expression_t *value = expression->value;
7855 value->base.type = revert_automatic_type_conversion(value);
7857 type_t *orig_type = value->base.type;
7858 if (!is_type_valid(skip_typeref(orig_type)))
7861 set_address_taken(value, false);
7863 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7866 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7867 static expression_t *parse_##unexpression_type(unsigned precedence) \
7869 expression_t *unary_expression \
7870 = allocate_expression_zero(unexpression_type); \
7871 unary_expression->base.source_position = *HERE; \
7873 unary_expression->unary.value = parse_sub_expression(precedence); \
7875 sfunc(&unary_expression->unary); \
7877 return unary_expression; \
7880 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7881 semantic_unexpr_arithmetic)
7882 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7883 semantic_unexpr_plus)
7884 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7886 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7887 semantic_dereference)
7888 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7890 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7891 semantic_unexpr_integer)
7892 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7894 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7897 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7899 static expression_t *parse_##unexpression_type(unsigned precedence, \
7900 expression_t *left) \
7902 (void) precedence; \
7904 expression_t *unary_expression \
7905 = allocate_expression_zero(unexpression_type); \
7906 unary_expression->base.source_position = *HERE; \
7908 unary_expression->unary.value = left; \
7910 sfunc(&unary_expression->unary); \
7912 return unary_expression; \
7915 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7916 EXPR_UNARY_POSTFIX_INCREMENT,
7918 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7919 EXPR_UNARY_POSTFIX_DECREMENT,
7922 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7924 /* TODO: handle complex + imaginary types */
7926 type_left = get_unqualified_type(type_left);
7927 type_right = get_unqualified_type(type_right);
7929 /* § 6.3.1.8 Usual arithmetic conversions */
7930 if (type_left == type_long_double || type_right == type_long_double) {
7931 return type_long_double;
7932 } else if (type_left == type_double || type_right == type_double) {
7934 } else if (type_left == type_float || type_right == type_float) {
7938 type_left = promote_integer(type_left);
7939 type_right = promote_integer(type_right);
7941 if (type_left == type_right)
7944 bool const signed_left = is_type_signed(type_left);
7945 bool const signed_right = is_type_signed(type_right);
7946 int const rank_left = get_rank(type_left);
7947 int const rank_right = get_rank(type_right);
7949 if (signed_left == signed_right)
7950 return rank_left >= rank_right ? type_left : type_right;
7959 u_rank = rank_right;
7960 u_type = type_right;
7962 s_rank = rank_right;
7963 s_type = type_right;
7968 if (u_rank >= s_rank)
7971 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7973 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7974 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7978 case ATOMIC_TYPE_INT: return type_unsigned_int;
7979 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7980 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7982 default: panic("invalid atomic type");
7987 * Check the semantic restrictions for a binary expression.
7989 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7991 expression_t *const left = expression->left;
7992 expression_t *const right = expression->right;
7993 type_t *const orig_type_left = left->base.type;
7994 type_t *const orig_type_right = right->base.type;
7995 type_t *const type_left = skip_typeref(orig_type_left);
7996 type_t *const type_right = skip_typeref(orig_type_right);
7998 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7999 /* TODO: improve error message */
8000 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8001 errorf(&expression->base.source_position,
8002 "operation needs arithmetic types");
8007 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8008 expression->left = create_implicit_cast(left, arithmetic_type);
8009 expression->right = create_implicit_cast(right, arithmetic_type);
8010 expression->base.type = arithmetic_type;
8013 static void warn_div_by_zero(binary_expression_t const *const expression)
8015 if (!warning.div_by_zero ||
8016 !is_type_integer(expression->base.type))
8019 expression_t const *const right = expression->right;
8020 /* The type of the right operand can be different for /= */
8021 if (is_type_integer(right->base.type) &&
8022 is_constant_expression(right) &&
8023 fold_constant(right) == 0) {
8024 warningf(&expression->base.source_position, "division by zero");
8029 * Check the semantic restrictions for a div/mod expression.
8031 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
8032 semantic_binexpr_arithmetic(expression);
8033 warn_div_by_zero(expression);
8036 static void semantic_shift_op(binary_expression_t *expression)
8038 expression_t *const left = expression->left;
8039 expression_t *const right = expression->right;
8040 type_t *const orig_type_left = left->base.type;
8041 type_t *const orig_type_right = right->base.type;
8042 type_t * type_left = skip_typeref(orig_type_left);
8043 type_t * type_right = skip_typeref(orig_type_right);
8045 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
8046 /* TODO: improve error message */
8047 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8048 errorf(&expression->base.source_position,
8049 "operands of shift operation must have integer types");
8054 type_left = promote_integer(type_left);
8055 type_right = promote_integer(type_right);
8057 expression->left = create_implicit_cast(left, type_left);
8058 expression->right = create_implicit_cast(right, type_right);
8059 expression->base.type = type_left;
8062 static void semantic_add(binary_expression_t *expression)
8064 expression_t *const left = expression->left;
8065 expression_t *const right = expression->right;
8066 type_t *const orig_type_left = left->base.type;
8067 type_t *const orig_type_right = right->base.type;
8068 type_t *const type_left = skip_typeref(orig_type_left);
8069 type_t *const type_right = skip_typeref(orig_type_right);
8072 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8073 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8074 expression->left = create_implicit_cast(left, arithmetic_type);
8075 expression->right = create_implicit_cast(right, arithmetic_type);
8076 expression->base.type = arithmetic_type;
8078 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8079 check_pointer_arithmetic(&expression->base.source_position,
8080 type_left, orig_type_left);
8081 expression->base.type = type_left;
8082 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
8083 check_pointer_arithmetic(&expression->base.source_position,
8084 type_right, orig_type_right);
8085 expression->base.type = type_right;
8086 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8087 errorf(&expression->base.source_position,
8088 "invalid operands to binary + ('%T', '%T')",
8089 orig_type_left, orig_type_right);
8093 static void semantic_sub(binary_expression_t *expression)
8095 expression_t *const left = expression->left;
8096 expression_t *const right = expression->right;
8097 type_t *const orig_type_left = left->base.type;
8098 type_t *const orig_type_right = right->base.type;
8099 type_t *const type_left = skip_typeref(orig_type_left);
8100 type_t *const type_right = skip_typeref(orig_type_right);
8101 source_position_t const *const pos = &expression->base.source_position;
8104 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8105 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8106 expression->left = create_implicit_cast(left, arithmetic_type);
8107 expression->right = create_implicit_cast(right, arithmetic_type);
8108 expression->base.type = arithmetic_type;
8110 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8111 check_pointer_arithmetic(&expression->base.source_position,
8112 type_left, orig_type_left);
8113 expression->base.type = type_left;
8114 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8115 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
8116 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
8117 if (!types_compatible(unqual_left, unqual_right)) {
8119 "subtracting pointers to incompatible types '%T' and '%T'",
8120 orig_type_left, orig_type_right);
8121 } else if (!is_type_object(unqual_left)) {
8122 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
8123 warningf(pos, "subtracting pointers to void");
8125 errorf(pos, "subtracting pointers to non-object types '%T'",
8129 expression->base.type = type_ptrdiff_t;
8130 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8131 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
8132 orig_type_left, orig_type_right);
8136 static void warn_string_literal_address(expression_t const* expr)
8138 while (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
8139 expr = expr->unary.value;
8140 if (expr->kind != EXPR_UNARY_DEREFERENCE)
8142 expr = expr->unary.value;
8145 if (expr->kind == EXPR_STRING_LITERAL ||
8146 expr->kind == EXPR_WIDE_STRING_LITERAL) {
8147 warningf(&expr->base.source_position,
8148 "comparison with string literal results in unspecified behaviour");
8153 * Check the semantics of comparison expressions.
8155 * @param expression The expression to check.
8157 static void semantic_comparison(binary_expression_t *expression)
8159 expression_t *left = expression->left;
8160 expression_t *right = expression->right;
8162 if (warning.address) {
8163 warn_string_literal_address(left);
8164 warn_string_literal_address(right);
8166 expression_t const* const func_left = get_reference_address(left);
8167 if (func_left != NULL && is_null_pointer_constant(right)) {
8168 warningf(&expression->base.source_position,
8169 "the address of '%Y' will never be NULL",
8170 func_left->reference.declaration->symbol);
8173 expression_t const* const func_right = get_reference_address(right);
8174 if (func_right != NULL && is_null_pointer_constant(right)) {
8175 warningf(&expression->base.source_position,
8176 "the address of '%Y' will never be NULL",
8177 func_right->reference.declaration->symbol);
8181 type_t *orig_type_left = left->base.type;
8182 type_t *orig_type_right = right->base.type;
8183 type_t *type_left = skip_typeref(orig_type_left);
8184 type_t *type_right = skip_typeref(orig_type_right);
8186 /* TODO non-arithmetic types */
8187 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8188 /* test for signed vs unsigned compares */
8189 if (warning.sign_compare &&
8190 (expression->base.kind != EXPR_BINARY_EQUAL &&
8191 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
8192 (is_type_signed(type_left) != is_type_signed(type_right))) {
8194 /* check if 1 of the operands is a constant, in this case we just
8195 * check wether we can safely represent the resulting constant in
8196 * the type of the other operand. */
8197 expression_t *const_expr = NULL;
8198 expression_t *other_expr = NULL;
8200 if (is_constant_expression(left)) {
8203 } else if (is_constant_expression(right)) {
8208 if (const_expr != NULL) {
8209 type_t *other_type = skip_typeref(other_expr->base.type);
8210 long val = fold_constant(const_expr);
8211 /* TODO: check if val can be represented by other_type */
8215 warningf(&expression->base.source_position,
8216 "comparison between signed and unsigned");
8218 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8219 expression->left = create_implicit_cast(left, arithmetic_type);
8220 expression->right = create_implicit_cast(right, arithmetic_type);
8221 expression->base.type = arithmetic_type;
8222 if (warning.float_equal &&
8223 (expression->base.kind == EXPR_BINARY_EQUAL ||
8224 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
8225 is_type_float(arithmetic_type)) {
8226 warningf(&expression->base.source_position,
8227 "comparing floating point with == or != is unsafe");
8229 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8230 /* TODO check compatibility */
8231 } else if (is_type_pointer(type_left)) {
8232 expression->right = create_implicit_cast(right, type_left);
8233 } else if (is_type_pointer(type_right)) {
8234 expression->left = create_implicit_cast(left, type_right);
8235 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8236 type_error_incompatible("invalid operands in comparison",
8237 &expression->base.source_position,
8238 type_left, type_right);
8240 expression->base.type = type_int;
8244 * Checks if a compound type has constant fields.
8246 static bool has_const_fields(const compound_type_t *type)
8248 const scope_t *scope = &type->declaration->scope;
8249 const declaration_t *declaration = scope->declarations;
8251 for (; declaration != NULL; declaration = declaration->next) {
8252 if (declaration->namespc != NAMESPACE_NORMAL)
8255 const type_t *decl_type = skip_typeref(declaration->type);
8256 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
8263 static bool is_valid_assignment_lhs(expression_t const* const left)
8265 type_t *const orig_type_left = revert_automatic_type_conversion(left);
8266 type_t *const type_left = skip_typeref(orig_type_left);
8268 if (!is_lvalue(left)) {
8269 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
8274 if (is_type_array(type_left)) {
8275 errorf(HERE, "cannot assign to arrays ('%E')", left);
8278 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
8279 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
8283 if (is_type_incomplete(type_left)) {
8284 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
8285 left, orig_type_left);
8288 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
8289 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
8290 left, orig_type_left);
8297 static void semantic_arithmetic_assign(binary_expression_t *expression)
8299 expression_t *left = expression->left;
8300 expression_t *right = expression->right;
8301 type_t *orig_type_left = left->base.type;
8302 type_t *orig_type_right = right->base.type;
8304 if (!is_valid_assignment_lhs(left))
8307 type_t *type_left = skip_typeref(orig_type_left);
8308 type_t *type_right = skip_typeref(orig_type_right);
8310 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
8311 /* TODO: improve error message */
8312 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8313 errorf(&expression->base.source_position,
8314 "operation needs arithmetic types");
8319 /* combined instructions are tricky. We can't create an implicit cast on
8320 * the left side, because we need the uncasted form for the store.
8321 * The ast2firm pass has to know that left_type must be right_type
8322 * for the arithmetic operation and create a cast by itself */
8323 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8324 expression->right = create_implicit_cast(right, arithmetic_type);
8325 expression->base.type = type_left;
8328 static void semantic_divmod_assign(binary_expression_t *expression)
8330 semantic_arithmetic_assign(expression);
8331 warn_div_by_zero(expression);
8334 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
8336 expression_t *const left = expression->left;
8337 expression_t *const right = expression->right;
8338 type_t *const orig_type_left = left->base.type;
8339 type_t *const orig_type_right = right->base.type;
8340 type_t *const type_left = skip_typeref(orig_type_left);
8341 type_t *const type_right = skip_typeref(orig_type_right);
8343 if (!is_valid_assignment_lhs(left))
8346 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8347 /* combined instructions are tricky. We can't create an implicit cast on
8348 * the left side, because we need the uncasted form for the store.
8349 * The ast2firm pass has to know that left_type must be right_type
8350 * for the arithmetic operation and create a cast by itself */
8351 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
8352 expression->right = create_implicit_cast(right, arithmetic_type);
8353 expression->base.type = type_left;
8354 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8355 check_pointer_arithmetic(&expression->base.source_position,
8356 type_left, orig_type_left);
8357 expression->base.type = type_left;
8358 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8359 errorf(&expression->base.source_position,
8360 "incompatible types '%T' and '%T' in assignment",
8361 orig_type_left, orig_type_right);
8366 * Check the semantic restrictions of a logical expression.
8368 static void semantic_logical_op(binary_expression_t *expression)
8370 expression_t *const left = expression->left;
8371 expression_t *const right = expression->right;
8372 type_t *const orig_type_left = left->base.type;
8373 type_t *const orig_type_right = right->base.type;
8374 type_t *const type_left = skip_typeref(orig_type_left);
8375 type_t *const type_right = skip_typeref(orig_type_right);
8377 warn_function_address_as_bool(left);
8378 warn_function_address_as_bool(right);
8380 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
8381 /* TODO: improve error message */
8382 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8383 errorf(&expression->base.source_position,
8384 "operation needs scalar types");
8389 expression->base.type = type_int;
8393 * Check the semantic restrictions of a binary assign expression.
8395 static void semantic_binexpr_assign(binary_expression_t *expression)
8397 expression_t *left = expression->left;
8398 type_t *orig_type_left = left->base.type;
8400 if (!is_valid_assignment_lhs(left))
8403 assign_error_t error = semantic_assign(orig_type_left, expression->right);
8404 report_assign_error(error, orig_type_left, expression->right,
8405 "assignment", &left->base.source_position);
8406 expression->right = create_implicit_cast(expression->right, orig_type_left);
8407 expression->base.type = orig_type_left;
8411 * Determine if the outermost operation (or parts thereof) of the given
8412 * expression has no effect in order to generate a warning about this fact.
8413 * Therefore in some cases this only examines some of the operands of the
8414 * expression (see comments in the function and examples below).
8416 * f() + 23; // warning, because + has no effect
8417 * x || f(); // no warning, because x controls execution of f()
8418 * x ? y : f(); // warning, because y has no effect
8419 * (void)x; // no warning to be able to suppress the warning
8420 * This function can NOT be used for an "expression has definitely no effect"-
8422 static bool expression_has_effect(const expression_t *const expr)
8424 switch (expr->kind) {
8425 case EXPR_UNKNOWN: break;
8426 case EXPR_INVALID: return true; /* do NOT warn */
8427 case EXPR_REFERENCE: return false;
8428 /* suppress the warning for microsoft __noop operations */
8429 case EXPR_CONST: return expr->conste.is_ms_noop;
8430 case EXPR_CHARACTER_CONSTANT: return false;
8431 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
8432 case EXPR_STRING_LITERAL: return false;
8433 case EXPR_WIDE_STRING_LITERAL: return false;
8434 case EXPR_LABEL_ADDRESS: return false;
8437 const call_expression_t *const call = &expr->call;
8438 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
8441 switch (call->function->builtin_symbol.symbol->ID) {
8442 case T___builtin_va_end: return true;
8443 default: return false;
8447 /* Generate the warning if either the left or right hand side of a
8448 * conditional expression has no effect */
8449 case EXPR_CONDITIONAL: {
8450 const conditional_expression_t *const cond = &expr->conditional;
8452 expression_has_effect(cond->true_expression) &&
8453 expression_has_effect(cond->false_expression);
8456 case EXPR_SELECT: return false;
8457 case EXPR_ARRAY_ACCESS: return false;
8458 case EXPR_SIZEOF: return false;
8459 case EXPR_CLASSIFY_TYPE: return false;
8460 case EXPR_ALIGNOF: return false;
8462 case EXPR_FUNCNAME: return false;
8463 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
8464 case EXPR_BUILTIN_CONSTANT_P: return false;
8465 case EXPR_BUILTIN_PREFETCH: return true;
8466 case EXPR_OFFSETOF: return false;
8467 case EXPR_VA_START: return true;
8468 case EXPR_VA_ARG: return true;
8469 case EXPR_STATEMENT: return true; // TODO
8470 case EXPR_COMPOUND_LITERAL: return false;
8472 case EXPR_UNARY_NEGATE: return false;
8473 case EXPR_UNARY_PLUS: return false;
8474 case EXPR_UNARY_BITWISE_NEGATE: return false;
8475 case EXPR_UNARY_NOT: return false;
8476 case EXPR_UNARY_DEREFERENCE: return false;
8477 case EXPR_UNARY_TAKE_ADDRESS: return false;
8478 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
8479 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
8480 case EXPR_UNARY_PREFIX_INCREMENT: return true;
8481 case EXPR_UNARY_PREFIX_DECREMENT: return true;
8483 /* Treat void casts as if they have an effect in order to being able to
8484 * suppress the warning */
8485 case EXPR_UNARY_CAST: {
8486 type_t *const type = skip_typeref(expr->base.type);
8487 return is_type_atomic(type, ATOMIC_TYPE_VOID);
8490 case EXPR_UNARY_CAST_IMPLICIT: return true;
8491 case EXPR_UNARY_ASSUME: return true;
8493 case EXPR_BINARY_ADD: return false;
8494 case EXPR_BINARY_SUB: return false;
8495 case EXPR_BINARY_MUL: return false;
8496 case EXPR_BINARY_DIV: return false;
8497 case EXPR_BINARY_MOD: return false;
8498 case EXPR_BINARY_EQUAL: return false;
8499 case EXPR_BINARY_NOTEQUAL: return false;
8500 case EXPR_BINARY_LESS: return false;
8501 case EXPR_BINARY_LESSEQUAL: return false;
8502 case EXPR_BINARY_GREATER: return false;
8503 case EXPR_BINARY_GREATEREQUAL: return false;
8504 case EXPR_BINARY_BITWISE_AND: return false;
8505 case EXPR_BINARY_BITWISE_OR: return false;
8506 case EXPR_BINARY_BITWISE_XOR: return false;
8507 case EXPR_BINARY_SHIFTLEFT: return false;
8508 case EXPR_BINARY_SHIFTRIGHT: return false;
8509 case EXPR_BINARY_ASSIGN: return true;
8510 case EXPR_BINARY_MUL_ASSIGN: return true;
8511 case EXPR_BINARY_DIV_ASSIGN: return true;
8512 case EXPR_BINARY_MOD_ASSIGN: return true;
8513 case EXPR_BINARY_ADD_ASSIGN: return true;
8514 case EXPR_BINARY_SUB_ASSIGN: return true;
8515 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
8516 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
8517 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
8518 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
8519 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
8521 /* Only examine the right hand side of && and ||, because the left hand
8522 * side already has the effect of controlling the execution of the right
8524 case EXPR_BINARY_LOGICAL_AND:
8525 case EXPR_BINARY_LOGICAL_OR:
8526 /* Only examine the right hand side of a comma expression, because the left
8527 * hand side has a separate warning */
8528 case EXPR_BINARY_COMMA:
8529 return expression_has_effect(expr->binary.right);
8531 case EXPR_BINARY_BUILTIN_EXPECT: return true;
8532 case EXPR_BINARY_ISGREATER: return false;
8533 case EXPR_BINARY_ISGREATEREQUAL: return false;
8534 case EXPR_BINARY_ISLESS: return false;
8535 case EXPR_BINARY_ISLESSEQUAL: return false;
8536 case EXPR_BINARY_ISLESSGREATER: return false;
8537 case EXPR_BINARY_ISUNORDERED: return false;
8540 internal_errorf(HERE, "unexpected expression");
8543 static void semantic_comma(binary_expression_t *expression)
8545 if (warning.unused_value) {
8546 const expression_t *const left = expression->left;
8547 if (!expression_has_effect(left)) {
8548 warningf(&left->base.source_position,
8549 "left-hand operand of comma expression has no effect");
8552 expression->base.type = expression->right->base.type;
8555 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
8556 static expression_t *parse_##binexpression_type(unsigned precedence, \
8557 expression_t *left) \
8559 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
8560 binexpr->base.source_position = *HERE; \
8561 binexpr->binary.left = left; \
8564 expression_t *right = parse_sub_expression(precedence + lr); \
8566 binexpr->binary.right = right; \
8567 sfunc(&binexpr->binary); \
8572 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
8573 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
8574 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_divmod_arithmetic, 1)
8575 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_divmod_arithmetic, 1)
8576 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
8577 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
8578 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
8579 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
8580 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
8582 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
8583 semantic_comparison, 1)
8584 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
8585 semantic_comparison, 1)
8586 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
8587 semantic_comparison, 1)
8588 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
8589 semantic_comparison, 1)
8591 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
8592 semantic_binexpr_arithmetic, 1)
8593 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
8594 semantic_binexpr_arithmetic, 1)
8595 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
8596 semantic_binexpr_arithmetic, 1)
8597 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
8598 semantic_logical_op, 1)
8599 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
8600 semantic_logical_op, 1)
8601 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
8602 semantic_shift_op, 1)
8603 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
8604 semantic_shift_op, 1)
8605 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
8606 semantic_arithmetic_addsubb_assign, 0)
8607 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
8608 semantic_arithmetic_addsubb_assign, 0)
8609 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
8610 semantic_arithmetic_assign, 0)
8611 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
8612 semantic_divmod_assign, 0)
8613 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
8614 semantic_divmod_assign, 0)
8615 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
8616 semantic_arithmetic_assign, 0)
8617 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
8618 semantic_arithmetic_assign, 0)
8619 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
8620 semantic_arithmetic_assign, 0)
8621 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
8622 semantic_arithmetic_assign, 0)
8623 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
8624 semantic_arithmetic_assign, 0)
8626 static expression_t *parse_sub_expression(unsigned precedence)
8628 if (token.type < 0) {
8629 return expected_expression_error();
8632 expression_parser_function_t *parser
8633 = &expression_parsers[token.type];
8634 source_position_t source_position = token.source_position;
8637 if (parser->parser != NULL) {
8638 left = parser->parser(parser->precedence);
8640 left = parse_primary_expression();
8642 assert(left != NULL);
8643 left->base.source_position = source_position;
8646 if (token.type < 0) {
8647 return expected_expression_error();
8650 parser = &expression_parsers[token.type];
8651 if (parser->infix_parser == NULL)
8653 if (parser->infix_precedence < precedence)
8656 left = parser->infix_parser(parser->infix_precedence, left);
8658 assert(left != NULL);
8659 assert(left->kind != EXPR_UNKNOWN);
8660 left->base.source_position = source_position;
8667 * Parse an expression.
8669 static expression_t *parse_expression(void)
8671 return parse_sub_expression(1);
8675 * Register a parser for a prefix-like operator with given precedence.
8677 * @param parser the parser function
8678 * @param token_type the token type of the prefix token
8679 * @param precedence the precedence of the operator
8681 static void register_expression_parser(parse_expression_function parser,
8682 int token_type, unsigned precedence)
8684 expression_parser_function_t *entry = &expression_parsers[token_type];
8686 if (entry->parser != NULL) {
8687 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8688 panic("trying to register multiple expression parsers for a token");
8690 entry->parser = parser;
8691 entry->precedence = precedence;
8695 * Register a parser for an infix operator with given precedence.
8697 * @param parser the parser function
8698 * @param token_type the token type of the infix operator
8699 * @param precedence the precedence of the operator
8701 static void register_infix_parser(parse_expression_infix_function parser,
8702 int token_type, unsigned precedence)
8704 expression_parser_function_t *entry = &expression_parsers[token_type];
8706 if (entry->infix_parser != NULL) {
8707 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8708 panic("trying to register multiple infix expression parsers for a "
8711 entry->infix_parser = parser;
8712 entry->infix_precedence = precedence;
8716 * Initialize the expression parsers.
8718 static void init_expression_parsers(void)
8720 memset(&expression_parsers, 0, sizeof(expression_parsers));
8722 register_infix_parser(parse_array_expression, '[', 30);
8723 register_infix_parser(parse_call_expression, '(', 30);
8724 register_infix_parser(parse_select_expression, '.', 30);
8725 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
8726 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
8728 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
8731 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
8732 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
8733 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
8734 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
8735 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
8736 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
8737 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
8738 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
8739 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
8740 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
8741 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
8742 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
8743 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
8744 T_EXCLAMATIONMARKEQUAL, 13);
8745 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
8746 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
8747 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
8748 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
8749 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
8750 register_infix_parser(parse_conditional_expression, '?', 7);
8751 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
8752 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
8753 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
8754 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
8755 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
8756 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
8757 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
8758 T_LESSLESSEQUAL, 2);
8759 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
8760 T_GREATERGREATEREQUAL, 2);
8761 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
8763 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
8765 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
8768 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
8770 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
8771 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
8772 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
8773 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
8774 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
8775 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
8776 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
8778 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
8780 register_expression_parser(parse_sizeof, T_sizeof, 25);
8781 register_expression_parser(parse_alignof, T___alignof__, 25);
8782 register_expression_parser(parse_extension, T___extension__, 25);
8783 register_expression_parser(parse_builtin_classify_type,
8784 T___builtin_classify_type, 25);
8788 * Parse a asm statement arguments specification.
8790 static asm_argument_t *parse_asm_arguments(bool is_out)
8792 asm_argument_t *result = NULL;
8793 asm_argument_t *last = NULL;
8795 while (token.type == T_STRING_LITERAL || token.type == '[') {
8796 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8797 memset(argument, 0, sizeof(argument[0]));
8799 if (token.type == '[') {
8801 if (token.type != T_IDENTIFIER) {
8802 parse_error_expected("while parsing asm argument",
8803 T_IDENTIFIER, NULL);
8806 argument->symbol = token.v.symbol;
8811 argument->constraints = parse_string_literals();
8813 add_anchor_token(')');
8814 expression_t *expression = parse_expression();
8815 rem_anchor_token(')');
8817 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8818 * change size or type representation (e.g. int -> long is ok, but
8819 * int -> float is not) */
8820 if (expression->kind == EXPR_UNARY_CAST) {
8821 type_t *const type = expression->base.type;
8822 type_kind_t const kind = type->kind;
8823 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8826 if (kind == TYPE_ATOMIC) {
8827 atomic_type_kind_t const akind = type->atomic.akind;
8828 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8829 size = get_atomic_type_size(akind);
8831 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8832 size = get_atomic_type_size(get_intptr_kind());
8836 expression_t *const value = expression->unary.value;
8837 type_t *const value_type = value->base.type;
8838 type_kind_t const value_kind = value_type->kind;
8840 unsigned value_flags;
8841 unsigned value_size;
8842 if (value_kind == TYPE_ATOMIC) {
8843 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8844 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8845 value_size = get_atomic_type_size(value_akind);
8846 } else if (value_kind == TYPE_POINTER) {
8847 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8848 value_size = get_atomic_type_size(get_intptr_kind());
8853 if (value_flags != flags || value_size != size)
8857 } while (expression->kind == EXPR_UNARY_CAST);
8861 if (!is_lvalue(expression)) {
8862 errorf(&expression->base.source_position,
8863 "asm output argument is not an lvalue");
8866 if (argument->constraints.begin[0] == '+')
8867 mark_decls_read(expression, NULL);
8869 mark_decls_read(expression, NULL);
8871 argument->expression = expression;
8874 set_address_taken(expression, true);
8877 last->next = argument;
8883 if (token.type != ',')
8894 * Parse a asm statement clobber specification.
8896 static asm_clobber_t *parse_asm_clobbers(void)
8898 asm_clobber_t *result = NULL;
8899 asm_clobber_t *last = NULL;
8901 while(token.type == T_STRING_LITERAL) {
8902 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8903 clobber->clobber = parse_string_literals();
8906 last->next = clobber;
8912 if (token.type != ',')
8921 * Parse an asm statement.
8923 static statement_t *parse_asm_statement(void)
8925 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8926 asm_statement_t *asm_statement = &statement->asms;
8930 if (token.type == T_volatile) {
8932 asm_statement->is_volatile = true;
8936 add_anchor_token(')');
8937 add_anchor_token(':');
8938 asm_statement->asm_text = parse_string_literals();
8940 if (token.type != ':') {
8941 rem_anchor_token(':');
8946 asm_statement->outputs = parse_asm_arguments(true);
8947 if (token.type != ':') {
8948 rem_anchor_token(':');
8953 asm_statement->inputs = parse_asm_arguments(false);
8954 if (token.type != ':') {
8955 rem_anchor_token(':');
8958 rem_anchor_token(':');
8961 asm_statement->clobbers = parse_asm_clobbers();
8964 rem_anchor_token(')');
8968 if (asm_statement->outputs == NULL) {
8969 /* GCC: An 'asm' instruction without any output operands will be treated
8970 * identically to a volatile 'asm' instruction. */
8971 asm_statement->is_volatile = true;
8976 return create_invalid_statement();
8980 * Parse a case statement.
8982 static statement_t *parse_case_statement(void)
8984 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8985 source_position_t *const pos = &statement->base.source_position;
8989 expression_t *const expression = parse_expression();
8990 statement->case_label.expression = expression;
8991 if (!is_constant_expression(expression)) {
8992 /* This check does not prevent the error message in all cases of an
8993 * prior error while parsing the expression. At least it catches the
8994 * common case of a mistyped enum entry. */
8995 if (is_type_valid(skip_typeref(expression->base.type))) {
8996 errorf(pos, "case label does not reduce to an integer constant");
8998 statement->case_label.is_bad = true;
9000 long const val = fold_constant(expression);
9001 statement->case_label.first_case = val;
9002 statement->case_label.last_case = val;
9006 if (token.type == T_DOTDOTDOT) {
9008 expression_t *const end_range = parse_expression();
9009 statement->case_label.end_range = end_range;
9010 if (!is_constant_expression(end_range)) {
9011 /* This check does not prevent the error message in all cases of an
9012 * prior error while parsing the expression. At least it catches the
9013 * common case of a mistyped enum entry. */
9014 if (is_type_valid(skip_typeref(end_range->base.type))) {
9015 errorf(pos, "case range does not reduce to an integer constant");
9017 statement->case_label.is_bad = true;
9019 long const val = fold_constant(end_range);
9020 statement->case_label.last_case = val;
9022 if (val < statement->case_label.first_case) {
9023 statement->case_label.is_empty_range = true;
9024 warningf(pos, "empty range specified");
9030 PUSH_PARENT(statement);
9034 if (current_switch != NULL) {
9035 if (! statement->case_label.is_bad) {
9036 /* Check for duplicate case values */
9037 case_label_statement_t *c = &statement->case_label;
9038 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
9039 if (l->is_bad || l->is_empty_range || l->expression == NULL)
9042 if (c->last_case < l->first_case || c->first_case > l->last_case)
9045 errorf(pos, "duplicate case value (previously used %P)",
9046 &l->base.source_position);
9050 /* link all cases into the switch statement */
9051 if (current_switch->last_case == NULL) {
9052 current_switch->first_case = &statement->case_label;
9054 current_switch->last_case->next = &statement->case_label;
9056 current_switch->last_case = &statement->case_label;
9058 errorf(pos, "case label not within a switch statement");
9061 statement_t *const inner_stmt = parse_statement();
9062 statement->case_label.statement = inner_stmt;
9063 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9064 errorf(&inner_stmt->base.source_position, "declaration after case label");
9071 return create_invalid_statement();
9075 * Parse a default statement.
9077 static statement_t *parse_default_statement(void)
9079 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9083 PUSH_PARENT(statement);
9086 if (current_switch != NULL) {
9087 const case_label_statement_t *def_label = current_switch->default_label;
9088 if (def_label != NULL) {
9089 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
9090 &def_label->base.source_position);
9092 current_switch->default_label = &statement->case_label;
9094 /* link all cases into the switch statement */
9095 if (current_switch->last_case == NULL) {
9096 current_switch->first_case = &statement->case_label;
9098 current_switch->last_case->next = &statement->case_label;
9100 current_switch->last_case = &statement->case_label;
9103 errorf(&statement->base.source_position,
9104 "'default' label not within a switch statement");
9107 statement_t *const inner_stmt = parse_statement();
9108 statement->case_label.statement = inner_stmt;
9109 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9110 errorf(&inner_stmt->base.source_position, "declaration after default label");
9117 return create_invalid_statement();
9121 * Parse a label statement.
9123 static statement_t *parse_label_statement(void)
9125 assert(token.type == T_IDENTIFIER);
9126 symbol_t *symbol = token.v.symbol;
9127 declaration_t *label = get_label(symbol);
9129 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
9130 statement->label.label = label;
9134 PUSH_PARENT(statement);
9136 /* if statement is already set then the label is defined twice,
9137 * otherwise it was just mentioned in a goto/local label declaration so far */
9138 if (label->init.statement != NULL) {
9139 errorf(HERE, "duplicate label '%Y' (declared %P)",
9140 symbol, &label->source_position);
9142 label->source_position = token.source_position;
9143 label->init.statement = statement;
9148 if (token.type == '}') {
9149 /* TODO only warn? */
9151 warningf(HERE, "label at end of compound statement");
9152 statement->label.statement = create_empty_statement();
9154 errorf(HERE, "label at end of compound statement");
9155 statement->label.statement = create_invalid_statement();
9157 } else if (token.type == ';') {
9158 /* Eat an empty statement here, to avoid the warning about an empty
9159 * statement after a label. label:; is commonly used to have a label
9160 * before a closing brace. */
9161 statement->label.statement = create_empty_statement();
9164 statement_t *const inner_stmt = parse_statement();
9165 statement->label.statement = inner_stmt;
9166 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9167 errorf(&inner_stmt->base.source_position, "declaration after label");
9171 /* remember the labels in a list for later checking */
9172 if (label_last == NULL) {
9173 label_first = &statement->label;
9175 label_last->next = &statement->label;
9177 label_last = &statement->label;
9184 * Parse an if statement.
9186 static statement_t *parse_if(void)
9188 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
9192 PUSH_PARENT(statement);
9194 add_anchor_token('{');
9197 add_anchor_token(')');
9198 expression_t *const expr = parse_expression();
9199 statement->ifs.condition = expr;
9200 mark_decls_read(expr, NULL);
9201 rem_anchor_token(')');
9205 rem_anchor_token('{');
9207 add_anchor_token(T_else);
9208 statement->ifs.true_statement = parse_statement();
9209 rem_anchor_token(T_else);
9211 if (token.type == T_else) {
9213 statement->ifs.false_statement = parse_statement();
9221 * Check that all enums are handled in a switch.
9223 * @param statement the switch statement to check
9225 static void check_enum_cases(const switch_statement_t *statement) {
9226 const type_t *type = skip_typeref(statement->expression->base.type);
9227 if (! is_type_enum(type))
9229 const enum_type_t *enumt = &type->enumt;
9231 /* if we have a default, no warnings */
9232 if (statement->default_label != NULL)
9235 /* FIXME: calculation of value should be done while parsing */
9236 const declaration_t *declaration;
9237 long last_value = -1;
9238 for (declaration = enumt->declaration->next;
9239 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
9240 declaration = declaration->next) {
9241 const expression_t *expression = declaration->init.enum_value;
9242 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
9244 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
9245 if (l->expression == NULL)
9247 if (l->first_case <= value && value <= l->last_case) {
9253 warningf(&statement->base.source_position,
9254 "enumeration value '%Y' not handled in switch", declaration->symbol);
9261 * Parse a switch statement.
9263 static statement_t *parse_switch(void)
9265 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
9269 PUSH_PARENT(statement);
9272 add_anchor_token(')');
9273 expression_t *const expr = parse_expression();
9274 mark_decls_read(expr, NULL);
9275 type_t * type = skip_typeref(expr->base.type);
9276 if (is_type_integer(type)) {
9277 type = promote_integer(type);
9278 if (warning.traditional) {
9279 if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
9280 warningf(&expr->base.source_position,
9281 "'%T' switch expression not converted to '%T' in ISO C",
9285 } else if (is_type_valid(type)) {
9286 errorf(&expr->base.source_position,
9287 "switch quantity is not an integer, but '%T'", type);
9288 type = type_error_type;
9290 statement->switchs.expression = create_implicit_cast(expr, type);
9292 rem_anchor_token(')');
9294 switch_statement_t *rem = current_switch;
9295 current_switch = &statement->switchs;
9296 statement->switchs.body = parse_statement();
9297 current_switch = rem;
9299 if (warning.switch_default &&
9300 statement->switchs.default_label == NULL) {
9301 warningf(&statement->base.source_position, "switch has no default case");
9303 if (warning.switch_enum)
9304 check_enum_cases(&statement->switchs);
9310 return create_invalid_statement();
9313 static statement_t *parse_loop_body(statement_t *const loop)
9315 statement_t *const rem = current_loop;
9316 current_loop = loop;
9318 statement_t *const body = parse_statement();
9325 * Parse a while statement.
9327 static statement_t *parse_while(void)
9329 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
9333 PUSH_PARENT(statement);
9336 add_anchor_token(')');
9337 expression_t *const cond = parse_expression();
9338 statement->whiles.condition = cond;
9339 mark_decls_read(cond, NULL);
9340 rem_anchor_token(')');
9343 statement->whiles.body = parse_loop_body(statement);
9349 return create_invalid_statement();
9353 * Parse a do statement.
9355 static statement_t *parse_do(void)
9357 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
9361 PUSH_PARENT(statement);
9363 add_anchor_token(T_while);
9364 statement->do_while.body = parse_loop_body(statement);
9365 rem_anchor_token(T_while);
9369 add_anchor_token(')');
9370 expression_t *const cond = parse_expression();
9371 statement->do_while.condition = cond;
9372 mark_decls_read(cond, NULL);
9373 rem_anchor_token(')');
9381 return create_invalid_statement();
9385 * Parse a for statement.
9387 static statement_t *parse_for(void)
9389 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
9393 PUSH_PARENT(statement);
9395 size_t const top = environment_top();
9396 scope_push(&statement->fors.scope);
9399 add_anchor_token(')');
9401 if (token.type != ';') {
9402 if (is_declaration_specifier(&token, false)) {
9403 parse_declaration(record_declaration);
9405 add_anchor_token(';');
9406 expression_t *const init = parse_expression();
9407 statement->fors.initialisation = init;
9408 mark_decls_read(init, DECL_ANY);
9409 if (warning.unused_value && !expression_has_effect(init)) {
9410 warningf(&init->base.source_position,
9411 "initialisation of 'for'-statement has no effect");
9413 rem_anchor_token(';');
9420 if (token.type != ';') {
9421 add_anchor_token(';');
9422 expression_t *const cond = parse_expression();
9423 statement->fors.condition = cond;
9424 mark_decls_read(cond, NULL);
9425 rem_anchor_token(';');
9428 if (token.type != ')') {
9429 expression_t *const step = parse_expression();
9430 statement->fors.step = step;
9431 mark_decls_read(step, DECL_ANY);
9432 if (warning.unused_value && !expression_has_effect(step)) {
9433 warningf(&step->base.source_position,
9434 "step of 'for'-statement has no effect");
9437 rem_anchor_token(')');
9439 statement->fors.body = parse_loop_body(statement);
9441 assert(scope == &statement->fors.scope);
9443 environment_pop_to(top);
9450 rem_anchor_token(')');
9451 assert(scope == &statement->fors.scope);
9453 environment_pop_to(top);
9455 return create_invalid_statement();
9459 * Parse a goto statement.
9461 static statement_t *parse_goto(void)
9463 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
9466 if (GNU_MODE && token.type == '*') {
9468 expression_t *expression = parse_expression();
9469 mark_decls_read(expression, NULL);
9471 /* Argh: although documentation say the expression must be of type void *,
9472 * gcc excepts anything that can be casted into void * without error */
9473 type_t *type = expression->base.type;
9475 if (type != type_error_type) {
9476 if (!is_type_pointer(type) && !is_type_integer(type)) {
9477 errorf(&expression->base.source_position,
9478 "cannot convert to a pointer type");
9479 } else if (type != type_void_ptr) {
9480 warningf(&expression->base.source_position,
9481 "type of computed goto expression should be 'void*' not '%T'", type);
9483 expression = create_implicit_cast(expression, type_void_ptr);
9486 statement->gotos.expression = expression;
9488 if (token.type != T_IDENTIFIER) {
9490 parse_error_expected("while parsing goto", T_IDENTIFIER, '*', NULL);
9492 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
9496 symbol_t *symbol = token.v.symbol;
9499 statement->gotos.label = get_label(symbol);
9501 if (statement->gotos.label->parent_scope->depth < current_function->scope.depth) {
9502 statement->gotos.outer_fkt_jmp = true;
9506 /* remember the goto's in a list for later checking */
9507 if (goto_last == NULL) {
9508 goto_first = &statement->gotos;
9510 goto_last->next = &statement->gotos;
9512 goto_last = &statement->gotos;
9518 return create_invalid_statement();
9522 * Parse a continue statement.
9524 static statement_t *parse_continue(void)
9526 if (current_loop == NULL) {
9527 errorf(HERE, "continue statement not within loop");
9530 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
9540 * Parse a break statement.
9542 static statement_t *parse_break(void)
9544 if (current_switch == NULL && current_loop == NULL) {
9545 errorf(HERE, "break statement not within loop or switch");
9548 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
9558 * Parse a __leave statement.
9560 static statement_t *parse_leave_statement(void)
9562 if (current_try == NULL) {
9563 errorf(HERE, "__leave statement not within __try");
9566 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
9576 * Check if a given declaration represents a local variable.
9578 static bool is_local_var_declaration(const declaration_t *declaration)
9580 switch ((storage_class_tag_t) declaration->storage_class) {
9581 case STORAGE_CLASS_AUTO:
9582 case STORAGE_CLASS_REGISTER: {
9583 const type_t *type = skip_typeref(declaration->type);
9584 if (is_type_function(type)) {
9596 * Check if a given declaration represents a variable.
9598 static bool is_var_declaration(const declaration_t *declaration)
9600 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
9603 const type_t *type = skip_typeref(declaration->type);
9604 return !is_type_function(type);
9608 * Check if a given expression represents a local variable.
9610 static bool is_local_variable(const expression_t *expression)
9612 if (expression->base.kind != EXPR_REFERENCE) {
9615 const declaration_t *declaration = expression->reference.declaration;
9616 return is_local_var_declaration(declaration);
9620 * Check if a given expression represents a local variable and
9621 * return its declaration then, else return NULL.
9623 declaration_t *expr_is_variable(const expression_t *expression)
9625 if (expression->base.kind != EXPR_REFERENCE) {
9628 declaration_t *declaration = expression->reference.declaration;
9629 if (is_var_declaration(declaration))
9635 * Parse a return statement.
9637 static statement_t *parse_return(void)
9641 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
9643 expression_t *return_value = NULL;
9644 if (token.type != ';') {
9645 return_value = parse_expression();
9646 mark_decls_read(return_value, NULL);
9649 const type_t *const func_type = current_function->type;
9650 assert(is_type_function(func_type));
9651 type_t *const return_type = skip_typeref(func_type->function.return_type);
9653 if (return_value != NULL) {
9654 type_t *return_value_type = skip_typeref(return_value->base.type);
9656 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
9657 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
9658 warningf(&statement->base.source_position,
9659 "'return' with a value, in function returning void");
9660 return_value = NULL;
9662 assign_error_t error = semantic_assign(return_type, return_value);
9663 report_assign_error(error, return_type, return_value, "'return'",
9664 &statement->base.source_position);
9665 return_value = create_implicit_cast(return_value, return_type);
9667 /* check for returning address of a local var */
9668 if (return_value != NULL &&
9669 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9670 const expression_t *expression = return_value->unary.value;
9671 if (is_local_variable(expression)) {
9672 warningf(&statement->base.source_position,
9673 "function returns address of local variable");
9677 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9678 warningf(&statement->base.source_position,
9679 "'return' without value, in function returning non-void");
9682 statement->returns.value = return_value;
9691 * Parse a declaration statement.
9693 static statement_t *parse_declaration_statement(void)
9695 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9697 declaration_t *before = last_declaration;
9699 parse_external_declaration();
9701 parse_declaration(record_declaration);
9703 if (before == NULL) {
9704 statement->declaration.declarations_begin = scope->declarations;
9706 statement->declaration.declarations_begin = before->next;
9708 statement->declaration.declarations_end = last_declaration;
9714 * Parse an expression statement, ie. expr ';'.
9716 static statement_t *parse_expression_statement(void)
9718 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9720 expression_t *const expr = parse_expression();
9721 statement->expression.expression = expr;
9722 mark_decls_read(expr, DECL_ANY);
9731 * Parse a microsoft __try { } __finally { } or
9732 * __try{ } __except() { }
9734 static statement_t *parse_ms_try_statment(void)
9736 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9739 PUSH_PARENT(statement);
9741 ms_try_statement_t *rem = current_try;
9742 current_try = &statement->ms_try;
9743 statement->ms_try.try_statement = parse_compound_statement(false);
9748 if (token.type == T___except) {
9751 add_anchor_token(')');
9752 expression_t *const expr = parse_expression();
9753 mark_decls_read(expr, NULL);
9754 type_t * type = skip_typeref(expr->base.type);
9755 if (is_type_integer(type)) {
9756 type = promote_integer(type);
9757 } else if (is_type_valid(type)) {
9758 errorf(&expr->base.source_position,
9759 "__expect expression is not an integer, but '%T'", type);
9760 type = type_error_type;
9762 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9763 rem_anchor_token(')');
9765 statement->ms_try.final_statement = parse_compound_statement(false);
9766 } else if (token.type == T__finally) {
9768 statement->ms_try.final_statement = parse_compound_statement(false);
9770 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9771 return create_invalid_statement();
9775 return create_invalid_statement();
9778 static statement_t *parse_empty_statement(void)
9780 if (warning.empty_statement) {
9781 warningf(HERE, "statement is empty");
9783 statement_t *const statement = create_empty_statement();
9788 static statement_t *parse_local_label_declaration(void) {
9789 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9793 declaration_t *begin = NULL, *end = NULL;
9796 if (token.type != T_IDENTIFIER) {
9797 parse_error_expected("while parsing local label declaration",
9798 T_IDENTIFIER, NULL);
9801 symbol_t *symbol = token.v.symbol;
9802 declaration_t *declaration = get_declaration(symbol, NAMESPACE_LOCAL_LABEL);
9803 if (declaration != NULL) {
9804 errorf(HERE, "multiple definitions of '__label__ %Y' (previous definition at %P)",
9805 symbol, &declaration->source_position);
9807 declaration = allocate_declaration_zero();
9808 declaration->namespc = NAMESPACE_LOCAL_LABEL;
9809 declaration->source_position = token.source_position;
9810 declaration->symbol = symbol;
9811 declaration->parent_scope = scope;
9812 declaration->init.statement = NULL;
9815 end->next = declaration;
9818 begin = declaration;
9820 local_label_push(declaration);
9824 if (token.type != ',')
9830 statement->declaration.declarations_begin = begin;
9831 statement->declaration.declarations_end = end;
9836 * Parse a statement.
9837 * There's also parse_statement() which additionally checks for
9838 * "statement has no effect" warnings
9840 static statement_t *intern_parse_statement(void)
9842 statement_t *statement = NULL;
9844 /* declaration or statement */
9845 add_anchor_token(';');
9846 switch (token.type) {
9847 case T_IDENTIFIER: {
9848 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9849 if (la1_type == ':') {
9850 statement = parse_label_statement();
9851 } else if (is_typedef_symbol(token.v.symbol)) {
9852 statement = parse_declaration_statement();
9853 } else switch (la1_type) {
9855 if (get_declaration(token.v.symbol, NAMESPACE_NORMAL) != NULL)
9856 goto expression_statment;
9861 statement = parse_declaration_statement();
9865 expression_statment:
9866 statement = parse_expression_statement();
9872 case T___extension__:
9873 /* This can be a prefix to a declaration or an expression statement.
9874 * We simply eat it now and parse the rest with tail recursion. */
9877 } while (token.type == T___extension__);
9878 bool old_gcc_extension = in_gcc_extension;
9879 in_gcc_extension = true;
9880 statement = parse_statement();
9881 in_gcc_extension = old_gcc_extension;
9885 statement = parse_declaration_statement();
9889 statement = parse_local_label_declaration();
9892 case ';': statement = parse_empty_statement(); break;
9893 case '{': statement = parse_compound_statement(false); break;
9894 case T___leave: statement = parse_leave_statement(); break;
9895 case T___try: statement = parse_ms_try_statment(); break;
9896 case T_asm: statement = parse_asm_statement(); break;
9897 case T_break: statement = parse_break(); break;
9898 case T_case: statement = parse_case_statement(); break;
9899 case T_continue: statement = parse_continue(); break;
9900 case T_default: statement = parse_default_statement(); break;
9901 case T_do: statement = parse_do(); break;
9902 case T_for: statement = parse_for(); break;
9903 case T_goto: statement = parse_goto(); break;
9904 case T_if: statement = parse_if(); break;
9905 case T_return: statement = parse_return(); break;
9906 case T_switch: statement = parse_switch(); break;
9907 case T_while: statement = parse_while(); break;
9917 case T_CHARACTER_CONSTANT:
9918 case T_FLOATINGPOINT:
9922 case T_STRING_LITERAL:
9923 case T_WIDE_CHARACTER_CONSTANT:
9924 case T_WIDE_STRING_LITERAL:
9925 case T___FUNCDNAME__:
9927 case T___FUNCTION__:
9928 case T___PRETTY_FUNCTION__:
9929 case T___builtin_alloca:
9930 case T___builtin_classify_type:
9931 case T___builtin_constant_p:
9932 case T___builtin_expect:
9933 case T___builtin_huge_val:
9934 case T___builtin_isgreater:
9935 case T___builtin_isgreaterequal:
9936 case T___builtin_isless:
9937 case T___builtin_islessequal:
9938 case T___builtin_islessgreater:
9939 case T___builtin_isunordered:
9940 case T___builtin_inf:
9941 case T___builtin_inff:
9942 case T___builtin_infl:
9943 case T___builtin_nan:
9944 case T___builtin_nanf:
9945 case T___builtin_nanl:
9946 case T___builtin_offsetof:
9947 case T___builtin_prefetch:
9948 case T___builtin_va_arg:
9949 case T___builtin_va_end:
9950 case T___builtin_va_start:
9954 statement = parse_expression_statement();
9958 errorf(HERE, "unexpected token %K while parsing statement", &token);
9959 statement = create_invalid_statement();
9964 rem_anchor_token(';');
9966 assert(statement != NULL
9967 && statement->base.source_position.input_name != NULL);
9973 * parse a statement and emits "statement has no effect" warning if needed
9974 * (This is really a wrapper around intern_parse_statement with check for 1
9975 * single warning. It is needed, because for statement expressions we have
9976 * to avoid the warning on the last statement)
9978 static statement_t *parse_statement(void)
9980 statement_t *statement = intern_parse_statement();
9982 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9983 expression_t *expression = statement->expression.expression;
9984 if (!expression_has_effect(expression)) {
9985 warningf(&expression->base.source_position,
9986 "statement has no effect");
9994 * Parse a compound statement.
9996 static statement_t *parse_compound_statement(bool inside_expression_statement)
9998 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
10000 PUSH_PARENT(statement);
10003 add_anchor_token('}');
10005 size_t const top = environment_top();
10006 size_t const top_local = local_label_top();
10007 scope_push(&statement->compound.scope);
10009 statement_t **anchor = &statement->compound.statements;
10010 bool only_decls_so_far = true;
10011 while (token.type != '}') {
10012 if (token.type == T_EOF) {
10013 errorf(&statement->base.source_position,
10014 "EOF while parsing compound statement");
10017 statement_t *sub_statement = intern_parse_statement();
10018 if (is_invalid_statement(sub_statement)) {
10019 /* an error occurred. if we are at an anchor, return */
10025 if (warning.declaration_after_statement) {
10026 if (sub_statement->kind != STATEMENT_DECLARATION) {
10027 only_decls_so_far = false;
10028 } else if (!only_decls_so_far) {
10029 warningf(&sub_statement->base.source_position,
10030 "ISO C90 forbids mixed declarations and code");
10034 *anchor = sub_statement;
10036 while (sub_statement->base.next != NULL)
10037 sub_statement = sub_statement->base.next;
10039 anchor = &sub_statement->base.next;
10043 /* look over all statements again to produce no effect warnings */
10044 if (warning.unused_value) {
10045 statement_t *sub_statement = statement->compound.statements;
10046 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
10047 if (sub_statement->kind != STATEMENT_EXPRESSION)
10049 /* don't emit a warning for the last expression in an expression
10050 * statement as it has always an effect */
10051 if (inside_expression_statement && sub_statement->base.next == NULL)
10054 expression_t *expression = sub_statement->expression.expression;
10055 if (!expression_has_effect(expression)) {
10056 warningf(&expression->base.source_position,
10057 "statement has no effect");
10063 rem_anchor_token('}');
10064 assert(scope == &statement->compound.scope);
10066 environment_pop_to(top);
10067 local_label_pop_to(top_local);
10074 * Initialize builtin types.
10076 static void initialize_builtin_types(void)
10078 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
10079 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
10080 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
10081 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
10082 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
10083 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
10084 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
10085 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
10087 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
10088 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
10089 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
10090 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
10092 /* const version of wchar_t */
10093 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
10094 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
10095 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
10097 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
10101 * Check for unused global static functions and variables
10103 static void check_unused_globals(void)
10105 if (!warning.unused_function && !warning.unused_variable)
10108 for (const declaration_t *decl = file_scope->declarations; decl != NULL; decl = decl->next) {
10110 decl->modifiers & DM_UNUSED ||
10111 decl->modifiers & DM_USED ||
10112 decl->storage_class != STORAGE_CLASS_STATIC)
10115 type_t *const type = decl->type;
10117 if (is_type_function(skip_typeref(type))) {
10118 if (!warning.unused_function || decl->is_inline)
10121 s = (decl->init.statement != NULL ? "defined" : "declared");
10123 if (!warning.unused_variable)
10129 warningf(&decl->source_position, "'%#T' %s but not used",
10130 type, decl->symbol, s);
10134 static void parse_global_asm(void)
10136 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
10141 statement->asms.asm_text = parse_string_literals();
10142 statement->base.next = unit->global_asm;
10143 unit->global_asm = statement;
10152 * Parse a translation unit.
10154 static void parse_translation_unit(void)
10156 add_anchor_token(T_EOF);
10159 unsigned char token_anchor_copy[T_LAST_TOKEN];
10160 memcpy(token_anchor_copy, token_anchor_set, sizeof(token_anchor_copy));
10164 bool anchor_leak = false;
10165 for (int i = 0; i != T_LAST_TOKEN; ++i) {
10166 unsigned char count = token_anchor_set[i] - token_anchor_copy[i];
10168 errorf(HERE, "Leaked anchor token %k %d times", i, count);
10169 anchor_leak = true;
10172 if (in_gcc_extension) {
10173 errorf(HERE, "Leaked __extension__");
10174 anchor_leak = true;
10181 switch (token.type) {
10184 case T___extension__:
10185 parse_external_declaration();
10189 parse_global_asm();
10193 rem_anchor_token(T_EOF);
10197 if (!strict_mode) {
10198 warningf(HERE, "stray ';' outside of function");
10205 errorf(HERE, "stray %K outside of function", &token);
10206 if (token.type == '(' || token.type == '{' || token.type == '[')
10207 eat_until_matching_token(token.type);
10217 * @return the translation unit or NULL if errors occurred.
10219 void start_parsing(void)
10221 environment_stack = NEW_ARR_F(stack_entry_t, 0);
10222 label_stack = NEW_ARR_F(stack_entry_t, 0);
10223 local_label_stack = NEW_ARR_F(stack_entry_t, 0);
10224 diagnostic_count = 0;
10228 type_set_output(stderr);
10229 ast_set_output(stderr);
10231 assert(unit == NULL);
10232 unit = allocate_ast_zero(sizeof(unit[0]));
10234 assert(file_scope == NULL);
10235 file_scope = &unit->scope;
10237 assert(scope == NULL);
10238 scope_push(&unit->scope);
10240 initialize_builtin_types();
10243 translation_unit_t *finish_parsing(void)
10245 /* do NOT use scope_pop() here, this will crash, will it by hand */
10246 assert(scope == &unit->scope);
10248 last_declaration = NULL;
10250 assert(file_scope == &unit->scope);
10251 check_unused_globals();
10254 DEL_ARR_F(environment_stack);
10255 DEL_ARR_F(label_stack);
10256 DEL_ARR_F(local_label_stack);
10258 translation_unit_t *result = unit;
10265 lookahead_bufpos = 0;
10266 for (int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
10269 parse_translation_unit();
10273 * Initialize the parser.
10275 void init_parser(void)
10277 sym_anonymous = symbol_table_insert("<anonymous>");
10279 if (c_mode & _MS) {
10280 /* add predefined symbols for extended-decl-modifier */
10281 sym_align = symbol_table_insert("align");
10282 sym_allocate = symbol_table_insert("allocate");
10283 sym_dllimport = symbol_table_insert("dllimport");
10284 sym_dllexport = symbol_table_insert("dllexport");
10285 sym_naked = symbol_table_insert("naked");
10286 sym_noinline = symbol_table_insert("noinline");
10287 sym_noreturn = symbol_table_insert("noreturn");
10288 sym_nothrow = symbol_table_insert("nothrow");
10289 sym_novtable = symbol_table_insert("novtable");
10290 sym_property = symbol_table_insert("property");
10291 sym_get = symbol_table_insert("get");
10292 sym_put = symbol_table_insert("put");
10293 sym_selectany = symbol_table_insert("selectany");
10294 sym_thread = symbol_table_insert("thread");
10295 sym_uuid = symbol_table_insert("uuid");
10296 sym_deprecated = symbol_table_insert("deprecated");
10297 sym_restrict = symbol_table_insert("restrict");
10298 sym_noalias = symbol_table_insert("noalias");
10300 memset(token_anchor_set, 0, sizeof(token_anchor_set));
10302 init_expression_parsers();
10303 obstack_init(&temp_obst);
10305 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
10306 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
10310 * Terminate the parser.
10312 void exit_parser(void)
10314 obstack_free(&temp_obst, NULL);