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 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct argument_list_t argument_list_t;
52 struct argument_list_t {
54 argument_list_t *next;
57 typedef struct gnu_attribute_t gnu_attribute_t;
58 struct gnu_attribute_t {
59 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
60 gnu_attribute_t *next;
61 bool invalid; /**< Set if this attribute had argument errors, */
62 bool have_arguments; /**< True, if this attribute has arguments. */
66 atomic_type_kind_t akind;
67 long argument; /**< Single argument. */
68 argument_list_t *arguments; /**< List of argument expressions. */
72 typedef struct declaration_specifiers_t declaration_specifiers_t;
73 struct declaration_specifiers_t {
74 source_position_t source_position;
75 unsigned char declared_storage_class;
76 unsigned char alignment; /**< Alignment, 0 if not set. */
77 unsigned int is_inline : 1;
78 unsigned int deprecated : 1;
79 decl_modifiers_t modifiers; /**< declaration modifiers */
80 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
81 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
82 symbol_t *get_property_sym; /**< the name of the get property if set. */
83 symbol_t *put_property_sym; /**< the name of the put property if set. */
88 * An environment for parsing initializers (and compound literals).
90 typedef struct parse_initializer_env_t {
91 type_t *type; /**< the type of the initializer. In case of an
92 array type with unspecified size this gets
93 adjusted to the actual size. */
94 declaration_t *declaration; /**< the declaration that is initialized if any */
95 bool must_be_constant;
96 } parse_initializer_env_t;
98 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
100 static token_t token;
101 static token_t lookahead_buffer[MAX_LOOKAHEAD];
102 static int lookahead_bufpos;
103 static stack_entry_t *environment_stack = NULL;
104 static stack_entry_t *label_stack = NULL;
105 static scope_t *global_scope = NULL;
106 static scope_t *scope = NULL;
107 static declaration_t *last_declaration = NULL;
108 static declaration_t *current_function = NULL;
109 static switch_statement_t *current_switch = NULL;
110 static statement_t *current_loop = NULL;
111 static ms_try_statement_t *current_try = NULL;
112 static goto_statement_t *goto_first = NULL;
113 static goto_statement_t *goto_last = NULL;
114 static label_statement_t *label_first = NULL;
115 static label_statement_t *label_last = NULL;
116 static translation_unit_t *unit = NULL;
117 static struct obstack temp_obst;
119 static source_position_t null_position = { NULL, 0 };
121 /* symbols for Microsoft extended-decl-modifier */
122 static const symbol_t *sym_align = NULL;
123 static const symbol_t *sym_allocate = NULL;
124 static const symbol_t *sym_dllimport = NULL;
125 static const symbol_t *sym_dllexport = NULL;
126 static const symbol_t *sym_naked = NULL;
127 static const symbol_t *sym_noinline = NULL;
128 static const symbol_t *sym_noreturn = NULL;
129 static const symbol_t *sym_nothrow = NULL;
130 static const symbol_t *sym_novtable = NULL;
131 static const symbol_t *sym_property = NULL;
132 static const symbol_t *sym_get = NULL;
133 static const symbol_t *sym_put = NULL;
134 static const symbol_t *sym_selectany = NULL;
135 static const symbol_t *sym_thread = NULL;
136 static const symbol_t *sym_uuid = NULL;
137 static const symbol_t *sym_deprecated = NULL;
138 static const symbol_t *sym_restrict = NULL;
139 static const symbol_t *sym_noalias = NULL;
141 /** The token anchor set */
142 static unsigned char token_anchor_set[T_LAST_TOKEN];
144 /** The current source position. */
145 #define HERE (&token.source_position)
147 static type_t *type_valist;
149 static statement_t *parse_compound_statement(bool inside_expression_statement);
150 static statement_t *parse_statement(void);
152 static expression_t *parse_sub_expression(unsigned precedence);
153 static expression_t *parse_expression(void);
154 static type_t *parse_typename(void);
156 static void parse_compound_type_entries(declaration_t *compound_declaration);
157 static declaration_t *parse_declarator(
158 const declaration_specifiers_t *specifiers, bool may_be_abstract);
159 static declaration_t *record_declaration(declaration_t *declaration);
161 static void semantic_comparison(binary_expression_t *expression);
163 #define STORAGE_CLASSES \
170 #define TYPE_QUALIFIERS \
175 case T__forceinline: \
176 case T___attribute__:
178 #ifdef PROVIDE_COMPLEX
179 #define COMPLEX_SPECIFIERS \
181 #define IMAGINARY_SPECIFIERS \
184 #define COMPLEX_SPECIFIERS
185 #define IMAGINARY_SPECIFIERS
188 #define TYPE_SPECIFIERS \
203 case T___builtin_va_list: \
208 #define DECLARATION_START \
213 #define TYPENAME_START \
218 * Allocate an AST node with given size and
219 * initialize all fields with zero.
221 static void *allocate_ast_zero(size_t size)
223 void *res = allocate_ast(size);
224 memset(res, 0, size);
228 static declaration_t *allocate_declaration_zero(void)
230 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
231 declaration->type = type_error_type;
232 declaration->alignment = 0;
237 * Returns the size of a statement node.
239 * @param kind the statement kind
241 static size_t get_statement_struct_size(statement_kind_t kind)
243 static const size_t sizes[] = {
244 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
245 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
246 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
247 [STATEMENT_RETURN] = sizeof(return_statement_t),
248 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
249 [STATEMENT_IF] = sizeof(if_statement_t),
250 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
251 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
252 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
253 [STATEMENT_BREAK] = sizeof(statement_base_t),
254 [STATEMENT_GOTO] = sizeof(goto_statement_t),
255 [STATEMENT_LABEL] = sizeof(label_statement_t),
256 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
257 [STATEMENT_WHILE] = sizeof(while_statement_t),
258 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
259 [STATEMENT_FOR] = sizeof(for_statement_t),
260 [STATEMENT_ASM] = sizeof(asm_statement_t),
261 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
262 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
264 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
265 assert(sizes[kind] != 0);
270 * Returns the size of an expression node.
272 * @param kind the expression kind
274 static size_t get_expression_struct_size(expression_kind_t kind)
276 static const size_t sizes[] = {
277 [EXPR_INVALID] = sizeof(expression_base_t),
278 [EXPR_REFERENCE] = sizeof(reference_expression_t),
279 [EXPR_CONST] = sizeof(const_expression_t),
280 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
281 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
282 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
283 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
284 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
285 [EXPR_CALL] = sizeof(call_expression_t),
286 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
287 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
288 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
289 [EXPR_SELECT] = sizeof(select_expression_t),
290 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
291 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
292 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
293 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
294 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
295 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
296 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
297 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
298 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
299 [EXPR_VA_START] = sizeof(va_start_expression_t),
300 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
301 [EXPR_STATEMENT] = sizeof(statement_expression_t),
303 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
304 return sizes[EXPR_UNARY_FIRST];
306 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
307 return sizes[EXPR_BINARY_FIRST];
309 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
310 assert(sizes[kind] != 0);
315 * Allocate a statement node of given kind and initialize all
318 static statement_t *allocate_statement_zero(statement_kind_t kind)
320 size_t size = get_statement_struct_size(kind);
321 statement_t *res = allocate_ast_zero(size);
323 res->base.kind = kind;
328 * Allocate an expression node of given kind and initialize all
331 static expression_t *allocate_expression_zero(expression_kind_t kind)
333 size_t size = get_expression_struct_size(kind);
334 expression_t *res = allocate_ast_zero(size);
336 res->base.kind = kind;
337 res->base.type = type_error_type;
342 * Creates a new invalid expression.
344 static expression_t *create_invalid_expression(void)
346 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
347 expression->base.source_position = token.source_position;
352 * Creates a new invalid statement.
354 static statement_t *create_invalid_statement(void)
356 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
357 statement->base.source_position = token.source_position;
362 * Allocate a new empty statement.
364 static statement_t *create_empty_statement(void)
366 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
367 statement->base.source_position = token.source_position;
372 * Returns the size of a type node.
374 * @param kind the type kind
376 static size_t get_type_struct_size(type_kind_t kind)
378 static const size_t sizes[] = {
379 [TYPE_ATOMIC] = sizeof(atomic_type_t),
380 [TYPE_COMPLEX] = sizeof(complex_type_t),
381 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
382 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
383 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
384 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
385 [TYPE_ENUM] = sizeof(enum_type_t),
386 [TYPE_FUNCTION] = sizeof(function_type_t),
387 [TYPE_POINTER] = sizeof(pointer_type_t),
388 [TYPE_ARRAY] = sizeof(array_type_t),
389 [TYPE_BUILTIN] = sizeof(builtin_type_t),
390 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
391 [TYPE_TYPEOF] = sizeof(typeof_type_t),
393 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
394 assert(kind <= TYPE_TYPEOF);
395 assert(sizes[kind] != 0);
400 * Allocate a type node of given kind and initialize all
403 * @param kind type kind to allocate
404 * @param source_position the source position of the type definition
406 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
408 size_t size = get_type_struct_size(kind);
409 type_t *res = obstack_alloc(type_obst, size);
410 memset(res, 0, size);
412 res->base.kind = kind;
413 res->base.source_position = *source_position;
418 * Returns the size of an initializer node.
420 * @param kind the initializer kind
422 static size_t get_initializer_size(initializer_kind_t kind)
424 static const size_t sizes[] = {
425 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
426 [INITIALIZER_STRING] = sizeof(initializer_string_t),
427 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
428 [INITIALIZER_LIST] = sizeof(initializer_list_t),
429 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
431 assert(kind < sizeof(sizes) / sizeof(*sizes));
432 assert(sizes[kind] != 0);
437 * Allocate an initializer node of given kind and initialize all
440 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
442 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
449 * Free a type from the type obstack.
451 static void free_type(void *type)
453 obstack_free(type_obst, type);
457 * Returns the index of the top element of the environment stack.
459 static size_t environment_top(void)
461 return ARR_LEN(environment_stack);
465 * Returns the index of the top element of the label stack.
467 static size_t label_top(void)
469 return ARR_LEN(label_stack);
473 * Return the next token.
475 static inline void next_token(void)
477 token = lookahead_buffer[lookahead_bufpos];
478 lookahead_buffer[lookahead_bufpos] = lexer_token;
481 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
484 print_token(stderr, &token);
485 fprintf(stderr, "\n");
490 * Return the next token with a given lookahead.
492 static inline const token_t *look_ahead(int num)
494 assert(num > 0 && num <= MAX_LOOKAHEAD);
495 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
496 return &lookahead_buffer[pos];
500 * Adds a token to the token anchor set (a multi-set).
502 static void add_anchor_token(int token_type)
504 assert(0 <= token_type && token_type < T_LAST_TOKEN);
505 ++token_anchor_set[token_type];
508 static int save_and_reset_anchor_state(int token_type)
510 assert(0 <= token_type && token_type < T_LAST_TOKEN);
511 int count = token_anchor_set[token_type];
512 token_anchor_set[token_type] = 0;
516 static void restore_anchor_state(int token_type, int count)
518 assert(0 <= token_type && token_type < T_LAST_TOKEN);
519 token_anchor_set[token_type] = count;
523 * Remove a token from the token anchor set (a multi-set).
525 static void rem_anchor_token(int token_type)
527 assert(0 <= token_type && token_type < T_LAST_TOKEN);
528 --token_anchor_set[token_type];
531 static bool at_anchor(void)
535 return token_anchor_set[token.type];
539 * Eat tokens until a matching token is found.
541 static void eat_until_matching_token(int type)
543 unsigned parenthesis_count = 0;
544 unsigned brace_count = 0;
545 unsigned bracket_count = 0;
549 case '(': end_token = ')'; break;
550 case '{': end_token = '}'; break;
551 case '[': end_token = ']'; break;
552 default: end_token = type; break;
555 while (token.type != end_token ||
556 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
560 case '(': ++parenthesis_count; break;
561 case '{': ++brace_count; break;
562 case '[': ++bracket_count; break;
564 if (parenthesis_count > 0)
572 if (bracket_count > 0)
583 * Eat input tokens until an anchor is found.
585 static void eat_until_anchor(void)
587 if (token.type == T_EOF)
589 while (token_anchor_set[token.type] == 0) {
590 if (token.type == '(' || token.type == '{' || token.type == '[')
591 eat_until_matching_token(token.type);
592 if (token.type == T_EOF)
598 static void eat_block(void)
600 eat_until_matching_token('{');
601 if (token.type == '}')
606 * eat all token until a ';' is reached or a stop token is found.
608 static void eat_statement(void)
610 eat_until_matching_token(';');
611 if (token.type == ';')
615 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
618 * Report a parse error because an expected token was not found.
621 #if defined __GNUC__ && __GNUC__ >= 4
622 __attribute__((sentinel))
624 void parse_error_expected(const char *message, ...)
626 if (message != NULL) {
627 errorf(HERE, "%s", message);
630 va_start(ap, message);
631 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
636 * Report a type error.
638 static void type_error(const char *msg, const source_position_t *source_position,
641 errorf(source_position, "%s, but found type '%T'", msg, type);
645 * Report an incompatible type.
647 static void type_error_incompatible(const char *msg,
648 const source_position_t *source_position, type_t *type1, type_t *type2)
650 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
655 * Expect the the current token is the expected token.
656 * If not, generate an error, eat the current statement,
657 * and goto the end_error label.
659 #define expect(expected) \
661 if (UNLIKELY(token.type != (expected))) { \
662 parse_error_expected(NULL, (expected), NULL); \
663 add_anchor_token(expected); \
664 eat_until_anchor(); \
665 if (token.type == expected) \
667 rem_anchor_token(expected); \
673 static void set_scope(scope_t *new_scope)
676 scope->last_declaration = last_declaration;
680 last_declaration = new_scope->last_declaration;
684 * Search a symbol in a given namespace and returns its declaration or
685 * NULL if this symbol was not found.
687 static declaration_t *get_declaration(const symbol_t *const symbol,
688 const namespace_t namespc)
690 declaration_t *declaration = symbol->declaration;
691 for( ; declaration != NULL; declaration = declaration->symbol_next) {
692 if (declaration->namespc == namespc)
700 * pushs an environment_entry on the environment stack and links the
701 * corresponding symbol to the new entry
703 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
705 symbol_t *symbol = declaration->symbol;
706 namespace_t namespc = (namespace_t) declaration->namespc;
708 /* replace/add declaration into declaration list of the symbol */
709 declaration_t *iter = symbol->declaration;
711 symbol->declaration = declaration;
713 declaration_t *iter_last = NULL;
714 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
715 /* replace an entry? */
716 if (iter->namespc == namespc) {
717 if (iter_last == NULL) {
718 symbol->declaration = declaration;
720 iter_last->symbol_next = declaration;
722 declaration->symbol_next = iter->symbol_next;
727 assert(iter_last->symbol_next == NULL);
728 iter_last->symbol_next = declaration;
732 /* remember old declaration */
734 entry.symbol = symbol;
735 entry.old_declaration = iter;
736 entry.namespc = (unsigned short) namespc;
737 ARR_APP1(stack_entry_t, *stack_ptr, entry);
740 static void environment_push(declaration_t *declaration)
742 assert(declaration->source_position.input_name != NULL);
743 assert(declaration->parent_scope != NULL);
744 stack_push(&environment_stack, declaration);
748 * Push a declaration of the label stack.
750 * @param declaration the declaration
752 static void label_push(declaration_t *declaration)
754 declaration->parent_scope = ¤t_function->scope;
755 stack_push(&label_stack, declaration);
759 * pops symbols from the environment stack until @p new_top is the top element
761 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
763 stack_entry_t *stack = *stack_ptr;
764 size_t top = ARR_LEN(stack);
767 assert(new_top <= top);
771 for(i = top; i > new_top; --i) {
772 stack_entry_t *entry = &stack[i - 1];
774 declaration_t *old_declaration = entry->old_declaration;
775 symbol_t *symbol = entry->symbol;
776 namespace_t namespc = (namespace_t)entry->namespc;
778 /* replace/remove declaration */
779 declaration_t *declaration = symbol->declaration;
780 assert(declaration != NULL);
781 if (declaration->namespc == namespc) {
782 if (old_declaration == NULL) {
783 symbol->declaration = declaration->symbol_next;
785 symbol->declaration = old_declaration;
788 declaration_t *iter_last = declaration;
789 declaration_t *iter = declaration->symbol_next;
790 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
791 /* replace an entry? */
792 if (iter->namespc == namespc) {
793 assert(iter_last != NULL);
794 iter_last->symbol_next = old_declaration;
795 if (old_declaration != NULL) {
796 old_declaration->symbol_next = iter->symbol_next;
801 assert(iter != NULL);
805 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
808 static void environment_pop_to(size_t new_top)
810 stack_pop_to(&environment_stack, new_top);
814 * Pop all entries on the label stack until the new_top
817 * @param new_top the new stack top
819 static void label_pop_to(size_t new_top)
821 stack_pop_to(&label_stack, new_top);
825 static int get_rank(const type_t *type)
827 assert(!is_typeref(type));
828 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
829 * and esp. footnote 108). However we can't fold constants (yet), so we
830 * can't decide whether unsigned int is possible, while int always works.
831 * (unsigned int would be preferable when possible... for stuff like
832 * struct { enum { ... } bla : 4; } ) */
833 if (type->kind == TYPE_ENUM)
834 return ATOMIC_TYPE_INT;
836 assert(type->kind == TYPE_ATOMIC);
837 return type->atomic.akind;
840 static type_t *promote_integer(type_t *type)
842 if (type->kind == TYPE_BITFIELD)
843 type = type->bitfield.base_type;
845 if (get_rank(type) < ATOMIC_TYPE_INT)
852 * Create a cast expression.
854 * @param expression the expression to cast
855 * @param dest_type the destination type
857 static expression_t *create_cast_expression(expression_t *expression,
860 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
862 cast->unary.value = expression;
863 cast->base.type = dest_type;
869 * Check if a given expression represents the 0 pointer constant.
871 static bool is_null_pointer_constant(const expression_t *expression)
873 /* skip void* cast */
874 if (expression->kind == EXPR_UNARY_CAST
875 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
876 expression = expression->unary.value;
879 /* TODO: not correct yet, should be any constant integer expression
880 * which evaluates to 0 */
881 if (expression->kind != EXPR_CONST)
884 type_t *const type = skip_typeref(expression->base.type);
885 if (!is_type_integer(type))
888 return expression->conste.v.int_value == 0;
892 * Create an implicit cast expression.
894 * @param expression the expression to cast
895 * @param dest_type the destination type
897 static expression_t *create_implicit_cast(expression_t *expression,
900 type_t *const source_type = expression->base.type;
902 if (source_type == dest_type)
905 return create_cast_expression(expression, dest_type);
908 typedef enum assign_error_t {
910 ASSIGN_ERROR_INCOMPATIBLE,
911 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
912 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
913 ASSIGN_WARNING_POINTER_FROM_INT,
914 ASSIGN_WARNING_INT_FROM_POINTER
917 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
918 const expression_t *const right,
920 const source_position_t *source_position)
922 type_t *const orig_type_right = right->base.type;
923 type_t *const type_left = skip_typeref(orig_type_left);
924 type_t *const type_right = skip_typeref(orig_type_right);
929 case ASSIGN_ERROR_INCOMPATIBLE:
930 errorf(source_position,
931 "destination type '%T' in %s is incompatible with type '%T'",
932 orig_type_left, context, orig_type_right);
935 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
936 type_t *points_to_left
937 = skip_typeref(type_left->pointer.points_to);
938 type_t *points_to_right
939 = skip_typeref(type_right->pointer.points_to);
941 /* the left type has all qualifiers from the right type */
942 unsigned missing_qualifiers
943 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
944 errorf(source_position,
945 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type",
946 orig_type_left, context, orig_type_right, missing_qualifiers);
950 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
951 warningf(source_position,
952 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
953 orig_type_left, context, right, orig_type_right);
956 case ASSIGN_WARNING_POINTER_FROM_INT:
957 warningf(source_position,
958 "%s makes integer '%T' from pointer '%T' without a cast",
959 context, orig_type_left, orig_type_right);
962 case ASSIGN_WARNING_INT_FROM_POINTER:
963 warningf(source_position,
964 "%s makes integer '%T' from pointer '%T' without a cast",
965 context, orig_type_left, orig_type_right);
969 panic("invalid error value");
973 /** Implements the rules from § 6.5.16.1 */
974 static assign_error_t semantic_assign(type_t *orig_type_left,
975 const expression_t *const right)
977 type_t *const orig_type_right = right->base.type;
978 type_t *const type_left = skip_typeref(orig_type_left);
979 type_t *const type_right = skip_typeref(orig_type_right);
981 if (is_type_pointer(type_left)) {
982 if (is_null_pointer_constant(right)) {
983 return ASSIGN_SUCCESS;
984 } else if (is_type_pointer(type_right)) {
985 type_t *points_to_left
986 = skip_typeref(type_left->pointer.points_to);
987 type_t *points_to_right
988 = skip_typeref(type_right->pointer.points_to);
990 /* the left type has all qualifiers from the right type */
991 unsigned missing_qualifiers
992 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
993 if (missing_qualifiers != 0) {
994 return ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
997 points_to_left = get_unqualified_type(points_to_left);
998 points_to_right = get_unqualified_type(points_to_right);
1000 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1001 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1002 return ASSIGN_SUCCESS;
1005 if (!types_compatible(points_to_left, points_to_right)) {
1006 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1009 return ASSIGN_SUCCESS;
1010 } else if (is_type_integer(type_right)) {
1011 return ASSIGN_WARNING_POINTER_FROM_INT;
1013 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1014 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1015 && is_type_pointer(type_right))) {
1016 return ASSIGN_SUCCESS;
1017 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1018 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1019 type_t *const unqual_type_left = get_unqualified_type(type_left);
1020 type_t *const unqual_type_right = get_unqualified_type(type_right);
1021 if (types_compatible(unqual_type_left, unqual_type_right)) {
1022 return ASSIGN_SUCCESS;
1024 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1025 return ASSIGN_WARNING_INT_FROM_POINTER;
1028 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1029 return ASSIGN_SUCCESS;
1031 return ASSIGN_ERROR_INCOMPATIBLE;
1034 static expression_t *parse_constant_expression(void)
1036 /* start parsing at precedence 7 (conditional expression) */
1037 expression_t *result = parse_sub_expression(7);
1039 if (!is_constant_expression(result)) {
1040 errorf(&result->base.source_position,
1041 "expression '%E' is not constant\n", result);
1047 static expression_t *parse_assignment_expression(void)
1049 /* start parsing at precedence 2 (assignment expression) */
1050 return parse_sub_expression(2);
1053 static type_t *make_global_typedef(const char *name, type_t *type)
1055 symbol_t *const symbol = symbol_table_insert(name);
1057 declaration_t *const declaration = allocate_declaration_zero();
1058 declaration->namespc = NAMESPACE_NORMAL;
1059 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1060 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1061 declaration->type = type;
1062 declaration->symbol = symbol;
1063 declaration->source_position = builtin_source_position;
1065 record_declaration(declaration);
1067 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1068 typedef_type->typedeft.declaration = declaration;
1070 return typedef_type;
1073 static string_t parse_string_literals(void)
1075 assert(token.type == T_STRING_LITERAL);
1076 string_t result = token.v.string;
1080 while (token.type == T_STRING_LITERAL) {
1081 result = concat_strings(&result, &token.v.string);
1088 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1089 [GNU_AK_CONST] = "const",
1090 [GNU_AK_VOLATILE] = "volatile",
1091 [GNU_AK_CDECL] = "cdecl",
1092 [GNU_AK_STDCALL] = "stdcall",
1093 [GNU_AK_FASTCALL] = "fastcall",
1094 [GNU_AK_DEPRECATED] = "deprecated",
1095 [GNU_AK_NOINLINE] = "noinline",
1096 [GNU_AK_NORETURN] = "noreturn",
1097 [GNU_AK_NAKED] = "naked",
1098 [GNU_AK_PURE] = "pure",
1099 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1100 [GNU_AK_MALLOC] = "malloc",
1101 [GNU_AK_WEAK] = "weak",
1102 [GNU_AK_CONSTRUCTOR] = "constructor",
1103 [GNU_AK_DESTRUCTOR] = "destructor",
1104 [GNU_AK_NOTHROW] = "nothrow",
1105 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1106 [GNU_AK_COMMON] = "common",
1107 [GNU_AK_NOCOMMON] = "nocommon",
1108 [GNU_AK_PACKED] = "packed",
1109 [GNU_AK_SHARED] = "shared",
1110 [GNU_AK_NOTSHARED] = "notshared",
1111 [GNU_AK_USED] = "used",
1112 [GNU_AK_UNUSED] = "unused",
1113 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1114 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1115 [GNU_AK_LONGCALL] = "longcall",
1116 [GNU_AK_SHORTCALL] = "shortcall",
1117 [GNU_AK_LONG_CALL] = "long_call",
1118 [GNU_AK_SHORT_CALL] = "short_call",
1119 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1120 [GNU_AK_INTERRUPT] = "interrupt",
1121 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1122 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1123 [GNU_AK_NESTING] = "nesting",
1124 [GNU_AK_NEAR] = "near",
1125 [GNU_AK_FAR] = "far",
1126 [GNU_AK_SIGNAL] = "signal",
1127 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1128 [GNU_AK_TINY_DATA] = "tiny_data",
1129 [GNU_AK_SAVEALL] = "saveall",
1130 [GNU_AK_FLATTEN] = "flatten",
1131 [GNU_AK_SSEREGPARM] = "sseregparm",
1132 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1133 [GNU_AK_RETURN_TWICE] = "return_twice",
1134 [GNU_AK_MAY_ALIAS] = "may_alias",
1135 [GNU_AK_MS_STRUCT] = "ms_struct",
1136 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1137 [GNU_AK_DLLIMPORT] = "dllimport",
1138 [GNU_AK_DLLEXPORT] = "dllexport",
1139 [GNU_AK_ALIGNED] = "aligned",
1140 [GNU_AK_ALIAS] = "alias",
1141 [GNU_AK_SECTION] = "section",
1142 [GNU_AK_FORMAT] = "format",
1143 [GNU_AK_FORMAT_ARG] = "format_arg",
1144 [GNU_AK_WEAKREF] = "weakref",
1145 [GNU_AK_NONNULL] = "nonnull",
1146 [GNU_AK_TLS_MODEL] = "tls_model",
1147 [GNU_AK_VISIBILITY] = "visibility",
1148 [GNU_AK_REGPARM] = "regparm",
1149 [GNU_AK_MODE] = "mode",
1150 [GNU_AK_MODEL] = "model",
1151 [GNU_AK_TRAP_EXIT] = "trap_exit",
1152 [GNU_AK_SP_SWITCH] = "sp_switch",
1153 [GNU_AK_SENTINEL] = "sentinel"
1157 * compare two string, ignoring double underscores on the second.
1159 static int strcmp_underscore(const char *s1, const char *s2)
1161 if (s2[0] == '_' && s2[1] == '_') {
1162 size_t len2 = strlen(s2);
1163 size_t len1 = strlen(s1);
1164 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1165 return strncmp(s1, s2+2, len2-4);
1169 return strcmp(s1, s2);
1173 * Allocate a new gnu temporal attribute.
1175 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1177 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1178 attribute->kind = kind;
1179 attribute->next = NULL;
1180 attribute->invalid = false;
1181 attribute->have_arguments = false;
1187 * parse one constant expression argument.
1189 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1191 expression_t *expression;
1192 add_anchor_token(')');
1193 expression = parse_constant_expression();
1194 rem_anchor_token(')');
1196 attribute->u.argument = fold_constant(expression);
1199 attribute->invalid = true;
1203 * parse a list of constant expressions arguments.
1205 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1207 argument_list_t **list = &attribute->u.arguments;
1208 argument_list_t *entry;
1209 expression_t *expression;
1210 add_anchor_token(')');
1211 add_anchor_token(',');
1213 expression = parse_constant_expression();
1214 entry = obstack_alloc(&temp_obst, sizeof(entry));
1215 entry->argument = fold_constant(expression);
1218 list = &entry->next;
1219 if (token.type != ',')
1223 rem_anchor_token(',');
1224 rem_anchor_token(')');
1228 attribute->invalid = true;
1232 * parse one string literal argument.
1234 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1237 add_anchor_token('(');
1238 if (token.type != T_STRING_LITERAL) {
1239 parse_error_expected("while parsing attribute directive",
1240 T_STRING_LITERAL, NULL);
1243 *string = parse_string_literals();
1244 rem_anchor_token('(');
1248 attribute->invalid = true;
1252 * parse one tls model.
1254 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1256 static const char *const tls_models[] = {
1262 string_t string = { NULL, 0 };
1263 parse_gnu_attribute_string_arg(attribute, &string);
1264 if (string.begin != NULL) {
1265 for(size_t i = 0; i < 4; ++i) {
1266 if (strcmp(tls_models[i], string.begin) == 0) {
1267 attribute->u.value = i;
1271 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1273 attribute->invalid = true;
1277 * parse one tls model.
1279 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1281 static const char *const visibilities[] = {
1287 string_t string = { NULL, 0 };
1288 parse_gnu_attribute_string_arg(attribute, &string);
1289 if (string.begin != NULL) {
1290 for(size_t i = 0; i < 4; ++i) {
1291 if (strcmp(visibilities[i], string.begin) == 0) {
1292 attribute->u.value = i;
1296 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1298 attribute->invalid = true;
1302 * parse one (code) model.
1304 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1306 static const char *const visibilities[] = {
1311 string_t string = { NULL, 0 };
1312 parse_gnu_attribute_string_arg(attribute, &string);
1313 if (string.begin != NULL) {
1314 for(int i = 0; i < 3; ++i) {
1315 if (strcmp(visibilities[i], string.begin) == 0) {
1316 attribute->u.value = i;
1320 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1322 attribute->invalid = true;
1325 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1327 /* TODO: find out what is allowed here... */
1329 /* at least: byte, word, pointer, list of machine modes
1330 * __XXX___ is interpreted as XXX */
1331 add_anchor_token(')');
1333 if (token.type != T_IDENTIFIER) {
1334 expect(T_IDENTIFIER);
1337 /* This isn't really correct, the backend should provide a list of machine
1338 * specific modes (according to gcc philosophy that is...) */
1339 const char *symbol_str = token.v.symbol->string;
1340 if (strcmp_underscore("QI", symbol_str) == 0 ||
1341 strcmp_underscore("byte", symbol_str) == 0) {
1342 attribute->u.akind = ATOMIC_TYPE_CHAR;
1343 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1344 attribute->u.akind = ATOMIC_TYPE_SHORT;
1345 } else if (strcmp_underscore("SI", symbol_str) == 0
1346 || strcmp_underscore("word", symbol_str) == 0
1347 || strcmp_underscore("pointer", symbol_str) == 0) {
1348 attribute->u.akind = ATOMIC_TYPE_INT;
1349 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1350 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1352 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1353 attribute->invalid = true;
1357 rem_anchor_token(')');
1361 attribute->invalid = true;
1365 * parse one interrupt argument.
1367 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1369 static const char *const interrupts[] = {
1376 string_t string = { NULL, 0 };
1377 parse_gnu_attribute_string_arg(attribute, &string);
1378 if (string.begin != NULL) {
1379 for(size_t i = 0; i < 5; ++i) {
1380 if (strcmp(interrupts[i], string.begin) == 0) {
1381 attribute->u.value = i;
1385 errorf(HERE, "'%s' is not an interrupt", string.begin);
1387 attribute->invalid = true;
1391 * parse ( identifier, const expression, const expression )
1393 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1395 static const char *const format_names[] = {
1403 if (token.type != T_IDENTIFIER) {
1404 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1407 const char *name = token.v.symbol->string;
1408 for(i = 0; i < 4; ++i) {
1409 if (strcmp_underscore(format_names[i], name) == 0)
1413 if (warning.attribute)
1414 warningf(HERE, "'%s' is an unrecognized format function type", name);
1419 add_anchor_token(')');
1420 add_anchor_token(',');
1421 parse_constant_expression();
1422 rem_anchor_token(',');
1423 rem_anchor_token('(');
1426 add_anchor_token(')');
1427 parse_constant_expression();
1428 rem_anchor_token('(');
1432 attribute->u.value = true;
1435 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1437 if (!attribute->have_arguments)
1440 /* should have no arguments */
1441 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1442 eat_until_matching_token('(');
1443 /* we have already consumed '(', so we stop before ')', eat it */
1445 attribute->invalid = true;
1449 * Parse one GNU attribute.
1451 * Note that attribute names can be specified WITH or WITHOUT
1452 * double underscores, ie const or __const__.
1454 * The following attributes are parsed without arguments
1479 * no_instrument_function
1480 * warn_unused_result
1497 * externally_visible
1505 * The following attributes are parsed with arguments
1506 * aligned( const expression )
1507 * alias( string literal )
1508 * section( string literal )
1509 * format( identifier, const expression, const expression )
1510 * format_arg( const expression )
1511 * tls_model( string literal )
1512 * visibility( string literal )
1513 * regparm( const expression )
1514 * model( string leteral )
1515 * trap_exit( const expression )
1516 * sp_switch( string literal )
1518 * The following attributes might have arguments
1519 * weak_ref( string literal )
1520 * non_null( const expression // ',' )
1521 * interrupt( string literal )
1522 * sentinel( constant expression )
1524 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1526 gnu_attribute_t *head = *attributes;
1527 gnu_attribute_t *last = *attributes;
1528 decl_modifiers_t modifiers = 0;
1529 gnu_attribute_t *attribute;
1531 eat(T___attribute__);
1535 if (token.type != ')') {
1536 /* find the end of the list */
1538 while (last->next != NULL)
1542 /* non-empty attribute list */
1545 if (token.type == T_const) {
1547 } else if (token.type == T_volatile) {
1549 } else if (token.type == T_cdecl) {
1550 /* __attribute__((cdecl)), WITH ms mode */
1552 } else if (token.type == T_IDENTIFIER) {
1553 const symbol_t *sym = token.v.symbol;
1556 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1563 for(i = 0; i < GNU_AK_LAST; ++i) {
1564 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1567 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1570 if (kind == GNU_AK_LAST) {
1571 if (warning.attribute)
1572 warningf(HERE, "'%s' attribute directive ignored", name);
1574 /* skip possible arguments */
1575 if (token.type == '(') {
1576 eat_until_matching_token(')');
1579 /* check for arguments */
1580 attribute = allocate_gnu_attribute(kind);
1581 if (token.type == '(') {
1583 if (token.type == ')') {
1584 /* empty args are allowed */
1587 attribute->have_arguments = true;
1592 case GNU_AK_VOLATILE:
1593 case GNU_AK_DEPRECATED:
1598 case GNU_AK_NOCOMMON:
1600 case GNU_AK_NOTSHARED:
1601 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1602 case GNU_AK_WARN_UNUSED_RESULT:
1603 case GNU_AK_LONGCALL:
1604 case GNU_AK_SHORTCALL:
1605 case GNU_AK_LONG_CALL:
1606 case GNU_AK_SHORT_CALL:
1607 case GNU_AK_FUNCTION_VECTOR:
1608 case GNU_AK_INTERRUPT_HANDLER:
1609 case GNU_AK_NMI_HANDLER:
1610 case GNU_AK_NESTING:
1614 case GNU_AK_EIGTHBIT_DATA:
1615 case GNU_AK_TINY_DATA:
1616 case GNU_AK_SAVEALL:
1617 case GNU_AK_FLATTEN:
1618 case GNU_AK_SSEREGPARM:
1619 case GNU_AK_EXTERNALLY_VISIBLE:
1620 case GNU_AK_RETURN_TWICE:
1621 case GNU_AK_MAY_ALIAS:
1622 case GNU_AK_MS_STRUCT:
1623 case GNU_AK_GCC_STRUCT:
1626 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1627 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1628 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1629 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1630 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1631 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1632 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1633 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1634 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1635 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1636 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1637 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1638 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1639 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1640 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1641 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1643 case GNU_AK_ALIGNED:
1644 /* __align__ may be used without an argument */
1645 if (attribute->have_arguments) {
1646 parse_gnu_attribute_const_arg(attribute);
1650 case GNU_AK_FORMAT_ARG:
1651 case GNU_AK_REGPARM:
1652 case GNU_AK_TRAP_EXIT:
1653 if (!attribute->have_arguments) {
1654 /* should have arguments */
1655 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1656 attribute->invalid = true;
1658 parse_gnu_attribute_const_arg(attribute);
1661 case GNU_AK_SECTION:
1662 case GNU_AK_SP_SWITCH:
1663 if (!attribute->have_arguments) {
1664 /* should have arguments */
1665 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1666 attribute->invalid = true;
1668 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1671 if (!attribute->have_arguments) {
1672 /* should have arguments */
1673 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1674 attribute->invalid = true;
1676 parse_gnu_attribute_format_args(attribute);
1678 case GNU_AK_WEAKREF:
1679 /* may have one string argument */
1680 if (attribute->have_arguments)
1681 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1683 case GNU_AK_NONNULL:
1684 if (attribute->have_arguments)
1685 parse_gnu_attribute_const_arg_list(attribute);
1687 case GNU_AK_TLS_MODEL:
1688 if (!attribute->have_arguments) {
1689 /* should have arguments */
1690 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1692 parse_gnu_attribute_tls_model_arg(attribute);
1694 case GNU_AK_VISIBILITY:
1695 if (!attribute->have_arguments) {
1696 /* should have arguments */
1697 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1699 parse_gnu_attribute_visibility_arg(attribute);
1702 if (!attribute->have_arguments) {
1703 /* should have arguments */
1704 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1706 parse_gnu_attribute_model_arg(attribute);
1710 if (!attribute->have_arguments) {
1711 /* should have arguments */
1712 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1714 parse_gnu_attribute_mode_arg(attribute);
1717 case GNU_AK_INTERRUPT:
1718 /* may have one string argument */
1719 if (attribute->have_arguments)
1720 parse_gnu_attribute_interrupt_arg(attribute);
1722 case GNU_AK_SENTINEL:
1723 /* may have one string argument */
1724 if (attribute->have_arguments)
1725 parse_gnu_attribute_const_arg(attribute);
1728 /* already handled */
1732 check_no_argument(attribute, name);
1735 if (attribute != NULL) {
1737 last->next = attribute;
1740 head = last = attribute;
1744 if (token.type != ',')
1758 * Parse GNU attributes.
1760 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1762 decl_modifiers_t modifiers = 0;
1765 switch(token.type) {
1766 case T___attribute__:
1767 modifiers |= parse_gnu_attribute(attributes);
1773 if (token.type != T_STRING_LITERAL) {
1774 parse_error_expected("while parsing assembler attribute",
1775 T_STRING_LITERAL, NULL);
1776 eat_until_matching_token('(');
1779 parse_string_literals();
1784 case T_cdecl: modifiers |= DM_CDECL; break;
1785 case T__fastcall: modifiers |= DM_FASTCALL; break;
1786 case T__stdcall: modifiers |= DM_STDCALL; break;
1789 /* TODO record modifier */
1790 warningf(HERE, "Ignoring declaration modifier %K", &token);
1794 default: return modifiers;
1801 static designator_t *parse_designation(void)
1803 designator_t *result = NULL;
1804 designator_t *last = NULL;
1807 designator_t *designator;
1808 switch(token.type) {
1810 designator = allocate_ast_zero(sizeof(designator[0]));
1811 designator->source_position = token.source_position;
1813 add_anchor_token(']');
1814 designator->array_index = parse_constant_expression();
1815 rem_anchor_token(']');
1819 designator = allocate_ast_zero(sizeof(designator[0]));
1820 designator->source_position = token.source_position;
1822 if (token.type != T_IDENTIFIER) {
1823 parse_error_expected("while parsing designator",
1824 T_IDENTIFIER, NULL);
1827 designator->symbol = token.v.symbol;
1835 assert(designator != NULL);
1837 last->next = designator;
1839 result = designator;
1847 static initializer_t *initializer_from_string(array_type_t *type,
1848 const string_t *const string)
1850 /* TODO: check len vs. size of array type */
1853 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1854 initializer->string.string = *string;
1859 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1860 wide_string_t *const string)
1862 /* TODO: check len vs. size of array type */
1865 initializer_t *const initializer =
1866 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1867 initializer->wide_string.string = *string;
1873 * Build an initializer from a given expression.
1875 static initializer_t *initializer_from_expression(type_t *orig_type,
1876 expression_t *expression)
1878 /* TODO check that expression is a constant expression */
1880 /* § 6.7.8.14/15 char array may be initialized by string literals */
1881 type_t *type = skip_typeref(orig_type);
1882 type_t *expr_type_orig = expression->base.type;
1883 type_t *expr_type = skip_typeref(expr_type_orig);
1884 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1885 array_type_t *const array_type = &type->array;
1886 type_t *const element_type = skip_typeref(array_type->element_type);
1888 if (element_type->kind == TYPE_ATOMIC) {
1889 atomic_type_kind_t akind = element_type->atomic.akind;
1890 switch (expression->kind) {
1891 case EXPR_STRING_LITERAL:
1892 if (akind == ATOMIC_TYPE_CHAR
1893 || akind == ATOMIC_TYPE_SCHAR
1894 || akind == ATOMIC_TYPE_UCHAR) {
1895 return initializer_from_string(array_type,
1896 &expression->string.value);
1899 case EXPR_WIDE_STRING_LITERAL: {
1900 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1901 if (get_unqualified_type(element_type) == bare_wchar_type) {
1902 return initializer_from_wide_string(array_type,
1903 &expression->wide_string.value);
1913 assign_error_t error = semantic_assign(type, expression);
1914 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1916 report_assign_error(error, type, expression, "initializer",
1917 &expression->base.source_position);
1919 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1920 result->value.value = create_implicit_cast(expression, type);
1926 * Checks if a given expression can be used as an constant initializer.
1928 static bool is_initializer_constant(const expression_t *expression)
1930 return is_constant_expression(expression)
1931 || is_address_constant(expression);
1935 * Parses an scalar initializer.
1937 * § 6.7.8.11; eat {} without warning
1939 static initializer_t *parse_scalar_initializer(type_t *type,
1940 bool must_be_constant)
1942 /* there might be extra {} hierarchies */
1944 if (token.type == '{') {
1945 warningf(HERE, "extra curly braces around scalar initializer");
1949 } while (token.type == '{');
1952 expression_t *expression = parse_assignment_expression();
1953 if (must_be_constant && !is_initializer_constant(expression)) {
1954 errorf(&expression->base.source_position,
1955 "Initialisation expression '%E' is not constant\n",
1959 initializer_t *initializer = initializer_from_expression(type, expression);
1961 if (initializer == NULL) {
1962 errorf(&expression->base.source_position,
1963 "expression '%E' (type '%T') doesn't match expected type '%T'",
1964 expression, expression->base.type, type);
1969 bool additional_warning_displayed = false;
1970 while (braces > 0) {
1971 if (token.type == ',') {
1974 if (token.type != '}') {
1975 if (!additional_warning_displayed) {
1976 warningf(HERE, "additional elements in scalar initializer");
1977 additional_warning_displayed = true;
1988 * An entry in the type path.
1990 typedef struct type_path_entry_t type_path_entry_t;
1991 struct type_path_entry_t {
1992 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1994 size_t index; /**< For array types: the current index. */
1995 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2000 * A type path expression a position inside compound or array types.
2002 typedef struct type_path_t type_path_t;
2003 struct type_path_t {
2004 type_path_entry_t *path; /**< An flexible array containing the current path. */
2005 type_t *top_type; /**< type of the element the path points */
2006 size_t max_index; /**< largest index in outermost array */
2010 * Prints a type path for debugging.
2012 static __attribute__((unused)) void debug_print_type_path(
2013 const type_path_t *path)
2015 size_t len = ARR_LEN(path->path);
2017 for(size_t i = 0; i < len; ++i) {
2018 const type_path_entry_t *entry = & path->path[i];
2020 type_t *type = skip_typeref(entry->type);
2021 if (is_type_compound(type)) {
2022 /* in gcc mode structs can have no members */
2023 if (entry->v.compound_entry == NULL) {
2027 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2028 } else if (is_type_array(type)) {
2029 fprintf(stderr, "[%zu]", entry->v.index);
2031 fprintf(stderr, "-INVALID-");
2034 if (path->top_type != NULL) {
2035 fprintf(stderr, " (");
2036 print_type(path->top_type);
2037 fprintf(stderr, ")");
2042 * Return the top type path entry, ie. in a path
2043 * (type).a.b returns the b.
2045 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2047 size_t len = ARR_LEN(path->path);
2049 return &path->path[len-1];
2053 * Enlarge the type path by an (empty) element.
2055 static type_path_entry_t *append_to_type_path(type_path_t *path)
2057 size_t len = ARR_LEN(path->path);
2058 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2060 type_path_entry_t *result = & path->path[len];
2061 memset(result, 0, sizeof(result[0]));
2066 * Descending into a sub-type. Enter the scope of the current
2069 static void descend_into_subtype(type_path_t *path)
2071 type_t *orig_top_type = path->top_type;
2072 type_t *top_type = skip_typeref(orig_top_type);
2074 assert(is_type_compound(top_type) || is_type_array(top_type));
2076 type_path_entry_t *top = append_to_type_path(path);
2077 top->type = top_type;
2079 if (is_type_compound(top_type)) {
2080 declaration_t *declaration = top_type->compound.declaration;
2081 declaration_t *entry = declaration->scope.declarations;
2082 top->v.compound_entry = entry;
2084 if (entry != NULL) {
2085 path->top_type = entry->type;
2087 path->top_type = NULL;
2090 assert(is_type_array(top_type));
2093 path->top_type = top_type->array.element_type;
2098 * Pop an entry from the given type path, ie. returning from
2099 * (type).a.b to (type).a
2101 static void ascend_from_subtype(type_path_t *path)
2103 type_path_entry_t *top = get_type_path_top(path);
2105 path->top_type = top->type;
2107 size_t len = ARR_LEN(path->path);
2108 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2112 * Pop entries from the given type path until the given
2113 * path level is reached.
2115 static void ascend_to(type_path_t *path, size_t top_path_level)
2117 size_t len = ARR_LEN(path->path);
2119 while (len > top_path_level) {
2120 ascend_from_subtype(path);
2121 len = ARR_LEN(path->path);
2125 static bool walk_designator(type_path_t *path, const designator_t *designator,
2126 bool used_in_offsetof)
2128 for( ; designator != NULL; designator = designator->next) {
2129 type_path_entry_t *top = get_type_path_top(path);
2130 type_t *orig_type = top->type;
2132 type_t *type = skip_typeref(orig_type);
2134 if (designator->symbol != NULL) {
2135 symbol_t *symbol = designator->symbol;
2136 if (!is_type_compound(type)) {
2137 if (is_type_valid(type)) {
2138 errorf(&designator->source_position,
2139 "'.%Y' designator used for non-compound type '%T'",
2145 declaration_t *declaration = type->compound.declaration;
2146 declaration_t *iter = declaration->scope.declarations;
2147 for( ; iter != NULL; iter = iter->next) {
2148 if (iter->symbol == symbol) {
2153 errorf(&designator->source_position,
2154 "'%T' has no member named '%Y'", orig_type, symbol);
2157 if (used_in_offsetof) {
2158 type_t *real_type = skip_typeref(iter->type);
2159 if (real_type->kind == TYPE_BITFIELD) {
2160 errorf(&designator->source_position,
2161 "offsetof designator '%Y' may not specify bitfield",
2167 top->type = orig_type;
2168 top->v.compound_entry = iter;
2169 orig_type = iter->type;
2171 expression_t *array_index = designator->array_index;
2172 assert(designator->array_index != NULL);
2174 if (!is_type_array(type)) {
2175 if (is_type_valid(type)) {
2176 errorf(&designator->source_position,
2177 "[%E] designator used for non-array type '%T'",
2178 array_index, orig_type);
2182 if (!is_type_valid(array_index->base.type)) {
2186 long index = fold_constant(array_index);
2187 if (!used_in_offsetof) {
2189 errorf(&designator->source_position,
2190 "array index [%E] must be positive", array_index);
2193 if (type->array.size_constant == true) {
2194 long array_size = type->array.size;
2195 if (index >= array_size) {
2196 errorf(&designator->source_position,
2197 "designator [%E] (%d) exceeds array size %d",
2198 array_index, index, array_size);
2204 top->type = orig_type;
2205 top->v.index = (size_t) index;
2206 orig_type = type->array.element_type;
2208 path->top_type = orig_type;
2210 if (designator->next != NULL) {
2211 descend_into_subtype(path);
2220 static void advance_current_object(type_path_t *path, size_t top_path_level)
2222 type_path_entry_t *top = get_type_path_top(path);
2224 type_t *type = skip_typeref(top->type);
2225 if (is_type_union(type)) {
2226 /* in unions only the first element is initialized */
2227 top->v.compound_entry = NULL;
2228 } else if (is_type_struct(type)) {
2229 declaration_t *entry = top->v.compound_entry;
2231 entry = entry->next;
2232 top->v.compound_entry = entry;
2233 if (entry != NULL) {
2234 path->top_type = entry->type;
2238 assert(is_type_array(type));
2242 if (!type->array.size_constant || top->v.index < type->array.size) {
2247 /* we're past the last member of the current sub-aggregate, try if we
2248 * can ascend in the type hierarchy and continue with another subobject */
2249 size_t len = ARR_LEN(path->path);
2251 if (len > top_path_level) {
2252 ascend_from_subtype(path);
2253 advance_current_object(path, top_path_level);
2255 path->top_type = NULL;
2260 * skip until token is found.
2262 static void skip_until(int type)
2264 while (token.type != type) {
2265 if (token.type == T_EOF)
2272 * skip any {...} blocks until a closing bracket is reached.
2274 static void skip_initializers(void)
2276 if (token.type == '{')
2279 while (token.type != '}') {
2280 if (token.type == T_EOF)
2282 if (token.type == '{') {
2290 static initializer_t *create_empty_initializer(void)
2292 static initializer_t empty_initializer
2293 = { .list = { { INITIALIZER_LIST }, 0 } };
2294 return &empty_initializer;
2298 * Parse a part of an initialiser for a struct or union,
2300 static initializer_t *parse_sub_initializer(type_path_t *path,
2301 type_t *outer_type, size_t top_path_level,
2302 parse_initializer_env_t *env)
2304 if (token.type == '}') {
2305 /* empty initializer */
2306 return create_empty_initializer();
2309 type_t *orig_type = path->top_type;
2310 type_t *type = NULL;
2312 if (orig_type == NULL) {
2313 /* We are initializing an empty compound. */
2315 type = skip_typeref(orig_type);
2317 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2318 * initializers in this case. */
2319 if (!is_type_valid(type)) {
2320 skip_initializers();
2321 return create_empty_initializer();
2325 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2328 designator_t *designator = NULL;
2329 if (token.type == '.' || token.type == '[') {
2330 designator = parse_designation();
2332 /* reset path to toplevel, evaluate designator from there */
2333 ascend_to(path, top_path_level);
2334 if (!walk_designator(path, designator, false)) {
2335 /* can't continue after designation error */
2339 initializer_t *designator_initializer
2340 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2341 designator_initializer->designator.designator = designator;
2342 ARR_APP1(initializer_t*, initializers, designator_initializer);
2344 orig_type = path->top_type;
2345 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2350 if (token.type == '{') {
2351 if (type != NULL && is_type_scalar(type)) {
2352 sub = parse_scalar_initializer(type, env->must_be_constant);
2356 if (env->declaration != NULL) {
2357 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2358 env->declaration->symbol);
2360 errorf(HERE, "extra brace group at end of initializer");
2363 descend_into_subtype(path);
2365 add_anchor_token('}');
2366 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2368 rem_anchor_token('}');
2371 ascend_from_subtype(path);
2375 goto error_parse_next;
2379 /* must be an expression */
2380 expression_t *expression = parse_assignment_expression();
2382 if (env->must_be_constant && !is_initializer_constant(expression)) {
2383 errorf(&expression->base.source_position,
2384 "Initialisation expression '%E' is not constant\n",
2389 /* we are already outside, ... */
2393 /* handle { "string" } special case */
2394 if ((expression->kind == EXPR_STRING_LITERAL
2395 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2396 && outer_type != NULL) {
2397 sub = initializer_from_expression(outer_type, expression);
2399 if (token.type == ',') {
2402 if (token.type != '}') {
2403 warningf(HERE, "excessive elements in initializer for type '%T'",
2406 /* TODO: eat , ... */
2411 /* descend into subtypes until expression matches type */
2413 orig_type = path->top_type;
2414 type = skip_typeref(orig_type);
2416 sub = initializer_from_expression(orig_type, expression);
2420 if (!is_type_valid(type)) {
2423 if (is_type_scalar(type)) {
2424 errorf(&expression->base.source_position,
2425 "expression '%E' doesn't match expected type '%T'",
2426 expression, orig_type);
2430 descend_into_subtype(path);
2434 /* update largest index of top array */
2435 const type_path_entry_t *first = &path->path[0];
2436 type_t *first_type = first->type;
2437 first_type = skip_typeref(first_type);
2438 if (is_type_array(first_type)) {
2439 size_t index = first->v.index;
2440 if (index > path->max_index)
2441 path->max_index = index;
2445 /* append to initializers list */
2446 ARR_APP1(initializer_t*, initializers, sub);
2449 if (env->declaration != NULL)
2450 warningf(HERE, "excess elements in struct initializer for '%Y'",
2451 env->declaration->symbol);
2453 warningf(HERE, "excess elements in struct initializer");
2457 if (token.type == '}') {
2461 if (token.type == '}') {
2466 /* advance to the next declaration if we are not at the end */
2467 advance_current_object(path, top_path_level);
2468 orig_type = path->top_type;
2469 if (orig_type != NULL)
2470 type = skip_typeref(orig_type);
2476 size_t len = ARR_LEN(initializers);
2477 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2478 initializer_t *result = allocate_ast_zero(size);
2479 result->kind = INITIALIZER_LIST;
2480 result->list.len = len;
2481 memcpy(&result->list.initializers, initializers,
2482 len * sizeof(initializers[0]));
2484 DEL_ARR_F(initializers);
2485 ascend_to(path, top_path_level+1);
2490 skip_initializers();
2491 DEL_ARR_F(initializers);
2492 ascend_to(path, top_path_level+1);
2497 * Parses an initializer. Parsers either a compound literal
2498 * (env->declaration == NULL) or an initializer of a declaration.
2500 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2502 type_t *type = skip_typeref(env->type);
2503 initializer_t *result = NULL;
2506 if (is_type_scalar(type)) {
2507 result = parse_scalar_initializer(type, env->must_be_constant);
2508 } else if (token.type == '{') {
2512 memset(&path, 0, sizeof(path));
2513 path.top_type = env->type;
2514 path.path = NEW_ARR_F(type_path_entry_t, 0);
2516 descend_into_subtype(&path);
2518 add_anchor_token('}');
2519 result = parse_sub_initializer(&path, env->type, 1, env);
2520 rem_anchor_token('}');
2522 max_index = path.max_index;
2523 DEL_ARR_F(path.path);
2527 /* parse_scalar_initializer() also works in this case: we simply
2528 * have an expression without {} around it */
2529 result = parse_scalar_initializer(type, env->must_be_constant);
2532 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2533 * the array type size */
2534 if (is_type_array(type) && type->array.size_expression == NULL
2535 && result != NULL) {
2537 switch (result->kind) {
2538 case INITIALIZER_LIST:
2539 size = max_index + 1;
2542 case INITIALIZER_STRING:
2543 size = result->string.string.size;
2546 case INITIALIZER_WIDE_STRING:
2547 size = result->wide_string.string.size;
2550 case INITIALIZER_DESIGNATOR:
2551 case INITIALIZER_VALUE:
2552 /* can happen for parse errors */
2557 internal_errorf(HERE, "invalid initializer type");
2560 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2561 cnst->base.type = type_size_t;
2562 cnst->conste.v.int_value = size;
2564 type_t *new_type = duplicate_type(type);
2566 new_type->array.size_expression = cnst;
2567 new_type->array.size_constant = true;
2568 new_type->array.size = size;
2569 env->type = new_type;
2577 static declaration_t *append_declaration(declaration_t *declaration);
2579 static declaration_t *parse_compound_type_specifier(bool is_struct)
2581 gnu_attribute_t *attributes = NULL;
2582 decl_modifiers_t modifiers = 0;
2589 symbol_t *symbol = NULL;
2590 declaration_t *declaration = NULL;
2592 if (token.type == T___attribute__) {
2593 modifiers |= parse_attributes(&attributes);
2596 if (token.type == T_IDENTIFIER) {
2597 symbol = token.v.symbol;
2600 namespace_t const namespc =
2601 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2602 declaration = get_declaration(symbol, namespc);
2603 if (declaration != NULL) {
2604 if (declaration->parent_scope != scope &&
2605 (token.type == '{' || token.type == ';')) {
2607 } else if (declaration->init.complete &&
2608 token.type == '{') {
2609 assert(symbol != NULL);
2610 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2611 is_struct ? "struct" : "union", symbol,
2612 &declaration->source_position);
2613 declaration->scope.declarations = NULL;
2616 } else if (token.type != '{') {
2618 parse_error_expected("while parsing struct type specifier",
2619 T_IDENTIFIER, '{', NULL);
2621 parse_error_expected("while parsing union type specifier",
2622 T_IDENTIFIER, '{', NULL);
2628 if (declaration == NULL) {
2629 declaration = allocate_declaration_zero();
2630 declaration->namespc =
2631 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2632 declaration->source_position = token.source_position;
2633 declaration->symbol = symbol;
2634 declaration->parent_scope = scope;
2635 if (symbol != NULL) {
2636 environment_push(declaration);
2638 append_declaration(declaration);
2641 if (token.type == '{') {
2642 declaration->init.complete = true;
2644 parse_compound_type_entries(declaration);
2645 modifiers |= parse_attributes(&attributes);
2648 declaration->modifiers |= modifiers;
2652 static void parse_enum_entries(type_t *const enum_type)
2656 if (token.type == '}') {
2658 errorf(HERE, "empty enum not allowed");
2662 add_anchor_token('}');
2664 if (token.type != T_IDENTIFIER) {
2665 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2667 rem_anchor_token('}');
2671 declaration_t *const entry = allocate_declaration_zero();
2672 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2673 entry->type = enum_type;
2674 entry->symbol = token.v.symbol;
2675 entry->source_position = token.source_position;
2678 if (token.type == '=') {
2680 expression_t *value = parse_constant_expression();
2682 value = create_implicit_cast(value, enum_type);
2683 entry->init.enum_value = value;
2688 record_declaration(entry);
2690 if (token.type != ',')
2693 } while (token.type != '}');
2694 rem_anchor_token('}');
2702 static type_t *parse_enum_specifier(void)
2704 gnu_attribute_t *attributes = NULL;
2705 declaration_t *declaration;
2709 if (token.type == T_IDENTIFIER) {
2710 symbol = token.v.symbol;
2713 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2714 } else if (token.type != '{') {
2715 parse_error_expected("while parsing enum type specifier",
2716 T_IDENTIFIER, '{', NULL);
2723 if (declaration == NULL) {
2724 declaration = allocate_declaration_zero();
2725 declaration->namespc = NAMESPACE_ENUM;
2726 declaration->source_position = token.source_position;
2727 declaration->symbol = symbol;
2728 declaration->parent_scope = scope;
2731 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2732 type->enumt.declaration = declaration;
2734 if (token.type == '{') {
2735 if (declaration->init.complete) {
2736 errorf(HERE, "multiple definitions of enum %Y", symbol);
2738 if (symbol != NULL) {
2739 environment_push(declaration);
2741 append_declaration(declaration);
2742 declaration->init.complete = true;
2744 parse_enum_entries(type);
2745 parse_attributes(&attributes);
2752 * if a symbol is a typedef to another type, return true
2754 static bool is_typedef_symbol(symbol_t *symbol)
2756 const declaration_t *const declaration =
2757 get_declaration(symbol, NAMESPACE_NORMAL);
2759 declaration != NULL &&
2760 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2763 static type_t *parse_typeof(void)
2770 add_anchor_token(')');
2772 expression_t *expression = NULL;
2775 switch(token.type) {
2776 case T___extension__:
2777 /* This can be a prefix to a typename or an expression. We simply eat
2781 } while (token.type == T___extension__);
2785 if (is_typedef_symbol(token.v.symbol)) {
2786 type = parse_typename();
2788 expression = parse_expression();
2789 type = expression->base.type;
2794 type = parse_typename();
2798 expression = parse_expression();
2799 type = expression->base.type;
2803 rem_anchor_token(')');
2806 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2807 typeof_type->typeoft.expression = expression;
2808 typeof_type->typeoft.typeof_type = type;
2815 typedef enum specifiers_t {
2816 SPECIFIER_SIGNED = 1 << 0,
2817 SPECIFIER_UNSIGNED = 1 << 1,
2818 SPECIFIER_LONG = 1 << 2,
2819 SPECIFIER_INT = 1 << 3,
2820 SPECIFIER_DOUBLE = 1 << 4,
2821 SPECIFIER_CHAR = 1 << 5,
2822 SPECIFIER_SHORT = 1 << 6,
2823 SPECIFIER_LONG_LONG = 1 << 7,
2824 SPECIFIER_FLOAT = 1 << 8,
2825 SPECIFIER_BOOL = 1 << 9,
2826 SPECIFIER_VOID = 1 << 10,
2827 SPECIFIER_INT8 = 1 << 11,
2828 SPECIFIER_INT16 = 1 << 12,
2829 SPECIFIER_INT32 = 1 << 13,
2830 SPECIFIER_INT64 = 1 << 14,
2831 SPECIFIER_INT128 = 1 << 15,
2832 SPECIFIER_COMPLEX = 1 << 16,
2833 SPECIFIER_IMAGINARY = 1 << 17,
2836 static type_t *create_builtin_type(symbol_t *const symbol,
2837 type_t *const real_type)
2839 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2840 type->builtin.symbol = symbol;
2841 type->builtin.real_type = real_type;
2843 type_t *result = typehash_insert(type);
2844 if (type != result) {
2851 static type_t *get_typedef_type(symbol_t *symbol)
2853 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2854 if (declaration == NULL ||
2855 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2858 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2859 type->typedeft.declaration = declaration;
2865 * check for the allowed MS alignment values.
2867 static bool check_alignment_value(long long intvalue)
2869 if (intvalue < 1 || intvalue > 8192) {
2870 errorf(HERE, "illegal alignment value");
2873 unsigned v = (unsigned)intvalue;
2874 for(unsigned i = 1; i <= 8192; i += i) {
2878 errorf(HERE, "alignment must be power of two");
2882 #define DET_MOD(name, tag) do { \
2883 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2884 *modifiers |= tag; \
2887 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2889 decl_modifiers_t *modifiers = &specifiers->modifiers;
2892 if (token.type == T_restrict) {
2894 DET_MOD(restrict, DM_RESTRICT);
2896 } else if (token.type != T_IDENTIFIER)
2898 symbol_t *symbol = token.v.symbol;
2899 if (symbol == sym_align) {
2902 if (token.type != T_INTEGER)
2904 if (check_alignment_value(token.v.intvalue)) {
2905 if (specifiers->alignment != 0)
2906 warningf(HERE, "align used more than once");
2907 specifiers->alignment = (unsigned char)token.v.intvalue;
2911 } else if (symbol == sym_allocate) {
2914 if (token.type != T_IDENTIFIER)
2916 (void)token.v.symbol;
2918 } else if (symbol == sym_dllimport) {
2920 DET_MOD(dllimport, DM_DLLIMPORT);
2921 } else if (symbol == sym_dllexport) {
2923 DET_MOD(dllexport, DM_DLLEXPORT);
2924 } else if (symbol == sym_thread) {
2926 DET_MOD(thread, DM_THREAD);
2927 } else if (symbol == sym_naked) {
2929 DET_MOD(naked, DM_NAKED);
2930 } else if (symbol == sym_noinline) {
2932 DET_MOD(noinline, DM_NOINLINE);
2933 } else if (symbol == sym_noreturn) {
2935 DET_MOD(noreturn, DM_NORETURN);
2936 } else if (symbol == sym_nothrow) {
2938 DET_MOD(nothrow, DM_NOTHROW);
2939 } else if (symbol == sym_novtable) {
2941 DET_MOD(novtable, DM_NOVTABLE);
2942 } else if (symbol == sym_property) {
2946 bool is_get = false;
2947 if (token.type != T_IDENTIFIER)
2949 if (token.v.symbol == sym_get) {
2951 } else if (token.v.symbol == sym_put) {
2953 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2958 if (token.type != T_IDENTIFIER)
2961 if (specifiers->get_property_sym != NULL) {
2962 errorf(HERE, "get property name already specified");
2964 specifiers->get_property_sym = token.v.symbol;
2967 if (specifiers->put_property_sym != NULL) {
2968 errorf(HERE, "put property name already specified");
2970 specifiers->put_property_sym = token.v.symbol;
2974 if (token.type == ',') {
2981 } else if (symbol == sym_selectany) {
2983 DET_MOD(selectany, DM_SELECTANY);
2984 } else if (symbol == sym_uuid) {
2987 if (token.type != T_STRING_LITERAL)
2991 } else if (symbol == sym_deprecated) {
2993 if (specifiers->deprecated != 0)
2994 warningf(HERE, "deprecated used more than once");
2995 specifiers->deprecated = 1;
2996 if (token.type == '(') {
2998 if (token.type == T_STRING_LITERAL) {
2999 specifiers->deprecated_string = token.v.string.begin;
3002 errorf(HERE, "string literal expected");
3006 } else if (symbol == sym_noalias) {
3008 DET_MOD(noalias, DM_NOALIAS);
3010 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3012 if (token.type == '(')
3016 if (token.type == ',')
3023 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3025 type_t *type = NULL;
3026 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3027 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3028 unsigned type_specifiers = 0;
3031 specifiers->source_position = token.source_position;
3034 specifiers->modifiers
3035 |= parse_attributes(&specifiers->gnu_attributes);
3036 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3037 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3039 switch(token.type) {
3042 #define MATCH_STORAGE_CLASS(token, class) \
3044 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3045 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3047 specifiers->declared_storage_class = class; \
3051 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3052 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3053 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3054 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3055 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3060 add_anchor_token(')');
3061 parse_microsoft_extended_decl_modifier(specifiers);
3062 rem_anchor_token(')');
3067 switch (specifiers->declared_storage_class) {
3068 case STORAGE_CLASS_NONE:
3069 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3072 case STORAGE_CLASS_EXTERN:
3073 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3076 case STORAGE_CLASS_STATIC:
3077 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3081 errorf(HERE, "multiple storage classes in declaration specifiers");
3087 /* type qualifiers */
3088 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3090 qualifiers |= qualifier; \
3094 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3095 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3096 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3097 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3098 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3099 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3100 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3101 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3103 case T___extension__:
3108 /* type specifiers */
3109 #define MATCH_SPECIFIER(token, specifier, name) \
3112 if (type_specifiers & specifier) { \
3113 errorf(HERE, "multiple " name " type specifiers given"); \
3115 type_specifiers |= specifier; \
3119 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3120 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3121 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3122 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3123 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3124 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3125 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3126 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3127 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3128 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3129 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3130 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3131 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3132 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3133 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3134 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3136 case T__forceinline:
3137 /* only in microsoft mode */
3138 specifiers->modifiers |= DM_FORCEINLINE;
3142 specifiers->is_inline = true;
3147 if (type_specifiers & SPECIFIER_LONG_LONG) {
3148 errorf(HERE, "multiple type specifiers given");
3149 } else if (type_specifiers & SPECIFIER_LONG) {
3150 type_specifiers |= SPECIFIER_LONG_LONG;
3152 type_specifiers |= SPECIFIER_LONG;
3157 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3159 type->compound.declaration = parse_compound_type_specifier(true);
3163 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3164 type->compound.declaration = parse_compound_type_specifier(false);
3165 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3166 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3170 type = parse_enum_specifier();
3173 type = parse_typeof();
3175 case T___builtin_va_list:
3176 type = duplicate_type(type_valist);
3180 case T_IDENTIFIER: {
3181 /* only parse identifier if we haven't found a type yet */
3182 if (type != NULL || type_specifiers != 0)
3183 goto finish_specifiers;
3185 type_t *typedef_type = get_typedef_type(token.v.symbol);
3187 if (typedef_type == NULL)
3188 goto finish_specifiers;
3191 type = typedef_type;
3195 /* function specifier */
3197 goto finish_specifiers;
3204 atomic_type_kind_t atomic_type;
3206 /* match valid basic types */
3207 switch(type_specifiers) {
3208 case SPECIFIER_VOID:
3209 atomic_type = ATOMIC_TYPE_VOID;
3211 case SPECIFIER_CHAR:
3212 atomic_type = ATOMIC_TYPE_CHAR;
3214 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3215 atomic_type = ATOMIC_TYPE_SCHAR;
3217 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3218 atomic_type = ATOMIC_TYPE_UCHAR;
3220 case SPECIFIER_SHORT:
3221 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3222 case SPECIFIER_SHORT | SPECIFIER_INT:
3223 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3224 atomic_type = ATOMIC_TYPE_SHORT;
3226 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3227 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3228 atomic_type = ATOMIC_TYPE_USHORT;
3231 case SPECIFIER_SIGNED:
3232 case SPECIFIER_SIGNED | SPECIFIER_INT:
3233 atomic_type = ATOMIC_TYPE_INT;
3235 case SPECIFIER_UNSIGNED:
3236 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3237 atomic_type = ATOMIC_TYPE_UINT;
3239 case SPECIFIER_LONG:
3240 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3241 case SPECIFIER_LONG | SPECIFIER_INT:
3242 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3243 atomic_type = ATOMIC_TYPE_LONG;
3245 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3246 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3247 atomic_type = ATOMIC_TYPE_ULONG;
3249 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3250 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3251 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3252 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3254 atomic_type = ATOMIC_TYPE_LONGLONG;
3256 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3257 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3259 atomic_type = ATOMIC_TYPE_ULONGLONG;
3262 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3263 atomic_type = unsigned_int8_type_kind;
3266 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3267 atomic_type = unsigned_int16_type_kind;
3270 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3271 atomic_type = unsigned_int32_type_kind;
3274 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3275 atomic_type = unsigned_int64_type_kind;
3278 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3279 atomic_type = unsigned_int128_type_kind;
3282 case SPECIFIER_INT8:
3283 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3284 atomic_type = int8_type_kind;
3287 case SPECIFIER_INT16:
3288 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3289 atomic_type = int16_type_kind;
3292 case SPECIFIER_INT32:
3293 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3294 atomic_type = int32_type_kind;
3297 case SPECIFIER_INT64:
3298 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3299 atomic_type = int64_type_kind;
3302 case SPECIFIER_INT128:
3303 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3304 atomic_type = int128_type_kind;
3307 case SPECIFIER_FLOAT:
3308 atomic_type = ATOMIC_TYPE_FLOAT;
3310 case SPECIFIER_DOUBLE:
3311 atomic_type = ATOMIC_TYPE_DOUBLE;
3313 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3314 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3316 case SPECIFIER_BOOL:
3317 atomic_type = ATOMIC_TYPE_BOOL;
3319 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3320 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3321 atomic_type = ATOMIC_TYPE_FLOAT;
3323 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3324 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3325 atomic_type = ATOMIC_TYPE_DOUBLE;
3327 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3328 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3329 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3332 /* invalid specifier combination, give an error message */
3333 if (type_specifiers == 0) {
3334 if (! strict_mode) {
3335 if (warning.implicit_int) {
3336 warningf(HERE, "no type specifiers in declaration, using 'int'");
3338 atomic_type = ATOMIC_TYPE_INT;
3341 errorf(HERE, "no type specifiers given in declaration");
3343 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3344 (type_specifiers & SPECIFIER_UNSIGNED)) {
3345 errorf(HERE, "signed and unsigned specifiers gives");
3346 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3347 errorf(HERE, "only integer types can be signed or unsigned");
3349 errorf(HERE, "multiple datatypes in declaration");
3351 atomic_type = ATOMIC_TYPE_INVALID;
3354 if (type_specifiers & SPECIFIER_COMPLEX &&
3355 atomic_type != ATOMIC_TYPE_INVALID) {
3356 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3357 type->complex.akind = atomic_type;
3358 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3359 atomic_type != ATOMIC_TYPE_INVALID) {
3360 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3361 type->imaginary.akind = atomic_type;
3363 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3364 type->atomic.akind = atomic_type;
3368 if (type_specifiers != 0) {
3369 errorf(HERE, "multiple datatypes in declaration");
3373 /* FIXME: check type qualifiers here */
3375 type->base.qualifiers = qualifiers;
3376 type->base.modifiers = modifiers;
3378 type_t *result = typehash_insert(type);
3379 if (newtype && result != type) {
3383 specifiers->type = result;
3388 static type_qualifiers_t parse_type_qualifiers(void)
3390 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3393 switch(token.type) {
3394 /* type qualifiers */
3395 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3396 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3397 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3398 /* microsoft extended type modifiers */
3399 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3400 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3401 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3402 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3403 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3411 static declaration_t *parse_identifier_list(void)
3413 declaration_t *declarations = NULL;
3414 declaration_t *last_declaration = NULL;
3416 declaration_t *const declaration = allocate_declaration_zero();
3417 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3418 declaration->source_position = token.source_position;
3419 declaration->symbol = token.v.symbol;
3422 if (last_declaration != NULL) {
3423 last_declaration->next = declaration;
3425 declarations = declaration;
3427 last_declaration = declaration;
3429 if (token.type != ',') {
3433 } while (token.type == T_IDENTIFIER);
3435 return declarations;
3438 static type_t *automatic_type_conversion(type_t *orig_type);
3440 static void semantic_parameter(declaration_t *declaration)
3442 /* TODO: improve error messages */
3444 if (declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3445 errorf(HERE, "typedef not allowed in parameter list");
3446 } else if (declaration->declared_storage_class != STORAGE_CLASS_NONE
3447 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3448 errorf(HERE, "parameter may only have none or register storage class");
3451 type_t *const orig_type = declaration->type;
3452 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3453 * sugar. Turn it into a pointer.
3454 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3455 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3457 type_t *const type = automatic_type_conversion(orig_type);
3458 declaration->type = type;
3460 if (is_type_incomplete(skip_typeref(type))) {
3461 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3462 orig_type, declaration->symbol);
3466 static declaration_t *parse_parameter(void)
3468 declaration_specifiers_t specifiers;
3469 memset(&specifiers, 0, sizeof(specifiers));
3471 parse_declaration_specifiers(&specifiers);
3473 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3478 static declaration_t *parse_parameters(function_type_t *type)
3480 declaration_t *declarations = NULL;
3483 add_anchor_token(')');
3484 int saved_comma_state = save_and_reset_anchor_state(',');
3486 if (token.type == T_IDENTIFIER) {
3487 symbol_t *symbol = token.v.symbol;
3488 if (!is_typedef_symbol(symbol)) {
3489 type->kr_style_parameters = true;
3490 declarations = parse_identifier_list();
3491 goto parameters_finished;
3495 if (token.type == ')') {
3496 type->unspecified_parameters = 1;
3497 goto parameters_finished;
3500 declaration_t *declaration;
3501 declaration_t *last_declaration = NULL;
3502 function_parameter_t *parameter;
3503 function_parameter_t *last_parameter = NULL;
3506 switch(token.type) {
3510 goto parameters_finished;
3513 case T___extension__:
3515 declaration = parse_parameter();
3517 /* func(void) is not a parameter */
3518 if (last_parameter == NULL
3519 && token.type == ')'
3520 && declaration->symbol == NULL
3521 && skip_typeref(declaration->type) == type_void) {
3522 goto parameters_finished;
3524 semantic_parameter(declaration);
3526 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3527 memset(parameter, 0, sizeof(parameter[0]));
3528 parameter->type = declaration->type;
3530 if (last_parameter != NULL) {
3531 last_declaration->next = declaration;
3532 last_parameter->next = parameter;
3534 type->parameters = parameter;
3535 declarations = declaration;
3537 last_parameter = parameter;
3538 last_declaration = declaration;
3542 goto parameters_finished;
3544 if (token.type != ',') {
3545 goto parameters_finished;
3551 parameters_finished:
3552 rem_anchor_token(')');
3555 restore_anchor_state(',', saved_comma_state);
3556 return declarations;
3559 restore_anchor_state(',', saved_comma_state);
3563 typedef enum construct_type_kind_t {
3568 } construct_type_kind_t;
3570 typedef struct construct_type_t construct_type_t;
3571 struct construct_type_t {
3572 construct_type_kind_t kind;
3573 construct_type_t *next;
3576 typedef struct parsed_pointer_t parsed_pointer_t;
3577 struct parsed_pointer_t {
3578 construct_type_t construct_type;
3579 type_qualifiers_t type_qualifiers;
3582 typedef struct construct_function_type_t construct_function_type_t;
3583 struct construct_function_type_t {
3584 construct_type_t construct_type;
3585 type_t *function_type;
3588 typedef struct parsed_array_t parsed_array_t;
3589 struct parsed_array_t {
3590 construct_type_t construct_type;
3591 type_qualifiers_t type_qualifiers;
3597 typedef struct construct_base_type_t construct_base_type_t;
3598 struct construct_base_type_t {
3599 construct_type_t construct_type;
3603 static construct_type_t *parse_pointer_declarator(void)
3607 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3608 memset(pointer, 0, sizeof(pointer[0]));
3609 pointer->construct_type.kind = CONSTRUCT_POINTER;
3610 pointer->type_qualifiers = parse_type_qualifiers();
3612 return (construct_type_t*) pointer;
3615 static construct_type_t *parse_array_declarator(void)
3618 add_anchor_token(']');
3620 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3621 memset(array, 0, sizeof(array[0]));
3622 array->construct_type.kind = CONSTRUCT_ARRAY;
3624 if (token.type == T_static) {
3625 array->is_static = true;
3629 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3630 if (type_qualifiers != 0) {
3631 if (token.type == T_static) {
3632 array->is_static = true;
3636 array->type_qualifiers = type_qualifiers;
3638 if (token.type == '*' && look_ahead(1)->type == ']') {
3639 array->is_variable = true;
3641 } else if (token.type != ']') {
3642 array->size = parse_assignment_expression();
3645 rem_anchor_token(']');
3648 return (construct_type_t*) array;
3653 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3656 if (declaration != NULL) {
3657 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3659 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3661 if (mask & (mask-1)) {
3662 const char *first = NULL, *second = NULL;
3664 /* more than one calling convention set */
3665 if (declaration->modifiers & DM_CDECL) {
3666 if (first == NULL) first = "cdecl";
3667 else if (second == NULL) second = "cdecl";
3669 if (declaration->modifiers & DM_STDCALL) {
3670 if (first == NULL) first = "stdcall";
3671 else if (second == NULL) second = "stdcall";
3673 if (declaration->modifiers & DM_FASTCALL) {
3674 if (first == NULL) first = "faslcall";
3675 else if (second == NULL) second = "fastcall";
3677 if (declaration->modifiers & DM_THISCALL) {
3678 if (first == NULL) first = "thiscall";
3679 else if (second == NULL) second = "thiscall";
3681 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3684 if (declaration->modifiers & DM_CDECL)
3685 type->function.calling_convention = CC_CDECL;
3686 else if (declaration->modifiers & DM_STDCALL)
3687 type->function.calling_convention = CC_STDCALL;
3688 else if (declaration->modifiers & DM_FASTCALL)
3689 type->function.calling_convention = CC_FASTCALL;
3690 else if (declaration->modifiers & DM_THISCALL)
3691 type->function.calling_convention = CC_THISCALL;
3693 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3696 declaration_t *parameters = parse_parameters(&type->function);
3697 if (declaration != NULL) {
3698 declaration->scope.declarations = parameters;
3701 construct_function_type_t *construct_function_type =
3702 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3703 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3704 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3705 construct_function_type->function_type = type;
3707 return &construct_function_type->construct_type;
3710 static void fix_declaration_type(declaration_t *declaration)
3712 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3713 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3715 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3716 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3718 if (declaration->type->base.modifiers == type_modifiers)
3721 type_t *copy = duplicate_type(declaration->type);
3722 copy->base.modifiers = type_modifiers;
3724 type_t *result = typehash_insert(copy);
3725 if (result != copy) {
3726 obstack_free(type_obst, copy);
3729 declaration->type = result;
3732 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3733 bool may_be_abstract)
3735 /* construct a single linked list of construct_type_t's which describe
3736 * how to construct the final declarator type */
3737 construct_type_t *first = NULL;
3738 construct_type_t *last = NULL;
3739 gnu_attribute_t *attributes = NULL;
3741 decl_modifiers_t modifiers = parse_attributes(&attributes);
3744 while (token.type == '*') {
3745 construct_type_t *type = parse_pointer_declarator();
3755 /* TODO: find out if this is correct */
3756 modifiers |= parse_attributes(&attributes);
3759 construct_type_t *inner_types = NULL;
3761 switch(token.type) {
3763 if (declaration == NULL) {
3764 errorf(HERE, "no identifier expected in typename");
3766 declaration->symbol = token.v.symbol;
3767 declaration->source_position = token.source_position;
3773 add_anchor_token(')');
3774 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3775 rem_anchor_token(')');
3779 if (may_be_abstract)
3781 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3782 /* avoid a loop in the outermost scope, because eat_statement doesn't
3784 if (token.type == '}' && current_function == NULL) {
3792 construct_type_t *p = last;
3795 construct_type_t *type;
3796 switch(token.type) {
3798 type = parse_function_declarator(declaration);
3801 type = parse_array_declarator();
3804 goto declarator_finished;
3807 /* insert in the middle of the list (behind p) */
3809 type->next = p->next;
3820 declarator_finished:
3821 /* append inner_types at the end of the list, we don't to set last anymore
3822 * as it's not needed anymore */
3824 assert(first == NULL);
3825 first = inner_types;
3827 last->next = inner_types;
3835 static void parse_declaration_attributes(declaration_t *declaration)
3837 gnu_attribute_t *attributes = NULL;
3838 decl_modifiers_t modifiers = parse_attributes(&attributes);
3840 if (declaration == NULL)
3843 declaration->modifiers |= modifiers;
3844 /* check if we have these stupid mode attributes... */
3845 type_t *old_type = declaration->type;
3846 if (old_type == NULL)
3849 gnu_attribute_t *attribute = attributes;
3850 for ( ; attribute != NULL; attribute = attribute->next) {
3851 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3854 atomic_type_kind_t akind = attribute->u.akind;
3855 if (!is_type_signed(old_type)) {
3857 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3858 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3859 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3860 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3862 panic("invalid akind in mode attribute");
3866 = make_atomic_type(akind, old_type->base.qualifiers);
3870 static type_t *construct_declarator_type(construct_type_t *construct_list,
3873 construct_type_t *iter = construct_list;
3874 for( ; iter != NULL; iter = iter->next) {
3875 switch(iter->kind) {
3876 case CONSTRUCT_INVALID:
3877 internal_errorf(HERE, "invalid type construction found");
3878 case CONSTRUCT_FUNCTION: {
3879 construct_function_type_t *construct_function_type
3880 = (construct_function_type_t*) iter;
3882 type_t *function_type = construct_function_type->function_type;
3884 function_type->function.return_type = type;
3886 type_t *skipped_return_type = skip_typeref(type);
3887 if (is_type_function(skipped_return_type)) {
3888 errorf(HERE, "function returning function is not allowed");
3889 type = type_error_type;
3890 } else if (is_type_array(skipped_return_type)) {
3891 errorf(HERE, "function returning array is not allowed");
3892 type = type_error_type;
3894 type = function_type;
3899 case CONSTRUCT_POINTER: {
3900 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3901 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3902 pointer_type->pointer.points_to = type;
3903 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3905 type = pointer_type;
3909 case CONSTRUCT_ARRAY: {
3910 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3911 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3913 expression_t *size_expression = parsed_array->size;
3914 if (size_expression != NULL) {
3916 = create_implicit_cast(size_expression, type_size_t);
3919 array_type->base.qualifiers = parsed_array->type_qualifiers;
3920 array_type->array.element_type = type;
3921 array_type->array.is_static = parsed_array->is_static;
3922 array_type->array.is_variable = parsed_array->is_variable;
3923 array_type->array.size_expression = size_expression;
3925 if (size_expression != NULL) {
3926 if (is_constant_expression(size_expression)) {
3927 array_type->array.size_constant = true;
3928 array_type->array.size
3929 = fold_constant(size_expression);
3931 array_type->array.is_vla = true;
3935 type_t *skipped_type = skip_typeref(type);
3936 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3937 errorf(HERE, "array of void is not allowed");
3938 type = type_error_type;
3946 type_t *hashed_type = typehash_insert(type);
3947 if (hashed_type != type) {
3948 /* the function type was constructed earlier freeing it here will
3949 * destroy other types... */
3950 if (iter->kind != CONSTRUCT_FUNCTION) {
3960 static declaration_t *parse_declarator(
3961 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3963 declaration_t *const declaration = allocate_declaration_zero();
3964 declaration->declared_storage_class = specifiers->declared_storage_class;
3965 declaration->modifiers = specifiers->modifiers;
3966 declaration->deprecated = specifiers->deprecated;
3967 declaration->deprecated_string = specifiers->deprecated_string;
3968 declaration->get_property_sym = specifiers->get_property_sym;
3969 declaration->put_property_sym = specifiers->put_property_sym;
3970 declaration->is_inline = specifiers->is_inline;
3972 declaration->storage_class = specifiers->declared_storage_class;
3973 if (declaration->storage_class == STORAGE_CLASS_NONE
3974 && scope != global_scope) {
3975 declaration->storage_class = STORAGE_CLASS_AUTO;
3978 if (specifiers->alignment != 0) {
3979 /* TODO: add checks here */
3980 declaration->alignment = specifiers->alignment;
3983 construct_type_t *construct_type
3984 = parse_inner_declarator(declaration, may_be_abstract);
3985 type_t *const type = specifiers->type;
3986 declaration->type = construct_declarator_type(construct_type, type);
3988 parse_declaration_attributes(declaration);
3990 fix_declaration_type(declaration);
3992 if (construct_type != NULL) {
3993 obstack_free(&temp_obst, construct_type);
3999 static type_t *parse_abstract_declarator(type_t *base_type)
4001 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4003 type_t *result = construct_declarator_type(construct_type, base_type);
4004 if (construct_type != NULL) {
4005 obstack_free(&temp_obst, construct_type);
4011 static declaration_t *append_declaration(declaration_t* const declaration)
4013 if (last_declaration != NULL) {
4014 last_declaration->next = declaration;
4016 scope->declarations = declaration;
4018 last_declaration = declaration;
4023 * Check if the declaration of main is suspicious. main should be a
4024 * function with external linkage, returning int, taking either zero
4025 * arguments, two, or three arguments of appropriate types, ie.
4027 * int main([ int argc, char **argv [, char **env ] ]).
4029 * @param decl the declaration to check
4030 * @param type the function type of the declaration
4032 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4034 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4035 warningf(&decl->source_position,
4036 "'main' is normally a non-static function");
4038 if (skip_typeref(func_type->return_type) != type_int) {
4039 warningf(&decl->source_position,
4040 "return type of 'main' should be 'int', but is '%T'",
4041 func_type->return_type);
4043 const function_parameter_t *parm = func_type->parameters;
4045 type_t *const first_type = parm->type;
4046 if (!types_compatible(skip_typeref(first_type), type_int)) {
4047 warningf(&decl->source_position,
4048 "first argument of 'main' should be 'int', but is '%T'", first_type);
4052 type_t *const second_type = parm->type;
4053 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4054 warningf(&decl->source_position,
4055 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4059 type_t *const third_type = parm->type;
4060 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4061 warningf(&decl->source_position,
4062 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4066 goto warn_arg_count;
4070 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4076 * Check if a symbol is the equal to "main".
4078 static bool is_sym_main(const symbol_t *const sym)
4080 return strcmp(sym->string, "main") == 0;
4083 static declaration_t *internal_record_declaration(
4084 declaration_t *const declaration,
4085 const bool is_definition)
4087 const symbol_t *const symbol = declaration->symbol;
4088 const namespace_t namespc = (namespace_t)declaration->namespc;
4090 assert(declaration->symbol != NULL);
4091 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4093 type_t *const orig_type = declaration->type;
4094 type_t *const type = skip_typeref(orig_type);
4095 if (is_type_function(type) &&
4096 type->function.unspecified_parameters &&
4097 warning.strict_prototypes &&
4098 previous_declaration == NULL) {
4099 warningf(&declaration->source_position,
4100 "function declaration '%#T' is not a prototype",
4101 orig_type, declaration->symbol);
4104 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4105 check_type_of_main(declaration, &type->function);
4108 assert(declaration != previous_declaration);
4109 if (previous_declaration != NULL
4110 && previous_declaration->parent_scope == scope) {
4111 /* can happen for K&R style declarations */
4112 if (previous_declaration->type == NULL) {
4113 previous_declaration->type = declaration->type;
4116 const type_t *prev_type = skip_typeref(previous_declaration->type);
4117 if (!types_compatible(type, prev_type)) {
4118 errorf(&declaration->source_position,
4119 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4120 orig_type, symbol, previous_declaration->type, symbol,
4121 &previous_declaration->source_position);
4123 unsigned old_storage_class = previous_declaration->storage_class;
4124 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4125 errorf(&declaration->source_position,
4126 "redeclaration of enum entry '%Y' (declared %P)",
4127 symbol, &previous_declaration->source_position);
4128 return previous_declaration;
4131 if (warning.redundant_decls &&
4133 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4134 !(previous_declaration->modifiers & DM_USED) &&
4135 !previous_declaration->used) {
4136 warningf(&previous_declaration->source_position,
4137 "unnecessary static forward declaration for '%#T'",
4138 previous_declaration->type, symbol);
4141 unsigned new_storage_class = declaration->storage_class;
4143 if (is_type_incomplete(prev_type)) {
4144 previous_declaration->type = type;
4148 /* pretend no storage class means extern for function
4149 * declarations (except if the previous declaration is neither
4150 * none nor extern) */
4151 if (is_type_function(type)) {
4152 if (prev_type->function.unspecified_parameters) {
4153 previous_declaration->type = type;
4157 switch (old_storage_class) {
4158 case STORAGE_CLASS_NONE:
4159 old_storage_class = STORAGE_CLASS_EXTERN;
4162 case STORAGE_CLASS_EXTERN:
4163 if (is_definition) {
4164 if (warning.missing_prototypes &&
4165 prev_type->function.unspecified_parameters &&
4166 !is_sym_main(symbol)) {
4167 warningf(&declaration->source_position,
4168 "no previous prototype for '%#T'",
4171 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4172 new_storage_class = STORAGE_CLASS_EXTERN;
4181 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4182 new_storage_class == STORAGE_CLASS_EXTERN) {
4183 warn_redundant_declaration:
4184 if (!is_definition &&
4185 warning.redundant_decls &&
4186 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4187 warningf(&declaration->source_position,
4188 "redundant declaration for '%Y' (declared %P)",
4189 symbol, &previous_declaration->source_position);
4191 } else if (current_function == NULL) {
4192 if (old_storage_class != STORAGE_CLASS_STATIC &&
4193 new_storage_class == STORAGE_CLASS_STATIC) {
4194 errorf(&declaration->source_position,
4195 "static declaration of '%Y' follows non-static declaration (declared %P)",
4196 symbol, &previous_declaration->source_position);
4197 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4198 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4199 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4201 goto warn_redundant_declaration;
4203 } else if (old_storage_class == new_storage_class) {
4204 errorf(&declaration->source_position,
4205 "redeclaration of '%Y' (declared %P)",
4206 symbol, &previous_declaration->source_position);
4208 errorf(&declaration->source_position,
4209 "redeclaration of '%Y' with different linkage (declared %P)",
4210 symbol, &previous_declaration->source_position);
4214 if (declaration->is_inline)
4215 previous_declaration->is_inline = true;
4216 return previous_declaration;
4217 } else if (is_type_function(type)) {
4218 if (is_definition &&
4219 declaration->storage_class != STORAGE_CLASS_STATIC) {
4220 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4221 warningf(&declaration->source_position,
4222 "no previous prototype for '%#T'", orig_type, symbol);
4223 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4224 warningf(&declaration->source_position,
4225 "no previous declaration for '%#T'", orig_type,
4230 if (warning.missing_declarations &&
4231 scope == global_scope && (
4232 declaration->storage_class == STORAGE_CLASS_NONE ||
4233 declaration->storage_class == STORAGE_CLASS_THREAD
4235 warningf(&declaration->source_position,
4236 "no previous declaration for '%#T'", orig_type, symbol);
4240 assert(declaration->parent_scope == NULL);
4241 assert(scope != NULL);
4243 declaration->parent_scope = scope;
4245 environment_push(declaration);
4246 return append_declaration(declaration);
4249 static declaration_t *record_declaration(declaration_t *declaration)
4251 return internal_record_declaration(declaration, false);
4254 static declaration_t *record_definition(declaration_t *declaration)
4256 return internal_record_declaration(declaration, true);
4259 static void parser_error_multiple_definition(declaration_t *declaration,
4260 const source_position_t *source_position)
4262 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4263 declaration->symbol, &declaration->source_position);
4266 static bool is_declaration_specifier(const token_t *token,
4267 bool only_specifiers_qualifiers)
4269 switch(token->type) {
4274 return is_typedef_symbol(token->v.symbol);
4276 case T___extension__:
4278 return !only_specifiers_qualifiers;
4285 static void parse_init_declarator_rest(declaration_t *declaration)
4289 type_t *orig_type = declaration->type;
4290 type_t *type = skip_typeref(orig_type);
4292 if (declaration->init.initializer != NULL) {
4293 parser_error_multiple_definition(declaration, HERE);
4296 bool must_be_constant = false;
4297 if (declaration->storage_class == STORAGE_CLASS_STATIC
4298 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4299 || declaration->parent_scope == global_scope) {
4300 must_be_constant = true;
4303 parse_initializer_env_t env;
4304 env.type = orig_type;
4305 env.must_be_constant = must_be_constant;
4306 env.declaration = declaration;
4308 initializer_t *initializer = parse_initializer(&env);
4310 if (env.type != orig_type) {
4311 orig_type = env.type;
4312 type = skip_typeref(orig_type);
4313 declaration->type = env.type;
4316 if (is_type_function(type)) {
4317 errorf(&declaration->source_position,
4318 "initializers not allowed for function types at declator '%Y' (type '%T')",
4319 declaration->symbol, orig_type);
4321 declaration->init.initializer = initializer;
4325 /* parse rest of a declaration without any declarator */
4326 static void parse_anonymous_declaration_rest(
4327 const declaration_specifiers_t *specifiers,
4328 parsed_declaration_func finished_declaration)
4332 declaration_t *const declaration = allocate_declaration_zero();
4333 declaration->type = specifiers->type;
4334 declaration->declared_storage_class = specifiers->declared_storage_class;
4335 declaration->source_position = specifiers->source_position;
4336 declaration->modifiers = specifiers->modifiers;
4338 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4339 warningf(&declaration->source_position,
4340 "useless storage class in empty declaration");
4342 declaration->storage_class = STORAGE_CLASS_NONE;
4344 type_t *type = declaration->type;
4345 switch (type->kind) {
4346 case TYPE_COMPOUND_STRUCT:
4347 case TYPE_COMPOUND_UNION: {
4348 if (type->compound.declaration->symbol == NULL) {
4349 warningf(&declaration->source_position,
4350 "unnamed struct/union that defines no instances");
4359 warningf(&declaration->source_position, "empty declaration");
4363 finished_declaration(declaration);
4366 static void parse_declaration_rest(declaration_t *ndeclaration,
4367 const declaration_specifiers_t *specifiers,
4368 parsed_declaration_func finished_declaration)
4370 add_anchor_token(';');
4371 add_anchor_token('=');
4372 add_anchor_token(',');
4374 declaration_t *declaration = finished_declaration(ndeclaration);
4376 type_t *orig_type = declaration->type;
4377 type_t *type = skip_typeref(orig_type);
4379 if (type->kind != TYPE_FUNCTION &&
4380 declaration->is_inline &&
4381 is_type_valid(type)) {
4382 warningf(&declaration->source_position,
4383 "variable '%Y' declared 'inline'\n", declaration->symbol);
4386 if (token.type == '=') {
4387 parse_init_declarator_rest(declaration);
4390 if (token.type != ',')
4394 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4399 rem_anchor_token(';');
4400 rem_anchor_token('=');
4401 rem_anchor_token(',');
4404 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4406 symbol_t *symbol = declaration->symbol;
4407 if (symbol == NULL) {
4408 errorf(HERE, "anonymous declaration not valid as function parameter");
4411 namespace_t namespc = (namespace_t) declaration->namespc;
4412 if (namespc != NAMESPACE_NORMAL) {
4413 return record_declaration(declaration);
4416 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4417 if (previous_declaration == NULL ||
4418 previous_declaration->parent_scope != scope) {
4419 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4424 if (previous_declaration->type == NULL) {
4425 previous_declaration->type = declaration->type;
4426 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4427 previous_declaration->storage_class = declaration->storage_class;
4428 previous_declaration->parent_scope = scope;
4429 return previous_declaration;
4431 return record_declaration(declaration);
4435 static void parse_declaration(parsed_declaration_func finished_declaration)
4437 declaration_specifiers_t specifiers;
4438 memset(&specifiers, 0, sizeof(specifiers));
4439 parse_declaration_specifiers(&specifiers);
4441 if (token.type == ';') {
4442 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4444 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4445 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4449 static type_t *get_default_promoted_type(type_t *orig_type)
4451 type_t *result = orig_type;
4453 type_t *type = skip_typeref(orig_type);
4454 if (is_type_integer(type)) {
4455 result = promote_integer(type);
4456 } else if (type == type_float) {
4457 result = type_double;
4463 static void parse_kr_declaration_list(declaration_t *declaration)
4465 type_t *type = skip_typeref(declaration->type);
4466 if (!is_type_function(type))
4469 if (!type->function.kr_style_parameters)
4472 /* push function parameters */
4473 int top = environment_top();
4474 scope_t *last_scope = scope;
4475 set_scope(&declaration->scope);
4477 declaration_t *parameter = declaration->scope.declarations;
4478 for ( ; parameter != NULL; parameter = parameter->next) {
4479 assert(parameter->parent_scope == NULL);
4480 parameter->parent_scope = scope;
4481 environment_push(parameter);
4484 /* parse declaration list */
4485 while (is_declaration_specifier(&token, false)) {
4486 parse_declaration(finished_kr_declaration);
4489 /* pop function parameters */
4490 assert(scope == &declaration->scope);
4491 set_scope(last_scope);
4492 environment_pop_to(top);
4494 /* update function type */
4495 type_t *new_type = duplicate_type(type);
4497 function_parameter_t *parameters = NULL;
4498 function_parameter_t *last_parameter = NULL;
4500 declaration_t *parameter_declaration = declaration->scope.declarations;
4501 for( ; parameter_declaration != NULL;
4502 parameter_declaration = parameter_declaration->next) {
4503 type_t *parameter_type = parameter_declaration->type;
4504 if (parameter_type == NULL) {
4506 errorf(HERE, "no type specified for function parameter '%Y'",
4507 parameter_declaration->symbol);
4509 if (warning.implicit_int) {
4510 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4511 parameter_declaration->symbol);
4513 parameter_type = type_int;
4514 parameter_declaration->type = parameter_type;
4518 semantic_parameter(parameter_declaration);
4519 parameter_type = parameter_declaration->type;
4522 * we need the default promoted types for the function type
4524 parameter_type = get_default_promoted_type(parameter_type);
4526 function_parameter_t *function_parameter
4527 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4528 memset(function_parameter, 0, sizeof(function_parameter[0]));
4530 function_parameter->type = parameter_type;
4531 if (last_parameter != NULL) {
4532 last_parameter->next = function_parameter;
4534 parameters = function_parameter;
4536 last_parameter = function_parameter;
4539 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4541 new_type->function.parameters = parameters;
4542 new_type->function.unspecified_parameters = true;
4544 type = typehash_insert(new_type);
4545 if (type != new_type) {
4546 obstack_free(type_obst, new_type);
4549 declaration->type = type;
4552 static bool first_err = true;
4555 * When called with first_err set, prints the name of the current function,
4558 static void print_in_function(void)
4562 diagnosticf("%s: In function '%Y':\n",
4563 current_function->source_position.input_name,
4564 current_function->symbol);
4569 * Check if all labels are defined in the current function.
4570 * Check if all labels are used in the current function.
4572 static void check_labels(void)
4574 for (const goto_statement_t *goto_statement = goto_first;
4575 goto_statement != NULL;
4576 goto_statement = goto_statement->next) {
4577 declaration_t *label = goto_statement->label;
4580 if (label->source_position.input_name == NULL) {
4581 print_in_function();
4582 errorf(&goto_statement->base.source_position,
4583 "label '%Y' used but not defined", label->symbol);
4586 goto_first = goto_last = NULL;
4588 if (warning.unused_label) {
4589 for (const label_statement_t *label_statement = label_first;
4590 label_statement != NULL;
4591 label_statement = label_statement->next) {
4592 const declaration_t *label = label_statement->label;
4594 if (! label->used) {
4595 print_in_function();
4596 warningf(&label_statement->base.source_position,
4597 "label '%Y' defined but not used", label->symbol);
4601 label_first = label_last = NULL;
4605 * Check declarations of current_function for unused entities.
4607 static void check_declarations(void)
4609 if (warning.unused_parameter) {
4610 const scope_t *scope = ¤t_function->scope;
4612 const declaration_t *parameter = scope->declarations;
4613 for (; parameter != NULL; parameter = parameter->next) {
4614 if (! parameter->used) {
4615 print_in_function();
4616 warningf(¶meter->source_position,
4617 "unused parameter '%Y'", parameter->symbol);
4621 if (warning.unused_variable) {
4625 static void parse_external_declaration(void)
4627 /* function-definitions and declarations both start with declaration
4629 declaration_specifiers_t specifiers;
4630 memset(&specifiers, 0, sizeof(specifiers));
4632 add_anchor_token(';');
4633 parse_declaration_specifiers(&specifiers);
4634 rem_anchor_token(';');
4636 /* must be a declaration */
4637 if (token.type == ';') {
4638 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4642 add_anchor_token(',');
4643 add_anchor_token('=');
4644 rem_anchor_token(';');
4646 /* declarator is common to both function-definitions and declarations */
4647 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4649 rem_anchor_token(',');
4650 rem_anchor_token('=');
4651 rem_anchor_token(';');
4653 /* must be a declaration */
4654 switch (token.type) {
4657 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4661 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
4665 /* must be a function definition */
4666 parse_kr_declaration_list(ndeclaration);
4668 if (token.type != '{') {
4669 parse_error_expected("while parsing function definition", '{', NULL);
4670 eat_until_matching_token(';');
4674 type_t *type = ndeclaration->type;
4676 /* note that we don't skip typerefs: the standard doesn't allow them here
4677 * (so we can't use is_type_function here) */
4678 if (type->kind != TYPE_FUNCTION) {
4679 if (is_type_valid(type)) {
4680 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4681 type, ndeclaration->symbol);
4687 /* § 6.7.5.3 (14) a function definition with () means no
4688 * parameters (and not unspecified parameters) */
4689 if (type->function.unspecified_parameters
4690 && type->function.parameters == NULL
4691 && !type->function.kr_style_parameters) {
4692 type_t *duplicate = duplicate_type(type);
4693 duplicate->function.unspecified_parameters = false;
4695 type = typehash_insert(duplicate);
4696 if (type != duplicate) {
4697 obstack_free(type_obst, duplicate);
4699 ndeclaration->type = type;
4702 declaration_t *const declaration = record_definition(ndeclaration);
4703 if (ndeclaration != declaration) {
4704 declaration->scope = ndeclaration->scope;
4706 type = skip_typeref(declaration->type);
4708 /* push function parameters and switch scope */
4709 int top = environment_top();
4710 scope_t *last_scope = scope;
4711 set_scope(&declaration->scope);
4713 declaration_t *parameter = declaration->scope.declarations;
4714 for( ; parameter != NULL; parameter = parameter->next) {
4715 if (parameter->parent_scope == &ndeclaration->scope) {
4716 parameter->parent_scope = scope;
4718 assert(parameter->parent_scope == NULL
4719 || parameter->parent_scope == scope);
4720 parameter->parent_scope = scope;
4721 if (parameter->symbol == NULL) {
4722 errorf(&ndeclaration->source_position, "parameter name omitted");
4725 environment_push(parameter);
4728 if (declaration->init.statement != NULL) {
4729 parser_error_multiple_definition(declaration, HERE);
4732 /* parse function body */
4733 int label_stack_top = label_top();
4734 declaration_t *old_current_function = current_function;
4735 current_function = declaration;
4737 declaration->init.statement = parse_compound_statement(false);
4740 check_declarations();
4742 assert(current_function == declaration);
4743 current_function = old_current_function;
4744 label_pop_to(label_stack_top);
4747 assert(scope == &declaration->scope);
4748 set_scope(last_scope);
4749 environment_pop_to(top);
4752 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4753 source_position_t *source_position)
4755 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4757 type->bitfield.base_type = base_type;
4758 type->bitfield.size = size;
4763 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4766 declaration_t *iter = compound_declaration->scope.declarations;
4767 for( ; iter != NULL; iter = iter->next) {
4768 if (iter->namespc != NAMESPACE_NORMAL)
4771 if (iter->symbol == NULL) {
4772 type_t *type = skip_typeref(iter->type);
4773 if (is_type_compound(type)) {
4774 declaration_t *result
4775 = find_compound_entry(type->compound.declaration, symbol);
4782 if (iter->symbol == symbol) {
4790 static void parse_compound_declarators(declaration_t *struct_declaration,
4791 const declaration_specifiers_t *specifiers)
4793 declaration_t *last_declaration = struct_declaration->scope.declarations;
4794 if (last_declaration != NULL) {
4795 while(last_declaration->next != NULL) {
4796 last_declaration = last_declaration->next;
4801 declaration_t *declaration;
4803 if (token.type == ':') {
4804 source_position_t source_position = *HERE;
4807 type_t *base_type = specifiers->type;
4808 expression_t *size = parse_constant_expression();
4810 if (!is_type_integer(skip_typeref(base_type))) {
4811 errorf(HERE, "bitfield base type '%T' is not an integer type",
4815 type_t *type = make_bitfield_type(base_type, size, &source_position);
4817 declaration = allocate_declaration_zero();
4818 declaration->namespc = NAMESPACE_NORMAL;
4819 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4820 declaration->storage_class = STORAGE_CLASS_NONE;
4821 declaration->source_position = source_position;
4822 declaration->modifiers = specifiers->modifiers;
4823 declaration->type = type;
4825 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4827 type_t *orig_type = declaration->type;
4828 type_t *type = skip_typeref(orig_type);
4830 if (token.type == ':') {
4831 source_position_t source_position = *HERE;
4833 expression_t *size = parse_constant_expression();
4835 if (!is_type_integer(type)) {
4836 errorf(HERE, "bitfield base type '%T' is not an "
4837 "integer type", orig_type);
4840 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4841 declaration->type = bitfield_type;
4843 /* TODO we ignore arrays for now... what is missing is a check
4844 * that they're at the end of the struct */
4845 if (is_type_incomplete(type) && !is_type_array(type)) {
4847 "compound member '%Y' has incomplete type '%T'",
4848 declaration->symbol, orig_type);
4849 } else if (is_type_function(type)) {
4850 errorf(HERE, "compound member '%Y' must not have function "
4851 "type '%T'", declaration->symbol, orig_type);
4856 /* make sure we don't define a symbol multiple times */
4857 symbol_t *symbol = declaration->symbol;
4858 if (symbol != NULL) {
4859 declaration_t *prev_decl
4860 = find_compound_entry(struct_declaration, symbol);
4862 if (prev_decl != NULL) {
4863 assert(prev_decl->symbol == symbol);
4864 errorf(&declaration->source_position,
4865 "multiple declarations of symbol '%Y' (declared %P)",
4866 symbol, &prev_decl->source_position);
4870 /* append declaration */
4871 if (last_declaration != NULL) {
4872 last_declaration->next = declaration;
4874 struct_declaration->scope.declarations = declaration;
4876 last_declaration = declaration;
4878 if (token.type != ',')
4888 static void parse_compound_type_entries(declaration_t *compound_declaration)
4891 add_anchor_token('}');
4893 while(token.type != '}' && token.type != T_EOF) {
4894 declaration_specifiers_t specifiers;
4895 memset(&specifiers, 0, sizeof(specifiers));
4896 parse_declaration_specifiers(&specifiers);
4898 parse_compound_declarators(compound_declaration, &specifiers);
4900 rem_anchor_token('}');
4902 if (token.type == T_EOF) {
4903 errorf(HERE, "EOF while parsing struct");
4908 static type_t *parse_typename(void)
4910 declaration_specifiers_t specifiers;
4911 memset(&specifiers, 0, sizeof(specifiers));
4912 parse_declaration_specifiers(&specifiers);
4913 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4914 /* TODO: improve error message, user does probably not know what a
4915 * storage class is...
4917 errorf(HERE, "typename may not have a storage class");
4920 type_t *result = parse_abstract_declarator(specifiers.type);
4928 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4929 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4930 expression_t *left);
4932 typedef struct expression_parser_function_t expression_parser_function_t;
4933 struct expression_parser_function_t {
4934 unsigned precedence;
4935 parse_expression_function parser;
4936 unsigned infix_precedence;
4937 parse_expression_infix_function infix_parser;
4940 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4943 * Prints an error message if an expression was expected but not read
4945 static expression_t *expected_expression_error(void)
4947 /* skip the error message if the error token was read */
4948 if (token.type != T_ERROR) {
4949 errorf(HERE, "expected expression, got token '%K'", &token);
4953 return create_invalid_expression();
4957 * Parse a string constant.
4959 static expression_t *parse_string_const(void)
4962 if (token.type == T_STRING_LITERAL) {
4963 string_t res = token.v.string;
4965 while (token.type == T_STRING_LITERAL) {
4966 res = concat_strings(&res, &token.v.string);
4969 if (token.type != T_WIDE_STRING_LITERAL) {
4970 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4971 /* note: that we use type_char_ptr here, which is already the
4972 * automatic converted type. revert_automatic_type_conversion
4973 * will construct the array type */
4974 cnst->base.type = type_char_ptr;
4975 cnst->string.value = res;
4979 wres = concat_string_wide_string(&res, &token.v.wide_string);
4981 wres = token.v.wide_string;
4986 switch (token.type) {
4987 case T_WIDE_STRING_LITERAL:
4988 wres = concat_wide_strings(&wres, &token.v.wide_string);
4991 case T_STRING_LITERAL:
4992 wres = concat_wide_string_string(&wres, &token.v.string);
4996 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4997 cnst->base.type = type_wchar_t_ptr;
4998 cnst->wide_string.value = wres;
5007 * Parse an integer constant.
5009 static expression_t *parse_int_const(void)
5011 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5012 cnst->base.source_position = *HERE;
5013 cnst->base.type = token.datatype;
5014 cnst->conste.v.int_value = token.v.intvalue;
5022 * Parse a character constant.
5024 static expression_t *parse_character_constant(void)
5026 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5028 cnst->base.source_position = *HERE;
5029 cnst->base.type = token.datatype;
5030 cnst->conste.v.character = token.v.string;
5032 if (cnst->conste.v.character.size != 1) {
5033 if (warning.multichar && (c_mode & _GNUC)) {
5035 warningf(HERE, "multi-character character constant");
5037 errorf(HERE, "more than 1 characters in character constant");
5046 * Parse a wide character constant.
5048 static expression_t *parse_wide_character_constant(void)
5050 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5052 cnst->base.source_position = *HERE;
5053 cnst->base.type = token.datatype;
5054 cnst->conste.v.wide_character = token.v.wide_string;
5056 if (cnst->conste.v.wide_character.size != 1) {
5057 if (warning.multichar && (c_mode & _GNUC)) {
5059 warningf(HERE, "multi-character character constant");
5061 errorf(HERE, "more than 1 characters in character constant");
5070 * Parse a float constant.
5072 static expression_t *parse_float_const(void)
5074 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5075 cnst->base.type = token.datatype;
5076 cnst->conste.v.float_value = token.v.floatvalue;
5083 static declaration_t *create_implicit_function(symbol_t *symbol,
5084 const source_position_t *source_position)
5086 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5087 ntype->function.return_type = type_int;
5088 ntype->function.unspecified_parameters = true;
5090 type_t *type = typehash_insert(ntype);
5091 if (type != ntype) {
5095 declaration_t *const declaration = allocate_declaration_zero();
5096 declaration->storage_class = STORAGE_CLASS_EXTERN;
5097 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5098 declaration->type = type;
5099 declaration->symbol = symbol;
5100 declaration->source_position = *source_position;
5102 bool strict_prototypes_old = warning.strict_prototypes;
5103 warning.strict_prototypes = false;
5104 record_declaration(declaration);
5105 warning.strict_prototypes = strict_prototypes_old;
5111 * Creates a return_type (func)(argument_type) function type if not
5114 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5115 type_t *argument_type2)
5117 function_parameter_t *parameter2
5118 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5119 memset(parameter2, 0, sizeof(parameter2[0]));
5120 parameter2->type = argument_type2;
5122 function_parameter_t *parameter1
5123 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5124 memset(parameter1, 0, sizeof(parameter1[0]));
5125 parameter1->type = argument_type1;
5126 parameter1->next = parameter2;
5128 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5129 type->function.return_type = return_type;
5130 type->function.parameters = parameter1;
5132 type_t *result = typehash_insert(type);
5133 if (result != type) {
5141 * Creates a return_type (func)(argument_type) function type if not
5144 * @param return_type the return type
5145 * @param argument_type the argument type
5147 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5149 function_parameter_t *parameter
5150 = obstack_alloc(type_obst, sizeof(parameter[0]));
5151 memset(parameter, 0, sizeof(parameter[0]));
5152 parameter->type = argument_type;
5154 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5155 type->function.return_type = return_type;
5156 type->function.parameters = parameter;
5158 type_t *result = typehash_insert(type);
5159 if (result != type) {
5166 static type_t *make_function_0_type(type_t *return_type)
5168 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5169 type->function.return_type = return_type;
5170 type->function.parameters = NULL;
5172 type_t *result = typehash_insert(type);
5173 if (result != type) {
5181 * Creates a function type for some function like builtins.
5183 * @param symbol the symbol describing the builtin
5185 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5187 switch(symbol->ID) {
5188 case T___builtin_alloca:
5189 return make_function_1_type(type_void_ptr, type_size_t);
5190 case T___builtin_huge_val:
5191 return make_function_0_type(type_double);
5192 case T___builtin_nan:
5193 return make_function_1_type(type_double, type_char_ptr);
5194 case T___builtin_nanf:
5195 return make_function_1_type(type_float, type_char_ptr);
5196 case T___builtin_nand:
5197 return make_function_1_type(type_long_double, type_char_ptr);
5198 case T___builtin_va_end:
5199 return make_function_1_type(type_void, type_valist);
5200 case T___builtin_expect:
5201 return make_function_2_type(type_long, type_long, type_long);
5203 internal_errorf(HERE, "not implemented builtin symbol found");
5208 * Performs automatic type cast as described in § 6.3.2.1.
5210 * @param orig_type the original type
5212 static type_t *automatic_type_conversion(type_t *orig_type)
5214 type_t *type = skip_typeref(orig_type);
5215 if (is_type_array(type)) {
5216 array_type_t *array_type = &type->array;
5217 type_t *element_type = array_type->element_type;
5218 unsigned qualifiers = array_type->base.qualifiers;
5220 return make_pointer_type(element_type, qualifiers);
5223 if (is_type_function(type)) {
5224 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5231 * reverts the automatic casts of array to pointer types and function
5232 * to function-pointer types as defined § 6.3.2.1
5234 type_t *revert_automatic_type_conversion(const expression_t *expression)
5236 switch (expression->kind) {
5237 case EXPR_REFERENCE: return expression->reference.declaration->type;
5238 case EXPR_SELECT: return expression->select.compound_entry->type;
5240 case EXPR_UNARY_DEREFERENCE: {
5241 const expression_t *const value = expression->unary.value;
5242 type_t *const type = skip_typeref(value->base.type);
5243 assert(is_type_pointer(type));
5244 return type->pointer.points_to;
5247 case EXPR_BUILTIN_SYMBOL:
5248 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5250 case EXPR_ARRAY_ACCESS: {
5251 const expression_t *array_ref = expression->array_access.array_ref;
5252 type_t *type_left = skip_typeref(array_ref->base.type);
5253 if (!is_type_valid(type_left))
5255 assert(is_type_pointer(type_left));
5256 return type_left->pointer.points_to;
5259 case EXPR_STRING_LITERAL: {
5260 size_t size = expression->string.value.size;
5261 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5264 case EXPR_WIDE_STRING_LITERAL: {
5265 size_t size = expression->wide_string.value.size;
5266 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5269 case EXPR_COMPOUND_LITERAL:
5270 return expression->compound_literal.type;
5275 return expression->base.type;
5278 static expression_t *parse_reference(void)
5280 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5282 reference_expression_t *ref = &expression->reference;
5283 symbol_t *const symbol = token.v.symbol;
5285 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5287 source_position_t source_position = token.source_position;
5290 if (declaration == NULL) {
5291 if (! strict_mode && token.type == '(') {
5292 /* an implicitly defined function */
5293 if (warning.implicit_function_declaration) {
5294 warningf(HERE, "implicit declaration of function '%Y'",
5298 declaration = create_implicit_function(symbol,
5301 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5302 return create_invalid_expression();
5306 type_t *type = declaration->type;
5308 /* we always do the auto-type conversions; the & and sizeof parser contains
5309 * code to revert this! */
5310 type = automatic_type_conversion(type);
5312 ref->declaration = declaration;
5313 ref->base.type = type;
5315 /* this declaration is used */
5316 declaration->used = true;
5318 /* check for deprecated functions */
5319 if (declaration->deprecated != 0) {
5320 const char *prefix = "";
5321 if (is_type_function(declaration->type))
5322 prefix = "function ";
5324 if (declaration->deprecated_string != NULL) {
5325 warningf(&source_position,
5326 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
5327 declaration->deprecated_string);
5329 warningf(&source_position,
5330 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
5337 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
5341 /* TODO check if explicit cast is allowed and issue warnings/errors */
5344 static expression_t *parse_compound_literal(type_t *type)
5346 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5348 parse_initializer_env_t env;
5350 env.declaration = NULL;
5351 env.must_be_constant = false;
5352 initializer_t *initializer = parse_initializer(&env);
5355 expression->compound_literal.initializer = initializer;
5356 expression->compound_literal.type = type;
5357 expression->base.type = automatic_type_conversion(type);
5363 * Parse a cast expression.
5365 static expression_t *parse_cast(void)
5367 source_position_t source_position = token.source_position;
5369 type_t *type = parse_typename();
5371 /* matching add_anchor_token() is at call site */
5372 rem_anchor_token(')');
5375 if (token.type == '{') {
5376 return parse_compound_literal(type);
5379 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5380 cast->base.source_position = source_position;
5382 expression_t *value = parse_sub_expression(20);
5384 check_cast_allowed(value, type);
5386 cast->base.type = type;
5387 cast->unary.value = value;
5391 return create_invalid_expression();
5395 * Parse a statement expression.
5397 static expression_t *parse_statement_expression(void)
5399 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5401 statement_t *statement = parse_compound_statement(true);
5402 expression->statement.statement = statement;
5403 expression->base.source_position = statement->base.source_position;
5405 /* find last statement and use its type */
5406 type_t *type = type_void;
5407 const statement_t *stmt = statement->compound.statements;
5409 while (stmt->base.next != NULL)
5410 stmt = stmt->base.next;
5412 if (stmt->kind == STATEMENT_EXPRESSION) {
5413 type = stmt->expression.expression->base.type;
5416 warningf(&expression->base.source_position, "empty statement expression ({})");
5418 expression->base.type = type;
5424 return create_invalid_expression();
5428 * Parse a braced expression.
5430 static expression_t *parse_brace_expression(void)
5433 add_anchor_token(')');
5435 switch(token.type) {
5437 /* gcc extension: a statement expression */
5438 return parse_statement_expression();
5442 return parse_cast();
5444 if (is_typedef_symbol(token.v.symbol)) {
5445 return parse_cast();
5449 expression_t *result = parse_expression();
5450 rem_anchor_token(')');
5455 return create_invalid_expression();
5458 static expression_t *parse_function_keyword(void)
5463 if (current_function == NULL) {
5464 errorf(HERE, "'__func__' used outside of a function");
5467 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5468 expression->base.type = type_char_ptr;
5469 expression->funcname.kind = FUNCNAME_FUNCTION;
5474 static expression_t *parse_pretty_function_keyword(void)
5476 eat(T___PRETTY_FUNCTION__);
5478 if (current_function == NULL) {
5479 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5482 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5483 expression->base.type = type_char_ptr;
5484 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5489 static expression_t *parse_funcsig_keyword(void)
5493 if (current_function == NULL) {
5494 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5497 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5498 expression->base.type = type_char_ptr;
5499 expression->funcname.kind = FUNCNAME_FUNCSIG;
5504 static expression_t *parse_funcdname_keyword(void)
5506 eat(T___FUNCDNAME__);
5508 if (current_function == NULL) {
5509 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5512 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5513 expression->base.type = type_char_ptr;
5514 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5519 static designator_t *parse_designator(void)
5521 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5522 result->source_position = *HERE;
5524 if (token.type != T_IDENTIFIER) {
5525 parse_error_expected("while parsing member designator",
5526 T_IDENTIFIER, NULL);
5529 result->symbol = token.v.symbol;
5532 designator_t *last_designator = result;
5534 if (token.type == '.') {
5536 if (token.type != T_IDENTIFIER) {
5537 parse_error_expected("while parsing member designator",
5538 T_IDENTIFIER, NULL);
5541 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5542 designator->source_position = *HERE;
5543 designator->symbol = token.v.symbol;
5546 last_designator->next = designator;
5547 last_designator = designator;
5550 if (token.type == '[') {
5552 add_anchor_token(']');
5553 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5554 designator->source_position = *HERE;
5555 designator->array_index = parse_expression();
5556 rem_anchor_token(']');
5558 if (designator->array_index == NULL) {
5562 last_designator->next = designator;
5563 last_designator = designator;
5575 * Parse the __builtin_offsetof() expression.
5577 static expression_t *parse_offsetof(void)
5579 eat(T___builtin_offsetof);
5581 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5582 expression->base.type = type_size_t;
5585 add_anchor_token(',');
5586 type_t *type = parse_typename();
5587 rem_anchor_token(',');
5589 add_anchor_token(')');
5590 designator_t *designator = parse_designator();
5591 rem_anchor_token(')');
5594 expression->offsetofe.type = type;
5595 expression->offsetofe.designator = designator;
5598 memset(&path, 0, sizeof(path));
5599 path.top_type = type;
5600 path.path = NEW_ARR_F(type_path_entry_t, 0);
5602 descend_into_subtype(&path);
5604 if (!walk_designator(&path, designator, true)) {
5605 return create_invalid_expression();
5608 DEL_ARR_F(path.path);
5612 return create_invalid_expression();
5616 * Parses a _builtin_va_start() expression.
5618 static expression_t *parse_va_start(void)
5620 eat(T___builtin_va_start);
5622 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5625 add_anchor_token(',');
5626 expression->va_starte.ap = parse_assignment_expression();
5627 rem_anchor_token(',');
5629 expression_t *const expr = parse_assignment_expression();
5630 if (expr->kind == EXPR_REFERENCE) {
5631 declaration_t *const decl = expr->reference.declaration;
5633 return create_invalid_expression();
5634 if (decl->parent_scope == ¤t_function->scope &&
5635 decl->next == NULL) {
5636 expression->va_starte.parameter = decl;
5641 errorf(&expr->base.source_position,
5642 "second argument of 'va_start' must be last parameter of the current function");
5644 return create_invalid_expression();
5648 * Parses a _builtin_va_arg() expression.
5650 static expression_t *parse_va_arg(void)
5652 eat(T___builtin_va_arg);
5654 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5657 expression->va_arge.ap = parse_assignment_expression();
5659 expression->base.type = parse_typename();
5664 return create_invalid_expression();
5667 static expression_t *parse_builtin_symbol(void)
5669 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5671 symbol_t *symbol = token.v.symbol;
5673 expression->builtin_symbol.symbol = symbol;
5676 type_t *type = get_builtin_symbol_type(symbol);
5677 type = automatic_type_conversion(type);
5679 expression->base.type = type;
5684 * Parses a __builtin_constant() expression.
5686 static expression_t *parse_builtin_constant(void)
5688 eat(T___builtin_constant_p);
5690 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5693 add_anchor_token(')');
5694 expression->builtin_constant.value = parse_assignment_expression();
5695 rem_anchor_token(')');
5697 expression->base.type = type_int;
5701 return create_invalid_expression();
5705 * Parses a __builtin_prefetch() expression.
5707 static expression_t *parse_builtin_prefetch(void)
5709 eat(T___builtin_prefetch);
5711 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5714 add_anchor_token(')');
5715 expression->builtin_prefetch.adr = parse_assignment_expression();
5716 if (token.type == ',') {
5718 expression->builtin_prefetch.rw = parse_assignment_expression();
5720 if (token.type == ',') {
5722 expression->builtin_prefetch.locality = parse_assignment_expression();
5724 rem_anchor_token(')');
5726 expression->base.type = type_void;
5730 return create_invalid_expression();
5734 * Parses a __builtin_is_*() compare expression.
5736 static expression_t *parse_compare_builtin(void)
5738 expression_t *expression;
5740 switch(token.type) {
5741 case T___builtin_isgreater:
5742 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5744 case T___builtin_isgreaterequal:
5745 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5747 case T___builtin_isless:
5748 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5750 case T___builtin_islessequal:
5751 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5753 case T___builtin_islessgreater:
5754 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5756 case T___builtin_isunordered:
5757 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5760 internal_errorf(HERE, "invalid compare builtin found");
5763 expression->base.source_position = *HERE;
5767 expression->binary.left = parse_assignment_expression();
5769 expression->binary.right = parse_assignment_expression();
5772 type_t *const orig_type_left = expression->binary.left->base.type;
5773 type_t *const orig_type_right = expression->binary.right->base.type;
5775 type_t *const type_left = skip_typeref(orig_type_left);
5776 type_t *const type_right = skip_typeref(orig_type_right);
5777 if (!is_type_float(type_left) && !is_type_float(type_right)) {
5778 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5779 type_error_incompatible("invalid operands in comparison",
5780 &expression->base.source_position, orig_type_left, orig_type_right);
5783 semantic_comparison(&expression->binary);
5788 return create_invalid_expression();
5793 * Parses a __builtin_expect() expression.
5795 static expression_t *parse_builtin_expect(void)
5797 eat(T___builtin_expect);
5799 expression_t *expression
5800 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5803 expression->binary.left = parse_assignment_expression();
5805 expression->binary.right = parse_constant_expression();
5808 expression->base.type = expression->binary.left->base.type;
5812 return create_invalid_expression();
5817 * Parses a MS assume() expression.
5819 static expression_t *parse_assume(void)
5823 expression_t *expression
5824 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5827 add_anchor_token(')');
5828 expression->unary.value = parse_assignment_expression();
5829 rem_anchor_token(')');
5832 expression->base.type = type_void;
5835 return create_invalid_expression();
5839 * Parse a microsoft __noop expression.
5841 static expression_t *parse_noop_expression(void)
5843 source_position_t source_position = *HERE;
5846 if (token.type == '(') {
5847 /* parse arguments */
5849 add_anchor_token(')');
5850 add_anchor_token(',');
5852 if (token.type != ')') {
5854 (void)parse_assignment_expression();
5855 if (token.type != ',')
5861 rem_anchor_token(',');
5862 rem_anchor_token(')');
5865 /* the result is a (int)0 */
5866 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5867 cnst->base.source_position = source_position;
5868 cnst->base.type = type_int;
5869 cnst->conste.v.int_value = 0;
5870 cnst->conste.is_ms_noop = true;
5875 return create_invalid_expression();
5879 * Parses a primary expression.
5881 static expression_t *parse_primary_expression(void)
5883 switch (token.type) {
5884 case T_INTEGER: return parse_int_const();
5885 case T_CHARACTER_CONSTANT: return parse_character_constant();
5886 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5887 case T_FLOATINGPOINT: return parse_float_const();
5888 case T_STRING_LITERAL:
5889 case T_WIDE_STRING_LITERAL: return parse_string_const();
5890 case T_IDENTIFIER: return parse_reference();
5891 case T___FUNCTION__:
5892 case T___func__: return parse_function_keyword();
5893 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5894 case T___FUNCSIG__: return parse_funcsig_keyword();
5895 case T___FUNCDNAME__: return parse_funcdname_keyword();
5896 case T___builtin_offsetof: return parse_offsetof();
5897 case T___builtin_va_start: return parse_va_start();
5898 case T___builtin_va_arg: return parse_va_arg();
5899 case T___builtin_expect:
5900 case T___builtin_alloca:
5901 case T___builtin_nan:
5902 case T___builtin_nand:
5903 case T___builtin_nanf:
5904 case T___builtin_huge_val:
5905 case T___builtin_va_end: return parse_builtin_symbol();
5906 case T___builtin_isgreater:
5907 case T___builtin_isgreaterequal:
5908 case T___builtin_isless:
5909 case T___builtin_islessequal:
5910 case T___builtin_islessgreater:
5911 case T___builtin_isunordered: return parse_compare_builtin();
5912 case T___builtin_constant_p: return parse_builtin_constant();
5913 case T___builtin_prefetch: return parse_builtin_prefetch();
5914 case T__assume: return parse_assume();
5916 case '(': return parse_brace_expression();
5917 case T___noop: return parse_noop_expression();
5920 errorf(HERE, "unexpected token %K, expected an expression", &token);
5921 return create_invalid_expression();
5925 * Check if the expression has the character type and issue a warning then.
5927 static void check_for_char_index_type(const expression_t *expression)
5929 type_t *const type = expression->base.type;
5930 const type_t *const base_type = skip_typeref(type);
5932 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5933 warning.char_subscripts) {
5934 warningf(&expression->base.source_position,
5935 "array subscript has type '%T'", type);
5939 static expression_t *parse_array_expression(unsigned precedence,
5945 add_anchor_token(']');
5947 expression_t *inside = parse_expression();
5949 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5951 array_access_expression_t *array_access = &expression->array_access;
5953 type_t *const orig_type_left = left->base.type;
5954 type_t *const orig_type_inside = inside->base.type;
5956 type_t *const type_left = skip_typeref(orig_type_left);
5957 type_t *const type_inside = skip_typeref(orig_type_inside);
5959 type_t *return_type;
5960 if (is_type_pointer(type_left)) {
5961 return_type = type_left->pointer.points_to;
5962 array_access->array_ref = left;
5963 array_access->index = inside;
5964 check_for_char_index_type(inside);
5965 } else if (is_type_pointer(type_inside)) {
5966 return_type = type_inside->pointer.points_to;
5967 array_access->array_ref = inside;
5968 array_access->index = left;
5969 array_access->flipped = true;
5970 check_for_char_index_type(left);
5972 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5974 "array access on object with non-pointer types '%T', '%T'",
5975 orig_type_left, orig_type_inside);
5977 return_type = type_error_type;
5978 array_access->array_ref = create_invalid_expression();
5981 rem_anchor_token(']');
5982 if (token.type != ']') {
5983 parse_error_expected("Problem while parsing array access", ']', NULL);
5988 return_type = automatic_type_conversion(return_type);
5989 expression->base.type = return_type;
5994 static expression_t *parse_typeprop(expression_kind_t const kind,
5995 source_position_t const pos,
5996 unsigned const precedence)
5998 expression_t *tp_expression = allocate_expression_zero(kind);
5999 tp_expression->base.type = type_size_t;
6000 tp_expression->base.source_position = pos;
6002 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
6004 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
6006 add_anchor_token(')');
6007 type_t* const orig_type = parse_typename();
6008 tp_expression->typeprop.type = orig_type;
6010 type_t const* const type = skip_typeref(orig_type);
6011 char const* const wrong_type =
6012 is_type_incomplete(type) ? "incomplete" :
6013 type->kind == TYPE_FUNCTION ? "function designator" :
6014 type->kind == TYPE_BITFIELD ? "bitfield" :
6016 if (wrong_type != NULL) {
6017 errorf(&pos, "operand of %s expression must not be %s type '%T'",
6018 what, wrong_type, type);
6021 rem_anchor_token(')');
6024 expression_t *expression = parse_sub_expression(precedence);
6026 type_t* const orig_type = revert_automatic_type_conversion(expression);
6027 expression->base.type = orig_type;
6029 type_t const* const type = skip_typeref(orig_type);
6030 char const* const wrong_type =
6031 is_type_incomplete(type) ? "incomplete" :
6032 type->kind == TYPE_FUNCTION ? "function designator" :
6033 type->kind == TYPE_BITFIELD ? "bitfield" :
6035 if (wrong_type != NULL) {
6036 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6039 tp_expression->typeprop.type = expression->base.type;
6040 tp_expression->typeprop.tp_expression = expression;
6043 return tp_expression;
6045 return create_invalid_expression();
6048 static expression_t *parse_sizeof(unsigned precedence)
6050 source_position_t pos = *HERE;
6052 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6055 static expression_t *parse_alignof(unsigned precedence)
6057 source_position_t pos = *HERE;
6059 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6062 static expression_t *parse_select_expression(unsigned precedence,
6063 expression_t *compound)
6066 assert(token.type == '.' || token.type == T_MINUSGREATER);
6068 bool is_pointer = (token.type == T_MINUSGREATER);
6071 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6072 select->select.compound = compound;
6074 if (token.type != T_IDENTIFIER) {
6075 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6078 symbol_t *symbol = token.v.symbol;
6079 select->select.symbol = symbol;
6082 type_t *const orig_type = compound->base.type;
6083 type_t *const type = skip_typeref(orig_type);
6085 type_t *type_left = type;
6087 if (!is_type_pointer(type)) {
6088 if (is_type_valid(type)) {
6089 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6091 return create_invalid_expression();
6093 type_left = type->pointer.points_to;
6095 type_left = skip_typeref(type_left);
6097 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6098 type_left->kind != TYPE_COMPOUND_UNION) {
6099 if (is_type_valid(type_left)) {
6100 errorf(HERE, "request for member '%Y' in something not a struct or "
6101 "union, but '%T'", symbol, type_left);
6103 return create_invalid_expression();
6106 declaration_t *const declaration = type_left->compound.declaration;
6108 if (!declaration->init.complete) {
6109 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6111 return create_invalid_expression();
6114 declaration_t *iter = find_compound_entry(declaration, symbol);
6116 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6117 return create_invalid_expression();
6120 /* we always do the auto-type conversions; the & and sizeof parser contains
6121 * code to revert this! */
6122 type_t *expression_type = automatic_type_conversion(iter->type);
6124 select->select.compound_entry = iter;
6125 select->base.type = expression_type;
6127 type_t *skipped = skip_typeref(iter->type);
6128 if (skipped->kind == TYPE_BITFIELD) {
6129 select->base.type = skipped->bitfield.base_type;
6135 static void check_call_argument(const function_parameter_t *parameter,
6136 call_argument_t *argument)
6138 type_t *expected_type = parameter->type;
6139 type_t *expected_type_skip = skip_typeref(expected_type);
6140 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6141 expression_t *arg_expr = argument->expression;
6143 /* handle transparent union gnu extension */
6144 if (is_type_union(expected_type_skip)
6145 && (expected_type_skip->base.modifiers
6146 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6147 declaration_t *union_decl = expected_type_skip->compound.declaration;
6149 declaration_t *declaration = union_decl->scope.declarations;
6150 type_t *best_type = NULL;
6151 for ( ; declaration != NULL; declaration = declaration->next) {
6152 type_t *decl_type = declaration->type;
6153 error = semantic_assign(decl_type, arg_expr);
6154 if (error == ASSIGN_ERROR_INCOMPATIBLE
6155 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6158 if (error == ASSIGN_SUCCESS) {
6159 best_type = decl_type;
6160 } else if (best_type == NULL) {
6161 best_type = decl_type;
6165 if (best_type != NULL) {
6166 expected_type = best_type;
6170 error = semantic_assign(expected_type, arg_expr);
6171 argument->expression = create_implicit_cast(argument->expression,
6174 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6175 report_assign_error(error, expected_type, arg_expr, "function call",
6176 &arg_expr->base.source_position);
6180 * Parse a call expression, ie. expression '( ... )'.
6182 * @param expression the function address
6184 static expression_t *parse_call_expression(unsigned precedence,
6185 expression_t *expression)
6188 expression_t *result = allocate_expression_zero(EXPR_CALL);
6189 result->base.source_position = expression->base.source_position;
6191 call_expression_t *call = &result->call;
6192 call->function = expression;
6194 type_t *const orig_type = expression->base.type;
6195 type_t *const type = skip_typeref(orig_type);
6197 function_type_t *function_type = NULL;
6198 if (is_type_pointer(type)) {
6199 type_t *const to_type = skip_typeref(type->pointer.points_to);
6201 if (is_type_function(to_type)) {
6202 function_type = &to_type->function;
6203 call->base.type = function_type->return_type;
6207 if (function_type == NULL && is_type_valid(type)) {
6208 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6211 /* parse arguments */
6213 add_anchor_token(')');
6214 add_anchor_token(',');
6216 if (token.type != ')') {
6217 call_argument_t *last_argument = NULL;
6220 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6222 argument->expression = parse_assignment_expression();
6223 if (last_argument == NULL) {
6224 call->arguments = argument;
6226 last_argument->next = argument;
6228 last_argument = argument;
6230 if (token.type != ',')
6235 rem_anchor_token(',');
6236 rem_anchor_token(')');
6239 if (function_type == NULL)
6242 function_parameter_t *parameter = function_type->parameters;
6243 call_argument_t *argument = call->arguments;
6244 if (!function_type->unspecified_parameters) {
6245 for( ; parameter != NULL && argument != NULL;
6246 parameter = parameter->next, argument = argument->next) {
6247 check_call_argument(parameter, argument);
6250 if (parameter != NULL) {
6251 errorf(HERE, "too few arguments to function '%E'", expression);
6252 } else if (argument != NULL && !function_type->variadic) {
6253 errorf(HERE, "too many arguments to function '%E'", expression);
6257 /* do default promotion */
6258 for( ; argument != NULL; argument = argument->next) {
6259 type_t *type = argument->expression->base.type;
6261 type = get_default_promoted_type(type);
6263 argument->expression
6264 = create_implicit_cast(argument->expression, type);
6267 check_format(&result->call);
6271 return create_invalid_expression();
6274 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6276 static bool same_compound_type(const type_t *type1, const type_t *type2)
6279 is_type_compound(type1) &&
6280 type1->kind == type2->kind &&
6281 type1->compound.declaration == type2->compound.declaration;
6285 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6287 * @param expression the conditional expression
6289 static expression_t *parse_conditional_expression(unsigned precedence,
6290 expression_t *expression)
6293 add_anchor_token(':');
6295 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6297 conditional_expression_t *conditional = &result->conditional;
6298 conditional->condition = expression;
6301 type_t *const condition_type_orig = expression->base.type;
6302 type_t *const condition_type = skip_typeref(condition_type_orig);
6303 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6304 type_error("expected a scalar type in conditional condition",
6305 &expression->base.source_position, condition_type_orig);
6308 expression_t *true_expression = parse_expression();
6309 rem_anchor_token(':');
6311 expression_t *false_expression = parse_sub_expression(precedence);
6313 type_t *const orig_true_type = true_expression->base.type;
6314 type_t *const orig_false_type = false_expression->base.type;
6315 type_t *const true_type = skip_typeref(orig_true_type);
6316 type_t *const false_type = skip_typeref(orig_false_type);
6319 type_t *result_type;
6320 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6321 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6322 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6323 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6324 warningf(&expression->base.source_position,
6325 "ISO C forbids conditional expression with only one void side");
6327 result_type = type_void;
6328 } else if (is_type_arithmetic(true_type)
6329 && is_type_arithmetic(false_type)) {
6330 result_type = semantic_arithmetic(true_type, false_type);
6332 true_expression = create_implicit_cast(true_expression, result_type);
6333 false_expression = create_implicit_cast(false_expression, result_type);
6335 conditional->true_expression = true_expression;
6336 conditional->false_expression = false_expression;
6337 conditional->base.type = result_type;
6338 } else if (same_compound_type(true_type, false_type)) {
6339 /* just take 1 of the 2 types */
6340 result_type = true_type;
6341 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6342 type_t *pointer_type;
6344 expression_t *other_expression;
6345 if (is_type_pointer(true_type) &&
6346 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6347 pointer_type = true_type;
6348 other_type = false_type;
6349 other_expression = false_expression;
6351 pointer_type = false_type;
6352 other_type = true_type;
6353 other_expression = true_expression;
6356 if (is_null_pointer_constant(other_expression)) {
6357 result_type = pointer_type;
6358 } else if (is_type_pointer(other_type)) {
6359 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
6360 type_t *to2 = skip_typeref(other_type->pointer.points_to);
6363 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
6364 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
6366 } else if (types_compatible(get_unqualified_type(to1),
6367 get_unqualified_type(to2))) {
6370 warningf(&expression->base.source_position,
6371 "pointer types '%T' and '%T' in conditional expression are incompatible",
6372 true_type, false_type);
6376 type_t *const copy = duplicate_type(to);
6377 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
6379 type_t *const type = typehash_insert(copy);
6383 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
6384 } else if (is_type_integer(other_type)) {
6385 warningf(&expression->base.source_position,
6386 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
6387 result_type = pointer_type;
6389 type_error_incompatible("while parsing conditional",
6390 &expression->base.source_position, true_type, false_type);
6391 result_type = type_error_type;
6394 /* TODO: one pointer to void*, other some pointer */
6396 if (is_type_valid(true_type) && is_type_valid(false_type)) {
6397 type_error_incompatible("while parsing conditional",
6398 &expression->base.source_position, true_type,
6401 result_type = type_error_type;
6404 conditional->true_expression
6405 = create_implicit_cast(true_expression, result_type);
6406 conditional->false_expression
6407 = create_implicit_cast(false_expression, result_type);
6408 conditional->base.type = result_type;
6411 return create_invalid_expression();
6415 * Parse an extension expression.
6417 static expression_t *parse_extension(unsigned precedence)
6419 eat(T___extension__);
6421 /* TODO enable extensions */
6422 expression_t *expression = parse_sub_expression(precedence);
6423 /* TODO disable extensions */
6428 * Parse a __builtin_classify_type() expression.
6430 static expression_t *parse_builtin_classify_type(const unsigned precedence)
6432 eat(T___builtin_classify_type);
6434 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
6435 result->base.type = type_int;
6438 add_anchor_token(')');
6439 expression_t *expression = parse_sub_expression(precedence);
6440 rem_anchor_token(')');
6442 result->classify_type.type_expression = expression;
6446 return create_invalid_expression();
6449 static void check_pointer_arithmetic(const source_position_t *source_position,
6450 type_t *pointer_type,
6451 type_t *orig_pointer_type)
6453 type_t *points_to = pointer_type->pointer.points_to;
6454 points_to = skip_typeref(points_to);
6456 if (is_type_incomplete(points_to) &&
6458 || !is_type_atomic(points_to, ATOMIC_TYPE_VOID))) {
6459 errorf(source_position,
6460 "arithmetic with pointer to incomplete type '%T' not allowed",
6462 } else if (is_type_function(points_to)) {
6463 errorf(source_position,
6464 "arithmetic with pointer to function type '%T' not allowed",
6469 static void semantic_incdec(unary_expression_t *expression)
6471 type_t *const orig_type = expression->value->base.type;
6472 type_t *const type = skip_typeref(orig_type);
6473 if (is_type_pointer(type)) {
6474 check_pointer_arithmetic(&expression->base.source_position,
6476 } else if (!is_type_real(type) && is_type_valid(type)) {
6477 /* TODO: improve error message */
6478 errorf(HERE, "operation needs an arithmetic or pointer type");
6480 expression->base.type = orig_type;
6483 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6485 type_t *const orig_type = expression->value->base.type;
6486 type_t *const type = skip_typeref(orig_type);
6487 if (!is_type_arithmetic(type)) {
6488 if (is_type_valid(type)) {
6489 /* TODO: improve error message */
6490 errorf(HERE, "operation needs an arithmetic type");
6495 expression->base.type = orig_type;
6498 static void semantic_unexpr_scalar(unary_expression_t *expression)
6500 type_t *const orig_type = expression->value->base.type;
6501 type_t *const type = skip_typeref(orig_type);
6502 if (!is_type_scalar(type)) {
6503 if (is_type_valid(type)) {
6504 errorf(HERE, "operand of ! must be of scalar type");
6509 expression->base.type = orig_type;
6512 static void semantic_unexpr_integer(unary_expression_t *expression)
6514 type_t *const orig_type = expression->value->base.type;
6515 type_t *const type = skip_typeref(orig_type);
6516 if (!is_type_integer(type)) {
6517 if (is_type_valid(type)) {
6518 errorf(HERE, "operand of ~ must be of integer type");
6523 expression->base.type = orig_type;
6526 static void semantic_dereference(unary_expression_t *expression)
6528 type_t *const orig_type = expression->value->base.type;
6529 type_t *const type = skip_typeref(orig_type);
6530 if (!is_type_pointer(type)) {
6531 if (is_type_valid(type)) {
6532 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6537 type_t *result_type = type->pointer.points_to;
6538 result_type = automatic_type_conversion(result_type);
6539 expression->base.type = result_type;
6542 static void set_address_taken(expression_t *expression, bool may_be_register)
6544 if (expression->kind != EXPR_REFERENCE)
6547 declaration_t *const declaration = expression->reference.declaration;
6548 /* happens for parse errors */
6549 if (declaration == NULL)
6552 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
6553 errorf(&expression->base.source_position,
6554 "address of register variable '%Y' requested",
6555 declaration->symbol);
6557 declaration->address_taken = 1;
6562 * Check the semantic of the address taken expression.
6564 static void semantic_take_addr(unary_expression_t *expression)
6566 expression_t *value = expression->value;
6567 value->base.type = revert_automatic_type_conversion(value);
6569 type_t *orig_type = value->base.type;
6570 if (!is_type_valid(orig_type))
6573 set_address_taken(value, false);
6575 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6578 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6579 static expression_t *parse_##unexpression_type(unsigned precedence) \
6583 expression_t *unary_expression \
6584 = allocate_expression_zero(unexpression_type); \
6585 unary_expression->base.source_position = *HERE; \
6586 unary_expression->unary.value = parse_sub_expression(precedence); \
6588 sfunc(&unary_expression->unary); \
6590 return unary_expression; \
6593 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6594 semantic_unexpr_arithmetic)
6595 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6596 semantic_unexpr_arithmetic)
6597 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6598 semantic_unexpr_scalar)
6599 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6600 semantic_dereference)
6601 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6603 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6604 semantic_unexpr_integer)
6605 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6607 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6610 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6612 static expression_t *parse_##unexpression_type(unsigned precedence, \
6613 expression_t *left) \
6615 (void) precedence; \
6618 expression_t *unary_expression \
6619 = allocate_expression_zero(unexpression_type); \
6620 unary_expression->unary.value = left; \
6622 sfunc(&unary_expression->unary); \
6624 return unary_expression; \
6627 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6628 EXPR_UNARY_POSTFIX_INCREMENT,
6630 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6631 EXPR_UNARY_POSTFIX_DECREMENT,
6634 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6636 /* TODO: handle complex + imaginary types */
6638 /* § 6.3.1.8 Usual arithmetic conversions */
6639 if (type_left == type_long_double || type_right == type_long_double) {
6640 return type_long_double;
6641 } else if (type_left == type_double || type_right == type_double) {
6643 } else if (type_left == type_float || type_right == type_float) {
6647 type_left = promote_integer(type_left);
6648 type_right = promote_integer(type_right);
6650 if (type_left == type_right)
6653 bool const signed_left = is_type_signed(type_left);
6654 bool const signed_right = is_type_signed(type_right);
6655 int const rank_left = get_rank(type_left);
6656 int const rank_right = get_rank(type_right);
6658 if (signed_left == signed_right)
6659 return rank_left >= rank_right ? type_left : type_right;
6668 u_rank = rank_right;
6669 u_type = type_right;
6671 s_rank = rank_right;
6672 s_type = type_right;
6677 if (u_rank >= s_rank)
6680 if (get_atomic_type_size(s_rank) > get_atomic_type_size(u_rank))
6684 type_t *const type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
6686 case ATOMIC_TYPE_INT: type->atomic.akind = ATOMIC_TYPE_UINT; break;
6687 case ATOMIC_TYPE_LONG: type->atomic.akind = ATOMIC_TYPE_ULONG; break;
6688 case ATOMIC_TYPE_LONGLONG: type->atomic.akind = ATOMIC_TYPE_ULONGLONG; break;
6690 default: panic("invalid atomic type");
6693 type_t* const result = typehash_insert(type);
6701 * Check the semantic restrictions for a binary expression.
6703 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6705 expression_t *const left = expression->left;
6706 expression_t *const right = expression->right;
6707 type_t *const orig_type_left = left->base.type;
6708 type_t *const orig_type_right = right->base.type;
6709 type_t *const type_left = skip_typeref(orig_type_left);
6710 type_t *const type_right = skip_typeref(orig_type_right);
6712 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6713 /* TODO: improve error message */
6714 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6715 errorf(HERE, "operation needs arithmetic types");
6720 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6721 expression->left = create_implicit_cast(left, arithmetic_type);
6722 expression->right = create_implicit_cast(right, arithmetic_type);
6723 expression->base.type = arithmetic_type;
6726 static void semantic_shift_op(binary_expression_t *expression)
6728 expression_t *const left = expression->left;
6729 expression_t *const right = expression->right;
6730 type_t *const orig_type_left = left->base.type;
6731 type_t *const orig_type_right = right->base.type;
6732 type_t * type_left = skip_typeref(orig_type_left);
6733 type_t * type_right = skip_typeref(orig_type_right);
6735 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
6736 /* TODO: improve error message */
6737 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6738 errorf(HERE, "operation needs integer types");
6743 type_left = promote_integer(type_left);
6744 type_right = promote_integer(type_right);
6746 expression->left = create_implicit_cast(left, type_left);
6747 expression->right = create_implicit_cast(right, type_right);
6748 expression->base.type = type_left;
6751 static void semantic_add(binary_expression_t *expression)
6753 expression_t *const left = expression->left;
6754 expression_t *const right = expression->right;
6755 type_t *const orig_type_left = left->base.type;
6756 type_t *const orig_type_right = right->base.type;
6757 type_t *const type_left = skip_typeref(orig_type_left);
6758 type_t *const type_right = skip_typeref(orig_type_right);
6761 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6762 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6763 expression->left = create_implicit_cast(left, arithmetic_type);
6764 expression->right = create_implicit_cast(right, arithmetic_type);
6765 expression->base.type = arithmetic_type;
6767 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6768 check_pointer_arithmetic(&expression->base.source_position,
6769 type_left, orig_type_left);
6770 expression->base.type = type_left;
6771 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
6772 check_pointer_arithmetic(&expression->base.source_position,
6773 type_right, orig_type_right);
6774 expression->base.type = type_right;
6775 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6776 errorf(&expression->base.source_position,
6777 "invalid operands to binary + ('%T', '%T')",
6778 orig_type_left, orig_type_right);
6782 static void semantic_sub(binary_expression_t *expression)
6784 expression_t *const left = expression->left;
6785 expression_t *const right = expression->right;
6786 type_t *const orig_type_left = left->base.type;
6787 type_t *const orig_type_right = right->base.type;
6788 type_t *const type_left = skip_typeref(orig_type_left);
6789 type_t *const type_right = skip_typeref(orig_type_right);
6792 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6793 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6794 expression->left = create_implicit_cast(left, arithmetic_type);
6795 expression->right = create_implicit_cast(right, arithmetic_type);
6796 expression->base.type = arithmetic_type;
6798 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6799 check_pointer_arithmetic(&expression->base.source_position,
6800 type_left, orig_type_left);
6801 expression->base.type = type_left;
6802 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6803 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
6804 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
6805 if (!types_compatible(unqual_left, unqual_right)) {
6806 errorf(&expression->base.source_position,
6807 "subtracting pointers to incompatible types '%T' and '%T'",
6808 orig_type_left, orig_type_right);
6809 } else if (!is_type_object(unqual_left)) {
6810 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
6811 warningf(&expression->base.source_position,
6812 "subtracting pointers to void");
6814 errorf(&expression->base.source_position,
6815 "subtracting pointers to non-object types '%T'",
6819 expression->base.type = type_ptrdiff_t;
6820 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6821 errorf(HERE, "invalid operands of types '%T' and '%T' to binary '-'",
6822 orig_type_left, orig_type_right);
6827 * Check the semantics of comparison expressions.
6829 * @param expression The expression to check.
6831 static void semantic_comparison(binary_expression_t *expression)
6833 expression_t *left = expression->left;
6834 expression_t *right = expression->right;
6835 type_t *orig_type_left = left->base.type;
6836 type_t *orig_type_right = right->base.type;
6838 type_t *type_left = skip_typeref(orig_type_left);
6839 type_t *type_right = skip_typeref(orig_type_right);
6841 /* TODO non-arithmetic types */
6842 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6843 /* test for signed vs unsigned compares */
6844 if (warning.sign_compare &&
6845 (expression->base.kind != EXPR_BINARY_EQUAL &&
6846 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6847 (is_type_signed(type_left) != is_type_signed(type_right))) {
6849 /* check if 1 of the operands is a constant, in this case we just
6850 * check wether we can safely represent the resulting constant in
6851 * the type of the other operand. */
6852 expression_t *const_expr = NULL;
6853 expression_t *other_expr = NULL;
6855 if (is_constant_expression(left)) {
6858 } else if (is_constant_expression(right)) {
6863 if (const_expr != NULL) {
6864 type_t *other_type = skip_typeref(other_expr->base.type);
6865 long val = fold_constant(const_expr);
6866 /* TODO: check if val can be represented by other_type */
6870 warningf(&expression->base.source_position,
6871 "comparison between signed and unsigned");
6873 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6874 expression->left = create_implicit_cast(left, arithmetic_type);
6875 expression->right = create_implicit_cast(right, arithmetic_type);
6876 expression->base.type = arithmetic_type;
6877 if (warning.float_equal &&
6878 (expression->base.kind == EXPR_BINARY_EQUAL ||
6879 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6880 is_type_float(arithmetic_type)) {
6881 warningf(&expression->base.source_position,
6882 "comparing floating point with == or != is unsafe");
6884 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6885 /* TODO check compatibility */
6886 } else if (is_type_pointer(type_left)) {
6887 expression->right = create_implicit_cast(right, type_left);
6888 } else if (is_type_pointer(type_right)) {
6889 expression->left = create_implicit_cast(left, type_right);
6890 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6891 type_error_incompatible("invalid operands in comparison",
6892 &expression->base.source_position,
6893 type_left, type_right);
6895 expression->base.type = type_int;
6899 * Checks if a compound type has constant fields.
6901 static bool has_const_fields(const compound_type_t *type)
6903 const scope_t *scope = &type->declaration->scope;
6904 const declaration_t *declaration = scope->declarations;
6906 for (; declaration != NULL; declaration = declaration->next) {
6907 if (declaration->namespc != NAMESPACE_NORMAL)
6910 const type_t *decl_type = skip_typeref(declaration->type);
6911 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6918 static bool is_lvalue(const expression_t *expression)
6920 switch (expression->kind) {
6921 case EXPR_REFERENCE:
6922 case EXPR_ARRAY_ACCESS:
6924 case EXPR_UNARY_DEREFERENCE:
6932 static bool is_valid_assignment_lhs(expression_t const* const left)
6934 type_t *const orig_type_left = revert_automatic_type_conversion(left);
6935 type_t *const type_left = skip_typeref(orig_type_left);
6937 if (!is_lvalue(left)) {
6938 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
6943 if (is_type_array(type_left)) {
6944 errorf(HERE, "cannot assign to arrays ('%E')", left);
6947 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6948 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6952 if (is_type_incomplete(type_left)) {
6953 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
6954 left, orig_type_left);
6957 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6958 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6959 left, orig_type_left);
6966 static void semantic_arithmetic_assign(binary_expression_t *expression)
6968 expression_t *left = expression->left;
6969 expression_t *right = expression->right;
6970 type_t *orig_type_left = left->base.type;
6971 type_t *orig_type_right = right->base.type;
6973 if (!is_valid_assignment_lhs(left))
6976 type_t *type_left = skip_typeref(orig_type_left);
6977 type_t *type_right = skip_typeref(orig_type_right);
6979 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6980 /* TODO: improve error message */
6981 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6982 errorf(HERE, "operation needs arithmetic types");
6987 /* combined instructions are tricky. We can't create an implicit cast on
6988 * the left side, because we need the uncasted form for the store.
6989 * The ast2firm pass has to know that left_type must be right_type
6990 * for the arithmetic operation and create a cast by itself */
6991 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6992 expression->right = create_implicit_cast(right, arithmetic_type);
6993 expression->base.type = type_left;
6996 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6998 expression_t *const left = expression->left;
6999 expression_t *const right = expression->right;
7000 type_t *const orig_type_left = left->base.type;
7001 type_t *const orig_type_right = right->base.type;
7002 type_t *const type_left = skip_typeref(orig_type_left);
7003 type_t *const type_right = skip_typeref(orig_type_right);
7005 if (!is_valid_assignment_lhs(left))
7008 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7009 /* combined instructions are tricky. We can't create an implicit cast on
7010 * the left side, because we need the uncasted form for the store.
7011 * The ast2firm pass has to know that left_type must be right_type
7012 * for the arithmetic operation and create a cast by itself */
7013 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
7014 expression->right = create_implicit_cast(right, arithmetic_type);
7015 expression->base.type = type_left;
7016 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7017 check_pointer_arithmetic(&expression->base.source_position,
7018 type_left, orig_type_left);
7019 expression->base.type = type_left;
7020 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7021 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
7026 * Check the semantic restrictions of a logical expression.
7028 static void semantic_logical_op(binary_expression_t *expression)
7030 expression_t *const left = expression->left;
7031 expression_t *const right = expression->right;
7032 type_t *const orig_type_left = left->base.type;
7033 type_t *const orig_type_right = right->base.type;
7034 type_t *const type_left = skip_typeref(orig_type_left);
7035 type_t *const type_right = skip_typeref(orig_type_right);
7037 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7038 /* TODO: improve error message */
7039 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7040 errorf(HERE, "operation needs scalar types");
7045 expression->base.type = type_int;
7049 * Check the semantic restrictions of a binary assign expression.
7051 static void semantic_binexpr_assign(binary_expression_t *expression)
7053 expression_t *left = expression->left;
7054 type_t *orig_type_left = left->base.type;
7056 type_t *type_left = revert_automatic_type_conversion(left);
7057 type_left = skip_typeref(orig_type_left);
7059 if (!is_valid_assignment_lhs(left))
7062 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7063 report_assign_error(error, orig_type_left, expression->right,
7064 "assignment", &left->base.source_position);
7065 expression->right = create_implicit_cast(expression->right, orig_type_left);
7066 expression->base.type = orig_type_left;
7070 * Determine if the outermost operation (or parts thereof) of the given
7071 * expression has no effect in order to generate a warning about this fact.
7072 * Therefore in some cases this only examines some of the operands of the
7073 * expression (see comments in the function and examples below).
7075 * f() + 23; // warning, because + has no effect
7076 * x || f(); // no warning, because x controls execution of f()
7077 * x ? y : f(); // warning, because y has no effect
7078 * (void)x; // no warning to be able to suppress the warning
7079 * This function can NOT be used for an "expression has definitely no effect"-
7081 static bool expression_has_effect(const expression_t *const expr)
7083 switch (expr->kind) {
7084 case EXPR_UNKNOWN: break;
7085 case EXPR_INVALID: return true; /* do NOT warn */
7086 case EXPR_REFERENCE: return false;
7087 /* suppress the warning for microsoft __noop operations */
7088 case EXPR_CONST: return expr->conste.is_ms_noop;
7089 case EXPR_CHARACTER_CONSTANT: return false;
7090 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7091 case EXPR_STRING_LITERAL: return false;
7092 case EXPR_WIDE_STRING_LITERAL: return false;
7095 const call_expression_t *const call = &expr->call;
7096 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7099 switch (call->function->builtin_symbol.symbol->ID) {
7100 case T___builtin_va_end: return true;
7101 default: return false;
7105 /* Generate the warning if either the left or right hand side of a
7106 * conditional expression has no effect */
7107 case EXPR_CONDITIONAL: {
7108 const conditional_expression_t *const cond = &expr->conditional;
7110 expression_has_effect(cond->true_expression) &&
7111 expression_has_effect(cond->false_expression);
7114 case EXPR_SELECT: return false;
7115 case EXPR_ARRAY_ACCESS: return false;
7116 case EXPR_SIZEOF: return false;
7117 case EXPR_CLASSIFY_TYPE: return false;
7118 case EXPR_ALIGNOF: return false;
7120 case EXPR_FUNCNAME: return false;
7121 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7122 case EXPR_BUILTIN_CONSTANT_P: return false;
7123 case EXPR_BUILTIN_PREFETCH: return true;
7124 case EXPR_OFFSETOF: return false;
7125 case EXPR_VA_START: return true;
7126 case EXPR_VA_ARG: return true;
7127 case EXPR_STATEMENT: return true; // TODO
7128 case EXPR_COMPOUND_LITERAL: return false;
7130 case EXPR_UNARY_NEGATE: return false;
7131 case EXPR_UNARY_PLUS: return false;
7132 case EXPR_UNARY_BITWISE_NEGATE: return false;
7133 case EXPR_UNARY_NOT: return false;
7134 case EXPR_UNARY_DEREFERENCE: return false;
7135 case EXPR_UNARY_TAKE_ADDRESS: return false;
7136 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7137 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7138 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7139 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7141 /* Treat void casts as if they have an effect in order to being able to
7142 * suppress the warning */
7143 case EXPR_UNARY_CAST: {
7144 type_t *const type = skip_typeref(expr->base.type);
7145 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7148 case EXPR_UNARY_CAST_IMPLICIT: return true;
7149 case EXPR_UNARY_ASSUME: return true;
7151 case EXPR_BINARY_ADD: return false;
7152 case EXPR_BINARY_SUB: return false;
7153 case EXPR_BINARY_MUL: return false;
7154 case EXPR_BINARY_DIV: return false;
7155 case EXPR_BINARY_MOD: return false;
7156 case EXPR_BINARY_EQUAL: return false;
7157 case EXPR_BINARY_NOTEQUAL: return false;
7158 case EXPR_BINARY_LESS: return false;
7159 case EXPR_BINARY_LESSEQUAL: return false;
7160 case EXPR_BINARY_GREATER: return false;
7161 case EXPR_BINARY_GREATEREQUAL: return false;
7162 case EXPR_BINARY_BITWISE_AND: return false;
7163 case EXPR_BINARY_BITWISE_OR: return false;
7164 case EXPR_BINARY_BITWISE_XOR: return false;
7165 case EXPR_BINARY_SHIFTLEFT: return false;
7166 case EXPR_BINARY_SHIFTRIGHT: return false;
7167 case EXPR_BINARY_ASSIGN: return true;
7168 case EXPR_BINARY_MUL_ASSIGN: return true;
7169 case EXPR_BINARY_DIV_ASSIGN: return true;
7170 case EXPR_BINARY_MOD_ASSIGN: return true;
7171 case EXPR_BINARY_ADD_ASSIGN: return true;
7172 case EXPR_BINARY_SUB_ASSIGN: return true;
7173 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7174 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7175 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7176 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7177 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7179 /* Only examine the right hand side of && and ||, because the left hand
7180 * side already has the effect of controlling the execution of the right
7182 case EXPR_BINARY_LOGICAL_AND:
7183 case EXPR_BINARY_LOGICAL_OR:
7184 /* Only examine the right hand side of a comma expression, because the left
7185 * hand side has a separate warning */
7186 case EXPR_BINARY_COMMA:
7187 return expression_has_effect(expr->binary.right);
7189 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7190 case EXPR_BINARY_ISGREATER: return false;
7191 case EXPR_BINARY_ISGREATEREQUAL: return false;
7192 case EXPR_BINARY_ISLESS: return false;
7193 case EXPR_BINARY_ISLESSEQUAL: return false;
7194 case EXPR_BINARY_ISLESSGREATER: return false;
7195 case EXPR_BINARY_ISUNORDERED: return false;
7198 internal_errorf(HERE, "unexpected expression");
7201 static void semantic_comma(binary_expression_t *expression)
7203 if (warning.unused_value) {
7204 const expression_t *const left = expression->left;
7205 if (!expression_has_effect(left)) {
7206 warningf(&left->base.source_position,
7207 "left-hand operand of comma expression has no effect");
7210 expression->base.type = expression->right->base.type;
7213 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7214 static expression_t *parse_##binexpression_type(unsigned precedence, \
7215 expression_t *left) \
7218 source_position_t pos = *HERE; \
7220 expression_t *right = parse_sub_expression(precedence + lr); \
7222 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7223 binexpr->base.source_position = pos; \
7224 binexpr->binary.left = left; \
7225 binexpr->binary.right = right; \
7226 sfunc(&binexpr->binary); \
7231 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7232 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7233 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
7234 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
7235 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7236 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7237 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7238 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7239 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7241 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7242 semantic_comparison, 1)
7243 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7244 semantic_comparison, 1)
7245 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7246 semantic_comparison, 1)
7247 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7248 semantic_comparison, 1)
7250 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7251 semantic_binexpr_arithmetic, 1)
7252 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7253 semantic_binexpr_arithmetic, 1)
7254 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7255 semantic_binexpr_arithmetic, 1)
7256 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7257 semantic_logical_op, 1)
7258 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7259 semantic_logical_op, 1)
7260 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7261 semantic_shift_op, 1)
7262 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7263 semantic_shift_op, 1)
7264 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7265 semantic_arithmetic_addsubb_assign, 0)
7266 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7267 semantic_arithmetic_addsubb_assign, 0)
7268 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7269 semantic_arithmetic_assign, 0)
7270 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7271 semantic_arithmetic_assign, 0)
7272 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7273 semantic_arithmetic_assign, 0)
7274 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7275 semantic_arithmetic_assign, 0)
7276 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7277 semantic_arithmetic_assign, 0)
7278 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7279 semantic_arithmetic_assign, 0)
7280 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7281 semantic_arithmetic_assign, 0)
7282 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7283 semantic_arithmetic_assign, 0)
7285 static expression_t *parse_sub_expression(unsigned precedence)
7287 if (token.type < 0) {
7288 return expected_expression_error();
7291 expression_parser_function_t *parser
7292 = &expression_parsers[token.type];
7293 source_position_t source_position = token.source_position;
7296 if (parser->parser != NULL) {
7297 left = parser->parser(parser->precedence);
7299 left = parse_primary_expression();
7301 assert(left != NULL);
7302 left->base.source_position = source_position;
7305 if (token.type < 0) {
7306 return expected_expression_error();
7309 parser = &expression_parsers[token.type];
7310 if (parser->infix_parser == NULL)
7312 if (parser->infix_precedence < precedence)
7315 left = parser->infix_parser(parser->infix_precedence, left);
7317 assert(left != NULL);
7318 assert(left->kind != EXPR_UNKNOWN);
7319 left->base.source_position = source_position;
7326 * Parse an expression.
7328 static expression_t *parse_expression(void)
7330 return parse_sub_expression(1);
7334 * Register a parser for a prefix-like operator with given precedence.
7336 * @param parser the parser function
7337 * @param token_type the token type of the prefix token
7338 * @param precedence the precedence of the operator
7340 static void register_expression_parser(parse_expression_function parser,
7341 int token_type, unsigned precedence)
7343 expression_parser_function_t *entry = &expression_parsers[token_type];
7345 if (entry->parser != NULL) {
7346 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7347 panic("trying to register multiple expression parsers for a token");
7349 entry->parser = parser;
7350 entry->precedence = precedence;
7354 * Register a parser for an infix operator with given precedence.
7356 * @param parser the parser function
7357 * @param token_type the token type of the infix operator
7358 * @param precedence the precedence of the operator
7360 static void register_infix_parser(parse_expression_infix_function parser,
7361 int token_type, unsigned precedence)
7363 expression_parser_function_t *entry = &expression_parsers[token_type];
7365 if (entry->infix_parser != NULL) {
7366 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7367 panic("trying to register multiple infix expression parsers for a "
7370 entry->infix_parser = parser;
7371 entry->infix_precedence = precedence;
7375 * Initialize the expression parsers.
7377 static void init_expression_parsers(void)
7379 memset(&expression_parsers, 0, sizeof(expression_parsers));
7381 register_infix_parser(parse_array_expression, '[', 30);
7382 register_infix_parser(parse_call_expression, '(', 30);
7383 register_infix_parser(parse_select_expression, '.', 30);
7384 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
7385 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
7387 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
7390 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
7391 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
7392 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
7393 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
7394 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
7395 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
7396 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
7397 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
7398 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
7399 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
7400 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
7401 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
7402 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
7403 T_EXCLAMATIONMARKEQUAL, 13);
7404 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
7405 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
7406 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
7407 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
7408 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
7409 register_infix_parser(parse_conditional_expression, '?', 7);
7410 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
7411 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
7412 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
7413 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
7414 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
7415 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
7416 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
7417 T_LESSLESSEQUAL, 2);
7418 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7419 T_GREATERGREATEREQUAL, 2);
7420 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
7422 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
7424 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
7427 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
7429 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
7430 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
7431 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
7432 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
7433 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
7434 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
7435 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
7437 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
7439 register_expression_parser(parse_sizeof, T_sizeof, 25);
7440 register_expression_parser(parse_alignof, T___alignof__, 25);
7441 register_expression_parser(parse_extension, T___extension__, 25);
7442 register_expression_parser(parse_builtin_classify_type,
7443 T___builtin_classify_type, 25);
7447 * Parse a asm statement arguments specification.
7449 static asm_argument_t *parse_asm_arguments(bool is_out)
7451 asm_argument_t *result = NULL;
7452 asm_argument_t *last = NULL;
7454 while (token.type == T_STRING_LITERAL || token.type == '[') {
7455 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7456 memset(argument, 0, sizeof(argument[0]));
7458 if (token.type == '[') {
7460 if (token.type != T_IDENTIFIER) {
7461 parse_error_expected("while parsing asm argument",
7462 T_IDENTIFIER, NULL);
7465 argument->symbol = token.v.symbol;
7470 argument->constraints = parse_string_literals();
7472 add_anchor_token(')');
7473 expression_t *expression = parse_expression();
7474 rem_anchor_token(')');
7476 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
7477 * change size or type representation (e.g. int -> long is ok, but
7478 * int -> float is not) */
7479 if (expression->kind == EXPR_UNARY_CAST) {
7480 type_t *const type = expression->base.type;
7481 type_kind_t const kind = type->kind;
7482 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
7485 if (kind == TYPE_ATOMIC) {
7486 atomic_type_kind_t const akind = type->atomic.akind;
7487 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7488 size = get_atomic_type_size(akind);
7490 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7491 size = get_atomic_type_size(get_intptr_kind());
7495 expression_t *const value = expression->unary.value;
7496 type_t *const value_type = value->base.type;
7497 type_kind_t const value_kind = value_type->kind;
7499 unsigned value_flags;
7500 unsigned value_size;
7501 if (value_kind == TYPE_ATOMIC) {
7502 atomic_type_kind_t const value_akind = value_type->atomic.akind;
7503 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7504 value_size = get_atomic_type_size(value_akind);
7505 } else if (value_kind == TYPE_POINTER) {
7506 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7507 value_size = get_atomic_type_size(get_intptr_kind());
7512 if (value_flags != flags || value_size != size)
7516 } while (expression->kind == EXPR_UNARY_CAST);
7520 if (!is_lvalue(expression)) {
7521 errorf(&expression->base.source_position,
7522 "asm output argument is not an lvalue");
7525 argument->expression = expression;
7528 set_address_taken(expression, true);
7531 last->next = argument;
7537 if (token.type != ',')
7548 * Parse a asm statement clobber specification.
7550 static asm_clobber_t *parse_asm_clobbers(void)
7552 asm_clobber_t *result = NULL;
7553 asm_clobber_t *last = NULL;
7555 while(token.type == T_STRING_LITERAL) {
7556 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
7557 clobber->clobber = parse_string_literals();
7560 last->next = clobber;
7566 if (token.type != ',')
7575 * Parse an asm statement.
7577 static statement_t *parse_asm_statement(void)
7581 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
7582 statement->base.source_position = token.source_position;
7584 asm_statement_t *asm_statement = &statement->asms;
7586 if (token.type == T_volatile) {
7588 asm_statement->is_volatile = true;
7592 add_anchor_token(')');
7593 add_anchor_token(':');
7594 asm_statement->asm_text = parse_string_literals();
7596 if (token.type != ':') {
7597 rem_anchor_token(':');
7602 asm_statement->outputs = parse_asm_arguments(true);
7603 if (token.type != ':') {
7604 rem_anchor_token(':');
7609 asm_statement->inputs = parse_asm_arguments(false);
7610 if (token.type != ':') {
7611 rem_anchor_token(':');
7614 rem_anchor_token(':');
7617 asm_statement->clobbers = parse_asm_clobbers();
7620 rem_anchor_token(')');
7624 if (asm_statement->outputs == NULL) {
7625 /* GCC: An 'asm' instruction without any output operands will be treated
7626 * identically to a volatile 'asm' instruction. */
7627 asm_statement->is_volatile = true;
7632 return create_invalid_statement();
7636 * Parse a case statement.
7638 static statement_t *parse_case_statement(void)
7642 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7644 statement->base.source_position = token.source_position;
7645 statement->case_label.expression = parse_expression();
7647 if (c_mode & _GNUC) {
7648 if (token.type == T_DOTDOTDOT) {
7650 statement->case_label.end_range = parse_expression();
7656 if (! is_constant_expression(statement->case_label.expression)) {
7657 errorf(&statement->base.source_position,
7658 "case label does not reduce to an integer constant");
7660 /* TODO: check if the case label is already known */
7661 if (current_switch != NULL) {
7662 /* link all cases into the switch statement */
7663 if (current_switch->last_case == NULL) {
7664 current_switch->first_case =
7665 current_switch->last_case = &statement->case_label;
7667 current_switch->last_case->next = &statement->case_label;
7670 errorf(&statement->base.source_position,
7671 "case label not within a switch statement");
7674 statement->case_label.statement = parse_statement();
7678 return create_invalid_statement();
7682 * Finds an existing default label of a switch statement.
7684 static case_label_statement_t *
7685 find_default_label(const switch_statement_t *statement)
7687 case_label_statement_t *label = statement->first_case;
7688 for ( ; label != NULL; label = label->next) {
7689 if (label->expression == NULL)
7696 * Parse a default statement.
7698 static statement_t *parse_default_statement(void)
7702 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7704 statement->base.source_position = token.source_position;
7707 if (current_switch != NULL) {
7708 const case_label_statement_t *def_label = find_default_label(current_switch);
7709 if (def_label != NULL) {
7710 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7711 &def_label->base.source_position);
7713 /* link all cases into the switch statement */
7714 if (current_switch->last_case == NULL) {
7715 current_switch->first_case =
7716 current_switch->last_case = &statement->case_label;
7718 current_switch->last_case->next = &statement->case_label;
7722 errorf(&statement->base.source_position,
7723 "'default' label not within a switch statement");
7725 statement->case_label.statement = parse_statement();
7729 return create_invalid_statement();
7733 * Return the declaration for a given label symbol or create a new one.
7735 * @param symbol the symbol of the label
7737 static declaration_t *get_label(symbol_t *symbol)
7739 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7740 assert(current_function != NULL);
7741 /* if we found a label in the same function, then we already created the
7743 if (candidate != NULL
7744 && candidate->parent_scope == ¤t_function->scope) {
7748 /* otherwise we need to create a new one */
7749 declaration_t *const declaration = allocate_declaration_zero();
7750 declaration->namespc = NAMESPACE_LABEL;
7751 declaration->symbol = symbol;
7753 label_push(declaration);
7759 * Parse a label statement.
7761 static statement_t *parse_label_statement(void)
7763 assert(token.type == T_IDENTIFIER);
7764 symbol_t *symbol = token.v.symbol;
7767 declaration_t *label = get_label(symbol);
7769 /* if source position is already set then the label is defined twice,
7770 * otherwise it was just mentioned in a goto so far */
7771 if (label->source_position.input_name != NULL) {
7772 errorf(HERE, "duplicate label '%Y' (declared %P)",
7773 symbol, &label->source_position);
7775 label->source_position = token.source_position;
7778 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7780 statement->base.source_position = token.source_position;
7781 statement->label.label = label;
7785 if (token.type == '}') {
7786 /* TODO only warn? */
7788 warningf(HERE, "label at end of compound statement");
7789 statement->label.statement = create_empty_statement();
7791 errorf(HERE, "label at end of compound statement");
7792 statement->label.statement = create_invalid_statement();
7794 } else if (token.type == ';') {
7795 /* Eat an empty statement here, to avoid the warning about an empty
7796 * statement after a label. label:; is commonly used to have a label
7797 * before a closing brace. */
7798 statement->label.statement = create_empty_statement();
7801 statement->label.statement = parse_statement();
7804 /* remember the labels in a list for later checking */
7805 if (label_last == NULL) {
7806 label_first = &statement->label;
7808 label_last->next = &statement->label;
7810 label_last = &statement->label;
7816 * Parse an if statement.
7818 static statement_t *parse_if(void)
7822 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7823 statement->base.source_position = token.source_position;
7826 add_anchor_token(')');
7827 statement->ifs.condition = parse_expression();
7828 rem_anchor_token(')');
7831 add_anchor_token(T_else);
7832 statement->ifs.true_statement = parse_statement();
7833 rem_anchor_token(T_else);
7835 if (token.type == T_else) {
7837 statement->ifs.false_statement = parse_statement();
7842 return create_invalid_statement();
7846 * Parse a switch statement.
7848 static statement_t *parse_switch(void)
7852 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7853 statement->base.source_position = token.source_position;
7856 expression_t *const expr = parse_expression();
7857 type_t * type = skip_typeref(expr->base.type);
7858 if (is_type_integer(type)) {
7859 type = promote_integer(type);
7860 } else if (is_type_valid(type)) {
7861 errorf(&expr->base.source_position,
7862 "switch quantity is not an integer, but '%T'", type);
7863 type = type_error_type;
7865 statement->switchs.expression = create_implicit_cast(expr, type);
7868 switch_statement_t *rem = current_switch;
7869 current_switch = &statement->switchs;
7870 statement->switchs.body = parse_statement();
7871 current_switch = rem;
7873 if (warning.switch_default &&
7874 find_default_label(&statement->switchs) == NULL) {
7875 warningf(&statement->base.source_position, "switch has no default case");
7880 return create_invalid_statement();
7883 static statement_t *parse_loop_body(statement_t *const loop)
7885 statement_t *const rem = current_loop;
7886 current_loop = loop;
7888 statement_t *const body = parse_statement();
7895 * Parse a while statement.
7897 static statement_t *parse_while(void)
7901 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7902 statement->base.source_position = token.source_position;
7905 add_anchor_token(')');
7906 statement->whiles.condition = parse_expression();
7907 rem_anchor_token(')');
7910 statement->whiles.body = parse_loop_body(statement);
7914 return create_invalid_statement();
7918 * Parse a do statement.
7920 static statement_t *parse_do(void)
7924 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7926 statement->base.source_position = token.source_position;
7928 add_anchor_token(T_while);
7929 statement->do_while.body = parse_loop_body(statement);
7930 rem_anchor_token(T_while);
7934 add_anchor_token(')');
7935 statement->do_while.condition = parse_expression();
7936 rem_anchor_token(')');
7942 return create_invalid_statement();
7946 * Parse a for statement.
7948 static statement_t *parse_for(void)
7952 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7953 statement->base.source_position = token.source_position;
7955 int top = environment_top();
7956 scope_t *last_scope = scope;
7957 set_scope(&statement->fors.scope);
7960 add_anchor_token(')');
7962 if (token.type != ';') {
7963 if (is_declaration_specifier(&token, false)) {
7964 parse_declaration(record_declaration);
7966 add_anchor_token(';');
7967 expression_t *const init = parse_expression();
7968 statement->fors.initialisation = init;
7969 if (warning.unused_value && !expression_has_effect(init)) {
7970 warningf(&init->base.source_position,
7971 "initialisation of 'for'-statement has no effect");
7973 rem_anchor_token(';');
7980 if (token.type != ';') {
7981 add_anchor_token(';');
7982 statement->fors.condition = parse_expression();
7983 rem_anchor_token(';');
7986 if (token.type != ')') {
7987 expression_t *const step = parse_expression();
7988 statement->fors.step = step;
7989 if (warning.unused_value && !expression_has_effect(step)) {
7990 warningf(&step->base.source_position,
7991 "step of 'for'-statement has no effect");
7994 rem_anchor_token(')');
7996 statement->fors.body = parse_loop_body(statement);
7998 assert(scope == &statement->fors.scope);
7999 set_scope(last_scope);
8000 environment_pop_to(top);
8005 rem_anchor_token(')');
8006 assert(scope == &statement->fors.scope);
8007 set_scope(last_scope);
8008 environment_pop_to(top);
8010 return create_invalid_statement();
8014 * Parse a goto statement.
8016 static statement_t *parse_goto(void)
8020 if (token.type != T_IDENTIFIER) {
8021 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8025 symbol_t *symbol = token.v.symbol;
8028 declaration_t *label = get_label(symbol);
8030 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8031 statement->base.source_position = token.source_position;
8033 statement->gotos.label = label;
8035 /* remember the goto's in a list for later checking */
8036 if (goto_last == NULL) {
8037 goto_first = &statement->gotos;
8039 goto_last->next = &statement->gotos;
8041 goto_last = &statement->gotos;
8047 return create_invalid_statement();
8051 * Parse a continue statement.
8053 static statement_t *parse_continue(void)
8055 statement_t *statement;
8056 if (current_loop == NULL) {
8057 errorf(HERE, "continue statement not within loop");
8058 statement = create_invalid_statement();
8060 statement = allocate_statement_zero(STATEMENT_CONTINUE);
8062 statement->base.source_position = token.source_position;
8070 return create_invalid_statement();
8074 * Parse a break statement.
8076 static statement_t *parse_break(void)
8078 statement_t *statement;
8079 if (current_switch == NULL && current_loop == NULL) {
8080 errorf(HERE, "break statement not within loop or switch");
8081 statement = create_invalid_statement();
8083 statement = allocate_statement_zero(STATEMENT_BREAK);
8085 statement->base.source_position = token.source_position;
8093 return create_invalid_statement();
8097 * Parse a __leave statement.
8099 static statement_t *parse_leave(void)
8101 statement_t *statement;
8102 if (current_try == NULL) {
8103 errorf(HERE, "__leave statement not within __try");
8104 statement = create_invalid_statement();
8106 statement = allocate_statement_zero(STATEMENT_LEAVE);
8108 statement->base.source_position = token.source_position;
8116 return create_invalid_statement();
8120 * Check if a given declaration represents a local variable.
8122 static bool is_local_var_declaration(const declaration_t *declaration)
8124 switch ((storage_class_tag_t) declaration->storage_class) {
8125 case STORAGE_CLASS_AUTO:
8126 case STORAGE_CLASS_REGISTER: {
8127 const type_t *type = skip_typeref(declaration->type);
8128 if (is_type_function(type)) {
8140 * Check if a given declaration represents a variable.
8142 static bool is_var_declaration(const declaration_t *declaration)
8144 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8147 const type_t *type = skip_typeref(declaration->type);
8148 return !is_type_function(type);
8152 * Check if a given expression represents a local variable.
8154 static bool is_local_variable(const expression_t *expression)
8156 if (expression->base.kind != EXPR_REFERENCE) {
8159 const declaration_t *declaration = expression->reference.declaration;
8160 return is_local_var_declaration(declaration);
8164 * Check if a given expression represents a local variable and
8165 * return its declaration then, else return NULL.
8167 declaration_t *expr_is_variable(const expression_t *expression)
8169 if (expression->base.kind != EXPR_REFERENCE) {
8172 declaration_t *declaration = expression->reference.declaration;
8173 if (is_var_declaration(declaration))
8179 * Parse a return statement.
8181 static statement_t *parse_return(void)
8183 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8184 statement->base.source_position = token.source_position;
8188 expression_t *return_value = NULL;
8189 if (token.type != ';') {
8190 return_value = parse_expression();
8194 const type_t *const func_type = current_function->type;
8195 assert(is_type_function(func_type));
8196 type_t *const return_type = skip_typeref(func_type->function.return_type);
8198 if (return_value != NULL) {
8199 type_t *return_value_type = skip_typeref(return_value->base.type);
8201 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8202 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8203 warningf(&statement->base.source_position,
8204 "'return' with a value, in function returning void");
8205 return_value = NULL;
8207 assign_error_t error = semantic_assign(return_type, return_value);
8208 report_assign_error(error, return_type, return_value, "'return'",
8209 &statement->base.source_position);
8210 return_value = create_implicit_cast(return_value, return_type);
8212 /* check for returning address of a local var */
8213 if (return_value != NULL &&
8214 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
8215 const expression_t *expression = return_value->unary.value;
8216 if (is_local_variable(expression)) {
8217 warningf(&statement->base.source_position,
8218 "function returns address of local variable");
8222 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
8223 warningf(&statement->base.source_position,
8224 "'return' without value, in function returning non-void");
8227 statement->returns.value = return_value;
8231 return create_invalid_statement();
8235 * Parse a declaration statement.
8237 static statement_t *parse_declaration_statement(void)
8239 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
8241 statement->base.source_position = token.source_position;
8243 declaration_t *before = last_declaration;
8244 parse_declaration(record_declaration);
8246 if (before == NULL) {
8247 statement->declaration.declarations_begin = scope->declarations;
8249 statement->declaration.declarations_begin = before->next;
8251 statement->declaration.declarations_end = last_declaration;
8257 * Parse an expression statement, ie. expr ';'.
8259 static statement_t *parse_expression_statement(void)
8261 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
8263 statement->base.source_position = token.source_position;
8264 expression_t *const expr = parse_expression();
8265 statement->expression.expression = expr;
8271 return create_invalid_statement();
8275 * Parse a microsoft __try { } __finally { } or
8276 * __try{ } __except() { }
8278 static statement_t *parse_ms_try_statment(void)
8280 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
8282 statement->base.source_position = token.source_position;
8285 ms_try_statement_t *rem = current_try;
8286 current_try = &statement->ms_try;
8287 statement->ms_try.try_statement = parse_compound_statement(false);
8290 if (token.type == T___except) {
8293 add_anchor_token(')');
8294 expression_t *const expr = parse_expression();
8295 type_t * type = skip_typeref(expr->base.type);
8296 if (is_type_integer(type)) {
8297 type = promote_integer(type);
8298 } else if (is_type_valid(type)) {
8299 errorf(&expr->base.source_position,
8300 "__expect expression is not an integer, but '%T'", type);
8301 type = type_error_type;
8303 statement->ms_try.except_expression = create_implicit_cast(expr, type);
8304 rem_anchor_token(')');
8306 statement->ms_try.final_statement = parse_compound_statement(false);
8307 } else if (token.type == T__finally) {
8309 statement->ms_try.final_statement = parse_compound_statement(false);
8311 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
8312 return create_invalid_statement();
8316 return create_invalid_statement();
8319 static statement_t *parse_empty_statement(void)
8321 if (warning.empty_statement) {
8322 warningf(HERE, "statement is empty");
8325 return create_empty_statement();
8329 * Parse a statement.
8330 * There's also parse_statement() which additionally checks for
8331 * "statement has no effect" warnings
8333 static statement_t *intern_parse_statement(void)
8335 statement_t *statement = NULL;
8337 /* declaration or statement */
8338 add_anchor_token(';');
8339 switch (token.type) {
8341 if (look_ahead(1)->type == ':') {
8342 statement = parse_label_statement();
8343 } else if (is_typedef_symbol(token.v.symbol)) {
8344 statement = parse_declaration_statement();
8346 statement = parse_expression_statement();
8350 case T___extension__:
8351 /* This can be a prefix to a declaration or an expression statement.
8352 * We simply eat it now and parse the rest with tail recursion. */
8355 } while (token.type == T___extension__);
8356 statement = parse_statement();
8360 statement = parse_declaration_statement();
8363 case ';': statement = parse_empty_statement(); break;
8364 case '{': statement = parse_compound_statement(false); break;
8365 case T___leave: statement = parse_leave(); break;
8366 case T___try: statement = parse_ms_try_statment(); break;
8367 case T_asm: statement = parse_asm_statement(); break;
8368 case T_break: statement = parse_break(); break;
8369 case T_case: statement = parse_case_statement(); break;
8370 case T_continue: statement = parse_continue(); break;
8371 case T_default: statement = parse_default_statement(); break;
8372 case T_do: statement = parse_do(); break;
8373 case T_for: statement = parse_for(); break;
8374 case T_goto: statement = parse_goto(); break;
8375 case T_if: statement = parse_if (); break;
8376 case T_return: statement = parse_return(); break;
8377 case T_switch: statement = parse_switch(); break;
8378 case T_while: statement = parse_while(); break;
8379 default: statement = parse_expression_statement(); break;
8381 rem_anchor_token(';');
8383 assert(statement != NULL
8384 && statement->base.source_position.input_name != NULL);
8390 * parse a statement and emits "statement has no effect" warning if needed
8391 * (This is really a wrapper around intern_parse_statement with check for 1
8392 * single warning. It is needed, because for statement expressions we have
8393 * to avoid the warning on the last statement)
8395 static statement_t *parse_statement(void)
8397 statement_t *statement = intern_parse_statement();
8399 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
8400 expression_t *expression = statement->expression.expression;
8401 if (!expression_has_effect(expression)) {
8402 warningf(&expression->base.source_position,
8403 "statement has no effect");
8411 * Parse a compound statement.
8413 static statement_t *parse_compound_statement(bool inside_expression_statement)
8415 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
8417 statement->base.source_position = token.source_position;
8420 add_anchor_token('}');
8422 int top = environment_top();
8423 scope_t *last_scope = scope;
8424 set_scope(&statement->compound.scope);
8426 statement_t *last_statement = NULL;
8428 while (token.type != '}' && token.type != T_EOF) {
8429 statement_t *sub_statement = intern_parse_statement();
8430 if (is_invalid_statement(sub_statement)) {
8431 /* an error occurred. if we are at an anchor, return */
8437 if (last_statement != NULL) {
8438 last_statement->base.next = sub_statement;
8440 statement->compound.statements = sub_statement;
8443 while (sub_statement->base.next != NULL)
8444 sub_statement = sub_statement->base.next;
8446 last_statement = sub_statement;
8449 if (token.type == '}') {
8452 errorf(&statement->base.source_position,
8453 "end of file while looking for closing '}'");
8456 /* look over all statements again to produce no effect warnings */
8457 if (warning.unused_value) {
8458 statement_t *sub_statement = statement->compound.statements;
8459 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
8460 if (sub_statement->kind != STATEMENT_EXPRESSION)
8462 /* don't emit a warning for the last expression in an expression
8463 * statement as it has always an effect */
8464 if (inside_expression_statement && sub_statement->base.next == NULL)
8467 expression_t *expression = sub_statement->expression.expression;
8468 if (!expression_has_effect(expression)) {
8469 warningf(&expression->base.source_position,
8470 "statement has no effect");
8476 rem_anchor_token('}');
8477 assert(scope == &statement->compound.scope);
8478 set_scope(last_scope);
8479 environment_pop_to(top);
8485 * Initialize builtin types.
8487 static void initialize_builtin_types(void)
8489 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
8490 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
8491 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
8492 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
8493 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
8494 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
8495 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
8496 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
8498 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
8499 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
8500 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
8501 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
8505 * Check for unused global static functions and variables
8507 static void check_unused_globals(void)
8509 if (!warning.unused_function && !warning.unused_variable)
8512 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
8514 decl->modifiers & DM_UNUSED ||
8515 decl->modifiers & DM_USED ||
8516 decl->storage_class != STORAGE_CLASS_STATIC)
8519 type_t *const type = decl->type;
8521 if (is_type_function(skip_typeref(type))) {
8522 if (!warning.unused_function || decl->is_inline)
8525 s = (decl->init.statement != NULL ? "defined" : "declared");
8527 if (!warning.unused_variable)
8533 warningf(&decl->source_position, "'%#T' %s but not used",
8534 type, decl->symbol, s);
8538 static void parse_global_asm(void)
8543 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8544 statement->base.source_position = token.source_position;
8545 statement->asms.asm_text = parse_string_literals();
8546 statement->base.next = unit->global_asm;
8547 unit->global_asm = statement;
8556 * Parse a translation unit.
8558 static void parse_translation_unit(void)
8560 while (token.type != T_EOF) {
8561 switch (token.type) {
8563 /* TODO error in strict mode */
8564 warningf(HERE, "stray ';' outside of function");
8573 parse_external_declaration();
8582 * @return the translation unit or NULL if errors occurred.
8584 void start_parsing(void)
8586 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8587 label_stack = NEW_ARR_F(stack_entry_t, 0);
8588 diagnostic_count = 0;
8592 type_set_output(stderr);
8593 ast_set_output(stderr);
8595 assert(unit == NULL);
8596 unit = allocate_ast_zero(sizeof(unit[0]));
8598 assert(global_scope == NULL);
8599 global_scope = &unit->scope;
8601 assert(scope == NULL);
8602 set_scope(&unit->scope);
8604 initialize_builtin_types();
8607 translation_unit_t *finish_parsing(void)
8609 assert(scope == &unit->scope);
8611 last_declaration = NULL;
8613 assert(global_scope == &unit->scope);
8614 check_unused_globals();
8615 global_scope = NULL;
8617 DEL_ARR_F(environment_stack);
8618 DEL_ARR_F(label_stack);
8620 translation_unit_t *result = unit;
8627 lookahead_bufpos = 0;
8628 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8631 parse_translation_unit();
8635 * Initialize the parser.
8637 void init_parser(void)
8640 /* add predefined symbols for extended-decl-modifier */
8641 sym_align = symbol_table_insert("align");
8642 sym_allocate = symbol_table_insert("allocate");
8643 sym_dllimport = symbol_table_insert("dllimport");
8644 sym_dllexport = symbol_table_insert("dllexport");
8645 sym_naked = symbol_table_insert("naked");
8646 sym_noinline = symbol_table_insert("noinline");
8647 sym_noreturn = symbol_table_insert("noreturn");
8648 sym_nothrow = symbol_table_insert("nothrow");
8649 sym_novtable = symbol_table_insert("novtable");
8650 sym_property = symbol_table_insert("property");
8651 sym_get = symbol_table_insert("get");
8652 sym_put = symbol_table_insert("put");
8653 sym_selectany = symbol_table_insert("selectany");
8654 sym_thread = symbol_table_insert("thread");
8655 sym_uuid = symbol_table_insert("uuid");
8656 sym_deprecated = symbol_table_insert("deprecated");
8657 sym_restrict = symbol_table_insert("restrict");
8658 sym_noalias = symbol_table_insert("noalias");
8660 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8662 init_expression_parsers();
8663 obstack_init(&temp_obst);
8665 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8666 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8670 * Terminate the parser.
8672 void exit_parser(void)
8674 obstack_free(&temp_obst, NULL);