2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct gnu_attribute_t gnu_attribute_t;
52 struct gnu_attribute_t {
53 gnu_attribute_kind_t kind;
54 gnu_attribute_t *next;
63 typedef struct declaration_specifiers_t declaration_specifiers_t;
64 struct declaration_specifiers_t {
65 source_position_t source_position;
66 unsigned char declared_storage_class;
67 unsigned char alignment; /**< Alignment, 0 if not set. */
68 unsigned int is_inline : 1;
69 unsigned int deprecated : 1;
70 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
71 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
72 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
73 symbol_t *get_property_sym; /**< the name of the get property if set. */
74 symbol_t *put_property_sym; /**< the name of the put property if set. */
79 * An environment for parsing initializers (and compound literals).
81 typedef struct parse_initializer_env_t {
82 type_t *type; /**< the type of the initializer. In case of an
83 array type with unspecified size this gets
84 adjusted to the actual size. */
85 declaration_t *declaration; /**< the declaration that is initialized if any */
86 bool must_be_constant;
87 } parse_initializer_env_t;
89 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
92 static token_t lookahead_buffer[MAX_LOOKAHEAD];
93 static int lookahead_bufpos;
94 static stack_entry_t *environment_stack = NULL;
95 static stack_entry_t *label_stack = NULL;
96 static scope_t *global_scope = NULL;
97 static scope_t *scope = NULL;
98 static declaration_t *last_declaration = NULL;
99 static declaration_t *current_function = NULL;
100 static switch_statement_t *current_switch = NULL;
101 static statement_t *current_loop = NULL;
102 static ms_try_statement_t *current_try = NULL;
103 static goto_statement_t *goto_first = NULL;
104 static goto_statement_t *goto_last = NULL;
105 static label_statement_t *label_first = NULL;
106 static label_statement_t *label_last = NULL;
107 static struct obstack temp_obst;
109 static source_position_t null_position = { NULL, 0 };
111 /* symbols for Microsoft extended-decl-modifier */
112 static const symbol_t *sym_align = NULL;
113 static const symbol_t *sym_allocate = NULL;
114 static const symbol_t *sym_dllimport = NULL;
115 static const symbol_t *sym_dllexport = NULL;
116 static const symbol_t *sym_naked = NULL;
117 static const symbol_t *sym_noinline = NULL;
118 static const symbol_t *sym_noreturn = NULL;
119 static const symbol_t *sym_nothrow = NULL;
120 static const symbol_t *sym_novtable = NULL;
121 static const symbol_t *sym_property = NULL;
122 static const symbol_t *sym_get = NULL;
123 static const symbol_t *sym_put = NULL;
124 static const symbol_t *sym_selectany = NULL;
125 static const symbol_t *sym_thread = NULL;
126 static const symbol_t *sym_uuid = NULL;
127 static const symbol_t *sym_deprecated = NULL;
128 static const symbol_t *sym_restrict = NULL;
129 static const symbol_t *sym_noalias = NULL;
131 /** The token anchor set */
132 static unsigned char token_anchor_set[T_LAST_TOKEN];
134 /** The current source position. */
135 #define HERE &token.source_position
137 static type_t *type_valist;
139 static statement_t *parse_compound_statement(void);
140 static statement_t *parse_statement(void);
142 static expression_t *parse_sub_expression(unsigned precedence);
143 static expression_t *parse_expression(void);
144 static type_t *parse_typename(void);
146 static void parse_compound_type_entries(declaration_t *compound_declaration);
147 static declaration_t *parse_declarator(
148 const declaration_specifiers_t *specifiers, bool may_be_abstract);
149 static declaration_t *record_declaration(declaration_t *declaration);
151 static void semantic_comparison(binary_expression_t *expression);
153 #define STORAGE_CLASSES \
160 #define TYPE_QUALIFIERS \
167 #ifdef PROVIDE_COMPLEX
168 #define COMPLEX_SPECIFIERS \
170 #define IMAGINARY_SPECIFIERS \
173 #define COMPLEX_SPECIFIERS
174 #define IMAGINARY_SPECIFIERS
177 #define TYPE_SPECIFIERS \
192 case T___builtin_va_list: \
197 #define DECLARATION_START \
202 #define TYPENAME_START \
207 * Allocate an AST node with given size and
208 * initialize all fields with zero.
210 static void *allocate_ast_zero(size_t size)
212 void *res = allocate_ast(size);
213 memset(res, 0, size);
217 static declaration_t *allocate_declaration_zero(void)
219 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
220 declaration->type = type_error_type;
221 declaration->alignment = 0;
226 * Returns the size of a statement node.
228 * @param kind the statement kind
230 static size_t get_statement_struct_size(statement_kind_t kind)
232 static const size_t sizes[] = {
233 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
234 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
235 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
236 [STATEMENT_RETURN] = sizeof(return_statement_t),
237 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
238 [STATEMENT_IF] = sizeof(if_statement_t),
239 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
240 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
241 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
242 [STATEMENT_BREAK] = sizeof(statement_base_t),
243 [STATEMENT_GOTO] = sizeof(goto_statement_t),
244 [STATEMENT_LABEL] = sizeof(label_statement_t),
245 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
246 [STATEMENT_WHILE] = sizeof(while_statement_t),
247 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
248 [STATEMENT_FOR] = sizeof(for_statement_t),
249 [STATEMENT_ASM] = sizeof(asm_statement_t),
250 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t),
251 [STATEMENT_LEAVE] = sizeof(leave_statement_t)
253 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
254 assert(sizes[kind] != 0);
259 * Returns the size of an expression node.
261 * @param kind the expression kind
263 static size_t get_expression_struct_size(expression_kind_t kind)
265 static const size_t sizes[] = {
266 [EXPR_INVALID] = sizeof(expression_base_t),
267 [EXPR_REFERENCE] = sizeof(reference_expression_t),
268 [EXPR_CONST] = sizeof(const_expression_t),
269 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
270 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
271 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
272 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
273 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
274 [EXPR_CALL] = sizeof(call_expression_t),
275 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
276 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
277 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
278 [EXPR_SELECT] = sizeof(select_expression_t),
279 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
280 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
281 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
282 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
283 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
284 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
285 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
286 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
287 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
288 [EXPR_VA_START] = sizeof(va_start_expression_t),
289 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
290 [EXPR_STATEMENT] = sizeof(statement_expression_t),
292 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
293 return sizes[EXPR_UNARY_FIRST];
295 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
296 return sizes[EXPR_BINARY_FIRST];
298 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
299 assert(sizes[kind] != 0);
304 * Allocate a statement node of given kind and initialize all
307 static statement_t *allocate_statement_zero(statement_kind_t kind)
309 size_t size = get_statement_struct_size(kind);
310 statement_t *res = allocate_ast_zero(size);
312 res->base.kind = kind;
317 * Allocate an expression node of given kind and initialize all
320 static expression_t *allocate_expression_zero(expression_kind_t kind)
322 size_t size = get_expression_struct_size(kind);
323 expression_t *res = allocate_ast_zero(size);
325 res->base.kind = kind;
326 res->base.type = type_error_type;
331 * Creates a new invalid expression.
333 static expression_t *create_invalid_expression(void)
335 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
336 expression->base.source_position = token.source_position;
341 * Creates a new invalid statement.
343 static statement_t *create_invalid_statement(void)
345 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
346 statement->base.source_position = token.source_position;
351 * Allocate a new empty statement.
353 static statement_t *create_empty_statement(void)
355 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
356 statement->base.source_position = token.source_position;
361 * Returns the size of a type node.
363 * @param kind the type kind
365 static size_t get_type_struct_size(type_kind_t kind)
367 static const size_t sizes[] = {
368 [TYPE_ATOMIC] = sizeof(atomic_type_t),
369 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
370 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
371 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
372 [TYPE_ENUM] = sizeof(enum_type_t),
373 [TYPE_FUNCTION] = sizeof(function_type_t),
374 [TYPE_POINTER] = sizeof(pointer_type_t),
375 [TYPE_ARRAY] = sizeof(array_type_t),
376 [TYPE_BUILTIN] = sizeof(builtin_type_t),
377 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
378 [TYPE_TYPEOF] = sizeof(typeof_type_t),
380 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
381 assert(kind <= TYPE_TYPEOF);
382 assert(sizes[kind] != 0);
387 * Allocate a type node of given kind and initialize all
390 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
392 size_t size = get_type_struct_size(kind);
393 type_t *res = obstack_alloc(type_obst, size);
394 memset(res, 0, size);
396 res->base.kind = kind;
397 res->base.source_position = *source_position;
402 * Returns the size of an initializer node.
404 * @param kind the initializer kind
406 static size_t get_initializer_size(initializer_kind_t kind)
408 static const size_t sizes[] = {
409 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
410 [INITIALIZER_STRING] = sizeof(initializer_string_t),
411 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
412 [INITIALIZER_LIST] = sizeof(initializer_list_t),
413 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
415 assert(kind < sizeof(sizes) / sizeof(*sizes));
416 assert(sizes[kind] != 0);
421 * Allocate an initializer node of given kind and initialize all
424 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
426 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
433 * Free a type from the type obstack.
435 static void free_type(void *type)
437 obstack_free(type_obst, type);
441 * Returns the index of the top element of the environment stack.
443 static size_t environment_top(void)
445 return ARR_LEN(environment_stack);
449 * Returns the index of the top element of the label stack.
451 static size_t label_top(void)
453 return ARR_LEN(label_stack);
457 * Return the next token.
459 static inline void next_token(void)
461 token = lookahead_buffer[lookahead_bufpos];
462 lookahead_buffer[lookahead_bufpos] = lexer_token;
465 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
468 print_token(stderr, &token);
469 fprintf(stderr, "\n");
474 * Return the next token with a given lookahead.
476 static inline const token_t *look_ahead(int num)
478 assert(num > 0 && num <= MAX_LOOKAHEAD);
479 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
480 return &lookahead_buffer[pos];
484 * Adds a token to the token anchor set (a multi-set).
486 static void add_anchor_token(int token_type) {
487 assert(0 <= token_type && token_type < T_LAST_TOKEN);
488 ++token_anchor_set[token_type];
492 * Remove a token from the token anchor set (a multi-set).
494 static void rem_anchor_token(int token_type) {
495 assert(0 <= token_type && token_type < T_LAST_TOKEN);
496 --token_anchor_set[token_type];
499 static bool at_anchor(void) {
502 return token_anchor_set[token.type];
506 * Eat tokens until a matching token is found.
508 static void eat_until_matching_token(int type) {
509 unsigned parenthesis_count = 0;
510 unsigned brace_count = 0;
511 unsigned bracket_count = 0;
512 int end_token = type;
521 while(token.type != end_token ||
522 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
526 case '(': ++parenthesis_count; break;
527 case '{': ++brace_count; break;
528 case '[': ++bracket_count; break;
530 if(parenthesis_count > 0)
538 if(bracket_count > 0)
549 * Eat input tokens until an anchor is found.
551 static void eat_until_anchor(void) {
552 if(token.type == T_EOF)
554 while(token_anchor_set[token.type] == 0) {
555 if(token.type == '(' || token.type == '{' || token.type == '[')
556 eat_until_matching_token(token.type);
557 if(token.type == T_EOF)
563 static void eat_block(void) {
564 eat_until_matching_token('{');
565 if(token.type == '}')
570 * eat all token until a ';' is reached
571 * or a stop token is found.
573 static void eat_statement(void) {
574 eat_until_matching_token(';');
575 if(token.type == ';')
579 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
582 * Report a parse error because an expected token was not found.
584 static void parse_error_expected(const char *message, ...)
586 if(message != NULL) {
587 errorf(HERE, "%s", message);
590 va_start(ap, message);
591 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
596 * Report a type error.
598 static void type_error(const char *msg, const source_position_t *source_position,
601 errorf(source_position, "%s, but found type '%T'", msg, type);
605 * Report an incompatible type.
607 static void type_error_incompatible(const char *msg,
608 const source_position_t *source_position, type_t *type1, type_t *type2)
610 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
614 * Expect the the current token is the expected token.
615 * If not, generate an error, eat the current statement,
616 * and goto the end_error label.
618 #define expect(expected) \
620 if(UNLIKELY(token.type != (expected))) { \
621 parse_error_expected(NULL, (expected), 0); \
622 add_anchor_token(expected); \
623 eat_until_anchor(); \
624 rem_anchor_token(expected); \
630 static void set_scope(scope_t *new_scope)
633 scope->last_declaration = last_declaration;
637 last_declaration = new_scope->last_declaration;
641 * Search a symbol in a given namespace and returns its declaration or
642 * NULL if this symbol was not found.
644 static declaration_t *get_declaration(const symbol_t *const symbol,
645 const namespace_t namespc)
647 declaration_t *declaration = symbol->declaration;
648 for( ; declaration != NULL; declaration = declaration->symbol_next) {
649 if(declaration->namespc == namespc)
657 * pushs an environment_entry on the environment stack and links the
658 * corresponding symbol to the new entry
660 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
662 symbol_t *symbol = declaration->symbol;
663 namespace_t namespc = (namespace_t) declaration->namespc;
665 /* replace/add declaration into declaration list of the symbol */
666 declaration_t *iter = symbol->declaration;
668 symbol->declaration = declaration;
670 declaration_t *iter_last = NULL;
671 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
672 /* replace an entry? */
673 if(iter->namespc == namespc) {
674 if(iter_last == NULL) {
675 symbol->declaration = declaration;
677 iter_last->symbol_next = declaration;
679 declaration->symbol_next = iter->symbol_next;
684 assert(iter_last->symbol_next == NULL);
685 iter_last->symbol_next = declaration;
689 /* remember old declaration */
691 entry.symbol = symbol;
692 entry.old_declaration = iter;
693 entry.namespc = (unsigned short) namespc;
694 ARR_APP1(stack_entry_t, *stack_ptr, entry);
697 static void environment_push(declaration_t *declaration)
699 assert(declaration->source_position.input_name != NULL);
700 assert(declaration->parent_scope != NULL);
701 stack_push(&environment_stack, declaration);
704 static void label_push(declaration_t *declaration)
706 declaration->parent_scope = ¤t_function->scope;
707 stack_push(&label_stack, declaration);
711 * pops symbols from the environment stack until @p new_top is the top element
713 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
715 stack_entry_t *stack = *stack_ptr;
716 size_t top = ARR_LEN(stack);
719 assert(new_top <= top);
723 for(i = top; i > new_top; --i) {
724 stack_entry_t *entry = &stack[i - 1];
726 declaration_t *old_declaration = entry->old_declaration;
727 symbol_t *symbol = entry->symbol;
728 namespace_t namespc = (namespace_t)entry->namespc;
730 /* replace/remove declaration */
731 declaration_t *declaration = symbol->declaration;
732 assert(declaration != NULL);
733 if(declaration->namespc == namespc) {
734 if(old_declaration == NULL) {
735 symbol->declaration = declaration->symbol_next;
737 symbol->declaration = old_declaration;
740 declaration_t *iter_last = declaration;
741 declaration_t *iter = declaration->symbol_next;
742 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
743 /* replace an entry? */
744 if(iter->namespc == namespc) {
745 assert(iter_last != NULL);
746 iter_last->symbol_next = old_declaration;
747 if(old_declaration != NULL) {
748 old_declaration->symbol_next = iter->symbol_next;
753 assert(iter != NULL);
757 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
760 static void environment_pop_to(size_t new_top)
762 stack_pop_to(&environment_stack, new_top);
765 static void label_pop_to(size_t new_top)
767 stack_pop_to(&label_stack, new_top);
771 static int get_rank(const type_t *type)
773 assert(!is_typeref(type));
774 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
775 * and esp. footnote 108). However we can't fold constants (yet), so we
776 * can't decide whether unsigned int is possible, while int always works.
777 * (unsigned int would be preferable when possible... for stuff like
778 * struct { enum { ... } bla : 4; } ) */
779 if(type->kind == TYPE_ENUM)
780 return ATOMIC_TYPE_INT;
782 assert(type->kind == TYPE_ATOMIC);
783 return type->atomic.akind;
786 static type_t *promote_integer(type_t *type)
788 if(type->kind == TYPE_BITFIELD)
789 type = type->bitfield.base;
791 if(get_rank(type) < ATOMIC_TYPE_INT)
798 * Create a cast expression.
800 * @param expression the expression to cast
801 * @param dest_type the destination type
803 static expression_t *create_cast_expression(expression_t *expression,
806 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
808 cast->unary.value = expression;
809 cast->base.type = dest_type;
815 * Check if a given expression represents the 0 pointer constant.
817 static bool is_null_pointer_constant(const expression_t *expression)
819 /* skip void* cast */
820 if(expression->kind == EXPR_UNARY_CAST
821 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
822 expression = expression->unary.value;
825 /* TODO: not correct yet, should be any constant integer expression
826 * which evaluates to 0 */
827 if (expression->kind != EXPR_CONST)
830 type_t *const type = skip_typeref(expression->base.type);
831 if (!is_type_integer(type))
834 return expression->conste.v.int_value == 0;
838 * Create an implicit cast expression.
840 * @param expression the expression to cast
841 * @param dest_type the destination type
843 static expression_t *create_implicit_cast(expression_t *expression,
846 type_t *const source_type = expression->base.type;
848 if (source_type == dest_type)
851 return create_cast_expression(expression, dest_type);
854 /** Implements the rules from § 6.5.16.1 */
855 static type_t *semantic_assign(type_t *orig_type_left,
856 const expression_t *const right,
858 const source_position_t *source_position)
860 type_t *const orig_type_right = right->base.type;
861 type_t *const type_left = skip_typeref(orig_type_left);
862 type_t *const type_right = skip_typeref(orig_type_right);
864 if(is_type_pointer(type_left)) {
865 if(is_null_pointer_constant(right)) {
866 return orig_type_left;
867 } else if(is_type_pointer(type_right)) {
868 type_t *points_to_left
869 = skip_typeref(type_left->pointer.points_to);
870 type_t *points_to_right
871 = skip_typeref(type_right->pointer.points_to);
873 /* the left type has all qualifiers from the right type */
874 unsigned missing_qualifiers
875 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
876 if(missing_qualifiers != 0) {
877 errorf(source_position,
878 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
879 return orig_type_left;
882 points_to_left = get_unqualified_type(points_to_left);
883 points_to_right = get_unqualified_type(points_to_right);
885 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
886 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
887 return orig_type_left;
890 if (!types_compatible(points_to_left, points_to_right)) {
891 warningf(source_position,
892 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
893 orig_type_left, context, right, orig_type_right);
896 return orig_type_left;
897 } else if(is_type_integer(type_right)) {
898 warningf(source_position,
899 "%s makes pointer '%T' from integer '%T' without a cast",
900 context, orig_type_left, orig_type_right);
901 return orig_type_left;
903 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
904 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
905 && is_type_pointer(type_right))) {
906 return orig_type_left;
907 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
908 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
909 type_t *const unqual_type_left = get_unqualified_type(type_left);
910 type_t *const unqual_type_right = get_unqualified_type(type_right);
911 if (types_compatible(unqual_type_left, unqual_type_right)) {
912 return orig_type_left;
914 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
915 warningf(source_position,
916 "%s makes integer '%T' from pointer '%T' without a cast",
917 context, orig_type_left, orig_type_right);
918 return orig_type_left;
921 if (!is_type_valid(type_left))
924 if (!is_type_valid(type_right))
925 return orig_type_right;
930 static expression_t *parse_constant_expression(void)
932 /* start parsing at precedence 7 (conditional expression) */
933 expression_t *result = parse_sub_expression(7);
935 if(!is_constant_expression(result)) {
936 errorf(&result->base.source_position, "expression '%E' is not constant\n", result);
942 static expression_t *parse_assignment_expression(void)
944 /* start parsing at precedence 2 (assignment expression) */
945 return parse_sub_expression(2);
948 static type_t *make_global_typedef(const char *name, type_t *type)
950 symbol_t *const symbol = symbol_table_insert(name);
952 declaration_t *const declaration = allocate_declaration_zero();
953 declaration->namespc = NAMESPACE_NORMAL;
954 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
955 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
956 declaration->type = type;
957 declaration->symbol = symbol;
958 declaration->source_position = builtin_source_position;
960 record_declaration(declaration);
962 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
963 typedef_type->typedeft.declaration = declaration;
968 static string_t parse_string_literals(void)
970 assert(token.type == T_STRING_LITERAL);
971 string_t result = token.v.string;
975 while (token.type == T_STRING_LITERAL) {
976 result = concat_strings(&result, &token.v.string);
983 static const char *gnu_attribute_names[GNU_AK_LAST] = {
984 [GNU_AK_CONST] = "const",
985 [GNU_AK_VOLATILE] = "volatile",
986 [GNU_AK_CDECL] = "cdecl",
987 [GNU_AK_STDCALL] = "stdcall",
988 [GNU_AK_FASTCALL] = "fastcall",
989 [GNU_AK_DEPRECATED] = "deprecated",
990 [GNU_AK_NOINLINE] = "noinline",
991 [GNU_AK_NORETURN] = "noreturn",
992 [GNU_AK_NAKED] = "naked",
993 [GNU_AK_PURE] = "pure",
994 [GNU_AK_ALWAYS_INLINE] = "always_inline",
995 [GNU_AK_MALLOC] = "malloc",
996 [GNU_AK_WEAK] = "weak",
997 [GNU_AK_CONSTRUCTOR] = "constructor",
998 [GNU_AK_DESTRUCTOR] = "destructor",
999 [GNU_AK_NOTHROW] = "nothrow",
1000 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
1001 [GNU_AK_COMMON] = "coommon",
1002 [GNU_AK_NOCOMMON] = "nocommon",
1003 [GNU_AK_PACKED] = "packed",
1004 [GNU_AK_SHARED] = "shared",
1005 [GNU_AK_NOTSHARED] = "notshared",
1006 [GNU_AK_USED] = "used",
1007 [GNU_AK_UNUSED] = "unused",
1008 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1009 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1010 [GNU_AK_LONGCALL] = "longcall",
1011 [GNU_AK_SHORTCALL] = "shortcall",
1012 [GNU_AK_LONG_CALL] = "long_call",
1013 [GNU_AK_SHORT_CALL] = "short_call",
1014 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1015 [GNU_AK_INTERRUPT] = "interrupt",
1016 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1017 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1018 [GNU_AK_NESTING] = "nesting",
1019 [GNU_AK_NEAR] = "near",
1020 [GNU_AK_FAR] = "far",
1021 [GNU_AK_SIGNAL] = "signal",
1022 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1023 [GNU_AK_TINY_DATA] = "tiny_data",
1024 [GNU_AK_SAVEALL] = "saveall",
1025 [GNU_AK_FLATTEN] = "flatten",
1026 [GNU_AK_SSEREGPARM] = "sseregparm",
1027 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1028 [GNU_AK_RETURN_TWICE] = "return_twice",
1029 [GNU_AK_MAY_ALIAS] = "may_alias",
1030 [GNU_AK_MS_STRUCT] = "ms_struct",
1031 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1032 [GNU_AK_DLLIMPORT] = "dllimport",
1033 [GNU_AK_DLLEXPORT] = "dllexport",
1034 [GNU_AK_ALIGNED] = "aligned",
1035 [GNU_AK_ALIAS] = "alias",
1036 [GNU_AK_SECTION] = "section",
1037 [GNU_AK_FORMAT] = "format",
1038 [GNU_AK_FORMAT_ARG] = "format_arg",
1039 [GNU_AK_WEAKREF] = "weakref",
1040 [GNU_AK_NONNULL] = "nonnull",
1041 [GNU_AK_TLS_MODEL] = "tls_model",
1042 [GNU_AK_VISIBILITY] = "visibility",
1043 [GNU_AK_REGPARM] = "regparm",
1044 [GNU_AK_MODEL] = "model",
1045 [GNU_AK_TRAP_EXIT] = "trap_exit",
1046 [GNU_AK_SP_SWITCH] = "sp_switch",
1047 [GNU_AK_SENTINEL] = "sentinel"
1051 * compare two string, ignoring double underscores on the second.
1053 static int strcmp_underscore(const char *s1, const char *s2) {
1054 if(s2[0] == '_' && s2[1] == '_') {
1056 size_t l1 = strlen(s1);
1057 if(l1 + 2 != strlen(s2)) {
1061 return strncmp(s1, s2, l1);
1063 return strcmp(s1, s2);
1067 * Allocate a new gnu temporal attribute.
1069 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1070 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1071 attribute->kind = kind;
1072 attribute->next = NULL;
1073 attribute->invalid = false;
1074 attribute->have_arguments = false;
1079 * parse one constant expression argument.
1081 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1082 expression_t *expression;
1083 add_anchor_token(')');
1084 expression = parse_constant_expression();
1085 rem_anchor_token(')');
1090 attribute->invalid = true;
1094 * parse a list of constant expressions arguments.
1096 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1097 expression_t *expression;
1098 add_anchor_token(')');
1099 add_anchor_token(',');
1101 expression = parse_constant_expression();
1102 if(token.type != ',')
1106 rem_anchor_token(',');
1107 rem_anchor_token(')');
1112 attribute->invalid = true;
1116 * parse one string literal argument.
1118 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute, string_t *string) {
1119 add_anchor_token('(');
1120 if(token.type != T_STRING_LITERAL) {
1121 parse_error_expected("while parsing attribute directive", T_STRING_LITERAL, 0);
1124 *string = parse_string_literals();
1125 rem_anchor_token('(');
1129 attribute->invalid = true;
1133 * parse one tls model.
1135 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1136 static const char *tls_models[] = {
1142 string_t string = { NULL, 0 };
1143 parse_gnu_attribute_string_arg(attribute, &string);
1144 if(string.begin != NULL) {
1145 for(size_t i = 0; i < 4; ++i) {
1146 if(strcmp(tls_models[i], string.begin) == 0) {
1147 attribute->u.value = i;
1152 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1153 attribute->invalid = true;
1157 * parse one tls model.
1159 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1160 static const char *visibilities[] = {
1166 string_t string = { NULL, 0 };
1167 parse_gnu_attribute_string_arg(attribute, &string);
1168 if(string.begin != NULL) {
1169 for(size_t i = 0; i < 4; ++i) {
1170 if(strcmp(visibilities[i], string.begin) == 0) {
1171 attribute->u.value = i;
1176 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1177 attribute->invalid = true;
1181 * parse one (code) model.
1183 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1184 static const char *visibilities[] = {
1189 string_t string = { NULL, 0 };
1190 parse_gnu_attribute_string_arg(attribute, &string);
1191 if(string.begin != NULL) {
1192 for(int i = 0; i < 3; ++i) {
1193 if(strcmp(visibilities[i], string.begin) == 0) {
1194 attribute->u.value = i;
1199 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1200 attribute->invalid = true;
1204 * parse one interrupt argument.
1206 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1207 static const char *interrupts[] = {
1214 string_t string = { NULL, 0 };
1215 parse_gnu_attribute_string_arg(attribute, &string);
1216 if(string.begin != NULL) {
1217 for(size_t i = 0; i < 5; ++i) {
1218 if(strcmp(interrupts[i], string.begin) == 0) {
1219 attribute->u.value = i;
1224 errorf(HERE, "'%s' is an interrupt", string.begin);
1225 attribute->invalid = true;
1229 * parse ( identifier, const expression, const expression )
1231 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1232 static const char *format_names[] = {
1240 if(token.type != T_IDENTIFIER) {
1241 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, 0);
1244 const char *name = token.v.symbol->string;
1245 for(i = 0; i < 4; ++i) {
1246 if(strcmp_underscore(format_names[i], name) == 0)
1250 if(warning.attribute)
1251 warningf(HERE, "'%s' is an unrecognized format function type", name);
1256 add_anchor_token(')');
1257 add_anchor_token(',');
1258 parse_constant_expression();
1259 rem_anchor_token(',');
1260 rem_anchor_token('(');
1263 add_anchor_token(')');
1264 parse_constant_expression();
1265 rem_anchor_token('(');
1269 attribute->u.value = true;
1273 * Parse one GNU attribute.
1275 * Note that attribute names can be specified WITH or WITHOUT
1276 * double underscores, ie const or __const__.
1278 * The following attributes are parsed without arguments
1303 * no_instrument_function
1304 * warn_unused_result
1321 * externally_visible
1329 * The following attributes are parsed with arguments
1330 * aligned( const expression )
1331 * alias( string literal )
1332 * section( string literal )
1333 * format( identifier, const expression, const expression )
1334 * format_arg( const expression )
1335 * tls_model( string literal )
1336 * visibility( string literal )
1337 * regparm( const expression )
1338 * model( string leteral )
1339 * trap_exit( const expression )
1340 * sp_switch( string literal )
1342 * The following attributes might have arguments
1343 * weak_ref( string literal )
1344 * non_null( const expression // ',' )
1345 * interrupt( string literal )
1346 * sentinel( constant expression )
1348 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1350 gnu_attribute_t *head = *attributes;
1351 gnu_attribute_t *last = *attributes;
1352 gnu_attribute_t *attribute;
1354 eat(T___attribute__);
1358 if(token.type != ')') {
1359 /* find the end of the list */
1361 while(last->next != NULL)
1365 /* non-empty attribute list */
1368 if(token.type == T_const) {
1370 } else if(token.type == T_volatile) {
1372 } else if(token.type == T_cdecl) {
1373 /* __attribute__((cdecl)), WITH ms mode */
1375 } else if(token.type != T_IDENTIFIER) {
1376 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, 0);
1379 const symbol_t *sym = token.v.symbol;
1384 for(i = 0; i < GNU_AK_LAST; ++i) {
1385 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1388 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1391 if(kind == GNU_AK_LAST) {
1392 if(warning.attribute)
1393 warningf(HERE, "'%s' attribute directive ignored", name);
1395 /* skip possible arguments */
1396 if(token.type == '(') {
1397 eat_until_matching_token(')');
1400 /* check for arguments */
1401 attribute = allocate_gnu_attribute(kind);
1402 if(token.type == '(') {
1404 if(token.type == ')') {
1405 /* empty args are allowed */
1408 attribute->have_arguments = true;
1413 case GNU_AK_VOLATILE:
1415 case GNU_AK_STDCALL:
1416 case GNU_AK_FASTCALL:
1417 case GNU_AK_DEPRECATED:
1418 case GNU_AK_NOINLINE:
1419 case GNU_AK_NORETURN:
1422 case GNU_AK_ALWAYS_INLINE:
1425 case GNU_AK_CONSTRUCTOR:
1426 case GNU_AK_DESTRUCTOR:
1427 case GNU_AK_NOTHROW:
1428 case GNU_AK_TRANSPARENT_UNION:
1430 case GNU_AK_NOCOMMON:
1433 case GNU_AK_NOTSHARED:
1436 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1437 case GNU_AK_WARN_UNUSED_RESULT:
1438 case GNU_AK_LONGCALL:
1439 case GNU_AK_SHORTCALL:
1440 case GNU_AK_LONG_CALL:
1441 case GNU_AK_SHORT_CALL:
1442 case GNU_AK_FUNCTION_VECTOR:
1443 case GNU_AK_INTERRUPT_HANDLER:
1444 case GNU_AK_NMI_HANDLER:
1445 case GNU_AK_NESTING:
1449 case GNU_AK_EIGTHBIT_DATA:
1450 case GNU_AK_TINY_DATA:
1451 case GNU_AK_SAVEALL:
1452 case GNU_AK_FLATTEN:
1453 case GNU_AK_SSEREGPARM:
1454 case GNU_AK_EXTERNALLY_VISIBLE:
1455 case GNU_AK_RETURN_TWICE:
1456 case GNU_AK_MAY_ALIAS:
1457 case GNU_AK_MS_STRUCT:
1458 case GNU_AK_GCC_STRUCT:
1459 case GNU_AK_DLLIMPORT:
1460 case GNU_AK_DLLEXPORT:
1461 if(attribute->have_arguments) {
1462 /* should have no arguments */
1463 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1464 eat_until_matching_token('(');
1465 /* we have already consumed '(', so we stop before ')', eat it */
1467 attribute->invalid = true;
1471 case GNU_AK_ALIGNED:
1472 case GNU_AK_FORMAT_ARG:
1473 case GNU_AK_REGPARM:
1474 case GNU_AK_TRAP_EXIT:
1475 if(!attribute->have_arguments) {
1476 /* should have arguments */
1477 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1478 attribute->invalid = true;
1480 parse_gnu_attribute_const_arg(attribute);
1483 case GNU_AK_SECTION:
1484 case GNU_AK_SP_SWITCH:
1485 if(!attribute->have_arguments) {
1486 /* should have arguments */
1487 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1488 attribute->invalid = true;
1490 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1493 if(!attribute->have_arguments) {
1494 /* should have arguments */
1495 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1496 attribute->invalid = true;
1498 parse_gnu_attribute_format_args(attribute);
1500 case GNU_AK_WEAKREF:
1501 /* may have one string argument */
1502 if(attribute->have_arguments)
1503 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1505 case GNU_AK_NONNULL:
1506 if(attribute->have_arguments)
1507 parse_gnu_attribute_const_arg_list(attribute);
1509 case GNU_AK_TLS_MODEL:
1510 if(!attribute->have_arguments) {
1511 /* should have arguments */
1512 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1514 parse_gnu_attribute_tls_model_arg(attribute);
1516 case GNU_AK_VISIBILITY:
1517 if(!attribute->have_arguments) {
1518 /* should have arguments */
1519 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1521 parse_gnu_attribute_visibility_arg(attribute);
1524 if(!attribute->have_arguments) {
1525 /* should have arguments */
1526 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1528 parse_gnu_attribute_model_arg(attribute);
1529 case GNU_AK_INTERRUPT:
1530 /* may have one string argument */
1531 if(attribute->have_arguments)
1532 parse_gnu_attribute_interrupt_arg(attribute);
1534 case GNU_AK_SENTINEL:
1535 /* may have one string argument */
1536 if(attribute->have_arguments)
1537 parse_gnu_attribute_const_arg(attribute);
1540 /* already handled */
1544 if(attribute != NULL) {
1546 last->next = attribute;
1549 head = last = attribute;
1553 if(token.type != ',')
1565 * Parse GNU attributes.
1567 static void parse_attributes(gnu_attribute_t **attributes)
1570 switch(token.type) {
1571 case T___attribute__: {
1572 parse_gnu_attribute(attributes);
1578 if(token.type != T_STRING_LITERAL) {
1579 parse_error_expected("while parsing assembler attribute",
1580 T_STRING_LITERAL, 0);
1581 eat_until_matching_token('(');
1584 parse_string_literals();
1589 goto attributes_finished;
1593 attributes_finished:
1598 static designator_t *parse_designation(void)
1600 designator_t *result = NULL;
1601 designator_t *last = NULL;
1604 designator_t *designator;
1605 switch(token.type) {
1607 designator = allocate_ast_zero(sizeof(designator[0]));
1608 designator->source_position = token.source_position;
1610 add_anchor_token(']');
1611 designator->array_index = parse_constant_expression();
1612 rem_anchor_token(']');
1616 designator = allocate_ast_zero(sizeof(designator[0]));
1617 designator->source_position = token.source_position;
1619 if(token.type != T_IDENTIFIER) {
1620 parse_error_expected("while parsing designator",
1624 designator->symbol = token.v.symbol;
1632 assert(designator != NULL);
1634 last->next = designator;
1636 result = designator;
1644 static initializer_t *initializer_from_string(array_type_t *type,
1645 const string_t *const string)
1647 /* TODO: check len vs. size of array type */
1650 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1651 initializer->string.string = *string;
1656 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1657 wide_string_t *const string)
1659 /* TODO: check len vs. size of array type */
1662 initializer_t *const initializer =
1663 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1664 initializer->wide_string.string = *string;
1670 * Build an initializer from a given expression.
1672 static initializer_t *initializer_from_expression(type_t *orig_type,
1673 expression_t *expression)
1675 /* TODO check that expression is a constant expression */
1677 /* § 6.7.8.14/15 char array may be initialized by string literals */
1678 type_t *type = skip_typeref(orig_type);
1679 type_t *expr_type_orig = expression->base.type;
1680 type_t *expr_type = skip_typeref(expr_type_orig);
1681 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1682 array_type_t *const array_type = &type->array;
1683 type_t *const element_type = skip_typeref(array_type->element_type);
1685 if (element_type->kind == TYPE_ATOMIC) {
1686 atomic_type_kind_t akind = element_type->atomic.akind;
1687 switch (expression->kind) {
1688 case EXPR_STRING_LITERAL:
1689 if (akind == ATOMIC_TYPE_CHAR
1690 || akind == ATOMIC_TYPE_SCHAR
1691 || akind == ATOMIC_TYPE_UCHAR) {
1692 return initializer_from_string(array_type,
1693 &expression->string.value);
1696 case EXPR_WIDE_STRING_LITERAL: {
1697 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1698 if (get_unqualified_type(element_type) == bare_wchar_type) {
1699 return initializer_from_wide_string(array_type,
1700 &expression->wide_string.value);
1710 type_t *const res_type = semantic_assign(type, expression, "initializer",
1711 &expression->base.source_position);
1712 if (res_type == NULL)
1715 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1716 result->value.value = create_implicit_cast(expression, res_type);
1722 * Checks if a given expression can be used as an constant initializer.
1724 static bool is_initializer_constant(const expression_t *expression)
1726 return is_constant_expression(expression)
1727 || is_address_constant(expression);
1731 * Parses an scalar initializer.
1733 * § 6.7.8.11; eat {} without warning
1735 static initializer_t *parse_scalar_initializer(type_t *type,
1736 bool must_be_constant)
1738 /* there might be extra {} hierarchies */
1740 while(token.type == '{') {
1743 warningf(HERE, "extra curly braces around scalar initializer");
1748 expression_t *expression = parse_assignment_expression();
1749 if(must_be_constant && !is_initializer_constant(expression)) {
1750 errorf(&expression->base.source_position,
1751 "Initialisation expression '%E' is not constant\n",
1755 initializer_t *initializer = initializer_from_expression(type, expression);
1757 if(initializer == NULL) {
1758 errorf(&expression->base.source_position,
1759 "expression '%E' (type '%T') doesn't match expected type '%T'",
1760 expression, expression->base.type, type);
1765 bool additional_warning_displayed = false;
1767 if(token.type == ',') {
1770 if(token.type != '}') {
1771 if(!additional_warning_displayed) {
1772 warningf(HERE, "additional elements in scalar initializer");
1773 additional_warning_displayed = true;
1784 * An entry in the type path.
1786 typedef struct type_path_entry_t type_path_entry_t;
1787 struct type_path_entry_t {
1788 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1790 size_t index; /**< For array types: the current index. */
1791 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1796 * A type path expression a position inside compound or array types.
1798 typedef struct type_path_t type_path_t;
1799 struct type_path_t {
1800 type_path_entry_t *path; /**< An flexible array containing the current path. */
1801 type_t *top_type; /**< type of the element the path points */
1802 size_t max_index; /**< largest index in outermost array */
1806 * Prints a type path for debugging.
1808 static __attribute__((unused)) void debug_print_type_path(
1809 const type_path_t *path)
1811 size_t len = ARR_LEN(path->path);
1813 for(size_t i = 0; i < len; ++i) {
1814 const type_path_entry_t *entry = & path->path[i];
1816 type_t *type = skip_typeref(entry->type);
1817 if(is_type_compound(type)) {
1818 /* in gcc mode structs can have no members */
1819 if(entry->v.compound_entry == NULL) {
1823 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1824 } else if(is_type_array(type)) {
1825 fprintf(stderr, "[%zd]", entry->v.index);
1827 fprintf(stderr, "-INVALID-");
1830 if(path->top_type != NULL) {
1831 fprintf(stderr, " (");
1832 print_type(path->top_type);
1833 fprintf(stderr, ")");
1838 * Return the top type path entry, ie. in a path
1839 * (type).a.b returns the b.
1841 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1843 size_t len = ARR_LEN(path->path);
1845 return &path->path[len-1];
1849 * Enlarge the type path by an (empty) element.
1851 static type_path_entry_t *append_to_type_path(type_path_t *path)
1853 size_t len = ARR_LEN(path->path);
1854 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1856 type_path_entry_t *result = & path->path[len];
1857 memset(result, 0, sizeof(result[0]));
1862 * Descending into a sub-type. Enter the scope of the current
1865 static void descend_into_subtype(type_path_t *path)
1867 type_t *orig_top_type = path->top_type;
1868 type_t *top_type = skip_typeref(orig_top_type);
1870 assert(is_type_compound(top_type) || is_type_array(top_type));
1872 type_path_entry_t *top = append_to_type_path(path);
1873 top->type = top_type;
1875 if(is_type_compound(top_type)) {
1876 declaration_t *declaration = top_type->compound.declaration;
1877 declaration_t *entry = declaration->scope.declarations;
1878 top->v.compound_entry = entry;
1881 path->top_type = entry->type;
1883 path->top_type = NULL;
1886 assert(is_type_array(top_type));
1889 path->top_type = top_type->array.element_type;
1894 * Pop an entry from the given type path, ie. returning from
1895 * (type).a.b to (type).a
1897 static void ascend_from_subtype(type_path_t *path)
1899 type_path_entry_t *top = get_type_path_top(path);
1901 path->top_type = top->type;
1903 size_t len = ARR_LEN(path->path);
1904 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1908 * Pop entries from the given type path until the given
1909 * path level is reached.
1911 static void ascend_to(type_path_t *path, size_t top_path_level)
1913 size_t len = ARR_LEN(path->path);
1915 while(len > top_path_level) {
1916 ascend_from_subtype(path);
1917 len = ARR_LEN(path->path);
1921 static bool walk_designator(type_path_t *path, const designator_t *designator,
1922 bool used_in_offsetof)
1924 for( ; designator != NULL; designator = designator->next) {
1925 type_path_entry_t *top = get_type_path_top(path);
1926 type_t *orig_type = top->type;
1928 type_t *type = skip_typeref(orig_type);
1930 if(designator->symbol != NULL) {
1931 symbol_t *symbol = designator->symbol;
1932 if(!is_type_compound(type)) {
1933 if(is_type_valid(type)) {
1934 errorf(&designator->source_position,
1935 "'.%Y' designator used for non-compound type '%T'",
1941 declaration_t *declaration = type->compound.declaration;
1942 declaration_t *iter = declaration->scope.declarations;
1943 for( ; iter != NULL; iter = iter->next) {
1944 if(iter->symbol == symbol) {
1949 errorf(&designator->source_position,
1950 "'%T' has no member named '%Y'", orig_type, symbol);
1953 if(used_in_offsetof) {
1954 type_t *real_type = skip_typeref(iter->type);
1955 if(real_type->kind == TYPE_BITFIELD) {
1956 errorf(&designator->source_position,
1957 "offsetof designator '%Y' may not specify bitfield",
1963 top->type = orig_type;
1964 top->v.compound_entry = iter;
1965 orig_type = iter->type;
1967 expression_t *array_index = designator->array_index;
1968 assert(designator->array_index != NULL);
1970 if(!is_type_array(type)) {
1971 if(is_type_valid(type)) {
1972 errorf(&designator->source_position,
1973 "[%E] designator used for non-array type '%T'",
1974 array_index, orig_type);
1978 if(!is_type_valid(array_index->base.type)) {
1982 long index = fold_constant(array_index);
1983 if(!used_in_offsetof) {
1985 errorf(&designator->source_position,
1986 "array index [%E] must be positive", array_index);
1989 if(type->array.size_constant == true) {
1990 long array_size = type->array.size;
1991 if(index >= array_size) {
1992 errorf(&designator->source_position,
1993 "designator [%E] (%d) exceeds array size %d",
1994 array_index, index, array_size);
2000 top->type = orig_type;
2001 top->v.index = (size_t) index;
2002 orig_type = type->array.element_type;
2004 path->top_type = orig_type;
2006 if(designator->next != NULL) {
2007 descend_into_subtype(path);
2016 static void advance_current_object(type_path_t *path, size_t top_path_level)
2018 type_path_entry_t *top = get_type_path_top(path);
2020 type_t *type = skip_typeref(top->type);
2021 if(is_type_union(type)) {
2022 /* in unions only the first element is initialized */
2023 top->v.compound_entry = NULL;
2024 } else if(is_type_struct(type)) {
2025 declaration_t *entry = top->v.compound_entry;
2027 entry = entry->next;
2028 top->v.compound_entry = entry;
2030 path->top_type = entry->type;
2034 assert(is_type_array(type));
2038 if(!type->array.size_constant || top->v.index < type->array.size) {
2043 /* we're past the last member of the current sub-aggregate, try if we
2044 * can ascend in the type hierarchy and continue with another subobject */
2045 size_t len = ARR_LEN(path->path);
2047 if(len > top_path_level) {
2048 ascend_from_subtype(path);
2049 advance_current_object(path, top_path_level);
2051 path->top_type = NULL;
2056 * skip until token is found.
2058 static void skip_until(int type) {
2059 while(token.type != type) {
2060 if(token.type == T_EOF)
2067 * skip any {...} blocks until a closing braket is reached.
2069 static void skip_initializers(void)
2071 if(token.type == '{')
2074 while(token.type != '}') {
2075 if(token.type == T_EOF)
2077 if(token.type == '{') {
2085 static initializer_t *create_empty_initializer(void)
2087 static initializer_t empty_initializer
2088 = { .list = { { INITIALIZER_LIST }, 0 } };
2089 return &empty_initializer;
2093 * Parse a part of an initialiser for a struct or union,
2095 static initializer_t *parse_sub_initializer(type_path_t *path,
2096 type_t *outer_type, size_t top_path_level,
2097 parse_initializer_env_t *env)
2099 if(token.type == '}') {
2100 /* empty initializer */
2101 return create_empty_initializer();
2104 type_t *orig_type = path->top_type;
2105 type_t *type = NULL;
2107 if (orig_type == NULL) {
2108 /* We are initializing an empty compound. */
2110 type = skip_typeref(orig_type);
2112 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2113 * initializers in this case. */
2114 if(!is_type_valid(type)) {
2115 skip_initializers();
2116 return create_empty_initializer();
2120 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2123 designator_t *designator = NULL;
2124 if(token.type == '.' || token.type == '[') {
2125 designator = parse_designation();
2127 /* reset path to toplevel, evaluate designator from there */
2128 ascend_to(path, top_path_level);
2129 if(!walk_designator(path, designator, false)) {
2130 /* can't continue after designation error */
2134 initializer_t *designator_initializer
2135 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2136 designator_initializer->designator.designator = designator;
2137 ARR_APP1(initializer_t*, initializers, designator_initializer);
2142 if(token.type == '{') {
2143 if(type != NULL && is_type_scalar(type)) {
2144 sub = parse_scalar_initializer(type, env->must_be_constant);
2148 if (env->declaration != NULL)
2149 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2150 env->declaration->symbol);
2152 errorf(HERE, "extra brace group at end of initializer");
2154 descend_into_subtype(path);
2156 add_anchor_token('}');
2157 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2159 rem_anchor_token('}');
2162 ascend_from_subtype(path);
2166 goto error_parse_next;
2170 /* must be an expression */
2171 expression_t *expression = parse_assignment_expression();
2173 if(env->must_be_constant && !is_initializer_constant(expression)) {
2174 errorf(&expression->base.source_position,
2175 "Initialisation expression '%E' is not constant\n",
2180 /* we are already outside, ... */
2184 /* handle { "string" } special case */
2185 if((expression->kind == EXPR_STRING_LITERAL
2186 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2187 && outer_type != NULL) {
2188 sub = initializer_from_expression(outer_type, expression);
2190 if(token.type == ',') {
2193 if(token.type != '}') {
2194 warningf(HERE, "excessive elements in initializer for type '%T'",
2197 /* TODO: eat , ... */
2202 /* descend into subtypes until expression matches type */
2204 orig_type = path->top_type;
2205 type = skip_typeref(orig_type);
2207 sub = initializer_from_expression(orig_type, expression);
2211 if(!is_type_valid(type)) {
2214 if(is_type_scalar(type)) {
2215 errorf(&expression->base.source_position,
2216 "expression '%E' doesn't match expected type '%T'",
2217 expression, orig_type);
2221 descend_into_subtype(path);
2225 /* update largest index of top array */
2226 const type_path_entry_t *first = &path->path[0];
2227 type_t *first_type = first->type;
2228 first_type = skip_typeref(first_type);
2229 if(is_type_array(first_type)) {
2230 size_t index = first->v.index;
2231 if(index > path->max_index)
2232 path->max_index = index;
2236 /* append to initializers list */
2237 ARR_APP1(initializer_t*, initializers, sub);
2240 if(env->declaration != NULL)
2241 warningf(HERE, "excess elements in struct initializer for '%Y'",
2242 env->declaration->symbol);
2244 warningf(HERE, "excess elements in struct initializer");
2248 if(token.type == '}') {
2252 if(token.type == '}') {
2257 /* advance to the next declaration if we are not at the end */
2258 advance_current_object(path, top_path_level);
2259 orig_type = path->top_type;
2260 if(orig_type != NULL)
2261 type = skip_typeref(orig_type);
2267 size_t len = ARR_LEN(initializers);
2268 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2269 initializer_t *result = allocate_ast_zero(size);
2270 result->kind = INITIALIZER_LIST;
2271 result->list.len = len;
2272 memcpy(&result->list.initializers, initializers,
2273 len * sizeof(initializers[0]));
2275 DEL_ARR_F(initializers);
2276 ascend_to(path, top_path_level);
2281 skip_initializers();
2282 DEL_ARR_F(initializers);
2283 ascend_to(path, top_path_level);
2288 * Parses an initializer. Parsers either a compound literal
2289 * (env->declaration == NULL) or an initializer of a declaration.
2291 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2293 type_t *type = skip_typeref(env->type);
2294 initializer_t *result = NULL;
2297 if(is_type_scalar(type)) {
2298 result = parse_scalar_initializer(type, env->must_be_constant);
2299 } else if(token.type == '{') {
2303 memset(&path, 0, sizeof(path));
2304 path.top_type = env->type;
2305 path.path = NEW_ARR_F(type_path_entry_t, 0);
2307 descend_into_subtype(&path);
2309 add_anchor_token('}');
2310 result = parse_sub_initializer(&path, env->type, 1, env);
2311 rem_anchor_token('}');
2313 max_index = path.max_index;
2314 DEL_ARR_F(path.path);
2318 /* parse_scalar_initializer() also works in this case: we simply
2319 * have an expression without {} around it */
2320 result = parse_scalar_initializer(type, env->must_be_constant);
2323 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2324 * the array type size */
2325 if(is_type_array(type) && type->array.size_expression == NULL
2326 && result != NULL) {
2328 switch (result->kind) {
2329 case INITIALIZER_LIST:
2330 size = max_index + 1;
2333 case INITIALIZER_STRING:
2334 size = result->string.string.size;
2337 case INITIALIZER_WIDE_STRING:
2338 size = result->wide_string.string.size;
2342 internal_errorf(HERE, "invalid initializer type");
2345 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2346 cnst->base.type = type_size_t;
2347 cnst->conste.v.int_value = size;
2349 type_t *new_type = duplicate_type(type);
2351 new_type->array.size_expression = cnst;
2352 new_type->array.size_constant = true;
2353 new_type->array.size = size;
2354 env->type = new_type;
2362 static declaration_t *append_declaration(declaration_t *declaration);
2364 static declaration_t *parse_compound_type_specifier(bool is_struct)
2366 gnu_attribute_t *attributes = NULL;
2373 symbol_t *symbol = NULL;
2374 declaration_t *declaration = NULL;
2376 if (token.type == T___attribute__) {
2377 parse_attributes(&attributes);
2380 if(token.type == T_IDENTIFIER) {
2381 symbol = token.v.symbol;
2385 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2387 declaration = get_declaration(symbol, NAMESPACE_UNION);
2389 } else if(token.type != '{') {
2391 parse_error_expected("while parsing struct type specifier",
2392 T_IDENTIFIER, '{', 0);
2394 parse_error_expected("while parsing union type specifier",
2395 T_IDENTIFIER, '{', 0);
2401 if(declaration == NULL) {
2402 declaration = allocate_declaration_zero();
2403 declaration->namespc =
2404 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2405 declaration->source_position = token.source_position;
2406 declaration->symbol = symbol;
2407 declaration->parent_scope = scope;
2408 if (symbol != NULL) {
2409 environment_push(declaration);
2411 append_declaration(declaration);
2414 if(token.type == '{') {
2415 if(declaration->init.is_defined) {
2416 assert(symbol != NULL);
2417 errorf(HERE, "multiple definitions of '%s %Y'",
2418 is_struct ? "struct" : "union", symbol);
2419 declaration->scope.declarations = NULL;
2421 declaration->init.is_defined = true;
2423 parse_compound_type_entries(declaration);
2424 parse_attributes(&attributes);
2430 static void parse_enum_entries(type_t *const enum_type)
2434 if(token.type == '}') {
2436 errorf(HERE, "empty enum not allowed");
2440 add_anchor_token('}');
2442 if(token.type != T_IDENTIFIER) {
2443 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
2445 rem_anchor_token('}');
2449 declaration_t *const entry = allocate_declaration_zero();
2450 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2451 entry->type = enum_type;
2452 entry->symbol = token.v.symbol;
2453 entry->source_position = token.source_position;
2456 if(token.type == '=') {
2458 expression_t *value = parse_constant_expression();
2460 value = create_implicit_cast(value, enum_type);
2461 entry->init.enum_value = value;
2466 record_declaration(entry);
2468 if(token.type != ',')
2471 } while(token.type != '}');
2472 rem_anchor_token('}');
2480 static type_t *parse_enum_specifier(void)
2482 gnu_attribute_t *attributes = NULL;
2483 declaration_t *declaration;
2487 if(token.type == T_IDENTIFIER) {
2488 symbol = token.v.symbol;
2491 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2492 } else if(token.type != '{') {
2493 parse_error_expected("while parsing enum type specifier",
2494 T_IDENTIFIER, '{', 0);
2501 if(declaration == NULL) {
2502 declaration = allocate_declaration_zero();
2503 declaration->namespc = NAMESPACE_ENUM;
2504 declaration->source_position = token.source_position;
2505 declaration->symbol = symbol;
2506 declaration->parent_scope = scope;
2509 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2510 type->enumt.declaration = declaration;
2512 if(token.type == '{') {
2513 if(declaration->init.is_defined) {
2514 errorf(HERE, "multiple definitions of enum %Y", symbol);
2516 if (symbol != NULL) {
2517 environment_push(declaration);
2519 append_declaration(declaration);
2520 declaration->init.is_defined = 1;
2522 parse_enum_entries(type);
2523 parse_attributes(&attributes);
2530 * if a symbol is a typedef to another type, return true
2532 static bool is_typedef_symbol(symbol_t *symbol)
2534 const declaration_t *const declaration =
2535 get_declaration(symbol, NAMESPACE_NORMAL);
2537 declaration != NULL &&
2538 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2541 static type_t *parse_typeof(void)
2548 add_anchor_token(')');
2550 expression_t *expression = NULL;
2553 switch(token.type) {
2554 case T___extension__:
2555 /* this can be a prefix to a typename or an expression */
2556 /* we simply eat it now. */
2559 } while(token.type == T___extension__);
2563 if(is_typedef_symbol(token.v.symbol)) {
2564 type = parse_typename();
2566 expression = parse_expression();
2567 type = expression->base.type;
2572 type = parse_typename();
2576 expression = parse_expression();
2577 type = expression->base.type;
2581 rem_anchor_token(')');
2584 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2585 typeof_type->typeoft.expression = expression;
2586 typeof_type->typeoft.typeof_type = type;
2594 SPECIFIER_SIGNED = 1 << 0,
2595 SPECIFIER_UNSIGNED = 1 << 1,
2596 SPECIFIER_LONG = 1 << 2,
2597 SPECIFIER_INT = 1 << 3,
2598 SPECIFIER_DOUBLE = 1 << 4,
2599 SPECIFIER_CHAR = 1 << 5,
2600 SPECIFIER_SHORT = 1 << 6,
2601 SPECIFIER_LONG_LONG = 1 << 7,
2602 SPECIFIER_FLOAT = 1 << 8,
2603 SPECIFIER_BOOL = 1 << 9,
2604 SPECIFIER_VOID = 1 << 10,
2605 SPECIFIER_INT8 = 1 << 11,
2606 SPECIFIER_INT16 = 1 << 12,
2607 SPECIFIER_INT32 = 1 << 13,
2608 SPECIFIER_INT64 = 1 << 14,
2609 SPECIFIER_INT128 = 1 << 15,
2610 #ifdef PROVIDE_COMPLEX
2611 SPECIFIER_COMPLEX = 1 << 16,
2612 SPECIFIER_IMAGINARY = 1 << 17,
2616 static type_t *create_builtin_type(symbol_t *const symbol,
2617 type_t *const real_type)
2619 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2620 type->builtin.symbol = symbol;
2621 type->builtin.real_type = real_type;
2623 type_t *result = typehash_insert(type);
2624 if (type != result) {
2631 static type_t *get_typedef_type(symbol_t *symbol)
2633 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2634 if(declaration == NULL
2635 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2638 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2639 type->typedeft.declaration = declaration;
2645 * check for the allowed MS alignment values.
2647 static bool check_elignment_value(long long intvalue) {
2648 if(intvalue < 1 || intvalue > 8192) {
2649 errorf(HERE, "illegal alignment value");
2652 unsigned v = (unsigned)intvalue;
2653 for(unsigned i = 1; i <= 8192; i += i) {
2657 errorf(HERE, "alignment must be power of two");
2661 #define DET_MOD(name, tag) do { \
2662 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2663 *modifiers |= tag; \
2666 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2668 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2671 if(token.type == T_restrict) {
2673 DET_MOD(restrict, DM_RESTRICT);
2675 } else if(token.type != T_IDENTIFIER)
2677 symbol_t *symbol = token.v.symbol;
2678 if(symbol == sym_align) {
2681 if(token.type != T_INTEGER)
2683 if(check_elignment_value(token.v.intvalue)) {
2684 if(specifiers->alignment != 0)
2685 warningf(HERE, "align used more than once");
2686 specifiers->alignment = (unsigned char)token.v.intvalue;
2690 } else if(symbol == sym_allocate) {
2693 if(token.type != T_IDENTIFIER)
2695 (void)token.v.symbol;
2697 } else if(symbol == sym_dllimport) {
2699 DET_MOD(dllimport, DM_DLLIMPORT);
2700 } else if(symbol == sym_dllexport) {
2702 DET_MOD(dllexport, DM_DLLEXPORT);
2703 } else if(symbol == sym_thread) {
2705 DET_MOD(thread, DM_THREAD);
2706 } else if(symbol == sym_naked) {
2708 DET_MOD(naked, DM_NAKED);
2709 } else if(symbol == sym_noinline) {
2711 DET_MOD(noinline, DM_NOINLINE);
2712 } else if(symbol == sym_noreturn) {
2714 DET_MOD(noreturn, DM_NORETURN);
2715 } else if(symbol == sym_nothrow) {
2717 DET_MOD(nothrow, DM_NOTHROW);
2718 } else if(symbol == sym_novtable) {
2720 DET_MOD(novtable, DM_NOVTABLE);
2721 } else if(symbol == sym_property) {
2725 bool is_get = false;
2726 if(token.type != T_IDENTIFIER)
2728 if(token.v.symbol == sym_get) {
2730 } else if(token.v.symbol == sym_put) {
2732 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2737 if(token.type != T_IDENTIFIER)
2740 if(specifiers->get_property_sym != NULL) {
2741 errorf(HERE, "get property name already specified");
2743 specifiers->get_property_sym = token.v.symbol;
2746 if(specifiers->put_property_sym != NULL) {
2747 errorf(HERE, "put property name already specified");
2749 specifiers->put_property_sym = token.v.symbol;
2753 if(token.type == ',') {
2760 } else if(symbol == sym_selectany) {
2762 DET_MOD(selectany, DM_SELECTANY);
2763 } else if(symbol == sym_uuid) {
2766 if(token.type != T_STRING_LITERAL)
2770 } else if(symbol == sym_deprecated) {
2772 if(specifiers->deprecated != 0)
2773 warningf(HERE, "deprecated used more than once");
2774 specifiers->deprecated = 1;
2775 if(token.type == '(') {
2777 if(token.type == T_STRING_LITERAL) {
2778 specifiers->deprecated_string = token.v.string.begin;
2781 errorf(HERE, "string literal expected");
2785 } else if(symbol == sym_noalias) {
2787 DET_MOD(noalias, DM_NOALIAS);
2789 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2791 if(token.type == '(')
2795 if (token.type == ',')
2802 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2804 type_t *type = NULL;
2805 unsigned type_qualifiers = 0;
2806 unsigned type_specifiers = 0;
2809 specifiers->source_position = token.source_position;
2812 switch(token.type) {
2815 #define MATCH_STORAGE_CLASS(token, class) \
2817 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2818 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2820 specifiers->declared_storage_class = class; \
2824 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2825 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2826 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2827 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2828 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2833 add_anchor_token(')');
2834 parse_microsoft_extended_decl_modifier(specifiers);
2835 rem_anchor_token(')');
2840 switch (specifiers->declared_storage_class) {
2841 case STORAGE_CLASS_NONE:
2842 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2845 case STORAGE_CLASS_EXTERN:
2846 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2849 case STORAGE_CLASS_STATIC:
2850 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2854 errorf(HERE, "multiple storage classes in declaration specifiers");
2860 /* type qualifiers */
2861 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2863 type_qualifiers |= qualifier; \
2867 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2868 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2869 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2870 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2871 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2872 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2873 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2874 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2876 case T___extension__:
2881 /* type specifiers */
2882 #define MATCH_SPECIFIER(token, specifier, name) \
2885 if(type_specifiers & specifier) { \
2886 errorf(HERE, "multiple " name " type specifiers given"); \
2888 type_specifiers |= specifier; \
2892 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2893 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2894 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2895 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2896 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2897 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2898 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2899 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2900 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2901 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2902 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2903 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2904 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2905 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2906 #ifdef PROVIDE_COMPLEX
2907 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2908 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2910 case T__forceinline:
2911 /* only in microsoft mode */
2912 specifiers->decl_modifiers |= DM_FORCEINLINE;
2916 specifiers->is_inline = true;
2921 if(type_specifiers & SPECIFIER_LONG_LONG) {
2922 errorf(HERE, "multiple type specifiers given");
2923 } else if(type_specifiers & SPECIFIER_LONG) {
2924 type_specifiers |= SPECIFIER_LONG_LONG;
2926 type_specifiers |= SPECIFIER_LONG;
2931 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2933 type->compound.declaration = parse_compound_type_specifier(true);
2937 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2939 type->compound.declaration = parse_compound_type_specifier(false);
2943 type = parse_enum_specifier();
2946 type = parse_typeof();
2948 case T___builtin_va_list:
2949 type = duplicate_type(type_valist);
2953 case T___attribute__:
2954 parse_attributes(&specifiers->gnu_attributes);
2957 case T_IDENTIFIER: {
2958 /* only parse identifier if we haven't found a type yet */
2959 if(type != NULL || type_specifiers != 0)
2960 goto finish_specifiers;
2962 type_t *typedef_type = get_typedef_type(token.v.symbol);
2964 if(typedef_type == NULL)
2965 goto finish_specifiers;
2968 type = typedef_type;
2972 /* function specifier */
2974 goto finish_specifiers;
2981 atomic_type_kind_t atomic_type;
2983 /* match valid basic types */
2984 switch(type_specifiers) {
2985 case SPECIFIER_VOID:
2986 atomic_type = ATOMIC_TYPE_VOID;
2988 case SPECIFIER_CHAR:
2989 atomic_type = ATOMIC_TYPE_CHAR;
2991 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2992 atomic_type = ATOMIC_TYPE_SCHAR;
2994 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2995 atomic_type = ATOMIC_TYPE_UCHAR;
2997 case SPECIFIER_SHORT:
2998 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2999 case SPECIFIER_SHORT | SPECIFIER_INT:
3000 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3001 atomic_type = ATOMIC_TYPE_SHORT;
3003 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3004 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3005 atomic_type = ATOMIC_TYPE_USHORT;
3008 case SPECIFIER_SIGNED:
3009 case SPECIFIER_SIGNED | SPECIFIER_INT:
3010 atomic_type = ATOMIC_TYPE_INT;
3012 case SPECIFIER_UNSIGNED:
3013 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3014 atomic_type = ATOMIC_TYPE_UINT;
3016 case SPECIFIER_LONG:
3017 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3018 case SPECIFIER_LONG | SPECIFIER_INT:
3019 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3020 atomic_type = ATOMIC_TYPE_LONG;
3022 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3023 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3024 atomic_type = ATOMIC_TYPE_ULONG;
3026 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3027 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3028 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3029 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3031 atomic_type = ATOMIC_TYPE_LONGLONG;
3033 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3034 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3036 atomic_type = ATOMIC_TYPE_ULONGLONG;
3039 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3040 atomic_type = unsigned_int8_type_kind;
3043 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3044 atomic_type = unsigned_int16_type_kind;
3047 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3048 atomic_type = unsigned_int32_type_kind;
3051 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3052 atomic_type = unsigned_int64_type_kind;
3055 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3056 atomic_type = unsigned_int128_type_kind;
3059 case SPECIFIER_INT8:
3060 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3061 atomic_type = int8_type_kind;
3064 case SPECIFIER_INT16:
3065 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3066 atomic_type = int16_type_kind;
3069 case SPECIFIER_INT32:
3070 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3071 atomic_type = int32_type_kind;
3074 case SPECIFIER_INT64:
3075 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3076 atomic_type = int64_type_kind;
3079 case SPECIFIER_INT128:
3080 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3081 atomic_type = int128_type_kind;
3084 case SPECIFIER_FLOAT:
3085 atomic_type = ATOMIC_TYPE_FLOAT;
3087 case SPECIFIER_DOUBLE:
3088 atomic_type = ATOMIC_TYPE_DOUBLE;
3090 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3091 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3093 case SPECIFIER_BOOL:
3094 atomic_type = ATOMIC_TYPE_BOOL;
3096 #ifdef PROVIDE_COMPLEX
3097 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3098 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
3100 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3101 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
3103 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3104 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
3106 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3107 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
3109 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3110 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
3112 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3113 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
3117 /* invalid specifier combination, give an error message */
3118 if(type_specifiers == 0) {
3119 if (! strict_mode) {
3120 if (warning.implicit_int) {
3121 warningf(HERE, "no type specifiers in declaration, using 'int'");
3123 atomic_type = ATOMIC_TYPE_INT;
3126 errorf(HERE, "no type specifiers given in declaration");
3128 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3129 (type_specifiers & SPECIFIER_UNSIGNED)) {
3130 errorf(HERE, "signed and unsigned specifiers gives");
3131 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3132 errorf(HERE, "only integer types can be signed or unsigned");
3134 errorf(HERE, "multiple datatypes in declaration");
3136 atomic_type = ATOMIC_TYPE_INVALID;
3139 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3140 type->atomic.akind = atomic_type;
3143 if(type_specifiers != 0) {
3144 errorf(HERE, "multiple datatypes in declaration");
3148 type->base.qualifiers = type_qualifiers;
3149 /* FIXME: check type qualifiers here */
3151 type_t *result = typehash_insert(type);
3152 if(newtype && result != type) {
3156 specifiers->type = result;
3161 static type_qualifiers_t parse_type_qualifiers(void)
3163 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3166 switch(token.type) {
3167 /* type qualifiers */
3168 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3169 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3170 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3171 /* microsoft extended type modifiers */
3172 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3173 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3174 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3175 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3176 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3179 return type_qualifiers;
3184 static declaration_t *parse_identifier_list(void)
3186 declaration_t *declarations = NULL;
3187 declaration_t *last_declaration = NULL;
3189 declaration_t *const declaration = allocate_declaration_zero();
3190 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3191 declaration->source_position = token.source_position;
3192 declaration->symbol = token.v.symbol;
3195 if(last_declaration != NULL) {
3196 last_declaration->next = declaration;
3198 declarations = declaration;
3200 last_declaration = declaration;
3202 if(token.type != ',')
3205 } while(token.type == T_IDENTIFIER);
3207 return declarations;
3210 static void semantic_parameter(declaration_t *declaration)
3212 /* TODO: improve error messages */
3214 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3215 errorf(HERE, "typedef not allowed in parameter list");
3216 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3217 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3218 errorf(HERE, "parameter may only have none or register storage class");
3221 type_t *const orig_type = declaration->type;
3222 type_t * type = skip_typeref(orig_type);
3224 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3225 * into a pointer. § 6.7.5.3 (7) */
3226 if (is_type_array(type)) {
3227 type_t *const element_type = type->array.element_type;
3229 type = make_pointer_type(element_type, type->base.qualifiers);
3231 declaration->type = type;
3234 if(is_type_incomplete(type)) {
3235 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3236 orig_type, declaration->symbol);
3240 static declaration_t *parse_parameter(void)
3242 declaration_specifiers_t specifiers;
3243 memset(&specifiers, 0, sizeof(specifiers));
3245 parse_declaration_specifiers(&specifiers);
3247 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3249 semantic_parameter(declaration);
3254 static declaration_t *parse_parameters(function_type_t *type)
3256 if(token.type == T_IDENTIFIER) {
3257 symbol_t *symbol = token.v.symbol;
3258 if(!is_typedef_symbol(symbol)) {
3259 type->kr_style_parameters = true;
3260 return parse_identifier_list();
3264 if(token.type == ')') {
3265 type->unspecified_parameters = 1;
3268 if(token.type == T_void && look_ahead(1)->type == ')') {
3273 declaration_t *declarations = NULL;
3274 declaration_t *declaration;
3275 declaration_t *last_declaration = NULL;
3276 function_parameter_t *parameter;
3277 function_parameter_t *last_parameter = NULL;
3280 switch(token.type) {
3284 return declarations;
3287 case T___extension__:
3289 declaration = parse_parameter();
3291 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3292 memset(parameter, 0, sizeof(parameter[0]));
3293 parameter->type = declaration->type;
3295 if(last_parameter != NULL) {
3296 last_declaration->next = declaration;
3297 last_parameter->next = parameter;
3299 type->parameters = parameter;
3300 declarations = declaration;
3302 last_parameter = parameter;
3303 last_declaration = declaration;
3307 return declarations;
3309 if(token.type != ',')
3310 return declarations;
3320 } construct_type_kind_t;
3322 typedef struct construct_type_t construct_type_t;
3323 struct construct_type_t {
3324 construct_type_kind_t kind;
3325 construct_type_t *next;
3328 typedef struct parsed_pointer_t parsed_pointer_t;
3329 struct parsed_pointer_t {
3330 construct_type_t construct_type;
3331 type_qualifiers_t type_qualifiers;
3334 typedef struct construct_function_type_t construct_function_type_t;
3335 struct construct_function_type_t {
3336 construct_type_t construct_type;
3337 type_t *function_type;
3340 typedef struct parsed_array_t parsed_array_t;
3341 struct parsed_array_t {
3342 construct_type_t construct_type;
3343 type_qualifiers_t type_qualifiers;
3349 typedef struct construct_base_type_t construct_base_type_t;
3350 struct construct_base_type_t {
3351 construct_type_t construct_type;
3355 static construct_type_t *parse_pointer_declarator(void)
3359 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3360 memset(pointer, 0, sizeof(pointer[0]));
3361 pointer->construct_type.kind = CONSTRUCT_POINTER;
3362 pointer->type_qualifiers = parse_type_qualifiers();
3364 return (construct_type_t*) pointer;
3367 static construct_type_t *parse_array_declarator(void)
3370 add_anchor_token(']');
3372 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3373 memset(array, 0, sizeof(array[0]));
3374 array->construct_type.kind = CONSTRUCT_ARRAY;
3376 if(token.type == T_static) {
3377 array->is_static = true;
3381 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3382 if(type_qualifiers != 0) {
3383 if(token.type == T_static) {
3384 array->is_static = true;
3388 array->type_qualifiers = type_qualifiers;
3390 if(token.type == '*' && look_ahead(1)->type == ']') {
3391 array->is_variable = true;
3393 } else if(token.type != ']') {
3394 array->size = parse_assignment_expression();
3397 rem_anchor_token(']');
3400 return (construct_type_t*) array;
3405 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3408 add_anchor_token(')');
3411 if(declaration != NULL) {
3412 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3414 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3417 declaration_t *parameters = parse_parameters(&type->function);
3418 if(declaration != NULL) {
3419 declaration->scope.declarations = parameters;
3422 construct_function_type_t *construct_function_type =
3423 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3424 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3425 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3426 construct_function_type->function_type = type;
3428 rem_anchor_token(')');
3432 return (construct_type_t*) construct_function_type;
3435 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3436 bool may_be_abstract)
3438 /* construct a single linked list of construct_type_t's which describe
3439 * how to construct the final declarator type */
3440 construct_type_t *first = NULL;
3441 construct_type_t *last = NULL;
3442 gnu_attribute_t *attributes = NULL;
3445 while(token.type == '*') {
3446 construct_type_t *type = parse_pointer_declarator();
3457 /* TODO: find out if this is correct */
3458 parse_attributes(&attributes);
3460 construct_type_t *inner_types = NULL;
3462 switch(token.type) {
3464 if(declaration == NULL) {
3465 errorf(HERE, "no identifier expected in typename");
3467 declaration->symbol = token.v.symbol;
3468 declaration->source_position = token.source_position;
3474 add_anchor_token(')');
3475 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3476 rem_anchor_token(')');
3482 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
3483 /* avoid a loop in the outermost scope, because eat_statement doesn't
3485 if(token.type == '}' && current_function == NULL) {
3493 construct_type_t *p = last;
3496 construct_type_t *type;
3497 switch(token.type) {
3499 type = parse_function_declarator(declaration);
3502 type = parse_array_declarator();
3505 goto declarator_finished;
3508 /* insert in the middle of the list (behind p) */
3510 type->next = p->next;
3521 declarator_finished:
3522 parse_attributes(&attributes);
3524 /* append inner_types at the end of the list, we don't to set last anymore
3525 * as it's not needed anymore */
3527 assert(first == NULL);
3528 first = inner_types;
3530 last->next = inner_types;
3538 static type_t *construct_declarator_type(construct_type_t *construct_list,
3541 construct_type_t *iter = construct_list;
3542 for( ; iter != NULL; iter = iter->next) {
3543 switch(iter->kind) {
3544 case CONSTRUCT_INVALID:
3545 internal_errorf(HERE, "invalid type construction found");
3546 case CONSTRUCT_FUNCTION: {
3547 construct_function_type_t *construct_function_type
3548 = (construct_function_type_t*) iter;
3550 type_t *function_type = construct_function_type->function_type;
3552 function_type->function.return_type = type;
3554 type_t *skipped_return_type = skip_typeref(type);
3555 if (is_type_function(skipped_return_type)) {
3556 errorf(HERE, "function returning function is not allowed");
3557 type = type_error_type;
3558 } else if (is_type_array(skipped_return_type)) {
3559 errorf(HERE, "function returning array is not allowed");
3560 type = type_error_type;
3562 type = function_type;
3567 case CONSTRUCT_POINTER: {
3568 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3569 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3570 pointer_type->pointer.points_to = type;
3571 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3573 type = pointer_type;
3577 case CONSTRUCT_ARRAY: {
3578 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3579 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3581 expression_t *size_expression = parsed_array->size;
3582 if(size_expression != NULL) {
3584 = create_implicit_cast(size_expression, type_size_t);
3587 array_type->base.qualifiers = parsed_array->type_qualifiers;
3588 array_type->array.element_type = type;
3589 array_type->array.is_static = parsed_array->is_static;
3590 array_type->array.is_variable = parsed_array->is_variable;
3591 array_type->array.size_expression = size_expression;
3593 if(size_expression != NULL) {
3594 if(is_constant_expression(size_expression)) {
3595 array_type->array.size_constant = true;
3596 array_type->array.size
3597 = fold_constant(size_expression);
3599 array_type->array.is_vla = true;
3603 type_t *skipped_type = skip_typeref(type);
3604 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3605 errorf(HERE, "array of void is not allowed");
3606 type = type_error_type;
3614 type_t *hashed_type = typehash_insert(type);
3615 if(hashed_type != type) {
3616 /* the function type was constructed earlier freeing it here will
3617 * destroy other types... */
3618 if(iter->kind != CONSTRUCT_FUNCTION) {
3628 static declaration_t *parse_declarator(
3629 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3631 declaration_t *const declaration = allocate_declaration_zero();
3632 declaration->declared_storage_class = specifiers->declared_storage_class;
3633 declaration->modifiers = specifiers->decl_modifiers;
3634 declaration->deprecated = specifiers->deprecated;
3635 declaration->deprecated_string = specifiers->deprecated_string;
3636 declaration->get_property_sym = specifiers->get_property_sym;
3637 declaration->put_property_sym = specifiers->put_property_sym;
3638 declaration->is_inline = specifiers->is_inline;
3640 declaration->storage_class = specifiers->declared_storage_class;
3641 if(declaration->storage_class == STORAGE_CLASS_NONE
3642 && scope != global_scope) {
3643 declaration->storage_class = STORAGE_CLASS_AUTO;
3646 if(specifiers->alignment != 0) {
3647 /* TODO: add checks here */
3648 declaration->alignment = specifiers->alignment;
3651 construct_type_t *construct_type
3652 = parse_inner_declarator(declaration, may_be_abstract);
3653 type_t *const type = specifiers->type;
3654 declaration->type = construct_declarator_type(construct_type, type);
3656 if(construct_type != NULL) {
3657 obstack_free(&temp_obst, construct_type);
3663 static type_t *parse_abstract_declarator(type_t *base_type)
3665 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3667 type_t *result = construct_declarator_type(construct_type, base_type);
3668 if(construct_type != NULL) {
3669 obstack_free(&temp_obst, construct_type);
3675 static declaration_t *append_declaration(declaration_t* const declaration)
3677 if (last_declaration != NULL) {
3678 last_declaration->next = declaration;
3680 scope->declarations = declaration;
3682 last_declaration = declaration;
3687 * Check if the declaration of main is suspicious. main should be a
3688 * function with external linkage, returning int, taking either zero
3689 * arguments, two, or three arguments of appropriate types, ie.
3691 * int main([ int argc, char **argv [, char **env ] ]).
3693 * @param decl the declaration to check
3694 * @param type the function type of the declaration
3696 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3698 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3699 warningf(&decl->source_position,
3700 "'main' is normally a non-static function");
3702 if (skip_typeref(func_type->return_type) != type_int) {
3703 warningf(&decl->source_position,
3704 "return type of 'main' should be 'int', but is '%T'",
3705 func_type->return_type);
3707 const function_parameter_t *parm = func_type->parameters;
3709 type_t *const first_type = parm->type;
3710 if (!types_compatible(skip_typeref(first_type), type_int)) {
3711 warningf(&decl->source_position,
3712 "first argument of 'main' should be 'int', but is '%T'", first_type);
3716 type_t *const second_type = parm->type;
3717 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3718 warningf(&decl->source_position,
3719 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3723 type_t *const third_type = parm->type;
3724 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3725 warningf(&decl->source_position,
3726 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3730 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3734 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3740 * Check if a symbol is the equal to "main".
3742 static bool is_sym_main(const symbol_t *const sym)
3744 return strcmp(sym->string, "main") == 0;
3747 static declaration_t *internal_record_declaration(
3748 declaration_t *const declaration,
3749 const bool is_function_definition)
3751 const symbol_t *const symbol = declaration->symbol;
3752 const namespace_t namespc = (namespace_t)declaration->namespc;
3754 type_t *const orig_type = declaration->type;
3755 type_t *const type = skip_typeref(orig_type);
3756 if (is_type_function(type) &&
3757 type->function.unspecified_parameters &&
3758 warning.strict_prototypes) {
3759 warningf(&declaration->source_position,
3760 "function declaration '%#T' is not a prototype",
3761 orig_type, declaration->symbol);
3764 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3765 check_type_of_main(declaration, &type->function);
3768 assert(declaration->symbol != NULL);
3769 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3771 assert(declaration != previous_declaration);
3772 if (previous_declaration != NULL) {
3773 if (previous_declaration->parent_scope == scope) {
3774 /* can happen for K&R style declarations */
3775 if(previous_declaration->type == NULL) {
3776 previous_declaration->type = declaration->type;
3779 const type_t *prev_type = skip_typeref(previous_declaration->type);
3780 if (!types_compatible(type, prev_type)) {
3781 errorf(&declaration->source_position,
3782 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3783 orig_type, symbol, previous_declaration->type, symbol,
3784 &previous_declaration->source_position);
3786 unsigned old_storage_class = previous_declaration->storage_class;
3787 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3788 errorf(&declaration->source_position,
3789 "redeclaration of enum entry '%Y' (declared %P)",
3790 symbol, &previous_declaration->source_position);
3791 return previous_declaration;
3794 unsigned new_storage_class = declaration->storage_class;
3796 if(is_type_incomplete(prev_type)) {
3797 previous_declaration->type = type;
3801 /* pretend no storage class means extern for function
3802 * declarations (except if the previous declaration is neither
3803 * none nor extern) */
3804 if (is_type_function(type)) {
3805 switch (old_storage_class) {
3806 case STORAGE_CLASS_NONE:
3807 old_storage_class = STORAGE_CLASS_EXTERN;
3809 case STORAGE_CLASS_EXTERN:
3810 if (is_function_definition) {
3811 if (warning.missing_prototypes &&
3812 prev_type->function.unspecified_parameters &&
3813 !is_sym_main(symbol)) {
3814 warningf(&declaration->source_position,
3815 "no previous prototype for '%#T'",
3818 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3819 new_storage_class = STORAGE_CLASS_EXTERN;
3827 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3828 new_storage_class == STORAGE_CLASS_EXTERN) {
3829 warn_redundant_declaration:
3830 if (warning.redundant_decls) {
3831 warningf(&declaration->source_position,
3832 "redundant declaration for '%Y' (declared %P)",
3833 symbol, &previous_declaration->source_position);
3835 } else if (current_function == NULL) {
3836 if (old_storage_class != STORAGE_CLASS_STATIC &&
3837 new_storage_class == STORAGE_CLASS_STATIC) {
3838 errorf(&declaration->source_position,
3839 "static declaration of '%Y' follows non-static declaration (declared %P)",
3840 symbol, &previous_declaration->source_position);
3842 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3843 goto warn_redundant_declaration;
3845 if (new_storage_class == STORAGE_CLASS_NONE) {
3846 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3847 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3851 if (old_storage_class == new_storage_class) {
3852 errorf(&declaration->source_position,
3853 "redeclaration of '%Y' (declared %P)",
3854 symbol, &previous_declaration->source_position);
3856 errorf(&declaration->source_position,
3857 "redeclaration of '%Y' with different linkage (declared %P)",
3858 symbol, &previous_declaration->source_position);
3862 return previous_declaration;
3864 } else if (is_function_definition) {
3865 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3866 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3867 warningf(&declaration->source_position,
3868 "no previous prototype for '%#T'", orig_type, symbol);
3869 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3870 warningf(&declaration->source_position,
3871 "no previous declaration for '%#T'", orig_type,
3875 } else if (warning.missing_declarations &&
3876 scope == global_scope &&
3877 !is_type_function(type) && (
3878 declaration->storage_class == STORAGE_CLASS_NONE ||
3879 declaration->storage_class == STORAGE_CLASS_THREAD
3881 warningf(&declaration->source_position,
3882 "no previous declaration for '%#T'", orig_type, symbol);
3885 assert(declaration->parent_scope == NULL);
3886 assert(scope != NULL);
3888 declaration->parent_scope = scope;
3890 environment_push(declaration);
3891 return append_declaration(declaration);
3894 static declaration_t *record_declaration(declaration_t *declaration)
3896 return internal_record_declaration(declaration, false);
3899 static declaration_t *record_function_definition(declaration_t *declaration)
3901 return internal_record_declaration(declaration, true);
3904 static void parser_error_multiple_definition(declaration_t *declaration,
3905 const source_position_t *source_position)
3907 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3908 declaration->symbol, &declaration->source_position);
3911 static bool is_declaration_specifier(const token_t *token,
3912 bool only_type_specifiers)
3914 switch(token->type) {
3918 return is_typedef_symbol(token->v.symbol);
3920 case T___extension__:
3923 return !only_type_specifiers;
3930 static void parse_init_declarator_rest(declaration_t *declaration)
3934 type_t *orig_type = declaration->type;
3935 type_t *type = skip_typeref(orig_type);
3937 if(declaration->init.initializer != NULL) {
3938 parser_error_multiple_definition(declaration, HERE);
3941 bool must_be_constant = false;
3942 if(declaration->storage_class == STORAGE_CLASS_STATIC
3943 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3944 || declaration->parent_scope == global_scope) {
3945 must_be_constant = true;
3948 parse_initializer_env_t env;
3949 env.type = orig_type;
3950 env.must_be_constant = must_be_constant;
3951 env.declaration = declaration;
3953 initializer_t *initializer = parse_initializer(&env);
3955 if(env.type != orig_type) {
3956 orig_type = env.type;
3957 type = skip_typeref(orig_type);
3958 declaration->type = env.type;
3961 if(is_type_function(type)) {
3962 errorf(&declaration->source_position,
3963 "initializers not allowed for function types at declator '%Y' (type '%T')",
3964 declaration->symbol, orig_type);
3966 declaration->init.initializer = initializer;
3970 /* parse rest of a declaration without any declarator */
3971 static void parse_anonymous_declaration_rest(
3972 const declaration_specifiers_t *specifiers,
3973 parsed_declaration_func finished_declaration)
3977 declaration_t *const declaration = allocate_declaration_zero();
3978 declaration->type = specifiers->type;
3979 declaration->declared_storage_class = specifiers->declared_storage_class;
3980 declaration->source_position = specifiers->source_position;
3981 declaration->modifiers = specifiers->decl_modifiers;
3983 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3984 warningf(&declaration->source_position,
3985 "useless storage class in empty declaration");
3987 declaration->storage_class = STORAGE_CLASS_NONE;
3989 type_t *type = declaration->type;
3990 switch (type->kind) {
3991 case TYPE_COMPOUND_STRUCT:
3992 case TYPE_COMPOUND_UNION: {
3993 if (type->compound.declaration->symbol == NULL) {
3994 warningf(&declaration->source_position,
3995 "unnamed struct/union that defines no instances");
4004 warningf(&declaration->source_position, "empty declaration");
4008 finished_declaration(declaration);
4011 static void parse_declaration_rest(declaration_t *ndeclaration,
4012 const declaration_specifiers_t *specifiers,
4013 parsed_declaration_func finished_declaration)
4015 add_anchor_token(';');
4016 add_anchor_token('=');
4017 add_anchor_token(',');
4019 declaration_t *declaration = finished_declaration(ndeclaration);
4021 type_t *orig_type = declaration->type;
4022 type_t *type = skip_typeref(orig_type);
4024 if (type->kind != TYPE_FUNCTION &&
4025 declaration->is_inline &&
4026 is_type_valid(type)) {
4027 warningf(&declaration->source_position,
4028 "variable '%Y' declared 'inline'\n", declaration->symbol);
4031 if(token.type == '=') {
4032 parse_init_declarator_rest(declaration);
4035 if(token.type != ',')
4039 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4044 rem_anchor_token(';');
4045 rem_anchor_token('=');
4046 rem_anchor_token(',');
4049 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4051 symbol_t *symbol = declaration->symbol;
4052 if(symbol == NULL) {
4053 errorf(HERE, "anonymous declaration not valid as function parameter");
4056 namespace_t namespc = (namespace_t) declaration->namespc;
4057 if(namespc != NAMESPACE_NORMAL) {
4058 return record_declaration(declaration);
4061 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4062 if(previous_declaration == NULL ||
4063 previous_declaration->parent_scope != scope) {
4064 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4069 if(previous_declaration->type == NULL) {
4070 previous_declaration->type = declaration->type;
4071 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4072 previous_declaration->storage_class = declaration->storage_class;
4073 previous_declaration->parent_scope = scope;
4074 return previous_declaration;
4076 return record_declaration(declaration);
4080 static void parse_declaration(parsed_declaration_func finished_declaration)
4082 declaration_specifiers_t specifiers;
4083 memset(&specifiers, 0, sizeof(specifiers));
4084 parse_declaration_specifiers(&specifiers);
4086 if(token.type == ';') {
4087 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4089 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4090 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4094 static void parse_kr_declaration_list(declaration_t *declaration)
4096 type_t *type = skip_typeref(declaration->type);
4097 if(!is_type_function(type))
4100 if(!type->function.kr_style_parameters)
4103 /* push function parameters */
4104 int top = environment_top();
4105 scope_t *last_scope = scope;
4106 set_scope(&declaration->scope);
4108 declaration_t *parameter = declaration->scope.declarations;
4109 for( ; parameter != NULL; parameter = parameter->next) {
4110 assert(parameter->parent_scope == NULL);
4111 parameter->parent_scope = scope;
4112 environment_push(parameter);
4115 /* parse declaration list */
4116 while(is_declaration_specifier(&token, false)) {
4117 parse_declaration(finished_kr_declaration);
4120 /* pop function parameters */
4121 assert(scope == &declaration->scope);
4122 set_scope(last_scope);
4123 environment_pop_to(top);
4125 /* update function type */
4126 type_t *new_type = duplicate_type(type);
4127 new_type->function.kr_style_parameters = false;
4129 function_parameter_t *parameters = NULL;
4130 function_parameter_t *last_parameter = NULL;
4132 declaration_t *parameter_declaration = declaration->scope.declarations;
4133 for( ; parameter_declaration != NULL;
4134 parameter_declaration = parameter_declaration->next) {
4135 type_t *parameter_type = parameter_declaration->type;
4136 if(parameter_type == NULL) {
4138 errorf(HERE, "no type specified for function parameter '%Y'",
4139 parameter_declaration->symbol);
4141 if (warning.implicit_int) {
4142 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4143 parameter_declaration->symbol);
4145 parameter_type = type_int;
4146 parameter_declaration->type = parameter_type;
4150 semantic_parameter(parameter_declaration);
4151 parameter_type = parameter_declaration->type;
4153 function_parameter_t *function_parameter
4154 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4155 memset(function_parameter, 0, sizeof(function_parameter[0]));
4157 function_parameter->type = parameter_type;
4158 if(last_parameter != NULL) {
4159 last_parameter->next = function_parameter;
4161 parameters = function_parameter;
4163 last_parameter = function_parameter;
4165 new_type->function.parameters = parameters;
4167 type = typehash_insert(new_type);
4168 if(type != new_type) {
4169 obstack_free(type_obst, new_type);
4172 declaration->type = type;
4175 static bool first_err = true;
4178 * When called with first_err set, prints the name of the current function,
4181 static void print_in_function(void) {
4184 diagnosticf("%s: In function '%Y':\n",
4185 current_function->source_position.input_name,
4186 current_function->symbol);
4191 * Check if all labels are defined in the current function.
4192 * Check if all labels are used in the current function.
4194 static void check_labels(void)
4196 for (const goto_statement_t *goto_statement = goto_first;
4197 goto_statement != NULL;
4198 goto_statement = goto_statement->next) {
4199 declaration_t *label = goto_statement->label;
4202 if (label->source_position.input_name == NULL) {
4203 print_in_function();
4204 errorf(&goto_statement->base.source_position,
4205 "label '%Y' used but not defined", label->symbol);
4208 goto_first = goto_last = NULL;
4210 if (warning.unused_label) {
4211 for (const label_statement_t *label_statement = label_first;
4212 label_statement != NULL;
4213 label_statement = label_statement->next) {
4214 const declaration_t *label = label_statement->label;
4216 if (! label->used) {
4217 print_in_function();
4218 warningf(&label_statement->base.source_position,
4219 "label '%Y' defined but not used", label->symbol);
4223 label_first = label_last = NULL;
4227 * Check declarations of current_function for unused entities.
4229 static void check_declarations(void)
4231 if (warning.unused_parameter) {
4232 const scope_t *scope = ¤t_function->scope;
4234 const declaration_t *parameter = scope->declarations;
4235 for (; parameter != NULL; parameter = parameter->next) {
4236 if (! parameter->used) {
4237 print_in_function();
4238 warningf(¶meter->source_position,
4239 "unused parameter '%Y'", parameter->symbol);
4243 if (warning.unused_variable) {
4247 static void parse_external_declaration(void)
4249 /* function-definitions and declarations both start with declaration
4251 declaration_specifiers_t specifiers;
4252 memset(&specifiers, 0, sizeof(specifiers));
4254 add_anchor_token(';');
4255 parse_declaration_specifiers(&specifiers);
4256 rem_anchor_token(';');
4258 /* must be a declaration */
4259 if(token.type == ';') {
4260 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4264 add_anchor_token(',');
4265 add_anchor_token('=');
4266 rem_anchor_token(';');
4268 /* declarator is common to both function-definitions and declarations */
4269 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4271 rem_anchor_token(',');
4272 rem_anchor_token('=');
4273 rem_anchor_token(';');
4275 /* must be a declaration */
4276 if(token.type == ',' || token.type == '=' || token.type == ';') {
4277 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4281 /* must be a function definition */
4282 parse_kr_declaration_list(ndeclaration);
4284 if(token.type != '{') {
4285 parse_error_expected("while parsing function definition", '{', 0);
4286 eat_until_matching_token(';');
4290 type_t *type = ndeclaration->type;
4292 /* note that we don't skip typerefs: the standard doesn't allow them here
4293 * (so we can't use is_type_function here) */
4294 if(type->kind != TYPE_FUNCTION) {
4295 if (is_type_valid(type)) {
4296 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4297 type, ndeclaration->symbol);
4303 /* § 6.7.5.3 (14) a function definition with () means no
4304 * parameters (and not unspecified parameters) */
4305 if(type->function.unspecified_parameters) {
4306 type_t *duplicate = duplicate_type(type);
4307 duplicate->function.unspecified_parameters = false;
4309 type = typehash_insert(duplicate);
4310 if(type != duplicate) {
4311 obstack_free(type_obst, duplicate);
4313 ndeclaration->type = type;
4316 declaration_t *const declaration = record_function_definition(ndeclaration);
4317 if(ndeclaration != declaration) {
4318 declaration->scope = ndeclaration->scope;
4320 type = skip_typeref(declaration->type);
4322 /* push function parameters and switch scope */
4323 int top = environment_top();
4324 scope_t *last_scope = scope;
4325 set_scope(&declaration->scope);
4327 declaration_t *parameter = declaration->scope.declarations;
4328 for( ; parameter != NULL; parameter = parameter->next) {
4329 if(parameter->parent_scope == &ndeclaration->scope) {
4330 parameter->parent_scope = scope;
4332 assert(parameter->parent_scope == NULL
4333 || parameter->parent_scope == scope);
4334 parameter->parent_scope = scope;
4335 environment_push(parameter);
4338 if(declaration->init.statement != NULL) {
4339 parser_error_multiple_definition(declaration, HERE);
4341 goto end_of_parse_external_declaration;
4343 /* parse function body */
4344 int label_stack_top = label_top();
4345 declaration_t *old_current_function = current_function;
4346 current_function = declaration;
4348 declaration->init.statement = parse_compound_statement();
4351 check_declarations();
4353 assert(current_function == declaration);
4354 current_function = old_current_function;
4355 label_pop_to(label_stack_top);
4358 end_of_parse_external_declaration:
4359 assert(scope == &declaration->scope);
4360 set_scope(last_scope);
4361 environment_pop_to(top);
4364 static type_t *make_bitfield_type(type_t *base, expression_t *size,
4365 source_position_t *source_position)
4367 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4368 type->bitfield.base = base;
4369 type->bitfield.size = size;
4374 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4377 declaration_t *iter = compound_declaration->scope.declarations;
4378 for( ; iter != NULL; iter = iter->next) {
4379 if(iter->namespc != NAMESPACE_NORMAL)
4382 if(iter->symbol == NULL) {
4383 type_t *type = skip_typeref(iter->type);
4384 if(is_type_compound(type)) {
4385 declaration_t *result
4386 = find_compound_entry(type->compound.declaration, symbol);
4393 if(iter->symbol == symbol) {
4401 static void parse_compound_declarators(declaration_t *struct_declaration,
4402 const declaration_specifiers_t *specifiers)
4404 declaration_t *last_declaration = struct_declaration->scope.declarations;
4405 if(last_declaration != NULL) {
4406 while(last_declaration->next != NULL) {
4407 last_declaration = last_declaration->next;
4412 declaration_t *declaration;
4414 if(token.type == ':') {
4415 source_position_t source_position = *HERE;
4418 type_t *base_type = specifiers->type;
4419 expression_t *size = parse_constant_expression();
4421 if(!is_type_integer(skip_typeref(base_type))) {
4422 errorf(HERE, "bitfield base type '%T' is not an integer type",
4426 type_t *type = make_bitfield_type(base_type, size, &source_position);
4428 declaration = allocate_declaration_zero();
4429 declaration->namespc = NAMESPACE_NORMAL;
4430 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4431 declaration->storage_class = STORAGE_CLASS_NONE;
4432 declaration->source_position = source_position;
4433 declaration->modifiers = specifiers->decl_modifiers;
4434 declaration->type = type;
4436 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4438 type_t *orig_type = declaration->type;
4439 type_t *type = skip_typeref(orig_type);
4441 if(token.type == ':') {
4442 source_position_t source_position = *HERE;
4444 expression_t *size = parse_constant_expression();
4446 if(!is_type_integer(type)) {
4447 errorf(HERE, "bitfield base type '%T' is not an "
4448 "integer type", orig_type);
4451 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4452 declaration->type = bitfield_type;
4454 /* TODO we ignore arrays for now... what is missing is a check
4455 * that they're at the end of the struct */
4456 if(is_type_incomplete(type) && !is_type_array(type)) {
4458 "compound member '%Y' has incomplete type '%T'",
4459 declaration->symbol, orig_type);
4460 } else if(is_type_function(type)) {
4461 errorf(HERE, "compound member '%Y' must not have function "
4462 "type '%T'", declaration->symbol, orig_type);
4467 /* make sure we don't define a symbol multiple times */
4468 symbol_t *symbol = declaration->symbol;
4469 if(symbol != NULL) {
4470 declaration_t *prev_decl
4471 = find_compound_entry(struct_declaration, symbol);
4473 if(prev_decl != NULL) {
4474 assert(prev_decl->symbol == symbol);
4475 errorf(&declaration->source_position,
4476 "multiple declarations of symbol '%Y' (declared %P)",
4477 symbol, &prev_decl->source_position);
4481 /* append declaration */
4482 if(last_declaration != NULL) {
4483 last_declaration->next = declaration;
4485 struct_declaration->scope.declarations = declaration;
4487 last_declaration = declaration;
4489 if(token.type != ',')
4499 static void parse_compound_type_entries(declaration_t *compound_declaration)
4502 add_anchor_token('}');
4504 while(token.type != '}' && token.type != T_EOF) {
4505 declaration_specifiers_t specifiers;
4506 memset(&specifiers, 0, sizeof(specifiers));
4507 parse_declaration_specifiers(&specifiers);
4509 parse_compound_declarators(compound_declaration, &specifiers);
4511 rem_anchor_token('}');
4513 if(token.type == T_EOF) {
4514 errorf(HERE, "EOF while parsing struct");
4519 static type_t *parse_typename(void)
4521 declaration_specifiers_t specifiers;
4522 memset(&specifiers, 0, sizeof(specifiers));
4523 parse_declaration_specifiers(&specifiers);
4524 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4525 /* TODO: improve error message, user does probably not know what a
4526 * storage class is...
4528 errorf(HERE, "typename may not have a storage class");
4531 type_t *result = parse_abstract_declarator(specifiers.type);
4539 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4540 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4541 expression_t *left);
4543 typedef struct expression_parser_function_t expression_parser_function_t;
4544 struct expression_parser_function_t {
4545 unsigned precedence;
4546 parse_expression_function parser;
4547 unsigned infix_precedence;
4548 parse_expression_infix_function infix_parser;
4551 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4554 * Prints an error message if an expression was expected but not read
4556 static expression_t *expected_expression_error(void)
4558 /* skip the error message if the error token was read */
4559 if (token.type != T_ERROR) {
4560 errorf(HERE, "expected expression, got token '%K'", &token);
4564 return create_invalid_expression();
4568 * Parse a string constant.
4570 static expression_t *parse_string_const(void)
4573 if (token.type == T_STRING_LITERAL) {
4574 string_t res = token.v.string;
4576 while (token.type == T_STRING_LITERAL) {
4577 res = concat_strings(&res, &token.v.string);
4580 if (token.type != T_WIDE_STRING_LITERAL) {
4581 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4582 /* note: that we use type_char_ptr here, which is already the
4583 * automatic converted type. revert_automatic_type_conversion
4584 * will construct the array type */
4585 cnst->base.type = type_char_ptr;
4586 cnst->string.value = res;
4590 wres = concat_string_wide_string(&res, &token.v.wide_string);
4592 wres = token.v.wide_string;
4597 switch (token.type) {
4598 case T_WIDE_STRING_LITERAL:
4599 wres = concat_wide_strings(&wres, &token.v.wide_string);
4602 case T_STRING_LITERAL:
4603 wres = concat_wide_string_string(&wres, &token.v.string);
4607 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4608 cnst->base.type = type_wchar_t_ptr;
4609 cnst->wide_string.value = wres;
4618 * Parse an integer constant.
4620 static expression_t *parse_int_const(void)
4622 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4623 cnst->base.source_position = *HERE;
4624 cnst->base.type = token.datatype;
4625 cnst->conste.v.int_value = token.v.intvalue;
4633 * Parse a character constant.
4635 static expression_t *parse_character_constant(void)
4637 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4639 cnst->base.source_position = *HERE;
4640 cnst->base.type = token.datatype;
4641 cnst->conste.v.character = token.v.string;
4643 if (cnst->conste.v.character.size != 1) {
4644 if (warning.multichar && (c_mode & _GNUC)) {
4646 warningf(HERE, "multi-character character constant");
4648 errorf(HERE, "more than 1 characters in character constant");
4657 * Parse a wide character constant.
4659 static expression_t *parse_wide_character_constant(void)
4661 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4663 cnst->base.source_position = *HERE;
4664 cnst->base.type = token.datatype;
4665 cnst->conste.v.wide_character = token.v.wide_string;
4667 if (cnst->conste.v.wide_character.size != 1) {
4668 if (warning.multichar && (c_mode & _GNUC)) {
4670 warningf(HERE, "multi-character character constant");
4672 errorf(HERE, "more than 1 characters in character constant");
4681 * Parse a float constant.
4683 static expression_t *parse_float_const(void)
4685 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4686 cnst->base.type = token.datatype;
4687 cnst->conste.v.float_value = token.v.floatvalue;
4694 static declaration_t *create_implicit_function(symbol_t *symbol,
4695 const source_position_t *source_position)
4697 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4698 ntype->function.return_type = type_int;
4699 ntype->function.unspecified_parameters = true;
4701 type_t *type = typehash_insert(ntype);
4706 declaration_t *const declaration = allocate_declaration_zero();
4707 declaration->storage_class = STORAGE_CLASS_EXTERN;
4708 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4709 declaration->type = type;
4710 declaration->symbol = symbol;
4711 declaration->source_position = *source_position;
4712 declaration->parent_scope = global_scope;
4714 scope_t *old_scope = scope;
4715 set_scope(global_scope);
4717 environment_push(declaration);
4718 /* prepends the declaration to the global declarations list */
4719 declaration->next = scope->declarations;
4720 scope->declarations = declaration;
4722 assert(scope == global_scope);
4723 set_scope(old_scope);
4729 * Creates a return_type (func)(argument_type) function type if not
4732 * @param return_type the return type
4733 * @param argument_type the argument type
4735 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4737 function_parameter_t *parameter
4738 = obstack_alloc(type_obst, sizeof(parameter[0]));
4739 memset(parameter, 0, sizeof(parameter[0]));
4740 parameter->type = argument_type;
4742 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4743 type->function.return_type = return_type;
4744 type->function.parameters = parameter;
4746 type_t *result = typehash_insert(type);
4747 if(result != type) {
4755 * Creates a function type for some function like builtins.
4757 * @param symbol the symbol describing the builtin
4759 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4761 switch(symbol->ID) {
4762 case T___builtin_alloca:
4763 return make_function_1_type(type_void_ptr, type_size_t);
4764 case T___builtin_nan:
4765 return make_function_1_type(type_double, type_char_ptr);
4766 case T___builtin_nanf:
4767 return make_function_1_type(type_float, type_char_ptr);
4768 case T___builtin_nand:
4769 return make_function_1_type(type_long_double, type_char_ptr);
4770 case T___builtin_va_end:
4771 return make_function_1_type(type_void, type_valist);
4773 internal_errorf(HERE, "not implemented builtin symbol found");
4778 * Performs automatic type cast as described in § 6.3.2.1.
4780 * @param orig_type the original type
4782 static type_t *automatic_type_conversion(type_t *orig_type)
4784 type_t *type = skip_typeref(orig_type);
4785 if(is_type_array(type)) {
4786 array_type_t *array_type = &type->array;
4787 type_t *element_type = array_type->element_type;
4788 unsigned qualifiers = array_type->type.qualifiers;
4790 return make_pointer_type(element_type, qualifiers);
4793 if(is_type_function(type)) {
4794 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4801 * reverts the automatic casts of array to pointer types and function
4802 * to function-pointer types as defined § 6.3.2.1
4804 type_t *revert_automatic_type_conversion(const expression_t *expression)
4806 switch (expression->kind) {
4807 case EXPR_REFERENCE: return expression->reference.declaration->type;
4808 case EXPR_SELECT: return expression->select.compound_entry->type;
4810 case EXPR_UNARY_DEREFERENCE: {
4811 const expression_t *const value = expression->unary.value;
4812 type_t *const type = skip_typeref(value->base.type);
4813 assert(is_type_pointer(type));
4814 return type->pointer.points_to;
4817 case EXPR_BUILTIN_SYMBOL:
4818 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4820 case EXPR_ARRAY_ACCESS: {
4821 const expression_t *array_ref = expression->array_access.array_ref;
4822 type_t *type_left = skip_typeref(array_ref->base.type);
4823 if (!is_type_valid(type_left))
4825 assert(is_type_pointer(type_left));
4826 return type_left->pointer.points_to;
4829 case EXPR_STRING_LITERAL: {
4830 size_t size = expression->string.value.size;
4831 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4834 case EXPR_WIDE_STRING_LITERAL: {
4835 size_t size = expression->wide_string.value.size;
4836 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4839 case EXPR_COMPOUND_LITERAL:
4840 return expression->compound_literal.type;
4845 return expression->base.type;
4848 static expression_t *parse_reference(void)
4850 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4852 reference_expression_t *ref = &expression->reference;
4853 ref->symbol = token.v.symbol;
4855 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4857 source_position_t source_position = token.source_position;
4860 if(declaration == NULL) {
4861 if (! strict_mode && token.type == '(') {
4862 /* an implicitly defined function */
4863 if (warning.implicit_function_declaration) {
4864 warningf(HERE, "implicit declaration of function '%Y'",
4868 declaration = create_implicit_function(ref->symbol,
4871 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4872 return create_invalid_expression();
4876 type_t *type = declaration->type;
4878 /* we always do the auto-type conversions; the & and sizeof parser contains
4879 * code to revert this! */
4880 type = automatic_type_conversion(type);
4882 ref->declaration = declaration;
4883 ref->base.type = type;
4885 /* this declaration is used */
4886 declaration->used = true;
4888 /* check for deprecated functions */
4889 if(declaration->deprecated != 0) {
4890 const char *prefix = "";
4891 if (is_type_function(declaration->type))
4892 prefix = "function ";
4894 if (declaration->deprecated_string != NULL) {
4895 warningf(&source_position,
4896 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4897 declaration->deprecated_string);
4899 warningf(&source_position,
4900 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4907 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4911 /* TODO check if explicit cast is allowed and issue warnings/errors */
4914 static expression_t *parse_compound_literal(type_t *type)
4916 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4918 parse_initializer_env_t env;
4920 env.declaration = NULL;
4921 env.must_be_constant = false;
4922 initializer_t *initializer = parse_initializer(&env);
4925 expression->compound_literal.initializer = initializer;
4926 expression->compound_literal.type = type;
4927 expression->base.type = automatic_type_conversion(type);
4933 * Parse a cast expression.
4935 static expression_t *parse_cast(void)
4937 source_position_t source_position = token.source_position;
4939 type_t *type = parse_typename();
4941 /* matching add_anchor_token() is at call site */
4942 rem_anchor_token(')');
4945 if(token.type == '{') {
4946 return parse_compound_literal(type);
4949 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4950 cast->base.source_position = source_position;
4952 expression_t *value = parse_sub_expression(20);
4954 check_cast_allowed(value, type);
4956 cast->base.type = type;
4957 cast->unary.value = value;
4961 return create_invalid_expression();
4965 * Parse a statement expression.
4967 static expression_t *parse_statement_expression(void)
4969 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4971 statement_t *statement = parse_compound_statement();
4972 expression->statement.statement = statement;
4973 expression->base.source_position = statement->base.source_position;
4975 /* find last statement and use its type */
4976 type_t *type = type_void;
4977 const statement_t *stmt = statement->compound.statements;
4979 while (stmt->base.next != NULL)
4980 stmt = stmt->base.next;
4982 if (stmt->kind == STATEMENT_EXPRESSION) {
4983 type = stmt->expression.expression->base.type;
4986 warningf(&expression->base.source_position, "empty statement expression ({})");
4988 expression->base.type = type;
4994 return create_invalid_expression();
4998 * Parse a braced expression.
5000 static expression_t *parse_brace_expression(void)
5003 add_anchor_token(')');
5005 switch(token.type) {
5007 /* gcc extension: a statement expression */
5008 return parse_statement_expression();
5012 return parse_cast();
5014 if(is_typedef_symbol(token.v.symbol)) {
5015 return parse_cast();
5019 expression_t *result = parse_expression();
5020 rem_anchor_token(')');
5025 return create_invalid_expression();
5028 static expression_t *parse_function_keyword(void)
5033 if (current_function == NULL) {
5034 errorf(HERE, "'__func__' used outside of a function");
5037 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5038 expression->base.type = type_char_ptr;
5039 expression->funcname.kind = FUNCNAME_FUNCTION;
5044 static expression_t *parse_pretty_function_keyword(void)
5046 eat(T___PRETTY_FUNCTION__);
5048 if (current_function == NULL) {
5049 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5052 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5053 expression->base.type = type_char_ptr;
5054 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5059 static expression_t *parse_funcsig_keyword(void)
5063 if (current_function == NULL) {
5064 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5067 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5068 expression->base.type = type_char_ptr;
5069 expression->funcname.kind = FUNCNAME_FUNCSIG;
5074 static expression_t *parse_funcdname_keyword(void)
5076 eat(T___FUNCDNAME__);
5078 if (current_function == NULL) {
5079 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5082 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5083 expression->base.type = type_char_ptr;
5084 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5089 static designator_t *parse_designator(void)
5091 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5092 result->source_position = *HERE;
5094 if(token.type != T_IDENTIFIER) {
5095 parse_error_expected("while parsing member designator",
5099 result->symbol = token.v.symbol;
5102 designator_t *last_designator = result;
5104 if(token.type == '.') {
5106 if(token.type != T_IDENTIFIER) {
5107 parse_error_expected("while parsing member designator",
5111 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5112 designator->source_position = *HERE;
5113 designator->symbol = token.v.symbol;
5116 last_designator->next = designator;
5117 last_designator = designator;
5120 if(token.type == '[') {
5122 add_anchor_token(']');
5123 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5124 designator->source_position = *HERE;
5125 designator->array_index = parse_expression();
5126 rem_anchor_token(']');
5128 if(designator->array_index == NULL) {
5132 last_designator->next = designator;
5133 last_designator = designator;
5145 * Parse the __builtin_offsetof() expression.
5147 static expression_t *parse_offsetof(void)
5149 eat(T___builtin_offsetof);
5151 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5152 expression->base.type = type_size_t;
5155 add_anchor_token(',');
5156 type_t *type = parse_typename();
5157 rem_anchor_token(',');
5159 add_anchor_token(')');
5160 designator_t *designator = parse_designator();
5161 rem_anchor_token(')');
5164 expression->offsetofe.type = type;
5165 expression->offsetofe.designator = designator;
5168 memset(&path, 0, sizeof(path));
5169 path.top_type = type;
5170 path.path = NEW_ARR_F(type_path_entry_t, 0);
5172 descend_into_subtype(&path);
5174 if(!walk_designator(&path, designator, true)) {
5175 return create_invalid_expression();
5178 DEL_ARR_F(path.path);
5182 return create_invalid_expression();
5186 * Parses a _builtin_va_start() expression.
5188 static expression_t *parse_va_start(void)
5190 eat(T___builtin_va_start);
5192 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5195 add_anchor_token(',');
5196 expression->va_starte.ap = parse_assignment_expression();
5197 rem_anchor_token(',');
5199 expression_t *const expr = parse_assignment_expression();
5200 if (expr->kind == EXPR_REFERENCE) {
5201 declaration_t *const decl = expr->reference.declaration;
5203 return create_invalid_expression();
5204 if (decl->parent_scope == ¤t_function->scope &&
5205 decl->next == NULL) {
5206 expression->va_starte.parameter = decl;
5211 errorf(&expr->base.source_position,
5212 "second argument of 'va_start' must be last parameter of the current function");
5214 return create_invalid_expression();
5218 * Parses a _builtin_va_arg() expression.
5220 static expression_t *parse_va_arg(void)
5222 eat(T___builtin_va_arg);
5224 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5227 expression->va_arge.ap = parse_assignment_expression();
5229 expression->base.type = parse_typename();
5234 return create_invalid_expression();
5237 static expression_t *parse_builtin_symbol(void)
5239 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5241 symbol_t *symbol = token.v.symbol;
5243 expression->builtin_symbol.symbol = symbol;
5246 type_t *type = get_builtin_symbol_type(symbol);
5247 type = automatic_type_conversion(type);
5249 expression->base.type = type;
5254 * Parses a __builtin_constant() expression.
5256 static expression_t *parse_builtin_constant(void)
5258 eat(T___builtin_constant_p);
5260 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5263 add_anchor_token(')');
5264 expression->builtin_constant.value = parse_assignment_expression();
5265 rem_anchor_token(')');
5267 expression->base.type = type_int;
5271 return create_invalid_expression();
5275 * Parses a __builtin_prefetch() expression.
5277 static expression_t *parse_builtin_prefetch(void)
5279 eat(T___builtin_prefetch);
5281 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5284 add_anchor_token(')');
5285 expression->builtin_prefetch.adr = parse_assignment_expression();
5286 if (token.type == ',') {
5288 expression->builtin_prefetch.rw = parse_assignment_expression();
5290 if (token.type == ',') {
5292 expression->builtin_prefetch.locality = parse_assignment_expression();
5294 rem_anchor_token(')');
5296 expression->base.type = type_void;
5300 return create_invalid_expression();
5304 * Parses a __builtin_is_*() compare expression.
5306 static expression_t *parse_compare_builtin(void)
5308 expression_t *expression;
5310 switch(token.type) {
5311 case T___builtin_isgreater:
5312 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5314 case T___builtin_isgreaterequal:
5315 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5317 case T___builtin_isless:
5318 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5320 case T___builtin_islessequal:
5321 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5323 case T___builtin_islessgreater:
5324 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5326 case T___builtin_isunordered:
5327 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5330 internal_errorf(HERE, "invalid compare builtin found");
5333 expression->base.source_position = *HERE;
5337 expression->binary.left = parse_assignment_expression();
5339 expression->binary.right = parse_assignment_expression();
5342 type_t *const orig_type_left = expression->binary.left->base.type;
5343 type_t *const orig_type_right = expression->binary.right->base.type;
5345 type_t *const type_left = skip_typeref(orig_type_left);
5346 type_t *const type_right = skip_typeref(orig_type_right);
5347 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5348 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5349 type_error_incompatible("invalid operands in comparison",
5350 &expression->base.source_position, orig_type_left, orig_type_right);
5353 semantic_comparison(&expression->binary);
5358 return create_invalid_expression();
5362 * Parses a __builtin_expect() expression.
5364 static expression_t *parse_builtin_expect(void)
5366 eat(T___builtin_expect);
5368 expression_t *expression
5369 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5372 expression->binary.left = parse_assignment_expression();
5374 expression->binary.right = parse_constant_expression();
5377 expression->base.type = expression->binary.left->base.type;
5381 return create_invalid_expression();
5385 * Parses a MS assume() expression.
5387 static expression_t *parse_assume(void) {
5390 expression_t *expression
5391 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5394 add_anchor_token(')');
5395 expression->unary.value = parse_assignment_expression();
5396 rem_anchor_token(')');
5399 expression->base.type = type_void;
5402 return create_invalid_expression();
5406 * Parse a microsoft __noop expression.
5408 static expression_t *parse_noop_expression(void) {
5409 source_position_t source_position = *HERE;
5412 if (token.type == '(') {
5413 /* parse arguments */
5415 add_anchor_token(')');
5416 add_anchor_token(',');
5418 if(token.type != ')') {
5420 (void)parse_assignment_expression();
5421 if(token.type != ',')
5427 rem_anchor_token(',');
5428 rem_anchor_token(')');
5431 /* the result is a (int)0 */
5432 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5433 cnst->base.source_position = source_position;
5434 cnst->base.type = type_int;
5435 cnst->conste.v.int_value = 0;
5436 cnst->conste.is_ms_noop = true;
5441 return create_invalid_expression();
5445 * Parses a primary expression.
5447 static expression_t *parse_primary_expression(void)
5449 switch (token.type) {
5450 case T_INTEGER: return parse_int_const();
5451 case T_CHARACTER_CONSTANT: return parse_character_constant();
5452 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5453 case T_FLOATINGPOINT: return parse_float_const();
5454 case T_STRING_LITERAL:
5455 case T_WIDE_STRING_LITERAL: return parse_string_const();
5456 case T_IDENTIFIER: return parse_reference();
5457 case T___FUNCTION__:
5458 case T___func__: return parse_function_keyword();
5459 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5460 case T___FUNCSIG__: return parse_funcsig_keyword();
5461 case T___FUNCDNAME__: return parse_funcdname_keyword();
5462 case T___builtin_offsetof: return parse_offsetof();
5463 case T___builtin_va_start: return parse_va_start();
5464 case T___builtin_va_arg: return parse_va_arg();
5465 case T___builtin_expect: return parse_builtin_expect();
5466 case T___builtin_alloca:
5467 case T___builtin_nan:
5468 case T___builtin_nand:
5469 case T___builtin_nanf:
5470 case T___builtin_va_end: return parse_builtin_symbol();
5471 case T___builtin_isgreater:
5472 case T___builtin_isgreaterequal:
5473 case T___builtin_isless:
5474 case T___builtin_islessequal:
5475 case T___builtin_islessgreater:
5476 case T___builtin_isunordered: return parse_compare_builtin();
5477 case T___builtin_constant_p: return parse_builtin_constant();
5478 case T___builtin_prefetch: return parse_builtin_prefetch();
5479 case T__assume: return parse_assume();
5481 case '(': return parse_brace_expression();
5482 case T___noop: return parse_noop_expression();
5485 errorf(HERE, "unexpected token %K, expected an expression", &token);
5486 return create_invalid_expression();
5490 * Check if the expression has the character type and issue a warning then.
5492 static void check_for_char_index_type(const expression_t *expression) {
5493 type_t *const type = expression->base.type;
5494 const type_t *const base_type = skip_typeref(type);
5496 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5497 warning.char_subscripts) {
5498 warningf(&expression->base.source_position,
5499 "array subscript has type '%T'", type);
5503 static expression_t *parse_array_expression(unsigned precedence,
5509 add_anchor_token(']');
5511 expression_t *inside = parse_expression();
5513 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5515 array_access_expression_t *array_access = &expression->array_access;
5517 type_t *const orig_type_left = left->base.type;
5518 type_t *const orig_type_inside = inside->base.type;
5520 type_t *const type_left = skip_typeref(orig_type_left);
5521 type_t *const type_inside = skip_typeref(orig_type_inside);
5523 type_t *return_type;
5524 if (is_type_pointer(type_left)) {
5525 return_type = type_left->pointer.points_to;
5526 array_access->array_ref = left;
5527 array_access->index = inside;
5528 check_for_char_index_type(inside);
5529 } else if (is_type_pointer(type_inside)) {
5530 return_type = type_inside->pointer.points_to;
5531 array_access->array_ref = inside;
5532 array_access->index = left;
5533 array_access->flipped = true;
5534 check_for_char_index_type(left);
5536 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5538 "array access on object with non-pointer types '%T', '%T'",
5539 orig_type_left, orig_type_inside);
5541 return_type = type_error_type;
5542 array_access->array_ref = create_invalid_expression();
5545 rem_anchor_token(']');
5546 if(token.type != ']') {
5547 parse_error_expected("Problem while parsing array access", ']', 0);
5552 return_type = automatic_type_conversion(return_type);
5553 expression->base.type = return_type;
5558 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5560 expression_t *tp_expression = allocate_expression_zero(kind);
5561 tp_expression->base.type = type_size_t;
5563 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5565 add_anchor_token(')');
5566 tp_expression->typeprop.type = parse_typename();
5567 rem_anchor_token(')');
5570 expression_t *expression = parse_sub_expression(precedence);
5571 expression->base.type = revert_automatic_type_conversion(expression);
5573 tp_expression->typeprop.type = expression->base.type;
5574 tp_expression->typeprop.tp_expression = expression;
5577 return tp_expression;
5579 return create_invalid_expression();
5582 static expression_t *parse_sizeof(unsigned precedence)
5585 return parse_typeprop(EXPR_SIZEOF, precedence);
5588 static expression_t *parse_alignof(unsigned precedence)
5591 return parse_typeprop(EXPR_SIZEOF, precedence);
5594 static expression_t *parse_select_expression(unsigned precedence,
5595 expression_t *compound)
5598 assert(token.type == '.' || token.type == T_MINUSGREATER);
5600 bool is_pointer = (token.type == T_MINUSGREATER);
5603 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5604 select->select.compound = compound;
5606 if(token.type != T_IDENTIFIER) {
5607 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
5610 symbol_t *symbol = token.v.symbol;
5611 select->select.symbol = symbol;
5614 type_t *const orig_type = compound->base.type;
5615 type_t *const type = skip_typeref(orig_type);
5617 type_t *type_left = type;
5619 if (!is_type_pointer(type)) {
5620 if (is_type_valid(type)) {
5621 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5623 return create_invalid_expression();
5625 type_left = type->pointer.points_to;
5627 type_left = skip_typeref(type_left);
5629 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5630 type_left->kind != TYPE_COMPOUND_UNION) {
5631 if (is_type_valid(type_left)) {
5632 errorf(HERE, "request for member '%Y' in something not a struct or "
5633 "union, but '%T'", symbol, type_left);
5635 return create_invalid_expression();
5638 declaration_t *const declaration = type_left->compound.declaration;
5640 if(!declaration->init.is_defined) {
5641 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5643 return create_invalid_expression();
5646 declaration_t *iter = find_compound_entry(declaration, symbol);
5648 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5649 return create_invalid_expression();
5652 /* we always do the auto-type conversions; the & and sizeof parser contains
5653 * code to revert this! */
5654 type_t *expression_type = automatic_type_conversion(iter->type);
5656 select->select.compound_entry = iter;
5657 select->base.type = expression_type;
5659 if(expression_type->kind == TYPE_BITFIELD) {
5660 expression_t *extract
5661 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5662 extract->unary.value = select;
5663 extract->base.type = expression_type->bitfield.base;
5672 * Parse a call expression, ie. expression '( ... )'.
5674 * @param expression the function address
5676 static expression_t *parse_call_expression(unsigned precedence,
5677 expression_t *expression)
5680 expression_t *result = allocate_expression_zero(EXPR_CALL);
5681 result->base.source_position = expression->base.source_position;
5683 call_expression_t *call = &result->call;
5684 call->function = expression;
5686 type_t *const orig_type = expression->base.type;
5687 type_t *const type = skip_typeref(orig_type);
5689 function_type_t *function_type = NULL;
5690 if (is_type_pointer(type)) {
5691 type_t *const to_type = skip_typeref(type->pointer.points_to);
5693 if (is_type_function(to_type)) {
5694 function_type = &to_type->function;
5695 call->base.type = function_type->return_type;
5699 if (function_type == NULL && is_type_valid(type)) {
5700 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5703 /* parse arguments */
5705 add_anchor_token(')');
5706 add_anchor_token(',');
5708 if(token.type != ')') {
5709 call_argument_t *last_argument = NULL;
5712 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5714 argument->expression = parse_assignment_expression();
5715 if(last_argument == NULL) {
5716 call->arguments = argument;
5718 last_argument->next = argument;
5720 last_argument = argument;
5722 if(token.type != ',')
5727 rem_anchor_token(',');
5728 rem_anchor_token(')');
5731 if(function_type != NULL) {
5732 function_parameter_t *parameter = function_type->parameters;
5733 call_argument_t *argument = call->arguments;
5734 for( ; parameter != NULL && argument != NULL;
5735 parameter = parameter->next, argument = argument->next) {
5736 type_t *expected_type = parameter->type;
5737 /* TODO report scope in error messages */
5738 expression_t *const arg_expr = argument->expression;
5739 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5741 &arg_expr->base.source_position);
5742 if (res_type == NULL) {
5743 /* TODO improve error message */
5744 errorf(&arg_expr->base.source_position,
5745 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5746 arg_expr, arg_expr->base.type, expected_type);
5748 argument->expression = create_implicit_cast(argument->expression, expected_type);
5751 /* too few parameters */
5752 if(parameter != NULL) {
5753 errorf(HERE, "too few arguments to function '%E'", expression);
5754 } else if(argument != NULL) {
5755 /* too many parameters */
5756 if(!function_type->variadic
5757 && !function_type->unspecified_parameters) {
5758 errorf(HERE, "too many arguments to function '%E'", expression);
5760 /* do default promotion */
5761 for( ; argument != NULL; argument = argument->next) {
5762 type_t *type = argument->expression->base.type;
5764 type = skip_typeref(type);
5765 if(is_type_integer(type)) {
5766 type = promote_integer(type);
5767 } else if(type == type_float) {
5771 argument->expression
5772 = create_implicit_cast(argument->expression, type);
5775 check_format(&result->call);
5778 check_format(&result->call);
5784 return create_invalid_expression();
5787 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5789 static bool same_compound_type(const type_t *type1, const type_t *type2)
5792 is_type_compound(type1) &&
5793 type1->kind == type2->kind &&
5794 type1->compound.declaration == type2->compound.declaration;
5798 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5800 * @param expression the conditional expression
5802 static expression_t *parse_conditional_expression(unsigned precedence,
5803 expression_t *expression)
5806 add_anchor_token(':');
5808 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5810 conditional_expression_t *conditional = &result->conditional;
5811 conditional->condition = expression;
5814 type_t *const condition_type_orig = expression->base.type;
5815 type_t *const condition_type = skip_typeref(condition_type_orig);
5816 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5817 type_error("expected a scalar type in conditional condition",
5818 &expression->base.source_position, condition_type_orig);
5821 expression_t *true_expression = parse_expression();
5822 rem_anchor_token(':');
5824 expression_t *false_expression = parse_sub_expression(precedence);
5826 type_t *const orig_true_type = true_expression->base.type;
5827 type_t *const orig_false_type = false_expression->base.type;
5828 type_t *const true_type = skip_typeref(orig_true_type);
5829 type_t *const false_type = skip_typeref(orig_false_type);
5832 type_t *result_type;
5833 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5834 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5835 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5836 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5837 warningf(&expression->base.source_position,
5838 "ISO C forbids conditional expression with only one void side");
5840 result_type = type_void;
5841 } else if (is_type_arithmetic(true_type)
5842 && is_type_arithmetic(false_type)) {
5843 result_type = semantic_arithmetic(true_type, false_type);
5845 true_expression = create_implicit_cast(true_expression, result_type);
5846 false_expression = create_implicit_cast(false_expression, result_type);
5848 conditional->true_expression = true_expression;
5849 conditional->false_expression = false_expression;
5850 conditional->base.type = result_type;
5851 } else if (same_compound_type(true_type, false_type)) {
5852 /* just take 1 of the 2 types */
5853 result_type = true_type;
5854 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5855 type_t *pointer_type;
5857 expression_t *other_expression;
5858 if (is_type_pointer(true_type)) {
5859 pointer_type = true_type;
5860 other_type = false_type;
5861 other_expression = false_expression;
5863 pointer_type = false_type;
5864 other_type = true_type;
5865 other_expression = true_expression;
5868 if(is_type_pointer(other_type)) {
5869 if(!pointers_compatible(true_type, false_type)) {
5870 warningf(&expression->base.source_position,
5871 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5873 result_type = true_type;
5874 } else if(is_null_pointer_constant(other_expression)) {
5875 result_type = pointer_type;
5876 } else if(is_type_integer(other_type)) {
5877 warningf(&expression->base.source_position,
5878 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5879 result_type = pointer_type;
5881 type_error_incompatible("while parsing conditional",
5882 &expression->base.source_position, true_type, false_type);
5883 result_type = type_error_type;
5886 /* TODO: one pointer to void*, other some pointer */
5888 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5889 type_error_incompatible("while parsing conditional",
5890 &expression->base.source_position, true_type,
5893 result_type = type_error_type;
5896 conditional->true_expression
5897 = create_implicit_cast(true_expression, result_type);
5898 conditional->false_expression
5899 = create_implicit_cast(false_expression, result_type);
5900 conditional->base.type = result_type;
5903 return create_invalid_expression();
5907 * Parse an extension expression.
5909 static expression_t *parse_extension(unsigned precedence)
5911 eat(T___extension__);
5913 /* TODO enable extensions */
5914 expression_t *expression = parse_sub_expression(precedence);
5915 /* TODO disable extensions */
5920 * Parse a __builtin_classify_type() expression.
5922 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5924 eat(T___builtin_classify_type);
5926 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5927 result->base.type = type_int;
5930 add_anchor_token(')');
5931 expression_t *expression = parse_sub_expression(precedence);
5932 rem_anchor_token(')');
5934 result->classify_type.type_expression = expression;
5938 return create_invalid_expression();
5941 static void semantic_incdec(unary_expression_t *expression)
5943 type_t *const orig_type = expression->value->base.type;
5944 type_t *const type = skip_typeref(orig_type);
5945 /* TODO !is_type_real && !is_type_pointer */
5946 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5947 if (is_type_valid(type)) {
5948 /* TODO: improve error message */
5949 errorf(HERE, "operation needs an arithmetic or pointer type");
5954 expression->base.type = orig_type;
5957 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5959 type_t *const orig_type = expression->value->base.type;
5960 type_t *const type = skip_typeref(orig_type);
5961 if(!is_type_arithmetic(type)) {
5962 if (is_type_valid(type)) {
5963 /* TODO: improve error message */
5964 errorf(HERE, "operation needs an arithmetic type");
5969 expression->base.type = orig_type;
5972 static void semantic_unexpr_scalar(unary_expression_t *expression)
5974 type_t *const orig_type = expression->value->base.type;
5975 type_t *const type = skip_typeref(orig_type);
5976 if (!is_type_scalar(type)) {
5977 if (is_type_valid(type)) {
5978 errorf(HERE, "operand of ! must be of scalar type");
5983 expression->base.type = orig_type;
5986 static void semantic_unexpr_integer(unary_expression_t *expression)
5988 type_t *const orig_type = expression->value->base.type;
5989 type_t *const type = skip_typeref(orig_type);
5990 if (!is_type_integer(type)) {
5991 if (is_type_valid(type)) {
5992 errorf(HERE, "operand of ~ must be of integer type");
5997 expression->base.type = orig_type;
6000 static void semantic_dereference(unary_expression_t *expression)
6002 type_t *const orig_type = expression->value->base.type;
6003 type_t *const type = skip_typeref(orig_type);
6004 if(!is_type_pointer(type)) {
6005 if (is_type_valid(type)) {
6006 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6011 type_t *result_type = type->pointer.points_to;
6012 result_type = automatic_type_conversion(result_type);
6013 expression->base.type = result_type;
6017 * Check the semantic of the address taken expression.
6019 static void semantic_take_addr(unary_expression_t *expression)
6021 expression_t *value = expression->value;
6022 value->base.type = revert_automatic_type_conversion(value);
6024 type_t *orig_type = value->base.type;
6025 if(!is_type_valid(orig_type))
6028 if(value->kind == EXPR_REFERENCE) {
6029 declaration_t *const declaration = value->reference.declaration;
6030 if(declaration != NULL) {
6031 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6032 errorf(&expression->base.source_position,
6033 "address of register variable '%Y' requested",
6034 declaration->symbol);
6036 declaration->address_taken = 1;
6040 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6043 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6044 static expression_t *parse_##unexpression_type(unsigned precedence) \
6048 expression_t *unary_expression \
6049 = allocate_expression_zero(unexpression_type); \
6050 unary_expression->base.source_position = *HERE; \
6051 unary_expression->unary.value = parse_sub_expression(precedence); \
6053 sfunc(&unary_expression->unary); \
6055 return unary_expression; \
6058 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6059 semantic_unexpr_arithmetic)
6060 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6061 semantic_unexpr_arithmetic)
6062 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6063 semantic_unexpr_scalar)
6064 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6065 semantic_dereference)
6066 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6068 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6069 semantic_unexpr_integer)
6070 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6072 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6075 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6077 static expression_t *parse_##unexpression_type(unsigned precedence, \
6078 expression_t *left) \
6080 (void) precedence; \
6083 expression_t *unary_expression \
6084 = allocate_expression_zero(unexpression_type); \
6085 unary_expression->unary.value = left; \
6087 sfunc(&unary_expression->unary); \
6089 return unary_expression; \
6092 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6093 EXPR_UNARY_POSTFIX_INCREMENT,
6095 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6096 EXPR_UNARY_POSTFIX_DECREMENT,
6099 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6101 /* TODO: handle complex + imaginary types */
6103 /* § 6.3.1.8 Usual arithmetic conversions */
6104 if(type_left == type_long_double || type_right == type_long_double) {
6105 return type_long_double;
6106 } else if(type_left == type_double || type_right == type_double) {
6108 } else if(type_left == type_float || type_right == type_float) {
6112 type_right = promote_integer(type_right);
6113 type_left = promote_integer(type_left);
6115 if(type_left == type_right)
6118 bool signed_left = is_type_signed(type_left);
6119 bool signed_right = is_type_signed(type_right);
6120 int rank_left = get_rank(type_left);
6121 int rank_right = get_rank(type_right);
6122 if(rank_left < rank_right) {
6123 if(signed_left == signed_right || !signed_right) {
6129 if(signed_left == signed_right || !signed_left) {
6138 * Check the semantic restrictions for a binary expression.
6140 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6142 expression_t *const left = expression->left;
6143 expression_t *const right = expression->right;
6144 type_t *const orig_type_left = left->base.type;
6145 type_t *const orig_type_right = right->base.type;
6146 type_t *const type_left = skip_typeref(orig_type_left);
6147 type_t *const type_right = skip_typeref(orig_type_right);
6149 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6150 /* TODO: improve error message */
6151 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6152 errorf(HERE, "operation needs arithmetic types");
6157 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6158 expression->left = create_implicit_cast(left, arithmetic_type);
6159 expression->right = create_implicit_cast(right, arithmetic_type);
6160 expression->base.type = arithmetic_type;
6163 static void semantic_shift_op(binary_expression_t *expression)
6165 expression_t *const left = expression->left;
6166 expression_t *const right = expression->right;
6167 type_t *const orig_type_left = left->base.type;
6168 type_t *const orig_type_right = right->base.type;
6169 type_t * type_left = skip_typeref(orig_type_left);
6170 type_t * type_right = skip_typeref(orig_type_right);
6172 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6173 /* TODO: improve error message */
6174 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6175 errorf(HERE, "operation needs integer types");
6180 type_left = promote_integer(type_left);
6181 type_right = promote_integer(type_right);
6183 expression->left = create_implicit_cast(left, type_left);
6184 expression->right = create_implicit_cast(right, type_right);
6185 expression->base.type = type_left;
6188 static void semantic_add(binary_expression_t *expression)
6190 expression_t *const left = expression->left;
6191 expression_t *const right = expression->right;
6192 type_t *const orig_type_left = left->base.type;
6193 type_t *const orig_type_right = right->base.type;
6194 type_t *const type_left = skip_typeref(orig_type_left);
6195 type_t *const type_right = skip_typeref(orig_type_right);
6198 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6199 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6200 expression->left = create_implicit_cast(left, arithmetic_type);
6201 expression->right = create_implicit_cast(right, arithmetic_type);
6202 expression->base.type = arithmetic_type;
6204 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6205 expression->base.type = type_left;
6206 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6207 expression->base.type = type_right;
6208 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6209 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6213 static void semantic_sub(binary_expression_t *expression)
6215 expression_t *const left = expression->left;
6216 expression_t *const right = expression->right;
6217 type_t *const orig_type_left = left->base.type;
6218 type_t *const orig_type_right = right->base.type;
6219 type_t *const type_left = skip_typeref(orig_type_left);
6220 type_t *const type_right = skip_typeref(orig_type_right);
6223 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6224 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6225 expression->left = create_implicit_cast(left, arithmetic_type);
6226 expression->right = create_implicit_cast(right, arithmetic_type);
6227 expression->base.type = arithmetic_type;
6229 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6230 expression->base.type = type_left;
6231 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6232 if(!pointers_compatible(type_left, type_right)) {
6234 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6235 orig_type_left, orig_type_right);
6237 expression->base.type = type_ptrdiff_t;
6239 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6240 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6241 orig_type_left, orig_type_right);
6246 * Check the semantics of comparison expressions.
6248 * @param expression The expression to check.
6250 static void semantic_comparison(binary_expression_t *expression)
6252 expression_t *left = expression->left;
6253 expression_t *right = expression->right;
6254 type_t *orig_type_left = left->base.type;
6255 type_t *orig_type_right = right->base.type;
6257 type_t *type_left = skip_typeref(orig_type_left);
6258 type_t *type_right = skip_typeref(orig_type_right);
6260 /* TODO non-arithmetic types */
6261 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6262 if (warning.sign_compare &&
6263 (expression->base.kind != EXPR_BINARY_EQUAL &&
6264 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6265 (is_type_signed(type_left) != is_type_signed(type_right))) {
6266 warningf(&expression->base.source_position,
6267 "comparison between signed and unsigned");
6269 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6270 expression->left = create_implicit_cast(left, arithmetic_type);
6271 expression->right = create_implicit_cast(right, arithmetic_type);
6272 expression->base.type = arithmetic_type;
6273 if (warning.float_equal &&
6274 (expression->base.kind == EXPR_BINARY_EQUAL ||
6275 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6276 is_type_float(arithmetic_type)) {
6277 warningf(&expression->base.source_position,
6278 "comparing floating point with == or != is unsafe");
6280 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6281 /* TODO check compatibility */
6282 } else if (is_type_pointer(type_left)) {
6283 expression->right = create_implicit_cast(right, type_left);
6284 } else if (is_type_pointer(type_right)) {
6285 expression->left = create_implicit_cast(left, type_right);
6286 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6287 type_error_incompatible("invalid operands in comparison",
6288 &expression->base.source_position,
6289 type_left, type_right);
6291 expression->base.type = type_int;
6294 static void semantic_arithmetic_assign(binary_expression_t *expression)
6296 expression_t *left = expression->left;
6297 expression_t *right = expression->right;
6298 type_t *orig_type_left = left->base.type;
6299 type_t *orig_type_right = right->base.type;
6301 type_t *type_left = skip_typeref(orig_type_left);
6302 type_t *type_right = skip_typeref(orig_type_right);
6304 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6305 /* TODO: improve error message */
6306 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6307 errorf(HERE, "operation needs arithmetic types");
6312 /* combined instructions are tricky. We can't create an implicit cast on
6313 * the left side, because we need the uncasted form for the store.
6314 * The ast2firm pass has to know that left_type must be right_type
6315 * for the arithmetic operation and create a cast by itself */
6316 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6317 expression->right = create_implicit_cast(right, arithmetic_type);
6318 expression->base.type = type_left;
6321 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6323 expression_t *const left = expression->left;
6324 expression_t *const right = expression->right;
6325 type_t *const orig_type_left = left->base.type;
6326 type_t *const orig_type_right = right->base.type;
6327 type_t *const type_left = skip_typeref(orig_type_left);
6328 type_t *const type_right = skip_typeref(orig_type_right);
6330 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6331 /* combined instructions are tricky. We can't create an implicit cast on
6332 * the left side, because we need the uncasted form for the store.
6333 * The ast2firm pass has to know that left_type must be right_type
6334 * for the arithmetic operation and create a cast by itself */
6335 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6336 expression->right = create_implicit_cast(right, arithmetic_type);
6337 expression->base.type = type_left;
6338 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6339 expression->base.type = type_left;
6340 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6341 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6346 * Check the semantic restrictions of a logical expression.
6348 static void semantic_logical_op(binary_expression_t *expression)
6350 expression_t *const left = expression->left;
6351 expression_t *const right = expression->right;
6352 type_t *const orig_type_left = left->base.type;
6353 type_t *const orig_type_right = right->base.type;
6354 type_t *const type_left = skip_typeref(orig_type_left);
6355 type_t *const type_right = skip_typeref(orig_type_right);
6357 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6358 /* TODO: improve error message */
6359 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6360 errorf(HERE, "operation needs scalar types");
6365 expression->base.type = type_int;
6369 * Checks if a compound type has constant fields.
6371 static bool has_const_fields(const compound_type_t *type)
6373 const scope_t *scope = &type->declaration->scope;
6374 const declaration_t *declaration = scope->declarations;
6376 for (; declaration != NULL; declaration = declaration->next) {
6377 if (declaration->namespc != NAMESPACE_NORMAL)
6380 const type_t *decl_type = skip_typeref(declaration->type);
6381 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6389 * Check the semantic restrictions of a binary assign expression.
6391 static void semantic_binexpr_assign(binary_expression_t *expression)
6393 expression_t *left = expression->left;
6394 type_t *orig_type_left = left->base.type;
6396 type_t *type_left = revert_automatic_type_conversion(left);
6397 type_left = skip_typeref(orig_type_left);
6399 /* must be a modifiable lvalue */
6400 if (is_type_array(type_left)) {
6401 errorf(HERE, "cannot assign to arrays ('%E')", left);
6404 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6405 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6409 if(is_type_incomplete(type_left)) {
6411 "left-hand side of assignment '%E' has incomplete type '%T'",
6412 left, orig_type_left);
6415 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6416 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6417 left, orig_type_left);
6421 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6422 "assignment", &left->base.source_position);
6423 if (res_type == NULL) {
6424 errorf(&expression->base.source_position,
6425 "cannot assign to '%T' from '%T'",
6426 orig_type_left, expression->right->base.type);
6428 expression->right = create_implicit_cast(expression->right, res_type);
6431 expression->base.type = orig_type_left;
6435 * Determine if the outermost operation (or parts thereof) of the given
6436 * expression has no effect in order to generate a warning about this fact.
6437 * Therefore in some cases this only examines some of the operands of the
6438 * expression (see comments in the function and examples below).
6440 * f() + 23; // warning, because + has no effect
6441 * x || f(); // no warning, because x controls execution of f()
6442 * x ? y : f(); // warning, because y has no effect
6443 * (void)x; // no warning to be able to suppress the warning
6444 * This function can NOT be used for an "expression has definitely no effect"-
6446 static bool expression_has_effect(const expression_t *const expr)
6448 switch (expr->kind) {
6449 case EXPR_UNKNOWN: break;
6450 case EXPR_INVALID: return true; /* do NOT warn */
6451 case EXPR_REFERENCE: return false;
6452 /* suppress the warning for microsoft __noop operations */
6453 case EXPR_CONST: return expr->conste.is_ms_noop;
6454 case EXPR_CHARACTER_CONSTANT: return false;
6455 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6456 case EXPR_STRING_LITERAL: return false;
6457 case EXPR_WIDE_STRING_LITERAL: return false;
6460 const call_expression_t *const call = &expr->call;
6461 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6464 switch (call->function->builtin_symbol.symbol->ID) {
6465 case T___builtin_va_end: return true;
6466 default: return false;
6470 /* Generate the warning if either the left or right hand side of a
6471 * conditional expression has no effect */
6472 case EXPR_CONDITIONAL: {
6473 const conditional_expression_t *const cond = &expr->conditional;
6475 expression_has_effect(cond->true_expression) &&
6476 expression_has_effect(cond->false_expression);
6479 case EXPR_SELECT: return false;
6480 case EXPR_ARRAY_ACCESS: return false;
6481 case EXPR_SIZEOF: return false;
6482 case EXPR_CLASSIFY_TYPE: return false;
6483 case EXPR_ALIGNOF: return false;
6485 case EXPR_FUNCNAME: return false;
6486 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6487 case EXPR_BUILTIN_CONSTANT_P: return false;
6488 case EXPR_BUILTIN_PREFETCH: return true;
6489 case EXPR_OFFSETOF: return false;
6490 case EXPR_VA_START: return true;
6491 case EXPR_VA_ARG: return true;
6492 case EXPR_STATEMENT: return true; // TODO
6493 case EXPR_COMPOUND_LITERAL: return false;
6495 case EXPR_UNARY_NEGATE: return false;
6496 case EXPR_UNARY_PLUS: return false;
6497 case EXPR_UNARY_BITWISE_NEGATE: return false;
6498 case EXPR_UNARY_NOT: return false;
6499 case EXPR_UNARY_DEREFERENCE: return false;
6500 case EXPR_UNARY_TAKE_ADDRESS: return false;
6501 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6502 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6503 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6504 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6506 /* Treat void casts as if they have an effect in order to being able to
6507 * suppress the warning */
6508 case EXPR_UNARY_CAST: {
6509 type_t *const type = skip_typeref(expr->base.type);
6510 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6513 case EXPR_UNARY_CAST_IMPLICIT: return true;
6514 case EXPR_UNARY_ASSUME: return true;
6515 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6517 case EXPR_BINARY_ADD: return false;
6518 case EXPR_BINARY_SUB: return false;
6519 case EXPR_BINARY_MUL: return false;
6520 case EXPR_BINARY_DIV: return false;
6521 case EXPR_BINARY_MOD: return false;
6522 case EXPR_BINARY_EQUAL: return false;
6523 case EXPR_BINARY_NOTEQUAL: return false;
6524 case EXPR_BINARY_LESS: return false;
6525 case EXPR_BINARY_LESSEQUAL: return false;
6526 case EXPR_BINARY_GREATER: return false;
6527 case EXPR_BINARY_GREATEREQUAL: return false;
6528 case EXPR_BINARY_BITWISE_AND: return false;
6529 case EXPR_BINARY_BITWISE_OR: return false;
6530 case EXPR_BINARY_BITWISE_XOR: return false;
6531 case EXPR_BINARY_SHIFTLEFT: return false;
6532 case EXPR_BINARY_SHIFTRIGHT: return false;
6533 case EXPR_BINARY_ASSIGN: return true;
6534 case EXPR_BINARY_MUL_ASSIGN: return true;
6535 case EXPR_BINARY_DIV_ASSIGN: return true;
6536 case EXPR_BINARY_MOD_ASSIGN: return true;
6537 case EXPR_BINARY_ADD_ASSIGN: return true;
6538 case EXPR_BINARY_SUB_ASSIGN: return true;
6539 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6540 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6541 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6542 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6543 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6545 /* Only examine the right hand side of && and ||, because the left hand
6546 * side already has the effect of controlling the execution of the right
6548 case EXPR_BINARY_LOGICAL_AND:
6549 case EXPR_BINARY_LOGICAL_OR:
6550 /* Only examine the right hand side of a comma expression, because the left
6551 * hand side has a separate warning */
6552 case EXPR_BINARY_COMMA:
6553 return expression_has_effect(expr->binary.right);
6555 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6556 case EXPR_BINARY_ISGREATER: return false;
6557 case EXPR_BINARY_ISGREATEREQUAL: return false;
6558 case EXPR_BINARY_ISLESS: return false;
6559 case EXPR_BINARY_ISLESSEQUAL: return false;
6560 case EXPR_BINARY_ISLESSGREATER: return false;
6561 case EXPR_BINARY_ISUNORDERED: return false;
6564 internal_errorf(HERE, "unexpected expression");
6567 static void semantic_comma(binary_expression_t *expression)
6569 if (warning.unused_value) {
6570 const expression_t *const left = expression->left;
6571 if (!expression_has_effect(left)) {
6572 warningf(&left->base.source_position,
6573 "left-hand operand of comma expression has no effect");
6576 expression->base.type = expression->right->base.type;
6579 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6580 static expression_t *parse_##binexpression_type(unsigned precedence, \
6581 expression_t *left) \
6584 source_position_t pos = *HERE; \
6586 expression_t *right = parse_sub_expression(precedence + lr); \
6588 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6589 binexpr->base.source_position = pos; \
6590 binexpr->binary.left = left; \
6591 binexpr->binary.right = right; \
6592 sfunc(&binexpr->binary); \
6597 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6598 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6599 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6600 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6601 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6602 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6603 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6604 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6605 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6607 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6608 semantic_comparison, 1)
6609 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6610 semantic_comparison, 1)
6611 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6612 semantic_comparison, 1)
6613 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6614 semantic_comparison, 1)
6616 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6617 semantic_binexpr_arithmetic, 1)
6618 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6619 semantic_binexpr_arithmetic, 1)
6620 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6621 semantic_binexpr_arithmetic, 1)
6622 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6623 semantic_logical_op, 1)
6624 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6625 semantic_logical_op, 1)
6626 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6627 semantic_shift_op, 1)
6628 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6629 semantic_shift_op, 1)
6630 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6631 semantic_arithmetic_addsubb_assign, 0)
6632 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6633 semantic_arithmetic_addsubb_assign, 0)
6634 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6635 semantic_arithmetic_assign, 0)
6636 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6637 semantic_arithmetic_assign, 0)
6638 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6639 semantic_arithmetic_assign, 0)
6640 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6641 semantic_arithmetic_assign, 0)
6642 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6643 semantic_arithmetic_assign, 0)
6644 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6645 semantic_arithmetic_assign, 0)
6646 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6647 semantic_arithmetic_assign, 0)
6648 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6649 semantic_arithmetic_assign, 0)
6651 static expression_t *parse_sub_expression(unsigned precedence)
6653 if(token.type < 0) {
6654 return expected_expression_error();
6657 expression_parser_function_t *parser
6658 = &expression_parsers[token.type];
6659 source_position_t source_position = token.source_position;
6662 if(parser->parser != NULL) {
6663 left = parser->parser(parser->precedence);
6665 left = parse_primary_expression();
6667 assert(left != NULL);
6668 left->base.source_position = source_position;
6671 if(token.type < 0) {
6672 return expected_expression_error();
6675 parser = &expression_parsers[token.type];
6676 if(parser->infix_parser == NULL)
6678 if(parser->infix_precedence < precedence)
6681 left = parser->infix_parser(parser->infix_precedence, left);
6683 assert(left != NULL);
6684 assert(left->kind != EXPR_UNKNOWN);
6685 left->base.source_position = source_position;
6692 * Parse an expression.
6694 static expression_t *parse_expression(void)
6696 return parse_sub_expression(1);
6700 * Register a parser for a prefix-like operator with given precedence.
6702 * @param parser the parser function
6703 * @param token_type the token type of the prefix token
6704 * @param precedence the precedence of the operator
6706 static void register_expression_parser(parse_expression_function parser,
6707 int token_type, unsigned precedence)
6709 expression_parser_function_t *entry = &expression_parsers[token_type];
6711 if(entry->parser != NULL) {
6712 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6713 panic("trying to register multiple expression parsers for a token");
6715 entry->parser = parser;
6716 entry->precedence = precedence;
6720 * Register a parser for an infix operator with given precedence.
6722 * @param parser the parser function
6723 * @param token_type the token type of the infix operator
6724 * @param precedence the precedence of the operator
6726 static void register_infix_parser(parse_expression_infix_function parser,
6727 int token_type, unsigned precedence)
6729 expression_parser_function_t *entry = &expression_parsers[token_type];
6731 if(entry->infix_parser != NULL) {
6732 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6733 panic("trying to register multiple infix expression parsers for a "
6736 entry->infix_parser = parser;
6737 entry->infix_precedence = precedence;
6741 * Initialize the expression parsers.
6743 static void init_expression_parsers(void)
6745 memset(&expression_parsers, 0, sizeof(expression_parsers));
6747 register_infix_parser(parse_array_expression, '[', 30);
6748 register_infix_parser(parse_call_expression, '(', 30);
6749 register_infix_parser(parse_select_expression, '.', 30);
6750 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6751 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6753 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6756 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6757 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6758 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6759 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6760 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6761 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6762 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6763 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6764 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6765 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6766 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6767 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6768 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6769 T_EXCLAMATIONMARKEQUAL, 13);
6770 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6771 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6772 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6773 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6774 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6775 register_infix_parser(parse_conditional_expression, '?', 7);
6776 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6777 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6778 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6779 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6780 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6781 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6782 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6783 T_LESSLESSEQUAL, 2);
6784 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6785 T_GREATERGREATEREQUAL, 2);
6786 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6788 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6790 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6793 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6795 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6796 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6797 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6798 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6799 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6800 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6801 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6803 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6805 register_expression_parser(parse_sizeof, T_sizeof, 25);
6806 register_expression_parser(parse_alignof, T___alignof__, 25);
6807 register_expression_parser(parse_extension, T___extension__, 25);
6808 register_expression_parser(parse_builtin_classify_type,
6809 T___builtin_classify_type, 25);
6813 * Parse a asm statement constraints specification.
6815 static asm_constraint_t *parse_asm_constraints(void)
6817 asm_constraint_t *result = NULL;
6818 asm_constraint_t *last = NULL;
6820 while(token.type == T_STRING_LITERAL || token.type == '[') {
6821 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6822 memset(constraint, 0, sizeof(constraint[0]));
6824 if(token.type == '[') {
6826 if(token.type != T_IDENTIFIER) {
6827 parse_error_expected("while parsing asm constraint",
6831 constraint->symbol = token.v.symbol;
6836 constraint->constraints = parse_string_literals();
6838 constraint->expression = parse_expression();
6842 last->next = constraint;
6844 result = constraint;
6848 if(token.type != ',')
6859 * Parse a asm statement clobber specification.
6861 static asm_clobber_t *parse_asm_clobbers(void)
6863 asm_clobber_t *result = NULL;
6864 asm_clobber_t *last = NULL;
6866 while(token.type == T_STRING_LITERAL) {
6867 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6868 clobber->clobber = parse_string_literals();
6871 last->next = clobber;
6877 if(token.type != ',')
6886 * Parse an asm statement.
6888 static statement_t *parse_asm_statement(void)
6892 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6893 statement->base.source_position = token.source_position;
6895 asm_statement_t *asm_statement = &statement->asms;
6897 if(token.type == T_volatile) {
6899 asm_statement->is_volatile = true;
6903 add_anchor_token(')');
6904 add_anchor_token(':');
6905 asm_statement->asm_text = parse_string_literals();
6907 if(token.type != ':') {
6908 rem_anchor_token(':');
6913 asm_statement->inputs = parse_asm_constraints();
6914 if(token.type != ':') {
6915 rem_anchor_token(':');
6920 asm_statement->outputs = parse_asm_constraints();
6921 if(token.type != ':') {
6922 rem_anchor_token(':');
6925 rem_anchor_token(':');
6928 asm_statement->clobbers = parse_asm_clobbers();
6931 rem_anchor_token(')');
6936 return create_invalid_statement();
6940 * Parse a case statement.
6942 static statement_t *parse_case_statement(void)
6946 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6948 statement->base.source_position = token.source_position;
6949 statement->case_label.expression = parse_expression();
6951 if (c_mode & _GNUC) {
6952 if (token.type == T_DOTDOTDOT) {
6954 statement->case_label.end_range = parse_expression();
6960 if (! is_constant_expression(statement->case_label.expression)) {
6961 errorf(&statement->base.source_position,
6962 "case label does not reduce to an integer constant");
6964 /* TODO: check if the case label is already known */
6965 if (current_switch != NULL) {
6966 /* link all cases into the switch statement */
6967 if (current_switch->last_case == NULL) {
6968 current_switch->first_case =
6969 current_switch->last_case = &statement->case_label;
6971 current_switch->last_case->next = &statement->case_label;
6974 errorf(&statement->base.source_position,
6975 "case label not within a switch statement");
6978 statement->case_label.statement = parse_statement();
6982 return create_invalid_statement();
6986 * Finds an existing default label of a switch statement.
6988 static case_label_statement_t *
6989 find_default_label(const switch_statement_t *statement)
6991 case_label_statement_t *label = statement->first_case;
6992 for ( ; label != NULL; label = label->next) {
6993 if (label->expression == NULL)
7000 * Parse a default statement.
7002 static statement_t *parse_default_statement(void)
7006 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7008 statement->base.source_position = token.source_position;
7011 if (current_switch != NULL) {
7012 const case_label_statement_t *def_label = find_default_label(current_switch);
7013 if (def_label != NULL) {
7014 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7015 &def_label->base.source_position);
7017 /* link all cases into the switch statement */
7018 if (current_switch->last_case == NULL) {
7019 current_switch->first_case =
7020 current_switch->last_case = &statement->case_label;
7022 current_switch->last_case->next = &statement->case_label;
7026 errorf(&statement->base.source_position,
7027 "'default' label not within a switch statement");
7029 statement->case_label.statement = parse_statement();
7033 return create_invalid_statement();
7037 * Return the declaration for a given label symbol or create a new one.
7039 static declaration_t *get_label(symbol_t *symbol)
7041 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7042 assert(current_function != NULL);
7043 /* if we found a label in the same function, then we already created the
7045 if(candidate != NULL
7046 && candidate->parent_scope == ¤t_function->scope) {
7050 /* otherwise we need to create a new one */
7051 declaration_t *const declaration = allocate_declaration_zero();
7052 declaration->namespc = NAMESPACE_LABEL;
7053 declaration->symbol = symbol;
7055 label_push(declaration);
7061 * Parse a label statement.
7063 static statement_t *parse_label_statement(void)
7065 assert(token.type == T_IDENTIFIER);
7066 symbol_t *symbol = token.v.symbol;
7069 declaration_t *label = get_label(symbol);
7071 /* if source position is already set then the label is defined twice,
7072 * otherwise it was just mentioned in a goto so far */
7073 if(label->source_position.input_name != NULL) {
7074 errorf(HERE, "duplicate label '%Y' (declared %P)",
7075 symbol, &label->source_position);
7077 label->source_position = token.source_position;
7080 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7082 statement->base.source_position = token.source_position;
7083 statement->label.label = label;
7087 if(token.type == '}') {
7088 /* TODO only warn? */
7090 warningf(HERE, "label at end of compound statement");
7091 statement->label.statement = create_empty_statement();
7093 errorf(HERE, "label at end of compound statement");
7094 statement->label.statement = create_invalid_statement();
7098 if (token.type == ';') {
7099 /* eat an empty statement here, to avoid the warning about an empty
7100 * after a label. label:; is commonly used to have a label before
7102 statement->label.statement = create_empty_statement();
7105 statement->label.statement = parse_statement();
7109 /* remember the labels's in a list for later checking */
7110 if (label_last == NULL) {
7111 label_first = &statement->label;
7113 label_last->next = &statement->label;
7115 label_last = &statement->label;
7121 * Parse an if statement.
7123 static statement_t *parse_if(void)
7127 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7128 statement->base.source_position = token.source_position;
7131 add_anchor_token(')');
7132 statement->ifs.condition = parse_expression();
7133 rem_anchor_token(')');
7136 add_anchor_token(T_else);
7137 statement->ifs.true_statement = parse_statement();
7138 rem_anchor_token(T_else);
7140 if(token.type == T_else) {
7142 statement->ifs.false_statement = parse_statement();
7147 return create_invalid_statement();
7151 * Parse a switch statement.
7153 static statement_t *parse_switch(void)
7157 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7158 statement->base.source_position = token.source_position;
7161 expression_t *const expr = parse_expression();
7162 type_t * type = skip_typeref(expr->base.type);
7163 if (is_type_integer(type)) {
7164 type = promote_integer(type);
7165 } else if (is_type_valid(type)) {
7166 errorf(&expr->base.source_position,
7167 "switch quantity is not an integer, but '%T'", type);
7168 type = type_error_type;
7170 statement->switchs.expression = create_implicit_cast(expr, type);
7173 switch_statement_t *rem = current_switch;
7174 current_switch = &statement->switchs;
7175 statement->switchs.body = parse_statement();
7176 current_switch = rem;
7178 if(warning.switch_default &&
7179 find_default_label(&statement->switchs) == NULL) {
7180 warningf(&statement->base.source_position, "switch has no default case");
7185 return create_invalid_statement();
7188 static statement_t *parse_loop_body(statement_t *const loop)
7190 statement_t *const rem = current_loop;
7191 current_loop = loop;
7193 statement_t *const body = parse_statement();
7200 * Parse a while statement.
7202 static statement_t *parse_while(void)
7206 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7207 statement->base.source_position = token.source_position;
7210 add_anchor_token(')');
7211 statement->whiles.condition = parse_expression();
7212 rem_anchor_token(')');
7215 statement->whiles.body = parse_loop_body(statement);
7219 return create_invalid_statement();
7223 * Parse a do statement.
7225 static statement_t *parse_do(void)
7229 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7231 statement->base.source_position = token.source_position;
7233 add_anchor_token(T_while);
7234 statement->do_while.body = parse_loop_body(statement);
7235 rem_anchor_token(T_while);
7239 add_anchor_token(')');
7240 statement->do_while.condition = parse_expression();
7241 rem_anchor_token(')');
7247 return create_invalid_statement();
7251 * Parse a for statement.
7253 static statement_t *parse_for(void)
7257 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7258 statement->base.source_position = token.source_position;
7260 int top = environment_top();
7261 scope_t *last_scope = scope;
7262 set_scope(&statement->fors.scope);
7265 add_anchor_token(')');
7267 if(token.type != ';') {
7268 if(is_declaration_specifier(&token, false)) {
7269 parse_declaration(record_declaration);
7271 add_anchor_token(';');
7272 expression_t *const init = parse_expression();
7273 statement->fors.initialisation = init;
7274 if (warning.unused_value && !expression_has_effect(init)) {
7275 warningf(&init->base.source_position,
7276 "initialisation of 'for'-statement has no effect");
7278 rem_anchor_token(';');
7285 if(token.type != ';') {
7286 add_anchor_token(';');
7287 statement->fors.condition = parse_expression();
7288 rem_anchor_token(';');
7291 if(token.type != ')') {
7292 expression_t *const step = parse_expression();
7293 statement->fors.step = step;
7294 if (warning.unused_value && !expression_has_effect(step)) {
7295 warningf(&step->base.source_position,
7296 "step of 'for'-statement has no effect");
7299 rem_anchor_token(')');
7301 statement->fors.body = parse_loop_body(statement);
7303 assert(scope == &statement->fors.scope);
7304 set_scope(last_scope);
7305 environment_pop_to(top);
7310 rem_anchor_token(')');
7311 assert(scope == &statement->fors.scope);
7312 set_scope(last_scope);
7313 environment_pop_to(top);
7315 return create_invalid_statement();
7319 * Parse a goto statement.
7321 static statement_t *parse_goto(void)
7325 if(token.type != T_IDENTIFIER) {
7326 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
7330 symbol_t *symbol = token.v.symbol;
7333 declaration_t *label = get_label(symbol);
7335 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7336 statement->base.source_position = token.source_position;
7338 statement->gotos.label = label;
7340 /* remember the goto's in a list for later checking */
7341 if (goto_last == NULL) {
7342 goto_first = &statement->gotos;
7344 goto_last->next = &statement->gotos;
7346 goto_last = &statement->gotos;
7352 return create_invalid_statement();
7356 * Parse a continue statement.
7358 static statement_t *parse_continue(void)
7360 statement_t *statement;
7361 if (current_loop == NULL) {
7362 errorf(HERE, "continue statement not within loop");
7363 statement = create_invalid_statement();
7365 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7367 statement->base.source_position = token.source_position;
7375 return create_invalid_statement();
7379 * Parse a break statement.
7381 static statement_t *parse_break(void)
7383 statement_t *statement;
7384 if (current_switch == NULL && current_loop == NULL) {
7385 errorf(HERE, "break statement not within loop or switch");
7386 statement = create_invalid_statement();
7388 statement = allocate_statement_zero(STATEMENT_BREAK);
7390 statement->base.source_position = token.source_position;
7398 return create_invalid_statement();
7402 * Parse a __leave statement.
7404 static statement_t *parse_leave(void)
7406 statement_t *statement;
7407 if (current_try == NULL) {
7408 errorf(HERE, "__leave statement not within __try");
7409 statement = create_invalid_statement();
7411 statement = allocate_statement_zero(STATEMENT_LEAVE);
7413 statement->base.source_position = token.source_position;
7421 return create_invalid_statement();
7425 * Check if a given declaration represents a local variable.
7427 static bool is_local_var_declaration(const declaration_t *declaration) {
7428 switch ((storage_class_tag_t) declaration->storage_class) {
7429 case STORAGE_CLASS_AUTO:
7430 case STORAGE_CLASS_REGISTER: {
7431 const type_t *type = skip_typeref(declaration->type);
7432 if(is_type_function(type)) {
7444 * Check if a given declaration represents a variable.
7446 static bool is_var_declaration(const declaration_t *declaration) {
7447 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7450 const type_t *type = skip_typeref(declaration->type);
7451 return !is_type_function(type);
7455 * Check if a given expression represents a local variable.
7457 static bool is_local_variable(const expression_t *expression)
7459 if (expression->base.kind != EXPR_REFERENCE) {
7462 const declaration_t *declaration = expression->reference.declaration;
7463 return is_local_var_declaration(declaration);
7467 * Check if a given expression represents a local variable and
7468 * return its declaration then, else return NULL.
7470 declaration_t *expr_is_variable(const expression_t *expression)
7472 if (expression->base.kind != EXPR_REFERENCE) {
7475 declaration_t *declaration = expression->reference.declaration;
7476 if (is_var_declaration(declaration))
7482 * Parse a return statement.
7484 static statement_t *parse_return(void)
7486 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7487 statement->base.source_position = token.source_position;
7491 expression_t *return_value = NULL;
7492 if(token.type != ';') {
7493 return_value = parse_expression();
7497 const type_t *const func_type = current_function->type;
7498 assert(is_type_function(func_type));
7499 type_t *const return_type = skip_typeref(func_type->function.return_type);
7501 if(return_value != NULL) {
7502 type_t *return_value_type = skip_typeref(return_value->base.type);
7504 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7505 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7506 warningf(&statement->base.source_position,
7507 "'return' with a value, in function returning void");
7508 return_value = NULL;
7510 type_t *const res_type = semantic_assign(return_type,
7511 return_value, "'return'", &statement->base.source_position);
7512 if (res_type == NULL) {
7513 errorf(&statement->base.source_position,
7514 "cannot return something of type '%T' in function returning '%T'",
7515 return_value->base.type, return_type);
7517 return_value = create_implicit_cast(return_value, res_type);
7520 /* check for returning address of a local var */
7521 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7522 const expression_t *expression = return_value->unary.value;
7523 if (is_local_variable(expression)) {
7524 warningf(&statement->base.source_position,
7525 "function returns address of local variable");
7529 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7530 warningf(&statement->base.source_position,
7531 "'return' without value, in function returning non-void");
7534 statement->returns.value = return_value;
7538 return create_invalid_statement();
7542 * Parse a declaration statement.
7544 static statement_t *parse_declaration_statement(void)
7546 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7548 statement->base.source_position = token.source_position;
7550 declaration_t *before = last_declaration;
7551 parse_declaration(record_declaration);
7553 if(before == NULL) {
7554 statement->declaration.declarations_begin = scope->declarations;
7556 statement->declaration.declarations_begin = before->next;
7558 statement->declaration.declarations_end = last_declaration;
7564 * Parse an expression statement, ie. expr ';'.
7566 static statement_t *parse_expression_statement(void)
7568 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7570 statement->base.source_position = token.source_position;
7571 expression_t *const expr = parse_expression();
7572 statement->expression.expression = expr;
7574 if (warning.unused_value && !expression_has_effect(expr)) {
7575 warningf(&expr->base.source_position, "statement has no effect");
7582 return create_invalid_statement();
7586 * Parse a microsoft __try { } __finally { } or
7587 * __try{ } __except() { }
7589 static statement_t *parse_ms_try_statment(void) {
7590 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
7592 statement->base.source_position = token.source_position;
7595 ms_try_statement_t *rem = current_try;
7596 current_try = &statement->ms_try;
7597 statement->ms_try.try_statement = parse_compound_statement();
7600 if(token.type == T___except) {
7603 add_anchor_token(')');
7604 expression_t *const expr = parse_expression();
7605 type_t * type = skip_typeref(expr->base.type);
7606 if (is_type_integer(type)) {
7607 type = promote_integer(type);
7608 } else if (is_type_valid(type)) {
7609 errorf(&expr->base.source_position,
7610 "__expect expression is not an integer, but '%T'", type);
7611 type = type_error_type;
7613 statement->ms_try.except_expression = create_implicit_cast(expr, type);
7614 rem_anchor_token(')');
7616 statement->ms_try.final_statement = parse_compound_statement();
7617 } else if(token.type == T__finally) {
7619 statement->ms_try.final_statement = parse_compound_statement();
7621 parse_error_expected("while parsing __try statement", T___except, T___finally, 0);
7622 return create_invalid_statement();
7626 return create_invalid_statement();
7630 * Parse a statement.
7632 static statement_t *parse_statement(void)
7634 statement_t *statement = NULL;
7636 /* declaration or statement */
7637 add_anchor_token(';');
7638 switch(token.type) {
7640 statement = parse_asm_statement();
7644 statement = parse_case_statement();
7648 statement = parse_default_statement();
7652 statement = parse_compound_statement();
7656 statement = parse_if();
7660 statement = parse_switch();
7664 statement = parse_while();
7668 statement = parse_do();
7672 statement = parse_for();
7676 statement = parse_goto();
7680 statement = parse_continue();
7684 statement = parse_break();
7688 statement = parse_leave();
7692 statement = parse_return();
7696 if(warning.empty_statement) {
7697 warningf(HERE, "statement is empty");
7699 statement = create_empty_statement();
7704 if(look_ahead(1)->type == ':') {
7705 statement = parse_label_statement();
7709 if(is_typedef_symbol(token.v.symbol)) {
7710 statement = parse_declaration_statement();
7714 statement = parse_expression_statement();
7717 case T___extension__:
7718 /* this can be a prefix to a declaration or an expression statement */
7719 /* we simply eat it now and parse the rest with tail recursion */
7722 } while(token.type == T___extension__);
7723 statement = parse_statement();
7727 statement = parse_declaration_statement();
7731 statement = parse_ms_try_statment();
7735 statement = parse_expression_statement();
7738 rem_anchor_token(';');
7740 assert(statement != NULL
7741 && statement->base.source_position.input_name != NULL);
7747 * Parse a compound statement.
7749 static statement_t *parse_compound_statement(void)
7751 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7753 statement->base.source_position = token.source_position;
7756 add_anchor_token('}');
7758 int top = environment_top();
7759 scope_t *last_scope = scope;
7760 set_scope(&statement->compound.scope);
7762 statement_t *last_statement = NULL;
7764 while(token.type != '}' && token.type != T_EOF) {
7765 statement_t *sub_statement = parse_statement();
7766 if(is_invalid_statement(sub_statement)) {
7767 /* an error occurred. if we are at an anchor, return */
7773 if(last_statement != NULL) {
7774 last_statement->base.next = sub_statement;
7776 statement->compound.statements = sub_statement;
7779 while(sub_statement->base.next != NULL)
7780 sub_statement = sub_statement->base.next;
7782 last_statement = sub_statement;
7785 if(token.type == '}') {
7788 errorf(&statement->base.source_position,
7789 "end of file while looking for closing '}'");
7793 rem_anchor_token('}');
7794 assert(scope == &statement->compound.scope);
7795 set_scope(last_scope);
7796 environment_pop_to(top);
7802 * Initialize builtin types.
7804 static void initialize_builtin_types(void)
7806 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7807 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7808 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7809 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7810 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7811 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7812 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7813 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7815 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7816 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7817 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7818 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7822 * Check for unused global static functions and variables
7824 static void check_unused_globals(void)
7826 if (!warning.unused_function && !warning.unused_variable)
7829 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7830 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7833 type_t *const type = decl->type;
7835 if (is_type_function(skip_typeref(type))) {
7836 if (!warning.unused_function || decl->is_inline)
7839 s = (decl->init.statement != NULL ? "defined" : "declared");
7841 if (!warning.unused_variable)
7847 warningf(&decl->source_position, "'%#T' %s but not used",
7848 type, decl->symbol, s);
7853 * Parse a translation unit.
7855 static translation_unit_t *parse_translation_unit(void)
7857 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7859 assert(global_scope == NULL);
7860 global_scope = &unit->scope;
7862 assert(scope == NULL);
7863 set_scope(&unit->scope);
7865 initialize_builtin_types();
7867 while(token.type != T_EOF) {
7868 if (token.type == ';') {
7869 /* TODO error in strict mode */
7870 warningf(HERE, "stray ';' outside of function");
7873 parse_external_declaration();
7877 assert(scope == &unit->scope);
7879 last_declaration = NULL;
7881 assert(global_scope == &unit->scope);
7882 check_unused_globals();
7883 global_scope = NULL;
7891 * @return the translation unit or NULL if errors occurred.
7893 translation_unit_t *parse(void)
7895 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7896 label_stack = NEW_ARR_F(stack_entry_t, 0);
7897 diagnostic_count = 0;
7901 type_set_output(stderr);
7902 ast_set_output(stderr);
7904 lookahead_bufpos = 0;
7905 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7908 translation_unit_t *unit = parse_translation_unit();
7910 DEL_ARR_F(environment_stack);
7911 DEL_ARR_F(label_stack);
7917 * Initialize the parser.
7919 void init_parser(void)
7922 /* add predefined symbols for extended-decl-modifier */
7923 sym_align = symbol_table_insert("align");
7924 sym_allocate = symbol_table_insert("allocate");
7925 sym_dllimport = symbol_table_insert("dllimport");
7926 sym_dllexport = symbol_table_insert("dllexport");
7927 sym_naked = symbol_table_insert("naked");
7928 sym_noinline = symbol_table_insert("noinline");
7929 sym_noreturn = symbol_table_insert("noreturn");
7930 sym_nothrow = symbol_table_insert("nothrow");
7931 sym_novtable = symbol_table_insert("novtable");
7932 sym_property = symbol_table_insert("property");
7933 sym_get = symbol_table_insert("get");
7934 sym_put = symbol_table_insert("put");
7935 sym_selectany = symbol_table_insert("selectany");
7936 sym_thread = symbol_table_insert("thread");
7937 sym_uuid = symbol_table_insert("uuid");
7938 sym_deprecated = symbol_table_insert("deprecated");
7939 sym_restrict = symbol_table_insert("restrict");
7940 sym_noalias = symbol_table_insert("noalias");
7942 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7944 init_expression_parsers();
7945 obstack_init(&temp_obst);
7947 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7948 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7952 * Terminate the parser.
7954 void exit_parser(void)
7956 obstack_free(&temp_obst, NULL);