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) {
503 assert(0 <= token_type && token_type < T_LAST_TOKEN);
504 ++token_anchor_set[token_type];
507 static int save_and_reset_anchor_state(int token_type) {
508 assert(0 <= token_type && token_type < T_LAST_TOKEN);
509 int count = token_anchor_set[token_type];
510 token_anchor_set[token_type] = 0;
514 static void restore_anchor_state(int token_type, int count) {
515 assert(0 <= token_type && token_type < T_LAST_TOKEN);
516 token_anchor_set[token_type] = count;
520 * Remove a token from the token anchor set (a multi-set).
522 static void rem_anchor_token(int token_type) {
523 assert(0 <= token_type && token_type < T_LAST_TOKEN);
524 --token_anchor_set[token_type];
527 static bool at_anchor(void) {
530 return token_anchor_set[token.type];
534 * Eat tokens until a matching token is found.
536 static void eat_until_matching_token(int type) {
537 unsigned parenthesis_count = 0;
538 unsigned brace_count = 0;
539 unsigned bracket_count = 0;
543 case '(': end_token = ')'; break;
544 case '{': end_token = '}'; break;
545 case '[': end_token = ']'; break;
546 default: end_token = type; break;
549 while(token.type != end_token ||
550 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
554 case '(': ++parenthesis_count; break;
555 case '{': ++brace_count; break;
556 case '[': ++bracket_count; break;
558 if (parenthesis_count > 0)
566 if (bracket_count > 0)
577 * Eat input tokens until an anchor is found.
579 static void eat_until_anchor(void) {
580 if (token.type == T_EOF)
582 while(token_anchor_set[token.type] == 0) {
583 if (token.type == '(' || token.type == '{' || token.type == '[')
584 eat_until_matching_token(token.type);
585 if (token.type == T_EOF)
591 static void eat_block(void) {
592 eat_until_matching_token('{');
593 if (token.type == '}')
598 * eat all token until a ';' is reached or a stop token is found.
600 static void eat_statement(void) {
601 eat_until_matching_token(';');
602 if (token.type == ';')
606 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
609 * Report a parse error because an expected token was not found.
612 #if defined __GNUC__ && __GNUC__ >= 4
613 __attribute__((sentinel))
615 void parse_error_expected(const char *message, ...)
617 if (message != NULL) {
618 errorf(HERE, "%s", message);
621 va_start(ap, message);
622 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
627 * Report a type error.
629 static void type_error(const char *msg, const source_position_t *source_position,
632 errorf(source_position, "%s, but found type '%T'", msg, type);
636 * Report an incompatible type.
638 static void type_error_incompatible(const char *msg,
639 const source_position_t *source_position, type_t *type1, type_t *type2)
641 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
646 * Expect the the current token is the expected token.
647 * If not, generate an error, eat the current statement,
648 * and goto the end_error label.
650 #define expect(expected) \
652 if (UNLIKELY(token.type != (expected))) { \
653 parse_error_expected(NULL, (expected), NULL); \
654 add_anchor_token(expected); \
655 eat_until_anchor(); \
656 if (token.type == expected) \
658 rem_anchor_token(expected); \
664 static void set_scope(scope_t *new_scope)
667 scope->last_declaration = last_declaration;
671 last_declaration = new_scope->last_declaration;
675 * Search a symbol in a given namespace and returns its declaration or
676 * NULL if this symbol was not found.
678 static declaration_t *get_declaration(const symbol_t *const symbol,
679 const namespace_t namespc)
681 declaration_t *declaration = symbol->declaration;
682 for( ; declaration != NULL; declaration = declaration->symbol_next) {
683 if (declaration->namespc == namespc)
691 * pushs an environment_entry on the environment stack and links the
692 * corresponding symbol to the new entry
694 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
696 symbol_t *symbol = declaration->symbol;
697 namespace_t namespc = (namespace_t) declaration->namespc;
699 /* replace/add declaration into declaration list of the symbol */
700 declaration_t *iter = symbol->declaration;
702 symbol->declaration = declaration;
704 declaration_t *iter_last = NULL;
705 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
706 /* replace an entry? */
707 if (iter->namespc == namespc) {
708 if (iter_last == NULL) {
709 symbol->declaration = declaration;
711 iter_last->symbol_next = declaration;
713 declaration->symbol_next = iter->symbol_next;
718 assert(iter_last->symbol_next == NULL);
719 iter_last->symbol_next = declaration;
723 /* remember old declaration */
725 entry.symbol = symbol;
726 entry.old_declaration = iter;
727 entry.namespc = (unsigned short) namespc;
728 ARR_APP1(stack_entry_t, *stack_ptr, entry);
731 static void environment_push(declaration_t *declaration)
733 assert(declaration->source_position.input_name != NULL);
734 assert(declaration->parent_scope != NULL);
735 stack_push(&environment_stack, declaration);
739 * Push a declaration of the label stack.
741 * @param declaration the declaration
743 static void label_push(declaration_t *declaration)
745 declaration->parent_scope = ¤t_function->scope;
746 stack_push(&label_stack, declaration);
750 * pops symbols from the environment stack until @p new_top is the top element
752 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
754 stack_entry_t *stack = *stack_ptr;
755 size_t top = ARR_LEN(stack);
758 assert(new_top <= top);
762 for(i = top; i > new_top; --i) {
763 stack_entry_t *entry = &stack[i - 1];
765 declaration_t *old_declaration = entry->old_declaration;
766 symbol_t *symbol = entry->symbol;
767 namespace_t namespc = (namespace_t)entry->namespc;
769 /* replace/remove declaration */
770 declaration_t *declaration = symbol->declaration;
771 assert(declaration != NULL);
772 if (declaration->namespc == namespc) {
773 if (old_declaration == NULL) {
774 symbol->declaration = declaration->symbol_next;
776 symbol->declaration = old_declaration;
779 declaration_t *iter_last = declaration;
780 declaration_t *iter = declaration->symbol_next;
781 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
782 /* replace an entry? */
783 if (iter->namespc == namespc) {
784 assert(iter_last != NULL);
785 iter_last->symbol_next = old_declaration;
786 if (old_declaration != NULL) {
787 old_declaration->symbol_next = iter->symbol_next;
792 assert(iter != NULL);
796 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
799 static void environment_pop_to(size_t new_top)
801 stack_pop_to(&environment_stack, new_top);
805 * Pop all entries on the label stack until the new_top
808 * @param new_top the new stack top
810 static void label_pop_to(size_t new_top)
812 stack_pop_to(&label_stack, new_top);
816 static int get_rank(const type_t *type)
818 assert(!is_typeref(type));
819 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
820 * and esp. footnote 108). However we can't fold constants (yet), so we
821 * can't decide whether unsigned int is possible, while int always works.
822 * (unsigned int would be preferable when possible... for stuff like
823 * struct { enum { ... } bla : 4; } ) */
824 if (type->kind == TYPE_ENUM)
825 return ATOMIC_TYPE_INT;
827 assert(type->kind == TYPE_ATOMIC);
828 return type->atomic.akind;
831 static type_t *promote_integer(type_t *type)
833 if (type->kind == TYPE_BITFIELD)
834 type = type->bitfield.base_type;
836 if (get_rank(type) < ATOMIC_TYPE_INT)
843 * Create a cast expression.
845 * @param expression the expression to cast
846 * @param dest_type the destination type
848 static expression_t *create_cast_expression(expression_t *expression,
851 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
853 cast->unary.value = expression;
854 cast->base.type = dest_type;
860 * Check if a given expression represents the 0 pointer constant.
862 static bool is_null_pointer_constant(const expression_t *expression)
864 /* skip void* cast */
865 if (expression->kind == EXPR_UNARY_CAST
866 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
867 expression = expression->unary.value;
870 /* TODO: not correct yet, should be any constant integer expression
871 * which evaluates to 0 */
872 if (expression->kind != EXPR_CONST)
875 type_t *const type = skip_typeref(expression->base.type);
876 if (!is_type_integer(type))
879 return expression->conste.v.int_value == 0;
883 * Create an implicit cast expression.
885 * @param expression the expression to cast
886 * @param dest_type the destination type
888 static expression_t *create_implicit_cast(expression_t *expression,
891 type_t *const source_type = expression->base.type;
893 if (source_type == dest_type)
896 return create_cast_expression(expression, dest_type);
899 typedef enum assign_error_t {
901 ASSIGN_ERROR_INCOMPATIBLE,
902 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
903 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
904 ASSIGN_WARNING_POINTER_FROM_INT,
905 ASSIGN_WARNING_INT_FROM_POINTER
908 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
909 const expression_t *const right,
911 const source_position_t *source_position)
913 type_t *const orig_type_right = right->base.type;
914 type_t *const type_left = skip_typeref(orig_type_left);
915 type_t *const type_right = skip_typeref(orig_type_right);
920 case ASSIGN_ERROR_INCOMPATIBLE:
921 errorf(source_position,
922 "destination type '%T' in %s is incompatible with type '%T'",
923 orig_type_left, context, orig_type_right);
926 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
927 type_t *points_to_left
928 = skip_typeref(type_left->pointer.points_to);
929 type_t *points_to_right
930 = skip_typeref(type_right->pointer.points_to);
932 /* the left type has all qualifiers from the right type */
933 unsigned missing_qualifiers
934 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
935 errorf(source_position,
936 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type",
937 orig_type_left, context, orig_type_right, missing_qualifiers);
941 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
942 warningf(source_position,
943 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
944 orig_type_left, context, right, orig_type_right);
947 case ASSIGN_WARNING_POINTER_FROM_INT:
948 warningf(source_position,
949 "%s makes integer '%T' from pointer '%T' without a cast",
950 context, orig_type_left, orig_type_right);
953 case ASSIGN_WARNING_INT_FROM_POINTER:
954 warningf(source_position,
955 "%s makes integer '%T' from pointer '%T' without a cast",
956 context, orig_type_left, orig_type_right);
960 panic("invalid error value");
964 /** Implements the rules from § 6.5.16.1 */
965 static assign_error_t semantic_assign(type_t *orig_type_left,
966 const expression_t *const right)
968 type_t *const orig_type_right = right->base.type;
969 type_t *const type_left = skip_typeref(orig_type_left);
970 type_t *const type_right = skip_typeref(orig_type_right);
972 if (is_type_pointer(type_left)) {
973 if (is_null_pointer_constant(right)) {
974 return ASSIGN_SUCCESS;
975 } else if (is_type_pointer(type_right)) {
976 type_t *points_to_left
977 = skip_typeref(type_left->pointer.points_to);
978 type_t *points_to_right
979 = skip_typeref(type_right->pointer.points_to);
981 /* the left type has all qualifiers from the right type */
982 unsigned missing_qualifiers
983 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
984 if (missing_qualifiers != 0) {
985 return ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
988 points_to_left = get_unqualified_type(points_to_left);
989 points_to_right = get_unqualified_type(points_to_right);
991 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
992 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
993 return ASSIGN_SUCCESS;
996 if (!types_compatible(points_to_left, points_to_right)) {
997 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1000 return ASSIGN_SUCCESS;
1001 } else if (is_type_integer(type_right)) {
1002 return ASSIGN_WARNING_POINTER_FROM_INT;
1004 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1005 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1006 && is_type_pointer(type_right))) {
1007 return ASSIGN_SUCCESS;
1008 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1009 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1010 type_t *const unqual_type_left = get_unqualified_type(type_left);
1011 type_t *const unqual_type_right = get_unqualified_type(type_right);
1012 if (types_compatible(unqual_type_left, unqual_type_right)) {
1013 return ASSIGN_SUCCESS;
1015 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1016 return ASSIGN_WARNING_INT_FROM_POINTER;
1019 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1020 return ASSIGN_SUCCESS;
1022 return ASSIGN_ERROR_INCOMPATIBLE;
1025 static expression_t *parse_constant_expression(void)
1027 /* start parsing at precedence 7 (conditional expression) */
1028 expression_t *result = parse_sub_expression(7);
1030 if (!is_constant_expression(result)) {
1031 errorf(&result->base.source_position,
1032 "expression '%E' is not constant\n", result);
1038 static expression_t *parse_assignment_expression(void)
1040 /* start parsing at precedence 2 (assignment expression) */
1041 return parse_sub_expression(2);
1044 static type_t *make_global_typedef(const char *name, type_t *type)
1046 symbol_t *const symbol = symbol_table_insert(name);
1048 declaration_t *const declaration = allocate_declaration_zero();
1049 declaration->namespc = NAMESPACE_NORMAL;
1050 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1051 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1052 declaration->type = type;
1053 declaration->symbol = symbol;
1054 declaration->source_position = builtin_source_position;
1056 record_declaration(declaration);
1058 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1059 typedef_type->typedeft.declaration = declaration;
1061 return typedef_type;
1064 static string_t parse_string_literals(void)
1066 assert(token.type == T_STRING_LITERAL);
1067 string_t result = token.v.string;
1071 while (token.type == T_STRING_LITERAL) {
1072 result = concat_strings(&result, &token.v.string);
1079 static const char *gnu_attribute_names[GNU_AK_LAST] = {
1080 [GNU_AK_CONST] = "const",
1081 [GNU_AK_VOLATILE] = "volatile",
1082 [GNU_AK_CDECL] = "cdecl",
1083 [GNU_AK_STDCALL] = "stdcall",
1084 [GNU_AK_FASTCALL] = "fastcall",
1085 [GNU_AK_DEPRECATED] = "deprecated",
1086 [GNU_AK_NOINLINE] = "noinline",
1087 [GNU_AK_NORETURN] = "noreturn",
1088 [GNU_AK_NAKED] = "naked",
1089 [GNU_AK_PURE] = "pure",
1090 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1091 [GNU_AK_MALLOC] = "malloc",
1092 [GNU_AK_WEAK] = "weak",
1093 [GNU_AK_CONSTRUCTOR] = "constructor",
1094 [GNU_AK_DESTRUCTOR] = "destructor",
1095 [GNU_AK_NOTHROW] = "nothrow",
1096 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1097 [GNU_AK_COMMON] = "common",
1098 [GNU_AK_NOCOMMON] = "nocommon",
1099 [GNU_AK_PACKED] = "packed",
1100 [GNU_AK_SHARED] = "shared",
1101 [GNU_AK_NOTSHARED] = "notshared",
1102 [GNU_AK_USED] = "used",
1103 [GNU_AK_UNUSED] = "unused",
1104 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1105 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1106 [GNU_AK_LONGCALL] = "longcall",
1107 [GNU_AK_SHORTCALL] = "shortcall",
1108 [GNU_AK_LONG_CALL] = "long_call",
1109 [GNU_AK_SHORT_CALL] = "short_call",
1110 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1111 [GNU_AK_INTERRUPT] = "interrupt",
1112 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1113 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1114 [GNU_AK_NESTING] = "nesting",
1115 [GNU_AK_NEAR] = "near",
1116 [GNU_AK_FAR] = "far",
1117 [GNU_AK_SIGNAL] = "signal",
1118 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1119 [GNU_AK_TINY_DATA] = "tiny_data",
1120 [GNU_AK_SAVEALL] = "saveall",
1121 [GNU_AK_FLATTEN] = "flatten",
1122 [GNU_AK_SSEREGPARM] = "sseregparm",
1123 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1124 [GNU_AK_RETURN_TWICE] = "return_twice",
1125 [GNU_AK_MAY_ALIAS] = "may_alias",
1126 [GNU_AK_MS_STRUCT] = "ms_struct",
1127 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1128 [GNU_AK_DLLIMPORT] = "dllimport",
1129 [GNU_AK_DLLEXPORT] = "dllexport",
1130 [GNU_AK_ALIGNED] = "aligned",
1131 [GNU_AK_ALIAS] = "alias",
1132 [GNU_AK_SECTION] = "section",
1133 [GNU_AK_FORMAT] = "format",
1134 [GNU_AK_FORMAT_ARG] = "format_arg",
1135 [GNU_AK_WEAKREF] = "weakref",
1136 [GNU_AK_NONNULL] = "nonnull",
1137 [GNU_AK_TLS_MODEL] = "tls_model",
1138 [GNU_AK_VISIBILITY] = "visibility",
1139 [GNU_AK_REGPARM] = "regparm",
1140 [GNU_AK_MODE] = "mode",
1141 [GNU_AK_MODEL] = "model",
1142 [GNU_AK_TRAP_EXIT] = "trap_exit",
1143 [GNU_AK_SP_SWITCH] = "sp_switch",
1144 [GNU_AK_SENTINEL] = "sentinel"
1148 * compare two string, ignoring double underscores on the second.
1150 static int strcmp_underscore(const char *s1, const char *s2) {
1151 if (s2[0] == '_' && s2[1] == '_') {
1152 size_t len2 = strlen(s2);
1153 size_t len1 = strlen(s1);
1154 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1155 return strncmp(s1, s2+2, len2-4);
1159 return strcmp(s1, s2);
1163 * Allocate a new gnu temporal attribute.
1165 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1166 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1167 attribute->kind = kind;
1168 attribute->next = NULL;
1169 attribute->invalid = false;
1170 attribute->have_arguments = false;
1176 * parse one constant expression argument.
1178 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1179 expression_t *expression;
1180 add_anchor_token(')');
1181 expression = parse_constant_expression();
1182 rem_anchor_token(')');
1184 attribute->u.argument = fold_constant(expression);
1187 attribute->invalid = true;
1191 * parse a list of constant expressions arguments.
1193 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1194 argument_list_t **list = &attribute->u.arguments;
1195 argument_list_t *entry;
1196 expression_t *expression;
1197 add_anchor_token(')');
1198 add_anchor_token(',');
1200 expression = parse_constant_expression();
1201 entry = obstack_alloc(&temp_obst, sizeof(entry));
1202 entry->argument = fold_constant(expression);
1205 list = &entry->next;
1206 if (token.type != ',')
1210 rem_anchor_token(',');
1211 rem_anchor_token(')');
1215 attribute->invalid = true;
1219 * parse one string literal argument.
1221 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1224 add_anchor_token('(');
1225 if (token.type != T_STRING_LITERAL) {
1226 parse_error_expected("while parsing attribute directive",
1227 T_STRING_LITERAL, NULL);
1230 *string = parse_string_literals();
1231 rem_anchor_token('(');
1235 attribute->invalid = true;
1239 * parse one tls model.
1241 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1242 static const char *tls_models[] = {
1248 string_t string = { NULL, 0 };
1249 parse_gnu_attribute_string_arg(attribute, &string);
1250 if (string.begin != NULL) {
1251 for(size_t i = 0; i < 4; ++i) {
1252 if (strcmp(tls_models[i], string.begin) == 0) {
1253 attribute->u.value = i;
1257 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1259 attribute->invalid = true;
1263 * parse one tls model.
1265 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1266 static const char *visibilities[] = {
1272 string_t string = { NULL, 0 };
1273 parse_gnu_attribute_string_arg(attribute, &string);
1274 if (string.begin != NULL) {
1275 for(size_t i = 0; i < 4; ++i) {
1276 if (strcmp(visibilities[i], string.begin) == 0) {
1277 attribute->u.value = i;
1281 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1283 attribute->invalid = true;
1287 * parse one (code) model.
1289 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1290 static const char *visibilities[] = {
1295 string_t string = { NULL, 0 };
1296 parse_gnu_attribute_string_arg(attribute, &string);
1297 if (string.begin != NULL) {
1298 for(int i = 0; i < 3; ++i) {
1299 if (strcmp(visibilities[i], string.begin) == 0) {
1300 attribute->u.value = i;
1304 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1306 attribute->invalid = true;
1309 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1311 /* TODO: find out what is allowed here... */
1313 /* at least: byte, word, pointer, list of machine modes
1314 * __XXX___ is interpreted as XXX */
1315 add_anchor_token(')');
1317 if (token.type != T_IDENTIFIER) {
1318 expect(T_IDENTIFIER);
1321 /* This isn't really correct, the backend should provide a list of machine
1322 * specific modes (according to gcc philosophy that is...) */
1323 const char *symbol_str = token.v.symbol->string;
1324 if (strcmp_underscore("QI", symbol_str) == 0 ||
1325 strcmp_underscore("byte", symbol_str) == 0) {
1326 attribute->u.akind = ATOMIC_TYPE_CHAR;
1327 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1328 attribute->u.akind = ATOMIC_TYPE_SHORT;
1329 } else if (strcmp_underscore("SI", symbol_str) == 0
1330 || strcmp_underscore("word", symbol_str) == 0
1331 || strcmp_underscore("pointer", symbol_str) == 0) {
1332 attribute->u.akind = ATOMIC_TYPE_INT;
1333 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1334 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1336 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1337 attribute->invalid = true;
1341 rem_anchor_token(')');
1345 attribute->invalid = true;
1349 * parse one interrupt argument.
1351 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1352 static const char *interrupts[] = {
1359 string_t string = { NULL, 0 };
1360 parse_gnu_attribute_string_arg(attribute, &string);
1361 if (string.begin != NULL) {
1362 for(size_t i = 0; i < 5; ++i) {
1363 if (strcmp(interrupts[i], string.begin) == 0) {
1364 attribute->u.value = i;
1368 errorf(HERE, "'%s' is not an interrupt", string.begin);
1370 attribute->invalid = true;
1374 * parse ( identifier, const expression, const expression )
1376 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1377 static const char *format_names[] = {
1385 if (token.type != T_IDENTIFIER) {
1386 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1389 const char *name = token.v.symbol->string;
1390 for(i = 0; i < 4; ++i) {
1391 if (strcmp_underscore(format_names[i], name) == 0)
1395 if (warning.attribute)
1396 warningf(HERE, "'%s' is an unrecognized format function type", name);
1401 add_anchor_token(')');
1402 add_anchor_token(',');
1403 parse_constant_expression();
1404 rem_anchor_token(',');
1405 rem_anchor_token('(');
1408 add_anchor_token(')');
1409 parse_constant_expression();
1410 rem_anchor_token('(');
1414 attribute->u.value = true;
1417 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1419 if (!attribute->have_arguments)
1422 /* should have no arguments */
1423 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1424 eat_until_matching_token('(');
1425 /* we have already consumed '(', so we stop before ')', eat it */
1427 attribute->invalid = true;
1431 * Parse one GNU attribute.
1433 * Note that attribute names can be specified WITH or WITHOUT
1434 * double underscores, ie const or __const__.
1436 * The following attributes are parsed without arguments
1461 * no_instrument_function
1462 * warn_unused_result
1479 * externally_visible
1487 * The following attributes are parsed with arguments
1488 * aligned( const expression )
1489 * alias( string literal )
1490 * section( string literal )
1491 * format( identifier, const expression, const expression )
1492 * format_arg( const expression )
1493 * tls_model( string literal )
1494 * visibility( string literal )
1495 * regparm( const expression )
1496 * model( string leteral )
1497 * trap_exit( const expression )
1498 * sp_switch( string literal )
1500 * The following attributes might have arguments
1501 * weak_ref( string literal )
1502 * non_null( const expression // ',' )
1503 * interrupt( string literal )
1504 * sentinel( constant expression )
1506 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1508 gnu_attribute_t *head = *attributes;
1509 gnu_attribute_t *last = *attributes;
1510 decl_modifiers_t modifiers = 0;
1511 gnu_attribute_t *attribute;
1513 eat(T___attribute__);
1517 if (token.type != ')') {
1518 /* find the end of the list */
1520 while(last->next != NULL)
1524 /* non-empty attribute list */
1527 if (token.type == T_const) {
1529 } else if (token.type == T_volatile) {
1531 } else if (token.type == T_cdecl) {
1532 /* __attribute__((cdecl)), WITH ms mode */
1534 } else if (token.type == T_IDENTIFIER) {
1535 const symbol_t *sym = token.v.symbol;
1538 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1545 for(i = 0; i < GNU_AK_LAST; ++i) {
1546 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1549 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1552 if (kind == GNU_AK_LAST) {
1553 if (warning.attribute)
1554 warningf(HERE, "'%s' attribute directive ignored", name);
1556 /* skip possible arguments */
1557 if (token.type == '(') {
1558 eat_until_matching_token(')');
1561 /* check for arguments */
1562 attribute = allocate_gnu_attribute(kind);
1563 if (token.type == '(') {
1565 if (token.type == ')') {
1566 /* empty args are allowed */
1569 attribute->have_arguments = true;
1574 case GNU_AK_VOLATILE:
1575 case GNU_AK_DEPRECATED:
1580 case GNU_AK_NOCOMMON:
1582 case GNU_AK_NOTSHARED:
1583 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1584 case GNU_AK_WARN_UNUSED_RESULT:
1585 case GNU_AK_LONGCALL:
1586 case GNU_AK_SHORTCALL:
1587 case GNU_AK_LONG_CALL:
1588 case GNU_AK_SHORT_CALL:
1589 case GNU_AK_FUNCTION_VECTOR:
1590 case GNU_AK_INTERRUPT_HANDLER:
1591 case GNU_AK_NMI_HANDLER:
1592 case GNU_AK_NESTING:
1596 case GNU_AK_EIGTHBIT_DATA:
1597 case GNU_AK_TINY_DATA:
1598 case GNU_AK_SAVEALL:
1599 case GNU_AK_FLATTEN:
1600 case GNU_AK_SSEREGPARM:
1601 case GNU_AK_EXTERNALLY_VISIBLE:
1602 case GNU_AK_RETURN_TWICE:
1603 case GNU_AK_MAY_ALIAS:
1604 case GNU_AK_MS_STRUCT:
1605 case GNU_AK_GCC_STRUCT:
1608 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1609 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1610 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1611 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1612 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1613 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1614 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1615 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1616 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1617 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1618 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1619 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1620 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1621 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1622 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1623 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1625 case GNU_AK_ALIGNED:
1626 /* __align__ may be used without an argument */
1627 if (attribute->have_arguments) {
1628 parse_gnu_attribute_const_arg(attribute);
1632 case GNU_AK_FORMAT_ARG:
1633 case GNU_AK_REGPARM:
1634 case GNU_AK_TRAP_EXIT:
1635 if (!attribute->have_arguments) {
1636 /* should have arguments */
1637 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1638 attribute->invalid = true;
1640 parse_gnu_attribute_const_arg(attribute);
1643 case GNU_AK_SECTION:
1644 case GNU_AK_SP_SWITCH:
1645 if (!attribute->have_arguments) {
1646 /* should have arguments */
1647 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1648 attribute->invalid = true;
1650 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
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_format_args(attribute);
1660 case GNU_AK_WEAKREF:
1661 /* may have one string argument */
1662 if (attribute->have_arguments)
1663 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1665 case GNU_AK_NONNULL:
1666 if (attribute->have_arguments)
1667 parse_gnu_attribute_const_arg_list(attribute);
1669 case GNU_AK_TLS_MODEL:
1670 if (!attribute->have_arguments) {
1671 /* should have arguments */
1672 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1674 parse_gnu_attribute_tls_model_arg(attribute);
1676 case GNU_AK_VISIBILITY:
1677 if (!attribute->have_arguments) {
1678 /* should have arguments */
1679 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1681 parse_gnu_attribute_visibility_arg(attribute);
1684 if (!attribute->have_arguments) {
1685 /* should have arguments */
1686 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1688 parse_gnu_attribute_model_arg(attribute);
1692 if (!attribute->have_arguments) {
1693 /* should have arguments */
1694 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1696 parse_gnu_attribute_mode_arg(attribute);
1699 case GNU_AK_INTERRUPT:
1700 /* may have one string argument */
1701 if (attribute->have_arguments)
1702 parse_gnu_attribute_interrupt_arg(attribute);
1704 case GNU_AK_SENTINEL:
1705 /* may have one string argument */
1706 if (attribute->have_arguments)
1707 parse_gnu_attribute_const_arg(attribute);
1710 /* already handled */
1714 check_no_argument(attribute, name);
1717 if (attribute != NULL) {
1719 last->next = attribute;
1722 head = last = attribute;
1726 if (token.type != ',')
1740 * Parse GNU attributes.
1742 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1744 decl_modifiers_t modifiers = 0;
1747 switch(token.type) {
1748 case T___attribute__:
1749 modifiers |= parse_gnu_attribute(attributes);
1755 if (token.type != T_STRING_LITERAL) {
1756 parse_error_expected("while parsing assembler attribute",
1757 T_STRING_LITERAL, NULL);
1758 eat_until_matching_token('(');
1761 parse_string_literals();
1766 case T_cdecl: modifiers |= DM_CDECL; break;
1767 case T__fastcall: modifiers |= DM_FASTCALL; break;
1768 case T__stdcall: modifiers |= DM_STDCALL; break;
1771 /* TODO record modifier */
1772 warningf(HERE, "Ignoring declaration modifier %K", &token);
1776 default: return modifiers;
1783 static designator_t *parse_designation(void)
1785 designator_t *result = NULL;
1786 designator_t *last = NULL;
1789 designator_t *designator;
1790 switch(token.type) {
1792 designator = allocate_ast_zero(sizeof(designator[0]));
1793 designator->source_position = token.source_position;
1795 add_anchor_token(']');
1796 designator->array_index = parse_constant_expression();
1797 rem_anchor_token(']');
1801 designator = allocate_ast_zero(sizeof(designator[0]));
1802 designator->source_position = token.source_position;
1804 if (token.type != T_IDENTIFIER) {
1805 parse_error_expected("while parsing designator",
1806 T_IDENTIFIER, NULL);
1809 designator->symbol = token.v.symbol;
1817 assert(designator != NULL);
1819 last->next = designator;
1821 result = designator;
1829 static initializer_t *initializer_from_string(array_type_t *type,
1830 const string_t *const string)
1832 /* TODO: check len vs. size of array type */
1835 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1836 initializer->string.string = *string;
1841 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1842 wide_string_t *const string)
1844 /* TODO: check len vs. size of array type */
1847 initializer_t *const initializer =
1848 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1849 initializer->wide_string.string = *string;
1855 * Build an initializer from a given expression.
1857 static initializer_t *initializer_from_expression(type_t *orig_type,
1858 expression_t *expression)
1860 /* TODO check that expression is a constant expression */
1862 /* § 6.7.8.14/15 char array may be initialized by string literals */
1863 type_t *type = skip_typeref(orig_type);
1864 type_t *expr_type_orig = expression->base.type;
1865 type_t *expr_type = skip_typeref(expr_type_orig);
1866 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1867 array_type_t *const array_type = &type->array;
1868 type_t *const element_type = skip_typeref(array_type->element_type);
1870 if (element_type->kind == TYPE_ATOMIC) {
1871 atomic_type_kind_t akind = element_type->atomic.akind;
1872 switch (expression->kind) {
1873 case EXPR_STRING_LITERAL:
1874 if (akind == ATOMIC_TYPE_CHAR
1875 || akind == ATOMIC_TYPE_SCHAR
1876 || akind == ATOMIC_TYPE_UCHAR) {
1877 return initializer_from_string(array_type,
1878 &expression->string.value);
1881 case EXPR_WIDE_STRING_LITERAL: {
1882 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1883 if (get_unqualified_type(element_type) == bare_wchar_type) {
1884 return initializer_from_wide_string(array_type,
1885 &expression->wide_string.value);
1895 assign_error_t error = semantic_assign(type, expression);
1896 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1898 report_assign_error(error, type, expression, "initializer",
1899 &expression->base.source_position);
1901 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1902 result->value.value = create_implicit_cast(expression, type);
1908 * Checks if a given expression can be used as an constant initializer.
1910 static bool is_initializer_constant(const expression_t *expression)
1912 return is_constant_expression(expression)
1913 || is_address_constant(expression);
1917 * Parses an scalar initializer.
1919 * § 6.7.8.11; eat {} without warning
1921 static initializer_t *parse_scalar_initializer(type_t *type,
1922 bool must_be_constant)
1924 /* there might be extra {} hierarchies */
1926 if (token.type == '{') {
1927 warningf(HERE, "extra curly braces around scalar initializer");
1931 } while (token.type == '{');
1934 expression_t *expression = parse_assignment_expression();
1935 if (must_be_constant && !is_initializer_constant(expression)) {
1936 errorf(&expression->base.source_position,
1937 "Initialisation expression '%E' is not constant\n",
1941 initializer_t *initializer = initializer_from_expression(type, expression);
1943 if (initializer == NULL) {
1944 errorf(&expression->base.source_position,
1945 "expression '%E' (type '%T') doesn't match expected type '%T'",
1946 expression, expression->base.type, type);
1951 bool additional_warning_displayed = false;
1953 if (token.type == ',') {
1956 if (token.type != '}') {
1957 if (!additional_warning_displayed) {
1958 warningf(HERE, "additional elements in scalar initializer");
1959 additional_warning_displayed = true;
1970 * An entry in the type path.
1972 typedef struct type_path_entry_t type_path_entry_t;
1973 struct type_path_entry_t {
1974 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1976 size_t index; /**< For array types: the current index. */
1977 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1982 * A type path expression a position inside compound or array types.
1984 typedef struct type_path_t type_path_t;
1985 struct type_path_t {
1986 type_path_entry_t *path; /**< An flexible array containing the current path. */
1987 type_t *top_type; /**< type of the element the path points */
1988 size_t max_index; /**< largest index in outermost array */
1992 * Prints a type path for debugging.
1994 static __attribute__((unused)) void debug_print_type_path(
1995 const type_path_t *path)
1997 size_t len = ARR_LEN(path->path);
1999 for(size_t i = 0; i < len; ++i) {
2000 const type_path_entry_t *entry = & path->path[i];
2002 type_t *type = skip_typeref(entry->type);
2003 if (is_type_compound(type)) {
2004 /* in gcc mode structs can have no members */
2005 if (entry->v.compound_entry == NULL) {
2009 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2010 } else if (is_type_array(type)) {
2011 fprintf(stderr, "[%zd]", entry->v.index);
2013 fprintf(stderr, "-INVALID-");
2016 if (path->top_type != NULL) {
2017 fprintf(stderr, " (");
2018 print_type(path->top_type);
2019 fprintf(stderr, ")");
2024 * Return the top type path entry, ie. in a path
2025 * (type).a.b returns the b.
2027 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2029 size_t len = ARR_LEN(path->path);
2031 return &path->path[len-1];
2035 * Enlarge the type path by an (empty) element.
2037 static type_path_entry_t *append_to_type_path(type_path_t *path)
2039 size_t len = ARR_LEN(path->path);
2040 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2042 type_path_entry_t *result = & path->path[len];
2043 memset(result, 0, sizeof(result[0]));
2048 * Descending into a sub-type. Enter the scope of the current
2051 static void descend_into_subtype(type_path_t *path)
2053 type_t *orig_top_type = path->top_type;
2054 type_t *top_type = skip_typeref(orig_top_type);
2056 assert(is_type_compound(top_type) || is_type_array(top_type));
2058 type_path_entry_t *top = append_to_type_path(path);
2059 top->type = top_type;
2061 if (is_type_compound(top_type)) {
2062 declaration_t *declaration = top_type->compound.declaration;
2063 declaration_t *entry = declaration->scope.declarations;
2064 top->v.compound_entry = entry;
2066 if (entry != NULL) {
2067 path->top_type = entry->type;
2069 path->top_type = NULL;
2072 assert(is_type_array(top_type));
2075 path->top_type = top_type->array.element_type;
2080 * Pop an entry from the given type path, ie. returning from
2081 * (type).a.b to (type).a
2083 static void ascend_from_subtype(type_path_t *path)
2085 type_path_entry_t *top = get_type_path_top(path);
2087 path->top_type = top->type;
2089 size_t len = ARR_LEN(path->path);
2090 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2094 * Pop entries from the given type path until the given
2095 * path level is reached.
2097 static void ascend_to(type_path_t *path, size_t top_path_level)
2099 size_t len = ARR_LEN(path->path);
2101 while(len > top_path_level) {
2102 ascend_from_subtype(path);
2103 len = ARR_LEN(path->path);
2107 static bool walk_designator(type_path_t *path, const designator_t *designator,
2108 bool used_in_offsetof)
2110 for( ; designator != NULL; designator = designator->next) {
2111 type_path_entry_t *top = get_type_path_top(path);
2112 type_t *orig_type = top->type;
2114 type_t *type = skip_typeref(orig_type);
2116 if (designator->symbol != NULL) {
2117 symbol_t *symbol = designator->symbol;
2118 if (!is_type_compound(type)) {
2119 if (is_type_valid(type)) {
2120 errorf(&designator->source_position,
2121 "'.%Y' designator used for non-compound type '%T'",
2127 declaration_t *declaration = type->compound.declaration;
2128 declaration_t *iter = declaration->scope.declarations;
2129 for( ; iter != NULL; iter = iter->next) {
2130 if (iter->symbol == symbol) {
2135 errorf(&designator->source_position,
2136 "'%T' has no member named '%Y'", orig_type, symbol);
2139 if (used_in_offsetof) {
2140 type_t *real_type = skip_typeref(iter->type);
2141 if (real_type->kind == TYPE_BITFIELD) {
2142 errorf(&designator->source_position,
2143 "offsetof designator '%Y' may not specify bitfield",
2149 top->type = orig_type;
2150 top->v.compound_entry = iter;
2151 orig_type = iter->type;
2153 expression_t *array_index = designator->array_index;
2154 assert(designator->array_index != NULL);
2156 if (!is_type_array(type)) {
2157 if (is_type_valid(type)) {
2158 errorf(&designator->source_position,
2159 "[%E] designator used for non-array type '%T'",
2160 array_index, orig_type);
2164 if (!is_type_valid(array_index->base.type)) {
2168 long index = fold_constant(array_index);
2169 if (!used_in_offsetof) {
2171 errorf(&designator->source_position,
2172 "array index [%E] must be positive", array_index);
2175 if (type->array.size_constant == true) {
2176 long array_size = type->array.size;
2177 if (index >= array_size) {
2178 errorf(&designator->source_position,
2179 "designator [%E] (%d) exceeds array size %d",
2180 array_index, index, array_size);
2186 top->type = orig_type;
2187 top->v.index = (size_t) index;
2188 orig_type = type->array.element_type;
2190 path->top_type = orig_type;
2192 if (designator->next != NULL) {
2193 descend_into_subtype(path);
2202 static void advance_current_object(type_path_t *path, size_t top_path_level)
2204 type_path_entry_t *top = get_type_path_top(path);
2206 type_t *type = skip_typeref(top->type);
2207 if (is_type_union(type)) {
2208 /* in unions only the first element is initialized */
2209 top->v.compound_entry = NULL;
2210 } else if (is_type_struct(type)) {
2211 declaration_t *entry = top->v.compound_entry;
2213 entry = entry->next;
2214 top->v.compound_entry = entry;
2215 if (entry != NULL) {
2216 path->top_type = entry->type;
2220 assert(is_type_array(type));
2224 if (!type->array.size_constant || top->v.index < type->array.size) {
2229 /* we're past the last member of the current sub-aggregate, try if we
2230 * can ascend in the type hierarchy and continue with another subobject */
2231 size_t len = ARR_LEN(path->path);
2233 if (len > top_path_level) {
2234 ascend_from_subtype(path);
2235 advance_current_object(path, top_path_level);
2237 path->top_type = NULL;
2242 * skip until token is found.
2244 static void skip_until(int type) {
2245 while(token.type != type) {
2246 if (token.type == T_EOF)
2253 * skip any {...} blocks until a closing bracket is reached.
2255 static void skip_initializers(void)
2257 if (token.type == '{')
2260 while(token.type != '}') {
2261 if (token.type == T_EOF)
2263 if (token.type == '{') {
2271 static initializer_t *create_empty_initializer(void)
2273 static initializer_t empty_initializer
2274 = { .list = { { INITIALIZER_LIST }, 0 } };
2275 return &empty_initializer;
2279 * Parse a part of an initialiser for a struct or union,
2281 static initializer_t *parse_sub_initializer(type_path_t *path,
2282 type_t *outer_type, size_t top_path_level,
2283 parse_initializer_env_t *env)
2285 if (token.type == '}') {
2286 /* empty initializer */
2287 return create_empty_initializer();
2290 type_t *orig_type = path->top_type;
2291 type_t *type = NULL;
2293 if (orig_type == NULL) {
2294 /* We are initializing an empty compound. */
2296 type = skip_typeref(orig_type);
2298 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2299 * initializers in this case. */
2300 if (!is_type_valid(type)) {
2301 skip_initializers();
2302 return create_empty_initializer();
2306 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2309 designator_t *designator = NULL;
2310 if (token.type == '.' || token.type == '[') {
2311 designator = parse_designation();
2313 /* reset path to toplevel, evaluate designator from there */
2314 ascend_to(path, top_path_level);
2315 if (!walk_designator(path, designator, false)) {
2316 /* can't continue after designation error */
2320 initializer_t *designator_initializer
2321 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2322 designator_initializer->designator.designator = designator;
2323 ARR_APP1(initializer_t*, initializers, designator_initializer);
2325 orig_type = path->top_type;
2326 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2331 if (token.type == '{') {
2332 if (type != NULL && is_type_scalar(type)) {
2333 sub = parse_scalar_initializer(type, env->must_be_constant);
2337 if (env->declaration != NULL) {
2338 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2339 env->declaration->symbol);
2341 errorf(HERE, "extra brace group at end of initializer");
2344 descend_into_subtype(path);
2346 add_anchor_token('}');
2347 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2349 rem_anchor_token('}');
2352 ascend_from_subtype(path);
2356 goto error_parse_next;
2360 /* must be an expression */
2361 expression_t *expression = parse_assignment_expression();
2363 if (env->must_be_constant && !is_initializer_constant(expression)) {
2364 errorf(&expression->base.source_position,
2365 "Initialisation expression '%E' is not constant\n",
2370 /* we are already outside, ... */
2374 /* handle { "string" } special case */
2375 if ((expression->kind == EXPR_STRING_LITERAL
2376 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2377 && outer_type != NULL) {
2378 sub = initializer_from_expression(outer_type, expression);
2380 if (token.type == ',') {
2383 if (token.type != '}') {
2384 warningf(HERE, "excessive elements in initializer for type '%T'",
2387 /* TODO: eat , ... */
2392 /* descend into subtypes until expression matches type */
2394 orig_type = path->top_type;
2395 type = skip_typeref(orig_type);
2397 sub = initializer_from_expression(orig_type, expression);
2401 if (!is_type_valid(type)) {
2404 if (is_type_scalar(type)) {
2405 errorf(&expression->base.source_position,
2406 "expression '%E' doesn't match expected type '%T'",
2407 expression, orig_type);
2411 descend_into_subtype(path);
2415 /* update largest index of top array */
2416 const type_path_entry_t *first = &path->path[0];
2417 type_t *first_type = first->type;
2418 first_type = skip_typeref(first_type);
2419 if (is_type_array(first_type)) {
2420 size_t index = first->v.index;
2421 if (index > path->max_index)
2422 path->max_index = index;
2426 /* append to initializers list */
2427 ARR_APP1(initializer_t*, initializers, sub);
2430 if (env->declaration != NULL)
2431 warningf(HERE, "excess elements in struct initializer for '%Y'",
2432 env->declaration->symbol);
2434 warningf(HERE, "excess elements in struct initializer");
2438 if (token.type == '}') {
2442 if (token.type == '}') {
2447 /* advance to the next declaration if we are not at the end */
2448 advance_current_object(path, top_path_level);
2449 orig_type = path->top_type;
2450 if (orig_type != NULL)
2451 type = skip_typeref(orig_type);
2457 size_t len = ARR_LEN(initializers);
2458 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2459 initializer_t *result = allocate_ast_zero(size);
2460 result->kind = INITIALIZER_LIST;
2461 result->list.len = len;
2462 memcpy(&result->list.initializers, initializers,
2463 len * sizeof(initializers[0]));
2465 DEL_ARR_F(initializers);
2466 ascend_to(path, top_path_level+1);
2471 skip_initializers();
2472 DEL_ARR_F(initializers);
2473 ascend_to(path, top_path_level+1);
2478 * Parses an initializer. Parsers either a compound literal
2479 * (env->declaration == NULL) or an initializer of a declaration.
2481 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2483 type_t *type = skip_typeref(env->type);
2484 initializer_t *result = NULL;
2487 if (is_type_scalar(type)) {
2488 result = parse_scalar_initializer(type, env->must_be_constant);
2489 } else if (token.type == '{') {
2493 memset(&path, 0, sizeof(path));
2494 path.top_type = env->type;
2495 path.path = NEW_ARR_F(type_path_entry_t, 0);
2497 descend_into_subtype(&path);
2499 add_anchor_token('}');
2500 result = parse_sub_initializer(&path, env->type, 1, env);
2501 rem_anchor_token('}');
2503 max_index = path.max_index;
2504 DEL_ARR_F(path.path);
2508 /* parse_scalar_initializer() also works in this case: we simply
2509 * have an expression without {} around it */
2510 result = parse_scalar_initializer(type, env->must_be_constant);
2513 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2514 * the array type size */
2515 if (is_type_array(type) && type->array.size_expression == NULL
2516 && result != NULL) {
2518 switch (result->kind) {
2519 case INITIALIZER_LIST:
2520 size = max_index + 1;
2523 case INITIALIZER_STRING:
2524 size = result->string.string.size;
2527 case INITIALIZER_WIDE_STRING:
2528 size = result->wide_string.string.size;
2531 case INITIALIZER_DESIGNATOR:
2532 case INITIALIZER_VALUE:
2533 /* can happen for parse errors */
2538 internal_errorf(HERE, "invalid initializer type");
2541 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2542 cnst->base.type = type_size_t;
2543 cnst->conste.v.int_value = size;
2545 type_t *new_type = duplicate_type(type);
2547 new_type->array.size_expression = cnst;
2548 new_type->array.size_constant = true;
2549 new_type->array.size = size;
2550 env->type = new_type;
2558 static declaration_t *append_declaration(declaration_t *declaration);
2560 static declaration_t *parse_compound_type_specifier(bool is_struct)
2562 gnu_attribute_t *attributes = NULL;
2563 decl_modifiers_t modifiers = 0;
2570 symbol_t *symbol = NULL;
2571 declaration_t *declaration = NULL;
2573 if (token.type == T___attribute__) {
2574 modifiers |= parse_attributes(&attributes);
2577 if (token.type == T_IDENTIFIER) {
2578 symbol = token.v.symbol;
2581 namespace_t const namespc =
2582 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2583 declaration = get_declaration(symbol, namespc);
2584 if (declaration != NULL) {
2585 if (declaration->parent_scope != scope &&
2586 (token.type == '{' || token.type == ';')) {
2588 } else if (declaration->init.complete &&
2589 token.type == '{') {
2590 assert(symbol != NULL);
2591 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2592 is_struct ? "struct" : "union", symbol,
2593 &declaration->source_position);
2594 declaration->scope.declarations = NULL;
2597 } else if (token.type != '{') {
2599 parse_error_expected("while parsing struct type specifier",
2600 T_IDENTIFIER, '{', NULL);
2602 parse_error_expected("while parsing union type specifier",
2603 T_IDENTIFIER, '{', NULL);
2609 if (declaration == NULL) {
2610 declaration = allocate_declaration_zero();
2611 declaration->namespc =
2612 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2613 declaration->source_position = token.source_position;
2614 declaration->symbol = symbol;
2615 declaration->parent_scope = scope;
2616 if (symbol != NULL) {
2617 environment_push(declaration);
2619 append_declaration(declaration);
2622 if (token.type == '{') {
2623 declaration->init.complete = true;
2625 parse_compound_type_entries(declaration);
2626 modifiers |= parse_attributes(&attributes);
2629 declaration->modifiers |= modifiers;
2633 static void parse_enum_entries(type_t *const enum_type)
2637 if (token.type == '}') {
2639 errorf(HERE, "empty enum not allowed");
2643 add_anchor_token('}');
2645 if (token.type != T_IDENTIFIER) {
2646 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2648 rem_anchor_token('}');
2652 declaration_t *const entry = allocate_declaration_zero();
2653 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2654 entry->type = enum_type;
2655 entry->symbol = token.v.symbol;
2656 entry->source_position = token.source_position;
2659 if (token.type == '=') {
2661 expression_t *value = parse_constant_expression();
2663 value = create_implicit_cast(value, enum_type);
2664 entry->init.enum_value = value;
2669 record_declaration(entry);
2671 if (token.type != ',')
2674 } while(token.type != '}');
2675 rem_anchor_token('}');
2683 static type_t *parse_enum_specifier(void)
2685 gnu_attribute_t *attributes = NULL;
2686 declaration_t *declaration;
2690 if (token.type == T_IDENTIFIER) {
2691 symbol = token.v.symbol;
2694 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2695 } else if (token.type != '{') {
2696 parse_error_expected("while parsing enum type specifier",
2697 T_IDENTIFIER, '{', NULL);
2704 if (declaration == NULL) {
2705 declaration = allocate_declaration_zero();
2706 declaration->namespc = NAMESPACE_ENUM;
2707 declaration->source_position = token.source_position;
2708 declaration->symbol = symbol;
2709 declaration->parent_scope = scope;
2712 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2713 type->enumt.declaration = declaration;
2715 if (token.type == '{') {
2716 if (declaration->init.complete) {
2717 errorf(HERE, "multiple definitions of enum %Y", symbol);
2719 if (symbol != NULL) {
2720 environment_push(declaration);
2722 append_declaration(declaration);
2723 declaration->init.complete = true;
2725 parse_enum_entries(type);
2726 parse_attributes(&attributes);
2733 * if a symbol is a typedef to another type, return true
2735 static bool is_typedef_symbol(symbol_t *symbol)
2737 const declaration_t *const declaration =
2738 get_declaration(symbol, NAMESPACE_NORMAL);
2740 declaration != NULL &&
2741 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2744 static type_t *parse_typeof(void)
2751 add_anchor_token(')');
2753 expression_t *expression = NULL;
2756 switch(token.type) {
2757 case T___extension__:
2758 /* this can be a prefix to a typename or an expression */
2759 /* we simply eat it now. */
2762 } while(token.type == T___extension__);
2766 if (is_typedef_symbol(token.v.symbol)) {
2767 type = parse_typename();
2769 expression = parse_expression();
2770 type = expression->base.type;
2775 type = parse_typename();
2779 expression = parse_expression();
2780 type = expression->base.type;
2784 rem_anchor_token(')');
2787 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2788 typeof_type->typeoft.expression = expression;
2789 typeof_type->typeoft.typeof_type = type;
2796 typedef enum specifiers_t {
2797 SPECIFIER_SIGNED = 1 << 0,
2798 SPECIFIER_UNSIGNED = 1 << 1,
2799 SPECIFIER_LONG = 1 << 2,
2800 SPECIFIER_INT = 1 << 3,
2801 SPECIFIER_DOUBLE = 1 << 4,
2802 SPECIFIER_CHAR = 1 << 5,
2803 SPECIFIER_SHORT = 1 << 6,
2804 SPECIFIER_LONG_LONG = 1 << 7,
2805 SPECIFIER_FLOAT = 1 << 8,
2806 SPECIFIER_BOOL = 1 << 9,
2807 SPECIFIER_VOID = 1 << 10,
2808 SPECIFIER_INT8 = 1 << 11,
2809 SPECIFIER_INT16 = 1 << 12,
2810 SPECIFIER_INT32 = 1 << 13,
2811 SPECIFIER_INT64 = 1 << 14,
2812 SPECIFIER_INT128 = 1 << 15,
2813 SPECIFIER_COMPLEX = 1 << 16,
2814 SPECIFIER_IMAGINARY = 1 << 17,
2817 static type_t *create_builtin_type(symbol_t *const symbol,
2818 type_t *const real_type)
2820 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2821 type->builtin.symbol = symbol;
2822 type->builtin.real_type = real_type;
2824 type_t *result = typehash_insert(type);
2825 if (type != result) {
2832 static type_t *get_typedef_type(symbol_t *symbol)
2834 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2835 if (declaration == NULL ||
2836 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2839 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2840 type->typedeft.declaration = declaration;
2846 * check for the allowed MS alignment values.
2848 static bool check_elignment_value(long long intvalue) {
2849 if (intvalue < 1 || intvalue > 8192) {
2850 errorf(HERE, "illegal alignment value");
2853 unsigned v = (unsigned)intvalue;
2854 for(unsigned i = 1; i <= 8192; i += i) {
2858 errorf(HERE, "alignment must be power of two");
2862 #define DET_MOD(name, tag) do { \
2863 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2864 *modifiers |= tag; \
2867 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2869 decl_modifiers_t *modifiers = &specifiers->modifiers;
2872 if (token.type == T_restrict) {
2874 DET_MOD(restrict, DM_RESTRICT);
2876 } else if (token.type != T_IDENTIFIER)
2878 symbol_t *symbol = token.v.symbol;
2879 if (symbol == sym_align) {
2882 if (token.type != T_INTEGER)
2884 if (check_elignment_value(token.v.intvalue)) {
2885 if (specifiers->alignment != 0)
2886 warningf(HERE, "align used more than once");
2887 specifiers->alignment = (unsigned char)token.v.intvalue;
2891 } else if (symbol == sym_allocate) {
2894 if (token.type != T_IDENTIFIER)
2896 (void)token.v.symbol;
2898 } else if (symbol == sym_dllimport) {
2900 DET_MOD(dllimport, DM_DLLIMPORT);
2901 } else if (symbol == sym_dllexport) {
2903 DET_MOD(dllexport, DM_DLLEXPORT);
2904 } else if (symbol == sym_thread) {
2906 DET_MOD(thread, DM_THREAD);
2907 } else if (symbol == sym_naked) {
2909 DET_MOD(naked, DM_NAKED);
2910 } else if (symbol == sym_noinline) {
2912 DET_MOD(noinline, DM_NOINLINE);
2913 } else if (symbol == sym_noreturn) {
2915 DET_MOD(noreturn, DM_NORETURN);
2916 } else if (symbol == sym_nothrow) {
2918 DET_MOD(nothrow, DM_NOTHROW);
2919 } else if (symbol == sym_novtable) {
2921 DET_MOD(novtable, DM_NOVTABLE);
2922 } else if (symbol == sym_property) {
2926 bool is_get = false;
2927 if (token.type != T_IDENTIFIER)
2929 if (token.v.symbol == sym_get) {
2931 } else if (token.v.symbol == sym_put) {
2933 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2938 if (token.type != T_IDENTIFIER)
2941 if (specifiers->get_property_sym != NULL) {
2942 errorf(HERE, "get property name already specified");
2944 specifiers->get_property_sym = token.v.symbol;
2947 if (specifiers->put_property_sym != NULL) {
2948 errorf(HERE, "put property name already specified");
2950 specifiers->put_property_sym = token.v.symbol;
2954 if (token.type == ',') {
2961 } else if (symbol == sym_selectany) {
2963 DET_MOD(selectany, DM_SELECTANY);
2964 } else if (symbol == sym_uuid) {
2967 if (token.type != T_STRING_LITERAL)
2971 } else if (symbol == sym_deprecated) {
2973 if (specifiers->deprecated != 0)
2974 warningf(HERE, "deprecated used more than once");
2975 specifiers->deprecated = 1;
2976 if (token.type == '(') {
2978 if (token.type == T_STRING_LITERAL) {
2979 specifiers->deprecated_string = token.v.string.begin;
2982 errorf(HERE, "string literal expected");
2986 } else if (symbol == sym_noalias) {
2988 DET_MOD(noalias, DM_NOALIAS);
2990 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2992 if (token.type == '(')
2996 if (token.type == ',')
3003 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3005 type_t *type = NULL;
3006 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3007 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3008 unsigned type_specifiers = 0;
3011 specifiers->source_position = token.source_position;
3014 specifiers->modifiers
3015 |= parse_attributes(&specifiers->gnu_attributes);
3016 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3017 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3019 switch(token.type) {
3022 #define MATCH_STORAGE_CLASS(token, class) \
3024 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3025 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3027 specifiers->declared_storage_class = class; \
3031 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3032 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3033 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3034 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3035 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3040 add_anchor_token(')');
3041 parse_microsoft_extended_decl_modifier(specifiers);
3042 rem_anchor_token(')');
3047 switch (specifiers->declared_storage_class) {
3048 case STORAGE_CLASS_NONE:
3049 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3052 case STORAGE_CLASS_EXTERN:
3053 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3056 case STORAGE_CLASS_STATIC:
3057 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3061 errorf(HERE, "multiple storage classes in declaration specifiers");
3067 /* type qualifiers */
3068 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3070 qualifiers |= qualifier; \
3074 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3075 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3076 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3077 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3078 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3079 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3080 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3081 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3083 case T___extension__:
3088 /* type specifiers */
3089 #define MATCH_SPECIFIER(token, specifier, name) \
3092 if (type_specifiers & specifier) { \
3093 errorf(HERE, "multiple " name " type specifiers given"); \
3095 type_specifiers |= specifier; \
3099 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
3100 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
3101 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
3102 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
3103 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
3104 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
3105 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
3106 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
3107 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
3108 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
3109 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
3110 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
3111 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
3112 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
3113 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
3114 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
3116 case T__forceinline:
3117 /* only in microsoft mode */
3118 specifiers->modifiers |= DM_FORCEINLINE;
3122 specifiers->is_inline = true;
3127 if (type_specifiers & SPECIFIER_LONG_LONG) {
3128 errorf(HERE, "multiple type specifiers given");
3129 } else if (type_specifiers & SPECIFIER_LONG) {
3130 type_specifiers |= SPECIFIER_LONG_LONG;
3132 type_specifiers |= SPECIFIER_LONG;
3137 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3139 type->compound.declaration = parse_compound_type_specifier(true);
3143 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3144 type->compound.declaration = parse_compound_type_specifier(false);
3145 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3146 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3150 type = parse_enum_specifier();
3153 type = parse_typeof();
3155 case T___builtin_va_list:
3156 type = duplicate_type(type_valist);
3160 case T_IDENTIFIER: {
3161 /* only parse identifier if we haven't found a type yet */
3162 if (type != NULL || type_specifiers != 0)
3163 goto finish_specifiers;
3165 type_t *typedef_type = get_typedef_type(token.v.symbol);
3167 if (typedef_type == NULL)
3168 goto finish_specifiers;
3171 type = typedef_type;
3175 /* function specifier */
3177 goto finish_specifiers;
3184 atomic_type_kind_t atomic_type;
3186 /* match valid basic types */
3187 switch(type_specifiers) {
3188 case SPECIFIER_VOID:
3189 atomic_type = ATOMIC_TYPE_VOID;
3191 case SPECIFIER_CHAR:
3192 atomic_type = ATOMIC_TYPE_CHAR;
3194 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3195 atomic_type = ATOMIC_TYPE_SCHAR;
3197 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3198 atomic_type = ATOMIC_TYPE_UCHAR;
3200 case SPECIFIER_SHORT:
3201 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3202 case SPECIFIER_SHORT | SPECIFIER_INT:
3203 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3204 atomic_type = ATOMIC_TYPE_SHORT;
3206 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3207 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3208 atomic_type = ATOMIC_TYPE_USHORT;
3211 case SPECIFIER_SIGNED:
3212 case SPECIFIER_SIGNED | SPECIFIER_INT:
3213 atomic_type = ATOMIC_TYPE_INT;
3215 case SPECIFIER_UNSIGNED:
3216 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3217 atomic_type = ATOMIC_TYPE_UINT;
3219 case SPECIFIER_LONG:
3220 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3221 case SPECIFIER_LONG | SPECIFIER_INT:
3222 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3223 atomic_type = ATOMIC_TYPE_LONG;
3225 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3226 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3227 atomic_type = ATOMIC_TYPE_ULONG;
3229 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3230 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3231 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3232 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3234 atomic_type = ATOMIC_TYPE_LONGLONG;
3236 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3237 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3239 atomic_type = ATOMIC_TYPE_ULONGLONG;
3242 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3243 atomic_type = unsigned_int8_type_kind;
3246 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3247 atomic_type = unsigned_int16_type_kind;
3250 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3251 atomic_type = unsigned_int32_type_kind;
3254 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3255 atomic_type = unsigned_int64_type_kind;
3258 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3259 atomic_type = unsigned_int128_type_kind;
3262 case SPECIFIER_INT8:
3263 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3264 atomic_type = int8_type_kind;
3267 case SPECIFIER_INT16:
3268 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3269 atomic_type = int16_type_kind;
3272 case SPECIFIER_INT32:
3273 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3274 atomic_type = int32_type_kind;
3277 case SPECIFIER_INT64:
3278 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3279 atomic_type = int64_type_kind;
3282 case SPECIFIER_INT128:
3283 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3284 atomic_type = int128_type_kind;
3287 case SPECIFIER_FLOAT:
3288 atomic_type = ATOMIC_TYPE_FLOAT;
3290 case SPECIFIER_DOUBLE:
3291 atomic_type = ATOMIC_TYPE_DOUBLE;
3293 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3294 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3296 case SPECIFIER_BOOL:
3297 atomic_type = ATOMIC_TYPE_BOOL;
3299 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3300 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3301 atomic_type = ATOMIC_TYPE_FLOAT;
3303 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3304 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3305 atomic_type = ATOMIC_TYPE_DOUBLE;
3307 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3308 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3309 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3312 /* invalid specifier combination, give an error message */
3313 if (type_specifiers == 0) {
3314 if (! strict_mode) {
3315 if (warning.implicit_int) {
3316 warningf(HERE, "no type specifiers in declaration, using 'int'");
3318 atomic_type = ATOMIC_TYPE_INT;
3321 errorf(HERE, "no type specifiers given in declaration");
3323 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3324 (type_specifiers & SPECIFIER_UNSIGNED)) {
3325 errorf(HERE, "signed and unsigned specifiers gives");
3326 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3327 errorf(HERE, "only integer types can be signed or unsigned");
3329 errorf(HERE, "multiple datatypes in declaration");
3331 atomic_type = ATOMIC_TYPE_INVALID;
3334 if (type_specifiers & SPECIFIER_COMPLEX &&
3335 atomic_type != ATOMIC_TYPE_INVALID) {
3336 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3337 type->complex.akind = atomic_type;
3338 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3339 atomic_type != ATOMIC_TYPE_INVALID) {
3340 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3341 type->imaginary.akind = atomic_type;
3343 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3344 type->atomic.akind = atomic_type;
3348 if (type_specifiers != 0) {
3349 errorf(HERE, "multiple datatypes in declaration");
3353 /* FIXME: check type qualifiers here */
3355 type->base.qualifiers = qualifiers;
3356 type->base.modifiers = modifiers;
3358 type_t *result = typehash_insert(type);
3359 if (newtype && result != type) {
3363 specifiers->type = result;
3368 static type_qualifiers_t parse_type_qualifiers(void)
3370 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3373 switch(token.type) {
3374 /* type qualifiers */
3375 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3376 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3377 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3378 /* microsoft extended type modifiers */
3379 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3380 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3381 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3382 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3383 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3391 static declaration_t *parse_identifier_list(void)
3393 declaration_t *declarations = NULL;
3394 declaration_t *last_declaration = NULL;
3396 declaration_t *const declaration = allocate_declaration_zero();
3397 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3398 declaration->source_position = token.source_position;
3399 declaration->symbol = token.v.symbol;
3402 if (last_declaration != NULL) {
3403 last_declaration->next = declaration;
3405 declarations = declaration;
3407 last_declaration = declaration;
3409 if (token.type != ',') {
3413 } while(token.type == T_IDENTIFIER);
3415 return declarations;
3418 static type_t *automatic_type_conversion(type_t *orig_type);
3420 static void semantic_parameter(declaration_t *declaration)
3422 /* TODO: improve error messages */
3424 if (declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3425 errorf(HERE, "typedef not allowed in parameter list");
3426 } else if (declaration->declared_storage_class != STORAGE_CLASS_NONE
3427 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3428 errorf(HERE, "parameter may only have none or register storage class");
3431 type_t *const orig_type = declaration->type;
3432 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3433 * sugar. Turn it into a pointer.
3434 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3435 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3437 type_t *const type = automatic_type_conversion(orig_type);
3438 declaration->type = type;
3440 if (is_type_incomplete(skip_typeref(type))) {
3441 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3442 orig_type, declaration->symbol);
3446 static declaration_t *parse_parameter(void)
3448 declaration_specifiers_t specifiers;
3449 memset(&specifiers, 0, sizeof(specifiers));
3451 parse_declaration_specifiers(&specifiers);
3453 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3458 static declaration_t *parse_parameters(function_type_t *type)
3460 declaration_t *declarations = NULL;
3463 add_anchor_token(')');
3464 int saved_comma_state = save_and_reset_anchor_state(',');
3466 if (token.type == T_IDENTIFIER) {
3467 symbol_t *symbol = token.v.symbol;
3468 if (!is_typedef_symbol(symbol)) {
3469 type->kr_style_parameters = true;
3470 declarations = parse_identifier_list();
3471 goto parameters_finished;
3475 if (token.type == ')') {
3476 type->unspecified_parameters = 1;
3477 goto parameters_finished;
3480 declaration_t *declaration;
3481 declaration_t *last_declaration = NULL;
3482 function_parameter_t *parameter;
3483 function_parameter_t *last_parameter = NULL;
3486 switch(token.type) {
3490 goto parameters_finished;
3493 case T___extension__:
3495 declaration = parse_parameter();
3497 /* func(void) is not a parameter */
3498 if (last_parameter == NULL
3499 && token.type == ')'
3500 && declaration->symbol == NULL
3501 && skip_typeref(declaration->type) == type_void) {
3502 goto parameters_finished;
3504 semantic_parameter(declaration);
3506 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3507 memset(parameter, 0, sizeof(parameter[0]));
3508 parameter->type = declaration->type;
3510 if (last_parameter != NULL) {
3511 last_declaration->next = declaration;
3512 last_parameter->next = parameter;
3514 type->parameters = parameter;
3515 declarations = declaration;
3517 last_parameter = parameter;
3518 last_declaration = declaration;
3522 goto parameters_finished;
3524 if (token.type != ',') {
3525 goto parameters_finished;
3531 parameters_finished:
3532 rem_anchor_token(')');
3535 restore_anchor_state(',', saved_comma_state);
3536 return declarations;
3539 restore_anchor_state(',', saved_comma_state);
3543 typedef enum construct_type_kind_t {
3548 } construct_type_kind_t;
3550 typedef struct construct_type_t construct_type_t;
3551 struct construct_type_t {
3552 construct_type_kind_t kind;
3553 construct_type_t *next;
3556 typedef struct parsed_pointer_t parsed_pointer_t;
3557 struct parsed_pointer_t {
3558 construct_type_t construct_type;
3559 type_qualifiers_t type_qualifiers;
3562 typedef struct construct_function_type_t construct_function_type_t;
3563 struct construct_function_type_t {
3564 construct_type_t construct_type;
3565 type_t *function_type;
3568 typedef struct parsed_array_t parsed_array_t;
3569 struct parsed_array_t {
3570 construct_type_t construct_type;
3571 type_qualifiers_t type_qualifiers;
3577 typedef struct construct_base_type_t construct_base_type_t;
3578 struct construct_base_type_t {
3579 construct_type_t construct_type;
3583 static construct_type_t *parse_pointer_declarator(void)
3587 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3588 memset(pointer, 0, sizeof(pointer[0]));
3589 pointer->construct_type.kind = CONSTRUCT_POINTER;
3590 pointer->type_qualifiers = parse_type_qualifiers();
3592 return (construct_type_t*) pointer;
3595 static construct_type_t *parse_array_declarator(void)
3598 add_anchor_token(']');
3600 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3601 memset(array, 0, sizeof(array[0]));
3602 array->construct_type.kind = CONSTRUCT_ARRAY;
3604 if (token.type == T_static) {
3605 array->is_static = true;
3609 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3610 if (type_qualifiers != 0) {
3611 if (token.type == T_static) {
3612 array->is_static = true;
3616 array->type_qualifiers = type_qualifiers;
3618 if (token.type == '*' && look_ahead(1)->type == ']') {
3619 array->is_variable = true;
3621 } else if (token.type != ']') {
3622 array->size = parse_assignment_expression();
3625 rem_anchor_token(']');
3628 return (construct_type_t*) array;
3633 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3636 if (declaration != NULL) {
3637 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3639 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3641 if (mask & (mask-1)) {
3642 const char *first = NULL, *second = NULL;
3644 /* more than one calling convention set */
3645 if (declaration->modifiers & DM_CDECL) {
3646 if (first == NULL) first = "cdecl";
3647 else if (second == NULL) second = "cdecl";
3649 if (declaration->modifiers & DM_STDCALL) {
3650 if (first == NULL) first = "stdcall";
3651 else if (second == NULL) second = "stdcall";
3653 if (declaration->modifiers & DM_FASTCALL) {
3654 if (first == NULL) first = "faslcall";
3655 else if (second == NULL) second = "fastcall";
3657 if (declaration->modifiers & DM_THISCALL) {
3658 if (first == NULL) first = "thiscall";
3659 else if (second == NULL) second = "thiscall";
3661 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3664 if (declaration->modifiers & DM_CDECL)
3665 type->function.calling_convention = CC_CDECL;
3666 else if (declaration->modifiers & DM_STDCALL)
3667 type->function.calling_convention = CC_STDCALL;
3668 else if (declaration->modifiers & DM_FASTCALL)
3669 type->function.calling_convention = CC_FASTCALL;
3670 else if (declaration->modifiers & DM_THISCALL)
3671 type->function.calling_convention = CC_THISCALL;
3673 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3676 declaration_t *parameters = parse_parameters(&type->function);
3677 if (declaration != NULL) {
3678 declaration->scope.declarations = parameters;
3681 construct_function_type_t *construct_function_type =
3682 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3683 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3684 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3685 construct_function_type->function_type = type;
3687 return &construct_function_type->construct_type;
3690 static void fix_declaration_type(declaration_t *declaration)
3692 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3693 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3695 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3696 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3698 if (declaration->type->base.modifiers == type_modifiers)
3701 type_t *copy = duplicate_type(declaration->type);
3702 copy->base.modifiers = type_modifiers;
3704 type_t *result = typehash_insert(copy);
3705 if (result != copy) {
3706 obstack_free(type_obst, copy);
3709 declaration->type = result;
3712 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3713 bool may_be_abstract)
3715 /* construct a single linked list of construct_type_t's which describe
3716 * how to construct the final declarator type */
3717 construct_type_t *first = NULL;
3718 construct_type_t *last = NULL;
3719 gnu_attribute_t *attributes = NULL;
3721 decl_modifiers_t modifiers = parse_attributes(&attributes);
3724 while(token.type == '*') {
3725 construct_type_t *type = parse_pointer_declarator();
3735 /* TODO: find out if this is correct */
3736 modifiers |= parse_attributes(&attributes);
3739 construct_type_t *inner_types = NULL;
3741 switch(token.type) {
3743 if (declaration == NULL) {
3744 errorf(HERE, "no identifier expected in typename");
3746 declaration->symbol = token.v.symbol;
3747 declaration->source_position = token.source_position;
3753 add_anchor_token(')');
3754 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3755 rem_anchor_token(')');
3759 if (may_be_abstract)
3761 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3762 /* avoid a loop in the outermost scope, because eat_statement doesn't
3764 if (token.type == '}' && current_function == NULL) {
3772 construct_type_t *p = last;
3775 construct_type_t *type;
3776 switch(token.type) {
3778 type = parse_function_declarator(declaration);
3781 type = parse_array_declarator();
3784 goto declarator_finished;
3787 /* insert in the middle of the list (behind p) */
3789 type->next = p->next;
3800 declarator_finished:
3801 /* append inner_types at the end of the list, we don't to set last anymore
3802 * as it's not needed anymore */
3804 assert(first == NULL);
3805 first = inner_types;
3807 last->next = inner_types;
3815 static void parse_declaration_attributes(declaration_t *declaration)
3817 gnu_attribute_t *attributes = NULL;
3818 decl_modifiers_t modifiers = parse_attributes(&attributes);
3820 if (declaration == NULL)
3823 declaration->modifiers |= modifiers;
3824 /* check if we have these stupid mode attributes... */
3825 type_t *old_type = declaration->type;
3826 if (old_type == NULL)
3829 gnu_attribute_t *attribute = attributes;
3830 for ( ; attribute != NULL; attribute = attribute->next) {
3831 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3834 atomic_type_kind_t akind = attribute->u.akind;
3835 if (!is_type_signed(old_type)) {
3837 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3838 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3839 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3840 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3842 panic("invalid akind in mode attribute");
3846 = make_atomic_type(akind, old_type->base.qualifiers);
3850 static type_t *construct_declarator_type(construct_type_t *construct_list,
3853 construct_type_t *iter = construct_list;
3854 for( ; iter != NULL; iter = iter->next) {
3855 switch(iter->kind) {
3856 case CONSTRUCT_INVALID:
3857 internal_errorf(HERE, "invalid type construction found");
3858 case CONSTRUCT_FUNCTION: {
3859 construct_function_type_t *construct_function_type
3860 = (construct_function_type_t*) iter;
3862 type_t *function_type = construct_function_type->function_type;
3864 function_type->function.return_type = type;
3866 type_t *skipped_return_type = skip_typeref(type);
3867 if (is_type_function(skipped_return_type)) {
3868 errorf(HERE, "function returning function is not allowed");
3869 type = type_error_type;
3870 } else if (is_type_array(skipped_return_type)) {
3871 errorf(HERE, "function returning array is not allowed");
3872 type = type_error_type;
3874 type = function_type;
3879 case CONSTRUCT_POINTER: {
3880 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3881 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3882 pointer_type->pointer.points_to = type;
3883 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3885 type = pointer_type;
3889 case CONSTRUCT_ARRAY: {
3890 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3891 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3893 expression_t *size_expression = parsed_array->size;
3894 if (size_expression != NULL) {
3896 = create_implicit_cast(size_expression, type_size_t);
3899 array_type->base.qualifiers = parsed_array->type_qualifiers;
3900 array_type->array.element_type = type;
3901 array_type->array.is_static = parsed_array->is_static;
3902 array_type->array.is_variable = parsed_array->is_variable;
3903 array_type->array.size_expression = size_expression;
3905 if (size_expression != NULL) {
3906 if (is_constant_expression(size_expression)) {
3907 array_type->array.size_constant = true;
3908 array_type->array.size
3909 = fold_constant(size_expression);
3911 array_type->array.is_vla = true;
3915 type_t *skipped_type = skip_typeref(type);
3916 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3917 errorf(HERE, "array of void is not allowed");
3918 type = type_error_type;
3926 type_t *hashed_type = typehash_insert(type);
3927 if (hashed_type != type) {
3928 /* the function type was constructed earlier freeing it here will
3929 * destroy other types... */
3930 if (iter->kind != CONSTRUCT_FUNCTION) {
3940 static declaration_t *parse_declarator(
3941 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3943 declaration_t *const declaration = allocate_declaration_zero();
3944 declaration->declared_storage_class = specifiers->declared_storage_class;
3945 declaration->modifiers = specifiers->modifiers;
3946 declaration->deprecated = specifiers->deprecated;
3947 declaration->deprecated_string = specifiers->deprecated_string;
3948 declaration->get_property_sym = specifiers->get_property_sym;
3949 declaration->put_property_sym = specifiers->put_property_sym;
3950 declaration->is_inline = specifiers->is_inline;
3952 declaration->storage_class = specifiers->declared_storage_class;
3953 if (declaration->storage_class == STORAGE_CLASS_NONE
3954 && scope != global_scope) {
3955 declaration->storage_class = STORAGE_CLASS_AUTO;
3958 if (specifiers->alignment != 0) {
3959 /* TODO: add checks here */
3960 declaration->alignment = specifiers->alignment;
3963 construct_type_t *construct_type
3964 = parse_inner_declarator(declaration, may_be_abstract);
3965 type_t *const type = specifiers->type;
3966 declaration->type = construct_declarator_type(construct_type, type);
3968 parse_declaration_attributes(declaration);
3970 fix_declaration_type(declaration);
3972 if (construct_type != NULL) {
3973 obstack_free(&temp_obst, construct_type);
3979 static type_t *parse_abstract_declarator(type_t *base_type)
3981 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3983 type_t *result = construct_declarator_type(construct_type, base_type);
3984 if (construct_type != NULL) {
3985 obstack_free(&temp_obst, construct_type);
3991 static declaration_t *append_declaration(declaration_t* const declaration)
3993 if (last_declaration != NULL) {
3994 last_declaration->next = declaration;
3996 scope->declarations = declaration;
3998 last_declaration = declaration;
4003 * Check if the declaration of main is suspicious. main should be a
4004 * function with external linkage, returning int, taking either zero
4005 * arguments, two, or three arguments of appropriate types, ie.
4007 * int main([ int argc, char **argv [, char **env ] ]).
4009 * @param decl the declaration to check
4010 * @param type the function type of the declaration
4012 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4014 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4015 warningf(&decl->source_position,
4016 "'main' is normally a non-static function");
4018 if (skip_typeref(func_type->return_type) != type_int) {
4019 warningf(&decl->source_position,
4020 "return type of 'main' should be 'int', but is '%T'",
4021 func_type->return_type);
4023 const function_parameter_t *parm = func_type->parameters;
4025 type_t *const first_type = parm->type;
4026 if (!types_compatible(skip_typeref(first_type), type_int)) {
4027 warningf(&decl->source_position,
4028 "first argument of 'main' should be 'int', but is '%T'", first_type);
4032 type_t *const second_type = parm->type;
4033 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4034 warningf(&decl->source_position,
4035 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4039 type_t *const third_type = parm->type;
4040 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4041 warningf(&decl->source_position,
4042 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4046 goto warn_arg_count;
4050 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4056 * Check if a symbol is the equal to "main".
4058 static bool is_sym_main(const symbol_t *const sym)
4060 return strcmp(sym->string, "main") == 0;
4063 static declaration_t *internal_record_declaration(
4064 declaration_t *const declaration,
4065 const bool is_definition)
4067 const symbol_t *const symbol = declaration->symbol;
4068 const namespace_t namespc = (namespace_t)declaration->namespc;
4070 assert(declaration->symbol != NULL);
4071 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4073 type_t *const orig_type = declaration->type;
4074 type_t *const type = skip_typeref(orig_type);
4075 if (is_type_function(type) &&
4076 type->function.unspecified_parameters &&
4077 warning.strict_prototypes &&
4078 previous_declaration == NULL) {
4079 warningf(&declaration->source_position,
4080 "function declaration '%#T' is not a prototype",
4081 orig_type, declaration->symbol);
4084 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4085 check_type_of_main(declaration, &type->function);
4088 assert(declaration != previous_declaration);
4089 if (previous_declaration != NULL
4090 && previous_declaration->parent_scope == scope) {
4091 /* can happen for K&R style declarations */
4092 if (previous_declaration->type == NULL) {
4093 previous_declaration->type = declaration->type;
4096 const type_t *prev_type = skip_typeref(previous_declaration->type);
4097 if (!types_compatible(type, prev_type)) {
4098 errorf(&declaration->source_position,
4099 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4100 orig_type, symbol, previous_declaration->type, symbol,
4101 &previous_declaration->source_position);
4103 unsigned old_storage_class = previous_declaration->storage_class;
4104 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4105 errorf(&declaration->source_position,
4106 "redeclaration of enum entry '%Y' (declared %P)",
4107 symbol, &previous_declaration->source_position);
4108 return previous_declaration;
4111 if (warning.redundant_decls &&
4113 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4114 !(previous_declaration->modifiers & DM_USED) &&
4115 !previous_declaration->used) {
4116 warningf(&previous_declaration->source_position,
4117 "unnecessary static forward declaration for '%#T'",
4118 previous_declaration->type, symbol);
4121 unsigned new_storage_class = declaration->storage_class;
4123 if (is_type_incomplete(prev_type)) {
4124 previous_declaration->type = type;
4128 /* pretend no storage class means extern for function
4129 * declarations (except if the previous declaration is neither
4130 * none nor extern) */
4131 if (is_type_function(type)) {
4132 if (prev_type->function.unspecified_parameters) {
4133 previous_declaration->type = type;
4137 switch (old_storage_class) {
4138 case STORAGE_CLASS_NONE:
4139 old_storage_class = STORAGE_CLASS_EXTERN;
4142 case STORAGE_CLASS_EXTERN:
4143 if (is_definition) {
4144 if (warning.missing_prototypes &&
4145 prev_type->function.unspecified_parameters &&
4146 !is_sym_main(symbol)) {
4147 warningf(&declaration->source_position,
4148 "no previous prototype for '%#T'",
4151 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4152 new_storage_class = STORAGE_CLASS_EXTERN;
4161 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4162 new_storage_class == STORAGE_CLASS_EXTERN) {
4163 warn_redundant_declaration:
4164 if (!is_definition &&
4165 warning.redundant_decls &&
4166 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4167 warningf(&declaration->source_position,
4168 "redundant declaration for '%Y' (declared %P)",
4169 symbol, &previous_declaration->source_position);
4171 } else if (current_function == NULL) {
4172 if (old_storage_class != STORAGE_CLASS_STATIC &&
4173 new_storage_class == STORAGE_CLASS_STATIC) {
4174 errorf(&declaration->source_position,
4175 "static declaration of '%Y' follows non-static declaration (declared %P)",
4176 symbol, &previous_declaration->source_position);
4177 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4178 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4179 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4181 goto warn_redundant_declaration;
4183 } else if (old_storage_class == new_storage_class) {
4184 errorf(&declaration->source_position,
4185 "redeclaration of '%Y' (declared %P)",
4186 symbol, &previous_declaration->source_position);
4188 errorf(&declaration->source_position,
4189 "redeclaration of '%Y' with different linkage (declared %P)",
4190 symbol, &previous_declaration->source_position);
4194 if (declaration->is_inline)
4195 previous_declaration->is_inline = true;
4196 return previous_declaration;
4197 } else if (is_type_function(type)) {
4198 if (is_definition &&
4199 declaration->storage_class != STORAGE_CLASS_STATIC) {
4200 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4201 warningf(&declaration->source_position,
4202 "no previous prototype for '%#T'", orig_type, symbol);
4203 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4204 warningf(&declaration->source_position,
4205 "no previous declaration for '%#T'", orig_type,
4210 if (warning.missing_declarations &&
4211 scope == global_scope && (
4212 declaration->storage_class == STORAGE_CLASS_NONE ||
4213 declaration->storage_class == STORAGE_CLASS_THREAD
4215 warningf(&declaration->source_position,
4216 "no previous declaration for '%#T'", orig_type, symbol);
4220 assert(declaration->parent_scope == NULL);
4221 assert(scope != NULL);
4223 declaration->parent_scope = scope;
4225 environment_push(declaration);
4226 return append_declaration(declaration);
4229 static declaration_t *record_declaration(declaration_t *declaration)
4231 return internal_record_declaration(declaration, false);
4234 static declaration_t *record_definition(declaration_t *declaration)
4236 return internal_record_declaration(declaration, true);
4239 static void parser_error_multiple_definition(declaration_t *declaration,
4240 const source_position_t *source_position)
4242 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4243 declaration->symbol, &declaration->source_position);
4246 static bool is_declaration_specifier(const token_t *token,
4247 bool only_specifiers_qualifiers)
4249 switch(token->type) {
4254 return is_typedef_symbol(token->v.symbol);
4256 case T___extension__:
4258 return !only_specifiers_qualifiers;
4265 static void parse_init_declarator_rest(declaration_t *declaration)
4269 type_t *orig_type = declaration->type;
4270 type_t *type = skip_typeref(orig_type);
4272 if (declaration->init.initializer != NULL) {
4273 parser_error_multiple_definition(declaration, HERE);
4276 bool must_be_constant = false;
4277 if (declaration->storage_class == STORAGE_CLASS_STATIC
4278 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4279 || declaration->parent_scope == global_scope) {
4280 must_be_constant = true;
4283 parse_initializer_env_t env;
4284 env.type = orig_type;
4285 env.must_be_constant = must_be_constant;
4286 env.declaration = declaration;
4288 initializer_t *initializer = parse_initializer(&env);
4290 if (env.type != orig_type) {
4291 orig_type = env.type;
4292 type = skip_typeref(orig_type);
4293 declaration->type = env.type;
4296 if (is_type_function(type)) {
4297 errorf(&declaration->source_position,
4298 "initializers not allowed for function types at declator '%Y' (type '%T')",
4299 declaration->symbol, orig_type);
4301 declaration->init.initializer = initializer;
4305 /* parse rest of a declaration without any declarator */
4306 static void parse_anonymous_declaration_rest(
4307 const declaration_specifiers_t *specifiers,
4308 parsed_declaration_func finished_declaration)
4312 declaration_t *const declaration = allocate_declaration_zero();
4313 declaration->type = specifiers->type;
4314 declaration->declared_storage_class = specifiers->declared_storage_class;
4315 declaration->source_position = specifiers->source_position;
4316 declaration->modifiers = specifiers->modifiers;
4318 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4319 warningf(&declaration->source_position,
4320 "useless storage class in empty declaration");
4322 declaration->storage_class = STORAGE_CLASS_NONE;
4324 type_t *type = declaration->type;
4325 switch (type->kind) {
4326 case TYPE_COMPOUND_STRUCT:
4327 case TYPE_COMPOUND_UNION: {
4328 if (type->compound.declaration->symbol == NULL) {
4329 warningf(&declaration->source_position,
4330 "unnamed struct/union that defines no instances");
4339 warningf(&declaration->source_position, "empty declaration");
4343 finished_declaration(declaration);
4346 static void parse_declaration_rest(declaration_t *ndeclaration,
4347 const declaration_specifiers_t *specifiers,
4348 parsed_declaration_func finished_declaration)
4350 add_anchor_token(';');
4351 add_anchor_token('=');
4352 add_anchor_token(',');
4354 declaration_t *declaration = finished_declaration(ndeclaration);
4356 type_t *orig_type = declaration->type;
4357 type_t *type = skip_typeref(orig_type);
4359 if (type->kind != TYPE_FUNCTION &&
4360 declaration->is_inline &&
4361 is_type_valid(type)) {
4362 warningf(&declaration->source_position,
4363 "variable '%Y' declared 'inline'\n", declaration->symbol);
4366 if (token.type == '=') {
4367 parse_init_declarator_rest(declaration);
4370 if (token.type != ',')
4374 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4379 rem_anchor_token(';');
4380 rem_anchor_token('=');
4381 rem_anchor_token(',');
4384 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4386 symbol_t *symbol = declaration->symbol;
4387 if (symbol == NULL) {
4388 errorf(HERE, "anonymous declaration not valid as function parameter");
4391 namespace_t namespc = (namespace_t) declaration->namespc;
4392 if (namespc != NAMESPACE_NORMAL) {
4393 return record_declaration(declaration);
4396 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4397 if (previous_declaration == NULL ||
4398 previous_declaration->parent_scope != scope) {
4399 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4404 if (previous_declaration->type == NULL) {
4405 previous_declaration->type = declaration->type;
4406 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4407 previous_declaration->storage_class = declaration->storage_class;
4408 previous_declaration->parent_scope = scope;
4409 return previous_declaration;
4411 return record_declaration(declaration);
4415 static void parse_declaration(parsed_declaration_func finished_declaration)
4417 declaration_specifiers_t specifiers;
4418 memset(&specifiers, 0, sizeof(specifiers));
4419 parse_declaration_specifiers(&specifiers);
4421 if (token.type == ';') {
4422 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4424 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4425 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4429 static type_t *get_default_promoted_type(type_t *orig_type)
4431 type_t *result = orig_type;
4433 type_t *type = skip_typeref(orig_type);
4434 if (is_type_integer(type)) {
4435 result = promote_integer(type);
4436 } else if (type == type_float) {
4437 result = type_double;
4443 static void parse_kr_declaration_list(declaration_t *declaration)
4445 type_t *type = skip_typeref(declaration->type);
4446 if (!is_type_function(type))
4449 if (!type->function.kr_style_parameters)
4452 /* push function parameters */
4453 int top = environment_top();
4454 scope_t *last_scope = scope;
4455 set_scope(&declaration->scope);
4457 declaration_t *parameter = declaration->scope.declarations;
4458 for ( ; parameter != NULL; parameter = parameter->next) {
4459 assert(parameter->parent_scope == NULL);
4460 parameter->parent_scope = scope;
4461 environment_push(parameter);
4464 /* parse declaration list */
4465 while (is_declaration_specifier(&token, false)) {
4466 parse_declaration(finished_kr_declaration);
4469 /* pop function parameters */
4470 assert(scope == &declaration->scope);
4471 set_scope(last_scope);
4472 environment_pop_to(top);
4474 /* update function type */
4475 type_t *new_type = duplicate_type(type);
4477 function_parameter_t *parameters = NULL;
4478 function_parameter_t *last_parameter = NULL;
4480 declaration_t *parameter_declaration = declaration->scope.declarations;
4481 for( ; parameter_declaration != NULL;
4482 parameter_declaration = parameter_declaration->next) {
4483 type_t *parameter_type = parameter_declaration->type;
4484 if (parameter_type == NULL) {
4486 errorf(HERE, "no type specified for function parameter '%Y'",
4487 parameter_declaration->symbol);
4489 if (warning.implicit_int) {
4490 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4491 parameter_declaration->symbol);
4493 parameter_type = type_int;
4494 parameter_declaration->type = parameter_type;
4498 semantic_parameter(parameter_declaration);
4499 parameter_type = parameter_declaration->type;
4502 * we need the default promoted types for the function type
4504 parameter_type = get_default_promoted_type(parameter_type);
4506 function_parameter_t *function_parameter
4507 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4508 memset(function_parameter, 0, sizeof(function_parameter[0]));
4510 function_parameter->type = parameter_type;
4511 if (last_parameter != NULL) {
4512 last_parameter->next = function_parameter;
4514 parameters = function_parameter;
4516 last_parameter = function_parameter;
4519 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4521 new_type->function.parameters = parameters;
4522 new_type->function.unspecified_parameters = true;
4524 type = typehash_insert(new_type);
4525 if (type != new_type) {
4526 obstack_free(type_obst, new_type);
4529 declaration->type = type;
4532 static bool first_err = true;
4535 * When called with first_err set, prints the name of the current function,
4538 static void print_in_function(void) {
4541 diagnosticf("%s: In function '%Y':\n",
4542 current_function->source_position.input_name,
4543 current_function->symbol);
4548 * Check if all labels are defined in the current function.
4549 * Check if all labels are used in the current function.
4551 static void check_labels(void)
4553 for (const goto_statement_t *goto_statement = goto_first;
4554 goto_statement != NULL;
4555 goto_statement = goto_statement->next) {
4556 declaration_t *label = goto_statement->label;
4559 if (label->source_position.input_name == NULL) {
4560 print_in_function();
4561 errorf(&goto_statement->base.source_position,
4562 "label '%Y' used but not defined", label->symbol);
4565 goto_first = goto_last = NULL;
4567 if (warning.unused_label) {
4568 for (const label_statement_t *label_statement = label_first;
4569 label_statement != NULL;
4570 label_statement = label_statement->next) {
4571 const declaration_t *label = label_statement->label;
4573 if (! label->used) {
4574 print_in_function();
4575 warningf(&label_statement->base.source_position,
4576 "label '%Y' defined but not used", label->symbol);
4580 label_first = label_last = NULL;
4584 * Check declarations of current_function for unused entities.
4586 static void check_declarations(void)
4588 if (warning.unused_parameter) {
4589 const scope_t *scope = ¤t_function->scope;
4591 const declaration_t *parameter = scope->declarations;
4592 for (; parameter != NULL; parameter = parameter->next) {
4593 if (! parameter->used) {
4594 print_in_function();
4595 warningf(¶meter->source_position,
4596 "unused parameter '%Y'", parameter->symbol);
4600 if (warning.unused_variable) {
4604 static void parse_external_declaration(void)
4606 /* function-definitions and declarations both start with declaration
4608 declaration_specifiers_t specifiers;
4609 memset(&specifiers, 0, sizeof(specifiers));
4611 add_anchor_token(';');
4612 parse_declaration_specifiers(&specifiers);
4613 rem_anchor_token(';');
4615 /* must be a declaration */
4616 if (token.type == ';') {
4617 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4621 add_anchor_token(',');
4622 add_anchor_token('=');
4623 rem_anchor_token(';');
4625 /* declarator is common to both function-definitions and declarations */
4626 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4628 rem_anchor_token(',');
4629 rem_anchor_token('=');
4630 rem_anchor_token(';');
4632 /* must be a declaration */
4633 switch (token.type) {
4636 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4640 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
4644 /* must be a function definition */
4645 parse_kr_declaration_list(ndeclaration);
4647 if (token.type != '{') {
4648 parse_error_expected("while parsing function definition", '{', NULL);
4649 eat_until_matching_token(';');
4653 type_t *type = ndeclaration->type;
4655 /* note that we don't skip typerefs: the standard doesn't allow them here
4656 * (so we can't use is_type_function here) */
4657 if (type->kind != TYPE_FUNCTION) {
4658 if (is_type_valid(type)) {
4659 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4660 type, ndeclaration->symbol);
4666 /* § 6.7.5.3 (14) a function definition with () means no
4667 * parameters (and not unspecified parameters) */
4668 if (type->function.unspecified_parameters
4669 && type->function.parameters == NULL
4670 && !type->function.kr_style_parameters) {
4671 type_t *duplicate = duplicate_type(type);
4672 duplicate->function.unspecified_parameters = false;
4674 type = typehash_insert(duplicate);
4675 if (type != duplicate) {
4676 obstack_free(type_obst, duplicate);
4678 ndeclaration->type = type;
4681 declaration_t *const declaration = record_definition(ndeclaration);
4682 if (ndeclaration != declaration) {
4683 declaration->scope = ndeclaration->scope;
4685 type = skip_typeref(declaration->type);
4687 /* push function parameters and switch scope */
4688 int top = environment_top();
4689 scope_t *last_scope = scope;
4690 set_scope(&declaration->scope);
4692 declaration_t *parameter = declaration->scope.declarations;
4693 for( ; parameter != NULL; parameter = parameter->next) {
4694 if (parameter->parent_scope == &ndeclaration->scope) {
4695 parameter->parent_scope = scope;
4697 assert(parameter->parent_scope == NULL
4698 || parameter->parent_scope == scope);
4699 parameter->parent_scope = scope;
4700 if (parameter->symbol == NULL) {
4701 errorf(&ndeclaration->source_position, "parameter name omitted");
4704 environment_push(parameter);
4707 if (declaration->init.statement != NULL) {
4708 parser_error_multiple_definition(declaration, HERE);
4711 /* parse function body */
4712 int label_stack_top = label_top();
4713 declaration_t *old_current_function = current_function;
4714 current_function = declaration;
4716 declaration->init.statement = parse_compound_statement(false);
4719 check_declarations();
4721 assert(current_function == declaration);
4722 current_function = old_current_function;
4723 label_pop_to(label_stack_top);
4726 assert(scope == &declaration->scope);
4727 set_scope(last_scope);
4728 environment_pop_to(top);
4731 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4732 source_position_t *source_position)
4734 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4736 type->bitfield.base_type = base_type;
4737 type->bitfield.size = size;
4742 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4745 declaration_t *iter = compound_declaration->scope.declarations;
4746 for( ; iter != NULL; iter = iter->next) {
4747 if (iter->namespc != NAMESPACE_NORMAL)
4750 if (iter->symbol == NULL) {
4751 type_t *type = skip_typeref(iter->type);
4752 if (is_type_compound(type)) {
4753 declaration_t *result
4754 = find_compound_entry(type->compound.declaration, symbol);
4761 if (iter->symbol == symbol) {
4769 static void parse_compound_declarators(declaration_t *struct_declaration,
4770 const declaration_specifiers_t *specifiers)
4772 declaration_t *last_declaration = struct_declaration->scope.declarations;
4773 if (last_declaration != NULL) {
4774 while(last_declaration->next != NULL) {
4775 last_declaration = last_declaration->next;
4780 declaration_t *declaration;
4782 if (token.type == ':') {
4783 source_position_t source_position = *HERE;
4786 type_t *base_type = specifiers->type;
4787 expression_t *size = parse_constant_expression();
4789 if (!is_type_integer(skip_typeref(base_type))) {
4790 errorf(HERE, "bitfield base type '%T' is not an integer type",
4794 type_t *type = make_bitfield_type(base_type, size, &source_position);
4796 declaration = allocate_declaration_zero();
4797 declaration->namespc = NAMESPACE_NORMAL;
4798 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4799 declaration->storage_class = STORAGE_CLASS_NONE;
4800 declaration->source_position = source_position;
4801 declaration->modifiers = specifiers->modifiers;
4802 declaration->type = type;
4804 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4806 type_t *orig_type = declaration->type;
4807 type_t *type = skip_typeref(orig_type);
4809 if (token.type == ':') {
4810 source_position_t source_position = *HERE;
4812 expression_t *size = parse_constant_expression();
4814 if (!is_type_integer(type)) {
4815 errorf(HERE, "bitfield base type '%T' is not an "
4816 "integer type", orig_type);
4819 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4820 declaration->type = bitfield_type;
4822 /* TODO we ignore arrays for now... what is missing is a check
4823 * that they're at the end of the struct */
4824 if (is_type_incomplete(type) && !is_type_array(type)) {
4826 "compound member '%Y' has incomplete type '%T'",
4827 declaration->symbol, orig_type);
4828 } else if (is_type_function(type)) {
4829 errorf(HERE, "compound member '%Y' must not have function "
4830 "type '%T'", declaration->symbol, orig_type);
4835 /* make sure we don't define a symbol multiple times */
4836 symbol_t *symbol = declaration->symbol;
4837 if (symbol != NULL) {
4838 declaration_t *prev_decl
4839 = find_compound_entry(struct_declaration, symbol);
4841 if (prev_decl != NULL) {
4842 assert(prev_decl->symbol == symbol);
4843 errorf(&declaration->source_position,
4844 "multiple declarations of symbol '%Y' (declared %P)",
4845 symbol, &prev_decl->source_position);
4849 /* append declaration */
4850 if (last_declaration != NULL) {
4851 last_declaration->next = declaration;
4853 struct_declaration->scope.declarations = declaration;
4855 last_declaration = declaration;
4857 if (token.type != ',')
4867 static void parse_compound_type_entries(declaration_t *compound_declaration)
4870 add_anchor_token('}');
4872 while(token.type != '}' && token.type != T_EOF) {
4873 declaration_specifiers_t specifiers;
4874 memset(&specifiers, 0, sizeof(specifiers));
4875 parse_declaration_specifiers(&specifiers);
4877 parse_compound_declarators(compound_declaration, &specifiers);
4879 rem_anchor_token('}');
4881 if (token.type == T_EOF) {
4882 errorf(HERE, "EOF while parsing struct");
4887 static type_t *parse_typename(void)
4889 declaration_specifiers_t specifiers;
4890 memset(&specifiers, 0, sizeof(specifiers));
4891 parse_declaration_specifiers(&specifiers);
4892 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4893 /* TODO: improve error message, user does probably not know what a
4894 * storage class is...
4896 errorf(HERE, "typename may not have a storage class");
4899 type_t *result = parse_abstract_declarator(specifiers.type);
4907 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4908 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4909 expression_t *left);
4911 typedef struct expression_parser_function_t expression_parser_function_t;
4912 struct expression_parser_function_t {
4913 unsigned precedence;
4914 parse_expression_function parser;
4915 unsigned infix_precedence;
4916 parse_expression_infix_function infix_parser;
4919 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4922 * Prints an error message if an expression was expected but not read
4924 static expression_t *expected_expression_error(void)
4926 /* skip the error message if the error token was read */
4927 if (token.type != T_ERROR) {
4928 errorf(HERE, "expected expression, got token '%K'", &token);
4932 return create_invalid_expression();
4936 * Parse a string constant.
4938 static expression_t *parse_string_const(void)
4941 if (token.type == T_STRING_LITERAL) {
4942 string_t res = token.v.string;
4944 while (token.type == T_STRING_LITERAL) {
4945 res = concat_strings(&res, &token.v.string);
4948 if (token.type != T_WIDE_STRING_LITERAL) {
4949 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4950 /* note: that we use type_char_ptr here, which is already the
4951 * automatic converted type. revert_automatic_type_conversion
4952 * will construct the array type */
4953 cnst->base.type = type_char_ptr;
4954 cnst->string.value = res;
4958 wres = concat_string_wide_string(&res, &token.v.wide_string);
4960 wres = token.v.wide_string;
4965 switch (token.type) {
4966 case T_WIDE_STRING_LITERAL:
4967 wres = concat_wide_strings(&wres, &token.v.wide_string);
4970 case T_STRING_LITERAL:
4971 wres = concat_wide_string_string(&wres, &token.v.string);
4975 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4976 cnst->base.type = type_wchar_t_ptr;
4977 cnst->wide_string.value = wres;
4986 * Parse an integer constant.
4988 static expression_t *parse_int_const(void)
4990 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4991 cnst->base.source_position = *HERE;
4992 cnst->base.type = token.datatype;
4993 cnst->conste.v.int_value = token.v.intvalue;
5001 * Parse a character constant.
5003 static expression_t *parse_character_constant(void)
5005 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5007 cnst->base.source_position = *HERE;
5008 cnst->base.type = token.datatype;
5009 cnst->conste.v.character = token.v.string;
5011 if (cnst->conste.v.character.size != 1) {
5012 if (warning.multichar && (c_mode & _GNUC)) {
5014 warningf(HERE, "multi-character character constant");
5016 errorf(HERE, "more than 1 characters in character constant");
5025 * Parse a wide character constant.
5027 static expression_t *parse_wide_character_constant(void)
5029 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5031 cnst->base.source_position = *HERE;
5032 cnst->base.type = token.datatype;
5033 cnst->conste.v.wide_character = token.v.wide_string;
5035 if (cnst->conste.v.wide_character.size != 1) {
5036 if (warning.multichar && (c_mode & _GNUC)) {
5038 warningf(HERE, "multi-character character constant");
5040 errorf(HERE, "more than 1 characters in character constant");
5049 * Parse a float constant.
5051 static expression_t *parse_float_const(void)
5053 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5054 cnst->base.type = token.datatype;
5055 cnst->conste.v.float_value = token.v.floatvalue;
5062 static declaration_t *create_implicit_function(symbol_t *symbol,
5063 const source_position_t *source_position)
5065 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5066 ntype->function.return_type = type_int;
5067 ntype->function.unspecified_parameters = true;
5069 type_t *type = typehash_insert(ntype);
5070 if (type != ntype) {
5074 declaration_t *const declaration = allocate_declaration_zero();
5075 declaration->storage_class = STORAGE_CLASS_EXTERN;
5076 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5077 declaration->type = type;
5078 declaration->symbol = symbol;
5079 declaration->source_position = *source_position;
5081 bool strict_prototypes_old = warning.strict_prototypes;
5082 warning.strict_prototypes = false;
5083 record_declaration(declaration);
5084 warning.strict_prototypes = strict_prototypes_old;
5090 * Creates a return_type (func)(argument_type) function type if not
5093 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5094 type_t *argument_type2)
5096 function_parameter_t *parameter2
5097 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5098 memset(parameter2, 0, sizeof(parameter2[0]));
5099 parameter2->type = argument_type2;
5101 function_parameter_t *parameter1
5102 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5103 memset(parameter1, 0, sizeof(parameter1[0]));
5104 parameter1->type = argument_type1;
5105 parameter1->next = parameter2;
5107 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5108 type->function.return_type = return_type;
5109 type->function.parameters = parameter1;
5111 type_t *result = typehash_insert(type);
5112 if (result != type) {
5120 * Creates a return_type (func)(argument_type) function type if not
5123 * @param return_type the return type
5124 * @param argument_type the argument type
5126 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5128 function_parameter_t *parameter
5129 = obstack_alloc(type_obst, sizeof(parameter[0]));
5130 memset(parameter, 0, sizeof(parameter[0]));
5131 parameter->type = argument_type;
5133 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5134 type->function.return_type = return_type;
5135 type->function.parameters = parameter;
5137 type_t *result = typehash_insert(type);
5138 if (result != type) {
5145 static type_t *make_function_0_type(type_t *return_type)
5147 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5148 type->function.return_type = return_type;
5149 type->function.parameters = NULL;
5151 type_t *result = typehash_insert(type);
5152 if (result != type) {
5160 * Creates a function type for some function like builtins.
5162 * @param symbol the symbol describing the builtin
5164 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5166 switch(symbol->ID) {
5167 case T___builtin_alloca:
5168 return make_function_1_type(type_void_ptr, type_size_t);
5169 case T___builtin_huge_val:
5170 return make_function_0_type(type_double);
5171 case T___builtin_nan:
5172 return make_function_1_type(type_double, type_char_ptr);
5173 case T___builtin_nanf:
5174 return make_function_1_type(type_float, type_char_ptr);
5175 case T___builtin_nand:
5176 return make_function_1_type(type_long_double, type_char_ptr);
5177 case T___builtin_va_end:
5178 return make_function_1_type(type_void, type_valist);
5179 case T___builtin_expect:
5180 return make_function_2_type(type_long, type_long, type_long);
5182 internal_errorf(HERE, "not implemented builtin symbol found");
5187 * Performs automatic type cast as described in § 6.3.2.1.
5189 * @param orig_type the original type
5191 static type_t *automatic_type_conversion(type_t *orig_type)
5193 type_t *type = skip_typeref(orig_type);
5194 if (is_type_array(type)) {
5195 array_type_t *array_type = &type->array;
5196 type_t *element_type = array_type->element_type;
5197 unsigned qualifiers = array_type->base.qualifiers;
5199 return make_pointer_type(element_type, qualifiers);
5202 if (is_type_function(type)) {
5203 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5210 * reverts the automatic casts of array to pointer types and function
5211 * to function-pointer types as defined § 6.3.2.1
5213 type_t *revert_automatic_type_conversion(const expression_t *expression)
5215 switch (expression->kind) {
5216 case EXPR_REFERENCE: return expression->reference.declaration->type;
5217 case EXPR_SELECT: return expression->select.compound_entry->type;
5219 case EXPR_UNARY_DEREFERENCE: {
5220 const expression_t *const value = expression->unary.value;
5221 type_t *const type = skip_typeref(value->base.type);
5222 assert(is_type_pointer(type));
5223 return type->pointer.points_to;
5226 case EXPR_BUILTIN_SYMBOL:
5227 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5229 case EXPR_ARRAY_ACCESS: {
5230 const expression_t *array_ref = expression->array_access.array_ref;
5231 type_t *type_left = skip_typeref(array_ref->base.type);
5232 if (!is_type_valid(type_left))
5234 assert(is_type_pointer(type_left));
5235 return type_left->pointer.points_to;
5238 case EXPR_STRING_LITERAL: {
5239 size_t size = expression->string.value.size;
5240 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5243 case EXPR_WIDE_STRING_LITERAL: {
5244 size_t size = expression->wide_string.value.size;
5245 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5248 case EXPR_COMPOUND_LITERAL:
5249 return expression->compound_literal.type;
5254 return expression->base.type;
5257 static expression_t *parse_reference(void)
5259 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5261 reference_expression_t *ref = &expression->reference;
5262 symbol_t *const symbol = token.v.symbol;
5264 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5266 source_position_t source_position = token.source_position;
5269 if (declaration == NULL) {
5270 if (! strict_mode && token.type == '(') {
5271 /* an implicitly defined function */
5272 if (warning.implicit_function_declaration) {
5273 warningf(HERE, "implicit declaration of function '%Y'",
5277 declaration = create_implicit_function(symbol,
5280 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5281 return create_invalid_expression();
5285 type_t *type = declaration->type;
5287 /* we always do the auto-type conversions; the & and sizeof parser contains
5288 * code to revert this! */
5289 type = automatic_type_conversion(type);
5291 ref->declaration = declaration;
5292 ref->base.type = type;
5294 /* this declaration is used */
5295 declaration->used = true;
5297 /* check for deprecated functions */
5298 if (declaration->deprecated != 0) {
5299 const char *prefix = "";
5300 if (is_type_function(declaration->type))
5301 prefix = "function ";
5303 if (declaration->deprecated_string != NULL) {
5304 warningf(&source_position,
5305 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
5306 declaration->deprecated_string);
5308 warningf(&source_position,
5309 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
5316 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
5320 /* TODO check if explicit cast is allowed and issue warnings/errors */
5323 static expression_t *parse_compound_literal(type_t *type)
5325 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5327 parse_initializer_env_t env;
5329 env.declaration = NULL;
5330 env.must_be_constant = false;
5331 initializer_t *initializer = parse_initializer(&env);
5334 expression->compound_literal.initializer = initializer;
5335 expression->compound_literal.type = type;
5336 expression->base.type = automatic_type_conversion(type);
5342 * Parse a cast expression.
5344 static expression_t *parse_cast(void)
5346 source_position_t source_position = token.source_position;
5348 type_t *type = parse_typename();
5350 /* matching add_anchor_token() is at call site */
5351 rem_anchor_token(')');
5354 if (token.type == '{') {
5355 return parse_compound_literal(type);
5358 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5359 cast->base.source_position = source_position;
5361 expression_t *value = parse_sub_expression(20);
5363 check_cast_allowed(value, type);
5365 cast->base.type = type;
5366 cast->unary.value = value;
5370 return create_invalid_expression();
5374 * Parse a statement expression.
5376 static expression_t *parse_statement_expression(void)
5378 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5380 statement_t *statement = parse_compound_statement(true);
5381 expression->statement.statement = statement;
5382 expression->base.source_position = statement->base.source_position;
5384 /* find last statement and use its type */
5385 type_t *type = type_void;
5386 const statement_t *stmt = statement->compound.statements;
5388 while (stmt->base.next != NULL)
5389 stmt = stmt->base.next;
5391 if (stmt->kind == STATEMENT_EXPRESSION) {
5392 type = stmt->expression.expression->base.type;
5395 warningf(&expression->base.source_position, "empty statement expression ({})");
5397 expression->base.type = type;
5403 return create_invalid_expression();
5407 * Parse a braced expression.
5409 static expression_t *parse_brace_expression(void)
5412 add_anchor_token(')');
5414 switch(token.type) {
5416 /* gcc extension: a statement expression */
5417 return parse_statement_expression();
5421 return parse_cast();
5423 if (is_typedef_symbol(token.v.symbol)) {
5424 return parse_cast();
5428 expression_t *result = parse_expression();
5429 rem_anchor_token(')');
5434 return create_invalid_expression();
5437 static expression_t *parse_function_keyword(void)
5442 if (current_function == NULL) {
5443 errorf(HERE, "'__func__' used outside of a function");
5446 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5447 expression->base.type = type_char_ptr;
5448 expression->funcname.kind = FUNCNAME_FUNCTION;
5453 static expression_t *parse_pretty_function_keyword(void)
5455 eat(T___PRETTY_FUNCTION__);
5457 if (current_function == NULL) {
5458 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5461 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5462 expression->base.type = type_char_ptr;
5463 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5468 static expression_t *parse_funcsig_keyword(void)
5472 if (current_function == NULL) {
5473 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5476 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5477 expression->base.type = type_char_ptr;
5478 expression->funcname.kind = FUNCNAME_FUNCSIG;
5483 static expression_t *parse_funcdname_keyword(void)
5485 eat(T___FUNCDNAME__);
5487 if (current_function == NULL) {
5488 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5491 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5492 expression->base.type = type_char_ptr;
5493 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5498 static designator_t *parse_designator(void)
5500 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5501 result->source_position = *HERE;
5503 if (token.type != T_IDENTIFIER) {
5504 parse_error_expected("while parsing member designator",
5505 T_IDENTIFIER, NULL);
5508 result->symbol = token.v.symbol;
5511 designator_t *last_designator = result;
5513 if (token.type == '.') {
5515 if (token.type != T_IDENTIFIER) {
5516 parse_error_expected("while parsing member designator",
5517 T_IDENTIFIER, NULL);
5520 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5521 designator->source_position = *HERE;
5522 designator->symbol = token.v.symbol;
5525 last_designator->next = designator;
5526 last_designator = designator;
5529 if (token.type == '[') {
5531 add_anchor_token(']');
5532 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5533 designator->source_position = *HERE;
5534 designator->array_index = parse_expression();
5535 rem_anchor_token(']');
5537 if (designator->array_index == NULL) {
5541 last_designator->next = designator;
5542 last_designator = designator;
5554 * Parse the __builtin_offsetof() expression.
5556 static expression_t *parse_offsetof(void)
5558 eat(T___builtin_offsetof);
5560 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5561 expression->base.type = type_size_t;
5564 add_anchor_token(',');
5565 type_t *type = parse_typename();
5566 rem_anchor_token(',');
5568 add_anchor_token(')');
5569 designator_t *designator = parse_designator();
5570 rem_anchor_token(')');
5573 expression->offsetofe.type = type;
5574 expression->offsetofe.designator = designator;
5577 memset(&path, 0, sizeof(path));
5578 path.top_type = type;
5579 path.path = NEW_ARR_F(type_path_entry_t, 0);
5581 descend_into_subtype(&path);
5583 if (!walk_designator(&path, designator, true)) {
5584 return create_invalid_expression();
5587 DEL_ARR_F(path.path);
5591 return create_invalid_expression();
5595 * Parses a _builtin_va_start() expression.
5597 static expression_t *parse_va_start(void)
5599 eat(T___builtin_va_start);
5601 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5604 add_anchor_token(',');
5605 expression->va_starte.ap = parse_assignment_expression();
5606 rem_anchor_token(',');
5608 expression_t *const expr = parse_assignment_expression();
5609 if (expr->kind == EXPR_REFERENCE) {
5610 declaration_t *const decl = expr->reference.declaration;
5612 return create_invalid_expression();
5613 if (decl->parent_scope == ¤t_function->scope &&
5614 decl->next == NULL) {
5615 expression->va_starte.parameter = decl;
5620 errorf(&expr->base.source_position,
5621 "second argument of 'va_start' must be last parameter of the current function");
5623 return create_invalid_expression();
5627 * Parses a _builtin_va_arg() expression.
5629 static expression_t *parse_va_arg(void)
5631 eat(T___builtin_va_arg);
5633 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5636 expression->va_arge.ap = parse_assignment_expression();
5638 expression->base.type = parse_typename();
5643 return create_invalid_expression();
5646 static expression_t *parse_builtin_symbol(void)
5648 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5650 symbol_t *symbol = token.v.symbol;
5652 expression->builtin_symbol.symbol = symbol;
5655 type_t *type = get_builtin_symbol_type(symbol);
5656 type = automatic_type_conversion(type);
5658 expression->base.type = type;
5663 * Parses a __builtin_constant() expression.
5665 static expression_t *parse_builtin_constant(void)
5667 eat(T___builtin_constant_p);
5669 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5672 add_anchor_token(')');
5673 expression->builtin_constant.value = parse_assignment_expression();
5674 rem_anchor_token(')');
5676 expression->base.type = type_int;
5680 return create_invalid_expression();
5684 * Parses a __builtin_prefetch() expression.
5686 static expression_t *parse_builtin_prefetch(void)
5688 eat(T___builtin_prefetch);
5690 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5693 add_anchor_token(')');
5694 expression->builtin_prefetch.adr = parse_assignment_expression();
5695 if (token.type == ',') {
5697 expression->builtin_prefetch.rw = parse_assignment_expression();
5699 if (token.type == ',') {
5701 expression->builtin_prefetch.locality = parse_assignment_expression();
5703 rem_anchor_token(')');
5705 expression->base.type = type_void;
5709 return create_invalid_expression();
5713 * Parses a __builtin_is_*() compare expression.
5715 static expression_t *parse_compare_builtin(void)
5717 expression_t *expression;
5719 switch(token.type) {
5720 case T___builtin_isgreater:
5721 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5723 case T___builtin_isgreaterequal:
5724 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5726 case T___builtin_isless:
5727 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5729 case T___builtin_islessequal:
5730 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5732 case T___builtin_islessgreater:
5733 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5735 case T___builtin_isunordered:
5736 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5739 internal_errorf(HERE, "invalid compare builtin found");
5742 expression->base.source_position = *HERE;
5746 expression->binary.left = parse_assignment_expression();
5748 expression->binary.right = parse_assignment_expression();
5751 type_t *const orig_type_left = expression->binary.left->base.type;
5752 type_t *const orig_type_right = expression->binary.right->base.type;
5754 type_t *const type_left = skip_typeref(orig_type_left);
5755 type_t *const type_right = skip_typeref(orig_type_right);
5756 if (!is_type_float(type_left) && !is_type_float(type_right)) {
5757 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5758 type_error_incompatible("invalid operands in comparison",
5759 &expression->base.source_position, orig_type_left, orig_type_right);
5762 semantic_comparison(&expression->binary);
5767 return create_invalid_expression();
5772 * Parses a __builtin_expect() expression.
5774 static expression_t *parse_builtin_expect(void)
5776 eat(T___builtin_expect);
5778 expression_t *expression
5779 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5782 expression->binary.left = parse_assignment_expression();
5784 expression->binary.right = parse_constant_expression();
5787 expression->base.type = expression->binary.left->base.type;
5791 return create_invalid_expression();
5796 * Parses a MS assume() expression.
5798 static expression_t *parse_assume(void) {
5801 expression_t *expression
5802 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5805 add_anchor_token(')');
5806 expression->unary.value = parse_assignment_expression();
5807 rem_anchor_token(')');
5810 expression->base.type = type_void;
5813 return create_invalid_expression();
5817 * Parse a microsoft __noop expression.
5819 static expression_t *parse_noop_expression(void) {
5820 source_position_t source_position = *HERE;
5823 if (token.type == '(') {
5824 /* parse arguments */
5826 add_anchor_token(')');
5827 add_anchor_token(',');
5829 if (token.type != ')') {
5831 (void)parse_assignment_expression();
5832 if (token.type != ',')
5838 rem_anchor_token(',');
5839 rem_anchor_token(')');
5842 /* the result is a (int)0 */
5843 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5844 cnst->base.source_position = source_position;
5845 cnst->base.type = type_int;
5846 cnst->conste.v.int_value = 0;
5847 cnst->conste.is_ms_noop = true;
5852 return create_invalid_expression();
5856 * Parses a primary expression.
5858 static expression_t *parse_primary_expression(void)
5860 switch (token.type) {
5861 case T_INTEGER: return parse_int_const();
5862 case T_CHARACTER_CONSTANT: return parse_character_constant();
5863 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5864 case T_FLOATINGPOINT: return parse_float_const();
5865 case T_STRING_LITERAL:
5866 case T_WIDE_STRING_LITERAL: return parse_string_const();
5867 case T_IDENTIFIER: return parse_reference();
5868 case T___FUNCTION__:
5869 case T___func__: return parse_function_keyword();
5870 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5871 case T___FUNCSIG__: return parse_funcsig_keyword();
5872 case T___FUNCDNAME__: return parse_funcdname_keyword();
5873 case T___builtin_offsetof: return parse_offsetof();
5874 case T___builtin_va_start: return parse_va_start();
5875 case T___builtin_va_arg: return parse_va_arg();
5876 case T___builtin_expect:
5877 case T___builtin_alloca:
5878 case T___builtin_nan:
5879 case T___builtin_nand:
5880 case T___builtin_nanf:
5881 case T___builtin_huge_val:
5882 case T___builtin_va_end: return parse_builtin_symbol();
5883 case T___builtin_isgreater:
5884 case T___builtin_isgreaterequal:
5885 case T___builtin_isless:
5886 case T___builtin_islessequal:
5887 case T___builtin_islessgreater:
5888 case T___builtin_isunordered: return parse_compare_builtin();
5889 case T___builtin_constant_p: return parse_builtin_constant();
5890 case T___builtin_prefetch: return parse_builtin_prefetch();
5891 case T__assume: return parse_assume();
5893 case '(': return parse_brace_expression();
5894 case T___noop: return parse_noop_expression();
5897 errorf(HERE, "unexpected token %K, expected an expression", &token);
5898 return create_invalid_expression();
5902 * Check if the expression has the character type and issue a warning then.
5904 static void check_for_char_index_type(const expression_t *expression) {
5905 type_t *const type = expression->base.type;
5906 const type_t *const base_type = skip_typeref(type);
5908 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5909 warning.char_subscripts) {
5910 warningf(&expression->base.source_position,
5911 "array subscript has type '%T'", type);
5915 static expression_t *parse_array_expression(unsigned precedence,
5921 add_anchor_token(']');
5923 expression_t *inside = parse_expression();
5925 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5927 array_access_expression_t *array_access = &expression->array_access;
5929 type_t *const orig_type_left = left->base.type;
5930 type_t *const orig_type_inside = inside->base.type;
5932 type_t *const type_left = skip_typeref(orig_type_left);
5933 type_t *const type_inside = skip_typeref(orig_type_inside);
5935 type_t *return_type;
5936 if (is_type_pointer(type_left)) {
5937 return_type = type_left->pointer.points_to;
5938 array_access->array_ref = left;
5939 array_access->index = inside;
5940 check_for_char_index_type(inside);
5941 } else if (is_type_pointer(type_inside)) {
5942 return_type = type_inside->pointer.points_to;
5943 array_access->array_ref = inside;
5944 array_access->index = left;
5945 array_access->flipped = true;
5946 check_for_char_index_type(left);
5948 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5950 "array access on object with non-pointer types '%T', '%T'",
5951 orig_type_left, orig_type_inside);
5953 return_type = type_error_type;
5954 array_access->array_ref = create_invalid_expression();
5957 rem_anchor_token(']');
5958 if (token.type != ']') {
5959 parse_error_expected("Problem while parsing array access", ']', NULL);
5964 return_type = automatic_type_conversion(return_type);
5965 expression->base.type = return_type;
5970 static expression_t *parse_typeprop(expression_kind_t const kind,
5971 source_position_t const pos,
5972 unsigned const precedence)
5974 expression_t *tp_expression = allocate_expression_zero(kind);
5975 tp_expression->base.type = type_size_t;
5976 tp_expression->base.source_position = pos;
5978 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
5980 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5982 add_anchor_token(')');
5983 type_t* const orig_type = parse_typename();
5984 tp_expression->typeprop.type = orig_type;
5986 type_t const* const type = skip_typeref(orig_type);
5987 char const* const wrong_type =
5988 is_type_incomplete(type) ? "incomplete" :
5989 type->kind == TYPE_FUNCTION ? "function designator" :
5990 type->kind == TYPE_BITFIELD ? "bitfield" :
5992 if (wrong_type != NULL) {
5993 errorf(&pos, "operand of %s expression must not be %s type '%T'",
5994 what, wrong_type, type);
5997 rem_anchor_token(')');
6000 expression_t *expression = parse_sub_expression(precedence);
6002 type_t* const orig_type = revert_automatic_type_conversion(expression);
6003 expression->base.type = orig_type;
6005 type_t const* const type = skip_typeref(orig_type);
6006 char const* const wrong_type =
6007 is_type_incomplete(type) ? "incomplete" :
6008 type->kind == TYPE_FUNCTION ? "function designator" :
6009 type->kind == TYPE_BITFIELD ? "bitfield" :
6011 if (wrong_type != NULL) {
6012 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6015 tp_expression->typeprop.type = expression->base.type;
6016 tp_expression->typeprop.tp_expression = expression;
6019 return tp_expression;
6021 return create_invalid_expression();
6024 static expression_t *parse_sizeof(unsigned precedence)
6026 source_position_t pos = *HERE;
6028 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6031 static expression_t *parse_alignof(unsigned precedence)
6033 source_position_t pos = *HERE;
6035 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6038 static expression_t *parse_select_expression(unsigned precedence,
6039 expression_t *compound)
6042 assert(token.type == '.' || token.type == T_MINUSGREATER);
6044 bool is_pointer = (token.type == T_MINUSGREATER);
6047 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6048 select->select.compound = compound;
6050 if (token.type != T_IDENTIFIER) {
6051 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6054 symbol_t *symbol = token.v.symbol;
6055 select->select.symbol = symbol;
6058 type_t *const orig_type = compound->base.type;
6059 type_t *const type = skip_typeref(orig_type);
6061 type_t *type_left = type;
6063 if (!is_type_pointer(type)) {
6064 if (is_type_valid(type)) {
6065 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6067 return create_invalid_expression();
6069 type_left = type->pointer.points_to;
6071 type_left = skip_typeref(type_left);
6073 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6074 type_left->kind != TYPE_COMPOUND_UNION) {
6075 if (is_type_valid(type_left)) {
6076 errorf(HERE, "request for member '%Y' in something not a struct or "
6077 "union, but '%T'", symbol, type_left);
6079 return create_invalid_expression();
6082 declaration_t *const declaration = type_left->compound.declaration;
6084 if (!declaration->init.complete) {
6085 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6087 return create_invalid_expression();
6090 declaration_t *iter = find_compound_entry(declaration, symbol);
6092 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6093 return create_invalid_expression();
6096 /* we always do the auto-type conversions; the & and sizeof parser contains
6097 * code to revert this! */
6098 type_t *expression_type = automatic_type_conversion(iter->type);
6100 select->select.compound_entry = iter;
6101 select->base.type = expression_type;
6103 type_t *skipped = skip_typeref(iter->type);
6104 if (skipped->kind == TYPE_BITFIELD) {
6105 select->base.type = skipped->bitfield.base_type;
6111 static void check_call_argument(const function_parameter_t *parameter,
6112 call_argument_t *argument)
6114 type_t *expected_type = parameter->type;
6115 type_t *expected_type_skip = skip_typeref(expected_type);
6116 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6117 expression_t *arg_expr = argument->expression;
6119 /* handle transparent union gnu extension */
6120 if (is_type_union(expected_type_skip)
6121 && (expected_type_skip->base.modifiers
6122 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6123 declaration_t *union_decl = expected_type_skip->compound.declaration;
6125 declaration_t *declaration = union_decl->scope.declarations;
6126 type_t *best_type = NULL;
6127 for ( ; declaration != NULL; declaration = declaration->next) {
6128 type_t *decl_type = declaration->type;
6129 error = semantic_assign(decl_type, arg_expr);
6130 if (error == ASSIGN_ERROR_INCOMPATIBLE
6131 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6134 if (error == ASSIGN_SUCCESS) {
6135 best_type = decl_type;
6136 } else if (best_type == NULL) {
6137 best_type = decl_type;
6141 if (best_type != NULL) {
6142 expected_type = best_type;
6146 error = semantic_assign(expected_type, arg_expr);
6147 argument->expression = create_implicit_cast(argument->expression,
6150 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6151 report_assign_error(error, expected_type, arg_expr, "function call",
6152 &arg_expr->base.source_position);
6156 * Parse a call expression, ie. expression '( ... )'.
6158 * @param expression the function address
6160 static expression_t *parse_call_expression(unsigned precedence,
6161 expression_t *expression)
6164 expression_t *result = allocate_expression_zero(EXPR_CALL);
6165 result->base.source_position = expression->base.source_position;
6167 call_expression_t *call = &result->call;
6168 call->function = expression;
6170 type_t *const orig_type = expression->base.type;
6171 type_t *const type = skip_typeref(orig_type);
6173 function_type_t *function_type = NULL;
6174 if (is_type_pointer(type)) {
6175 type_t *const to_type = skip_typeref(type->pointer.points_to);
6177 if (is_type_function(to_type)) {
6178 function_type = &to_type->function;
6179 call->base.type = function_type->return_type;
6183 if (function_type == NULL && is_type_valid(type)) {
6184 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6187 /* parse arguments */
6189 add_anchor_token(')');
6190 add_anchor_token(',');
6192 if (token.type != ')') {
6193 call_argument_t *last_argument = NULL;
6196 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6198 argument->expression = parse_assignment_expression();
6199 if (last_argument == NULL) {
6200 call->arguments = argument;
6202 last_argument->next = argument;
6204 last_argument = argument;
6206 if (token.type != ',')
6211 rem_anchor_token(',');
6212 rem_anchor_token(')');
6215 if (function_type == NULL)
6218 function_parameter_t *parameter = function_type->parameters;
6219 call_argument_t *argument = call->arguments;
6220 if (!function_type->unspecified_parameters) {
6221 for( ; parameter != NULL && argument != NULL;
6222 parameter = parameter->next, argument = argument->next) {
6223 check_call_argument(parameter, argument);
6226 if (parameter != NULL) {
6227 errorf(HERE, "too few arguments to function '%E'", expression);
6228 } else if (argument != NULL && !function_type->variadic) {
6229 errorf(HERE, "too many arguments to function '%E'", expression);
6233 /* do default promotion */
6234 for( ; argument != NULL; argument = argument->next) {
6235 type_t *type = argument->expression->base.type;
6237 type = get_default_promoted_type(type);
6239 argument->expression
6240 = create_implicit_cast(argument->expression, type);
6243 check_format(&result->call);
6247 return create_invalid_expression();
6250 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6252 static bool same_compound_type(const type_t *type1, const type_t *type2)
6255 is_type_compound(type1) &&
6256 type1->kind == type2->kind &&
6257 type1->compound.declaration == type2->compound.declaration;
6261 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6263 * @param expression the conditional expression
6265 static expression_t *parse_conditional_expression(unsigned precedence,
6266 expression_t *expression)
6269 add_anchor_token(':');
6271 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6273 conditional_expression_t *conditional = &result->conditional;
6274 conditional->condition = expression;
6277 type_t *const condition_type_orig = expression->base.type;
6278 type_t *const condition_type = skip_typeref(condition_type_orig);
6279 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6280 type_error("expected a scalar type in conditional condition",
6281 &expression->base.source_position, condition_type_orig);
6284 expression_t *true_expression = parse_expression();
6285 rem_anchor_token(':');
6287 expression_t *false_expression = parse_sub_expression(precedence);
6289 type_t *const orig_true_type = true_expression->base.type;
6290 type_t *const orig_false_type = false_expression->base.type;
6291 type_t *const true_type = skip_typeref(orig_true_type);
6292 type_t *const false_type = skip_typeref(orig_false_type);
6295 type_t *result_type;
6296 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6297 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6298 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6299 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6300 warningf(&expression->base.source_position,
6301 "ISO C forbids conditional expression with only one void side");
6303 result_type = type_void;
6304 } else if (is_type_arithmetic(true_type)
6305 && is_type_arithmetic(false_type)) {
6306 result_type = semantic_arithmetic(true_type, false_type);
6308 true_expression = create_implicit_cast(true_expression, result_type);
6309 false_expression = create_implicit_cast(false_expression, result_type);
6311 conditional->true_expression = true_expression;
6312 conditional->false_expression = false_expression;
6313 conditional->base.type = result_type;
6314 } else if (same_compound_type(true_type, false_type)) {
6315 /* just take 1 of the 2 types */
6316 result_type = true_type;
6317 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6318 type_t *pointer_type;
6320 expression_t *other_expression;
6321 if (is_type_pointer(true_type) &&
6322 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6323 pointer_type = true_type;
6324 other_type = false_type;
6325 other_expression = false_expression;
6327 pointer_type = false_type;
6328 other_type = true_type;
6329 other_expression = true_expression;
6332 if (is_null_pointer_constant(other_expression)) {
6333 result_type = pointer_type;
6334 } else if (is_type_pointer(other_type)) {
6335 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
6336 type_t *to2 = skip_typeref(other_type->pointer.points_to);
6339 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
6340 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
6342 } else if (types_compatible(get_unqualified_type(to1),
6343 get_unqualified_type(to2))) {
6346 warningf(&expression->base.source_position,
6347 "pointer types '%T' and '%T' in conditional expression are incompatible",
6348 true_type, false_type);
6352 type_t *const copy = duplicate_type(to);
6353 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
6355 type_t *const type = typehash_insert(copy);
6359 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
6360 } else if (is_type_integer(other_type)) {
6361 warningf(&expression->base.source_position,
6362 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
6363 result_type = pointer_type;
6365 type_error_incompatible("while parsing conditional",
6366 &expression->base.source_position, true_type, false_type);
6367 result_type = type_error_type;
6370 /* TODO: one pointer to void*, other some pointer */
6372 if (is_type_valid(true_type) && is_type_valid(false_type)) {
6373 type_error_incompatible("while parsing conditional",
6374 &expression->base.source_position, true_type,
6377 result_type = type_error_type;
6380 conditional->true_expression
6381 = create_implicit_cast(true_expression, result_type);
6382 conditional->false_expression
6383 = create_implicit_cast(false_expression, result_type);
6384 conditional->base.type = result_type;
6387 return create_invalid_expression();
6391 * Parse an extension expression.
6393 static expression_t *parse_extension(unsigned precedence)
6395 eat(T___extension__);
6397 /* TODO enable extensions */
6398 expression_t *expression = parse_sub_expression(precedence);
6399 /* TODO disable extensions */
6404 * Parse a __builtin_classify_type() expression.
6406 static expression_t *parse_builtin_classify_type(const unsigned precedence)
6408 eat(T___builtin_classify_type);
6410 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
6411 result->base.type = type_int;
6414 add_anchor_token(')');
6415 expression_t *expression = parse_sub_expression(precedence);
6416 rem_anchor_token(')');
6418 result->classify_type.type_expression = expression;
6422 return create_invalid_expression();
6425 static void check_pointer_arithmetic(const source_position_t *source_position,
6426 type_t *pointer_type,
6427 type_t *orig_pointer_type)
6429 type_t *points_to = pointer_type->pointer.points_to;
6430 points_to = skip_typeref(points_to);
6432 if (is_type_incomplete(points_to) &&
6434 || !is_type_atomic(points_to, ATOMIC_TYPE_VOID))) {
6435 errorf(source_position,
6436 "arithmetic with pointer to incomplete type '%T' not allowed",
6438 } else if (is_type_function(points_to)) {
6439 errorf(source_position,
6440 "arithmetic with pointer to function type '%T' not allowed",
6445 static void semantic_incdec(unary_expression_t *expression)
6447 type_t *const orig_type = expression->value->base.type;
6448 type_t *const type = skip_typeref(orig_type);
6449 if (is_type_pointer(type)) {
6450 check_pointer_arithmetic(&expression->base.source_position,
6452 } else if (!is_type_real(type) && is_type_valid(type)) {
6453 /* TODO: improve error message */
6454 errorf(HERE, "operation needs an arithmetic or pointer type");
6456 expression->base.type = orig_type;
6459 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6461 type_t *const orig_type = expression->value->base.type;
6462 type_t *const type = skip_typeref(orig_type);
6463 if (!is_type_arithmetic(type)) {
6464 if (is_type_valid(type)) {
6465 /* TODO: improve error message */
6466 errorf(HERE, "operation needs an arithmetic type");
6471 expression->base.type = orig_type;
6474 static void semantic_unexpr_scalar(unary_expression_t *expression)
6476 type_t *const orig_type = expression->value->base.type;
6477 type_t *const type = skip_typeref(orig_type);
6478 if (!is_type_scalar(type)) {
6479 if (is_type_valid(type)) {
6480 errorf(HERE, "operand of ! must be of scalar type");
6485 expression->base.type = orig_type;
6488 static void semantic_unexpr_integer(unary_expression_t *expression)
6490 type_t *const orig_type = expression->value->base.type;
6491 type_t *const type = skip_typeref(orig_type);
6492 if (!is_type_integer(type)) {
6493 if (is_type_valid(type)) {
6494 errorf(HERE, "operand of ~ must be of integer type");
6499 expression->base.type = orig_type;
6502 static void semantic_dereference(unary_expression_t *expression)
6504 type_t *const orig_type = expression->value->base.type;
6505 type_t *const type = skip_typeref(orig_type);
6506 if (!is_type_pointer(type)) {
6507 if (is_type_valid(type)) {
6508 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6513 type_t *result_type = type->pointer.points_to;
6514 result_type = automatic_type_conversion(result_type);
6515 expression->base.type = result_type;
6518 static void set_address_taken(expression_t *expression, bool may_be_register)
6520 if (expression->kind != EXPR_REFERENCE)
6523 declaration_t *const declaration = expression->reference.declaration;
6524 /* happens for parse errors */
6525 if (declaration == NULL)
6528 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
6529 errorf(&expression->base.source_position,
6530 "address of register variable '%Y' requested",
6531 declaration->symbol);
6533 declaration->address_taken = 1;
6538 * Check the semantic of the address taken expression.
6540 static void semantic_take_addr(unary_expression_t *expression)
6542 expression_t *value = expression->value;
6543 value->base.type = revert_automatic_type_conversion(value);
6545 type_t *orig_type = value->base.type;
6546 if (!is_type_valid(orig_type))
6549 set_address_taken(value, false);
6551 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6554 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6555 static expression_t *parse_##unexpression_type(unsigned precedence) \
6559 expression_t *unary_expression \
6560 = allocate_expression_zero(unexpression_type); \
6561 unary_expression->base.source_position = *HERE; \
6562 unary_expression->unary.value = parse_sub_expression(precedence); \
6564 sfunc(&unary_expression->unary); \
6566 return unary_expression; \
6569 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6570 semantic_unexpr_arithmetic)
6571 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6572 semantic_unexpr_arithmetic)
6573 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6574 semantic_unexpr_scalar)
6575 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6576 semantic_dereference)
6577 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6579 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6580 semantic_unexpr_integer)
6581 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6583 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6586 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6588 static expression_t *parse_##unexpression_type(unsigned precedence, \
6589 expression_t *left) \
6591 (void) precedence; \
6594 expression_t *unary_expression \
6595 = allocate_expression_zero(unexpression_type); \
6596 unary_expression->unary.value = left; \
6598 sfunc(&unary_expression->unary); \
6600 return unary_expression; \
6603 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6604 EXPR_UNARY_POSTFIX_INCREMENT,
6606 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6607 EXPR_UNARY_POSTFIX_DECREMENT,
6610 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6612 /* TODO: handle complex + imaginary types */
6614 /* § 6.3.1.8 Usual arithmetic conversions */
6615 if (type_left == type_long_double || type_right == type_long_double) {
6616 return type_long_double;
6617 } else if (type_left == type_double || type_right == type_double) {
6619 } else if (type_left == type_float || type_right == type_float) {
6623 type_left = promote_integer(type_left);
6624 type_right = promote_integer(type_right);
6626 if (type_left == type_right)
6629 bool const signed_left = is_type_signed(type_left);
6630 bool const signed_right = is_type_signed(type_right);
6631 int const rank_left = get_rank(type_left);
6632 int const rank_right = get_rank(type_right);
6634 if (signed_left == signed_right)
6635 return rank_left >= rank_right ? type_left : type_right;
6644 u_rank = rank_right;
6645 u_type = type_right;
6647 s_rank = rank_right;
6648 s_type = type_right;
6653 if (u_rank >= s_rank)
6656 if (get_atomic_type_size(s_rank) > get_atomic_type_size(u_rank))
6660 type_t *const type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
6662 case ATOMIC_TYPE_INT: type->atomic.akind = ATOMIC_TYPE_UINT; break;
6663 case ATOMIC_TYPE_LONG: type->atomic.akind = ATOMIC_TYPE_ULONG; break;
6664 case ATOMIC_TYPE_LONGLONG: type->atomic.akind = ATOMIC_TYPE_ULONGLONG; break;
6666 default: panic("invalid atomic type");
6669 type_t* const result = typehash_insert(type);
6677 * Check the semantic restrictions for a binary expression.
6679 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6681 expression_t *const left = expression->left;
6682 expression_t *const right = expression->right;
6683 type_t *const orig_type_left = left->base.type;
6684 type_t *const orig_type_right = right->base.type;
6685 type_t *const type_left = skip_typeref(orig_type_left);
6686 type_t *const type_right = skip_typeref(orig_type_right);
6688 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6689 /* TODO: improve error message */
6690 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6691 errorf(HERE, "operation needs arithmetic types");
6696 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6697 expression->left = create_implicit_cast(left, arithmetic_type);
6698 expression->right = create_implicit_cast(right, arithmetic_type);
6699 expression->base.type = arithmetic_type;
6702 static void semantic_shift_op(binary_expression_t *expression)
6704 expression_t *const left = expression->left;
6705 expression_t *const right = expression->right;
6706 type_t *const orig_type_left = left->base.type;
6707 type_t *const orig_type_right = right->base.type;
6708 type_t * type_left = skip_typeref(orig_type_left);
6709 type_t * type_right = skip_typeref(orig_type_right);
6711 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
6712 /* TODO: improve error message */
6713 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6714 errorf(HERE, "operation needs integer types");
6719 type_left = promote_integer(type_left);
6720 type_right = promote_integer(type_right);
6722 expression->left = create_implicit_cast(left, type_left);
6723 expression->right = create_implicit_cast(right, type_right);
6724 expression->base.type = type_left;
6727 static void semantic_add(binary_expression_t *expression)
6729 expression_t *const left = expression->left;
6730 expression_t *const right = expression->right;
6731 type_t *const orig_type_left = left->base.type;
6732 type_t *const orig_type_right = right->base.type;
6733 type_t *const type_left = skip_typeref(orig_type_left);
6734 type_t *const type_right = skip_typeref(orig_type_right);
6737 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6738 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6739 expression->left = create_implicit_cast(left, arithmetic_type);
6740 expression->right = create_implicit_cast(right, arithmetic_type);
6741 expression->base.type = arithmetic_type;
6743 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6744 check_pointer_arithmetic(&expression->base.source_position,
6745 type_left, orig_type_left);
6746 expression->base.type = type_left;
6747 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
6748 check_pointer_arithmetic(&expression->base.source_position,
6749 type_right, orig_type_right);
6750 expression->base.type = type_right;
6751 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6752 errorf(&expression->base.source_position,
6753 "invalid operands to binary + ('%T', '%T')",
6754 orig_type_left, orig_type_right);
6758 static void semantic_sub(binary_expression_t *expression)
6760 expression_t *const left = expression->left;
6761 expression_t *const right = expression->right;
6762 type_t *const orig_type_left = left->base.type;
6763 type_t *const orig_type_right = right->base.type;
6764 type_t *const type_left = skip_typeref(orig_type_left);
6765 type_t *const type_right = skip_typeref(orig_type_right);
6768 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6769 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6770 expression->left = create_implicit_cast(left, arithmetic_type);
6771 expression->right = create_implicit_cast(right, arithmetic_type);
6772 expression->base.type = arithmetic_type;
6774 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6775 check_pointer_arithmetic(&expression->base.source_position,
6776 type_left, orig_type_left);
6777 expression->base.type = type_left;
6778 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6779 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
6780 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
6781 if (!types_compatible(unqual_left, unqual_right)) {
6782 errorf(&expression->base.source_position,
6783 "subtracting pointers to incompatible types '%T' and '%T'",
6784 orig_type_left, orig_type_right);
6785 } else if (!is_type_object(unqual_left)) {
6786 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
6787 warningf(&expression->base.source_position,
6788 "subtracting pointers to void");
6790 errorf(&expression->base.source_position,
6791 "subtracting pointers to non-object types '%T'",
6795 expression->base.type = type_ptrdiff_t;
6796 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6797 errorf(HERE, "invalid operands of types '%T' and '%T' to binary '-'",
6798 orig_type_left, orig_type_right);
6803 * Check the semantics of comparison expressions.
6805 * @param expression The expression to check.
6807 static void semantic_comparison(binary_expression_t *expression)
6809 expression_t *left = expression->left;
6810 expression_t *right = expression->right;
6811 type_t *orig_type_left = left->base.type;
6812 type_t *orig_type_right = right->base.type;
6814 type_t *type_left = skip_typeref(orig_type_left);
6815 type_t *type_right = skip_typeref(orig_type_right);
6817 /* TODO non-arithmetic types */
6818 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6819 /* test for signed vs unsigned compares */
6820 if (warning.sign_compare &&
6821 (expression->base.kind != EXPR_BINARY_EQUAL &&
6822 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6823 (is_type_signed(type_left) != is_type_signed(type_right))) {
6825 /* check if 1 of the operands is a constant, in this case we just
6826 * check wether we can safely represent the resulting constant in
6827 * the type of the other operand. */
6828 expression_t *const_expr = NULL;
6829 expression_t *other_expr = NULL;
6831 if (is_constant_expression(left)) {
6834 } else if (is_constant_expression(right)) {
6839 if (const_expr != NULL) {
6840 type_t *other_type = skip_typeref(other_expr->base.type);
6841 long val = fold_constant(const_expr);
6842 /* TODO: check if val can be represented by other_type */
6846 warningf(&expression->base.source_position,
6847 "comparison between signed and unsigned");
6849 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6850 expression->left = create_implicit_cast(left, arithmetic_type);
6851 expression->right = create_implicit_cast(right, arithmetic_type);
6852 expression->base.type = arithmetic_type;
6853 if (warning.float_equal &&
6854 (expression->base.kind == EXPR_BINARY_EQUAL ||
6855 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6856 is_type_float(arithmetic_type)) {
6857 warningf(&expression->base.source_position,
6858 "comparing floating point with == or != is unsafe");
6860 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6861 /* TODO check compatibility */
6862 } else if (is_type_pointer(type_left)) {
6863 expression->right = create_implicit_cast(right, type_left);
6864 } else if (is_type_pointer(type_right)) {
6865 expression->left = create_implicit_cast(left, type_right);
6866 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6867 type_error_incompatible("invalid operands in comparison",
6868 &expression->base.source_position,
6869 type_left, type_right);
6871 expression->base.type = type_int;
6875 * Checks if a compound type has constant fields.
6877 static bool has_const_fields(const compound_type_t *type)
6879 const scope_t *scope = &type->declaration->scope;
6880 const declaration_t *declaration = scope->declarations;
6882 for (; declaration != NULL; declaration = declaration->next) {
6883 if (declaration->namespc != NAMESPACE_NORMAL)
6886 const type_t *decl_type = skip_typeref(declaration->type);
6887 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6894 static bool is_lvalue(const expression_t *expression)
6896 switch (expression->kind) {
6897 case EXPR_REFERENCE:
6898 case EXPR_ARRAY_ACCESS:
6900 case EXPR_UNARY_DEREFERENCE:
6908 static bool is_valid_assignment_lhs(expression_t const* const left)
6910 type_t *const orig_type_left = revert_automatic_type_conversion(left);
6911 type_t *const type_left = skip_typeref(orig_type_left);
6913 if (!is_lvalue(left)) {
6914 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
6919 if (is_type_array(type_left)) {
6920 errorf(HERE, "cannot assign to arrays ('%E')", left);
6923 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6924 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6928 if (is_type_incomplete(type_left)) {
6929 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
6930 left, orig_type_left);
6933 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6934 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6935 left, orig_type_left);
6942 static void semantic_arithmetic_assign(binary_expression_t *expression)
6944 expression_t *left = expression->left;
6945 expression_t *right = expression->right;
6946 type_t *orig_type_left = left->base.type;
6947 type_t *orig_type_right = right->base.type;
6949 if (!is_valid_assignment_lhs(left))
6952 type_t *type_left = skip_typeref(orig_type_left);
6953 type_t *type_right = skip_typeref(orig_type_right);
6955 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6956 /* TODO: improve error message */
6957 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6958 errorf(HERE, "operation needs arithmetic types");
6963 /* combined instructions are tricky. We can't create an implicit cast on
6964 * the left side, because we need the uncasted form for the store.
6965 * The ast2firm pass has to know that left_type must be right_type
6966 * for the arithmetic operation and create a cast by itself */
6967 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6968 expression->right = create_implicit_cast(right, arithmetic_type);
6969 expression->base.type = type_left;
6972 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6974 expression_t *const left = expression->left;
6975 expression_t *const right = expression->right;
6976 type_t *const orig_type_left = left->base.type;
6977 type_t *const orig_type_right = right->base.type;
6978 type_t *const type_left = skip_typeref(orig_type_left);
6979 type_t *const type_right = skip_typeref(orig_type_right);
6981 if (!is_valid_assignment_lhs(left))
6984 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6985 /* combined instructions are tricky. We can't create an implicit cast on
6986 * the left side, because we need the uncasted form for the store.
6987 * The ast2firm pass has to know that left_type must be right_type
6988 * for the arithmetic operation and create a cast by itself */
6989 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6990 expression->right = create_implicit_cast(right, arithmetic_type);
6991 expression->base.type = type_left;
6992 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6993 check_pointer_arithmetic(&expression->base.source_position,
6994 type_left, orig_type_left);
6995 expression->base.type = type_left;
6996 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6997 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
7002 * Check the semantic restrictions of a logical expression.
7004 static void semantic_logical_op(binary_expression_t *expression)
7006 expression_t *const left = expression->left;
7007 expression_t *const right = expression->right;
7008 type_t *const orig_type_left = left->base.type;
7009 type_t *const orig_type_right = right->base.type;
7010 type_t *const type_left = skip_typeref(orig_type_left);
7011 type_t *const type_right = skip_typeref(orig_type_right);
7013 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7014 /* TODO: improve error message */
7015 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7016 errorf(HERE, "operation needs scalar types");
7021 expression->base.type = type_int;
7025 * Check the semantic restrictions of a binary assign expression.
7027 static void semantic_binexpr_assign(binary_expression_t *expression)
7029 expression_t *left = expression->left;
7030 type_t *orig_type_left = left->base.type;
7032 type_t *type_left = revert_automatic_type_conversion(left);
7033 type_left = skip_typeref(orig_type_left);
7035 if (!is_valid_assignment_lhs(left))
7038 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7039 report_assign_error(error, orig_type_left, expression->right,
7040 "assignment", &left->base.source_position);
7041 expression->right = create_implicit_cast(expression->right, orig_type_left);
7042 expression->base.type = orig_type_left;
7046 * Determine if the outermost operation (or parts thereof) of the given
7047 * expression has no effect in order to generate a warning about this fact.
7048 * Therefore in some cases this only examines some of the operands of the
7049 * expression (see comments in the function and examples below).
7051 * f() + 23; // warning, because + has no effect
7052 * x || f(); // no warning, because x controls execution of f()
7053 * x ? y : f(); // warning, because y has no effect
7054 * (void)x; // no warning to be able to suppress the warning
7055 * This function can NOT be used for an "expression has definitely no effect"-
7057 static bool expression_has_effect(const expression_t *const expr)
7059 switch (expr->kind) {
7060 case EXPR_UNKNOWN: break;
7061 case EXPR_INVALID: return true; /* do NOT warn */
7062 case EXPR_REFERENCE: return false;
7063 /* suppress the warning for microsoft __noop operations */
7064 case EXPR_CONST: return expr->conste.is_ms_noop;
7065 case EXPR_CHARACTER_CONSTANT: return false;
7066 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7067 case EXPR_STRING_LITERAL: return false;
7068 case EXPR_WIDE_STRING_LITERAL: return false;
7071 const call_expression_t *const call = &expr->call;
7072 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7075 switch (call->function->builtin_symbol.symbol->ID) {
7076 case T___builtin_va_end: return true;
7077 default: return false;
7081 /* Generate the warning if either the left or right hand side of a
7082 * conditional expression has no effect */
7083 case EXPR_CONDITIONAL: {
7084 const conditional_expression_t *const cond = &expr->conditional;
7086 expression_has_effect(cond->true_expression) &&
7087 expression_has_effect(cond->false_expression);
7090 case EXPR_SELECT: return false;
7091 case EXPR_ARRAY_ACCESS: return false;
7092 case EXPR_SIZEOF: return false;
7093 case EXPR_CLASSIFY_TYPE: return false;
7094 case EXPR_ALIGNOF: return false;
7096 case EXPR_FUNCNAME: return false;
7097 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7098 case EXPR_BUILTIN_CONSTANT_P: return false;
7099 case EXPR_BUILTIN_PREFETCH: return true;
7100 case EXPR_OFFSETOF: return false;
7101 case EXPR_VA_START: return true;
7102 case EXPR_VA_ARG: return true;
7103 case EXPR_STATEMENT: return true; // TODO
7104 case EXPR_COMPOUND_LITERAL: return false;
7106 case EXPR_UNARY_NEGATE: return false;
7107 case EXPR_UNARY_PLUS: return false;
7108 case EXPR_UNARY_BITWISE_NEGATE: return false;
7109 case EXPR_UNARY_NOT: return false;
7110 case EXPR_UNARY_DEREFERENCE: return false;
7111 case EXPR_UNARY_TAKE_ADDRESS: return false;
7112 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7113 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7114 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7115 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7117 /* Treat void casts as if they have an effect in order to being able to
7118 * suppress the warning */
7119 case EXPR_UNARY_CAST: {
7120 type_t *const type = skip_typeref(expr->base.type);
7121 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7124 case EXPR_UNARY_CAST_IMPLICIT: return true;
7125 case EXPR_UNARY_ASSUME: return true;
7127 case EXPR_BINARY_ADD: return false;
7128 case EXPR_BINARY_SUB: return false;
7129 case EXPR_BINARY_MUL: return false;
7130 case EXPR_BINARY_DIV: return false;
7131 case EXPR_BINARY_MOD: return false;
7132 case EXPR_BINARY_EQUAL: return false;
7133 case EXPR_BINARY_NOTEQUAL: return false;
7134 case EXPR_BINARY_LESS: return false;
7135 case EXPR_BINARY_LESSEQUAL: return false;
7136 case EXPR_BINARY_GREATER: return false;
7137 case EXPR_BINARY_GREATEREQUAL: return false;
7138 case EXPR_BINARY_BITWISE_AND: return false;
7139 case EXPR_BINARY_BITWISE_OR: return false;
7140 case EXPR_BINARY_BITWISE_XOR: return false;
7141 case EXPR_BINARY_SHIFTLEFT: return false;
7142 case EXPR_BINARY_SHIFTRIGHT: return false;
7143 case EXPR_BINARY_ASSIGN: return true;
7144 case EXPR_BINARY_MUL_ASSIGN: return true;
7145 case EXPR_BINARY_DIV_ASSIGN: return true;
7146 case EXPR_BINARY_MOD_ASSIGN: return true;
7147 case EXPR_BINARY_ADD_ASSIGN: return true;
7148 case EXPR_BINARY_SUB_ASSIGN: return true;
7149 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7150 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7151 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7152 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7153 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7155 /* Only examine the right hand side of && and ||, because the left hand
7156 * side already has the effect of controlling the execution of the right
7158 case EXPR_BINARY_LOGICAL_AND:
7159 case EXPR_BINARY_LOGICAL_OR:
7160 /* Only examine the right hand side of a comma expression, because the left
7161 * hand side has a separate warning */
7162 case EXPR_BINARY_COMMA:
7163 return expression_has_effect(expr->binary.right);
7165 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7166 case EXPR_BINARY_ISGREATER: return false;
7167 case EXPR_BINARY_ISGREATEREQUAL: return false;
7168 case EXPR_BINARY_ISLESS: return false;
7169 case EXPR_BINARY_ISLESSEQUAL: return false;
7170 case EXPR_BINARY_ISLESSGREATER: return false;
7171 case EXPR_BINARY_ISUNORDERED: return false;
7174 internal_errorf(HERE, "unexpected expression");
7177 static void semantic_comma(binary_expression_t *expression)
7179 if (warning.unused_value) {
7180 const expression_t *const left = expression->left;
7181 if (!expression_has_effect(left)) {
7182 warningf(&left->base.source_position,
7183 "left-hand operand of comma expression has no effect");
7186 expression->base.type = expression->right->base.type;
7189 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7190 static expression_t *parse_##binexpression_type(unsigned precedence, \
7191 expression_t *left) \
7194 source_position_t pos = *HERE; \
7196 expression_t *right = parse_sub_expression(precedence + lr); \
7198 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7199 binexpr->base.source_position = pos; \
7200 binexpr->binary.left = left; \
7201 binexpr->binary.right = right; \
7202 sfunc(&binexpr->binary); \
7207 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7208 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7209 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
7210 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
7211 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7212 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7213 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7214 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7215 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7217 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7218 semantic_comparison, 1)
7219 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7220 semantic_comparison, 1)
7221 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7222 semantic_comparison, 1)
7223 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7224 semantic_comparison, 1)
7226 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7227 semantic_binexpr_arithmetic, 1)
7228 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7229 semantic_binexpr_arithmetic, 1)
7230 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7231 semantic_binexpr_arithmetic, 1)
7232 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7233 semantic_logical_op, 1)
7234 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7235 semantic_logical_op, 1)
7236 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7237 semantic_shift_op, 1)
7238 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7239 semantic_shift_op, 1)
7240 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7241 semantic_arithmetic_addsubb_assign, 0)
7242 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7243 semantic_arithmetic_addsubb_assign, 0)
7244 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7245 semantic_arithmetic_assign, 0)
7246 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7247 semantic_arithmetic_assign, 0)
7248 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7249 semantic_arithmetic_assign, 0)
7250 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7251 semantic_arithmetic_assign, 0)
7252 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7253 semantic_arithmetic_assign, 0)
7254 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7255 semantic_arithmetic_assign, 0)
7256 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7257 semantic_arithmetic_assign, 0)
7258 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7259 semantic_arithmetic_assign, 0)
7261 static expression_t *parse_sub_expression(unsigned precedence)
7263 if (token.type < 0) {
7264 return expected_expression_error();
7267 expression_parser_function_t *parser
7268 = &expression_parsers[token.type];
7269 source_position_t source_position = token.source_position;
7272 if (parser->parser != NULL) {
7273 left = parser->parser(parser->precedence);
7275 left = parse_primary_expression();
7277 assert(left != NULL);
7278 left->base.source_position = source_position;
7281 if (token.type < 0) {
7282 return expected_expression_error();
7285 parser = &expression_parsers[token.type];
7286 if (parser->infix_parser == NULL)
7288 if (parser->infix_precedence < precedence)
7291 left = parser->infix_parser(parser->infix_precedence, left);
7293 assert(left != NULL);
7294 assert(left->kind != EXPR_UNKNOWN);
7295 left->base.source_position = source_position;
7302 * Parse an expression.
7304 static expression_t *parse_expression(void)
7306 return parse_sub_expression(1);
7310 * Register a parser for a prefix-like operator with given precedence.
7312 * @param parser the parser function
7313 * @param token_type the token type of the prefix token
7314 * @param precedence the precedence of the operator
7316 static void register_expression_parser(parse_expression_function parser,
7317 int token_type, unsigned precedence)
7319 expression_parser_function_t *entry = &expression_parsers[token_type];
7321 if (entry->parser != NULL) {
7322 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7323 panic("trying to register multiple expression parsers for a token");
7325 entry->parser = parser;
7326 entry->precedence = precedence;
7330 * Register a parser for an infix operator with given precedence.
7332 * @param parser the parser function
7333 * @param token_type the token type of the infix operator
7334 * @param precedence the precedence of the operator
7336 static void register_infix_parser(parse_expression_infix_function parser,
7337 int token_type, unsigned precedence)
7339 expression_parser_function_t *entry = &expression_parsers[token_type];
7341 if (entry->infix_parser != NULL) {
7342 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7343 panic("trying to register multiple infix expression parsers for a "
7346 entry->infix_parser = parser;
7347 entry->infix_precedence = precedence;
7351 * Initialize the expression parsers.
7353 static void init_expression_parsers(void)
7355 memset(&expression_parsers, 0, sizeof(expression_parsers));
7357 register_infix_parser(parse_array_expression, '[', 30);
7358 register_infix_parser(parse_call_expression, '(', 30);
7359 register_infix_parser(parse_select_expression, '.', 30);
7360 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
7361 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
7363 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
7366 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
7367 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
7368 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
7369 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
7370 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
7371 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
7372 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
7373 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
7374 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
7375 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
7376 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
7377 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
7378 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
7379 T_EXCLAMATIONMARKEQUAL, 13);
7380 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
7381 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
7382 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
7383 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
7384 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
7385 register_infix_parser(parse_conditional_expression, '?', 7);
7386 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
7387 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
7388 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
7389 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
7390 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
7391 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
7392 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
7393 T_LESSLESSEQUAL, 2);
7394 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7395 T_GREATERGREATEREQUAL, 2);
7396 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
7398 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
7400 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
7403 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
7405 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
7406 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
7407 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
7408 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
7409 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
7410 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
7411 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
7413 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
7415 register_expression_parser(parse_sizeof, T_sizeof, 25);
7416 register_expression_parser(parse_alignof, T___alignof__, 25);
7417 register_expression_parser(parse_extension, T___extension__, 25);
7418 register_expression_parser(parse_builtin_classify_type,
7419 T___builtin_classify_type, 25);
7423 * Parse a asm statement arguments specification.
7425 static asm_argument_t *parse_asm_arguments(bool is_out)
7427 asm_argument_t *result = NULL;
7428 asm_argument_t *last = NULL;
7430 while (token.type == T_STRING_LITERAL || token.type == '[') {
7431 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7432 memset(argument, 0, sizeof(argument[0]));
7434 if (token.type == '[') {
7436 if (token.type != T_IDENTIFIER) {
7437 parse_error_expected("while parsing asm argument",
7438 T_IDENTIFIER, NULL);
7441 argument->symbol = token.v.symbol;
7446 argument->constraints = parse_string_literals();
7448 add_anchor_token(')');
7449 expression_t *expression = parse_expression();
7450 rem_anchor_token(')');
7452 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
7453 * change size or type representation (e.g. int -> long is ok, but
7454 * int -> float is not) */
7455 if (expression->kind == EXPR_UNARY_CAST) {
7456 type_t *const type = expression->base.type;
7457 type_kind_t const kind = type->kind;
7458 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
7461 if (kind == TYPE_ATOMIC) {
7462 atomic_type_kind_t const akind = type->atomic.akind;
7463 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7464 size = get_atomic_type_size(akind);
7466 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7467 size = get_atomic_type_size(get_intptr_kind());
7471 expression_t *const value = expression->unary.value;
7472 type_t *const value_type = value->base.type;
7473 type_kind_t const value_kind = value_type->kind;
7475 unsigned value_flags;
7476 unsigned value_size;
7477 if (value_kind == TYPE_ATOMIC) {
7478 atomic_type_kind_t const value_akind = value_type->atomic.akind;
7479 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7480 value_size = get_atomic_type_size(value_akind);
7481 } else if (value_kind == TYPE_POINTER) {
7482 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7483 value_size = get_atomic_type_size(get_intptr_kind());
7488 if (value_flags != flags || value_size != size)
7492 } while (expression->kind == EXPR_UNARY_CAST);
7496 if (!is_lvalue(expression)) {
7497 errorf(&expression->base.source_position,
7498 "asm output argument is not an lvalue");
7501 argument->expression = expression;
7504 set_address_taken(expression, true);
7507 last->next = argument;
7513 if (token.type != ',')
7524 * Parse a asm statement clobber specification.
7526 static asm_clobber_t *parse_asm_clobbers(void)
7528 asm_clobber_t *result = NULL;
7529 asm_clobber_t *last = NULL;
7531 while(token.type == T_STRING_LITERAL) {
7532 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
7533 clobber->clobber = parse_string_literals();
7536 last->next = clobber;
7542 if (token.type != ',')
7551 * Parse an asm statement.
7553 static statement_t *parse_asm_statement(void)
7557 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
7558 statement->base.source_position = token.source_position;
7560 asm_statement_t *asm_statement = &statement->asms;
7562 if (token.type == T_volatile) {
7564 asm_statement->is_volatile = true;
7568 add_anchor_token(')');
7569 add_anchor_token(':');
7570 asm_statement->asm_text = parse_string_literals();
7572 if (token.type != ':') {
7573 rem_anchor_token(':');
7578 asm_statement->outputs = parse_asm_arguments(true);
7579 if (token.type != ':') {
7580 rem_anchor_token(':');
7585 asm_statement->inputs = parse_asm_arguments(false);
7586 if (token.type != ':') {
7587 rem_anchor_token(':');
7590 rem_anchor_token(':');
7593 asm_statement->clobbers = parse_asm_clobbers();
7596 rem_anchor_token(')');
7600 if (asm_statement->outputs == NULL) {
7601 /* GCC: An 'asm' instruction without any output operands will be treated
7602 * identically to a volatile 'asm' instruction. */
7603 asm_statement->is_volatile = true;
7608 return create_invalid_statement();
7612 * Parse a case statement.
7614 static statement_t *parse_case_statement(void)
7618 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7620 statement->base.source_position = token.source_position;
7621 statement->case_label.expression = parse_expression();
7623 if (c_mode & _GNUC) {
7624 if (token.type == T_DOTDOTDOT) {
7626 statement->case_label.end_range = parse_expression();
7632 if (! is_constant_expression(statement->case_label.expression)) {
7633 errorf(&statement->base.source_position,
7634 "case label does not reduce to an integer constant");
7636 /* TODO: check if the case label is already known */
7637 if (current_switch != NULL) {
7638 /* link all cases into the switch statement */
7639 if (current_switch->last_case == NULL) {
7640 current_switch->first_case =
7641 current_switch->last_case = &statement->case_label;
7643 current_switch->last_case->next = &statement->case_label;
7646 errorf(&statement->base.source_position,
7647 "case label not within a switch statement");
7650 statement->case_label.statement = parse_statement();
7654 return create_invalid_statement();
7658 * Finds an existing default label of a switch statement.
7660 static case_label_statement_t *
7661 find_default_label(const switch_statement_t *statement)
7663 case_label_statement_t *label = statement->first_case;
7664 for ( ; label != NULL; label = label->next) {
7665 if (label->expression == NULL)
7672 * Parse a default statement.
7674 static statement_t *parse_default_statement(void)
7678 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7680 statement->base.source_position = token.source_position;
7683 if (current_switch != NULL) {
7684 const case_label_statement_t *def_label = find_default_label(current_switch);
7685 if (def_label != NULL) {
7686 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7687 &def_label->base.source_position);
7689 /* link all cases into the switch statement */
7690 if (current_switch->last_case == NULL) {
7691 current_switch->first_case =
7692 current_switch->last_case = &statement->case_label;
7694 current_switch->last_case->next = &statement->case_label;
7698 errorf(&statement->base.source_position,
7699 "'default' label not within a switch statement");
7701 statement->case_label.statement = parse_statement();
7705 return create_invalid_statement();
7709 * Return the declaration for a given label symbol or create a new one.
7711 * @param symbol the symbol of the label
7713 static declaration_t *get_label(symbol_t *symbol)
7715 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7716 assert(current_function != NULL);
7717 /* if we found a label in the same function, then we already created the
7719 if (candidate != NULL
7720 && candidate->parent_scope == ¤t_function->scope) {
7724 /* otherwise we need to create a new one */
7725 declaration_t *const declaration = allocate_declaration_zero();
7726 declaration->namespc = NAMESPACE_LABEL;
7727 declaration->symbol = symbol;
7729 label_push(declaration);
7735 * Parse a label statement.
7737 static statement_t *parse_label_statement(void)
7739 assert(token.type == T_IDENTIFIER);
7740 symbol_t *symbol = token.v.symbol;
7743 declaration_t *label = get_label(symbol);
7745 /* if source position is already set then the label is defined twice,
7746 * otherwise it was just mentioned in a goto so far */
7747 if (label->source_position.input_name != NULL) {
7748 errorf(HERE, "duplicate label '%Y' (declared %P)",
7749 symbol, &label->source_position);
7751 label->source_position = token.source_position;
7754 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7756 statement->base.source_position = token.source_position;
7757 statement->label.label = label;
7761 if (token.type == '}') {
7762 /* TODO only warn? */
7764 warningf(HERE, "label at end of compound statement");
7765 statement->label.statement = create_empty_statement();
7767 errorf(HERE, "label at end of compound statement");
7768 statement->label.statement = create_invalid_statement();
7771 if (token.type == ';') {
7772 /* eat an empty statement here, to avoid the warning about an empty
7773 * after a label. label:; is commonly used to have a label before
7775 statement->label.statement = create_empty_statement();
7778 statement->label.statement = parse_statement();
7782 /* remember the labels in a list for later checking */
7783 if (label_last == NULL) {
7784 label_first = &statement->label;
7786 label_last->next = &statement->label;
7788 label_last = &statement->label;
7794 * Parse an if statement.
7796 static statement_t *parse_if(void)
7800 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7801 statement->base.source_position = token.source_position;
7804 add_anchor_token(')');
7805 statement->ifs.condition = parse_expression();
7806 rem_anchor_token(')');
7809 add_anchor_token(T_else);
7810 statement->ifs.true_statement = parse_statement();
7811 rem_anchor_token(T_else);
7813 if (token.type == T_else) {
7815 statement->ifs.false_statement = parse_statement();
7820 return create_invalid_statement();
7824 * Parse a switch statement.
7826 static statement_t *parse_switch(void)
7830 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7831 statement->base.source_position = token.source_position;
7834 expression_t *const expr = parse_expression();
7835 type_t * type = skip_typeref(expr->base.type);
7836 if (is_type_integer(type)) {
7837 type = promote_integer(type);
7838 } else if (is_type_valid(type)) {
7839 errorf(&expr->base.source_position,
7840 "switch quantity is not an integer, but '%T'", type);
7841 type = type_error_type;
7843 statement->switchs.expression = create_implicit_cast(expr, type);
7846 switch_statement_t *rem = current_switch;
7847 current_switch = &statement->switchs;
7848 statement->switchs.body = parse_statement();
7849 current_switch = rem;
7851 if (warning.switch_default &&
7852 find_default_label(&statement->switchs) == NULL) {
7853 warningf(&statement->base.source_position, "switch has no default case");
7858 return create_invalid_statement();
7861 static statement_t *parse_loop_body(statement_t *const loop)
7863 statement_t *const rem = current_loop;
7864 current_loop = loop;
7866 statement_t *const body = parse_statement();
7873 * Parse a while statement.
7875 static statement_t *parse_while(void)
7879 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7880 statement->base.source_position = token.source_position;
7883 add_anchor_token(')');
7884 statement->whiles.condition = parse_expression();
7885 rem_anchor_token(')');
7888 statement->whiles.body = parse_loop_body(statement);
7892 return create_invalid_statement();
7896 * Parse a do statement.
7898 static statement_t *parse_do(void)
7902 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7904 statement->base.source_position = token.source_position;
7906 add_anchor_token(T_while);
7907 statement->do_while.body = parse_loop_body(statement);
7908 rem_anchor_token(T_while);
7912 add_anchor_token(')');
7913 statement->do_while.condition = parse_expression();
7914 rem_anchor_token(')');
7920 return create_invalid_statement();
7924 * Parse a for statement.
7926 static statement_t *parse_for(void)
7930 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7931 statement->base.source_position = token.source_position;
7933 int top = environment_top();
7934 scope_t *last_scope = scope;
7935 set_scope(&statement->fors.scope);
7938 add_anchor_token(')');
7940 if (token.type != ';') {
7941 if (is_declaration_specifier(&token, false)) {
7942 parse_declaration(record_declaration);
7944 add_anchor_token(';');
7945 expression_t *const init = parse_expression();
7946 statement->fors.initialisation = init;
7947 if (warning.unused_value && !expression_has_effect(init)) {
7948 warningf(&init->base.source_position,
7949 "initialisation of 'for'-statement has no effect");
7951 rem_anchor_token(';');
7958 if (token.type != ';') {
7959 add_anchor_token(';');
7960 statement->fors.condition = parse_expression();
7961 rem_anchor_token(';');
7964 if (token.type != ')') {
7965 expression_t *const step = parse_expression();
7966 statement->fors.step = step;
7967 if (warning.unused_value && !expression_has_effect(step)) {
7968 warningf(&step->base.source_position,
7969 "step of 'for'-statement has no effect");
7972 rem_anchor_token(')');
7974 statement->fors.body = parse_loop_body(statement);
7976 assert(scope == &statement->fors.scope);
7977 set_scope(last_scope);
7978 environment_pop_to(top);
7983 rem_anchor_token(')');
7984 assert(scope == &statement->fors.scope);
7985 set_scope(last_scope);
7986 environment_pop_to(top);
7988 return create_invalid_statement();
7992 * Parse a goto statement.
7994 static statement_t *parse_goto(void)
7998 if (token.type != T_IDENTIFIER) {
7999 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8003 symbol_t *symbol = token.v.symbol;
8006 declaration_t *label = get_label(symbol);
8008 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8009 statement->base.source_position = token.source_position;
8011 statement->gotos.label = label;
8013 /* remember the goto's in a list for later checking */
8014 if (goto_last == NULL) {
8015 goto_first = &statement->gotos;
8017 goto_last->next = &statement->gotos;
8019 goto_last = &statement->gotos;
8025 return create_invalid_statement();
8029 * Parse a continue statement.
8031 static statement_t *parse_continue(void)
8033 statement_t *statement;
8034 if (current_loop == NULL) {
8035 errorf(HERE, "continue statement not within loop");
8036 statement = create_invalid_statement();
8038 statement = allocate_statement_zero(STATEMENT_CONTINUE);
8040 statement->base.source_position = token.source_position;
8048 return create_invalid_statement();
8052 * Parse a break statement.
8054 static statement_t *parse_break(void)
8056 statement_t *statement;
8057 if (current_switch == NULL && current_loop == NULL) {
8058 errorf(HERE, "break statement not within loop or switch");
8059 statement = create_invalid_statement();
8061 statement = allocate_statement_zero(STATEMENT_BREAK);
8063 statement->base.source_position = token.source_position;
8071 return create_invalid_statement();
8075 * Parse a __leave statement.
8077 static statement_t *parse_leave(void)
8079 statement_t *statement;
8080 if (current_try == NULL) {
8081 errorf(HERE, "__leave statement not within __try");
8082 statement = create_invalid_statement();
8084 statement = allocate_statement_zero(STATEMENT_LEAVE);
8086 statement->base.source_position = token.source_position;
8094 return create_invalid_statement();
8098 * Check if a given declaration represents a local variable.
8100 static bool is_local_var_declaration(const declaration_t *declaration) {
8101 switch ((storage_class_tag_t) declaration->storage_class) {
8102 case STORAGE_CLASS_AUTO:
8103 case STORAGE_CLASS_REGISTER: {
8104 const type_t *type = skip_typeref(declaration->type);
8105 if (is_type_function(type)) {
8117 * Check if a given declaration represents a variable.
8119 static bool is_var_declaration(const declaration_t *declaration) {
8120 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8123 const type_t *type = skip_typeref(declaration->type);
8124 return !is_type_function(type);
8128 * Check if a given expression represents a local variable.
8130 static bool is_local_variable(const expression_t *expression)
8132 if (expression->base.kind != EXPR_REFERENCE) {
8135 const declaration_t *declaration = expression->reference.declaration;
8136 return is_local_var_declaration(declaration);
8140 * Check if a given expression represents a local variable and
8141 * return its declaration then, else return NULL.
8143 declaration_t *expr_is_variable(const expression_t *expression)
8145 if (expression->base.kind != EXPR_REFERENCE) {
8148 declaration_t *declaration = expression->reference.declaration;
8149 if (is_var_declaration(declaration))
8155 * Parse a return statement.
8157 static statement_t *parse_return(void)
8159 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8160 statement->base.source_position = token.source_position;
8164 expression_t *return_value = NULL;
8165 if (token.type != ';') {
8166 return_value = parse_expression();
8170 const type_t *const func_type = current_function->type;
8171 assert(is_type_function(func_type));
8172 type_t *const return_type = skip_typeref(func_type->function.return_type);
8174 if (return_value != NULL) {
8175 type_t *return_value_type = skip_typeref(return_value->base.type);
8177 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8178 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8179 warningf(&statement->base.source_position,
8180 "'return' with a value, in function returning void");
8181 return_value = NULL;
8183 assign_error_t error = semantic_assign(return_type, return_value);
8184 report_assign_error(error, return_type, return_value, "'return'",
8185 &statement->base.source_position);
8186 return_value = create_implicit_cast(return_value, return_type);
8188 /* check for returning address of a local var */
8189 if (return_value != NULL &&
8190 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
8191 const expression_t *expression = return_value->unary.value;
8192 if (is_local_variable(expression)) {
8193 warningf(&statement->base.source_position,
8194 "function returns address of local variable");
8198 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
8199 warningf(&statement->base.source_position,
8200 "'return' without value, in function returning non-void");
8203 statement->returns.value = return_value;
8207 return create_invalid_statement();
8211 * Parse a declaration statement.
8213 static statement_t *parse_declaration_statement(void)
8215 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
8217 statement->base.source_position = token.source_position;
8219 declaration_t *before = last_declaration;
8220 parse_declaration(record_declaration);
8222 if (before == NULL) {
8223 statement->declaration.declarations_begin = scope->declarations;
8225 statement->declaration.declarations_begin = before->next;
8227 statement->declaration.declarations_end = last_declaration;
8233 * Parse an expression statement, ie. expr ';'.
8235 static statement_t *parse_expression_statement(void)
8237 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
8239 statement->base.source_position = token.source_position;
8240 expression_t *const expr = parse_expression();
8241 statement->expression.expression = expr;
8247 return create_invalid_statement();
8251 * Parse a microsoft __try { } __finally { } or
8252 * __try{ } __except() { }
8254 static statement_t *parse_ms_try_statment(void) {
8255 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
8257 statement->base.source_position = token.source_position;
8260 ms_try_statement_t *rem = current_try;
8261 current_try = &statement->ms_try;
8262 statement->ms_try.try_statement = parse_compound_statement(false);
8265 if (token.type == T___except) {
8268 add_anchor_token(')');
8269 expression_t *const expr = parse_expression();
8270 type_t * type = skip_typeref(expr->base.type);
8271 if (is_type_integer(type)) {
8272 type = promote_integer(type);
8273 } else if (is_type_valid(type)) {
8274 errorf(&expr->base.source_position,
8275 "__expect expression is not an integer, but '%T'", type);
8276 type = type_error_type;
8278 statement->ms_try.except_expression = create_implicit_cast(expr, type);
8279 rem_anchor_token(')');
8281 statement->ms_try.final_statement = parse_compound_statement(false);
8282 } else if (token.type == T__finally) {
8284 statement->ms_try.final_statement = parse_compound_statement(false);
8286 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
8287 return create_invalid_statement();
8291 return create_invalid_statement();
8295 * Parse a statement.
8296 * There's also parse_statement() which additionally checks for
8297 * "statement has no effect" warnings
8299 static statement_t *intern_parse_statement(void)
8301 statement_t *statement = NULL;
8303 /* declaration or statement */
8304 add_anchor_token(';');
8305 switch(token.type) {
8307 statement = parse_asm_statement();
8311 statement = parse_case_statement();
8315 statement = parse_default_statement();
8319 statement = parse_compound_statement(false);
8323 statement = parse_if ();
8327 statement = parse_switch();
8331 statement = parse_while();
8335 statement = parse_do();
8339 statement = parse_for();
8343 statement = parse_goto();
8347 statement = parse_continue();
8351 statement = parse_break();
8355 statement = parse_leave();
8359 statement = parse_return();
8363 if (warning.empty_statement) {
8364 warningf(HERE, "statement is empty");
8366 statement = create_empty_statement();
8371 if (look_ahead(1)->type == ':') {
8372 statement = parse_label_statement();
8376 if (is_typedef_symbol(token.v.symbol)) {
8377 statement = parse_declaration_statement();
8381 statement = parse_expression_statement();
8384 case T___extension__:
8385 /* this can be a prefix to a declaration or an expression statement */
8386 /* we simply eat it now and parse the rest with tail recursion */
8389 } while(token.type == T___extension__);
8390 statement = parse_statement();
8394 statement = parse_declaration_statement();
8398 statement = parse_ms_try_statment();
8402 statement = parse_expression_statement();
8405 rem_anchor_token(';');
8407 assert(statement != NULL
8408 && statement->base.source_position.input_name != NULL);
8414 * parse a statement and emits "statement has no effect" warning if needed
8415 * (This is really a wrapper around intern_parse_statement with check for 1
8416 * single warning. It is needed, because for statement expressions we have
8417 * to avoid the warning on the last statement)
8419 static statement_t *parse_statement(void)
8421 statement_t *statement = intern_parse_statement();
8423 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
8424 expression_t *expression = statement->expression.expression;
8425 if (!expression_has_effect(expression)) {
8426 warningf(&expression->base.source_position,
8427 "statement has no effect");
8435 * Parse a compound statement.
8437 static statement_t *parse_compound_statement(bool inside_expression_statement)
8439 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
8441 statement->base.source_position = token.source_position;
8444 add_anchor_token('}');
8446 int top = environment_top();
8447 scope_t *last_scope = scope;
8448 set_scope(&statement->compound.scope);
8450 statement_t *last_statement = NULL;
8452 while(token.type != '}' && token.type != T_EOF) {
8453 statement_t *sub_statement = intern_parse_statement();
8454 if (is_invalid_statement(sub_statement)) {
8455 /* an error occurred. if we are at an anchor, return */
8461 if (last_statement != NULL) {
8462 last_statement->base.next = sub_statement;
8464 statement->compound.statements = sub_statement;
8467 while(sub_statement->base.next != NULL)
8468 sub_statement = sub_statement->base.next;
8470 last_statement = sub_statement;
8473 if (token.type == '}') {
8476 errorf(&statement->base.source_position,
8477 "end of file while looking for closing '}'");
8480 /* look over all statements again to produce no effect warnings */
8481 if (warning.unused_value) {
8482 statement_t *sub_statement = statement->compound.statements;
8483 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
8484 if (sub_statement->kind != STATEMENT_EXPRESSION)
8486 /* don't emit a warning for the last expression in an expression
8487 * statement as it has always an effect */
8488 if (inside_expression_statement && sub_statement->base.next == NULL)
8491 expression_t *expression = sub_statement->expression.expression;
8492 if (!expression_has_effect(expression)) {
8493 warningf(&expression->base.source_position,
8494 "statement has no effect");
8500 rem_anchor_token('}');
8501 assert(scope == &statement->compound.scope);
8502 set_scope(last_scope);
8503 environment_pop_to(top);
8509 * Initialize builtin types.
8511 static void initialize_builtin_types(void)
8513 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
8514 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
8515 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
8516 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
8517 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
8518 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
8519 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
8520 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
8522 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
8523 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
8524 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
8525 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
8529 * Check for unused global static functions and variables
8531 static void check_unused_globals(void)
8533 if (!warning.unused_function && !warning.unused_variable)
8536 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
8538 decl->modifiers & DM_UNUSED ||
8539 decl->modifiers & DM_USED ||
8540 decl->storage_class != STORAGE_CLASS_STATIC)
8543 type_t *const type = decl->type;
8545 if (is_type_function(skip_typeref(type))) {
8546 if (!warning.unused_function || decl->is_inline)
8549 s = (decl->init.statement != NULL ? "defined" : "declared");
8551 if (!warning.unused_variable)
8557 warningf(&decl->source_position, "'%#T' %s but not used",
8558 type, decl->symbol, s);
8562 static void parse_global_asm(void)
8567 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8568 statement->base.source_position = token.source_position;
8569 statement->asms.asm_text = parse_string_literals();
8570 statement->base.next = unit->global_asm;
8571 unit->global_asm = statement;
8580 * Parse a translation unit.
8582 static void parse_translation_unit(void)
8584 while(token.type != T_EOF) {
8585 switch (token.type) {
8587 /* TODO error in strict mode */
8588 warningf(HERE, "stray ';' outside of function");
8597 parse_external_declaration();
8606 * @return the translation unit or NULL if errors occurred.
8608 void start_parsing(void)
8610 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8611 label_stack = NEW_ARR_F(stack_entry_t, 0);
8612 diagnostic_count = 0;
8616 type_set_output(stderr);
8617 ast_set_output(stderr);
8619 assert(unit == NULL);
8620 unit = allocate_ast_zero(sizeof(unit[0]));
8622 assert(global_scope == NULL);
8623 global_scope = &unit->scope;
8625 assert(scope == NULL);
8626 set_scope(&unit->scope);
8628 initialize_builtin_types();
8631 translation_unit_t *finish_parsing(void)
8633 assert(scope == &unit->scope);
8635 last_declaration = NULL;
8637 assert(global_scope == &unit->scope);
8638 check_unused_globals();
8639 global_scope = NULL;
8641 DEL_ARR_F(environment_stack);
8642 DEL_ARR_F(label_stack);
8644 translation_unit_t *result = unit;
8651 lookahead_bufpos = 0;
8652 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8655 parse_translation_unit();
8659 * Initialize the parser.
8661 void init_parser(void)
8664 /* add predefined symbols for extended-decl-modifier */
8665 sym_align = symbol_table_insert("align");
8666 sym_allocate = symbol_table_insert("allocate");
8667 sym_dllimport = symbol_table_insert("dllimport");
8668 sym_dllexport = symbol_table_insert("dllexport");
8669 sym_naked = symbol_table_insert("naked");
8670 sym_noinline = symbol_table_insert("noinline");
8671 sym_noreturn = symbol_table_insert("noreturn");
8672 sym_nothrow = symbol_table_insert("nothrow");
8673 sym_novtable = symbol_table_insert("novtable");
8674 sym_property = symbol_table_insert("property");
8675 sym_get = symbol_table_insert("get");
8676 sym_put = symbol_table_insert("put");
8677 sym_selectany = symbol_table_insert("selectany");
8678 sym_thread = symbol_table_insert("thread");
8679 sym_uuid = symbol_table_insert("uuid");
8680 sym_deprecated = symbol_table_insert("deprecated");
8681 sym_restrict = symbol_table_insert("restrict");
8682 sym_noalias = symbol_table_insert("noalias");
8684 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8686 init_expression_parsers();
8687 obstack_init(&temp_obst);
8689 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8690 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8694 * Terminate the parser.
8696 void exit_parser(void)
8698 obstack_free(&temp_obst, NULL);