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"
37 #include "lang_features.h"
38 #include "walk_statements.h"
40 #include "adt/bitfiddle.h"
41 #include "adt/error.h"
42 #include "adt/array.h"
44 //#define PRINT_TOKENS
45 #define MAX_LOOKAHEAD 1
50 entity_namespace_t namespc;
53 typedef struct argument_list_t argument_list_t;
54 struct argument_list_t {
56 argument_list_t *next;
59 typedef struct gnu_attribute_t gnu_attribute_t;
60 struct gnu_attribute_t {
61 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
62 gnu_attribute_t *next;
63 bool invalid; /**< Set if this attribute had argument errors, */
64 bool has_arguments; /**< True, if this attribute has arguments. */
69 long argument; /**< Single argument. */
70 argument_list_t *arguments; /**< List of argument expressions. */
74 typedef struct declaration_specifiers_t declaration_specifiers_t;
75 struct declaration_specifiers_t {
76 source_position_t source_position;
77 storage_class_t storage_class;
78 unsigned char alignment; /**< Alignment, 0 if not set. */
80 bool thread_local : 1; /**< GCC __thread */
82 decl_modifiers_t modifiers; /**< declaration modifiers */
83 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
84 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
85 symbol_t *get_property_sym; /**< the name of the get property if set. */
86 symbol_t *put_property_sym; /**< the name of the put property if set. */
91 * An environment for parsing initializers (and compound literals).
93 typedef struct parse_initializer_env_t {
94 type_t *type; /**< the type of the initializer. In case of an
95 array type with unspecified size this gets
96 adjusted to the actual size. */
97 entity_t *entity; /**< the variable that is initialized if any */
98 bool must_be_constant;
99 } parse_initializer_env_t;
102 * Capture a MS __base extension.
104 typedef struct based_spec_t {
105 source_position_t source_position;
106 variable_t *base_variable;
109 typedef entity_t* (*parsed_declaration_func) (entity_t *declaration, bool is_definition);
111 /** The current token. */
112 static token_t token;
113 /** The lookahead ring-buffer. */
114 static token_t lookahead_buffer[MAX_LOOKAHEAD];
115 /** Position of the next token in the lookahead buffer. */
116 static size_t lookahead_bufpos;
117 static stack_entry_t *environment_stack = NULL;
118 static stack_entry_t *label_stack = NULL;
119 static scope_t *file_scope = NULL;
120 static scope_t *current_scope = NULL;
121 /** Point to the current function declaration if inside a function. */
122 static function_t *current_function = NULL;
123 static entity_t *current_init_decl = NULL;
124 static switch_statement_t *current_switch = NULL;
125 static statement_t *current_loop = NULL;
126 static statement_t *current_parent = NULL;
127 static ms_try_statement_t *current_try = NULL;
128 static linkage_kind_t current_linkage = LINKAGE_INVALID;
129 static goto_statement_t *goto_first = NULL;
130 static goto_statement_t **goto_anchor = NULL;
131 static label_statement_t *label_first = NULL;
132 static label_statement_t **label_anchor = NULL;
133 /** current translation unit. */
134 static translation_unit_t *unit = NULL;
135 /** true if we are in a type property context (evaluation only for type. */
136 static bool in_type_prop = false;
137 /** true in we are in a __extension__ context. */
138 static bool in_gcc_extension = false;
139 static struct obstack temp_obst;
140 static entity_t *anonymous_entity;
141 static declaration_t **incomplete_arrays;
144 #define PUSH_PARENT(stmt) \
145 statement_t *const prev_parent = current_parent; \
146 ((void)(current_parent = (stmt)))
147 #define POP_PARENT ((void)(current_parent = prev_parent))
149 /** special symbol used for anonymous entities. */
150 static const symbol_t *sym_anonymous = NULL;
152 /* symbols for Microsoft extended-decl-modifier */
153 static const symbol_t *sym_align = NULL;
154 static const symbol_t *sym_allocate = NULL;
155 static const symbol_t *sym_dllimport = NULL;
156 static const symbol_t *sym_dllexport = NULL;
157 static const symbol_t *sym_naked = NULL;
158 static const symbol_t *sym_noinline = NULL;
159 static const symbol_t *sym_returns_twice = NULL;
160 static const symbol_t *sym_noreturn = NULL;
161 static const symbol_t *sym_nothrow = NULL;
162 static const symbol_t *sym_novtable = NULL;
163 static const symbol_t *sym_property = NULL;
164 static const symbol_t *sym_get = NULL;
165 static const symbol_t *sym_put = NULL;
166 static const symbol_t *sym_selectany = NULL;
167 static const symbol_t *sym_thread = NULL;
168 static const symbol_t *sym_uuid = NULL;
169 static const symbol_t *sym_deprecated = NULL;
170 static const symbol_t *sym_restrict = NULL;
171 static const symbol_t *sym_noalias = NULL;
173 /** The token anchor set */
174 static unsigned char token_anchor_set[T_LAST_TOKEN];
176 /** The current source position. */
177 #define HERE (&token.source_position)
179 /** true if we are in GCC mode. */
180 #define GNU_MODE ((c_mode & _GNUC) || in_gcc_extension)
182 static type_t *type_valist;
184 static statement_t *parse_compound_statement(bool inside_expression_statement);
185 static statement_t *parse_statement(void);
187 static expression_t *parse_sub_expression(precedence_t);
188 static expression_t *parse_expression(void);
189 static type_t *parse_typename(void);
190 static void parse_externals(void);
191 static void parse_external(void);
193 static void parse_compound_type_entries(compound_t *compound_declaration);
195 typedef enum declarator_flags_t {
197 DECL_MAY_BE_ABSTRACT = 1U << 0,
198 DECL_CREATE_COMPOUND_MEMBER = 1U << 1,
199 DECL_IS_PARAMETER = 1U << 2
200 } declarator_flags_t;
202 static entity_t *parse_declarator(const declaration_specifiers_t *specifiers,
203 declarator_flags_t flags);
205 static entity_t *record_entity(entity_t *entity, bool is_definition);
207 static void semantic_comparison(binary_expression_t *expression);
209 static void create_gnu_builtins(void);
210 static void create_microsoft_intrinsics(void);
212 #define STORAGE_CLASSES \
213 STORAGE_CLASSES_NO_EXTERN \
216 #define STORAGE_CLASSES_NO_EXTERN \
223 #define TYPE_QUALIFIERS \
228 case T__forceinline: \
229 case T___attribute__:
231 #define COMPLEX_SPECIFIERS \
233 #define IMAGINARY_SPECIFIERS \
236 #define TYPE_SPECIFIERS \
238 case T___builtin_va_list: \
258 #define DECLARATION_START \
263 #define DECLARATION_START_NO_EXTERN \
264 STORAGE_CLASSES_NO_EXTERN \
268 #define TYPENAME_START \
272 #define EXPRESSION_START \
281 case T_CHARACTER_CONSTANT: \
282 case T_FLOATINGPOINT: \
286 case T_STRING_LITERAL: \
287 case T_WIDE_CHARACTER_CONSTANT: \
288 case T_WIDE_STRING_LITERAL: \
289 case T___FUNCDNAME__: \
290 case T___FUNCSIG__: \
291 case T___FUNCTION__: \
292 case T___PRETTY_FUNCTION__: \
293 case T___alignof__: \
294 case T___builtin_classify_type: \
295 case T___builtin_constant_p: \
296 case T___builtin_isgreater: \
297 case T___builtin_isgreaterequal: \
298 case T___builtin_isless: \
299 case T___builtin_islessequal: \
300 case T___builtin_islessgreater: \
301 case T___builtin_isunordered: \
302 case T___builtin_offsetof: \
303 case T___builtin_va_arg: \
304 case T___builtin_va_start: \
315 * Allocate an AST node with given size and
316 * initialize all fields with zero.
318 static void *allocate_ast_zero(size_t size)
320 void *res = allocate_ast(size);
321 memset(res, 0, size);
326 * Returns the size of an entity node.
328 * @param kind the entity kind
330 static size_t get_entity_struct_size(entity_kind_t kind)
332 static const size_t sizes[] = {
333 [ENTITY_VARIABLE] = sizeof(variable_t),
334 [ENTITY_PARAMETER] = sizeof(parameter_t),
335 [ENTITY_COMPOUND_MEMBER] = sizeof(compound_member_t),
336 [ENTITY_FUNCTION] = sizeof(function_t),
337 [ENTITY_TYPEDEF] = sizeof(typedef_t),
338 [ENTITY_STRUCT] = sizeof(compound_t),
339 [ENTITY_UNION] = sizeof(compound_t),
340 [ENTITY_ENUM] = sizeof(enum_t),
341 [ENTITY_ENUM_VALUE] = sizeof(enum_value_t),
342 [ENTITY_LABEL] = sizeof(label_t),
343 [ENTITY_LOCAL_LABEL] = sizeof(label_t),
344 [ENTITY_NAMESPACE] = sizeof(namespace_t)
346 assert(kind < lengthof(sizes));
347 assert(sizes[kind] != 0);
352 * Allocate an entity of given kind and initialize all
355 static entity_t *allocate_entity_zero(entity_kind_t kind)
357 size_t size = get_entity_struct_size(kind);
358 entity_t *entity = allocate_ast_zero(size);
364 * Returns the size of a statement node.
366 * @param kind the statement kind
368 static size_t get_statement_struct_size(statement_kind_t kind)
370 static const size_t sizes[] = {
371 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
372 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
373 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
374 [STATEMENT_RETURN] = sizeof(return_statement_t),
375 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
376 [STATEMENT_IF] = sizeof(if_statement_t),
377 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
378 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
379 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
380 [STATEMENT_BREAK] = sizeof(statement_base_t),
381 [STATEMENT_GOTO] = sizeof(goto_statement_t),
382 [STATEMENT_LABEL] = sizeof(label_statement_t),
383 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
384 [STATEMENT_WHILE] = sizeof(while_statement_t),
385 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
386 [STATEMENT_FOR] = sizeof(for_statement_t),
387 [STATEMENT_ASM] = sizeof(asm_statement_t),
388 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
389 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
391 assert(kind < lengthof(sizes));
392 assert(sizes[kind] != 0);
397 * Returns the size of an expression node.
399 * @param kind the expression kind
401 static size_t get_expression_struct_size(expression_kind_t kind)
403 static const size_t sizes[] = {
404 [EXPR_INVALID] = sizeof(expression_base_t),
405 [EXPR_REFERENCE] = sizeof(reference_expression_t),
406 [EXPR_REFERENCE_ENUM_VALUE] = sizeof(reference_expression_t),
407 [EXPR_CONST] = sizeof(const_expression_t),
408 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
409 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
410 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
411 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
412 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
413 [EXPR_CALL] = sizeof(call_expression_t),
414 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
415 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
416 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
417 [EXPR_SELECT] = sizeof(select_expression_t),
418 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
419 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
420 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
421 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
422 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
423 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
424 [EXPR_BUILTIN_TYPES_COMPATIBLE_P] = sizeof(builtin_types_compatible_expression_t),
425 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
426 [EXPR_VA_START] = sizeof(va_start_expression_t),
427 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
428 [EXPR_STATEMENT] = sizeof(statement_expression_t),
429 [EXPR_LABEL_ADDRESS] = sizeof(label_address_expression_t),
431 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
432 return sizes[EXPR_UNARY_FIRST];
434 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
435 return sizes[EXPR_BINARY_FIRST];
437 assert(kind < lengthof(sizes));
438 assert(sizes[kind] != 0);
443 * Allocate a statement node of given kind and initialize all
444 * fields with zero. Sets its source position to the position
445 * of the current token.
447 static statement_t *allocate_statement_zero(statement_kind_t kind)
449 size_t size = get_statement_struct_size(kind);
450 statement_t *res = allocate_ast_zero(size);
452 res->base.kind = kind;
453 res->base.parent = current_parent;
454 res->base.source_position = token.source_position;
459 * Allocate an expression node of given kind and initialize all
462 static expression_t *allocate_expression_zero(expression_kind_t kind)
464 size_t size = get_expression_struct_size(kind);
465 expression_t *res = allocate_ast_zero(size);
467 res->base.kind = kind;
468 res->base.type = type_error_type;
469 res->base.source_position = token.source_position;
474 * Creates a new invalid expression at the source position
475 * of the current token.
477 static expression_t *create_invalid_expression(void)
479 return allocate_expression_zero(EXPR_INVALID);
483 * Creates a new invalid statement.
485 static statement_t *create_invalid_statement(void)
487 return allocate_statement_zero(STATEMENT_INVALID);
491 * Allocate a new empty statement.
493 static statement_t *create_empty_statement(void)
495 return allocate_statement_zero(STATEMENT_EMPTY);
499 * Returns the size of a type node.
501 * @param kind the type kind
503 static size_t get_type_struct_size(type_kind_t kind)
505 static const size_t sizes[] = {
506 [TYPE_ATOMIC] = sizeof(atomic_type_t),
507 [TYPE_COMPLEX] = sizeof(complex_type_t),
508 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
509 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
510 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
511 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
512 [TYPE_ENUM] = sizeof(enum_type_t),
513 [TYPE_FUNCTION] = sizeof(function_type_t),
514 [TYPE_POINTER] = sizeof(pointer_type_t),
515 [TYPE_ARRAY] = sizeof(array_type_t),
516 [TYPE_BUILTIN] = sizeof(builtin_type_t),
517 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
518 [TYPE_TYPEOF] = sizeof(typeof_type_t),
520 assert(lengthof(sizes) == (int)TYPE_TYPEOF + 1);
521 assert(kind <= TYPE_TYPEOF);
522 assert(sizes[kind] != 0);
527 * Allocate a type node of given kind and initialize all
530 * @param kind type kind to allocate
532 static type_t *allocate_type_zero(type_kind_t kind)
534 size_t size = get_type_struct_size(kind);
535 type_t *res = obstack_alloc(type_obst, size);
536 memset(res, 0, size);
537 res->base.kind = kind;
543 * Returns the size of an initializer node.
545 * @param kind the initializer kind
547 static size_t get_initializer_size(initializer_kind_t kind)
549 static const size_t sizes[] = {
550 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
551 [INITIALIZER_STRING] = sizeof(initializer_string_t),
552 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
553 [INITIALIZER_LIST] = sizeof(initializer_list_t),
554 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
556 assert(kind < lengthof(sizes));
557 assert(sizes[kind] != 0);
562 * Allocate an initializer node of given kind and initialize all
565 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
567 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
574 * Returns the index of the top element of the environment stack.
576 static size_t environment_top(void)
578 return ARR_LEN(environment_stack);
582 * Returns the index of the top element of the global label stack.
584 static size_t label_top(void)
586 return ARR_LEN(label_stack);
590 * Return the next token.
592 static inline void next_token(void)
594 token = lookahead_buffer[lookahead_bufpos];
595 lookahead_buffer[lookahead_bufpos] = lexer_token;
598 lookahead_bufpos = (lookahead_bufpos + 1) % MAX_LOOKAHEAD;
601 print_token(stderr, &token);
602 fprintf(stderr, "\n");
607 * Return the next token with a given lookahead.
609 static inline const token_t *look_ahead(size_t num)
611 assert(0 < num && num <= MAX_LOOKAHEAD);
612 size_t pos = (lookahead_bufpos + num - 1) % MAX_LOOKAHEAD;
613 return &lookahead_buffer[pos];
617 * Adds a token type to the token type anchor set (a multi-set).
619 static void add_anchor_token(int token_type)
621 assert(0 <= token_type && token_type < T_LAST_TOKEN);
622 ++token_anchor_set[token_type];
626 * Set the number of tokens types of the given type
627 * to zero and return the old count.
629 static int save_and_reset_anchor_state(int token_type)
631 assert(0 <= token_type && token_type < T_LAST_TOKEN);
632 int count = token_anchor_set[token_type];
633 token_anchor_set[token_type] = 0;
638 * Restore the number of token types to the given count.
640 static void restore_anchor_state(int token_type, int count)
642 assert(0 <= token_type && token_type < T_LAST_TOKEN);
643 token_anchor_set[token_type] = count;
647 * Remove a token type from the token type anchor set (a multi-set).
649 static void rem_anchor_token(int token_type)
651 assert(0 <= token_type && token_type < T_LAST_TOKEN);
652 assert(token_anchor_set[token_type] != 0);
653 --token_anchor_set[token_type];
657 * Return true if the token type of the current token is
660 static bool at_anchor(void)
664 return token_anchor_set[token.type];
668 * Eat tokens until a matching token type is found.
670 static void eat_until_matching_token(int type)
674 case '(': end_token = ')'; break;
675 case '{': end_token = '}'; break;
676 case '[': end_token = ']'; break;
677 default: end_token = type; break;
680 unsigned parenthesis_count = 0;
681 unsigned brace_count = 0;
682 unsigned bracket_count = 0;
683 while (token.type != end_token ||
684 parenthesis_count != 0 ||
686 bracket_count != 0) {
687 switch (token.type) {
689 case '(': ++parenthesis_count; break;
690 case '{': ++brace_count; break;
691 case '[': ++bracket_count; break;
694 if (parenthesis_count > 0)
704 if (bracket_count > 0)
707 if (token.type == end_token &&
708 parenthesis_count == 0 &&
722 * Eat input tokens until an anchor is found.
724 static void eat_until_anchor(void)
726 while (token_anchor_set[token.type] == 0) {
727 if (token.type == '(' || token.type == '{' || token.type == '[')
728 eat_until_matching_token(token.type);
734 * Eat a whole block from input tokens.
736 static void eat_block(void)
738 eat_until_matching_token('{');
739 if (token.type == '}')
743 #define eat(token_type) (assert(token.type == (token_type)), next_token())
746 * Report a parse error because an expected token was not found.
749 #if defined __GNUC__ && __GNUC__ >= 4
750 __attribute__((sentinel))
752 void parse_error_expected(const char *message, ...)
754 if (message != NULL) {
755 errorf(HERE, "%s", message);
758 va_start(ap, message);
759 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
764 * Report an incompatible type.
766 static void type_error_incompatible(const char *msg,
767 const source_position_t *source_position, type_t *type1, type_t *type2)
769 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
774 * Expect the current token is the expected token.
775 * If not, generate an error, eat the current statement,
776 * and goto the end_error label.
778 #define expect(expected, error_label) \
780 if (UNLIKELY(token.type != (expected))) { \
781 parse_error_expected(NULL, (expected), NULL); \
782 add_anchor_token(expected); \
783 eat_until_anchor(); \
784 if (token.type == expected) \
786 rem_anchor_token(expected); \
793 * Push a given scope on the scope stack and make it the
796 static scope_t *scope_push(scope_t *new_scope)
798 if (current_scope != NULL) {
799 new_scope->depth = current_scope->depth + 1;
802 scope_t *old_scope = current_scope;
803 current_scope = new_scope;
808 * Pop the current scope from the scope stack.
810 static void scope_pop(scope_t *old_scope)
812 current_scope = old_scope;
816 * Search an entity by its symbol in a given namespace.
818 static entity_t *get_entity(const symbol_t *const symbol,
819 namespace_tag_t namespc)
821 entity_t *entity = symbol->entity;
822 for (; entity != NULL; entity = entity->base.symbol_next) {
823 if (entity->base.namespc == namespc)
831 * pushs an entity on the environment stack and links the corresponding symbol
834 static void stack_push(stack_entry_t **stack_ptr, entity_t *entity)
836 symbol_t *symbol = entity->base.symbol;
837 entity_namespace_t namespc = entity->base.namespc;
838 assert(namespc != NAMESPACE_INVALID);
840 /* replace/add entity into entity list of the symbol */
843 for (anchor = &symbol->entity; ; anchor = &iter->base.symbol_next) {
848 /* replace an entry? */
849 if (iter->base.namespc == namespc) {
850 entity->base.symbol_next = iter->base.symbol_next;
856 /* remember old declaration */
858 entry.symbol = symbol;
859 entry.old_entity = iter;
860 entry.namespc = namespc;
861 ARR_APP1(stack_entry_t, *stack_ptr, entry);
865 * Push an entity on the environment stack.
867 static void environment_push(entity_t *entity)
869 assert(entity->base.source_position.input_name != NULL);
870 assert(entity->base.parent_scope != NULL);
871 stack_push(&environment_stack, entity);
875 * Push a declaration on the global label stack.
877 * @param declaration the declaration
879 static void label_push(entity_t *label)
881 /* we abuse the parameters scope as parent for the labels */
882 label->base.parent_scope = ¤t_function->parameters;
883 stack_push(&label_stack, label);
887 * pops symbols from the environment stack until @p new_top is the top element
889 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
891 stack_entry_t *stack = *stack_ptr;
892 size_t top = ARR_LEN(stack);
895 assert(new_top <= top);
899 for (i = top; i > new_top; --i) {
900 stack_entry_t *entry = &stack[i - 1];
902 entity_t *old_entity = entry->old_entity;
903 symbol_t *symbol = entry->symbol;
904 entity_namespace_t namespc = entry->namespc;
906 /* replace with old_entity/remove */
909 for (anchor = &symbol->entity; ; anchor = &iter->base.symbol_next) {
911 assert(iter != NULL);
912 /* replace an entry? */
913 if (iter->base.namespc == namespc)
917 /* restore definition from outer scopes (if there was one) */
918 if (old_entity != NULL) {
919 old_entity->base.symbol_next = iter->base.symbol_next;
920 *anchor = old_entity;
922 /* remove entry from list */
923 *anchor = iter->base.symbol_next;
927 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
931 * Pop all entries from the environment stack until the new_top
934 * @param new_top the new stack top
936 static void environment_pop_to(size_t new_top)
938 stack_pop_to(&environment_stack, new_top);
942 * Pop all entries from the global label stack until the new_top
945 * @param new_top the new stack top
947 static void label_pop_to(size_t new_top)
949 stack_pop_to(&label_stack, new_top);
952 static int get_akind_rank(atomic_type_kind_t akind)
958 * Return the type rank for an atomic type.
960 static int get_rank(const type_t *type)
962 assert(!is_typeref(type));
963 if (type->kind == TYPE_ENUM)
964 return get_akind_rank(type->enumt.akind);
966 assert(type->kind == TYPE_ATOMIC);
967 return get_akind_rank(type->atomic.akind);
971 * Do integer promotion for a given type.
973 * @param type the type to promote
974 * @return the promoted type
976 static type_t *promote_integer(type_t *type)
978 if (type->kind == TYPE_BITFIELD)
979 type = type->bitfield.base_type;
981 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
988 * Create a cast expression.
990 * @param expression the expression to cast
991 * @param dest_type the destination type
993 static expression_t *create_cast_expression(expression_t *expression,
996 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
998 cast->unary.value = expression;
999 cast->base.type = dest_type;
1005 * Check if a given expression represents a null pointer constant.
1007 * @param expression the expression to check
1009 static bool is_null_pointer_constant(const expression_t *expression)
1011 /* skip void* cast */
1012 if (expression->kind == EXPR_UNARY_CAST ||
1013 expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
1014 type_t *const type = skip_typeref(expression->base.type);
1015 if (types_compatible(type, type_void_ptr))
1016 expression = expression->unary.value;
1019 type_t *const type = skip_typeref(expression->base.type);
1021 is_type_integer(type) &&
1022 is_constant_expression(expression) &&
1023 fold_constant(expression) == 0;
1027 * Create an implicit cast expression.
1029 * @param expression the expression to cast
1030 * @param dest_type the destination type
1032 static expression_t *create_implicit_cast(expression_t *expression,
1035 type_t *const source_type = expression->base.type;
1037 if (source_type == dest_type)
1040 return create_cast_expression(expression, dest_type);
1043 typedef enum assign_error_t {
1045 ASSIGN_ERROR_INCOMPATIBLE,
1046 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
1047 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
1048 ASSIGN_WARNING_POINTER_FROM_INT,
1049 ASSIGN_WARNING_INT_FROM_POINTER
1052 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
1053 const expression_t *const right,
1054 const char *context,
1055 const source_position_t *source_position)
1057 type_t *const orig_type_right = right->base.type;
1058 type_t *const type_left = skip_typeref(orig_type_left);
1059 type_t *const type_right = skip_typeref(orig_type_right);
1062 case ASSIGN_SUCCESS:
1064 case ASSIGN_ERROR_INCOMPATIBLE:
1065 errorf(source_position,
1066 "destination type '%T' in %s is incompatible with type '%T'",
1067 orig_type_left, context, orig_type_right);
1070 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
1071 if (warning.other) {
1072 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
1073 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
1075 /* the left type has all qualifiers from the right type */
1076 unsigned missing_qualifiers
1077 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1078 warningf(source_position,
1079 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
1080 orig_type_left, context, orig_type_right, missing_qualifiers);
1085 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
1086 if (warning.other) {
1087 warningf(source_position,
1088 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
1089 orig_type_left, context, right, orig_type_right);
1093 case ASSIGN_WARNING_POINTER_FROM_INT:
1094 if (warning.other) {
1095 warningf(source_position,
1096 "%s makes pointer '%T' from integer '%T' without a cast",
1097 context, orig_type_left, orig_type_right);
1101 case ASSIGN_WARNING_INT_FROM_POINTER:
1102 if (warning.other) {
1103 warningf(source_position,
1104 "%s makes integer '%T' from pointer '%T' without a cast",
1105 context, orig_type_left, orig_type_right);
1110 panic("invalid error value");
1114 /** Implements the rules from §6.5.16.1 */
1115 static assign_error_t semantic_assign(type_t *orig_type_left,
1116 const expression_t *const right)
1118 type_t *const orig_type_right = right->base.type;
1119 type_t *const type_left = skip_typeref(orig_type_left);
1120 type_t *const type_right = skip_typeref(orig_type_right);
1122 if (is_type_pointer(type_left)) {
1123 if (is_null_pointer_constant(right)) {
1124 return ASSIGN_SUCCESS;
1125 } else if (is_type_pointer(type_right)) {
1126 type_t *points_to_left
1127 = skip_typeref(type_left->pointer.points_to);
1128 type_t *points_to_right
1129 = skip_typeref(type_right->pointer.points_to);
1130 assign_error_t res = ASSIGN_SUCCESS;
1132 /* the left type has all qualifiers from the right type */
1133 unsigned missing_qualifiers
1134 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1135 if (missing_qualifiers != 0) {
1136 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1139 points_to_left = get_unqualified_type(points_to_left);
1140 points_to_right = get_unqualified_type(points_to_right);
1142 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID))
1145 if (is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1146 /* ISO/IEC 14882:1998(E) §C.1.2:6 */
1147 return c_mode & _CXX ? ASSIGN_ERROR_INCOMPATIBLE : res;
1150 if (!types_compatible(points_to_left, points_to_right)) {
1151 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1155 } else if (is_type_integer(type_right)) {
1156 return ASSIGN_WARNING_POINTER_FROM_INT;
1158 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1159 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1160 && is_type_pointer(type_right))) {
1161 return ASSIGN_SUCCESS;
1162 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1163 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1164 type_t *const unqual_type_left = get_unqualified_type(type_left);
1165 type_t *const unqual_type_right = get_unqualified_type(type_right);
1166 if (types_compatible(unqual_type_left, unqual_type_right)) {
1167 return ASSIGN_SUCCESS;
1169 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1170 return ASSIGN_WARNING_INT_FROM_POINTER;
1173 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1174 return ASSIGN_SUCCESS;
1176 return ASSIGN_ERROR_INCOMPATIBLE;
1179 static expression_t *parse_constant_expression(void)
1181 expression_t *result = parse_sub_expression(PREC_CONDITIONAL);
1183 if (!is_constant_expression(result)) {
1184 errorf(&result->base.source_position,
1185 "expression '%E' is not constant", result);
1191 static expression_t *parse_assignment_expression(void)
1193 return parse_sub_expression(PREC_ASSIGNMENT);
1196 static string_t parse_string_literals(void)
1198 assert(token.type == T_STRING_LITERAL);
1199 string_t result = token.v.string;
1203 while (token.type == T_STRING_LITERAL) {
1204 result = concat_strings(&result, &token.v.string);
1211 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1212 [GNU_AK_CONST] = "const",
1213 [GNU_AK_VOLATILE] = "volatile",
1214 [GNU_AK_CDECL] = "cdecl",
1215 [GNU_AK_STDCALL] = "stdcall",
1216 [GNU_AK_FASTCALL] = "fastcall",
1217 [GNU_AK_DEPRECATED] = "deprecated",
1218 [GNU_AK_NOINLINE] = "noinline",
1219 [GNU_AK_RETURNS_TWICE] = "returns_twice",
1220 [GNU_AK_NORETURN] = "noreturn",
1221 [GNU_AK_NAKED] = "naked",
1222 [GNU_AK_PURE] = "pure",
1223 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1224 [GNU_AK_MALLOC] = "malloc",
1225 [GNU_AK_WEAK] = "weak",
1226 [GNU_AK_CONSTRUCTOR] = "constructor",
1227 [GNU_AK_DESTRUCTOR] = "destructor",
1228 [GNU_AK_NOTHROW] = "nothrow",
1229 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1230 [GNU_AK_COMMON] = "common",
1231 [GNU_AK_NOCOMMON] = "nocommon",
1232 [GNU_AK_PACKED] = "packed",
1233 [GNU_AK_SHARED] = "shared",
1234 [GNU_AK_NOTSHARED] = "notshared",
1235 [GNU_AK_USED] = "used",
1236 [GNU_AK_UNUSED] = "unused",
1237 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1238 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1239 [GNU_AK_LONGCALL] = "longcall",
1240 [GNU_AK_SHORTCALL] = "shortcall",
1241 [GNU_AK_LONG_CALL] = "long_call",
1242 [GNU_AK_SHORT_CALL] = "short_call",
1243 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1244 [GNU_AK_INTERRUPT] = "interrupt",
1245 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1246 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1247 [GNU_AK_NESTING] = "nesting",
1248 [GNU_AK_NEAR] = "near",
1249 [GNU_AK_FAR] = "far",
1250 [GNU_AK_SIGNAL] = "signal",
1251 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1252 [GNU_AK_TINY_DATA] = "tiny_data",
1253 [GNU_AK_SAVEALL] = "saveall",
1254 [GNU_AK_FLATTEN] = "flatten",
1255 [GNU_AK_SSEREGPARM] = "sseregparm",
1256 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1257 [GNU_AK_RETURN_TWICE] = "return_twice",
1258 [GNU_AK_MAY_ALIAS] = "may_alias",
1259 [GNU_AK_MS_STRUCT] = "ms_struct",
1260 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1261 [GNU_AK_DLLIMPORT] = "dllimport",
1262 [GNU_AK_DLLEXPORT] = "dllexport",
1263 [GNU_AK_ALIGNED] = "aligned",
1264 [GNU_AK_ALIAS] = "alias",
1265 [GNU_AK_SECTION] = "section",
1266 [GNU_AK_FORMAT] = "format",
1267 [GNU_AK_FORMAT_ARG] = "format_arg",
1268 [GNU_AK_WEAKREF] = "weakref",
1269 [GNU_AK_NONNULL] = "nonnull",
1270 [GNU_AK_TLS_MODEL] = "tls_model",
1271 [GNU_AK_VISIBILITY] = "visibility",
1272 [GNU_AK_REGPARM] = "regparm",
1273 [GNU_AK_MODE] = "mode",
1274 [GNU_AK_MODEL] = "model",
1275 [GNU_AK_TRAP_EXIT] = "trap_exit",
1276 [GNU_AK_SP_SWITCH] = "sp_switch",
1277 [GNU_AK_SENTINEL] = "sentinel"
1281 * compare two string, ignoring double underscores on the second.
1283 static int strcmp_underscore(const char *s1, const char *s2)
1285 if (s2[0] == '_' && s2[1] == '_') {
1286 size_t len2 = strlen(s2);
1287 size_t len1 = strlen(s1);
1288 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1289 return strncmp(s1, s2+2, len2-4);
1293 return strcmp(s1, s2);
1297 * Allocate a new gnu temporal attribute of given kind.
1299 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1301 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1302 attribute->kind = kind;
1303 attribute->next = NULL;
1304 attribute->invalid = false;
1305 attribute->has_arguments = false;
1311 * Parse one constant expression argument of the given attribute.
1313 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1315 expression_t *expression;
1316 add_anchor_token(')');
1317 expression = parse_constant_expression();
1318 rem_anchor_token(')');
1319 expect(')', end_error);
1320 attribute->u.argument = fold_constant(expression);
1323 attribute->invalid = true;
1327 * Parse a list of constant expressions arguments of the given attribute.
1329 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1331 argument_list_t **list = &attribute->u.arguments;
1332 argument_list_t *entry;
1333 expression_t *expression;
1334 add_anchor_token(')');
1335 add_anchor_token(',');
1337 expression = parse_constant_expression();
1338 entry = obstack_alloc(&temp_obst, sizeof(entry));
1339 entry->argument = fold_constant(expression);
1342 list = &entry->next;
1343 if (token.type != ',')
1347 rem_anchor_token(',');
1348 rem_anchor_token(')');
1349 expect(')', end_error);
1352 attribute->invalid = true;
1356 * Parse one string literal argument of the given attribute.
1358 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1361 add_anchor_token('(');
1362 if (token.type != T_STRING_LITERAL) {
1363 parse_error_expected("while parsing attribute directive",
1364 T_STRING_LITERAL, NULL);
1367 *string = parse_string_literals();
1368 rem_anchor_token('(');
1369 expect(')', end_error);
1372 attribute->invalid = true;
1376 * Parse one tls model of the given attribute.
1378 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1380 static const char *const tls_models[] = {
1386 string_t string = { NULL, 0 };
1387 parse_gnu_attribute_string_arg(attribute, &string);
1388 if (string.begin != NULL) {
1389 for (size_t i = 0; i < 4; ++i) {
1390 if (strcmp(tls_models[i], string.begin) == 0) {
1391 attribute->u.value = i;
1395 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1397 attribute->invalid = true;
1401 * Parse one tls model of the given attribute.
1403 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1405 static const char *const visibilities[] = {
1411 string_t string = { NULL, 0 };
1412 parse_gnu_attribute_string_arg(attribute, &string);
1413 if (string.begin != NULL) {
1414 for (size_t i = 0; i < 4; ++i) {
1415 if (strcmp(visibilities[i], string.begin) == 0) {
1416 attribute->u.value = i;
1420 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1422 attribute->invalid = true;
1426 * Parse one (code) model of the given attribute.
1428 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1430 static const char *const visibilities[] = {
1435 string_t string = { NULL, 0 };
1436 parse_gnu_attribute_string_arg(attribute, &string);
1437 if (string.begin != NULL) {
1438 for (int i = 0; i < 3; ++i) {
1439 if (strcmp(visibilities[i], string.begin) == 0) {
1440 attribute->u.value = i;
1444 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1446 attribute->invalid = true;
1450 * Parse one mode of the given attribute.
1452 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1454 add_anchor_token(')');
1456 if (token.type != T_IDENTIFIER) {
1457 expect(T_IDENTIFIER, end_error);
1460 attribute->u.symbol = token.v.symbol;
1463 rem_anchor_token(')');
1464 expect(')', end_error);
1467 attribute->invalid = true;
1471 * Parse one interrupt argument of the given attribute.
1473 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1475 static const char *const interrupts[] = {
1482 string_t string = { NULL, 0 };
1483 parse_gnu_attribute_string_arg(attribute, &string);
1484 if (string.begin != NULL) {
1485 for (size_t i = 0; i < 5; ++i) {
1486 if (strcmp(interrupts[i], string.begin) == 0) {
1487 attribute->u.value = i;
1491 errorf(HERE, "'%s' is not an interrupt", string.begin);
1493 attribute->invalid = true;
1497 * Parse ( identifier, const expression, const expression )
1499 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1501 static const char *const format_names[] = {
1509 if (token.type != T_IDENTIFIER) {
1510 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1513 const char *name = token.v.symbol->string;
1514 for (i = 0; i < 4; ++i) {
1515 if (strcmp_underscore(format_names[i], name) == 0)
1519 if (warning.attribute)
1520 warningf(HERE, "'%s' is an unrecognized format function type", name);
1524 expect(',', end_error);
1525 add_anchor_token(')');
1526 add_anchor_token(',');
1527 parse_constant_expression();
1528 rem_anchor_token(',');
1529 rem_anchor_token(')');
1531 expect(',', end_error);
1532 add_anchor_token(')');
1533 parse_constant_expression();
1534 rem_anchor_token(')');
1535 expect(')', end_error);
1538 attribute->u.value = true;
1542 * Check that a given GNU attribute has no arguments.
1544 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1546 if (!attribute->has_arguments)
1549 /* should have no arguments */
1550 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1551 eat_until_matching_token('(');
1552 /* we have already consumed '(', so we stop before ')', eat it */
1554 attribute->invalid = true;
1558 * Parse one GNU attribute.
1560 * Note that attribute names can be specified WITH or WITHOUT
1561 * double underscores, ie const or __const__.
1563 * The following attributes are parsed without arguments
1588 * no_instrument_function
1589 * warn_unused_result
1606 * externally_visible
1614 * The following attributes are parsed with arguments
1615 * aligned( const expression )
1616 * alias( string literal )
1617 * section( string literal )
1618 * format( identifier, const expression, const expression )
1619 * format_arg( const expression )
1620 * tls_model( string literal )
1621 * visibility( string literal )
1622 * regparm( const expression )
1623 * model( string leteral )
1624 * trap_exit( const expression )
1625 * sp_switch( string literal )
1627 * The following attributes might have arguments
1628 * weak_ref( string literal )
1629 * non_null( const expression // ',' )
1630 * interrupt( string literal )
1631 * sentinel( constant expression )
1633 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1635 gnu_attribute_t *head = *attributes;
1636 gnu_attribute_t *last = *attributes;
1637 decl_modifiers_t modifiers = 0;
1638 gnu_attribute_t *attribute;
1640 eat(T___attribute__);
1641 expect('(', end_error);
1642 expect('(', end_error);
1644 if (token.type != ')') {
1645 /* find the end of the list */
1647 while (last->next != NULL)
1651 /* non-empty attribute list */
1654 if (token.type == T_const) {
1656 } else if (token.type == T_volatile) {
1658 } else if (token.type == T_cdecl) {
1659 /* __attribute__((cdecl)), WITH ms mode */
1661 } else if (token.type == T_IDENTIFIER) {
1662 const symbol_t *sym = token.v.symbol;
1665 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1672 for (i = 0; i < GNU_AK_LAST; ++i) {
1673 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1676 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1679 if (kind == GNU_AK_LAST) {
1680 if (warning.attribute)
1681 warningf(HERE, "'%s' attribute directive ignored", name);
1683 /* skip possible arguments */
1684 if (token.type == '(') {
1685 eat_until_matching_token(')');
1688 /* check for arguments */
1689 attribute = allocate_gnu_attribute(kind);
1690 if (token.type == '(') {
1692 if (token.type == ')') {
1693 /* empty args are allowed */
1696 attribute->has_arguments = true;
1700 case GNU_AK_VOLATILE:
1705 case GNU_AK_NOCOMMON:
1707 case GNU_AK_NOTSHARED:
1708 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1709 case GNU_AK_WARN_UNUSED_RESULT:
1710 case GNU_AK_LONGCALL:
1711 case GNU_AK_SHORTCALL:
1712 case GNU_AK_LONG_CALL:
1713 case GNU_AK_SHORT_CALL:
1714 case GNU_AK_FUNCTION_VECTOR:
1715 case GNU_AK_INTERRUPT_HANDLER:
1716 case GNU_AK_NMI_HANDLER:
1717 case GNU_AK_NESTING:
1721 case GNU_AK_EIGTHBIT_DATA:
1722 case GNU_AK_TINY_DATA:
1723 case GNU_AK_SAVEALL:
1724 case GNU_AK_FLATTEN:
1725 case GNU_AK_SSEREGPARM:
1726 case GNU_AK_EXTERNALLY_VISIBLE:
1727 case GNU_AK_RETURN_TWICE:
1728 case GNU_AK_MAY_ALIAS:
1729 case GNU_AK_MS_STRUCT:
1730 case GNU_AK_GCC_STRUCT:
1733 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1734 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1735 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1736 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1737 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1738 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1739 case GNU_AK_CONST: modifiers |= DM_CONST; goto no_arg;
1740 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1741 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1742 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1743 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1744 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1745 case GNU_AK_RETURNS_TWICE: modifiers |= DM_RETURNS_TWICE; goto no_arg;
1746 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1747 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1748 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1749 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1750 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1751 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1753 case GNU_AK_ALIGNED:
1754 /* __align__ may be used without an argument */
1755 if (attribute->has_arguments) {
1756 parse_gnu_attribute_const_arg(attribute);
1760 case GNU_AK_FORMAT_ARG:
1761 case GNU_AK_REGPARM:
1762 case GNU_AK_TRAP_EXIT:
1763 if (!attribute->has_arguments) {
1764 /* should have arguments */
1765 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1766 attribute->invalid = true;
1768 parse_gnu_attribute_const_arg(attribute);
1771 case GNU_AK_SECTION:
1772 case GNU_AK_SP_SWITCH:
1773 if (!attribute->has_arguments) {
1774 /* should have arguments */
1775 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1776 attribute->invalid = true;
1778 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1781 if (!attribute->has_arguments) {
1782 /* should have arguments */
1783 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1784 attribute->invalid = true;
1786 parse_gnu_attribute_format_args(attribute);
1788 case GNU_AK_WEAKREF:
1789 /* may have one string argument */
1790 if (attribute->has_arguments)
1791 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1793 case GNU_AK_NONNULL:
1794 if (attribute->has_arguments)
1795 parse_gnu_attribute_const_arg_list(attribute);
1797 case GNU_AK_TLS_MODEL:
1798 if (!attribute->has_arguments) {
1799 /* should have arguments */
1800 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1802 parse_gnu_attribute_tls_model_arg(attribute);
1804 case GNU_AK_VISIBILITY:
1805 if (!attribute->has_arguments) {
1806 /* should have arguments */
1807 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1809 parse_gnu_attribute_visibility_arg(attribute);
1812 if (!attribute->has_arguments) {
1813 /* should have arguments */
1814 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1816 parse_gnu_attribute_model_arg(attribute);
1820 if (!attribute->has_arguments) {
1821 /* should have arguments */
1822 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1824 parse_gnu_attribute_mode_arg(attribute);
1827 case GNU_AK_INTERRUPT:
1828 /* may have one string argument */
1829 if (attribute->has_arguments)
1830 parse_gnu_attribute_interrupt_arg(attribute);
1832 case GNU_AK_SENTINEL:
1833 /* may have one string argument */
1834 if (attribute->has_arguments)
1835 parse_gnu_attribute_const_arg(attribute);
1838 /* already handled */
1842 check_no_argument(attribute, name);
1845 if (attribute != NULL) {
1847 last->next = attribute;
1850 head = last = attribute;
1854 if (token.type != ',')
1859 expect(')', end_error);
1860 expect(')', end_error);
1868 * Parse GNU attributes.
1870 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1872 decl_modifiers_t modifiers = 0;
1875 switch (token.type) {
1876 case T___attribute__:
1877 modifiers |= parse_gnu_attribute(attributes);
1882 expect('(', end_error);
1883 if (token.type != T_STRING_LITERAL) {
1884 parse_error_expected("while parsing assembler attribute",
1885 T_STRING_LITERAL, NULL);
1886 eat_until_matching_token('(');
1889 parse_string_literals();
1891 expect(')', end_error);
1894 case T_cdecl: modifiers |= DM_CDECL; break;
1895 case T__fastcall: modifiers |= DM_FASTCALL; break;
1896 case T__stdcall: modifiers |= DM_STDCALL; break;
1899 /* TODO record modifier */
1901 warningf(HERE, "Ignoring declaration modifier %K", &token);
1905 default: return modifiers;
1912 static void mark_vars_read(expression_t *expr, entity_t *lhs_ent);
1914 static entity_t *determine_lhs_ent(expression_t *const expr,
1917 switch (expr->kind) {
1918 case EXPR_REFERENCE: {
1919 entity_t *const entity = expr->reference.entity;
1920 /* we should only find variables as lvalues... */
1921 if (entity->base.kind != ENTITY_VARIABLE
1922 && entity->base.kind != ENTITY_PARAMETER)
1928 case EXPR_ARRAY_ACCESS: {
1929 expression_t *const ref = expr->array_access.array_ref;
1930 entity_t * ent = NULL;
1931 if (is_type_array(skip_typeref(revert_automatic_type_conversion(ref)))) {
1932 ent = determine_lhs_ent(ref, lhs_ent);
1935 mark_vars_read(expr->select.compound, lhs_ent);
1937 mark_vars_read(expr->array_access.index, lhs_ent);
1942 if (is_type_compound(skip_typeref(expr->base.type))) {
1943 return determine_lhs_ent(expr->select.compound, lhs_ent);
1945 mark_vars_read(expr->select.compound, lhs_ent);
1950 case EXPR_UNARY_DEREFERENCE: {
1951 expression_t *const val = expr->unary.value;
1952 if (val->kind == EXPR_UNARY_TAKE_ADDRESS) {
1954 return determine_lhs_ent(val->unary.value, lhs_ent);
1956 mark_vars_read(val, NULL);
1962 mark_vars_read(expr, NULL);
1967 #define ENT_ANY ((entity_t*)-1)
1970 * Mark declarations, which are read. This is used to detect variables, which
1974 * x is not marked as "read", because it is only read to calculate its own new
1978 * x and y are not detected as "not read", because multiple variables are
1981 static void mark_vars_read(expression_t *const expr, entity_t *lhs_ent)
1983 switch (expr->kind) {
1984 case EXPR_REFERENCE: {
1985 entity_t *const entity = expr->reference.entity;
1986 if (entity->kind != ENTITY_VARIABLE
1987 && entity->kind != ENTITY_PARAMETER)
1990 if (lhs_ent != entity && lhs_ent != ENT_ANY) {
1991 if (entity->kind == ENTITY_VARIABLE) {
1992 entity->variable.read = true;
1994 entity->parameter.read = true;
2001 // TODO respect pure/const
2002 mark_vars_read(expr->call.function, NULL);
2003 for (call_argument_t *arg = expr->call.arguments; arg != NULL; arg = arg->next) {
2004 mark_vars_read(arg->expression, NULL);
2008 case EXPR_CONDITIONAL:
2009 // TODO lhs_decl should depend on whether true/false have an effect
2010 mark_vars_read(expr->conditional.condition, NULL);
2011 if (expr->conditional.true_expression != NULL)
2012 mark_vars_read(expr->conditional.true_expression, lhs_ent);
2013 mark_vars_read(expr->conditional.false_expression, lhs_ent);
2017 if (lhs_ent == ENT_ANY
2018 && !is_type_compound(skip_typeref(expr->base.type)))
2020 mark_vars_read(expr->select.compound, lhs_ent);
2023 case EXPR_ARRAY_ACCESS: {
2024 expression_t *const ref = expr->array_access.array_ref;
2025 mark_vars_read(ref, lhs_ent);
2026 lhs_ent = determine_lhs_ent(ref, lhs_ent);
2027 mark_vars_read(expr->array_access.index, lhs_ent);
2032 mark_vars_read(expr->va_arge.ap, lhs_ent);
2035 case EXPR_UNARY_CAST:
2036 /* Special case: Use void cast to mark a variable as "read" */
2037 if (is_type_atomic(skip_typeref(expr->base.type), ATOMIC_TYPE_VOID))
2042 case EXPR_UNARY_THROW:
2043 if (expr->unary.value == NULL)
2046 case EXPR_UNARY_DEREFERENCE:
2047 case EXPR_UNARY_DELETE:
2048 case EXPR_UNARY_DELETE_ARRAY:
2049 if (lhs_ent == ENT_ANY)
2053 case EXPR_UNARY_NEGATE:
2054 case EXPR_UNARY_PLUS:
2055 case EXPR_UNARY_BITWISE_NEGATE:
2056 case EXPR_UNARY_NOT:
2057 case EXPR_UNARY_TAKE_ADDRESS:
2058 case EXPR_UNARY_POSTFIX_INCREMENT:
2059 case EXPR_UNARY_POSTFIX_DECREMENT:
2060 case EXPR_UNARY_PREFIX_INCREMENT:
2061 case EXPR_UNARY_PREFIX_DECREMENT:
2062 case EXPR_UNARY_CAST_IMPLICIT:
2063 case EXPR_UNARY_ASSUME:
2065 mark_vars_read(expr->unary.value, lhs_ent);
2068 case EXPR_BINARY_ADD:
2069 case EXPR_BINARY_SUB:
2070 case EXPR_BINARY_MUL:
2071 case EXPR_BINARY_DIV:
2072 case EXPR_BINARY_MOD:
2073 case EXPR_BINARY_EQUAL:
2074 case EXPR_BINARY_NOTEQUAL:
2075 case EXPR_BINARY_LESS:
2076 case EXPR_BINARY_LESSEQUAL:
2077 case EXPR_BINARY_GREATER:
2078 case EXPR_BINARY_GREATEREQUAL:
2079 case EXPR_BINARY_BITWISE_AND:
2080 case EXPR_BINARY_BITWISE_OR:
2081 case EXPR_BINARY_BITWISE_XOR:
2082 case EXPR_BINARY_LOGICAL_AND:
2083 case EXPR_BINARY_LOGICAL_OR:
2084 case EXPR_BINARY_SHIFTLEFT:
2085 case EXPR_BINARY_SHIFTRIGHT:
2086 case EXPR_BINARY_COMMA:
2087 case EXPR_BINARY_ISGREATER:
2088 case EXPR_BINARY_ISGREATEREQUAL:
2089 case EXPR_BINARY_ISLESS:
2090 case EXPR_BINARY_ISLESSEQUAL:
2091 case EXPR_BINARY_ISLESSGREATER:
2092 case EXPR_BINARY_ISUNORDERED:
2093 mark_vars_read(expr->binary.left, lhs_ent);
2094 mark_vars_read(expr->binary.right, lhs_ent);
2097 case EXPR_BINARY_ASSIGN:
2098 case EXPR_BINARY_MUL_ASSIGN:
2099 case EXPR_BINARY_DIV_ASSIGN:
2100 case EXPR_BINARY_MOD_ASSIGN:
2101 case EXPR_BINARY_ADD_ASSIGN:
2102 case EXPR_BINARY_SUB_ASSIGN:
2103 case EXPR_BINARY_SHIFTLEFT_ASSIGN:
2104 case EXPR_BINARY_SHIFTRIGHT_ASSIGN:
2105 case EXPR_BINARY_BITWISE_AND_ASSIGN:
2106 case EXPR_BINARY_BITWISE_XOR_ASSIGN:
2107 case EXPR_BINARY_BITWISE_OR_ASSIGN: {
2108 if (lhs_ent == ENT_ANY)
2110 lhs_ent = determine_lhs_ent(expr->binary.left, lhs_ent);
2111 mark_vars_read(expr->binary.right, lhs_ent);
2116 determine_lhs_ent(expr->va_starte.ap, lhs_ent);
2122 case EXPR_CHARACTER_CONSTANT:
2123 case EXPR_WIDE_CHARACTER_CONSTANT:
2124 case EXPR_STRING_LITERAL:
2125 case EXPR_WIDE_STRING_LITERAL:
2126 case EXPR_COMPOUND_LITERAL: // TODO init?
2128 case EXPR_CLASSIFY_TYPE:
2131 case EXPR_BUILTIN_CONSTANT_P:
2132 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
2134 case EXPR_STATEMENT: // TODO
2135 case EXPR_LABEL_ADDRESS:
2136 case EXPR_REFERENCE_ENUM_VALUE:
2140 panic("unhandled expression");
2143 static designator_t *parse_designation(void)
2145 designator_t *result = NULL;
2146 designator_t *last = NULL;
2149 designator_t *designator;
2150 switch (token.type) {
2152 designator = allocate_ast_zero(sizeof(designator[0]));
2153 designator->source_position = token.source_position;
2155 add_anchor_token(']');
2156 designator->array_index = parse_constant_expression();
2157 rem_anchor_token(']');
2158 expect(']', end_error);
2161 designator = allocate_ast_zero(sizeof(designator[0]));
2162 designator->source_position = token.source_position;
2164 if (token.type != T_IDENTIFIER) {
2165 parse_error_expected("while parsing designator",
2166 T_IDENTIFIER, NULL);
2169 designator->symbol = token.v.symbol;
2173 expect('=', end_error);
2177 assert(designator != NULL);
2179 last->next = designator;
2181 result = designator;
2189 static initializer_t *initializer_from_string(array_type_t *type,
2190 const string_t *const string)
2192 /* TODO: check len vs. size of array type */
2195 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
2196 initializer->string.string = *string;
2201 static initializer_t *initializer_from_wide_string(array_type_t *const type,
2202 wide_string_t *const string)
2204 /* TODO: check len vs. size of array type */
2207 initializer_t *const initializer =
2208 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
2209 initializer->wide_string.string = *string;
2215 * Build an initializer from a given expression.
2217 static initializer_t *initializer_from_expression(type_t *orig_type,
2218 expression_t *expression)
2220 /* TODO check that expression is a constant expression */
2222 /* §6.7.8.14/15 char array may be initialized by string literals */
2223 type_t *type = skip_typeref(orig_type);
2224 type_t *expr_type_orig = expression->base.type;
2225 type_t *expr_type = skip_typeref(expr_type_orig);
2226 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
2227 array_type_t *const array_type = &type->array;
2228 type_t *const element_type = skip_typeref(array_type->element_type);
2230 if (element_type->kind == TYPE_ATOMIC) {
2231 atomic_type_kind_t akind = element_type->atomic.akind;
2232 switch (expression->kind) {
2233 case EXPR_STRING_LITERAL:
2234 if (akind == ATOMIC_TYPE_CHAR
2235 || akind == ATOMIC_TYPE_SCHAR
2236 || akind == ATOMIC_TYPE_UCHAR) {
2237 return initializer_from_string(array_type,
2238 &expression->string.value);
2242 case EXPR_WIDE_STRING_LITERAL: {
2243 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
2244 if (get_unqualified_type(element_type) == bare_wchar_type) {
2245 return initializer_from_wide_string(array_type,
2246 &expression->wide_string.value);
2257 assign_error_t error = semantic_assign(type, expression);
2258 if (error == ASSIGN_ERROR_INCOMPATIBLE)
2260 report_assign_error(error, type, expression, "initializer",
2261 &expression->base.source_position);
2263 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
2265 if (type->kind == TYPE_BITFIELD) {
2266 type = type->bitfield.base_type;
2269 result->value.value = create_implicit_cast(expression, type);
2275 * Checks if a given expression can be used as an constant initializer.
2277 static bool is_initializer_constant(const expression_t *expression)
2279 return is_constant_expression(expression)
2280 || is_address_constant(expression);
2284 * Parses an scalar initializer.
2286 * §6.7.8.11; eat {} without warning
2288 static initializer_t *parse_scalar_initializer(type_t *type,
2289 bool must_be_constant)
2291 /* there might be extra {} hierarchies */
2293 if (token.type == '{') {
2295 warningf(HERE, "extra curly braces around scalar initializer");
2299 } while (token.type == '{');
2302 expression_t *expression = parse_assignment_expression();
2303 mark_vars_read(expression, NULL);
2304 if (must_be_constant && !is_initializer_constant(expression)) {
2305 errorf(&expression->base.source_position,
2306 "Initialisation expression '%E' is not constant",
2310 initializer_t *initializer = initializer_from_expression(type, expression);
2312 if (initializer == NULL) {
2313 errorf(&expression->base.source_position,
2314 "expression '%E' (type '%T') doesn't match expected type '%T'",
2315 expression, expression->base.type, type);
2320 bool additional_warning_displayed = false;
2321 while (braces > 0) {
2322 if (token.type == ',') {
2325 if (token.type != '}') {
2326 if (!additional_warning_displayed && warning.other) {
2327 warningf(HERE, "additional elements in scalar initializer");
2328 additional_warning_displayed = true;
2339 * An entry in the type path.
2341 typedef struct type_path_entry_t type_path_entry_t;
2342 struct type_path_entry_t {
2343 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2345 size_t index; /**< For array types: the current index. */
2346 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2351 * A type path expression a position inside compound or array types.
2353 typedef struct type_path_t type_path_t;
2354 struct type_path_t {
2355 type_path_entry_t *path; /**< An flexible array containing the current path. */
2356 type_t *top_type; /**< type of the element the path points */
2357 size_t max_index; /**< largest index in outermost array */
2361 * Prints a type path for debugging.
2363 static __attribute__((unused)) void debug_print_type_path(
2364 const type_path_t *path)
2366 size_t len = ARR_LEN(path->path);
2368 for (size_t i = 0; i < len; ++i) {
2369 const type_path_entry_t *entry = & path->path[i];
2371 type_t *type = skip_typeref(entry->type);
2372 if (is_type_compound(type)) {
2373 /* in gcc mode structs can have no members */
2374 if (entry->v.compound_entry == NULL) {
2378 fprintf(stderr, ".%s",
2379 entry->v.compound_entry->base.symbol->string);
2380 } else if (is_type_array(type)) {
2381 fprintf(stderr, "[%u]", (unsigned) entry->v.index);
2383 fprintf(stderr, "-INVALID-");
2386 if (path->top_type != NULL) {
2387 fprintf(stderr, " (");
2388 print_type(path->top_type);
2389 fprintf(stderr, ")");
2394 * Return the top type path entry, ie. in a path
2395 * (type).a.b returns the b.
2397 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2399 size_t len = ARR_LEN(path->path);
2401 return &path->path[len-1];
2405 * Enlarge the type path by an (empty) element.
2407 static type_path_entry_t *append_to_type_path(type_path_t *path)
2409 size_t len = ARR_LEN(path->path);
2410 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2412 type_path_entry_t *result = & path->path[len];
2413 memset(result, 0, sizeof(result[0]));
2418 * Descending into a sub-type. Enter the scope of the current top_type.
2420 static void descend_into_subtype(type_path_t *path)
2422 type_t *orig_top_type = path->top_type;
2423 type_t *top_type = skip_typeref(orig_top_type);
2425 type_path_entry_t *top = append_to_type_path(path);
2426 top->type = top_type;
2428 if (is_type_compound(top_type)) {
2429 compound_t *compound = top_type->compound.compound;
2430 entity_t *entry = compound->members.entities;
2432 if (entry != NULL) {
2433 assert(entry->kind == ENTITY_COMPOUND_MEMBER);
2434 top->v.compound_entry = &entry->declaration;
2435 path->top_type = entry->declaration.type;
2437 path->top_type = NULL;
2439 } else if (is_type_array(top_type)) {
2441 path->top_type = top_type->array.element_type;
2443 assert(!is_type_valid(top_type));
2448 * Pop an entry from the given type path, ie. returning from
2449 * (type).a.b to (type).a
2451 static void ascend_from_subtype(type_path_t *path)
2453 type_path_entry_t *top = get_type_path_top(path);
2455 path->top_type = top->type;
2457 size_t len = ARR_LEN(path->path);
2458 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2462 * Pop entries from the given type path until the given
2463 * path level is reached.
2465 static void ascend_to(type_path_t *path, size_t top_path_level)
2467 size_t len = ARR_LEN(path->path);
2469 while (len > top_path_level) {
2470 ascend_from_subtype(path);
2471 len = ARR_LEN(path->path);
2475 static bool walk_designator(type_path_t *path, const designator_t *designator,
2476 bool used_in_offsetof)
2478 for (; designator != NULL; designator = designator->next) {
2479 type_path_entry_t *top = get_type_path_top(path);
2480 type_t *orig_type = top->type;
2482 type_t *type = skip_typeref(orig_type);
2484 if (designator->symbol != NULL) {
2485 symbol_t *symbol = designator->symbol;
2486 if (!is_type_compound(type)) {
2487 if (is_type_valid(type)) {
2488 errorf(&designator->source_position,
2489 "'.%Y' designator used for non-compound type '%T'",
2493 top->type = type_error_type;
2494 top->v.compound_entry = NULL;
2495 orig_type = type_error_type;
2497 compound_t *compound = type->compound.compound;
2498 entity_t *iter = compound->members.entities;
2499 for (; iter != NULL; iter = iter->base.next) {
2500 if (iter->base.symbol == symbol) {
2505 errorf(&designator->source_position,
2506 "'%T' has no member named '%Y'", orig_type, symbol);
2509 assert(iter->kind == ENTITY_COMPOUND_MEMBER);
2510 if (used_in_offsetof) {
2511 type_t *real_type = skip_typeref(iter->declaration.type);
2512 if (real_type->kind == TYPE_BITFIELD) {
2513 errorf(&designator->source_position,
2514 "offsetof designator '%Y' may not specify bitfield",
2520 top->type = orig_type;
2521 top->v.compound_entry = &iter->declaration;
2522 orig_type = iter->declaration.type;
2525 expression_t *array_index = designator->array_index;
2526 assert(designator->array_index != NULL);
2528 if (!is_type_array(type)) {
2529 if (is_type_valid(type)) {
2530 errorf(&designator->source_position,
2531 "[%E] designator used for non-array type '%T'",
2532 array_index, orig_type);
2537 long index = fold_constant(array_index);
2538 if (!used_in_offsetof) {
2540 errorf(&designator->source_position,
2541 "array index [%E] must be positive", array_index);
2542 } else if (type->array.size_constant) {
2543 long array_size = type->array.size;
2544 if (index >= array_size) {
2545 errorf(&designator->source_position,
2546 "designator [%E] (%d) exceeds array size %d",
2547 array_index, index, array_size);
2552 top->type = orig_type;
2553 top->v.index = (size_t) index;
2554 orig_type = type->array.element_type;
2556 path->top_type = orig_type;
2558 if (designator->next != NULL) {
2559 descend_into_subtype(path);
2568 static void advance_current_object(type_path_t *path, size_t top_path_level)
2570 type_path_entry_t *top = get_type_path_top(path);
2572 type_t *type = skip_typeref(top->type);
2573 if (is_type_union(type)) {
2574 /* in unions only the first element is initialized */
2575 top->v.compound_entry = NULL;
2576 } else if (is_type_struct(type)) {
2577 declaration_t *entry = top->v.compound_entry;
2579 entity_t *next_entity = entry->base.next;
2580 if (next_entity != NULL) {
2581 assert(is_declaration(next_entity));
2582 entry = &next_entity->declaration;
2587 top->v.compound_entry = entry;
2588 if (entry != NULL) {
2589 path->top_type = entry->type;
2592 } else if (is_type_array(type)) {
2593 assert(is_type_array(type));
2597 if (!type->array.size_constant || top->v.index < type->array.size) {
2601 assert(!is_type_valid(type));
2605 /* we're past the last member of the current sub-aggregate, try if we
2606 * can ascend in the type hierarchy and continue with another subobject */
2607 size_t len = ARR_LEN(path->path);
2609 if (len > top_path_level) {
2610 ascend_from_subtype(path);
2611 advance_current_object(path, top_path_level);
2613 path->top_type = NULL;
2618 * skip until token is found.
2620 static void skip_until(int type)
2622 while (token.type != type) {
2623 if (token.type == T_EOF)
2630 * skip any {...} blocks until a closing bracket is reached.
2632 static void skip_initializers(void)
2634 if (token.type == '{')
2637 while (token.type != '}') {
2638 if (token.type == T_EOF)
2640 if (token.type == '{') {
2648 static initializer_t *create_empty_initializer(void)
2650 static initializer_t empty_initializer
2651 = { .list = { { INITIALIZER_LIST }, 0 } };
2652 return &empty_initializer;
2656 * Parse a part of an initialiser for a struct or union,
2658 static initializer_t *parse_sub_initializer(type_path_t *path,
2659 type_t *outer_type, size_t top_path_level,
2660 parse_initializer_env_t *env)
2662 if (token.type == '}') {
2663 /* empty initializer */
2664 return create_empty_initializer();
2667 type_t *orig_type = path->top_type;
2668 type_t *type = NULL;
2670 if (orig_type == NULL) {
2671 /* We are initializing an empty compound. */
2673 type = skip_typeref(orig_type);
2676 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2679 designator_t *designator = NULL;
2680 if (token.type == '.' || token.type == '[') {
2681 designator = parse_designation();
2682 goto finish_designator;
2683 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2684 /* GNU-style designator ("identifier: value") */
2685 designator = allocate_ast_zero(sizeof(designator[0]));
2686 designator->source_position = token.source_position;
2687 designator->symbol = token.v.symbol;
2692 /* reset path to toplevel, evaluate designator from there */
2693 ascend_to(path, top_path_level);
2694 if (!walk_designator(path, designator, false)) {
2695 /* can't continue after designation error */
2699 initializer_t *designator_initializer
2700 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2701 designator_initializer->designator.designator = designator;
2702 ARR_APP1(initializer_t*, initializers, designator_initializer);
2704 orig_type = path->top_type;
2705 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2710 if (token.type == '{') {
2711 if (type != NULL && is_type_scalar(type)) {
2712 sub = parse_scalar_initializer(type, env->must_be_constant);
2716 if (env->entity != NULL) {
2718 "extra brace group at end of initializer for '%Y'",
2719 env->entity->base.symbol);
2721 errorf(HERE, "extra brace group at end of initializer");
2724 descend_into_subtype(path);
2726 add_anchor_token('}');
2727 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2729 rem_anchor_token('}');
2732 ascend_from_subtype(path);
2733 expect('}', end_error);
2735 expect('}', end_error);
2736 goto error_parse_next;
2740 /* must be an expression */
2741 expression_t *expression = parse_assignment_expression();
2742 mark_vars_read(expression, NULL);
2744 if (env->must_be_constant && !is_initializer_constant(expression)) {
2745 errorf(&expression->base.source_position,
2746 "Initialisation expression '%E' is not constant",
2751 /* we are already outside, ... */
2752 type_t *const outer_type_skip = skip_typeref(outer_type);
2753 if (is_type_compound(outer_type_skip) &&
2754 !outer_type_skip->compound.compound->complete) {
2755 goto error_parse_next;
2760 /* handle { "string" } special case */
2761 if ((expression->kind == EXPR_STRING_LITERAL
2762 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2763 && outer_type != NULL) {
2764 sub = initializer_from_expression(outer_type, expression);
2766 if (token.type == ',') {
2769 if (token.type != '}' && warning.other) {
2770 warningf(HERE, "excessive elements in initializer for type '%T'",
2773 /* TODO: eat , ... */
2778 /* descend into subtypes until expression matches type */
2780 orig_type = path->top_type;
2781 type = skip_typeref(orig_type);
2783 sub = initializer_from_expression(orig_type, expression);
2787 if (!is_type_valid(type)) {
2790 if (is_type_scalar(type)) {
2791 errorf(&expression->base.source_position,
2792 "expression '%E' doesn't match expected type '%T'",
2793 expression, orig_type);
2797 descend_into_subtype(path);
2801 /* update largest index of top array */
2802 const type_path_entry_t *first = &path->path[0];
2803 type_t *first_type = first->type;
2804 first_type = skip_typeref(first_type);
2805 if (is_type_array(first_type)) {
2806 size_t index = first->v.index;
2807 if (index > path->max_index)
2808 path->max_index = index;
2812 /* append to initializers list */
2813 ARR_APP1(initializer_t*, initializers, sub);
2816 if (warning.other) {
2817 if (env->entity != NULL) {
2818 warningf(HERE, "excess elements in struct initializer for '%Y'",
2819 env->entity->base.symbol);
2821 warningf(HERE, "excess elements in struct initializer");
2827 if (token.type == '}') {
2830 expect(',', end_error);
2831 if (token.type == '}') {
2836 /* advance to the next declaration if we are not at the end */
2837 advance_current_object(path, top_path_level);
2838 orig_type = path->top_type;
2839 if (orig_type != NULL)
2840 type = skip_typeref(orig_type);
2846 size_t len = ARR_LEN(initializers);
2847 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2848 initializer_t *result = allocate_ast_zero(size);
2849 result->kind = INITIALIZER_LIST;
2850 result->list.len = len;
2851 memcpy(&result->list.initializers, initializers,
2852 len * sizeof(initializers[0]));
2854 DEL_ARR_F(initializers);
2855 ascend_to(path, top_path_level+1);
2860 skip_initializers();
2861 DEL_ARR_F(initializers);
2862 ascend_to(path, top_path_level+1);
2867 * Parses an initializer. Parsers either a compound literal
2868 * (env->declaration == NULL) or an initializer of a declaration.
2870 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2872 type_t *type = skip_typeref(env->type);
2873 size_t max_index = 0xdeadbeaf; // TODO: Resolve this uninitialized variable problem
2874 initializer_t *result;
2876 if (is_type_scalar(type)) {
2877 result = parse_scalar_initializer(type, env->must_be_constant);
2878 } else if (token.type == '{') {
2882 memset(&path, 0, sizeof(path));
2883 path.top_type = env->type;
2884 path.path = NEW_ARR_F(type_path_entry_t, 0);
2886 descend_into_subtype(&path);
2888 add_anchor_token('}');
2889 result = parse_sub_initializer(&path, env->type, 1, env);
2890 rem_anchor_token('}');
2892 max_index = path.max_index;
2893 DEL_ARR_F(path.path);
2895 expect('}', end_error);
2897 /* parse_scalar_initializer() also works in this case: we simply
2898 * have an expression without {} around it */
2899 result = parse_scalar_initializer(type, env->must_be_constant);
2902 /* §6.7.8:22 array initializers for arrays with unknown size determine
2903 * the array type size */
2904 if (is_type_array(type) && type->array.size_expression == NULL
2905 && result != NULL) {
2907 switch (result->kind) {
2908 case INITIALIZER_LIST:
2909 assert(max_index != 0xdeadbeaf);
2910 size = max_index + 1;
2913 case INITIALIZER_STRING:
2914 size = result->string.string.size;
2917 case INITIALIZER_WIDE_STRING:
2918 size = result->wide_string.string.size;
2921 case INITIALIZER_DESIGNATOR:
2922 case INITIALIZER_VALUE:
2923 /* can happen for parse errors */
2928 internal_errorf(HERE, "invalid initializer type");
2931 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2932 cnst->base.type = type_size_t;
2933 cnst->conste.v.int_value = size;
2935 type_t *new_type = duplicate_type(type);
2937 new_type->array.size_expression = cnst;
2938 new_type->array.size_constant = true;
2939 new_type->array.has_implicit_size = true;
2940 new_type->array.size = size;
2941 env->type = new_type;
2949 static void append_entity(scope_t *scope, entity_t *entity)
2951 if (scope->last_entity != NULL) {
2952 scope->last_entity->base.next = entity;
2954 scope->entities = entity;
2956 scope->last_entity = entity;
2960 static compound_t *parse_compound_type_specifier(bool is_struct)
2962 gnu_attribute_t *attributes = NULL;
2963 decl_modifiers_t modifiers = 0;
2970 symbol_t *symbol = NULL;
2971 compound_t *compound = NULL;
2973 if (token.type == T___attribute__) {
2974 modifiers |= parse_attributes(&attributes);
2977 if (token.type == T_IDENTIFIER) {
2978 symbol = token.v.symbol;
2981 namespace_tag_t const namespc =
2982 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2983 entity_t *entity = get_entity(symbol, namespc);
2984 if (entity != NULL) {
2985 assert(entity->kind == (is_struct ? ENTITY_STRUCT : ENTITY_UNION));
2986 compound = &entity->compound;
2987 if (compound->base.parent_scope != current_scope &&
2988 (token.type == '{' || token.type == ';')) {
2989 /* we're in an inner scope and have a definition. Shadow
2990 * existing definition in outer scope */
2992 } else if (compound->complete && token.type == '{') {
2993 assert(symbol != NULL);
2994 errorf(HERE, "multiple definitions of '%s %Y' (previous definition %P)",
2995 is_struct ? "struct" : "union", symbol,
2996 &compound->base.source_position);
2997 /* clear members in the hope to avoid further errors */
2998 compound->members.entities = NULL;
3001 } else if (token.type != '{') {
3003 parse_error_expected("while parsing struct type specifier",
3004 T_IDENTIFIER, '{', NULL);
3006 parse_error_expected("while parsing union type specifier",
3007 T_IDENTIFIER, '{', NULL);
3013 if (compound == NULL) {
3014 entity_kind_t kind = is_struct ? ENTITY_STRUCT : ENTITY_UNION;
3015 entity_t *entity = allocate_entity_zero(kind);
3016 compound = &entity->compound;
3018 compound->base.namespc =
3019 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
3020 compound->base.source_position = token.source_position;
3021 compound->base.symbol = symbol;
3022 compound->base.parent_scope = current_scope;
3023 if (symbol != NULL) {
3024 environment_push(entity);
3026 append_entity(current_scope, entity);
3029 if (token.type == '{') {
3030 parse_compound_type_entries(compound);
3031 modifiers |= parse_attributes(&attributes);
3033 if (symbol == NULL) {
3034 assert(anonymous_entity == NULL);
3035 anonymous_entity = (entity_t*)compound;
3039 compound->modifiers |= modifiers;
3043 static void parse_enum_entries(type_t *const enum_type)
3047 if (token.type == '}') {
3048 errorf(HERE, "empty enum not allowed");
3053 add_anchor_token('}');
3055 if (token.type != T_IDENTIFIER) {
3056 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
3058 rem_anchor_token('}');
3062 entity_t *entity = allocate_entity_zero(ENTITY_ENUM_VALUE);
3063 entity->enum_value.enum_type = enum_type;
3064 entity->base.symbol = token.v.symbol;
3065 entity->base.source_position = token.source_position;
3068 if (token.type == '=') {
3070 expression_t *value = parse_constant_expression();
3072 value = create_implicit_cast(value, enum_type);
3073 entity->enum_value.value = value;
3078 record_entity(entity, false);
3080 if (token.type != ',')
3083 } while (token.type != '}');
3084 rem_anchor_token('}');
3086 expect('}', end_error);
3092 static type_t *parse_enum_specifier(void)
3094 gnu_attribute_t *attributes = NULL;
3099 if (token.type == T_IDENTIFIER) {
3100 symbol = token.v.symbol;
3103 entity = get_entity(symbol, NAMESPACE_ENUM);
3104 if (entity != NULL) {
3105 assert(entity->kind == ENTITY_ENUM);
3106 if (entity->base.parent_scope != current_scope &&
3107 (token.type == '{' || token.type == ';')) {
3108 /* we're in an inner scope and have a definition. Shadow
3109 * existing definition in outer scope */
3111 } else if (entity->enume.complete && token.type == '{') {
3112 errorf(HERE, "multiple definitions of 'enum %Y' (previous definition %P)",
3113 symbol, &entity->base.source_position);
3116 } else if (token.type != '{') {
3117 parse_error_expected("while parsing enum type specifier",
3118 T_IDENTIFIER, '{', NULL);
3125 if (entity == NULL) {
3126 entity = allocate_entity_zero(ENTITY_ENUM);
3127 entity->base.namespc = NAMESPACE_ENUM;
3128 entity->base.source_position = token.source_position;
3129 entity->base.symbol = symbol;
3130 entity->base.parent_scope = current_scope;
3133 type_t *const type = allocate_type_zero(TYPE_ENUM);
3134 type->enumt.enume = &entity->enume;
3135 type->enumt.akind = ATOMIC_TYPE_INT;
3137 if (token.type == '{') {
3138 if (symbol != NULL) {
3139 environment_push(entity);
3141 append_entity(current_scope, entity);
3142 entity->enume.complete = true;
3144 parse_enum_entries(type);
3145 parse_attributes(&attributes);
3147 if (symbol == NULL) {
3148 assert(anonymous_entity == NULL);
3149 anonymous_entity = entity;
3151 } else if (!entity->enume.complete && !(c_mode & _GNUC)) {
3152 errorf(HERE, "'enum %Y' used before definition (incomplete enums are a GNU extension)",
3160 * if a symbol is a typedef to another type, return true
3162 static bool is_typedef_symbol(symbol_t *symbol)
3164 const entity_t *const entity = get_entity(symbol, NAMESPACE_NORMAL);
3165 return entity != NULL && entity->kind == ENTITY_TYPEDEF;
3168 static type_t *parse_typeof(void)
3174 expect('(', end_error);
3175 add_anchor_token(')');
3177 expression_t *expression = NULL;
3179 bool old_type_prop = in_type_prop;
3180 bool old_gcc_extension = in_gcc_extension;
3181 in_type_prop = true;
3183 while (token.type == T___extension__) {
3184 /* This can be a prefix to a typename or an expression. */
3186 in_gcc_extension = true;
3188 switch (token.type) {
3190 if (is_typedef_symbol(token.v.symbol)) {
3191 type = parse_typename();
3193 expression = parse_expression();
3194 type = revert_automatic_type_conversion(expression);
3199 type = parse_typename();
3203 expression = parse_expression();
3204 type = expression->base.type;
3207 in_type_prop = old_type_prop;
3208 in_gcc_extension = old_gcc_extension;
3210 rem_anchor_token(')');
3211 expect(')', end_error);
3213 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
3214 typeof_type->typeoft.expression = expression;
3215 typeof_type->typeoft.typeof_type = type;
3222 typedef enum specifiers_t {
3223 SPECIFIER_SIGNED = 1 << 0,
3224 SPECIFIER_UNSIGNED = 1 << 1,
3225 SPECIFIER_LONG = 1 << 2,
3226 SPECIFIER_INT = 1 << 3,
3227 SPECIFIER_DOUBLE = 1 << 4,
3228 SPECIFIER_CHAR = 1 << 5,
3229 SPECIFIER_WCHAR_T = 1 << 6,
3230 SPECIFIER_SHORT = 1 << 7,
3231 SPECIFIER_LONG_LONG = 1 << 8,
3232 SPECIFIER_FLOAT = 1 << 9,
3233 SPECIFIER_BOOL = 1 << 10,
3234 SPECIFIER_VOID = 1 << 11,
3235 SPECIFIER_INT8 = 1 << 12,
3236 SPECIFIER_INT16 = 1 << 13,
3237 SPECIFIER_INT32 = 1 << 14,
3238 SPECIFIER_INT64 = 1 << 15,
3239 SPECIFIER_INT128 = 1 << 16,
3240 SPECIFIER_COMPLEX = 1 << 17,
3241 SPECIFIER_IMAGINARY = 1 << 18,
3244 static type_t *create_builtin_type(symbol_t *const symbol,
3245 type_t *const real_type)
3247 type_t *type = allocate_type_zero(TYPE_BUILTIN);
3248 type->builtin.symbol = symbol;
3249 type->builtin.real_type = real_type;
3250 return identify_new_type(type);
3253 static type_t *get_typedef_type(symbol_t *symbol)
3255 entity_t *entity = get_entity(symbol, NAMESPACE_NORMAL);
3256 if (entity == NULL || entity->kind != ENTITY_TYPEDEF)
3259 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
3260 type->typedeft.typedefe = &entity->typedefe;
3266 * check for the allowed MS alignment values.
3268 static bool check_alignment_value(long long intvalue)
3270 if (intvalue < 1 || intvalue > 8192) {
3271 errorf(HERE, "illegal alignment value");
3274 unsigned v = (unsigned)intvalue;
3275 for (unsigned i = 1; i <= 8192; i += i) {
3279 errorf(HERE, "alignment must be power of two");
3283 #define DET_MOD(name, tag) do { \
3284 if (*modifiers & tag && warning.other) warningf(HERE, #name " used more than once"); \
3285 *modifiers |= tag; \
3288 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
3290 decl_modifiers_t *modifiers = &specifiers->modifiers;
3293 if (token.type == T_restrict) {
3295 DET_MOD(restrict, DM_RESTRICT);
3297 } else if (token.type != T_IDENTIFIER)
3299 symbol_t *symbol = token.v.symbol;
3300 if (symbol == sym_align) {
3302 expect('(', end_error);
3303 if (token.type != T_INTEGER)
3305 if (check_alignment_value(token.v.intvalue)) {
3306 if (specifiers->alignment != 0 && warning.other)
3307 warningf(HERE, "align used more than once");
3308 specifiers->alignment = (unsigned char)token.v.intvalue;
3311 expect(')', end_error);
3312 } else if (symbol == sym_allocate) {
3314 expect('(', end_error);
3315 if (token.type != T_IDENTIFIER)
3317 (void)token.v.symbol;
3318 expect(')', end_error);
3319 } else if (symbol == sym_dllimport) {
3321 DET_MOD(dllimport, DM_DLLIMPORT);
3322 } else if (symbol == sym_dllexport) {
3324 DET_MOD(dllexport, DM_DLLEXPORT);
3325 } else if (symbol == sym_thread) {
3327 DET_MOD(thread, DM_THREAD);
3328 } else if (symbol == sym_naked) {
3330 DET_MOD(naked, DM_NAKED);
3331 } else if (symbol == sym_noinline) {
3333 DET_MOD(noinline, DM_NOINLINE);
3334 } else if (symbol == sym_returns_twice) {
3336 DET_MOD(returns_twice, DM_RETURNS_TWICE);
3337 } else if (symbol == sym_noreturn) {
3339 DET_MOD(noreturn, DM_NORETURN);
3340 } else if (symbol == sym_nothrow) {
3342 DET_MOD(nothrow, DM_NOTHROW);
3343 } else if (symbol == sym_novtable) {
3345 DET_MOD(novtable, DM_NOVTABLE);
3346 } else if (symbol == sym_property) {
3348 expect('(', end_error);
3350 bool is_get = false;
3351 if (token.type != T_IDENTIFIER)
3353 if (token.v.symbol == sym_get) {
3355 } else if (token.v.symbol == sym_put) {
3357 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
3361 expect('=', end_error);
3362 if (token.type != T_IDENTIFIER)
3365 if (specifiers->get_property_sym != NULL) {
3366 errorf(HERE, "get property name already specified");
3368 specifiers->get_property_sym = token.v.symbol;
3371 if (specifiers->put_property_sym != NULL) {
3372 errorf(HERE, "put property name already specified");
3374 specifiers->put_property_sym = token.v.symbol;
3378 if (token.type == ',') {
3384 expect(')', end_error);
3385 } else if (symbol == sym_selectany) {
3387 DET_MOD(selectany, DM_SELECTANY);
3388 } else if (symbol == sym_uuid) {
3390 expect('(', end_error);
3391 if (token.type != T_STRING_LITERAL)
3394 expect(')', end_error);
3395 } else if (symbol == sym_deprecated) {
3397 if (specifiers->deprecated != 0 && warning.other)
3398 warningf(HERE, "deprecated used more than once");
3399 specifiers->deprecated = true;
3400 if (token.type == '(') {
3402 if (token.type == T_STRING_LITERAL) {
3403 specifiers->deprecated_string = token.v.string.begin;
3406 errorf(HERE, "string literal expected");
3408 expect(')', end_error);
3410 } else if (symbol == sym_noalias) {
3412 DET_MOD(noalias, DM_NOALIAS);
3415 warningf(HERE, "Unknown modifier '%Y' ignored", token.v.symbol);
3417 if (token.type == '(')
3421 if (token.type == ',')
3428 static entity_t *create_error_entity(symbol_t *symbol, entity_kind_tag_t kind)
3430 entity_t *entity = allocate_entity_zero(kind);
3431 entity->base.source_position = *HERE;
3432 entity->base.symbol = symbol;
3433 if (is_declaration(entity)) {
3434 entity->declaration.type = type_error_type;
3435 entity->declaration.implicit = true;
3436 } else if (kind == ENTITY_TYPEDEF) {
3437 entity->typedefe.type = type_error_type;
3438 entity->typedefe.builtin = true;
3440 if (kind != ENTITY_COMPOUND_MEMBER)
3441 record_entity(entity, false);
3445 static void parse_microsoft_based(based_spec_t *based_spec)
3447 if (token.type != T_IDENTIFIER) {
3448 parse_error_expected("while parsing __based", T_IDENTIFIER, NULL);
3451 symbol_t *symbol = token.v.symbol;
3452 entity_t *entity = get_entity(symbol, NAMESPACE_NORMAL);
3454 if (entity == NULL || entity->base.kind != ENTITY_VARIABLE) {
3455 errorf(HERE, "'%Y' is not a variable name.", symbol);
3456 entity = create_error_entity(symbol, ENTITY_VARIABLE);
3458 variable_t *variable = &entity->variable;
3460 if (based_spec->base_variable != NULL) {
3461 errorf(HERE, "__based type qualifier specified more than once");
3463 based_spec->source_position = token.source_position;
3464 based_spec->base_variable = variable;
3466 type_t *const type = variable->base.type;
3468 if (is_type_valid(type)) {
3469 if (! is_type_pointer(skip_typeref(type))) {
3470 errorf(HERE, "variable in __based modifier must have pointer type instead of '%T'", type);
3472 if (variable->base.base.parent_scope != file_scope) {
3473 errorf(HERE, "a nonstatic local variable may not be used in a __based specification");
3481 * Finish the construction of a struct type by calculating
3482 * its size, offsets, alignment.
3484 static void finish_struct_type(compound_type_t *type)
3486 assert(type->compound != NULL);
3488 compound_t *compound = type->compound;
3489 if (!compound->complete)
3494 il_alignment_t alignment = 1;
3495 bool need_pad = false;
3497 entity_t *entry = compound->members.entities;
3498 for (; entry != NULL; entry = entry->base.next) {
3499 if (entry->kind != ENTITY_COMPOUND_MEMBER)
3502 type_t *m_type = skip_typeref(entry->declaration.type);
3503 if (! is_type_valid(m_type)) {
3504 /* simply ignore errors here */
3507 il_alignment_t m_alignment = m_type->base.alignment;
3508 if (m_alignment > alignment)
3509 alignment = m_alignment;
3511 offset = (size + m_alignment - 1) & -m_alignment;
3515 entry->compound_member.offset = offset;
3516 size = offset + m_type->base.size;
3518 if (type->base.alignment != 0) {
3519 alignment = type->base.alignment;
3522 offset = (size + alignment - 1) & -alignment;
3527 if (warning.padded) {
3528 warningf(&compound->base.source_position, "'%T' needs padding", type);
3531 if (compound->modifiers & DM_PACKED && warning.packed) {
3532 warningf(&compound->base.source_position,
3533 "superfluous packed attribute on '%T'", type);
3537 type->base.size = offset;
3538 type->base.alignment = alignment;
3542 * Finish the construction of an union type by calculating
3543 * its size and alignment.
3545 static void finish_union_type(compound_type_t *type)
3547 assert(type->compound != NULL);
3549 compound_t *compound = type->compound;
3550 if (! compound->complete)
3554 il_alignment_t alignment = 1;
3556 entity_t *entry = compound->members.entities;
3557 for (; entry != NULL; entry = entry->base.next) {
3558 if (entry->kind != ENTITY_COMPOUND_MEMBER)
3561 type_t *m_type = skip_typeref(entry->declaration.type);
3562 if (! is_type_valid(m_type))
3565 entry->compound_member.offset = 0;
3566 if (m_type->base.size > size)
3567 size = m_type->base.size;
3568 if (m_type->base.alignment > alignment)
3569 alignment = m_type->base.alignment;
3571 if (type->base.alignment != 0) {
3572 alignment = type->base.alignment;
3574 size = (size + alignment - 1) & -alignment;
3575 type->base.size = size;
3576 type->base.alignment = alignment;
3579 static type_t *handle_attribute_mode(const gnu_attribute_t *attribute,
3582 type_t *type = skip_typeref(orig_type);
3584 /* at least: byte, word, pointer, list of machine modes
3585 * __XXX___ is interpreted as XXX */
3587 /* This isn't really correct, the backend should provide a list of machine
3588 * specific modes (according to gcc philosophy that is...) */
3589 const char *symbol_str = attribute->u.symbol->string;
3590 bool sign = is_type_signed(type);
3591 atomic_type_kind_t akind;
3592 if (strcmp_underscore("QI", symbol_str) == 0 ||
3593 strcmp_underscore("byte", symbol_str) == 0) {
3594 akind = sign ? ATOMIC_TYPE_CHAR : ATOMIC_TYPE_UCHAR;
3595 } else if (strcmp_underscore("HI", symbol_str) == 0) {
3596 akind = sign ? ATOMIC_TYPE_SHORT : ATOMIC_TYPE_USHORT;
3597 } else if (strcmp_underscore("SI", symbol_str) == 0
3598 || strcmp_underscore("word", symbol_str) == 0
3599 || strcmp_underscore("pointer", symbol_str) == 0) {
3600 akind = sign ? ATOMIC_TYPE_INT : ATOMIC_TYPE_UINT;
3601 } else if (strcmp_underscore("DI", symbol_str) == 0) {
3602 akind = sign ? ATOMIC_TYPE_LONGLONG : ATOMIC_TYPE_ULONGLONG;
3605 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
3609 if (type->kind == TYPE_ATOMIC) {
3610 type_t *copy = duplicate_type(type);
3611 copy->atomic.akind = akind;
3612 return identify_new_type(copy);
3613 } else if (type->kind == TYPE_ENUM) {
3614 type_t *copy = duplicate_type(type);
3615 copy->enumt.akind = akind;
3616 return identify_new_type(copy);
3617 } else if (is_type_pointer(type)) {
3618 warningf(HERE, "__attribute__((mode)) on pointers not implemented yet (ignored)");
3622 errorf(HERE, "__attribute__((mode)) only allowed on integer, enum or pointer type");
3626 static type_t *handle_type_attributes(const gnu_attribute_t *attributes,
3629 const gnu_attribute_t *attribute = attributes;
3630 for ( ; attribute != NULL; attribute = attribute->next) {
3631 if (attribute->invalid)
3634 if (attribute->kind == GNU_AK_MODE) {
3635 type = handle_attribute_mode(attribute, type);
3636 } else if (attribute->kind == GNU_AK_ALIGNED) {
3637 int alignment = 32; /* TODO: fill in maximum useful alignment for
3639 if (attribute->has_arguments)
3640 alignment = attribute->u.argument;
3642 type_t *copy = duplicate_type(type);
3643 copy->base.alignment = attribute->u.argument;
3644 type = identify_new_type(copy);
3651 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3653 type_t *type = NULL;
3654 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3655 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3656 unsigned type_specifiers = 0;
3657 bool newtype = false;
3658 bool saw_error = false;
3659 bool old_gcc_extension = in_gcc_extension;
3661 specifiers->source_position = token.source_position;
3664 specifiers->modifiers
3665 |= parse_attributes(&specifiers->gnu_attributes);
3667 switch (token.type) {
3669 #define MATCH_STORAGE_CLASS(token, class) \
3671 if (specifiers->storage_class != STORAGE_CLASS_NONE) { \
3672 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3674 specifiers->storage_class = class; \
3675 if (specifiers->thread_local) \
3676 goto check_thread_storage_class; \
3680 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3681 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3682 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3683 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3684 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3688 expect('(', end_error);
3689 add_anchor_token(')');
3690 parse_microsoft_extended_decl_modifier(specifiers);
3691 rem_anchor_token(')');
3692 expect(')', end_error);
3696 if (specifiers->thread_local) {
3697 errorf(HERE, "duplicate '__thread'");
3699 specifiers->thread_local = true;
3700 check_thread_storage_class:
3701 switch (specifiers->storage_class) {
3702 case STORAGE_CLASS_EXTERN:
3703 case STORAGE_CLASS_NONE:
3704 case STORAGE_CLASS_STATIC:
3708 case STORAGE_CLASS_AUTO: wrong = "auto"; goto wrong_thread_stoarge_class;
3709 case STORAGE_CLASS_REGISTER: wrong = "register"; goto wrong_thread_stoarge_class;
3710 case STORAGE_CLASS_TYPEDEF: wrong = "typedef"; goto wrong_thread_stoarge_class;
3711 wrong_thread_stoarge_class:
3712 errorf(HERE, "'__thread' used with '%s'", wrong);
3719 /* type qualifiers */
3720 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3722 qualifiers |= qualifier; \
3726 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3727 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3728 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3729 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3730 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3731 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3732 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3733 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3735 case T___extension__:
3737 in_gcc_extension = true;
3740 /* type specifiers */
3741 #define MATCH_SPECIFIER(token, specifier, name) \
3743 if (type_specifiers & specifier) { \
3744 errorf(HERE, "multiple " name " type specifiers given"); \
3746 type_specifiers |= specifier; \
3751 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3752 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3753 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3754 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3755 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3756 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3757 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3758 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3759 MATCH_SPECIFIER(T_bool, SPECIFIER_BOOL, "bool");
3760 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3761 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3762 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3763 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3764 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3765 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3766 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3767 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3768 MATCH_SPECIFIER(T_wchar_t, SPECIFIER_WCHAR_T, "wchar_t");
3770 case T__forceinline:
3771 /* only in microsoft mode */
3772 specifiers->modifiers |= DM_FORCEINLINE;
3777 specifiers->is_inline = true;
3781 if (type_specifiers & SPECIFIER_LONG_LONG) {
3782 errorf(HERE, "multiple type specifiers given");
3783 } else if (type_specifiers & SPECIFIER_LONG) {
3784 type_specifiers |= SPECIFIER_LONG_LONG;
3786 type_specifiers |= SPECIFIER_LONG;
3792 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
3794 type->compound.compound = parse_compound_type_specifier(true);
3795 finish_struct_type(&type->compound);
3799 type = allocate_type_zero(TYPE_COMPOUND_UNION);
3800 type->compound.compound = parse_compound_type_specifier(false);
3801 if (type->compound.compound->modifiers & DM_TRANSPARENT_UNION)
3802 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3803 finish_union_type(&type->compound);
3807 type = parse_enum_specifier();
3810 type = parse_typeof();
3812 case T___builtin_va_list:
3813 type = duplicate_type(type_valist);
3817 case T_IDENTIFIER: {
3818 /* only parse identifier if we haven't found a type yet */
3819 if (type != NULL || type_specifiers != 0) {
3820 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3821 * declaration, so it doesn't generate errors about expecting '(' or
3823 switch (look_ahead(1)->type) {
3830 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3834 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3839 goto finish_specifiers;
3843 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3844 if (typedef_type == NULL) {
3845 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3846 * declaration, so it doesn't generate 'implicit int' followed by more
3847 * errors later on. */
3848 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3854 errorf(HERE, "%K does not name a type", &token);
3857 create_error_entity(token.v.symbol, ENTITY_TYPEDEF);
3859 type = allocate_type_zero(TYPE_TYPEDEF);
3860 type->typedeft.typedefe = &entity->typedefe;
3864 if (la1_type == '&' || la1_type == '*')
3865 goto finish_specifiers;
3870 goto finish_specifiers;
3875 type = typedef_type;
3879 /* function specifier */
3881 goto finish_specifiers;
3886 specifiers->modifiers
3887 |= parse_attributes(&specifiers->gnu_attributes);
3889 in_gcc_extension = old_gcc_extension;
3891 if (type == NULL || (saw_error && type_specifiers != 0)) {
3892 atomic_type_kind_t atomic_type;
3894 /* match valid basic types */
3895 switch (type_specifiers) {
3896 case SPECIFIER_VOID:
3897 atomic_type = ATOMIC_TYPE_VOID;
3899 case SPECIFIER_WCHAR_T:
3900 atomic_type = ATOMIC_TYPE_WCHAR_T;
3902 case SPECIFIER_CHAR:
3903 atomic_type = ATOMIC_TYPE_CHAR;
3905 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3906 atomic_type = ATOMIC_TYPE_SCHAR;
3908 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3909 atomic_type = ATOMIC_TYPE_UCHAR;
3911 case SPECIFIER_SHORT:
3912 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3913 case SPECIFIER_SHORT | SPECIFIER_INT:
3914 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3915 atomic_type = ATOMIC_TYPE_SHORT;
3917 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3918 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3919 atomic_type = ATOMIC_TYPE_USHORT;
3922 case SPECIFIER_SIGNED:
3923 case SPECIFIER_SIGNED | SPECIFIER_INT:
3924 atomic_type = ATOMIC_TYPE_INT;
3926 case SPECIFIER_UNSIGNED:
3927 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3928 atomic_type = ATOMIC_TYPE_UINT;
3930 case SPECIFIER_LONG:
3931 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3932 case SPECIFIER_LONG | SPECIFIER_INT:
3933 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3934 atomic_type = ATOMIC_TYPE_LONG;
3936 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3937 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3938 atomic_type = ATOMIC_TYPE_ULONG;
3941 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3942 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3943 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3944 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3946 atomic_type = ATOMIC_TYPE_LONGLONG;
3947 goto warn_about_long_long;
3949 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3950 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3952 atomic_type = ATOMIC_TYPE_ULONGLONG;
3953 warn_about_long_long:
3954 if (warning.long_long) {
3955 warningf(&specifiers->source_position,
3956 "ISO C90 does not support 'long long'");
3960 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3961 atomic_type = unsigned_int8_type_kind;
3964 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3965 atomic_type = unsigned_int16_type_kind;
3968 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3969 atomic_type = unsigned_int32_type_kind;
3972 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3973 atomic_type = unsigned_int64_type_kind;
3976 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3977 atomic_type = unsigned_int128_type_kind;
3980 case SPECIFIER_INT8:
3981 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3982 atomic_type = int8_type_kind;
3985 case SPECIFIER_INT16:
3986 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3987 atomic_type = int16_type_kind;
3990 case SPECIFIER_INT32:
3991 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3992 atomic_type = int32_type_kind;
3995 case SPECIFIER_INT64:
3996 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3997 atomic_type = int64_type_kind;
4000 case SPECIFIER_INT128:
4001 case SPECIFIER_SIGNED | SPECIFIER_INT128:
4002 atomic_type = int128_type_kind;
4005 case SPECIFIER_FLOAT:
4006 atomic_type = ATOMIC_TYPE_FLOAT;
4008 case SPECIFIER_DOUBLE:
4009 atomic_type = ATOMIC_TYPE_DOUBLE;
4011 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
4012 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
4014 case SPECIFIER_BOOL:
4015 atomic_type = ATOMIC_TYPE_BOOL;
4017 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
4018 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
4019 atomic_type = ATOMIC_TYPE_FLOAT;
4021 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
4022 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
4023 atomic_type = ATOMIC_TYPE_DOUBLE;
4025 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
4026 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
4027 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
4030 /* invalid specifier combination, give an error message */
4031 if (type_specifiers == 0) {
4035 /* ISO/IEC 14882:1998(E) §C.1.5:4 */
4036 if (!(c_mode & _CXX) && !strict_mode) {
4037 if (warning.implicit_int) {
4038 warningf(HERE, "no type specifiers in declaration, using 'int'");
4040 atomic_type = ATOMIC_TYPE_INT;
4043 errorf(HERE, "no type specifiers given in declaration");
4045 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
4046 (type_specifiers & SPECIFIER_UNSIGNED)) {
4047 errorf(HERE, "signed and unsigned specifiers given");
4048 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
4049 errorf(HERE, "only integer types can be signed or unsigned");
4051 errorf(HERE, "multiple datatypes in declaration");
4056 if (type_specifiers & SPECIFIER_COMPLEX) {
4057 type = allocate_type_zero(TYPE_COMPLEX);
4058 type->complex.akind = atomic_type;
4059 } else if (type_specifiers & SPECIFIER_IMAGINARY) {
4060 type = allocate_type_zero(TYPE_IMAGINARY);
4061 type->imaginary.akind = atomic_type;
4063 type = allocate_type_zero(TYPE_ATOMIC);
4064 type->atomic.akind = atomic_type;
4066 type->base.alignment = get_atomic_type_alignment(atomic_type);
4067 unsigned const size = get_atomic_type_size(atomic_type);
4069 type_specifiers & SPECIFIER_COMPLEX ? size * 2 : size;
4071 } else if (type_specifiers != 0) {
4072 errorf(HERE, "multiple datatypes in declaration");
4075 /* FIXME: check type qualifiers here */
4077 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
4078 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4079 type->base.qualifiers = qualifiers;
4080 type->base.modifiers = modifiers;
4083 type = identify_new_type(type);
4085 type = typehash_insert(type);
4088 type = handle_type_attributes(specifiers->gnu_attributes, type);
4089 specifiers->type = type;
4093 specifiers->type = type_error_type;
4097 static type_qualifiers_t parse_type_qualifiers(void)
4099 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
4102 switch (token.type) {
4103 /* type qualifiers */
4104 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
4105 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
4106 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
4107 /* microsoft extended type modifiers */
4108 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
4109 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
4110 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
4111 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
4112 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
4121 * Parses an K&R identifier list
4123 static void parse_identifier_list(scope_t *scope)
4126 entity_t *entity = allocate_entity_zero(ENTITY_PARAMETER);
4127 entity->base.source_position = token.source_position;
4128 entity->base.namespc = NAMESPACE_NORMAL;
4129 entity->base.symbol = token.v.symbol;
4130 /* a K&R parameter has no type, yet */
4134 append_entity(scope, entity);
4136 if (token.type != ',') {
4140 } while (token.type == T_IDENTIFIER);
4143 static entity_t *parse_parameter(void)
4145 declaration_specifiers_t specifiers;
4146 memset(&specifiers, 0, sizeof(specifiers));
4148 parse_declaration_specifiers(&specifiers);
4150 entity_t *entity = parse_declarator(&specifiers,
4151 DECL_MAY_BE_ABSTRACT | DECL_IS_PARAMETER);
4152 anonymous_entity = NULL;
4156 static void semantic_parameter_incomplete(const entity_t *entity)
4158 assert(entity->kind == ENTITY_PARAMETER);
4160 /* §6.7.5.3:4 After adjustment, the parameters in a parameter type
4161 * list in a function declarator that is part of a
4162 * definition of that function shall not have
4163 * incomplete type. */
4164 type_t *type = skip_typeref(entity->declaration.type);
4165 if (is_type_incomplete(type)) {
4166 errorf(&entity->base.source_position,
4167 "parameter '%Y' has incomplete type '%T'", entity->base.symbol,
4168 entity->declaration.type);
4173 * Parses function type parameters (and optionally creates variable_t entities
4174 * for them in a scope)
4176 static void parse_parameters(function_type_t *type, scope_t *scope)
4179 add_anchor_token(')');
4180 int saved_comma_state = save_and_reset_anchor_state(',');
4182 if (token.type == T_IDENTIFIER &&
4183 !is_typedef_symbol(token.v.symbol)) {
4184 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
4185 if (la1_type == ',' || la1_type == ')') {
4186 type->kr_style_parameters = true;
4187 type->unspecified_parameters = true;
4188 parse_identifier_list(scope);
4189 goto parameters_finished;
4193 if (token.type == ')') {
4194 /* ISO/IEC 14882:1998(E) §C.1.6:1 */
4195 if (!(c_mode & _CXX))
4196 type->unspecified_parameters = true;
4197 goto parameters_finished;
4200 function_parameter_t *parameter;
4201 function_parameter_t *last_parameter = NULL;
4204 switch (token.type) {
4207 type->variadic = true;
4208 goto parameters_finished;
4211 case T___extension__:
4214 entity_t *entity = parse_parameter();
4215 if (entity->kind == ENTITY_TYPEDEF) {
4216 errorf(&entity->base.source_position,
4217 "typedef not allowed as function parameter");
4220 assert(is_declaration(entity));
4222 /* func(void) is not a parameter */
4223 if (last_parameter == NULL
4224 && token.type == ')'
4225 && entity->base.symbol == NULL
4226 && skip_typeref(entity->declaration.type) == type_void) {
4227 goto parameters_finished;
4229 semantic_parameter_incomplete(entity);
4231 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
4232 memset(parameter, 0, sizeof(parameter[0]));
4233 parameter->type = entity->declaration.type;
4235 if (scope != NULL) {
4236 append_entity(scope, entity);
4239 if (last_parameter != NULL) {
4240 last_parameter->next = parameter;
4242 type->parameters = parameter;
4244 last_parameter = parameter;
4249 goto parameters_finished;
4251 if (token.type != ',') {
4252 goto parameters_finished;
4258 parameters_finished:
4259 rem_anchor_token(')');
4260 expect(')', end_error);
4263 restore_anchor_state(',', saved_comma_state);
4266 typedef enum construct_type_kind_t {
4269 CONSTRUCT_REFERENCE,
4272 } construct_type_kind_t;
4274 typedef union construct_type_t construct_type_t;
4276 typedef struct construct_type_base_t {
4277 construct_type_kind_t kind;
4278 construct_type_t *next;
4279 } construct_type_base_t;
4281 typedef struct parsed_pointer_t {
4282 construct_type_base_t base;
4283 type_qualifiers_t type_qualifiers;
4284 variable_t *base_variable; /**< MS __based extension. */
4287 typedef struct parsed_reference_t {
4288 construct_type_base_t base;
4289 } parsed_reference_t;
4291 typedef struct construct_function_type_t {
4292 construct_type_base_t base;
4293 type_t *function_type;
4294 } construct_function_type_t;
4296 typedef struct parsed_array_t {
4297 construct_type_base_t base;
4298 type_qualifiers_t type_qualifiers;
4304 union construct_type_t {
4305 construct_type_kind_t kind;
4306 construct_type_base_t base;
4307 parsed_pointer_t pointer;
4308 parsed_reference_t reference;
4309 construct_function_type_t function;
4310 parsed_array_t array;
4313 static construct_type_t *parse_pointer_declarator(variable_t *base_variable)
4317 construct_type_t *cons = obstack_alloc(&temp_obst, sizeof(cons->pointer));
4318 parsed_pointer_t *pointer = &cons->pointer;
4319 memset(pointer, 0, sizeof(*pointer));
4320 cons->kind = CONSTRUCT_POINTER;
4321 pointer->type_qualifiers = parse_type_qualifiers();
4322 pointer->base_variable = base_variable;
4327 static construct_type_t *parse_reference_declarator(void)
4331 construct_type_t *cons = obstack_alloc(&temp_obst, sizeof(cons->reference));
4332 parsed_reference_t *reference = &cons->reference;
4333 memset(reference, 0, sizeof(*reference));
4334 cons->kind = CONSTRUCT_REFERENCE;
4339 static construct_type_t *parse_array_declarator(void)
4342 add_anchor_token(']');
4344 construct_type_t *cons = obstack_alloc(&temp_obst, sizeof(cons->array));
4345 parsed_array_t *array = &cons->array;
4346 memset(array, 0, sizeof(*array));
4347 cons->kind = CONSTRUCT_ARRAY;
4349 if (token.type == T_static) {
4350 array->is_static = true;
4354 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
4355 if (type_qualifiers != 0) {
4356 if (token.type == T_static) {
4357 array->is_static = true;
4361 array->type_qualifiers = type_qualifiers;
4363 if (token.type == '*' && look_ahead(1)->type == ']') {
4364 array->is_variable = true;
4366 } else if (token.type != ']') {
4367 expression_t *const size = parse_assignment_expression();
4369 mark_vars_read(size, NULL);
4372 rem_anchor_token(']');
4373 expect(']', end_error);
4379 static construct_type_t *parse_function_declarator(scope_t *scope,
4380 decl_modifiers_t modifiers)
4382 type_t *type = allocate_type_zero(TYPE_FUNCTION);
4383 function_type_t *ftype = &type->function;
4385 ftype->linkage = current_linkage;
4387 switch (modifiers & (DM_CDECL | DM_STDCALL | DM_FASTCALL | DM_THISCALL)) {
4388 case DM_NONE: break;
4389 case DM_CDECL: ftype->calling_convention = CC_CDECL; break;
4390 case DM_STDCALL: ftype->calling_convention = CC_STDCALL; break;
4391 case DM_FASTCALL: ftype->calling_convention = CC_FASTCALL; break;
4392 case DM_THISCALL: ftype->calling_convention = CC_THISCALL; break;
4395 errorf(HERE, "multiple calling conventions in declaration");
4399 parse_parameters(ftype, scope);
4401 construct_type_t *cons = obstack_alloc(&temp_obst, sizeof(cons->function));
4402 construct_function_type_t *function = &cons->function;
4403 memset(function, 0, sizeof(*function));
4404 cons->kind = CONSTRUCT_FUNCTION;
4405 function->function_type = type;
4410 typedef struct parse_declarator_env_t {
4411 decl_modifiers_t modifiers;
4413 source_position_t source_position;
4415 } parse_declarator_env_t;
4417 static construct_type_t *parse_inner_declarator(parse_declarator_env_t *env,
4418 bool may_be_abstract)
4420 /* construct a single linked list of construct_type_t's which describe
4421 * how to construct the final declarator type */
4422 construct_type_t *first = NULL;
4423 construct_type_t **anchor = &first;
4424 gnu_attribute_t *attributes = NULL;
4426 decl_modifiers_t modifiers = parse_attributes(&attributes);
4428 /* MS __based extension */
4429 based_spec_t base_spec;
4430 base_spec.base_variable = NULL;
4433 construct_type_t *type;
4434 switch (token.type) {
4436 if (!(c_mode & _CXX))
4437 errorf(HERE, "references are only available for C++");
4438 if (base_spec.base_variable != NULL && warning.other) {
4439 warningf(&base_spec.source_position,
4440 "__based does not precede a pointer operator, ignored");
4442 type = parse_reference_declarator();
4444 base_spec.base_variable = NULL;
4448 type = parse_pointer_declarator(base_spec.base_variable);
4450 base_spec.base_variable = NULL;
4455 expect('(', end_error);
4456 add_anchor_token(')');
4457 parse_microsoft_based(&base_spec);
4458 rem_anchor_token(')');
4459 expect(')', end_error);
4463 goto ptr_operator_end;
4467 anchor = &type->base.next;
4469 /* TODO: find out if this is correct */
4470 modifiers |= parse_attributes(&attributes);
4473 if (base_spec.base_variable != NULL && warning.other) {
4474 warningf(&base_spec.source_position,
4475 "__based does not precede a pointer operator, ignored");
4479 modifiers |= env->modifiers;
4480 env->modifiers = modifiers;
4483 construct_type_t *inner_types = NULL;
4485 switch (token.type) {
4488 errorf(HERE, "no identifier expected in typename");
4490 env->symbol = token.v.symbol;
4491 env->source_position = token.source_position;
4496 /* §6.7.6:2 footnote 126: Empty parentheses in a type name are
4497 * interpreted as ``function with no parameter specification'', rather
4498 * than redundant parentheses around the omitted identifier. */
4499 if (look_ahead(1)->type != ')') {
4501 add_anchor_token(')');
4502 inner_types = parse_inner_declarator(env, may_be_abstract);
4503 if (inner_types != NULL) {
4504 /* All later declarators only modify the return type */
4507 rem_anchor_token(')');
4508 expect(')', end_error);
4512 if (may_be_abstract)
4514 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
4519 construct_type_t **const p = anchor;
4522 construct_type_t *type;
4523 switch (token.type) {
4525 scope_t *scope = NULL;
4527 scope = &env->parameters;
4529 type = parse_function_declarator(scope, modifiers);
4533 type = parse_array_declarator();
4536 goto declarator_finished;
4539 /* insert in the middle of the list (at p) */
4540 type->base.next = *p;
4543 anchor = &type->base.next;
4546 declarator_finished:
4547 /* append inner_types at the end of the list, we don't to set anchor anymore
4548 * as it's not needed anymore */
4549 *anchor = inner_types;
4556 static void parse_declaration_attributes(entity_t *entity)
4558 gnu_attribute_t *attributes = NULL;
4559 decl_modifiers_t modifiers = parse_attributes(&attributes);
4565 if (entity->kind == ENTITY_TYPEDEF) {
4566 modifiers |= entity->typedefe.modifiers;
4567 type = entity->typedefe.type;
4569 assert(is_declaration(entity));
4570 modifiers |= entity->declaration.modifiers;
4571 type = entity->declaration.type;
4576 gnu_attribute_t *attribute = attributes;
4577 for ( ; attribute != NULL; attribute = attribute->next) {
4578 if (attribute->invalid)
4581 if (attribute->kind == GNU_AK_MODE) {
4582 type = handle_attribute_mode(attribute, type);
4583 } else if (attribute->kind == GNU_AK_ALIGNED) {
4584 int alignment = 32; /* TODO: fill in maximum usefull alignment for target machine */
4585 if (attribute->has_arguments)
4586 alignment = attribute->u.argument;
4588 if (entity->kind == ENTITY_TYPEDEF) {
4589 type_t *copy = duplicate_type(type);
4590 copy->base.alignment = attribute->u.argument;
4591 type = identify_new_type(copy);
4592 } else if(entity->kind == ENTITY_VARIABLE) {
4593 entity->variable.alignment = alignment;
4594 } else if(entity->kind == ENTITY_COMPOUND_MEMBER) {
4595 entity->compound_member.alignment = alignment;
4600 type_modifiers_t type_modifiers = type->base.modifiers;
4601 if (modifiers & DM_TRANSPARENT_UNION)
4602 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
4604 if (type->base.modifiers != type_modifiers) {
4605 type_t *copy = duplicate_type(type);
4606 copy->base.modifiers = type_modifiers;
4607 type = identify_new_type(copy);
4610 if (entity->kind == ENTITY_TYPEDEF) {
4611 entity->typedefe.type = type;
4612 entity->typedefe.modifiers = modifiers;
4614 entity->declaration.type = type;
4615 entity->declaration.modifiers = modifiers;
4619 static type_t *construct_declarator_type(construct_type_t *construct_list, type_t *type)
4621 construct_type_t *iter = construct_list;
4622 for (; iter != NULL; iter = iter->base.next) {
4623 switch (iter->kind) {
4624 case CONSTRUCT_INVALID:
4626 case CONSTRUCT_FUNCTION: {
4627 construct_function_type_t *function = &iter->function;
4628 type_t *function_type = function->function_type;
4630 function_type->function.return_type = type;
4632 type_t *skipped_return_type = skip_typeref(type);
4634 if (is_type_function(skipped_return_type)) {
4635 errorf(HERE, "function returning function is not allowed");
4636 } else if (is_type_array(skipped_return_type)) {
4637 errorf(HERE, "function returning array is not allowed");
4639 if (skipped_return_type->base.qualifiers != 0 && warning.other) {
4641 "type qualifiers in return type of function type are meaningless");
4645 /* The function type was constructed earlier. Freeing it here will
4646 * destroy other types. */
4647 type = typehash_insert(function_type);
4651 case CONSTRUCT_POINTER: {
4652 if (is_type_reference(skip_typeref(type)))
4653 errorf(HERE, "cannot declare a pointer to reference");
4655 parsed_pointer_t *pointer = &iter->pointer;
4656 type = make_based_pointer_type(type, pointer->type_qualifiers, pointer->base_variable);
4660 case CONSTRUCT_REFERENCE:
4661 if (is_type_reference(skip_typeref(type)))
4662 errorf(HERE, "cannot declare a reference to reference");
4664 type = make_reference_type(type);
4667 case CONSTRUCT_ARRAY: {
4668 if (is_type_reference(skip_typeref(type)))
4669 errorf(HERE, "cannot declare an array of references");
4671 parsed_array_t *array = &iter->array;
4672 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
4674 expression_t *size_expression = array->size;
4675 if (size_expression != NULL) {
4677 = create_implicit_cast(size_expression, type_size_t);
4680 array_type->base.qualifiers = array->type_qualifiers;
4681 array_type->array.element_type = type;
4682 array_type->array.is_static = array->is_static;
4683 array_type->array.is_variable = array->is_variable;
4684 array_type->array.size_expression = size_expression;
4686 if (size_expression != NULL) {
4687 if (is_constant_expression(size_expression)) {
4688 array_type->array.size_constant = true;
4689 array_type->array.size
4690 = fold_constant(size_expression);
4692 array_type->array.is_vla = true;
4696 type_t *skipped_type = skip_typeref(type);
4698 if (is_type_incomplete(skipped_type)) {
4699 errorf(HERE, "array of incomplete type '%T' is not allowed", type);
4700 } else if (is_type_function(skipped_type)) {
4701 errorf(HERE, "array of functions is not allowed");
4703 type = identify_new_type(array_type);
4707 internal_errorf(HERE, "invalid type construction found");
4713 static type_t *automatic_type_conversion(type_t *orig_type);
4715 static type_t *semantic_parameter(const source_position_t *pos,
4717 const declaration_specifiers_t *specifiers,
4720 /* §6.7.5.3:7 A declaration of a parameter as ``array of type''
4721 * shall be adjusted to ``qualified pointer to type'',
4723 * §6.7.5.3:8 A declaration of a parameter as ``function returning
4724 * type'' shall be adjusted to ``pointer to function
4725 * returning type'', as in 6.3.2.1. */
4726 type = automatic_type_conversion(type);
4728 if (specifiers->is_inline && is_type_valid(type)) {
4729 errorf(pos, "parameter '%Y' declared 'inline'", symbol);
4732 /* §6.9.1:6 The declarations in the declaration list shall contain
4733 * no storage-class specifier other than register and no
4734 * initializations. */
4735 if (specifiers->thread_local || (
4736 specifiers->storage_class != STORAGE_CLASS_NONE &&
4737 specifiers->storage_class != STORAGE_CLASS_REGISTER)
4739 errorf(pos, "invalid storage class for parameter '%Y'", symbol);
4742 /* delay test for incomplete type, because we might have (void)
4743 * which is legal but incomplete... */
4748 static entity_t *parse_declarator(const declaration_specifiers_t *specifiers,
4749 declarator_flags_t flags)
4751 parse_declarator_env_t env;
4752 memset(&env, 0, sizeof(env));
4753 env.modifiers = specifiers->modifiers;
4755 construct_type_t *construct_type =
4756 parse_inner_declarator(&env, (flags & DECL_MAY_BE_ABSTRACT) != 0);
4758 construct_declarator_type(construct_type, specifiers->type);
4759 type_t *type = skip_typeref(orig_type);
4761 if (construct_type != NULL) {
4762 obstack_free(&temp_obst, construct_type);
4766 if (specifiers->storage_class == STORAGE_CLASS_TYPEDEF) {
4767 entity = allocate_entity_zero(ENTITY_TYPEDEF);
4768 entity->base.symbol = env.symbol;
4769 entity->base.source_position = env.source_position;
4770 entity->typedefe.type = orig_type;
4772 if (anonymous_entity != NULL) {
4773 if (is_type_compound(type)) {
4774 assert(anonymous_entity->compound.alias == NULL);
4775 assert(anonymous_entity->kind == ENTITY_STRUCT ||
4776 anonymous_entity->kind == ENTITY_UNION);
4777 anonymous_entity->compound.alias = entity;
4778 anonymous_entity = NULL;
4779 } else if (is_type_enum(type)) {
4780 assert(anonymous_entity->enume.alias == NULL);
4781 assert(anonymous_entity->kind == ENTITY_ENUM);
4782 anonymous_entity->enume.alias = entity;
4783 anonymous_entity = NULL;
4787 /* create a declaration type entity */
4788 if (flags & DECL_CREATE_COMPOUND_MEMBER) {
4789 entity = allocate_entity_zero(ENTITY_COMPOUND_MEMBER);
4791 if (env.symbol != NULL) {
4792 if (specifiers->is_inline && is_type_valid(type)) {
4793 errorf(&env.source_position,
4794 "compound member '%Y' declared 'inline'", env.symbol);
4797 if (specifiers->thread_local ||
4798 specifiers->storage_class != STORAGE_CLASS_NONE) {
4799 errorf(&env.source_position,
4800 "compound member '%Y' must have no storage class",
4804 } else if (flags & DECL_IS_PARAMETER) {
4805 orig_type = semantic_parameter(&env.source_position, orig_type,
4806 specifiers, env.symbol);
4808 entity = allocate_entity_zero(ENTITY_PARAMETER);
4809 } else if (is_type_function(type)) {
4810 entity = allocate_entity_zero(ENTITY_FUNCTION);
4812 entity->function.is_inline = specifiers->is_inline;
4813 entity->function.parameters = env.parameters;
4815 if (env.symbol != NULL) {
4816 if (specifiers->thread_local || (
4817 specifiers->storage_class != STORAGE_CLASS_EXTERN &&
4818 specifiers->storage_class != STORAGE_CLASS_NONE &&
4819 specifiers->storage_class != STORAGE_CLASS_STATIC
4821 errorf(&env.source_position,
4822 "invalid storage class for function '%Y'", env.symbol);
4826 entity = allocate_entity_zero(ENTITY_VARIABLE);
4828 entity->variable.get_property_sym = specifiers->get_property_sym;
4829 entity->variable.put_property_sym = specifiers->put_property_sym;
4831 entity->variable.thread_local = specifiers->thread_local;
4833 if (env.symbol != NULL) {
4834 if (specifiers->is_inline && is_type_valid(type)) {
4835 errorf(&env.source_position,
4836 "variable '%Y' declared 'inline'", env.symbol);
4839 bool invalid_storage_class = false;
4840 if (current_scope == file_scope) {
4841 if (specifiers->storage_class != STORAGE_CLASS_EXTERN &&
4842 specifiers->storage_class != STORAGE_CLASS_NONE &&
4843 specifiers->storage_class != STORAGE_CLASS_STATIC) {
4844 invalid_storage_class = true;
4847 if (specifiers->thread_local &&
4848 specifiers->storage_class == STORAGE_CLASS_NONE) {
4849 invalid_storage_class = true;
4852 if (invalid_storage_class) {
4853 errorf(&env.source_position,
4854 "invalid storage class for variable '%Y'", env.symbol);
4859 if (env.symbol != NULL) {
4860 entity->base.symbol = env.symbol;
4861 entity->base.source_position = env.source_position;
4863 entity->base.source_position = specifiers->source_position;
4865 entity->base.namespc = NAMESPACE_NORMAL;
4866 entity->declaration.type = orig_type;
4867 entity->declaration.modifiers = env.modifiers;
4868 entity->declaration.deprecated_string = specifiers->deprecated_string;
4870 storage_class_t storage_class = specifiers->storage_class;
4871 entity->declaration.declared_storage_class = storage_class;
4873 if (storage_class == STORAGE_CLASS_NONE && current_scope != file_scope)
4874 storage_class = STORAGE_CLASS_AUTO;
4875 entity->declaration.storage_class = storage_class;
4878 parse_declaration_attributes(entity);
4883 static type_t *parse_abstract_declarator(type_t *base_type)
4885 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4887 type_t *result = construct_declarator_type(construct_type, base_type);
4888 if (construct_type != NULL) {
4889 obstack_free(&temp_obst, construct_type);
4896 * Check if the declaration of main is suspicious. main should be a
4897 * function with external linkage, returning int, taking either zero
4898 * arguments, two, or three arguments of appropriate types, ie.
4900 * int main([ int argc, char **argv [, char **env ] ]).
4902 * @param decl the declaration to check
4903 * @param type the function type of the declaration
4905 static void check_type_of_main(const entity_t *entity)
4907 const source_position_t *pos = &entity->base.source_position;
4908 if (entity->kind != ENTITY_FUNCTION) {
4909 warningf(pos, "'main' is not a function");
4913 if (entity->declaration.storage_class == STORAGE_CLASS_STATIC) {
4914 warningf(pos, "'main' is normally a non-static function");
4917 type_t *type = skip_typeref(entity->declaration.type);
4918 assert(is_type_function(type));
4920 function_type_t *func_type = &type->function;
4921 if (!types_compatible(skip_typeref(func_type->return_type), type_int)) {
4922 warningf(pos, "return type of 'main' should be 'int', but is '%T'",
4923 func_type->return_type);
4925 const function_parameter_t *parm = func_type->parameters;
4927 type_t *const first_type = parm->type;
4928 if (!types_compatible(skip_typeref(first_type), type_int)) {
4930 "first argument of 'main' should be 'int', but is '%T'",
4935 type_t *const second_type = parm->type;
4936 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4937 warningf(pos, "second argument of 'main' should be 'char**', but is '%T'", second_type);
4941 type_t *const third_type = parm->type;
4942 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4943 warningf(pos, "third argument of 'main' should be 'char**', but is '%T'", third_type);
4947 goto warn_arg_count;
4951 warningf(pos, "'main' takes only zero, two or three arguments");
4957 * Check if a symbol is the equal to "main".
4959 static bool is_sym_main(const symbol_t *const sym)
4961 return strcmp(sym->string, "main") == 0;
4964 static void error_redefined_as_different_kind(const source_position_t *pos,
4965 const entity_t *old, entity_kind_t new_kind)
4967 errorf(pos, "redeclaration of %s '%Y' as %s (declared %P)",
4968 get_entity_kind_name(old->kind), old->base.symbol,
4969 get_entity_kind_name(new_kind), &old->base.source_position);
4972 static bool is_error_entity(entity_t *const ent)
4974 if (is_declaration(ent)) {
4975 return is_type_valid(skip_typeref(ent->declaration.type));
4976 } else if (ent->kind == ENTITY_TYPEDEF) {
4977 return is_type_valid(skip_typeref(ent->typedefe.type));
4983 * record entities for the NAMESPACE_NORMAL, and produce error messages/warnings
4984 * for various problems that occur for multiple definitions
4986 static entity_t *record_entity(entity_t *entity, const bool is_definition)
4988 const symbol_t *const symbol = entity->base.symbol;
4989 const namespace_tag_t namespc = (namespace_tag_t)entity->base.namespc;
4990 const source_position_t *pos = &entity->base.source_position;
4992 /* can happen in error cases */
4996 entity_t *const previous_entity = get_entity(symbol, namespc);
4997 /* pushing the same entity twice will break the stack structure */
4998 assert(previous_entity != entity);
5000 if (entity->kind == ENTITY_FUNCTION) {
5001 type_t *const orig_type = entity->declaration.type;
5002 type_t *const type = skip_typeref(orig_type);
5004 assert(is_type_function(type));
5005 if (type->function.unspecified_parameters &&
5006 warning.strict_prototypes &&
5007 previous_entity == NULL) {
5008 warningf(pos, "function declaration '%#T' is not a prototype",
5012 if (warning.main && current_scope == file_scope
5013 && is_sym_main(symbol)) {
5014 check_type_of_main(entity);
5018 if (is_declaration(entity) &&
5019 warning.nested_externs &&
5020 entity->declaration.storage_class == STORAGE_CLASS_EXTERN &&
5021 current_scope != file_scope) {
5022 warningf(pos, "nested extern declaration of '%#T'",
5023 entity->declaration.type, symbol);
5026 if (previous_entity != NULL) {
5027 if (previous_entity->base.parent_scope == ¤t_function->parameters &&
5028 previous_entity->base.parent_scope->depth + 1 == current_scope->depth) {
5029 assert(previous_entity->kind == ENTITY_PARAMETER);
5031 "declaration '%#T' redeclares the parameter '%#T' (declared %P)",
5032 entity->declaration.type, symbol,
5033 previous_entity->declaration.type, symbol,
5034 &previous_entity->base.source_position);
5038 if (previous_entity->base.parent_scope == current_scope) {
5039 if (previous_entity->kind != entity->kind) {
5040 if (!is_error_entity(previous_entity) && !is_error_entity(entity)) {
5041 error_redefined_as_different_kind(pos, previous_entity,
5046 if (previous_entity->kind == ENTITY_ENUM_VALUE) {
5047 errorf(pos, "redeclaration of enum entry '%Y' (declared %P)",
5048 symbol, &previous_entity->base.source_position);
5051 if (previous_entity->kind == ENTITY_TYPEDEF) {
5052 /* TODO: C++ allows this for exactly the same type */
5053 errorf(pos, "redefinition of typedef '%Y' (declared %P)",
5054 symbol, &previous_entity->base.source_position);
5058 /* at this point we should have only VARIABLES or FUNCTIONS */
5059 assert(is_declaration(previous_entity) && is_declaration(entity));
5061 declaration_t *const prev_decl = &previous_entity->declaration;
5062 declaration_t *const decl = &entity->declaration;
5064 /* can happen for K&R style declarations */
5065 if (prev_decl->type == NULL &&
5066 previous_entity->kind == ENTITY_PARAMETER &&
5067 entity->kind == ENTITY_PARAMETER) {
5068 prev_decl->type = decl->type;
5069 prev_decl->storage_class = decl->storage_class;
5070 prev_decl->declared_storage_class = decl->declared_storage_class;
5071 prev_decl->modifiers = decl->modifiers;
5072 prev_decl->deprecated_string = decl->deprecated_string;
5073 return previous_entity;
5076 type_t *const orig_type = decl->type;
5077 assert(orig_type != NULL);
5078 type_t *const type = skip_typeref(orig_type);
5079 type_t *const prev_type = skip_typeref(prev_decl->type);
5081 if (!types_compatible(type, prev_type)) {
5083 "declaration '%#T' is incompatible with '%#T' (declared %P)",
5084 orig_type, symbol, prev_decl->type, symbol,
5085 &previous_entity->base.source_position);
5087 unsigned old_storage_class = prev_decl->storage_class;
5088 if (warning.redundant_decls &&
5091 !(prev_decl->modifiers & DM_USED) &&
5092 prev_decl->storage_class == STORAGE_CLASS_STATIC) {
5093 warningf(&previous_entity->base.source_position,
5094 "unnecessary static forward declaration for '%#T'",
5095 prev_decl->type, symbol);
5098 storage_class_t new_storage_class = decl->storage_class;
5100 /* pretend no storage class means extern for function
5101 * declarations (except if the previous declaration is neither
5102 * none nor extern) */
5103 if (entity->kind == ENTITY_FUNCTION) {
5104 /* the previous declaration could have unspecified parameters or
5105 * be a typedef, so use the new type */
5106 if (prev_type->function.unspecified_parameters || is_definition)
5107 prev_decl->type = type;
5109 switch (old_storage_class) {
5110 case STORAGE_CLASS_NONE:
5111 old_storage_class = STORAGE_CLASS_EXTERN;
5114 case STORAGE_CLASS_EXTERN:
5115 if (is_definition) {
5116 if (warning.missing_prototypes &&
5117 prev_type->function.unspecified_parameters &&
5118 !is_sym_main(symbol)) {
5119 warningf(pos, "no previous prototype for '%#T'",
5122 } else if (new_storage_class == STORAGE_CLASS_NONE) {
5123 new_storage_class = STORAGE_CLASS_EXTERN;
5130 } else if (is_type_incomplete(prev_type)) {
5131 prev_decl->type = type;
5134 if (old_storage_class == STORAGE_CLASS_EXTERN &&
5135 new_storage_class == STORAGE_CLASS_EXTERN) {
5136 warn_redundant_declaration:
5137 if (!is_definition &&
5138 warning.redundant_decls &&
5139 is_type_valid(prev_type) &&
5140 strcmp(previous_entity->base.source_position.input_name,
5141 "<builtin>") != 0) {
5143 "redundant declaration for '%Y' (declared %P)",
5144 symbol, &previous_entity->base.source_position);
5146 } else if (current_function == NULL) {
5147 if (old_storage_class != STORAGE_CLASS_STATIC &&
5148 new_storage_class == STORAGE_CLASS_STATIC) {
5150 "static declaration of '%Y' follows non-static declaration (declared %P)",
5151 symbol, &previous_entity->base.source_position);
5152 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
5153 prev_decl->storage_class = STORAGE_CLASS_NONE;
5154 prev_decl->declared_storage_class = STORAGE_CLASS_NONE;
5156 /* ISO/IEC 14882:1998(E) §C.1.2:1 */
5158 goto error_redeclaration;
5159 goto warn_redundant_declaration;
5161 } else if (is_type_valid(prev_type)) {
5162 if (old_storage_class == new_storage_class) {
5163 error_redeclaration:
5164 errorf(pos, "redeclaration of '%Y' (declared %P)",
5165 symbol, &previous_entity->base.source_position);
5168 "redeclaration of '%Y' with different linkage (declared %P)",
5169 symbol, &previous_entity->base.source_position);
5174 prev_decl->modifiers |= decl->modifiers;
5175 if (entity->kind == ENTITY_FUNCTION) {
5176 previous_entity->function.is_inline |= entity->function.is_inline;
5178 return previous_entity;
5181 if (warning.shadow) {
5182 warningf(pos, "%s '%Y' shadows %s (declared %P)",
5183 get_entity_kind_name(entity->kind), symbol,
5184 get_entity_kind_name(previous_entity->kind),
5185 &previous_entity->base.source_position);
5189 if (entity->kind == ENTITY_FUNCTION) {
5190 if (is_definition &&
5191 entity->declaration.storage_class != STORAGE_CLASS_STATIC) {
5192 if (warning.missing_prototypes && !is_sym_main(symbol)) {
5193 warningf(pos, "no previous prototype for '%#T'",
5194 entity->declaration.type, symbol);
5195 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
5196 warningf(pos, "no previous declaration for '%#T'",
5197 entity->declaration.type, symbol);
5200 } else if (warning.missing_declarations &&
5201 entity->kind == ENTITY_VARIABLE &&
5202 current_scope == file_scope) {
5203 declaration_t *declaration = &entity->declaration;
5204 if (declaration->storage_class == STORAGE_CLASS_NONE) {
5205 warningf(pos, "no previous declaration for '%#T'",
5206 declaration->type, symbol);
5211 assert(entity->base.parent_scope == NULL);
5212 assert(current_scope != NULL);
5214 entity->base.parent_scope = current_scope;
5215 entity->base.namespc = NAMESPACE_NORMAL;
5216 environment_push(entity);
5217 append_entity(current_scope, entity);
5222 static void parser_error_multiple_definition(entity_t *entity,
5223 const source_position_t *source_position)
5225 errorf(source_position, "multiple definition of '%Y' (declared %P)",
5226 entity->base.symbol, &entity->base.source_position);
5229 static bool is_declaration_specifier(const token_t *token,
5230 bool only_specifiers_qualifiers)
5232 switch (token->type) {
5237 return is_typedef_symbol(token->v.symbol);
5239 case T___extension__:
5241 return !only_specifiers_qualifiers;
5248 static void parse_init_declarator_rest(entity_t *entity)
5250 assert(is_declaration(entity));
5251 declaration_t *const declaration = &entity->declaration;
5255 type_t *orig_type = declaration->type;
5256 type_t *type = skip_typeref(orig_type);
5258 if (entity->kind == ENTITY_VARIABLE
5259 && entity->variable.initializer != NULL) {
5260 parser_error_multiple_definition(entity, HERE);
5263 bool must_be_constant = false;
5264 if (declaration->storage_class == STORAGE_CLASS_STATIC ||
5265 entity->base.parent_scope == file_scope) {
5266 must_be_constant = true;
5269 if (is_type_function(type)) {
5270 errorf(&entity->base.source_position,
5271 "function '%#T' is initialized like a variable",
5272 orig_type, entity->base.symbol);
5273 orig_type = type_error_type;
5276 parse_initializer_env_t env;
5277 env.type = orig_type;
5278 env.must_be_constant = must_be_constant;
5279 env.entity = entity;
5280 current_init_decl = entity;
5282 initializer_t *initializer = parse_initializer(&env);
5283 current_init_decl = NULL;
5285 if (entity->kind == ENTITY_VARIABLE) {
5286 /* §6.7.5:22 array initializers for arrays with unknown size
5287 * determine the array type size */
5288 declaration->type = env.type;
5289 entity->variable.initializer = initializer;
5293 /* parse rest of a declaration without any declarator */
5294 static void parse_anonymous_declaration_rest(
5295 const declaration_specifiers_t *specifiers)
5298 anonymous_entity = NULL;
5300 if (warning.other) {
5301 if (specifiers->storage_class != STORAGE_CLASS_NONE ||
5302 specifiers->thread_local) {
5303 warningf(&specifiers->source_position,
5304 "useless storage class in empty declaration");
5307 type_t *type = specifiers->type;
5308 switch (type->kind) {
5309 case TYPE_COMPOUND_STRUCT:
5310 case TYPE_COMPOUND_UNION: {
5311 if (type->compound.compound->base.symbol == NULL) {
5312 warningf(&specifiers->source_position,
5313 "unnamed struct/union that defines no instances");
5322 warningf(&specifiers->source_position, "empty declaration");
5328 static void check_variable_type_complete(entity_t *ent)
5330 if (ent->kind != ENTITY_VARIABLE)
5333 /* §6.7:7 If an identifier for an object is declared with no linkage, the
5334 * type for the object shall be complete [...] */
5335 declaration_t *decl = &ent->declaration;
5336 if (decl->storage_class == STORAGE_CLASS_EXTERN ||
5337 decl->storage_class == STORAGE_CLASS_STATIC)
5340 type_t *const orig_type = decl->type;
5341 type_t *const type = skip_typeref(orig_type);
5342 if (!is_type_incomplete(type))
5345 /* §6.9.2:2 and §6.9.2:5: At the end of the translation incomplete arrays
5346 * are given length one. */
5347 if (is_type_array(type) && ent->base.parent_scope == file_scope) {
5348 ARR_APP1(declaration_t*, incomplete_arrays, decl);
5352 errorf(&ent->base.source_position, "variable '%#T' has incomplete type",
5353 orig_type, ent->base.symbol);
5357 static void parse_declaration_rest(entity_t *ndeclaration,
5358 const declaration_specifiers_t *specifiers,
5359 parsed_declaration_func finished_declaration,
5360 declarator_flags_t flags)
5362 add_anchor_token(';');
5363 add_anchor_token(',');
5365 entity_t *entity = finished_declaration(ndeclaration, token.type == '=');
5367 if (token.type == '=') {
5368 parse_init_declarator_rest(entity);
5369 } else if (entity->kind == ENTITY_VARIABLE) {
5370 /* ISO/IEC 14882:1998(E) §8.5.3:3 The initializer can be omitted
5371 * [...] where the extern specifier is explicitly used. */
5372 declaration_t *decl = &entity->declaration;
5373 if (decl->storage_class != STORAGE_CLASS_EXTERN) {
5374 type_t *type = decl->type;
5375 if (is_type_reference(skip_typeref(type))) {
5376 errorf(&entity->base.source_position,
5377 "reference '%#T' must be initialized",
5378 type, entity->base.symbol);
5383 check_variable_type_complete(entity);
5385 if (token.type != ',')
5389 add_anchor_token('=');
5390 ndeclaration = parse_declarator(specifiers, flags);
5391 rem_anchor_token('=');
5393 expect(';', end_error);
5396 anonymous_entity = NULL;
5397 rem_anchor_token(';');
5398 rem_anchor_token(',');
5401 static entity_t *finished_kr_declaration(entity_t *entity, bool is_definition)
5403 symbol_t *symbol = entity->base.symbol;
5404 if (symbol == NULL) {
5405 errorf(HERE, "anonymous declaration not valid as function parameter");
5409 assert(entity->base.namespc == NAMESPACE_NORMAL);
5410 entity_t *previous_entity = get_entity(symbol, NAMESPACE_NORMAL);
5411 if (previous_entity == NULL
5412 || previous_entity->base.parent_scope != current_scope) {
5413 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
5418 if (is_definition) {
5419 errorf(HERE, "parameter '%Y' is initialised", entity->base.symbol);
5422 return record_entity(entity, false);
5425 static void parse_declaration(parsed_declaration_func finished_declaration,
5426 declarator_flags_t flags)
5428 declaration_specifiers_t specifiers;
5429 memset(&specifiers, 0, sizeof(specifiers));
5431 add_anchor_token(';');
5432 parse_declaration_specifiers(&specifiers);
5433 rem_anchor_token(';');
5435 if (token.type == ';') {
5436 parse_anonymous_declaration_rest(&specifiers);
5438 entity_t *entity = parse_declarator(&specifiers, flags);
5439 parse_declaration_rest(entity, &specifiers, finished_declaration, flags);
5443 static type_t *get_default_promoted_type(type_t *orig_type)
5445 type_t *result = orig_type;
5447 type_t *type = skip_typeref(orig_type);
5448 if (is_type_integer(type)) {
5449 result = promote_integer(type);
5450 } else if (is_type_atomic(type, ATOMIC_TYPE_FLOAT)) {
5451 result = type_double;
5457 static void parse_kr_declaration_list(entity_t *entity)
5459 if (entity->kind != ENTITY_FUNCTION)
5462 type_t *type = skip_typeref(entity->declaration.type);
5463 assert(is_type_function(type));
5464 if (!type->function.kr_style_parameters)
5468 add_anchor_token('{');
5470 /* push function parameters */
5471 size_t const top = environment_top();
5472 scope_t *old_scope = scope_push(&entity->function.parameters);
5474 entity_t *parameter = entity->function.parameters.entities;
5475 for ( ; parameter != NULL; parameter = parameter->base.next) {
5476 assert(parameter->base.parent_scope == NULL);
5477 parameter->base.parent_scope = current_scope;
5478 environment_push(parameter);
5481 /* parse declaration list */
5483 switch (token.type) {
5485 case T___extension__:
5486 /* This covers symbols, which are no type, too, and results in
5487 * better error messages. The typical cases are misspelled type
5488 * names and missing includes. */
5490 parse_declaration(finished_kr_declaration, DECL_IS_PARAMETER);
5498 /* pop function parameters */
5499 assert(current_scope == &entity->function.parameters);
5500 scope_pop(old_scope);
5501 environment_pop_to(top);
5503 /* update function type */
5504 type_t *new_type = duplicate_type(type);
5506 function_parameter_t *parameters = NULL;
5507 function_parameter_t *last_parameter = NULL;
5509 parameter = entity->function.parameters.entities;
5510 for (; parameter != NULL; parameter = parameter->base.next) {
5511 if (parameter->kind != ENTITY_PARAMETER)
5514 type_t *parameter_type = parameter->declaration.type;
5515 if (parameter_type == NULL) {
5517 errorf(HERE, "no type specified for function parameter '%Y'",
5518 parameter->base.symbol);
5520 if (warning.implicit_int) {
5521 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
5522 parameter->base.symbol);
5524 parameter_type = type_int;
5525 parameter->declaration.type = parameter_type;
5529 semantic_parameter_incomplete(parameter);
5530 parameter_type = parameter->declaration.type;
5533 * we need the default promoted types for the function type
5535 parameter_type = get_default_promoted_type(parameter_type);
5537 function_parameter_t *function_parameter
5538 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
5539 memset(function_parameter, 0, sizeof(function_parameter[0]));
5541 function_parameter->type = parameter_type;
5542 if (last_parameter != NULL) {
5543 last_parameter->next = function_parameter;
5545 parameters = function_parameter;
5547 last_parameter = function_parameter;
5550 /* §6.9.1.7: A K&R style parameter list does NOT act as a function
5552 new_type->function.parameters = parameters;
5553 new_type->function.unspecified_parameters = true;
5555 new_type = identify_new_type(new_type);
5557 entity->declaration.type = new_type;
5559 rem_anchor_token('{');
5562 static bool first_err = true;
5565 * When called with first_err set, prints the name of the current function,
5568 static void print_in_function(void)
5572 diagnosticf("%s: In function '%Y':\n",
5573 current_function->base.base.source_position.input_name,
5574 current_function->base.base.symbol);
5579 * Check if all labels are defined in the current function.
5580 * Check if all labels are used in the current function.
5582 static void check_labels(void)
5584 for (const goto_statement_t *goto_statement = goto_first;
5585 goto_statement != NULL;
5586 goto_statement = goto_statement->next) {
5587 /* skip computed gotos */
5588 if (goto_statement->expression != NULL)
5591 label_t *label = goto_statement->label;
5594 if (label->base.source_position.input_name == NULL) {
5595 print_in_function();
5596 errorf(&goto_statement->base.source_position,
5597 "label '%Y' used but not defined", label->base.symbol);
5601 if (warning.unused_label) {
5602 for (const label_statement_t *label_statement = label_first;
5603 label_statement != NULL;
5604 label_statement = label_statement->next) {
5605 label_t *label = label_statement->label;
5607 if (! label->used) {
5608 print_in_function();
5609 warningf(&label_statement->base.source_position,
5610 "label '%Y' defined but not used", label->base.symbol);
5616 static void warn_unused_entity(entity_t *entity, entity_t *last)
5618 entity_t const *const end = last != NULL ? last->base.next : NULL;
5619 for (; entity != end; entity = entity->base.next) {
5620 if (!is_declaration(entity))
5623 declaration_t *declaration = &entity->declaration;
5624 if (declaration->implicit)
5627 if (!declaration->used) {
5628 print_in_function();
5629 const char *what = get_entity_kind_name(entity->kind);
5630 warningf(&entity->base.source_position, "%s '%Y' is unused",
5631 what, entity->base.symbol);
5632 } else if (entity->kind == ENTITY_VARIABLE && !entity->variable.read) {
5633 print_in_function();
5634 const char *what = get_entity_kind_name(entity->kind);
5635 warningf(&entity->base.source_position, "%s '%Y' is never read",
5636 what, entity->base.symbol);
5641 static void check_unused_variables(statement_t *const stmt, void *const env)
5645 switch (stmt->kind) {
5646 case STATEMENT_DECLARATION: {
5647 declaration_statement_t const *const decls = &stmt->declaration;
5648 warn_unused_entity(decls->declarations_begin,
5649 decls->declarations_end);
5654 warn_unused_entity(stmt->fors.scope.entities, NULL);
5663 * Check declarations of current_function for unused entities.
5665 static void check_declarations(void)
5667 if (warning.unused_parameter) {
5668 const scope_t *scope = ¤t_function->parameters;
5670 /* do not issue unused warnings for main */
5671 if (!is_sym_main(current_function->base.base.symbol)) {
5672 warn_unused_entity(scope->entities, NULL);
5675 if (warning.unused_variable) {
5676 walk_statements(current_function->statement, check_unused_variables,
5681 static int determine_truth(expression_t const* const cond)
5684 !is_constant_expression(cond) ? 0 :
5685 fold_constant(cond) != 0 ? 1 :
5689 static void check_reachable(statement_t *);
5690 static bool reaches_end;
5692 static bool expression_returns(expression_t const *const expr)
5694 switch (expr->kind) {
5696 expression_t const *const func = expr->call.function;
5697 if (func->kind == EXPR_REFERENCE) {
5698 entity_t *entity = func->reference.entity;
5699 if (entity->kind == ENTITY_FUNCTION
5700 && entity->declaration.modifiers & DM_NORETURN)
5704 if (!expression_returns(func))
5707 for (call_argument_t const* arg = expr->call.arguments; arg != NULL; arg = arg->next) {
5708 if (!expression_returns(arg->expression))
5715 case EXPR_REFERENCE:
5716 case EXPR_REFERENCE_ENUM_VALUE:
5718 case EXPR_CHARACTER_CONSTANT:
5719 case EXPR_WIDE_CHARACTER_CONSTANT:
5720 case EXPR_STRING_LITERAL:
5721 case EXPR_WIDE_STRING_LITERAL:
5722 case EXPR_COMPOUND_LITERAL: // TODO descend into initialisers
5723 case EXPR_LABEL_ADDRESS:
5724 case EXPR_CLASSIFY_TYPE:
5725 case EXPR_SIZEOF: // TODO handle obscure VLA case
5728 case EXPR_BUILTIN_CONSTANT_P:
5729 case EXPR_BUILTIN_TYPES_COMPATIBLE_P:
5734 case EXPR_STATEMENT: {
5735 bool old_reaches_end = reaches_end;
5736 reaches_end = false;
5737 check_reachable(expr->statement.statement);
5738 bool returns = reaches_end;
5739 reaches_end = old_reaches_end;
5743 case EXPR_CONDITIONAL:
5744 // TODO handle constant expression
5746 if (!expression_returns(expr->conditional.condition))
5749 if (expr->conditional.true_expression != NULL
5750 && expression_returns(expr->conditional.true_expression))
5753 return expression_returns(expr->conditional.false_expression);
5756 return expression_returns(expr->select.compound);
5758 case EXPR_ARRAY_ACCESS:
5760 expression_returns(expr->array_access.array_ref) &&
5761 expression_returns(expr->array_access.index);
5764 return expression_returns(expr->va_starte.ap);
5767 return expression_returns(expr->va_arge.ap);
5769 EXPR_UNARY_CASES_MANDATORY
5770 return expression_returns(expr->unary.value);
5772 case EXPR_UNARY_THROW:
5776 // TODO handle constant lhs of && and ||
5778 expression_returns(expr->binary.left) &&
5779 expression_returns(expr->binary.right);
5785 panic("unhandled expression");
5788 static bool initializer_returns(initializer_t const *const init)
5790 switch (init->kind) {
5791 case INITIALIZER_VALUE:
5792 return expression_returns(init->value.value);
5794 case INITIALIZER_LIST: {
5795 initializer_t * const* i = init->list.initializers;
5796 initializer_t * const* const end = i + init->list.len;
5797 bool returns = true;
5798 for (; i != end; ++i) {
5799 if (!initializer_returns(*i))
5805 case INITIALIZER_STRING:
5806 case INITIALIZER_WIDE_STRING:
5807 case INITIALIZER_DESIGNATOR: // designators have no payload
5810 panic("unhandled initializer");
5813 static bool noreturn_candidate;
5815 static void check_reachable(statement_t *const stmt)
5817 if (stmt->base.reachable)
5819 if (stmt->kind != STATEMENT_DO_WHILE)
5820 stmt->base.reachable = true;
5822 statement_t *last = stmt;
5824 switch (stmt->kind) {
5825 case STATEMENT_INVALID:
5826 case STATEMENT_EMPTY:
5828 next = stmt->base.next;
5831 case STATEMENT_DECLARATION: {
5832 declaration_statement_t const *const decl = &stmt->declaration;
5833 entity_t const * ent = decl->declarations_begin;
5834 entity_t const *const last = decl->declarations_end;
5836 for (;; ent = ent->base.next) {
5837 if (ent->kind == ENTITY_VARIABLE &&
5838 ent->variable.initializer != NULL &&
5839 !initializer_returns(ent->variable.initializer)) {
5846 next = stmt->base.next;
5850 case STATEMENT_COMPOUND:
5851 next = stmt->compound.statements;
5853 next = stmt->base.next;
5856 case STATEMENT_RETURN: {
5857 expression_t const *const val = stmt->returns.value;
5858 if (val == NULL || expression_returns(val))
5859 noreturn_candidate = false;
5863 case STATEMENT_IF: {
5864 if_statement_t const *const ifs = &stmt->ifs;
5865 expression_t const *const cond = ifs->condition;
5867 if (!expression_returns(cond))
5870 int const val = determine_truth(cond);
5873 check_reachable(ifs->true_statement);
5878 if (ifs->false_statement != NULL) {
5879 check_reachable(ifs->false_statement);
5883 next = stmt->base.next;
5887 case STATEMENT_SWITCH: {
5888 switch_statement_t const *const switchs = &stmt->switchs;
5889 expression_t const *const expr = switchs->expression;
5891 if (!expression_returns(expr))
5894 if (is_constant_expression(expr)) {
5895 long const val = fold_constant(expr);
5896 case_label_statement_t * defaults = NULL;
5897 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5898 if (i->expression == NULL) {
5903 if (i->first_case <= val && val <= i->last_case) {
5904 check_reachable((statement_t*)i);
5909 if (defaults != NULL) {
5910 check_reachable((statement_t*)defaults);
5914 bool has_default = false;
5915 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
5916 if (i->expression == NULL)
5919 check_reachable((statement_t*)i);
5926 next = stmt->base.next;
5930 case STATEMENT_EXPRESSION: {
5931 /* Check for noreturn function call */
5932 expression_t const *const expr = stmt->expression.expression;
5933 if (!expression_returns(expr))
5936 next = stmt->base.next;
5940 case STATEMENT_CONTINUE: {
5941 statement_t *parent = stmt;
5943 parent = parent->base.parent;
5944 if (parent == NULL) /* continue not within loop */
5948 switch (parent->kind) {
5949 case STATEMENT_WHILE: goto continue_while;
5950 case STATEMENT_DO_WHILE: goto continue_do_while;
5951 case STATEMENT_FOR: goto continue_for;
5958 case STATEMENT_BREAK: {
5959 statement_t *parent = stmt;
5961 parent = parent->base.parent;
5962 if (parent == NULL) /* break not within loop/switch */
5965 switch (parent->kind) {
5966 case STATEMENT_SWITCH:
5967 case STATEMENT_WHILE:
5968 case STATEMENT_DO_WHILE:
5971 next = parent->base.next;
5972 goto found_break_parent;
5981 case STATEMENT_GOTO:
5982 if (stmt->gotos.expression) {
5983 if (!expression_returns(stmt->gotos.expression))
5986 statement_t *parent = stmt->base.parent;
5987 if (parent == NULL) /* top level goto */
5991 next = stmt->gotos.label->statement;
5992 if (next == NULL) /* missing label */
5997 case STATEMENT_LABEL:
5998 next = stmt->label.statement;
6001 case STATEMENT_CASE_LABEL:
6002 next = stmt->case_label.statement;
6005 case STATEMENT_WHILE: {
6006 while_statement_t const *const whiles = &stmt->whiles;
6007 expression_t const *const cond = whiles->condition;
6009 if (!expression_returns(cond))
6012 int const val = determine_truth(cond);
6015 check_reachable(whiles->body);
6020 next = stmt->base.next;
6024 case STATEMENT_DO_WHILE:
6025 next = stmt->do_while.body;
6028 case STATEMENT_FOR: {
6029 for_statement_t *const fors = &stmt->fors;
6031 if (fors->condition_reachable)
6033 fors->condition_reachable = true;
6035 expression_t const *const cond = fors->condition;
6040 } else if (expression_returns(cond)) {
6041 val = determine_truth(cond);
6047 check_reachable(fors->body);
6052 next = stmt->base.next;
6056 case STATEMENT_MS_TRY: {
6057 ms_try_statement_t const *const ms_try = &stmt->ms_try;
6058 check_reachable(ms_try->try_statement);
6059 next = ms_try->final_statement;
6063 case STATEMENT_LEAVE: {
6064 statement_t *parent = stmt;
6066 parent = parent->base.parent;
6067 if (parent == NULL) /* __leave not within __try */
6070 if (parent->kind == STATEMENT_MS_TRY) {
6072 next = parent->ms_try.final_statement;
6080 panic("invalid statement kind");
6083 while (next == NULL) {
6084 next = last->base.parent;
6086 noreturn_candidate = false;
6088 type_t *const type = skip_typeref(current_function->base.type);
6089 assert(is_type_function(type));
6090 type_t *const ret = skip_typeref(type->function.return_type);
6091 if (warning.return_type &&
6092 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
6093 is_type_valid(ret) &&
6094 !is_sym_main(current_function->base.base.symbol)) {
6095 warningf(&stmt->base.source_position,
6096 "control reaches end of non-void function");
6101 switch (next->kind) {
6102 case STATEMENT_INVALID:
6103 case STATEMENT_EMPTY:
6104 case STATEMENT_DECLARATION:
6105 case STATEMENT_EXPRESSION:
6107 case STATEMENT_RETURN:
6108 case STATEMENT_CONTINUE:
6109 case STATEMENT_BREAK:
6110 case STATEMENT_GOTO:
6111 case STATEMENT_LEAVE:
6112 panic("invalid control flow in function");
6114 case STATEMENT_COMPOUND:
6115 if (next->compound.stmt_expr) {
6121 case STATEMENT_SWITCH:
6122 case STATEMENT_LABEL:
6123 case STATEMENT_CASE_LABEL:
6125 next = next->base.next;
6128 case STATEMENT_WHILE: {
6130 if (next->base.reachable)
6132 next->base.reachable = true;
6134 while_statement_t const *const whiles = &next->whiles;
6135 expression_t const *const cond = whiles->condition;
6137 if (!expression_returns(cond))
6140 int const val = determine_truth(cond);
6143 check_reachable(whiles->body);
6149 next = next->base.next;
6153 case STATEMENT_DO_WHILE: {
6155 if (next->base.reachable)
6157 next->base.reachable = true;
6159 do_while_statement_t const *const dw = &next->do_while;
6160 expression_t const *const cond = dw->condition;
6162 if (!expression_returns(cond))
6165 int const val = determine_truth(cond);
6168 check_reachable(dw->body);
6174 next = next->base.next;
6178 case STATEMENT_FOR: {
6180 for_statement_t *const fors = &next->fors;
6182 fors->step_reachable = true;
6184 if (fors->condition_reachable)
6186 fors->condition_reachable = true;
6188 expression_t const *const cond = fors->condition;
6193 } else if (expression_returns(cond)) {
6194 val = determine_truth(cond);
6200 check_reachable(fors->body);
6206 next = next->base.next;
6210 case STATEMENT_MS_TRY:
6212 next = next->ms_try.final_statement;
6217 check_reachable(next);
6220 static void check_unreachable(statement_t* const stmt, void *const env)
6224 switch (stmt->kind) {
6225 case STATEMENT_DO_WHILE:
6226 if (!stmt->base.reachable) {
6227 expression_t const *const cond = stmt->do_while.condition;
6228 if (determine_truth(cond) >= 0) {
6229 warningf(&cond->base.source_position,
6230 "condition of do-while-loop is unreachable");
6235 case STATEMENT_FOR: {
6236 for_statement_t const* const fors = &stmt->fors;
6238 // if init and step are unreachable, cond is unreachable, too
6239 if (!stmt->base.reachable && !fors->step_reachable) {
6240 warningf(&stmt->base.source_position, "statement is unreachable");
6242 if (!stmt->base.reachable && fors->initialisation != NULL) {
6243 warningf(&fors->initialisation->base.source_position,
6244 "initialisation of for-statement is unreachable");
6247 if (!fors->condition_reachable && fors->condition != NULL) {
6248 warningf(&fors->condition->base.source_position,
6249 "condition of for-statement is unreachable");
6252 if (!fors->step_reachable && fors->step != NULL) {
6253 warningf(&fors->step->base.source_position,
6254 "step of for-statement is unreachable");
6260 case STATEMENT_COMPOUND:
6261 if (stmt->compound.statements != NULL)
6263 goto warn_unreachable;
6265 case STATEMENT_DECLARATION: {
6266 /* Only warn if there is at least one declarator with an initializer.
6267 * This typically occurs in switch statements. */
6268 declaration_statement_t const *const decl = &stmt->declaration;
6269 entity_t const * ent = decl->declarations_begin;
6270 entity_t const *const last = decl->declarations_end;
6272 for (;; ent = ent->base.next) {
6273 if (ent->kind == ENTITY_VARIABLE &&
6274 ent->variable.initializer != NULL) {
6275 goto warn_unreachable;
6285 if (!stmt->base.reachable)
6286 warningf(&stmt->base.source_position, "statement is unreachable");
6291 static void parse_external_declaration(void)
6293 /* function-definitions and declarations both start with declaration
6295 declaration_specifiers_t specifiers;
6296 memset(&specifiers, 0, sizeof(specifiers));
6298 add_anchor_token(';');
6299 parse_declaration_specifiers(&specifiers);
6300 rem_anchor_token(';');
6302 /* must be a declaration */
6303 if (token.type == ';') {
6304 parse_anonymous_declaration_rest(&specifiers);
6308 add_anchor_token(',');
6309 add_anchor_token('=');
6310 add_anchor_token(';');
6311 add_anchor_token('{');
6313 /* declarator is common to both function-definitions and declarations */
6314 entity_t *ndeclaration = parse_declarator(&specifiers, DECL_FLAGS_NONE);
6316 rem_anchor_token('{');
6317 rem_anchor_token(';');
6318 rem_anchor_token('=');
6319 rem_anchor_token(',');
6321 /* must be a declaration */
6322 switch (token.type) {
6326 parse_declaration_rest(ndeclaration, &specifiers, record_entity,
6331 /* must be a function definition */
6332 parse_kr_declaration_list(ndeclaration);
6334 if (token.type != '{') {
6335 parse_error_expected("while parsing function definition", '{', NULL);
6336 eat_until_matching_token(';');
6340 assert(is_declaration(ndeclaration));
6341 type_t *const orig_type = ndeclaration->declaration.type;
6342 type_t * type = skip_typeref(orig_type);
6344 if (!is_type_function(type)) {
6345 if (is_type_valid(type)) {
6346 errorf(HERE, "declarator '%#T' has a body but is not a function type",
6347 type, ndeclaration->base.symbol);
6351 } else if (is_typeref(orig_type)) {
6353 errorf(&ndeclaration->base.source_position,
6354 "type of function definition '%#T' is a typedef",
6355 orig_type, ndeclaration->base.symbol);
6358 if (warning.aggregate_return &&
6359 is_type_compound(skip_typeref(type->function.return_type))) {
6360 warningf(HERE, "function '%Y' returns an aggregate",
6361 ndeclaration->base.symbol);
6363 if (warning.traditional && !type->function.unspecified_parameters) {
6364 warningf(HERE, "traditional C rejects ISO C style function definition of function '%Y'",
6365 ndeclaration->base.symbol);
6367 if (warning.old_style_definition && type->function.unspecified_parameters) {
6368 warningf(HERE, "old-style function definition '%Y'",
6369 ndeclaration->base.symbol);
6372 /* §6.7.5.3:14 a function definition with () means no
6373 * parameters (and not unspecified parameters) */
6374 if (type->function.unspecified_parameters &&
6375 type->function.parameters == NULL &&
6376 !type->function.kr_style_parameters) {
6377 type_t *copy = duplicate_type(type);
6378 copy->function.unspecified_parameters = false;
6379 type = identify_new_type(copy);
6381 ndeclaration->declaration.type = type;
6384 entity_t *const entity = record_entity(ndeclaration, true);
6385 assert(entity->kind == ENTITY_FUNCTION);
6386 assert(ndeclaration->kind == ENTITY_FUNCTION);
6388 function_t *function = &entity->function;
6389 if (ndeclaration != entity) {
6390 function->parameters = ndeclaration->function.parameters;
6392 assert(is_declaration(entity));
6393 type = skip_typeref(entity->declaration.type);
6395 /* push function parameters and switch scope */
6396 size_t const top = environment_top();
6397 scope_t *old_scope = scope_push(&function->parameters);
6399 entity_t *parameter = function->parameters.entities;
6400 for (; parameter != NULL; parameter = parameter->base.next) {
6401 if (parameter->base.parent_scope == &ndeclaration->function.parameters) {
6402 parameter->base.parent_scope = current_scope;
6404 assert(parameter->base.parent_scope == NULL
6405 || parameter->base.parent_scope == current_scope);
6406 parameter->base.parent_scope = current_scope;
6407 if (parameter->base.symbol == NULL) {
6408 errorf(¶meter->base.source_position, "parameter name omitted");
6411 environment_push(parameter);
6414 if (function->statement != NULL) {
6415 parser_error_multiple_definition(entity, HERE);
6418 /* parse function body */
6419 int label_stack_top = label_top();
6420 function_t *old_current_function = current_function;
6421 current_function = function;
6422 current_parent = NULL;
6425 goto_anchor = &goto_first;
6427 label_anchor = &label_first;
6429 statement_t *const body = parse_compound_statement(false);
6430 function->statement = body;
6433 check_declarations();
6434 if (warning.return_type ||
6435 warning.unreachable_code ||
6436 (warning.missing_noreturn
6437 && !(function->base.modifiers & DM_NORETURN))) {
6438 noreturn_candidate = true;
6439 check_reachable(body);
6440 if (warning.unreachable_code)
6441 walk_statements(body, check_unreachable, NULL);
6442 if (warning.missing_noreturn &&
6443 noreturn_candidate &&
6444 !(function->base.modifiers & DM_NORETURN)) {
6445 warningf(&body->base.source_position,
6446 "function '%#T' is candidate for attribute 'noreturn'",
6447 type, entity->base.symbol);
6451 assert(current_parent == NULL);
6452 assert(current_function == function);
6453 current_function = old_current_function;
6454 label_pop_to(label_stack_top);
6457 assert(current_scope == &function->parameters);
6458 scope_pop(old_scope);
6459 environment_pop_to(top);
6462 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
6463 source_position_t *source_position,
6464 const symbol_t *symbol)
6466 type_t *type = allocate_type_zero(TYPE_BITFIELD);
6468 type->bitfield.base_type = base_type;
6469 type->bitfield.size_expression = size;
6472 type_t *skipped_type = skip_typeref(base_type);
6473 if (!is_type_integer(skipped_type)) {
6474 errorf(HERE, "bitfield base type '%T' is not an integer type",
6478 bit_size = skipped_type->base.size * 8;
6481 if (is_constant_expression(size)) {
6482 long v = fold_constant(size);
6485 errorf(source_position, "negative width in bit-field '%Y'", symbol);
6486 } else if (v == 0) {
6487 errorf(source_position, "zero width for bit-field '%Y'", symbol);
6488 } else if (bit_size > 0 && (il_size_t)v > bit_size) {
6489 errorf(source_position, "width of '%Y' exceeds its type", symbol);
6491 type->bitfield.bit_size = v;
6498 static entity_t *find_compound_entry(compound_t *compound, symbol_t *symbol)
6500 entity_t *iter = compound->members.entities;
6501 for (; iter != NULL; iter = iter->base.next) {
6502 if (iter->kind != ENTITY_COMPOUND_MEMBER)
6505 if (iter->base.symbol == symbol) {
6507 } else if (iter->base.symbol == NULL) {
6508 type_t *type = skip_typeref(iter->declaration.type);
6509 if (is_type_compound(type)) {
6511 = find_compound_entry(type->compound.compound, symbol);
6522 static void parse_compound_declarators(compound_t *compound,
6523 const declaration_specifiers_t *specifiers)
6528 if (token.type == ':') {
6529 source_position_t source_position = *HERE;
6532 type_t *base_type = specifiers->type;
6533 expression_t *size = parse_constant_expression();
6535 type_t *type = make_bitfield_type(base_type, size,
6536 &source_position, sym_anonymous);
6538 entity = allocate_entity_zero(ENTITY_COMPOUND_MEMBER);
6539 entity->base.namespc = NAMESPACE_NORMAL;
6540 entity->base.source_position = source_position;
6541 entity->declaration.declared_storage_class = STORAGE_CLASS_NONE;
6542 entity->declaration.storage_class = STORAGE_CLASS_NONE;
6543 entity->declaration.modifiers = specifiers->modifiers;
6544 entity->declaration.type = type;
6545 append_entity(&compound->members, entity);
6547 entity = parse_declarator(specifiers,
6548 DECL_MAY_BE_ABSTRACT | DECL_CREATE_COMPOUND_MEMBER);
6549 if (entity->kind == ENTITY_TYPEDEF) {
6550 errorf(&entity->base.source_position,
6551 "typedef not allowed as compound member");
6553 assert(entity->kind == ENTITY_COMPOUND_MEMBER);
6555 /* make sure we don't define a symbol multiple times */
6556 symbol_t *symbol = entity->base.symbol;
6557 if (symbol != NULL) {
6558 entity_t *prev = find_compound_entry(compound, symbol);
6560 errorf(&entity->base.source_position,
6561 "multiple declarations of symbol '%Y' (declared %P)",
6562 symbol, &prev->base.source_position);
6566 if (token.type == ':') {
6567 source_position_t source_position = *HERE;
6569 expression_t *size = parse_constant_expression();
6571 type_t *type = entity->declaration.type;
6572 type_t *bitfield_type = make_bitfield_type(type, size,
6573 &source_position, entity->base.symbol);
6574 entity->declaration.type = bitfield_type;
6576 type_t *orig_type = entity->declaration.type;
6577 type_t *type = skip_typeref(orig_type);
6578 if (is_type_function(type)) {
6579 errorf(&entity->base.source_position,
6580 "compound member '%Y' must not have function type '%T'",
6581 entity->base.symbol, orig_type);
6582 } else if (is_type_incomplete(type)) {
6583 /* §6.7.2.1:16 flexible array member */
6584 if (!is_type_array(type) ||
6585 token.type != ';' ||
6586 look_ahead(1)->type != '}') {
6587 errorf(&entity->base.source_position,
6588 "compound member '%Y' has incomplete type '%T'",
6589 entity->base.symbol, orig_type);
6594 append_entity(&compound->members, entity);
6598 if (token.type != ',')
6602 expect(';', end_error);
6605 anonymous_entity = NULL;
6608 static void parse_compound_type_entries(compound_t *compound)
6611 add_anchor_token('}');
6613 while (token.type != '}') {
6614 if (token.type == T_EOF) {
6615 errorf(HERE, "EOF while parsing struct");
6618 declaration_specifiers_t specifiers;
6619 memset(&specifiers, 0, sizeof(specifiers));
6620 parse_declaration_specifiers(&specifiers);
6622 parse_compound_declarators(compound, &specifiers);
6624 rem_anchor_token('}');
6628 compound->complete = true;
6631 static type_t *parse_typename(void)
6633 declaration_specifiers_t specifiers;
6634 memset(&specifiers, 0, sizeof(specifiers));
6635 parse_declaration_specifiers(&specifiers);
6636 if (specifiers.storage_class != STORAGE_CLASS_NONE ||
6637 specifiers.thread_local) {
6638 /* TODO: improve error message, user does probably not know what a
6639 * storage class is...
6641 errorf(HERE, "typename may not have a storage class");
6644 type_t *result = parse_abstract_declarator(specifiers.type);
6652 typedef expression_t* (*parse_expression_function)(void);
6653 typedef expression_t* (*parse_expression_infix_function)(expression_t *left);
6655 typedef struct expression_parser_function_t expression_parser_function_t;
6656 struct expression_parser_function_t {
6657 parse_expression_function parser;
6658 precedence_t infix_precedence;
6659 parse_expression_infix_function infix_parser;
6662 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
6665 * Prints an error message if an expression was expected but not read
6667 static expression_t *expected_expression_error(void)
6669 /* skip the error message if the error token was read */
6670 if (token.type != T_ERROR) {
6671 errorf(HERE, "expected expression, got token %K", &token);
6675 return create_invalid_expression();
6679 * Parse a string constant.
6681 static expression_t *parse_string_const(void)
6684 if (token.type == T_STRING_LITERAL) {
6685 string_t res = token.v.string;
6687 while (token.type == T_STRING_LITERAL) {
6688 res = concat_strings(&res, &token.v.string);
6691 if (token.type != T_WIDE_STRING_LITERAL) {
6692 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
6693 /* note: that we use type_char_ptr here, which is already the
6694 * automatic converted type. revert_automatic_type_conversion
6695 * will construct the array type */
6696 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
6697 cnst->string.value = res;
6701 wres = concat_string_wide_string(&res, &token.v.wide_string);
6703 wres = token.v.wide_string;
6708 switch (token.type) {
6709 case T_WIDE_STRING_LITERAL:
6710 wres = concat_wide_strings(&wres, &token.v.wide_string);
6713 case T_STRING_LITERAL:
6714 wres = concat_wide_string_string(&wres, &token.v.string);
6718 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
6719 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
6720 cnst->wide_string.value = wres;
6729 * Parse a boolean constant.
6731 static expression_t *parse_bool_const(bool value)
6733 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6734 cnst->base.type = type_bool;
6735 cnst->conste.v.int_value = value;
6743 * Parse an integer constant.
6745 static expression_t *parse_int_const(void)
6747 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6748 cnst->base.type = token.datatype;
6749 cnst->conste.v.int_value = token.v.intvalue;
6757 * Parse a character constant.
6759 static expression_t *parse_character_constant(void)
6761 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
6762 cnst->base.type = token.datatype;
6763 cnst->conste.v.character = token.v.string;
6765 if (cnst->conste.v.character.size != 1) {
6767 errorf(HERE, "more than 1 character in character constant");
6768 } else if (warning.multichar) {
6769 warningf(HERE, "multi-character character constant");
6778 * Parse a wide character constant.
6780 static expression_t *parse_wide_character_constant(void)
6782 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
6783 cnst->base.type = token.datatype;
6784 cnst->conste.v.wide_character = token.v.wide_string;
6786 if (cnst->conste.v.wide_character.size != 1) {
6788 errorf(HERE, "more than 1 character in character constant");
6789 } else if (warning.multichar) {
6790 warningf(HERE, "multi-character character constant");
6799 * Parse a float constant.
6801 static expression_t *parse_float_const(void)
6803 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6804 cnst->base.type = token.datatype;
6805 cnst->conste.v.float_value = token.v.floatvalue;
6812 static entity_t *create_implicit_function(symbol_t *symbol,
6813 const source_position_t *source_position)
6815 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
6816 ntype->function.return_type = type_int;
6817 ntype->function.unspecified_parameters = true;
6818 ntype->function.linkage = LINKAGE_C;
6819 type_t *type = identify_new_type(ntype);
6821 entity_t *entity = allocate_entity_zero(ENTITY_FUNCTION);
6822 entity->declaration.storage_class = STORAGE_CLASS_EXTERN;
6823 entity->declaration.declared_storage_class = STORAGE_CLASS_EXTERN;
6824 entity->declaration.type = type;
6825 entity->declaration.implicit = true;
6826 entity->base.symbol = symbol;
6827 entity->base.source_position = *source_position;
6829 bool strict_prototypes_old = warning.strict_prototypes;
6830 warning.strict_prototypes = false;
6831 record_entity(entity, false);
6832 warning.strict_prototypes = strict_prototypes_old;
6838 * Creates a return_type (func)(argument_type) function type if not
6841 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
6842 type_t *argument_type2)
6844 function_parameter_t *parameter2
6845 = obstack_alloc(type_obst, sizeof(parameter2[0]));
6846 memset(parameter2, 0, sizeof(parameter2[0]));
6847 parameter2->type = argument_type2;
6849 function_parameter_t *parameter1
6850 = obstack_alloc(type_obst, sizeof(parameter1[0]));
6851 memset(parameter1, 0, sizeof(parameter1[0]));
6852 parameter1->type = argument_type1;
6853 parameter1->next = parameter2;
6855 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6856 type->function.return_type = return_type;
6857 type->function.parameters = parameter1;
6859 return identify_new_type(type);
6863 * Creates a return_type (func)(argument_type) function type if not
6866 * @param return_type the return type
6867 * @param argument_type the argument type
6869 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
6871 function_parameter_t *parameter
6872 = obstack_alloc(type_obst, sizeof(parameter[0]));
6873 memset(parameter, 0, sizeof(parameter[0]));
6874 parameter->type = argument_type;
6876 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6877 type->function.return_type = return_type;
6878 type->function.parameters = parameter;
6880 return identify_new_type(type);
6883 static type_t *make_function_1_type_variadic(type_t *return_type, type_t *argument_type)
6885 type_t *res = make_function_1_type(return_type, argument_type);
6886 res->function.variadic = 1;
6891 * Creates a return_type (func)(void) function type if not
6894 * @param return_type the return type
6896 static type_t *make_function_0_type(type_t *return_type)
6898 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6899 type->function.return_type = return_type;
6900 type->function.parameters = NULL;
6902 return identify_new_type(type);
6906 * Creates a NO_RETURN return_type (func)(void) function type if not
6909 * @param return_type the return type
6911 static type_t *make_function_0_type_noreturn(type_t *return_type)
6913 type_t *type = allocate_type_zero(TYPE_FUNCTION);
6914 type->function.return_type = return_type;
6915 type->function.parameters = NULL;
6916 type->function.base.modifiers |= DM_NORETURN;
6919 return identify_new_type(type);
6923 * Performs automatic type cast as described in §6.3.2.1.
6925 * @param orig_type the original type
6927 static type_t *automatic_type_conversion(type_t *orig_type)
6929 type_t *type = skip_typeref(orig_type);
6930 if (is_type_array(type)) {
6931 array_type_t *array_type = &type->array;
6932 type_t *element_type = array_type->element_type;
6933 unsigned qualifiers = array_type->base.qualifiers;
6935 return make_pointer_type(element_type, qualifiers);
6938 if (is_type_function(type)) {
6939 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6946 * reverts the automatic casts of array to pointer types and function
6947 * to function-pointer types as defined §6.3.2.1
6949 type_t *revert_automatic_type_conversion(const expression_t *expression)
6951 switch (expression->kind) {
6952 case EXPR_REFERENCE: {
6953 entity_t *entity = expression->reference.entity;
6954 if (is_declaration(entity)) {
6955 return entity->declaration.type;
6956 } else if (entity->kind == ENTITY_ENUM_VALUE) {
6957 return entity->enum_value.enum_type;
6959 panic("no declaration or enum in reference");
6964 entity_t *entity = expression->select.compound_entry;
6965 assert(is_declaration(entity));
6966 type_t *type = entity->declaration.type;
6967 return get_qualified_type(type,
6968 expression->base.type->base.qualifiers);
6971 case EXPR_UNARY_DEREFERENCE: {
6972 const expression_t *const value = expression->unary.value;
6973 type_t *const type = skip_typeref(value->base.type);
6974 if (!is_type_pointer(type))
6975 return type_error_type;
6976 return type->pointer.points_to;
6979 case EXPR_ARRAY_ACCESS: {
6980 const expression_t *array_ref = expression->array_access.array_ref;
6981 type_t *type_left = skip_typeref(array_ref->base.type);
6982 if (!is_type_pointer(type_left))
6983 return type_error_type;
6984 return type_left->pointer.points_to;
6987 case EXPR_STRING_LITERAL: {
6988 size_t size = expression->string.value.size;
6989 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
6992 case EXPR_WIDE_STRING_LITERAL: {
6993 size_t size = expression->wide_string.value.size;
6994 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
6997 case EXPR_COMPOUND_LITERAL:
6998 return expression->compound_literal.type;
7001 return expression->base.type;
7005 static expression_t *parse_reference(void)
7007 symbol_t *const symbol = token.v.symbol;
7009 entity_t *entity = get_entity(symbol, NAMESPACE_NORMAL);
7011 if (entity == NULL) {
7012 if (!strict_mode && look_ahead(1)->type == '(') {
7013 /* an implicitly declared function */
7014 if (warning.error_implicit_function_declaration) {
7015 errorf(HERE, "implicit declaration of function '%Y'", symbol);
7016 } else if (warning.implicit_function_declaration) {
7017 warningf(HERE, "implicit declaration of function '%Y'", symbol);
7020 entity = create_implicit_function(symbol, HERE);
7022 errorf(HERE, "unknown identifier '%Y' found.", symbol);
7023 entity = create_error_entity(symbol, ENTITY_VARIABLE);
7029 if (is_declaration(entity)) {
7030 orig_type = entity->declaration.type;
7031 } else if (entity->kind == ENTITY_ENUM_VALUE) {
7032 orig_type = entity->enum_value.enum_type;
7033 } else if (entity->kind == ENTITY_TYPEDEF) {
7034 errorf(HERE, "encountered typedef name '%Y' while parsing expression",
7037 return create_invalid_expression();
7039 panic("expected declaration or enum value in reference");
7042 /* we always do the auto-type conversions; the & and sizeof parser contains
7043 * code to revert this! */
7044 type_t *type = automatic_type_conversion(orig_type);
7046 expression_kind_t kind = EXPR_REFERENCE;
7047 if (entity->kind == ENTITY_ENUM_VALUE)
7048 kind = EXPR_REFERENCE_ENUM_VALUE;
7050 expression_t *expression = allocate_expression_zero(kind);
7051 expression->reference.entity = entity;
7052 expression->base.type = type;
7054 /* this declaration is used */
7055 if (is_declaration(entity)) {
7056 entity->declaration.used = true;
7059 if (entity->base.parent_scope != file_scope
7060 && entity->base.parent_scope->depth < current_function->parameters.depth
7061 && is_type_valid(orig_type) && !is_type_function(orig_type)) {
7062 if (entity->kind == ENTITY_VARIABLE) {
7063 /* access of a variable from an outer function */
7064 entity->variable.address_taken = true;
7065 } else if (entity->kind == ENTITY_PARAMETER) {
7066 entity->parameter.address_taken = true;
7068 current_function->need_closure = true;
7071 /* check for deprecated functions */
7072 if (warning.deprecated_declarations
7073 && is_declaration(entity)
7074 && entity->declaration.modifiers & DM_DEPRECATED) {
7075 declaration_t *declaration = &entity->declaration;
7077 char const *const prefix = entity->kind == ENTITY_FUNCTION ?
7078 "function" : "variable";
7080 if (declaration->deprecated_string != NULL) {
7081 warningf(HERE, "%s '%Y' is deprecated (declared %P): \"%s\"",
7082 prefix, entity->base.symbol, &entity->base.source_position,
7083 declaration->deprecated_string);
7085 warningf(HERE, "%s '%Y' is deprecated (declared %P)", prefix,
7086 entity->base.symbol, &entity->base.source_position);
7090 if (warning.init_self && entity == current_init_decl && !in_type_prop
7091 && entity->kind == ENTITY_VARIABLE) {
7092 current_init_decl = NULL;
7093 warningf(HERE, "variable '%#T' is initialized by itself",
7094 entity->declaration.type, entity->base.symbol);
7101 static bool semantic_cast(expression_t *cast)
7103 expression_t *expression = cast->unary.value;
7104 type_t *orig_dest_type = cast->base.type;
7105 type_t *orig_type_right = expression->base.type;
7106 type_t const *dst_type = skip_typeref(orig_dest_type);
7107 type_t const *src_type = skip_typeref(orig_type_right);
7108 source_position_t const *pos = &cast->base.source_position;
7110 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
7111 if (dst_type == type_void)
7114 /* only integer and pointer can be casted to pointer */
7115 if (is_type_pointer(dst_type) &&
7116 !is_type_pointer(src_type) &&
7117 !is_type_integer(src_type) &&
7118 is_type_valid(src_type)) {
7119 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
7123 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
7124 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
7128 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
7129 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
7133 if (warning.cast_qual &&
7134 is_type_pointer(src_type) &&
7135 is_type_pointer(dst_type)) {
7136 type_t *src = skip_typeref(src_type->pointer.points_to);
7137 type_t *dst = skip_typeref(dst_type->pointer.points_to);
7138 unsigned missing_qualifiers =
7139 src->base.qualifiers & ~dst->base.qualifiers;
7140 if (missing_qualifiers != 0) {
7142 "cast discards qualifiers '%Q' in pointer target type of '%T'",
7143 missing_qualifiers, orig_type_right);
7149 static expression_t *parse_compound_literal(type_t *type)
7151 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
7153 parse_initializer_env_t env;
7156 env.must_be_constant = false;
7157 initializer_t *initializer = parse_initializer(&env);
7160 expression->compound_literal.initializer = initializer;
7161 expression->compound_literal.type = type;
7162 expression->base.type = automatic_type_conversion(type);
7168 * Parse a cast expression.
7170 static expression_t *parse_cast(void)
7172 add_anchor_token(')');
7174 source_position_t source_position = token.source_position;
7176 type_t *type = parse_typename();
7178 rem_anchor_token(')');
7179 expect(')', end_error);
7181 if (token.type == '{') {
7182 return parse_compound_literal(type);
7185 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
7186 cast->base.source_position = source_position;
7188 expression_t *value = parse_sub_expression(PREC_CAST);
7189 cast->base.type = type;
7190 cast->unary.value = value;
7192 if (! semantic_cast(cast)) {
7193 /* TODO: record the error in the AST. else it is impossible to detect it */
7198 return create_invalid_expression();
7202 * Parse a statement expression.
7204 static expression_t *parse_statement_expression(void)
7206 add_anchor_token(')');
7208 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
7210 statement_t *statement = parse_compound_statement(true);
7211 statement->compound.stmt_expr = true;
7212 expression->statement.statement = statement;
7214 /* find last statement and use its type */
7215 type_t *type = type_void;
7216 const statement_t *stmt = statement->compound.statements;
7218 while (stmt->base.next != NULL)
7219 stmt = stmt->base.next;
7221 if (stmt->kind == STATEMENT_EXPRESSION) {
7222 type = stmt->expression.expression->base.type;
7224 } else if (warning.other) {
7225 warningf(&expression->base.source_position, "empty statement expression ({})");
7227 expression->base.type = type;
7229 rem_anchor_token(')');
7230 expect(')', end_error);
7237 * Parse a parenthesized expression.
7239 static expression_t *parse_parenthesized_expression(void)
7243 switch (token.type) {
7245 /* gcc extension: a statement expression */
7246 return parse_statement_expression();
7250 return parse_cast();
7252 if (is_typedef_symbol(token.v.symbol)) {
7253 return parse_cast();
7257 add_anchor_token(')');
7258 expression_t *result = parse_expression();
7259 result->base.parenthesized = true;
7260 rem_anchor_token(')');
7261 expect(')', end_error);
7267 static expression_t *parse_function_keyword(void)
7271 if (current_function == NULL) {
7272 errorf(HERE, "'__func__' used outside of a function");
7275 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
7276 expression->base.type = type_char_ptr;
7277 expression->funcname.kind = FUNCNAME_FUNCTION;
7284 static expression_t *parse_pretty_function_keyword(void)
7286 if (current_function == NULL) {
7287 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
7290 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
7291 expression->base.type = type_char_ptr;
7292 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
7294 eat(T___PRETTY_FUNCTION__);
7299 static expression_t *parse_funcsig_keyword(void)
7301 if (current_function == NULL) {
7302 errorf(HERE, "'__FUNCSIG__' used outside of a function");
7305 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
7306 expression->base.type = type_char_ptr;
7307 expression->funcname.kind = FUNCNAME_FUNCSIG;
7314 static expression_t *parse_funcdname_keyword(void)
7316 if (current_function == NULL) {
7317 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
7320 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
7321 expression->base.type = type_char_ptr;
7322 expression->funcname.kind = FUNCNAME_FUNCDNAME;
7324 eat(T___FUNCDNAME__);
7329 static designator_t *parse_designator(void)
7331 designator_t *result = allocate_ast_zero(sizeof(result[0]));
7332 result->source_position = *HERE;
7334 if (token.type != T_IDENTIFIER) {
7335 parse_error_expected("while parsing member designator",
7336 T_IDENTIFIER, NULL);
7339 result->symbol = token.v.symbol;
7342 designator_t *last_designator = result;
7344 if (token.type == '.') {
7346 if (token.type != T_IDENTIFIER) {
7347 parse_error_expected("while parsing member designator",
7348 T_IDENTIFIER, NULL);
7351 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
7352 designator->source_position = *HERE;
7353 designator->symbol = token.v.symbol;
7356 last_designator->next = designator;
7357 last_designator = designator;
7360 if (token.type == '[') {
7362 add_anchor_token(']');
7363 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
7364 designator->source_position = *HERE;
7365 designator->array_index = parse_expression();
7366 rem_anchor_token(']');
7367 expect(']', end_error);
7368 if (designator->array_index == NULL) {
7372 last_designator->next = designator;
7373 last_designator = designator;
7385 * Parse the __builtin_offsetof() expression.
7387 static expression_t *parse_offsetof(void)
7389 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
7390 expression->base.type = type_size_t;
7392 eat(T___builtin_offsetof);
7394 expect('(', end_error);
7395 add_anchor_token(',');
7396 type_t *type = parse_typename();
7397 rem_anchor_token(',');
7398 expect(',', end_error);
7399 add_anchor_token(')');
7400 designator_t *designator = parse_designator();
7401 rem_anchor_token(')');
7402 expect(')', end_error);
7404 expression->offsetofe.type = type;
7405 expression->offsetofe.designator = designator;
7408 memset(&path, 0, sizeof(path));
7409 path.top_type = type;
7410 path.path = NEW_ARR_F(type_path_entry_t, 0);
7412 descend_into_subtype(&path);
7414 if (!walk_designator(&path, designator, true)) {
7415 return create_invalid_expression();
7418 DEL_ARR_F(path.path);
7422 return create_invalid_expression();
7426 * Parses a _builtin_va_start() expression.
7428 static expression_t *parse_va_start(void)
7430 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
7432 eat(T___builtin_va_start);
7434 expect('(', end_error);
7435 add_anchor_token(',');
7436 expression->va_starte.ap = parse_assignment_expression();
7437 rem_anchor_token(',');
7438 expect(',', end_error);
7439 expression_t *const expr = parse_assignment_expression();
7440 if (expr->kind == EXPR_REFERENCE) {
7441 entity_t *const entity = expr->reference.entity;
7442 if (entity->base.parent_scope != ¤t_function->parameters
7443 || entity->base.next != NULL
7444 || entity->kind != ENTITY_PARAMETER) {
7445 errorf(&expr->base.source_position,
7446 "second argument of 'va_start' must be last parameter of the current function");
7448 expression->va_starte.parameter = &entity->variable;
7450 expect(')', end_error);
7453 expect(')', end_error);
7455 return create_invalid_expression();
7459 * Parses a _builtin_va_arg() expression.
7461 static expression_t *parse_va_arg(void)
7463 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
7465 eat(T___builtin_va_arg);
7467 expect('(', end_error);
7468 expression->va_arge.ap = parse_assignment_expression();
7469 expect(',', end_error);
7470 expression->base.type = parse_typename();
7471 expect(')', end_error);
7475 return create_invalid_expression();
7479 * Parses a __builtin_constant_p() expression.
7481 static expression_t *parse_builtin_constant(void)
7483 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
7485 eat(T___builtin_constant_p);
7487 expect('(', end_error);
7488 add_anchor_token(')');
7489 expression->builtin_constant.value = parse_assignment_expression();
7490 rem_anchor_token(')');
7491 expect(')', end_error);
7492 expression->base.type = type_int;
7496 return create_invalid_expression();
7500 * Parses a __builtin_types_compatible_p() expression.
7502 static expression_t *parse_builtin_types_compatible(void)
7504 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_TYPES_COMPATIBLE_P);
7506 eat(T___builtin_types_compatible_p);
7508 expect('(', end_error);
7509 add_anchor_token(')');
7510 add_anchor_token(',');
7511 expression->builtin_types_compatible.left = parse_typename();
7512 rem_anchor_token(',');
7513 expect(',', end_error);
7514 expression->builtin_types_compatible.right = parse_typename();
7515 rem_anchor_token(')');
7516 expect(')', end_error);
7517 expression->base.type = type_int;
7521 return create_invalid_expression();
7525 * Parses a __builtin_is_*() compare expression.
7527 static expression_t *parse_compare_builtin(void)
7529 expression_t *expression;
7531 switch (token.type) {
7532 case T___builtin_isgreater:
7533 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
7535 case T___builtin_isgreaterequal:
7536 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
7538 case T___builtin_isless:
7539 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
7541 case T___builtin_islessequal:
7542 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
7544 case T___builtin_islessgreater:
7545 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
7547 case T___builtin_isunordered:
7548 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
7551 internal_errorf(HERE, "invalid compare builtin found");
7553 expression->base.source_position = *HERE;
7556 expect('(', end_error);
7557 expression->binary.left = parse_assignment_expression();
7558 expect(',', end_error);
7559 expression->binary.right = parse_assignment_expression();
7560 expect(')', end_error);
7562 type_t *const orig_type_left = expression->binary.left->base.type;
7563 type_t *const orig_type_right = expression->binary.right->base.type;
7565 type_t *const type_left = skip_typeref(orig_type_left);
7566 type_t *const type_right = skip_typeref(orig_type_right);
7567 if (!is_type_float(type_left) && !is_type_float(type_right)) {
7568 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7569 type_error_incompatible("invalid operands in comparison",
7570 &expression->base.source_position, orig_type_left, orig_type_right);
7573 semantic_comparison(&expression->binary);
7578 return create_invalid_expression();
7583 * Parses a __builtin_expect(, end_error) expression.
7585 static expression_t *parse_builtin_expect(void, end_error)
7587 expression_t *expression
7588 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
7590 eat(T___builtin_expect);
7592 expect('(', end_error);
7593 expression->binary.left = parse_assignment_expression();
7594 expect(',', end_error);
7595 expression->binary.right = parse_constant_expression();
7596 expect(')', end_error);
7598 expression->base.type = expression->binary.left->base.type;
7602 return create_invalid_expression();
7607 * Parses a MS assume() expression.
7609 static expression_t *parse_assume(void)
7611 expression_t *expression = allocate_expression_zero(EXPR_UNARY_ASSUME);
7615 expect('(', end_error);
7616 add_anchor_token(')');
7617 expression->unary.value = parse_assignment_expression();
7618 rem_anchor_token(')');
7619 expect(')', end_error);
7621 expression->base.type = type_void;
7624 return create_invalid_expression();
7628 * Return the declaration for a given label symbol or create a new one.
7630 * @param symbol the symbol of the label
7632 static label_t *get_label(symbol_t *symbol)
7635 assert(current_function != NULL);
7637 label = get_entity(symbol, NAMESPACE_LABEL);
7638 /* if we found a local label, we already created the declaration */
7639 if (label != NULL && label->kind == ENTITY_LOCAL_LABEL) {
7640 if (label->base.parent_scope != current_scope) {
7641 assert(label->base.parent_scope->depth < current_scope->depth);
7642 current_function->goto_to_outer = true;
7644 return &label->label;
7647 label = get_entity(symbol, NAMESPACE_LABEL);
7648 /* if we found a label in the same function, then we already created the
7651 && label->base.parent_scope == ¤t_function->parameters) {
7652 return &label->label;
7655 /* otherwise we need to create a new one */
7656 label = allocate_entity_zero(ENTITY_LABEL);
7657 label->base.namespc = NAMESPACE_LABEL;
7658 label->base.symbol = symbol;
7662 return &label->label;
7666 * Parses a GNU && label address expression.
7668 static expression_t *parse_label_address(void)
7670 source_position_t source_position = token.source_position;
7672 if (token.type != T_IDENTIFIER) {
7673 parse_error_expected("while parsing label address", T_IDENTIFIER, NULL);
7676 symbol_t *symbol = token.v.symbol;
7679 label_t *label = get_label(symbol);
7681 label->address_taken = true;
7683 expression_t *expression = allocate_expression_zero(EXPR_LABEL_ADDRESS);
7684 expression->base.source_position = source_position;
7686 /* label address is threaten as a void pointer */
7687 expression->base.type = type_void_ptr;
7688 expression->label_address.label = label;
7691 return create_invalid_expression();
7695 * Parse a microsoft __noop expression.
7697 static expression_t *parse_noop_expression(void)
7699 /* the result is a (int)0 */
7700 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
7701 cnst->base.type = type_int;
7702 cnst->conste.v.int_value = 0;
7703 cnst->conste.is_ms_noop = true;
7707 if (token.type == '(') {
7708 /* parse arguments */
7710 add_anchor_token(')');
7711 add_anchor_token(',');
7713 if (token.type != ')') {
7715 (void)parse_assignment_expression();
7716 if (token.type != ',')
7722 rem_anchor_token(',');
7723 rem_anchor_token(')');
7724 expect(')', end_error);
7731 * Parses a primary expression.
7733 static expression_t *parse_primary_expression(void)
7735 switch (token.type) {
7736 case T_false: return parse_bool_const(false);
7737 case T_true: return parse_bool_const(true);
7738 case T_INTEGER: return parse_int_const();
7739 case T_CHARACTER_CONSTANT: return parse_character_constant();
7740 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
7741 case T_FLOATINGPOINT: return parse_float_const();
7742 case T_STRING_LITERAL:
7743 case T_WIDE_STRING_LITERAL: return parse_string_const();
7744 case T_IDENTIFIER: return parse_reference();
7745 case T___FUNCTION__:
7746 case T___func__: return parse_function_keyword();
7747 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
7748 case T___FUNCSIG__: return parse_funcsig_keyword();
7749 case T___FUNCDNAME__: return parse_funcdname_keyword();
7750 case T___builtin_offsetof: return parse_offsetof();
7751 case T___builtin_va_start: return parse_va_start();
7752 case T___builtin_va_arg: return parse_va_arg();
7753 case T___builtin_isgreater:
7754 case T___builtin_isgreaterequal:
7755 case T___builtin_isless:
7756 case T___builtin_islessequal:
7757 case T___builtin_islessgreater:
7758 case T___builtin_isunordered: return parse_compare_builtin();
7759 case T___builtin_constant_p: return parse_builtin_constant();
7760 case T___builtin_types_compatible_p: return parse_builtin_types_compatible();
7761 case T__assume: return parse_assume();
7764 return parse_label_address();
7767 case '(': return parse_parenthesized_expression();
7768 case T___noop: return parse_noop_expression();
7771 errorf(HERE, "unexpected token %K, expected an expression", &token);
7772 return create_invalid_expression();
7776 * Check if the expression has the character type and issue a warning then.
7778 static void check_for_char_index_type(const expression_t *expression)
7780 type_t *const type = expression->base.type;
7781 const type_t *const base_type = skip_typeref(type);
7783 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
7784 warning.char_subscripts) {
7785 warningf(&expression->base.source_position,
7786 "array subscript has type '%T'", type);
7790 static expression_t *parse_array_expression(expression_t *left)
7792 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
7795 add_anchor_token(']');
7797 expression_t *inside = parse_expression();
7799 type_t *const orig_type_left = left->base.type;
7800 type_t *const orig_type_inside = inside->base.type;
7802 type_t *const type_left = skip_typeref(orig_type_left);
7803 type_t *const type_inside = skip_typeref(orig_type_inside);
7805 type_t *return_type;
7806 array_access_expression_t *array_access = &expression->array_access;
7807 if (is_type_pointer(type_left)) {
7808 return_type = type_left->pointer.points_to;
7809 array_access->array_ref = left;
7810 array_access->index = inside;
7811 check_for_char_index_type(inside);
7812 } else if (is_type_pointer(type_inside)) {
7813 return_type = type_inside->pointer.points_to;
7814 array_access->array_ref = inside;
7815 array_access->index = left;
7816 array_access->flipped = true;
7817 check_for_char_index_type(left);
7819 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
7821 "array access on object with non-pointer types '%T', '%T'",
7822 orig_type_left, orig_type_inside);
7824 return_type = type_error_type;
7825 array_access->array_ref = left;
7826 array_access->index = inside;
7829 expression->base.type = automatic_type_conversion(return_type);
7831 rem_anchor_token(']');
7832 expect(']', end_error);
7837 static expression_t *parse_typeprop(expression_kind_t const kind)
7839 expression_t *tp_expression = allocate_expression_zero(kind);
7840 tp_expression->base.type = type_size_t;
7842 eat(kind == EXPR_SIZEOF ? T_sizeof : T___alignof__);
7844 /* we only refer to a type property, mark this case */
7845 bool old = in_type_prop;
7846 in_type_prop = true;
7849 expression_t *expression;
7850 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
7852 add_anchor_token(')');
7853 orig_type = parse_typename();
7854 rem_anchor_token(')');
7855 expect(')', end_error);
7857 if (token.type == '{') {
7858 /* It was not sizeof(type) after all. It is sizeof of an expression
7859 * starting with a compound literal */
7860 expression = parse_compound_literal(orig_type);
7861 goto typeprop_expression;
7864 expression = parse_sub_expression(PREC_UNARY);
7866 typeprop_expression:
7867 tp_expression->typeprop.tp_expression = expression;
7869 orig_type = revert_automatic_type_conversion(expression);
7870 expression->base.type = orig_type;
7873 tp_expression->typeprop.type = orig_type;
7874 type_t const* const type = skip_typeref(orig_type);
7875 char const* const wrong_type =
7876 GNU_MODE && is_type_atomic(type, ATOMIC_TYPE_VOID) ? NULL :
7877 is_type_incomplete(type) ? "incomplete" :
7878 type->kind == TYPE_FUNCTION ? "function designator" :
7879 type->kind == TYPE_BITFIELD ? "bitfield" :
7881 if (wrong_type != NULL) {
7882 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
7883 errorf(&tp_expression->base.source_position,
7884 "operand of %s expression must not be of %s type '%T'",
7885 what, wrong_type, orig_type);
7890 return tp_expression;
7893 static expression_t *parse_sizeof(void)
7895 return parse_typeprop(EXPR_SIZEOF);
7898 static expression_t *parse_alignof(void)
7900 return parse_typeprop(EXPR_ALIGNOF);
7903 static expression_t *parse_select_expression(expression_t *compound)
7905 expression_t *select = allocate_expression_zero(EXPR_SELECT);
7906 select->select.compound = compound;
7908 assert(token.type == '.' || token.type == T_MINUSGREATER);
7909 bool is_pointer = (token.type == T_MINUSGREATER);
7912 if (token.type != T_IDENTIFIER) {
7913 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
7916 symbol_t *symbol = token.v.symbol;
7919 type_t *const orig_type = compound->base.type;
7920 type_t *const type = skip_typeref(orig_type);
7923 bool saw_error = false;
7924 if (is_type_pointer(type)) {
7927 "request for member '%Y' in something not a struct or union, but '%T'",
7931 type_left = skip_typeref(type->pointer.points_to);
7933 if (is_pointer && is_type_valid(type)) {
7934 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
7941 if (type_left->kind == TYPE_COMPOUND_STRUCT ||
7942 type_left->kind == TYPE_COMPOUND_UNION) {
7943 compound_t *compound = type_left->compound.compound;
7945 if (!compound->complete) {
7946 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
7948 goto create_error_entry;
7951 entry = find_compound_entry(compound, symbol);
7952 if (entry == NULL) {
7953 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
7954 goto create_error_entry;
7957 if (is_type_valid(type_left) && !saw_error) {
7959 "request for member '%Y' in something not a struct or union, but '%T'",
7963 entry = create_error_entity(symbol, ENTITY_COMPOUND_MEMBER);
7966 assert(is_declaration(entry));
7967 select->select.compound_entry = entry;
7969 type_t *entry_type = entry->declaration.type;
7971 = get_qualified_type(entry_type, type_left->base.qualifiers);
7973 /* we always do the auto-type conversions; the & and sizeof parser contains
7974 * code to revert this! */
7975 select->base.type = automatic_type_conversion(res_type);
7977 type_t *skipped = skip_typeref(res_type);
7978 if (skipped->kind == TYPE_BITFIELD) {
7979 select->base.type = skipped->bitfield.base_type;
7985 static void check_call_argument(const function_parameter_t *parameter,
7986 call_argument_t *argument, unsigned pos)
7988 type_t *expected_type = parameter->type;
7989 type_t *expected_type_skip = skip_typeref(expected_type);
7990 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
7991 expression_t *arg_expr = argument->expression;
7992 type_t *arg_type = skip_typeref(arg_expr->base.type);
7994 /* handle transparent union gnu extension */
7995 if (is_type_union(expected_type_skip)
7996 && (expected_type_skip->base.modifiers
7997 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
7998 compound_t *union_decl = expected_type_skip->compound.compound;
7999 type_t *best_type = NULL;
8000 entity_t *entry = union_decl->members.entities;
8001 for ( ; entry != NULL; entry = entry->base.next) {
8002 assert(is_declaration(entry));
8003 type_t *decl_type = entry->declaration.type;
8004 error = semantic_assign(decl_type, arg_expr);
8005 if (error == ASSIGN_ERROR_INCOMPATIBLE
8006 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
8009 if (error == ASSIGN_SUCCESS) {
8010 best_type = decl_type;
8011 } else if (best_type == NULL) {
8012 best_type = decl_type;
8016 if (best_type != NULL) {
8017 expected_type = best_type;
8021 error = semantic_assign(expected_type, arg_expr);
8022 argument->expression = create_implicit_cast(argument->expression,
8025 if (error != ASSIGN_SUCCESS) {
8026 /* report exact scope in error messages (like "in argument 3") */
8028 snprintf(buf, sizeof(buf), "call argument %u", pos);
8029 report_assign_error(error, expected_type, arg_expr, buf,
8030 &arg_expr->base.source_position);
8031 } else if (warning.traditional || warning.conversion) {
8032 type_t *const promoted_type = get_default_promoted_type(arg_type);
8033 if (!types_compatible(expected_type_skip, promoted_type) &&
8034 !types_compatible(expected_type_skip, type_void_ptr) &&
8035 !types_compatible(type_void_ptr, promoted_type)) {
8036 /* Deliberately show the skipped types in this warning */
8037 warningf(&arg_expr->base.source_position,
8038 "passing call argument %u as '%T' rather than '%T' due to prototype",
8039 pos, expected_type_skip, promoted_type);
8045 * Handle the semantic restrictions of builtin calls
8047 static void handle_builtin_argument_restrictions(call_expression_t *call) {
8048 switch (call->function->reference.entity->function.btk) {
8049 case bk_gnu_builtin_return_address:
8050 case bk_gnu_builtin_frame_address: {
8051 /* argument must be constant */
8052 call_argument_t *argument = call->arguments;
8054 if (! is_constant_expression(argument->expression)) {
8055 errorf(&call->base.source_position,
8056 "argument of '%Y' must be a constant expression",
8057 call->function->reference.entity->base.symbol);
8061 case bk_gnu_builtin_prefetch: {
8062 /* second and third argument must be constant if existent */
8063 call_argument_t *rw = call->arguments->next;
8064 call_argument_t *locality = NULL;
8067 if (! is_constant_expression(rw->expression)) {
8068 errorf(&call->base.source_position,
8069 "second argument of '%Y' must be a constant expression",
8070 call->function->reference.entity->base.symbol);
8072 locality = rw->next;
8074 if (locality != NULL) {
8075 if (! is_constant_expression(locality->expression)) {
8076 errorf(&call->base.source_position,
8077 "third argument of '%Y' must be a constant expression",
8078 call->function->reference.entity->base.symbol);
8080 locality = rw->next;
8090 * Parse a call expression, ie. expression '( ... )'.
8092 * @param expression the function address
8094 static expression_t *parse_call_expression(expression_t *expression)
8096 expression_t *result = allocate_expression_zero(EXPR_CALL);
8097 call_expression_t *call = &result->call;
8098 call->function = expression;
8100 type_t *const orig_type = expression->base.type;
8101 type_t *const type = skip_typeref(orig_type);
8103 function_type_t *function_type = NULL;
8104 if (is_type_pointer(type)) {
8105 type_t *const to_type = skip_typeref(type->pointer.points_to);
8107 if (is_type_function(to_type)) {
8108 function_type = &to_type->function;
8109 call->base.type = function_type->return_type;
8113 if (function_type == NULL && is_type_valid(type)) {
8114 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
8117 /* parse arguments */
8119 add_anchor_token(')');
8120 add_anchor_token(',');
8122 if (token.type != ')') {
8123 call_argument_t *last_argument = NULL;
8126 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8128 argument->expression = parse_assignment_expression();
8129 if (last_argument == NULL) {
8130 call->arguments = argument;
8132 last_argument->next = argument;
8134 last_argument = argument;
8136 if (token.type != ',')
8141 rem_anchor_token(',');
8142 rem_anchor_token(')');
8143 expect(')', end_error);
8145 if (function_type == NULL)
8148 function_parameter_t *parameter = function_type->parameters;
8149 call_argument_t *argument = call->arguments;
8150 if (!function_type->unspecified_parameters) {
8151 for (unsigned pos = 0; parameter != NULL && argument != NULL;
8152 parameter = parameter->next, argument = argument->next) {
8153 check_call_argument(parameter, argument, ++pos);
8156 if (parameter != NULL) {
8157 errorf(HERE, "too few arguments to function '%E'", expression);
8158 } else if (argument != NULL && !function_type->variadic) {
8159 errorf(HERE, "too many arguments to function '%E'", expression);
8163 /* do default promotion */
8164 for (; argument != NULL; argument = argument->next) {
8165 type_t *type = argument->expression->base.type;
8167 type = get_default_promoted_type(type);
8169 argument->expression
8170 = create_implicit_cast(argument->expression, type);
8173 check_format(&result->call);
8175 if (warning.aggregate_return &&
8176 is_type_compound(skip_typeref(function_type->return_type))) {
8177 warningf(&result->base.source_position,
8178 "function call has aggregate value");
8181 if (call->function->kind == EXPR_REFERENCE) {
8182 reference_expression_t *reference = &call->function->reference;
8183 if (reference->entity->kind == ENTITY_FUNCTION &&
8184 reference->entity->function.btk != bk_none)
8185 handle_builtin_argument_restrictions(call);
8192 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
8194 static bool same_compound_type(const type_t *type1, const type_t *type2)
8197 is_type_compound(type1) &&
8198 type1->kind == type2->kind &&
8199 type1->compound.compound == type2->compound.compound;
8202 static expression_t const *get_reference_address(expression_t const *expr)
8204 bool regular_take_address = true;
8206 if (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
8207 expr = expr->unary.value;
8209 regular_take_address = false;
8212 if (expr->kind != EXPR_UNARY_DEREFERENCE)
8215 expr = expr->unary.value;
8218 if (expr->kind != EXPR_REFERENCE)
8221 /* special case for functions which are automatically converted to a
8222 * pointer to function without an extra TAKE_ADDRESS operation */
8223 if (!regular_take_address &&
8224 expr->reference.entity->kind != ENTITY_FUNCTION) {
8231 static void warn_reference_address_as_bool(expression_t const* expr)
8233 if (!warning.address)
8236 expr = get_reference_address(expr);
8238 warningf(&expr->base.source_position,
8239 "the address of '%Y' will always evaluate as 'true'",
8240 expr->reference.entity->base.symbol);
8244 static void warn_assignment_in_condition(const expression_t *const expr)
8246 if (!warning.parentheses)
8248 if (expr->base.kind != EXPR_BINARY_ASSIGN)
8250 if (expr->base.parenthesized)
8252 warningf(&expr->base.source_position,
8253 "suggest parentheses around assignment used as truth value");
8256 static void semantic_condition(expression_t const *const expr,
8257 char const *const context)
8259 type_t *const type = skip_typeref(expr->base.type);
8260 if (is_type_scalar(type)) {
8261 warn_reference_address_as_bool(expr);
8262 warn_assignment_in_condition(expr);
8263 } else if (is_type_valid(type)) {
8264 errorf(&expr->base.source_position,
8265 "%s must have scalar type", context);
8270 * Parse a conditional expression, ie. 'expression ? ... : ...'.
8272 * @param expression the conditional expression
8274 static expression_t *parse_conditional_expression(expression_t *expression)
8276 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
8278 conditional_expression_t *conditional = &result->conditional;
8279 conditional->condition = expression;
8282 add_anchor_token(':');
8284 /* §6.5.15:2 The first operand shall have scalar type. */
8285 semantic_condition(expression, "condition of conditional operator");
8287 expression_t *true_expression = expression;
8288 bool gnu_cond = false;
8289 if (GNU_MODE && token.type == ':') {
8292 true_expression = parse_expression();
8294 rem_anchor_token(':');
8295 expect(':', end_error);
8297 expression_t *false_expression =
8298 parse_sub_expression(c_mode & _CXX ? PREC_ASSIGNMENT : PREC_CONDITIONAL);
8300 type_t *const orig_true_type = true_expression->base.type;
8301 type_t *const orig_false_type = false_expression->base.type;
8302 type_t *const true_type = skip_typeref(orig_true_type);
8303 type_t *const false_type = skip_typeref(orig_false_type);
8306 type_t *result_type;
8307 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
8308 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
8309 /* ISO/IEC 14882:1998(E) §5.16:2 */
8310 if (true_expression->kind == EXPR_UNARY_THROW) {
8311 result_type = false_type;
8312 } else if (false_expression->kind == EXPR_UNARY_THROW) {
8313 result_type = true_type;
8315 if (warning.other && (
8316 !is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
8317 !is_type_atomic(false_type, ATOMIC_TYPE_VOID)
8319 warningf(&conditional->base.source_position,
8320 "ISO C forbids conditional expression with only one void side");
8322 result_type = type_void;
8324 } else if (is_type_arithmetic(true_type)
8325 && is_type_arithmetic(false_type)) {
8326 result_type = semantic_arithmetic(true_type, false_type);
8328 true_expression = create_implicit_cast(true_expression, result_type);
8329 false_expression = create_implicit_cast(false_expression, result_type);
8331 conditional->true_expression = true_expression;
8332 conditional->false_expression = false_expression;
8333 conditional->base.type = result_type;
8334 } else if (same_compound_type(true_type, false_type)) {
8335 /* just take 1 of the 2 types */
8336 result_type = true_type;
8337 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
8338 type_t *pointer_type;
8340 expression_t *other_expression;
8341 if (is_type_pointer(true_type) &&
8342 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
8343 pointer_type = true_type;
8344 other_type = false_type;
8345 other_expression = false_expression;
8347 pointer_type = false_type;
8348 other_type = true_type;
8349 other_expression = true_expression;
8352 if (is_null_pointer_constant(other_expression)) {
8353 result_type = pointer_type;
8354 } else if (is_type_pointer(other_type)) {
8355 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
8356 type_t *to2 = skip_typeref(other_type->pointer.points_to);
8359 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
8360 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
8362 } else if (types_compatible(get_unqualified_type(to1),
8363 get_unqualified_type(to2))) {
8366 if (warning.other) {
8367 warningf(&conditional->base.source_position,
8368 "pointer types '%T' and '%T' in conditional expression are incompatible",
8369 true_type, false_type);
8374 type_t *const type =
8375 get_qualified_type(to, to1->base.qualifiers | to2->base.qualifiers);
8376 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
8377 } else if (is_type_integer(other_type)) {
8378 if (warning.other) {
8379 warningf(&conditional->base.source_position,
8380 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
8382 result_type = pointer_type;
8384 if (is_type_valid(other_type)) {
8385 type_error_incompatible("while parsing conditional",
8386 &expression->base.source_position, true_type, false_type);
8388 result_type = type_error_type;
8391 if (is_type_valid(true_type) && is_type_valid(false_type)) {
8392 type_error_incompatible("while parsing conditional",
8393 &conditional->base.source_position, true_type,
8396 result_type = type_error_type;
8399 conditional->true_expression
8400 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
8401 conditional->false_expression
8402 = create_implicit_cast(false_expression, result_type);
8403 conditional->base.type = result_type;
8408 * Parse an extension expression.
8410 static expression_t *parse_extension(void)
8412 eat(T___extension__);
8414 bool old_gcc_extension = in_gcc_extension;
8415 in_gcc_extension = true;
8416 expression_t *expression = parse_sub_expression(PREC_UNARY);
8417 in_gcc_extension = old_gcc_extension;
8422 * Parse a __builtin_classify_type() expression.
8424 static expression_t *parse_builtin_classify_type(void)
8426 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
8427 result->base.type = type_int;
8429 eat(T___builtin_classify_type);
8431 expect('(', end_error);
8432 add_anchor_token(')');
8433 expression_t *expression = parse_expression();
8434 rem_anchor_token(')');
8435 expect(')', end_error);
8436 result->classify_type.type_expression = expression;
8440 return create_invalid_expression();
8444 * Parse a delete expression
8445 * ISO/IEC 14882:1998(E) §5.3.5
8447 static expression_t *parse_delete(void)
8449 expression_t *const result = allocate_expression_zero(EXPR_UNARY_DELETE);
8450 result->base.type = type_void;
8454 if (token.type == '[') {
8456 result->kind = EXPR_UNARY_DELETE_ARRAY;
8457 expect(']', end_error);
8461 expression_t *const value = parse_sub_expression(PREC_CAST);
8462 result->unary.value = value;
8464 type_t *const type = skip_typeref(value->base.type);
8465 if (!is_type_pointer(type)) {
8466 if (is_type_valid(type)) {
8467 errorf(&value->base.source_position,
8468 "operand of delete must have pointer type");
8470 } else if (warning.other &&
8471 is_type_atomic(skip_typeref(type->pointer.points_to), ATOMIC_TYPE_VOID)) {
8472 warningf(&value->base.source_position,
8473 "deleting 'void*' is undefined");
8480 * Parse a throw expression
8481 * ISO/IEC 14882:1998(E) §15:1
8483 static expression_t *parse_throw(void)
8485 expression_t *const result = allocate_expression_zero(EXPR_UNARY_THROW);
8486 result->base.type = type_void;
8490 expression_t *value = NULL;
8491 switch (token.type) {
8493 value = parse_assignment_expression();
8494 /* ISO/IEC 14882:1998(E) §15.1:3 */
8495 type_t *const orig_type = value->base.type;
8496 type_t *const type = skip_typeref(orig_type);
8497 if (is_type_incomplete(type)) {
8498 errorf(&value->base.source_position,
8499 "cannot throw object of incomplete type '%T'", orig_type);
8500 } else if (is_type_pointer(type)) {
8501 type_t *const points_to = skip_typeref(type->pointer.points_to);
8502 if (is_type_incomplete(points_to) &&
8503 !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
8504 errorf(&value->base.source_position,
8505 "cannot throw pointer to incomplete type '%T'", orig_type);
8513 result->unary.value = value;
8518 static bool check_pointer_arithmetic(const source_position_t *source_position,
8519 type_t *pointer_type,
8520 type_t *orig_pointer_type)
8522 type_t *points_to = pointer_type->pointer.points_to;
8523 points_to = skip_typeref(points_to);
8525 if (is_type_incomplete(points_to)) {
8526 if (!GNU_MODE || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
8527 errorf(source_position,
8528 "arithmetic with pointer to incomplete type '%T' not allowed",
8531 } else if (warning.pointer_arith) {
8532 warningf(source_position,
8533 "pointer of type '%T' used in arithmetic",
8536 } else if (is_type_function(points_to)) {
8538 errorf(source_position,
8539 "arithmetic with pointer to function type '%T' not allowed",
8542 } else if (warning.pointer_arith) {
8543 warningf(source_position,
8544 "pointer to a function '%T' used in arithmetic",
8551 static bool is_lvalue(const expression_t *expression)
8553 /* TODO: doesn't seem to be consistent with §6.3.2.1:1 */
8554 switch (expression->kind) {
8555 case EXPR_ARRAY_ACCESS:
8556 case EXPR_COMPOUND_LITERAL:
8557 case EXPR_REFERENCE:
8559 case EXPR_UNARY_DEREFERENCE:
8563 type_t *type = skip_typeref(expression->base.type);
8565 /* ISO/IEC 14882:1998(E) §3.10:3 */
8566 is_type_reference(type) ||
8567 /* Claim it is an lvalue, if the type is invalid. There was a parse
8568 * error before, which maybe prevented properly recognizing it as
8570 !is_type_valid(type);
8575 static void semantic_incdec(unary_expression_t *expression)
8577 type_t *const orig_type = expression->value->base.type;
8578 type_t *const type = skip_typeref(orig_type);
8579 if (is_type_pointer(type)) {
8580 if (!check_pointer_arithmetic(&expression->base.source_position,
8584 } else if (!is_type_real(type) && is_type_valid(type)) {
8585 /* TODO: improve error message */
8586 errorf(&expression->base.source_position,
8587 "operation needs an arithmetic or pointer type");
8590 if (!is_lvalue(expression->value)) {
8591 /* TODO: improve error message */
8592 errorf(&expression->base.source_position, "lvalue required as operand");
8594 expression->base.type = orig_type;
8597 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
8599 type_t *const orig_type = expression->value->base.type;
8600 type_t *const type = skip_typeref(orig_type);
8601 if (!is_type_arithmetic(type)) {
8602 if (is_type_valid(type)) {
8603 /* TODO: improve error message */
8604 errorf(&expression->base.source_position,
8605 "operation needs an arithmetic type");
8610 expression->base.type = orig_type;
8613 static void semantic_unexpr_plus(unary_expression_t *expression)
8615 semantic_unexpr_arithmetic(expression);
8616 if (warning.traditional)
8617 warningf(&expression->base.source_position,
8618 "traditional C rejects the unary plus operator");
8621 static void semantic_not(unary_expression_t *expression)
8623 /* §6.5.3.3:1 The operand [...] of the ! operator, scalar type. */
8624 semantic_condition(expression->value, "operand of !");
8625 expression->base.type = c_mode & _CXX ? type_bool : type_int;
8628 static void semantic_unexpr_integer(unary_expression_t *expression)
8630 type_t *const orig_type = expression->value->base.type;
8631 type_t *const type = skip_typeref(orig_type);
8632 if (!is_type_integer(type)) {
8633 if (is_type_valid(type)) {
8634 errorf(&expression->base.source_position,
8635 "operand of ~ must be of integer type");
8640 expression->base.type = orig_type;
8643 static void semantic_dereference(unary_expression_t *expression)
8645 type_t *const orig_type = expression->value->base.type;
8646 type_t *const type = skip_typeref(orig_type);
8647 if (!is_type_pointer(type)) {
8648 if (is_type_valid(type)) {
8649 errorf(&expression->base.source_position,
8650 "Unary '*' needs pointer or array type, but type '%T' given", orig_type);
8655 type_t *result_type = type->pointer.points_to;
8656 result_type = automatic_type_conversion(result_type);
8657 expression->base.type = result_type;
8661 * Record that an address is taken (expression represents an lvalue).
8663 * @param expression the expression
8664 * @param may_be_register if true, the expression might be an register
8666 static void set_address_taken(expression_t *expression, bool may_be_register)
8668 if (expression->kind != EXPR_REFERENCE)
8671 entity_t *const entity = expression->reference.entity;
8673 if (entity->kind != ENTITY_VARIABLE && entity->kind != ENTITY_PARAMETER)
8676 if (entity->declaration.storage_class == STORAGE_CLASS_REGISTER
8677 && !may_be_register) {
8678 errorf(&expression->base.source_position,
8679 "address of register %s '%Y' requested",
8680 get_entity_kind_name(entity->kind), entity->base.symbol);
8683 if (entity->kind == ENTITY_VARIABLE) {
8684 entity->variable.address_taken = true;
8686 assert(entity->kind == ENTITY_PARAMETER);
8687 entity->parameter.address_taken = true;
8692 * Check the semantic of the address taken expression.
8694 static void semantic_take_addr(unary_expression_t *expression)
8696 expression_t *value = expression->value;
8697 value->base.type = revert_automatic_type_conversion(value);
8699 type_t *orig_type = value->base.type;
8700 type_t *type = skip_typeref(orig_type);
8701 if (!is_type_valid(type))
8705 if (!is_lvalue(value)) {
8706 errorf(&expression->base.source_position, "'&' requires an lvalue");
8708 if (type->kind == TYPE_BITFIELD) {
8709 errorf(&expression->base.source_position,
8710 "'&' not allowed on object with bitfield type '%T'",
8714 set_address_taken(value, false);
8716 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
8719 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
8720 static expression_t *parse_##unexpression_type(void) \
8722 expression_t *unary_expression \
8723 = allocate_expression_zero(unexpression_type); \
8725 unary_expression->unary.value = parse_sub_expression(PREC_UNARY); \
8727 sfunc(&unary_expression->unary); \
8729 return unary_expression; \
8732 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
8733 semantic_unexpr_arithmetic)
8734 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
8735 semantic_unexpr_plus)
8736 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
8738 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
8739 semantic_dereference)
8740 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
8742 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
8743 semantic_unexpr_integer)
8744 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
8746 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
8749 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
8751 static expression_t *parse_##unexpression_type(expression_t *left) \
8753 expression_t *unary_expression \
8754 = allocate_expression_zero(unexpression_type); \
8756 unary_expression->unary.value = left; \
8758 sfunc(&unary_expression->unary); \
8760 return unary_expression; \
8763 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
8764 EXPR_UNARY_POSTFIX_INCREMENT,
8766 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
8767 EXPR_UNARY_POSTFIX_DECREMENT,
8770 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
8772 /* TODO: handle complex + imaginary types */
8774 type_left = get_unqualified_type(type_left);
8775 type_right = get_unqualified_type(type_right);
8777 /* §6.3.1.8 Usual arithmetic conversions */
8778 if (type_left == type_long_double || type_right == type_long_double) {
8779 return type_long_double;
8780 } else if (type_left == type_double || type_right == type_double) {
8782 } else if (type_left == type_float || type_right == type_float) {
8786 type_left = promote_integer(type_left);
8787 type_right = promote_integer(type_right);
8789 if (type_left == type_right)
8792 bool const signed_left = is_type_signed(type_left);
8793 bool const signed_right = is_type_signed(type_right);
8794 int const rank_left = get_rank(type_left);
8795 int const rank_right = get_rank(type_right);
8797 if (signed_left == signed_right)
8798 return rank_left >= rank_right ? type_left : type_right;
8807 u_rank = rank_right;
8808 u_type = type_right;
8810 s_rank = rank_right;
8811 s_type = type_right;
8816 if (u_rank >= s_rank)
8819 /* casting rank to atomic_type_kind is a bit hacky, but makes things
8821 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
8822 > get_atomic_type_size((atomic_type_kind_t) u_rank))
8826 case ATOMIC_TYPE_INT: return type_unsigned_int;
8827 case ATOMIC_TYPE_LONG: return type_unsigned_long;
8828 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
8830 default: panic("invalid atomic type");
8835 * Check the semantic restrictions for a binary expression.
8837 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
8839 expression_t *const left = expression->left;
8840 expression_t *const right = expression->right;
8841 type_t *const orig_type_left = left->base.type;
8842 type_t *const orig_type_right = right->base.type;
8843 type_t *const type_left = skip_typeref(orig_type_left);
8844 type_t *const type_right = skip_typeref(orig_type_right);
8846 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
8847 /* TODO: improve error message */
8848 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8849 errorf(&expression->base.source_position,
8850 "operation needs arithmetic types");
8855 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8856 expression->left = create_implicit_cast(left, arithmetic_type);
8857 expression->right = create_implicit_cast(right, arithmetic_type);
8858 expression->base.type = arithmetic_type;
8861 static void warn_div_by_zero(binary_expression_t const *const expression)
8863 if (!warning.div_by_zero ||
8864 !is_type_integer(expression->base.type))
8867 expression_t const *const right = expression->right;
8868 /* The type of the right operand can be different for /= */
8869 if (is_type_integer(right->base.type) &&
8870 is_constant_expression(right) &&
8871 fold_constant(right) == 0) {
8872 warningf(&expression->base.source_position, "division by zero");
8877 * Check the semantic restrictions for a div/mod expression.
8879 static void semantic_divmod_arithmetic(binary_expression_t *expression)
8881 semantic_binexpr_arithmetic(expression);
8882 warn_div_by_zero(expression);
8885 static void warn_addsub_in_shift(const expression_t *const expr)
8887 if (expr->base.parenthesized)
8891 switch (expr->kind) {
8892 case EXPR_BINARY_ADD: op = '+'; break;
8893 case EXPR_BINARY_SUB: op = '-'; break;
8897 warningf(&expr->base.source_position,
8898 "suggest parentheses around '%c' inside shift", op);
8901 static void semantic_shift_op(binary_expression_t *expression)
8903 expression_t *const left = expression->left;
8904 expression_t *const right = expression->right;
8905 type_t *const orig_type_left = left->base.type;
8906 type_t *const orig_type_right = right->base.type;
8907 type_t * type_left = skip_typeref(orig_type_left);
8908 type_t * type_right = skip_typeref(orig_type_right);
8910 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
8911 /* TODO: improve error message */
8912 if (is_type_valid(type_left) && is_type_valid(type_right)) {
8913 errorf(&expression->base.source_position,
8914 "operands of shift operation must have integer types");
8919 if (warning.parentheses) {
8920 warn_addsub_in_shift(left);
8921 warn_addsub_in_shift(right);
8924 type_left = promote_integer(type_left);
8925 type_right = promote_integer(type_right);
8927 expression->left = create_implicit_cast(left, type_left);
8928 expression->right = create_implicit_cast(right, type_right);
8929 expression->base.type = type_left;
8932 static void semantic_add(binary_expression_t *expression)
8934 expression_t *const left = expression->left;
8935 expression_t *const right = expression->right;
8936 type_t *const orig_type_left = left->base.type;
8937 type_t *const orig_type_right = right->base.type;
8938 type_t *const type_left = skip_typeref(orig_type_left);
8939 type_t *const type_right = skip_typeref(orig_type_right);
8942 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8943 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8944 expression->left = create_implicit_cast(left, arithmetic_type);
8945 expression->right = create_implicit_cast(right, arithmetic_type);
8946 expression->base.type = arithmetic_type;
8947 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8948 check_pointer_arithmetic(&expression->base.source_position,
8949 type_left, orig_type_left);
8950 expression->base.type = type_left;
8951 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
8952 check_pointer_arithmetic(&expression->base.source_position,
8953 type_right, orig_type_right);
8954 expression->base.type = type_right;
8955 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8956 errorf(&expression->base.source_position,
8957 "invalid operands to binary + ('%T', '%T')",
8958 orig_type_left, orig_type_right);
8962 static void semantic_sub(binary_expression_t *expression)
8964 expression_t *const left = expression->left;
8965 expression_t *const right = expression->right;
8966 type_t *const orig_type_left = left->base.type;
8967 type_t *const orig_type_right = right->base.type;
8968 type_t *const type_left = skip_typeref(orig_type_left);
8969 type_t *const type_right = skip_typeref(orig_type_right);
8970 source_position_t const *const pos = &expression->base.source_position;
8973 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
8974 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
8975 expression->left = create_implicit_cast(left, arithmetic_type);
8976 expression->right = create_implicit_cast(right, arithmetic_type);
8977 expression->base.type = arithmetic_type;
8978 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
8979 check_pointer_arithmetic(&expression->base.source_position,
8980 type_left, orig_type_left);
8981 expression->base.type = type_left;
8982 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
8983 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
8984 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
8985 if (!types_compatible(unqual_left, unqual_right)) {
8987 "subtracting pointers to incompatible types '%T' and '%T'",
8988 orig_type_left, orig_type_right);
8989 } else if (!is_type_object(unqual_left)) {
8990 if (!is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
8991 errorf(pos, "subtracting pointers to non-object types '%T'",
8993 } else if (warning.other) {
8994 warningf(pos, "subtracting pointers to void");
8997 expression->base.type = type_ptrdiff_t;
8998 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
8999 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
9000 orig_type_left, orig_type_right);
9004 static void warn_string_literal_address(expression_t const* expr)
9006 while (expr->kind == EXPR_UNARY_TAKE_ADDRESS) {
9007 expr = expr->unary.value;
9008 if (expr->kind != EXPR_UNARY_DEREFERENCE)
9010 expr = expr->unary.value;
9013 if (expr->kind == EXPR_STRING_LITERAL ||
9014 expr->kind == EXPR_WIDE_STRING_LITERAL) {
9015 warningf(&expr->base.source_position,
9016 "comparison with string literal results in unspecified behaviour");
9020 static void warn_comparison_in_comparison(const expression_t *const expr)
9022 if (expr->base.parenthesized)
9024 switch (expr->base.kind) {
9025 case EXPR_BINARY_LESS:
9026 case EXPR_BINARY_GREATER:
9027 case EXPR_BINARY_LESSEQUAL:
9028 case EXPR_BINARY_GREATEREQUAL:
9029 case EXPR_BINARY_NOTEQUAL:
9030 case EXPR_BINARY_EQUAL:
9031 warningf(&expr->base.source_position,
9032 "comparisons like 'x <= y < z' do not have their mathematical meaning");
9039 static bool maybe_negative(expression_t const *const expr)
9042 !is_constant_expression(expr) ||
9043 fold_constant(expr) < 0;
9047 * Check the semantics of comparison expressions.
9049 * @param expression The expression to check.
9051 static void semantic_comparison(binary_expression_t *expression)
9053 expression_t *left = expression->left;
9054 expression_t *right = expression->right;
9056 if (warning.address) {
9057 warn_string_literal_address(left);
9058 warn_string_literal_address(right);
9060 expression_t const* const func_left = get_reference_address(left);
9061 if (func_left != NULL && is_null_pointer_constant(right)) {
9062 warningf(&expression->base.source_position,
9063 "the address of '%Y' will never be NULL",
9064 func_left->reference.entity->base.symbol);
9067 expression_t const* const func_right = get_reference_address(right);
9068 if (func_right != NULL && is_null_pointer_constant(right)) {
9069 warningf(&expression->base.source_position,
9070 "the address of '%Y' will never be NULL",
9071 func_right->reference.entity->base.symbol);
9075 if (warning.parentheses) {
9076 warn_comparison_in_comparison(left);
9077 warn_comparison_in_comparison(right);
9080 type_t *orig_type_left = left->base.type;
9081 type_t *orig_type_right = right->base.type;
9082 type_t *type_left = skip_typeref(orig_type_left);
9083 type_t *type_right = skip_typeref(orig_type_right);
9085 /* TODO non-arithmetic types */
9086 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
9087 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
9089 /* test for signed vs unsigned compares */
9090 if (warning.sign_compare && is_type_integer(arithmetic_type)) {
9091 bool const signed_left = is_type_signed(type_left);
9092 bool const signed_right = is_type_signed(type_right);
9093 if (signed_left != signed_right) {
9094 /* FIXME long long needs better const folding magic */
9095 /* TODO check whether constant value can be represented by other type */
9096 if ((signed_left && maybe_negative(left)) ||
9097 (signed_right && maybe_negative(right))) {
9098 warningf(&expression->base.source_position,
9099 "comparison between signed and unsigned");
9104 expression->left = create_implicit_cast(left, arithmetic_type);
9105 expression->right = create_implicit_cast(right, arithmetic_type);
9106 expression->base.type = arithmetic_type;
9107 if (warning.float_equal &&
9108 (expression->base.kind == EXPR_BINARY_EQUAL ||
9109 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
9110 is_type_float(arithmetic_type)) {
9111 warningf(&expression->base.source_position,
9112 "comparing floating point with == or != is unsafe");
9114 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
9115 /* TODO check compatibility */
9116 } else if (is_type_pointer(type_left)) {
9117 expression->right = create_implicit_cast(right, type_left);
9118 } else if (is_type_pointer(type_right)) {
9119 expression->left = create_implicit_cast(left, type_right);
9120 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
9121 type_error_incompatible("invalid operands in comparison",
9122 &expression->base.source_position,
9123 type_left, type_right);
9125 expression->base.type = c_mode & _CXX ? type_bool : type_int;
9129 * Checks if a compound type has constant fields.
9131 static bool has_const_fields(const compound_type_t *type)
9133 compound_t *compound = type->compound;
9134 entity_t *entry = compound->members.entities;
9136 for (; entry != NULL; entry = entry->base.next) {
9137 if (!is_declaration(entry))
9140 const type_t *decl_type = skip_typeref(entry->declaration.type);
9141 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
9148 static bool is_valid_assignment_lhs(expression_t const* const left)
9150 type_t *const orig_type_left = revert_automatic_type_conversion(left);
9151 type_t *const type_left = skip_typeref(orig_type_left);
9153 if (!is_lvalue(left)) {
9154 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
9159 if (left->kind == EXPR_REFERENCE
9160 && left->reference.entity->kind == ENTITY_FUNCTION) {
9161 errorf(HERE, "cannot assign to function '%E'", left);
9165 if (is_type_array(type_left)) {
9166 errorf(HERE, "cannot assign to array '%E'", left);
9169 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
9170 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
9174 if (is_type_incomplete(type_left)) {
9175 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
9176 left, orig_type_left);
9179 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
9180 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
9181 left, orig_type_left);
9188 static void semantic_arithmetic_assign(binary_expression_t *expression)
9190 expression_t *left = expression->left;
9191 expression_t *right = expression->right;
9192 type_t *orig_type_left = left->base.type;
9193 type_t *orig_type_right = right->base.type;
9195 if (!is_valid_assignment_lhs(left))
9198 type_t *type_left = skip_typeref(orig_type_left);
9199 type_t *type_right = skip_typeref(orig_type_right);
9201 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
9202 /* TODO: improve error message */
9203 if (is_type_valid(type_left) && is_type_valid(type_right)) {
9204 errorf(&expression->base.source_position,
9205 "operation needs arithmetic types");
9210 /* combined instructions are tricky. We can't create an implicit cast on
9211 * the left side, because we need the uncasted form for the store.
9212 * The ast2firm pass has to know that left_type must be right_type
9213 * for the arithmetic operation and create a cast by itself */
9214 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
9215 expression->right = create_implicit_cast(right, arithmetic_type);
9216 expression->base.type = type_left;
9219 static void semantic_divmod_assign(binary_expression_t *expression)
9221 semantic_arithmetic_assign(expression);
9222 warn_div_by_zero(expression);
9225 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
9227 expression_t *const left = expression->left;
9228 expression_t *const right = expression->right;
9229 type_t *const orig_type_left = left->base.type;
9230 type_t *const orig_type_right = right->base.type;
9231 type_t *const type_left = skip_typeref(orig_type_left);
9232 type_t *const type_right = skip_typeref(orig_type_right);
9234 if (!is_valid_assignment_lhs(left))
9237 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
9238 /* combined instructions are tricky. We can't create an implicit cast on
9239 * the left side, because we need the uncasted form for the store.
9240 * The ast2firm pass has to know that left_type must be right_type
9241 * for the arithmetic operation and create a cast by itself */
9242 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
9243 expression->right = create_implicit_cast(right, arithmetic_type);
9244 expression->base.type = type_left;
9245 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
9246 check_pointer_arithmetic(&expression->base.source_position,
9247 type_left, orig_type_left);
9248 expression->base.type = type_left;
9249 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
9250 errorf(&expression->base.source_position,
9251 "incompatible types '%T' and '%T' in assignment",
9252 orig_type_left, orig_type_right);
9256 static void warn_logical_and_within_or(const expression_t *const expr)
9258 if (expr->base.kind != EXPR_BINARY_LOGICAL_AND)
9260 if (expr->base.parenthesized)
9262 warningf(&expr->base.source_position,
9263 "suggest parentheses around && within ||");
9267 * Check the semantic restrictions of a logical expression.
9269 static void semantic_logical_op(binary_expression_t *expression)
9271 /* §6.5.13:2 Each of the operands shall have scalar type.
9272 * §6.5.14:2 Each of the operands shall have scalar type. */
9273 semantic_condition(expression->left, "left operand of logical operator");
9274 semantic_condition(expression->right, "right operand of logical operator");
9275 if (expression->base.kind == EXPR_BINARY_LOGICAL_OR &&
9276 warning.parentheses) {
9277 warn_logical_and_within_or(expression->left);
9278 warn_logical_and_within_or(expression->right);
9280 expression->base.type = c_mode & _CXX ? type_bool : type_int;
9284 * Check the semantic restrictions of a binary assign expression.
9286 static void semantic_binexpr_assign(binary_expression_t *expression)
9288 expression_t *left = expression->left;
9289 type_t *orig_type_left = left->base.type;
9291 if (!is_valid_assignment_lhs(left))
9294 assign_error_t error = semantic_assign(orig_type_left, expression->right);
9295 report_assign_error(error, orig_type_left, expression->right,
9296 "assignment", &left->base.source_position);
9297 expression->right = create_implicit_cast(expression->right, orig_type_left);
9298 expression->base.type = orig_type_left;
9302 * Determine if the outermost operation (or parts thereof) of the given
9303 * expression has no effect in order to generate a warning about this fact.
9304 * Therefore in some cases this only examines some of the operands of the
9305 * expression (see comments in the function and examples below).
9307 * f() + 23; // warning, because + has no effect
9308 * x || f(); // no warning, because x controls execution of f()
9309 * x ? y : f(); // warning, because y has no effect
9310 * (void)x; // no warning to be able to suppress the warning
9311 * This function can NOT be used for an "expression has definitely no effect"-
9313 static bool expression_has_effect(const expression_t *const expr)
9315 switch (expr->kind) {
9316 case EXPR_UNKNOWN: break;
9317 case EXPR_INVALID: return true; /* do NOT warn */
9318 case EXPR_REFERENCE: return false;
9319 case EXPR_REFERENCE_ENUM_VALUE: return false;
9320 /* suppress the warning for microsoft __noop operations */
9321 case EXPR_CONST: return expr->conste.is_ms_noop;
9322 case EXPR_CHARACTER_CONSTANT: return false;
9323 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
9324 case EXPR_STRING_LITERAL: return false;
9325 case EXPR_WIDE_STRING_LITERAL: return false;
9326 case EXPR_LABEL_ADDRESS: return false;
9329 const call_expression_t *const call = &expr->call;
9330 if (call->function->kind != EXPR_REFERENCE)
9333 switch (call->function->reference.entity->function.btk) {
9334 /* FIXME: which builtins have no effect? */
9335 default: return true;
9339 /* Generate the warning if either the left or right hand side of a
9340 * conditional expression has no effect */
9341 case EXPR_CONDITIONAL: {
9342 conditional_expression_t const *const cond = &expr->conditional;
9343 expression_t const *const t = cond->true_expression;
9345 (t == NULL || expression_has_effect(t)) &&
9346 expression_has_effect(cond->false_expression);
9349 case EXPR_SELECT: return false;
9350 case EXPR_ARRAY_ACCESS: return false;
9351 case EXPR_SIZEOF: return false;
9352 case EXPR_CLASSIFY_TYPE: return false;
9353 case EXPR_ALIGNOF: return false;
9355 case EXPR_FUNCNAME: return false;
9356 case EXPR_BUILTIN_CONSTANT_P: return false;
9357 case EXPR_BUILTIN_TYPES_COMPATIBLE_P: return false;
9358 case EXPR_OFFSETOF: return false;
9359 case EXPR_VA_START: return true;
9360 case EXPR_VA_ARG: return true;
9361 case EXPR_STATEMENT: return true; // TODO
9362 case EXPR_COMPOUND_LITERAL: return false;
9364 case EXPR_UNARY_NEGATE: return false;
9365 case EXPR_UNARY_PLUS: return false;
9366 case EXPR_UNARY_BITWISE_NEGATE: return false;
9367 case EXPR_UNARY_NOT: return false;
9368 case EXPR_UNARY_DEREFERENCE: return false;
9369 case EXPR_UNARY_TAKE_ADDRESS: return false;
9370 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
9371 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
9372 case EXPR_UNARY_PREFIX_INCREMENT: return true;
9373 case EXPR_UNARY_PREFIX_DECREMENT: return true;
9375 /* Treat void casts as if they have an effect in order to being able to
9376 * suppress the warning */
9377 case EXPR_UNARY_CAST: {
9378 type_t *const type = skip_typeref(expr->base.type);
9379 return is_type_atomic(type, ATOMIC_TYPE_VOID);
9382 case EXPR_UNARY_CAST_IMPLICIT: return true;
9383 case EXPR_UNARY_ASSUME: return true;
9384 case EXPR_UNARY_DELETE: return true;
9385 case EXPR_UNARY_DELETE_ARRAY: return true;
9386 case EXPR_UNARY_THROW: return true;
9388 case EXPR_BINARY_ADD: return false;
9389 case EXPR_BINARY_SUB: return false;
9390 case EXPR_BINARY_MUL: return false;
9391 case EXPR_BINARY_DIV: return false;
9392 case EXPR_BINARY_MOD: return false;
9393 case EXPR_BINARY_EQUAL: return false;
9394 case EXPR_BINARY_NOTEQUAL: return false;
9395 case EXPR_BINARY_LESS: return false;
9396 case EXPR_BINARY_LESSEQUAL: return false;
9397 case EXPR_BINARY_GREATER: return false;
9398 case EXPR_BINARY_GREATEREQUAL: return false;
9399 case EXPR_BINARY_BITWISE_AND: return false;
9400 case EXPR_BINARY_BITWISE_OR: return false;
9401 case EXPR_BINARY_BITWISE_XOR: return false;
9402 case EXPR_BINARY_SHIFTLEFT: return false;
9403 case EXPR_BINARY_SHIFTRIGHT: return false;
9404 case EXPR_BINARY_ASSIGN: return true;
9405 case EXPR_BINARY_MUL_ASSIGN: return true;
9406 case EXPR_BINARY_DIV_ASSIGN: return true;
9407 case EXPR_BINARY_MOD_ASSIGN: return true;
9408 case EXPR_BINARY_ADD_ASSIGN: return true;
9409 case EXPR_BINARY_SUB_ASSIGN: return true;
9410 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
9411 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
9412 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
9413 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
9414 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
9416 /* Only examine the right hand side of && and ||, because the left hand
9417 * side already has the effect of controlling the execution of the right
9419 case EXPR_BINARY_LOGICAL_AND:
9420 case EXPR_BINARY_LOGICAL_OR:
9421 /* Only examine the right hand side of a comma expression, because the left
9422 * hand side has a separate warning */
9423 case EXPR_BINARY_COMMA:
9424 return expression_has_effect(expr->binary.right);
9426 case EXPR_BINARY_ISGREATER: return false;
9427 case EXPR_BINARY_ISGREATEREQUAL: return false;
9428 case EXPR_BINARY_ISLESS: return false;
9429 case EXPR_BINARY_ISLESSEQUAL: return false;
9430 case EXPR_BINARY_ISLESSGREATER: return false;
9431 case EXPR_BINARY_ISUNORDERED: return false;
9434 internal_errorf(HERE, "unexpected expression");
9437 static void semantic_comma(binary_expression_t *expression)
9439 if (warning.unused_value) {
9440 const expression_t *const left = expression->left;
9441 if (!expression_has_effect(left)) {
9442 warningf(&left->base.source_position,
9443 "left-hand operand of comma expression has no effect");
9446 expression->base.type = expression->right->base.type;
9450 * @param prec_r precedence of the right operand
9452 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, prec_r, sfunc) \
9453 static expression_t *parse_##binexpression_type(expression_t *left) \
9455 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
9456 binexpr->binary.left = left; \
9459 expression_t *right = parse_sub_expression(prec_r); \
9461 binexpr->binary.right = right; \
9462 sfunc(&binexpr->binary); \
9467 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, PREC_CAST, semantic_binexpr_arithmetic)
9468 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, PREC_CAST, semantic_divmod_arithmetic)
9469 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, PREC_CAST, semantic_divmod_arithmetic)
9470 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, PREC_MULTIPLICATIVE, semantic_add)
9471 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, PREC_MULTIPLICATIVE, semantic_sub)
9472 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT, PREC_ADDITIVE, semantic_shift_op)
9473 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT, PREC_ADDITIVE, semantic_shift_op)
9474 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, PREC_SHIFT, semantic_comparison)
9475 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, PREC_SHIFT, semantic_comparison)
9476 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL, PREC_SHIFT, semantic_comparison)
9477 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL, PREC_SHIFT, semantic_comparison)
9478 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL, PREC_RELATIONAL, semantic_comparison)
9479 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL, PREC_RELATIONAL, semantic_comparison)
9480 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND, PREC_EQUALITY, semantic_binexpr_arithmetic)
9481 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR, PREC_AND, semantic_binexpr_arithmetic)
9482 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR, PREC_XOR, semantic_binexpr_arithmetic)
9483 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND, PREC_OR, semantic_logical_op)
9484 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR, PREC_LOGICAL_AND, semantic_logical_op)
9485 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, PREC_ASSIGNMENT, semantic_binexpr_assign)
9486 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
9487 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_addsubb_assign)
9488 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9489 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
9490 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN, PREC_ASSIGNMENT, semantic_divmod_assign)
9491 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9492 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9493 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9494 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9495 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN, PREC_ASSIGNMENT, semantic_arithmetic_assign)
9496 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, PREC_ASSIGNMENT, semantic_comma)
9499 static expression_t *parse_sub_expression(precedence_t precedence)
9501 if (token.type < 0) {
9502 return expected_expression_error();
9505 expression_parser_function_t *parser
9506 = &expression_parsers[token.type];
9507 source_position_t source_position = token.source_position;
9510 if (parser->parser != NULL) {
9511 left = parser->parser();
9513 left = parse_primary_expression();
9515 assert(left != NULL);
9516 left->base.source_position = source_position;
9519 if (token.type < 0) {
9520 return expected_expression_error();
9523 parser = &expression_parsers[token.type];
9524 if (parser->infix_parser == NULL)
9526 if (parser->infix_precedence < precedence)
9529 left = parser->infix_parser(left);
9531 assert(left != NULL);
9532 assert(left->kind != EXPR_UNKNOWN);
9533 left->base.source_position = source_position;
9540 * Parse an expression.
9542 static expression_t *parse_expression(void)
9544 return parse_sub_expression(PREC_EXPRESSION);
9548 * Register a parser for a prefix-like operator.
9550 * @param parser the parser function
9551 * @param token_type the token type of the prefix token
9553 static void register_expression_parser(parse_expression_function parser,
9556 expression_parser_function_t *entry = &expression_parsers[token_type];
9558 if (entry->parser != NULL) {
9559 diagnosticf("for token '%k'\n", (token_type_t)token_type);
9560 panic("trying to register multiple expression parsers for a token");
9562 entry->parser = parser;
9566 * Register a parser for an infix operator with given precedence.
9568 * @param parser the parser function
9569 * @param token_type the token type of the infix operator
9570 * @param precedence the precedence of the operator
9572 static void register_infix_parser(parse_expression_infix_function parser,
9573 int token_type, precedence_t precedence)
9575 expression_parser_function_t *entry = &expression_parsers[token_type];
9577 if (entry->infix_parser != NULL) {
9578 diagnosticf("for token '%k'\n", (token_type_t)token_type);
9579 panic("trying to register multiple infix expression parsers for a "
9582 entry->infix_parser = parser;
9583 entry->infix_precedence = precedence;
9587 * Initialize the expression parsers.
9589 static void init_expression_parsers(void)
9591 memset(&expression_parsers, 0, sizeof(expression_parsers));
9593 register_infix_parser(parse_array_expression, '[', PREC_POSTFIX);
9594 register_infix_parser(parse_call_expression, '(', PREC_POSTFIX);
9595 register_infix_parser(parse_select_expression, '.', PREC_POSTFIX);
9596 register_infix_parser(parse_select_expression, T_MINUSGREATER, PREC_POSTFIX);
9597 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT, T_PLUSPLUS, PREC_POSTFIX);
9598 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT, T_MINUSMINUS, PREC_POSTFIX);
9599 register_infix_parser(parse_EXPR_BINARY_MUL, '*', PREC_MULTIPLICATIVE);
9600 register_infix_parser(parse_EXPR_BINARY_DIV, '/', PREC_MULTIPLICATIVE);
9601 register_infix_parser(parse_EXPR_BINARY_MOD, '%', PREC_MULTIPLICATIVE);
9602 register_infix_parser(parse_EXPR_BINARY_ADD, '+', PREC_ADDITIVE);
9603 register_infix_parser(parse_EXPR_BINARY_SUB, '-', PREC_ADDITIVE);
9604 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, PREC_SHIFT);
9605 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, PREC_SHIFT);
9606 register_infix_parser(parse_EXPR_BINARY_LESS, '<', PREC_RELATIONAL);
9607 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', PREC_RELATIONAL);
9608 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, PREC_RELATIONAL);
9609 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, PREC_RELATIONAL);
9610 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, PREC_EQUALITY);
9611 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL, T_EXCLAMATIONMARKEQUAL, PREC_EQUALITY);
9612 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', PREC_AND);
9613 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', PREC_XOR);
9614 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', PREC_OR);
9615 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, PREC_LOGICAL_AND);
9616 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, PREC_LOGICAL_OR);
9617 register_infix_parser(parse_conditional_expression, '?', PREC_CONDITIONAL);
9618 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', PREC_ASSIGNMENT);
9619 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, PREC_ASSIGNMENT);
9620 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, PREC_ASSIGNMENT);
9621 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, PREC_ASSIGNMENT);
9622 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, PREC_ASSIGNMENT);
9623 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, PREC_ASSIGNMENT);
9624 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN, T_LESSLESSEQUAL, PREC_ASSIGNMENT);
9625 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN, T_GREATERGREATEREQUAL, PREC_ASSIGNMENT);
9626 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN, T_ANDEQUAL, PREC_ASSIGNMENT);
9627 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN, T_PIPEEQUAL, PREC_ASSIGNMENT);
9628 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN, T_CARETEQUAL, PREC_ASSIGNMENT);
9629 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', PREC_EXPRESSION);
9631 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-');
9632 register_expression_parser(parse_EXPR_UNARY_PLUS, '+');
9633 register_expression_parser(parse_EXPR_UNARY_NOT, '!');
9634 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~');
9635 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*');
9636 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&');
9637 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT, T_PLUSPLUS);
9638 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT, T_MINUSMINUS);
9639 register_expression_parser(parse_sizeof, T_sizeof);
9640 register_expression_parser(parse_alignof, T___alignof__);
9641 register_expression_parser(parse_extension, T___extension__);
9642 register_expression_parser(parse_builtin_classify_type, T___builtin_classify_type);
9643 register_expression_parser(parse_delete, T_delete);
9644 register_expression_parser(parse_throw, T_throw);
9648 * Parse a asm statement arguments specification.
9650 static asm_argument_t *parse_asm_arguments(bool is_out)
9652 asm_argument_t *result = NULL;
9653 asm_argument_t **anchor = &result;
9655 while (token.type == T_STRING_LITERAL || token.type == '[') {
9656 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
9657 memset(argument, 0, sizeof(argument[0]));
9659 if (token.type == '[') {
9661 if (token.type != T_IDENTIFIER) {
9662 parse_error_expected("while parsing asm argument",
9663 T_IDENTIFIER, NULL);
9666 argument->symbol = token.v.symbol;
9668 expect(']', end_error);
9671 argument->constraints = parse_string_literals();
9672 expect('(', end_error);
9673 add_anchor_token(')');
9674 expression_t *expression = parse_expression();
9675 rem_anchor_token(')');
9677 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
9678 * change size or type representation (e.g. int -> long is ok, but
9679 * int -> float is not) */
9680 if (expression->kind == EXPR_UNARY_CAST) {
9681 type_t *const type = expression->base.type;
9682 type_kind_t const kind = type->kind;
9683 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
9686 if (kind == TYPE_ATOMIC) {
9687 atomic_type_kind_t const akind = type->atomic.akind;
9688 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
9689 size = get_atomic_type_size(akind);
9691 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
9692 size = get_atomic_type_size(get_intptr_kind());
9696 expression_t *const value = expression->unary.value;
9697 type_t *const value_type = value->base.type;
9698 type_kind_t const value_kind = value_type->kind;
9700 unsigned value_flags;
9701 unsigned value_size;
9702 if (value_kind == TYPE_ATOMIC) {
9703 atomic_type_kind_t const value_akind = value_type->atomic.akind;
9704 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
9705 value_size = get_atomic_type_size(value_akind);
9706 } else if (value_kind == TYPE_POINTER) {
9707 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
9708 value_size = get_atomic_type_size(get_intptr_kind());
9713 if (value_flags != flags || value_size != size)
9717 } while (expression->kind == EXPR_UNARY_CAST);
9721 if (!is_lvalue(expression)) {
9722 errorf(&expression->base.source_position,
9723 "asm output argument is not an lvalue");
9726 if (argument->constraints.begin[0] == '+')
9727 mark_vars_read(expression, NULL);
9729 mark_vars_read(expression, NULL);
9731 argument->expression = expression;
9732 expect(')', end_error);
9734 set_address_taken(expression, true);
9737 anchor = &argument->next;
9739 if (token.type != ',')
9750 * Parse a asm statement clobber specification.
9752 static asm_clobber_t *parse_asm_clobbers(void)
9754 asm_clobber_t *result = NULL;
9755 asm_clobber_t *last = NULL;
9757 while (token.type == T_STRING_LITERAL) {
9758 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
9759 clobber->clobber = parse_string_literals();
9762 last->next = clobber;
9768 if (token.type != ',')
9777 * Parse an asm statement.
9779 static statement_t *parse_asm_statement(void)
9781 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
9782 asm_statement_t *asm_statement = &statement->asms;
9786 if (token.type == T_volatile) {
9788 asm_statement->is_volatile = true;
9791 expect('(', end_error);
9792 add_anchor_token(')');
9793 add_anchor_token(':');
9794 asm_statement->asm_text = parse_string_literals();
9796 if (token.type != ':') {
9797 rem_anchor_token(':');
9802 asm_statement->outputs = parse_asm_arguments(true);
9803 if (token.type != ':') {
9804 rem_anchor_token(':');
9809 asm_statement->inputs = parse_asm_arguments(false);
9810 if (token.type != ':') {
9811 rem_anchor_token(':');
9814 rem_anchor_token(':');
9817 asm_statement->clobbers = parse_asm_clobbers();
9820 rem_anchor_token(')');
9821 expect(')', end_error);
9822 expect(';', end_error);
9824 if (asm_statement->outputs == NULL) {
9825 /* GCC: An 'asm' instruction without any output operands will be treated
9826 * identically to a volatile 'asm' instruction. */
9827 asm_statement->is_volatile = true;
9832 return create_invalid_statement();
9836 * Parse a case statement.
9838 static statement_t *parse_case_statement(void)
9840 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9841 source_position_t *const pos = &statement->base.source_position;
9845 expression_t *const expression = parse_expression();
9846 statement->case_label.expression = expression;
9847 if (!is_constant_expression(expression)) {
9848 /* This check does not prevent the error message in all cases of an
9849 * prior error while parsing the expression. At least it catches the
9850 * common case of a mistyped enum entry. */
9851 if (is_type_valid(skip_typeref(expression->base.type))) {
9852 errorf(pos, "case label does not reduce to an integer constant");
9854 statement->case_label.is_bad = true;
9856 long const val = fold_constant(expression);
9857 statement->case_label.first_case = val;
9858 statement->case_label.last_case = val;
9862 if (token.type == T_DOTDOTDOT) {
9864 expression_t *const end_range = parse_expression();
9865 statement->case_label.end_range = end_range;
9866 if (!is_constant_expression(end_range)) {
9867 /* This check does not prevent the error message in all cases of an
9868 * prior error while parsing the expression. At least it catches the
9869 * common case of a mistyped enum entry. */
9870 if (is_type_valid(skip_typeref(end_range->base.type))) {
9871 errorf(pos, "case range does not reduce to an integer constant");
9873 statement->case_label.is_bad = true;
9875 long const val = fold_constant(end_range);
9876 statement->case_label.last_case = val;
9878 if (warning.other && val < statement->case_label.first_case) {
9879 statement->case_label.is_empty_range = true;
9880 warningf(pos, "empty range specified");
9886 PUSH_PARENT(statement);
9888 expect(':', end_error);
9891 if (current_switch != NULL) {
9892 if (! statement->case_label.is_bad) {
9893 /* Check for duplicate case values */
9894 case_label_statement_t *c = &statement->case_label;
9895 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
9896 if (l->is_bad || l->is_empty_range || l->expression == NULL)
9899 if (c->last_case < l->first_case || c->first_case > l->last_case)
9902 errorf(pos, "duplicate case value (previously used %P)",
9903 &l->base.source_position);
9907 /* link all cases into the switch statement */
9908 if (current_switch->last_case == NULL) {
9909 current_switch->first_case = &statement->case_label;
9911 current_switch->last_case->next = &statement->case_label;
9913 current_switch->last_case = &statement->case_label;
9915 errorf(pos, "case label not within a switch statement");
9918 statement_t *const inner_stmt = parse_statement();
9919 statement->case_label.statement = inner_stmt;
9920 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9921 errorf(&inner_stmt->base.source_position, "declaration after case label");
9929 * Parse a default statement.
9931 static statement_t *parse_default_statement(void)
9933 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
9937 PUSH_PARENT(statement);
9939 expect(':', end_error);
9940 if (current_switch != NULL) {
9941 const case_label_statement_t *def_label = current_switch->default_label;
9942 if (def_label != NULL) {
9943 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
9944 &def_label->base.source_position);
9946 current_switch->default_label = &statement->case_label;
9948 /* link all cases into the switch statement */
9949 if (current_switch->last_case == NULL) {
9950 current_switch->first_case = &statement->case_label;
9952 current_switch->last_case->next = &statement->case_label;
9954 current_switch->last_case = &statement->case_label;
9957 errorf(&statement->base.source_position,
9958 "'default' label not within a switch statement");
9961 statement_t *const inner_stmt = parse_statement();
9962 statement->case_label.statement = inner_stmt;
9963 if (inner_stmt->kind == STATEMENT_DECLARATION) {
9964 errorf(&inner_stmt->base.source_position, "declaration after default label");
9971 return create_invalid_statement();
9975 * Parse a label statement.
9977 static statement_t *parse_label_statement(void)
9979 assert(token.type == T_IDENTIFIER);
9980 symbol_t *symbol = token.v.symbol;
9981 label_t *label = get_label(symbol);
9983 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
9984 statement->label.label = label;
9988 PUSH_PARENT(statement);
9990 /* if statement is already set then the label is defined twice,
9991 * otherwise it was just mentioned in a goto/local label declaration so far
9993 if (label->statement != NULL) {
9994 errorf(HERE, "duplicate label '%Y' (declared %P)",
9995 symbol, &label->base.source_position);
9997 label->base.source_position = token.source_position;
9998 label->statement = statement;
10003 if (token.type == '}') {
10004 /* TODO only warn? */
10005 if (warning.other && false) {
10006 warningf(HERE, "label at end of compound statement");
10007 statement->label.statement = create_empty_statement();
10009 errorf(HERE, "label at end of compound statement");
10010 statement->label.statement = create_invalid_statement();
10012 } else if (token.type == ';') {
10013 /* Eat an empty statement here, to avoid the warning about an empty
10014 * statement after a label. label:; is commonly used to have a label
10015 * before a closing brace. */
10016 statement->label.statement = create_empty_statement();
10019 statement_t *const inner_stmt = parse_statement();
10020 statement->label.statement = inner_stmt;
10021 if (inner_stmt->kind == STATEMENT_DECLARATION) {
10022 errorf(&inner_stmt->base.source_position, "declaration after label");
10026 /* remember the labels in a list for later checking */
10027 *label_anchor = &statement->label;
10028 label_anchor = &statement->label.next;
10035 * Parse an if statement.
10037 static statement_t *parse_if(void)
10039 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
10043 PUSH_PARENT(statement);
10045 add_anchor_token('{');
10047 expect('(', end_error);
10048 add_anchor_token(')');
10049 expression_t *const expr = parse_expression();
10050 statement->ifs.condition = expr;
10051 /* §6.8.4.1:1 The controlling expression of an if statement shall have
10053 semantic_condition(expr, "condition of 'if'-statment");
10054 mark_vars_read(expr, NULL);
10055 rem_anchor_token(')');
10056 expect(')', end_error);
10059 rem_anchor_token('{');
10061 add_anchor_token(T_else);
10062 statement_t *const true_stmt = parse_statement();
10063 statement->ifs.true_statement = true_stmt;
10064 rem_anchor_token(T_else);
10066 if (token.type == T_else) {
10068 statement->ifs.false_statement = parse_statement();
10069 } else if (warning.parentheses &&
10070 true_stmt->kind == STATEMENT_IF &&
10071 true_stmt->ifs.false_statement != NULL) {
10072 warningf(&true_stmt->base.source_position,
10073 "suggest explicit braces to avoid ambiguous 'else'");
10081 * Check that all enums are handled in a switch.
10083 * @param statement the switch statement to check
10085 static void check_enum_cases(const switch_statement_t *statement)
10087 const type_t *type = skip_typeref(statement->expression->base.type);
10088 if (! is_type_enum(type))
10090 const enum_type_t *enumt = &type->enumt;
10092 /* if we have a default, no warnings */
10093 if (statement->default_label != NULL)
10096 /* FIXME: calculation of value should be done while parsing */
10097 /* TODO: quadratic algorithm here. Change to an n log n one */
10098 long last_value = -1;
10099 const entity_t *entry = enumt->enume->base.next;
10100 for (; entry != NULL && entry->kind == ENTITY_ENUM_VALUE;
10101 entry = entry->base.next) {
10102 const expression_t *expression = entry->enum_value.value;
10103 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
10104 bool found = false;
10105 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
10106 if (l->expression == NULL)
10108 if (l->first_case <= value && value <= l->last_case) {
10114 warningf(&statement->base.source_position,
10115 "enumeration value '%Y' not handled in switch",
10116 entry->base.symbol);
10118 last_value = value;
10123 * Parse a switch statement.
10125 static statement_t *parse_switch(void)
10127 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
10131 PUSH_PARENT(statement);
10133 expect('(', end_error);
10134 add_anchor_token(')');
10135 expression_t *const expr = parse_expression();
10136 mark_vars_read(expr, NULL);
10137 type_t * type = skip_typeref(expr->base.type);
10138 if (is_type_integer(type)) {
10139 type = promote_integer(type);
10140 if (warning.traditional) {
10141 if (get_rank(type) >= get_akind_rank(ATOMIC_TYPE_LONG)) {
10142 warningf(&expr->base.source_position,
10143 "'%T' switch expression not converted to '%T' in ISO C",
10147 } else if (is_type_valid(type)) {
10148 errorf(&expr->base.source_position,
10149 "switch quantity is not an integer, but '%T'", type);
10150 type = type_error_type;
10152 statement->switchs.expression = create_implicit_cast(expr, type);
10153 expect(')', end_error);
10154 rem_anchor_token(')');
10156 switch_statement_t *rem = current_switch;
10157 current_switch = &statement->switchs;
10158 statement->switchs.body = parse_statement();
10159 current_switch = rem;
10161 if (warning.switch_default &&
10162 statement->switchs.default_label == NULL) {
10163 warningf(&statement->base.source_position, "switch has no default case");
10165 if (warning.switch_enum)
10166 check_enum_cases(&statement->switchs);
10172 return create_invalid_statement();
10175 static statement_t *parse_loop_body(statement_t *const loop)
10177 statement_t *const rem = current_loop;
10178 current_loop = loop;
10180 statement_t *const body = parse_statement();
10182 current_loop = rem;
10187 * Parse a while statement.
10189 static statement_t *parse_while(void)
10191 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
10195 PUSH_PARENT(statement);
10197 expect('(', end_error);
10198 add_anchor_token(')');
10199 expression_t *const cond = parse_expression();
10200 statement->whiles.condition = cond;
10201 /* §6.8.5:2 The controlling expression of an iteration statement shall
10202 * have scalar type. */
10203 semantic_condition(cond, "condition of 'while'-statement");
10204 mark_vars_read(cond, NULL);
10205 rem_anchor_token(')');
10206 expect(')', end_error);
10208 statement->whiles.body = parse_loop_body(statement);
10214 return create_invalid_statement();
10218 * Parse a do statement.
10220 static statement_t *parse_do(void)
10222 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
10226 PUSH_PARENT(statement);
10228 add_anchor_token(T_while);
10229 statement->do_while.body = parse_loop_body(statement);
10230 rem_anchor_token(T_while);
10232 expect(T_while, end_error);
10233 expect('(', end_error);
10234 add_anchor_token(')');
10235 expression_t *const cond = parse_expression();
10236 statement->do_while.condition = cond;
10237 /* §6.8.5:2 The controlling expression of an iteration statement shall
10238 * have scalar type. */
10239 semantic_condition(cond, "condition of 'do-while'-statement");
10240 mark_vars_read(cond, NULL);
10241 rem_anchor_token(')');
10242 expect(')', end_error);
10243 expect(';', end_error);
10249 return create_invalid_statement();
10253 * Parse a for statement.
10255 static statement_t *parse_for(void)
10257 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
10261 expect('(', end_error1);
10262 add_anchor_token(')');
10264 PUSH_PARENT(statement);
10266 size_t const top = environment_top();
10267 scope_t *old_scope = scope_push(&statement->fors.scope);
10269 if (token.type == ';') {
10271 } else if (is_declaration_specifier(&token, false)) {
10272 parse_declaration(record_entity, DECL_FLAGS_NONE);
10274 add_anchor_token(';');
10275 expression_t *const init = parse_expression();
10276 statement->fors.initialisation = init;
10277 mark_vars_read(init, ENT_ANY);
10278 if (warning.unused_value && !expression_has_effect(init)) {
10279 warningf(&init->base.source_position,
10280 "initialisation of 'for'-statement has no effect");
10282 rem_anchor_token(';');
10283 expect(';', end_error2);
10286 if (token.type != ';') {
10287 add_anchor_token(';');
10288 expression_t *const cond = parse_expression();
10289 statement->fors.condition = cond;
10290 /* §6.8.5:2 The controlling expression of an iteration statement
10291 * shall have scalar type. */
10292 semantic_condition(cond, "condition of 'for'-statement");
10293 mark_vars_read(cond, NULL);
10294 rem_anchor_token(';');
10296 expect(';', end_error2);
10297 if (token.type != ')') {
10298 expression_t *const step = parse_expression();
10299 statement->fors.step = step;
10300 mark_vars_read(step, ENT_ANY);
10301 if (warning.unused_value && !expression_has_effect(step)) {
10302 warningf(&step->base.source_position,
10303 "step of 'for'-statement has no effect");
10306 expect(')', end_error2);
10307 rem_anchor_token(')');
10308 statement->fors.body = parse_loop_body(statement);
10310 assert(current_scope == &statement->fors.scope);
10311 scope_pop(old_scope);
10312 environment_pop_to(top);
10319 rem_anchor_token(')');
10320 assert(current_scope == &statement->fors.scope);
10321 scope_pop(old_scope);
10322 environment_pop_to(top);
10326 return create_invalid_statement();
10330 * Parse a goto statement.
10332 static statement_t *parse_goto(void)
10334 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
10337 if (GNU_MODE && token.type == '*') {
10339 expression_t *expression = parse_expression();
10340 mark_vars_read(expression, NULL);
10342 /* Argh: although documentation says the expression must be of type void*,
10343 * gcc accepts anything that can be casted into void* without error */
10344 type_t *type = expression->base.type;
10346 if (type != type_error_type) {
10347 if (!is_type_pointer(type) && !is_type_integer(type)) {
10348 errorf(&expression->base.source_position,
10349 "cannot convert to a pointer type");
10350 } else if (warning.other && type != type_void_ptr) {
10351 warningf(&expression->base.source_position,
10352 "type of computed goto expression should be 'void*' not '%T'", type);
10354 expression = create_implicit_cast(expression, type_void_ptr);
10357 statement->gotos.expression = expression;
10359 if (token.type != T_IDENTIFIER) {
10361 parse_error_expected("while parsing goto", T_IDENTIFIER, '*', NULL);
10363 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
10364 eat_until_anchor();
10367 symbol_t *symbol = token.v.symbol;
10370 statement->gotos.label = get_label(symbol);
10373 /* remember the goto's in a list for later checking */
10374 *goto_anchor = &statement->gotos;
10375 goto_anchor = &statement->gotos.next;
10377 expect(';', end_error);
10381 return create_invalid_statement();
10385 * Parse a continue statement.
10387 static statement_t *parse_continue(void)
10389 if (current_loop == NULL) {
10390 errorf(HERE, "continue statement not within loop");
10393 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
10396 expect(';', end_error);
10403 * Parse a break statement.
10405 static statement_t *parse_break(void)
10407 if (current_switch == NULL && current_loop == NULL) {
10408 errorf(HERE, "break statement not within loop or switch");
10411 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
10414 expect(';', end_error);
10421 * Parse a __leave statement.
10423 static statement_t *parse_leave_statement(void)
10425 if (current_try == NULL) {
10426 errorf(HERE, "__leave statement not within __try");
10429 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
10432 expect(';', end_error);
10439 * Check if a given entity represents a local variable.
10441 static bool is_local_variable(const entity_t *entity)
10443 if (entity->kind != ENTITY_VARIABLE)
10446 switch ((storage_class_tag_t) entity->declaration.storage_class) {
10447 case STORAGE_CLASS_AUTO:
10448 case STORAGE_CLASS_REGISTER: {
10449 const type_t *type = skip_typeref(entity->declaration.type);
10450 if (is_type_function(type)) {
10462 * Check if a given expression represents a local variable.
10464 static bool expression_is_local_variable(const expression_t *expression)
10466 if (expression->base.kind != EXPR_REFERENCE) {
10469 const entity_t *entity = expression->reference.entity;
10470 return is_local_variable(entity);
10474 * Check if a given expression represents a local variable and
10475 * return its declaration then, else return NULL.
10477 entity_t *expression_is_variable(const expression_t *expression)
10479 if (expression->base.kind != EXPR_REFERENCE) {
10482 entity_t *entity = expression->reference.entity;
10483 if (entity->kind != ENTITY_VARIABLE)
10490 * Parse a return statement.
10492 static statement_t *parse_return(void)
10496 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
10498 expression_t *return_value = NULL;
10499 if (token.type != ';') {
10500 return_value = parse_expression();
10501 mark_vars_read(return_value, NULL);
10504 const type_t *const func_type = skip_typeref(current_function->base.type);
10505 assert(is_type_function(func_type));
10506 type_t *const return_type = skip_typeref(func_type->function.return_type);
10508 source_position_t const *const pos = &statement->base.source_position;
10509 if (return_value != NULL) {
10510 type_t *return_value_type = skip_typeref(return_value->base.type);
10512 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
10513 if (is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
10514 /* ISO/IEC 14882:1998(E) §6.6.3:2 */
10515 /* Only warn in C mode, because GCC does the same */
10516 if (c_mode & _CXX || strict_mode) {
10518 "'return' with a value, in function returning 'void'");
10519 } else if (warning.other) {
10521 "'return' with a value, in function returning 'void'");
10523 } else if (!(c_mode & _CXX)) { /* ISO/IEC 14882:1998(E) §6.6.3:3 */
10524 /* Only warn in C mode, because GCC does the same */
10527 "'return' with expression in function return 'void'");
10528 } else if (warning.other) {
10530 "'return' with expression in function return 'void'");
10534 assign_error_t error = semantic_assign(return_type, return_value);
10535 report_assign_error(error, return_type, return_value, "'return'",
10538 return_value = create_implicit_cast(return_value, return_type);
10539 /* check for returning address of a local var */
10540 if (warning.other && return_value != NULL
10541 && return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
10542 const expression_t *expression = return_value->unary.value;
10543 if (expression_is_local_variable(expression)) {
10544 warningf(pos, "function returns address of local variable");
10547 } else if (warning.other && !is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
10548 /* ISO/IEC 14882:1998(E) §6.6.3:3 */
10549 if (c_mode & _CXX || strict_mode) {
10551 "'return' without value, in function returning non-void");
10554 "'return' without value, in function returning non-void");
10557 statement->returns.value = return_value;
10559 expect(';', end_error);
10566 * Parse a declaration statement.
10568 static statement_t *parse_declaration_statement(void)
10570 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
10572 entity_t *before = current_scope->last_entity;
10574 parse_external_declaration();
10576 parse_declaration(record_entity, DECL_FLAGS_NONE);
10579 declaration_statement_t *const decl = &statement->declaration;
10580 entity_t *const begin =
10581 before != NULL ? before->base.next : current_scope->entities;
10582 decl->declarations_begin = begin;
10583 decl->declarations_end = begin != NULL ? current_scope->last_entity : NULL;
10589 * Parse an expression statement, ie. expr ';'.
10591 static statement_t *parse_expression_statement(void)
10593 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
10595 expression_t *const expr = parse_expression();
10596 statement->expression.expression = expr;
10597 mark_vars_read(expr, ENT_ANY);
10599 expect(';', end_error);
10606 * Parse a microsoft __try { } __finally { } or
10607 * __try{ } __except() { }
10609 static statement_t *parse_ms_try_statment(void)
10611 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
10614 PUSH_PARENT(statement);
10616 ms_try_statement_t *rem = current_try;
10617 current_try = &statement->ms_try;
10618 statement->ms_try.try_statement = parse_compound_statement(false);
10623 if (token.type == T___except) {
10625 expect('(', end_error);
10626 add_anchor_token(')');
10627 expression_t *const expr = parse_expression();
10628 mark_vars_read(expr, NULL);
10629 type_t * type = skip_typeref(expr->base.type);
10630 if (is_type_integer(type)) {
10631 type = promote_integer(type);
10632 } else if (is_type_valid(type)) {
10633 errorf(&expr->base.source_position,
10634 "__expect expression is not an integer, but '%T'", type);
10635 type = type_error_type;
10637 statement->ms_try.except_expression = create_implicit_cast(expr, type);
10638 rem_anchor_token(')');
10639 expect(')', end_error);
10640 statement->ms_try.final_statement = parse_compound_statement(false);
10641 } else if (token.type == T__finally) {
10643 statement->ms_try.final_statement = parse_compound_statement(false);
10645 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
10646 return create_invalid_statement();
10650 return create_invalid_statement();
10653 static statement_t *parse_empty_statement(void)
10655 if (warning.empty_statement) {
10656 warningf(HERE, "statement is empty");
10658 statement_t *const statement = create_empty_statement();
10663 static statement_t *parse_local_label_declaration(void)
10665 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
10669 entity_t *begin = NULL, *end = NULL;
10672 if (token.type != T_IDENTIFIER) {
10673 parse_error_expected("while parsing local label declaration",
10674 T_IDENTIFIER, NULL);
10677 symbol_t *symbol = token.v.symbol;
10678 entity_t *entity = get_entity(symbol, NAMESPACE_LABEL);
10679 if (entity != NULL && entity->base.parent_scope == current_scope) {
10680 errorf(HERE, "multiple definitions of '__label__ %Y' (previous definition %P)",
10681 symbol, &entity->base.source_position);
10683 entity = allocate_entity_zero(ENTITY_LOCAL_LABEL);
10685 entity->base.parent_scope = current_scope;
10686 entity->base.namespc = NAMESPACE_LABEL;
10687 entity->base.source_position = token.source_position;
10688 entity->base.symbol = symbol;
10691 end->base.next = entity;
10696 environment_push(entity);
10700 if (token.type != ',')
10706 statement->declaration.declarations_begin = begin;
10707 statement->declaration.declarations_end = end;
10711 static void parse_namespace_definition(void)
10715 entity_t *entity = NULL;
10716 symbol_t *symbol = NULL;
10718 if (token.type == T_IDENTIFIER) {
10719 symbol = token.v.symbol;
10722 entity = get_entity(symbol, NAMESPACE_NORMAL);
10723 if (entity != NULL &&
10724 entity->kind != ENTITY_NAMESPACE &&
10725 entity->base.parent_scope == current_scope) {
10726 if (!is_error_entity(entity)) {
10727 error_redefined_as_different_kind(&token.source_position,
10728 entity, ENTITY_NAMESPACE);
10734 if (entity == NULL) {
10735 entity = allocate_entity_zero(ENTITY_NAMESPACE);
10736 entity->base.symbol = symbol;
10737 entity->base.source_position = token.source_position;
10738 entity->base.namespc = NAMESPACE_NORMAL;
10739 entity->base.parent_scope = current_scope;
10742 if (token.type == '=') {
10743 /* TODO: parse namespace alias */
10744 panic("namespace alias definition not supported yet");
10747 environment_push(entity);
10748 append_entity(current_scope, entity);
10750 size_t const top = environment_top();
10751 scope_t *old_scope = scope_push(&entity->namespacee.members);
10753 expect('{', end_error);
10755 expect('}', end_error);
10758 assert(current_scope == &entity->namespacee.members);
10759 scope_pop(old_scope);
10760 environment_pop_to(top);
10764 * Parse a statement.
10765 * There's also parse_statement() which additionally checks for
10766 * "statement has no effect" warnings
10768 static statement_t *intern_parse_statement(void)
10770 statement_t *statement = NULL;
10772 /* declaration or statement */
10773 add_anchor_token(';');
10774 switch (token.type) {
10775 case T_IDENTIFIER: {
10776 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
10777 if (la1_type == ':') {
10778 statement = parse_label_statement();
10779 } else if (is_typedef_symbol(token.v.symbol)) {
10780 statement = parse_declaration_statement();
10782 /* it's an identifier, the grammar says this must be an
10783 * expression statement. However it is common that users mistype
10784 * declaration types, so we guess a bit here to improve robustness
10785 * for incorrect programs */
10786 switch (la1_type) {
10789 if (get_entity(token.v.symbol, NAMESPACE_NORMAL) != NULL)
10790 goto expression_statment;
10795 statement = parse_declaration_statement();
10799 expression_statment:
10800 statement = parse_expression_statement();
10807 case T___extension__:
10808 /* This can be a prefix to a declaration or an expression statement.
10809 * We simply eat it now and parse the rest with tail recursion. */
10812 } while (token.type == T___extension__);
10813 bool old_gcc_extension = in_gcc_extension;
10814 in_gcc_extension = true;
10815 statement = intern_parse_statement();
10816 in_gcc_extension = old_gcc_extension;
10820 statement = parse_declaration_statement();
10824 statement = parse_local_label_declaration();
10827 case ';': statement = parse_empty_statement(); break;
10828 case '{': statement = parse_compound_statement(false); break;
10829 case T___leave: statement = parse_leave_statement(); break;
10830 case T___try: statement = parse_ms_try_statment(); break;
10831 case T_asm: statement = parse_asm_statement(); break;
10832 case T_break: statement = parse_break(); break;
10833 case T_case: statement = parse_case_statement(); break;
10834 case T_continue: statement = parse_continue(); break;
10835 case T_default: statement = parse_default_statement(); break;
10836 case T_do: statement = parse_do(); break;
10837 case T_for: statement = parse_for(); break;
10838 case T_goto: statement = parse_goto(); break;
10839 case T_if: statement = parse_if(); break;
10840 case T_return: statement = parse_return(); break;
10841 case T_switch: statement = parse_switch(); break;
10842 case T_while: statement = parse_while(); break;
10845 statement = parse_expression_statement();
10849 errorf(HERE, "unexpected token %K while parsing statement", &token);
10850 statement = create_invalid_statement();
10855 rem_anchor_token(';');
10857 assert(statement != NULL
10858 && statement->base.source_position.input_name != NULL);
10864 * parse a statement and emits "statement has no effect" warning if needed
10865 * (This is really a wrapper around intern_parse_statement with check for 1
10866 * single warning. It is needed, because for statement expressions we have
10867 * to avoid the warning on the last statement)
10869 static statement_t *parse_statement(void)
10871 statement_t *statement = intern_parse_statement();
10873 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
10874 expression_t *expression = statement->expression.expression;
10875 if (!expression_has_effect(expression)) {
10876 warningf(&expression->base.source_position,
10877 "statement has no effect");
10885 * Parse a compound statement.
10887 static statement_t *parse_compound_statement(bool inside_expression_statement)
10889 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
10891 PUSH_PARENT(statement);
10894 add_anchor_token('}');
10895 /* tokens, which can start a statement */
10896 /* TODO MS, __builtin_FOO */
10897 add_anchor_token('!');
10898 add_anchor_token('&');
10899 add_anchor_token('(');
10900 add_anchor_token('*');
10901 add_anchor_token('+');
10902 add_anchor_token('-');
10903 add_anchor_token('{');
10904 add_anchor_token('~');
10905 add_anchor_token(T_CHARACTER_CONSTANT);
10906 add_anchor_token(T_COLONCOLON);
10907 add_anchor_token(T_FLOATINGPOINT);
10908 add_anchor_token(T_IDENTIFIER);
10909 add_anchor_token(T_INTEGER);
10910 add_anchor_token(T_MINUSMINUS);
10911 add_anchor_token(T_PLUSPLUS);
10912 add_anchor_token(T_STRING_LITERAL);
10913 add_anchor_token(T_WIDE_CHARACTER_CONSTANT);
10914 add_anchor_token(T_WIDE_STRING_LITERAL);
10915 add_anchor_token(T__Bool);
10916 add_anchor_token(T__Complex);
10917 add_anchor_token(T__Imaginary);
10918 add_anchor_token(T___FUNCTION__);
10919 add_anchor_token(T___PRETTY_FUNCTION__);
10920 add_anchor_token(T___alignof__);
10921 add_anchor_token(T___attribute__);
10922 add_anchor_token(T___builtin_va_start);
10923 add_anchor_token(T___extension__);
10924 add_anchor_token(T___func__);
10925 add_anchor_token(T___imag__);
10926 add_anchor_token(T___label__);
10927 add_anchor_token(T___real__);
10928 add_anchor_token(T___thread);
10929 add_anchor_token(T_asm);
10930 add_anchor_token(T_auto);
10931 add_anchor_token(T_bool);
10932 add_anchor_token(T_break);
10933 add_anchor_token(T_case);
10934 add_anchor_token(T_char);
10935 add_anchor_token(T_class);
10936 add_anchor_token(T_const);
10937 add_anchor_token(T_const_cast);
10938 add_anchor_token(T_continue);
10939 add_anchor_token(T_default);
10940 add_anchor_token(T_delete);
10941 add_anchor_token(T_double);
10942 add_anchor_token(T_do);
10943 add_anchor_token(T_dynamic_cast);
10944 add_anchor_token(T_enum);
10945 add_anchor_token(T_extern);
10946 add_anchor_token(T_false);
10947 add_anchor_token(T_float);
10948 add_anchor_token(T_for);
10949 add_anchor_token(T_goto);
10950 add_anchor_token(T_if);
10951 add_anchor_token(T_inline);
10952 add_anchor_token(T_int);
10953 add_anchor_token(T_long);
10954 add_anchor_token(T_new);
10955 add_anchor_token(T_operator);
10956 add_anchor_token(T_register);
10957 add_anchor_token(T_reinterpret_cast);
10958 add_anchor_token(T_restrict);
10959 add_anchor_token(T_return);
10960 add_anchor_token(T_short);
10961 add_anchor_token(T_signed);
10962 add_anchor_token(T_sizeof);
10963 add_anchor_token(T_static);
10964 add_anchor_token(T_static_cast);
10965 add_anchor_token(T_struct);
10966 add_anchor_token(T_switch);
10967 add_anchor_token(T_template);
10968 add_anchor_token(T_this);
10969 add_anchor_token(T_throw);
10970 add_anchor_token(T_true);
10971 add_anchor_token(T_try);
10972 add_anchor_token(T_typedef);
10973 add_anchor_token(T_typeid);
10974 add_anchor_token(T_typename);
10975 add_anchor_token(T_typeof);
10976 add_anchor_token(T_union);
10977 add_anchor_token(T_unsigned);
10978 add_anchor_token(T_using);
10979 add_anchor_token(T_void);
10980 add_anchor_token(T_volatile);
10981 add_anchor_token(T_wchar_t);
10982 add_anchor_token(T_while);
10984 size_t const top = environment_top();
10985 scope_t *old_scope = scope_push(&statement->compound.scope);
10987 statement_t **anchor = &statement->compound.statements;
10988 bool only_decls_so_far = true;
10989 while (token.type != '}') {
10990 if (token.type == T_EOF) {
10991 errorf(&statement->base.source_position,
10992 "EOF while parsing compound statement");
10995 statement_t *sub_statement = intern_parse_statement();
10996 if (is_invalid_statement(sub_statement)) {
10997 /* an error occurred. if we are at an anchor, return */
11003 if (warning.declaration_after_statement) {
11004 if (sub_statement->kind != STATEMENT_DECLARATION) {
11005 only_decls_so_far = false;
11006 } else if (!only_decls_so_far) {
11007 warningf(&sub_statement->base.source_position,
11008 "ISO C90 forbids mixed declarations and code");
11012 *anchor = sub_statement;
11014 while (sub_statement->base.next != NULL)
11015 sub_statement = sub_statement->base.next;
11017 anchor = &sub_statement->base.next;
11021 /* look over all statements again to produce no effect warnings */
11022 if (warning.unused_value) {
11023 statement_t *sub_statement = statement->compound.statements;
11024 for (; sub_statement != NULL; sub_statement = sub_statement->base.next) {
11025 if (sub_statement->kind != STATEMENT_EXPRESSION)
11027 /* don't emit a warning for the last expression in an expression
11028 * statement as it has always an effect */
11029 if (inside_expression_statement && sub_statement->base.next == NULL)
11032 expression_t *expression = sub_statement->expression.expression;
11033 if (!expression_has_effect(expression)) {
11034 warningf(&expression->base.source_position,
11035 "statement has no effect");
11041 rem_anchor_token(T_while);
11042 rem_anchor_token(T_wchar_t);
11043 rem_anchor_token(T_volatile);
11044 rem_anchor_token(T_void);
11045 rem_anchor_token(T_using);
11046 rem_anchor_token(T_unsigned);
11047 rem_anchor_token(T_union);
11048 rem_anchor_token(T_typeof);
11049 rem_anchor_token(T_typename);
11050 rem_anchor_token(T_typeid);
11051 rem_anchor_token(T_typedef);
11052 rem_anchor_token(T_try);
11053 rem_anchor_token(T_true);
11054 rem_anchor_token(T_throw);
11055 rem_anchor_token(T_this);
11056 rem_anchor_token(T_template);
11057 rem_anchor_token(T_switch);
11058 rem_anchor_token(T_struct);
11059 rem_anchor_token(T_static_cast);
11060 rem_anchor_token(T_static);
11061 rem_anchor_token(T_sizeof);
11062 rem_anchor_token(T_signed);
11063 rem_anchor_token(T_short);
11064 rem_anchor_token(T_return);
11065 rem_anchor_token(T_restrict);
11066 rem_anchor_token(T_reinterpret_cast);
11067 rem_anchor_token(T_register);
11068 rem_anchor_token(T_operator);
11069 rem_anchor_token(T_new);
11070 rem_anchor_token(T_long);
11071 rem_anchor_token(T_int);
11072 rem_anchor_token(T_inline);
11073 rem_anchor_token(T_if);
11074 rem_anchor_token(T_goto);
11075 rem_anchor_token(T_for);
11076 rem_anchor_token(T_float);
11077 rem_anchor_token(T_false);
11078 rem_anchor_token(T_extern);
11079 rem_anchor_token(T_enum);
11080 rem_anchor_token(T_dynamic_cast);
11081 rem_anchor_token(T_do);
11082 rem_anchor_token(T_double);
11083 rem_anchor_token(T_delete);
11084 rem_anchor_token(T_default);
11085 rem_anchor_token(T_continue);
11086 rem_anchor_token(T_const_cast);
11087 rem_anchor_token(T_const);
11088 rem_anchor_token(T_class);
11089 rem_anchor_token(T_char);
11090 rem_anchor_token(T_case);
11091 rem_anchor_token(T_break);
11092 rem_anchor_token(T_bool);
11093 rem_anchor_token(T_auto);
11094 rem_anchor_token(T_asm);
11095 rem_anchor_token(T___thread);
11096 rem_anchor_token(T___real__);
11097 rem_anchor_token(T___label__);
11098 rem_anchor_token(T___imag__);
11099 rem_anchor_token(T___func__);
11100 rem_anchor_token(T___extension__);
11101 rem_anchor_token(T___builtin_va_start);
11102 rem_anchor_token(T___attribute__);
11103 rem_anchor_token(T___alignof__);
11104 rem_anchor_token(T___PRETTY_FUNCTION__);
11105 rem_anchor_token(T___FUNCTION__);
11106 rem_anchor_token(T__Imaginary);
11107 rem_anchor_token(T__Complex);
11108 rem_anchor_token(T__Bool);
11109 rem_anchor_token(T_WIDE_STRING_LITERAL);
11110 rem_anchor_token(T_WIDE_CHARACTER_CONSTANT);
11111 rem_anchor_token(T_STRING_LITERAL);
11112 rem_anchor_token(T_PLUSPLUS);
11113 rem_anchor_token(T_MINUSMINUS);
11114 rem_anchor_token(T_INTEGER);
11115 rem_anchor_token(T_IDENTIFIER);
11116 rem_anchor_token(T_FLOATINGPOINT);
11117 rem_anchor_token(T_COLONCOLON);
11118 rem_anchor_token(T_CHARACTER_CONSTANT);
11119 rem_anchor_token('~');
11120 rem_anchor_token('{');
11121 rem_anchor_token('-');
11122 rem_anchor_token('+');
11123 rem_anchor_token('*');
11124 rem_anchor_token('(');
11125 rem_anchor_token('&');
11126 rem_anchor_token('!');
11127 rem_anchor_token('}');
11128 assert(current_scope == &statement->compound.scope);
11129 scope_pop(old_scope);
11130 environment_pop_to(top);
11137 * Check for unused global static functions and variables
11139 static void check_unused_globals(void)
11141 if (!warning.unused_function && !warning.unused_variable)
11144 for (const entity_t *entity = file_scope->entities; entity != NULL;
11145 entity = entity->base.next) {
11146 if (!is_declaration(entity))
11149 const declaration_t *declaration = &entity->declaration;
11150 if (declaration->used ||
11151 declaration->modifiers & DM_UNUSED ||
11152 declaration->modifiers & DM_USED ||
11153 declaration->storage_class != STORAGE_CLASS_STATIC)
11156 type_t *const type = declaration->type;
11158 if (entity->kind == ENTITY_FUNCTION) {
11159 /* inhibit warning for static inline functions */
11160 if (entity->function.is_inline)
11163 s = entity->function.statement != NULL ? "defined" : "declared";
11168 warningf(&declaration->base.source_position, "'%#T' %s but not used",
11169 type, declaration->base.symbol, s);
11173 static void parse_global_asm(void)
11175 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
11178 expect('(', end_error);
11180 statement->asms.asm_text = parse_string_literals();
11181 statement->base.next = unit->global_asm;
11182 unit->global_asm = statement;
11184 expect(')', end_error);
11185 expect(';', end_error);
11190 static void parse_linkage_specification(void)
11193 assert(token.type == T_STRING_LITERAL);
11195 const char *linkage = parse_string_literals().begin;
11197 linkage_kind_t old_linkage = current_linkage;
11198 linkage_kind_t new_linkage;
11199 if (strcmp(linkage, "C") == 0) {
11200 new_linkage = LINKAGE_C;
11201 } else if (strcmp(linkage, "C++") == 0) {
11202 new_linkage = LINKAGE_CXX;
11204 errorf(HERE, "linkage string \"%s\" not recognized", linkage);
11205 new_linkage = LINKAGE_INVALID;
11207 current_linkage = new_linkage;
11209 if (token.type == '{') {
11212 expect('}', end_error);
11218 assert(current_linkage == new_linkage);
11219 current_linkage = old_linkage;
11222 static void parse_external(void)
11224 switch (token.type) {
11225 DECLARATION_START_NO_EXTERN
11227 case T___extension__:
11228 /* tokens below are for implicit int */
11229 case '&': /* & x; -> int& x; (and error later, because C++ has no
11231 case '*': /* * x; -> int* x; */
11232 case '(': /* (x); -> int (x); */
11233 parse_external_declaration();
11237 if (look_ahead(1)->type == T_STRING_LITERAL) {
11238 parse_linkage_specification();
11240 parse_external_declaration();
11245 parse_global_asm();
11249 parse_namespace_definition();
11253 if (!strict_mode) {
11255 warningf(HERE, "stray ';' outside of function");
11262 errorf(HERE, "stray %K outside of function", &token);
11263 if (token.type == '(' || token.type == '{' || token.type == '[')
11264 eat_until_matching_token(token.type);
11270 static void parse_externals(void)
11272 add_anchor_token('}');
11273 add_anchor_token(T_EOF);
11276 unsigned char token_anchor_copy[T_LAST_TOKEN];
11277 memcpy(token_anchor_copy, token_anchor_set, sizeof(token_anchor_copy));
11280 while (token.type != T_EOF && token.type != '}') {
11282 bool anchor_leak = false;
11283 for (int i = 0; i != T_LAST_TOKEN; ++i) {
11284 unsigned char count = token_anchor_set[i] - token_anchor_copy[i];
11286 errorf(HERE, "Leaked anchor token %k %d times", i, count);
11287 anchor_leak = true;
11290 if (in_gcc_extension) {
11291 errorf(HERE, "Leaked __extension__");
11292 anchor_leak = true;
11302 rem_anchor_token(T_EOF);
11303 rem_anchor_token('}');
11307 * Parse a translation unit.
11309 static void parse_translation_unit(void)
11311 add_anchor_token(T_EOF);
11316 if (token.type == T_EOF)
11319 errorf(HERE, "stray %K outside of function", &token);
11320 if (token.type == '(' || token.type == '{' || token.type == '[')
11321 eat_until_matching_token(token.type);
11329 * @return the translation unit or NULL if errors occurred.
11331 void start_parsing(void)
11333 environment_stack = NEW_ARR_F(stack_entry_t, 0);
11334 label_stack = NEW_ARR_F(stack_entry_t, 0);
11335 diagnostic_count = 0;
11339 type_set_output(stderr);
11340 ast_set_output(stderr);
11342 assert(unit == NULL);
11343 unit = allocate_ast_zero(sizeof(unit[0]));
11345 assert(file_scope == NULL);
11346 file_scope = &unit->scope;
11348 assert(current_scope == NULL);
11349 scope_push(&unit->scope);
11351 create_gnu_builtins();
11353 create_microsoft_intrinsics();
11356 translation_unit_t *finish_parsing(void)
11358 assert(current_scope == &unit->scope);
11361 assert(file_scope == &unit->scope);
11362 check_unused_globals();
11365 DEL_ARR_F(environment_stack);
11366 DEL_ARR_F(label_stack);
11368 translation_unit_t *result = unit;
11373 /* §6.9.2:2 and §6.9.2:5: At the end of the translation incomplete arrays
11374 * are given length one. */
11375 static void complete_incomplete_arrays(void)
11377 size_t n = ARR_LEN(incomplete_arrays);
11378 for (size_t i = 0; i != n; ++i) {
11379 declaration_t *const decl = incomplete_arrays[i];
11380 type_t *const orig_type = decl->type;
11381 type_t *const type = skip_typeref(orig_type);
11383 if (!is_type_incomplete(type))
11386 if (warning.other) {
11387 warningf(&decl->base.source_position,
11388 "array '%#T' assumed to have one element",
11389 orig_type, decl->base.symbol);
11392 type_t *const new_type = duplicate_type(type);
11393 new_type->array.size_constant = true;
11394 new_type->array.has_implicit_size = true;
11395 new_type->array.size = 1;
11397 type_t *const result = identify_new_type(new_type);
11399 decl->type = result;
11405 lookahead_bufpos = 0;
11406 for (int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
11409 current_linkage = c_mode & _CXX ? LINKAGE_CXX : LINKAGE_C;
11410 incomplete_arrays = NEW_ARR_F(declaration_t*, 0);
11411 parse_translation_unit();
11412 complete_incomplete_arrays();
11413 DEL_ARR_F(incomplete_arrays);
11414 incomplete_arrays = NULL;
11418 * create a builtin function.
11420 static entity_t *create_builtin_function(builtin_kind_t kind, const char *name, type_t *function_type)
11422 symbol_t *symbol = symbol_table_insert(name);
11423 entity_t *entity = allocate_entity_zero(ENTITY_FUNCTION);
11424 entity->declaration.storage_class = STORAGE_CLASS_EXTERN;
11425 entity->declaration.declared_storage_class = STORAGE_CLASS_EXTERN;
11426 entity->declaration.type = function_type;
11427 entity->declaration.implicit = true;
11428 entity->base.symbol = symbol;
11429 entity->base.source_position = builtin_source_position;
11431 entity->function.btk = kind;
11433 record_entity(entity, /*is_definition=*/false);
11439 * Create predefined gnu builtins.
11441 static void create_gnu_builtins(void)
11443 #define GNU_BUILTIN(a, b) create_builtin_function(bk_gnu_builtin_##a, "__builtin_" #a, b)
11445 GNU_BUILTIN(alloca, make_function_1_type(type_void_ptr, type_size_t));
11446 GNU_BUILTIN(huge_val, make_function_0_type(type_double));
11447 GNU_BUILTIN(inf, make_function_0_type(type_double));
11448 GNU_BUILTIN(inff, make_function_0_type(type_float));
11449 GNU_BUILTIN(infl, make_function_0_type(type_long_double));
11450 GNU_BUILTIN(nan, make_function_1_type(type_double, type_char_ptr));
11451 GNU_BUILTIN(nanf, make_function_1_type(type_float, type_char_ptr));
11452 GNU_BUILTIN(nanl, make_function_1_type(type_long_double, type_char_ptr));
11453 GNU_BUILTIN(va_end, make_function_1_type(type_void, type_valist));
11454 GNU_BUILTIN(expect, make_function_2_type(type_long, type_long, type_long));
11455 GNU_BUILTIN(return_address, make_function_1_type(type_void_ptr, type_unsigned_int));
11456 GNU_BUILTIN(frame_address, make_function_1_type(type_void_ptr, type_unsigned_int));
11457 GNU_BUILTIN(ffs, make_function_1_type(type_int, type_unsigned_int));
11458 GNU_BUILTIN(clz, make_function_1_type(type_int, type_unsigned_int));
11459 GNU_BUILTIN(ctz, make_function_1_type(type_int, type_unsigned_int));
11460 GNU_BUILTIN(popcount, make_function_1_type(type_int, type_unsigned_int));
11461 GNU_BUILTIN(parity, make_function_1_type(type_int, type_unsigned_int));
11462 GNU_BUILTIN(prefetch, make_function_1_type_variadic(type_float, type_void_ptr));
11463 GNU_BUILTIN(trap, make_function_0_type_noreturn(type_void));
11469 * Create predefined MS intrinsics.
11471 static void create_microsoft_intrinsics(void)
11473 #define MS_BUILTIN(a, b) create_builtin_function(bk_ms##a, #a, b)
11475 /* intrinsics for all architectures */
11476 MS_BUILTIN(_rotl, make_function_2_type(type_unsigned_int, type_unsigned_int, type_int));
11477 MS_BUILTIN(_rotr, make_function_2_type(type_unsigned_int, type_unsigned_int, type_int));
11478 MS_BUILTIN(_rotl64, make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int));
11479 MS_BUILTIN(_rotr64, make_function_2_type(type_unsigned_int64, type_unsigned_int64, type_int));
11480 MS_BUILTIN(_byteswap_ushort, make_function_1_type(type_unsigned_short, type_unsigned_short));
11481 MS_BUILTIN(_byteswap_ulong, make_function_1_type(type_unsigned_long, type_unsigned_long));
11482 MS_BUILTIN(_byteswap_uint64, make_function_1_type(type_unsigned_int64, type_unsigned_int64));
11484 MS_BUILTIN(__debugbreak, make_function_0_type(type_void));
11485 MS_BUILTIN(_ReturnAddress, make_function_0_type(type_void_ptr));
11486 MS_BUILTIN(__popcount, make_function_1_type(type_unsigned_int, type_unsigned_int));
11489 MS_BUILTIN(_enable, make_function_0_type(type_void));
11490 MS_BUILTIN(_disable, make_function_0_type(type_void));
11491 MS_BUILTIN(__inbyte, make_function_1_type(type_unsigned_char, type_unsigned_short));
11492 MS_BUILTIN(__inword, make_function_1_type(type_unsigned_short, type_unsigned_short));
11493 MS_BUILTIN(__indword, make_function_1_type(type_unsigned_long, type_unsigned_short));
11494 MS_BUILTIN(__outbyte, make_function_2_type(type_void, type_unsigned_short, type_unsigned_char));
11495 MS_BUILTIN(__outword, make_function_2_type(type_void, type_unsigned_short, type_unsigned_short));
11496 MS_BUILTIN(__outdword, make_function_2_type(type_void, type_unsigned_short, type_unsigned_long));
11497 MS_BUILTIN(__ud2, make_function_0_type_noreturn(type_void));
11498 MS_BUILTIN(_BitScanForward, make_function_2_type(type_unsigned_char, type_unsigned_long_ptr, type_unsigned_long));
11499 MS_BUILTIN(_BitScanReverse, make_function_2_type(type_unsigned_char, type_unsigned_long_ptr, type_unsigned_long));
11500 MS_BUILTIN(_InterlockedExchange, make_function_2_type(type_long, type_long_ptr, type_long));
11501 MS_BUILTIN(_InterlockedExchange64, make_function_2_type(type_int64, type_int64_ptr, type_int64));
11503 if (machine_size <= 32) {
11504 MS_BUILTIN(__readeflags, make_function_0_type(type_unsigned_int));
11505 MS_BUILTIN(__writeeflags, make_function_1_type(type_void, type_unsigned_int));
11507 MS_BUILTIN(__readeflags, make_function_0_type(type_unsigned_int64));
11508 MS_BUILTIN(__writeeflags, make_function_1_type(type_void, type_unsigned_int64));
11515 * Initialize the parser.
11517 void init_parser(void)
11519 sym_anonymous = symbol_table_insert("<anonymous>");
11521 if (c_mode & _MS) {
11522 /* add predefined symbols for extended-decl-modifier */
11523 sym_align = symbol_table_insert("align");
11524 sym_allocate = symbol_table_insert("allocate");
11525 sym_dllimport = symbol_table_insert("dllimport");
11526 sym_dllexport = symbol_table_insert("dllexport");
11527 sym_naked = symbol_table_insert("naked");
11528 sym_noinline = symbol_table_insert("noinline");
11529 sym_returns_twice = symbol_table_insert("returns_twice");
11530 sym_noreturn = symbol_table_insert("noreturn");
11531 sym_nothrow = symbol_table_insert("nothrow");
11532 sym_novtable = symbol_table_insert("novtable");
11533 sym_property = symbol_table_insert("property");
11534 sym_get = symbol_table_insert("get");
11535 sym_put = symbol_table_insert("put");
11536 sym_selectany = symbol_table_insert("selectany");
11537 sym_thread = symbol_table_insert("thread");
11538 sym_uuid = symbol_table_insert("uuid");
11539 sym_deprecated = symbol_table_insert("deprecated");
11540 sym_restrict = symbol_table_insert("restrict");
11541 sym_noalias = symbol_table_insert("noalias");
11543 memset(token_anchor_set, 0, sizeof(token_anchor_set));
11545 init_expression_parsers();
11546 obstack_init(&temp_obst);
11548 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
11549 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
11553 * Terminate the parser.
11555 void exit_parser(void)
11557 obstack_free(&temp_obst, NULL);