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 /** if wchar_t is equal to unsigned short. */
43 bool opt_short_wchar_t =
50 //#define PRINT_TOKENS
51 #define MAX_LOOKAHEAD 2
54 declaration_t *old_declaration;
56 unsigned short namespc;
59 typedef struct argument_list_t argument_list_t;
60 struct argument_list_t {
62 argument_list_t *next;
65 typedef struct gnu_attribute_t gnu_attribute_t;
66 struct gnu_attribute_t {
67 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
68 gnu_attribute_t *next;
69 bool invalid; /**< Set if this attribute had argument errors, */
70 bool have_arguments; /**< True, if this attribute has arguments. */
74 atomic_type_kind_t akind;
75 long argument; /**< Single argument. */
76 argument_list_t *arguments; /**< List of argument expressions. */
80 typedef struct declaration_specifiers_t declaration_specifiers_t;
81 struct declaration_specifiers_t {
82 source_position_t source_position;
83 unsigned char declared_storage_class;
84 unsigned char alignment; /**< Alignment, 0 if not set. */
85 unsigned int is_inline : 1;
86 unsigned int deprecated : 1;
87 decl_modifiers_t modifiers; /**< declaration modifiers */
88 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
89 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
90 symbol_t *get_property_sym; /**< the name of the get property if set. */
91 symbol_t *put_property_sym; /**< the name of the put property if set. */
96 * An environment for parsing initializers (and compound literals).
98 typedef struct parse_initializer_env_t {
99 type_t *type; /**< the type of the initializer. In case of an
100 array type with unspecified size this gets
101 adjusted to the actual size. */
102 declaration_t *declaration; /**< the declaration that is initialized if any */
103 bool must_be_constant;
104 } parse_initializer_env_t;
106 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
108 static token_t token;
109 static token_t lookahead_buffer[MAX_LOOKAHEAD];
110 static int lookahead_bufpos;
111 static stack_entry_t *environment_stack = NULL;
112 static stack_entry_t *label_stack = NULL;
113 static scope_t *global_scope = NULL;
114 static scope_t *scope = NULL;
115 static declaration_t *last_declaration = NULL;
116 static declaration_t *current_function = NULL;
117 static switch_statement_t *current_switch = NULL;
118 static statement_t *current_loop = NULL;
119 static statement_t *current_parent = NULL;
120 static ms_try_statement_t *current_try = NULL;
121 static goto_statement_t *goto_first = NULL;
122 static goto_statement_t *goto_last = NULL;
123 static label_statement_t *label_first = NULL;
124 static label_statement_t *label_last = NULL;
125 static translation_unit_t *unit = NULL;
126 static struct obstack temp_obst;
128 #define PUSH_PARENT(stmt) \
129 statement_t *const prev_parent = current_parent; \
130 current_parent = (stmt);
131 #define POP_PARENT ((void)(current_parent = prev_parent))
133 static source_position_t null_position = { NULL, 0 };
135 /* symbols for Microsoft extended-decl-modifier */
136 static const symbol_t *sym_align = NULL;
137 static const symbol_t *sym_allocate = NULL;
138 static const symbol_t *sym_dllimport = NULL;
139 static const symbol_t *sym_dllexport = NULL;
140 static const symbol_t *sym_naked = NULL;
141 static const symbol_t *sym_noinline = NULL;
142 static const symbol_t *sym_noreturn = NULL;
143 static const symbol_t *sym_nothrow = NULL;
144 static const symbol_t *sym_novtable = NULL;
145 static const symbol_t *sym_property = NULL;
146 static const symbol_t *sym_get = NULL;
147 static const symbol_t *sym_put = NULL;
148 static const symbol_t *sym_selectany = NULL;
149 static const symbol_t *sym_thread = NULL;
150 static const symbol_t *sym_uuid = NULL;
151 static const symbol_t *sym_deprecated = NULL;
152 static const symbol_t *sym_restrict = NULL;
153 static const symbol_t *sym_noalias = NULL;
155 /** The token anchor set */
156 static unsigned char token_anchor_set[T_LAST_TOKEN];
158 /** The current source position. */
159 #define HERE (&token.source_position)
161 static type_t *type_valist;
163 static statement_t *parse_compound_statement(bool inside_expression_statement);
164 static statement_t *parse_statement(void);
166 static expression_t *parse_sub_expression(unsigned precedence);
167 static expression_t *parse_expression(void);
168 static type_t *parse_typename(void);
170 static void parse_compound_type_entries(declaration_t *compound_declaration);
171 static declaration_t *parse_declarator(
172 const declaration_specifiers_t *specifiers, bool may_be_abstract);
173 static declaration_t *record_declaration(declaration_t *declaration);
175 static void semantic_comparison(binary_expression_t *expression);
177 #define STORAGE_CLASSES \
185 #define TYPE_QUALIFIERS \
190 case T__forceinline: \
191 case T___attribute__:
193 #ifdef PROVIDE_COMPLEX
194 #define COMPLEX_SPECIFIERS \
196 #define IMAGINARY_SPECIFIERS \
199 #define COMPLEX_SPECIFIERS
200 #define IMAGINARY_SPECIFIERS
203 #define TYPE_SPECIFIERS \
218 case T___builtin_va_list: \
223 #define DECLARATION_START \
228 #define TYPENAME_START \
233 * Allocate an AST node with given size and
234 * initialize all fields with zero.
236 static void *allocate_ast_zero(size_t size)
238 void *res = allocate_ast(size);
239 memset(res, 0, size);
243 static declaration_t *allocate_declaration_zero(void)
245 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
246 declaration->type = type_error_type;
247 declaration->alignment = 0;
252 * Returns the size of a statement node.
254 * @param kind the statement kind
256 static size_t get_statement_struct_size(statement_kind_t kind)
258 static const size_t sizes[] = {
259 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
260 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
261 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
262 [STATEMENT_RETURN] = sizeof(return_statement_t),
263 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
264 [STATEMENT_IF] = sizeof(if_statement_t),
265 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
266 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
267 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
268 [STATEMENT_BREAK] = sizeof(statement_base_t),
269 [STATEMENT_GOTO] = sizeof(goto_statement_t),
270 [STATEMENT_LABEL] = sizeof(label_statement_t),
271 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
272 [STATEMENT_WHILE] = sizeof(while_statement_t),
273 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
274 [STATEMENT_FOR] = sizeof(for_statement_t),
275 [STATEMENT_ASM] = sizeof(asm_statement_t),
276 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
277 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
279 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
280 assert(sizes[kind] != 0);
285 * Returns the size of an expression node.
287 * @param kind the expression kind
289 static size_t get_expression_struct_size(expression_kind_t kind)
291 static const size_t sizes[] = {
292 [EXPR_INVALID] = sizeof(expression_base_t),
293 [EXPR_REFERENCE] = sizeof(reference_expression_t),
294 [EXPR_CONST] = sizeof(const_expression_t),
295 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
296 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
297 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
298 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
299 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
300 [EXPR_CALL] = sizeof(call_expression_t),
301 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
302 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
303 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
304 [EXPR_SELECT] = sizeof(select_expression_t),
305 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
306 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
307 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
308 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
309 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
310 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
311 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
312 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
313 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
314 [EXPR_VA_START] = sizeof(va_start_expression_t),
315 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
316 [EXPR_STATEMENT] = sizeof(statement_expression_t),
318 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
319 return sizes[EXPR_UNARY_FIRST];
321 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
322 return sizes[EXPR_BINARY_FIRST];
324 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
325 assert(sizes[kind] != 0);
330 * Allocate a statement node of given kind and initialize all
333 static statement_t *allocate_statement_zero(statement_kind_t kind)
335 size_t size = get_statement_struct_size(kind);
336 statement_t *res = allocate_ast_zero(size);
338 res->base.kind = kind;
339 res->base.parent = current_parent;
344 * Allocate an expression node of given kind and initialize all
347 static expression_t *allocate_expression_zero(expression_kind_t kind)
349 size_t size = get_expression_struct_size(kind);
350 expression_t *res = allocate_ast_zero(size);
352 res->base.kind = kind;
353 res->base.type = type_error_type;
358 * Creates a new invalid expression.
360 static expression_t *create_invalid_expression(void)
362 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
363 expression->base.source_position = token.source_position;
368 * Creates a new invalid statement.
370 static statement_t *create_invalid_statement(void)
372 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
373 statement->base.source_position = token.source_position;
378 * Allocate a new empty statement.
380 static statement_t *create_empty_statement(void)
382 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
383 statement->base.source_position = token.source_position;
388 * Returns the size of a type node.
390 * @param kind the type kind
392 static size_t get_type_struct_size(type_kind_t kind)
394 static const size_t sizes[] = {
395 [TYPE_ATOMIC] = sizeof(atomic_type_t),
396 [TYPE_COMPLEX] = sizeof(complex_type_t),
397 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
398 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
399 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
400 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
401 [TYPE_ENUM] = sizeof(enum_type_t),
402 [TYPE_FUNCTION] = sizeof(function_type_t),
403 [TYPE_POINTER] = sizeof(pointer_type_t),
404 [TYPE_ARRAY] = sizeof(array_type_t),
405 [TYPE_BUILTIN] = sizeof(builtin_type_t),
406 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
407 [TYPE_TYPEOF] = sizeof(typeof_type_t),
409 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
410 assert(kind <= TYPE_TYPEOF);
411 assert(sizes[kind] != 0);
416 * Allocate a type node of given kind and initialize all
419 * @param kind type kind to allocate
420 * @param source_position the source position of the type definition
422 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
424 size_t size = get_type_struct_size(kind);
425 type_t *res = obstack_alloc(type_obst, size);
426 memset(res, 0, size);
428 res->base.kind = kind;
429 res->base.source_position = *source_position;
434 * Returns the size of an initializer node.
436 * @param kind the initializer kind
438 static size_t get_initializer_size(initializer_kind_t kind)
440 static const size_t sizes[] = {
441 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
442 [INITIALIZER_STRING] = sizeof(initializer_string_t),
443 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
444 [INITIALIZER_LIST] = sizeof(initializer_list_t),
445 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
447 assert(kind < sizeof(sizes) / sizeof(*sizes));
448 assert(sizes[kind] != 0);
453 * Allocate an initializer node of given kind and initialize all
456 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
458 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
465 * Free a type from the type obstack.
467 static void free_type(void *type)
469 obstack_free(type_obst, type);
473 * Returns the index of the top element of the environment stack.
475 static size_t environment_top(void)
477 return ARR_LEN(environment_stack);
481 * Returns the index of the top element of the label stack.
483 static size_t label_top(void)
485 return ARR_LEN(label_stack);
489 * Return the next token.
491 static inline void next_token(void)
493 token = lookahead_buffer[lookahead_bufpos];
494 lookahead_buffer[lookahead_bufpos] = lexer_token;
497 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
500 print_token(stderr, &token);
501 fprintf(stderr, "\n");
506 * Return the next token with a given lookahead.
508 static inline const token_t *look_ahead(int num)
510 assert(num > 0 && num <= MAX_LOOKAHEAD);
511 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
512 return &lookahead_buffer[pos];
516 * Adds a token to the token anchor set (a multi-set).
518 static void add_anchor_token(int token_type)
520 assert(0 <= token_type && token_type < T_LAST_TOKEN);
521 ++token_anchor_set[token_type];
524 static int save_and_reset_anchor_state(int token_type)
526 assert(0 <= token_type && token_type < T_LAST_TOKEN);
527 int count = token_anchor_set[token_type];
528 token_anchor_set[token_type] = 0;
532 static void restore_anchor_state(int token_type, int count)
534 assert(0 <= token_type && token_type < T_LAST_TOKEN);
535 token_anchor_set[token_type] = count;
539 * Remove a token from the token anchor set (a multi-set).
541 static void rem_anchor_token(int token_type)
543 assert(0 <= token_type && token_type < T_LAST_TOKEN);
544 --token_anchor_set[token_type];
547 static bool at_anchor(void)
551 return token_anchor_set[token.type];
555 * Eat tokens until a matching token is found.
557 static void eat_until_matching_token(int type)
561 case '(': end_token = ')'; break;
562 case '{': end_token = '}'; break;
563 case '[': end_token = ']'; break;
564 default: end_token = type; break;
567 unsigned parenthesis_count = 0;
568 unsigned brace_count = 0;
569 unsigned bracket_count = 0;
570 while (token.type != end_token ||
571 parenthesis_count != 0 ||
573 bracket_count != 0) {
574 switch (token.type) {
576 case '(': ++parenthesis_count; break;
577 case '{': ++brace_count; break;
578 case '[': ++bracket_count; break;
581 if (parenthesis_count > 0)
591 if (bracket_count > 0)
594 if (token.type == end_token &&
595 parenthesis_count == 0 &&
609 * Eat input tokens until an anchor is found.
611 static void eat_until_anchor(void)
613 if (token.type == T_EOF)
615 while (token_anchor_set[token.type] == 0) {
616 if (token.type == '(' || token.type == '{' || token.type == '[')
617 eat_until_matching_token(token.type);
618 if (token.type == T_EOF)
624 static void eat_block(void)
626 eat_until_matching_token('{');
627 if (token.type == '}')
632 * eat all token until a ';' is reached or a stop token is found.
634 static void eat_statement(void)
636 eat_until_matching_token(';');
637 if (token.type == ';')
641 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while (0)
644 * Report a parse error because an expected token was not found.
647 #if defined __GNUC__ && __GNUC__ >= 4
648 __attribute__((sentinel))
650 void parse_error_expected(const char *message, ...)
652 if (message != NULL) {
653 errorf(HERE, "%s", message);
656 va_start(ap, message);
657 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
662 * Report a type error.
664 static void type_error(const char *msg, const source_position_t *source_position,
667 errorf(source_position, "%s, but found type '%T'", msg, type);
671 * Report an incompatible type.
673 static void type_error_incompatible(const char *msg,
674 const source_position_t *source_position, type_t *type1, type_t *type2)
676 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
681 * Expect the the current token is the expected token.
682 * If not, generate an error, eat the current statement,
683 * and goto the end_error label.
685 #define expect(expected) \
687 if (UNLIKELY(token.type != (expected))) { \
688 parse_error_expected(NULL, (expected), NULL); \
689 add_anchor_token(expected); \
690 eat_until_anchor(); \
691 if (token.type == expected) \
693 rem_anchor_token(expected); \
699 static void set_scope(scope_t *new_scope)
702 scope->last_declaration = last_declaration;
706 last_declaration = new_scope->last_declaration;
710 * Search a symbol in a given namespace and returns its declaration or
711 * NULL if this symbol was not found.
713 static declaration_t *get_declaration(const symbol_t *const symbol,
714 const namespace_t namespc)
716 declaration_t *declaration = symbol->declaration;
717 for( ; declaration != NULL; declaration = declaration->symbol_next) {
718 if (declaration->namespc == namespc)
726 * pushs an environment_entry on the environment stack and links the
727 * corresponding symbol to the new entry
729 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
731 symbol_t *symbol = declaration->symbol;
732 namespace_t namespc = (namespace_t) declaration->namespc;
734 /* replace/add declaration into declaration list of the symbol */
735 declaration_t *iter = symbol->declaration;
737 symbol->declaration = declaration;
739 declaration_t *iter_last = NULL;
740 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
741 /* replace an entry? */
742 if (iter->namespc == namespc) {
743 if (iter_last == NULL) {
744 symbol->declaration = declaration;
746 iter_last->symbol_next = declaration;
748 declaration->symbol_next = iter->symbol_next;
753 assert(iter_last->symbol_next == NULL);
754 iter_last->symbol_next = declaration;
758 /* remember old declaration */
760 entry.symbol = symbol;
761 entry.old_declaration = iter;
762 entry.namespc = (unsigned short) namespc;
763 ARR_APP1(stack_entry_t, *stack_ptr, entry);
766 static void environment_push(declaration_t *declaration)
768 assert(declaration->source_position.input_name != NULL);
769 assert(declaration->parent_scope != NULL);
770 stack_push(&environment_stack, declaration);
774 * Push a declaration of the label stack.
776 * @param declaration the declaration
778 static void label_push(declaration_t *declaration)
780 declaration->parent_scope = ¤t_function->scope;
781 stack_push(&label_stack, declaration);
785 * pops symbols from the environment stack until @p new_top is the top element
787 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
789 stack_entry_t *stack = *stack_ptr;
790 size_t top = ARR_LEN(stack);
793 assert(new_top <= top);
797 for(i = top; i > new_top; --i) {
798 stack_entry_t *entry = &stack[i - 1];
800 declaration_t *old_declaration = entry->old_declaration;
801 symbol_t *symbol = entry->symbol;
802 namespace_t namespc = (namespace_t)entry->namespc;
804 /* replace/remove declaration */
805 declaration_t *declaration = symbol->declaration;
806 assert(declaration != NULL);
807 if (declaration->namespc == namespc) {
808 if (old_declaration == NULL) {
809 symbol->declaration = declaration->symbol_next;
811 symbol->declaration = old_declaration;
814 declaration_t *iter_last = declaration;
815 declaration_t *iter = declaration->symbol_next;
816 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
817 /* replace an entry? */
818 if (iter->namespc == namespc) {
819 assert(iter_last != NULL);
820 iter_last->symbol_next = old_declaration;
821 if (old_declaration != NULL) {
822 old_declaration->symbol_next = iter->symbol_next;
827 assert(iter != NULL);
831 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
834 static void environment_pop_to(size_t new_top)
836 stack_pop_to(&environment_stack, new_top);
840 * Pop all entries on the label stack until the new_top
843 * @param new_top the new stack top
845 static void label_pop_to(size_t new_top)
847 stack_pop_to(&label_stack, new_top);
850 static int get_akind_rank(atomic_type_kind_t akind)
855 static int get_rank(const type_t *type)
857 assert(!is_typeref(type));
858 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
859 * and esp. footnote 108). However we can't fold constants (yet), so we
860 * can't decide whether unsigned int is possible, while int always works.
861 * (unsigned int would be preferable when possible... for stuff like
862 * struct { enum { ... } bla : 4; } ) */
863 if (type->kind == TYPE_ENUM)
864 return get_akind_rank(ATOMIC_TYPE_INT);
866 assert(type->kind == TYPE_ATOMIC);
867 return get_akind_rank(type->atomic.akind);
870 static type_t *promote_integer(type_t *type)
872 if (type->kind == TYPE_BITFIELD)
873 type = type->bitfield.base_type;
875 if (get_rank(type) < get_akind_rank(ATOMIC_TYPE_INT))
882 * Create a cast expression.
884 * @param expression the expression to cast
885 * @param dest_type the destination type
887 static expression_t *create_cast_expression(expression_t *expression,
890 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
892 cast->unary.value = expression;
893 cast->base.type = dest_type;
899 * Check if a given expression represents the 0 pointer constant.
901 static bool is_null_pointer_constant(const expression_t *expression)
903 /* skip void* cast */
904 if (expression->kind == EXPR_UNARY_CAST
905 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
906 expression = expression->unary.value;
909 /* TODO: not correct yet, should be any constant integer expression
910 * which evaluates to 0 */
911 if (expression->kind != EXPR_CONST)
914 type_t *const type = skip_typeref(expression->base.type);
915 if (!is_type_integer(type))
918 return expression->conste.v.int_value == 0;
922 * Create an implicit cast expression.
924 * @param expression the expression to cast
925 * @param dest_type the destination type
927 static expression_t *create_implicit_cast(expression_t *expression,
930 type_t *const source_type = expression->base.type;
932 if (source_type == dest_type)
935 return create_cast_expression(expression, dest_type);
938 typedef enum assign_error_t {
940 ASSIGN_ERROR_INCOMPATIBLE,
941 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
942 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
943 ASSIGN_WARNING_POINTER_FROM_INT,
944 ASSIGN_WARNING_INT_FROM_POINTER
947 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
948 const expression_t *const right,
950 const source_position_t *source_position)
952 type_t *const orig_type_right = right->base.type;
953 type_t *const type_left = skip_typeref(orig_type_left);
954 type_t *const type_right = skip_typeref(orig_type_right);
959 case ASSIGN_ERROR_INCOMPATIBLE:
960 errorf(source_position,
961 "destination type '%T' in %s is incompatible with type '%T'",
962 orig_type_left, context, orig_type_right);
965 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
966 type_t *points_to_left
967 = skip_typeref(type_left->pointer.points_to);
968 type_t *points_to_right
969 = skip_typeref(type_right->pointer.points_to);
971 /* the left type has all qualifiers from the right type */
972 unsigned missing_qualifiers
973 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
974 warningf(source_position,
975 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointer target type",
976 orig_type_left, context, orig_type_right, missing_qualifiers);
980 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
981 warningf(source_position,
982 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
983 orig_type_left, context, right, orig_type_right);
986 case ASSIGN_WARNING_POINTER_FROM_INT:
987 warningf(source_position,
988 "%s makes integer '%T' from pointer '%T' without a cast",
989 context, orig_type_left, orig_type_right);
992 case ASSIGN_WARNING_INT_FROM_POINTER:
993 warningf(source_position,
994 "%s makes integer '%T' from pointer '%T' without a cast",
995 context, orig_type_left, orig_type_right);
999 panic("invalid error value");
1003 /** Implements the rules from § 6.5.16.1 */
1004 static assign_error_t semantic_assign(type_t *orig_type_left,
1005 const expression_t *const right)
1007 type_t *const orig_type_right = right->base.type;
1008 type_t *const type_left = skip_typeref(orig_type_left);
1009 type_t *const type_right = skip_typeref(orig_type_right);
1011 if (is_type_pointer(type_left)) {
1012 if (is_null_pointer_constant(right)) {
1013 return ASSIGN_SUCCESS;
1014 } else if (is_type_pointer(type_right)) {
1015 type_t *points_to_left
1016 = skip_typeref(type_left->pointer.points_to);
1017 type_t *points_to_right
1018 = skip_typeref(type_right->pointer.points_to);
1019 assign_error_t res = ASSIGN_SUCCESS;
1021 /* the left type has all qualifiers from the right type */
1022 unsigned missing_qualifiers
1023 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
1024 if (missing_qualifiers != 0) {
1025 res = ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
1028 points_to_left = get_unqualified_type(points_to_left);
1029 points_to_right = get_unqualified_type(points_to_right);
1031 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
1032 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
1036 if (!types_compatible(points_to_left, points_to_right)) {
1037 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1041 } else if (is_type_integer(type_right)) {
1042 return ASSIGN_WARNING_POINTER_FROM_INT;
1044 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1045 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1046 && is_type_pointer(type_right))) {
1047 return ASSIGN_SUCCESS;
1048 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1049 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1050 type_t *const unqual_type_left = get_unqualified_type(type_left);
1051 type_t *const unqual_type_right = get_unqualified_type(type_right);
1052 if (types_compatible(unqual_type_left, unqual_type_right)) {
1053 return ASSIGN_SUCCESS;
1055 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1056 return ASSIGN_WARNING_INT_FROM_POINTER;
1059 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1060 return ASSIGN_SUCCESS;
1062 return ASSIGN_ERROR_INCOMPATIBLE;
1065 static expression_t *parse_constant_expression(void)
1067 /* start parsing at precedence 7 (conditional expression) */
1068 expression_t *result = parse_sub_expression(7);
1070 if (!is_constant_expression(result)) {
1071 errorf(&result->base.source_position,
1072 "expression '%E' is not constant\n", result);
1078 static expression_t *parse_assignment_expression(void)
1080 /* start parsing at precedence 2 (assignment expression) */
1081 return parse_sub_expression(2);
1084 static type_t *make_global_typedef(const char *name, type_t *type)
1086 symbol_t *const symbol = symbol_table_insert(name);
1088 declaration_t *const declaration = allocate_declaration_zero();
1089 declaration->namespc = NAMESPACE_NORMAL;
1090 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1091 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1092 declaration->type = type;
1093 declaration->symbol = symbol;
1094 declaration->source_position = builtin_source_position;
1096 record_declaration(declaration);
1098 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1099 typedef_type->typedeft.declaration = declaration;
1101 return typedef_type;
1104 static string_t parse_string_literals(void)
1106 assert(token.type == T_STRING_LITERAL);
1107 string_t result = token.v.string;
1111 while (token.type == T_STRING_LITERAL) {
1112 result = concat_strings(&result, &token.v.string);
1119 static const char *const gnu_attribute_names[GNU_AK_LAST] = {
1120 [GNU_AK_CONST] = "const",
1121 [GNU_AK_VOLATILE] = "volatile",
1122 [GNU_AK_CDECL] = "cdecl",
1123 [GNU_AK_STDCALL] = "stdcall",
1124 [GNU_AK_FASTCALL] = "fastcall",
1125 [GNU_AK_DEPRECATED] = "deprecated",
1126 [GNU_AK_NOINLINE] = "noinline",
1127 [GNU_AK_NORETURN] = "noreturn",
1128 [GNU_AK_NAKED] = "naked",
1129 [GNU_AK_PURE] = "pure",
1130 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1131 [GNU_AK_MALLOC] = "malloc",
1132 [GNU_AK_WEAK] = "weak",
1133 [GNU_AK_CONSTRUCTOR] = "constructor",
1134 [GNU_AK_DESTRUCTOR] = "destructor",
1135 [GNU_AK_NOTHROW] = "nothrow",
1136 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1137 [GNU_AK_COMMON] = "common",
1138 [GNU_AK_NOCOMMON] = "nocommon",
1139 [GNU_AK_PACKED] = "packed",
1140 [GNU_AK_SHARED] = "shared",
1141 [GNU_AK_NOTSHARED] = "notshared",
1142 [GNU_AK_USED] = "used",
1143 [GNU_AK_UNUSED] = "unused",
1144 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1145 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1146 [GNU_AK_LONGCALL] = "longcall",
1147 [GNU_AK_SHORTCALL] = "shortcall",
1148 [GNU_AK_LONG_CALL] = "long_call",
1149 [GNU_AK_SHORT_CALL] = "short_call",
1150 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1151 [GNU_AK_INTERRUPT] = "interrupt",
1152 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1153 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1154 [GNU_AK_NESTING] = "nesting",
1155 [GNU_AK_NEAR] = "near",
1156 [GNU_AK_FAR] = "far",
1157 [GNU_AK_SIGNAL] = "signal",
1158 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1159 [GNU_AK_TINY_DATA] = "tiny_data",
1160 [GNU_AK_SAVEALL] = "saveall",
1161 [GNU_AK_FLATTEN] = "flatten",
1162 [GNU_AK_SSEREGPARM] = "sseregparm",
1163 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1164 [GNU_AK_RETURN_TWICE] = "return_twice",
1165 [GNU_AK_MAY_ALIAS] = "may_alias",
1166 [GNU_AK_MS_STRUCT] = "ms_struct",
1167 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1168 [GNU_AK_DLLIMPORT] = "dllimport",
1169 [GNU_AK_DLLEXPORT] = "dllexport",
1170 [GNU_AK_ALIGNED] = "aligned",
1171 [GNU_AK_ALIAS] = "alias",
1172 [GNU_AK_SECTION] = "section",
1173 [GNU_AK_FORMAT] = "format",
1174 [GNU_AK_FORMAT_ARG] = "format_arg",
1175 [GNU_AK_WEAKREF] = "weakref",
1176 [GNU_AK_NONNULL] = "nonnull",
1177 [GNU_AK_TLS_MODEL] = "tls_model",
1178 [GNU_AK_VISIBILITY] = "visibility",
1179 [GNU_AK_REGPARM] = "regparm",
1180 [GNU_AK_MODE] = "mode",
1181 [GNU_AK_MODEL] = "model",
1182 [GNU_AK_TRAP_EXIT] = "trap_exit",
1183 [GNU_AK_SP_SWITCH] = "sp_switch",
1184 [GNU_AK_SENTINEL] = "sentinel"
1188 * compare two string, ignoring double underscores on the second.
1190 static int strcmp_underscore(const char *s1, const char *s2)
1192 if (s2[0] == '_' && s2[1] == '_') {
1193 size_t len2 = strlen(s2);
1194 size_t len1 = strlen(s1);
1195 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1196 return strncmp(s1, s2+2, len2-4);
1200 return strcmp(s1, s2);
1204 * Allocate a new gnu temporal attribute.
1206 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind)
1208 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1209 attribute->kind = kind;
1210 attribute->next = NULL;
1211 attribute->invalid = false;
1212 attribute->have_arguments = false;
1218 * parse one constant expression argument.
1220 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute)
1222 expression_t *expression;
1223 add_anchor_token(')');
1224 expression = parse_constant_expression();
1225 rem_anchor_token(')');
1227 attribute->u.argument = fold_constant(expression);
1230 attribute->invalid = true;
1234 * parse a list of constant expressions arguments.
1236 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute)
1238 argument_list_t **list = &attribute->u.arguments;
1239 argument_list_t *entry;
1240 expression_t *expression;
1241 add_anchor_token(')');
1242 add_anchor_token(',');
1244 expression = parse_constant_expression();
1245 entry = obstack_alloc(&temp_obst, sizeof(entry));
1246 entry->argument = fold_constant(expression);
1249 list = &entry->next;
1250 if (token.type != ',')
1254 rem_anchor_token(',');
1255 rem_anchor_token(')');
1259 attribute->invalid = true;
1263 * parse one string literal argument.
1265 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1268 add_anchor_token('(');
1269 if (token.type != T_STRING_LITERAL) {
1270 parse_error_expected("while parsing attribute directive",
1271 T_STRING_LITERAL, NULL);
1274 *string = parse_string_literals();
1275 rem_anchor_token('(');
1279 attribute->invalid = true;
1283 * parse one tls model.
1285 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute)
1287 static const char *const tls_models[] = {
1293 string_t string = { NULL, 0 };
1294 parse_gnu_attribute_string_arg(attribute, &string);
1295 if (string.begin != NULL) {
1296 for(size_t i = 0; i < 4; ++i) {
1297 if (strcmp(tls_models[i], string.begin) == 0) {
1298 attribute->u.value = i;
1302 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1304 attribute->invalid = true;
1308 * parse one tls model.
1310 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute)
1312 static const char *const visibilities[] = {
1318 string_t string = { NULL, 0 };
1319 parse_gnu_attribute_string_arg(attribute, &string);
1320 if (string.begin != NULL) {
1321 for(size_t i = 0; i < 4; ++i) {
1322 if (strcmp(visibilities[i], string.begin) == 0) {
1323 attribute->u.value = i;
1327 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1329 attribute->invalid = true;
1333 * parse one (code) model.
1335 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute)
1337 static const char *const visibilities[] = {
1342 string_t string = { NULL, 0 };
1343 parse_gnu_attribute_string_arg(attribute, &string);
1344 if (string.begin != NULL) {
1345 for(int i = 0; i < 3; ++i) {
1346 if (strcmp(visibilities[i], string.begin) == 0) {
1347 attribute->u.value = i;
1351 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1353 attribute->invalid = true;
1356 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1358 /* TODO: find out what is allowed here... */
1360 /* at least: byte, word, pointer, list of machine modes
1361 * __XXX___ is interpreted as XXX */
1362 add_anchor_token(')');
1364 if (token.type != T_IDENTIFIER) {
1365 expect(T_IDENTIFIER);
1368 /* This isn't really correct, the backend should provide a list of machine
1369 * specific modes (according to gcc philosophy that is...) */
1370 const char *symbol_str = token.v.symbol->string;
1371 if (strcmp_underscore("QI", symbol_str) == 0 ||
1372 strcmp_underscore("byte", symbol_str) == 0) {
1373 attribute->u.akind = ATOMIC_TYPE_CHAR;
1374 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1375 attribute->u.akind = ATOMIC_TYPE_SHORT;
1376 } else if (strcmp_underscore("SI", symbol_str) == 0
1377 || strcmp_underscore("word", symbol_str) == 0
1378 || strcmp_underscore("pointer", symbol_str) == 0) {
1379 attribute->u.akind = ATOMIC_TYPE_INT;
1380 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1381 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1383 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1384 attribute->invalid = true;
1388 rem_anchor_token(')');
1392 attribute->invalid = true;
1396 * parse one interrupt argument.
1398 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute)
1400 static const char *const interrupts[] = {
1407 string_t string = { NULL, 0 };
1408 parse_gnu_attribute_string_arg(attribute, &string);
1409 if (string.begin != NULL) {
1410 for(size_t i = 0; i < 5; ++i) {
1411 if (strcmp(interrupts[i], string.begin) == 0) {
1412 attribute->u.value = i;
1416 errorf(HERE, "'%s' is not an interrupt", string.begin);
1418 attribute->invalid = true;
1422 * parse ( identifier, const expression, const expression )
1424 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute)
1426 static const char *const format_names[] = {
1434 if (token.type != T_IDENTIFIER) {
1435 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1438 const char *name = token.v.symbol->string;
1439 for(i = 0; i < 4; ++i) {
1440 if (strcmp_underscore(format_names[i], name) == 0)
1444 if (warning.attribute)
1445 warningf(HERE, "'%s' is an unrecognized format function type", name);
1450 add_anchor_token(')');
1451 add_anchor_token(',');
1452 parse_constant_expression();
1453 rem_anchor_token(',');
1454 rem_anchor_token('(');
1457 add_anchor_token(')');
1458 parse_constant_expression();
1459 rem_anchor_token('(');
1463 attribute->u.value = true;
1466 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1468 if (!attribute->have_arguments)
1471 /* should have no arguments */
1472 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1473 eat_until_matching_token('(');
1474 /* we have already consumed '(', so we stop before ')', eat it */
1476 attribute->invalid = true;
1480 * Parse one GNU attribute.
1482 * Note that attribute names can be specified WITH or WITHOUT
1483 * double underscores, ie const or __const__.
1485 * The following attributes are parsed without arguments
1510 * no_instrument_function
1511 * warn_unused_result
1528 * externally_visible
1536 * The following attributes are parsed with arguments
1537 * aligned( const expression )
1538 * alias( string literal )
1539 * section( string literal )
1540 * format( identifier, const expression, const expression )
1541 * format_arg( const expression )
1542 * tls_model( string literal )
1543 * visibility( string literal )
1544 * regparm( const expression )
1545 * model( string leteral )
1546 * trap_exit( const expression )
1547 * sp_switch( string literal )
1549 * The following attributes might have arguments
1550 * weak_ref( string literal )
1551 * non_null( const expression // ',' )
1552 * interrupt( string literal )
1553 * sentinel( constant expression )
1555 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1557 gnu_attribute_t *head = *attributes;
1558 gnu_attribute_t *last = *attributes;
1559 decl_modifiers_t modifiers = 0;
1560 gnu_attribute_t *attribute;
1562 eat(T___attribute__);
1566 if (token.type != ')') {
1567 /* find the end of the list */
1569 while (last->next != NULL)
1573 /* non-empty attribute list */
1576 if (token.type == T_const) {
1578 } else if (token.type == T_volatile) {
1580 } else if (token.type == T_cdecl) {
1581 /* __attribute__((cdecl)), WITH ms mode */
1583 } else if (token.type == T_IDENTIFIER) {
1584 const symbol_t *sym = token.v.symbol;
1587 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1594 for(i = 0; i < GNU_AK_LAST; ++i) {
1595 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1598 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1601 if (kind == GNU_AK_LAST) {
1602 if (warning.attribute)
1603 warningf(HERE, "'%s' attribute directive ignored", name);
1605 /* skip possible arguments */
1606 if (token.type == '(') {
1607 eat_until_matching_token(')');
1610 /* check for arguments */
1611 attribute = allocate_gnu_attribute(kind);
1612 if (token.type == '(') {
1614 if (token.type == ')') {
1615 /* empty args are allowed */
1618 attribute->have_arguments = true;
1623 case GNU_AK_VOLATILE:
1628 case GNU_AK_NOCOMMON:
1630 case GNU_AK_NOTSHARED:
1631 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1632 case GNU_AK_WARN_UNUSED_RESULT:
1633 case GNU_AK_LONGCALL:
1634 case GNU_AK_SHORTCALL:
1635 case GNU_AK_LONG_CALL:
1636 case GNU_AK_SHORT_CALL:
1637 case GNU_AK_FUNCTION_VECTOR:
1638 case GNU_AK_INTERRUPT_HANDLER:
1639 case GNU_AK_NMI_HANDLER:
1640 case GNU_AK_NESTING:
1644 case GNU_AK_EIGTHBIT_DATA:
1645 case GNU_AK_TINY_DATA:
1646 case GNU_AK_SAVEALL:
1647 case GNU_AK_FLATTEN:
1648 case GNU_AK_SSEREGPARM:
1649 case GNU_AK_EXTERNALLY_VISIBLE:
1650 case GNU_AK_RETURN_TWICE:
1651 case GNU_AK_MAY_ALIAS:
1652 case GNU_AK_MS_STRUCT:
1653 case GNU_AK_GCC_STRUCT:
1656 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1657 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1658 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1659 case GNU_AK_UNUSED: modifiers |= DM_UNUSED; goto no_arg;
1660 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1661 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1662 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1663 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1664 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1665 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1666 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1667 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1668 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1669 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1670 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1671 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1672 case GNU_AK_DEPRECATED: modifiers |= DM_DEPRECATED; goto no_arg;
1674 case GNU_AK_ALIGNED:
1675 /* __align__ may be used without an argument */
1676 if (attribute->have_arguments) {
1677 parse_gnu_attribute_const_arg(attribute);
1681 case GNU_AK_FORMAT_ARG:
1682 case GNU_AK_REGPARM:
1683 case GNU_AK_TRAP_EXIT:
1684 if (!attribute->have_arguments) {
1685 /* should have arguments */
1686 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1687 attribute->invalid = true;
1689 parse_gnu_attribute_const_arg(attribute);
1692 case GNU_AK_SECTION:
1693 case GNU_AK_SP_SWITCH:
1694 if (!attribute->have_arguments) {
1695 /* should have arguments */
1696 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1697 attribute->invalid = true;
1699 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1702 if (!attribute->have_arguments) {
1703 /* should have arguments */
1704 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1705 attribute->invalid = true;
1707 parse_gnu_attribute_format_args(attribute);
1709 case GNU_AK_WEAKREF:
1710 /* may have one string argument */
1711 if (attribute->have_arguments)
1712 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1714 case GNU_AK_NONNULL:
1715 if (attribute->have_arguments)
1716 parse_gnu_attribute_const_arg_list(attribute);
1718 case GNU_AK_TLS_MODEL:
1719 if (!attribute->have_arguments) {
1720 /* should have arguments */
1721 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1723 parse_gnu_attribute_tls_model_arg(attribute);
1725 case GNU_AK_VISIBILITY:
1726 if (!attribute->have_arguments) {
1727 /* should have arguments */
1728 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1730 parse_gnu_attribute_visibility_arg(attribute);
1733 if (!attribute->have_arguments) {
1734 /* should have arguments */
1735 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1737 parse_gnu_attribute_model_arg(attribute);
1741 if (!attribute->have_arguments) {
1742 /* should have arguments */
1743 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1745 parse_gnu_attribute_mode_arg(attribute);
1748 case GNU_AK_INTERRUPT:
1749 /* may have one string argument */
1750 if (attribute->have_arguments)
1751 parse_gnu_attribute_interrupt_arg(attribute);
1753 case GNU_AK_SENTINEL:
1754 /* may have one string argument */
1755 if (attribute->have_arguments)
1756 parse_gnu_attribute_const_arg(attribute);
1759 /* already handled */
1763 check_no_argument(attribute, name);
1766 if (attribute != NULL) {
1768 last->next = attribute;
1771 head = last = attribute;
1775 if (token.type != ',')
1789 * Parse GNU attributes.
1791 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1793 decl_modifiers_t modifiers = 0;
1796 switch(token.type) {
1797 case T___attribute__:
1798 modifiers |= parse_gnu_attribute(attributes);
1804 if (token.type != T_STRING_LITERAL) {
1805 parse_error_expected("while parsing assembler attribute",
1806 T_STRING_LITERAL, NULL);
1807 eat_until_matching_token('(');
1810 string_t asm_string = parse_string_literals();
1815 case T_cdecl: modifiers |= DM_CDECL; break;
1816 case T__fastcall: modifiers |= DM_FASTCALL; break;
1817 case T__stdcall: modifiers |= DM_STDCALL; break;
1820 /* TODO record modifier */
1821 warningf(HERE, "Ignoring declaration modifier %K", &token);
1825 default: return modifiers;
1832 static designator_t *parse_designation(void)
1834 designator_t *result = NULL;
1835 designator_t *last = NULL;
1838 designator_t *designator;
1839 switch(token.type) {
1841 designator = allocate_ast_zero(sizeof(designator[0]));
1842 designator->source_position = token.source_position;
1844 add_anchor_token(']');
1845 designator->array_index = parse_constant_expression();
1846 rem_anchor_token(']');
1850 designator = allocate_ast_zero(sizeof(designator[0]));
1851 designator->source_position = token.source_position;
1853 if (token.type != T_IDENTIFIER) {
1854 parse_error_expected("while parsing designator",
1855 T_IDENTIFIER, NULL);
1858 designator->symbol = token.v.symbol;
1866 assert(designator != NULL);
1868 last->next = designator;
1870 result = designator;
1878 static initializer_t *initializer_from_string(array_type_t *type,
1879 const string_t *const string)
1881 /* TODO: check len vs. size of array type */
1884 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1885 initializer->string.string = *string;
1890 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1891 wide_string_t *const string)
1893 /* TODO: check len vs. size of array type */
1896 initializer_t *const initializer =
1897 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1898 initializer->wide_string.string = *string;
1904 * Build an initializer from a given expression.
1906 static initializer_t *initializer_from_expression(type_t *orig_type,
1907 expression_t *expression)
1909 /* TODO check that expression is a constant expression */
1911 /* § 6.7.8.14/15 char array may be initialized by string literals */
1912 type_t *type = skip_typeref(orig_type);
1913 type_t *expr_type_orig = expression->base.type;
1914 type_t *expr_type = skip_typeref(expr_type_orig);
1915 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1916 array_type_t *const array_type = &type->array;
1917 type_t *const element_type = skip_typeref(array_type->element_type);
1919 if (element_type->kind == TYPE_ATOMIC) {
1920 atomic_type_kind_t akind = element_type->atomic.akind;
1921 switch (expression->kind) {
1922 case EXPR_STRING_LITERAL:
1923 if (akind == ATOMIC_TYPE_CHAR
1924 || akind == ATOMIC_TYPE_SCHAR
1925 || akind == ATOMIC_TYPE_UCHAR) {
1926 return initializer_from_string(array_type,
1927 &expression->string.value);
1930 case EXPR_WIDE_STRING_LITERAL: {
1931 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1932 if (get_unqualified_type(element_type) == bare_wchar_type) {
1933 return initializer_from_wide_string(array_type,
1934 &expression->wide_string.value);
1944 assign_error_t error = semantic_assign(type, expression);
1945 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1947 report_assign_error(error, type, expression, "initializer",
1948 &expression->base.source_position);
1950 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1951 result->value.value = create_implicit_cast(expression, type);
1957 * Checks if a given expression can be used as an constant initializer.
1959 static bool is_initializer_constant(const expression_t *expression)
1961 return is_constant_expression(expression)
1962 || is_address_constant(expression);
1966 * Parses an scalar initializer.
1968 * § 6.7.8.11; eat {} without warning
1970 static initializer_t *parse_scalar_initializer(type_t *type,
1971 bool must_be_constant)
1973 /* there might be extra {} hierarchies */
1975 if (token.type == '{') {
1976 warningf(HERE, "extra curly braces around scalar initializer");
1980 } while (token.type == '{');
1983 expression_t *expression = parse_assignment_expression();
1984 if (must_be_constant && !is_initializer_constant(expression)) {
1985 errorf(&expression->base.source_position,
1986 "Initialisation expression '%E' is not constant\n",
1990 initializer_t *initializer = initializer_from_expression(type, expression);
1992 if (initializer == NULL) {
1993 errorf(&expression->base.source_position,
1994 "expression '%E' (type '%T') doesn't match expected type '%T'",
1995 expression, expression->base.type, type);
2000 bool additional_warning_displayed = false;
2001 while (braces > 0) {
2002 if (token.type == ',') {
2005 if (token.type != '}') {
2006 if (!additional_warning_displayed) {
2007 warningf(HERE, "additional elements in scalar initializer");
2008 additional_warning_displayed = true;
2019 * An entry in the type path.
2021 typedef struct type_path_entry_t type_path_entry_t;
2022 struct type_path_entry_t {
2023 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
2025 size_t index; /**< For array types: the current index. */
2026 declaration_t *compound_entry; /**< For compound types: the current declaration. */
2031 * A type path expression a position inside compound or array types.
2033 typedef struct type_path_t type_path_t;
2034 struct type_path_t {
2035 type_path_entry_t *path; /**< An flexible array containing the current path. */
2036 type_t *top_type; /**< type of the element the path points */
2037 size_t max_index; /**< largest index in outermost array */
2041 * Prints a type path for debugging.
2043 static __attribute__((unused)) void debug_print_type_path(
2044 const type_path_t *path)
2046 size_t len = ARR_LEN(path->path);
2048 for(size_t i = 0; i < len; ++i) {
2049 const type_path_entry_t *entry = & path->path[i];
2051 type_t *type = skip_typeref(entry->type);
2052 if (is_type_compound(type)) {
2053 /* in gcc mode structs can have no members */
2054 if (entry->v.compound_entry == NULL) {
2058 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2059 } else if (is_type_array(type)) {
2060 fprintf(stderr, "[%zu]", entry->v.index);
2062 fprintf(stderr, "-INVALID-");
2065 if (path->top_type != NULL) {
2066 fprintf(stderr, " (");
2067 print_type(path->top_type);
2068 fprintf(stderr, ")");
2073 * Return the top type path entry, ie. in a path
2074 * (type).a.b returns the b.
2076 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2078 size_t len = ARR_LEN(path->path);
2080 return &path->path[len-1];
2084 * Enlarge the type path by an (empty) element.
2086 static type_path_entry_t *append_to_type_path(type_path_t *path)
2088 size_t len = ARR_LEN(path->path);
2089 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2091 type_path_entry_t *result = & path->path[len];
2092 memset(result, 0, sizeof(result[0]));
2097 * Descending into a sub-type. Enter the scope of the current
2100 static void descend_into_subtype(type_path_t *path)
2102 type_t *orig_top_type = path->top_type;
2103 type_t *top_type = skip_typeref(orig_top_type);
2105 assert(is_type_compound(top_type) || is_type_array(top_type));
2107 type_path_entry_t *top = append_to_type_path(path);
2108 top->type = top_type;
2110 if (is_type_compound(top_type)) {
2111 declaration_t *declaration = top_type->compound.declaration;
2112 declaration_t *entry = declaration->scope.declarations;
2113 top->v.compound_entry = entry;
2115 if (entry != NULL) {
2116 path->top_type = entry->type;
2118 path->top_type = NULL;
2121 assert(is_type_array(top_type));
2124 path->top_type = top_type->array.element_type;
2129 * Pop an entry from the given type path, ie. returning from
2130 * (type).a.b to (type).a
2132 static void ascend_from_subtype(type_path_t *path)
2134 type_path_entry_t *top = get_type_path_top(path);
2136 path->top_type = top->type;
2138 size_t len = ARR_LEN(path->path);
2139 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2143 * Pop entries from the given type path until the given
2144 * path level is reached.
2146 static void ascend_to(type_path_t *path, size_t top_path_level)
2148 size_t len = ARR_LEN(path->path);
2150 while (len > top_path_level) {
2151 ascend_from_subtype(path);
2152 len = ARR_LEN(path->path);
2156 static bool walk_designator(type_path_t *path, const designator_t *designator,
2157 bool used_in_offsetof)
2159 for( ; designator != NULL; designator = designator->next) {
2160 type_path_entry_t *top = get_type_path_top(path);
2161 type_t *orig_type = top->type;
2163 type_t *type = skip_typeref(orig_type);
2165 if (designator->symbol != NULL) {
2166 symbol_t *symbol = designator->symbol;
2167 if (!is_type_compound(type)) {
2168 if (is_type_valid(type)) {
2169 errorf(&designator->source_position,
2170 "'.%Y' designator used for non-compound type '%T'",
2176 declaration_t *declaration = type->compound.declaration;
2177 declaration_t *iter = declaration->scope.declarations;
2178 for( ; iter != NULL; iter = iter->next) {
2179 if (iter->symbol == symbol) {
2184 errorf(&designator->source_position,
2185 "'%T' has no member named '%Y'", orig_type, symbol);
2188 if (used_in_offsetof) {
2189 type_t *real_type = skip_typeref(iter->type);
2190 if (real_type->kind == TYPE_BITFIELD) {
2191 errorf(&designator->source_position,
2192 "offsetof designator '%Y' may not specify bitfield",
2198 top->type = orig_type;
2199 top->v.compound_entry = iter;
2200 orig_type = iter->type;
2202 expression_t *array_index = designator->array_index;
2203 assert(designator->array_index != NULL);
2205 if (!is_type_array(type)) {
2206 if (is_type_valid(type)) {
2207 errorf(&designator->source_position,
2208 "[%E] designator used for non-array type '%T'",
2209 array_index, orig_type);
2213 if (!is_type_valid(array_index->base.type)) {
2217 long index = fold_constant(array_index);
2218 if (!used_in_offsetof) {
2220 errorf(&designator->source_position,
2221 "array index [%E] must be positive", array_index);
2224 if (type->array.size_constant == true) {
2225 long array_size = type->array.size;
2226 if (index >= array_size) {
2227 errorf(&designator->source_position,
2228 "designator [%E] (%d) exceeds array size %d",
2229 array_index, index, array_size);
2235 top->type = orig_type;
2236 top->v.index = (size_t) index;
2237 orig_type = type->array.element_type;
2239 path->top_type = orig_type;
2241 if (designator->next != NULL) {
2242 descend_into_subtype(path);
2251 static void advance_current_object(type_path_t *path, size_t top_path_level)
2253 type_path_entry_t *top = get_type_path_top(path);
2255 type_t *type = skip_typeref(top->type);
2256 if (is_type_union(type)) {
2257 /* in unions only the first element is initialized */
2258 top->v.compound_entry = NULL;
2259 } else if (is_type_struct(type)) {
2260 declaration_t *entry = top->v.compound_entry;
2262 entry = entry->next;
2263 top->v.compound_entry = entry;
2264 if (entry != NULL) {
2265 path->top_type = entry->type;
2269 assert(is_type_array(type));
2273 if (!type->array.size_constant || top->v.index < type->array.size) {
2278 /* we're past the last member of the current sub-aggregate, try if we
2279 * can ascend in the type hierarchy and continue with another subobject */
2280 size_t len = ARR_LEN(path->path);
2282 if (len > top_path_level) {
2283 ascend_from_subtype(path);
2284 advance_current_object(path, top_path_level);
2286 path->top_type = NULL;
2291 * skip until token is found.
2293 static void skip_until(int type)
2295 while (token.type != type) {
2296 if (token.type == T_EOF)
2303 * skip any {...} blocks until a closing bracket is reached.
2305 static void skip_initializers(void)
2307 if (token.type == '{')
2310 while (token.type != '}') {
2311 if (token.type == T_EOF)
2313 if (token.type == '{') {
2321 static initializer_t *create_empty_initializer(void)
2323 static initializer_t empty_initializer
2324 = { .list = { { INITIALIZER_LIST }, 0 } };
2325 return &empty_initializer;
2329 * Parse a part of an initialiser for a struct or union,
2331 static initializer_t *parse_sub_initializer(type_path_t *path,
2332 type_t *outer_type, size_t top_path_level,
2333 parse_initializer_env_t *env)
2335 if (token.type == '}') {
2336 /* empty initializer */
2337 return create_empty_initializer();
2340 type_t *orig_type = path->top_type;
2341 type_t *type = NULL;
2343 if (orig_type == NULL) {
2344 /* We are initializing an empty compound. */
2346 type = skip_typeref(orig_type);
2348 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2349 * initializers in this case. */
2350 if (!is_type_valid(type)) {
2351 skip_initializers();
2352 return create_empty_initializer();
2356 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2359 designator_t *designator = NULL;
2360 if (token.type == '.' || token.type == '[') {
2361 designator = parse_designation();
2362 goto finish_designator;
2363 } else if (token.type == T_IDENTIFIER && look_ahead(1)->type == ':') {
2364 /* GNU-style designator ("identifier: value") */
2365 designator = allocate_ast_zero(sizeof(designator[0]));
2366 designator->source_position = token.source_position;
2367 designator->symbol = token.v.symbol;
2372 /* reset path to toplevel, evaluate designator from there */
2373 ascend_to(path, top_path_level);
2374 if (!walk_designator(path, designator, false)) {
2375 /* can't continue after designation error */
2379 initializer_t *designator_initializer
2380 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2381 designator_initializer->designator.designator = designator;
2382 ARR_APP1(initializer_t*, initializers, designator_initializer);
2384 orig_type = path->top_type;
2385 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2390 if (token.type == '{') {
2391 if (type != NULL && is_type_scalar(type)) {
2392 sub = parse_scalar_initializer(type, env->must_be_constant);
2396 if (env->declaration != NULL) {
2397 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2398 env->declaration->symbol);
2400 errorf(HERE, "extra brace group at end of initializer");
2403 descend_into_subtype(path);
2405 add_anchor_token('}');
2406 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2408 rem_anchor_token('}');
2411 ascend_from_subtype(path);
2415 goto error_parse_next;
2419 /* must be an expression */
2420 expression_t *expression = parse_assignment_expression();
2422 if (env->must_be_constant && !is_initializer_constant(expression)) {
2423 errorf(&expression->base.source_position,
2424 "Initialisation expression '%E' is not constant\n",
2429 /* we are already outside, ... */
2433 /* handle { "string" } special case */
2434 if ((expression->kind == EXPR_STRING_LITERAL
2435 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2436 && outer_type != NULL) {
2437 sub = initializer_from_expression(outer_type, expression);
2439 if (token.type == ',') {
2442 if (token.type != '}') {
2443 warningf(HERE, "excessive elements in initializer for type '%T'",
2446 /* TODO: eat , ... */
2451 /* descend into subtypes until expression matches type */
2453 orig_type = path->top_type;
2454 type = skip_typeref(orig_type);
2456 sub = initializer_from_expression(orig_type, expression);
2460 if (!is_type_valid(type)) {
2463 if (is_type_scalar(type)) {
2464 errorf(&expression->base.source_position,
2465 "expression '%E' doesn't match expected type '%T'",
2466 expression, orig_type);
2470 descend_into_subtype(path);
2474 /* update largest index of top array */
2475 const type_path_entry_t *first = &path->path[0];
2476 type_t *first_type = first->type;
2477 first_type = skip_typeref(first_type);
2478 if (is_type_array(first_type)) {
2479 size_t index = first->v.index;
2480 if (index > path->max_index)
2481 path->max_index = index;
2485 /* append to initializers list */
2486 ARR_APP1(initializer_t*, initializers, sub);
2489 if (env->declaration != NULL)
2490 warningf(HERE, "excess elements in struct initializer for '%Y'",
2491 env->declaration->symbol);
2493 warningf(HERE, "excess elements in struct initializer");
2497 if (token.type == '}') {
2501 if (token.type == '}') {
2506 /* advance to the next declaration if we are not at the end */
2507 advance_current_object(path, top_path_level);
2508 orig_type = path->top_type;
2509 if (orig_type != NULL)
2510 type = skip_typeref(orig_type);
2516 size_t len = ARR_LEN(initializers);
2517 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2518 initializer_t *result = allocate_ast_zero(size);
2519 result->kind = INITIALIZER_LIST;
2520 result->list.len = len;
2521 memcpy(&result->list.initializers, initializers,
2522 len * sizeof(initializers[0]));
2524 DEL_ARR_F(initializers);
2525 ascend_to(path, top_path_level+1);
2530 skip_initializers();
2531 DEL_ARR_F(initializers);
2532 ascend_to(path, top_path_level+1);
2537 * Parses an initializer. Parsers either a compound literal
2538 * (env->declaration == NULL) or an initializer of a declaration.
2540 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2542 type_t *type = skip_typeref(env->type);
2543 initializer_t *result = NULL;
2546 if (is_type_scalar(type)) {
2547 result = parse_scalar_initializer(type, env->must_be_constant);
2548 } else if (token.type == '{') {
2552 memset(&path, 0, sizeof(path));
2553 path.top_type = env->type;
2554 path.path = NEW_ARR_F(type_path_entry_t, 0);
2556 descend_into_subtype(&path);
2558 add_anchor_token('}');
2559 result = parse_sub_initializer(&path, env->type, 1, env);
2560 rem_anchor_token('}');
2562 max_index = path.max_index;
2563 DEL_ARR_F(path.path);
2567 /* parse_scalar_initializer() also works in this case: we simply
2568 * have an expression without {} around it */
2569 result = parse_scalar_initializer(type, env->must_be_constant);
2572 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2573 * the array type size */
2574 if (is_type_array(type) && type->array.size_expression == NULL
2575 && result != NULL) {
2577 switch (result->kind) {
2578 case INITIALIZER_LIST:
2579 size = max_index + 1;
2582 case INITIALIZER_STRING:
2583 size = result->string.string.size;
2586 case INITIALIZER_WIDE_STRING:
2587 size = result->wide_string.string.size;
2590 case INITIALIZER_DESIGNATOR:
2591 case INITIALIZER_VALUE:
2592 /* can happen for parse errors */
2597 internal_errorf(HERE, "invalid initializer type");
2600 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2601 cnst->base.type = type_size_t;
2602 cnst->conste.v.int_value = size;
2604 type_t *new_type = duplicate_type(type);
2606 new_type->array.size_expression = cnst;
2607 new_type->array.size_constant = true;
2608 new_type->array.size = size;
2609 env->type = new_type;
2617 static declaration_t *append_declaration(declaration_t *declaration);
2619 static declaration_t *parse_compound_type_specifier(bool is_struct)
2621 gnu_attribute_t *attributes = NULL;
2622 decl_modifiers_t modifiers = 0;
2629 symbol_t *symbol = NULL;
2630 declaration_t *declaration = NULL;
2632 if (token.type == T___attribute__) {
2633 modifiers |= parse_attributes(&attributes);
2636 if (token.type == T_IDENTIFIER) {
2637 symbol = token.v.symbol;
2640 namespace_t const namespc =
2641 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2642 declaration = get_declaration(symbol, namespc);
2643 if (declaration != NULL) {
2644 if (declaration->parent_scope != scope &&
2645 (token.type == '{' || token.type == ';')) {
2647 } else if (declaration->init.complete &&
2648 token.type == '{') {
2649 assert(symbol != NULL);
2650 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2651 is_struct ? "struct" : "union", symbol,
2652 &declaration->source_position);
2653 declaration->scope.declarations = NULL;
2656 } else if (token.type != '{') {
2658 parse_error_expected("while parsing struct type specifier",
2659 T_IDENTIFIER, '{', NULL);
2661 parse_error_expected("while parsing union type specifier",
2662 T_IDENTIFIER, '{', NULL);
2668 if (declaration == NULL) {
2669 declaration = allocate_declaration_zero();
2670 declaration->namespc =
2671 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2672 declaration->source_position = token.source_position;
2673 declaration->symbol = symbol;
2674 declaration->parent_scope = scope;
2675 if (symbol != NULL) {
2676 environment_push(declaration);
2678 append_declaration(declaration);
2681 if (token.type == '{') {
2682 declaration->init.complete = true;
2684 parse_compound_type_entries(declaration);
2685 modifiers |= parse_attributes(&attributes);
2688 declaration->modifiers |= modifiers;
2692 static void parse_enum_entries(type_t *const enum_type)
2696 if (token.type == '}') {
2698 errorf(HERE, "empty enum not allowed");
2702 add_anchor_token('}');
2704 if (token.type != T_IDENTIFIER) {
2705 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2707 rem_anchor_token('}');
2711 declaration_t *const entry = allocate_declaration_zero();
2712 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2713 entry->type = enum_type;
2714 entry->symbol = token.v.symbol;
2715 entry->source_position = token.source_position;
2718 if (token.type == '=') {
2720 expression_t *value = parse_constant_expression();
2722 value = create_implicit_cast(value, enum_type);
2723 entry->init.enum_value = value;
2728 record_declaration(entry);
2730 if (token.type != ',')
2733 } while (token.type != '}');
2734 rem_anchor_token('}');
2742 static type_t *parse_enum_specifier(void)
2744 gnu_attribute_t *attributes = NULL;
2745 declaration_t *declaration;
2749 if (token.type == T_IDENTIFIER) {
2750 symbol = token.v.symbol;
2753 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2754 } else if (token.type != '{') {
2755 parse_error_expected("while parsing enum type specifier",
2756 T_IDENTIFIER, '{', NULL);
2763 if (declaration == NULL) {
2764 declaration = allocate_declaration_zero();
2765 declaration->namespc = NAMESPACE_ENUM;
2766 declaration->source_position = token.source_position;
2767 declaration->symbol = symbol;
2768 declaration->parent_scope = scope;
2771 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2772 type->enumt.declaration = declaration;
2774 if (token.type == '{') {
2775 if (declaration->init.complete) {
2776 errorf(HERE, "multiple definitions of enum %Y", symbol);
2778 if (symbol != NULL) {
2779 environment_push(declaration);
2781 append_declaration(declaration);
2782 declaration->init.complete = true;
2784 parse_enum_entries(type);
2785 parse_attributes(&attributes);
2792 * if a symbol is a typedef to another type, return true
2794 static bool is_typedef_symbol(symbol_t *symbol)
2796 const declaration_t *const declaration =
2797 get_declaration(symbol, NAMESPACE_NORMAL);
2799 declaration != NULL &&
2800 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2803 static type_t *parse_typeof(void)
2810 add_anchor_token(')');
2812 expression_t *expression = NULL;
2815 switch(token.type) {
2816 case T___extension__:
2817 /* This can be a prefix to a typename or an expression. We simply eat
2821 } while (token.type == T___extension__);
2825 if (is_typedef_symbol(token.v.symbol)) {
2826 type = parse_typename();
2828 expression = parse_expression();
2829 type = expression->base.type;
2834 type = parse_typename();
2838 expression = parse_expression();
2839 type = expression->base.type;
2843 rem_anchor_token(')');
2846 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2847 typeof_type->typeoft.expression = expression;
2848 typeof_type->typeoft.typeof_type = type;
2855 typedef enum specifiers_t {
2856 SPECIFIER_SIGNED = 1 << 0,
2857 SPECIFIER_UNSIGNED = 1 << 1,
2858 SPECIFIER_LONG = 1 << 2,
2859 SPECIFIER_INT = 1 << 3,
2860 SPECIFIER_DOUBLE = 1 << 4,
2861 SPECIFIER_CHAR = 1 << 5,
2862 SPECIFIER_SHORT = 1 << 6,
2863 SPECIFIER_LONG_LONG = 1 << 7,
2864 SPECIFIER_FLOAT = 1 << 8,
2865 SPECIFIER_BOOL = 1 << 9,
2866 SPECIFIER_VOID = 1 << 10,
2867 SPECIFIER_INT8 = 1 << 11,
2868 SPECIFIER_INT16 = 1 << 12,
2869 SPECIFIER_INT32 = 1 << 13,
2870 SPECIFIER_INT64 = 1 << 14,
2871 SPECIFIER_INT128 = 1 << 15,
2872 SPECIFIER_COMPLEX = 1 << 16,
2873 SPECIFIER_IMAGINARY = 1 << 17,
2876 static type_t *create_builtin_type(symbol_t *const symbol,
2877 type_t *const real_type)
2879 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2880 type->builtin.symbol = symbol;
2881 type->builtin.real_type = real_type;
2883 type_t *result = typehash_insert(type);
2884 if (type != result) {
2891 static type_t *get_typedef_type(symbol_t *symbol)
2893 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2894 if (declaration == NULL ||
2895 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2898 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2899 type->typedeft.declaration = declaration;
2905 * check for the allowed MS alignment values.
2907 static bool check_alignment_value(long long intvalue)
2909 if (intvalue < 1 || intvalue > 8192) {
2910 errorf(HERE, "illegal alignment value");
2913 unsigned v = (unsigned)intvalue;
2914 for(unsigned i = 1; i <= 8192; i += i) {
2918 errorf(HERE, "alignment must be power of two");
2922 #define DET_MOD(name, tag) do { \
2923 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2924 *modifiers |= tag; \
2927 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2929 decl_modifiers_t *modifiers = &specifiers->modifiers;
2932 if (token.type == T_restrict) {
2934 DET_MOD(restrict, DM_RESTRICT);
2936 } else if (token.type != T_IDENTIFIER)
2938 symbol_t *symbol = token.v.symbol;
2939 if (symbol == sym_align) {
2942 if (token.type != T_INTEGER)
2944 if (check_alignment_value(token.v.intvalue)) {
2945 if (specifiers->alignment != 0)
2946 warningf(HERE, "align used more than once");
2947 specifiers->alignment = (unsigned char)token.v.intvalue;
2951 } else if (symbol == sym_allocate) {
2954 if (token.type != T_IDENTIFIER)
2956 (void)token.v.symbol;
2958 } else if (symbol == sym_dllimport) {
2960 DET_MOD(dllimport, DM_DLLIMPORT);
2961 } else if (symbol == sym_dllexport) {
2963 DET_MOD(dllexport, DM_DLLEXPORT);
2964 } else if (symbol == sym_thread) {
2966 DET_MOD(thread, DM_THREAD);
2967 } else if (symbol == sym_naked) {
2969 DET_MOD(naked, DM_NAKED);
2970 } else if (symbol == sym_noinline) {
2972 DET_MOD(noinline, DM_NOINLINE);
2973 } else if (symbol == sym_noreturn) {
2975 DET_MOD(noreturn, DM_NORETURN);
2976 } else if (symbol == sym_nothrow) {
2978 DET_MOD(nothrow, DM_NOTHROW);
2979 } else if (symbol == sym_novtable) {
2981 DET_MOD(novtable, DM_NOVTABLE);
2982 } else if (symbol == sym_property) {
2986 bool is_get = false;
2987 if (token.type != T_IDENTIFIER)
2989 if (token.v.symbol == sym_get) {
2991 } else if (token.v.symbol == sym_put) {
2993 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2998 if (token.type != T_IDENTIFIER)
3001 if (specifiers->get_property_sym != NULL) {
3002 errorf(HERE, "get property name already specified");
3004 specifiers->get_property_sym = token.v.symbol;
3007 if (specifiers->put_property_sym != NULL) {
3008 errorf(HERE, "put property name already specified");
3010 specifiers->put_property_sym = token.v.symbol;
3014 if (token.type == ',') {
3021 } else if (symbol == sym_selectany) {
3023 DET_MOD(selectany, DM_SELECTANY);
3024 } else if (symbol == sym_uuid) {
3027 if (token.type != T_STRING_LITERAL)
3031 } else if (symbol == sym_deprecated) {
3033 if (specifiers->deprecated != 0)
3034 warningf(HERE, "deprecated used more than once");
3035 specifiers->deprecated = 1;
3036 if (token.type == '(') {
3038 if (token.type == T_STRING_LITERAL) {
3039 specifiers->deprecated_string = token.v.string.begin;
3042 errorf(HERE, "string literal expected");
3046 } else if (symbol == sym_noalias) {
3048 DET_MOD(noalias, DM_NOALIAS);
3050 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
3052 if (token.type == '(')
3056 if (token.type == ',')
3063 static declaration_t *create_error_declaration(symbol_t *symbol, storage_class_tag_t storage_class)
3065 declaration_t *const decl = allocate_declaration_zero();
3066 decl->source_position = *HERE;
3067 decl->storage_class =
3068 storage_class != STORAGE_CLASS_NONE || scope == global_scope ?
3069 storage_class : STORAGE_CLASS_AUTO;
3070 decl->declared_storage_class = decl->storage_class;
3071 decl->symbol = symbol;
3072 decl->implicit = true;
3073 record_declaration(decl);
3077 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3079 type_t *type = NULL;
3080 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3081 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3082 unsigned type_specifiers = 0;
3083 bool newtype = false;
3084 bool saw_error = false;
3086 specifiers->source_position = token.source_position;
3089 specifiers->modifiers
3090 |= parse_attributes(&specifiers->gnu_attributes);
3091 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3092 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3094 switch(token.type) {
3097 #define MATCH_STORAGE_CLASS(token, class) \
3099 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3100 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3102 specifiers->declared_storage_class = class; \
3106 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3107 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3108 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3109 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3110 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3115 add_anchor_token(')');
3116 parse_microsoft_extended_decl_modifier(specifiers);
3117 rem_anchor_token(')');
3122 switch (specifiers->declared_storage_class) {
3123 case STORAGE_CLASS_NONE:
3124 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3127 case STORAGE_CLASS_EXTERN:
3128 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3131 case STORAGE_CLASS_STATIC:
3132 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3136 errorf(HERE, "multiple storage classes in declaration specifiers");
3142 /* type qualifiers */
3143 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3145 qualifiers |= qualifier; \
3149 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3150 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3151 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3152 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3153 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3154 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3155 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3156 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3158 case T___extension__:
3163 /* type specifiers */
3164 #define MATCH_SPECIFIER(token, specifier, name) \
3167 if (type_specifiers & specifier) { \
3168 errorf(HERE, "multiple " name " type specifiers given"); \
3170 type_specifiers |= specifier; \
3174 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void");
3175 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char");
3176 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short");
3177 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int");
3178 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float");
3179 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double");
3180 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed");
3181 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned");
3182 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool");
3183 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8");
3184 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16");
3185 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32");
3186 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64");
3187 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128");
3188 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex");
3189 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary");
3191 case T__forceinline:
3192 /* only in microsoft mode */
3193 specifiers->modifiers |= DM_FORCEINLINE;
3198 specifiers->is_inline = true;
3203 if (type_specifiers & SPECIFIER_LONG_LONG) {
3204 errorf(HERE, "multiple type specifiers given");
3205 } else if (type_specifiers & SPECIFIER_LONG) {
3206 type_specifiers |= SPECIFIER_LONG_LONG;
3208 type_specifiers |= SPECIFIER_LONG;
3213 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3215 type->compound.declaration = parse_compound_type_specifier(true);
3219 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3220 type->compound.declaration = parse_compound_type_specifier(false);
3221 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3222 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3226 type = parse_enum_specifier();
3229 type = parse_typeof();
3231 case T___builtin_va_list:
3232 type = duplicate_type(type_valist);
3236 case T_IDENTIFIER: {
3237 /* only parse identifier if we haven't found a type yet */
3238 if (type != NULL || type_specifiers != 0) {
3239 /* Be somewhat resilient to typos like 'unsigned lng* f()' in a
3240 * declaration, so it doesn't generate errors about expecting '(' or
3242 switch (look_ahead(1)->type) {
3249 case T__forceinline: /* ^ DECLARATION_START except for __attribute__ */
3252 errorf(HERE, "discarding stray %K in declaration specifier", &token);
3257 goto finish_specifiers;
3261 type_t *const typedef_type = get_typedef_type(token.v.symbol);
3262 if (typedef_type == NULL) {
3263 /* Be somewhat resilient to typos like 'vodi f()' at the beginning of a
3264 * declaration, so it doesn't generate 'implicit int' followed by more
3265 * errors later on. */
3266 token_type_t const la1_type = (token_type_t)look_ahead(1)->type;
3271 errorf(HERE, "%K does not name a type", &token);
3273 declaration_t *const decl =
3274 create_error_declaration(token.v.symbol, STORAGE_CLASS_TYPEDEF);
3276 type = allocate_type_zero(TYPE_TYPEDEF, HERE);
3277 type->typedeft.declaration = decl;
3281 if (la1_type == '*')
3282 goto finish_specifiers;
3287 goto finish_specifiers;
3292 type = typedef_type;
3296 /* function specifier */
3298 goto finish_specifiers;
3303 if (type == NULL || (saw_error && type_specifiers != 0)) {
3304 atomic_type_kind_t atomic_type;
3306 /* match valid basic types */
3307 switch(type_specifiers) {
3308 case SPECIFIER_VOID:
3309 atomic_type = ATOMIC_TYPE_VOID;
3311 case SPECIFIER_CHAR:
3312 atomic_type = ATOMIC_TYPE_CHAR;
3314 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3315 atomic_type = ATOMIC_TYPE_SCHAR;
3317 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3318 atomic_type = ATOMIC_TYPE_UCHAR;
3320 case SPECIFIER_SHORT:
3321 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3322 case SPECIFIER_SHORT | SPECIFIER_INT:
3323 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3324 atomic_type = ATOMIC_TYPE_SHORT;
3326 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3327 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3328 atomic_type = ATOMIC_TYPE_USHORT;
3331 case SPECIFIER_SIGNED:
3332 case SPECIFIER_SIGNED | SPECIFIER_INT:
3333 atomic_type = ATOMIC_TYPE_INT;
3335 case SPECIFIER_UNSIGNED:
3336 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3337 atomic_type = ATOMIC_TYPE_UINT;
3339 case SPECIFIER_LONG:
3340 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3341 case SPECIFIER_LONG | SPECIFIER_INT:
3342 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3343 atomic_type = ATOMIC_TYPE_LONG;
3345 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3346 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3347 atomic_type = ATOMIC_TYPE_ULONG;
3350 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3351 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3352 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3353 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3355 atomic_type = ATOMIC_TYPE_LONGLONG;
3356 goto warn_about_long_long;
3358 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3359 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3361 atomic_type = ATOMIC_TYPE_ULONGLONG;
3362 warn_about_long_long:
3363 if (warning.long_long) {
3364 warningf(&specifiers->source_position,
3365 "ISO C90 does not support 'long long'");
3369 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3370 atomic_type = unsigned_int8_type_kind;
3373 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3374 atomic_type = unsigned_int16_type_kind;
3377 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3378 atomic_type = unsigned_int32_type_kind;
3381 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3382 atomic_type = unsigned_int64_type_kind;
3385 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3386 atomic_type = unsigned_int128_type_kind;
3389 case SPECIFIER_INT8:
3390 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3391 atomic_type = int8_type_kind;
3394 case SPECIFIER_INT16:
3395 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3396 atomic_type = int16_type_kind;
3399 case SPECIFIER_INT32:
3400 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3401 atomic_type = int32_type_kind;
3404 case SPECIFIER_INT64:
3405 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3406 atomic_type = int64_type_kind;
3409 case SPECIFIER_INT128:
3410 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3411 atomic_type = int128_type_kind;
3414 case SPECIFIER_FLOAT:
3415 atomic_type = ATOMIC_TYPE_FLOAT;
3417 case SPECIFIER_DOUBLE:
3418 atomic_type = ATOMIC_TYPE_DOUBLE;
3420 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3421 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3423 case SPECIFIER_BOOL:
3424 atomic_type = ATOMIC_TYPE_BOOL;
3426 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3427 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3428 atomic_type = ATOMIC_TYPE_FLOAT;
3430 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3431 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3432 atomic_type = ATOMIC_TYPE_DOUBLE;
3434 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3435 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3436 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3439 /* invalid specifier combination, give an error message */
3440 if (type_specifiers == 0) {
3442 specifiers->type = type_error_type;
3447 if (warning.implicit_int) {
3448 warningf(HERE, "no type specifiers in declaration, using 'int'");
3450 atomic_type = ATOMIC_TYPE_INT;
3453 errorf(HERE, "no type specifiers given in declaration");
3455 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3456 (type_specifiers & SPECIFIER_UNSIGNED)) {
3457 errorf(HERE, "signed and unsigned specifiers given");
3458 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3459 errorf(HERE, "only integer types can be signed or unsigned");
3461 errorf(HERE, "multiple datatypes in declaration");
3463 atomic_type = ATOMIC_TYPE_INVALID;
3466 if (type_specifiers & SPECIFIER_COMPLEX &&
3467 atomic_type != ATOMIC_TYPE_INVALID) {
3468 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3469 type->complex.akind = atomic_type;
3470 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3471 atomic_type != ATOMIC_TYPE_INVALID) {
3472 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3473 type->imaginary.akind = atomic_type;
3475 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3476 type->atomic.akind = atomic_type;
3479 } else if (type_specifiers != 0) {
3480 errorf(HERE, "multiple datatypes in declaration");
3483 /* FIXME: check type qualifiers here */
3485 type->base.qualifiers = qualifiers;
3486 type->base.modifiers = modifiers;
3488 type_t *result = typehash_insert(type);
3489 if (newtype && result != type) {
3493 specifiers->type = result;
3498 static type_qualifiers_t parse_type_qualifiers(void)
3500 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3503 switch(token.type) {
3504 /* type qualifiers */
3505 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3506 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3507 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3508 /* microsoft extended type modifiers */
3509 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3510 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3511 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3512 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3513 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3521 static declaration_t *parse_identifier_list(void)
3523 declaration_t *declarations = NULL;
3524 declaration_t *last_declaration = NULL;
3526 declaration_t *const declaration = allocate_declaration_zero();
3527 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3528 declaration->source_position = token.source_position;
3529 declaration->symbol = token.v.symbol;
3532 if (last_declaration != NULL) {
3533 last_declaration->next = declaration;
3535 declarations = declaration;
3537 last_declaration = declaration;
3539 if (token.type != ',') {
3543 } while (token.type == T_IDENTIFIER);
3545 return declarations;
3548 static type_t *automatic_type_conversion(type_t *orig_type);
3550 static void semantic_parameter(declaration_t *declaration)
3552 /* TODO: improve error messages */
3553 source_position_t const* const pos = &declaration->source_position;
3555 switch (declaration->declared_storage_class) {
3556 case STORAGE_CLASS_TYPEDEF:
3557 errorf(pos, "typedef not allowed in parameter list");
3560 /* Allowed storage classes */
3561 case STORAGE_CLASS_NONE:
3562 case STORAGE_CLASS_REGISTER:
3566 errorf(pos, "parameter may only have none or register storage class");
3570 type_t *const orig_type = declaration->type;
3571 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3572 * sugar. Turn it into a pointer.
3573 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3574 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3576 type_t *const type = automatic_type_conversion(orig_type);
3577 declaration->type = type;
3579 if (is_type_incomplete(skip_typeref(type))) {
3580 errorf(pos, "incomplete type '%T' not allowed for parameter '%Y'",
3581 orig_type, declaration->symbol);
3585 static declaration_t *parse_parameter(void)
3587 declaration_specifiers_t specifiers;
3588 memset(&specifiers, 0, sizeof(specifiers));
3590 parse_declaration_specifiers(&specifiers);
3592 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3597 static declaration_t *parse_parameters(function_type_t *type)
3599 declaration_t *declarations = NULL;
3602 add_anchor_token(')');
3603 int saved_comma_state = save_and_reset_anchor_state(',');
3605 if (token.type == T_IDENTIFIER) {
3606 symbol_t *symbol = token.v.symbol;
3607 if (!is_typedef_symbol(symbol)) {
3608 type->kr_style_parameters = true;
3609 declarations = parse_identifier_list();
3610 goto parameters_finished;
3614 if (token.type == ')') {
3615 type->unspecified_parameters = 1;
3616 goto parameters_finished;
3619 declaration_t *declaration;
3620 declaration_t *last_declaration = NULL;
3621 function_parameter_t *parameter;
3622 function_parameter_t *last_parameter = NULL;
3625 switch(token.type) {
3629 goto parameters_finished;
3632 case T___extension__:
3634 declaration = parse_parameter();
3636 /* func(void) is not a parameter */
3637 if (last_parameter == NULL
3638 && token.type == ')'
3639 && declaration->symbol == NULL
3640 && skip_typeref(declaration->type) == type_void) {
3641 goto parameters_finished;
3643 semantic_parameter(declaration);
3645 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3646 memset(parameter, 0, sizeof(parameter[0]));
3647 parameter->type = declaration->type;
3649 if (last_parameter != NULL) {
3650 last_declaration->next = declaration;
3651 last_parameter->next = parameter;
3653 type->parameters = parameter;
3654 declarations = declaration;
3656 last_parameter = parameter;
3657 last_declaration = declaration;
3661 goto parameters_finished;
3663 if (token.type != ',') {
3664 goto parameters_finished;
3670 parameters_finished:
3671 rem_anchor_token(')');
3674 restore_anchor_state(',', saved_comma_state);
3675 return declarations;
3678 restore_anchor_state(',', saved_comma_state);
3682 typedef enum construct_type_kind_t {
3687 } construct_type_kind_t;
3689 typedef struct construct_type_t construct_type_t;
3690 struct construct_type_t {
3691 construct_type_kind_t kind;
3692 construct_type_t *next;
3695 typedef struct parsed_pointer_t parsed_pointer_t;
3696 struct parsed_pointer_t {
3697 construct_type_t construct_type;
3698 type_qualifiers_t type_qualifiers;
3701 typedef struct construct_function_type_t construct_function_type_t;
3702 struct construct_function_type_t {
3703 construct_type_t construct_type;
3704 type_t *function_type;
3707 typedef struct parsed_array_t parsed_array_t;
3708 struct parsed_array_t {
3709 construct_type_t construct_type;
3710 type_qualifiers_t type_qualifiers;
3716 typedef struct construct_base_type_t construct_base_type_t;
3717 struct construct_base_type_t {
3718 construct_type_t construct_type;
3722 static construct_type_t *parse_pointer_declarator(void)
3726 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3727 memset(pointer, 0, sizeof(pointer[0]));
3728 pointer->construct_type.kind = CONSTRUCT_POINTER;
3729 pointer->type_qualifiers = parse_type_qualifiers();
3731 return (construct_type_t*) pointer;
3734 static construct_type_t *parse_array_declarator(void)
3737 add_anchor_token(']');
3739 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3740 memset(array, 0, sizeof(array[0]));
3741 array->construct_type.kind = CONSTRUCT_ARRAY;
3743 if (token.type == T_static) {
3744 array->is_static = true;
3748 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3749 if (type_qualifiers != 0) {
3750 if (token.type == T_static) {
3751 array->is_static = true;
3755 array->type_qualifiers = type_qualifiers;
3757 if (token.type == '*' && look_ahead(1)->type == ']') {
3758 array->is_variable = true;
3760 } else if (token.type != ']') {
3761 array->size = parse_assignment_expression();
3764 rem_anchor_token(']');
3767 return (construct_type_t*) array;
3772 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3775 if (declaration != NULL) {
3776 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3778 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3780 if (mask & (mask-1)) {
3781 const char *first = NULL, *second = NULL;
3783 /* more than one calling convention set */
3784 if (declaration->modifiers & DM_CDECL) {
3785 if (first == NULL) first = "cdecl";
3786 else if (second == NULL) second = "cdecl";
3788 if (declaration->modifiers & DM_STDCALL) {
3789 if (first == NULL) first = "stdcall";
3790 else if (second == NULL) second = "stdcall";
3792 if (declaration->modifiers & DM_FASTCALL) {
3793 if (first == NULL) first = "fastcall";
3794 else if (second == NULL) second = "fastcall";
3796 if (declaration->modifiers & DM_THISCALL) {
3797 if (first == NULL) first = "thiscall";
3798 else if (second == NULL) second = "thiscall";
3800 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3803 if (declaration->modifiers & DM_CDECL)
3804 type->function.calling_convention = CC_CDECL;
3805 else if (declaration->modifiers & DM_STDCALL)
3806 type->function.calling_convention = CC_STDCALL;
3807 else if (declaration->modifiers & DM_FASTCALL)
3808 type->function.calling_convention = CC_FASTCALL;
3809 else if (declaration->modifiers & DM_THISCALL)
3810 type->function.calling_convention = CC_THISCALL;
3812 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3815 declaration_t *parameters = parse_parameters(&type->function);
3816 if (declaration != NULL) {
3817 declaration->scope.declarations = parameters;
3820 construct_function_type_t *construct_function_type =
3821 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3822 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3823 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3824 construct_function_type->function_type = type;
3826 return &construct_function_type->construct_type;
3829 static void fix_declaration_type(declaration_t *declaration)
3831 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3832 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3834 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3835 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3837 if (declaration->type->base.modifiers == type_modifiers)
3840 type_t *copy = duplicate_type(declaration->type);
3841 copy->base.modifiers = type_modifiers;
3843 type_t *result = typehash_insert(copy);
3844 if (result != copy) {
3845 obstack_free(type_obst, copy);
3848 declaration->type = result;
3851 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3852 bool may_be_abstract)
3854 /* construct a single linked list of construct_type_t's which describe
3855 * how to construct the final declarator type */
3856 construct_type_t *first = NULL;
3857 construct_type_t *last = NULL;
3858 gnu_attribute_t *attributes = NULL;
3860 decl_modifiers_t modifiers = parse_attributes(&attributes);
3863 while (token.type == '*') {
3864 construct_type_t *type = parse_pointer_declarator();
3874 /* TODO: find out if this is correct */
3875 modifiers |= parse_attributes(&attributes);
3878 if (declaration != NULL)
3879 declaration->modifiers |= modifiers;
3881 construct_type_t *inner_types = NULL;
3883 switch(token.type) {
3885 if (declaration == NULL) {
3886 errorf(HERE, "no identifier expected in typename");
3888 declaration->symbol = token.v.symbol;
3889 declaration->source_position = token.source_position;
3895 add_anchor_token(')');
3896 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3897 /* All later declarators only modify the return type, not declaration */
3899 rem_anchor_token(')');
3903 if (may_be_abstract)
3905 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3906 /* avoid a loop in the outermost scope, because eat_statement doesn't
3908 if (token.type == '}' && current_function == NULL) {
3916 construct_type_t *p = last;
3919 construct_type_t *type;
3920 switch(token.type) {
3922 type = parse_function_declarator(declaration);
3925 type = parse_array_declarator();
3928 goto declarator_finished;
3931 /* insert in the middle of the list (behind p) */
3933 type->next = p->next;
3944 declarator_finished:
3945 /* append inner_types at the end of the list, we don't to set last anymore
3946 * as it's not needed anymore */
3948 assert(first == NULL);
3949 first = inner_types;
3951 last->next = inner_types;
3959 static void parse_declaration_attributes(declaration_t *declaration)
3961 gnu_attribute_t *attributes = NULL;
3962 decl_modifiers_t modifiers = parse_attributes(&attributes);
3964 if (declaration == NULL)
3967 declaration->modifiers |= modifiers;
3968 /* check if we have these stupid mode attributes... */
3969 type_t *old_type = declaration->type;
3970 if (old_type == NULL)
3973 gnu_attribute_t *attribute = attributes;
3974 for ( ; attribute != NULL; attribute = attribute->next) {
3975 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3978 atomic_type_kind_t akind = attribute->u.akind;
3979 if (!is_type_signed(old_type)) {
3981 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3982 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3983 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3984 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3986 panic("invalid akind in mode attribute");
3990 = make_atomic_type(akind, old_type->base.qualifiers);
3994 static type_t *construct_declarator_type(construct_type_t *construct_list,
3997 construct_type_t *iter = construct_list;
3998 for( ; iter != NULL; iter = iter->next) {
3999 switch(iter->kind) {
4000 case CONSTRUCT_INVALID:
4001 internal_errorf(HERE, "invalid type construction found");
4002 case CONSTRUCT_FUNCTION: {
4003 construct_function_type_t *construct_function_type
4004 = (construct_function_type_t*) iter;
4006 type_t *function_type = construct_function_type->function_type;
4008 function_type->function.return_type = type;
4010 type_t *skipped_return_type = skip_typeref(type);
4011 if (is_type_function(skipped_return_type)) {
4012 errorf(HERE, "function returning function is not allowed");
4013 type = type_error_type;
4014 } else if (is_type_array(skipped_return_type)) {
4015 errorf(HERE, "function returning array is not allowed");
4016 type = type_error_type;
4018 type = function_type;
4023 case CONSTRUCT_POINTER: {
4024 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
4025 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
4026 pointer_type->pointer.points_to = type;
4027 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
4029 type = pointer_type;
4033 case CONSTRUCT_ARRAY: {
4034 parsed_array_t *parsed_array = (parsed_array_t*) iter;
4035 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
4037 expression_t *size_expression = parsed_array->size;
4038 if (size_expression != NULL) {
4040 = create_implicit_cast(size_expression, type_size_t);
4043 array_type->base.qualifiers = parsed_array->type_qualifiers;
4044 array_type->array.element_type = type;
4045 array_type->array.is_static = parsed_array->is_static;
4046 array_type->array.is_variable = parsed_array->is_variable;
4047 array_type->array.size_expression = size_expression;
4049 if (size_expression != NULL) {
4050 if (is_constant_expression(size_expression)) {
4051 array_type->array.size_constant = true;
4052 array_type->array.size
4053 = fold_constant(size_expression);
4055 array_type->array.is_vla = true;
4059 type_t *skipped_type = skip_typeref(type);
4060 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
4061 errorf(HERE, "array of void is not allowed");
4062 type = type_error_type;
4070 type_t *hashed_type = typehash_insert(type);
4071 if (hashed_type != type) {
4072 /* the function type was constructed earlier freeing it here will
4073 * destroy other types... */
4074 if (iter->kind != CONSTRUCT_FUNCTION) {
4084 static declaration_t *parse_declarator(
4085 const declaration_specifiers_t *specifiers, bool may_be_abstract)
4087 declaration_t *const declaration = allocate_declaration_zero();
4088 declaration->source_position = specifiers->source_position;
4089 declaration->declared_storage_class = specifiers->declared_storage_class;
4090 declaration->modifiers = specifiers->modifiers;
4091 declaration->deprecated_string = specifiers->deprecated_string;
4092 declaration->get_property_sym = specifiers->get_property_sym;
4093 declaration->put_property_sym = specifiers->put_property_sym;
4094 declaration->is_inline = specifiers->is_inline;
4096 declaration->storage_class = specifiers->declared_storage_class;
4097 if (declaration->storage_class == STORAGE_CLASS_NONE
4098 && scope != global_scope) {
4099 declaration->storage_class = STORAGE_CLASS_AUTO;
4102 if (specifiers->alignment != 0) {
4103 /* TODO: add checks here */
4104 declaration->alignment = specifiers->alignment;
4107 construct_type_t *construct_type
4108 = parse_inner_declarator(declaration, may_be_abstract);
4109 type_t *const type = specifiers->type;
4110 declaration->type = construct_declarator_type(construct_type, type);
4112 parse_declaration_attributes(declaration);
4114 fix_declaration_type(declaration);
4116 if (construct_type != NULL) {
4117 obstack_free(&temp_obst, construct_type);
4123 static type_t *parse_abstract_declarator(type_t *base_type)
4125 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
4127 type_t *result = construct_declarator_type(construct_type, base_type);
4128 if (construct_type != NULL) {
4129 obstack_free(&temp_obst, construct_type);
4135 static declaration_t *append_declaration(declaration_t* const declaration)
4137 if (last_declaration != NULL) {
4138 last_declaration->next = declaration;
4140 scope->declarations = declaration;
4142 last_declaration = declaration;
4147 * Check if the declaration of main is suspicious. main should be a
4148 * function with external linkage, returning int, taking either zero
4149 * arguments, two, or three arguments of appropriate types, ie.
4151 * int main([ int argc, char **argv [, char **env ] ]).
4153 * @param decl the declaration to check
4154 * @param type the function type of the declaration
4156 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4158 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4159 warningf(&decl->source_position,
4160 "'main' is normally a non-static function");
4162 if (skip_typeref(func_type->return_type) != type_int) {
4163 warningf(&decl->source_position,
4164 "return type of 'main' should be 'int', but is '%T'",
4165 func_type->return_type);
4167 const function_parameter_t *parm = func_type->parameters;
4169 type_t *const first_type = parm->type;
4170 if (!types_compatible(skip_typeref(first_type), type_int)) {
4171 warningf(&decl->source_position,
4172 "first argument of 'main' should be 'int', but is '%T'", first_type);
4176 type_t *const second_type = parm->type;
4177 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4178 warningf(&decl->source_position,
4179 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4183 type_t *const third_type = parm->type;
4184 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4185 warningf(&decl->source_position,
4186 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4190 goto warn_arg_count;
4194 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4200 * Check if a symbol is the equal to "main".
4202 static bool is_sym_main(const symbol_t *const sym)
4204 return strcmp(sym->string, "main") == 0;
4207 static declaration_t *internal_record_declaration(
4208 declaration_t *const declaration,
4209 const bool is_definition)
4211 const symbol_t *const symbol = declaration->symbol;
4212 const namespace_t namespc = (namespace_t)declaration->namespc;
4214 assert(symbol != NULL);
4215 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4217 type_t *const orig_type = declaration->type;
4218 type_t *const type = skip_typeref(orig_type);
4219 if (is_type_function(type) &&
4220 type->function.unspecified_parameters &&
4221 warning.strict_prototypes &&
4222 previous_declaration == NULL) {
4223 warningf(&declaration->source_position,
4224 "function declaration '%#T' is not a prototype",
4225 orig_type, declaration->symbol);
4228 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4229 check_type_of_main(declaration, &type->function);
4232 if (warning.nested_externs &&
4233 declaration->storage_class == STORAGE_CLASS_EXTERN &&
4234 scope != global_scope) {
4235 warningf(&declaration->source_position,
4236 "nested extern declaration of '%#T'", declaration->type, symbol);
4239 assert(declaration != previous_declaration);
4240 if (previous_declaration != NULL
4241 && previous_declaration->parent_scope == scope) {
4242 /* can happen for K&R style declarations */
4243 if (previous_declaration->type == NULL) {
4244 previous_declaration->type = declaration->type;
4247 const type_t *prev_type = skip_typeref(previous_declaration->type);
4248 if (!types_compatible(type, prev_type)) {
4249 errorf(&declaration->source_position,
4250 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4251 orig_type, symbol, previous_declaration->type, symbol,
4252 &previous_declaration->source_position);
4254 unsigned old_storage_class = previous_declaration->storage_class;
4255 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4256 errorf(&declaration->source_position,
4257 "redeclaration of enum entry '%Y' (declared %P)",
4258 symbol, &previous_declaration->source_position);
4259 return previous_declaration;
4262 if (warning.redundant_decls &&
4264 previous_declaration->storage_class == STORAGE_CLASS_STATIC &&
4265 !(previous_declaration->modifiers & DM_USED) &&
4266 !previous_declaration->used) {
4267 warningf(&previous_declaration->source_position,
4268 "unnecessary static forward declaration for '%#T'",
4269 previous_declaration->type, symbol);
4272 unsigned new_storage_class = declaration->storage_class;
4274 if (is_type_incomplete(prev_type)) {
4275 previous_declaration->type = type;
4279 /* pretend no storage class means extern for function
4280 * declarations (except if the previous declaration is neither
4281 * none nor extern) */
4282 if (is_type_function(type)) {
4283 if (prev_type->function.unspecified_parameters) {
4284 previous_declaration->type = type;
4288 switch (old_storage_class) {
4289 case STORAGE_CLASS_NONE:
4290 old_storage_class = STORAGE_CLASS_EXTERN;
4293 case STORAGE_CLASS_EXTERN:
4294 if (is_definition) {
4295 if (warning.missing_prototypes &&
4296 prev_type->function.unspecified_parameters &&
4297 !is_sym_main(symbol)) {
4298 warningf(&declaration->source_position,
4299 "no previous prototype for '%#T'",
4302 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4303 new_storage_class = STORAGE_CLASS_EXTERN;
4312 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4313 new_storage_class == STORAGE_CLASS_EXTERN) {
4314 warn_redundant_declaration:
4315 if (!is_definition &&
4316 warning.redundant_decls &&
4317 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4318 warningf(&declaration->source_position,
4319 "redundant declaration for '%Y' (declared %P)",
4320 symbol, &previous_declaration->source_position);
4322 } else if (current_function == NULL) {
4323 if (old_storage_class != STORAGE_CLASS_STATIC &&
4324 new_storage_class == STORAGE_CLASS_STATIC) {
4325 errorf(&declaration->source_position,
4326 "static declaration of '%Y' follows non-static declaration (declared %P)",
4327 symbol, &previous_declaration->source_position);
4328 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4329 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4330 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4332 goto warn_redundant_declaration;
4334 } else if (old_storage_class == new_storage_class) {
4335 errorf(&declaration->source_position,
4336 "redeclaration of '%Y' (declared %P)",
4337 symbol, &previous_declaration->source_position);
4339 errorf(&declaration->source_position,
4340 "redeclaration of '%Y' with different linkage (declared %P)",
4341 symbol, &previous_declaration->source_position);
4345 previous_declaration->modifiers |= declaration->modifiers;
4346 previous_declaration->is_inline |= declaration->is_inline;
4347 return previous_declaration;
4348 } else if (is_type_function(type)) {
4349 if (is_definition &&
4350 declaration->storage_class != STORAGE_CLASS_STATIC) {
4351 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4352 warningf(&declaration->source_position,
4353 "no previous prototype for '%#T'", orig_type, symbol);
4354 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4355 warningf(&declaration->source_position,
4356 "no previous declaration for '%#T'", orig_type,
4361 if (warning.missing_declarations &&
4362 scope == global_scope && (
4363 declaration->storage_class == STORAGE_CLASS_NONE ||
4364 declaration->storage_class == STORAGE_CLASS_THREAD
4366 warningf(&declaration->source_position,
4367 "no previous declaration for '%#T'", orig_type, symbol);
4371 assert(declaration->parent_scope == NULL);
4372 assert(scope != NULL);
4374 declaration->parent_scope = scope;
4376 environment_push(declaration);
4377 return append_declaration(declaration);
4380 static declaration_t *record_declaration(declaration_t *declaration)
4382 return internal_record_declaration(declaration, false);
4385 static declaration_t *record_definition(declaration_t *declaration)
4387 return internal_record_declaration(declaration, true);
4390 static void parser_error_multiple_definition(declaration_t *declaration,
4391 const source_position_t *source_position)
4393 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4394 declaration->symbol, &declaration->source_position);
4397 static bool is_declaration_specifier(const token_t *token,
4398 bool only_specifiers_qualifiers)
4400 switch(token->type) {
4405 return is_typedef_symbol(token->v.symbol);
4407 case T___extension__:
4409 return !only_specifiers_qualifiers;
4416 static void parse_init_declarator_rest(declaration_t *declaration)
4420 type_t *orig_type = declaration->type;
4421 type_t *type = skip_typeref(orig_type);
4423 if (declaration->init.initializer != NULL) {
4424 parser_error_multiple_definition(declaration, HERE);
4427 bool must_be_constant = false;
4428 if (declaration->storage_class == STORAGE_CLASS_STATIC
4429 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4430 || declaration->parent_scope == global_scope) {
4431 must_be_constant = true;
4434 parse_initializer_env_t env;
4435 env.type = orig_type;
4436 env.must_be_constant = must_be_constant;
4437 env.declaration = declaration;
4439 initializer_t *initializer = parse_initializer(&env);
4441 if (env.type != orig_type) {
4442 orig_type = env.type;
4443 type = skip_typeref(orig_type);
4444 declaration->type = env.type;
4447 if (is_type_function(type)) {
4448 errorf(&declaration->source_position,
4449 "initializers not allowed for function types at declator '%Y' (type '%T')",
4450 declaration->symbol, orig_type);
4452 declaration->init.initializer = initializer;
4456 /* parse rest of a declaration without any declarator */
4457 static void parse_anonymous_declaration_rest(
4458 const declaration_specifiers_t *specifiers,
4459 parsed_declaration_func finished_declaration)
4463 declaration_t *const declaration = allocate_declaration_zero();
4464 declaration->type = specifiers->type;
4465 declaration->declared_storage_class = specifiers->declared_storage_class;
4466 declaration->source_position = specifiers->source_position;
4467 declaration->modifiers = specifiers->modifiers;
4469 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4470 warningf(&declaration->source_position,
4471 "useless storage class in empty declaration");
4473 declaration->storage_class = STORAGE_CLASS_NONE;
4475 type_t *type = declaration->type;
4476 switch (type->kind) {
4477 case TYPE_COMPOUND_STRUCT:
4478 case TYPE_COMPOUND_UNION: {
4479 if (type->compound.declaration->symbol == NULL) {
4480 warningf(&declaration->source_position,
4481 "unnamed struct/union that defines no instances");
4490 warningf(&declaration->source_position, "empty declaration");
4494 finished_declaration(declaration);
4497 static void parse_declaration_rest(declaration_t *ndeclaration,
4498 const declaration_specifiers_t *specifiers,
4499 parsed_declaration_func finished_declaration)
4501 add_anchor_token(';');
4502 add_anchor_token('=');
4503 add_anchor_token(',');
4505 declaration_t *declaration = finished_declaration(ndeclaration);
4507 type_t *orig_type = declaration->type;
4508 type_t *type = skip_typeref(orig_type);
4510 if (type->kind != TYPE_FUNCTION &&
4511 declaration->is_inline &&
4512 is_type_valid(type)) {
4513 warningf(&declaration->source_position,
4514 "variable '%Y' declared 'inline'\n", declaration->symbol);
4517 if (token.type == '=') {
4518 parse_init_declarator_rest(declaration);
4521 if (token.type != ',')
4525 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4530 rem_anchor_token(';');
4531 rem_anchor_token('=');
4532 rem_anchor_token(',');
4535 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4537 symbol_t *symbol = declaration->symbol;
4538 if (symbol == NULL) {
4539 errorf(HERE, "anonymous declaration not valid as function parameter");
4542 namespace_t namespc = (namespace_t) declaration->namespc;
4543 if (namespc != NAMESPACE_NORMAL) {
4544 return record_declaration(declaration);
4547 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4548 if (previous_declaration == NULL ||
4549 previous_declaration->parent_scope != scope) {
4550 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4555 if (previous_declaration->type == NULL) {
4556 previous_declaration->type = declaration->type;
4557 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4558 previous_declaration->storage_class = declaration->storage_class;
4559 previous_declaration->parent_scope = scope;
4560 return previous_declaration;
4562 return record_declaration(declaration);
4566 static void parse_declaration(parsed_declaration_func finished_declaration)
4568 declaration_specifiers_t specifiers;
4569 memset(&specifiers, 0, sizeof(specifiers));
4570 parse_declaration_specifiers(&specifiers);
4572 if (token.type == ';') {
4573 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4575 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4576 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4580 static type_t *get_default_promoted_type(type_t *orig_type)
4582 type_t *result = orig_type;
4584 type_t *type = skip_typeref(orig_type);
4585 if (is_type_integer(type)) {
4586 result = promote_integer(type);
4587 } else if (type == type_float) {
4588 result = type_double;
4594 static void parse_kr_declaration_list(declaration_t *declaration)
4596 type_t *type = skip_typeref(declaration->type);
4597 if (!is_type_function(type))
4600 if (!type->function.kr_style_parameters)
4603 /* push function parameters */
4604 int top = environment_top();
4605 scope_t *last_scope = scope;
4606 set_scope(&declaration->scope);
4608 declaration_t *parameter = declaration->scope.declarations;
4609 for ( ; parameter != NULL; parameter = parameter->next) {
4610 assert(parameter->parent_scope == NULL);
4611 parameter->parent_scope = scope;
4612 environment_push(parameter);
4615 /* parse declaration list */
4616 while (is_declaration_specifier(&token, false)) {
4617 parse_declaration(finished_kr_declaration);
4620 /* pop function parameters */
4621 assert(scope == &declaration->scope);
4622 set_scope(last_scope);
4623 environment_pop_to(top);
4625 /* update function type */
4626 type_t *new_type = duplicate_type(type);
4628 function_parameter_t *parameters = NULL;
4629 function_parameter_t *last_parameter = NULL;
4631 declaration_t *parameter_declaration = declaration->scope.declarations;
4632 for( ; parameter_declaration != NULL;
4633 parameter_declaration = parameter_declaration->next) {
4634 type_t *parameter_type = parameter_declaration->type;
4635 if (parameter_type == NULL) {
4637 errorf(HERE, "no type specified for function parameter '%Y'",
4638 parameter_declaration->symbol);
4640 if (warning.implicit_int) {
4641 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4642 parameter_declaration->symbol);
4644 parameter_type = type_int;
4645 parameter_declaration->type = parameter_type;
4649 semantic_parameter(parameter_declaration);
4650 parameter_type = parameter_declaration->type;
4653 * we need the default promoted types for the function type
4655 parameter_type = get_default_promoted_type(parameter_type);
4657 function_parameter_t *function_parameter
4658 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4659 memset(function_parameter, 0, sizeof(function_parameter[0]));
4661 function_parameter->type = parameter_type;
4662 if (last_parameter != NULL) {
4663 last_parameter->next = function_parameter;
4665 parameters = function_parameter;
4667 last_parameter = function_parameter;
4670 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4672 new_type->function.parameters = parameters;
4673 new_type->function.unspecified_parameters = true;
4675 type = typehash_insert(new_type);
4676 if (type != new_type) {
4677 obstack_free(type_obst, new_type);
4680 declaration->type = type;
4683 static bool first_err = true;
4686 * When called with first_err set, prints the name of the current function,
4689 static void print_in_function(void)
4693 diagnosticf("%s: In function '%Y':\n",
4694 current_function->source_position.input_name,
4695 current_function->symbol);
4700 * Check if all labels are defined in the current function.
4701 * Check if all labels are used in the current function.
4703 static void check_labels(void)
4705 for (const goto_statement_t *goto_statement = goto_first;
4706 goto_statement != NULL;
4707 goto_statement = goto_statement->next) {
4708 declaration_t *label = goto_statement->label;
4711 if (label->source_position.input_name == NULL) {
4712 print_in_function();
4713 errorf(&goto_statement->base.source_position,
4714 "label '%Y' used but not defined", label->symbol);
4717 goto_first = goto_last = NULL;
4719 if (warning.unused_label) {
4720 for (const label_statement_t *label_statement = label_first;
4721 label_statement != NULL;
4722 label_statement = label_statement->next) {
4723 const declaration_t *label = label_statement->label;
4725 if (! label->used) {
4726 print_in_function();
4727 warningf(&label_statement->base.source_position,
4728 "label '%Y' defined but not used", label->symbol);
4732 label_first = label_last = NULL;
4736 * Check declarations of current_function for unused entities.
4738 static void check_declarations(void)
4740 if (warning.unused_parameter) {
4741 const scope_t *scope = ¤t_function->scope;
4743 if (is_sym_main(current_function->symbol)) {
4744 /* do not issue unused warnings for main */
4747 const declaration_t *parameter = scope->declarations;
4748 for (; parameter != NULL; parameter = parameter->next) {
4749 if (! parameter->used) {
4750 print_in_function();
4751 warningf(¶meter->source_position,
4752 "unused parameter '%Y'", parameter->symbol);
4756 if (warning.unused_variable) {
4760 static int determine_truth(expression_t const* const cond)
4763 !is_constant_expression(cond) ? 0 :
4764 fold_constant(cond) != 0 ? 1 :
4768 static bool noreturn_candidate;
4770 static void check_reachable(statement_t *const stmt)
4772 if (stmt->base.reachable)
4774 if (stmt->kind != STATEMENT_DO_WHILE)
4775 stmt->base.reachable = true;
4777 statement_t *last = stmt;
4779 switch (stmt->kind) {
4780 case STATEMENT_INVALID:
4781 case STATEMENT_EMPTY:
4782 case STATEMENT_DECLARATION:
4784 next = stmt->base.next;
4787 case STATEMENT_COMPOUND:
4788 next = stmt->compound.statements;
4791 case STATEMENT_RETURN:
4792 noreturn_candidate = false;
4795 case STATEMENT_IF: {
4796 if_statement_t const* const ifs = &stmt->ifs;
4797 int const val = determine_truth(ifs->condition);
4800 check_reachable(ifs->true_statement);
4805 if (ifs->false_statement != NULL) {
4806 check_reachable(ifs->false_statement);
4810 next = stmt->base.next;
4814 case STATEMENT_SWITCH: {
4815 switch_statement_t const *const switchs = &stmt->switchs;
4816 expression_t const *const expr = switchs->expression;
4818 if (is_constant_expression(expr)) {
4819 long const val = fold_constant(expr);
4820 case_label_statement_t * defaults = NULL;
4821 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
4822 if (i->expression == NULL) {
4827 if (i->first_case <= val && val <= i->last_case) {
4828 check_reachable((statement_t*)i);
4833 if (defaults != NULL) {
4834 check_reachable((statement_t*)defaults);
4838 bool has_default = false;
4839 for (case_label_statement_t *i = switchs->first_case; i != NULL; i = i->next) {
4840 if (i->expression == NULL)
4843 check_reachable((statement_t*)i);
4850 next = stmt->base.next;
4854 case STATEMENT_EXPRESSION: {
4855 /* Check for noreturn function call */
4856 expression_t const *const expr = stmt->expression.expression;
4857 if (expr->kind == EXPR_CALL) {
4858 expression_t const *const func = expr->call.function;
4859 if (func->kind == EXPR_REFERENCE) {
4860 declaration_t const *const decl = func->reference.declaration;
4861 if (decl != NULL && decl->modifiers & DM_NORETURN) {
4867 next = stmt->base.next;
4871 case STATEMENT_CONTINUE: {
4872 statement_t *parent = stmt;
4874 parent = parent->base.parent;
4875 if (parent == NULL) /* continue not within loop */
4879 switch (parent->kind) {
4880 case STATEMENT_WHILE: goto continue_while;
4881 case STATEMENT_DO_WHILE: goto continue_do_while;
4882 case STATEMENT_FOR: goto continue_for;
4889 case STATEMENT_BREAK: {
4890 statement_t *parent = stmt;
4892 parent = parent->base.parent;
4893 if (parent == NULL) /* break not within loop/switch */
4896 switch (parent->kind) {
4897 case STATEMENT_SWITCH:
4898 case STATEMENT_WHILE:
4899 case STATEMENT_DO_WHILE:
4902 next = parent->base.next;
4903 goto found_break_parent;
4912 case STATEMENT_GOTO:
4913 next = stmt->gotos.label->init.statement;
4914 if (next == NULL) /* missing label */
4918 case STATEMENT_LABEL:
4919 next = stmt->label.statement;
4922 case STATEMENT_CASE_LABEL:
4923 next = stmt->case_label.statement;
4926 case STATEMENT_WHILE: {
4927 while_statement_t const *const whiles = &stmt->whiles;
4928 int const val = determine_truth(whiles->condition);
4931 check_reachable(whiles->body);
4936 next = stmt->base.next;
4940 case STATEMENT_DO_WHILE:
4941 next = stmt->do_while.body;
4944 case STATEMENT_FOR: {
4945 for_statement_t *const fors = &stmt->fors;
4947 if (fors->condition_reachable)
4949 fors->condition_reachable = true;
4951 expression_t const *const cond = fors->condition;
4953 cond == NULL ? 1 : determine_truth(cond);
4956 check_reachable(fors->body);
4961 next = stmt->base.next;
4965 case STATEMENT_MS_TRY:
4966 case STATEMENT_LEAVE:
4967 panic("unimplemented");
4970 while (next == NULL) {
4971 next = last->base.parent;
4973 noreturn_candidate = false;
4975 type_t *const type = current_function->type;
4976 assert(is_type_function(type));
4977 type_t *const ret = skip_typeref(type->function.return_type);
4978 if (warning.return_type &&
4979 !is_type_atomic(ret, ATOMIC_TYPE_VOID) &&
4980 is_type_valid(ret) &&
4981 !is_sym_main(current_function->symbol)) {
4982 warningf(&stmt->base.source_position,
4983 "control reaches end of non-void function");
4988 switch (next->kind) {
4989 case STATEMENT_INVALID:
4990 case STATEMENT_EMPTY:
4991 case STATEMENT_DECLARATION:
4992 case STATEMENT_EXPRESSION:
4994 case STATEMENT_RETURN:
4995 case STATEMENT_CONTINUE:
4996 case STATEMENT_BREAK:
4997 case STATEMENT_GOTO:
4998 case STATEMENT_LEAVE:
4999 panic("invalid control flow in function");
5001 case STATEMENT_COMPOUND:
5003 case STATEMENT_SWITCH:
5004 case STATEMENT_LABEL:
5005 case STATEMENT_CASE_LABEL:
5007 next = next->base.next;
5010 case STATEMENT_WHILE: {
5012 if (next->base.reachable)
5014 next->base.reachable = true;
5016 while_statement_t const *const whiles = &next->whiles;
5017 int const val = determine_truth(whiles->condition);
5020 check_reachable(whiles->body);
5026 next = next->base.next;
5030 case STATEMENT_DO_WHILE: {
5032 if (next->base.reachable)
5034 next->base.reachable = true;
5036 do_while_statement_t const *const dw = &next->do_while;
5037 int const val = determine_truth(dw->condition);
5040 check_reachable(dw->body);
5046 next = next->base.next;
5050 case STATEMENT_FOR: {
5052 for_statement_t *const fors = &next->fors;
5054 fors->step_reachable = true;
5056 if (fors->condition_reachable)
5058 fors->condition_reachable = true;
5060 expression_t const *const cond = fors->condition;
5062 cond == NULL ? 1 : determine_truth(cond);
5065 check_reachable(fors->body);
5071 next = next->base.next;
5075 case STATEMENT_MS_TRY:
5076 panic("unimplemented");
5081 next = stmt->base.parent;
5083 warningf(&stmt->base.source_position,
5084 "control reaches end of non-void function");
5088 check_reachable(next);
5091 static void check_unreachable(statement_t const* const stmt)
5093 if (!stmt->base.reachable &&
5094 stmt->kind != STATEMENT_DO_WHILE &&
5095 stmt->kind != STATEMENT_FOR &&
5096 (stmt->kind != STATEMENT_COMPOUND || stmt->compound.statements == NULL)) {
5097 warningf(&stmt->base.source_position, "statement is unreachable");
5100 switch (stmt->kind) {
5101 case STATEMENT_INVALID:
5102 case STATEMENT_EMPTY:
5103 case STATEMENT_RETURN:
5104 case STATEMENT_DECLARATION:
5105 case STATEMENT_EXPRESSION:
5106 case STATEMENT_CONTINUE:
5107 case STATEMENT_BREAK:
5108 case STATEMENT_GOTO:
5110 case STATEMENT_LEAVE:
5113 case STATEMENT_COMPOUND:
5114 if (stmt->compound.statements)
5115 check_unreachable(stmt->compound.statements);
5119 check_unreachable(stmt->ifs.true_statement);
5120 if (stmt->ifs.false_statement != NULL)
5121 check_unreachable(stmt->ifs.false_statement);
5124 case STATEMENT_SWITCH:
5125 check_unreachable(stmt->switchs.body);
5128 case STATEMENT_LABEL:
5129 check_unreachable(stmt->label.statement);
5132 case STATEMENT_CASE_LABEL:
5133 check_unreachable(stmt->case_label.statement);
5136 case STATEMENT_WHILE:
5137 check_unreachable(stmt->whiles.body);
5140 case STATEMENT_DO_WHILE:
5141 check_unreachable(stmt->do_while.body);
5142 if (!stmt->base.reachable) {
5143 expression_t const *const cond = stmt->do_while.condition;
5144 if (determine_truth(cond) >= 0) {
5145 warningf(&cond->base.source_position,
5146 "condition of do-while-loop is unreachable");
5151 case STATEMENT_FOR: {
5152 for_statement_t const* const fors = &stmt->fors;
5154 // if init and step are unreachable, cond is unreachable, too
5155 if (!stmt->base.reachable && !fors->step_reachable) {
5156 warningf(&stmt->base.source_position, "statement is unreachable");
5158 if (!stmt->base.reachable && fors->initialisation != NULL) {
5159 warningf(&fors->initialisation->base.source_position,
5160 "initialisation of for-statement is unreachable");
5163 if (!fors->condition_reachable && fors->condition != NULL) {
5164 warningf(&fors->condition->base.source_position,
5165 "condition of for-statement is unreachable");
5168 if (!fors->step_reachable && fors->step != NULL) {
5169 warningf(&fors->step->base.source_position,
5170 "step of for-statement is unreachable");
5174 check_unreachable(stmt->fors.body);
5178 case STATEMENT_MS_TRY:
5179 panic("unimplemented");
5182 if (stmt->base.next)
5183 check_unreachable(stmt->base.next);
5186 static void parse_external_declaration(void)
5188 /* function-definitions and declarations both start with declaration
5190 declaration_specifiers_t specifiers;
5191 memset(&specifiers, 0, sizeof(specifiers));
5193 add_anchor_token(';');
5194 parse_declaration_specifiers(&specifiers);
5195 rem_anchor_token(';');
5197 /* must be a declaration */
5198 if (token.type == ';') {
5199 parse_anonymous_declaration_rest(&specifiers, append_declaration);
5203 add_anchor_token(',');
5204 add_anchor_token('=');
5205 rem_anchor_token(';');
5207 /* declarator is common to both function-definitions and declarations */
5208 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
5210 rem_anchor_token(',');
5211 rem_anchor_token('=');
5212 rem_anchor_token(';');
5214 /* must be a declaration */
5215 switch (token.type) {
5218 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
5222 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
5226 /* must be a function definition */
5227 parse_kr_declaration_list(ndeclaration);
5229 if (token.type != '{') {
5230 parse_error_expected("while parsing function definition", '{', NULL);
5231 eat_until_matching_token(';');
5235 type_t *type = ndeclaration->type;
5237 /* note that we don't skip typerefs: the standard doesn't allow them here
5238 * (so we can't use is_type_function here) */
5239 if (type->kind != TYPE_FUNCTION) {
5240 if (is_type_valid(type)) {
5241 errorf(HERE, "declarator '%#T' has a body but is not a function type",
5242 type, ndeclaration->symbol);
5248 if (warning.aggregate_return &&
5249 is_type_compound(skip_typeref(type->function.return_type))) {
5250 warningf(HERE, "function '%Y' returns an aggregate",
5251 ndeclaration->symbol);
5254 /* § 6.7.5.3 (14) a function definition with () means no
5255 * parameters (and not unspecified parameters) */
5256 if (type->function.unspecified_parameters
5257 && type->function.parameters == NULL
5258 && !type->function.kr_style_parameters) {
5259 type_t *duplicate = duplicate_type(type);
5260 duplicate->function.unspecified_parameters = false;
5262 type = typehash_insert(duplicate);
5263 if (type != duplicate) {
5264 obstack_free(type_obst, duplicate);
5266 ndeclaration->type = type;
5269 declaration_t *const declaration = record_definition(ndeclaration);
5270 if (ndeclaration != declaration) {
5271 declaration->scope = ndeclaration->scope;
5273 type = skip_typeref(declaration->type);
5275 /* push function parameters and switch scope */
5276 int top = environment_top();
5277 scope_t *last_scope = scope;
5278 set_scope(&declaration->scope);
5280 declaration_t *parameter = declaration->scope.declarations;
5281 for( ; parameter != NULL; parameter = parameter->next) {
5282 if (parameter->parent_scope == &ndeclaration->scope) {
5283 parameter->parent_scope = scope;
5285 assert(parameter->parent_scope == NULL
5286 || parameter->parent_scope == scope);
5287 parameter->parent_scope = scope;
5288 if (parameter->symbol == NULL) {
5289 errorf(¶meter->source_position, "parameter name omitted");
5292 environment_push(parameter);
5295 if (declaration->init.statement != NULL) {
5296 parser_error_multiple_definition(declaration, HERE);
5299 /* parse function body */
5300 int label_stack_top = label_top();
5301 declaration_t *old_current_function = current_function;
5302 current_function = declaration;
5303 current_parent = NULL;
5305 statement_t *const body = parse_compound_statement(false);
5306 declaration->init.statement = body;
5309 check_declarations();
5310 if (warning.return_type ||
5311 warning.unreachable_code ||
5312 (warning.missing_noreturn && !(declaration->modifiers & DM_NORETURN))) {
5313 noreturn_candidate = true;
5314 check_reachable(body);
5315 if (warning.unreachable_code)
5316 check_unreachable(body);
5317 if (warning.missing_noreturn &&
5318 noreturn_candidate &&
5319 !(declaration->modifiers & DM_NORETURN)) {
5320 warningf(&body->base.source_position,
5321 "function '%#T' is candidate for attribute 'noreturn'",
5322 type, declaration->symbol);
5326 assert(current_parent == NULL);
5327 assert(current_function == declaration);
5328 current_function = old_current_function;
5329 label_pop_to(label_stack_top);
5332 assert(scope == &declaration->scope);
5333 set_scope(last_scope);
5334 environment_pop_to(top);
5337 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
5338 source_position_t *source_position)
5340 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
5342 type->bitfield.base_type = base_type;
5343 type->bitfield.size = size;
5348 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
5351 declaration_t *iter = compound_declaration->scope.declarations;
5352 for( ; iter != NULL; iter = iter->next) {
5353 if (iter->namespc != NAMESPACE_NORMAL)
5356 if (iter->symbol == NULL) {
5357 type_t *type = skip_typeref(iter->type);
5358 if (is_type_compound(type)) {
5359 declaration_t *result
5360 = find_compound_entry(type->compound.declaration, symbol);
5367 if (iter->symbol == symbol) {
5375 static void parse_compound_declarators(declaration_t *struct_declaration,
5376 const declaration_specifiers_t *specifiers)
5378 declaration_t *last_declaration = struct_declaration->scope.declarations;
5379 if (last_declaration != NULL) {
5380 while(last_declaration->next != NULL) {
5381 last_declaration = last_declaration->next;
5386 declaration_t *declaration;
5388 if (token.type == ':') {
5389 source_position_t source_position = *HERE;
5392 type_t *base_type = specifiers->type;
5393 expression_t *size = parse_constant_expression();
5395 if (!is_type_integer(skip_typeref(base_type))) {
5396 errorf(HERE, "bitfield base type '%T' is not an integer type",
5400 type_t *type = make_bitfield_type(base_type, size, &source_position);
5402 declaration = allocate_declaration_zero();
5403 declaration->namespc = NAMESPACE_NORMAL;
5404 declaration->declared_storage_class = STORAGE_CLASS_NONE;
5405 declaration->storage_class = STORAGE_CLASS_NONE;
5406 declaration->source_position = source_position;
5407 declaration->modifiers = specifiers->modifiers;
5408 declaration->type = type;
5410 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
5412 type_t *orig_type = declaration->type;
5413 type_t *type = skip_typeref(orig_type);
5415 if (token.type == ':') {
5416 source_position_t source_position = *HERE;
5418 expression_t *size = parse_constant_expression();
5420 if (!is_type_integer(type)) {
5421 errorf(HERE, "bitfield base type '%T' is not an "
5422 "integer type", orig_type);
5425 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
5426 declaration->type = bitfield_type;
5428 /* TODO we ignore arrays for now... what is missing is a check
5429 * that they're at the end of the struct */
5430 if (is_type_incomplete(type) && !is_type_array(type)) {
5432 "compound member '%Y' has incomplete type '%T'",
5433 declaration->symbol, orig_type);
5434 } else if (is_type_function(type)) {
5435 errorf(HERE, "compound member '%Y' must not have function "
5436 "type '%T'", declaration->symbol, orig_type);
5441 /* make sure we don't define a symbol multiple times */
5442 symbol_t *symbol = declaration->symbol;
5443 if (symbol != NULL) {
5444 declaration_t *prev_decl
5445 = find_compound_entry(struct_declaration, symbol);
5447 if (prev_decl != NULL) {
5448 assert(prev_decl->symbol == symbol);
5449 errorf(&declaration->source_position,
5450 "multiple declarations of symbol '%Y' (declared %P)",
5451 symbol, &prev_decl->source_position);
5455 /* append declaration */
5456 if (last_declaration != NULL) {
5457 last_declaration->next = declaration;
5459 struct_declaration->scope.declarations = declaration;
5461 last_declaration = declaration;
5463 if (token.type != ',')
5473 static void parse_compound_type_entries(declaration_t *compound_declaration)
5476 add_anchor_token('}');
5478 while(token.type != '}' && token.type != T_EOF) {
5479 declaration_specifiers_t specifiers;
5480 memset(&specifiers, 0, sizeof(specifiers));
5481 parse_declaration_specifiers(&specifiers);
5483 parse_compound_declarators(compound_declaration, &specifiers);
5485 rem_anchor_token('}');
5487 if (token.type == T_EOF) {
5488 errorf(HERE, "EOF while parsing struct");
5493 static type_t *parse_typename(void)
5495 declaration_specifiers_t specifiers;
5496 memset(&specifiers, 0, sizeof(specifiers));
5497 parse_declaration_specifiers(&specifiers);
5498 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
5499 /* TODO: improve error message, user does probably not know what a
5500 * storage class is...
5502 errorf(HERE, "typename may not have a storage class");
5505 type_t *result = parse_abstract_declarator(specifiers.type);
5513 typedef expression_t* (*parse_expression_function) (unsigned precedence);
5514 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
5515 expression_t *left);
5517 typedef struct expression_parser_function_t expression_parser_function_t;
5518 struct expression_parser_function_t {
5519 unsigned precedence;
5520 parse_expression_function parser;
5521 unsigned infix_precedence;
5522 parse_expression_infix_function infix_parser;
5525 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
5528 * Prints an error message if an expression was expected but not read
5530 static expression_t *expected_expression_error(void)
5532 /* skip the error message if the error token was read */
5533 if (token.type != T_ERROR) {
5534 errorf(HERE, "expected expression, got token '%K'", &token);
5538 return create_invalid_expression();
5542 * Parse a string constant.
5544 static expression_t *parse_string_const(void)
5547 if (token.type == T_STRING_LITERAL) {
5548 string_t res = token.v.string;
5550 while (token.type == T_STRING_LITERAL) {
5551 res = concat_strings(&res, &token.v.string);
5554 if (token.type != T_WIDE_STRING_LITERAL) {
5555 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
5556 /* note: that we use type_char_ptr here, which is already the
5557 * automatic converted type. revert_automatic_type_conversion
5558 * will construct the array type */
5559 cnst->base.type = warning.write_strings ? type_const_char_ptr : type_char_ptr;
5560 cnst->string.value = res;
5564 wres = concat_string_wide_string(&res, &token.v.wide_string);
5566 wres = token.v.wide_string;
5571 switch (token.type) {
5572 case T_WIDE_STRING_LITERAL:
5573 wres = concat_wide_strings(&wres, &token.v.wide_string);
5576 case T_STRING_LITERAL:
5577 wres = concat_wide_string_string(&wres, &token.v.string);
5581 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
5582 cnst->base.type = warning.write_strings ? type_const_wchar_t_ptr : type_wchar_t_ptr;
5583 cnst->wide_string.value = wres;
5592 * Parse an integer constant.
5594 static expression_t *parse_int_const(void)
5596 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5597 cnst->base.source_position = *HERE;
5598 cnst->base.type = token.datatype;
5599 cnst->conste.v.int_value = token.v.intvalue;
5607 * Parse a character constant.
5609 static expression_t *parse_character_constant(void)
5611 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
5613 cnst->base.source_position = *HERE;
5614 cnst->base.type = token.datatype;
5615 cnst->conste.v.character = token.v.string;
5617 if (cnst->conste.v.character.size != 1) {
5618 if (warning.multichar && (c_mode & _GNUC)) {
5620 warningf(HERE, "multi-character character constant");
5622 errorf(HERE, "more than 1 characters in character constant");
5631 * Parse a wide character constant.
5633 static expression_t *parse_wide_character_constant(void)
5635 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5637 cnst->base.source_position = *HERE;
5638 cnst->base.type = token.datatype;
5639 cnst->conste.v.wide_character = token.v.wide_string;
5641 if (cnst->conste.v.wide_character.size != 1) {
5642 if (warning.multichar && (c_mode & _GNUC)) {
5644 warningf(HERE, "multi-character character constant");
5646 errorf(HERE, "more than 1 characters in character constant");
5655 * Parse a float constant.
5657 static expression_t *parse_float_const(void)
5659 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5660 cnst->base.type = token.datatype;
5661 cnst->conste.v.float_value = token.v.floatvalue;
5668 static declaration_t *create_implicit_function(symbol_t *symbol,
5669 const source_position_t *source_position)
5671 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5672 ntype->function.return_type = type_int;
5673 ntype->function.unspecified_parameters = true;
5675 type_t *type = typehash_insert(ntype);
5676 if (type != ntype) {
5680 declaration_t *const declaration = allocate_declaration_zero();
5681 declaration->storage_class = STORAGE_CLASS_EXTERN;
5682 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5683 declaration->type = type;
5684 declaration->symbol = symbol;
5685 declaration->source_position = *source_position;
5686 declaration->implicit = true;
5688 bool strict_prototypes_old = warning.strict_prototypes;
5689 warning.strict_prototypes = false;
5690 record_declaration(declaration);
5691 warning.strict_prototypes = strict_prototypes_old;
5697 * Creates a return_type (func)(argument_type) function type if not
5700 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5701 type_t *argument_type2)
5703 function_parameter_t *parameter2
5704 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5705 memset(parameter2, 0, sizeof(parameter2[0]));
5706 parameter2->type = argument_type2;
5708 function_parameter_t *parameter1
5709 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5710 memset(parameter1, 0, sizeof(parameter1[0]));
5711 parameter1->type = argument_type1;
5712 parameter1->next = parameter2;
5714 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5715 type->function.return_type = return_type;
5716 type->function.parameters = parameter1;
5718 type_t *result = typehash_insert(type);
5719 if (result != type) {
5727 * Creates a return_type (func)(argument_type) function type if not
5730 * @param return_type the return type
5731 * @param argument_type the argument type
5733 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5735 function_parameter_t *parameter
5736 = obstack_alloc(type_obst, sizeof(parameter[0]));
5737 memset(parameter, 0, sizeof(parameter[0]));
5738 parameter->type = argument_type;
5740 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5741 type->function.return_type = return_type;
5742 type->function.parameters = parameter;
5744 type_t *result = typehash_insert(type);
5745 if (result != type) {
5752 static type_t *make_function_0_type(type_t *return_type)
5754 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5755 type->function.return_type = return_type;
5756 type->function.parameters = NULL;
5758 type_t *result = typehash_insert(type);
5759 if (result != type) {
5767 * Creates a function type for some function like builtins.
5769 * @param symbol the symbol describing the builtin
5771 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5773 switch(symbol->ID) {
5774 case T___builtin_alloca:
5775 return make_function_1_type(type_void_ptr, type_size_t);
5776 case T___builtin_huge_val:
5777 return make_function_0_type(type_double);
5778 case T___builtin_nan:
5779 return make_function_1_type(type_double, type_char_ptr);
5780 case T___builtin_nanf:
5781 return make_function_1_type(type_float, type_char_ptr);
5782 case T___builtin_nand:
5783 return make_function_1_type(type_long_double, type_char_ptr);
5784 case T___builtin_va_end:
5785 return make_function_1_type(type_void, type_valist);
5786 case T___builtin_expect:
5787 return make_function_2_type(type_long, type_long, type_long);
5789 internal_errorf(HERE, "not implemented builtin symbol found");
5794 * Performs automatic type cast as described in § 6.3.2.1.
5796 * @param orig_type the original type
5798 static type_t *automatic_type_conversion(type_t *orig_type)
5800 type_t *type = skip_typeref(orig_type);
5801 if (is_type_array(type)) {
5802 array_type_t *array_type = &type->array;
5803 type_t *element_type = array_type->element_type;
5804 unsigned qualifiers = array_type->base.qualifiers;
5806 return make_pointer_type(element_type, qualifiers);
5809 if (is_type_function(type)) {
5810 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5817 * reverts the automatic casts of array to pointer types and function
5818 * to function-pointer types as defined § 6.3.2.1
5820 type_t *revert_automatic_type_conversion(const expression_t *expression)
5822 switch (expression->kind) {
5823 case EXPR_REFERENCE: return expression->reference.declaration->type;
5824 case EXPR_SELECT: return expression->select.compound_entry->type;
5826 case EXPR_UNARY_DEREFERENCE: {
5827 const expression_t *const value = expression->unary.value;
5828 type_t *const type = skip_typeref(value->base.type);
5829 assert(is_type_pointer(type));
5830 return type->pointer.points_to;
5833 case EXPR_BUILTIN_SYMBOL:
5834 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5836 case EXPR_ARRAY_ACCESS: {
5837 const expression_t *array_ref = expression->array_access.array_ref;
5838 type_t *type_left = skip_typeref(array_ref->base.type);
5839 if (!is_type_valid(type_left))
5841 assert(is_type_pointer(type_left));
5842 return type_left->pointer.points_to;
5845 case EXPR_STRING_LITERAL: {
5846 size_t size = expression->string.value.size;
5847 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5850 case EXPR_WIDE_STRING_LITERAL: {
5851 size_t size = expression->wide_string.value.size;
5852 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5855 case EXPR_COMPOUND_LITERAL:
5856 return expression->compound_literal.type;
5861 return expression->base.type;
5864 static expression_t *parse_reference(void)
5866 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5868 reference_expression_t *ref = &expression->reference;
5869 symbol_t *const symbol = token.v.symbol;
5871 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5873 source_position_t source_position = token.source_position;
5876 if (declaration == NULL) {
5877 if (token.type == '(') {
5878 /* an implicitly declared function */
5880 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5881 } else if (warning.implicit_function_declaration) {
5882 warningf(HERE, "implicit declaration of function '%Y'",
5886 declaration = create_implicit_function(symbol,
5889 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5890 declaration = create_error_declaration(symbol, STORAGE_CLASS_NONE);
5894 type_t *type = declaration->type;
5896 /* we always do the auto-type conversions; the & and sizeof parser contains
5897 * code to revert this! */
5898 type = automatic_type_conversion(type);
5900 ref->declaration = declaration;
5901 ref->base.type = type;
5903 /* this declaration is used */
5904 declaration->used = true;
5906 /* check for deprecated functions */
5907 if (warning.deprecated_declarations &&
5908 declaration->modifiers & DM_DEPRECATED) {
5909 char const *const prefix = is_type_function(declaration->type) ?
5910 "function" : "variable";
5912 if (declaration->deprecated_string != NULL) {
5913 warningf(&source_position,
5914 "%s '%Y' is deprecated (declared %P): \"%s\"", prefix,
5915 declaration->symbol, &declaration->source_position,
5916 declaration->deprecated_string);
5918 warningf(&source_position,
5919 "%s '%Y' is deprecated (declared %P)", prefix,
5920 declaration->symbol, &declaration->source_position);
5927 static bool semantic_cast(expression_t *cast)
5929 expression_t *expression = cast->unary.value;
5930 type_t *orig_dest_type = cast->base.type;
5931 type_t *orig_type_right = expression->base.type;
5932 type_t const *dst_type = skip_typeref(orig_dest_type);
5933 type_t const *src_type = skip_typeref(orig_type_right);
5934 source_position_t const *pos = &cast->base.source_position;
5936 /* §6.5.4 A (void) cast is explicitly permitted, more for documentation than for utility. */
5937 if (dst_type == type_void)
5940 /* only integer and pointer can be casted to pointer */
5941 if (is_type_pointer(dst_type) &&
5942 !is_type_pointer(src_type) &&
5943 !is_type_integer(src_type) &&
5944 is_type_valid(src_type)) {
5945 errorf(pos, "cannot convert type '%T' to a pointer type", orig_type_right);
5949 if (!is_type_scalar(dst_type) && is_type_valid(dst_type)) {
5950 errorf(pos, "conversion to non-scalar type '%T' requested", orig_dest_type);
5954 if (!is_type_scalar(src_type) && is_type_valid(src_type)) {
5955 errorf(pos, "conversion from non-scalar type '%T' requested", orig_type_right);
5959 if (warning.cast_qual &&
5960 is_type_pointer(src_type) &&
5961 is_type_pointer(dst_type)) {
5962 type_t *src = skip_typeref(src_type->pointer.points_to);
5963 type_t *dst = skip_typeref(dst_type->pointer.points_to);
5964 unsigned missing_qualifiers =
5965 src->base.qualifiers & ~dst->base.qualifiers;
5966 if (missing_qualifiers != 0) {
5968 "cast discards qualifiers '%Q' in pointer target type of '%T'",
5969 missing_qualifiers, orig_type_right);
5975 static expression_t *parse_compound_literal(type_t *type)
5977 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5979 parse_initializer_env_t env;
5981 env.declaration = NULL;
5982 env.must_be_constant = false;
5983 initializer_t *initializer = parse_initializer(&env);
5986 expression->compound_literal.initializer = initializer;
5987 expression->compound_literal.type = type;
5988 expression->base.type = automatic_type_conversion(type);
5994 * Parse a cast expression.
5996 static expression_t *parse_cast(void)
5998 source_position_t source_position = token.source_position;
6000 type_t *type = parse_typename();
6002 /* matching add_anchor_token() is at call site */
6003 rem_anchor_token(')');
6006 if (token.type == '{') {
6007 return parse_compound_literal(type);
6010 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
6011 cast->base.source_position = source_position;
6013 expression_t *value = parse_sub_expression(20);
6014 cast->base.type = type;
6015 cast->unary.value = value;
6017 if (! semantic_cast(cast)) {
6018 /* TODO: record the error in the AST. else it is impossible to detect it */
6023 return create_invalid_expression();
6027 * Parse a statement expression.
6029 static expression_t *parse_statement_expression(void)
6031 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
6033 statement_t *statement = parse_compound_statement(true);
6034 expression->statement.statement = statement;
6035 expression->base.source_position = statement->base.source_position;
6037 /* find last statement and use its type */
6038 type_t *type = type_void;
6039 const statement_t *stmt = statement->compound.statements;
6041 while (stmt->base.next != NULL)
6042 stmt = stmt->base.next;
6044 if (stmt->kind == STATEMENT_EXPRESSION) {
6045 type = stmt->expression.expression->base.type;
6048 warningf(&expression->base.source_position, "empty statement expression ({})");
6050 expression->base.type = type;
6056 return create_invalid_expression();
6060 * Parse a parenthesized expression.
6062 static expression_t *parse_parenthesized_expression(void)
6065 add_anchor_token(')');
6067 switch(token.type) {
6069 /* gcc extension: a statement expression */
6070 return parse_statement_expression();
6074 return parse_cast();
6076 if (is_typedef_symbol(token.v.symbol)) {
6077 return parse_cast();
6081 expression_t *result = parse_expression();
6082 rem_anchor_token(')');
6087 return create_invalid_expression();
6090 static expression_t *parse_function_keyword(void)
6095 if (current_function == NULL) {
6096 errorf(HERE, "'__func__' used outside of a function");
6099 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6100 expression->base.type = type_char_ptr;
6101 expression->funcname.kind = FUNCNAME_FUNCTION;
6106 static expression_t *parse_pretty_function_keyword(void)
6108 eat(T___PRETTY_FUNCTION__);
6110 if (current_function == NULL) {
6111 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
6114 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6115 expression->base.type = type_char_ptr;
6116 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
6121 static expression_t *parse_funcsig_keyword(void)
6125 if (current_function == NULL) {
6126 errorf(HERE, "'__FUNCSIG__' used outside of a function");
6129 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6130 expression->base.type = type_char_ptr;
6131 expression->funcname.kind = FUNCNAME_FUNCSIG;
6136 static expression_t *parse_funcdname_keyword(void)
6138 eat(T___FUNCDNAME__);
6140 if (current_function == NULL) {
6141 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
6144 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
6145 expression->base.type = type_char_ptr;
6146 expression->funcname.kind = FUNCNAME_FUNCDNAME;
6151 static designator_t *parse_designator(void)
6153 designator_t *result = allocate_ast_zero(sizeof(result[0]));
6154 result->source_position = *HERE;
6156 if (token.type != T_IDENTIFIER) {
6157 parse_error_expected("while parsing member designator",
6158 T_IDENTIFIER, NULL);
6161 result->symbol = token.v.symbol;
6164 designator_t *last_designator = result;
6166 if (token.type == '.') {
6168 if (token.type != T_IDENTIFIER) {
6169 parse_error_expected("while parsing member designator",
6170 T_IDENTIFIER, NULL);
6173 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6174 designator->source_position = *HERE;
6175 designator->symbol = token.v.symbol;
6178 last_designator->next = designator;
6179 last_designator = designator;
6182 if (token.type == '[') {
6184 add_anchor_token(']');
6185 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
6186 designator->source_position = *HERE;
6187 designator->array_index = parse_expression();
6188 rem_anchor_token(']');
6190 if (designator->array_index == NULL) {
6194 last_designator->next = designator;
6195 last_designator = designator;
6207 * Parse the __builtin_offsetof() expression.
6209 static expression_t *parse_offsetof(void)
6211 eat(T___builtin_offsetof);
6213 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
6214 expression->base.type = type_size_t;
6217 add_anchor_token(',');
6218 type_t *type = parse_typename();
6219 rem_anchor_token(',');
6221 add_anchor_token(')');
6222 designator_t *designator = parse_designator();
6223 rem_anchor_token(')');
6226 expression->offsetofe.type = type;
6227 expression->offsetofe.designator = designator;
6230 memset(&path, 0, sizeof(path));
6231 path.top_type = type;
6232 path.path = NEW_ARR_F(type_path_entry_t, 0);
6234 descend_into_subtype(&path);
6236 if (!walk_designator(&path, designator, true)) {
6237 return create_invalid_expression();
6240 DEL_ARR_F(path.path);
6244 return create_invalid_expression();
6248 * Parses a _builtin_va_start() expression.
6250 static expression_t *parse_va_start(void)
6252 eat(T___builtin_va_start);
6254 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
6257 add_anchor_token(',');
6258 expression->va_starte.ap = parse_assignment_expression();
6259 rem_anchor_token(',');
6261 expression_t *const expr = parse_assignment_expression();
6262 if (expr->kind == EXPR_REFERENCE) {
6263 declaration_t *const decl = expr->reference.declaration;
6265 return create_invalid_expression();
6266 if (decl->parent_scope == ¤t_function->scope &&
6267 decl->next == NULL) {
6268 expression->va_starte.parameter = decl;
6273 errorf(&expr->base.source_position,
6274 "second argument of 'va_start' must be last parameter of the current function");
6276 return create_invalid_expression();
6280 * Parses a _builtin_va_arg() expression.
6282 static expression_t *parse_va_arg(void)
6284 eat(T___builtin_va_arg);
6286 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
6289 expression->va_arge.ap = parse_assignment_expression();
6291 expression->base.type = parse_typename();
6296 return create_invalid_expression();
6299 static expression_t *parse_builtin_symbol(void)
6301 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
6303 symbol_t *symbol = token.v.symbol;
6305 expression->builtin_symbol.symbol = symbol;
6308 type_t *type = get_builtin_symbol_type(symbol);
6309 type = automatic_type_conversion(type);
6311 expression->base.type = type;
6316 * Parses a __builtin_constant() expression.
6318 static expression_t *parse_builtin_constant(void)
6320 eat(T___builtin_constant_p);
6322 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
6325 add_anchor_token(')');
6326 expression->builtin_constant.value = parse_assignment_expression();
6327 rem_anchor_token(')');
6329 expression->base.type = type_int;
6333 return create_invalid_expression();
6337 * Parses a __builtin_prefetch() expression.
6339 static expression_t *parse_builtin_prefetch(void)
6341 eat(T___builtin_prefetch);
6343 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
6346 add_anchor_token(')');
6347 expression->builtin_prefetch.adr = parse_assignment_expression();
6348 if (token.type == ',') {
6350 expression->builtin_prefetch.rw = parse_assignment_expression();
6352 if (token.type == ',') {
6354 expression->builtin_prefetch.locality = parse_assignment_expression();
6356 rem_anchor_token(')');
6358 expression->base.type = type_void;
6362 return create_invalid_expression();
6366 * Parses a __builtin_is_*() compare expression.
6368 static expression_t *parse_compare_builtin(void)
6370 expression_t *expression;
6372 switch(token.type) {
6373 case T___builtin_isgreater:
6374 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
6376 case T___builtin_isgreaterequal:
6377 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
6379 case T___builtin_isless:
6380 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
6382 case T___builtin_islessequal:
6383 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
6385 case T___builtin_islessgreater:
6386 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
6388 case T___builtin_isunordered:
6389 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
6392 internal_errorf(HERE, "invalid compare builtin found");
6395 expression->base.source_position = *HERE;
6399 expression->binary.left = parse_assignment_expression();
6401 expression->binary.right = parse_assignment_expression();
6404 type_t *const orig_type_left = expression->binary.left->base.type;
6405 type_t *const orig_type_right = expression->binary.right->base.type;
6407 type_t *const type_left = skip_typeref(orig_type_left);
6408 type_t *const type_right = skip_typeref(orig_type_right);
6409 if (!is_type_float(type_left) && !is_type_float(type_right)) {
6410 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6411 type_error_incompatible("invalid operands in comparison",
6412 &expression->base.source_position, orig_type_left, orig_type_right);
6415 semantic_comparison(&expression->binary);
6420 return create_invalid_expression();
6425 * Parses a __builtin_expect() expression.
6427 static expression_t *parse_builtin_expect(void)
6429 eat(T___builtin_expect);
6431 expression_t *expression
6432 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
6435 expression->binary.left = parse_assignment_expression();
6437 expression->binary.right = parse_constant_expression();
6440 expression->base.type = expression->binary.left->base.type;
6444 return create_invalid_expression();
6449 * Parses a MS assume() expression.
6451 static expression_t *parse_assume(void)
6455 expression_t *expression
6456 = allocate_expression_zero(EXPR_UNARY_ASSUME);
6459 add_anchor_token(')');
6460 expression->unary.value = parse_assignment_expression();
6461 rem_anchor_token(')');
6464 expression->base.type = type_void;
6467 return create_invalid_expression();
6471 * Parse a microsoft __noop expression.
6473 static expression_t *parse_noop_expression(void)
6475 source_position_t source_position = *HERE;
6478 if (token.type == '(') {
6479 /* parse arguments */
6481 add_anchor_token(')');
6482 add_anchor_token(',');
6484 if (token.type != ')') {
6486 (void)parse_assignment_expression();
6487 if (token.type != ',')
6493 rem_anchor_token(',');
6494 rem_anchor_token(')');
6497 /* the result is a (int)0 */
6498 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
6499 cnst->base.source_position = source_position;
6500 cnst->base.type = type_int;
6501 cnst->conste.v.int_value = 0;
6502 cnst->conste.is_ms_noop = true;
6507 return create_invalid_expression();
6511 * Parses a primary expression.
6513 static expression_t *parse_primary_expression(void)
6515 switch (token.type) {
6516 case T_INTEGER: return parse_int_const();
6517 case T_CHARACTER_CONSTANT: return parse_character_constant();
6518 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
6519 case T_FLOATINGPOINT: return parse_float_const();
6520 case T_STRING_LITERAL:
6521 case T_WIDE_STRING_LITERAL: return parse_string_const();
6522 case T_IDENTIFIER: return parse_reference();
6523 case T___FUNCTION__:
6524 case T___func__: return parse_function_keyword();
6525 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
6526 case T___FUNCSIG__: return parse_funcsig_keyword();
6527 case T___FUNCDNAME__: return parse_funcdname_keyword();
6528 case T___builtin_offsetof: return parse_offsetof();
6529 case T___builtin_va_start: return parse_va_start();
6530 case T___builtin_va_arg: return parse_va_arg();
6531 case T___builtin_expect:
6532 case T___builtin_alloca:
6533 case T___builtin_nan:
6534 case T___builtin_nand:
6535 case T___builtin_nanf:
6536 case T___builtin_huge_val:
6537 case T___builtin_va_end: return parse_builtin_symbol();
6538 case T___builtin_isgreater:
6539 case T___builtin_isgreaterequal:
6540 case T___builtin_isless:
6541 case T___builtin_islessequal:
6542 case T___builtin_islessgreater:
6543 case T___builtin_isunordered: return parse_compare_builtin();
6544 case T___builtin_constant_p: return parse_builtin_constant();
6545 case T___builtin_prefetch: return parse_builtin_prefetch();
6546 case T__assume: return parse_assume();
6548 case '(': return parse_parenthesized_expression();
6549 case T___noop: return parse_noop_expression();
6552 errorf(HERE, "unexpected token %K, expected an expression", &token);
6553 return create_invalid_expression();
6557 * Check if the expression has the character type and issue a warning then.
6559 static void check_for_char_index_type(const expression_t *expression)
6561 type_t *const type = expression->base.type;
6562 const type_t *const base_type = skip_typeref(type);
6564 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
6565 warning.char_subscripts) {
6566 warningf(&expression->base.source_position,
6567 "array subscript has type '%T'", type);
6571 static expression_t *parse_array_expression(unsigned precedence,
6577 add_anchor_token(']');
6579 expression_t *inside = parse_expression();
6581 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
6583 array_access_expression_t *array_access = &expression->array_access;
6585 type_t *const orig_type_left = left->base.type;
6586 type_t *const orig_type_inside = inside->base.type;
6588 type_t *const type_left = skip_typeref(orig_type_left);
6589 type_t *const type_inside = skip_typeref(orig_type_inside);
6591 type_t *return_type;
6592 if (is_type_pointer(type_left)) {
6593 return_type = type_left->pointer.points_to;
6594 array_access->array_ref = left;
6595 array_access->index = inside;
6596 check_for_char_index_type(inside);
6597 } else if (is_type_pointer(type_inside)) {
6598 return_type = type_inside->pointer.points_to;
6599 array_access->array_ref = inside;
6600 array_access->index = left;
6601 array_access->flipped = true;
6602 check_for_char_index_type(left);
6604 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
6606 "array access on object with non-pointer types '%T', '%T'",
6607 orig_type_left, orig_type_inside);
6609 return_type = type_error_type;
6610 array_access->array_ref = create_invalid_expression();
6613 rem_anchor_token(']');
6614 if (token.type != ']') {
6615 parse_error_expected("Problem while parsing array access", ']', NULL);
6620 return_type = automatic_type_conversion(return_type);
6621 expression->base.type = return_type;
6626 static expression_t *parse_typeprop(expression_kind_t const kind,
6627 source_position_t const pos,
6628 unsigned const precedence)
6630 expression_t *tp_expression = allocate_expression_zero(kind);
6631 tp_expression->base.type = type_size_t;
6632 tp_expression->base.source_position = pos;
6634 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
6636 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
6638 add_anchor_token(')');
6639 type_t* const orig_type = parse_typename();
6640 tp_expression->typeprop.type = orig_type;
6642 type_t const* const type = skip_typeref(orig_type);
6643 char const* const wrong_type =
6644 is_type_incomplete(type) ? "incomplete" :
6645 type->kind == TYPE_FUNCTION ? "function designator" :
6646 type->kind == TYPE_BITFIELD ? "bitfield" :
6648 if (wrong_type != NULL) {
6649 errorf(&pos, "operand of %s expression must not be %s type '%T'",
6650 what, wrong_type, type);
6653 rem_anchor_token(')');
6656 expression_t *expression = parse_sub_expression(precedence);
6658 type_t* const orig_type = revert_automatic_type_conversion(expression);
6659 expression->base.type = orig_type;
6661 type_t const* const type = skip_typeref(orig_type);
6662 char const* const wrong_type =
6663 is_type_incomplete(type) ? "incomplete" :
6664 type->kind == TYPE_FUNCTION ? "function designator" :
6665 type->kind == TYPE_BITFIELD ? "bitfield" :
6667 if (wrong_type != NULL) {
6668 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6671 tp_expression->typeprop.type = expression->base.type;
6672 tp_expression->typeprop.tp_expression = expression;
6675 return tp_expression;
6677 return create_invalid_expression();
6680 static expression_t *parse_sizeof(unsigned precedence)
6682 source_position_t pos = *HERE;
6684 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6687 static expression_t *parse_alignof(unsigned precedence)
6689 source_position_t pos = *HERE;
6691 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6694 static expression_t *parse_select_expression(unsigned precedence,
6695 expression_t *compound)
6698 assert(token.type == '.' || token.type == T_MINUSGREATER);
6700 bool is_pointer = (token.type == T_MINUSGREATER);
6703 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6704 select->select.compound = compound;
6706 if (token.type != T_IDENTIFIER) {
6707 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6710 symbol_t *symbol = token.v.symbol;
6711 select->select.symbol = symbol;
6714 type_t *const orig_type = compound->base.type;
6715 type_t *const type = skip_typeref(orig_type);
6717 type_t *type_left = type;
6719 if (!is_type_pointer(type)) {
6720 if (is_type_valid(type)) {
6721 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6723 return create_invalid_expression();
6725 type_left = type->pointer.points_to;
6727 type_left = skip_typeref(type_left);
6729 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6730 type_left->kind != TYPE_COMPOUND_UNION) {
6731 if (is_type_valid(type_left)) {
6732 errorf(HERE, "request for member '%Y' in something not a struct or "
6733 "union, but '%T'", symbol, type_left);
6735 return create_invalid_expression();
6738 declaration_t *const declaration = type_left->compound.declaration;
6740 if (!declaration->init.complete) {
6741 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6743 return create_invalid_expression();
6746 declaration_t *iter = find_compound_entry(declaration, symbol);
6748 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6749 return create_invalid_expression();
6752 /* we always do the auto-type conversions; the & and sizeof parser contains
6753 * code to revert this! */
6754 type_t *expression_type = automatic_type_conversion(iter->type);
6756 select->select.compound_entry = iter;
6757 select->base.type = expression_type;
6759 type_t *skipped = skip_typeref(iter->type);
6760 if (skipped->kind == TYPE_BITFIELD) {
6761 select->base.type = skipped->bitfield.base_type;
6767 static void check_call_argument(const function_parameter_t *parameter,
6768 call_argument_t *argument)
6770 type_t *expected_type = parameter->type;
6771 type_t *expected_type_skip = skip_typeref(expected_type);
6772 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6773 expression_t *arg_expr = argument->expression;
6775 /* handle transparent union gnu extension */
6776 if (is_type_union(expected_type_skip)
6777 && (expected_type_skip->base.modifiers
6778 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6779 declaration_t *union_decl = expected_type_skip->compound.declaration;
6781 declaration_t *declaration = union_decl->scope.declarations;
6782 type_t *best_type = NULL;
6783 for ( ; declaration != NULL; declaration = declaration->next) {
6784 type_t *decl_type = declaration->type;
6785 error = semantic_assign(decl_type, arg_expr);
6786 if (error == ASSIGN_ERROR_INCOMPATIBLE
6787 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6790 if (error == ASSIGN_SUCCESS) {
6791 best_type = decl_type;
6792 } else if (best_type == NULL) {
6793 best_type = decl_type;
6797 if (best_type != NULL) {
6798 expected_type = best_type;
6802 error = semantic_assign(expected_type, arg_expr);
6803 argument->expression = create_implicit_cast(argument->expression,
6806 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6807 report_assign_error(error, expected_type, arg_expr, "function call",
6808 &arg_expr->base.source_position);
6812 * Parse a call expression, ie. expression '( ... )'.
6814 * @param expression the function address
6816 static expression_t *parse_call_expression(unsigned precedence,
6817 expression_t *expression)
6820 expression_t *result = allocate_expression_zero(EXPR_CALL);
6821 result->base.source_position = expression->base.source_position;
6823 call_expression_t *call = &result->call;
6824 call->function = expression;
6826 type_t *const orig_type = expression->base.type;
6827 type_t *const type = skip_typeref(orig_type);
6829 function_type_t *function_type = NULL;
6830 if (is_type_pointer(type)) {
6831 type_t *const to_type = skip_typeref(type->pointer.points_to);
6833 if (is_type_function(to_type)) {
6834 function_type = &to_type->function;
6835 call->base.type = function_type->return_type;
6839 if (function_type == NULL && is_type_valid(type)) {
6840 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6843 /* parse arguments */
6845 add_anchor_token(')');
6846 add_anchor_token(',');
6848 if (token.type != ')') {
6849 call_argument_t *last_argument = NULL;
6852 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6854 argument->expression = parse_assignment_expression();
6855 if (last_argument == NULL) {
6856 call->arguments = argument;
6858 last_argument->next = argument;
6860 last_argument = argument;
6862 if (token.type != ',')
6867 rem_anchor_token(',');
6868 rem_anchor_token(')');
6871 if (function_type == NULL)
6874 function_parameter_t *parameter = function_type->parameters;
6875 call_argument_t *argument = call->arguments;
6876 if (!function_type->unspecified_parameters) {
6877 for( ; parameter != NULL && argument != NULL;
6878 parameter = parameter->next, argument = argument->next) {
6879 check_call_argument(parameter, argument);
6882 if (parameter != NULL) {
6883 errorf(HERE, "too few arguments to function '%E'", expression);
6884 } else if (argument != NULL && !function_type->variadic) {
6885 errorf(HERE, "too many arguments to function '%E'", expression);
6889 /* do default promotion */
6890 for( ; argument != NULL; argument = argument->next) {
6891 type_t *type = argument->expression->base.type;
6893 type = get_default_promoted_type(type);
6895 argument->expression
6896 = create_implicit_cast(argument->expression, type);
6899 check_format(&result->call);
6901 if (warning.aggregate_return &&
6902 is_type_compound(skip_typeref(function_type->return_type))) {
6903 warningf(&result->base.source_position,
6904 "function call has aggregate value");
6909 return create_invalid_expression();
6912 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6914 static bool same_compound_type(const type_t *type1, const type_t *type2)
6917 is_type_compound(type1) &&
6918 type1->kind == type2->kind &&
6919 type1->compound.declaration == type2->compound.declaration;
6923 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6925 * @param expression the conditional expression
6927 static expression_t *parse_conditional_expression(unsigned precedence,
6928 expression_t *expression)
6930 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6932 conditional_expression_t *conditional = &result->conditional;
6933 conditional->base.source_position = *HERE;
6934 conditional->condition = expression;
6937 add_anchor_token(':');
6940 type_t *const condition_type_orig = expression->base.type;
6941 type_t *const condition_type = skip_typeref(condition_type_orig);
6942 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6943 type_error("expected a scalar type in conditional condition",
6944 &expression->base.source_position, condition_type_orig);
6947 expression_t *true_expression = expression;
6948 bool gnu_cond = false;
6949 if ((c_mode & _GNUC) && token.type == ':') {
6952 true_expression = parse_expression();
6953 rem_anchor_token(':');
6955 expression_t *false_expression = parse_sub_expression(precedence);
6957 type_t *const orig_true_type = true_expression->base.type;
6958 type_t *const orig_false_type = false_expression->base.type;
6959 type_t *const true_type = skip_typeref(orig_true_type);
6960 type_t *const false_type = skip_typeref(orig_false_type);
6963 type_t *result_type;
6964 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6965 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6966 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6967 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6968 warningf(&conditional->base.source_position,
6969 "ISO C forbids conditional expression with only one void side");
6971 result_type = type_void;
6972 } else if (is_type_arithmetic(true_type)
6973 && is_type_arithmetic(false_type)) {
6974 result_type = semantic_arithmetic(true_type, false_type);
6976 true_expression = create_implicit_cast(true_expression, result_type);
6977 false_expression = create_implicit_cast(false_expression, result_type);
6979 conditional->true_expression = true_expression;
6980 conditional->false_expression = false_expression;
6981 conditional->base.type = result_type;
6982 } else if (same_compound_type(true_type, false_type)) {
6983 /* just take 1 of the 2 types */
6984 result_type = true_type;
6985 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6986 type_t *pointer_type;
6988 expression_t *other_expression;
6989 if (is_type_pointer(true_type) &&
6990 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6991 pointer_type = true_type;
6992 other_type = false_type;
6993 other_expression = false_expression;
6995 pointer_type = false_type;
6996 other_type = true_type;
6997 other_expression = true_expression;
7000 if (is_null_pointer_constant(other_expression)) {
7001 result_type = pointer_type;
7002 } else if (is_type_pointer(other_type)) {
7003 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
7004 type_t *to2 = skip_typeref(other_type->pointer.points_to);
7007 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
7008 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
7010 } else if (types_compatible(get_unqualified_type(to1),
7011 get_unqualified_type(to2))) {
7014 warningf(&conditional->base.source_position,
7015 "pointer types '%T' and '%T' in conditional expression are incompatible",
7016 true_type, false_type);
7020 type_t *const copy = duplicate_type(to);
7021 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
7023 type_t *const type = typehash_insert(copy);
7027 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
7028 } else if (is_type_integer(other_type)) {
7029 warningf(&conditional->base.source_position,
7030 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
7031 result_type = pointer_type;
7033 type_error_incompatible("while parsing conditional",
7034 &expression->base.source_position, true_type, false_type);
7035 result_type = type_error_type;
7038 /* TODO: one pointer to void*, other some pointer */
7040 if (is_type_valid(true_type) && is_type_valid(false_type)) {
7041 type_error_incompatible("while parsing conditional",
7042 &conditional->base.source_position, true_type,
7045 result_type = type_error_type;
7048 conditional->true_expression
7049 = gnu_cond ? NULL : create_implicit_cast(true_expression, result_type);
7050 conditional->false_expression
7051 = create_implicit_cast(false_expression, result_type);
7052 conditional->base.type = result_type;
7055 return create_invalid_expression();
7059 * Parse an extension expression.
7061 static expression_t *parse_extension(unsigned precedence)
7063 eat(T___extension__);
7065 /* TODO enable extensions */
7066 expression_t *expression = parse_sub_expression(precedence);
7067 /* TODO disable extensions */
7072 * Parse a __builtin_classify_type() expression.
7074 static expression_t *parse_builtin_classify_type(const unsigned precedence)
7076 eat(T___builtin_classify_type);
7078 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
7079 result->base.type = type_int;
7082 add_anchor_token(')');
7083 expression_t *expression = parse_sub_expression(precedence);
7084 rem_anchor_token(')');
7086 result->classify_type.type_expression = expression;
7090 return create_invalid_expression();
7093 static bool check_pointer_arithmetic(const source_position_t *source_position,
7094 type_t *pointer_type,
7095 type_t *orig_pointer_type)
7097 type_t *points_to = pointer_type->pointer.points_to;
7098 points_to = skip_typeref(points_to);
7100 if (is_type_incomplete(points_to)) {
7101 if (!(c_mode & _GNUC) || !is_type_atomic(points_to, ATOMIC_TYPE_VOID)) {
7102 errorf(source_position,
7103 "arithmetic with pointer to incomplete type '%T' not allowed",
7106 } else if (warning.pointer_arith) {
7107 warningf(source_position,
7108 "pointer of type '%T' used in arithmetic",
7111 } else if (is_type_function(points_to)) {
7112 if (!(c_mode && _GNUC)) {
7113 errorf(source_position,
7114 "arithmetic with pointer to function type '%T' not allowed",
7117 } else if (warning.pointer_arith) {
7118 warningf(source_position,
7119 "pointer to a function '%T' used in arithmetic",
7126 static void semantic_incdec(unary_expression_t *expression)
7128 type_t *const orig_type = expression->value->base.type;
7129 type_t *const type = skip_typeref(orig_type);
7130 if (is_type_pointer(type)) {
7131 if (!check_pointer_arithmetic(&expression->base.source_position,
7135 } else if (!is_type_real(type) && is_type_valid(type)) {
7136 /* TODO: improve error message */
7137 errorf(&expression->base.source_position,
7138 "operation needs an arithmetic or pointer type");
7141 expression->base.type = orig_type;
7144 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
7146 type_t *const orig_type = expression->value->base.type;
7147 type_t *const type = skip_typeref(orig_type);
7148 if (!is_type_arithmetic(type)) {
7149 if (is_type_valid(type)) {
7150 /* TODO: improve error message */
7151 errorf(&expression->base.source_position,
7152 "operation needs an arithmetic type");
7157 expression->base.type = orig_type;
7160 static void semantic_not(unary_expression_t *expression)
7162 type_t *const orig_type = expression->value->base.type;
7163 type_t *const type = skip_typeref(orig_type);
7164 if (!is_type_scalar(type) && is_type_valid(type)) {
7165 errorf(&expression->base.source_position,
7166 "operand of ! must be of scalar type");
7169 expression->base.type = type_int;
7172 static void semantic_unexpr_integer(unary_expression_t *expression)
7174 type_t *const orig_type = expression->value->base.type;
7175 type_t *const type = skip_typeref(orig_type);
7176 if (!is_type_integer(type)) {
7177 if (is_type_valid(type)) {
7178 errorf(&expression->base.source_position,
7179 "operand of ~ must be of integer type");
7184 expression->base.type = orig_type;
7187 static void semantic_dereference(unary_expression_t *expression)
7189 type_t *const orig_type = expression->value->base.type;
7190 type_t *const type = skip_typeref(orig_type);
7191 if (!is_type_pointer(type)) {
7192 if (is_type_valid(type)) {
7193 errorf(&expression->base.source_position,
7194 "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
7199 type_t *result_type = type->pointer.points_to;
7200 result_type = automatic_type_conversion(result_type);
7201 expression->base.type = result_type;
7204 static void set_address_taken(expression_t *expression, bool may_be_register)
7206 if (expression->kind != EXPR_REFERENCE)
7209 declaration_t *const declaration = expression->reference.declaration;
7210 /* happens for parse errors */
7211 if (declaration == NULL)
7214 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
7215 errorf(&expression->base.source_position,
7216 "address of register variable '%Y' requested",
7217 declaration->symbol);
7219 declaration->address_taken = 1;
7224 * Check the semantic of the address taken expression.
7226 static void semantic_take_addr(unary_expression_t *expression)
7228 expression_t *value = expression->value;
7229 value->base.type = revert_automatic_type_conversion(value);
7231 type_t *orig_type = value->base.type;
7232 if (!is_type_valid(orig_type))
7235 set_address_taken(value, false);
7237 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
7240 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
7241 static expression_t *parse_##unexpression_type(unsigned precedence) \
7243 expression_t *unary_expression \
7244 = allocate_expression_zero(unexpression_type); \
7245 unary_expression->base.source_position = *HERE; \
7247 unary_expression->unary.value = parse_sub_expression(precedence); \
7249 sfunc(&unary_expression->unary); \
7251 return unary_expression; \
7254 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
7255 semantic_unexpr_arithmetic)
7256 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
7257 semantic_unexpr_arithmetic)
7258 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
7260 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
7261 semantic_dereference)
7262 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
7264 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
7265 semantic_unexpr_integer)
7266 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
7268 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
7271 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
7273 static expression_t *parse_##unexpression_type(unsigned precedence, \
7274 expression_t *left) \
7276 (void) precedence; \
7278 expression_t *unary_expression \
7279 = allocate_expression_zero(unexpression_type); \
7280 unary_expression->base.source_position = *HERE; \
7282 unary_expression->unary.value = left; \
7284 sfunc(&unary_expression->unary); \
7286 return unary_expression; \
7289 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
7290 EXPR_UNARY_POSTFIX_INCREMENT,
7292 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
7293 EXPR_UNARY_POSTFIX_DECREMENT,
7296 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
7298 /* TODO: handle complex + imaginary types */
7300 /* § 6.3.1.8 Usual arithmetic conversions */
7301 if (type_left == type_long_double || type_right == type_long_double) {
7302 return type_long_double;
7303 } else if (type_left == type_double || type_right == type_double) {
7305 } else if (type_left == type_float || type_right == type_float) {
7309 type_left = promote_integer(type_left);
7310 type_right = promote_integer(type_right);
7312 if (type_left == type_right)
7315 bool const signed_left = is_type_signed(type_left);
7316 bool const signed_right = is_type_signed(type_right);
7317 int const rank_left = get_rank(type_left);
7318 int const rank_right = get_rank(type_right);
7320 if (signed_left == signed_right)
7321 return rank_left >= rank_right ? type_left : type_right;
7330 u_rank = rank_right;
7331 u_type = type_right;
7333 s_rank = rank_right;
7334 s_type = type_right;
7339 if (u_rank >= s_rank)
7342 /* casting rank to atomic_type_kind is a bit hacky, but makes things
7344 if (get_atomic_type_size((atomic_type_kind_t) s_rank)
7345 > get_atomic_type_size((atomic_type_kind_t) u_rank))
7349 case ATOMIC_TYPE_INT: return type_unsigned_int;
7350 case ATOMIC_TYPE_LONG: return type_unsigned_long;
7351 case ATOMIC_TYPE_LONGLONG: return type_unsigned_long_long;
7353 default: panic("invalid atomic type");
7358 * Check the semantic restrictions for a binary expression.
7360 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
7362 expression_t *const left = expression->left;
7363 expression_t *const right = expression->right;
7364 type_t *const orig_type_left = left->base.type;
7365 type_t *const orig_type_right = right->base.type;
7366 type_t *const type_left = skip_typeref(orig_type_left);
7367 type_t *const type_right = skip_typeref(orig_type_right);
7369 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7370 /* TODO: improve error message */
7371 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7372 errorf(&expression->base.source_position,
7373 "operation needs arithmetic types");
7378 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7379 expression->left = create_implicit_cast(left, arithmetic_type);
7380 expression->right = create_implicit_cast(right, arithmetic_type);
7381 expression->base.type = arithmetic_type;
7384 static void warn_div_by_zero(binary_expression_t const *const expression)
7386 if (warning.div_by_zero &&
7387 is_type_integer(expression->base.type) &&
7388 is_constant_expression(expression->right) &&
7389 fold_constant(expression->right) == 0) {
7390 warningf(&expression->base.source_position, "division by zero");
7395 * Check the semantic restrictions for a div/mod expression.
7397 static void semantic_divmod_arithmetic(binary_expression_t *expression) {
7398 semantic_binexpr_arithmetic(expression);
7399 warn_div_by_zero(expression);
7402 static void semantic_shift_op(binary_expression_t *expression)
7404 expression_t *const left = expression->left;
7405 expression_t *const right = expression->right;
7406 type_t *const orig_type_left = left->base.type;
7407 type_t *const orig_type_right = right->base.type;
7408 type_t * type_left = skip_typeref(orig_type_left);
7409 type_t * type_right = skip_typeref(orig_type_right);
7411 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
7412 /* TODO: improve error message */
7413 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7414 errorf(&expression->base.source_position,
7415 "operands of shift operation must have integer types");
7420 type_left = promote_integer(type_left);
7421 type_right = promote_integer(type_right);
7423 expression->left = create_implicit_cast(left, type_left);
7424 expression->right = create_implicit_cast(right, type_right);
7425 expression->base.type = type_left;
7428 static void semantic_add(binary_expression_t *expression)
7430 expression_t *const left = expression->left;
7431 expression_t *const right = expression->right;
7432 type_t *const orig_type_left = left->base.type;
7433 type_t *const orig_type_right = right->base.type;
7434 type_t *const type_left = skip_typeref(orig_type_left);
7435 type_t *const type_right = skip_typeref(orig_type_right);
7438 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7439 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7440 expression->left = create_implicit_cast(left, arithmetic_type);
7441 expression->right = create_implicit_cast(right, arithmetic_type);
7442 expression->base.type = arithmetic_type;
7444 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7445 check_pointer_arithmetic(&expression->base.source_position,
7446 type_left, orig_type_left);
7447 expression->base.type = type_left;
7448 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
7449 check_pointer_arithmetic(&expression->base.source_position,
7450 type_right, orig_type_right);
7451 expression->base.type = type_right;
7452 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7453 errorf(&expression->base.source_position,
7454 "invalid operands to binary + ('%T', '%T')",
7455 orig_type_left, orig_type_right);
7459 static void semantic_sub(binary_expression_t *expression)
7461 expression_t *const left = expression->left;
7462 expression_t *const right = expression->right;
7463 type_t *const orig_type_left = left->base.type;
7464 type_t *const orig_type_right = right->base.type;
7465 type_t *const type_left = skip_typeref(orig_type_left);
7466 type_t *const type_right = skip_typeref(orig_type_right);
7467 source_position_t const *const pos = &expression->base.source_position;
7470 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7471 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7472 expression->left = create_implicit_cast(left, arithmetic_type);
7473 expression->right = create_implicit_cast(right, arithmetic_type);
7474 expression->base.type = arithmetic_type;
7476 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7477 check_pointer_arithmetic(&expression->base.source_position,
7478 type_left, orig_type_left);
7479 expression->base.type = type_left;
7480 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
7481 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
7482 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
7483 if (!types_compatible(unqual_left, unqual_right)) {
7485 "subtracting pointers to incompatible types '%T' and '%T'",
7486 orig_type_left, orig_type_right);
7487 } else if (!is_type_object(unqual_left)) {
7488 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
7489 warningf(pos, "subtracting pointers to void");
7491 errorf(pos, "subtracting pointers to non-object types '%T'",
7495 expression->base.type = type_ptrdiff_t;
7496 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7497 errorf(pos, "invalid operands of types '%T' and '%T' to binary '-'",
7498 orig_type_left, orig_type_right);
7503 * Check the semantics of comparison expressions.
7505 * @param expression The expression to check.
7507 static void semantic_comparison(binary_expression_t *expression)
7509 expression_t *left = expression->left;
7510 expression_t *right = expression->right;
7511 type_t *orig_type_left = left->base.type;
7512 type_t *orig_type_right = right->base.type;
7514 type_t *type_left = skip_typeref(orig_type_left);
7515 type_t *type_right = skip_typeref(orig_type_right);
7517 /* TODO non-arithmetic types */
7518 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7519 /* test for signed vs unsigned compares */
7520 if (warning.sign_compare &&
7521 (expression->base.kind != EXPR_BINARY_EQUAL &&
7522 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
7523 (is_type_signed(type_left) != is_type_signed(type_right))) {
7525 /* check if 1 of the operands is a constant, in this case we just
7526 * check wether we can safely represent the resulting constant in
7527 * the type of the other operand. */
7528 expression_t *const_expr = NULL;
7529 expression_t *other_expr = NULL;
7531 if (is_constant_expression(left)) {
7534 } else if (is_constant_expression(right)) {
7539 if (const_expr != NULL) {
7540 type_t *other_type = skip_typeref(other_expr->base.type);
7541 long val = fold_constant(const_expr);
7542 /* TODO: check if val can be represented by other_type */
7546 warningf(&expression->base.source_position,
7547 "comparison between signed and unsigned");
7549 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7550 expression->left = create_implicit_cast(left, arithmetic_type);
7551 expression->right = create_implicit_cast(right, arithmetic_type);
7552 expression->base.type = arithmetic_type;
7553 if (warning.float_equal &&
7554 (expression->base.kind == EXPR_BINARY_EQUAL ||
7555 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
7556 is_type_float(arithmetic_type)) {
7557 warningf(&expression->base.source_position,
7558 "comparing floating point with == or != is unsafe");
7560 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
7561 /* TODO check compatibility */
7562 } else if (is_type_pointer(type_left)) {
7563 expression->right = create_implicit_cast(right, type_left);
7564 } else if (is_type_pointer(type_right)) {
7565 expression->left = create_implicit_cast(left, type_right);
7566 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7567 type_error_incompatible("invalid operands in comparison",
7568 &expression->base.source_position,
7569 type_left, type_right);
7571 expression->base.type = type_int;
7575 * Checks if a compound type has constant fields.
7577 static bool has_const_fields(const compound_type_t *type)
7579 const scope_t *scope = &type->declaration->scope;
7580 const declaration_t *declaration = scope->declarations;
7582 for (; declaration != NULL; declaration = declaration->next) {
7583 if (declaration->namespc != NAMESPACE_NORMAL)
7586 const type_t *decl_type = skip_typeref(declaration->type);
7587 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
7594 static bool is_lvalue(const expression_t *expression)
7596 switch (expression->kind) {
7597 case EXPR_REFERENCE:
7598 case EXPR_ARRAY_ACCESS:
7600 case EXPR_UNARY_DEREFERENCE:
7608 static bool is_valid_assignment_lhs(expression_t const* const left)
7610 type_t *const orig_type_left = revert_automatic_type_conversion(left);
7611 type_t *const type_left = skip_typeref(orig_type_left);
7613 if (!is_lvalue(left)) {
7614 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
7619 if (is_type_array(type_left)) {
7620 errorf(HERE, "cannot assign to arrays ('%E')", left);
7623 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
7624 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
7628 if (is_type_incomplete(type_left)) {
7629 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
7630 left, orig_type_left);
7633 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
7634 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
7635 left, orig_type_left);
7642 static void semantic_arithmetic_assign(binary_expression_t *expression)
7644 expression_t *left = expression->left;
7645 expression_t *right = expression->right;
7646 type_t *orig_type_left = left->base.type;
7647 type_t *orig_type_right = right->base.type;
7649 if (!is_valid_assignment_lhs(left))
7652 type_t *type_left = skip_typeref(orig_type_left);
7653 type_t *type_right = skip_typeref(orig_type_right);
7655 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
7656 /* TODO: improve error message */
7657 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7658 errorf(&expression->base.source_position,
7659 "operation needs arithmetic types");
7664 /* combined instructions are tricky. We can't create an implicit cast on
7665 * the left side, because we need the uncasted form for the store.
7666 * The ast2firm pass has to know that left_type must be right_type
7667 * for the arithmetic operation and create a cast by itself */
7668 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
7669 expression->right = create_implicit_cast(right, arithmetic_type);
7670 expression->base.type = type_left;
7673 static void semantic_divmod_assign(binary_expression_t *expression)
7675 semantic_arithmetic_assign(expression);
7676 warn_div_by_zero(expression);
7679 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
7681 expression_t *const left = expression->left;
7682 expression_t *const right = expression->right;
7683 type_t *const orig_type_left = left->base.type;
7684 type_t *const orig_type_right = right->base.type;
7685 type_t *const type_left = skip_typeref(orig_type_left);
7686 type_t *const type_right = skip_typeref(orig_type_right);
7688 if (!is_valid_assignment_lhs(left))
7691 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
7692 /* combined instructions are tricky. We can't create an implicit cast on
7693 * the left side, because we need the uncasted form for the store.
7694 * The ast2firm pass has to know that left_type must be right_type
7695 * for the arithmetic operation and create a cast by itself */
7696 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
7697 expression->right = create_implicit_cast(right, arithmetic_type);
7698 expression->base.type = type_left;
7699 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
7700 check_pointer_arithmetic(&expression->base.source_position,
7701 type_left, orig_type_left);
7702 expression->base.type = type_left;
7703 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
7704 errorf(&expression->base.source_position,
7705 "incompatible types '%T' and '%T' in assignment",
7706 orig_type_left, orig_type_right);
7711 * Check the semantic restrictions of a logical expression.
7713 static void semantic_logical_op(binary_expression_t *expression)
7715 expression_t *const left = expression->left;
7716 expression_t *const right = expression->right;
7717 type_t *const orig_type_left = left->base.type;
7718 type_t *const orig_type_right = right->base.type;
7719 type_t *const type_left = skip_typeref(orig_type_left);
7720 type_t *const type_right = skip_typeref(orig_type_right);
7722 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7723 /* TODO: improve error message */
7724 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7725 errorf(&expression->base.source_position,
7726 "operation needs scalar types");
7731 expression->base.type = type_int;
7735 * Check the semantic restrictions of a binary assign expression.
7737 static void semantic_binexpr_assign(binary_expression_t *expression)
7739 expression_t *left = expression->left;
7740 type_t *orig_type_left = left->base.type;
7742 type_t *type_left = revert_automatic_type_conversion(left);
7743 type_left = skip_typeref(orig_type_left);
7745 if (!is_valid_assignment_lhs(left))
7748 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7749 report_assign_error(error, orig_type_left, expression->right,
7750 "assignment", &left->base.source_position);
7751 expression->right = create_implicit_cast(expression->right, orig_type_left);
7752 expression->base.type = orig_type_left;
7756 * Determine if the outermost operation (or parts thereof) of the given
7757 * expression has no effect in order to generate a warning about this fact.
7758 * Therefore in some cases this only examines some of the operands of the
7759 * expression (see comments in the function and examples below).
7761 * f() + 23; // warning, because + has no effect
7762 * x || f(); // no warning, because x controls execution of f()
7763 * x ? y : f(); // warning, because y has no effect
7764 * (void)x; // no warning to be able to suppress the warning
7765 * This function can NOT be used for an "expression has definitely no effect"-
7767 static bool expression_has_effect(const expression_t *const expr)
7769 switch (expr->kind) {
7770 case EXPR_UNKNOWN: break;
7771 case EXPR_INVALID: return true; /* do NOT warn */
7772 case EXPR_REFERENCE: return false;
7773 /* suppress the warning for microsoft __noop operations */
7774 case EXPR_CONST: return expr->conste.is_ms_noop;
7775 case EXPR_CHARACTER_CONSTANT: return false;
7776 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7777 case EXPR_STRING_LITERAL: return false;
7778 case EXPR_WIDE_STRING_LITERAL: return false;
7781 const call_expression_t *const call = &expr->call;
7782 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7785 switch (call->function->builtin_symbol.symbol->ID) {
7786 case T___builtin_va_end: return true;
7787 default: return false;
7791 /* Generate the warning if either the left or right hand side of a
7792 * conditional expression has no effect */
7793 case EXPR_CONDITIONAL: {
7794 const conditional_expression_t *const cond = &expr->conditional;
7796 expression_has_effect(cond->true_expression) &&
7797 expression_has_effect(cond->false_expression);
7800 case EXPR_SELECT: return false;
7801 case EXPR_ARRAY_ACCESS: return false;
7802 case EXPR_SIZEOF: return false;
7803 case EXPR_CLASSIFY_TYPE: return false;
7804 case EXPR_ALIGNOF: return false;
7806 case EXPR_FUNCNAME: return false;
7807 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7808 case EXPR_BUILTIN_CONSTANT_P: return false;
7809 case EXPR_BUILTIN_PREFETCH: return true;
7810 case EXPR_OFFSETOF: return false;
7811 case EXPR_VA_START: return true;
7812 case EXPR_VA_ARG: return true;
7813 case EXPR_STATEMENT: return true; // TODO
7814 case EXPR_COMPOUND_LITERAL: return false;
7816 case EXPR_UNARY_NEGATE: return false;
7817 case EXPR_UNARY_PLUS: return false;
7818 case EXPR_UNARY_BITWISE_NEGATE: return false;
7819 case EXPR_UNARY_NOT: return false;
7820 case EXPR_UNARY_DEREFERENCE: return false;
7821 case EXPR_UNARY_TAKE_ADDRESS: return false;
7822 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7823 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7824 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7825 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7827 /* Treat void casts as if they have an effect in order to being able to
7828 * suppress the warning */
7829 case EXPR_UNARY_CAST: {
7830 type_t *const type = skip_typeref(expr->base.type);
7831 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7834 case EXPR_UNARY_CAST_IMPLICIT: return true;
7835 case EXPR_UNARY_ASSUME: return true;
7837 case EXPR_BINARY_ADD: return false;
7838 case EXPR_BINARY_SUB: return false;
7839 case EXPR_BINARY_MUL: return false;
7840 case EXPR_BINARY_DIV: return false;
7841 case EXPR_BINARY_MOD: return false;
7842 case EXPR_BINARY_EQUAL: return false;
7843 case EXPR_BINARY_NOTEQUAL: return false;
7844 case EXPR_BINARY_LESS: return false;
7845 case EXPR_BINARY_LESSEQUAL: return false;
7846 case EXPR_BINARY_GREATER: return false;
7847 case EXPR_BINARY_GREATEREQUAL: return false;
7848 case EXPR_BINARY_BITWISE_AND: return false;
7849 case EXPR_BINARY_BITWISE_OR: return false;
7850 case EXPR_BINARY_BITWISE_XOR: return false;
7851 case EXPR_BINARY_SHIFTLEFT: return false;
7852 case EXPR_BINARY_SHIFTRIGHT: return false;
7853 case EXPR_BINARY_ASSIGN: return true;
7854 case EXPR_BINARY_MUL_ASSIGN: return true;
7855 case EXPR_BINARY_DIV_ASSIGN: return true;
7856 case EXPR_BINARY_MOD_ASSIGN: return true;
7857 case EXPR_BINARY_ADD_ASSIGN: return true;
7858 case EXPR_BINARY_SUB_ASSIGN: return true;
7859 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7860 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7861 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7862 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7863 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7865 /* Only examine the right hand side of && and ||, because the left hand
7866 * side already has the effect of controlling the execution of the right
7868 case EXPR_BINARY_LOGICAL_AND:
7869 case EXPR_BINARY_LOGICAL_OR:
7870 /* Only examine the right hand side of a comma expression, because the left
7871 * hand side has a separate warning */
7872 case EXPR_BINARY_COMMA:
7873 return expression_has_effect(expr->binary.right);
7875 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7876 case EXPR_BINARY_ISGREATER: return false;
7877 case EXPR_BINARY_ISGREATEREQUAL: return false;
7878 case EXPR_BINARY_ISLESS: return false;
7879 case EXPR_BINARY_ISLESSEQUAL: return false;
7880 case EXPR_BINARY_ISLESSGREATER: return false;
7881 case EXPR_BINARY_ISUNORDERED: return false;
7884 internal_errorf(HERE, "unexpected expression");
7887 static void semantic_comma(binary_expression_t *expression)
7889 if (warning.unused_value) {
7890 const expression_t *const left = expression->left;
7891 if (!expression_has_effect(left)) {
7892 warningf(&left->base.source_position,
7893 "left-hand operand of comma expression has no effect");
7896 expression->base.type = expression->right->base.type;
7899 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7900 static expression_t *parse_##binexpression_type(unsigned precedence, \
7901 expression_t *left) \
7903 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7904 binexpr->base.source_position = *HERE; \
7905 binexpr->binary.left = left; \
7908 expression_t *right = parse_sub_expression(precedence + lr); \
7910 binexpr->binary.right = right; \
7911 sfunc(&binexpr->binary); \
7916 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7917 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7918 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_divmod_arithmetic, 1)
7919 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_divmod_arithmetic, 1)
7920 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7921 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7922 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7923 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7924 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7926 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7927 semantic_comparison, 1)
7928 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7929 semantic_comparison, 1)
7930 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7931 semantic_comparison, 1)
7932 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7933 semantic_comparison, 1)
7935 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7936 semantic_binexpr_arithmetic, 1)
7937 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7938 semantic_binexpr_arithmetic, 1)
7939 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7940 semantic_binexpr_arithmetic, 1)
7941 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7942 semantic_logical_op, 1)
7943 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7944 semantic_logical_op, 1)
7945 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7946 semantic_shift_op, 1)
7947 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7948 semantic_shift_op, 1)
7949 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7950 semantic_arithmetic_addsubb_assign, 0)
7951 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7952 semantic_arithmetic_addsubb_assign, 0)
7953 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7954 semantic_arithmetic_assign, 0)
7955 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7956 semantic_divmod_assign, 0)
7957 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7958 semantic_divmod_assign, 0)
7959 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7960 semantic_arithmetic_assign, 0)
7961 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7962 semantic_arithmetic_assign, 0)
7963 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7964 semantic_arithmetic_assign, 0)
7965 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7966 semantic_arithmetic_assign, 0)
7967 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7968 semantic_arithmetic_assign, 0)
7970 static expression_t *parse_sub_expression(unsigned precedence)
7972 if (token.type < 0) {
7973 return expected_expression_error();
7976 expression_parser_function_t *parser
7977 = &expression_parsers[token.type];
7978 source_position_t source_position = token.source_position;
7981 if (parser->parser != NULL) {
7982 left = parser->parser(parser->precedence);
7984 left = parse_primary_expression();
7986 assert(left != NULL);
7987 left->base.source_position = source_position;
7990 if (token.type < 0) {
7991 return expected_expression_error();
7994 parser = &expression_parsers[token.type];
7995 if (parser->infix_parser == NULL)
7997 if (parser->infix_precedence < precedence)
8000 left = parser->infix_parser(parser->infix_precedence, left);
8002 assert(left != NULL);
8003 assert(left->kind != EXPR_UNKNOWN);
8004 left->base.source_position = source_position;
8011 * Parse an expression.
8013 static expression_t *parse_expression(void)
8015 return parse_sub_expression(1);
8019 * Register a parser for a prefix-like operator with given precedence.
8021 * @param parser the parser function
8022 * @param token_type the token type of the prefix token
8023 * @param precedence the precedence of the operator
8025 static void register_expression_parser(parse_expression_function parser,
8026 int token_type, unsigned precedence)
8028 expression_parser_function_t *entry = &expression_parsers[token_type];
8030 if (entry->parser != NULL) {
8031 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8032 panic("trying to register multiple expression parsers for a token");
8034 entry->parser = parser;
8035 entry->precedence = precedence;
8039 * Register a parser for an infix operator with given precedence.
8041 * @param parser the parser function
8042 * @param token_type the token type of the infix operator
8043 * @param precedence the precedence of the operator
8045 static void register_infix_parser(parse_expression_infix_function parser,
8046 int token_type, unsigned precedence)
8048 expression_parser_function_t *entry = &expression_parsers[token_type];
8050 if (entry->infix_parser != NULL) {
8051 diagnosticf("for token '%k'\n", (token_type_t)token_type);
8052 panic("trying to register multiple infix expression parsers for a "
8055 entry->infix_parser = parser;
8056 entry->infix_precedence = precedence;
8060 * Initialize the expression parsers.
8062 static void init_expression_parsers(void)
8064 memset(&expression_parsers, 0, sizeof(expression_parsers));
8066 register_infix_parser(parse_array_expression, '[', 30);
8067 register_infix_parser(parse_call_expression, '(', 30);
8068 register_infix_parser(parse_select_expression, '.', 30);
8069 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
8070 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
8072 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
8075 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
8076 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
8077 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
8078 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
8079 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
8080 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
8081 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
8082 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
8083 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
8084 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
8085 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
8086 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
8087 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
8088 T_EXCLAMATIONMARKEQUAL, 13);
8089 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
8090 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
8091 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
8092 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
8093 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
8094 register_infix_parser(parse_conditional_expression, '?', 7);
8095 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
8096 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
8097 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
8098 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
8099 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
8100 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
8101 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
8102 T_LESSLESSEQUAL, 2);
8103 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
8104 T_GREATERGREATEREQUAL, 2);
8105 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
8107 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
8109 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
8112 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
8114 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
8115 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
8116 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
8117 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
8118 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
8119 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
8120 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
8122 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
8124 register_expression_parser(parse_sizeof, T_sizeof, 25);
8125 register_expression_parser(parse_alignof, T___alignof__, 25);
8126 register_expression_parser(parse_extension, T___extension__, 25);
8127 register_expression_parser(parse_builtin_classify_type,
8128 T___builtin_classify_type, 25);
8132 * Parse a asm statement arguments specification.
8134 static asm_argument_t *parse_asm_arguments(bool is_out)
8136 asm_argument_t *result = NULL;
8137 asm_argument_t *last = NULL;
8139 while (token.type == T_STRING_LITERAL || token.type == '[') {
8140 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
8141 memset(argument, 0, sizeof(argument[0]));
8143 if (token.type == '[') {
8145 if (token.type != T_IDENTIFIER) {
8146 parse_error_expected("while parsing asm argument",
8147 T_IDENTIFIER, NULL);
8150 argument->symbol = token.v.symbol;
8155 argument->constraints = parse_string_literals();
8157 add_anchor_token(')');
8158 expression_t *expression = parse_expression();
8159 rem_anchor_token(')');
8161 /* Ugly GCC stuff: Allow lvalue casts. Skip casts, when they do not
8162 * change size or type representation (e.g. int -> long is ok, but
8163 * int -> float is not) */
8164 if (expression->kind == EXPR_UNARY_CAST) {
8165 type_t *const type = expression->base.type;
8166 type_kind_t const kind = type->kind;
8167 if (kind == TYPE_ATOMIC || kind == TYPE_POINTER) {
8170 if (kind == TYPE_ATOMIC) {
8171 atomic_type_kind_t const akind = type->atomic.akind;
8172 flags = get_atomic_type_flags(akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8173 size = get_atomic_type_size(akind);
8175 flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8176 size = get_atomic_type_size(get_intptr_kind());
8180 expression_t *const value = expression->unary.value;
8181 type_t *const value_type = value->base.type;
8182 type_kind_t const value_kind = value_type->kind;
8184 unsigned value_flags;
8185 unsigned value_size;
8186 if (value_kind == TYPE_ATOMIC) {
8187 atomic_type_kind_t const value_akind = value_type->atomic.akind;
8188 value_flags = get_atomic_type_flags(value_akind) & ~ATOMIC_TYPE_FLAG_SIGNED;
8189 value_size = get_atomic_type_size(value_akind);
8190 } else if (value_kind == TYPE_POINTER) {
8191 value_flags = ATOMIC_TYPE_FLAG_INTEGER | ATOMIC_TYPE_FLAG_ARITHMETIC;
8192 value_size = get_atomic_type_size(get_intptr_kind());
8197 if (value_flags != flags || value_size != size)
8201 } while (expression->kind == EXPR_UNARY_CAST);
8205 if (!is_lvalue(expression)) {
8206 errorf(&expression->base.source_position,
8207 "asm output argument is not an lvalue");
8210 argument->expression = expression;
8213 set_address_taken(expression, true);
8216 last->next = argument;
8222 if (token.type != ',')
8233 * Parse a asm statement clobber specification.
8235 static asm_clobber_t *parse_asm_clobbers(void)
8237 asm_clobber_t *result = NULL;
8238 asm_clobber_t *last = NULL;
8240 while(token.type == T_STRING_LITERAL) {
8241 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
8242 clobber->clobber = parse_string_literals();
8245 last->next = clobber;
8251 if (token.type != ',')
8260 * Parse an asm statement.
8262 static statement_t *parse_asm_statement(void)
8266 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8267 statement->base.source_position = token.source_position;
8269 asm_statement_t *asm_statement = &statement->asms;
8271 if (token.type == T_volatile) {
8273 asm_statement->is_volatile = true;
8277 add_anchor_token(')');
8278 add_anchor_token(':');
8279 asm_statement->asm_text = parse_string_literals();
8281 if (token.type != ':') {
8282 rem_anchor_token(':');
8287 asm_statement->outputs = parse_asm_arguments(true);
8288 if (token.type != ':') {
8289 rem_anchor_token(':');
8294 asm_statement->inputs = parse_asm_arguments(false);
8295 if (token.type != ':') {
8296 rem_anchor_token(':');
8299 rem_anchor_token(':');
8302 asm_statement->clobbers = parse_asm_clobbers();
8305 rem_anchor_token(')');
8309 if (asm_statement->outputs == NULL) {
8310 /* GCC: An 'asm' instruction without any output operands will be treated
8311 * identically to a volatile 'asm' instruction. */
8312 asm_statement->is_volatile = true;
8317 return create_invalid_statement();
8321 * Parse a case statement.
8323 static statement_t *parse_case_statement(void)
8327 statement_t *const statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8328 source_position_t *const pos = &statement->base.source_position;
8330 *pos = token.source_position;
8331 statement->case_label.expression = parse_expression();
8332 if (! is_constant_expression(statement->case_label.expression)) {
8333 errorf(pos, "case label does not reduce to an integer constant");
8334 statement->case_label.is_bad = true;
8336 long const val = fold_constant(statement->case_label.expression);
8337 statement->case_label.first_case = val;
8338 statement->case_label.last_case = val;
8341 if (c_mode & _GNUC) {
8342 if (token.type == T_DOTDOTDOT) {
8344 statement->case_label.end_range = parse_expression();
8345 if (! is_constant_expression(statement->case_label.end_range)) {
8346 errorf(pos, "case range does not reduce to an integer constant");
8347 statement->case_label.is_bad = true;
8349 long const val = fold_constant(statement->case_label.end_range);
8350 statement->case_label.last_case = val;
8352 if (val < statement->case_label.first_case) {
8353 statement->case_label.is_empty = true;
8354 warningf(pos, "empty range specified");
8360 PUSH_PARENT(statement);
8364 if (current_switch != NULL) {
8365 if (! statement->case_label.is_bad) {
8366 /* Check for duplicate case values */
8367 case_label_statement_t *c = &statement->case_label;
8368 for (case_label_statement_t *l = current_switch->first_case; l != NULL; l = l->next) {
8369 if (l->is_bad || l->is_empty || l->expression == NULL)
8372 if (c->last_case < l->first_case || c->first_case > l->last_case)
8375 errorf(pos, "duplicate case value (previously used %P)",
8376 &l->base.source_position);
8380 /* link all cases into the switch statement */
8381 if (current_switch->last_case == NULL) {
8382 current_switch->first_case = &statement->case_label;
8384 current_switch->last_case->next = &statement->case_label;
8386 current_switch->last_case = &statement->case_label;
8388 errorf(pos, "case label not within a switch statement");
8391 statement_t *const inner_stmt = parse_statement();
8392 statement->case_label.statement = inner_stmt;
8393 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8394 errorf(&inner_stmt->base.source_position, "declaration after case label");
8401 return create_invalid_statement();
8405 * Parse a default statement.
8407 static statement_t *parse_default_statement(void)
8411 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
8412 statement->base.source_position = token.source_position;
8414 PUSH_PARENT(statement);
8417 if (current_switch != NULL) {
8418 const case_label_statement_t *def_label = current_switch->default_label;
8419 if (def_label != NULL) {
8420 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
8421 &def_label->base.source_position);
8423 current_switch->default_label = &statement->case_label;
8425 /* link all cases into the switch statement */
8426 if (current_switch->last_case == NULL) {
8427 current_switch->first_case = &statement->case_label;
8429 current_switch->last_case->next = &statement->case_label;
8431 current_switch->last_case = &statement->case_label;
8434 errorf(&statement->base.source_position,
8435 "'default' label not within a switch statement");
8438 statement_t *const inner_stmt = parse_statement();
8439 statement->case_label.statement = inner_stmt;
8440 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8441 errorf(&inner_stmt->base.source_position, "declaration after default label");
8448 return create_invalid_statement();
8452 * Return the declaration for a given label symbol or create a new one.
8454 * @param symbol the symbol of the label
8456 static declaration_t *get_label(symbol_t *symbol)
8458 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
8459 assert(current_function != NULL);
8460 /* if we found a label in the same function, then we already created the
8462 if (candidate != NULL
8463 && candidate->parent_scope == ¤t_function->scope) {
8467 /* otherwise we need to create a new one */
8468 declaration_t *const declaration = allocate_declaration_zero();
8469 declaration->namespc = NAMESPACE_LABEL;
8470 declaration->symbol = symbol;
8472 label_push(declaration);
8478 * Parse a label statement.
8480 static statement_t *parse_label_statement(void)
8482 assert(token.type == T_IDENTIFIER);
8483 symbol_t *symbol = token.v.symbol;
8486 declaration_t *label = get_label(symbol);
8488 statement_t *const statement = allocate_statement_zero(STATEMENT_LABEL);
8489 statement->base.source_position = token.source_position;
8490 statement->label.label = label;
8492 PUSH_PARENT(statement);
8494 /* if source position is already set then the label is defined twice,
8495 * otherwise it was just mentioned in a goto so far */
8496 if (label->source_position.input_name != NULL) {
8497 errorf(HERE, "duplicate label '%Y' (declared %P)",
8498 symbol, &label->source_position);
8500 label->source_position = token.source_position;
8501 label->init.statement = statement;
8506 if (token.type == '}') {
8507 /* TODO only warn? */
8509 warningf(HERE, "label at end of compound statement");
8510 statement->label.statement = create_empty_statement();
8512 errorf(HERE, "label at end of compound statement");
8513 statement->label.statement = create_invalid_statement();
8515 } else if (token.type == ';') {
8516 /* Eat an empty statement here, to avoid the warning about an empty
8517 * statement after a label. label:; is commonly used to have a label
8518 * before a closing brace. */
8519 statement->label.statement = create_empty_statement();
8522 statement_t *const inner_stmt = parse_statement();
8523 statement->label.statement = inner_stmt;
8524 if (inner_stmt->kind == STATEMENT_DECLARATION) {
8525 errorf(&inner_stmt->base.source_position, "declaration after label");
8529 /* remember the labels in a list for later checking */
8530 if (label_last == NULL) {
8531 label_first = &statement->label;
8533 label_last->next = &statement->label;
8535 label_last = &statement->label;
8542 * Parse an if statement.
8544 static statement_t *parse_if(void)
8548 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
8549 statement->base.source_position = token.source_position;
8551 PUSH_PARENT(statement);
8554 add_anchor_token(')');
8555 statement->ifs.condition = parse_expression();
8556 rem_anchor_token(')');
8559 add_anchor_token(T_else);
8560 statement->ifs.true_statement = parse_statement();
8561 rem_anchor_token(T_else);
8563 if (token.type == T_else) {
8565 statement->ifs.false_statement = parse_statement();
8572 return create_invalid_statement();
8576 * Check that all enums are handled in a switch.
8578 * @param statement the switch statement to check
8580 static void check_enum_cases(const switch_statement_t *statement) {
8581 const type_t *type = skip_typeref(statement->expression->base.type);
8582 if (! is_type_enum(type))
8584 const enum_type_t *enumt = &type->enumt;
8586 /* if we have a default, no warnings */
8587 if (statement->default_label != NULL)
8590 /* FIXME: calculation of value should be done while parsing */
8591 const declaration_t *declaration;
8592 long last_value = -1;
8593 for (declaration = enumt->declaration->next;
8594 declaration != NULL && declaration->storage_class == STORAGE_CLASS_ENUM_ENTRY;
8595 declaration = declaration->next) {
8596 const expression_t *expression = declaration->init.enum_value;
8597 long value = expression != NULL ? fold_constant(expression) : last_value + 1;
8599 for (const case_label_statement_t *l = statement->first_case; l != NULL; l = l->next) {
8600 if (l->expression == NULL)
8602 if (l->first_case <= value && value <= l->last_case) {
8608 warningf(&statement->base.source_position,
8609 "enumeration value '%Y' not handled in switch", declaration->symbol);
8616 * Parse a switch statement.
8618 static statement_t *parse_switch(void)
8622 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
8623 statement->base.source_position = token.source_position;
8625 PUSH_PARENT(statement);
8628 add_anchor_token(')');
8629 expression_t *const expr = parse_expression();
8630 type_t * type = skip_typeref(expr->base.type);
8631 if (is_type_integer(type)) {
8632 type = promote_integer(type);
8633 } else if (is_type_valid(type)) {
8634 errorf(&expr->base.source_position,
8635 "switch quantity is not an integer, but '%T'", type);
8636 type = type_error_type;
8638 statement->switchs.expression = create_implicit_cast(expr, type);
8640 rem_anchor_token(')');
8642 switch_statement_t *rem = current_switch;
8643 current_switch = &statement->switchs;
8644 statement->switchs.body = parse_statement();
8645 current_switch = rem;
8647 if (warning.switch_default &&
8648 statement->switchs.default_label == NULL) {
8649 warningf(&statement->base.source_position, "switch has no default case");
8651 if (warning.switch_enum)
8652 check_enum_cases(&statement->switchs);
8658 return create_invalid_statement();
8661 static statement_t *parse_loop_body(statement_t *const loop)
8663 statement_t *const rem = current_loop;
8664 current_loop = loop;
8666 statement_t *const body = parse_statement();
8673 * Parse a while statement.
8675 static statement_t *parse_while(void)
8679 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
8680 statement->base.source_position = token.source_position;
8682 PUSH_PARENT(statement);
8685 add_anchor_token(')');
8686 statement->whiles.condition = parse_expression();
8687 rem_anchor_token(')');
8690 statement->whiles.body = parse_loop_body(statement);
8696 return create_invalid_statement();
8700 * Parse a do statement.
8702 static statement_t *parse_do(void)
8706 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
8707 statement->base.source_position = token.source_position;
8709 PUSH_PARENT(statement)
8711 add_anchor_token(T_while);
8712 statement->do_while.body = parse_loop_body(statement);
8713 rem_anchor_token(T_while);
8717 add_anchor_token(')');
8718 statement->do_while.condition = parse_expression();
8719 rem_anchor_token(')');
8727 return create_invalid_statement();
8731 * Parse a for statement.
8733 static statement_t *parse_for(void)
8737 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
8738 statement->base.source_position = token.source_position;
8740 PUSH_PARENT(statement);
8742 int top = environment_top();
8743 scope_t *last_scope = scope;
8744 set_scope(&statement->fors.scope);
8747 add_anchor_token(')');
8749 if (token.type != ';') {
8750 if (is_declaration_specifier(&token, false)) {
8751 parse_declaration(record_declaration);
8753 add_anchor_token(';');
8754 expression_t *const init = parse_expression();
8755 statement->fors.initialisation = init;
8756 if (warning.unused_value && !expression_has_effect(init)) {
8757 warningf(&init->base.source_position,
8758 "initialisation of 'for'-statement has no effect");
8760 rem_anchor_token(';');
8767 if (token.type != ';') {
8768 add_anchor_token(';');
8769 statement->fors.condition = parse_expression();
8770 rem_anchor_token(';');
8773 if (token.type != ')') {
8774 expression_t *const step = parse_expression();
8775 statement->fors.step = step;
8776 if (warning.unused_value && !expression_has_effect(step)) {
8777 warningf(&step->base.source_position,
8778 "step of 'for'-statement has no effect");
8781 rem_anchor_token(')');
8783 statement->fors.body = parse_loop_body(statement);
8785 assert(scope == &statement->fors.scope);
8786 set_scope(last_scope);
8787 environment_pop_to(top);
8794 rem_anchor_token(')');
8795 assert(scope == &statement->fors.scope);
8796 set_scope(last_scope);
8797 environment_pop_to(top);
8799 return create_invalid_statement();
8803 * Parse a goto statement.
8805 static statement_t *parse_goto(void)
8809 if (token.type != T_IDENTIFIER) {
8810 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
8814 symbol_t *symbol = token.v.symbol;
8817 declaration_t *label = get_label(symbol);
8819 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
8820 statement->base.source_position = token.source_position;
8822 statement->gotos.label = label;
8824 /* remember the goto's in a list for later checking */
8825 if (goto_last == NULL) {
8826 goto_first = &statement->gotos;
8828 goto_last->next = &statement->gotos;
8830 goto_last = &statement->gotos;
8836 return create_invalid_statement();
8840 * Parse a continue statement.
8842 static statement_t *parse_continue(void)
8844 statement_t *statement;
8845 if (current_loop == NULL) {
8846 errorf(HERE, "continue statement not within loop");
8847 statement = create_invalid_statement();
8849 statement = allocate_statement_zero(STATEMENT_CONTINUE);
8851 statement->base.source_position = token.source_position;
8859 return create_invalid_statement();
8863 * Parse a break statement.
8865 static statement_t *parse_break(void)
8867 statement_t *statement;
8868 if (current_switch == NULL && current_loop == NULL) {
8869 errorf(HERE, "break statement not within loop or switch");
8870 statement = create_invalid_statement();
8872 statement = allocate_statement_zero(STATEMENT_BREAK);
8874 statement->base.source_position = token.source_position;
8882 return create_invalid_statement();
8886 * Parse a __leave statement.
8888 static statement_t *parse_leave(void)
8890 statement_t *statement;
8891 if (current_try == NULL) {
8892 errorf(HERE, "__leave statement not within __try");
8893 statement = create_invalid_statement();
8895 statement = allocate_statement_zero(STATEMENT_LEAVE);
8897 statement->base.source_position = token.source_position;
8905 return create_invalid_statement();
8909 * Check if a given declaration represents a local variable.
8911 static bool is_local_var_declaration(const declaration_t *declaration)
8913 switch ((storage_class_tag_t) declaration->storage_class) {
8914 case STORAGE_CLASS_AUTO:
8915 case STORAGE_CLASS_REGISTER: {
8916 const type_t *type = skip_typeref(declaration->type);
8917 if (is_type_function(type)) {
8929 * Check if a given declaration represents a variable.
8931 static bool is_var_declaration(const declaration_t *declaration)
8933 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8936 const type_t *type = skip_typeref(declaration->type);
8937 return !is_type_function(type);
8941 * Check if a given expression represents a local variable.
8943 static bool is_local_variable(const expression_t *expression)
8945 if (expression->base.kind != EXPR_REFERENCE) {
8948 const declaration_t *declaration = expression->reference.declaration;
8949 return is_local_var_declaration(declaration);
8953 * Check if a given expression represents a local variable and
8954 * return its declaration then, else return NULL.
8956 declaration_t *expr_is_variable(const expression_t *expression)
8958 if (expression->base.kind != EXPR_REFERENCE) {
8961 declaration_t *declaration = expression->reference.declaration;
8962 if (is_var_declaration(declaration))
8968 * Parse a return statement.
8970 static statement_t *parse_return(void)
8972 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8973 statement->base.source_position = token.source_position;
8977 expression_t *return_value = NULL;
8978 if (token.type != ';') {
8979 return_value = parse_expression();
8983 const type_t *const func_type = current_function->type;
8984 assert(is_type_function(func_type));
8985 type_t *const return_type = skip_typeref(func_type->function.return_type);
8987 if (return_value != NULL) {
8988 type_t *return_value_type = skip_typeref(return_value->base.type);
8990 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8991 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8992 warningf(&statement->base.source_position,
8993 "'return' with a value, in function returning void");
8994 return_value = NULL;
8996 assign_error_t error = semantic_assign(return_type, return_value);
8997 report_assign_error(error, return_type, return_value, "'return'",
8998 &statement->base.source_position);
8999 return_value = create_implicit_cast(return_value, return_type);
9001 /* check for returning address of a local var */
9002 if (return_value != NULL &&
9003 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
9004 const expression_t *expression = return_value->unary.value;
9005 if (is_local_variable(expression)) {
9006 warningf(&statement->base.source_position,
9007 "function returns address of local variable");
9011 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
9012 warningf(&statement->base.source_position,
9013 "'return' without value, in function returning non-void");
9016 statement->returns.value = return_value;
9020 return create_invalid_statement();
9024 * Parse a declaration statement.
9026 static statement_t *parse_declaration_statement(void)
9028 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
9030 statement->base.source_position = token.source_position;
9032 declaration_t *before = last_declaration;
9033 parse_declaration(record_declaration);
9035 if (before == NULL) {
9036 statement->declaration.declarations_begin = scope->declarations;
9038 statement->declaration.declarations_begin = before->next;
9040 statement->declaration.declarations_end = last_declaration;
9046 * Parse an expression statement, ie. expr ';'.
9048 static statement_t *parse_expression_statement(void)
9050 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
9052 statement->base.source_position = token.source_position;
9053 expression_t *const expr = parse_expression();
9054 statement->expression.expression = expr;
9060 return create_invalid_statement();
9064 * Parse a microsoft __try { } __finally { } or
9065 * __try{ } __except() { }
9067 static statement_t *parse_ms_try_statment(void)
9069 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
9071 statement->base.source_position = token.source_position;
9074 ms_try_statement_t *rem = current_try;
9075 current_try = &statement->ms_try;
9076 statement->ms_try.try_statement = parse_compound_statement(false);
9079 if (token.type == T___except) {
9082 add_anchor_token(')');
9083 expression_t *const expr = parse_expression();
9084 type_t * type = skip_typeref(expr->base.type);
9085 if (is_type_integer(type)) {
9086 type = promote_integer(type);
9087 } else if (is_type_valid(type)) {
9088 errorf(&expr->base.source_position,
9089 "__expect expression is not an integer, but '%T'", type);
9090 type = type_error_type;
9092 statement->ms_try.except_expression = create_implicit_cast(expr, type);
9093 rem_anchor_token(')');
9095 statement->ms_try.final_statement = parse_compound_statement(false);
9096 } else if (token.type == T__finally) {
9098 statement->ms_try.final_statement = parse_compound_statement(false);
9100 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
9101 return create_invalid_statement();
9105 return create_invalid_statement();
9108 static statement_t *parse_empty_statement(void)
9110 if (warning.empty_statement) {
9111 warningf(HERE, "statement is empty");
9113 statement_t *const statement = create_empty_statement();
9119 * Parse a statement.
9120 * There's also parse_statement() which additionally checks for
9121 * "statement has no effect" warnings
9123 static statement_t *intern_parse_statement(void)
9125 statement_t *statement = NULL;
9127 /* declaration or statement */
9128 add_anchor_token(';');
9129 switch (token.type) {
9130 case T_IDENTIFIER: {
9131 token_type_t la1_type = (token_type_t)look_ahead(1)->type;
9132 if (la1_type == ':') {
9133 statement = parse_label_statement();
9134 } else if (is_typedef_symbol(token.v.symbol)) {
9135 statement = parse_declaration_statement();
9136 } else switch (la1_type) {
9140 statement = parse_declaration_statement();
9144 statement = parse_expression_statement();
9150 case T___extension__:
9151 /* This can be a prefix to a declaration or an expression statement.
9152 * We simply eat it now and parse the rest with tail recursion. */
9155 } while (token.type == T___extension__);
9156 statement = parse_statement();
9160 statement = parse_declaration_statement();
9163 case ';': statement = parse_empty_statement(); break;
9164 case '{': statement = parse_compound_statement(false); break;
9165 case T___leave: statement = parse_leave(); break;
9166 case T___try: statement = parse_ms_try_statment(); break;
9167 case T_asm: statement = parse_asm_statement(); break;
9168 case T_break: statement = parse_break(); break;
9169 case T_case: statement = parse_case_statement(); break;
9170 case T_continue: statement = parse_continue(); break;
9171 case T_default: statement = parse_default_statement(); break;
9172 case T_do: statement = parse_do(); break;
9173 case T_for: statement = parse_for(); break;
9174 case T_goto: statement = parse_goto(); break;
9175 case T_if: statement = parse_if (); break;
9176 case T_return: statement = parse_return(); break;
9177 case T_switch: statement = parse_switch(); break;
9178 case T_while: statement = parse_while(); break;
9179 default: statement = parse_expression_statement(); break;
9181 rem_anchor_token(';');
9183 assert(statement != NULL
9184 && statement->base.source_position.input_name != NULL);
9190 * parse a statement and emits "statement has no effect" warning if needed
9191 * (This is really a wrapper around intern_parse_statement with check for 1
9192 * single warning. It is needed, because for statement expressions we have
9193 * to avoid the warning on the last statement)
9195 static statement_t *parse_statement(void)
9197 statement_t *statement = intern_parse_statement();
9199 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
9200 expression_t *expression = statement->expression.expression;
9201 if (!expression_has_effect(expression)) {
9202 warningf(&expression->base.source_position,
9203 "statement has no effect");
9211 * Parse a compound statement.
9213 static statement_t *parse_compound_statement(bool inside_expression_statement)
9215 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
9216 statement->base.source_position = token.source_position;
9218 PUSH_PARENT(statement);
9221 add_anchor_token('}');
9223 int top = environment_top();
9224 scope_t *last_scope = scope;
9225 set_scope(&statement->compound.scope);
9227 statement_t **anchor = &statement->compound.statements;
9228 bool only_decls_so_far = true;
9229 while (token.type != '}' && token.type != T_EOF) {
9230 statement_t *sub_statement = intern_parse_statement();
9231 if (is_invalid_statement(sub_statement)) {
9232 /* an error occurred. if we are at an anchor, return */
9238 if (warning.declaration_after_statement) {
9239 if (sub_statement->kind != STATEMENT_DECLARATION) {
9240 only_decls_so_far = false;
9241 } else if (!only_decls_so_far) {
9242 warningf(&sub_statement->base.source_position,
9243 "ISO C90 forbids mixed declarations and code");
9247 *anchor = sub_statement;
9249 while (sub_statement->base.next != NULL)
9250 sub_statement = sub_statement->base.next;
9252 anchor = &sub_statement->base.next;
9255 if (token.type == '}') {
9258 errorf(&statement->base.source_position,
9259 "end of file while looking for closing '}'");
9262 /* look over all statements again to produce no effect warnings */
9263 if (warning.unused_value) {
9264 statement_t *sub_statement = statement->compound.statements;
9265 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
9266 if (sub_statement->kind != STATEMENT_EXPRESSION)
9268 /* don't emit a warning for the last expression in an expression
9269 * statement as it has always an effect */
9270 if (inside_expression_statement && sub_statement->base.next == NULL)
9273 expression_t *expression = sub_statement->expression.expression;
9274 if (!expression_has_effect(expression)) {
9275 warningf(&expression->base.source_position,
9276 "statement has no effect");
9282 rem_anchor_token('}');
9283 assert(scope == &statement->compound.scope);
9284 set_scope(last_scope);
9285 environment_pop_to(top);
9292 * Initialize builtin types.
9294 static void initialize_builtin_types(void)
9296 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
9297 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
9298 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
9299 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
9300 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
9301 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
9302 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", opt_short_wchar_t ? type_unsigned_short : type_int);
9303 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
9305 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
9306 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
9307 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
9308 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
9310 /* const version of wchar_t */
9311 type_const_wchar_t = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
9312 type_const_wchar_t->typedeft.declaration = type_wchar_t->typedeft.declaration;
9313 type_const_wchar_t->base.qualifiers |= TYPE_QUALIFIER_CONST;
9315 type_const_wchar_t_ptr = make_pointer_type(type_const_wchar_t, TYPE_QUALIFIER_NONE);
9319 * Check for unused global static functions and variables
9321 static void check_unused_globals(void)
9323 if (!warning.unused_function && !warning.unused_variable)
9326 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
9328 decl->modifiers & DM_UNUSED ||
9329 decl->modifiers & DM_USED ||
9330 decl->storage_class != STORAGE_CLASS_STATIC)
9333 type_t *const type = decl->type;
9335 if (is_type_function(skip_typeref(type))) {
9336 if (!warning.unused_function || decl->is_inline)
9339 s = (decl->init.statement != NULL ? "defined" : "declared");
9341 if (!warning.unused_variable)
9347 warningf(&decl->source_position, "'%#T' %s but not used",
9348 type, decl->symbol, s);
9352 static void parse_global_asm(void)
9357 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
9358 statement->base.source_position = token.source_position;
9359 statement->asms.asm_text = parse_string_literals();
9360 statement->base.next = unit->global_asm;
9361 unit->global_asm = statement;
9370 * Parse a translation unit.
9372 static void parse_translation_unit(void)
9374 for (;;) switch (token.type) {
9377 case T___extension__:
9378 parse_external_declaration();
9389 /* TODO error in strict mode */
9390 warningf(HERE, "stray ';' outside of function");
9395 errorf(HERE, "stray %K outside of function", &token);
9396 if (token.type == '(' || token.type == '{' || token.type == '[')
9397 eat_until_matching_token(token.type);
9406 * @return the translation unit or NULL if errors occurred.
9408 void start_parsing(void)
9410 environment_stack = NEW_ARR_F(stack_entry_t, 0);
9411 label_stack = NEW_ARR_F(stack_entry_t, 0);
9412 diagnostic_count = 0;
9416 type_set_output(stderr);
9417 ast_set_output(stderr);
9419 assert(unit == NULL);
9420 unit = allocate_ast_zero(sizeof(unit[0]));
9422 assert(global_scope == NULL);
9423 global_scope = &unit->scope;
9425 assert(scope == NULL);
9426 set_scope(&unit->scope);
9428 initialize_builtin_types();
9431 translation_unit_t *finish_parsing(void)
9433 assert(scope == &unit->scope);
9435 last_declaration = NULL;
9437 assert(global_scope == &unit->scope);
9438 check_unused_globals();
9439 global_scope = NULL;
9441 DEL_ARR_F(environment_stack);
9442 DEL_ARR_F(label_stack);
9444 translation_unit_t *result = unit;
9451 lookahead_bufpos = 0;
9452 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
9455 parse_translation_unit();
9459 * Initialize the parser.
9461 void init_parser(void)
9464 /* add predefined symbols for extended-decl-modifier */
9465 sym_align = symbol_table_insert("align");
9466 sym_allocate = symbol_table_insert("allocate");
9467 sym_dllimport = symbol_table_insert("dllimport");
9468 sym_dllexport = symbol_table_insert("dllexport");
9469 sym_naked = symbol_table_insert("naked");
9470 sym_noinline = symbol_table_insert("noinline");
9471 sym_noreturn = symbol_table_insert("noreturn");
9472 sym_nothrow = symbol_table_insert("nothrow");
9473 sym_novtable = symbol_table_insert("novtable");
9474 sym_property = symbol_table_insert("property");
9475 sym_get = symbol_table_insert("get");
9476 sym_put = symbol_table_insert("put");
9477 sym_selectany = symbol_table_insert("selectany");
9478 sym_thread = symbol_table_insert("thread");
9479 sym_uuid = symbol_table_insert("uuid");
9480 sym_deprecated = symbol_table_insert("deprecated");
9481 sym_restrict = symbol_table_insert("restrict");
9482 sym_noalias = symbol_table_insert("noalias");
9484 memset(token_anchor_set, 0, sizeof(token_anchor_set));
9486 init_expression_parsers();
9487 obstack_init(&temp_obst);
9489 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
9490 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
9494 * Terminate the parser.
9496 void exit_parser(void)
9498 obstack_free(&temp_obst, NULL);