2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct argument_list_t argument_list_t;
52 struct argument_list_t {
54 argument_list_t *next;
57 typedef struct gnu_attribute_t gnu_attribute_t;
58 struct gnu_attribute_t {
59 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
60 gnu_attribute_t *next;
61 bool invalid; /**< Set if this attribute had argument errors, */
62 bool have_arguments; /**< True, if this attribute has arguments. */
66 atomic_type_kind_t akind;
67 long argument; /**< Single argument. */
68 argument_list_t *arguments; /**< List of argument expressions. */
72 typedef struct declaration_specifiers_t declaration_specifiers_t;
73 struct declaration_specifiers_t {
74 source_position_t source_position;
75 unsigned char declared_storage_class;
76 unsigned char alignment; /**< Alignment, 0 if not set. */
77 unsigned int is_inline : 1;
78 unsigned int deprecated : 1;
79 decl_modifiers_t modifiers; /**< declaration modifiers */
80 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
81 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
82 symbol_t *get_property_sym; /**< the name of the get property if set. */
83 symbol_t *put_property_sym; /**< the name of the put property if set. */
88 * An environment for parsing initializers (and compound literals).
90 typedef struct parse_initializer_env_t {
91 type_t *type; /**< the type of the initializer. In case of an
92 array type with unspecified size this gets
93 adjusted to the actual size. */
94 declaration_t *declaration; /**< the declaration that is initialized if any */
95 bool must_be_constant;
96 } parse_initializer_env_t;
98 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
100 static token_t token;
101 static token_t lookahead_buffer[MAX_LOOKAHEAD];
102 static int lookahead_bufpos;
103 static stack_entry_t *environment_stack = NULL;
104 static stack_entry_t *label_stack = NULL;
105 static scope_t *global_scope = NULL;
106 static scope_t *scope = NULL;
107 static declaration_t *last_declaration = NULL;
108 static declaration_t *current_function = NULL;
109 static switch_statement_t *current_switch = NULL;
110 static statement_t *current_loop = NULL;
111 static ms_try_statement_t *current_try = NULL;
112 static goto_statement_t *goto_first = NULL;
113 static goto_statement_t *goto_last = NULL;
114 static label_statement_t *label_first = NULL;
115 static label_statement_t *label_last = NULL;
116 static translation_unit_t *unit = NULL;
117 static struct obstack temp_obst;
119 static source_position_t null_position = { NULL, 0 };
121 /* symbols for Microsoft extended-decl-modifier */
122 static const symbol_t *sym_align = NULL;
123 static const symbol_t *sym_allocate = NULL;
124 static const symbol_t *sym_dllimport = NULL;
125 static const symbol_t *sym_dllexport = NULL;
126 static const symbol_t *sym_naked = NULL;
127 static const symbol_t *sym_noinline = NULL;
128 static const symbol_t *sym_noreturn = NULL;
129 static const symbol_t *sym_nothrow = NULL;
130 static const symbol_t *sym_novtable = NULL;
131 static const symbol_t *sym_property = NULL;
132 static const symbol_t *sym_get = NULL;
133 static const symbol_t *sym_put = NULL;
134 static const symbol_t *sym_selectany = NULL;
135 static const symbol_t *sym_thread = NULL;
136 static const symbol_t *sym_uuid = NULL;
137 static const symbol_t *sym_deprecated = NULL;
138 static const symbol_t *sym_restrict = NULL;
139 static const symbol_t *sym_noalias = NULL;
141 /** The token anchor set */
142 static unsigned char token_anchor_set[T_LAST_TOKEN];
144 /** The current source position. */
145 #define HERE (&token.source_position)
147 static type_t *type_valist;
149 static statement_t *parse_compound_statement(bool inside_expression_statement);
150 static statement_t *parse_statement(void);
152 static expression_t *parse_sub_expression(unsigned precedence);
153 static expression_t *parse_expression(void);
154 static type_t *parse_typename(void);
156 static void parse_compound_type_entries(declaration_t *compound_declaration);
157 static declaration_t *parse_declarator(
158 const declaration_specifiers_t *specifiers, bool may_be_abstract);
159 static declaration_t *record_declaration(declaration_t *declaration);
161 static void semantic_comparison(binary_expression_t *expression);
163 #define STORAGE_CLASSES \
170 #define TYPE_QUALIFIERS \
175 case T__forceinline: \
176 case T___attribute__:
178 #ifdef PROVIDE_COMPLEX
179 #define COMPLEX_SPECIFIERS \
181 #define IMAGINARY_SPECIFIERS \
184 #define COMPLEX_SPECIFIERS
185 #define IMAGINARY_SPECIFIERS
188 #define TYPE_SPECIFIERS \
203 case T___builtin_va_list: \
208 #define DECLARATION_START \
213 #define TYPENAME_START \
218 * Allocate an AST node with given size and
219 * initialize all fields with zero.
221 static void *allocate_ast_zero(size_t size)
223 void *res = allocate_ast(size);
224 memset(res, 0, size);
228 static declaration_t *allocate_declaration_zero(void)
230 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
231 declaration->type = type_error_type;
232 declaration->alignment = 0;
237 * Returns the size of a statement node.
239 * @param kind the statement kind
241 static size_t get_statement_struct_size(statement_kind_t kind)
243 static const size_t sizes[] = {
244 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
245 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
246 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
247 [STATEMENT_RETURN] = sizeof(return_statement_t),
248 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
249 [STATEMENT_IF] = sizeof(if_statement_t),
250 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
251 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
252 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
253 [STATEMENT_BREAK] = sizeof(statement_base_t),
254 [STATEMENT_GOTO] = sizeof(goto_statement_t),
255 [STATEMENT_LABEL] = sizeof(label_statement_t),
256 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
257 [STATEMENT_WHILE] = sizeof(while_statement_t),
258 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
259 [STATEMENT_FOR] = sizeof(for_statement_t),
260 [STATEMENT_ASM] = sizeof(asm_statement_t),
261 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
262 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
264 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
265 assert(sizes[kind] != 0);
270 * Returns the size of an expression node.
272 * @param kind the expression kind
274 static size_t get_expression_struct_size(expression_kind_t kind)
276 static const size_t sizes[] = {
277 [EXPR_INVALID] = sizeof(expression_base_t),
278 [EXPR_REFERENCE] = sizeof(reference_expression_t),
279 [EXPR_CONST] = sizeof(const_expression_t),
280 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
281 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
282 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
283 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
284 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
285 [EXPR_CALL] = sizeof(call_expression_t),
286 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
287 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
288 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
289 [EXPR_SELECT] = sizeof(select_expression_t),
290 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
291 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
292 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
293 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
294 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
295 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
296 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
297 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
298 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
299 [EXPR_VA_START] = sizeof(va_start_expression_t),
300 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
301 [EXPR_STATEMENT] = sizeof(statement_expression_t),
303 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
304 return sizes[EXPR_UNARY_FIRST];
306 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
307 return sizes[EXPR_BINARY_FIRST];
309 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
310 assert(sizes[kind] != 0);
315 * Allocate a statement node of given kind and initialize all
318 static statement_t *allocate_statement_zero(statement_kind_t kind)
320 size_t size = get_statement_struct_size(kind);
321 statement_t *res = allocate_ast_zero(size);
323 res->base.kind = kind;
328 * Allocate an expression node of given kind and initialize all
331 static expression_t *allocate_expression_zero(expression_kind_t kind)
333 size_t size = get_expression_struct_size(kind);
334 expression_t *res = allocate_ast_zero(size);
336 res->base.kind = kind;
337 res->base.type = type_error_type;
342 * Creates a new invalid expression.
344 static expression_t *create_invalid_expression(void)
346 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
347 expression->base.source_position = token.source_position;
352 * Creates a new invalid statement.
354 static statement_t *create_invalid_statement(void)
356 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
357 statement->base.source_position = token.source_position;
362 * Allocate a new empty statement.
364 static statement_t *create_empty_statement(void)
366 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
367 statement->base.source_position = token.source_position;
372 * Returns the size of a type node.
374 * @param kind the type kind
376 static size_t get_type_struct_size(type_kind_t kind)
378 static const size_t sizes[] = {
379 [TYPE_ATOMIC] = sizeof(atomic_type_t),
380 [TYPE_COMPLEX] = sizeof(complex_type_t),
381 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
382 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
383 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
384 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
385 [TYPE_ENUM] = sizeof(enum_type_t),
386 [TYPE_FUNCTION] = sizeof(function_type_t),
387 [TYPE_POINTER] = sizeof(pointer_type_t),
388 [TYPE_ARRAY] = sizeof(array_type_t),
389 [TYPE_BUILTIN] = sizeof(builtin_type_t),
390 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
391 [TYPE_TYPEOF] = sizeof(typeof_type_t),
393 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
394 assert(kind <= TYPE_TYPEOF);
395 assert(sizes[kind] != 0);
400 * Allocate a type node of given kind and initialize all
403 * @param kind type kind to allocate
404 * @param source_position the source position of the type definition
406 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
408 size_t size = get_type_struct_size(kind);
409 type_t *res = obstack_alloc(type_obst, size);
410 memset(res, 0, size);
412 res->base.kind = kind;
413 res->base.source_position = *source_position;
418 * Returns the size of an initializer node.
420 * @param kind the initializer kind
422 static size_t get_initializer_size(initializer_kind_t kind)
424 static const size_t sizes[] = {
425 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
426 [INITIALIZER_STRING] = sizeof(initializer_string_t),
427 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
428 [INITIALIZER_LIST] = sizeof(initializer_list_t),
429 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
431 assert(kind < sizeof(sizes) / sizeof(*sizes));
432 assert(sizes[kind] != 0);
437 * Allocate an initializer node of given kind and initialize all
440 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
442 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
449 * Free a type from the type obstack.
451 static void free_type(void *type)
453 obstack_free(type_obst, type);
457 * Returns the index of the top element of the environment stack.
459 static size_t environment_top(void)
461 return ARR_LEN(environment_stack);
465 * Returns the index of the top element of the label stack.
467 static size_t label_top(void)
469 return ARR_LEN(label_stack);
473 * Return the next token.
475 static inline void next_token(void)
477 token = lookahead_buffer[lookahead_bufpos];
478 lookahead_buffer[lookahead_bufpos] = lexer_token;
481 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
484 print_token(stderr, &token);
485 fprintf(stderr, "\n");
490 * Return the next token with a given lookahead.
492 static inline const token_t *look_ahead(int num)
494 assert(num > 0 && num <= MAX_LOOKAHEAD);
495 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
496 return &lookahead_buffer[pos];
500 * Adds a token to the token anchor set (a multi-set).
502 static void add_anchor_token(int token_type)
504 assert(0 <= token_type && token_type < T_LAST_TOKEN);
505 ++token_anchor_set[token_type];
508 static int save_and_reset_anchor_state(int token_type)
510 assert(0 <= token_type && token_type < T_LAST_TOKEN);
511 int count = token_anchor_set[token_type];
512 token_anchor_set[token_type] = 0;
516 static void restore_anchor_state(int token_type, int count)
518 assert(0 <= token_type && token_type < T_LAST_TOKEN);
519 token_anchor_set[token_type] = count;
523 * Remove a token from the token anchor set (a multi-set).
525 static void rem_anchor_token(int token_type)
527 assert(0 <= token_type && token_type < T_LAST_TOKEN);
528 --token_anchor_set[token_type];
531 static bool at_anchor(void)
535 return token_anchor_set[token.type];
539 * Eat tokens until a matching token is found.
541 static void eat_until_matching_token(int type)
545 case '(': end_token = ')'; break;
546 case '{': end_token = '}'; break;
547 case '[': end_token = ']'; break;
548 default: end_token = type; break;
551 unsigned parenthesis_count = 0;
552 unsigned brace_count = 0;
553 unsigned bracket_count = 0;
554 while (token.type != end_token ||
555 parenthesis_count != 0 ||
557 bracket_count != 0) {
558 switch (token.type) {
560 case '(': ++parenthesis_count; break;
561 case '{': ++brace_count; break;
562 case '[': ++bracket_count; break;
565 if (parenthesis_count > 0)
575 if (bracket_count > 0)
578 if (token.type == end_token &&
579 parenthesis_count == 0 &&
593 * Eat input tokens until an anchor is found.
595 static void eat_until_anchor(void)
597 if (token.type == T_EOF)
599 while (token_anchor_set[token.type] == 0) {
600 if (token.type == '(' || token.type == '{' || token.type == '[')
601 eat_until_matching_token(token.type);
602 if (token.type == T_EOF)
608 static void eat_block(void)
610 eat_until_matching_token('{');
611 if (token.type == '}')
616 * eat all token until a ';' is reached or a stop token is found.
618 static void eat_statement(void)
620 eat_until_matching_token(';');
621 if (token.type == ';')
625 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
628 * Report a parse error because an expected token was not found.
631 #if defined __GNUC__ && __GNUC__ >= 4
632 __attribute__((sentinel))
634 void parse_error_expected(const char *message, ...)
636 if (message != NULL) {
637 errorf(HERE, "%s", message);
640 va_start(ap, message);
641 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
646 * Report a type error.
648 static void type_error(const char *msg, const source_position_t *source_position,
651 errorf(source_position, "%s, but found type '%T'", msg, type);
655 * Report an incompatible type.
657 static void type_error_incompatible(const char *msg,
658 const source_position_t *source_position, type_t *type1, type_t *type2)
660 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
665 * Expect the the current token is the expected token.
666 * If not, generate an error, eat the current statement,
667 * and goto the end_error label.
669 #define expect(expected) \
671 if (UNLIKELY(token.type != (expected))) { \
672 parse_error_expected(NULL, (expected), NULL); \
673 add_anchor_token(expected); \
674 eat_until_anchor(); \
675 if (token.type == expected) \
677 rem_anchor_token(expected); \
683 static void set_scope(scope_t *new_scope)
686 scope->last_declaration = last_declaration;
690 last_declaration = new_scope->last_declaration;
694 * Search a symbol in a given namespace and returns its declaration or
695 * NULL if this symbol was not found.
697 static declaration_t *get_declaration(const symbol_t *const symbol,
698 const namespace_t namespc)
700 declaration_t *declaration = symbol->declaration;
701 for( ; declaration != NULL; declaration = declaration->symbol_next) {
702 if (declaration->namespc == namespc)
710 * pushs an environment_entry on the environment stack and links the
711 * corresponding symbol to the new entry
713 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
715 symbol_t *symbol = declaration->symbol;
716 namespace_t namespc = (namespace_t) declaration->namespc;
718 /* replace/add declaration into declaration list of the symbol */
719 declaration_t *iter = symbol->declaration;
721 symbol->declaration = declaration;
723 declaration_t *iter_last = NULL;
724 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
725 /* replace an entry? */
726 if (iter->namespc == namespc) {
727 if (iter_last == NULL) {
728 symbol->declaration = declaration;
730 iter_last->symbol_next = declaration;
732 declaration->symbol_next = iter->symbol_next;
737 assert(iter_last->symbol_next == NULL);
738 iter_last->symbol_next = declaration;
742 /* remember old declaration */
744 entry.symbol = symbol;
745 entry.old_declaration = iter;
746 entry.namespc = (unsigned short) namespc;
747 ARR_APP1(stack_entry_t, *stack_ptr, entry);
750 static void environment_push(declaration_t *declaration)
752 assert(declaration->source_position.input_name != NULL);
753 assert(declaration->parent_scope != NULL);
754 stack_push(&environment_stack, declaration);
758 * Push a declaration of the label stack.
760 * @param declaration the declaration
762 static void label_push(declaration_t *declaration)
764 declaration->parent_scope = ¤t_function->scope;
765 stack_push(&label_stack, declaration);
769 * pops symbols from the environment stack until @p new_top is the top element
771 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
773 stack_entry_t *stack = *stack_ptr;
774 size_t top = ARR_LEN(stack);
777 assert(new_top <= top);
781 for(i = top; i > new_top; --i) {
782 stack_entry_t *entry = &stack[i - 1];
784 declaration_t *old_declaration = entry->old_declaration;
785 symbol_t *symbol = entry->symbol;
786 namespace_t namespc = (namespace_t)entry->namespc;
788 /* replace/remove declaration */
789 declaration_t *declaration = symbol->declaration;
790 assert(declaration != NULL);
791 if (declaration->namespc == namespc) {
792 if (old_declaration == NULL) {
793 symbol->declaration = declaration->symbol_next;
795 symbol->declaration = old_declaration;
798 declaration_t *iter_last = declaration;
799 declaration_t *iter = declaration->symbol_next;
800 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
801 /* replace an entry? */
802 if (iter->namespc == namespc) {
803 assert(iter_last != NULL);
804 iter_last->symbol_next = old_declaration;
805 if (old_declaration != NULL) {
806 old_declaration->symbol_next = iter->symbol_next;
811 assert(iter != NULL);
815 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
818 static void environment_pop_to(size_t new_top)
820 stack_pop_to(&environment_stack, new_top);
824 * Pop all entries on the label stack until the new_top
827 * @param new_top the new stack top
829 static void label_pop_to(size_t new_top)
831 stack_pop_to(&label_stack, new_top);
835 static int get_rank(const type_t *type)
837 assert(!is_typeref(type));
838 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
839 * and esp. footnote 108). However we can't fold constants (yet), so we
840 * can't decide whether unsigned int is possible, while int always works.
841 * (unsigned int would be preferable when possible... for stuff like
842 * struct { enum { ... } bla : 4; } ) */
843 if (type->kind == TYPE_ENUM)
844 return ATOMIC_TYPE_INT;
846 assert(type->kind == TYPE_ATOMIC);
847 return type->atomic.akind;
850 static type_t *promote_integer(type_t *type)
852 if (type->kind == TYPE_BITFIELD)
853 type = type->bitfield.base_type;
855 if (get_rank(type) < ATOMIC_TYPE_INT)
862 * Create a cast expression.
864 * @param expression the expression to cast
865 * @param dest_type the destination type
867 static expression_t *create_cast_expression(expression_t *expression,
870 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
872 cast->unary.value = expression;
873 cast->base.type = dest_type;
879 * Check if a given expression represents the 0 pointer constant.
881 static bool is_null_pointer_constant(const expression_t *expression)
883 /* skip void* cast */
884 if (expression->kind == EXPR_UNARY_CAST
885 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
886 expression = expression->unary.value;
889 /* TODO: not correct yet, should be any constant integer expression
890 * which evaluates to 0 */
891 if (expression->kind != EXPR_CONST)
894 type_t *const type = skip_typeref(expression->base.type);
895 if (!is_type_integer(type))
898 return expression->conste.v.int_value == 0;
902 * Create an implicit cast expression.
904 * @param expression the expression to cast
905 * @param dest_type the destination type
907 static expression_t *create_implicit_cast(expression_t *expression,
910 type_t *const source_type = expression->base.type;
912 if (source_type == dest_type)
915 return create_cast_expression(expression, dest_type);
918 typedef enum assign_error_t {
920 ASSIGN_ERROR_INCOMPATIBLE,
921 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
922 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
923 ASSIGN_WARNING_POINTER_FROM_INT,
924 ASSIGN_WARNING_INT_FROM_POINTER
927 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
928 const expression_t *const right,
930 const source_position_t *source_position)
932 type_t *const orig_type_right = right->base.type;
933 type_t *const type_left = skip_typeref(orig_type_left);
934 type_t *const type_right = skip_typeref(orig_type_right);
939 case ASSIGN_ERROR_INCOMPATIBLE:
940 errorf(source_position,
941 "destination type '%T' in %s is incompatible with type '%T'",
942 orig_type_left, context, orig_type_right);
945 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
946 type_t *points_to_left
947 = skip_typeref(type_left->pointer.points_to);
948 type_t *points_to_right
949 = skip_typeref(type_right->pointer.points_to);
951 /* the left type has all qualifiers from the right type */
952 unsigned missing_qualifiers
953 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
954 errorf(source_position,
955 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type",
956 orig_type_left, context, orig_type_right, missing_qualifiers);
960 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
961 warningf(source_position,
962 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
963 orig_type_left, context, right, orig_type_right);
966 case ASSIGN_WARNING_POINTER_FROM_INT:
967 warningf(source_position,
968 "%s makes integer '%T' from pointer '%T' without a cast",
969 context, orig_type_left, orig_type_right);
972 case ASSIGN_WARNING_INT_FROM_POINTER:
973 warningf(source_position,
974 "%s makes integer '%T' from pointer '%T' without a cast",
975 context, orig_type_left, orig_type_right);
979 panic("invalid error value");
983 /** Implements the rules from § 6.5.16.1 */
984 static assign_error_t semantic_assign(type_t *orig_type_left,
985 const expression_t *const right)
987 type_t *const orig_type_right = right->base.type;
988 type_t *const type_left = skip_typeref(orig_type_left);
989 type_t *const type_right = skip_typeref(orig_type_right);
991 if (is_type_pointer(type_left)) {
992 if (is_null_pointer_constant(right)) {
993 return ASSIGN_SUCCESS;
994 } else if (is_type_pointer(type_right)) {
995 type_t *points_to_left
996 = skip_typeref(type_left->pointer.points_to);
997 type_t *points_to_right
998 = skip_typeref(type_right->pointer.points_to);
1000 /* the left type has all qualifiers from the right type */
1001 unsigned missing_qualifiers
1002 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1003 if (missing_qualifiers != 0) {
1004 return ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1007 points_to_left = get_unqualified_type(points_to_left);
1008 points_to_right = get_unqualified_type(points_to_right);
1010 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1011 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1012 return ASSIGN_SUCCESS;
1015 if (!types_compatible(points_to_left, points_to_right)) {
1016 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1019 return ASSIGN_SUCCESS;
1020 } else if (is_type_integer(type_right)) {
1021 return ASSIGN_WARNING_POINTER_FROM_INT;
1023 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1024 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1025 && is_type_pointer(type_right))) {
1026 return ASSIGN_SUCCESS;
1027 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1028 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1029 type_t *const unqual_type_left = get_unqualified_type(type_left);
1030 type_t *const unqual_type_right = get_unqualified_type(type_right);
1031 if (types_compatible(unqual_type_left, unqual_type_right)) {
1032 return ASSIGN_SUCCESS;
1034 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1035 return ASSIGN_WARNING_INT_FROM_POINTER;
1038 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1039 return ASSIGN_SUCCESS;
1041 return ASSIGN_ERROR_INCOMPATIBLE;
1044 static expression_t *parse_constant_expression(void)
1046 /* start parsing at precedence 7 (conditional expression) */
1047 expression_t *result = parse_sub_expression(7);
1049 if (!is_constant_expression(result)) {
1050 errorf(&result->base.source_position,
1051 "expression '%E' is not constant\n", result);
1057 static expression_t *parse_assignment_expression(void)
1059 /* start parsing at precedence 2 (assignment expression) */
1060 return parse_sub_expression(2);
1063 static type_t *make_global_typedef(const char *name, type_t *type)
1065 symbol_t *const symbol = symbol_table_insert(name);
1067 declaration_t *const declaration = allocate_declaration_zero();
1068 declaration->namespc = NAMESPACE_NORMAL;
1069 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1070 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1071 declaration->type = type;
1072 declaration->symbol = symbol;
1073 declaration->source_position = builtin_source_position;
1075 record_declaration(declaration);
1077 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1078 typedef_type->typedeft.declaration = declaration;
1080 return typedef_type;
1083 static string_t parse_string_literals(void)
1085 assert(token.type == T_STRING_LITERAL);
1086 string_t result = token.v.string;
1090 while (token.type == T_STRING_LITERAL) {
1091 result = concat_strings(&result, &token.v.string);
1098 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1099 [GNU_AK_CONST] = "const",
1100 [GNU_AK_VOLATILE] = "volatile",
1101 [GNU_AK_CDECL] = "cdecl",
1102 [GNU_AK_STDCALL] = "stdcall",
1103 [GNU_AK_FASTCALL] = "fastcall",
1104 [GNU_AK_DEPRECATED] = "deprecated",
1105 [GNU_AK_NOINLINE] = "noinline",
1106 [GNU_AK_NORETURN] = "noreturn",
1107 [GNU_AK_NAKED] = "naked",
1108 [GNU_AK_PURE] = "pure",
1109 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1110 [GNU_AK_MALLOC] = "malloc",
1111 [GNU_AK_WEAK] = "weak",
1112 [GNU_AK_CONSTRUCTOR] = "constructor",
1113 [GNU_AK_DESTRUCTOR] = "destructor",
1114 [GNU_AK_NOTHROW] = "nothrow",
1115 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1116 [GNU_AK_COMMON] = "common",
1117 [GNU_AK_NOCOMMON] = "nocommon",
1118 [GNU_AK_PACKED] = "packed",
1119 [GNU_AK_SHARED] = "shared",
1120 [GNU_AK_NOTSHARED] = "notshared",
1121 [GNU_AK_USED] = "used",
1122 [GNU_AK_UNUSED] = "unused",
1123 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1124 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1125 [GNU_AK_LONGCALL] = "longcall",
1126 [GNU_AK_SHORTCALL] = "shortcall",
1127 [GNU_AK_LONG_CALL] = "long_call",
1128 [GNU_AK_SHORT_CALL] = "short_call",
1129 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1130 [GNU_AK_INTERRUPT] = "interrupt",
1131 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1132 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1133 [GNU_AK_NESTING] = "nesting",
1134 [GNU_AK_NEAR] = "near",
1135 [GNU_AK_FAR] = "far",
1136 [GNU_AK_SIGNAL] = "signal",
1137 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1138 [GNU_AK_TINY_DATA] = "tiny_data",
1139 [GNU_AK_SAVEALL] = "saveall",
1140 [GNU_AK_FLATTEN] = "flatten",
1141 [GNU_AK_SSEREGPARM] = "sseregparm",
1142 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1143 [GNU_AK_RETURN_TWICE] = "return_twice",
1144 [GNU_AK_MAY_ALIAS] = "may_alias",
1145 [GNU_AK_MS_STRUCT] = "ms_struct",
1146 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1147 [GNU_AK_DLLIMPORT] = "dllimport",
1148 [GNU_AK_DLLEXPORT] = "dllexport",
1149 [GNU_AK_ALIGNED] = "aligned",
1150 [GNU_AK_ALIAS] = "alias",
1151 [GNU_AK_SECTION] = "section",
1152 [GNU_AK_FORMAT] = "format",
1153 [GNU_AK_FORMAT_ARG] = "format_arg",
1154 [GNU_AK_WEAKREF] = "weakref",
1155 [GNU_AK_NONNULL] = "nonnull",
1156 [GNU_AK_TLS_MODEL] = "tls_model",
1157 [GNU_AK_VISIBILITY] = "visibility",
1158 [GNU_AK_REGPARM] = "regparm",
1159 [GNU_AK_MODE] = "mode",
1160 [GNU_AK_MODEL] = "model",
1161 [GNU_AK_TRAP_EXIT] = "trap_exit",
1162 [GNU_AK_SP_SWITCH] = "sp_switch",
1163 [GNU_AK_SENTINEL] = "sentinel"
1167 * compare two string, ignoring double underscores on the second.
1169 static int strcmp_underscore(const char *s1, const char *s2)
1171 if (s2[0] == '_' && s2[1] == '_') {
1172 size_t len2 = strlen(s2);
1173 size_t len1 = strlen(s1);
1174 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1175 return strncmp(s1, s2+2, len2-4);
1179 return strcmp(s1, s2);
1183 * Allocate a new gnu temporal attribute.
1185 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1187 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1188 attribute->kind = kind;
1189 attribute->next = NULL;
1190 attribute->invalid = false;
1191 attribute->have_arguments = false;
1197 * parse one constant expression argument.
1199 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1201 expression_t *expression;
1202 add_anchor_token(')');
1203 expression = parse_constant_expression();
1204 rem_anchor_token(')');
1206 attribute->u.argument = fold_constant(expression);
1209 attribute->invalid = true;
1213 * parse a list of constant expressions arguments.
1215 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1217 argument_list_t **list = &attribute->u.arguments;
1218 argument_list_t *entry;
1219 expression_t *expression;
1220 add_anchor_token(')');
1221 add_anchor_token(',');
1223 expression = parse_constant_expression();
1224 entry = obstack_alloc(&temp_obst, sizeof(entry));
1225 entry->argument = fold_constant(expression);
1228 list = &entry->next;
1229 if (token.type != ',')
1233 rem_anchor_token(',');
1234 rem_anchor_token(')');
1238 attribute->invalid = true;
1242 * parse one string literal argument.
1244 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1247 add_anchor_token('(');
1248 if (token.type != T_STRING_LITERAL) {
1249 parse_error_expected("while parsing attribute directive",
1250 T_STRING_LITERAL, NULL);
1253 *string = parse_string_literals();
1254 rem_anchor_token('(');
1258 attribute->invalid = true;
1262 * parse one tls model.
1264 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1266 static const char *const tls_models[] = {
1272 string_t string = { NULL, 0 };
1273 parse_gnu_attribute_string_arg(attribute, &string);
1274 if (string.begin != NULL) {
1275 for(size_t i = 0; i < 4; ++i) {
1276 if (strcmp(tls_models[i], string.begin) == 0) {
1277 attribute->u.value = i;
1281 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1283 attribute->invalid = true;
1287 * parse one tls model.
1289 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1291 static const char *const visibilities[] = {
1297 string_t string = { NULL, 0 };
1298 parse_gnu_attribute_string_arg(attribute, &string);
1299 if (string.begin != NULL) {
1300 for(size_t i = 0; i < 4; ++i) {
1301 if (strcmp(visibilities[i], string.begin) == 0) {
1302 attribute->u.value = i;
1306 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1308 attribute->invalid = true;
1312 * parse one (code) model.
1314 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1316 static const char *const visibilities[] = {
1321 string_t string = { NULL, 0 };
1322 parse_gnu_attribute_string_arg(attribute, &string);
1323 if (string.begin != NULL) {
1324 for(int i = 0; i < 3; ++i) {
1325 if (strcmp(visibilities[i], string.begin) == 0) {
1326 attribute->u.value = i;
1330 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1332 attribute->invalid = true;
1335 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1337 /* TODO: find out what is allowed here... */
1339 /* at least: byte, word, pointer, list of machine modes
1340 * __XXX___ is interpreted as XXX */
1341 add_anchor_token(')');
1343 if (token.type != T_IDENTIFIER) {
1344 expect(T_IDENTIFIER);
1347 /* This isn't really correct, the backend should provide a list of machine
1348 * specific modes (according to gcc philosophy that is...) */
1349 const char *symbol_str = token.v.symbol->string;
1350 if (strcmp_underscore("QI", symbol_str) == 0 ||
1351 strcmp_underscore("byte", symbol_str) == 0) {
1352 attribute->u.akind = ATOMIC_TYPE_CHAR;
1353 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1354 attribute->u.akind = ATOMIC_TYPE_SHORT;
1355 } else if (strcmp_underscore("SI", symbol_str) == 0
1356 || strcmp_underscore("word", symbol_str) == 0
1357 || strcmp_underscore("pointer", symbol_str) == 0) {
1358 attribute->u.akind = ATOMIC_TYPE_INT;
1359 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1360 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1362 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1363 attribute->invalid = true;
1367 rem_anchor_token(')');
1371 attribute->invalid = true;
1375 * parse one interrupt argument.
1377 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1379 static const char *const interrupts[] = {
1386 string_t string = { NULL, 0 };
1387 parse_gnu_attribute_string_arg(attribute, &string);
1388 if (string.begin != NULL) {
1389 for(size_t i = 0; i < 5; ++i) {
1390 if (strcmp(interrupts[i], string.begin) == 0) {
1391 attribute->u.value = i;
1395 errorf(HERE, "'%s' is not an interrupt", string.begin);
1397 attribute->invalid = true;
1401 * parse ( identifier, const expression, const expression )
1403 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1405 static const char *const format_names[] = {
1413 if (token.type != T_IDENTIFIER) {
1414 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1417 const char *name = token.v.symbol->string;
1418 for(i = 0; i < 4; ++i) {
1419 if (strcmp_underscore(format_names[i], name) == 0)
1423 if (warning.attribute)
1424 warningf(HERE, "'%s' is an unrecognized format function type", name);
1429 add_anchor_token(')');
1430 add_anchor_token(',');
1431 parse_constant_expression();
1432 rem_anchor_token(',');
1433 rem_anchor_token('(');
1436 add_anchor_token(')');
1437 parse_constant_expression();
1438 rem_anchor_token('(');
1442 attribute->u.value = true;
1445 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1447 if (!attribute->have_arguments)
1450 /* should have no arguments */
1451 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1452 eat_until_matching_token('(');
1453 /* we have already consumed '(', so we stop before ')', eat it */
1455 attribute->invalid = true;
1459 * Parse one GNU attribute.
1461 * Note that attribute names can be specified WITH or WITHOUT
1462 * double underscores, ie const or __const__.
1464 * The following attributes are parsed without arguments
1489 * no_instrument_function
1490 * warn_unused_result
1507 * externally_visible
1515 * The following attributes are parsed with arguments
1516 * aligned( const expression )
1517 * alias( string literal )
1518 * section( string literal )
1519 * format( identifier, const expression, const expression )
1520 * format_arg( const expression )
1521 * tls_model( string literal )
1522 * visibility( string literal )
1523 * regparm( const expression )
1524 * model( string leteral )
1525 * trap_exit( const expression )
1526 * sp_switch( string literal )
1528 * The following attributes might have arguments
1529 * weak_ref( string literal )
1530 * non_null( const expression // ',' )
1531 * interrupt( string literal )
1532 * sentinel( constant expression )
1534 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1536 gnu_attribute_t *head = *attributes;
1537 gnu_attribute_t *last = *attributes;
1538 decl_modifiers_t modifiers = 0;
1539 gnu_attribute_t *attribute;
1541 eat(T___attribute__);
1545 if (token.type != ')') {
1546 /* find the end of the list */
1548 while (last->next != NULL)
1552 /* non-empty attribute list */
1555 if (token.type == T_const) {
1557 } else if (token.type == T_volatile) {
1559 } else if (token.type == T_cdecl) {
1560 /* __attribute__((cdecl)), WITH ms mode */
1562 } else if (token.type == T_IDENTIFIER) {
1563 const symbol_t *sym = token.v.symbol;
1566 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1573 for(i = 0; i < GNU_AK_LAST; ++i) {
1574 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1577 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1580 if (kind == GNU_AK_LAST) {
1581 if (warning.attribute)
1582 warningf(HERE, "'%s' attribute directive ignored", name);
1584 /* skip possible arguments */
1585 if (token.type == '(') {
1586 eat_until_matching_token(')');
1589 /* check for arguments */
1590 attribute = allocate_gnu_attribute(kind);
1591 if (token.type == '(') {
1593 if (token.type == ')') {
1594 /* empty args are allowed */
1597 attribute->have_arguments = true;
1602 case GNU_AK_VOLATILE:
1603 case GNU_AK_DEPRECATED:
1608 case GNU_AK_NOCOMMON:
1610 case GNU_AK_NOTSHARED:
1611 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1612 case GNU_AK_WARN_UNUSED_RESULT:
1613 case GNU_AK_LONGCALL:
1614 case GNU_AK_SHORTCALL:
1615 case GNU_AK_LONG_CALL:
1616 case GNU_AK_SHORT_CALL:
1617 case GNU_AK_FUNCTION_VECTOR:
1618 case GNU_AK_INTERRUPT_HANDLER:
1619 case GNU_AK_NMI_HANDLER:
1620 case GNU_AK_NESTING:
1624 case GNU_AK_EIGTHBIT_DATA:
1625 case GNU_AK_TINY_DATA:
1626 case GNU_AK_SAVEALL:
1627 case GNU_AK_FLATTEN:
1628 case GNU_AK_SSEREGPARM:
1629 case GNU_AK_EXTERNALLY_VISIBLE:
1630 case GNU_AK_RETURN_TWICE:
1631 case GNU_AK_MAY_ALIAS:
1632 case GNU_AK_MS_STRUCT:
1633 case GNU_AK_GCC_STRUCT:
1636 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1637 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1638 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1639 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1640 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1641 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1642 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1643 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1644 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1645 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1646 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1647 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1648 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1649 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1650 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1651 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1653 case GNU_AK_ALIGNED:
1654 /* __align__ may be used without an argument */
1655 if (attribute->have_arguments) {
1656 parse_gnu_attribute_const_arg(attribute);
1660 case GNU_AK_FORMAT_ARG:
1661 case GNU_AK_REGPARM:
1662 case GNU_AK_TRAP_EXIT:
1663 if (!attribute->have_arguments) {
1664 /* should have arguments */
1665 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1666 attribute->invalid = true;
1668 parse_gnu_attribute_const_arg(attribute);
1671 case GNU_AK_SECTION:
1672 case GNU_AK_SP_SWITCH:
1673 if (!attribute->have_arguments) {
1674 /* should have arguments */
1675 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1676 attribute->invalid = true;
1678 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1681 if (!attribute->have_arguments) {
1682 /* should have arguments */
1683 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1684 attribute->invalid = true;
1686 parse_gnu_attribute_format_args(attribute);
1688 case GNU_AK_WEAKREF:
1689 /* may have one string argument */
1690 if (attribute->have_arguments)
1691 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1693 case GNU_AK_NONNULL:
1694 if (attribute->have_arguments)
1695 parse_gnu_attribute_const_arg_list(attribute);
1697 case GNU_AK_TLS_MODEL:
1698 if (!attribute->have_arguments) {
1699 /* should have arguments */
1700 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1702 parse_gnu_attribute_tls_model_arg(attribute);
1704 case GNU_AK_VISIBILITY:
1705 if (!attribute->have_arguments) {
1706 /* should have arguments */
1707 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1709 parse_gnu_attribute_visibility_arg(attribute);
1712 if (!attribute->have_arguments) {
1713 /* should have arguments */
1714 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1716 parse_gnu_attribute_model_arg(attribute);
1720 if (!attribute->have_arguments) {
1721 /* should have arguments */
1722 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1724 parse_gnu_attribute_mode_arg(attribute);
1727 case GNU_AK_INTERRUPT:
1728 /* may have one string argument */
1729 if (attribute->have_arguments)
1730 parse_gnu_attribute_interrupt_arg(attribute);
1732 case GNU_AK_SENTINEL:
1733 /* may have one string argument */
1734 if (attribute->have_arguments)
1735 parse_gnu_attribute_const_arg(attribute);
1738 /* already handled */
1742 check_no_argument(attribute, name);
1745 if (attribute != NULL) {
1747 last->next = attribute;
1750 head = last = attribute;
1754 if (token.type != ',')
1768 * Parse GNU attributes.
1770 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1772 decl_modifiers_t modifiers = 0;
1775 switch(token.type) {
1776 case T___attribute__:
1777 modifiers |= parse_gnu_attribute(attributes);
1783 if (token.type != T_STRING_LITERAL) {
1784 parse_error_expected("while parsing assembler attribute",
1785 T_STRING_LITERAL, NULL);
1786 eat_until_matching_token('(');
1789 parse_string_literals();
1794 case T_cdecl: modifiers |= DM_CDECL; break;
1795 case T__fastcall: modifiers |= DM_FASTCALL; break;
1796 case T__stdcall: modifiers |= DM_STDCALL; break;
1799 /* TODO record modifier */
1800 warningf(HERE, "Ignoring declaration modifier %K", &token);
1804 default: return modifiers;
1811 static designator_t *parse_designation(void)
1813 designator_t *result = NULL;
1814 designator_t *last = NULL;
1817 designator_t *designator;
1818 switch(token.type) {
1820 designator = allocate_ast_zero(sizeof(designator[0]));
1821 designator->source_position = token.source_position;
1823 add_anchor_token(']');
1824 designator->array_index = parse_constant_expression();
1825 rem_anchor_token(']');
1829 designator = allocate_ast_zero(sizeof(designator[0]));
1830 designator->source_position = token.source_position;
1832 if (token.type != T_IDENTIFIER) {
1833 parse_error_expected("while parsing designator",
1834 T_IDENTIFIER, NULL);
1837 designator->symbol = token.v.symbol;
1845 assert(designator != NULL);
1847 last->next = designator;
1849 result = designator;
1857 static initializer_t *initializer_from_string(array_type_t *type,
1858 const string_t *const string)
1860 /* TODO: check len vs. size of array type */
1863 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1864 initializer->string.string = *string;
1869 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1870 wide_string_t *const string)
1872 /* TODO: check len vs. size of array type */
1875 initializer_t *const initializer =
1876 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1877 initializer->wide_string.string = *string;
1883 * Build an initializer from a given expression.
1885 static initializer_t *initializer_from_expression(type_t *orig_type,
1886 expression_t *expression)
1888 /* TODO check that expression is a constant expression */
1890 /* § 6.7.8.14/15 char array may be initialized by string literals */
1891 type_t *type = skip_typeref(orig_type);
1892 type_t *expr_type_orig = expression->base.type;
1893 type_t *expr_type = skip_typeref(expr_type_orig);
1894 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1895 array_type_t *const array_type = &type->array;
1896 type_t *const element_type = skip_typeref(array_type->element_type);
1898 if (element_type->kind == TYPE_ATOMIC) {
1899 atomic_type_kind_t akind = element_type->atomic.akind;
1900 switch (expression->kind) {
1901 case EXPR_STRING_LITERAL:
1902 if (akind == ATOMIC_TYPE_CHAR
1903 || akind == ATOMIC_TYPE_SCHAR
1904 || akind == ATOMIC_TYPE_UCHAR) {
1905 return initializer_from_string(array_type,
1906 &expression->string.value);
1909 case EXPR_WIDE_STRING_LITERAL: {
1910 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1911 if (get_unqualified_type(element_type) == bare_wchar_type) {
1912 return initializer_from_wide_string(array_type,
1913 &expression->wide_string.value);
1923 assign_error_t error = semantic_assign(type, expression);
1924 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1926 report_assign_error(error, type, expression, "initializer",
1927 &expression->base.source_position);
1929 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1930 result->value.value = create_implicit_cast(expression, type);
1936 * Checks if a given expression can be used as an constant initializer.
1938 static bool is_initializer_constant(const expression_t *expression)
1940 return is_constant_expression(expression)
1941 || is_address_constant(expression);
1945 * Parses an scalar initializer.
1947 * § 6.7.8.11; eat {} without warning
1949 static initializer_t *parse_scalar_initializer(type_t *type,
1950 bool must_be_constant)
1952 /* there might be extra {} hierarchies */
1954 if (token.type == '{') {
1955 warningf(HERE, "extra curly braces around scalar initializer");
1959 } while (token.type == '{');
1962 expression_t *expression = parse_assignment_expression();
1963 if (must_be_constant && !is_initializer_constant(expression)) {
1964 errorf(&expression->base.source_position,
1965 "Initialisation expression '%E' is not constant\n",
1969 initializer_t *initializer = initializer_from_expression(type, expression);
1971 if (initializer == NULL) {
1972 errorf(&expression->base.source_position,
1973 "expression '%E' (type '%T') doesn't match expected type '%T'",
1974 expression, expression->base.type, type);
1979 bool additional_warning_displayed = false;
1980 while (braces > 0) {
1981 if (token.type == ',') {
1984 if (token.type != '}') {
1985 if (!additional_warning_displayed) {
1986 warningf(HERE, "additional elements in scalar initializer");
1987 additional_warning_displayed = true;
1998 * An entry in the type path.
2000 typedef struct type_path_entry_t type_path_entry_t;
2001 struct type_path_entry_t {
2002 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2004 size_t index; /**< For array types: the current index. */
2005 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2010 * A type path expression a position inside compound or array types.
2012 typedef struct type_path_t type_path_t;
2013 struct type_path_t {
2014 type_path_entry_t *path; /**< An flexible array containing the current path. */
2015 type_t *top_type; /**< type of the element the path points */
2016 size_t max_index; /**< largest index in outermost array */
2020 * Prints a type path for debugging.
2022 static __attribute__((unused)) void debug_print_type_path(
2023 const type_path_t *path)
2025 size_t len = ARR_LEN(path->path);
2027 for(size_t i = 0; i < len; ++i) {
2028 const type_path_entry_t *entry = & path->path[i];
2030 type_t *type = skip_typeref(entry->type);
2031 if (is_type_compound(type)) {
2032 /* in gcc mode structs can have no members */
2033 if (entry->v.compound_entry == NULL) {
2037 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2038 } else if (is_type_array(type)) {
2039 fprintf(stderr, "[%zu]", entry->v.index);
2041 fprintf(stderr, "-INVALID-");
2044 if (path->top_type != NULL) {
2045 fprintf(stderr, " (");
2046 print_type(path->top_type);
2047 fprintf(stderr, ")");
2052 * Return the top type path entry, ie. in a path
2053 * (type).a.b returns the b.
2055 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2057 size_t len = ARR_LEN(path->path);
2059 return &path->path[len-1];
2063 * Enlarge the type path by an (empty) element.
2065 static type_path_entry_t *append_to_type_path(type_path_t *path)
2067 size_t len = ARR_LEN(path->path);
2068 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2070 type_path_entry_t *result = & path->path[len];
2071 memset(result, 0, sizeof(result[0]));
2076 * Descending into a sub-type. Enter the scope of the current
2079 static void descend_into_subtype(type_path_t *path)
2081 type_t *orig_top_type = path->top_type;
2082 type_t *top_type = skip_typeref(orig_top_type);
2084 assert(is_type_compound(top_type) || is_type_array(top_type));
2086 type_path_entry_t *top = append_to_type_path(path);
2087 top->type = top_type;
2089 if (is_type_compound(top_type)) {
2090 declaration_t *declaration = top_type->compound.declaration;
2091 declaration_t *entry = declaration->scope.declarations;
2092 top->v.compound_entry = entry;
2094 if (entry != NULL) {
2095 path->top_type = entry->type;
2097 path->top_type = NULL;
2100 assert(is_type_array(top_type));
2103 path->top_type = top_type->array.element_type;
2108 * Pop an entry from the given type path, ie. returning from
2109 * (type).a.b to (type).a
2111 static void ascend_from_subtype(type_path_t *path)
2113 type_path_entry_t *top = get_type_path_top(path);
2115 path->top_type = top->type;
2117 size_t len = ARR_LEN(path->path);
2118 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2122 * Pop entries from the given type path until the given
2123 * path level is reached.
2125 static void ascend_to(type_path_t *path, size_t top_path_level)
2127 size_t len = ARR_LEN(path->path);
2129 while (len > top_path_level) {
2130 ascend_from_subtype(path);
2131 len = ARR_LEN(path->path);
2135 static bool walk_designator(type_path_t *path, const designator_t *designator,
2136 bool used_in_offsetof)
2138 for( ; designator != NULL; designator = designator->next) {
2139 type_path_entry_t *top = get_type_path_top(path);
2140 type_t *orig_type = top->type;
2142 type_t *type = skip_typeref(orig_type);
2144 if (designator->symbol != NULL) {
2145 symbol_t *symbol = designator->symbol;
2146 if (!is_type_compound(type)) {
2147 if (is_type_valid(type)) {
2148 errorf(&designator->source_position,
2149 "'.%Y' designator used for non-compound type '%T'",
2155 declaration_t *declaration = type->compound.declaration;
2156 declaration_t *iter = declaration->scope.declarations;
2157 for( ; iter != NULL; iter = iter->next) {
2158 if (iter->symbol == symbol) {
2163 errorf(&designator->source_position,
2164 "'%T' has no member named '%Y'", orig_type, symbol);
2167 if (used_in_offsetof) {
2168 type_t *real_type = skip_typeref(iter->type);
2169 if (real_type->kind == TYPE_BITFIELD) {
2170 errorf(&designator->source_position,
2171 "offsetof designator '%Y' may not specify bitfield",
2177 top->type = orig_type;
2178 top->v.compound_entry = iter;
2179 orig_type = iter->type;
2181 expression_t *array_index = designator->array_index;
2182 assert(designator->array_index != NULL);
2184 if (!is_type_array(type)) {
2185 if (is_type_valid(type)) {
2186 errorf(&designator->source_position,
2187 "[%E] designator used for non-array type '%T'",
2188 array_index, orig_type);
2192 if (!is_type_valid(array_index->base.type)) {
2196 long index = fold_constant(array_index);
2197 if (!used_in_offsetof) {
2199 errorf(&designator->source_position,
2200 "array index [%E] must be positive", array_index);
2203 if (type->array.size_constant == true) {
2204 long array_size = type->array.size;
2205 if (index >= array_size) {
2206 errorf(&designator->source_position,
2207 "designator [%E] (%d) exceeds array size %d",
2208 array_index, index, array_size);
2214 top->type = orig_type;
2215 top->v.index = (size_t) index;
2216 orig_type = type->array.element_type;
2218 path->top_type = orig_type;
2220 if (designator->next != NULL) {
2221 descend_into_subtype(path);
2230 static void advance_current_object(type_path_t *path, size_t top_path_level)
2232 type_path_entry_t *top = get_type_path_top(path);
2234 type_t *type = skip_typeref(top->type);
2235 if (is_type_union(type)) {
2236 /* in unions only the first element is initialized */
2237 top->v.compound_entry = NULL;
2238 } else if (is_type_struct(type)) {
2239 declaration_t *entry = top->v.compound_entry;
2241 entry = entry->next;
2242 top->v.compound_entry = entry;
2243 if (entry != NULL) {
2244 path->top_type = entry->type;
2248 assert(is_type_array(type));
2252 if (!type->array.size_constant || top->v.index < type->array.size) {
2257 /* we're past the last member of the current sub-aggregate, try if we
2258 * can ascend in the type hierarchy and continue with another subobject */
2259 size_t len = ARR_LEN(path->path);
2261 if (len > top_path_level) {
2262 ascend_from_subtype(path);
2263 advance_current_object(path, top_path_level);
2265 path->top_type = NULL;
2270 * skip until token is found.
2272 static void skip_until(int type)
2274 while (token.type != type) {
2275 if (token.type == T_EOF)
2282 * skip any {...} blocks until a closing bracket is reached.
2284 static void skip_initializers(void)
2286 if (token.type == '{')
2289 while (token.type != '}') {
2290 if (token.type == T_EOF)
2292 if (token.type == '{') {
2300 static initializer_t *create_empty_initializer(void)
2302 static initializer_t empty_initializer
2303 = { .list = { { INITIALIZER_LIST }, 0 } };
2304 return &empty_initializer;
2308 * Parse a part of an initialiser for a struct or union,
2310 static initializer_t *parse_sub_initializer(type_path_t *path,
2311 type_t *outer_type, size_t top_path_level,
2312 parse_initializer_env_t *env)
2314 if (token.type == '}') {
2315 /* empty initializer */
2316 return create_empty_initializer();
2319 type_t *orig_type = path->top_type;
2320 type_t *type = NULL;
2322 if (orig_type == NULL) {
2323 /* We are initializing an empty compound. */
2325 type = skip_typeref(orig_type);
2327 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2328 * initializers in this case. */
2329 if (!is_type_valid(type)) {
2330 skip_initializers();
2331 return create_empty_initializer();
2335 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2338 designator_t *designator = NULL;
2339 if (token.type == '.' || token.type == '[') {
2340 designator = parse_designation();
2342 /* reset path to toplevel, evaluate designator from there */
2343 ascend_to(path, top_path_level);
2344 if (!walk_designator(path, designator, false)) {
2345 /* can't continue after designation error */
2349 initializer_t *designator_initializer
2350 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2351 designator_initializer->designator.designator = designator;
2352 ARR_APP1(initializer_t*, initializers, designator_initializer);
2354 orig_type = path->top_type;
2355 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2360 if (token.type == '{') {
2361 if (type != NULL && is_type_scalar(type)) {
2362 sub = parse_scalar_initializer(type, env->must_be_constant);
2366 if (env->declaration != NULL) {
2367 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2368 env->declaration->symbol);
2370 errorf(HERE, "extra brace group at end of initializer");
2373 descend_into_subtype(path);
2375 add_anchor_token('}');
2376 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2378 rem_anchor_token('}');
2381 ascend_from_subtype(path);
2385 goto error_parse_next;
2389 /* must be an expression */
2390 expression_t *expression = parse_assignment_expression();
2392 if (env->must_be_constant && !is_initializer_constant(expression)) {
2393 errorf(&expression->base.source_position,
2394 "Initialisation expression '%E' is not constant\n",
2399 /* we are already outside, ... */
2403 /* handle { "string" } special case */
2404 if ((expression->kind == EXPR_STRING_LITERAL
2405 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2406 && outer_type != NULL) {
2407 sub = initializer_from_expression(outer_type, expression);
2409 if (token.type == ',') {
2412 if (token.type != '}') {
2413 warningf(HERE, "excessive elements in initializer for type '%T'",
2416 /* TODO: eat , ... */
2421 /* descend into subtypes until expression matches type */
2423 orig_type = path->top_type;
2424 type = skip_typeref(orig_type);
2426 sub = initializer_from_expression(orig_type, expression);
2430 if (!is_type_valid(type)) {
2433 if (is_type_scalar(type)) {
2434 errorf(&expression->base.source_position,
2435 "expression '%E' doesn't match expected type '%T'",
2436 expression, orig_type);
2440 descend_into_subtype(path);
2444 /* update largest index of top array */
2445 const type_path_entry_t *first = &path->path[0];
2446 type_t *first_type = first->type;
2447 first_type = skip_typeref(first_type);
2448 if (is_type_array(first_type)) {
2449 size_t index = first->v.index;
2450 if (index > path->max_index)
2451 path->max_index = index;
2455 /* append to initializers list */
2456 ARR_APP1(initializer_t*, initializers, sub);
2459 if (env->declaration != NULL)
2460 warningf(HERE, "excess elements in struct initializer for '%Y'",
2461 env->declaration->symbol);
2463 warningf(HERE, "excess elements in struct initializer");
2467 if (token.type == '}') {
2471 if (token.type == '}') {
2476 /* advance to the next declaration if we are not at the end */
2477 advance_current_object(path, top_path_level);
2478 orig_type = path->top_type;
2479 if (orig_type != NULL)
2480 type = skip_typeref(orig_type);
2486 size_t len = ARR_LEN(initializers);
2487 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2488 initializer_t *result = allocate_ast_zero(size);
2489 result->kind = INITIALIZER_LIST;
2490 result->list.len = len;
2491 memcpy(&result->list.initializers, initializers,
2492 len * sizeof(initializers[0]));
2494 DEL_ARR_F(initializers);
2495 ascend_to(path, top_path_level+1);
2500 skip_initializers();
2501 DEL_ARR_F(initializers);
2502 ascend_to(path, top_path_level+1);
2507 * Parses an initializer. Parsers either a compound literal
2508 * (env->declaration == NULL) or an initializer of a declaration.
2510 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2512 type_t *type = skip_typeref(env->type);
2513 initializer_t *result = NULL;
2516 if (is_type_scalar(type)) {
2517 result = parse_scalar_initializer(type, env->must_be_constant);
2518 } else if (token.type == '{') {
2522 memset(&path, 0, sizeof(path));
2523 path.top_type = env->type;
2524 path.path = NEW_ARR_F(type_path_entry_t, 0);
2526 descend_into_subtype(&path);
2528 add_anchor_token('}');
2529 result = parse_sub_initializer(&path, env->type, 1, env);
2530 rem_anchor_token('}');
2532 max_index = path.max_index;
2533 DEL_ARR_F(path.path);
2537 /* parse_scalar_initializer() also works in this case: we simply
2538 * have an expression without {} around it */
2539 result = parse_scalar_initializer(type, env->must_be_constant);
2542 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2543 * the array type size */
2544 if (is_type_array(type) && type->array.size_expression == NULL
2545 && result != NULL) {
2547 switch (result->kind) {
2548 case INITIALIZER_LIST:
2549 size = max_index + 1;
2552 case INITIALIZER_STRING:
2553 size = result->string.string.size;
2556 case INITIALIZER_WIDE_STRING:
2557 size = result->wide_string.string.size;
2560 case INITIALIZER_DESIGNATOR:
2561 case INITIALIZER_VALUE:
2562 /* can happen for parse errors */
2567 internal_errorf(HERE, "invalid initializer type");
2570 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2571 cnst->base.type = type_size_t;
2572 cnst->conste.v.int_value = size;
2574 type_t *new_type = duplicate_type(type);
2576 new_type->array.size_expression = cnst;
2577 new_type->array.size_constant = true;
2578 new_type->array.size = size;
2579 env->type = new_type;
2587 static declaration_t *append_declaration(declaration_t *declaration);
2589 static declaration_t *parse_compound_type_specifier(bool is_struct)
2591 gnu_attribute_t *attributes = NULL;
2592 decl_modifiers_t modifiers = 0;
2599 symbol_t *symbol = NULL;
2600 declaration_t *declaration = NULL;
2602 if (token.type == T___attribute__) {
2603 modifiers |= parse_attributes(&attributes);
2606 if (token.type == T_IDENTIFIER) {
2607 symbol = token.v.symbol;
2610 namespace_t const namespc =
2611 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2612 declaration = get_declaration(symbol, namespc);
2613 if (declaration != NULL) {
2614 if (declaration->parent_scope != scope &&
2615 (token.type == '{' || token.type == ';')) {
2617 } else if (declaration->init.complete &&
2618 token.type == '{') {
2619 assert(symbol != NULL);
2620 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2621 is_struct ? "struct" : "union", symbol,
2622 &declaration->source_position);
2623 declaration->scope.declarations = NULL;
2626 } else if (token.type != '{') {
2628 parse_error_expected("while parsing struct type specifier",
2629 T_IDENTIFIER, '{', NULL);
2631 parse_error_expected("while parsing union type specifier",
2632 T_IDENTIFIER, '{', NULL);
2638 if (declaration == NULL) {
2639 declaration = allocate_declaration_zero();
2640 declaration->namespc =
2641 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2642 declaration->source_position = token.source_position;
2643 declaration->symbol = symbol;
2644 declaration->parent_scope = scope;
2645 if (symbol != NULL) {
2646 environment_push(declaration);
2648 append_declaration(declaration);
2651 if (token.type == '{') {
2652 declaration->init.complete = true;
2654 parse_compound_type_entries(declaration);
2655 modifiers |= parse_attributes(&attributes);
2658 declaration->modifiers |= modifiers;
2662 static void parse_enum_entries(type_t *const enum_type)
2666 if (token.type == '}') {
2668 errorf(HERE, "empty enum not allowed");
2672 add_anchor_token('}');
2674 if (token.type != T_IDENTIFIER) {
2675 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2677 rem_anchor_token('}');
2681 declaration_t *const entry = allocate_declaration_zero();
2682 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2683 entry->type = enum_type;
2684 entry->symbol = token.v.symbol;
2685 entry->source_position = token.source_position;
2688 if (token.type == '=') {
2690 expression_t *value = parse_constant_expression();
2692 value = create_implicit_cast(value, enum_type);
2693 entry->init.enum_value = value;
2698 record_declaration(entry);
2700 if (token.type != ',')
2703 } while (token.type != '}');
2704 rem_anchor_token('}');
2712 static type_t *parse_enum_specifier(void)
2714 gnu_attribute_t *attributes = NULL;
2715 declaration_t *declaration;
2719 if (token.type == T_IDENTIFIER) {
2720 symbol = token.v.symbol;
2723 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2724 } else if (token.type != '{') {
2725 parse_error_expected("while parsing enum type specifier",
2726 T_IDENTIFIER, '{', NULL);
2733 if (declaration == NULL) {
2734 declaration = allocate_declaration_zero();
2735 declaration->namespc = NAMESPACE_ENUM;
2736 declaration->source_position = token.source_position;
2737 declaration->symbol = symbol;
2738 declaration->parent_scope = scope;
2741 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2742 type->enumt.declaration = declaration;
2744 if (token.type == '{') {
2745 if (declaration->init.complete) {
2746 errorf(HERE, "multiple definitions of enum %Y", symbol);
2748 if (symbol != NULL) {
2749 environment_push(declaration);
2751 append_declaration(declaration);
2752 declaration->init.complete = true;
2754 parse_enum_entries(type);
2755 parse_attributes(&attributes);
2762 * if a symbol is a typedef to another type, return true
2764 static bool is_typedef_symbol(symbol_t *symbol)
2766 const declaration_t *const declaration =
2767 get_declaration(symbol, NAMESPACE_NORMAL);
2769 declaration != NULL &&
2770 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2773 static type_t *parse_typeof(void)
2780 add_anchor_token(')');
2782 expression_t *expression = NULL;
2785 switch(token.type) {
2786 case T___extension__:
2787 /* This can be a prefix to a typename or an expression. We simply eat
2791 } while (token.type == T___extension__);
2795 if (is_typedef_symbol(token.v.symbol)) {
2796 type = parse_typename();
2798 expression = parse_expression();
2799 type = expression->base.type;
2804 type = parse_typename();
2808 expression = parse_expression();
2809 type = expression->base.type;
2813 rem_anchor_token(')');
2816 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2817 typeof_type->typeoft.expression = expression;
2818 typeof_type->typeoft.typeof_type = type;
2825 typedef enum specifiers_t {
2826 SPECIFIER_SIGNED = 1 << 0,
2827 SPECIFIER_UNSIGNED = 1 << 1,
2828 SPECIFIER_LONG = 1 << 2,
2829 SPECIFIER_INT = 1 << 3,
2830 SPECIFIER_DOUBLE = 1 << 4,
2831 SPECIFIER_CHAR = 1 << 5,
2832 SPECIFIER_SHORT = 1 << 6,
2833 SPECIFIER_LONG_LONG = 1 << 7,
2834 SPECIFIER_FLOAT = 1 << 8,
2835 SPECIFIER_BOOL = 1 << 9,
2836 SPECIFIER_VOID = 1 << 10,
2837 SPECIFIER_INT8 = 1 << 11,
2838 SPECIFIER_INT16 = 1 << 12,
2839 SPECIFIER_INT32 = 1 << 13,
2840 SPECIFIER_INT64 = 1 << 14,
2841 SPECIFIER_INT128 = 1 << 15,
2842 SPECIFIER_COMPLEX = 1 << 16,
2843 SPECIFIER_IMAGINARY = 1 << 17,
2846 static type_t *create_builtin_type(symbol_t *const symbol,
2847 type_t *const real_type)
2849 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2850 type->builtin.symbol = symbol;
2851 type->builtin.real_type = real_type;
2853 type_t *result = typehash_insert(type);
2854 if (type != result) {
2861 static type_t *get_typedef_type(symbol_t *symbol)
2863 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2864 if (declaration == NULL ||
2865 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2868 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2869 type->typedeft.declaration = declaration;
2875 * check for the allowed MS alignment values.
2877 static bool check_alignment_value(long long intvalue)
2879 if (intvalue < 1 || intvalue > 8192) {
2880 errorf(HERE, "illegal alignment value");
2883 unsigned v = (unsigned)intvalue;
2884 for(unsigned i = 1; i <= 8192; i += i) {
2888 errorf(HERE, "alignment must be power of two");
2892 #define DET_MOD(name, tag) do { \
2893 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2894 *modifiers |= tag; \
2897 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2899 decl_modifiers_t *modifiers = &specifiers->modifiers;
2902 if (token.type == T_restrict) {
2904 DET_MOD(restrict, DM_RESTRICT);
2906 } else if (token.type != T_IDENTIFIER)
2908 symbol_t *symbol = token.v.symbol;
2909 if (symbol == sym_align) {
2912 if (token.type != T_INTEGER)
2914 if (check_alignment_value(token.v.intvalue)) {
2915 if (specifiers->alignment != 0)
2916 warningf(HERE, "align used more than once");
2917 specifiers->alignment = (unsigned char)token.v.intvalue;
2921 } else if (symbol == sym_allocate) {
2924 if (token.type != T_IDENTIFIER)
2926 (void)token.v.symbol;
2928 } else if (symbol == sym_dllimport) {
2930 DET_MOD(dllimport, DM_DLLIMPORT);
2931 } else if (symbol == sym_dllexport) {
2933 DET_MOD(dllexport, DM_DLLEXPORT);
2934 } else if (symbol == sym_thread) {
2936 DET_MOD(thread, DM_THREAD);
2937 } else if (symbol == sym_naked) {
2939 DET_MOD(naked, DM_NAKED);
2940 } else if (symbol == sym_noinline) {
2942 DET_MOD(noinline, DM_NOINLINE);
2943 } else if (symbol == sym_noreturn) {
2945 DET_MOD(noreturn, DM_NORETURN);
2946 } else if (symbol == sym_nothrow) {
2948 DET_MOD(nothrow, DM_NOTHROW);
2949 } else if (symbol == sym_novtable) {
2951 DET_MOD(novtable, DM_NOVTABLE);
2952 } else if (symbol == sym_property) {
2956 bool is_get = false;
2957 if (token.type != T_IDENTIFIER)
2959 if (token.v.symbol == sym_get) {
2961 } else if (token.v.symbol == sym_put) {
2963 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2968 if (token.type != T_IDENTIFIER)
2971 if (specifiers->get_property_sym != NULL) {
2972 errorf(HERE, "get property name already specified");
2974 specifiers->get_property_sym = token.v.symbol;
2977 if (specifiers->put_property_sym != NULL) {
2978 errorf(HERE, "put property name already specified");
2980 specifiers->put_property_sym = token.v.symbol;
2984 if (token.type == ',') {
2991 } else if (symbol == sym_selectany) {
2993 DET_MOD(selectany, DM_SELECTANY);
2994 } else if (symbol == sym_uuid) {
2997 if (token.type != T_STRING_LITERAL)
3001 } else if (symbol == sym_deprecated) {
3003 if (specifiers->deprecated != 0)
3004 warningf(HERE, "deprecated used more than once");
3005 specifiers->deprecated = 1;
3006 if (token.type == '(') {
3008 if (token.type == T_STRING_LITERAL) {
3009 specifiers->deprecated_string = token.v.string.begin;
3012 errorf(HERE, "string literal expected");
3016 } else if (symbol == sym_noalias) {
3018 DET_MOD(noalias, DM_NOALIAS);
3020 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3022 if (token.type == '(')
3026 if (token.type == ',')
3033 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3035 type_t *type = NULL;
3036 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3037 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3038 unsigned type_specifiers = 0;
3041 specifiers->source_position = token.source_position;
3044 specifiers->modifiers
3045 |= parse_attributes(&specifiers->gnu_attributes);
3046 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3047 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3049 switch(token.type) {
3052 #define MATCH_STORAGE_CLASS(token, class) \
3054 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3055 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3057 specifiers->declared_storage_class = class; \
3061 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3062 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3063 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3064 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3065 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3070 add_anchor_token(')');
3071 parse_microsoft_extended_decl_modifier(specifiers);
3072 rem_anchor_token(')');
3077 switch (specifiers->declared_storage_class) {
3078 case STORAGE_CLASS_NONE:
3079 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3082 case STORAGE_CLASS_EXTERN:
3083 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3086 case STORAGE_CLASS_STATIC:
3087 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3091 errorf(HERE, "multiple storage classes in declaration specifiers");
3097 /* type qualifiers */
3098 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3100 qualifiers |= qualifier; \
3104 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3105 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3106 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3107 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3108 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3109 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3110 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3111 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3113 case T___extension__:
3118 /* type specifiers */
3119 #define MATCH_SPECIFIER(token, specifier, name) \
3122 if (type_specifiers & specifier) { \
3123 errorf(HERE, "multiple " name " type specifiers given"); \
3125 type_specifiers |= specifier; \
3129 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3130 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3131 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3132 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3133 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3134 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3135 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3136 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3137 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3138 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3139 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3140 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3141 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3142 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3143 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3144 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3146 case T__forceinline:
3147 /* only in microsoft mode */
3148 specifiers->modifiers |= DM_FORCEINLINE;
3152 specifiers->is_inline = true;
3157 if (type_specifiers & SPECIFIER_LONG_LONG) {
3158 errorf(HERE, "multiple type specifiers given");
3159 } else if (type_specifiers & SPECIFIER_LONG) {
3160 type_specifiers |= SPECIFIER_LONG_LONG;
3162 type_specifiers |= SPECIFIER_LONG;
3167 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3169 type->compound.declaration = parse_compound_type_specifier(true);
3173 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3174 type->compound.declaration = parse_compound_type_specifier(false);
3175 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3176 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3180 type = parse_enum_specifier();
3183 type = parse_typeof();
3185 case T___builtin_va_list:
3186 type = duplicate_type(type_valist);
3190 case T_IDENTIFIER: {
3191 /* only parse identifier if we haven't found a type yet */
3192 if (type != NULL || type_specifiers != 0)
3193 goto finish_specifiers;
3195 type_t *typedef_type = get_typedef_type(token.v.symbol);
3197 if (typedef_type == NULL)
3198 goto finish_specifiers;
3201 type = typedef_type;
3205 /* function specifier */
3207 goto finish_specifiers;
3214 atomic_type_kind_t atomic_type;
3216 /* match valid basic types */
3217 switch(type_specifiers) {
3218 case SPECIFIER_VOID:
3219 atomic_type = ATOMIC_TYPE_VOID;
3221 case SPECIFIER_CHAR:
3222 atomic_type = ATOMIC_TYPE_CHAR;
3224 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3225 atomic_type = ATOMIC_TYPE_SCHAR;
3227 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3228 atomic_type = ATOMIC_TYPE_UCHAR;
3230 case SPECIFIER_SHORT:
3231 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3232 case SPECIFIER_SHORT | SPECIFIER_INT:
3233 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3234 atomic_type = ATOMIC_TYPE_SHORT;
3236 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3237 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3238 atomic_type = ATOMIC_TYPE_USHORT;
3241 case SPECIFIER_SIGNED:
3242 case SPECIFIER_SIGNED | SPECIFIER_INT:
3243 atomic_type = ATOMIC_TYPE_INT;
3245 case SPECIFIER_UNSIGNED:
3246 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3247 atomic_type = ATOMIC_TYPE_UINT;
3249 case SPECIFIER_LONG:
3250 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3251 case SPECIFIER_LONG | SPECIFIER_INT:
3252 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3253 atomic_type = ATOMIC_TYPE_LONG;
3255 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3256 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3257 atomic_type = ATOMIC_TYPE_ULONG;
3259 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3260 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3261 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3262 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3264 atomic_type = ATOMIC_TYPE_LONGLONG;
3266 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3267 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3269 atomic_type = ATOMIC_TYPE_ULONGLONG;
3272 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3273 atomic_type = unsigned_int8_type_kind;
3276 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3277 atomic_type = unsigned_int16_type_kind;
3280 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3281 atomic_type = unsigned_int32_type_kind;
3284 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3285 atomic_type = unsigned_int64_type_kind;
3288 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3289 atomic_type = unsigned_int128_type_kind;
3292 case SPECIFIER_INT8:
3293 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3294 atomic_type = int8_type_kind;
3297 case SPECIFIER_INT16:
3298 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3299 atomic_type = int16_type_kind;
3302 case SPECIFIER_INT32:
3303 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3304 atomic_type = int32_type_kind;
3307 case SPECIFIER_INT64:
3308 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3309 atomic_type = int64_type_kind;
3312 case SPECIFIER_INT128:
3313 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3314 atomic_type = int128_type_kind;
3317 case SPECIFIER_FLOAT:
3318 atomic_type = ATOMIC_TYPE_FLOAT;
3320 case SPECIFIER_DOUBLE:
3321 atomic_type = ATOMIC_TYPE_DOUBLE;
3323 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3324 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3326 case SPECIFIER_BOOL:
3327 atomic_type = ATOMIC_TYPE_BOOL;
3329 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3330 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3331 atomic_type = ATOMIC_TYPE_FLOAT;
3333 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3334 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3335 atomic_type = ATOMIC_TYPE_DOUBLE;
3337 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3338 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3339 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3342 /* invalid specifier combination, give an error message */
3343 if (type_specifiers == 0) {
3344 if (! strict_mode) {
3345 if (warning.implicit_int) {
3346 warningf(HERE, "no type specifiers in declaration, using 'int'");
3348 atomic_type = ATOMIC_TYPE_INT;
3351 errorf(HERE, "no type specifiers given in declaration");
3353 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3354 (type_specifiers & SPECIFIER_UNSIGNED)) {
3355 errorf(HERE, "signed and unsigned specifiers gives");
3356 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3357 errorf(HERE, "only integer types can be signed or unsigned");
3359 errorf(HERE, "multiple datatypes in declaration");
3361 atomic_type = ATOMIC_TYPE_INVALID;
3364 if (type_specifiers & SPECIFIER_COMPLEX &&
3365 atomic_type != ATOMIC_TYPE_INVALID) {
3366 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3367 type->complex.akind = atomic_type;
3368 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3369 atomic_type != ATOMIC_TYPE_INVALID) {
3370 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3371 type->imaginary.akind = atomic_type;
3373 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3374 type->atomic.akind = atomic_type;
3378 if (type_specifiers != 0) {
3379 errorf(HERE, "multiple datatypes in declaration");
3383 /* FIXME: check type qualifiers here */
3385 type->base.qualifiers = qualifiers;
3386 type->base.modifiers = modifiers;
3388 type_t *result = typehash_insert(type);
3389 if (newtype && result != type) {
3393 specifiers->type = result;
3398 static type_qualifiers_t parse_type_qualifiers(void)
3400 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3403 switch(token.type) {
3404 /* type qualifiers */
3405 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3406 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3407 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3408 /* microsoft extended type modifiers */
3409 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3410 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3411 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3412 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3413 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3421 static declaration_t *parse_identifier_list(void)
3423 declaration_t *declarations = NULL;
3424 declaration_t *last_declaration = NULL;
3426 declaration_t *const declaration = allocate_declaration_zero();
3427 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3428 declaration->source_position = token.source_position;
3429 declaration->symbol = token.v.symbol;
3432 if (last_declaration != NULL) {
3433 last_declaration->next = declaration;
3435 declarations = declaration;
3437 last_declaration = declaration;
3439 if (token.type != ',') {
3443 } while (token.type == T_IDENTIFIER);
3445 return declarations;
3448 static type_t *automatic_type_conversion(type_t *orig_type);
3450 static void semantic_parameter(declaration_t *declaration)
3452 /* TODO: improve error messages */
3454 if (declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3455 errorf(HERE, "typedef not allowed in parameter list");
3456 } else if (declaration->declared_storage_class != STORAGE_CLASS_NONE
3457 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3458 errorf(HERE, "parameter may only have none or register storage class");
3461 type_t *const orig_type = declaration->type;
3462 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3463 * sugar. Turn it into a pointer.
3464 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3465 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3467 type_t *const type = automatic_type_conversion(orig_type);
3468 declaration->type = type;
3470 if (is_type_incomplete(skip_typeref(type))) {
3471 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3472 orig_type, declaration->symbol);
3476 static declaration_t *parse_parameter(void)
3478 declaration_specifiers_t specifiers;
3479 memset(&specifiers, 0, sizeof(specifiers));
3481 parse_declaration_specifiers(&specifiers);
3483 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3488 static declaration_t *parse_parameters(function_type_t *type)
3490 declaration_t *declarations = NULL;
3493 add_anchor_token(')');
3494 int saved_comma_state = save_and_reset_anchor_state(',');
3496 if (token.type == T_IDENTIFIER) {
3497 symbol_t *symbol = token.v.symbol;
3498 if (!is_typedef_symbol(symbol)) {
3499 type->kr_style_parameters = true;
3500 declarations = parse_identifier_list();
3501 goto parameters_finished;
3505 if (token.type == ')') {
3506 type->unspecified_parameters = 1;
3507 goto parameters_finished;
3510 declaration_t *declaration;
3511 declaration_t *last_declaration = NULL;
3512 function_parameter_t *parameter;
3513 function_parameter_t *last_parameter = NULL;
3516 switch(token.type) {
3520 goto parameters_finished;
3523 case T___extension__:
3525 declaration = parse_parameter();
3527 /* func(void) is not a parameter */
3528 if (last_parameter == NULL
3529 && token.type == ')'
3530 && declaration->symbol == NULL
3531 && skip_typeref(declaration->type) == type_void) {
3532 goto parameters_finished;
3534 semantic_parameter(declaration);
3536 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3537 memset(parameter, 0, sizeof(parameter[0]));
3538 parameter->type = declaration->type;
3540 if (last_parameter != NULL) {
3541 last_declaration->next = declaration;
3542 last_parameter->next = parameter;
3544 type->parameters = parameter;
3545 declarations = declaration;
3547 last_parameter = parameter;
3548 last_declaration = declaration;
3552 goto parameters_finished;
3554 if (token.type != ',') {
3555 goto parameters_finished;
3561 parameters_finished:
3562 rem_anchor_token(')');
3565 restore_anchor_state(',', saved_comma_state);
3566 return declarations;
3569 restore_anchor_state(',', saved_comma_state);
3573 typedef enum construct_type_kind_t {
3578 } construct_type_kind_t;
3580 typedef struct construct_type_t construct_type_t;
3581 struct construct_type_t {
3582 construct_type_kind_t kind;
3583 construct_type_t *next;
3586 typedef struct parsed_pointer_t parsed_pointer_t;
3587 struct parsed_pointer_t {
3588 construct_type_t construct_type;
3589 type_qualifiers_t type_qualifiers;
3592 typedef struct construct_function_type_t construct_function_type_t;
3593 struct construct_function_type_t {
3594 construct_type_t construct_type;
3595 type_t *function_type;
3598 typedef struct parsed_array_t parsed_array_t;
3599 struct parsed_array_t {
3600 construct_type_t construct_type;
3601 type_qualifiers_t type_qualifiers;
3607 typedef struct construct_base_type_t construct_base_type_t;
3608 struct construct_base_type_t {
3609 construct_type_t construct_type;
3613 static construct_type_t *parse_pointer_declarator(void)
3617 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3618 memset(pointer, 0, sizeof(pointer[0]));
3619 pointer->construct_type.kind = CONSTRUCT_POINTER;
3620 pointer->type_qualifiers = parse_type_qualifiers();
3622 return (construct_type_t*) pointer;
3625 static construct_type_t *parse_array_declarator(void)
3628 add_anchor_token(']');
3630 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3631 memset(array, 0, sizeof(array[0]));
3632 array->construct_type.kind = CONSTRUCT_ARRAY;
3634 if (token.type == T_static) {
3635 array->is_static = true;
3639 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3640 if (type_qualifiers != 0) {
3641 if (token.type == T_static) {
3642 array->is_static = true;
3646 array->type_qualifiers = type_qualifiers;
3648 if (token.type == '*' && look_ahead(1)->type == ']') {
3649 array->is_variable = true;
3651 } else if (token.type != ']') {
3652 array->size = parse_assignment_expression();
3655 rem_anchor_token(']');
3658 return (construct_type_t*) array;
3663 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3666 if (declaration != NULL) {
3667 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3669 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3671 if (mask & (mask-1)) {
3672 const char *first = NULL, *second = NULL;
3674 /* more than one calling convention set */
3675 if (declaration->modifiers & DM_CDECL) {
3676 if (first == NULL) first = "cdecl";
3677 else if (second == NULL) second = "cdecl";
3679 if (declaration->modifiers & DM_STDCALL) {
3680 if (first == NULL) first = "stdcall";
3681 else if (second == NULL) second = "stdcall";
3683 if (declaration->modifiers & DM_FASTCALL) {
3684 if (first == NULL) first = "faslcall";
3685 else if (second == NULL) second = "fastcall";
3687 if (declaration->modifiers & DM_THISCALL) {
3688 if (first == NULL) first = "thiscall";
3689 else if (second == NULL) second = "thiscall";
3691 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3694 if (declaration->modifiers & DM_CDECL)
3695 type->function.calling_convention = CC_CDECL;
3696 else if (declaration->modifiers & DM_STDCALL)
3697 type->function.calling_convention = CC_STDCALL;
3698 else if (declaration->modifiers & DM_FASTCALL)
3699 type->function.calling_convention = CC_FASTCALL;
3700 else if (declaration->modifiers & DM_THISCALL)
3701 type->function.calling_convention = CC_THISCALL;
3703 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3706 declaration_t *parameters = parse_parameters(&type->function);
3707 if (declaration != NULL) {
3708 declaration->scope.declarations = parameters;
3711 construct_function_type_t *construct_function_type =
3712 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3713 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3714 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3715 construct_function_type->function_type = type;
3717 return &construct_function_type->construct_type;
3720 static void fix_declaration_type(declaration_t *declaration)
3722 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3723 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3725 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3726 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3728 if (declaration->type->base.modifiers == type_modifiers)
3731 type_t *copy = duplicate_type(declaration->type);
3732 copy->base.modifiers = type_modifiers;
3734 type_t *result = typehash_insert(copy);
3735 if (result != copy) {
3736 obstack_free(type_obst, copy);
3739 declaration->type = result;
3742 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3743 bool may_be_abstract)
3745 /* construct a single linked list of construct_type_t's which describe
3746 * how to construct the final declarator type */
3747 construct_type_t *first = NULL;
3748 construct_type_t *last = NULL;
3749 gnu_attribute_t *attributes = NULL;
3751 decl_modifiers_t modifiers = parse_attributes(&attributes);
3754 while (token.type == '*') {
3755 construct_type_t *type = parse_pointer_declarator();
3765 /* TODO: find out if this is correct */
3766 modifiers |= parse_attributes(&attributes);
3769 construct_type_t *inner_types = NULL;
3771 switch(token.type) {
3773 if (declaration == NULL) {
3774 errorf(HERE, "no identifier expected in typename");
3776 declaration->symbol = token.v.symbol;
3777 declaration->source_position = token.source_position;
3783 add_anchor_token(')');
3784 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3785 rem_anchor_token(')');
3789 if (may_be_abstract)
3791 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3792 /* avoid a loop in the outermost scope, because eat_statement doesn't
3794 if (token.type == '}' && current_function == NULL) {
3802 construct_type_t *p = last;
3805 construct_type_t *type;
3806 switch(token.type) {
3808 type = parse_function_declarator(declaration);
3811 type = parse_array_declarator();
3814 goto declarator_finished;
3817 /* insert in the middle of the list (behind p) */
3819 type->next = p->next;
3830 declarator_finished:
3831 /* append inner_types at the end of the list, we don't to set last anymore
3832 * as it's not needed anymore */
3834 assert(first == NULL);
3835 first = inner_types;
3837 last->next = inner_types;
3845 static void parse_declaration_attributes(declaration_t *declaration)
3847 gnu_attribute_t *attributes = NULL;
3848 decl_modifiers_t modifiers = parse_attributes(&attributes);
3850 if (declaration == NULL)
3853 declaration->modifiers |= modifiers;
3854 /* check if we have these stupid mode attributes... */
3855 type_t *old_type = declaration->type;
3856 if (old_type == NULL)
3859 gnu_attribute_t *attribute = attributes;
3860 for ( ; attribute != NULL; attribute = attribute->next) {
3861 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3864 atomic_type_kind_t akind = attribute->u.akind;
3865 if (!is_type_signed(old_type)) {
3867 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3868 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3869 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3870 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3872 panic("invalid akind in mode attribute");
3876 = make_atomic_type(akind, old_type->base.qualifiers);
3880 static type_t *construct_declarator_type(construct_type_t *construct_list,
3883 construct_type_t *iter = construct_list;
3884 for( ; iter != NULL; iter = iter->next) {
3885 switch(iter->kind) {
3886 case CONSTRUCT_INVALID:
3887 internal_errorf(HERE, "invalid type construction found");
3888 case CONSTRUCT_FUNCTION: {
3889 construct_function_type_t *construct_function_type
3890 = (construct_function_type_t*) iter;
3892 type_t *function_type = construct_function_type->function_type;
3894 function_type->function.return_type = type;
3896 type_t *skipped_return_type = skip_typeref(type);
3897 if (is_type_function(skipped_return_type)) {
3898 errorf(HERE, "function returning function is not allowed");
3899 type = type_error_type;
3900 } else if (is_type_array(skipped_return_type)) {
3901 errorf(HERE, "function returning array is not allowed");
3902 type = type_error_type;
3904 type = function_type;
3909 case CONSTRUCT_POINTER: {
3910 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3911 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3912 pointer_type->pointer.points_to = type;
3913 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3915 type = pointer_type;
3919 case CONSTRUCT_ARRAY: {
3920 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3921 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3923 expression_t *size_expression = parsed_array->size;
3924 if (size_expression != NULL) {
3926 = create_implicit_cast(size_expression, type_size_t);
3929 array_type->base.qualifiers = parsed_array->type_qualifiers;
3930 array_type->array.element_type = type;
3931 array_type->array.is_static = parsed_array->is_static;
3932 array_type->array.is_variable = parsed_array->is_variable;
3933 array_type->array.size_expression = size_expression;
3935 if (size_expression != NULL) {
3936 if (is_constant_expression(size_expression)) {
3937 array_type->array.size_constant = true;
3938 array_type->array.size
3939 = fold_constant(size_expression);
3941 array_type->array.is_vla = true;
3945 type_t *skipped_type = skip_typeref(type);
3946 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3947 errorf(HERE, "array of void is not allowed");
3948 type = type_error_type;
3956 type_t *hashed_type = typehash_insert(type);
3957 if (hashed_type != type) {
3958 /* the function type was constructed earlier freeing it here will
3959 * destroy other types... */
3960 if (iter->kind != CONSTRUCT_FUNCTION) {
3970 static declaration_t *parse_declarator(
3971 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3973 declaration_t *const declaration = allocate_declaration_zero();
3974 declaration->declared_storage_class = specifiers->declared_storage_class;
3975 declaration->modifiers = specifiers->modifiers;
3976 declaration->deprecated = specifiers->deprecated;
3977 declaration->deprecated_string = specifiers->deprecated_string;
3978 declaration->get_property_sym = specifiers->get_property_sym;
3979 declaration->put_property_sym = specifiers->put_property_sym;
3980 declaration->is_inline = specifiers->is_inline;
3982 declaration->storage_class = specifiers->declared_storage_class;
3983 if (declaration->storage_class == STORAGE_CLASS_NONE
3984 && scope != global_scope) {
3985 declaration->storage_class = STORAGE_CLASS_AUTO;
3988 if (specifiers->alignment != 0) {
3989 /* TODO: add checks here */
3990 declaration->alignment = specifiers->alignment;
3993 construct_type_t *construct_type
3994 = parse_inner_declarator(declaration, may_be_abstract);
3995 type_t *const type = specifiers->type;
3996 declaration->type = construct_declarator_type(construct_type, type);
3998 parse_declaration_attributes(declaration);
4000 fix_declaration_type(declaration);
4002 if (construct_type != NULL) {
4003 obstack_free(&temp_obst, construct_type);
4009 static type_t *parse_abstract_declarator(type_t *base_type)
4011 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4013 type_t *result = construct_declarator_type(construct_type, base_type);
4014 if (construct_type != NULL) {
4015 obstack_free(&temp_obst, construct_type);
4021 static declaration_t *append_declaration(declaration_t* const declaration)
4023 if (last_declaration != NULL) {
4024 last_declaration->next = declaration;
4026 scope->declarations = declaration;
4028 last_declaration = declaration;
4033 * Check if the declaration of main is suspicious. main should be a
4034 * function with external linkage, returning int, taking either zero
4035 * arguments, two, or three arguments of appropriate types, ie.
4037 * int main([ int argc, char **argv [, char **env ] ]).
4039 * @param decl the declaration to check
4040 * @param type the function type of the declaration
4042 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4044 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4045 warningf(&decl->source_position,
4046 "'main' is normally a non-static function");
4048 if (skip_typeref(func_type->return_type) != type_int) {
4049 warningf(&decl->source_position,
4050 "return type of 'main' should be 'int', but is '%T'",
4051 func_type->return_type);
4053 const function_parameter_t *parm = func_type->parameters;
4055 type_t *const first_type = parm->type;
4056 if (!types_compatible(skip_typeref(first_type), type_int)) {
4057 warningf(&decl->source_position,
4058 "first argument of 'main' should be 'int', but is '%T'", first_type);
4062 type_t *const second_type = parm->type;
4063 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4064 warningf(&decl->source_position,
4065 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4069 type_t *const third_type = parm->type;
4070 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4071 warningf(&decl->source_position,
4072 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4076 goto warn_arg_count;
4080 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4086 * Check if a symbol is the equal to "main".
4088 static bool is_sym_main(const symbol_t *const sym)
4090 return strcmp(sym->string, "main") == 0;
4093 static declaration_t *internal_record_declaration(
4094 declaration_t *const declaration,
4095 const bool is_definition)
4097 const symbol_t *const symbol = declaration->symbol;
4098 const namespace_t namespc = (namespace_t)declaration->namespc;
4100 assert(declaration->symbol != NULL);
4101 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4103 type_t *const orig_type = declaration->type;
4104 type_t *const type = skip_typeref(orig_type);
4105 if (is_type_function(type) &&
4106 type->function.unspecified_parameters &&
4107 warning.strict_prototypes &&
4108 previous_declaration == NULL) {
4109 warningf(&declaration->source_position,
4110 "function declaration '%#T' is not a prototype",
4111 orig_type, declaration->symbol);
4114 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4115 check_type_of_main(declaration, &type->function);
4118 assert(declaration != previous_declaration);
4119 if (previous_declaration != NULL
4120 && previous_declaration->parent_scope == scope) {
4121 /* can happen for K&R style declarations */
4122 if (previous_declaration->type == NULL) {
4123 previous_declaration->type = declaration->type;
4126 const type_t *prev_type = skip_typeref(previous_declaration->type);
4127 if (!types_compatible(type, prev_type)) {
4128 errorf(&declaration->source_position,
4129 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4130 orig_type, symbol, previous_declaration->type, symbol,
4131 &previous_declaration->source_position);
4133 unsigned old_storage_class = previous_declaration->storage_class;
4134 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4135 errorf(&declaration->source_position,
4136 "redeclaration of enum entry '%Y' (declared %P)",
4137 symbol, &previous_declaration->source_position);
4138 return previous_declaration;
4141 if (warning.redundant_decls &&
4143 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4144 !(previous_declaration->modifiers & DM_USED) &&
4145 !previous_declaration->used) {
4146 warningf(&previous_declaration->source_position,
4147 "unnecessary static forward declaration for '%#T'",
4148 previous_declaration->type, symbol);
4151 unsigned new_storage_class = declaration->storage_class;
4153 if (is_type_incomplete(prev_type)) {
4154 previous_declaration->type = type;
4158 /* pretend no storage class means extern for function
4159 * declarations (except if the previous declaration is neither
4160 * none nor extern) */
4161 if (is_type_function(type)) {
4162 if (prev_type->function.unspecified_parameters) {
4163 previous_declaration->type = type;
4167 switch (old_storage_class) {
4168 case STORAGE_CLASS_NONE:
4169 old_storage_class = STORAGE_CLASS_EXTERN;
4172 case STORAGE_CLASS_EXTERN:
4173 if (is_definition) {
4174 if (warning.missing_prototypes &&
4175 prev_type->function.unspecified_parameters &&
4176 !is_sym_main(symbol)) {
4177 warningf(&declaration->source_position,
4178 "no previous prototype for '%#T'",
4181 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4182 new_storage_class = STORAGE_CLASS_EXTERN;
4191 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4192 new_storage_class == STORAGE_CLASS_EXTERN) {
4193 warn_redundant_declaration:
4194 if (!is_definition &&
4195 warning.redundant_decls &&
4196 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4197 warningf(&declaration->source_position,
4198 "redundant declaration for '%Y' (declared %P)",
4199 symbol, &previous_declaration->source_position);
4201 } else if (current_function == NULL) {
4202 if (old_storage_class != STORAGE_CLASS_STATIC &&
4203 new_storage_class == STORAGE_CLASS_STATIC) {
4204 errorf(&declaration->source_position,
4205 "static declaration of '%Y' follows non-static declaration (declared %P)",
4206 symbol, &previous_declaration->source_position);
4207 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4208 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4209 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4211 goto warn_redundant_declaration;
4213 } else if (old_storage_class == new_storage_class) {
4214 errorf(&declaration->source_position,
4215 "redeclaration of '%Y' (declared %P)",
4216 symbol, &previous_declaration->source_position);
4218 errorf(&declaration->source_position,
4219 "redeclaration of '%Y' with different linkage (declared %P)",
4220 symbol, &previous_declaration->source_position);
4224 if (declaration->is_inline)
4225 previous_declaration->is_inline = true;
4226 return previous_declaration;
4227 } else if (is_type_function(type)) {
4228 if (is_definition &&
4229 declaration->storage_class != STORAGE_CLASS_STATIC) {
4230 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4231 warningf(&declaration->source_position,
4232 "no previous prototype for '%#T'", orig_type, symbol);
4233 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4234 warningf(&declaration->source_position,
4235 "no previous declaration for '%#T'", orig_type,
4240 if (warning.missing_declarations &&
4241 scope == global_scope && (
4242 declaration->storage_class == STORAGE_CLASS_NONE ||
4243 declaration->storage_class == STORAGE_CLASS_THREAD
4245 warningf(&declaration->source_position,
4246 "no previous declaration for '%#T'", orig_type, symbol);
4250 assert(declaration->parent_scope == NULL);
4251 assert(scope != NULL);
4253 declaration->parent_scope = scope;
4255 environment_push(declaration);
4256 return append_declaration(declaration);
4259 static declaration_t *record_declaration(declaration_t *declaration)
4261 return internal_record_declaration(declaration, false);
4264 static declaration_t *record_definition(declaration_t *declaration)
4266 return internal_record_declaration(declaration, true);
4269 static void parser_error_multiple_definition(declaration_t *declaration,
4270 const source_position_t *source_position)
4272 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4273 declaration->symbol, &declaration->source_position);
4276 static bool is_declaration_specifier(const token_t *token,
4277 bool only_specifiers_qualifiers)
4279 switch(token->type) {
4284 return is_typedef_symbol(token->v.symbol);
4286 case T___extension__:
4288 return !only_specifiers_qualifiers;
4295 static void parse_init_declarator_rest(declaration_t *declaration)
4299 type_t *orig_type = declaration->type;
4300 type_t *type = skip_typeref(orig_type);
4302 if (declaration->init.initializer != NULL) {
4303 parser_error_multiple_definition(declaration, HERE);
4306 bool must_be_constant = false;
4307 if (declaration->storage_class == STORAGE_CLASS_STATIC
4308 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4309 || declaration->parent_scope == global_scope) {
4310 must_be_constant = true;
4313 parse_initializer_env_t env;
4314 env.type = orig_type;
4315 env.must_be_constant = must_be_constant;
4316 env.declaration = declaration;
4318 initializer_t *initializer = parse_initializer(&env);
4320 if (env.type != orig_type) {
4321 orig_type = env.type;
4322 type = skip_typeref(orig_type);
4323 declaration->type = env.type;
4326 if (is_type_function(type)) {
4327 errorf(&declaration->source_position,
4328 "initializers not allowed for function types at declator '%Y' (type '%T')",
4329 declaration->symbol, orig_type);
4331 declaration->init.initializer = initializer;
4335 /* parse rest of a declaration without any declarator */
4336 static void parse_anonymous_declaration_rest(
4337 const declaration_specifiers_t *specifiers,
4338 parsed_declaration_func finished_declaration)
4342 declaration_t *const declaration = allocate_declaration_zero();
4343 declaration->type = specifiers->type;
4344 declaration->declared_storage_class = specifiers->declared_storage_class;
4345 declaration->source_position = specifiers->source_position;
4346 declaration->modifiers = specifiers->modifiers;
4348 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4349 warningf(&declaration->source_position,
4350 "useless storage class in empty declaration");
4352 declaration->storage_class = STORAGE_CLASS_NONE;
4354 type_t *type = declaration->type;
4355 switch (type->kind) {
4356 case TYPE_COMPOUND_STRUCT:
4357 case TYPE_COMPOUND_UNION: {
4358 if (type->compound.declaration->symbol == NULL) {
4359 warningf(&declaration->source_position,
4360 "unnamed struct/union that defines no instances");
4369 warningf(&declaration->source_position, "empty declaration");
4373 finished_declaration(declaration);
4376 static void parse_declaration_rest(declaration_t *ndeclaration,
4377 const declaration_specifiers_t *specifiers,
4378 parsed_declaration_func finished_declaration)
4380 add_anchor_token(';');
4381 add_anchor_token('=');
4382 add_anchor_token(',');
4384 declaration_t *declaration = finished_declaration(ndeclaration);
4386 type_t *orig_type = declaration->type;
4387 type_t *type = skip_typeref(orig_type);
4389 if (type->kind != TYPE_FUNCTION &&
4390 declaration->is_inline &&
4391 is_type_valid(type)) {
4392 warningf(&declaration->source_position,
4393 "variable '%Y' declared 'inline'\n", declaration->symbol);
4396 if (token.type == '=') {
4397 parse_init_declarator_rest(declaration);
4400 if (token.type != ',')
4404 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4409 rem_anchor_token(';');
4410 rem_anchor_token('=');
4411 rem_anchor_token(',');
4414 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4416 symbol_t *symbol = declaration->symbol;
4417 if (symbol == NULL) {
4418 errorf(HERE, "anonymous declaration not valid as function parameter");
4421 namespace_t namespc = (namespace_t) declaration->namespc;
4422 if (namespc != NAMESPACE_NORMAL) {
4423 return record_declaration(declaration);
4426 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4427 if (previous_declaration == NULL ||
4428 previous_declaration->parent_scope != scope) {
4429 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4434 if (previous_declaration->type == NULL) {
4435 previous_declaration->type = declaration->type;
4436 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4437 previous_declaration->storage_class = declaration->storage_class;
4438 previous_declaration->parent_scope = scope;
4439 return previous_declaration;
4441 return record_declaration(declaration);
4445 static void parse_declaration(parsed_declaration_func finished_declaration)
4447 declaration_specifiers_t specifiers;
4448 memset(&specifiers, 0, sizeof(specifiers));
4449 parse_declaration_specifiers(&specifiers);
4451 if (token.type == ';') {
4452 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4454 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4455 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4459 static type_t *get_default_promoted_type(type_t *orig_type)
4461 type_t *result = orig_type;
4463 type_t *type = skip_typeref(orig_type);
4464 if (is_type_integer(type)) {
4465 result = promote_integer(type);
4466 } else if (type == type_float) {
4467 result = type_double;
4473 static void parse_kr_declaration_list(declaration_t *declaration)
4475 type_t *type = skip_typeref(declaration->type);
4476 if (!is_type_function(type))
4479 if (!type->function.kr_style_parameters)
4482 /* push function parameters */
4483 int top = environment_top();
4484 scope_t *last_scope = scope;
4485 set_scope(&declaration->scope);
4487 declaration_t *parameter = declaration->scope.declarations;
4488 for ( ; parameter != NULL; parameter = parameter->next) {
4489 assert(parameter->parent_scope == NULL);
4490 parameter->parent_scope = scope;
4491 environment_push(parameter);
4494 /* parse declaration list */
4495 while (is_declaration_specifier(&token, false)) {
4496 parse_declaration(finished_kr_declaration);
4499 /* pop function parameters */
4500 assert(scope == &declaration->scope);
4501 set_scope(last_scope);
4502 environment_pop_to(top);
4504 /* update function type */
4505 type_t *new_type = duplicate_type(type);
4507 function_parameter_t *parameters = NULL;
4508 function_parameter_t *last_parameter = NULL;
4510 declaration_t *parameter_declaration = declaration->scope.declarations;
4511 for( ; parameter_declaration != NULL;
4512 parameter_declaration = parameter_declaration->next) {
4513 type_t *parameter_type = parameter_declaration->type;
4514 if (parameter_type == NULL) {
4516 errorf(HERE, "no type specified for function parameter '%Y'",
4517 parameter_declaration->symbol);
4519 if (warning.implicit_int) {
4520 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4521 parameter_declaration->symbol);
4523 parameter_type = type_int;
4524 parameter_declaration->type = parameter_type;
4528 semantic_parameter(parameter_declaration);
4529 parameter_type = parameter_declaration->type;
4532 * we need the default promoted types for the function type
4534 parameter_type = get_default_promoted_type(parameter_type);
4536 function_parameter_t *function_parameter
4537 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4538 memset(function_parameter, 0, sizeof(function_parameter[0]));
4540 function_parameter->type = parameter_type;
4541 if (last_parameter != NULL) {
4542 last_parameter->next = function_parameter;
4544 parameters = function_parameter;
4546 last_parameter = function_parameter;
4549 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4551 new_type->function.parameters = parameters;
4552 new_type->function.unspecified_parameters = true;
4554 type = typehash_insert(new_type);
4555 if (type != new_type) {
4556 obstack_free(type_obst, new_type);
4559 declaration->type = type;
4562 static bool first_err = true;
4565 * When called with first_err set, prints the name of the current function,
4568 static void print_in_function(void)
4572 diagnosticf("%s: In function '%Y':\n",
4573 current_function->source_position.input_name,
4574 current_function->symbol);
4579 * Check if all labels are defined in the current function.
4580 * Check if all labels are used in the current function.
4582 static void check_labels(void)
4584 for (const goto_statement_t *goto_statement = goto_first;
4585 goto_statement != NULL;
4586 goto_statement = goto_statement->next) {
4587 declaration_t *label = goto_statement->label;
4590 if (label->source_position.input_name == NULL) {
4591 print_in_function();
4592 errorf(&goto_statement->base.source_position,
4593 "label '%Y' used but not defined", label->symbol);
4596 goto_first = goto_last = NULL;
4598 if (warning.unused_label) {
4599 for (const label_statement_t *label_statement = label_first;
4600 label_statement != NULL;
4601 label_statement = label_statement->next) {
4602 const declaration_t *label = label_statement->label;
4604 if (! label->used) {
4605 print_in_function();
4606 warningf(&label_statement->base.source_position,
4607 "label '%Y' defined but not used", label->symbol);
4611 label_first = label_last = NULL;
4615 * Check declarations of current_function for unused entities.
4617 static void check_declarations(void)
4619 if (warning.unused_parameter) {
4620 const scope_t *scope = ¤t_function->scope;
4622 const declaration_t *parameter = scope->declarations;
4623 for (; parameter != NULL; parameter = parameter->next) {
4624 if (! parameter->used) {
4625 print_in_function();
4626 warningf(¶meter->source_position,
4627 "unused parameter '%Y'", parameter->symbol);
4631 if (warning.unused_variable) {
4635 static void parse_external_declaration(void)
4637 /* function-definitions and declarations both start with declaration
4639 declaration_specifiers_t specifiers;
4640 memset(&specifiers, 0, sizeof(specifiers));
4642 add_anchor_token(';');
4643 parse_declaration_specifiers(&specifiers);
4644 rem_anchor_token(';');
4646 /* must be a declaration */
4647 if (token.type == ';') {
4648 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4652 add_anchor_token(',');
4653 add_anchor_token('=');
4654 rem_anchor_token(';');
4656 /* declarator is common to both function-definitions and declarations */
4657 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4659 rem_anchor_token(',');
4660 rem_anchor_token('=');
4661 rem_anchor_token(';');
4663 /* must be a declaration */
4664 switch (token.type) {
4667 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4671 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
4675 /* must be a function definition */
4676 parse_kr_declaration_list(ndeclaration);
4678 if (token.type != '{') {
4679 parse_error_expected("while parsing function definition", '{', NULL);
4680 eat_until_matching_token(';');
4684 type_t *type = ndeclaration->type;
4686 /* note that we don't skip typerefs: the standard doesn't allow them here
4687 * (so we can't use is_type_function here) */
4688 if (type->kind != TYPE_FUNCTION) {
4689 if (is_type_valid(type)) {
4690 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4691 type, ndeclaration->symbol);
4697 /* § 6.7.5.3 (14) a function definition with () means no
4698 * parameters (and not unspecified parameters) */
4699 if (type->function.unspecified_parameters
4700 && type->function.parameters == NULL
4701 && !type->function.kr_style_parameters) {
4702 type_t *duplicate = duplicate_type(type);
4703 duplicate->function.unspecified_parameters = false;
4705 type = typehash_insert(duplicate);
4706 if (type != duplicate) {
4707 obstack_free(type_obst, duplicate);
4709 ndeclaration->type = type;
4712 declaration_t *const declaration = record_definition(ndeclaration);
4713 if (ndeclaration != declaration) {
4714 declaration->scope = ndeclaration->scope;
4716 type = skip_typeref(declaration->type);
4718 /* push function parameters and switch scope */
4719 int top = environment_top();
4720 scope_t *last_scope = scope;
4721 set_scope(&declaration->scope);
4723 declaration_t *parameter = declaration->scope.declarations;
4724 for( ; parameter != NULL; parameter = parameter->next) {
4725 if (parameter->parent_scope == &ndeclaration->scope) {
4726 parameter->parent_scope = scope;
4728 assert(parameter->parent_scope == NULL
4729 || parameter->parent_scope == scope);
4730 parameter->parent_scope = scope;
4731 if (parameter->symbol == NULL) {
4732 errorf(&ndeclaration->source_position, "parameter name omitted");
4735 environment_push(parameter);
4738 if (declaration->init.statement != NULL) {
4739 parser_error_multiple_definition(declaration, HERE);
4742 /* parse function body */
4743 int label_stack_top = label_top();
4744 declaration_t *old_current_function = current_function;
4745 current_function = declaration;
4747 declaration->init.statement = parse_compound_statement(false);
4750 check_declarations();
4752 assert(current_function == declaration);
4753 current_function = old_current_function;
4754 label_pop_to(label_stack_top);
4757 assert(scope == &declaration->scope);
4758 set_scope(last_scope);
4759 environment_pop_to(top);
4762 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4763 source_position_t *source_position)
4765 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4767 type->bitfield.base_type = base_type;
4768 type->bitfield.size = size;
4773 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4776 declaration_t *iter = compound_declaration->scope.declarations;
4777 for( ; iter != NULL; iter = iter->next) {
4778 if (iter->namespc != NAMESPACE_NORMAL)
4781 if (iter->symbol == NULL) {
4782 type_t *type = skip_typeref(iter->type);
4783 if (is_type_compound(type)) {
4784 declaration_t *result
4785 = find_compound_entry(type->compound.declaration, symbol);
4792 if (iter->symbol == symbol) {
4800 static void parse_compound_declarators(declaration_t *struct_declaration,
4801 const declaration_specifiers_t *specifiers)
4803 declaration_t *last_declaration = struct_declaration->scope.declarations;
4804 if (last_declaration != NULL) {
4805 while(last_declaration->next != NULL) {
4806 last_declaration = last_declaration->next;
4811 declaration_t *declaration;
4813 if (token.type == ':') {
4814 source_position_t source_position = *HERE;
4817 type_t *base_type = specifiers->type;
4818 expression_t *size = parse_constant_expression();
4820 if (!is_type_integer(skip_typeref(base_type))) {
4821 errorf(HERE, "bitfield base type '%T' is not an integer type",
4825 type_t *type = make_bitfield_type(base_type, size, &source_position);
4827 declaration = allocate_declaration_zero();
4828 declaration->namespc = NAMESPACE_NORMAL;
4829 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4830 declaration->storage_class = STORAGE_CLASS_NONE;
4831 declaration->source_position = source_position;
4832 declaration->modifiers = specifiers->modifiers;
4833 declaration->type = type;
4835 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4837 type_t *orig_type = declaration->type;
4838 type_t *type = skip_typeref(orig_type);
4840 if (token.type == ':') {
4841 source_position_t source_position = *HERE;
4843 expression_t *size = parse_constant_expression();
4845 if (!is_type_integer(type)) {
4846 errorf(HERE, "bitfield base type '%T' is not an "
4847 "integer type", orig_type);
4850 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4851 declaration->type = bitfield_type;
4853 /* TODO we ignore arrays for now... what is missing is a check
4854 * that they're at the end of the struct */
4855 if (is_type_incomplete(type) && !is_type_array(type)) {
4857 "compound member '%Y' has incomplete type '%T'",
4858 declaration->symbol, orig_type);
4859 } else if (is_type_function(type)) {
4860 errorf(HERE, "compound member '%Y' must not have function "
4861 "type '%T'", declaration->symbol, orig_type);
4866 /* make sure we don't define a symbol multiple times */
4867 symbol_t *symbol = declaration->symbol;
4868 if (symbol != NULL) {
4869 declaration_t *prev_decl
4870 = find_compound_entry(struct_declaration, symbol);
4872 if (prev_decl != NULL) {
4873 assert(prev_decl->symbol == symbol);
4874 errorf(&declaration->source_position,
4875 "multiple declarations of symbol '%Y' (declared %P)",
4876 symbol, &prev_decl->source_position);
4880 /* append declaration */
4881 if (last_declaration != NULL) {
4882 last_declaration->next = declaration;
4884 struct_declaration->scope.declarations = declaration;
4886 last_declaration = declaration;
4888 if (token.type != ',')
4898 static void parse_compound_type_entries(declaration_t *compound_declaration)
4901 add_anchor_token('}');
4903 while(token.type != '}' && token.type != T_EOF) {
4904 declaration_specifiers_t specifiers;
4905 memset(&specifiers, 0, sizeof(specifiers));
4906 parse_declaration_specifiers(&specifiers);
4908 parse_compound_declarators(compound_declaration, &specifiers);
4910 rem_anchor_token('}');
4912 if (token.type == T_EOF) {
4913 errorf(HERE, "EOF while parsing struct");
4918 static type_t *parse_typename(void)
4920 declaration_specifiers_t specifiers;
4921 memset(&specifiers, 0, sizeof(specifiers));
4922 parse_declaration_specifiers(&specifiers);
4923 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4924 /* TODO: improve error message, user does probably not know what a
4925 * storage class is...
4927 errorf(HERE, "typename may not have a storage class");
4930 type_t *result = parse_abstract_declarator(specifiers.type);
4938 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4939 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4940 expression_t *left);
4942 typedef struct expression_parser_function_t expression_parser_function_t;
4943 struct expression_parser_function_t {
4944 unsigned precedence;
4945 parse_expression_function parser;
4946 unsigned infix_precedence;
4947 parse_expression_infix_function infix_parser;
4950 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4953 * Prints an error message if an expression was expected but not read
4955 static expression_t *expected_expression_error(void)
4957 /* skip the error message if the error token was read */
4958 if (token.type != T_ERROR) {
4959 errorf(HERE, "expected expression, got token '%K'", &token);
4963 return create_invalid_expression();
4967 * Parse a string constant.
4969 static expression_t *parse_string_const(void)
4972 if (token.type == T_STRING_LITERAL) {
4973 string_t res = token.v.string;
4975 while (token.type == T_STRING_LITERAL) {
4976 res = concat_strings(&res, &token.v.string);
4979 if (token.type != T_WIDE_STRING_LITERAL) {
4980 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4981 /* note: that we use type_char_ptr here, which is already the
4982 * automatic converted type. revert_automatic_type_conversion
4983 * will construct the array type */
4984 cnst->base.type = type_char_ptr;
4985 cnst->string.value = res;
4989 wres = concat_string_wide_string(&res, &token.v.wide_string);
4991 wres = token.v.wide_string;
4996 switch (token.type) {
4997 case T_WIDE_STRING_LITERAL:
4998 wres = concat_wide_strings(&wres, &token.v.wide_string);
5001 case T_STRING_LITERAL:
5002 wres = concat_wide_string_string(&wres, &token.v.string);
5006 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
5007 cnst->base.type = type_wchar_t_ptr;
5008 cnst->wide_string.value = wres;
5017 * Parse an integer constant.
5019 static expression_t *parse_int_const(void)
5021 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5022 cnst->base.source_position = *HERE;
5023 cnst->base.type = token.datatype;
5024 cnst->conste.v.int_value = token.v.intvalue;
5032 * Parse a character constant.
5034 static expression_t *parse_character_constant(void)
5036 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5038 cnst->base.source_position = *HERE;
5039 cnst->base.type = token.datatype;
5040 cnst->conste.v.character = token.v.string;
5042 if (cnst->conste.v.character.size != 1) {
5043 if (warning.multichar && (c_mode & _GNUC)) {
5045 warningf(HERE, "multi-character character constant");
5047 errorf(HERE, "more than 1 characters in character constant");
5056 * Parse a wide character constant.
5058 static expression_t *parse_wide_character_constant(void)
5060 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5062 cnst->base.source_position = *HERE;
5063 cnst->base.type = token.datatype;
5064 cnst->conste.v.wide_character = token.v.wide_string;
5066 if (cnst->conste.v.wide_character.size != 1) {
5067 if (warning.multichar && (c_mode & _GNUC)) {
5069 warningf(HERE, "multi-character character constant");
5071 errorf(HERE, "more than 1 characters in character constant");
5080 * Parse a float constant.
5082 static expression_t *parse_float_const(void)
5084 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5085 cnst->base.type = token.datatype;
5086 cnst->conste.v.float_value = token.v.floatvalue;
5093 static declaration_t *create_implicit_function(symbol_t *symbol,
5094 const source_position_t *source_position)
5096 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5097 ntype->function.return_type = type_int;
5098 ntype->function.unspecified_parameters = true;
5100 type_t *type = typehash_insert(ntype);
5101 if (type != ntype) {
5105 declaration_t *const declaration = allocate_declaration_zero();
5106 declaration->storage_class = STORAGE_CLASS_EXTERN;
5107 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5108 declaration->type = type;
5109 declaration->symbol = symbol;
5110 declaration->source_position = *source_position;
5112 bool strict_prototypes_old = warning.strict_prototypes;
5113 warning.strict_prototypes = false;
5114 record_declaration(declaration);
5115 warning.strict_prototypes = strict_prototypes_old;
5121 * Creates a return_type (func)(argument_type) function type if not
5124 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5125 type_t *argument_type2)
5127 function_parameter_t *parameter2
5128 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5129 memset(parameter2, 0, sizeof(parameter2[0]));
5130 parameter2->type = argument_type2;
5132 function_parameter_t *parameter1
5133 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5134 memset(parameter1, 0, sizeof(parameter1[0]));
5135 parameter1->type = argument_type1;
5136 parameter1->next = parameter2;
5138 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5139 type->function.return_type = return_type;
5140 type->function.parameters = parameter1;
5142 type_t *result = typehash_insert(type);
5143 if (result != type) {
5151 * Creates a return_type (func)(argument_type) function type if not
5154 * @param return_type the return type
5155 * @param argument_type the argument type
5157 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5159 function_parameter_t *parameter
5160 = obstack_alloc(type_obst, sizeof(parameter[0]));
5161 memset(parameter, 0, sizeof(parameter[0]));
5162 parameter->type = argument_type;
5164 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5165 type->function.return_type = return_type;
5166 type->function.parameters = parameter;
5168 type_t *result = typehash_insert(type);
5169 if (result != type) {
5176 static type_t *make_function_0_type(type_t *return_type)
5178 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5179 type->function.return_type = return_type;
5180 type->function.parameters = NULL;
5182 type_t *result = typehash_insert(type);
5183 if (result != type) {
5191 * Creates a function type for some function like builtins.
5193 * @param symbol the symbol describing the builtin
5195 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5197 switch(symbol->ID) {
5198 case T___builtin_alloca:
5199 return make_function_1_type(type_void_ptr, type_size_t);
5200 case T___builtin_huge_val:
5201 return make_function_0_type(type_double);
5202 case T___builtin_nan:
5203 return make_function_1_type(type_double, type_char_ptr);
5204 case T___builtin_nanf:
5205 return make_function_1_type(type_float, type_char_ptr);
5206 case T___builtin_nand:
5207 return make_function_1_type(type_long_double, type_char_ptr);
5208 case T___builtin_va_end:
5209 return make_function_1_type(type_void, type_valist);
5210 case T___builtin_expect:
5211 return make_function_2_type(type_long, type_long, type_long);
5213 internal_errorf(HERE, "not implemented builtin symbol found");
5218 * Performs automatic type cast as described in § 6.3.2.1.
5220 * @param orig_type the original type
5222 static type_t *automatic_type_conversion(type_t *orig_type)
5224 type_t *type = skip_typeref(orig_type);
5225 if (is_type_array(type)) {
5226 array_type_t *array_type = &type->array;
5227 type_t *element_type = array_type->element_type;
5228 unsigned qualifiers = array_type->base.qualifiers;
5230 return make_pointer_type(element_type, qualifiers);
5233 if (is_type_function(type)) {
5234 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5241 * reverts the automatic casts of array to pointer types and function
5242 * to function-pointer types as defined § 6.3.2.1
5244 type_t *revert_automatic_type_conversion(const expression_t *expression)
5246 switch (expression->kind) {
5247 case EXPR_REFERENCE: return expression->reference.declaration->type;
5248 case EXPR_SELECT: return expression->select.compound_entry->type;
5250 case EXPR_UNARY_DEREFERENCE: {
5251 const expression_t *const value = expression->unary.value;
5252 type_t *const type = skip_typeref(value->base.type);
5253 assert(is_type_pointer(type));
5254 return type->pointer.points_to;
5257 case EXPR_BUILTIN_SYMBOL:
5258 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5260 case EXPR_ARRAY_ACCESS: {
5261 const expression_t *array_ref = expression->array_access.array_ref;
5262 type_t *type_left = skip_typeref(array_ref->base.type);
5263 if (!is_type_valid(type_left))
5265 assert(is_type_pointer(type_left));
5266 return type_left->pointer.points_to;
5269 case EXPR_STRING_LITERAL: {
5270 size_t size = expression->string.value.size;
5271 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5274 case EXPR_WIDE_STRING_LITERAL: {
5275 size_t size = expression->wide_string.value.size;
5276 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5279 case EXPR_COMPOUND_LITERAL:
5280 return expression->compound_literal.type;
5285 return expression->base.type;
5288 static expression_t *parse_reference(void)
5290 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5292 reference_expression_t *ref = &expression->reference;
5293 symbol_t *const symbol = token.v.symbol;
5295 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5297 source_position_t source_position = token.source_position;
5300 if (declaration == NULL) {
5301 if (! strict_mode && token.type == '(') {
5302 /* an implicitly defined function */
5303 if (warning.implicit_function_declaration) {
5304 warningf(HERE, "implicit declaration of function '%Y'",
5308 declaration = create_implicit_function(symbol,
5311 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5312 return create_invalid_expression();
5316 type_t *type = declaration->type;
5318 /* we always do the auto-type conversions; the & and sizeof parser contains
5319 * code to revert this! */
5320 type = automatic_type_conversion(type);
5322 ref->declaration = declaration;
5323 ref->base.type = type;
5325 /* this declaration is used */
5326 declaration->used = true;
5328 /* check for deprecated functions */
5329 if (declaration->deprecated != 0) {
5330 const char *prefix = "";
5331 if (is_type_function(declaration->type))
5332 prefix = "function ";
5334 if (declaration->deprecated_string != NULL) {
5335 warningf(&source_position,
5336 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
5337 declaration->deprecated_string);
5339 warningf(&source_position,
5340 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
5347 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
5351 /* TODO check if explicit cast is allowed and issue warnings/errors */
5354 static expression_t *parse_compound_literal(type_t *type)
5356 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5358 parse_initializer_env_t env;
5360 env.declaration = NULL;
5361 env.must_be_constant = false;
5362 initializer_t *initializer = parse_initializer(&env);
5365 expression->compound_literal.initializer = initializer;
5366 expression->compound_literal.type = type;
5367 expression->base.type = automatic_type_conversion(type);
5373 * Parse a cast expression.
5375 static expression_t *parse_cast(void)
5377 source_position_t source_position = token.source_position;
5379 type_t *type = parse_typename();
5381 /* matching add_anchor_token() is at call site */
5382 rem_anchor_token(')');
5385 if (token.type == '{') {
5386 return parse_compound_literal(type);
5389 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5390 cast->base.source_position = source_position;
5392 expression_t *value = parse_sub_expression(20);
5394 check_cast_allowed(value, type);
5396 cast->base.type = type;
5397 cast->unary.value = value;
5401 return create_invalid_expression();
5405 * Parse a statement expression.
5407 static expression_t *parse_statement_expression(void)
5409 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5411 statement_t *statement = parse_compound_statement(true);
5412 expression->statement.statement = statement;
5413 expression->base.source_position = statement->base.source_position;
5415 /* find last statement and use its type */
5416 type_t *type = type_void;
5417 const statement_t *stmt = statement->compound.statements;
5419 while (stmt->base.next != NULL)
5420 stmt = stmt->base.next;
5422 if (stmt->kind == STATEMENT_EXPRESSION) {
5423 type = stmt->expression.expression->base.type;
5426 warningf(&expression->base.source_position, "empty statement expression ({})");
5428 expression->base.type = type;
5434 return create_invalid_expression();
5438 * Parse a braced expression.
5440 static expression_t *parse_brace_expression(void)
5443 add_anchor_token(')');
5445 switch(token.type) {
5447 /* gcc extension: a statement expression */
5448 return parse_statement_expression();
5452 return parse_cast();
5454 if (is_typedef_symbol(token.v.symbol)) {
5455 return parse_cast();
5459 expression_t *result = parse_expression();
5460 rem_anchor_token(')');
5465 return create_invalid_expression();
5468 static expression_t *parse_function_keyword(void)
5473 if (current_function == NULL) {
5474 errorf(HERE, "'__func__' used outside of a function");
5477 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5478 expression->base.type = type_char_ptr;
5479 expression->funcname.kind = FUNCNAME_FUNCTION;
5484 static expression_t *parse_pretty_function_keyword(void)
5486 eat(T___PRETTY_FUNCTION__);
5488 if (current_function == NULL) {
5489 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5492 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5493 expression->base.type = type_char_ptr;
5494 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5499 static expression_t *parse_funcsig_keyword(void)
5503 if (current_function == NULL) {
5504 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5507 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5508 expression->base.type = type_char_ptr;
5509 expression->funcname.kind = FUNCNAME_FUNCSIG;
5514 static expression_t *parse_funcdname_keyword(void)
5516 eat(T___FUNCDNAME__);
5518 if (current_function == NULL) {
5519 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5522 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5523 expression->base.type = type_char_ptr;
5524 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5529 static designator_t *parse_designator(void)
5531 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5532 result->source_position = *HERE;
5534 if (token.type != T_IDENTIFIER) {
5535 parse_error_expected("while parsing member designator",
5536 T_IDENTIFIER, NULL);
5539 result->symbol = token.v.symbol;
5542 designator_t *last_designator = result;
5544 if (token.type == '.') {
5546 if (token.type != T_IDENTIFIER) {
5547 parse_error_expected("while parsing member designator",
5548 T_IDENTIFIER, NULL);
5551 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5552 designator->source_position = *HERE;
5553 designator->symbol = token.v.symbol;
5556 last_designator->next = designator;
5557 last_designator = designator;
5560 if (token.type == '[') {
5562 add_anchor_token(']');
5563 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5564 designator->source_position = *HERE;
5565 designator->array_index = parse_expression();
5566 rem_anchor_token(']');
5568 if (designator->array_index == NULL) {
5572 last_designator->next = designator;
5573 last_designator = designator;
5585 * Parse the __builtin_offsetof() expression.
5587 static expression_t *parse_offsetof(void)
5589 eat(T___builtin_offsetof);
5591 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5592 expression->base.type = type_size_t;
5595 add_anchor_token(',');
5596 type_t *type = parse_typename();
5597 rem_anchor_token(',');
5599 add_anchor_token(')');
5600 designator_t *designator = parse_designator();
5601 rem_anchor_token(')');
5604 expression->offsetofe.type = type;
5605 expression->offsetofe.designator = designator;
5608 memset(&path, 0, sizeof(path));
5609 path.top_type = type;
5610 path.path = NEW_ARR_F(type_path_entry_t, 0);
5612 descend_into_subtype(&path);
5614 if (!walk_designator(&path, designator, true)) {
5615 return create_invalid_expression();
5618 DEL_ARR_F(path.path);
5622 return create_invalid_expression();
5626 * Parses a _builtin_va_start() expression.
5628 static expression_t *parse_va_start(void)
5630 eat(T___builtin_va_start);
5632 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5635 add_anchor_token(',');
5636 expression->va_starte.ap = parse_assignment_expression();
5637 rem_anchor_token(',');
5639 expression_t *const expr = parse_assignment_expression();
5640 if (expr->kind == EXPR_REFERENCE) {
5641 declaration_t *const decl = expr->reference.declaration;
5643 return create_invalid_expression();
5644 if (decl->parent_scope == ¤t_function->scope &&
5645 decl->next == NULL) {
5646 expression->va_starte.parameter = decl;
5651 errorf(&expr->base.source_position,
5652 "second argument of 'va_start' must be last parameter of the current function");
5654 return create_invalid_expression();
5658 * Parses a _builtin_va_arg() expression.
5660 static expression_t *parse_va_arg(void)
5662 eat(T___builtin_va_arg);
5664 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5667 expression->va_arge.ap = parse_assignment_expression();
5669 expression->base.type = parse_typename();
5674 return create_invalid_expression();
5677 static expression_t *parse_builtin_symbol(void)
5679 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5681 symbol_t *symbol = token.v.symbol;
5683 expression->builtin_symbol.symbol = symbol;
5686 type_t *type = get_builtin_symbol_type(symbol);
5687 type = automatic_type_conversion(type);
5689 expression->base.type = type;
5694 * Parses a __builtin_constant() expression.
5696 static expression_t *parse_builtin_constant(void)
5698 eat(T___builtin_constant_p);
5700 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5703 add_anchor_token(')');
5704 expression->builtin_constant.value = parse_assignment_expression();
5705 rem_anchor_token(')');
5707 expression->base.type = type_int;
5711 return create_invalid_expression();
5715 * Parses a __builtin_prefetch() expression.
5717 static expression_t *parse_builtin_prefetch(void)
5719 eat(T___builtin_prefetch);
5721 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5724 add_anchor_token(')');
5725 expression->builtin_prefetch.adr = parse_assignment_expression();
5726 if (token.type == ',') {
5728 expression->builtin_prefetch.rw = parse_assignment_expression();
5730 if (token.type == ',') {
5732 expression->builtin_prefetch.locality = parse_assignment_expression();
5734 rem_anchor_token(')');
5736 expression->base.type = type_void;
5740 return create_invalid_expression();
5744 * Parses a __builtin_is_*() compare expression.
5746 static expression_t *parse_compare_builtin(void)
5748 expression_t *expression;
5750 switch(token.type) {
5751 case T___builtin_isgreater:
5752 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5754 case T___builtin_isgreaterequal:
5755 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5757 case T___builtin_isless:
5758 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5760 case T___builtin_islessequal:
5761 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5763 case T___builtin_islessgreater:
5764 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5766 case T___builtin_isunordered:
5767 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5770 internal_errorf(HERE, "invalid compare builtin found");
5773 expression->base.source_position = *HERE;
5777 expression->binary.left = parse_assignment_expression();
5779 expression->binary.right = parse_assignment_expression();
5782 type_t *const orig_type_left = expression->binary.left->base.type;
5783 type_t *const orig_type_right = expression->binary.right->base.type;
5785 type_t *const type_left = skip_typeref(orig_type_left);
5786 type_t *const type_right = skip_typeref(orig_type_right);
5787 if (!is_type_float(type_left) && !is_type_float(type_right)) {
5788 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5789 type_error_incompatible("invalid operands in comparison",
5790 &expression->base.source_position, orig_type_left, orig_type_right);
5793 semantic_comparison(&expression->binary);
5798 return create_invalid_expression();
5803 * Parses a __builtin_expect() expression.
5805 static expression_t *parse_builtin_expect(void)
5807 eat(T___builtin_expect);
5809 expression_t *expression
5810 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5813 expression->binary.left = parse_assignment_expression();
5815 expression->binary.right = parse_constant_expression();
5818 expression->base.type = expression->binary.left->base.type;
5822 return create_invalid_expression();
5827 * Parses a MS assume() expression.
5829 static expression_t *parse_assume(void)
5833 expression_t *expression
5834 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5837 add_anchor_token(')');
5838 expression->unary.value = parse_assignment_expression();
5839 rem_anchor_token(')');
5842 expression->base.type = type_void;
5845 return create_invalid_expression();
5849 * Parse a microsoft __noop expression.
5851 static expression_t *parse_noop_expression(void)
5853 source_position_t source_position = *HERE;
5856 if (token.type == '(') {
5857 /* parse arguments */
5859 add_anchor_token(')');
5860 add_anchor_token(',');
5862 if (token.type != ')') {
5864 (void)parse_assignment_expression();
5865 if (token.type != ',')
5871 rem_anchor_token(',');
5872 rem_anchor_token(')');
5875 /* the result is a (int)0 */
5876 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5877 cnst->base.source_position = source_position;
5878 cnst->base.type = type_int;
5879 cnst->conste.v.int_value = 0;
5880 cnst->conste.is_ms_noop = true;
5885 return create_invalid_expression();
5889 * Parses a primary expression.
5891 static expression_t *parse_primary_expression(void)
5893 switch (token.type) {
5894 case T_INTEGER: return parse_int_const();
5895 case T_CHARACTER_CONSTANT: return parse_character_constant();
5896 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5897 case T_FLOATINGPOINT: return parse_float_const();
5898 case T_STRING_LITERAL:
5899 case T_WIDE_STRING_LITERAL: return parse_string_const();
5900 case T_IDENTIFIER: return parse_reference();
5901 case T___FUNCTION__:
5902 case T___func__: return parse_function_keyword();
5903 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5904 case T___FUNCSIG__: return parse_funcsig_keyword();
5905 case T___FUNCDNAME__: return parse_funcdname_keyword();
5906 case T___builtin_offsetof: return parse_offsetof();
5907 case T___builtin_va_start: return parse_va_start();
5908 case T___builtin_va_arg: return parse_va_arg();
5909 case T___builtin_expect:
5910 case T___builtin_alloca:
5911 case T___builtin_nan:
5912 case T___builtin_nand:
5913 case T___builtin_nanf:
5914 case T___builtin_huge_val:
5915 case T___builtin_va_end: return parse_builtin_symbol();
5916 case T___builtin_isgreater:
5917 case T___builtin_isgreaterequal:
5918 case T___builtin_isless:
5919 case T___builtin_islessequal:
5920 case T___builtin_islessgreater:
5921 case T___builtin_isunordered: return parse_compare_builtin();
5922 case T___builtin_constant_p: return parse_builtin_constant();
5923 case T___builtin_prefetch: return parse_builtin_prefetch();
5924 case T__assume: return parse_assume();
5926 case '(': return parse_brace_expression();
5927 case T___noop: return parse_noop_expression();
5930 errorf(HERE, "unexpected token %K, expected an expression", &token);
5931 return create_invalid_expression();
5935 * Check if the expression has the character type and issue a warning then.
5937 static void check_for_char_index_type(const expression_t *expression)
5939 type_t *const type = expression->base.type;
5940 const type_t *const base_type = skip_typeref(type);
5942 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5943 warning.char_subscripts) {
5944 warningf(&expression->base.source_position,
5945 "array subscript has type '%T'", type);
5949 static expression_t *parse_array_expression(unsigned precedence,
5955 add_anchor_token(']');
5957 expression_t *inside = parse_expression();
5959 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5961 array_access_expression_t *array_access = &expression->array_access;
5963 type_t *const orig_type_left = left->base.type;
5964 type_t *const orig_type_inside = inside->base.type;
5966 type_t *const type_left = skip_typeref(orig_type_left);
5967 type_t *const type_inside = skip_typeref(orig_type_inside);
5969 type_t *return_type;
5970 if (is_type_pointer(type_left)) {
5971 return_type = type_left->pointer.points_to;
5972 array_access->array_ref = left;
5973 array_access->index = inside;
5974 check_for_char_index_type(inside);
5975 } else if (is_type_pointer(type_inside)) {
5976 return_type = type_inside->pointer.points_to;
5977 array_access->array_ref = inside;
5978 array_access->index = left;
5979 array_access->flipped = true;
5980 check_for_char_index_type(left);
5982 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5984 "array access on object with non-pointer types '%T', '%T'",
5985 orig_type_left, orig_type_inside);
5987 return_type = type_error_type;
5988 array_access->array_ref = create_invalid_expression();
5991 rem_anchor_token(']');
5992 if (token.type != ']') {
5993 parse_error_expected("Problem while parsing array access", ']', NULL);
5998 return_type = automatic_type_conversion(return_type);
5999 expression->base.type = return_type;
6004 static expression_t *parse_typeprop(expression_kind_t const kind,
6005 source_position_t const pos,
6006 unsigned const precedence)
6008 expression_t *tp_expression = allocate_expression_zero(kind);
6009 tp_expression->base.type = type_size_t;
6010 tp_expression->base.source_position = pos;
6012 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
6014 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
6016 add_anchor_token(')');
6017 type_t* const orig_type = parse_typename();
6018 tp_expression->typeprop.type = orig_type;
6020 type_t const* const type = skip_typeref(orig_type);
6021 char const* const wrong_type =
6022 is_type_incomplete(type) ? "incomplete" :
6023 type->kind == TYPE_FUNCTION ? "function designator" :
6024 type->kind == TYPE_BITFIELD ? "bitfield" :
6026 if (wrong_type != NULL) {
6027 errorf(&pos, "operand of %s expression must not be %s type '%T'",
6028 what, wrong_type, type);
6031 rem_anchor_token(')');
6034 expression_t *expression = parse_sub_expression(precedence);
6036 type_t* const orig_type = revert_automatic_type_conversion(expression);
6037 expression->base.type = orig_type;
6039 type_t const* const type = skip_typeref(orig_type);
6040 char const* const wrong_type =
6041 is_type_incomplete(type) ? "incomplete" :
6042 type->kind == TYPE_FUNCTION ? "function designator" :
6043 type->kind == TYPE_BITFIELD ? "bitfield" :
6045 if (wrong_type != NULL) {
6046 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6049 tp_expression->typeprop.type = expression->base.type;
6050 tp_expression->typeprop.tp_expression = expression;
6053 return tp_expression;
6055 return create_invalid_expression();
6058 static expression_t *parse_sizeof(unsigned precedence)
6060 source_position_t pos = *HERE;
6062 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6065 static expression_t *parse_alignof(unsigned precedence)
6067 source_position_t pos = *HERE;
6069 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6072 static expression_t *parse_select_expression(unsigned precedence,
6073 expression_t *compound)
6076 assert(token.type == '.' || token.type == T_MINUSGREATER);
6078 bool is_pointer = (token.type == T_MINUSGREATER);
6081 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6082 select->select.compound = compound;
6084 if (token.type != T_IDENTIFIER) {
6085 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6088 symbol_t *symbol = token.v.symbol;
6089 select->select.symbol = symbol;
6092 type_t *const orig_type = compound->base.type;
6093 type_t *const type = skip_typeref(orig_type);
6095 type_t *type_left = type;
6097 if (!is_type_pointer(type)) {
6098 if (is_type_valid(type)) {
6099 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6101 return create_invalid_expression();
6103 type_left = type->pointer.points_to;
6105 type_left = skip_typeref(type_left);
6107 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6108 type_left->kind != TYPE_COMPOUND_UNION) {
6109 if (is_type_valid(type_left)) {
6110 errorf(HERE, "request for member '%Y' in something not a struct or "
6111 "union, but '%T'", symbol, type_left);
6113 return create_invalid_expression();
6116 declaration_t *const declaration = type_left->compound.declaration;
6118 if (!declaration->init.complete) {
6119 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6121 return create_invalid_expression();
6124 declaration_t *iter = find_compound_entry(declaration, symbol);
6126 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6127 return create_invalid_expression();
6130 /* we always do the auto-type conversions; the & and sizeof parser contains
6131 * code to revert this! */
6132 type_t *expression_type = automatic_type_conversion(iter->type);
6134 select->select.compound_entry = iter;
6135 select->base.type = expression_type;
6137 type_t *skipped = skip_typeref(iter->type);
6138 if (skipped->kind == TYPE_BITFIELD) {
6139 select->base.type = skipped->bitfield.base_type;
6145 static void check_call_argument(const function_parameter_t *parameter,
6146 call_argument_t *argument)
6148 type_t *expected_type = parameter->type;
6149 type_t *expected_type_skip = skip_typeref(expected_type);
6150 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6151 expression_t *arg_expr = argument->expression;
6153 /* handle transparent union gnu extension */
6154 if (is_type_union(expected_type_skip)
6155 && (expected_type_skip->base.modifiers
6156 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6157 declaration_t *union_decl = expected_type_skip->compound.declaration;
6159 declaration_t *declaration = union_decl->scope.declarations;
6160 type_t *best_type = NULL;
6161 for ( ; declaration != NULL; declaration = declaration->next) {
6162 type_t *decl_type = declaration->type;
6163 error = semantic_assign(decl_type, arg_expr);
6164 if (error == ASSIGN_ERROR_INCOMPATIBLE
6165 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6168 if (error == ASSIGN_SUCCESS) {
6169 best_type = decl_type;
6170 } else if (best_type == NULL) {
6171 best_type = decl_type;
6175 if (best_type != NULL) {
6176 expected_type = best_type;
6180 error = semantic_assign(expected_type, arg_expr);
6181 argument->expression = create_implicit_cast(argument->expression,
6184 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6185 report_assign_error(error, expected_type, arg_expr, "function call",
6186 &arg_expr->base.source_position);
6190 * Parse a call expression, ie. expression '( ... )'.
6192 * @param expression the function address
6194 static expression_t *parse_call_expression(unsigned precedence,
6195 expression_t *expression)
6198 expression_t *result = allocate_expression_zero(EXPR_CALL);
6199 result->base.source_position = expression->base.source_position;
6201 call_expression_t *call = &result->call;
6202 call->function = expression;
6204 type_t *const orig_type = expression->base.type;
6205 type_t *const type = skip_typeref(orig_type);
6207 function_type_t *function_type = NULL;
6208 if (is_type_pointer(type)) {
6209 type_t *const to_type = skip_typeref(type->pointer.points_to);
6211 if (is_type_function(to_type)) {
6212 function_type = &to_type->function;
6213 call->base.type = function_type->return_type;
6217 if (function_type == NULL && is_type_valid(type)) {
6218 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6221 /* parse arguments */
6223 add_anchor_token(')');
6224 add_anchor_token(',');
6226 if (token.type != ')') {
6227 call_argument_t *last_argument = NULL;
6230 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6232 argument->expression = parse_assignment_expression();
6233 if (last_argument == NULL) {
6234 call->arguments = argument;
6236 last_argument->next = argument;
6238 last_argument = argument;
6240 if (token.type != ',')
6245 rem_anchor_token(',');
6246 rem_anchor_token(')');
6249 if (function_type == NULL)
6252 function_parameter_t *parameter = function_type->parameters;
6253 call_argument_t *argument = call->arguments;
6254 if (!function_type->unspecified_parameters) {
6255 for( ; parameter != NULL && argument != NULL;
6256 parameter = parameter->next, argument = argument->next) {
6257 check_call_argument(parameter, argument);
6260 if (parameter != NULL) {
6261 errorf(HERE, "too few arguments to function '%E'", expression);
6262 } else if (argument != NULL && !function_type->variadic) {
6263 errorf(HERE, "too many arguments to function '%E'", expression);
6267 /* do default promotion */
6268 for( ; argument != NULL; argument = argument->next) {
6269 type_t *type = argument->expression->base.type;
6271 type = get_default_promoted_type(type);
6273 argument->expression
6274 = create_implicit_cast(argument->expression, type);
6277 check_format(&result->call);
6281 return create_invalid_expression();
6284 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6286 static bool same_compound_type(const type_t *type1, const type_t *type2)
6289 is_type_compound(type1) &&
6290 type1->kind == type2->kind &&
6291 type1->compound.declaration == type2->compound.declaration;
6295 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6297 * @param expression the conditional expression
6299 static expression_t *parse_conditional_expression(unsigned precedence,
6300 expression_t *expression)
6303 add_anchor_token(':');
6305 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6307 conditional_expression_t *conditional = &result->conditional;
6308 conditional->condition = expression;
6311 type_t *const condition_type_orig = expression->base.type;
6312 type_t *const condition_type = skip_typeref(condition_type_orig);
6313 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6314 type_error("expected a scalar type in conditional condition",
6315 &expression->base.source_position, condition_type_orig);
6318 expression_t *true_expression = parse_expression();
6319 rem_anchor_token(':');
6321 expression_t *false_expression = parse_sub_expression(precedence);
6323 type_t *const orig_true_type = true_expression->base.type;
6324 type_t *const orig_false_type = false_expression->base.type;
6325 type_t *const true_type = skip_typeref(orig_true_type);
6326 type_t *const false_type = skip_typeref(orig_false_type);
6329 type_t *result_type;
6330 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6331 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6332 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6333 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6334 warningf(&expression->base.source_position,
6335 "ISO C forbids conditional expression with only one void side");
6337 result_type = type_void;
6338 } else if (is_type_arithmetic(true_type)
6339 && is_type_arithmetic(false_type)) {
6340 result_type = semantic_arithmetic(true_type, false_type);
6342 true_expression = create_implicit_cast(true_expression, result_type);
6343 false_expression = create_implicit_cast(false_expression, result_type);
6345 conditional->true_expression = true_expression;
6346 conditional->false_expression = false_expression;
6347 conditional->base.type = result_type;
6348 } else if (same_compound_type(true_type, false_type)) {
6349 /* just take 1 of the 2 types */
6350 result_type = true_type;
6351 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6352 type_t *pointer_type;
6354 expression_t *other_expression;
6355 if (is_type_pointer(true_type) &&
6356 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6357 pointer_type = true_type;
6358 other_type = false_type;
6359 other_expression = false_expression;
6361 pointer_type = false_type;
6362 other_type = true_type;
6363 other_expression = true_expression;
6366 if (is_null_pointer_constant(other_expression)) {
6367 result_type = pointer_type;
6368 } else if (is_type_pointer(other_type)) {
6369 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
6370 type_t *to2 = skip_typeref(other_type->pointer.points_to);
6373 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
6374 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
6376 } else if (types_compatible(get_unqualified_type(to1),
6377 get_unqualified_type(to2))) {
6380 warningf(&expression->base.source_position,
6381 "pointer types '%T' and '%T' in conditional expression are incompatible",
6382 true_type, false_type);
6386 type_t *const copy = duplicate_type(to);
6387 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
6389 type_t *const type = typehash_insert(copy);
6393 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
6394 } else if (is_type_integer(other_type)) {
6395 warningf(&expression->base.source_position,
6396 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
6397 result_type = pointer_type;
6399 type_error_incompatible("while parsing conditional",
6400 &expression->base.source_position, true_type, false_type);
6401 result_type = type_error_type;
6404 /* TODO: one pointer to void*, other some pointer */
6406 if (is_type_valid(true_type) && is_type_valid(false_type)) {
6407 type_error_incompatible("while parsing conditional",
6408 &expression->base.source_position, true_type,
6411 result_type = type_error_type;
6414 conditional->true_expression
6415 = create_implicit_cast(true_expression, result_type);
6416 conditional->false_expression
6417 = create_implicit_cast(false_expression, result_type);
6418 conditional->base.type = result_type;
6421 return create_invalid_expression();
6425 * Parse an extension expression.
6427 static expression_t *parse_extension(unsigned precedence)
6429 eat(T___extension__);
6431 /* TODO enable extensions */
6432 expression_t *expression = parse_sub_expression(precedence);
6433 /* TODO disable extensions */
6438 * Parse a __builtin_classify_type() expression.
6440 static expression_t *parse_builtin_classify_type(const unsigned precedence)
6442 eat(T___builtin_classify_type);
6444 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
6445 result->base.type = type_int;
6448 add_anchor_token(')');
6449 expression_t *expression = parse_sub_expression(precedence);
6450 rem_anchor_token(')');
6452 result->classify_type.type_expression = expression;
6456 return create_invalid_expression();
6459 static void check_pointer_arithmetic(const source_position_t *source_position,
6460 type_t *pointer_type,
6461 type_t *orig_pointer_type)
6463 type_t *points_to = pointer_type->pointer.points_to;
6464 points_to = skip_typeref(points_to);
6466 if (is_type_incomplete(points_to) &&
6468 || !is_type_atomic(points_to, ATOMIC_TYPE_VOID))) {
6469 errorf(source_position,
6470 "arithmetic with pointer to incomplete type '%T' not allowed",
6472 } else if (is_type_function(points_to)) {
6473 errorf(source_position,
6474 "arithmetic with pointer to function type '%T' not allowed",
6479 static void semantic_incdec(unary_expression_t *expression)
6481 type_t *const orig_type = expression->value->base.type;
6482 type_t *const type = skip_typeref(orig_type);
6483 if (is_type_pointer(type)) {
6484 check_pointer_arithmetic(&expression->base.source_position,
6486 } else if (!is_type_real(type) && is_type_valid(type)) {
6487 /* TODO: improve error message */
6488 errorf(HERE, "operation needs an arithmetic or pointer type");
6490 expression->base.type = orig_type;
6493 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6495 type_t *const orig_type = expression->value->base.type;
6496 type_t *const type = skip_typeref(orig_type);
6497 if (!is_type_arithmetic(type)) {
6498 if (is_type_valid(type)) {
6499 /* TODO: improve error message */
6500 errorf(HERE, "operation needs an arithmetic type");
6505 expression->base.type = orig_type;
6508 static void semantic_unexpr_scalar(unary_expression_t *expression)
6510 type_t *const orig_type = expression->value->base.type;
6511 type_t *const type = skip_typeref(orig_type);
6512 if (!is_type_scalar(type)) {
6513 if (is_type_valid(type)) {
6514 errorf(HERE, "operand of ! must be of scalar type");
6519 expression->base.type = orig_type;
6522 static void semantic_unexpr_integer(unary_expression_t *expression)
6524 type_t *const orig_type = expression->value->base.type;
6525 type_t *const type = skip_typeref(orig_type);
6526 if (!is_type_integer(type)) {
6527 if (is_type_valid(type)) {
6528 errorf(HERE, "operand of ~ must be of integer type");
6533 expression->base.type = orig_type;
6536 static void semantic_dereference(unary_expression_t *expression)
6538 type_t *const orig_type = expression->value->base.type;
6539 type_t *const type = skip_typeref(orig_type);
6540 if (!is_type_pointer(type)) {
6541 if (is_type_valid(type)) {
6542 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6547 type_t *result_type = type->pointer.points_to;
6548 result_type = automatic_type_conversion(result_type);
6549 expression->base.type = result_type;
6552 static void set_address_taken(expression_t *expression, bool may_be_register)
6554 if (expression->kind != EXPR_REFERENCE)
6557 declaration_t *const declaration = expression->reference.declaration;
6558 /* happens for parse errors */
6559 if (declaration == NULL)
6562 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
6563 errorf(&expression->base.source_position,
6564 "address of register variable '%Y' requested",
6565 declaration->symbol);
6567 declaration->address_taken = 1;
6572 * Check the semantic of the address taken expression.
6574 static void semantic_take_addr(unary_expression_t *expression)
6576 expression_t *value = expression->value;
6577 value->base.type = revert_automatic_type_conversion(value);
6579 type_t *orig_type = value->base.type;
6580 if (!is_type_valid(orig_type))
6583 set_address_taken(value, false);
6585 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6588 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6589 static expression_t *parse_##unexpression_type(unsigned precedence) \
6593 expression_t *unary_expression \
6594 = allocate_expression_zero(unexpression_type); \
6595 unary_expression->base.source_position = *HERE; \
6596 unary_expression->unary.value = parse_sub_expression(precedence); \
6598 sfunc(&unary_expression->unary); \
6600 return unary_expression; \
6603 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6604 semantic_unexpr_arithmetic)
6605 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6606 semantic_unexpr_arithmetic)
6607 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6608 semantic_unexpr_scalar)
6609 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6610 semantic_dereference)
6611 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6613 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6614 semantic_unexpr_integer)
6615 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6617 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6620 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6622 static expression_t *parse_##unexpression_type(unsigned precedence, \
6623 expression_t *left) \
6625 (void) precedence; \
6628 expression_t *unary_expression \
6629 = allocate_expression_zero(unexpression_type); \
6630 unary_expression->unary.value = left; \
6632 sfunc(&unary_expression->unary); \
6634 return unary_expression; \
6637 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6638 EXPR_UNARY_POSTFIX_INCREMENT,
6640 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6641 EXPR_UNARY_POSTFIX_DECREMENT,
6644 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6646 /* TODO: handle complex + imaginary types */
6648 /* § 6.3.1.8 Usual arithmetic conversions */
6649 if (type_left == type_long_double || type_right == type_long_double) {
6650 return type_long_double;
6651 } else if (type_left == type_double || type_right == type_double) {
6653 } else if (type_left == type_float || type_right == type_float) {
6657 type_left = promote_integer(type_left);
6658 type_right = promote_integer(type_right);
6660 if (type_left == type_right)
6663 bool const signed_left = is_type_signed(type_left);
6664 bool const signed_right = is_type_signed(type_right);
6665 int const rank_left = get_rank(type_left);
6666 int const rank_right = get_rank(type_right);
6668 if (signed_left == signed_right)
6669 return rank_left >= rank_right ? type_left : type_right;
6678 u_rank = rank_right;
6679 u_type = type_right;
6681 s_rank = rank_right;
6682 s_type = type_right;
6687 if (u_rank >= s_rank)
6690 if (get_atomic_type_size(s_rank) > get_atomic_type_size(u_rank))
6694 type_t *const type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
6696 case ATOMIC_TYPE_INT: type->atomic.akind = ATOMIC_TYPE_UINT; break;
6697 case ATOMIC_TYPE_LONG: type->atomic.akind = ATOMIC_TYPE_ULONG; break;
6698 case ATOMIC_TYPE_LONGLONG: type->atomic.akind = ATOMIC_TYPE_ULONGLONG; break;
6700 default: panic("invalid atomic type");
6703 type_t* const result = typehash_insert(type);
6711 * Check the semantic restrictions for a binary expression.
6713 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6715 expression_t *const left = expression->left;
6716 expression_t *const right = expression->right;
6717 type_t *const orig_type_left = left->base.type;
6718 type_t *const orig_type_right = right->base.type;
6719 type_t *const type_left = skip_typeref(orig_type_left);
6720 type_t *const type_right = skip_typeref(orig_type_right);
6722 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6723 /* TODO: improve error message */
6724 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6725 errorf(HERE, "operation needs arithmetic types");
6730 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6731 expression->left = create_implicit_cast(left, arithmetic_type);
6732 expression->right = create_implicit_cast(right, arithmetic_type);
6733 expression->base.type = arithmetic_type;
6736 static void semantic_shift_op(binary_expression_t *expression)
6738 expression_t *const left = expression->left;
6739 expression_t *const right = expression->right;
6740 type_t *const orig_type_left = left->base.type;
6741 type_t *const orig_type_right = right->base.type;
6742 type_t * type_left = skip_typeref(orig_type_left);
6743 type_t * type_right = skip_typeref(orig_type_right);
6745 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
6746 /* TODO: improve error message */
6747 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6748 errorf(HERE, "operation needs integer types");
6753 type_left = promote_integer(type_left);
6754 type_right = promote_integer(type_right);
6756 expression->left = create_implicit_cast(left, type_left);
6757 expression->right = create_implicit_cast(right, type_right);
6758 expression->base.type = type_left;
6761 static void semantic_add(binary_expression_t *expression)
6763 expression_t *const left = expression->left;
6764 expression_t *const right = expression->right;
6765 type_t *const orig_type_left = left->base.type;
6766 type_t *const orig_type_right = right->base.type;
6767 type_t *const type_left = skip_typeref(orig_type_left);
6768 type_t *const type_right = skip_typeref(orig_type_right);
6771 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6772 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6773 expression->left = create_implicit_cast(left, arithmetic_type);
6774 expression->right = create_implicit_cast(right, arithmetic_type);
6775 expression->base.type = arithmetic_type;
6777 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6778 check_pointer_arithmetic(&expression->base.source_position,
6779 type_left, orig_type_left);
6780 expression->base.type = type_left;
6781 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
6782 check_pointer_arithmetic(&expression->base.source_position,
6783 type_right, orig_type_right);
6784 expression->base.type = type_right;
6785 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6786 errorf(&expression->base.source_position,
6787 "invalid operands to binary + ('%T', '%T')",
6788 orig_type_left, orig_type_right);
6792 static void semantic_sub(binary_expression_t *expression)
6794 expression_t *const left = expression->left;
6795 expression_t *const right = expression->right;
6796 type_t *const orig_type_left = left->base.type;
6797 type_t *const orig_type_right = right->base.type;
6798 type_t *const type_left = skip_typeref(orig_type_left);
6799 type_t *const type_right = skip_typeref(orig_type_right);
6802 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6803 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6804 expression->left = create_implicit_cast(left, arithmetic_type);
6805 expression->right = create_implicit_cast(right, arithmetic_type);
6806 expression->base.type = arithmetic_type;
6808 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6809 check_pointer_arithmetic(&expression->base.source_position,
6810 type_left, orig_type_left);
6811 expression->base.type = type_left;
6812 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6813 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
6814 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
6815 if (!types_compatible(unqual_left, unqual_right)) {
6816 errorf(&expression->base.source_position,
6817 "subtracting pointers to incompatible types '%T' and '%T'",
6818 orig_type_left, orig_type_right);
6819 } else if (!is_type_object(unqual_left)) {
6820 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
6821 warningf(&expression->base.source_position,
6822 "subtracting pointers to void");
6824 errorf(&expression->base.source_position,
6825 "subtracting pointers to non-object types '%T'",
6829 expression->base.type = type_ptrdiff_t;
6830 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6831 errorf(HERE, "invalid operands of types '%T' and '%T' to binary '-'",
6832 orig_type_left, orig_type_right);
6837 * Check the semantics of comparison expressions.
6839 * @param expression The expression to check.
6841 static void semantic_comparison(binary_expression_t *expression)
6843 expression_t *left = expression->left;
6844 expression_t *right = expression->right;
6845 type_t *orig_type_left = left->base.type;
6846 type_t *orig_type_right = right->base.type;
6848 type_t *type_left = skip_typeref(orig_type_left);
6849 type_t *type_right = skip_typeref(orig_type_right);
6851 /* TODO non-arithmetic types */
6852 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6853 /* test for signed vs unsigned compares */
6854 if (warning.sign_compare &&
6855 (expression->base.kind != EXPR_BINARY_EQUAL &&
6856 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6857 (is_type_signed(type_left) != is_type_signed(type_right))) {
6859 /* check if 1 of the operands is a constant, in this case we just
6860 * check wether we can safely represent the resulting constant in
6861 * the type of the other operand. */
6862 expression_t *const_expr = NULL;
6863 expression_t *other_expr = NULL;
6865 if (is_constant_expression(left)) {
6868 } else if (is_constant_expression(right)) {
6873 if (const_expr != NULL) {
6874 type_t *other_type = skip_typeref(other_expr->base.type);
6875 long val = fold_constant(const_expr);
6876 /* TODO: check if val can be represented by other_type */
6880 warningf(&expression->base.source_position,
6881 "comparison between signed and unsigned");
6883 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6884 expression->left = create_implicit_cast(left, arithmetic_type);
6885 expression->right = create_implicit_cast(right, arithmetic_type);
6886 expression->base.type = arithmetic_type;
6887 if (warning.float_equal &&
6888 (expression->base.kind == EXPR_BINARY_EQUAL ||
6889 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6890 is_type_float(arithmetic_type)) {
6891 warningf(&expression->base.source_position,
6892 "comparing floating point with == or != is unsafe");
6894 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6895 /* TODO check compatibility */
6896 } else if (is_type_pointer(type_left)) {
6897 expression->right = create_implicit_cast(right, type_left);
6898 } else if (is_type_pointer(type_right)) {
6899 expression->left = create_implicit_cast(left, type_right);
6900 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6901 type_error_incompatible("invalid operands in comparison",
6902 &expression->base.source_position,
6903 type_left, type_right);
6905 expression->base.type = type_int;
6909 * Checks if a compound type has constant fields.
6911 static bool has_const_fields(const compound_type_t *type)
6913 const scope_t *scope = &type->declaration->scope;
6914 const declaration_t *declaration = scope->declarations;
6916 for (; declaration != NULL; declaration = declaration->next) {
6917 if (declaration->namespc != NAMESPACE_NORMAL)
6920 const type_t *decl_type = skip_typeref(declaration->type);
6921 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6928 static bool is_lvalue(const expression_t *expression)
6930 switch (expression->kind) {
6931 case EXPR_REFERENCE:
6932 case EXPR_ARRAY_ACCESS:
6934 case EXPR_UNARY_DEREFERENCE:
6942 static bool is_valid_assignment_lhs(expression_t const* const left)
6944 type_t *const orig_type_left = revert_automatic_type_conversion(left);
6945 type_t *const type_left = skip_typeref(orig_type_left);
6947 if (!is_lvalue(left)) {
6948 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
6953 if (is_type_array(type_left)) {
6954 errorf(HERE, "cannot assign to arrays ('%E')", left);
6957 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6958 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6962 if (is_type_incomplete(type_left)) {
6963 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
6964 left, orig_type_left);
6967 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6968 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6969 left, orig_type_left);
6976 static void semantic_arithmetic_assign(binary_expression_t *expression)
6978 expression_t *left = expression->left;
6979 expression_t *right = expression->right;
6980 type_t *orig_type_left = left->base.type;
6981 type_t *orig_type_right = right->base.type;
6983 if (!is_valid_assignment_lhs(left))
6986 type_t *type_left = skip_typeref(orig_type_left);
6987 type_t *type_right = skip_typeref(orig_type_right);
6989 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6990 /* TODO: improve error message */
6991 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6992 errorf(HERE, "operation needs arithmetic types");
6997 /* combined instructions are tricky. We can't create an implicit cast on
6998 * the left side, because we need the uncasted form for the store.
6999 * The ast2firm pass has to know that left_type must be right_type
7000 * for the arithmetic operation and create a cast by itself */
7001 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7002 expression->right = create_implicit_cast(right, arithmetic_type);
7003 expression->base.type = type_left;
7006 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
7008 expression_t *const left = expression->left;
7009 expression_t *const right = expression->right;
7010 type_t *const orig_type_left = left->base.type;
7011 type_t *const orig_type_right = right->base.type;
7012 type_t *const type_left = skip_typeref(orig_type_left);
7013 type_t *const type_right = skip_typeref(orig_type_right);
7015 if (!is_valid_assignment_lhs(left))
7018 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7019 /* combined instructions are tricky. We can't create an implicit cast on
7020 * the left side, because we need the uncasted form for the store.
7021 * The ast2firm pass has to know that left_type must be right_type
7022 * for the arithmetic operation and create a cast by itself */
7023 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
7024 expression->right = create_implicit_cast(right, arithmetic_type);
7025 expression->base.type = type_left;
7026 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7027 check_pointer_arithmetic(&expression->base.source_position,
7028 type_left, orig_type_left);
7029 expression->base.type = type_left;
7030 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7031 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
7036 * Check the semantic restrictions of a logical expression.
7038 static void semantic_logical_op(binary_expression_t *expression)
7040 expression_t *const left = expression->left;
7041 expression_t *const right = expression->right;
7042 type_t *const orig_type_left = left->base.type;
7043 type_t *const orig_type_right = right->base.type;
7044 type_t *const type_left = skip_typeref(orig_type_left);
7045 type_t *const type_right = skip_typeref(orig_type_right);
7047 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7048 /* TODO: improve error message */
7049 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7050 errorf(HERE, "operation needs scalar types");
7055 expression->base.type = type_int;
7059 * Check the semantic restrictions of a binary assign expression.
7061 static void semantic_binexpr_assign(binary_expression_t *expression)
7063 expression_t *left = expression->left;
7064 type_t *orig_type_left = left->base.type;
7066 type_t *type_left = revert_automatic_type_conversion(left);
7067 type_left = skip_typeref(orig_type_left);
7069 if (!is_valid_assignment_lhs(left))
7072 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7073 report_assign_error(error, orig_type_left, expression->right,
7074 "assignment", &left->base.source_position);
7075 expression->right = create_implicit_cast(expression->right, orig_type_left);
7076 expression->base.type = orig_type_left;
7080 * Determine if the outermost operation (or parts thereof) of the given
7081 * expression has no effect in order to generate a warning about this fact.
7082 * Therefore in some cases this only examines some of the operands of the
7083 * expression (see comments in the function and examples below).
7085 * f() + 23; // warning, because + has no effect
7086 * x || f(); // no warning, because x controls execution of f()
7087 * x ? y : f(); // warning, because y has no effect
7088 * (void)x; // no warning to be able to suppress the warning
7089 * This function can NOT be used for an "expression has definitely no effect"-
7091 static bool expression_has_effect(const expression_t *const expr)
7093 switch (expr->kind) {
7094 case EXPR_UNKNOWN: break;
7095 case EXPR_INVALID: return true; /* do NOT warn */
7096 case EXPR_REFERENCE: return false;
7097 /* suppress the warning for microsoft __noop operations */
7098 case EXPR_CONST: return expr->conste.is_ms_noop;
7099 case EXPR_CHARACTER_CONSTANT: return false;
7100 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7101 case EXPR_STRING_LITERAL: return false;
7102 case EXPR_WIDE_STRING_LITERAL: return false;
7105 const call_expression_t *const call = &expr->call;
7106 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7109 switch (call->function->builtin_symbol.symbol->ID) {
7110 case T___builtin_va_end: return true;
7111 default: return false;
7115 /* Generate the warning if either the left or right hand side of a
7116 * conditional expression has no effect */
7117 case EXPR_CONDITIONAL: {
7118 const conditional_expression_t *const cond = &expr->conditional;
7120 expression_has_effect(cond->true_expression) &&
7121 expression_has_effect(cond->false_expression);
7124 case EXPR_SELECT: return false;
7125 case EXPR_ARRAY_ACCESS: return false;
7126 case EXPR_SIZEOF: return false;
7127 case EXPR_CLASSIFY_TYPE: return false;
7128 case EXPR_ALIGNOF: return false;
7130 case EXPR_FUNCNAME: return false;
7131 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7132 case EXPR_BUILTIN_CONSTANT_P: return false;
7133 case EXPR_BUILTIN_PREFETCH: return true;
7134 case EXPR_OFFSETOF: return false;
7135 case EXPR_VA_START: return true;
7136 case EXPR_VA_ARG: return true;
7137 case EXPR_STATEMENT: return true; // TODO
7138 case EXPR_COMPOUND_LITERAL: return false;
7140 case EXPR_UNARY_NEGATE: return false;
7141 case EXPR_UNARY_PLUS: return false;
7142 case EXPR_UNARY_BITWISE_NEGATE: return false;
7143 case EXPR_UNARY_NOT: return false;
7144 case EXPR_UNARY_DEREFERENCE: return false;
7145 case EXPR_UNARY_TAKE_ADDRESS: return false;
7146 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7147 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7148 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7149 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7151 /* Treat void casts as if they have an effect in order to being able to
7152 * suppress the warning */
7153 case EXPR_UNARY_CAST: {
7154 type_t *const type = skip_typeref(expr->base.type);
7155 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7158 case EXPR_UNARY_CAST_IMPLICIT: return true;
7159 case EXPR_UNARY_ASSUME: return true;
7161 case EXPR_BINARY_ADD: return false;
7162 case EXPR_BINARY_SUB: return false;
7163 case EXPR_BINARY_MUL: return false;
7164 case EXPR_BINARY_DIV: return false;
7165 case EXPR_BINARY_MOD: return false;
7166 case EXPR_BINARY_EQUAL: return false;
7167 case EXPR_BINARY_NOTEQUAL: return false;
7168 case EXPR_BINARY_LESS: return false;
7169 case EXPR_BINARY_LESSEQUAL: return false;
7170 case EXPR_BINARY_GREATER: return false;
7171 case EXPR_BINARY_GREATEREQUAL: return false;
7172 case EXPR_BINARY_BITWISE_AND: return false;
7173 case EXPR_BINARY_BITWISE_OR: return false;
7174 case EXPR_BINARY_BITWISE_XOR: return false;
7175 case EXPR_BINARY_SHIFTLEFT: return false;
7176 case EXPR_BINARY_SHIFTRIGHT: return false;
7177 case EXPR_BINARY_ASSIGN: return true;
7178 case EXPR_BINARY_MUL_ASSIGN: return true;
7179 case EXPR_BINARY_DIV_ASSIGN: return true;
7180 case EXPR_BINARY_MOD_ASSIGN: return true;
7181 case EXPR_BINARY_ADD_ASSIGN: return true;
7182 case EXPR_BINARY_SUB_ASSIGN: return true;
7183 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7184 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7185 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7186 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7187 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7189 /* Only examine the right hand side of && and ||, because the left hand
7190 * side already has the effect of controlling the execution of the right
7192 case EXPR_BINARY_LOGICAL_AND:
7193 case EXPR_BINARY_LOGICAL_OR:
7194 /* Only examine the right hand side of a comma expression, because the left
7195 * hand side has a separate warning */
7196 case EXPR_BINARY_COMMA:
7197 return expression_has_effect(expr->binary.right);
7199 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7200 case EXPR_BINARY_ISGREATER: return false;
7201 case EXPR_BINARY_ISGREATEREQUAL: return false;
7202 case EXPR_BINARY_ISLESS: return false;
7203 case EXPR_BINARY_ISLESSEQUAL: return false;
7204 case EXPR_BINARY_ISLESSGREATER: return false;
7205 case EXPR_BINARY_ISUNORDERED: return false;
7208 internal_errorf(HERE, "unexpected expression");
7211 static void semantic_comma(binary_expression_t *expression)
7213 if (warning.unused_value) {
7214 const expression_t *const left = expression->left;
7215 if (!expression_has_effect(left)) {
7216 warningf(&left->base.source_position,
7217 "left-hand operand of comma expression has no effect");
7220 expression->base.type = expression->right->base.type;
7223 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7224 static expression_t *parse_##binexpression_type(unsigned precedence, \
7225 expression_t *left) \
7228 source_position_t pos = *HERE; \
7230 expression_t *right = parse_sub_expression(precedence + lr); \
7232 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7233 binexpr->base.source_position = pos; \
7234 binexpr->binary.left = left; \
7235 binexpr->binary.right = right; \
7236 sfunc(&binexpr->binary); \
7241 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7242 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7243 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
7244 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
7245 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7246 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7247 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7248 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7249 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7251 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7252 semantic_comparison, 1)
7253 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7254 semantic_comparison, 1)
7255 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7256 semantic_comparison, 1)
7257 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7258 semantic_comparison, 1)
7260 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7261 semantic_binexpr_arithmetic, 1)
7262 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7263 semantic_binexpr_arithmetic, 1)
7264 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7265 semantic_binexpr_arithmetic, 1)
7266 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7267 semantic_logical_op, 1)
7268 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7269 semantic_logical_op, 1)
7270 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7271 semantic_shift_op, 1)
7272 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7273 semantic_shift_op, 1)
7274 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7275 semantic_arithmetic_addsubb_assign, 0)
7276 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7277 semantic_arithmetic_addsubb_assign, 0)
7278 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7279 semantic_arithmetic_assign, 0)
7280 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7281 semantic_arithmetic_assign, 0)
7282 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7283 semantic_arithmetic_assign, 0)
7284 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7285 semantic_arithmetic_assign, 0)
7286 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7287 semantic_arithmetic_assign, 0)
7288 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7289 semantic_arithmetic_assign, 0)
7290 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7291 semantic_arithmetic_assign, 0)
7292 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7293 semantic_arithmetic_assign, 0)
7295 static expression_t *parse_sub_expression(unsigned precedence)
7297 if (token.type < 0) {
7298 return expected_expression_error();
7301 expression_parser_function_t *parser
7302 = &expression_parsers[token.type];
7303 source_position_t source_position = token.source_position;
7306 if (parser->parser != NULL) {
7307 left = parser->parser(parser->precedence);
7309 left = parse_primary_expression();
7311 assert(left != NULL);
7312 left->base.source_position = source_position;
7315 if (token.type < 0) {
7316 return expected_expression_error();
7319 parser = &expression_parsers[token.type];
7320 if (parser->infix_parser == NULL)
7322 if (parser->infix_precedence < precedence)
7325 left = parser->infix_parser(parser->infix_precedence, left);
7327 assert(left != NULL);
7328 assert(left->kind != EXPR_UNKNOWN);
7329 left->base.source_position = source_position;
7336 * Parse an expression.
7338 static expression_t *parse_expression(void)
7340 return parse_sub_expression(1);
7344 * Register a parser for a prefix-like operator with given precedence.
7346 * @param parser the parser function
7347 * @param token_type the token type of the prefix token
7348 * @param precedence the precedence of the operator
7350 static void register_expression_parser(parse_expression_function parser,
7351 int token_type, unsigned precedence)
7353 expression_parser_function_t *entry = &expression_parsers[token_type];
7355 if (entry->parser != NULL) {
7356 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7357 panic("trying to register multiple expression parsers for a token");
7359 entry->parser = parser;
7360 entry->precedence = precedence;
7364 * Register a parser for an infix operator with given precedence.
7366 * @param parser the parser function
7367 * @param token_type the token type of the infix operator
7368 * @param precedence the precedence of the operator
7370 static void register_infix_parser(parse_expression_infix_function parser,
7371 int token_type, unsigned precedence)
7373 expression_parser_function_t *entry = &expression_parsers[token_type];
7375 if (entry->infix_parser != NULL) {
7376 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7377 panic("trying to register multiple infix expression parsers for a "
7380 entry->infix_parser = parser;
7381 entry->infix_precedence = precedence;
7385 * Initialize the expression parsers.
7387 static void init_expression_parsers(void)
7389 memset(&expression_parsers, 0, sizeof(expression_parsers));
7391 register_infix_parser(parse_array_expression, '[', 30);
7392 register_infix_parser(parse_call_expression, '(', 30);
7393 register_infix_parser(parse_select_expression, '.', 30);
7394 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
7395 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
7397 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
7400 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
7401 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
7402 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
7403 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
7404 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
7405 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
7406 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
7407 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
7408 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
7409 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
7410 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
7411 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
7412 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
7413 T_EXCLAMATIONMARKEQUAL, 13);
7414 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
7415 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
7416 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
7417 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
7418 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
7419 register_infix_parser(parse_conditional_expression, '?', 7);
7420 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
7421 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
7422 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
7423 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
7424 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
7425 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
7426 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
7427 T_LESSLESSEQUAL, 2);
7428 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7429 T_GREATERGREATEREQUAL, 2);
7430 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
7432 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
7434 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
7437 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
7439 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
7440 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
7441 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
7442 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
7443 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
7444 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
7445 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
7447 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
7449 register_expression_parser(parse_sizeof, T_sizeof, 25);
7450 register_expression_parser(parse_alignof, T___alignof__, 25);
7451 register_expression_parser(parse_extension, T___extension__, 25);
7452 register_expression_parser(parse_builtin_classify_type,
7453 T___builtin_classify_type, 25);
7457 * Parse a asm statement arguments specification.
7459 static asm_argument_t *parse_asm_arguments(bool is_out)
7461 asm_argument_t *result = NULL;
7462 asm_argument_t *last = NULL;
7464 while (token.type == T_STRING_LITERAL || token.type == '[') {
7465 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7466 memset(argument, 0, sizeof(argument[0]));
7468 if (token.type == '[') {
7470 if (token.type != T_IDENTIFIER) {
7471 parse_error_expected("while parsing asm argument",
7472 T_IDENTIFIER, NULL);
7475 argument->symbol = token.v.symbol;
7480 argument->constraints = parse_string_literals();
7482 add_anchor_token(')');
7483 expression_t *expression = parse_expression();
7484 rem_anchor_token(')');
7486 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
7487 * change size or type representation (e.g. int -> long is ok, but
7488 * int -> float is not) */
7489 if (expression->kind == EXPR_UNARY_CAST) {
7490 type_t *const type = expression->base.type;
7491 type_kind_t const kind = type->kind;
7492 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
7495 if (kind == TYPE_ATOMIC) {
7496 atomic_type_kind_t const akind = type->atomic.akind;
7497 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7498 size = get_atomic_type_size(akind);
7500 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7501 size = get_atomic_type_size(get_intptr_kind());
7505 expression_t *const value = expression->unary.value;
7506 type_t *const value_type = value->base.type;
7507 type_kind_t const value_kind = value_type->kind;
7509 unsigned value_flags;
7510 unsigned value_size;
7511 if (value_kind == TYPE_ATOMIC) {
7512 atomic_type_kind_t const value_akind = value_type->atomic.akind;
7513 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
7514 value_size = get_atomic_type_size(value_akind);
7515 } else if (value_kind == TYPE_POINTER) {
7516 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
7517 value_size = get_atomic_type_size(get_intptr_kind());
7522 if (value_flags != flags || value_size != size)
7526 } while (expression->kind == EXPR_UNARY_CAST);
7530 if (!is_lvalue(expression)) {
7531 errorf(&expression->base.source_position,
7532 "asm output argument is not an lvalue");
7535 argument->expression = expression;
7538 set_address_taken(expression, true);
7541 last->next = argument;
7547 if (token.type != ',')
7558 * Parse a asm statement clobber specification.
7560 static asm_clobber_t *parse_asm_clobbers(void)
7562 asm_clobber_t *result = NULL;
7563 asm_clobber_t *last = NULL;
7565 while(token.type == T_STRING_LITERAL) {
7566 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
7567 clobber->clobber = parse_string_literals();
7570 last->next = clobber;
7576 if (token.type != ',')
7585 * Parse an asm statement.
7587 static statement_t *parse_asm_statement(void)
7591 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
7592 statement->base.source_position = token.source_position;
7594 asm_statement_t *asm_statement = &statement->asms;
7596 if (token.type == T_volatile) {
7598 asm_statement->is_volatile = true;
7602 add_anchor_token(')');
7603 add_anchor_token(':');
7604 asm_statement->asm_text = parse_string_literals();
7606 if (token.type != ':') {
7607 rem_anchor_token(':');
7612 asm_statement->outputs = parse_asm_arguments(true);
7613 if (token.type != ':') {
7614 rem_anchor_token(':');
7619 asm_statement->inputs = parse_asm_arguments(false);
7620 if (token.type != ':') {
7621 rem_anchor_token(':');
7624 rem_anchor_token(':');
7627 asm_statement->clobbers = parse_asm_clobbers();
7630 rem_anchor_token(')');
7634 if (asm_statement->outputs == NULL) {
7635 /* GCC: An 'asm' instruction without any output operands will be treated
7636 * identically to a volatile 'asm' instruction. */
7637 asm_statement->is_volatile = true;
7642 return create_invalid_statement();
7646 * Parse a case statement.
7648 static statement_t *parse_case_statement(void)
7652 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7654 statement->base.source_position = token.source_position;
7655 statement->case_label.expression = parse_expression();
7657 if (c_mode & _GNUC) {
7658 if (token.type == T_DOTDOTDOT) {
7660 statement->case_label.end_range = parse_expression();
7666 if (! is_constant_expression(statement->case_label.expression)) {
7667 errorf(&statement->base.source_position,
7668 "case label does not reduce to an integer constant");
7670 /* TODO: check if the case label is already known */
7671 if (current_switch != NULL) {
7672 /* link all cases into the switch statement */
7673 if (current_switch->last_case == NULL) {
7674 current_switch->first_case =
7675 current_switch->last_case = &statement->case_label;
7677 current_switch->last_case->next = &statement->case_label;
7680 errorf(&statement->base.source_position,
7681 "case label not within a switch statement");
7684 statement->case_label.statement = parse_statement();
7688 return create_invalid_statement();
7692 * Finds an existing default label of a switch statement.
7694 static case_label_statement_t *
7695 find_default_label(const switch_statement_t *statement)
7697 case_label_statement_t *label = statement->first_case;
7698 for ( ; label != NULL; label = label->next) {
7699 if (label->expression == NULL)
7706 * Parse a default statement.
7708 static statement_t *parse_default_statement(void)
7712 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7714 statement->base.source_position = token.source_position;
7717 if (current_switch != NULL) {
7718 const case_label_statement_t *def_label = find_default_label(current_switch);
7719 if (def_label != NULL) {
7720 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7721 &def_label->base.source_position);
7723 /* link all cases into the switch statement */
7724 if (current_switch->last_case == NULL) {
7725 current_switch->first_case =
7726 current_switch->last_case = &statement->case_label;
7728 current_switch->last_case->next = &statement->case_label;
7732 errorf(&statement->base.source_position,
7733 "'default' label not within a switch statement");
7735 statement->case_label.statement = parse_statement();
7739 return create_invalid_statement();
7743 * Return the declaration for a given label symbol or create a new one.
7745 * @param symbol the symbol of the label
7747 static declaration_t *get_label(symbol_t *symbol)
7749 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7750 assert(current_function != NULL);
7751 /* if we found a label in the same function, then we already created the
7753 if (candidate != NULL
7754 && candidate->parent_scope == ¤t_function->scope) {
7758 /* otherwise we need to create a new one */
7759 declaration_t *const declaration = allocate_declaration_zero();
7760 declaration->namespc = NAMESPACE_LABEL;
7761 declaration->symbol = symbol;
7763 label_push(declaration);
7769 * Parse a label statement.
7771 static statement_t *parse_label_statement(void)
7773 assert(token.type == T_IDENTIFIER);
7774 symbol_t *symbol = token.v.symbol;
7777 declaration_t *label = get_label(symbol);
7779 /* if source position is already set then the label is defined twice,
7780 * otherwise it was just mentioned in a goto so far */
7781 if (label->source_position.input_name != NULL) {
7782 errorf(HERE, "duplicate label '%Y' (declared %P)",
7783 symbol, &label->source_position);
7785 label->source_position = token.source_position;
7788 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7790 statement->base.source_position = token.source_position;
7791 statement->label.label = label;
7795 if (token.type == '}') {
7796 /* TODO only warn? */
7798 warningf(HERE, "label at end of compound statement");
7799 statement->label.statement = create_empty_statement();
7801 errorf(HERE, "label at end of compound statement");
7802 statement->label.statement = create_invalid_statement();
7804 } else if (token.type == ';') {
7805 /* Eat an empty statement here, to avoid the warning about an empty
7806 * statement after a label. label:; is commonly used to have a label
7807 * before a closing brace. */
7808 statement->label.statement = create_empty_statement();
7811 statement->label.statement = parse_statement();
7814 /* remember the labels in a list for later checking */
7815 if (label_last == NULL) {
7816 label_first = &statement->label;
7818 label_last->next = &statement->label;
7820 label_last = &statement->label;
7826 * Parse an if statement.
7828 static statement_t *parse_if(void)
7832 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7833 statement->base.source_position = token.source_position;
7836 add_anchor_token(')');
7837 statement->ifs.condition = parse_expression();
7838 rem_anchor_token(')');
7841 add_anchor_token(T_else);
7842 statement->ifs.true_statement = parse_statement();
7843 rem_anchor_token(T_else);
7845 if (token.type == T_else) {
7847 statement->ifs.false_statement = parse_statement();
7852 return create_invalid_statement();
7856 * Parse a switch statement.
7858 static statement_t *parse_switch(void)
7862 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7863 statement->base.source_position = token.source_position;
7866 expression_t *const expr = parse_expression();
7867 type_t * type = skip_typeref(expr->base.type);
7868 if (is_type_integer(type)) {
7869 type = promote_integer(type);
7870 } else if (is_type_valid(type)) {
7871 errorf(&expr->base.source_position,
7872 "switch quantity is not an integer, but '%T'", type);
7873 type = type_error_type;
7875 statement->switchs.expression = create_implicit_cast(expr, type);
7878 switch_statement_t *rem = current_switch;
7879 current_switch = &statement->switchs;
7880 statement->switchs.body = parse_statement();
7881 current_switch = rem;
7883 if (warning.switch_default &&
7884 find_default_label(&statement->switchs) == NULL) {
7885 warningf(&statement->base.source_position, "switch has no default case");
7890 return create_invalid_statement();
7893 static statement_t *parse_loop_body(statement_t *const loop)
7895 statement_t *const rem = current_loop;
7896 current_loop = loop;
7898 statement_t *const body = parse_statement();
7905 * Parse a while statement.
7907 static statement_t *parse_while(void)
7911 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7912 statement->base.source_position = token.source_position;
7915 add_anchor_token(')');
7916 statement->whiles.condition = parse_expression();
7917 rem_anchor_token(')');
7920 statement->whiles.body = parse_loop_body(statement);
7924 return create_invalid_statement();
7928 * Parse a do statement.
7930 static statement_t *parse_do(void)
7934 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7936 statement->base.source_position = token.source_position;
7938 add_anchor_token(T_while);
7939 statement->do_while.body = parse_loop_body(statement);
7940 rem_anchor_token(T_while);
7944 add_anchor_token(')');
7945 statement->do_while.condition = parse_expression();
7946 rem_anchor_token(')');
7952 return create_invalid_statement();
7956 * Parse a for statement.
7958 static statement_t *parse_for(void)
7962 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7963 statement->base.source_position = token.source_position;
7965 int top = environment_top();
7966 scope_t *last_scope = scope;
7967 set_scope(&statement->fors.scope);
7970 add_anchor_token(')');
7972 if (token.type != ';') {
7973 if (is_declaration_specifier(&token, false)) {
7974 parse_declaration(record_declaration);
7976 add_anchor_token(';');
7977 expression_t *const init = parse_expression();
7978 statement->fors.initialisation = init;
7979 if (warning.unused_value && !expression_has_effect(init)) {
7980 warningf(&init->base.source_position,
7981 "initialisation of 'for'-statement has no effect");
7983 rem_anchor_token(';');
7990 if (token.type != ';') {
7991 add_anchor_token(';');
7992 statement->fors.condition = parse_expression();
7993 rem_anchor_token(';');
7996 if (token.type != ')') {
7997 expression_t *const step = parse_expression();
7998 statement->fors.step = step;
7999 if (warning.unused_value && !expression_has_effect(step)) {
8000 warningf(&step->base.source_position,
8001 "step of 'for'-statement has no effect");
8004 rem_anchor_token(')');
8006 statement->fors.body = parse_loop_body(statement);
8008 assert(scope == &statement->fors.scope);
8009 set_scope(last_scope);
8010 environment_pop_to(top);
8015 rem_anchor_token(')');
8016 assert(scope == &statement->fors.scope);
8017 set_scope(last_scope);
8018 environment_pop_to(top);
8020 return create_invalid_statement();
8024 * Parse a goto statement.
8026 static statement_t *parse_goto(void)
8030 if (token.type != T_IDENTIFIER) {
8031 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8035 symbol_t *symbol = token.v.symbol;
8038 declaration_t *label = get_label(symbol);
8040 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8041 statement->base.source_position = token.source_position;
8043 statement->gotos.label = label;
8045 /* remember the goto's in a list for later checking */
8046 if (goto_last == NULL) {
8047 goto_first = &statement->gotos;
8049 goto_last->next = &statement->gotos;
8051 goto_last = &statement->gotos;
8057 return create_invalid_statement();
8061 * Parse a continue statement.
8063 static statement_t *parse_continue(void)
8065 statement_t *statement;
8066 if (current_loop == NULL) {
8067 errorf(HERE, "continue statement not within loop");
8068 statement = create_invalid_statement();
8070 statement = allocate_statement_zero(STATEMENT_CONTINUE);
8072 statement->base.source_position = token.source_position;
8080 return create_invalid_statement();
8084 * Parse a break statement.
8086 static statement_t *parse_break(void)
8088 statement_t *statement;
8089 if (current_switch == NULL && current_loop == NULL) {
8090 errorf(HERE, "break statement not within loop or switch");
8091 statement = create_invalid_statement();
8093 statement = allocate_statement_zero(STATEMENT_BREAK);
8095 statement->base.source_position = token.source_position;
8103 return create_invalid_statement();
8107 * Parse a __leave statement.
8109 static statement_t *parse_leave(void)
8111 statement_t *statement;
8112 if (current_try == NULL) {
8113 errorf(HERE, "__leave statement not within __try");
8114 statement = create_invalid_statement();
8116 statement = allocate_statement_zero(STATEMENT_LEAVE);
8118 statement->base.source_position = token.source_position;
8126 return create_invalid_statement();
8130 * Check if a given declaration represents a local variable.
8132 static bool is_local_var_declaration(const declaration_t *declaration)
8134 switch ((storage_class_tag_t) declaration->storage_class) {
8135 case STORAGE_CLASS_AUTO:
8136 case STORAGE_CLASS_REGISTER: {
8137 const type_t *type = skip_typeref(declaration->type);
8138 if (is_type_function(type)) {
8150 * Check if a given declaration represents a variable.
8152 static bool is_var_declaration(const declaration_t *declaration)
8154 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8157 const type_t *type = skip_typeref(declaration->type);
8158 return !is_type_function(type);
8162 * Check if a given expression represents a local variable.
8164 static bool is_local_variable(const expression_t *expression)
8166 if (expression->base.kind != EXPR_REFERENCE) {
8169 const declaration_t *declaration = expression->reference.declaration;
8170 return is_local_var_declaration(declaration);
8174 * Check if a given expression represents a local variable and
8175 * return its declaration then, else return NULL.
8177 declaration_t *expr_is_variable(const expression_t *expression)
8179 if (expression->base.kind != EXPR_REFERENCE) {
8182 declaration_t *declaration = expression->reference.declaration;
8183 if (is_var_declaration(declaration))
8189 * Parse a return statement.
8191 static statement_t *parse_return(void)
8193 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8194 statement->base.source_position = token.source_position;
8198 expression_t *return_value = NULL;
8199 if (token.type != ';') {
8200 return_value = parse_expression();
8204 const type_t *const func_type = current_function->type;
8205 assert(is_type_function(func_type));
8206 type_t *const return_type = skip_typeref(func_type->function.return_type);
8208 if (return_value != NULL) {
8209 type_t *return_value_type = skip_typeref(return_value->base.type);
8211 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8212 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8213 warningf(&statement->base.source_position,
8214 "'return' with a value, in function returning void");
8215 return_value = NULL;
8217 assign_error_t error = semantic_assign(return_type, return_value);
8218 report_assign_error(error, return_type, return_value, "'return'",
8219 &statement->base.source_position);
8220 return_value = create_implicit_cast(return_value, return_type);
8222 /* check for returning address of a local var */
8223 if (return_value != NULL &&
8224 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
8225 const expression_t *expression = return_value->unary.value;
8226 if (is_local_variable(expression)) {
8227 warningf(&statement->base.source_position,
8228 "function returns address of local variable");
8232 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
8233 warningf(&statement->base.source_position,
8234 "'return' without value, in function returning non-void");
8237 statement->returns.value = return_value;
8241 return create_invalid_statement();
8245 * Parse a declaration statement.
8247 static statement_t *parse_declaration_statement(void)
8249 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
8251 statement->base.source_position = token.source_position;
8253 declaration_t *before = last_declaration;
8254 parse_declaration(record_declaration);
8256 if (before == NULL) {
8257 statement->declaration.declarations_begin = scope->declarations;
8259 statement->declaration.declarations_begin = before->next;
8261 statement->declaration.declarations_end = last_declaration;
8267 * Parse an expression statement, ie. expr ';'.
8269 static statement_t *parse_expression_statement(void)
8271 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
8273 statement->base.source_position = token.source_position;
8274 expression_t *const expr = parse_expression();
8275 statement->expression.expression = expr;
8281 return create_invalid_statement();
8285 * Parse a microsoft __try { } __finally { } or
8286 * __try{ } __except() { }
8288 static statement_t *parse_ms_try_statment(void)
8290 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
8292 statement->base.source_position = token.source_position;
8295 ms_try_statement_t *rem = current_try;
8296 current_try = &statement->ms_try;
8297 statement->ms_try.try_statement = parse_compound_statement(false);
8300 if (token.type == T___except) {
8303 add_anchor_token(')');
8304 expression_t *const expr = parse_expression();
8305 type_t * type = skip_typeref(expr->base.type);
8306 if (is_type_integer(type)) {
8307 type = promote_integer(type);
8308 } else if (is_type_valid(type)) {
8309 errorf(&expr->base.source_position,
8310 "__expect expression is not an integer, but '%T'", type);
8311 type = type_error_type;
8313 statement->ms_try.except_expression = create_implicit_cast(expr, type);
8314 rem_anchor_token(')');
8316 statement->ms_try.final_statement = parse_compound_statement(false);
8317 } else if (token.type == T__finally) {
8319 statement->ms_try.final_statement = parse_compound_statement(false);
8321 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
8322 return create_invalid_statement();
8326 return create_invalid_statement();
8329 static statement_t *parse_empty_statement(void)
8331 if (warning.empty_statement) {
8332 warningf(HERE, "statement is empty");
8335 return create_empty_statement();
8339 * Parse a statement.
8340 * There's also parse_statement() which additionally checks for
8341 * "statement has no effect" warnings
8343 static statement_t *intern_parse_statement(void)
8345 statement_t *statement = NULL;
8347 /* declaration or statement */
8348 add_anchor_token(';');
8349 switch (token.type) {
8351 if (look_ahead(1)->type == ':') {
8352 statement = parse_label_statement();
8353 } else if (is_typedef_symbol(token.v.symbol)) {
8354 statement = parse_declaration_statement();
8356 statement = parse_expression_statement();
8360 case T___extension__:
8361 /* This can be a prefix to a declaration or an expression statement.
8362 * We simply eat it now and parse the rest with tail recursion. */
8365 } while (token.type == T___extension__);
8366 statement = parse_statement();
8370 statement = parse_declaration_statement();
8373 case ';': statement = parse_empty_statement(); break;
8374 case '{': statement = parse_compound_statement(false); break;
8375 case T___leave: statement = parse_leave(); break;
8376 case T___try: statement = parse_ms_try_statment(); break;
8377 case T_asm: statement = parse_asm_statement(); break;
8378 case T_break: statement = parse_break(); break;
8379 case T_case: statement = parse_case_statement(); break;
8380 case T_continue: statement = parse_continue(); break;
8381 case T_default: statement = parse_default_statement(); break;
8382 case T_do: statement = parse_do(); break;
8383 case T_for: statement = parse_for(); break;
8384 case T_goto: statement = parse_goto(); break;
8385 case T_if: statement = parse_if (); break;
8386 case T_return: statement = parse_return(); break;
8387 case T_switch: statement = parse_switch(); break;
8388 case T_while: statement = parse_while(); break;
8389 default: statement = parse_expression_statement(); break;
8391 rem_anchor_token(';');
8393 assert(statement != NULL
8394 && statement->base.source_position.input_name != NULL);
8400 * parse a statement and emits "statement has no effect" warning if needed
8401 * (This is really a wrapper around intern_parse_statement with check for 1
8402 * single warning. It is needed, because for statement expressions we have
8403 * to avoid the warning on the last statement)
8405 static statement_t *parse_statement(void)
8407 statement_t *statement = intern_parse_statement();
8409 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
8410 expression_t *expression = statement->expression.expression;
8411 if (!expression_has_effect(expression)) {
8412 warningf(&expression->base.source_position,
8413 "statement has no effect");
8421 * Parse a compound statement.
8423 static statement_t *parse_compound_statement(bool inside_expression_statement)
8425 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
8427 statement->base.source_position = token.source_position;
8430 add_anchor_token('}');
8432 int top = environment_top();
8433 scope_t *last_scope = scope;
8434 set_scope(&statement->compound.scope);
8436 statement_t *last_statement = NULL;
8438 while (token.type != '}' && token.type != T_EOF) {
8439 statement_t *sub_statement = intern_parse_statement();
8440 if (is_invalid_statement(sub_statement)) {
8441 /* an error occurred. if we are at an anchor, return */
8447 if (last_statement != NULL) {
8448 last_statement->base.next = sub_statement;
8450 statement->compound.statements = sub_statement;
8453 while (sub_statement->base.next != NULL)
8454 sub_statement = sub_statement->base.next;
8456 last_statement = sub_statement;
8459 if (token.type == '}') {
8462 errorf(&statement->base.source_position,
8463 "end of file while looking for closing '}'");
8466 /* look over all statements again to produce no effect warnings */
8467 if (warning.unused_value) {
8468 statement_t *sub_statement = statement->compound.statements;
8469 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
8470 if (sub_statement->kind != STATEMENT_EXPRESSION)
8472 /* don't emit a warning for the last expression in an expression
8473 * statement as it has always an effect */
8474 if (inside_expression_statement && sub_statement->base.next == NULL)
8477 expression_t *expression = sub_statement->expression.expression;
8478 if (!expression_has_effect(expression)) {
8479 warningf(&expression->base.source_position,
8480 "statement has no effect");
8486 rem_anchor_token('}');
8487 assert(scope == &statement->compound.scope);
8488 set_scope(last_scope);
8489 environment_pop_to(top);
8495 * Initialize builtin types.
8497 static void initialize_builtin_types(void)
8499 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
8500 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
8501 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
8502 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
8503 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
8504 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
8505 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
8506 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
8508 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
8509 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
8510 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
8511 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
8515 * Check for unused global static functions and variables
8517 static void check_unused_globals(void)
8519 if (!warning.unused_function && !warning.unused_variable)
8522 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
8524 decl->modifiers & DM_UNUSED ||
8525 decl->modifiers & DM_USED ||
8526 decl->storage_class != STORAGE_CLASS_STATIC)
8529 type_t *const type = decl->type;
8531 if (is_type_function(skip_typeref(type))) {
8532 if (!warning.unused_function || decl->is_inline)
8535 s = (decl->init.statement != NULL ? "defined" : "declared");
8537 if (!warning.unused_variable)
8543 warningf(&decl->source_position, "'%#T' %s but not used",
8544 type, decl->symbol, s);
8548 static void parse_global_asm(void)
8553 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8554 statement->base.source_position = token.source_position;
8555 statement->asms.asm_text = parse_string_literals();
8556 statement->base.next = unit->global_asm;
8557 unit->global_asm = statement;
8566 * Parse a translation unit.
8568 static void parse_translation_unit(void)
8570 while (token.type != T_EOF) {
8571 switch (token.type) {
8573 /* TODO error in strict mode */
8574 warningf(HERE, "stray ';' outside of function");
8583 parse_external_declaration();
8592 * @return the translation unit or NULL if errors occurred.
8594 void start_parsing(void)
8596 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8597 label_stack = NEW_ARR_F(stack_entry_t, 0);
8598 diagnostic_count = 0;
8602 type_set_output(stderr);
8603 ast_set_output(stderr);
8605 assert(unit == NULL);
8606 unit = allocate_ast_zero(sizeof(unit[0]));
8608 assert(global_scope == NULL);
8609 global_scope = &unit->scope;
8611 assert(scope == NULL);
8612 set_scope(&unit->scope);
8614 initialize_builtin_types();
8617 translation_unit_t *finish_parsing(void)
8619 assert(scope == &unit->scope);
8621 last_declaration = NULL;
8623 assert(global_scope == &unit->scope);
8624 check_unused_globals();
8625 global_scope = NULL;
8627 DEL_ARR_F(environment_stack);
8628 DEL_ARR_F(label_stack);
8630 translation_unit_t *result = unit;
8637 lookahead_bufpos = 0;
8638 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8641 parse_translation_unit();
8645 * Initialize the parser.
8647 void init_parser(void)
8650 /* add predefined symbols for extended-decl-modifier */
8651 sym_align = symbol_table_insert("align");
8652 sym_allocate = symbol_table_insert("allocate");
8653 sym_dllimport = symbol_table_insert("dllimport");
8654 sym_dllexport = symbol_table_insert("dllexport");
8655 sym_naked = symbol_table_insert("naked");
8656 sym_noinline = symbol_table_insert("noinline");
8657 sym_noreturn = symbol_table_insert("noreturn");
8658 sym_nothrow = symbol_table_insert("nothrow");
8659 sym_novtable = symbol_table_insert("novtable");
8660 sym_property = symbol_table_insert("property");
8661 sym_get = symbol_table_insert("get");
8662 sym_put = symbol_table_insert("put");
8663 sym_selectany = symbol_table_insert("selectany");
8664 sym_thread = symbol_table_insert("thread");
8665 sym_uuid = symbol_table_insert("uuid");
8666 sym_deprecated = symbol_table_insert("deprecated");
8667 sym_restrict = symbol_table_insert("restrict");
8668 sym_noalias = symbol_table_insert("noalias");
8670 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8672 init_expression_parsers();
8673 obstack_init(&temp_obst);
8675 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8676 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8680 * Terminate the parser.
8682 void exit_parser(void)
8684 obstack_free(&temp_obst, NULL);