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. */
56 unsigned int is_inline : 1;
57 unsigned int deprecated : 1;
58 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
59 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
60 symbol_t *get_property_sym; /**< the name of the get property if set. */
61 symbol_t *put_property_sym; /**< the name of the put property if set. */
66 * An environment for parsing initializers (and compound literals).
68 typedef struct parse_initializer_env_t {
69 type_t *type; /**< the type of the initializer. In case of an
70 array type with unspecified size this gets
71 adjusted to the actual size. */
72 declaration_t *declaration; /**< the declaration that is initialized if any */
73 bool must_be_constant;
74 } parse_initializer_env_t;
76 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
79 static token_t lookahead_buffer[MAX_LOOKAHEAD];
80 static int lookahead_bufpos;
81 static stack_entry_t *environment_stack = NULL;
82 static stack_entry_t *label_stack = NULL;
83 static scope_t *global_scope = NULL;
84 static scope_t *scope = NULL;
85 static declaration_t *last_declaration = NULL;
86 static declaration_t *current_function = NULL;
87 static switch_statement_t *current_switch = NULL;
88 static statement_t *current_loop = NULL;
89 static goto_statement_t *goto_first = NULL;
90 static goto_statement_t *goto_last = NULL;
91 static label_statement_t *label_first = NULL;
92 static label_statement_t *label_last = NULL;
93 static struct obstack temp_obst;
95 /* symbols for Microsoft extended-decl-modifier */
96 static const symbol_t *sym_align = NULL;
97 static const symbol_t *sym_allocate = NULL;
98 static const symbol_t *sym_dllimport = NULL;
99 static const symbol_t *sym_dllexport = NULL;
100 static const symbol_t *sym_naked = NULL;
101 static const symbol_t *sym_noinline = NULL;
102 static const symbol_t *sym_noreturn = NULL;
103 static const symbol_t *sym_nothrow = NULL;
104 static const symbol_t *sym_novtable = NULL;
105 static const symbol_t *sym_property = NULL;
106 static const symbol_t *sym_get = NULL;
107 static const symbol_t *sym_put = NULL;
108 static const symbol_t *sym_selectany = NULL;
109 static const symbol_t *sym_thread = NULL;
110 static const symbol_t *sym_uuid = NULL;
111 static const symbol_t *sym_deprecated = NULL;
112 static const symbol_t *sym_restrict = NULL;
113 static const symbol_t *sym_noalias = NULL;
115 /** The token anchor set */
116 static unsigned char token_anchor_set[T_LAST_TOKEN];
118 /** The current source position. */
119 #define HERE token.source_position
121 static type_t *type_valist;
123 static statement_t *parse_compound_statement(void);
124 static statement_t *parse_statement(void);
126 static expression_t *parse_sub_expression(unsigned precedence);
127 static expression_t *parse_expression(void);
128 static type_t *parse_typename(void);
130 static void parse_compound_type_entries(declaration_t *compound_declaration);
131 static declaration_t *parse_declarator(
132 const declaration_specifiers_t *specifiers, bool may_be_abstract);
133 static declaration_t *record_declaration(declaration_t *declaration);
135 static void semantic_comparison(binary_expression_t *expression);
137 #define STORAGE_CLASSES \
144 #define TYPE_QUALIFIERS \
151 #ifdef PROVIDE_COMPLEX
152 #define COMPLEX_SPECIFIERS \
154 #define IMAGINARY_SPECIFIERS \
157 #define COMPLEX_SPECIFIERS
158 #define IMAGINARY_SPECIFIERS
161 #define TYPE_SPECIFIERS \
176 case T___builtin_va_list: \
181 #define DECLARATION_START \
186 #define TYPENAME_START \
191 * Allocate an AST node with given size and
192 * initialize all fields with zero.
194 static void *allocate_ast_zero(size_t size)
196 void *res = allocate_ast(size);
197 memset(res, 0, size);
201 static declaration_t *allocate_declaration_zero(void)
203 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
204 declaration->type = type_error_type;
205 declaration->alignment = 0;
210 * Returns the size of a statement node.
212 * @param kind the statement kind
214 static size_t get_statement_struct_size(statement_kind_t kind)
216 static const size_t sizes[] = {
217 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
218 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
219 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
220 [STATEMENT_RETURN] = sizeof(return_statement_t),
221 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
222 [STATEMENT_IF] = sizeof(if_statement_t),
223 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
224 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
225 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
226 [STATEMENT_BREAK] = sizeof(statement_base_t),
227 [STATEMENT_GOTO] = sizeof(goto_statement_t),
228 [STATEMENT_LABEL] = sizeof(label_statement_t),
229 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
230 [STATEMENT_WHILE] = sizeof(while_statement_t),
231 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
232 [STATEMENT_FOR] = sizeof(for_statement_t),
233 [STATEMENT_ASM] = sizeof(asm_statement_t)
235 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
236 assert(sizes[kind] != 0);
241 * Allocate a statement node of given kind and initialize all
244 static statement_t *allocate_statement_zero(statement_kind_t kind)
246 size_t size = get_statement_struct_size(kind);
247 statement_t *res = allocate_ast_zero(size);
249 res->base.kind = kind;
254 * Creates a new invalid statement.
256 static statement_t *create_invalid_statement(void)
258 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
259 statement->base.source_position = token.source_position;
264 * Allocate a new empty statement.
266 static statement_t *create_empty_statement(void)
268 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
269 statement->base.source_position = token.source_position;
274 * Returns the size of an expression node.
276 * @param kind the expression kind
278 static size_t get_expression_struct_size(expression_kind_t kind)
280 static const size_t sizes[] = {
281 [EXPR_INVALID] = sizeof(expression_base_t),
282 [EXPR_REFERENCE] = sizeof(reference_expression_t),
283 [EXPR_CONST] = sizeof(const_expression_t),
284 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
285 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
286 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
287 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
288 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
289 [EXPR_CALL] = sizeof(call_expression_t),
290 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
291 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
292 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
293 [EXPR_SELECT] = sizeof(select_expression_t),
294 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
295 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
296 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
297 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
298 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
299 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
300 [EXPR_FUNCSIG] = sizeof(string_literal_expression_t),
301 [EXPR_FUNCDNAME] = sizeof(string_literal_expression_t),
302 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
303 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
304 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
305 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
306 [EXPR_VA_START] = sizeof(va_start_expression_t),
307 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
308 [EXPR_STATEMENT] = sizeof(statement_expression_t),
310 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
311 return sizes[EXPR_UNARY_FIRST];
313 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
314 return sizes[EXPR_BINARY_FIRST];
316 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
317 assert(sizes[kind] != 0);
322 * Allocate an expression node of given kind and initialize all
325 static expression_t *allocate_expression_zero(expression_kind_t kind)
327 size_t size = get_expression_struct_size(kind);
328 expression_t *res = allocate_ast_zero(size);
330 res->base.kind = kind;
331 res->base.type = type_error_type;
336 * Returns the size of a type node.
338 * @param kind the type kind
340 static size_t get_type_struct_size(type_kind_t kind)
342 static const size_t sizes[] = {
343 [TYPE_ATOMIC] = sizeof(atomic_type_t),
344 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
345 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
346 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
347 [TYPE_ENUM] = sizeof(enum_type_t),
348 [TYPE_FUNCTION] = sizeof(function_type_t),
349 [TYPE_POINTER] = sizeof(pointer_type_t),
350 [TYPE_ARRAY] = sizeof(array_type_t),
351 [TYPE_BUILTIN] = sizeof(builtin_type_t),
352 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
353 [TYPE_TYPEOF] = sizeof(typeof_type_t),
355 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
356 assert(kind <= TYPE_TYPEOF);
357 assert(sizes[kind] != 0);
362 * Allocate a type node of given kind and initialize all
365 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
367 size_t size = get_type_struct_size(kind);
368 type_t *res = obstack_alloc(type_obst, size);
369 memset(res, 0, size);
371 res->base.kind = kind;
372 res->base.source_position = source_position;
377 * Returns the size of an initializer node.
379 * @param kind the initializer kind
381 static size_t get_initializer_size(initializer_kind_t kind)
383 static const size_t sizes[] = {
384 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
385 [INITIALIZER_STRING] = sizeof(initializer_string_t),
386 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
387 [INITIALIZER_LIST] = sizeof(initializer_list_t),
388 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
390 assert(kind < sizeof(sizes) / sizeof(*sizes));
391 assert(sizes[kind] != 0);
396 * Allocate an initializer node of given kind and initialize all
399 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
401 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
408 * Free a type from the type obstack.
410 static void free_type(void *type)
412 obstack_free(type_obst, type);
416 * Returns the index of the top element of the environment stack.
418 static size_t environment_top(void)
420 return ARR_LEN(environment_stack);
424 * Returns the index of the top element of the label stack.
426 static size_t label_top(void)
428 return ARR_LEN(label_stack);
432 * Return the next token.
434 static inline void next_token(void)
436 token = lookahead_buffer[lookahead_bufpos];
437 lookahead_buffer[lookahead_bufpos] = lexer_token;
440 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
443 print_token(stderr, &token);
444 fprintf(stderr, "\n");
449 * Return the next token with a given lookahead.
451 static inline const token_t *look_ahead(int num)
453 assert(num > 0 && num <= MAX_LOOKAHEAD);
454 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
455 return &lookahead_buffer[pos];
459 * Adds a token to the token anchor set (a multi-set).
461 static void add_anchor_token(int token_type) {
462 assert(0 <= token_type && token_type < T_LAST_TOKEN);
463 ++token_anchor_set[token_type];
467 * Remove a token from the token anchor set (a multi-set).
469 static void rem_anchor_token(int token_type) {
470 assert(0 <= token_type && token_type < T_LAST_TOKEN);
471 --token_anchor_set[token_type];
474 static bool at_anchor(void) {
477 return token_anchor_set[token.type];
481 * Eat tokens until a matching token is found.
483 static void eat_until_matching_token(int type) {
484 unsigned parenthesis_count = 0;
485 unsigned brace_count = 0;
486 unsigned bracket_count = 0;
487 int end_token = type;
496 while(token.type != end_token ||
497 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
501 case '(': ++parenthesis_count; break;
502 case '{': ++brace_count; break;
503 case '[': ++bracket_count; break;
505 if(parenthesis_count > 0)
513 if(bracket_count > 0)
524 * Eat input tokens until an anchor is found.
526 static void eat_until_anchor(void) {
527 if(token.type == T_EOF)
529 while(token_anchor_set[token.type] == 0) {
530 if(token.type == '(' || token.type == '{' || token.type == '[')
531 eat_until_matching_token(token.type);
532 if(token.type == T_EOF)
538 static void eat_block(void) {
539 eat_until_matching_token('{');
540 if(token.type == '}')
545 * eat all token until a ';' is reached
546 * or a stop token is found.
548 static void eat_statement(void) {
549 eat_until_matching_token(';');
550 if(token.type == ';')
554 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
557 * Report a parse error because an expected token was not found.
559 static void parse_error_expected(const char *message, ...)
561 if(message != NULL) {
562 errorf(HERE, "%s", message);
565 va_start(ap, message);
566 errorf(HERE, "got %K, expected %#k", &token, &ap, "a ");
571 * Report a type error.
573 static void type_error(const char *msg, const source_position_t source_position,
576 errorf(source_position, "%s, but found type '%T'", msg, type);
580 * Report an incompatible type.
582 static void type_error_incompatible(const char *msg,
583 const source_position_t source_position, type_t *type1, type_t *type2)
585 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
589 * Expect the the current token is the expected token.
590 * If not, generate an error, eat the current statement,
591 * and goto the end_error label.
593 #define expect(expected) \
595 if(UNLIKELY(token.type != (expected))) { \
596 parse_error_expected(NULL, (expected), 0); \
597 add_anchor_token(expected); \
598 eat_until_anchor(); \
599 rem_anchor_token(expected); \
605 static void set_scope(scope_t *new_scope)
608 scope->last_declaration = last_declaration;
612 last_declaration = new_scope->last_declaration;
616 * Search a symbol in a given namespace and returns its declaration or
617 * NULL if this symbol was not found.
619 static declaration_t *get_declaration(const symbol_t *const symbol,
620 const namespace_t namespc)
622 declaration_t *declaration = symbol->declaration;
623 for( ; declaration != NULL; declaration = declaration->symbol_next) {
624 if(declaration->namespc == namespc)
632 * pushs an environment_entry on the environment stack and links the
633 * corresponding symbol to the new entry
635 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
637 symbol_t *symbol = declaration->symbol;
638 namespace_t namespc = (namespace_t) declaration->namespc;
640 /* replace/add declaration into declaration list of the symbol */
641 declaration_t *iter = symbol->declaration;
643 symbol->declaration = declaration;
645 declaration_t *iter_last = NULL;
646 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
647 /* replace an entry? */
648 if(iter->namespc == namespc) {
649 if(iter_last == NULL) {
650 symbol->declaration = declaration;
652 iter_last->symbol_next = declaration;
654 declaration->symbol_next = iter->symbol_next;
659 assert(iter_last->symbol_next == NULL);
660 iter_last->symbol_next = declaration;
664 /* remember old declaration */
666 entry.symbol = symbol;
667 entry.old_declaration = iter;
668 entry.namespc = (unsigned short) namespc;
669 ARR_APP1(stack_entry_t, *stack_ptr, entry);
672 static void environment_push(declaration_t *declaration)
674 assert(declaration->source_position.input_name != NULL);
675 assert(declaration->parent_scope != NULL);
676 stack_push(&environment_stack, declaration);
679 static void label_push(declaration_t *declaration)
681 declaration->parent_scope = ¤t_function->scope;
682 stack_push(&label_stack, declaration);
686 * pops symbols from the environment stack until @p new_top is the top element
688 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
690 stack_entry_t *stack = *stack_ptr;
691 size_t top = ARR_LEN(stack);
694 assert(new_top <= top);
698 for(i = top; i > new_top; --i) {
699 stack_entry_t *entry = &stack[i - 1];
701 declaration_t *old_declaration = entry->old_declaration;
702 symbol_t *symbol = entry->symbol;
703 namespace_t namespc = (namespace_t)entry->namespc;
705 /* replace/remove declaration */
706 declaration_t *declaration = symbol->declaration;
707 assert(declaration != NULL);
708 if(declaration->namespc == namespc) {
709 if(old_declaration == NULL) {
710 symbol->declaration = declaration->symbol_next;
712 symbol->declaration = old_declaration;
715 declaration_t *iter_last = declaration;
716 declaration_t *iter = declaration->symbol_next;
717 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
718 /* replace an entry? */
719 if(iter->namespc == namespc) {
720 assert(iter_last != NULL);
721 iter_last->symbol_next = old_declaration;
722 if(old_declaration != NULL) {
723 old_declaration->symbol_next = iter->symbol_next;
728 assert(iter != NULL);
732 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
735 static void environment_pop_to(size_t new_top)
737 stack_pop_to(&environment_stack, new_top);
740 static void label_pop_to(size_t new_top)
742 stack_pop_to(&label_stack, new_top);
746 static int get_rank(const type_t *type)
748 assert(!is_typeref(type));
749 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
750 * and esp. footnote 108). However we can't fold constants (yet), so we
751 * can't decide whether unsigned int is possible, while int always works.
752 * (unsigned int would be preferable when possible... for stuff like
753 * struct { enum { ... } bla : 4; } ) */
754 if(type->kind == TYPE_ENUM)
755 return ATOMIC_TYPE_INT;
757 assert(type->kind == TYPE_ATOMIC);
758 return type->atomic.akind;
761 static type_t *promote_integer(type_t *type)
763 if(type->kind == TYPE_BITFIELD)
764 type = type->bitfield.base;
766 if(get_rank(type) < ATOMIC_TYPE_INT)
773 * Create a cast expression.
775 * @param expression the expression to cast
776 * @param dest_type the destination type
778 static expression_t *create_cast_expression(expression_t *expression,
781 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
783 cast->unary.value = expression;
784 cast->base.type = dest_type;
790 * Check if a given expression represents the 0 pointer constant.
792 static bool is_null_pointer_constant(const expression_t *expression)
794 /* skip void* cast */
795 if(expression->kind == EXPR_UNARY_CAST
796 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
797 expression = expression->unary.value;
800 /* TODO: not correct yet, should be any constant integer expression
801 * which evaluates to 0 */
802 if (expression->kind != EXPR_CONST)
805 type_t *const type = skip_typeref(expression->base.type);
806 if (!is_type_integer(type))
809 return expression->conste.v.int_value == 0;
813 * Create an implicit cast expression.
815 * @param expression the expression to cast
816 * @param dest_type the destination type
818 static expression_t *create_implicit_cast(expression_t *expression,
821 type_t *const source_type = expression->base.type;
823 if (source_type == dest_type)
826 return create_cast_expression(expression, dest_type);
829 /** Implements the rules from § 6.5.16.1 */
830 static type_t *semantic_assign(type_t *orig_type_left,
831 const expression_t *const right,
834 type_t *const orig_type_right = right->base.type;
835 type_t *const type_left = skip_typeref(orig_type_left);
836 type_t *const type_right = skip_typeref(orig_type_right);
838 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
839 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
840 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
841 && is_type_pointer(type_right))) {
842 return orig_type_left;
845 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
846 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
847 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
849 /* the left type has all qualifiers from the right type */
850 unsigned missing_qualifiers
851 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
852 if(missing_qualifiers != 0) {
853 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
854 return orig_type_left;
857 points_to_left = get_unqualified_type(points_to_left);
858 points_to_right = get_unqualified_type(points_to_right);
860 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
861 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
862 return orig_type_left;
865 if (!types_compatible(points_to_left, points_to_right)) {
866 warningf(right->base.source_position,
867 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
868 orig_type_left, context, right, orig_type_right);
871 return orig_type_left;
874 if ((is_type_compound(type_left) && is_type_compound(type_right))
875 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
876 type_t *const unqual_type_left = get_unqualified_type(type_left);
877 type_t *const unqual_type_right = get_unqualified_type(type_right);
878 if (types_compatible(unqual_type_left, unqual_type_right)) {
879 return orig_type_left;
883 if (!is_type_valid(type_left))
886 if (!is_type_valid(type_right))
887 return orig_type_right;
892 static expression_t *parse_constant_expression(void)
894 /* start parsing at precedence 7 (conditional expression) */
895 expression_t *result = parse_sub_expression(7);
897 if(!is_constant_expression(result)) {
898 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
904 static expression_t *parse_assignment_expression(void)
906 /* start parsing at precedence 2 (assignment expression) */
907 return parse_sub_expression(2);
910 static type_t *make_global_typedef(const char *name, type_t *type)
912 symbol_t *const symbol = symbol_table_insert(name);
914 declaration_t *const declaration = allocate_declaration_zero();
915 declaration->namespc = NAMESPACE_NORMAL;
916 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
917 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
918 declaration->type = type;
919 declaration->symbol = symbol;
920 declaration->source_position = builtin_source_position;
922 record_declaration(declaration);
924 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
925 typedef_type->typedeft.declaration = declaration;
930 static string_t parse_string_literals(void)
932 assert(token.type == T_STRING_LITERAL);
933 string_t result = token.v.string;
937 while (token.type == T_STRING_LITERAL) {
938 result = concat_strings(&result, &token.v.string);
946 * Parse one GNU attribute.
948 static void parse_gnu_attribute(void)
950 eat(T___attribute__);
954 if(token.type != T_IDENTIFIER)
956 symbol_t *symbol = token.v.symbol;
958 if(token.type == '(')
959 eat_until_matching_token('(');
960 if(token.type != ',')
971 * Parse GNU attributes.
973 static void parse_attributes(void)
977 case T___attribute__: {
978 parse_gnu_attribute();
984 if(token.type != T_STRING_LITERAL) {
985 parse_error_expected("while parsing assembler attribute",
987 eat_until_matching_token('(');
990 parse_string_literals();
995 goto attributes_finished;
1000 attributes_finished:
1004 static designator_t *parse_designation(void)
1006 designator_t *result = NULL;
1007 designator_t *last = NULL;
1010 designator_t *designator;
1011 switch(token.type) {
1013 designator = allocate_ast_zero(sizeof(designator[0]));
1014 designator->source_position = token.source_position;
1016 add_anchor_token(']');
1017 designator->array_index = parse_constant_expression();
1018 rem_anchor_token(']');
1022 designator = allocate_ast_zero(sizeof(designator[0]));
1023 designator->source_position = token.source_position;
1025 if(token.type != T_IDENTIFIER) {
1026 parse_error_expected("while parsing designator",
1030 designator->symbol = token.v.symbol;
1038 assert(designator != NULL);
1040 last->next = designator;
1042 result = designator;
1050 static initializer_t *initializer_from_string(array_type_t *type,
1051 const string_t *const string)
1053 /* TODO: check len vs. size of array type */
1056 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1057 initializer->string.string = *string;
1062 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1063 wide_string_t *const string)
1065 /* TODO: check len vs. size of array type */
1068 initializer_t *const initializer =
1069 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1070 initializer->wide_string.string = *string;
1076 * Build an initializer from a given expression.
1078 static initializer_t *initializer_from_expression(type_t *orig_type,
1079 expression_t *expression)
1081 /* TODO check that expression is a constant expression */
1083 /* § 6.7.8.14/15 char array may be initialized by string literals */
1084 type_t *type = skip_typeref(orig_type);
1085 type_t *expr_type_orig = expression->base.type;
1086 type_t *expr_type = skip_typeref(expr_type_orig);
1087 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1088 array_type_t *const array_type = &type->array;
1089 type_t *const element_type = skip_typeref(array_type->element_type);
1091 if (element_type->kind == TYPE_ATOMIC) {
1092 atomic_type_kind_t akind = element_type->atomic.akind;
1093 switch (expression->kind) {
1094 case EXPR_STRING_LITERAL:
1095 if (akind == ATOMIC_TYPE_CHAR
1096 || akind == ATOMIC_TYPE_SCHAR
1097 || akind == ATOMIC_TYPE_UCHAR) {
1098 return initializer_from_string(array_type,
1099 &expression->string.value);
1102 case EXPR_WIDE_STRING_LITERAL: {
1103 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1104 if (get_unqualified_type(element_type) == bare_wchar_type) {
1105 return initializer_from_wide_string(array_type,
1106 &expression->wide_string.value);
1116 type_t *const res_type = semantic_assign(type, expression, "initializer");
1117 if (res_type == NULL)
1120 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1121 result->value.value = create_implicit_cast(expression, res_type);
1127 * Checks if a given expression can be used as an constant initializer.
1129 static bool is_initializer_constant(const expression_t *expression)
1131 return is_constant_expression(expression)
1132 || is_address_constant(expression);
1136 * Parses an scalar initializer.
1138 * § 6.7.8.11; eat {} without warning
1140 static initializer_t *parse_scalar_initializer(type_t *type,
1141 bool must_be_constant)
1143 /* there might be extra {} hierarchies */
1145 while(token.type == '{') {
1148 warningf(HERE, "extra curly braces around scalar initializer");
1153 expression_t *expression = parse_assignment_expression();
1154 if(must_be_constant && !is_initializer_constant(expression)) {
1155 errorf(expression->base.source_position,
1156 "Initialisation expression '%E' is not constant\n",
1160 initializer_t *initializer = initializer_from_expression(type, expression);
1162 if(initializer == NULL) {
1163 errorf(expression->base.source_position,
1164 "expression '%E' doesn't match expected type '%T'",
1170 bool additional_warning_displayed = false;
1172 if(token.type == ',') {
1175 if(token.type != '}') {
1176 if(!additional_warning_displayed) {
1177 warningf(HERE, "additional elements in scalar initializer");
1178 additional_warning_displayed = true;
1189 * An entry in the type path.
1191 typedef struct type_path_entry_t type_path_entry_t;
1192 struct type_path_entry_t {
1193 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1195 size_t index; /**< For array types: the current index. */
1196 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1201 * A type path expression a position inside compound or array types.
1203 typedef struct type_path_t type_path_t;
1204 struct type_path_t {
1205 type_path_entry_t *path; /**< An flexible array containing the current path. */
1206 type_t *top_type; /**< type of the element the path points */
1207 size_t max_index; /**< largest index in outermost array */
1211 * Prints a type path for debugging.
1213 static __attribute__((unused)) void debug_print_type_path(
1214 const type_path_t *path)
1216 size_t len = ARR_LEN(path->path);
1218 for(size_t i = 0; i < len; ++i) {
1219 const type_path_entry_t *entry = & path->path[i];
1221 type_t *type = skip_typeref(entry->type);
1222 if(is_type_compound(type)) {
1223 /* in gcc mode structs can have no members */
1224 if(entry->v.compound_entry == NULL) {
1228 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1229 } else if(is_type_array(type)) {
1230 fprintf(stderr, "[%u]", entry->v.index);
1232 fprintf(stderr, "-INVALID-");
1235 if(path->top_type != NULL) {
1236 fprintf(stderr, " (");
1237 print_type(path->top_type);
1238 fprintf(stderr, ")");
1243 * Return the top type path entry, ie. in a path
1244 * (type).a.b returns the b.
1246 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1248 size_t len = ARR_LEN(path->path);
1250 return &path->path[len-1];
1254 * Enlarge the type path by an (empty) element.
1256 static type_path_entry_t *append_to_type_path(type_path_t *path)
1258 size_t len = ARR_LEN(path->path);
1259 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1261 type_path_entry_t *result = & path->path[len];
1262 memset(result, 0, sizeof(result[0]));
1267 * Descending into a sub-type. Enter the scope of the current
1270 static void descend_into_subtype(type_path_t *path)
1272 type_t *orig_top_type = path->top_type;
1273 type_t *top_type = skip_typeref(orig_top_type);
1275 assert(is_type_compound(top_type) || is_type_array(top_type));
1277 type_path_entry_t *top = append_to_type_path(path);
1278 top->type = top_type;
1280 if(is_type_compound(top_type)) {
1281 declaration_t *declaration = top_type->compound.declaration;
1282 declaration_t *entry = declaration->scope.declarations;
1283 top->v.compound_entry = entry;
1286 path->top_type = entry->type;
1288 path->top_type = NULL;
1291 assert(is_type_array(top_type));
1294 path->top_type = top_type->array.element_type;
1299 * Pop an entry from the given type path, ie. returning from
1300 * (type).a.b to (type).a
1302 static void ascend_from_subtype(type_path_t *path)
1304 type_path_entry_t *top = get_type_path_top(path);
1306 path->top_type = top->type;
1308 size_t len = ARR_LEN(path->path);
1309 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1313 * Pop entries from the given type path until the given
1314 * path level is reached.
1316 static void ascend_to(type_path_t *path, size_t top_path_level)
1318 size_t len = ARR_LEN(path->path);
1320 while(len > top_path_level) {
1321 ascend_from_subtype(path);
1322 len = ARR_LEN(path->path);
1326 static bool walk_designator(type_path_t *path, const designator_t *designator,
1327 bool used_in_offsetof)
1329 for( ; designator != NULL; designator = designator->next) {
1330 type_path_entry_t *top = get_type_path_top(path);
1331 type_t *orig_type = top->type;
1333 type_t *type = skip_typeref(orig_type);
1335 if(designator->symbol != NULL) {
1336 symbol_t *symbol = designator->symbol;
1337 if(!is_type_compound(type)) {
1338 if(is_type_valid(type)) {
1339 errorf(designator->source_position,
1340 "'.%Y' designator used for non-compound type '%T'",
1346 declaration_t *declaration = type->compound.declaration;
1347 declaration_t *iter = declaration->scope.declarations;
1348 for( ; iter != NULL; iter = iter->next) {
1349 if(iter->symbol == symbol) {
1354 errorf(designator->source_position,
1355 "'%T' has no member named '%Y'", orig_type, symbol);
1358 if(used_in_offsetof) {
1359 type_t *real_type = skip_typeref(iter->type);
1360 if(real_type->kind == TYPE_BITFIELD) {
1361 errorf(designator->source_position,
1362 "offsetof designator '%Y' may not specify bitfield",
1368 top->type = orig_type;
1369 top->v.compound_entry = iter;
1370 orig_type = iter->type;
1372 expression_t *array_index = designator->array_index;
1373 assert(designator->array_index != NULL);
1375 if(!is_type_array(type)) {
1376 if(is_type_valid(type)) {
1377 errorf(designator->source_position,
1378 "[%E] designator used for non-array type '%T'",
1379 array_index, orig_type);
1383 if(!is_type_valid(array_index->base.type)) {
1387 long index = fold_constant(array_index);
1388 if(!used_in_offsetof) {
1390 errorf(designator->source_position,
1391 "array index [%E] must be positive", array_index);
1394 if(type->array.size_constant == true) {
1395 long array_size = type->array.size;
1396 if(index >= array_size) {
1397 errorf(designator->source_position,
1398 "designator [%E] (%d) exceeds array size %d",
1399 array_index, index, array_size);
1405 top->type = orig_type;
1406 top->v.index = (size_t) index;
1407 orig_type = type->array.element_type;
1409 path->top_type = orig_type;
1411 if(designator->next != NULL) {
1412 descend_into_subtype(path);
1421 static void advance_current_object(type_path_t *path, size_t top_path_level)
1423 type_path_entry_t *top = get_type_path_top(path);
1425 type_t *type = skip_typeref(top->type);
1426 if(is_type_union(type)) {
1427 /* in unions only the first element is initialized */
1428 top->v.compound_entry = NULL;
1429 } else if(is_type_struct(type)) {
1430 declaration_t *entry = top->v.compound_entry;
1432 entry = entry->next;
1433 top->v.compound_entry = entry;
1435 path->top_type = entry->type;
1439 assert(is_type_array(type));
1443 if(!type->array.size_constant || top->v.index < type->array.size) {
1448 /* we're past the last member of the current sub-aggregate, try if we
1449 * can ascend in the type hierarchy and continue with another subobject */
1450 size_t len = ARR_LEN(path->path);
1452 if(len > top_path_level) {
1453 ascend_from_subtype(path);
1454 advance_current_object(path, top_path_level);
1456 path->top_type = NULL;
1461 * skip until token is found.
1463 static void skip_until(int type) {
1464 while(token.type != type) {
1465 if(token.type == T_EOF)
1472 * skip any {...} blocks until a closing braket is reached.
1474 static void skip_initializers(void)
1476 if(token.type == '{')
1479 while(token.type != '}') {
1480 if(token.type == T_EOF)
1482 if(token.type == '{') {
1490 static initializer_t *create_empty_initializer(void)
1492 static initializer_t empty_initializer
1493 = { .list = { { INITIALIZER_LIST }, 0 } };
1494 return &empty_initializer;
1498 * Parse a part of an initialiser for a struct or union,
1500 static initializer_t *parse_sub_initializer(type_path_t *path,
1501 type_t *outer_type, size_t top_path_level,
1502 parse_initializer_env_t *env)
1504 if(token.type == '}') {
1505 /* empty initializer */
1506 return create_empty_initializer();
1509 type_t *orig_type = path->top_type;
1510 type_t *type = NULL;
1512 if (orig_type == NULL) {
1513 /* We are initializing an empty compound. */
1515 type = skip_typeref(orig_type);
1517 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1518 * initializers in this case. */
1519 if(!is_type_valid(type)) {
1520 skip_initializers();
1521 return create_empty_initializer();
1525 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1528 designator_t *designator = NULL;
1529 if(token.type == '.' || token.type == '[') {
1530 designator = parse_designation();
1532 /* reset path to toplevel, evaluate designator from there */
1533 ascend_to(path, top_path_level);
1534 if(!walk_designator(path, designator, false)) {
1535 /* can't continue after designation error */
1539 initializer_t *designator_initializer
1540 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1541 designator_initializer->designator.designator = designator;
1542 ARR_APP1(initializer_t*, initializers, designator_initializer);
1547 if(token.type == '{') {
1548 if(type != NULL && is_type_scalar(type)) {
1549 sub = parse_scalar_initializer(type, env->must_be_constant);
1553 if (env->declaration != NULL)
1554 errorf(HERE, "extra brace group at end of initializer for '%Y'",
1555 env->declaration->symbol);
1557 errorf(HERE, "extra brace group at end of initializer");
1559 descend_into_subtype(path);
1561 add_anchor_token('}');
1562 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1564 rem_anchor_token('}');
1567 ascend_from_subtype(path);
1571 goto error_parse_next;
1575 /* must be an expression */
1576 expression_t *expression = parse_assignment_expression();
1578 if(env->must_be_constant && !is_initializer_constant(expression)) {
1579 errorf(expression->base.source_position,
1580 "Initialisation expression '%E' is not constant\n",
1585 /* we are already outside, ... */
1589 /* handle { "string" } special case */
1590 if((expression->kind == EXPR_STRING_LITERAL
1591 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1592 && outer_type != NULL) {
1593 sub = initializer_from_expression(outer_type, expression);
1595 if(token.type == ',') {
1598 if(token.type != '}') {
1599 warningf(HERE, "excessive elements in initializer for type '%T'",
1602 /* TODO: eat , ... */
1607 /* descend into subtypes until expression matches type */
1609 orig_type = path->top_type;
1610 type = skip_typeref(orig_type);
1612 sub = initializer_from_expression(orig_type, expression);
1616 if(!is_type_valid(type)) {
1619 if(is_type_scalar(type)) {
1620 errorf(expression->base.source_position,
1621 "expression '%E' doesn't match expected type '%T'",
1622 expression, orig_type);
1626 descend_into_subtype(path);
1630 /* update largest index of top array */
1631 const type_path_entry_t *first = &path->path[0];
1632 type_t *first_type = first->type;
1633 first_type = skip_typeref(first_type);
1634 if(is_type_array(first_type)) {
1635 size_t index = first->v.index;
1636 if(index > path->max_index)
1637 path->max_index = index;
1641 /* append to initializers list */
1642 ARR_APP1(initializer_t*, initializers, sub);
1645 if(env->declaration != NULL)
1646 warningf(HERE, "excess elements in struct initializer for '%Y'",
1647 env->declaration->symbol);
1649 warningf(HERE, "excess elements in struct initializer");
1653 if(token.type == '}') {
1657 if(token.type == '}') {
1662 /* advance to the next declaration if we are not at the end */
1663 advance_current_object(path, top_path_level);
1664 orig_type = path->top_type;
1665 if(orig_type != NULL)
1666 type = skip_typeref(orig_type);
1672 size_t len = ARR_LEN(initializers);
1673 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1674 initializer_t *result = allocate_ast_zero(size);
1675 result->kind = INITIALIZER_LIST;
1676 result->list.len = len;
1677 memcpy(&result->list.initializers, initializers,
1678 len * sizeof(initializers[0]));
1680 DEL_ARR_F(initializers);
1681 ascend_to(path, top_path_level);
1686 skip_initializers();
1687 DEL_ARR_F(initializers);
1688 ascend_to(path, top_path_level);
1693 * Parses an initializer. Parsers either a compound literal
1694 * (env->declaration == NULL) or an initializer of a declaration.
1696 static initializer_t *parse_initializer(parse_initializer_env_t *env)
1698 type_t *type = skip_typeref(env->type);
1699 initializer_t *result = NULL;
1702 if(is_type_scalar(type)) {
1703 result = parse_scalar_initializer(type, env->must_be_constant);
1704 } else if(token.type == '{') {
1708 memset(&path, 0, sizeof(path));
1709 path.top_type = env->type;
1710 path.path = NEW_ARR_F(type_path_entry_t, 0);
1712 descend_into_subtype(&path);
1714 add_anchor_token('}');
1715 result = parse_sub_initializer(&path, env->type, 1, env);
1716 rem_anchor_token('}');
1718 max_index = path.max_index;
1719 DEL_ARR_F(path.path);
1723 /* parse_scalar_initializer() also works in this case: we simply
1724 * have an expression without {} around it */
1725 result = parse_scalar_initializer(type, env->must_be_constant);
1728 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1729 * the array type size */
1730 if(is_type_array(type) && type->array.size_expression == NULL
1731 && result != NULL) {
1733 switch (result->kind) {
1734 case INITIALIZER_LIST:
1735 size = max_index + 1;
1738 case INITIALIZER_STRING:
1739 size = result->string.string.size;
1742 case INITIALIZER_WIDE_STRING:
1743 size = result->wide_string.string.size;
1747 internal_errorf(HERE, "invalid initializer type");
1750 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1751 cnst->base.type = type_size_t;
1752 cnst->conste.v.int_value = size;
1754 type_t *new_type = duplicate_type(type);
1756 new_type->array.size_expression = cnst;
1757 new_type->array.size_constant = true;
1758 new_type->array.size = size;
1759 env->type = new_type;
1767 static declaration_t *append_declaration(declaration_t *declaration);
1769 static declaration_t *parse_compound_type_specifier(bool is_struct)
1777 symbol_t *symbol = NULL;
1778 declaration_t *declaration = NULL;
1780 if (token.type == T___attribute__) {
1785 if(token.type == T_IDENTIFIER) {
1786 symbol = token.v.symbol;
1790 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1792 declaration = get_declaration(symbol, NAMESPACE_UNION);
1794 } else if(token.type != '{') {
1796 parse_error_expected("while parsing struct type specifier",
1797 T_IDENTIFIER, '{', 0);
1799 parse_error_expected("while parsing union type specifier",
1800 T_IDENTIFIER, '{', 0);
1806 if(declaration == NULL) {
1807 declaration = allocate_declaration_zero();
1808 declaration->namespc =
1809 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1810 declaration->source_position = token.source_position;
1811 declaration->symbol = symbol;
1812 declaration->parent_scope = scope;
1813 if (symbol != NULL) {
1814 environment_push(declaration);
1816 append_declaration(declaration);
1819 if(token.type == '{') {
1820 if(declaration->init.is_defined) {
1821 assert(symbol != NULL);
1822 errorf(HERE, "multiple definitions of '%s %Y'",
1823 is_struct ? "struct" : "union", symbol);
1824 declaration->scope.declarations = NULL;
1826 declaration->init.is_defined = true;
1828 parse_compound_type_entries(declaration);
1835 static void parse_enum_entries(type_t *const enum_type)
1839 if(token.type == '}') {
1841 errorf(HERE, "empty enum not allowed");
1845 add_anchor_token('}');
1847 if(token.type != T_IDENTIFIER) {
1848 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1850 rem_anchor_token('}');
1854 declaration_t *const entry = allocate_declaration_zero();
1855 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1856 entry->type = enum_type;
1857 entry->symbol = token.v.symbol;
1858 entry->source_position = token.source_position;
1861 if(token.type == '=') {
1863 expression_t *value = parse_constant_expression();
1865 value = create_implicit_cast(value, enum_type);
1866 entry->init.enum_value = value;
1871 record_declaration(entry);
1873 if(token.type != ',')
1876 } while(token.type != '}');
1877 rem_anchor_token('}');
1885 static type_t *parse_enum_specifier(void)
1889 declaration_t *declaration;
1892 if(token.type == T_IDENTIFIER) {
1893 symbol = token.v.symbol;
1896 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1897 } else if(token.type != '{') {
1898 parse_error_expected("while parsing enum type specifier",
1899 T_IDENTIFIER, '{', 0);
1906 if(declaration == NULL) {
1907 declaration = allocate_declaration_zero();
1908 declaration->namespc = NAMESPACE_ENUM;
1909 declaration->source_position = token.source_position;
1910 declaration->symbol = symbol;
1911 declaration->parent_scope = scope;
1914 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1915 type->enumt.declaration = declaration;
1917 if(token.type == '{') {
1918 if(declaration->init.is_defined) {
1919 errorf(HERE, "multiple definitions of enum %Y", symbol);
1921 if (symbol != NULL) {
1922 environment_push(declaration);
1924 append_declaration(declaration);
1925 declaration->init.is_defined = 1;
1927 parse_enum_entries(type);
1935 * if a symbol is a typedef to another type, return true
1937 static bool is_typedef_symbol(symbol_t *symbol)
1939 const declaration_t *const declaration =
1940 get_declaration(symbol, NAMESPACE_NORMAL);
1942 declaration != NULL &&
1943 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1946 static type_t *parse_typeof(void)
1953 add_anchor_token(')');
1955 expression_t *expression = NULL;
1958 switch(token.type) {
1959 case T___extension__:
1960 /* this can be a prefix to a typename or an expression */
1961 /* we simply eat it now. */
1964 } while(token.type == T___extension__);
1968 if(is_typedef_symbol(token.v.symbol)) {
1969 type = parse_typename();
1971 expression = parse_expression();
1972 type = expression->base.type;
1977 type = parse_typename();
1981 expression = parse_expression();
1982 type = expression->base.type;
1986 rem_anchor_token(')');
1989 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1990 typeof_type->typeoft.expression = expression;
1991 typeof_type->typeoft.typeof_type = type;
1999 SPECIFIER_SIGNED = 1 << 0,
2000 SPECIFIER_UNSIGNED = 1 << 1,
2001 SPECIFIER_LONG = 1 << 2,
2002 SPECIFIER_INT = 1 << 3,
2003 SPECIFIER_DOUBLE = 1 << 4,
2004 SPECIFIER_CHAR = 1 << 5,
2005 SPECIFIER_SHORT = 1 << 6,
2006 SPECIFIER_LONG_LONG = 1 << 7,
2007 SPECIFIER_FLOAT = 1 << 8,
2008 SPECIFIER_BOOL = 1 << 9,
2009 SPECIFIER_VOID = 1 << 10,
2010 SPECIFIER_INT8 = 1 << 11,
2011 SPECIFIER_INT16 = 1 << 12,
2012 SPECIFIER_INT32 = 1 << 13,
2013 SPECIFIER_INT64 = 1 << 14,
2014 SPECIFIER_INT128 = 1 << 15,
2015 #ifdef PROVIDE_COMPLEX
2016 SPECIFIER_COMPLEX = 1 << 16,
2017 SPECIFIER_IMAGINARY = 1 << 17,
2021 static type_t *create_builtin_type(symbol_t *const symbol,
2022 type_t *const real_type)
2024 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
2025 type->builtin.symbol = symbol;
2026 type->builtin.real_type = real_type;
2028 type_t *result = typehash_insert(type);
2029 if (type != result) {
2036 static type_t *get_typedef_type(symbol_t *symbol)
2038 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2039 if(declaration == NULL
2040 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2043 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
2044 type->typedeft.declaration = declaration;
2050 * check for the allowed MS alignment values.
2052 static bool check_elignment_value(long long intvalue) {
2053 if(intvalue < 1 || intvalue > 8192) {
2054 errorf(HERE, "illegal alignment value");
2057 unsigned v = (unsigned)intvalue;
2058 for(unsigned i = 1; i <= 8192; i += i) {
2062 errorf(HERE, "alignment must be power of two");
2066 #define DET_MOD(name, tag) do { \
2067 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2068 *modifiers |= tag; \
2071 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2073 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2076 if(token.type == T_restrict) {
2078 DET_MOD(restrict, DM_RESTRICT);
2080 } else if(token.type != T_IDENTIFIER)
2082 symbol_t *symbol = token.v.symbol;
2083 if(symbol == sym_align) {
2086 if(token.type != T_INTEGER)
2088 if(check_elignment_value(token.v.intvalue)) {
2089 if(specifiers->alignment != 0)
2090 warningf(HERE, "align used more than once");
2091 specifiers->alignment = (unsigned char)token.v.intvalue;
2095 } else if(symbol == sym_allocate) {
2098 if(token.type != T_IDENTIFIER)
2100 (void)token.v.symbol;
2102 } else if(symbol == sym_dllimport) {
2104 DET_MOD(dllimport, DM_DLLIMPORT);
2105 } else if(symbol == sym_dllexport) {
2107 DET_MOD(dllexport, DM_DLLEXPORT);
2108 } else if(symbol == sym_thread) {
2110 DET_MOD(thread, DM_THREAD);
2111 } else if(symbol == sym_naked) {
2113 DET_MOD(naked, DM_NAKED);
2114 } else if(symbol == sym_noinline) {
2116 DET_MOD(noinline, DM_NOINLINE);
2117 } else if(symbol == sym_noreturn) {
2119 DET_MOD(noreturn, DM_NORETURN);
2120 } else if(symbol == sym_nothrow) {
2122 DET_MOD(nothrow, DM_NOTHROW);
2123 } else if(symbol == sym_novtable) {
2125 DET_MOD(novtable, DM_NOVTABLE);
2126 } else if(symbol == sym_property) {
2130 bool is_get = false;
2131 if(token.type != T_IDENTIFIER)
2133 if(token.v.symbol == sym_get) {
2135 } else if(token.v.symbol == sym_put) {
2137 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2142 if(token.type != T_IDENTIFIER)
2145 if(specifiers->get_property_sym != NULL) {
2146 errorf(HERE, "get property name already specified");
2148 specifiers->get_property_sym = token.v.symbol;
2151 if(specifiers->put_property_sym != NULL) {
2152 errorf(HERE, "put property name already specified");
2154 specifiers->put_property_sym = token.v.symbol;
2158 if(token.type == ',') {
2165 } else if(symbol == sym_selectany) {
2167 DET_MOD(selectany, DM_SELECTANY);
2168 } else if(symbol == sym_uuid) {
2171 if(token.type != T_STRING_LITERAL)
2175 } else if(symbol == sym_deprecated) {
2177 if(specifiers->deprecated != 0)
2178 warningf(HERE, "deprecated used more than once");
2179 specifiers->deprecated = 1;
2180 if(token.type == '(') {
2182 if(token.type == T_STRING_LITERAL) {
2183 specifiers->deprecated_string = token.v.string.begin;
2186 errorf(HERE, "string literal expected");
2190 } else if(symbol == sym_noalias) {
2192 DET_MOD(noalias, DM_NOALIAS);
2194 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2196 if(token.type == '(')
2200 if (token.type == ',')
2207 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2209 type_t *type = NULL;
2210 unsigned type_qualifiers = 0;
2211 unsigned type_specifiers = 0;
2214 specifiers->source_position = token.source_position;
2217 switch(token.type) {
2220 #define MATCH_STORAGE_CLASS(token, class) \
2222 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2223 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2225 specifiers->declared_storage_class = class; \
2229 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2230 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2231 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2232 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2233 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2238 add_anchor_token(')');
2239 parse_microsoft_extended_decl_modifier(specifiers);
2240 rem_anchor_token(')');
2245 switch (specifiers->declared_storage_class) {
2246 case STORAGE_CLASS_NONE:
2247 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2250 case STORAGE_CLASS_EXTERN:
2251 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2254 case STORAGE_CLASS_STATIC:
2255 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2259 errorf(HERE, "multiple storage classes in declaration specifiers");
2265 /* type qualifiers */
2266 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2268 type_qualifiers |= qualifier; \
2272 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2273 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2274 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2275 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2276 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2277 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2278 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2279 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2281 case T___extension__:
2286 /* type specifiers */
2287 #define MATCH_SPECIFIER(token, specifier, name) \
2290 if(type_specifiers & specifier) { \
2291 errorf(HERE, "multiple " name " type specifiers given"); \
2293 type_specifiers |= specifier; \
2297 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2298 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2299 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2300 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2301 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2302 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2303 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2304 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2305 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2306 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2307 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2308 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2309 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2310 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2311 #ifdef PROVIDE_COMPLEX
2312 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2313 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2315 case T__forceinline:
2316 /* only in microsoft mode */
2317 specifiers->decl_modifiers |= DM_FORCEINLINE;
2321 specifiers->is_inline = true;
2326 if(type_specifiers & SPECIFIER_LONG_LONG) {
2327 errorf(HERE, "multiple type specifiers given");
2328 } else if(type_specifiers & SPECIFIER_LONG) {
2329 type_specifiers |= SPECIFIER_LONG_LONG;
2331 type_specifiers |= SPECIFIER_LONG;
2336 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2338 type->compound.declaration = parse_compound_type_specifier(true);
2342 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2344 type->compound.declaration = parse_compound_type_specifier(false);
2348 type = parse_enum_specifier();
2351 type = parse_typeof();
2353 case T___builtin_va_list:
2354 type = duplicate_type(type_valist);
2358 case T___attribute__:
2362 case T_IDENTIFIER: {
2363 /* only parse identifier if we haven't found a type yet */
2364 if(type != NULL || type_specifiers != 0)
2365 goto finish_specifiers;
2367 type_t *typedef_type = get_typedef_type(token.v.symbol);
2369 if(typedef_type == NULL)
2370 goto finish_specifiers;
2373 type = typedef_type;
2377 /* function specifier */
2379 goto finish_specifiers;
2386 atomic_type_kind_t atomic_type;
2388 /* match valid basic types */
2389 switch(type_specifiers) {
2390 case SPECIFIER_VOID:
2391 atomic_type = ATOMIC_TYPE_VOID;
2393 case SPECIFIER_CHAR:
2394 atomic_type = ATOMIC_TYPE_CHAR;
2396 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2397 atomic_type = ATOMIC_TYPE_SCHAR;
2399 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2400 atomic_type = ATOMIC_TYPE_UCHAR;
2402 case SPECIFIER_SHORT:
2403 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2404 case SPECIFIER_SHORT | SPECIFIER_INT:
2405 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2406 atomic_type = ATOMIC_TYPE_SHORT;
2408 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2409 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2410 atomic_type = ATOMIC_TYPE_USHORT;
2413 case SPECIFIER_SIGNED:
2414 case SPECIFIER_SIGNED | SPECIFIER_INT:
2415 atomic_type = ATOMIC_TYPE_INT;
2417 case SPECIFIER_UNSIGNED:
2418 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2419 atomic_type = ATOMIC_TYPE_UINT;
2421 case SPECIFIER_LONG:
2422 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2423 case SPECIFIER_LONG | SPECIFIER_INT:
2424 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2425 atomic_type = ATOMIC_TYPE_LONG;
2427 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2428 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2429 atomic_type = ATOMIC_TYPE_ULONG;
2431 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2432 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2433 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2434 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2436 atomic_type = ATOMIC_TYPE_LONGLONG;
2438 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2439 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2441 atomic_type = ATOMIC_TYPE_ULONGLONG;
2444 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
2445 atomic_type = unsigned_int8_type_kind;
2448 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
2449 atomic_type = unsigned_int16_type_kind;
2452 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
2453 atomic_type = unsigned_int32_type_kind;
2456 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
2457 atomic_type = unsigned_int64_type_kind;
2460 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
2461 atomic_type = unsigned_int128_type_kind;
2464 case SPECIFIER_INT8:
2465 case SPECIFIER_SIGNED | SPECIFIER_INT8:
2466 atomic_type = int8_type_kind;
2469 case SPECIFIER_INT16:
2470 case SPECIFIER_SIGNED | SPECIFIER_INT16:
2471 atomic_type = int16_type_kind;
2474 case SPECIFIER_INT32:
2475 case SPECIFIER_SIGNED | SPECIFIER_INT32:
2476 atomic_type = int32_type_kind;
2479 case SPECIFIER_INT64:
2480 case SPECIFIER_SIGNED | SPECIFIER_INT64:
2481 atomic_type = int64_type_kind;
2484 case SPECIFIER_INT128:
2485 case SPECIFIER_SIGNED | SPECIFIER_INT128:
2486 atomic_type = int128_type_kind;
2489 case SPECIFIER_FLOAT:
2490 atomic_type = ATOMIC_TYPE_FLOAT;
2492 case SPECIFIER_DOUBLE:
2493 atomic_type = ATOMIC_TYPE_DOUBLE;
2495 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2496 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2498 case SPECIFIER_BOOL:
2499 atomic_type = ATOMIC_TYPE_BOOL;
2501 #ifdef PROVIDE_COMPLEX
2502 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2503 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2505 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2506 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2508 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2509 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2511 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2512 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2514 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2515 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2517 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2518 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2522 /* invalid specifier combination, give an error message */
2523 if(type_specifiers == 0) {
2524 if (! strict_mode) {
2525 if (warning.implicit_int) {
2526 warningf(HERE, "no type specifiers in declaration, using 'int'");
2528 atomic_type = ATOMIC_TYPE_INT;
2531 errorf(HERE, "no type specifiers given in declaration");
2533 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2534 (type_specifiers & SPECIFIER_UNSIGNED)) {
2535 errorf(HERE, "signed and unsigned specifiers gives");
2536 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2537 errorf(HERE, "only integer types can be signed or unsigned");
2539 errorf(HERE, "multiple datatypes in declaration");
2541 atomic_type = ATOMIC_TYPE_INVALID;
2544 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2545 type->atomic.akind = atomic_type;
2548 if(type_specifiers != 0) {
2549 errorf(HERE, "multiple datatypes in declaration");
2553 type->base.qualifiers = type_qualifiers;
2554 /* FIXME: check type qualifiers here */
2556 type_t *result = typehash_insert(type);
2557 if(newtype && result != type) {
2561 specifiers->type = result;
2566 static type_qualifiers_t parse_type_qualifiers(void)
2568 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2571 switch(token.type) {
2572 /* type qualifiers */
2573 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2574 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2575 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2576 /* microsoft extended type modifiers */
2577 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2578 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2579 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2580 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2581 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2584 return type_qualifiers;
2589 static declaration_t *parse_identifier_list(void)
2591 declaration_t *declarations = NULL;
2592 declaration_t *last_declaration = NULL;
2594 declaration_t *const declaration = allocate_declaration_zero();
2595 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2596 declaration->source_position = token.source_position;
2597 declaration->symbol = token.v.symbol;
2600 if(last_declaration != NULL) {
2601 last_declaration->next = declaration;
2603 declarations = declaration;
2605 last_declaration = declaration;
2607 if(token.type != ',')
2610 } while(token.type == T_IDENTIFIER);
2612 return declarations;
2615 static void semantic_parameter(declaration_t *declaration)
2617 /* TODO: improve error messages */
2619 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2620 errorf(HERE, "typedef not allowed in parameter list");
2621 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2622 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2623 errorf(HERE, "parameter may only have none or register storage class");
2626 type_t *const orig_type = declaration->type;
2627 type_t * type = skip_typeref(orig_type);
2629 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2630 * into a pointer. § 6.7.5.3 (7) */
2631 if (is_type_array(type)) {
2632 type_t *const element_type = type->array.element_type;
2634 type = make_pointer_type(element_type, type->base.qualifiers);
2636 declaration->type = type;
2639 if(is_type_incomplete(type)) {
2640 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2641 orig_type, declaration->symbol);
2645 static declaration_t *parse_parameter(void)
2647 declaration_specifiers_t specifiers;
2648 memset(&specifiers, 0, sizeof(specifiers));
2650 parse_declaration_specifiers(&specifiers);
2652 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2654 semantic_parameter(declaration);
2659 static declaration_t *parse_parameters(function_type_t *type)
2661 if(token.type == T_IDENTIFIER) {
2662 symbol_t *symbol = token.v.symbol;
2663 if(!is_typedef_symbol(symbol)) {
2664 type->kr_style_parameters = true;
2665 return parse_identifier_list();
2669 if(token.type == ')') {
2670 type->unspecified_parameters = 1;
2673 if(token.type == T_void && look_ahead(1)->type == ')') {
2678 declaration_t *declarations = NULL;
2679 declaration_t *declaration;
2680 declaration_t *last_declaration = NULL;
2681 function_parameter_t *parameter;
2682 function_parameter_t *last_parameter = NULL;
2685 switch(token.type) {
2689 return declarations;
2692 case T___extension__:
2694 declaration = parse_parameter();
2696 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2697 memset(parameter, 0, sizeof(parameter[0]));
2698 parameter->type = declaration->type;
2700 if(last_parameter != NULL) {
2701 last_declaration->next = declaration;
2702 last_parameter->next = parameter;
2704 type->parameters = parameter;
2705 declarations = declaration;
2707 last_parameter = parameter;
2708 last_declaration = declaration;
2712 return declarations;
2714 if(token.type != ',')
2715 return declarations;
2725 } construct_type_kind_t;
2727 typedef struct construct_type_t construct_type_t;
2728 struct construct_type_t {
2729 construct_type_kind_t kind;
2730 construct_type_t *next;
2733 typedef struct parsed_pointer_t parsed_pointer_t;
2734 struct parsed_pointer_t {
2735 construct_type_t construct_type;
2736 type_qualifiers_t type_qualifiers;
2739 typedef struct construct_function_type_t construct_function_type_t;
2740 struct construct_function_type_t {
2741 construct_type_t construct_type;
2742 type_t *function_type;
2745 typedef struct parsed_array_t parsed_array_t;
2746 struct parsed_array_t {
2747 construct_type_t construct_type;
2748 type_qualifiers_t type_qualifiers;
2754 typedef struct construct_base_type_t construct_base_type_t;
2755 struct construct_base_type_t {
2756 construct_type_t construct_type;
2760 static construct_type_t *parse_pointer_declarator(void)
2764 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2765 memset(pointer, 0, sizeof(pointer[0]));
2766 pointer->construct_type.kind = CONSTRUCT_POINTER;
2767 pointer->type_qualifiers = parse_type_qualifiers();
2769 return (construct_type_t*) pointer;
2772 static construct_type_t *parse_array_declarator(void)
2775 add_anchor_token(']');
2777 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2778 memset(array, 0, sizeof(array[0]));
2779 array->construct_type.kind = CONSTRUCT_ARRAY;
2781 if(token.type == T_static) {
2782 array->is_static = true;
2786 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2787 if(type_qualifiers != 0) {
2788 if(token.type == T_static) {
2789 array->is_static = true;
2793 array->type_qualifiers = type_qualifiers;
2795 if(token.type == '*' && look_ahead(1)->type == ']') {
2796 array->is_variable = true;
2798 } else if(token.type != ']') {
2799 array->size = parse_assignment_expression();
2802 rem_anchor_token(']');
2805 return (construct_type_t*) array;
2810 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2813 add_anchor_token(')');
2816 if(declaration != NULL) {
2817 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2819 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2822 declaration_t *parameters = parse_parameters(&type->function);
2823 if(declaration != NULL) {
2824 declaration->scope.declarations = parameters;
2827 construct_function_type_t *construct_function_type =
2828 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2829 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2830 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2831 construct_function_type->function_type = type;
2833 rem_anchor_token(')');
2837 return (construct_type_t*) construct_function_type;
2840 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2841 bool may_be_abstract)
2843 /* construct a single linked list of construct_type_t's which describe
2844 * how to construct the final declarator type */
2845 construct_type_t *first = NULL;
2846 construct_type_t *last = NULL;
2849 while(token.type == '*') {
2850 construct_type_t *type = parse_pointer_declarator();
2861 /* TODO: find out if this is correct */
2864 construct_type_t *inner_types = NULL;
2866 switch(token.type) {
2868 if(declaration == NULL) {
2869 errorf(HERE, "no identifier expected in typename");
2871 declaration->symbol = token.v.symbol;
2872 declaration->source_position = token.source_position;
2878 add_anchor_token(')');
2879 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2880 rem_anchor_token(')');
2886 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2887 /* avoid a loop in the outermost scope, because eat_statement doesn't
2889 if(token.type == '}' && current_function == NULL) {
2897 construct_type_t *p = last;
2900 construct_type_t *type;
2901 switch(token.type) {
2903 type = parse_function_declarator(declaration);
2906 type = parse_array_declarator();
2909 goto declarator_finished;
2912 /* insert in the middle of the list (behind p) */
2914 type->next = p->next;
2925 declarator_finished:
2928 /* append inner_types at the end of the list, we don't to set last anymore
2929 * as it's not needed anymore */
2931 assert(first == NULL);
2932 first = inner_types;
2934 last->next = inner_types;
2942 static type_t *construct_declarator_type(construct_type_t *construct_list,
2945 construct_type_t *iter = construct_list;
2946 for( ; iter != NULL; iter = iter->next) {
2947 switch(iter->kind) {
2948 case CONSTRUCT_INVALID:
2949 internal_errorf(HERE, "invalid type construction found");
2950 case CONSTRUCT_FUNCTION: {
2951 construct_function_type_t *construct_function_type
2952 = (construct_function_type_t*) iter;
2954 type_t *function_type = construct_function_type->function_type;
2956 function_type->function.return_type = type;
2958 type_t *skipped_return_type = skip_typeref(type);
2959 if (is_type_function(skipped_return_type)) {
2960 errorf(HERE, "function returning function is not allowed");
2961 type = type_error_type;
2962 } else if (is_type_array(skipped_return_type)) {
2963 errorf(HERE, "function returning array is not allowed");
2964 type = type_error_type;
2966 type = function_type;
2971 case CONSTRUCT_POINTER: {
2972 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2973 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2974 pointer_type->pointer.points_to = type;
2975 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2977 type = pointer_type;
2981 case CONSTRUCT_ARRAY: {
2982 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2983 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2985 expression_t *size_expression = parsed_array->size;
2986 if(size_expression != NULL) {
2988 = create_implicit_cast(size_expression, type_size_t);
2991 array_type->base.qualifiers = parsed_array->type_qualifiers;
2992 array_type->array.element_type = type;
2993 array_type->array.is_static = parsed_array->is_static;
2994 array_type->array.is_variable = parsed_array->is_variable;
2995 array_type->array.size_expression = size_expression;
2997 if(size_expression != NULL) {
2998 if(is_constant_expression(size_expression)) {
2999 array_type->array.size_constant = true;
3000 array_type->array.size
3001 = fold_constant(size_expression);
3003 array_type->array.is_vla = true;
3007 type_t *skipped_type = skip_typeref(type);
3008 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3009 errorf(HERE, "array of void is not allowed");
3010 type = type_error_type;
3018 type_t *hashed_type = typehash_insert(type);
3019 if(hashed_type != type) {
3020 /* the function type was constructed earlier freeing it here will
3021 * destroy other types... */
3022 if(iter->kind != CONSTRUCT_FUNCTION) {
3032 static declaration_t *parse_declarator(
3033 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3035 declaration_t *const declaration = allocate_declaration_zero();
3036 declaration->declared_storage_class = specifiers->declared_storage_class;
3037 declaration->modifiers = specifiers->decl_modifiers;
3038 declaration->deprecated = specifiers->deprecated;
3039 declaration->deprecated_string = specifiers->deprecated_string;
3040 declaration->get_property_sym = specifiers->get_property_sym;
3041 declaration->put_property_sym = specifiers->put_property_sym;
3042 declaration->is_inline = specifiers->is_inline;
3044 declaration->storage_class = specifiers->declared_storage_class;
3045 if(declaration->storage_class == STORAGE_CLASS_NONE
3046 && scope != global_scope) {
3047 declaration->storage_class = STORAGE_CLASS_AUTO;
3050 if(specifiers->alignment != 0) {
3051 /* TODO: add checks here */
3052 declaration->alignment = specifiers->alignment;
3055 construct_type_t *construct_type
3056 = parse_inner_declarator(declaration, may_be_abstract);
3057 type_t *const type = specifiers->type;
3058 declaration->type = construct_declarator_type(construct_type, type);
3060 if(construct_type != NULL) {
3061 obstack_free(&temp_obst, construct_type);
3067 static type_t *parse_abstract_declarator(type_t *base_type)
3069 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3071 type_t *result = construct_declarator_type(construct_type, base_type);
3072 if(construct_type != NULL) {
3073 obstack_free(&temp_obst, construct_type);
3079 static declaration_t *append_declaration(declaration_t* const declaration)
3081 if (last_declaration != NULL) {
3082 last_declaration->next = declaration;
3084 scope->declarations = declaration;
3086 last_declaration = declaration;
3091 * Check if the declaration of main is suspicious. main should be a
3092 * function with external linkage, returning int, taking either zero
3093 * arguments, two, or three arguments of appropriate types, ie.
3095 * int main([ int argc, char **argv [, char **env ] ]).
3097 * @param decl the declaration to check
3098 * @param type the function type of the declaration
3100 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3102 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3103 warningf(decl->source_position, "'main' is normally a non-static function");
3105 if (skip_typeref(func_type->return_type) != type_int) {
3106 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
3108 const function_parameter_t *parm = func_type->parameters;
3110 type_t *const first_type = parm->type;
3111 if (!types_compatible(skip_typeref(first_type), type_int)) {
3112 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
3116 type_t *const second_type = parm->type;
3117 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3118 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
3122 type_t *const third_type = parm->type;
3123 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3124 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
3128 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3132 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3138 * Check if a symbol is the equal to "main".
3140 static bool is_sym_main(const symbol_t *const sym)
3142 return strcmp(sym->string, "main") == 0;
3145 static declaration_t *internal_record_declaration(
3146 declaration_t *const declaration,
3147 const bool is_function_definition)
3149 const symbol_t *const symbol = declaration->symbol;
3150 const namespace_t namespc = (namespace_t)declaration->namespc;
3152 type_t *const orig_type = declaration->type;
3153 type_t *const type = skip_typeref(orig_type);
3154 if (is_type_function(type) &&
3155 type->function.unspecified_parameters &&
3156 warning.strict_prototypes) {
3157 warningf(declaration->source_position,
3158 "function declaration '%#T' is not a prototype",
3159 orig_type, declaration->symbol);
3162 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3163 check_type_of_main(declaration, &type->function);
3166 assert(declaration->symbol != NULL);
3167 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3169 assert(declaration != previous_declaration);
3170 if (previous_declaration != NULL) {
3171 if (previous_declaration->parent_scope == scope) {
3172 /* can happen for K&R style declarations */
3173 if(previous_declaration->type == NULL) {
3174 previous_declaration->type = declaration->type;
3177 const type_t *prev_type = skip_typeref(previous_declaration->type);
3178 if (!types_compatible(type, prev_type)) {
3179 errorf(declaration->source_position,
3180 "declaration '%#T' is incompatible with "
3181 "previous declaration '%#T'",
3182 orig_type, symbol, previous_declaration->type, symbol);
3183 errorf(previous_declaration->source_position,
3184 "previous declaration of '%Y' was here", symbol);
3186 unsigned old_storage_class = previous_declaration->storage_class;
3187 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3188 errorf(declaration->source_position, "redeclaration of enum entry '%Y'", symbol);
3189 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
3190 return previous_declaration;
3193 unsigned new_storage_class = declaration->storage_class;
3195 if(is_type_incomplete(prev_type)) {
3196 previous_declaration->type = type;
3200 /* pretend no storage class means extern for function
3201 * declarations (except if the previous declaration is neither
3202 * none nor extern) */
3203 if (is_type_function(type)) {
3204 switch (old_storage_class) {
3205 case STORAGE_CLASS_NONE:
3206 old_storage_class = STORAGE_CLASS_EXTERN;
3208 case STORAGE_CLASS_EXTERN:
3209 if (is_function_definition) {
3210 if (warning.missing_prototypes &&
3211 prev_type->function.unspecified_parameters &&
3212 !is_sym_main(symbol)) {
3213 warningf(declaration->source_position,
3214 "no previous prototype for '%#T'",
3217 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3218 new_storage_class = STORAGE_CLASS_EXTERN;
3226 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3227 new_storage_class == STORAGE_CLASS_EXTERN) {
3228 warn_redundant_declaration:
3229 if (warning.redundant_decls) {
3230 warningf(declaration->source_position,
3231 "redundant declaration for '%Y'", symbol);
3232 warningf(previous_declaration->source_position,
3233 "previous declaration of '%Y' was here",
3236 } else if (current_function == NULL) {
3237 if (old_storage_class != STORAGE_CLASS_STATIC &&
3238 new_storage_class == STORAGE_CLASS_STATIC) {
3239 errorf(declaration->source_position,
3240 "static declaration of '%Y' follows non-static declaration",
3242 errorf(previous_declaration->source_position,
3243 "previous declaration of '%Y' was here", symbol);
3245 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3246 goto warn_redundant_declaration;
3248 if (new_storage_class == STORAGE_CLASS_NONE) {
3249 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3250 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3254 if (old_storage_class == new_storage_class) {
3255 errorf(declaration->source_position,
3256 "redeclaration of '%Y'", symbol);
3258 errorf(declaration->source_position,
3259 "redeclaration of '%Y' with different linkage",
3262 errorf(previous_declaration->source_position,
3263 "previous declaration of '%Y' was here", symbol);
3266 return previous_declaration;
3268 } else if (is_function_definition) {
3269 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3270 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3271 warningf(declaration->source_position,
3272 "no previous prototype for '%#T'", orig_type, symbol);
3273 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3274 warningf(declaration->source_position,
3275 "no previous declaration for '%#T'", orig_type,
3279 } else if (warning.missing_declarations &&
3280 scope == global_scope &&
3281 !is_type_function(type) && (
3282 declaration->storage_class == STORAGE_CLASS_NONE ||
3283 declaration->storage_class == STORAGE_CLASS_THREAD
3285 warningf(declaration->source_position,
3286 "no previous declaration for '%#T'", orig_type, symbol);
3289 assert(declaration->parent_scope == NULL);
3290 assert(scope != NULL);
3292 declaration->parent_scope = scope;
3294 environment_push(declaration);
3295 return append_declaration(declaration);
3298 static declaration_t *record_declaration(declaration_t *declaration)
3300 return internal_record_declaration(declaration, false);
3303 static declaration_t *record_function_definition(declaration_t *declaration)
3305 return internal_record_declaration(declaration, true);
3308 static void parser_error_multiple_definition(declaration_t *declaration,
3309 const source_position_t source_position)
3311 errorf(source_position, "multiple definition of symbol '%Y'",
3312 declaration->symbol);
3313 errorf(declaration->source_position,
3314 "this is the location of the previous definition.");
3317 static bool is_declaration_specifier(const token_t *token,
3318 bool only_type_specifiers)
3320 switch(token->type) {
3324 return is_typedef_symbol(token->v.symbol);
3326 case T___extension__:
3329 return !only_type_specifiers;
3336 static void parse_init_declarator_rest(declaration_t *declaration)
3340 type_t *orig_type = declaration->type;
3341 type_t *type = skip_typeref(orig_type);
3343 if(declaration->init.initializer != NULL) {
3344 parser_error_multiple_definition(declaration, token.source_position);
3347 bool must_be_constant = false;
3348 if(declaration->storage_class == STORAGE_CLASS_STATIC
3349 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3350 || declaration->parent_scope == global_scope) {
3351 must_be_constant = true;
3354 parse_initializer_env_t env;
3355 env.type = orig_type;
3356 env.must_be_constant = must_be_constant;
3357 env.declaration = declaration;
3359 initializer_t *initializer = parse_initializer(&env);
3361 if(env.type != orig_type) {
3362 orig_type = env.type;
3363 type = skip_typeref(orig_type);
3364 declaration->type = env.type;
3367 if(is_type_function(type)) {
3368 errorf(declaration->source_position,
3369 "initializers not allowed for function types at declator '%Y' (type '%T')",
3370 declaration->symbol, orig_type);
3372 declaration->init.initializer = initializer;
3376 /* parse rest of a declaration without any declarator */
3377 static void parse_anonymous_declaration_rest(
3378 const declaration_specifiers_t *specifiers,
3379 parsed_declaration_func finished_declaration)
3383 declaration_t *const declaration = allocate_declaration_zero();
3384 declaration->type = specifiers->type;
3385 declaration->declared_storage_class = specifiers->declared_storage_class;
3386 declaration->source_position = specifiers->source_position;
3387 declaration->modifiers = specifiers->decl_modifiers;
3389 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3390 warningf(declaration->source_position, "useless storage class in empty declaration");
3392 declaration->storage_class = STORAGE_CLASS_NONE;
3394 type_t *type = declaration->type;
3395 switch (type->kind) {
3396 case TYPE_COMPOUND_STRUCT:
3397 case TYPE_COMPOUND_UNION: {
3398 if (type->compound.declaration->symbol == NULL) {
3399 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
3408 warningf(declaration->source_position, "empty declaration");
3412 finished_declaration(declaration);
3415 static void parse_declaration_rest(declaration_t *ndeclaration,
3416 const declaration_specifiers_t *specifiers,
3417 parsed_declaration_func finished_declaration)
3419 add_anchor_token(';');
3420 add_anchor_token('=');
3421 add_anchor_token(',');
3423 declaration_t *declaration = finished_declaration(ndeclaration);
3425 type_t *orig_type = declaration->type;
3426 type_t *type = skip_typeref(orig_type);
3428 if (type->kind != TYPE_FUNCTION &&
3429 declaration->is_inline &&
3430 is_type_valid(type)) {
3431 warningf(declaration->source_position,
3432 "variable '%Y' declared 'inline'\n", declaration->symbol);
3435 if(token.type == '=') {
3436 parse_init_declarator_rest(declaration);
3439 if(token.type != ',')
3443 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
3448 rem_anchor_token(';');
3449 rem_anchor_token('=');
3450 rem_anchor_token(',');
3453 static declaration_t *finished_kr_declaration(declaration_t *declaration)
3455 symbol_t *symbol = declaration->symbol;
3456 if(symbol == NULL) {
3457 errorf(HERE, "anonymous declaration not valid as function parameter");
3460 namespace_t namespc = (namespace_t) declaration->namespc;
3461 if(namespc != NAMESPACE_NORMAL) {
3462 return record_declaration(declaration);
3465 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3466 if(previous_declaration == NULL ||
3467 previous_declaration->parent_scope != scope) {
3468 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3473 if(previous_declaration->type == NULL) {
3474 previous_declaration->type = declaration->type;
3475 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3476 previous_declaration->storage_class = declaration->storage_class;
3477 previous_declaration->parent_scope = scope;
3478 return previous_declaration;
3480 return record_declaration(declaration);
3484 static void parse_declaration(parsed_declaration_func finished_declaration)
3486 declaration_specifiers_t specifiers;
3487 memset(&specifiers, 0, sizeof(specifiers));
3488 parse_declaration_specifiers(&specifiers);
3490 if(token.type == ';') {
3491 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3493 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3494 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3498 static void parse_kr_declaration_list(declaration_t *declaration)
3500 type_t *type = skip_typeref(declaration->type);
3501 if(!is_type_function(type))
3504 if(!type->function.kr_style_parameters)
3507 /* push function parameters */
3508 int top = environment_top();
3509 scope_t *last_scope = scope;
3510 set_scope(&declaration->scope);
3512 declaration_t *parameter = declaration->scope.declarations;
3513 for( ; parameter != NULL; parameter = parameter->next) {
3514 assert(parameter->parent_scope == NULL);
3515 parameter->parent_scope = scope;
3516 environment_push(parameter);
3519 /* parse declaration list */
3520 while(is_declaration_specifier(&token, false)) {
3521 parse_declaration(finished_kr_declaration);
3524 /* pop function parameters */
3525 assert(scope == &declaration->scope);
3526 set_scope(last_scope);
3527 environment_pop_to(top);
3529 /* update function type */
3530 type_t *new_type = duplicate_type(type);
3531 new_type->function.kr_style_parameters = false;
3533 function_parameter_t *parameters = NULL;
3534 function_parameter_t *last_parameter = NULL;
3536 declaration_t *parameter_declaration = declaration->scope.declarations;
3537 for( ; parameter_declaration != NULL;
3538 parameter_declaration = parameter_declaration->next) {
3539 type_t *parameter_type = parameter_declaration->type;
3540 if(parameter_type == NULL) {
3542 errorf(HERE, "no type specified for function parameter '%Y'",
3543 parameter_declaration->symbol);
3545 if (warning.implicit_int) {
3546 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3547 parameter_declaration->symbol);
3549 parameter_type = type_int;
3550 parameter_declaration->type = parameter_type;
3554 semantic_parameter(parameter_declaration);
3555 parameter_type = parameter_declaration->type;
3557 function_parameter_t *function_parameter
3558 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3559 memset(function_parameter, 0, sizeof(function_parameter[0]));
3561 function_parameter->type = parameter_type;
3562 if(last_parameter != NULL) {
3563 last_parameter->next = function_parameter;
3565 parameters = function_parameter;
3567 last_parameter = function_parameter;
3569 new_type->function.parameters = parameters;
3571 type = typehash_insert(new_type);
3572 if(type != new_type) {
3573 obstack_free(type_obst, new_type);
3576 declaration->type = type;
3579 static bool first_err = true;
3582 * When called with first_err set, prints the name of the current function,
3585 static void print_in_function(void) {
3588 diagnosticf("%s: In function '%Y':\n",
3589 current_function->source_position.input_name,
3590 current_function->symbol);
3595 * Check if all labels are defined in the current function.
3596 * Check if all labels are used in the current function.
3598 static void check_labels(void)
3600 for (const goto_statement_t *goto_statement = goto_first;
3601 goto_statement != NULL;
3602 goto_statement = goto_statement->next) {
3603 declaration_t *label = goto_statement->label;
3606 if (label->source_position.input_name == NULL) {
3607 print_in_function();
3608 errorf(goto_statement->base.source_position,
3609 "label '%Y' used but not defined", label->symbol);
3612 goto_first = goto_last = NULL;
3614 if (warning.unused_label) {
3615 for (const label_statement_t *label_statement = label_first;
3616 label_statement != NULL;
3617 label_statement = label_statement->next) {
3618 const declaration_t *label = label_statement->label;
3620 if (! label->used) {
3621 print_in_function();
3622 warningf(label_statement->base.source_position,
3623 "label '%Y' defined but not used", label->symbol);
3627 label_first = label_last = NULL;
3631 * Check declarations of current_function for unused entities.
3633 static void check_declarations(void)
3635 if (warning.unused_parameter) {
3636 const scope_t *scope = ¤t_function->scope;
3638 const declaration_t *parameter = scope->declarations;
3639 for (; parameter != NULL; parameter = parameter->next) {
3640 if (! parameter->used) {
3641 print_in_function();
3642 warningf(parameter->source_position,
3643 "unused parameter '%Y'", parameter->symbol);
3647 if (warning.unused_variable) {
3651 static void parse_external_declaration(void)
3653 /* function-definitions and declarations both start with declaration
3655 declaration_specifiers_t specifiers;
3656 memset(&specifiers, 0, sizeof(specifiers));
3658 add_anchor_token(';');
3659 parse_declaration_specifiers(&specifiers);
3660 rem_anchor_token(';');
3662 /* must be a declaration */
3663 if(token.type == ';') {
3664 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3668 add_anchor_token(',');
3669 add_anchor_token('=');
3670 rem_anchor_token(';');
3672 /* declarator is common to both function-definitions and declarations */
3673 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3675 rem_anchor_token(',');
3676 rem_anchor_token('=');
3677 rem_anchor_token(';');
3679 /* must be a declaration */
3680 if(token.type == ',' || token.type == '=' || token.type == ';') {
3681 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3685 /* must be a function definition */
3686 parse_kr_declaration_list(ndeclaration);
3688 if(token.type != '{') {
3689 parse_error_expected("while parsing function definition", '{', 0);
3690 eat_until_matching_token(';');
3694 type_t *type = ndeclaration->type;
3696 /* note that we don't skip typerefs: the standard doesn't allow them here
3697 * (so we can't use is_type_function here) */
3698 if(type->kind != TYPE_FUNCTION) {
3699 if (is_type_valid(type)) {
3700 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3701 type, ndeclaration->symbol);
3707 /* § 6.7.5.3 (14) a function definition with () means no
3708 * parameters (and not unspecified parameters) */
3709 if(type->function.unspecified_parameters) {
3710 type_t *duplicate = duplicate_type(type);
3711 duplicate->function.unspecified_parameters = false;
3713 type = typehash_insert(duplicate);
3714 if(type != duplicate) {
3715 obstack_free(type_obst, duplicate);
3717 ndeclaration->type = type;
3720 declaration_t *const declaration = record_function_definition(ndeclaration);
3721 if(ndeclaration != declaration) {
3722 declaration->scope = ndeclaration->scope;
3724 type = skip_typeref(declaration->type);
3726 /* push function parameters and switch scope */
3727 int top = environment_top();
3728 scope_t *last_scope = scope;
3729 set_scope(&declaration->scope);
3731 declaration_t *parameter = declaration->scope.declarations;
3732 for( ; parameter != NULL; parameter = parameter->next) {
3733 if(parameter->parent_scope == &ndeclaration->scope) {
3734 parameter->parent_scope = scope;
3736 assert(parameter->parent_scope == NULL
3737 || parameter->parent_scope == scope);
3738 parameter->parent_scope = scope;
3739 environment_push(parameter);
3742 if(declaration->init.statement != NULL) {
3743 parser_error_multiple_definition(declaration, token.source_position);
3745 goto end_of_parse_external_declaration;
3747 /* parse function body */
3748 int label_stack_top = label_top();
3749 declaration_t *old_current_function = current_function;
3750 current_function = declaration;
3752 declaration->init.statement = parse_compound_statement();
3755 check_declarations();
3757 assert(current_function == declaration);
3758 current_function = old_current_function;
3759 label_pop_to(label_stack_top);
3762 end_of_parse_external_declaration:
3763 assert(scope == &declaration->scope);
3764 set_scope(last_scope);
3765 environment_pop_to(top);
3768 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3769 source_position_t source_position)
3771 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3772 type->bitfield.base = base;
3773 type->bitfield.size = size;
3778 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3781 declaration_t *iter = compound_declaration->scope.declarations;
3782 for( ; iter != NULL; iter = iter->next) {
3783 if(iter->namespc != NAMESPACE_NORMAL)
3786 if(iter->symbol == NULL) {
3787 type_t *type = skip_typeref(iter->type);
3788 if(is_type_compound(type)) {
3789 declaration_t *result
3790 = find_compound_entry(type->compound.declaration, symbol);
3797 if(iter->symbol == symbol) {
3805 static void parse_compound_declarators(declaration_t *struct_declaration,
3806 const declaration_specifiers_t *specifiers)
3808 declaration_t *last_declaration = struct_declaration->scope.declarations;
3809 if(last_declaration != NULL) {
3810 while(last_declaration->next != NULL) {
3811 last_declaration = last_declaration->next;
3816 declaration_t *declaration;
3818 if(token.type == ':') {
3819 source_position_t source_position = HERE;
3822 type_t *base_type = specifiers->type;
3823 expression_t *size = parse_constant_expression();
3825 if(!is_type_integer(skip_typeref(base_type))) {
3826 errorf(HERE, "bitfield base type '%T' is not an integer type",
3830 type_t *type = make_bitfield_type(base_type, size, source_position);
3832 declaration = allocate_declaration_zero();
3833 declaration->namespc = NAMESPACE_NORMAL;
3834 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3835 declaration->storage_class = STORAGE_CLASS_NONE;
3836 declaration->source_position = source_position;
3837 declaration->modifiers = specifiers->decl_modifiers;
3838 declaration->type = type;
3840 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3842 type_t *orig_type = declaration->type;
3843 type_t *type = skip_typeref(orig_type);
3845 if(token.type == ':') {
3846 source_position_t source_position = HERE;
3848 expression_t *size = parse_constant_expression();
3850 if(!is_type_integer(type)) {
3851 errorf(HERE, "bitfield base type '%T' is not an "
3852 "integer type", orig_type);
3855 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3856 declaration->type = bitfield_type;
3858 /* TODO we ignore arrays for now... what is missing is a check
3859 * that they're at the end of the struct */
3860 if(is_type_incomplete(type) && !is_type_array(type)) {
3862 "compound member '%Y' has incomplete type '%T'",
3863 declaration->symbol, orig_type);
3864 } else if(is_type_function(type)) {
3865 errorf(HERE, "compound member '%Y' must not have function "
3866 "type '%T'", declaration->symbol, orig_type);
3871 /* make sure we don't define a symbol multiple times */
3872 symbol_t *symbol = declaration->symbol;
3873 if(symbol != NULL) {
3874 declaration_t *prev_decl
3875 = find_compound_entry(struct_declaration, symbol);
3877 if(prev_decl != NULL) {
3878 assert(prev_decl->symbol == symbol);
3879 errorf(declaration->source_position,
3880 "multiple declarations of symbol '%Y'", symbol);
3881 errorf(prev_decl->source_position,
3882 "previous declaration of '%Y' was here", symbol);
3886 /* append declaration */
3887 if(last_declaration != NULL) {
3888 last_declaration->next = declaration;
3890 struct_declaration->scope.declarations = declaration;
3892 last_declaration = declaration;
3894 if(token.type != ',')
3904 static void parse_compound_type_entries(declaration_t *compound_declaration)
3907 add_anchor_token('}');
3909 while(token.type != '}' && token.type != T_EOF) {
3910 declaration_specifiers_t specifiers;
3911 memset(&specifiers, 0, sizeof(specifiers));
3912 parse_declaration_specifiers(&specifiers);
3914 parse_compound_declarators(compound_declaration, &specifiers);
3916 rem_anchor_token('}');
3918 if(token.type == T_EOF) {
3919 errorf(HERE, "EOF while parsing struct");
3924 static type_t *parse_typename(void)
3926 declaration_specifiers_t specifiers;
3927 memset(&specifiers, 0, sizeof(specifiers));
3928 parse_declaration_specifiers(&specifiers);
3929 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3930 /* TODO: improve error message, user does probably not know what a
3931 * storage class is...
3933 errorf(HERE, "typename may not have a storage class");
3936 type_t *result = parse_abstract_declarator(specifiers.type);
3944 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3945 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3946 expression_t *left);
3948 typedef struct expression_parser_function_t expression_parser_function_t;
3949 struct expression_parser_function_t {
3950 unsigned precedence;
3951 parse_expression_function parser;
3952 unsigned infix_precedence;
3953 parse_expression_infix_function infix_parser;
3956 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3959 * Creates a new invalid expression.
3961 static expression_t *create_invalid_expression(void)
3963 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3964 expression->base.source_position = token.source_position;
3969 * Prints an error message if an expression was expected but not read
3971 static expression_t *expected_expression_error(void)
3973 /* skip the error message if the error token was read */
3974 if (token.type != T_ERROR) {
3975 errorf(HERE, "expected expression, got token '%K'", &token);
3979 return create_invalid_expression();
3983 * Parse a string constant.
3985 static expression_t *parse_string_const(void)
3988 if (token.type == T_STRING_LITERAL) {
3989 string_t res = token.v.string;
3991 while (token.type == T_STRING_LITERAL) {
3992 res = concat_strings(&res, &token.v.string);
3995 if (token.type != T_WIDE_STRING_LITERAL) {
3996 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3997 /* note: that we use type_char_ptr here, which is already the
3998 * automatic converted type. revert_automatic_type_conversion
3999 * will construct the array type */
4000 cnst->base.type = type_char_ptr;
4001 cnst->string.value = res;
4005 wres = concat_string_wide_string(&res, &token.v.wide_string);
4007 wres = token.v.wide_string;
4012 switch (token.type) {
4013 case T_WIDE_STRING_LITERAL:
4014 wres = concat_wide_strings(&wres, &token.v.wide_string);
4017 case T_STRING_LITERAL:
4018 wres = concat_wide_string_string(&wres, &token.v.string);
4022 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4023 cnst->base.type = type_wchar_t_ptr;
4024 cnst->wide_string.value = wres;
4033 * Parse an integer constant.
4035 static expression_t *parse_int_const(void)
4037 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4038 cnst->base.source_position = HERE;
4039 cnst->base.type = token.datatype;
4040 cnst->conste.v.int_value = token.v.intvalue;
4048 * Parse a character constant.
4050 static expression_t *parse_character_constant(void)
4052 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4054 cnst->base.source_position = HERE;
4055 cnst->base.type = token.datatype;
4056 cnst->conste.v.character = token.v.string;
4058 if (cnst->conste.v.character.size != 1) {
4059 if (warning.multichar && (c_mode & _GNUC)) {
4061 warningf(HERE, "multi-character character constant");
4063 errorf(HERE, "more than 1 characters in character constant");
4072 * Parse a wide character constant.
4074 static expression_t *parse_wide_character_constant(void)
4076 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4078 cnst->base.source_position = HERE;
4079 cnst->base.type = token.datatype;
4080 cnst->conste.v.wide_character = token.v.wide_string;
4082 if (cnst->conste.v.wide_character.size != 1) {
4083 if (warning.multichar && (c_mode & _GNUC)) {
4085 warningf(HERE, "multi-character character constant");
4087 errorf(HERE, "more than 1 characters in character constant");
4096 * Parse a float constant.
4098 static expression_t *parse_float_const(void)
4100 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4101 cnst->base.type = token.datatype;
4102 cnst->conste.v.float_value = token.v.floatvalue;
4109 static declaration_t *create_implicit_function(symbol_t *symbol,
4110 const source_position_t source_position)
4112 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4113 ntype->function.return_type = type_int;
4114 ntype->function.unspecified_parameters = true;
4116 type_t *type = typehash_insert(ntype);
4121 declaration_t *const declaration = allocate_declaration_zero();
4122 declaration->storage_class = STORAGE_CLASS_EXTERN;
4123 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4124 declaration->type = type;
4125 declaration->symbol = symbol;
4126 declaration->source_position = source_position;
4127 declaration->parent_scope = global_scope;
4129 scope_t *old_scope = scope;
4130 set_scope(global_scope);
4132 environment_push(declaration);
4133 /* prepends the declaration to the global declarations list */
4134 declaration->next = scope->declarations;
4135 scope->declarations = declaration;
4137 assert(scope == global_scope);
4138 set_scope(old_scope);
4144 * Creates a return_type (func)(argument_type) function type if not
4147 * @param return_type the return type
4148 * @param argument_type the argument type
4150 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4152 function_parameter_t *parameter
4153 = obstack_alloc(type_obst, sizeof(parameter[0]));
4154 memset(parameter, 0, sizeof(parameter[0]));
4155 parameter->type = argument_type;
4157 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
4158 type->function.return_type = return_type;
4159 type->function.parameters = parameter;
4161 type_t *result = typehash_insert(type);
4162 if(result != type) {
4170 * Creates a function type for some function like builtins.
4172 * @param symbol the symbol describing the builtin
4174 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4176 switch(symbol->ID) {
4177 case T___builtin_alloca:
4178 return make_function_1_type(type_void_ptr, type_size_t);
4179 case T___builtin_nan:
4180 return make_function_1_type(type_double, type_char_ptr);
4181 case T___builtin_nanf:
4182 return make_function_1_type(type_float, type_char_ptr);
4183 case T___builtin_nand:
4184 return make_function_1_type(type_long_double, type_char_ptr);
4185 case T___builtin_va_end:
4186 return make_function_1_type(type_void, type_valist);
4188 internal_errorf(HERE, "not implemented builtin symbol found");
4193 * Performs automatic type cast as described in § 6.3.2.1.
4195 * @param orig_type the original type
4197 static type_t *automatic_type_conversion(type_t *orig_type)
4199 type_t *type = skip_typeref(orig_type);
4200 if(is_type_array(type)) {
4201 array_type_t *array_type = &type->array;
4202 type_t *element_type = array_type->element_type;
4203 unsigned qualifiers = array_type->type.qualifiers;
4205 return make_pointer_type(element_type, qualifiers);
4208 if(is_type_function(type)) {
4209 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4216 * reverts the automatic casts of array to pointer types and function
4217 * to function-pointer types as defined § 6.3.2.1
4219 type_t *revert_automatic_type_conversion(const expression_t *expression)
4221 switch (expression->kind) {
4222 case EXPR_REFERENCE: return expression->reference.declaration->type;
4223 case EXPR_SELECT: return expression->select.compound_entry->type;
4225 case EXPR_UNARY_DEREFERENCE: {
4226 const expression_t *const value = expression->unary.value;
4227 type_t *const type = skip_typeref(value->base.type);
4228 assert(is_type_pointer(type));
4229 return type->pointer.points_to;
4232 case EXPR_BUILTIN_SYMBOL:
4233 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4235 case EXPR_ARRAY_ACCESS: {
4236 const expression_t *array_ref = expression->array_access.array_ref;
4237 type_t *type_left = skip_typeref(array_ref->base.type);
4238 if (!is_type_valid(type_left))
4240 assert(is_type_pointer(type_left));
4241 return type_left->pointer.points_to;
4244 case EXPR_STRING_LITERAL: {
4245 size_t size = expression->string.value.size;
4246 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4249 case EXPR_WIDE_STRING_LITERAL: {
4250 size_t size = expression->wide_string.value.size;
4251 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4254 case EXPR_COMPOUND_LITERAL:
4255 return expression->compound_literal.type;
4260 return expression->base.type;
4263 static expression_t *parse_reference(void)
4265 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4267 reference_expression_t *ref = &expression->reference;
4268 ref->symbol = token.v.symbol;
4270 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4272 source_position_t source_position = token.source_position;
4275 if(declaration == NULL) {
4276 if (! strict_mode && token.type == '(') {
4277 /* an implicitly defined function */
4278 if (warning.implicit_function_declaration) {
4279 warningf(HERE, "implicit declaration of function '%Y'",
4283 declaration = create_implicit_function(ref->symbol,
4286 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4287 return create_invalid_expression();
4291 type_t *type = declaration->type;
4293 /* we always do the auto-type conversions; the & and sizeof parser contains
4294 * code to revert this! */
4295 type = automatic_type_conversion(type);
4297 ref->declaration = declaration;
4298 ref->base.type = type;
4300 /* this declaration is used */
4301 declaration->used = true;
4303 /* check for deprecated functions */
4304 if(declaration->deprecated != 0) {
4305 const char *prefix = "";
4306 if (is_type_function(declaration->type))
4307 prefix = "function ";
4309 if (declaration->deprecated_string != NULL) {
4310 warningf(source_position,
4311 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4312 declaration->deprecated_string);
4314 warningf(source_position,
4315 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4322 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4326 /* TODO check if explicit cast is allowed and issue warnings/errors */
4329 static expression_t *parse_compound_literal(type_t *type)
4331 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4333 parse_initializer_env_t env;
4335 env.declaration = NULL;
4336 env.must_be_constant = false;
4337 initializer_t *initializer = parse_initializer(&env);
4340 expression->compound_literal.initializer = initializer;
4341 expression->compound_literal.type = type;
4342 expression->base.type = automatic_type_conversion(type);
4348 * Parse a cast expression.
4350 static expression_t *parse_cast(void)
4352 source_position_t source_position = token.source_position;
4354 type_t *type = parse_typename();
4356 /* matching add_anchor_token() is at call site */
4357 rem_anchor_token(')');
4360 if(token.type == '{') {
4361 return parse_compound_literal(type);
4364 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4365 cast->base.source_position = source_position;
4367 expression_t *value = parse_sub_expression(20);
4369 check_cast_allowed(value, type);
4371 cast->base.type = type;
4372 cast->unary.value = value;
4376 return create_invalid_expression();
4380 * Parse a statement expression.
4382 static expression_t *parse_statement_expression(void)
4384 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4386 statement_t *statement = parse_compound_statement();
4387 expression->statement.statement = statement;
4388 expression->base.source_position = statement->base.source_position;
4390 /* find last statement and use its type */
4391 type_t *type = type_void;
4392 const statement_t *stmt = statement->compound.statements;
4394 while (stmt->base.next != NULL)
4395 stmt = stmt->base.next;
4397 if (stmt->kind == STATEMENT_EXPRESSION) {
4398 type = stmt->expression.expression->base.type;
4401 warningf(expression->base.source_position, "empty statement expression ({})");
4403 expression->base.type = type;
4409 return create_invalid_expression();
4413 * Parse a braced expression.
4415 static expression_t *parse_brace_expression(void)
4418 add_anchor_token(')');
4420 switch(token.type) {
4422 /* gcc extension: a statement expression */
4423 return parse_statement_expression();
4427 return parse_cast();
4429 if(is_typedef_symbol(token.v.symbol)) {
4430 return parse_cast();
4434 expression_t *result = parse_expression();
4435 rem_anchor_token(')');
4440 return create_invalid_expression();
4443 static expression_t *parse_function_keyword(void)
4448 if (current_function == NULL) {
4449 errorf(HERE, "'__func__' used outside of a function");
4452 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
4453 expression->base.type = type_char_ptr;
4458 static expression_t *parse_pretty_function_keyword(void)
4460 eat(T___PRETTY_FUNCTION__);
4462 if (current_function == NULL) {
4463 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
4466 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
4467 expression->base.type = type_char_ptr;
4472 static expression_t *parse_funcsig_keyword(void)
4476 if (current_function == NULL) {
4477 errorf(HERE, "'__FUNCSIG__' used outside of a function");
4480 expression_t *expression = allocate_expression_zero(EXPR_FUNCSIG);
4481 expression->base.type = type_char_ptr;
4486 static expression_t *parse_funcdname_keyword(void)
4490 if (current_function == NULL) {
4491 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
4494 expression_t *expression = allocate_expression_zero(EXPR_FUNCDNAME);
4495 expression->base.type = type_char_ptr;
4500 static designator_t *parse_designator(void)
4502 designator_t *result = allocate_ast_zero(sizeof(result[0]));
4503 result->source_position = HERE;
4505 if(token.type != T_IDENTIFIER) {
4506 parse_error_expected("while parsing member designator",
4510 result->symbol = token.v.symbol;
4513 designator_t *last_designator = result;
4515 if(token.type == '.') {
4517 if(token.type != T_IDENTIFIER) {
4518 parse_error_expected("while parsing member designator",
4522 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4523 designator->source_position = HERE;
4524 designator->symbol = token.v.symbol;
4527 last_designator->next = designator;
4528 last_designator = designator;
4531 if(token.type == '[') {
4533 add_anchor_token(']');
4534 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4535 designator->source_position = HERE;
4536 designator->array_index = parse_expression();
4537 rem_anchor_token(']');
4539 if(designator->array_index == NULL) {
4543 last_designator->next = designator;
4544 last_designator = designator;
4556 * Parse the __builtin_offsetof() expression.
4558 static expression_t *parse_offsetof(void)
4560 eat(T___builtin_offsetof);
4562 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4563 expression->base.type = type_size_t;
4566 add_anchor_token(',');
4567 type_t *type = parse_typename();
4568 rem_anchor_token(',');
4570 add_anchor_token(')');
4571 designator_t *designator = parse_designator();
4572 rem_anchor_token(')');
4575 expression->offsetofe.type = type;
4576 expression->offsetofe.designator = designator;
4579 memset(&path, 0, sizeof(path));
4580 path.top_type = type;
4581 path.path = NEW_ARR_F(type_path_entry_t, 0);
4583 descend_into_subtype(&path);
4585 if(!walk_designator(&path, designator, true)) {
4586 return create_invalid_expression();
4589 DEL_ARR_F(path.path);
4593 return create_invalid_expression();
4597 * Parses a _builtin_va_start() expression.
4599 static expression_t *parse_va_start(void)
4601 eat(T___builtin_va_start);
4603 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4606 add_anchor_token(',');
4607 expression->va_starte.ap = parse_assignment_expression();
4608 rem_anchor_token(',');
4610 expression_t *const expr = parse_assignment_expression();
4611 if (expr->kind == EXPR_REFERENCE) {
4612 declaration_t *const decl = expr->reference.declaration;
4614 return create_invalid_expression();
4615 if (decl->parent_scope == ¤t_function->scope &&
4616 decl->next == NULL) {
4617 expression->va_starte.parameter = decl;
4622 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4624 return create_invalid_expression();
4628 * Parses a _builtin_va_arg() expression.
4630 static expression_t *parse_va_arg(void)
4632 eat(T___builtin_va_arg);
4634 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4637 expression->va_arge.ap = parse_assignment_expression();
4639 expression->base.type = parse_typename();
4644 return create_invalid_expression();
4647 static expression_t *parse_builtin_symbol(void)
4649 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4651 symbol_t *symbol = token.v.symbol;
4653 expression->builtin_symbol.symbol = symbol;
4656 type_t *type = get_builtin_symbol_type(symbol);
4657 type = automatic_type_conversion(type);
4659 expression->base.type = type;
4664 * Parses a __builtin_constant() expression.
4666 static expression_t *parse_builtin_constant(void)
4668 eat(T___builtin_constant_p);
4670 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4673 add_anchor_token(')');
4674 expression->builtin_constant.value = parse_assignment_expression();
4675 rem_anchor_token(')');
4677 expression->base.type = type_int;
4681 return create_invalid_expression();
4685 * Parses a __builtin_prefetch() expression.
4687 static expression_t *parse_builtin_prefetch(void)
4689 eat(T___builtin_prefetch);
4691 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4694 add_anchor_token(')');
4695 expression->builtin_prefetch.adr = parse_assignment_expression();
4696 if (token.type == ',') {
4698 expression->builtin_prefetch.rw = parse_assignment_expression();
4700 if (token.type == ',') {
4702 expression->builtin_prefetch.locality = parse_assignment_expression();
4704 rem_anchor_token(')');
4706 expression->base.type = type_void;
4710 return create_invalid_expression();
4714 * Parses a __builtin_is_*() compare expression.
4716 static expression_t *parse_compare_builtin(void)
4718 expression_t *expression;
4720 switch(token.type) {
4721 case T___builtin_isgreater:
4722 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4724 case T___builtin_isgreaterequal:
4725 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4727 case T___builtin_isless:
4728 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4730 case T___builtin_islessequal:
4731 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4733 case T___builtin_islessgreater:
4734 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4736 case T___builtin_isunordered:
4737 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4740 internal_errorf(HERE, "invalid compare builtin found");
4743 expression->base.source_position = HERE;
4747 expression->binary.left = parse_assignment_expression();
4749 expression->binary.right = parse_assignment_expression();
4752 type_t *const orig_type_left = expression->binary.left->base.type;
4753 type_t *const orig_type_right = expression->binary.right->base.type;
4755 type_t *const type_left = skip_typeref(orig_type_left);
4756 type_t *const type_right = skip_typeref(orig_type_right);
4757 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4758 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4759 type_error_incompatible("invalid operands in comparison",
4760 expression->base.source_position, orig_type_left, orig_type_right);
4763 semantic_comparison(&expression->binary);
4768 return create_invalid_expression();
4772 * Parses a __builtin_expect() expression.
4774 static expression_t *parse_builtin_expect(void)
4776 eat(T___builtin_expect);
4778 expression_t *expression
4779 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4782 expression->binary.left = parse_assignment_expression();
4784 expression->binary.right = parse_constant_expression();
4787 expression->base.type = expression->binary.left->base.type;
4791 return create_invalid_expression();
4795 * Parses a MS assume() expression.
4797 static expression_t *parse_assume(void) {
4800 expression_t *expression
4801 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4804 add_anchor_token(')');
4805 expression->unary.value = parse_assignment_expression();
4806 rem_anchor_token(')');
4809 expression->base.type = type_void;
4812 return create_invalid_expression();
4816 * Parses a primary expression.
4818 static expression_t *parse_primary_expression(void)
4820 switch (token.type) {
4821 case T_INTEGER: return parse_int_const();
4822 case T_CHARACTER_CONSTANT: return parse_character_constant();
4823 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4824 case T_FLOATINGPOINT: return parse_float_const();
4825 case T_STRING_LITERAL:
4826 case T_WIDE_STRING_LITERAL: return parse_string_const();
4827 case T_IDENTIFIER: return parse_reference();
4828 case T___FUNCTION__:
4829 case T___func__: return parse_function_keyword();
4830 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4831 case T___FUNCSIG__: return parse_funcsig_keyword();
4832 case T___FUNCDNAME__: return parse_funcdname_keyword();
4833 case T___builtin_offsetof: return parse_offsetof();
4834 case T___builtin_va_start: return parse_va_start();
4835 case T___builtin_va_arg: return parse_va_arg();
4836 case T___builtin_expect: return parse_builtin_expect();
4837 case T___builtin_alloca:
4838 case T___builtin_nan:
4839 case T___builtin_nand:
4840 case T___builtin_nanf:
4841 case T___builtin_va_end: return parse_builtin_symbol();
4842 case T___builtin_isgreater:
4843 case T___builtin_isgreaterequal:
4844 case T___builtin_isless:
4845 case T___builtin_islessequal:
4846 case T___builtin_islessgreater:
4847 case T___builtin_isunordered: return parse_compare_builtin();
4848 case T___builtin_constant_p: return parse_builtin_constant();
4849 case T___builtin_prefetch: return parse_builtin_prefetch();
4850 case T__assume: return parse_assume();
4852 case '(': return parse_brace_expression();
4855 errorf(HERE, "unexpected token %K, expected an expression", &token);
4856 return create_invalid_expression();
4860 * Check if the expression has the character type and issue a warning then.
4862 static void check_for_char_index_type(const expression_t *expression) {
4863 type_t *const type = expression->base.type;
4864 const type_t *const base_type = skip_typeref(type);
4866 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4867 warning.char_subscripts) {
4868 warningf(expression->base.source_position,
4869 "array subscript has type '%T'", type);
4873 static expression_t *parse_array_expression(unsigned precedence,
4879 add_anchor_token(']');
4881 expression_t *inside = parse_expression();
4883 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4885 array_access_expression_t *array_access = &expression->array_access;
4887 type_t *const orig_type_left = left->base.type;
4888 type_t *const orig_type_inside = inside->base.type;
4890 type_t *const type_left = skip_typeref(orig_type_left);
4891 type_t *const type_inside = skip_typeref(orig_type_inside);
4893 type_t *return_type;
4894 if (is_type_pointer(type_left)) {
4895 return_type = type_left->pointer.points_to;
4896 array_access->array_ref = left;
4897 array_access->index = inside;
4898 check_for_char_index_type(inside);
4899 } else if (is_type_pointer(type_inside)) {
4900 return_type = type_inside->pointer.points_to;
4901 array_access->array_ref = inside;
4902 array_access->index = left;
4903 array_access->flipped = true;
4904 check_for_char_index_type(left);
4906 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4908 "array access on object with non-pointer types '%T', '%T'",
4909 orig_type_left, orig_type_inside);
4911 return_type = type_error_type;
4912 array_access->array_ref = create_invalid_expression();
4915 rem_anchor_token(']');
4916 if(token.type != ']') {
4917 parse_error_expected("Problem while parsing array access", ']', 0);
4922 return_type = automatic_type_conversion(return_type);
4923 expression->base.type = return_type;
4928 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4930 expression_t *tp_expression = allocate_expression_zero(kind);
4931 tp_expression->base.type = type_size_t;
4933 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4935 add_anchor_token(')');
4936 tp_expression->typeprop.type = parse_typename();
4937 rem_anchor_token(')');
4940 expression_t *expression = parse_sub_expression(precedence);
4941 expression->base.type = revert_automatic_type_conversion(expression);
4943 tp_expression->typeprop.type = expression->base.type;
4944 tp_expression->typeprop.tp_expression = expression;
4947 return tp_expression;
4949 return create_invalid_expression();
4952 static expression_t *parse_sizeof(unsigned precedence)
4955 return parse_typeprop(EXPR_SIZEOF, precedence);
4958 static expression_t *parse_alignof(unsigned precedence)
4961 return parse_typeprop(EXPR_SIZEOF, precedence);
4964 static expression_t *parse_select_expression(unsigned precedence,
4965 expression_t *compound)
4968 assert(token.type == '.' || token.type == T_MINUSGREATER);
4970 bool is_pointer = (token.type == T_MINUSGREATER);
4973 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4974 select->select.compound = compound;
4976 if(token.type != T_IDENTIFIER) {
4977 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4980 symbol_t *symbol = token.v.symbol;
4981 select->select.symbol = symbol;
4984 type_t *const orig_type = compound->base.type;
4985 type_t *const type = skip_typeref(orig_type);
4987 type_t *type_left = type;
4989 if (!is_type_pointer(type)) {
4990 if (is_type_valid(type)) {
4991 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4993 return create_invalid_expression();
4995 type_left = type->pointer.points_to;
4997 type_left = skip_typeref(type_left);
4999 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5000 type_left->kind != TYPE_COMPOUND_UNION) {
5001 if (is_type_valid(type_left)) {
5002 errorf(HERE, "request for member '%Y' in something not a struct or "
5003 "union, but '%T'", symbol, type_left);
5005 return create_invalid_expression();
5008 declaration_t *const declaration = type_left->compound.declaration;
5010 if(!declaration->init.is_defined) {
5011 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5013 return create_invalid_expression();
5016 declaration_t *iter = find_compound_entry(declaration, symbol);
5018 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5019 return create_invalid_expression();
5022 /* we always do the auto-type conversions; the & and sizeof parser contains
5023 * code to revert this! */
5024 type_t *expression_type = automatic_type_conversion(iter->type);
5026 select->select.compound_entry = iter;
5027 select->base.type = expression_type;
5029 if(expression_type->kind == TYPE_BITFIELD) {
5030 expression_t *extract
5031 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5032 extract->unary.value = select;
5033 extract->base.type = expression_type->bitfield.base;
5042 * Parse a call expression, ie. expression '( ... )'.
5044 * @param expression the function address
5046 static expression_t *parse_call_expression(unsigned precedence,
5047 expression_t *expression)
5050 expression_t *result = allocate_expression_zero(EXPR_CALL);
5051 result->base.source_position = expression->base.source_position;
5053 call_expression_t *call = &result->call;
5054 call->function = expression;
5056 type_t *const orig_type = expression->base.type;
5057 type_t *const type = skip_typeref(orig_type);
5059 function_type_t *function_type = NULL;
5060 if (is_type_pointer(type)) {
5061 type_t *const to_type = skip_typeref(type->pointer.points_to);
5063 if (is_type_function(to_type)) {
5064 function_type = &to_type->function;
5065 call->base.type = function_type->return_type;
5069 if (function_type == NULL && is_type_valid(type)) {
5070 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5073 /* parse arguments */
5075 add_anchor_token(')');
5076 add_anchor_token(',');
5078 if(token.type != ')') {
5079 call_argument_t *last_argument = NULL;
5082 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5084 argument->expression = parse_assignment_expression();
5085 if(last_argument == NULL) {
5086 call->arguments = argument;
5088 last_argument->next = argument;
5090 last_argument = argument;
5092 if(token.type != ',')
5097 rem_anchor_token(',');
5098 rem_anchor_token(')');
5101 if(function_type != NULL) {
5102 function_parameter_t *parameter = function_type->parameters;
5103 call_argument_t *argument = call->arguments;
5104 for( ; parameter != NULL && argument != NULL;
5105 parameter = parameter->next, argument = argument->next) {
5106 type_t *expected_type = parameter->type;
5107 /* TODO report scope in error messages */
5108 expression_t *const arg_expr = argument->expression;
5109 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
5110 if (res_type == NULL) {
5111 /* TODO improve error message */
5112 errorf(arg_expr->base.source_position,
5113 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5114 arg_expr, arg_expr->base.type, expected_type);
5116 argument->expression = create_implicit_cast(argument->expression, expected_type);
5119 /* too few parameters */
5120 if(parameter != NULL) {
5121 errorf(HERE, "too few arguments to function '%E'", expression);
5122 } else if(argument != NULL) {
5123 /* too many parameters */
5124 if(!function_type->variadic
5125 && !function_type->unspecified_parameters) {
5126 errorf(HERE, "too many arguments to function '%E'", expression);
5128 /* do default promotion */
5129 for( ; argument != NULL; argument = argument->next) {
5130 type_t *type = argument->expression->base.type;
5132 type = skip_typeref(type);
5133 if(is_type_integer(type)) {
5134 type = promote_integer(type);
5135 } else if(type == type_float) {
5139 argument->expression
5140 = create_implicit_cast(argument->expression, type);
5143 check_format(&result->call);
5146 check_format(&result->call);
5152 return create_invalid_expression();
5155 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5157 static bool same_compound_type(const type_t *type1, const type_t *type2)
5160 is_type_compound(type1) &&
5161 type1->kind == type2->kind &&
5162 type1->compound.declaration == type2->compound.declaration;
5166 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5168 * @param expression the conditional expression
5170 static expression_t *parse_conditional_expression(unsigned precedence,
5171 expression_t *expression)
5174 add_anchor_token(':');
5176 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5178 conditional_expression_t *conditional = &result->conditional;
5179 conditional->condition = expression;
5182 type_t *const condition_type_orig = expression->base.type;
5183 type_t *const condition_type = skip_typeref(condition_type_orig);
5184 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5185 type_error("expected a scalar type in conditional condition",
5186 expression->base.source_position, condition_type_orig);
5189 expression_t *true_expression = parse_expression();
5190 rem_anchor_token(':');
5192 expression_t *false_expression = parse_sub_expression(precedence);
5194 type_t *const orig_true_type = true_expression->base.type;
5195 type_t *const orig_false_type = false_expression->base.type;
5196 type_t *const true_type = skip_typeref(orig_true_type);
5197 type_t *const false_type = skip_typeref(orig_false_type);
5200 type_t *result_type;
5201 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
5202 result_type = semantic_arithmetic(true_type, false_type);
5204 true_expression = create_implicit_cast(true_expression, result_type);
5205 false_expression = create_implicit_cast(false_expression, result_type);
5207 conditional->true_expression = true_expression;
5208 conditional->false_expression = false_expression;
5209 conditional->base.type = result_type;
5210 } else if (same_compound_type(true_type, false_type) || (
5211 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
5212 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
5214 /* just take 1 of the 2 types */
5215 result_type = true_type;
5216 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
5217 && pointers_compatible(true_type, false_type)) {
5219 result_type = true_type;
5220 } else if (is_type_pointer(true_type)
5221 && is_null_pointer_constant(false_expression)) {
5222 result_type = true_type;
5223 } else if (is_type_pointer(false_type)
5224 && is_null_pointer_constant(true_expression)) {
5225 result_type = false_type;
5227 /* TODO: one pointer to void*, other some pointer */
5229 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5230 type_error_incompatible("while parsing conditional",
5231 expression->base.source_position, true_type,
5234 result_type = type_error_type;
5237 conditional->true_expression
5238 = create_implicit_cast(true_expression, result_type);
5239 conditional->false_expression
5240 = create_implicit_cast(false_expression, result_type);
5241 conditional->base.type = result_type;
5244 return create_invalid_expression();
5248 * Parse an extension expression.
5250 static expression_t *parse_extension(unsigned precedence)
5252 eat(T___extension__);
5254 /* TODO enable extensions */
5255 expression_t *expression = parse_sub_expression(precedence);
5256 /* TODO disable extensions */
5261 * Parse a __builtin_classify_type() expression.
5263 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5265 eat(T___builtin_classify_type);
5267 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5268 result->base.type = type_int;
5271 add_anchor_token(')');
5272 expression_t *expression = parse_sub_expression(precedence);
5273 rem_anchor_token(')');
5275 result->classify_type.type_expression = expression;
5279 return create_invalid_expression();
5282 static void semantic_incdec(unary_expression_t *expression)
5284 type_t *const orig_type = expression->value->base.type;
5285 type_t *const type = skip_typeref(orig_type);
5286 /* TODO !is_type_real && !is_type_pointer */
5287 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5288 if (is_type_valid(type)) {
5289 /* TODO: improve error message */
5290 errorf(HERE, "operation needs an arithmetic or pointer type");
5295 expression->base.type = orig_type;
5298 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5300 type_t *const orig_type = expression->value->base.type;
5301 type_t *const type = skip_typeref(orig_type);
5302 if(!is_type_arithmetic(type)) {
5303 if (is_type_valid(type)) {
5304 /* TODO: improve error message */
5305 errorf(HERE, "operation needs an arithmetic type");
5310 expression->base.type = orig_type;
5313 static void semantic_unexpr_scalar(unary_expression_t *expression)
5315 type_t *const orig_type = expression->value->base.type;
5316 type_t *const type = skip_typeref(orig_type);
5317 if (!is_type_scalar(type)) {
5318 if (is_type_valid(type)) {
5319 errorf(HERE, "operand of ! must be of scalar type");
5324 expression->base.type = orig_type;
5327 static void semantic_unexpr_integer(unary_expression_t *expression)
5329 type_t *const orig_type = expression->value->base.type;
5330 type_t *const type = skip_typeref(orig_type);
5331 if (!is_type_integer(type)) {
5332 if (is_type_valid(type)) {
5333 errorf(HERE, "operand of ~ must be of integer type");
5338 expression->base.type = orig_type;
5341 static void semantic_dereference(unary_expression_t *expression)
5343 type_t *const orig_type = expression->value->base.type;
5344 type_t *const type = skip_typeref(orig_type);
5345 if(!is_type_pointer(type)) {
5346 if (is_type_valid(type)) {
5347 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
5352 type_t *result_type = type->pointer.points_to;
5353 result_type = automatic_type_conversion(result_type);
5354 expression->base.type = result_type;
5358 * Check the semantic of the address taken expression.
5360 static void semantic_take_addr(unary_expression_t *expression)
5362 expression_t *value = expression->value;
5363 value->base.type = revert_automatic_type_conversion(value);
5365 type_t *orig_type = value->base.type;
5366 if(!is_type_valid(orig_type))
5369 if(value->kind == EXPR_REFERENCE) {
5370 declaration_t *const declaration = value->reference.declaration;
5371 if(declaration != NULL) {
5372 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
5373 errorf(expression->base.source_position,
5374 "address of register variable '%Y' requested",
5375 declaration->symbol);
5377 declaration->address_taken = 1;
5381 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5384 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
5385 static expression_t *parse_##unexpression_type(unsigned precedence) \
5389 expression_t *unary_expression \
5390 = allocate_expression_zero(unexpression_type); \
5391 unary_expression->base.source_position = HERE; \
5392 unary_expression->unary.value = parse_sub_expression(precedence); \
5394 sfunc(&unary_expression->unary); \
5396 return unary_expression; \
5399 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
5400 semantic_unexpr_arithmetic)
5401 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
5402 semantic_unexpr_arithmetic)
5403 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
5404 semantic_unexpr_scalar)
5405 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
5406 semantic_dereference)
5407 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
5409 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
5410 semantic_unexpr_integer)
5411 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
5413 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
5416 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
5418 static expression_t *parse_##unexpression_type(unsigned precedence, \
5419 expression_t *left) \
5421 (void) precedence; \
5424 expression_t *unary_expression \
5425 = allocate_expression_zero(unexpression_type); \
5426 unary_expression->unary.value = left; \
5428 sfunc(&unary_expression->unary); \
5430 return unary_expression; \
5433 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
5434 EXPR_UNARY_POSTFIX_INCREMENT,
5436 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
5437 EXPR_UNARY_POSTFIX_DECREMENT,
5440 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
5442 /* TODO: handle complex + imaginary types */
5444 /* § 6.3.1.8 Usual arithmetic conversions */
5445 if(type_left == type_long_double || type_right == type_long_double) {
5446 return type_long_double;
5447 } else if(type_left == type_double || type_right == type_double) {
5449 } else if(type_left == type_float || type_right == type_float) {
5453 type_right = promote_integer(type_right);
5454 type_left = promote_integer(type_left);
5456 if(type_left == type_right)
5459 bool signed_left = is_type_signed(type_left);
5460 bool signed_right = is_type_signed(type_right);
5461 int rank_left = get_rank(type_left);
5462 int rank_right = get_rank(type_right);
5463 if(rank_left < rank_right) {
5464 if(signed_left == signed_right || !signed_right) {
5470 if(signed_left == signed_right || !signed_left) {
5479 * Check the semantic restrictions for a binary expression.
5481 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
5483 expression_t *const left = expression->left;
5484 expression_t *const right = expression->right;
5485 type_t *const orig_type_left = left->base.type;
5486 type_t *const orig_type_right = right->base.type;
5487 type_t *const type_left = skip_typeref(orig_type_left);
5488 type_t *const type_right = skip_typeref(orig_type_right);
5490 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5491 /* TODO: improve error message */
5492 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5493 errorf(HERE, "operation needs arithmetic types");
5498 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5499 expression->left = create_implicit_cast(left, arithmetic_type);
5500 expression->right = create_implicit_cast(right, arithmetic_type);
5501 expression->base.type = arithmetic_type;
5504 static void semantic_shift_op(binary_expression_t *expression)
5506 expression_t *const left = expression->left;
5507 expression_t *const right = expression->right;
5508 type_t *const orig_type_left = left->base.type;
5509 type_t *const orig_type_right = right->base.type;
5510 type_t * type_left = skip_typeref(orig_type_left);
5511 type_t * type_right = skip_typeref(orig_type_right);
5513 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
5514 /* TODO: improve error message */
5515 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5516 errorf(HERE, "operation needs integer types");
5521 type_left = promote_integer(type_left);
5522 type_right = promote_integer(type_right);
5524 expression->left = create_implicit_cast(left, type_left);
5525 expression->right = create_implicit_cast(right, type_right);
5526 expression->base.type = type_left;
5529 static void semantic_add(binary_expression_t *expression)
5531 expression_t *const left = expression->left;
5532 expression_t *const right = expression->right;
5533 type_t *const orig_type_left = left->base.type;
5534 type_t *const orig_type_right = right->base.type;
5535 type_t *const type_left = skip_typeref(orig_type_left);
5536 type_t *const type_right = skip_typeref(orig_type_right);
5539 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5540 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5541 expression->left = create_implicit_cast(left, arithmetic_type);
5542 expression->right = create_implicit_cast(right, arithmetic_type);
5543 expression->base.type = arithmetic_type;
5545 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5546 expression->base.type = type_left;
5547 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
5548 expression->base.type = type_right;
5549 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5550 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
5554 static void semantic_sub(binary_expression_t *expression)
5556 expression_t *const left = expression->left;
5557 expression_t *const right = expression->right;
5558 type_t *const orig_type_left = left->base.type;
5559 type_t *const orig_type_right = right->base.type;
5560 type_t *const type_left = skip_typeref(orig_type_left);
5561 type_t *const type_right = skip_typeref(orig_type_right);
5564 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5565 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5566 expression->left = create_implicit_cast(left, arithmetic_type);
5567 expression->right = create_implicit_cast(right, arithmetic_type);
5568 expression->base.type = arithmetic_type;
5570 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5571 expression->base.type = type_left;
5572 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5573 if(!pointers_compatible(type_left, type_right)) {
5575 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5576 orig_type_left, orig_type_right);
5578 expression->base.type = type_ptrdiff_t;
5580 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5581 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5582 orig_type_left, orig_type_right);
5587 * Check the semantics of comparison expressions.
5589 * @param expression The expression to check.
5591 static void semantic_comparison(binary_expression_t *expression)
5593 expression_t *left = expression->left;
5594 expression_t *right = expression->right;
5595 type_t *orig_type_left = left->base.type;
5596 type_t *orig_type_right = right->base.type;
5598 type_t *type_left = skip_typeref(orig_type_left);
5599 type_t *type_right = skip_typeref(orig_type_right);
5601 /* TODO non-arithmetic types */
5602 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5603 if (warning.sign_compare &&
5604 (expression->base.kind != EXPR_BINARY_EQUAL &&
5605 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5606 (is_type_signed(type_left) != is_type_signed(type_right))) {
5607 warningf(expression->base.source_position,
5608 "comparison between signed and unsigned");
5610 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5611 expression->left = create_implicit_cast(left, arithmetic_type);
5612 expression->right = create_implicit_cast(right, arithmetic_type);
5613 expression->base.type = arithmetic_type;
5614 if (warning.float_equal &&
5615 (expression->base.kind == EXPR_BINARY_EQUAL ||
5616 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5617 is_type_float(arithmetic_type)) {
5618 warningf(expression->base.source_position,
5619 "comparing floating point with == or != is unsafe");
5621 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5622 /* TODO check compatibility */
5623 } else if (is_type_pointer(type_left)) {
5624 expression->right = create_implicit_cast(right, type_left);
5625 } else if (is_type_pointer(type_right)) {
5626 expression->left = create_implicit_cast(left, type_right);
5627 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5628 type_error_incompatible("invalid operands in comparison",
5629 expression->base.source_position,
5630 type_left, type_right);
5632 expression->base.type = type_int;
5635 static void semantic_arithmetic_assign(binary_expression_t *expression)
5637 expression_t *left = expression->left;
5638 expression_t *right = expression->right;
5639 type_t *orig_type_left = left->base.type;
5640 type_t *orig_type_right = right->base.type;
5642 type_t *type_left = skip_typeref(orig_type_left);
5643 type_t *type_right = skip_typeref(orig_type_right);
5645 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5646 /* TODO: improve error message */
5647 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5648 errorf(HERE, "operation needs arithmetic types");
5653 /* combined instructions are tricky. We can't create an implicit cast on
5654 * the left side, because we need the uncasted form for the store.
5655 * The ast2firm pass has to know that left_type must be right_type
5656 * for the arithmetic operation and create a cast by itself */
5657 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5658 expression->right = create_implicit_cast(right, arithmetic_type);
5659 expression->base.type = type_left;
5662 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5664 expression_t *const left = expression->left;
5665 expression_t *const right = expression->right;
5666 type_t *const orig_type_left = left->base.type;
5667 type_t *const orig_type_right = right->base.type;
5668 type_t *const type_left = skip_typeref(orig_type_left);
5669 type_t *const type_right = skip_typeref(orig_type_right);
5671 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5672 /* combined instructions are tricky. We can't create an implicit cast on
5673 * the left side, because we need the uncasted form for the store.
5674 * The ast2firm pass has to know that left_type must be right_type
5675 * for the arithmetic operation and create a cast by itself */
5676 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5677 expression->right = create_implicit_cast(right, arithmetic_type);
5678 expression->base.type = type_left;
5679 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5680 expression->base.type = type_left;
5681 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5682 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5687 * Check the semantic restrictions of a logical expression.
5689 static void semantic_logical_op(binary_expression_t *expression)
5691 expression_t *const left = expression->left;
5692 expression_t *const right = expression->right;
5693 type_t *const orig_type_left = left->base.type;
5694 type_t *const orig_type_right = right->base.type;
5695 type_t *const type_left = skip_typeref(orig_type_left);
5696 type_t *const type_right = skip_typeref(orig_type_right);
5698 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5699 /* TODO: improve error message */
5700 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5701 errorf(HERE, "operation needs scalar types");
5706 expression->base.type = type_int;
5710 * Checks if a compound type has constant fields.
5712 static bool has_const_fields(const compound_type_t *type)
5714 const scope_t *scope = &type->declaration->scope;
5715 const declaration_t *declaration = scope->declarations;
5717 for (; declaration != NULL; declaration = declaration->next) {
5718 if (declaration->namespc != NAMESPACE_NORMAL)
5721 const type_t *decl_type = skip_typeref(declaration->type);
5722 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5730 * Check the semantic restrictions of a binary assign expression.
5732 static void semantic_binexpr_assign(binary_expression_t *expression)
5734 expression_t *left = expression->left;
5735 type_t *orig_type_left = left->base.type;
5737 type_t *type_left = revert_automatic_type_conversion(left);
5738 type_left = skip_typeref(orig_type_left);
5740 /* must be a modifiable lvalue */
5741 if (is_type_array(type_left)) {
5742 errorf(HERE, "cannot assign to arrays ('%E')", left);
5745 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5746 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5750 if(is_type_incomplete(type_left)) {
5752 "left-hand side of assignment '%E' has incomplete type '%T'",
5753 left, orig_type_left);
5756 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5757 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5758 left, orig_type_left);
5762 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5764 if (res_type == NULL) {
5765 errorf(expression->base.source_position,
5766 "cannot assign to '%T' from '%T'",
5767 orig_type_left, expression->right->base.type);
5769 expression->right = create_implicit_cast(expression->right, res_type);
5772 expression->base.type = orig_type_left;
5776 * Determine if the outermost operation (or parts thereof) of the given
5777 * expression has no effect in order to generate a warning about this fact.
5778 * Therefore in some cases this only examines some of the operands of the
5779 * expression (see comments in the function and examples below).
5781 * f() + 23; // warning, because + has no effect
5782 * x || f(); // no warning, because x controls execution of f()
5783 * x ? y : f(); // warning, because y has no effect
5784 * (void)x; // no warning to be able to suppress the warning
5785 * This function can NOT be used for an "expression has definitely no effect"-
5787 static bool expression_has_effect(const expression_t *const expr)
5789 switch (expr->kind) {
5790 case EXPR_UNKNOWN: break;
5791 case EXPR_INVALID: return true; /* do NOT warn */
5792 case EXPR_REFERENCE: return false;
5793 case EXPR_CONST: return false;
5794 case EXPR_CHARACTER_CONSTANT: return false;
5795 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5796 case EXPR_STRING_LITERAL: return false;
5797 case EXPR_WIDE_STRING_LITERAL: return false;
5800 const call_expression_t *const call = &expr->call;
5801 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5804 switch (call->function->builtin_symbol.symbol->ID) {
5805 case T___builtin_va_end: return true;
5806 default: return false;
5810 /* Generate the warning if either the left or right hand side of a
5811 * conditional expression has no effect */
5812 case EXPR_CONDITIONAL: {
5813 const conditional_expression_t *const cond = &expr->conditional;
5815 expression_has_effect(cond->true_expression) &&
5816 expression_has_effect(cond->false_expression);
5819 case EXPR_SELECT: return false;
5820 case EXPR_ARRAY_ACCESS: return false;
5821 case EXPR_SIZEOF: return false;
5822 case EXPR_CLASSIFY_TYPE: return false;
5823 case EXPR_ALIGNOF: return false;
5825 case EXPR_FUNCTION: return false;
5826 case EXPR_PRETTY_FUNCTION: return false;
5827 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5828 case EXPR_BUILTIN_CONSTANT_P: return false;
5829 case EXPR_BUILTIN_PREFETCH: return true;
5830 case EXPR_OFFSETOF: return false;
5831 case EXPR_VA_START: return true;
5832 case EXPR_VA_ARG: return true;
5833 case EXPR_STATEMENT: return true; // TODO
5834 case EXPR_COMPOUND_LITERAL: return false;
5836 case EXPR_UNARY_NEGATE: return false;
5837 case EXPR_UNARY_PLUS: return false;
5838 case EXPR_UNARY_BITWISE_NEGATE: return false;
5839 case EXPR_UNARY_NOT: return false;
5840 case EXPR_UNARY_DEREFERENCE: return false;
5841 case EXPR_UNARY_TAKE_ADDRESS: return false;
5842 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5843 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5844 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5845 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5847 /* Treat void casts as if they have an effect in order to being able to
5848 * suppress the warning */
5849 case EXPR_UNARY_CAST: {
5850 type_t *const type = skip_typeref(expr->base.type);
5851 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5854 case EXPR_UNARY_CAST_IMPLICIT: return true;
5855 case EXPR_UNARY_ASSUME: return true;
5856 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5858 case EXPR_BINARY_ADD: return false;
5859 case EXPR_BINARY_SUB: return false;
5860 case EXPR_BINARY_MUL: return false;
5861 case EXPR_BINARY_DIV: return false;
5862 case EXPR_BINARY_MOD: return false;
5863 case EXPR_BINARY_EQUAL: return false;
5864 case EXPR_BINARY_NOTEQUAL: return false;
5865 case EXPR_BINARY_LESS: return false;
5866 case EXPR_BINARY_LESSEQUAL: return false;
5867 case EXPR_BINARY_GREATER: return false;
5868 case EXPR_BINARY_GREATEREQUAL: return false;
5869 case EXPR_BINARY_BITWISE_AND: return false;
5870 case EXPR_BINARY_BITWISE_OR: return false;
5871 case EXPR_BINARY_BITWISE_XOR: return false;
5872 case EXPR_BINARY_SHIFTLEFT: return false;
5873 case EXPR_BINARY_SHIFTRIGHT: return false;
5874 case EXPR_BINARY_ASSIGN: return true;
5875 case EXPR_BINARY_MUL_ASSIGN: return true;
5876 case EXPR_BINARY_DIV_ASSIGN: return true;
5877 case EXPR_BINARY_MOD_ASSIGN: return true;
5878 case EXPR_BINARY_ADD_ASSIGN: return true;
5879 case EXPR_BINARY_SUB_ASSIGN: return true;
5880 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5881 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5882 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5883 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5884 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5886 /* Only examine the right hand side of && and ||, because the left hand
5887 * side already has the effect of controlling the execution of the right
5889 case EXPR_BINARY_LOGICAL_AND:
5890 case EXPR_BINARY_LOGICAL_OR:
5891 /* Only examine the right hand side of a comma expression, because the left
5892 * hand side has a separate warning */
5893 case EXPR_BINARY_COMMA:
5894 return expression_has_effect(expr->binary.right);
5896 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5897 case EXPR_BINARY_ISGREATER: return false;
5898 case EXPR_BINARY_ISGREATEREQUAL: return false;
5899 case EXPR_BINARY_ISLESS: return false;
5900 case EXPR_BINARY_ISLESSEQUAL: return false;
5901 case EXPR_BINARY_ISLESSGREATER: return false;
5902 case EXPR_BINARY_ISUNORDERED: return false;
5905 internal_errorf(HERE, "unexpected expression");
5908 static void semantic_comma(binary_expression_t *expression)
5910 if (warning.unused_value) {
5911 const expression_t *const left = expression->left;
5912 if (!expression_has_effect(left)) {
5913 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5916 expression->base.type = expression->right->base.type;
5919 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5920 static expression_t *parse_##binexpression_type(unsigned precedence, \
5921 expression_t *left) \
5924 source_position_t pos = HERE; \
5926 expression_t *right = parse_sub_expression(precedence + lr); \
5928 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5929 binexpr->base.source_position = pos; \
5930 binexpr->binary.left = left; \
5931 binexpr->binary.right = right; \
5932 sfunc(&binexpr->binary); \
5937 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5938 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5939 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5940 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5941 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5942 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5943 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5944 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5945 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5947 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5948 semantic_comparison, 1)
5949 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5950 semantic_comparison, 1)
5951 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5952 semantic_comparison, 1)
5953 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5954 semantic_comparison, 1)
5956 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5957 semantic_binexpr_arithmetic, 1)
5958 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5959 semantic_binexpr_arithmetic, 1)
5960 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5961 semantic_binexpr_arithmetic, 1)
5962 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5963 semantic_logical_op, 1)
5964 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5965 semantic_logical_op, 1)
5966 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5967 semantic_shift_op, 1)
5968 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5969 semantic_shift_op, 1)
5970 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5971 semantic_arithmetic_addsubb_assign, 0)
5972 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5973 semantic_arithmetic_addsubb_assign, 0)
5974 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5975 semantic_arithmetic_assign, 0)
5976 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5977 semantic_arithmetic_assign, 0)
5978 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5979 semantic_arithmetic_assign, 0)
5980 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5981 semantic_arithmetic_assign, 0)
5982 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5983 semantic_arithmetic_assign, 0)
5984 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5985 semantic_arithmetic_assign, 0)
5986 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5987 semantic_arithmetic_assign, 0)
5988 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5989 semantic_arithmetic_assign, 0)
5991 static expression_t *parse_sub_expression(unsigned precedence)
5993 if(token.type < 0) {
5994 return expected_expression_error();
5997 expression_parser_function_t *parser
5998 = &expression_parsers[token.type];
5999 source_position_t source_position = token.source_position;
6002 if(parser->parser != NULL) {
6003 left = parser->parser(parser->precedence);
6005 left = parse_primary_expression();
6007 assert(left != NULL);
6008 left->base.source_position = source_position;
6011 if(token.type < 0) {
6012 return expected_expression_error();
6015 parser = &expression_parsers[token.type];
6016 if(parser->infix_parser == NULL)
6018 if(parser->infix_precedence < precedence)
6021 left = parser->infix_parser(parser->infix_precedence, left);
6023 assert(left != NULL);
6024 assert(left->kind != EXPR_UNKNOWN);
6025 left->base.source_position = source_position;
6032 * Parse an expression.
6034 static expression_t *parse_expression(void)
6036 return parse_sub_expression(1);
6040 * Register a parser for a prefix-like operator with given precedence.
6042 * @param parser the parser function
6043 * @param token_type the token type of the prefix token
6044 * @param precedence the precedence of the operator
6046 static void register_expression_parser(parse_expression_function parser,
6047 int token_type, unsigned precedence)
6049 expression_parser_function_t *entry = &expression_parsers[token_type];
6051 if(entry->parser != NULL) {
6052 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6053 panic("trying to register multiple expression parsers for a token");
6055 entry->parser = parser;
6056 entry->precedence = precedence;
6060 * Register a parser for an infix operator with given precedence.
6062 * @param parser the parser function
6063 * @param token_type the token type of the infix operator
6064 * @param precedence the precedence of the operator
6066 static void register_infix_parser(parse_expression_infix_function parser,
6067 int token_type, unsigned precedence)
6069 expression_parser_function_t *entry = &expression_parsers[token_type];
6071 if(entry->infix_parser != NULL) {
6072 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6073 panic("trying to register multiple infix expression parsers for a "
6076 entry->infix_parser = parser;
6077 entry->infix_precedence = precedence;
6081 * Initialize the expression parsers.
6083 static void init_expression_parsers(void)
6085 memset(&expression_parsers, 0, sizeof(expression_parsers));
6087 register_infix_parser(parse_array_expression, '[', 30);
6088 register_infix_parser(parse_call_expression, '(', 30);
6089 register_infix_parser(parse_select_expression, '.', 30);
6090 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6091 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6093 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6096 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6097 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6098 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6099 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6100 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6101 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6102 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6103 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6104 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6105 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6106 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6107 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6108 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6109 T_EXCLAMATIONMARKEQUAL, 13);
6110 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6111 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6112 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6113 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6114 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6115 register_infix_parser(parse_conditional_expression, '?', 7);
6116 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6117 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6118 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6119 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6120 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6121 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6122 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6123 T_LESSLESSEQUAL, 2);
6124 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6125 T_GREATERGREATEREQUAL, 2);
6126 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6128 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6130 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6133 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6135 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6136 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6137 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6138 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6139 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6140 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6141 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6143 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6145 register_expression_parser(parse_sizeof, T_sizeof, 25);
6146 register_expression_parser(parse_alignof, T___alignof__, 25);
6147 register_expression_parser(parse_extension, T___extension__, 25);
6148 register_expression_parser(parse_builtin_classify_type,
6149 T___builtin_classify_type, 25);
6153 * Parse a asm statement constraints specification.
6155 static asm_constraint_t *parse_asm_constraints(void)
6157 asm_constraint_t *result = NULL;
6158 asm_constraint_t *last = NULL;
6160 while(token.type == T_STRING_LITERAL || token.type == '[') {
6161 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6162 memset(constraint, 0, sizeof(constraint[0]));
6164 if(token.type == '[') {
6166 if(token.type != T_IDENTIFIER) {
6167 parse_error_expected("while parsing asm constraint",
6171 constraint->symbol = token.v.symbol;
6176 constraint->constraints = parse_string_literals();
6178 constraint->expression = parse_expression();
6182 last->next = constraint;
6184 result = constraint;
6188 if(token.type != ',')
6199 * Parse a asm statement clobber specification.
6201 static asm_clobber_t *parse_asm_clobbers(void)
6203 asm_clobber_t *result = NULL;
6204 asm_clobber_t *last = NULL;
6206 while(token.type == T_STRING_LITERAL) {
6207 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6208 clobber->clobber = parse_string_literals();
6211 last->next = clobber;
6217 if(token.type != ',')
6226 * Parse an asm statement.
6228 static statement_t *parse_asm_statement(void)
6232 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6233 statement->base.source_position = token.source_position;
6235 asm_statement_t *asm_statement = &statement->asms;
6237 if(token.type == T_volatile) {
6239 asm_statement->is_volatile = true;
6243 add_anchor_token(')');
6244 add_anchor_token(':');
6245 asm_statement->asm_text = parse_string_literals();
6247 if(token.type != ':') {
6248 rem_anchor_token(':');
6253 asm_statement->inputs = parse_asm_constraints();
6254 if(token.type != ':') {
6255 rem_anchor_token(':');
6260 asm_statement->outputs = parse_asm_constraints();
6261 if(token.type != ':') {
6262 rem_anchor_token(':');
6265 rem_anchor_token(':');
6268 asm_statement->clobbers = parse_asm_clobbers();
6271 rem_anchor_token(')');
6276 return create_invalid_statement();
6280 * Parse a case statement.
6282 static statement_t *parse_case_statement(void)
6286 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6288 statement->base.source_position = token.source_position;
6289 statement->case_label.expression = parse_expression();
6291 if (c_mode & _GNUC) {
6292 if (token.type == T_DOTDOTDOT) {
6294 statement->case_label.end_range = parse_expression();
6300 if (! is_constant_expression(statement->case_label.expression)) {
6301 errorf(statement->base.source_position,
6302 "case label does not reduce to an integer constant");
6304 /* TODO: check if the case label is already known */
6305 if (current_switch != NULL) {
6306 /* link all cases into the switch statement */
6307 if (current_switch->last_case == NULL) {
6308 current_switch->first_case =
6309 current_switch->last_case = &statement->case_label;
6311 current_switch->last_case->next = &statement->case_label;
6314 errorf(statement->base.source_position,
6315 "case label not within a switch statement");
6318 statement->case_label.statement = parse_statement();
6322 return create_invalid_statement();
6326 * Finds an existing default label of a switch statement.
6328 static case_label_statement_t *
6329 find_default_label(const switch_statement_t *statement)
6331 case_label_statement_t *label = statement->first_case;
6332 for ( ; label != NULL; label = label->next) {
6333 if (label->expression == NULL)
6340 * Parse a default statement.
6342 static statement_t *parse_default_statement(void)
6346 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6348 statement->base.source_position = token.source_position;
6351 if (current_switch != NULL) {
6352 const case_label_statement_t *def_label = find_default_label(current_switch);
6353 if (def_label != NULL) {
6354 errorf(HERE, "multiple default labels in one switch");
6355 errorf(def_label->base.source_position,
6356 "this is the first default label");
6358 /* link all cases into the switch statement */
6359 if (current_switch->last_case == NULL) {
6360 current_switch->first_case =
6361 current_switch->last_case = &statement->case_label;
6363 current_switch->last_case->next = &statement->case_label;
6367 errorf(statement->base.source_position,
6368 "'default' label not within a switch statement");
6370 statement->case_label.statement = parse_statement();
6374 return create_invalid_statement();
6378 * Return the declaration for a given label symbol or create a new one.
6380 static declaration_t *get_label(symbol_t *symbol)
6382 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
6383 assert(current_function != NULL);
6384 /* if we found a label in the same function, then we already created the
6386 if(candidate != NULL
6387 && candidate->parent_scope == ¤t_function->scope) {
6391 /* otherwise we need to create a new one */
6392 declaration_t *const declaration = allocate_declaration_zero();
6393 declaration->namespc = NAMESPACE_LABEL;
6394 declaration->symbol = symbol;
6396 label_push(declaration);
6402 * Parse a label statement.
6404 static statement_t *parse_label_statement(void)
6406 assert(token.type == T_IDENTIFIER);
6407 symbol_t *symbol = token.v.symbol;
6410 declaration_t *label = get_label(symbol);
6412 /* if source position is already set then the label is defined twice,
6413 * otherwise it was just mentioned in a goto so far */
6414 if(label->source_position.input_name != NULL) {
6415 errorf(HERE, "duplicate label '%Y'", symbol);
6416 errorf(label->source_position, "previous definition of '%Y' was here",
6419 label->source_position = token.source_position;
6422 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
6424 statement->base.source_position = token.source_position;
6425 statement->label.label = label;
6429 if(token.type == '}') {
6430 /* TODO only warn? */
6432 warningf(HERE, "label at end of compound statement");
6433 statement->label.statement = create_empty_statement();
6435 errorf(HERE, "label at end of compound statement");
6436 statement->label.statement = create_invalid_statement();
6440 if (token.type == ';') {
6441 /* eat an empty statement here, to avoid the warning about an empty
6442 * after a label. label:; is commonly used to have a label before
6444 statement->label.statement = create_empty_statement();
6447 statement->label.statement = parse_statement();
6451 /* remember the labels's in a list for later checking */
6452 if (label_last == NULL) {
6453 label_first = &statement->label;
6455 label_last->next = &statement->label;
6457 label_last = &statement->label;
6463 * Parse an if statement.
6465 static statement_t *parse_if(void)
6469 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
6470 statement->base.source_position = token.source_position;
6473 add_anchor_token(')');
6474 statement->ifs.condition = parse_expression();
6475 rem_anchor_token(')');
6478 add_anchor_token(T_else);
6479 statement->ifs.true_statement = parse_statement();
6480 rem_anchor_token(T_else);
6482 if(token.type == T_else) {
6484 statement->ifs.false_statement = parse_statement();
6489 return create_invalid_statement();
6493 * Parse a switch statement.
6495 static statement_t *parse_switch(void)
6499 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
6500 statement->base.source_position = token.source_position;
6503 expression_t *const expr = parse_expression();
6504 type_t * type = skip_typeref(expr->base.type);
6505 if (is_type_integer(type)) {
6506 type = promote_integer(type);
6507 } else if (is_type_valid(type)) {
6508 errorf(expr->base.source_position,
6509 "switch quantity is not an integer, but '%T'", type);
6510 type = type_error_type;
6512 statement->switchs.expression = create_implicit_cast(expr, type);
6515 switch_statement_t *rem = current_switch;
6516 current_switch = &statement->switchs;
6517 statement->switchs.body = parse_statement();
6518 current_switch = rem;
6520 if (warning.switch_default
6521 && find_default_label(&statement->switchs) == NULL) {
6522 warningf(statement->base.source_position, "switch has no default case");
6527 return create_invalid_statement();
6530 static statement_t *parse_loop_body(statement_t *const loop)
6532 statement_t *const rem = current_loop;
6533 current_loop = loop;
6535 statement_t *const body = parse_statement();
6542 * Parse a while statement.
6544 static statement_t *parse_while(void)
6548 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
6549 statement->base.source_position = token.source_position;
6552 add_anchor_token(')');
6553 statement->whiles.condition = parse_expression();
6554 rem_anchor_token(')');
6557 statement->whiles.body = parse_loop_body(statement);
6561 return create_invalid_statement();
6565 * Parse a do statement.
6567 static statement_t *parse_do(void)
6571 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
6573 statement->base.source_position = token.source_position;
6575 add_anchor_token(T_while);
6576 statement->do_while.body = parse_loop_body(statement);
6577 rem_anchor_token(T_while);
6581 add_anchor_token(')');
6582 statement->do_while.condition = parse_expression();
6583 rem_anchor_token(')');
6589 return create_invalid_statement();
6593 * Parse a for statement.
6595 static statement_t *parse_for(void)
6599 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6600 statement->base.source_position = token.source_position;
6602 int top = environment_top();
6603 scope_t *last_scope = scope;
6604 set_scope(&statement->fors.scope);
6607 add_anchor_token(')');
6609 if(token.type != ';') {
6610 if(is_declaration_specifier(&token, false)) {
6611 parse_declaration(record_declaration);
6613 expression_t *const init = parse_expression();
6614 statement->fors.initialisation = init;
6615 if (warning.unused_value && !expression_has_effect(init)) {
6616 warningf(init->base.source_position,
6617 "initialisation of 'for'-statement has no effect");
6625 if(token.type != ';') {
6626 statement->fors.condition = parse_expression();
6629 if(token.type != ')') {
6630 expression_t *const step = parse_expression();
6631 statement->fors.step = step;
6632 if (warning.unused_value && !expression_has_effect(step)) {
6633 warningf(step->base.source_position,
6634 "step of 'for'-statement has no effect");
6637 rem_anchor_token(')');
6639 statement->fors.body = parse_loop_body(statement);
6641 assert(scope == &statement->fors.scope);
6642 set_scope(last_scope);
6643 environment_pop_to(top);
6648 rem_anchor_token(')');
6649 assert(scope == &statement->fors.scope);
6650 set_scope(last_scope);
6651 environment_pop_to(top);
6653 return create_invalid_statement();
6657 * Parse a goto statement.
6659 static statement_t *parse_goto(void)
6663 if(token.type != T_IDENTIFIER) {
6664 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6668 symbol_t *symbol = token.v.symbol;
6671 declaration_t *label = get_label(symbol);
6673 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6674 statement->base.source_position = token.source_position;
6676 statement->gotos.label = label;
6678 /* remember the goto's in a list for later checking */
6679 if (goto_last == NULL) {
6680 goto_first = &statement->gotos;
6682 goto_last->next = &statement->gotos;
6684 goto_last = &statement->gotos;
6690 return create_invalid_statement();
6694 * Parse a continue statement.
6696 static statement_t *parse_continue(void)
6698 statement_t *statement;
6699 if (current_loop == NULL) {
6700 errorf(HERE, "continue statement not within loop");
6703 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6705 statement->base.source_position = token.source_position;
6713 return create_invalid_statement();
6717 * Parse a break statement.
6719 static statement_t *parse_break(void)
6721 statement_t *statement;
6722 if (current_switch == NULL && current_loop == NULL) {
6723 errorf(HERE, "break statement not within loop or switch");
6726 statement = allocate_statement_zero(STATEMENT_BREAK);
6728 statement->base.source_position = token.source_position;
6736 return create_invalid_statement();
6740 * Check if a given declaration represents a local variable.
6742 static bool is_local_var_declaration(const declaration_t *declaration) {
6743 switch ((storage_class_tag_t) declaration->storage_class) {
6744 case STORAGE_CLASS_AUTO:
6745 case STORAGE_CLASS_REGISTER: {
6746 const type_t *type = skip_typeref(declaration->type);
6747 if(is_type_function(type)) {
6759 * Check if a given declaration represents a variable.
6761 static bool is_var_declaration(const declaration_t *declaration) {
6762 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6765 const type_t *type = skip_typeref(declaration->type);
6766 return !is_type_function(type);
6770 * Check if a given expression represents a local variable.
6772 static bool is_local_variable(const expression_t *expression)
6774 if (expression->base.kind != EXPR_REFERENCE) {
6777 const declaration_t *declaration = expression->reference.declaration;
6778 return is_local_var_declaration(declaration);
6782 * Check if a given expression represents a local variable and
6783 * return its declaration then, else return NULL.
6785 declaration_t *expr_is_variable(const expression_t *expression)
6787 if (expression->base.kind != EXPR_REFERENCE) {
6790 declaration_t *declaration = expression->reference.declaration;
6791 if (is_var_declaration(declaration))
6797 * Parse a return statement.
6799 static statement_t *parse_return(void)
6803 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6804 statement->base.source_position = token.source_position;
6806 expression_t *return_value = NULL;
6807 if(token.type != ';') {
6808 return_value = parse_expression();
6812 const type_t *const func_type = current_function->type;
6813 assert(is_type_function(func_type));
6814 type_t *const return_type = skip_typeref(func_type->function.return_type);
6816 if(return_value != NULL) {
6817 type_t *return_value_type = skip_typeref(return_value->base.type);
6819 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6820 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6821 warningf(statement->base.source_position,
6822 "'return' with a value, in function returning void");
6823 return_value = NULL;
6825 type_t *const res_type = semantic_assign(return_type,
6826 return_value, "'return'");
6827 if (res_type == NULL) {
6828 errorf(statement->base.source_position,
6829 "cannot return something of type '%T' in function returning '%T'",
6830 return_value->base.type, return_type);
6832 return_value = create_implicit_cast(return_value, res_type);
6835 /* check for returning address of a local var */
6836 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6837 const expression_t *expression = return_value->unary.value;
6838 if (is_local_variable(expression)) {
6839 warningf(statement->base.source_position,
6840 "function returns address of local variable");
6844 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6845 warningf(statement->base.source_position,
6846 "'return' without value, in function returning non-void");
6849 statement->returns.value = return_value;
6853 return create_invalid_statement();
6857 * Parse a declaration statement.
6859 static statement_t *parse_declaration_statement(void)
6861 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6863 statement->base.source_position = token.source_position;
6865 declaration_t *before = last_declaration;
6866 parse_declaration(record_declaration);
6868 if(before == NULL) {
6869 statement->declaration.declarations_begin = scope->declarations;
6871 statement->declaration.declarations_begin = before->next;
6873 statement->declaration.declarations_end = last_declaration;
6879 * Parse an expression statement, ie. expr ';'.
6881 static statement_t *parse_expression_statement(void)
6883 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6885 statement->base.source_position = token.source_position;
6886 expression_t *const expr = parse_expression();
6887 statement->expression.expression = expr;
6889 if (warning.unused_value && !expression_has_effect(expr)) {
6890 warningf(expr->base.source_position, "statement has no effect");
6897 return create_invalid_statement();
6901 * Parse a statement.
6903 static statement_t *parse_statement(void)
6905 statement_t *statement = NULL;
6907 /* declaration or statement */
6908 add_anchor_token(';');
6909 switch(token.type) {
6911 statement = parse_asm_statement();
6915 statement = parse_case_statement();
6919 statement = parse_default_statement();
6923 statement = parse_compound_statement();
6927 statement = parse_if();
6931 statement = parse_switch();
6935 statement = parse_while();
6939 statement = parse_do();
6943 statement = parse_for();
6947 statement = parse_goto();
6951 statement = parse_continue();
6955 statement = parse_break();
6959 statement = parse_return();
6963 if(warning.empty_statement) {
6964 warningf(HERE, "statement is empty");
6966 statement = create_empty_statement();
6971 if(look_ahead(1)->type == ':') {
6972 statement = parse_label_statement();
6976 if(is_typedef_symbol(token.v.symbol)) {
6977 statement = parse_declaration_statement();
6981 statement = parse_expression_statement();
6984 case T___extension__:
6985 /* this can be a prefix to a declaration or an expression statement */
6986 /* we simply eat it now and parse the rest with tail recursion */
6989 } while(token.type == T___extension__);
6990 statement = parse_statement();
6994 statement = parse_declaration_statement();
6998 statement = parse_expression_statement();
7001 rem_anchor_token(';');
7003 assert(statement != NULL
7004 && statement->base.source_position.input_name != NULL);
7010 * Parse a compound statement.
7012 static statement_t *parse_compound_statement(void)
7014 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7016 statement->base.source_position = token.source_position;
7019 add_anchor_token('}');
7021 int top = environment_top();
7022 scope_t *last_scope = scope;
7023 set_scope(&statement->compound.scope);
7025 statement_t *last_statement = NULL;
7027 while(token.type != '}' && token.type != T_EOF) {
7028 statement_t *sub_statement = parse_statement();
7029 if(is_invalid_statement(sub_statement)) {
7030 /* an error occurred. if we are at an anchor, return */
7036 if(last_statement != NULL) {
7037 last_statement->base.next = sub_statement;
7039 statement->compound.statements = sub_statement;
7042 while(sub_statement->base.next != NULL)
7043 sub_statement = sub_statement->base.next;
7045 last_statement = sub_statement;
7048 if(token.type == '}') {
7051 errorf(statement->base.source_position,
7052 "end of file while looking for closing '}'");
7056 rem_anchor_token('}');
7057 assert(scope == &statement->compound.scope);
7058 set_scope(last_scope);
7059 environment_pop_to(top);
7065 * Initialize builtin types.
7067 static void initialize_builtin_types(void)
7069 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7070 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7071 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7072 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7073 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7074 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7075 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7076 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7078 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7079 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7080 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7081 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7085 * Check for unused global static functions and variables
7087 static void check_unused_globals(void)
7089 if (!warning.unused_function && !warning.unused_variable)
7092 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7093 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7096 type_t *const type = decl->type;
7098 if (is_type_function(skip_typeref(type))) {
7099 if (!warning.unused_function || decl->is_inline)
7102 s = (decl->init.statement != NULL ? "defined" : "declared");
7104 if (!warning.unused_variable)
7110 warningf(decl->source_position, "'%#T' %s but not used",
7111 type, decl->symbol, s);
7116 * Parse a translation unit.
7118 static translation_unit_t *parse_translation_unit(void)
7120 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7122 assert(global_scope == NULL);
7123 global_scope = &unit->scope;
7125 assert(scope == NULL);
7126 set_scope(&unit->scope);
7128 initialize_builtin_types();
7130 while(token.type != T_EOF) {
7131 if (token.type == ';') {
7132 /* TODO error in strict mode */
7133 warningf(HERE, "stray ';' outside of function");
7136 parse_external_declaration();
7140 assert(scope == &unit->scope);
7142 last_declaration = NULL;
7144 assert(global_scope == &unit->scope);
7145 check_unused_globals();
7146 global_scope = NULL;
7154 * @return the translation unit or NULL if errors occurred.
7156 translation_unit_t *parse(void)
7158 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7159 label_stack = NEW_ARR_F(stack_entry_t, 0);
7160 diagnostic_count = 0;
7164 type_set_output(stderr);
7165 ast_set_output(stderr);
7167 lookahead_bufpos = 0;
7168 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7171 translation_unit_t *unit = parse_translation_unit();
7173 DEL_ARR_F(environment_stack);
7174 DEL_ARR_F(label_stack);
7180 * Initialize the parser.
7182 void init_parser(void)
7185 /* add predefined symbols for extended-decl-modifier */
7186 sym_align = symbol_table_insert("align");
7187 sym_allocate = symbol_table_insert("allocate");
7188 sym_dllimport = symbol_table_insert("dllimport");
7189 sym_dllexport = symbol_table_insert("dllexport");
7190 sym_naked = symbol_table_insert("naked");
7191 sym_noinline = symbol_table_insert("noinline");
7192 sym_noreturn = symbol_table_insert("noreturn");
7193 sym_nothrow = symbol_table_insert("nothrow");
7194 sym_novtable = symbol_table_insert("novtable");
7195 sym_property = symbol_table_insert("property");
7196 sym_get = symbol_table_insert("get");
7197 sym_put = symbol_table_insert("put");
7198 sym_selectany = symbol_table_insert("selectany");
7199 sym_thread = symbol_table_insert("thread");
7200 sym_uuid = symbol_table_insert("uuid");
7201 sym_deprecated = symbol_table_insert("deprecated");
7202 sym_restrict = symbol_table_insert("restrict");
7203 sym_noalias = symbol_table_insert("noalias");
7205 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7207 init_expression_parsers();
7208 obstack_init(&temp_obst);
7210 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7211 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7215 * Terminate the parser.
7217 void exit_parser(void)
7219 obstack_free(&temp_obst, NULL);