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;
55 unsigned char alignment; /**< Alignment, 0 if not set. */
57 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
58 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
59 symbol_t *get_property_sym; /**< the name of the get property if set. */
60 symbol_t *put_property_sym; /**< the name of the put property if set. */
65 * An environment for parsing initializers (and compound literals).
67 typedef struct parse_initializer_env_t {
68 type_t *type; /**< the type of the initializer. In case of an
69 array type with unspecified size this gets
70 adjusted to the actual size. */
71 declaration_t *declaration; /**< the declaration that is initialized if any */
72 bool must_be_constant;
73 } parse_initializer_env_t;
75 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
78 static token_t lookahead_buffer[MAX_LOOKAHEAD];
79 static int lookahead_bufpos;
80 static stack_entry_t *environment_stack = NULL;
81 static stack_entry_t *label_stack = NULL;
82 static scope_t *global_scope = NULL;
83 static scope_t *scope = NULL;
84 static declaration_t *last_declaration = NULL;
85 static declaration_t *current_function = NULL;
86 static switch_statement_t *current_switch = NULL;
87 static statement_t *current_loop = NULL;
88 static goto_statement_t *goto_first = NULL;
89 static goto_statement_t *goto_last = NULL;
90 static label_statement_t *label_first = NULL;
91 static label_statement_t *label_last = NULL;
92 static struct obstack temp_obst;
94 /* symbols for Microsoft extended-decl-modifier */
95 static const symbol_t *sym_align = NULL;
96 static const symbol_t *sym_allocate = NULL;
97 static const symbol_t *sym_dllimport = NULL;
98 static const symbol_t *sym_dllexport = NULL;
99 static const symbol_t *sym_naked = NULL;
100 static const symbol_t *sym_noinline = NULL;
101 static const symbol_t *sym_noreturn = NULL;
102 static const symbol_t *sym_nothrow = NULL;
103 static const symbol_t *sym_novtable = NULL;
104 static const symbol_t *sym_property = NULL;
105 static const symbol_t *sym_get = NULL;
106 static const symbol_t *sym_put = NULL;
107 static const symbol_t *sym_selectany = NULL;
108 static const symbol_t *sym_thread = NULL;
109 static const symbol_t *sym_uuid = NULL;
110 static const symbol_t *sym_deprecated = NULL;
112 /** The current source position. */
113 #define HERE token.source_position
115 static type_t *type_valist;
117 static statement_t *parse_compound_statement(void);
118 static statement_t *parse_statement(void);
120 static expression_t *parse_sub_expression(unsigned precedence);
121 static expression_t *parse_expression(void);
122 static type_t *parse_typename(void);
124 static void parse_compound_type_entries(declaration_t *compound_declaration);
125 static declaration_t *parse_declarator(
126 const declaration_specifiers_t *specifiers, bool may_be_abstract);
127 static declaration_t *record_declaration(declaration_t *declaration);
129 static void semantic_comparison(binary_expression_t *expression);
131 #define STORAGE_CLASSES \
138 #define TYPE_QUALIFIERS \
145 #ifdef PROVIDE_COMPLEX
146 #define COMPLEX_SPECIFIERS \
148 #define IMAGINARY_SPECIFIERS \
151 #define COMPLEX_SPECIFIERS
152 #define IMAGINARY_SPECIFIERS
155 #define TYPE_SPECIFIERS \
170 case T___builtin_va_list: \
174 #define DECLARATION_START \
179 #define TYPENAME_START \
184 * Allocate an AST node with given size and
185 * initialize all fields with zero.
187 static void *allocate_ast_zero(size_t size)
189 void *res = allocate_ast(size);
190 memset(res, 0, size);
194 static declaration_t *allocate_declaration_zero(void)
196 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
197 declaration->type = type_error_type;
198 declaration->alignment = 0;
203 * Returns the size of a statement node.
205 * @param kind the statement kind
207 static size_t get_statement_struct_size(statement_kind_t kind)
209 static const size_t sizes[] = {
210 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
211 [STATEMENT_RETURN] = sizeof(return_statement_t),
212 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
213 [STATEMENT_IF] = sizeof(if_statement_t),
214 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
215 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
216 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
217 [STATEMENT_BREAK] = sizeof(statement_base_t),
218 [STATEMENT_GOTO] = sizeof(goto_statement_t),
219 [STATEMENT_LABEL] = sizeof(label_statement_t),
220 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
221 [STATEMENT_WHILE] = sizeof(while_statement_t),
222 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
223 [STATEMENT_FOR] = sizeof(for_statement_t),
224 [STATEMENT_ASM] = sizeof(asm_statement_t)
226 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
227 assert(sizes[kind] != 0);
232 * Allocate a statement node of given kind and initialize all
235 static statement_t *allocate_statement_zero(statement_kind_t kind)
237 size_t size = get_statement_struct_size(kind);
238 statement_t *res = allocate_ast_zero(size);
240 res->base.kind = kind;
245 * Returns the size of an expression node.
247 * @param kind the expression kind
249 static size_t get_expression_struct_size(expression_kind_t kind)
251 static const size_t sizes[] = {
252 [EXPR_INVALID] = sizeof(expression_base_t),
253 [EXPR_REFERENCE] = sizeof(reference_expression_t),
254 [EXPR_CONST] = sizeof(const_expression_t),
255 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
256 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
257 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
258 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
259 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
260 [EXPR_CALL] = sizeof(call_expression_t),
261 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
262 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
263 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
264 [EXPR_SELECT] = sizeof(select_expression_t),
265 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
266 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
267 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
268 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
269 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
270 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
271 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
272 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
273 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
274 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
275 [EXPR_VA_START] = sizeof(va_start_expression_t),
276 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
277 [EXPR_STATEMENT] = sizeof(statement_expression_t),
279 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
280 return sizes[EXPR_UNARY_FIRST];
282 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
283 return sizes[EXPR_BINARY_FIRST];
285 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
286 assert(sizes[kind] != 0);
291 * Allocate an expression node of given kind and initialize all
294 static expression_t *allocate_expression_zero(expression_kind_t kind)
296 size_t size = get_expression_struct_size(kind);
297 expression_t *res = allocate_ast_zero(size);
299 res->base.kind = kind;
300 res->base.type = type_error_type;
305 * Returns the size of a type node.
307 * @param kind the type kind
309 static size_t get_type_struct_size(type_kind_t kind)
311 static const size_t sizes[] = {
312 [TYPE_ATOMIC] = sizeof(atomic_type_t),
313 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
314 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
315 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
316 [TYPE_ENUM] = sizeof(enum_type_t),
317 [TYPE_FUNCTION] = sizeof(function_type_t),
318 [TYPE_POINTER] = sizeof(pointer_type_t),
319 [TYPE_ARRAY] = sizeof(array_type_t),
320 [TYPE_BUILTIN] = sizeof(builtin_type_t),
321 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
322 [TYPE_TYPEOF] = sizeof(typeof_type_t),
324 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
325 assert(kind <= TYPE_TYPEOF);
326 assert(sizes[kind] != 0);
331 * Allocate a type node of given kind and initialize all
334 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
336 size_t size = get_type_struct_size(kind);
337 type_t *res = obstack_alloc(type_obst, size);
338 memset(res, 0, size);
340 res->base.kind = kind;
341 res->base.source_position = source_position;
346 * Returns the size of an initializer node.
348 * @param kind the initializer kind
350 static size_t get_initializer_size(initializer_kind_t kind)
352 static const size_t sizes[] = {
353 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
354 [INITIALIZER_STRING] = sizeof(initializer_string_t),
355 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
356 [INITIALIZER_LIST] = sizeof(initializer_list_t),
357 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
359 assert(kind < sizeof(sizes) / sizeof(*sizes));
360 assert(sizes[kind] != 0);
365 * Allocate an initializer node of given kind and initialize all
368 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
370 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
377 * Free a type from the type obstack.
379 static void free_type(void *type)
381 obstack_free(type_obst, type);
385 * Returns the index of the top element of the environment stack.
387 static size_t environment_top(void)
389 return ARR_LEN(environment_stack);
393 * Returns the index of the top element of the label stack.
395 static size_t label_top(void)
397 return ARR_LEN(label_stack);
402 * Return the next token.
404 static inline void next_token(void)
406 token = lookahead_buffer[lookahead_bufpos];
407 lookahead_buffer[lookahead_bufpos] = lexer_token;
410 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
413 print_token(stderr, &token);
414 fprintf(stderr, "\n");
419 * Return the next token with a given lookahead.
421 static inline const token_t *look_ahead(int num)
423 assert(num > 0 && num <= MAX_LOOKAHEAD);
424 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
425 return &lookahead_buffer[pos];
428 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
431 * Report a parse error because an expected token was not found.
433 static void parse_error_expected(const char *message, ...)
435 if(message != NULL) {
436 errorf(HERE, "%s", message);
439 va_start(ap, message);
440 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
445 * Report a type error.
447 static void type_error(const char *msg, const source_position_t source_position,
450 errorf(source_position, "%s, but found type '%T'", msg, type);
454 * Report an incompatible type.
456 static void type_error_incompatible(const char *msg,
457 const source_position_t source_position, type_t *type1, type_t *type2)
459 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
463 * Eat an complete block, ie. '{ ... }'.
465 static void eat_block(void)
467 if(token.type == '{')
470 while(token.type != '}') {
471 if(token.type == T_EOF)
473 if(token.type == '{') {
483 * Eat a statement until an ';' token.
485 static void eat_statement(void)
487 while(token.type != ';') {
488 if(token.type == T_EOF)
490 if(token.type == '}')
492 if(token.type == '{') {
502 * Eat a parenthesed term, ie. '( ... )'.
504 static void eat_paren(void)
506 if(token.type == '(')
509 while(token.type != ')') {
510 if(token.type == T_EOF)
512 if(token.type == ')' || token.type == ';' || token.type == '}') {
515 if(token.type == ')') {
519 if(token.type == '(') {
523 if(token.type == '{') {
532 * Expect the the current token is the expected token.
533 * If not, generate an error, eat the current statement,
534 * and goto the end_error label.
536 #define expect(expected) \
538 if(UNLIKELY(token.type != (expected))) { \
539 parse_error_expected(NULL, (expected), 0); \
546 #define expect_block(expected) \
548 if(UNLIKELY(token.type != (expected))) { \
549 parse_error_expected(NULL, (expected), 0); \
556 static void set_scope(scope_t *new_scope)
559 scope->last_declaration = last_declaration;
563 last_declaration = new_scope->last_declaration;
567 * Search a symbol in a given namespace and returns its declaration or
568 * NULL if this symbol was not found.
570 static declaration_t *get_declaration(const symbol_t *const symbol,
571 const namespace_t namespc)
573 declaration_t *declaration = symbol->declaration;
574 for( ; declaration != NULL; declaration = declaration->symbol_next) {
575 if(declaration->namespc == namespc)
583 * pushs an environment_entry on the environment stack and links the
584 * corresponding symbol to the new entry
586 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
588 symbol_t *symbol = declaration->symbol;
589 namespace_t namespc = (namespace_t) declaration->namespc;
591 /* replace/add declaration into declaration list of the symbol */
592 declaration_t *iter = symbol->declaration;
594 symbol->declaration = declaration;
596 declaration_t *iter_last = NULL;
597 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
598 /* replace an entry? */
599 if(iter->namespc == namespc) {
600 if(iter_last == NULL) {
601 symbol->declaration = declaration;
603 iter_last->symbol_next = declaration;
605 declaration->symbol_next = iter->symbol_next;
610 assert(iter_last->symbol_next == NULL);
611 iter_last->symbol_next = declaration;
615 /* remember old declaration */
617 entry.symbol = symbol;
618 entry.old_declaration = iter;
619 entry.namespc = (unsigned short) namespc;
620 ARR_APP1(stack_entry_t, *stack_ptr, entry);
623 static void environment_push(declaration_t *declaration)
625 assert(declaration->source_position.input_name != NULL);
626 assert(declaration->parent_scope != NULL);
627 stack_push(&environment_stack, declaration);
630 static void label_push(declaration_t *declaration)
632 declaration->parent_scope = ¤t_function->scope;
633 stack_push(&label_stack, declaration);
637 * pops symbols from the environment stack until @p new_top is the top element
639 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
641 stack_entry_t *stack = *stack_ptr;
642 size_t top = ARR_LEN(stack);
645 assert(new_top <= top);
649 for(i = top; i > new_top; --i) {
650 stack_entry_t *entry = &stack[i - 1];
652 declaration_t *old_declaration = entry->old_declaration;
653 symbol_t *symbol = entry->symbol;
654 namespace_t namespc = (namespace_t)entry->namespc;
656 /* replace/remove declaration */
657 declaration_t *declaration = symbol->declaration;
658 assert(declaration != NULL);
659 if(declaration->namespc == namespc) {
660 if(old_declaration == NULL) {
661 symbol->declaration = declaration->symbol_next;
663 symbol->declaration = old_declaration;
666 declaration_t *iter_last = declaration;
667 declaration_t *iter = declaration->symbol_next;
668 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
669 /* replace an entry? */
670 if(iter->namespc == namespc) {
671 assert(iter_last != NULL);
672 iter_last->symbol_next = old_declaration;
673 if(old_declaration != NULL) {
674 old_declaration->symbol_next = iter->symbol_next;
679 assert(iter != NULL);
683 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
686 static void environment_pop_to(size_t new_top)
688 stack_pop_to(&environment_stack, new_top);
691 static void label_pop_to(size_t new_top)
693 stack_pop_to(&label_stack, new_top);
697 static int get_rank(const type_t *type)
699 assert(!is_typeref(type));
700 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
701 * and esp. footnote 108). However we can't fold constants (yet), so we
702 * can't decide whether unsigned int is possible, while int always works.
703 * (unsigned int would be preferable when possible... for stuff like
704 * struct { enum { ... } bla : 4; } ) */
705 if(type->kind == TYPE_ENUM)
706 return ATOMIC_TYPE_INT;
708 assert(type->kind == TYPE_ATOMIC);
709 return type->atomic.akind;
712 static type_t *promote_integer(type_t *type)
714 if(type->kind == TYPE_BITFIELD)
715 type = type->bitfield.base;
717 if(get_rank(type) < ATOMIC_TYPE_INT)
724 * Create a cast expression.
726 * @param expression the expression to cast
727 * @param dest_type the destination type
729 static expression_t *create_cast_expression(expression_t *expression,
732 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
734 cast->unary.value = expression;
735 cast->base.type = dest_type;
741 * Check if a given expression represents the 0 pointer constant.
743 static bool is_null_pointer_constant(const expression_t *expression)
745 /* skip void* cast */
746 if(expression->kind == EXPR_UNARY_CAST
747 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
748 expression = expression->unary.value;
751 /* TODO: not correct yet, should be any constant integer expression
752 * which evaluates to 0 */
753 if (expression->kind != EXPR_CONST)
756 type_t *const type = skip_typeref(expression->base.type);
757 if (!is_type_integer(type))
760 return expression->conste.v.int_value == 0;
764 * Create an implicit cast expression.
766 * @param expression the expression to cast
767 * @param dest_type the destination type
769 static expression_t *create_implicit_cast(expression_t *expression,
772 type_t *const source_type = expression->base.type;
774 if (source_type == dest_type)
777 return create_cast_expression(expression, dest_type);
780 /** Implements the rules from § 6.5.16.1 */
781 static type_t *semantic_assign(type_t *orig_type_left,
782 const expression_t *const right,
785 type_t *const orig_type_right = right->base.type;
786 type_t *const type_left = skip_typeref(orig_type_left);
787 type_t *const type_right = skip_typeref(orig_type_right);
789 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
790 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
791 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
792 && is_type_pointer(type_right))) {
793 return orig_type_left;
796 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
797 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
798 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
800 /* the left type has all qualifiers from the right type */
801 unsigned missing_qualifiers
802 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
803 if(missing_qualifiers != 0) {
804 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
805 return orig_type_left;
808 points_to_left = get_unqualified_type(points_to_left);
809 points_to_right = get_unqualified_type(points_to_right);
811 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
812 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
813 return orig_type_left;
816 if (!types_compatible(points_to_left, points_to_right)) {
817 warningf(right->base.source_position,
818 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
819 orig_type_left, context, right, orig_type_right);
822 return orig_type_left;
825 if ((is_type_compound(type_left) && is_type_compound(type_right))
826 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
827 type_t *const unqual_type_left = get_unqualified_type(type_left);
828 type_t *const unqual_type_right = get_unqualified_type(type_right);
829 if (types_compatible(unqual_type_left, unqual_type_right)) {
830 return orig_type_left;
834 if (!is_type_valid(type_left))
837 if (!is_type_valid(type_right))
838 return orig_type_right;
843 static expression_t *parse_constant_expression(void)
845 /* start parsing at precedence 7 (conditional expression) */
846 expression_t *result = parse_sub_expression(7);
848 if(!is_constant_expression(result)) {
849 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
855 static expression_t *parse_assignment_expression(void)
857 /* start parsing at precedence 2 (assignment expression) */
858 return parse_sub_expression(2);
861 static type_t *make_global_typedef(const char *name, type_t *type)
863 symbol_t *const symbol = symbol_table_insert(name);
865 declaration_t *const declaration = allocate_declaration_zero();
866 declaration->namespc = NAMESPACE_NORMAL;
867 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
868 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
869 declaration->type = type;
870 declaration->symbol = symbol;
871 declaration->source_position = builtin_source_position;
873 record_declaration(declaration);
875 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
876 typedef_type->typedeft.declaration = declaration;
881 static string_t parse_string_literals(void)
883 assert(token.type == T_STRING_LITERAL);
884 string_t result = token.v.string;
888 while (token.type == T_STRING_LITERAL) {
889 result = concat_strings(&result, &token.v.string);
896 static void parse_attributes(void)
900 case T___attribute__: {
908 errorf(HERE, "EOF while parsing attribute");
927 if(token.type != T_STRING_LITERAL) {
928 parse_error_expected("while parsing assembler attribute",
933 parse_string_literals();
938 goto attributes_finished;
947 static designator_t *parse_designation(void)
949 designator_t *result = NULL;
950 designator_t *last = NULL;
953 designator_t *designator;
956 designator = allocate_ast_zero(sizeof(designator[0]));
957 designator->source_position = token.source_position;
959 designator->array_index = parse_constant_expression();
963 designator = allocate_ast_zero(sizeof(designator[0]));
964 designator->source_position = token.source_position;
966 if(token.type != T_IDENTIFIER) {
967 parse_error_expected("while parsing designator",
971 designator->symbol = token.v.symbol;
979 assert(designator != NULL);
981 last->next = designator;
991 static initializer_t *initializer_from_string(array_type_t *type,
992 const string_t *const string)
994 /* TODO: check len vs. size of array type */
997 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
998 initializer->string.string = *string;
1003 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1004 wide_string_t *const string)
1006 /* TODO: check len vs. size of array type */
1009 initializer_t *const initializer =
1010 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1011 initializer->wide_string.string = *string;
1017 * Build an initializer from a given expression.
1019 static initializer_t *initializer_from_expression(type_t *orig_type,
1020 expression_t *expression)
1022 /* TODO check that expression is a constant expression */
1024 /* § 6.7.8.14/15 char array may be initialized by string literals */
1025 type_t *type = skip_typeref(orig_type);
1026 type_t *expr_type_orig = expression->base.type;
1027 type_t *expr_type = skip_typeref(expr_type_orig);
1028 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1029 array_type_t *const array_type = &type->array;
1030 type_t *const element_type = skip_typeref(array_type->element_type);
1032 if (element_type->kind == TYPE_ATOMIC) {
1033 atomic_type_kind_t akind = element_type->atomic.akind;
1034 switch (expression->kind) {
1035 case EXPR_STRING_LITERAL:
1036 if (akind == ATOMIC_TYPE_CHAR
1037 || akind == ATOMIC_TYPE_SCHAR
1038 || akind == ATOMIC_TYPE_UCHAR) {
1039 return initializer_from_string(array_type,
1040 &expression->string.value);
1043 case EXPR_WIDE_STRING_LITERAL: {
1044 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1045 if (get_unqualified_type(element_type) == bare_wchar_type) {
1046 return initializer_from_wide_string(array_type,
1047 &expression->wide_string.value);
1057 type_t *const res_type = semantic_assign(type, expression, "initializer");
1058 if (res_type == NULL)
1061 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1062 result->value.value = create_implicit_cast(expression, res_type);
1068 * Checks if a given expression can be used as an constant initializer.
1070 static bool is_initializer_constant(const expression_t *expression)
1072 return is_constant_expression(expression)
1073 || is_address_constant(expression);
1077 * Parses an scalar initializer.
1079 * § 6.7.8.11; eat {} without warning
1081 static initializer_t *parse_scalar_initializer(type_t *type,
1082 bool must_be_constant)
1084 /* there might be extra {} hierarchies */
1086 while(token.type == '{') {
1089 warningf(HERE, "extra curly braces around scalar initializer");
1094 expression_t *expression = parse_assignment_expression();
1095 if(must_be_constant && !is_initializer_constant(expression)) {
1096 errorf(expression->base.source_position,
1097 "Initialisation expression '%E' is not constant\n",
1101 initializer_t *initializer = initializer_from_expression(type, expression);
1103 if(initializer == NULL) {
1104 errorf(expression->base.source_position,
1105 "expression '%E' doesn't match expected type '%T'",
1111 bool additional_warning_displayed = false;
1113 if(token.type == ',') {
1116 if(token.type != '}') {
1117 if(!additional_warning_displayed) {
1118 warningf(HERE, "additional elements in scalar initializer");
1119 additional_warning_displayed = true;
1130 * An entry in the type path.
1132 typedef struct type_path_entry_t type_path_entry_t;
1133 struct type_path_entry_t {
1134 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1136 size_t index; /**< For array types: the current index. */
1137 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1142 * A type path expression a position inside compound or array types.
1144 typedef struct type_path_t type_path_t;
1145 struct type_path_t {
1146 type_path_entry_t *path; /**< An flexible array containing the current path. */
1147 type_t *top_type; /**< type of the element the path points */
1148 size_t max_index; /**< largest index in outermost array */
1152 * Prints a type path for debugging.
1154 static __attribute__((unused)) void debug_print_type_path(
1155 const type_path_t *path)
1157 size_t len = ARR_LEN(path->path);
1159 for(size_t i = 0; i < len; ++i) {
1160 const type_path_entry_t *entry = & path->path[i];
1162 type_t *type = skip_typeref(entry->type);
1163 if(is_type_compound(type)) {
1164 /* in gcc mode structs can have no members */
1165 if(entry->v.compound_entry == NULL) {
1169 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1170 } else if(is_type_array(type)) {
1171 fprintf(stderr, "[%u]", entry->v.index);
1173 fprintf(stderr, "-INVALID-");
1176 if(path->top_type != NULL) {
1177 fprintf(stderr, " (");
1178 print_type(path->top_type);
1179 fprintf(stderr, ")");
1184 * Return the top type path entry, ie. in a path
1185 * (type).a.b returns the b.
1187 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1189 size_t len = ARR_LEN(path->path);
1191 return &path->path[len-1];
1195 * Enlarge the type path by an (empty) element.
1197 static type_path_entry_t *append_to_type_path(type_path_t *path)
1199 size_t len = ARR_LEN(path->path);
1200 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1202 type_path_entry_t *result = & path->path[len];
1203 memset(result, 0, sizeof(result[0]));
1208 * Descending into a sub-type. Enter the scope of the current
1211 static void descend_into_subtype(type_path_t *path)
1213 type_t *orig_top_type = path->top_type;
1214 type_t *top_type = skip_typeref(orig_top_type);
1216 assert(is_type_compound(top_type) || is_type_array(top_type));
1218 type_path_entry_t *top = append_to_type_path(path);
1219 top->type = top_type;
1221 if(is_type_compound(top_type)) {
1222 declaration_t *declaration = top_type->compound.declaration;
1223 declaration_t *entry = declaration->scope.declarations;
1224 top->v.compound_entry = entry;
1227 path->top_type = entry->type;
1229 path->top_type = NULL;
1232 assert(is_type_array(top_type));
1235 path->top_type = top_type->array.element_type;
1240 * Pop an entry from the given type path, ie. returning from
1241 * (type).a.b to (type).a
1243 static void ascend_from_subtype(type_path_t *path)
1245 type_path_entry_t *top = get_type_path_top(path);
1247 path->top_type = top->type;
1249 size_t len = ARR_LEN(path->path);
1250 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1254 * Pop entries from the given type path until the given
1255 * path level is reached.
1257 static void ascend_to(type_path_t *path, size_t top_path_level)
1259 size_t len = ARR_LEN(path->path);
1261 while(len > top_path_level) {
1262 ascend_from_subtype(path);
1263 len = ARR_LEN(path->path);
1267 static bool walk_designator(type_path_t *path, const designator_t *designator,
1268 bool used_in_offsetof)
1270 for( ; designator != NULL; designator = designator->next) {
1271 type_path_entry_t *top = get_type_path_top(path);
1272 type_t *orig_type = top->type;
1274 type_t *type = skip_typeref(orig_type);
1276 if(designator->symbol != NULL) {
1277 symbol_t *symbol = designator->symbol;
1278 if(!is_type_compound(type)) {
1279 if(is_type_valid(type)) {
1280 errorf(designator->source_position,
1281 "'.%Y' designator used for non-compound type '%T'",
1287 declaration_t *declaration = type->compound.declaration;
1288 declaration_t *iter = declaration->scope.declarations;
1289 for( ; iter != NULL; iter = iter->next) {
1290 if(iter->symbol == symbol) {
1295 errorf(designator->source_position,
1296 "'%T' has no member named '%Y'", orig_type, symbol);
1299 if(used_in_offsetof) {
1300 type_t *real_type = skip_typeref(iter->type);
1301 if(real_type->kind == TYPE_BITFIELD) {
1302 errorf(designator->source_position,
1303 "offsetof designator '%Y' may not specify bitfield",
1309 top->type = orig_type;
1310 top->v.compound_entry = iter;
1311 orig_type = iter->type;
1313 expression_t *array_index = designator->array_index;
1314 assert(designator->array_index != NULL);
1316 if(!is_type_array(type)) {
1317 if(is_type_valid(type)) {
1318 errorf(designator->source_position,
1319 "[%E] designator used for non-array type '%T'",
1320 array_index, orig_type);
1324 if(!is_type_valid(array_index->base.type)) {
1328 long index = fold_constant(array_index);
1329 if(!used_in_offsetof) {
1331 errorf(designator->source_position,
1332 "array index [%E] must be positive", array_index);
1335 if(type->array.size_constant == true) {
1336 long array_size = type->array.size;
1337 if(index >= array_size) {
1338 errorf(designator->source_position,
1339 "designator [%E] (%d) exceeds array size %d",
1340 array_index, index, array_size);
1346 top->type = orig_type;
1347 top->v.index = (size_t) index;
1348 orig_type = type->array.element_type;
1350 path->top_type = orig_type;
1352 if(designator->next != NULL) {
1353 descend_into_subtype(path);
1362 static void advance_current_object(type_path_t *path, size_t top_path_level)
1364 type_path_entry_t *top = get_type_path_top(path);
1366 type_t *type = skip_typeref(top->type);
1367 if(is_type_union(type)) {
1368 /* in unions only the first element is initialized */
1369 top->v.compound_entry = NULL;
1370 } else if(is_type_struct(type)) {
1371 declaration_t *entry = top->v.compound_entry;
1373 entry = entry->next;
1374 top->v.compound_entry = entry;
1376 path->top_type = entry->type;
1380 assert(is_type_array(type));
1384 if(!type->array.size_constant || top->v.index < type->array.size) {
1389 /* we're past the last member of the current sub-aggregate, try if we
1390 * can ascend in the type hierarchy and continue with another subobject */
1391 size_t len = ARR_LEN(path->path);
1393 if(len > top_path_level) {
1394 ascend_from_subtype(path);
1395 advance_current_object(path, top_path_level);
1397 path->top_type = NULL;
1402 * skip until token is found.
1404 static void skip_until(int type) {
1405 while(token.type != type) {
1406 if(token.type == T_EOF)
1413 * skip any {...} blocks until a closing braket is reached.
1415 static void skip_initializers(void)
1417 if(token.type == '{')
1420 while(token.type != '}') {
1421 if(token.type == T_EOF)
1423 if(token.type == '{') {
1432 * Parse a part of an initialiser for a struct or union,
1434 static initializer_t *parse_sub_initializer(type_path_t *path,
1435 type_t *outer_type, size_t top_path_level,
1436 parse_initializer_env_t *env)
1438 if(token.type == '}') {
1439 /* empty initializer */
1443 type_t *orig_type = path->top_type;
1444 type_t *type = NULL;
1446 if (orig_type == NULL) {
1447 /* We are initializing an empty compound. */
1449 type = skip_typeref(orig_type);
1451 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1452 * initializers in this case. */
1453 if(!is_type_valid(type)) {
1454 skip_initializers();
1459 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1462 designator_t *designator = NULL;
1463 if(token.type == '.' || token.type == '[') {
1464 designator = parse_designation();
1466 /* reset path to toplevel, evaluate designator from there */
1467 ascend_to(path, top_path_level);
1468 if(!walk_designator(path, designator, false)) {
1469 /* can't continue after designation error */
1473 initializer_t *designator_initializer
1474 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1475 designator_initializer->designator.designator = designator;
1476 ARR_APP1(initializer_t*, initializers, designator_initializer);
1481 if(token.type == '{') {
1482 if(type != NULL && is_type_scalar(type)) {
1483 sub = parse_scalar_initializer(type, env->must_be_constant);
1487 if (env->declaration != NULL)
1488 errorf(HERE, "extra brace group at end of initializer for '%Y'",
1489 env->declaration->symbol);
1491 errorf(HERE, "extra brace group at end of initializer");
1493 descend_into_subtype(path);
1495 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1499 ascend_from_subtype(path);
1503 goto error_parse_next;
1507 /* must be an expression */
1508 expression_t *expression = parse_assignment_expression();
1510 if(env->must_be_constant && !is_initializer_constant(expression)) {
1511 errorf(expression->base.source_position,
1512 "Initialisation expression '%E' is not constant\n",
1517 /* we are already outside, ... */
1521 /* handle { "string" } special case */
1522 if((expression->kind == EXPR_STRING_LITERAL
1523 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1524 && outer_type != NULL) {
1525 sub = initializer_from_expression(outer_type, expression);
1527 if(token.type == ',') {
1530 if(token.type != '}') {
1531 warningf(HERE, "excessive elements in initializer for type '%T'",
1534 /* TODO: eat , ... */
1539 /* descend into subtypes until expression matches type */
1541 orig_type = path->top_type;
1542 type = skip_typeref(orig_type);
1544 sub = initializer_from_expression(orig_type, expression);
1548 if(!is_type_valid(type)) {
1551 if(is_type_scalar(type)) {
1552 errorf(expression->base.source_position,
1553 "expression '%E' doesn't match expected type '%T'",
1554 expression, orig_type);
1558 descend_into_subtype(path);
1562 /* update largest index of top array */
1563 const type_path_entry_t *first = &path->path[0];
1564 type_t *first_type = first->type;
1565 first_type = skip_typeref(first_type);
1566 if(is_type_array(first_type)) {
1567 size_t index = first->v.index;
1568 if(index > path->max_index)
1569 path->max_index = index;
1573 /* append to initializers list */
1574 ARR_APP1(initializer_t*, initializers, sub);
1577 if(env->declaration != NULL)
1578 warningf(HERE, "excess elements in struct initializer for '%Y'",
1579 env->declaration->symbol);
1581 warningf(HERE, "excess elements in struct initializer");
1585 if(token.type == '}') {
1589 if(token.type == '}') {
1594 /* advance to the next declaration if we are not at the end */
1595 advance_current_object(path, top_path_level);
1596 orig_type = path->top_type;
1597 if(orig_type != NULL)
1598 type = skip_typeref(orig_type);
1604 size_t len = ARR_LEN(initializers);
1605 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1606 initializer_t *result = allocate_ast_zero(size);
1607 result->kind = INITIALIZER_LIST;
1608 result->list.len = len;
1609 memcpy(&result->list.initializers, initializers,
1610 len * sizeof(initializers[0]));
1612 DEL_ARR_F(initializers);
1613 ascend_to(path, top_path_level);
1618 skip_initializers();
1619 DEL_ARR_F(initializers);
1620 ascend_to(path, top_path_level);
1625 * Parses an initializer. Parsers either a compound literal
1626 * (env->declaration == NULL) or an initializer of a declaration.
1628 static initializer_t *parse_initializer(parse_initializer_env_t *env)
1630 type_t *type = skip_typeref(env->type);
1631 initializer_t *result = NULL;
1634 if(is_type_scalar(type)) {
1635 result = parse_scalar_initializer(type, env->must_be_constant);
1636 } else if(token.type == '{') {
1640 memset(&path, 0, sizeof(path));
1641 path.top_type = env->type;
1642 path.path = NEW_ARR_F(type_path_entry_t, 0);
1644 descend_into_subtype(&path);
1646 result = parse_sub_initializer(&path, env->type, 1, env);
1648 max_index = path.max_index;
1649 DEL_ARR_F(path.path);
1653 /* parse_scalar_initializer() also works in this case: we simply
1654 * have an expression without {} around it */
1655 result = parse_scalar_initializer(type, env->must_be_constant);
1658 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1659 * the array type size */
1660 if(is_type_array(type) && type->array.size_expression == NULL
1661 && result != NULL) {
1663 switch (result->kind) {
1664 case INITIALIZER_LIST:
1665 size = max_index + 1;
1668 case INITIALIZER_STRING:
1669 size = result->string.string.size;
1672 case INITIALIZER_WIDE_STRING:
1673 size = result->wide_string.string.size;
1677 panic("invalid initializer type");
1680 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1681 cnst->base.type = type_size_t;
1682 cnst->conste.v.int_value = size;
1684 type_t *new_type = duplicate_type(type);
1686 new_type->array.size_expression = cnst;
1687 new_type->array.size_constant = true;
1688 new_type->array.size = size;
1689 env->type = new_type;
1697 static declaration_t *append_declaration(declaration_t *declaration);
1699 static declaration_t *parse_compound_type_specifier(bool is_struct)
1707 symbol_t *symbol = NULL;
1708 declaration_t *declaration = NULL;
1710 if (token.type == T___attribute__) {
1715 if(token.type == T_IDENTIFIER) {
1716 symbol = token.v.symbol;
1720 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1722 declaration = get_declaration(symbol, NAMESPACE_UNION);
1724 } else if(token.type != '{') {
1726 parse_error_expected("while parsing struct type specifier",
1727 T_IDENTIFIER, '{', 0);
1729 parse_error_expected("while parsing union type specifier",
1730 T_IDENTIFIER, '{', 0);
1736 if(declaration == NULL) {
1737 declaration = allocate_declaration_zero();
1738 declaration->namespc =
1739 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1740 declaration->source_position = token.source_position;
1741 declaration->symbol = symbol;
1742 declaration->parent_scope = scope;
1743 if (symbol != NULL) {
1744 environment_push(declaration);
1746 append_declaration(declaration);
1749 if(token.type == '{') {
1750 if(declaration->init.is_defined) {
1751 assert(symbol != NULL);
1752 errorf(HERE, "multiple definitions of '%s %Y'",
1753 is_struct ? "struct" : "union", symbol);
1754 declaration->scope.declarations = NULL;
1756 declaration->init.is_defined = true;
1758 parse_compound_type_entries(declaration);
1765 static void parse_enum_entries(type_t *const enum_type)
1769 if(token.type == '}') {
1771 errorf(HERE, "empty enum not allowed");
1776 if(token.type != T_IDENTIFIER) {
1777 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1782 declaration_t *const entry = allocate_declaration_zero();
1783 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1784 entry->type = enum_type;
1785 entry->symbol = token.v.symbol;
1786 entry->source_position = token.source_position;
1789 if(token.type == '=') {
1791 expression_t *value = parse_constant_expression();
1793 value = create_implicit_cast(value, enum_type);
1794 entry->init.enum_value = value;
1799 record_declaration(entry);
1801 if(token.type != ',')
1804 } while(token.type != '}');
1812 static type_t *parse_enum_specifier(void)
1816 declaration_t *declaration;
1819 if(token.type == T_IDENTIFIER) {
1820 symbol = token.v.symbol;
1823 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1824 } else if(token.type != '{') {
1825 parse_error_expected("while parsing enum type specifier",
1826 T_IDENTIFIER, '{', 0);
1833 if(declaration == NULL) {
1834 declaration = allocate_declaration_zero();
1835 declaration->namespc = NAMESPACE_ENUM;
1836 declaration->source_position = token.source_position;
1837 declaration->symbol = symbol;
1838 declaration->parent_scope = scope;
1841 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1842 type->enumt.declaration = declaration;
1844 if(token.type == '{') {
1845 if(declaration->init.is_defined) {
1846 errorf(HERE, "multiple definitions of enum %Y", symbol);
1848 if (symbol != NULL) {
1849 environment_push(declaration);
1851 append_declaration(declaration);
1852 declaration->init.is_defined = 1;
1854 parse_enum_entries(type);
1862 * if a symbol is a typedef to another type, return true
1864 static bool is_typedef_symbol(symbol_t *symbol)
1866 const declaration_t *const declaration =
1867 get_declaration(symbol, NAMESPACE_NORMAL);
1869 declaration != NULL &&
1870 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1873 static type_t *parse_typeof(void)
1881 expression_t *expression = NULL;
1884 switch(token.type) {
1885 case T___extension__:
1886 /* this can be a prefix to a typename or an expression */
1887 /* we simply eat it now. */
1890 } while(token.type == T___extension__);
1894 if(is_typedef_symbol(token.v.symbol)) {
1895 type = parse_typename();
1897 expression = parse_expression();
1898 type = expression->base.type;
1903 type = parse_typename();
1907 expression = parse_expression();
1908 type = expression->base.type;
1914 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1915 typeof_type->typeoft.expression = expression;
1916 typeof_type->typeoft.typeof_type = type;
1924 SPECIFIER_SIGNED = 1 << 0,
1925 SPECIFIER_UNSIGNED = 1 << 1,
1926 SPECIFIER_LONG = 1 << 2,
1927 SPECIFIER_INT = 1 << 3,
1928 SPECIFIER_DOUBLE = 1 << 4,
1929 SPECIFIER_CHAR = 1 << 5,
1930 SPECIFIER_SHORT = 1 << 6,
1931 SPECIFIER_LONG_LONG = 1 << 7,
1932 SPECIFIER_FLOAT = 1 << 8,
1933 SPECIFIER_BOOL = 1 << 9,
1934 SPECIFIER_VOID = 1 << 10,
1935 #ifdef PROVIDE_COMPLEX
1936 SPECIFIER_COMPLEX = 1 << 11,
1937 SPECIFIER_IMAGINARY = 1 << 12,
1941 static type_t *create_builtin_type(symbol_t *const symbol,
1942 type_t *const real_type)
1944 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1945 type->builtin.symbol = symbol;
1946 type->builtin.real_type = real_type;
1948 type_t *result = typehash_insert(type);
1949 if (type != result) {
1956 static type_t *get_typedef_type(symbol_t *symbol)
1958 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1959 if(declaration == NULL
1960 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1963 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1964 type->typedeft.declaration = declaration;
1970 * check for the allowed MS alignment values.
1972 static bool check_elignment_value(long long intvalue) {
1973 if(intvalue < 1 || intvalue > 8192) {
1974 errorf(HERE, "illegal alignment value");
1977 unsigned v = (unsigned)intvalue;
1978 for(unsigned i = 1; i <= 8192; i += i) {
1982 errorf(HERE, "alignment must be power of two");
1986 #define DET_MOD(name, tag) do { \
1987 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
1988 *modifiers |= tag; \
1991 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
1994 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
1996 while(token.type == T_IDENTIFIER) {
1997 symbol = token.v.symbol;
1998 if(symbol == sym_align) {
2001 if(token.type != T_INTEGER)
2003 if(check_elignment_value(token.v.intvalue)) {
2004 if(specifiers->alignment != 0)
2005 warningf(HERE, "align used more than once");
2006 specifiers->alignment = (unsigned char)token.v.intvalue;
2010 } else if(symbol == sym_allocate) {
2013 if(token.type != T_IDENTIFIER)
2015 (void)token.v.symbol;
2017 } else if(symbol == sym_dllimport) {
2019 DET_MOD(dllimport, DM_DLLIMPORT);
2020 } else if(symbol == sym_dllexport) {
2022 DET_MOD(dllexport, DM_DLLEXPORT);
2023 } else if(symbol == sym_thread) {
2025 DET_MOD(thread, DM_THREAD);
2026 } else if(symbol == sym_naked) {
2028 DET_MOD(naked, DM_NAKED);
2029 } else if(symbol == sym_noinline) {
2031 DET_MOD(noinline, DM_NOINLINE);
2032 } else if(symbol == sym_noreturn) {
2034 DET_MOD(noreturn, DM_NORETURN);
2035 } else if(symbol == sym_nothrow) {
2037 DET_MOD(nothrow, DM_NOTHROW);
2038 } else if(symbol == sym_novtable) {
2040 DET_MOD(novtable, DM_NOVTABLE);
2041 } else if(symbol == sym_property) {
2045 bool is_get = false;
2046 if(token.type != T_IDENTIFIER)
2048 if(token.v.symbol == sym_get) {
2050 } else if(token.v.symbol == sym_put) {
2052 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2057 if(token.type != T_IDENTIFIER)
2060 if(specifiers->get_property_sym != NULL) {
2061 errorf(HERE, "get property name already specified");
2063 specifiers->get_property_sym = token.v.symbol;
2066 if(specifiers->put_property_sym != NULL) {
2067 errorf(HERE, "put property name already specified");
2069 specifiers->put_property_sym = token.v.symbol;
2073 if(token.type == ',') {
2080 } else if(symbol == sym_selectany) {
2082 DET_MOD(selectany, DM_SELECTANY);
2083 } else if(symbol == sym_uuid) {
2086 if(token.type != T_STRING_LITERAL)
2090 } else if(symbol == sym_deprecated) {
2092 DET_MOD(deprecated, DM_DEPRECATED);
2093 if(token.type == '(') {
2095 if(token.type == T_STRING_LITERAL) {
2096 specifiers->deprecated_string = token.v.string.begin;
2099 errorf(HERE, "string literal expected");
2104 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2106 if(token.type == '(')
2109 if (token.type == ',')
2116 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2118 type_t *type = NULL;
2119 unsigned type_qualifiers = 0;
2120 unsigned type_specifiers = 0;
2123 specifiers->source_position = token.source_position;
2126 switch(token.type) {
2129 #define MATCH_STORAGE_CLASS(token, class) \
2131 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2132 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2134 specifiers->declared_storage_class = class; \
2138 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2139 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2140 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2141 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2142 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2147 parse_microsoft_extended_decl_modifier(specifiers);
2152 switch (specifiers->declared_storage_class) {
2153 case STORAGE_CLASS_NONE:
2154 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2157 case STORAGE_CLASS_EXTERN:
2158 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2161 case STORAGE_CLASS_STATIC:
2162 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2166 errorf(HERE, "multiple storage classes in declaration specifiers");
2172 /* type qualifiers */
2173 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2175 type_qualifiers |= qualifier; \
2179 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2180 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2181 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2183 case T___extension__:
2188 /* type specifiers */
2189 #define MATCH_SPECIFIER(token, specifier, name) \
2192 if(type_specifiers & specifier) { \
2193 errorf(HERE, "multiple " name " type specifiers given"); \
2195 type_specifiers |= specifier; \
2199 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2200 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2201 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2202 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2203 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2204 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2205 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2206 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2207 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2208 #ifdef PROVIDE_COMPLEX
2209 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2210 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2213 /* only in microsoft mode */
2214 specifiers->decl_modifiers |= DM_FORCEINLINE;
2218 specifiers->is_inline = true;
2223 if(type_specifiers & SPECIFIER_LONG_LONG) {
2224 errorf(HERE, "multiple type specifiers given");
2225 } else if(type_specifiers & SPECIFIER_LONG) {
2226 type_specifiers |= SPECIFIER_LONG_LONG;
2228 type_specifiers |= SPECIFIER_LONG;
2233 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2235 type->compound.declaration = parse_compound_type_specifier(true);
2239 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2241 type->compound.declaration = parse_compound_type_specifier(false);
2245 type = parse_enum_specifier();
2248 type = parse_typeof();
2250 case T___builtin_va_list:
2251 type = duplicate_type(type_valist);
2255 case T___attribute__:
2259 case T_IDENTIFIER: {
2260 /* only parse identifier if we haven't found a type yet */
2261 if(type != NULL || type_specifiers != 0)
2262 goto finish_specifiers;
2264 type_t *typedef_type = get_typedef_type(token.v.symbol);
2266 if(typedef_type == NULL)
2267 goto finish_specifiers;
2270 type = typedef_type;
2274 /* function specifier */
2276 goto finish_specifiers;
2283 atomic_type_kind_t atomic_type;
2285 /* match valid basic types */
2286 switch(type_specifiers) {
2287 case SPECIFIER_VOID:
2288 atomic_type = ATOMIC_TYPE_VOID;
2290 case SPECIFIER_CHAR:
2291 atomic_type = ATOMIC_TYPE_CHAR;
2293 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2294 atomic_type = ATOMIC_TYPE_SCHAR;
2296 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2297 atomic_type = ATOMIC_TYPE_UCHAR;
2299 case SPECIFIER_SHORT:
2300 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2301 case SPECIFIER_SHORT | SPECIFIER_INT:
2302 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2303 atomic_type = ATOMIC_TYPE_SHORT;
2305 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2306 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2307 atomic_type = ATOMIC_TYPE_USHORT;
2310 case SPECIFIER_SIGNED:
2311 case SPECIFIER_SIGNED | SPECIFIER_INT:
2312 atomic_type = ATOMIC_TYPE_INT;
2314 case SPECIFIER_UNSIGNED:
2315 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2316 atomic_type = ATOMIC_TYPE_UINT;
2318 case SPECIFIER_LONG:
2319 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2320 case SPECIFIER_LONG | SPECIFIER_INT:
2321 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2322 atomic_type = ATOMIC_TYPE_LONG;
2324 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2325 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2326 atomic_type = ATOMIC_TYPE_ULONG;
2328 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2329 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2330 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2331 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2333 atomic_type = ATOMIC_TYPE_LONGLONG;
2335 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2336 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2338 atomic_type = ATOMIC_TYPE_ULONGLONG;
2340 case SPECIFIER_FLOAT:
2341 atomic_type = ATOMIC_TYPE_FLOAT;
2343 case SPECIFIER_DOUBLE:
2344 atomic_type = ATOMIC_TYPE_DOUBLE;
2346 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2347 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2349 case SPECIFIER_BOOL:
2350 atomic_type = ATOMIC_TYPE_BOOL;
2352 #ifdef PROVIDE_COMPLEX
2353 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2354 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2356 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2357 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2359 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2360 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2362 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2363 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2365 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2366 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2368 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2369 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2373 /* invalid specifier combination, give an error message */
2374 if(type_specifiers == 0) {
2375 if (! strict_mode) {
2376 if (warning.implicit_int) {
2377 warningf(HERE, "no type specifiers in declaration, using 'int'");
2379 atomic_type = ATOMIC_TYPE_INT;
2382 errorf(HERE, "no type specifiers given in declaration");
2384 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2385 (type_specifiers & SPECIFIER_UNSIGNED)) {
2386 errorf(HERE, "signed and unsigned specifiers gives");
2387 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2388 errorf(HERE, "only integer types can be signed or unsigned");
2390 errorf(HERE, "multiple datatypes in declaration");
2392 atomic_type = ATOMIC_TYPE_INVALID;
2395 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2396 type->atomic.akind = atomic_type;
2399 if(type_specifiers != 0) {
2400 errorf(HERE, "multiple datatypes in declaration");
2404 type->base.qualifiers = type_qualifiers;
2406 type_t *result = typehash_insert(type);
2407 if(newtype && result != type) {
2411 specifiers->type = result;
2416 static type_qualifiers_t parse_type_qualifiers(void)
2418 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2421 switch(token.type) {
2422 /* type qualifiers */
2423 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2424 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2425 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2428 return type_qualifiers;
2433 static declaration_t *parse_identifier_list(void)
2435 declaration_t *declarations = NULL;
2436 declaration_t *last_declaration = NULL;
2438 declaration_t *const declaration = allocate_declaration_zero();
2439 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2440 declaration->source_position = token.source_position;
2441 declaration->symbol = token.v.symbol;
2444 if(last_declaration != NULL) {
2445 last_declaration->next = declaration;
2447 declarations = declaration;
2449 last_declaration = declaration;
2451 if(token.type != ',')
2454 } while(token.type == T_IDENTIFIER);
2456 return declarations;
2459 static void semantic_parameter(declaration_t *declaration)
2461 /* TODO: improve error messages */
2463 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2464 errorf(HERE, "typedef not allowed in parameter list");
2465 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2466 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2467 errorf(HERE, "parameter may only have none or register storage class");
2470 type_t *const orig_type = declaration->type;
2471 type_t * type = skip_typeref(orig_type);
2473 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2474 * into a pointer. § 6.7.5.3 (7) */
2475 if (is_type_array(type)) {
2476 type_t *const element_type = type->array.element_type;
2478 type = make_pointer_type(element_type, type->base.qualifiers);
2480 declaration->type = type;
2483 if(is_type_incomplete(type)) {
2484 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2485 orig_type, declaration->symbol);
2489 static declaration_t *parse_parameter(void)
2491 declaration_specifiers_t specifiers;
2492 memset(&specifiers, 0, sizeof(specifiers));
2494 parse_declaration_specifiers(&specifiers);
2496 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2498 semantic_parameter(declaration);
2503 static declaration_t *parse_parameters(function_type_t *type)
2505 if(token.type == T_IDENTIFIER) {
2506 symbol_t *symbol = token.v.symbol;
2507 if(!is_typedef_symbol(symbol)) {
2508 type->kr_style_parameters = true;
2509 return parse_identifier_list();
2513 if(token.type == ')') {
2514 type->unspecified_parameters = 1;
2517 if(token.type == T_void && look_ahead(1)->type == ')') {
2522 declaration_t *declarations = NULL;
2523 declaration_t *declaration;
2524 declaration_t *last_declaration = NULL;
2525 function_parameter_t *parameter;
2526 function_parameter_t *last_parameter = NULL;
2529 switch(token.type) {
2533 return declarations;
2536 case T___extension__:
2538 declaration = parse_parameter();
2540 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2541 memset(parameter, 0, sizeof(parameter[0]));
2542 parameter->type = declaration->type;
2544 if(last_parameter != NULL) {
2545 last_declaration->next = declaration;
2546 last_parameter->next = parameter;
2548 type->parameters = parameter;
2549 declarations = declaration;
2551 last_parameter = parameter;
2552 last_declaration = declaration;
2556 return declarations;
2558 if(token.type != ',')
2559 return declarations;
2569 } construct_type_kind_t;
2571 typedef struct construct_type_t construct_type_t;
2572 struct construct_type_t {
2573 construct_type_kind_t kind;
2574 construct_type_t *next;
2577 typedef struct parsed_pointer_t parsed_pointer_t;
2578 struct parsed_pointer_t {
2579 construct_type_t construct_type;
2580 type_qualifiers_t type_qualifiers;
2583 typedef struct construct_function_type_t construct_function_type_t;
2584 struct construct_function_type_t {
2585 construct_type_t construct_type;
2586 type_t *function_type;
2589 typedef struct parsed_array_t parsed_array_t;
2590 struct parsed_array_t {
2591 construct_type_t construct_type;
2592 type_qualifiers_t type_qualifiers;
2598 typedef struct construct_base_type_t construct_base_type_t;
2599 struct construct_base_type_t {
2600 construct_type_t construct_type;
2604 static construct_type_t *parse_pointer_declarator(void)
2608 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2609 memset(pointer, 0, sizeof(pointer[0]));
2610 pointer->construct_type.kind = CONSTRUCT_POINTER;
2611 pointer->type_qualifiers = parse_type_qualifiers();
2613 return (construct_type_t*) pointer;
2616 static construct_type_t *parse_array_declarator(void)
2620 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2621 memset(array, 0, sizeof(array[0]));
2622 array->construct_type.kind = CONSTRUCT_ARRAY;
2624 if(token.type == T_static) {
2625 array->is_static = true;
2629 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2630 if(type_qualifiers != 0) {
2631 if(token.type == T_static) {
2632 array->is_static = true;
2636 array->type_qualifiers = type_qualifiers;
2638 if(token.type == '*' && look_ahead(1)->type == ']') {
2639 array->is_variable = true;
2641 } else if(token.type != ']') {
2642 array->size = parse_assignment_expression();
2647 return (construct_type_t*) array;
2652 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2657 if(declaration != NULL) {
2658 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2660 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2663 declaration_t *parameters = parse_parameters(&type->function);
2664 if(declaration != NULL) {
2665 declaration->scope.declarations = parameters;
2668 construct_function_type_t *construct_function_type =
2669 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2670 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2671 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2672 construct_function_type->function_type = type;
2676 return (construct_type_t*) construct_function_type;
2681 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2682 bool may_be_abstract)
2684 /* construct a single linked list of construct_type_t's which describe
2685 * how to construct the final declarator type */
2686 construct_type_t *first = NULL;
2687 construct_type_t *last = NULL;
2690 while(token.type == '*') {
2691 construct_type_t *type = parse_pointer_declarator();
2702 /* TODO: find out if this is correct */
2705 construct_type_t *inner_types = NULL;
2707 switch(token.type) {
2709 if(declaration == NULL) {
2710 errorf(HERE, "no identifier expected in typename");
2712 declaration->symbol = token.v.symbol;
2713 declaration->source_position = token.source_position;
2719 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2725 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2726 /* avoid a loop in the outermost scope, because eat_statement doesn't
2728 if(token.type == '}' && current_function == NULL) {
2736 construct_type_t *p = last;
2739 construct_type_t *type;
2740 switch(token.type) {
2742 type = parse_function_declarator(declaration);
2745 type = parse_array_declarator();
2748 goto declarator_finished;
2751 /* insert in the middle of the list (behind p) */
2753 type->next = p->next;
2764 declarator_finished:
2767 /* append inner_types at the end of the list, we don't to set last anymore
2768 * as it's not needed anymore */
2770 assert(first == NULL);
2771 first = inner_types;
2773 last->next = inner_types;
2781 static type_t *construct_declarator_type(construct_type_t *construct_list,
2784 construct_type_t *iter = construct_list;
2785 for( ; iter != NULL; iter = iter->next) {
2786 switch(iter->kind) {
2787 case CONSTRUCT_INVALID:
2788 panic("invalid type construction found");
2789 case CONSTRUCT_FUNCTION: {
2790 construct_function_type_t *construct_function_type
2791 = (construct_function_type_t*) iter;
2793 type_t *function_type = construct_function_type->function_type;
2795 function_type->function.return_type = type;
2797 type_t *skipped_return_type = skip_typeref(type);
2798 if (is_type_function(skipped_return_type)) {
2799 errorf(HERE, "function returning function is not allowed");
2800 type = type_error_type;
2801 } else if (is_type_array(skipped_return_type)) {
2802 errorf(HERE, "function returning array is not allowed");
2803 type = type_error_type;
2805 type = function_type;
2810 case CONSTRUCT_POINTER: {
2811 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2812 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2813 pointer_type->pointer.points_to = type;
2814 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2816 type = pointer_type;
2820 case CONSTRUCT_ARRAY: {
2821 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2822 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2824 expression_t *size_expression = parsed_array->size;
2825 if(size_expression != NULL) {
2827 = create_implicit_cast(size_expression, type_size_t);
2830 array_type->base.qualifiers = parsed_array->type_qualifiers;
2831 array_type->array.element_type = type;
2832 array_type->array.is_static = parsed_array->is_static;
2833 array_type->array.is_variable = parsed_array->is_variable;
2834 array_type->array.size_expression = size_expression;
2836 if(size_expression != NULL) {
2837 if(is_constant_expression(size_expression)) {
2838 array_type->array.size_constant = true;
2839 array_type->array.size
2840 = fold_constant(size_expression);
2842 array_type->array.is_vla = true;
2846 type_t *skipped_type = skip_typeref(type);
2847 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2848 errorf(HERE, "array of void is not allowed");
2849 type = type_error_type;
2857 type_t *hashed_type = typehash_insert(type);
2858 if(hashed_type != type) {
2859 /* the function type was constructed earlier freeing it here will
2860 * destroy other types... */
2861 if(iter->kind != CONSTRUCT_FUNCTION) {
2871 static declaration_t *parse_declarator(
2872 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2874 declaration_t *const declaration = allocate_declaration_zero();
2875 declaration->declared_storage_class = specifiers->declared_storage_class;
2876 declaration->modifiers = specifiers->decl_modifiers;
2877 declaration->deprecated_string = specifiers->deprecated_string;
2878 declaration->get_property_sym = specifiers->get_property_sym;
2879 declaration->put_property_sym = specifiers->put_property_sym;
2880 declaration->is_inline = specifiers->is_inline;
2882 declaration->storage_class = specifiers->declared_storage_class;
2883 if(declaration->storage_class == STORAGE_CLASS_NONE
2884 && scope != global_scope) {
2885 declaration->storage_class = STORAGE_CLASS_AUTO;
2888 if(specifiers->alignment != 0) {
2889 /* TODO: add checks here */
2890 declaration->alignment = specifiers->alignment;
2893 construct_type_t *construct_type
2894 = parse_inner_declarator(declaration, may_be_abstract);
2895 type_t *const type = specifiers->type;
2896 declaration->type = construct_declarator_type(construct_type, type);
2898 if(construct_type != NULL) {
2899 obstack_free(&temp_obst, construct_type);
2905 static type_t *parse_abstract_declarator(type_t *base_type)
2907 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2909 type_t *result = construct_declarator_type(construct_type, base_type);
2910 if(construct_type != NULL) {
2911 obstack_free(&temp_obst, construct_type);
2917 static declaration_t *append_declaration(declaration_t* const declaration)
2919 if (last_declaration != NULL) {
2920 last_declaration->next = declaration;
2922 scope->declarations = declaration;
2924 last_declaration = declaration;
2929 * Check if the declaration of main is suspicious. main should be a
2930 * function with external linkage, returning int, taking either zero
2931 * arguments, two, or three arguments of appropriate types, ie.
2933 * int main([ int argc, char **argv [, char **env ] ]).
2935 * @param decl the declaration to check
2936 * @param type the function type of the declaration
2938 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2940 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2941 warningf(decl->source_position, "'main' is normally a non-static function");
2943 if (skip_typeref(func_type->return_type) != type_int) {
2944 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2946 const function_parameter_t *parm = func_type->parameters;
2948 type_t *const first_type = parm->type;
2949 if (!types_compatible(skip_typeref(first_type), type_int)) {
2950 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2954 type_t *const second_type = parm->type;
2955 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2956 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2960 type_t *const third_type = parm->type;
2961 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2962 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2966 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2970 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2976 * Check if a symbol is the equal to "main".
2978 static bool is_sym_main(const symbol_t *const sym)
2980 return strcmp(sym->string, "main") == 0;
2983 static declaration_t *internal_record_declaration(
2984 declaration_t *const declaration,
2985 const bool is_function_definition)
2987 const symbol_t *const symbol = declaration->symbol;
2988 const namespace_t namespc = (namespace_t)declaration->namespc;
2990 type_t *const orig_type = declaration->type;
2991 type_t *const type = skip_typeref(orig_type);
2992 if (is_type_function(type) &&
2993 type->function.unspecified_parameters &&
2994 warning.strict_prototypes) {
2995 warningf(declaration->source_position,
2996 "function declaration '%#T' is not a prototype",
2997 orig_type, declaration->symbol);
3000 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3001 check_type_of_main(declaration, &type->function);
3004 assert(declaration->symbol != NULL);
3005 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3007 assert(declaration != previous_declaration);
3008 if (previous_declaration != NULL) {
3009 if (previous_declaration->parent_scope == scope) {
3010 /* can happen for K&R style declarations */
3011 if(previous_declaration->type == NULL) {
3012 previous_declaration->type = declaration->type;
3015 const type_t *prev_type = skip_typeref(previous_declaration->type);
3016 if (!types_compatible(type, prev_type)) {
3017 errorf(declaration->source_position,
3018 "declaration '%#T' is incompatible with "
3019 "previous declaration '%#T'",
3020 orig_type, symbol, previous_declaration->type, symbol);
3021 errorf(previous_declaration->source_position,
3022 "previous declaration of '%Y' was here", symbol);
3024 unsigned old_storage_class = previous_declaration->storage_class;
3025 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3026 errorf(declaration->source_position, "redeclaration of enum entry '%Y'", symbol);
3027 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
3028 return previous_declaration;
3031 unsigned new_storage_class = declaration->storage_class;
3033 if(is_type_incomplete(prev_type)) {
3034 previous_declaration->type = type;
3038 /* pretend no storage class means extern for function
3039 * declarations (except if the previous declaration is neither
3040 * none nor extern) */
3041 if (is_type_function(type)) {
3042 switch (old_storage_class) {
3043 case STORAGE_CLASS_NONE:
3044 old_storage_class = STORAGE_CLASS_EXTERN;
3046 case STORAGE_CLASS_EXTERN:
3047 if (is_function_definition) {
3048 if (warning.missing_prototypes &&
3049 prev_type->function.unspecified_parameters &&
3050 !is_sym_main(symbol)) {
3051 warningf(declaration->source_position,
3052 "no previous prototype for '%#T'",
3055 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3056 new_storage_class = STORAGE_CLASS_EXTERN;
3064 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3065 new_storage_class == STORAGE_CLASS_EXTERN) {
3066 warn_redundant_declaration:
3067 if (warning.redundant_decls) {
3068 warningf(declaration->source_position,
3069 "redundant declaration for '%Y'", symbol);
3070 warningf(previous_declaration->source_position,
3071 "previous declaration of '%Y' was here",
3074 } else if (current_function == NULL) {
3075 if (old_storage_class != STORAGE_CLASS_STATIC &&
3076 new_storage_class == STORAGE_CLASS_STATIC) {
3077 errorf(declaration->source_position,
3078 "static declaration of '%Y' follows non-static declaration",
3080 errorf(previous_declaration->source_position,
3081 "previous declaration of '%Y' was here", symbol);
3083 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3084 goto warn_redundant_declaration;
3086 if (new_storage_class == STORAGE_CLASS_NONE) {
3087 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3088 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3092 if (old_storage_class == new_storage_class) {
3093 errorf(declaration->source_position,
3094 "redeclaration of '%Y'", symbol);
3096 errorf(declaration->source_position,
3097 "redeclaration of '%Y' with different linkage",
3100 errorf(previous_declaration->source_position,
3101 "previous declaration of '%Y' was here", symbol);
3104 return previous_declaration;
3106 } else if (is_function_definition) {
3107 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3108 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3109 warningf(declaration->source_position,
3110 "no previous prototype for '%#T'", orig_type, symbol);
3111 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3112 warningf(declaration->source_position,
3113 "no previous declaration for '%#T'", orig_type,
3117 } else if (warning.missing_declarations &&
3118 scope == global_scope &&
3119 !is_type_function(type) && (
3120 declaration->storage_class == STORAGE_CLASS_NONE ||
3121 declaration->storage_class == STORAGE_CLASS_THREAD
3123 warningf(declaration->source_position,
3124 "no previous declaration for '%#T'", orig_type, symbol);
3127 assert(declaration->parent_scope == NULL);
3128 assert(scope != NULL);
3130 declaration->parent_scope = scope;
3132 environment_push(declaration);
3133 return append_declaration(declaration);
3136 static declaration_t *record_declaration(declaration_t *declaration)
3138 return internal_record_declaration(declaration, false);
3141 static declaration_t *record_function_definition(declaration_t *declaration)
3143 return internal_record_declaration(declaration, true);
3146 static void parser_error_multiple_definition(declaration_t *declaration,
3147 const source_position_t source_position)
3149 errorf(source_position, "multiple definition of symbol '%Y'",
3150 declaration->symbol);
3151 errorf(declaration->source_position,
3152 "this is the location of the previous definition.");
3155 static bool is_declaration_specifier(const token_t *token,
3156 bool only_type_specifiers)
3158 switch(token->type) {
3162 return is_typedef_symbol(token->v.symbol);
3164 case T___extension__:
3167 return !only_type_specifiers;
3174 static void parse_init_declarator_rest(declaration_t *declaration)
3178 type_t *orig_type = declaration->type;
3179 type_t *type = skip_typeref(orig_type);
3181 if(declaration->init.initializer != NULL) {
3182 parser_error_multiple_definition(declaration, token.source_position);
3185 bool must_be_constant = false;
3186 if(declaration->storage_class == STORAGE_CLASS_STATIC
3187 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3188 || declaration->parent_scope == global_scope) {
3189 must_be_constant = true;
3192 parse_initializer_env_t env;
3193 env.type = orig_type;
3194 env.must_be_constant = must_be_constant;
3195 env.declaration = declaration;
3197 initializer_t *initializer = parse_initializer(&env);
3199 if(env.type != orig_type) {
3200 orig_type = env.type;
3201 type = skip_typeref(orig_type);
3202 declaration->type = env.type;
3205 if(is_type_function(type)) {
3206 errorf(declaration->source_position,
3207 "initializers not allowed for function types at declator '%Y' (type '%T')",
3208 declaration->symbol, orig_type);
3210 declaration->init.initializer = initializer;
3214 /* parse rest of a declaration without any declarator */
3215 static void parse_anonymous_declaration_rest(
3216 const declaration_specifiers_t *specifiers,
3217 parsed_declaration_func finished_declaration)
3221 declaration_t *const declaration = allocate_declaration_zero();
3222 declaration->type = specifiers->type;
3223 declaration->declared_storage_class = specifiers->declared_storage_class;
3224 declaration->source_position = specifiers->source_position;
3225 declaration->modifiers = specifiers->decl_modifiers;
3227 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3228 warningf(declaration->source_position, "useless storage class in empty declaration");
3230 declaration->storage_class = STORAGE_CLASS_NONE;
3232 type_t *type = declaration->type;
3233 switch (type->kind) {
3234 case TYPE_COMPOUND_STRUCT:
3235 case TYPE_COMPOUND_UNION: {
3236 if (type->compound.declaration->symbol == NULL) {
3237 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
3246 warningf(declaration->source_position, "empty declaration");
3250 finished_declaration(declaration);
3253 static void parse_declaration_rest(declaration_t *ndeclaration,
3254 const declaration_specifiers_t *specifiers,
3255 parsed_declaration_func finished_declaration)
3258 declaration_t *declaration = finished_declaration(ndeclaration);
3260 type_t *orig_type = declaration->type;
3261 type_t *type = skip_typeref(orig_type);
3263 if (type->kind != TYPE_FUNCTION &&
3264 declaration->is_inline &&
3265 is_type_valid(type)) {
3266 warningf(declaration->source_position,
3267 "variable '%Y' declared 'inline'\n", declaration->symbol);
3270 if(token.type == '=') {
3271 parse_init_declarator_rest(declaration);
3274 if(token.type != ',')
3278 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
3286 static declaration_t *finished_kr_declaration(declaration_t *declaration)
3288 symbol_t *symbol = declaration->symbol;
3289 if(symbol == NULL) {
3290 errorf(HERE, "anonymous declaration not valid as function parameter");
3293 namespace_t namespc = (namespace_t) declaration->namespc;
3294 if(namespc != NAMESPACE_NORMAL) {
3295 return record_declaration(declaration);
3298 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3299 if(previous_declaration == NULL ||
3300 previous_declaration->parent_scope != scope) {
3301 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3306 if(previous_declaration->type == NULL) {
3307 previous_declaration->type = declaration->type;
3308 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3309 previous_declaration->storage_class = declaration->storage_class;
3310 previous_declaration->parent_scope = scope;
3311 return previous_declaration;
3313 return record_declaration(declaration);
3317 static void parse_declaration(parsed_declaration_func finished_declaration)
3319 declaration_specifiers_t specifiers;
3320 memset(&specifiers, 0, sizeof(specifiers));
3321 parse_declaration_specifiers(&specifiers);
3323 if(token.type == ';') {
3324 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3326 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3327 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3331 static void parse_kr_declaration_list(declaration_t *declaration)
3333 type_t *type = skip_typeref(declaration->type);
3334 if(!is_type_function(type))
3337 if(!type->function.kr_style_parameters)
3340 /* push function parameters */
3341 int top = environment_top();
3342 scope_t *last_scope = scope;
3343 set_scope(&declaration->scope);
3345 declaration_t *parameter = declaration->scope.declarations;
3346 for( ; parameter != NULL; parameter = parameter->next) {
3347 assert(parameter->parent_scope == NULL);
3348 parameter->parent_scope = scope;
3349 environment_push(parameter);
3352 /* parse declaration list */
3353 while(is_declaration_specifier(&token, false)) {
3354 parse_declaration(finished_kr_declaration);
3357 /* pop function parameters */
3358 assert(scope == &declaration->scope);
3359 set_scope(last_scope);
3360 environment_pop_to(top);
3362 /* update function type */
3363 type_t *new_type = duplicate_type(type);
3364 new_type->function.kr_style_parameters = false;
3366 function_parameter_t *parameters = NULL;
3367 function_parameter_t *last_parameter = NULL;
3369 declaration_t *parameter_declaration = declaration->scope.declarations;
3370 for( ; parameter_declaration != NULL;
3371 parameter_declaration = parameter_declaration->next) {
3372 type_t *parameter_type = parameter_declaration->type;
3373 if(parameter_type == NULL) {
3375 errorf(HERE, "no type specified for function parameter '%Y'",
3376 parameter_declaration->symbol);
3378 if (warning.implicit_int) {
3379 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3380 parameter_declaration->symbol);
3382 parameter_type = type_int;
3383 parameter_declaration->type = parameter_type;
3387 semantic_parameter(parameter_declaration);
3388 parameter_type = parameter_declaration->type;
3390 function_parameter_t *function_parameter
3391 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3392 memset(function_parameter, 0, sizeof(function_parameter[0]));
3394 function_parameter->type = parameter_type;
3395 if(last_parameter != NULL) {
3396 last_parameter->next = function_parameter;
3398 parameters = function_parameter;
3400 last_parameter = function_parameter;
3402 new_type->function.parameters = parameters;
3404 type = typehash_insert(new_type);
3405 if(type != new_type) {
3406 obstack_free(type_obst, new_type);
3409 declaration->type = type;
3412 static bool first_err = true;
3415 * When called with first_err set, prints the name of the current function,
3418 static void print_in_function(void) {
3421 diagnosticf("%s: In function '%Y':\n",
3422 current_function->source_position.input_name,
3423 current_function->symbol);
3428 * Check if all labels are defined in the current function.
3429 * Check if all labels are used in the current function.
3431 static void check_labels(void)
3433 for (const goto_statement_t *goto_statement = goto_first;
3434 goto_statement != NULL;
3435 goto_statement = goto_statement->next) {
3436 declaration_t *label = goto_statement->label;
3439 if (label->source_position.input_name == NULL) {
3440 print_in_function();
3441 errorf(goto_statement->base.source_position,
3442 "label '%Y' used but not defined", label->symbol);
3445 goto_first = goto_last = NULL;
3447 if (warning.unused_label) {
3448 for (const label_statement_t *label_statement = label_first;
3449 label_statement != NULL;
3450 label_statement = label_statement->next) {
3451 const declaration_t *label = label_statement->label;
3453 if (! label->used) {
3454 print_in_function();
3455 warningf(label_statement->base.source_position,
3456 "label '%Y' defined but not used", label->symbol);
3460 label_first = label_last = NULL;
3464 * Check declarations of current_function for unused entities.
3466 static void check_declarations(void)
3468 if (warning.unused_parameter) {
3469 const scope_t *scope = ¤t_function->scope;
3471 const declaration_t *parameter = scope->declarations;
3472 for (; parameter != NULL; parameter = parameter->next) {
3473 if (! parameter->used) {
3474 print_in_function();
3475 warningf(parameter->source_position,
3476 "unused parameter '%Y'", parameter->symbol);
3480 if (warning.unused_variable) {
3484 static void parse_external_declaration(void)
3486 /* function-definitions and declarations both start with declaration
3488 declaration_specifiers_t specifiers;
3489 memset(&specifiers, 0, sizeof(specifiers));
3490 parse_declaration_specifiers(&specifiers);
3492 /* must be a declaration */
3493 if(token.type == ';') {
3494 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3498 /* declarator is common to both function-definitions and declarations */
3499 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3501 /* must be a declaration */
3502 if(token.type == ',' || token.type == '=' || token.type == ';') {
3503 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3507 /* must be a function definition */
3508 parse_kr_declaration_list(ndeclaration);
3510 if(token.type != '{') {
3511 parse_error_expected("while parsing function definition", '{', 0);
3516 type_t *type = ndeclaration->type;
3518 /* note that we don't skip typerefs: the standard doesn't allow them here
3519 * (so we can't use is_type_function here) */
3520 if(type->kind != TYPE_FUNCTION) {
3521 if (is_type_valid(type)) {
3522 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3523 type, ndeclaration->symbol);
3529 /* § 6.7.5.3 (14) a function definition with () means no
3530 * parameters (and not unspecified parameters) */
3531 if(type->function.unspecified_parameters) {
3532 type_t *duplicate = duplicate_type(type);
3533 duplicate->function.unspecified_parameters = false;
3535 type = typehash_insert(duplicate);
3536 if(type != duplicate) {
3537 obstack_free(type_obst, duplicate);
3539 ndeclaration->type = type;
3542 declaration_t *const declaration = record_function_definition(ndeclaration);
3543 if(ndeclaration != declaration) {
3544 declaration->scope = ndeclaration->scope;
3546 type = skip_typeref(declaration->type);
3548 /* push function parameters and switch scope */
3549 int top = environment_top();
3550 scope_t *last_scope = scope;
3551 set_scope(&declaration->scope);
3553 declaration_t *parameter = declaration->scope.declarations;
3554 for( ; parameter != NULL; parameter = parameter->next) {
3555 if(parameter->parent_scope == &ndeclaration->scope) {
3556 parameter->parent_scope = scope;
3558 assert(parameter->parent_scope == NULL
3559 || parameter->parent_scope == scope);
3560 parameter->parent_scope = scope;
3561 environment_push(parameter);
3564 if(declaration->init.statement != NULL) {
3565 parser_error_multiple_definition(declaration, token.source_position);
3567 goto end_of_parse_external_declaration;
3569 /* parse function body */
3570 int label_stack_top = label_top();
3571 declaration_t *old_current_function = current_function;
3572 current_function = declaration;
3574 declaration->init.statement = parse_compound_statement();
3577 check_declarations();
3579 assert(current_function == declaration);
3580 current_function = old_current_function;
3581 label_pop_to(label_stack_top);
3584 end_of_parse_external_declaration:
3585 assert(scope == &declaration->scope);
3586 set_scope(last_scope);
3587 environment_pop_to(top);
3590 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3591 source_position_t source_position)
3593 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3594 type->bitfield.base = base;
3595 type->bitfield.size = size;
3600 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3603 declaration_t *iter = compound_declaration->scope.declarations;
3604 for( ; iter != NULL; iter = iter->next) {
3605 if(iter->namespc != NAMESPACE_NORMAL)
3608 if(iter->symbol == NULL) {
3609 type_t *type = skip_typeref(iter->type);
3610 if(is_type_compound(type)) {
3611 declaration_t *result
3612 = find_compound_entry(type->compound.declaration, symbol);
3619 if(iter->symbol == symbol) {
3627 static void parse_compound_declarators(declaration_t *struct_declaration,
3628 const declaration_specifiers_t *specifiers)
3630 declaration_t *last_declaration = struct_declaration->scope.declarations;
3631 if(last_declaration != NULL) {
3632 while(last_declaration->next != NULL) {
3633 last_declaration = last_declaration->next;
3638 declaration_t *declaration;
3640 if(token.type == ':') {
3641 source_position_t source_position = HERE;
3644 type_t *base_type = specifiers->type;
3645 expression_t *size = parse_constant_expression();
3647 if(!is_type_integer(skip_typeref(base_type))) {
3648 errorf(HERE, "bitfield base type '%T' is not an integer type",
3652 type_t *type = make_bitfield_type(base_type, size, source_position);
3654 declaration = allocate_declaration_zero();
3655 declaration->namespc = NAMESPACE_NORMAL;
3656 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3657 declaration->storage_class = STORAGE_CLASS_NONE;
3658 declaration->source_position = source_position;
3659 declaration->modifiers = specifiers->decl_modifiers;
3660 declaration->type = type;
3662 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3664 type_t *orig_type = declaration->type;
3665 type_t *type = skip_typeref(orig_type);
3667 if(token.type == ':') {
3668 source_position_t source_position = HERE;
3670 expression_t *size = parse_constant_expression();
3672 if(!is_type_integer(type)) {
3673 errorf(HERE, "bitfield base type '%T' is not an "
3674 "integer type", orig_type);
3677 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3678 declaration->type = bitfield_type;
3680 /* TODO we ignore arrays for now... what is missing is a check
3681 * that they're at the end of the struct */
3682 if(is_type_incomplete(type) && !is_type_array(type)) {
3684 "compound member '%Y' has incomplete type '%T'",
3685 declaration->symbol, orig_type);
3686 } else if(is_type_function(type)) {
3687 errorf(HERE, "compound member '%Y' must not have function "
3688 "type '%T'", declaration->symbol, orig_type);
3693 /* make sure we don't define a symbol multiple times */
3694 symbol_t *symbol = declaration->symbol;
3695 if(symbol != NULL) {
3696 declaration_t *prev_decl
3697 = find_compound_entry(struct_declaration, symbol);
3699 if(prev_decl != NULL) {
3700 assert(prev_decl->symbol == symbol);
3701 errorf(declaration->source_position,
3702 "multiple declarations of symbol '%Y'", symbol);
3703 errorf(prev_decl->source_position,
3704 "previous declaration of '%Y' was here", symbol);
3708 /* append declaration */
3709 if(last_declaration != NULL) {
3710 last_declaration->next = declaration;
3712 struct_declaration->scope.declarations = declaration;
3714 last_declaration = declaration;
3716 if(token.type != ',')
3726 static void parse_compound_type_entries(declaration_t *compound_declaration)
3730 while(token.type != '}' && token.type != T_EOF) {
3731 declaration_specifiers_t specifiers;
3732 memset(&specifiers, 0, sizeof(specifiers));
3733 parse_declaration_specifiers(&specifiers);
3735 parse_compound_declarators(compound_declaration, &specifiers);
3737 if(token.type == T_EOF) {
3738 errorf(HERE, "EOF while parsing struct");
3743 static type_t *parse_typename(void)
3745 declaration_specifiers_t specifiers;
3746 memset(&specifiers, 0, sizeof(specifiers));
3747 parse_declaration_specifiers(&specifiers);
3748 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3749 /* TODO: improve error message, user does probably not know what a
3750 * storage class is...
3752 errorf(HERE, "typename may not have a storage class");
3755 type_t *result = parse_abstract_declarator(specifiers.type);
3763 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3764 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3765 expression_t *left);
3767 typedef struct expression_parser_function_t expression_parser_function_t;
3768 struct expression_parser_function_t {
3769 unsigned precedence;
3770 parse_expression_function parser;
3771 unsigned infix_precedence;
3772 parse_expression_infix_function infix_parser;
3775 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3778 * Creates a new invalid expression.
3780 static expression_t *create_invalid_expression(void)
3782 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3783 expression->base.source_position = token.source_position;
3788 * Prints an error message if an expression was expected but not read
3790 static expression_t *expected_expression_error(void)
3792 /* skip the error message if the error token was read */
3793 if (token.type != T_ERROR) {
3794 errorf(HERE, "expected expression, got token '%K'", &token);
3798 return create_invalid_expression();
3802 * Parse a string constant.
3804 static expression_t *parse_string_const(void)
3807 if (token.type == T_STRING_LITERAL) {
3808 string_t res = token.v.string;
3810 while (token.type == T_STRING_LITERAL) {
3811 res = concat_strings(&res, &token.v.string);
3814 if (token.type != T_WIDE_STRING_LITERAL) {
3815 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3816 /* note: that we use type_char_ptr here, which is already the
3817 * automatic converted type. revert_automatic_type_conversion
3818 * will construct the array type */
3819 cnst->base.type = type_char_ptr;
3820 cnst->string.value = res;
3824 wres = concat_string_wide_string(&res, &token.v.wide_string);
3826 wres = token.v.wide_string;
3831 switch (token.type) {
3832 case T_WIDE_STRING_LITERAL:
3833 wres = concat_wide_strings(&wres, &token.v.wide_string);
3836 case T_STRING_LITERAL:
3837 wres = concat_wide_string_string(&wres, &token.v.string);
3841 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3842 cnst->base.type = type_wchar_t_ptr;
3843 cnst->wide_string.value = wres;
3852 * Parse an integer constant.
3854 static expression_t *parse_int_const(void)
3856 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3857 cnst->base.source_position = HERE;
3858 cnst->base.type = token.datatype;
3859 cnst->conste.v.int_value = token.v.intvalue;
3867 * Parse a character constant.
3869 static expression_t *parse_character_constant(void)
3871 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
3873 cnst->base.source_position = HERE;
3874 cnst->base.type = token.datatype;
3875 cnst->conste.v.character = token.v.string;
3877 if (cnst->conste.v.character.size != 1) {
3878 if (warning.multichar && (c_mode & _GNUC)) {
3880 warningf(HERE, "multi-character character constant");
3882 errorf(HERE, "more than 1 characters in character constant");
3891 * Parse a wide character constant.
3893 static expression_t *parse_wide_character_constant(void)
3895 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
3897 cnst->base.source_position = HERE;
3898 cnst->base.type = token.datatype;
3899 cnst->conste.v.wide_character = token.v.wide_string;
3901 if (cnst->conste.v.wide_character.size != 1) {
3902 if (warning.multichar && (c_mode & _GNUC)) {
3904 warningf(HERE, "multi-character character constant");
3906 errorf(HERE, "more than 1 characters in character constant");
3915 * Parse a float constant.
3917 static expression_t *parse_float_const(void)
3919 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3920 cnst->base.type = token.datatype;
3921 cnst->conste.v.float_value = token.v.floatvalue;
3928 static declaration_t *create_implicit_function(symbol_t *symbol,
3929 const source_position_t source_position)
3931 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3932 ntype->function.return_type = type_int;
3933 ntype->function.unspecified_parameters = true;
3935 type_t *type = typehash_insert(ntype);
3940 declaration_t *const declaration = allocate_declaration_zero();
3941 declaration->storage_class = STORAGE_CLASS_EXTERN;
3942 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
3943 declaration->type = type;
3944 declaration->symbol = symbol;
3945 declaration->source_position = source_position;
3946 declaration->parent_scope = global_scope;
3948 scope_t *old_scope = scope;
3949 set_scope(global_scope);
3951 environment_push(declaration);
3952 /* prepends the declaration to the global declarations list */
3953 declaration->next = scope->declarations;
3954 scope->declarations = declaration;
3956 assert(scope == global_scope);
3957 set_scope(old_scope);
3963 * Creates a return_type (func)(argument_type) function type if not
3966 * @param return_type the return type
3967 * @param argument_type the argument type
3969 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3971 function_parameter_t *parameter
3972 = obstack_alloc(type_obst, sizeof(parameter[0]));
3973 memset(parameter, 0, sizeof(parameter[0]));
3974 parameter->type = argument_type;
3976 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3977 type->function.return_type = return_type;
3978 type->function.parameters = parameter;
3980 type_t *result = typehash_insert(type);
3981 if(result != type) {
3989 * Creates a function type for some function like builtins.
3991 * @param symbol the symbol describing the builtin
3993 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3995 switch(symbol->ID) {
3996 case T___builtin_alloca:
3997 return make_function_1_type(type_void_ptr, type_size_t);
3998 case T___builtin_nan:
3999 return make_function_1_type(type_double, type_char_ptr);
4000 case T___builtin_nanf:
4001 return make_function_1_type(type_float, type_char_ptr);
4002 case T___builtin_nand:
4003 return make_function_1_type(type_long_double, type_char_ptr);
4004 case T___builtin_va_end:
4005 return make_function_1_type(type_void, type_valist);
4007 panic("not implemented builtin symbol found");
4012 * Performs automatic type cast as described in § 6.3.2.1.
4014 * @param orig_type the original type
4016 static type_t *automatic_type_conversion(type_t *orig_type)
4018 type_t *type = skip_typeref(orig_type);
4019 if(is_type_array(type)) {
4020 array_type_t *array_type = &type->array;
4021 type_t *element_type = array_type->element_type;
4022 unsigned qualifiers = array_type->type.qualifiers;
4024 return make_pointer_type(element_type, qualifiers);
4027 if(is_type_function(type)) {
4028 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4035 * reverts the automatic casts of array to pointer types and function
4036 * to function-pointer types as defined § 6.3.2.1
4038 type_t *revert_automatic_type_conversion(const expression_t *expression)
4040 switch (expression->kind) {
4041 case EXPR_REFERENCE: return expression->reference.declaration->type;
4042 case EXPR_SELECT: return expression->select.compound_entry->type;
4044 case EXPR_UNARY_DEREFERENCE: {
4045 const expression_t *const value = expression->unary.value;
4046 type_t *const type = skip_typeref(value->base.type);
4047 assert(is_type_pointer(type));
4048 return type->pointer.points_to;
4051 case EXPR_BUILTIN_SYMBOL:
4052 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4054 case EXPR_ARRAY_ACCESS: {
4055 const expression_t *array_ref = expression->array_access.array_ref;
4056 type_t *type_left = skip_typeref(array_ref->base.type);
4057 if (!is_type_valid(type_left))
4059 assert(is_type_pointer(type_left));
4060 return type_left->pointer.points_to;
4063 case EXPR_STRING_LITERAL: {
4064 size_t size = expression->string.value.size;
4065 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4068 case EXPR_WIDE_STRING_LITERAL: {
4069 size_t size = expression->wide_string.value.size;
4070 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4073 case EXPR_COMPOUND_LITERAL:
4074 return expression->compound_literal.type;
4079 return expression->base.type;
4082 static expression_t *parse_reference(void)
4084 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4086 reference_expression_t *ref = &expression->reference;
4087 ref->symbol = token.v.symbol;
4089 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4091 source_position_t source_position = token.source_position;
4094 if(declaration == NULL) {
4095 if (! strict_mode && token.type == '(') {
4096 /* an implicitly defined function */
4097 if (warning.implicit_function_declaration) {
4098 warningf(HERE, "implicit declaration of function '%Y'",
4102 declaration = create_implicit_function(ref->symbol,
4105 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4106 return create_invalid_expression();
4110 type_t *type = declaration->type;
4112 /* we always do the auto-type conversions; the & and sizeof parser contains
4113 * code to revert this! */
4114 type = automatic_type_conversion(type);
4116 ref->declaration = declaration;
4117 ref->base.type = type;
4119 /* this declaration is used */
4120 declaration->used = true;
4125 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4129 /* TODO check if explicit cast is allowed and issue warnings/errors */
4132 static expression_t *parse_compound_literal(type_t *type)
4134 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4136 parse_initializer_env_t env;
4138 env.declaration = NULL;
4139 env.must_be_constant = false;
4140 initializer_t *initializer = parse_initializer(&env);
4143 expression->compound_literal.initializer = initializer;
4144 expression->compound_literal.type = type;
4145 expression->base.type = automatic_type_conversion(type);
4151 * Parse a cast expression.
4153 static expression_t *parse_cast(void)
4155 source_position_t source_position = token.source_position;
4157 type_t *type = parse_typename();
4161 if(token.type == '{') {
4162 return parse_compound_literal(type);
4165 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4166 cast->base.source_position = source_position;
4168 expression_t *value = parse_sub_expression(20);
4170 check_cast_allowed(value, type);
4172 cast->base.type = type;
4173 cast->unary.value = value;
4177 return create_invalid_expression();
4181 * Parse a statement expression.
4183 static expression_t *parse_statement_expression(void)
4185 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4187 statement_t *statement = parse_compound_statement();
4188 expression->statement.statement = statement;
4189 expression->base.source_position = statement->base.source_position;
4191 /* find last statement and use its type */
4192 type_t *type = type_void;
4193 const statement_t *stmt = statement->compound.statements;
4195 while (stmt->base.next != NULL)
4196 stmt = stmt->base.next;
4198 if (stmt->kind == STATEMENT_EXPRESSION) {
4199 type = stmt->expression.expression->base.type;
4202 warningf(expression->base.source_position, "empty statement expression ({})");
4204 expression->base.type = type;
4210 return create_invalid_expression();
4214 * Parse a braced expression.
4216 static expression_t *parse_brace_expression(void)
4220 switch(token.type) {
4222 /* gcc extension: a statement expression */
4223 return parse_statement_expression();
4227 return parse_cast();
4229 if(is_typedef_symbol(token.v.symbol)) {
4230 return parse_cast();
4234 expression_t *result = parse_expression();
4239 return create_invalid_expression();
4242 static expression_t *parse_function_keyword(void)
4247 if (current_function == NULL) {
4248 errorf(HERE, "'__func__' used outside of a function");
4251 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
4252 expression->base.type = type_char_ptr;
4257 static expression_t *parse_pretty_function_keyword(void)
4259 eat(T___PRETTY_FUNCTION__);
4262 if (current_function == NULL) {
4263 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
4266 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
4267 expression->base.type = type_char_ptr;
4272 static designator_t *parse_designator(void)
4274 designator_t *result = allocate_ast_zero(sizeof(result[0]));
4275 result->source_position = HERE;
4277 if(token.type != T_IDENTIFIER) {
4278 parse_error_expected("while parsing member designator",
4283 result->symbol = token.v.symbol;
4286 designator_t *last_designator = result;
4288 if(token.type == '.') {
4290 if(token.type != T_IDENTIFIER) {
4291 parse_error_expected("while parsing member designator",
4296 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4297 designator->source_position = HERE;
4298 designator->symbol = token.v.symbol;
4301 last_designator->next = designator;
4302 last_designator = designator;
4305 if(token.type == '[') {
4307 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4308 designator->source_position = HERE;
4309 designator->array_index = parse_expression();
4310 if(designator->array_index == NULL) {
4316 last_designator->next = designator;
4317 last_designator = designator;
4329 * Parse the __builtin_offsetof() expression.
4331 static expression_t *parse_offsetof(void)
4333 eat(T___builtin_offsetof);
4335 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4336 expression->base.type = type_size_t;
4339 type_t *type = parse_typename();
4341 designator_t *designator = parse_designator();
4344 expression->offsetofe.type = type;
4345 expression->offsetofe.designator = designator;
4348 memset(&path, 0, sizeof(path));
4349 path.top_type = type;
4350 path.path = NEW_ARR_F(type_path_entry_t, 0);
4352 descend_into_subtype(&path);
4354 if(!walk_designator(&path, designator, true)) {
4355 return create_invalid_expression();
4358 DEL_ARR_F(path.path);
4362 return create_invalid_expression();
4366 * Parses a _builtin_va_start() expression.
4368 static expression_t *parse_va_start(void)
4370 eat(T___builtin_va_start);
4372 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4375 expression->va_starte.ap = parse_assignment_expression();
4377 expression_t *const expr = parse_assignment_expression();
4378 if (expr->kind == EXPR_REFERENCE) {
4379 declaration_t *const decl = expr->reference.declaration;
4381 return create_invalid_expression();
4382 if (decl->parent_scope == ¤t_function->scope &&
4383 decl->next == NULL) {
4384 expression->va_starte.parameter = decl;
4389 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4391 return create_invalid_expression();
4395 * Parses a _builtin_va_arg() expression.
4397 static expression_t *parse_va_arg(void)
4399 eat(T___builtin_va_arg);
4401 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4404 expression->va_arge.ap = parse_assignment_expression();
4406 expression->base.type = parse_typename();
4411 return create_invalid_expression();
4414 static expression_t *parse_builtin_symbol(void)
4416 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4418 symbol_t *symbol = token.v.symbol;
4420 expression->builtin_symbol.symbol = symbol;
4423 type_t *type = get_builtin_symbol_type(symbol);
4424 type = automatic_type_conversion(type);
4426 expression->base.type = type;
4431 * Parses a __builtin_constant() expression.
4433 static expression_t *parse_builtin_constant(void)
4435 eat(T___builtin_constant_p);
4437 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4440 expression->builtin_constant.value = parse_assignment_expression();
4442 expression->base.type = type_int;
4446 return create_invalid_expression();
4450 * Parses a __builtin_prefetch() expression.
4452 static expression_t *parse_builtin_prefetch(void)
4454 eat(T___builtin_prefetch);
4456 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4459 expression->builtin_prefetch.adr = parse_assignment_expression();
4460 if (token.type == ',') {
4462 expression->builtin_prefetch.rw = parse_assignment_expression();
4464 if (token.type == ',') {
4466 expression->builtin_prefetch.locality = parse_assignment_expression();
4469 expression->base.type = type_void;
4473 return create_invalid_expression();
4477 * Parses a __builtin_is_*() compare expression.
4479 static expression_t *parse_compare_builtin(void)
4481 expression_t *expression;
4483 switch(token.type) {
4484 case T___builtin_isgreater:
4485 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4487 case T___builtin_isgreaterequal:
4488 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4490 case T___builtin_isless:
4491 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4493 case T___builtin_islessequal:
4494 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4496 case T___builtin_islessgreater:
4497 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4499 case T___builtin_isunordered:
4500 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4503 panic("invalid compare builtin found");
4506 expression->base.source_position = HERE;
4510 expression->binary.left = parse_assignment_expression();
4512 expression->binary.right = parse_assignment_expression();
4515 type_t *const orig_type_left = expression->binary.left->base.type;
4516 type_t *const orig_type_right = expression->binary.right->base.type;
4518 type_t *const type_left = skip_typeref(orig_type_left);
4519 type_t *const type_right = skip_typeref(orig_type_right);
4520 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4521 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4522 type_error_incompatible("invalid operands in comparison",
4523 expression->base.source_position, orig_type_left, orig_type_right);
4526 semantic_comparison(&expression->binary);
4531 return create_invalid_expression();
4535 * Parses a __builtin_expect() expression.
4537 static expression_t *parse_builtin_expect(void)
4539 eat(T___builtin_expect);
4541 expression_t *expression
4542 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4545 expression->binary.left = parse_assignment_expression();
4547 expression->binary.right = parse_constant_expression();
4550 expression->base.type = expression->binary.left->base.type;
4554 return create_invalid_expression();
4558 * Parses a MS assume() expression.
4560 static expression_t *parse_assume(void) {
4563 expression_t *expression
4564 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4567 expression->unary.value = parse_assignment_expression();
4570 expression->base.type = type_void;
4573 return create_invalid_expression();
4577 * Parses a primary expression.
4579 static expression_t *parse_primary_expression(void)
4581 switch (token.type) {
4582 case T_INTEGER: return parse_int_const();
4583 case T_CHARACTER_CONSTANT: return parse_character_constant();
4584 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4585 case T_FLOATINGPOINT: return parse_float_const();
4586 case T_STRING_LITERAL:
4587 case T_WIDE_STRING_LITERAL: return parse_string_const();
4588 case T_IDENTIFIER: return parse_reference();
4589 case T___FUNCTION__:
4590 case T___func__: return parse_function_keyword();
4591 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4592 case T___builtin_offsetof: return parse_offsetof();
4593 case T___builtin_va_start: return parse_va_start();
4594 case T___builtin_va_arg: return parse_va_arg();
4595 case T___builtin_expect: return parse_builtin_expect();
4596 case T___builtin_alloca:
4597 case T___builtin_nan:
4598 case T___builtin_nand:
4599 case T___builtin_nanf:
4600 case T___builtin_va_end: return parse_builtin_symbol();
4601 case T___builtin_isgreater:
4602 case T___builtin_isgreaterequal:
4603 case T___builtin_isless:
4604 case T___builtin_islessequal:
4605 case T___builtin_islessgreater:
4606 case T___builtin_isunordered: return parse_compare_builtin();
4607 case T___builtin_constant_p: return parse_builtin_constant();
4608 case T___builtin_prefetch: return parse_builtin_prefetch();
4609 case T_assume: return parse_assume();
4611 case '(': return parse_brace_expression();
4614 errorf(HERE, "unexpected token %K, expected an expression", &token);
4617 return create_invalid_expression();
4621 * Check if the expression has the character type and issue a warning then.
4623 static void check_for_char_index_type(const expression_t *expression) {
4624 type_t *const type = expression->base.type;
4625 const type_t *const base_type = skip_typeref(type);
4627 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4628 warning.char_subscripts) {
4629 warningf(expression->base.source_position,
4630 "array subscript has type '%T'", type);
4634 static expression_t *parse_array_expression(unsigned precedence,
4641 expression_t *inside = parse_expression();
4643 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4645 array_access_expression_t *array_access = &expression->array_access;
4647 type_t *const orig_type_left = left->base.type;
4648 type_t *const orig_type_inside = inside->base.type;
4650 type_t *const type_left = skip_typeref(orig_type_left);
4651 type_t *const type_inside = skip_typeref(orig_type_inside);
4653 type_t *return_type;
4654 if (is_type_pointer(type_left)) {
4655 return_type = type_left->pointer.points_to;
4656 array_access->array_ref = left;
4657 array_access->index = inside;
4658 check_for_char_index_type(inside);
4659 } else if (is_type_pointer(type_inside)) {
4660 return_type = type_inside->pointer.points_to;
4661 array_access->array_ref = inside;
4662 array_access->index = left;
4663 array_access->flipped = true;
4664 check_for_char_index_type(left);
4666 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4668 "array access on object with non-pointer types '%T', '%T'",
4669 orig_type_left, orig_type_inside);
4671 return_type = type_error_type;
4672 array_access->array_ref = create_invalid_expression();
4675 if(token.type != ']') {
4676 parse_error_expected("Problem while parsing array access", ']', 0);
4681 return_type = automatic_type_conversion(return_type);
4682 expression->base.type = return_type;
4687 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4689 expression_t *tp_expression = allocate_expression_zero(kind);
4690 tp_expression->base.type = type_size_t;
4692 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4694 tp_expression->typeprop.type = parse_typename();
4697 expression_t *expression = parse_sub_expression(precedence);
4698 expression->base.type = revert_automatic_type_conversion(expression);
4700 tp_expression->typeprop.type = expression->base.type;
4701 tp_expression->typeprop.tp_expression = expression;
4704 return tp_expression;
4706 return create_invalid_expression();
4709 static expression_t *parse_sizeof(unsigned precedence)
4712 return parse_typeprop(EXPR_SIZEOF, precedence);
4715 static expression_t *parse_alignof(unsigned precedence)
4718 return parse_typeprop(EXPR_SIZEOF, precedence);
4721 static expression_t *parse_select_expression(unsigned precedence,
4722 expression_t *compound)
4725 assert(token.type == '.' || token.type == T_MINUSGREATER);
4727 bool is_pointer = (token.type == T_MINUSGREATER);
4730 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4731 select->select.compound = compound;
4733 if(token.type != T_IDENTIFIER) {
4734 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4737 symbol_t *symbol = token.v.symbol;
4738 select->select.symbol = symbol;
4741 type_t *const orig_type = compound->base.type;
4742 type_t *const type = skip_typeref(orig_type);
4744 type_t *type_left = type;
4746 if (!is_type_pointer(type)) {
4747 if (is_type_valid(type)) {
4748 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4750 return create_invalid_expression();
4752 type_left = type->pointer.points_to;
4754 type_left = skip_typeref(type_left);
4756 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4757 type_left->kind != TYPE_COMPOUND_UNION) {
4758 if (is_type_valid(type_left)) {
4759 errorf(HERE, "request for member '%Y' in something not a struct or "
4760 "union, but '%T'", symbol, type_left);
4762 return create_invalid_expression();
4765 declaration_t *const declaration = type_left->compound.declaration;
4767 if(!declaration->init.is_defined) {
4768 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4770 return create_invalid_expression();
4773 declaration_t *iter = find_compound_entry(declaration, symbol);
4775 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4776 return create_invalid_expression();
4779 /* we always do the auto-type conversions; the & and sizeof parser contains
4780 * code to revert this! */
4781 type_t *expression_type = automatic_type_conversion(iter->type);
4783 select->select.compound_entry = iter;
4784 select->base.type = expression_type;
4786 if(expression_type->kind == TYPE_BITFIELD) {
4787 expression_t *extract
4788 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4789 extract->unary.value = select;
4790 extract->base.type = expression_type->bitfield.base;
4799 * Parse a call expression, ie. expression '( ... )'.
4801 * @param expression the function address
4803 static expression_t *parse_call_expression(unsigned precedence,
4804 expression_t *expression)
4807 expression_t *result = allocate_expression_zero(EXPR_CALL);
4809 call_expression_t *call = &result->call;
4810 call->function = expression;
4812 type_t *const orig_type = expression->base.type;
4813 type_t *const type = skip_typeref(orig_type);
4815 function_type_t *function_type = NULL;
4816 if (is_type_pointer(type)) {
4817 type_t *const to_type = skip_typeref(type->pointer.points_to);
4819 if (is_type_function(to_type)) {
4820 function_type = &to_type->function;
4821 call->base.type = function_type->return_type;
4825 if (function_type == NULL && is_type_valid(type)) {
4826 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4829 /* parse arguments */
4832 if(token.type != ')') {
4833 call_argument_t *last_argument = NULL;
4836 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4838 argument->expression = parse_assignment_expression();
4839 if(last_argument == NULL) {
4840 call->arguments = argument;
4842 last_argument->next = argument;
4844 last_argument = argument;
4846 if(token.type != ',')
4853 if(function_type != NULL) {
4854 function_parameter_t *parameter = function_type->parameters;
4855 call_argument_t *argument = call->arguments;
4856 for( ; parameter != NULL && argument != NULL;
4857 parameter = parameter->next, argument = argument->next) {
4858 type_t *expected_type = parameter->type;
4859 /* TODO report scope in error messages */
4860 expression_t *const arg_expr = argument->expression;
4861 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4862 if (res_type == NULL) {
4863 /* TODO improve error message */
4864 errorf(arg_expr->base.source_position,
4865 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4866 arg_expr, arg_expr->base.type, expected_type);
4868 argument->expression = create_implicit_cast(argument->expression, expected_type);
4871 /* too few parameters */
4872 if(parameter != NULL) {
4873 errorf(HERE, "too few arguments to function '%E'", expression);
4874 } else if(argument != NULL) {
4875 /* too many parameters */
4876 if(!function_type->variadic
4877 && !function_type->unspecified_parameters) {
4878 errorf(HERE, "too many arguments to function '%E'", expression);
4880 /* do default promotion */
4881 for( ; argument != NULL; argument = argument->next) {
4882 type_t *type = argument->expression->base.type;
4884 type = skip_typeref(type);
4885 if(is_type_integer(type)) {
4886 type = promote_integer(type);
4887 } else if(type == type_float) {
4891 argument->expression
4892 = create_implicit_cast(argument->expression, type);
4895 check_format(&result->call);
4898 check_format(&result->call);
4904 return create_invalid_expression();
4907 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4909 static bool same_compound_type(const type_t *type1, const type_t *type2)
4912 is_type_compound(type1) &&
4913 type1->kind == type2->kind &&
4914 type1->compound.declaration == type2->compound.declaration;
4918 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4920 * @param expression the conditional expression
4922 static expression_t *parse_conditional_expression(unsigned precedence,
4923 expression_t *expression)
4927 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4929 conditional_expression_t *conditional = &result->conditional;
4930 conditional->condition = expression;
4933 type_t *const condition_type_orig = expression->base.type;
4934 type_t *const condition_type = skip_typeref(condition_type_orig);
4935 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4936 type_error("expected a scalar type in conditional condition",
4937 expression->base.source_position, condition_type_orig);
4940 expression_t *true_expression = parse_expression();
4942 expression_t *false_expression = parse_sub_expression(precedence);
4944 type_t *const orig_true_type = true_expression->base.type;
4945 type_t *const orig_false_type = false_expression->base.type;
4946 type_t *const true_type = skip_typeref(orig_true_type);
4947 type_t *const false_type = skip_typeref(orig_false_type);
4950 type_t *result_type;
4951 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4952 result_type = semantic_arithmetic(true_type, false_type);
4954 true_expression = create_implicit_cast(true_expression, result_type);
4955 false_expression = create_implicit_cast(false_expression, result_type);
4957 conditional->true_expression = true_expression;
4958 conditional->false_expression = false_expression;
4959 conditional->base.type = result_type;
4960 } else if (same_compound_type(true_type, false_type) || (
4961 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4962 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4964 /* just take 1 of the 2 types */
4965 result_type = true_type;
4966 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4967 && pointers_compatible(true_type, false_type)) {
4969 result_type = true_type;
4970 } else if (is_type_pointer(true_type)
4971 && is_null_pointer_constant(false_expression)) {
4972 result_type = true_type;
4973 } else if (is_type_pointer(false_type)
4974 && is_null_pointer_constant(true_expression)) {
4975 result_type = false_type;
4977 /* TODO: one pointer to void*, other some pointer */
4979 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4980 type_error_incompatible("while parsing conditional",
4981 expression->base.source_position, true_type,
4984 result_type = type_error_type;
4987 conditional->true_expression
4988 = create_implicit_cast(true_expression, result_type);
4989 conditional->false_expression
4990 = create_implicit_cast(false_expression, result_type);
4991 conditional->base.type = result_type;
4994 return create_invalid_expression();
4998 * Parse an extension expression.
5000 static expression_t *parse_extension(unsigned precedence)
5002 eat(T___extension__);
5004 /* TODO enable extensions */
5005 expression_t *expression = parse_sub_expression(precedence);
5006 /* TODO disable extensions */
5011 * Parse a __builtin_classify_type() expression.
5013 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5015 eat(T___builtin_classify_type);
5017 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5018 result->base.type = type_int;
5021 expression_t *expression = parse_sub_expression(precedence);
5023 result->classify_type.type_expression = expression;
5027 return create_invalid_expression();
5030 static void semantic_incdec(unary_expression_t *expression)
5032 type_t *const orig_type = expression->value->base.type;
5033 type_t *const type = skip_typeref(orig_type);
5034 /* TODO !is_type_real && !is_type_pointer */
5035 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5036 if (is_type_valid(type)) {
5037 /* TODO: improve error message */
5038 errorf(HERE, "operation needs an arithmetic or pointer type");
5043 expression->base.type = orig_type;
5046 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5048 type_t *const orig_type = expression->value->base.type;
5049 type_t *const type = skip_typeref(orig_type);
5050 if(!is_type_arithmetic(type)) {
5051 if (is_type_valid(type)) {
5052 /* TODO: improve error message */
5053 errorf(HERE, "operation needs an arithmetic type");
5058 expression->base.type = orig_type;
5061 static void semantic_unexpr_scalar(unary_expression_t *expression)
5063 type_t *const orig_type = expression->value->base.type;
5064 type_t *const type = skip_typeref(orig_type);
5065 if (!is_type_scalar(type)) {
5066 if (is_type_valid(type)) {
5067 errorf(HERE, "operand of ! must be of scalar type");
5072 expression->base.type = orig_type;
5075 static void semantic_unexpr_integer(unary_expression_t *expression)
5077 type_t *const orig_type = expression->value->base.type;
5078 type_t *const type = skip_typeref(orig_type);
5079 if (!is_type_integer(type)) {
5080 if (is_type_valid(type)) {
5081 errorf(HERE, "operand of ~ must be of integer type");
5086 expression->base.type = orig_type;
5089 static void semantic_dereference(unary_expression_t *expression)
5091 type_t *const orig_type = expression->value->base.type;
5092 type_t *const type = skip_typeref(orig_type);
5093 if(!is_type_pointer(type)) {
5094 if (is_type_valid(type)) {
5095 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
5100 type_t *result_type = type->pointer.points_to;
5101 result_type = automatic_type_conversion(result_type);
5102 expression->base.type = result_type;
5106 * Check the semantic of the address taken expression.
5108 static void semantic_take_addr(unary_expression_t *expression)
5110 expression_t *value = expression->value;
5111 value->base.type = revert_automatic_type_conversion(value);
5113 type_t *orig_type = value->base.type;
5114 if(!is_type_valid(orig_type))
5117 if(value->kind == EXPR_REFERENCE) {
5118 declaration_t *const declaration = value->reference.declaration;
5119 if(declaration != NULL) {
5120 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
5121 errorf(expression->base.source_position,
5122 "address of register variable '%Y' requested",
5123 declaration->symbol);
5125 declaration->address_taken = 1;
5129 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5132 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
5133 static expression_t *parse_##unexpression_type(unsigned precedence) \
5137 expression_t *unary_expression \
5138 = allocate_expression_zero(unexpression_type); \
5139 unary_expression->base.source_position = HERE; \
5140 unary_expression->unary.value = parse_sub_expression(precedence); \
5142 sfunc(&unary_expression->unary); \
5144 return unary_expression; \
5147 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
5148 semantic_unexpr_arithmetic)
5149 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
5150 semantic_unexpr_arithmetic)
5151 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
5152 semantic_unexpr_scalar)
5153 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
5154 semantic_dereference)
5155 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
5157 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
5158 semantic_unexpr_integer)
5159 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
5161 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
5164 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
5166 static expression_t *parse_##unexpression_type(unsigned precedence, \
5167 expression_t *left) \
5169 (void) precedence; \
5172 expression_t *unary_expression \
5173 = allocate_expression_zero(unexpression_type); \
5174 unary_expression->unary.value = left; \
5176 sfunc(&unary_expression->unary); \
5178 return unary_expression; \
5181 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
5182 EXPR_UNARY_POSTFIX_INCREMENT,
5184 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
5185 EXPR_UNARY_POSTFIX_DECREMENT,
5188 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
5190 /* TODO: handle complex + imaginary types */
5192 /* § 6.3.1.8 Usual arithmetic conversions */
5193 if(type_left == type_long_double || type_right == type_long_double) {
5194 return type_long_double;
5195 } else if(type_left == type_double || type_right == type_double) {
5197 } else if(type_left == type_float || type_right == type_float) {
5201 type_right = promote_integer(type_right);
5202 type_left = promote_integer(type_left);
5204 if(type_left == type_right)
5207 bool signed_left = is_type_signed(type_left);
5208 bool signed_right = is_type_signed(type_right);
5209 int rank_left = get_rank(type_left);
5210 int rank_right = get_rank(type_right);
5211 if(rank_left < rank_right) {
5212 if(signed_left == signed_right || !signed_right) {
5218 if(signed_left == signed_right || !signed_left) {
5227 * Check the semantic restrictions for a binary expression.
5229 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
5231 expression_t *const left = expression->left;
5232 expression_t *const right = expression->right;
5233 type_t *const orig_type_left = left->base.type;
5234 type_t *const orig_type_right = right->base.type;
5235 type_t *const type_left = skip_typeref(orig_type_left);
5236 type_t *const type_right = skip_typeref(orig_type_right);
5238 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5239 /* TODO: improve error message */
5240 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5241 errorf(HERE, "operation needs arithmetic types");
5246 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5247 expression->left = create_implicit_cast(left, arithmetic_type);
5248 expression->right = create_implicit_cast(right, arithmetic_type);
5249 expression->base.type = arithmetic_type;
5252 static void semantic_shift_op(binary_expression_t *expression)
5254 expression_t *const left = expression->left;
5255 expression_t *const right = expression->right;
5256 type_t *const orig_type_left = left->base.type;
5257 type_t *const orig_type_right = right->base.type;
5258 type_t * type_left = skip_typeref(orig_type_left);
5259 type_t * type_right = skip_typeref(orig_type_right);
5261 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
5262 /* TODO: improve error message */
5263 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5264 errorf(HERE, "operation needs integer types");
5269 type_left = promote_integer(type_left);
5270 type_right = promote_integer(type_right);
5272 expression->left = create_implicit_cast(left, type_left);
5273 expression->right = create_implicit_cast(right, type_right);
5274 expression->base.type = type_left;
5277 static void semantic_add(binary_expression_t *expression)
5279 expression_t *const left = expression->left;
5280 expression_t *const right = expression->right;
5281 type_t *const orig_type_left = left->base.type;
5282 type_t *const orig_type_right = right->base.type;
5283 type_t *const type_left = skip_typeref(orig_type_left);
5284 type_t *const type_right = skip_typeref(orig_type_right);
5287 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5288 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5289 expression->left = create_implicit_cast(left, arithmetic_type);
5290 expression->right = create_implicit_cast(right, arithmetic_type);
5291 expression->base.type = arithmetic_type;
5293 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5294 expression->base.type = type_left;
5295 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
5296 expression->base.type = type_right;
5297 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5298 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
5302 static void semantic_sub(binary_expression_t *expression)
5304 expression_t *const left = expression->left;
5305 expression_t *const right = expression->right;
5306 type_t *const orig_type_left = left->base.type;
5307 type_t *const orig_type_right = right->base.type;
5308 type_t *const type_left = skip_typeref(orig_type_left);
5309 type_t *const type_right = skip_typeref(orig_type_right);
5312 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5313 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5314 expression->left = create_implicit_cast(left, arithmetic_type);
5315 expression->right = create_implicit_cast(right, arithmetic_type);
5316 expression->base.type = arithmetic_type;
5318 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5319 expression->base.type = type_left;
5320 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5321 if(!pointers_compatible(type_left, type_right)) {
5323 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5324 orig_type_left, orig_type_right);
5326 expression->base.type = type_ptrdiff_t;
5328 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5329 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5330 orig_type_left, orig_type_right);
5335 * Check the semantics of comparison expressions.
5337 * @param expression The expression to check.
5339 static void semantic_comparison(binary_expression_t *expression)
5341 expression_t *left = expression->left;
5342 expression_t *right = expression->right;
5343 type_t *orig_type_left = left->base.type;
5344 type_t *orig_type_right = right->base.type;
5346 type_t *type_left = skip_typeref(orig_type_left);
5347 type_t *type_right = skip_typeref(orig_type_right);
5349 /* TODO non-arithmetic types */
5350 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5351 if (warning.sign_compare &&
5352 (expression->base.kind != EXPR_BINARY_EQUAL &&
5353 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5354 (is_type_signed(type_left) != is_type_signed(type_right))) {
5355 warningf(expression->base.source_position,
5356 "comparison between signed and unsigned");
5358 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5359 expression->left = create_implicit_cast(left, arithmetic_type);
5360 expression->right = create_implicit_cast(right, arithmetic_type);
5361 expression->base.type = arithmetic_type;
5362 if (warning.float_equal &&
5363 (expression->base.kind == EXPR_BINARY_EQUAL ||
5364 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5365 is_type_float(arithmetic_type)) {
5366 warningf(expression->base.source_position,
5367 "comparing floating point with == or != is unsafe");
5369 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5370 /* TODO check compatibility */
5371 } else if (is_type_pointer(type_left)) {
5372 expression->right = create_implicit_cast(right, type_left);
5373 } else if (is_type_pointer(type_right)) {
5374 expression->left = create_implicit_cast(left, type_right);
5375 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5376 type_error_incompatible("invalid operands in comparison",
5377 expression->base.source_position,
5378 type_left, type_right);
5380 expression->base.type = type_int;
5383 static void semantic_arithmetic_assign(binary_expression_t *expression)
5385 expression_t *left = expression->left;
5386 expression_t *right = expression->right;
5387 type_t *orig_type_left = left->base.type;
5388 type_t *orig_type_right = right->base.type;
5390 type_t *type_left = skip_typeref(orig_type_left);
5391 type_t *type_right = skip_typeref(orig_type_right);
5393 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5394 /* TODO: improve error message */
5395 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5396 errorf(HERE, "operation needs arithmetic types");
5401 /* combined instructions are tricky. We can't create an implicit cast on
5402 * the left side, because we need the uncasted form for the store.
5403 * The ast2firm pass has to know that left_type must be right_type
5404 * for the arithmetic operation and create a cast by itself */
5405 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5406 expression->right = create_implicit_cast(right, arithmetic_type);
5407 expression->base.type = type_left;
5410 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5412 expression_t *const left = expression->left;
5413 expression_t *const right = expression->right;
5414 type_t *const orig_type_left = left->base.type;
5415 type_t *const orig_type_right = right->base.type;
5416 type_t *const type_left = skip_typeref(orig_type_left);
5417 type_t *const type_right = skip_typeref(orig_type_right);
5419 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5420 /* combined instructions are tricky. We can't create an implicit cast on
5421 * the left side, because we need the uncasted form for the store.
5422 * The ast2firm pass has to know that left_type must be right_type
5423 * for the arithmetic operation and create a cast by itself */
5424 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5425 expression->right = create_implicit_cast(right, arithmetic_type);
5426 expression->base.type = type_left;
5427 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5428 expression->base.type = type_left;
5429 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5430 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5435 * Check the semantic restrictions of a logical expression.
5437 static void semantic_logical_op(binary_expression_t *expression)
5439 expression_t *const left = expression->left;
5440 expression_t *const right = expression->right;
5441 type_t *const orig_type_left = left->base.type;
5442 type_t *const orig_type_right = right->base.type;
5443 type_t *const type_left = skip_typeref(orig_type_left);
5444 type_t *const type_right = skip_typeref(orig_type_right);
5446 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5447 /* TODO: improve error message */
5448 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5449 errorf(HERE, "operation needs scalar types");
5454 expression->base.type = type_int;
5458 * Checks if a compound type has constant fields.
5460 static bool has_const_fields(const compound_type_t *type)
5462 const scope_t *scope = &type->declaration->scope;
5463 const declaration_t *declaration = scope->declarations;
5465 for (; declaration != NULL; declaration = declaration->next) {
5466 if (declaration->namespc != NAMESPACE_NORMAL)
5469 const type_t *decl_type = skip_typeref(declaration->type);
5470 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5478 * Check the semantic restrictions of a binary assign expression.
5480 static void semantic_binexpr_assign(binary_expression_t *expression)
5482 expression_t *left = expression->left;
5483 type_t *orig_type_left = left->base.type;
5485 type_t *type_left = revert_automatic_type_conversion(left);
5486 type_left = skip_typeref(orig_type_left);
5488 /* must be a modifiable lvalue */
5489 if (is_type_array(type_left)) {
5490 errorf(HERE, "cannot assign to arrays ('%E')", left);
5493 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5494 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5498 if(is_type_incomplete(type_left)) {
5500 "left-hand side of assignment '%E' has incomplete type '%T'",
5501 left, orig_type_left);
5504 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5505 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5506 left, orig_type_left);
5510 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5512 if (res_type == NULL) {
5513 errorf(expression->base.source_position,
5514 "cannot assign to '%T' from '%T'",
5515 orig_type_left, expression->right->base.type);
5517 expression->right = create_implicit_cast(expression->right, res_type);
5520 expression->base.type = orig_type_left;
5524 * Determine if the outermost operation (or parts thereof) of the given
5525 * expression has no effect in order to generate a warning about this fact.
5526 * Therefore in some cases this only examines some of the operands of the
5527 * expression (see comments in the function and examples below).
5529 * f() + 23; // warning, because + has no effect
5530 * x || f(); // no warning, because x controls execution of f()
5531 * x ? y : f(); // warning, because y has no effect
5532 * (void)x; // no warning to be able to suppress the warning
5533 * This function can NOT be used for an "expression has definitely no effect"-
5535 static bool expression_has_effect(const expression_t *const expr)
5537 switch (expr->kind) {
5538 case EXPR_UNKNOWN: break;
5539 case EXPR_INVALID: return true; /* do NOT warn */
5540 case EXPR_REFERENCE: return false;
5541 case EXPR_CONST: return false;
5542 case EXPR_CHARACTER_CONSTANT: return false;
5543 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5544 case EXPR_STRING_LITERAL: return false;
5545 case EXPR_WIDE_STRING_LITERAL: return false;
5548 const call_expression_t *const call = &expr->call;
5549 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5552 switch (call->function->builtin_symbol.symbol->ID) {
5553 case T___builtin_va_end: return true;
5554 default: return false;
5558 /* Generate the warning if either the left or right hand side of a
5559 * conditional expression has no effect */
5560 case EXPR_CONDITIONAL: {
5561 const conditional_expression_t *const cond = &expr->conditional;
5563 expression_has_effect(cond->true_expression) &&
5564 expression_has_effect(cond->false_expression);
5567 case EXPR_SELECT: return false;
5568 case EXPR_ARRAY_ACCESS: return false;
5569 case EXPR_SIZEOF: return false;
5570 case EXPR_CLASSIFY_TYPE: return false;
5571 case EXPR_ALIGNOF: return false;
5573 case EXPR_FUNCTION: return false;
5574 case EXPR_PRETTY_FUNCTION: return false;
5575 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5576 case EXPR_BUILTIN_CONSTANT_P: return false;
5577 case EXPR_BUILTIN_PREFETCH: return true;
5578 case EXPR_OFFSETOF: return false;
5579 case EXPR_VA_START: return true;
5580 case EXPR_VA_ARG: return true;
5581 case EXPR_STATEMENT: return true; // TODO
5582 case EXPR_COMPOUND_LITERAL: return false;
5584 case EXPR_UNARY_NEGATE: return false;
5585 case EXPR_UNARY_PLUS: return false;
5586 case EXPR_UNARY_BITWISE_NEGATE: return false;
5587 case EXPR_UNARY_NOT: return false;
5588 case EXPR_UNARY_DEREFERENCE: return false;
5589 case EXPR_UNARY_TAKE_ADDRESS: return false;
5590 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5591 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5592 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5593 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5595 /* Treat void casts as if they have an effect in order to being able to
5596 * suppress the warning */
5597 case EXPR_UNARY_CAST: {
5598 type_t *const type = skip_typeref(expr->base.type);
5599 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5602 case EXPR_UNARY_CAST_IMPLICIT: return true;
5603 case EXPR_UNARY_ASSUME: return true;
5604 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5606 case EXPR_BINARY_ADD: return false;
5607 case EXPR_BINARY_SUB: return false;
5608 case EXPR_BINARY_MUL: return false;
5609 case EXPR_BINARY_DIV: return false;
5610 case EXPR_BINARY_MOD: return false;
5611 case EXPR_BINARY_EQUAL: return false;
5612 case EXPR_BINARY_NOTEQUAL: return false;
5613 case EXPR_BINARY_LESS: return false;
5614 case EXPR_BINARY_LESSEQUAL: return false;
5615 case EXPR_BINARY_GREATER: return false;
5616 case EXPR_BINARY_GREATEREQUAL: return false;
5617 case EXPR_BINARY_BITWISE_AND: return false;
5618 case EXPR_BINARY_BITWISE_OR: return false;
5619 case EXPR_BINARY_BITWISE_XOR: return false;
5620 case EXPR_BINARY_SHIFTLEFT: return false;
5621 case EXPR_BINARY_SHIFTRIGHT: return false;
5622 case EXPR_BINARY_ASSIGN: return true;
5623 case EXPR_BINARY_MUL_ASSIGN: return true;
5624 case EXPR_BINARY_DIV_ASSIGN: return true;
5625 case EXPR_BINARY_MOD_ASSIGN: return true;
5626 case EXPR_BINARY_ADD_ASSIGN: return true;
5627 case EXPR_BINARY_SUB_ASSIGN: return true;
5628 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5629 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5630 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5631 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5632 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5634 /* Only examine the right hand side of && and ||, because the left hand
5635 * side already has the effect of controlling the execution of the right
5637 case EXPR_BINARY_LOGICAL_AND:
5638 case EXPR_BINARY_LOGICAL_OR:
5639 /* Only examine the right hand side of a comma expression, because the left
5640 * hand side has a separate warning */
5641 case EXPR_BINARY_COMMA:
5642 return expression_has_effect(expr->binary.right);
5644 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5645 case EXPR_BINARY_ISGREATER: return false;
5646 case EXPR_BINARY_ISGREATEREQUAL: return false;
5647 case EXPR_BINARY_ISLESS: return false;
5648 case EXPR_BINARY_ISLESSEQUAL: return false;
5649 case EXPR_BINARY_ISLESSGREATER: return false;
5650 case EXPR_BINARY_ISUNORDERED: return false;
5653 panic("unexpected expression");
5656 static void semantic_comma(binary_expression_t *expression)
5658 if (warning.unused_value) {
5659 const expression_t *const left = expression->left;
5660 if (!expression_has_effect(left)) {
5661 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5664 expression->base.type = expression->right->base.type;
5667 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5668 static expression_t *parse_##binexpression_type(unsigned precedence, \
5669 expression_t *left) \
5672 source_position_t pos = HERE; \
5674 expression_t *right = parse_sub_expression(precedence + lr); \
5676 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5677 binexpr->base.source_position = pos; \
5678 binexpr->binary.left = left; \
5679 binexpr->binary.right = right; \
5680 sfunc(&binexpr->binary); \
5685 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5686 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5687 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5688 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5689 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5690 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5691 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5692 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5693 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5695 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5696 semantic_comparison, 1)
5697 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5698 semantic_comparison, 1)
5699 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5700 semantic_comparison, 1)
5701 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5702 semantic_comparison, 1)
5704 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5705 semantic_binexpr_arithmetic, 1)
5706 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5707 semantic_binexpr_arithmetic, 1)
5708 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5709 semantic_binexpr_arithmetic, 1)
5710 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5711 semantic_logical_op, 1)
5712 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5713 semantic_logical_op, 1)
5714 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5715 semantic_shift_op, 1)
5716 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5717 semantic_shift_op, 1)
5718 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5719 semantic_arithmetic_addsubb_assign, 0)
5720 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5721 semantic_arithmetic_addsubb_assign, 0)
5722 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5723 semantic_arithmetic_assign, 0)
5724 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5725 semantic_arithmetic_assign, 0)
5726 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5727 semantic_arithmetic_assign, 0)
5728 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5729 semantic_arithmetic_assign, 0)
5730 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5731 semantic_arithmetic_assign, 0)
5732 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5733 semantic_arithmetic_assign, 0)
5734 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5735 semantic_arithmetic_assign, 0)
5736 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5737 semantic_arithmetic_assign, 0)
5739 static expression_t *parse_sub_expression(unsigned precedence)
5741 if(token.type < 0) {
5742 return expected_expression_error();
5745 expression_parser_function_t *parser
5746 = &expression_parsers[token.type];
5747 source_position_t source_position = token.source_position;
5750 if(parser->parser != NULL) {
5751 left = parser->parser(parser->precedence);
5753 left = parse_primary_expression();
5755 assert(left != NULL);
5756 left->base.source_position = source_position;
5759 if(token.type < 0) {
5760 return expected_expression_error();
5763 parser = &expression_parsers[token.type];
5764 if(parser->infix_parser == NULL)
5766 if(parser->infix_precedence < precedence)
5769 left = parser->infix_parser(parser->infix_precedence, left);
5771 assert(left != NULL);
5772 assert(left->kind != EXPR_UNKNOWN);
5773 left->base.source_position = source_position;
5780 * Parse an expression.
5782 static expression_t *parse_expression(void)
5784 return parse_sub_expression(1);
5788 * Register a parser for a prefix-like operator with given precedence.
5790 * @param parser the parser function
5791 * @param token_type the token type of the prefix token
5792 * @param precedence the precedence of the operator
5794 static void register_expression_parser(parse_expression_function parser,
5795 int token_type, unsigned precedence)
5797 expression_parser_function_t *entry = &expression_parsers[token_type];
5799 if(entry->parser != NULL) {
5800 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5801 panic("trying to register multiple expression parsers for a token");
5803 entry->parser = parser;
5804 entry->precedence = precedence;
5808 * Register a parser for an infix operator with given precedence.
5810 * @param parser the parser function
5811 * @param token_type the token type of the infix operator
5812 * @param precedence the precedence of the operator
5814 static void register_infix_parser(parse_expression_infix_function parser,
5815 int token_type, unsigned precedence)
5817 expression_parser_function_t *entry = &expression_parsers[token_type];
5819 if(entry->infix_parser != NULL) {
5820 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5821 panic("trying to register multiple infix expression parsers for a "
5824 entry->infix_parser = parser;
5825 entry->infix_precedence = precedence;
5829 * Initialize the expression parsers.
5831 static void init_expression_parsers(void)
5833 memset(&expression_parsers, 0, sizeof(expression_parsers));
5835 register_infix_parser(parse_array_expression, '[', 30);
5836 register_infix_parser(parse_call_expression, '(', 30);
5837 register_infix_parser(parse_select_expression, '.', 30);
5838 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5839 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5841 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5844 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5845 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5846 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5847 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5848 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5849 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5850 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5851 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5852 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5853 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5854 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5855 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5856 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5857 T_EXCLAMATIONMARKEQUAL, 13);
5858 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5859 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5860 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5861 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5862 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5863 register_infix_parser(parse_conditional_expression, '?', 7);
5864 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5865 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5866 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5867 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5868 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5869 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5870 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5871 T_LESSLESSEQUAL, 2);
5872 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5873 T_GREATERGREATEREQUAL, 2);
5874 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5876 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5878 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5881 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5883 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5884 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5885 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5886 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5887 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5888 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5889 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5891 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5893 register_expression_parser(parse_sizeof, T_sizeof, 25);
5894 register_expression_parser(parse_alignof, T___alignof__, 25);
5895 register_expression_parser(parse_extension, T___extension__, 25);
5896 register_expression_parser(parse_builtin_classify_type,
5897 T___builtin_classify_type, 25);
5901 * Parse a asm statement constraints specification.
5903 static asm_constraint_t *parse_asm_constraints(void)
5905 asm_constraint_t *result = NULL;
5906 asm_constraint_t *last = NULL;
5908 while(token.type == T_STRING_LITERAL || token.type == '[') {
5909 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5910 memset(constraint, 0, sizeof(constraint[0]));
5912 if(token.type == '[') {
5914 if(token.type != T_IDENTIFIER) {
5915 parse_error_expected("while parsing asm constraint",
5919 constraint->symbol = token.v.symbol;
5924 constraint->constraints = parse_string_literals();
5926 constraint->expression = parse_expression();
5930 last->next = constraint;
5932 result = constraint;
5936 if(token.type != ',')
5947 * Parse a asm statement clobber specification.
5949 static asm_clobber_t *parse_asm_clobbers(void)
5951 asm_clobber_t *result = NULL;
5952 asm_clobber_t *last = NULL;
5954 while(token.type == T_STRING_LITERAL) {
5955 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5956 clobber->clobber = parse_string_literals();
5959 last->next = clobber;
5965 if(token.type != ',')
5974 * Parse an asm statement.
5976 static statement_t *parse_asm_statement(void)
5980 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5981 statement->base.source_position = token.source_position;
5983 asm_statement_t *asm_statement = &statement->asms;
5985 if(token.type == T_volatile) {
5987 asm_statement->is_volatile = true;
5991 asm_statement->asm_text = parse_string_literals();
5993 if(token.type != ':')
5997 asm_statement->inputs = parse_asm_constraints();
5998 if(token.type != ':')
6002 asm_statement->outputs = parse_asm_constraints();
6003 if(token.type != ':')
6007 asm_statement->clobbers = parse_asm_clobbers();
6018 * Parse a case statement.
6020 static statement_t *parse_case_statement(void)
6024 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6026 statement->base.source_position = token.source_position;
6027 statement->case_label.expression = parse_expression();
6029 if (c_mode & _GNUC) {
6030 if (token.type == T_DOTDOTDOT) {
6032 statement->case_label.end_range = parse_expression();
6038 if (! is_constant_expression(statement->case_label.expression)) {
6039 errorf(statement->base.source_position,
6040 "case label does not reduce to an integer constant");
6042 /* TODO: check if the case label is already known */
6043 if (current_switch != NULL) {
6044 /* link all cases into the switch statement */
6045 if (current_switch->last_case == NULL) {
6046 current_switch->first_case =
6047 current_switch->last_case = &statement->case_label;
6049 current_switch->last_case->next = &statement->case_label;
6052 errorf(statement->base.source_position,
6053 "case label not within a switch statement");
6056 statement->case_label.statement = parse_statement();
6064 * Finds an existing default label of a switch statement.
6066 static case_label_statement_t *
6067 find_default_label(const switch_statement_t *statement)
6069 case_label_statement_t *label = statement->first_case;
6070 for ( ; label != NULL; label = label->next) {
6071 if (label->expression == NULL)
6078 * Parse a default statement.
6080 static statement_t *parse_default_statement(void)
6084 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6086 statement->base.source_position = token.source_position;
6089 if (current_switch != NULL) {
6090 const case_label_statement_t *def_label = find_default_label(current_switch);
6091 if (def_label != NULL) {
6092 errorf(HERE, "multiple default labels in one switch");
6093 errorf(def_label->base.source_position,
6094 "this is the first default label");
6096 /* link all cases into the switch statement */
6097 if (current_switch->last_case == NULL) {
6098 current_switch->first_case =
6099 current_switch->last_case = &statement->case_label;
6101 current_switch->last_case->next = &statement->case_label;
6105 errorf(statement->base.source_position,
6106 "'default' label not within a switch statement");
6108 statement->case_label.statement = parse_statement();
6116 * Return the declaration for a given label symbol or create a new one.
6118 static declaration_t *get_label(symbol_t *symbol)
6120 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
6121 assert(current_function != NULL);
6122 /* if we found a label in the same function, then we already created the
6124 if(candidate != NULL
6125 && candidate->parent_scope == ¤t_function->scope) {
6129 /* otherwise we need to create a new one */
6130 declaration_t *const declaration = allocate_declaration_zero();
6131 declaration->namespc = NAMESPACE_LABEL;
6132 declaration->symbol = symbol;
6134 label_push(declaration);
6140 * Parse a label statement.
6142 static statement_t *parse_label_statement(void)
6144 assert(token.type == T_IDENTIFIER);
6145 symbol_t *symbol = token.v.symbol;
6148 declaration_t *label = get_label(symbol);
6150 /* if source position is already set then the label is defined twice,
6151 * otherwise it was just mentioned in a goto so far */
6152 if(label->source_position.input_name != NULL) {
6153 errorf(HERE, "duplicate label '%Y'", symbol);
6154 errorf(label->source_position, "previous definition of '%Y' was here",
6157 label->source_position = token.source_position;
6160 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
6162 statement->base.source_position = token.source_position;
6163 statement->label.label = label;
6167 if(token.type == '}') {
6168 /* TODO only warn? */
6169 errorf(HERE, "label at end of compound statement");
6172 if (token.type == ';') {
6173 /* eat an empty statement here, to avoid the warning about an empty
6174 * after a label. label:; is commonly used to have a label before
6178 statement->label.statement = parse_statement();
6182 /* remember the labels's in a list for later checking */
6183 if (label_last == NULL) {
6184 label_first = &statement->label;
6186 label_last->next = &statement->label;
6188 label_last = &statement->label;
6194 * Parse an if statement.
6196 static statement_t *parse_if(void)
6200 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
6201 statement->base.source_position = token.source_position;
6204 statement->ifs.condition = parse_expression();
6207 statement->ifs.true_statement = parse_statement();
6208 if(token.type == T_else) {
6210 statement->ifs.false_statement = parse_statement();
6219 * Parse a switch statement.
6221 static statement_t *parse_switch(void)
6225 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
6226 statement->base.source_position = token.source_position;
6229 expression_t *const expr = parse_expression();
6230 type_t * type = skip_typeref(expr->base.type);
6231 if (is_type_integer(type)) {
6232 type = promote_integer(type);
6233 } else if (is_type_valid(type)) {
6234 errorf(expr->base.source_position,
6235 "switch quantity is not an integer, but '%T'", type);
6236 type = type_error_type;
6238 statement->switchs.expression = create_implicit_cast(expr, type);
6241 switch_statement_t *rem = current_switch;
6242 current_switch = &statement->switchs;
6243 statement->switchs.body = parse_statement();
6244 current_switch = rem;
6246 if (warning.switch_default
6247 && find_default_label(&statement->switchs) == NULL) {
6248 warningf(statement->base.source_position, "switch has no default case");
6256 static statement_t *parse_loop_body(statement_t *const loop)
6258 statement_t *const rem = current_loop;
6259 current_loop = loop;
6261 statement_t *const body = parse_statement();
6268 * Parse a while statement.
6270 static statement_t *parse_while(void)
6274 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
6275 statement->base.source_position = token.source_position;
6278 statement->whiles.condition = parse_expression();
6281 statement->whiles.body = parse_loop_body(statement);
6289 * Parse a do statement.
6291 static statement_t *parse_do(void)
6295 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
6297 statement->base.source_position = token.source_position;
6299 statement->do_while.body = parse_loop_body(statement);
6303 statement->do_while.condition = parse_expression();
6313 * Parse a for statement.
6315 static statement_t *parse_for(void)
6319 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6320 statement->base.source_position = token.source_position;
6322 int top = environment_top();
6323 scope_t *last_scope = scope;
6324 set_scope(&statement->fors.scope);
6328 if(token.type != ';') {
6329 if(is_declaration_specifier(&token, false)) {
6330 parse_declaration(record_declaration);
6332 expression_t *const init = parse_expression();
6333 statement->fors.initialisation = init;
6334 if (warning.unused_value && !expression_has_effect(init)) {
6335 warningf(init->base.source_position,
6336 "initialisation of 'for'-statement has no effect");
6344 if(token.type != ';') {
6345 statement->fors.condition = parse_expression();
6348 if(token.type != ')') {
6349 expression_t *const step = parse_expression();
6350 statement->fors.step = step;
6351 if (warning.unused_value && !expression_has_effect(step)) {
6352 warningf(step->base.source_position,
6353 "step of 'for'-statement has no effect");
6357 statement->fors.body = parse_loop_body(statement);
6359 assert(scope == &statement->fors.scope);
6360 set_scope(last_scope);
6361 environment_pop_to(top);
6366 assert(scope == &statement->fors.scope);
6367 set_scope(last_scope);
6368 environment_pop_to(top);
6374 * Parse a goto statement.
6376 static statement_t *parse_goto(void)
6380 if(token.type != T_IDENTIFIER) {
6381 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6385 symbol_t *symbol = token.v.symbol;
6388 declaration_t *label = get_label(symbol);
6390 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6391 statement->base.source_position = token.source_position;
6393 statement->gotos.label = label;
6395 /* remember the goto's in a list for later checking */
6396 if (goto_last == NULL) {
6397 goto_first = &statement->gotos;
6399 goto_last->next = &statement->gotos;
6401 goto_last = &statement->gotos;
6411 * Parse a continue statement.
6413 static statement_t *parse_continue(void)
6415 statement_t *statement;
6416 if (current_loop == NULL) {
6417 errorf(HERE, "continue statement not within loop");
6420 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6422 statement->base.source_position = token.source_position;
6434 * Parse a break statement.
6436 static statement_t *parse_break(void)
6438 statement_t *statement;
6439 if (current_switch == NULL && current_loop == NULL) {
6440 errorf(HERE, "break statement not within loop or switch");
6443 statement = allocate_statement_zero(STATEMENT_BREAK);
6445 statement->base.source_position = token.source_position;
6457 * Check if a given declaration represents a local variable.
6459 static bool is_local_var_declaration(const declaration_t *declaration) {
6460 switch ((storage_class_tag_t) declaration->storage_class) {
6461 case STORAGE_CLASS_AUTO:
6462 case STORAGE_CLASS_REGISTER: {
6463 const type_t *type = skip_typeref(declaration->type);
6464 if(is_type_function(type)) {
6476 * Check if a given declaration represents a variable.
6478 static bool is_var_declaration(const declaration_t *declaration) {
6479 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6482 const type_t *type = skip_typeref(declaration->type);
6483 return !is_type_function(type);
6487 * Check if a given expression represents a local variable.
6489 static bool is_local_variable(const expression_t *expression)
6491 if (expression->base.kind != EXPR_REFERENCE) {
6494 const declaration_t *declaration = expression->reference.declaration;
6495 return is_local_var_declaration(declaration);
6499 * Check if a given expression represents a local variable and
6500 * return its declaration then, else return NULL.
6502 declaration_t *expr_is_variable(const expression_t *expression)
6504 if (expression->base.kind != EXPR_REFERENCE) {
6507 declaration_t *declaration = expression->reference.declaration;
6508 if (is_var_declaration(declaration))
6514 * Parse a return statement.
6516 static statement_t *parse_return(void)
6520 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6521 statement->base.source_position = token.source_position;
6523 expression_t *return_value = NULL;
6524 if(token.type != ';') {
6525 return_value = parse_expression();
6529 const type_t *const func_type = current_function->type;
6530 assert(is_type_function(func_type));
6531 type_t *const return_type = skip_typeref(func_type->function.return_type);
6533 if(return_value != NULL) {
6534 type_t *return_value_type = skip_typeref(return_value->base.type);
6536 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6537 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6538 warningf(statement->base.source_position,
6539 "'return' with a value, in function returning void");
6540 return_value = NULL;
6542 type_t *const res_type = semantic_assign(return_type,
6543 return_value, "'return'");
6544 if (res_type == NULL) {
6545 errorf(statement->base.source_position,
6546 "cannot return something of type '%T' in function returning '%T'",
6547 return_value->base.type, return_type);
6549 return_value = create_implicit_cast(return_value, res_type);
6552 /* check for returning address of a local var */
6553 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6554 const expression_t *expression = return_value->unary.value;
6555 if (is_local_variable(expression)) {
6556 warningf(statement->base.source_position,
6557 "function returns address of local variable");
6561 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6562 warningf(statement->base.source_position,
6563 "'return' without value, in function returning non-void");
6566 statement->returns.value = return_value;
6574 * Parse a declaration statement.
6576 static statement_t *parse_declaration_statement(void)
6578 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6580 statement->base.source_position = token.source_position;
6582 declaration_t *before = last_declaration;
6583 parse_declaration(record_declaration);
6585 if(before == NULL) {
6586 statement->declaration.declarations_begin = scope->declarations;
6588 statement->declaration.declarations_begin = before->next;
6590 statement->declaration.declarations_end = last_declaration;
6596 * Parse an expression statement, ie. expr ';'.
6598 static statement_t *parse_expression_statement(void)
6600 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6602 statement->base.source_position = token.source_position;
6603 expression_t *const expr = parse_expression();
6604 statement->expression.expression = expr;
6606 if (warning.unused_value && !expression_has_effect(expr)) {
6607 warningf(expr->base.source_position, "statement has no effect");
6618 * Parse a statement.
6620 static statement_t *parse_statement(void)
6622 statement_t *statement = NULL;
6624 /* declaration or statement */
6625 switch(token.type) {
6627 statement = parse_asm_statement();
6631 statement = parse_case_statement();
6635 statement = parse_default_statement();
6639 statement = parse_compound_statement();
6643 statement = parse_if();
6647 statement = parse_switch();
6651 statement = parse_while();
6655 statement = parse_do();
6659 statement = parse_for();
6663 statement = parse_goto();
6667 statement = parse_continue();
6671 statement = parse_break();
6675 statement = parse_return();
6679 if (warning.empty_statement) {
6680 warningf(HERE, "statement is empty");
6687 if(look_ahead(1)->type == ':') {
6688 statement = parse_label_statement();
6692 if(is_typedef_symbol(token.v.symbol)) {
6693 statement = parse_declaration_statement();
6697 statement = parse_expression_statement();
6700 case T___extension__:
6701 /* this can be a prefix to a declaration or an expression statement */
6702 /* we simply eat it now and parse the rest with tail recursion */
6705 } while(token.type == T___extension__);
6706 statement = parse_statement();
6710 statement = parse_declaration_statement();
6714 statement = parse_expression_statement();
6718 assert(statement == NULL
6719 || statement->base.source_position.input_name != NULL);
6725 * Parse a compound statement.
6727 static statement_t *parse_compound_statement(void)
6729 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6731 statement->base.source_position = token.source_position;
6735 int top = environment_top();
6736 scope_t *last_scope = scope;
6737 set_scope(&statement->compound.scope);
6739 statement_t *last_statement = NULL;
6741 while(token.type != '}' && token.type != T_EOF) {
6742 statement_t *sub_statement = parse_statement();
6743 if(sub_statement == NULL)
6746 if(last_statement != NULL) {
6747 last_statement->base.next = sub_statement;
6749 statement->compound.statements = sub_statement;
6752 while(sub_statement->base.next != NULL)
6753 sub_statement = sub_statement->base.next;
6755 last_statement = sub_statement;
6758 if(token.type == '}') {
6761 errorf(statement->base.source_position,
6762 "end of file while looking for closing '}'");
6765 assert(scope == &statement->compound.scope);
6766 set_scope(last_scope);
6767 environment_pop_to(top);
6773 * Initialize builtin types.
6775 static void initialize_builtin_types(void)
6777 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6778 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6779 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6780 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6781 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6782 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6783 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6784 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6786 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6787 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6788 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6789 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6793 * Check for unused global static functions and variables
6795 static void check_unused_globals(void)
6797 if (!warning.unused_function && !warning.unused_variable)
6800 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6801 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6804 type_t *const type = decl->type;
6806 if (is_type_function(skip_typeref(type))) {
6807 if (!warning.unused_function || decl->is_inline)
6810 s = (decl->init.statement != NULL ? "defined" : "declared");
6812 if (!warning.unused_variable)
6818 warningf(decl->source_position, "'%#T' %s but not used",
6819 type, decl->symbol, s);
6824 * Parse a translation unit.
6826 static translation_unit_t *parse_translation_unit(void)
6828 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6830 assert(global_scope == NULL);
6831 global_scope = &unit->scope;
6833 assert(scope == NULL);
6834 set_scope(&unit->scope);
6836 initialize_builtin_types();
6838 while(token.type != T_EOF) {
6839 if (token.type == ';') {
6840 /* TODO error in strict mode */
6841 warningf(HERE, "stray ';' outside of function");
6844 parse_external_declaration();
6848 assert(scope == &unit->scope);
6850 last_declaration = NULL;
6852 assert(global_scope == &unit->scope);
6853 check_unused_globals();
6854 global_scope = NULL;
6862 * @return the translation unit or NULL if errors occurred.
6864 translation_unit_t *parse(void)
6866 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6867 label_stack = NEW_ARR_F(stack_entry_t, 0);
6868 diagnostic_count = 0;
6872 type_set_output(stderr);
6873 ast_set_output(stderr);
6875 lookahead_bufpos = 0;
6876 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6879 translation_unit_t *unit = parse_translation_unit();
6881 DEL_ARR_F(environment_stack);
6882 DEL_ARR_F(label_stack);
6891 * Initialize the parser.
6893 void init_parser(void)
6896 /* add predefined symbols for extended-decl-modifier */
6897 sym_align = symbol_table_insert("align");
6898 sym_allocate = symbol_table_insert("allocate");
6899 sym_dllimport = symbol_table_insert("dllimport");
6900 sym_dllexport = symbol_table_insert("dllexport");
6901 sym_naked = symbol_table_insert("naked");
6902 sym_noinline = symbol_table_insert("noinline");
6903 sym_noreturn = symbol_table_insert("noreturn");
6904 sym_nothrow = symbol_table_insert("nothrow");
6905 sym_novtable = symbol_table_insert("novtable");
6906 sym_property = symbol_table_insert("property");
6907 sym_get = symbol_table_insert("get");
6908 sym_put = symbol_table_insert("put");
6909 sym_selectany = symbol_table_insert("selectany");
6910 sym_thread = symbol_table_insert("thread");
6911 sym_uuid = symbol_table_insert("uuid");
6912 sym_deprecated = symbol_table_insert("deprecated");
6914 init_expression_parsers();
6915 obstack_init(&temp_obst);
6917 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6918 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6922 * Terminate the parser.
6924 void exit_parser(void)
6926 obstack_free(&temp_obst, NULL);