2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct argument_list_t argument_list_t;
52 struct argument_list_t {
54 argument_list_t *next;
57 typedef struct gnu_attribute_t gnu_attribute_t;
58 struct gnu_attribute_t {
59 gnu_attribute_kind_t kind; /**< The kind of the GNU attribute. */
60 gnu_attribute_t *next;
61 bool invalid; /**< Set if this attribute had argument errors, */
62 bool have_arguments; /**< True, if this attribute has arguments. */
66 atomic_type_kind_t akind;
67 long argument; /**< Single argument. */
68 argument_list_t *arguments; /**< List of argument expressions. */
72 typedef struct declaration_specifiers_t declaration_specifiers_t;
73 struct declaration_specifiers_t {
74 source_position_t source_position;
75 unsigned char declared_storage_class;
76 unsigned char alignment; /**< Alignment, 0 if not set. */
77 unsigned int is_inline : 1;
78 unsigned int deprecated : 1;
79 decl_modifiers_t modifiers; /**< declaration modifiers */
80 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
81 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
82 symbol_t *get_property_sym; /**< the name of the get property if set. */
83 symbol_t *put_property_sym; /**< the name of the put property if set. */
88 * An environment for parsing initializers (and compound literals).
90 typedef struct parse_initializer_env_t {
91 type_t *type; /**< the type of the initializer. In case of an
92 array type with unspecified size this gets
93 adjusted to the actual size. */
94 declaration_t *declaration; /**< the declaration that is initialized if any */
95 bool must_be_constant;
96 } parse_initializer_env_t;
98 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
100 static token_t token;
101 static token_t lookahead_buffer[MAX_LOOKAHEAD];
102 static int lookahead_bufpos;
103 static stack_entry_t *environment_stack = NULL;
104 static stack_entry_t *label_stack = NULL;
105 static scope_t *global_scope = NULL;
106 static scope_t *scope = NULL;
107 static declaration_t *last_declaration = NULL;
108 static declaration_t *current_function = NULL;
109 static switch_statement_t *current_switch = NULL;
110 static statement_t *current_loop = NULL;
111 static ms_try_statement_t *current_try = NULL;
112 static goto_statement_t *goto_first = NULL;
113 static goto_statement_t *goto_last = NULL;
114 static label_statement_t *label_first = NULL;
115 static label_statement_t *label_last = NULL;
116 static translation_unit_t *unit = NULL;
117 static struct obstack temp_obst;
119 static source_position_t null_position = { NULL, 0 };
121 /* symbols for Microsoft extended-decl-modifier */
122 static const symbol_t *sym_align = NULL;
123 static const symbol_t *sym_allocate = NULL;
124 static const symbol_t *sym_dllimport = NULL;
125 static const symbol_t *sym_dllexport = NULL;
126 static const symbol_t *sym_naked = NULL;
127 static const symbol_t *sym_noinline = NULL;
128 static const symbol_t *sym_noreturn = NULL;
129 static const symbol_t *sym_nothrow = NULL;
130 static const symbol_t *sym_novtable = NULL;
131 static const symbol_t *sym_property = NULL;
132 static const symbol_t *sym_get = NULL;
133 static const symbol_t *sym_put = NULL;
134 static const symbol_t *sym_selectany = NULL;
135 static const symbol_t *sym_thread = NULL;
136 static const symbol_t *sym_uuid = NULL;
137 static const symbol_t *sym_deprecated = NULL;
138 static const symbol_t *sym_restrict = NULL;
139 static const symbol_t *sym_noalias = NULL;
141 /** The token anchor set */
142 static unsigned char token_anchor_set[T_LAST_TOKEN];
144 /** The current source position. */
145 #define HERE (&token.source_position)
147 static type_t *type_valist;
149 static statement_t *parse_compound_statement(bool inside_expression_statement);
150 static statement_t *parse_statement(void);
152 static expression_t *parse_sub_expression(unsigned precedence);
153 static expression_t *parse_expression(void);
154 static type_t *parse_typename(void);
156 static void parse_compound_type_entries(declaration_t *compound_declaration);
157 static declaration_t *parse_declarator(
158 const declaration_specifiers_t *specifiers, bool may_be_abstract);
159 static declaration_t *record_declaration(declaration_t *declaration);
161 static void semantic_comparison(binary_expression_t *expression);
163 #define STORAGE_CLASSES \
170 #define TYPE_QUALIFIERS \
175 case T__forceinline: \
176 case T___attribute__:
178 #ifdef PROVIDE_COMPLEX
179 #define COMPLEX_SPECIFIERS \
181 #define IMAGINARY_SPECIFIERS \
184 #define COMPLEX_SPECIFIERS
185 #define IMAGINARY_SPECIFIERS
188 #define TYPE_SPECIFIERS \
203 case T___builtin_va_list: \
208 #define DECLARATION_START \
213 #define TYPENAME_START \
218 * Allocate an AST node with given size and
219 * initialize all fields with zero.
221 static void *allocate_ast_zero(size_t size)
223 void *res = allocate_ast(size);
224 memset(res, 0, size);
228 static declaration_t *allocate_declaration_zero(void)
230 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
231 declaration->type = type_error_type;
232 declaration->alignment = 0;
237 * Returns the size of a statement node.
239 * @param kind the statement kind
241 static size_t get_statement_struct_size(statement_kind_t kind)
243 static const size_t sizes[] = {
244 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
245 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
246 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
247 [STATEMENT_RETURN] = sizeof(return_statement_t),
248 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
249 [STATEMENT_IF] = sizeof(if_statement_t),
250 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
251 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
252 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
253 [STATEMENT_BREAK] = sizeof(statement_base_t),
254 [STATEMENT_GOTO] = sizeof(goto_statement_t),
255 [STATEMENT_LABEL] = sizeof(label_statement_t),
256 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
257 [STATEMENT_WHILE] = sizeof(while_statement_t),
258 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
259 [STATEMENT_FOR] = sizeof(for_statement_t),
260 [STATEMENT_ASM] = sizeof(asm_statement_t),
261 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
262 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
264 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
265 assert(sizes[kind] != 0);
270 * Returns the size of an expression node.
272 * @param kind the expression kind
274 static size_t get_expression_struct_size(expression_kind_t kind)
276 static const size_t sizes[] = {
277 [EXPR_INVALID] = sizeof(expression_base_t),
278 [EXPR_REFERENCE] = sizeof(reference_expression_t),
279 [EXPR_CONST] = sizeof(const_expression_t),
280 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
281 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
282 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
283 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
284 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
285 [EXPR_CALL] = sizeof(call_expression_t),
286 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
287 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
288 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
289 [EXPR_SELECT] = sizeof(select_expression_t),
290 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
291 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
292 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
293 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
294 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
295 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
296 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
297 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
298 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
299 [EXPR_VA_START] = sizeof(va_start_expression_t),
300 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
301 [EXPR_STATEMENT] = sizeof(statement_expression_t),
303 if (kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
304 return sizes[EXPR_UNARY_FIRST];
306 if (kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
307 return sizes[EXPR_BINARY_FIRST];
309 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
310 assert(sizes[kind] != 0);
315 * Allocate a statement node of given kind and initialize all
318 static statement_t *allocate_statement_zero(statement_kind_t kind)
320 size_t size = get_statement_struct_size(kind);
321 statement_t *res = allocate_ast_zero(size);
323 res->base.kind = kind;
328 * Allocate an expression node of given kind and initialize all
331 static expression_t *allocate_expression_zero(expression_kind_t kind)
333 size_t size = get_expression_struct_size(kind);
334 expression_t *res = allocate_ast_zero(size);
336 res->base.kind = kind;
337 res->base.type = type_error_type;
342 * Creates a new invalid expression.
344 static expression_t *create_invalid_expression(void)
346 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
347 expression->base.source_position = token.source_position;
352 * Creates a new invalid statement.
354 static statement_t *create_invalid_statement(void)
356 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
357 statement->base.source_position = token.source_position;
362 * Allocate a new empty statement.
364 static statement_t *create_empty_statement(void)
366 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
367 statement->base.source_position = token.source_position;
372 * Returns the size of a type node.
374 * @param kind the type kind
376 static size_t get_type_struct_size(type_kind_t kind)
378 static const size_t sizes[] = {
379 [TYPE_ATOMIC] = sizeof(atomic_type_t),
380 [TYPE_COMPLEX] = sizeof(complex_type_t),
381 [TYPE_IMAGINARY] = sizeof(imaginary_type_t),
382 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
383 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
384 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
385 [TYPE_ENUM] = sizeof(enum_type_t),
386 [TYPE_FUNCTION] = sizeof(function_type_t),
387 [TYPE_POINTER] = sizeof(pointer_type_t),
388 [TYPE_ARRAY] = sizeof(array_type_t),
389 [TYPE_BUILTIN] = sizeof(builtin_type_t),
390 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
391 [TYPE_TYPEOF] = sizeof(typeof_type_t),
393 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
394 assert(kind <= TYPE_TYPEOF);
395 assert(sizes[kind] != 0);
400 * Allocate a type node of given kind and initialize all
403 * @param kind type kind to allocate
404 * @param source_position the source position of the type definition
406 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
408 size_t size = get_type_struct_size(kind);
409 type_t *res = obstack_alloc(type_obst, size);
410 memset(res, 0, size);
412 res->base.kind = kind;
413 res->base.source_position = *source_position;
418 * Returns the size of an initializer node.
420 * @param kind the initializer kind
422 static size_t get_initializer_size(initializer_kind_t kind)
424 static const size_t sizes[] = {
425 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
426 [INITIALIZER_STRING] = sizeof(initializer_string_t),
427 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
428 [INITIALIZER_LIST] = sizeof(initializer_list_t),
429 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
431 assert(kind < sizeof(sizes) / sizeof(*sizes));
432 assert(sizes[kind] != 0);
437 * Allocate an initializer node of given kind and initialize all
440 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
442 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
449 * Free a type from the type obstack.
451 static void free_type(void *type)
453 obstack_free(type_obst, type);
457 * Returns the index of the top element of the environment stack.
459 static size_t environment_top(void)
461 return ARR_LEN(environment_stack);
465 * Returns the index of the top element of the label stack.
467 static size_t label_top(void)
469 return ARR_LEN(label_stack);
473 * Return the next token.
475 static inline void next_token(void)
477 token = lookahead_buffer[lookahead_bufpos];
478 lookahead_buffer[lookahead_bufpos] = lexer_token;
481 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
484 print_token(stderr, &token);
485 fprintf(stderr, "\n");
490 * Return the next token with a given lookahead.
492 static inline const token_t *look_ahead(int num)
494 assert(num > 0 && num <= MAX_LOOKAHEAD);
495 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
496 return &lookahead_buffer[pos];
500 * Adds a token to the token anchor set (a multi-set).
502 static void add_anchor_token(int token_type) {
503 assert(0 <= token_type && token_type < T_LAST_TOKEN);
504 ++token_anchor_set[token_type];
507 static int save_and_reset_anchor_state(int token_type) {
508 assert(0 <= token_type && token_type < T_LAST_TOKEN);
509 int count = token_anchor_set[token_type];
510 token_anchor_set[token_type] = 0;
514 static void restore_anchor_state(int token_type, int count) {
515 assert(0 <= token_type && token_type < T_LAST_TOKEN);
516 token_anchor_set[token_type] = count;
520 * Remove a token from the token anchor set (a multi-set).
522 static void rem_anchor_token(int token_type) {
523 assert(0 <= token_type && token_type < T_LAST_TOKEN);
524 --token_anchor_set[token_type];
527 static bool at_anchor(void) {
530 return token_anchor_set[token.type];
534 * Eat tokens until a matching token is found.
536 static void eat_until_matching_token(int type) {
537 unsigned parenthesis_count = 0;
538 unsigned brace_count = 0;
539 unsigned bracket_count = 0;
543 case '(': end_token = ')'; break;
544 case '{': end_token = '}'; break;
545 case '[': end_token = ']'; break;
546 default: end_token = type; break;
549 while(token.type != end_token ||
550 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
554 case '(': ++parenthesis_count; break;
555 case '{': ++brace_count; break;
556 case '[': ++bracket_count; break;
558 if (parenthesis_count > 0)
566 if (bracket_count > 0)
577 * Eat input tokens until an anchor is found.
579 static void eat_until_anchor(void) {
580 if (token.type == T_EOF)
582 while(token_anchor_set[token.type] == 0) {
583 if (token.type == '(' || token.type == '{' || token.type == '[')
584 eat_until_matching_token(token.type);
585 if (token.type == T_EOF)
591 static void eat_block(void) {
592 eat_until_matching_token('{');
593 if (token.type == '}')
598 * eat all token until a ';' is reached or a stop token is found.
600 static void eat_statement(void) {
601 eat_until_matching_token(';');
602 if (token.type == ';')
606 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
609 * Report a parse error because an expected token was not found.
612 #if defined __GNUC__ && __GNUC__ >= 4
613 __attribute__((sentinel))
615 void parse_error_expected(const char *message, ...)
617 if (message != NULL) {
618 errorf(HERE, "%s", message);
621 va_start(ap, message);
622 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
627 * Report a type error.
629 static void type_error(const char *msg, const source_position_t *source_position,
632 errorf(source_position, "%s, but found type '%T'", msg, type);
636 * Report an incompatible type.
638 static void type_error_incompatible(const char *msg,
639 const source_position_t *source_position, type_t *type1, type_t *type2)
641 errorf(source_position, "%s, incompatible types: '%T' - '%T'",
646 * Expect the the current token is the expected token.
647 * If not, generate an error, eat the current statement,
648 * and goto the end_error label.
650 #define expect(expected) \
652 if (UNLIKELY(token.type != (expected))) { \
653 parse_error_expected(NULL, (expected), NULL); \
654 add_anchor_token(expected); \
655 eat_until_anchor(); \
656 if (token.type == expected) \
658 rem_anchor_token(expected); \
664 static void set_scope(scope_t *new_scope)
667 scope->last_declaration = last_declaration;
671 last_declaration = new_scope->last_declaration;
675 * Search a symbol in a given namespace and returns its declaration or
676 * NULL if this symbol was not found.
678 static declaration_t *get_declaration(const symbol_t *const symbol,
679 const namespace_t namespc)
681 declaration_t *declaration = symbol->declaration;
682 for( ; declaration != NULL; declaration = declaration->symbol_next) {
683 if (declaration->namespc == namespc)
691 * pushs an environment_entry on the environment stack and links the
692 * corresponding symbol to the new entry
694 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
696 symbol_t *symbol = declaration->symbol;
697 namespace_t namespc = (namespace_t) declaration->namespc;
699 /* replace/add declaration into declaration list of the symbol */
700 declaration_t *iter = symbol->declaration;
702 symbol->declaration = declaration;
704 declaration_t *iter_last = NULL;
705 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
706 /* replace an entry? */
707 if (iter->namespc == namespc) {
708 if (iter_last == NULL) {
709 symbol->declaration = declaration;
711 iter_last->symbol_next = declaration;
713 declaration->symbol_next = iter->symbol_next;
718 assert(iter_last->symbol_next == NULL);
719 iter_last->symbol_next = declaration;
723 /* remember old declaration */
725 entry.symbol = symbol;
726 entry.old_declaration = iter;
727 entry.namespc = (unsigned short) namespc;
728 ARR_APP1(stack_entry_t, *stack_ptr, entry);
731 static void environment_push(declaration_t *declaration)
733 assert(declaration->source_position.input_name != NULL);
734 assert(declaration->parent_scope != NULL);
735 stack_push(&environment_stack, declaration);
739 * Push a declaration of the label stack.
741 * @param declaration the declaration
743 static void label_push(declaration_t *declaration)
745 declaration->parent_scope = ¤t_function->scope;
746 stack_push(&label_stack, declaration);
750 * pops symbols from the environment stack until @p new_top is the top element
752 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
754 stack_entry_t *stack = *stack_ptr;
755 size_t top = ARR_LEN(stack);
758 assert(new_top <= top);
762 for(i = top; i > new_top; --i) {
763 stack_entry_t *entry = &stack[i - 1];
765 declaration_t *old_declaration = entry->old_declaration;
766 symbol_t *symbol = entry->symbol;
767 namespace_t namespc = (namespace_t)entry->namespc;
769 /* replace/remove declaration */
770 declaration_t *declaration = symbol->declaration;
771 assert(declaration != NULL);
772 if (declaration->namespc == namespc) {
773 if (old_declaration == NULL) {
774 symbol->declaration = declaration->symbol_next;
776 symbol->declaration = old_declaration;
779 declaration_t *iter_last = declaration;
780 declaration_t *iter = declaration->symbol_next;
781 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
782 /* replace an entry? */
783 if (iter->namespc == namespc) {
784 assert(iter_last != NULL);
785 iter_last->symbol_next = old_declaration;
786 if (old_declaration != NULL) {
787 old_declaration->symbol_next = iter->symbol_next;
792 assert(iter != NULL);
796 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
799 static void environment_pop_to(size_t new_top)
801 stack_pop_to(&environment_stack, new_top);
805 * Pop all entries on the label stack until the new_top
808 * @param new_top the new stack top
810 static void label_pop_to(size_t new_top)
812 stack_pop_to(&label_stack, new_top);
816 static int get_rank(const type_t *type)
818 assert(!is_typeref(type));
819 /* The C-standard allows promoting enums to int or unsigned int (see § 7.2.2
820 * and esp. footnote 108). However we can't fold constants (yet), so we
821 * can't decide whether unsigned int is possible, while int always works.
822 * (unsigned int would be preferable when possible... for stuff like
823 * struct { enum { ... } bla : 4; } ) */
824 if (type->kind == TYPE_ENUM)
825 return ATOMIC_TYPE_INT;
827 assert(type->kind == TYPE_ATOMIC);
828 return type->atomic.akind;
831 static type_t *promote_integer(type_t *type)
833 if (type->kind == TYPE_BITFIELD)
834 type = type->bitfield.base_type;
836 if (get_rank(type) < ATOMIC_TYPE_INT)
843 * Create a cast expression.
845 * @param expression the expression to cast
846 * @param dest_type the destination type
848 static expression_t *create_cast_expression(expression_t *expression,
851 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
853 cast->unary.value = expression;
854 cast->base.type = dest_type;
860 * Check if a given expression represents the 0 pointer constant.
862 static bool is_null_pointer_constant(const expression_t *expression)
864 /* skip void* cast */
865 if (expression->kind == EXPR_UNARY_CAST
866 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
867 expression = expression->unary.value;
870 /* TODO: not correct yet, should be any constant integer expression
871 * which evaluates to 0 */
872 if (expression->kind != EXPR_CONST)
875 type_t *const type = skip_typeref(expression->base.type);
876 if (!is_type_integer(type))
879 return expression->conste.v.int_value == 0;
883 * Create an implicit cast expression.
885 * @param expression the expression to cast
886 * @param dest_type the destination type
888 static expression_t *create_implicit_cast(expression_t *expression,
891 type_t *const source_type = expression->base.type;
893 if (source_type == dest_type)
896 return create_cast_expression(expression, dest_type);
899 typedef enum assign_error_t {
901 ASSIGN_ERROR_INCOMPATIBLE,
902 ASSIGN_ERROR_POINTER_QUALIFIER_MISSING,
903 ASSIGN_WARNING_POINTER_INCOMPATIBLE,
904 ASSIGN_WARNING_POINTER_FROM_INT,
905 ASSIGN_WARNING_INT_FROM_POINTER
908 static void report_assign_error(assign_error_t error, type_t *orig_type_left,
909 const expression_t *const right,
911 const source_position_t *source_position)
913 type_t *const orig_type_right = right->base.type;
914 type_t *const type_left = skip_typeref(orig_type_left);
915 type_t *const type_right = skip_typeref(orig_type_right);
920 case ASSIGN_ERROR_INCOMPATIBLE:
921 errorf(source_position,
922 "destination type '%T' in %s is incompatible with type '%T'",
923 orig_type_left, context, orig_type_right);
926 case ASSIGN_ERROR_POINTER_QUALIFIER_MISSING: {
927 type_t *points_to_left
928 = skip_typeref(type_left->pointer.points_to);
929 type_t *points_to_right
930 = skip_typeref(type_right->pointer.points_to);
932 /* the left type has all qualifiers from the right type */
933 unsigned missing_qualifiers
934 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
935 errorf(source_position,
936 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type",
937 orig_type_left, context, orig_type_right, missing_qualifiers);
941 case ASSIGN_WARNING_POINTER_INCOMPATIBLE:
942 warningf(source_position,
943 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
944 orig_type_left, context, right, orig_type_right);
947 case ASSIGN_WARNING_POINTER_FROM_INT:
948 warningf(source_position,
949 "%s makes integer '%T' from pointer '%T' without a cast",
950 context, orig_type_left, orig_type_right);
953 case ASSIGN_WARNING_INT_FROM_POINTER:
954 warningf(source_position,
955 "%s makes integer '%T' from pointer '%T' without a cast",
956 context, orig_type_left, orig_type_right);
960 panic("invalid error value");
964 /** Implements the rules from § 6.5.16.1 */
965 static assign_error_t semantic_assign(type_t *orig_type_left,
966 const expression_t *const right)
968 type_t *const orig_type_right = right->base.type;
969 type_t *const type_left = skip_typeref(orig_type_left);
970 type_t *const type_right = skip_typeref(orig_type_right);
972 if (is_type_pointer(type_left)) {
973 if (is_null_pointer_constant(right)) {
974 return ASSIGN_SUCCESS;
975 } else if (is_type_pointer(type_right)) {
976 type_t *points_to_left
977 = skip_typeref(type_left->pointer.points_to);
978 type_t *points_to_right
979 = skip_typeref(type_right->pointer.points_to);
981 /* the left type has all qualifiers from the right type */
982 unsigned missing_qualifiers
983 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
984 if (missing_qualifiers != 0) {
985 return ASSIGN_ERROR_POINTER_QUALIFIER_MISSING;
988 points_to_left = get_unqualified_type(points_to_left);
989 points_to_right = get_unqualified_type(points_to_right);
991 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
992 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
993 return ASSIGN_SUCCESS;
996 if (!types_compatible(points_to_left, points_to_right)) {
997 return ASSIGN_WARNING_POINTER_INCOMPATIBLE;
1000 return ASSIGN_SUCCESS;
1001 } else if (is_type_integer(type_right)) {
1002 return ASSIGN_WARNING_POINTER_FROM_INT;
1004 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
1005 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
1006 && is_type_pointer(type_right))) {
1007 return ASSIGN_SUCCESS;
1008 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
1009 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
1010 type_t *const unqual_type_left = get_unqualified_type(type_left);
1011 type_t *const unqual_type_right = get_unqualified_type(type_right);
1012 if (types_compatible(unqual_type_left, unqual_type_right)) {
1013 return ASSIGN_SUCCESS;
1015 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
1016 return ASSIGN_WARNING_INT_FROM_POINTER;
1019 if (!is_type_valid(type_left) || !is_type_valid(type_right))
1020 return ASSIGN_SUCCESS;
1022 return ASSIGN_ERROR_INCOMPATIBLE;
1025 static expression_t *parse_constant_expression(void)
1027 /* start parsing at precedence 7 (conditional expression) */
1028 expression_t *result = parse_sub_expression(7);
1030 if (!is_constant_expression(result)) {
1031 errorf(&result->base.source_position,
1032 "expression '%E' is not constant\n", result);
1038 static expression_t *parse_assignment_expression(void)
1040 /* start parsing at precedence 2 (assignment expression) */
1041 return parse_sub_expression(2);
1044 static type_t *make_global_typedef(const char *name, type_t *type)
1046 symbol_t *const symbol = symbol_table_insert(name);
1048 declaration_t *const declaration = allocate_declaration_zero();
1049 declaration->namespc = NAMESPACE_NORMAL;
1050 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
1051 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
1052 declaration->type = type;
1053 declaration->symbol = symbol;
1054 declaration->source_position = builtin_source_position;
1056 record_declaration(declaration);
1058 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
1059 typedef_type->typedeft.declaration = declaration;
1061 return typedef_type;
1064 static string_t parse_string_literals(void)
1066 assert(token.type == T_STRING_LITERAL);
1067 string_t result = token.v.string;
1071 while (token.type == T_STRING_LITERAL) {
1072 result = concat_strings(&result, &token.v.string);
1079 static const char *gnu_attribute_names[GNU_AK_LAST] = {
1080 [GNU_AK_CONST] = "const",
1081 [GNU_AK_VOLATILE] = "volatile",
1082 [GNU_AK_CDECL] = "cdecl",
1083 [GNU_AK_STDCALL] = "stdcall",
1084 [GNU_AK_FASTCALL] = "fastcall",
1085 [GNU_AK_DEPRECATED] = "deprecated",
1086 [GNU_AK_NOINLINE] = "noinline",
1087 [GNU_AK_NORETURN] = "noreturn",
1088 [GNU_AK_NAKED] = "naked",
1089 [GNU_AK_PURE] = "pure",
1090 [GNU_AK_ALWAYS_INLINE] = "always_inline",
1091 [GNU_AK_MALLOC] = "malloc",
1092 [GNU_AK_WEAK] = "weak",
1093 [GNU_AK_CONSTRUCTOR] = "constructor",
1094 [GNU_AK_DESTRUCTOR] = "destructor",
1095 [GNU_AK_NOTHROW] = "nothrow",
1096 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1097 [GNU_AK_COMMON] = "common",
1098 [GNU_AK_NOCOMMON] = "nocommon",
1099 [GNU_AK_PACKED] = "packed",
1100 [GNU_AK_SHARED] = "shared",
1101 [GNU_AK_NOTSHARED] = "notshared",
1102 [GNU_AK_USED] = "used",
1103 [GNU_AK_UNUSED] = "unused",
1104 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1105 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1106 [GNU_AK_LONGCALL] = "longcall",
1107 [GNU_AK_SHORTCALL] = "shortcall",
1108 [GNU_AK_LONG_CALL] = "long_call",
1109 [GNU_AK_SHORT_CALL] = "short_call",
1110 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1111 [GNU_AK_INTERRUPT] = "interrupt",
1112 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1113 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1114 [GNU_AK_NESTING] = "nesting",
1115 [GNU_AK_NEAR] = "near",
1116 [GNU_AK_FAR] = "far",
1117 [GNU_AK_SIGNAL] = "signal",
1118 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1119 [GNU_AK_TINY_DATA] = "tiny_data",
1120 [GNU_AK_SAVEALL] = "saveall",
1121 [GNU_AK_FLATTEN] = "flatten",
1122 [GNU_AK_SSEREGPARM] = "sseregparm",
1123 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1124 [GNU_AK_RETURN_TWICE] = "return_twice",
1125 [GNU_AK_MAY_ALIAS] = "may_alias",
1126 [GNU_AK_MS_STRUCT] = "ms_struct",
1127 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1128 [GNU_AK_DLLIMPORT] = "dllimport",
1129 [GNU_AK_DLLEXPORT] = "dllexport",
1130 [GNU_AK_ALIGNED] = "aligned",
1131 [GNU_AK_ALIAS] = "alias",
1132 [GNU_AK_SECTION] = "section",
1133 [GNU_AK_FORMAT] = "format",
1134 [GNU_AK_FORMAT_ARG] = "format_arg",
1135 [GNU_AK_WEAKREF] = "weakref",
1136 [GNU_AK_NONNULL] = "nonnull",
1137 [GNU_AK_TLS_MODEL] = "tls_model",
1138 [GNU_AK_VISIBILITY] = "visibility",
1139 [GNU_AK_REGPARM] = "regparm",
1140 [GNU_AK_MODE] = "mode",
1141 [GNU_AK_MODEL] = "model",
1142 [GNU_AK_TRAP_EXIT] = "trap_exit",
1143 [GNU_AK_SP_SWITCH] = "sp_switch",
1144 [GNU_AK_SENTINEL] = "sentinel"
1148 * compare two string, ignoring double underscores on the second.
1150 static int strcmp_underscore(const char *s1, const char *s2) {
1151 if (s2[0] == '_' && s2[1] == '_') {
1152 size_t len2 = strlen(s2);
1153 size_t len1 = strlen(s1);
1154 if (len1 == len2-4 && s2[len2-2] == '_' && s2[len2-1] == '_') {
1155 return strncmp(s1, s2+2, len2-4);
1159 return strcmp(s1, s2);
1163 * Allocate a new gnu temporal attribute.
1165 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1166 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1167 attribute->kind = kind;
1168 attribute->next = NULL;
1169 attribute->invalid = false;
1170 attribute->have_arguments = false;
1176 * parse one constant expression argument.
1178 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1179 expression_t *expression;
1180 add_anchor_token(')');
1181 expression = parse_constant_expression();
1182 rem_anchor_token(')');
1184 attribute->u.argument = fold_constant(expression);
1187 attribute->invalid = true;
1191 * parse a list of constant expressions arguments.
1193 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1194 argument_list_t **list = &attribute->u.arguments;
1195 argument_list_t *entry;
1196 expression_t *expression;
1197 add_anchor_token(')');
1198 add_anchor_token(',');
1200 expression = parse_constant_expression();
1201 entry = obstack_alloc(&temp_obst, sizeof(entry));
1202 entry->argument = fold_constant(expression);
1205 list = &entry->next;
1206 if (token.type != ',')
1210 rem_anchor_token(',');
1211 rem_anchor_token(')');
1215 attribute->invalid = true;
1219 * parse one string literal argument.
1221 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute,
1224 add_anchor_token('(');
1225 if (token.type != T_STRING_LITERAL) {
1226 parse_error_expected("while parsing attribute directive",
1227 T_STRING_LITERAL, NULL);
1230 *string = parse_string_literals();
1231 rem_anchor_token('(');
1235 attribute->invalid = true;
1239 * parse one tls model.
1241 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1242 static const char *tls_models[] = {
1248 string_t string = { NULL, 0 };
1249 parse_gnu_attribute_string_arg(attribute, &string);
1250 if (string.begin != NULL) {
1251 for(size_t i = 0; i < 4; ++i) {
1252 if (strcmp(tls_models[i], string.begin) == 0) {
1253 attribute->u.value = i;
1257 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1259 attribute->invalid = true;
1263 * parse one tls model.
1265 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1266 static const char *visibilities[] = {
1272 string_t string = { NULL, 0 };
1273 parse_gnu_attribute_string_arg(attribute, &string);
1274 if (string.begin != NULL) {
1275 for(size_t i = 0; i < 4; ++i) {
1276 if (strcmp(visibilities[i], string.begin) == 0) {
1277 attribute->u.value = i;
1281 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1283 attribute->invalid = true;
1287 * parse one (code) model.
1289 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1290 static const char *visibilities[] = {
1295 string_t string = { NULL, 0 };
1296 parse_gnu_attribute_string_arg(attribute, &string);
1297 if (string.begin != NULL) {
1298 for(int i = 0; i < 3; ++i) {
1299 if (strcmp(visibilities[i], string.begin) == 0) {
1300 attribute->u.value = i;
1304 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1306 attribute->invalid = true;
1309 static void parse_gnu_attribute_mode_arg(gnu_attribute_t *attribute)
1311 /* TODO: find out what is allowed here... */
1313 /* at least: byte, word, pointer, list of machine modes
1314 * __XXX___ is interpreted as XXX */
1315 add_anchor_token(')');
1317 if (token.type != T_IDENTIFIER) {
1318 expect(T_IDENTIFIER);
1321 /* This isn't really correct, the backend should provide a list of machine
1322 * specific modes (according to gcc philosophy that is...) */
1323 const char *symbol_str = token.v.symbol->string;
1324 if (strcmp_underscore("QI", symbol_str) == 0 ||
1325 strcmp_underscore("byte", symbol_str) == 0) {
1326 attribute->u.akind = ATOMIC_TYPE_CHAR;
1327 } else if (strcmp_underscore("HI", symbol_str) == 0) {
1328 attribute->u.akind = ATOMIC_TYPE_SHORT;
1329 } else if (strcmp_underscore("SI", symbol_str) == 0
1330 || strcmp_underscore("word", symbol_str) == 0
1331 || strcmp_underscore("pointer", symbol_str) == 0) {
1332 attribute->u.akind = ATOMIC_TYPE_INT;
1333 } else if (strcmp_underscore("DI", symbol_str) == 0) {
1334 attribute->u.akind = ATOMIC_TYPE_LONGLONG;
1336 warningf(HERE, "ignoring unknown mode '%s'", symbol_str);
1337 attribute->invalid = true;
1341 rem_anchor_token(')');
1345 attribute->invalid = true;
1349 * parse one interrupt argument.
1351 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1352 static const char *interrupts[] = {
1359 string_t string = { NULL, 0 };
1360 parse_gnu_attribute_string_arg(attribute, &string);
1361 if (string.begin != NULL) {
1362 for(size_t i = 0; i < 5; ++i) {
1363 if (strcmp(interrupts[i], string.begin) == 0) {
1364 attribute->u.value = i;
1368 errorf(HERE, "'%s' is not an interrupt", string.begin);
1370 attribute->invalid = true;
1374 * parse ( identifier, const expression, const expression )
1376 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1377 static const char *format_names[] = {
1385 if (token.type != T_IDENTIFIER) {
1386 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, NULL);
1389 const char *name = token.v.symbol->string;
1390 for(i = 0; i < 4; ++i) {
1391 if (strcmp_underscore(format_names[i], name) == 0)
1395 if (warning.attribute)
1396 warningf(HERE, "'%s' is an unrecognized format function type", name);
1401 add_anchor_token(')');
1402 add_anchor_token(',');
1403 parse_constant_expression();
1404 rem_anchor_token(',');
1405 rem_anchor_token('(');
1408 add_anchor_token(')');
1409 parse_constant_expression();
1410 rem_anchor_token('(');
1414 attribute->u.value = true;
1417 static void check_no_argument(gnu_attribute_t *attribute, const char *name)
1419 if (!attribute->have_arguments)
1422 /* should have no arguments */
1423 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1424 eat_until_matching_token('(');
1425 /* we have already consumed '(', so we stop before ')', eat it */
1427 attribute->invalid = true;
1431 * Parse one GNU attribute.
1433 * Note that attribute names can be specified WITH or WITHOUT
1434 * double underscores, ie const or __const__.
1436 * The following attributes are parsed without arguments
1461 * no_instrument_function
1462 * warn_unused_result
1479 * externally_visible
1487 * The following attributes are parsed with arguments
1488 * aligned( const expression )
1489 * alias( string literal )
1490 * section( string literal )
1491 * format( identifier, const expression, const expression )
1492 * format_arg( const expression )
1493 * tls_model( string literal )
1494 * visibility( string literal )
1495 * regparm( const expression )
1496 * model( string leteral )
1497 * trap_exit( const expression )
1498 * sp_switch( string literal )
1500 * The following attributes might have arguments
1501 * weak_ref( string literal )
1502 * non_null( const expression // ',' )
1503 * interrupt( string literal )
1504 * sentinel( constant expression )
1506 static decl_modifiers_t parse_gnu_attribute(gnu_attribute_t **attributes)
1508 gnu_attribute_t *head = *attributes;
1509 gnu_attribute_t *last = *attributes;
1510 decl_modifiers_t modifiers = 0;
1511 gnu_attribute_t *attribute;
1513 eat(T___attribute__);
1517 if (token.type != ')') {
1518 /* find the end of the list */
1520 while(last->next != NULL)
1524 /* non-empty attribute list */
1527 if (token.type == T_const) {
1529 } else if (token.type == T_volatile) {
1531 } else if (token.type == T_cdecl) {
1532 /* __attribute__((cdecl)), WITH ms mode */
1534 } else if (token.type == T_IDENTIFIER) {
1535 const symbol_t *sym = token.v.symbol;
1538 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, NULL);
1545 for(i = 0; i < GNU_AK_LAST; ++i) {
1546 if (strcmp_underscore(gnu_attribute_names[i], name) == 0)
1549 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1552 if (kind == GNU_AK_LAST) {
1553 if (warning.attribute)
1554 warningf(HERE, "'%s' attribute directive ignored", name);
1556 /* skip possible arguments */
1557 if (token.type == '(') {
1558 eat_until_matching_token(')');
1561 /* check for arguments */
1562 attribute = allocate_gnu_attribute(kind);
1563 if (token.type == '(') {
1565 if (token.type == ')') {
1566 /* empty args are allowed */
1569 attribute->have_arguments = true;
1574 case GNU_AK_VOLATILE:
1575 case GNU_AK_DEPRECATED:
1580 case GNU_AK_NOCOMMON:
1582 case GNU_AK_NOTSHARED:
1584 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1585 case GNU_AK_WARN_UNUSED_RESULT:
1586 case GNU_AK_LONGCALL:
1587 case GNU_AK_SHORTCALL:
1588 case GNU_AK_LONG_CALL:
1589 case GNU_AK_SHORT_CALL:
1590 case GNU_AK_FUNCTION_VECTOR:
1591 case GNU_AK_INTERRUPT_HANDLER:
1592 case GNU_AK_NMI_HANDLER:
1593 case GNU_AK_NESTING:
1597 case GNU_AK_EIGTHBIT_DATA:
1598 case GNU_AK_TINY_DATA:
1599 case GNU_AK_SAVEALL:
1600 case GNU_AK_FLATTEN:
1601 case GNU_AK_SSEREGPARM:
1602 case GNU_AK_EXTERNALLY_VISIBLE:
1603 case GNU_AK_RETURN_TWICE:
1604 case GNU_AK_MAY_ALIAS:
1605 case GNU_AK_MS_STRUCT:
1606 case GNU_AK_GCC_STRUCT:
1609 case GNU_AK_CDECL: modifiers |= DM_CDECL; goto no_arg;
1610 case GNU_AK_FASTCALL: modifiers |= DM_FASTCALL; goto no_arg;
1611 case GNU_AK_STDCALL: modifiers |= DM_STDCALL; goto no_arg;
1612 case GNU_AK_USED: modifiers |= DM_USED; goto no_arg;
1613 case GNU_AK_PURE: modifiers |= DM_PURE; goto no_arg;
1614 case GNU_AK_ALWAYS_INLINE: modifiers |= DM_FORCEINLINE; goto no_arg;
1615 case GNU_AK_DLLIMPORT: modifiers |= DM_DLLIMPORT; goto no_arg;
1616 case GNU_AK_DLLEXPORT: modifiers |= DM_DLLEXPORT; goto no_arg;
1617 case GNU_AK_PACKED: modifiers |= DM_PACKED; goto no_arg;
1618 case GNU_AK_NOINLINE: modifiers |= DM_NOINLINE; goto no_arg;
1619 case GNU_AK_NORETURN: modifiers |= DM_NORETURN; goto no_arg;
1620 case GNU_AK_NOTHROW: modifiers |= DM_NOTHROW; goto no_arg;
1621 case GNU_AK_TRANSPARENT_UNION: modifiers |= DM_TRANSPARENT_UNION; goto no_arg;
1622 case GNU_AK_CONSTRUCTOR: modifiers |= DM_CONSTRUCTOR; goto no_arg;
1623 case GNU_AK_DESTRUCTOR: modifiers |= DM_DESTRUCTOR; goto no_arg;
1625 case GNU_AK_ALIGNED:
1626 /* __align__ may be used without an argument */
1627 if (attribute->have_arguments) {
1628 parse_gnu_attribute_const_arg(attribute);
1632 case GNU_AK_FORMAT_ARG:
1633 case GNU_AK_REGPARM:
1634 case GNU_AK_TRAP_EXIT:
1635 if (!attribute->have_arguments) {
1636 /* should have arguments */
1637 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1638 attribute->invalid = true;
1640 parse_gnu_attribute_const_arg(attribute);
1643 case GNU_AK_SECTION:
1644 case GNU_AK_SP_SWITCH:
1645 if (!attribute->have_arguments) {
1646 /* should have arguments */
1647 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1648 attribute->invalid = true;
1650 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1653 if (!attribute->have_arguments) {
1654 /* should have arguments */
1655 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1656 attribute->invalid = true;
1658 parse_gnu_attribute_format_args(attribute);
1660 case GNU_AK_WEAKREF:
1661 /* may have one string argument */
1662 if (attribute->have_arguments)
1663 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1665 case GNU_AK_NONNULL:
1666 if (attribute->have_arguments)
1667 parse_gnu_attribute_const_arg_list(attribute);
1669 case GNU_AK_TLS_MODEL:
1670 if (!attribute->have_arguments) {
1671 /* should have arguments */
1672 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1674 parse_gnu_attribute_tls_model_arg(attribute);
1676 case GNU_AK_VISIBILITY:
1677 if (!attribute->have_arguments) {
1678 /* should have arguments */
1679 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1681 parse_gnu_attribute_visibility_arg(attribute);
1684 if (!attribute->have_arguments) {
1685 /* should have arguments */
1686 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1688 parse_gnu_attribute_model_arg(attribute);
1692 if (!attribute->have_arguments) {
1693 /* should have arguments */
1694 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1696 parse_gnu_attribute_mode_arg(attribute);
1699 case GNU_AK_INTERRUPT:
1700 /* may have one string argument */
1701 if (attribute->have_arguments)
1702 parse_gnu_attribute_interrupt_arg(attribute);
1704 case GNU_AK_SENTINEL:
1705 /* may have one string argument */
1706 if (attribute->have_arguments)
1707 parse_gnu_attribute_const_arg(attribute);
1710 /* already handled */
1714 check_no_argument(attribute, name);
1717 if (attribute != NULL) {
1719 last->next = attribute;
1722 head = last = attribute;
1726 if (token.type != ',')
1740 * Parse GNU attributes.
1742 static decl_modifiers_t parse_attributes(gnu_attribute_t **attributes)
1744 decl_modifiers_t modifiers = 0;
1747 switch(token.type) {
1748 case T___attribute__:
1749 modifiers |= parse_gnu_attribute(attributes);
1755 if (token.type != T_STRING_LITERAL) {
1756 parse_error_expected("while parsing assembler attribute",
1757 T_STRING_LITERAL, NULL);
1758 eat_until_matching_token('(');
1761 parse_string_literals();
1766 case T_cdecl: modifiers |= DM_CDECL; break;
1767 case T__fastcall: modifiers |= DM_FASTCALL; break;
1768 case T__stdcall: modifiers |= DM_STDCALL; break;
1771 /* TODO record modifier */
1772 warningf(HERE, "Ignoring declaration modifier %K", &token);
1776 default: return modifiers;
1783 static designator_t *parse_designation(void)
1785 designator_t *result = NULL;
1786 designator_t *last = NULL;
1789 designator_t *designator;
1790 switch(token.type) {
1792 designator = allocate_ast_zero(sizeof(designator[0]));
1793 designator->source_position = token.source_position;
1795 add_anchor_token(']');
1796 designator->array_index = parse_constant_expression();
1797 rem_anchor_token(']');
1801 designator = allocate_ast_zero(sizeof(designator[0]));
1802 designator->source_position = token.source_position;
1804 if (token.type != T_IDENTIFIER) {
1805 parse_error_expected("while parsing designator",
1806 T_IDENTIFIER, NULL);
1809 designator->symbol = token.v.symbol;
1817 assert(designator != NULL);
1819 last->next = designator;
1821 result = designator;
1829 static initializer_t *initializer_from_string(array_type_t *type,
1830 const string_t *const string)
1832 /* TODO: check len vs. size of array type */
1835 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1836 initializer->string.string = *string;
1841 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1842 wide_string_t *const string)
1844 /* TODO: check len vs. size of array type */
1847 initializer_t *const initializer =
1848 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1849 initializer->wide_string.string = *string;
1855 * Build an initializer from a given expression.
1857 static initializer_t *initializer_from_expression(type_t *orig_type,
1858 expression_t *expression)
1860 /* TODO check that expression is a constant expression */
1862 /* § 6.7.8.14/15 char array may be initialized by string literals */
1863 type_t *type = skip_typeref(orig_type);
1864 type_t *expr_type_orig = expression->base.type;
1865 type_t *expr_type = skip_typeref(expr_type_orig);
1866 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1867 array_type_t *const array_type = &type->array;
1868 type_t *const element_type = skip_typeref(array_type->element_type);
1870 if (element_type->kind == TYPE_ATOMIC) {
1871 atomic_type_kind_t akind = element_type->atomic.akind;
1872 switch (expression->kind) {
1873 case EXPR_STRING_LITERAL:
1874 if (akind == ATOMIC_TYPE_CHAR
1875 || akind == ATOMIC_TYPE_SCHAR
1876 || akind == ATOMIC_TYPE_UCHAR) {
1877 return initializer_from_string(array_type,
1878 &expression->string.value);
1881 case EXPR_WIDE_STRING_LITERAL: {
1882 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1883 if (get_unqualified_type(element_type) == bare_wchar_type) {
1884 return initializer_from_wide_string(array_type,
1885 &expression->wide_string.value);
1895 assign_error_t error = semantic_assign(type, expression);
1896 if (error == ASSIGN_ERROR_INCOMPATIBLE)
1898 report_assign_error(error, type, expression, "initializer",
1899 &expression->base.source_position);
1901 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1902 result->value.value = create_implicit_cast(expression, type);
1908 * Checks if a given expression can be used as an constant initializer.
1910 static bool is_initializer_constant(const expression_t *expression)
1912 return is_constant_expression(expression)
1913 || is_address_constant(expression);
1917 * Parses an scalar initializer.
1919 * § 6.7.8.11; eat {} without warning
1921 static initializer_t *parse_scalar_initializer(type_t *type,
1922 bool must_be_constant)
1924 /* there might be extra {} hierarchies */
1926 if (token.type == '{') {
1927 warningf(HERE, "extra curly braces around scalar initializer");
1931 } while (token.type == '{');
1934 expression_t *expression = parse_assignment_expression();
1935 if (must_be_constant && !is_initializer_constant(expression)) {
1936 errorf(&expression->base.source_position,
1937 "Initialisation expression '%E' is not constant\n",
1941 initializer_t *initializer = initializer_from_expression(type, expression);
1943 if (initializer == NULL) {
1944 errorf(&expression->base.source_position,
1945 "expression '%E' (type '%T') doesn't match expected type '%T'",
1946 expression, expression->base.type, type);
1951 bool additional_warning_displayed = false;
1953 if (token.type == ',') {
1956 if (token.type != '}') {
1957 if (!additional_warning_displayed) {
1958 warningf(HERE, "additional elements in scalar initializer");
1959 additional_warning_displayed = true;
1970 * An entry in the type path.
1972 typedef struct type_path_entry_t type_path_entry_t;
1973 struct type_path_entry_t {
1974 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1976 size_t index; /**< For array types: the current index. */
1977 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1982 * A type path expression a position inside compound or array types.
1984 typedef struct type_path_t type_path_t;
1985 struct type_path_t {
1986 type_path_entry_t *path; /**< An flexible array containing the current path. */
1987 type_t *top_type; /**< type of the element the path points */
1988 size_t max_index; /**< largest index in outermost array */
1992 * Prints a type path for debugging.
1994 static __attribute__((unused)) void debug_print_type_path(
1995 const type_path_t *path)
1997 size_t len = ARR_LEN(path->path);
1999 for(size_t i = 0; i < len; ++i) {
2000 const type_path_entry_t *entry = & path->path[i];
2002 type_t *type = skip_typeref(entry->type);
2003 if (is_type_compound(type)) {
2004 /* in gcc mode structs can have no members */
2005 if (entry->v.compound_entry == NULL) {
2009 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
2010 } else if (is_type_array(type)) {
2011 fprintf(stderr, "[%zd]", entry->v.index);
2013 fprintf(stderr, "-INVALID-");
2016 if (path->top_type != NULL) {
2017 fprintf(stderr, " (");
2018 print_type(path->top_type);
2019 fprintf(stderr, ")");
2024 * Return the top type path entry, ie. in a path
2025 * (type).a.b returns the b.
2027 static type_path_entry_t *get_type_path_top(const type_path_t *path)
2029 size_t len = ARR_LEN(path->path);
2031 return &path->path[len-1];
2035 * Enlarge the type path by an (empty) element.
2037 static type_path_entry_t *append_to_type_path(type_path_t *path)
2039 size_t len = ARR_LEN(path->path);
2040 ARR_RESIZE(type_path_entry_t, path->path, len+1);
2042 type_path_entry_t *result = & path->path[len];
2043 memset(result, 0, sizeof(result[0]));
2048 * Descending into a sub-type. Enter the scope of the current
2051 static void descend_into_subtype(type_path_t *path)
2053 type_t *orig_top_type = path->top_type;
2054 type_t *top_type = skip_typeref(orig_top_type);
2056 assert(is_type_compound(top_type) || is_type_array(top_type));
2058 type_path_entry_t *top = append_to_type_path(path);
2059 top->type = top_type;
2061 if (is_type_compound(top_type)) {
2062 declaration_t *declaration = top_type->compound.declaration;
2063 declaration_t *entry = declaration->scope.declarations;
2064 top->v.compound_entry = entry;
2066 if (entry != NULL) {
2067 path->top_type = entry->type;
2069 path->top_type = NULL;
2072 assert(is_type_array(top_type));
2075 path->top_type = top_type->array.element_type;
2080 * Pop an entry from the given type path, ie. returning from
2081 * (type).a.b to (type).a
2083 static void ascend_from_subtype(type_path_t *path)
2085 type_path_entry_t *top = get_type_path_top(path);
2087 path->top_type = top->type;
2089 size_t len = ARR_LEN(path->path);
2090 ARR_RESIZE(type_path_entry_t, path->path, len-1);
2094 * Pop entries from the given type path until the given
2095 * path level is reached.
2097 static void ascend_to(type_path_t *path, size_t top_path_level)
2099 size_t len = ARR_LEN(path->path);
2101 while(len > top_path_level) {
2102 ascend_from_subtype(path);
2103 len = ARR_LEN(path->path);
2107 static bool walk_designator(type_path_t *path, const designator_t *designator,
2108 bool used_in_offsetof)
2110 for( ; designator != NULL; designator = designator->next) {
2111 type_path_entry_t *top = get_type_path_top(path);
2112 type_t *orig_type = top->type;
2114 type_t *type = skip_typeref(orig_type);
2116 if (designator->symbol != NULL) {
2117 symbol_t *symbol = designator->symbol;
2118 if (!is_type_compound(type)) {
2119 if (is_type_valid(type)) {
2120 errorf(&designator->source_position,
2121 "'.%Y' designator used for non-compound type '%T'",
2127 declaration_t *declaration = type->compound.declaration;
2128 declaration_t *iter = declaration->scope.declarations;
2129 for( ; iter != NULL; iter = iter->next) {
2130 if (iter->symbol == symbol) {
2135 errorf(&designator->source_position,
2136 "'%T' has no member named '%Y'", orig_type, symbol);
2139 if (used_in_offsetof) {
2140 type_t *real_type = skip_typeref(iter->type);
2141 if (real_type->kind == TYPE_BITFIELD) {
2142 errorf(&designator->source_position,
2143 "offsetof designator '%Y' may not specify bitfield",
2149 top->type = orig_type;
2150 top->v.compound_entry = iter;
2151 orig_type = iter->type;
2153 expression_t *array_index = designator->array_index;
2154 assert(designator->array_index != NULL);
2156 if (!is_type_array(type)) {
2157 if (is_type_valid(type)) {
2158 errorf(&designator->source_position,
2159 "[%E] designator used for non-array type '%T'",
2160 array_index, orig_type);
2164 if (!is_type_valid(array_index->base.type)) {
2168 long index = fold_constant(array_index);
2169 if (!used_in_offsetof) {
2171 errorf(&designator->source_position,
2172 "array index [%E] must be positive", array_index);
2175 if (type->array.size_constant == true) {
2176 long array_size = type->array.size;
2177 if (index >= array_size) {
2178 errorf(&designator->source_position,
2179 "designator [%E] (%d) exceeds array size %d",
2180 array_index, index, array_size);
2186 top->type = orig_type;
2187 top->v.index = (size_t) index;
2188 orig_type = type->array.element_type;
2190 path->top_type = orig_type;
2192 if (designator->next != NULL) {
2193 descend_into_subtype(path);
2202 static void advance_current_object(type_path_t *path, size_t top_path_level)
2204 type_path_entry_t *top = get_type_path_top(path);
2206 type_t *type = skip_typeref(top->type);
2207 if (is_type_union(type)) {
2208 /* in unions only the first element is initialized */
2209 top->v.compound_entry = NULL;
2210 } else if (is_type_struct(type)) {
2211 declaration_t *entry = top->v.compound_entry;
2213 entry = entry->next;
2214 top->v.compound_entry = entry;
2215 if (entry != NULL) {
2216 path->top_type = entry->type;
2220 assert(is_type_array(type));
2224 if (!type->array.size_constant || top->v.index < type->array.size) {
2229 /* we're past the last member of the current sub-aggregate, try if we
2230 * can ascend in the type hierarchy and continue with another subobject */
2231 size_t len = ARR_LEN(path->path);
2233 if (len > top_path_level) {
2234 ascend_from_subtype(path);
2235 advance_current_object(path, top_path_level);
2237 path->top_type = NULL;
2242 * skip until token is found.
2244 static void skip_until(int type) {
2245 while(token.type != type) {
2246 if (token.type == T_EOF)
2253 * skip any {...} blocks until a closing bracket is reached.
2255 static void skip_initializers(void)
2257 if (token.type == '{')
2260 while(token.type != '}') {
2261 if (token.type == T_EOF)
2263 if (token.type == '{') {
2271 static initializer_t *create_empty_initializer(void)
2273 static initializer_t empty_initializer
2274 = { .list = { { INITIALIZER_LIST }, 0 } };
2275 return &empty_initializer;
2279 * Parse a part of an initialiser for a struct or union,
2281 static initializer_t *parse_sub_initializer(type_path_t *path,
2282 type_t *outer_type, size_t top_path_level,
2283 parse_initializer_env_t *env)
2285 if (token.type == '}') {
2286 /* empty initializer */
2287 return create_empty_initializer();
2290 type_t *orig_type = path->top_type;
2291 type_t *type = NULL;
2293 if (orig_type == NULL) {
2294 /* We are initializing an empty compound. */
2296 type = skip_typeref(orig_type);
2298 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2299 * initializers in this case. */
2300 if (!is_type_valid(type)) {
2301 skip_initializers();
2302 return create_empty_initializer();
2306 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2309 designator_t *designator = NULL;
2310 if (token.type == '.' || token.type == '[') {
2311 designator = parse_designation();
2313 /* reset path to toplevel, evaluate designator from there */
2314 ascend_to(path, top_path_level);
2315 if (!walk_designator(path, designator, false)) {
2316 /* can't continue after designation error */
2320 initializer_t *designator_initializer
2321 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2322 designator_initializer->designator.designator = designator;
2323 ARR_APP1(initializer_t*, initializers, designator_initializer);
2325 orig_type = path->top_type;
2326 type = orig_type != NULL ? skip_typeref(orig_type) : NULL;
2331 if (token.type == '{') {
2332 if (type != NULL && is_type_scalar(type)) {
2333 sub = parse_scalar_initializer(type, env->must_be_constant);
2337 if (env->declaration != NULL) {
2338 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2339 env->declaration->symbol);
2341 errorf(HERE, "extra brace group at end of initializer");
2344 descend_into_subtype(path);
2346 add_anchor_token('}');
2347 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2349 rem_anchor_token('}');
2352 ascend_from_subtype(path);
2356 goto error_parse_next;
2360 /* must be an expression */
2361 expression_t *expression = parse_assignment_expression();
2363 if (env->must_be_constant && !is_initializer_constant(expression)) {
2364 errorf(&expression->base.source_position,
2365 "Initialisation expression '%E' is not constant\n",
2370 /* we are already outside, ... */
2374 /* handle { "string" } special case */
2375 if ((expression->kind == EXPR_STRING_LITERAL
2376 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2377 && outer_type != NULL) {
2378 sub = initializer_from_expression(outer_type, expression);
2380 if (token.type == ',') {
2383 if (token.type != '}') {
2384 warningf(HERE, "excessive elements in initializer for type '%T'",
2387 /* TODO: eat , ... */
2392 /* descend into subtypes until expression matches type */
2394 orig_type = path->top_type;
2395 type = skip_typeref(orig_type);
2397 sub = initializer_from_expression(orig_type, expression);
2401 if (!is_type_valid(type)) {
2404 if (is_type_scalar(type)) {
2405 errorf(&expression->base.source_position,
2406 "expression '%E' doesn't match expected type '%T'",
2407 expression, orig_type);
2411 descend_into_subtype(path);
2415 /* update largest index of top array */
2416 const type_path_entry_t *first = &path->path[0];
2417 type_t *first_type = first->type;
2418 first_type = skip_typeref(first_type);
2419 if (is_type_array(first_type)) {
2420 size_t index = first->v.index;
2421 if (index > path->max_index)
2422 path->max_index = index;
2426 /* append to initializers list */
2427 ARR_APP1(initializer_t*, initializers, sub);
2430 if (env->declaration != NULL)
2431 warningf(HERE, "excess elements in struct initializer for '%Y'",
2432 env->declaration->symbol);
2434 warningf(HERE, "excess elements in struct initializer");
2438 if (token.type == '}') {
2442 if (token.type == '}') {
2447 /* advance to the next declaration if we are not at the end */
2448 advance_current_object(path, top_path_level);
2449 orig_type = path->top_type;
2450 if (orig_type != NULL)
2451 type = skip_typeref(orig_type);
2457 size_t len = ARR_LEN(initializers);
2458 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2459 initializer_t *result = allocate_ast_zero(size);
2460 result->kind = INITIALIZER_LIST;
2461 result->list.len = len;
2462 memcpy(&result->list.initializers, initializers,
2463 len * sizeof(initializers[0]));
2465 DEL_ARR_F(initializers);
2466 ascend_to(path, top_path_level+1);
2471 skip_initializers();
2472 DEL_ARR_F(initializers);
2473 ascend_to(path, top_path_level+1);
2478 * Parses an initializer. Parsers either a compound literal
2479 * (env->declaration == NULL) or an initializer of a declaration.
2481 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2483 type_t *type = skip_typeref(env->type);
2484 initializer_t *result = NULL;
2487 if (is_type_scalar(type)) {
2488 result = parse_scalar_initializer(type, env->must_be_constant);
2489 } else if (token.type == '{') {
2493 memset(&path, 0, sizeof(path));
2494 path.top_type = env->type;
2495 path.path = NEW_ARR_F(type_path_entry_t, 0);
2497 descend_into_subtype(&path);
2499 add_anchor_token('}');
2500 result = parse_sub_initializer(&path, env->type, 1, env);
2501 rem_anchor_token('}');
2503 max_index = path.max_index;
2504 DEL_ARR_F(path.path);
2508 /* parse_scalar_initializer() also works in this case: we simply
2509 * have an expression without {} around it */
2510 result = parse_scalar_initializer(type, env->must_be_constant);
2513 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2514 * the array type size */
2515 if (is_type_array(type) && type->array.size_expression == NULL
2516 && result != NULL) {
2518 switch (result->kind) {
2519 case INITIALIZER_LIST:
2520 size = max_index + 1;
2523 case INITIALIZER_STRING:
2524 size = result->string.string.size;
2527 case INITIALIZER_WIDE_STRING:
2528 size = result->wide_string.string.size;
2531 case INITIALIZER_DESIGNATOR:
2532 case INITIALIZER_VALUE:
2533 /* can happen for parse errors */
2538 internal_errorf(HERE, "invalid initializer type");
2541 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2542 cnst->base.type = type_size_t;
2543 cnst->conste.v.int_value = size;
2545 type_t *new_type = duplicate_type(type);
2547 new_type->array.size_expression = cnst;
2548 new_type->array.size_constant = true;
2549 new_type->array.size = size;
2550 env->type = new_type;
2558 static declaration_t *append_declaration(declaration_t *declaration);
2560 static declaration_t *parse_compound_type_specifier(bool is_struct)
2562 gnu_attribute_t *attributes = NULL;
2563 decl_modifiers_t modifiers = 0;
2570 symbol_t *symbol = NULL;
2571 declaration_t *declaration = NULL;
2573 if (token.type == T___attribute__) {
2574 modifiers |= parse_attributes(&attributes);
2577 if (token.type == T_IDENTIFIER) {
2578 symbol = token.v.symbol;
2581 namespace_t const namespc =
2582 is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION;
2583 declaration = get_declaration(symbol, namespc);
2584 if (declaration != NULL) {
2585 if (declaration->parent_scope != scope &&
2586 (token.type == '{' || token.type == ';')) {
2588 } else if (declaration->init.complete &&
2589 token.type == '{') {
2590 assert(symbol != NULL);
2591 errorf(HERE, "multiple definitions of '%s %Y' (previous definition at %P)",
2592 is_struct ? "struct" : "union", symbol,
2593 &declaration->source_position);
2594 declaration->scope.declarations = NULL;
2597 } else if (token.type != '{') {
2599 parse_error_expected("while parsing struct type specifier",
2600 T_IDENTIFIER, '{', NULL);
2602 parse_error_expected("while parsing union type specifier",
2603 T_IDENTIFIER, '{', NULL);
2609 if (declaration == NULL) {
2610 declaration = allocate_declaration_zero();
2611 declaration->namespc =
2612 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2613 declaration->source_position = token.source_position;
2614 declaration->symbol = symbol;
2615 declaration->parent_scope = scope;
2616 if (symbol != NULL) {
2617 environment_push(declaration);
2619 append_declaration(declaration);
2622 if (token.type == '{') {
2623 declaration->init.complete = true;
2625 parse_compound_type_entries(declaration);
2626 modifiers |= parse_attributes(&attributes);
2629 declaration->modifiers |= modifiers;
2633 static void parse_enum_entries(type_t *const enum_type)
2637 if (token.type == '}') {
2639 errorf(HERE, "empty enum not allowed");
2643 add_anchor_token('}');
2645 if (token.type != T_IDENTIFIER) {
2646 parse_error_expected("while parsing enum entry", T_IDENTIFIER, NULL);
2648 rem_anchor_token('}');
2652 declaration_t *const entry = allocate_declaration_zero();
2653 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2654 entry->type = enum_type;
2655 entry->symbol = token.v.symbol;
2656 entry->source_position = token.source_position;
2659 if (token.type == '=') {
2661 expression_t *value = parse_constant_expression();
2663 value = create_implicit_cast(value, enum_type);
2664 entry->init.enum_value = value;
2669 record_declaration(entry);
2671 if (token.type != ',')
2674 } while(token.type != '}');
2675 rem_anchor_token('}');
2683 static type_t *parse_enum_specifier(void)
2685 gnu_attribute_t *attributes = NULL;
2686 declaration_t *declaration;
2690 if (token.type == T_IDENTIFIER) {
2691 symbol = token.v.symbol;
2694 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2695 } else if (token.type != '{') {
2696 parse_error_expected("while parsing enum type specifier",
2697 T_IDENTIFIER, '{', NULL);
2704 if (declaration == NULL) {
2705 declaration = allocate_declaration_zero();
2706 declaration->namespc = NAMESPACE_ENUM;
2707 declaration->source_position = token.source_position;
2708 declaration->symbol = symbol;
2709 declaration->parent_scope = scope;
2712 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2713 type->enumt.declaration = declaration;
2715 if (token.type == '{') {
2716 if (declaration->init.complete) {
2717 errorf(HERE, "multiple definitions of enum %Y", symbol);
2719 if (symbol != NULL) {
2720 environment_push(declaration);
2722 append_declaration(declaration);
2723 declaration->init.complete = true;
2725 parse_enum_entries(type);
2726 parse_attributes(&attributes);
2733 * if a symbol is a typedef to another type, return true
2735 static bool is_typedef_symbol(symbol_t *symbol)
2737 const declaration_t *const declaration =
2738 get_declaration(symbol, NAMESPACE_NORMAL);
2740 declaration != NULL &&
2741 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2744 static type_t *parse_typeof(void)
2751 add_anchor_token(')');
2753 expression_t *expression = NULL;
2756 switch(token.type) {
2757 case T___extension__:
2758 /* this can be a prefix to a typename or an expression */
2759 /* we simply eat it now. */
2762 } while(token.type == T___extension__);
2766 if (is_typedef_symbol(token.v.symbol)) {
2767 type = parse_typename();
2769 expression = parse_expression();
2770 type = expression->base.type;
2775 type = parse_typename();
2779 expression = parse_expression();
2780 type = expression->base.type;
2784 rem_anchor_token(')');
2787 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2788 typeof_type->typeoft.expression = expression;
2789 typeof_type->typeoft.typeof_type = type;
2796 typedef enum specifiers_t {
2797 SPECIFIER_SIGNED = 1 << 0,
2798 SPECIFIER_UNSIGNED = 1 << 1,
2799 SPECIFIER_LONG = 1 << 2,
2800 SPECIFIER_INT = 1 << 3,
2801 SPECIFIER_DOUBLE = 1 << 4,
2802 SPECIFIER_CHAR = 1 << 5,
2803 SPECIFIER_SHORT = 1 << 6,
2804 SPECIFIER_LONG_LONG = 1 << 7,
2805 SPECIFIER_FLOAT = 1 << 8,
2806 SPECIFIER_BOOL = 1 << 9,
2807 SPECIFIER_VOID = 1 << 10,
2808 SPECIFIER_INT8 = 1 << 11,
2809 SPECIFIER_INT16 = 1 << 12,
2810 SPECIFIER_INT32 = 1 << 13,
2811 SPECIFIER_INT64 = 1 << 14,
2812 SPECIFIER_INT128 = 1 << 15,
2813 SPECIFIER_COMPLEX = 1 << 16,
2814 SPECIFIER_IMAGINARY = 1 << 17,
2817 static type_t *create_builtin_type(symbol_t *const symbol,
2818 type_t *const real_type)
2820 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2821 type->builtin.symbol = symbol;
2822 type->builtin.real_type = real_type;
2824 type_t *result = typehash_insert(type);
2825 if (type != result) {
2832 static type_t *get_typedef_type(symbol_t *symbol)
2834 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2835 if (declaration == NULL ||
2836 declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2839 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2840 type->typedeft.declaration = declaration;
2846 * check for the allowed MS alignment values.
2848 static bool check_elignment_value(long long intvalue) {
2849 if (intvalue < 1 || intvalue > 8192) {
2850 errorf(HERE, "illegal alignment value");
2853 unsigned v = (unsigned)intvalue;
2854 for(unsigned i = 1; i <= 8192; i += i) {
2858 errorf(HERE, "alignment must be power of two");
2862 #define DET_MOD(name, tag) do { \
2863 if (*modifiers & tag) warningf(HERE, #name " used more than once"); \
2864 *modifiers |= tag; \
2867 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2869 decl_modifiers_t *modifiers = &specifiers->modifiers;
2872 if (token.type == T_restrict) {
2874 DET_MOD(restrict, DM_RESTRICT);
2876 } else if (token.type != T_IDENTIFIER)
2878 symbol_t *symbol = token.v.symbol;
2879 if (symbol == sym_align) {
2882 if (token.type != T_INTEGER)
2884 if (check_elignment_value(token.v.intvalue)) {
2885 if (specifiers->alignment != 0)
2886 warningf(HERE, "align used more than once");
2887 specifiers->alignment = (unsigned char)token.v.intvalue;
2891 } else if (symbol == sym_allocate) {
2894 if (token.type != T_IDENTIFIER)
2896 (void)token.v.symbol;
2898 } else if (symbol == sym_dllimport) {
2900 DET_MOD(dllimport, DM_DLLIMPORT);
2901 } else if (symbol == sym_dllexport) {
2903 DET_MOD(dllexport, DM_DLLEXPORT);
2904 } else if (symbol == sym_thread) {
2906 DET_MOD(thread, DM_THREAD);
2907 } else if (symbol == sym_naked) {
2909 DET_MOD(naked, DM_NAKED);
2910 } else if (symbol == sym_noinline) {
2912 DET_MOD(noinline, DM_NOINLINE);
2913 } else if (symbol == sym_noreturn) {
2915 DET_MOD(noreturn, DM_NORETURN);
2916 } else if (symbol == sym_nothrow) {
2918 DET_MOD(nothrow, DM_NOTHROW);
2919 } else if (symbol == sym_novtable) {
2921 DET_MOD(novtable, DM_NOVTABLE);
2922 } else if (symbol == sym_property) {
2926 bool is_get = false;
2927 if (token.type != T_IDENTIFIER)
2929 if (token.v.symbol == sym_get) {
2931 } else if (token.v.symbol == sym_put) {
2933 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2938 if (token.type != T_IDENTIFIER)
2941 if (specifiers->get_property_sym != NULL) {
2942 errorf(HERE, "get property name already specified");
2944 specifiers->get_property_sym = token.v.symbol;
2947 if (specifiers->put_property_sym != NULL) {
2948 errorf(HERE, "put property name already specified");
2950 specifiers->put_property_sym = token.v.symbol;
2954 if (token.type == ',') {
2961 } else if (symbol == sym_selectany) {
2963 DET_MOD(selectany, DM_SELECTANY);
2964 } else if (symbol == sym_uuid) {
2967 if (token.type != T_STRING_LITERAL)
2971 } else if (symbol == sym_deprecated) {
2973 if (specifiers->deprecated != 0)
2974 warningf(HERE, "deprecated used more than once");
2975 specifiers->deprecated = 1;
2976 if (token.type == '(') {
2978 if (token.type == T_STRING_LITERAL) {
2979 specifiers->deprecated_string = token.v.string.begin;
2982 errorf(HERE, "string literal expected");
2986 } else if (symbol == sym_noalias) {
2988 DET_MOD(noalias, DM_NOALIAS);
2990 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2992 if (token.type == '(')
2996 if (token.type == ',')
3003 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
3005 type_t *type = NULL;
3006 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3007 type_modifiers_t modifiers = TYPE_MODIFIER_NONE;
3008 unsigned type_specifiers = 0;
3011 specifiers->source_position = token.source_position;
3014 specifiers->modifiers
3015 |= parse_attributes(&specifiers->gnu_attributes);
3016 if (specifiers->modifiers & DM_TRANSPARENT_UNION)
3017 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3019 switch(token.type) {
3022 #define MATCH_STORAGE_CLASS(token, class) \
3024 if (specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
3025 errorf(HERE, "multiple storage classes in declaration specifiers"); \
3027 specifiers->declared_storage_class = class; \
3031 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
3032 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
3033 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
3034 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
3035 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
3040 add_anchor_token(')');
3041 parse_microsoft_extended_decl_modifier(specifiers);
3042 rem_anchor_token(')');
3047 switch (specifiers->declared_storage_class) {
3048 case STORAGE_CLASS_NONE:
3049 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
3052 case STORAGE_CLASS_EXTERN:
3053 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
3056 case STORAGE_CLASS_STATIC:
3057 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
3061 errorf(HERE, "multiple storage classes in declaration specifiers");
3067 /* type qualifiers */
3068 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
3070 qualifiers |= qualifier; \
3074 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3075 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3076 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3077 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3078 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3079 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3080 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3081 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3083 case T___extension__:
3088 /* type specifiers */
3089 #define MATCH_SPECIFIER(token, specifier, name) \
3092 if (type_specifiers & specifier) { \
3093 errorf(HERE, "multiple " name " type specifiers given"); \
3095 type_specifiers |= specifier; \
3099 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
3100 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
3101 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
3102 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
3103 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
3104 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
3105 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
3106 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
3107 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
3108 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
3109 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
3110 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
3111 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
3112 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
3113 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
3114 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
3116 case T__forceinline:
3117 /* only in microsoft mode */
3118 specifiers->modifiers |= DM_FORCEINLINE;
3122 specifiers->is_inline = true;
3127 if (type_specifiers & SPECIFIER_LONG_LONG) {
3128 errorf(HERE, "multiple type specifiers given");
3129 } else if (type_specifiers & SPECIFIER_LONG) {
3130 type_specifiers |= SPECIFIER_LONG_LONG;
3132 type_specifiers |= SPECIFIER_LONG;
3137 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
3139 type->compound.declaration = parse_compound_type_specifier(true);
3143 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
3144 type->compound.declaration = parse_compound_type_specifier(false);
3145 if (type->compound.declaration->modifiers & DM_TRANSPARENT_UNION)
3146 modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3150 type = parse_enum_specifier();
3153 type = parse_typeof();
3155 case T___builtin_va_list:
3156 type = duplicate_type(type_valist);
3160 case T_IDENTIFIER: {
3161 /* only parse identifier if we haven't found a type yet */
3162 if (type != NULL || type_specifiers != 0)
3163 goto finish_specifiers;
3165 type_t *typedef_type = get_typedef_type(token.v.symbol);
3167 if (typedef_type == NULL)
3168 goto finish_specifiers;
3171 type = typedef_type;
3175 /* function specifier */
3177 goto finish_specifiers;
3184 atomic_type_kind_t atomic_type;
3186 /* match valid basic types */
3187 switch(type_specifiers) {
3188 case SPECIFIER_VOID:
3189 atomic_type = ATOMIC_TYPE_VOID;
3191 case SPECIFIER_CHAR:
3192 atomic_type = ATOMIC_TYPE_CHAR;
3194 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
3195 atomic_type = ATOMIC_TYPE_SCHAR;
3197 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
3198 atomic_type = ATOMIC_TYPE_UCHAR;
3200 case SPECIFIER_SHORT:
3201 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
3202 case SPECIFIER_SHORT | SPECIFIER_INT:
3203 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3204 atomic_type = ATOMIC_TYPE_SHORT;
3206 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3207 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3208 atomic_type = ATOMIC_TYPE_USHORT;
3211 case SPECIFIER_SIGNED:
3212 case SPECIFIER_SIGNED | SPECIFIER_INT:
3213 atomic_type = ATOMIC_TYPE_INT;
3215 case SPECIFIER_UNSIGNED:
3216 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3217 atomic_type = ATOMIC_TYPE_UINT;
3219 case SPECIFIER_LONG:
3220 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3221 case SPECIFIER_LONG | SPECIFIER_INT:
3222 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3223 atomic_type = ATOMIC_TYPE_LONG;
3225 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3226 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3227 atomic_type = ATOMIC_TYPE_ULONG;
3229 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3230 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3231 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3232 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3234 atomic_type = ATOMIC_TYPE_LONGLONG;
3236 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3237 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3239 atomic_type = ATOMIC_TYPE_ULONGLONG;
3242 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3243 atomic_type = unsigned_int8_type_kind;
3246 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3247 atomic_type = unsigned_int16_type_kind;
3250 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3251 atomic_type = unsigned_int32_type_kind;
3254 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3255 atomic_type = unsigned_int64_type_kind;
3258 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3259 atomic_type = unsigned_int128_type_kind;
3262 case SPECIFIER_INT8:
3263 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3264 atomic_type = int8_type_kind;
3267 case SPECIFIER_INT16:
3268 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3269 atomic_type = int16_type_kind;
3272 case SPECIFIER_INT32:
3273 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3274 atomic_type = int32_type_kind;
3277 case SPECIFIER_INT64:
3278 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3279 atomic_type = int64_type_kind;
3282 case SPECIFIER_INT128:
3283 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3284 atomic_type = int128_type_kind;
3287 case SPECIFIER_FLOAT:
3288 atomic_type = ATOMIC_TYPE_FLOAT;
3290 case SPECIFIER_DOUBLE:
3291 atomic_type = ATOMIC_TYPE_DOUBLE;
3293 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3294 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3296 case SPECIFIER_BOOL:
3297 atomic_type = ATOMIC_TYPE_BOOL;
3299 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3300 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3301 atomic_type = ATOMIC_TYPE_FLOAT;
3303 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3304 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3305 atomic_type = ATOMIC_TYPE_DOUBLE;
3307 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3308 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3309 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3312 /* invalid specifier combination, give an error message */
3313 if (type_specifiers == 0) {
3314 if (! strict_mode) {
3315 if (warning.implicit_int) {
3316 warningf(HERE, "no type specifiers in declaration, using 'int'");
3318 atomic_type = ATOMIC_TYPE_INT;
3321 errorf(HERE, "no type specifiers given in declaration");
3323 } else if ((type_specifiers & SPECIFIER_SIGNED) &&
3324 (type_specifiers & SPECIFIER_UNSIGNED)) {
3325 errorf(HERE, "signed and unsigned specifiers gives");
3326 } else if (type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3327 errorf(HERE, "only integer types can be signed or unsigned");
3329 errorf(HERE, "multiple datatypes in declaration");
3331 atomic_type = ATOMIC_TYPE_INVALID;
3334 if (type_specifiers & SPECIFIER_COMPLEX &&
3335 atomic_type != ATOMIC_TYPE_INVALID) {
3336 type = allocate_type_zero(TYPE_COMPLEX, &builtin_source_position);
3337 type->complex.akind = atomic_type;
3338 } else if (type_specifiers & SPECIFIER_IMAGINARY &&
3339 atomic_type != ATOMIC_TYPE_INVALID) {
3340 type = allocate_type_zero(TYPE_IMAGINARY, &builtin_source_position);
3341 type->imaginary.akind = atomic_type;
3343 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3344 type->atomic.akind = atomic_type;
3348 if (type_specifiers != 0) {
3349 errorf(HERE, "multiple datatypes in declaration");
3353 /* FIXME: check type qualifiers here */
3355 type->base.qualifiers = qualifiers;
3356 type->base.modifiers = modifiers;
3358 type_t *result = typehash_insert(type);
3359 if (newtype && result != type) {
3363 specifiers->type = result;
3368 static type_qualifiers_t parse_type_qualifiers(void)
3370 type_qualifiers_t qualifiers = TYPE_QUALIFIER_NONE;
3373 switch(token.type) {
3374 /* type qualifiers */
3375 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3376 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3377 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3378 /* microsoft extended type modifiers */
3379 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3380 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3381 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3382 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3383 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3391 static declaration_t *parse_identifier_list(void)
3393 declaration_t *declarations = NULL;
3394 declaration_t *last_declaration = NULL;
3396 declaration_t *const declaration = allocate_declaration_zero();
3397 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3398 declaration->source_position = token.source_position;
3399 declaration->symbol = token.v.symbol;
3402 if (last_declaration != NULL) {
3403 last_declaration->next = declaration;
3405 declarations = declaration;
3407 last_declaration = declaration;
3409 if (token.type != ',') {
3413 } while(token.type == T_IDENTIFIER);
3415 return declarations;
3418 static type_t *automatic_type_conversion(type_t *orig_type);
3420 static void semantic_parameter(declaration_t *declaration)
3422 /* TODO: improve error messages */
3424 if (declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3425 errorf(HERE, "typedef not allowed in parameter list");
3426 } else if (declaration->declared_storage_class != STORAGE_CLASS_NONE
3427 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3428 errorf(HERE, "parameter may only have none or register storage class");
3431 type_t *const orig_type = declaration->type;
3432 /* §6.7.5.3(7): Array as last part of a parameter type is just syntactic
3433 * sugar. Turn it into a pointer.
3434 * §6.7.5.3(8): A declaration of a parameter as ``function returning type''
3435 * shall be adjusted to ``pointer to function returning type'', as in 6.3.2.1.
3437 type_t *const type = automatic_type_conversion(orig_type);
3438 declaration->type = type;
3440 if (is_type_incomplete(skip_typeref(type))) {
3441 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3442 orig_type, declaration->symbol);
3446 static declaration_t *parse_parameter(void)
3448 declaration_specifiers_t specifiers;
3449 memset(&specifiers, 0, sizeof(specifiers));
3451 parse_declaration_specifiers(&specifiers);
3453 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3458 static declaration_t *parse_parameters(function_type_t *type)
3460 declaration_t *declarations = NULL;
3463 add_anchor_token(')');
3464 int saved_comma_state = save_and_reset_anchor_state(',');
3466 if (token.type == T_IDENTIFIER) {
3467 symbol_t *symbol = token.v.symbol;
3468 if (!is_typedef_symbol(symbol)) {
3469 type->kr_style_parameters = true;
3470 declarations = parse_identifier_list();
3471 goto parameters_finished;
3475 if (token.type == ')') {
3476 type->unspecified_parameters = 1;
3477 goto parameters_finished;
3480 declaration_t *declaration;
3481 declaration_t *last_declaration = NULL;
3482 function_parameter_t *parameter;
3483 function_parameter_t *last_parameter = NULL;
3486 switch(token.type) {
3490 goto parameters_finished;
3493 case T___extension__:
3495 declaration = parse_parameter();
3497 /* func(void) is not a parameter */
3498 if (last_parameter == NULL
3499 && token.type == ')'
3500 && declaration->symbol == NULL
3501 && skip_typeref(declaration->type) == type_void) {
3502 goto parameters_finished;
3504 semantic_parameter(declaration);
3506 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3507 memset(parameter, 0, sizeof(parameter[0]));
3508 parameter->type = declaration->type;
3510 if (last_parameter != NULL) {
3511 last_declaration->next = declaration;
3512 last_parameter->next = parameter;
3514 type->parameters = parameter;
3515 declarations = declaration;
3517 last_parameter = parameter;
3518 last_declaration = declaration;
3522 goto parameters_finished;
3524 if (token.type != ',') {
3525 goto parameters_finished;
3531 parameters_finished:
3532 rem_anchor_token(')');
3535 restore_anchor_state(',', saved_comma_state);
3536 return declarations;
3539 restore_anchor_state(',', saved_comma_state);
3543 typedef enum construct_type_kind_t {
3548 } construct_type_kind_t;
3550 typedef struct construct_type_t construct_type_t;
3551 struct construct_type_t {
3552 construct_type_kind_t kind;
3553 construct_type_t *next;
3556 typedef struct parsed_pointer_t parsed_pointer_t;
3557 struct parsed_pointer_t {
3558 construct_type_t construct_type;
3559 type_qualifiers_t type_qualifiers;
3562 typedef struct construct_function_type_t construct_function_type_t;
3563 struct construct_function_type_t {
3564 construct_type_t construct_type;
3565 type_t *function_type;
3568 typedef struct parsed_array_t parsed_array_t;
3569 struct parsed_array_t {
3570 construct_type_t construct_type;
3571 type_qualifiers_t type_qualifiers;
3577 typedef struct construct_base_type_t construct_base_type_t;
3578 struct construct_base_type_t {
3579 construct_type_t construct_type;
3583 static construct_type_t *parse_pointer_declarator(void)
3587 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3588 memset(pointer, 0, sizeof(pointer[0]));
3589 pointer->construct_type.kind = CONSTRUCT_POINTER;
3590 pointer->type_qualifiers = parse_type_qualifiers();
3592 return (construct_type_t*) pointer;
3595 static construct_type_t *parse_array_declarator(void)
3598 add_anchor_token(']');
3600 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3601 memset(array, 0, sizeof(array[0]));
3602 array->construct_type.kind = CONSTRUCT_ARRAY;
3604 if (token.type == T_static) {
3605 array->is_static = true;
3609 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3610 if (type_qualifiers != 0) {
3611 if (token.type == T_static) {
3612 array->is_static = true;
3616 array->type_qualifiers = type_qualifiers;
3618 if (token.type == '*' && look_ahead(1)->type == ']') {
3619 array->is_variable = true;
3621 } else if (token.type != ']') {
3622 array->size = parse_assignment_expression();
3625 rem_anchor_token(']');
3628 return (construct_type_t*) array;
3633 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3636 if (declaration != NULL) {
3637 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3639 unsigned mask = declaration->modifiers & (DM_CDECL|DM_STDCALL|DM_FASTCALL|DM_THISCALL);
3641 if (mask & (mask-1)) {
3642 const char *first = NULL, *second = NULL;
3644 /* more than one calling convention set */
3645 if (declaration->modifiers & DM_CDECL) {
3646 if (first == NULL) first = "cdecl";
3647 else if (second == NULL) second = "cdecl";
3649 if (declaration->modifiers & DM_STDCALL) {
3650 if (first == NULL) first = "stdcall";
3651 else if (second == NULL) second = "stdcall";
3653 if (declaration->modifiers & DM_FASTCALL) {
3654 if (first == NULL) first = "faslcall";
3655 else if (second == NULL) second = "fastcall";
3657 if (declaration->modifiers & DM_THISCALL) {
3658 if (first == NULL) first = "thiscall";
3659 else if (second == NULL) second = "thiscall";
3661 errorf(&declaration->source_position, "%s and %s attributes are not compatible", first, second);
3664 if (declaration->modifiers & DM_CDECL)
3665 type->function.calling_convention = CC_CDECL;
3666 else if (declaration->modifiers & DM_STDCALL)
3667 type->function.calling_convention = CC_STDCALL;
3668 else if (declaration->modifiers & DM_FASTCALL)
3669 type->function.calling_convention = CC_FASTCALL;
3670 else if (declaration->modifiers & DM_THISCALL)
3671 type->function.calling_convention = CC_THISCALL;
3673 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3676 declaration_t *parameters = parse_parameters(&type->function);
3677 if (declaration != NULL) {
3678 declaration->scope.declarations = parameters;
3681 construct_function_type_t *construct_function_type =
3682 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3683 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3684 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3685 construct_function_type->function_type = type;
3687 return &construct_function_type->construct_type;
3690 static void fix_declaration_type(declaration_t *declaration)
3692 decl_modifiers_t declaration_modifiers = declaration->modifiers;
3693 type_modifiers_t type_modifiers = declaration->type->base.modifiers;
3695 if (declaration_modifiers & DM_TRANSPARENT_UNION)
3696 type_modifiers |= TYPE_MODIFIER_TRANSPARENT_UNION;
3698 if (declaration->type->base.modifiers == type_modifiers)
3701 type_t *copy = duplicate_type(declaration->type);
3702 copy->base.modifiers = type_modifiers;
3704 type_t *result = typehash_insert(copy);
3705 if (result != copy) {
3706 obstack_free(type_obst, copy);
3709 declaration->type = result;
3712 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3713 bool may_be_abstract)
3715 /* construct a single linked list of construct_type_t's which describe
3716 * how to construct the final declarator type */
3717 construct_type_t *first = NULL;
3718 construct_type_t *last = NULL;
3719 gnu_attribute_t *attributes = NULL;
3721 decl_modifiers_t modifiers = parse_attributes(&attributes);
3724 while(token.type == '*') {
3725 construct_type_t *type = parse_pointer_declarator();
3735 /* TODO: find out if this is correct */
3736 modifiers |= parse_attributes(&attributes);
3739 construct_type_t *inner_types = NULL;
3741 switch(token.type) {
3743 if (declaration == NULL) {
3744 errorf(HERE, "no identifier expected in typename");
3746 declaration->symbol = token.v.symbol;
3747 declaration->source_position = token.source_position;
3753 add_anchor_token(')');
3754 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3755 rem_anchor_token(')');
3759 if (may_be_abstract)
3761 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', NULL);
3762 /* avoid a loop in the outermost scope, because eat_statement doesn't
3764 if (token.type == '}' && current_function == NULL) {
3772 construct_type_t *p = last;
3775 construct_type_t *type;
3776 switch(token.type) {
3778 type = parse_function_declarator(declaration);
3781 type = parse_array_declarator();
3784 goto declarator_finished;
3787 /* insert in the middle of the list (behind p) */
3789 type->next = p->next;
3800 declarator_finished:
3801 /* append inner_types at the end of the list, we don't to set last anymore
3802 * as it's not needed anymore */
3804 assert(first == NULL);
3805 first = inner_types;
3807 last->next = inner_types;
3815 static void parse_declaration_attributes(declaration_t *declaration)
3817 gnu_attribute_t *attributes = NULL;
3818 decl_modifiers_t modifiers = parse_attributes(&attributes);
3820 if (declaration == NULL)
3823 declaration->modifiers |= modifiers;
3824 /* check if we have these stupid mode attributes... */
3825 type_t *old_type = declaration->type;
3826 if (old_type == NULL)
3829 gnu_attribute_t *attribute = attributes;
3830 for ( ; attribute != NULL; attribute = attribute->next) {
3831 if (attribute->kind != GNU_AK_MODE || attribute->invalid)
3834 atomic_type_kind_t akind = attribute->u.akind;
3835 if (!is_type_signed(old_type)) {
3837 case ATOMIC_TYPE_CHAR: akind = ATOMIC_TYPE_UCHAR; break;
3838 case ATOMIC_TYPE_SHORT: akind = ATOMIC_TYPE_USHORT; break;
3839 case ATOMIC_TYPE_INT: akind = ATOMIC_TYPE_UINT; break;
3840 case ATOMIC_TYPE_LONGLONG: akind = ATOMIC_TYPE_ULONGLONG; break;
3842 panic("invalid akind in mode attribute");
3846 = make_atomic_type(akind, old_type->base.qualifiers);
3850 static type_t *construct_declarator_type(construct_type_t *construct_list,
3853 construct_type_t *iter = construct_list;
3854 for( ; iter != NULL; iter = iter->next) {
3855 switch(iter->kind) {
3856 case CONSTRUCT_INVALID:
3857 internal_errorf(HERE, "invalid type construction found");
3858 case CONSTRUCT_FUNCTION: {
3859 construct_function_type_t *construct_function_type
3860 = (construct_function_type_t*) iter;
3862 type_t *function_type = construct_function_type->function_type;
3864 function_type->function.return_type = type;
3866 type_t *skipped_return_type = skip_typeref(type);
3867 if (is_type_function(skipped_return_type)) {
3868 errorf(HERE, "function returning function is not allowed");
3869 type = type_error_type;
3870 } else if (is_type_array(skipped_return_type)) {
3871 errorf(HERE, "function returning array is not allowed");
3872 type = type_error_type;
3874 type = function_type;
3879 case CONSTRUCT_POINTER: {
3880 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3881 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3882 pointer_type->pointer.points_to = type;
3883 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3885 type = pointer_type;
3889 case CONSTRUCT_ARRAY: {
3890 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3891 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3893 expression_t *size_expression = parsed_array->size;
3894 if (size_expression != NULL) {
3896 = create_implicit_cast(size_expression, type_size_t);
3899 array_type->base.qualifiers = parsed_array->type_qualifiers;
3900 array_type->array.element_type = type;
3901 array_type->array.is_static = parsed_array->is_static;
3902 array_type->array.is_variable = parsed_array->is_variable;
3903 array_type->array.size_expression = size_expression;
3905 if (size_expression != NULL) {
3906 if (is_constant_expression(size_expression)) {
3907 array_type->array.size_constant = true;
3908 array_type->array.size
3909 = fold_constant(size_expression);
3911 array_type->array.is_vla = true;
3915 type_t *skipped_type = skip_typeref(type);
3916 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3917 errorf(HERE, "array of void is not allowed");
3918 type = type_error_type;
3926 type_t *hashed_type = typehash_insert(type);
3927 if (hashed_type != type) {
3928 /* the function type was constructed earlier freeing it here will
3929 * destroy other types... */
3930 if (iter->kind != CONSTRUCT_FUNCTION) {
3940 static declaration_t *parse_declarator(
3941 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3943 declaration_t *const declaration = allocate_declaration_zero();
3944 declaration->declared_storage_class = specifiers->declared_storage_class;
3945 declaration->modifiers = specifiers->modifiers;
3946 declaration->deprecated = specifiers->deprecated;
3947 declaration->deprecated_string = specifiers->deprecated_string;
3948 declaration->get_property_sym = specifiers->get_property_sym;
3949 declaration->put_property_sym = specifiers->put_property_sym;
3950 declaration->is_inline = specifiers->is_inline;
3952 declaration->storage_class = specifiers->declared_storage_class;
3953 if (declaration->storage_class == STORAGE_CLASS_NONE
3954 && scope != global_scope) {
3955 declaration->storage_class = STORAGE_CLASS_AUTO;
3958 if (specifiers->alignment != 0) {
3959 /* TODO: add checks here */
3960 declaration->alignment = specifiers->alignment;
3963 construct_type_t *construct_type
3964 = parse_inner_declarator(declaration, may_be_abstract);
3965 type_t *const type = specifiers->type;
3966 declaration->type = construct_declarator_type(construct_type, type);
3968 parse_declaration_attributes(declaration);
3970 fix_declaration_type(declaration);
3972 if (construct_type != NULL) {
3973 obstack_free(&temp_obst, construct_type);
3979 static type_t *parse_abstract_declarator(type_t *base_type)
3981 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3983 type_t *result = construct_declarator_type(construct_type, base_type);
3984 if (construct_type != NULL) {
3985 obstack_free(&temp_obst, construct_type);
3991 static declaration_t *append_declaration(declaration_t* const declaration)
3993 if (last_declaration != NULL) {
3994 last_declaration->next = declaration;
3996 scope->declarations = declaration;
3998 last_declaration = declaration;
4003 * Check if the declaration of main is suspicious. main should be a
4004 * function with external linkage, returning int, taking either zero
4005 * arguments, two, or three arguments of appropriate types, ie.
4007 * int main([ int argc, char **argv [, char **env ] ]).
4009 * @param decl the declaration to check
4010 * @param type the function type of the declaration
4012 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
4014 if (decl->storage_class == STORAGE_CLASS_STATIC) {
4015 warningf(&decl->source_position,
4016 "'main' is normally a non-static function");
4018 if (skip_typeref(func_type->return_type) != type_int) {
4019 warningf(&decl->source_position,
4020 "return type of 'main' should be 'int', but is '%T'",
4021 func_type->return_type);
4023 const function_parameter_t *parm = func_type->parameters;
4025 type_t *const first_type = parm->type;
4026 if (!types_compatible(skip_typeref(first_type), type_int)) {
4027 warningf(&decl->source_position,
4028 "first argument of 'main' should be 'int', but is '%T'", first_type);
4032 type_t *const second_type = parm->type;
4033 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
4034 warningf(&decl->source_position,
4035 "second argument of 'main' should be 'char**', but is '%T'", second_type);
4039 type_t *const third_type = parm->type;
4040 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
4041 warningf(&decl->source_position,
4042 "third argument of 'main' should be 'char**', but is '%T'", third_type);
4046 goto warn_arg_count;
4050 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
4056 * Check if a symbol is the equal to "main".
4058 static bool is_sym_main(const symbol_t *const sym)
4060 return strcmp(sym->string, "main") == 0;
4063 static declaration_t *internal_record_declaration(
4064 declaration_t *const declaration,
4065 const bool is_definition)
4067 const symbol_t *const symbol = declaration->symbol;
4068 const namespace_t namespc = (namespace_t)declaration->namespc;
4070 assert(declaration->symbol != NULL);
4071 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4073 type_t *const orig_type = declaration->type;
4074 type_t *const type = skip_typeref(orig_type);
4075 if (is_type_function(type) &&
4076 type->function.unspecified_parameters &&
4077 warning.strict_prototypes &&
4078 previous_declaration == NULL) {
4079 warningf(&declaration->source_position,
4080 "function declaration '%#T' is not a prototype",
4081 orig_type, declaration->symbol);
4084 if (warning.main && is_type_function(type) && is_sym_main(symbol)) {
4085 check_type_of_main(declaration, &type->function);
4088 assert(declaration != previous_declaration);
4089 if (previous_declaration != NULL
4090 && previous_declaration->parent_scope == scope) {
4091 /* can happen for K&R style declarations */
4092 if (previous_declaration->type == NULL) {
4093 previous_declaration->type = declaration->type;
4096 const type_t *prev_type = skip_typeref(previous_declaration->type);
4097 if (!types_compatible(type, prev_type)) {
4098 errorf(&declaration->source_position,
4099 "declaration '%#T' is incompatible with '%#T' (declared %P)",
4100 orig_type, symbol, previous_declaration->type, symbol,
4101 &previous_declaration->source_position);
4103 unsigned old_storage_class = previous_declaration->storage_class;
4104 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
4105 errorf(&declaration->source_position,
4106 "redeclaration of enum entry '%Y' (declared %P)",
4107 symbol, &previous_declaration->source_position);
4108 return previous_declaration;
4111 unsigned new_storage_class = declaration->storage_class;
4113 if (is_type_incomplete(prev_type)) {
4114 previous_declaration->type = type;
4118 /* pretend no storage class means extern for function
4119 * declarations (except if the previous declaration is neither
4120 * none nor extern) */
4121 if (is_type_function(type)) {
4122 if (prev_type->function.unspecified_parameters) {
4123 previous_declaration->type = type;
4127 switch (old_storage_class) {
4128 case STORAGE_CLASS_NONE:
4129 old_storage_class = STORAGE_CLASS_EXTERN;
4132 case STORAGE_CLASS_EXTERN:
4133 if (is_definition) {
4134 if (warning.missing_prototypes &&
4135 prev_type->function.unspecified_parameters &&
4136 !is_sym_main(symbol)) {
4137 warningf(&declaration->source_position,
4138 "no previous prototype for '%#T'",
4141 } else if (new_storage_class == STORAGE_CLASS_NONE) {
4142 new_storage_class = STORAGE_CLASS_EXTERN;
4151 if (old_storage_class == STORAGE_CLASS_EXTERN &&
4152 new_storage_class == STORAGE_CLASS_EXTERN) {
4153 warn_redundant_declaration:
4154 if (!is_definition &&
4155 warning.redundant_decls &&
4156 strcmp(previous_declaration->source_position.input_name, "<builtin>") != 0) {
4157 warningf(&declaration->source_position,
4158 "redundant declaration for '%Y' (declared %P)",
4159 symbol, &previous_declaration->source_position);
4161 } else if (current_function == NULL) {
4162 if (old_storage_class != STORAGE_CLASS_STATIC &&
4163 new_storage_class == STORAGE_CLASS_STATIC) {
4164 errorf(&declaration->source_position,
4165 "static declaration of '%Y' follows non-static declaration (declared %P)",
4166 symbol, &previous_declaration->source_position);
4167 } else if (old_storage_class == STORAGE_CLASS_EXTERN) {
4168 previous_declaration->storage_class = STORAGE_CLASS_NONE;
4169 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
4171 goto warn_redundant_declaration;
4173 } else if (old_storage_class == new_storage_class) {
4174 errorf(&declaration->source_position,
4175 "redeclaration of '%Y' (declared %P)",
4176 symbol, &previous_declaration->source_position);
4178 errorf(&declaration->source_position,
4179 "redeclaration of '%Y' with different linkage (declared %P)",
4180 symbol, &previous_declaration->source_position);
4184 if (declaration->is_inline)
4185 previous_declaration->is_inline = true;
4186 return previous_declaration;
4187 } else if (is_type_function(type)) {
4188 if (is_definition &&
4189 declaration->storage_class != STORAGE_CLASS_STATIC) {
4190 if (warning.missing_prototypes && !is_sym_main(symbol)) {
4191 warningf(&declaration->source_position,
4192 "no previous prototype for '%#T'", orig_type, symbol);
4193 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
4194 warningf(&declaration->source_position,
4195 "no previous declaration for '%#T'", orig_type,
4200 if (warning.missing_declarations &&
4201 scope == global_scope && (
4202 declaration->storage_class == STORAGE_CLASS_NONE ||
4203 declaration->storage_class == STORAGE_CLASS_THREAD
4205 warningf(&declaration->source_position,
4206 "no previous declaration for '%#T'", orig_type, symbol);
4210 assert(declaration->parent_scope == NULL);
4211 assert(scope != NULL);
4213 declaration->parent_scope = scope;
4215 environment_push(declaration);
4216 return append_declaration(declaration);
4219 static declaration_t *record_declaration(declaration_t *declaration)
4221 return internal_record_declaration(declaration, false);
4224 static declaration_t *record_definition(declaration_t *declaration)
4226 return internal_record_declaration(declaration, true);
4229 static void parser_error_multiple_definition(declaration_t *declaration,
4230 const source_position_t *source_position)
4232 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
4233 declaration->symbol, &declaration->source_position);
4236 static bool is_declaration_specifier(const token_t *token,
4237 bool only_specifiers_qualifiers)
4239 switch(token->type) {
4244 return is_typedef_symbol(token->v.symbol);
4246 case T___extension__:
4248 return !only_specifiers_qualifiers;
4255 static void parse_init_declarator_rest(declaration_t *declaration)
4259 type_t *orig_type = declaration->type;
4260 type_t *type = skip_typeref(orig_type);
4262 if (declaration->init.initializer != NULL) {
4263 parser_error_multiple_definition(declaration, HERE);
4266 bool must_be_constant = false;
4267 if (declaration->storage_class == STORAGE_CLASS_STATIC
4268 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
4269 || declaration->parent_scope == global_scope) {
4270 must_be_constant = true;
4273 parse_initializer_env_t env;
4274 env.type = orig_type;
4275 env.must_be_constant = must_be_constant;
4276 env.declaration = declaration;
4278 initializer_t *initializer = parse_initializer(&env);
4280 if (env.type != orig_type) {
4281 orig_type = env.type;
4282 type = skip_typeref(orig_type);
4283 declaration->type = env.type;
4286 if (is_type_function(type)) {
4287 errorf(&declaration->source_position,
4288 "initializers not allowed for function types at declator '%Y' (type '%T')",
4289 declaration->symbol, orig_type);
4291 declaration->init.initializer = initializer;
4295 /* parse rest of a declaration without any declarator */
4296 static void parse_anonymous_declaration_rest(
4297 const declaration_specifiers_t *specifiers,
4298 parsed_declaration_func finished_declaration)
4302 declaration_t *const declaration = allocate_declaration_zero();
4303 declaration->type = specifiers->type;
4304 declaration->declared_storage_class = specifiers->declared_storage_class;
4305 declaration->source_position = specifiers->source_position;
4306 declaration->modifiers = specifiers->modifiers;
4308 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
4309 warningf(&declaration->source_position,
4310 "useless storage class in empty declaration");
4312 declaration->storage_class = STORAGE_CLASS_NONE;
4314 type_t *type = declaration->type;
4315 switch (type->kind) {
4316 case TYPE_COMPOUND_STRUCT:
4317 case TYPE_COMPOUND_UNION: {
4318 if (type->compound.declaration->symbol == NULL) {
4319 warningf(&declaration->source_position,
4320 "unnamed struct/union that defines no instances");
4329 warningf(&declaration->source_position, "empty declaration");
4333 finished_declaration(declaration);
4336 static void parse_declaration_rest(declaration_t *ndeclaration,
4337 const declaration_specifiers_t *specifiers,
4338 parsed_declaration_func finished_declaration)
4340 add_anchor_token(';');
4341 add_anchor_token('=');
4342 add_anchor_token(',');
4344 declaration_t *declaration = finished_declaration(ndeclaration);
4346 type_t *orig_type = declaration->type;
4347 type_t *type = skip_typeref(orig_type);
4349 if (type->kind != TYPE_FUNCTION &&
4350 declaration->is_inline &&
4351 is_type_valid(type)) {
4352 warningf(&declaration->source_position,
4353 "variable '%Y' declared 'inline'\n", declaration->symbol);
4356 if (token.type == '=') {
4357 parse_init_declarator_rest(declaration);
4360 if (token.type != ',')
4364 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4369 rem_anchor_token(';');
4370 rem_anchor_token('=');
4371 rem_anchor_token(',');
4374 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4376 symbol_t *symbol = declaration->symbol;
4377 if (symbol == NULL) {
4378 errorf(HERE, "anonymous declaration not valid as function parameter");
4381 namespace_t namespc = (namespace_t) declaration->namespc;
4382 if (namespc != NAMESPACE_NORMAL) {
4383 return record_declaration(declaration);
4386 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4387 if (previous_declaration == NULL ||
4388 previous_declaration->parent_scope != scope) {
4389 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4394 if (previous_declaration->type == NULL) {
4395 previous_declaration->type = declaration->type;
4396 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4397 previous_declaration->storage_class = declaration->storage_class;
4398 previous_declaration->parent_scope = scope;
4399 return previous_declaration;
4401 return record_declaration(declaration);
4405 static void parse_declaration(parsed_declaration_func finished_declaration)
4407 declaration_specifiers_t specifiers;
4408 memset(&specifiers, 0, sizeof(specifiers));
4409 parse_declaration_specifiers(&specifiers);
4411 if (token.type == ';') {
4412 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4414 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4415 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4419 static type_t *get_default_promoted_type(type_t *orig_type)
4421 type_t *result = orig_type;
4423 type_t *type = skip_typeref(orig_type);
4424 if (is_type_integer(type)) {
4425 result = promote_integer(type);
4426 } else if (type == type_float) {
4427 result = type_double;
4433 static void parse_kr_declaration_list(declaration_t *declaration)
4435 type_t *type = skip_typeref(declaration->type);
4436 if (!is_type_function(type))
4439 if (!type->function.kr_style_parameters)
4442 /* push function parameters */
4443 int top = environment_top();
4444 scope_t *last_scope = scope;
4445 set_scope(&declaration->scope);
4447 declaration_t *parameter = declaration->scope.declarations;
4448 for ( ; parameter != NULL; parameter = parameter->next) {
4449 assert(parameter->parent_scope == NULL);
4450 parameter->parent_scope = scope;
4451 environment_push(parameter);
4454 /* parse declaration list */
4455 while (is_declaration_specifier(&token, false)) {
4456 parse_declaration(finished_kr_declaration);
4459 /* pop function parameters */
4460 assert(scope == &declaration->scope);
4461 set_scope(last_scope);
4462 environment_pop_to(top);
4464 /* update function type */
4465 type_t *new_type = duplicate_type(type);
4467 function_parameter_t *parameters = NULL;
4468 function_parameter_t *last_parameter = NULL;
4470 declaration_t *parameter_declaration = declaration->scope.declarations;
4471 for( ; parameter_declaration != NULL;
4472 parameter_declaration = parameter_declaration->next) {
4473 type_t *parameter_type = parameter_declaration->type;
4474 if (parameter_type == NULL) {
4476 errorf(HERE, "no type specified for function parameter '%Y'",
4477 parameter_declaration->symbol);
4479 if (warning.implicit_int) {
4480 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4481 parameter_declaration->symbol);
4483 parameter_type = type_int;
4484 parameter_declaration->type = parameter_type;
4488 semantic_parameter(parameter_declaration);
4489 parameter_type = parameter_declaration->type;
4492 * we need the default promoted types for the function type
4494 parameter_type = get_default_promoted_type(parameter_type);
4496 function_parameter_t *function_parameter
4497 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4498 memset(function_parameter, 0, sizeof(function_parameter[0]));
4500 function_parameter->type = parameter_type;
4501 if (last_parameter != NULL) {
4502 last_parameter->next = function_parameter;
4504 parameters = function_parameter;
4506 last_parameter = function_parameter;
4509 /* § 6.9.1.7: A K&R style parameter list does NOT act as a function
4511 new_type->function.parameters = parameters;
4512 new_type->function.unspecified_parameters = true;
4514 type = typehash_insert(new_type);
4515 if (type != new_type) {
4516 obstack_free(type_obst, new_type);
4519 declaration->type = type;
4522 static bool first_err = true;
4525 * When called with first_err set, prints the name of the current function,
4528 static void print_in_function(void) {
4531 diagnosticf("%s: In function '%Y':\n",
4532 current_function->source_position.input_name,
4533 current_function->symbol);
4538 * Check if all labels are defined in the current function.
4539 * Check if all labels are used in the current function.
4541 static void check_labels(void)
4543 for (const goto_statement_t *goto_statement = goto_first;
4544 goto_statement != NULL;
4545 goto_statement = goto_statement->next) {
4546 declaration_t *label = goto_statement->label;
4549 if (label->source_position.input_name == NULL) {
4550 print_in_function();
4551 errorf(&goto_statement->base.source_position,
4552 "label '%Y' used but not defined", label->symbol);
4555 goto_first = goto_last = NULL;
4557 if (warning.unused_label) {
4558 for (const label_statement_t *label_statement = label_first;
4559 label_statement != NULL;
4560 label_statement = label_statement->next) {
4561 const declaration_t *label = label_statement->label;
4563 if (! label->used) {
4564 print_in_function();
4565 warningf(&label_statement->base.source_position,
4566 "label '%Y' defined but not used", label->symbol);
4570 label_first = label_last = NULL;
4574 * Check declarations of current_function for unused entities.
4576 static void check_declarations(void)
4578 if (warning.unused_parameter) {
4579 const scope_t *scope = ¤t_function->scope;
4581 const declaration_t *parameter = scope->declarations;
4582 for (; parameter != NULL; parameter = parameter->next) {
4583 if (! parameter->used) {
4584 print_in_function();
4585 warningf(¶meter->source_position,
4586 "unused parameter '%Y'", parameter->symbol);
4590 if (warning.unused_variable) {
4594 static void parse_external_declaration(void)
4596 /* function-definitions and declarations both start with declaration
4598 declaration_specifiers_t specifiers;
4599 memset(&specifiers, 0, sizeof(specifiers));
4601 add_anchor_token(';');
4602 parse_declaration_specifiers(&specifiers);
4603 rem_anchor_token(';');
4605 /* must be a declaration */
4606 if (token.type == ';') {
4607 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4611 add_anchor_token(',');
4612 add_anchor_token('=');
4613 rem_anchor_token(';');
4615 /* declarator is common to both function-definitions and declarations */
4616 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4618 rem_anchor_token(',');
4619 rem_anchor_token('=');
4620 rem_anchor_token(';');
4622 /* must be a declaration */
4623 switch (token.type) {
4626 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4630 parse_declaration_rest(ndeclaration, &specifiers, record_definition);
4634 /* must be a function definition */
4635 parse_kr_declaration_list(ndeclaration);
4637 if (token.type != '{') {
4638 parse_error_expected("while parsing function definition", '{', NULL);
4639 eat_until_matching_token(';');
4643 type_t *type = ndeclaration->type;
4645 /* note that we don't skip typerefs: the standard doesn't allow them here
4646 * (so we can't use is_type_function here) */
4647 if (type->kind != TYPE_FUNCTION) {
4648 if (is_type_valid(type)) {
4649 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4650 type, ndeclaration->symbol);
4656 /* § 6.7.5.3 (14) a function definition with () means no
4657 * parameters (and not unspecified parameters) */
4658 if (type->function.unspecified_parameters
4659 && type->function.parameters == NULL
4660 && !type->function.kr_style_parameters) {
4661 type_t *duplicate = duplicate_type(type);
4662 duplicate->function.unspecified_parameters = false;
4664 type = typehash_insert(duplicate);
4665 if (type != duplicate) {
4666 obstack_free(type_obst, duplicate);
4668 ndeclaration->type = type;
4671 declaration_t *const declaration = record_definition(ndeclaration);
4672 if (ndeclaration != declaration) {
4673 declaration->scope = ndeclaration->scope;
4675 type = skip_typeref(declaration->type);
4677 /* push function parameters and switch scope */
4678 int top = environment_top();
4679 scope_t *last_scope = scope;
4680 set_scope(&declaration->scope);
4682 declaration_t *parameter = declaration->scope.declarations;
4683 for( ; parameter != NULL; parameter = parameter->next) {
4684 if (parameter->parent_scope == &ndeclaration->scope) {
4685 parameter->parent_scope = scope;
4687 assert(parameter->parent_scope == NULL
4688 || parameter->parent_scope == scope);
4689 parameter->parent_scope = scope;
4690 if (parameter->symbol == NULL) {
4691 errorf(&ndeclaration->source_position, "parameter name omitted");
4694 environment_push(parameter);
4697 if (declaration->init.statement != NULL) {
4698 parser_error_multiple_definition(declaration, HERE);
4701 /* parse function body */
4702 int label_stack_top = label_top();
4703 declaration_t *old_current_function = current_function;
4704 current_function = declaration;
4706 declaration->init.statement = parse_compound_statement(false);
4709 check_declarations();
4711 assert(current_function == declaration);
4712 current_function = old_current_function;
4713 label_pop_to(label_stack_top);
4716 assert(scope == &declaration->scope);
4717 set_scope(last_scope);
4718 environment_pop_to(top);
4721 static type_t *make_bitfield_type(type_t *base_type, expression_t *size,
4722 source_position_t *source_position)
4724 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4726 type->bitfield.base_type = base_type;
4727 type->bitfield.size = size;
4732 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4735 declaration_t *iter = compound_declaration->scope.declarations;
4736 for( ; iter != NULL; iter = iter->next) {
4737 if (iter->namespc != NAMESPACE_NORMAL)
4740 if (iter->symbol == NULL) {
4741 type_t *type = skip_typeref(iter->type);
4742 if (is_type_compound(type)) {
4743 declaration_t *result
4744 = find_compound_entry(type->compound.declaration, symbol);
4751 if (iter->symbol == symbol) {
4759 static void parse_compound_declarators(declaration_t *struct_declaration,
4760 const declaration_specifiers_t *specifiers)
4762 declaration_t *last_declaration = struct_declaration->scope.declarations;
4763 if (last_declaration != NULL) {
4764 while(last_declaration->next != NULL) {
4765 last_declaration = last_declaration->next;
4770 declaration_t *declaration;
4772 if (token.type == ':') {
4773 source_position_t source_position = *HERE;
4776 type_t *base_type = specifiers->type;
4777 expression_t *size = parse_constant_expression();
4779 if (!is_type_integer(skip_typeref(base_type))) {
4780 errorf(HERE, "bitfield base type '%T' is not an integer type",
4784 type_t *type = make_bitfield_type(base_type, size, &source_position);
4786 declaration = allocate_declaration_zero();
4787 declaration->namespc = NAMESPACE_NORMAL;
4788 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4789 declaration->storage_class = STORAGE_CLASS_NONE;
4790 declaration->source_position = source_position;
4791 declaration->modifiers = specifiers->modifiers;
4792 declaration->type = type;
4794 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4796 type_t *orig_type = declaration->type;
4797 type_t *type = skip_typeref(orig_type);
4799 if (token.type == ':') {
4800 source_position_t source_position = *HERE;
4802 expression_t *size = parse_constant_expression();
4804 if (!is_type_integer(type)) {
4805 errorf(HERE, "bitfield base type '%T' is not an "
4806 "integer type", orig_type);
4809 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4810 declaration->type = bitfield_type;
4812 /* TODO we ignore arrays for now... what is missing is a check
4813 * that they're at the end of the struct */
4814 if (is_type_incomplete(type) && !is_type_array(type)) {
4816 "compound member '%Y' has incomplete type '%T'",
4817 declaration->symbol, orig_type);
4818 } else if (is_type_function(type)) {
4819 errorf(HERE, "compound member '%Y' must not have function "
4820 "type '%T'", declaration->symbol, orig_type);
4825 /* make sure we don't define a symbol multiple times */
4826 symbol_t *symbol = declaration->symbol;
4827 if (symbol != NULL) {
4828 declaration_t *prev_decl
4829 = find_compound_entry(struct_declaration, symbol);
4831 if (prev_decl != NULL) {
4832 assert(prev_decl->symbol == symbol);
4833 errorf(&declaration->source_position,
4834 "multiple declarations of symbol '%Y' (declared %P)",
4835 symbol, &prev_decl->source_position);
4839 /* append declaration */
4840 if (last_declaration != NULL) {
4841 last_declaration->next = declaration;
4843 struct_declaration->scope.declarations = declaration;
4845 last_declaration = declaration;
4847 if (token.type != ',')
4857 static void parse_compound_type_entries(declaration_t *compound_declaration)
4860 add_anchor_token('}');
4862 while(token.type != '}' && token.type != T_EOF) {
4863 declaration_specifiers_t specifiers;
4864 memset(&specifiers, 0, sizeof(specifiers));
4865 parse_declaration_specifiers(&specifiers);
4867 parse_compound_declarators(compound_declaration, &specifiers);
4869 rem_anchor_token('}');
4871 if (token.type == T_EOF) {
4872 errorf(HERE, "EOF while parsing struct");
4877 static type_t *parse_typename(void)
4879 declaration_specifiers_t specifiers;
4880 memset(&specifiers, 0, sizeof(specifiers));
4881 parse_declaration_specifiers(&specifiers);
4882 if (specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4883 /* TODO: improve error message, user does probably not know what a
4884 * storage class is...
4886 errorf(HERE, "typename may not have a storage class");
4889 type_t *result = parse_abstract_declarator(specifiers.type);
4897 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4898 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4899 expression_t *left);
4901 typedef struct expression_parser_function_t expression_parser_function_t;
4902 struct expression_parser_function_t {
4903 unsigned precedence;
4904 parse_expression_function parser;
4905 unsigned infix_precedence;
4906 parse_expression_infix_function infix_parser;
4909 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4912 * Prints an error message if an expression was expected but not read
4914 static expression_t *expected_expression_error(void)
4916 /* skip the error message if the error token was read */
4917 if (token.type != T_ERROR) {
4918 errorf(HERE, "expected expression, got token '%K'", &token);
4922 return create_invalid_expression();
4926 * Parse a string constant.
4928 static expression_t *parse_string_const(void)
4931 if (token.type == T_STRING_LITERAL) {
4932 string_t res = token.v.string;
4934 while (token.type == T_STRING_LITERAL) {
4935 res = concat_strings(&res, &token.v.string);
4938 if (token.type != T_WIDE_STRING_LITERAL) {
4939 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4940 /* note: that we use type_char_ptr here, which is already the
4941 * automatic converted type. revert_automatic_type_conversion
4942 * will construct the array type */
4943 cnst->base.type = type_char_ptr;
4944 cnst->string.value = res;
4948 wres = concat_string_wide_string(&res, &token.v.wide_string);
4950 wres = token.v.wide_string;
4955 switch (token.type) {
4956 case T_WIDE_STRING_LITERAL:
4957 wres = concat_wide_strings(&wres, &token.v.wide_string);
4960 case T_STRING_LITERAL:
4961 wres = concat_wide_string_string(&wres, &token.v.string);
4965 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4966 cnst->base.type = type_wchar_t_ptr;
4967 cnst->wide_string.value = wres;
4976 * Parse an integer constant.
4978 static expression_t *parse_int_const(void)
4980 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4981 cnst->base.source_position = *HERE;
4982 cnst->base.type = token.datatype;
4983 cnst->conste.v.int_value = token.v.intvalue;
4991 * Parse a character constant.
4993 static expression_t *parse_character_constant(void)
4995 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4997 cnst->base.source_position = *HERE;
4998 cnst->base.type = token.datatype;
4999 cnst->conste.v.character = token.v.string;
5001 if (cnst->conste.v.character.size != 1) {
5002 if (warning.multichar && (c_mode & _GNUC)) {
5004 warningf(HERE, "multi-character character constant");
5006 errorf(HERE, "more than 1 characters in character constant");
5015 * Parse a wide character constant.
5017 static expression_t *parse_wide_character_constant(void)
5019 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
5021 cnst->base.source_position = *HERE;
5022 cnst->base.type = token.datatype;
5023 cnst->conste.v.wide_character = token.v.wide_string;
5025 if (cnst->conste.v.wide_character.size != 1) {
5026 if (warning.multichar && (c_mode & _GNUC)) {
5028 warningf(HERE, "multi-character character constant");
5030 errorf(HERE, "more than 1 characters in character constant");
5039 * Parse a float constant.
5041 static expression_t *parse_float_const(void)
5043 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5044 cnst->base.type = token.datatype;
5045 cnst->conste.v.float_value = token.v.floatvalue;
5052 static declaration_t *create_implicit_function(symbol_t *symbol,
5053 const source_position_t *source_position)
5055 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
5056 ntype->function.return_type = type_int;
5057 ntype->function.unspecified_parameters = true;
5059 type_t *type = typehash_insert(ntype);
5060 if (type != ntype) {
5064 declaration_t *const declaration = allocate_declaration_zero();
5065 declaration->storage_class = STORAGE_CLASS_EXTERN;
5066 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
5067 declaration->type = type;
5068 declaration->symbol = symbol;
5069 declaration->source_position = *source_position;
5071 bool strict_prototypes_old = warning.strict_prototypes;
5072 warning.strict_prototypes = false;
5073 record_declaration(declaration);
5074 warning.strict_prototypes = strict_prototypes_old;
5080 * Creates a return_type (func)(argument_type) function type if not
5083 static type_t *make_function_2_type(type_t *return_type, type_t *argument_type1,
5084 type_t *argument_type2)
5086 function_parameter_t *parameter2
5087 = obstack_alloc(type_obst, sizeof(parameter2[0]));
5088 memset(parameter2, 0, sizeof(parameter2[0]));
5089 parameter2->type = argument_type2;
5091 function_parameter_t *parameter1
5092 = obstack_alloc(type_obst, sizeof(parameter1[0]));
5093 memset(parameter1, 0, sizeof(parameter1[0]));
5094 parameter1->type = argument_type1;
5095 parameter1->next = parameter2;
5097 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5098 type->function.return_type = return_type;
5099 type->function.parameters = parameter1;
5101 type_t *result = typehash_insert(type);
5102 if (result != type) {
5110 * Creates a return_type (func)(argument_type) function type if not
5113 * @param return_type the return type
5114 * @param argument_type the argument type
5116 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
5118 function_parameter_t *parameter
5119 = obstack_alloc(type_obst, sizeof(parameter[0]));
5120 memset(parameter, 0, sizeof(parameter[0]));
5121 parameter->type = argument_type;
5123 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5124 type->function.return_type = return_type;
5125 type->function.parameters = parameter;
5127 type_t *result = typehash_insert(type);
5128 if (result != type) {
5135 static type_t *make_function_0_type(type_t *return_type)
5137 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
5138 type->function.return_type = return_type;
5139 type->function.parameters = NULL;
5141 type_t *result = typehash_insert(type);
5142 if (result != type) {
5150 * Creates a function type for some function like builtins.
5152 * @param symbol the symbol describing the builtin
5154 static type_t *get_builtin_symbol_type(symbol_t *symbol)
5156 switch(symbol->ID) {
5157 case T___builtin_alloca:
5158 return make_function_1_type(type_void_ptr, type_size_t);
5159 case T___builtin_huge_val:
5160 return make_function_0_type(type_double);
5161 case T___builtin_nan:
5162 return make_function_1_type(type_double, type_char_ptr);
5163 case T___builtin_nanf:
5164 return make_function_1_type(type_float, type_char_ptr);
5165 case T___builtin_nand:
5166 return make_function_1_type(type_long_double, type_char_ptr);
5167 case T___builtin_va_end:
5168 return make_function_1_type(type_void, type_valist);
5169 case T___builtin_expect:
5170 return make_function_2_type(type_long, type_long, type_long);
5172 internal_errorf(HERE, "not implemented builtin symbol found");
5177 * Performs automatic type cast as described in § 6.3.2.1.
5179 * @param orig_type the original type
5181 static type_t *automatic_type_conversion(type_t *orig_type)
5183 type_t *type = skip_typeref(orig_type);
5184 if (is_type_array(type)) {
5185 array_type_t *array_type = &type->array;
5186 type_t *element_type = array_type->element_type;
5187 unsigned qualifiers = array_type->base.qualifiers;
5189 return make_pointer_type(element_type, qualifiers);
5192 if (is_type_function(type)) {
5193 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5200 * reverts the automatic casts of array to pointer types and function
5201 * to function-pointer types as defined § 6.3.2.1
5203 type_t *revert_automatic_type_conversion(const expression_t *expression)
5205 switch (expression->kind) {
5206 case EXPR_REFERENCE: return expression->reference.declaration->type;
5207 case EXPR_SELECT: return expression->select.compound_entry->type;
5209 case EXPR_UNARY_DEREFERENCE: {
5210 const expression_t *const value = expression->unary.value;
5211 type_t *const type = skip_typeref(value->base.type);
5212 assert(is_type_pointer(type));
5213 return type->pointer.points_to;
5216 case EXPR_BUILTIN_SYMBOL:
5217 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
5219 case EXPR_ARRAY_ACCESS: {
5220 const expression_t *array_ref = expression->array_access.array_ref;
5221 type_t *type_left = skip_typeref(array_ref->base.type);
5222 if (!is_type_valid(type_left))
5224 assert(is_type_pointer(type_left));
5225 return type_left->pointer.points_to;
5228 case EXPR_STRING_LITERAL: {
5229 size_t size = expression->string.value.size;
5230 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
5233 case EXPR_WIDE_STRING_LITERAL: {
5234 size_t size = expression->wide_string.value.size;
5235 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
5238 case EXPR_COMPOUND_LITERAL:
5239 return expression->compound_literal.type;
5244 return expression->base.type;
5247 static expression_t *parse_reference(void)
5249 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
5251 reference_expression_t *ref = &expression->reference;
5252 symbol_t *const symbol = token.v.symbol;
5254 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
5256 source_position_t source_position = token.source_position;
5259 if (declaration == NULL) {
5260 if (! strict_mode && token.type == '(') {
5261 /* an implicitly defined function */
5262 if (warning.implicit_function_declaration) {
5263 warningf(HERE, "implicit declaration of function '%Y'",
5267 declaration = create_implicit_function(symbol,
5270 errorf(HERE, "unknown symbol '%Y' found.", symbol);
5271 return create_invalid_expression();
5275 type_t *type = declaration->type;
5277 /* we always do the auto-type conversions; the & and sizeof parser contains
5278 * code to revert this! */
5279 type = automatic_type_conversion(type);
5281 ref->declaration = declaration;
5282 ref->base.type = type;
5284 /* this declaration is used */
5285 declaration->used = true;
5287 /* check for deprecated functions */
5288 if (declaration->deprecated != 0) {
5289 const char *prefix = "";
5290 if (is_type_function(declaration->type))
5291 prefix = "function ";
5293 if (declaration->deprecated_string != NULL) {
5294 warningf(&source_position,
5295 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
5296 declaration->deprecated_string);
5298 warningf(&source_position,
5299 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
5306 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
5310 /* TODO check if explicit cast is allowed and issue warnings/errors */
5313 static expression_t *parse_compound_literal(type_t *type)
5315 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
5317 parse_initializer_env_t env;
5319 env.declaration = NULL;
5320 env.must_be_constant = false;
5321 initializer_t *initializer = parse_initializer(&env);
5324 expression->compound_literal.initializer = initializer;
5325 expression->compound_literal.type = type;
5326 expression->base.type = automatic_type_conversion(type);
5332 * Parse a cast expression.
5334 static expression_t *parse_cast(void)
5336 source_position_t source_position = token.source_position;
5338 type_t *type = parse_typename();
5340 /* matching add_anchor_token() is at call site */
5341 rem_anchor_token(')');
5344 if (token.type == '{') {
5345 return parse_compound_literal(type);
5348 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
5349 cast->base.source_position = source_position;
5351 expression_t *value = parse_sub_expression(20);
5353 check_cast_allowed(value, type);
5355 cast->base.type = type;
5356 cast->unary.value = value;
5360 return create_invalid_expression();
5364 * Parse a statement expression.
5366 static expression_t *parse_statement_expression(void)
5368 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
5370 statement_t *statement = parse_compound_statement(true);
5371 expression->statement.statement = statement;
5372 expression->base.source_position = statement->base.source_position;
5374 /* find last statement and use its type */
5375 type_t *type = type_void;
5376 const statement_t *stmt = statement->compound.statements;
5378 while (stmt->base.next != NULL)
5379 stmt = stmt->base.next;
5381 if (stmt->kind == STATEMENT_EXPRESSION) {
5382 type = stmt->expression.expression->base.type;
5385 warningf(&expression->base.source_position, "empty statement expression ({})");
5387 expression->base.type = type;
5393 return create_invalid_expression();
5397 * Parse a braced expression.
5399 static expression_t *parse_brace_expression(void)
5402 add_anchor_token(')');
5404 switch(token.type) {
5406 /* gcc extension: a statement expression */
5407 return parse_statement_expression();
5411 return parse_cast();
5413 if (is_typedef_symbol(token.v.symbol)) {
5414 return parse_cast();
5418 expression_t *result = parse_expression();
5419 rem_anchor_token(')');
5424 return create_invalid_expression();
5427 static expression_t *parse_function_keyword(void)
5432 if (current_function == NULL) {
5433 errorf(HERE, "'__func__' used outside of a function");
5436 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5437 expression->base.type = type_char_ptr;
5438 expression->funcname.kind = FUNCNAME_FUNCTION;
5443 static expression_t *parse_pretty_function_keyword(void)
5445 eat(T___PRETTY_FUNCTION__);
5447 if (current_function == NULL) {
5448 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5451 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5452 expression->base.type = type_char_ptr;
5453 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5458 static expression_t *parse_funcsig_keyword(void)
5462 if (current_function == NULL) {
5463 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5466 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5467 expression->base.type = type_char_ptr;
5468 expression->funcname.kind = FUNCNAME_FUNCSIG;
5473 static expression_t *parse_funcdname_keyword(void)
5475 eat(T___FUNCDNAME__);
5477 if (current_function == NULL) {
5478 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5481 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5482 expression->base.type = type_char_ptr;
5483 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5488 static designator_t *parse_designator(void)
5490 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5491 result->source_position = *HERE;
5493 if (token.type != T_IDENTIFIER) {
5494 parse_error_expected("while parsing member designator",
5495 T_IDENTIFIER, NULL);
5498 result->symbol = token.v.symbol;
5501 designator_t *last_designator = result;
5503 if (token.type == '.') {
5505 if (token.type != T_IDENTIFIER) {
5506 parse_error_expected("while parsing member designator",
5507 T_IDENTIFIER, NULL);
5510 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5511 designator->source_position = *HERE;
5512 designator->symbol = token.v.symbol;
5515 last_designator->next = designator;
5516 last_designator = designator;
5519 if (token.type == '[') {
5521 add_anchor_token(']');
5522 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5523 designator->source_position = *HERE;
5524 designator->array_index = parse_expression();
5525 rem_anchor_token(']');
5527 if (designator->array_index == NULL) {
5531 last_designator->next = designator;
5532 last_designator = designator;
5544 * Parse the __builtin_offsetof() expression.
5546 static expression_t *parse_offsetof(void)
5548 eat(T___builtin_offsetof);
5550 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5551 expression->base.type = type_size_t;
5554 add_anchor_token(',');
5555 type_t *type = parse_typename();
5556 rem_anchor_token(',');
5558 add_anchor_token(')');
5559 designator_t *designator = parse_designator();
5560 rem_anchor_token(')');
5563 expression->offsetofe.type = type;
5564 expression->offsetofe.designator = designator;
5567 memset(&path, 0, sizeof(path));
5568 path.top_type = type;
5569 path.path = NEW_ARR_F(type_path_entry_t, 0);
5571 descend_into_subtype(&path);
5573 if (!walk_designator(&path, designator, true)) {
5574 return create_invalid_expression();
5577 DEL_ARR_F(path.path);
5581 return create_invalid_expression();
5585 * Parses a _builtin_va_start() expression.
5587 static expression_t *parse_va_start(void)
5589 eat(T___builtin_va_start);
5591 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5594 add_anchor_token(',');
5595 expression->va_starte.ap = parse_assignment_expression();
5596 rem_anchor_token(',');
5598 expression_t *const expr = parse_assignment_expression();
5599 if (expr->kind == EXPR_REFERENCE) {
5600 declaration_t *const decl = expr->reference.declaration;
5602 return create_invalid_expression();
5603 if (decl->parent_scope == ¤t_function->scope &&
5604 decl->next == NULL) {
5605 expression->va_starte.parameter = decl;
5610 errorf(&expr->base.source_position,
5611 "second argument of 'va_start' must be last parameter of the current function");
5613 return create_invalid_expression();
5617 * Parses a _builtin_va_arg() expression.
5619 static expression_t *parse_va_arg(void)
5621 eat(T___builtin_va_arg);
5623 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5626 expression->va_arge.ap = parse_assignment_expression();
5628 expression->base.type = parse_typename();
5633 return create_invalid_expression();
5636 static expression_t *parse_builtin_symbol(void)
5638 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5640 symbol_t *symbol = token.v.symbol;
5642 expression->builtin_symbol.symbol = symbol;
5645 type_t *type = get_builtin_symbol_type(symbol);
5646 type = automatic_type_conversion(type);
5648 expression->base.type = type;
5653 * Parses a __builtin_constant() expression.
5655 static expression_t *parse_builtin_constant(void)
5657 eat(T___builtin_constant_p);
5659 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5662 add_anchor_token(')');
5663 expression->builtin_constant.value = parse_assignment_expression();
5664 rem_anchor_token(')');
5666 expression->base.type = type_int;
5670 return create_invalid_expression();
5674 * Parses a __builtin_prefetch() expression.
5676 static expression_t *parse_builtin_prefetch(void)
5678 eat(T___builtin_prefetch);
5680 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5683 add_anchor_token(')');
5684 expression->builtin_prefetch.adr = parse_assignment_expression();
5685 if (token.type == ',') {
5687 expression->builtin_prefetch.rw = parse_assignment_expression();
5689 if (token.type == ',') {
5691 expression->builtin_prefetch.locality = parse_assignment_expression();
5693 rem_anchor_token(')');
5695 expression->base.type = type_void;
5699 return create_invalid_expression();
5703 * Parses a __builtin_is_*() compare expression.
5705 static expression_t *parse_compare_builtin(void)
5707 expression_t *expression;
5709 switch(token.type) {
5710 case T___builtin_isgreater:
5711 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5713 case T___builtin_isgreaterequal:
5714 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5716 case T___builtin_isless:
5717 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5719 case T___builtin_islessequal:
5720 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5722 case T___builtin_islessgreater:
5723 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5725 case T___builtin_isunordered:
5726 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5729 internal_errorf(HERE, "invalid compare builtin found");
5732 expression->base.source_position = *HERE;
5736 expression->binary.left = parse_assignment_expression();
5738 expression->binary.right = parse_assignment_expression();
5741 type_t *const orig_type_left = expression->binary.left->base.type;
5742 type_t *const orig_type_right = expression->binary.right->base.type;
5744 type_t *const type_left = skip_typeref(orig_type_left);
5745 type_t *const type_right = skip_typeref(orig_type_right);
5746 if (!is_type_float(type_left) && !is_type_float(type_right)) {
5747 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5748 type_error_incompatible("invalid operands in comparison",
5749 &expression->base.source_position, orig_type_left, orig_type_right);
5752 semantic_comparison(&expression->binary);
5757 return create_invalid_expression();
5762 * Parses a __builtin_expect() expression.
5764 static expression_t *parse_builtin_expect(void)
5766 eat(T___builtin_expect);
5768 expression_t *expression
5769 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5772 expression->binary.left = parse_assignment_expression();
5774 expression->binary.right = parse_constant_expression();
5777 expression->base.type = expression->binary.left->base.type;
5781 return create_invalid_expression();
5786 * Parses a MS assume() expression.
5788 static expression_t *parse_assume(void) {
5791 expression_t *expression
5792 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5795 add_anchor_token(')');
5796 expression->unary.value = parse_assignment_expression();
5797 rem_anchor_token(')');
5800 expression->base.type = type_void;
5803 return create_invalid_expression();
5807 * Parse a microsoft __noop expression.
5809 static expression_t *parse_noop_expression(void) {
5810 source_position_t source_position = *HERE;
5813 if (token.type == '(') {
5814 /* parse arguments */
5816 add_anchor_token(')');
5817 add_anchor_token(',');
5819 if (token.type != ')') {
5821 (void)parse_assignment_expression();
5822 if (token.type != ',')
5828 rem_anchor_token(',');
5829 rem_anchor_token(')');
5832 /* the result is a (int)0 */
5833 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5834 cnst->base.source_position = source_position;
5835 cnst->base.type = type_int;
5836 cnst->conste.v.int_value = 0;
5837 cnst->conste.is_ms_noop = true;
5842 return create_invalid_expression();
5846 * Parses a primary expression.
5848 static expression_t *parse_primary_expression(void)
5850 switch (token.type) {
5851 case T_INTEGER: return parse_int_const();
5852 case T_CHARACTER_CONSTANT: return parse_character_constant();
5853 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5854 case T_FLOATINGPOINT: return parse_float_const();
5855 case T_STRING_LITERAL:
5856 case T_WIDE_STRING_LITERAL: return parse_string_const();
5857 case T_IDENTIFIER: return parse_reference();
5858 case T___FUNCTION__:
5859 case T___func__: return parse_function_keyword();
5860 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5861 case T___FUNCSIG__: return parse_funcsig_keyword();
5862 case T___FUNCDNAME__: return parse_funcdname_keyword();
5863 case T___builtin_offsetof: return parse_offsetof();
5864 case T___builtin_va_start: return parse_va_start();
5865 case T___builtin_va_arg: return parse_va_arg();
5866 case T___builtin_expect:
5867 case T___builtin_alloca:
5868 case T___builtin_nan:
5869 case T___builtin_nand:
5870 case T___builtin_nanf:
5871 case T___builtin_huge_val:
5872 case T___builtin_va_end: return parse_builtin_symbol();
5873 case T___builtin_isgreater:
5874 case T___builtin_isgreaterequal:
5875 case T___builtin_isless:
5876 case T___builtin_islessequal:
5877 case T___builtin_islessgreater:
5878 case T___builtin_isunordered: return parse_compare_builtin();
5879 case T___builtin_constant_p: return parse_builtin_constant();
5880 case T___builtin_prefetch: return parse_builtin_prefetch();
5881 case T__assume: return parse_assume();
5883 case '(': return parse_brace_expression();
5884 case T___noop: return parse_noop_expression();
5887 errorf(HERE, "unexpected token %K, expected an expression", &token);
5888 return create_invalid_expression();
5892 * Check if the expression has the character type and issue a warning then.
5894 static void check_for_char_index_type(const expression_t *expression) {
5895 type_t *const type = expression->base.type;
5896 const type_t *const base_type = skip_typeref(type);
5898 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5899 warning.char_subscripts) {
5900 warningf(&expression->base.source_position,
5901 "array subscript has type '%T'", type);
5905 static expression_t *parse_array_expression(unsigned precedence,
5911 add_anchor_token(']');
5913 expression_t *inside = parse_expression();
5915 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5917 array_access_expression_t *array_access = &expression->array_access;
5919 type_t *const orig_type_left = left->base.type;
5920 type_t *const orig_type_inside = inside->base.type;
5922 type_t *const type_left = skip_typeref(orig_type_left);
5923 type_t *const type_inside = skip_typeref(orig_type_inside);
5925 type_t *return_type;
5926 if (is_type_pointer(type_left)) {
5927 return_type = type_left->pointer.points_to;
5928 array_access->array_ref = left;
5929 array_access->index = inside;
5930 check_for_char_index_type(inside);
5931 } else if (is_type_pointer(type_inside)) {
5932 return_type = type_inside->pointer.points_to;
5933 array_access->array_ref = inside;
5934 array_access->index = left;
5935 array_access->flipped = true;
5936 check_for_char_index_type(left);
5938 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5940 "array access on object with non-pointer types '%T', '%T'",
5941 orig_type_left, orig_type_inside);
5943 return_type = type_error_type;
5944 array_access->array_ref = create_invalid_expression();
5947 rem_anchor_token(']');
5948 if (token.type != ']') {
5949 parse_error_expected("Problem while parsing array access", ']', NULL);
5954 return_type = automatic_type_conversion(return_type);
5955 expression->base.type = return_type;
5960 static expression_t *parse_typeprop(expression_kind_t const kind,
5961 source_position_t const pos,
5962 unsigned const precedence)
5964 expression_t *tp_expression = allocate_expression_zero(kind);
5965 tp_expression->base.type = type_size_t;
5966 tp_expression->base.source_position = pos;
5968 char const* const what = kind == EXPR_SIZEOF ? "sizeof" : "alignof";
5970 if (token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5972 add_anchor_token(')');
5973 type_t* const orig_type = parse_typename();
5974 tp_expression->typeprop.type = orig_type;
5976 type_t const* const type = skip_typeref(orig_type);
5977 char const* const wrong_type =
5978 is_type_incomplete(type) ? "incomplete" :
5979 type->kind == TYPE_FUNCTION ? "function designator" :
5980 type->kind == TYPE_BITFIELD ? "bitfield" :
5982 if (wrong_type != NULL) {
5983 errorf(&pos, "operand of %s expression must not be %s type '%T'",
5984 what, wrong_type, type);
5987 rem_anchor_token(')');
5990 expression_t *expression = parse_sub_expression(precedence);
5992 type_t* const orig_type = revert_automatic_type_conversion(expression);
5993 expression->base.type = orig_type;
5995 type_t const* const type = skip_typeref(orig_type);
5996 char const* const wrong_type =
5997 is_type_incomplete(type) ? "incomplete" :
5998 type->kind == TYPE_FUNCTION ? "function designator" :
5999 type->kind == TYPE_BITFIELD ? "bitfield" :
6001 if (wrong_type != NULL) {
6002 errorf(&pos, "operand of %s expression must not be expression of %s type '%T'", what, wrong_type, type);
6005 tp_expression->typeprop.type = expression->base.type;
6006 tp_expression->typeprop.tp_expression = expression;
6009 return tp_expression;
6011 return create_invalid_expression();
6014 static expression_t *parse_sizeof(unsigned precedence)
6016 source_position_t pos = *HERE;
6018 return parse_typeprop(EXPR_SIZEOF, pos, precedence);
6021 static expression_t *parse_alignof(unsigned precedence)
6023 source_position_t pos = *HERE;
6025 return parse_typeprop(EXPR_ALIGNOF, pos, precedence);
6028 static expression_t *parse_select_expression(unsigned precedence,
6029 expression_t *compound)
6032 assert(token.type == '.' || token.type == T_MINUSGREATER);
6034 bool is_pointer = (token.type == T_MINUSGREATER);
6037 expression_t *select = allocate_expression_zero(EXPR_SELECT);
6038 select->select.compound = compound;
6040 if (token.type != T_IDENTIFIER) {
6041 parse_error_expected("while parsing select", T_IDENTIFIER, NULL);
6044 symbol_t *symbol = token.v.symbol;
6045 select->select.symbol = symbol;
6048 type_t *const orig_type = compound->base.type;
6049 type_t *const type = skip_typeref(orig_type);
6051 type_t *type_left = type;
6053 if (!is_type_pointer(type)) {
6054 if (is_type_valid(type)) {
6055 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
6057 return create_invalid_expression();
6059 type_left = type->pointer.points_to;
6061 type_left = skip_typeref(type_left);
6063 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
6064 type_left->kind != TYPE_COMPOUND_UNION) {
6065 if (is_type_valid(type_left)) {
6066 errorf(HERE, "request for member '%Y' in something not a struct or "
6067 "union, but '%T'", symbol, type_left);
6069 return create_invalid_expression();
6072 declaration_t *const declaration = type_left->compound.declaration;
6074 if (!declaration->init.complete) {
6075 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
6077 return create_invalid_expression();
6080 declaration_t *iter = find_compound_entry(declaration, symbol);
6082 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
6083 return create_invalid_expression();
6086 /* we always do the auto-type conversions; the & and sizeof parser contains
6087 * code to revert this! */
6088 type_t *expression_type = automatic_type_conversion(iter->type);
6090 select->select.compound_entry = iter;
6091 select->base.type = expression_type;
6093 type_t *skipped = skip_typeref(iter->type);
6094 if (skipped->kind == TYPE_BITFIELD) {
6095 select->base.type = skipped->bitfield.base_type;
6101 static void check_call_argument(const function_parameter_t *parameter,
6102 call_argument_t *argument)
6104 type_t *expected_type = parameter->type;
6105 type_t *expected_type_skip = skip_typeref(expected_type);
6106 assign_error_t error = ASSIGN_ERROR_INCOMPATIBLE;
6107 expression_t *arg_expr = argument->expression;
6109 /* handle transparent union gnu extension */
6110 if (is_type_union(expected_type_skip)
6111 && (expected_type_skip->base.modifiers
6112 & TYPE_MODIFIER_TRANSPARENT_UNION)) {
6113 declaration_t *union_decl = expected_type_skip->compound.declaration;
6115 declaration_t *declaration = union_decl->scope.declarations;
6116 type_t *best_type = NULL;
6117 for ( ; declaration != NULL; declaration = declaration->next) {
6118 type_t *decl_type = declaration->type;
6119 error = semantic_assign(decl_type, arg_expr);
6120 if (error == ASSIGN_ERROR_INCOMPATIBLE
6121 || error == ASSIGN_ERROR_POINTER_QUALIFIER_MISSING)
6124 if (error == ASSIGN_SUCCESS) {
6125 best_type = decl_type;
6126 } else if (best_type == NULL) {
6127 best_type = decl_type;
6131 if (best_type != NULL) {
6132 expected_type = best_type;
6136 error = semantic_assign(expected_type, arg_expr);
6137 argument->expression = create_implicit_cast(argument->expression,
6140 /* TODO report exact scope in error messages (like "in 3rd parameter") */
6141 report_assign_error(error, expected_type, arg_expr, "function call",
6142 &arg_expr->base.source_position);
6146 * Parse a call expression, ie. expression '( ... )'.
6148 * @param expression the function address
6150 static expression_t *parse_call_expression(unsigned precedence,
6151 expression_t *expression)
6154 expression_t *result = allocate_expression_zero(EXPR_CALL);
6155 result->base.source_position = expression->base.source_position;
6157 call_expression_t *call = &result->call;
6158 call->function = expression;
6160 type_t *const orig_type = expression->base.type;
6161 type_t *const type = skip_typeref(orig_type);
6163 function_type_t *function_type = NULL;
6164 if (is_type_pointer(type)) {
6165 type_t *const to_type = skip_typeref(type->pointer.points_to);
6167 if (is_type_function(to_type)) {
6168 function_type = &to_type->function;
6169 call->base.type = function_type->return_type;
6173 if (function_type == NULL && is_type_valid(type)) {
6174 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
6177 /* parse arguments */
6179 add_anchor_token(')');
6180 add_anchor_token(',');
6182 if (token.type != ')') {
6183 call_argument_t *last_argument = NULL;
6186 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
6188 argument->expression = parse_assignment_expression();
6189 if (last_argument == NULL) {
6190 call->arguments = argument;
6192 last_argument->next = argument;
6194 last_argument = argument;
6196 if (token.type != ',')
6201 rem_anchor_token(',');
6202 rem_anchor_token(')');
6205 if (function_type == NULL)
6208 function_parameter_t *parameter = function_type->parameters;
6209 call_argument_t *argument = call->arguments;
6210 if (!function_type->unspecified_parameters) {
6211 for( ; parameter != NULL && argument != NULL;
6212 parameter = parameter->next, argument = argument->next) {
6213 check_call_argument(parameter, argument);
6216 if (parameter != NULL) {
6217 errorf(HERE, "too few arguments to function '%E'", expression);
6218 } else if (argument != NULL && !function_type->variadic) {
6219 errorf(HERE, "too many arguments to function '%E'", expression);
6223 /* do default promotion */
6224 for( ; argument != NULL; argument = argument->next) {
6225 type_t *type = argument->expression->base.type;
6227 type = get_default_promoted_type(type);
6229 argument->expression
6230 = create_implicit_cast(argument->expression, type);
6233 check_format(&result->call);
6237 return create_invalid_expression();
6240 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
6242 static bool same_compound_type(const type_t *type1, const type_t *type2)
6245 is_type_compound(type1) &&
6246 type1->kind == type2->kind &&
6247 type1->compound.declaration == type2->compound.declaration;
6251 * Parse a conditional expression, ie. 'expression ? ... : ...'.
6253 * @param expression the conditional expression
6255 static expression_t *parse_conditional_expression(unsigned precedence,
6256 expression_t *expression)
6259 add_anchor_token(':');
6261 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
6263 conditional_expression_t *conditional = &result->conditional;
6264 conditional->condition = expression;
6267 type_t *const condition_type_orig = expression->base.type;
6268 type_t *const condition_type = skip_typeref(condition_type_orig);
6269 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
6270 type_error("expected a scalar type in conditional condition",
6271 &expression->base.source_position, condition_type_orig);
6274 expression_t *true_expression = parse_expression();
6275 rem_anchor_token(':');
6277 expression_t *false_expression = parse_sub_expression(precedence);
6279 type_t *const orig_true_type = true_expression->base.type;
6280 type_t *const orig_false_type = false_expression->base.type;
6281 type_t *const true_type = skip_typeref(orig_true_type);
6282 type_t *const false_type = skip_typeref(orig_false_type);
6285 type_t *result_type;
6286 if (is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
6287 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6288 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
6289 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
6290 warningf(&expression->base.source_position,
6291 "ISO C forbids conditional expression with only one void side");
6293 result_type = type_void;
6294 } else if (is_type_arithmetic(true_type)
6295 && is_type_arithmetic(false_type)) {
6296 result_type = semantic_arithmetic(true_type, false_type);
6298 true_expression = create_implicit_cast(true_expression, result_type);
6299 false_expression = create_implicit_cast(false_expression, result_type);
6301 conditional->true_expression = true_expression;
6302 conditional->false_expression = false_expression;
6303 conditional->base.type = result_type;
6304 } else if (same_compound_type(true_type, false_type)) {
6305 /* just take 1 of the 2 types */
6306 result_type = true_type;
6307 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
6308 type_t *pointer_type;
6310 expression_t *other_expression;
6311 if (is_type_pointer(true_type) &&
6312 (!is_type_pointer(false_type) || is_null_pointer_constant(false_expression))) {
6313 pointer_type = true_type;
6314 other_type = false_type;
6315 other_expression = false_expression;
6317 pointer_type = false_type;
6318 other_type = true_type;
6319 other_expression = true_expression;
6322 if (is_null_pointer_constant(other_expression)) {
6323 result_type = pointer_type;
6324 } else if (is_type_pointer(other_type)) {
6325 type_t *to1 = skip_typeref(pointer_type->pointer.points_to);
6326 type_t *to2 = skip_typeref(other_type->pointer.points_to);
6329 if (is_type_atomic(to1, ATOMIC_TYPE_VOID) ||
6330 is_type_atomic(to2, ATOMIC_TYPE_VOID)) {
6332 } else if (types_compatible(get_unqualified_type(to1),
6333 get_unqualified_type(to2))) {
6336 warningf(&expression->base.source_position,
6337 "pointer types '%T' and '%T' in conditional expression are incompatible",
6338 true_type, false_type);
6342 type_t *const copy = duplicate_type(to);
6343 copy->base.qualifiers = to1->base.qualifiers | to2->base.qualifiers;
6345 type_t *const type = typehash_insert(copy);
6349 result_type = make_pointer_type(type, TYPE_QUALIFIER_NONE);
6350 } else if (is_type_integer(other_type)) {
6351 warningf(&expression->base.source_position,
6352 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
6353 result_type = pointer_type;
6355 type_error_incompatible("while parsing conditional",
6356 &expression->base.source_position, true_type, false_type);
6357 result_type = type_error_type;
6360 /* TODO: one pointer to void*, other some pointer */
6362 if (is_type_valid(true_type) && is_type_valid(false_type)) {
6363 type_error_incompatible("while parsing conditional",
6364 &expression->base.source_position, true_type,
6367 result_type = type_error_type;
6370 conditional->true_expression
6371 = create_implicit_cast(true_expression, result_type);
6372 conditional->false_expression
6373 = create_implicit_cast(false_expression, result_type);
6374 conditional->base.type = result_type;
6377 return create_invalid_expression();
6381 * Parse an extension expression.
6383 static expression_t *parse_extension(unsigned precedence)
6385 eat(T___extension__);
6387 /* TODO enable extensions */
6388 expression_t *expression = parse_sub_expression(precedence);
6389 /* TODO disable extensions */
6394 * Parse a __builtin_classify_type() expression.
6396 static expression_t *parse_builtin_classify_type(const unsigned precedence)
6398 eat(T___builtin_classify_type);
6400 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
6401 result->base.type = type_int;
6404 add_anchor_token(')');
6405 expression_t *expression = parse_sub_expression(precedence);
6406 rem_anchor_token(')');
6408 result->classify_type.type_expression = expression;
6412 return create_invalid_expression();
6415 static void check_pointer_arithmetic(const source_position_t *source_position,
6416 type_t *pointer_type,
6417 type_t *orig_pointer_type)
6419 type_t *points_to = pointer_type->pointer.points_to;
6420 points_to = skip_typeref(points_to);
6422 if (is_type_incomplete(points_to) &&
6424 || !is_type_atomic(points_to, ATOMIC_TYPE_VOID))) {
6425 errorf(source_position,
6426 "arithmetic with pointer to incomplete type '%T' not allowed",
6428 } else if (is_type_function(points_to)) {
6429 errorf(source_position,
6430 "arithmetic with pointer to function type '%T' not allowed",
6435 static void semantic_incdec(unary_expression_t *expression)
6437 type_t *const orig_type = expression->value->base.type;
6438 type_t *const type = skip_typeref(orig_type);
6439 if (is_type_pointer(type)) {
6440 check_pointer_arithmetic(&expression->base.source_position,
6442 } else if (!is_type_real(type) && is_type_valid(type)) {
6443 /* TODO: improve error message */
6444 errorf(HERE, "operation needs an arithmetic or pointer type");
6446 expression->base.type = orig_type;
6449 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
6451 type_t *const orig_type = expression->value->base.type;
6452 type_t *const type = skip_typeref(orig_type);
6453 if (!is_type_arithmetic(type)) {
6454 if (is_type_valid(type)) {
6455 /* TODO: improve error message */
6456 errorf(HERE, "operation needs an arithmetic type");
6461 expression->base.type = orig_type;
6464 static void semantic_unexpr_scalar(unary_expression_t *expression)
6466 type_t *const orig_type = expression->value->base.type;
6467 type_t *const type = skip_typeref(orig_type);
6468 if (!is_type_scalar(type)) {
6469 if (is_type_valid(type)) {
6470 errorf(HERE, "operand of ! must be of scalar type");
6475 expression->base.type = orig_type;
6478 static void semantic_unexpr_integer(unary_expression_t *expression)
6480 type_t *const orig_type = expression->value->base.type;
6481 type_t *const type = skip_typeref(orig_type);
6482 if (!is_type_integer(type)) {
6483 if (is_type_valid(type)) {
6484 errorf(HERE, "operand of ~ must be of integer type");
6489 expression->base.type = orig_type;
6492 static void semantic_dereference(unary_expression_t *expression)
6494 type_t *const orig_type = expression->value->base.type;
6495 type_t *const type = skip_typeref(orig_type);
6496 if (!is_type_pointer(type)) {
6497 if (is_type_valid(type)) {
6498 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6503 type_t *result_type = type->pointer.points_to;
6504 result_type = automatic_type_conversion(result_type);
6505 expression->base.type = result_type;
6508 static void set_address_taken(expression_t *expression, bool may_be_register)
6510 if (expression->kind != EXPR_REFERENCE)
6513 declaration_t *const declaration = expression->reference.declaration;
6514 /* happens for parse errors */
6515 if (declaration == NULL)
6518 if (declaration->storage_class == STORAGE_CLASS_REGISTER && !may_be_register) {
6519 errorf(&expression->base.source_position,
6520 "address of register variable '%Y' requested",
6521 declaration->symbol);
6523 declaration->address_taken = 1;
6528 * Check the semantic of the address taken expression.
6530 static void semantic_take_addr(unary_expression_t *expression)
6532 expression_t *value = expression->value;
6533 value->base.type = revert_automatic_type_conversion(value);
6535 type_t *orig_type = value->base.type;
6536 if (!is_type_valid(orig_type))
6539 set_address_taken(value, false);
6541 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6544 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6545 static expression_t *parse_##unexpression_type(unsigned precedence) \
6549 expression_t *unary_expression \
6550 = allocate_expression_zero(unexpression_type); \
6551 unary_expression->base.source_position = *HERE; \
6552 unary_expression->unary.value = parse_sub_expression(precedence); \
6554 sfunc(&unary_expression->unary); \
6556 return unary_expression; \
6559 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6560 semantic_unexpr_arithmetic)
6561 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6562 semantic_unexpr_arithmetic)
6563 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6564 semantic_unexpr_scalar)
6565 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6566 semantic_dereference)
6567 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6569 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6570 semantic_unexpr_integer)
6571 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6573 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6576 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6578 static expression_t *parse_##unexpression_type(unsigned precedence, \
6579 expression_t *left) \
6581 (void) precedence; \
6584 expression_t *unary_expression \
6585 = allocate_expression_zero(unexpression_type); \
6586 unary_expression->unary.value = left; \
6588 sfunc(&unary_expression->unary); \
6590 return unary_expression; \
6593 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6594 EXPR_UNARY_POSTFIX_INCREMENT,
6596 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6597 EXPR_UNARY_POSTFIX_DECREMENT,
6600 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6602 /* TODO: handle complex + imaginary types */
6604 /* § 6.3.1.8 Usual arithmetic conversions */
6605 if (type_left == type_long_double || type_right == type_long_double) {
6606 return type_long_double;
6607 } else if (type_left == type_double || type_right == type_double) {
6609 } else if (type_left == type_float || type_right == type_float) {
6613 type_left = promote_integer(type_left);
6614 type_right = promote_integer(type_right);
6616 if (type_left == type_right)
6619 bool const signed_left = is_type_signed(type_left);
6620 bool const signed_right = is_type_signed(type_right);
6621 int const rank_left = get_rank(type_left);
6622 int const rank_right = get_rank(type_right);
6624 if (signed_left == signed_right)
6625 return rank_left >= rank_right ? type_left : type_right;
6634 u_rank = rank_right;
6635 u_type = type_right;
6637 s_rank = rank_right;
6638 s_type = type_right;
6643 if (u_rank >= s_rank)
6646 if (get_atomic_type_size(s_rank) > get_atomic_type_size(u_rank))
6650 type_t *const type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
6652 case ATOMIC_TYPE_INT: type->atomic.akind = ATOMIC_TYPE_UINT; break;
6653 case ATOMIC_TYPE_LONG: type->atomic.akind = ATOMIC_TYPE_ULONG; break;
6654 case ATOMIC_TYPE_LONGLONG: type->atomic.akind = ATOMIC_TYPE_ULONGLONG; break;
6656 default: panic("invalid atomic type");
6659 type_t* const result = typehash_insert(type);
6667 * Check the semantic restrictions for a binary expression.
6669 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6671 expression_t *const left = expression->left;
6672 expression_t *const right = expression->right;
6673 type_t *const orig_type_left = left->base.type;
6674 type_t *const orig_type_right = right->base.type;
6675 type_t *const type_left = skip_typeref(orig_type_left);
6676 type_t *const type_right = skip_typeref(orig_type_right);
6678 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6679 /* TODO: improve error message */
6680 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6681 errorf(HERE, "operation needs arithmetic types");
6686 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6687 expression->left = create_implicit_cast(left, arithmetic_type);
6688 expression->right = create_implicit_cast(right, arithmetic_type);
6689 expression->base.type = arithmetic_type;
6692 static void semantic_shift_op(binary_expression_t *expression)
6694 expression_t *const left = expression->left;
6695 expression_t *const right = expression->right;
6696 type_t *const orig_type_left = left->base.type;
6697 type_t *const orig_type_right = right->base.type;
6698 type_t * type_left = skip_typeref(orig_type_left);
6699 type_t * type_right = skip_typeref(orig_type_right);
6701 if (!is_type_integer(type_left) || !is_type_integer(type_right)) {
6702 /* TODO: improve error message */
6703 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6704 errorf(HERE, "operation needs integer types");
6709 type_left = promote_integer(type_left);
6710 type_right = promote_integer(type_right);
6712 expression->left = create_implicit_cast(left, type_left);
6713 expression->right = create_implicit_cast(right, type_right);
6714 expression->base.type = type_left;
6717 static void semantic_add(binary_expression_t *expression)
6719 expression_t *const left = expression->left;
6720 expression_t *const right = expression->right;
6721 type_t *const orig_type_left = left->base.type;
6722 type_t *const orig_type_right = right->base.type;
6723 type_t *const type_left = skip_typeref(orig_type_left);
6724 type_t *const type_right = skip_typeref(orig_type_right);
6727 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6728 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6729 expression->left = create_implicit_cast(left, arithmetic_type);
6730 expression->right = create_implicit_cast(right, arithmetic_type);
6731 expression->base.type = arithmetic_type;
6733 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6734 check_pointer_arithmetic(&expression->base.source_position,
6735 type_left, orig_type_left);
6736 expression->base.type = type_left;
6737 } else if (is_type_pointer(type_right) && is_type_integer(type_left)) {
6738 check_pointer_arithmetic(&expression->base.source_position,
6739 type_right, orig_type_right);
6740 expression->base.type = type_right;
6741 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6742 errorf(&expression->base.source_position,
6743 "invalid operands to binary + ('%T', '%T')",
6744 orig_type_left, orig_type_right);
6748 static void semantic_sub(binary_expression_t *expression)
6750 expression_t *const left = expression->left;
6751 expression_t *const right = expression->right;
6752 type_t *const orig_type_left = left->base.type;
6753 type_t *const orig_type_right = right->base.type;
6754 type_t *const type_left = skip_typeref(orig_type_left);
6755 type_t *const type_right = skip_typeref(orig_type_right);
6758 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6759 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6760 expression->left = create_implicit_cast(left, arithmetic_type);
6761 expression->right = create_implicit_cast(right, arithmetic_type);
6762 expression->base.type = arithmetic_type;
6764 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6765 check_pointer_arithmetic(&expression->base.source_position,
6766 type_left, orig_type_left);
6767 expression->base.type = type_left;
6768 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6769 type_t *const unqual_left = get_unqualified_type(skip_typeref(type_left->pointer.points_to));
6770 type_t *const unqual_right = get_unqualified_type(skip_typeref(type_right->pointer.points_to));
6771 if (!types_compatible(unqual_left, unqual_right)) {
6772 errorf(&expression->base.source_position,
6773 "subtracting pointers to incompatible types '%T' and '%T'",
6774 orig_type_left, orig_type_right);
6775 } else if (!is_type_object(unqual_left)) {
6776 if (is_type_atomic(unqual_left, ATOMIC_TYPE_VOID)) {
6777 warningf(&expression->base.source_position,
6778 "subtracting pointers to void");
6780 errorf(&expression->base.source_position,
6781 "subtracting pointers to non-object types '%T'",
6785 expression->base.type = type_ptrdiff_t;
6786 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6787 errorf(HERE, "invalid operands of types '%T' and '%T' to binary '-'",
6788 orig_type_left, orig_type_right);
6793 * Check the semantics of comparison expressions.
6795 * @param expression The expression to check.
6797 static void semantic_comparison(binary_expression_t *expression)
6799 expression_t *left = expression->left;
6800 expression_t *right = expression->right;
6801 type_t *orig_type_left = left->base.type;
6802 type_t *orig_type_right = right->base.type;
6804 type_t *type_left = skip_typeref(orig_type_left);
6805 type_t *type_right = skip_typeref(orig_type_right);
6807 /* TODO non-arithmetic types */
6808 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6809 /* test for signed vs unsigned compares */
6810 if (warning.sign_compare &&
6811 (expression->base.kind != EXPR_BINARY_EQUAL &&
6812 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6813 (is_type_signed(type_left) != is_type_signed(type_right))) {
6815 /* check if 1 of the operands is a constant, in this case we just
6816 * check wether we can safely represent the resulting constant in
6817 * the type of the other operand. */
6818 expression_t *const_expr = NULL;
6819 expression_t *other_expr = NULL;
6821 if (is_constant_expression(left)) {
6824 } else if (is_constant_expression(right)) {
6829 if (const_expr != NULL) {
6830 type_t *other_type = skip_typeref(other_expr->base.type);
6831 long val = fold_constant(const_expr);
6832 /* TODO: check if val can be represented by other_type */
6836 warningf(&expression->base.source_position,
6837 "comparison between signed and unsigned");
6839 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6840 expression->left = create_implicit_cast(left, arithmetic_type);
6841 expression->right = create_implicit_cast(right, arithmetic_type);
6842 expression->base.type = arithmetic_type;
6843 if (warning.float_equal &&
6844 (expression->base.kind == EXPR_BINARY_EQUAL ||
6845 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6846 is_type_float(arithmetic_type)) {
6847 warningf(&expression->base.source_position,
6848 "comparing floating point with == or != is unsafe");
6850 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6851 /* TODO check compatibility */
6852 } else if (is_type_pointer(type_left)) {
6853 expression->right = create_implicit_cast(right, type_left);
6854 } else if (is_type_pointer(type_right)) {
6855 expression->left = create_implicit_cast(left, type_right);
6856 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6857 type_error_incompatible("invalid operands in comparison",
6858 &expression->base.source_position,
6859 type_left, type_right);
6861 expression->base.type = type_int;
6865 * Checks if a compound type has constant fields.
6867 static bool has_const_fields(const compound_type_t *type)
6869 const scope_t *scope = &type->declaration->scope;
6870 const declaration_t *declaration = scope->declarations;
6872 for (; declaration != NULL; declaration = declaration->next) {
6873 if (declaration->namespc != NAMESPACE_NORMAL)
6876 const type_t *decl_type = skip_typeref(declaration->type);
6877 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6884 static bool is_lvalue(const expression_t *expression)
6886 switch (expression->kind) {
6887 case EXPR_REFERENCE:
6888 case EXPR_ARRAY_ACCESS:
6890 case EXPR_UNARY_DEREFERENCE:
6898 static bool is_valid_assignment_lhs(expression_t const* const left)
6900 type_t *const orig_type_left = revert_automatic_type_conversion(left);
6901 type_t *const type_left = skip_typeref(orig_type_left);
6903 if (!is_lvalue(left)) {
6904 errorf(HERE, "left hand side '%E' of assignment is not an lvalue",
6909 if (is_type_array(type_left)) {
6910 errorf(HERE, "cannot assign to arrays ('%E')", left);
6913 if (type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6914 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6918 if (is_type_incomplete(type_left)) {
6919 errorf(HERE, "left-hand side '%E' of assignment has incomplete type '%T'",
6920 left, orig_type_left);
6923 if (is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6924 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6925 left, orig_type_left);
6932 static void semantic_arithmetic_assign(binary_expression_t *expression)
6934 expression_t *left = expression->left;
6935 expression_t *right = expression->right;
6936 type_t *orig_type_left = left->base.type;
6937 type_t *orig_type_right = right->base.type;
6939 if (!is_valid_assignment_lhs(left))
6942 type_t *type_left = skip_typeref(orig_type_left);
6943 type_t *type_right = skip_typeref(orig_type_right);
6945 if (!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6946 /* TODO: improve error message */
6947 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6948 errorf(HERE, "operation needs arithmetic types");
6953 /* combined instructions are tricky. We can't create an implicit cast on
6954 * the left side, because we need the uncasted form for the store.
6955 * The ast2firm pass has to know that left_type must be right_type
6956 * for the arithmetic operation and create a cast by itself */
6957 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6958 expression->right = create_implicit_cast(right, arithmetic_type);
6959 expression->base.type = type_left;
6962 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6964 expression_t *const left = expression->left;
6965 expression_t *const right = expression->right;
6966 type_t *const orig_type_left = left->base.type;
6967 type_t *const orig_type_right = right->base.type;
6968 type_t *const type_left = skip_typeref(orig_type_left);
6969 type_t *const type_right = skip_typeref(orig_type_right);
6971 if (!is_valid_assignment_lhs(left))
6974 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6975 /* combined instructions are tricky. We can't create an implicit cast on
6976 * the left side, because we need the uncasted form for the store.
6977 * The ast2firm pass has to know that left_type must be right_type
6978 * for the arithmetic operation and create a cast by itself */
6979 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6980 expression->right = create_implicit_cast(right, arithmetic_type);
6981 expression->base.type = type_left;
6982 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6983 check_pointer_arithmetic(&expression->base.source_position,
6984 type_left, orig_type_left);
6985 expression->base.type = type_left;
6986 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6987 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6992 * Check the semantic restrictions of a logical expression.
6994 static void semantic_logical_op(binary_expression_t *expression)
6996 expression_t *const left = expression->left;
6997 expression_t *const right = expression->right;
6998 type_t *const orig_type_left = left->base.type;
6999 type_t *const orig_type_right = right->base.type;
7000 type_t *const type_left = skip_typeref(orig_type_left);
7001 type_t *const type_right = skip_typeref(orig_type_right);
7003 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
7004 /* TODO: improve error message */
7005 if (is_type_valid(type_left) && is_type_valid(type_right)) {
7006 errorf(HERE, "operation needs scalar types");
7011 expression->base.type = type_int;
7015 * Check the semantic restrictions of a binary assign expression.
7017 static void semantic_binexpr_assign(binary_expression_t *expression)
7019 expression_t *left = expression->left;
7020 type_t *orig_type_left = left->base.type;
7022 type_t *type_left = revert_automatic_type_conversion(left);
7023 type_left = skip_typeref(orig_type_left);
7025 if (!is_valid_assignment_lhs(left))
7028 assign_error_t error = semantic_assign(orig_type_left, expression->right);
7029 report_assign_error(error, orig_type_left, expression->right,
7030 "assignment", &left->base.source_position);
7031 expression->right = create_implicit_cast(expression->right, orig_type_left);
7032 expression->base.type = orig_type_left;
7036 * Determine if the outermost operation (or parts thereof) of the given
7037 * expression has no effect in order to generate a warning about this fact.
7038 * Therefore in some cases this only examines some of the operands of the
7039 * expression (see comments in the function and examples below).
7041 * f() + 23; // warning, because + has no effect
7042 * x || f(); // no warning, because x controls execution of f()
7043 * x ? y : f(); // warning, because y has no effect
7044 * (void)x; // no warning to be able to suppress the warning
7045 * This function can NOT be used for an "expression has definitely no effect"-
7047 static bool expression_has_effect(const expression_t *const expr)
7049 switch (expr->kind) {
7050 case EXPR_UNKNOWN: break;
7051 case EXPR_INVALID: return true; /* do NOT warn */
7052 case EXPR_REFERENCE: return false;
7053 /* suppress the warning for microsoft __noop operations */
7054 case EXPR_CONST: return expr->conste.is_ms_noop;
7055 case EXPR_CHARACTER_CONSTANT: return false;
7056 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
7057 case EXPR_STRING_LITERAL: return false;
7058 case EXPR_WIDE_STRING_LITERAL: return false;
7061 const call_expression_t *const call = &expr->call;
7062 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
7065 switch (call->function->builtin_symbol.symbol->ID) {
7066 case T___builtin_va_end: return true;
7067 default: return false;
7071 /* Generate the warning if either the left or right hand side of a
7072 * conditional expression has no effect */
7073 case EXPR_CONDITIONAL: {
7074 const conditional_expression_t *const cond = &expr->conditional;
7076 expression_has_effect(cond->true_expression) &&
7077 expression_has_effect(cond->false_expression);
7080 case EXPR_SELECT: return false;
7081 case EXPR_ARRAY_ACCESS: return false;
7082 case EXPR_SIZEOF: return false;
7083 case EXPR_CLASSIFY_TYPE: return false;
7084 case EXPR_ALIGNOF: return false;
7086 case EXPR_FUNCNAME: return false;
7087 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
7088 case EXPR_BUILTIN_CONSTANT_P: return false;
7089 case EXPR_BUILTIN_PREFETCH: return true;
7090 case EXPR_OFFSETOF: return false;
7091 case EXPR_VA_START: return true;
7092 case EXPR_VA_ARG: return true;
7093 case EXPR_STATEMENT: return true; // TODO
7094 case EXPR_COMPOUND_LITERAL: return false;
7096 case EXPR_UNARY_NEGATE: return false;
7097 case EXPR_UNARY_PLUS: return false;
7098 case EXPR_UNARY_BITWISE_NEGATE: return false;
7099 case EXPR_UNARY_NOT: return false;
7100 case EXPR_UNARY_DEREFERENCE: return false;
7101 case EXPR_UNARY_TAKE_ADDRESS: return false;
7102 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
7103 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
7104 case EXPR_UNARY_PREFIX_INCREMENT: return true;
7105 case EXPR_UNARY_PREFIX_DECREMENT: return true;
7107 /* Treat void casts as if they have an effect in order to being able to
7108 * suppress the warning */
7109 case EXPR_UNARY_CAST: {
7110 type_t *const type = skip_typeref(expr->base.type);
7111 return is_type_atomic(type, ATOMIC_TYPE_VOID);
7114 case EXPR_UNARY_CAST_IMPLICIT: return true;
7115 case EXPR_UNARY_ASSUME: return true;
7117 case EXPR_BINARY_ADD: return false;
7118 case EXPR_BINARY_SUB: return false;
7119 case EXPR_BINARY_MUL: return false;
7120 case EXPR_BINARY_DIV: return false;
7121 case EXPR_BINARY_MOD: return false;
7122 case EXPR_BINARY_EQUAL: return false;
7123 case EXPR_BINARY_NOTEQUAL: return false;
7124 case EXPR_BINARY_LESS: return false;
7125 case EXPR_BINARY_LESSEQUAL: return false;
7126 case EXPR_BINARY_GREATER: return false;
7127 case EXPR_BINARY_GREATEREQUAL: return false;
7128 case EXPR_BINARY_BITWISE_AND: return false;
7129 case EXPR_BINARY_BITWISE_OR: return false;
7130 case EXPR_BINARY_BITWISE_XOR: return false;
7131 case EXPR_BINARY_SHIFTLEFT: return false;
7132 case EXPR_BINARY_SHIFTRIGHT: return false;
7133 case EXPR_BINARY_ASSIGN: return true;
7134 case EXPR_BINARY_MUL_ASSIGN: return true;
7135 case EXPR_BINARY_DIV_ASSIGN: return true;
7136 case EXPR_BINARY_MOD_ASSIGN: return true;
7137 case EXPR_BINARY_ADD_ASSIGN: return true;
7138 case EXPR_BINARY_SUB_ASSIGN: return true;
7139 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
7140 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
7141 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
7142 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
7143 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
7145 /* Only examine the right hand side of && and ||, because the left hand
7146 * side already has the effect of controlling the execution of the right
7148 case EXPR_BINARY_LOGICAL_AND:
7149 case EXPR_BINARY_LOGICAL_OR:
7150 /* Only examine the right hand side of a comma expression, because the left
7151 * hand side has a separate warning */
7152 case EXPR_BINARY_COMMA:
7153 return expression_has_effect(expr->binary.right);
7155 case EXPR_BINARY_BUILTIN_EXPECT: return true;
7156 case EXPR_BINARY_ISGREATER: return false;
7157 case EXPR_BINARY_ISGREATEREQUAL: return false;
7158 case EXPR_BINARY_ISLESS: return false;
7159 case EXPR_BINARY_ISLESSEQUAL: return false;
7160 case EXPR_BINARY_ISLESSGREATER: return false;
7161 case EXPR_BINARY_ISUNORDERED: return false;
7164 internal_errorf(HERE, "unexpected expression");
7167 static void semantic_comma(binary_expression_t *expression)
7169 if (warning.unused_value) {
7170 const expression_t *const left = expression->left;
7171 if (!expression_has_effect(left)) {
7172 warningf(&left->base.source_position,
7173 "left-hand operand of comma expression has no effect");
7176 expression->base.type = expression->right->base.type;
7179 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
7180 static expression_t *parse_##binexpression_type(unsigned precedence, \
7181 expression_t *left) \
7184 source_position_t pos = *HERE; \
7186 expression_t *right = parse_sub_expression(precedence + lr); \
7188 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
7189 binexpr->base.source_position = pos; \
7190 binexpr->binary.left = left; \
7191 binexpr->binary.right = right; \
7192 sfunc(&binexpr->binary); \
7197 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
7198 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
7199 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
7200 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
7201 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
7202 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
7203 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
7204 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
7205 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
7207 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
7208 semantic_comparison, 1)
7209 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
7210 semantic_comparison, 1)
7211 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
7212 semantic_comparison, 1)
7213 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
7214 semantic_comparison, 1)
7216 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
7217 semantic_binexpr_arithmetic, 1)
7218 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
7219 semantic_binexpr_arithmetic, 1)
7220 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
7221 semantic_binexpr_arithmetic, 1)
7222 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
7223 semantic_logical_op, 1)
7224 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
7225 semantic_logical_op, 1)
7226 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
7227 semantic_shift_op, 1)
7228 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
7229 semantic_shift_op, 1)
7230 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
7231 semantic_arithmetic_addsubb_assign, 0)
7232 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
7233 semantic_arithmetic_addsubb_assign, 0)
7234 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
7235 semantic_arithmetic_assign, 0)
7236 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
7237 semantic_arithmetic_assign, 0)
7238 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
7239 semantic_arithmetic_assign, 0)
7240 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
7241 semantic_arithmetic_assign, 0)
7242 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7243 semantic_arithmetic_assign, 0)
7244 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
7245 semantic_arithmetic_assign, 0)
7246 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
7247 semantic_arithmetic_assign, 0)
7248 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
7249 semantic_arithmetic_assign, 0)
7251 static expression_t *parse_sub_expression(unsigned precedence)
7253 if (token.type < 0) {
7254 return expected_expression_error();
7257 expression_parser_function_t *parser
7258 = &expression_parsers[token.type];
7259 source_position_t source_position = token.source_position;
7262 if (parser->parser != NULL) {
7263 left = parser->parser(parser->precedence);
7265 left = parse_primary_expression();
7267 assert(left != NULL);
7268 left->base.source_position = source_position;
7271 if (token.type < 0) {
7272 return expected_expression_error();
7275 parser = &expression_parsers[token.type];
7276 if (parser->infix_parser == NULL)
7278 if (parser->infix_precedence < precedence)
7281 left = parser->infix_parser(parser->infix_precedence, left);
7283 assert(left != NULL);
7284 assert(left->kind != EXPR_UNKNOWN);
7285 left->base.source_position = source_position;
7292 * Parse an expression.
7294 static expression_t *parse_expression(void)
7296 return parse_sub_expression(1);
7300 * Register a parser for a prefix-like operator with given precedence.
7302 * @param parser the parser function
7303 * @param token_type the token type of the prefix token
7304 * @param precedence the precedence of the operator
7306 static void register_expression_parser(parse_expression_function parser,
7307 int token_type, unsigned precedence)
7309 expression_parser_function_t *entry = &expression_parsers[token_type];
7311 if (entry->parser != NULL) {
7312 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7313 panic("trying to register multiple expression parsers for a token");
7315 entry->parser = parser;
7316 entry->precedence = precedence;
7320 * Register a parser for an infix operator with given precedence.
7322 * @param parser the parser function
7323 * @param token_type the token type of the infix operator
7324 * @param precedence the precedence of the operator
7326 static void register_infix_parser(parse_expression_infix_function parser,
7327 int token_type, unsigned precedence)
7329 expression_parser_function_t *entry = &expression_parsers[token_type];
7331 if (entry->infix_parser != NULL) {
7332 diagnosticf("for token '%k'\n", (token_type_t)token_type);
7333 panic("trying to register multiple infix expression parsers for a "
7336 entry->infix_parser = parser;
7337 entry->infix_precedence = precedence;
7341 * Initialize the expression parsers.
7343 static void init_expression_parsers(void)
7345 memset(&expression_parsers, 0, sizeof(expression_parsers));
7347 register_infix_parser(parse_array_expression, '[', 30);
7348 register_infix_parser(parse_call_expression, '(', 30);
7349 register_infix_parser(parse_select_expression, '.', 30);
7350 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
7351 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
7353 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
7356 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 17);
7357 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 17);
7358 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 17);
7359 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 16);
7360 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 16);
7361 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 15);
7362 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 15);
7363 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
7364 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
7365 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
7366 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
7367 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
7368 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
7369 T_EXCLAMATIONMARKEQUAL, 13);
7370 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
7371 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
7372 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
7373 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
7374 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
7375 register_infix_parser(parse_conditional_expression, '?', 7);
7376 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
7377 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
7378 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
7379 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
7380 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
7381 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
7382 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
7383 T_LESSLESSEQUAL, 2);
7384 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
7385 T_GREATERGREATEREQUAL, 2);
7386 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
7388 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
7390 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
7393 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
7395 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
7396 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
7397 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
7398 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
7399 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
7400 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
7401 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
7403 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
7405 register_expression_parser(parse_sizeof, T_sizeof, 25);
7406 register_expression_parser(parse_alignof, T___alignof__, 25);
7407 register_expression_parser(parse_extension, T___extension__, 25);
7408 register_expression_parser(parse_builtin_classify_type,
7409 T___builtin_classify_type, 25);
7413 * Parse a asm statement arguments specification.
7415 static asm_argument_t *parse_asm_arguments(bool is_out)
7417 asm_argument_t *result = NULL;
7418 asm_argument_t *last = NULL;
7420 while (token.type == T_STRING_LITERAL || token.type == '[') {
7421 asm_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
7422 memset(argument, 0, sizeof(argument[0]));
7424 if (token.type == '[') {
7426 if (token.type != T_IDENTIFIER) {
7427 parse_error_expected("while parsing asm argument",
7428 T_IDENTIFIER, NULL);
7431 argument->symbol = token.v.symbol;
7436 argument->constraints = parse_string_literals();
7438 expression_t *expression = parse_expression();
7439 argument->expression = expression;
7440 if (is_out && !is_lvalue(expression)) {
7441 errorf(&expression->base.source_position,
7442 "asm output argument is not an lvalue");
7446 set_address_taken(expression, true);
7449 last->next = argument;
7455 if (token.type != ',')
7466 * Parse a asm statement clobber specification.
7468 static asm_clobber_t *parse_asm_clobbers(void)
7470 asm_clobber_t *result = NULL;
7471 asm_clobber_t *last = NULL;
7473 while(token.type == T_STRING_LITERAL) {
7474 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
7475 clobber->clobber = parse_string_literals();
7478 last->next = clobber;
7484 if (token.type != ',')
7493 * Parse an asm statement.
7495 static statement_t *parse_asm_statement(void)
7499 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
7500 statement->base.source_position = token.source_position;
7502 asm_statement_t *asm_statement = &statement->asms;
7504 if (token.type == T_volatile) {
7506 asm_statement->is_volatile = true;
7510 add_anchor_token(')');
7511 add_anchor_token(':');
7512 asm_statement->asm_text = parse_string_literals();
7514 if (token.type != ':') {
7515 rem_anchor_token(':');
7520 asm_statement->outputs = parse_asm_arguments(true);
7521 if (token.type != ':') {
7522 rem_anchor_token(':');
7527 asm_statement->inputs = parse_asm_arguments(false);
7528 if (token.type != ':') {
7529 rem_anchor_token(':');
7532 rem_anchor_token(':');
7535 asm_statement->clobbers = parse_asm_clobbers();
7538 rem_anchor_token(')');
7543 return create_invalid_statement();
7547 * Parse a case statement.
7549 static statement_t *parse_case_statement(void)
7553 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7555 statement->base.source_position = token.source_position;
7556 statement->case_label.expression = parse_expression();
7558 if (c_mode & _GNUC) {
7559 if (token.type == T_DOTDOTDOT) {
7561 statement->case_label.end_range = parse_expression();
7567 if (! is_constant_expression(statement->case_label.expression)) {
7568 errorf(&statement->base.source_position,
7569 "case label does not reduce to an integer constant");
7571 /* TODO: check if the case label is already known */
7572 if (current_switch != NULL) {
7573 /* link all cases into the switch statement */
7574 if (current_switch->last_case == NULL) {
7575 current_switch->first_case =
7576 current_switch->last_case = &statement->case_label;
7578 current_switch->last_case->next = &statement->case_label;
7581 errorf(&statement->base.source_position,
7582 "case label not within a switch statement");
7585 statement->case_label.statement = parse_statement();
7589 return create_invalid_statement();
7593 * Finds an existing default label of a switch statement.
7595 static case_label_statement_t *
7596 find_default_label(const switch_statement_t *statement)
7598 case_label_statement_t *label = statement->first_case;
7599 for ( ; label != NULL; label = label->next) {
7600 if (label->expression == NULL)
7607 * Parse a default statement.
7609 static statement_t *parse_default_statement(void)
7613 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7615 statement->base.source_position = token.source_position;
7618 if (current_switch != NULL) {
7619 const case_label_statement_t *def_label = find_default_label(current_switch);
7620 if (def_label != NULL) {
7621 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7622 &def_label->base.source_position);
7624 /* link all cases into the switch statement */
7625 if (current_switch->last_case == NULL) {
7626 current_switch->first_case =
7627 current_switch->last_case = &statement->case_label;
7629 current_switch->last_case->next = &statement->case_label;
7633 errorf(&statement->base.source_position,
7634 "'default' label not within a switch statement");
7636 statement->case_label.statement = parse_statement();
7640 return create_invalid_statement();
7644 * Return the declaration for a given label symbol or create a new one.
7646 * @param symbol the symbol of the label
7648 static declaration_t *get_label(symbol_t *symbol)
7650 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7651 assert(current_function != NULL);
7652 /* if we found a label in the same function, then we already created the
7654 if (candidate != NULL
7655 && candidate->parent_scope == ¤t_function->scope) {
7659 /* otherwise we need to create a new one */
7660 declaration_t *const declaration = allocate_declaration_zero();
7661 declaration->namespc = NAMESPACE_LABEL;
7662 declaration->symbol = symbol;
7664 label_push(declaration);
7670 * Parse a label statement.
7672 static statement_t *parse_label_statement(void)
7674 assert(token.type == T_IDENTIFIER);
7675 symbol_t *symbol = token.v.symbol;
7678 declaration_t *label = get_label(symbol);
7680 /* if source position is already set then the label is defined twice,
7681 * otherwise it was just mentioned in a goto so far */
7682 if (label->source_position.input_name != NULL) {
7683 errorf(HERE, "duplicate label '%Y' (declared %P)",
7684 symbol, &label->source_position);
7686 label->source_position = token.source_position;
7689 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7691 statement->base.source_position = token.source_position;
7692 statement->label.label = label;
7696 if (token.type == '}') {
7697 /* TODO only warn? */
7699 warningf(HERE, "label at end of compound statement");
7700 statement->label.statement = create_empty_statement();
7702 errorf(HERE, "label at end of compound statement");
7703 statement->label.statement = create_invalid_statement();
7707 if (token.type == ';') {
7708 /* eat an empty statement here, to avoid the warning about an empty
7709 * after a label. label:; is commonly used to have a label before
7711 statement->label.statement = create_empty_statement();
7714 statement->label.statement = parse_statement();
7718 /* remember the labels in a list for later checking */
7719 if (label_last == NULL) {
7720 label_first = &statement->label;
7722 label_last->next = &statement->label;
7724 label_last = &statement->label;
7730 * Parse an if statement.
7732 static statement_t *parse_if(void)
7736 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7737 statement->base.source_position = token.source_position;
7740 add_anchor_token(')');
7741 statement->ifs.condition = parse_expression();
7742 rem_anchor_token(')');
7745 add_anchor_token(T_else);
7746 statement->ifs.true_statement = parse_statement();
7747 rem_anchor_token(T_else);
7749 if (token.type == T_else) {
7751 statement->ifs.false_statement = parse_statement();
7756 return create_invalid_statement();
7760 * Parse a switch statement.
7762 static statement_t *parse_switch(void)
7766 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7767 statement->base.source_position = token.source_position;
7770 expression_t *const expr = parse_expression();
7771 type_t * type = skip_typeref(expr->base.type);
7772 if (is_type_integer(type)) {
7773 type = promote_integer(type);
7774 } else if (is_type_valid(type)) {
7775 errorf(&expr->base.source_position,
7776 "switch quantity is not an integer, but '%T'", type);
7777 type = type_error_type;
7779 statement->switchs.expression = create_implicit_cast(expr, type);
7782 switch_statement_t *rem = current_switch;
7783 current_switch = &statement->switchs;
7784 statement->switchs.body = parse_statement();
7785 current_switch = rem;
7787 if (warning.switch_default &&
7788 find_default_label(&statement->switchs) == NULL) {
7789 warningf(&statement->base.source_position, "switch has no default case");
7794 return create_invalid_statement();
7797 static statement_t *parse_loop_body(statement_t *const loop)
7799 statement_t *const rem = current_loop;
7800 current_loop = loop;
7802 statement_t *const body = parse_statement();
7809 * Parse a while statement.
7811 static statement_t *parse_while(void)
7815 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7816 statement->base.source_position = token.source_position;
7819 add_anchor_token(')');
7820 statement->whiles.condition = parse_expression();
7821 rem_anchor_token(')');
7824 statement->whiles.body = parse_loop_body(statement);
7828 return create_invalid_statement();
7832 * Parse a do statement.
7834 static statement_t *parse_do(void)
7838 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7840 statement->base.source_position = token.source_position;
7842 add_anchor_token(T_while);
7843 statement->do_while.body = parse_loop_body(statement);
7844 rem_anchor_token(T_while);
7848 add_anchor_token(')');
7849 statement->do_while.condition = parse_expression();
7850 rem_anchor_token(')');
7856 return create_invalid_statement();
7860 * Parse a for statement.
7862 static statement_t *parse_for(void)
7866 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7867 statement->base.source_position = token.source_position;
7869 int top = environment_top();
7870 scope_t *last_scope = scope;
7871 set_scope(&statement->fors.scope);
7874 add_anchor_token(')');
7876 if (token.type != ';') {
7877 if (is_declaration_specifier(&token, false)) {
7878 parse_declaration(record_declaration);
7880 add_anchor_token(';');
7881 expression_t *const init = parse_expression();
7882 statement->fors.initialisation = init;
7883 if (warning.unused_value && !expression_has_effect(init)) {
7884 warningf(&init->base.source_position,
7885 "initialisation of 'for'-statement has no effect");
7887 rem_anchor_token(';');
7894 if (token.type != ';') {
7895 add_anchor_token(';');
7896 statement->fors.condition = parse_expression();
7897 rem_anchor_token(';');
7900 if (token.type != ')') {
7901 expression_t *const step = parse_expression();
7902 statement->fors.step = step;
7903 if (warning.unused_value && !expression_has_effect(step)) {
7904 warningf(&step->base.source_position,
7905 "step of 'for'-statement has no effect");
7908 rem_anchor_token(')');
7910 statement->fors.body = parse_loop_body(statement);
7912 assert(scope == &statement->fors.scope);
7913 set_scope(last_scope);
7914 environment_pop_to(top);
7919 rem_anchor_token(')');
7920 assert(scope == &statement->fors.scope);
7921 set_scope(last_scope);
7922 environment_pop_to(top);
7924 return create_invalid_statement();
7928 * Parse a goto statement.
7930 static statement_t *parse_goto(void)
7934 if (token.type != T_IDENTIFIER) {
7935 parse_error_expected("while parsing goto", T_IDENTIFIER, NULL);
7939 symbol_t *symbol = token.v.symbol;
7942 declaration_t *label = get_label(symbol);
7944 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7945 statement->base.source_position = token.source_position;
7947 statement->gotos.label = label;
7949 /* remember the goto's in a list for later checking */
7950 if (goto_last == NULL) {
7951 goto_first = &statement->gotos;
7953 goto_last->next = &statement->gotos;
7955 goto_last = &statement->gotos;
7961 return create_invalid_statement();
7965 * Parse a continue statement.
7967 static statement_t *parse_continue(void)
7969 statement_t *statement;
7970 if (current_loop == NULL) {
7971 errorf(HERE, "continue statement not within loop");
7972 statement = create_invalid_statement();
7974 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7976 statement->base.source_position = token.source_position;
7984 return create_invalid_statement();
7988 * Parse a break statement.
7990 static statement_t *parse_break(void)
7992 statement_t *statement;
7993 if (current_switch == NULL && current_loop == NULL) {
7994 errorf(HERE, "break statement not within loop or switch");
7995 statement = create_invalid_statement();
7997 statement = allocate_statement_zero(STATEMENT_BREAK);
7999 statement->base.source_position = token.source_position;
8007 return create_invalid_statement();
8011 * Parse a __leave statement.
8013 static statement_t *parse_leave(void)
8015 statement_t *statement;
8016 if (current_try == NULL) {
8017 errorf(HERE, "__leave statement not within __try");
8018 statement = create_invalid_statement();
8020 statement = allocate_statement_zero(STATEMENT_LEAVE);
8022 statement->base.source_position = token.source_position;
8030 return create_invalid_statement();
8034 * Check if a given declaration represents a local variable.
8036 static bool is_local_var_declaration(const declaration_t *declaration) {
8037 switch ((storage_class_tag_t) declaration->storage_class) {
8038 case STORAGE_CLASS_AUTO:
8039 case STORAGE_CLASS_REGISTER: {
8040 const type_t *type = skip_typeref(declaration->type);
8041 if (is_type_function(type)) {
8053 * Check if a given declaration represents a variable.
8055 static bool is_var_declaration(const declaration_t *declaration) {
8056 if (declaration->storage_class == STORAGE_CLASS_TYPEDEF)
8059 const type_t *type = skip_typeref(declaration->type);
8060 return !is_type_function(type);
8064 * Check if a given expression represents a local variable.
8066 static bool is_local_variable(const expression_t *expression)
8068 if (expression->base.kind != EXPR_REFERENCE) {
8071 const declaration_t *declaration = expression->reference.declaration;
8072 return is_local_var_declaration(declaration);
8076 * Check if a given expression represents a local variable and
8077 * return its declaration then, else return NULL.
8079 declaration_t *expr_is_variable(const expression_t *expression)
8081 if (expression->base.kind != EXPR_REFERENCE) {
8084 declaration_t *declaration = expression->reference.declaration;
8085 if (is_var_declaration(declaration))
8091 * Parse a return statement.
8093 static statement_t *parse_return(void)
8095 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
8096 statement->base.source_position = token.source_position;
8100 expression_t *return_value = NULL;
8101 if (token.type != ';') {
8102 return_value = parse_expression();
8106 const type_t *const func_type = current_function->type;
8107 assert(is_type_function(func_type));
8108 type_t *const return_type = skip_typeref(func_type->function.return_type);
8110 if (return_value != NULL) {
8111 type_t *return_value_type = skip_typeref(return_value->base.type);
8113 if (is_type_atomic(return_type, ATOMIC_TYPE_VOID)
8114 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
8115 warningf(&statement->base.source_position,
8116 "'return' with a value, in function returning void");
8117 return_value = NULL;
8119 assign_error_t error = semantic_assign(return_type, return_value);
8120 report_assign_error(error, return_type, return_value, "'return'",
8121 &statement->base.source_position);
8122 return_value = create_implicit_cast(return_value, return_type);
8124 /* check for returning address of a local var */
8125 if (return_value != NULL &&
8126 return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
8127 const expression_t *expression = return_value->unary.value;
8128 if (is_local_variable(expression)) {
8129 warningf(&statement->base.source_position,
8130 "function returns address of local variable");
8134 if (!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
8135 warningf(&statement->base.source_position,
8136 "'return' without value, in function returning non-void");
8139 statement->returns.value = return_value;
8143 return create_invalid_statement();
8147 * Parse a declaration statement.
8149 static statement_t *parse_declaration_statement(void)
8151 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
8153 statement->base.source_position = token.source_position;
8155 declaration_t *before = last_declaration;
8156 parse_declaration(record_declaration);
8158 if (before == NULL) {
8159 statement->declaration.declarations_begin = scope->declarations;
8161 statement->declaration.declarations_begin = before->next;
8163 statement->declaration.declarations_end = last_declaration;
8169 * Parse an expression statement, ie. expr ';'.
8171 static statement_t *parse_expression_statement(void)
8173 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
8175 statement->base.source_position = token.source_position;
8176 expression_t *const expr = parse_expression();
8177 statement->expression.expression = expr;
8183 return create_invalid_statement();
8187 * Parse a microsoft __try { } __finally { } or
8188 * __try{ } __except() { }
8190 static statement_t *parse_ms_try_statment(void) {
8191 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
8193 statement->base.source_position = token.source_position;
8196 ms_try_statement_t *rem = current_try;
8197 current_try = &statement->ms_try;
8198 statement->ms_try.try_statement = parse_compound_statement(false);
8201 if (token.type == T___except) {
8204 add_anchor_token(')');
8205 expression_t *const expr = parse_expression();
8206 type_t * type = skip_typeref(expr->base.type);
8207 if (is_type_integer(type)) {
8208 type = promote_integer(type);
8209 } else if (is_type_valid(type)) {
8210 errorf(&expr->base.source_position,
8211 "__expect expression is not an integer, but '%T'", type);
8212 type = type_error_type;
8214 statement->ms_try.except_expression = create_implicit_cast(expr, type);
8215 rem_anchor_token(')');
8217 statement->ms_try.final_statement = parse_compound_statement(false);
8218 } else if (token.type == T__finally) {
8220 statement->ms_try.final_statement = parse_compound_statement(false);
8222 parse_error_expected("while parsing __try statement", T___except, T___finally, NULL);
8223 return create_invalid_statement();
8227 return create_invalid_statement();
8231 * Parse a statement.
8232 * There's also parse_statement() which additionally checks for
8233 * "statement has no effect" warnings
8235 static statement_t *intern_parse_statement(void)
8237 statement_t *statement = NULL;
8239 /* declaration or statement */
8240 add_anchor_token(';');
8241 switch(token.type) {
8243 statement = parse_asm_statement();
8247 statement = parse_case_statement();
8251 statement = parse_default_statement();
8255 statement = parse_compound_statement(false);
8259 statement = parse_if ();
8263 statement = parse_switch();
8267 statement = parse_while();
8271 statement = parse_do();
8275 statement = parse_for();
8279 statement = parse_goto();
8283 statement = parse_continue();
8287 statement = parse_break();
8291 statement = parse_leave();
8295 statement = parse_return();
8299 if (warning.empty_statement) {
8300 warningf(HERE, "statement is empty");
8302 statement = create_empty_statement();
8307 if (look_ahead(1)->type == ':') {
8308 statement = parse_label_statement();
8312 if (is_typedef_symbol(token.v.symbol)) {
8313 statement = parse_declaration_statement();
8317 statement = parse_expression_statement();
8320 case T___extension__:
8321 /* this can be a prefix to a declaration or an expression statement */
8322 /* we simply eat it now and parse the rest with tail recursion */
8325 } while(token.type == T___extension__);
8326 statement = parse_statement();
8330 statement = parse_declaration_statement();
8334 statement = parse_ms_try_statment();
8338 statement = parse_expression_statement();
8341 rem_anchor_token(';');
8343 assert(statement != NULL
8344 && statement->base.source_position.input_name != NULL);
8350 * parse a statement and emits "statement has no effect" warning if needed
8351 * (This is really a wrapper around intern_parse_statement with check for 1
8352 * single warning. It is needed, because for statement expressions we have
8353 * to avoid the warning on the last statement)
8355 static statement_t *parse_statement(void)
8357 statement_t *statement = intern_parse_statement();
8359 if (statement->kind == STATEMENT_EXPRESSION && warning.unused_value) {
8360 expression_t *expression = statement->expression.expression;
8361 if (!expression_has_effect(expression)) {
8362 warningf(&expression->base.source_position,
8363 "statement has no effect");
8371 * Parse a compound statement.
8373 static statement_t *parse_compound_statement(bool inside_expression_statement)
8375 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
8377 statement->base.source_position = token.source_position;
8380 add_anchor_token('}');
8382 int top = environment_top();
8383 scope_t *last_scope = scope;
8384 set_scope(&statement->compound.scope);
8386 statement_t *last_statement = NULL;
8388 while(token.type != '}' && token.type != T_EOF) {
8389 statement_t *sub_statement = intern_parse_statement();
8390 if (is_invalid_statement(sub_statement)) {
8391 /* an error occurred. if we are at an anchor, return */
8397 if (last_statement != NULL) {
8398 last_statement->base.next = sub_statement;
8400 statement->compound.statements = sub_statement;
8403 while(sub_statement->base.next != NULL)
8404 sub_statement = sub_statement->base.next;
8406 last_statement = sub_statement;
8409 if (token.type == '}') {
8412 errorf(&statement->base.source_position,
8413 "end of file while looking for closing '}'");
8416 /* look over all statements again to produce no effect warnings */
8417 if (warning.unused_value) {
8418 statement_t *sub_statement = statement->compound.statements;
8419 for( ; sub_statement != NULL; sub_statement = sub_statement->base.next) {
8420 if (sub_statement->kind != STATEMENT_EXPRESSION)
8422 /* don't emit a warning for the last expression in an expression
8423 * statement as it has always an effect */
8424 if (inside_expression_statement && sub_statement->base.next == NULL)
8427 expression_t *expression = sub_statement->expression.expression;
8428 if (!expression_has_effect(expression)) {
8429 warningf(&expression->base.source_position,
8430 "statement has no effect");
8436 rem_anchor_token('}');
8437 assert(scope == &statement->compound.scope);
8438 set_scope(last_scope);
8439 environment_pop_to(top);
8445 * Initialize builtin types.
8447 static void initialize_builtin_types(void)
8449 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
8450 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
8451 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
8452 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
8453 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
8454 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
8455 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
8456 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
8458 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
8459 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
8460 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
8461 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
8465 * Check for unused global static functions and variables
8467 static void check_unused_globals(void)
8469 if (!warning.unused_function && !warning.unused_variable)
8472 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
8474 decl->modifiers & DM_USED ||
8475 decl->storage_class != STORAGE_CLASS_STATIC)
8478 type_t *const type = decl->type;
8480 if (is_type_function(skip_typeref(type))) {
8481 if (!warning.unused_function || decl->is_inline)
8484 s = (decl->init.statement != NULL ? "defined" : "declared");
8486 if (!warning.unused_variable)
8492 warningf(&decl->source_position, "'%#T' %s but not used",
8493 type, decl->symbol, s);
8497 static void parse_global_asm(void)
8502 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
8503 statement->base.source_position = token.source_position;
8504 statement->asms.asm_text = parse_string_literals();
8505 statement->base.next = unit->global_asm;
8506 unit->global_asm = statement;
8515 * Parse a translation unit.
8517 static void parse_translation_unit(void)
8519 while(token.type != T_EOF) {
8520 switch (token.type) {
8522 /* TODO error in strict mode */
8523 warningf(HERE, "stray ';' outside of function");
8532 parse_external_declaration();
8541 * @return the translation unit or NULL if errors occurred.
8543 void start_parsing(void)
8545 environment_stack = NEW_ARR_F(stack_entry_t, 0);
8546 label_stack = NEW_ARR_F(stack_entry_t, 0);
8547 diagnostic_count = 0;
8551 type_set_output(stderr);
8552 ast_set_output(stderr);
8554 assert(unit == NULL);
8555 unit = allocate_ast_zero(sizeof(unit[0]));
8557 assert(global_scope == NULL);
8558 global_scope = &unit->scope;
8560 assert(scope == NULL);
8561 set_scope(&unit->scope);
8563 initialize_builtin_types();
8566 translation_unit_t *finish_parsing(void)
8568 assert(scope == &unit->scope);
8570 last_declaration = NULL;
8572 assert(global_scope == &unit->scope);
8573 check_unused_globals();
8574 global_scope = NULL;
8576 DEL_ARR_F(environment_stack);
8577 DEL_ARR_F(label_stack);
8579 translation_unit_t *result = unit;
8586 lookahead_bufpos = 0;
8587 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
8590 parse_translation_unit();
8594 * Initialize the parser.
8596 void init_parser(void)
8599 /* add predefined symbols for extended-decl-modifier */
8600 sym_align = symbol_table_insert("align");
8601 sym_allocate = symbol_table_insert("allocate");
8602 sym_dllimport = symbol_table_insert("dllimport");
8603 sym_dllexport = symbol_table_insert("dllexport");
8604 sym_naked = symbol_table_insert("naked");
8605 sym_noinline = symbol_table_insert("noinline");
8606 sym_noreturn = symbol_table_insert("noreturn");
8607 sym_nothrow = symbol_table_insert("nothrow");
8608 sym_novtable = symbol_table_insert("novtable");
8609 sym_property = symbol_table_insert("property");
8610 sym_get = symbol_table_insert("get");
8611 sym_put = symbol_table_insert("put");
8612 sym_selectany = symbol_table_insert("selectany");
8613 sym_thread = symbol_table_insert("thread");
8614 sym_uuid = symbol_table_insert("uuid");
8615 sym_deprecated = symbol_table_insert("deprecated");
8616 sym_restrict = symbol_table_insert("restrict");
8617 sym_noalias = symbol_table_insert("noalias");
8619 memset(token_anchor_set, 0, sizeof(token_anchor_set));
8621 init_expression_parsers();
8622 obstack_init(&temp_obst);
8624 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
8625 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
8629 * Terminate the parser.
8631 void exit_parser(void)
8633 obstack_free(&temp_obst, NULL);