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 gnu_attribute_t gnu_attribute_t;
52 struct gnu_attribute_t {
53 gnu_attribute_kind_t kind;
54 gnu_attribute_t *next;
63 typedef struct declaration_specifiers_t declaration_specifiers_t;
64 struct declaration_specifiers_t {
65 source_position_t source_position;
66 unsigned char declared_storage_class;
67 unsigned char alignment; /**< Alignment, 0 if not set. */
68 unsigned int is_inline : 1;
69 unsigned int deprecated : 1;
70 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
71 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
72 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
73 symbol_t *get_property_sym; /**< the name of the get property if set. */
74 symbol_t *put_property_sym; /**< the name of the put property if set. */
79 * An environment for parsing initializers (and compound literals).
81 typedef struct parse_initializer_env_t {
82 type_t *type; /**< the type of the initializer. In case of an
83 array type with unspecified size this gets
84 adjusted to the actual size. */
85 declaration_t *declaration; /**< the declaration that is initialized if any */
86 bool must_be_constant;
87 } parse_initializer_env_t;
89 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
92 static token_t lookahead_buffer[MAX_LOOKAHEAD];
93 static int lookahead_bufpos;
94 static stack_entry_t *environment_stack = NULL;
95 static stack_entry_t *label_stack = NULL;
96 static scope_t *global_scope = NULL;
97 static scope_t *scope = NULL;
98 static declaration_t *last_declaration = NULL;
99 static declaration_t *current_function = NULL;
100 static switch_statement_t *current_switch = NULL;
101 static statement_t *current_loop = NULL;
102 static ms_try_statement_t *current_try = NULL;
103 static goto_statement_t *goto_first = NULL;
104 static goto_statement_t *goto_last = NULL;
105 static label_statement_t *label_first = NULL;
106 static label_statement_t *label_last = NULL;
107 static struct obstack temp_obst;
109 static source_position_t null_position = { NULL, 0 };
111 /* symbols for Microsoft extended-decl-modifier */
112 static const symbol_t *sym_align = NULL;
113 static const symbol_t *sym_allocate = NULL;
114 static const symbol_t *sym_dllimport = NULL;
115 static const symbol_t *sym_dllexport = NULL;
116 static const symbol_t *sym_naked = NULL;
117 static const symbol_t *sym_noinline = NULL;
118 static const symbol_t *sym_noreturn = NULL;
119 static const symbol_t *sym_nothrow = NULL;
120 static const symbol_t *sym_novtable = NULL;
121 static const symbol_t *sym_property = NULL;
122 static const symbol_t *sym_get = NULL;
123 static const symbol_t *sym_put = NULL;
124 static const symbol_t *sym_selectany = NULL;
125 static const symbol_t *sym_thread = NULL;
126 static const symbol_t *sym_uuid = NULL;
127 static const symbol_t *sym_deprecated = NULL;
128 static const symbol_t *sym_restrict = NULL;
129 static const symbol_t *sym_noalias = NULL;
131 /** The token anchor set */
132 static unsigned char token_anchor_set[T_LAST_TOKEN];
134 /** The current source position. */
135 #define HERE &token.source_position
137 static type_t *type_valist;
139 static statement_t *parse_compound_statement(bool inside_expression_statement);
140 static statement_t *parse_statement(void);
142 static expression_t *parse_sub_expression(unsigned precedence);
143 static expression_t *parse_expression(void);
144 static type_t *parse_typename(void);
146 static void parse_compound_type_entries(declaration_t *compound_declaration);
147 static declaration_t *parse_declarator(
148 const declaration_specifiers_t *specifiers, bool may_be_abstract);
149 static declaration_t *record_declaration(declaration_t *declaration);
151 static void semantic_comparison(binary_expression_t *expression);
153 #define STORAGE_CLASSES \
160 #define TYPE_QUALIFIERS \
167 #ifdef PROVIDE_COMPLEX
168 #define COMPLEX_SPECIFIERS \
170 #define IMAGINARY_SPECIFIERS \
173 #define COMPLEX_SPECIFIERS
174 #define IMAGINARY_SPECIFIERS
177 #define TYPE_SPECIFIERS \
192 case T___builtin_va_list: \
197 #define DECLARATION_START \
202 #define TYPENAME_START \
207 * Allocate an AST node with given size and
208 * initialize all fields with zero.
210 static void *allocate_ast_zero(size_t size)
212 void *res = allocate_ast(size);
213 memset(res, 0, size);
217 static declaration_t *allocate_declaration_zero(void)
219 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
220 declaration->type = type_error_type;
221 declaration->alignment = 0;
226 * Returns the size of a statement node.
228 * @param kind the statement kind
230 static size_t get_statement_struct_size(statement_kind_t kind)
232 static const size_t sizes[] = {
233 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
234 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
235 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
236 [STATEMENT_RETURN] = sizeof(return_statement_t),
237 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
238 [STATEMENT_IF] = sizeof(if_statement_t),
239 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
240 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
241 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
242 [STATEMENT_BREAK] = sizeof(statement_base_t),
243 [STATEMENT_GOTO] = sizeof(goto_statement_t),
244 [STATEMENT_LABEL] = sizeof(label_statement_t),
245 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
246 [STATEMENT_WHILE] = sizeof(while_statement_t),
247 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
248 [STATEMENT_FOR] = sizeof(for_statement_t),
249 [STATEMENT_ASM] = sizeof(asm_statement_t),
250 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
251 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
253 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
254 assert(sizes[kind] != 0);
259 * Returns the size of an expression node.
261 * @param kind the expression kind
263 static size_t get_expression_struct_size(expression_kind_t kind)
265 static const size_t sizes[] = {
266 [EXPR_INVALID] = sizeof(expression_base_t),
267 [EXPR_REFERENCE] = sizeof(reference_expression_t),
268 [EXPR_CONST] = sizeof(const_expression_t),
269 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
270 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
271 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
272 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
273 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
274 [EXPR_CALL] = sizeof(call_expression_t),
275 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
276 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
277 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
278 [EXPR_SELECT] = sizeof(select_expression_t),
279 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
280 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
281 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
282 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
283 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
284 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
285 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
286 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
287 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
288 [EXPR_VA_START] = sizeof(va_start_expression_t),
289 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
290 [EXPR_STATEMENT] = sizeof(statement_expression_t),
292 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
293 return sizes[EXPR_UNARY_FIRST];
295 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
296 return sizes[EXPR_BINARY_FIRST];
298 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
299 assert(sizes[kind] != 0);
304 * Allocate a statement node of given kind and initialize all
307 static statement_t *allocate_statement_zero(statement_kind_t kind)
309 size_t size = get_statement_struct_size(kind);
310 statement_t *res = allocate_ast_zero(size);
312 res->base.kind = kind;
317 * Allocate an expression node of given kind and initialize all
320 static expression_t *allocate_expression_zero(expression_kind_t kind)
322 size_t size = get_expression_struct_size(kind);
323 expression_t *res = allocate_ast_zero(size);
325 res->base.kind = kind;
326 res->base.type = type_error_type;
331 * Creates a new invalid expression.
333 static expression_t *create_invalid_expression(void)
335 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
336 expression->base.source_position = token.source_position;
341 * Creates a new invalid statement.
343 static statement_t *create_invalid_statement(void)
345 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
346 statement->base.source_position = token.source_position;
351 * Allocate a new empty statement.
353 static statement_t *create_empty_statement(void)
355 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
356 statement->base.source_position = token.source_position;
361 * Returns the size of a type node.
363 * @param kind the type kind
365 static size_t get_type_struct_size(type_kind_t kind)
367 static const size_t sizes[] = {
368 [TYPE_ATOMIC] = sizeof(atomic_type_t),
369 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
370 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
371 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
372 [TYPE_ENUM] = sizeof(enum_type_t),
373 [TYPE_FUNCTION] = sizeof(function_type_t),
374 [TYPE_POINTER] = sizeof(pointer_type_t),
375 [TYPE_ARRAY] = sizeof(array_type_t),
376 [TYPE_BUILTIN] = sizeof(builtin_type_t),
377 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
378 [TYPE_TYPEOF] = sizeof(typeof_type_t),
380 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
381 assert(kind <= TYPE_TYPEOF);
382 assert(sizes[kind] != 0);
387 * Allocate a type node of given kind and initialize all
390 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
392 size_t size = get_type_struct_size(kind);
393 type_t *res = obstack_alloc(type_obst, size);
394 memset(res, 0, size);
396 res->base.kind = kind;
397 res->base.source_position = *source_position;
402 * Returns the size of an initializer node.
404 * @param kind the initializer kind
406 static size_t get_initializer_size(initializer_kind_t kind)
408 static const size_t sizes[] = {
409 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
410 [INITIALIZER_STRING] = sizeof(initializer_string_t),
411 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
412 [INITIALIZER_LIST] = sizeof(initializer_list_t),
413 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
415 assert(kind < sizeof(sizes) / sizeof(*sizes));
416 assert(sizes[kind] != 0);
421 * Allocate an initializer node of given kind and initialize all
424 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
426 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
433 * Free a type from the type obstack.
435 static void free_type(void *type)
437 obstack_free(type_obst, type);
441 * Returns the index of the top element of the environment stack.
443 static size_t environment_top(void)
445 return ARR_LEN(environment_stack);
449 * Returns the index of the top element of the label stack.
451 static size_t label_top(void)
453 return ARR_LEN(label_stack);
457 * Return the next token.
459 static inline void next_token(void)
461 token = lookahead_buffer[lookahead_bufpos];
462 lookahead_buffer[lookahead_bufpos] = lexer_token;
465 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
468 print_token(stderr, &token);
469 fprintf(stderr, "\n");
474 * Return the next token with a given lookahead.
476 static inline const token_t *look_ahead(int num)
478 assert(num > 0 && num <= MAX_LOOKAHEAD);
479 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
480 return &lookahead_buffer[pos];
484 * Adds a token to the token anchor set (a multi-set).
486 static void add_anchor_token(int token_type) {
487 assert(0 <= token_type && token_type < T_LAST_TOKEN);
488 ++token_anchor_set[token_type];
492 * Remove a token from the token anchor set (a multi-set).
494 static void rem_anchor_token(int token_type) {
495 assert(0 <= token_type && token_type < T_LAST_TOKEN);
496 --token_anchor_set[token_type];
499 static bool at_anchor(void) {
502 return token_anchor_set[token.type];
506 * Eat tokens until a matching token is found.
508 static void eat_until_matching_token(int type) {
509 unsigned parenthesis_count = 0;
510 unsigned brace_count = 0;
511 unsigned bracket_count = 0;
512 int end_token = type;
521 while(token.type != end_token ||
522 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
526 case '(': ++parenthesis_count; break;
527 case '{': ++brace_count; break;
528 case '[': ++bracket_count; break;
530 if(parenthesis_count > 0)
538 if(bracket_count > 0)
549 * Eat input tokens until an anchor is found.
551 static void eat_until_anchor(void) {
552 if(token.type == T_EOF)
554 while(token_anchor_set[token.type] == 0) {
555 if(token.type == '(' || token.type == '{' || token.type == '[')
556 eat_until_matching_token(token.type);
557 if(token.type == T_EOF)
563 static void eat_block(void) {
564 eat_until_matching_token('{');
565 if(token.type == '}')
570 * eat all token until a ';' is reached or a stop token is found.
572 static void eat_statement(void) {
573 eat_until_matching_token(';');
574 if(token.type == ';')
578 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
581 * Report a parse error because an expected token was not found.
583 static __attribute__((sentinel))
584 void parse_error_expected(const char *message, ...)
586 if(message != NULL) {
587 errorf(HERE, "%s", message);
590 va_start(ap, message);
591 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
596 * Report a type error.
598 static void type_error(const char *msg, const source_position_t *source_position,
601 errorf(source_position, "%s, but found type '%T'", msg, type);
605 * Report an incompatible type.
607 static void type_error_incompatible(const char *msg,
608 const source_position_t *source_position, type_t *type1, type_t *type2)
610 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
615 * Expect the the current token is the expected token.
616 * If not, generate an error, eat the current statement,
617 * and goto the end_error label.
619 #define expect(expected) \
621 if(UNLIKELY(token.type != (expected))) { \
622 parse_error_expected(NULL, (expected), NULL); \
623 add_anchor_token(expected); \
624 eat_until_anchor(); \
625 rem_anchor_token(expected); \
631 static void set_scope(scope_t *new_scope)
634 scope->last_declaration = last_declaration;
638 last_declaration = new_scope->last_declaration;
642 * Search a symbol in a given namespace and returns its declaration or
643 * NULL if this symbol was not found.
645 static declaration_t *get_declaration(const symbol_t *const symbol,
646 const namespace_t namespc)
648 declaration_t *declaration = symbol->declaration;
649 for( ; declaration != NULL; declaration = declaration->symbol_next) {
650 if(declaration->namespc == namespc)
658 * pushs an environment_entry on the environment stack and links the
659 * corresponding symbol to the new entry
661 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
663 symbol_t *symbol = declaration->symbol;
664 namespace_t namespc = (namespace_t) declaration->namespc;
666 /* replace/add declaration into declaration list of the symbol */
667 declaration_t *iter = symbol->declaration;
669 symbol->declaration = declaration;
671 declaration_t *iter_last = NULL;
672 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
673 /* replace an entry? */
674 if(iter->namespc == namespc) {
675 if(iter_last == NULL) {
676 symbol->declaration = declaration;
678 iter_last->symbol_next = declaration;
680 declaration->symbol_next = iter->symbol_next;
685 assert(iter_last->symbol_next == NULL);
686 iter_last->symbol_next = declaration;
690 /* remember old declaration */
692 entry.symbol = symbol;
693 entry.old_declaration = iter;
694 entry.namespc = (unsigned short) namespc;
695 ARR_APP1(stack_entry_t, *stack_ptr, entry);
698 static void environment_push(declaration_t *declaration)
700 assert(declaration->source_position.input_name != NULL);
701 assert(declaration->parent_scope != NULL);
702 stack_push(&environment_stack, declaration);
705 static void label_push(declaration_t *declaration)
707 declaration->parent_scope = ¤t_function->scope;
708 stack_push(&label_stack, declaration);
712 * pops symbols from the environment stack until @p new_top is the top element
714 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
716 stack_entry_t *stack = *stack_ptr;
717 size_t top = ARR_LEN(stack);
720 assert(new_top <= top);
724 for(i = top; i > new_top; --i) {
725 stack_entry_t *entry = &stack[i - 1];
727 declaration_t *old_declaration = entry->old_declaration;
728 symbol_t *symbol = entry->symbol;
729 namespace_t namespc = (namespace_t)entry->namespc;
731 /* replace/remove declaration */
732 declaration_t *declaration = symbol->declaration;
733 assert(declaration != NULL);
734 if(declaration->namespc == namespc) {
735 if(old_declaration == NULL) {
736 symbol->declaration = declaration->symbol_next;
738 symbol->declaration = old_declaration;
741 declaration_t *iter_last = declaration;
742 declaration_t *iter = declaration->symbol_next;
743 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
744 /* replace an entry? */
745 if(iter->namespc == namespc) {
746 assert(iter_last != NULL);
747 iter_last->symbol_next = old_declaration;
748 if(old_declaration != NULL) {
749 old_declaration->symbol_next = iter->symbol_next;
754 assert(iter != NULL);
758 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
761 static void environment_pop_to(size_t new_top)
763 stack_pop_to(&environment_stack, new_top);
766 static void label_pop_to(size_t new_top)
768 stack_pop_to(&label_stack, new_top);
772 static int get_rank(const type_t *type)
774 assert(!is_typeref(type));
775 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
776 * and esp. footnote 108). However we can't fold constants (yet), so we
777 * can't decide whether unsigned int is possible, while int always works.
778 * (unsigned int would be preferable when possible... for stuff like
779 * struct { enum { ... } bla : 4; } ) */
780 if(type->kind == TYPE_ENUM)
781 return ATOMIC_TYPE_INT;
783 assert(type->kind == TYPE_ATOMIC);
784 return type->atomic.akind;
787 static type_t *promote_integer(type_t *type)
789 if(type->kind == TYPE_BITFIELD)
790 type = type->bitfield.base_type;
792 if(get_rank(type) < ATOMIC_TYPE_INT)
799 * Create a cast expression.
801 * @param expression the expression to cast
802 * @param dest_type the destination type
804 static expression_t *create_cast_expression(expression_t *expression,
807 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
809 cast->unary.value = expression;
810 cast->base.type = dest_type;
816 * Check if a given expression represents the 0 pointer constant.
818 static bool is_null_pointer_constant(const expression_t *expression)
820 /* skip void* cast */
821 if(expression->kind == EXPR_UNARY_CAST
822 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
823 expression = expression->unary.value;
826 /* TODO: not correct yet, should be any constant integer expression
827 * which evaluates to 0 */
828 if (expression->kind != EXPR_CONST)
831 type_t *const type = skip_typeref(expression->base.type);
832 if (!is_type_integer(type))
835 return expression->conste.v.int_value == 0;
839 * Create an implicit cast expression.
841 * @param expression the expression to cast
842 * @param dest_type the destination type
844 static expression_t *create_implicit_cast(expression_t *expression,
847 type_t *const source_type = expression->base.type;
849 if (source_type == dest_type)
852 return create_cast_expression(expression, dest_type);
855 /** Implements the rules from § 6.5.16.1 */
856 static type_t *semantic_assign(type_t *orig_type_left,
857 const expression_t *const right,
859 const source_position_t *source_position)
861 type_t *const orig_type_right = right->base.type;
862 type_t *const type_left = skip_typeref(orig_type_left);
863 type_t *const type_right = skip_typeref(orig_type_right);
865 if(is_type_pointer(type_left)) {
866 if(is_null_pointer_constant(right)) {
867 return orig_type_left;
868 } else if(is_type_pointer(type_right)) {
869 type_t *points_to_left
870 = skip_typeref(type_left->pointer.points_to);
871 type_t *points_to_right
872 = skip_typeref(type_right->pointer.points_to);
874 /* the left type has all qualifiers from the right type */
875 unsigned missing_qualifiers
876 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
877 if(missing_qualifiers != 0) {
878 errorf(source_position,
879 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
880 return orig_type_left;
883 points_to_left = get_unqualified_type(points_to_left);
884 points_to_right = get_unqualified_type(points_to_right);
886 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
887 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
888 return orig_type_left;
891 if (!types_compatible(points_to_left, points_to_right)) {
892 warningf(source_position,
893 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
894 orig_type_left, context, right, orig_type_right);
897 return orig_type_left;
898 } else if(is_type_integer(type_right)) {
899 warningf(source_position,
900 "%s makes pointer '%T' from integer '%T' without a cast",
901 context, orig_type_left, orig_type_right);
902 return orig_type_left;
904 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
905 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
906 && is_type_pointer(type_right))) {
907 return orig_type_left;
908 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
909 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
910 type_t *const unqual_type_left = get_unqualified_type(type_left);
911 type_t *const unqual_type_right = get_unqualified_type(type_right);
912 if (types_compatible(unqual_type_left, unqual_type_right)) {
913 return orig_type_left;
915 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
916 warningf(source_position,
917 "%s makes integer '%T' from pointer '%T' without a cast",
918 context, orig_type_left, orig_type_right);
919 return orig_type_left;
922 if (!is_type_valid(type_left))
925 if (!is_type_valid(type_right))
926 return orig_type_right;
931 static expression_t *parse_constant_expression(void)
933 /* start parsing at precedence 7 (conditional expression) */
934 expression_t *result = parse_sub_expression(7);
936 if(!is_constant_expression(result)) {
937 errorf(&result->base.source_position,
938 "expression '%E' is not constant\n", result);
944 static expression_t *parse_assignment_expression(void)
946 /* start parsing at precedence 2 (assignment expression) */
947 return parse_sub_expression(2);
950 static type_t *make_global_typedef(const char *name, type_t *type)
952 symbol_t *const symbol = symbol_table_insert(name);
954 declaration_t *const declaration = allocate_declaration_zero();
955 declaration->namespc = NAMESPACE_NORMAL;
956 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
957 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
958 declaration->type = type;
959 declaration->symbol = symbol;
960 declaration->source_position = builtin_source_position;
962 record_declaration(declaration);
964 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
965 typedef_type->typedeft.declaration = declaration;
970 static string_t parse_string_literals(void)
972 assert(token.type == T_STRING_LITERAL);
973 string_t result = token.v.string;
977 while (token.type == T_STRING_LITERAL) {
978 result = concat_strings(&result, &token.v.string);
985 static const char *gnu_attribute_names[GNU_AK_LAST] = {
986 [GNU_AK_CONST] = "const",
987 [GNU_AK_VOLATILE] = "volatile",
988 [GNU_AK_CDECL] = "cdecl",
989 [GNU_AK_STDCALL] = "stdcall",
990 [GNU_AK_FASTCALL] = "fastcall",
991 [GNU_AK_DEPRECATED] = "deprecated",
992 [GNU_AK_NOINLINE] = "noinline",
993 [GNU_AK_NORETURN] = "noreturn",
994 [GNU_AK_NAKED] = "naked",
995 [GNU_AK_PURE] = "pure",
996 [GNU_AK_ALWAYS_INLINE] = "always_inline",
997 [GNU_AK_MALLOC] = "malloc",
998 [GNU_AK_WEAK] = "weak",
999 [GNU_AK_CONSTRUCTOR] = "constructor",
1000 [GNU_AK_DESTRUCTOR] = "destructor",
1001 [GNU_AK_NOTHROW] = "nothrow",
1002 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1003 [GNU_AK_COMMON] = "coommon",
1004 [GNU_AK_NOCOMMON] = "nocommon",
1005 [GNU_AK_PACKED] = "packed",
1006 [GNU_AK_SHARED] = "shared",
1007 [GNU_AK_NOTSHARED] = "notshared",
1008 [GNU_AK_USED] = "used",
1009 [GNU_AK_UNUSED] = "unused",
1010 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1011 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1012 [GNU_AK_LONGCALL] = "longcall",
1013 [GNU_AK_SHORTCALL] = "shortcall",
1014 [GNU_AK_LONG_CALL] = "long_call",
1015 [GNU_AK_SHORT_CALL] = "short_call",
1016 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1017 [GNU_AK_INTERRUPT] = "interrupt",
1018 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1019 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1020 [GNU_AK_NESTING] = "nesting",
1021 [GNU_AK_NEAR] = "near",
1022 [GNU_AK_FAR] = "far",
1023 [GNU_AK_SIGNAL] = "signal",
1024 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1025 [GNU_AK_TINY_DATA] = "tiny_data",
1026 [GNU_AK_SAVEALL] = "saveall",
1027 [GNU_AK_FLATTEN] = "flatten",
1028 [GNU_AK_SSEREGPARM] = "sseregparm",
1029 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1030 [GNU_AK_RETURN_TWICE] = "return_twice",
1031 [GNU_AK_MAY_ALIAS] = "may_alias",
1032 [GNU_AK_MS_STRUCT] = "ms_struct",
1033 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1034 [GNU_AK_DLLIMPORT] = "dllimport",
1035 [GNU_AK_DLLEXPORT] = "dllexport",
1036 [GNU_AK_ALIGNED] = "aligned",
1037 [GNU_AK_ALIAS] = "alias",
1038 [GNU_AK_SECTION] = "section",
1039 [GNU_AK_FORMAT] = "format",
1040 [GNU_AK_FORMAT_ARG] = "format_arg",
1041 [GNU_AK_WEAKREF] = "weakref",
1042 [GNU_AK_NONNULL] = "nonnull",
1043 [GNU_AK_TLS_MODEL] = "tls_model",
1044 [GNU_AK_VISIBILITY] = "visibility",
1045 [GNU_AK_REGPARM] = "regparm",
1046 [GNU_AK_MODEL] = "model",
1047 [GNU_AK_TRAP_EXIT] = "trap_exit",
1048 [GNU_AK_SP_SWITCH] = "sp_switch",
1049 [GNU_AK_SENTINEL] = "sentinel"
1053 * compare two string, ignoring double underscores on the second.
1055 static int strcmp_underscore(const char *s1, const char *s2) {
1056 if(s2[0] == '_' && s2[1] == '_') {
1058 size_t l1 = strlen(s1);
1059 if(l1 + 2 != strlen(s2)) {
1063 return strncmp(s1, s2, l1);
1065 return strcmp(s1, s2);
1069 * Allocate a new gnu temporal attribute.
1071 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1072 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1073 attribute->kind = kind;
1074 attribute->next = NULL;
1075 attribute->invalid = false;
1076 attribute->have_arguments = false;
1083 * parse one constant expression argument.
1085 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1086 expression_t *expression;
1087 add_anchor_token(')');
1088 expression = parse_constant_expression();
1089 rem_anchor_token(')');
1094 attribute->invalid = true;
1098 * parse a list of constant expressions arguments.
1100 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1101 expression_t *expression;
1102 add_anchor_token(')');
1103 add_anchor_token(',');
1105 expression = parse_constant_expression();
1106 if(token.type != ',')
1110 rem_anchor_token(',');
1111 rem_anchor_token(')');
1116 attribute->invalid = true;
1120 * parse one string literal argument.
1122 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1125 add_anchor_token('(');
1126 if(token.type != T_STRING_LITERAL) {
1127 parse_error_expected("while parsing attribute directive",
1128 T_STRING_LITERAL, NULL);
1131 *string = parse_string_literals();
1132 rem_anchor_token('(');
1136 attribute->invalid = true;
1140 * parse one tls model.
1142 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1143 static const char *tls_models[] = {
1149 string_t string = { NULL, 0 };
1150 parse_gnu_attribute_string_arg(attribute, &string);
1151 if(string.begin != NULL) {
1152 for(size_t i = 0; i < 4; ++i) {
1153 if(strcmp(tls_models[i], string.begin) == 0) {
1154 attribute->u.value = i;
1159 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1160 attribute->invalid = true;
1164 * parse one tls model.
1166 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1167 static const char *visibilities[] = {
1173 string_t string = { NULL, 0 };
1174 parse_gnu_attribute_string_arg(attribute, &string);
1175 if(string.begin != NULL) {
1176 for(size_t i = 0; i < 4; ++i) {
1177 if(strcmp(visibilities[i], string.begin) == 0) {
1178 attribute->u.value = i;
1183 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1184 attribute->invalid = true;
1188 * parse one (code) model.
1190 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1191 static const char *visibilities[] = {
1196 string_t string = { NULL, 0 };
1197 parse_gnu_attribute_string_arg(attribute, &string);
1198 if(string.begin != NULL) {
1199 for(int i = 0; i < 3; ++i) {
1200 if(strcmp(visibilities[i], string.begin) == 0) {
1201 attribute->u.value = i;
1206 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1207 attribute->invalid = true;
1211 * parse one interrupt argument.
1213 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1214 static const char *interrupts[] = {
1221 string_t string = { NULL, 0 };
1222 parse_gnu_attribute_string_arg(attribute, &string);
1223 if(string.begin != NULL) {
1224 for(size_t i = 0; i < 5; ++i) {
1225 if(strcmp(interrupts[i], string.begin) == 0) {
1226 attribute->u.value = i;
1231 errorf(HERE, "'%s' is an interrupt", string.begin);
1232 attribute->invalid = true;
1236 * parse ( identifier, const expression, const expression )
1238 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1239 static const char *format_names[] = {
1247 if(token.type != T_IDENTIFIER) {
1248 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1251 const char *name = token.v.symbol->string;
1252 for(i = 0; i < 4; ++i) {
1253 if(strcmp_underscore(format_names[i], name) == 0)
1257 if(warning.attribute)
1258 warningf(HERE, "'%s' is an unrecognized format function type", name);
1263 add_anchor_token(')');
1264 add_anchor_token(',');
1265 parse_constant_expression();
1266 rem_anchor_token(',');
1267 rem_anchor_token('(');
1270 add_anchor_token(')');
1271 parse_constant_expression();
1272 rem_anchor_token('(');
1276 attribute->u.value = true;
1280 * Parse one GNU attribute.
1282 * Note that attribute names can be specified WITH or WITHOUT
1283 * double underscores, ie const or __const__.
1285 * The following attributes are parsed without arguments
1310 * no_instrument_function
1311 * warn_unused_result
1328 * externally_visible
1336 * The following attributes are parsed with arguments
1337 * aligned( const expression )
1338 * alias( string literal )
1339 * section( string literal )
1340 * format( identifier, const expression, const expression )
1341 * format_arg( const expression )
1342 * tls_model( string literal )
1343 * visibility( string literal )
1344 * regparm( const expression )
1345 * model( string leteral )
1346 * trap_exit( const expression )
1347 * sp_switch( string literal )
1349 * The following attributes might have arguments
1350 * weak_ref( string literal )
1351 * non_null( const expression // ',' )
1352 * interrupt( string literal )
1353 * sentinel( constant expression )
1355 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1357 gnu_attribute_t *head = *attributes;
1358 gnu_attribute_t *last = *attributes;
1359 gnu_attribute_t *attribute;
1361 eat(T___attribute__);
1365 if(token.type != ')') {
1366 /* find the end of the list */
1368 while(last->next != NULL)
1372 /* non-empty attribute list */
1375 if(token.type == T_const) {
1377 } else if(token.type == T_volatile) {
1379 } else if(token.type == T_cdecl) {
1380 /* __attribute__((cdecl)), WITH ms mode */
1382 } else if(token.type != T_IDENTIFIER) {
1383 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1386 const symbol_t *sym = token.v.symbol;
1391 for(i = 0; i < GNU_AK_LAST; ++i) {
1392 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1395 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1398 if(kind == GNU_AK_LAST) {
1399 if(warning.attribute)
1400 warningf(HERE, "'%s' attribute directive ignored", name);
1402 /* skip possible arguments */
1403 if(token.type == '(') {
1404 eat_until_matching_token(')');
1407 /* check for arguments */
1408 attribute = allocate_gnu_attribute(kind);
1409 if(token.type == '(') {
1411 if(token.type == ')') {
1412 /* empty args are allowed */
1415 attribute->have_arguments = true;
1420 case GNU_AK_VOLATILE:
1422 case GNU_AK_STDCALL:
1423 case GNU_AK_FASTCALL:
1424 case GNU_AK_DEPRECATED:
1425 case GNU_AK_NOINLINE:
1426 case GNU_AK_NORETURN:
1429 case GNU_AK_ALWAYS_INLINE:
1432 case GNU_AK_CONSTRUCTOR:
1433 case GNU_AK_DESTRUCTOR:
1434 case GNU_AK_NOTHROW:
1435 case GNU_AK_TRANSPARENT_UNION:
1437 case GNU_AK_NOCOMMON:
1440 case GNU_AK_NOTSHARED:
1443 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1444 case GNU_AK_WARN_UNUSED_RESULT:
1445 case GNU_AK_LONGCALL:
1446 case GNU_AK_SHORTCALL:
1447 case GNU_AK_LONG_CALL:
1448 case GNU_AK_SHORT_CALL:
1449 case GNU_AK_FUNCTION_VECTOR:
1450 case GNU_AK_INTERRUPT_HANDLER:
1451 case GNU_AK_NMI_HANDLER:
1452 case GNU_AK_NESTING:
1456 case GNU_AK_EIGTHBIT_DATA:
1457 case GNU_AK_TINY_DATA:
1458 case GNU_AK_SAVEALL:
1459 case GNU_AK_FLATTEN:
1460 case GNU_AK_SSEREGPARM:
1461 case GNU_AK_EXTERNALLY_VISIBLE:
1462 case GNU_AK_RETURN_TWICE:
1463 case GNU_AK_MAY_ALIAS:
1464 case GNU_AK_MS_STRUCT:
1465 case GNU_AK_GCC_STRUCT:
1466 case GNU_AK_DLLIMPORT:
1467 case GNU_AK_DLLEXPORT:
1468 if(attribute->have_arguments) {
1469 /* should have no arguments */
1470 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1471 eat_until_matching_token('(');
1472 /* we have already consumed '(', so we stop before ')', eat it */
1474 attribute->invalid = true;
1478 case GNU_AK_ALIGNED:
1479 case GNU_AK_FORMAT_ARG:
1480 case GNU_AK_REGPARM:
1481 case GNU_AK_TRAP_EXIT:
1482 if(!attribute->have_arguments) {
1483 /* should have arguments */
1484 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1485 attribute->invalid = true;
1487 parse_gnu_attribute_const_arg(attribute);
1490 case GNU_AK_SECTION:
1491 case GNU_AK_SP_SWITCH:
1492 if(!attribute->have_arguments) {
1493 /* should have arguments */
1494 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1495 attribute->invalid = true;
1497 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1500 if(!attribute->have_arguments) {
1501 /* should have arguments */
1502 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1503 attribute->invalid = true;
1505 parse_gnu_attribute_format_args(attribute);
1507 case GNU_AK_WEAKREF:
1508 /* may have one string argument */
1509 if(attribute->have_arguments)
1510 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1512 case GNU_AK_NONNULL:
1513 if(attribute->have_arguments)
1514 parse_gnu_attribute_const_arg_list(attribute);
1516 case GNU_AK_TLS_MODEL:
1517 if(!attribute->have_arguments) {
1518 /* should have arguments */
1519 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1521 parse_gnu_attribute_tls_model_arg(attribute);
1523 case GNU_AK_VISIBILITY:
1524 if(!attribute->have_arguments) {
1525 /* should have arguments */
1526 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1528 parse_gnu_attribute_visibility_arg(attribute);
1531 if(!attribute->have_arguments) {
1532 /* should have arguments */
1533 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1535 parse_gnu_attribute_model_arg(attribute);
1536 case GNU_AK_INTERRUPT:
1537 /* may have one string argument */
1538 if(attribute->have_arguments)
1539 parse_gnu_attribute_interrupt_arg(attribute);
1541 case GNU_AK_SENTINEL:
1542 /* may have one string argument */
1543 if(attribute->have_arguments)
1544 parse_gnu_attribute_const_arg(attribute);
1547 /* already handled */
1551 if(attribute != NULL) {
1553 last->next = attribute;
1556 head = last = attribute;
1560 if(token.type != ',')
1572 * Parse GNU attributes.
1574 static void parse_attributes(gnu_attribute_t **attributes)
1577 switch(token.type) {
1578 case T___attribute__: {
1579 parse_gnu_attribute(attributes);
1585 if(token.type != T_STRING_LITERAL) {
1586 parse_error_expected("while parsing assembler attribute",
1587 T_STRING_LITERAL, NULL);
1588 eat_until_matching_token('(');
1591 parse_string_literals();
1596 goto attributes_finished;
1600 attributes_finished:
1605 static designator_t *parse_designation(void)
1607 designator_t *result = NULL;
1608 designator_t *last = NULL;
1611 designator_t *designator;
1612 switch(token.type) {
1614 designator = allocate_ast_zero(sizeof(designator[0]));
1615 designator->source_position = token.source_position;
1617 add_anchor_token(']');
1618 designator->array_index = parse_constant_expression();
1619 rem_anchor_token(']');
1623 designator = allocate_ast_zero(sizeof(designator[0]));
1624 designator->source_position = token.source_position;
1626 if(token.type != T_IDENTIFIER) {
1627 parse_error_expected("while parsing designator",
1628 T_IDENTIFIER, NULL);
1631 designator->symbol = token.v.symbol;
1639 assert(designator != NULL);
1641 last->next = designator;
1643 result = designator;
1651 static initializer_t *initializer_from_string(array_type_t *type,
1652 const string_t *const string)
1654 /* TODO: check len vs. size of array type */
1657 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1658 initializer->string.string = *string;
1663 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1664 wide_string_t *const string)
1666 /* TODO: check len vs. size of array type */
1669 initializer_t *const initializer =
1670 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1671 initializer->wide_string.string = *string;
1677 * Build an initializer from a given expression.
1679 static initializer_t *initializer_from_expression(type_t *orig_type,
1680 expression_t *expression)
1682 /* TODO check that expression is a constant expression */
1684 /* § 6.7.8.14/15 char array may be initialized by string literals */
1685 type_t *type = skip_typeref(orig_type);
1686 type_t *expr_type_orig = expression->base.type;
1687 type_t *expr_type = skip_typeref(expr_type_orig);
1688 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1689 array_type_t *const array_type = &type->array;
1690 type_t *const element_type = skip_typeref(array_type->element_type);
1692 if (element_type->kind == TYPE_ATOMIC) {
1693 atomic_type_kind_t akind = element_type->atomic.akind;
1694 switch (expression->kind) {
1695 case EXPR_STRING_LITERAL:
1696 if (akind == ATOMIC_TYPE_CHAR
1697 || akind == ATOMIC_TYPE_SCHAR
1698 || akind == ATOMIC_TYPE_UCHAR) {
1699 return initializer_from_string(array_type,
1700 &expression->string.value);
1703 case EXPR_WIDE_STRING_LITERAL: {
1704 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1705 if (get_unqualified_type(element_type) == bare_wchar_type) {
1706 return initializer_from_wide_string(array_type,
1707 &expression->wide_string.value);
1717 type_t *const res_type = semantic_assign(type, expression, "initializer",
1718 &expression->base.source_position);
1719 if (res_type == NULL)
1722 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1723 result->value.value = create_implicit_cast(expression, res_type);
1729 * Checks if a given expression can be used as an constant initializer.
1731 static bool is_initializer_constant(const expression_t *expression)
1733 return is_constant_expression(expression)
1734 || is_address_constant(expression);
1738 * Parses an scalar initializer.
1740 * § 6.7.8.11; eat {} without warning
1742 static initializer_t *parse_scalar_initializer(type_t *type,
1743 bool must_be_constant)
1745 /* there might be extra {} hierarchies */
1747 while(token.type == '{') {
1750 warningf(HERE, "extra curly braces around scalar initializer");
1755 expression_t *expression = parse_assignment_expression();
1756 if(must_be_constant && !is_initializer_constant(expression)) {
1757 errorf(&expression->base.source_position,
1758 "Initialisation expression '%E' is not constant\n",
1762 initializer_t *initializer = initializer_from_expression(type, expression);
1764 if(initializer == NULL) {
1765 errorf(&expression->base.source_position,
1766 "expression '%E' (type '%T') doesn't match expected type '%T'",
1767 expression, expression->base.type, type);
1772 bool additional_warning_displayed = false;
1774 if(token.type == ',') {
1777 if(token.type != '}') {
1778 if(!additional_warning_displayed) {
1779 warningf(HERE, "additional elements in scalar initializer");
1780 additional_warning_displayed = true;
1791 * An entry in the type path.
1793 typedef struct type_path_entry_t type_path_entry_t;
1794 struct type_path_entry_t {
1795 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1797 size_t index; /**< For array types: the current index. */
1798 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1803 * A type path expression a position inside compound or array types.
1805 typedef struct type_path_t type_path_t;
1806 struct type_path_t {
1807 type_path_entry_t *path; /**< An flexible array containing the current path. */
1808 type_t *top_type; /**< type of the element the path points */
1809 size_t max_index; /**< largest index in outermost array */
1813 * Prints a type path for debugging.
1815 static __attribute__((unused)) void debug_print_type_path(
1816 const type_path_t *path)
1818 size_t len = ARR_LEN(path->path);
1820 for(size_t i = 0; i < len; ++i) {
1821 const type_path_entry_t *entry = & path->path[i];
1823 type_t *type = skip_typeref(entry->type);
1824 if(is_type_compound(type)) {
1825 /* in gcc mode structs can have no members */
1826 if(entry->v.compound_entry == NULL) {
1830 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1831 } else if(is_type_array(type)) {
1832 fprintf(stderr, "[%zd]", entry->v.index);
1834 fprintf(stderr, "-INVALID-");
1837 if(path->top_type != NULL) {
1838 fprintf(stderr, " (");
1839 print_type(path->top_type);
1840 fprintf(stderr, ")");
1845 * Return the top type path entry, ie. in a path
1846 * (type).a.b returns the b.
1848 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1850 size_t len = ARR_LEN(path->path);
1852 return &path->path[len-1];
1856 * Enlarge the type path by an (empty) element.
1858 static type_path_entry_t *append_to_type_path(type_path_t *path)
1860 size_t len = ARR_LEN(path->path);
1861 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1863 type_path_entry_t *result = & path->path[len];
1864 memset(result, 0, sizeof(result[0]));
1869 * Descending into a sub-type. Enter the scope of the current
1872 static void descend_into_subtype(type_path_t *path)
1874 type_t *orig_top_type = path->top_type;
1875 type_t *top_type = skip_typeref(orig_top_type);
1877 assert(is_type_compound(top_type) || is_type_array(top_type));
1879 type_path_entry_t *top = append_to_type_path(path);
1880 top->type = top_type;
1882 if(is_type_compound(top_type)) {
1883 declaration_t *declaration = top_type->compound.declaration;
1884 declaration_t *entry = declaration->scope.declarations;
1885 top->v.compound_entry = entry;
1888 path->top_type = entry->type;
1890 path->top_type = NULL;
1893 assert(is_type_array(top_type));
1896 path->top_type = top_type->array.element_type;
1901 * Pop an entry from the given type path, ie. returning from
1902 * (type).a.b to (type).a
1904 static void ascend_from_subtype(type_path_t *path)
1906 type_path_entry_t *top = get_type_path_top(path);
1908 path->top_type = top->type;
1910 size_t len = ARR_LEN(path->path);
1911 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1915 * Pop entries from the given type path until the given
1916 * path level is reached.
1918 static void ascend_to(type_path_t *path, size_t top_path_level)
1920 size_t len = ARR_LEN(path->path);
1922 while(len > top_path_level) {
1923 ascend_from_subtype(path);
1924 len = ARR_LEN(path->path);
1928 static bool walk_designator(type_path_t *path, const designator_t *designator,
1929 bool used_in_offsetof)
1931 for( ; designator != NULL; designator = designator->next) {
1932 type_path_entry_t *top = get_type_path_top(path);
1933 type_t *orig_type = top->type;
1935 type_t *type = skip_typeref(orig_type);
1937 if(designator->symbol != NULL) {
1938 symbol_t *symbol = designator->symbol;
1939 if(!is_type_compound(type)) {
1940 if(is_type_valid(type)) {
1941 errorf(&designator->source_position,
1942 "'.%Y' designator used for non-compound type '%T'",
1948 declaration_t *declaration = type->compound.declaration;
1949 declaration_t *iter = declaration->scope.declarations;
1950 for( ; iter != NULL; iter = iter->next) {
1951 if(iter->symbol == symbol) {
1956 errorf(&designator->source_position,
1957 "'%T' has no member named '%Y'", orig_type, symbol);
1960 if(used_in_offsetof) {
1961 type_t *real_type = skip_typeref(iter->type);
1962 if(real_type->kind == TYPE_BITFIELD) {
1963 errorf(&designator->source_position,
1964 "offsetof designator '%Y' may not specify bitfield",
1970 top->type = orig_type;
1971 top->v.compound_entry = iter;
1972 orig_type = iter->type;
1974 expression_t *array_index = designator->array_index;
1975 assert(designator->array_index != NULL);
1977 if(!is_type_array(type)) {
1978 if(is_type_valid(type)) {
1979 errorf(&designator->source_position,
1980 "[%E] designator used for non-array type '%T'",
1981 array_index, orig_type);
1985 if(!is_type_valid(array_index->base.type)) {
1989 long index = fold_constant(array_index);
1990 if(!used_in_offsetof) {
1992 errorf(&designator->source_position,
1993 "array index [%E] must be positive", array_index);
1996 if(type->array.size_constant == true) {
1997 long array_size = type->array.size;
1998 if(index >= array_size) {
1999 errorf(&designator->source_position,
2000 "designator [%E] (%d) exceeds array size %d",
2001 array_index, index, array_size);
2007 top->type = orig_type;
2008 top->v.index = (size_t) index;
2009 orig_type = type->array.element_type;
2011 path->top_type = orig_type;
2013 if(designator->next != NULL) {
2014 descend_into_subtype(path);
2023 static void advance_current_object(type_path_t *path, size_t top_path_level)
2025 type_path_entry_t *top = get_type_path_top(path);
2027 type_t *type = skip_typeref(top->type);
2028 if(is_type_union(type)) {
2029 /* in unions only the first element is initialized */
2030 top->v.compound_entry = NULL;
2031 } else if(is_type_struct(type)) {
2032 declaration_t *entry = top->v.compound_entry;
2034 entry = entry->next;
2035 top->v.compound_entry = entry;
2037 path->top_type = entry->type;
2041 assert(is_type_array(type));
2045 if(!type->array.size_constant || top->v.index < type->array.size) {
2050 /* we're past the last member of the current sub-aggregate, try if we
2051 * can ascend in the type hierarchy and continue with another subobject */
2052 size_t len = ARR_LEN(path->path);
2054 if(len > top_path_level) {
2055 ascend_from_subtype(path);
2056 advance_current_object(path, top_path_level);
2058 path->top_type = NULL;
2063 * skip until token is found.
2065 static void skip_until(int type) {
2066 while(token.type != type) {
2067 if(token.type == T_EOF)
2074 * skip any {...} blocks until a closing braket is reached.
2076 static void skip_initializers(void)
2078 if(token.type == '{')
2081 while(token.type != '}') {
2082 if(token.type == T_EOF)
2084 if(token.type == '{') {
2092 static initializer_t *create_empty_initializer(void)
2094 static initializer_t empty_initializer
2095 = { .list = { { INITIALIZER_LIST }, 0 } };
2096 return &empty_initializer;
2100 * Parse a part of an initialiser for a struct or union,
2102 static initializer_t *parse_sub_initializer(type_path_t *path,
2103 type_t *outer_type, size_t top_path_level,
2104 parse_initializer_env_t *env)
2106 if(token.type == '}') {
2107 /* empty initializer */
2108 return create_empty_initializer();
2111 type_t *orig_type = path->top_type;
2112 type_t *type = NULL;
2114 if (orig_type == NULL) {
2115 /* We are initializing an empty compound. */
2117 type = skip_typeref(orig_type);
2119 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2120 * initializers in this case. */
2121 if(!is_type_valid(type)) {
2122 skip_initializers();
2123 return create_empty_initializer();
2127 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2130 designator_t *designator = NULL;
2131 if(token.type == '.' || token.type == '[') {
2132 designator = parse_designation();
2134 /* reset path to toplevel, evaluate designator from there */
2135 ascend_to(path, top_path_level);
2136 if(!walk_designator(path, designator, false)) {
2137 /* can't continue after designation error */
2141 initializer_t *designator_initializer
2142 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2143 designator_initializer->designator.designator = designator;
2144 ARR_APP1(initializer_t*, initializers, designator_initializer);
2149 if(token.type == '{') {
2150 if(type != NULL && is_type_scalar(type)) {
2151 sub = parse_scalar_initializer(type, env->must_be_constant);
2155 if (env->declaration != NULL)
2156 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2157 env->declaration->symbol);
2159 errorf(HERE, "extra brace group at end of initializer");
2161 descend_into_subtype(path);
2163 add_anchor_token('}');
2164 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2166 rem_anchor_token('}');
2169 ascend_from_subtype(path);
2173 goto error_parse_next;
2177 /* must be an expression */
2178 expression_t *expression = parse_assignment_expression();
2180 if(env->must_be_constant && !is_initializer_constant(expression)) {
2181 errorf(&expression->base.source_position,
2182 "Initialisation expression '%E' is not constant\n",
2187 /* we are already outside, ... */
2191 /* handle { "string" } special case */
2192 if((expression->kind == EXPR_STRING_LITERAL
2193 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2194 && outer_type != NULL) {
2195 sub = initializer_from_expression(outer_type, expression);
2197 if(token.type == ',') {
2200 if(token.type != '}') {
2201 warningf(HERE, "excessive elements in initializer for type '%T'",
2204 /* TODO: eat , ... */
2209 /* descend into subtypes until expression matches type */
2211 orig_type = path->top_type;
2212 type = skip_typeref(orig_type);
2214 sub = initializer_from_expression(orig_type, expression);
2218 if(!is_type_valid(type)) {
2221 if(is_type_scalar(type)) {
2222 errorf(&expression->base.source_position,
2223 "expression '%E' doesn't match expected type '%T'",
2224 expression, orig_type);
2228 descend_into_subtype(path);
2232 /* update largest index of top array */
2233 const type_path_entry_t *first = &path->path[0];
2234 type_t *first_type = first->type;
2235 first_type = skip_typeref(first_type);
2236 if(is_type_array(first_type)) {
2237 size_t index = first->v.index;
2238 if(index > path->max_index)
2239 path->max_index = index;
2243 /* append to initializers list */
2244 ARR_APP1(initializer_t*, initializers, sub);
2247 if(env->declaration != NULL)
2248 warningf(HERE, "excess elements in struct initializer for '%Y'",
2249 env->declaration->symbol);
2251 warningf(HERE, "excess elements in struct initializer");
2255 if(token.type == '}') {
2259 if(token.type == '}') {
2264 /* advance to the next declaration if we are not at the end */
2265 advance_current_object(path, top_path_level);
2266 orig_type = path->top_type;
2267 if(orig_type != NULL)
2268 type = skip_typeref(orig_type);
2274 size_t len = ARR_LEN(initializers);
2275 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2276 initializer_t *result = allocate_ast_zero(size);
2277 result->kind = INITIALIZER_LIST;
2278 result->list.len = len;
2279 memcpy(&result->list.initializers, initializers,
2280 len * sizeof(initializers[0]));
2282 DEL_ARR_F(initializers);
2283 ascend_to(path, top_path_level);
2288 skip_initializers();
2289 DEL_ARR_F(initializers);
2290 ascend_to(path, top_path_level);
2295 * Parses an initializer. Parsers either a compound literal
2296 * (env->declaration == NULL) or an initializer of a declaration.
2298 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2300 type_t *type = skip_typeref(env->type);
2301 initializer_t *result = NULL;
2304 if(is_type_scalar(type)) {
2305 result = parse_scalar_initializer(type, env->must_be_constant);
2306 } else if(token.type == '{') {
2310 memset(&path, 0, sizeof(path));
2311 path.top_type = env->type;
2312 path.path = NEW_ARR_F(type_path_entry_t, 0);
2314 descend_into_subtype(&path);
2316 add_anchor_token('}');
2317 result = parse_sub_initializer(&path, env->type, 1, env);
2318 rem_anchor_token('}');
2320 max_index = path.max_index;
2321 DEL_ARR_F(path.path);
2325 /* parse_scalar_initializer() also works in this case: we simply
2326 * have an expression without {} around it */
2327 result = parse_scalar_initializer(type, env->must_be_constant);
2330 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2331 * the array type size */
2332 if(is_type_array(type) && type->array.size_expression == NULL
2333 && result != NULL) {
2335 switch (result->kind) {
2336 case INITIALIZER_LIST:
2337 size = max_index + 1;
2340 case INITIALIZER_STRING:
2341 size = result->string.string.size;
2344 case INITIALIZER_WIDE_STRING:
2345 size = result->wide_string.string.size;
2349 internal_errorf(HERE, "invalid initializer type");
2352 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2353 cnst->base.type = type_size_t;
2354 cnst->conste.v.int_value = size;
2356 type_t *new_type = duplicate_type(type);
2358 new_type->array.size_expression = cnst;
2359 new_type->array.size_constant = true;
2360 new_type->array.size = size;
2361 env->type = new_type;
2369 static declaration_t *append_declaration(declaration_t *declaration);
2371 static declaration_t *parse_compound_type_specifier(bool is_struct)
2373 gnu_attribute_t *attributes = NULL;
2380 symbol_t *symbol = NULL;
2381 declaration_t *declaration = NULL;
2383 if (token.type == T___attribute__) {
2384 parse_attributes(&attributes);
2387 if(token.type == T_IDENTIFIER) {
2388 symbol = token.v.symbol;
2392 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2394 declaration = get_declaration(symbol, NAMESPACE_UNION);
2396 } else if(token.type != '{') {
2398 parse_error_expected("while parsing struct type specifier",
2399 T_IDENTIFIER, '{', NULL);
2401 parse_error_expected("while parsing union type specifier",
2402 T_IDENTIFIER, '{', NULL);
2408 if(declaration == NULL) {
2409 declaration = allocate_declaration_zero();
2410 declaration->namespc =
2411 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2412 declaration->source_position = token.source_position;
2413 declaration->symbol = symbol;
2414 declaration->parent_scope = scope;
2415 if (symbol != NULL) {
2416 environment_push(declaration);
2418 append_declaration(declaration);
2421 if(token.type == '{') {
2422 if(declaration->init.is_defined) {
2423 assert(symbol != NULL);
2424 errorf(HERE, "multiple definitions of '%s %Y'",
2425 is_struct ? "struct" : "union", symbol);
2426 declaration->scope.declarations = NULL;
2428 declaration->init.is_defined = true;
2430 parse_compound_type_entries(declaration);
2431 parse_attributes(&attributes);
2437 static void parse_enum_entries(type_t *const enum_type)
2441 if(token.type == '}') {
2443 errorf(HERE, "empty enum not allowed");
2447 add_anchor_token('}');
2449 if(token.type != T_IDENTIFIER) {
2450 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2452 rem_anchor_token('}');
2456 declaration_t *const entry = allocate_declaration_zero();
2457 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2458 entry->type = enum_type;
2459 entry->symbol = token.v.symbol;
2460 entry->source_position = token.source_position;
2463 if(token.type == '=') {
2465 expression_t *value = parse_constant_expression();
2467 value = create_implicit_cast(value, enum_type);
2468 entry->init.enum_value = value;
2473 record_declaration(entry);
2475 if(token.type != ',')
2478 } while(token.type != '}');
2479 rem_anchor_token('}');
2487 static type_t *parse_enum_specifier(void)
2489 gnu_attribute_t *attributes = NULL;
2490 declaration_t *declaration;
2494 if(token.type == T_IDENTIFIER) {
2495 symbol = token.v.symbol;
2498 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2499 } else if(token.type != '{') {
2500 parse_error_expected("while parsing enum type specifier",
2501 T_IDENTIFIER, '{', NULL);
2508 if(declaration == NULL) {
2509 declaration = allocate_declaration_zero();
2510 declaration->namespc = NAMESPACE_ENUM;
2511 declaration->source_position = token.source_position;
2512 declaration->symbol = symbol;
2513 declaration->parent_scope = scope;
2516 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2517 type->enumt.declaration = declaration;
2519 if(token.type == '{') {
2520 if(declaration->init.is_defined) {
2521 errorf(HERE, "multiple definitions of enum %Y", symbol);
2523 if (symbol != NULL) {
2524 environment_push(declaration);
2526 append_declaration(declaration);
2527 declaration->init.is_defined = 1;
2529 parse_enum_entries(type);
2530 parse_attributes(&attributes);
2537 * if a symbol is a typedef to another type, return true
2539 static bool is_typedef_symbol(symbol_t *symbol)
2541 const declaration_t *const declaration =
2542 get_declaration(symbol, NAMESPACE_NORMAL);
2544 declaration != NULL &&
2545 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2548 static type_t *parse_typeof(void)
2555 add_anchor_token(')');
2557 expression_t *expression = NULL;
2560 switch(token.type) {
2561 case T___extension__:
2562 /* this can be a prefix to a typename or an expression */
2563 /* we simply eat it now. */
2566 } while(token.type == T___extension__);
2570 if(is_typedef_symbol(token.v.symbol)) {
2571 type = parse_typename();
2573 expression = parse_expression();
2574 type = expression->base.type;
2579 type = parse_typename();
2583 expression = parse_expression();
2584 type = expression->base.type;
2588 rem_anchor_token(')');
2591 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2592 typeof_type->typeoft.expression = expression;
2593 typeof_type->typeoft.typeof_type = type;
2601 SPECIFIER_SIGNED = 1 << 0,
2602 SPECIFIER_UNSIGNED = 1 << 1,
2603 SPECIFIER_LONG = 1 << 2,
2604 SPECIFIER_INT = 1 << 3,
2605 SPECIFIER_DOUBLE = 1 << 4,
2606 SPECIFIER_CHAR = 1 << 5,
2607 SPECIFIER_SHORT = 1 << 6,
2608 SPECIFIER_LONG_LONG = 1 << 7,
2609 SPECIFIER_FLOAT = 1 << 8,
2610 SPECIFIER_BOOL = 1 << 9,
2611 SPECIFIER_VOID = 1 << 10,
2612 SPECIFIER_INT8 = 1 << 11,
2613 SPECIFIER_INT16 = 1 << 12,
2614 SPECIFIER_INT32 = 1 << 13,
2615 SPECIFIER_INT64 = 1 << 14,
2616 SPECIFIER_INT128 = 1 << 15,
2617 #ifdef PROVIDE_COMPLEX
2618 SPECIFIER_COMPLEX = 1 << 16,
2619 SPECIFIER_IMAGINARY = 1 << 17,
2623 static type_t *create_builtin_type(symbol_t *const symbol,
2624 type_t *const real_type)
2626 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2627 type->builtin.symbol = symbol;
2628 type->builtin.real_type = real_type;
2630 type_t *result = typehash_insert(type);
2631 if (type != result) {
2638 static type_t *get_typedef_type(symbol_t *symbol)
2640 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2641 if(declaration == NULL
2642 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2645 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2646 type->typedeft.declaration = declaration;
2652 * check for the allowed MS alignment values.
2654 static bool check_elignment_value(long long intvalue) {
2655 if(intvalue < 1 || intvalue > 8192) {
2656 errorf(HERE, "illegal alignment value");
2659 unsigned v = (unsigned)intvalue;
2660 for(unsigned i = 1; i <= 8192; i += i) {
2664 errorf(HERE, "alignment must be power of two");
2668 #define DET_MOD(name, tag) do { \
2669 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2670 *modifiers |= tag; \
2673 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2675 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2678 if(token.type == T_restrict) {
2680 DET_MOD(restrict, DM_RESTRICT);
2682 } else if(token.type != T_IDENTIFIER)
2684 symbol_t *symbol = token.v.symbol;
2685 if(symbol == sym_align) {
2688 if(token.type != T_INTEGER)
2690 if(check_elignment_value(token.v.intvalue)) {
2691 if(specifiers->alignment != 0)
2692 warningf(HERE, "align used more than once");
2693 specifiers->alignment = (unsigned char)token.v.intvalue;
2697 } else if(symbol == sym_allocate) {
2700 if(token.type != T_IDENTIFIER)
2702 (void)token.v.symbol;
2704 } else if(symbol == sym_dllimport) {
2706 DET_MOD(dllimport, DM_DLLIMPORT);
2707 } else if(symbol == sym_dllexport) {
2709 DET_MOD(dllexport, DM_DLLEXPORT);
2710 } else if(symbol == sym_thread) {
2712 DET_MOD(thread, DM_THREAD);
2713 } else if(symbol == sym_naked) {
2715 DET_MOD(naked, DM_NAKED);
2716 } else if(symbol == sym_noinline) {
2718 DET_MOD(noinline, DM_NOINLINE);
2719 } else if(symbol == sym_noreturn) {
2721 DET_MOD(noreturn, DM_NORETURN);
2722 } else if(symbol == sym_nothrow) {
2724 DET_MOD(nothrow, DM_NOTHROW);
2725 } else if(symbol == sym_novtable) {
2727 DET_MOD(novtable, DM_NOVTABLE);
2728 } else if(symbol == sym_property) {
2732 bool is_get = false;
2733 if(token.type != T_IDENTIFIER)
2735 if(token.v.symbol == sym_get) {
2737 } else if(token.v.symbol == sym_put) {
2739 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2744 if(token.type != T_IDENTIFIER)
2747 if(specifiers->get_property_sym != NULL) {
2748 errorf(HERE, "get property name already specified");
2750 specifiers->get_property_sym = token.v.symbol;
2753 if(specifiers->put_property_sym != NULL) {
2754 errorf(HERE, "put property name already specified");
2756 specifiers->put_property_sym = token.v.symbol;
2760 if(token.type == ',') {
2767 } else if(symbol == sym_selectany) {
2769 DET_MOD(selectany, DM_SELECTANY);
2770 } else if(symbol == sym_uuid) {
2773 if(token.type != T_STRING_LITERAL)
2777 } else if(symbol == sym_deprecated) {
2779 if(specifiers->deprecated != 0)
2780 warningf(HERE, "deprecated used more than once");
2781 specifiers->deprecated = 1;
2782 if(token.type == '(') {
2784 if(token.type == T_STRING_LITERAL) {
2785 specifiers->deprecated_string = token.v.string.begin;
2788 errorf(HERE, "string literal expected");
2792 } else if(symbol == sym_noalias) {
2794 DET_MOD(noalias, DM_NOALIAS);
2796 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2798 if(token.type == '(')
2802 if (token.type == ',')
2809 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2811 type_t *type = NULL;
2812 unsigned type_qualifiers = 0;
2813 unsigned type_specifiers = 0;
2816 specifiers->source_position = token.source_position;
2819 switch(token.type) {
2822 #define MATCH_STORAGE_CLASS(token, class) \
2824 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2825 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2827 specifiers->declared_storage_class = class; \
2831 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2832 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2833 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2834 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2835 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2840 add_anchor_token(')');
2841 parse_microsoft_extended_decl_modifier(specifiers);
2842 rem_anchor_token(')');
2847 switch (specifiers->declared_storage_class) {
2848 case STORAGE_CLASS_NONE:
2849 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2852 case STORAGE_CLASS_EXTERN:
2853 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2856 case STORAGE_CLASS_STATIC:
2857 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2861 errorf(HERE, "multiple storage classes in declaration specifiers");
2867 /* type qualifiers */
2868 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2870 type_qualifiers |= qualifier; \
2874 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2875 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2876 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2877 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2878 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2879 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2880 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2881 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2883 case T___extension__:
2888 /* type specifiers */
2889 #define MATCH_SPECIFIER(token, specifier, name) \
2892 if(type_specifiers & specifier) { \
2893 errorf(HERE, "multiple " name " type specifiers given"); \
2895 type_specifiers |= specifier; \
2899 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2900 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2901 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2902 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2903 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2904 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2905 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2906 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2907 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2908 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2909 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2910 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2911 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2912 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2913 #ifdef PROVIDE_COMPLEX
2914 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2915 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2917 case T__forceinline:
2918 /* only in microsoft mode */
2919 specifiers->decl_modifiers |= DM_FORCEINLINE;
2923 specifiers->is_inline = true;
2928 if(type_specifiers & SPECIFIER_LONG_LONG) {
2929 errorf(HERE, "multiple type specifiers given");
2930 } else if(type_specifiers & SPECIFIER_LONG) {
2931 type_specifiers |= SPECIFIER_LONG_LONG;
2933 type_specifiers |= SPECIFIER_LONG;
2938 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2940 type->compound.declaration = parse_compound_type_specifier(true);
2944 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2946 type->compound.declaration = parse_compound_type_specifier(false);
2950 type = parse_enum_specifier();
2953 type = parse_typeof();
2955 case T___builtin_va_list:
2956 type = duplicate_type(type_valist);
2960 case T___attribute__:
2961 parse_attributes(&specifiers->gnu_attributes);
2964 case T_IDENTIFIER: {
2965 /* only parse identifier if we haven't found a type yet */
2966 if(type != NULL || type_specifiers != 0)
2967 goto finish_specifiers;
2969 type_t *typedef_type = get_typedef_type(token.v.symbol);
2971 if(typedef_type == NULL)
2972 goto finish_specifiers;
2975 type = typedef_type;
2979 /* function specifier */
2981 goto finish_specifiers;
2988 atomic_type_kind_t atomic_type;
2990 /* match valid basic types */
2991 switch(type_specifiers) {
2992 case SPECIFIER_VOID:
2993 atomic_type = ATOMIC_TYPE_VOID;
2995 case SPECIFIER_CHAR:
2996 atomic_type = ATOMIC_TYPE_CHAR;
2998 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2999 atomic_type = ATOMIC_TYPE_SCHAR;
3001 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3002 atomic_type = ATOMIC_TYPE_UCHAR;
3004 case SPECIFIER_SHORT:
3005 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3006 case SPECIFIER_SHORT | SPECIFIER_INT:
3007 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3008 atomic_type = ATOMIC_TYPE_SHORT;
3010 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3011 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3012 atomic_type = ATOMIC_TYPE_USHORT;
3015 case SPECIFIER_SIGNED:
3016 case SPECIFIER_SIGNED | SPECIFIER_INT:
3017 atomic_type = ATOMIC_TYPE_INT;
3019 case SPECIFIER_UNSIGNED:
3020 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3021 atomic_type = ATOMIC_TYPE_UINT;
3023 case SPECIFIER_LONG:
3024 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3025 case SPECIFIER_LONG | SPECIFIER_INT:
3026 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3027 atomic_type = ATOMIC_TYPE_LONG;
3029 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3030 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3031 atomic_type = ATOMIC_TYPE_ULONG;
3033 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3034 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3035 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3036 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3038 atomic_type = ATOMIC_TYPE_LONGLONG;
3040 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3041 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3043 atomic_type = ATOMIC_TYPE_ULONGLONG;
3046 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3047 atomic_type = unsigned_int8_type_kind;
3050 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3051 atomic_type = unsigned_int16_type_kind;
3054 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3055 atomic_type = unsigned_int32_type_kind;
3058 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3059 atomic_type = unsigned_int64_type_kind;
3062 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3063 atomic_type = unsigned_int128_type_kind;
3066 case SPECIFIER_INT8:
3067 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3068 atomic_type = int8_type_kind;
3071 case SPECIFIER_INT16:
3072 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3073 atomic_type = int16_type_kind;
3076 case SPECIFIER_INT32:
3077 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3078 atomic_type = int32_type_kind;
3081 case SPECIFIER_INT64:
3082 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3083 atomic_type = int64_type_kind;
3086 case SPECIFIER_INT128:
3087 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3088 atomic_type = int128_type_kind;
3091 case SPECIFIER_FLOAT:
3092 atomic_type = ATOMIC_TYPE_FLOAT;
3094 case SPECIFIER_DOUBLE:
3095 atomic_type = ATOMIC_TYPE_DOUBLE;
3097 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3098 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3100 case SPECIFIER_BOOL:
3101 atomic_type = ATOMIC_TYPE_BOOL;
3103 #ifdef PROVIDE_COMPLEX
3104 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3105 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
3107 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3108 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
3110 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3111 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
3113 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3114 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
3116 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3117 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
3119 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3120 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
3124 /* invalid specifier combination, give an error message */
3125 if(type_specifiers == 0) {
3126 if (! strict_mode) {
3127 if (warning.implicit_int) {
3128 warningf(HERE, "no type specifiers in declaration, using 'int'");
3130 atomic_type = ATOMIC_TYPE_INT;
3133 errorf(HERE, "no type specifiers given in declaration");
3135 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3136 (type_specifiers & SPECIFIER_UNSIGNED)) {
3137 errorf(HERE, "signed and unsigned specifiers gives");
3138 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3139 errorf(HERE, "only integer types can be signed or unsigned");
3141 errorf(HERE, "multiple datatypes in declaration");
3143 atomic_type = ATOMIC_TYPE_INVALID;
3146 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3147 type->atomic.akind = atomic_type;
3150 if(type_specifiers != 0) {
3151 errorf(HERE, "multiple datatypes in declaration");
3155 type->base.qualifiers = type_qualifiers;
3156 /* FIXME: check type qualifiers here */
3158 type_t *result = typehash_insert(type);
3159 if(newtype && result != type) {
3163 specifiers->type = result;
3168 static type_qualifiers_t parse_type_qualifiers(void)
3170 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3173 switch(token.type) {
3174 /* type qualifiers */
3175 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3176 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3177 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3178 /* microsoft extended type modifiers */
3179 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3180 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3181 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3182 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3183 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3186 return type_qualifiers;
3191 static declaration_t *parse_identifier_list(void)
3193 declaration_t *declarations = NULL;
3194 declaration_t *last_declaration = NULL;
3196 declaration_t *const declaration = allocate_declaration_zero();
3197 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3198 declaration->source_position = token.source_position;
3199 declaration->symbol = token.v.symbol;
3202 if(last_declaration != NULL) {
3203 last_declaration->next = declaration;
3205 declarations = declaration;
3207 last_declaration = declaration;
3209 if(token.type != ',')
3212 } while(token.type == T_IDENTIFIER);
3214 return declarations;
3217 static void semantic_parameter(declaration_t *declaration)
3219 /* TODO: improve error messages */
3221 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3222 errorf(HERE, "typedef not allowed in parameter list");
3223 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3224 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3225 errorf(HERE, "parameter may only have none or register storage class");
3228 type_t *const orig_type = declaration->type;
3229 type_t * type = skip_typeref(orig_type);
3231 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3232 * into a pointer. § 6.7.5.3 (7) */
3233 if (is_type_array(type)) {
3234 type_t *const element_type = type->array.element_type;
3236 type = make_pointer_type(element_type, type->base.qualifiers);
3238 declaration->type = type;
3241 if(is_type_incomplete(type)) {
3242 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3243 orig_type, declaration->symbol);
3247 static declaration_t *parse_parameter(void)
3249 declaration_specifiers_t specifiers;
3250 memset(&specifiers, 0, sizeof(specifiers));
3252 parse_declaration_specifiers(&specifiers);
3254 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3256 semantic_parameter(declaration);
3261 static declaration_t *parse_parameters(function_type_t *type)
3263 if(token.type == T_IDENTIFIER) {
3264 symbol_t *symbol = token.v.symbol;
3265 if(!is_typedef_symbol(symbol)) {
3266 type->kr_style_parameters = true;
3267 return parse_identifier_list();
3271 if(token.type == ')') {
3272 type->unspecified_parameters = 1;
3275 if(token.type == T_void && look_ahead(1)->type == ')') {
3280 declaration_t *declarations = NULL;
3281 declaration_t *declaration;
3282 declaration_t *last_declaration = NULL;
3283 function_parameter_t *parameter;
3284 function_parameter_t *last_parameter = NULL;
3287 switch(token.type) {
3291 return declarations;
3294 case T___extension__:
3296 declaration = parse_parameter();
3298 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3299 memset(parameter, 0, sizeof(parameter[0]));
3300 parameter->type = declaration->type;
3302 if(last_parameter != NULL) {
3303 last_declaration->next = declaration;
3304 last_parameter->next = parameter;
3306 type->parameters = parameter;
3307 declarations = declaration;
3309 last_parameter = parameter;
3310 last_declaration = declaration;
3314 return declarations;
3316 if(token.type != ',')
3317 return declarations;
3327 } construct_type_kind_t;
3329 typedef struct construct_type_t construct_type_t;
3330 struct construct_type_t {
3331 construct_type_kind_t kind;
3332 construct_type_t *next;
3335 typedef struct parsed_pointer_t parsed_pointer_t;
3336 struct parsed_pointer_t {
3337 construct_type_t construct_type;
3338 type_qualifiers_t type_qualifiers;
3341 typedef struct construct_function_type_t construct_function_type_t;
3342 struct construct_function_type_t {
3343 construct_type_t construct_type;
3344 type_t *function_type;
3347 typedef struct parsed_array_t parsed_array_t;
3348 struct parsed_array_t {
3349 construct_type_t construct_type;
3350 type_qualifiers_t type_qualifiers;
3356 typedef struct construct_base_type_t construct_base_type_t;
3357 struct construct_base_type_t {
3358 construct_type_t construct_type;
3362 static construct_type_t *parse_pointer_declarator(void)
3366 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3367 memset(pointer, 0, sizeof(pointer[0]));
3368 pointer->construct_type.kind = CONSTRUCT_POINTER;
3369 pointer->type_qualifiers = parse_type_qualifiers();
3371 return (construct_type_t*) pointer;
3374 static construct_type_t *parse_array_declarator(void)
3377 add_anchor_token(']');
3379 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3380 memset(array, 0, sizeof(array[0]));
3381 array->construct_type.kind = CONSTRUCT_ARRAY;
3383 if(token.type == T_static) {
3384 array->is_static = true;
3388 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3389 if(type_qualifiers != 0) {
3390 if(token.type == T_static) {
3391 array->is_static = true;
3395 array->type_qualifiers = type_qualifiers;
3397 if(token.type == '*' && look_ahead(1)->type == ']') {
3398 array->is_variable = true;
3400 } else if(token.type != ']') {
3401 array->size = parse_assignment_expression();
3404 rem_anchor_token(']');
3407 return (construct_type_t*) array;
3412 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3415 add_anchor_token(')');
3418 if(declaration != NULL) {
3419 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3421 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3424 declaration_t *parameters = parse_parameters(&type->function);
3425 if(declaration != NULL) {
3426 declaration->scope.declarations = parameters;
3429 construct_function_type_t *construct_function_type =
3430 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3431 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3432 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3433 construct_function_type->function_type = type;
3435 rem_anchor_token(')');
3439 return (construct_type_t*) construct_function_type;
3442 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3443 bool may_be_abstract)
3445 /* construct a single linked list of construct_type_t's which describe
3446 * how to construct the final declarator type */
3447 construct_type_t *first = NULL;
3448 construct_type_t *last = NULL;
3449 gnu_attribute_t *attributes = NULL;
3452 while(token.type == '*') {
3453 construct_type_t *type = parse_pointer_declarator();
3464 /* TODO: find out if this is correct */
3465 parse_attributes(&attributes);
3467 construct_type_t *inner_types = NULL;
3469 switch(token.type) {
3471 if(declaration == NULL) {
3472 errorf(HERE, "no identifier expected in typename");
3474 declaration->symbol = token.v.symbol;
3475 declaration->source_position = token.source_position;
3481 add_anchor_token(')');
3482 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3483 rem_anchor_token(')');
3489 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3490 /* avoid a loop in the outermost scope, because eat_statement doesn't
3492 if(token.type == '}' && current_function == NULL) {
3500 construct_type_t *p = last;
3503 construct_type_t *type;
3504 switch(token.type) {
3506 type = parse_function_declarator(declaration);
3509 type = parse_array_declarator();
3512 goto declarator_finished;
3515 /* insert in the middle of the list (behind p) */
3517 type->next = p->next;
3528 declarator_finished:
3529 parse_attributes(&attributes);
3531 /* append inner_types at the end of the list, we don't to set last anymore
3532 * as it's not needed anymore */
3534 assert(first == NULL);
3535 first = inner_types;
3537 last->next = inner_types;
3545 static type_t *construct_declarator_type(construct_type_t *construct_list,
3548 construct_type_t *iter = construct_list;
3549 for( ; iter != NULL; iter = iter->next) {
3550 switch(iter->kind) {
3551 case CONSTRUCT_INVALID:
3552 internal_errorf(HERE, "invalid type construction found");
3553 case CONSTRUCT_FUNCTION: {
3554 construct_function_type_t *construct_function_type
3555 = (construct_function_type_t*) iter;
3557 type_t *function_type = construct_function_type->function_type;
3559 function_type->function.return_type = type;
3561 type_t *skipped_return_type = skip_typeref(type);
3562 if (is_type_function(skipped_return_type)) {
3563 errorf(HERE, "function returning function is not allowed");
3564 type = type_error_type;
3565 } else if (is_type_array(skipped_return_type)) {
3566 errorf(HERE, "function returning array is not allowed");
3567 type = type_error_type;
3569 type = function_type;
3574 case CONSTRUCT_POINTER: {
3575 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3576 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3577 pointer_type->pointer.points_to = type;
3578 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3580 type = pointer_type;
3584 case CONSTRUCT_ARRAY: {
3585 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3586 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3588 expression_t *size_expression = parsed_array->size;
3589 if(size_expression != NULL) {
3591 = create_implicit_cast(size_expression, type_size_t);
3594 array_type->base.qualifiers = parsed_array->type_qualifiers;
3595 array_type->array.element_type = type;
3596 array_type->array.is_static = parsed_array->is_static;
3597 array_type->array.is_variable = parsed_array->is_variable;
3598 array_type->array.size_expression = size_expression;
3600 if(size_expression != NULL) {
3601 if(is_constant_expression(size_expression)) {
3602 array_type->array.size_constant = true;
3603 array_type->array.size
3604 = fold_constant(size_expression);
3606 array_type->array.is_vla = true;
3610 type_t *skipped_type = skip_typeref(type);
3611 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3612 errorf(HERE, "array of void is not allowed");
3613 type = type_error_type;
3621 type_t *hashed_type = typehash_insert(type);
3622 if(hashed_type != type) {
3623 /* the function type was constructed earlier freeing it here will
3624 * destroy other types... */
3625 if(iter->kind != CONSTRUCT_FUNCTION) {
3635 static declaration_t *parse_declarator(
3636 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3638 declaration_t *const declaration = allocate_declaration_zero();
3639 declaration->declared_storage_class = specifiers->declared_storage_class;
3640 declaration->decl_modifiers = specifiers->decl_modifiers;
3641 declaration->deprecated = specifiers->deprecated;
3642 declaration->deprecated_string = specifiers->deprecated_string;
3643 declaration->get_property_sym = specifiers->get_property_sym;
3644 declaration->put_property_sym = specifiers->put_property_sym;
3645 declaration->is_inline = specifiers->is_inline;
3647 declaration->storage_class = specifiers->declared_storage_class;
3648 if(declaration->storage_class == STORAGE_CLASS_NONE
3649 && scope != global_scope) {
3650 declaration->storage_class = STORAGE_CLASS_AUTO;
3653 if(specifiers->alignment != 0) {
3654 /* TODO: add checks here */
3655 declaration->alignment = specifiers->alignment;
3658 construct_type_t *construct_type
3659 = parse_inner_declarator(declaration, may_be_abstract);
3660 type_t *const type = specifiers->type;
3661 declaration->type = construct_declarator_type(construct_type, type);
3663 if(construct_type != NULL) {
3664 obstack_free(&temp_obst, construct_type);
3670 static type_t *parse_abstract_declarator(type_t *base_type)
3672 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3674 type_t *result = construct_declarator_type(construct_type, base_type);
3675 if(construct_type != NULL) {
3676 obstack_free(&temp_obst, construct_type);
3682 static declaration_t *append_declaration(declaration_t* const declaration)
3684 if (last_declaration != NULL) {
3685 last_declaration->next = declaration;
3687 scope->declarations = declaration;
3689 last_declaration = declaration;
3694 * Check if the declaration of main is suspicious. main should be a
3695 * function with external linkage, returning int, taking either zero
3696 * arguments, two, or three arguments of appropriate types, ie.
3698 * int main([ int argc, char **argv [, char **env ] ]).
3700 * @param decl the declaration to check
3701 * @param type the function type of the declaration
3703 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3705 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3706 warningf(&decl->source_position,
3707 "'main' is normally a non-static function");
3709 if (skip_typeref(func_type->return_type) != type_int) {
3710 warningf(&decl->source_position,
3711 "return type of 'main' should be 'int', but is '%T'",
3712 func_type->return_type);
3714 const function_parameter_t *parm = func_type->parameters;
3716 type_t *const first_type = parm->type;
3717 if (!types_compatible(skip_typeref(first_type), type_int)) {
3718 warningf(&decl->source_position,
3719 "first argument of 'main' should be 'int', but is '%T'", first_type);
3723 type_t *const second_type = parm->type;
3724 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3725 warningf(&decl->source_position,
3726 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3730 type_t *const third_type = parm->type;
3731 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3732 warningf(&decl->source_position,
3733 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3737 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3741 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3747 * Check if a symbol is the equal to "main".
3749 static bool is_sym_main(const symbol_t *const sym)
3751 return strcmp(sym->string, "main") == 0;
3754 static declaration_t *internal_record_declaration(
3755 declaration_t *const declaration,
3756 const bool is_function_definition)
3758 const symbol_t *const symbol = declaration->symbol;
3759 const namespace_t namespc = (namespace_t)declaration->namespc;
3761 type_t *const orig_type = declaration->type;
3762 type_t *const type = skip_typeref(orig_type);
3763 if (is_type_function(type) &&
3764 type->function.unspecified_parameters &&
3765 warning.strict_prototypes) {
3766 warningf(&declaration->source_position,
3767 "function declaration '%#T' is not a prototype",
3768 orig_type, declaration->symbol);
3771 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3772 check_type_of_main(declaration, &type->function);
3775 assert(declaration->symbol != NULL);
3776 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3778 assert(declaration != previous_declaration);
3779 if (previous_declaration != NULL) {
3780 if (previous_declaration->parent_scope == scope) {
3781 /* can happen for K&R style declarations */
3782 if(previous_declaration->type == NULL) {
3783 previous_declaration->type = declaration->type;
3786 const type_t *prev_type = skip_typeref(previous_declaration->type);
3787 if (!types_compatible(type, prev_type)) {
3788 errorf(&declaration->source_position,
3789 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3790 orig_type, symbol, previous_declaration->type, symbol,
3791 &previous_declaration->source_position);
3793 unsigned old_storage_class = previous_declaration->storage_class;
3794 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3795 errorf(&declaration->source_position,
3796 "redeclaration of enum entry '%Y' (declared %P)",
3797 symbol, &previous_declaration->source_position);
3798 return previous_declaration;
3801 unsigned new_storage_class = declaration->storage_class;
3803 if(is_type_incomplete(prev_type)) {
3804 previous_declaration->type = type;
3808 /* pretend no storage class means extern for function
3809 * declarations (except if the previous declaration is neither
3810 * none nor extern) */
3811 if (is_type_function(type)) {
3812 switch (old_storage_class) {
3813 case STORAGE_CLASS_NONE:
3814 old_storage_class = STORAGE_CLASS_EXTERN;
3816 case STORAGE_CLASS_EXTERN:
3817 if (is_function_definition) {
3818 if (warning.missing_prototypes &&
3819 prev_type->function.unspecified_parameters &&
3820 !is_sym_main(symbol)) {
3821 warningf(&declaration->source_position,
3822 "no previous prototype for '%#T'",
3825 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3826 new_storage_class = STORAGE_CLASS_EXTERN;
3834 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3835 new_storage_class == STORAGE_CLASS_EXTERN) {
3836 warn_redundant_declaration:
3837 if (warning.redundant_decls) {
3838 warningf(&declaration->source_position,
3839 "redundant declaration for '%Y' (declared %P)",
3840 symbol, &previous_declaration->source_position);
3842 } else if (current_function == NULL) {
3843 if (old_storage_class != STORAGE_CLASS_STATIC &&
3844 new_storage_class == STORAGE_CLASS_STATIC) {
3845 errorf(&declaration->source_position,
3846 "static declaration of '%Y' follows non-static declaration (declared %P)",
3847 symbol, &previous_declaration->source_position);
3849 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3850 goto warn_redundant_declaration;
3852 if (new_storage_class == STORAGE_CLASS_NONE) {
3853 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3854 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3858 if (old_storage_class == new_storage_class) {
3859 errorf(&declaration->source_position,
3860 "redeclaration of '%Y' (declared %P)",
3861 symbol, &previous_declaration->source_position);
3863 errorf(&declaration->source_position,
3864 "redeclaration of '%Y' with different linkage (declared %P)",
3865 symbol, &previous_declaration->source_position);
3869 return previous_declaration;
3871 } else if (is_function_definition) {
3872 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3873 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3874 warningf(&declaration->source_position,
3875 "no previous prototype for '%#T'", orig_type, symbol);
3876 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3877 warningf(&declaration->source_position,
3878 "no previous declaration for '%#T'", orig_type,
3882 } else if (warning.missing_declarations &&
3883 scope == global_scope &&
3884 !is_type_function(type) && (
3885 declaration->storage_class == STORAGE_CLASS_NONE ||
3886 declaration->storage_class == STORAGE_CLASS_THREAD
3888 warningf(&declaration->source_position,
3889 "no previous declaration for '%#T'", orig_type, symbol);
3892 assert(declaration->parent_scope == NULL);
3893 assert(scope != NULL);
3895 declaration->parent_scope = scope;
3897 environment_push(declaration);
3898 return append_declaration(declaration);
3901 static declaration_t *record_declaration(declaration_t *declaration)
3903 return internal_record_declaration(declaration, false);
3906 static declaration_t *record_function_definition(declaration_t *declaration)
3908 return internal_record_declaration(declaration, true);
3911 static void parser_error_multiple_definition(declaration_t *declaration,
3912 const source_position_t *source_position)
3914 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3915 declaration->symbol, &declaration->source_position);
3918 static bool is_declaration_specifier(const token_t *token,
3919 bool only_type_specifiers)
3921 switch(token->type) {
3925 return is_typedef_symbol(token->v.symbol);
3927 case T___extension__:
3930 return !only_type_specifiers;
3937 static void parse_init_declarator_rest(declaration_t *declaration)
3941 type_t *orig_type = declaration->type;
3942 type_t *type = skip_typeref(orig_type);
3944 if(declaration->init.initializer != NULL) {
3945 parser_error_multiple_definition(declaration, HERE);
3948 bool must_be_constant = false;
3949 if(declaration->storage_class == STORAGE_CLASS_STATIC
3950 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3951 || declaration->parent_scope == global_scope) {
3952 must_be_constant = true;
3955 parse_initializer_env_t env;
3956 env.type = orig_type;
3957 env.must_be_constant = must_be_constant;
3958 env.declaration = declaration;
3960 initializer_t *initializer = parse_initializer(&env);
3962 if(env.type != orig_type) {
3963 orig_type = env.type;
3964 type = skip_typeref(orig_type);
3965 declaration->type = env.type;
3968 if(is_type_function(type)) {
3969 errorf(&declaration->source_position,
3970 "initializers not allowed for function types at declator '%Y' (type '%T')",
3971 declaration->symbol, orig_type);
3973 declaration->init.initializer = initializer;
3977 /* parse rest of a declaration without any declarator */
3978 static void parse_anonymous_declaration_rest(
3979 const declaration_specifiers_t *specifiers,
3980 parsed_declaration_func finished_declaration)
3984 declaration_t *const declaration = allocate_declaration_zero();
3985 declaration->type = specifiers->type;
3986 declaration->declared_storage_class = specifiers->declared_storage_class;
3987 declaration->source_position = specifiers->source_position;
3988 declaration->decl_modifiers = specifiers->decl_modifiers;
3990 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3991 warningf(&declaration->source_position,
3992 "useless storage class in empty declaration");
3994 declaration->storage_class = STORAGE_CLASS_NONE;
3996 type_t *type = declaration->type;
3997 switch (type->kind) {
3998 case TYPE_COMPOUND_STRUCT:
3999 case TYPE_COMPOUND_UNION: {
4000 if (type->compound.declaration->symbol == NULL) {
4001 warningf(&declaration->source_position,
4002 "unnamed struct/union that defines no instances");
4011 warningf(&declaration->source_position, "empty declaration");
4015 finished_declaration(declaration);
4018 static void parse_declaration_rest(declaration_t *ndeclaration,
4019 const declaration_specifiers_t *specifiers,
4020 parsed_declaration_func finished_declaration)
4022 add_anchor_token(';');
4023 add_anchor_token('=');
4024 add_anchor_token(',');
4026 declaration_t *declaration = finished_declaration(ndeclaration);
4028 type_t *orig_type = declaration->type;
4029 type_t *type = skip_typeref(orig_type);
4031 if (type->kind != TYPE_FUNCTION &&
4032 declaration->is_inline &&
4033 is_type_valid(type)) {
4034 warningf(&declaration->source_position,
4035 "variable '%Y' declared 'inline'\n", declaration->symbol);
4038 if(token.type == '=') {
4039 parse_init_declarator_rest(declaration);
4042 if(token.type != ',')
4046 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4051 rem_anchor_token(';');
4052 rem_anchor_token('=');
4053 rem_anchor_token(',');
4056 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4058 symbol_t *symbol = declaration->symbol;
4059 if(symbol == NULL) {
4060 errorf(HERE, "anonymous declaration not valid as function parameter");
4063 namespace_t namespc = (namespace_t) declaration->namespc;
4064 if(namespc != NAMESPACE_NORMAL) {
4065 return record_declaration(declaration);
4068 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4069 if(previous_declaration == NULL ||
4070 previous_declaration->parent_scope != scope) {
4071 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4076 if(previous_declaration->type == NULL) {
4077 previous_declaration->type = declaration->type;
4078 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4079 previous_declaration->storage_class = declaration->storage_class;
4080 previous_declaration->parent_scope = scope;
4081 return previous_declaration;
4083 return record_declaration(declaration);
4087 static void parse_declaration(parsed_declaration_func finished_declaration)
4089 declaration_specifiers_t specifiers;
4090 memset(&specifiers, 0, sizeof(specifiers));
4091 parse_declaration_specifiers(&specifiers);
4093 if(token.type == ';') {
4094 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4096 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4097 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4101 static void parse_kr_declaration_list(declaration_t *declaration)
4103 type_t *type = skip_typeref(declaration->type);
4104 if(!is_type_function(type))
4107 if(!type->function.kr_style_parameters)
4110 /* push function parameters */
4111 int top = environment_top();
4112 scope_t *last_scope = scope;
4113 set_scope(&declaration->scope);
4115 declaration_t *parameter = declaration->scope.declarations;
4116 for( ; parameter != NULL; parameter = parameter->next) {
4117 assert(parameter->parent_scope == NULL);
4118 parameter->parent_scope = scope;
4119 environment_push(parameter);
4122 /* parse declaration list */
4123 while(is_declaration_specifier(&token, false)) {
4124 parse_declaration(finished_kr_declaration);
4127 /* pop function parameters */
4128 assert(scope == &declaration->scope);
4129 set_scope(last_scope);
4130 environment_pop_to(top);
4132 /* update function type */
4133 type_t *new_type = duplicate_type(type);
4134 new_type->function.kr_style_parameters = false;
4136 function_parameter_t *parameters = NULL;
4137 function_parameter_t *last_parameter = NULL;
4139 declaration_t *parameter_declaration = declaration->scope.declarations;
4140 for( ; parameter_declaration != NULL;
4141 parameter_declaration = parameter_declaration->next) {
4142 type_t *parameter_type = parameter_declaration->type;
4143 if(parameter_type == NULL) {
4145 errorf(HERE, "no type specified for function parameter '%Y'",
4146 parameter_declaration->symbol);
4148 if (warning.implicit_int) {
4149 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4150 parameter_declaration->symbol);
4152 parameter_type = type_int;
4153 parameter_declaration->type = parameter_type;
4157 semantic_parameter(parameter_declaration);
4158 parameter_type = parameter_declaration->type;
4160 function_parameter_t *function_parameter
4161 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4162 memset(function_parameter, 0, sizeof(function_parameter[0]));
4164 function_parameter->type = parameter_type;
4165 if(last_parameter != NULL) {
4166 last_parameter->next = function_parameter;
4168 parameters = function_parameter;
4170 last_parameter = function_parameter;
4172 new_type->function.parameters = parameters;
4174 type = typehash_insert(new_type);
4175 if(type != new_type) {
4176 obstack_free(type_obst, new_type);
4179 declaration->type = type;
4182 static bool first_err = true;
4185 * When called with first_err set, prints the name of the current function,
4188 static void print_in_function(void) {
4191 diagnosticf("%s: In function '%Y':\n",
4192 current_function->source_position.input_name,
4193 current_function->symbol);
4198 * Check if all labels are defined in the current function.
4199 * Check if all labels are used in the current function.
4201 static void check_labels(void)
4203 for (const goto_statement_t *goto_statement = goto_first;
4204 goto_statement != NULL;
4205 goto_statement = goto_statement->next) {
4206 declaration_t *label = goto_statement->label;
4209 if (label->source_position.input_name == NULL) {
4210 print_in_function();
4211 errorf(&goto_statement->base.source_position,
4212 "label '%Y' used but not defined", label->symbol);
4215 goto_first = goto_last = NULL;
4217 if (warning.unused_label) {
4218 for (const label_statement_t *label_statement = label_first;
4219 label_statement != NULL;
4220 label_statement = label_statement->next) {
4221 const declaration_t *label = label_statement->label;
4223 if (! label->used) {
4224 print_in_function();
4225 warningf(&label_statement->base.source_position,
4226 "label '%Y' defined but not used", label->symbol);
4230 label_first = label_last = NULL;
4234 * Check declarations of current_function for unused entities.
4236 static void check_declarations(void)
4238 if (warning.unused_parameter) {
4239 const scope_t *scope = ¤t_function->scope;
4241 const declaration_t *parameter = scope->declarations;
4242 for (; parameter != NULL; parameter = parameter->next) {
4243 if (! parameter->used) {
4244 print_in_function();
4245 warningf(¶meter->source_position,
4246 "unused parameter '%Y'", parameter->symbol);
4250 if (warning.unused_variable) {
4254 static void parse_external_declaration(void)
4256 /* function-definitions and declarations both start with declaration
4258 declaration_specifiers_t specifiers;
4259 memset(&specifiers, 0, sizeof(specifiers));
4261 add_anchor_token(';');
4262 parse_declaration_specifiers(&specifiers);
4263 rem_anchor_token(';');
4265 /* must be a declaration */
4266 if(token.type == ';') {
4267 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4271 add_anchor_token(',');
4272 add_anchor_token('=');
4273 rem_anchor_token(';');
4275 /* declarator is common to both function-definitions and declarations */
4276 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4278 rem_anchor_token(',');
4279 rem_anchor_token('=');
4280 rem_anchor_token(';');
4282 /* must be a declaration */
4283 if(token.type == ',' || token.type == '=' || token.type == ';') {
4284 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4288 /* must be a function definition */
4289 parse_kr_declaration_list(ndeclaration);
4291 if(token.type != '{') {
4292 parse_error_expected("while parsing function definition", '{', NULL);
4293 eat_until_matching_token(';');
4297 type_t *type = ndeclaration->type;
4299 /* note that we don't skip typerefs: the standard doesn't allow them here
4300 * (so we can't use is_type_function here) */
4301 if(type->kind != TYPE_FUNCTION) {
4302 if (is_type_valid(type)) {
4303 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4304 type, ndeclaration->symbol);
4310 /* § 6.7.5.3 (14) a function definition with () means no
4311 * parameters (and not unspecified parameters) */
4312 if(type->function.unspecified_parameters) {
4313 type_t *duplicate = duplicate_type(type);
4314 duplicate->function.unspecified_parameters = false;
4316 type = typehash_insert(duplicate);
4317 if(type != duplicate) {
4318 obstack_free(type_obst, duplicate);
4320 ndeclaration->type = type;
4323 declaration_t *const declaration = record_function_definition(ndeclaration);
4324 if(ndeclaration != declaration) {
4325 declaration->scope = ndeclaration->scope;
4327 type = skip_typeref(declaration->type);
4329 /* push function parameters and switch scope */
4330 int top = environment_top();
4331 scope_t *last_scope = scope;
4332 set_scope(&declaration->scope);
4334 declaration_t *parameter = declaration->scope.declarations;
4335 for( ; parameter != NULL; parameter = parameter->next) {
4336 if(parameter->parent_scope == &ndeclaration->scope) {
4337 parameter->parent_scope = scope;
4339 assert(parameter->parent_scope == NULL
4340 || parameter->parent_scope == scope);
4341 parameter->parent_scope = scope;
4342 environment_push(parameter);
4345 if(declaration->init.statement != NULL) {
4346 parser_error_multiple_definition(declaration, HERE);
4348 goto end_of_parse_external_declaration;
4350 /* parse function body */
4351 int label_stack_top = label_top();
4352 declaration_t *old_current_function = current_function;
4353 current_function = declaration;
4355 declaration->init.statement = parse_compound_statement(false);
4358 check_declarations();
4360 assert(current_function == declaration);
4361 current_function = old_current_function;
4362 label_pop_to(label_stack_top);
4365 end_of_parse_external_declaration:
4366 assert(scope == &declaration->scope);
4367 set_scope(last_scope);
4368 environment_pop_to(top);
4371 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4372 source_position_t *source_position)
4374 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4376 type->bitfield.base_type = base_type;
4377 type->bitfield.size = size;
4382 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4385 declaration_t *iter = compound_declaration->scope.declarations;
4386 for( ; iter != NULL; iter = iter->next) {
4387 if(iter->namespc != NAMESPACE_NORMAL)
4390 if(iter->symbol == NULL) {
4391 type_t *type = skip_typeref(iter->type);
4392 if(is_type_compound(type)) {
4393 declaration_t *result
4394 = find_compound_entry(type->compound.declaration, symbol);
4401 if(iter->symbol == symbol) {
4409 static void parse_compound_declarators(declaration_t *struct_declaration,
4410 const declaration_specifiers_t *specifiers)
4412 declaration_t *last_declaration = struct_declaration->scope.declarations;
4413 if(last_declaration != NULL) {
4414 while(last_declaration->next != NULL) {
4415 last_declaration = last_declaration->next;
4420 declaration_t *declaration;
4422 if(token.type == ':') {
4423 source_position_t source_position = *HERE;
4426 type_t *base_type = specifiers->type;
4427 expression_t *size = parse_constant_expression();
4429 if(!is_type_integer(skip_typeref(base_type))) {
4430 errorf(HERE, "bitfield base type '%T' is not an integer type",
4434 type_t *type = make_bitfield_type(base_type, size, &source_position);
4436 declaration = allocate_declaration_zero();
4437 declaration->namespc = NAMESPACE_NORMAL;
4438 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4439 declaration->storage_class = STORAGE_CLASS_NONE;
4440 declaration->source_position = source_position;
4441 declaration->decl_modifiers = specifiers->decl_modifiers;
4442 declaration->type = type;
4444 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4446 type_t *orig_type = declaration->type;
4447 type_t *type = skip_typeref(orig_type);
4449 if(token.type == ':') {
4450 source_position_t source_position = *HERE;
4452 expression_t *size = parse_constant_expression();
4454 if(!is_type_integer(type)) {
4455 errorf(HERE, "bitfield base type '%T' is not an "
4456 "integer type", orig_type);
4459 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4460 declaration->type = bitfield_type;
4462 /* TODO we ignore arrays for now... what is missing is a check
4463 * that they're at the end of the struct */
4464 if(is_type_incomplete(type) && !is_type_array(type)) {
4466 "compound member '%Y' has incomplete type '%T'",
4467 declaration->symbol, orig_type);
4468 } else if(is_type_function(type)) {
4469 errorf(HERE, "compound member '%Y' must not have function "
4470 "type '%T'", declaration->symbol, orig_type);
4475 /* make sure we don't define a symbol multiple times */
4476 symbol_t *symbol = declaration->symbol;
4477 if(symbol != NULL) {
4478 declaration_t *prev_decl
4479 = find_compound_entry(struct_declaration, symbol);
4481 if(prev_decl != NULL) {
4482 assert(prev_decl->symbol == symbol);
4483 errorf(&declaration->source_position,
4484 "multiple declarations of symbol '%Y' (declared %P)",
4485 symbol, &prev_decl->source_position);
4489 /* append declaration */
4490 if(last_declaration != NULL) {
4491 last_declaration->next = declaration;
4493 struct_declaration->scope.declarations = declaration;
4495 last_declaration = declaration;
4497 if(token.type != ',')
4507 static void parse_compound_type_entries(declaration_t *compound_declaration)
4510 add_anchor_token('}');
4512 while(token.type != '}' && token.type != T_EOF) {
4513 declaration_specifiers_t specifiers;
4514 memset(&specifiers, 0, sizeof(specifiers));
4515 parse_declaration_specifiers(&specifiers);
4517 parse_compound_declarators(compound_declaration, &specifiers);
4519 rem_anchor_token('}');
4521 if(token.type == T_EOF) {
4522 errorf(HERE, "EOF while parsing struct");
4527 static type_t *parse_typename(void)
4529 declaration_specifiers_t specifiers;
4530 memset(&specifiers, 0, sizeof(specifiers));
4531 parse_declaration_specifiers(&specifiers);
4532 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4533 /* TODO: improve error message, user does probably not know what a
4534 * storage class is...
4536 errorf(HERE, "typename may not have a storage class");
4539 type_t *result = parse_abstract_declarator(specifiers.type);
4547 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4548 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4549 expression_t *left);
4551 typedef struct expression_parser_function_t expression_parser_function_t;
4552 struct expression_parser_function_t {
4553 unsigned precedence;
4554 parse_expression_function parser;
4555 unsigned infix_precedence;
4556 parse_expression_infix_function infix_parser;
4559 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4562 * Prints an error message if an expression was expected but not read
4564 static expression_t *expected_expression_error(void)
4566 /* skip the error message if the error token was read */
4567 if (token.type != T_ERROR) {
4568 errorf(HERE, "expected expression, got token '%K'", &token);
4572 return create_invalid_expression();
4576 * Parse a string constant.
4578 static expression_t *parse_string_const(void)
4581 if (token.type == T_STRING_LITERAL) {
4582 string_t res = token.v.string;
4584 while (token.type == T_STRING_LITERAL) {
4585 res = concat_strings(&res, &token.v.string);
4588 if (token.type != T_WIDE_STRING_LITERAL) {
4589 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4590 /* note: that we use type_char_ptr here, which is already the
4591 * automatic converted type. revert_automatic_type_conversion
4592 * will construct the array type */
4593 cnst->base.type = type_char_ptr;
4594 cnst->string.value = res;
4598 wres = concat_string_wide_string(&res, &token.v.wide_string);
4600 wres = token.v.wide_string;
4605 switch (token.type) {
4606 case T_WIDE_STRING_LITERAL:
4607 wres = concat_wide_strings(&wres, &token.v.wide_string);
4610 case T_STRING_LITERAL:
4611 wres = concat_wide_string_string(&wres, &token.v.string);
4615 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4616 cnst->base.type = type_wchar_t_ptr;
4617 cnst->wide_string.value = wres;
4626 * Parse an integer constant.
4628 static expression_t *parse_int_const(void)
4630 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4631 cnst->base.source_position = *HERE;
4632 cnst->base.type = token.datatype;
4633 cnst->conste.v.int_value = token.v.intvalue;
4641 * Parse a character constant.
4643 static expression_t *parse_character_constant(void)
4645 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4647 cnst->base.source_position = *HERE;
4648 cnst->base.type = token.datatype;
4649 cnst->conste.v.character = token.v.string;
4651 if (cnst->conste.v.character.size != 1) {
4652 if (warning.multichar && (c_mode & _GNUC)) {
4654 warningf(HERE, "multi-character character constant");
4656 errorf(HERE, "more than 1 characters in character constant");
4665 * Parse a wide character constant.
4667 static expression_t *parse_wide_character_constant(void)
4669 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4671 cnst->base.source_position = *HERE;
4672 cnst->base.type = token.datatype;
4673 cnst->conste.v.wide_character = token.v.wide_string;
4675 if (cnst->conste.v.wide_character.size != 1) {
4676 if (warning.multichar && (c_mode & _GNUC)) {
4678 warningf(HERE, "multi-character character constant");
4680 errorf(HERE, "more than 1 characters in character constant");
4689 * Parse a float constant.
4691 static expression_t *parse_float_const(void)
4693 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4694 cnst->base.type = token.datatype;
4695 cnst->conste.v.float_value = token.v.floatvalue;
4702 static declaration_t *create_implicit_function(symbol_t *symbol,
4703 const source_position_t *source_position)
4705 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4706 ntype->function.return_type = type_int;
4707 ntype->function.unspecified_parameters = true;
4709 type_t *type = typehash_insert(ntype);
4714 declaration_t *const declaration = allocate_declaration_zero();
4715 declaration->storage_class = STORAGE_CLASS_EXTERN;
4716 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4717 declaration->type = type;
4718 declaration->symbol = symbol;
4719 declaration->source_position = *source_position;
4720 declaration->parent_scope = global_scope;
4722 scope_t *old_scope = scope;
4723 set_scope(global_scope);
4725 environment_push(declaration);
4726 /* prepends the declaration to the global declarations list */
4727 declaration->next = scope->declarations;
4728 scope->declarations = declaration;
4730 assert(scope == global_scope);
4731 set_scope(old_scope);
4737 * Creates a return_type (func)(argument_type) function type if not
4740 * @param return_type the return type
4741 * @param argument_type the argument type
4743 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4745 function_parameter_t *parameter
4746 = obstack_alloc(type_obst, sizeof(parameter[0]));
4747 memset(parameter, 0, sizeof(parameter[0]));
4748 parameter->type = argument_type;
4750 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4751 type->function.return_type = return_type;
4752 type->function.parameters = parameter;
4754 type_t *result = typehash_insert(type);
4755 if(result != type) {
4763 * Creates a function type for some function like builtins.
4765 * @param symbol the symbol describing the builtin
4767 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4769 switch(symbol->ID) {
4770 case T___builtin_alloca:
4771 return make_function_1_type(type_void_ptr, type_size_t);
4772 case T___builtin_nan:
4773 return make_function_1_type(type_double, type_char_ptr);
4774 case T___builtin_nanf:
4775 return make_function_1_type(type_float, type_char_ptr);
4776 case T___builtin_nand:
4777 return make_function_1_type(type_long_double, type_char_ptr);
4778 case T___builtin_va_end:
4779 return make_function_1_type(type_void, type_valist);
4781 internal_errorf(HERE, "not implemented builtin symbol found");
4786 * Performs automatic type cast as described in § 6.3.2.1.
4788 * @param orig_type the original type
4790 static type_t *automatic_type_conversion(type_t *orig_type)
4792 type_t *type = skip_typeref(orig_type);
4793 if(is_type_array(type)) {
4794 array_type_t *array_type = &type->array;
4795 type_t *element_type = array_type->element_type;
4796 unsigned qualifiers = array_type->base.qualifiers;
4798 return make_pointer_type(element_type, qualifiers);
4801 if(is_type_function(type)) {
4802 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4809 * reverts the automatic casts of array to pointer types and function
4810 * to function-pointer types as defined § 6.3.2.1
4812 type_t *revert_automatic_type_conversion(const expression_t *expression)
4814 switch (expression->kind) {
4815 case EXPR_REFERENCE: return expression->reference.declaration->type;
4816 case EXPR_SELECT: return expression->select.compound_entry->type;
4818 case EXPR_UNARY_DEREFERENCE: {
4819 const expression_t *const value = expression->unary.value;
4820 type_t *const type = skip_typeref(value->base.type);
4821 assert(is_type_pointer(type));
4822 return type->pointer.points_to;
4825 case EXPR_BUILTIN_SYMBOL:
4826 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4828 case EXPR_ARRAY_ACCESS: {
4829 const expression_t *array_ref = expression->array_access.array_ref;
4830 type_t *type_left = skip_typeref(array_ref->base.type);
4831 if (!is_type_valid(type_left))
4833 assert(is_type_pointer(type_left));
4834 return type_left->pointer.points_to;
4837 case EXPR_STRING_LITERAL: {
4838 size_t size = expression->string.value.size;
4839 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4842 case EXPR_WIDE_STRING_LITERAL: {
4843 size_t size = expression->wide_string.value.size;
4844 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4847 case EXPR_COMPOUND_LITERAL:
4848 return expression->compound_literal.type;
4853 return expression->base.type;
4856 static expression_t *parse_reference(void)
4858 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4860 reference_expression_t *ref = &expression->reference;
4861 symbol_t *const symbol = token.v.symbol;
4863 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
4865 source_position_t source_position = token.source_position;
4868 if(declaration == NULL) {
4869 if (! strict_mode && token.type == '(') {
4870 /* an implicitly defined function */
4871 if (warning.implicit_function_declaration) {
4872 warningf(HERE, "implicit declaration of function '%Y'",
4876 declaration = create_implicit_function(symbol,
4879 errorf(HERE, "unknown symbol '%Y' found.", symbol);
4880 return create_invalid_expression();
4884 type_t *type = declaration->type;
4886 /* we always do the auto-type conversions; the & and sizeof parser contains
4887 * code to revert this! */
4888 type = automatic_type_conversion(type);
4890 ref->declaration = declaration;
4891 ref->base.type = type;
4893 /* this declaration is used */
4894 declaration->used = true;
4896 /* check for deprecated functions */
4897 if(declaration->deprecated != 0) {
4898 const char *prefix = "";
4899 if (is_type_function(declaration->type))
4900 prefix = "function ";
4902 if (declaration->deprecated_string != NULL) {
4903 warningf(&source_position,
4904 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4905 declaration->deprecated_string);
4907 warningf(&source_position,
4908 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4915 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4919 /* TODO check if explicit cast is allowed and issue warnings/errors */
4922 static expression_t *parse_compound_literal(type_t *type)
4924 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4926 parse_initializer_env_t env;
4928 env.declaration = NULL;
4929 env.must_be_constant = false;
4930 initializer_t *initializer = parse_initializer(&env);
4933 expression->compound_literal.initializer = initializer;
4934 expression->compound_literal.type = type;
4935 expression->base.type = automatic_type_conversion(type);
4941 * Parse a cast expression.
4943 static expression_t *parse_cast(void)
4945 source_position_t source_position = token.source_position;
4947 type_t *type = parse_typename();
4949 /* matching add_anchor_token() is at call site */
4950 rem_anchor_token(')');
4953 if(token.type == '{') {
4954 return parse_compound_literal(type);
4957 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4958 cast->base.source_position = source_position;
4960 expression_t *value = parse_sub_expression(20);
4962 check_cast_allowed(value, type);
4964 cast->base.type = type;
4965 cast->unary.value = value;
4969 return create_invalid_expression();
4973 * Parse a statement expression.
4975 static expression_t *parse_statement_expression(void)
4977 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4979 statement_t *statement = parse_compound_statement(true);
4980 expression->statement.statement = statement;
4981 expression->base.source_position = statement->base.source_position;
4983 /* find last statement and use its type */
4984 type_t *type = type_void;
4985 const statement_t *stmt = statement->compound.statements;
4987 while (stmt->base.next != NULL)
4988 stmt = stmt->base.next;
4990 if (stmt->kind == STATEMENT_EXPRESSION) {
4991 type = stmt->expression.expression->base.type;
4994 warningf(&expression->base.source_position, "empty statement expression ({})");
4996 expression->base.type = type;
5002 return create_invalid_expression();
5006 * Parse a braced expression.
5008 static expression_t *parse_brace_expression(void)
5011 add_anchor_token(')');
5013 switch(token.type) {
5015 /* gcc extension: a statement expression */
5016 return parse_statement_expression();
5020 return parse_cast();
5022 if(is_typedef_symbol(token.v.symbol)) {
5023 return parse_cast();
5027 expression_t *result = parse_expression();
5028 rem_anchor_token(')');
5033 return create_invalid_expression();
5036 static expression_t *parse_function_keyword(void)
5041 if (current_function == NULL) {
5042 errorf(HERE, "'__func__' used outside of a function");
5045 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5046 expression->base.type = type_char_ptr;
5047 expression->funcname.kind = FUNCNAME_FUNCTION;
5052 static expression_t *parse_pretty_function_keyword(void)
5054 eat(T___PRETTY_FUNCTION__);
5056 if (current_function == NULL) {
5057 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5060 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5061 expression->base.type = type_char_ptr;
5062 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5067 static expression_t *parse_funcsig_keyword(void)
5071 if (current_function == NULL) {
5072 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5075 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5076 expression->base.type = type_char_ptr;
5077 expression->funcname.kind = FUNCNAME_FUNCSIG;
5082 static expression_t *parse_funcdname_keyword(void)
5084 eat(T___FUNCDNAME__);
5086 if (current_function == NULL) {
5087 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5090 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5091 expression->base.type = type_char_ptr;
5092 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5097 static designator_t *parse_designator(void)
5099 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5100 result->source_position = *HERE;
5102 if(token.type != T_IDENTIFIER) {
5103 parse_error_expected("while parsing member designator",
5104 T_IDENTIFIER, NULL);
5107 result->symbol = token.v.symbol;
5110 designator_t *last_designator = result;
5112 if(token.type == '.') {
5114 if(token.type != T_IDENTIFIER) {
5115 parse_error_expected("while parsing member designator",
5116 T_IDENTIFIER, NULL);
5119 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5120 designator->source_position = *HERE;
5121 designator->symbol = token.v.symbol;
5124 last_designator->next = designator;
5125 last_designator = designator;
5128 if(token.type == '[') {
5130 add_anchor_token(']');
5131 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5132 designator->source_position = *HERE;
5133 designator->array_index = parse_expression();
5134 rem_anchor_token(']');
5136 if(designator->array_index == NULL) {
5140 last_designator->next = designator;
5141 last_designator = designator;
5153 * Parse the __builtin_offsetof() expression.
5155 static expression_t *parse_offsetof(void)
5157 eat(T___builtin_offsetof);
5159 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5160 expression->base.type = type_size_t;
5163 add_anchor_token(',');
5164 type_t *type = parse_typename();
5165 rem_anchor_token(',');
5167 add_anchor_token(')');
5168 designator_t *designator = parse_designator();
5169 rem_anchor_token(')');
5172 expression->offsetofe.type = type;
5173 expression->offsetofe.designator = designator;
5176 memset(&path, 0, sizeof(path));
5177 path.top_type = type;
5178 path.path = NEW_ARR_F(type_path_entry_t, 0);
5180 descend_into_subtype(&path);
5182 if(!walk_designator(&path, designator, true)) {
5183 return create_invalid_expression();
5186 DEL_ARR_F(path.path);
5190 return create_invalid_expression();
5194 * Parses a _builtin_va_start() expression.
5196 static expression_t *parse_va_start(void)
5198 eat(T___builtin_va_start);
5200 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5203 add_anchor_token(',');
5204 expression->va_starte.ap = parse_assignment_expression();
5205 rem_anchor_token(',');
5207 expression_t *const expr = parse_assignment_expression();
5208 if (expr->kind == EXPR_REFERENCE) {
5209 declaration_t *const decl = expr->reference.declaration;
5211 return create_invalid_expression();
5212 if (decl->parent_scope == ¤t_function->scope &&
5213 decl->next == NULL) {
5214 expression->va_starte.parameter = decl;
5219 errorf(&expr->base.source_position,
5220 "second argument of 'va_start' must be last parameter of the current function");
5222 return create_invalid_expression();
5226 * Parses a _builtin_va_arg() expression.
5228 static expression_t *parse_va_arg(void)
5230 eat(T___builtin_va_arg);
5232 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5235 expression->va_arge.ap = parse_assignment_expression();
5237 expression->base.type = parse_typename();
5242 return create_invalid_expression();
5245 static expression_t *parse_builtin_symbol(void)
5247 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5249 symbol_t *symbol = token.v.symbol;
5251 expression->builtin_symbol.symbol = symbol;
5254 type_t *type = get_builtin_symbol_type(symbol);
5255 type = automatic_type_conversion(type);
5257 expression->base.type = type;
5262 * Parses a __builtin_constant() expression.
5264 static expression_t *parse_builtin_constant(void)
5266 eat(T___builtin_constant_p);
5268 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5271 add_anchor_token(')');
5272 expression->builtin_constant.value = parse_assignment_expression();
5273 rem_anchor_token(')');
5275 expression->base.type = type_int;
5279 return create_invalid_expression();
5283 * Parses a __builtin_prefetch() expression.
5285 static expression_t *parse_builtin_prefetch(void)
5287 eat(T___builtin_prefetch);
5289 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5292 add_anchor_token(')');
5293 expression->builtin_prefetch.adr = parse_assignment_expression();
5294 if (token.type == ',') {
5296 expression->builtin_prefetch.rw = parse_assignment_expression();
5298 if (token.type == ',') {
5300 expression->builtin_prefetch.locality = parse_assignment_expression();
5302 rem_anchor_token(')');
5304 expression->base.type = type_void;
5308 return create_invalid_expression();
5312 * Parses a __builtin_is_*() compare expression.
5314 static expression_t *parse_compare_builtin(void)
5316 expression_t *expression;
5318 switch(token.type) {
5319 case T___builtin_isgreater:
5320 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5322 case T___builtin_isgreaterequal:
5323 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5325 case T___builtin_isless:
5326 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5328 case T___builtin_islessequal:
5329 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5331 case T___builtin_islessgreater:
5332 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5334 case T___builtin_isunordered:
5335 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5338 internal_errorf(HERE, "invalid compare builtin found");
5341 expression->base.source_position = *HERE;
5345 expression->binary.left = parse_assignment_expression();
5347 expression->binary.right = parse_assignment_expression();
5350 type_t *const orig_type_left = expression->binary.left->base.type;
5351 type_t *const orig_type_right = expression->binary.right->base.type;
5353 type_t *const type_left = skip_typeref(orig_type_left);
5354 type_t *const type_right = skip_typeref(orig_type_right);
5355 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5356 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5357 type_error_incompatible("invalid operands in comparison",
5358 &expression->base.source_position, orig_type_left, orig_type_right);
5361 semantic_comparison(&expression->binary);
5366 return create_invalid_expression();
5370 * Parses a __builtin_expect() expression.
5372 static expression_t *parse_builtin_expect(void)
5374 eat(T___builtin_expect);
5376 expression_t *expression
5377 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5380 expression->binary.left = parse_assignment_expression();
5382 expression->binary.right = parse_constant_expression();
5385 expression->base.type = expression->binary.left->base.type;
5389 return create_invalid_expression();
5393 * Parses a MS assume() expression.
5395 static expression_t *parse_assume(void) {
5398 expression_t *expression
5399 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5402 add_anchor_token(')');
5403 expression->unary.value = parse_assignment_expression();
5404 rem_anchor_token(')');
5407 expression->base.type = type_void;
5410 return create_invalid_expression();
5414 * Parse a microsoft __noop expression.
5416 static expression_t *parse_noop_expression(void) {
5417 source_position_t source_position = *HERE;
5420 if (token.type == '(') {
5421 /* parse arguments */
5423 add_anchor_token(')');
5424 add_anchor_token(',');
5426 if(token.type != ')') {
5428 (void)parse_assignment_expression();
5429 if(token.type != ',')
5435 rem_anchor_token(',');
5436 rem_anchor_token(')');
5439 /* the result is a (int)0 */
5440 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5441 cnst->base.source_position = source_position;
5442 cnst->base.type = type_int;
5443 cnst->conste.v.int_value = 0;
5444 cnst->conste.is_ms_noop = true;
5449 return create_invalid_expression();
5453 * Parses a primary expression.
5455 static expression_t *parse_primary_expression(void)
5457 switch (token.type) {
5458 case T_INTEGER: return parse_int_const();
5459 case T_CHARACTER_CONSTANT: return parse_character_constant();
5460 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5461 case T_FLOATINGPOINT: return parse_float_const();
5462 case T_STRING_LITERAL:
5463 case T_WIDE_STRING_LITERAL: return parse_string_const();
5464 case T_IDENTIFIER: return parse_reference();
5465 case T___FUNCTION__:
5466 case T___func__: return parse_function_keyword();
5467 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5468 case T___FUNCSIG__: return parse_funcsig_keyword();
5469 case T___FUNCDNAME__: return parse_funcdname_keyword();
5470 case T___builtin_offsetof: return parse_offsetof();
5471 case T___builtin_va_start: return parse_va_start();
5472 case T___builtin_va_arg: return parse_va_arg();
5473 case T___builtin_expect: return parse_builtin_expect();
5474 case T___builtin_alloca:
5475 case T___builtin_nan:
5476 case T___builtin_nand:
5477 case T___builtin_nanf:
5478 case T___builtin_va_end: return parse_builtin_symbol();
5479 case T___builtin_isgreater:
5480 case T___builtin_isgreaterequal:
5481 case T___builtin_isless:
5482 case T___builtin_islessequal:
5483 case T___builtin_islessgreater:
5484 case T___builtin_isunordered: return parse_compare_builtin();
5485 case T___builtin_constant_p: return parse_builtin_constant();
5486 case T___builtin_prefetch: return parse_builtin_prefetch();
5487 case T__assume: return parse_assume();
5489 case '(': return parse_brace_expression();
5490 case T___noop: return parse_noop_expression();
5493 errorf(HERE, "unexpected token %K, expected an expression", &token);
5494 return create_invalid_expression();
5498 * Check if the expression has the character type and issue a warning then.
5500 static void check_for_char_index_type(const expression_t *expression) {
5501 type_t *const type = expression->base.type;
5502 const type_t *const base_type = skip_typeref(type);
5504 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5505 warning.char_subscripts) {
5506 warningf(&expression->base.source_position,
5507 "array subscript has type '%T'", type);
5511 static expression_t *parse_array_expression(unsigned precedence,
5517 add_anchor_token(']');
5519 expression_t *inside = parse_expression();
5521 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5523 array_access_expression_t *array_access = &expression->array_access;
5525 type_t *const orig_type_left = left->base.type;
5526 type_t *const orig_type_inside = inside->base.type;
5528 type_t *const type_left = skip_typeref(orig_type_left);
5529 type_t *const type_inside = skip_typeref(orig_type_inside);
5531 type_t *return_type;
5532 if (is_type_pointer(type_left)) {
5533 return_type = type_left->pointer.points_to;
5534 array_access->array_ref = left;
5535 array_access->index = inside;
5536 check_for_char_index_type(inside);
5537 } else if (is_type_pointer(type_inside)) {
5538 return_type = type_inside->pointer.points_to;
5539 array_access->array_ref = inside;
5540 array_access->index = left;
5541 array_access->flipped = true;
5542 check_for_char_index_type(left);
5544 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5546 "array access on object with non-pointer types '%T', '%T'",
5547 orig_type_left, orig_type_inside);
5549 return_type = type_error_type;
5550 array_access->array_ref = create_invalid_expression();
5553 rem_anchor_token(']');
5554 if(token.type != ']') {
5555 parse_error_expected("Problem while parsing array access", ']', NULL);
5560 return_type = automatic_type_conversion(return_type);
5561 expression->base.type = return_type;
5566 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5568 expression_t *tp_expression = allocate_expression_zero(kind);
5569 tp_expression->base.type = type_size_t;
5571 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5573 add_anchor_token(')');
5574 tp_expression->typeprop.type = parse_typename();
5575 rem_anchor_token(')');
5578 expression_t *expression = parse_sub_expression(precedence);
5579 expression->base.type = revert_automatic_type_conversion(expression);
5581 tp_expression->typeprop.type = expression->base.type;
5582 tp_expression->typeprop.tp_expression = expression;
5585 return tp_expression;
5587 return create_invalid_expression();
5590 static expression_t *parse_sizeof(unsigned precedence)
5593 return parse_typeprop(EXPR_SIZEOF, precedence);
5596 static expression_t *parse_alignof(unsigned precedence)
5599 return parse_typeprop(EXPR_SIZEOF, precedence);
5602 static expression_t *parse_select_expression(unsigned precedence,
5603 expression_t *compound)
5606 assert(token.type == '.' || token.type == T_MINUSGREATER);
5608 bool is_pointer = (token.type == T_MINUSGREATER);
5611 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5612 select->select.compound = compound;
5614 if(token.type != T_IDENTIFIER) {
5615 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
5618 symbol_t *symbol = token.v.symbol;
5619 select->select.symbol = symbol;
5622 type_t *const orig_type = compound->base.type;
5623 type_t *const type = skip_typeref(orig_type);
5625 type_t *type_left = type;
5627 if (!is_type_pointer(type)) {
5628 if (is_type_valid(type)) {
5629 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5631 return create_invalid_expression();
5633 type_left = type->pointer.points_to;
5635 type_left = skip_typeref(type_left);
5637 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5638 type_left->kind != TYPE_COMPOUND_UNION) {
5639 if (is_type_valid(type_left)) {
5640 errorf(HERE, "request for member '%Y' in something not a struct or "
5641 "union, but '%T'", symbol, type_left);
5643 return create_invalid_expression();
5646 declaration_t *const declaration = type_left->compound.declaration;
5648 if(!declaration->init.is_defined) {
5649 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5651 return create_invalid_expression();
5654 declaration_t *iter = find_compound_entry(declaration, symbol);
5656 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5657 return create_invalid_expression();
5660 /* we always do the auto-type conversions; the & and sizeof parser contains
5661 * code to revert this! */
5662 type_t *expression_type = automatic_type_conversion(iter->type);
5664 select->select.compound_entry = iter;
5665 select->base.type = expression_type;
5667 if(expression_type->kind == TYPE_BITFIELD) {
5668 expression_t *extract
5669 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5670 extract->unary.value = select;
5671 extract->base.type = expression_type->bitfield.base_type;
5680 * Parse a call expression, ie. expression '( ... )'.
5682 * @param expression the function address
5684 static expression_t *parse_call_expression(unsigned precedence,
5685 expression_t *expression)
5688 expression_t *result = allocate_expression_zero(EXPR_CALL);
5689 result->base.source_position = expression->base.source_position;
5691 call_expression_t *call = &result->call;
5692 call->function = expression;
5694 type_t *const orig_type = expression->base.type;
5695 type_t *const type = skip_typeref(orig_type);
5697 function_type_t *function_type = NULL;
5698 if (is_type_pointer(type)) {
5699 type_t *const to_type = skip_typeref(type->pointer.points_to);
5701 if (is_type_function(to_type)) {
5702 function_type = &to_type->function;
5703 call->base.type = function_type->return_type;
5707 if (function_type == NULL && is_type_valid(type)) {
5708 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5711 /* parse arguments */
5713 add_anchor_token(')');
5714 add_anchor_token(',');
5716 if(token.type != ')') {
5717 call_argument_t *last_argument = NULL;
5720 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5722 argument->expression = parse_assignment_expression();
5723 if(last_argument == NULL) {
5724 call->arguments = argument;
5726 last_argument->next = argument;
5728 last_argument = argument;
5730 if(token.type != ',')
5735 rem_anchor_token(',');
5736 rem_anchor_token(')');
5739 if(function_type != NULL) {
5740 function_parameter_t *parameter = function_type->parameters;
5741 call_argument_t *argument = call->arguments;
5742 for( ; parameter != NULL && argument != NULL;
5743 parameter = parameter->next, argument = argument->next) {
5744 type_t *expected_type = parameter->type;
5745 /* TODO report scope in error messages */
5746 expression_t *const arg_expr = argument->expression;
5747 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5749 &arg_expr->base.source_position);
5750 if (res_type == NULL) {
5751 /* TODO improve error message */
5752 errorf(&arg_expr->base.source_position,
5753 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5754 arg_expr, arg_expr->base.type, expected_type);
5756 argument->expression = create_implicit_cast(argument->expression, expected_type);
5759 /* too few parameters */
5760 if(parameter != NULL) {
5761 errorf(HERE, "too few arguments to function '%E'", expression);
5762 } else if(argument != NULL) {
5763 /* too many parameters */
5764 if(!function_type->variadic
5765 && !function_type->unspecified_parameters) {
5766 errorf(HERE, "too many arguments to function '%E'", expression);
5768 /* do default promotion */
5769 for( ; argument != NULL; argument = argument->next) {
5770 type_t *type = argument->expression->base.type;
5772 type = skip_typeref(type);
5773 if(is_type_integer(type)) {
5774 type = promote_integer(type);
5775 } else if(type == type_float) {
5779 argument->expression
5780 = create_implicit_cast(argument->expression, type);
5783 check_format(&result->call);
5786 check_format(&result->call);
5792 return create_invalid_expression();
5795 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5797 static bool same_compound_type(const type_t *type1, const type_t *type2)
5800 is_type_compound(type1) &&
5801 type1->kind == type2->kind &&
5802 type1->compound.declaration == type2->compound.declaration;
5806 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5808 * @param expression the conditional expression
5810 static expression_t *parse_conditional_expression(unsigned precedence,
5811 expression_t *expression)
5814 add_anchor_token(':');
5816 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5818 conditional_expression_t *conditional = &result->conditional;
5819 conditional->condition = expression;
5822 type_t *const condition_type_orig = expression->base.type;
5823 type_t *const condition_type = skip_typeref(condition_type_orig);
5824 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5825 type_error("expected a scalar type in conditional condition",
5826 &expression->base.source_position, condition_type_orig);
5829 expression_t *true_expression = parse_expression();
5830 rem_anchor_token(':');
5832 expression_t *false_expression = parse_sub_expression(precedence);
5834 type_t *const orig_true_type = true_expression->base.type;
5835 type_t *const orig_false_type = false_expression->base.type;
5836 type_t *const true_type = skip_typeref(orig_true_type);
5837 type_t *const false_type = skip_typeref(orig_false_type);
5840 type_t *result_type;
5841 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5842 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5843 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5844 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5845 warningf(&expression->base.source_position,
5846 "ISO C forbids conditional expression with only one void side");
5848 result_type = type_void;
5849 } else if (is_type_arithmetic(true_type)
5850 && is_type_arithmetic(false_type)) {
5851 result_type = semantic_arithmetic(true_type, false_type);
5853 true_expression = create_implicit_cast(true_expression, result_type);
5854 false_expression = create_implicit_cast(false_expression, result_type);
5856 conditional->true_expression = true_expression;
5857 conditional->false_expression = false_expression;
5858 conditional->base.type = result_type;
5859 } else if (same_compound_type(true_type, false_type)) {
5860 /* just take 1 of the 2 types */
5861 result_type = true_type;
5862 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5863 type_t *pointer_type;
5865 expression_t *other_expression;
5866 if (is_type_pointer(true_type)) {
5867 pointer_type = true_type;
5868 other_type = false_type;
5869 other_expression = false_expression;
5871 pointer_type = false_type;
5872 other_type = true_type;
5873 other_expression = true_expression;
5876 if(is_type_pointer(other_type)) {
5877 if(!pointers_compatible(true_type, false_type)) {
5878 warningf(&expression->base.source_position,
5879 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5881 result_type = true_type;
5882 } else if(is_null_pointer_constant(other_expression)) {
5883 result_type = pointer_type;
5884 } else if(is_type_integer(other_type)) {
5885 warningf(&expression->base.source_position,
5886 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5887 result_type = pointer_type;
5889 type_error_incompatible("while parsing conditional",
5890 &expression->base.source_position, true_type, false_type);
5891 result_type = type_error_type;
5894 /* TODO: one pointer to void*, other some pointer */
5896 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5897 type_error_incompatible("while parsing conditional",
5898 &expression->base.source_position, true_type,
5901 result_type = type_error_type;
5904 conditional->true_expression
5905 = create_implicit_cast(true_expression, result_type);
5906 conditional->false_expression
5907 = create_implicit_cast(false_expression, result_type);
5908 conditional->base.type = result_type;
5911 return create_invalid_expression();
5915 * Parse an extension expression.
5917 static expression_t *parse_extension(unsigned precedence)
5919 eat(T___extension__);
5921 /* TODO enable extensions */
5922 expression_t *expression = parse_sub_expression(precedence);
5923 /* TODO disable extensions */
5928 * Parse a __builtin_classify_type() expression.
5930 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5932 eat(T___builtin_classify_type);
5934 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5935 result->base.type = type_int;
5938 add_anchor_token(')');
5939 expression_t *expression = parse_sub_expression(precedence);
5940 rem_anchor_token(')');
5942 result->classify_type.type_expression = expression;
5946 return create_invalid_expression();
5949 static void semantic_incdec(unary_expression_t *expression)
5951 type_t *const orig_type = expression->value->base.type;
5952 type_t *const type = skip_typeref(orig_type);
5953 /* TODO !is_type_real && !is_type_pointer */
5954 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5955 if (is_type_valid(type)) {
5956 /* TODO: improve error message */
5957 errorf(HERE, "operation needs an arithmetic or pointer type");
5962 expression->base.type = orig_type;
5965 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5967 type_t *const orig_type = expression->value->base.type;
5968 type_t *const type = skip_typeref(orig_type);
5969 if(!is_type_arithmetic(type)) {
5970 if (is_type_valid(type)) {
5971 /* TODO: improve error message */
5972 errorf(HERE, "operation needs an arithmetic type");
5977 expression->base.type = orig_type;
5980 static void semantic_unexpr_scalar(unary_expression_t *expression)
5982 type_t *const orig_type = expression->value->base.type;
5983 type_t *const type = skip_typeref(orig_type);
5984 if (!is_type_scalar(type)) {
5985 if (is_type_valid(type)) {
5986 errorf(HERE, "operand of ! must be of scalar type");
5991 expression->base.type = orig_type;
5994 static void semantic_unexpr_integer(unary_expression_t *expression)
5996 type_t *const orig_type = expression->value->base.type;
5997 type_t *const type = skip_typeref(orig_type);
5998 if (!is_type_integer(type)) {
5999 if (is_type_valid(type)) {
6000 errorf(HERE, "operand of ~ must be of integer type");
6005 expression->base.type = orig_type;
6008 static void semantic_dereference(unary_expression_t *expression)
6010 type_t *const orig_type = expression->value->base.type;
6011 type_t *const type = skip_typeref(orig_type);
6012 if(!is_type_pointer(type)) {
6013 if (is_type_valid(type)) {
6014 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6019 type_t *result_type = type->pointer.points_to;
6020 result_type = automatic_type_conversion(result_type);
6021 expression->base.type = result_type;
6025 * Check the semantic of the address taken expression.
6027 static void semantic_take_addr(unary_expression_t *expression)
6029 expression_t *value = expression->value;
6030 value->base.type = revert_automatic_type_conversion(value);
6032 type_t *orig_type = value->base.type;
6033 if(!is_type_valid(orig_type))
6036 if(value->kind == EXPR_REFERENCE) {
6037 declaration_t *const declaration = value->reference.declaration;
6038 if(declaration != NULL) {
6039 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6040 errorf(&expression->base.source_position,
6041 "address of register variable '%Y' requested",
6042 declaration->symbol);
6044 declaration->address_taken = 1;
6048 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6051 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6052 static expression_t *parse_##unexpression_type(unsigned precedence) \
6056 expression_t *unary_expression \
6057 = allocate_expression_zero(unexpression_type); \
6058 unary_expression->base.source_position = *HERE; \
6059 unary_expression->unary.value = parse_sub_expression(precedence); \
6061 sfunc(&unary_expression->unary); \
6063 return unary_expression; \
6066 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6067 semantic_unexpr_arithmetic)
6068 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6069 semantic_unexpr_arithmetic)
6070 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6071 semantic_unexpr_scalar)
6072 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6073 semantic_dereference)
6074 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6076 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6077 semantic_unexpr_integer)
6078 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6080 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6083 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6085 static expression_t *parse_##unexpression_type(unsigned precedence, \
6086 expression_t *left) \
6088 (void) precedence; \
6091 expression_t *unary_expression \
6092 = allocate_expression_zero(unexpression_type); \
6093 unary_expression->unary.value = left; \
6095 sfunc(&unary_expression->unary); \
6097 return unary_expression; \
6100 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6101 EXPR_UNARY_POSTFIX_INCREMENT,
6103 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6104 EXPR_UNARY_POSTFIX_DECREMENT,
6107 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6109 /* TODO: handle complex + imaginary types */
6111 /* § 6.3.1.8 Usual arithmetic conversions */
6112 if(type_left == type_long_double || type_right == type_long_double) {
6113 return type_long_double;
6114 } else if(type_left == type_double || type_right == type_double) {
6116 } else if(type_left == type_float || type_right == type_float) {
6120 type_right = promote_integer(type_right);
6121 type_left = promote_integer(type_left);
6123 if(type_left == type_right)
6126 bool signed_left = is_type_signed(type_left);
6127 bool signed_right = is_type_signed(type_right);
6128 int rank_left = get_rank(type_left);
6129 int rank_right = get_rank(type_right);
6130 if(rank_left < rank_right) {
6131 if(signed_left == signed_right || !signed_right) {
6137 if(signed_left == signed_right || !signed_left) {
6146 * Check the semantic restrictions for a binary expression.
6148 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6150 expression_t *const left = expression->left;
6151 expression_t *const right = expression->right;
6152 type_t *const orig_type_left = left->base.type;
6153 type_t *const orig_type_right = right->base.type;
6154 type_t *const type_left = skip_typeref(orig_type_left);
6155 type_t *const type_right = skip_typeref(orig_type_right);
6157 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6158 /* TODO: improve error message */
6159 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6160 errorf(HERE, "operation needs arithmetic types");
6165 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6166 expression->left = create_implicit_cast(left, arithmetic_type);
6167 expression->right = create_implicit_cast(right, arithmetic_type);
6168 expression->base.type = arithmetic_type;
6171 static void semantic_shift_op(binary_expression_t *expression)
6173 expression_t *const left = expression->left;
6174 expression_t *const right = expression->right;
6175 type_t *const orig_type_left = left->base.type;
6176 type_t *const orig_type_right = right->base.type;
6177 type_t * type_left = skip_typeref(orig_type_left);
6178 type_t * type_right = skip_typeref(orig_type_right);
6180 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6181 /* TODO: improve error message */
6182 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6183 errorf(HERE, "operation needs integer types");
6188 type_left = promote_integer(type_left);
6189 type_right = promote_integer(type_right);
6191 expression->left = create_implicit_cast(left, type_left);
6192 expression->right = create_implicit_cast(right, type_right);
6193 expression->base.type = type_left;
6196 static void semantic_add(binary_expression_t *expression)
6198 expression_t *const left = expression->left;
6199 expression_t *const right = expression->right;
6200 type_t *const orig_type_left = left->base.type;
6201 type_t *const orig_type_right = right->base.type;
6202 type_t *const type_left = skip_typeref(orig_type_left);
6203 type_t *const type_right = skip_typeref(orig_type_right);
6206 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6207 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6208 expression->left = create_implicit_cast(left, arithmetic_type);
6209 expression->right = create_implicit_cast(right, arithmetic_type);
6210 expression->base.type = arithmetic_type;
6212 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6213 expression->base.type = type_left;
6214 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6215 expression->base.type = type_right;
6216 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6217 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6221 static void semantic_sub(binary_expression_t *expression)
6223 expression_t *const left = expression->left;
6224 expression_t *const right = expression->right;
6225 type_t *const orig_type_left = left->base.type;
6226 type_t *const orig_type_right = right->base.type;
6227 type_t *const type_left = skip_typeref(orig_type_left);
6228 type_t *const type_right = skip_typeref(orig_type_right);
6231 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6232 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6233 expression->left = create_implicit_cast(left, arithmetic_type);
6234 expression->right = create_implicit_cast(right, arithmetic_type);
6235 expression->base.type = arithmetic_type;
6237 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6238 expression->base.type = type_left;
6239 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6240 if(!pointers_compatible(type_left, type_right)) {
6242 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6243 orig_type_left, orig_type_right);
6245 expression->base.type = type_ptrdiff_t;
6247 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6248 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6249 orig_type_left, orig_type_right);
6254 * Check the semantics of comparison expressions.
6256 * @param expression The expression to check.
6258 static void semantic_comparison(binary_expression_t *expression)
6260 expression_t *left = expression->left;
6261 expression_t *right = expression->right;
6262 type_t *orig_type_left = left->base.type;
6263 type_t *orig_type_right = right->base.type;
6265 type_t *type_left = skip_typeref(orig_type_left);
6266 type_t *type_right = skip_typeref(orig_type_right);
6268 /* TODO non-arithmetic types */
6269 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6270 /* test for signed vs unsigned compares */
6271 if (warning.sign_compare &&
6272 (expression->base.kind != EXPR_BINARY_EQUAL &&
6273 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6274 (is_type_signed(type_left) != is_type_signed(type_right))) {
6276 /* check if 1 of the operands is a constant, in this case we just
6277 * check wether we can safely represent the resulting constant in
6278 * the type of the other operand. */
6279 expression_t *const_expr = NULL;
6280 expression_t *other_expr = NULL;
6282 if(is_constant_expression(left)) {
6285 } else if(is_constant_expression(right)) {
6290 type_t *other_type = skip_typeref(other_expr->base.type);
6291 if(const_expr != NULL) {
6292 long val = fold_constant(const_expr);
6293 /* TODO: check if val can be represented by other_type */
6297 warningf(&expression->base.source_position,
6298 "comparison between signed and unsigned");
6300 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6301 expression->left = create_implicit_cast(left, arithmetic_type);
6302 expression->right = create_implicit_cast(right, arithmetic_type);
6303 expression->base.type = arithmetic_type;
6304 if (warning.float_equal &&
6305 (expression->base.kind == EXPR_BINARY_EQUAL ||
6306 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6307 is_type_float(arithmetic_type)) {
6308 warningf(&expression->base.source_position,
6309 "comparing floating point with == or != is unsafe");
6311 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6312 /* TODO check compatibility */
6313 } else if (is_type_pointer(type_left)) {
6314 expression->right = create_implicit_cast(right, type_left);
6315 } else if (is_type_pointer(type_right)) {
6316 expression->left = create_implicit_cast(left, type_right);
6317 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6318 type_error_incompatible("invalid operands in comparison",
6319 &expression->base.source_position,
6320 type_left, type_right);
6322 expression->base.type = type_int;
6325 static void semantic_arithmetic_assign(binary_expression_t *expression)
6327 expression_t *left = expression->left;
6328 expression_t *right = expression->right;
6329 type_t *orig_type_left = left->base.type;
6330 type_t *orig_type_right = right->base.type;
6332 type_t *type_left = skip_typeref(orig_type_left);
6333 type_t *type_right = skip_typeref(orig_type_right);
6335 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6336 /* TODO: improve error message */
6337 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6338 errorf(HERE, "operation needs arithmetic types");
6343 /* combined instructions are tricky. We can't create an implicit cast on
6344 * the left side, because we need the uncasted form for the store.
6345 * The ast2firm pass has to know that left_type must be right_type
6346 * for the arithmetic operation and create a cast by itself */
6347 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6348 expression->right = create_implicit_cast(right, arithmetic_type);
6349 expression->base.type = type_left;
6352 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6354 expression_t *const left = expression->left;
6355 expression_t *const right = expression->right;
6356 type_t *const orig_type_left = left->base.type;
6357 type_t *const orig_type_right = right->base.type;
6358 type_t *const type_left = skip_typeref(orig_type_left);
6359 type_t *const type_right = skip_typeref(orig_type_right);
6361 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6362 /* combined instructions are tricky. We can't create an implicit cast on
6363 * the left side, because we need the uncasted form for the store.
6364 * The ast2firm pass has to know that left_type must be right_type
6365 * for the arithmetic operation and create a cast by itself */
6366 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6367 expression->right = create_implicit_cast(right, arithmetic_type);
6368 expression->base.type = type_left;
6369 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6370 expression->base.type = type_left;
6371 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6372 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6377 * Check the semantic restrictions of a logical expression.
6379 static void semantic_logical_op(binary_expression_t *expression)
6381 expression_t *const left = expression->left;
6382 expression_t *const right = expression->right;
6383 type_t *const orig_type_left = left->base.type;
6384 type_t *const orig_type_right = right->base.type;
6385 type_t *const type_left = skip_typeref(orig_type_left);
6386 type_t *const type_right = skip_typeref(orig_type_right);
6388 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6389 /* TODO: improve error message */
6390 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6391 errorf(HERE, "operation needs scalar types");
6396 expression->base.type = type_int;
6400 * Checks if a compound type has constant fields.
6402 static bool has_const_fields(const compound_type_t *type)
6404 const scope_t *scope = &type->declaration->scope;
6405 const declaration_t *declaration = scope->declarations;
6407 for (; declaration != NULL; declaration = declaration->next) {
6408 if (declaration->namespc != NAMESPACE_NORMAL)
6411 const type_t *decl_type = skip_typeref(declaration->type);
6412 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6420 * Check the semantic restrictions of a binary assign expression.
6422 static void semantic_binexpr_assign(binary_expression_t *expression)
6424 expression_t *left = expression->left;
6425 type_t *orig_type_left = left->base.type;
6427 type_t *type_left = revert_automatic_type_conversion(left);
6428 type_left = skip_typeref(orig_type_left);
6430 /* must be a modifiable lvalue */
6431 if (is_type_array(type_left)) {
6432 errorf(HERE, "cannot assign to arrays ('%E')", left);
6435 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6436 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6440 if(is_type_incomplete(type_left)) {
6442 "left-hand side of assignment '%E' has incomplete type '%T'",
6443 left, orig_type_left);
6446 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6447 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6448 left, orig_type_left);
6452 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6453 "assignment", &left->base.source_position);
6454 if (res_type == NULL) {
6455 errorf(&expression->base.source_position,
6456 "cannot assign to '%T' from '%T'",
6457 orig_type_left, expression->right->base.type);
6459 expression->right = create_implicit_cast(expression->right, res_type);
6462 expression->base.type = orig_type_left;
6466 * Determine if the outermost operation (or parts thereof) of the given
6467 * expression has no effect in order to generate a warning about this fact.
6468 * Therefore in some cases this only examines some of the operands of the
6469 * expression (see comments in the function and examples below).
6471 * f() + 23; // warning, because + has no effect
6472 * x || f(); // no warning, because x controls execution of f()
6473 * x ? y : f(); // warning, because y has no effect
6474 * (void)x; // no warning to be able to suppress the warning
6475 * This function can NOT be used for an "expression has definitely no effect"-
6477 static bool expression_has_effect(const expression_t *const expr)
6479 switch (expr->kind) {
6480 case EXPR_UNKNOWN: break;
6481 case EXPR_INVALID: return true; /* do NOT warn */
6482 case EXPR_REFERENCE: return false;
6483 /* suppress the warning for microsoft __noop operations */
6484 case EXPR_CONST: return expr->conste.is_ms_noop;
6485 case EXPR_CHARACTER_CONSTANT: return false;
6486 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6487 case EXPR_STRING_LITERAL: return false;
6488 case EXPR_WIDE_STRING_LITERAL: return false;
6491 const call_expression_t *const call = &expr->call;
6492 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6495 switch (call->function->builtin_symbol.symbol->ID) {
6496 case T___builtin_va_end: return true;
6497 default: return false;
6501 /* Generate the warning if either the left or right hand side of a
6502 * conditional expression has no effect */
6503 case EXPR_CONDITIONAL: {
6504 const conditional_expression_t *const cond = &expr->conditional;
6506 expression_has_effect(cond->true_expression) &&
6507 expression_has_effect(cond->false_expression);
6510 case EXPR_SELECT: return false;
6511 case EXPR_ARRAY_ACCESS: return false;
6512 case EXPR_SIZEOF: return false;
6513 case EXPR_CLASSIFY_TYPE: return false;
6514 case EXPR_ALIGNOF: return false;
6516 case EXPR_FUNCNAME: return false;
6517 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6518 case EXPR_BUILTIN_CONSTANT_P: return false;
6519 case EXPR_BUILTIN_PREFETCH: return true;
6520 case EXPR_OFFSETOF: return false;
6521 case EXPR_VA_START: return true;
6522 case EXPR_VA_ARG: return true;
6523 case EXPR_STATEMENT: return true; // TODO
6524 case EXPR_COMPOUND_LITERAL: return false;
6526 case EXPR_UNARY_NEGATE: return false;
6527 case EXPR_UNARY_PLUS: return false;
6528 case EXPR_UNARY_BITWISE_NEGATE: return false;
6529 case EXPR_UNARY_NOT: return false;
6530 case EXPR_UNARY_DEREFERENCE: return false;
6531 case EXPR_UNARY_TAKE_ADDRESS: return false;
6532 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6533 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6534 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6535 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6537 /* Treat void casts as if they have an effect in order to being able to
6538 * suppress the warning */
6539 case EXPR_UNARY_CAST: {
6540 type_t *const type = skip_typeref(expr->base.type);
6541 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6544 case EXPR_UNARY_CAST_IMPLICIT: return true;
6545 case EXPR_UNARY_ASSUME: return true;
6546 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6548 case EXPR_BINARY_ADD: return false;
6549 case EXPR_BINARY_SUB: return false;
6550 case EXPR_BINARY_MUL: return false;
6551 case EXPR_BINARY_DIV: return false;
6552 case EXPR_BINARY_MOD: return false;
6553 case EXPR_BINARY_EQUAL: return false;
6554 case EXPR_BINARY_NOTEQUAL: return false;
6555 case EXPR_BINARY_LESS: return false;
6556 case EXPR_BINARY_LESSEQUAL: return false;
6557 case EXPR_BINARY_GREATER: return false;
6558 case EXPR_BINARY_GREATEREQUAL: return false;
6559 case EXPR_BINARY_BITWISE_AND: return false;
6560 case EXPR_BINARY_BITWISE_OR: return false;
6561 case EXPR_BINARY_BITWISE_XOR: return false;
6562 case EXPR_BINARY_SHIFTLEFT: return false;
6563 case EXPR_BINARY_SHIFTRIGHT: return false;
6564 case EXPR_BINARY_ASSIGN: return true;
6565 case EXPR_BINARY_MUL_ASSIGN: return true;
6566 case EXPR_BINARY_DIV_ASSIGN: return true;
6567 case EXPR_BINARY_MOD_ASSIGN: return true;
6568 case EXPR_BINARY_ADD_ASSIGN: return true;
6569 case EXPR_BINARY_SUB_ASSIGN: return true;
6570 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6571 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6572 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6573 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6574 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6576 /* Only examine the right hand side of && and ||, because the left hand
6577 * side already has the effect of controlling the execution of the right
6579 case EXPR_BINARY_LOGICAL_AND:
6580 case EXPR_BINARY_LOGICAL_OR:
6581 /* Only examine the right hand side of a comma expression, because the left
6582 * hand side has a separate warning */
6583 case EXPR_BINARY_COMMA:
6584 return expression_has_effect(expr->binary.right);
6586 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6587 case EXPR_BINARY_ISGREATER: return false;
6588 case EXPR_BINARY_ISGREATEREQUAL: return false;
6589 case EXPR_BINARY_ISLESS: return false;
6590 case EXPR_BINARY_ISLESSEQUAL: return false;
6591 case EXPR_BINARY_ISLESSGREATER: return false;
6592 case EXPR_BINARY_ISUNORDERED: return false;
6595 internal_errorf(HERE, "unexpected expression");
6598 static void semantic_comma(binary_expression_t *expression)
6600 if (warning.unused_value) {
6601 const expression_t *const left = expression->left;
6602 if (!expression_has_effect(left)) {
6603 warningf(&left->base.source_position,
6604 "left-hand operand of comma expression has no effect");
6607 expression->base.type = expression->right->base.type;
6610 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6611 static expression_t *parse_##binexpression_type(unsigned precedence, \
6612 expression_t *left) \
6615 source_position_t pos = *HERE; \
6617 expression_t *right = parse_sub_expression(precedence + lr); \
6619 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6620 binexpr->base.source_position = pos; \
6621 binexpr->binary.left = left; \
6622 binexpr->binary.right = right; \
6623 sfunc(&binexpr->binary); \
6628 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6629 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6630 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6631 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6632 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6633 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6634 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6635 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6636 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6638 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6639 semantic_comparison, 1)
6640 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6641 semantic_comparison, 1)
6642 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6643 semantic_comparison, 1)
6644 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6645 semantic_comparison, 1)
6647 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6648 semantic_binexpr_arithmetic, 1)
6649 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6650 semantic_binexpr_arithmetic, 1)
6651 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6652 semantic_binexpr_arithmetic, 1)
6653 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6654 semantic_logical_op, 1)
6655 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6656 semantic_logical_op, 1)
6657 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6658 semantic_shift_op, 1)
6659 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6660 semantic_shift_op, 1)
6661 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6662 semantic_arithmetic_addsubb_assign, 0)
6663 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6664 semantic_arithmetic_addsubb_assign, 0)
6665 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6666 semantic_arithmetic_assign, 0)
6667 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6668 semantic_arithmetic_assign, 0)
6669 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6670 semantic_arithmetic_assign, 0)
6671 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6672 semantic_arithmetic_assign, 0)
6673 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6674 semantic_arithmetic_assign, 0)
6675 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6676 semantic_arithmetic_assign, 0)
6677 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6678 semantic_arithmetic_assign, 0)
6679 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6680 semantic_arithmetic_assign, 0)
6682 static expression_t *parse_sub_expression(unsigned precedence)
6684 if(token.type < 0) {
6685 return expected_expression_error();
6688 expression_parser_function_t *parser
6689 = &expression_parsers[token.type];
6690 source_position_t source_position = token.source_position;
6693 if(parser->parser != NULL) {
6694 left = parser->parser(parser->precedence);
6696 left = parse_primary_expression();
6698 assert(left != NULL);
6699 left->base.source_position = source_position;
6702 if(token.type < 0) {
6703 return expected_expression_error();
6706 parser = &expression_parsers[token.type];
6707 if(parser->infix_parser == NULL)
6709 if(parser->infix_precedence < precedence)
6712 left = parser->infix_parser(parser->infix_precedence, left);
6714 assert(left != NULL);
6715 assert(left->kind != EXPR_UNKNOWN);
6716 left->base.source_position = source_position;
6723 * Parse an expression.
6725 static expression_t *parse_expression(void)
6727 return parse_sub_expression(1);
6731 * Register a parser for a prefix-like operator with given precedence.
6733 * @param parser the parser function
6734 * @param token_type the token type of the prefix token
6735 * @param precedence the precedence of the operator
6737 static void register_expression_parser(parse_expression_function parser,
6738 int token_type, unsigned precedence)
6740 expression_parser_function_t *entry = &expression_parsers[token_type];
6742 if(entry->parser != NULL) {
6743 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6744 panic("trying to register multiple expression parsers for a token");
6746 entry->parser = parser;
6747 entry->precedence = precedence;
6751 * Register a parser for an infix operator with given precedence.
6753 * @param parser the parser function
6754 * @param token_type the token type of the infix operator
6755 * @param precedence the precedence of the operator
6757 static void register_infix_parser(parse_expression_infix_function parser,
6758 int token_type, unsigned precedence)
6760 expression_parser_function_t *entry = &expression_parsers[token_type];
6762 if(entry->infix_parser != NULL) {
6763 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6764 panic("trying to register multiple infix expression parsers for a "
6767 entry->infix_parser = parser;
6768 entry->infix_precedence = precedence;
6772 * Initialize the expression parsers.
6774 static void init_expression_parsers(void)
6776 memset(&expression_parsers, 0, sizeof(expression_parsers));
6778 register_infix_parser(parse_array_expression, '[', 30);
6779 register_infix_parser(parse_call_expression, '(', 30);
6780 register_infix_parser(parse_select_expression, '.', 30);
6781 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6782 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6784 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6787 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6788 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6789 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6790 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6791 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6792 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6793 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6794 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6795 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6796 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6797 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6798 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6799 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6800 T_EXCLAMATIONMARKEQUAL, 13);
6801 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6802 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6803 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6804 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6805 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6806 register_infix_parser(parse_conditional_expression, '?', 7);
6807 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6808 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6809 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6810 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6811 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6812 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6813 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6814 T_LESSLESSEQUAL, 2);
6815 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6816 T_GREATERGREATEREQUAL, 2);
6817 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6819 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6821 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6824 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6826 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6827 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6828 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6829 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6830 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6831 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6832 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6834 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6836 register_expression_parser(parse_sizeof, T_sizeof, 25);
6837 register_expression_parser(parse_alignof, T___alignof__, 25);
6838 register_expression_parser(parse_extension, T___extension__, 25);
6839 register_expression_parser(parse_builtin_classify_type,
6840 T___builtin_classify_type, 25);
6844 * Parse a asm statement constraints specification.
6846 static asm_constraint_t *parse_asm_constraints(void)
6848 asm_constraint_t *result = NULL;
6849 asm_constraint_t *last = NULL;
6851 while(token.type == T_STRING_LITERAL || token.type == '[') {
6852 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6853 memset(constraint, 0, sizeof(constraint[0]));
6855 if(token.type == '[') {
6857 if(token.type != T_IDENTIFIER) {
6858 parse_error_expected("while parsing asm constraint",
6859 T_IDENTIFIER, NULL);
6862 constraint->symbol = token.v.symbol;
6867 constraint->constraints = parse_string_literals();
6869 constraint->expression = parse_expression();
6873 last->next = constraint;
6875 result = constraint;
6879 if(token.type != ',')
6890 * Parse a asm statement clobber specification.
6892 static asm_clobber_t *parse_asm_clobbers(void)
6894 asm_clobber_t *result = NULL;
6895 asm_clobber_t *last = NULL;
6897 while(token.type == T_STRING_LITERAL) {
6898 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6899 clobber->clobber = parse_string_literals();
6902 last->next = clobber;
6908 if(token.type != ',')
6917 * Parse an asm statement.
6919 static statement_t *parse_asm_statement(void)
6923 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6924 statement->base.source_position = token.source_position;
6926 asm_statement_t *asm_statement = &statement->asms;
6928 if(token.type == T_volatile) {
6930 asm_statement->is_volatile = true;
6934 add_anchor_token(')');
6935 add_anchor_token(':');
6936 asm_statement->asm_text = parse_string_literals();
6938 if(token.type != ':') {
6939 rem_anchor_token(':');
6944 asm_statement->inputs = parse_asm_constraints();
6945 if(token.type != ':') {
6946 rem_anchor_token(':');
6951 asm_statement->outputs = parse_asm_constraints();
6952 if(token.type != ':') {
6953 rem_anchor_token(':');
6956 rem_anchor_token(':');
6959 asm_statement->clobbers = parse_asm_clobbers();
6962 rem_anchor_token(')');
6967 return create_invalid_statement();
6971 * Parse a case statement.
6973 static statement_t *parse_case_statement(void)
6977 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6979 statement->base.source_position = token.source_position;
6980 statement->case_label.expression = parse_expression();
6982 if (c_mode & _GNUC) {
6983 if (token.type == T_DOTDOTDOT) {
6985 statement->case_label.end_range = parse_expression();
6991 if (! is_constant_expression(statement->case_label.expression)) {
6992 errorf(&statement->base.source_position,
6993 "case label does not reduce to an integer constant");
6995 /* TODO: check if the case label is already known */
6996 if (current_switch != NULL) {
6997 /* link all cases into the switch statement */
6998 if (current_switch->last_case == NULL) {
6999 current_switch->first_case =
7000 current_switch->last_case = &statement->case_label;
7002 current_switch->last_case->next = &statement->case_label;
7005 errorf(&statement->base.source_position,
7006 "case label not within a switch statement");
7009 statement->case_label.statement = parse_statement();
7013 return create_invalid_statement();
7017 * Finds an existing default label of a switch statement.
7019 static case_label_statement_t *
7020 find_default_label(const switch_statement_t *statement)
7022 case_label_statement_t *label = statement->first_case;
7023 for ( ; label != NULL; label = label->next) {
7024 if (label->expression == NULL)
7031 * Parse a default statement.
7033 static statement_t *parse_default_statement(void)
7037 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7039 statement->base.source_position = token.source_position;
7042 if (current_switch != NULL) {
7043 const case_label_statement_t *def_label = find_default_label(current_switch);
7044 if (def_label != NULL) {
7045 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7046 &def_label->base.source_position);
7048 /* link all cases into the switch statement */
7049 if (current_switch->last_case == NULL) {
7050 current_switch->first_case =
7051 current_switch->last_case = &statement->case_label;
7053 current_switch->last_case->next = &statement->case_label;
7057 errorf(&statement->base.source_position,
7058 "'default' label not within a switch statement");
7060 statement->case_label.statement = parse_statement();
7064 return create_invalid_statement();
7068 * Return the declaration for a given label symbol or create a new one.
7070 static declaration_t *get_label(symbol_t *symbol)
7072 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7073 assert(current_function != NULL);
7074 /* if we found a label in the same function, then we already created the
7076 if(candidate != NULL
7077 && candidate->parent_scope == ¤t_function->scope) {
7081 /* otherwise we need to create a new one */
7082 declaration_t *const declaration = allocate_declaration_zero();
7083 declaration->namespc = NAMESPACE_LABEL;
7084 declaration->symbol = symbol;
7086 label_push(declaration);
7092 * Parse a label statement.
7094 static statement_t *parse_label_statement(void)
7096 assert(token.type == T_IDENTIFIER);
7097 symbol_t *symbol = token.v.symbol;
7100 declaration_t *label = get_label(symbol);
7102 /* if source position is already set then the label is defined twice,
7103 * otherwise it was just mentioned in a goto so far */
7104 if(label->source_position.input_name != NULL) {
7105 errorf(HERE, "duplicate label '%Y' (declared %P)",
7106 symbol, &label->source_position);
7108 label->source_position = token.source_position;
7111 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7113 statement->base.source_position = token.source_position;
7114 statement->label.label = label;
7118 if(token.type == '}') {
7119 /* TODO only warn? */
7121 warningf(HERE, "label at end of compound statement");
7122 statement->label.statement = create_empty_statement();
7124 errorf(HERE, "label at end of compound statement");
7125 statement->label.statement = create_invalid_statement();
7129 if (token.type == ';') {
7130 /* eat an empty statement here, to avoid the warning about an empty
7131 * after a label. label:; is commonly used to have a label before
7133 statement->label.statement = create_empty_statement();
7136 statement->label.statement = parse_statement();
7140 /* remember the labels's in a list for later checking */
7141 if (label_last == NULL) {
7142 label_first = &statement->label;
7144 label_last->next = &statement->label;
7146 label_last = &statement->label;
7152 * Parse an if statement.
7154 static statement_t *parse_if(void)
7158 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7159 statement->base.source_position = token.source_position;
7162 add_anchor_token(')');
7163 statement->ifs.condition = parse_expression();
7164 rem_anchor_token(')');
7167 add_anchor_token(T_else);
7168 statement->ifs.true_statement = parse_statement();
7169 rem_anchor_token(T_else);
7171 if(token.type == T_else) {
7173 statement->ifs.false_statement = parse_statement();
7178 return create_invalid_statement();
7182 * Parse a switch statement.
7184 static statement_t *parse_switch(void)
7188 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7189 statement->base.source_position = token.source_position;
7192 expression_t *const expr = parse_expression();
7193 type_t * type = skip_typeref(expr->base.type);
7194 if (is_type_integer(type)) {
7195 type = promote_integer(type);
7196 } else if (is_type_valid(type)) {
7197 errorf(&expr->base.source_position,
7198 "switch quantity is not an integer, but '%T'", type);
7199 type = type_error_type;
7201 statement->switchs.expression = create_implicit_cast(expr, type);
7204 switch_statement_t *rem = current_switch;
7205 current_switch = &statement->switchs;
7206 statement->switchs.body = parse_statement();
7207 current_switch = rem;
7209 if(warning.switch_default &&
7210 find_default_label(&statement->switchs) == NULL) {
7211 warningf(&statement->base.source_position, "switch has no default case");
7216 return create_invalid_statement();
7219 static statement_t *parse_loop_body(statement_t *const loop)
7221 statement_t *const rem = current_loop;
7222 current_loop = loop;
7224 statement_t *const body = parse_statement();
7231 * Parse a while statement.
7233 static statement_t *parse_while(void)
7237 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7238 statement->base.source_position = token.source_position;
7241 add_anchor_token(')');
7242 statement->whiles.condition = parse_expression();
7243 rem_anchor_token(')');
7246 statement->whiles.body = parse_loop_body(statement);
7250 return create_invalid_statement();
7254 * Parse a do statement.
7256 static statement_t *parse_do(void)
7260 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7262 statement->base.source_position = token.source_position;
7264 add_anchor_token(T_while);
7265 statement->do_while.body = parse_loop_body(statement);
7266 rem_anchor_token(T_while);
7270 add_anchor_token(')');
7271 statement->do_while.condition = parse_expression();
7272 rem_anchor_token(')');
7278 return create_invalid_statement();
7282 * Parse a for statement.
7284 static statement_t *parse_for(void)
7288 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7289 statement->base.source_position = token.source_position;
7291 int top = environment_top();
7292 scope_t *last_scope = scope;
7293 set_scope(&statement->fors.scope);
7296 add_anchor_token(')');
7298 if(token.type != ';') {
7299 if(is_declaration_specifier(&token, false)) {
7300 parse_declaration(record_declaration);
7302 add_anchor_token(';');
7303 expression_t *const init = parse_expression();
7304 statement->fors.initialisation = init;
7305 if (warning.unused_value && !expression_has_effect(init)) {
7306 warningf(&init->base.source_position,
7307 "initialisation of 'for'-statement has no effect");
7309 rem_anchor_token(';');
7316 if(token.type != ';') {
7317 add_anchor_token(';');
7318 statement->fors.condition = parse_expression();
7319 rem_anchor_token(';');
7322 if(token.type != ')') {
7323 expression_t *const step = parse_expression();
7324 statement->fors.step = step;
7325 if (warning.unused_value && !expression_has_effect(step)) {
7326 warningf(&step->base.source_position,
7327 "step of 'for'-statement has no effect");
7330 rem_anchor_token(')');
7332 statement->fors.body = parse_loop_body(statement);
7334 assert(scope == &statement->fors.scope);
7335 set_scope(last_scope);
7336 environment_pop_to(top);
7341 rem_anchor_token(')');
7342 assert(scope == &statement->fors.scope);
7343 set_scope(last_scope);
7344 environment_pop_to(top);
7346 return create_invalid_statement();
7350 * Parse a goto statement.
7352 static statement_t *parse_goto(void)
7356 if(token.type != T_IDENTIFIER) {
7357 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
7361 symbol_t *symbol = token.v.symbol;
7364 declaration_t *label = get_label(symbol);
7366 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7367 statement->base.source_position = token.source_position;
7369 statement->gotos.label = label;
7371 /* remember the goto's in a list for later checking */
7372 if (goto_last == NULL) {
7373 goto_first = &statement->gotos;
7375 goto_last->next = &statement->gotos;
7377 goto_last = &statement->gotos;
7383 return create_invalid_statement();
7387 * Parse a continue statement.
7389 static statement_t *parse_continue(void)
7391 statement_t *statement;
7392 if (current_loop == NULL) {
7393 errorf(HERE, "continue statement not within loop");
7394 statement = create_invalid_statement();
7396 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7398 statement->base.source_position = token.source_position;
7406 return create_invalid_statement();
7410 * Parse a break statement.
7412 static statement_t *parse_break(void)
7414 statement_t *statement;
7415 if (current_switch == NULL && current_loop == NULL) {
7416 errorf(HERE, "break statement not within loop or switch");
7417 statement = create_invalid_statement();
7419 statement = allocate_statement_zero(STATEMENT_BREAK);
7421 statement->base.source_position = token.source_position;
7429 return create_invalid_statement();
7433 * Parse a __leave statement.
7435 static statement_t *parse_leave(void)
7437 statement_t *statement;
7438 if (current_try == NULL) {
7439 errorf(HERE, "__leave statement not within __try");
7440 statement = create_invalid_statement();
7442 statement = allocate_statement_zero(STATEMENT_LEAVE);
7444 statement->base.source_position = token.source_position;
7452 return create_invalid_statement();
7456 * Check if a given declaration represents a local variable.
7458 static bool is_local_var_declaration(const declaration_t *declaration) {
7459 switch ((storage_class_tag_t) declaration->storage_class) {
7460 case STORAGE_CLASS_AUTO:
7461 case STORAGE_CLASS_REGISTER: {
7462 const type_t *type = skip_typeref(declaration->type);
7463 if(is_type_function(type)) {
7475 * Check if a given declaration represents a variable.
7477 static bool is_var_declaration(const declaration_t *declaration) {
7478 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7481 const type_t *type = skip_typeref(declaration->type);
7482 return !is_type_function(type);
7486 * Check if a given expression represents a local variable.
7488 static bool is_local_variable(const expression_t *expression)
7490 if (expression->base.kind != EXPR_REFERENCE) {
7493 const declaration_t *declaration = expression->reference.declaration;
7494 return is_local_var_declaration(declaration);
7498 * Check if a given expression represents a local variable and
7499 * return its declaration then, else return NULL.
7501 declaration_t *expr_is_variable(const expression_t *expression)
7503 if (expression->base.kind != EXPR_REFERENCE) {
7506 declaration_t *declaration = expression->reference.declaration;
7507 if (is_var_declaration(declaration))
7513 * Parse a return statement.
7515 static statement_t *parse_return(void)
7517 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7518 statement->base.source_position = token.source_position;
7522 expression_t *return_value = NULL;
7523 if(token.type != ';') {
7524 return_value = parse_expression();
7528 const type_t *const func_type = current_function->type;
7529 assert(is_type_function(func_type));
7530 type_t *const return_type = skip_typeref(func_type->function.return_type);
7532 if(return_value != NULL) {
7533 type_t *return_value_type = skip_typeref(return_value->base.type);
7535 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7536 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7537 warningf(&statement->base.source_position,
7538 "'return' with a value, in function returning void");
7539 return_value = NULL;
7541 type_t *const res_type = semantic_assign(return_type,
7542 return_value, "'return'", &statement->base.source_position);
7543 if (res_type == NULL) {
7544 errorf(&statement->base.source_position,
7545 "cannot return something of type '%T' in function returning '%T'",
7546 return_value->base.type, return_type);
7548 return_value = create_implicit_cast(return_value, res_type);
7551 /* check for returning address of a local var */
7552 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7553 const expression_t *expression = return_value->unary.value;
7554 if (is_local_variable(expression)) {
7555 warningf(&statement->base.source_position,
7556 "function returns address of local variable");
7560 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7561 warningf(&statement->base.source_position,
7562 "'return' without value, in function returning non-void");
7565 statement->returns.value = return_value;
7569 return create_invalid_statement();
7573 * Parse a declaration statement.
7575 static statement_t *parse_declaration_statement(void)
7577 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7579 statement->base.source_position = token.source_position;
7581 declaration_t *before = last_declaration;
7582 parse_declaration(record_declaration);
7584 if(before == NULL) {
7585 statement->declaration.declarations_begin = scope->declarations;
7587 statement->declaration.declarations_begin = before->next;
7589 statement->declaration.declarations_end = last_declaration;
7595 * Parse an expression statement, ie. expr ';'.
7597 static statement_t *parse_expression_statement(void)
7599 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7601 statement->base.source_position = token.source_position;
7602 expression_t *const expr = parse_expression();
7603 statement->expression.expression = expr;
7609 return create_invalid_statement();
7613 * Parse a microsoft __try { } __finally { } or
7614 * __try{ } __except() { }
7616 static statement_t *parse_ms_try_statment(void) {
7617 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
7619 statement->base.source_position = token.source_position;
7622 ms_try_statement_t *rem = current_try;
7623 current_try = &statement->ms_try;
7624 statement->ms_try.try_statement = parse_compound_statement(false);
7627 if(token.type == T___except) {
7630 add_anchor_token(')');
7631 expression_t *const expr = parse_expression();
7632 type_t * type = skip_typeref(expr->base.type);
7633 if (is_type_integer(type)) {
7634 type = promote_integer(type);
7635 } else if (is_type_valid(type)) {
7636 errorf(&expr->base.source_position,
7637 "__expect expression is not an integer, but '%T'", type);
7638 type = type_error_type;
7640 statement->ms_try.except_expression = create_implicit_cast(expr, type);
7641 rem_anchor_token(')');
7643 statement->ms_try.final_statement = parse_compound_statement(false);
7644 } else if(token.type == T__finally) {
7646 statement->ms_try.final_statement = parse_compound_statement(false);
7648 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
7649 return create_invalid_statement();
7653 return create_invalid_statement();
7657 * Parse a statement.
7658 * There's also parse_statement() which additionally checks for
7659 * "statement has no effect" warnings
7661 static statement_t *intern_parse_statement(void)
7663 statement_t *statement = NULL;
7665 /* declaration or statement */
7666 add_anchor_token(';');
7667 switch(token.type) {
7669 statement = parse_asm_statement();
7673 statement = parse_case_statement();
7677 statement = parse_default_statement();
7681 statement = parse_compound_statement(false);
7685 statement = parse_if();
7689 statement = parse_switch();
7693 statement = parse_while();
7697 statement = parse_do();
7701 statement = parse_for();
7705 statement = parse_goto();
7709 statement = parse_continue();
7713 statement = parse_break();
7717 statement = parse_leave();
7721 statement = parse_return();
7725 if(warning.empty_statement) {
7726 warningf(HERE, "statement is empty");
7728 statement = create_empty_statement();
7733 if(look_ahead(1)->type == ':') {
7734 statement = parse_label_statement();
7738 if(is_typedef_symbol(token.v.symbol)) {
7739 statement = parse_declaration_statement();
7743 statement = parse_expression_statement();
7746 case T___extension__:
7747 /* this can be a prefix to a declaration or an expression statement */
7748 /* we simply eat it now and parse the rest with tail recursion */
7751 } while(token.type == T___extension__);
7752 statement = parse_statement();
7756 statement = parse_declaration_statement();
7760 statement = parse_ms_try_statment();
7764 statement = parse_expression_statement();
7767 rem_anchor_token(';');
7769 assert(statement != NULL
7770 && statement->base.source_position.input_name != NULL);
7776 * parse a statement and emits "statement has no effect" warning if needed
7777 * (This is really a wrapper around intern_parse_statement with check for 1
7778 * single warning. It is needed, because for statement expressions we have
7779 * to avoid the warning on the last statement)
7781 static statement_t *parse_statement(void)
7783 statement_t *statement = intern_parse_statement();
7785 if(statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
7786 expression_t *expression = statement->expression.expression;
7787 if(!expression_has_effect(expression)) {
7788 warningf(&expression->base.source_position,
7789 "statement has no effect");
7797 * Parse a compound statement.
7799 static statement_t *parse_compound_statement(bool inside_expression_statement)
7801 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7803 statement->base.source_position = token.source_position;
7806 add_anchor_token('}');
7808 int top = environment_top();
7809 scope_t *last_scope = scope;
7810 set_scope(&statement->compound.scope);
7812 statement_t *last_statement = NULL;
7814 while(token.type != '}' && token.type != T_EOF) {
7815 statement_t *sub_statement = intern_parse_statement();
7816 if(is_invalid_statement(sub_statement)) {
7817 /* an error occurred. if we are at an anchor, return */
7823 if(last_statement != NULL) {
7824 last_statement->base.next = sub_statement;
7826 statement->compound.statements = sub_statement;
7829 while(sub_statement->base.next != NULL)
7830 sub_statement = sub_statement->base.next;
7832 last_statement = sub_statement;
7835 if(token.type == '}') {
7838 errorf(&statement->base.source_position,
7839 "end of file while looking for closing '}'");
7842 /* look over all statements again to produce no effect warnings */
7843 if(warning.unused_value) {
7844 statement_t *sub_statement = statement->compound.statements;
7845 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
7846 if(sub_statement->kind != STATEMENT_EXPRESSION)
7848 /* don't emit a warning for the last expression in an expression
7849 * statement as it has always an effect */
7850 if(inside_expression_statement && sub_statement->base.next == NULL)
7853 expression_t *expression = sub_statement->expression.expression;
7854 if(!expression_has_effect(expression)) {
7855 warningf(&expression->base.source_position,
7856 "statement has no effect");
7862 rem_anchor_token('}');
7863 assert(scope == &statement->compound.scope);
7864 set_scope(last_scope);
7865 environment_pop_to(top);
7871 * Initialize builtin types.
7873 static void initialize_builtin_types(void)
7875 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7876 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7877 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7878 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7879 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7880 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7881 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7882 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7884 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7885 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7886 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7887 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7891 * Check for unused global static functions and variables
7893 static void check_unused_globals(void)
7895 if (!warning.unused_function && !warning.unused_variable)
7898 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7899 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7902 type_t *const type = decl->type;
7904 if (is_type_function(skip_typeref(type))) {
7905 if (!warning.unused_function || decl->is_inline)
7908 s = (decl->init.statement != NULL ? "defined" : "declared");
7910 if (!warning.unused_variable)
7916 warningf(&decl->source_position, "'%#T' %s but not used",
7917 type, decl->symbol, s);
7922 * Parse a translation unit.
7924 static translation_unit_t *parse_translation_unit(void)
7926 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7928 assert(global_scope == NULL);
7929 global_scope = &unit->scope;
7931 assert(scope == NULL);
7932 set_scope(&unit->scope);
7934 initialize_builtin_types();
7936 while(token.type != T_EOF) {
7937 if (token.type == ';') {
7938 /* TODO error in strict mode */
7939 warningf(HERE, "stray ';' outside of function");
7942 parse_external_declaration();
7946 assert(scope == &unit->scope);
7948 last_declaration = NULL;
7950 assert(global_scope == &unit->scope);
7951 check_unused_globals();
7952 global_scope = NULL;
7960 * @return the translation unit or NULL if errors occurred.
7962 translation_unit_t *parse(void)
7964 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7965 label_stack = NEW_ARR_F(stack_entry_t, 0);
7966 diagnostic_count = 0;
7970 type_set_output(stderr);
7971 ast_set_output(stderr);
7973 lookahead_bufpos = 0;
7974 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7977 translation_unit_t *unit = parse_translation_unit();
7979 DEL_ARR_F(environment_stack);
7980 DEL_ARR_F(label_stack);
7986 * Initialize the parser.
7988 void init_parser(void)
7991 /* add predefined symbols for extended-decl-modifier */
7992 sym_align = symbol_table_insert("align");
7993 sym_allocate = symbol_table_insert("allocate");
7994 sym_dllimport = symbol_table_insert("dllimport");
7995 sym_dllexport = symbol_table_insert("dllexport");
7996 sym_naked = symbol_table_insert("naked");
7997 sym_noinline = symbol_table_insert("noinline");
7998 sym_noreturn = symbol_table_insert("noreturn");
7999 sym_nothrow = symbol_table_insert("nothrow");
8000 sym_novtable = symbol_table_insert("novtable");
8001 sym_property = symbol_table_insert("property");
8002 sym_get = symbol_table_insert("get");
8003 sym_put = symbol_table_insert("put");
8004 sym_selectany = symbol_table_insert("selectany");
8005 sym_thread = symbol_table_insert("thread");
8006 sym_uuid = symbol_table_insert("uuid");
8007 sym_deprecated = symbol_table_insert("deprecated");
8008 sym_restrict = symbol_table_insert("restrict");
8009 sym_noalias = symbol_table_insert("noalias");
8011 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8013 init_expression_parsers();
8014 obstack_init(&temp_obst);
8016 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8017 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8021 * Terminate the parser.
8023 void exit_parser(void)
8025 obstack_free(&temp_obst, NULL);