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 *sym = token.v.symbol;
957 if(sym == sym_deprecated) {
960 if(token.type == '(')
961 eat_until_matching_token('(');
962 if(token.type != ',')
973 * Parse GNU attributes.
975 static void parse_attributes(void)
979 case T___attribute__: {
980 parse_gnu_attribute();
986 if(token.type != T_STRING_LITERAL) {
987 parse_error_expected("while parsing assembler attribute",
989 eat_until_matching_token('(');
992 parse_string_literals();
997 goto attributes_finished;
1002 attributes_finished:
1006 static designator_t *parse_designation(void)
1008 designator_t *result = NULL;
1009 designator_t *last = NULL;
1012 designator_t *designator;
1013 switch(token.type) {
1015 designator = allocate_ast_zero(sizeof(designator[0]));
1016 designator->source_position = token.source_position;
1018 add_anchor_token(']');
1019 designator->array_index = parse_constant_expression();
1020 rem_anchor_token(']');
1024 designator = allocate_ast_zero(sizeof(designator[0]));
1025 designator->source_position = token.source_position;
1027 if(token.type != T_IDENTIFIER) {
1028 parse_error_expected("while parsing designator",
1032 designator->symbol = token.v.symbol;
1040 assert(designator != NULL);
1042 last->next = designator;
1044 result = designator;
1052 static initializer_t *initializer_from_string(array_type_t *type,
1053 const string_t *const string)
1055 /* TODO: check len vs. size of array type */
1058 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1059 initializer->string.string = *string;
1064 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1065 wide_string_t *const string)
1067 /* TODO: check len vs. size of array type */
1070 initializer_t *const initializer =
1071 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1072 initializer->wide_string.string = *string;
1078 * Build an initializer from a given expression.
1080 static initializer_t *initializer_from_expression(type_t *orig_type,
1081 expression_t *expression)
1083 /* TODO check that expression is a constant expression */
1085 /* § 6.7.8.14/15 char array may be initialized by string literals */
1086 type_t *type = skip_typeref(orig_type);
1087 type_t *expr_type_orig = expression->base.type;
1088 type_t *expr_type = skip_typeref(expr_type_orig);
1089 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1090 array_type_t *const array_type = &type->array;
1091 type_t *const element_type = skip_typeref(array_type->element_type);
1093 if (element_type->kind == TYPE_ATOMIC) {
1094 atomic_type_kind_t akind = element_type->atomic.akind;
1095 switch (expression->kind) {
1096 case EXPR_STRING_LITERAL:
1097 if (akind == ATOMIC_TYPE_CHAR
1098 || akind == ATOMIC_TYPE_SCHAR
1099 || akind == ATOMIC_TYPE_UCHAR) {
1100 return initializer_from_string(array_type,
1101 &expression->string.value);
1104 case EXPR_WIDE_STRING_LITERAL: {
1105 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1106 if (get_unqualified_type(element_type) == bare_wchar_type) {
1107 return initializer_from_wide_string(array_type,
1108 &expression->wide_string.value);
1118 type_t *const res_type = semantic_assign(type, expression, "initializer");
1119 if (res_type == NULL)
1122 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1123 result->value.value = create_implicit_cast(expression, res_type);
1129 * Checks if a given expression can be used as an constant initializer.
1131 static bool is_initializer_constant(const expression_t *expression)
1133 return is_constant_expression(expression)
1134 || is_address_constant(expression);
1138 * Parses an scalar initializer.
1140 * § 6.7.8.11; eat {} without warning
1142 static initializer_t *parse_scalar_initializer(type_t *type,
1143 bool must_be_constant)
1145 /* there might be extra {} hierarchies */
1147 while(token.type == '{') {
1150 warningf(HERE, "extra curly braces around scalar initializer");
1155 expression_t *expression = parse_assignment_expression();
1156 if(must_be_constant && !is_initializer_constant(expression)) {
1157 errorf(expression->base.source_position,
1158 "Initialisation expression '%E' is not constant\n",
1162 initializer_t *initializer = initializer_from_expression(type, expression);
1164 if(initializer == NULL) {
1165 errorf(expression->base.source_position,
1166 "expression '%E' doesn't match expected type '%T'",
1172 bool additional_warning_displayed = false;
1174 if(token.type == ',') {
1177 if(token.type != '}') {
1178 if(!additional_warning_displayed) {
1179 warningf(HERE, "additional elements in scalar initializer");
1180 additional_warning_displayed = true;
1191 * An entry in the type path.
1193 typedef struct type_path_entry_t type_path_entry_t;
1194 struct type_path_entry_t {
1195 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1197 size_t index; /**< For array types: the current index. */
1198 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1203 * A type path expression a position inside compound or array types.
1205 typedef struct type_path_t type_path_t;
1206 struct type_path_t {
1207 type_path_entry_t *path; /**< An flexible array containing the current path. */
1208 type_t *top_type; /**< type of the element the path points */
1209 size_t max_index; /**< largest index in outermost array */
1213 * Prints a type path for debugging.
1215 static __attribute__((unused)) void debug_print_type_path(
1216 const type_path_t *path)
1218 size_t len = ARR_LEN(path->path);
1220 for(size_t i = 0; i < len; ++i) {
1221 const type_path_entry_t *entry = & path->path[i];
1223 type_t *type = skip_typeref(entry->type);
1224 if(is_type_compound(type)) {
1225 /* in gcc mode structs can have no members */
1226 if(entry->v.compound_entry == NULL) {
1230 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1231 } else if(is_type_array(type)) {
1232 fprintf(stderr, "[%zd]", entry->v.index);
1234 fprintf(stderr, "-INVALID-");
1237 if(path->top_type != NULL) {
1238 fprintf(stderr, " (");
1239 print_type(path->top_type);
1240 fprintf(stderr, ")");
1245 * Return the top type path entry, ie. in a path
1246 * (type).a.b returns the b.
1248 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1250 size_t len = ARR_LEN(path->path);
1252 return &path->path[len-1];
1256 * Enlarge the type path by an (empty) element.
1258 static type_path_entry_t *append_to_type_path(type_path_t *path)
1260 size_t len = ARR_LEN(path->path);
1261 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1263 type_path_entry_t *result = & path->path[len];
1264 memset(result, 0, sizeof(result[0]));
1269 * Descending into a sub-type. Enter the scope of the current
1272 static void descend_into_subtype(type_path_t *path)
1274 type_t *orig_top_type = path->top_type;
1275 type_t *top_type = skip_typeref(orig_top_type);
1277 assert(is_type_compound(top_type) || is_type_array(top_type));
1279 type_path_entry_t *top = append_to_type_path(path);
1280 top->type = top_type;
1282 if(is_type_compound(top_type)) {
1283 declaration_t *declaration = top_type->compound.declaration;
1284 declaration_t *entry = declaration->scope.declarations;
1285 top->v.compound_entry = entry;
1288 path->top_type = entry->type;
1290 path->top_type = NULL;
1293 assert(is_type_array(top_type));
1296 path->top_type = top_type->array.element_type;
1301 * Pop an entry from the given type path, ie. returning from
1302 * (type).a.b to (type).a
1304 static void ascend_from_subtype(type_path_t *path)
1306 type_path_entry_t *top = get_type_path_top(path);
1308 path->top_type = top->type;
1310 size_t len = ARR_LEN(path->path);
1311 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1315 * Pop entries from the given type path until the given
1316 * path level is reached.
1318 static void ascend_to(type_path_t *path, size_t top_path_level)
1320 size_t len = ARR_LEN(path->path);
1322 while(len > top_path_level) {
1323 ascend_from_subtype(path);
1324 len = ARR_LEN(path->path);
1328 static bool walk_designator(type_path_t *path, const designator_t *designator,
1329 bool used_in_offsetof)
1331 for( ; designator != NULL; designator = designator->next) {
1332 type_path_entry_t *top = get_type_path_top(path);
1333 type_t *orig_type = top->type;
1335 type_t *type = skip_typeref(orig_type);
1337 if(designator->symbol != NULL) {
1338 symbol_t *symbol = designator->symbol;
1339 if(!is_type_compound(type)) {
1340 if(is_type_valid(type)) {
1341 errorf(designator->source_position,
1342 "'.%Y' designator used for non-compound type '%T'",
1348 declaration_t *declaration = type->compound.declaration;
1349 declaration_t *iter = declaration->scope.declarations;
1350 for( ; iter != NULL; iter = iter->next) {
1351 if(iter->symbol == symbol) {
1356 errorf(designator->source_position,
1357 "'%T' has no member named '%Y'", orig_type, symbol);
1360 if(used_in_offsetof) {
1361 type_t *real_type = skip_typeref(iter->type);
1362 if(real_type->kind == TYPE_BITFIELD) {
1363 errorf(designator->source_position,
1364 "offsetof designator '%Y' may not specify bitfield",
1370 top->type = orig_type;
1371 top->v.compound_entry = iter;
1372 orig_type = iter->type;
1374 expression_t *array_index = designator->array_index;
1375 assert(designator->array_index != NULL);
1377 if(!is_type_array(type)) {
1378 if(is_type_valid(type)) {
1379 errorf(designator->source_position,
1380 "[%E] designator used for non-array type '%T'",
1381 array_index, orig_type);
1385 if(!is_type_valid(array_index->base.type)) {
1389 long index = fold_constant(array_index);
1390 if(!used_in_offsetof) {
1392 errorf(designator->source_position,
1393 "array index [%E] must be positive", array_index);
1396 if(type->array.size_constant == true) {
1397 long array_size = type->array.size;
1398 if(index >= array_size) {
1399 errorf(designator->source_position,
1400 "designator [%E] (%d) exceeds array size %d",
1401 array_index, index, array_size);
1407 top->type = orig_type;
1408 top->v.index = (size_t) index;
1409 orig_type = type->array.element_type;
1411 path->top_type = orig_type;
1413 if(designator->next != NULL) {
1414 descend_into_subtype(path);
1423 static void advance_current_object(type_path_t *path, size_t top_path_level)
1425 type_path_entry_t *top = get_type_path_top(path);
1427 type_t *type = skip_typeref(top->type);
1428 if(is_type_union(type)) {
1429 /* in unions only the first element is initialized */
1430 top->v.compound_entry = NULL;
1431 } else if(is_type_struct(type)) {
1432 declaration_t *entry = top->v.compound_entry;
1434 entry = entry->next;
1435 top->v.compound_entry = entry;
1437 path->top_type = entry->type;
1441 assert(is_type_array(type));
1445 if(!type->array.size_constant || top->v.index < type->array.size) {
1450 /* we're past the last member of the current sub-aggregate, try if we
1451 * can ascend in the type hierarchy and continue with another subobject */
1452 size_t len = ARR_LEN(path->path);
1454 if(len > top_path_level) {
1455 ascend_from_subtype(path);
1456 advance_current_object(path, top_path_level);
1458 path->top_type = NULL;
1463 * skip until token is found.
1465 static void skip_until(int type) {
1466 while(token.type != type) {
1467 if(token.type == T_EOF)
1474 * skip any {...} blocks until a closing braket is reached.
1476 static void skip_initializers(void)
1478 if(token.type == '{')
1481 while(token.type != '}') {
1482 if(token.type == T_EOF)
1484 if(token.type == '{') {
1492 static initializer_t *create_empty_initializer(void)
1494 static initializer_t empty_initializer
1495 = { .list = { { INITIALIZER_LIST }, 0 } };
1496 return &empty_initializer;
1500 * Parse a part of an initialiser for a struct or union,
1502 static initializer_t *parse_sub_initializer(type_path_t *path,
1503 type_t *outer_type, size_t top_path_level,
1504 parse_initializer_env_t *env)
1506 if(token.type == '}') {
1507 /* empty initializer */
1508 return create_empty_initializer();
1511 type_t *orig_type = path->top_type;
1512 type_t *type = NULL;
1514 if (orig_type == NULL) {
1515 /* We are initializing an empty compound. */
1517 type = skip_typeref(orig_type);
1519 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1520 * initializers in this case. */
1521 if(!is_type_valid(type)) {
1522 skip_initializers();
1523 return create_empty_initializer();
1527 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1530 designator_t *designator = NULL;
1531 if(token.type == '.' || token.type == '[') {
1532 designator = parse_designation();
1534 /* reset path to toplevel, evaluate designator from there */
1535 ascend_to(path, top_path_level);
1536 if(!walk_designator(path, designator, false)) {
1537 /* can't continue after designation error */
1541 initializer_t *designator_initializer
1542 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1543 designator_initializer->designator.designator = designator;
1544 ARR_APP1(initializer_t*, initializers, designator_initializer);
1549 if(token.type == '{') {
1550 if(type != NULL && is_type_scalar(type)) {
1551 sub = parse_scalar_initializer(type, env->must_be_constant);
1555 if (env->declaration != NULL)
1556 errorf(HERE, "extra brace group at end of initializer for '%Y'",
1557 env->declaration->symbol);
1559 errorf(HERE, "extra brace group at end of initializer");
1561 descend_into_subtype(path);
1563 add_anchor_token('}');
1564 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1566 rem_anchor_token('}');
1569 ascend_from_subtype(path);
1573 goto error_parse_next;
1577 /* must be an expression */
1578 expression_t *expression = parse_assignment_expression();
1580 if(env->must_be_constant && !is_initializer_constant(expression)) {
1581 errorf(expression->base.source_position,
1582 "Initialisation expression '%E' is not constant\n",
1587 /* we are already outside, ... */
1591 /* handle { "string" } special case */
1592 if((expression->kind == EXPR_STRING_LITERAL
1593 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1594 && outer_type != NULL) {
1595 sub = initializer_from_expression(outer_type, expression);
1597 if(token.type == ',') {
1600 if(token.type != '}') {
1601 warningf(HERE, "excessive elements in initializer for type '%T'",
1604 /* TODO: eat , ... */
1609 /* descend into subtypes until expression matches type */
1611 orig_type = path->top_type;
1612 type = skip_typeref(orig_type);
1614 sub = initializer_from_expression(orig_type, expression);
1618 if(!is_type_valid(type)) {
1621 if(is_type_scalar(type)) {
1622 errorf(expression->base.source_position,
1623 "expression '%E' doesn't match expected type '%T'",
1624 expression, orig_type);
1628 descend_into_subtype(path);
1632 /* update largest index of top array */
1633 const type_path_entry_t *first = &path->path[0];
1634 type_t *first_type = first->type;
1635 first_type = skip_typeref(first_type);
1636 if(is_type_array(first_type)) {
1637 size_t index = first->v.index;
1638 if(index > path->max_index)
1639 path->max_index = index;
1643 /* append to initializers list */
1644 ARR_APP1(initializer_t*, initializers, sub);
1647 if(env->declaration != NULL)
1648 warningf(HERE, "excess elements in struct initializer for '%Y'",
1649 env->declaration->symbol);
1651 warningf(HERE, "excess elements in struct initializer");
1655 if(token.type == '}') {
1659 if(token.type == '}') {
1664 /* advance to the next declaration if we are not at the end */
1665 advance_current_object(path, top_path_level);
1666 orig_type = path->top_type;
1667 if(orig_type != NULL)
1668 type = skip_typeref(orig_type);
1674 size_t len = ARR_LEN(initializers);
1675 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1676 initializer_t *result = allocate_ast_zero(size);
1677 result->kind = INITIALIZER_LIST;
1678 result->list.len = len;
1679 memcpy(&result->list.initializers, initializers,
1680 len * sizeof(initializers[0]));
1682 DEL_ARR_F(initializers);
1683 ascend_to(path, top_path_level);
1688 skip_initializers();
1689 DEL_ARR_F(initializers);
1690 ascend_to(path, top_path_level);
1695 * Parses an initializer. Parsers either a compound literal
1696 * (env->declaration == NULL) or an initializer of a declaration.
1698 static initializer_t *parse_initializer(parse_initializer_env_t *env)
1700 type_t *type = skip_typeref(env->type);
1701 initializer_t *result = NULL;
1704 if(is_type_scalar(type)) {
1705 result = parse_scalar_initializer(type, env->must_be_constant);
1706 } else if(token.type == '{') {
1710 memset(&path, 0, sizeof(path));
1711 path.top_type = env->type;
1712 path.path = NEW_ARR_F(type_path_entry_t, 0);
1714 descend_into_subtype(&path);
1716 add_anchor_token('}');
1717 result = parse_sub_initializer(&path, env->type, 1, env);
1718 rem_anchor_token('}');
1720 max_index = path.max_index;
1721 DEL_ARR_F(path.path);
1725 /* parse_scalar_initializer() also works in this case: we simply
1726 * have an expression without {} around it */
1727 result = parse_scalar_initializer(type, env->must_be_constant);
1730 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1731 * the array type size */
1732 if(is_type_array(type) && type->array.size_expression == NULL
1733 && result != NULL) {
1735 switch (result->kind) {
1736 case INITIALIZER_LIST:
1737 size = max_index + 1;
1740 case INITIALIZER_STRING:
1741 size = result->string.string.size;
1744 case INITIALIZER_WIDE_STRING:
1745 size = result->wide_string.string.size;
1749 internal_errorf(HERE, "invalid initializer type");
1752 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1753 cnst->base.type = type_size_t;
1754 cnst->conste.v.int_value = size;
1756 type_t *new_type = duplicate_type(type);
1758 new_type->array.size_expression = cnst;
1759 new_type->array.size_constant = true;
1760 new_type->array.size = size;
1761 env->type = new_type;
1769 static declaration_t *append_declaration(declaration_t *declaration);
1771 static declaration_t *parse_compound_type_specifier(bool is_struct)
1779 symbol_t *symbol = NULL;
1780 declaration_t *declaration = NULL;
1782 if (token.type == T___attribute__) {
1787 if(token.type == T_IDENTIFIER) {
1788 symbol = token.v.symbol;
1792 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1794 declaration = get_declaration(symbol, NAMESPACE_UNION);
1796 } else if(token.type != '{') {
1798 parse_error_expected("while parsing struct type specifier",
1799 T_IDENTIFIER, '{', 0);
1801 parse_error_expected("while parsing union type specifier",
1802 T_IDENTIFIER, '{', 0);
1808 if(declaration == NULL) {
1809 declaration = allocate_declaration_zero();
1810 declaration->namespc =
1811 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1812 declaration->source_position = token.source_position;
1813 declaration->symbol = symbol;
1814 declaration->parent_scope = scope;
1815 if (symbol != NULL) {
1816 environment_push(declaration);
1818 append_declaration(declaration);
1821 if(token.type == '{') {
1822 if(declaration->init.is_defined) {
1823 assert(symbol != NULL);
1824 errorf(HERE, "multiple definitions of '%s %Y'",
1825 is_struct ? "struct" : "union", symbol);
1826 declaration->scope.declarations = NULL;
1828 declaration->init.is_defined = true;
1830 parse_compound_type_entries(declaration);
1837 static void parse_enum_entries(type_t *const enum_type)
1841 if(token.type == '}') {
1843 errorf(HERE, "empty enum not allowed");
1847 add_anchor_token('}');
1849 if(token.type != T_IDENTIFIER) {
1850 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1852 rem_anchor_token('}');
1856 declaration_t *const entry = allocate_declaration_zero();
1857 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1858 entry->type = enum_type;
1859 entry->symbol = token.v.symbol;
1860 entry->source_position = token.source_position;
1863 if(token.type == '=') {
1865 expression_t *value = parse_constant_expression();
1867 value = create_implicit_cast(value, enum_type);
1868 entry->init.enum_value = value;
1873 record_declaration(entry);
1875 if(token.type != ',')
1878 } while(token.type != '}');
1879 rem_anchor_token('}');
1887 static type_t *parse_enum_specifier(void)
1891 declaration_t *declaration;
1894 if(token.type == T_IDENTIFIER) {
1895 symbol = token.v.symbol;
1898 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1899 } else if(token.type != '{') {
1900 parse_error_expected("while parsing enum type specifier",
1901 T_IDENTIFIER, '{', 0);
1908 if(declaration == NULL) {
1909 declaration = allocate_declaration_zero();
1910 declaration->namespc = NAMESPACE_ENUM;
1911 declaration->source_position = token.source_position;
1912 declaration->symbol = symbol;
1913 declaration->parent_scope = scope;
1916 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1917 type->enumt.declaration = declaration;
1919 if(token.type == '{') {
1920 if(declaration->init.is_defined) {
1921 errorf(HERE, "multiple definitions of enum %Y", symbol);
1923 if (symbol != NULL) {
1924 environment_push(declaration);
1926 append_declaration(declaration);
1927 declaration->init.is_defined = 1;
1929 parse_enum_entries(type);
1937 * if a symbol is a typedef to another type, return true
1939 static bool is_typedef_symbol(symbol_t *symbol)
1941 const declaration_t *const declaration =
1942 get_declaration(symbol, NAMESPACE_NORMAL);
1944 declaration != NULL &&
1945 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1948 static type_t *parse_typeof(void)
1955 add_anchor_token(')');
1957 expression_t *expression = NULL;
1960 switch(token.type) {
1961 case T___extension__:
1962 /* this can be a prefix to a typename or an expression */
1963 /* we simply eat it now. */
1966 } while(token.type == T___extension__);
1970 if(is_typedef_symbol(token.v.symbol)) {
1971 type = parse_typename();
1973 expression = parse_expression();
1974 type = expression->base.type;
1979 type = parse_typename();
1983 expression = parse_expression();
1984 type = expression->base.type;
1988 rem_anchor_token(')');
1991 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1992 typeof_type->typeoft.expression = expression;
1993 typeof_type->typeoft.typeof_type = type;
2001 SPECIFIER_SIGNED = 1 << 0,
2002 SPECIFIER_UNSIGNED = 1 << 1,
2003 SPECIFIER_LONG = 1 << 2,
2004 SPECIFIER_INT = 1 << 3,
2005 SPECIFIER_DOUBLE = 1 << 4,
2006 SPECIFIER_CHAR = 1 << 5,
2007 SPECIFIER_SHORT = 1 << 6,
2008 SPECIFIER_LONG_LONG = 1 << 7,
2009 SPECIFIER_FLOAT = 1 << 8,
2010 SPECIFIER_BOOL = 1 << 9,
2011 SPECIFIER_VOID = 1 << 10,
2012 SPECIFIER_INT8 = 1 << 11,
2013 SPECIFIER_INT16 = 1 << 12,
2014 SPECIFIER_INT32 = 1 << 13,
2015 SPECIFIER_INT64 = 1 << 14,
2016 SPECIFIER_INT128 = 1 << 15,
2017 #ifdef PROVIDE_COMPLEX
2018 SPECIFIER_COMPLEX = 1 << 16,
2019 SPECIFIER_IMAGINARY = 1 << 17,
2023 static type_t *create_builtin_type(symbol_t *const symbol,
2024 type_t *const real_type)
2026 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
2027 type->builtin.symbol = symbol;
2028 type->builtin.real_type = real_type;
2030 type_t *result = typehash_insert(type);
2031 if (type != result) {
2038 static type_t *get_typedef_type(symbol_t *symbol)
2040 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2041 if(declaration == NULL
2042 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2045 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
2046 type->typedeft.declaration = declaration;
2052 * check for the allowed MS alignment values.
2054 static bool check_elignment_value(long long intvalue) {
2055 if(intvalue < 1 || intvalue > 8192) {
2056 errorf(HERE, "illegal alignment value");
2059 unsigned v = (unsigned)intvalue;
2060 for(unsigned i = 1; i <= 8192; i += i) {
2064 errorf(HERE, "alignment must be power of two");
2068 #define DET_MOD(name, tag) do { \
2069 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2070 *modifiers |= tag; \
2073 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2075 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2078 if(token.type == T_restrict) {
2080 DET_MOD(restrict, DM_RESTRICT);
2082 } else if(token.type != T_IDENTIFIER)
2084 symbol_t *symbol = token.v.symbol;
2085 if(symbol == sym_align) {
2088 if(token.type != T_INTEGER)
2090 if(check_elignment_value(token.v.intvalue)) {
2091 if(specifiers->alignment != 0)
2092 warningf(HERE, "align used more than once");
2093 specifiers->alignment = (unsigned char)token.v.intvalue;
2097 } else if(symbol == sym_allocate) {
2100 if(token.type != T_IDENTIFIER)
2102 (void)token.v.symbol;
2104 } else if(symbol == sym_dllimport) {
2106 DET_MOD(dllimport, DM_DLLIMPORT);
2107 } else if(symbol == sym_dllexport) {
2109 DET_MOD(dllexport, DM_DLLEXPORT);
2110 } else if(symbol == sym_thread) {
2112 DET_MOD(thread, DM_THREAD);
2113 } else if(symbol == sym_naked) {
2115 DET_MOD(naked, DM_NAKED);
2116 } else if(symbol == sym_noinline) {
2118 DET_MOD(noinline, DM_NOINLINE);
2119 } else if(symbol == sym_noreturn) {
2121 DET_MOD(noreturn, DM_NORETURN);
2122 } else if(symbol == sym_nothrow) {
2124 DET_MOD(nothrow, DM_NOTHROW);
2125 } else if(symbol == sym_novtable) {
2127 DET_MOD(novtable, DM_NOVTABLE);
2128 } else if(symbol == sym_property) {
2132 bool is_get = false;
2133 if(token.type != T_IDENTIFIER)
2135 if(token.v.symbol == sym_get) {
2137 } else if(token.v.symbol == sym_put) {
2139 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2144 if(token.type != T_IDENTIFIER)
2147 if(specifiers->get_property_sym != NULL) {
2148 errorf(HERE, "get property name already specified");
2150 specifiers->get_property_sym = token.v.symbol;
2153 if(specifiers->put_property_sym != NULL) {
2154 errorf(HERE, "put property name already specified");
2156 specifiers->put_property_sym = token.v.symbol;
2160 if(token.type == ',') {
2167 } else if(symbol == sym_selectany) {
2169 DET_MOD(selectany, DM_SELECTANY);
2170 } else if(symbol == sym_uuid) {
2173 if(token.type != T_STRING_LITERAL)
2177 } else if(symbol == sym_deprecated) {
2179 if(specifiers->deprecated != 0)
2180 warningf(HERE, "deprecated used more than once");
2181 specifiers->deprecated = 1;
2182 if(token.type == '(') {
2184 if(token.type == T_STRING_LITERAL) {
2185 specifiers->deprecated_string = token.v.string.begin;
2188 errorf(HERE, "string literal expected");
2192 } else if(symbol == sym_noalias) {
2194 DET_MOD(noalias, DM_NOALIAS);
2196 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2198 if(token.type == '(')
2202 if (token.type == ',')
2209 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2211 type_t *type = NULL;
2212 unsigned type_qualifiers = 0;
2213 unsigned type_specifiers = 0;
2216 specifiers->source_position = token.source_position;
2219 switch(token.type) {
2222 #define MATCH_STORAGE_CLASS(token, class) \
2224 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2225 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2227 specifiers->declared_storage_class = class; \
2231 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2232 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2233 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2234 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2235 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2240 add_anchor_token(')');
2241 parse_microsoft_extended_decl_modifier(specifiers);
2242 rem_anchor_token(')');
2247 switch (specifiers->declared_storage_class) {
2248 case STORAGE_CLASS_NONE:
2249 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2252 case STORAGE_CLASS_EXTERN:
2253 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2256 case STORAGE_CLASS_STATIC:
2257 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2261 errorf(HERE, "multiple storage classes in declaration specifiers");
2267 /* type qualifiers */
2268 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2270 type_qualifiers |= qualifier; \
2274 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2275 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2276 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2277 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2278 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2279 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2280 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2281 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2283 case T___extension__:
2288 /* type specifiers */
2289 #define MATCH_SPECIFIER(token, specifier, name) \
2292 if(type_specifiers & specifier) { \
2293 errorf(HERE, "multiple " name " type specifiers given"); \
2295 type_specifiers |= specifier; \
2299 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2300 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2301 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2302 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2303 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2304 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2305 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2306 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2307 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2308 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2309 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2310 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2311 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2312 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2313 #ifdef PROVIDE_COMPLEX
2314 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2315 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2317 case T__forceinline:
2318 /* only in microsoft mode */
2319 specifiers->decl_modifiers |= DM_FORCEINLINE;
2323 specifiers->is_inline = true;
2328 if(type_specifiers & SPECIFIER_LONG_LONG) {
2329 errorf(HERE, "multiple type specifiers given");
2330 } else if(type_specifiers & SPECIFIER_LONG) {
2331 type_specifiers |= SPECIFIER_LONG_LONG;
2333 type_specifiers |= SPECIFIER_LONG;
2338 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2340 type->compound.declaration = parse_compound_type_specifier(true);
2344 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2346 type->compound.declaration = parse_compound_type_specifier(false);
2350 type = parse_enum_specifier();
2353 type = parse_typeof();
2355 case T___builtin_va_list:
2356 type = duplicate_type(type_valist);
2360 case T___attribute__:
2364 case T_IDENTIFIER: {
2365 /* only parse identifier if we haven't found a type yet */
2366 if(type != NULL || type_specifiers != 0)
2367 goto finish_specifiers;
2369 type_t *typedef_type = get_typedef_type(token.v.symbol);
2371 if(typedef_type == NULL)
2372 goto finish_specifiers;
2375 type = typedef_type;
2379 /* function specifier */
2381 goto finish_specifiers;
2388 atomic_type_kind_t atomic_type;
2390 /* match valid basic types */
2391 switch(type_specifiers) {
2392 case SPECIFIER_VOID:
2393 atomic_type = ATOMIC_TYPE_VOID;
2395 case SPECIFIER_CHAR:
2396 atomic_type = ATOMIC_TYPE_CHAR;
2398 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2399 atomic_type = ATOMIC_TYPE_SCHAR;
2401 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2402 atomic_type = ATOMIC_TYPE_UCHAR;
2404 case SPECIFIER_SHORT:
2405 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2406 case SPECIFIER_SHORT | SPECIFIER_INT:
2407 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2408 atomic_type = ATOMIC_TYPE_SHORT;
2410 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2411 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2412 atomic_type = ATOMIC_TYPE_USHORT;
2415 case SPECIFIER_SIGNED:
2416 case SPECIFIER_SIGNED | SPECIFIER_INT:
2417 atomic_type = ATOMIC_TYPE_INT;
2419 case SPECIFIER_UNSIGNED:
2420 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2421 atomic_type = ATOMIC_TYPE_UINT;
2423 case SPECIFIER_LONG:
2424 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2425 case SPECIFIER_LONG | SPECIFIER_INT:
2426 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2427 atomic_type = ATOMIC_TYPE_LONG;
2429 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2430 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2431 atomic_type = ATOMIC_TYPE_ULONG;
2433 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2434 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2435 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2436 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2438 atomic_type = ATOMIC_TYPE_LONGLONG;
2440 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2441 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2443 atomic_type = ATOMIC_TYPE_ULONGLONG;
2446 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
2447 atomic_type = unsigned_int8_type_kind;
2450 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
2451 atomic_type = unsigned_int16_type_kind;
2454 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
2455 atomic_type = unsigned_int32_type_kind;
2458 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
2459 atomic_type = unsigned_int64_type_kind;
2462 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
2463 atomic_type = unsigned_int128_type_kind;
2466 case SPECIFIER_INT8:
2467 case SPECIFIER_SIGNED | SPECIFIER_INT8:
2468 atomic_type = int8_type_kind;
2471 case SPECIFIER_INT16:
2472 case SPECIFIER_SIGNED | SPECIFIER_INT16:
2473 atomic_type = int16_type_kind;
2476 case SPECIFIER_INT32:
2477 case SPECIFIER_SIGNED | SPECIFIER_INT32:
2478 atomic_type = int32_type_kind;
2481 case SPECIFIER_INT64:
2482 case SPECIFIER_SIGNED | SPECIFIER_INT64:
2483 atomic_type = int64_type_kind;
2486 case SPECIFIER_INT128:
2487 case SPECIFIER_SIGNED | SPECIFIER_INT128:
2488 atomic_type = int128_type_kind;
2491 case SPECIFIER_FLOAT:
2492 atomic_type = ATOMIC_TYPE_FLOAT;
2494 case SPECIFIER_DOUBLE:
2495 atomic_type = ATOMIC_TYPE_DOUBLE;
2497 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2498 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2500 case SPECIFIER_BOOL:
2501 atomic_type = ATOMIC_TYPE_BOOL;
2503 #ifdef PROVIDE_COMPLEX
2504 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2505 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2507 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2508 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2510 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2511 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2513 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2514 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2516 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2517 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2519 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2520 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2524 /* invalid specifier combination, give an error message */
2525 if(type_specifiers == 0) {
2526 if (! strict_mode) {
2527 if (warning.implicit_int) {
2528 warningf(HERE, "no type specifiers in declaration, using 'int'");
2530 atomic_type = ATOMIC_TYPE_INT;
2533 errorf(HERE, "no type specifiers given in declaration");
2535 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2536 (type_specifiers & SPECIFIER_UNSIGNED)) {
2537 errorf(HERE, "signed and unsigned specifiers gives");
2538 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2539 errorf(HERE, "only integer types can be signed or unsigned");
2541 errorf(HERE, "multiple datatypes in declaration");
2543 atomic_type = ATOMIC_TYPE_INVALID;
2546 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2547 type->atomic.akind = atomic_type;
2550 if(type_specifiers != 0) {
2551 errorf(HERE, "multiple datatypes in declaration");
2555 type->base.qualifiers = type_qualifiers;
2556 /* FIXME: check type qualifiers here */
2558 type_t *result = typehash_insert(type);
2559 if(newtype && result != type) {
2563 specifiers->type = result;
2568 static type_qualifiers_t parse_type_qualifiers(void)
2570 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2573 switch(token.type) {
2574 /* type qualifiers */
2575 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2576 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2577 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2578 /* microsoft extended type modifiers */
2579 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2580 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2581 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2582 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2583 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2586 return type_qualifiers;
2591 static declaration_t *parse_identifier_list(void)
2593 declaration_t *declarations = NULL;
2594 declaration_t *last_declaration = NULL;
2596 declaration_t *const declaration = allocate_declaration_zero();
2597 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2598 declaration->source_position = token.source_position;
2599 declaration->symbol = token.v.symbol;
2602 if(last_declaration != NULL) {
2603 last_declaration->next = declaration;
2605 declarations = declaration;
2607 last_declaration = declaration;
2609 if(token.type != ',')
2612 } while(token.type == T_IDENTIFIER);
2614 return declarations;
2617 static void semantic_parameter(declaration_t *declaration)
2619 /* TODO: improve error messages */
2621 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2622 errorf(HERE, "typedef not allowed in parameter list");
2623 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2624 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2625 errorf(HERE, "parameter may only have none or register storage class");
2628 type_t *const orig_type = declaration->type;
2629 type_t * type = skip_typeref(orig_type);
2631 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2632 * into a pointer. § 6.7.5.3 (7) */
2633 if (is_type_array(type)) {
2634 type_t *const element_type = type->array.element_type;
2636 type = make_pointer_type(element_type, type->base.qualifiers);
2638 declaration->type = type;
2641 if(is_type_incomplete(type)) {
2642 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2643 orig_type, declaration->symbol);
2647 static declaration_t *parse_parameter(void)
2649 declaration_specifiers_t specifiers;
2650 memset(&specifiers, 0, sizeof(specifiers));
2652 parse_declaration_specifiers(&specifiers);
2654 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2656 semantic_parameter(declaration);
2661 static declaration_t *parse_parameters(function_type_t *type)
2663 if(token.type == T_IDENTIFIER) {
2664 symbol_t *symbol = token.v.symbol;
2665 if(!is_typedef_symbol(symbol)) {
2666 type->kr_style_parameters = true;
2667 return parse_identifier_list();
2671 if(token.type == ')') {
2672 type->unspecified_parameters = 1;
2675 if(token.type == T_void && look_ahead(1)->type == ')') {
2680 declaration_t *declarations = NULL;
2681 declaration_t *declaration;
2682 declaration_t *last_declaration = NULL;
2683 function_parameter_t *parameter;
2684 function_parameter_t *last_parameter = NULL;
2687 switch(token.type) {
2691 return declarations;
2694 case T___extension__:
2696 declaration = parse_parameter();
2698 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2699 memset(parameter, 0, sizeof(parameter[0]));
2700 parameter->type = declaration->type;
2702 if(last_parameter != NULL) {
2703 last_declaration->next = declaration;
2704 last_parameter->next = parameter;
2706 type->parameters = parameter;
2707 declarations = declaration;
2709 last_parameter = parameter;
2710 last_declaration = declaration;
2714 return declarations;
2716 if(token.type != ',')
2717 return declarations;
2727 } construct_type_kind_t;
2729 typedef struct construct_type_t construct_type_t;
2730 struct construct_type_t {
2731 construct_type_kind_t kind;
2732 construct_type_t *next;
2735 typedef struct parsed_pointer_t parsed_pointer_t;
2736 struct parsed_pointer_t {
2737 construct_type_t construct_type;
2738 type_qualifiers_t type_qualifiers;
2741 typedef struct construct_function_type_t construct_function_type_t;
2742 struct construct_function_type_t {
2743 construct_type_t construct_type;
2744 type_t *function_type;
2747 typedef struct parsed_array_t parsed_array_t;
2748 struct parsed_array_t {
2749 construct_type_t construct_type;
2750 type_qualifiers_t type_qualifiers;
2756 typedef struct construct_base_type_t construct_base_type_t;
2757 struct construct_base_type_t {
2758 construct_type_t construct_type;
2762 static construct_type_t *parse_pointer_declarator(void)
2766 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2767 memset(pointer, 0, sizeof(pointer[0]));
2768 pointer->construct_type.kind = CONSTRUCT_POINTER;
2769 pointer->type_qualifiers = parse_type_qualifiers();
2771 return (construct_type_t*) pointer;
2774 static construct_type_t *parse_array_declarator(void)
2777 add_anchor_token(']');
2779 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2780 memset(array, 0, sizeof(array[0]));
2781 array->construct_type.kind = CONSTRUCT_ARRAY;
2783 if(token.type == T_static) {
2784 array->is_static = true;
2788 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2789 if(type_qualifiers != 0) {
2790 if(token.type == T_static) {
2791 array->is_static = true;
2795 array->type_qualifiers = type_qualifiers;
2797 if(token.type == '*' && look_ahead(1)->type == ']') {
2798 array->is_variable = true;
2800 } else if(token.type != ']') {
2801 array->size = parse_assignment_expression();
2804 rem_anchor_token(']');
2807 return (construct_type_t*) array;
2812 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2815 add_anchor_token(')');
2818 if(declaration != NULL) {
2819 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2821 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2824 declaration_t *parameters = parse_parameters(&type->function);
2825 if(declaration != NULL) {
2826 declaration->scope.declarations = parameters;
2829 construct_function_type_t *construct_function_type =
2830 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2831 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2832 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2833 construct_function_type->function_type = type;
2835 rem_anchor_token(')');
2839 return (construct_type_t*) construct_function_type;
2842 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2843 bool may_be_abstract)
2845 /* construct a single linked list of construct_type_t's which describe
2846 * how to construct the final declarator type */
2847 construct_type_t *first = NULL;
2848 construct_type_t *last = NULL;
2851 while(token.type == '*') {
2852 construct_type_t *type = parse_pointer_declarator();
2863 /* TODO: find out if this is correct */
2866 construct_type_t *inner_types = NULL;
2868 switch(token.type) {
2870 if(declaration == NULL) {
2871 errorf(HERE, "no identifier expected in typename");
2873 declaration->symbol = token.v.symbol;
2874 declaration->source_position = token.source_position;
2880 add_anchor_token(')');
2881 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2882 rem_anchor_token(')');
2888 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2889 /* avoid a loop in the outermost scope, because eat_statement doesn't
2891 if(token.type == '}' && current_function == NULL) {
2899 construct_type_t *p = last;
2902 construct_type_t *type;
2903 switch(token.type) {
2905 type = parse_function_declarator(declaration);
2908 type = parse_array_declarator();
2911 goto declarator_finished;
2914 /* insert in the middle of the list (behind p) */
2916 type->next = p->next;
2927 declarator_finished:
2930 /* append inner_types at the end of the list, we don't to set last anymore
2931 * as it's not needed anymore */
2933 assert(first == NULL);
2934 first = inner_types;
2936 last->next = inner_types;
2944 static type_t *construct_declarator_type(construct_type_t *construct_list,
2947 construct_type_t *iter = construct_list;
2948 for( ; iter != NULL; iter = iter->next) {
2949 switch(iter->kind) {
2950 case CONSTRUCT_INVALID:
2951 internal_errorf(HERE, "invalid type construction found");
2952 case CONSTRUCT_FUNCTION: {
2953 construct_function_type_t *construct_function_type
2954 = (construct_function_type_t*) iter;
2956 type_t *function_type = construct_function_type->function_type;
2958 function_type->function.return_type = type;
2960 type_t *skipped_return_type = skip_typeref(type);
2961 if (is_type_function(skipped_return_type)) {
2962 errorf(HERE, "function returning function is not allowed");
2963 type = type_error_type;
2964 } else if (is_type_array(skipped_return_type)) {
2965 errorf(HERE, "function returning array is not allowed");
2966 type = type_error_type;
2968 type = function_type;
2973 case CONSTRUCT_POINTER: {
2974 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2975 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2976 pointer_type->pointer.points_to = type;
2977 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2979 type = pointer_type;
2983 case CONSTRUCT_ARRAY: {
2984 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2985 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2987 expression_t *size_expression = parsed_array->size;
2988 if(size_expression != NULL) {
2990 = create_implicit_cast(size_expression, type_size_t);
2993 array_type->base.qualifiers = parsed_array->type_qualifiers;
2994 array_type->array.element_type = type;
2995 array_type->array.is_static = parsed_array->is_static;
2996 array_type->array.is_variable = parsed_array->is_variable;
2997 array_type->array.size_expression = size_expression;
2999 if(size_expression != NULL) {
3000 if(is_constant_expression(size_expression)) {
3001 array_type->array.size_constant = true;
3002 array_type->array.size
3003 = fold_constant(size_expression);
3005 array_type->array.is_vla = true;
3009 type_t *skipped_type = skip_typeref(type);
3010 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3011 errorf(HERE, "array of void is not allowed");
3012 type = type_error_type;
3020 type_t *hashed_type = typehash_insert(type);
3021 if(hashed_type != type) {
3022 /* the function type was constructed earlier freeing it here will
3023 * destroy other types... */
3024 if(iter->kind != CONSTRUCT_FUNCTION) {
3034 static declaration_t *parse_declarator(
3035 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3037 declaration_t *const declaration = allocate_declaration_zero();
3038 declaration->declared_storage_class = specifiers->declared_storage_class;
3039 declaration->modifiers = specifiers->decl_modifiers;
3040 declaration->deprecated = specifiers->deprecated;
3041 declaration->deprecated_string = specifiers->deprecated_string;
3042 declaration->get_property_sym = specifiers->get_property_sym;
3043 declaration->put_property_sym = specifiers->put_property_sym;
3044 declaration->is_inline = specifiers->is_inline;
3046 declaration->storage_class = specifiers->declared_storage_class;
3047 if(declaration->storage_class == STORAGE_CLASS_NONE
3048 && scope != global_scope) {
3049 declaration->storage_class = STORAGE_CLASS_AUTO;
3052 if(specifiers->alignment != 0) {
3053 /* TODO: add checks here */
3054 declaration->alignment = specifiers->alignment;
3057 construct_type_t *construct_type
3058 = parse_inner_declarator(declaration, may_be_abstract);
3059 type_t *const type = specifiers->type;
3060 declaration->type = construct_declarator_type(construct_type, type);
3062 if(construct_type != NULL) {
3063 obstack_free(&temp_obst, construct_type);
3069 static type_t *parse_abstract_declarator(type_t *base_type)
3071 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3073 type_t *result = construct_declarator_type(construct_type, base_type);
3074 if(construct_type != NULL) {
3075 obstack_free(&temp_obst, construct_type);
3081 static declaration_t *append_declaration(declaration_t* const declaration)
3083 if (last_declaration != NULL) {
3084 last_declaration->next = declaration;
3086 scope->declarations = declaration;
3088 last_declaration = declaration;
3093 * Check if the declaration of main is suspicious. main should be a
3094 * function with external linkage, returning int, taking either zero
3095 * arguments, two, or three arguments of appropriate types, ie.
3097 * int main([ int argc, char **argv [, char **env ] ]).
3099 * @param decl the declaration to check
3100 * @param type the function type of the declaration
3102 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3104 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3105 warningf(decl->source_position, "'main' is normally a non-static function");
3107 if (skip_typeref(func_type->return_type) != type_int) {
3108 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
3110 const function_parameter_t *parm = func_type->parameters;
3112 type_t *const first_type = parm->type;
3113 if (!types_compatible(skip_typeref(first_type), type_int)) {
3114 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
3118 type_t *const second_type = parm->type;
3119 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3120 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
3124 type_t *const third_type = parm->type;
3125 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3126 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
3130 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3134 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3140 * Check if a symbol is the equal to "main".
3142 static bool is_sym_main(const symbol_t *const sym)
3144 return strcmp(sym->string, "main") == 0;
3147 static declaration_t *internal_record_declaration(
3148 declaration_t *const declaration,
3149 const bool is_function_definition)
3151 const symbol_t *const symbol = declaration->symbol;
3152 const namespace_t namespc = (namespace_t)declaration->namespc;
3154 type_t *const orig_type = declaration->type;
3155 type_t *const type = skip_typeref(orig_type);
3156 if (is_type_function(type) &&
3157 type->function.unspecified_parameters &&
3158 warning.strict_prototypes) {
3159 warningf(declaration->source_position,
3160 "function declaration '%#T' is not a prototype",
3161 orig_type, declaration->symbol);
3164 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3165 check_type_of_main(declaration, &type->function);
3168 assert(declaration->symbol != NULL);
3169 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3171 assert(declaration != previous_declaration);
3172 if (previous_declaration != NULL) {
3173 if (previous_declaration->parent_scope == scope) {
3174 /* can happen for K&R style declarations */
3175 if(previous_declaration->type == NULL) {
3176 previous_declaration->type = declaration->type;
3179 const type_t *prev_type = skip_typeref(previous_declaration->type);
3180 if (!types_compatible(type, prev_type)) {
3181 errorf(declaration->source_position,
3182 "declaration '%#T' is incompatible with "
3183 "previous declaration '%#T'",
3184 orig_type, symbol, previous_declaration->type, symbol);
3185 errorf(previous_declaration->source_position,
3186 "previous declaration of '%Y' was here", symbol);
3188 unsigned old_storage_class = previous_declaration->storage_class;
3189 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3190 errorf(declaration->source_position, "redeclaration of enum entry '%Y'", symbol);
3191 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
3192 return previous_declaration;
3195 unsigned new_storage_class = declaration->storage_class;
3197 if(is_type_incomplete(prev_type)) {
3198 previous_declaration->type = type;
3202 /* pretend no storage class means extern for function
3203 * declarations (except if the previous declaration is neither
3204 * none nor extern) */
3205 if (is_type_function(type)) {
3206 switch (old_storage_class) {
3207 case STORAGE_CLASS_NONE:
3208 old_storage_class = STORAGE_CLASS_EXTERN;
3210 case STORAGE_CLASS_EXTERN:
3211 if (is_function_definition) {
3212 if (warning.missing_prototypes &&
3213 prev_type->function.unspecified_parameters &&
3214 !is_sym_main(symbol)) {
3215 warningf(declaration->source_position,
3216 "no previous prototype for '%#T'",
3219 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3220 new_storage_class = STORAGE_CLASS_EXTERN;
3228 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3229 new_storage_class == STORAGE_CLASS_EXTERN) {
3230 warn_redundant_declaration:
3231 if (warning.redundant_decls) {
3232 warningf(declaration->source_position,
3233 "redundant declaration for '%Y'", symbol);
3234 warningf(previous_declaration->source_position,
3235 "previous declaration of '%Y' was here",
3238 } else if (current_function == NULL) {
3239 if (old_storage_class != STORAGE_CLASS_STATIC &&
3240 new_storage_class == STORAGE_CLASS_STATIC) {
3241 errorf(declaration->source_position,
3242 "static declaration of '%Y' follows non-static declaration",
3244 errorf(previous_declaration->source_position,
3245 "previous declaration of '%Y' was here", symbol);
3247 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3248 goto warn_redundant_declaration;
3250 if (new_storage_class == STORAGE_CLASS_NONE) {
3251 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3252 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3256 if (old_storage_class == new_storage_class) {
3257 errorf(declaration->source_position,
3258 "redeclaration of '%Y'", symbol);
3260 errorf(declaration->source_position,
3261 "redeclaration of '%Y' with different linkage",
3264 errorf(previous_declaration->source_position,
3265 "previous declaration of '%Y' was here", symbol);
3268 return previous_declaration;
3270 } else if (is_function_definition) {
3271 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3272 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3273 warningf(declaration->source_position,
3274 "no previous prototype for '%#T'", orig_type, symbol);
3275 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3276 warningf(declaration->source_position,
3277 "no previous declaration for '%#T'", orig_type,
3281 } else if (warning.missing_declarations &&
3282 scope == global_scope &&
3283 !is_type_function(type) && (
3284 declaration->storage_class == STORAGE_CLASS_NONE ||
3285 declaration->storage_class == STORAGE_CLASS_THREAD
3287 warningf(declaration->source_position,
3288 "no previous declaration for '%#T'", orig_type, symbol);
3291 assert(declaration->parent_scope == NULL);
3292 assert(scope != NULL);
3294 declaration->parent_scope = scope;
3296 environment_push(declaration);
3297 return append_declaration(declaration);
3300 static declaration_t *record_declaration(declaration_t *declaration)
3302 return internal_record_declaration(declaration, false);
3305 static declaration_t *record_function_definition(declaration_t *declaration)
3307 return internal_record_declaration(declaration, true);
3310 static void parser_error_multiple_definition(declaration_t *declaration,
3311 const source_position_t source_position)
3313 errorf(source_position, "multiple definition of symbol '%Y'",
3314 declaration->symbol);
3315 errorf(declaration->source_position,
3316 "this is the location of the previous definition.");
3319 static bool is_declaration_specifier(const token_t *token,
3320 bool only_type_specifiers)
3322 switch(token->type) {
3326 return is_typedef_symbol(token->v.symbol);
3328 case T___extension__:
3331 return !only_type_specifiers;
3338 static void parse_init_declarator_rest(declaration_t *declaration)
3342 type_t *orig_type = declaration->type;
3343 type_t *type = skip_typeref(orig_type);
3345 if(declaration->init.initializer != NULL) {
3346 parser_error_multiple_definition(declaration, token.source_position);
3349 bool must_be_constant = false;
3350 if(declaration->storage_class == STORAGE_CLASS_STATIC
3351 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3352 || declaration->parent_scope == global_scope) {
3353 must_be_constant = true;
3356 parse_initializer_env_t env;
3357 env.type = orig_type;
3358 env.must_be_constant = must_be_constant;
3359 env.declaration = declaration;
3361 initializer_t *initializer = parse_initializer(&env);
3363 if(env.type != orig_type) {
3364 orig_type = env.type;
3365 type = skip_typeref(orig_type);
3366 declaration->type = env.type;
3369 if(is_type_function(type)) {
3370 errorf(declaration->source_position,
3371 "initializers not allowed for function types at declator '%Y' (type '%T')",
3372 declaration->symbol, orig_type);
3374 declaration->init.initializer = initializer;
3378 /* parse rest of a declaration without any declarator */
3379 static void parse_anonymous_declaration_rest(
3380 const declaration_specifiers_t *specifiers,
3381 parsed_declaration_func finished_declaration)
3385 declaration_t *const declaration = allocate_declaration_zero();
3386 declaration->type = specifiers->type;
3387 declaration->declared_storage_class = specifiers->declared_storage_class;
3388 declaration->source_position = specifiers->source_position;
3389 declaration->modifiers = specifiers->decl_modifiers;
3391 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3392 warningf(declaration->source_position, "useless storage class in empty declaration");
3394 declaration->storage_class = STORAGE_CLASS_NONE;
3396 type_t *type = declaration->type;
3397 switch (type->kind) {
3398 case TYPE_COMPOUND_STRUCT:
3399 case TYPE_COMPOUND_UNION: {
3400 if (type->compound.declaration->symbol == NULL) {
3401 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
3410 warningf(declaration->source_position, "empty declaration");
3414 finished_declaration(declaration);
3417 static void parse_declaration_rest(declaration_t *ndeclaration,
3418 const declaration_specifiers_t *specifiers,
3419 parsed_declaration_func finished_declaration)
3421 add_anchor_token(';');
3422 add_anchor_token('=');
3423 add_anchor_token(',');
3425 declaration_t *declaration = finished_declaration(ndeclaration);
3427 type_t *orig_type = declaration->type;
3428 type_t *type = skip_typeref(orig_type);
3430 if (type->kind != TYPE_FUNCTION &&
3431 declaration->is_inline &&
3432 is_type_valid(type)) {
3433 warningf(declaration->source_position,
3434 "variable '%Y' declared 'inline'\n", declaration->symbol);
3437 if(token.type == '=') {
3438 parse_init_declarator_rest(declaration);
3441 if(token.type != ',')
3445 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
3450 rem_anchor_token(';');
3451 rem_anchor_token('=');
3452 rem_anchor_token(',');
3455 static declaration_t *finished_kr_declaration(declaration_t *declaration)
3457 symbol_t *symbol = declaration->symbol;
3458 if(symbol == NULL) {
3459 errorf(HERE, "anonymous declaration not valid as function parameter");
3462 namespace_t namespc = (namespace_t) declaration->namespc;
3463 if(namespc != NAMESPACE_NORMAL) {
3464 return record_declaration(declaration);
3467 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3468 if(previous_declaration == NULL ||
3469 previous_declaration->parent_scope != scope) {
3470 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3475 if(previous_declaration->type == NULL) {
3476 previous_declaration->type = declaration->type;
3477 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3478 previous_declaration->storage_class = declaration->storage_class;
3479 previous_declaration->parent_scope = scope;
3480 return previous_declaration;
3482 return record_declaration(declaration);
3486 static void parse_declaration(parsed_declaration_func finished_declaration)
3488 declaration_specifiers_t specifiers;
3489 memset(&specifiers, 0, sizeof(specifiers));
3490 parse_declaration_specifiers(&specifiers);
3492 if(token.type == ';') {
3493 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3495 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3496 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3500 static void parse_kr_declaration_list(declaration_t *declaration)
3502 type_t *type = skip_typeref(declaration->type);
3503 if(!is_type_function(type))
3506 if(!type->function.kr_style_parameters)
3509 /* push function parameters */
3510 int top = environment_top();
3511 scope_t *last_scope = scope;
3512 set_scope(&declaration->scope);
3514 declaration_t *parameter = declaration->scope.declarations;
3515 for( ; parameter != NULL; parameter = parameter->next) {
3516 assert(parameter->parent_scope == NULL);
3517 parameter->parent_scope = scope;
3518 environment_push(parameter);
3521 /* parse declaration list */
3522 while(is_declaration_specifier(&token, false)) {
3523 parse_declaration(finished_kr_declaration);
3526 /* pop function parameters */
3527 assert(scope == &declaration->scope);
3528 set_scope(last_scope);
3529 environment_pop_to(top);
3531 /* update function type */
3532 type_t *new_type = duplicate_type(type);
3533 new_type->function.kr_style_parameters = false;
3535 function_parameter_t *parameters = NULL;
3536 function_parameter_t *last_parameter = NULL;
3538 declaration_t *parameter_declaration = declaration->scope.declarations;
3539 for( ; parameter_declaration != NULL;
3540 parameter_declaration = parameter_declaration->next) {
3541 type_t *parameter_type = parameter_declaration->type;
3542 if(parameter_type == NULL) {
3544 errorf(HERE, "no type specified for function parameter '%Y'",
3545 parameter_declaration->symbol);
3547 if (warning.implicit_int) {
3548 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3549 parameter_declaration->symbol);
3551 parameter_type = type_int;
3552 parameter_declaration->type = parameter_type;
3556 semantic_parameter(parameter_declaration);
3557 parameter_type = parameter_declaration->type;
3559 function_parameter_t *function_parameter
3560 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3561 memset(function_parameter, 0, sizeof(function_parameter[0]));
3563 function_parameter->type = parameter_type;
3564 if(last_parameter != NULL) {
3565 last_parameter->next = function_parameter;
3567 parameters = function_parameter;
3569 last_parameter = function_parameter;
3571 new_type->function.parameters = parameters;
3573 type = typehash_insert(new_type);
3574 if(type != new_type) {
3575 obstack_free(type_obst, new_type);
3578 declaration->type = type;
3581 static bool first_err = true;
3584 * When called with first_err set, prints the name of the current function,
3587 static void print_in_function(void) {
3590 diagnosticf("%s: In function '%Y':\n",
3591 current_function->source_position.input_name,
3592 current_function->symbol);
3597 * Check if all labels are defined in the current function.
3598 * Check if all labels are used in the current function.
3600 static void check_labels(void)
3602 for (const goto_statement_t *goto_statement = goto_first;
3603 goto_statement != NULL;
3604 goto_statement = goto_statement->next) {
3605 declaration_t *label = goto_statement->label;
3608 if (label->source_position.input_name == NULL) {
3609 print_in_function();
3610 errorf(goto_statement->base.source_position,
3611 "label '%Y' used but not defined", label->symbol);
3614 goto_first = goto_last = NULL;
3616 if (warning.unused_label) {
3617 for (const label_statement_t *label_statement = label_first;
3618 label_statement != NULL;
3619 label_statement = label_statement->next) {
3620 const declaration_t *label = label_statement->label;
3622 if (! label->used) {
3623 print_in_function();
3624 warningf(label_statement->base.source_position,
3625 "label '%Y' defined but not used", label->symbol);
3629 label_first = label_last = NULL;
3633 * Check declarations of current_function for unused entities.
3635 static void check_declarations(void)
3637 if (warning.unused_parameter) {
3638 const scope_t *scope = ¤t_function->scope;
3640 const declaration_t *parameter = scope->declarations;
3641 for (; parameter != NULL; parameter = parameter->next) {
3642 if (! parameter->used) {
3643 print_in_function();
3644 warningf(parameter->source_position,
3645 "unused parameter '%Y'", parameter->symbol);
3649 if (warning.unused_variable) {
3653 static void parse_external_declaration(void)
3655 /* function-definitions and declarations both start with declaration
3657 declaration_specifiers_t specifiers;
3658 memset(&specifiers, 0, sizeof(specifiers));
3660 add_anchor_token(';');
3661 parse_declaration_specifiers(&specifiers);
3662 rem_anchor_token(';');
3664 /* must be a declaration */
3665 if(token.type == ';') {
3666 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3670 add_anchor_token(',');
3671 add_anchor_token('=');
3672 rem_anchor_token(';');
3674 /* declarator is common to both function-definitions and declarations */
3675 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3677 rem_anchor_token(',');
3678 rem_anchor_token('=');
3679 rem_anchor_token(';');
3681 /* must be a declaration */
3682 if(token.type == ',' || token.type == '=' || token.type == ';') {
3683 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3687 /* must be a function definition */
3688 parse_kr_declaration_list(ndeclaration);
3690 if(token.type != '{') {
3691 parse_error_expected("while parsing function definition", '{', 0);
3692 eat_until_matching_token(';');
3696 type_t *type = ndeclaration->type;
3698 /* note that we don't skip typerefs: the standard doesn't allow them here
3699 * (so we can't use is_type_function here) */
3700 if(type->kind != TYPE_FUNCTION) {
3701 if (is_type_valid(type)) {
3702 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3703 type, ndeclaration->symbol);
3709 /* § 6.7.5.3 (14) a function definition with () means no
3710 * parameters (and not unspecified parameters) */
3711 if(type->function.unspecified_parameters) {
3712 type_t *duplicate = duplicate_type(type);
3713 duplicate->function.unspecified_parameters = false;
3715 type = typehash_insert(duplicate);
3716 if(type != duplicate) {
3717 obstack_free(type_obst, duplicate);
3719 ndeclaration->type = type;
3722 declaration_t *const declaration = record_function_definition(ndeclaration);
3723 if(ndeclaration != declaration) {
3724 declaration->scope = ndeclaration->scope;
3726 type = skip_typeref(declaration->type);
3728 /* push function parameters and switch scope */
3729 int top = environment_top();
3730 scope_t *last_scope = scope;
3731 set_scope(&declaration->scope);
3733 declaration_t *parameter = declaration->scope.declarations;
3734 for( ; parameter != NULL; parameter = parameter->next) {
3735 if(parameter->parent_scope == &ndeclaration->scope) {
3736 parameter->parent_scope = scope;
3738 assert(parameter->parent_scope == NULL
3739 || parameter->parent_scope == scope);
3740 parameter->parent_scope = scope;
3741 environment_push(parameter);
3744 if(declaration->init.statement != NULL) {
3745 parser_error_multiple_definition(declaration, token.source_position);
3747 goto end_of_parse_external_declaration;
3749 /* parse function body */
3750 int label_stack_top = label_top();
3751 declaration_t *old_current_function = current_function;
3752 current_function = declaration;
3754 declaration->init.statement = parse_compound_statement();
3757 check_declarations();
3759 assert(current_function == declaration);
3760 current_function = old_current_function;
3761 label_pop_to(label_stack_top);
3764 end_of_parse_external_declaration:
3765 assert(scope == &declaration->scope);
3766 set_scope(last_scope);
3767 environment_pop_to(top);
3770 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3771 source_position_t source_position)
3773 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3774 type->bitfield.base = base;
3775 type->bitfield.size = size;
3780 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3783 declaration_t *iter = compound_declaration->scope.declarations;
3784 for( ; iter != NULL; iter = iter->next) {
3785 if(iter->namespc != NAMESPACE_NORMAL)
3788 if(iter->symbol == NULL) {
3789 type_t *type = skip_typeref(iter->type);
3790 if(is_type_compound(type)) {
3791 declaration_t *result
3792 = find_compound_entry(type->compound.declaration, symbol);
3799 if(iter->symbol == symbol) {
3807 static void parse_compound_declarators(declaration_t *struct_declaration,
3808 const declaration_specifiers_t *specifiers)
3810 declaration_t *last_declaration = struct_declaration->scope.declarations;
3811 if(last_declaration != NULL) {
3812 while(last_declaration->next != NULL) {
3813 last_declaration = last_declaration->next;
3818 declaration_t *declaration;
3820 if(token.type == ':') {
3821 source_position_t source_position = HERE;
3824 type_t *base_type = specifiers->type;
3825 expression_t *size = parse_constant_expression();
3827 if(!is_type_integer(skip_typeref(base_type))) {
3828 errorf(HERE, "bitfield base type '%T' is not an integer type",
3832 type_t *type = make_bitfield_type(base_type, size, source_position);
3834 declaration = allocate_declaration_zero();
3835 declaration->namespc = NAMESPACE_NORMAL;
3836 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3837 declaration->storage_class = STORAGE_CLASS_NONE;
3838 declaration->source_position = source_position;
3839 declaration->modifiers = specifiers->decl_modifiers;
3840 declaration->type = type;
3842 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3844 type_t *orig_type = declaration->type;
3845 type_t *type = skip_typeref(orig_type);
3847 if(token.type == ':') {
3848 source_position_t source_position = HERE;
3850 expression_t *size = parse_constant_expression();
3852 if(!is_type_integer(type)) {
3853 errorf(HERE, "bitfield base type '%T' is not an "
3854 "integer type", orig_type);
3857 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3858 declaration->type = bitfield_type;
3860 /* TODO we ignore arrays for now... what is missing is a check
3861 * that they're at the end of the struct */
3862 if(is_type_incomplete(type) && !is_type_array(type)) {
3864 "compound member '%Y' has incomplete type '%T'",
3865 declaration->symbol, orig_type);
3866 } else if(is_type_function(type)) {
3867 errorf(HERE, "compound member '%Y' must not have function "
3868 "type '%T'", declaration->symbol, orig_type);
3873 /* make sure we don't define a symbol multiple times */
3874 symbol_t *symbol = declaration->symbol;
3875 if(symbol != NULL) {
3876 declaration_t *prev_decl
3877 = find_compound_entry(struct_declaration, symbol);
3879 if(prev_decl != NULL) {
3880 assert(prev_decl->symbol == symbol);
3881 errorf(declaration->source_position,
3882 "multiple declarations of symbol '%Y'", symbol);
3883 errorf(prev_decl->source_position,
3884 "previous declaration of '%Y' was here", symbol);
3888 /* append declaration */
3889 if(last_declaration != NULL) {
3890 last_declaration->next = declaration;
3892 struct_declaration->scope.declarations = declaration;
3894 last_declaration = declaration;
3896 if(token.type != ',')
3906 static void parse_compound_type_entries(declaration_t *compound_declaration)
3909 add_anchor_token('}');
3911 while(token.type != '}' && token.type != T_EOF) {
3912 declaration_specifiers_t specifiers;
3913 memset(&specifiers, 0, sizeof(specifiers));
3914 parse_declaration_specifiers(&specifiers);
3916 parse_compound_declarators(compound_declaration, &specifiers);
3918 rem_anchor_token('}');
3920 if(token.type == T_EOF) {
3921 errorf(HERE, "EOF while parsing struct");
3926 static type_t *parse_typename(void)
3928 declaration_specifiers_t specifiers;
3929 memset(&specifiers, 0, sizeof(specifiers));
3930 parse_declaration_specifiers(&specifiers);
3931 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3932 /* TODO: improve error message, user does probably not know what a
3933 * storage class is...
3935 errorf(HERE, "typename may not have a storage class");
3938 type_t *result = parse_abstract_declarator(specifiers.type);
3946 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3947 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3948 expression_t *left);
3950 typedef struct expression_parser_function_t expression_parser_function_t;
3951 struct expression_parser_function_t {
3952 unsigned precedence;
3953 parse_expression_function parser;
3954 unsigned infix_precedence;
3955 parse_expression_infix_function infix_parser;
3958 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3961 * Creates a new invalid expression.
3963 static expression_t *create_invalid_expression(void)
3965 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3966 expression->base.source_position = token.source_position;
3971 * Prints an error message if an expression was expected but not read
3973 static expression_t *expected_expression_error(void)
3975 /* skip the error message if the error token was read */
3976 if (token.type != T_ERROR) {
3977 errorf(HERE, "expected expression, got token '%K'", &token);
3981 return create_invalid_expression();
3985 * Parse a string constant.
3987 static expression_t *parse_string_const(void)
3990 if (token.type == T_STRING_LITERAL) {
3991 string_t res = token.v.string;
3993 while (token.type == T_STRING_LITERAL) {
3994 res = concat_strings(&res, &token.v.string);
3997 if (token.type != T_WIDE_STRING_LITERAL) {
3998 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3999 /* note: that we use type_char_ptr here, which is already the
4000 * automatic converted type. revert_automatic_type_conversion
4001 * will construct the array type */
4002 cnst->base.type = type_char_ptr;
4003 cnst->string.value = res;
4007 wres = concat_string_wide_string(&res, &token.v.wide_string);
4009 wres = token.v.wide_string;
4014 switch (token.type) {
4015 case T_WIDE_STRING_LITERAL:
4016 wres = concat_wide_strings(&wres, &token.v.wide_string);
4019 case T_STRING_LITERAL:
4020 wres = concat_wide_string_string(&wres, &token.v.string);
4024 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4025 cnst->base.type = type_wchar_t_ptr;
4026 cnst->wide_string.value = wres;
4035 * Parse an integer constant.
4037 static expression_t *parse_int_const(void)
4039 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4040 cnst->base.source_position = HERE;
4041 cnst->base.type = token.datatype;
4042 cnst->conste.v.int_value = token.v.intvalue;
4050 * Parse a character constant.
4052 static expression_t *parse_character_constant(void)
4054 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4056 cnst->base.source_position = HERE;
4057 cnst->base.type = token.datatype;
4058 cnst->conste.v.character = token.v.string;
4060 if (cnst->conste.v.character.size != 1) {
4061 if (warning.multichar && (c_mode & _GNUC)) {
4063 warningf(HERE, "multi-character character constant");
4065 errorf(HERE, "more than 1 characters in character constant");
4074 * Parse a wide character constant.
4076 static expression_t *parse_wide_character_constant(void)
4078 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4080 cnst->base.source_position = HERE;
4081 cnst->base.type = token.datatype;
4082 cnst->conste.v.wide_character = token.v.wide_string;
4084 if (cnst->conste.v.wide_character.size != 1) {
4085 if (warning.multichar && (c_mode & _GNUC)) {
4087 warningf(HERE, "multi-character character constant");
4089 errorf(HERE, "more than 1 characters in character constant");
4098 * Parse a float constant.
4100 static expression_t *parse_float_const(void)
4102 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4103 cnst->base.type = token.datatype;
4104 cnst->conste.v.float_value = token.v.floatvalue;
4111 static declaration_t *create_implicit_function(symbol_t *symbol,
4112 const source_position_t source_position)
4114 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4115 ntype->function.return_type = type_int;
4116 ntype->function.unspecified_parameters = true;
4118 type_t *type = typehash_insert(ntype);
4123 declaration_t *const declaration = allocate_declaration_zero();
4124 declaration->storage_class = STORAGE_CLASS_EXTERN;
4125 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4126 declaration->type = type;
4127 declaration->symbol = symbol;
4128 declaration->source_position = source_position;
4129 declaration->parent_scope = global_scope;
4131 scope_t *old_scope = scope;
4132 set_scope(global_scope);
4134 environment_push(declaration);
4135 /* prepends the declaration to the global declarations list */
4136 declaration->next = scope->declarations;
4137 scope->declarations = declaration;
4139 assert(scope == global_scope);
4140 set_scope(old_scope);
4146 * Creates a return_type (func)(argument_type) function type if not
4149 * @param return_type the return type
4150 * @param argument_type the argument type
4152 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4154 function_parameter_t *parameter
4155 = obstack_alloc(type_obst, sizeof(parameter[0]));
4156 memset(parameter, 0, sizeof(parameter[0]));
4157 parameter->type = argument_type;
4159 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
4160 type->function.return_type = return_type;
4161 type->function.parameters = parameter;
4163 type_t *result = typehash_insert(type);
4164 if(result != type) {
4172 * Creates a function type for some function like builtins.
4174 * @param symbol the symbol describing the builtin
4176 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4178 switch(symbol->ID) {
4179 case T___builtin_alloca:
4180 return make_function_1_type(type_void_ptr, type_size_t);
4181 case T___builtin_nan:
4182 return make_function_1_type(type_double, type_char_ptr);
4183 case T___builtin_nanf:
4184 return make_function_1_type(type_float, type_char_ptr);
4185 case T___builtin_nand:
4186 return make_function_1_type(type_long_double, type_char_ptr);
4187 case T___builtin_va_end:
4188 return make_function_1_type(type_void, type_valist);
4190 internal_errorf(HERE, "not implemented builtin symbol found");
4195 * Performs automatic type cast as described in § 6.3.2.1.
4197 * @param orig_type the original type
4199 static type_t *automatic_type_conversion(type_t *orig_type)
4201 type_t *type = skip_typeref(orig_type);
4202 if(is_type_array(type)) {
4203 array_type_t *array_type = &type->array;
4204 type_t *element_type = array_type->element_type;
4205 unsigned qualifiers = array_type->type.qualifiers;
4207 return make_pointer_type(element_type, qualifiers);
4210 if(is_type_function(type)) {
4211 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4218 * reverts the automatic casts of array to pointer types and function
4219 * to function-pointer types as defined § 6.3.2.1
4221 type_t *revert_automatic_type_conversion(const expression_t *expression)
4223 switch (expression->kind) {
4224 case EXPR_REFERENCE: return expression->reference.declaration->type;
4225 case EXPR_SELECT: return expression->select.compound_entry->type;
4227 case EXPR_UNARY_DEREFERENCE: {
4228 const expression_t *const value = expression->unary.value;
4229 type_t *const type = skip_typeref(value->base.type);
4230 assert(is_type_pointer(type));
4231 return type->pointer.points_to;
4234 case EXPR_BUILTIN_SYMBOL:
4235 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4237 case EXPR_ARRAY_ACCESS: {
4238 const expression_t *array_ref = expression->array_access.array_ref;
4239 type_t *type_left = skip_typeref(array_ref->base.type);
4240 if (!is_type_valid(type_left))
4242 assert(is_type_pointer(type_left));
4243 return type_left->pointer.points_to;
4246 case EXPR_STRING_LITERAL: {
4247 size_t size = expression->string.value.size;
4248 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4251 case EXPR_WIDE_STRING_LITERAL: {
4252 size_t size = expression->wide_string.value.size;
4253 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4256 case EXPR_COMPOUND_LITERAL:
4257 return expression->compound_literal.type;
4262 return expression->base.type;
4265 static expression_t *parse_reference(void)
4267 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4269 reference_expression_t *ref = &expression->reference;
4270 ref->symbol = token.v.symbol;
4272 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4274 source_position_t source_position = token.source_position;
4277 if(declaration == NULL) {
4278 if (! strict_mode && token.type == '(') {
4279 /* an implicitly defined function */
4280 if (warning.implicit_function_declaration) {
4281 warningf(HERE, "implicit declaration of function '%Y'",
4285 declaration = create_implicit_function(ref->symbol,
4288 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4289 return create_invalid_expression();
4293 type_t *type = declaration->type;
4295 /* we always do the auto-type conversions; the & and sizeof parser contains
4296 * code to revert this! */
4297 type = automatic_type_conversion(type);
4299 ref->declaration = declaration;
4300 ref->base.type = type;
4302 /* this declaration is used */
4303 declaration->used = true;
4305 /* check for deprecated functions */
4306 if(declaration->deprecated != 0) {
4307 const char *prefix = "";
4308 if (is_type_function(declaration->type))
4309 prefix = "function ";
4311 if (declaration->deprecated_string != NULL) {
4312 warningf(source_position,
4313 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4314 declaration->deprecated_string);
4316 warningf(source_position,
4317 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4324 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4328 /* TODO check if explicit cast is allowed and issue warnings/errors */
4331 static expression_t *parse_compound_literal(type_t *type)
4333 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4335 parse_initializer_env_t env;
4337 env.declaration = NULL;
4338 env.must_be_constant = false;
4339 initializer_t *initializer = parse_initializer(&env);
4342 expression->compound_literal.initializer = initializer;
4343 expression->compound_literal.type = type;
4344 expression->base.type = automatic_type_conversion(type);
4350 * Parse a cast expression.
4352 static expression_t *parse_cast(void)
4354 source_position_t source_position = token.source_position;
4356 type_t *type = parse_typename();
4358 /* matching add_anchor_token() is at call site */
4359 rem_anchor_token(')');
4362 if(token.type == '{') {
4363 return parse_compound_literal(type);
4366 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4367 cast->base.source_position = source_position;
4369 expression_t *value = parse_sub_expression(20);
4371 check_cast_allowed(value, type);
4373 cast->base.type = type;
4374 cast->unary.value = value;
4378 return create_invalid_expression();
4382 * Parse a statement expression.
4384 static expression_t *parse_statement_expression(void)
4386 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4388 statement_t *statement = parse_compound_statement();
4389 expression->statement.statement = statement;
4390 expression->base.source_position = statement->base.source_position;
4392 /* find last statement and use its type */
4393 type_t *type = type_void;
4394 const statement_t *stmt = statement->compound.statements;
4396 while (stmt->base.next != NULL)
4397 stmt = stmt->base.next;
4399 if (stmt->kind == STATEMENT_EXPRESSION) {
4400 type = stmt->expression.expression->base.type;
4403 warningf(expression->base.source_position, "empty statement expression ({})");
4405 expression->base.type = type;
4411 return create_invalid_expression();
4415 * Parse a braced expression.
4417 static expression_t *parse_brace_expression(void)
4420 add_anchor_token(')');
4422 switch(token.type) {
4424 /* gcc extension: a statement expression */
4425 return parse_statement_expression();
4429 return parse_cast();
4431 if(is_typedef_symbol(token.v.symbol)) {
4432 return parse_cast();
4436 expression_t *result = parse_expression();
4437 rem_anchor_token(')');
4442 return create_invalid_expression();
4445 static expression_t *parse_function_keyword(void)
4450 if (current_function == NULL) {
4451 errorf(HERE, "'__func__' used outside of a function");
4454 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
4455 expression->base.type = type_char_ptr;
4460 static expression_t *parse_pretty_function_keyword(void)
4462 eat(T___PRETTY_FUNCTION__);
4464 if (current_function == NULL) {
4465 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
4468 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
4469 expression->base.type = type_char_ptr;
4474 static expression_t *parse_funcsig_keyword(void)
4478 if (current_function == NULL) {
4479 errorf(HERE, "'__FUNCSIG__' used outside of a function");
4482 expression_t *expression = allocate_expression_zero(EXPR_FUNCSIG);
4483 expression->base.type = type_char_ptr;
4488 static expression_t *parse_funcdname_keyword(void)
4492 if (current_function == NULL) {
4493 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
4496 expression_t *expression = allocate_expression_zero(EXPR_FUNCDNAME);
4497 expression->base.type = type_char_ptr;
4502 static designator_t *parse_designator(void)
4504 designator_t *result = allocate_ast_zero(sizeof(result[0]));
4505 result->source_position = HERE;
4507 if(token.type != T_IDENTIFIER) {
4508 parse_error_expected("while parsing member designator",
4512 result->symbol = token.v.symbol;
4515 designator_t *last_designator = result;
4517 if(token.type == '.') {
4519 if(token.type != T_IDENTIFIER) {
4520 parse_error_expected("while parsing member designator",
4524 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4525 designator->source_position = HERE;
4526 designator->symbol = token.v.symbol;
4529 last_designator->next = designator;
4530 last_designator = designator;
4533 if(token.type == '[') {
4535 add_anchor_token(']');
4536 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4537 designator->source_position = HERE;
4538 designator->array_index = parse_expression();
4539 rem_anchor_token(']');
4541 if(designator->array_index == NULL) {
4545 last_designator->next = designator;
4546 last_designator = designator;
4558 * Parse the __builtin_offsetof() expression.
4560 static expression_t *parse_offsetof(void)
4562 eat(T___builtin_offsetof);
4564 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4565 expression->base.type = type_size_t;
4568 add_anchor_token(',');
4569 type_t *type = parse_typename();
4570 rem_anchor_token(',');
4572 add_anchor_token(')');
4573 designator_t *designator = parse_designator();
4574 rem_anchor_token(')');
4577 expression->offsetofe.type = type;
4578 expression->offsetofe.designator = designator;
4581 memset(&path, 0, sizeof(path));
4582 path.top_type = type;
4583 path.path = NEW_ARR_F(type_path_entry_t, 0);
4585 descend_into_subtype(&path);
4587 if(!walk_designator(&path, designator, true)) {
4588 return create_invalid_expression();
4591 DEL_ARR_F(path.path);
4595 return create_invalid_expression();
4599 * Parses a _builtin_va_start() expression.
4601 static expression_t *parse_va_start(void)
4603 eat(T___builtin_va_start);
4605 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4608 add_anchor_token(',');
4609 expression->va_starte.ap = parse_assignment_expression();
4610 rem_anchor_token(',');
4612 expression_t *const expr = parse_assignment_expression();
4613 if (expr->kind == EXPR_REFERENCE) {
4614 declaration_t *const decl = expr->reference.declaration;
4616 return create_invalid_expression();
4617 if (decl->parent_scope == ¤t_function->scope &&
4618 decl->next == NULL) {
4619 expression->va_starte.parameter = decl;
4624 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4626 return create_invalid_expression();
4630 * Parses a _builtin_va_arg() expression.
4632 static expression_t *parse_va_arg(void)
4634 eat(T___builtin_va_arg);
4636 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4639 expression->va_arge.ap = parse_assignment_expression();
4641 expression->base.type = parse_typename();
4646 return create_invalid_expression();
4649 static expression_t *parse_builtin_symbol(void)
4651 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4653 symbol_t *symbol = token.v.symbol;
4655 expression->builtin_symbol.symbol = symbol;
4658 type_t *type = get_builtin_symbol_type(symbol);
4659 type = automatic_type_conversion(type);
4661 expression->base.type = type;
4666 * Parses a __builtin_constant() expression.
4668 static expression_t *parse_builtin_constant(void)
4670 eat(T___builtin_constant_p);
4672 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4675 add_anchor_token(')');
4676 expression->builtin_constant.value = parse_assignment_expression();
4677 rem_anchor_token(')');
4679 expression->base.type = type_int;
4683 return create_invalid_expression();
4687 * Parses a __builtin_prefetch() expression.
4689 static expression_t *parse_builtin_prefetch(void)
4691 eat(T___builtin_prefetch);
4693 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4696 add_anchor_token(')');
4697 expression->builtin_prefetch.adr = parse_assignment_expression();
4698 if (token.type == ',') {
4700 expression->builtin_prefetch.rw = parse_assignment_expression();
4702 if (token.type == ',') {
4704 expression->builtin_prefetch.locality = parse_assignment_expression();
4706 rem_anchor_token(')');
4708 expression->base.type = type_void;
4712 return create_invalid_expression();
4716 * Parses a __builtin_is_*() compare expression.
4718 static expression_t *parse_compare_builtin(void)
4720 expression_t *expression;
4722 switch(token.type) {
4723 case T___builtin_isgreater:
4724 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4726 case T___builtin_isgreaterequal:
4727 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4729 case T___builtin_isless:
4730 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4732 case T___builtin_islessequal:
4733 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4735 case T___builtin_islessgreater:
4736 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4738 case T___builtin_isunordered:
4739 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4742 internal_errorf(HERE, "invalid compare builtin found");
4745 expression->base.source_position = HERE;
4749 expression->binary.left = parse_assignment_expression();
4751 expression->binary.right = parse_assignment_expression();
4754 type_t *const orig_type_left = expression->binary.left->base.type;
4755 type_t *const orig_type_right = expression->binary.right->base.type;
4757 type_t *const type_left = skip_typeref(orig_type_left);
4758 type_t *const type_right = skip_typeref(orig_type_right);
4759 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4760 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4761 type_error_incompatible("invalid operands in comparison",
4762 expression->base.source_position, orig_type_left, orig_type_right);
4765 semantic_comparison(&expression->binary);
4770 return create_invalid_expression();
4774 * Parses a __builtin_expect() expression.
4776 static expression_t *parse_builtin_expect(void)
4778 eat(T___builtin_expect);
4780 expression_t *expression
4781 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4784 expression->binary.left = parse_assignment_expression();
4786 expression->binary.right = parse_constant_expression();
4789 expression->base.type = expression->binary.left->base.type;
4793 return create_invalid_expression();
4797 * Parses a MS assume() expression.
4799 static expression_t *parse_assume(void) {
4802 expression_t *expression
4803 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4806 add_anchor_token(')');
4807 expression->unary.value = parse_assignment_expression();
4808 rem_anchor_token(')');
4811 expression->base.type = type_void;
4814 return create_invalid_expression();
4818 * Parses a primary expression.
4820 static expression_t *parse_primary_expression(void)
4822 switch (token.type) {
4823 case T_INTEGER: return parse_int_const();
4824 case T_CHARACTER_CONSTANT: return parse_character_constant();
4825 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4826 case T_FLOATINGPOINT: return parse_float_const();
4827 case T_STRING_LITERAL:
4828 case T_WIDE_STRING_LITERAL: return parse_string_const();
4829 case T_IDENTIFIER: return parse_reference();
4830 case T___FUNCTION__:
4831 case T___func__: return parse_function_keyword();
4832 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4833 case T___FUNCSIG__: return parse_funcsig_keyword();
4834 case T___FUNCDNAME__: return parse_funcdname_keyword();
4835 case T___builtin_offsetof: return parse_offsetof();
4836 case T___builtin_va_start: return parse_va_start();
4837 case T___builtin_va_arg: return parse_va_arg();
4838 case T___builtin_expect: return parse_builtin_expect();
4839 case T___builtin_alloca:
4840 case T___builtin_nan:
4841 case T___builtin_nand:
4842 case T___builtin_nanf:
4843 case T___builtin_va_end: return parse_builtin_symbol();
4844 case T___builtin_isgreater:
4845 case T___builtin_isgreaterequal:
4846 case T___builtin_isless:
4847 case T___builtin_islessequal:
4848 case T___builtin_islessgreater:
4849 case T___builtin_isunordered: return parse_compare_builtin();
4850 case T___builtin_constant_p: return parse_builtin_constant();
4851 case T___builtin_prefetch: return parse_builtin_prefetch();
4852 case T__assume: return parse_assume();
4854 case '(': return parse_brace_expression();
4857 errorf(HERE, "unexpected token %K, expected an expression", &token);
4858 return create_invalid_expression();
4862 * Check if the expression has the character type and issue a warning then.
4864 static void check_for_char_index_type(const expression_t *expression) {
4865 type_t *const type = expression->base.type;
4866 const type_t *const base_type = skip_typeref(type);
4868 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4869 warning.char_subscripts) {
4870 warningf(expression->base.source_position,
4871 "array subscript has type '%T'", type);
4875 static expression_t *parse_array_expression(unsigned precedence,
4881 add_anchor_token(']');
4883 expression_t *inside = parse_expression();
4885 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4887 array_access_expression_t *array_access = &expression->array_access;
4889 type_t *const orig_type_left = left->base.type;
4890 type_t *const orig_type_inside = inside->base.type;
4892 type_t *const type_left = skip_typeref(orig_type_left);
4893 type_t *const type_inside = skip_typeref(orig_type_inside);
4895 type_t *return_type;
4896 if (is_type_pointer(type_left)) {
4897 return_type = type_left->pointer.points_to;
4898 array_access->array_ref = left;
4899 array_access->index = inside;
4900 check_for_char_index_type(inside);
4901 } else if (is_type_pointer(type_inside)) {
4902 return_type = type_inside->pointer.points_to;
4903 array_access->array_ref = inside;
4904 array_access->index = left;
4905 array_access->flipped = true;
4906 check_for_char_index_type(left);
4908 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4910 "array access on object with non-pointer types '%T', '%T'",
4911 orig_type_left, orig_type_inside);
4913 return_type = type_error_type;
4914 array_access->array_ref = create_invalid_expression();
4917 rem_anchor_token(']');
4918 if(token.type != ']') {
4919 parse_error_expected("Problem while parsing array access", ']', 0);
4924 return_type = automatic_type_conversion(return_type);
4925 expression->base.type = return_type;
4930 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4932 expression_t *tp_expression = allocate_expression_zero(kind);
4933 tp_expression->base.type = type_size_t;
4935 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4937 add_anchor_token(')');
4938 tp_expression->typeprop.type = parse_typename();
4939 rem_anchor_token(')');
4942 expression_t *expression = parse_sub_expression(precedence);
4943 expression->base.type = revert_automatic_type_conversion(expression);
4945 tp_expression->typeprop.type = expression->base.type;
4946 tp_expression->typeprop.tp_expression = expression;
4949 return tp_expression;
4951 return create_invalid_expression();
4954 static expression_t *parse_sizeof(unsigned precedence)
4957 return parse_typeprop(EXPR_SIZEOF, precedence);
4960 static expression_t *parse_alignof(unsigned precedence)
4963 return parse_typeprop(EXPR_SIZEOF, precedence);
4966 static expression_t *parse_select_expression(unsigned precedence,
4967 expression_t *compound)
4970 assert(token.type == '.' || token.type == T_MINUSGREATER);
4972 bool is_pointer = (token.type == T_MINUSGREATER);
4975 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4976 select->select.compound = compound;
4978 if(token.type != T_IDENTIFIER) {
4979 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4982 symbol_t *symbol = token.v.symbol;
4983 select->select.symbol = symbol;
4986 type_t *const orig_type = compound->base.type;
4987 type_t *const type = skip_typeref(orig_type);
4989 type_t *type_left = type;
4991 if (!is_type_pointer(type)) {
4992 if (is_type_valid(type)) {
4993 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4995 return create_invalid_expression();
4997 type_left = type->pointer.points_to;
4999 type_left = skip_typeref(type_left);
5001 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5002 type_left->kind != TYPE_COMPOUND_UNION) {
5003 if (is_type_valid(type_left)) {
5004 errorf(HERE, "request for member '%Y' in something not a struct or "
5005 "union, but '%T'", symbol, type_left);
5007 return create_invalid_expression();
5010 declaration_t *const declaration = type_left->compound.declaration;
5012 if(!declaration->init.is_defined) {
5013 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5015 return create_invalid_expression();
5018 declaration_t *iter = find_compound_entry(declaration, symbol);
5020 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5021 return create_invalid_expression();
5024 /* we always do the auto-type conversions; the & and sizeof parser contains
5025 * code to revert this! */
5026 type_t *expression_type = automatic_type_conversion(iter->type);
5028 select->select.compound_entry = iter;
5029 select->base.type = expression_type;
5031 if(expression_type->kind == TYPE_BITFIELD) {
5032 expression_t *extract
5033 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5034 extract->unary.value = select;
5035 extract->base.type = expression_type->bitfield.base;
5044 * Parse a call expression, ie. expression '( ... )'.
5046 * @param expression the function address
5048 static expression_t *parse_call_expression(unsigned precedence,
5049 expression_t *expression)
5052 expression_t *result = allocate_expression_zero(EXPR_CALL);
5053 result->base.source_position = expression->base.source_position;
5055 call_expression_t *call = &result->call;
5056 call->function = expression;
5058 type_t *const orig_type = expression->base.type;
5059 type_t *const type = skip_typeref(orig_type);
5061 function_type_t *function_type = NULL;
5062 if (is_type_pointer(type)) {
5063 type_t *const to_type = skip_typeref(type->pointer.points_to);
5065 if (is_type_function(to_type)) {
5066 function_type = &to_type->function;
5067 call->base.type = function_type->return_type;
5071 if (function_type == NULL && is_type_valid(type)) {
5072 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5075 /* parse arguments */
5077 add_anchor_token(')');
5078 add_anchor_token(',');
5080 if(token.type != ')') {
5081 call_argument_t *last_argument = NULL;
5084 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5086 argument->expression = parse_assignment_expression();
5087 if(last_argument == NULL) {
5088 call->arguments = argument;
5090 last_argument->next = argument;
5092 last_argument = argument;
5094 if(token.type != ',')
5099 rem_anchor_token(',');
5100 rem_anchor_token(')');
5103 if(function_type != NULL) {
5104 function_parameter_t *parameter = function_type->parameters;
5105 call_argument_t *argument = call->arguments;
5106 for( ; parameter != NULL && argument != NULL;
5107 parameter = parameter->next, argument = argument->next) {
5108 type_t *expected_type = parameter->type;
5109 /* TODO report scope in error messages */
5110 expression_t *const arg_expr = argument->expression;
5111 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
5112 if (res_type == NULL) {
5113 /* TODO improve error message */
5114 errorf(arg_expr->base.source_position,
5115 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5116 arg_expr, arg_expr->base.type, expected_type);
5118 argument->expression = create_implicit_cast(argument->expression, expected_type);
5121 /* too few parameters */
5122 if(parameter != NULL) {
5123 errorf(HERE, "too few arguments to function '%E'", expression);
5124 } else if(argument != NULL) {
5125 /* too many parameters */
5126 if(!function_type->variadic
5127 && !function_type->unspecified_parameters) {
5128 errorf(HERE, "too many arguments to function '%E'", expression);
5130 /* do default promotion */
5131 for( ; argument != NULL; argument = argument->next) {
5132 type_t *type = argument->expression->base.type;
5134 type = skip_typeref(type);
5135 if(is_type_integer(type)) {
5136 type = promote_integer(type);
5137 } else if(type == type_float) {
5141 argument->expression
5142 = create_implicit_cast(argument->expression, type);
5145 check_format(&result->call);
5148 check_format(&result->call);
5154 return create_invalid_expression();
5157 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5159 static bool same_compound_type(const type_t *type1, const type_t *type2)
5162 is_type_compound(type1) &&
5163 type1->kind == type2->kind &&
5164 type1->compound.declaration == type2->compound.declaration;
5168 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5170 * @param expression the conditional expression
5172 static expression_t *parse_conditional_expression(unsigned precedence,
5173 expression_t *expression)
5176 add_anchor_token(':');
5178 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5180 conditional_expression_t *conditional = &result->conditional;
5181 conditional->condition = expression;
5184 type_t *const condition_type_orig = expression->base.type;
5185 type_t *const condition_type = skip_typeref(condition_type_orig);
5186 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5187 type_error("expected a scalar type in conditional condition",
5188 expression->base.source_position, condition_type_orig);
5191 expression_t *true_expression = parse_expression();
5192 rem_anchor_token(':');
5194 expression_t *false_expression = parse_sub_expression(precedence);
5196 type_t *const orig_true_type = true_expression->base.type;
5197 type_t *const orig_false_type = false_expression->base.type;
5198 type_t *const true_type = skip_typeref(orig_true_type);
5199 type_t *const false_type = skip_typeref(orig_false_type);
5202 type_t *result_type;
5203 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
5204 result_type = semantic_arithmetic(true_type, false_type);
5206 true_expression = create_implicit_cast(true_expression, result_type);
5207 false_expression = create_implicit_cast(false_expression, result_type);
5209 conditional->true_expression = true_expression;
5210 conditional->false_expression = false_expression;
5211 conditional->base.type = result_type;
5212 } else if (same_compound_type(true_type, false_type) || (
5213 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
5214 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
5216 /* just take 1 of the 2 types */
5217 result_type = true_type;
5218 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
5219 && pointers_compatible(true_type, false_type)) {
5221 result_type = true_type;
5222 } else if (is_type_pointer(true_type)
5223 && is_null_pointer_constant(false_expression)) {
5224 result_type = true_type;
5225 } else if (is_type_pointer(false_type)
5226 && is_null_pointer_constant(true_expression)) {
5227 result_type = false_type;
5229 /* TODO: one pointer to void*, other some pointer */
5231 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5232 type_error_incompatible("while parsing conditional",
5233 expression->base.source_position, true_type,
5236 result_type = type_error_type;
5239 conditional->true_expression
5240 = create_implicit_cast(true_expression, result_type);
5241 conditional->false_expression
5242 = create_implicit_cast(false_expression, result_type);
5243 conditional->base.type = result_type;
5246 return create_invalid_expression();
5250 * Parse an extension expression.
5252 static expression_t *parse_extension(unsigned precedence)
5254 eat(T___extension__);
5256 /* TODO enable extensions */
5257 expression_t *expression = parse_sub_expression(precedence);
5258 /* TODO disable extensions */
5263 * Parse a __builtin_classify_type() expression.
5265 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5267 eat(T___builtin_classify_type);
5269 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5270 result->base.type = type_int;
5273 add_anchor_token(')');
5274 expression_t *expression = parse_sub_expression(precedence);
5275 rem_anchor_token(')');
5277 result->classify_type.type_expression = expression;
5281 return create_invalid_expression();
5284 static void semantic_incdec(unary_expression_t *expression)
5286 type_t *const orig_type = expression->value->base.type;
5287 type_t *const type = skip_typeref(orig_type);
5288 /* TODO !is_type_real && !is_type_pointer */
5289 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5290 if (is_type_valid(type)) {
5291 /* TODO: improve error message */
5292 errorf(HERE, "operation needs an arithmetic or pointer type");
5297 expression->base.type = orig_type;
5300 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5302 type_t *const orig_type = expression->value->base.type;
5303 type_t *const type = skip_typeref(orig_type);
5304 if(!is_type_arithmetic(type)) {
5305 if (is_type_valid(type)) {
5306 /* TODO: improve error message */
5307 errorf(HERE, "operation needs an arithmetic type");
5312 expression->base.type = orig_type;
5315 static void semantic_unexpr_scalar(unary_expression_t *expression)
5317 type_t *const orig_type = expression->value->base.type;
5318 type_t *const type = skip_typeref(orig_type);
5319 if (!is_type_scalar(type)) {
5320 if (is_type_valid(type)) {
5321 errorf(HERE, "operand of ! must be of scalar type");
5326 expression->base.type = orig_type;
5329 static void semantic_unexpr_integer(unary_expression_t *expression)
5331 type_t *const orig_type = expression->value->base.type;
5332 type_t *const type = skip_typeref(orig_type);
5333 if (!is_type_integer(type)) {
5334 if (is_type_valid(type)) {
5335 errorf(HERE, "operand of ~ must be of integer type");
5340 expression->base.type = orig_type;
5343 static void semantic_dereference(unary_expression_t *expression)
5345 type_t *const orig_type = expression->value->base.type;
5346 type_t *const type = skip_typeref(orig_type);
5347 if(!is_type_pointer(type)) {
5348 if (is_type_valid(type)) {
5349 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
5354 type_t *result_type = type->pointer.points_to;
5355 result_type = automatic_type_conversion(result_type);
5356 expression->base.type = result_type;
5360 * Check the semantic of the address taken expression.
5362 static void semantic_take_addr(unary_expression_t *expression)
5364 expression_t *value = expression->value;
5365 value->base.type = revert_automatic_type_conversion(value);
5367 type_t *orig_type = value->base.type;
5368 if(!is_type_valid(orig_type))
5371 if(value->kind == EXPR_REFERENCE) {
5372 declaration_t *const declaration = value->reference.declaration;
5373 if(declaration != NULL) {
5374 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
5375 errorf(expression->base.source_position,
5376 "address of register variable '%Y' requested",
5377 declaration->symbol);
5379 declaration->address_taken = 1;
5383 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5386 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
5387 static expression_t *parse_##unexpression_type(unsigned precedence) \
5391 expression_t *unary_expression \
5392 = allocate_expression_zero(unexpression_type); \
5393 unary_expression->base.source_position = HERE; \
5394 unary_expression->unary.value = parse_sub_expression(precedence); \
5396 sfunc(&unary_expression->unary); \
5398 return unary_expression; \
5401 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
5402 semantic_unexpr_arithmetic)
5403 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
5404 semantic_unexpr_arithmetic)
5405 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
5406 semantic_unexpr_scalar)
5407 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
5408 semantic_dereference)
5409 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
5411 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
5412 semantic_unexpr_integer)
5413 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
5415 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
5418 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
5420 static expression_t *parse_##unexpression_type(unsigned precedence, \
5421 expression_t *left) \
5423 (void) precedence; \
5426 expression_t *unary_expression \
5427 = allocate_expression_zero(unexpression_type); \
5428 unary_expression->unary.value = left; \
5430 sfunc(&unary_expression->unary); \
5432 return unary_expression; \
5435 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
5436 EXPR_UNARY_POSTFIX_INCREMENT,
5438 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
5439 EXPR_UNARY_POSTFIX_DECREMENT,
5442 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
5444 /* TODO: handle complex + imaginary types */
5446 /* § 6.3.1.8 Usual arithmetic conversions */
5447 if(type_left == type_long_double || type_right == type_long_double) {
5448 return type_long_double;
5449 } else if(type_left == type_double || type_right == type_double) {
5451 } else if(type_left == type_float || type_right == type_float) {
5455 type_right = promote_integer(type_right);
5456 type_left = promote_integer(type_left);
5458 if(type_left == type_right)
5461 bool signed_left = is_type_signed(type_left);
5462 bool signed_right = is_type_signed(type_right);
5463 int rank_left = get_rank(type_left);
5464 int rank_right = get_rank(type_right);
5465 if(rank_left < rank_right) {
5466 if(signed_left == signed_right || !signed_right) {
5472 if(signed_left == signed_right || !signed_left) {
5481 * Check the semantic restrictions for a binary expression.
5483 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
5485 expression_t *const left = expression->left;
5486 expression_t *const right = expression->right;
5487 type_t *const orig_type_left = left->base.type;
5488 type_t *const orig_type_right = right->base.type;
5489 type_t *const type_left = skip_typeref(orig_type_left);
5490 type_t *const type_right = skip_typeref(orig_type_right);
5492 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5493 /* TODO: improve error message */
5494 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5495 errorf(HERE, "operation needs arithmetic types");
5500 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5501 expression->left = create_implicit_cast(left, arithmetic_type);
5502 expression->right = create_implicit_cast(right, arithmetic_type);
5503 expression->base.type = arithmetic_type;
5506 static void semantic_shift_op(binary_expression_t *expression)
5508 expression_t *const left = expression->left;
5509 expression_t *const right = expression->right;
5510 type_t *const orig_type_left = left->base.type;
5511 type_t *const orig_type_right = right->base.type;
5512 type_t * type_left = skip_typeref(orig_type_left);
5513 type_t * type_right = skip_typeref(orig_type_right);
5515 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
5516 /* TODO: improve error message */
5517 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5518 errorf(HERE, "operation needs integer types");
5523 type_left = promote_integer(type_left);
5524 type_right = promote_integer(type_right);
5526 expression->left = create_implicit_cast(left, type_left);
5527 expression->right = create_implicit_cast(right, type_right);
5528 expression->base.type = type_left;
5531 static void semantic_add(binary_expression_t *expression)
5533 expression_t *const left = expression->left;
5534 expression_t *const right = expression->right;
5535 type_t *const orig_type_left = left->base.type;
5536 type_t *const orig_type_right = right->base.type;
5537 type_t *const type_left = skip_typeref(orig_type_left);
5538 type_t *const type_right = skip_typeref(orig_type_right);
5541 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5542 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5543 expression->left = create_implicit_cast(left, arithmetic_type);
5544 expression->right = create_implicit_cast(right, arithmetic_type);
5545 expression->base.type = arithmetic_type;
5547 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5548 expression->base.type = type_left;
5549 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
5550 expression->base.type = type_right;
5551 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5552 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
5556 static void semantic_sub(binary_expression_t *expression)
5558 expression_t *const left = expression->left;
5559 expression_t *const right = expression->right;
5560 type_t *const orig_type_left = left->base.type;
5561 type_t *const orig_type_right = right->base.type;
5562 type_t *const type_left = skip_typeref(orig_type_left);
5563 type_t *const type_right = skip_typeref(orig_type_right);
5566 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5567 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5568 expression->left = create_implicit_cast(left, arithmetic_type);
5569 expression->right = create_implicit_cast(right, arithmetic_type);
5570 expression->base.type = arithmetic_type;
5572 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5573 expression->base.type = type_left;
5574 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5575 if(!pointers_compatible(type_left, type_right)) {
5577 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5578 orig_type_left, orig_type_right);
5580 expression->base.type = type_ptrdiff_t;
5582 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5583 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5584 orig_type_left, orig_type_right);
5589 * Check the semantics of comparison expressions.
5591 * @param expression The expression to check.
5593 static void semantic_comparison(binary_expression_t *expression)
5595 expression_t *left = expression->left;
5596 expression_t *right = expression->right;
5597 type_t *orig_type_left = left->base.type;
5598 type_t *orig_type_right = right->base.type;
5600 type_t *type_left = skip_typeref(orig_type_left);
5601 type_t *type_right = skip_typeref(orig_type_right);
5603 /* TODO non-arithmetic types */
5604 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5605 if (warning.sign_compare &&
5606 (expression->base.kind != EXPR_BINARY_EQUAL &&
5607 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5608 (is_type_signed(type_left) != is_type_signed(type_right))) {
5609 warningf(expression->base.source_position,
5610 "comparison between signed and unsigned");
5612 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5613 expression->left = create_implicit_cast(left, arithmetic_type);
5614 expression->right = create_implicit_cast(right, arithmetic_type);
5615 expression->base.type = arithmetic_type;
5616 if (warning.float_equal &&
5617 (expression->base.kind == EXPR_BINARY_EQUAL ||
5618 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5619 is_type_float(arithmetic_type)) {
5620 warningf(expression->base.source_position,
5621 "comparing floating point with == or != is unsafe");
5623 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5624 /* TODO check compatibility */
5625 } else if (is_type_pointer(type_left)) {
5626 expression->right = create_implicit_cast(right, type_left);
5627 } else if (is_type_pointer(type_right)) {
5628 expression->left = create_implicit_cast(left, type_right);
5629 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5630 type_error_incompatible("invalid operands in comparison",
5631 expression->base.source_position,
5632 type_left, type_right);
5634 expression->base.type = type_int;
5637 static void semantic_arithmetic_assign(binary_expression_t *expression)
5639 expression_t *left = expression->left;
5640 expression_t *right = expression->right;
5641 type_t *orig_type_left = left->base.type;
5642 type_t *orig_type_right = right->base.type;
5644 type_t *type_left = skip_typeref(orig_type_left);
5645 type_t *type_right = skip_typeref(orig_type_right);
5647 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5648 /* TODO: improve error message */
5649 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5650 errorf(HERE, "operation needs arithmetic types");
5655 /* combined instructions are tricky. We can't create an implicit cast on
5656 * the left side, because we need the uncasted form for the store.
5657 * The ast2firm pass has to know that left_type must be right_type
5658 * for the arithmetic operation and create a cast by itself */
5659 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5660 expression->right = create_implicit_cast(right, arithmetic_type);
5661 expression->base.type = type_left;
5664 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5666 expression_t *const left = expression->left;
5667 expression_t *const right = expression->right;
5668 type_t *const orig_type_left = left->base.type;
5669 type_t *const orig_type_right = right->base.type;
5670 type_t *const type_left = skip_typeref(orig_type_left);
5671 type_t *const type_right = skip_typeref(orig_type_right);
5673 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5674 /* combined instructions are tricky. We can't create an implicit cast on
5675 * the left side, because we need the uncasted form for the store.
5676 * The ast2firm pass has to know that left_type must be right_type
5677 * for the arithmetic operation and create a cast by itself */
5678 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5679 expression->right = create_implicit_cast(right, arithmetic_type);
5680 expression->base.type = type_left;
5681 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5682 expression->base.type = type_left;
5683 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5684 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5689 * Check the semantic restrictions of a logical expression.
5691 static void semantic_logical_op(binary_expression_t *expression)
5693 expression_t *const left = expression->left;
5694 expression_t *const right = expression->right;
5695 type_t *const orig_type_left = left->base.type;
5696 type_t *const orig_type_right = right->base.type;
5697 type_t *const type_left = skip_typeref(orig_type_left);
5698 type_t *const type_right = skip_typeref(orig_type_right);
5700 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5701 /* TODO: improve error message */
5702 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5703 errorf(HERE, "operation needs scalar types");
5708 expression->base.type = type_int;
5712 * Checks if a compound type has constant fields.
5714 static bool has_const_fields(const compound_type_t *type)
5716 const scope_t *scope = &type->declaration->scope;
5717 const declaration_t *declaration = scope->declarations;
5719 for (; declaration != NULL; declaration = declaration->next) {
5720 if (declaration->namespc != NAMESPACE_NORMAL)
5723 const type_t *decl_type = skip_typeref(declaration->type);
5724 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5732 * Check the semantic restrictions of a binary assign expression.
5734 static void semantic_binexpr_assign(binary_expression_t *expression)
5736 expression_t *left = expression->left;
5737 type_t *orig_type_left = left->base.type;
5739 type_t *type_left = revert_automatic_type_conversion(left);
5740 type_left = skip_typeref(orig_type_left);
5742 /* must be a modifiable lvalue */
5743 if (is_type_array(type_left)) {
5744 errorf(HERE, "cannot assign to arrays ('%E')", left);
5747 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5748 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5752 if(is_type_incomplete(type_left)) {
5754 "left-hand side of assignment '%E' has incomplete type '%T'",
5755 left, orig_type_left);
5758 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5759 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5760 left, orig_type_left);
5764 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5766 if (res_type == NULL) {
5767 errorf(expression->base.source_position,
5768 "cannot assign to '%T' from '%T'",
5769 orig_type_left, expression->right->base.type);
5771 expression->right = create_implicit_cast(expression->right, res_type);
5774 expression->base.type = orig_type_left;
5778 * Determine if the outermost operation (or parts thereof) of the given
5779 * expression has no effect in order to generate a warning about this fact.
5780 * Therefore in some cases this only examines some of the operands of the
5781 * expression (see comments in the function and examples below).
5783 * f() + 23; // warning, because + has no effect
5784 * x || f(); // no warning, because x controls execution of f()
5785 * x ? y : f(); // warning, because y has no effect
5786 * (void)x; // no warning to be able to suppress the warning
5787 * This function can NOT be used for an "expression has definitely no effect"-
5789 static bool expression_has_effect(const expression_t *const expr)
5791 switch (expr->kind) {
5792 case EXPR_UNKNOWN: break;
5793 case EXPR_INVALID: return true; /* do NOT warn */
5794 case EXPR_REFERENCE: return false;
5795 case EXPR_CONST: return false;
5796 case EXPR_CHARACTER_CONSTANT: return false;
5797 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5798 case EXPR_STRING_LITERAL: return false;
5799 case EXPR_WIDE_STRING_LITERAL: return false;
5802 const call_expression_t *const call = &expr->call;
5803 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5806 switch (call->function->builtin_symbol.symbol->ID) {
5807 case T___builtin_va_end: return true;
5808 default: return false;
5812 /* Generate the warning if either the left or right hand side of a
5813 * conditional expression has no effect */
5814 case EXPR_CONDITIONAL: {
5815 const conditional_expression_t *const cond = &expr->conditional;
5817 expression_has_effect(cond->true_expression) &&
5818 expression_has_effect(cond->false_expression);
5821 case EXPR_SELECT: return false;
5822 case EXPR_ARRAY_ACCESS: return false;
5823 case EXPR_SIZEOF: return false;
5824 case EXPR_CLASSIFY_TYPE: return false;
5825 case EXPR_ALIGNOF: return false;
5827 case EXPR_FUNCTION: return false;
5828 case EXPR_PRETTY_FUNCTION: return false;
5829 case EXPR_FUNCSIG: return false;
5830 case EXPR_FUNCDNAME: return false;
5831 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5832 case EXPR_BUILTIN_CONSTANT_P: return false;
5833 case EXPR_BUILTIN_PREFETCH: return true;
5834 case EXPR_OFFSETOF: return false;
5835 case EXPR_VA_START: return true;
5836 case EXPR_VA_ARG: return true;
5837 case EXPR_STATEMENT: return true; // TODO
5838 case EXPR_COMPOUND_LITERAL: return false;
5840 case EXPR_UNARY_NEGATE: return false;
5841 case EXPR_UNARY_PLUS: return false;
5842 case EXPR_UNARY_BITWISE_NEGATE: return false;
5843 case EXPR_UNARY_NOT: return false;
5844 case EXPR_UNARY_DEREFERENCE: return false;
5845 case EXPR_UNARY_TAKE_ADDRESS: return false;
5846 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5847 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5848 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5849 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5851 /* Treat void casts as if they have an effect in order to being able to
5852 * suppress the warning */
5853 case EXPR_UNARY_CAST: {
5854 type_t *const type = skip_typeref(expr->base.type);
5855 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5858 case EXPR_UNARY_CAST_IMPLICIT: return true;
5859 case EXPR_UNARY_ASSUME: return true;
5860 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5862 case EXPR_BINARY_ADD: return false;
5863 case EXPR_BINARY_SUB: return false;
5864 case EXPR_BINARY_MUL: return false;
5865 case EXPR_BINARY_DIV: return false;
5866 case EXPR_BINARY_MOD: return false;
5867 case EXPR_BINARY_EQUAL: return false;
5868 case EXPR_BINARY_NOTEQUAL: return false;
5869 case EXPR_BINARY_LESS: return false;
5870 case EXPR_BINARY_LESSEQUAL: return false;
5871 case EXPR_BINARY_GREATER: return false;
5872 case EXPR_BINARY_GREATEREQUAL: return false;
5873 case EXPR_BINARY_BITWISE_AND: return false;
5874 case EXPR_BINARY_BITWISE_OR: return false;
5875 case EXPR_BINARY_BITWISE_XOR: return false;
5876 case EXPR_BINARY_SHIFTLEFT: return false;
5877 case EXPR_BINARY_SHIFTRIGHT: return false;
5878 case EXPR_BINARY_ASSIGN: return true;
5879 case EXPR_BINARY_MUL_ASSIGN: return true;
5880 case EXPR_BINARY_DIV_ASSIGN: return true;
5881 case EXPR_BINARY_MOD_ASSIGN: return true;
5882 case EXPR_BINARY_ADD_ASSIGN: return true;
5883 case EXPR_BINARY_SUB_ASSIGN: return true;
5884 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5885 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5886 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5887 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5888 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5890 /* Only examine the right hand side of && and ||, because the left hand
5891 * side already has the effect of controlling the execution of the right
5893 case EXPR_BINARY_LOGICAL_AND:
5894 case EXPR_BINARY_LOGICAL_OR:
5895 /* Only examine the right hand side of a comma expression, because the left
5896 * hand side has a separate warning */
5897 case EXPR_BINARY_COMMA:
5898 return expression_has_effect(expr->binary.right);
5900 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5901 case EXPR_BINARY_ISGREATER: return false;
5902 case EXPR_BINARY_ISGREATEREQUAL: return false;
5903 case EXPR_BINARY_ISLESS: return false;
5904 case EXPR_BINARY_ISLESSEQUAL: return false;
5905 case EXPR_BINARY_ISLESSGREATER: return false;
5906 case EXPR_BINARY_ISUNORDERED: return false;
5909 internal_errorf(HERE, "unexpected expression");
5912 static void semantic_comma(binary_expression_t *expression)
5914 if (warning.unused_value) {
5915 const expression_t *const left = expression->left;
5916 if (!expression_has_effect(left)) {
5917 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5920 expression->base.type = expression->right->base.type;
5923 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5924 static expression_t *parse_##binexpression_type(unsigned precedence, \
5925 expression_t *left) \
5928 source_position_t pos = HERE; \
5930 expression_t *right = parse_sub_expression(precedence + lr); \
5932 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5933 binexpr->base.source_position = pos; \
5934 binexpr->binary.left = left; \
5935 binexpr->binary.right = right; \
5936 sfunc(&binexpr->binary); \
5941 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5942 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5943 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5944 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5945 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5946 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5947 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5948 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5949 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5951 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5952 semantic_comparison, 1)
5953 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5954 semantic_comparison, 1)
5955 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5956 semantic_comparison, 1)
5957 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5958 semantic_comparison, 1)
5960 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5961 semantic_binexpr_arithmetic, 1)
5962 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5963 semantic_binexpr_arithmetic, 1)
5964 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5965 semantic_binexpr_arithmetic, 1)
5966 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5967 semantic_logical_op, 1)
5968 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5969 semantic_logical_op, 1)
5970 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5971 semantic_shift_op, 1)
5972 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5973 semantic_shift_op, 1)
5974 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5975 semantic_arithmetic_addsubb_assign, 0)
5976 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5977 semantic_arithmetic_addsubb_assign, 0)
5978 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5979 semantic_arithmetic_assign, 0)
5980 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5981 semantic_arithmetic_assign, 0)
5982 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5983 semantic_arithmetic_assign, 0)
5984 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5985 semantic_arithmetic_assign, 0)
5986 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5987 semantic_arithmetic_assign, 0)
5988 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5989 semantic_arithmetic_assign, 0)
5990 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5991 semantic_arithmetic_assign, 0)
5992 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5993 semantic_arithmetic_assign, 0)
5995 static expression_t *parse_sub_expression(unsigned precedence)
5997 if(token.type < 0) {
5998 return expected_expression_error();
6001 expression_parser_function_t *parser
6002 = &expression_parsers[token.type];
6003 source_position_t source_position = token.source_position;
6006 if(parser->parser != NULL) {
6007 left = parser->parser(parser->precedence);
6009 left = parse_primary_expression();
6011 assert(left != NULL);
6012 left->base.source_position = source_position;
6015 if(token.type < 0) {
6016 return expected_expression_error();
6019 parser = &expression_parsers[token.type];
6020 if(parser->infix_parser == NULL)
6022 if(parser->infix_precedence < precedence)
6025 left = parser->infix_parser(parser->infix_precedence, left);
6027 assert(left != NULL);
6028 assert(left->kind != EXPR_UNKNOWN);
6029 left->base.source_position = source_position;
6036 * Parse an expression.
6038 static expression_t *parse_expression(void)
6040 return parse_sub_expression(1);
6044 * Register a parser for a prefix-like operator with given precedence.
6046 * @param parser the parser function
6047 * @param token_type the token type of the prefix token
6048 * @param precedence the precedence of the operator
6050 static void register_expression_parser(parse_expression_function parser,
6051 int token_type, unsigned precedence)
6053 expression_parser_function_t *entry = &expression_parsers[token_type];
6055 if(entry->parser != NULL) {
6056 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6057 panic("trying to register multiple expression parsers for a token");
6059 entry->parser = parser;
6060 entry->precedence = precedence;
6064 * Register a parser for an infix operator with given precedence.
6066 * @param parser the parser function
6067 * @param token_type the token type of the infix operator
6068 * @param precedence the precedence of the operator
6070 static void register_infix_parser(parse_expression_infix_function parser,
6071 int token_type, unsigned precedence)
6073 expression_parser_function_t *entry = &expression_parsers[token_type];
6075 if(entry->infix_parser != NULL) {
6076 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6077 panic("trying to register multiple infix expression parsers for a "
6080 entry->infix_parser = parser;
6081 entry->infix_precedence = precedence;
6085 * Initialize the expression parsers.
6087 static void init_expression_parsers(void)
6089 memset(&expression_parsers, 0, sizeof(expression_parsers));
6091 register_infix_parser(parse_array_expression, '[', 30);
6092 register_infix_parser(parse_call_expression, '(', 30);
6093 register_infix_parser(parse_select_expression, '.', 30);
6094 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6095 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6097 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6100 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6101 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6102 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6103 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6104 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6105 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6106 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6107 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6108 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6109 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6110 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6111 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6112 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6113 T_EXCLAMATIONMARKEQUAL, 13);
6114 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6115 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6116 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6117 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6118 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6119 register_infix_parser(parse_conditional_expression, '?', 7);
6120 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6121 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6122 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6123 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6124 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6125 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6126 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6127 T_LESSLESSEQUAL, 2);
6128 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6129 T_GREATERGREATEREQUAL, 2);
6130 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6132 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6134 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6137 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6139 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6140 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6141 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6142 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6143 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6144 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6145 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6147 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6149 register_expression_parser(parse_sizeof, T_sizeof, 25);
6150 register_expression_parser(parse_alignof, T___alignof__, 25);
6151 register_expression_parser(parse_extension, T___extension__, 25);
6152 register_expression_parser(parse_builtin_classify_type,
6153 T___builtin_classify_type, 25);
6157 * Parse a asm statement constraints specification.
6159 static asm_constraint_t *parse_asm_constraints(void)
6161 asm_constraint_t *result = NULL;
6162 asm_constraint_t *last = NULL;
6164 while(token.type == T_STRING_LITERAL || token.type == '[') {
6165 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6166 memset(constraint, 0, sizeof(constraint[0]));
6168 if(token.type == '[') {
6170 if(token.type != T_IDENTIFIER) {
6171 parse_error_expected("while parsing asm constraint",
6175 constraint->symbol = token.v.symbol;
6180 constraint->constraints = parse_string_literals();
6182 constraint->expression = parse_expression();
6186 last->next = constraint;
6188 result = constraint;
6192 if(token.type != ',')
6203 * Parse a asm statement clobber specification.
6205 static asm_clobber_t *parse_asm_clobbers(void)
6207 asm_clobber_t *result = NULL;
6208 asm_clobber_t *last = NULL;
6210 while(token.type == T_STRING_LITERAL) {
6211 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6212 clobber->clobber = parse_string_literals();
6215 last->next = clobber;
6221 if(token.type != ',')
6230 * Parse an asm statement.
6232 static statement_t *parse_asm_statement(void)
6236 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6237 statement->base.source_position = token.source_position;
6239 asm_statement_t *asm_statement = &statement->asms;
6241 if(token.type == T_volatile) {
6243 asm_statement->is_volatile = true;
6247 add_anchor_token(')');
6248 add_anchor_token(':');
6249 asm_statement->asm_text = parse_string_literals();
6251 if(token.type != ':') {
6252 rem_anchor_token(':');
6257 asm_statement->inputs = parse_asm_constraints();
6258 if(token.type != ':') {
6259 rem_anchor_token(':');
6264 asm_statement->outputs = parse_asm_constraints();
6265 if(token.type != ':') {
6266 rem_anchor_token(':');
6269 rem_anchor_token(':');
6272 asm_statement->clobbers = parse_asm_clobbers();
6275 rem_anchor_token(')');
6280 return create_invalid_statement();
6284 * Parse a case statement.
6286 static statement_t *parse_case_statement(void)
6290 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6292 statement->base.source_position = token.source_position;
6293 statement->case_label.expression = parse_expression();
6295 if (c_mode & _GNUC) {
6296 if (token.type == T_DOTDOTDOT) {
6298 statement->case_label.end_range = parse_expression();
6304 if (! is_constant_expression(statement->case_label.expression)) {
6305 errorf(statement->base.source_position,
6306 "case label does not reduce to an integer constant");
6308 /* TODO: check if the case label is already known */
6309 if (current_switch != NULL) {
6310 /* link all cases into the switch statement */
6311 if (current_switch->last_case == NULL) {
6312 current_switch->first_case =
6313 current_switch->last_case = &statement->case_label;
6315 current_switch->last_case->next = &statement->case_label;
6318 errorf(statement->base.source_position,
6319 "case label not within a switch statement");
6322 statement->case_label.statement = parse_statement();
6326 return create_invalid_statement();
6330 * Finds an existing default label of a switch statement.
6332 static case_label_statement_t *
6333 find_default_label(const switch_statement_t *statement)
6335 case_label_statement_t *label = statement->first_case;
6336 for ( ; label != NULL; label = label->next) {
6337 if (label->expression == NULL)
6344 * Parse a default statement.
6346 static statement_t *parse_default_statement(void)
6350 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6352 statement->base.source_position = token.source_position;
6355 if (current_switch != NULL) {
6356 const case_label_statement_t *def_label = find_default_label(current_switch);
6357 if (def_label != NULL) {
6358 errorf(HERE, "multiple default labels in one switch");
6359 errorf(def_label->base.source_position,
6360 "this is the first default label");
6362 /* link all cases into the switch statement */
6363 if (current_switch->last_case == NULL) {
6364 current_switch->first_case =
6365 current_switch->last_case = &statement->case_label;
6367 current_switch->last_case->next = &statement->case_label;
6371 errorf(statement->base.source_position,
6372 "'default' label not within a switch statement");
6374 statement->case_label.statement = parse_statement();
6378 return create_invalid_statement();
6382 * Return the declaration for a given label symbol or create a new one.
6384 static declaration_t *get_label(symbol_t *symbol)
6386 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
6387 assert(current_function != NULL);
6388 /* if we found a label in the same function, then we already created the
6390 if(candidate != NULL
6391 && candidate->parent_scope == ¤t_function->scope) {
6395 /* otherwise we need to create a new one */
6396 declaration_t *const declaration = allocate_declaration_zero();
6397 declaration->namespc = NAMESPACE_LABEL;
6398 declaration->symbol = symbol;
6400 label_push(declaration);
6406 * Parse a label statement.
6408 static statement_t *parse_label_statement(void)
6410 assert(token.type == T_IDENTIFIER);
6411 symbol_t *symbol = token.v.symbol;
6414 declaration_t *label = get_label(symbol);
6416 /* if source position is already set then the label is defined twice,
6417 * otherwise it was just mentioned in a goto so far */
6418 if(label->source_position.input_name != NULL) {
6419 errorf(HERE, "duplicate label '%Y'", symbol);
6420 errorf(label->source_position, "previous definition of '%Y' was here",
6423 label->source_position = token.source_position;
6426 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
6428 statement->base.source_position = token.source_position;
6429 statement->label.label = label;
6433 if(token.type == '}') {
6434 /* TODO only warn? */
6436 warningf(HERE, "label at end of compound statement");
6437 statement->label.statement = create_empty_statement();
6439 errorf(HERE, "label at end of compound statement");
6440 statement->label.statement = create_invalid_statement();
6444 if (token.type == ';') {
6445 /* eat an empty statement here, to avoid the warning about an empty
6446 * after a label. label:; is commonly used to have a label before
6448 statement->label.statement = create_empty_statement();
6451 statement->label.statement = parse_statement();
6455 /* remember the labels's in a list for later checking */
6456 if (label_last == NULL) {
6457 label_first = &statement->label;
6459 label_last->next = &statement->label;
6461 label_last = &statement->label;
6467 * Parse an if statement.
6469 static statement_t *parse_if(void)
6473 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
6474 statement->base.source_position = token.source_position;
6477 add_anchor_token(')');
6478 statement->ifs.condition = parse_expression();
6479 rem_anchor_token(')');
6482 add_anchor_token(T_else);
6483 statement->ifs.true_statement = parse_statement();
6484 rem_anchor_token(T_else);
6486 if(token.type == T_else) {
6488 statement->ifs.false_statement = parse_statement();
6493 return create_invalid_statement();
6497 * Parse a switch statement.
6499 static statement_t *parse_switch(void)
6503 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
6504 statement->base.source_position = token.source_position;
6507 expression_t *const expr = parse_expression();
6508 type_t * type = skip_typeref(expr->base.type);
6509 if (is_type_integer(type)) {
6510 type = promote_integer(type);
6511 } else if (is_type_valid(type)) {
6512 errorf(expr->base.source_position,
6513 "switch quantity is not an integer, but '%T'", type);
6514 type = type_error_type;
6516 statement->switchs.expression = create_implicit_cast(expr, type);
6519 switch_statement_t *rem = current_switch;
6520 current_switch = &statement->switchs;
6521 statement->switchs.body = parse_statement();
6522 current_switch = rem;
6524 if (warning.switch_default
6525 && find_default_label(&statement->switchs) == NULL) {
6526 warningf(statement->base.source_position, "switch has no default case");
6531 return create_invalid_statement();
6534 static statement_t *parse_loop_body(statement_t *const loop)
6536 statement_t *const rem = current_loop;
6537 current_loop = loop;
6539 statement_t *const body = parse_statement();
6546 * Parse a while statement.
6548 static statement_t *parse_while(void)
6552 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
6553 statement->base.source_position = token.source_position;
6556 add_anchor_token(')');
6557 statement->whiles.condition = parse_expression();
6558 rem_anchor_token(')');
6561 statement->whiles.body = parse_loop_body(statement);
6565 return create_invalid_statement();
6569 * Parse a do statement.
6571 static statement_t *parse_do(void)
6575 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
6577 statement->base.source_position = token.source_position;
6579 add_anchor_token(T_while);
6580 statement->do_while.body = parse_loop_body(statement);
6581 rem_anchor_token(T_while);
6585 add_anchor_token(')');
6586 statement->do_while.condition = parse_expression();
6587 rem_anchor_token(')');
6593 return create_invalid_statement();
6597 * Parse a for statement.
6599 static statement_t *parse_for(void)
6603 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6604 statement->base.source_position = token.source_position;
6606 int top = environment_top();
6607 scope_t *last_scope = scope;
6608 set_scope(&statement->fors.scope);
6611 add_anchor_token(')');
6613 if(token.type != ';') {
6614 if(is_declaration_specifier(&token, false)) {
6615 parse_declaration(record_declaration);
6617 expression_t *const init = parse_expression();
6618 statement->fors.initialisation = init;
6619 if (warning.unused_value && !expression_has_effect(init)) {
6620 warningf(init->base.source_position,
6621 "initialisation of 'for'-statement has no effect");
6629 if(token.type != ';') {
6630 statement->fors.condition = parse_expression();
6633 if(token.type != ')') {
6634 expression_t *const step = parse_expression();
6635 statement->fors.step = step;
6636 if (warning.unused_value && !expression_has_effect(step)) {
6637 warningf(step->base.source_position,
6638 "step of 'for'-statement has no effect");
6641 rem_anchor_token(')');
6643 statement->fors.body = parse_loop_body(statement);
6645 assert(scope == &statement->fors.scope);
6646 set_scope(last_scope);
6647 environment_pop_to(top);
6652 rem_anchor_token(')');
6653 assert(scope == &statement->fors.scope);
6654 set_scope(last_scope);
6655 environment_pop_to(top);
6657 return create_invalid_statement();
6661 * Parse a goto statement.
6663 static statement_t *parse_goto(void)
6667 if(token.type != T_IDENTIFIER) {
6668 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6672 symbol_t *symbol = token.v.symbol;
6675 declaration_t *label = get_label(symbol);
6677 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6678 statement->base.source_position = token.source_position;
6680 statement->gotos.label = label;
6682 /* remember the goto's in a list for later checking */
6683 if (goto_last == NULL) {
6684 goto_first = &statement->gotos;
6686 goto_last->next = &statement->gotos;
6688 goto_last = &statement->gotos;
6694 return create_invalid_statement();
6698 * Parse a continue statement.
6700 static statement_t *parse_continue(void)
6702 statement_t *statement;
6703 if (current_loop == NULL) {
6704 errorf(HERE, "continue statement not within loop");
6707 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6709 statement->base.source_position = token.source_position;
6717 return create_invalid_statement();
6721 * Parse a break statement.
6723 static statement_t *parse_break(void)
6725 statement_t *statement;
6726 if (current_switch == NULL && current_loop == NULL) {
6727 errorf(HERE, "break statement not within loop or switch");
6730 statement = allocate_statement_zero(STATEMENT_BREAK);
6732 statement->base.source_position = token.source_position;
6740 return create_invalid_statement();
6744 * Check if a given declaration represents a local variable.
6746 static bool is_local_var_declaration(const declaration_t *declaration) {
6747 switch ((storage_class_tag_t) declaration->storage_class) {
6748 case STORAGE_CLASS_AUTO:
6749 case STORAGE_CLASS_REGISTER: {
6750 const type_t *type = skip_typeref(declaration->type);
6751 if(is_type_function(type)) {
6763 * Check if a given declaration represents a variable.
6765 static bool is_var_declaration(const declaration_t *declaration) {
6766 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6769 const type_t *type = skip_typeref(declaration->type);
6770 return !is_type_function(type);
6774 * Check if a given expression represents a local variable.
6776 static bool is_local_variable(const expression_t *expression)
6778 if (expression->base.kind != EXPR_REFERENCE) {
6781 const declaration_t *declaration = expression->reference.declaration;
6782 return is_local_var_declaration(declaration);
6786 * Check if a given expression represents a local variable and
6787 * return its declaration then, else return NULL.
6789 declaration_t *expr_is_variable(const expression_t *expression)
6791 if (expression->base.kind != EXPR_REFERENCE) {
6794 declaration_t *declaration = expression->reference.declaration;
6795 if (is_var_declaration(declaration))
6801 * Parse a return statement.
6803 static statement_t *parse_return(void)
6807 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6808 statement->base.source_position = token.source_position;
6810 expression_t *return_value = NULL;
6811 if(token.type != ';') {
6812 return_value = parse_expression();
6816 const type_t *const func_type = current_function->type;
6817 assert(is_type_function(func_type));
6818 type_t *const return_type = skip_typeref(func_type->function.return_type);
6820 if(return_value != NULL) {
6821 type_t *return_value_type = skip_typeref(return_value->base.type);
6823 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6824 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6825 warningf(statement->base.source_position,
6826 "'return' with a value, in function returning void");
6827 return_value = NULL;
6829 type_t *const res_type = semantic_assign(return_type,
6830 return_value, "'return'");
6831 if (res_type == NULL) {
6832 errorf(statement->base.source_position,
6833 "cannot return something of type '%T' in function returning '%T'",
6834 return_value->base.type, return_type);
6836 return_value = create_implicit_cast(return_value, res_type);
6839 /* check for returning address of a local var */
6840 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6841 const expression_t *expression = return_value->unary.value;
6842 if (is_local_variable(expression)) {
6843 warningf(statement->base.source_position,
6844 "function returns address of local variable");
6848 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6849 warningf(statement->base.source_position,
6850 "'return' without value, in function returning non-void");
6853 statement->returns.value = return_value;
6857 return create_invalid_statement();
6861 * Parse a declaration statement.
6863 static statement_t *parse_declaration_statement(void)
6865 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6867 statement->base.source_position = token.source_position;
6869 declaration_t *before = last_declaration;
6870 parse_declaration(record_declaration);
6872 if(before == NULL) {
6873 statement->declaration.declarations_begin = scope->declarations;
6875 statement->declaration.declarations_begin = before->next;
6877 statement->declaration.declarations_end = last_declaration;
6883 * Parse an expression statement, ie. expr ';'.
6885 static statement_t *parse_expression_statement(void)
6887 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6889 statement->base.source_position = token.source_position;
6890 expression_t *const expr = parse_expression();
6891 statement->expression.expression = expr;
6893 if (warning.unused_value && !expression_has_effect(expr)) {
6894 warningf(expr->base.source_position, "statement has no effect");
6901 return create_invalid_statement();
6905 * Parse a statement.
6907 static statement_t *parse_statement(void)
6909 statement_t *statement = NULL;
6911 /* declaration or statement */
6912 add_anchor_token(';');
6913 switch(token.type) {
6915 statement = parse_asm_statement();
6919 statement = parse_case_statement();
6923 statement = parse_default_statement();
6927 statement = parse_compound_statement();
6931 statement = parse_if();
6935 statement = parse_switch();
6939 statement = parse_while();
6943 statement = parse_do();
6947 statement = parse_for();
6951 statement = parse_goto();
6955 statement = parse_continue();
6959 statement = parse_break();
6963 statement = parse_return();
6967 if(warning.empty_statement) {
6968 warningf(HERE, "statement is empty");
6970 statement = create_empty_statement();
6975 if(look_ahead(1)->type == ':') {
6976 statement = parse_label_statement();
6980 if(is_typedef_symbol(token.v.symbol)) {
6981 statement = parse_declaration_statement();
6985 statement = parse_expression_statement();
6988 case T___extension__:
6989 /* this can be a prefix to a declaration or an expression statement */
6990 /* we simply eat it now and parse the rest with tail recursion */
6993 } while(token.type == T___extension__);
6994 statement = parse_statement();
6998 statement = parse_declaration_statement();
7002 statement = parse_expression_statement();
7005 rem_anchor_token(';');
7007 assert(statement != NULL
7008 && statement->base.source_position.input_name != NULL);
7014 * Parse a compound statement.
7016 static statement_t *parse_compound_statement(void)
7018 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7020 statement->base.source_position = token.source_position;
7023 add_anchor_token('}');
7025 int top = environment_top();
7026 scope_t *last_scope = scope;
7027 set_scope(&statement->compound.scope);
7029 statement_t *last_statement = NULL;
7031 while(token.type != '}' && token.type != T_EOF) {
7032 statement_t *sub_statement = parse_statement();
7033 if(is_invalid_statement(sub_statement)) {
7034 /* an error occurred. if we are at an anchor, return */
7040 if(last_statement != NULL) {
7041 last_statement->base.next = sub_statement;
7043 statement->compound.statements = sub_statement;
7046 while(sub_statement->base.next != NULL)
7047 sub_statement = sub_statement->base.next;
7049 last_statement = sub_statement;
7052 if(token.type == '}') {
7055 errorf(statement->base.source_position,
7056 "end of file while looking for closing '}'");
7060 rem_anchor_token('}');
7061 assert(scope == &statement->compound.scope);
7062 set_scope(last_scope);
7063 environment_pop_to(top);
7069 * Initialize builtin types.
7071 static void initialize_builtin_types(void)
7073 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7074 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7075 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7076 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7077 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7078 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7079 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7080 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7082 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7083 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7084 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7085 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7089 * Check for unused global static functions and variables
7091 static void check_unused_globals(void)
7093 if (!warning.unused_function && !warning.unused_variable)
7096 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7097 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7100 type_t *const type = decl->type;
7102 if (is_type_function(skip_typeref(type))) {
7103 if (!warning.unused_function || decl->is_inline)
7106 s = (decl->init.statement != NULL ? "defined" : "declared");
7108 if (!warning.unused_variable)
7114 warningf(decl->source_position, "'%#T' %s but not used",
7115 type, decl->symbol, s);
7120 * Parse a translation unit.
7122 static translation_unit_t *parse_translation_unit(void)
7124 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7126 assert(global_scope == NULL);
7127 global_scope = &unit->scope;
7129 assert(scope == NULL);
7130 set_scope(&unit->scope);
7132 initialize_builtin_types();
7134 while(token.type != T_EOF) {
7135 if (token.type == ';') {
7136 /* TODO error in strict mode */
7137 warningf(HERE, "stray ';' outside of function");
7140 parse_external_declaration();
7144 assert(scope == &unit->scope);
7146 last_declaration = NULL;
7148 assert(global_scope == &unit->scope);
7149 check_unused_globals();
7150 global_scope = NULL;
7158 * @return the translation unit or NULL if errors occurred.
7160 translation_unit_t *parse(void)
7162 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7163 label_stack = NEW_ARR_F(stack_entry_t, 0);
7164 diagnostic_count = 0;
7168 type_set_output(stderr);
7169 ast_set_output(stderr);
7171 lookahead_bufpos = 0;
7172 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7175 translation_unit_t *unit = parse_translation_unit();
7177 DEL_ARR_F(environment_stack);
7178 DEL_ARR_F(label_stack);
7184 * Initialize the parser.
7186 void init_parser(void)
7189 /* add predefined symbols for extended-decl-modifier */
7190 sym_align = symbol_table_insert("align");
7191 sym_allocate = symbol_table_insert("allocate");
7192 sym_dllimport = symbol_table_insert("dllimport");
7193 sym_dllexport = symbol_table_insert("dllexport");
7194 sym_naked = symbol_table_insert("naked");
7195 sym_noinline = symbol_table_insert("noinline");
7196 sym_noreturn = symbol_table_insert("noreturn");
7197 sym_nothrow = symbol_table_insert("nothrow");
7198 sym_novtable = symbol_table_insert("novtable");
7199 sym_property = symbol_table_insert("property");
7200 sym_get = symbol_table_insert("get");
7201 sym_put = symbol_table_insert("put");
7202 sym_selectany = symbol_table_insert("selectany");
7203 sym_thread = symbol_table_insert("thread");
7204 sym_uuid = symbol_table_insert("uuid");
7205 sym_deprecated = symbol_table_insert("deprecated");
7206 sym_restrict = symbol_table_insert("restrict");
7207 sym_noalias = symbol_table_insert("noalias");
7209 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7211 init_expression_parsers();
7212 obstack_init(&temp_obst);
7214 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7215 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7219 * Terminate the parser.
7221 void exit_parser(void)
7223 obstack_free(&temp_obst, NULL);