2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 /** if wchar_t is equal to unsigned short. */
43 bool opt_short_wchar_t =
50 //#define PRINT_TOKENS
51 #define MAX_LOOKAHEAD 2
54 declaration_t *old_declaration;
56 unsigned short namespc;
59 typedef struct argument_list_t argument_list_t;
60 struct argument_list_t {
62 argument_list_t *next;
65 typedef struct gnu_attribute_t gnu_attribute_t;
66 struct gnu_attribute_t {
67 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
68 gnu_attribute_t *next;
69 bool invalid; /**< Set if this attribute had argument errors, */
70 bool have_arguments; /**< True, if this attribute has arguments. */
74 atomic_type_kind_t akind;
75 long argument; /**< Single argument. */
76 argument_list_t *arguments; /**< List of argument expressions. */
80 typedef struct declaration_specifiers_t declaration_specifiers_t;
81 struct declaration_specifiers_t {
82 source_position_t source_position;
83 unsigned char declared_storage_class;
84 unsigned char alignment; /**< Alignment, 0 if not set. */
85 unsigned int is_inline : 1;
86 unsigned int deprecated : 1;
87 decl_modifiers_t modifiers; /**< declaration modifiers */
88 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
89 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
90 symbol_t *get_property_sym; /**< the name of the get property if set. */
91 symbol_t *put_property_sym; /**< the name of the put property if set. */
96 * An environment for parsing initializers (and compound literals).
98 typedef struct parse_initializer_env_t {
99 type_t *type; /**< the type of the initializer. In case of an
100 array type with unspecified size this gets
101 adjusted to the actual size. */
102 declaration_t *declaration; /**< the declaration that is initialized if any */
103 bool must_be_constant;
104 } parse_initializer_env_t;
106 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
108 static token_t token;
109 static token_t lookahead_buffer[MAX_LOOKAHEAD];
110 static int lookahead_bufpos;
111 static stack_entry_t *environment_stack = NULL;
112 static stack_entry_t *label_stack = NULL;
113 static scope_t *global_scope = NULL;
114 static scope_t *scope = NULL;
115 static declaration_t *last_declaration = NULL;
116 static declaration_t *current_function = NULL;
117 static switch_statement_t *current_switch = NULL;
118 static statement_t *current_loop = NULL;
119 static statement_t *current_parent = NULL;
120 static ms_try_statement_t *current_try = NULL;
121 static goto_statement_t *goto_first = NULL;
122 static goto_statement_t *goto_last = NULL;
123 static label_statement_t *label_first = NULL;
124 static label_statement_t *label_last = NULL;
125 static translation_unit_t *unit = NULL;
126 static struct obstack temp_obst;
128 #define PUSH_PARENT(stmt) \
129 statement_t *const prev_parent = current_parent; \
130 current_parent = (stmt);
131 #define POP_PARENT ((void)(current_parent = prev_parent))
133 static source_position_t null_position = { NULL, 0 };
135 /* symbols for Microsoft extended-decl-modifier */
136 static const symbol_t *sym_align = NULL;
137 static const symbol_t *sym_allocate = NULL;
138 static const symbol_t *sym_dllimport = NULL;
139 static const symbol_t *sym_dllexport = NULL;
140 static const symbol_t *sym_naked = NULL;
141 static const symbol_t *sym_noinline = NULL;
142 static const symbol_t *sym_noreturn = NULL;
143 static const symbol_t *sym_nothrow = NULL;
144 static const symbol_t *sym_novtable = NULL;
145 static const symbol_t *sym_property = NULL;
146 static const symbol_t *sym_get = NULL;
147 static const symbol_t *sym_put = NULL;
148 static const symbol_t *sym_selectany = NULL;
149 static const symbol_t *sym_thread = NULL;
150 static const symbol_t *sym_uuid = NULL;
151 static const symbol_t *sym_deprecated = NULL;
152 static const symbol_t *sym_restrict = NULL;
153 static const symbol_t *sym_noalias = NULL;
155 /** The token anchor set */
156 static unsigned char token_anchor_set[T_LAST_TOKEN];
158 /** The current source position. */
159 #define HERE (&token.source_position)
161 static type_t *type_valist;
163 static statement_t *parse_compound_statement(bool inside_expression_statement);
164 static statement_t *parse_statement(void);
166 static expression_t *parse_sub_expression(unsigned precedence);
167 static expression_t *parse_expression(void);
168 static type_t *parse_typename(void);
170 static void parse_compound_type_entries(declaration_t *compound_declaration);
171 static declaration_t *parse_declarator(
172 const declaration_specifiers_t *specifiers, bool may_be_abstract);
173 static declaration_t *record_declaration(declaration_t *declaration);
175 static void semantic_comparison(binary_expression_t *expression);
177 #define STORAGE_CLASSES \
185 #define TYPE_QUALIFIERS \
190 case T__forceinline: \
191 case T___attribute__:
193 #ifdef PROVIDE_COMPLEX
194 #define COMPLEX_SPECIFIERS \
196 #define IMAGINARY_SPECIFIERS \
199 #define COMPLEX_SPECIFIERS
200 #define IMAGINARY_SPECIFIERS
203 #define TYPE_SPECIFIERS \
218 case T___builtin_va_list: \
223 #define DECLARATION_START \
228 #define TYPENAME_START \
233 * Allocate an AST node with given size and
234 * initialize all fields with zero.
236 static void *allocate_ast_zero(size_t size)
238 void *res = allocate_ast(size);
239 memset(res, 0, size);
243 static declaration_t *allocate_declaration_zero(void)
245 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
246 declaration->type = type_error_type;
247 declaration->alignment = 0;
252 * Returns the size of a statement node.
254 * @param kind the statement kind
256 static size_t get_statement_struct_size(statement_kind_t kind)
258 static const size_t sizes[] = {
259 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
260 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
261 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
262 [STATEMENT_RETURN] = sizeof(return_statement_t),
263 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
264 [STATEMENT_IF] = sizeof(if_statement_t),
265 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
266 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
267 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
268 [STATEMENT_BREAK] = sizeof(statement_base_t),
269 [STATEMENT_GOTO] = sizeof(goto_statement_t),
270 [STATEMENT_LABEL] = sizeof(label_statement_t),
271 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
272 [STATEMENT_WHILE] = sizeof(while_statement_t),
273 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
274 [STATEMENT_FOR] = sizeof(for_statement_t),
275 [STATEMENT_ASM] = sizeof(asm_statement_t),
276 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
277 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
279 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
280 assert(sizes[kind] != 0);
285 * Returns the size of an expression node.
287 * @param kind the expression kind
289 static size_t get_expression_struct_size(expression_kind_t kind)
291 static const size_t sizes[] = {
292 [EXPR_INVALID] = sizeof(expression_base_t),
293 [EXPR_REFERENCE] = sizeof(reference_expression_t),
294 [EXPR_CONST] = sizeof(const_expression_t),
295 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
296 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
297 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
298 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
299 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
300 [EXPR_CALL] = sizeof(call_expression_t),
301 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
302 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
303 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
304 [EXPR_SELECT] = sizeof(select_expression_t),
305 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
306 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
307 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
308 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
309 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
310 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
311 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
312 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
313 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
314 [EXPR_VA_START] = sizeof(va_start_expression_t),
315 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
316 [EXPR_STATEMENT] = sizeof(statement_expression_t),
318 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
319 return sizes[EXPR_UNARY_FIRST];
321 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
322 return sizes[EXPR_BINARY_FIRST];
324 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
325 assert(sizes[kind] != 0);
330 * Allocate a statement node of given kind and initialize all
333 static statement_t *allocate_statement_zero(statement_kind_t kind)
335 size_t size = get_statement_struct_size(kind);
336 statement_t *res = allocate_ast_zero(size);
338 res->base.kind = kind;
339 res->base.parent = current_parent;
344 * Allocate an expression node of given kind and initialize all
347 static expression_t *allocate_expression_zero(expression_kind_t kind)
349 size_t size = get_expression_struct_size(kind);
350 expression_t *res = allocate_ast_zero(size);
352 res->base.kind = kind;
353 res->base.type = type_error_type;
358 * Creates a new invalid expression.
360 static expression_t *create_invalid_expression(void)
362 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
363 expression->base.source_position = token.source_position;
368 * Creates a new invalid statement.
370 static statement_t *create_invalid_statement(void)
372 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
373 statement->base.source_position = token.source_position;
378 * Allocate a new empty statement.
380 static statement_t *create_empty_statement(void)
382 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
383 statement->base.source_position = token.source_position;
388 * Returns the size of a type node.
390 * @param kind the type kind
392 static size_t get_type_struct_size(type_kind_t kind)
394 static const size_t sizes[] = {
395 [TYPE_ATOMIC] = sizeof(atomic_type_t),
396 [TYPE_COMPLEX] = sizeof(complex_type_t),
397 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
398 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
399 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
400 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
401 [TYPE_ENUM] = sizeof(enum_type_t),
402 [TYPE_FUNCTION] = sizeof(function_type_t),
403 [TYPE_POINTER] = sizeof(pointer_type_t),
404 [TYPE_ARRAY] = sizeof(array_type_t),
405 [TYPE_BUILTIN] = sizeof(builtin_type_t),
406 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
407 [TYPE_TYPEOF] = sizeof(typeof_type_t),
409 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
410 assert(kind <= TYPE_TYPEOF);
411 assert(sizes[kind] != 0);
416 * Allocate a type node of given kind and initialize all
419 * @param kind type kind to allocate
420 * @param source_position the source position of the type definition
422 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
424 size_t size = get_type_struct_size(kind);
425 type_t *res = obstack_alloc(type_obst, size);
426 memset(res, 0, size);
428 res->base.kind = kind;
429 res->base.source_position = *source_position;
434 * Returns the size of an initializer node.
436 * @param kind the initializer kind
438 static size_t get_initializer_size(initializer_kind_t kind)
440 static const size_t sizes[] = {
441 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
442 [INITIALIZER_STRING] = sizeof(initializer_string_t),
443 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
444 [INITIALIZER_LIST] = sizeof(initializer_list_t),
445 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
447 assert(kind < sizeof(sizes) / sizeof(*sizes));
448 assert(sizes[kind] != 0);
453 * Allocate an initializer node of given kind and initialize all
456 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
458 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
465 * Free a type from the type obstack.
467 static void free_type(void *type)
469 obstack_free(type_obst, type);
473 * Returns the index of the top element of the environment stack.
475 static size_t environment_top(void)
477 return ARR_LEN(environment_stack);
481 * Returns the index of the top element of the label stack.
483 static size_t label_top(void)
485 return ARR_LEN(label_stack);
489 * Return the next token.
491 static inline void next_token(void)
493 token = lookahead_buffer[lookahead_bufpos];
494 lookahead_buffer[lookahead_bufpos] = lexer_token;
497 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
500 print_token(stderr, &token);
501 fprintf(stderr, "\n");
506 * Return the next token with a given lookahead.
508 static inline const token_t *look_ahead(int num)
510 assert(num > 0 && num <= MAX_LOOKAHEAD);
511 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
512 return &lookahead_buffer[pos];
516 * Adds a token to the token anchor set (a multi-set).
518 static void add_anchor_token(int token_type)
520 assert(0 <= token_type && token_type < T_LAST_TOKEN);
521 ++token_anchor_set[token_type];
524 static int save_and_reset_anchor_state(int token_type)
526 assert(0 <= token_type && token_type < T_LAST_TOKEN);
527 int count = token_anchor_set[token_type];
528 token_anchor_set[token_type] = 0;
532 static void restore_anchor_state(int token_type, int count)
534 assert(0 <= token_type && token_type < T_LAST_TOKEN);
535 token_anchor_set[token_type] = count;
539 * Remove a token from the token anchor set (a multi-set).
541 static void rem_anchor_token(int token_type)
543 assert(0 <= token_type && token_type < T_LAST_TOKEN);
544 --token_anchor_set[token_type];
547 static bool at_anchor(void)
551 return token_anchor_set[token.type];
555 * Eat tokens until a matching token is found.
557 static void eat_until_matching_token(int type)
561 case '(': end_token = ')'; break;
562 case '{': end_token = '}'; break;
563 case '[': end_token = ']'; break;
564 default: end_token = type; break;
567 unsigned parenthesis_count = 0;
568 unsigned brace_count = 0;
569 unsigned bracket_count = 0;
570 while (token.type != end_token ||
571 parenthesis_count != 0 ||
573 bracket_count != 0) {
574 switch (token.type) {
576 case '(': ++parenthesis_count; break;
577 case '{': ++brace_count; break;
578 case '[': ++bracket_count; break;
581 if (parenthesis_count > 0)
591 if (bracket_count > 0)
594 if (token.type == end_token &&
595 parenthesis_count == 0 &&
609 * Eat input tokens until an anchor is found.
611 static void eat_until_anchor(void)
613 if (token.type == T_EOF)
615 while (token_anchor_set[token.type] == 0) {
616 if (token.type == '(' || token.type == '{' || token.type == '[')
617 eat_until_matching_token(token.type);
618 if (token.type == T_EOF)
624 static void eat_block(void)
626 eat_until_matching_token('{');
627 if (token.type == '}')
632 * eat all token until a ';' is reached or a stop token is found.
634 static void eat_statement(void)
636 eat_until_matching_token(';');
637 if (token.type == ';')
641 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
644 * Report a parse error because an expected token was not found.
647 #if defined __GNUC__ && __GNUC__ >= 4
648 __attribute__((sentinel))
650 void parse_error_expected(const char *message, ...)
652 if (message != NULL) {
653 errorf(HERE, "%s", message);
656 va_start(ap, message);
657 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
662 * Report a type error.
664 static void type_error(const char *msg, const source_position_t *source_position,
667 errorf(source_position, "%s, but found type '%T'", msg, type);
671 * Report an incompatible type.
673 static void type_error_incompatible(const char *msg,
674 const source_position_t *source_position, type_t *type1, type_t *type2)
676 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
681 * Expect the the current token is the expected token.
682 * If not, generate an error, eat the current statement,
683 * and goto the end_error label.
685 #define expect(expected) \
687 if (UNLIKELY(token.type != (expected))) { \
688 parse_error_expected(NULL, (expected), NULL); \
689 add_anchor_token(expected); \
690 eat_until_anchor(); \
691 if (token.type == expected) \
693 rem_anchor_token(expected); \
699 static void set_scope(scope_t *new_scope)
702 scope->last_declaration = last_declaration;
706 last_declaration = new_scope->last_declaration;
710 * Search a symbol in a given namespace and returns its declaration or
711 * NULL if this symbol was not found.
713 static declaration_t *get_declaration(const symbol_t *const symbol,
714 const namespace_t namespc)
716 declaration_t *declaration = symbol->declaration;
717 for( ; declaration != NULL; declaration = declaration->symbol_next) {
718 if (declaration->namespc == namespc)
726 * pushs an environment_entry on the environment stack and links the
727 * corresponding symbol to the new entry
729 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
731 symbol_t *symbol = declaration->symbol;
732 namespace_t namespc = (namespace_t) declaration->namespc;
734 /* replace/add declaration into declaration list of the symbol */
735 declaration_t *iter = symbol->declaration;
737 symbol->declaration = declaration;
739 declaration_t *iter_last = NULL;
740 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
741 /* replace an entry? */
742 if (iter->namespc == namespc) {
743 if (iter_last == NULL) {
744 symbol->declaration = declaration;
746 iter_last->symbol_next = declaration;
748 declaration->symbol_next = iter->symbol_next;
753 assert(iter_last->symbol_next == NULL);
754 iter_last->symbol_next = declaration;
758 /* remember old declaration */
760 entry.symbol = symbol;
761 entry.old_declaration = iter;
762 entry.namespc = (unsigned short) namespc;
763 ARR_APP1(stack_entry_t, *stack_ptr, entry);
766 static void environment_push(declaration_t *declaration)
768 assert(declaration->source_position.input_name != NULL);
769 assert(declaration->parent_scope != NULL);
770 stack_push(&environment_stack, declaration);
774 * Push a declaration of the label stack.
776 * @param declaration the declaration
778 static void label_push(declaration_t *declaration)
780 declaration->parent_scope = ¤t_function->scope;
781 stack_push(&label_stack, declaration);
785 * pops symbols from the environment stack until @p new_top is the top element
787 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
789 stack_entry_t *stack = *stack_ptr;
790 size_t top = ARR_LEN(stack);
793 assert(new_top <= top);
797 for(i = top; i > new_top; --i) {
798 stack_entry_t *entry = &stack[i - 1];
800 declaration_t *old_declaration = entry->old_declaration;
801 symbol_t *symbol = entry->symbol;
802 namespace_t namespc = (namespace_t)entry->namespc;
804 /* replace/remove declaration */
805 declaration_t *declaration = symbol->declaration;
806 assert(declaration != NULL);
807 if (declaration->namespc == namespc) {
808 if (old_declaration == NULL) {
809 symbol->declaration = declaration->symbol_next;
811 symbol->declaration = old_declaration;
814 declaration_t *iter_last = declaration;
815 declaration_t *iter = declaration->symbol_next;
816 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
817 /* replace an entry? */
818 if (iter->namespc == namespc) {
819 assert(iter_last != NULL);
820 iter_last->symbol_next = old_declaration;
821 if (old_declaration != NULL) {
822 old_declaration->symbol_next = iter->symbol_next;
827 assert(iter != NULL);
831 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
834 static void environment_pop_to(size_t new_top)
836 stack_pop_to(&environment_stack, new_top);
840 * Pop all entries on the label stack until the new_top
843 * @param new_top the new stack top
845 static void label_pop_to(size_t new_top)
847 stack_pop_to(&label_stack, new_top);
850 static int get_akind_rank(atomic_type_kind_t akind)
855 static int get_rank(const type_t *type)
857 assert(!is_typeref(type));
858 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
859 * and esp. footnote 108). However we can't fold constants (yet), so we
860 * can't decide whether unsigned int is possible, while int always works.
861 * (unsigned int would be preferable when possible... for stuff like
862 * struct { enum { ... } bla : 4; } ) */
863 if (type->kind == TYPE_ENUM)
864 return get_akind_rank(ATOMIC_TYPE_INT);
866 assert(type->kind == TYPE_ATOMIC);
867 return get_akind_rank(type->atomic.akind);
870 static type_t *promote_integer(type_t *type)
872 if (type->kind == TYPE_BITFIELD)
873 type = type->bitfield.base_type;
875 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
882 * Create a cast expression.
884 * @param expression the expression to cast
885 * @param dest_type the destination type
887 static expression_t *create_cast_expression(expression_t *expression,
890 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
892 cast->unary.value = expression;
893 cast->base.type = dest_type;
899 * Check if a given expression represents the 0 pointer constant.
901 static bool is_null_pointer_constant(const expression_t *expression)
903 /* skip void* cast */
904 if (expression->kind == EXPR_UNARY_CAST
905 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
906 expression = expression->unary.value;
909 /* TODO: not correct yet, should be any constant integer expression
910 * which evaluates to 0 */
911 if (expression->kind != EXPR_CONST)
914 type_t *const type = skip_typeref(expression->base.type);
915 if (!is_type_integer(type))
918 return expression->conste.v.int_value == 0;
922 * Create an implicit cast expression.
924 * @param expression the expression to cast
925 * @param dest_type the destination type
927 static expression_t *create_implicit_cast(expression_t *expression,
930 type_t *const source_type = expression->base.type;
932 if (source_type == dest_type)
935 return create_cast_expression(expression, dest_type);
938 typedef enum assign_error_t {
940 ASSIGN_ERROR_INCOMPATIBLE,
941 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
942 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
943 ASSIGN_WARNING_POINTER_FROM_INT,
944 ASSIGN_WARNING_INT_FROM_POINTER
947 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
948 const expression_t *const right,
950 const source_position_t *source_position)
952 type_t *const orig_type_right = right->base.type;
953 type_t *const type_left = skip_typeref(orig_type_left);
954 type_t *const type_right = skip_typeref(orig_type_right);
959 case ASSIGN_ERROR_INCOMPATIBLE:
960 errorf(source_position,
961 "destination type '%T' in %s is incompatible with type '%T'",
962 orig_type_left, context, orig_type_right);
965 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
966 type_t *points_to_left
967 = skip_typeref(type_left->pointer.points_to);
968 type_t *points_to_right
969 = skip_typeref(type_right->pointer.points_to);
971 /* the left type has all qualifiers from the right type */
972 unsigned missing_qualifiers
973 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
974 warningf(source_position,
975 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
976 orig_type_left, context, orig_type_right, missing_qualifiers);
980 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
981 warningf(source_position,
982 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
983 orig_type_left, context, right, orig_type_right);
986 case ASSIGN_WARNING_POINTER_FROM_INT:
987 warningf(source_position,
988 "%s makes integer '%T' from pointer '%T' without a cast",
989 context, orig_type_left, orig_type_right);
992 case ASSIGN_WARNING_INT_FROM_POINTER:
993 warningf(source_position,
994 "%s makes integer '%T' from pointer '%T' without a cast",
995 context, orig_type_left, orig_type_right);
999 panic("invalid error value");
1003 /** Implements the rules from § 6.5.16.1 */
1004 static assign_error_t semantic_assign(type_t *orig_type_left,
1005 const expression_t *const right)
1007 type_t *const orig_type_right = right->base.type;
1008 type_t *const type_left = skip_typeref(orig_type_left);
1009 type_t *const type_right = skip_typeref(orig_type_right);
1011 if (is_type_pointer(type_left)) {
1012 if (is_null_pointer_constant(right)) {
1013 return ASSIGN_SUCCESS;
1014 } else if (is_type_pointer(type_right)) {
1015 type_t *points_to_left
1016 = skip_typeref(type_left->pointer.points_to);
1017 type_t *points_to_right
1018 = skip_typeref(type_right->pointer.points_to);
1019 assign_error_t res = ASSIGN_SUCCESS;
1021 /* the left type has all qualifiers from the right type */
1022 unsigned missing_qualifiers
1023 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1024 if (missing_qualifiers != 0) {
1025 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1028 points_to_left = get_unqualified_type(points_to_left);
1029 points_to_right = get_unqualified_type(points_to_right);
1031 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1032 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1036 if (!types_compatible(points_to_left, points_to_right)) {
1037 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1041 } else if (is_type_integer(type_right)) {
1042 return ASSIGN_WARNING_POINTER_FROM_INT;
1044 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1045 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1046 && is_type_pointer(type_right))) {
1047 return ASSIGN_SUCCESS;
1048 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1049 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1050 type_t *const unqual_type_left = get_unqualified_type(type_left);
1051 type_t *const unqual_type_right = get_unqualified_type(type_right);
1052 if (types_compatible(unqual_type_left, unqual_type_right)) {
1053 return ASSIGN_SUCCESS;
1055 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1056 return ASSIGN_WARNING_INT_FROM_POINTER;
1059 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1060 return ASSIGN_SUCCESS;
1062 return ASSIGN_ERROR_INCOMPATIBLE;
1065 static expression_t *parse_constant_expression(void)
1067 /* start parsing at precedence 7 (conditional expression) */
1068 expression_t *result = parse_sub_expression(7);
1070 if (!is_constant_expression(result)) {
1071 errorf(&result->base.source_position,
1072 "expression '%E' is not constant\n", result);
1078 static expression_t *parse_assignment_expression(void)
1080 /* start parsing at precedence 2 (assignment expression) */
1081 return parse_sub_expression(2);
1084 static type_t *make_global_typedef(const char *name, type_t *type)
1086 symbol_t *const symbol = symbol_table_insert(name);
1088 declaration_t *const declaration = allocate_declaration_zero();
1089 declaration->namespc = NAMESPACE_NORMAL;
1090 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1091 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1092 declaration->type = type;
1093 declaration->symbol = symbol;
1094 declaration->source_position = builtin_source_position;
1095 declaration->implicit = true;
1097 record_declaration(declaration);
1099 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1100 typedef_type->typedeft.declaration = declaration;
1102 return typedef_type;
1105 static string_t parse_string_literals(void)
1107 assert(token.type == T_STRING_LITERAL);
1108 string_t result = token.v.string;
1112 while (token.type == T_STRING_LITERAL) {
1113 result = concat_strings(&result, &token.v.string);
1120 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1121 [GNU_AK_CONST] = "const",
1122 [GNU_AK_VOLATILE] = "volatile",
1123 [GNU_AK_CDECL] = "cdecl",
1124 [GNU_AK_STDCALL] = "stdcall",
1125 [GNU_AK_FASTCALL] = "fastcall",
1126 [GNU_AK_DEPRECATED] = "deprecated",
1127 [GNU_AK_NOINLINE] = "noinline",
1128 [GNU_AK_NORETURN] = "noreturn",
1129 [GNU_AK_NAKED] = "naked",
1130 [GNU_AK_PURE] = "pure",
1131 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1132 [GNU_AK_MALLOC] = "malloc",
1133 [GNU_AK_WEAK] = "weak",
1134 [GNU_AK_CONSTRUCTOR] = "constructor",
1135 [GNU_AK_DESTRUCTOR] = "destructor",
1136 [GNU_AK_NOTHROW] = "nothrow",
1137 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1138 [GNU_AK_COMMON] = "common",
1139 [GNU_AK_NOCOMMON] = "nocommon",
1140 [GNU_AK_PACKED] = "packed",
1141 [GNU_AK_SHARED] = "shared",
1142 [GNU_AK_NOTSHARED] = "notshared",
1143 [GNU_AK_USED] = "used",
1144 [GNU_AK_UNUSED] = "unused",
1145 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1146 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1147 [GNU_AK_LONGCALL] = "longcall",
1148 [GNU_AK_SHORTCALL] = "shortcall",
1149 [GNU_AK_LONG_CALL] = "long_call",
1150 [GNU_AK_SHORT_CALL] = "short_call",
1151 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1152 [GNU_AK_INTERRUPT] = "interrupt",
1153 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1154 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1155 [GNU_AK_NESTING] = "nesting",
1156 [GNU_AK_NEAR] = "near",
1157 [GNU_AK_FAR] = "far",
1158 [GNU_AK_SIGNAL] = "signal",
1159 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1160 [GNU_AK_TINY_DATA] = "tiny_data",
1161 [GNU_AK_SAVEALL] = "saveall",
1162 [GNU_AK_FLATTEN] = "flatten",
1163 [GNU_AK_SSEREGPARM] = "sseregparm",
1164 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1165 [GNU_AK_RETURN_TWICE] = "return_twice",
1166 [GNU_AK_MAY_ALIAS] = "may_alias",
1167 [GNU_AK_MS_STRUCT] = "ms_struct",
1168 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1169 [GNU_AK_DLLIMPORT] = "dllimport",
1170 [GNU_AK_DLLEXPORT] = "dllexport",
1171 [GNU_AK_ALIGNED] = "aligned",
1172 [GNU_AK_ALIAS] = "alias",
1173 [GNU_AK_SECTION] = "section",
1174 [GNU_AK_FORMAT] = "format",
1175 [GNU_AK_FORMAT_ARG] = "format_arg",
1176 [GNU_AK_WEAKREF] = "weakref",
1177 [GNU_AK_NONNULL] = "nonnull",
1178 [GNU_AK_TLS_MODEL] = "tls_model",
1179 [GNU_AK_VISIBILITY] = "visibility",
1180 [GNU_AK_REGPARM] = "regparm",
1181 [GNU_AK_MODE] = "mode",
1182 [GNU_AK_MODEL] = "model",
1183 [GNU_AK_TRAP_EXIT] = "trap_exit",
1184 [GNU_AK_SP_SWITCH] = "sp_switch",
1185 [GNU_AK_SENTINEL] = "sentinel"
1189 * compare two string, ignoring double underscores on the second.
1191 static int strcmp_underscore(const char *s1, const char *s2)
1193 if (s2[0] == '_' && s2[1] == '_') {
1194 size_t len2 = strlen(s2);
1195 size_t len1 = strlen(s1);
1196 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1197 return strncmp(s1, s2+2, len2-4);
1201 return strcmp(s1, s2);
1205 * Allocate a new gnu temporal attribute.
1207 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1209 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1210 attribute->kind = kind;
1211 attribute->next = NULL;
1212 attribute->invalid = false;
1213 attribute->have_arguments = false;
1219 * parse one constant expression argument.
1221 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1223 expression_t *expression;
1224 add_anchor_token(')');
1225 expression = parse_constant_expression();
1226 rem_anchor_token(')');
1228 attribute->u.argument = fold_constant(expression);
1231 attribute->invalid = true;
1235 * parse a list of constant expressions arguments.
1237 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1239 argument_list_t **list = &attribute->u.arguments;
1240 argument_list_t *entry;
1241 expression_t *expression;
1242 add_anchor_token(')');
1243 add_anchor_token(',');
1245 expression = parse_constant_expression();
1246 entry = obstack_alloc(&temp_obst, sizeof(entry));
1247 entry->argument = fold_constant(expression);
1250 list = &entry->next;
1251 if (token.type != ',')
1255 rem_anchor_token(',');
1256 rem_anchor_token(')');
1260 attribute->invalid = true;
1264 * parse one string literal argument.
1266 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1269 add_anchor_token('(');
1270 if (token.type != T_STRING_LITERAL) {
1271 parse_error_expected("while parsing attribute directive",
1272 T_STRING_LITERAL, NULL);
1275 *string = parse_string_literals();
1276 rem_anchor_token('(');
1280 attribute->invalid = true;
1284 * parse one tls model.
1286 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1288 static const char *const tls_models[] = {
1294 string_t string = { NULL, 0 };
1295 parse_gnu_attribute_string_arg(attribute, &string);
1296 if (string.begin != NULL) {
1297 for(size_t i = 0; i < 4; ++i) {
1298 if (strcmp(tls_models[i], string.begin) == 0) {
1299 attribute->u.value = i;
1303 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1305 attribute->invalid = true;
1309 * parse one tls model.
1311 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1313 static const char *const visibilities[] = {
1319 string_t string = { NULL, 0 };
1320 parse_gnu_attribute_string_arg(attribute, &string);
1321 if (string.begin != NULL) {
1322 for(size_t i = 0; i < 4; ++i) {
1323 if (strcmp(visibilities[i], string.begin) == 0) {
1324 attribute->u.value = i;
1328 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1330 attribute->invalid = true;
1334 * parse one (code) model.
1336 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1338 static const char *const visibilities[] = {
1343 string_t string = { NULL, 0 };
1344 parse_gnu_attribute_string_arg(attribute, &string);
1345 if (string.begin != NULL) {
1346 for(int i = 0; i < 3; ++i) {
1347 if (strcmp(visibilities[i], string.begin) == 0) {
1348 attribute->u.value = i;
1352 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1354 attribute->invalid = true;
1357 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1359 /* TODO: find out what is allowed here... */
1361 /* at least: byte, word, pointer, list of machine modes
1362 * __XXX___ is interpreted as XXX */
1363 add_anchor_token(')');
1365 if (token.type != T_IDENTIFIER) {
1366 expect(T_IDENTIFIER);
1369 /* This isn't really correct, the backend should provide a list of machine
1370 * specific modes (according to gcc philosophy that is...) */
1371 const char *symbol_str = token.v.symbol->string;
1372 if (strcmp_underscore("QI", symbol_str) == 0 ||
1373 strcmp_underscore("byte", symbol_str) == 0) {
1374 attribute->u.akind = ATOMIC_TYPE_CHAR;
1375 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1376 attribute->u.akind = ATOMIC_TYPE_SHORT;
1377 } else if (strcmp_underscore("SI", symbol_str) == 0
1378 || strcmp_underscore("word", symbol_str) == 0
1379 || strcmp_underscore("pointer", symbol_str) == 0) {
1380 attribute->u.akind = ATOMIC_TYPE_INT;
1381 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1382 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1384 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1385 attribute->invalid = true;
1389 rem_anchor_token(')');
1393 attribute->invalid = true;
1397 * parse one interrupt argument.
1399 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1401 static const char *const interrupts[] = {
1408 string_t string = { NULL, 0 };
1409 parse_gnu_attribute_string_arg(attribute, &string);
1410 if (string.begin != NULL) {
1411 for(size_t i = 0; i < 5; ++i) {
1412 if (strcmp(interrupts[i], string.begin) == 0) {
1413 attribute->u.value = i;
1417 errorf(HERE, "'%s' is not an interrupt", string.begin);
1419 attribute->invalid = true;
1423 * parse ( identifier, const expression, const expression )
1425 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1427 static const char *const format_names[] = {
1435 if (token.type != T_IDENTIFIER) {
1436 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1439 const char *name = token.v.symbol->string;
1440 for(i = 0; i < 4; ++i) {
1441 if (strcmp_underscore(format_names[i], name) == 0)
1445 if (warning.attribute)
1446 warningf(HERE, "'%s' is an unrecognized format function type", name);
1451 add_anchor_token(')');
1452 add_anchor_token(',');
1453 parse_constant_expression();
1454 rem_anchor_token(',');
1455 rem_anchor_token('(');
1458 add_anchor_token(')');
1459 parse_constant_expression();
1460 rem_anchor_token('(');
1464 attribute->u.value = true;
1467 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1469 if (!attribute->have_arguments)
1472 /* should have no arguments */
1473 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1474 eat_until_matching_token('(');
1475 /* we have already consumed '(', so we stop before ')', eat it */
1477 attribute->invalid = true;
1481 * Parse one GNU attribute.
1483 * Note that attribute names can be specified WITH or WITHOUT
1484 * double underscores, ie const or __const__.
1486 * The following attributes are parsed without arguments
1511 * no_instrument_function
1512 * warn_unused_result
1529 * externally_visible
1537 * The following attributes are parsed with arguments
1538 * aligned( const expression )
1539 * alias( string literal )
1540 * section( string literal )
1541 * format( identifier, const expression, const expression )
1542 * format_arg( const expression )
1543 * tls_model( string literal )
1544 * visibility( string literal )
1545 * regparm( const expression )
1546 * model( string leteral )
1547 * trap_exit( const expression )
1548 * sp_switch( string literal )
1550 * The following attributes might have arguments
1551 * weak_ref( string literal )
1552 * non_null( const expression // ',' )
1553 * interrupt( string literal )
1554 * sentinel( constant expression )
1556 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1558 gnu_attribute_t *head = *attributes;
1559 gnu_attribute_t *last = *attributes;
1560 decl_modifiers_t modifiers = 0;
1561 gnu_attribute_t *attribute;
1563 eat(T___attribute__);
1567 if (token.type != ')') {
1568 /* find the end of the list */
1570 while (last->next != NULL)
1574 /* non-empty attribute list */
1577 if (token.type == T_const) {
1579 } else if (token.type == T_volatile) {
1581 } else if (token.type == T_cdecl) {
1582 /* __attribute__((cdecl)), WITH ms mode */
1584 } else if (token.type == T_IDENTIFIER) {
1585 const symbol_t *sym = token.v.symbol;
1588 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1595 for(i = 0; i < GNU_AK_LAST; ++i) {
1596 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1599 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1602 if (kind == GNU_AK_LAST) {
1603 if (warning.attribute)
1604 warningf(HERE, "'%s' attribute directive ignored", name);
1606 /* skip possible arguments */
1607 if (token.type == '(') {
1608 eat_until_matching_token(')');
1611 /* check for arguments */
1612 attribute = allocate_gnu_attribute(kind);
1613 if (token.type == '(') {
1615 if (token.type == ')') {
1616 /* empty args are allowed */
1619 attribute->have_arguments = true;
1624 case GNU_AK_VOLATILE:
1629 case GNU_AK_NOCOMMON:
1631 case GNU_AK_NOTSHARED:
1632 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1633 case GNU_AK_WARN_UNUSED_RESULT:
1634 case GNU_AK_LONGCALL:
1635 case GNU_AK_SHORTCALL:
1636 case GNU_AK_LONG_CALL:
1637 case GNU_AK_SHORT_CALL:
1638 case GNU_AK_FUNCTION_VECTOR:
1639 case GNU_AK_INTERRUPT_HANDLER:
1640 case GNU_AK_NMI_HANDLER:
1641 case GNU_AK_NESTING:
1645 case GNU_AK_EIGTHBIT_DATA:
1646 case GNU_AK_TINY_DATA:
1647 case GNU_AK_SAVEALL:
1648 case GNU_AK_FLATTEN:
1649 case GNU_AK_SSEREGPARM:
1650 case GNU_AK_EXTERNALLY_VISIBLE:
1651 case GNU_AK_RETURN_TWICE:
1652 case GNU_AK_MAY_ALIAS:
1653 case GNU_AK_MS_STRUCT:
1654 case GNU_AK_GCC_STRUCT:
1657 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1658 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1659 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1660 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1661 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1662 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1663 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1664 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1665 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1666 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1667 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1668 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1669 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1670 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1671 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1672 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1673 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1675 case GNU_AK_ALIGNED:
1676 /* __align__ may be used without an argument */
1677 if (attribute->have_arguments) {
1678 parse_gnu_attribute_const_arg(attribute);
1682 case GNU_AK_FORMAT_ARG:
1683 case GNU_AK_REGPARM:
1684 case GNU_AK_TRAP_EXIT:
1685 if (!attribute->have_arguments) {
1686 /* should have arguments */
1687 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1688 attribute->invalid = true;
1690 parse_gnu_attribute_const_arg(attribute);
1693 case GNU_AK_SECTION:
1694 case GNU_AK_SP_SWITCH:
1695 if (!attribute->have_arguments) {
1696 /* should have arguments */
1697 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1698 attribute->invalid = true;
1700 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1703 if (!attribute->have_arguments) {
1704 /* should have arguments */
1705 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1706 attribute->invalid = true;
1708 parse_gnu_attribute_format_args(attribute);
1710 case GNU_AK_WEAKREF:
1711 /* may have one string argument */
1712 if (attribute->have_arguments)
1713 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1715 case GNU_AK_NONNULL:
1716 if (attribute->have_arguments)
1717 parse_gnu_attribute_const_arg_list(attribute);
1719 case GNU_AK_TLS_MODEL:
1720 if (!attribute->have_arguments) {
1721 /* should have arguments */
1722 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1724 parse_gnu_attribute_tls_model_arg(attribute);
1726 case GNU_AK_VISIBILITY:
1727 if (!attribute->have_arguments) {
1728 /* should have arguments */
1729 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1731 parse_gnu_attribute_visibility_arg(attribute);
1734 if (!attribute->have_arguments) {
1735 /* should have arguments */
1736 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1738 parse_gnu_attribute_model_arg(attribute);
1742 if (!attribute->have_arguments) {
1743 /* should have arguments */
1744 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1746 parse_gnu_attribute_mode_arg(attribute);
1749 case GNU_AK_INTERRUPT:
1750 /* may have one string argument */
1751 if (attribute->have_arguments)
1752 parse_gnu_attribute_interrupt_arg(attribute);
1754 case GNU_AK_SENTINEL:
1755 /* may have one string argument */
1756 if (attribute->have_arguments)
1757 parse_gnu_attribute_const_arg(attribute);
1760 /* already handled */
1764 check_no_argument(attribute, name);
1767 if (attribute != NULL) {
1769 last->next = attribute;
1772 head = last = attribute;
1776 if (token.type != ',')
1790 * Parse GNU attributes.
1792 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1794 decl_modifiers_t modifiers = 0;
1797 switch(token.type) {
1798 case T___attribute__:
1799 modifiers |= parse_gnu_attribute(attributes);
1805 if (token.type != T_STRING_LITERAL) {
1806 parse_error_expected("while parsing assembler attribute",
1807 T_STRING_LITERAL, NULL);
1808 eat_until_matching_token('(');
1811 parse_string_literals();
1816 case T_cdecl: modifiers |= DM_CDECL; break;
1817 case T__fastcall: modifiers |= DM_FASTCALL; break;
1818 case T__stdcall: modifiers |= DM_STDCALL; break;
1821 /* TODO record modifier */
1822 warningf(HERE, "Ignoring declaration modifier %K", &token);
1826 default: return modifiers;
1833 static designator_t *parse_designation(void)
1835 designator_t *result = NULL;
1836 designator_t *last = NULL;
1839 designator_t *designator;
1840 switch(token.type) {
1842 designator = allocate_ast_zero(sizeof(designator[0]));
1843 designator->source_position = token.source_position;
1845 add_anchor_token(']');
1846 designator->array_index = parse_constant_expression();
1847 rem_anchor_token(']');
1851 designator = allocate_ast_zero(sizeof(designator[0]));
1852 designator->source_position = token.source_position;
1854 if (token.type != T_IDENTIFIER) {
1855 parse_error_expected("while parsing designator",
1856 T_IDENTIFIER, NULL);
1859 designator->symbol = token.v.symbol;
1867 assert(designator != NULL);
1869 last->next = designator;
1871 result = designator;
1879 static initializer_t *initializer_from_string(array_type_t *type,
1880 const string_t *const string)
1882 /* TODO: check len vs. size of array type */
1885 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1886 initializer->string.string = *string;
1891 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1892 wide_string_t *const string)
1894 /* TODO: check len vs. size of array type */
1897 initializer_t *const initializer =
1898 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1899 initializer->wide_string.string = *string;
1905 * Build an initializer from a given expression.
1907 static initializer_t *initializer_from_expression(type_t *orig_type,
1908 expression_t *expression)
1910 /* TODO check that expression is a constant expression */
1912 /* § 6.7.8.14/15 char array may be initialized by string literals */
1913 type_t *type = skip_typeref(orig_type);
1914 type_t *expr_type_orig = expression->base.type;
1915 type_t *expr_type = skip_typeref(expr_type_orig);
1916 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1917 array_type_t *const array_type = &type->array;
1918 type_t *const element_type = skip_typeref(array_type->element_type);
1920 if (element_type->kind == TYPE_ATOMIC) {
1921 atomic_type_kind_t akind = element_type->atomic.akind;
1922 switch (expression->kind) {
1923 case EXPR_STRING_LITERAL:
1924 if (akind == ATOMIC_TYPE_CHAR
1925 || akind == ATOMIC_TYPE_SCHAR
1926 || akind == ATOMIC_TYPE_UCHAR) {
1927 return initializer_from_string(array_type,
1928 &expression->string.value);
1931 case EXPR_WIDE_STRING_LITERAL: {
1932 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1933 if (get_unqualified_type(element_type) == bare_wchar_type) {
1934 return initializer_from_wide_string(array_type,
1935 &expression->wide_string.value);
1945 assign_error_t error = semantic_assign(type, expression);
1946 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1948 report_assign_error(error, type, expression, "initializer",
1949 &expression->base.source_position);
1951 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1952 result->value.value = create_implicit_cast(expression, type);
1958 * Checks if a given expression can be used as an constant initializer.
1960 static bool is_initializer_constant(const expression_t *expression)
1962 return is_constant_expression(expression)
1963 || is_address_constant(expression);
1967 * Parses an scalar initializer.
1969 * § 6.7.8.11; eat {} without warning
1971 static initializer_t *parse_scalar_initializer(type_t *type,
1972 bool must_be_constant)
1974 /* there might be extra {} hierarchies */
1976 if (token.type == '{') {
1977 warningf(HERE, "extra curly braces around scalar initializer");
1981 } while (token.type == '{');
1984 expression_t *expression = parse_assignment_expression();
1985 if (must_be_constant && !is_initializer_constant(expression)) {
1986 errorf(&expression->base.source_position,
1987 "Initialisation expression '%E' is not constant\n",
1991 initializer_t *initializer = initializer_from_expression(type, expression);
1993 if (initializer == NULL) {
1994 errorf(&expression->base.source_position,
1995 "expression '%E' (type '%T') doesn't match expected type '%T'",
1996 expression, expression->base.type, type);
2001 bool additional_warning_displayed = false;
2002 while (braces > 0) {
2003 if (token.type == ',') {
2006 if (token.type != '}') {
2007 if (!additional_warning_displayed) {
2008 warningf(HERE, "additional elements in scalar initializer");
2009 additional_warning_displayed = true;
2020 * An entry in the type path.
2022 typedef struct type_path_entry_t type_path_entry_t;
2023 struct type_path_entry_t {
2024 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2026 size_t index; /**< For array types: the current index. */
2027 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2032 * A type path expression a position inside compound or array types.
2034 typedef struct type_path_t type_path_t;
2035 struct type_path_t {
2036 type_path_entry_t *path; /**< An flexible array containing the current path. */
2037 type_t *top_type; /**< type of the element the path points */
2038 size_t max_index; /**< largest index in outermost array */
2042 * Prints a type path for debugging.
2044 static __attribute__((unused)) void debug_print_type_path(
2045 const type_path_t *path)
2047 size_t len = ARR_LEN(path->path);
2049 for(size_t i = 0; i < len; ++i) {
2050 const type_path_entry_t *entry = & path->path[i];
2052 type_t *type = skip_typeref(entry->type);
2053 if (is_type_compound(type)) {
2054 /* in gcc mode structs can have no members */
2055 if (entry->v.compound_entry == NULL) {
2059 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2060 } else if (is_type_array(type)) {
2061 fprintf(stderr, "[%zu]", entry->v.index);
2063 fprintf(stderr, "-INVALID-");
2066 if (path->top_type != NULL) {
2067 fprintf(stderr, " (");
2068 print_type(path->top_type);
2069 fprintf(stderr, ")");
2074 * Return the top type path entry, ie. in a path
2075 * (type).a.b returns the b.
2077 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2079 size_t len = ARR_LEN(path->path);
2081 return &path->path[len-1];
2085 * Enlarge the type path by an (empty) element.
2087 static type_path_entry_t *append_to_type_path(type_path_t *path)
2089 size_t len = ARR_LEN(path->path);
2090 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2092 type_path_entry_t *result = & path->path[len];
2093 memset(result, 0, sizeof(result[0]));
2098 * Descending into a sub-type. Enter the scope of the current
2101 static void descend_into_subtype(type_path_t *path)
2103 type_t *orig_top_type = path->top_type;
2104 type_t *top_type = skip_typeref(orig_top_type);
2106 assert(is_type_compound(top_type) || is_type_array(top_type));
2108 type_path_entry_t *top = append_to_type_path(path);
2109 top->type = top_type;
2111 if (is_type_compound(top_type)) {
2112 declaration_t *declaration = top_type->compound.declaration;
2113 declaration_t *entry = declaration->scope.declarations;
2114 top->v.compound_entry = entry;
2116 if (entry != NULL) {
2117 path->top_type = entry->type;
2119 path->top_type = NULL;
2122 assert(is_type_array(top_type));
2125 path->top_type = top_type->array.element_type;
2130 * Pop an entry from the given type path, ie. returning from
2131 * (type).a.b to (type).a
2133 static void ascend_from_subtype(type_path_t *path)
2135 type_path_entry_t *top = get_type_path_top(path);
2137 path->top_type = top->type;
2139 size_t len = ARR_LEN(path->path);
2140 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2144 * Pop entries from the given type path until the given
2145 * path level is reached.
2147 static void ascend_to(type_path_t *path, size_t top_path_level)
2149 size_t len = ARR_LEN(path->path);
2151 while (len > top_path_level) {
2152 ascend_from_subtype(path);
2153 len = ARR_LEN(path->path);
2157 static bool walk_designator(type_path_t *path, const designator_t *designator,
2158 bool used_in_offsetof)
2160 for( ; designator != NULL; designator = designator->next) {
2161 type_path_entry_t *top = get_type_path_top(path);
2162 type_t *orig_type = top->type;
2164 type_t *type = skip_typeref(orig_type);
2166 if (designator->symbol != NULL) {
2167 symbol_t *symbol = designator->symbol;
2168 if (!is_type_compound(type)) {
2169 if (is_type_valid(type)) {
2170 errorf(&designator->source_position,
2171 "'.%Y' designator used for non-compound type '%T'",
2177 declaration_t *declaration = type->compound.declaration;
2178 declaration_t *iter = declaration->scope.declarations;
2179 for( ; iter != NULL; iter = iter->next) {
2180 if (iter->symbol == symbol) {
2185 errorf(&designator->source_position,
2186 "'%T' has no member named '%Y'", orig_type, symbol);
2189 if (used_in_offsetof) {
2190 type_t *real_type = skip_typeref(iter->type);
2191 if (real_type->kind == TYPE_BITFIELD) {
2192 errorf(&designator->source_position,
2193 "offsetof designator '%Y' may not specify bitfield",
2199 top->type = orig_type;
2200 top->v.compound_entry = iter;
2201 orig_type = iter->type;
2203 expression_t *array_index = designator->array_index;
2204 assert(designator->array_index != NULL);
2206 if (!is_type_array(type)) {
2207 if (is_type_valid(type)) {
2208 errorf(&designator->source_position,
2209 "[%E] designator used for non-array type '%T'",
2210 array_index, orig_type);
2214 if (!is_type_valid(array_index->base.type)) {
2218 long index = fold_constant(array_index);
2219 if (!used_in_offsetof) {
2221 errorf(&designator->source_position,
2222 "array index [%E] must be positive", array_index);
2225 if (type->array.size_constant == true) {
2226 long array_size = type->array.size;
2227 if (index >= array_size) {
2228 errorf(&designator->source_position,
2229 "designator [%E] (%d) exceeds array size %d",
2230 array_index, index, array_size);
2236 top->type = orig_type;
2237 top->v.index = (size_t) index;
2238 orig_type = type->array.element_type;
2240 path->top_type = orig_type;
2242 if (designator->next != NULL) {
2243 descend_into_subtype(path);
2252 static void advance_current_object(type_path_t *path, size_t top_path_level)
2254 type_path_entry_t *top = get_type_path_top(path);
2256 type_t *type = skip_typeref(top->type);
2257 if (is_type_union(type)) {
2258 /* in unions only the first element is initialized */
2259 top->v.compound_entry = NULL;
2260 } else if (is_type_struct(type)) {
2261 declaration_t *entry = top->v.compound_entry;
2263 entry = entry->next;
2264 top->v.compound_entry = entry;
2265 if (entry != NULL) {
2266 path->top_type = entry->type;
2270 assert(is_type_array(type));
2274 if (!type->array.size_constant || top->v.index < type->array.size) {
2279 /* we're past the last member of the current sub-aggregate, try if we
2280 * can ascend in the type hierarchy and continue with another subobject */
2281 size_t len = ARR_LEN(path->path);
2283 if (len > top_path_level) {
2284 ascend_from_subtype(path);
2285 advance_current_object(path, top_path_level);
2287 path->top_type = NULL;
2292 * skip until token is found.
2294 static void skip_until(int type)
2296 while (token.type != type) {
2297 if (token.type == T_EOF)
2304 * skip any {...} blocks until a closing bracket is reached.
2306 static void skip_initializers(void)
2308 if (token.type == '{')
2311 while (token.type != '}') {
2312 if (token.type == T_EOF)
2314 if (token.type == '{') {
2322 static initializer_t *create_empty_initializer(void)
2324 static initializer_t empty_initializer
2325 = { .list = { { INITIALIZER_LIST }, 0 } };
2326 return &empty_initializer;
2330 * Parse a part of an initialiser for a struct or union,
2332 static initializer_t *parse_sub_initializer(type_path_t *path,
2333 type_t *outer_type, size_t top_path_level,
2334 parse_initializer_env_t *env)
2336 if (token.type == '}') {
2337 /* empty initializer */
2338 return create_empty_initializer();
2341 type_t *orig_type = path->top_type;
2342 type_t *type = NULL;
2344 if (orig_type == NULL) {
2345 /* We are initializing an empty compound. */
2347 type = skip_typeref(orig_type);
2349 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2350 * initializers in this case. */
2351 if (!is_type_valid(type)) {
2352 skip_initializers();
2353 return create_empty_initializer();
2357 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2360 designator_t *designator = NULL;
2361 if (token.type == '.' || token.type == '[') {
2362 designator = parse_designation();
2363 goto finish_designator;
2364 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2365 /* GNU-style designator ("identifier: value") */
2366 designator = allocate_ast_zero(sizeof(designator[0]));
2367 designator->source_position = token.source_position;
2368 designator->symbol = token.v.symbol;
2373 /* reset path to toplevel, evaluate designator from there */
2374 ascend_to(path, top_path_level);
2375 if (!walk_designator(path, designator, false)) {
2376 /* can't continue after designation error */
2380 initializer_t *designator_initializer
2381 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2382 designator_initializer->designator.designator = designator;
2383 ARR_APP1(initializer_t*, initializers, designator_initializer);
2385 orig_type = path->top_type;
2386 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2391 if (token.type == '{') {
2392 if (type != NULL && is_type_scalar(type)) {
2393 sub = parse_scalar_initializer(type, env->must_be_constant);
2397 if (env->declaration != NULL) {
2398 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2399 env->declaration->symbol);
2401 errorf(HERE, "extra brace group at end of initializer");
2404 descend_into_subtype(path);
2406 add_anchor_token('}');
2407 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2409 rem_anchor_token('}');
2412 ascend_from_subtype(path);
2416 goto error_parse_next;
2420 /* must be an expression */
2421 expression_t *expression = parse_assignment_expression();
2423 if (env->must_be_constant && !is_initializer_constant(expression)) {
2424 errorf(&expression->base.source_position,
2425 "Initialisation expression '%E' is not constant\n",
2430 /* we are already outside, ... */
2434 /* handle { "string" } special case */
2435 if ((expression->kind == EXPR_STRING_LITERAL
2436 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2437 && outer_type != NULL) {
2438 sub = initializer_from_expression(outer_type, expression);
2440 if (token.type == ',') {
2443 if (token.type != '}') {
2444 warningf(HERE, "excessive elements in initializer for type '%T'",
2447 /* TODO: eat , ... */
2452 /* descend into subtypes until expression matches type */
2454 orig_type = path->top_type;
2455 type = skip_typeref(orig_type);
2457 sub = initializer_from_expression(orig_type, expression);
2461 if (!is_type_valid(type)) {
2464 if (is_type_scalar(type)) {
2465 errorf(&expression->base.source_position,
2466 "expression '%E' doesn't match expected type '%T'",
2467 expression, orig_type);
2471 descend_into_subtype(path);
2475 /* update largest index of top array */
2476 const type_path_entry_t *first = &path->path[0];
2477 type_t *first_type = first->type;
2478 first_type = skip_typeref(first_type);
2479 if (is_type_array(first_type)) {
2480 size_t index = first->v.index;
2481 if (index > path->max_index)
2482 path->max_index = index;
2486 /* append to initializers list */
2487 ARR_APP1(initializer_t*, initializers, sub);
2490 if (env->declaration != NULL)
2491 warningf(HERE, "excess elements in struct initializer for '%Y'",
2492 env->declaration->symbol);
2494 warningf(HERE, "excess elements in struct initializer");
2498 if (token.type == '}') {
2502 if (token.type == '}') {
2507 /* advance to the next declaration if we are not at the end */
2508 advance_current_object(path, top_path_level);
2509 orig_type = path->top_type;
2510 if (orig_type != NULL)
2511 type = skip_typeref(orig_type);
2517 size_t len = ARR_LEN(initializers);
2518 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2519 initializer_t *result = allocate_ast_zero(size);
2520 result->kind = INITIALIZER_LIST;
2521 result->list.len = len;
2522 memcpy(&result->list.initializers, initializers,
2523 len * sizeof(initializers[0]));
2525 DEL_ARR_F(initializers);
2526 ascend_to(path, top_path_level+1);
2531 skip_initializers();
2532 DEL_ARR_F(initializers);
2533 ascend_to(path, top_path_level+1);
2538 * Parses an initializer. Parsers either a compound literal
2539 * (env->declaration == NULL) or an initializer of a declaration.
2541 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2543 type_t *type = skip_typeref(env->type);
2544 initializer_t *result = NULL;
2547 if (is_type_scalar(type)) {
2548 result = parse_scalar_initializer(type, env->must_be_constant);
2549 } else if (token.type == '{') {
2553 memset(&path, 0, sizeof(path));
2554 path.top_type = env->type;
2555 path.path = NEW_ARR_F(type_path_entry_t, 0);
2557 descend_into_subtype(&path);
2559 add_anchor_token('}');
2560 result = parse_sub_initializer(&path, env->type, 1, env);
2561 rem_anchor_token('}');
2563 max_index = path.max_index;
2564 DEL_ARR_F(path.path);
2568 /* parse_scalar_initializer() also works in this case: we simply
2569 * have an expression without {} around it */
2570 result = parse_scalar_initializer(type, env->must_be_constant);
2573 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2574 * the array type size */
2575 if (is_type_array(type) && type->array.size_expression == NULL
2576 && result != NULL) {
2578 switch (result->kind) {
2579 case INITIALIZER_LIST:
2580 size = max_index + 1;
2583 case INITIALIZER_STRING:
2584 size = result->string.string.size;
2587 case INITIALIZER_WIDE_STRING:
2588 size = result->wide_string.string.size;
2591 case INITIALIZER_DESIGNATOR:
2592 case INITIALIZER_VALUE:
2593 /* can happen for parse errors */
2598 internal_errorf(HERE, "invalid initializer type");
2601 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2602 cnst->base.type = type_size_t;
2603 cnst->conste.v.int_value = size;
2605 type_t *new_type = duplicate_type(type);
2607 new_type->array.size_expression = cnst;
2608 new_type->array.size_constant = true;
2609 new_type->array.size = size;
2610 env->type = new_type;
2618 static declaration_t *append_declaration(declaration_t *declaration);
2620 static declaration_t *parse_compound_type_specifier(bool is_struct)
2622 gnu_attribute_t *attributes = NULL;
2623 decl_modifiers_t modifiers = 0;
2630 symbol_t *symbol = NULL;
2631 declaration_t *declaration = NULL;
2633 if (token.type == T___attribute__) {
2634 modifiers |= parse_attributes(&attributes);
2637 if (token.type == T_IDENTIFIER) {
2638 symbol = token.v.symbol;
2641 namespace_t const namespc =
2642 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2643 declaration = get_declaration(symbol, namespc);
2644 if (declaration != NULL) {
2645 if (declaration->parent_scope != scope &&
2646 (token.type == '{' || token.type == ';')) {
2648 } else if (declaration->init.complete &&
2649 token.type == '{') {
2650 assert(symbol != NULL);
2651 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2652 is_struct ? "struct" : "union", symbol,
2653 &declaration->source_position);
2654 declaration->scope.declarations = NULL;
2657 } else if (token.type != '{') {
2659 parse_error_expected("while parsing struct type specifier",
2660 T_IDENTIFIER, '{', NULL);
2662 parse_error_expected("while parsing union type specifier",
2663 T_IDENTIFIER, '{', NULL);
2669 if (declaration == NULL) {
2670 declaration = allocate_declaration_zero();
2671 declaration->namespc =
2672 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2673 declaration->source_position = token.source_position;
2674 declaration->symbol = symbol;
2675 declaration->parent_scope = scope;
2676 if (symbol != NULL) {
2677 environment_push(declaration);
2679 append_declaration(declaration);
2682 if (token.type == '{') {
2683 declaration->init.complete = true;
2685 parse_compound_type_entries(declaration);
2686 modifiers |= parse_attributes(&attributes);
2689 declaration->modifiers |= modifiers;
2693 static void parse_enum_entries(type_t *const enum_type)
2697 if (token.type == '}') {
2699 errorf(HERE, "empty enum not allowed");
2703 add_anchor_token('}');
2705 if (token.type != T_IDENTIFIER) {
2706 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2708 rem_anchor_token('}');
2712 declaration_t *const entry = allocate_declaration_zero();
2713 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2714 entry->type = enum_type;
2715 entry->symbol = token.v.symbol;
2716 entry->source_position = token.source_position;
2719 if (token.type == '=') {
2721 expression_t *value = parse_constant_expression();
2723 value = create_implicit_cast(value, enum_type);
2724 entry->init.enum_value = value;
2729 record_declaration(entry);
2731 if (token.type != ',')
2734 } while (token.type != '}');
2735 rem_anchor_token('}');
2743 static type_t *parse_enum_specifier(void)
2745 gnu_attribute_t *attributes = NULL;
2746 declaration_t *declaration;
2750 if (token.type == T_IDENTIFIER) {
2751 symbol = token.v.symbol;
2754 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2755 } else if (token.type != '{') {
2756 parse_error_expected("while parsing enum type specifier",
2757 T_IDENTIFIER, '{', NULL);
2764 if (declaration == NULL) {
2765 declaration = allocate_declaration_zero();
2766 declaration->namespc = NAMESPACE_ENUM;
2767 declaration->source_position = token.source_position;
2768 declaration->symbol = symbol;
2769 declaration->parent_scope = scope;
2772 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2773 type->enumt.declaration = declaration;
2775 if (token.type == '{') {
2776 if (declaration->init.complete) {
2777 errorf(HERE, "multiple definitions of enum %Y", symbol);
2779 if (symbol != NULL) {
2780 environment_push(declaration);
2782 append_declaration(declaration);
2783 declaration->init.complete = true;
2785 parse_enum_entries(type);
2786 parse_attributes(&attributes);
2793 * if a symbol is a typedef to another type, return true
2795 static bool is_typedef_symbol(symbol_t *symbol)
2797 const declaration_t *const declaration =
2798 get_declaration(symbol, NAMESPACE_NORMAL);
2800 declaration != NULL &&
2801 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2804 static type_t *parse_typeof(void)
2811 add_anchor_token(')');
2813 expression_t *expression = NULL;
2816 switch(token.type) {
2817 case T___extension__:
2818 /* This can be a prefix to a typename or an expression. We simply eat
2822 } while (token.type == T___extension__);
2826 if (is_typedef_symbol(token.v.symbol)) {
2827 type = parse_typename();
2829 expression = parse_expression();
2830 type = expression->base.type;
2835 type = parse_typename();
2839 expression = parse_expression();
2840 type = expression->base.type;
2844 rem_anchor_token(')');
2847 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2848 typeof_type->typeoft.expression = expression;
2849 typeof_type->typeoft.typeof_type = type;
2856 typedef enum specifiers_t {
2857 SPECIFIER_SIGNED = 1 << 0,
2858 SPECIFIER_UNSIGNED = 1 << 1,
2859 SPECIFIER_LONG = 1 << 2,
2860 SPECIFIER_INT = 1 << 3,
2861 SPECIFIER_DOUBLE = 1 << 4,
2862 SPECIFIER_CHAR = 1 << 5,
2863 SPECIFIER_SHORT = 1 << 6,
2864 SPECIFIER_LONG_LONG = 1 << 7,
2865 SPECIFIER_FLOAT = 1 << 8,
2866 SPECIFIER_BOOL = 1 << 9,
2867 SPECIFIER_VOID = 1 << 10,
2868 SPECIFIER_INT8 = 1 << 11,
2869 SPECIFIER_INT16 = 1 << 12,
2870 SPECIFIER_INT32 = 1 << 13,
2871 SPECIFIER_INT64 = 1 << 14,
2872 SPECIFIER_INT128 = 1 << 15,
2873 SPECIFIER_COMPLEX = 1 << 16,
2874 SPECIFIER_IMAGINARY = 1 << 17,
2877 static type_t *create_builtin_type(symbol_t *const symbol,
2878 type_t *const real_type)
2880 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2881 type->builtin.symbol = symbol;
2882 type->builtin.real_type = real_type;
2884 type_t *result = typehash_insert(type);
2885 if (type != result) {
2892 static type_t *get_typedef_type(symbol_t *symbol)
2894 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2895 if (declaration == NULL ||
2896 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2899 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2900 type->typedeft.declaration = declaration;
2906 * check for the allowed MS alignment values.
2908 static bool check_alignment_value(long long intvalue)
2910 if (intvalue < 1 || intvalue > 8192) {
2911 errorf(HERE, "illegal alignment value");
2914 unsigned v = (unsigned)intvalue;
2915 for(unsigned i = 1; i <= 8192; i += i) {
2919 errorf(HERE, "alignment must be power of two");
2923 #define DET_MOD(name, tag) do { \
2924 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2925 *modifiers |= tag; \
2928 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2930 decl_modifiers_t *modifiers = &specifiers->modifiers;
2933 if (token.type == T_restrict) {
2935 DET_MOD(restrict, DM_RESTRICT);
2937 } else if (token.type != T_IDENTIFIER)
2939 symbol_t *symbol = token.v.symbol;
2940 if (symbol == sym_align) {
2943 if (token.type != T_INTEGER)
2945 if (check_alignment_value(token.v.intvalue)) {
2946 if (specifiers->alignment != 0)
2947 warningf(HERE, "align used more than once");
2948 specifiers->alignment = (unsigned char)token.v.intvalue;
2952 } else if (symbol == sym_allocate) {
2955 if (token.type != T_IDENTIFIER)
2957 (void)token.v.symbol;
2959 } else if (symbol == sym_dllimport) {
2961 DET_MOD(dllimport, DM_DLLIMPORT);
2962 } else if (symbol == sym_dllexport) {
2964 DET_MOD(dllexport, DM_DLLEXPORT);
2965 } else if (symbol == sym_thread) {
2967 DET_MOD(thread, DM_THREAD);
2968 } else if (symbol == sym_naked) {
2970 DET_MOD(naked, DM_NAKED);
2971 } else if (symbol == sym_noinline) {
2973 DET_MOD(noinline, DM_NOINLINE);
2974 } else if (symbol == sym_noreturn) {
2976 DET_MOD(noreturn, DM_NORETURN);
2977 } else if (symbol == sym_nothrow) {
2979 DET_MOD(nothrow, DM_NOTHROW);
2980 } else if (symbol == sym_novtable) {
2982 DET_MOD(novtable, DM_NOVTABLE);
2983 } else if (symbol == sym_property) {
2987 bool is_get = false;
2988 if (token.type != T_IDENTIFIER)
2990 if (token.v.symbol == sym_get) {
2992 } else if (token.v.symbol == sym_put) {
2994 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2999 if (token.type != T_IDENTIFIER)
3002 if (specifiers->get_property_sym != NULL) {
3003 errorf(HERE, "get property name already specified");
3005 specifiers->get_property_sym = token.v.symbol;
3008 if (specifiers->put_property_sym != NULL) {
3009 errorf(HERE, "put property name already specified");
3011 specifiers->put_property_sym = token.v.symbol;
3015 if (token.type == ',') {
3022 } else if (symbol == sym_selectany) {
3024 DET_MOD(selectany, DM_SELECTANY);
3025 } else if (symbol == sym_uuid) {
3028 if (token.type != T_STRING_LITERAL)
3032 } else if (symbol == sym_deprecated) {
3034 if (specifiers->deprecated != 0)
3035 warningf(HERE, "deprecated used more than once");
3036 specifiers->deprecated = 1;
3037 if (token.type == '(') {
3039 if (token.type == T_STRING_LITERAL) {
3040 specifiers->deprecated_string = token.v.string.begin;
3043 errorf(HERE, "string literal expected");
3047 } else if (symbol == sym_noalias) {
3049 DET_MOD(noalias, DM_NOALIAS);
3051 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3053 if (token.type == '(')
3057 if (token.type == ',')
3064 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3066 declaration_t *const decl = allocate_declaration_zero();
3067 decl->source_position = *HERE;
3068 decl->storage_class =
3069 storage_class != STORAGE_CLASS_NONE || scope == global_scope ?
3070 storage_class : STORAGE_CLASS_AUTO;
3071 decl->declared_storage_class = decl->storage_class;
3072 decl->symbol = symbol;
3073 decl->implicit = true;
3074 record_declaration(decl);
3078 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3080 type_t *type = NULL;
3081 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3082 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3083 unsigned type_specifiers = 0;
3084 bool newtype = false;
3085 bool saw_error = false;
3087 specifiers->source_position = token.source_position;
3090 specifiers->modifiers
3091 |= parse_attributes(&specifiers->gnu_attributes);
3092 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3093 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3095 switch(token.type) {
3098 #define MATCH_STORAGE_CLASS(token, class) \
3100 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3101 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3103 specifiers->declared_storage_class = class; \
3107 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3108 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3109 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3110 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3111 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3116 add_anchor_token(')');
3117 parse_microsoft_extended_decl_modifier(specifiers);
3118 rem_anchor_token(')');
3123 switch (specifiers->declared_storage_class) {
3124 case STORAGE_CLASS_NONE:
3125 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3128 case STORAGE_CLASS_EXTERN:
3129 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3132 case STORAGE_CLASS_STATIC:
3133 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3137 errorf(HERE, "multiple storage classes in declaration specifiers");
3143 /* type qualifiers */
3144 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3146 qualifiers |= qualifier; \
3150 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3151 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3152 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3153 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3154 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3155 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3156 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3157 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3159 case T___extension__:
3164 /* type specifiers */
3165 #define MATCH_SPECIFIER(token, specifier, name) \
3168 if (type_specifiers & specifier) { \
3169 errorf(HERE, "multiple " name " type specifiers given"); \
3171 type_specifiers |= specifier; \
3175 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3176 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3177 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3178 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3179 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3180 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3181 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3182 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3183 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3184 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3185 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3186 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3187 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3188 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3189 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3190 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3192 case T__forceinline:
3193 /* only in microsoft mode */
3194 specifiers->modifiers |= DM_FORCEINLINE;
3199 specifiers->is_inline = true;
3204 if (type_specifiers & SPECIFIER_LONG_LONG) {
3205 errorf(HERE, "multiple type specifiers given");
3206 } else if (type_specifiers & SPECIFIER_LONG) {
3207 type_specifiers |= SPECIFIER_LONG_LONG;
3209 type_specifiers |= SPECIFIER_LONG;
3214 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3216 type->compound.declaration = parse_compound_type_specifier(true);
3220 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3221 type->compound.declaration = parse_compound_type_specifier(false);
3222 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3223 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3227 type = parse_enum_specifier();
3230 type = parse_typeof();
3232 case T___builtin_va_list:
3233 type = duplicate_type(type_valist);
3237 case T_IDENTIFIER: {
3238 /* only parse identifier if we haven't found a type yet */
3239 if (type != NULL || type_specifiers != 0) {
3240 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3241 * declaration, so it doesn't generate errors about expecting '(' or
3243 switch (look_ahead(1)->type) {
3250 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3253 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3258 goto finish_specifiers;
3262 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3263 if (typedef_type == NULL) {
3264 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3265 * declaration, so it doesn't generate 'implicit int' followed by more
3266 * errors later on. */
3267 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3272 errorf(HERE, "%K does not name a type", &token);
3274 declaration_t *const decl =
3275 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3277 type = allocate_type_zero(TYPE_TYPEDEF, HERE);
3278 type->typedeft.declaration = decl;
3282 if (la1_type == '*')
3283 goto finish_specifiers;
3288 goto finish_specifiers;
3293 type = typedef_type;
3297 /* function specifier */
3299 goto finish_specifiers;
3304 if (type == NULL || (saw_error && type_specifiers != 0)) {
3305 atomic_type_kind_t atomic_type;
3307 /* match valid basic types */
3308 switch(type_specifiers) {
3309 case SPECIFIER_VOID:
3310 atomic_type = ATOMIC_TYPE_VOID;
3312 case SPECIFIER_CHAR:
3313 atomic_type = ATOMIC_TYPE_CHAR;
3315 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3316 atomic_type = ATOMIC_TYPE_SCHAR;
3318 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3319 atomic_type = ATOMIC_TYPE_UCHAR;
3321 case SPECIFIER_SHORT:
3322 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3323 case SPECIFIER_SHORT | SPECIFIER_INT:
3324 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3325 atomic_type = ATOMIC_TYPE_SHORT;
3327 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3328 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3329 atomic_type = ATOMIC_TYPE_USHORT;
3332 case SPECIFIER_SIGNED:
3333 case SPECIFIER_SIGNED | SPECIFIER_INT:
3334 atomic_type = ATOMIC_TYPE_INT;
3336 case SPECIFIER_UNSIGNED:
3337 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3338 atomic_type = ATOMIC_TYPE_UINT;
3340 case SPECIFIER_LONG:
3341 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3342 case SPECIFIER_LONG | SPECIFIER_INT:
3343 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3344 atomic_type = ATOMIC_TYPE_LONG;
3346 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3347 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3348 atomic_type = ATOMIC_TYPE_ULONG;
3351 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3352 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3353 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3354 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3356 atomic_type = ATOMIC_TYPE_LONGLONG;
3357 goto warn_about_long_long;
3359 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3360 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3362 atomic_type = ATOMIC_TYPE_ULONGLONG;
3363 warn_about_long_long:
3364 if (warning.long_long) {
3365 warningf(&specifiers->source_position,
3366 "ISO C90 does not support 'long long'");
3370 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3371 atomic_type = unsigned_int8_type_kind;
3374 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3375 atomic_type = unsigned_int16_type_kind;
3378 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3379 atomic_type = unsigned_int32_type_kind;
3382 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3383 atomic_type = unsigned_int64_type_kind;
3386 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3387 atomic_type = unsigned_int128_type_kind;
3390 case SPECIFIER_INT8:
3391 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3392 atomic_type = int8_type_kind;
3395 case SPECIFIER_INT16:
3396 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3397 atomic_type = int16_type_kind;
3400 case SPECIFIER_INT32:
3401 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3402 atomic_type = int32_type_kind;
3405 case SPECIFIER_INT64:
3406 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3407 atomic_type = int64_type_kind;
3410 case SPECIFIER_INT128:
3411 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3412 atomic_type = int128_type_kind;
3415 case SPECIFIER_FLOAT:
3416 atomic_type = ATOMIC_TYPE_FLOAT;
3418 case SPECIFIER_DOUBLE:
3419 atomic_type = ATOMIC_TYPE_DOUBLE;
3421 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3422 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3424 case SPECIFIER_BOOL:
3425 atomic_type = ATOMIC_TYPE_BOOL;
3427 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3428 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3429 atomic_type = ATOMIC_TYPE_FLOAT;
3431 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3432 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3433 atomic_type = ATOMIC_TYPE_DOUBLE;
3435 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3436 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3437 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3440 /* invalid specifier combination, give an error message */
3441 if (type_specifiers == 0) {
3443 specifiers->type = type_error_type;
3448 if (warning.implicit_int) {
3449 warningf(HERE, "no type specifiers in declaration, using 'int'");
3451 atomic_type = ATOMIC_TYPE_INT;
3454 errorf(HERE, "no type specifiers given in declaration");
3456 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3457 (type_specifiers & SPECIFIER_UNSIGNED)) {
3458 errorf(HERE, "signed and unsigned specifiers given");
3459 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3460 errorf(HERE, "only integer types can be signed or unsigned");
3462 errorf(HERE, "multiple datatypes in declaration");
3464 atomic_type = ATOMIC_TYPE_INVALID;
3467 if (type_specifiers & SPECIFIER_COMPLEX &&
3468 atomic_type != ATOMIC_TYPE_INVALID) {
3469 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3470 type->complex.akind = atomic_type;
3471 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3472 atomic_type != ATOMIC_TYPE_INVALID) {
3473 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3474 type->imaginary.akind = atomic_type;
3476 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3477 type->atomic.akind = atomic_type;
3480 } else if (type_specifiers != 0) {
3481 errorf(HERE, "multiple datatypes in declaration");
3484 /* FIXME: check type qualifiers here */
3486 type->base.qualifiers = qualifiers;
3487 type->base.modifiers = modifiers;
3489 type_t *result = typehash_insert(type);
3490 if (newtype && result != type) {
3494 specifiers->type = result;
3499 static type_qualifiers_t parse_type_qualifiers(void)
3501 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3504 switch(token.type) {
3505 /* type qualifiers */
3506 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3507 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3508 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3509 /* microsoft extended type modifiers */
3510 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3511 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3512 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3513 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3514 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3522 static declaration_t *parse_identifier_list(void)
3524 declaration_t *declarations = NULL;
3525 declaration_t *last_declaration = NULL;
3527 declaration_t *const declaration = allocate_declaration_zero();
3528 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3529 declaration->source_position = token.source_position;
3530 declaration->symbol = token.v.symbol;
3533 if (last_declaration != NULL) {
3534 last_declaration->next = declaration;
3536 declarations = declaration;
3538 last_declaration = declaration;
3540 if (token.type != ',') {
3544 } while (token.type == T_IDENTIFIER);
3546 return declarations;
3549 static type_t *automatic_type_conversion(type_t *orig_type);
3551 static void semantic_parameter(declaration_t *declaration)
3553 /* TODO: improve error messages */
3554 source_position_t const* const pos = &declaration->source_position;
3556 switch (declaration->declared_storage_class) {
3557 case STORAGE_CLASS_TYPEDEF:
3558 errorf(pos, "typedef not allowed in parameter list");
3561 /* Allowed storage classes */
3562 case STORAGE_CLASS_NONE:
3563 case STORAGE_CLASS_REGISTER:
3567 errorf(pos, "parameter may only have none or register storage class");
3571 type_t *const orig_type = declaration->type;
3572 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3573 * sugar. Turn it into a pointer.
3574 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3575 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3577 type_t *const type = automatic_type_conversion(orig_type);
3578 declaration->type = type;
3580 if (is_type_incomplete(skip_typeref(type))) {
3581 errorf(pos, "incomplete type '%T' not allowed for parameter '%Y'",
3582 orig_type, declaration->symbol);
3586 static declaration_t *parse_parameter(void)
3588 declaration_specifiers_t specifiers;
3589 memset(&specifiers, 0, sizeof(specifiers));
3591 parse_declaration_specifiers(&specifiers);
3593 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3598 static declaration_t *parse_parameters(function_type_t *type)
3600 declaration_t *declarations = NULL;
3603 add_anchor_token(')');
3604 int saved_comma_state = save_and_reset_anchor_state(',');
3606 if (token.type == T_IDENTIFIER &&
3607 !is_typedef_symbol(token.v.symbol)) {
3608 token_type_t la1_type = look_ahead(1)->type;
3609 if (la1_type == ',' || la1_type == ')') {
3610 type->kr_style_parameters = true;
3611 declarations = parse_identifier_list();
3612 goto parameters_finished;
3616 if (token.type == ')') {
3617 type->unspecified_parameters = 1;
3618 goto parameters_finished;
3621 declaration_t *declaration;
3622 declaration_t *last_declaration = NULL;
3623 function_parameter_t *parameter;
3624 function_parameter_t *last_parameter = NULL;
3627 switch(token.type) {
3631 goto parameters_finished;
3634 case T___extension__:
3636 declaration = parse_parameter();
3638 /* func(void) is not a parameter */
3639 if (last_parameter == NULL
3640 && token.type == ')'
3641 && declaration->symbol == NULL
3642 && skip_typeref(declaration->type) == type_void) {
3643 goto parameters_finished;
3645 semantic_parameter(declaration);
3647 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3648 memset(parameter, 0, sizeof(parameter[0]));
3649 parameter->type = declaration->type;
3651 if (last_parameter != NULL) {
3652 last_declaration->next = declaration;
3653 last_parameter->next = parameter;
3655 type->parameters = parameter;
3656 declarations = declaration;
3658 last_parameter = parameter;
3659 last_declaration = declaration;
3663 goto parameters_finished;
3665 if (token.type != ',') {
3666 goto parameters_finished;
3672 parameters_finished:
3673 rem_anchor_token(')');
3676 restore_anchor_state(',', saved_comma_state);
3677 return declarations;
3680 restore_anchor_state(',', saved_comma_state);
3684 typedef enum construct_type_kind_t {
3689 } construct_type_kind_t;
3691 typedef struct construct_type_t construct_type_t;
3692 struct construct_type_t {
3693 construct_type_kind_t kind;
3694 construct_type_t *next;
3697 typedef struct parsed_pointer_t parsed_pointer_t;
3698 struct parsed_pointer_t {
3699 construct_type_t construct_type;
3700 type_qualifiers_t type_qualifiers;
3703 typedef struct construct_function_type_t construct_function_type_t;
3704 struct construct_function_type_t {
3705 construct_type_t construct_type;
3706 type_t *function_type;
3709 typedef struct parsed_array_t parsed_array_t;
3710 struct parsed_array_t {
3711 construct_type_t construct_type;
3712 type_qualifiers_t type_qualifiers;
3718 typedef struct construct_base_type_t construct_base_type_t;
3719 struct construct_base_type_t {
3720 construct_type_t construct_type;
3724 static construct_type_t *parse_pointer_declarator(void)
3728 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3729 memset(pointer, 0, sizeof(pointer[0]));
3730 pointer->construct_type.kind = CONSTRUCT_POINTER;
3731 pointer->type_qualifiers = parse_type_qualifiers();
3733 return (construct_type_t*) pointer;
3736 static construct_type_t *parse_array_declarator(void)
3739 add_anchor_token(']');
3741 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3742 memset(array, 0, sizeof(array[0]));
3743 array->construct_type.kind = CONSTRUCT_ARRAY;
3745 if (token.type == T_static) {
3746 array->is_static = true;
3750 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3751 if (type_qualifiers != 0) {
3752 if (token.type == T_static) {
3753 array->is_static = true;
3757 array->type_qualifiers = type_qualifiers;
3759 if (token.type == '*' && look_ahead(1)->type == ']') {
3760 array->is_variable = true;
3762 } else if (token.type != ']') {
3763 array->size = parse_assignment_expression();
3766 rem_anchor_token(']');
3769 return (construct_type_t*) array;
3774 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3777 if (declaration != NULL) {
3778 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3780 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3782 if (mask & (mask-1)) {
3783 const char *first = NULL, *second = NULL;
3785 /* more than one calling convention set */
3786 if (declaration->modifiers & DM_CDECL) {
3787 if (first == NULL) first = "cdecl";
3788 else if (second == NULL) second = "cdecl";
3790 if (declaration->modifiers & DM_STDCALL) {
3791 if (first == NULL) first = "stdcall";
3792 else if (second == NULL) second = "stdcall";
3794 if (declaration->modifiers & DM_FASTCALL) {
3795 if (first == NULL) first = "fastcall";
3796 else if (second == NULL) second = "fastcall";
3798 if (declaration->modifiers & DM_THISCALL) {
3799 if (first == NULL) first = "thiscall";
3800 else if (second == NULL) second = "thiscall";
3802 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3805 if (declaration->modifiers & DM_CDECL)
3806 type->function.calling_convention = CC_CDECL;
3807 else if (declaration->modifiers & DM_STDCALL)
3808 type->function.calling_convention = CC_STDCALL;
3809 else if (declaration->modifiers & DM_FASTCALL)
3810 type->function.calling_convention = CC_FASTCALL;
3811 else if (declaration->modifiers & DM_THISCALL)
3812 type->function.calling_convention = CC_THISCALL;
3814 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3817 declaration_t *parameters = parse_parameters(&type->function);
3818 if (declaration != NULL) {
3819 declaration->scope.declarations = parameters;
3822 construct_function_type_t *construct_function_type =
3823 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3824 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3825 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3826 construct_function_type->function_type = type;
3828 return &construct_function_type->construct_type;
3831 static void fix_declaration_type(declaration_t *declaration)
3833 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3834 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3836 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3837 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3839 if (declaration->type->base.modifiers == type_modifiers)
3842 type_t *copy = duplicate_type(declaration->type);
3843 copy->base.modifiers = type_modifiers;
3845 type_t *result = typehash_insert(copy);
3846 if (result != copy) {
3847 obstack_free(type_obst, copy);
3850 declaration->type = result;
3853 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3854 bool may_be_abstract)
3856 /* construct a single linked list of construct_type_t's which describe
3857 * how to construct the final declarator type */
3858 construct_type_t *first = NULL;
3859 construct_type_t *last = NULL;
3860 gnu_attribute_t *attributes = NULL;
3862 decl_modifiers_t modifiers = parse_attributes(&attributes);
3865 while (token.type == '*') {
3866 construct_type_t *type = parse_pointer_declarator();
3876 /* TODO: find out if this is correct */
3877 modifiers |= parse_attributes(&attributes);
3880 if (declaration != NULL)
3881 declaration->modifiers |= modifiers;
3883 construct_type_t *inner_types = NULL;
3885 switch(token.type) {
3887 if (declaration == NULL) {
3888 errorf(HERE, "no identifier expected in typename");
3890 declaration->symbol = token.v.symbol;
3891 declaration->source_position = token.source_position;
3897 add_anchor_token(')');
3898 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3899 /* All later declarators only modify the return type, not declaration */
3901 rem_anchor_token(')');
3905 if (may_be_abstract)
3907 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3908 /* avoid a loop in the outermost scope, because eat_statement doesn't
3910 if (token.type == '}' && current_function == NULL) {
3918 construct_type_t *p = last;
3921 construct_type_t *type;
3922 switch(token.type) {
3924 type = parse_function_declarator(declaration);
3927 type = parse_array_declarator();
3930 goto declarator_finished;
3933 /* insert in the middle of the list (behind p) */
3935 type->next = p->next;
3946 declarator_finished:
3947 /* append inner_types at the end of the list, we don't to set last anymore
3948 * as it's not needed anymore */
3950 assert(first == NULL);
3951 first = inner_types;
3953 last->next = inner_types;
3961 static void parse_declaration_attributes(declaration_t *declaration)
3963 gnu_attribute_t *attributes = NULL;
3964 decl_modifiers_t modifiers = parse_attributes(&attributes);
3966 if (declaration == NULL)
3969 declaration->modifiers |= modifiers;
3970 /* check if we have these stupid mode attributes... */
3971 type_t *old_type = declaration->type;
3972 if (old_type == NULL)
3975 gnu_attribute_t *attribute = attributes;
3976 for ( ; attribute != NULL; attribute = attribute->next) {
3977 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3980 atomic_type_kind_t akind = attribute->u.akind;
3981 if (!is_type_signed(old_type)) {
3983 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3984 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3985 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3986 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3988 panic("invalid akind in mode attribute");
3992 = make_atomic_type(akind, old_type->base.qualifiers);
3996 static type_t *construct_declarator_type(construct_type_t *construct_list,
3999 construct_type_t *iter = construct_list;
4000 for( ; iter != NULL; iter = iter->next) {
4001 switch(iter->kind) {
4002 case CONSTRUCT_INVALID:
4003 internal_errorf(HERE, "invalid type construction found");
4004 case CONSTRUCT_FUNCTION: {
4005 construct_function_type_t *construct_function_type
4006 = (construct_function_type_t*) iter;
4008 type_t *function_type = construct_function_type->function_type;
4010 function_type->function.return_type = type;
4012 type_t *skipped_return_type = skip_typeref(type);
4013 if (is_type_function(skipped_return_type)) {
4014 errorf(HERE, "function returning function is not allowed");
4015 type = type_error_type;
4016 } else if (is_type_array(skipped_return_type)) {
4017 errorf(HERE, "function returning array is not allowed");
4018 type = type_error_type;
4020 type = function_type;
4025 case CONSTRUCT_POINTER: {
4026 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4027 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
4028 pointer_type->pointer.points_to = type;
4029 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
4031 type = pointer_type;
4035 case CONSTRUCT_ARRAY: {
4036 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4037 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
4039 expression_t *size_expression = parsed_array->size;
4040 if (size_expression != NULL) {
4042 = create_implicit_cast(size_expression, type_size_t);
4045 array_type->base.qualifiers = parsed_array->type_qualifiers;
4046 array_type->array.element_type = type;
4047 array_type->array.is_static = parsed_array->is_static;
4048 array_type->array.is_variable = parsed_array->is_variable;
4049 array_type->array.size_expression = size_expression;
4051 if (size_expression != NULL) {
4052 if (is_constant_expression(size_expression)) {
4053 array_type->array.size_constant = true;
4054 array_type->array.size
4055 = fold_constant(size_expression);
4057 array_type->array.is_vla = true;
4061 type_t *skipped_type = skip_typeref(type);
4062 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
4063 errorf(HERE, "array of void is not allowed");
4064 type = type_error_type;
4072 type_t *hashed_type = typehash_insert(type);
4073 if (hashed_type != type) {
4074 /* the function type was constructed earlier freeing it here will
4075 * destroy other types... */
4076 if (iter->kind != CONSTRUCT_FUNCTION) {
4086 static declaration_t *parse_declarator(
4087 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4089 declaration_t *const declaration = allocate_declaration_zero();
4090 declaration->source_position = specifiers->source_position;
4091 declaration->declared_storage_class = specifiers->declared_storage_class;
4092 declaration->modifiers = specifiers->modifiers;
4093 declaration->deprecated_string = specifiers->deprecated_string;
4094 declaration->get_property_sym = specifiers->get_property_sym;
4095 declaration->put_property_sym = specifiers->put_property_sym;
4096 declaration->is_inline = specifiers->is_inline;
4098 declaration->storage_class = specifiers->declared_storage_class;
4099 if (declaration->storage_class == STORAGE_CLASS_NONE
4100 && scope != global_scope) {
4101 declaration->storage_class = STORAGE_CLASS_AUTO;
4104 if (specifiers->alignment != 0) {
4105 /* TODO: add checks here */
4106 declaration->alignment = specifiers->alignment;
4109 construct_type_t *construct_type
4110 = parse_inner_declarator(declaration, may_be_abstract);
4111 type_t *const type = specifiers->type;
4112 declaration->type = construct_declarator_type(construct_type, type);
4114 parse_declaration_attributes(declaration);
4116 fix_declaration_type(declaration);
4118 if (construct_type != NULL) {
4119 obstack_free(&temp_obst, construct_type);
4125 static type_t *parse_abstract_declarator(type_t *base_type)
4127 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4129 type_t *result = construct_declarator_type(construct_type, base_type);
4130 if (construct_type != NULL) {
4131 obstack_free(&temp_obst, construct_type);
4137 static declaration_t *append_declaration(declaration_t* const declaration)
4139 if (last_declaration != NULL) {
4140 last_declaration->next = declaration;
4142 scope->declarations = declaration;
4144 last_declaration = declaration;
4149 * Check if the declaration of main is suspicious. main should be a
4150 * function with external linkage, returning int, taking either zero
4151 * arguments, two, or three arguments of appropriate types, ie.
4153 * int main([ int argc, char **argv [, char **env ] ]).
4155 * @param decl the declaration to check
4156 * @param type the function type of the declaration
4158 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4160 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4161 warningf(&decl->source_position,
4162 "'main' is normally a non-static function");
4164 if (skip_typeref(func_type->return_type) != type_int) {
4165 warningf(&decl->source_position,
4166 "return type of 'main' should be 'int', but is '%T'",
4167 func_type->return_type);
4169 const function_parameter_t *parm = func_type->parameters;
4171 type_t *const first_type = parm->type;
4172 if (!types_compatible(skip_typeref(first_type), type_int)) {
4173 warningf(&decl->source_position,
4174 "first argument of 'main' should be 'int', but is '%T'", first_type);
4178 type_t *const second_type = parm->type;
4179 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4180 warningf(&decl->source_position,
4181 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4185 type_t *const third_type = parm->type;
4186 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4187 warningf(&decl->source_position,
4188 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4192 goto warn_arg_count;
4196 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4202 * Check if a symbol is the equal to "main".
4204 static bool is_sym_main(const symbol_t *const sym)
4206 return strcmp(sym->string, "main") == 0;
4209 static declaration_t *internal_record_declaration(
4210 declaration_t *const declaration,
4211 const bool is_definition)
4213 const symbol_t *const symbol = declaration->symbol;
4214 const namespace_t namespc = (namespace_t)declaration->namespc;
4216 assert(symbol != NULL);
4217 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4219 type_t *const orig_type = declaration->type;
4220 type_t *const type = skip_typeref(orig_type);
4221 if (is_type_function(type) &&
4222 type->function.unspecified_parameters &&
4223 warning.strict_prototypes &&
4224 previous_declaration == NULL) {
4225 warningf(&declaration->source_position,
4226 "function declaration '%#T' is not a prototype",
4227 orig_type, declaration->symbol);
4230 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4231 check_type_of_main(declaration, &type->function);
4234 if (warning.nested_externs &&
4235 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4236 scope != global_scope) {
4237 warningf(&declaration->source_position,
4238 "nested extern declaration of '%#T'", declaration->type, symbol);
4241 assert(declaration != previous_declaration);
4242 if (previous_declaration != NULL
4243 && previous_declaration->parent_scope == scope) {
4244 /* can happen for K&R style declarations */
4245 if (previous_declaration->type == NULL) {
4246 previous_declaration->type = declaration->type;
4249 const type_t *prev_type = skip_typeref(previous_declaration->type);
4250 if (!types_compatible(type, prev_type)) {
4251 errorf(&declaration->source_position,
4252 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4253 orig_type, symbol, previous_declaration->type, symbol,
4254 &previous_declaration->source_position);
4256 unsigned old_storage_class = previous_declaration->storage_class;
4257 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4258 errorf(&declaration->source_position,
4259 "redeclaration of enum entry '%Y' (declared %P)",
4260 symbol, &previous_declaration->source_position);
4261 return previous_declaration;
4264 if (warning.redundant_decls &&
4266 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4267 !(previous_declaration->modifiers & DM_USED) &&
4268 !previous_declaration->used) {
4269 warningf(&previous_declaration->source_position,
4270 "unnecessary static forward declaration for '%#T'",
4271 previous_declaration->type, symbol);
4274 unsigned new_storage_class = declaration->storage_class;
4276 if (is_type_incomplete(prev_type)) {
4277 previous_declaration->type = type;
4281 /* pretend no storage class means extern for function
4282 * declarations (except if the previous declaration is neither
4283 * none nor extern) */
4284 if (is_type_function(type)) {
4285 if (prev_type->function.unspecified_parameters) {
4286 previous_declaration->type = type;
4290 switch (old_storage_class) {
4291 case STORAGE_CLASS_NONE:
4292 old_storage_class = STORAGE_CLASS_EXTERN;
4295 case STORAGE_CLASS_EXTERN:
4296 if (is_definition) {
4297 if (warning.missing_prototypes &&
4298 prev_type->function.unspecified_parameters &&
4299 !is_sym_main(symbol)) {
4300 warningf(&declaration->source_position,
4301 "no previous prototype for '%#T'",
4304 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4305 new_storage_class = STORAGE_CLASS_EXTERN;
4314 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4315 new_storage_class == STORAGE_CLASS_EXTERN) {
4316 warn_redundant_declaration:
4317 if (!is_definition &&
4318 warning.redundant_decls &&
4319 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4320 warningf(&declaration->source_position,
4321 "redundant declaration for '%Y' (declared %P)",
4322 symbol, &previous_declaration->source_position);
4324 } else if (current_function == NULL) {
4325 if (old_storage_class != STORAGE_CLASS_STATIC &&
4326 new_storage_class == STORAGE_CLASS_STATIC) {
4327 errorf(&declaration->source_position,
4328 "static declaration of '%Y' follows non-static declaration (declared %P)",
4329 symbol, &previous_declaration->source_position);
4330 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4331 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4332 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4334 goto warn_redundant_declaration;
4336 } else if (old_storage_class == new_storage_class) {
4337 errorf(&declaration->source_position,
4338 "redeclaration of '%Y' (declared %P)",
4339 symbol, &previous_declaration->source_position);
4341 errorf(&declaration->source_position,
4342 "redeclaration of '%Y' with different linkage (declared %P)",
4343 symbol, &previous_declaration->source_position);
4347 previous_declaration->modifiers |= declaration->modifiers;
4348 previous_declaration->is_inline |= declaration->is_inline;
4349 return previous_declaration;
4350 } else if (is_type_function(type)) {
4351 if (is_definition &&
4352 declaration->storage_class != STORAGE_CLASS_STATIC) {
4353 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4354 warningf(&declaration->source_position,
4355 "no previous prototype for '%#T'", orig_type, symbol);
4356 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4357 warningf(&declaration->source_position,
4358 "no previous declaration for '%#T'", orig_type,
4363 if (warning.missing_declarations &&
4364 scope == global_scope && (
4365 declaration->storage_class == STORAGE_CLASS_NONE ||
4366 declaration->storage_class == STORAGE_CLASS_THREAD
4368 warningf(&declaration->source_position,
4369 "no previous declaration for '%#T'", orig_type, symbol);
4373 assert(declaration->parent_scope == NULL);
4374 assert(scope != NULL);
4376 declaration->parent_scope = scope;
4378 environment_push(declaration);
4379 return append_declaration(declaration);
4382 static declaration_t *record_declaration(declaration_t *declaration)
4384 return internal_record_declaration(declaration, false);
4387 static declaration_t *record_definition(declaration_t *declaration)
4389 return internal_record_declaration(declaration, true);
4392 static void parser_error_multiple_definition(declaration_t *declaration,
4393 const source_position_t *source_position)
4395 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4396 declaration->symbol, &declaration->source_position);
4399 static bool is_declaration_specifier(const token_t *token,
4400 bool only_specifiers_qualifiers)
4402 switch(token->type) {
4407 return is_typedef_symbol(token->v.symbol);
4409 case T___extension__:
4411 return !only_specifiers_qualifiers;
4418 static void parse_init_declarator_rest(declaration_t *declaration)
4422 type_t *orig_type = declaration->type;
4423 type_t *type = skip_typeref(orig_type);
4425 if (declaration->init.initializer != NULL) {
4426 parser_error_multiple_definition(declaration, HERE);
4429 bool must_be_constant = false;
4430 if (declaration->storage_class == STORAGE_CLASS_STATIC
4431 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4432 || declaration->parent_scope == global_scope) {
4433 must_be_constant = true;
4436 parse_initializer_env_t env;
4437 env.type = orig_type;
4438 env.must_be_constant = must_be_constant;
4439 env.declaration = declaration;
4441 initializer_t *initializer = parse_initializer(&env);
4443 if (env.type != orig_type) {
4444 orig_type = env.type;
4445 type = skip_typeref(orig_type);
4446 declaration->type = env.type;
4449 if (is_type_function(type)) {
4450 errorf(&declaration->source_position,
4451 "initializers not allowed for function types at declator '%Y' (type '%T')",
4452 declaration->symbol, orig_type);
4454 declaration->init.initializer = initializer;
4458 /* parse rest of a declaration without any declarator */
4459 static void parse_anonymous_declaration_rest(
4460 const declaration_specifiers_t *specifiers,
4461 parsed_declaration_func finished_declaration)
4465 declaration_t *const declaration = allocate_declaration_zero();
4466 declaration->type = specifiers->type;
4467 declaration->declared_storage_class = specifiers->declared_storage_class;
4468 declaration->source_position = specifiers->source_position;
4469 declaration->modifiers = specifiers->modifiers;
4471 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4472 warningf(&declaration->source_position,
4473 "useless storage class in empty declaration");
4475 declaration->storage_class = STORAGE_CLASS_NONE;
4477 type_t *type = declaration->type;
4478 switch (type->kind) {
4479 case TYPE_COMPOUND_STRUCT:
4480 case TYPE_COMPOUND_UNION: {
4481 if (type->compound.declaration->symbol == NULL) {
4482 warningf(&declaration->source_position,
4483 "unnamed struct/union that defines no instances");
4492 warningf(&declaration->source_position, "empty declaration");
4496 finished_declaration(declaration);
4499 static void parse_declaration_rest(declaration_t *ndeclaration,
4500 const declaration_specifiers_t *specifiers,
4501 parsed_declaration_func finished_declaration)
4503 add_anchor_token(';');
4504 add_anchor_token('=');
4505 add_anchor_token(',');
4507 declaration_t *declaration = finished_declaration(ndeclaration);
4509 type_t *orig_type = declaration->type;
4510 type_t *type = skip_typeref(orig_type);
4512 if (type->kind != TYPE_FUNCTION &&
4513 declaration->is_inline &&
4514 is_type_valid(type)) {
4515 warningf(&declaration->source_position,
4516 "variable '%Y' declared 'inline'\n", declaration->symbol);
4519 if (token.type == '=') {
4520 parse_init_declarator_rest(declaration);
4523 if (token.type != ',')
4527 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4532 rem_anchor_token(';');
4533 rem_anchor_token('=');
4534 rem_anchor_token(',');
4537 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4539 symbol_t *symbol = declaration->symbol;
4540 if (symbol == NULL) {
4541 errorf(HERE, "anonymous declaration not valid as function parameter");
4544 namespace_t namespc = (namespace_t) declaration->namespc;
4545 if (namespc != NAMESPACE_NORMAL) {
4546 return record_declaration(declaration);
4549 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4550 if (previous_declaration == NULL ||
4551 previous_declaration->parent_scope != scope) {
4552 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4557 if (previous_declaration->type == NULL) {
4558 previous_declaration->type = declaration->type;
4559 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4560 previous_declaration->storage_class = declaration->storage_class;
4561 previous_declaration->parent_scope = scope;
4562 return previous_declaration;
4564 return record_declaration(declaration);
4568 static void parse_declaration(parsed_declaration_func finished_declaration)
4570 declaration_specifiers_t specifiers;
4571 memset(&specifiers, 0, sizeof(specifiers));
4572 parse_declaration_specifiers(&specifiers);
4574 if (token.type == ';') {
4575 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4577 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4578 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4582 static type_t *get_default_promoted_type(type_t *orig_type)
4584 type_t *result = orig_type;
4586 type_t *type = skip_typeref(orig_type);
4587 if (is_type_integer(type)) {
4588 result = promote_integer(type);
4589 } else if (type == type_float) {
4590 result = type_double;
4596 static void parse_kr_declaration_list(declaration_t *declaration)
4598 type_t *type = skip_typeref(declaration->type);
4599 if (!is_type_function(type))
4602 if (!type->function.kr_style_parameters)
4605 /* push function parameters */
4606 int top = environment_top();
4607 scope_t *last_scope = scope;
4608 set_scope(&declaration->scope);
4610 declaration_t *parameter = declaration->scope.declarations;
4611 for ( ; parameter != NULL; parameter = parameter->next) {
4612 assert(parameter->parent_scope == NULL);
4613 parameter->parent_scope = scope;
4614 environment_push(parameter);
4617 /* parse declaration list */
4618 while (is_declaration_specifier(&token, false)) {
4619 parse_declaration(finished_kr_declaration);
4622 /* pop function parameters */
4623 assert(scope == &declaration->scope);
4624 set_scope(last_scope);
4625 environment_pop_to(top);
4627 /* update function type */
4628 type_t *new_type = duplicate_type(type);
4630 function_parameter_t *parameters = NULL;
4631 function_parameter_t *last_parameter = NULL;
4633 declaration_t *parameter_declaration = declaration->scope.declarations;
4634 for( ; parameter_declaration != NULL;
4635 parameter_declaration = parameter_declaration->next) {
4636 type_t *parameter_type = parameter_declaration->type;
4637 if (parameter_type == NULL) {
4639 errorf(HERE, "no type specified for function parameter '%Y'",
4640 parameter_declaration->symbol);
4642 if (warning.implicit_int) {
4643 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4644 parameter_declaration->symbol);
4646 parameter_type = type_int;
4647 parameter_declaration->type = parameter_type;
4651 semantic_parameter(parameter_declaration);
4652 parameter_type = parameter_declaration->type;
4655 * we need the default promoted types for the function type
4657 parameter_type = get_default_promoted_type(parameter_type);
4659 function_parameter_t *function_parameter
4660 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4661 memset(function_parameter, 0, sizeof(function_parameter[0]));
4663 function_parameter->type = parameter_type;
4664 if (last_parameter != NULL) {
4665 last_parameter->next = function_parameter;
4667 parameters = function_parameter;
4669 last_parameter = function_parameter;
4672 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4674 new_type->function.parameters = parameters;
4675 new_type->function.unspecified_parameters = true;
4677 type = typehash_insert(new_type);
4678 if (type != new_type) {
4679 obstack_free(type_obst, new_type);
4682 declaration->type = type;
4685 static bool first_err = true;
4688 * When called with first_err set, prints the name of the current function,
4691 static void print_in_function(void)
4695 diagnosticf("%s: In function '%Y':\n",
4696 current_function->source_position.input_name,
4697 current_function->symbol);
4702 * Check if all labels are defined in the current function.
4703 * Check if all labels are used in the current function.
4705 static void check_labels(void)
4707 for (const goto_statement_t *goto_statement = goto_first;
4708 goto_statement != NULL;
4709 goto_statement = goto_statement->next) {
4710 declaration_t *label = goto_statement->label;
4713 if (label->source_position.input_name == NULL) {
4714 print_in_function();
4715 errorf(&goto_statement->base.source_position,
4716 "label '%Y' used but not defined", label->symbol);
4719 goto_first = goto_last = NULL;
4721 if (warning.unused_label) {
4722 for (const label_statement_t *label_statement = label_first;
4723 label_statement != NULL;
4724 label_statement = label_statement->next) {
4725 const declaration_t *label = label_statement->label;
4727 if (! label->used) {
4728 print_in_function();
4729 warningf(&label_statement->base.source_position,
4730 "label '%Y' defined but not used", label->symbol);
4734 label_first = label_last = NULL;
4738 * Check declarations of current_function for unused entities.
4740 static void check_declarations(void)
4742 if (warning.unused_parameter) {
4743 const scope_t *scope = ¤t_function->scope;
4745 if (is_sym_main(current_function->symbol)) {
4746 /* do not issue unused warnings for main */
4749 const declaration_t *parameter = scope->declarations;
4750 for (; parameter != NULL; parameter = parameter->next) {
4751 if (! parameter->used) {
4752 print_in_function();
4753 warningf(¶meter->source_position,
4754 "unused parameter '%Y'", parameter->symbol);
4758 if (warning.unused_variable) {
4762 static int determine_truth(expression_t const* const cond)
4765 !is_constant_expression(cond) ? 0 :
4766 fold_constant(cond) != 0 ? 1 :
4770 static bool noreturn_candidate;
4772 static void check_reachable(statement_t *const stmt)
4774 if (stmt->base.reachable)
4776 if (stmt->kind != STATEMENT_DO_WHILE)
4777 stmt->base.reachable = true;
4779 statement_t *last = stmt;
4781 switch (stmt->kind) {
4782 case STATEMENT_INVALID:
4783 case STATEMENT_EMPTY:
4784 case STATEMENT_DECLARATION:
4786 next = stmt->base.next;
4789 case STATEMENT_COMPOUND:
4790 next = stmt->compound.statements;
4793 case STATEMENT_RETURN:
4794 noreturn_candidate = false;
4797 case STATEMENT_IF: {
4798 if_statement_t const* const ifs = &stmt->ifs;
4799 int const val = determine_truth(ifs->condition);
4802 check_reachable(ifs->true_statement);
4807 if (ifs->false_statement != NULL) {
4808 check_reachable(ifs->false_statement);
4812 next = stmt->base.next;
4816 case STATEMENT_SWITCH: {
4817 switch_statement_t const *const switchs = &stmt->switchs;
4818 expression_t const *const expr = switchs->expression;
4820 if (is_constant_expression(expr)) {
4821 long const val = fold_constant(expr);
4822 case_label_statement_t * defaults = NULL;
4823 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
4824 if (i->expression == NULL) {
4829 if (i->first_case <= val && val <= i->last_case) {
4830 check_reachable((statement_t*)i);
4835 if (defaults != NULL) {
4836 check_reachable((statement_t*)defaults);
4840 bool has_default = false;
4841 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
4842 if (i->expression == NULL)
4845 check_reachable((statement_t*)i);
4852 next = stmt->base.next;
4856 case STATEMENT_EXPRESSION: {
4857 /* Check for noreturn function call */
4858 expression_t const *const expr = stmt->expression.expression;
4859 if (expr->kind == EXPR_CALL) {
4860 expression_t const *const func = expr->call.function;
4861 if (func->kind == EXPR_REFERENCE) {
4862 declaration_t const *const decl = func->reference.declaration;
4863 if (decl != NULL && decl->modifiers & DM_NORETURN) {
4869 next = stmt->base.next;
4873 case STATEMENT_CONTINUE: {
4874 statement_t *parent = stmt;
4876 parent = parent->base.parent;
4877 if (parent == NULL) /* continue not within loop */
4881 switch (parent->kind) {
4882 case STATEMENT_WHILE: goto continue_while;
4883 case STATEMENT_DO_WHILE: goto continue_do_while;
4884 case STATEMENT_FOR: goto continue_for;
4891 case STATEMENT_BREAK: {
4892 statement_t *parent = stmt;
4894 parent = parent->base.parent;
4895 if (parent == NULL) /* break not within loop/switch */
4898 switch (parent->kind) {
4899 case STATEMENT_SWITCH:
4900 case STATEMENT_WHILE:
4901 case STATEMENT_DO_WHILE:
4904 next = parent->base.next;
4905 goto found_break_parent;
4914 case STATEMENT_GOTO:
4915 next = stmt->gotos.label->init.statement;
4916 if (next == NULL) /* missing label */
4920 case STATEMENT_LABEL:
4921 next = stmt->label.statement;
4924 case STATEMENT_CASE_LABEL:
4925 next = stmt->case_label.statement;
4928 case STATEMENT_WHILE: {
4929 while_statement_t const *const whiles = &stmt->whiles;
4930 int const val = determine_truth(whiles->condition);
4933 check_reachable(whiles->body);
4938 next = stmt->base.next;
4942 case STATEMENT_DO_WHILE:
4943 next = stmt->do_while.body;
4946 case STATEMENT_FOR: {
4947 for_statement_t *const fors = &stmt->fors;
4949 if (fors->condition_reachable)
4951 fors->condition_reachable = true;
4953 expression_t const *const cond = fors->condition;
4955 cond == NULL ? 1 : determine_truth(cond);
4958 check_reachable(fors->body);
4963 next = stmt->base.next;
4967 case STATEMENT_MS_TRY: {
4968 ms_try_statement_t const *const ms_try = &stmt->ms_try;
4969 check_reachable(ms_try->try_statement);
4970 next = ms_try->final_statement;
4974 case STATEMENT_LEAVE: {
4975 statement_t *parent = stmt;
4977 parent = parent->base.parent;
4978 if (parent == NULL) /* __leave not within __try */
4981 if (parent->kind == STATEMENT_MS_TRY) {
4983 next = parent->ms_try.final_statement;
4991 while (next == NULL) {
4992 next = last->base.parent;
4994 noreturn_candidate = false;
4996 type_t *const type = current_function->type;
4997 assert(is_type_function(type));
4998 type_t *const ret = skip_typeref(type->function.return_type);
4999 if (warning.return_type &&
5000 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
5001 is_type_valid(ret) &&
5002 !is_sym_main(current_function->symbol)) {
5003 warningf(&stmt->base.source_position,
5004 "control reaches end of non-void function");
5009 switch (next->kind) {
5010 case STATEMENT_INVALID:
5011 case STATEMENT_EMPTY:
5012 case STATEMENT_DECLARATION:
5013 case STATEMENT_EXPRESSION:
5015 case STATEMENT_RETURN:
5016 case STATEMENT_CONTINUE:
5017 case STATEMENT_BREAK:
5018 case STATEMENT_GOTO:
5019 case STATEMENT_LEAVE:
5020 panic("invalid control flow in function");
5022 case STATEMENT_COMPOUND:
5024 case STATEMENT_SWITCH:
5025 case STATEMENT_LABEL:
5026 case STATEMENT_CASE_LABEL:
5028 next = next->base.next;
5031 case STATEMENT_WHILE: {
5033 if (next->base.reachable)
5035 next->base.reachable = true;
5037 while_statement_t const *const whiles = &next->whiles;
5038 int const val = determine_truth(whiles->condition);
5041 check_reachable(whiles->body);
5047 next = next->base.next;
5051 case STATEMENT_DO_WHILE: {
5053 if (next->base.reachable)
5055 next->base.reachable = true;
5057 do_while_statement_t const *const dw = &next->do_while;
5058 int const val = determine_truth(dw->condition);
5061 check_reachable(dw->body);
5067 next = next->base.next;
5071 case STATEMENT_FOR: {
5073 for_statement_t *const fors = &next->fors;
5075 fors->step_reachable = true;
5077 if (fors->condition_reachable)
5079 fors->condition_reachable = true;
5081 expression_t const *const cond = fors->condition;
5083 cond == NULL ? 1 : determine_truth(cond);
5086 check_reachable(fors->body);
5092 next = next->base.next;
5096 case STATEMENT_MS_TRY:
5098 next = next->ms_try.final_statement;
5104 next = stmt->base.parent;
5106 warningf(&stmt->base.source_position,
5107 "control reaches end of non-void function");
5111 check_reachable(next);
5114 static void check_unreachable(statement_t const* const stmt)
5116 if (!stmt->base.reachable &&
5117 stmt->kind != STATEMENT_DO_WHILE &&
5118 stmt->kind != STATEMENT_FOR &&
5119 (stmt->kind != STATEMENT_COMPOUND || stmt->compound.statements == NULL)) {
5120 warningf(&stmt->base.source_position, "statement is unreachable");
5123 switch (stmt->kind) {
5124 case STATEMENT_INVALID:
5125 case STATEMENT_EMPTY:
5126 case STATEMENT_RETURN:
5127 case STATEMENT_DECLARATION:
5128 case STATEMENT_EXPRESSION:
5129 case STATEMENT_CONTINUE:
5130 case STATEMENT_BREAK:
5131 case STATEMENT_GOTO:
5133 case STATEMENT_LEAVE:
5136 case STATEMENT_COMPOUND:
5137 if (stmt->compound.statements)
5138 check_unreachable(stmt->compound.statements);
5142 check_unreachable(stmt->ifs.true_statement);
5143 if (stmt->ifs.false_statement != NULL)
5144 check_unreachable(stmt->ifs.false_statement);
5147 case STATEMENT_SWITCH:
5148 check_unreachable(stmt->switchs.body);
5151 case STATEMENT_LABEL:
5152 check_unreachable(stmt->label.statement);
5155 case STATEMENT_CASE_LABEL:
5156 check_unreachable(stmt->case_label.statement);
5159 case STATEMENT_WHILE:
5160 check_unreachable(stmt->whiles.body);
5163 case STATEMENT_DO_WHILE:
5164 check_unreachable(stmt->do_while.body);
5165 if (!stmt->base.reachable) {
5166 expression_t const *const cond = stmt->do_while.condition;
5167 if (determine_truth(cond) >= 0) {
5168 warningf(&cond->base.source_position,
5169 "condition of do-while-loop is unreachable");
5174 case STATEMENT_FOR: {
5175 for_statement_t const* const fors = &stmt->fors;
5177 // if init and step are unreachable, cond is unreachable, too
5178 if (!stmt->base.reachable && !fors->step_reachable) {
5179 warningf(&stmt->base.source_position, "statement is unreachable");
5181 if (!stmt->base.reachable && fors->initialisation != NULL) {
5182 warningf(&fors->initialisation->base.source_position,
5183 "initialisation of for-statement is unreachable");
5186 if (!fors->condition_reachable && fors->condition != NULL) {
5187 warningf(&fors->condition->base.source_position,
5188 "condition of for-statement is unreachable");
5191 if (!fors->step_reachable && fors->step != NULL) {
5192 warningf(&fors->step->base.source_position,
5193 "step of for-statement is unreachable");
5197 check_unreachable(fors->body);
5201 case STATEMENT_MS_TRY: {
5202 ms_try_statement_t const *const ms_try = &stmt->ms_try;
5203 check_unreachable(ms_try->try_statement);
5204 check_unreachable(ms_try->final_statement);
5208 if (stmt->base.next)
5209 check_unreachable(stmt->base.next);
5212 static void parse_external_declaration(void)
5214 /* function-definitions and declarations both start with declaration
5216 declaration_specifiers_t specifiers;
5217 memset(&specifiers, 0, sizeof(specifiers));
5219 add_anchor_token(';');
5220 parse_declaration_specifiers(&specifiers);
5221 rem_anchor_token(';');
5223 /* must be a declaration */
5224 if (token.type == ';') {
5225 parse_anonymous_declaration_rest(&specifiers, append_declaration);
5229 add_anchor_token(',');
5230 add_anchor_token('=');
5231 rem_anchor_token(';');
5233 /* declarator is common to both function-definitions and declarations */
5234 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5236 rem_anchor_token(',');
5237 rem_anchor_token('=');
5238 rem_anchor_token(';');
5240 /* must be a declaration */
5241 switch (token.type) {
5244 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5248 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
5252 /* must be a function definition */
5253 parse_kr_declaration_list(ndeclaration);
5255 if (token.type != '{') {
5256 parse_error_expected("while parsing function definition", '{', NULL);
5257 eat_until_matching_token(';');
5261 type_t *type = ndeclaration->type;
5263 /* note that we don't skip typerefs: the standard doesn't allow them here
5264 * (so we can't use is_type_function here) */
5265 if (type->kind != TYPE_FUNCTION) {
5266 if (is_type_valid(type)) {
5267 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5268 type, ndeclaration->symbol);
5274 if (warning.aggregate_return &&
5275 is_type_compound(skip_typeref(type->function.return_type))) {
5276 warningf(HERE, "function '%Y' returns an aggregate",
5277 ndeclaration->symbol);
5280 /* § 6.7.5.3 (14) a function definition with () means no
5281 * parameters (and not unspecified parameters) */
5282 if (type->function.unspecified_parameters
5283 && type->function.parameters == NULL
5284 && !type->function.kr_style_parameters) {
5285 type_t *duplicate = duplicate_type(type);
5286 duplicate->function.unspecified_parameters = false;
5288 type = typehash_insert(duplicate);
5289 if (type != duplicate) {
5290 obstack_free(type_obst, duplicate);
5292 ndeclaration->type = type;
5295 declaration_t *const declaration = record_definition(ndeclaration);
5296 if (ndeclaration != declaration) {
5297 declaration->scope = ndeclaration->scope;
5299 type = skip_typeref(declaration->type);
5301 /* push function parameters and switch scope */
5302 int top = environment_top();
5303 scope_t *last_scope = scope;
5304 set_scope(&declaration->scope);
5306 declaration_t *parameter = declaration->scope.declarations;
5307 for( ; parameter != NULL; parameter = parameter->next) {
5308 if (parameter->parent_scope == &ndeclaration->scope) {
5309 parameter->parent_scope = scope;
5311 assert(parameter->parent_scope == NULL
5312 || parameter->parent_scope == scope);
5313 parameter->parent_scope = scope;
5314 if (parameter->symbol == NULL) {
5315 errorf(¶meter->source_position, "parameter name omitted");
5318 environment_push(parameter);
5321 if (declaration->init.statement != NULL) {
5322 parser_error_multiple_definition(declaration, HERE);
5325 /* parse function body */
5326 int label_stack_top = label_top();
5327 declaration_t *old_current_function = current_function;
5328 current_function = declaration;
5329 current_parent = NULL;
5331 statement_t *const body = parse_compound_statement(false);
5332 declaration->init.statement = body;
5335 check_declarations();
5336 if (warning.return_type ||
5337 warning.unreachable_code ||
5338 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5339 noreturn_candidate = true;
5340 check_reachable(body);
5341 if (warning.unreachable_code)
5342 check_unreachable(body);
5343 if (warning.missing_noreturn &&
5344 noreturn_candidate &&
5345 !(declaration->modifiers & DM_NORETURN)) {
5346 warningf(&body->base.source_position,
5347 "function '%#T' is candidate for attribute 'noreturn'",
5348 type, declaration->symbol);
5352 assert(current_parent == NULL);
5353 assert(current_function == declaration);
5354 current_function = old_current_function;
5355 label_pop_to(label_stack_top);
5358 assert(scope == &declaration->scope);
5359 set_scope(last_scope);
5360 environment_pop_to(top);
5363 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5364 source_position_t *source_position)
5366 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
5368 type->bitfield.base_type = base_type;
5369 type->bitfield.size = size;
5374 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5377 declaration_t *iter = compound_declaration->scope.declarations;
5378 for( ; iter != NULL; iter = iter->next) {
5379 if (iter->namespc != NAMESPACE_NORMAL)
5382 if (iter->symbol == NULL) {
5383 type_t *type = skip_typeref(iter->type);
5384 if (is_type_compound(type)) {
5385 declaration_t *result
5386 = find_compound_entry(type->compound.declaration, symbol);
5393 if (iter->symbol == symbol) {
5401 static void parse_compound_declarators(declaration_t *struct_declaration,
5402 const declaration_specifiers_t *specifiers)
5404 declaration_t *last_declaration = struct_declaration->scope.declarations;
5405 if (last_declaration != NULL) {
5406 while(last_declaration->next != NULL) {
5407 last_declaration = last_declaration->next;
5412 declaration_t *declaration;
5414 if (token.type == ':') {
5415 source_position_t source_position = *HERE;
5418 type_t *base_type = specifiers->type;
5419 expression_t *size = parse_constant_expression();
5421 if (!is_type_integer(skip_typeref(base_type))) {
5422 errorf(HERE, "bitfield base type '%T' is not an integer type",
5426 type_t *type = make_bitfield_type(base_type, size, &source_position);
5428 declaration = allocate_declaration_zero();
5429 declaration->namespc = NAMESPACE_NORMAL;
5430 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5431 declaration->storage_class = STORAGE_CLASS_NONE;
5432 declaration->source_position = source_position;
5433 declaration->modifiers = specifiers->modifiers;
5434 declaration->type = type;
5436 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5438 type_t *orig_type = declaration->type;
5439 type_t *type = skip_typeref(orig_type);
5441 if (token.type == ':') {
5442 source_position_t source_position = *HERE;
5444 expression_t *size = parse_constant_expression();
5446 if (!is_type_integer(type)) {
5447 errorf(HERE, "bitfield base type '%T' is not an integer type",
5451 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
5452 declaration->type = bitfield_type;
5454 /* TODO we ignore arrays for now... what is missing is a check
5455 * that they're at the end of the struct */
5456 if (is_type_incomplete(type) && !is_type_array(type)) {
5458 "compound member '%Y' has incomplete type '%T'",
5459 declaration->symbol, orig_type);
5460 } else if (is_type_function(type)) {
5461 errorf(HERE, "compound member '%Y' must not have function type '%T'",
5462 declaration->symbol, orig_type);
5467 /* make sure we don't define a symbol multiple times */
5468 symbol_t *symbol = declaration->symbol;
5469 if (symbol != NULL) {
5470 declaration_t *prev_decl
5471 = find_compound_entry(struct_declaration, symbol);
5473 if (prev_decl != NULL) {
5474 assert(prev_decl->symbol == symbol);
5475 errorf(&declaration->source_position,
5476 "multiple declarations of symbol '%Y' (declared %P)",
5477 symbol, &prev_decl->source_position);
5481 /* append declaration */
5482 if (last_declaration != NULL) {
5483 last_declaration->next = declaration;
5485 struct_declaration->scope.declarations = declaration;
5487 last_declaration = declaration;
5489 if (token.type != ',')
5499 static void parse_compound_type_entries(declaration_t *compound_declaration)
5502 add_anchor_token('}');
5504 while(token.type != '}' && token.type != T_EOF) {
5505 declaration_specifiers_t specifiers;
5506 memset(&specifiers, 0, sizeof(specifiers));
5507 parse_declaration_specifiers(&specifiers);
5509 parse_compound_declarators(compound_declaration, &specifiers);
5511 rem_anchor_token('}');
5513 if (token.type == T_EOF) {
5514 errorf(HERE, "EOF while parsing struct");
5519 static type_t *parse_typename(void)
5521 declaration_specifiers_t specifiers;
5522 memset(&specifiers, 0, sizeof(specifiers));
5523 parse_declaration_specifiers(&specifiers);
5524 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5525 /* TODO: improve error message, user does probably not know what a
5526 * storage class is...
5528 errorf(HERE, "typename may not have a storage class");
5531 type_t *result = parse_abstract_declarator(specifiers.type);
5539 typedef expression_t* (*parse_expression_function) (unsigned precedence);
5540 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
5541 expression_t *left);
5543 typedef struct expression_parser_function_t expression_parser_function_t;
5544 struct expression_parser_function_t {
5545 unsigned precedence;
5546 parse_expression_function parser;
5547 unsigned infix_precedence;
5548 parse_expression_infix_function infix_parser;
5551 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5554 * Prints an error message if an expression was expected but not read
5556 static expression_t *expected_expression_error(void)
5558 /* skip the error message if the error token was read */
5559 if (token.type != T_ERROR) {
5560 errorf(HERE, "expected expression, got token '%K'", &token);
5564 return create_invalid_expression();
5568 * Parse a string constant.
5570 static expression_t *parse_string_const(void)
5573 if (token.type == T_STRING_LITERAL) {
5574 string_t res = token.v.string;
5576 while (token.type == T_STRING_LITERAL) {
5577 res = concat_strings(&res, &token.v.string);
5580 if (token.type != T_WIDE_STRING_LITERAL) {
5581 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
5582 /* note: that we use type_char_ptr here, which is already the
5583 * automatic converted type. revert_automatic_type_conversion
5584 * will construct the array type */
5585 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
5586 cnst->string.value = res;
5590 wres = concat_string_wide_string(&res, &token.v.wide_string);
5592 wres = token.v.wide_string;
5597 switch (token.type) {
5598 case T_WIDE_STRING_LITERAL:
5599 wres = concat_wide_strings(&wres, &token.v.wide_string);
5602 case T_STRING_LITERAL:
5603 wres = concat_wide_string_string(&wres, &token.v.string);
5607 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
5608 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
5609 cnst->wide_string.value = wres;
5618 * Parse an integer constant.
5620 static expression_t *parse_int_const(void)
5622 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5623 cnst->base.source_position = *HERE;
5624 cnst->base.type = token.datatype;
5625 cnst->conste.v.int_value = token.v.intvalue;
5633 * Parse a character constant.
5635 static expression_t *parse_character_constant(void)
5637 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5639 cnst->base.source_position = *HERE;
5640 cnst->base.type = token.datatype;
5641 cnst->conste.v.character = token.v.string;
5643 if (cnst->conste.v.character.size != 1) {
5644 if (warning.multichar && (c_mode & _GNUC)) {
5646 warningf(HERE, "multi-character character constant");
5648 errorf(HERE, "more than 1 characters in character constant");
5657 * Parse a wide character constant.
5659 static expression_t *parse_wide_character_constant(void)
5661 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5663 cnst->base.source_position = *HERE;
5664 cnst->base.type = token.datatype;
5665 cnst->conste.v.wide_character = token.v.wide_string;
5667 if (cnst->conste.v.wide_character.size != 1) {
5668 if (warning.multichar && (c_mode & _GNUC)) {
5670 warningf(HERE, "multi-character character constant");
5672 errorf(HERE, "more than 1 characters in character constant");
5681 * Parse a float constant.
5683 static expression_t *parse_float_const(void)
5685 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5686 cnst->base.type = token.datatype;
5687 cnst->conste.v.float_value = token.v.floatvalue;
5694 static declaration_t *create_implicit_function(symbol_t *symbol,
5695 const source_position_t *source_position)
5697 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5698 ntype->function.return_type = type_int;
5699 ntype->function.unspecified_parameters = true;
5701 type_t *type = typehash_insert(ntype);
5702 if (type != ntype) {
5706 declaration_t *const declaration = allocate_declaration_zero();
5707 declaration->storage_class = STORAGE_CLASS_EXTERN;
5708 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5709 declaration->type = type;
5710 declaration->symbol = symbol;
5711 declaration->source_position = *source_position;
5712 declaration->implicit = true;
5714 bool strict_prototypes_old = warning.strict_prototypes;
5715 warning.strict_prototypes = false;
5716 record_declaration(declaration);
5717 warning.strict_prototypes = strict_prototypes_old;
5723 * Creates a return_type (func)(argument_type) function type if not
5726 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5727 type_t *argument_type2)
5729 function_parameter_t *parameter2
5730 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5731 memset(parameter2, 0, sizeof(parameter2[0]));
5732 parameter2->type = argument_type2;
5734 function_parameter_t *parameter1
5735 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5736 memset(parameter1, 0, sizeof(parameter1[0]));
5737 parameter1->type = argument_type1;
5738 parameter1->next = parameter2;
5740 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5741 type->function.return_type = return_type;
5742 type->function.parameters = parameter1;
5744 type_t *result = typehash_insert(type);
5745 if (result != type) {
5753 * Creates a return_type (func)(argument_type) function type if not
5756 * @param return_type the return type
5757 * @param argument_type the argument type
5759 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5761 function_parameter_t *parameter
5762 = obstack_alloc(type_obst, sizeof(parameter[0]));
5763 memset(parameter, 0, sizeof(parameter[0]));
5764 parameter->type = argument_type;
5766 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5767 type->function.return_type = return_type;
5768 type->function.parameters = parameter;
5770 type_t *result = typehash_insert(type);
5771 if (result != type) {
5778 static type_t *make_function_0_type(type_t *return_type)
5780 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5781 type->function.return_type = return_type;
5782 type->function.parameters = NULL;
5784 type_t *result = typehash_insert(type);
5785 if (result != type) {
5793 * Creates a function type for some function like builtins.
5795 * @param symbol the symbol describing the builtin
5797 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5799 switch(symbol->ID) {
5800 case T___builtin_alloca:
5801 return make_function_1_type(type_void_ptr, type_size_t);
5802 case T___builtin_huge_val:
5803 return make_function_0_type(type_double);
5804 case T___builtin_nan:
5805 return make_function_1_type(type_double, type_char_ptr);
5806 case T___builtin_nanf:
5807 return make_function_1_type(type_float, type_char_ptr);
5808 case T___builtin_nand:
5809 return make_function_1_type(type_long_double, type_char_ptr);
5810 case T___builtin_va_end:
5811 return make_function_1_type(type_void, type_valist);
5812 case T___builtin_expect:
5813 return make_function_2_type(type_long, type_long, type_long);
5815 internal_errorf(HERE, "not implemented builtin symbol found");
5820 * Performs automatic type cast as described in § 6.3.2.1.
5822 * @param orig_type the original type
5824 static type_t *automatic_type_conversion(type_t *orig_type)
5826 type_t *type = skip_typeref(orig_type);
5827 if (is_type_array(type)) {
5828 array_type_t *array_type = &type->array;
5829 type_t *element_type = array_type->element_type;
5830 unsigned qualifiers = array_type->base.qualifiers;
5832 return make_pointer_type(element_type, qualifiers);
5835 if (is_type_function(type)) {
5836 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5843 * reverts the automatic casts of array to pointer types and function
5844 * to function-pointer types as defined § 6.3.2.1
5846 type_t *revert_automatic_type_conversion(const expression_t *expression)
5848 switch (expression->kind) {
5849 case EXPR_REFERENCE: return expression->reference.declaration->type;
5850 case EXPR_SELECT: return expression->select.compound_entry->type;
5852 case EXPR_UNARY_DEREFERENCE: {
5853 const expression_t *const value = expression->unary.value;
5854 type_t *const type = skip_typeref(value->base.type);
5855 assert(is_type_pointer(type));
5856 return type->pointer.points_to;
5859 case EXPR_BUILTIN_SYMBOL:
5860 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5862 case EXPR_ARRAY_ACCESS: {
5863 const expression_t *array_ref = expression->array_access.array_ref;
5864 type_t *type_left = skip_typeref(array_ref->base.type);
5865 if (!is_type_valid(type_left))
5867 assert(is_type_pointer(type_left));
5868 return type_left->pointer.points_to;
5871 case EXPR_STRING_LITERAL: {
5872 size_t size = expression->string.value.size;
5873 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5876 case EXPR_WIDE_STRING_LITERAL: {
5877 size_t size = expression->wide_string.value.size;
5878 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5881 case EXPR_COMPOUND_LITERAL:
5882 return expression->compound_literal.type;
5887 return expression->base.type;
5890 static expression_t *parse_reference(void)
5892 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5894 reference_expression_t *ref = &expression->reference;
5895 symbol_t *const symbol = token.v.symbol;
5897 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5899 source_position_t source_position = token.source_position;
5902 if (declaration == NULL) {
5903 if (token.type == '(') {
5904 /* an implicitly declared function */
5906 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5907 } else if (warning.implicit_function_declaration) {
5908 warningf(HERE, "implicit declaration of function '%Y'",
5912 declaration = create_implicit_function(symbol,
5915 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5916 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
5920 type_t *type = declaration->type;
5922 /* we always do the auto-type conversions; the & and sizeof parser contains
5923 * code to revert this! */
5924 type = automatic_type_conversion(type);
5926 ref->declaration = declaration;
5927 ref->base.type = type;
5929 /* this declaration is used */
5930 declaration->used = true;
5932 /* check for deprecated functions */
5933 if (warning.deprecated_declarations &&
5934 declaration->modifiers & DM_DEPRECATED) {
5935 char const *const prefix = is_type_function(declaration->type) ?
5936 "function" : "variable";
5938 if (declaration->deprecated_string != NULL) {
5939 warningf(&source_position,
5940 "%s '%Y' is deprecated (declared %P): \"%s\"", prefix,
5941 declaration->symbol, &declaration->source_position,
5942 declaration->deprecated_string);
5944 warningf(&source_position,
5945 "%s '%Y' is deprecated (declared %P)", prefix,
5946 declaration->symbol, &declaration->source_position);
5953 static bool semantic_cast(expression_t *cast)
5955 expression_t *expression = cast->unary.value;
5956 type_t *orig_dest_type = cast->base.type;
5957 type_t *orig_type_right = expression->base.type;
5958 type_t const *dst_type = skip_typeref(orig_dest_type);
5959 type_t const *src_type = skip_typeref(orig_type_right);
5960 source_position_t const *pos = &cast->base.source_position;
5962 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
5963 if (dst_type == type_void)
5966 /* only integer and pointer can be casted to pointer */
5967 if (is_type_pointer(dst_type) &&
5968 !is_type_pointer(src_type) &&
5969 !is_type_integer(src_type) &&
5970 is_type_valid(src_type)) {
5971 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
5975 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
5976 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
5980 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
5981 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
5985 if (warning.cast_qual &&
5986 is_type_pointer(src_type) &&
5987 is_type_pointer(dst_type)) {
5988 type_t *src = skip_typeref(src_type->pointer.points_to);
5989 type_t *dst = skip_typeref(dst_type->pointer.points_to);
5990 unsigned missing_qualifiers =
5991 src->base.qualifiers & ~dst->base.qualifiers;
5992 if (missing_qualifiers != 0) {
5994 "cast discards qualifiers '%Q' in pointer target type of '%T'",
5995 missing_qualifiers, orig_type_right);
6001 static expression_t *parse_compound_literal(type_t *type)
6003 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
6005 parse_initializer_env_t env;
6007 env.declaration = NULL;
6008 env.must_be_constant = false;
6009 initializer_t *initializer = parse_initializer(&env);
6012 expression->compound_literal.initializer = initializer;
6013 expression->compound_literal.type = type;
6014 expression->base.type = automatic_type_conversion(type);
6020 * Parse a cast expression.
6022 static expression_t *parse_cast(void)
6024 source_position_t source_position = token.source_position;
6026 type_t *type = parse_typename();
6028 /* matching add_anchor_token() is at call site */
6029 rem_anchor_token(')');
6032 if (token.type == '{') {
6033 return parse_compound_literal(type);
6036 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6037 cast->base.source_position = source_position;
6039 expression_t *value = parse_sub_expression(20);
6040 cast->base.type = type;
6041 cast->unary.value = value;
6043 if (! semantic_cast(cast)) {
6044 /* TODO: record the error in the AST. else it is impossible to detect it */
6049 return create_invalid_expression();
6053 * Parse a statement expression.
6055 static expression_t *parse_statement_expression(void)
6057 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6059 statement_t *statement = parse_compound_statement(true);
6060 expression->statement.statement = statement;
6061 expression->base.source_position = statement->base.source_position;
6063 /* find last statement and use its type */
6064 type_t *type = type_void;
6065 const statement_t *stmt = statement->compound.statements;
6067 while (stmt->base.next != NULL)
6068 stmt = stmt->base.next;
6070 if (stmt->kind == STATEMENT_EXPRESSION) {
6071 type = stmt->expression.expression->base.type;
6074 warningf(&expression->base.source_position, "empty statement expression ({})");
6076 expression->base.type = type;
6082 return create_invalid_expression();
6086 * Parse a parenthesized expression.
6088 static expression_t *parse_parenthesized_expression(void)
6091 add_anchor_token(')');
6093 switch(token.type) {
6095 /* gcc extension: a statement expression */
6096 return parse_statement_expression();
6100 return parse_cast();
6102 if (is_typedef_symbol(token.v.symbol)) {
6103 return parse_cast();
6107 expression_t *result = parse_expression();
6108 rem_anchor_token(')');
6113 return create_invalid_expression();
6116 static expression_t *parse_function_keyword(void)
6121 if (current_function == NULL) {
6122 errorf(HERE, "'__func__' used outside of a function");
6125 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6126 expression->base.type = type_char_ptr;
6127 expression->funcname.kind = FUNCNAME_FUNCTION;
6132 static expression_t *parse_pretty_function_keyword(void)
6134 eat(T___PRETTY_FUNCTION__);
6136 if (current_function == NULL) {
6137 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6140 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6141 expression->base.type = type_char_ptr;
6142 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6147 static expression_t *parse_funcsig_keyword(void)
6151 if (current_function == NULL) {
6152 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6155 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6156 expression->base.type = type_char_ptr;
6157 expression->funcname.kind = FUNCNAME_FUNCSIG;
6162 static expression_t *parse_funcdname_keyword(void)
6164 eat(T___FUNCDNAME__);
6166 if (current_function == NULL) {
6167 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6170 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6171 expression->base.type = type_char_ptr;
6172 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6177 static designator_t *parse_designator(void)
6179 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6180 result->source_position = *HERE;
6182 if (token.type != T_IDENTIFIER) {
6183 parse_error_expected("while parsing member designator",
6184 T_IDENTIFIER, NULL);
6187 result->symbol = token.v.symbol;
6190 designator_t *last_designator = result;
6192 if (token.type == '.') {
6194 if (token.type != T_IDENTIFIER) {
6195 parse_error_expected("while parsing member designator",
6196 T_IDENTIFIER, NULL);
6199 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6200 designator->source_position = *HERE;
6201 designator->symbol = token.v.symbol;
6204 last_designator->next = designator;
6205 last_designator = designator;
6208 if (token.type == '[') {
6210 add_anchor_token(']');
6211 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6212 designator->source_position = *HERE;
6213 designator->array_index = parse_expression();
6214 rem_anchor_token(']');
6216 if (designator->array_index == NULL) {
6220 last_designator->next = designator;
6221 last_designator = designator;
6233 * Parse the __builtin_offsetof() expression.
6235 static expression_t *parse_offsetof(void)
6237 eat(T___builtin_offsetof);
6239 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6240 expression->base.type = type_size_t;
6243 add_anchor_token(',');
6244 type_t *type = parse_typename();
6245 rem_anchor_token(',');
6247 add_anchor_token(')');
6248 designator_t *designator = parse_designator();
6249 rem_anchor_token(')');
6252 expression->offsetofe.type = type;
6253 expression->offsetofe.designator = designator;
6256 memset(&path, 0, sizeof(path));
6257 path.top_type = type;
6258 path.path = NEW_ARR_F(type_path_entry_t, 0);
6260 descend_into_subtype(&path);
6262 if (!walk_designator(&path, designator, true)) {
6263 return create_invalid_expression();
6266 DEL_ARR_F(path.path);
6270 return create_invalid_expression();
6274 * Parses a _builtin_va_start() expression.
6276 static expression_t *parse_va_start(void)
6278 eat(T___builtin_va_start);
6280 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6283 add_anchor_token(',');
6284 expression->va_starte.ap = parse_assignment_expression();
6285 rem_anchor_token(',');
6287 expression_t *const expr = parse_assignment_expression();
6288 if (expr->kind == EXPR_REFERENCE) {
6289 declaration_t *const decl = expr->reference.declaration;
6291 return create_invalid_expression();
6292 if (decl->parent_scope == ¤t_function->scope &&
6293 decl->next == NULL) {
6294 expression->va_starte.parameter = decl;
6299 errorf(&expr->base.source_position,
6300 "second argument of 'va_start' must be last parameter of the current function");
6302 return create_invalid_expression();
6306 * Parses a _builtin_va_arg() expression.
6308 static expression_t *parse_va_arg(void)
6310 eat(T___builtin_va_arg);
6312 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6315 expression->va_arge.ap = parse_assignment_expression();
6317 expression->base.type = parse_typename();
6322 return create_invalid_expression();
6325 static expression_t *parse_builtin_symbol(void)
6327 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6329 symbol_t *symbol = token.v.symbol;
6331 expression->builtin_symbol.symbol = symbol;
6334 type_t *type = get_builtin_symbol_type(symbol);
6335 type = automatic_type_conversion(type);
6337 expression->base.type = type;
6342 * Parses a __builtin_constant() expression.
6344 static expression_t *parse_builtin_constant(void)
6346 eat(T___builtin_constant_p);
6348 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6351 add_anchor_token(')');
6352 expression->builtin_constant.value = parse_assignment_expression();
6353 rem_anchor_token(')');
6355 expression->base.type = type_int;
6359 return create_invalid_expression();
6363 * Parses a __builtin_prefetch() expression.
6365 static expression_t *parse_builtin_prefetch(void)
6367 eat(T___builtin_prefetch);
6369 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6372 add_anchor_token(')');
6373 expression->builtin_prefetch.adr = parse_assignment_expression();
6374 if (token.type == ',') {
6376 expression->builtin_prefetch.rw = parse_assignment_expression();
6378 if (token.type == ',') {
6380 expression->builtin_prefetch.locality = parse_assignment_expression();
6382 rem_anchor_token(')');
6384 expression->base.type = type_void;
6388 return create_invalid_expression();
6392 * Parses a __builtin_is_*() compare expression.
6394 static expression_t *parse_compare_builtin(void)
6396 expression_t *expression;
6398 switch(token.type) {
6399 case T___builtin_isgreater:
6400 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6402 case T___builtin_isgreaterequal:
6403 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6405 case T___builtin_isless:
6406 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6408 case T___builtin_islessequal:
6409 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6411 case T___builtin_islessgreater:
6412 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6414 case T___builtin_isunordered:
6415 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6418 internal_errorf(HERE, "invalid compare builtin found");
6421 expression->base.source_position = *HERE;
6425 expression->binary.left = parse_assignment_expression();
6427 expression->binary.right = parse_assignment_expression();
6430 type_t *const orig_type_left = expression->binary.left->base.type;
6431 type_t *const orig_type_right = expression->binary.right->base.type;
6433 type_t *const type_left = skip_typeref(orig_type_left);
6434 type_t *const type_right = skip_typeref(orig_type_right);
6435 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6436 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6437 type_error_incompatible("invalid operands in comparison",
6438 &expression->base.source_position, orig_type_left, orig_type_right);
6441 semantic_comparison(&expression->binary);
6446 return create_invalid_expression();
6451 * Parses a __builtin_expect() expression.
6453 static expression_t *parse_builtin_expect(void)
6455 eat(T___builtin_expect);
6457 expression_t *expression
6458 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6461 expression->binary.left = parse_assignment_expression();
6463 expression->binary.right = parse_constant_expression();
6466 expression->base.type = expression->binary.left->base.type;
6470 return create_invalid_expression();
6475 * Parses a MS assume() expression.
6477 static expression_t *parse_assume(void)
6481 expression_t *expression
6482 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6485 add_anchor_token(')');
6486 expression->unary.value = parse_assignment_expression();
6487 rem_anchor_token(')');
6490 expression->base.type = type_void;
6493 return create_invalid_expression();
6497 * Parse a microsoft __noop expression.
6499 static expression_t *parse_noop_expression(void)
6501 source_position_t source_position = *HERE;
6504 if (token.type == '(') {
6505 /* parse arguments */
6507 add_anchor_token(')');
6508 add_anchor_token(',');
6510 if (token.type != ')') {
6512 (void)parse_assignment_expression();
6513 if (token.type != ',')
6519 rem_anchor_token(',');
6520 rem_anchor_token(')');
6523 /* the result is a (int)0 */
6524 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6525 cnst->base.source_position = source_position;
6526 cnst->base.type = type_int;
6527 cnst->conste.v.int_value = 0;
6528 cnst->conste.is_ms_noop = true;
6533 return create_invalid_expression();
6537 * Parses a primary expression.
6539 static expression_t *parse_primary_expression(void)
6541 switch (token.type) {
6542 case T_INTEGER: return parse_int_const();
6543 case T_CHARACTER_CONSTANT: return parse_character_constant();
6544 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
6545 case T_FLOATINGPOINT: return parse_float_const();
6546 case T_STRING_LITERAL:
6547 case T_WIDE_STRING_LITERAL: return parse_string_const();
6548 case T_IDENTIFIER: return parse_reference();
6549 case T___FUNCTION__:
6550 case T___func__: return parse_function_keyword();
6551 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
6552 case T___FUNCSIG__: return parse_funcsig_keyword();
6553 case T___FUNCDNAME__: return parse_funcdname_keyword();
6554 case T___builtin_offsetof: return parse_offsetof();
6555 case T___builtin_va_start: return parse_va_start();
6556 case T___builtin_va_arg: return parse_va_arg();
6557 case T___builtin_expect:
6558 case T___builtin_alloca:
6559 case T___builtin_nan:
6560 case T___builtin_nand:
6561 case T___builtin_nanf:
6562 case T___builtin_huge_val:
6563 case T___builtin_va_end: return parse_builtin_symbol();
6564 case T___builtin_isgreater:
6565 case T___builtin_isgreaterequal:
6566 case T___builtin_isless:
6567 case T___builtin_islessequal:
6568 case T___builtin_islessgreater:
6569 case T___builtin_isunordered: return parse_compare_builtin();
6570 case T___builtin_constant_p: return parse_builtin_constant();
6571 case T___builtin_prefetch: return parse_builtin_prefetch();
6572 case T__assume: return parse_assume();
6574 case '(': return parse_parenthesized_expression();
6575 case T___noop: return parse_noop_expression();
6578 errorf(HERE, "unexpected token %K, expected an expression", &token);
6579 return create_invalid_expression();
6583 * Check if the expression has the character type and issue a warning then.
6585 static void check_for_char_index_type(const expression_t *expression)
6587 type_t *const type = expression->base.type;
6588 const type_t *const base_type = skip_typeref(type);
6590 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
6591 warning.char_subscripts) {
6592 warningf(&expression->base.source_position,
6593 "array subscript has type '%T'", type);
6597 static expression_t *parse_array_expression(unsigned precedence,
6603 add_anchor_token(']');
6605 expression_t *inside = parse_expression();
6607 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
6609 array_access_expression_t *array_access = &expression->array_access;
6611 type_t *const orig_type_left = left->base.type;
6612 type_t *const orig_type_inside = inside->base.type;
6614 type_t *const type_left = skip_typeref(orig_type_left);
6615 type_t *const type_inside = skip_typeref(orig_type_inside);
6617 type_t *return_type;
6618 if (is_type_pointer(type_left)) {
6619 return_type = type_left->pointer.points_to;
6620 array_access->array_ref = left;
6621 array_access->index = inside;
6622 check_for_char_index_type(inside);
6623 } else if (is_type_pointer(type_inside)) {
6624 return_type = type_inside->pointer.points_to;
6625 array_access->array_ref = inside;
6626 array_access->index = left;
6627 array_access->flipped = true;
6628 check_for_char_index_type(left);
6630 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
6632 "array access on object with non-pointer types '%T', '%T'",
6633 orig_type_left, orig_type_inside);
6635 return_type = type_error_type;
6636 array_access->array_ref = create_invalid_expression();
6639 rem_anchor_token(']');
6640 if (token.type != ']') {
6641 parse_error_expected("Problem while parsing array access", ']', NULL);
6646 return_type = automatic_type_conversion(return_type);
6647 expression->base.type = return_type;
6652 static expression_t *parse_typeprop(expression_kind_t const kind,
6653 source_position_t const pos,
6654 unsigned const precedence)
6656 expression_t *tp_expression = allocate_expression_zero(kind);
6657 tp_expression->base.type = type_size_t;
6658 tp_expression->base.source_position = pos;
6660 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
6662 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
6664 add_anchor_token(')');
6665 type_t* const orig_type = parse_typename();
6666 tp_expression->typeprop.type = orig_type;
6668 type_t const* const type = skip_typeref(orig_type);
6669 char const* const wrong_type =
6670 is_type_incomplete(type) ? "incomplete" :
6671 type->kind == TYPE_FUNCTION ? "function designator" :
6672 type->kind == TYPE_BITFIELD ? "bitfield" :
6674 if (wrong_type != NULL) {
6675 errorf(&pos, "operand of %s expression must not be %s type '%T'",
6676 what, wrong_type, type);
6679 rem_anchor_token(')');
6682 expression_t *expression = parse_sub_expression(precedence);
6684 type_t* const orig_type = revert_automatic_type_conversion(expression);
6685 expression->base.type = orig_type;
6687 type_t const* const type = skip_typeref(orig_type);
6688 char const* const wrong_type =
6689 is_type_incomplete(type) ? "incomplete" :
6690 type->kind == TYPE_FUNCTION ? "function designator" :
6691 type->kind == TYPE_BITFIELD ? "bitfield" :
6693 if (wrong_type != NULL) {
6694 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6697 tp_expression->typeprop.type = expression->base.type;
6698 tp_expression->typeprop.tp_expression = expression;
6701 return tp_expression;
6703 return create_invalid_expression();
6706 static expression_t *parse_sizeof(unsigned precedence)
6708 source_position_t pos = *HERE;
6710 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6713 static expression_t *parse_alignof(unsigned precedence)
6715 source_position_t pos = *HERE;
6717 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6720 static expression_t *parse_select_expression(unsigned precedence,
6721 expression_t *compound)
6724 assert(token.type == '.' || token.type == T_MINUSGREATER);
6726 bool is_pointer = (token.type == T_MINUSGREATER);
6729 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6730 select->select.compound = compound;
6732 if (token.type != T_IDENTIFIER) {
6733 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6736 symbol_t *symbol = token.v.symbol;
6737 select->select.symbol = symbol;
6740 type_t *const orig_type = compound->base.type;
6741 type_t *const type = skip_typeref(orig_type);
6743 type_t *type_left = type;
6745 if (!is_type_pointer(type)) {
6746 if (is_type_valid(type)) {
6747 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6749 return create_invalid_expression();
6751 type_left = type->pointer.points_to;
6753 type_left = skip_typeref(type_left);
6755 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6756 type_left->kind != TYPE_COMPOUND_UNION) {
6757 if (is_type_valid(type_left)) {
6758 errorf(HERE, "request for member '%Y' in something not a struct or "
6759 "union, but '%T'", symbol, type_left);
6761 return create_invalid_expression();
6764 declaration_t *const declaration = type_left->compound.declaration;
6766 if (!declaration->init.complete) {
6767 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6769 return create_invalid_expression();
6772 declaration_t *iter = find_compound_entry(declaration, symbol);
6774 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6775 return create_invalid_expression();
6778 /* we always do the auto-type conversions; the & and sizeof parser contains
6779 * code to revert this! */
6780 type_t *expression_type = automatic_type_conversion(iter->type);
6782 select->select.compound_entry = iter;
6783 select->base.type = expression_type;
6785 type_t *skipped = skip_typeref(iter->type);
6786 if (skipped->kind == TYPE_BITFIELD) {
6787 select->base.type = skipped->bitfield.base_type;
6793 static void check_call_argument(const function_parameter_t *parameter,
6794 call_argument_t *argument)
6796 type_t *expected_type = parameter->type;
6797 type_t *expected_type_skip = skip_typeref(expected_type);
6798 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6799 expression_t *arg_expr = argument->expression;
6801 /* handle transparent union gnu extension */
6802 if (is_type_union(expected_type_skip)
6803 && (expected_type_skip->base.modifiers
6804 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6805 declaration_t *union_decl = expected_type_skip->compound.declaration;
6807 declaration_t *declaration = union_decl->scope.declarations;
6808 type_t *best_type = NULL;
6809 for ( ; declaration != NULL; declaration = declaration->next) {
6810 type_t *decl_type = declaration->type;
6811 error = semantic_assign(decl_type, arg_expr);
6812 if (error == ASSIGN_ERROR_INCOMPATIBLE
6813 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6816 if (error == ASSIGN_SUCCESS) {
6817 best_type = decl_type;
6818 } else if (best_type == NULL) {
6819 best_type = decl_type;
6823 if (best_type != NULL) {
6824 expected_type = best_type;
6828 error = semantic_assign(expected_type, arg_expr);
6829 argument->expression = create_implicit_cast(argument->expression,
6832 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6833 report_assign_error(error, expected_type, arg_expr, "function call",
6834 &arg_expr->base.source_position);
6838 * Parse a call expression, ie. expression '( ... )'.
6840 * @param expression the function address
6842 static expression_t *parse_call_expression(unsigned precedence,
6843 expression_t *expression)
6846 expression_t *result = allocate_expression_zero(EXPR_CALL);
6847 result->base.source_position = expression->base.source_position;
6849 call_expression_t *call = &result->call;
6850 call->function = expression;
6852 type_t *const orig_type = expression->base.type;
6853 type_t *const type = skip_typeref(orig_type);
6855 function_type_t *function_type = NULL;
6856 if (is_type_pointer(type)) {
6857 type_t *const to_type = skip_typeref(type->pointer.points_to);
6859 if (is_type_function(to_type)) {
6860 function_type = &to_type->function;
6861 call->base.type = function_type->return_type;
6865 if (function_type == NULL && is_type_valid(type)) {
6866 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6869 /* parse arguments */
6871 add_anchor_token(')');
6872 add_anchor_token(',');
6874 if (token.type != ')') {
6875 call_argument_t *last_argument = NULL;
6878 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6880 argument->expression = parse_assignment_expression();
6881 if (last_argument == NULL) {
6882 call->arguments = argument;
6884 last_argument->next = argument;
6886 last_argument = argument;
6888 if (token.type != ',')
6893 rem_anchor_token(',');
6894 rem_anchor_token(')');
6897 if (function_type == NULL)
6900 function_parameter_t *parameter = function_type->parameters;
6901 call_argument_t *argument = call->arguments;
6902 if (!function_type->unspecified_parameters) {
6903 for( ; parameter != NULL && argument != NULL;
6904 parameter = parameter->next, argument = argument->next) {
6905 check_call_argument(parameter, argument);
6908 if (parameter != NULL) {
6909 errorf(HERE, "too few arguments to function '%E'", expression);
6910 } else if (argument != NULL && !function_type->variadic) {
6911 errorf(HERE, "too many arguments to function '%E'", expression);
6915 /* do default promotion */
6916 for( ; argument != NULL; argument = argument->next) {
6917 type_t *type = argument->expression->base.type;
6919 type = get_default_promoted_type(type);
6921 argument->expression
6922 = create_implicit_cast(argument->expression, type);
6925 check_format(&result->call);
6927 if (warning.aggregate_return &&
6928 is_type_compound(skip_typeref(function_type->return_type))) {
6929 warningf(&result->base.source_position,
6930 "function call has aggregate value");
6935 return create_invalid_expression();
6938 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6940 static bool same_compound_type(const type_t *type1, const type_t *type2)
6943 is_type_compound(type1) &&
6944 type1->kind == type2->kind &&
6945 type1->compound.declaration == type2->compound.declaration;
6949 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6951 * @param expression the conditional expression
6953 static expression_t *parse_conditional_expression(unsigned precedence,
6954 expression_t *expression)
6956 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6958 conditional_expression_t *conditional = &result->conditional;
6959 conditional->base.source_position = *HERE;
6960 conditional->condition = expression;
6963 add_anchor_token(':');
6966 type_t *const condition_type_orig = expression->base.type;
6967 type_t *const condition_type = skip_typeref(condition_type_orig);
6968 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6969 type_error("expected a scalar type in conditional condition",
6970 &expression->base.source_position, condition_type_orig);
6973 expression_t *true_expression = expression;
6974 bool gnu_cond = false;
6975 if ((c_mode & _GNUC) && token.type == ':') {
6978 true_expression = parse_expression();
6979 rem_anchor_token(':');
6981 expression_t *false_expression = parse_sub_expression(precedence);
6983 type_t *const orig_true_type = true_expression->base.type;
6984 type_t *const orig_false_type = false_expression->base.type;
6985 type_t *const true_type = skip_typeref(orig_true_type);
6986 type_t *const false_type = skip_typeref(orig_false_type);
6989 type_t *result_type;
6990 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6991 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6992 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6993 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6994 warningf(&conditional->base.source_position,
6995 "ISO C forbids conditional expression with only one void side");
6997 result_type = type_void;
6998 } else if (is_type_arithmetic(true_type)
6999 && is_type_arithmetic(false_type)) {
7000 result_type = semantic_arithmetic(true_type, false_type);
7002 true_expression = create_implicit_cast(true_expression, result_type);
7003 false_expression = create_implicit_cast(false_expression, result_type);
7005 conditional->true_expression = true_expression;
7006 conditional->false_expression = false_expression;
7007 conditional->base.type = result_type;
7008 } else if (same_compound_type(true_type, false_type)) {
7009 /* just take 1 of the 2 types */
7010 result_type = true_type;
7011 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
7012 type_t *pointer_type;
7014 expression_t *other_expression;
7015 if (is_type_pointer(true_type) &&
7016 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
7017 pointer_type = true_type;
7018 other_type = false_type;
7019 other_expression = false_expression;
7021 pointer_type = false_type;
7022 other_type = true_type;
7023 other_expression = true_expression;
7026 if (is_null_pointer_constant(other_expression)) {
7027 result_type = pointer_type;
7028 } else if (is_type_pointer(other_type)) {
7029 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7030 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7033 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7034 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7036 } else if (types_compatible(get_unqualified_type(to1),
7037 get_unqualified_type(to2))) {
7040 warningf(&conditional->base.source_position,
7041 "pointer types '%T' and '%T' in conditional expression are incompatible",
7042 true_type, false_type);
7046 type_t *const copy = duplicate_type(to);
7047 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
7049 type_t *const type = typehash_insert(copy);
7053 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7054 } else if (is_type_integer(other_type)) {
7055 warningf(&conditional->base.source_position,
7056 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7057 result_type = pointer_type;
7059 type_error_incompatible("while parsing conditional",
7060 &expression->base.source_position, true_type, false_type);
7061 result_type = type_error_type;
7064 /* TODO: one pointer to void*, other some pointer */
7066 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7067 type_error_incompatible("while parsing conditional",
7068 &conditional->base.source_position, true_type,
7071 result_type = type_error_type;
7074 conditional->true_expression
7075 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7076 conditional->false_expression
7077 = create_implicit_cast(false_expression, result_type);
7078 conditional->base.type = result_type;
7081 return create_invalid_expression();
7085 * Parse an extension expression.
7087 static expression_t *parse_extension(unsigned precedence)
7089 eat(T___extension__);
7091 /* TODO enable extensions */
7092 expression_t *expression = parse_sub_expression(precedence);
7093 /* TODO disable extensions */
7098 * Parse a __builtin_classify_type() expression.
7100 static expression_t *parse_builtin_classify_type(const unsigned precedence)
7102 eat(T___builtin_classify_type);
7104 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7105 result->base.type = type_int;
7108 add_anchor_token(')');
7109 expression_t *expression = parse_sub_expression(precedence);
7110 rem_anchor_token(')');
7112 result->classify_type.type_expression = expression;
7116 return create_invalid_expression();
7119 static bool check_pointer_arithmetic(const source_position_t *source_position,
7120 type_t *pointer_type,
7121 type_t *orig_pointer_type)
7123 type_t *points_to = pointer_type->pointer.points_to;
7124 points_to = skip_typeref(points_to);
7126 if (is_type_incomplete(points_to)) {
7127 if (!(c_mode & _GNUC) || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7128 errorf(source_position,
7129 "arithmetic with pointer to incomplete type '%T' not allowed",
7132 } else if (warning.pointer_arith) {
7133 warningf(source_position,
7134 "pointer of type '%T' used in arithmetic",
7137 } else if (is_type_function(points_to)) {
7138 if (!(c_mode && _GNUC)) {
7139 errorf(source_position,
7140 "arithmetic with pointer to function type '%T' not allowed",
7143 } else if (warning.pointer_arith) {
7144 warningf(source_position,
7145 "pointer to a function '%T' used in arithmetic",
7152 static bool is_lvalue(const expression_t *expression)
7154 switch (expression->kind) {
7155 case EXPR_REFERENCE:
7156 case EXPR_ARRAY_ACCESS:
7158 case EXPR_UNARY_DEREFERENCE:
7166 static void semantic_incdec(unary_expression_t *expression)
7168 type_t *const orig_type = expression->value->base.type;
7169 type_t *const type = skip_typeref(orig_type);
7170 if (is_type_pointer(type)) {
7171 if (!check_pointer_arithmetic(&expression->base.source_position,
7175 } else if (!is_type_real(type) && is_type_valid(type)) {
7176 /* TODO: improve error message */
7177 errorf(&expression->base.source_position,
7178 "operation needs an arithmetic or pointer type");
7181 if (!is_lvalue(expression->value)) {
7182 /* TODO: improve error message */
7183 errorf(&expression->base.source_position, "lvalue required as operand");
7185 expression->base.type = orig_type;
7188 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7190 type_t *const orig_type = expression->value->base.type;
7191 type_t *const type = skip_typeref(orig_type);
7192 if (!is_type_arithmetic(type)) {
7193 if (is_type_valid(type)) {
7194 /* TODO: improve error message */
7195 errorf(&expression->base.source_position,
7196 "operation needs an arithmetic type");
7201 expression->base.type = orig_type;
7204 static void semantic_not(unary_expression_t *expression)
7206 type_t *const orig_type = expression->value->base.type;
7207 type_t *const type = skip_typeref(orig_type);
7208 if (!is_type_scalar(type) && is_type_valid(type)) {
7209 errorf(&expression->base.source_position,
7210 "operand of ! must be of scalar type");
7213 expression->base.type = type_int;
7216 static void semantic_unexpr_integer(unary_expression_t *expression)
7218 type_t *const orig_type = expression->value->base.type;
7219 type_t *const type = skip_typeref(orig_type);
7220 if (!is_type_integer(type)) {
7221 if (is_type_valid(type)) {
7222 errorf(&expression->base.source_position,
7223 "operand of ~ must be of integer type");
7228 expression->base.type = orig_type;
7231 static void semantic_dereference(unary_expression_t *expression)
7233 type_t *const orig_type = expression->value->base.type;
7234 type_t *const type = skip_typeref(orig_type);
7235 if (!is_type_pointer(type)) {
7236 if (is_type_valid(type)) {
7237 errorf(&expression->base.source_position,
7238 "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
7243 type_t *result_type = type->pointer.points_to;
7244 result_type = automatic_type_conversion(result_type);
7245 expression->base.type = result_type;
7248 static void set_address_taken(expression_t *expression, bool may_be_register)
7250 if (expression->kind != EXPR_REFERENCE)
7253 declaration_t *const declaration = expression->reference.declaration;
7254 /* happens for parse errors */
7255 if (declaration == NULL)
7258 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7259 errorf(&expression->base.source_position,
7260 "address of register variable '%Y' requested",
7261 declaration->symbol);
7263 declaration->address_taken = 1;
7268 * Check the semantic of the address taken expression.
7270 static void semantic_take_addr(unary_expression_t *expression)
7272 expression_t *value = expression->value;
7273 value->base.type = revert_automatic_type_conversion(value);
7275 type_t *orig_type = value->base.type;
7276 if (!is_type_valid(orig_type))
7279 set_address_taken(value, false);
7281 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7284 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7285 static expression_t *parse_##unexpression_type(unsigned precedence) \
7287 expression_t *unary_expression \
7288 = allocate_expression_zero(unexpression_type); \
7289 unary_expression->base.source_position = *HERE; \
7291 unary_expression->unary.value = parse_sub_expression(precedence); \
7293 sfunc(&unary_expression->unary); \
7295 return unary_expression; \
7298 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7299 semantic_unexpr_arithmetic)
7300 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7301 semantic_unexpr_arithmetic)
7302 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7304 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7305 semantic_dereference)
7306 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7308 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7309 semantic_unexpr_integer)
7310 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7312 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7315 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7317 static expression_t *parse_##unexpression_type(unsigned precedence, \
7318 expression_t *left) \
7320 (void) precedence; \
7322 expression_t *unary_expression \
7323 = allocate_expression_zero(unexpression_type); \
7324 unary_expression->base.source_position = *HERE; \
7326 unary_expression->unary.value = left; \
7328 sfunc(&unary_expression->unary); \
7330 return unary_expression; \
7333 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7334 EXPR_UNARY_POSTFIX_INCREMENT,
7336 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7337 EXPR_UNARY_POSTFIX_DECREMENT,
7340 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7342 /* TODO: handle complex + imaginary types */
7344 /* § 6.3.1.8 Usual arithmetic conversions */
7345 if (type_left == type_long_double || type_right == type_long_double) {
7346 return type_long_double;
7347 } else if (type_left == type_double || type_right == type_double) {
7349 } else if (type_left == type_float || type_right == type_float) {
7353 type_left = promote_integer(type_left);
7354 type_right = promote_integer(type_right);
7356 if (type_left == type_right)
7359 bool const signed_left = is_type_signed(type_left);
7360 bool const signed_right = is_type_signed(type_right);
7361 int const rank_left = get_rank(type_left);
7362 int const rank_right = get_rank(type_right);
7364 if (signed_left == signed_right)
7365 return rank_left >= rank_right ? type_left : type_right;
7374 u_rank = rank_right;
7375 u_type = type_right;
7377 s_rank = rank_right;
7378 s_type = type_right;
7383 if (u_rank >= s_rank)
7386 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7388 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7389 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7393 case ATOMIC_TYPE_INT: return type_unsigned_int;
7394 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7395 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7397 default: panic("invalid atomic type");
7402 * Check the semantic restrictions for a binary expression.
7404 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7406 expression_t *const left = expression->left;
7407 expression_t *const right = expression->right;
7408 type_t *const orig_type_left = left->base.type;
7409 type_t *const orig_type_right = right->base.type;
7410 type_t *const type_left = skip_typeref(orig_type_left);
7411 type_t *const type_right = skip_typeref(orig_type_right);
7413 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7414 /* TODO: improve error message */
7415 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7416 errorf(&expression->base.source_position,
7417 "operation needs arithmetic types");
7422 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7423 expression->left = create_implicit_cast(left, arithmetic_type);
7424 expression->right = create_implicit_cast(right, arithmetic_type);
7425 expression->base.type = arithmetic_type;
7428 static void warn_div_by_zero(binary_expression_t const *const expression)
7430 if (warning.div_by_zero &&
7431 is_type_integer(expression->base.type) &&
7432 is_constant_expression(expression->right) &&
7433 fold_constant(expression->right) == 0) {
7434 warningf(&expression->base.source_position, "division by zero");
7439 * Check the semantic restrictions for a div/mod expression.
7441 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
7442 semantic_binexpr_arithmetic(expression);
7443 warn_div_by_zero(expression);
7446 static void semantic_shift_op(binary_expression_t *expression)
7448 expression_t *const left = expression->left;
7449 expression_t *const right = expression->right;
7450 type_t *const orig_type_left = left->base.type;
7451 type_t *const orig_type_right = right->base.type;
7452 type_t * type_left = skip_typeref(orig_type_left);
7453 type_t * type_right = skip_typeref(orig_type_right);
7455 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
7456 /* TODO: improve error message */
7457 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7458 errorf(&expression->base.source_position,
7459 "operands of shift operation must have integer types");
7464 type_left = promote_integer(type_left);
7465 type_right = promote_integer(type_right);
7467 expression->left = create_implicit_cast(left, type_left);
7468 expression->right = create_implicit_cast(right, type_right);
7469 expression->base.type = type_left;
7472 static void semantic_add(binary_expression_t *expression)
7474 expression_t *const left = expression->left;
7475 expression_t *const right = expression->right;
7476 type_t *const orig_type_left = left->base.type;
7477 type_t *const orig_type_right = right->base.type;
7478 type_t *const type_left = skip_typeref(orig_type_left);
7479 type_t *const type_right = skip_typeref(orig_type_right);
7482 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7483 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7484 expression->left = create_implicit_cast(left, arithmetic_type);
7485 expression->right = create_implicit_cast(right, arithmetic_type);
7486 expression->base.type = arithmetic_type;
7488 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7489 check_pointer_arithmetic(&expression->base.source_position,
7490 type_left, orig_type_left);
7491 expression->base.type = type_left;
7492 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
7493 check_pointer_arithmetic(&expression->base.source_position,
7494 type_right, orig_type_right);
7495 expression->base.type = type_right;
7496 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7497 errorf(&expression->base.source_position,
7498 "invalid operands to binary + ('%T', '%T')",
7499 orig_type_left, orig_type_right);
7503 static void semantic_sub(binary_expression_t *expression)
7505 expression_t *const left = expression->left;
7506 expression_t *const right = expression->right;
7507 type_t *const orig_type_left = left->base.type;
7508 type_t *const orig_type_right = right->base.type;
7509 type_t *const type_left = skip_typeref(orig_type_left);
7510 type_t *const type_right = skip_typeref(orig_type_right);
7511 source_position_t const *const pos = &expression->base.source_position;
7514 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7515 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7516 expression->left = create_implicit_cast(left, arithmetic_type);
7517 expression->right = create_implicit_cast(right, arithmetic_type);
7518 expression->base.type = arithmetic_type;
7520 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7521 check_pointer_arithmetic(&expression->base.source_position,
7522 type_left, orig_type_left);
7523 expression->base.type = type_left;
7524 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
7525 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
7526 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
7527 if (!types_compatible(unqual_left, unqual_right)) {
7529 "subtracting pointers to incompatible types '%T' and '%T'",
7530 orig_type_left, orig_type_right);
7531 } else if (!is_type_object(unqual_left)) {
7532 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
7533 warningf(pos, "subtracting pointers to void");
7535 errorf(pos, "subtracting pointers to non-object types '%T'",
7539 expression->base.type = type_ptrdiff_t;
7540 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7541 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
7542 orig_type_left, orig_type_right);
7547 * Check the semantics of comparison expressions.
7549 * @param expression The expression to check.
7551 static void semantic_comparison(binary_expression_t *expression)
7553 expression_t *left = expression->left;
7554 expression_t *right = expression->right;
7555 type_t *orig_type_left = left->base.type;
7556 type_t *orig_type_right = right->base.type;
7558 type_t *type_left = skip_typeref(orig_type_left);
7559 type_t *type_right = skip_typeref(orig_type_right);
7561 /* TODO non-arithmetic types */
7562 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7563 /* test for signed vs unsigned compares */
7564 if (warning.sign_compare &&
7565 (expression->base.kind != EXPR_BINARY_EQUAL &&
7566 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
7567 (is_type_signed(type_left) != is_type_signed(type_right))) {
7569 /* check if 1 of the operands is a constant, in this case we just
7570 * check wether we can safely represent the resulting constant in
7571 * the type of the other operand. */
7572 expression_t *const_expr = NULL;
7573 expression_t *other_expr = NULL;
7575 if (is_constant_expression(left)) {
7578 } else if (is_constant_expression(right)) {
7583 if (const_expr != NULL) {
7584 type_t *other_type = skip_typeref(other_expr->base.type);
7585 long val = fold_constant(const_expr);
7586 /* TODO: check if val can be represented by other_type */
7590 warningf(&expression->base.source_position,
7591 "comparison between signed and unsigned");
7593 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7594 expression->left = create_implicit_cast(left, arithmetic_type);
7595 expression->right = create_implicit_cast(right, arithmetic_type);
7596 expression->base.type = arithmetic_type;
7597 if (warning.float_equal &&
7598 (expression->base.kind == EXPR_BINARY_EQUAL ||
7599 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
7600 is_type_float(arithmetic_type)) {
7601 warningf(&expression->base.source_position,
7602 "comparing floating point with == or != is unsafe");
7604 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
7605 /* TODO check compatibility */
7606 } else if (is_type_pointer(type_left)) {
7607 expression->right = create_implicit_cast(right, type_left);
7608 } else if (is_type_pointer(type_right)) {
7609 expression->left = create_implicit_cast(left, type_right);
7610 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7611 type_error_incompatible("invalid operands in comparison",
7612 &expression->base.source_position,
7613 type_left, type_right);
7615 expression->base.type = type_int;
7619 * Checks if a compound type has constant fields.
7621 static bool has_const_fields(const compound_type_t *type)
7623 const scope_t *scope = &type->declaration->scope;
7624 const declaration_t *declaration = scope->declarations;
7626 for (; declaration != NULL; declaration = declaration->next) {
7627 if (declaration->namespc != NAMESPACE_NORMAL)
7630 const type_t *decl_type = skip_typeref(declaration->type);
7631 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
7638 static bool is_valid_assignment_lhs(expression_t const* const left)
7640 type_t *const orig_type_left = revert_automatic_type_conversion(left);
7641 type_t *const type_left = skip_typeref(orig_type_left);
7643 if (!is_lvalue(left)) {
7644 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
7649 if (is_type_array(type_left)) {
7650 errorf(HERE, "cannot assign to arrays ('%E')", left);
7653 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
7654 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
7658 if (is_type_incomplete(type_left)) {
7659 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
7660 left, orig_type_left);
7663 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
7664 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
7665 left, orig_type_left);
7672 static void semantic_arithmetic_assign(binary_expression_t *expression)
7674 expression_t *left = expression->left;
7675 expression_t *right = expression->right;
7676 type_t *orig_type_left = left->base.type;
7677 type_t *orig_type_right = right->base.type;
7679 if (!is_valid_assignment_lhs(left))
7682 type_t *type_left = skip_typeref(orig_type_left);
7683 type_t *type_right = skip_typeref(orig_type_right);
7685 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7686 /* TODO: improve error message */
7687 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7688 errorf(&expression->base.source_position,
7689 "operation needs arithmetic types");
7694 /* combined instructions are tricky. We can't create an implicit cast on
7695 * the left side, because we need the uncasted form for the store.
7696 * The ast2firm pass has to know that left_type must be right_type
7697 * for the arithmetic operation and create a cast by itself */
7698 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7699 expression->right = create_implicit_cast(right, arithmetic_type);
7700 expression->base.type = type_left;
7703 static void semantic_divmod_assign(binary_expression_t *expression)
7705 semantic_arithmetic_assign(expression);
7706 warn_div_by_zero(expression);
7709 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
7711 expression_t *const left = expression->left;
7712 expression_t *const right = expression->right;
7713 type_t *const orig_type_left = left->base.type;
7714 type_t *const orig_type_right = right->base.type;
7715 type_t *const type_left = skip_typeref(orig_type_left);
7716 type_t *const type_right = skip_typeref(orig_type_right);
7718 if (!is_valid_assignment_lhs(left))
7721 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7722 /* combined instructions are tricky. We can't create an implicit cast on
7723 * the left side, because we need the uncasted form for the store.
7724 * The ast2firm pass has to know that left_type must be right_type
7725 * for the arithmetic operation and create a cast by itself */
7726 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
7727 expression->right = create_implicit_cast(right, arithmetic_type);
7728 expression->base.type = type_left;
7729 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7730 check_pointer_arithmetic(&expression->base.source_position,
7731 type_left, orig_type_left);
7732 expression->base.type = type_left;
7733 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7734 errorf(&expression->base.source_position,
7735 "incompatible types '%T' and '%T' in assignment",
7736 orig_type_left, orig_type_right);
7741 * Check the semantic restrictions of a logical expression.
7743 static void semantic_logical_op(binary_expression_t *expression)
7745 expression_t *const left = expression->left;
7746 expression_t *const right = expression->right;
7747 type_t *const orig_type_left = left->base.type;
7748 type_t *const orig_type_right = right->base.type;
7749 type_t *const type_left = skip_typeref(orig_type_left);
7750 type_t *const type_right = skip_typeref(orig_type_right);
7752 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7753 /* TODO: improve error message */
7754 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7755 errorf(&expression->base.source_position,
7756 "operation needs scalar types");
7761 expression->base.type = type_int;
7765 * Check the semantic restrictions of a binary assign expression.
7767 static void semantic_binexpr_assign(binary_expression_t *expression)
7769 expression_t *left = expression->left;
7770 type_t *orig_type_left = left->base.type;
7772 type_t *type_left = revert_automatic_type_conversion(left);
7773 type_left = skip_typeref(orig_type_left);
7775 if (!is_valid_assignment_lhs(left))
7778 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7779 report_assign_error(error, orig_type_left, expression->right,
7780 "assignment", &left->base.source_position);
7781 expression->right = create_implicit_cast(expression->right, orig_type_left);
7782 expression->base.type = orig_type_left;
7786 * Determine if the outermost operation (or parts thereof) of the given
7787 * expression has no effect in order to generate a warning about this fact.
7788 * Therefore in some cases this only examines some of the operands of the
7789 * expression (see comments in the function and examples below).
7791 * f() + 23; // warning, because + has no effect
7792 * x || f(); // no warning, because x controls execution of f()
7793 * x ? y : f(); // warning, because y has no effect
7794 * (void)x; // no warning to be able to suppress the warning
7795 * This function can NOT be used for an "expression has definitely no effect"-
7797 static bool expression_has_effect(const expression_t *const expr)
7799 switch (expr->kind) {
7800 case EXPR_UNKNOWN: break;
7801 case EXPR_INVALID: return true; /* do NOT warn */
7802 case EXPR_REFERENCE: return false;
7803 /* suppress the warning for microsoft __noop operations */
7804 case EXPR_CONST: return expr->conste.is_ms_noop;
7805 case EXPR_CHARACTER_CONSTANT: return false;
7806 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7807 case EXPR_STRING_LITERAL: return false;
7808 case EXPR_WIDE_STRING_LITERAL: return false;
7811 const call_expression_t *const call = &expr->call;
7812 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7815 switch (call->function->builtin_symbol.symbol->ID) {
7816 case T___builtin_va_end: return true;
7817 default: return false;
7821 /* Generate the warning if either the left or right hand side of a
7822 * conditional expression has no effect */
7823 case EXPR_CONDITIONAL: {
7824 const conditional_expression_t *const cond = &expr->conditional;
7826 expression_has_effect(cond->true_expression) &&
7827 expression_has_effect(cond->false_expression);
7830 case EXPR_SELECT: return false;
7831 case EXPR_ARRAY_ACCESS: return false;
7832 case EXPR_SIZEOF: return false;
7833 case EXPR_CLASSIFY_TYPE: return false;
7834 case EXPR_ALIGNOF: return false;
7836 case EXPR_FUNCNAME: return false;
7837 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7838 case EXPR_BUILTIN_CONSTANT_P: return false;
7839 case EXPR_BUILTIN_PREFETCH: return true;
7840 case EXPR_OFFSETOF: return false;
7841 case EXPR_VA_START: return true;
7842 case EXPR_VA_ARG: return true;
7843 case EXPR_STATEMENT: return true; // TODO
7844 case EXPR_COMPOUND_LITERAL: return false;
7846 case EXPR_UNARY_NEGATE: return false;
7847 case EXPR_UNARY_PLUS: return false;
7848 case EXPR_UNARY_BITWISE_NEGATE: return false;
7849 case EXPR_UNARY_NOT: return false;
7850 case EXPR_UNARY_DEREFERENCE: return false;
7851 case EXPR_UNARY_TAKE_ADDRESS: return false;
7852 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7853 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7854 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7855 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7857 /* Treat void casts as if they have an effect in order to being able to
7858 * suppress the warning */
7859 case EXPR_UNARY_CAST: {
7860 type_t *const type = skip_typeref(expr->base.type);
7861 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7864 case EXPR_UNARY_CAST_IMPLICIT: return true;
7865 case EXPR_UNARY_ASSUME: return true;
7867 case EXPR_BINARY_ADD: return false;
7868 case EXPR_BINARY_SUB: return false;
7869 case EXPR_BINARY_MUL: return false;
7870 case EXPR_BINARY_DIV: return false;
7871 case EXPR_BINARY_MOD: return false;
7872 case EXPR_BINARY_EQUAL: return false;
7873 case EXPR_BINARY_NOTEQUAL: return false;
7874 case EXPR_BINARY_LESS: return false;
7875 case EXPR_BINARY_LESSEQUAL: return false;
7876 case EXPR_BINARY_GREATER: return false;
7877 case EXPR_BINARY_GREATEREQUAL: return false;
7878 case EXPR_BINARY_BITWISE_AND: return false;
7879 case EXPR_BINARY_BITWISE_OR: return false;
7880 case EXPR_BINARY_BITWISE_XOR: return false;
7881 case EXPR_BINARY_SHIFTLEFT: return false;
7882 case EXPR_BINARY_SHIFTRIGHT: return false;
7883 case EXPR_BINARY_ASSIGN: return true;
7884 case EXPR_BINARY_MUL_ASSIGN: return true;
7885 case EXPR_BINARY_DIV_ASSIGN: return true;
7886 case EXPR_BINARY_MOD_ASSIGN: return true;
7887 case EXPR_BINARY_ADD_ASSIGN: return true;
7888 case EXPR_BINARY_SUB_ASSIGN: return true;
7889 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7890 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7891 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7892 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7893 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7895 /* Only examine the right hand side of && and ||, because the left hand
7896 * side already has the effect of controlling the execution of the right
7898 case EXPR_BINARY_LOGICAL_AND:
7899 case EXPR_BINARY_LOGICAL_OR:
7900 /* Only examine the right hand side of a comma expression, because the left
7901 * hand side has a separate warning */
7902 case EXPR_BINARY_COMMA:
7903 return expression_has_effect(expr->binary.right);
7905 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7906 case EXPR_BINARY_ISGREATER: return false;
7907 case EXPR_BINARY_ISGREATEREQUAL: return false;
7908 case EXPR_BINARY_ISLESS: return false;
7909 case EXPR_BINARY_ISLESSEQUAL: return false;
7910 case EXPR_BINARY_ISLESSGREATER: return false;
7911 case EXPR_BINARY_ISUNORDERED: return false;
7914 internal_errorf(HERE, "unexpected expression");
7917 static void semantic_comma(binary_expression_t *expression)
7919 if (warning.unused_value) {
7920 const expression_t *const left = expression->left;
7921 if (!expression_has_effect(left)) {
7922 warningf(&left->base.source_position,
7923 "left-hand operand of comma expression has no effect");
7926 expression->base.type = expression->right->base.type;
7929 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7930 static expression_t *parse_##binexpression_type(unsigned precedence, \
7931 expression_t *left) \
7933 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7934 binexpr->base.source_position = *HERE; \
7935 binexpr->binary.left = left; \
7938 expression_t *right = parse_sub_expression(precedence + lr); \
7940 binexpr->binary.right = right; \
7941 sfunc(&binexpr->binary); \
7946 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7947 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7948 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_divmod_arithmetic, 1)
7949 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_divmod_arithmetic, 1)
7950 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7951 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7952 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7953 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7954 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7956 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7957 semantic_comparison, 1)
7958 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7959 semantic_comparison, 1)
7960 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7961 semantic_comparison, 1)
7962 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7963 semantic_comparison, 1)
7965 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7966 semantic_binexpr_arithmetic, 1)
7967 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7968 semantic_binexpr_arithmetic, 1)
7969 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7970 semantic_binexpr_arithmetic, 1)
7971 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7972 semantic_logical_op, 1)
7973 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7974 semantic_logical_op, 1)
7975 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7976 semantic_shift_op, 1)
7977 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7978 semantic_shift_op, 1)
7979 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7980 semantic_arithmetic_addsubb_assign, 0)
7981 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7982 semantic_arithmetic_addsubb_assign, 0)
7983 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7984 semantic_arithmetic_assign, 0)
7985 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7986 semantic_divmod_assign, 0)
7987 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7988 semantic_divmod_assign, 0)
7989 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7990 semantic_arithmetic_assign, 0)
7991 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7992 semantic_arithmetic_assign, 0)
7993 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7994 semantic_arithmetic_assign, 0)
7995 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7996 semantic_arithmetic_assign, 0)
7997 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7998 semantic_arithmetic_assign, 0)
8000 static expression_t *parse_sub_expression(unsigned precedence)
8002 if (token.type < 0) {
8003 return expected_expression_error();
8006 expression_parser_function_t *parser
8007 = &expression_parsers[token.type];
8008 source_position_t source_position = token.source_position;
8011 if (parser->parser != NULL) {
8012 left = parser->parser(parser->precedence);
8014 left = parse_primary_expression();
8016 assert(left != NULL);
8017 left->base.source_position = source_position;
8020 if (token.type < 0) {
8021 return expected_expression_error();
8024 parser = &expression_parsers[token.type];
8025 if (parser->infix_parser == NULL)
8027 if (parser->infix_precedence < precedence)
8030 left = parser->infix_parser(parser->infix_precedence, left);
8032 assert(left != NULL);
8033 assert(left->kind != EXPR_UNKNOWN);
8034 left->base.source_position = source_position;
8041 * Parse an expression.
8043 static expression_t *parse_expression(void)
8045 return parse_sub_expression(1);
8049 * Register a parser for a prefix-like operator with given precedence.
8051 * @param parser the parser function
8052 * @param token_type the token type of the prefix token
8053 * @param precedence the precedence of the operator
8055 static void register_expression_parser(parse_expression_function parser,
8056 int token_type, unsigned precedence)
8058 expression_parser_function_t *entry = &expression_parsers[token_type];
8060 if (entry->parser != NULL) {
8061 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8062 panic("trying to register multiple expression parsers for a token");
8064 entry->parser = parser;
8065 entry->precedence = precedence;
8069 * Register a parser for an infix operator with given precedence.
8071 * @param parser the parser function
8072 * @param token_type the token type of the infix operator
8073 * @param precedence the precedence of the operator
8075 static void register_infix_parser(parse_expression_infix_function parser,
8076 int token_type, unsigned precedence)
8078 expression_parser_function_t *entry = &expression_parsers[token_type];
8080 if (entry->infix_parser != NULL) {
8081 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8082 panic("trying to register multiple infix expression parsers for a "
8085 entry->infix_parser = parser;
8086 entry->infix_precedence = precedence;
8090 * Initialize the expression parsers.
8092 static void init_expression_parsers(void)
8094 memset(&expression_parsers, 0, sizeof(expression_parsers));
8096 register_infix_parser(parse_array_expression, '[', 30);
8097 register_infix_parser(parse_call_expression, '(', 30);
8098 register_infix_parser(parse_select_expression, '.', 30);
8099 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
8100 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
8102 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
8105 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
8106 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
8107 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
8108 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
8109 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
8110 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
8111 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
8112 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
8113 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
8114 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
8115 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
8116 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
8117 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
8118 T_EXCLAMATIONMARKEQUAL, 13);
8119 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
8120 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
8121 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
8122 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
8123 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
8124 register_infix_parser(parse_conditional_expression, '?', 7);
8125 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
8126 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
8127 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
8128 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
8129 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
8130 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
8131 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
8132 T_LESSLESSEQUAL, 2);
8133 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
8134 T_GREATERGREATEREQUAL, 2);
8135 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
8137 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
8139 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
8142 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
8144 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
8145 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
8146 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
8147 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
8148 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
8149 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
8150 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
8152 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
8154 register_expression_parser(parse_sizeof, T_sizeof, 25);
8155 register_expression_parser(parse_alignof, T___alignof__, 25);
8156 register_expression_parser(parse_extension, T___extension__, 25);
8157 register_expression_parser(parse_builtin_classify_type,
8158 T___builtin_classify_type, 25);
8162 * Parse a asm statement arguments specification.
8164 static asm_argument_t *parse_asm_arguments(bool is_out)
8166 asm_argument_t *result = NULL;
8167 asm_argument_t *last = NULL;
8169 while (token.type == T_STRING_LITERAL || token.type == '[') {
8170 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8171 memset(argument, 0, sizeof(argument[0]));
8173 if (token.type == '[') {
8175 if (token.type != T_IDENTIFIER) {
8176 parse_error_expected("while parsing asm argument",
8177 T_IDENTIFIER, NULL);
8180 argument->symbol = token.v.symbol;
8185 argument->constraints = parse_string_literals();
8187 add_anchor_token(')');
8188 expression_t *expression = parse_expression();
8189 rem_anchor_token(')');
8191 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8192 * change size or type representation (e.g. int -> long is ok, but
8193 * int -> float is not) */
8194 if (expression->kind == EXPR_UNARY_CAST) {
8195 type_t *const type = expression->base.type;
8196 type_kind_t const kind = type->kind;
8197 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8200 if (kind == TYPE_ATOMIC) {
8201 atomic_type_kind_t const akind = type->atomic.akind;
8202 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8203 size = get_atomic_type_size(akind);
8205 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8206 size = get_atomic_type_size(get_intptr_kind());
8210 expression_t *const value = expression->unary.value;
8211 type_t *const value_type = value->base.type;
8212 type_kind_t const value_kind = value_type->kind;
8214 unsigned value_flags;
8215 unsigned value_size;
8216 if (value_kind == TYPE_ATOMIC) {
8217 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8218 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8219 value_size = get_atomic_type_size(value_akind);
8220 } else if (value_kind == TYPE_POINTER) {
8221 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8222 value_size = get_atomic_type_size(get_intptr_kind());
8227 if (value_flags != flags || value_size != size)
8231 } while (expression->kind == EXPR_UNARY_CAST);
8235 if (!is_lvalue(expression)) {
8236 errorf(&expression->base.source_position,
8237 "asm output argument is not an lvalue");
8240 argument->expression = expression;
8243 set_address_taken(expression, true);
8246 last->next = argument;
8252 if (token.type != ',')
8263 * Parse a asm statement clobber specification.
8265 static asm_clobber_t *parse_asm_clobbers(void)
8267 asm_clobber_t *result = NULL;
8268 asm_clobber_t *last = NULL;
8270 while(token.type == T_STRING_LITERAL) {
8271 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8272 clobber->clobber = parse_string_literals();
8275 last->next = clobber;
8281 if (token.type != ',')
8290 * Parse an asm statement.
8292 static statement_t *parse_asm_statement(void)
8296 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8297 statement->base.source_position = token.source_position;
8299 asm_statement_t *asm_statement = &statement->asms;
8301 if (token.type == T_volatile) {
8303 asm_statement->is_volatile = true;
8307 add_anchor_token(')');
8308 add_anchor_token(':');
8309 asm_statement->asm_text = parse_string_literals();
8311 if (token.type != ':') {
8312 rem_anchor_token(':');
8317 asm_statement->outputs = parse_asm_arguments(true);
8318 if (token.type != ':') {
8319 rem_anchor_token(':');
8324 asm_statement->inputs = parse_asm_arguments(false);
8325 if (token.type != ':') {
8326 rem_anchor_token(':');
8329 rem_anchor_token(':');
8332 asm_statement->clobbers = parse_asm_clobbers();
8335 rem_anchor_token(')');
8339 if (asm_statement->outputs == NULL) {
8340 /* GCC: An 'asm' instruction without any output operands will be treated
8341 * identically to a volatile 'asm' instruction. */
8342 asm_statement->is_volatile = true;
8347 return create_invalid_statement();
8351 * Parse a case statement.
8353 static statement_t *parse_case_statement(void)
8357 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8358 source_position_t *const pos = &statement->base.source_position;
8360 *pos = token.source_position;
8361 statement->case_label.expression = parse_expression();
8362 if (! is_constant_expression(statement->case_label.expression)) {
8363 errorf(pos, "case label does not reduce to an integer constant");
8364 statement->case_label.is_bad = true;
8366 long const val = fold_constant(statement->case_label.expression);
8367 statement->case_label.first_case = val;
8368 statement->case_label.last_case = val;
8371 if (c_mode & _GNUC) {
8372 if (token.type == T_DOTDOTDOT) {
8374 statement->case_label.end_range = parse_expression();
8375 if (! is_constant_expression(statement->case_label.end_range)) {
8376 errorf(pos, "case range does not reduce to an integer constant");
8377 statement->case_label.is_bad = true;
8379 long const val = fold_constant(statement->case_label.end_range);
8380 statement->case_label.last_case = val;
8382 if (val < statement->case_label.first_case) {
8383 statement->case_label.is_empty = true;
8384 warningf(pos, "empty range specified");
8390 PUSH_PARENT(statement);
8394 if (current_switch != NULL) {
8395 if (! statement->case_label.is_bad) {
8396 /* Check for duplicate case values */
8397 case_label_statement_t *c = &statement->case_label;
8398 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
8399 if (l->is_bad || l->is_empty || l->expression == NULL)
8402 if (c->last_case < l->first_case || c->first_case > l->last_case)
8405 errorf(pos, "duplicate case value (previously used %P)",
8406 &l->base.source_position);
8410 /* link all cases into the switch statement */
8411 if (current_switch->last_case == NULL) {
8412 current_switch->first_case = &statement->case_label;
8414 current_switch->last_case->next = &statement->case_label;
8416 current_switch->last_case = &statement->case_label;
8418 errorf(pos, "case label not within a switch statement");
8421 statement_t *const inner_stmt = parse_statement();
8422 statement->case_label.statement = inner_stmt;
8423 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8424 errorf(&inner_stmt->base.source_position, "declaration after case label");
8431 return create_invalid_statement();
8435 * Parse a default statement.
8437 static statement_t *parse_default_statement(void)
8441 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8442 statement->base.source_position = token.source_position;
8444 PUSH_PARENT(statement);
8447 if (current_switch != NULL) {
8448 const case_label_statement_t *def_label = current_switch->default_label;
8449 if (def_label != NULL) {
8450 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
8451 &def_label->base.source_position);
8453 current_switch->default_label = &statement->case_label;
8455 /* link all cases into the switch statement */
8456 if (current_switch->last_case == NULL) {
8457 current_switch->first_case = &statement->case_label;
8459 current_switch->last_case->next = &statement->case_label;
8461 current_switch->last_case = &statement->case_label;
8464 errorf(&statement->base.source_position,
8465 "'default' label not within a switch statement");
8468 statement_t *const inner_stmt = parse_statement();
8469 statement->case_label.statement = inner_stmt;
8470 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8471 errorf(&inner_stmt->base.source_position, "declaration after default label");
8478 return create_invalid_statement();
8482 * Return the declaration for a given label symbol or create a new one.
8484 * @param symbol the symbol of the label
8486 static declaration_t *get_label(symbol_t *symbol)
8488 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
8489 assert(current_function != NULL);
8490 /* if we found a label in the same function, then we already created the
8492 if (candidate != NULL
8493 && candidate->parent_scope == ¤t_function->scope) {
8497 /* otherwise we need to create a new one */
8498 declaration_t *const declaration = allocate_declaration_zero();
8499 declaration->namespc = NAMESPACE_LABEL;
8500 declaration->symbol = symbol;
8502 label_push(declaration);
8508 * Parse a label statement.
8510 static statement_t *parse_label_statement(void)
8512 assert(token.type == T_IDENTIFIER);
8513 symbol_t *symbol = token.v.symbol;
8516 declaration_t *label = get_label(symbol);
8518 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
8519 statement->base.source_position = token.source_position;
8520 statement->label.label = label;
8522 PUSH_PARENT(statement);
8524 /* if source position is already set then the label is defined twice,
8525 * otherwise it was just mentioned in a goto so far */
8526 if (label->source_position.input_name != NULL) {
8527 errorf(HERE, "duplicate label '%Y' (declared %P)",
8528 symbol, &label->source_position);
8530 label->source_position = token.source_position;
8531 label->init.statement = statement;
8536 if (token.type == '}') {
8537 /* TODO only warn? */
8539 warningf(HERE, "label at end of compound statement");
8540 statement->label.statement = create_empty_statement();
8542 errorf(HERE, "label at end of compound statement");
8543 statement->label.statement = create_invalid_statement();
8545 } else if (token.type == ';') {
8546 /* Eat an empty statement here, to avoid the warning about an empty
8547 * statement after a label. label:; is commonly used to have a label
8548 * before a closing brace. */
8549 statement->label.statement = create_empty_statement();
8552 statement_t *const inner_stmt = parse_statement();
8553 statement->label.statement = inner_stmt;
8554 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8555 errorf(&inner_stmt->base.source_position, "declaration after label");
8559 /* remember the labels in a list for later checking */
8560 if (label_last == NULL) {
8561 label_first = &statement->label;
8563 label_last->next = &statement->label;
8565 label_last = &statement->label;
8572 * Parse an if statement.
8574 static statement_t *parse_if(void)
8578 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
8579 statement->base.source_position = token.source_position;
8581 PUSH_PARENT(statement);
8584 add_anchor_token(')');
8585 statement->ifs.condition = parse_expression();
8586 rem_anchor_token(')');
8589 add_anchor_token(T_else);
8590 statement->ifs.true_statement = parse_statement();
8591 rem_anchor_token(T_else);
8593 if (token.type == T_else) {
8595 statement->ifs.false_statement = parse_statement();
8602 return create_invalid_statement();
8606 * Check that all enums are handled in a switch.
8608 * @param statement the switch statement to check
8610 static void check_enum_cases(const switch_statement_t *statement) {
8611 const type_t *type = skip_typeref(statement->expression->base.type);
8612 if (! is_type_enum(type))
8614 const enum_type_t *enumt = &type->enumt;
8616 /* if we have a default, no warnings */
8617 if (statement->default_label != NULL)
8620 /* FIXME: calculation of value should be done while parsing */
8621 const declaration_t *declaration;
8622 long last_value = -1;
8623 for (declaration = enumt->declaration->next;
8624 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
8625 declaration = declaration->next) {
8626 const expression_t *expression = declaration->init.enum_value;
8627 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
8629 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
8630 if (l->expression == NULL)
8632 if (l->first_case <= value && value <= l->last_case) {
8638 warningf(&statement->base.source_position,
8639 "enumeration value '%Y' not handled in switch", declaration->symbol);
8646 * Parse a switch statement.
8648 static statement_t *parse_switch(void)
8652 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
8653 statement->base.source_position = token.source_position;
8655 PUSH_PARENT(statement);
8658 add_anchor_token(')');
8659 expression_t *const expr = parse_expression();
8660 type_t * type = skip_typeref(expr->base.type);
8661 if (is_type_integer(type)) {
8662 type = promote_integer(type);
8663 } else if (is_type_valid(type)) {
8664 errorf(&expr->base.source_position,
8665 "switch quantity is not an integer, but '%T'", type);
8666 type = type_error_type;
8668 statement->switchs.expression = create_implicit_cast(expr, type);
8670 rem_anchor_token(')');
8672 switch_statement_t *rem = current_switch;
8673 current_switch = &statement->switchs;
8674 statement->switchs.body = parse_statement();
8675 current_switch = rem;
8677 if (warning.switch_default &&
8678 statement->switchs.default_label == NULL) {
8679 warningf(&statement->base.source_position, "switch has no default case");
8681 if (warning.switch_enum)
8682 check_enum_cases(&statement->switchs);
8688 return create_invalid_statement();
8691 static statement_t *parse_loop_body(statement_t *const loop)
8693 statement_t *const rem = current_loop;
8694 current_loop = loop;
8696 statement_t *const body = parse_statement();
8703 * Parse a while statement.
8705 static statement_t *parse_while(void)
8709 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
8710 statement->base.source_position = token.source_position;
8712 PUSH_PARENT(statement);
8715 add_anchor_token(')');
8716 statement->whiles.condition = parse_expression();
8717 rem_anchor_token(')');
8720 statement->whiles.body = parse_loop_body(statement);
8726 return create_invalid_statement();
8730 * Parse a do statement.
8732 static statement_t *parse_do(void)
8736 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
8737 statement->base.source_position = token.source_position;
8739 PUSH_PARENT(statement)
8741 add_anchor_token(T_while);
8742 statement->do_while.body = parse_loop_body(statement);
8743 rem_anchor_token(T_while);
8747 add_anchor_token(')');
8748 statement->do_while.condition = parse_expression();
8749 rem_anchor_token(')');
8757 return create_invalid_statement();
8761 * Parse a for statement.
8763 static statement_t *parse_for(void)
8767 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
8768 statement->base.source_position = token.source_position;
8770 PUSH_PARENT(statement);
8772 int top = environment_top();
8773 scope_t *last_scope = scope;
8774 set_scope(&statement->fors.scope);
8777 add_anchor_token(')');
8779 if (token.type != ';') {
8780 if (is_declaration_specifier(&token, false)) {
8781 parse_declaration(record_declaration);
8783 add_anchor_token(';');
8784 expression_t *const init = parse_expression();
8785 statement->fors.initialisation = init;
8786 if (warning.unused_value && !expression_has_effect(init)) {
8787 warningf(&init->base.source_position,
8788 "initialisation of 'for'-statement has no effect");
8790 rem_anchor_token(';');
8797 if (token.type != ';') {
8798 add_anchor_token(';');
8799 statement->fors.condition = parse_expression();
8800 rem_anchor_token(';');
8803 if (token.type != ')') {
8804 expression_t *const step = parse_expression();
8805 statement->fors.step = step;
8806 if (warning.unused_value && !expression_has_effect(step)) {
8807 warningf(&step->base.source_position,
8808 "step of 'for'-statement has no effect");
8811 rem_anchor_token(')');
8813 statement->fors.body = parse_loop_body(statement);
8815 assert(scope == &statement->fors.scope);
8816 set_scope(last_scope);
8817 environment_pop_to(top);
8824 rem_anchor_token(')');
8825 assert(scope == &statement->fors.scope);
8826 set_scope(last_scope);
8827 environment_pop_to(top);
8829 return create_invalid_statement();
8833 * Parse a goto statement.
8835 static statement_t *parse_goto(void)
8839 if (token.type != T_IDENTIFIER) {
8840 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8844 symbol_t *symbol = token.v.symbol;
8847 declaration_t *label = get_label(symbol);
8849 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8850 statement->base.source_position = token.source_position;
8852 statement->gotos.label = label;
8854 /* remember the goto's in a list for later checking */
8855 if (goto_last == NULL) {
8856 goto_first = &statement->gotos;
8858 goto_last->next = &statement->gotos;
8860 goto_last = &statement->gotos;
8866 return create_invalid_statement();
8870 * Parse a continue statement.
8872 static statement_t *parse_continue(void)
8874 if (current_loop == NULL) {
8875 errorf(HERE, "continue statement not within loop");
8878 statement_t *statement = allocate_statement_zero(STATEMENT_CONTINUE);
8879 statement->base.source_position = token.source_position;
8889 * Parse a break statement.
8891 static statement_t *parse_break(void)
8893 if (current_switch == NULL && current_loop == NULL) {
8894 errorf(HERE, "break statement not within loop or switch");
8897 statement_t *statement = allocate_statement_zero(STATEMENT_BREAK);
8898 statement->base.source_position = token.source_position;
8908 * Parse a __leave statement.
8910 static statement_t *parse_leave(void)
8912 if (current_try == NULL) {
8913 errorf(HERE, "__leave statement not within __try");
8916 statement_t *statement = allocate_statement_zero(STATEMENT_LEAVE);
8917 statement->base.source_position = token.source_position;
8927 * Check if a given declaration represents a local variable.
8929 static bool is_local_var_declaration(const declaration_t *declaration)
8931 switch ((storage_class_tag_t) declaration->storage_class) {
8932 case STORAGE_CLASS_AUTO:
8933 case STORAGE_CLASS_REGISTER: {
8934 const type_t *type = skip_typeref(declaration->type);
8935 if (is_type_function(type)) {
8947 * Check if a given declaration represents a variable.
8949 static bool is_var_declaration(const declaration_t *declaration)
8951 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8954 const type_t *type = skip_typeref(declaration->type);
8955 return !is_type_function(type);
8959 * Check if a given expression represents a local variable.
8961 static bool is_local_variable(const expression_t *expression)
8963 if (expression->base.kind != EXPR_REFERENCE) {
8966 const declaration_t *declaration = expression->reference.declaration;
8967 return is_local_var_declaration(declaration);
8971 * Check if a given expression represents a local variable and
8972 * return its declaration then, else return NULL.
8974 declaration_t *expr_is_variable(const expression_t *expression)
8976 if (expression->base.kind != EXPR_REFERENCE) {
8979 declaration_t *declaration = expression->reference.declaration;
8980 if (is_var_declaration(declaration))
8986 * Parse a return statement.
8988 static statement_t *parse_return(void)
8990 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8991 statement->base.source_position = token.source_position;
8995 expression_t *return_value = NULL;
8996 if (token.type != ';') {
8997 return_value = parse_expression();
9000 const type_t *const func_type = current_function->type;
9001 assert(is_type_function(func_type));
9002 type_t *const return_type = skip_typeref(func_type->function.return_type);
9004 if (return_value != NULL) {
9005 type_t *return_value_type = skip_typeref(return_value->base.type);
9007 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
9008 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
9009 warningf(&statement->base.source_position,
9010 "'return' with a value, in function returning void");
9011 return_value = NULL;
9013 assign_error_t error = semantic_assign(return_type, return_value);
9014 report_assign_error(error, return_type, return_value, "'return'",
9015 &statement->base.source_position);
9016 return_value = create_implicit_cast(return_value, return_type);
9018 /* check for returning address of a local var */
9019 if (return_value != NULL &&
9020 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9021 const expression_t *expression = return_value->unary.value;
9022 if (is_local_variable(expression)) {
9023 warningf(&statement->base.source_position,
9024 "function returns address of local variable");
9028 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9029 warningf(&statement->base.source_position,
9030 "'return' without value, in function returning non-void");
9033 statement->returns.value = return_value;
9042 * Parse a declaration statement.
9044 static statement_t *parse_declaration_statement(void)
9046 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9048 statement->base.source_position = token.source_position;
9050 declaration_t *before = last_declaration;
9051 parse_declaration(record_declaration);
9053 if (before == NULL) {
9054 statement->declaration.declarations_begin = scope->declarations;
9056 statement->declaration.declarations_begin = before->next;
9058 statement->declaration.declarations_end = last_declaration;
9064 * Parse an expression statement, ie. expr ';'.
9066 static statement_t *parse_expression_statement(void)
9068 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9070 statement->base.source_position = token.source_position;
9071 expression_t *const expr = parse_expression();
9072 statement->expression.expression = expr;
9081 * Parse a microsoft __try { } __finally { } or
9082 * __try{ } __except() { }
9084 static statement_t *parse_ms_try_statment(void)
9086 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9087 statement->base.source_position = token.source_position;
9090 PUSH_PARENT(statement);
9092 ms_try_statement_t *rem = current_try;
9093 current_try = &statement->ms_try;
9094 statement->ms_try.try_statement = parse_compound_statement(false);
9099 if (token.type == T___except) {
9102 add_anchor_token(')');
9103 expression_t *const expr = parse_expression();
9104 type_t * type = skip_typeref(expr->base.type);
9105 if (is_type_integer(type)) {
9106 type = promote_integer(type);
9107 } else if (is_type_valid(type)) {
9108 errorf(&expr->base.source_position,
9109 "__expect expression is not an integer, but '%T'", type);
9110 type = type_error_type;
9112 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9113 rem_anchor_token(')');
9115 statement->ms_try.final_statement = parse_compound_statement(false);
9116 } else if (token.type == T__finally) {
9118 statement->ms_try.final_statement = parse_compound_statement(false);
9120 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9121 return create_invalid_statement();
9125 return create_invalid_statement();
9128 static statement_t *parse_empty_statement(void)
9130 if (warning.empty_statement) {
9131 warningf(HERE, "statement is empty");
9133 statement_t *const statement = create_empty_statement();
9139 * Parse a statement.
9140 * There's also parse_statement() which additionally checks for
9141 * "statement has no effect" warnings
9143 static statement_t *intern_parse_statement(void)
9145 statement_t *statement = NULL;
9147 /* declaration or statement */
9148 add_anchor_token(';');
9149 switch (token.type) {
9150 case T_IDENTIFIER: {
9151 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9152 if (la1_type == ':') {
9153 statement = parse_label_statement();
9154 } else if (is_typedef_symbol(token.v.symbol)) {
9155 statement = parse_declaration_statement();
9156 } else switch (la1_type) {
9160 statement = parse_declaration_statement();
9164 statement = parse_expression_statement();
9170 case T___extension__:
9171 /* This can be a prefix to a declaration or an expression statement.
9172 * We simply eat it now and parse the rest with tail recursion. */
9175 } while (token.type == T___extension__);
9176 statement = parse_statement();
9180 statement = parse_declaration_statement();
9183 case ';': statement = parse_empty_statement(); break;
9184 case '{': statement = parse_compound_statement(false); break;
9185 case T___leave: statement = parse_leave(); break;
9186 case T___try: statement = parse_ms_try_statment(); break;
9187 case T_asm: statement = parse_asm_statement(); break;
9188 case T_break: statement = parse_break(); break;
9189 case T_case: statement = parse_case_statement(); break;
9190 case T_continue: statement = parse_continue(); break;
9191 case T_default: statement = parse_default_statement(); break;
9192 case T_do: statement = parse_do(); break;
9193 case T_for: statement = parse_for(); break;
9194 case T_goto: statement = parse_goto(); break;
9195 case T_if: statement = parse_if (); break;
9196 case T_return: statement = parse_return(); break;
9197 case T_switch: statement = parse_switch(); break;
9198 case T_while: statement = parse_while(); break;
9199 default: statement = parse_expression_statement(); break;
9201 rem_anchor_token(';');
9203 assert(statement != NULL
9204 && statement->base.source_position.input_name != NULL);
9210 * parse a statement and emits "statement has no effect" warning if needed
9211 * (This is really a wrapper around intern_parse_statement with check for 1
9212 * single warning. It is needed, because for statement expressions we have
9213 * to avoid the warning on the last statement)
9215 static statement_t *parse_statement(void)
9217 statement_t *statement = intern_parse_statement();
9219 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9220 expression_t *expression = statement->expression.expression;
9221 if (!expression_has_effect(expression)) {
9222 warningf(&expression->base.source_position,
9223 "statement has no effect");
9231 * Parse a compound statement.
9233 static statement_t *parse_compound_statement(bool inside_expression_statement)
9235 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
9236 statement->base.source_position = token.source_position;
9238 PUSH_PARENT(statement);
9241 add_anchor_token('}');
9243 int top = environment_top();
9244 scope_t *last_scope = scope;
9245 set_scope(&statement->compound.scope);
9247 statement_t **anchor = &statement->compound.statements;
9248 bool only_decls_so_far = true;
9249 while (token.type != '}' && token.type != T_EOF) {
9250 statement_t *sub_statement = intern_parse_statement();
9251 if (is_invalid_statement(sub_statement)) {
9252 /* an error occurred. if we are at an anchor, return */
9258 if (warning.declaration_after_statement) {
9259 if (sub_statement->kind != STATEMENT_DECLARATION) {
9260 only_decls_so_far = false;
9261 } else if (!only_decls_so_far) {
9262 warningf(&sub_statement->base.source_position,
9263 "ISO C90 forbids mixed declarations and code");
9267 *anchor = sub_statement;
9269 while (sub_statement->base.next != NULL)
9270 sub_statement = sub_statement->base.next;
9272 anchor = &sub_statement->base.next;
9275 if (token.type == '}') {
9278 errorf(&statement->base.source_position,
9279 "end of file while looking for closing '}'");
9282 /* look over all statements again to produce no effect warnings */
9283 if (warning.unused_value) {
9284 statement_t *sub_statement = statement->compound.statements;
9285 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
9286 if (sub_statement->kind != STATEMENT_EXPRESSION)
9288 /* don't emit a warning for the last expression in an expression
9289 * statement as it has always an effect */
9290 if (inside_expression_statement && sub_statement->base.next == NULL)
9293 expression_t *expression = sub_statement->expression.expression;
9294 if (!expression_has_effect(expression)) {
9295 warningf(&expression->base.source_position,
9296 "statement has no effect");
9302 rem_anchor_token('}');
9303 assert(scope == &statement->compound.scope);
9304 set_scope(last_scope);
9305 environment_pop_to(top);
9312 * Initialize builtin types.
9314 static void initialize_builtin_types(void)
9316 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
9317 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
9318 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
9319 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
9320 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
9321 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
9322 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
9323 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
9325 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
9326 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
9327 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
9328 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
9330 /* const version of wchar_t */
9331 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
9332 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
9333 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
9335 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
9339 * Check for unused global static functions and variables
9341 static void check_unused_globals(void)
9343 if (!warning.unused_function && !warning.unused_variable)
9346 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
9348 decl->modifiers & DM_UNUSED ||
9349 decl->modifiers & DM_USED ||
9350 decl->storage_class != STORAGE_CLASS_STATIC)
9353 type_t *const type = decl->type;
9355 if (is_type_function(skip_typeref(type))) {
9356 if (!warning.unused_function || decl->is_inline)
9359 s = (decl->init.statement != NULL ? "defined" : "declared");
9361 if (!warning.unused_variable)
9367 warningf(&decl->source_position, "'%#T' %s but not used",
9368 type, decl->symbol, s);
9372 static void parse_global_asm(void)
9377 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
9378 statement->base.source_position = token.source_position;
9379 statement->asms.asm_text = parse_string_literals();
9380 statement->base.next = unit->global_asm;
9381 unit->global_asm = statement;
9390 * Parse a translation unit.
9392 static void parse_translation_unit(void)
9394 for (;;) switch (token.type) {
9397 case T___extension__:
9398 parse_external_declaration();
9409 /* TODO error in strict mode */
9410 warningf(HERE, "stray ';' outside of function");
9415 errorf(HERE, "stray %K outside of function", &token);
9416 if (token.type == '(' || token.type == '{' || token.type == '[')
9417 eat_until_matching_token(token.type);
9426 * @return the translation unit or NULL if errors occurred.
9428 void start_parsing(void)
9430 environment_stack = NEW_ARR_F(stack_entry_t, 0);
9431 label_stack = NEW_ARR_F(stack_entry_t, 0);
9432 diagnostic_count = 0;
9436 type_set_output(stderr);
9437 ast_set_output(stderr);
9439 assert(unit == NULL);
9440 unit = allocate_ast_zero(sizeof(unit[0]));
9442 assert(global_scope == NULL);
9443 global_scope = &unit->scope;
9445 assert(scope == NULL);
9446 set_scope(&unit->scope);
9448 initialize_builtin_types();
9451 translation_unit_t *finish_parsing(void)
9453 assert(scope == &unit->scope);
9455 last_declaration = NULL;
9457 assert(global_scope == &unit->scope);
9458 check_unused_globals();
9459 global_scope = NULL;
9461 DEL_ARR_F(environment_stack);
9462 DEL_ARR_F(label_stack);
9464 translation_unit_t *result = unit;
9471 lookahead_bufpos = 0;
9472 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
9475 parse_translation_unit();
9479 * Initialize the parser.
9481 void init_parser(void)
9484 /* add predefined symbols for extended-decl-modifier */
9485 sym_align = symbol_table_insert("align");
9486 sym_allocate = symbol_table_insert("allocate");
9487 sym_dllimport = symbol_table_insert("dllimport");
9488 sym_dllexport = symbol_table_insert("dllexport");
9489 sym_naked = symbol_table_insert("naked");
9490 sym_noinline = symbol_table_insert("noinline");
9491 sym_noreturn = symbol_table_insert("noreturn");
9492 sym_nothrow = symbol_table_insert("nothrow");
9493 sym_novtable = symbol_table_insert("novtable");
9494 sym_property = symbol_table_insert("property");
9495 sym_get = symbol_table_insert("get");
9496 sym_put = symbol_table_insert("put");
9497 sym_selectany = symbol_table_insert("selectany");
9498 sym_thread = symbol_table_insert("thread");
9499 sym_uuid = symbol_table_insert("uuid");
9500 sym_deprecated = symbol_table_insert("deprecated");
9501 sym_restrict = symbol_table_insert("restrict");
9502 sym_noalias = symbol_table_insert("noalias");
9504 memset(token_anchor_set, 0, sizeof(token_anchor_set));
9506 init_expression_parsers();
9507 obstack_init(&temp_obst);
9509 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
9510 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
9514 * Terminate the parser.
9516 void exit_parser(void)
9518 obstack_free(&temp_obst, NULL);