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 \
171 #define TYPE_QUALIFIERS \
176 case T__forceinline: \
177 case T___attribute__:
179 #ifdef PROVIDE_COMPLEX
180 #define COMPLEX_SPECIFIERS \
182 #define IMAGINARY_SPECIFIERS \
185 #define COMPLEX_SPECIFIERS
186 #define IMAGINARY_SPECIFIERS
189 #define TYPE_SPECIFIERS \
204 case T___builtin_va_list: \
209 #define DECLARATION_START \
214 #define TYPENAME_START \
219 * Allocate an AST node with given size and
220 * initialize all fields with zero.
222 static void *allocate_ast_zero(size_t size)
224 void *res = allocate_ast(size);
225 memset(res, 0, size);
229 static declaration_t *allocate_declaration_zero(void)
231 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
232 declaration->type = type_error_type;
233 declaration->alignment = 0;
238 * Returns the size of a statement node.
240 * @param kind the statement kind
242 static size_t get_statement_struct_size(statement_kind_t kind)
244 static const size_t sizes[] = {
245 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
246 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
247 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
248 [STATEMENT_RETURN] = sizeof(return_statement_t),
249 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
250 [STATEMENT_IF] = sizeof(if_statement_t),
251 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
252 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
253 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
254 [STATEMENT_BREAK] = sizeof(statement_base_t),
255 [STATEMENT_GOTO] = sizeof(goto_statement_t),
256 [STATEMENT_LABEL] = sizeof(label_statement_t),
257 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
258 [STATEMENT_WHILE] = sizeof(while_statement_t),
259 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
260 [STATEMENT_FOR] = sizeof(for_statement_t),
261 [STATEMENT_ASM] = sizeof(asm_statement_t),
262 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
263 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
265 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
266 assert(sizes[kind] != 0);
271 * Returns the size of an expression node.
273 * @param kind the expression kind
275 static size_t get_expression_struct_size(expression_kind_t kind)
277 static const size_t sizes[] = {
278 [EXPR_INVALID] = sizeof(expression_base_t),
279 [EXPR_REFERENCE] = sizeof(reference_expression_t),
280 [EXPR_CONST] = sizeof(const_expression_t),
281 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
282 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
283 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
284 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
285 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
286 [EXPR_CALL] = sizeof(call_expression_t),
287 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
288 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
289 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
290 [EXPR_SELECT] = sizeof(select_expression_t),
291 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
292 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
293 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
294 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
295 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
296 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
297 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
298 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
299 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
300 [EXPR_VA_START] = sizeof(va_start_expression_t),
301 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
302 [EXPR_STATEMENT] = sizeof(statement_expression_t),
304 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
305 return sizes[EXPR_UNARY_FIRST];
307 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
308 return sizes[EXPR_BINARY_FIRST];
310 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
311 assert(sizes[kind] != 0);
316 * Allocate a statement node of given kind and initialize all
319 static statement_t *allocate_statement_zero(statement_kind_t kind)
321 size_t size = get_statement_struct_size(kind);
322 statement_t *res = allocate_ast_zero(size);
324 res->base.kind = kind;
329 * Allocate an expression node of given kind and initialize all
332 static expression_t *allocate_expression_zero(expression_kind_t kind)
334 size_t size = get_expression_struct_size(kind);
335 expression_t *res = allocate_ast_zero(size);
337 res->base.kind = kind;
338 res->base.type = type_error_type;
343 * Creates a new invalid expression.
345 static expression_t *create_invalid_expression(void)
347 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
348 expression->base.source_position = token.source_position;
353 * Creates a new invalid statement.
355 static statement_t *create_invalid_statement(void)
357 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
358 statement->base.source_position = token.source_position;
363 * Allocate a new empty statement.
365 static statement_t *create_empty_statement(void)
367 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
368 statement->base.source_position = token.source_position;
373 * Returns the size of a type node.
375 * @param kind the type kind
377 static size_t get_type_struct_size(type_kind_t kind)
379 static const size_t sizes[] = {
380 [TYPE_ATOMIC] = sizeof(atomic_type_t),
381 [TYPE_COMPLEX] = sizeof(complex_type_t),
382 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
383 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
384 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
385 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
386 [TYPE_ENUM] = sizeof(enum_type_t),
387 [TYPE_FUNCTION] = sizeof(function_type_t),
388 [TYPE_POINTER] = sizeof(pointer_type_t),
389 [TYPE_ARRAY] = sizeof(array_type_t),
390 [TYPE_BUILTIN] = sizeof(builtin_type_t),
391 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
392 [TYPE_TYPEOF] = sizeof(typeof_type_t),
394 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
395 assert(kind <= TYPE_TYPEOF);
396 assert(sizes[kind] != 0);
401 * Allocate a type node of given kind and initialize all
404 * @param kind type kind to allocate
405 * @param source_position the source position of the type definition
407 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
409 size_t size = get_type_struct_size(kind);
410 type_t *res = obstack_alloc(type_obst, size);
411 memset(res, 0, size);
413 res->base.kind = kind;
414 res->base.source_position = *source_position;
419 * Returns the size of an initializer node.
421 * @param kind the initializer kind
423 static size_t get_initializer_size(initializer_kind_t kind)
425 static const size_t sizes[] = {
426 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
427 [INITIALIZER_STRING] = sizeof(initializer_string_t),
428 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
429 [INITIALIZER_LIST] = sizeof(initializer_list_t),
430 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
432 assert(kind < sizeof(sizes) / sizeof(*sizes));
433 assert(sizes[kind] != 0);
438 * Allocate an initializer node of given kind and initialize all
441 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
443 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
450 * Free a type from the type obstack.
452 static void free_type(void *type)
454 obstack_free(type_obst, type);
458 * Returns the index of the top element of the environment stack.
460 static size_t environment_top(void)
462 return ARR_LEN(environment_stack);
466 * Returns the index of the top element of the label stack.
468 static size_t label_top(void)
470 return ARR_LEN(label_stack);
474 * Return the next token.
476 static inline void next_token(void)
478 token = lookahead_buffer[lookahead_bufpos];
479 lookahead_buffer[lookahead_bufpos] = lexer_token;
482 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
485 print_token(stderr, &token);
486 fprintf(stderr, "\n");
491 * Return the next token with a given lookahead.
493 static inline const token_t *look_ahead(int num)
495 assert(num > 0 && num <= MAX_LOOKAHEAD);
496 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
497 return &lookahead_buffer[pos];
501 * Adds a token to the token anchor set (a multi-set).
503 static void add_anchor_token(int token_type)
505 assert(0 <= token_type && token_type < T_LAST_TOKEN);
506 ++token_anchor_set[token_type];
509 static int save_and_reset_anchor_state(int token_type)
511 assert(0 <= token_type && token_type < T_LAST_TOKEN);
512 int count = token_anchor_set[token_type];
513 token_anchor_set[token_type] = 0;
517 static void restore_anchor_state(int token_type, int count)
519 assert(0 <= token_type && token_type < T_LAST_TOKEN);
520 token_anchor_set[token_type] = count;
524 * Remove a token from the token anchor set (a multi-set).
526 static void rem_anchor_token(int token_type)
528 assert(0 <= token_type && token_type < T_LAST_TOKEN);
529 --token_anchor_set[token_type];
532 static bool at_anchor(void)
536 return token_anchor_set[token.type];
540 * Eat tokens until a matching token is found.
542 static void eat_until_matching_token(int type)
546 case '(': end_token = ')'; break;
547 case '{': end_token = '}'; break;
548 case '[': end_token = ']'; break;
549 default: end_token = type; break;
552 unsigned parenthesis_count = 0;
553 unsigned brace_count = 0;
554 unsigned bracket_count = 0;
555 while (token.type != end_token ||
556 parenthesis_count != 0 ||
558 bracket_count != 0) {
559 switch (token.type) {
561 case '(': ++parenthesis_count; break;
562 case '{': ++brace_count; break;
563 case '[': ++bracket_count; break;
566 if (parenthesis_count > 0)
576 if (bracket_count > 0)
579 if (token.type == end_token &&
580 parenthesis_count == 0 &&
594 * Eat input tokens until an anchor is found.
596 static void eat_until_anchor(void)
598 if (token.type == T_EOF)
600 while (token_anchor_set[token.type] == 0) {
601 if (token.type == '(' || token.type == '{' || token.type == '[')
602 eat_until_matching_token(token.type);
603 if (token.type == T_EOF)
609 static void eat_block(void)
611 eat_until_matching_token('{');
612 if (token.type == '}')
617 * eat all token until a ';' is reached or a stop token is found.
619 static void eat_statement(void)
621 eat_until_matching_token(';');
622 if (token.type == ';')
626 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
629 * Report a parse error because an expected token was not found.
632 #if defined __GNUC__ && __GNUC__ >= 4
633 __attribute__((sentinel))
635 void parse_error_expected(const char *message, ...)
637 if (message != NULL) {
638 errorf(HERE, "%s", message);
641 va_start(ap, message);
642 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
647 * Report a type error.
649 static void type_error(const char *msg, const source_position_t *source_position,
652 errorf(source_position, "%s, but found type '%T'", msg, type);
656 * Report an incompatible type.
658 static void type_error_incompatible(const char *msg,
659 const source_position_t *source_position, type_t *type1, type_t *type2)
661 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
666 * Expect the the current token is the expected token.
667 * If not, generate an error, eat the current statement,
668 * and goto the end_error label.
670 #define expect(expected) \
672 if (UNLIKELY(token.type != (expected))) { \
673 parse_error_expected(NULL, (expected), NULL); \
674 add_anchor_token(expected); \
675 eat_until_anchor(); \
676 if (token.type == expected) \
678 rem_anchor_token(expected); \
684 static void set_scope(scope_t *new_scope)
687 scope->last_declaration = last_declaration;
691 last_declaration = new_scope->last_declaration;
695 * Search a symbol in a given namespace and returns its declaration or
696 * NULL if this symbol was not found.
698 static declaration_t *get_declaration(const symbol_t *const symbol,
699 const namespace_t namespc)
701 declaration_t *declaration = symbol->declaration;
702 for( ; declaration != NULL; declaration = declaration->symbol_next) {
703 if (declaration->namespc == namespc)
711 * pushs an environment_entry on the environment stack and links the
712 * corresponding symbol to the new entry
714 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
716 symbol_t *symbol = declaration->symbol;
717 namespace_t namespc = (namespace_t) declaration->namespc;
719 /* replace/add declaration into declaration list of the symbol */
720 declaration_t *iter = symbol->declaration;
722 symbol->declaration = declaration;
724 declaration_t *iter_last = NULL;
725 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
726 /* replace an entry? */
727 if (iter->namespc == namespc) {
728 if (iter_last == NULL) {
729 symbol->declaration = declaration;
731 iter_last->symbol_next = declaration;
733 declaration->symbol_next = iter->symbol_next;
738 assert(iter_last->symbol_next == NULL);
739 iter_last->symbol_next = declaration;
743 /* remember old declaration */
745 entry.symbol = symbol;
746 entry.old_declaration = iter;
747 entry.namespc = (unsigned short) namespc;
748 ARR_APP1(stack_entry_t, *stack_ptr, entry);
751 static void environment_push(declaration_t *declaration)
753 assert(declaration->source_position.input_name != NULL);
754 assert(declaration->parent_scope != NULL);
755 stack_push(&environment_stack, declaration);
759 * Push a declaration of the label stack.
761 * @param declaration the declaration
763 static void label_push(declaration_t *declaration)
765 declaration->parent_scope = ¤t_function->scope;
766 stack_push(&label_stack, declaration);
770 * pops symbols from the environment stack until @p new_top is the top element
772 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
774 stack_entry_t *stack = *stack_ptr;
775 size_t top = ARR_LEN(stack);
778 assert(new_top <= top);
782 for(i = top; i > new_top; --i) {
783 stack_entry_t *entry = &stack[i - 1];
785 declaration_t *old_declaration = entry->old_declaration;
786 symbol_t *symbol = entry->symbol;
787 namespace_t namespc = (namespace_t)entry->namespc;
789 /* replace/remove declaration */
790 declaration_t *declaration = symbol->declaration;
791 assert(declaration != NULL);
792 if (declaration->namespc == namespc) {
793 if (old_declaration == NULL) {
794 symbol->declaration = declaration->symbol_next;
796 symbol->declaration = old_declaration;
799 declaration_t *iter_last = declaration;
800 declaration_t *iter = declaration->symbol_next;
801 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
802 /* replace an entry? */
803 if (iter->namespc == namespc) {
804 assert(iter_last != NULL);
805 iter_last->symbol_next = old_declaration;
806 if (old_declaration != NULL) {
807 old_declaration->symbol_next = iter->symbol_next;
812 assert(iter != NULL);
816 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
819 static void environment_pop_to(size_t new_top)
821 stack_pop_to(&environment_stack, new_top);
825 * Pop all entries on the label stack until the new_top
828 * @param new_top the new stack top
830 static void label_pop_to(size_t new_top)
832 stack_pop_to(&label_stack, new_top);
836 static int get_rank(const type_t *type)
838 assert(!is_typeref(type));
839 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
840 * and esp. footnote 108). However we can't fold constants (yet), so we
841 * can't decide whether unsigned int is possible, while int always works.
842 * (unsigned int would be preferable when possible... for stuff like
843 * struct { enum { ... } bla : 4; } ) */
844 if (type->kind == TYPE_ENUM)
845 return ATOMIC_TYPE_INT;
847 assert(type->kind == TYPE_ATOMIC);
848 return type->atomic.akind;
851 static type_t *promote_integer(type_t *type)
853 if (type->kind == TYPE_BITFIELD)
854 type = type->bitfield.base_type;
856 if (get_rank(type) < ATOMIC_TYPE_INT)
863 * Create a cast expression.
865 * @param expression the expression to cast
866 * @param dest_type the destination type
868 static expression_t *create_cast_expression(expression_t *expression,
871 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
873 cast->unary.value = expression;
874 cast->base.type = dest_type;
880 * Check if a given expression represents the 0 pointer constant.
882 static bool is_null_pointer_constant(const expression_t *expression)
884 /* skip void* cast */
885 if (expression->kind == EXPR_UNARY_CAST
886 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
887 expression = expression->unary.value;
890 /* TODO: not correct yet, should be any constant integer expression
891 * which evaluates to 0 */
892 if (expression->kind != EXPR_CONST)
895 type_t *const type = skip_typeref(expression->base.type);
896 if (!is_type_integer(type))
899 return expression->conste.v.int_value == 0;
903 * Create an implicit cast expression.
905 * @param expression the expression to cast
906 * @param dest_type the destination type
908 static expression_t *create_implicit_cast(expression_t *expression,
911 type_t *const source_type = expression->base.type;
913 if (source_type == dest_type)
916 return create_cast_expression(expression, dest_type);
919 typedef enum assign_error_t {
921 ASSIGN_ERROR_INCOMPATIBLE,
922 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
923 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
924 ASSIGN_WARNING_POINTER_FROM_INT,
925 ASSIGN_WARNING_INT_FROM_POINTER
928 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
929 const expression_t *const right,
931 const source_position_t *source_position)
933 type_t *const orig_type_right = right->base.type;
934 type_t *const type_left = skip_typeref(orig_type_left);
935 type_t *const type_right = skip_typeref(orig_type_right);
940 case ASSIGN_ERROR_INCOMPATIBLE:
941 errorf(source_position,
942 "destination type '%T' in %s is incompatible with type '%T'",
943 orig_type_left, context, orig_type_right);
946 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
947 type_t *points_to_left
948 = skip_typeref(type_left->pointer.points_to);
949 type_t *points_to_right
950 = skip_typeref(type_right->pointer.points_to);
952 /* the left type has all qualifiers from the right type */
953 unsigned missing_qualifiers
954 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
955 errorf(source_position,
956 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type",
957 orig_type_left, context, orig_type_right, missing_qualifiers);
961 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
962 warningf(source_position,
963 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
964 orig_type_left, context, right, orig_type_right);
967 case ASSIGN_WARNING_POINTER_FROM_INT:
968 warningf(source_position,
969 "%s makes integer '%T' from pointer '%T' without a cast",
970 context, orig_type_left, orig_type_right);
973 case ASSIGN_WARNING_INT_FROM_POINTER:
974 warningf(source_position,
975 "%s makes integer '%T' from pointer '%T' without a cast",
976 context, orig_type_left, orig_type_right);
980 panic("invalid error value");
984 /** Implements the rules from § 6.5.16.1 */
985 static assign_error_t semantic_assign(type_t *orig_type_left,
986 const expression_t *const right)
988 type_t *const orig_type_right = right->base.type;
989 type_t *const type_left = skip_typeref(orig_type_left);
990 type_t *const type_right = skip_typeref(orig_type_right);
992 if (is_type_pointer(type_left)) {
993 if (is_null_pointer_constant(right)) {
994 return ASSIGN_SUCCESS;
995 } else if (is_type_pointer(type_right)) {
996 type_t *points_to_left
997 = skip_typeref(type_left->pointer.points_to);
998 type_t *points_to_right
999 = skip_typeref(type_right->pointer.points_to);
1001 /* the left type has all qualifiers from the right type */
1002 unsigned missing_qualifiers
1003 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1004 if (missing_qualifiers != 0) {
1005 return ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1008 points_to_left = get_unqualified_type(points_to_left);
1009 points_to_right = get_unqualified_type(points_to_right);
1011 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1012 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1013 return ASSIGN_SUCCESS;
1016 if (!types_compatible(points_to_left, points_to_right)) {
1017 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1020 return ASSIGN_SUCCESS;
1021 } else if (is_type_integer(type_right)) {
1022 return ASSIGN_WARNING_POINTER_FROM_INT;
1024 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1025 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1026 && is_type_pointer(type_right))) {
1027 return ASSIGN_SUCCESS;
1028 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1029 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1030 type_t *const unqual_type_left = get_unqualified_type(type_left);
1031 type_t *const unqual_type_right = get_unqualified_type(type_right);
1032 if (types_compatible(unqual_type_left, unqual_type_right)) {
1033 return ASSIGN_SUCCESS;
1035 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1036 return ASSIGN_WARNING_INT_FROM_POINTER;
1039 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1040 return ASSIGN_SUCCESS;
1042 return ASSIGN_ERROR_INCOMPATIBLE;
1045 static expression_t *parse_constant_expression(void)
1047 /* start parsing at precedence 7 (conditional expression) */
1048 expression_t *result = parse_sub_expression(7);
1050 if (!is_constant_expression(result)) {
1051 errorf(&result->base.source_position,
1052 "expression '%E' is not constant\n", result);
1058 static expression_t *parse_assignment_expression(void)
1060 /* start parsing at precedence 2 (assignment expression) */
1061 return parse_sub_expression(2);
1064 static type_t *make_global_typedef(const char *name, type_t *type)
1066 symbol_t *const symbol = symbol_table_insert(name);
1068 declaration_t *const declaration = allocate_declaration_zero();
1069 declaration->namespc = NAMESPACE_NORMAL;
1070 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1071 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1072 declaration->type = type;
1073 declaration->symbol = symbol;
1074 declaration->source_position = builtin_source_position;
1076 record_declaration(declaration);
1078 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1079 typedef_type->typedeft.declaration = declaration;
1081 return typedef_type;
1084 static string_t parse_string_literals(void)
1086 assert(token.type == T_STRING_LITERAL);
1087 string_t result = token.v.string;
1091 while (token.type == T_STRING_LITERAL) {
1092 result = concat_strings(&result, &token.v.string);
1099 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1100 [GNU_AK_CONST] = "const",
1101 [GNU_AK_VOLATILE] = "volatile",
1102 [GNU_AK_CDECL] = "cdecl",
1103 [GNU_AK_STDCALL] = "stdcall",
1104 [GNU_AK_FASTCALL] = "fastcall",
1105 [GNU_AK_DEPRECATED] = "deprecated",
1106 [GNU_AK_NOINLINE] = "noinline",
1107 [GNU_AK_NORETURN] = "noreturn",
1108 [GNU_AK_NAKED] = "naked",
1109 [GNU_AK_PURE] = "pure",
1110 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1111 [GNU_AK_MALLOC] = "malloc",
1112 [GNU_AK_WEAK] = "weak",
1113 [GNU_AK_CONSTRUCTOR] = "constructor",
1114 [GNU_AK_DESTRUCTOR] = "destructor",
1115 [GNU_AK_NOTHROW] = "nothrow",
1116 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1117 [GNU_AK_COMMON] = "common",
1118 [GNU_AK_NOCOMMON] = "nocommon",
1119 [GNU_AK_PACKED] = "packed",
1120 [GNU_AK_SHARED] = "shared",
1121 [GNU_AK_NOTSHARED] = "notshared",
1122 [GNU_AK_USED] = "used",
1123 [GNU_AK_UNUSED] = "unused",
1124 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1125 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1126 [GNU_AK_LONGCALL] = "longcall",
1127 [GNU_AK_SHORTCALL] = "shortcall",
1128 [GNU_AK_LONG_CALL] = "long_call",
1129 [GNU_AK_SHORT_CALL] = "short_call",
1130 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1131 [GNU_AK_INTERRUPT] = "interrupt",
1132 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1133 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1134 [GNU_AK_NESTING] = "nesting",
1135 [GNU_AK_NEAR] = "near",
1136 [GNU_AK_FAR] = "far",
1137 [GNU_AK_SIGNAL] = "signal",
1138 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1139 [GNU_AK_TINY_DATA] = "tiny_data",
1140 [GNU_AK_SAVEALL] = "saveall",
1141 [GNU_AK_FLATTEN] = "flatten",
1142 [GNU_AK_SSEREGPARM] = "sseregparm",
1143 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1144 [GNU_AK_RETURN_TWICE] = "return_twice",
1145 [GNU_AK_MAY_ALIAS] = "may_alias",
1146 [GNU_AK_MS_STRUCT] = "ms_struct",
1147 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1148 [GNU_AK_DLLIMPORT] = "dllimport",
1149 [GNU_AK_DLLEXPORT] = "dllexport",
1150 [GNU_AK_ALIGNED] = "aligned",
1151 [GNU_AK_ALIAS] = "alias",
1152 [GNU_AK_SECTION] = "section",
1153 [GNU_AK_FORMAT] = "format",
1154 [GNU_AK_FORMAT_ARG] = "format_arg",
1155 [GNU_AK_WEAKREF] = "weakref",
1156 [GNU_AK_NONNULL] = "nonnull",
1157 [GNU_AK_TLS_MODEL] = "tls_model",
1158 [GNU_AK_VISIBILITY] = "visibility",
1159 [GNU_AK_REGPARM] = "regparm",
1160 [GNU_AK_MODE] = "mode",
1161 [GNU_AK_MODEL] = "model",
1162 [GNU_AK_TRAP_EXIT] = "trap_exit",
1163 [GNU_AK_SP_SWITCH] = "sp_switch",
1164 [GNU_AK_SENTINEL] = "sentinel"
1168 * compare two string, ignoring double underscores on the second.
1170 static int strcmp_underscore(const char *s1, const char *s2)
1172 if (s2[0] == '_' && s2[1] == '_') {
1173 size_t len2 = strlen(s2);
1174 size_t len1 = strlen(s1);
1175 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1176 return strncmp(s1, s2+2, len2-4);
1180 return strcmp(s1, s2);
1184 * Allocate a new gnu temporal attribute.
1186 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1188 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1189 attribute->kind = kind;
1190 attribute->next = NULL;
1191 attribute->invalid = false;
1192 attribute->have_arguments = false;
1198 * parse one constant expression argument.
1200 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1202 expression_t *expression;
1203 add_anchor_token(')');
1204 expression = parse_constant_expression();
1205 rem_anchor_token(')');
1207 attribute->u.argument = fold_constant(expression);
1210 attribute->invalid = true;
1214 * parse a list of constant expressions arguments.
1216 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1218 argument_list_t **list = &attribute->u.arguments;
1219 argument_list_t *entry;
1220 expression_t *expression;
1221 add_anchor_token(')');
1222 add_anchor_token(',');
1224 expression = parse_constant_expression();
1225 entry = obstack_alloc(&temp_obst, sizeof(entry));
1226 entry->argument = fold_constant(expression);
1229 list = &entry->next;
1230 if (token.type != ',')
1234 rem_anchor_token(',');
1235 rem_anchor_token(')');
1239 attribute->invalid = true;
1243 * parse one string literal argument.
1245 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1248 add_anchor_token('(');
1249 if (token.type != T_STRING_LITERAL) {
1250 parse_error_expected("while parsing attribute directive",
1251 T_STRING_LITERAL, NULL);
1254 *string = parse_string_literals();
1255 rem_anchor_token('(');
1259 attribute->invalid = true;
1263 * parse one tls model.
1265 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1267 static const char *const tls_models[] = {
1273 string_t string = { NULL, 0 };
1274 parse_gnu_attribute_string_arg(attribute, &string);
1275 if (string.begin != NULL) {
1276 for(size_t i = 0; i < 4; ++i) {
1277 if (strcmp(tls_models[i], string.begin) == 0) {
1278 attribute->u.value = i;
1282 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1284 attribute->invalid = true;
1288 * parse one tls model.
1290 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1292 static const char *const visibilities[] = {
1298 string_t string = { NULL, 0 };
1299 parse_gnu_attribute_string_arg(attribute, &string);
1300 if (string.begin != NULL) {
1301 for(size_t i = 0; i < 4; ++i) {
1302 if (strcmp(visibilities[i], string.begin) == 0) {
1303 attribute->u.value = i;
1307 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1309 attribute->invalid = true;
1313 * parse one (code) model.
1315 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1317 static const char *const visibilities[] = {
1322 string_t string = { NULL, 0 };
1323 parse_gnu_attribute_string_arg(attribute, &string);
1324 if (string.begin != NULL) {
1325 for(int i = 0; i < 3; ++i) {
1326 if (strcmp(visibilities[i], string.begin) == 0) {
1327 attribute->u.value = i;
1331 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1333 attribute->invalid = true;
1336 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1338 /* TODO: find out what is allowed here... */
1340 /* at least: byte, word, pointer, list of machine modes
1341 * __XXX___ is interpreted as XXX */
1342 add_anchor_token(')');
1344 if (token.type != T_IDENTIFIER) {
1345 expect(T_IDENTIFIER);
1348 /* This isn't really correct, the backend should provide a list of machine
1349 * specific modes (according to gcc philosophy that is...) */
1350 const char *symbol_str = token.v.symbol->string;
1351 if (strcmp_underscore("QI", symbol_str) == 0 ||
1352 strcmp_underscore("byte", symbol_str) == 0) {
1353 attribute->u.akind = ATOMIC_TYPE_CHAR;
1354 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1355 attribute->u.akind = ATOMIC_TYPE_SHORT;
1356 } else if (strcmp_underscore("SI", symbol_str) == 0
1357 || strcmp_underscore("word", symbol_str) == 0
1358 || strcmp_underscore("pointer", symbol_str) == 0) {
1359 attribute->u.akind = ATOMIC_TYPE_INT;
1360 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1361 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1363 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1364 attribute->invalid = true;
1368 rem_anchor_token(')');
1372 attribute->invalid = true;
1376 * parse one interrupt argument.
1378 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1380 static const char *const interrupts[] = {
1387 string_t string = { NULL, 0 };
1388 parse_gnu_attribute_string_arg(attribute, &string);
1389 if (string.begin != NULL) {
1390 for(size_t i = 0; i < 5; ++i) {
1391 if (strcmp(interrupts[i], string.begin) == 0) {
1392 attribute->u.value = i;
1396 errorf(HERE, "'%s' is not an interrupt", string.begin);
1398 attribute->invalid = true;
1402 * parse ( identifier, const expression, const expression )
1404 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1406 static const char *const format_names[] = {
1414 if (token.type != T_IDENTIFIER) {
1415 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1418 const char *name = token.v.symbol->string;
1419 for(i = 0; i < 4; ++i) {
1420 if (strcmp_underscore(format_names[i], name) == 0)
1424 if (warning.attribute)
1425 warningf(HERE, "'%s' is an unrecognized format function type", name);
1430 add_anchor_token(')');
1431 add_anchor_token(',');
1432 parse_constant_expression();
1433 rem_anchor_token(',');
1434 rem_anchor_token('(');
1437 add_anchor_token(')');
1438 parse_constant_expression();
1439 rem_anchor_token('(');
1443 attribute->u.value = true;
1446 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1448 if (!attribute->have_arguments)
1451 /* should have no arguments */
1452 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1453 eat_until_matching_token('(');
1454 /* we have already consumed '(', so we stop before ')', eat it */
1456 attribute->invalid = true;
1460 * Parse one GNU attribute.
1462 * Note that attribute names can be specified WITH or WITHOUT
1463 * double underscores, ie const or __const__.
1465 * The following attributes are parsed without arguments
1490 * no_instrument_function
1491 * warn_unused_result
1508 * externally_visible
1516 * The following attributes are parsed with arguments
1517 * aligned( const expression )
1518 * alias( string literal )
1519 * section( string literal )
1520 * format( identifier, const expression, const expression )
1521 * format_arg( const expression )
1522 * tls_model( string literal )
1523 * visibility( string literal )
1524 * regparm( const expression )
1525 * model( string leteral )
1526 * trap_exit( const expression )
1527 * sp_switch( string literal )
1529 * The following attributes might have arguments
1530 * weak_ref( string literal )
1531 * non_null( const expression // ',' )
1532 * interrupt( string literal )
1533 * sentinel( constant expression )
1535 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1537 gnu_attribute_t *head = *attributes;
1538 gnu_attribute_t *last = *attributes;
1539 decl_modifiers_t modifiers = 0;
1540 gnu_attribute_t *attribute;
1542 eat(T___attribute__);
1546 if (token.type != ')') {
1547 /* find the end of the list */
1549 while (last->next != NULL)
1553 /* non-empty attribute list */
1556 if (token.type == T_const) {
1558 } else if (token.type == T_volatile) {
1560 } else if (token.type == T_cdecl) {
1561 /* __attribute__((cdecl)), WITH ms mode */
1563 } else if (token.type == T_IDENTIFIER) {
1564 const symbol_t *sym = token.v.symbol;
1567 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1574 for(i = 0; i < GNU_AK_LAST; ++i) {
1575 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1578 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1581 if (kind == GNU_AK_LAST) {
1582 if (warning.attribute)
1583 warningf(HERE, "'%s' attribute directive ignored", name);
1585 /* skip possible arguments */
1586 if (token.type == '(') {
1587 eat_until_matching_token(')');
1590 /* check for arguments */
1591 attribute = allocate_gnu_attribute(kind);
1592 if (token.type == '(') {
1594 if (token.type == ')') {
1595 /* empty args are allowed */
1598 attribute->have_arguments = true;
1603 case GNU_AK_VOLATILE:
1604 case GNU_AK_DEPRECATED:
1609 case GNU_AK_NOCOMMON:
1611 case GNU_AK_NOTSHARED:
1612 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1613 case GNU_AK_WARN_UNUSED_RESULT:
1614 case GNU_AK_LONGCALL:
1615 case GNU_AK_SHORTCALL:
1616 case GNU_AK_LONG_CALL:
1617 case GNU_AK_SHORT_CALL:
1618 case GNU_AK_FUNCTION_VECTOR:
1619 case GNU_AK_INTERRUPT_HANDLER:
1620 case GNU_AK_NMI_HANDLER:
1621 case GNU_AK_NESTING:
1625 case GNU_AK_EIGTHBIT_DATA:
1626 case GNU_AK_TINY_DATA:
1627 case GNU_AK_SAVEALL:
1628 case GNU_AK_FLATTEN:
1629 case GNU_AK_SSEREGPARM:
1630 case GNU_AK_EXTERNALLY_VISIBLE:
1631 case GNU_AK_RETURN_TWICE:
1632 case GNU_AK_MAY_ALIAS:
1633 case GNU_AK_MS_STRUCT:
1634 case GNU_AK_GCC_STRUCT:
1637 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1638 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1639 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1640 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1641 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1642 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1643 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1644 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1645 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1646 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1647 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1648 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1649 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1650 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1651 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1652 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1654 case GNU_AK_ALIGNED:
1655 /* __align__ may be used without an argument */
1656 if (attribute->have_arguments) {
1657 parse_gnu_attribute_const_arg(attribute);
1661 case GNU_AK_FORMAT_ARG:
1662 case GNU_AK_REGPARM:
1663 case GNU_AK_TRAP_EXIT:
1664 if (!attribute->have_arguments) {
1665 /* should have arguments */
1666 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1667 attribute->invalid = true;
1669 parse_gnu_attribute_const_arg(attribute);
1672 case GNU_AK_SECTION:
1673 case GNU_AK_SP_SWITCH:
1674 if (!attribute->have_arguments) {
1675 /* should have arguments */
1676 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1677 attribute->invalid = true;
1679 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1682 if (!attribute->have_arguments) {
1683 /* should have arguments */
1684 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1685 attribute->invalid = true;
1687 parse_gnu_attribute_format_args(attribute);
1689 case GNU_AK_WEAKREF:
1690 /* may have one string argument */
1691 if (attribute->have_arguments)
1692 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1694 case GNU_AK_NONNULL:
1695 if (attribute->have_arguments)
1696 parse_gnu_attribute_const_arg_list(attribute);
1698 case GNU_AK_TLS_MODEL:
1699 if (!attribute->have_arguments) {
1700 /* should have arguments */
1701 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1703 parse_gnu_attribute_tls_model_arg(attribute);
1705 case GNU_AK_VISIBILITY:
1706 if (!attribute->have_arguments) {
1707 /* should have arguments */
1708 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1710 parse_gnu_attribute_visibility_arg(attribute);
1713 if (!attribute->have_arguments) {
1714 /* should have arguments */
1715 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1717 parse_gnu_attribute_model_arg(attribute);
1721 if (!attribute->have_arguments) {
1722 /* should have arguments */
1723 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1725 parse_gnu_attribute_mode_arg(attribute);
1728 case GNU_AK_INTERRUPT:
1729 /* may have one string argument */
1730 if (attribute->have_arguments)
1731 parse_gnu_attribute_interrupt_arg(attribute);
1733 case GNU_AK_SENTINEL:
1734 /* may have one string argument */
1735 if (attribute->have_arguments)
1736 parse_gnu_attribute_const_arg(attribute);
1739 /* already handled */
1743 check_no_argument(attribute, name);
1746 if (attribute != NULL) {
1748 last->next = attribute;
1751 head = last = attribute;
1755 if (token.type != ',')
1769 * Parse GNU attributes.
1771 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1773 decl_modifiers_t modifiers = 0;
1776 switch(token.type) {
1777 case T___attribute__:
1778 modifiers |= parse_gnu_attribute(attributes);
1784 if (token.type != T_STRING_LITERAL) {
1785 parse_error_expected("while parsing assembler attribute",
1786 T_STRING_LITERAL, NULL);
1787 eat_until_matching_token('(');
1790 parse_string_literals();
1795 case T_cdecl: modifiers |= DM_CDECL; break;
1796 case T__fastcall: modifiers |= DM_FASTCALL; break;
1797 case T__stdcall: modifiers |= DM_STDCALL; break;
1800 /* TODO record modifier */
1801 warningf(HERE, "Ignoring declaration modifier %K", &token);
1805 default: return modifiers;
1812 static designator_t *parse_designation(void)
1814 designator_t *result = NULL;
1815 designator_t *last = NULL;
1818 designator_t *designator;
1819 switch(token.type) {
1821 designator = allocate_ast_zero(sizeof(designator[0]));
1822 designator->source_position = token.source_position;
1824 add_anchor_token(']');
1825 designator->array_index = parse_constant_expression();
1826 rem_anchor_token(']');
1830 designator = allocate_ast_zero(sizeof(designator[0]));
1831 designator->source_position = token.source_position;
1833 if (token.type != T_IDENTIFIER) {
1834 parse_error_expected("while parsing designator",
1835 T_IDENTIFIER, NULL);
1838 designator->symbol = token.v.symbol;
1846 assert(designator != NULL);
1848 last->next = designator;
1850 result = designator;
1858 static initializer_t *initializer_from_string(array_type_t *type,
1859 const string_t *const string)
1861 /* TODO: check len vs. size of array type */
1864 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1865 initializer->string.string = *string;
1870 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1871 wide_string_t *const string)
1873 /* TODO: check len vs. size of array type */
1876 initializer_t *const initializer =
1877 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1878 initializer->wide_string.string = *string;
1884 * Build an initializer from a given expression.
1886 static initializer_t *initializer_from_expression(type_t *orig_type,
1887 expression_t *expression)
1889 /* TODO check that expression is a constant expression */
1891 /* § 6.7.8.14/15 char array may be initialized by string literals */
1892 type_t *type = skip_typeref(orig_type);
1893 type_t *expr_type_orig = expression->base.type;
1894 type_t *expr_type = skip_typeref(expr_type_orig);
1895 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1896 array_type_t *const array_type = &type->array;
1897 type_t *const element_type = skip_typeref(array_type->element_type);
1899 if (element_type->kind == TYPE_ATOMIC) {
1900 atomic_type_kind_t akind = element_type->atomic.akind;
1901 switch (expression->kind) {
1902 case EXPR_STRING_LITERAL:
1903 if (akind == ATOMIC_TYPE_CHAR
1904 || akind == ATOMIC_TYPE_SCHAR
1905 || akind == ATOMIC_TYPE_UCHAR) {
1906 return initializer_from_string(array_type,
1907 &expression->string.value);
1910 case EXPR_WIDE_STRING_LITERAL: {
1911 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1912 if (get_unqualified_type(element_type) == bare_wchar_type) {
1913 return initializer_from_wide_string(array_type,
1914 &expression->wide_string.value);
1924 assign_error_t error = semantic_assign(type, expression);
1925 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1927 report_assign_error(error, type, expression, "initializer",
1928 &expression->base.source_position);
1930 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1931 result->value.value = create_implicit_cast(expression, type);
1937 * Checks if a given expression can be used as an constant initializer.
1939 static bool is_initializer_constant(const expression_t *expression)
1941 return is_constant_expression(expression)
1942 || is_address_constant(expression);
1946 * Parses an scalar initializer.
1948 * § 6.7.8.11; eat {} without warning
1950 static initializer_t *parse_scalar_initializer(type_t *type,
1951 bool must_be_constant)
1953 /* there might be extra {} hierarchies */
1955 if (token.type == '{') {
1956 warningf(HERE, "extra curly braces around scalar initializer");
1960 } while (token.type == '{');
1963 expression_t *expression = parse_assignment_expression();
1964 if (must_be_constant && !is_initializer_constant(expression)) {
1965 errorf(&expression->base.source_position,
1966 "Initialisation expression '%E' is not constant\n",
1970 initializer_t *initializer = initializer_from_expression(type, expression);
1972 if (initializer == NULL) {
1973 errorf(&expression->base.source_position,
1974 "expression '%E' (type '%T') doesn't match expected type '%T'",
1975 expression, expression->base.type, type);
1980 bool additional_warning_displayed = false;
1981 while (braces > 0) {
1982 if (token.type == ',') {
1985 if (token.type != '}') {
1986 if (!additional_warning_displayed) {
1987 warningf(HERE, "additional elements in scalar initializer");
1988 additional_warning_displayed = true;
1999 * An entry in the type path.
2001 typedef struct type_path_entry_t type_path_entry_t;
2002 struct type_path_entry_t {
2003 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2005 size_t index; /**< For array types: the current index. */
2006 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2011 * A type path expression a position inside compound or array types.
2013 typedef struct type_path_t type_path_t;
2014 struct type_path_t {
2015 type_path_entry_t *path; /**< An flexible array containing the current path. */
2016 type_t *top_type; /**< type of the element the path points */
2017 size_t max_index; /**< largest index in outermost array */
2021 * Prints a type path for debugging.
2023 static __attribute__((unused)) void debug_print_type_path(
2024 const type_path_t *path)
2026 size_t len = ARR_LEN(path->path);
2028 for(size_t i = 0; i < len; ++i) {
2029 const type_path_entry_t *entry = & path->path[i];
2031 type_t *type = skip_typeref(entry->type);
2032 if (is_type_compound(type)) {
2033 /* in gcc mode structs can have no members */
2034 if (entry->v.compound_entry == NULL) {
2038 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2039 } else if (is_type_array(type)) {
2040 fprintf(stderr, "[%zu]", entry->v.index);
2042 fprintf(stderr, "-INVALID-");
2045 if (path->top_type != NULL) {
2046 fprintf(stderr, " (");
2047 print_type(path->top_type);
2048 fprintf(stderr, ")");
2053 * Return the top type path entry, ie. in a path
2054 * (type).a.b returns the b.
2056 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2058 size_t len = ARR_LEN(path->path);
2060 return &path->path[len-1];
2064 * Enlarge the type path by an (empty) element.
2066 static type_path_entry_t *append_to_type_path(type_path_t *path)
2068 size_t len = ARR_LEN(path->path);
2069 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2071 type_path_entry_t *result = & path->path[len];
2072 memset(result, 0, sizeof(result[0]));
2077 * Descending into a sub-type. Enter the scope of the current
2080 static void descend_into_subtype(type_path_t *path)
2082 type_t *orig_top_type = path->top_type;
2083 type_t *top_type = skip_typeref(orig_top_type);
2085 assert(is_type_compound(top_type) || is_type_array(top_type));
2087 type_path_entry_t *top = append_to_type_path(path);
2088 top->type = top_type;
2090 if (is_type_compound(top_type)) {
2091 declaration_t *declaration = top_type->compound.declaration;
2092 declaration_t *entry = declaration->scope.declarations;
2093 top->v.compound_entry = entry;
2095 if (entry != NULL) {
2096 path->top_type = entry->type;
2098 path->top_type = NULL;
2101 assert(is_type_array(top_type));
2104 path->top_type = top_type->array.element_type;
2109 * Pop an entry from the given type path, ie. returning from
2110 * (type).a.b to (type).a
2112 static void ascend_from_subtype(type_path_t *path)
2114 type_path_entry_t *top = get_type_path_top(path);
2116 path->top_type = top->type;
2118 size_t len = ARR_LEN(path->path);
2119 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2123 * Pop entries from the given type path until the given
2124 * path level is reached.
2126 static void ascend_to(type_path_t *path, size_t top_path_level)
2128 size_t len = ARR_LEN(path->path);
2130 while (len > top_path_level) {
2131 ascend_from_subtype(path);
2132 len = ARR_LEN(path->path);
2136 static bool walk_designator(type_path_t *path, const designator_t *designator,
2137 bool used_in_offsetof)
2139 for( ; designator != NULL; designator = designator->next) {
2140 type_path_entry_t *top = get_type_path_top(path);
2141 type_t *orig_type = top->type;
2143 type_t *type = skip_typeref(orig_type);
2145 if (designator->symbol != NULL) {
2146 symbol_t *symbol = designator->symbol;
2147 if (!is_type_compound(type)) {
2148 if (is_type_valid(type)) {
2149 errorf(&designator->source_position,
2150 "'.%Y' designator used for non-compound type '%T'",
2156 declaration_t *declaration = type->compound.declaration;
2157 declaration_t *iter = declaration->scope.declarations;
2158 for( ; iter != NULL; iter = iter->next) {
2159 if (iter->symbol == symbol) {
2164 errorf(&designator->source_position,
2165 "'%T' has no member named '%Y'", orig_type, symbol);
2168 if (used_in_offsetof) {
2169 type_t *real_type = skip_typeref(iter->type);
2170 if (real_type->kind == TYPE_BITFIELD) {
2171 errorf(&designator->source_position,
2172 "offsetof designator '%Y' may not specify bitfield",
2178 top->type = orig_type;
2179 top->v.compound_entry = iter;
2180 orig_type = iter->type;
2182 expression_t *array_index = designator->array_index;
2183 assert(designator->array_index != NULL);
2185 if (!is_type_array(type)) {
2186 if (is_type_valid(type)) {
2187 errorf(&designator->source_position,
2188 "[%E] designator used for non-array type '%T'",
2189 array_index, orig_type);
2193 if (!is_type_valid(array_index->base.type)) {
2197 long index = fold_constant(array_index);
2198 if (!used_in_offsetof) {
2200 errorf(&designator->source_position,
2201 "array index [%E] must be positive", array_index);
2204 if (type->array.size_constant == true) {
2205 long array_size = type->array.size;
2206 if (index >= array_size) {
2207 errorf(&designator->source_position,
2208 "designator [%E] (%d) exceeds array size %d",
2209 array_index, index, array_size);
2215 top->type = orig_type;
2216 top->v.index = (size_t) index;
2217 orig_type = type->array.element_type;
2219 path->top_type = orig_type;
2221 if (designator->next != NULL) {
2222 descend_into_subtype(path);
2231 static void advance_current_object(type_path_t *path, size_t top_path_level)
2233 type_path_entry_t *top = get_type_path_top(path);
2235 type_t *type = skip_typeref(top->type);
2236 if (is_type_union(type)) {
2237 /* in unions only the first element is initialized */
2238 top->v.compound_entry = NULL;
2239 } else if (is_type_struct(type)) {
2240 declaration_t *entry = top->v.compound_entry;
2242 entry = entry->next;
2243 top->v.compound_entry = entry;
2244 if (entry != NULL) {
2245 path->top_type = entry->type;
2249 assert(is_type_array(type));
2253 if (!type->array.size_constant || top->v.index < type->array.size) {
2258 /* we're past the last member of the current sub-aggregate, try if we
2259 * can ascend in the type hierarchy and continue with another subobject */
2260 size_t len = ARR_LEN(path->path);
2262 if (len > top_path_level) {
2263 ascend_from_subtype(path);
2264 advance_current_object(path, top_path_level);
2266 path->top_type = NULL;
2271 * skip until token is found.
2273 static void skip_until(int type)
2275 while (token.type != type) {
2276 if (token.type == T_EOF)
2283 * skip any {...} blocks until a closing bracket is reached.
2285 static void skip_initializers(void)
2287 if (token.type == '{')
2290 while (token.type != '}') {
2291 if (token.type == T_EOF)
2293 if (token.type == '{') {
2301 static initializer_t *create_empty_initializer(void)
2303 static initializer_t empty_initializer
2304 = { .list = { { INITIALIZER_LIST }, 0 } };
2305 return &empty_initializer;
2309 * Parse a part of an initialiser for a struct or union,
2311 static initializer_t *parse_sub_initializer(type_path_t *path,
2312 type_t *outer_type, size_t top_path_level,
2313 parse_initializer_env_t *env)
2315 if (token.type == '}') {
2316 /* empty initializer */
2317 return create_empty_initializer();
2320 type_t *orig_type = path->top_type;
2321 type_t *type = NULL;
2323 if (orig_type == NULL) {
2324 /* We are initializing an empty compound. */
2326 type = skip_typeref(orig_type);
2328 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2329 * initializers in this case. */
2330 if (!is_type_valid(type)) {
2331 skip_initializers();
2332 return create_empty_initializer();
2336 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2339 designator_t *designator = NULL;
2340 if (token.type == '.' || token.type == '[') {
2341 designator = parse_designation();
2343 /* reset path to toplevel, evaluate designator from there */
2344 ascend_to(path, top_path_level);
2345 if (!walk_designator(path, designator, false)) {
2346 /* can't continue after designation error */
2350 initializer_t *designator_initializer
2351 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2352 designator_initializer->designator.designator = designator;
2353 ARR_APP1(initializer_t*, initializers, designator_initializer);
2355 orig_type = path->top_type;
2356 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2361 if (token.type == '{') {
2362 if (type != NULL && is_type_scalar(type)) {
2363 sub = parse_scalar_initializer(type, env->must_be_constant);
2367 if (env->declaration != NULL) {
2368 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2369 env->declaration->symbol);
2371 errorf(HERE, "extra brace group at end of initializer");
2374 descend_into_subtype(path);
2376 add_anchor_token('}');
2377 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2379 rem_anchor_token('}');
2382 ascend_from_subtype(path);
2386 goto error_parse_next;
2390 /* must be an expression */
2391 expression_t *expression = parse_assignment_expression();
2393 if (env->must_be_constant && !is_initializer_constant(expression)) {
2394 errorf(&expression->base.source_position,
2395 "Initialisation expression '%E' is not constant\n",
2400 /* we are already outside, ... */
2404 /* handle { "string" } special case */
2405 if ((expression->kind == EXPR_STRING_LITERAL
2406 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2407 && outer_type != NULL) {
2408 sub = initializer_from_expression(outer_type, expression);
2410 if (token.type == ',') {
2413 if (token.type != '}') {
2414 warningf(HERE, "excessive elements in initializer for type '%T'",
2417 /* TODO: eat , ... */
2422 /* descend into subtypes until expression matches type */
2424 orig_type = path->top_type;
2425 type = skip_typeref(orig_type);
2427 sub = initializer_from_expression(orig_type, expression);
2431 if (!is_type_valid(type)) {
2434 if (is_type_scalar(type)) {
2435 errorf(&expression->base.source_position,
2436 "expression '%E' doesn't match expected type '%T'",
2437 expression, orig_type);
2441 descend_into_subtype(path);
2445 /* update largest index of top array */
2446 const type_path_entry_t *first = &path->path[0];
2447 type_t *first_type = first->type;
2448 first_type = skip_typeref(first_type);
2449 if (is_type_array(first_type)) {
2450 size_t index = first->v.index;
2451 if (index > path->max_index)
2452 path->max_index = index;
2456 /* append to initializers list */
2457 ARR_APP1(initializer_t*, initializers, sub);
2460 if (env->declaration != NULL)
2461 warningf(HERE, "excess elements in struct initializer for '%Y'",
2462 env->declaration->symbol);
2464 warningf(HERE, "excess elements in struct initializer");
2468 if (token.type == '}') {
2472 if (token.type == '}') {
2477 /* advance to the next declaration if we are not at the end */
2478 advance_current_object(path, top_path_level);
2479 orig_type = path->top_type;
2480 if (orig_type != NULL)
2481 type = skip_typeref(orig_type);
2487 size_t len = ARR_LEN(initializers);
2488 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2489 initializer_t *result = allocate_ast_zero(size);
2490 result->kind = INITIALIZER_LIST;
2491 result->list.len = len;
2492 memcpy(&result->list.initializers, initializers,
2493 len * sizeof(initializers[0]));
2495 DEL_ARR_F(initializers);
2496 ascend_to(path, top_path_level+1);
2501 skip_initializers();
2502 DEL_ARR_F(initializers);
2503 ascend_to(path, top_path_level+1);
2508 * Parses an initializer. Parsers either a compound literal
2509 * (env->declaration == NULL) or an initializer of a declaration.
2511 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2513 type_t *type = skip_typeref(env->type);
2514 initializer_t *result = NULL;
2517 if (is_type_scalar(type)) {
2518 result = parse_scalar_initializer(type, env->must_be_constant);
2519 } else if (token.type == '{') {
2523 memset(&path, 0, sizeof(path));
2524 path.top_type = env->type;
2525 path.path = NEW_ARR_F(type_path_entry_t, 0);
2527 descend_into_subtype(&path);
2529 add_anchor_token('}');
2530 result = parse_sub_initializer(&path, env->type, 1, env);
2531 rem_anchor_token('}');
2533 max_index = path.max_index;
2534 DEL_ARR_F(path.path);
2538 /* parse_scalar_initializer() also works in this case: we simply
2539 * have an expression without {} around it */
2540 result = parse_scalar_initializer(type, env->must_be_constant);
2543 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2544 * the array type size */
2545 if (is_type_array(type) && type->array.size_expression == NULL
2546 && result != NULL) {
2548 switch (result->kind) {
2549 case INITIALIZER_LIST:
2550 size = max_index + 1;
2553 case INITIALIZER_STRING:
2554 size = result->string.string.size;
2557 case INITIALIZER_WIDE_STRING:
2558 size = result->wide_string.string.size;
2561 case INITIALIZER_DESIGNATOR:
2562 case INITIALIZER_VALUE:
2563 /* can happen for parse errors */
2568 internal_errorf(HERE, "invalid initializer type");
2571 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2572 cnst->base.type = type_size_t;
2573 cnst->conste.v.int_value = size;
2575 type_t *new_type = duplicate_type(type);
2577 new_type->array.size_expression = cnst;
2578 new_type->array.size_constant = true;
2579 new_type->array.size = size;
2580 env->type = new_type;
2588 static declaration_t *append_declaration(declaration_t *declaration);
2590 static declaration_t *parse_compound_type_specifier(bool is_struct)
2592 gnu_attribute_t *attributes = NULL;
2593 decl_modifiers_t modifiers = 0;
2600 symbol_t *symbol = NULL;
2601 declaration_t *declaration = NULL;
2603 if (token.type == T___attribute__) {
2604 modifiers |= parse_attributes(&attributes);
2607 if (token.type == T_IDENTIFIER) {
2608 symbol = token.v.symbol;
2611 namespace_t const namespc =
2612 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2613 declaration = get_declaration(symbol, namespc);
2614 if (declaration != NULL) {
2615 if (declaration->parent_scope != scope &&
2616 (token.type == '{' || token.type == ';')) {
2618 } else if (declaration->init.complete &&
2619 token.type == '{') {
2620 assert(symbol != NULL);
2621 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2622 is_struct ? "struct" : "union", symbol,
2623 &declaration->source_position);
2624 declaration->scope.declarations = NULL;
2627 } else if (token.type != '{') {
2629 parse_error_expected("while parsing struct type specifier",
2630 T_IDENTIFIER, '{', NULL);
2632 parse_error_expected("while parsing union type specifier",
2633 T_IDENTIFIER, '{', NULL);
2639 if (declaration == NULL) {
2640 declaration = allocate_declaration_zero();
2641 declaration->namespc =
2642 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2643 declaration->source_position = token.source_position;
2644 declaration->symbol = symbol;
2645 declaration->parent_scope = scope;
2646 if (symbol != NULL) {
2647 environment_push(declaration);
2649 append_declaration(declaration);
2652 if (token.type == '{') {
2653 declaration->init.complete = true;
2655 parse_compound_type_entries(declaration);
2656 modifiers |= parse_attributes(&attributes);
2659 declaration->modifiers |= modifiers;
2663 static void parse_enum_entries(type_t *const enum_type)
2667 if (token.type == '}') {
2669 errorf(HERE, "empty enum not allowed");
2673 add_anchor_token('}');
2675 if (token.type != T_IDENTIFIER) {
2676 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2678 rem_anchor_token('}');
2682 declaration_t *const entry = allocate_declaration_zero();
2683 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2684 entry->type = enum_type;
2685 entry->symbol = token.v.symbol;
2686 entry->source_position = token.source_position;
2689 if (token.type == '=') {
2691 expression_t *value = parse_constant_expression();
2693 value = create_implicit_cast(value, enum_type);
2694 entry->init.enum_value = value;
2699 record_declaration(entry);
2701 if (token.type != ',')
2704 } while (token.type != '}');
2705 rem_anchor_token('}');
2713 static type_t *parse_enum_specifier(void)
2715 gnu_attribute_t *attributes = NULL;
2716 declaration_t *declaration;
2720 if (token.type == T_IDENTIFIER) {
2721 symbol = token.v.symbol;
2724 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2725 } else if (token.type != '{') {
2726 parse_error_expected("while parsing enum type specifier",
2727 T_IDENTIFIER, '{', NULL);
2734 if (declaration == NULL) {
2735 declaration = allocate_declaration_zero();
2736 declaration->namespc = NAMESPACE_ENUM;
2737 declaration->source_position = token.source_position;
2738 declaration->symbol = symbol;
2739 declaration->parent_scope = scope;
2742 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2743 type->enumt.declaration = declaration;
2745 if (token.type == '{') {
2746 if (declaration->init.complete) {
2747 errorf(HERE, "multiple definitions of enum %Y", symbol);
2749 if (symbol != NULL) {
2750 environment_push(declaration);
2752 append_declaration(declaration);
2753 declaration->init.complete = true;
2755 parse_enum_entries(type);
2756 parse_attributes(&attributes);
2763 * if a symbol is a typedef to another type, return true
2765 static bool is_typedef_symbol(symbol_t *symbol)
2767 const declaration_t *const declaration =
2768 get_declaration(symbol, NAMESPACE_NORMAL);
2770 declaration != NULL &&
2771 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2774 static type_t *parse_typeof(void)
2781 add_anchor_token(')');
2783 expression_t *expression = NULL;
2786 switch(token.type) {
2787 case T___extension__:
2788 /* This can be a prefix to a typename or an expression. We simply eat
2792 } while (token.type == T___extension__);
2796 if (is_typedef_symbol(token.v.symbol)) {
2797 type = parse_typename();
2799 expression = parse_expression();
2800 type = expression->base.type;
2805 type = parse_typename();
2809 expression = parse_expression();
2810 type = expression->base.type;
2814 rem_anchor_token(')');
2817 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2818 typeof_type->typeoft.expression = expression;
2819 typeof_type->typeoft.typeof_type = type;
2826 typedef enum specifiers_t {
2827 SPECIFIER_SIGNED = 1 << 0,
2828 SPECIFIER_UNSIGNED = 1 << 1,
2829 SPECIFIER_LONG = 1 << 2,
2830 SPECIFIER_INT = 1 << 3,
2831 SPECIFIER_DOUBLE = 1 << 4,
2832 SPECIFIER_CHAR = 1 << 5,
2833 SPECIFIER_SHORT = 1 << 6,
2834 SPECIFIER_LONG_LONG = 1 << 7,
2835 SPECIFIER_FLOAT = 1 << 8,
2836 SPECIFIER_BOOL = 1 << 9,
2837 SPECIFIER_VOID = 1 << 10,
2838 SPECIFIER_INT8 = 1 << 11,
2839 SPECIFIER_INT16 = 1 << 12,
2840 SPECIFIER_INT32 = 1 << 13,
2841 SPECIFIER_INT64 = 1 << 14,
2842 SPECIFIER_INT128 = 1 << 15,
2843 SPECIFIER_COMPLEX = 1 << 16,
2844 SPECIFIER_IMAGINARY = 1 << 17,
2847 static type_t *create_builtin_type(symbol_t *const symbol,
2848 type_t *const real_type)
2850 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2851 type->builtin.symbol = symbol;
2852 type->builtin.real_type = real_type;
2854 type_t *result = typehash_insert(type);
2855 if (type != result) {
2862 static type_t *get_typedef_type(symbol_t *symbol)
2864 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2865 if (declaration == NULL ||
2866 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2869 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2870 type->typedeft.declaration = declaration;
2876 * check for the allowed MS alignment values.
2878 static bool check_alignment_value(long long intvalue)
2880 if (intvalue < 1 || intvalue > 8192) {
2881 errorf(HERE, "illegal alignment value");
2884 unsigned v = (unsigned)intvalue;
2885 for(unsigned i = 1; i <= 8192; i += i) {
2889 errorf(HERE, "alignment must be power of two");
2893 #define DET_MOD(name, tag) do { \
2894 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2895 *modifiers |= tag; \
2898 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2900 decl_modifiers_t *modifiers = &specifiers->modifiers;
2903 if (token.type == T_restrict) {
2905 DET_MOD(restrict, DM_RESTRICT);
2907 } else if (token.type != T_IDENTIFIER)
2909 symbol_t *symbol = token.v.symbol;
2910 if (symbol == sym_align) {
2913 if (token.type != T_INTEGER)
2915 if (check_alignment_value(token.v.intvalue)) {
2916 if (specifiers->alignment != 0)
2917 warningf(HERE, "align used more than once");
2918 specifiers->alignment = (unsigned char)token.v.intvalue;
2922 } else if (symbol == sym_allocate) {
2925 if (token.type != T_IDENTIFIER)
2927 (void)token.v.symbol;
2929 } else if (symbol == sym_dllimport) {
2931 DET_MOD(dllimport, DM_DLLIMPORT);
2932 } else if (symbol == sym_dllexport) {
2934 DET_MOD(dllexport, DM_DLLEXPORT);
2935 } else if (symbol == sym_thread) {
2937 DET_MOD(thread, DM_THREAD);
2938 } else if (symbol == sym_naked) {
2940 DET_MOD(naked, DM_NAKED);
2941 } else if (symbol == sym_noinline) {
2943 DET_MOD(noinline, DM_NOINLINE);
2944 } else if (symbol == sym_noreturn) {
2946 DET_MOD(noreturn, DM_NORETURN);
2947 } else if (symbol == sym_nothrow) {
2949 DET_MOD(nothrow, DM_NOTHROW);
2950 } else if (symbol == sym_novtable) {
2952 DET_MOD(novtable, DM_NOVTABLE);
2953 } else if (symbol == sym_property) {
2957 bool is_get = false;
2958 if (token.type != T_IDENTIFIER)
2960 if (token.v.symbol == sym_get) {
2962 } else if (token.v.symbol == sym_put) {
2964 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2969 if (token.type != T_IDENTIFIER)
2972 if (specifiers->get_property_sym != NULL) {
2973 errorf(HERE, "get property name already specified");
2975 specifiers->get_property_sym = token.v.symbol;
2978 if (specifiers->put_property_sym != NULL) {
2979 errorf(HERE, "put property name already specified");
2981 specifiers->put_property_sym = token.v.symbol;
2985 if (token.type == ',') {
2992 } else if (symbol == sym_selectany) {
2994 DET_MOD(selectany, DM_SELECTANY);
2995 } else if (symbol == sym_uuid) {
2998 if (token.type != T_STRING_LITERAL)
3002 } else if (symbol == sym_deprecated) {
3004 if (specifiers->deprecated != 0)
3005 warningf(HERE, "deprecated used more than once");
3006 specifiers->deprecated = 1;
3007 if (token.type == '(') {
3009 if (token.type == T_STRING_LITERAL) {
3010 specifiers->deprecated_string = token.v.string.begin;
3013 errorf(HERE, "string literal expected");
3017 } else if (symbol == sym_noalias) {
3019 DET_MOD(noalias, DM_NOALIAS);
3021 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3023 if (token.type == '(')
3027 if (token.type == ',')
3034 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3036 type_t *type = NULL;
3037 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3038 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3039 unsigned type_specifiers = 0;
3042 specifiers->source_position = token.source_position;
3045 specifiers->modifiers
3046 |= parse_attributes(&specifiers->gnu_attributes);
3047 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3048 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3050 switch(token.type) {
3053 #define MATCH_STORAGE_CLASS(token, class) \
3055 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3056 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3058 specifiers->declared_storage_class = class; \
3062 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3063 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3064 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3065 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3066 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3071 add_anchor_token(')');
3072 parse_microsoft_extended_decl_modifier(specifiers);
3073 rem_anchor_token(')');
3078 switch (specifiers->declared_storage_class) {
3079 case STORAGE_CLASS_NONE:
3080 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3083 case STORAGE_CLASS_EXTERN:
3084 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3087 case STORAGE_CLASS_STATIC:
3088 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3092 errorf(HERE, "multiple storage classes in declaration specifiers");
3098 /* type qualifiers */
3099 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3101 qualifiers |= qualifier; \
3105 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3106 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3107 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3108 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3109 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3110 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3111 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3112 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3114 case T___extension__:
3119 /* type specifiers */
3120 #define MATCH_SPECIFIER(token, specifier, name) \
3123 if (type_specifiers & specifier) { \
3124 errorf(HERE, "multiple " name " type specifiers given"); \
3126 type_specifiers |= specifier; \
3130 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3131 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3132 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3133 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3134 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3135 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3136 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3137 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3138 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3139 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3140 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3141 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3142 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3143 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3144 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3145 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3147 case T__forceinline:
3148 /* only in microsoft mode */
3149 specifiers->modifiers |= DM_FORCEINLINE;
3153 specifiers->is_inline = true;
3158 if (type_specifiers & SPECIFIER_LONG_LONG) {
3159 errorf(HERE, "multiple type specifiers given");
3160 } else if (type_specifiers & SPECIFIER_LONG) {
3161 type_specifiers |= SPECIFIER_LONG_LONG;
3163 type_specifiers |= SPECIFIER_LONG;
3168 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3170 type->compound.declaration = parse_compound_type_specifier(true);
3174 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3175 type->compound.declaration = parse_compound_type_specifier(false);
3176 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3177 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3181 type = parse_enum_specifier();
3184 type = parse_typeof();
3186 case T___builtin_va_list:
3187 type = duplicate_type(type_valist);
3191 case T_IDENTIFIER: {
3192 /* only parse identifier if we haven't found a type yet */
3193 if (type != NULL || type_specifiers != 0)
3194 goto finish_specifiers;
3196 type_t *typedef_type = get_typedef_type(token.v.symbol);
3198 if (typedef_type == NULL)
3199 goto finish_specifiers;
3202 type = typedef_type;
3206 /* function specifier */
3208 goto finish_specifiers;
3215 atomic_type_kind_t atomic_type;
3217 /* match valid basic types */
3218 switch(type_specifiers) {
3219 case SPECIFIER_VOID:
3220 atomic_type = ATOMIC_TYPE_VOID;
3222 case SPECIFIER_CHAR:
3223 atomic_type = ATOMIC_TYPE_CHAR;
3225 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3226 atomic_type = ATOMIC_TYPE_SCHAR;
3228 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3229 atomic_type = ATOMIC_TYPE_UCHAR;
3231 case SPECIFIER_SHORT:
3232 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3233 case SPECIFIER_SHORT | SPECIFIER_INT:
3234 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3235 atomic_type = ATOMIC_TYPE_SHORT;
3237 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3238 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3239 atomic_type = ATOMIC_TYPE_USHORT;
3242 case SPECIFIER_SIGNED:
3243 case SPECIFIER_SIGNED | SPECIFIER_INT:
3244 atomic_type = ATOMIC_TYPE_INT;
3246 case SPECIFIER_UNSIGNED:
3247 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3248 atomic_type = ATOMIC_TYPE_UINT;
3250 case SPECIFIER_LONG:
3251 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3252 case SPECIFIER_LONG | SPECIFIER_INT:
3253 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3254 atomic_type = ATOMIC_TYPE_LONG;
3256 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3257 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3258 atomic_type = ATOMIC_TYPE_ULONG;
3260 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3261 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3262 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3263 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3265 atomic_type = ATOMIC_TYPE_LONGLONG;
3267 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3268 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3270 atomic_type = ATOMIC_TYPE_ULONGLONG;
3273 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3274 atomic_type = unsigned_int8_type_kind;
3277 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3278 atomic_type = unsigned_int16_type_kind;
3281 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3282 atomic_type = unsigned_int32_type_kind;
3285 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3286 atomic_type = unsigned_int64_type_kind;
3289 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3290 atomic_type = unsigned_int128_type_kind;
3293 case SPECIFIER_INT8:
3294 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3295 atomic_type = int8_type_kind;
3298 case SPECIFIER_INT16:
3299 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3300 atomic_type = int16_type_kind;
3303 case SPECIFIER_INT32:
3304 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3305 atomic_type = int32_type_kind;
3308 case SPECIFIER_INT64:
3309 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3310 atomic_type = int64_type_kind;
3313 case SPECIFIER_INT128:
3314 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3315 atomic_type = int128_type_kind;
3318 case SPECIFIER_FLOAT:
3319 atomic_type = ATOMIC_TYPE_FLOAT;
3321 case SPECIFIER_DOUBLE:
3322 atomic_type = ATOMIC_TYPE_DOUBLE;
3324 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3325 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3327 case SPECIFIER_BOOL:
3328 atomic_type = ATOMIC_TYPE_BOOL;
3330 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3331 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3332 atomic_type = ATOMIC_TYPE_FLOAT;
3334 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3335 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3336 atomic_type = ATOMIC_TYPE_DOUBLE;
3338 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3339 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3340 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3343 /* invalid specifier combination, give an error message */
3344 if (type_specifiers == 0) {
3345 if (! strict_mode) {
3346 if (warning.implicit_int) {
3347 warningf(HERE, "no type specifiers in declaration, using 'int'");
3349 atomic_type = ATOMIC_TYPE_INT;
3352 errorf(HERE, "no type specifiers given in declaration");
3354 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3355 (type_specifiers & SPECIFIER_UNSIGNED)) {
3356 errorf(HERE, "signed and unsigned specifiers gives");
3357 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3358 errorf(HERE, "only integer types can be signed or unsigned");
3360 errorf(HERE, "multiple datatypes in declaration");
3362 atomic_type = ATOMIC_TYPE_INVALID;
3365 if (type_specifiers & SPECIFIER_COMPLEX &&
3366 atomic_type != ATOMIC_TYPE_INVALID) {
3367 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3368 type->complex.akind = atomic_type;
3369 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3370 atomic_type != ATOMIC_TYPE_INVALID) {
3371 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3372 type->imaginary.akind = atomic_type;
3374 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3375 type->atomic.akind = atomic_type;
3379 if (type_specifiers != 0) {
3380 errorf(HERE, "multiple datatypes in declaration");
3384 /* FIXME: check type qualifiers here */
3386 type->base.qualifiers = qualifiers;
3387 type->base.modifiers = modifiers;
3389 type_t *result = typehash_insert(type);
3390 if (newtype && result != type) {
3394 specifiers->type = result;
3399 static type_qualifiers_t parse_type_qualifiers(void)
3401 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3404 switch(token.type) {
3405 /* type qualifiers */
3406 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3407 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3408 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3409 /* microsoft extended type modifiers */
3410 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3411 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3412 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3413 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3414 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3422 static declaration_t *parse_identifier_list(void)
3424 declaration_t *declarations = NULL;
3425 declaration_t *last_declaration = NULL;
3427 declaration_t *const declaration = allocate_declaration_zero();
3428 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3429 declaration->source_position = token.source_position;
3430 declaration->symbol = token.v.symbol;
3433 if (last_declaration != NULL) {
3434 last_declaration->next = declaration;
3436 declarations = declaration;
3438 last_declaration = declaration;
3440 if (token.type != ',') {
3444 } while (token.type == T_IDENTIFIER);
3446 return declarations;
3449 static type_t *automatic_type_conversion(type_t *orig_type);
3451 static void semantic_parameter(declaration_t *declaration)
3453 /* TODO: improve error messages */
3455 if (declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3456 errorf(HERE, "typedef not allowed in parameter list");
3457 } else if (declaration->declared_storage_class != STORAGE_CLASS_NONE
3458 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3459 errorf(HERE, "parameter may only have none or register storage class");
3462 type_t *const orig_type = declaration->type;
3463 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3464 * sugar. Turn it into a pointer.
3465 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3466 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3468 type_t *const type = automatic_type_conversion(orig_type);
3469 declaration->type = type;
3471 if (is_type_incomplete(skip_typeref(type))) {
3472 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3473 orig_type, declaration->symbol);
3477 static declaration_t *parse_parameter(void)
3479 declaration_specifiers_t specifiers;
3480 memset(&specifiers, 0, sizeof(specifiers));
3482 parse_declaration_specifiers(&specifiers);
3484 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3489 static declaration_t *parse_parameters(function_type_t *type)
3491 declaration_t *declarations = NULL;
3494 add_anchor_token(')');
3495 int saved_comma_state = save_and_reset_anchor_state(',');
3497 if (token.type == T_IDENTIFIER) {
3498 symbol_t *symbol = token.v.symbol;
3499 if (!is_typedef_symbol(symbol)) {
3500 type->kr_style_parameters = true;
3501 declarations = parse_identifier_list();
3502 goto parameters_finished;
3506 if (token.type == ')') {
3507 type->unspecified_parameters = 1;
3508 goto parameters_finished;
3511 declaration_t *declaration;
3512 declaration_t *last_declaration = NULL;
3513 function_parameter_t *parameter;
3514 function_parameter_t *last_parameter = NULL;
3517 switch(token.type) {
3521 goto parameters_finished;
3524 case T___extension__:
3526 declaration = parse_parameter();
3528 /* func(void) is not a parameter */
3529 if (last_parameter == NULL
3530 && token.type == ')'
3531 && declaration->symbol == NULL
3532 && skip_typeref(declaration->type) == type_void) {
3533 goto parameters_finished;
3535 semantic_parameter(declaration);
3537 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3538 memset(parameter, 0, sizeof(parameter[0]));
3539 parameter->type = declaration->type;
3541 if (last_parameter != NULL) {
3542 last_declaration->next = declaration;
3543 last_parameter->next = parameter;
3545 type->parameters = parameter;
3546 declarations = declaration;
3548 last_parameter = parameter;
3549 last_declaration = declaration;
3553 goto parameters_finished;
3555 if (token.type != ',') {
3556 goto parameters_finished;
3562 parameters_finished:
3563 rem_anchor_token(')');
3566 restore_anchor_state(',', saved_comma_state);
3567 return declarations;
3570 restore_anchor_state(',', saved_comma_state);
3574 typedef enum construct_type_kind_t {
3579 } construct_type_kind_t;
3581 typedef struct construct_type_t construct_type_t;
3582 struct construct_type_t {
3583 construct_type_kind_t kind;
3584 construct_type_t *next;
3587 typedef struct parsed_pointer_t parsed_pointer_t;
3588 struct parsed_pointer_t {
3589 construct_type_t construct_type;
3590 type_qualifiers_t type_qualifiers;
3593 typedef struct construct_function_type_t construct_function_type_t;
3594 struct construct_function_type_t {
3595 construct_type_t construct_type;
3596 type_t *function_type;
3599 typedef struct parsed_array_t parsed_array_t;
3600 struct parsed_array_t {
3601 construct_type_t construct_type;
3602 type_qualifiers_t type_qualifiers;
3608 typedef struct construct_base_type_t construct_base_type_t;
3609 struct construct_base_type_t {
3610 construct_type_t construct_type;
3614 static construct_type_t *parse_pointer_declarator(void)
3618 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3619 memset(pointer, 0, sizeof(pointer[0]));
3620 pointer->construct_type.kind = CONSTRUCT_POINTER;
3621 pointer->type_qualifiers = parse_type_qualifiers();
3623 return (construct_type_t*) pointer;
3626 static construct_type_t *parse_array_declarator(void)
3629 add_anchor_token(']');
3631 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3632 memset(array, 0, sizeof(array[0]));
3633 array->construct_type.kind = CONSTRUCT_ARRAY;
3635 if (token.type == T_static) {
3636 array->is_static = true;
3640 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3641 if (type_qualifiers != 0) {
3642 if (token.type == T_static) {
3643 array->is_static = true;
3647 array->type_qualifiers = type_qualifiers;
3649 if (token.type == '*' && look_ahead(1)->type == ']') {
3650 array->is_variable = true;
3652 } else if (token.type != ']') {
3653 array->size = parse_assignment_expression();
3656 rem_anchor_token(']');
3659 return (construct_type_t*) array;
3664 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3667 if (declaration != NULL) {
3668 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3670 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3672 if (mask & (mask-1)) {
3673 const char *first = NULL, *second = NULL;
3675 /* more than one calling convention set */
3676 if (declaration->modifiers & DM_CDECL) {
3677 if (first == NULL) first = "cdecl";
3678 else if (second == NULL) second = "cdecl";
3680 if (declaration->modifiers & DM_STDCALL) {
3681 if (first == NULL) first = "stdcall";
3682 else if (second == NULL) second = "stdcall";
3684 if (declaration->modifiers & DM_FASTCALL) {
3685 if (first == NULL) first = "faslcall";
3686 else if (second == NULL) second = "fastcall";
3688 if (declaration->modifiers & DM_THISCALL) {
3689 if (first == NULL) first = "thiscall";
3690 else if (second == NULL) second = "thiscall";
3692 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3695 if (declaration->modifiers & DM_CDECL)
3696 type->function.calling_convention = CC_CDECL;
3697 else if (declaration->modifiers & DM_STDCALL)
3698 type->function.calling_convention = CC_STDCALL;
3699 else if (declaration->modifiers & DM_FASTCALL)
3700 type->function.calling_convention = CC_FASTCALL;
3701 else if (declaration->modifiers & DM_THISCALL)
3702 type->function.calling_convention = CC_THISCALL;
3704 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3707 declaration_t *parameters = parse_parameters(&type->function);
3708 if (declaration != NULL) {
3709 declaration->scope.declarations = parameters;
3712 construct_function_type_t *construct_function_type =
3713 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3714 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3715 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3716 construct_function_type->function_type = type;
3718 return &construct_function_type->construct_type;
3721 static void fix_declaration_type(declaration_t *declaration)
3723 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3724 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3726 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3727 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3729 if (declaration->type->base.modifiers == type_modifiers)
3732 type_t *copy = duplicate_type(declaration->type);
3733 copy->base.modifiers = type_modifiers;
3735 type_t *result = typehash_insert(copy);
3736 if (result != copy) {
3737 obstack_free(type_obst, copy);
3740 declaration->type = result;
3743 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3744 bool may_be_abstract)
3746 /* construct a single linked list of construct_type_t's which describe
3747 * how to construct the final declarator type */
3748 construct_type_t *first = NULL;
3749 construct_type_t *last = NULL;
3750 gnu_attribute_t *attributes = NULL;
3752 decl_modifiers_t modifiers = parse_attributes(&attributes);
3755 while (token.type == '*') {
3756 construct_type_t *type = parse_pointer_declarator();
3766 /* TODO: find out if this is correct */
3767 modifiers |= parse_attributes(&attributes);
3770 construct_type_t *inner_types = NULL;
3772 switch(token.type) {
3774 if (declaration == NULL) {
3775 errorf(HERE, "no identifier expected in typename");
3777 declaration->symbol = token.v.symbol;
3778 declaration->source_position = token.source_position;
3784 add_anchor_token(')');
3785 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3786 rem_anchor_token(')');
3790 if (may_be_abstract)
3792 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3793 /* avoid a loop in the outermost scope, because eat_statement doesn't
3795 if (token.type == '}' && current_function == NULL) {
3803 construct_type_t *p = last;
3806 construct_type_t *type;
3807 switch(token.type) {
3809 type = parse_function_declarator(declaration);
3812 type = parse_array_declarator();
3815 goto declarator_finished;
3818 /* insert in the middle of the list (behind p) */
3820 type->next = p->next;
3831 declarator_finished:
3832 /* append inner_types at the end of the list, we don't to set last anymore
3833 * as it's not needed anymore */
3835 assert(first == NULL);
3836 first = inner_types;
3838 last->next = inner_types;
3846 static void parse_declaration_attributes(declaration_t *declaration)
3848 gnu_attribute_t *attributes = NULL;
3849 decl_modifiers_t modifiers = parse_attributes(&attributes);
3851 if (declaration == NULL)
3854 declaration->modifiers |= modifiers;
3855 /* check if we have these stupid mode attributes... */
3856 type_t *old_type = declaration->type;
3857 if (old_type == NULL)
3860 gnu_attribute_t *attribute = attributes;
3861 for ( ; attribute != NULL; attribute = attribute->next) {
3862 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3865 atomic_type_kind_t akind = attribute->u.akind;
3866 if (!is_type_signed(old_type)) {
3868 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3869 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3870 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3871 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3873 panic("invalid akind in mode attribute");
3877 = make_atomic_type(akind, old_type->base.qualifiers);
3881 static type_t *construct_declarator_type(construct_type_t *construct_list,
3884 construct_type_t *iter = construct_list;
3885 for( ; iter != NULL; iter = iter->next) {
3886 switch(iter->kind) {
3887 case CONSTRUCT_INVALID:
3888 internal_errorf(HERE, "invalid type construction found");
3889 case CONSTRUCT_FUNCTION: {
3890 construct_function_type_t *construct_function_type
3891 = (construct_function_type_t*) iter;
3893 type_t *function_type = construct_function_type->function_type;
3895 function_type->function.return_type = type;
3897 type_t *skipped_return_type = skip_typeref(type);
3898 if (is_type_function(skipped_return_type)) {
3899 errorf(HERE, "function returning function is not allowed");
3900 type = type_error_type;
3901 } else if (is_type_array(skipped_return_type)) {
3902 errorf(HERE, "function returning array is not allowed");
3903 type = type_error_type;
3905 type = function_type;
3910 case CONSTRUCT_POINTER: {
3911 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3912 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3913 pointer_type->pointer.points_to = type;
3914 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3916 type = pointer_type;
3920 case CONSTRUCT_ARRAY: {
3921 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3922 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3924 expression_t *size_expression = parsed_array->size;
3925 if (size_expression != NULL) {
3927 = create_implicit_cast(size_expression, type_size_t);
3930 array_type->base.qualifiers = parsed_array->type_qualifiers;
3931 array_type->array.element_type = type;
3932 array_type->array.is_static = parsed_array->is_static;
3933 array_type->array.is_variable = parsed_array->is_variable;
3934 array_type->array.size_expression = size_expression;
3936 if (size_expression != NULL) {
3937 if (is_constant_expression(size_expression)) {
3938 array_type->array.size_constant = true;
3939 array_type->array.size
3940 = fold_constant(size_expression);
3942 array_type->array.is_vla = true;
3946 type_t *skipped_type = skip_typeref(type);
3947 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3948 errorf(HERE, "array of void is not allowed");
3949 type = type_error_type;
3957 type_t *hashed_type = typehash_insert(type);
3958 if (hashed_type != type) {
3959 /* the function type was constructed earlier freeing it here will
3960 * destroy other types... */
3961 if (iter->kind != CONSTRUCT_FUNCTION) {
3971 static declaration_t *parse_declarator(
3972 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3974 declaration_t *const declaration = allocate_declaration_zero();
3975 declaration->declared_storage_class = specifiers->declared_storage_class;
3976 declaration->modifiers = specifiers->modifiers;
3977 declaration->deprecated = specifiers->deprecated;
3978 declaration->deprecated_string = specifiers->deprecated_string;
3979 declaration->get_property_sym = specifiers->get_property_sym;
3980 declaration->put_property_sym = specifiers->put_property_sym;
3981 declaration->is_inline = specifiers->is_inline;
3983 declaration->storage_class = specifiers->declared_storage_class;
3984 if (declaration->storage_class == STORAGE_CLASS_NONE
3985 && scope != global_scope) {
3986 declaration->storage_class = STORAGE_CLASS_AUTO;
3989 if (specifiers->alignment != 0) {
3990 /* TODO: add checks here */
3991 declaration->alignment = specifiers->alignment;
3994 construct_type_t *construct_type
3995 = parse_inner_declarator(declaration, may_be_abstract);
3996 type_t *const type = specifiers->type;
3997 declaration->type = construct_declarator_type(construct_type, type);
3999 parse_declaration_attributes(declaration);
4001 fix_declaration_type(declaration);
4003 if (construct_type != NULL) {
4004 obstack_free(&temp_obst, construct_type);
4010 static type_t *parse_abstract_declarator(type_t *base_type)
4012 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4014 type_t *result = construct_declarator_type(construct_type, base_type);
4015 if (construct_type != NULL) {
4016 obstack_free(&temp_obst, construct_type);
4022 static declaration_t *append_declaration(declaration_t* const declaration)
4024 if (last_declaration != NULL) {
4025 last_declaration->next = declaration;
4027 scope->declarations = declaration;
4029 last_declaration = declaration;
4034 * Check if the declaration of main is suspicious. main should be a
4035 * function with external linkage, returning int, taking either zero
4036 * arguments, two, or three arguments of appropriate types, ie.
4038 * int main([ int argc, char **argv [, char **env ] ]).
4040 * @param decl the declaration to check
4041 * @param type the function type of the declaration
4043 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4045 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4046 warningf(&decl->source_position,
4047 "'main' is normally a non-static function");
4049 if (skip_typeref(func_type->return_type) != type_int) {
4050 warningf(&decl->source_position,
4051 "return type of 'main' should be 'int', but is '%T'",
4052 func_type->return_type);
4054 const function_parameter_t *parm = func_type->parameters;
4056 type_t *const first_type = parm->type;
4057 if (!types_compatible(skip_typeref(first_type), type_int)) {
4058 warningf(&decl->source_position,
4059 "first argument of 'main' should be 'int', but is '%T'", first_type);
4063 type_t *const second_type = parm->type;
4064 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4065 warningf(&decl->source_position,
4066 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4070 type_t *const third_type = parm->type;
4071 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4072 warningf(&decl->source_position,
4073 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4077 goto warn_arg_count;
4081 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4087 * Check if a symbol is the equal to "main".
4089 static bool is_sym_main(const symbol_t *const sym)
4091 return strcmp(sym->string, "main") == 0;
4094 static declaration_t *internal_record_declaration(
4095 declaration_t *const declaration,
4096 const bool is_definition)
4098 const symbol_t *const symbol = declaration->symbol;
4099 const namespace_t namespc = (namespace_t)declaration->namespc;
4101 assert(declaration->symbol != NULL);
4102 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4104 type_t *const orig_type = declaration->type;
4105 type_t *const type = skip_typeref(orig_type);
4106 if (is_type_function(type) &&
4107 type->function.unspecified_parameters &&
4108 warning.strict_prototypes &&
4109 previous_declaration == NULL) {
4110 warningf(&declaration->source_position,
4111 "function declaration '%#T' is not a prototype",
4112 orig_type, declaration->symbol);
4115 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4116 check_type_of_main(declaration, &type->function);
4119 assert(declaration != previous_declaration);
4120 if (previous_declaration != NULL
4121 && previous_declaration->parent_scope == scope) {
4122 /* can happen for K&R style declarations */
4123 if (previous_declaration->type == NULL) {
4124 previous_declaration->type = declaration->type;
4127 const type_t *prev_type = skip_typeref(previous_declaration->type);
4128 if (!types_compatible(type, prev_type)) {
4129 errorf(&declaration->source_position,
4130 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4131 orig_type, symbol, previous_declaration->type, symbol,
4132 &previous_declaration->source_position);
4134 unsigned old_storage_class = previous_declaration->storage_class;
4135 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4136 errorf(&declaration->source_position,
4137 "redeclaration of enum entry '%Y' (declared %P)",
4138 symbol, &previous_declaration->source_position);
4139 return previous_declaration;
4142 if (warning.redundant_decls &&
4144 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4145 !(previous_declaration->modifiers & DM_USED) &&
4146 !previous_declaration->used) {
4147 warningf(&previous_declaration->source_position,
4148 "unnecessary static forward declaration for '%#T'",
4149 previous_declaration->type, symbol);
4152 unsigned new_storage_class = declaration->storage_class;
4154 if (is_type_incomplete(prev_type)) {
4155 previous_declaration->type = type;
4159 /* pretend no storage class means extern for function
4160 * declarations (except if the previous declaration is neither
4161 * none nor extern) */
4162 if (is_type_function(type)) {
4163 if (prev_type->function.unspecified_parameters) {
4164 previous_declaration->type = type;
4168 switch (old_storage_class) {
4169 case STORAGE_CLASS_NONE:
4170 old_storage_class = STORAGE_CLASS_EXTERN;
4173 case STORAGE_CLASS_EXTERN:
4174 if (is_definition) {
4175 if (warning.missing_prototypes &&
4176 prev_type->function.unspecified_parameters &&
4177 !is_sym_main(symbol)) {
4178 warningf(&declaration->source_position,
4179 "no previous prototype for '%#T'",
4182 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4183 new_storage_class = STORAGE_CLASS_EXTERN;
4192 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4193 new_storage_class == STORAGE_CLASS_EXTERN) {
4194 warn_redundant_declaration:
4195 if (!is_definition &&
4196 warning.redundant_decls &&
4197 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4198 warningf(&declaration->source_position,
4199 "redundant declaration for '%Y' (declared %P)",
4200 symbol, &previous_declaration->source_position);
4202 } else if (current_function == NULL) {
4203 if (old_storage_class != STORAGE_CLASS_STATIC &&
4204 new_storage_class == STORAGE_CLASS_STATIC) {
4205 errorf(&declaration->source_position,
4206 "static declaration of '%Y' follows non-static declaration (declared %P)",
4207 symbol, &previous_declaration->source_position);
4208 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4209 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4210 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4212 goto warn_redundant_declaration;
4214 } else if (old_storage_class == new_storage_class) {
4215 errorf(&declaration->source_position,
4216 "redeclaration of '%Y' (declared %P)",
4217 symbol, &previous_declaration->source_position);
4219 errorf(&declaration->source_position,
4220 "redeclaration of '%Y' with different linkage (declared %P)",
4221 symbol, &previous_declaration->source_position);
4225 if (declaration->is_inline)
4226 previous_declaration->is_inline = true;
4227 return previous_declaration;
4228 } else if (is_type_function(type)) {
4229 if (is_definition &&
4230 declaration->storage_class != STORAGE_CLASS_STATIC) {
4231 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4232 warningf(&declaration->source_position,
4233 "no previous prototype for '%#T'", orig_type, symbol);
4234 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4235 warningf(&declaration->source_position,
4236 "no previous declaration for '%#T'", orig_type,
4241 if (warning.missing_declarations &&
4242 scope == global_scope && (
4243 declaration->storage_class == STORAGE_CLASS_NONE ||
4244 declaration->storage_class == STORAGE_CLASS_THREAD
4246 warningf(&declaration->source_position,
4247 "no previous declaration for '%#T'", orig_type, symbol);
4251 assert(declaration->parent_scope == NULL);
4252 assert(scope != NULL);
4254 declaration->parent_scope = scope;
4256 environment_push(declaration);
4257 return append_declaration(declaration);
4260 static declaration_t *record_declaration(declaration_t *declaration)
4262 return internal_record_declaration(declaration, false);
4265 static declaration_t *record_definition(declaration_t *declaration)
4267 return internal_record_declaration(declaration, true);
4270 static void parser_error_multiple_definition(declaration_t *declaration,
4271 const source_position_t *source_position)
4273 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4274 declaration->symbol, &declaration->source_position);
4277 static bool is_declaration_specifier(const token_t *token,
4278 bool only_specifiers_qualifiers)
4280 switch(token->type) {
4285 return is_typedef_symbol(token->v.symbol);
4287 case T___extension__:
4289 return !only_specifiers_qualifiers;
4296 static void parse_init_declarator_rest(declaration_t *declaration)
4300 type_t *orig_type = declaration->type;
4301 type_t *type = skip_typeref(orig_type);
4303 if (declaration->init.initializer != NULL) {
4304 parser_error_multiple_definition(declaration, HERE);
4307 bool must_be_constant = false;
4308 if (declaration->storage_class == STORAGE_CLASS_STATIC
4309 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4310 || declaration->parent_scope == global_scope) {
4311 must_be_constant = true;
4314 parse_initializer_env_t env;
4315 env.type = orig_type;
4316 env.must_be_constant = must_be_constant;
4317 env.declaration = declaration;
4319 initializer_t *initializer = parse_initializer(&env);
4321 if (env.type != orig_type) {
4322 orig_type = env.type;
4323 type = skip_typeref(orig_type);
4324 declaration->type = env.type;
4327 if (is_type_function(type)) {
4328 errorf(&declaration->source_position,
4329 "initializers not allowed for function types at declator '%Y' (type '%T')",
4330 declaration->symbol, orig_type);
4332 declaration->init.initializer = initializer;
4336 /* parse rest of a declaration without any declarator */
4337 static void parse_anonymous_declaration_rest(
4338 const declaration_specifiers_t *specifiers,
4339 parsed_declaration_func finished_declaration)
4343 declaration_t *const declaration = allocate_declaration_zero();
4344 declaration->type = specifiers->type;
4345 declaration->declared_storage_class = specifiers->declared_storage_class;
4346 declaration->source_position = specifiers->source_position;
4347 declaration->modifiers = specifiers->modifiers;
4349 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4350 warningf(&declaration->source_position,
4351 "useless storage class in empty declaration");
4353 declaration->storage_class = STORAGE_CLASS_NONE;
4355 type_t *type = declaration->type;
4356 switch (type->kind) {
4357 case TYPE_COMPOUND_STRUCT:
4358 case TYPE_COMPOUND_UNION: {
4359 if (type->compound.declaration->symbol == NULL) {
4360 warningf(&declaration->source_position,
4361 "unnamed struct/union that defines no instances");
4370 warningf(&declaration->source_position, "empty declaration");
4374 finished_declaration(declaration);
4377 static void parse_declaration_rest(declaration_t *ndeclaration,
4378 const declaration_specifiers_t *specifiers,
4379 parsed_declaration_func finished_declaration)
4381 add_anchor_token(';');
4382 add_anchor_token('=');
4383 add_anchor_token(',');
4385 declaration_t *declaration = finished_declaration(ndeclaration);
4387 type_t *orig_type = declaration->type;
4388 type_t *type = skip_typeref(orig_type);
4390 if (type->kind != TYPE_FUNCTION &&
4391 declaration->is_inline &&
4392 is_type_valid(type)) {
4393 warningf(&declaration->source_position,
4394 "variable '%Y' declared 'inline'\n", declaration->symbol);
4397 if (token.type == '=') {
4398 parse_init_declarator_rest(declaration);
4401 if (token.type != ',')
4405 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4410 rem_anchor_token(';');
4411 rem_anchor_token('=');
4412 rem_anchor_token(',');
4415 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4417 symbol_t *symbol = declaration->symbol;
4418 if (symbol == NULL) {
4419 errorf(HERE, "anonymous declaration not valid as function parameter");
4422 namespace_t namespc = (namespace_t) declaration->namespc;
4423 if (namespc != NAMESPACE_NORMAL) {
4424 return record_declaration(declaration);
4427 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4428 if (previous_declaration == NULL ||
4429 previous_declaration->parent_scope != scope) {
4430 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4435 if (previous_declaration->type == NULL) {
4436 previous_declaration->type = declaration->type;
4437 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4438 previous_declaration->storage_class = declaration->storage_class;
4439 previous_declaration->parent_scope = scope;
4440 return previous_declaration;
4442 return record_declaration(declaration);
4446 static void parse_declaration(parsed_declaration_func finished_declaration)
4448 declaration_specifiers_t specifiers;
4449 memset(&specifiers, 0, sizeof(specifiers));
4450 parse_declaration_specifiers(&specifiers);
4452 if (token.type == ';') {
4453 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4455 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4456 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4460 static type_t *get_default_promoted_type(type_t *orig_type)
4462 type_t *result = orig_type;
4464 type_t *type = skip_typeref(orig_type);
4465 if (is_type_integer(type)) {
4466 result = promote_integer(type);
4467 } else if (type == type_float) {
4468 result = type_double;
4474 static void parse_kr_declaration_list(declaration_t *declaration)
4476 type_t *type = skip_typeref(declaration->type);
4477 if (!is_type_function(type))
4480 if (!type->function.kr_style_parameters)
4483 /* push function parameters */
4484 int top = environment_top();
4485 scope_t *last_scope = scope;
4486 set_scope(&declaration->scope);
4488 declaration_t *parameter = declaration->scope.declarations;
4489 for ( ; parameter != NULL; parameter = parameter->next) {
4490 assert(parameter->parent_scope == NULL);
4491 parameter->parent_scope = scope;
4492 environment_push(parameter);
4495 /* parse declaration list */
4496 while (is_declaration_specifier(&token, false)) {
4497 parse_declaration(finished_kr_declaration);
4500 /* pop function parameters */
4501 assert(scope == &declaration->scope);
4502 set_scope(last_scope);
4503 environment_pop_to(top);
4505 /* update function type */
4506 type_t *new_type = duplicate_type(type);
4508 function_parameter_t *parameters = NULL;
4509 function_parameter_t *last_parameter = NULL;
4511 declaration_t *parameter_declaration = declaration->scope.declarations;
4512 for( ; parameter_declaration != NULL;
4513 parameter_declaration = parameter_declaration->next) {
4514 type_t *parameter_type = parameter_declaration->type;
4515 if (parameter_type == NULL) {
4517 errorf(HERE, "no type specified for function parameter '%Y'",
4518 parameter_declaration->symbol);
4520 if (warning.implicit_int) {
4521 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4522 parameter_declaration->symbol);
4524 parameter_type = type_int;
4525 parameter_declaration->type = parameter_type;
4529 semantic_parameter(parameter_declaration);
4530 parameter_type = parameter_declaration->type;
4533 * we need the default promoted types for the function type
4535 parameter_type = get_default_promoted_type(parameter_type);
4537 function_parameter_t *function_parameter
4538 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4539 memset(function_parameter, 0, sizeof(function_parameter[0]));
4541 function_parameter->type = parameter_type;
4542 if (last_parameter != NULL) {
4543 last_parameter->next = function_parameter;
4545 parameters = function_parameter;
4547 last_parameter = function_parameter;
4550 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4552 new_type->function.parameters = parameters;
4553 new_type->function.unspecified_parameters = true;
4555 type = typehash_insert(new_type);
4556 if (type != new_type) {
4557 obstack_free(type_obst, new_type);
4560 declaration->type = type;
4563 static bool first_err = true;
4566 * When called with first_err set, prints the name of the current function,
4569 static void print_in_function(void)
4573 diagnosticf("%s: In function '%Y':\n",
4574 current_function->source_position.input_name,
4575 current_function->symbol);
4580 * Check if all labels are defined in the current function.
4581 * Check if all labels are used in the current function.
4583 static void check_labels(void)
4585 for (const goto_statement_t *goto_statement = goto_first;
4586 goto_statement != NULL;
4587 goto_statement = goto_statement->next) {
4588 declaration_t *label = goto_statement->label;
4591 if (label->source_position.input_name == NULL) {
4592 print_in_function();
4593 errorf(&goto_statement->base.source_position,
4594 "label '%Y' used but not defined", label->symbol);
4597 goto_first = goto_last = NULL;
4599 if (warning.unused_label) {
4600 for (const label_statement_t *label_statement = label_first;
4601 label_statement != NULL;
4602 label_statement = label_statement->next) {
4603 const declaration_t *label = label_statement->label;
4605 if (! label->used) {
4606 print_in_function();
4607 warningf(&label_statement->base.source_position,
4608 "label '%Y' defined but not used", label->symbol);
4612 label_first = label_last = NULL;
4616 * Check declarations of current_function for unused entities.
4618 static void check_declarations(void)
4620 if (warning.unused_parameter) {
4621 const scope_t *scope = ¤t_function->scope;
4623 const declaration_t *parameter = scope->declarations;
4624 for (; parameter != NULL; parameter = parameter->next) {
4625 if (! parameter->used) {
4626 print_in_function();
4627 warningf(¶meter->source_position,
4628 "unused parameter '%Y'", parameter->symbol);
4632 if (warning.unused_variable) {
4636 static void parse_external_declaration(void)
4638 /* function-definitions and declarations both start with declaration
4640 declaration_specifiers_t specifiers;
4641 memset(&specifiers, 0, sizeof(specifiers));
4643 add_anchor_token(';');
4644 parse_declaration_specifiers(&specifiers);
4645 rem_anchor_token(';');
4647 /* must be a declaration */
4648 if (token.type == ';') {
4649 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4653 add_anchor_token(',');
4654 add_anchor_token('=');
4655 rem_anchor_token(';');
4657 /* declarator is common to both function-definitions and declarations */
4658 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4660 rem_anchor_token(',');
4661 rem_anchor_token('=');
4662 rem_anchor_token(';');
4664 /* must be a declaration */
4665 switch (token.type) {
4668 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4672 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
4676 /* must be a function definition */
4677 parse_kr_declaration_list(ndeclaration);
4679 if (token.type != '{') {
4680 parse_error_expected("while parsing function definition", '{', NULL);
4681 eat_until_matching_token(';');
4685 type_t *type = ndeclaration->type;
4687 /* note that we don't skip typerefs: the standard doesn't allow them here
4688 * (so we can't use is_type_function here) */
4689 if (type->kind != TYPE_FUNCTION) {
4690 if (is_type_valid(type)) {
4691 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4692 type, ndeclaration->symbol);
4698 /* § 6.7.5.3 (14) a function definition with () means no
4699 * parameters (and not unspecified parameters) */
4700 if (type->function.unspecified_parameters
4701 && type->function.parameters == NULL
4702 && !type->function.kr_style_parameters) {
4703 type_t *duplicate = duplicate_type(type);
4704 duplicate->function.unspecified_parameters = false;
4706 type = typehash_insert(duplicate);
4707 if (type != duplicate) {
4708 obstack_free(type_obst, duplicate);
4710 ndeclaration->type = type;
4713 declaration_t *const declaration = record_definition(ndeclaration);
4714 if (ndeclaration != declaration) {
4715 declaration->scope = ndeclaration->scope;
4717 type = skip_typeref(declaration->type);
4719 /* push function parameters and switch scope */
4720 int top = environment_top();
4721 scope_t *last_scope = scope;
4722 set_scope(&declaration->scope);
4724 declaration_t *parameter = declaration->scope.declarations;
4725 for( ; parameter != NULL; parameter = parameter->next) {
4726 if (parameter->parent_scope == &ndeclaration->scope) {
4727 parameter->parent_scope = scope;
4729 assert(parameter->parent_scope == NULL
4730 || parameter->parent_scope == scope);
4731 parameter->parent_scope = scope;
4732 if (parameter->symbol == NULL) {
4733 errorf(&ndeclaration->source_position, "parameter name omitted");
4736 environment_push(parameter);
4739 if (declaration->init.statement != NULL) {
4740 parser_error_multiple_definition(declaration, HERE);
4743 /* parse function body */
4744 int label_stack_top = label_top();
4745 declaration_t *old_current_function = current_function;
4746 current_function = declaration;
4748 declaration->init.statement = parse_compound_statement(false);
4751 check_declarations();
4753 assert(current_function == declaration);
4754 current_function = old_current_function;
4755 label_pop_to(label_stack_top);
4758 assert(scope == &declaration->scope);
4759 set_scope(last_scope);
4760 environment_pop_to(top);
4763 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4764 source_position_t *source_position)
4766 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4768 type->bitfield.base_type = base_type;
4769 type->bitfield.size = size;
4774 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4777 declaration_t *iter = compound_declaration->scope.declarations;
4778 for( ; iter != NULL; iter = iter->next) {
4779 if (iter->namespc != NAMESPACE_NORMAL)
4782 if (iter->symbol == NULL) {
4783 type_t *type = skip_typeref(iter->type);
4784 if (is_type_compound(type)) {
4785 declaration_t *result
4786 = find_compound_entry(type->compound.declaration, symbol);
4793 if (iter->symbol == symbol) {
4801 static void parse_compound_declarators(declaration_t *struct_declaration,
4802 const declaration_specifiers_t *specifiers)
4804 declaration_t *last_declaration = struct_declaration->scope.declarations;
4805 if (last_declaration != NULL) {
4806 while(last_declaration->next != NULL) {
4807 last_declaration = last_declaration->next;
4812 declaration_t *declaration;
4814 if (token.type == ':') {
4815 source_position_t source_position = *HERE;
4818 type_t *base_type = specifiers->type;
4819 expression_t *size = parse_constant_expression();
4821 if (!is_type_integer(skip_typeref(base_type))) {
4822 errorf(HERE, "bitfield base type '%T' is not an integer type",
4826 type_t *type = make_bitfield_type(base_type, size, &source_position);
4828 declaration = allocate_declaration_zero();
4829 declaration->namespc = NAMESPACE_NORMAL;
4830 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4831 declaration->storage_class = STORAGE_CLASS_NONE;
4832 declaration->source_position = source_position;
4833 declaration->modifiers = specifiers->modifiers;
4834 declaration->type = type;
4836 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4838 type_t *orig_type = declaration->type;
4839 type_t *type = skip_typeref(orig_type);
4841 if (token.type == ':') {
4842 source_position_t source_position = *HERE;
4844 expression_t *size = parse_constant_expression();
4846 if (!is_type_integer(type)) {
4847 errorf(HERE, "bitfield base type '%T' is not an "
4848 "integer type", orig_type);
4851 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4852 declaration->type = bitfield_type;
4854 /* TODO we ignore arrays for now... what is missing is a check
4855 * that they're at the end of the struct */
4856 if (is_type_incomplete(type) && !is_type_array(type)) {
4858 "compound member '%Y' has incomplete type '%T'",
4859 declaration->symbol, orig_type);
4860 } else if (is_type_function(type)) {
4861 errorf(HERE, "compound member '%Y' must not have function "
4862 "type '%T'", declaration->symbol, orig_type);
4867 /* make sure we don't define a symbol multiple times */
4868 symbol_t *symbol = declaration->symbol;
4869 if (symbol != NULL) {
4870 declaration_t *prev_decl
4871 = find_compound_entry(struct_declaration, symbol);
4873 if (prev_decl != NULL) {
4874 assert(prev_decl->symbol == symbol);
4875 errorf(&declaration->source_position,
4876 "multiple declarations of symbol '%Y' (declared %P)",
4877 symbol, &prev_decl->source_position);
4881 /* append declaration */
4882 if (last_declaration != NULL) {
4883 last_declaration->next = declaration;
4885 struct_declaration->scope.declarations = declaration;
4887 last_declaration = declaration;
4889 if (token.type != ',')
4899 static void parse_compound_type_entries(declaration_t *compound_declaration)
4902 add_anchor_token('}');
4904 while(token.type != '}' && token.type != T_EOF) {
4905 declaration_specifiers_t specifiers;
4906 memset(&specifiers, 0, sizeof(specifiers));
4907 parse_declaration_specifiers(&specifiers);
4909 parse_compound_declarators(compound_declaration, &specifiers);
4911 rem_anchor_token('}');
4913 if (token.type == T_EOF) {
4914 errorf(HERE, "EOF while parsing struct");
4919 static type_t *parse_typename(void)
4921 declaration_specifiers_t specifiers;
4922 memset(&specifiers, 0, sizeof(specifiers));
4923 parse_declaration_specifiers(&specifiers);
4924 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4925 /* TODO: improve error message, user does probably not know what a
4926 * storage class is...
4928 errorf(HERE, "typename may not have a storage class");
4931 type_t *result = parse_abstract_declarator(specifiers.type);
4939 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4940 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4941 expression_t *left);
4943 typedef struct expression_parser_function_t expression_parser_function_t;
4944 struct expression_parser_function_t {
4945 unsigned precedence;
4946 parse_expression_function parser;
4947 unsigned infix_precedence;
4948 parse_expression_infix_function infix_parser;
4951 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4954 * Prints an error message if an expression was expected but not read
4956 static expression_t *expected_expression_error(void)
4958 /* skip the error message if the error token was read */
4959 if (token.type != T_ERROR) {
4960 errorf(HERE, "expected expression, got token '%K'", &token);
4964 return create_invalid_expression();
4968 * Parse a string constant.
4970 static expression_t *parse_string_const(void)
4973 if (token.type == T_STRING_LITERAL) {
4974 string_t res = token.v.string;
4976 while (token.type == T_STRING_LITERAL) {
4977 res = concat_strings(&res, &token.v.string);
4980 if (token.type != T_WIDE_STRING_LITERAL) {
4981 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4982 /* note: that we use type_char_ptr here, which is already the
4983 * automatic converted type. revert_automatic_type_conversion
4984 * will construct the array type */
4985 cnst->base.type = type_char_ptr;
4986 cnst->string.value = res;
4990 wres = concat_string_wide_string(&res, &token.v.wide_string);
4992 wres = token.v.wide_string;
4997 switch (token.type) {
4998 case T_WIDE_STRING_LITERAL:
4999 wres = concat_wide_strings(&wres, &token.v.wide_string);
5002 case T_STRING_LITERAL:
5003 wres = concat_wide_string_string(&wres, &token.v.string);
5007 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
5008 cnst->base.type = type_wchar_t_ptr;
5009 cnst->wide_string.value = wres;
5018 * Parse an integer constant.
5020 static expression_t *parse_int_const(void)
5022 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5023 cnst->base.source_position = *HERE;
5024 cnst->base.type = token.datatype;
5025 cnst->conste.v.int_value = token.v.intvalue;
5033 * Parse a character constant.
5035 static expression_t *parse_character_constant(void)
5037 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5039 cnst->base.source_position = *HERE;
5040 cnst->base.type = token.datatype;
5041 cnst->conste.v.character = token.v.string;
5043 if (cnst->conste.v.character.size != 1) {
5044 if (warning.multichar && (c_mode & _GNUC)) {
5046 warningf(HERE, "multi-character character constant");
5048 errorf(HERE, "more than 1 characters in character constant");
5057 * Parse a wide character constant.
5059 static expression_t *parse_wide_character_constant(void)
5061 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5063 cnst->base.source_position = *HERE;
5064 cnst->base.type = token.datatype;
5065 cnst->conste.v.wide_character = token.v.wide_string;
5067 if (cnst->conste.v.wide_character.size != 1) {
5068 if (warning.multichar && (c_mode & _GNUC)) {
5070 warningf(HERE, "multi-character character constant");
5072 errorf(HERE, "more than 1 characters in character constant");
5081 * Parse a float constant.
5083 static expression_t *parse_float_const(void)
5085 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5086 cnst->base.type = token.datatype;
5087 cnst->conste.v.float_value = token.v.floatvalue;
5094 static declaration_t *create_implicit_function(symbol_t *symbol,
5095 const source_position_t *source_position)
5097 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5098 ntype->function.return_type = type_int;
5099 ntype->function.unspecified_parameters = true;
5101 type_t *type = typehash_insert(ntype);
5102 if (type != ntype) {
5106 declaration_t *const declaration = allocate_declaration_zero();
5107 declaration->storage_class = STORAGE_CLASS_EXTERN;
5108 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5109 declaration->type = type;
5110 declaration->symbol = symbol;
5111 declaration->source_position = *source_position;
5113 bool strict_prototypes_old = warning.strict_prototypes;
5114 warning.strict_prototypes = false;
5115 record_declaration(declaration);
5116 warning.strict_prototypes = strict_prototypes_old;
5122 * Creates a return_type (func)(argument_type) function type if not
5125 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5126 type_t *argument_type2)
5128 function_parameter_t *parameter2
5129 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5130 memset(parameter2, 0, sizeof(parameter2[0]));
5131 parameter2->type = argument_type2;
5133 function_parameter_t *parameter1
5134 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5135 memset(parameter1, 0, sizeof(parameter1[0]));
5136 parameter1->type = argument_type1;
5137 parameter1->next = parameter2;
5139 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5140 type->function.return_type = return_type;
5141 type->function.parameters = parameter1;
5143 type_t *result = typehash_insert(type);
5144 if (result != type) {
5152 * Creates a return_type (func)(argument_type) function type if not
5155 * @param return_type the return type
5156 * @param argument_type the argument type
5158 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5160 function_parameter_t *parameter
5161 = obstack_alloc(type_obst, sizeof(parameter[0]));
5162 memset(parameter, 0, sizeof(parameter[0]));
5163 parameter->type = argument_type;
5165 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5166 type->function.return_type = return_type;
5167 type->function.parameters = parameter;
5169 type_t *result = typehash_insert(type);
5170 if (result != type) {
5177 static type_t *make_function_0_type(type_t *return_type)
5179 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5180 type->function.return_type = return_type;
5181 type->function.parameters = NULL;
5183 type_t *result = typehash_insert(type);
5184 if (result != type) {
5192 * Creates a function type for some function like builtins.
5194 * @param symbol the symbol describing the builtin
5196 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5198 switch(symbol->ID) {
5199 case T___builtin_alloca:
5200 return make_function_1_type(type_void_ptr, type_size_t);
5201 case T___builtin_huge_val:
5202 return make_function_0_type(type_double);
5203 case T___builtin_nan:
5204 return make_function_1_type(type_double, type_char_ptr);
5205 case T___builtin_nanf:
5206 return make_function_1_type(type_float, type_char_ptr);
5207 case T___builtin_nand:
5208 return make_function_1_type(type_long_double, type_char_ptr);
5209 case T___builtin_va_end:
5210 return make_function_1_type(type_void, type_valist);
5211 case T___builtin_expect:
5212 return make_function_2_type(type_long, type_long, type_long);
5214 internal_errorf(HERE, "not implemented builtin symbol found");
5219 * Performs automatic type cast as described in § 6.3.2.1.
5221 * @param orig_type the original type
5223 static type_t *automatic_type_conversion(type_t *orig_type)
5225 type_t *type = skip_typeref(orig_type);
5226 if (is_type_array(type)) {
5227 array_type_t *array_type = &type->array;
5228 type_t *element_type = array_type->element_type;
5229 unsigned qualifiers = array_type->base.qualifiers;
5231 return make_pointer_type(element_type, qualifiers);
5234 if (is_type_function(type)) {
5235 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5242 * reverts the automatic casts of array to pointer types and function
5243 * to function-pointer types as defined § 6.3.2.1
5245 type_t *revert_automatic_type_conversion(const expression_t *expression)
5247 switch (expression->kind) {
5248 case EXPR_REFERENCE: return expression->reference.declaration->type;
5249 case EXPR_SELECT: return expression->select.compound_entry->type;
5251 case EXPR_UNARY_DEREFERENCE: {
5252 const expression_t *const value = expression->unary.value;
5253 type_t *const type = skip_typeref(value->base.type);
5254 assert(is_type_pointer(type));
5255 return type->pointer.points_to;
5258 case EXPR_BUILTIN_SYMBOL:
5259 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5261 case EXPR_ARRAY_ACCESS: {
5262 const expression_t *array_ref = expression->array_access.array_ref;
5263 type_t *type_left = skip_typeref(array_ref->base.type);
5264 if (!is_type_valid(type_left))
5266 assert(is_type_pointer(type_left));
5267 return type_left->pointer.points_to;
5270 case EXPR_STRING_LITERAL: {
5271 size_t size = expression->string.value.size;
5272 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5275 case EXPR_WIDE_STRING_LITERAL: {
5276 size_t size = expression->wide_string.value.size;
5277 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5280 case EXPR_COMPOUND_LITERAL:
5281 return expression->compound_literal.type;
5286 return expression->base.type;
5289 static expression_t *parse_reference(void)
5291 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5293 reference_expression_t *ref = &expression->reference;
5294 symbol_t *const symbol = token.v.symbol;
5296 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5298 source_position_t source_position = token.source_position;
5301 if (declaration == NULL) {
5302 if (! strict_mode && token.type == '(') {
5303 /* an implicitly defined function */
5304 if (warning.implicit_function_declaration) {
5305 warningf(HERE, "implicit declaration of function '%Y'",
5309 declaration = create_implicit_function(symbol,
5312 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5313 return create_invalid_expression();
5317 type_t *type = declaration->type;
5319 /* we always do the auto-type conversions; the & and sizeof parser contains
5320 * code to revert this! */
5321 type = automatic_type_conversion(type);
5323 ref->declaration = declaration;
5324 ref->base.type = type;
5326 /* this declaration is used */
5327 declaration->used = true;
5329 /* check for deprecated functions */
5330 if (declaration->deprecated != 0) {
5331 const char *prefix = "";
5332 if (is_type_function(declaration->type))
5333 prefix = "function ";
5335 if (declaration->deprecated_string != NULL) {
5336 warningf(&source_position,
5337 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
5338 declaration->deprecated_string);
5340 warningf(&source_position,
5341 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
5348 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
5352 /* TODO check if explicit cast is allowed and issue warnings/errors */
5355 static expression_t *parse_compound_literal(type_t *type)
5357 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5359 parse_initializer_env_t env;
5361 env.declaration = NULL;
5362 env.must_be_constant = false;
5363 initializer_t *initializer = parse_initializer(&env);
5366 expression->compound_literal.initializer = initializer;
5367 expression->compound_literal.type = type;
5368 expression->base.type = automatic_type_conversion(type);
5374 * Parse a cast expression.
5376 static expression_t *parse_cast(void)
5378 source_position_t source_position = token.source_position;
5380 type_t *type = parse_typename();
5382 /* matching add_anchor_token() is at call site */
5383 rem_anchor_token(')');
5386 if (token.type == '{') {
5387 return parse_compound_literal(type);
5390 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5391 cast->base.source_position = source_position;
5393 expression_t *value = parse_sub_expression(20);
5395 check_cast_allowed(value, type);
5397 cast->base.type = type;
5398 cast->unary.value = value;
5402 return create_invalid_expression();
5406 * Parse a statement expression.
5408 static expression_t *parse_statement_expression(void)
5410 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5412 statement_t *statement = parse_compound_statement(true);
5413 expression->statement.statement = statement;
5414 expression->base.source_position = statement->base.source_position;
5416 /* find last statement and use its type */
5417 type_t *type = type_void;
5418 const statement_t *stmt = statement->compound.statements;
5420 while (stmt->base.next != NULL)
5421 stmt = stmt->base.next;
5423 if (stmt->kind == STATEMENT_EXPRESSION) {
5424 type = stmt->expression.expression->base.type;
5427 warningf(&expression->base.source_position, "empty statement expression ({})");
5429 expression->base.type = type;
5435 return create_invalid_expression();
5439 * Parse a braced expression.
5441 static expression_t *parse_brace_expression(void)
5444 add_anchor_token(')');
5446 switch(token.type) {
5448 /* gcc extension: a statement expression */
5449 return parse_statement_expression();
5453 return parse_cast();
5455 if (is_typedef_symbol(token.v.symbol)) {
5456 return parse_cast();
5460 expression_t *result = parse_expression();
5461 rem_anchor_token(')');
5466 return create_invalid_expression();
5469 static expression_t *parse_function_keyword(void)
5474 if (current_function == NULL) {
5475 errorf(HERE, "'__func__' used outside of a function");
5478 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5479 expression->base.type = type_char_ptr;
5480 expression->funcname.kind = FUNCNAME_FUNCTION;
5485 static expression_t *parse_pretty_function_keyword(void)
5487 eat(T___PRETTY_FUNCTION__);
5489 if (current_function == NULL) {
5490 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5493 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5494 expression->base.type = type_char_ptr;
5495 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5500 static expression_t *parse_funcsig_keyword(void)
5504 if (current_function == NULL) {
5505 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5508 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5509 expression->base.type = type_char_ptr;
5510 expression->funcname.kind = FUNCNAME_FUNCSIG;
5515 static expression_t *parse_funcdname_keyword(void)
5517 eat(T___FUNCDNAME__);
5519 if (current_function == NULL) {
5520 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5523 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5524 expression->base.type = type_char_ptr;
5525 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5530 static designator_t *parse_designator(void)
5532 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5533 result->source_position = *HERE;
5535 if (token.type != T_IDENTIFIER) {
5536 parse_error_expected("while parsing member designator",
5537 T_IDENTIFIER, NULL);
5540 result->symbol = token.v.symbol;
5543 designator_t *last_designator = result;
5545 if (token.type == '.') {
5547 if (token.type != T_IDENTIFIER) {
5548 parse_error_expected("while parsing member designator",
5549 T_IDENTIFIER, NULL);
5552 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5553 designator->source_position = *HERE;
5554 designator->symbol = token.v.symbol;
5557 last_designator->next = designator;
5558 last_designator = designator;
5561 if (token.type == '[') {
5563 add_anchor_token(']');
5564 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5565 designator->source_position = *HERE;
5566 designator->array_index = parse_expression();
5567 rem_anchor_token(']');
5569 if (designator->array_index == NULL) {
5573 last_designator->next = designator;
5574 last_designator = designator;
5586 * Parse the __builtin_offsetof() expression.
5588 static expression_t *parse_offsetof(void)
5590 eat(T___builtin_offsetof);
5592 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5593 expression->base.type = type_size_t;
5596 add_anchor_token(',');
5597 type_t *type = parse_typename();
5598 rem_anchor_token(',');
5600 add_anchor_token(')');
5601 designator_t *designator = parse_designator();
5602 rem_anchor_token(')');
5605 expression->offsetofe.type = type;
5606 expression->offsetofe.designator = designator;
5609 memset(&path, 0, sizeof(path));
5610 path.top_type = type;
5611 path.path = NEW_ARR_F(type_path_entry_t, 0);
5613 descend_into_subtype(&path);
5615 if (!walk_designator(&path, designator, true)) {
5616 return create_invalid_expression();
5619 DEL_ARR_F(path.path);
5623 return create_invalid_expression();
5627 * Parses a _builtin_va_start() expression.
5629 static expression_t *parse_va_start(void)
5631 eat(T___builtin_va_start);
5633 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5636 add_anchor_token(',');
5637 expression->va_starte.ap = parse_assignment_expression();
5638 rem_anchor_token(',');
5640 expression_t *const expr = parse_assignment_expression();
5641 if (expr->kind == EXPR_REFERENCE) {
5642 declaration_t *const decl = expr->reference.declaration;
5644 return create_invalid_expression();
5645 if (decl->parent_scope == ¤t_function->scope &&
5646 decl->next == NULL) {
5647 expression->va_starte.parameter = decl;
5652 errorf(&expr->base.source_position,
5653 "second argument of 'va_start' must be last parameter of the current function");
5655 return create_invalid_expression();
5659 * Parses a _builtin_va_arg() expression.
5661 static expression_t *parse_va_arg(void)
5663 eat(T___builtin_va_arg);
5665 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5668 expression->va_arge.ap = parse_assignment_expression();
5670 expression->base.type = parse_typename();
5675 return create_invalid_expression();
5678 static expression_t *parse_builtin_symbol(void)
5680 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5682 symbol_t *symbol = token.v.symbol;
5684 expression->builtin_symbol.symbol = symbol;
5687 type_t *type = get_builtin_symbol_type(symbol);
5688 type = automatic_type_conversion(type);
5690 expression->base.type = type;
5695 * Parses a __builtin_constant() expression.
5697 static expression_t *parse_builtin_constant(void)
5699 eat(T___builtin_constant_p);
5701 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5704 add_anchor_token(')');
5705 expression->builtin_constant.value = parse_assignment_expression();
5706 rem_anchor_token(')');
5708 expression->base.type = type_int;
5712 return create_invalid_expression();
5716 * Parses a __builtin_prefetch() expression.
5718 static expression_t *parse_builtin_prefetch(void)
5720 eat(T___builtin_prefetch);
5722 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5725 add_anchor_token(')');
5726 expression->builtin_prefetch.adr = parse_assignment_expression();
5727 if (token.type == ',') {
5729 expression->builtin_prefetch.rw = parse_assignment_expression();
5731 if (token.type == ',') {
5733 expression->builtin_prefetch.locality = parse_assignment_expression();
5735 rem_anchor_token(')');
5737 expression->base.type = type_void;
5741 return create_invalid_expression();
5745 * Parses a __builtin_is_*() compare expression.
5747 static expression_t *parse_compare_builtin(void)
5749 expression_t *expression;
5751 switch(token.type) {
5752 case T___builtin_isgreater:
5753 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5755 case T___builtin_isgreaterequal:
5756 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5758 case T___builtin_isless:
5759 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5761 case T___builtin_islessequal:
5762 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5764 case T___builtin_islessgreater:
5765 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5767 case T___builtin_isunordered:
5768 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5771 internal_errorf(HERE, "invalid compare builtin found");
5774 expression->base.source_position = *HERE;
5778 expression->binary.left = parse_assignment_expression();
5780 expression->binary.right = parse_assignment_expression();
5783 type_t *const orig_type_left = expression->binary.left->base.type;
5784 type_t *const orig_type_right = expression->binary.right->base.type;
5786 type_t *const type_left = skip_typeref(orig_type_left);
5787 type_t *const type_right = skip_typeref(orig_type_right);
5788 if (!is_type_float(type_left) && !is_type_float(type_right)) {
5789 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5790 type_error_incompatible("invalid operands in comparison",
5791 &expression->base.source_position, orig_type_left, orig_type_right);
5794 semantic_comparison(&expression->binary);
5799 return create_invalid_expression();
5804 * Parses a __builtin_expect() expression.
5806 static expression_t *parse_builtin_expect(void)
5808 eat(T___builtin_expect);
5810 expression_t *expression
5811 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5814 expression->binary.left = parse_assignment_expression();
5816 expression->binary.right = parse_constant_expression();
5819 expression->base.type = expression->binary.left->base.type;
5823 return create_invalid_expression();
5828 * Parses a MS assume() expression.
5830 static expression_t *parse_assume(void)
5834 expression_t *expression
5835 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5838 add_anchor_token(')');
5839 expression->unary.value = parse_assignment_expression();
5840 rem_anchor_token(')');
5843 expression->base.type = type_void;
5846 return create_invalid_expression();
5850 * Parse a microsoft __noop expression.
5852 static expression_t *parse_noop_expression(void)
5854 source_position_t source_position = *HERE;
5857 if (token.type == '(') {
5858 /* parse arguments */
5860 add_anchor_token(')');
5861 add_anchor_token(',');
5863 if (token.type != ')') {
5865 (void)parse_assignment_expression();
5866 if (token.type != ',')
5872 rem_anchor_token(',');
5873 rem_anchor_token(')');
5876 /* the result is a (int)0 */
5877 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5878 cnst->base.source_position = source_position;
5879 cnst->base.type = type_int;
5880 cnst->conste.v.int_value = 0;
5881 cnst->conste.is_ms_noop = true;
5886 return create_invalid_expression();
5890 * Parses a primary expression.
5892 static expression_t *parse_primary_expression(void)
5894 switch (token.type) {
5895 case T_INTEGER: return parse_int_const();
5896 case T_CHARACTER_CONSTANT: return parse_character_constant();
5897 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5898 case T_FLOATINGPOINT: return parse_float_const();
5899 case T_STRING_LITERAL:
5900 case T_WIDE_STRING_LITERAL: return parse_string_const();
5901 case T_IDENTIFIER: return parse_reference();
5902 case T___FUNCTION__:
5903 case T___func__: return parse_function_keyword();
5904 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5905 case T___FUNCSIG__: return parse_funcsig_keyword();
5906 case T___FUNCDNAME__: return parse_funcdname_keyword();
5907 case T___builtin_offsetof: return parse_offsetof();
5908 case T___builtin_va_start: return parse_va_start();
5909 case T___builtin_va_arg: return parse_va_arg();
5910 case T___builtin_expect:
5911 case T___builtin_alloca:
5912 case T___builtin_nan:
5913 case T___builtin_nand:
5914 case T___builtin_nanf:
5915 case T___builtin_huge_val:
5916 case T___builtin_va_end: return parse_builtin_symbol();
5917 case T___builtin_isgreater:
5918 case T___builtin_isgreaterequal:
5919 case T___builtin_isless:
5920 case T___builtin_islessequal:
5921 case T___builtin_islessgreater:
5922 case T___builtin_isunordered: return parse_compare_builtin();
5923 case T___builtin_constant_p: return parse_builtin_constant();
5924 case T___builtin_prefetch: return parse_builtin_prefetch();
5925 case T__assume: return parse_assume();
5927 case '(': return parse_brace_expression();
5928 case T___noop: return parse_noop_expression();
5931 errorf(HERE, "unexpected token %K, expected an expression", &token);
5932 return create_invalid_expression();
5936 * Check if the expression has the character type and issue a warning then.
5938 static void check_for_char_index_type(const expression_t *expression)
5940 type_t *const type = expression->base.type;
5941 const type_t *const base_type = skip_typeref(type);
5943 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5944 warning.char_subscripts) {
5945 warningf(&expression->base.source_position,
5946 "array subscript has type '%T'", type);
5950 static expression_t *parse_array_expression(unsigned precedence,
5956 add_anchor_token(']');
5958 expression_t *inside = parse_expression();
5960 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5962 array_access_expression_t *array_access = &expression->array_access;
5964 type_t *const orig_type_left = left->base.type;
5965 type_t *const orig_type_inside = inside->base.type;
5967 type_t *const type_left = skip_typeref(orig_type_left);
5968 type_t *const type_inside = skip_typeref(orig_type_inside);
5970 type_t *return_type;
5971 if (is_type_pointer(type_left)) {
5972 return_type = type_left->pointer.points_to;
5973 array_access->array_ref = left;
5974 array_access->index = inside;
5975 check_for_char_index_type(inside);
5976 } else if (is_type_pointer(type_inside)) {
5977 return_type = type_inside->pointer.points_to;
5978 array_access->array_ref = inside;
5979 array_access->index = left;
5980 array_access->flipped = true;
5981 check_for_char_index_type(left);
5983 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5985 "array access on object with non-pointer types '%T', '%T'",
5986 orig_type_left, orig_type_inside);
5988 return_type = type_error_type;
5989 array_access->array_ref = create_invalid_expression();
5992 rem_anchor_token(']');
5993 if (token.type != ']') {
5994 parse_error_expected("Problem while parsing array access", ']', NULL);
5999 return_type = automatic_type_conversion(return_type);
6000 expression->base.type = return_type;
6005 static expression_t *parse_typeprop(expression_kind_t const kind,
6006 source_position_t const pos,
6007 unsigned const precedence)
6009 expression_t *tp_expression = allocate_expression_zero(kind);
6010 tp_expression->base.type = type_size_t;
6011 tp_expression->base.source_position = pos;
6013 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
6015 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
6017 add_anchor_token(')');
6018 type_t* const orig_type = parse_typename();
6019 tp_expression->typeprop.type = orig_type;
6021 type_t const* const type = skip_typeref(orig_type);
6022 char const* const wrong_type =
6023 is_type_incomplete(type) ? "incomplete" :
6024 type->kind == TYPE_FUNCTION ? "function designator" :
6025 type->kind == TYPE_BITFIELD ? "bitfield" :
6027 if (wrong_type != NULL) {
6028 errorf(&pos, "operand of %s expression must not be %s type '%T'",
6029 what, wrong_type, type);
6032 rem_anchor_token(')');
6035 expression_t *expression = parse_sub_expression(precedence);
6037 type_t* const orig_type = revert_automatic_type_conversion(expression);
6038 expression->base.type = orig_type;
6040 type_t const* const type = skip_typeref(orig_type);
6041 char const* const wrong_type =
6042 is_type_incomplete(type) ? "incomplete" :
6043 type->kind == TYPE_FUNCTION ? "function designator" :
6044 type->kind == TYPE_BITFIELD ? "bitfield" :
6046 if (wrong_type != NULL) {
6047 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6050 tp_expression->typeprop.type = expression->base.type;
6051 tp_expression->typeprop.tp_expression = expression;
6054 return tp_expression;
6056 return create_invalid_expression();
6059 static expression_t *parse_sizeof(unsigned precedence)
6061 source_position_t pos = *HERE;
6063 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6066 static expression_t *parse_alignof(unsigned precedence)
6068 source_position_t pos = *HERE;
6070 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6073 static expression_t *parse_select_expression(unsigned precedence,
6074 expression_t *compound)
6077 assert(token.type == '.' || token.type == T_MINUSGREATER);
6079 bool is_pointer = (token.type == T_MINUSGREATER);
6082 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6083 select->select.compound = compound;
6085 if (token.type != T_IDENTIFIER) {
6086 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6089 symbol_t *symbol = token.v.symbol;
6090 select->select.symbol = symbol;
6093 type_t *const orig_type = compound->base.type;
6094 type_t *const type = skip_typeref(orig_type);
6096 type_t *type_left = type;
6098 if (!is_type_pointer(type)) {
6099 if (is_type_valid(type)) {
6100 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6102 return create_invalid_expression();
6104 type_left = type->pointer.points_to;
6106 type_left = skip_typeref(type_left);
6108 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6109 type_left->kind != TYPE_COMPOUND_UNION) {
6110 if (is_type_valid(type_left)) {
6111 errorf(HERE, "request for member '%Y' in something not a struct or "
6112 "union, but '%T'", symbol, type_left);
6114 return create_invalid_expression();
6117 declaration_t *const declaration = type_left->compound.declaration;
6119 if (!declaration->init.complete) {
6120 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6122 return create_invalid_expression();
6125 declaration_t *iter = find_compound_entry(declaration, symbol);
6127 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6128 return create_invalid_expression();
6131 /* we always do the auto-type conversions; the & and sizeof parser contains
6132 * code to revert this! */
6133 type_t *expression_type = automatic_type_conversion(iter->type);
6135 select->select.compound_entry = iter;
6136 select->base.type = expression_type;
6138 type_t *skipped = skip_typeref(iter->type);
6139 if (skipped->kind == TYPE_BITFIELD) {
6140 select->base.type = skipped->bitfield.base_type;
6146 static void check_call_argument(const function_parameter_t *parameter,
6147 call_argument_t *argument)
6149 type_t *expected_type = parameter->type;
6150 type_t *expected_type_skip = skip_typeref(expected_type);
6151 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6152 expression_t *arg_expr = argument->expression;
6154 /* handle transparent union gnu extension */
6155 if (is_type_union(expected_type_skip)
6156 && (expected_type_skip->base.modifiers
6157 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6158 declaration_t *union_decl = expected_type_skip->compound.declaration;
6160 declaration_t *declaration = union_decl->scope.declarations;
6161 type_t *best_type = NULL;
6162 for ( ; declaration != NULL; declaration = declaration->next) {
6163 type_t *decl_type = declaration->type;
6164 error = semantic_assign(decl_type, arg_expr);
6165 if (error == ASSIGN_ERROR_INCOMPATIBLE
6166 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6169 if (error == ASSIGN_SUCCESS) {
6170 best_type = decl_type;
6171 } else if (best_type == NULL) {
6172 best_type = decl_type;
6176 if (best_type != NULL) {
6177 expected_type = best_type;
6181 error = semantic_assign(expected_type, arg_expr);
6182 argument->expression = create_implicit_cast(argument->expression,
6185 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6186 report_assign_error(error, expected_type, arg_expr, "function call",
6187 &arg_expr->base.source_position);
6191 * Parse a call expression, ie. expression '( ... )'.
6193 * @param expression the function address
6195 static expression_t *parse_call_expression(unsigned precedence,
6196 expression_t *expression)
6199 expression_t *result = allocate_expression_zero(EXPR_CALL);
6200 result->base.source_position = expression->base.source_position;
6202 call_expression_t *call = &result->call;
6203 call->function = expression;
6205 type_t *const orig_type = expression->base.type;
6206 type_t *const type = skip_typeref(orig_type);
6208 function_type_t *function_type = NULL;
6209 if (is_type_pointer(type)) {
6210 type_t *const to_type = skip_typeref(type->pointer.points_to);
6212 if (is_type_function(to_type)) {
6213 function_type = &to_type->function;
6214 call->base.type = function_type->return_type;
6218 if (function_type == NULL && is_type_valid(type)) {
6219 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6222 /* parse arguments */
6224 add_anchor_token(')');
6225 add_anchor_token(',');
6227 if (token.type != ')') {
6228 call_argument_t *last_argument = NULL;
6231 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6233 argument->expression = parse_assignment_expression();
6234 if (last_argument == NULL) {
6235 call->arguments = argument;
6237 last_argument->next = argument;
6239 last_argument = argument;
6241 if (token.type != ',')
6246 rem_anchor_token(',');
6247 rem_anchor_token(')');
6250 if (function_type == NULL)
6253 function_parameter_t *parameter = function_type->parameters;
6254 call_argument_t *argument = call->arguments;
6255 if (!function_type->unspecified_parameters) {
6256 for( ; parameter != NULL && argument != NULL;
6257 parameter = parameter->next, argument = argument->next) {
6258 check_call_argument(parameter, argument);
6261 if (parameter != NULL) {
6262 errorf(HERE, "too few arguments to function '%E'", expression);
6263 } else if (argument != NULL && !function_type->variadic) {
6264 errorf(HERE, "too many arguments to function '%E'", expression);
6268 /* do default promotion */
6269 for( ; argument != NULL; argument = argument->next) {
6270 type_t *type = argument->expression->base.type;
6272 type = get_default_promoted_type(type);
6274 argument->expression
6275 = create_implicit_cast(argument->expression, type);
6278 check_format(&result->call);
6282 return create_invalid_expression();
6285 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6287 static bool same_compound_type(const type_t *type1, const type_t *type2)
6290 is_type_compound(type1) &&
6291 type1->kind == type2->kind &&
6292 type1->compound.declaration == type2->compound.declaration;
6296 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6298 * @param expression the conditional expression
6300 static expression_t *parse_conditional_expression(unsigned precedence,
6301 expression_t *expression)
6304 add_anchor_token(':');
6306 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6308 conditional_expression_t *conditional = &result->conditional;
6309 conditional->condition = expression;
6312 type_t *const condition_type_orig = expression->base.type;
6313 type_t *const condition_type = skip_typeref(condition_type_orig);
6314 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6315 type_error("expected a scalar type in conditional condition",
6316 &expression->base.source_position, condition_type_orig);
6319 expression_t *true_expression = parse_expression();
6320 rem_anchor_token(':');
6322 expression_t *false_expression = parse_sub_expression(precedence);
6324 type_t *const orig_true_type = true_expression->base.type;
6325 type_t *const orig_false_type = false_expression->base.type;
6326 type_t *const true_type = skip_typeref(orig_true_type);
6327 type_t *const false_type = skip_typeref(orig_false_type);
6330 type_t *result_type;
6331 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6332 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6333 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6334 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6335 warningf(&expression->base.source_position,
6336 "ISO C forbids conditional expression with only one void side");
6338 result_type = type_void;
6339 } else if (is_type_arithmetic(true_type)
6340 && is_type_arithmetic(false_type)) {
6341 result_type = semantic_arithmetic(true_type, false_type);
6343 true_expression = create_implicit_cast(true_expression, result_type);
6344 false_expression = create_implicit_cast(false_expression, result_type);
6346 conditional->true_expression = true_expression;
6347 conditional->false_expression = false_expression;
6348 conditional->base.type = result_type;
6349 } else if (same_compound_type(true_type, false_type)) {
6350 /* just take 1 of the 2 types */
6351 result_type = true_type;
6352 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6353 type_t *pointer_type;
6355 expression_t *other_expression;
6356 if (is_type_pointer(true_type) &&
6357 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6358 pointer_type = true_type;
6359 other_type = false_type;
6360 other_expression = false_expression;
6362 pointer_type = false_type;
6363 other_type = true_type;
6364 other_expression = true_expression;
6367 if (is_null_pointer_constant(other_expression)) {
6368 result_type = pointer_type;
6369 } else if (is_type_pointer(other_type)) {
6370 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
6371 type_t *to2 = skip_typeref(other_type->pointer.points_to);
6374 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
6375 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
6377 } else if (types_compatible(get_unqualified_type(to1),
6378 get_unqualified_type(to2))) {
6381 warningf(&expression->base.source_position,
6382 "pointer types '%T' and '%T' in conditional expression are incompatible",
6383 true_type, false_type);
6387 type_t *const copy = duplicate_type(to);
6388 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
6390 type_t *const type = typehash_insert(copy);
6394 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
6395 } else if (is_type_integer(other_type)) {
6396 warningf(&expression->base.source_position,
6397 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
6398 result_type = pointer_type;
6400 type_error_incompatible("while parsing conditional",
6401 &expression->base.source_position, true_type, false_type);
6402 result_type = type_error_type;
6405 /* TODO: one pointer to void*, other some pointer */
6407 if (is_type_valid(true_type) && is_type_valid(false_type)) {
6408 type_error_incompatible("while parsing conditional",
6409 &expression->base.source_position, true_type,
6412 result_type = type_error_type;
6415 conditional->true_expression
6416 = create_implicit_cast(true_expression, result_type);
6417 conditional->false_expression
6418 = create_implicit_cast(false_expression, result_type);
6419 conditional->base.type = result_type;
6422 return create_invalid_expression();
6426 * Parse an extension expression.
6428 static expression_t *parse_extension(unsigned precedence)
6430 eat(T___extension__);
6432 /* TODO enable extensions */
6433 expression_t *expression = parse_sub_expression(precedence);
6434 /* TODO disable extensions */
6439 * Parse a __builtin_classify_type() expression.
6441 static expression_t *parse_builtin_classify_type(const unsigned precedence)
6443 eat(T___builtin_classify_type);
6445 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
6446 result->base.type = type_int;
6449 add_anchor_token(')');
6450 expression_t *expression = parse_sub_expression(precedence);
6451 rem_anchor_token(')');
6453 result->classify_type.type_expression = expression;
6457 return create_invalid_expression();
6460 static void check_pointer_arithmetic(const source_position_t *source_position,
6461 type_t *pointer_type,
6462 type_t *orig_pointer_type)
6464 type_t *points_to = pointer_type->pointer.points_to;
6465 points_to = skip_typeref(points_to);
6467 if (is_type_incomplete(points_to) &&
6469 || !is_type_atomic(points_to, ATOMIC_TYPE_VOID))) {
6470 errorf(source_position,
6471 "arithmetic with pointer to incomplete type '%T' not allowed",
6473 } else if (is_type_function(points_to)) {
6474 errorf(source_position,
6475 "arithmetic with pointer to function type '%T' not allowed",
6480 static void semantic_incdec(unary_expression_t *expression)
6482 type_t *const orig_type = expression->value->base.type;
6483 type_t *const type = skip_typeref(orig_type);
6484 if (is_type_pointer(type)) {
6485 check_pointer_arithmetic(&expression->base.source_position,
6487 } else if (!is_type_real(type) && is_type_valid(type)) {
6488 /* TODO: improve error message */
6489 errorf(HERE, "operation needs an arithmetic or pointer type");
6491 expression->base.type = orig_type;
6494 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6496 type_t *const orig_type = expression->value->base.type;
6497 type_t *const type = skip_typeref(orig_type);
6498 if (!is_type_arithmetic(type)) {
6499 if (is_type_valid(type)) {
6500 /* TODO: improve error message */
6501 errorf(HERE, "operation needs an arithmetic type");
6506 expression->base.type = orig_type;
6509 static void semantic_unexpr_scalar(unary_expression_t *expression)
6511 type_t *const orig_type = expression->value->base.type;
6512 type_t *const type = skip_typeref(orig_type);
6513 if (!is_type_scalar(type)) {
6514 if (is_type_valid(type)) {
6515 errorf(HERE, "operand of ! must be of scalar type");
6520 expression->base.type = orig_type;
6523 static void semantic_unexpr_integer(unary_expression_t *expression)
6525 type_t *const orig_type = expression->value->base.type;
6526 type_t *const type = skip_typeref(orig_type);
6527 if (!is_type_integer(type)) {
6528 if (is_type_valid(type)) {
6529 errorf(HERE, "operand of ~ must be of integer type");
6534 expression->base.type = orig_type;
6537 static void semantic_dereference(unary_expression_t *expression)
6539 type_t *const orig_type = expression->value->base.type;
6540 type_t *const type = skip_typeref(orig_type);
6541 if (!is_type_pointer(type)) {
6542 if (is_type_valid(type)) {
6543 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6548 type_t *result_type = type->pointer.points_to;
6549 result_type = automatic_type_conversion(result_type);
6550 expression->base.type = result_type;
6553 static void set_address_taken(expression_t *expression, bool may_be_register)
6555 if (expression->kind != EXPR_REFERENCE)
6558 declaration_t *const declaration = expression->reference.declaration;
6559 /* happens for parse errors */
6560 if (declaration == NULL)
6563 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
6564 errorf(&expression->base.source_position,
6565 "address of register variable '%Y' requested",
6566 declaration->symbol);
6568 declaration->address_taken = 1;
6573 * Check the semantic of the address taken expression.
6575 static void semantic_take_addr(unary_expression_t *expression)
6577 expression_t *value = expression->value;
6578 value->base.type = revert_automatic_type_conversion(value);
6580 type_t *orig_type = value->base.type;
6581 if (!is_type_valid(orig_type))
6584 set_address_taken(value, false);
6586 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6589 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6590 static expression_t *parse_##unexpression_type(unsigned precedence) \
6594 expression_t *unary_expression \
6595 = allocate_expression_zero(unexpression_type); \
6596 unary_expression->base.source_position = *HERE; \
6597 unary_expression->unary.value = parse_sub_expression(precedence); \
6599 sfunc(&unary_expression->unary); \
6601 return unary_expression; \
6604 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6605 semantic_unexpr_arithmetic)
6606 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6607 semantic_unexpr_arithmetic)
6608 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6609 semantic_unexpr_scalar)
6610 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6611 semantic_dereference)
6612 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6614 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6615 semantic_unexpr_integer)
6616 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6618 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6621 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6623 static expression_t *parse_##unexpression_type(unsigned precedence, \
6624 expression_t *left) \
6626 (void) precedence; \
6629 expression_t *unary_expression \
6630 = allocate_expression_zero(unexpression_type); \
6631 unary_expression->unary.value = left; \
6633 sfunc(&unary_expression->unary); \
6635 return unary_expression; \
6638 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6639 EXPR_UNARY_POSTFIX_INCREMENT,
6641 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6642 EXPR_UNARY_POSTFIX_DECREMENT,
6645 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6647 /* TODO: handle complex + imaginary types */
6649 /* § 6.3.1.8 Usual arithmetic conversions */
6650 if (type_left == type_long_double || type_right == type_long_double) {
6651 return type_long_double;
6652 } else if (type_left == type_double || type_right == type_double) {
6654 } else if (type_left == type_float || type_right == type_float) {
6658 type_left = promote_integer(type_left);
6659 type_right = promote_integer(type_right);
6661 if (type_left == type_right)
6664 bool const signed_left = is_type_signed(type_left);
6665 bool const signed_right = is_type_signed(type_right);
6666 int const rank_left = get_rank(type_left);
6667 int const rank_right = get_rank(type_right);
6669 if (signed_left == signed_right)
6670 return rank_left >= rank_right ? type_left : type_right;
6679 u_rank = rank_right;
6680 u_type = type_right;
6682 s_rank = rank_right;
6683 s_type = type_right;
6688 if (u_rank >= s_rank)
6691 if (get_atomic_type_size(s_rank) > get_atomic_type_size(u_rank))
6695 type_t *const type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
6697 case ATOMIC_TYPE_INT: type->atomic.akind = ATOMIC_TYPE_UINT; break;
6698 case ATOMIC_TYPE_LONG: type->atomic.akind = ATOMIC_TYPE_ULONG; break;
6699 case ATOMIC_TYPE_LONGLONG: type->atomic.akind = ATOMIC_TYPE_ULONGLONG; break;
6701 default: panic("invalid atomic type");
6704 type_t* const result = typehash_insert(type);
6712 * Check the semantic restrictions for a binary expression.
6714 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6716 expression_t *const left = expression->left;
6717 expression_t *const right = expression->right;
6718 type_t *const orig_type_left = left->base.type;
6719 type_t *const orig_type_right = right->base.type;
6720 type_t *const type_left = skip_typeref(orig_type_left);
6721 type_t *const type_right = skip_typeref(orig_type_right);
6723 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6724 /* TODO: improve error message */
6725 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6726 errorf(HERE, "operation needs arithmetic types");
6731 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6732 expression->left = create_implicit_cast(left, arithmetic_type);
6733 expression->right = create_implicit_cast(right, arithmetic_type);
6734 expression->base.type = arithmetic_type;
6737 static void semantic_shift_op(binary_expression_t *expression)
6739 expression_t *const left = expression->left;
6740 expression_t *const right = expression->right;
6741 type_t *const orig_type_left = left->base.type;
6742 type_t *const orig_type_right = right->base.type;
6743 type_t * type_left = skip_typeref(orig_type_left);
6744 type_t * type_right = skip_typeref(orig_type_right);
6746 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
6747 /* TODO: improve error message */
6748 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6749 errorf(HERE, "operation needs integer types");
6754 type_left = promote_integer(type_left);
6755 type_right = promote_integer(type_right);
6757 expression->left = create_implicit_cast(left, type_left);
6758 expression->right = create_implicit_cast(right, type_right);
6759 expression->base.type = type_left;
6762 static void semantic_add(binary_expression_t *expression)
6764 expression_t *const left = expression->left;
6765 expression_t *const right = expression->right;
6766 type_t *const orig_type_left = left->base.type;
6767 type_t *const orig_type_right = right->base.type;
6768 type_t *const type_left = skip_typeref(orig_type_left);
6769 type_t *const type_right = skip_typeref(orig_type_right);
6772 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6773 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6774 expression->left = create_implicit_cast(left, arithmetic_type);
6775 expression->right = create_implicit_cast(right, arithmetic_type);
6776 expression->base.type = arithmetic_type;
6778 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6779 check_pointer_arithmetic(&expression->base.source_position,
6780 type_left, orig_type_left);
6781 expression->base.type = type_left;
6782 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
6783 check_pointer_arithmetic(&expression->base.source_position,
6784 type_right, orig_type_right);
6785 expression->base.type = type_right;
6786 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6787 errorf(&expression->base.source_position,
6788 "invalid operands to binary + ('%T', '%T')",
6789 orig_type_left, orig_type_right);
6793 static void semantic_sub(binary_expression_t *expression)
6795 expression_t *const left = expression->left;
6796 expression_t *const right = expression->right;
6797 type_t *const orig_type_left = left->base.type;
6798 type_t *const orig_type_right = right->base.type;
6799 type_t *const type_left = skip_typeref(orig_type_left);
6800 type_t *const type_right = skip_typeref(orig_type_right);
6803 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6804 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6805 expression->left = create_implicit_cast(left, arithmetic_type);
6806 expression->right = create_implicit_cast(right, arithmetic_type);
6807 expression->base.type = arithmetic_type;
6809 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6810 check_pointer_arithmetic(&expression->base.source_position,
6811 type_left, orig_type_left);
6812 expression->base.type = type_left;
6813 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6814 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
6815 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
6816 if (!types_compatible(unqual_left, unqual_right)) {
6817 errorf(&expression->base.source_position,
6818 "subtracting pointers to incompatible types '%T' and '%T'",
6819 orig_type_left, orig_type_right);
6820 } else if (!is_type_object(unqual_left)) {
6821 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
6822 warningf(&expression->base.source_position,
6823 "subtracting pointers to void");
6825 errorf(&expression->base.source_position,
6826 "subtracting pointers to non-object types '%T'",
6830 expression->base.type = type_ptrdiff_t;
6831 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6832 errorf(HERE, "invalid operands of types '%T' and '%T' to binary '-'",
6833 orig_type_left, orig_type_right);
6838 * Check the semantics of comparison expressions.
6840 * @param expression The expression to check.
6842 static void semantic_comparison(binary_expression_t *expression)
6844 expression_t *left = expression->left;
6845 expression_t *right = expression->right;
6846 type_t *orig_type_left = left->base.type;
6847 type_t *orig_type_right = right->base.type;
6849 type_t *type_left = skip_typeref(orig_type_left);
6850 type_t *type_right = skip_typeref(orig_type_right);
6852 /* TODO non-arithmetic types */
6853 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6854 /* test for signed vs unsigned compares */
6855 if (warning.sign_compare &&
6856 (expression->base.kind != EXPR_BINARY_EQUAL &&
6857 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6858 (is_type_signed(type_left) != is_type_signed(type_right))) {
6860 /* check if 1 of the operands is a constant, in this case we just
6861 * check wether we can safely represent the resulting constant in
6862 * the type of the other operand. */
6863 expression_t *const_expr = NULL;
6864 expression_t *other_expr = NULL;
6866 if (is_constant_expression(left)) {
6869 } else if (is_constant_expression(right)) {
6874 if (const_expr != NULL) {
6875 type_t *other_type = skip_typeref(other_expr->base.type);
6876 long val = fold_constant(const_expr);
6877 /* TODO: check if val can be represented by other_type */
6881 warningf(&expression->base.source_position,
6882 "comparison between signed and unsigned");
6884 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6885 expression->left = create_implicit_cast(left, arithmetic_type);
6886 expression->right = create_implicit_cast(right, arithmetic_type);
6887 expression->base.type = arithmetic_type;
6888 if (warning.float_equal &&
6889 (expression->base.kind == EXPR_BINARY_EQUAL ||
6890 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6891 is_type_float(arithmetic_type)) {
6892 warningf(&expression->base.source_position,
6893 "comparing floating point with == or != is unsafe");
6895 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6896 /* TODO check compatibility */
6897 } else if (is_type_pointer(type_left)) {
6898 expression->right = create_implicit_cast(right, type_left);
6899 } else if (is_type_pointer(type_right)) {
6900 expression->left = create_implicit_cast(left, type_right);
6901 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6902 type_error_incompatible("invalid operands in comparison",
6903 &expression->base.source_position,
6904 type_left, type_right);
6906 expression->base.type = type_int;
6910 * Checks if a compound type has constant fields.
6912 static bool has_const_fields(const compound_type_t *type)
6914 const scope_t *scope = &type->declaration->scope;
6915 const declaration_t *declaration = scope->declarations;
6917 for (; declaration != NULL; declaration = declaration->next) {
6918 if (declaration->namespc != NAMESPACE_NORMAL)
6921 const type_t *decl_type = skip_typeref(declaration->type);
6922 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6929 static bool is_lvalue(const expression_t *expression)
6931 switch (expression->kind) {
6932 case EXPR_REFERENCE:
6933 case EXPR_ARRAY_ACCESS:
6935 case EXPR_UNARY_DEREFERENCE:
6943 static bool is_valid_assignment_lhs(expression_t const* const left)
6945 type_t *const orig_type_left = revert_automatic_type_conversion(left);
6946 type_t *const type_left = skip_typeref(orig_type_left);
6948 if (!is_lvalue(left)) {
6949 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
6954 if (is_type_array(type_left)) {
6955 errorf(HERE, "cannot assign to arrays ('%E')", left);
6958 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6959 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6963 if (is_type_incomplete(type_left)) {
6964 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
6965 left, orig_type_left);
6968 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6969 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6970 left, orig_type_left);
6977 static void semantic_arithmetic_assign(binary_expression_t *expression)
6979 expression_t *left = expression->left;
6980 expression_t *right = expression->right;
6981 type_t *orig_type_left = left->base.type;
6982 type_t *orig_type_right = right->base.type;
6984 if (!is_valid_assignment_lhs(left))
6987 type_t *type_left = skip_typeref(orig_type_left);
6988 type_t *type_right = skip_typeref(orig_type_right);
6990 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6991 /* TODO: improve error message */
6992 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6993 errorf(HERE, "operation needs arithmetic types");
6998 /* combined instructions are tricky. We can't create an implicit cast on
6999 * the left side, because we need the uncasted form for the store.
7000 * The ast2firm pass has to know that left_type must be right_type
7001 * for the arithmetic operation and create a cast by itself */
7002 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7003 expression->right = create_implicit_cast(right, arithmetic_type);
7004 expression->base.type = type_left;
7007 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
7009 expression_t *const left = expression->left;
7010 expression_t *const right = expression->right;
7011 type_t *const orig_type_left = left->base.type;
7012 type_t *const orig_type_right = right->base.type;
7013 type_t *const type_left = skip_typeref(orig_type_left);
7014 type_t *const type_right = skip_typeref(orig_type_right);
7016 if (!is_valid_assignment_lhs(left))
7019 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7020 /* combined instructions are tricky. We can't create an implicit cast on
7021 * the left side, because we need the uncasted form for the store.
7022 * The ast2firm pass has to know that left_type must be right_type
7023 * for the arithmetic operation and create a cast by itself */
7024 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
7025 expression->right = create_implicit_cast(right, arithmetic_type);
7026 expression->base.type = type_left;
7027 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7028 check_pointer_arithmetic(&expression->base.source_position,
7029 type_left, orig_type_left);
7030 expression->base.type = type_left;
7031 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7032 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
7037 * Check the semantic restrictions of a logical expression.
7039 static void semantic_logical_op(binary_expression_t *expression)
7041 expression_t *const left = expression->left;
7042 expression_t *const right = expression->right;
7043 type_t *const orig_type_left = left->base.type;
7044 type_t *const orig_type_right = right->base.type;
7045 type_t *const type_left = skip_typeref(orig_type_left);
7046 type_t *const type_right = skip_typeref(orig_type_right);
7048 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7049 /* TODO: improve error message */
7050 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7051 errorf(HERE, "operation needs scalar types");
7056 expression->base.type = type_int;
7060 * Check the semantic restrictions of a binary assign expression.
7062 static void semantic_binexpr_assign(binary_expression_t *expression)
7064 expression_t *left = expression->left;
7065 type_t *orig_type_left = left->base.type;
7067 type_t *type_left = revert_automatic_type_conversion(left);
7068 type_left = skip_typeref(orig_type_left);
7070 if (!is_valid_assignment_lhs(left))
7073 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7074 report_assign_error(error, orig_type_left, expression->right,
7075 "assignment", &left->base.source_position);
7076 expression->right = create_implicit_cast(expression->right, orig_type_left);
7077 expression->base.type = orig_type_left;
7081 * Determine if the outermost operation (or parts thereof) of the given
7082 * expression has no effect in order to generate a warning about this fact.
7083 * Therefore in some cases this only examines some of the operands of the
7084 * expression (see comments in the function and examples below).
7086 * f() + 23; // warning, because + has no effect
7087 * x || f(); // no warning, because x controls execution of f()
7088 * x ? y : f(); // warning, because y has no effect
7089 * (void)x; // no warning to be able to suppress the warning
7090 * This function can NOT be used for an "expression has definitely no effect"-
7092 static bool expression_has_effect(const expression_t *const expr)
7094 switch (expr->kind) {
7095 case EXPR_UNKNOWN: break;
7096 case EXPR_INVALID: return true; /* do NOT warn */
7097 case EXPR_REFERENCE: return false;
7098 /* suppress the warning for microsoft __noop operations */
7099 case EXPR_CONST: return expr->conste.is_ms_noop;
7100 case EXPR_CHARACTER_CONSTANT: return false;
7101 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7102 case EXPR_STRING_LITERAL: return false;
7103 case EXPR_WIDE_STRING_LITERAL: return false;
7106 const call_expression_t *const call = &expr->call;
7107 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7110 switch (call->function->builtin_symbol.symbol->ID) {
7111 case T___builtin_va_end: return true;
7112 default: return false;
7116 /* Generate the warning if either the left or right hand side of a
7117 * conditional expression has no effect */
7118 case EXPR_CONDITIONAL: {
7119 const conditional_expression_t *const cond = &expr->conditional;
7121 expression_has_effect(cond->true_expression) &&
7122 expression_has_effect(cond->false_expression);
7125 case EXPR_SELECT: return false;
7126 case EXPR_ARRAY_ACCESS: return false;
7127 case EXPR_SIZEOF: return false;
7128 case EXPR_CLASSIFY_TYPE: return false;
7129 case EXPR_ALIGNOF: return false;
7131 case EXPR_FUNCNAME: return false;
7132 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7133 case EXPR_BUILTIN_CONSTANT_P: return false;
7134 case EXPR_BUILTIN_PREFETCH: return true;
7135 case EXPR_OFFSETOF: return false;
7136 case EXPR_VA_START: return true;
7137 case EXPR_VA_ARG: return true;
7138 case EXPR_STATEMENT: return true; // TODO
7139 case EXPR_COMPOUND_LITERAL: return false;
7141 case EXPR_UNARY_NEGATE: return false;
7142 case EXPR_UNARY_PLUS: return false;
7143 case EXPR_UNARY_BITWISE_NEGATE: return false;
7144 case EXPR_UNARY_NOT: return false;
7145 case EXPR_UNARY_DEREFERENCE: return false;
7146 case EXPR_UNARY_TAKE_ADDRESS: return false;
7147 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7148 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7149 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7150 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7152 /* Treat void casts as if they have an effect in order to being able to
7153 * suppress the warning */
7154 case EXPR_UNARY_CAST: {
7155 type_t *const type = skip_typeref(expr->base.type);
7156 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7159 case EXPR_UNARY_CAST_IMPLICIT: return true;
7160 case EXPR_UNARY_ASSUME: return true;
7162 case EXPR_BINARY_ADD: return false;
7163 case EXPR_BINARY_SUB: return false;
7164 case EXPR_BINARY_MUL: return false;
7165 case EXPR_BINARY_DIV: return false;
7166 case EXPR_BINARY_MOD: return false;
7167 case EXPR_BINARY_EQUAL: return false;
7168 case EXPR_BINARY_NOTEQUAL: return false;
7169 case EXPR_BINARY_LESS: return false;
7170 case EXPR_BINARY_LESSEQUAL: return false;
7171 case EXPR_BINARY_GREATER: return false;
7172 case EXPR_BINARY_GREATEREQUAL: return false;
7173 case EXPR_BINARY_BITWISE_AND: return false;
7174 case EXPR_BINARY_BITWISE_OR: return false;
7175 case EXPR_BINARY_BITWISE_XOR: return false;
7176 case EXPR_BINARY_SHIFTLEFT: return false;
7177 case EXPR_BINARY_SHIFTRIGHT: return false;
7178 case EXPR_BINARY_ASSIGN: return true;
7179 case EXPR_BINARY_MUL_ASSIGN: return true;
7180 case EXPR_BINARY_DIV_ASSIGN: return true;
7181 case EXPR_BINARY_MOD_ASSIGN: return true;
7182 case EXPR_BINARY_ADD_ASSIGN: return true;
7183 case EXPR_BINARY_SUB_ASSIGN: return true;
7184 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7185 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7186 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7187 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7188 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7190 /* Only examine the right hand side of && and ||, because the left hand
7191 * side already has the effect of controlling the execution of the right
7193 case EXPR_BINARY_LOGICAL_AND:
7194 case EXPR_BINARY_LOGICAL_OR:
7195 /* Only examine the right hand side of a comma expression, because the left
7196 * hand side has a separate warning */
7197 case EXPR_BINARY_COMMA:
7198 return expression_has_effect(expr->binary.right);
7200 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7201 case EXPR_BINARY_ISGREATER: return false;
7202 case EXPR_BINARY_ISGREATEREQUAL: return false;
7203 case EXPR_BINARY_ISLESS: return false;
7204 case EXPR_BINARY_ISLESSEQUAL: return false;
7205 case EXPR_BINARY_ISLESSGREATER: return false;
7206 case EXPR_BINARY_ISUNORDERED: return false;
7209 internal_errorf(HERE, "unexpected expression");
7212 static void semantic_comma(binary_expression_t *expression)
7214 if (warning.unused_value) {
7215 const expression_t *const left = expression->left;
7216 if (!expression_has_effect(left)) {
7217 warningf(&left->base.source_position,
7218 "left-hand operand of comma expression has no effect");
7221 expression->base.type = expression->right->base.type;
7224 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7225 static expression_t *parse_##binexpression_type(unsigned precedence, \
7226 expression_t *left) \
7229 source_position_t pos = *HERE; \
7231 expression_t *right = parse_sub_expression(precedence + lr); \
7233 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7234 binexpr->base.source_position = pos; \
7235 binexpr->binary.left = left; \
7236 binexpr->binary.right = right; \
7237 sfunc(&binexpr->binary); \
7242 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7243 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7244 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
7245 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
7246 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7247 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7248 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7249 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7250 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7252 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7253 semantic_comparison, 1)
7254 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7255 semantic_comparison, 1)
7256 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7257 semantic_comparison, 1)
7258 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7259 semantic_comparison, 1)
7261 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7262 semantic_binexpr_arithmetic, 1)
7263 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7264 semantic_binexpr_arithmetic, 1)
7265 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7266 semantic_binexpr_arithmetic, 1)
7267 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7268 semantic_logical_op, 1)
7269 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7270 semantic_logical_op, 1)
7271 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7272 semantic_shift_op, 1)
7273 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7274 semantic_shift_op, 1)
7275 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7276 semantic_arithmetic_addsubb_assign, 0)
7277 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7278 semantic_arithmetic_addsubb_assign, 0)
7279 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7280 semantic_arithmetic_assign, 0)
7281 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7282 semantic_arithmetic_assign, 0)
7283 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7284 semantic_arithmetic_assign, 0)
7285 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7286 semantic_arithmetic_assign, 0)
7287 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7288 semantic_arithmetic_assign, 0)
7289 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7290 semantic_arithmetic_assign, 0)
7291 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7292 semantic_arithmetic_assign, 0)
7293 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7294 semantic_arithmetic_assign, 0)
7296 static expression_t *parse_sub_expression(unsigned precedence)
7298 if (token.type < 0) {
7299 return expected_expression_error();
7302 expression_parser_function_t *parser
7303 = &expression_parsers[token.type];
7304 source_position_t source_position = token.source_position;
7307 if (parser->parser != NULL) {
7308 left = parser->parser(parser->precedence);
7310 left = parse_primary_expression();
7312 assert(left != NULL);
7313 left->base.source_position = source_position;
7316 if (token.type < 0) {
7317 return expected_expression_error();
7320 parser = &expression_parsers[token.type];
7321 if (parser->infix_parser == NULL)
7323 if (parser->infix_precedence < precedence)
7326 left = parser->infix_parser(parser->infix_precedence, left);
7328 assert(left != NULL);
7329 assert(left->kind != EXPR_UNKNOWN);
7330 left->base.source_position = source_position;
7337 * Parse an expression.
7339 static expression_t *parse_expression(void)
7341 return parse_sub_expression(1);
7345 * Register a parser for a prefix-like operator with given precedence.
7347 * @param parser the parser function
7348 * @param token_type the token type of the prefix token
7349 * @param precedence the precedence of the operator
7351 static void register_expression_parser(parse_expression_function parser,
7352 int token_type, unsigned precedence)
7354 expression_parser_function_t *entry = &expression_parsers[token_type];
7356 if (entry->parser != NULL) {
7357 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7358 panic("trying to register multiple expression parsers for a token");
7360 entry->parser = parser;
7361 entry->precedence = precedence;
7365 * Register a parser for an infix operator with given precedence.
7367 * @param parser the parser function
7368 * @param token_type the token type of the infix operator
7369 * @param precedence the precedence of the operator
7371 static void register_infix_parser(parse_expression_infix_function parser,
7372 int token_type, unsigned precedence)
7374 expression_parser_function_t *entry = &expression_parsers[token_type];
7376 if (entry->infix_parser != NULL) {
7377 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7378 panic("trying to register multiple infix expression parsers for a "
7381 entry->infix_parser = parser;
7382 entry->infix_precedence = precedence;
7386 * Initialize the expression parsers.
7388 static void init_expression_parsers(void)
7390 memset(&expression_parsers, 0, sizeof(expression_parsers));
7392 register_infix_parser(parse_array_expression, '[', 30);
7393 register_infix_parser(parse_call_expression, '(', 30);
7394 register_infix_parser(parse_select_expression, '.', 30);
7395 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
7396 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
7398 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
7401 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
7402 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
7403 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
7404 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
7405 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
7406 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
7407 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
7408 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
7409 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
7410 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
7411 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
7412 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
7413 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
7414 T_EXCLAMATIONMARKEQUAL, 13);
7415 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
7416 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
7417 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
7418 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
7419 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
7420 register_infix_parser(parse_conditional_expression, '?', 7);
7421 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
7422 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
7423 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
7424 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
7425 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
7426 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
7427 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
7428 T_LESSLESSEQUAL, 2);
7429 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7430 T_GREATERGREATEREQUAL, 2);
7431 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
7433 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
7435 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
7438 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
7440 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
7441 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
7442 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
7443 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
7444 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
7445 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
7446 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
7448 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
7450 register_expression_parser(parse_sizeof, T_sizeof, 25);
7451 register_expression_parser(parse_alignof, T___alignof__, 25);
7452 register_expression_parser(parse_extension, T___extension__, 25);
7453 register_expression_parser(parse_builtin_classify_type,
7454 T___builtin_classify_type, 25);
7458 * Parse a asm statement arguments specification.
7460 static asm_argument_t *parse_asm_arguments(bool is_out)
7462 asm_argument_t *result = NULL;
7463 asm_argument_t *last = NULL;
7465 while (token.type == T_STRING_LITERAL || token.type == '[') {
7466 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7467 memset(argument, 0, sizeof(argument[0]));
7469 if (token.type == '[') {
7471 if (token.type != T_IDENTIFIER) {
7472 parse_error_expected("while parsing asm argument",
7473 T_IDENTIFIER, NULL);
7476 argument->symbol = token.v.symbol;
7481 argument->constraints = parse_string_literals();
7483 add_anchor_token(')');
7484 expression_t *expression = parse_expression();
7485 rem_anchor_token(')');
7487 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
7488 * change size or type representation (e.g. int -> long is ok, but
7489 * int -> float is not) */
7490 if (expression->kind == EXPR_UNARY_CAST) {
7491 type_t *const type = expression->base.type;
7492 type_kind_t const kind = type->kind;
7493 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
7496 if (kind == TYPE_ATOMIC) {
7497 atomic_type_kind_t const akind = type->atomic.akind;
7498 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7499 size = get_atomic_type_size(akind);
7501 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7502 size = get_atomic_type_size(get_intptr_kind());
7506 expression_t *const value = expression->unary.value;
7507 type_t *const value_type = value->base.type;
7508 type_kind_t const value_kind = value_type->kind;
7510 unsigned value_flags;
7511 unsigned value_size;
7512 if (value_kind == TYPE_ATOMIC) {
7513 atomic_type_kind_t const value_akind = value_type->atomic.akind;
7514 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7515 value_size = get_atomic_type_size(value_akind);
7516 } else if (value_kind == TYPE_POINTER) {
7517 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7518 value_size = get_atomic_type_size(get_intptr_kind());
7523 if (value_flags != flags || value_size != size)
7527 } while (expression->kind == EXPR_UNARY_CAST);
7531 if (!is_lvalue(expression)) {
7532 errorf(&expression->base.source_position,
7533 "asm output argument is not an lvalue");
7536 argument->expression = expression;
7539 set_address_taken(expression, true);
7542 last->next = argument;
7548 if (token.type != ',')
7559 * Parse a asm statement clobber specification.
7561 static asm_clobber_t *parse_asm_clobbers(void)
7563 asm_clobber_t *result = NULL;
7564 asm_clobber_t *last = NULL;
7566 while(token.type == T_STRING_LITERAL) {
7567 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
7568 clobber->clobber = parse_string_literals();
7571 last->next = clobber;
7577 if (token.type != ',')
7586 * Parse an asm statement.
7588 static statement_t *parse_asm_statement(void)
7592 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
7593 statement->base.source_position = token.source_position;
7595 asm_statement_t *asm_statement = &statement->asms;
7597 if (token.type == T_volatile) {
7599 asm_statement->is_volatile = true;
7603 add_anchor_token(')');
7604 add_anchor_token(':');
7605 asm_statement->asm_text = parse_string_literals();
7607 if (token.type != ':') {
7608 rem_anchor_token(':');
7613 asm_statement->outputs = parse_asm_arguments(true);
7614 if (token.type != ':') {
7615 rem_anchor_token(':');
7620 asm_statement->inputs = parse_asm_arguments(false);
7621 if (token.type != ':') {
7622 rem_anchor_token(':');
7625 rem_anchor_token(':');
7628 asm_statement->clobbers = parse_asm_clobbers();
7631 rem_anchor_token(')');
7635 if (asm_statement->outputs == NULL) {
7636 /* GCC: An 'asm' instruction without any output operands will be treated
7637 * identically to a volatile 'asm' instruction. */
7638 asm_statement->is_volatile = true;
7643 return create_invalid_statement();
7647 * Parse a case statement.
7649 static statement_t *parse_case_statement(void)
7653 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7655 statement->base.source_position = token.source_position;
7656 statement->case_label.expression = parse_expression();
7658 if (c_mode & _GNUC) {
7659 if (token.type == T_DOTDOTDOT) {
7661 statement->case_label.end_range = parse_expression();
7667 if (! is_constant_expression(statement->case_label.expression)) {
7668 errorf(&statement->base.source_position,
7669 "case label does not reduce to an integer constant");
7671 /* TODO: check if the case label is already known */
7672 if (current_switch != NULL) {
7673 /* link all cases into the switch statement */
7674 if (current_switch->last_case == NULL) {
7675 current_switch->first_case =
7676 current_switch->last_case = &statement->case_label;
7678 current_switch->last_case->next = &statement->case_label;
7681 errorf(&statement->base.source_position,
7682 "case label not within a switch statement");
7685 statement->case_label.statement = parse_statement();
7689 return create_invalid_statement();
7693 * Finds an existing default label of a switch statement.
7695 static case_label_statement_t *
7696 find_default_label(const switch_statement_t *statement)
7698 case_label_statement_t *label = statement->first_case;
7699 for ( ; label != NULL; label = label->next) {
7700 if (label->expression == NULL)
7707 * Parse a default statement.
7709 static statement_t *parse_default_statement(void)
7713 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7715 statement->base.source_position = token.source_position;
7718 if (current_switch != NULL) {
7719 const case_label_statement_t *def_label = find_default_label(current_switch);
7720 if (def_label != NULL) {
7721 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7722 &def_label->base.source_position);
7724 /* link all cases into the switch statement */
7725 if (current_switch->last_case == NULL) {
7726 current_switch->first_case =
7727 current_switch->last_case = &statement->case_label;
7729 current_switch->last_case->next = &statement->case_label;
7733 errorf(&statement->base.source_position,
7734 "'default' label not within a switch statement");
7736 statement->case_label.statement = parse_statement();
7740 return create_invalid_statement();
7744 * Return the declaration for a given label symbol or create a new one.
7746 * @param symbol the symbol of the label
7748 static declaration_t *get_label(symbol_t *symbol)
7750 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7751 assert(current_function != NULL);
7752 /* if we found a label in the same function, then we already created the
7754 if (candidate != NULL
7755 && candidate->parent_scope == ¤t_function->scope) {
7759 /* otherwise we need to create a new one */
7760 declaration_t *const declaration = allocate_declaration_zero();
7761 declaration->namespc = NAMESPACE_LABEL;
7762 declaration->symbol = symbol;
7764 label_push(declaration);
7770 * Parse a label statement.
7772 static statement_t *parse_label_statement(void)
7774 assert(token.type == T_IDENTIFIER);
7775 symbol_t *symbol = token.v.symbol;
7778 declaration_t *label = get_label(symbol);
7780 /* if source position is already set then the label is defined twice,
7781 * otherwise it was just mentioned in a goto so far */
7782 if (label->source_position.input_name != NULL) {
7783 errorf(HERE, "duplicate label '%Y' (declared %P)",
7784 symbol, &label->source_position);
7786 label->source_position = token.source_position;
7789 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7791 statement->base.source_position = token.source_position;
7792 statement->label.label = label;
7796 if (token.type == '}') {
7797 /* TODO only warn? */
7799 warningf(HERE, "label at end of compound statement");
7800 statement->label.statement = create_empty_statement();
7802 errorf(HERE, "label at end of compound statement");
7803 statement->label.statement = create_invalid_statement();
7805 } else if (token.type == ';') {
7806 /* Eat an empty statement here, to avoid the warning about an empty
7807 * statement after a label. label:; is commonly used to have a label
7808 * before a closing brace. */
7809 statement->label.statement = create_empty_statement();
7812 statement->label.statement = parse_statement();
7815 /* remember the labels in a list for later checking */
7816 if (label_last == NULL) {
7817 label_first = &statement->label;
7819 label_last->next = &statement->label;
7821 label_last = &statement->label;
7827 * Parse an if statement.
7829 static statement_t *parse_if(void)
7833 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7834 statement->base.source_position = token.source_position;
7837 add_anchor_token(')');
7838 statement->ifs.condition = parse_expression();
7839 rem_anchor_token(')');
7842 add_anchor_token(T_else);
7843 statement->ifs.true_statement = parse_statement();
7844 rem_anchor_token(T_else);
7846 if (token.type == T_else) {
7848 statement->ifs.false_statement = parse_statement();
7853 return create_invalid_statement();
7857 * Parse a switch statement.
7859 static statement_t *parse_switch(void)
7863 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7864 statement->base.source_position = token.source_position;
7867 expression_t *const expr = parse_expression();
7868 type_t * type = skip_typeref(expr->base.type);
7869 if (is_type_integer(type)) {
7870 type = promote_integer(type);
7871 } else if (is_type_valid(type)) {
7872 errorf(&expr->base.source_position,
7873 "switch quantity is not an integer, but '%T'", type);
7874 type = type_error_type;
7876 statement->switchs.expression = create_implicit_cast(expr, type);
7879 switch_statement_t *rem = current_switch;
7880 current_switch = &statement->switchs;
7881 statement->switchs.body = parse_statement();
7882 current_switch = rem;
7884 if (warning.switch_default &&
7885 find_default_label(&statement->switchs) == NULL) {
7886 warningf(&statement->base.source_position, "switch has no default case");
7891 return create_invalid_statement();
7894 static statement_t *parse_loop_body(statement_t *const loop)
7896 statement_t *const rem = current_loop;
7897 current_loop = loop;
7899 statement_t *const body = parse_statement();
7906 * Parse a while statement.
7908 static statement_t *parse_while(void)
7912 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7913 statement->base.source_position = token.source_position;
7916 add_anchor_token(')');
7917 statement->whiles.condition = parse_expression();
7918 rem_anchor_token(')');
7921 statement->whiles.body = parse_loop_body(statement);
7925 return create_invalid_statement();
7929 * Parse a do statement.
7931 static statement_t *parse_do(void)
7935 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7937 statement->base.source_position = token.source_position;
7939 add_anchor_token(T_while);
7940 statement->do_while.body = parse_loop_body(statement);
7941 rem_anchor_token(T_while);
7945 add_anchor_token(')');
7946 statement->do_while.condition = parse_expression();
7947 rem_anchor_token(')');
7953 return create_invalid_statement();
7957 * Parse a for statement.
7959 static statement_t *parse_for(void)
7963 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7964 statement->base.source_position = token.source_position;
7966 int top = environment_top();
7967 scope_t *last_scope = scope;
7968 set_scope(&statement->fors.scope);
7971 add_anchor_token(')');
7973 if (token.type != ';') {
7974 if (is_declaration_specifier(&token, false)) {
7975 parse_declaration(record_declaration);
7977 add_anchor_token(';');
7978 expression_t *const init = parse_expression();
7979 statement->fors.initialisation = init;
7980 if (warning.unused_value && !expression_has_effect(init)) {
7981 warningf(&init->base.source_position,
7982 "initialisation of 'for'-statement has no effect");
7984 rem_anchor_token(';');
7991 if (token.type != ';') {
7992 add_anchor_token(';');
7993 statement->fors.condition = parse_expression();
7994 rem_anchor_token(';');
7997 if (token.type != ')') {
7998 expression_t *const step = parse_expression();
7999 statement->fors.step = step;
8000 if (warning.unused_value && !expression_has_effect(step)) {
8001 warningf(&step->base.source_position,
8002 "step of 'for'-statement has no effect");
8005 rem_anchor_token(')');
8007 statement->fors.body = parse_loop_body(statement);
8009 assert(scope == &statement->fors.scope);
8010 set_scope(last_scope);
8011 environment_pop_to(top);
8016 rem_anchor_token(')');
8017 assert(scope == &statement->fors.scope);
8018 set_scope(last_scope);
8019 environment_pop_to(top);
8021 return create_invalid_statement();
8025 * Parse a goto statement.
8027 static statement_t *parse_goto(void)
8031 if (token.type != T_IDENTIFIER) {
8032 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8036 symbol_t *symbol = token.v.symbol;
8039 declaration_t *label = get_label(symbol);
8041 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8042 statement->base.source_position = token.source_position;
8044 statement->gotos.label = label;
8046 /* remember the goto's in a list for later checking */
8047 if (goto_last == NULL) {
8048 goto_first = &statement->gotos;
8050 goto_last->next = &statement->gotos;
8052 goto_last = &statement->gotos;
8058 return create_invalid_statement();
8062 * Parse a continue statement.
8064 static statement_t *parse_continue(void)
8066 statement_t *statement;
8067 if (current_loop == NULL) {
8068 errorf(HERE, "continue statement not within loop");
8069 statement = create_invalid_statement();
8071 statement = allocate_statement_zero(STATEMENT_CONTINUE);
8073 statement->base.source_position = token.source_position;
8081 return create_invalid_statement();
8085 * Parse a break statement.
8087 static statement_t *parse_break(void)
8089 statement_t *statement;
8090 if (current_switch == NULL && current_loop == NULL) {
8091 errorf(HERE, "break statement not within loop or switch");
8092 statement = create_invalid_statement();
8094 statement = allocate_statement_zero(STATEMENT_BREAK);
8096 statement->base.source_position = token.source_position;
8104 return create_invalid_statement();
8108 * Parse a __leave statement.
8110 static statement_t *parse_leave(void)
8112 statement_t *statement;
8113 if (current_try == NULL) {
8114 errorf(HERE, "__leave statement not within __try");
8115 statement = create_invalid_statement();
8117 statement = allocate_statement_zero(STATEMENT_LEAVE);
8119 statement->base.source_position = token.source_position;
8127 return create_invalid_statement();
8131 * Check if a given declaration represents a local variable.
8133 static bool is_local_var_declaration(const declaration_t *declaration)
8135 switch ((storage_class_tag_t) declaration->storage_class) {
8136 case STORAGE_CLASS_AUTO:
8137 case STORAGE_CLASS_REGISTER: {
8138 const type_t *type = skip_typeref(declaration->type);
8139 if (is_type_function(type)) {
8151 * Check if a given declaration represents a variable.
8153 static bool is_var_declaration(const declaration_t *declaration)
8155 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8158 const type_t *type = skip_typeref(declaration->type);
8159 return !is_type_function(type);
8163 * Check if a given expression represents a local variable.
8165 static bool is_local_variable(const expression_t *expression)
8167 if (expression->base.kind != EXPR_REFERENCE) {
8170 const declaration_t *declaration = expression->reference.declaration;
8171 return is_local_var_declaration(declaration);
8175 * Check if a given expression represents a local variable and
8176 * return its declaration then, else return NULL.
8178 declaration_t *expr_is_variable(const expression_t *expression)
8180 if (expression->base.kind != EXPR_REFERENCE) {
8183 declaration_t *declaration = expression->reference.declaration;
8184 if (is_var_declaration(declaration))
8190 * Parse a return statement.
8192 static statement_t *parse_return(void)
8194 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8195 statement->base.source_position = token.source_position;
8199 expression_t *return_value = NULL;
8200 if (token.type != ';') {
8201 return_value = parse_expression();
8205 const type_t *const func_type = current_function->type;
8206 assert(is_type_function(func_type));
8207 type_t *const return_type = skip_typeref(func_type->function.return_type);
8209 if (return_value != NULL) {
8210 type_t *return_value_type = skip_typeref(return_value->base.type);
8212 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8213 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8214 warningf(&statement->base.source_position,
8215 "'return' with a value, in function returning void");
8216 return_value = NULL;
8218 assign_error_t error = semantic_assign(return_type, return_value);
8219 report_assign_error(error, return_type, return_value, "'return'",
8220 &statement->base.source_position);
8221 return_value = create_implicit_cast(return_value, return_type);
8223 /* check for returning address of a local var */
8224 if (return_value != NULL &&
8225 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
8226 const expression_t *expression = return_value->unary.value;
8227 if (is_local_variable(expression)) {
8228 warningf(&statement->base.source_position,
8229 "function returns address of local variable");
8233 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
8234 warningf(&statement->base.source_position,
8235 "'return' without value, in function returning non-void");
8238 statement->returns.value = return_value;
8242 return create_invalid_statement();
8246 * Parse a declaration statement.
8248 static statement_t *parse_declaration_statement(void)
8250 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
8252 statement->base.source_position = token.source_position;
8254 declaration_t *before = last_declaration;
8255 parse_declaration(record_declaration);
8257 if (before == NULL) {
8258 statement->declaration.declarations_begin = scope->declarations;
8260 statement->declaration.declarations_begin = before->next;
8262 statement->declaration.declarations_end = last_declaration;
8268 * Parse an expression statement, ie. expr ';'.
8270 static statement_t *parse_expression_statement(void)
8272 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
8274 statement->base.source_position = token.source_position;
8275 expression_t *const expr = parse_expression();
8276 statement->expression.expression = expr;
8282 return create_invalid_statement();
8286 * Parse a microsoft __try { } __finally { } or
8287 * __try{ } __except() { }
8289 static statement_t *parse_ms_try_statment(void)
8291 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
8293 statement->base.source_position = token.source_position;
8296 ms_try_statement_t *rem = current_try;
8297 current_try = &statement->ms_try;
8298 statement->ms_try.try_statement = parse_compound_statement(false);
8301 if (token.type == T___except) {
8304 add_anchor_token(')');
8305 expression_t *const expr = parse_expression();
8306 type_t * type = skip_typeref(expr->base.type);
8307 if (is_type_integer(type)) {
8308 type = promote_integer(type);
8309 } else if (is_type_valid(type)) {
8310 errorf(&expr->base.source_position,
8311 "__expect expression is not an integer, but '%T'", type);
8312 type = type_error_type;
8314 statement->ms_try.except_expression = create_implicit_cast(expr, type);
8315 rem_anchor_token(')');
8317 statement->ms_try.final_statement = parse_compound_statement(false);
8318 } else if (token.type == T__finally) {
8320 statement->ms_try.final_statement = parse_compound_statement(false);
8322 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
8323 return create_invalid_statement();
8327 return create_invalid_statement();
8330 static statement_t *parse_empty_statement(void)
8332 if (warning.empty_statement) {
8333 warningf(HERE, "statement is empty");
8336 return create_empty_statement();
8340 * Parse a statement.
8341 * There's also parse_statement() which additionally checks for
8342 * "statement has no effect" warnings
8344 static statement_t *intern_parse_statement(void)
8346 statement_t *statement = NULL;
8348 /* declaration or statement */
8349 add_anchor_token(';');
8350 switch (token.type) {
8352 if (look_ahead(1)->type == ':') {
8353 statement = parse_label_statement();
8354 } else if (is_typedef_symbol(token.v.symbol)) {
8355 statement = parse_declaration_statement();
8357 statement = parse_expression_statement();
8361 case T___extension__:
8362 /* This can be a prefix to a declaration or an expression statement.
8363 * We simply eat it now and parse the rest with tail recursion. */
8366 } while (token.type == T___extension__);
8367 statement = parse_statement();
8371 statement = parse_declaration_statement();
8374 case ';': statement = parse_empty_statement(); break;
8375 case '{': statement = parse_compound_statement(false); break;
8376 case T___leave: statement = parse_leave(); break;
8377 case T___try: statement = parse_ms_try_statment(); break;
8378 case T_asm: statement = parse_asm_statement(); break;
8379 case T_break: statement = parse_break(); break;
8380 case T_case: statement = parse_case_statement(); break;
8381 case T_continue: statement = parse_continue(); break;
8382 case T_default: statement = parse_default_statement(); break;
8383 case T_do: statement = parse_do(); break;
8384 case T_for: statement = parse_for(); break;
8385 case T_goto: statement = parse_goto(); break;
8386 case T_if: statement = parse_if (); break;
8387 case T_return: statement = parse_return(); break;
8388 case T_switch: statement = parse_switch(); break;
8389 case T_while: statement = parse_while(); break;
8390 default: statement = parse_expression_statement(); break;
8392 rem_anchor_token(';');
8394 assert(statement != NULL
8395 && statement->base.source_position.input_name != NULL);
8401 * parse a statement and emits "statement has no effect" warning if needed
8402 * (This is really a wrapper around intern_parse_statement with check for 1
8403 * single warning. It is needed, because for statement expressions we have
8404 * to avoid the warning on the last statement)
8406 static statement_t *parse_statement(void)
8408 statement_t *statement = intern_parse_statement();
8410 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
8411 expression_t *expression = statement->expression.expression;
8412 if (!expression_has_effect(expression)) {
8413 warningf(&expression->base.source_position,
8414 "statement has no effect");
8422 * Parse a compound statement.
8424 static statement_t *parse_compound_statement(bool inside_expression_statement)
8426 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
8428 statement->base.source_position = token.source_position;
8431 add_anchor_token('}');
8433 int top = environment_top();
8434 scope_t *last_scope = scope;
8435 set_scope(&statement->compound.scope);
8437 statement_t *last_statement = NULL;
8439 while (token.type != '}' && token.type != T_EOF) {
8440 statement_t *sub_statement = intern_parse_statement();
8441 if (is_invalid_statement(sub_statement)) {
8442 /* an error occurred. if we are at an anchor, return */
8448 if (last_statement != NULL) {
8449 last_statement->base.next = sub_statement;
8451 statement->compound.statements = sub_statement;
8454 while (sub_statement->base.next != NULL)
8455 sub_statement = sub_statement->base.next;
8457 last_statement = sub_statement;
8460 if (token.type == '}') {
8463 errorf(&statement->base.source_position,
8464 "end of file while looking for closing '}'");
8467 /* look over all statements again to produce no effect warnings */
8468 if (warning.unused_value) {
8469 statement_t *sub_statement = statement->compound.statements;
8470 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
8471 if (sub_statement->kind != STATEMENT_EXPRESSION)
8473 /* don't emit a warning for the last expression in an expression
8474 * statement as it has always an effect */
8475 if (inside_expression_statement && sub_statement->base.next == NULL)
8478 expression_t *expression = sub_statement->expression.expression;
8479 if (!expression_has_effect(expression)) {
8480 warningf(&expression->base.source_position,
8481 "statement has no effect");
8487 rem_anchor_token('}');
8488 assert(scope == &statement->compound.scope);
8489 set_scope(last_scope);
8490 environment_pop_to(top);
8496 * Initialize builtin types.
8498 static void initialize_builtin_types(void)
8500 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
8501 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
8502 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
8503 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
8504 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
8505 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
8506 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
8507 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
8509 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
8510 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
8511 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
8512 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
8516 * Check for unused global static functions and variables
8518 static void check_unused_globals(void)
8520 if (!warning.unused_function && !warning.unused_variable)
8523 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
8525 decl->modifiers & DM_UNUSED ||
8526 decl->modifiers & DM_USED ||
8527 decl->storage_class != STORAGE_CLASS_STATIC)
8530 type_t *const type = decl->type;
8532 if (is_type_function(skip_typeref(type))) {
8533 if (!warning.unused_function || decl->is_inline)
8536 s = (decl->init.statement != NULL ? "defined" : "declared");
8538 if (!warning.unused_variable)
8544 warningf(&decl->source_position, "'%#T' %s but not used",
8545 type, decl->symbol, s);
8549 static void parse_global_asm(void)
8554 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8555 statement->base.source_position = token.source_position;
8556 statement->asms.asm_text = parse_string_literals();
8557 statement->base.next = unit->global_asm;
8558 unit->global_asm = statement;
8567 * Parse a translation unit.
8569 static void parse_translation_unit(void)
8571 while (token.type != T_EOF) {
8572 switch (token.type) {
8574 /* TODO error in strict mode */
8575 warningf(HERE, "stray ';' outside of function");
8584 parse_external_declaration();
8593 * @return the translation unit or NULL if errors occurred.
8595 void start_parsing(void)
8597 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8598 label_stack = NEW_ARR_F(stack_entry_t, 0);
8599 diagnostic_count = 0;
8603 type_set_output(stderr);
8604 ast_set_output(stderr);
8606 assert(unit == NULL);
8607 unit = allocate_ast_zero(sizeof(unit[0]));
8609 assert(global_scope == NULL);
8610 global_scope = &unit->scope;
8612 assert(scope == NULL);
8613 set_scope(&unit->scope);
8615 initialize_builtin_types();
8618 translation_unit_t *finish_parsing(void)
8620 assert(scope == &unit->scope);
8622 last_declaration = NULL;
8624 assert(global_scope == &unit->scope);
8625 check_unused_globals();
8626 global_scope = NULL;
8628 DEL_ARR_F(environment_stack);
8629 DEL_ARR_F(label_stack);
8631 translation_unit_t *result = unit;
8638 lookahead_bufpos = 0;
8639 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8642 parse_translation_unit();
8646 * Initialize the parser.
8648 void init_parser(void)
8651 /* add predefined symbols for extended-decl-modifier */
8652 sym_align = symbol_table_insert("align");
8653 sym_allocate = symbol_table_insert("allocate");
8654 sym_dllimport = symbol_table_insert("dllimport");
8655 sym_dllexport = symbol_table_insert("dllexport");
8656 sym_naked = symbol_table_insert("naked");
8657 sym_noinline = symbol_table_insert("noinline");
8658 sym_noreturn = symbol_table_insert("noreturn");
8659 sym_nothrow = symbol_table_insert("nothrow");
8660 sym_novtable = symbol_table_insert("novtable");
8661 sym_property = symbol_table_insert("property");
8662 sym_get = symbol_table_insert("get");
8663 sym_put = symbol_table_insert("put");
8664 sym_selectany = symbol_table_insert("selectany");
8665 sym_thread = symbol_table_insert("thread");
8666 sym_uuid = symbol_table_insert("uuid");
8667 sym_deprecated = symbol_table_insert("deprecated");
8668 sym_restrict = symbol_table_insert("restrict");
8669 sym_noalias = symbol_table_insert("noalias");
8671 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8673 init_expression_parsers();
8674 obstack_init(&temp_obst);
8676 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8677 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8681 * Terminate the parser.
8683 void exit_parser(void)
8685 obstack_free(&temp_obst, NULL);