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 declaration_specifiers_t declaration_specifiers_t;
52 struct declaration_specifiers_t {
53 source_position_t source_position;
54 unsigned char declared_storage_class;
56 decl_modifiers_t decl_modifiers;
61 * An environment for parsing initializers (and compound literals).
63 typedef struct parse_initializer_env_t {
64 type_t *type; /**< the type of the initializer. In case of an
65 array type with unspecified size this gets
66 adjusted to the actual size. */
67 declaration_t *declaration; /**< the declaration that is initialized if any */
68 bool must_be_constant;
69 } parse_initializer_env_t;
71 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
74 static token_t lookahead_buffer[MAX_LOOKAHEAD];
75 static int lookahead_bufpos;
76 static stack_entry_t *environment_stack = NULL;
77 static stack_entry_t *label_stack = NULL;
78 static scope_t *global_scope = NULL;
79 static scope_t *scope = NULL;
80 static declaration_t *last_declaration = NULL;
81 static declaration_t *current_function = NULL;
82 static switch_statement_t *current_switch = NULL;
83 static statement_t *current_loop = NULL;
84 static goto_statement_t *goto_first = NULL;
85 static goto_statement_t *goto_last = NULL;
86 static label_statement_t *label_first = NULL;
87 static label_statement_t *label_last = NULL;
88 static struct obstack temp_obst;
90 /* symbols for Microsoft extended-decl-modifier */
91 static const symbol_t *sym_align = NULL;
92 static const symbol_t *sym_allocate = NULL;
93 static const symbol_t *sym_dllimport = NULL;
94 static const symbol_t *sym_dllexport = NULL;
95 static const symbol_t *sym_naked = NULL;
96 static const symbol_t *sym_noinline = NULL;
97 static const symbol_t *sym_noreturn = NULL;
98 static const symbol_t *sym_nothrow = NULL;
99 static const symbol_t *sym_novtable = NULL;
100 static const symbol_t *sym_property = NULL;
101 static const symbol_t *sym_get = NULL;
102 static const symbol_t *sym_put = NULL;
103 static const symbol_t *sym_selectany = NULL;
104 static const symbol_t *sym_thread = NULL;
105 static const symbol_t *sym_uuid = NULL;
107 /** The current source position. */
108 #define HERE token.source_position
110 static type_t *type_valist;
112 static statement_t *parse_compound_statement(void);
113 static statement_t *parse_statement(void);
115 static expression_t *parse_sub_expression(unsigned precedence);
116 static expression_t *parse_expression(void);
117 static type_t *parse_typename(void);
119 static void parse_compound_type_entries(declaration_t *compound_declaration);
120 static declaration_t *parse_declarator(
121 const declaration_specifiers_t *specifiers, bool may_be_abstract);
122 static declaration_t *record_declaration(declaration_t *declaration);
124 static void semantic_comparison(binary_expression_t *expression);
126 #define STORAGE_CLASSES \
133 #define TYPE_QUALIFIERS \
140 #ifdef PROVIDE_COMPLEX
141 #define COMPLEX_SPECIFIERS \
143 #define IMAGINARY_SPECIFIERS \
146 #define COMPLEX_SPECIFIERS
147 #define IMAGINARY_SPECIFIERS
150 #define TYPE_SPECIFIERS \
165 case T___builtin_va_list: \
169 #define DECLARATION_START \
174 #define TYPENAME_START \
179 * Allocate an AST node with given size and
180 * initialize all fields with zero.
182 static void *allocate_ast_zero(size_t size)
184 void *res = allocate_ast(size);
185 memset(res, 0, size);
189 static declaration_t *allocate_declaration_zero(void)
191 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
192 declaration->type = type_error_type;
197 * Returns the size of a statement node.
199 * @param kind the statement kind
201 static size_t get_statement_struct_size(statement_kind_t kind)
203 static const size_t sizes[] = {
204 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
205 [STATEMENT_RETURN] = sizeof(return_statement_t),
206 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
207 [STATEMENT_IF] = sizeof(if_statement_t),
208 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
209 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
210 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
211 [STATEMENT_BREAK] = sizeof(statement_base_t),
212 [STATEMENT_GOTO] = sizeof(goto_statement_t),
213 [STATEMENT_LABEL] = sizeof(label_statement_t),
214 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
215 [STATEMENT_WHILE] = sizeof(while_statement_t),
216 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
217 [STATEMENT_FOR] = sizeof(for_statement_t),
218 [STATEMENT_ASM] = sizeof(asm_statement_t)
220 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
221 assert(sizes[kind] != 0);
226 * Allocate a statement node of given kind and initialize all
229 static statement_t *allocate_statement_zero(statement_kind_t kind)
231 size_t size = get_statement_struct_size(kind);
232 statement_t *res = allocate_ast_zero(size);
234 res->base.kind = kind;
239 * Returns the size of an expression node.
241 * @param kind the expression kind
243 static size_t get_expression_struct_size(expression_kind_t kind)
245 static const size_t sizes[] = {
246 [EXPR_INVALID] = sizeof(expression_base_t),
247 [EXPR_REFERENCE] = sizeof(reference_expression_t),
248 [EXPR_CONST] = sizeof(const_expression_t),
249 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
250 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
251 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
252 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
253 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
254 [EXPR_CALL] = sizeof(call_expression_t),
255 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
256 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
257 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
258 [EXPR_SELECT] = sizeof(select_expression_t),
259 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
260 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
261 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
262 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
263 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
264 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
265 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
266 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
267 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
268 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
269 [EXPR_VA_START] = sizeof(va_start_expression_t),
270 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
271 [EXPR_STATEMENT] = sizeof(statement_expression_t),
273 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
274 return sizes[EXPR_UNARY_FIRST];
276 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
277 return sizes[EXPR_BINARY_FIRST];
279 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
280 assert(sizes[kind] != 0);
285 * Allocate an expression node of given kind and initialize all
288 static expression_t *allocate_expression_zero(expression_kind_t kind)
290 size_t size = get_expression_struct_size(kind);
291 expression_t *res = allocate_ast_zero(size);
293 res->base.kind = kind;
294 res->base.type = type_error_type;
299 * Returns the size of a type node.
301 * @param kind the type kind
303 static size_t get_type_struct_size(type_kind_t kind)
305 static const size_t sizes[] = {
306 [TYPE_ATOMIC] = sizeof(atomic_type_t),
307 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
308 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
309 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
310 [TYPE_ENUM] = sizeof(enum_type_t),
311 [TYPE_FUNCTION] = sizeof(function_type_t),
312 [TYPE_POINTER] = sizeof(pointer_type_t),
313 [TYPE_ARRAY] = sizeof(array_type_t),
314 [TYPE_BUILTIN] = sizeof(builtin_type_t),
315 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
316 [TYPE_TYPEOF] = sizeof(typeof_type_t),
318 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
319 assert(kind <= TYPE_TYPEOF);
320 assert(sizes[kind] != 0);
325 * Allocate a type node of given kind and initialize all
328 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
330 size_t size = get_type_struct_size(kind);
331 type_t *res = obstack_alloc(type_obst, size);
332 memset(res, 0, size);
334 res->base.kind = kind;
335 res->base.source_position = source_position;
340 * Returns the size of an initializer node.
342 * @param kind the initializer kind
344 static size_t get_initializer_size(initializer_kind_t kind)
346 static const size_t sizes[] = {
347 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
348 [INITIALIZER_STRING] = sizeof(initializer_string_t),
349 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
350 [INITIALIZER_LIST] = sizeof(initializer_list_t),
351 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
353 assert(kind < sizeof(sizes) / sizeof(*sizes));
354 assert(sizes[kind] != 0);
359 * Allocate an initializer node of given kind and initialize all
362 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
364 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
371 * Free a type from the type obstack.
373 static void free_type(void *type)
375 obstack_free(type_obst, type);
379 * Returns the index of the top element of the environment stack.
381 static size_t environment_top(void)
383 return ARR_LEN(environment_stack);
387 * Returns the index of the top element of the label stack.
389 static size_t label_top(void)
391 return ARR_LEN(label_stack);
396 * Return the next token.
398 static inline void next_token(void)
400 token = lookahead_buffer[lookahead_bufpos];
401 lookahead_buffer[lookahead_bufpos] = lexer_token;
404 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
407 print_token(stderr, &token);
408 fprintf(stderr, "\n");
413 * Return the next token with a given lookahead.
415 static inline const token_t *look_ahead(int num)
417 assert(num > 0 && num <= MAX_LOOKAHEAD);
418 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
419 return &lookahead_buffer[pos];
422 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
425 * Report a parse error because an expected token was not found.
427 static void parse_error_expected(const char *message, ...)
429 if(message != NULL) {
430 errorf(HERE, "%s", message);
433 va_start(ap, message);
434 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
439 * Report a type error.
441 static void type_error(const char *msg, const source_position_t source_position,
444 errorf(source_position, "%s, but found type '%T'", msg, type);
448 * Report an incompatible type.
450 static void type_error_incompatible(const char *msg,
451 const source_position_t source_position, type_t *type1, type_t *type2)
453 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
457 * Eat an complete block, ie. '{ ... }'.
459 static void eat_block(void)
461 if(token.type == '{')
464 while(token.type != '}') {
465 if(token.type == T_EOF)
467 if(token.type == '{') {
477 * Eat a statement until an ';' token.
479 static void eat_statement(void)
481 while(token.type != ';') {
482 if(token.type == T_EOF)
484 if(token.type == '}')
486 if(token.type == '{') {
496 * Eat a parenthesed term, ie. '( ... )'.
498 static void eat_paren(void)
500 if(token.type == '(')
503 while(token.type != ')') {
504 if(token.type == T_EOF)
506 if(token.type == ')' || token.type == ';' || token.type == '}') {
509 if(token.type == ')') {
513 if(token.type == '(') {
517 if(token.type == '{') {
526 * Expect the the current token is the expected token.
527 * If not, generate an error, eat the current statement,
528 * and goto the end_error label.
530 #define expect(expected) \
532 if(UNLIKELY(token.type != (expected))) { \
533 parse_error_expected(NULL, (expected), 0); \
540 #define expect_block(expected) \
542 if(UNLIKELY(token.type != (expected))) { \
543 parse_error_expected(NULL, (expected), 0); \
550 static void set_scope(scope_t *new_scope)
553 scope->last_declaration = last_declaration;
557 last_declaration = new_scope->last_declaration;
561 * Search a symbol in a given namespace and returns its declaration or
562 * NULL if this symbol was not found.
564 static declaration_t *get_declaration(const symbol_t *const symbol,
565 const namespace_t namespc)
567 declaration_t *declaration = symbol->declaration;
568 for( ; declaration != NULL; declaration = declaration->symbol_next) {
569 if(declaration->namespc == namespc)
577 * pushs an environment_entry on the environment stack and links the
578 * corresponding symbol to the new entry
580 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
582 symbol_t *symbol = declaration->symbol;
583 namespace_t namespc = (namespace_t) declaration->namespc;
585 /* replace/add declaration into declaration list of the symbol */
586 declaration_t *iter = symbol->declaration;
588 symbol->declaration = declaration;
590 declaration_t *iter_last = NULL;
591 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
592 /* replace an entry? */
593 if(iter->namespc == namespc) {
594 if(iter_last == NULL) {
595 symbol->declaration = declaration;
597 iter_last->symbol_next = declaration;
599 declaration->symbol_next = iter->symbol_next;
604 assert(iter_last->symbol_next == NULL);
605 iter_last->symbol_next = declaration;
609 /* remember old declaration */
611 entry.symbol = symbol;
612 entry.old_declaration = iter;
613 entry.namespc = (unsigned short) namespc;
614 ARR_APP1(stack_entry_t, *stack_ptr, entry);
617 static void environment_push(declaration_t *declaration)
619 assert(declaration->source_position.input_name != NULL);
620 assert(declaration->parent_scope != NULL);
621 stack_push(&environment_stack, declaration);
624 static void label_push(declaration_t *declaration)
626 declaration->parent_scope = ¤t_function->scope;
627 stack_push(&label_stack, declaration);
631 * pops symbols from the environment stack until @p new_top is the top element
633 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
635 stack_entry_t *stack = *stack_ptr;
636 size_t top = ARR_LEN(stack);
639 assert(new_top <= top);
643 for(i = top; i > new_top; --i) {
644 stack_entry_t *entry = &stack[i - 1];
646 declaration_t *old_declaration = entry->old_declaration;
647 symbol_t *symbol = entry->symbol;
648 namespace_t namespc = (namespace_t)entry->namespc;
650 /* replace/remove declaration */
651 declaration_t *declaration = symbol->declaration;
652 assert(declaration != NULL);
653 if(declaration->namespc == namespc) {
654 if(old_declaration == NULL) {
655 symbol->declaration = declaration->symbol_next;
657 symbol->declaration = old_declaration;
660 declaration_t *iter_last = declaration;
661 declaration_t *iter = declaration->symbol_next;
662 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
663 /* replace an entry? */
664 if(iter->namespc == namespc) {
665 assert(iter_last != NULL);
666 iter_last->symbol_next = old_declaration;
667 if(old_declaration != NULL) {
668 old_declaration->symbol_next = iter->symbol_next;
673 assert(iter != NULL);
677 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
680 static void environment_pop_to(size_t new_top)
682 stack_pop_to(&environment_stack, new_top);
685 static void label_pop_to(size_t new_top)
687 stack_pop_to(&label_stack, new_top);
691 static int get_rank(const type_t *type)
693 assert(!is_typeref(type));
694 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
695 * and esp. footnote 108). However we can't fold constants (yet), so we
696 * can't decide whether unsigned int is possible, while int always works.
697 * (unsigned int would be preferable when possible... for stuff like
698 * struct { enum { ... } bla : 4; } ) */
699 if(type->kind == TYPE_ENUM)
700 return ATOMIC_TYPE_INT;
702 assert(type->kind == TYPE_ATOMIC);
703 return type->atomic.akind;
706 static type_t *promote_integer(type_t *type)
708 if(type->kind == TYPE_BITFIELD)
709 type = type->bitfield.base;
711 if(get_rank(type) < ATOMIC_TYPE_INT)
718 * Create a cast expression.
720 * @param expression the expression to cast
721 * @param dest_type the destination type
723 static expression_t *create_cast_expression(expression_t *expression,
726 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
728 cast->unary.value = expression;
729 cast->base.type = dest_type;
735 * Check if a given expression represents the 0 pointer constant.
737 static bool is_null_pointer_constant(const expression_t *expression)
739 /* skip void* cast */
740 if(expression->kind == EXPR_UNARY_CAST
741 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
742 expression = expression->unary.value;
745 /* TODO: not correct yet, should be any constant integer expression
746 * which evaluates to 0 */
747 if (expression->kind != EXPR_CONST)
750 type_t *const type = skip_typeref(expression->base.type);
751 if (!is_type_integer(type))
754 return expression->conste.v.int_value == 0;
758 * Create an implicit cast expression.
760 * @param expression the expression to cast
761 * @param dest_type the destination type
763 static expression_t *create_implicit_cast(expression_t *expression,
766 type_t *const source_type = expression->base.type;
768 if (source_type == dest_type)
771 return create_cast_expression(expression, dest_type);
774 /** Implements the rules from § 6.5.16.1 */
775 static type_t *semantic_assign(type_t *orig_type_left,
776 const expression_t *const right,
779 type_t *const orig_type_right = right->base.type;
780 type_t *const type_left = skip_typeref(orig_type_left);
781 type_t *const type_right = skip_typeref(orig_type_right);
783 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
784 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
785 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
786 && is_type_pointer(type_right))) {
787 return orig_type_left;
790 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
791 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
792 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
794 /* the left type has all qualifiers from the right type */
795 unsigned missing_qualifiers
796 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
797 if(missing_qualifiers != 0) {
798 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
799 return orig_type_left;
802 points_to_left = get_unqualified_type(points_to_left);
803 points_to_right = get_unqualified_type(points_to_right);
805 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
806 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
807 return orig_type_left;
810 if (!types_compatible(points_to_left, points_to_right)) {
811 warningf(right->base.source_position,
812 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
813 orig_type_left, context, right, orig_type_right);
816 return orig_type_left;
819 if ((is_type_compound(type_left) && is_type_compound(type_right))
820 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
821 type_t *const unqual_type_left = get_unqualified_type(type_left);
822 type_t *const unqual_type_right = get_unqualified_type(type_right);
823 if (types_compatible(unqual_type_left, unqual_type_right)) {
824 return orig_type_left;
828 if (!is_type_valid(type_left))
831 if (!is_type_valid(type_right))
832 return orig_type_right;
837 static expression_t *parse_constant_expression(void)
839 /* start parsing at precedence 7 (conditional expression) */
840 expression_t *result = parse_sub_expression(7);
842 if(!is_constant_expression(result)) {
843 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
849 static expression_t *parse_assignment_expression(void)
851 /* start parsing at precedence 2 (assignment expression) */
852 return parse_sub_expression(2);
855 static type_t *make_global_typedef(const char *name, type_t *type)
857 symbol_t *const symbol = symbol_table_insert(name);
859 declaration_t *const declaration = allocate_declaration_zero();
860 declaration->namespc = NAMESPACE_NORMAL;
861 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
862 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
863 declaration->type = type;
864 declaration->symbol = symbol;
865 declaration->source_position = builtin_source_position;
867 record_declaration(declaration);
869 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
870 typedef_type->typedeft.declaration = declaration;
875 static string_t parse_string_literals(void)
877 assert(token.type == T_STRING_LITERAL);
878 string_t result = token.v.string;
882 while (token.type == T_STRING_LITERAL) {
883 result = concat_strings(&result, &token.v.string);
890 static void parse_attributes(void)
894 case T___attribute__: {
902 errorf(HERE, "EOF while parsing attribute");
921 if(token.type != T_STRING_LITERAL) {
922 parse_error_expected("while parsing assembler attribute",
927 parse_string_literals();
932 goto attributes_finished;
941 static designator_t *parse_designation(void)
943 designator_t *result = NULL;
944 designator_t *last = NULL;
947 designator_t *designator;
950 designator = allocate_ast_zero(sizeof(designator[0]));
951 designator->source_position = token.source_position;
953 designator->array_index = parse_constant_expression();
957 designator = allocate_ast_zero(sizeof(designator[0]));
958 designator->source_position = token.source_position;
960 if(token.type != T_IDENTIFIER) {
961 parse_error_expected("while parsing designator",
965 designator->symbol = token.v.symbol;
973 assert(designator != NULL);
975 last->next = designator;
985 static initializer_t *initializer_from_string(array_type_t *type,
986 const string_t *const string)
988 /* TODO: check len vs. size of array type */
991 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
992 initializer->string.string = *string;
997 static initializer_t *initializer_from_wide_string(array_type_t *const type,
998 wide_string_t *const string)
1000 /* TODO: check len vs. size of array type */
1003 initializer_t *const initializer =
1004 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1005 initializer->wide_string.string = *string;
1011 * Build an initializer from a given expression.
1013 static initializer_t *initializer_from_expression(type_t *orig_type,
1014 expression_t *expression)
1016 /* TODO check that expression is a constant expression */
1018 /* § 6.7.8.14/15 char array may be initialized by string literals */
1019 type_t *type = skip_typeref(orig_type);
1020 type_t *expr_type_orig = expression->base.type;
1021 type_t *expr_type = skip_typeref(expr_type_orig);
1022 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1023 array_type_t *const array_type = &type->array;
1024 type_t *const element_type = skip_typeref(array_type->element_type);
1026 if (element_type->kind == TYPE_ATOMIC) {
1027 atomic_type_kind_t akind = element_type->atomic.akind;
1028 switch (expression->kind) {
1029 case EXPR_STRING_LITERAL:
1030 if (akind == ATOMIC_TYPE_CHAR
1031 || akind == ATOMIC_TYPE_SCHAR
1032 || akind == ATOMIC_TYPE_UCHAR) {
1033 return initializer_from_string(array_type,
1034 &expression->string.value);
1037 case EXPR_WIDE_STRING_LITERAL: {
1038 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1039 if (get_unqualified_type(element_type) == bare_wchar_type) {
1040 return initializer_from_wide_string(array_type,
1041 &expression->wide_string.value);
1051 type_t *const res_type = semantic_assign(type, expression, "initializer");
1052 if (res_type == NULL)
1055 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1056 result->value.value = create_implicit_cast(expression, res_type);
1062 * Checks if a given expression can be used as an constant initializer.
1064 static bool is_initializer_constant(const expression_t *expression)
1066 return is_constant_expression(expression)
1067 || is_address_constant(expression);
1071 * Parses an scalar initializer.
1073 * § 6.7.8.11; eat {} without warning
1075 static initializer_t *parse_scalar_initializer(type_t *type,
1076 bool must_be_constant)
1078 /* there might be extra {} hierarchies */
1080 while(token.type == '{') {
1083 warningf(HERE, "extra curly braces around scalar initializer");
1088 expression_t *expression = parse_assignment_expression();
1089 if(must_be_constant && !is_initializer_constant(expression)) {
1090 errorf(expression->base.source_position,
1091 "Initialisation expression '%E' is not constant\n",
1095 initializer_t *initializer = initializer_from_expression(type, expression);
1097 if(initializer == NULL) {
1098 errorf(expression->base.source_position,
1099 "expression '%E' doesn't match expected type '%T'",
1105 bool additional_warning_displayed = false;
1107 if(token.type == ',') {
1110 if(token.type != '}') {
1111 if(!additional_warning_displayed) {
1112 warningf(HERE, "additional elements in scalar initializer");
1113 additional_warning_displayed = true;
1124 * An entry in the type path.
1126 typedef struct type_path_entry_t type_path_entry_t;
1127 struct type_path_entry_t {
1128 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1130 size_t index; /**< For array types: the current index. */
1131 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1136 * A type path expression a position inside compound or array types.
1138 typedef struct type_path_t type_path_t;
1139 struct type_path_t {
1140 type_path_entry_t *path; /**< An flexible array containing the current path. */
1141 type_t *top_type; /**< type of the element the path points */
1142 size_t max_index; /**< largest index in outermost array */
1146 * Prints a type path for debugging.
1148 static __attribute__((unused)) void debug_print_type_path(
1149 const type_path_t *path)
1151 size_t len = ARR_LEN(path->path);
1153 for(size_t i = 0; i < len; ++i) {
1154 const type_path_entry_t *entry = & path->path[i];
1156 type_t *type = skip_typeref(entry->type);
1157 if(is_type_compound(type)) {
1158 /* in gcc mode structs can have no members */
1159 if(entry->v.compound_entry == NULL) {
1163 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1164 } else if(is_type_array(type)) {
1165 fprintf(stderr, "[%u]", entry->v.index);
1167 fprintf(stderr, "-INVALID-");
1170 if(path->top_type != NULL) {
1171 fprintf(stderr, " (");
1172 print_type(path->top_type);
1173 fprintf(stderr, ")");
1178 * Return the top type path entry, ie. in a path
1179 * (type).a.b returns the b.
1181 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1183 size_t len = ARR_LEN(path->path);
1185 return &path->path[len-1];
1189 * Enlarge the type path by an (empty) element.
1191 static type_path_entry_t *append_to_type_path(type_path_t *path)
1193 size_t len = ARR_LEN(path->path);
1194 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1196 type_path_entry_t *result = & path->path[len];
1197 memset(result, 0, sizeof(result[0]));
1202 * Descending into a sub-type. Enter the scope of the current
1205 static void descend_into_subtype(type_path_t *path)
1207 type_t *orig_top_type = path->top_type;
1208 type_t *top_type = skip_typeref(orig_top_type);
1210 assert(is_type_compound(top_type) || is_type_array(top_type));
1212 type_path_entry_t *top = append_to_type_path(path);
1213 top->type = top_type;
1215 if(is_type_compound(top_type)) {
1216 declaration_t *declaration = top_type->compound.declaration;
1217 declaration_t *entry = declaration->scope.declarations;
1218 top->v.compound_entry = entry;
1221 path->top_type = entry->type;
1223 path->top_type = NULL;
1226 assert(is_type_array(top_type));
1229 path->top_type = top_type->array.element_type;
1234 * Pop an entry from the given type path, ie. returning from
1235 * (type).a.b to (type).a
1237 static void ascend_from_subtype(type_path_t *path)
1239 type_path_entry_t *top = get_type_path_top(path);
1241 path->top_type = top->type;
1243 size_t len = ARR_LEN(path->path);
1244 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1248 * Pop entries from the given type path until the given
1249 * path level is reached.
1251 static void ascend_to(type_path_t *path, size_t top_path_level)
1253 size_t len = ARR_LEN(path->path);
1255 while(len > top_path_level) {
1256 ascend_from_subtype(path);
1257 len = ARR_LEN(path->path);
1261 static bool walk_designator(type_path_t *path, const designator_t *designator,
1262 bool used_in_offsetof)
1264 for( ; designator != NULL; designator = designator->next) {
1265 type_path_entry_t *top = get_type_path_top(path);
1266 type_t *orig_type = top->type;
1268 type_t *type = skip_typeref(orig_type);
1270 if(designator->symbol != NULL) {
1271 symbol_t *symbol = designator->symbol;
1272 if(!is_type_compound(type)) {
1273 if(is_type_valid(type)) {
1274 errorf(designator->source_position,
1275 "'.%Y' designator used for non-compound type '%T'",
1281 declaration_t *declaration = type->compound.declaration;
1282 declaration_t *iter = declaration->scope.declarations;
1283 for( ; iter != NULL; iter = iter->next) {
1284 if(iter->symbol == symbol) {
1289 errorf(designator->source_position,
1290 "'%T' has no member named '%Y'", orig_type, symbol);
1293 if(used_in_offsetof) {
1294 type_t *real_type = skip_typeref(iter->type);
1295 if(real_type->kind == TYPE_BITFIELD) {
1296 errorf(designator->source_position,
1297 "offsetof designator '%Y' may not specify bitfield",
1303 top->type = orig_type;
1304 top->v.compound_entry = iter;
1305 orig_type = iter->type;
1307 expression_t *array_index = designator->array_index;
1308 assert(designator->array_index != NULL);
1310 if(!is_type_array(type)) {
1311 if(is_type_valid(type)) {
1312 errorf(designator->source_position,
1313 "[%E] designator used for non-array type '%T'",
1314 array_index, orig_type);
1318 if(!is_type_valid(array_index->base.type)) {
1322 long index = fold_constant(array_index);
1323 if(!used_in_offsetof) {
1325 errorf(designator->source_position,
1326 "array index [%E] must be positive", array_index);
1329 if(type->array.size_constant == true) {
1330 long array_size = type->array.size;
1331 if(index >= array_size) {
1332 errorf(designator->source_position,
1333 "designator [%E] (%d) exceeds array size %d",
1334 array_index, index, array_size);
1340 top->type = orig_type;
1341 top->v.index = (size_t) index;
1342 orig_type = type->array.element_type;
1344 path->top_type = orig_type;
1346 if(designator->next != NULL) {
1347 descend_into_subtype(path);
1356 static void advance_current_object(type_path_t *path, size_t top_path_level)
1358 type_path_entry_t *top = get_type_path_top(path);
1360 type_t *type = skip_typeref(top->type);
1361 if(is_type_union(type)) {
1362 /* in unions only the first element is initialized */
1363 top->v.compound_entry = NULL;
1364 } else if(is_type_struct(type)) {
1365 declaration_t *entry = top->v.compound_entry;
1367 entry = entry->next;
1368 top->v.compound_entry = entry;
1370 path->top_type = entry->type;
1374 assert(is_type_array(type));
1378 if(!type->array.size_constant || top->v.index < type->array.size) {
1383 /* we're past the last member of the current sub-aggregate, try if we
1384 * can ascend in the type hierarchy and continue with another subobject */
1385 size_t len = ARR_LEN(path->path);
1387 if(len > top_path_level) {
1388 ascend_from_subtype(path);
1389 advance_current_object(path, top_path_level);
1391 path->top_type = NULL;
1396 * skip any {...} blocks until a closing braket is reached.
1398 static void skip_initializers(void)
1400 if(token.type == '{')
1403 while(token.type != '}') {
1404 if(token.type == T_EOF)
1406 if(token.type == '{') {
1415 * Parse a part of an initialiser for a struct or union,
1417 static initializer_t *parse_sub_initializer(type_path_t *path,
1418 type_t *outer_type, size_t top_path_level,
1419 parse_initializer_env_t *env)
1421 if(token.type == '}') {
1422 /* empty initializer */
1426 type_t *orig_type = path->top_type;
1427 type_t *type = NULL;
1429 if (orig_type == NULL) {
1430 /* We are initializing an empty compound. */
1432 type = skip_typeref(orig_type);
1434 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1435 * initializers in this case. */
1436 if(!is_type_valid(type)) {
1437 skip_initializers();
1442 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1445 designator_t *designator = NULL;
1446 if(token.type == '.' || token.type == '[') {
1447 designator = parse_designation();
1449 /* reset path to toplevel, evaluate designator from there */
1450 ascend_to(path, top_path_level);
1451 if(!walk_designator(path, designator, false)) {
1452 /* can't continue after designation error */
1456 initializer_t *designator_initializer
1457 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1458 designator_initializer->designator.designator = designator;
1459 ARR_APP1(initializer_t*, initializers, designator_initializer);
1464 if(token.type == '{') {
1465 if(type != NULL && is_type_scalar(type)) {
1466 sub = parse_scalar_initializer(type, env->must_be_constant);
1470 if (env->declaration != NULL)
1471 errorf(HERE, "extra brace group at end of initializer for '%Y'",
1472 env->declaration->symbol);
1474 errorf(HERE, "extra brace group at end of initializer");
1476 descend_into_subtype(path);
1478 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1482 ascend_from_subtype(path);
1486 goto error_parse_next;
1490 /* must be an expression */
1491 expression_t *expression = parse_assignment_expression();
1493 if(env->must_be_constant && !is_initializer_constant(expression)) {
1494 errorf(expression->base.source_position,
1495 "Initialisation expression '%E' is not constant\n",
1500 /* we are already outside, ... */
1504 /* handle { "string" } special case */
1505 if((expression->kind == EXPR_STRING_LITERAL
1506 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1507 && outer_type != NULL) {
1508 sub = initializer_from_expression(outer_type, expression);
1510 if(token.type == ',') {
1513 if(token.type != '}') {
1514 warningf(HERE, "excessive elements in initializer for type '%T'",
1517 /* TODO: eat , ... */
1522 /* descend into subtypes until expression matches type */
1524 orig_type = path->top_type;
1525 type = skip_typeref(orig_type);
1527 sub = initializer_from_expression(orig_type, expression);
1531 if(!is_type_valid(type)) {
1534 if(is_type_scalar(type)) {
1535 errorf(expression->base.source_position,
1536 "expression '%E' doesn't match expected type '%T'",
1537 expression, orig_type);
1541 descend_into_subtype(path);
1545 /* update largest index of top array */
1546 const type_path_entry_t *first = &path->path[0];
1547 type_t *first_type = first->type;
1548 first_type = skip_typeref(first_type);
1549 if(is_type_array(first_type)) {
1550 size_t index = first->v.index;
1551 if(index > path->max_index)
1552 path->max_index = index;
1556 /* append to initializers list */
1557 ARR_APP1(initializer_t*, initializers, sub);
1560 if(env->declaration != NULL)
1561 warningf(HERE, "excess elements in struct initializer for '%Y'",
1562 env->declaration->symbol);
1564 warningf(HERE, "excess elements in struct initializer");
1568 if(token.type == '}') {
1572 if(token.type == '}') {
1577 /* advance to the next declaration if we are not at the end */
1578 advance_current_object(path, top_path_level);
1579 orig_type = path->top_type;
1580 if(orig_type != NULL)
1581 type = skip_typeref(orig_type);
1587 size_t len = ARR_LEN(initializers);
1588 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1589 initializer_t *result = allocate_ast_zero(size);
1590 result->kind = INITIALIZER_LIST;
1591 result->list.len = len;
1592 memcpy(&result->list.initializers, initializers,
1593 len * sizeof(initializers[0]));
1595 DEL_ARR_F(initializers);
1596 ascend_to(path, top_path_level);
1601 skip_initializers();
1602 DEL_ARR_F(initializers);
1603 ascend_to(path, top_path_level);
1608 * Parses an initializer. Parsers either a compound literal
1609 * (env->declaration == NULL) or an initializer of a declaration.
1611 static initializer_t *parse_initializer(parse_initializer_env_t *env)
1613 type_t *type = skip_typeref(env->type);
1614 initializer_t *result = NULL;
1617 if(is_type_scalar(type)) {
1618 result = parse_scalar_initializer(type, env->must_be_constant);
1619 } else if(token.type == '{') {
1623 memset(&path, 0, sizeof(path));
1624 path.top_type = env->type;
1625 path.path = NEW_ARR_F(type_path_entry_t, 0);
1627 descend_into_subtype(&path);
1629 result = parse_sub_initializer(&path, env->type, 1, env);
1631 max_index = path.max_index;
1632 DEL_ARR_F(path.path);
1636 /* parse_scalar_initializer() also works in this case: we simply
1637 * have an expression without {} around it */
1638 result = parse_scalar_initializer(type, env->must_be_constant);
1641 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1642 * the array type size */
1643 if(is_type_array(type) && type->array.size_expression == NULL
1644 && result != NULL) {
1646 switch (result->kind) {
1647 case INITIALIZER_LIST:
1648 size = max_index + 1;
1651 case INITIALIZER_STRING:
1652 size = result->string.string.size;
1655 case INITIALIZER_WIDE_STRING:
1656 size = result->wide_string.string.size;
1660 panic("invalid initializer type");
1663 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1664 cnst->base.type = type_size_t;
1665 cnst->conste.v.int_value = size;
1667 type_t *new_type = duplicate_type(type);
1669 new_type->array.size_expression = cnst;
1670 new_type->array.size_constant = true;
1671 new_type->array.size = size;
1672 env->type = new_type;
1680 static declaration_t *append_declaration(declaration_t *declaration);
1682 static declaration_t *parse_compound_type_specifier(bool is_struct)
1690 symbol_t *symbol = NULL;
1691 declaration_t *declaration = NULL;
1693 if (token.type == T___attribute__) {
1698 if(token.type == T_IDENTIFIER) {
1699 symbol = token.v.symbol;
1703 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1705 declaration = get_declaration(symbol, NAMESPACE_UNION);
1707 } else if(token.type != '{') {
1709 parse_error_expected("while parsing struct type specifier",
1710 T_IDENTIFIER, '{', 0);
1712 parse_error_expected("while parsing union type specifier",
1713 T_IDENTIFIER, '{', 0);
1719 if(declaration == NULL) {
1720 declaration = allocate_declaration_zero();
1721 declaration->namespc =
1722 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1723 declaration->source_position = token.source_position;
1724 declaration->symbol = symbol;
1725 declaration->parent_scope = scope;
1726 if (symbol != NULL) {
1727 environment_push(declaration);
1729 append_declaration(declaration);
1732 if(token.type == '{') {
1733 if(declaration->init.is_defined) {
1734 assert(symbol != NULL);
1735 errorf(HERE, "multiple definitions of '%s %Y'",
1736 is_struct ? "struct" : "union", symbol);
1737 declaration->scope.declarations = NULL;
1739 declaration->init.is_defined = true;
1741 parse_compound_type_entries(declaration);
1748 static void parse_enum_entries(type_t *const enum_type)
1752 if(token.type == '}') {
1754 errorf(HERE, "empty enum not allowed");
1759 if(token.type != T_IDENTIFIER) {
1760 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1765 declaration_t *const entry = allocate_declaration_zero();
1766 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1767 entry->type = enum_type;
1768 entry->symbol = token.v.symbol;
1769 entry->source_position = token.source_position;
1772 if(token.type == '=') {
1774 expression_t *value = parse_constant_expression();
1776 value = create_implicit_cast(value, enum_type);
1777 entry->init.enum_value = value;
1782 record_declaration(entry);
1784 if(token.type != ',')
1787 } while(token.type != '}');
1795 static type_t *parse_enum_specifier(void)
1799 declaration_t *declaration;
1802 if(token.type == T_IDENTIFIER) {
1803 symbol = token.v.symbol;
1806 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1807 } else if(token.type != '{') {
1808 parse_error_expected("while parsing enum type specifier",
1809 T_IDENTIFIER, '{', 0);
1816 if(declaration == NULL) {
1817 declaration = allocate_declaration_zero();
1818 declaration->namespc = NAMESPACE_ENUM;
1819 declaration->source_position = token.source_position;
1820 declaration->symbol = symbol;
1821 declaration->parent_scope = scope;
1824 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1825 type->enumt.declaration = declaration;
1827 if(token.type == '{') {
1828 if(declaration->init.is_defined) {
1829 errorf(HERE, "multiple definitions of enum %Y", symbol);
1831 if (symbol != NULL) {
1832 environment_push(declaration);
1834 append_declaration(declaration);
1835 declaration->init.is_defined = 1;
1837 parse_enum_entries(type);
1845 * if a symbol is a typedef to another type, return true
1847 static bool is_typedef_symbol(symbol_t *symbol)
1849 const declaration_t *const declaration =
1850 get_declaration(symbol, NAMESPACE_NORMAL);
1852 declaration != NULL &&
1853 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1856 static type_t *parse_typeof(void)
1864 expression_t *expression = NULL;
1867 switch(token.type) {
1868 case T___extension__:
1869 /* this can be a prefix to a typename or an expression */
1870 /* we simply eat it now. */
1873 } while(token.type == T___extension__);
1877 if(is_typedef_symbol(token.v.symbol)) {
1878 type = parse_typename();
1880 expression = parse_expression();
1881 type = expression->base.type;
1886 type = parse_typename();
1890 expression = parse_expression();
1891 type = expression->base.type;
1897 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1898 typeof_type->typeoft.expression = expression;
1899 typeof_type->typeoft.typeof_type = type;
1907 SPECIFIER_SIGNED = 1 << 0,
1908 SPECIFIER_UNSIGNED = 1 << 1,
1909 SPECIFIER_LONG = 1 << 2,
1910 SPECIFIER_INT = 1 << 3,
1911 SPECIFIER_DOUBLE = 1 << 4,
1912 SPECIFIER_CHAR = 1 << 5,
1913 SPECIFIER_SHORT = 1 << 6,
1914 SPECIFIER_LONG_LONG = 1 << 7,
1915 SPECIFIER_FLOAT = 1 << 8,
1916 SPECIFIER_BOOL = 1 << 9,
1917 SPECIFIER_VOID = 1 << 10,
1918 #ifdef PROVIDE_COMPLEX
1919 SPECIFIER_COMPLEX = 1 << 11,
1920 SPECIFIER_IMAGINARY = 1 << 12,
1924 static type_t *create_builtin_type(symbol_t *const symbol,
1925 type_t *const real_type)
1927 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1928 type->builtin.symbol = symbol;
1929 type->builtin.real_type = real_type;
1931 type_t *result = typehash_insert(type);
1932 if (type != result) {
1939 static type_t *get_typedef_type(symbol_t *symbol)
1941 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1942 if(declaration == NULL
1943 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1946 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1947 type->typedeft.declaration = declaration;
1952 static void parse_microsoft_extended_decl_modifier(void)
1957 switch(token.type) {
1959 symbol = token.v.symbol;
1960 if(symbol == sym_align) {
1963 if (token.type != T_INTEGER)
1965 (void)token.v.intvalue;
1968 } else if(symbol == sym_allocate) {
1971 if (token.type != T_IDENTIFIER)
1973 (void)token.v.symbol;
1975 } else if(symbol == sym_dllimport) {
1977 } else if(symbol == sym_dllimport) {
1979 } else if(symbol == sym_dllexport) {
1981 } else if(symbol == sym_naked) {
1983 } else if(symbol == sym_noinline) {
1985 } else if(symbol == sym_noreturn) {
1987 } else if(symbol == sym_nothrow) {
1989 } else if(symbol == sym_novtable) {
1991 } else if(symbol == sym_property) {
1994 if(token.type != T_IDENTIFIER)
1996 if(token.v.symbol == sym_get) {
1997 } else if(token.v.symbol == sym_put) {
2002 if(token.type != T_IDENTIFIER)
2004 (void)token.v.symbol;
2007 } else if(symbol == sym_selectany) {
2009 } else if(symbol == sym_thread) {
2011 } else if(symbol == sym_uuid) {
2014 if(token.type != T_STRING_LITERAL)
2028 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2030 type_t *type = NULL;
2031 unsigned type_qualifiers = 0;
2032 unsigned type_specifiers = 0;
2035 specifiers->source_position = token.source_position;
2038 switch(token.type) {
2041 #define MATCH_STORAGE_CLASS(token, class) \
2043 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2044 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2046 specifiers->declared_storage_class = class; \
2050 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2051 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2052 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2053 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2054 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2059 parse_microsoft_extended_decl_modifier();
2064 switch (specifiers->declared_storage_class) {
2065 case STORAGE_CLASS_NONE:
2066 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2069 case STORAGE_CLASS_EXTERN:
2070 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2073 case STORAGE_CLASS_STATIC:
2074 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2078 errorf(HERE, "multiple storage classes in declaration specifiers");
2084 /* type qualifiers */
2085 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2087 type_qualifiers |= qualifier; \
2091 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2092 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2093 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2095 case T___extension__:
2100 /* type specifiers */
2101 #define MATCH_SPECIFIER(token, specifier, name) \
2104 if(type_specifiers & specifier) { \
2105 errorf(HERE, "multiple " name " type specifiers given"); \
2107 type_specifiers |= specifier; \
2111 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2112 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2113 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2114 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2115 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2116 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2117 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2118 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2119 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2120 #ifdef PROVIDE_COMPLEX
2121 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2122 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2125 /* only in microsoft mode */
2126 specifiers->decl_modifiers |= DM_FORCEINLINE;
2130 specifiers->is_inline = true;
2135 if(type_specifiers & SPECIFIER_LONG_LONG) {
2136 errorf(HERE, "multiple type specifiers given");
2137 } else if(type_specifiers & SPECIFIER_LONG) {
2138 type_specifiers |= SPECIFIER_LONG_LONG;
2140 type_specifiers |= SPECIFIER_LONG;
2145 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2147 type->compound.declaration = parse_compound_type_specifier(true);
2151 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2153 type->compound.declaration = parse_compound_type_specifier(false);
2157 type = parse_enum_specifier();
2160 type = parse_typeof();
2162 case T___builtin_va_list:
2163 type = duplicate_type(type_valist);
2167 case T___attribute__:
2171 case T_IDENTIFIER: {
2172 /* only parse identifier if we haven't found a type yet */
2173 if(type != NULL || type_specifiers != 0)
2174 goto finish_specifiers;
2176 type_t *typedef_type = get_typedef_type(token.v.symbol);
2178 if(typedef_type == NULL)
2179 goto finish_specifiers;
2182 type = typedef_type;
2186 /* function specifier */
2188 goto finish_specifiers;
2195 atomic_type_kind_t atomic_type;
2197 /* match valid basic types */
2198 switch(type_specifiers) {
2199 case SPECIFIER_VOID:
2200 atomic_type = ATOMIC_TYPE_VOID;
2202 case SPECIFIER_CHAR:
2203 atomic_type = ATOMIC_TYPE_CHAR;
2205 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2206 atomic_type = ATOMIC_TYPE_SCHAR;
2208 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2209 atomic_type = ATOMIC_TYPE_UCHAR;
2211 case SPECIFIER_SHORT:
2212 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2213 case SPECIFIER_SHORT | SPECIFIER_INT:
2214 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2215 atomic_type = ATOMIC_TYPE_SHORT;
2217 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2218 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2219 atomic_type = ATOMIC_TYPE_USHORT;
2222 case SPECIFIER_SIGNED:
2223 case SPECIFIER_SIGNED | SPECIFIER_INT:
2224 atomic_type = ATOMIC_TYPE_INT;
2226 case SPECIFIER_UNSIGNED:
2227 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2228 atomic_type = ATOMIC_TYPE_UINT;
2230 case SPECIFIER_LONG:
2231 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2232 case SPECIFIER_LONG | SPECIFIER_INT:
2233 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2234 atomic_type = ATOMIC_TYPE_LONG;
2236 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2237 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2238 atomic_type = ATOMIC_TYPE_ULONG;
2240 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2241 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2242 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2243 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2245 atomic_type = ATOMIC_TYPE_LONGLONG;
2247 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2248 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2250 atomic_type = ATOMIC_TYPE_ULONGLONG;
2252 case SPECIFIER_FLOAT:
2253 atomic_type = ATOMIC_TYPE_FLOAT;
2255 case SPECIFIER_DOUBLE:
2256 atomic_type = ATOMIC_TYPE_DOUBLE;
2258 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2259 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2261 case SPECIFIER_BOOL:
2262 atomic_type = ATOMIC_TYPE_BOOL;
2264 #ifdef PROVIDE_COMPLEX
2265 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2266 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2268 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2269 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2271 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2272 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2274 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2275 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2277 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2278 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2280 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2281 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2285 /* invalid specifier combination, give an error message */
2286 if(type_specifiers == 0) {
2287 if (! strict_mode) {
2288 if (warning.implicit_int) {
2289 warningf(HERE, "no type specifiers in declaration, using 'int'");
2291 atomic_type = ATOMIC_TYPE_INT;
2294 errorf(HERE, "no type specifiers given in declaration");
2296 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2297 (type_specifiers & SPECIFIER_UNSIGNED)) {
2298 errorf(HERE, "signed and unsigned specifiers gives");
2299 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2300 errorf(HERE, "only integer types can be signed or unsigned");
2302 errorf(HERE, "multiple datatypes in declaration");
2304 atomic_type = ATOMIC_TYPE_INVALID;
2307 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2308 type->atomic.akind = atomic_type;
2311 if(type_specifiers != 0) {
2312 errorf(HERE, "multiple datatypes in declaration");
2316 type->base.qualifiers = type_qualifiers;
2318 type_t *result = typehash_insert(type);
2319 if(newtype && result != type) {
2323 specifiers->type = result;
2328 static type_qualifiers_t parse_type_qualifiers(void)
2330 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2333 switch(token.type) {
2334 /* type qualifiers */
2335 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2336 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2337 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2340 return type_qualifiers;
2345 static declaration_t *parse_identifier_list(void)
2347 declaration_t *declarations = NULL;
2348 declaration_t *last_declaration = NULL;
2350 declaration_t *const declaration = allocate_declaration_zero();
2351 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2352 declaration->source_position = token.source_position;
2353 declaration->symbol = token.v.symbol;
2356 if(last_declaration != NULL) {
2357 last_declaration->next = declaration;
2359 declarations = declaration;
2361 last_declaration = declaration;
2363 if(token.type != ',')
2366 } while(token.type == T_IDENTIFIER);
2368 return declarations;
2371 static void semantic_parameter(declaration_t *declaration)
2373 /* TODO: improve error messages */
2375 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2376 errorf(HERE, "typedef not allowed in parameter list");
2377 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2378 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2379 errorf(HERE, "parameter may only have none or register storage class");
2382 type_t *const orig_type = declaration->type;
2383 type_t * type = skip_typeref(orig_type);
2385 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2386 * into a pointer. § 6.7.5.3 (7) */
2387 if (is_type_array(type)) {
2388 type_t *const element_type = type->array.element_type;
2390 type = make_pointer_type(element_type, type->base.qualifiers);
2392 declaration->type = type;
2395 if(is_type_incomplete(type)) {
2396 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2397 orig_type, declaration->symbol);
2401 static declaration_t *parse_parameter(void)
2403 declaration_specifiers_t specifiers;
2404 memset(&specifiers, 0, sizeof(specifiers));
2406 parse_declaration_specifiers(&specifiers);
2408 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2410 semantic_parameter(declaration);
2415 static declaration_t *parse_parameters(function_type_t *type)
2417 if(token.type == T_IDENTIFIER) {
2418 symbol_t *symbol = token.v.symbol;
2419 if(!is_typedef_symbol(symbol)) {
2420 type->kr_style_parameters = true;
2421 return parse_identifier_list();
2425 if(token.type == ')') {
2426 type->unspecified_parameters = 1;
2429 if(token.type == T_void && look_ahead(1)->type == ')') {
2434 declaration_t *declarations = NULL;
2435 declaration_t *declaration;
2436 declaration_t *last_declaration = NULL;
2437 function_parameter_t *parameter;
2438 function_parameter_t *last_parameter = NULL;
2441 switch(token.type) {
2445 return declarations;
2448 case T___extension__:
2450 declaration = parse_parameter();
2452 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2453 memset(parameter, 0, sizeof(parameter[0]));
2454 parameter->type = declaration->type;
2456 if(last_parameter != NULL) {
2457 last_declaration->next = declaration;
2458 last_parameter->next = parameter;
2460 type->parameters = parameter;
2461 declarations = declaration;
2463 last_parameter = parameter;
2464 last_declaration = declaration;
2468 return declarations;
2470 if(token.type != ',')
2471 return declarations;
2481 } construct_type_kind_t;
2483 typedef struct construct_type_t construct_type_t;
2484 struct construct_type_t {
2485 construct_type_kind_t kind;
2486 construct_type_t *next;
2489 typedef struct parsed_pointer_t parsed_pointer_t;
2490 struct parsed_pointer_t {
2491 construct_type_t construct_type;
2492 type_qualifiers_t type_qualifiers;
2495 typedef struct construct_function_type_t construct_function_type_t;
2496 struct construct_function_type_t {
2497 construct_type_t construct_type;
2498 type_t *function_type;
2501 typedef struct parsed_array_t parsed_array_t;
2502 struct parsed_array_t {
2503 construct_type_t construct_type;
2504 type_qualifiers_t type_qualifiers;
2510 typedef struct construct_base_type_t construct_base_type_t;
2511 struct construct_base_type_t {
2512 construct_type_t construct_type;
2516 static construct_type_t *parse_pointer_declarator(void)
2520 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2521 memset(pointer, 0, sizeof(pointer[0]));
2522 pointer->construct_type.kind = CONSTRUCT_POINTER;
2523 pointer->type_qualifiers = parse_type_qualifiers();
2525 return (construct_type_t*) pointer;
2528 static construct_type_t *parse_array_declarator(void)
2532 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2533 memset(array, 0, sizeof(array[0]));
2534 array->construct_type.kind = CONSTRUCT_ARRAY;
2536 if(token.type == T_static) {
2537 array->is_static = true;
2541 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2542 if(type_qualifiers != 0) {
2543 if(token.type == T_static) {
2544 array->is_static = true;
2548 array->type_qualifiers = type_qualifiers;
2550 if(token.type == '*' && look_ahead(1)->type == ']') {
2551 array->is_variable = true;
2553 } else if(token.type != ']') {
2554 array->size = parse_assignment_expression();
2559 return (construct_type_t*) array;
2564 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2569 if(declaration != NULL) {
2570 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2572 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2575 declaration_t *parameters = parse_parameters(&type->function);
2576 if(declaration != NULL) {
2577 declaration->scope.declarations = parameters;
2580 construct_function_type_t *construct_function_type =
2581 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2582 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2583 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2584 construct_function_type->function_type = type;
2588 return (construct_type_t*) construct_function_type;
2593 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2594 bool may_be_abstract)
2596 /* construct a single linked list of construct_type_t's which describe
2597 * how to construct the final declarator type */
2598 construct_type_t *first = NULL;
2599 construct_type_t *last = NULL;
2602 while(token.type == '*') {
2603 construct_type_t *type = parse_pointer_declarator();
2614 /* TODO: find out if this is correct */
2617 construct_type_t *inner_types = NULL;
2619 switch(token.type) {
2621 if(declaration == NULL) {
2622 errorf(HERE, "no identifier expected in typename");
2624 declaration->symbol = token.v.symbol;
2625 declaration->source_position = token.source_position;
2631 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2637 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2638 /* avoid a loop in the outermost scope, because eat_statement doesn't
2640 if(token.type == '}' && current_function == NULL) {
2648 construct_type_t *p = last;
2651 construct_type_t *type;
2652 switch(token.type) {
2654 type = parse_function_declarator(declaration);
2657 type = parse_array_declarator();
2660 goto declarator_finished;
2663 /* insert in the middle of the list (behind p) */
2665 type->next = p->next;
2676 declarator_finished:
2679 /* append inner_types at the end of the list, we don't to set last anymore
2680 * as it's not needed anymore */
2682 assert(first == NULL);
2683 first = inner_types;
2685 last->next = inner_types;
2693 static type_t *construct_declarator_type(construct_type_t *construct_list,
2696 construct_type_t *iter = construct_list;
2697 for( ; iter != NULL; iter = iter->next) {
2698 switch(iter->kind) {
2699 case CONSTRUCT_INVALID:
2700 panic("invalid type construction found");
2701 case CONSTRUCT_FUNCTION: {
2702 construct_function_type_t *construct_function_type
2703 = (construct_function_type_t*) iter;
2705 type_t *function_type = construct_function_type->function_type;
2707 function_type->function.return_type = type;
2709 type_t *skipped_return_type = skip_typeref(type);
2710 if (is_type_function(skipped_return_type)) {
2711 errorf(HERE, "function returning function is not allowed");
2712 type = type_error_type;
2713 } else if (is_type_array(skipped_return_type)) {
2714 errorf(HERE, "function returning array is not allowed");
2715 type = type_error_type;
2717 type = function_type;
2722 case CONSTRUCT_POINTER: {
2723 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2724 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2725 pointer_type->pointer.points_to = type;
2726 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2728 type = pointer_type;
2732 case CONSTRUCT_ARRAY: {
2733 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2734 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2736 expression_t *size_expression = parsed_array->size;
2737 if(size_expression != NULL) {
2739 = create_implicit_cast(size_expression, type_size_t);
2742 array_type->base.qualifiers = parsed_array->type_qualifiers;
2743 array_type->array.element_type = type;
2744 array_type->array.is_static = parsed_array->is_static;
2745 array_type->array.is_variable = parsed_array->is_variable;
2746 array_type->array.size_expression = size_expression;
2748 if(size_expression != NULL) {
2749 if(is_constant_expression(size_expression)) {
2750 array_type->array.size_constant = true;
2751 array_type->array.size
2752 = fold_constant(size_expression);
2754 array_type->array.is_vla = true;
2758 type_t *skipped_type = skip_typeref(type);
2759 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2760 errorf(HERE, "array of void is not allowed");
2761 type = type_error_type;
2769 type_t *hashed_type = typehash_insert(type);
2770 if(hashed_type != type) {
2771 /* the function type was constructed earlier freeing it here will
2772 * destroy other types... */
2773 if(iter->kind != CONSTRUCT_FUNCTION) {
2783 static declaration_t *parse_declarator(
2784 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2786 declaration_t *const declaration = allocate_declaration_zero();
2787 declaration->declared_storage_class = specifiers->declared_storage_class;
2788 declaration->modifiers = specifiers->decl_modifiers;
2789 declaration->is_inline = specifiers->is_inline;
2791 declaration->storage_class = specifiers->declared_storage_class;
2792 if(declaration->storage_class == STORAGE_CLASS_NONE
2793 && scope != global_scope) {
2794 declaration->storage_class = STORAGE_CLASS_AUTO;
2797 construct_type_t *construct_type
2798 = parse_inner_declarator(declaration, may_be_abstract);
2799 type_t *const type = specifiers->type;
2800 declaration->type = construct_declarator_type(construct_type, type);
2802 if(construct_type != NULL) {
2803 obstack_free(&temp_obst, construct_type);
2809 static type_t *parse_abstract_declarator(type_t *base_type)
2811 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2813 type_t *result = construct_declarator_type(construct_type, base_type);
2814 if(construct_type != NULL) {
2815 obstack_free(&temp_obst, construct_type);
2821 static declaration_t *append_declaration(declaration_t* const declaration)
2823 if (last_declaration != NULL) {
2824 last_declaration->next = declaration;
2826 scope->declarations = declaration;
2828 last_declaration = declaration;
2833 * Check if the declaration of main is suspicious. main should be a
2834 * function with external linkage, returning int, taking either zero
2835 * arguments, two, or three arguments of appropriate types, ie.
2837 * int main([ int argc, char **argv [, char **env ] ]).
2839 * @param decl the declaration to check
2840 * @param type the function type of the declaration
2842 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2844 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2845 warningf(decl->source_position, "'main' is normally a non-static function");
2847 if (skip_typeref(func_type->return_type) != type_int) {
2848 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2850 const function_parameter_t *parm = func_type->parameters;
2852 type_t *const first_type = parm->type;
2853 if (!types_compatible(skip_typeref(first_type), type_int)) {
2854 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2858 type_t *const second_type = parm->type;
2859 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2860 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2864 type_t *const third_type = parm->type;
2865 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2866 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2870 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2874 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2880 * Check if a symbol is the equal to "main".
2882 static bool is_sym_main(const symbol_t *const sym)
2884 return strcmp(sym->string, "main") == 0;
2887 static declaration_t *internal_record_declaration(
2888 declaration_t *const declaration,
2889 const bool is_function_definition)
2891 const symbol_t *const symbol = declaration->symbol;
2892 const namespace_t namespc = (namespace_t)declaration->namespc;
2894 type_t *const orig_type = declaration->type;
2895 type_t *const type = skip_typeref(orig_type);
2896 if (is_type_function(type) &&
2897 type->function.unspecified_parameters &&
2898 warning.strict_prototypes) {
2899 warningf(declaration->source_position,
2900 "function declaration '%#T' is not a prototype",
2901 orig_type, declaration->symbol);
2904 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2905 check_type_of_main(declaration, &type->function);
2908 assert(declaration->symbol != NULL);
2909 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2911 assert(declaration != previous_declaration);
2912 if (previous_declaration != NULL) {
2913 if (previous_declaration->parent_scope == scope) {
2914 /* can happen for K&R style declarations */
2915 if(previous_declaration->type == NULL) {
2916 previous_declaration->type = declaration->type;
2919 const type_t *prev_type = skip_typeref(previous_declaration->type);
2920 if (!types_compatible(type, prev_type)) {
2921 errorf(declaration->source_position,
2922 "declaration '%#T' is incompatible with "
2923 "previous declaration '%#T'",
2924 orig_type, symbol, previous_declaration->type, symbol);
2925 errorf(previous_declaration->source_position,
2926 "previous declaration of '%Y' was here", symbol);
2928 unsigned old_storage_class = previous_declaration->storage_class;
2929 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
2930 errorf(declaration->source_position, "redeclaration of enum entry '%Y'", symbol);
2931 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2932 return previous_declaration;
2935 unsigned new_storage_class = declaration->storage_class;
2937 if(is_type_incomplete(prev_type)) {
2938 previous_declaration->type = type;
2942 /* pretend no storage class means extern for function
2943 * declarations (except if the previous declaration is neither
2944 * none nor extern) */
2945 if (is_type_function(type)) {
2946 switch (old_storage_class) {
2947 case STORAGE_CLASS_NONE:
2948 old_storage_class = STORAGE_CLASS_EXTERN;
2950 case STORAGE_CLASS_EXTERN:
2951 if (is_function_definition) {
2952 if (warning.missing_prototypes &&
2953 prev_type->function.unspecified_parameters &&
2954 !is_sym_main(symbol)) {
2955 warningf(declaration->source_position,
2956 "no previous prototype for '%#T'",
2959 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2960 new_storage_class = STORAGE_CLASS_EXTERN;
2968 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2969 new_storage_class == STORAGE_CLASS_EXTERN) {
2970 warn_redundant_declaration:
2971 if (warning.redundant_decls) {
2972 warningf(declaration->source_position,
2973 "redundant declaration for '%Y'", symbol);
2974 warningf(previous_declaration->source_position,
2975 "previous declaration of '%Y' was here",
2978 } else if (current_function == NULL) {
2979 if (old_storage_class != STORAGE_CLASS_STATIC &&
2980 new_storage_class == STORAGE_CLASS_STATIC) {
2981 errorf(declaration->source_position,
2982 "static declaration of '%Y' follows non-static declaration",
2984 errorf(previous_declaration->source_position,
2985 "previous declaration of '%Y' was here", symbol);
2987 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2988 goto warn_redundant_declaration;
2990 if (new_storage_class == STORAGE_CLASS_NONE) {
2991 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2992 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
2996 if (old_storage_class == new_storage_class) {
2997 errorf(declaration->source_position,
2998 "redeclaration of '%Y'", symbol);
3000 errorf(declaration->source_position,
3001 "redeclaration of '%Y' with different linkage",
3004 errorf(previous_declaration->source_position,
3005 "previous declaration of '%Y' was here", symbol);
3008 return previous_declaration;
3010 } else if (is_function_definition) {
3011 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3012 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3013 warningf(declaration->source_position,
3014 "no previous prototype for '%#T'", orig_type, symbol);
3015 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3016 warningf(declaration->source_position,
3017 "no previous declaration for '%#T'", orig_type,
3021 } else if (warning.missing_declarations &&
3022 scope == global_scope &&
3023 !is_type_function(type) && (
3024 declaration->storage_class == STORAGE_CLASS_NONE ||
3025 declaration->storage_class == STORAGE_CLASS_THREAD
3027 warningf(declaration->source_position,
3028 "no previous declaration for '%#T'", orig_type, symbol);
3031 assert(declaration->parent_scope == NULL);
3032 assert(scope != NULL);
3034 declaration->parent_scope = scope;
3036 environment_push(declaration);
3037 return append_declaration(declaration);
3040 static declaration_t *record_declaration(declaration_t *declaration)
3042 return internal_record_declaration(declaration, false);
3045 static declaration_t *record_function_definition(declaration_t *declaration)
3047 return internal_record_declaration(declaration, true);
3050 static void parser_error_multiple_definition(declaration_t *declaration,
3051 const source_position_t source_position)
3053 errorf(source_position, "multiple definition of symbol '%Y'",
3054 declaration->symbol);
3055 errorf(declaration->source_position,
3056 "this is the location of the previous definition.");
3059 static bool is_declaration_specifier(const token_t *token,
3060 bool only_type_specifiers)
3062 switch(token->type) {
3066 return is_typedef_symbol(token->v.symbol);
3068 case T___extension__:
3071 return !only_type_specifiers;
3078 static void parse_init_declarator_rest(declaration_t *declaration)
3082 type_t *orig_type = declaration->type;
3083 type_t *type = skip_typeref(orig_type);
3085 if(declaration->init.initializer != NULL) {
3086 parser_error_multiple_definition(declaration, token.source_position);
3089 bool must_be_constant = false;
3090 if(declaration->storage_class == STORAGE_CLASS_STATIC
3091 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3092 || declaration->parent_scope == global_scope) {
3093 must_be_constant = true;
3096 parse_initializer_env_t env;
3097 env.type = orig_type;
3098 env.must_be_constant = must_be_constant;
3099 env.declaration = declaration;
3101 initializer_t *initializer = parse_initializer(&env);
3103 if(env.type != orig_type) {
3104 orig_type = env.type;
3105 type = skip_typeref(orig_type);
3106 declaration->type = env.type;
3109 if(is_type_function(type)) {
3110 errorf(declaration->source_position,
3111 "initializers not allowed for function types at declator '%Y' (type '%T')",
3112 declaration->symbol, orig_type);
3114 declaration->init.initializer = initializer;
3118 /* parse rest of a declaration without any declarator */
3119 static void parse_anonymous_declaration_rest(
3120 const declaration_specifiers_t *specifiers,
3121 parsed_declaration_func finished_declaration)
3125 declaration_t *const declaration = allocate_declaration_zero();
3126 declaration->type = specifiers->type;
3127 declaration->declared_storage_class = specifiers->declared_storage_class;
3128 declaration->source_position = specifiers->source_position;
3130 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3131 warningf(declaration->source_position, "useless storage class in empty declaration");
3133 declaration->storage_class = STORAGE_CLASS_NONE;
3135 type_t *type = declaration->type;
3136 switch (type->kind) {
3137 case TYPE_COMPOUND_STRUCT:
3138 case TYPE_COMPOUND_UNION: {
3139 if (type->compound.declaration->symbol == NULL) {
3140 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
3149 warningf(declaration->source_position, "empty declaration");
3153 finished_declaration(declaration);
3156 static void parse_declaration_rest(declaration_t *ndeclaration,
3157 const declaration_specifiers_t *specifiers,
3158 parsed_declaration_func finished_declaration)
3161 declaration_t *declaration = finished_declaration(ndeclaration);
3163 type_t *orig_type = declaration->type;
3164 type_t *type = skip_typeref(orig_type);
3166 if (type->kind != TYPE_FUNCTION &&
3167 declaration->is_inline &&
3168 is_type_valid(type)) {
3169 warningf(declaration->source_position,
3170 "variable '%Y' declared 'inline'\n", declaration->symbol);
3173 if(token.type == '=') {
3174 parse_init_declarator_rest(declaration);
3177 if(token.type != ',')
3181 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
3189 static declaration_t *finished_kr_declaration(declaration_t *declaration)
3191 symbol_t *symbol = declaration->symbol;
3192 if(symbol == NULL) {
3193 errorf(HERE, "anonymous declaration not valid as function parameter");
3196 namespace_t namespc = (namespace_t) declaration->namespc;
3197 if(namespc != NAMESPACE_NORMAL) {
3198 return record_declaration(declaration);
3201 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3202 if(previous_declaration == NULL ||
3203 previous_declaration->parent_scope != scope) {
3204 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3209 if(previous_declaration->type == NULL) {
3210 previous_declaration->type = declaration->type;
3211 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3212 previous_declaration->storage_class = declaration->storage_class;
3213 previous_declaration->parent_scope = scope;
3214 return previous_declaration;
3216 return record_declaration(declaration);
3220 static void parse_declaration(parsed_declaration_func finished_declaration)
3222 declaration_specifiers_t specifiers;
3223 memset(&specifiers, 0, sizeof(specifiers));
3224 parse_declaration_specifiers(&specifiers);
3226 if(token.type == ';') {
3227 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3229 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3230 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3234 static void parse_kr_declaration_list(declaration_t *declaration)
3236 type_t *type = skip_typeref(declaration->type);
3237 if(!is_type_function(type))
3240 if(!type->function.kr_style_parameters)
3243 /* push function parameters */
3244 int top = environment_top();
3245 scope_t *last_scope = scope;
3246 set_scope(&declaration->scope);
3248 declaration_t *parameter = declaration->scope.declarations;
3249 for( ; parameter != NULL; parameter = parameter->next) {
3250 assert(parameter->parent_scope == NULL);
3251 parameter->parent_scope = scope;
3252 environment_push(parameter);
3255 /* parse declaration list */
3256 while(is_declaration_specifier(&token, false)) {
3257 parse_declaration(finished_kr_declaration);
3260 /* pop function parameters */
3261 assert(scope == &declaration->scope);
3262 set_scope(last_scope);
3263 environment_pop_to(top);
3265 /* update function type */
3266 type_t *new_type = duplicate_type(type);
3267 new_type->function.kr_style_parameters = false;
3269 function_parameter_t *parameters = NULL;
3270 function_parameter_t *last_parameter = NULL;
3272 declaration_t *parameter_declaration = declaration->scope.declarations;
3273 for( ; parameter_declaration != NULL;
3274 parameter_declaration = parameter_declaration->next) {
3275 type_t *parameter_type = parameter_declaration->type;
3276 if(parameter_type == NULL) {
3278 errorf(HERE, "no type specified for function parameter '%Y'",
3279 parameter_declaration->symbol);
3281 if (warning.implicit_int) {
3282 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3283 parameter_declaration->symbol);
3285 parameter_type = type_int;
3286 parameter_declaration->type = parameter_type;
3290 semantic_parameter(parameter_declaration);
3291 parameter_type = parameter_declaration->type;
3293 function_parameter_t *function_parameter
3294 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3295 memset(function_parameter, 0, sizeof(function_parameter[0]));
3297 function_parameter->type = parameter_type;
3298 if(last_parameter != NULL) {
3299 last_parameter->next = function_parameter;
3301 parameters = function_parameter;
3303 last_parameter = function_parameter;
3305 new_type->function.parameters = parameters;
3307 type = typehash_insert(new_type);
3308 if(type != new_type) {
3309 obstack_free(type_obst, new_type);
3312 declaration->type = type;
3315 static bool first_err = true;
3318 * When called with first_err set, prints the name of the current function,
3321 static void print_in_function(void) {
3324 diagnosticf("%s: In function '%Y':\n",
3325 current_function->source_position.input_name,
3326 current_function->symbol);
3331 * Check if all labels are defined in the current function.
3332 * Check if all labels are used in the current function.
3334 static void check_labels(void)
3336 for (const goto_statement_t *goto_statement = goto_first;
3337 goto_statement != NULL;
3338 goto_statement = goto_statement->next) {
3339 declaration_t *label = goto_statement->label;
3342 if (label->source_position.input_name == NULL) {
3343 print_in_function();
3344 errorf(goto_statement->base.source_position,
3345 "label '%Y' used but not defined", label->symbol);
3348 goto_first = goto_last = NULL;
3350 if (warning.unused_label) {
3351 for (const label_statement_t *label_statement = label_first;
3352 label_statement != NULL;
3353 label_statement = label_statement->next) {
3354 const declaration_t *label = label_statement->label;
3356 if (! label->used) {
3357 print_in_function();
3358 warningf(label_statement->base.source_position,
3359 "label '%Y' defined but not used", label->symbol);
3363 label_first = label_last = NULL;
3367 * Check declarations of current_function for unused entities.
3369 static void check_declarations(void)
3371 if (warning.unused_parameter) {
3372 const scope_t *scope = ¤t_function->scope;
3374 const declaration_t *parameter = scope->declarations;
3375 for (; parameter != NULL; parameter = parameter->next) {
3376 if (! parameter->used) {
3377 print_in_function();
3378 warningf(parameter->source_position,
3379 "unused parameter '%Y'", parameter->symbol);
3383 if (warning.unused_variable) {
3387 static void parse_external_declaration(void)
3389 /* function-definitions and declarations both start with declaration
3391 declaration_specifiers_t specifiers;
3392 memset(&specifiers, 0, sizeof(specifiers));
3393 parse_declaration_specifiers(&specifiers);
3395 /* must be a declaration */
3396 if(token.type == ';') {
3397 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3401 /* declarator is common to both function-definitions and declarations */
3402 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3404 /* must be a declaration */
3405 if(token.type == ',' || token.type == '=' || token.type == ';') {
3406 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3410 /* must be a function definition */
3411 parse_kr_declaration_list(ndeclaration);
3413 if(token.type != '{') {
3414 parse_error_expected("while parsing function definition", '{', 0);
3419 type_t *type = ndeclaration->type;
3421 /* note that we don't skip typerefs: the standard doesn't allow them here
3422 * (so we can't use is_type_function here) */
3423 if(type->kind != TYPE_FUNCTION) {
3424 if (is_type_valid(type)) {
3425 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3426 type, ndeclaration->symbol);
3432 /* § 6.7.5.3 (14) a function definition with () means no
3433 * parameters (and not unspecified parameters) */
3434 if(type->function.unspecified_parameters) {
3435 type_t *duplicate = duplicate_type(type);
3436 duplicate->function.unspecified_parameters = false;
3438 type = typehash_insert(duplicate);
3439 if(type != duplicate) {
3440 obstack_free(type_obst, duplicate);
3442 ndeclaration->type = type;
3445 declaration_t *const declaration = record_function_definition(ndeclaration);
3446 if(ndeclaration != declaration) {
3447 declaration->scope = ndeclaration->scope;
3449 type = skip_typeref(declaration->type);
3451 /* push function parameters and switch scope */
3452 int top = environment_top();
3453 scope_t *last_scope = scope;
3454 set_scope(&declaration->scope);
3456 declaration_t *parameter = declaration->scope.declarations;
3457 for( ; parameter != NULL; parameter = parameter->next) {
3458 if(parameter->parent_scope == &ndeclaration->scope) {
3459 parameter->parent_scope = scope;
3461 assert(parameter->parent_scope == NULL
3462 || parameter->parent_scope == scope);
3463 parameter->parent_scope = scope;
3464 environment_push(parameter);
3467 if(declaration->init.statement != NULL) {
3468 parser_error_multiple_definition(declaration, token.source_position);
3470 goto end_of_parse_external_declaration;
3472 /* parse function body */
3473 int label_stack_top = label_top();
3474 declaration_t *old_current_function = current_function;
3475 current_function = declaration;
3477 declaration->init.statement = parse_compound_statement();
3480 check_declarations();
3482 assert(current_function == declaration);
3483 current_function = old_current_function;
3484 label_pop_to(label_stack_top);
3487 end_of_parse_external_declaration:
3488 assert(scope == &declaration->scope);
3489 set_scope(last_scope);
3490 environment_pop_to(top);
3493 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3494 source_position_t source_position)
3496 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3497 type->bitfield.base = base;
3498 type->bitfield.size = size;
3503 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3506 declaration_t *iter = compound_declaration->scope.declarations;
3507 for( ; iter != NULL; iter = iter->next) {
3508 if(iter->namespc != NAMESPACE_NORMAL)
3511 if(iter->symbol == NULL) {
3512 type_t *type = skip_typeref(iter->type);
3513 if(is_type_compound(type)) {
3514 declaration_t *result
3515 = find_compound_entry(type->compound.declaration, symbol);
3522 if(iter->symbol == symbol) {
3530 static void parse_compound_declarators(declaration_t *struct_declaration,
3531 const declaration_specifiers_t *specifiers)
3533 declaration_t *last_declaration = struct_declaration->scope.declarations;
3534 if(last_declaration != NULL) {
3535 while(last_declaration->next != NULL) {
3536 last_declaration = last_declaration->next;
3541 declaration_t *declaration;
3543 if(token.type == ':') {
3544 source_position_t source_position = HERE;
3547 type_t *base_type = specifiers->type;
3548 expression_t *size = parse_constant_expression();
3550 if(!is_type_integer(skip_typeref(base_type))) {
3551 errorf(HERE, "bitfield base type '%T' is not an integer type",
3555 type_t *type = make_bitfield_type(base_type, size, source_position);
3557 declaration = allocate_declaration_zero();
3558 declaration->namespc = NAMESPACE_NORMAL;
3559 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3560 declaration->storage_class = STORAGE_CLASS_NONE;
3561 declaration->source_position = source_position;
3562 declaration->modifiers = specifiers->decl_modifiers;
3563 declaration->type = type;
3565 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3567 type_t *orig_type = declaration->type;
3568 type_t *type = skip_typeref(orig_type);
3570 if(token.type == ':') {
3571 source_position_t source_position = HERE;
3573 expression_t *size = parse_constant_expression();
3575 if(!is_type_integer(type)) {
3576 errorf(HERE, "bitfield base type '%T' is not an "
3577 "integer type", orig_type);
3580 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3581 declaration->type = bitfield_type;
3583 /* TODO we ignore arrays for now... what is missing is a check
3584 * that they're at the end of the struct */
3585 if(is_type_incomplete(type) && !is_type_array(type)) {
3587 "compound member '%Y' has incomplete type '%T'",
3588 declaration->symbol, orig_type);
3589 } else if(is_type_function(type)) {
3590 errorf(HERE, "compound member '%Y' must not have function "
3591 "type '%T'", declaration->symbol, orig_type);
3596 /* make sure we don't define a symbol multiple times */
3597 symbol_t *symbol = declaration->symbol;
3598 if(symbol != NULL) {
3599 declaration_t *prev_decl
3600 = find_compound_entry(struct_declaration, symbol);
3602 if(prev_decl != NULL) {
3603 assert(prev_decl->symbol == symbol);
3604 errorf(declaration->source_position,
3605 "multiple declarations of symbol '%Y'", symbol);
3606 errorf(prev_decl->source_position,
3607 "previous declaration of '%Y' was here", symbol);
3611 /* append declaration */
3612 if(last_declaration != NULL) {
3613 last_declaration->next = declaration;
3615 struct_declaration->scope.declarations = declaration;
3617 last_declaration = declaration;
3619 if(token.type != ',')
3629 static void parse_compound_type_entries(declaration_t *compound_declaration)
3633 while(token.type != '}' && token.type != T_EOF) {
3634 declaration_specifiers_t specifiers;
3635 memset(&specifiers, 0, sizeof(specifiers));
3636 parse_declaration_specifiers(&specifiers);
3638 parse_compound_declarators(compound_declaration, &specifiers);
3640 if(token.type == T_EOF) {
3641 errorf(HERE, "EOF while parsing struct");
3646 static type_t *parse_typename(void)
3648 declaration_specifiers_t specifiers;
3649 memset(&specifiers, 0, sizeof(specifiers));
3650 parse_declaration_specifiers(&specifiers);
3651 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3652 /* TODO: improve error message, user does probably not know what a
3653 * storage class is...
3655 errorf(HERE, "typename may not have a storage class");
3658 type_t *result = parse_abstract_declarator(specifiers.type);
3666 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3667 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3668 expression_t *left);
3670 typedef struct expression_parser_function_t expression_parser_function_t;
3671 struct expression_parser_function_t {
3672 unsigned precedence;
3673 parse_expression_function parser;
3674 unsigned infix_precedence;
3675 parse_expression_infix_function infix_parser;
3678 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3681 * Creates a new invalid expression.
3683 static expression_t *create_invalid_expression(void)
3685 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3686 expression->base.source_position = token.source_position;
3691 * Prints an error message if an expression was expected but not read
3693 static expression_t *expected_expression_error(void)
3695 /* skip the error message if the error token was read */
3696 if (token.type != T_ERROR) {
3697 errorf(HERE, "expected expression, got token '%K'", &token);
3701 return create_invalid_expression();
3705 * Parse a string constant.
3707 static expression_t *parse_string_const(void)
3710 if (token.type == T_STRING_LITERAL) {
3711 string_t res = token.v.string;
3713 while (token.type == T_STRING_LITERAL) {
3714 res = concat_strings(&res, &token.v.string);
3717 if (token.type != T_WIDE_STRING_LITERAL) {
3718 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3719 /* note: that we use type_char_ptr here, which is already the
3720 * automatic converted type. revert_automatic_type_conversion
3721 * will construct the array type */
3722 cnst->base.type = type_char_ptr;
3723 cnst->string.value = res;
3727 wres = concat_string_wide_string(&res, &token.v.wide_string);
3729 wres = token.v.wide_string;
3734 switch (token.type) {
3735 case T_WIDE_STRING_LITERAL:
3736 wres = concat_wide_strings(&wres, &token.v.wide_string);
3739 case T_STRING_LITERAL:
3740 wres = concat_wide_string_string(&wres, &token.v.string);
3744 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3745 cnst->base.type = type_wchar_t_ptr;
3746 cnst->wide_string.value = wres;
3755 * Parse an integer constant.
3757 static expression_t *parse_int_const(void)
3759 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3760 cnst->base.source_position = HERE;
3761 cnst->base.type = token.datatype;
3762 cnst->conste.v.int_value = token.v.intvalue;
3770 * Parse a character constant.
3772 static expression_t *parse_character_constant(void)
3774 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
3776 cnst->base.source_position = HERE;
3777 cnst->base.type = token.datatype;
3778 cnst->conste.v.character = token.v.string;
3780 if (cnst->conste.v.character.size != 1) {
3781 if (warning.multichar && (c_mode & _GNUC)) {
3783 warningf(HERE, "multi-character character constant");
3785 errorf(HERE, "more than 1 characters in character constant");
3794 * Parse a wide character constant.
3796 static expression_t *parse_wide_character_constant(void)
3798 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
3800 cnst->base.source_position = HERE;
3801 cnst->base.type = token.datatype;
3802 cnst->conste.v.wide_character = token.v.wide_string;
3804 if (cnst->conste.v.wide_character.size != 1) {
3805 if (warning.multichar && (c_mode & _GNUC)) {
3807 warningf(HERE, "multi-character character constant");
3809 errorf(HERE, "more than 1 characters in character constant");
3818 * Parse a float constant.
3820 static expression_t *parse_float_const(void)
3822 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3823 cnst->base.type = token.datatype;
3824 cnst->conste.v.float_value = token.v.floatvalue;
3831 static declaration_t *create_implicit_function(symbol_t *symbol,
3832 const source_position_t source_position)
3834 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3835 ntype->function.return_type = type_int;
3836 ntype->function.unspecified_parameters = true;
3838 type_t *type = typehash_insert(ntype);
3843 declaration_t *const declaration = allocate_declaration_zero();
3844 declaration->storage_class = STORAGE_CLASS_EXTERN;
3845 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3846 declaration->type = type;
3847 declaration->symbol = symbol;
3848 declaration->source_position = source_position;
3849 declaration->parent_scope = global_scope;
3851 scope_t *old_scope = scope;
3852 set_scope(global_scope);
3854 environment_push(declaration);
3855 /* prepends the declaration to the global declarations list */
3856 declaration->next = scope->declarations;
3857 scope->declarations = declaration;
3859 assert(scope == global_scope);
3860 set_scope(old_scope);
3866 * Creates a return_type (func)(argument_type) function type if not
3869 * @param return_type the return type
3870 * @param argument_type the argument type
3872 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3874 function_parameter_t *parameter
3875 = obstack_alloc(type_obst, sizeof(parameter[0]));
3876 memset(parameter, 0, sizeof(parameter[0]));
3877 parameter->type = argument_type;
3879 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3880 type->function.return_type = return_type;
3881 type->function.parameters = parameter;
3883 type_t *result = typehash_insert(type);
3884 if(result != type) {
3892 * Creates a function type for some function like builtins.
3894 * @param symbol the symbol describing the builtin
3896 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3898 switch(symbol->ID) {
3899 case T___builtin_alloca:
3900 return make_function_1_type(type_void_ptr, type_size_t);
3901 case T___builtin_nan:
3902 return make_function_1_type(type_double, type_char_ptr);
3903 case T___builtin_nanf:
3904 return make_function_1_type(type_float, type_char_ptr);
3905 case T___builtin_nand:
3906 return make_function_1_type(type_long_double, type_char_ptr);
3907 case T___builtin_va_end:
3908 return make_function_1_type(type_void, type_valist);
3910 panic("not implemented builtin symbol found");
3915 * Performs automatic type cast as described in § 6.3.2.1.
3917 * @param orig_type the original type
3919 static type_t *automatic_type_conversion(type_t *orig_type)
3921 type_t *type = skip_typeref(orig_type);
3922 if(is_type_array(type)) {
3923 array_type_t *array_type = &type->array;
3924 type_t *element_type = array_type->element_type;
3925 unsigned qualifiers = array_type->type.qualifiers;
3927 return make_pointer_type(element_type, qualifiers);
3930 if(is_type_function(type)) {
3931 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3938 * reverts the automatic casts of array to pointer types and function
3939 * to function-pointer types as defined § 6.3.2.1
3941 type_t *revert_automatic_type_conversion(const expression_t *expression)
3943 switch (expression->kind) {
3944 case EXPR_REFERENCE: return expression->reference.declaration->type;
3945 case EXPR_SELECT: return expression->select.compound_entry->type;
3947 case EXPR_UNARY_DEREFERENCE: {
3948 const expression_t *const value = expression->unary.value;
3949 type_t *const type = skip_typeref(value->base.type);
3950 assert(is_type_pointer(type));
3951 return type->pointer.points_to;
3954 case EXPR_BUILTIN_SYMBOL:
3955 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3957 case EXPR_ARRAY_ACCESS: {
3958 const expression_t *array_ref = expression->array_access.array_ref;
3959 type_t *type_left = skip_typeref(array_ref->base.type);
3960 if (!is_type_valid(type_left))
3962 assert(is_type_pointer(type_left));
3963 return type_left->pointer.points_to;
3966 case EXPR_STRING_LITERAL: {
3967 size_t size = expression->string.value.size;
3968 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
3971 case EXPR_WIDE_STRING_LITERAL: {
3972 size_t size = expression->wide_string.value.size;
3973 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
3976 case EXPR_COMPOUND_LITERAL:
3977 return expression->compound_literal.type;
3982 return expression->base.type;
3985 static expression_t *parse_reference(void)
3987 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3989 reference_expression_t *ref = &expression->reference;
3990 ref->symbol = token.v.symbol;
3992 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3994 source_position_t source_position = token.source_position;
3997 if(declaration == NULL) {
3998 if (! strict_mode && token.type == '(') {
3999 /* an implicitly defined function */
4000 if (warning.implicit_function_declaration) {
4001 warningf(HERE, "implicit declaration of function '%Y'",
4005 declaration = create_implicit_function(ref->symbol,
4008 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4009 return create_invalid_expression();
4013 type_t *type = declaration->type;
4015 /* we always do the auto-type conversions; the & and sizeof parser contains
4016 * code to revert this! */
4017 type = automatic_type_conversion(type);
4019 ref->declaration = declaration;
4020 ref->base.type = type;
4022 /* this declaration is used */
4023 declaration->used = true;
4028 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4032 /* TODO check if explicit cast is allowed and issue warnings/errors */
4035 static expression_t *parse_compound_literal(type_t *type)
4037 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4039 parse_initializer_env_t env;
4041 env.declaration = NULL;
4042 env.must_be_constant = false;
4043 initializer_t *initializer = parse_initializer(&env);
4046 expression->compound_literal.initializer = initializer;
4047 expression->compound_literal.type = type;
4048 expression->base.type = automatic_type_conversion(type);
4054 * Parse a cast expression.
4056 static expression_t *parse_cast(void)
4058 source_position_t source_position = token.source_position;
4060 type_t *type = parse_typename();
4064 if(token.type == '{') {
4065 return parse_compound_literal(type);
4068 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4069 cast->base.source_position = source_position;
4071 expression_t *value = parse_sub_expression(20);
4073 check_cast_allowed(value, type);
4075 cast->base.type = type;
4076 cast->unary.value = value;
4080 return create_invalid_expression();
4084 * Parse a statement expression.
4086 static expression_t *parse_statement_expression(void)
4088 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4090 statement_t *statement = parse_compound_statement();
4091 expression->statement.statement = statement;
4092 expression->base.source_position = statement->base.source_position;
4094 /* find last statement and use its type */
4095 type_t *type = type_void;
4096 const statement_t *stmt = statement->compound.statements;
4098 while (stmt->base.next != NULL)
4099 stmt = stmt->base.next;
4101 if (stmt->kind == STATEMENT_EXPRESSION) {
4102 type = stmt->expression.expression->base.type;
4105 warningf(expression->base.source_position, "empty statement expression ({})");
4107 expression->base.type = type;
4113 return create_invalid_expression();
4117 * Parse a braced expression.
4119 static expression_t *parse_brace_expression(void)
4123 switch(token.type) {
4125 /* gcc extension: a statement expression */
4126 return parse_statement_expression();
4130 return parse_cast();
4132 if(is_typedef_symbol(token.v.symbol)) {
4133 return parse_cast();
4137 expression_t *result = parse_expression();
4142 return create_invalid_expression();
4145 static expression_t *parse_function_keyword(void)
4150 if (current_function == NULL) {
4151 errorf(HERE, "'__func__' used outside of a function");
4154 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
4155 expression->base.type = type_char_ptr;
4160 static expression_t *parse_pretty_function_keyword(void)
4162 eat(T___PRETTY_FUNCTION__);
4165 if (current_function == NULL) {
4166 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
4169 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
4170 expression->base.type = type_char_ptr;
4175 static designator_t *parse_designator(void)
4177 designator_t *result = allocate_ast_zero(sizeof(result[0]));
4178 result->source_position = HERE;
4180 if(token.type != T_IDENTIFIER) {
4181 parse_error_expected("while parsing member designator",
4186 result->symbol = token.v.symbol;
4189 designator_t *last_designator = result;
4191 if(token.type == '.') {
4193 if(token.type != T_IDENTIFIER) {
4194 parse_error_expected("while parsing member designator",
4199 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4200 designator->source_position = HERE;
4201 designator->symbol = token.v.symbol;
4204 last_designator->next = designator;
4205 last_designator = designator;
4208 if(token.type == '[') {
4210 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4211 designator->source_position = HERE;
4212 designator->array_index = parse_expression();
4213 if(designator->array_index == NULL) {
4219 last_designator->next = designator;
4220 last_designator = designator;
4232 * Parse the __builtin_offsetof() expression.
4234 static expression_t *parse_offsetof(void)
4236 eat(T___builtin_offsetof);
4238 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4239 expression->base.type = type_size_t;
4242 type_t *type = parse_typename();
4244 designator_t *designator = parse_designator();
4247 expression->offsetofe.type = type;
4248 expression->offsetofe.designator = designator;
4251 memset(&path, 0, sizeof(path));
4252 path.top_type = type;
4253 path.path = NEW_ARR_F(type_path_entry_t, 0);
4255 descend_into_subtype(&path);
4257 if(!walk_designator(&path, designator, true)) {
4258 return create_invalid_expression();
4261 DEL_ARR_F(path.path);
4265 return create_invalid_expression();
4269 * Parses a _builtin_va_start() expression.
4271 static expression_t *parse_va_start(void)
4273 eat(T___builtin_va_start);
4275 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4278 expression->va_starte.ap = parse_assignment_expression();
4280 expression_t *const expr = parse_assignment_expression();
4281 if (expr->kind == EXPR_REFERENCE) {
4282 declaration_t *const decl = expr->reference.declaration;
4284 return create_invalid_expression();
4285 if (decl->parent_scope == ¤t_function->scope &&
4286 decl->next == NULL) {
4287 expression->va_starte.parameter = decl;
4292 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4294 return create_invalid_expression();
4298 * Parses a _builtin_va_arg() expression.
4300 static expression_t *parse_va_arg(void)
4302 eat(T___builtin_va_arg);
4304 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4307 expression->va_arge.ap = parse_assignment_expression();
4309 expression->base.type = parse_typename();
4314 return create_invalid_expression();
4317 static expression_t *parse_builtin_symbol(void)
4319 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4321 symbol_t *symbol = token.v.symbol;
4323 expression->builtin_symbol.symbol = symbol;
4326 type_t *type = get_builtin_symbol_type(symbol);
4327 type = automatic_type_conversion(type);
4329 expression->base.type = type;
4334 * Parses a __builtin_constant() expression.
4336 static expression_t *parse_builtin_constant(void)
4338 eat(T___builtin_constant_p);
4340 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4343 expression->builtin_constant.value = parse_assignment_expression();
4345 expression->base.type = type_int;
4349 return create_invalid_expression();
4353 * Parses a __builtin_prefetch() expression.
4355 static expression_t *parse_builtin_prefetch(void)
4357 eat(T___builtin_prefetch);
4359 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4362 expression->builtin_prefetch.adr = parse_assignment_expression();
4363 if (token.type == ',') {
4365 expression->builtin_prefetch.rw = parse_assignment_expression();
4367 if (token.type == ',') {
4369 expression->builtin_prefetch.locality = parse_assignment_expression();
4372 expression->base.type = type_void;
4376 return create_invalid_expression();
4380 * Parses a __builtin_is_*() compare expression.
4382 static expression_t *parse_compare_builtin(void)
4384 expression_t *expression;
4386 switch(token.type) {
4387 case T___builtin_isgreater:
4388 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4390 case T___builtin_isgreaterequal:
4391 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4393 case T___builtin_isless:
4394 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4396 case T___builtin_islessequal:
4397 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4399 case T___builtin_islessgreater:
4400 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4402 case T___builtin_isunordered:
4403 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4406 panic("invalid compare builtin found");
4409 expression->base.source_position = HERE;
4413 expression->binary.left = parse_assignment_expression();
4415 expression->binary.right = parse_assignment_expression();
4418 type_t *const orig_type_left = expression->binary.left->base.type;
4419 type_t *const orig_type_right = expression->binary.right->base.type;
4421 type_t *const type_left = skip_typeref(orig_type_left);
4422 type_t *const type_right = skip_typeref(orig_type_right);
4423 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4424 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4425 type_error_incompatible("invalid operands in comparison",
4426 expression->base.source_position, orig_type_left, orig_type_right);
4429 semantic_comparison(&expression->binary);
4434 return create_invalid_expression();
4438 * Parses a __builtin_expect() expression.
4440 static expression_t *parse_builtin_expect(void)
4442 eat(T___builtin_expect);
4444 expression_t *expression
4445 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4448 expression->binary.left = parse_assignment_expression();
4450 expression->binary.right = parse_constant_expression();
4453 expression->base.type = expression->binary.left->base.type;
4457 return create_invalid_expression();
4461 * Parses a MS assume() expression.
4463 static expression_t *parse_assume(void) {
4466 expression_t *expression
4467 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4470 expression->unary.value = parse_assignment_expression();
4473 expression->base.type = type_void;
4476 return create_invalid_expression();
4480 * Parses a primary expression.
4482 static expression_t *parse_primary_expression(void)
4484 switch (token.type) {
4485 case T_INTEGER: return parse_int_const();
4486 case T_CHARACTER_CONSTANT: return parse_character_constant();
4487 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4488 case T_FLOATINGPOINT: return parse_float_const();
4489 case T_STRING_LITERAL:
4490 case T_WIDE_STRING_LITERAL: return parse_string_const();
4491 case T_IDENTIFIER: return parse_reference();
4492 case T___FUNCTION__:
4493 case T___func__: return parse_function_keyword();
4494 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4495 case T___builtin_offsetof: return parse_offsetof();
4496 case T___builtin_va_start: return parse_va_start();
4497 case T___builtin_va_arg: return parse_va_arg();
4498 case T___builtin_expect: return parse_builtin_expect();
4499 case T___builtin_alloca:
4500 case T___builtin_nan:
4501 case T___builtin_nand:
4502 case T___builtin_nanf:
4503 case T___builtin_va_end: return parse_builtin_symbol();
4504 case T___builtin_isgreater:
4505 case T___builtin_isgreaterequal:
4506 case T___builtin_isless:
4507 case T___builtin_islessequal:
4508 case T___builtin_islessgreater:
4509 case T___builtin_isunordered: return parse_compare_builtin();
4510 case T___builtin_constant_p: return parse_builtin_constant();
4511 case T___builtin_prefetch: return parse_builtin_prefetch();
4512 case T_assume: return parse_assume();
4514 case '(': return parse_brace_expression();
4517 errorf(HERE, "unexpected token %K, expected an expression", &token);
4520 return create_invalid_expression();
4524 * Check if the expression has the character type and issue a warning then.
4526 static void check_for_char_index_type(const expression_t *expression) {
4527 type_t *const type = expression->base.type;
4528 const type_t *const base_type = skip_typeref(type);
4530 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4531 warning.char_subscripts) {
4532 warningf(expression->base.source_position,
4533 "array subscript has type '%T'", type);
4537 static expression_t *parse_array_expression(unsigned precedence,
4544 expression_t *inside = parse_expression();
4546 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4548 array_access_expression_t *array_access = &expression->array_access;
4550 type_t *const orig_type_left = left->base.type;
4551 type_t *const orig_type_inside = inside->base.type;
4553 type_t *const type_left = skip_typeref(orig_type_left);
4554 type_t *const type_inside = skip_typeref(orig_type_inside);
4556 type_t *return_type;
4557 if (is_type_pointer(type_left)) {
4558 return_type = type_left->pointer.points_to;
4559 array_access->array_ref = left;
4560 array_access->index = inside;
4561 check_for_char_index_type(inside);
4562 } else if (is_type_pointer(type_inside)) {
4563 return_type = type_inside->pointer.points_to;
4564 array_access->array_ref = inside;
4565 array_access->index = left;
4566 array_access->flipped = true;
4567 check_for_char_index_type(left);
4569 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4571 "array access on object with non-pointer types '%T', '%T'",
4572 orig_type_left, orig_type_inside);
4574 return_type = type_error_type;
4575 array_access->array_ref = create_invalid_expression();
4578 if(token.type != ']') {
4579 parse_error_expected("Problem while parsing array access", ']', 0);
4584 return_type = automatic_type_conversion(return_type);
4585 expression->base.type = return_type;
4590 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4592 expression_t *tp_expression = allocate_expression_zero(kind);
4593 tp_expression->base.type = type_size_t;
4595 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4597 tp_expression->typeprop.type = parse_typename();
4600 expression_t *expression = parse_sub_expression(precedence);
4601 expression->base.type = revert_automatic_type_conversion(expression);
4603 tp_expression->typeprop.type = expression->base.type;
4604 tp_expression->typeprop.tp_expression = expression;
4607 return tp_expression;
4609 return create_invalid_expression();
4612 static expression_t *parse_sizeof(unsigned precedence)
4615 return parse_typeprop(EXPR_SIZEOF, precedence);
4618 static expression_t *parse_alignof(unsigned precedence)
4621 return parse_typeprop(EXPR_SIZEOF, precedence);
4624 static expression_t *parse_select_expression(unsigned precedence,
4625 expression_t *compound)
4628 assert(token.type == '.' || token.type == T_MINUSGREATER);
4630 bool is_pointer = (token.type == T_MINUSGREATER);
4633 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4634 select->select.compound = compound;
4636 if(token.type != T_IDENTIFIER) {
4637 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4640 symbol_t *symbol = token.v.symbol;
4641 select->select.symbol = symbol;
4644 type_t *const orig_type = compound->base.type;
4645 type_t *const type = skip_typeref(orig_type);
4647 type_t *type_left = type;
4649 if (!is_type_pointer(type)) {
4650 if (is_type_valid(type)) {
4651 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4653 return create_invalid_expression();
4655 type_left = type->pointer.points_to;
4657 type_left = skip_typeref(type_left);
4659 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4660 type_left->kind != TYPE_COMPOUND_UNION) {
4661 if (is_type_valid(type_left)) {
4662 errorf(HERE, "request for member '%Y' in something not a struct or "
4663 "union, but '%T'", symbol, type_left);
4665 return create_invalid_expression();
4668 declaration_t *const declaration = type_left->compound.declaration;
4670 if(!declaration->init.is_defined) {
4671 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4673 return create_invalid_expression();
4676 declaration_t *iter = find_compound_entry(declaration, symbol);
4678 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4679 return create_invalid_expression();
4682 /* we always do the auto-type conversions; the & and sizeof parser contains
4683 * code to revert this! */
4684 type_t *expression_type = automatic_type_conversion(iter->type);
4686 select->select.compound_entry = iter;
4687 select->base.type = expression_type;
4689 if(expression_type->kind == TYPE_BITFIELD) {
4690 expression_t *extract
4691 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4692 extract->unary.value = select;
4693 extract->base.type = expression_type->bitfield.base;
4702 * Parse a call expression, ie. expression '( ... )'.
4704 * @param expression the function address
4706 static expression_t *parse_call_expression(unsigned precedence,
4707 expression_t *expression)
4710 expression_t *result = allocate_expression_zero(EXPR_CALL);
4712 call_expression_t *call = &result->call;
4713 call->function = expression;
4715 type_t *const orig_type = expression->base.type;
4716 type_t *const type = skip_typeref(orig_type);
4718 function_type_t *function_type = NULL;
4719 if (is_type_pointer(type)) {
4720 type_t *const to_type = skip_typeref(type->pointer.points_to);
4722 if (is_type_function(to_type)) {
4723 function_type = &to_type->function;
4724 call->base.type = function_type->return_type;
4728 if (function_type == NULL && is_type_valid(type)) {
4729 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4732 /* parse arguments */
4735 if(token.type != ')') {
4736 call_argument_t *last_argument = NULL;
4739 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4741 argument->expression = parse_assignment_expression();
4742 if(last_argument == NULL) {
4743 call->arguments = argument;
4745 last_argument->next = argument;
4747 last_argument = argument;
4749 if(token.type != ',')
4756 if(function_type != NULL) {
4757 function_parameter_t *parameter = function_type->parameters;
4758 call_argument_t *argument = call->arguments;
4759 for( ; parameter != NULL && argument != NULL;
4760 parameter = parameter->next, argument = argument->next) {
4761 type_t *expected_type = parameter->type;
4762 /* TODO report scope in error messages */
4763 expression_t *const arg_expr = argument->expression;
4764 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4765 if (res_type == NULL) {
4766 /* TODO improve error message */
4767 errorf(arg_expr->base.source_position,
4768 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4769 arg_expr, arg_expr->base.type, expected_type);
4771 argument->expression = create_implicit_cast(argument->expression, expected_type);
4774 /* too few parameters */
4775 if(parameter != NULL) {
4776 errorf(HERE, "too few arguments to function '%E'", expression);
4777 } else if(argument != NULL) {
4778 /* too many parameters */
4779 if(!function_type->variadic
4780 && !function_type->unspecified_parameters) {
4781 errorf(HERE, "too many arguments to function '%E'", expression);
4783 /* do default promotion */
4784 for( ; argument != NULL; argument = argument->next) {
4785 type_t *type = argument->expression->base.type;
4787 type = skip_typeref(type);
4788 if(is_type_integer(type)) {
4789 type = promote_integer(type);
4790 } else if(type == type_float) {
4794 argument->expression
4795 = create_implicit_cast(argument->expression, type);
4798 check_format(&result->call);
4801 check_format(&result->call);
4807 return create_invalid_expression();
4810 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4812 static bool same_compound_type(const type_t *type1, const type_t *type2)
4815 is_type_compound(type1) &&
4816 type1->kind == type2->kind &&
4817 type1->compound.declaration == type2->compound.declaration;
4821 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4823 * @param expression the conditional expression
4825 static expression_t *parse_conditional_expression(unsigned precedence,
4826 expression_t *expression)
4830 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4832 conditional_expression_t *conditional = &result->conditional;
4833 conditional->condition = expression;
4836 type_t *const condition_type_orig = expression->base.type;
4837 type_t *const condition_type = skip_typeref(condition_type_orig);
4838 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4839 type_error("expected a scalar type in conditional condition",
4840 expression->base.source_position, condition_type_orig);
4843 expression_t *true_expression = parse_expression();
4845 expression_t *false_expression = parse_sub_expression(precedence);
4847 type_t *const orig_true_type = true_expression->base.type;
4848 type_t *const orig_false_type = false_expression->base.type;
4849 type_t *const true_type = skip_typeref(orig_true_type);
4850 type_t *const false_type = skip_typeref(orig_false_type);
4853 type_t *result_type;
4854 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4855 result_type = semantic_arithmetic(true_type, false_type);
4857 true_expression = create_implicit_cast(true_expression, result_type);
4858 false_expression = create_implicit_cast(false_expression, result_type);
4860 conditional->true_expression = true_expression;
4861 conditional->false_expression = false_expression;
4862 conditional->base.type = result_type;
4863 } else if (same_compound_type(true_type, false_type) || (
4864 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4865 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4867 /* just take 1 of the 2 types */
4868 result_type = true_type;
4869 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4870 && pointers_compatible(true_type, false_type)) {
4872 result_type = true_type;
4873 } else if (is_type_pointer(true_type)
4874 && is_null_pointer_constant(false_expression)) {
4875 result_type = true_type;
4876 } else if (is_type_pointer(false_type)
4877 && is_null_pointer_constant(true_expression)) {
4878 result_type = false_type;
4880 /* TODO: one pointer to void*, other some pointer */
4882 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4883 type_error_incompatible("while parsing conditional",
4884 expression->base.source_position, true_type,
4887 result_type = type_error_type;
4890 conditional->true_expression
4891 = create_implicit_cast(true_expression, result_type);
4892 conditional->false_expression
4893 = create_implicit_cast(false_expression, result_type);
4894 conditional->base.type = result_type;
4897 return create_invalid_expression();
4901 * Parse an extension expression.
4903 static expression_t *parse_extension(unsigned precedence)
4905 eat(T___extension__);
4907 /* TODO enable extensions */
4908 expression_t *expression = parse_sub_expression(precedence);
4909 /* TODO disable extensions */
4914 * Parse a __builtin_classify_type() expression.
4916 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4918 eat(T___builtin_classify_type);
4920 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4921 result->base.type = type_int;
4924 expression_t *expression = parse_sub_expression(precedence);
4926 result->classify_type.type_expression = expression;
4930 return create_invalid_expression();
4933 static void semantic_incdec(unary_expression_t *expression)
4935 type_t *const orig_type = expression->value->base.type;
4936 type_t *const type = skip_typeref(orig_type);
4937 /* TODO !is_type_real && !is_type_pointer */
4938 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4939 if (is_type_valid(type)) {
4940 /* TODO: improve error message */
4941 errorf(HERE, "operation needs an arithmetic or pointer type");
4946 expression->base.type = orig_type;
4949 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4951 type_t *const orig_type = expression->value->base.type;
4952 type_t *const type = skip_typeref(orig_type);
4953 if(!is_type_arithmetic(type)) {
4954 if (is_type_valid(type)) {
4955 /* TODO: improve error message */
4956 errorf(HERE, "operation needs an arithmetic type");
4961 expression->base.type = orig_type;
4964 static void semantic_unexpr_scalar(unary_expression_t *expression)
4966 type_t *const orig_type = expression->value->base.type;
4967 type_t *const type = skip_typeref(orig_type);
4968 if (!is_type_scalar(type)) {
4969 if (is_type_valid(type)) {
4970 errorf(HERE, "operand of ! must be of scalar type");
4975 expression->base.type = orig_type;
4978 static void semantic_unexpr_integer(unary_expression_t *expression)
4980 type_t *const orig_type = expression->value->base.type;
4981 type_t *const type = skip_typeref(orig_type);
4982 if (!is_type_integer(type)) {
4983 if (is_type_valid(type)) {
4984 errorf(HERE, "operand of ~ must be of integer type");
4989 expression->base.type = orig_type;
4992 static void semantic_dereference(unary_expression_t *expression)
4994 type_t *const orig_type = expression->value->base.type;
4995 type_t *const type = skip_typeref(orig_type);
4996 if(!is_type_pointer(type)) {
4997 if (is_type_valid(type)) {
4998 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
5003 type_t *result_type = type->pointer.points_to;
5004 result_type = automatic_type_conversion(result_type);
5005 expression->base.type = result_type;
5009 * Check the semantic of the address taken expression.
5011 static void semantic_take_addr(unary_expression_t *expression)
5013 expression_t *value = expression->value;
5014 value->base.type = revert_automatic_type_conversion(value);
5016 type_t *orig_type = value->base.type;
5017 if(!is_type_valid(orig_type))
5020 if(value->kind == EXPR_REFERENCE) {
5021 declaration_t *const declaration = value->reference.declaration;
5022 if(declaration != NULL) {
5023 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
5024 errorf(expression->base.source_position,
5025 "address of register variable '%Y' requested",
5026 declaration->symbol);
5028 declaration->address_taken = 1;
5032 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5035 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
5036 static expression_t *parse_##unexpression_type(unsigned precedence) \
5040 expression_t *unary_expression \
5041 = allocate_expression_zero(unexpression_type); \
5042 unary_expression->base.source_position = HERE; \
5043 unary_expression->unary.value = parse_sub_expression(precedence); \
5045 sfunc(&unary_expression->unary); \
5047 return unary_expression; \
5050 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
5051 semantic_unexpr_arithmetic)
5052 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
5053 semantic_unexpr_arithmetic)
5054 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
5055 semantic_unexpr_scalar)
5056 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
5057 semantic_dereference)
5058 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
5060 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
5061 semantic_unexpr_integer)
5062 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
5064 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
5067 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
5069 static expression_t *parse_##unexpression_type(unsigned precedence, \
5070 expression_t *left) \
5072 (void) precedence; \
5075 expression_t *unary_expression \
5076 = allocate_expression_zero(unexpression_type); \
5077 unary_expression->unary.value = left; \
5079 sfunc(&unary_expression->unary); \
5081 return unary_expression; \
5084 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
5085 EXPR_UNARY_POSTFIX_INCREMENT,
5087 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
5088 EXPR_UNARY_POSTFIX_DECREMENT,
5091 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
5093 /* TODO: handle complex + imaginary types */
5095 /* § 6.3.1.8 Usual arithmetic conversions */
5096 if(type_left == type_long_double || type_right == type_long_double) {
5097 return type_long_double;
5098 } else if(type_left == type_double || type_right == type_double) {
5100 } else if(type_left == type_float || type_right == type_float) {
5104 type_right = promote_integer(type_right);
5105 type_left = promote_integer(type_left);
5107 if(type_left == type_right)
5110 bool signed_left = is_type_signed(type_left);
5111 bool signed_right = is_type_signed(type_right);
5112 int rank_left = get_rank(type_left);
5113 int rank_right = get_rank(type_right);
5114 if(rank_left < rank_right) {
5115 if(signed_left == signed_right || !signed_right) {
5121 if(signed_left == signed_right || !signed_left) {
5130 * Check the semantic restrictions for a binary expression.
5132 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
5134 expression_t *const left = expression->left;
5135 expression_t *const right = expression->right;
5136 type_t *const orig_type_left = left->base.type;
5137 type_t *const orig_type_right = right->base.type;
5138 type_t *const type_left = skip_typeref(orig_type_left);
5139 type_t *const type_right = skip_typeref(orig_type_right);
5141 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5142 /* TODO: improve error message */
5143 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5144 errorf(HERE, "operation needs arithmetic types");
5149 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5150 expression->left = create_implicit_cast(left, arithmetic_type);
5151 expression->right = create_implicit_cast(right, arithmetic_type);
5152 expression->base.type = arithmetic_type;
5155 static void semantic_shift_op(binary_expression_t *expression)
5157 expression_t *const left = expression->left;
5158 expression_t *const right = expression->right;
5159 type_t *const orig_type_left = left->base.type;
5160 type_t *const orig_type_right = right->base.type;
5161 type_t * type_left = skip_typeref(orig_type_left);
5162 type_t * type_right = skip_typeref(orig_type_right);
5164 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
5165 /* TODO: improve error message */
5166 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5167 errorf(HERE, "operation needs integer types");
5172 type_left = promote_integer(type_left);
5173 type_right = promote_integer(type_right);
5175 expression->left = create_implicit_cast(left, type_left);
5176 expression->right = create_implicit_cast(right, type_right);
5177 expression->base.type = type_left;
5180 static void semantic_add(binary_expression_t *expression)
5182 expression_t *const left = expression->left;
5183 expression_t *const right = expression->right;
5184 type_t *const orig_type_left = left->base.type;
5185 type_t *const orig_type_right = right->base.type;
5186 type_t *const type_left = skip_typeref(orig_type_left);
5187 type_t *const type_right = skip_typeref(orig_type_right);
5190 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5191 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5192 expression->left = create_implicit_cast(left, arithmetic_type);
5193 expression->right = create_implicit_cast(right, arithmetic_type);
5194 expression->base.type = arithmetic_type;
5196 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5197 expression->base.type = type_left;
5198 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
5199 expression->base.type = type_right;
5200 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5201 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
5205 static void semantic_sub(binary_expression_t *expression)
5207 expression_t *const left = expression->left;
5208 expression_t *const right = expression->right;
5209 type_t *const orig_type_left = left->base.type;
5210 type_t *const orig_type_right = right->base.type;
5211 type_t *const type_left = skip_typeref(orig_type_left);
5212 type_t *const type_right = skip_typeref(orig_type_right);
5215 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5216 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5217 expression->left = create_implicit_cast(left, arithmetic_type);
5218 expression->right = create_implicit_cast(right, arithmetic_type);
5219 expression->base.type = arithmetic_type;
5221 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5222 expression->base.type = type_left;
5223 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5224 if(!pointers_compatible(type_left, type_right)) {
5226 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5227 orig_type_left, orig_type_right);
5229 expression->base.type = type_ptrdiff_t;
5231 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5232 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5233 orig_type_left, orig_type_right);
5238 * Check the semantics of comparison expressions.
5240 * @param expression The expression to check.
5242 static void semantic_comparison(binary_expression_t *expression)
5244 expression_t *left = expression->left;
5245 expression_t *right = expression->right;
5246 type_t *orig_type_left = left->base.type;
5247 type_t *orig_type_right = right->base.type;
5249 type_t *type_left = skip_typeref(orig_type_left);
5250 type_t *type_right = skip_typeref(orig_type_right);
5252 /* TODO non-arithmetic types */
5253 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5254 if (warning.sign_compare &&
5255 (expression->base.kind != EXPR_BINARY_EQUAL &&
5256 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5257 (is_type_signed(type_left) != is_type_signed(type_right))) {
5258 warningf(expression->base.source_position,
5259 "comparison between signed and unsigned");
5261 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5262 expression->left = create_implicit_cast(left, arithmetic_type);
5263 expression->right = create_implicit_cast(right, arithmetic_type);
5264 expression->base.type = arithmetic_type;
5265 if (warning.float_equal &&
5266 (expression->base.kind == EXPR_BINARY_EQUAL ||
5267 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5268 is_type_float(arithmetic_type)) {
5269 warningf(expression->base.source_position,
5270 "comparing floating point with == or != is unsafe");
5272 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5273 /* TODO check compatibility */
5274 } else if (is_type_pointer(type_left)) {
5275 expression->right = create_implicit_cast(right, type_left);
5276 } else if (is_type_pointer(type_right)) {
5277 expression->left = create_implicit_cast(left, type_right);
5278 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5279 type_error_incompatible("invalid operands in comparison",
5280 expression->base.source_position,
5281 type_left, type_right);
5283 expression->base.type = type_int;
5286 static void semantic_arithmetic_assign(binary_expression_t *expression)
5288 expression_t *left = expression->left;
5289 expression_t *right = expression->right;
5290 type_t *orig_type_left = left->base.type;
5291 type_t *orig_type_right = right->base.type;
5293 type_t *type_left = skip_typeref(orig_type_left);
5294 type_t *type_right = skip_typeref(orig_type_right);
5296 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5297 /* TODO: improve error message */
5298 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5299 errorf(HERE, "operation needs arithmetic types");
5304 /* combined instructions are tricky. We can't create an implicit cast on
5305 * the left side, because we need the uncasted form for the store.
5306 * The ast2firm pass has to know that left_type must be right_type
5307 * for the arithmetic operation and create a cast by itself */
5308 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5309 expression->right = create_implicit_cast(right, arithmetic_type);
5310 expression->base.type = type_left;
5313 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5315 expression_t *const left = expression->left;
5316 expression_t *const right = expression->right;
5317 type_t *const orig_type_left = left->base.type;
5318 type_t *const orig_type_right = right->base.type;
5319 type_t *const type_left = skip_typeref(orig_type_left);
5320 type_t *const type_right = skip_typeref(orig_type_right);
5322 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5323 /* combined instructions are tricky. We can't create an implicit cast on
5324 * the left side, because we need the uncasted form for the store.
5325 * The ast2firm pass has to know that left_type must be right_type
5326 * for the arithmetic operation and create a cast by itself */
5327 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5328 expression->right = create_implicit_cast(right, arithmetic_type);
5329 expression->base.type = type_left;
5330 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5331 expression->base.type = type_left;
5332 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5333 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5338 * Check the semantic restrictions of a logical expression.
5340 static void semantic_logical_op(binary_expression_t *expression)
5342 expression_t *const left = expression->left;
5343 expression_t *const right = expression->right;
5344 type_t *const orig_type_left = left->base.type;
5345 type_t *const orig_type_right = right->base.type;
5346 type_t *const type_left = skip_typeref(orig_type_left);
5347 type_t *const type_right = skip_typeref(orig_type_right);
5349 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5350 /* TODO: improve error message */
5351 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5352 errorf(HERE, "operation needs scalar types");
5357 expression->base.type = type_int;
5361 * Checks if a compound type has constant fields.
5363 static bool has_const_fields(const compound_type_t *type)
5365 const scope_t *scope = &type->declaration->scope;
5366 const declaration_t *declaration = scope->declarations;
5368 for (; declaration != NULL; declaration = declaration->next) {
5369 if (declaration->namespc != NAMESPACE_NORMAL)
5372 const type_t *decl_type = skip_typeref(declaration->type);
5373 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5381 * Check the semantic restrictions of a binary assign expression.
5383 static void semantic_binexpr_assign(binary_expression_t *expression)
5385 expression_t *left = expression->left;
5386 type_t *orig_type_left = left->base.type;
5388 type_t *type_left = revert_automatic_type_conversion(left);
5389 type_left = skip_typeref(orig_type_left);
5391 /* must be a modifiable lvalue */
5392 if (is_type_array(type_left)) {
5393 errorf(HERE, "cannot assign to arrays ('%E')", left);
5396 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5397 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5401 if(is_type_incomplete(type_left)) {
5403 "left-hand side of assignment '%E' has incomplete type '%T'",
5404 left, orig_type_left);
5407 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5408 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5409 left, orig_type_left);
5413 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5415 if (res_type == NULL) {
5416 errorf(expression->base.source_position,
5417 "cannot assign to '%T' from '%T'",
5418 orig_type_left, expression->right->base.type);
5420 expression->right = create_implicit_cast(expression->right, res_type);
5423 expression->base.type = orig_type_left;
5427 * Determine if the outermost operation (or parts thereof) of the given
5428 * expression has no effect in order to generate a warning about this fact.
5429 * Therefore in some cases this only examines some of the operands of the
5430 * expression (see comments in the function and examples below).
5432 * f() + 23; // warning, because + has no effect
5433 * x || f(); // no warning, because x controls execution of f()
5434 * x ? y : f(); // warning, because y has no effect
5435 * (void)x; // no warning to be able to suppress the warning
5436 * This function can NOT be used for an "expression has definitely no effect"-
5438 static bool expression_has_effect(const expression_t *const expr)
5440 switch (expr->kind) {
5441 case EXPR_UNKNOWN: break;
5442 case EXPR_INVALID: return true; /* do NOT warn */
5443 case EXPR_REFERENCE: return false;
5444 case EXPR_CONST: return false;
5445 case EXPR_CHARACTER_CONSTANT: return false;
5446 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5447 case EXPR_STRING_LITERAL: return false;
5448 case EXPR_WIDE_STRING_LITERAL: return false;
5451 const call_expression_t *const call = &expr->call;
5452 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5455 switch (call->function->builtin_symbol.symbol->ID) {
5456 case T___builtin_va_end: return true;
5457 default: return false;
5461 /* Generate the warning if either the left or right hand side of a
5462 * conditional expression has no effect */
5463 case EXPR_CONDITIONAL: {
5464 const conditional_expression_t *const cond = &expr->conditional;
5466 expression_has_effect(cond->true_expression) &&
5467 expression_has_effect(cond->false_expression);
5470 case EXPR_SELECT: return false;
5471 case EXPR_ARRAY_ACCESS: return false;
5472 case EXPR_SIZEOF: return false;
5473 case EXPR_CLASSIFY_TYPE: return false;
5474 case EXPR_ALIGNOF: return false;
5476 case EXPR_FUNCTION: return false;
5477 case EXPR_PRETTY_FUNCTION: return false;
5478 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5479 case EXPR_BUILTIN_CONSTANT_P: return false;
5480 case EXPR_BUILTIN_PREFETCH: return true;
5481 case EXPR_OFFSETOF: return false;
5482 case EXPR_VA_START: return true;
5483 case EXPR_VA_ARG: return true;
5484 case EXPR_STATEMENT: return true; // TODO
5485 case EXPR_COMPOUND_LITERAL: return false;
5487 case EXPR_UNARY_NEGATE: return false;
5488 case EXPR_UNARY_PLUS: return false;
5489 case EXPR_UNARY_BITWISE_NEGATE: return false;
5490 case EXPR_UNARY_NOT: return false;
5491 case EXPR_UNARY_DEREFERENCE: return false;
5492 case EXPR_UNARY_TAKE_ADDRESS: return false;
5493 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5494 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5495 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5496 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5498 /* Treat void casts as if they have an effect in order to being able to
5499 * suppress the warning */
5500 case EXPR_UNARY_CAST: {
5501 type_t *const type = skip_typeref(expr->base.type);
5502 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5505 case EXPR_UNARY_CAST_IMPLICIT: return true;
5506 case EXPR_UNARY_ASSUME: return true;
5507 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5509 case EXPR_BINARY_ADD: return false;
5510 case EXPR_BINARY_SUB: return false;
5511 case EXPR_BINARY_MUL: return false;
5512 case EXPR_BINARY_DIV: return false;
5513 case EXPR_BINARY_MOD: return false;
5514 case EXPR_BINARY_EQUAL: return false;
5515 case EXPR_BINARY_NOTEQUAL: return false;
5516 case EXPR_BINARY_LESS: return false;
5517 case EXPR_BINARY_LESSEQUAL: return false;
5518 case EXPR_BINARY_GREATER: return false;
5519 case EXPR_BINARY_GREATEREQUAL: return false;
5520 case EXPR_BINARY_BITWISE_AND: return false;
5521 case EXPR_BINARY_BITWISE_OR: return false;
5522 case EXPR_BINARY_BITWISE_XOR: return false;
5523 case EXPR_BINARY_SHIFTLEFT: return false;
5524 case EXPR_BINARY_SHIFTRIGHT: return false;
5525 case EXPR_BINARY_ASSIGN: return true;
5526 case EXPR_BINARY_MUL_ASSIGN: return true;
5527 case EXPR_BINARY_DIV_ASSIGN: return true;
5528 case EXPR_BINARY_MOD_ASSIGN: return true;
5529 case EXPR_BINARY_ADD_ASSIGN: return true;
5530 case EXPR_BINARY_SUB_ASSIGN: return true;
5531 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5532 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5533 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5534 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5535 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5537 /* Only examine the right hand side of && and ||, because the left hand
5538 * side already has the effect of controlling the execution of the right
5540 case EXPR_BINARY_LOGICAL_AND:
5541 case EXPR_BINARY_LOGICAL_OR:
5542 /* Only examine the right hand side of a comma expression, because the left
5543 * hand side has a separate warning */
5544 case EXPR_BINARY_COMMA:
5545 return expression_has_effect(expr->binary.right);
5547 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5548 case EXPR_BINARY_ISGREATER: return false;
5549 case EXPR_BINARY_ISGREATEREQUAL: return false;
5550 case EXPR_BINARY_ISLESS: return false;
5551 case EXPR_BINARY_ISLESSEQUAL: return false;
5552 case EXPR_BINARY_ISLESSGREATER: return false;
5553 case EXPR_BINARY_ISUNORDERED: return false;
5556 panic("unexpected expression");
5559 static void semantic_comma(binary_expression_t *expression)
5561 if (warning.unused_value) {
5562 const expression_t *const left = expression->left;
5563 if (!expression_has_effect(left)) {
5564 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5567 expression->base.type = expression->right->base.type;
5570 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5571 static expression_t *parse_##binexpression_type(unsigned precedence, \
5572 expression_t *left) \
5575 source_position_t pos = HERE; \
5577 expression_t *right = parse_sub_expression(precedence + lr); \
5579 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5580 binexpr->base.source_position = pos; \
5581 binexpr->binary.left = left; \
5582 binexpr->binary.right = right; \
5583 sfunc(&binexpr->binary); \
5588 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5589 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5590 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5591 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5592 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5593 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5594 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5595 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5596 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5598 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5599 semantic_comparison, 1)
5600 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5601 semantic_comparison, 1)
5602 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5603 semantic_comparison, 1)
5604 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5605 semantic_comparison, 1)
5607 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5608 semantic_binexpr_arithmetic, 1)
5609 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5610 semantic_binexpr_arithmetic, 1)
5611 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5612 semantic_binexpr_arithmetic, 1)
5613 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5614 semantic_logical_op, 1)
5615 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5616 semantic_logical_op, 1)
5617 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5618 semantic_shift_op, 1)
5619 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5620 semantic_shift_op, 1)
5621 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5622 semantic_arithmetic_addsubb_assign, 0)
5623 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5624 semantic_arithmetic_addsubb_assign, 0)
5625 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5626 semantic_arithmetic_assign, 0)
5627 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5628 semantic_arithmetic_assign, 0)
5629 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5630 semantic_arithmetic_assign, 0)
5631 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5632 semantic_arithmetic_assign, 0)
5633 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5634 semantic_arithmetic_assign, 0)
5635 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5636 semantic_arithmetic_assign, 0)
5637 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5638 semantic_arithmetic_assign, 0)
5639 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5640 semantic_arithmetic_assign, 0)
5642 static expression_t *parse_sub_expression(unsigned precedence)
5644 if(token.type < 0) {
5645 return expected_expression_error();
5648 expression_parser_function_t *parser
5649 = &expression_parsers[token.type];
5650 source_position_t source_position = token.source_position;
5653 if(parser->parser != NULL) {
5654 left = parser->parser(parser->precedence);
5656 left = parse_primary_expression();
5658 assert(left != NULL);
5659 left->base.source_position = source_position;
5662 if(token.type < 0) {
5663 return expected_expression_error();
5666 parser = &expression_parsers[token.type];
5667 if(parser->infix_parser == NULL)
5669 if(parser->infix_precedence < precedence)
5672 left = parser->infix_parser(parser->infix_precedence, left);
5674 assert(left != NULL);
5675 assert(left->kind != EXPR_UNKNOWN);
5676 left->base.source_position = source_position;
5683 * Parse an expression.
5685 static expression_t *parse_expression(void)
5687 return parse_sub_expression(1);
5691 * Register a parser for a prefix-like operator with given precedence.
5693 * @param parser the parser function
5694 * @param token_type the token type of the prefix token
5695 * @param precedence the precedence of the operator
5697 static void register_expression_parser(parse_expression_function parser,
5698 int token_type, unsigned precedence)
5700 expression_parser_function_t *entry = &expression_parsers[token_type];
5702 if(entry->parser != NULL) {
5703 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5704 panic("trying to register multiple expression parsers for a token");
5706 entry->parser = parser;
5707 entry->precedence = precedence;
5711 * Register a parser for an infix operator with given precedence.
5713 * @param parser the parser function
5714 * @param token_type the token type of the infix operator
5715 * @param precedence the precedence of the operator
5717 static void register_infix_parser(parse_expression_infix_function parser,
5718 int token_type, unsigned precedence)
5720 expression_parser_function_t *entry = &expression_parsers[token_type];
5722 if(entry->infix_parser != NULL) {
5723 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5724 panic("trying to register multiple infix expression parsers for a "
5727 entry->infix_parser = parser;
5728 entry->infix_precedence = precedence;
5732 * Initialize the expression parsers.
5734 static void init_expression_parsers(void)
5736 memset(&expression_parsers, 0, sizeof(expression_parsers));
5738 register_infix_parser(parse_array_expression, '[', 30);
5739 register_infix_parser(parse_call_expression, '(', 30);
5740 register_infix_parser(parse_select_expression, '.', 30);
5741 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5742 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5744 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5747 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5748 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5749 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5750 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5751 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5752 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5753 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5754 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5755 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5756 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5757 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5758 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5759 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5760 T_EXCLAMATIONMARKEQUAL, 13);
5761 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5762 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5763 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5764 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5765 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5766 register_infix_parser(parse_conditional_expression, '?', 7);
5767 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5768 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5769 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5770 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5771 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5772 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5773 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5774 T_LESSLESSEQUAL, 2);
5775 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5776 T_GREATERGREATEREQUAL, 2);
5777 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5779 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5781 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5784 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5786 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5787 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5788 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5789 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5790 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5791 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5792 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5794 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5796 register_expression_parser(parse_sizeof, T_sizeof, 25);
5797 register_expression_parser(parse_alignof, T___alignof__, 25);
5798 register_expression_parser(parse_extension, T___extension__, 25);
5799 register_expression_parser(parse_builtin_classify_type,
5800 T___builtin_classify_type, 25);
5804 * Parse a asm statement constraints specification.
5806 static asm_constraint_t *parse_asm_constraints(void)
5808 asm_constraint_t *result = NULL;
5809 asm_constraint_t *last = NULL;
5811 while(token.type == T_STRING_LITERAL || token.type == '[') {
5812 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5813 memset(constraint, 0, sizeof(constraint[0]));
5815 if(token.type == '[') {
5817 if(token.type != T_IDENTIFIER) {
5818 parse_error_expected("while parsing asm constraint",
5822 constraint->symbol = token.v.symbol;
5827 constraint->constraints = parse_string_literals();
5829 constraint->expression = parse_expression();
5833 last->next = constraint;
5835 result = constraint;
5839 if(token.type != ',')
5850 * Parse a asm statement clobber specification.
5852 static asm_clobber_t *parse_asm_clobbers(void)
5854 asm_clobber_t *result = NULL;
5855 asm_clobber_t *last = NULL;
5857 while(token.type == T_STRING_LITERAL) {
5858 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5859 clobber->clobber = parse_string_literals();
5862 last->next = clobber;
5868 if(token.type != ',')
5877 * Parse an asm statement.
5879 static statement_t *parse_asm_statement(void)
5883 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5884 statement->base.source_position = token.source_position;
5886 asm_statement_t *asm_statement = &statement->asms;
5888 if(token.type == T_volatile) {
5890 asm_statement->is_volatile = true;
5894 asm_statement->asm_text = parse_string_literals();
5896 if(token.type != ':')
5900 asm_statement->inputs = parse_asm_constraints();
5901 if(token.type != ':')
5905 asm_statement->outputs = parse_asm_constraints();
5906 if(token.type != ':')
5910 asm_statement->clobbers = parse_asm_clobbers();
5921 * Parse a case statement.
5923 static statement_t *parse_case_statement(void)
5927 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5929 statement->base.source_position = token.source_position;
5930 statement->case_label.expression = parse_expression();
5932 if (c_mode & _GNUC) {
5933 if (token.type == T_DOTDOTDOT) {
5935 statement->case_label.end_range = parse_expression();
5941 if (! is_constant_expression(statement->case_label.expression)) {
5942 errorf(statement->base.source_position,
5943 "case label does not reduce to an integer constant");
5945 /* TODO: check if the case label is already known */
5946 if (current_switch != NULL) {
5947 /* link all cases into the switch statement */
5948 if (current_switch->last_case == NULL) {
5949 current_switch->first_case =
5950 current_switch->last_case = &statement->case_label;
5952 current_switch->last_case->next = &statement->case_label;
5955 errorf(statement->base.source_position,
5956 "case label not within a switch statement");
5959 statement->case_label.statement = parse_statement();
5967 * Finds an existing default label of a switch statement.
5969 static case_label_statement_t *
5970 find_default_label(const switch_statement_t *statement)
5972 case_label_statement_t *label = statement->first_case;
5973 for ( ; label != NULL; label = label->next) {
5974 if (label->expression == NULL)
5981 * Parse a default statement.
5983 static statement_t *parse_default_statement(void)
5987 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5989 statement->base.source_position = token.source_position;
5992 if (current_switch != NULL) {
5993 const case_label_statement_t *def_label = find_default_label(current_switch);
5994 if (def_label != NULL) {
5995 errorf(HERE, "multiple default labels in one switch");
5996 errorf(def_label->base.source_position,
5997 "this is the first default label");
5999 /* link all cases into the switch statement */
6000 if (current_switch->last_case == NULL) {
6001 current_switch->first_case =
6002 current_switch->last_case = &statement->case_label;
6004 current_switch->last_case->next = &statement->case_label;
6008 errorf(statement->base.source_position,
6009 "'default' label not within a switch statement");
6011 statement->case_label.statement = parse_statement();
6019 * Return the declaration for a given label symbol or create a new one.
6021 static declaration_t *get_label(symbol_t *symbol)
6023 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
6024 assert(current_function != NULL);
6025 /* if we found a label in the same function, then we already created the
6027 if(candidate != NULL
6028 && candidate->parent_scope == ¤t_function->scope) {
6032 /* otherwise we need to create a new one */
6033 declaration_t *const declaration = allocate_declaration_zero();
6034 declaration->namespc = NAMESPACE_LABEL;
6035 declaration->symbol = symbol;
6037 label_push(declaration);
6043 * Parse a label statement.
6045 static statement_t *parse_label_statement(void)
6047 assert(token.type == T_IDENTIFIER);
6048 symbol_t *symbol = token.v.symbol;
6051 declaration_t *label = get_label(symbol);
6053 /* if source position is already set then the label is defined twice,
6054 * otherwise it was just mentioned in a goto so far */
6055 if(label->source_position.input_name != NULL) {
6056 errorf(HERE, "duplicate label '%Y'", symbol);
6057 errorf(label->source_position, "previous definition of '%Y' was here",
6060 label->source_position = token.source_position;
6063 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
6065 statement->base.source_position = token.source_position;
6066 statement->label.label = label;
6070 if(token.type == '}') {
6071 /* TODO only warn? */
6072 errorf(HERE, "label at end of compound statement");
6075 if (token.type == ';') {
6076 /* eat an empty statement here, to avoid the warning about an empty
6077 * after a label. label:; is commonly used to have a label before
6081 statement->label.statement = parse_statement();
6085 /* remember the labels's in a list for later checking */
6086 if (label_last == NULL) {
6087 label_first = &statement->label;
6089 label_last->next = &statement->label;
6091 label_last = &statement->label;
6097 * Parse an if statement.
6099 static statement_t *parse_if(void)
6103 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
6104 statement->base.source_position = token.source_position;
6107 statement->ifs.condition = parse_expression();
6110 statement->ifs.true_statement = parse_statement();
6111 if(token.type == T_else) {
6113 statement->ifs.false_statement = parse_statement();
6122 * Parse a switch statement.
6124 static statement_t *parse_switch(void)
6128 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
6129 statement->base.source_position = token.source_position;
6132 expression_t *const expr = parse_expression();
6133 type_t * type = skip_typeref(expr->base.type);
6134 if (is_type_integer(type)) {
6135 type = promote_integer(type);
6136 } else if (is_type_valid(type)) {
6137 errorf(expr->base.source_position,
6138 "switch quantity is not an integer, but '%T'", type);
6139 type = type_error_type;
6141 statement->switchs.expression = create_implicit_cast(expr, type);
6144 switch_statement_t *rem = current_switch;
6145 current_switch = &statement->switchs;
6146 statement->switchs.body = parse_statement();
6147 current_switch = rem;
6149 if (warning.switch_default
6150 && find_default_label(&statement->switchs) == NULL) {
6151 warningf(statement->base.source_position, "switch has no default case");
6159 static statement_t *parse_loop_body(statement_t *const loop)
6161 statement_t *const rem = current_loop;
6162 current_loop = loop;
6164 statement_t *const body = parse_statement();
6171 * Parse a while statement.
6173 static statement_t *parse_while(void)
6177 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
6178 statement->base.source_position = token.source_position;
6181 statement->whiles.condition = parse_expression();
6184 statement->whiles.body = parse_loop_body(statement);
6192 * Parse a do statement.
6194 static statement_t *parse_do(void)
6198 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
6200 statement->base.source_position = token.source_position;
6202 statement->do_while.body = parse_loop_body(statement);
6206 statement->do_while.condition = parse_expression();
6216 * Parse a for statement.
6218 static statement_t *parse_for(void)
6222 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6223 statement->base.source_position = token.source_position;
6225 int top = environment_top();
6226 scope_t *last_scope = scope;
6227 set_scope(&statement->fors.scope);
6231 if(token.type != ';') {
6232 if(is_declaration_specifier(&token, false)) {
6233 parse_declaration(record_declaration);
6235 expression_t *const init = parse_expression();
6236 statement->fors.initialisation = init;
6237 if (warning.unused_value && !expression_has_effect(init)) {
6238 warningf(init->base.source_position,
6239 "initialisation of 'for'-statement has no effect");
6247 if(token.type != ';') {
6248 statement->fors.condition = parse_expression();
6251 if(token.type != ')') {
6252 expression_t *const step = parse_expression();
6253 statement->fors.step = step;
6254 if (warning.unused_value && !expression_has_effect(step)) {
6255 warningf(step->base.source_position,
6256 "step of 'for'-statement has no effect");
6260 statement->fors.body = parse_loop_body(statement);
6262 assert(scope == &statement->fors.scope);
6263 set_scope(last_scope);
6264 environment_pop_to(top);
6269 assert(scope == &statement->fors.scope);
6270 set_scope(last_scope);
6271 environment_pop_to(top);
6277 * Parse a goto statement.
6279 static statement_t *parse_goto(void)
6283 if(token.type != T_IDENTIFIER) {
6284 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6288 symbol_t *symbol = token.v.symbol;
6291 declaration_t *label = get_label(symbol);
6293 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6294 statement->base.source_position = token.source_position;
6296 statement->gotos.label = label;
6298 /* remember the goto's in a list for later checking */
6299 if (goto_last == NULL) {
6300 goto_first = &statement->gotos;
6302 goto_last->next = &statement->gotos;
6304 goto_last = &statement->gotos;
6314 * Parse a continue statement.
6316 static statement_t *parse_continue(void)
6318 statement_t *statement;
6319 if (current_loop == NULL) {
6320 errorf(HERE, "continue statement not within loop");
6323 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6325 statement->base.source_position = token.source_position;
6337 * Parse a break statement.
6339 static statement_t *parse_break(void)
6341 statement_t *statement;
6342 if (current_switch == NULL && current_loop == NULL) {
6343 errorf(HERE, "break statement not within loop or switch");
6346 statement = allocate_statement_zero(STATEMENT_BREAK);
6348 statement->base.source_position = token.source_position;
6360 * Check if a given declaration represents a local variable.
6362 static bool is_local_var_declaration(const declaration_t *declaration) {
6363 switch ((storage_class_tag_t) declaration->storage_class) {
6364 case STORAGE_CLASS_AUTO:
6365 case STORAGE_CLASS_REGISTER: {
6366 const type_t *type = skip_typeref(declaration->type);
6367 if(is_type_function(type)) {
6379 * Check if a given declaration represents a variable.
6381 static bool is_var_declaration(const declaration_t *declaration) {
6382 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6385 const type_t *type = skip_typeref(declaration->type);
6386 return !is_type_function(type);
6390 * Check if a given expression represents a local variable.
6392 static bool is_local_variable(const expression_t *expression)
6394 if (expression->base.kind != EXPR_REFERENCE) {
6397 const declaration_t *declaration = expression->reference.declaration;
6398 return is_local_var_declaration(declaration);
6402 * Check if a given expression represents a local variable and
6403 * return its declaration then, else return NULL.
6405 declaration_t *expr_is_variable(const expression_t *expression)
6407 if (expression->base.kind != EXPR_REFERENCE) {
6410 declaration_t *declaration = expression->reference.declaration;
6411 if (is_var_declaration(declaration))
6417 * Parse a return statement.
6419 static statement_t *parse_return(void)
6423 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6424 statement->base.source_position = token.source_position;
6426 expression_t *return_value = NULL;
6427 if(token.type != ';') {
6428 return_value = parse_expression();
6432 const type_t *const func_type = current_function->type;
6433 assert(is_type_function(func_type));
6434 type_t *const return_type = skip_typeref(func_type->function.return_type);
6436 if(return_value != NULL) {
6437 type_t *return_value_type = skip_typeref(return_value->base.type);
6439 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6440 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6441 warningf(statement->base.source_position,
6442 "'return' with a value, in function returning void");
6443 return_value = NULL;
6445 type_t *const res_type = semantic_assign(return_type,
6446 return_value, "'return'");
6447 if (res_type == NULL) {
6448 errorf(statement->base.source_position,
6449 "cannot return something of type '%T' in function returning '%T'",
6450 return_value->base.type, return_type);
6452 return_value = create_implicit_cast(return_value, res_type);
6455 /* check for returning address of a local var */
6456 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6457 const expression_t *expression = return_value->unary.value;
6458 if (is_local_variable(expression)) {
6459 warningf(statement->base.source_position,
6460 "function returns address of local variable");
6464 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6465 warningf(statement->base.source_position,
6466 "'return' without value, in function returning non-void");
6469 statement->returns.value = return_value;
6477 * Parse a declaration statement.
6479 static statement_t *parse_declaration_statement(void)
6481 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6483 statement->base.source_position = token.source_position;
6485 declaration_t *before = last_declaration;
6486 parse_declaration(record_declaration);
6488 if(before == NULL) {
6489 statement->declaration.declarations_begin = scope->declarations;
6491 statement->declaration.declarations_begin = before->next;
6493 statement->declaration.declarations_end = last_declaration;
6499 * Parse an expression statement, ie. expr ';'.
6501 static statement_t *parse_expression_statement(void)
6503 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6505 statement->base.source_position = token.source_position;
6506 expression_t *const expr = parse_expression();
6507 statement->expression.expression = expr;
6509 if (warning.unused_value && !expression_has_effect(expr)) {
6510 warningf(expr->base.source_position, "statement has no effect");
6521 * Parse a statement.
6523 static statement_t *parse_statement(void)
6525 statement_t *statement = NULL;
6527 /* declaration or statement */
6528 switch(token.type) {
6530 statement = parse_asm_statement();
6534 statement = parse_case_statement();
6538 statement = parse_default_statement();
6542 statement = parse_compound_statement();
6546 statement = parse_if();
6550 statement = parse_switch();
6554 statement = parse_while();
6558 statement = parse_do();
6562 statement = parse_for();
6566 statement = parse_goto();
6570 statement = parse_continue();
6574 statement = parse_break();
6578 statement = parse_return();
6582 if (warning.empty_statement) {
6583 warningf(HERE, "statement is empty");
6590 if(look_ahead(1)->type == ':') {
6591 statement = parse_label_statement();
6595 if(is_typedef_symbol(token.v.symbol)) {
6596 statement = parse_declaration_statement();
6600 statement = parse_expression_statement();
6603 case T___extension__:
6604 /* this can be a prefix to a declaration or an expression statement */
6605 /* we simply eat it now and parse the rest with tail recursion */
6608 } while(token.type == T___extension__);
6609 statement = parse_statement();
6613 statement = parse_declaration_statement();
6617 statement = parse_expression_statement();
6621 assert(statement == NULL
6622 || statement->base.source_position.input_name != NULL);
6628 * Parse a compound statement.
6630 static statement_t *parse_compound_statement(void)
6632 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6634 statement->base.source_position = token.source_position;
6638 int top = environment_top();
6639 scope_t *last_scope = scope;
6640 set_scope(&statement->compound.scope);
6642 statement_t *last_statement = NULL;
6644 while(token.type != '}' && token.type != T_EOF) {
6645 statement_t *sub_statement = parse_statement();
6646 if(sub_statement == NULL)
6649 if(last_statement != NULL) {
6650 last_statement->base.next = sub_statement;
6652 statement->compound.statements = sub_statement;
6655 while(sub_statement->base.next != NULL)
6656 sub_statement = sub_statement->base.next;
6658 last_statement = sub_statement;
6661 if(token.type == '}') {
6664 errorf(statement->base.source_position,
6665 "end of file while looking for closing '}'");
6668 assert(scope == &statement->compound.scope);
6669 set_scope(last_scope);
6670 environment_pop_to(top);
6676 * Initialize builtin types.
6678 static void initialize_builtin_types(void)
6680 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6681 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6682 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6683 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6684 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6685 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6686 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6687 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6689 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6690 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6691 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6692 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6696 * Check for unused global static functions and variables
6698 static void check_unused_globals(void)
6700 if (!warning.unused_function && !warning.unused_variable)
6703 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6704 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6707 type_t *const type = decl->type;
6709 if (is_type_function(skip_typeref(type))) {
6710 if (!warning.unused_function || decl->is_inline)
6713 s = (decl->init.statement != NULL ? "defined" : "declared");
6715 if (!warning.unused_variable)
6721 warningf(decl->source_position, "'%#T' %s but not used",
6722 type, decl->symbol, s);
6727 * Parse a translation unit.
6729 static translation_unit_t *parse_translation_unit(void)
6731 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6733 assert(global_scope == NULL);
6734 global_scope = &unit->scope;
6736 assert(scope == NULL);
6737 set_scope(&unit->scope);
6739 initialize_builtin_types();
6741 while(token.type != T_EOF) {
6742 if (token.type == ';') {
6743 /* TODO error in strict mode */
6744 warningf(HERE, "stray ';' outside of function");
6747 parse_external_declaration();
6751 assert(scope == &unit->scope);
6753 last_declaration = NULL;
6755 assert(global_scope == &unit->scope);
6756 check_unused_globals();
6757 global_scope = NULL;
6765 * @return the translation unit or NULL if errors occurred.
6767 translation_unit_t *parse(void)
6769 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6770 label_stack = NEW_ARR_F(stack_entry_t, 0);
6771 diagnostic_count = 0;
6775 type_set_output(stderr);
6776 ast_set_output(stderr);
6778 lookahead_bufpos = 0;
6779 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6782 translation_unit_t *unit = parse_translation_unit();
6784 DEL_ARR_F(environment_stack);
6785 DEL_ARR_F(label_stack);
6794 * Initialize the parser.
6796 void init_parser(void)
6799 /* add predefined symbols for extended-decl-modifier */
6800 sym_align = symbol_table_insert("align");
6801 sym_allocate = symbol_table_insert("allocate");
6802 sym_dllimport = symbol_table_insert("dllimport");
6803 sym_dllexport = symbol_table_insert("dllexport");
6804 sym_naked = symbol_table_insert("naked");
6805 sym_noinline = symbol_table_insert("noinline");
6806 sym_noreturn = symbol_table_insert("noreturn");
6807 sym_nothrow = symbol_table_insert("nothrow");
6808 sym_novtable = symbol_table_insert("novtable");
6809 sym_property = symbol_table_insert("property");
6810 sym_get = symbol_table_insert("get");
6811 sym_put = symbol_table_insert("put");
6812 sym_selectany = symbol_table_insert("selectany");
6813 sym_thread = symbol_table_insert("thread");
6814 sym_uuid = symbol_table_insert("uuid");
6816 init_expression_parsers();
6817 obstack_init(&temp_obst);
6819 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6820 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6824 * Terminate the parser.
6826 void exit_parser(void)
6828 obstack_free(&temp_obst, NULL);