2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct gnu_attribute_t gnu_attribute_t;
52 struct gnu_attribute_t {
53 gnu_attribute_kind_t kind;
54 gnu_attribute_t *next;
63 typedef struct declaration_specifiers_t declaration_specifiers_t;
64 struct declaration_specifiers_t {
65 source_position_t source_position;
66 unsigned char declared_storage_class;
67 unsigned char alignment; /**< Alignment, 0 if not set. */
68 unsigned int is_inline : 1;
69 unsigned int deprecated : 1;
70 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
71 gnu_attribute_t *gnu_attributes; /**< list of GNU attributes */
72 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
73 symbol_t *get_property_sym; /**< the name of the get property if set. */
74 symbol_t *put_property_sym; /**< the name of the put property if set. */
79 * An environment for parsing initializers (and compound literals).
81 typedef struct parse_initializer_env_t {
82 type_t *type; /**< the type of the initializer. In case of an
83 array type with unspecified size this gets
84 adjusted to the actual size. */
85 declaration_t *declaration; /**< the declaration that is initialized if any */
86 bool must_be_constant;
87 } parse_initializer_env_t;
89 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
92 static token_t lookahead_buffer[MAX_LOOKAHEAD];
93 static int lookahead_bufpos;
94 static stack_entry_t *environment_stack = NULL;
95 static stack_entry_t *label_stack = NULL;
96 static scope_t *global_scope = NULL;
97 static scope_t *scope = NULL;
98 static declaration_t *last_declaration = NULL;
99 static declaration_t *current_function = NULL;
100 static switch_statement_t *current_switch = NULL;
101 static statement_t *current_loop = NULL;
102 static goto_statement_t *goto_first = NULL;
103 static goto_statement_t *goto_last = NULL;
104 static label_statement_t *label_first = NULL;
105 static label_statement_t *label_last = NULL;
106 static struct obstack temp_obst;
108 static source_position_t null_position = { NULL, 0 };
110 /* symbols for Microsoft extended-decl-modifier */
111 static const symbol_t *sym_align = NULL;
112 static const symbol_t *sym_allocate = NULL;
113 static const symbol_t *sym_dllimport = NULL;
114 static const symbol_t *sym_dllexport = NULL;
115 static const symbol_t *sym_naked = NULL;
116 static const symbol_t *sym_noinline = NULL;
117 static const symbol_t *sym_noreturn = NULL;
118 static const symbol_t *sym_nothrow = NULL;
119 static const symbol_t *sym_novtable = NULL;
120 static const symbol_t *sym_property = NULL;
121 static const symbol_t *sym_get = NULL;
122 static const symbol_t *sym_put = NULL;
123 static const symbol_t *sym_selectany = NULL;
124 static const symbol_t *sym_thread = NULL;
125 static const symbol_t *sym_uuid = NULL;
126 static const symbol_t *sym_deprecated = NULL;
127 static const symbol_t *sym_restrict = NULL;
128 static const symbol_t *sym_noalias = NULL;
130 /** The token anchor set */
131 static unsigned char token_anchor_set[T_LAST_TOKEN];
133 /** The current source position. */
134 #define HERE &token.source_position
136 static type_t *type_valist;
138 static statement_t *parse_compound_statement(void);
139 static statement_t *parse_statement(void);
141 static expression_t *parse_sub_expression(unsigned precedence);
142 static expression_t *parse_expression(void);
143 static type_t *parse_typename(void);
145 static void parse_compound_type_entries(declaration_t *compound_declaration);
146 static declaration_t *parse_declarator(
147 const declaration_specifiers_t *specifiers, bool may_be_abstract);
148 static declaration_t *record_declaration(declaration_t *declaration);
150 static void semantic_comparison(binary_expression_t *expression);
152 #define STORAGE_CLASSES \
159 #define TYPE_QUALIFIERS \
166 #ifdef PROVIDE_COMPLEX
167 #define COMPLEX_SPECIFIERS \
169 #define IMAGINARY_SPECIFIERS \
172 #define COMPLEX_SPECIFIERS
173 #define IMAGINARY_SPECIFIERS
176 #define TYPE_SPECIFIERS \
191 case T___builtin_va_list: \
196 #define DECLARATION_START \
201 #define TYPENAME_START \
206 * Allocate an AST node with given size and
207 * initialize all fields with zero.
209 static void *allocate_ast_zero(size_t size)
211 void *res = allocate_ast(size);
212 memset(res, 0, size);
216 static declaration_t *allocate_declaration_zero(void)
218 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
219 declaration->type = type_error_type;
220 declaration->alignment = 0;
225 * Returns the size of a statement node.
227 * @param kind the statement kind
229 static size_t get_statement_struct_size(statement_kind_t kind)
231 static const size_t sizes[] = {
232 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
233 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
234 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
235 [STATEMENT_RETURN] = sizeof(return_statement_t),
236 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
237 [STATEMENT_IF] = sizeof(if_statement_t),
238 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
239 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
240 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
241 [STATEMENT_BREAK] = sizeof(statement_base_t),
242 [STATEMENT_GOTO] = sizeof(goto_statement_t),
243 [STATEMENT_LABEL] = sizeof(label_statement_t),
244 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
245 [STATEMENT_WHILE] = sizeof(while_statement_t),
246 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
247 [STATEMENT_FOR] = sizeof(for_statement_t),
248 [STATEMENT_ASM] = sizeof(asm_statement_t),
249 [STATEMENT_MS_TRY] = sizeof(ms_try_statement_t)
251 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
252 assert(sizes[kind] != 0);
257 * Returns the size of an expression node.
259 * @param kind the expression kind
261 static size_t get_expression_struct_size(expression_kind_t kind)
263 static const size_t sizes[] = {
264 [EXPR_INVALID] = sizeof(expression_base_t),
265 [EXPR_REFERENCE] = sizeof(reference_expression_t),
266 [EXPR_CONST] = sizeof(const_expression_t),
267 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
268 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
269 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
270 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
271 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
272 [EXPR_CALL] = sizeof(call_expression_t),
273 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
274 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
275 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
276 [EXPR_SELECT] = sizeof(select_expression_t),
277 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
278 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
279 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
280 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
281 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
282 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
283 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
284 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
285 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
286 [EXPR_VA_START] = sizeof(va_start_expression_t),
287 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
288 [EXPR_STATEMENT] = sizeof(statement_expression_t),
290 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
291 return sizes[EXPR_UNARY_FIRST];
293 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
294 return sizes[EXPR_BINARY_FIRST];
296 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
297 assert(sizes[kind] != 0);
302 * Allocate a statement node of given kind and initialize all
305 static statement_t *allocate_statement_zero(statement_kind_t kind)
307 size_t size = get_statement_struct_size(kind);
308 statement_t *res = allocate_ast_zero(size);
310 res->base.kind = kind;
315 * Allocate an expression node of given kind and initialize all
318 static expression_t *allocate_expression_zero(expression_kind_t kind)
320 size_t size = get_expression_struct_size(kind);
321 expression_t *res = allocate_ast_zero(size);
323 res->base.kind = kind;
324 res->base.type = type_error_type;
329 * Creates a new invalid expression.
331 static expression_t *create_invalid_expression(void)
333 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
334 expression->base.source_position = token.source_position;
339 * Creates a new invalid statement.
341 static statement_t *create_invalid_statement(void)
343 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
344 statement->base.source_position = token.source_position;
349 * Allocate a new empty statement.
351 static statement_t *create_empty_statement(void)
353 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
354 statement->base.source_position = token.source_position;
359 * Returns the size of a type node.
361 * @param kind the type kind
363 static size_t get_type_struct_size(type_kind_t kind)
365 static const size_t sizes[] = {
366 [TYPE_ATOMIC] = sizeof(atomic_type_t),
367 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
368 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
369 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
370 [TYPE_ENUM] = sizeof(enum_type_t),
371 [TYPE_FUNCTION] = sizeof(function_type_t),
372 [TYPE_POINTER] = sizeof(pointer_type_t),
373 [TYPE_ARRAY] = sizeof(array_type_t),
374 [TYPE_BUILTIN] = sizeof(builtin_type_t),
375 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
376 [TYPE_TYPEOF] = sizeof(typeof_type_t),
378 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
379 assert(kind <= TYPE_TYPEOF);
380 assert(sizes[kind] != 0);
385 * Allocate a type node of given kind and initialize all
388 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
390 size_t size = get_type_struct_size(kind);
391 type_t *res = obstack_alloc(type_obst, size);
392 memset(res, 0, size);
394 res->base.kind = kind;
395 res->base.source_position = *source_position;
400 * Returns the size of an initializer node.
402 * @param kind the initializer kind
404 static size_t get_initializer_size(initializer_kind_t kind)
406 static const size_t sizes[] = {
407 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
408 [INITIALIZER_STRING] = sizeof(initializer_string_t),
409 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
410 [INITIALIZER_LIST] = sizeof(initializer_list_t),
411 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
413 assert(kind < sizeof(sizes) / sizeof(*sizes));
414 assert(sizes[kind] != 0);
419 * Allocate an initializer node of given kind and initialize all
422 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
424 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
431 * Free a type from the type obstack.
433 static void free_type(void *type)
435 obstack_free(type_obst, type);
439 * Returns the index of the top element of the environment stack.
441 static size_t environment_top(void)
443 return ARR_LEN(environment_stack);
447 * Returns the index of the top element of the label stack.
449 static size_t label_top(void)
451 return ARR_LEN(label_stack);
455 * Return the next token.
457 static inline void next_token(void)
459 token = lookahead_buffer[lookahead_bufpos];
460 lookahead_buffer[lookahead_bufpos] = lexer_token;
463 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
466 print_token(stderr, &token);
467 fprintf(stderr, "\n");
472 * Return the next token with a given lookahead.
474 static inline const token_t *look_ahead(int num)
476 assert(num > 0 && num <= MAX_LOOKAHEAD);
477 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
478 return &lookahead_buffer[pos];
482 * Adds a token to the token anchor set (a multi-set).
484 static void add_anchor_token(int token_type) {
485 assert(0 <= token_type && token_type < T_LAST_TOKEN);
486 ++token_anchor_set[token_type];
490 * Remove a token from the token anchor set (a multi-set).
492 static void rem_anchor_token(int token_type) {
493 assert(0 <= token_type && token_type < T_LAST_TOKEN);
494 --token_anchor_set[token_type];
497 static bool at_anchor(void) {
500 return token_anchor_set[token.type];
504 * Eat tokens until a matching token is found.
506 static void eat_until_matching_token(int type) {
507 unsigned parenthesis_count = 0;
508 unsigned brace_count = 0;
509 unsigned bracket_count = 0;
510 int end_token = type;
519 while(token.type != end_token ||
520 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
524 case '(': ++parenthesis_count; break;
525 case '{': ++brace_count; break;
526 case '[': ++bracket_count; break;
528 if(parenthesis_count > 0)
536 if(bracket_count > 0)
547 * Eat input tokens until an anchor is found.
549 static void eat_until_anchor(void) {
550 if(token.type == T_EOF)
552 while(token_anchor_set[token.type] == 0) {
553 if(token.type == '(' || token.type == '{' || token.type == '[')
554 eat_until_matching_token(token.type);
555 if(token.type == T_EOF)
561 static void eat_block(void) {
562 eat_until_matching_token('{');
563 if(token.type == '}')
568 * eat all token until a ';' is reached
569 * or a stop token is found.
571 static void eat_statement(void) {
572 eat_until_matching_token(';');
573 if(token.type == ';')
577 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
580 * Report a parse error because an expected token was not found.
582 static void parse_error_expected(const char *message, ...)
584 if(message != NULL) {
585 errorf(HERE, "%s", message);
588 va_start(ap, message);
589 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
594 * Report a type error.
596 static void type_error(const char *msg, const source_position_t *source_position,
599 errorf(source_position, "%s, but found type '%T'", msg, type);
603 * Report an incompatible type.
605 static void type_error_incompatible(const char *msg,
606 const source_position_t *source_position, type_t *type1, type_t *type2)
608 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
612 * Expect the the current token is the expected token.
613 * If not, generate an error, eat the current statement,
614 * and goto the end_error label.
616 #define expect(expected) \
618 if(UNLIKELY(token.type != (expected))) { \
619 parse_error_expected(NULL, (expected), 0); \
620 add_anchor_token(expected); \
621 eat_until_anchor(); \
622 rem_anchor_token(expected); \
628 static void set_scope(scope_t *new_scope)
631 scope->last_declaration = last_declaration;
635 last_declaration = new_scope->last_declaration;
639 * Search a symbol in a given namespace and returns its declaration or
640 * NULL if this symbol was not found.
642 static declaration_t *get_declaration(const symbol_t *const symbol,
643 const namespace_t namespc)
645 declaration_t *declaration = symbol->declaration;
646 for( ; declaration != NULL; declaration = declaration->symbol_next) {
647 if(declaration->namespc == namespc)
655 * pushs an environment_entry on the environment stack and links the
656 * corresponding symbol to the new entry
658 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
660 symbol_t *symbol = declaration->symbol;
661 namespace_t namespc = (namespace_t) declaration->namespc;
663 /* replace/add declaration into declaration list of the symbol */
664 declaration_t *iter = symbol->declaration;
666 symbol->declaration = declaration;
668 declaration_t *iter_last = NULL;
669 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
670 /* replace an entry? */
671 if(iter->namespc == namespc) {
672 if(iter_last == NULL) {
673 symbol->declaration = declaration;
675 iter_last->symbol_next = declaration;
677 declaration->symbol_next = iter->symbol_next;
682 assert(iter_last->symbol_next == NULL);
683 iter_last->symbol_next = declaration;
687 /* remember old declaration */
689 entry.symbol = symbol;
690 entry.old_declaration = iter;
691 entry.namespc = (unsigned short) namespc;
692 ARR_APP1(stack_entry_t, *stack_ptr, entry);
695 static void environment_push(declaration_t *declaration)
697 assert(declaration->source_position.input_name != NULL);
698 assert(declaration->parent_scope != NULL);
699 stack_push(&environment_stack, declaration);
702 static void label_push(declaration_t *declaration)
704 declaration->parent_scope = ¤t_function->scope;
705 stack_push(&label_stack, declaration);
709 * pops symbols from the environment stack until @p new_top is the top element
711 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
713 stack_entry_t *stack = *stack_ptr;
714 size_t top = ARR_LEN(stack);
717 assert(new_top <= top);
721 for(i = top; i > new_top; --i) {
722 stack_entry_t *entry = &stack[i - 1];
724 declaration_t *old_declaration = entry->old_declaration;
725 symbol_t *symbol = entry->symbol;
726 namespace_t namespc = (namespace_t)entry->namespc;
728 /* replace/remove declaration */
729 declaration_t *declaration = symbol->declaration;
730 assert(declaration != NULL);
731 if(declaration->namespc == namespc) {
732 if(old_declaration == NULL) {
733 symbol->declaration = declaration->symbol_next;
735 symbol->declaration = old_declaration;
738 declaration_t *iter_last = declaration;
739 declaration_t *iter = declaration->symbol_next;
740 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
741 /* replace an entry? */
742 if(iter->namespc == namespc) {
743 assert(iter_last != NULL);
744 iter_last->symbol_next = old_declaration;
745 if(old_declaration != NULL) {
746 old_declaration->symbol_next = iter->symbol_next;
751 assert(iter != NULL);
755 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
758 static void environment_pop_to(size_t new_top)
760 stack_pop_to(&environment_stack, new_top);
763 static void label_pop_to(size_t new_top)
765 stack_pop_to(&label_stack, new_top);
769 static int get_rank(const type_t *type)
771 assert(!is_typeref(type));
772 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
773 * and esp. footnote 108). However we can't fold constants (yet), so we
774 * can't decide whether unsigned int is possible, while int always works.
775 * (unsigned int would be preferable when possible... for stuff like
776 * struct { enum { ... } bla : 4; } ) */
777 if(type->kind == TYPE_ENUM)
778 return ATOMIC_TYPE_INT;
780 assert(type->kind == TYPE_ATOMIC);
781 return type->atomic.akind;
784 static type_t *promote_integer(type_t *type)
786 if(type->kind == TYPE_BITFIELD)
787 type = type->bitfield.base;
789 if(get_rank(type) < ATOMIC_TYPE_INT)
796 * Create a cast expression.
798 * @param expression the expression to cast
799 * @param dest_type the destination type
801 static expression_t *create_cast_expression(expression_t *expression,
804 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
806 cast->unary.value = expression;
807 cast->base.type = dest_type;
813 * Check if a given expression represents the 0 pointer constant.
815 static bool is_null_pointer_constant(const expression_t *expression)
817 /* skip void* cast */
818 if(expression->kind == EXPR_UNARY_CAST
819 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
820 expression = expression->unary.value;
823 /* TODO: not correct yet, should be any constant integer expression
824 * which evaluates to 0 */
825 if (expression->kind != EXPR_CONST)
828 type_t *const type = skip_typeref(expression->base.type);
829 if (!is_type_integer(type))
832 return expression->conste.v.int_value == 0;
836 * Create an implicit cast expression.
838 * @param expression the expression to cast
839 * @param dest_type the destination type
841 static expression_t *create_implicit_cast(expression_t *expression,
844 type_t *const source_type = expression->base.type;
846 if (source_type == dest_type)
849 return create_cast_expression(expression, dest_type);
852 /** Implements the rules from § 6.5.16.1 */
853 static type_t *semantic_assign(type_t *orig_type_left,
854 const expression_t *const right,
856 const source_position_t *source_position)
858 type_t *const orig_type_right = right->base.type;
859 type_t *const type_left = skip_typeref(orig_type_left);
860 type_t *const type_right = skip_typeref(orig_type_right);
862 if(is_type_pointer(type_left)) {
863 if(is_null_pointer_constant(right)) {
864 return orig_type_left;
865 } else if(is_type_pointer(type_right)) {
866 type_t *points_to_left
867 = skip_typeref(type_left->pointer.points_to);
868 type_t *points_to_right
869 = skip_typeref(type_right->pointer.points_to);
871 /* the left type has all qualifiers from the right type */
872 unsigned missing_qualifiers
873 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
874 if(missing_qualifiers != 0) {
875 errorf(source_position,
876 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
877 return orig_type_left;
880 points_to_left = get_unqualified_type(points_to_left);
881 points_to_right = get_unqualified_type(points_to_right);
883 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
884 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
885 return orig_type_left;
888 if (!types_compatible(points_to_left, points_to_right)) {
889 warningf(source_position,
890 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
891 orig_type_left, context, right, orig_type_right);
894 return orig_type_left;
895 } else if(is_type_integer(type_right)) {
896 warningf(source_position,
897 "%s makes pointer '%T' from integer '%T' without a cast",
898 context, orig_type_left, orig_type_right);
899 return orig_type_left;
901 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
902 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
903 && is_type_pointer(type_right))) {
904 return orig_type_left;
905 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
906 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
907 type_t *const unqual_type_left = get_unqualified_type(type_left);
908 type_t *const unqual_type_right = get_unqualified_type(type_right);
909 if (types_compatible(unqual_type_left, unqual_type_right)) {
910 return orig_type_left;
912 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
913 warningf(source_position,
914 "%s makes integer '%T' from pointer '%T' without a cast",
915 context, orig_type_left, orig_type_right);
916 return orig_type_left;
919 if (!is_type_valid(type_left))
922 if (!is_type_valid(type_right))
923 return orig_type_right;
928 static expression_t *parse_constant_expression(void)
930 /* start parsing at precedence 7 (conditional expression) */
931 expression_t *result = parse_sub_expression(7);
933 if(!is_constant_expression(result)) {
934 errorf(&result->base.source_position, "expression '%E' is not constant\n", result);
940 static expression_t *parse_assignment_expression(void)
942 /* start parsing at precedence 2 (assignment expression) */
943 return parse_sub_expression(2);
946 static type_t *make_global_typedef(const char *name, type_t *type)
948 symbol_t *const symbol = symbol_table_insert(name);
950 declaration_t *const declaration = allocate_declaration_zero();
951 declaration->namespc = NAMESPACE_NORMAL;
952 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
953 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
954 declaration->type = type;
955 declaration->symbol = symbol;
956 declaration->source_position = builtin_source_position;
958 record_declaration(declaration);
960 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
961 typedef_type->typedeft.declaration = declaration;
966 static string_t parse_string_literals(void)
968 assert(token.type == T_STRING_LITERAL);
969 string_t result = token.v.string;
973 while (token.type == T_STRING_LITERAL) {
974 result = concat_strings(&result, &token.v.string);
981 static const char *gnu_attribute_names[GNU_AK_LAST] = {
982 [GNU_AK_CONST] = "const",
983 [GNU_AK_VOLATILE] = "volatile",
984 [GNU_AK_CDECL] = "cdecl",
985 [GNU_AK_STDCALL] = "stdcall",
986 [GNU_AK_FASTCALL] = "fastcall",
987 [GNU_AK_DEPRECATED] = "deprecated",
988 [GNU_AK_NOINLINE] = "noinline",
989 [GNU_AK_NORETURN] = "noreturn",
990 [GNU_AK_NAKED] = "naked",
991 [GNU_AK_PURE] = "pure",
992 [GNU_AK_ALWAYS_INLINE] = "always_inline",
993 [GNU_AK_MALLOC] = "malloc",
994 [GNU_AK_WEAK] = "weak",
995 [GNU_AK_CONSTRUCTOR] = "constructor",
996 [GNU_AK_DESTRUCTOR] = "destructor",
997 [GNU_AK_NOTHROW] = "nothrow",
998 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
999 [GNU_AK_COMMON] = "coommon",
1000 [GNU_AK_NOCOMMON] = "nocommon",
1001 [GNU_AK_PACKED] = "packed",
1002 [GNU_AK_SHARED] = "shared",
1003 [GNU_AK_NOTSHARED] = "notshared",
1004 [GNU_AK_USED] = "used",
1005 [GNU_AK_UNUSED] = "unused",
1006 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1007 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1008 [GNU_AK_LONGCALL] = "longcall",
1009 [GNU_AK_SHORTCALL] = "shortcall",
1010 [GNU_AK_LONG_CALL] = "long_call",
1011 [GNU_AK_SHORT_CALL] = "short_call",
1012 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1013 [GNU_AK_INTERRUPT] = "interrupt",
1014 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1015 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1016 [GNU_AK_NESTING] = "nesting",
1017 [GNU_AK_NEAR] = "near",
1018 [GNU_AK_FAR] = "far",
1019 [GNU_AK_SIGNAL] = "signal",
1020 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1021 [GNU_AK_TINY_DATA] = "tiny_data",
1022 [GNU_AK_SAVEALL] = "saveall",
1023 [GNU_AK_FLATTEN] = "flatten",
1024 [GNU_AK_SSEREGPARM] = "sseregparm",
1025 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1026 [GNU_AK_RETURN_TWICE] = "return_twice",
1027 [GNU_AK_MAY_ALIAS] = "may_alias",
1028 [GNU_AK_MS_STRUCT] = "ms_struct",
1029 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1030 [GNU_AK_DLLIMPORT] = "dllimport",
1031 [GNU_AK_DLLEXPORT] = "dllexport",
1032 [GNU_AK_ALIGNED] = "aligned",
1033 [GNU_AK_ALIAS] = "alias",
1034 [GNU_AK_SECTION] = "section",
1035 [GNU_AK_FORMAT] = "format",
1036 [GNU_AK_FORMAT_ARG] = "format_arg",
1037 [GNU_AK_WEAKREF] = "weakref",
1038 [GNU_AK_NONNULL] = "nonnull",
1039 [GNU_AK_TLS_MODEL] = "tls_model",
1040 [GNU_AK_VISIBILITY] = "visibility",
1041 [GNU_AK_REGPARM] = "regparm",
1042 [GNU_AK_MODEL] = "model",
1043 [GNU_AK_TRAP_EXIT] = "trap_exit",
1044 [GNU_AK_SP_SWITCH] = "sp_switch",
1045 [GNU_AK_SENTINEL] = "sentinel"
1049 * compare two string, ignoring double underscores on the second.
1051 static int strcmp_underscore(const char *s1, const char *s2) {
1052 if(s2[0] == '_' && s2[1] == '_') {
1054 size_t l1 = strlen(s1);
1055 if(l1 + 2 != strlen(s2)) {
1059 return strncmp(s1, s2, l1);
1061 return strcmp(s1, s2);
1065 * Allocate a new gnu temporal attribute.
1067 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1068 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1069 attribute->kind = kind;
1070 attribute->next = NULL;
1071 attribute->invalid = false;
1072 attribute->have_arguments = false;
1077 * parse one constant expression argument.
1079 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1080 expression_t *expression;
1081 add_anchor_token(')');
1082 expression = parse_constant_expression();
1083 rem_anchor_token(')');
1088 attribute->invalid = true;
1092 * parse a list of constant expressions arguments.
1094 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1095 expression_t *expression;
1096 add_anchor_token(')');
1097 add_anchor_token(',');
1099 expression = parse_constant_expression();
1100 if(token.type != ',')
1104 rem_anchor_token(',');
1105 rem_anchor_token(')');
1110 attribute->invalid = true;
1114 * parse one string literal argument.
1116 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute, string_t *string) {
1117 add_anchor_token('(');
1118 if(token.type != T_STRING_LITERAL) {
1119 parse_error_expected("while parsing attribute directive", T_STRING_LITERAL, 0);
1122 *string = parse_string_literals();
1123 rem_anchor_token('(');
1127 attribute->invalid = true;
1131 * parse one tls model.
1133 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1134 static const char *tls_models[] = {
1140 string_t string = { NULL, 0 };
1141 parse_gnu_attribute_string_arg(attribute, &string);
1142 if(string.begin != NULL) {
1143 for(size_t i = 0; i < 4; ++i) {
1144 if(strcmp(tls_models[i], string.begin) == 0) {
1145 attribute->u.value = i;
1150 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1151 attribute->invalid = true;
1155 * parse one tls model.
1157 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1158 static const char *visibilities[] = {
1164 string_t string = { NULL, 0 };
1165 parse_gnu_attribute_string_arg(attribute, &string);
1166 if(string.begin != NULL) {
1167 for(size_t i = 0; i < 4; ++i) {
1168 if(strcmp(visibilities[i], string.begin) == 0) {
1169 attribute->u.value = i;
1174 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1175 attribute->invalid = true;
1179 * parse one (code) model.
1181 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1182 static const char *visibilities[] = {
1187 string_t string = { NULL, 0 };
1188 parse_gnu_attribute_string_arg(attribute, &string);
1189 if(string.begin != NULL) {
1190 for(int i = 0; i < 3; ++i) {
1191 if(strcmp(visibilities[i], string.begin) == 0) {
1192 attribute->u.value = i;
1197 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1198 attribute->invalid = true;
1202 * parse one interrupt argument.
1204 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1205 static const char *interrupts[] = {
1212 string_t string = { NULL, 0 };
1213 parse_gnu_attribute_string_arg(attribute, &string);
1214 if(string.begin != NULL) {
1215 for(size_t i = 0; i < 5; ++i) {
1216 if(strcmp(interrupts[i], string.begin) == 0) {
1217 attribute->u.value = i;
1222 errorf(HERE, "'%s' is an interrupt", string.begin);
1223 attribute->invalid = true;
1227 * parse ( identifier, const expression, const expression )
1229 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1230 static const char *format_names[] = {
1238 if(token.type != T_IDENTIFIER) {
1239 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER, 0);
1242 const char *name = token.v.symbol->string;
1243 for(i = 0; i < 4; ++i) {
1244 if(strcmp_underscore(format_names[i], name) == 0)
1248 if(warning.attribute)
1249 warningf(HERE, "'%s' is an unrecognized format function type", name);
1254 add_anchor_token(')');
1255 add_anchor_token(',');
1256 parse_constant_expression();
1257 rem_anchor_token(',');
1258 rem_anchor_token('(');
1261 add_anchor_token(')');
1262 parse_constant_expression();
1263 rem_anchor_token('(');
1267 attribute->u.value = true;
1271 * Parse one GNU attribute.
1273 * Note that attribute names can be specified WITH or WITHOUT
1274 * double underscores, ie const or __const__.
1276 * The following attributes are parsed without arguments
1301 * no_instrument_function
1302 * warn_unused_result
1319 * externally_visible
1327 * The following attributes are parsed with arguments
1328 * aligned( const expression )
1329 * alias( string literal )
1330 * section( string literal )
1331 * format( identifier, const expression, const expression )
1332 * format_arg( const expression )
1333 * tls_model( string literal )
1334 * visibility( string literal )
1335 * regparm( const expression )
1336 * model( string leteral )
1337 * trap_exit( const expression )
1338 * sp_switch( string literal )
1340 * The following attributes might have arguments
1341 * weak_ref( string literal )
1342 * non_null( const expression // ',' )
1343 * interrupt( string literal )
1344 * sentinel( constant expression )
1346 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1348 gnu_attribute_t *head = *attributes;
1349 gnu_attribute_t *last = *attributes;
1350 gnu_attribute_t *attribute;
1352 eat(T___attribute__);
1356 if(token.type != ')') {
1357 /* find the end of the list */
1359 while(last->next != NULL)
1363 /* non-empty attribute list */
1366 if(token.type == T_const) {
1368 } else if(token.type == T_volatile) {
1370 } else if(token.type == T_cdecl) {
1371 /* __attribute__((cdecl)), WITH ms mode */
1373 } else if(token.type != T_IDENTIFIER) {
1374 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER, 0);
1377 const symbol_t *sym = token.v.symbol;
1382 for(i = 0; i < GNU_AK_LAST; ++i) {
1383 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1386 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1389 if(kind == GNU_AK_LAST) {
1390 if(warning.attribute)
1391 warningf(HERE, "'%s' attribute directive ignored", name);
1393 /* skip possible arguments */
1394 if(token.type == '(') {
1395 eat_until_matching_token(')');
1398 /* check for arguments */
1399 attribute = allocate_gnu_attribute(kind);
1400 if(token.type == '(') {
1402 if(token.type == ')') {
1403 /* empty args are allowed */
1406 attribute->have_arguments = true;
1411 case GNU_AK_VOLATILE:
1413 case GNU_AK_STDCALL:
1414 case GNU_AK_FASTCALL:
1415 case GNU_AK_DEPRECATED:
1416 case GNU_AK_NOINLINE:
1417 case GNU_AK_NORETURN:
1420 case GNU_AK_ALWAYS_INLINE:
1423 case GNU_AK_CONSTRUCTOR:
1424 case GNU_AK_DESTRUCTOR:
1425 case GNU_AK_NOTHROW:
1426 case GNU_AK_TRANSPARENT_UNION:
1428 case GNU_AK_NOCOMMON:
1431 case GNU_AK_NOTSHARED:
1434 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1435 case GNU_AK_WARN_UNUSED_RESULT:
1436 case GNU_AK_LONGCALL:
1437 case GNU_AK_SHORTCALL:
1438 case GNU_AK_LONG_CALL:
1439 case GNU_AK_SHORT_CALL:
1440 case GNU_AK_FUNCTION_VECTOR:
1441 case GNU_AK_INTERRUPT_HANDLER:
1442 case GNU_AK_NMI_HANDLER:
1443 case GNU_AK_NESTING:
1447 case GNU_AK_EIGTHBIT_DATA:
1448 case GNU_AK_TINY_DATA:
1449 case GNU_AK_SAVEALL:
1450 case GNU_AK_FLATTEN:
1451 case GNU_AK_SSEREGPARM:
1452 case GNU_AK_EXTERNALLY_VISIBLE:
1453 case GNU_AK_RETURN_TWICE:
1454 case GNU_AK_MAY_ALIAS:
1455 case GNU_AK_MS_STRUCT:
1456 case GNU_AK_GCC_STRUCT:
1457 case GNU_AK_DLLIMPORT:
1458 case GNU_AK_DLLEXPORT:
1459 if(attribute->have_arguments) {
1460 /* should have no arguments */
1461 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1462 eat_until_matching_token('(');
1463 /* we have already consumed '(', so we stop before ')', eat it */
1465 attribute->invalid = true;
1469 case GNU_AK_ALIGNED:
1470 case GNU_AK_FORMAT_ARG:
1471 case GNU_AK_REGPARM:
1472 case GNU_AK_TRAP_EXIT:
1473 if(!attribute->have_arguments) {
1474 /* should have arguments */
1475 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1476 attribute->invalid = true;
1478 parse_gnu_attribute_const_arg(attribute);
1481 case GNU_AK_SECTION:
1482 case GNU_AK_SP_SWITCH:
1483 if(!attribute->have_arguments) {
1484 /* should have arguments */
1485 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1486 attribute->invalid = true;
1488 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1491 if(!attribute->have_arguments) {
1492 /* should have arguments */
1493 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1494 attribute->invalid = true;
1496 parse_gnu_attribute_format_args(attribute);
1498 case GNU_AK_WEAKREF:
1499 /* may have one string argument */
1500 if(attribute->have_arguments)
1501 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1503 case GNU_AK_NONNULL:
1504 if(attribute->have_arguments)
1505 parse_gnu_attribute_const_arg_list(attribute);
1507 case GNU_AK_TLS_MODEL:
1508 if(!attribute->have_arguments) {
1509 /* should have arguments */
1510 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1512 parse_gnu_attribute_tls_model_arg(attribute);
1514 case GNU_AK_VISIBILITY:
1515 if(!attribute->have_arguments) {
1516 /* should have arguments */
1517 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1519 parse_gnu_attribute_visibility_arg(attribute);
1522 if(!attribute->have_arguments) {
1523 /* should have arguments */
1524 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1526 parse_gnu_attribute_model_arg(attribute);
1527 case GNU_AK_INTERRUPT:
1528 /* may have one string argument */
1529 if(attribute->have_arguments)
1530 parse_gnu_attribute_interrupt_arg(attribute);
1532 case GNU_AK_SENTINEL:
1533 /* may have one string argument */
1534 if(attribute->have_arguments)
1535 parse_gnu_attribute_const_arg(attribute);
1538 /* already handled */
1542 if(attribute != NULL) {
1544 last->next = attribute;
1547 head = last = attribute;
1551 if(token.type != ',')
1563 * Parse GNU attributes.
1565 static void parse_attributes(gnu_attribute_t **attributes)
1568 switch(token.type) {
1569 case T___attribute__: {
1570 parse_gnu_attribute(attributes);
1576 if(token.type != T_STRING_LITERAL) {
1577 parse_error_expected("while parsing assembler attribute",
1578 T_STRING_LITERAL, 0);
1579 eat_until_matching_token('(');
1582 parse_string_literals();
1587 goto attributes_finished;
1591 attributes_finished:
1596 static designator_t *parse_designation(void)
1598 designator_t *result = NULL;
1599 designator_t *last = NULL;
1602 designator_t *designator;
1603 switch(token.type) {
1605 designator = allocate_ast_zero(sizeof(designator[0]));
1606 designator->source_position = token.source_position;
1608 add_anchor_token(']');
1609 designator->array_index = parse_constant_expression();
1610 rem_anchor_token(']');
1614 designator = allocate_ast_zero(sizeof(designator[0]));
1615 designator->source_position = token.source_position;
1617 if(token.type != T_IDENTIFIER) {
1618 parse_error_expected("while parsing designator",
1622 designator->symbol = token.v.symbol;
1630 assert(designator != NULL);
1632 last->next = designator;
1634 result = designator;
1642 static initializer_t *initializer_from_string(array_type_t *type,
1643 const string_t *const string)
1645 /* TODO: check len vs. size of array type */
1648 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1649 initializer->string.string = *string;
1654 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1655 wide_string_t *const string)
1657 /* TODO: check len vs. size of array type */
1660 initializer_t *const initializer =
1661 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1662 initializer->wide_string.string = *string;
1668 * Build an initializer from a given expression.
1670 static initializer_t *initializer_from_expression(type_t *orig_type,
1671 expression_t *expression)
1673 /* TODO check that expression is a constant expression */
1675 /* § 6.7.8.14/15 char array may be initialized by string literals */
1676 type_t *type = skip_typeref(orig_type);
1677 type_t *expr_type_orig = expression->base.type;
1678 type_t *expr_type = skip_typeref(expr_type_orig);
1679 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1680 array_type_t *const array_type = &type->array;
1681 type_t *const element_type = skip_typeref(array_type->element_type);
1683 if (element_type->kind == TYPE_ATOMIC) {
1684 atomic_type_kind_t akind = element_type->atomic.akind;
1685 switch (expression->kind) {
1686 case EXPR_STRING_LITERAL:
1687 if (akind == ATOMIC_TYPE_CHAR
1688 || akind == ATOMIC_TYPE_SCHAR
1689 || akind == ATOMIC_TYPE_UCHAR) {
1690 return initializer_from_string(array_type,
1691 &expression->string.value);
1694 case EXPR_WIDE_STRING_LITERAL: {
1695 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1696 if (get_unqualified_type(element_type) == bare_wchar_type) {
1697 return initializer_from_wide_string(array_type,
1698 &expression->wide_string.value);
1708 type_t *const res_type = semantic_assign(type, expression, "initializer",
1709 &expression->base.source_position);
1710 if (res_type == NULL)
1713 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1714 result->value.value = create_implicit_cast(expression, res_type);
1720 * Checks if a given expression can be used as an constant initializer.
1722 static bool is_initializer_constant(const expression_t *expression)
1724 return is_constant_expression(expression)
1725 || is_address_constant(expression);
1729 * Parses an scalar initializer.
1731 * § 6.7.8.11; eat {} without warning
1733 static initializer_t *parse_scalar_initializer(type_t *type,
1734 bool must_be_constant)
1736 /* there might be extra {} hierarchies */
1738 while(token.type == '{') {
1741 warningf(HERE, "extra curly braces around scalar initializer");
1746 expression_t *expression = parse_assignment_expression();
1747 if(must_be_constant && !is_initializer_constant(expression)) {
1748 errorf(&expression->base.source_position,
1749 "Initialisation expression '%E' is not constant\n",
1753 initializer_t *initializer = initializer_from_expression(type, expression);
1755 if(initializer == NULL) {
1756 errorf(&expression->base.source_position,
1757 "expression '%E' (type '%T') doesn't match expected type '%T'",
1758 expression, expression->base.type, type);
1763 bool additional_warning_displayed = false;
1765 if(token.type == ',') {
1768 if(token.type != '}') {
1769 if(!additional_warning_displayed) {
1770 warningf(HERE, "additional elements in scalar initializer");
1771 additional_warning_displayed = true;
1782 * An entry in the type path.
1784 typedef struct type_path_entry_t type_path_entry_t;
1785 struct type_path_entry_t {
1786 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1788 size_t index; /**< For array types: the current index. */
1789 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1794 * A type path expression a position inside compound or array types.
1796 typedef struct type_path_t type_path_t;
1797 struct type_path_t {
1798 type_path_entry_t *path; /**< An flexible array containing the current path. */
1799 type_t *top_type; /**< type of the element the path points */
1800 size_t max_index; /**< largest index in outermost array */
1804 * Prints a type path for debugging.
1806 static __attribute__((unused)) void debug_print_type_path(
1807 const type_path_t *path)
1809 size_t len = ARR_LEN(path->path);
1811 for(size_t i = 0; i < len; ++i) {
1812 const type_path_entry_t *entry = & path->path[i];
1814 type_t *type = skip_typeref(entry->type);
1815 if(is_type_compound(type)) {
1816 /* in gcc mode structs can have no members */
1817 if(entry->v.compound_entry == NULL) {
1821 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1822 } else if(is_type_array(type)) {
1823 fprintf(stderr, "[%zd]", entry->v.index);
1825 fprintf(stderr, "-INVALID-");
1828 if(path->top_type != NULL) {
1829 fprintf(stderr, " (");
1830 print_type(path->top_type);
1831 fprintf(stderr, ")");
1836 * Return the top type path entry, ie. in a path
1837 * (type).a.b returns the b.
1839 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1841 size_t len = ARR_LEN(path->path);
1843 return &path->path[len-1];
1847 * Enlarge the type path by an (empty) element.
1849 static type_path_entry_t *append_to_type_path(type_path_t *path)
1851 size_t len = ARR_LEN(path->path);
1852 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1854 type_path_entry_t *result = & path->path[len];
1855 memset(result, 0, sizeof(result[0]));
1860 * Descending into a sub-type. Enter the scope of the current
1863 static void descend_into_subtype(type_path_t *path)
1865 type_t *orig_top_type = path->top_type;
1866 type_t *top_type = skip_typeref(orig_top_type);
1868 assert(is_type_compound(top_type) || is_type_array(top_type));
1870 type_path_entry_t *top = append_to_type_path(path);
1871 top->type = top_type;
1873 if(is_type_compound(top_type)) {
1874 declaration_t *declaration = top_type->compound.declaration;
1875 declaration_t *entry = declaration->scope.declarations;
1876 top->v.compound_entry = entry;
1879 path->top_type = entry->type;
1881 path->top_type = NULL;
1884 assert(is_type_array(top_type));
1887 path->top_type = top_type->array.element_type;
1892 * Pop an entry from the given type path, ie. returning from
1893 * (type).a.b to (type).a
1895 static void ascend_from_subtype(type_path_t *path)
1897 type_path_entry_t *top = get_type_path_top(path);
1899 path->top_type = top->type;
1901 size_t len = ARR_LEN(path->path);
1902 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1906 * Pop entries from the given type path until the given
1907 * path level is reached.
1909 static void ascend_to(type_path_t *path, size_t top_path_level)
1911 size_t len = ARR_LEN(path->path);
1913 while(len > top_path_level) {
1914 ascend_from_subtype(path);
1915 len = ARR_LEN(path->path);
1919 static bool walk_designator(type_path_t *path, const designator_t *designator,
1920 bool used_in_offsetof)
1922 for( ; designator != NULL; designator = designator->next) {
1923 type_path_entry_t *top = get_type_path_top(path);
1924 type_t *orig_type = top->type;
1926 type_t *type = skip_typeref(orig_type);
1928 if(designator->symbol != NULL) {
1929 symbol_t *symbol = designator->symbol;
1930 if(!is_type_compound(type)) {
1931 if(is_type_valid(type)) {
1932 errorf(&designator->source_position,
1933 "'.%Y' designator used for non-compound type '%T'",
1939 declaration_t *declaration = type->compound.declaration;
1940 declaration_t *iter = declaration->scope.declarations;
1941 for( ; iter != NULL; iter = iter->next) {
1942 if(iter->symbol == symbol) {
1947 errorf(&designator->source_position,
1948 "'%T' has no member named '%Y'", orig_type, symbol);
1951 if(used_in_offsetof) {
1952 type_t *real_type = skip_typeref(iter->type);
1953 if(real_type->kind == TYPE_BITFIELD) {
1954 errorf(&designator->source_position,
1955 "offsetof designator '%Y' may not specify bitfield",
1961 top->type = orig_type;
1962 top->v.compound_entry = iter;
1963 orig_type = iter->type;
1965 expression_t *array_index = designator->array_index;
1966 assert(designator->array_index != NULL);
1968 if(!is_type_array(type)) {
1969 if(is_type_valid(type)) {
1970 errorf(&designator->source_position,
1971 "[%E] designator used for non-array type '%T'",
1972 array_index, orig_type);
1976 if(!is_type_valid(array_index->base.type)) {
1980 long index = fold_constant(array_index);
1981 if(!used_in_offsetof) {
1983 errorf(&designator->source_position,
1984 "array index [%E] must be positive", array_index);
1987 if(type->array.size_constant == true) {
1988 long array_size = type->array.size;
1989 if(index >= array_size) {
1990 errorf(&designator->source_position,
1991 "designator [%E] (%d) exceeds array size %d",
1992 array_index, index, array_size);
1998 top->type = orig_type;
1999 top->v.index = (size_t) index;
2000 orig_type = type->array.element_type;
2002 path->top_type = orig_type;
2004 if(designator->next != NULL) {
2005 descend_into_subtype(path);
2014 static void advance_current_object(type_path_t *path, size_t top_path_level)
2016 type_path_entry_t *top = get_type_path_top(path);
2018 type_t *type = skip_typeref(top->type);
2019 if(is_type_union(type)) {
2020 /* in unions only the first element is initialized */
2021 top->v.compound_entry = NULL;
2022 } else if(is_type_struct(type)) {
2023 declaration_t *entry = top->v.compound_entry;
2025 entry = entry->next;
2026 top->v.compound_entry = entry;
2028 path->top_type = entry->type;
2032 assert(is_type_array(type));
2036 if(!type->array.size_constant || top->v.index < type->array.size) {
2041 /* we're past the last member of the current sub-aggregate, try if we
2042 * can ascend in the type hierarchy and continue with another subobject */
2043 size_t len = ARR_LEN(path->path);
2045 if(len > top_path_level) {
2046 ascend_from_subtype(path);
2047 advance_current_object(path, top_path_level);
2049 path->top_type = NULL;
2054 * skip until token is found.
2056 static void skip_until(int type) {
2057 while(token.type != type) {
2058 if(token.type == T_EOF)
2065 * skip any {...} blocks until a closing braket is reached.
2067 static void skip_initializers(void)
2069 if(token.type == '{')
2072 while(token.type != '}') {
2073 if(token.type == T_EOF)
2075 if(token.type == '{') {
2083 static initializer_t *create_empty_initializer(void)
2085 static initializer_t empty_initializer
2086 = { .list = { { INITIALIZER_LIST }, 0 } };
2087 return &empty_initializer;
2091 * Parse a part of an initialiser for a struct or union,
2093 static initializer_t *parse_sub_initializer(type_path_t *path,
2094 type_t *outer_type, size_t top_path_level,
2095 parse_initializer_env_t *env)
2097 if(token.type == '}') {
2098 /* empty initializer */
2099 return create_empty_initializer();
2102 type_t *orig_type = path->top_type;
2103 type_t *type = NULL;
2105 if (orig_type == NULL) {
2106 /* We are initializing an empty compound. */
2108 type = skip_typeref(orig_type);
2110 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2111 * initializers in this case. */
2112 if(!is_type_valid(type)) {
2113 skip_initializers();
2114 return create_empty_initializer();
2118 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2121 designator_t *designator = NULL;
2122 if(token.type == '.' || token.type == '[') {
2123 designator = parse_designation();
2125 /* reset path to toplevel, evaluate designator from there */
2126 ascend_to(path, top_path_level);
2127 if(!walk_designator(path, designator, false)) {
2128 /* can't continue after designation error */
2132 initializer_t *designator_initializer
2133 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2134 designator_initializer->designator.designator = designator;
2135 ARR_APP1(initializer_t*, initializers, designator_initializer);
2140 if(token.type == '{') {
2141 if(type != NULL && is_type_scalar(type)) {
2142 sub = parse_scalar_initializer(type, env->must_be_constant);
2146 if (env->declaration != NULL)
2147 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2148 env->declaration->symbol);
2150 errorf(HERE, "extra brace group at end of initializer");
2152 descend_into_subtype(path);
2154 add_anchor_token('}');
2155 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2157 rem_anchor_token('}');
2160 ascend_from_subtype(path);
2164 goto error_parse_next;
2168 /* must be an expression */
2169 expression_t *expression = parse_assignment_expression();
2171 if(env->must_be_constant && !is_initializer_constant(expression)) {
2172 errorf(&expression->base.source_position,
2173 "Initialisation expression '%E' is not constant\n",
2178 /* we are already outside, ... */
2182 /* handle { "string" } special case */
2183 if((expression->kind == EXPR_STRING_LITERAL
2184 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2185 && outer_type != NULL) {
2186 sub = initializer_from_expression(outer_type, expression);
2188 if(token.type == ',') {
2191 if(token.type != '}') {
2192 warningf(HERE, "excessive elements in initializer for type '%T'",
2195 /* TODO: eat , ... */
2200 /* descend into subtypes until expression matches type */
2202 orig_type = path->top_type;
2203 type = skip_typeref(orig_type);
2205 sub = initializer_from_expression(orig_type, expression);
2209 if(!is_type_valid(type)) {
2212 if(is_type_scalar(type)) {
2213 errorf(&expression->base.source_position,
2214 "expression '%E' doesn't match expected type '%T'",
2215 expression, orig_type);
2219 descend_into_subtype(path);
2223 /* update largest index of top array */
2224 const type_path_entry_t *first = &path->path[0];
2225 type_t *first_type = first->type;
2226 first_type = skip_typeref(first_type);
2227 if(is_type_array(first_type)) {
2228 size_t index = first->v.index;
2229 if(index > path->max_index)
2230 path->max_index = index;
2234 /* append to initializers list */
2235 ARR_APP1(initializer_t*, initializers, sub);
2238 if(env->declaration != NULL)
2239 warningf(HERE, "excess elements in struct initializer for '%Y'",
2240 env->declaration->symbol);
2242 warningf(HERE, "excess elements in struct initializer");
2246 if(token.type == '}') {
2250 if(token.type == '}') {
2255 /* advance to the next declaration if we are not at the end */
2256 advance_current_object(path, top_path_level);
2257 orig_type = path->top_type;
2258 if(orig_type != NULL)
2259 type = skip_typeref(orig_type);
2265 size_t len = ARR_LEN(initializers);
2266 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2267 initializer_t *result = allocate_ast_zero(size);
2268 result->kind = INITIALIZER_LIST;
2269 result->list.len = len;
2270 memcpy(&result->list.initializers, initializers,
2271 len * sizeof(initializers[0]));
2273 DEL_ARR_F(initializers);
2274 ascend_to(path, top_path_level);
2279 skip_initializers();
2280 DEL_ARR_F(initializers);
2281 ascend_to(path, top_path_level);
2286 * Parses an initializer. Parsers either a compound literal
2287 * (env->declaration == NULL) or an initializer of a declaration.
2289 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2291 type_t *type = skip_typeref(env->type);
2292 initializer_t *result = NULL;
2295 if(is_type_scalar(type)) {
2296 result = parse_scalar_initializer(type, env->must_be_constant);
2297 } else if(token.type == '{') {
2301 memset(&path, 0, sizeof(path));
2302 path.top_type = env->type;
2303 path.path = NEW_ARR_F(type_path_entry_t, 0);
2305 descend_into_subtype(&path);
2307 add_anchor_token('}');
2308 result = parse_sub_initializer(&path, env->type, 1, env);
2309 rem_anchor_token('}');
2311 max_index = path.max_index;
2312 DEL_ARR_F(path.path);
2316 /* parse_scalar_initializer() also works in this case: we simply
2317 * have an expression without {} around it */
2318 result = parse_scalar_initializer(type, env->must_be_constant);
2321 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2322 * the array type size */
2323 if(is_type_array(type) && type->array.size_expression == NULL
2324 && result != NULL) {
2326 switch (result->kind) {
2327 case INITIALIZER_LIST:
2328 size = max_index + 1;
2331 case INITIALIZER_STRING:
2332 size = result->string.string.size;
2335 case INITIALIZER_WIDE_STRING:
2336 size = result->wide_string.string.size;
2340 internal_errorf(HERE, "invalid initializer type");
2343 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2344 cnst->base.type = type_size_t;
2345 cnst->conste.v.int_value = size;
2347 type_t *new_type = duplicate_type(type);
2349 new_type->array.size_expression = cnst;
2350 new_type->array.size_constant = true;
2351 new_type->array.size = size;
2352 env->type = new_type;
2360 static declaration_t *append_declaration(declaration_t *declaration);
2362 static declaration_t *parse_compound_type_specifier(bool is_struct)
2364 gnu_attribute_t *attributes = NULL;
2371 symbol_t *symbol = NULL;
2372 declaration_t *declaration = NULL;
2374 if (token.type == T___attribute__) {
2375 parse_attributes(&attributes);
2378 if(token.type == T_IDENTIFIER) {
2379 symbol = token.v.symbol;
2383 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2385 declaration = get_declaration(symbol, NAMESPACE_UNION);
2387 } else if(token.type != '{') {
2389 parse_error_expected("while parsing struct type specifier",
2390 T_IDENTIFIER, '{', 0);
2392 parse_error_expected("while parsing union type specifier",
2393 T_IDENTIFIER, '{', 0);
2399 if(declaration == NULL) {
2400 declaration = allocate_declaration_zero();
2401 declaration->namespc =
2402 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2403 declaration->source_position = token.source_position;
2404 declaration->symbol = symbol;
2405 declaration->parent_scope = scope;
2406 if (symbol != NULL) {
2407 environment_push(declaration);
2409 append_declaration(declaration);
2412 if(token.type == '{') {
2413 if(declaration->init.is_defined) {
2414 assert(symbol != NULL);
2415 errorf(HERE, "multiple definitions of '%s %Y'",
2416 is_struct ? "struct" : "union", symbol);
2417 declaration->scope.declarations = NULL;
2419 declaration->init.is_defined = true;
2421 parse_compound_type_entries(declaration);
2422 parse_attributes(&attributes);
2428 static void parse_enum_entries(type_t *const enum_type)
2432 if(token.type == '}') {
2434 errorf(HERE, "empty enum not allowed");
2438 add_anchor_token('}');
2440 if(token.type != T_IDENTIFIER) {
2441 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
2443 rem_anchor_token('}');
2447 declaration_t *const entry = allocate_declaration_zero();
2448 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2449 entry->type = enum_type;
2450 entry->symbol = token.v.symbol;
2451 entry->source_position = token.source_position;
2454 if(token.type == '=') {
2456 expression_t *value = parse_constant_expression();
2458 value = create_implicit_cast(value, enum_type);
2459 entry->init.enum_value = value;
2464 record_declaration(entry);
2466 if(token.type != ',')
2469 } while(token.type != '}');
2470 rem_anchor_token('}');
2478 static type_t *parse_enum_specifier(void)
2480 gnu_attribute_t *attributes = NULL;
2481 declaration_t *declaration;
2485 if(token.type == T_IDENTIFIER) {
2486 symbol = token.v.symbol;
2489 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2490 } else if(token.type != '{') {
2491 parse_error_expected("while parsing enum type specifier",
2492 T_IDENTIFIER, '{', 0);
2499 if(declaration == NULL) {
2500 declaration = allocate_declaration_zero();
2501 declaration->namespc = NAMESPACE_ENUM;
2502 declaration->source_position = token.source_position;
2503 declaration->symbol = symbol;
2504 declaration->parent_scope = scope;
2507 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2508 type->enumt.declaration = declaration;
2510 if(token.type == '{') {
2511 if(declaration->init.is_defined) {
2512 errorf(HERE, "multiple definitions of enum %Y", symbol);
2514 if (symbol != NULL) {
2515 environment_push(declaration);
2517 append_declaration(declaration);
2518 declaration->init.is_defined = 1;
2520 parse_enum_entries(type);
2521 parse_attributes(&attributes);
2528 * if a symbol is a typedef to another type, return true
2530 static bool is_typedef_symbol(symbol_t *symbol)
2532 const declaration_t *const declaration =
2533 get_declaration(symbol, NAMESPACE_NORMAL);
2535 declaration != NULL &&
2536 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2539 static type_t *parse_typeof(void)
2546 add_anchor_token(')');
2548 expression_t *expression = NULL;
2551 switch(token.type) {
2552 case T___extension__:
2553 /* this can be a prefix to a typename or an expression */
2554 /* we simply eat it now. */
2557 } while(token.type == T___extension__);
2561 if(is_typedef_symbol(token.v.symbol)) {
2562 type = parse_typename();
2564 expression = parse_expression();
2565 type = expression->base.type;
2570 type = parse_typename();
2574 expression = parse_expression();
2575 type = expression->base.type;
2579 rem_anchor_token(')');
2582 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2583 typeof_type->typeoft.expression = expression;
2584 typeof_type->typeoft.typeof_type = type;
2592 SPECIFIER_SIGNED = 1 << 0,
2593 SPECIFIER_UNSIGNED = 1 << 1,
2594 SPECIFIER_LONG = 1 << 2,
2595 SPECIFIER_INT = 1 << 3,
2596 SPECIFIER_DOUBLE = 1 << 4,
2597 SPECIFIER_CHAR = 1 << 5,
2598 SPECIFIER_SHORT = 1 << 6,
2599 SPECIFIER_LONG_LONG = 1 << 7,
2600 SPECIFIER_FLOAT = 1 << 8,
2601 SPECIFIER_BOOL = 1 << 9,
2602 SPECIFIER_VOID = 1 << 10,
2603 SPECIFIER_INT8 = 1 << 11,
2604 SPECIFIER_INT16 = 1 << 12,
2605 SPECIFIER_INT32 = 1 << 13,
2606 SPECIFIER_INT64 = 1 << 14,
2607 SPECIFIER_INT128 = 1 << 15,
2608 #ifdef PROVIDE_COMPLEX
2609 SPECIFIER_COMPLEX = 1 << 16,
2610 SPECIFIER_IMAGINARY = 1 << 17,
2614 static type_t *create_builtin_type(symbol_t *const symbol,
2615 type_t *const real_type)
2617 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2618 type->builtin.symbol = symbol;
2619 type->builtin.real_type = real_type;
2621 type_t *result = typehash_insert(type);
2622 if (type != result) {
2629 static type_t *get_typedef_type(symbol_t *symbol)
2631 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2632 if(declaration == NULL
2633 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2636 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2637 type->typedeft.declaration = declaration;
2643 * check for the allowed MS alignment values.
2645 static bool check_elignment_value(long long intvalue) {
2646 if(intvalue < 1 || intvalue > 8192) {
2647 errorf(HERE, "illegal alignment value");
2650 unsigned v = (unsigned)intvalue;
2651 for(unsigned i = 1; i <= 8192; i += i) {
2655 errorf(HERE, "alignment must be power of two");
2659 #define DET_MOD(name, tag) do { \
2660 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2661 *modifiers |= tag; \
2664 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2666 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2669 if(token.type == T_restrict) {
2671 DET_MOD(restrict, DM_RESTRICT);
2673 } else if(token.type != T_IDENTIFIER)
2675 symbol_t *symbol = token.v.symbol;
2676 if(symbol == sym_align) {
2679 if(token.type != T_INTEGER)
2681 if(check_elignment_value(token.v.intvalue)) {
2682 if(specifiers->alignment != 0)
2683 warningf(HERE, "align used more than once");
2684 specifiers->alignment = (unsigned char)token.v.intvalue;
2688 } else if(symbol == sym_allocate) {
2691 if(token.type != T_IDENTIFIER)
2693 (void)token.v.symbol;
2695 } else if(symbol == sym_dllimport) {
2697 DET_MOD(dllimport, DM_DLLIMPORT);
2698 } else if(symbol == sym_dllexport) {
2700 DET_MOD(dllexport, DM_DLLEXPORT);
2701 } else if(symbol == sym_thread) {
2703 DET_MOD(thread, DM_THREAD);
2704 } else if(symbol == sym_naked) {
2706 DET_MOD(naked, DM_NAKED);
2707 } else if(symbol == sym_noinline) {
2709 DET_MOD(noinline, DM_NOINLINE);
2710 } else if(symbol == sym_noreturn) {
2712 DET_MOD(noreturn, DM_NORETURN);
2713 } else if(symbol == sym_nothrow) {
2715 DET_MOD(nothrow, DM_NOTHROW);
2716 } else if(symbol == sym_novtable) {
2718 DET_MOD(novtable, DM_NOVTABLE);
2719 } else if(symbol == sym_property) {
2723 bool is_get = false;
2724 if(token.type != T_IDENTIFIER)
2726 if(token.v.symbol == sym_get) {
2728 } else if(token.v.symbol == sym_put) {
2730 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2735 if(token.type != T_IDENTIFIER)
2738 if(specifiers->get_property_sym != NULL) {
2739 errorf(HERE, "get property name already specified");
2741 specifiers->get_property_sym = token.v.symbol;
2744 if(specifiers->put_property_sym != NULL) {
2745 errorf(HERE, "put property name already specified");
2747 specifiers->put_property_sym = token.v.symbol;
2751 if(token.type == ',') {
2758 } else if(symbol == sym_selectany) {
2760 DET_MOD(selectany, DM_SELECTANY);
2761 } else if(symbol == sym_uuid) {
2764 if(token.type != T_STRING_LITERAL)
2768 } else if(symbol == sym_deprecated) {
2770 if(specifiers->deprecated != 0)
2771 warningf(HERE, "deprecated used more than once");
2772 specifiers->deprecated = 1;
2773 if(token.type == '(') {
2775 if(token.type == T_STRING_LITERAL) {
2776 specifiers->deprecated_string = token.v.string.begin;
2779 errorf(HERE, "string literal expected");
2783 } else if(symbol == sym_noalias) {
2785 DET_MOD(noalias, DM_NOALIAS);
2787 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2789 if(token.type == '(')
2793 if (token.type == ',')
2800 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2802 type_t *type = NULL;
2803 unsigned type_qualifiers = 0;
2804 unsigned type_specifiers = 0;
2807 specifiers->source_position = token.source_position;
2810 switch(token.type) {
2813 #define MATCH_STORAGE_CLASS(token, class) \
2815 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2816 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2818 specifiers->declared_storage_class = class; \
2822 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2823 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2824 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2825 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2826 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2831 add_anchor_token(')');
2832 parse_microsoft_extended_decl_modifier(specifiers);
2833 rem_anchor_token(')');
2838 switch (specifiers->declared_storage_class) {
2839 case STORAGE_CLASS_NONE:
2840 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2843 case STORAGE_CLASS_EXTERN:
2844 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2847 case STORAGE_CLASS_STATIC:
2848 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2852 errorf(HERE, "multiple storage classes in declaration specifiers");
2858 /* type qualifiers */
2859 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2861 type_qualifiers |= qualifier; \
2865 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2866 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2867 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2868 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2869 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2870 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2871 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2872 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2874 case T___extension__:
2879 /* type specifiers */
2880 #define MATCH_SPECIFIER(token, specifier, name) \
2883 if(type_specifiers & specifier) { \
2884 errorf(HERE, "multiple " name " type specifiers given"); \
2886 type_specifiers |= specifier; \
2890 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2891 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2892 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2893 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2894 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2895 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2896 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2897 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2898 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2899 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2900 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2901 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2902 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2903 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2904 #ifdef PROVIDE_COMPLEX
2905 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2906 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2908 case T__forceinline:
2909 /* only in microsoft mode */
2910 specifiers->decl_modifiers |= DM_FORCEINLINE;
2914 specifiers->is_inline = true;
2919 if(type_specifiers & SPECIFIER_LONG_LONG) {
2920 errorf(HERE, "multiple type specifiers given");
2921 } else if(type_specifiers & SPECIFIER_LONG) {
2922 type_specifiers |= SPECIFIER_LONG_LONG;
2924 type_specifiers |= SPECIFIER_LONG;
2929 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2931 type->compound.declaration = parse_compound_type_specifier(true);
2935 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2937 type->compound.declaration = parse_compound_type_specifier(false);
2941 type = parse_enum_specifier();
2944 type = parse_typeof();
2946 case T___builtin_va_list:
2947 type = duplicate_type(type_valist);
2951 case T___attribute__:
2952 parse_attributes(&specifiers->gnu_attributes);
2955 case T_IDENTIFIER: {
2956 /* only parse identifier if we haven't found a type yet */
2957 if(type != NULL || type_specifiers != 0)
2958 goto finish_specifiers;
2960 type_t *typedef_type = get_typedef_type(token.v.symbol);
2962 if(typedef_type == NULL)
2963 goto finish_specifiers;
2966 type = typedef_type;
2970 /* function specifier */
2972 goto finish_specifiers;
2979 atomic_type_kind_t atomic_type;
2981 /* match valid basic types */
2982 switch(type_specifiers) {
2983 case SPECIFIER_VOID:
2984 atomic_type = ATOMIC_TYPE_VOID;
2986 case SPECIFIER_CHAR:
2987 atomic_type = ATOMIC_TYPE_CHAR;
2989 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2990 atomic_type = ATOMIC_TYPE_SCHAR;
2992 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2993 atomic_type = ATOMIC_TYPE_UCHAR;
2995 case SPECIFIER_SHORT:
2996 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2997 case SPECIFIER_SHORT | SPECIFIER_INT:
2998 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2999 atomic_type = ATOMIC_TYPE_SHORT;
3001 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3002 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3003 atomic_type = ATOMIC_TYPE_USHORT;
3006 case SPECIFIER_SIGNED:
3007 case SPECIFIER_SIGNED | SPECIFIER_INT:
3008 atomic_type = ATOMIC_TYPE_INT;
3010 case SPECIFIER_UNSIGNED:
3011 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3012 atomic_type = ATOMIC_TYPE_UINT;
3014 case SPECIFIER_LONG:
3015 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3016 case SPECIFIER_LONG | SPECIFIER_INT:
3017 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3018 atomic_type = ATOMIC_TYPE_LONG;
3020 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3021 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3022 atomic_type = ATOMIC_TYPE_ULONG;
3024 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3025 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3026 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3027 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3029 atomic_type = ATOMIC_TYPE_LONGLONG;
3031 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3032 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3034 atomic_type = ATOMIC_TYPE_ULONGLONG;
3037 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3038 atomic_type = unsigned_int8_type_kind;
3041 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3042 atomic_type = unsigned_int16_type_kind;
3045 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3046 atomic_type = unsigned_int32_type_kind;
3049 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3050 atomic_type = unsigned_int64_type_kind;
3053 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3054 atomic_type = unsigned_int128_type_kind;
3057 case SPECIFIER_INT8:
3058 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3059 atomic_type = int8_type_kind;
3062 case SPECIFIER_INT16:
3063 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3064 atomic_type = int16_type_kind;
3067 case SPECIFIER_INT32:
3068 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3069 atomic_type = int32_type_kind;
3072 case SPECIFIER_INT64:
3073 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3074 atomic_type = int64_type_kind;
3077 case SPECIFIER_INT128:
3078 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3079 atomic_type = int128_type_kind;
3082 case SPECIFIER_FLOAT:
3083 atomic_type = ATOMIC_TYPE_FLOAT;
3085 case SPECIFIER_DOUBLE:
3086 atomic_type = ATOMIC_TYPE_DOUBLE;
3088 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3089 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3091 case SPECIFIER_BOOL:
3092 atomic_type = ATOMIC_TYPE_BOOL;
3094 #ifdef PROVIDE_COMPLEX
3095 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3096 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
3098 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3099 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
3101 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3102 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
3104 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3105 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
3107 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3108 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
3110 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3111 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
3115 /* invalid specifier combination, give an error message */
3116 if(type_specifiers == 0) {
3117 if (! strict_mode) {
3118 if (warning.implicit_int) {
3119 warningf(HERE, "no type specifiers in declaration, using 'int'");
3121 atomic_type = ATOMIC_TYPE_INT;
3124 errorf(HERE, "no type specifiers given in declaration");
3126 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3127 (type_specifiers & SPECIFIER_UNSIGNED)) {
3128 errorf(HERE, "signed and unsigned specifiers gives");
3129 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3130 errorf(HERE, "only integer types can be signed or unsigned");
3132 errorf(HERE, "multiple datatypes in declaration");
3134 atomic_type = ATOMIC_TYPE_INVALID;
3137 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3138 type->atomic.akind = atomic_type;
3141 if(type_specifiers != 0) {
3142 errorf(HERE, "multiple datatypes in declaration");
3146 type->base.qualifiers = type_qualifiers;
3147 /* FIXME: check type qualifiers here */
3149 type_t *result = typehash_insert(type);
3150 if(newtype && result != type) {
3154 specifiers->type = result;
3159 static type_qualifiers_t parse_type_qualifiers(void)
3161 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3164 switch(token.type) {
3165 /* type qualifiers */
3166 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3167 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3168 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3169 /* microsoft extended type modifiers */
3170 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3171 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3172 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3173 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3174 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3177 return type_qualifiers;
3182 static declaration_t *parse_identifier_list(void)
3184 declaration_t *declarations = NULL;
3185 declaration_t *last_declaration = NULL;
3187 declaration_t *const declaration = allocate_declaration_zero();
3188 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3189 declaration->source_position = token.source_position;
3190 declaration->symbol = token.v.symbol;
3193 if(last_declaration != NULL) {
3194 last_declaration->next = declaration;
3196 declarations = declaration;
3198 last_declaration = declaration;
3200 if(token.type != ',')
3203 } while(token.type == T_IDENTIFIER);
3205 return declarations;
3208 static void semantic_parameter(declaration_t *declaration)
3210 /* TODO: improve error messages */
3212 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3213 errorf(HERE, "typedef not allowed in parameter list");
3214 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3215 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3216 errorf(HERE, "parameter may only have none or register storage class");
3219 type_t *const orig_type = declaration->type;
3220 type_t * type = skip_typeref(orig_type);
3222 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3223 * into a pointer. § 6.7.5.3 (7) */
3224 if (is_type_array(type)) {
3225 type_t *const element_type = type->array.element_type;
3227 type = make_pointer_type(element_type, type->base.qualifiers);
3229 declaration->type = type;
3232 if(is_type_incomplete(type)) {
3233 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3234 orig_type, declaration->symbol);
3238 static declaration_t *parse_parameter(void)
3240 declaration_specifiers_t specifiers;
3241 memset(&specifiers, 0, sizeof(specifiers));
3243 parse_declaration_specifiers(&specifiers);
3245 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3247 semantic_parameter(declaration);
3252 static declaration_t *parse_parameters(function_type_t *type)
3254 if(token.type == T_IDENTIFIER) {
3255 symbol_t *symbol = token.v.symbol;
3256 if(!is_typedef_symbol(symbol)) {
3257 type->kr_style_parameters = true;
3258 return parse_identifier_list();
3262 if(token.type == ')') {
3263 type->unspecified_parameters = 1;
3266 if(token.type == T_void && look_ahead(1)->type == ')') {
3271 declaration_t *declarations = NULL;
3272 declaration_t *declaration;
3273 declaration_t *last_declaration = NULL;
3274 function_parameter_t *parameter;
3275 function_parameter_t *last_parameter = NULL;
3278 switch(token.type) {
3282 return declarations;
3285 case T___extension__:
3287 declaration = parse_parameter();
3289 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3290 memset(parameter, 0, sizeof(parameter[0]));
3291 parameter->type = declaration->type;
3293 if(last_parameter != NULL) {
3294 last_declaration->next = declaration;
3295 last_parameter->next = parameter;
3297 type->parameters = parameter;
3298 declarations = declaration;
3300 last_parameter = parameter;
3301 last_declaration = declaration;
3305 return declarations;
3307 if(token.type != ',')
3308 return declarations;
3318 } construct_type_kind_t;
3320 typedef struct construct_type_t construct_type_t;
3321 struct construct_type_t {
3322 construct_type_kind_t kind;
3323 construct_type_t *next;
3326 typedef struct parsed_pointer_t parsed_pointer_t;
3327 struct parsed_pointer_t {
3328 construct_type_t construct_type;
3329 type_qualifiers_t type_qualifiers;
3332 typedef struct construct_function_type_t construct_function_type_t;
3333 struct construct_function_type_t {
3334 construct_type_t construct_type;
3335 type_t *function_type;
3338 typedef struct parsed_array_t parsed_array_t;
3339 struct parsed_array_t {
3340 construct_type_t construct_type;
3341 type_qualifiers_t type_qualifiers;
3347 typedef struct construct_base_type_t construct_base_type_t;
3348 struct construct_base_type_t {
3349 construct_type_t construct_type;
3353 static construct_type_t *parse_pointer_declarator(void)
3357 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3358 memset(pointer, 0, sizeof(pointer[0]));
3359 pointer->construct_type.kind = CONSTRUCT_POINTER;
3360 pointer->type_qualifiers = parse_type_qualifiers();
3362 return (construct_type_t*) pointer;
3365 static construct_type_t *parse_array_declarator(void)
3368 add_anchor_token(']');
3370 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3371 memset(array, 0, sizeof(array[0]));
3372 array->construct_type.kind = CONSTRUCT_ARRAY;
3374 if(token.type == T_static) {
3375 array->is_static = true;
3379 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3380 if(type_qualifiers != 0) {
3381 if(token.type == T_static) {
3382 array->is_static = true;
3386 array->type_qualifiers = type_qualifiers;
3388 if(token.type == '*' && look_ahead(1)->type == ']') {
3389 array->is_variable = true;
3391 } else if(token.type != ']') {
3392 array->size = parse_assignment_expression();
3395 rem_anchor_token(']');
3398 return (construct_type_t*) array;
3403 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3406 add_anchor_token(')');
3409 if(declaration != NULL) {
3410 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3412 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3415 declaration_t *parameters = parse_parameters(&type->function);
3416 if(declaration != NULL) {
3417 declaration->scope.declarations = parameters;
3420 construct_function_type_t *construct_function_type =
3421 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3422 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3423 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3424 construct_function_type->function_type = type;
3426 rem_anchor_token(')');
3430 return (construct_type_t*) construct_function_type;
3433 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3434 bool may_be_abstract)
3436 /* construct a single linked list of construct_type_t's which describe
3437 * how to construct the final declarator type */
3438 construct_type_t *first = NULL;
3439 construct_type_t *last = NULL;
3440 gnu_attribute_t *attributes = NULL;
3443 while(token.type == '*') {
3444 construct_type_t *type = parse_pointer_declarator();
3455 /* TODO: find out if this is correct */
3456 parse_attributes(&attributes);
3458 construct_type_t *inner_types = NULL;
3460 switch(token.type) {
3462 if(declaration == NULL) {
3463 errorf(HERE, "no identifier expected in typename");
3465 declaration->symbol = token.v.symbol;
3466 declaration->source_position = token.source_position;
3472 add_anchor_token(')');
3473 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3474 rem_anchor_token(')');
3480 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
3481 /* avoid a loop in the outermost scope, because eat_statement doesn't
3483 if(token.type == '}' && current_function == NULL) {
3491 construct_type_t *p = last;
3494 construct_type_t *type;
3495 switch(token.type) {
3497 type = parse_function_declarator(declaration);
3500 type = parse_array_declarator();
3503 goto declarator_finished;
3506 /* insert in the middle of the list (behind p) */
3508 type->next = p->next;
3519 declarator_finished:
3520 parse_attributes(&attributes);
3522 /* append inner_types at the end of the list, we don't to set last anymore
3523 * as it's not needed anymore */
3525 assert(first == NULL);
3526 first = inner_types;
3528 last->next = inner_types;
3536 static type_t *construct_declarator_type(construct_type_t *construct_list,
3539 construct_type_t *iter = construct_list;
3540 for( ; iter != NULL; iter = iter->next) {
3541 switch(iter->kind) {
3542 case CONSTRUCT_INVALID:
3543 internal_errorf(HERE, "invalid type construction found");
3544 case CONSTRUCT_FUNCTION: {
3545 construct_function_type_t *construct_function_type
3546 = (construct_function_type_t*) iter;
3548 type_t *function_type = construct_function_type->function_type;
3550 function_type->function.return_type = type;
3552 type_t *skipped_return_type = skip_typeref(type);
3553 if (is_type_function(skipped_return_type)) {
3554 errorf(HERE, "function returning function is not allowed");
3555 type = type_error_type;
3556 } else if (is_type_array(skipped_return_type)) {
3557 errorf(HERE, "function returning array is not allowed");
3558 type = type_error_type;
3560 type = function_type;
3565 case CONSTRUCT_POINTER: {
3566 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3567 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3568 pointer_type->pointer.points_to = type;
3569 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3571 type = pointer_type;
3575 case CONSTRUCT_ARRAY: {
3576 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3577 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3579 expression_t *size_expression = parsed_array->size;
3580 if(size_expression != NULL) {
3582 = create_implicit_cast(size_expression, type_size_t);
3585 array_type->base.qualifiers = parsed_array->type_qualifiers;
3586 array_type->array.element_type = type;
3587 array_type->array.is_static = parsed_array->is_static;
3588 array_type->array.is_variable = parsed_array->is_variable;
3589 array_type->array.size_expression = size_expression;
3591 if(size_expression != NULL) {
3592 if(is_constant_expression(size_expression)) {
3593 array_type->array.size_constant = true;
3594 array_type->array.size
3595 = fold_constant(size_expression);
3597 array_type->array.is_vla = true;
3601 type_t *skipped_type = skip_typeref(type);
3602 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3603 errorf(HERE, "array of void is not allowed");
3604 type = type_error_type;
3612 type_t *hashed_type = typehash_insert(type);
3613 if(hashed_type != type) {
3614 /* the function type was constructed earlier freeing it here will
3615 * destroy other types... */
3616 if(iter->kind != CONSTRUCT_FUNCTION) {
3626 static declaration_t *parse_declarator(
3627 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3629 declaration_t *const declaration = allocate_declaration_zero();
3630 declaration->declared_storage_class = specifiers->declared_storage_class;
3631 declaration->modifiers = specifiers->decl_modifiers;
3632 declaration->deprecated = specifiers->deprecated;
3633 declaration->deprecated_string = specifiers->deprecated_string;
3634 declaration->get_property_sym = specifiers->get_property_sym;
3635 declaration->put_property_sym = specifiers->put_property_sym;
3636 declaration->is_inline = specifiers->is_inline;
3638 declaration->storage_class = specifiers->declared_storage_class;
3639 if(declaration->storage_class == STORAGE_CLASS_NONE
3640 && scope != global_scope) {
3641 declaration->storage_class = STORAGE_CLASS_AUTO;
3644 if(specifiers->alignment != 0) {
3645 /* TODO: add checks here */
3646 declaration->alignment = specifiers->alignment;
3649 construct_type_t *construct_type
3650 = parse_inner_declarator(declaration, may_be_abstract);
3651 type_t *const type = specifiers->type;
3652 declaration->type = construct_declarator_type(construct_type, type);
3654 if(construct_type != NULL) {
3655 obstack_free(&temp_obst, construct_type);
3661 static type_t *parse_abstract_declarator(type_t *base_type)
3663 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3665 type_t *result = construct_declarator_type(construct_type, base_type);
3666 if(construct_type != NULL) {
3667 obstack_free(&temp_obst, construct_type);
3673 static declaration_t *append_declaration(declaration_t* const declaration)
3675 if (last_declaration != NULL) {
3676 last_declaration->next = declaration;
3678 scope->declarations = declaration;
3680 last_declaration = declaration;
3685 * Check if the declaration of main is suspicious. main should be a
3686 * function with external linkage, returning int, taking either zero
3687 * arguments, two, or three arguments of appropriate types, ie.
3689 * int main([ int argc, char **argv [, char **env ] ]).
3691 * @param decl the declaration to check
3692 * @param type the function type of the declaration
3694 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3696 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3697 warningf(&decl->source_position,
3698 "'main' is normally a non-static function");
3700 if (skip_typeref(func_type->return_type) != type_int) {
3701 warningf(&decl->source_position,
3702 "return type of 'main' should be 'int', but is '%T'",
3703 func_type->return_type);
3705 const function_parameter_t *parm = func_type->parameters;
3707 type_t *const first_type = parm->type;
3708 if (!types_compatible(skip_typeref(first_type), type_int)) {
3709 warningf(&decl->source_position,
3710 "first argument of 'main' should be 'int', but is '%T'", first_type);
3714 type_t *const second_type = parm->type;
3715 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3716 warningf(&decl->source_position,
3717 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3721 type_t *const third_type = parm->type;
3722 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3723 warningf(&decl->source_position,
3724 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3728 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3732 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3738 * Check if a symbol is the equal to "main".
3740 static bool is_sym_main(const symbol_t *const sym)
3742 return strcmp(sym->string, "main") == 0;
3745 static declaration_t *internal_record_declaration(
3746 declaration_t *const declaration,
3747 const bool is_function_definition)
3749 const symbol_t *const symbol = declaration->symbol;
3750 const namespace_t namespc = (namespace_t)declaration->namespc;
3752 type_t *const orig_type = declaration->type;
3753 type_t *const type = skip_typeref(orig_type);
3754 if (is_type_function(type) &&
3755 type->function.unspecified_parameters &&
3756 warning.strict_prototypes) {
3757 warningf(&declaration->source_position,
3758 "function declaration '%#T' is not a prototype",
3759 orig_type, declaration->symbol);
3762 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3763 check_type_of_main(declaration, &type->function);
3766 assert(declaration->symbol != NULL);
3767 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3769 assert(declaration != previous_declaration);
3770 if (previous_declaration != NULL) {
3771 if (previous_declaration->parent_scope == scope) {
3772 /* can happen for K&R style declarations */
3773 if(previous_declaration->type == NULL) {
3774 previous_declaration->type = declaration->type;
3777 const type_t *prev_type = skip_typeref(previous_declaration->type);
3778 if (!types_compatible(type, prev_type)) {
3779 errorf(&declaration->source_position,
3780 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3781 orig_type, symbol, previous_declaration->type, symbol,
3782 &previous_declaration->source_position);
3784 unsigned old_storage_class = previous_declaration->storage_class;
3785 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3786 errorf(&declaration->source_position,
3787 "redeclaration of enum entry '%Y' (declared %P)",
3788 symbol, &previous_declaration->source_position);
3789 return previous_declaration;
3792 unsigned new_storage_class = declaration->storage_class;
3794 if(is_type_incomplete(prev_type)) {
3795 previous_declaration->type = type;
3799 /* pretend no storage class means extern for function
3800 * declarations (except if the previous declaration is neither
3801 * none nor extern) */
3802 if (is_type_function(type)) {
3803 switch (old_storage_class) {
3804 case STORAGE_CLASS_NONE:
3805 old_storage_class = STORAGE_CLASS_EXTERN;
3807 case STORAGE_CLASS_EXTERN:
3808 if (is_function_definition) {
3809 if (warning.missing_prototypes &&
3810 prev_type->function.unspecified_parameters &&
3811 !is_sym_main(symbol)) {
3812 warningf(&declaration->source_position,
3813 "no previous prototype for '%#T'",
3816 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3817 new_storage_class = STORAGE_CLASS_EXTERN;
3825 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3826 new_storage_class == STORAGE_CLASS_EXTERN) {
3827 warn_redundant_declaration:
3828 if (warning.redundant_decls) {
3829 warningf(&declaration->source_position,
3830 "redundant declaration for '%Y' (declared %P)",
3831 symbol, &previous_declaration->source_position);
3833 } else if (current_function == NULL) {
3834 if (old_storage_class != STORAGE_CLASS_STATIC &&
3835 new_storage_class == STORAGE_CLASS_STATIC) {
3836 errorf(&declaration->source_position,
3837 "static declaration of '%Y' follows non-static declaration (declared %P)",
3838 symbol, &previous_declaration->source_position);
3840 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3841 goto warn_redundant_declaration;
3843 if (new_storage_class == STORAGE_CLASS_NONE) {
3844 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3845 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3849 if (old_storage_class == new_storage_class) {
3850 errorf(&declaration->source_position,
3851 "redeclaration of '%Y' (declared %P)",
3852 symbol, &previous_declaration->source_position);
3854 errorf(&declaration->source_position,
3855 "redeclaration of '%Y' with different linkage (declared %P)",
3856 symbol, &previous_declaration->source_position);
3860 return previous_declaration;
3862 } else if (is_function_definition) {
3863 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3864 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3865 warningf(&declaration->source_position,
3866 "no previous prototype for '%#T'", orig_type, symbol);
3867 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3868 warningf(&declaration->source_position,
3869 "no previous declaration for '%#T'", orig_type,
3873 } else if (warning.missing_declarations &&
3874 scope == global_scope &&
3875 !is_type_function(type) && (
3876 declaration->storage_class == STORAGE_CLASS_NONE ||
3877 declaration->storage_class == STORAGE_CLASS_THREAD
3879 warningf(&declaration->source_position,
3880 "no previous declaration for '%#T'", orig_type, symbol);
3883 assert(declaration->parent_scope == NULL);
3884 assert(scope != NULL);
3886 declaration->parent_scope = scope;
3888 environment_push(declaration);
3889 return append_declaration(declaration);
3892 static declaration_t *record_declaration(declaration_t *declaration)
3894 return internal_record_declaration(declaration, false);
3897 static declaration_t *record_function_definition(declaration_t *declaration)
3899 return internal_record_declaration(declaration, true);
3902 static void parser_error_multiple_definition(declaration_t *declaration,
3903 const source_position_t *source_position)
3905 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3906 declaration->symbol, &declaration->source_position);
3909 static bool is_declaration_specifier(const token_t *token,
3910 bool only_type_specifiers)
3912 switch(token->type) {
3916 return is_typedef_symbol(token->v.symbol);
3918 case T___extension__:
3921 return !only_type_specifiers;
3928 static void parse_init_declarator_rest(declaration_t *declaration)
3932 type_t *orig_type = declaration->type;
3933 type_t *type = skip_typeref(orig_type);
3935 if(declaration->init.initializer != NULL) {
3936 parser_error_multiple_definition(declaration, HERE);
3939 bool must_be_constant = false;
3940 if(declaration->storage_class == STORAGE_CLASS_STATIC
3941 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3942 || declaration->parent_scope == global_scope) {
3943 must_be_constant = true;
3946 parse_initializer_env_t env;
3947 env.type = orig_type;
3948 env.must_be_constant = must_be_constant;
3949 env.declaration = declaration;
3951 initializer_t *initializer = parse_initializer(&env);
3953 if(env.type != orig_type) {
3954 orig_type = env.type;
3955 type = skip_typeref(orig_type);
3956 declaration->type = env.type;
3959 if(is_type_function(type)) {
3960 errorf(&declaration->source_position,
3961 "initializers not allowed for function types at declator '%Y' (type '%T')",
3962 declaration->symbol, orig_type);
3964 declaration->init.initializer = initializer;
3968 /* parse rest of a declaration without any declarator */
3969 static void parse_anonymous_declaration_rest(
3970 const declaration_specifiers_t *specifiers,
3971 parsed_declaration_func finished_declaration)
3975 declaration_t *const declaration = allocate_declaration_zero();
3976 declaration->type = specifiers->type;
3977 declaration->declared_storage_class = specifiers->declared_storage_class;
3978 declaration->source_position = specifiers->source_position;
3979 declaration->modifiers = specifiers->decl_modifiers;
3981 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3982 warningf(&declaration->source_position,
3983 "useless storage class in empty declaration");
3985 declaration->storage_class = STORAGE_CLASS_NONE;
3987 type_t *type = declaration->type;
3988 switch (type->kind) {
3989 case TYPE_COMPOUND_STRUCT:
3990 case TYPE_COMPOUND_UNION: {
3991 if (type->compound.declaration->symbol == NULL) {
3992 warningf(&declaration->source_position,
3993 "unnamed struct/union that defines no instances");
4002 warningf(&declaration->source_position, "empty declaration");
4006 finished_declaration(declaration);
4009 static void parse_declaration_rest(declaration_t *ndeclaration,
4010 const declaration_specifiers_t *specifiers,
4011 parsed_declaration_func finished_declaration)
4013 add_anchor_token(';');
4014 add_anchor_token('=');
4015 add_anchor_token(',');
4017 declaration_t *declaration = finished_declaration(ndeclaration);
4019 type_t *orig_type = declaration->type;
4020 type_t *type = skip_typeref(orig_type);
4022 if (type->kind != TYPE_FUNCTION &&
4023 declaration->is_inline &&
4024 is_type_valid(type)) {
4025 warningf(&declaration->source_position,
4026 "variable '%Y' declared 'inline'\n", declaration->symbol);
4029 if(token.type == '=') {
4030 parse_init_declarator_rest(declaration);
4033 if(token.type != ',')
4037 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4042 rem_anchor_token(';');
4043 rem_anchor_token('=');
4044 rem_anchor_token(',');
4047 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4049 symbol_t *symbol = declaration->symbol;
4050 if(symbol == NULL) {
4051 errorf(HERE, "anonymous declaration not valid as function parameter");
4054 namespace_t namespc = (namespace_t) declaration->namespc;
4055 if(namespc != NAMESPACE_NORMAL) {
4056 return record_declaration(declaration);
4059 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4060 if(previous_declaration == NULL ||
4061 previous_declaration->parent_scope != scope) {
4062 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4067 if(previous_declaration->type == NULL) {
4068 previous_declaration->type = declaration->type;
4069 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4070 previous_declaration->storage_class = declaration->storage_class;
4071 previous_declaration->parent_scope = scope;
4072 return previous_declaration;
4074 return record_declaration(declaration);
4078 static void parse_declaration(parsed_declaration_func finished_declaration)
4080 declaration_specifiers_t specifiers;
4081 memset(&specifiers, 0, sizeof(specifiers));
4082 parse_declaration_specifiers(&specifiers);
4084 if(token.type == ';') {
4085 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4087 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4088 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4092 static void parse_kr_declaration_list(declaration_t *declaration)
4094 type_t *type = skip_typeref(declaration->type);
4095 if(!is_type_function(type))
4098 if(!type->function.kr_style_parameters)
4101 /* push function parameters */
4102 int top = environment_top();
4103 scope_t *last_scope = scope;
4104 set_scope(&declaration->scope);
4106 declaration_t *parameter = declaration->scope.declarations;
4107 for( ; parameter != NULL; parameter = parameter->next) {
4108 assert(parameter->parent_scope == NULL);
4109 parameter->parent_scope = scope;
4110 environment_push(parameter);
4113 /* parse declaration list */
4114 while(is_declaration_specifier(&token, false)) {
4115 parse_declaration(finished_kr_declaration);
4118 /* pop function parameters */
4119 assert(scope == &declaration->scope);
4120 set_scope(last_scope);
4121 environment_pop_to(top);
4123 /* update function type */
4124 type_t *new_type = duplicate_type(type);
4125 new_type->function.kr_style_parameters = false;
4127 function_parameter_t *parameters = NULL;
4128 function_parameter_t *last_parameter = NULL;
4130 declaration_t *parameter_declaration = declaration->scope.declarations;
4131 for( ; parameter_declaration != NULL;
4132 parameter_declaration = parameter_declaration->next) {
4133 type_t *parameter_type = parameter_declaration->type;
4134 if(parameter_type == NULL) {
4136 errorf(HERE, "no type specified for function parameter '%Y'",
4137 parameter_declaration->symbol);
4139 if (warning.implicit_int) {
4140 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4141 parameter_declaration->symbol);
4143 parameter_type = type_int;
4144 parameter_declaration->type = parameter_type;
4148 semantic_parameter(parameter_declaration);
4149 parameter_type = parameter_declaration->type;
4151 function_parameter_t *function_parameter
4152 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4153 memset(function_parameter, 0, sizeof(function_parameter[0]));
4155 function_parameter->type = parameter_type;
4156 if(last_parameter != NULL) {
4157 last_parameter->next = function_parameter;
4159 parameters = function_parameter;
4161 last_parameter = function_parameter;
4163 new_type->function.parameters = parameters;
4165 type = typehash_insert(new_type);
4166 if(type != new_type) {
4167 obstack_free(type_obst, new_type);
4170 declaration->type = type;
4173 static bool first_err = true;
4176 * When called with first_err set, prints the name of the current function,
4179 static void print_in_function(void) {
4182 diagnosticf("%s: In function '%Y':\n",
4183 current_function->source_position.input_name,
4184 current_function->symbol);
4189 * Check if all labels are defined in the current function.
4190 * Check if all labels are used in the current function.
4192 static void check_labels(void)
4194 for (const goto_statement_t *goto_statement = goto_first;
4195 goto_statement != NULL;
4196 goto_statement = goto_statement->next) {
4197 declaration_t *label = goto_statement->label;
4200 if (label->source_position.input_name == NULL) {
4201 print_in_function();
4202 errorf(&goto_statement->base.source_position,
4203 "label '%Y' used but not defined", label->symbol);
4206 goto_first = goto_last = NULL;
4208 if (warning.unused_label) {
4209 for (const label_statement_t *label_statement = label_first;
4210 label_statement != NULL;
4211 label_statement = label_statement->next) {
4212 const declaration_t *label = label_statement->label;
4214 if (! label->used) {
4215 print_in_function();
4216 warningf(&label_statement->base.source_position,
4217 "label '%Y' defined but not used", label->symbol);
4221 label_first = label_last = NULL;
4225 * Check declarations of current_function for unused entities.
4227 static void check_declarations(void)
4229 if (warning.unused_parameter) {
4230 const scope_t *scope = ¤t_function->scope;
4232 const declaration_t *parameter = scope->declarations;
4233 for (; parameter != NULL; parameter = parameter->next) {
4234 if (! parameter->used) {
4235 print_in_function();
4236 warningf(¶meter->source_position,
4237 "unused parameter '%Y'", parameter->symbol);
4241 if (warning.unused_variable) {
4245 static void parse_external_declaration(void)
4247 /* function-definitions and declarations both start with declaration
4249 declaration_specifiers_t specifiers;
4250 memset(&specifiers, 0, sizeof(specifiers));
4252 add_anchor_token(';');
4253 parse_declaration_specifiers(&specifiers);
4254 rem_anchor_token(';');
4256 /* must be a declaration */
4257 if(token.type == ';') {
4258 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4262 add_anchor_token(',');
4263 add_anchor_token('=');
4264 rem_anchor_token(';');
4266 /* declarator is common to both function-definitions and declarations */
4267 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4269 rem_anchor_token(',');
4270 rem_anchor_token('=');
4271 rem_anchor_token(';');
4273 /* must be a declaration */
4274 if(token.type == ',' || token.type == '=' || token.type == ';') {
4275 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4279 /* must be a function definition */
4280 parse_kr_declaration_list(ndeclaration);
4282 if(token.type != '{') {
4283 parse_error_expected("while parsing function definition", '{', 0);
4284 eat_until_matching_token(';');
4288 type_t *type = ndeclaration->type;
4290 /* note that we don't skip typerefs: the standard doesn't allow them here
4291 * (so we can't use is_type_function here) */
4292 if(type->kind != TYPE_FUNCTION) {
4293 if (is_type_valid(type)) {
4294 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4295 type, ndeclaration->symbol);
4301 /* § 6.7.5.3 (14) a function definition with () means no
4302 * parameters (and not unspecified parameters) */
4303 if(type->function.unspecified_parameters) {
4304 type_t *duplicate = duplicate_type(type);
4305 duplicate->function.unspecified_parameters = false;
4307 type = typehash_insert(duplicate);
4308 if(type != duplicate) {
4309 obstack_free(type_obst, duplicate);
4311 ndeclaration->type = type;
4314 declaration_t *const declaration = record_function_definition(ndeclaration);
4315 if(ndeclaration != declaration) {
4316 declaration->scope = ndeclaration->scope;
4318 type = skip_typeref(declaration->type);
4320 /* push function parameters and switch scope */
4321 int top = environment_top();
4322 scope_t *last_scope = scope;
4323 set_scope(&declaration->scope);
4325 declaration_t *parameter = declaration->scope.declarations;
4326 for( ; parameter != NULL; parameter = parameter->next) {
4327 if(parameter->parent_scope == &ndeclaration->scope) {
4328 parameter->parent_scope = scope;
4330 assert(parameter->parent_scope == NULL
4331 || parameter->parent_scope == scope);
4332 parameter->parent_scope = scope;
4333 environment_push(parameter);
4336 if(declaration->init.statement != NULL) {
4337 parser_error_multiple_definition(declaration, HERE);
4339 goto end_of_parse_external_declaration;
4341 /* parse function body */
4342 int label_stack_top = label_top();
4343 declaration_t *old_current_function = current_function;
4344 current_function = declaration;
4346 declaration->init.statement = parse_compound_statement();
4349 check_declarations();
4351 assert(current_function == declaration);
4352 current_function = old_current_function;
4353 label_pop_to(label_stack_top);
4356 end_of_parse_external_declaration:
4357 assert(scope == &declaration->scope);
4358 set_scope(last_scope);
4359 environment_pop_to(top);
4362 static type_t *make_bitfield_type(type_t *base, expression_t *size,
4363 source_position_t *source_position)
4365 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4366 type->bitfield.base = base;
4367 type->bitfield.size = size;
4372 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4375 declaration_t *iter = compound_declaration->scope.declarations;
4376 for( ; iter != NULL; iter = iter->next) {
4377 if(iter->namespc != NAMESPACE_NORMAL)
4380 if(iter->symbol == NULL) {
4381 type_t *type = skip_typeref(iter->type);
4382 if(is_type_compound(type)) {
4383 declaration_t *result
4384 = find_compound_entry(type->compound.declaration, symbol);
4391 if(iter->symbol == symbol) {
4399 static void parse_compound_declarators(declaration_t *struct_declaration,
4400 const declaration_specifiers_t *specifiers)
4402 declaration_t *last_declaration = struct_declaration->scope.declarations;
4403 if(last_declaration != NULL) {
4404 while(last_declaration->next != NULL) {
4405 last_declaration = last_declaration->next;
4410 declaration_t *declaration;
4412 if(token.type == ':') {
4413 source_position_t source_position = *HERE;
4416 type_t *base_type = specifiers->type;
4417 expression_t *size = parse_constant_expression();
4419 if(!is_type_integer(skip_typeref(base_type))) {
4420 errorf(HERE, "bitfield base type '%T' is not an integer type",
4424 type_t *type = make_bitfield_type(base_type, size, &source_position);
4426 declaration = allocate_declaration_zero();
4427 declaration->namespc = NAMESPACE_NORMAL;
4428 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4429 declaration->storage_class = STORAGE_CLASS_NONE;
4430 declaration->source_position = source_position;
4431 declaration->modifiers = specifiers->decl_modifiers;
4432 declaration->type = type;
4434 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4436 type_t *orig_type = declaration->type;
4437 type_t *type = skip_typeref(orig_type);
4439 if(token.type == ':') {
4440 source_position_t source_position = *HERE;
4442 expression_t *size = parse_constant_expression();
4444 if(!is_type_integer(type)) {
4445 errorf(HERE, "bitfield base type '%T' is not an "
4446 "integer type", orig_type);
4449 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4450 declaration->type = bitfield_type;
4452 /* TODO we ignore arrays for now... what is missing is a check
4453 * that they're at the end of the struct */
4454 if(is_type_incomplete(type) && !is_type_array(type)) {
4456 "compound member '%Y' has incomplete type '%T'",
4457 declaration->symbol, orig_type);
4458 } else if(is_type_function(type)) {
4459 errorf(HERE, "compound member '%Y' must not have function "
4460 "type '%T'", declaration->symbol, orig_type);
4465 /* make sure we don't define a symbol multiple times */
4466 symbol_t *symbol = declaration->symbol;
4467 if(symbol != NULL) {
4468 declaration_t *prev_decl
4469 = find_compound_entry(struct_declaration, symbol);
4471 if(prev_decl != NULL) {
4472 assert(prev_decl->symbol == symbol);
4473 errorf(&declaration->source_position,
4474 "multiple declarations of symbol '%Y' (declared %P)",
4475 symbol, &prev_decl->source_position);
4479 /* append declaration */
4480 if(last_declaration != NULL) {
4481 last_declaration->next = declaration;
4483 struct_declaration->scope.declarations = declaration;
4485 last_declaration = declaration;
4487 if(token.type != ',')
4497 static void parse_compound_type_entries(declaration_t *compound_declaration)
4500 add_anchor_token('}');
4502 while(token.type != '}' && token.type != T_EOF) {
4503 declaration_specifiers_t specifiers;
4504 memset(&specifiers, 0, sizeof(specifiers));
4505 parse_declaration_specifiers(&specifiers);
4507 parse_compound_declarators(compound_declaration, &specifiers);
4509 rem_anchor_token('}');
4511 if(token.type == T_EOF) {
4512 errorf(HERE, "EOF while parsing struct");
4517 static type_t *parse_typename(void)
4519 declaration_specifiers_t specifiers;
4520 memset(&specifiers, 0, sizeof(specifiers));
4521 parse_declaration_specifiers(&specifiers);
4522 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4523 /* TODO: improve error message, user does probably not know what a
4524 * storage class is...
4526 errorf(HERE, "typename may not have a storage class");
4529 type_t *result = parse_abstract_declarator(specifiers.type);
4537 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4538 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4539 expression_t *left);
4541 typedef struct expression_parser_function_t expression_parser_function_t;
4542 struct expression_parser_function_t {
4543 unsigned precedence;
4544 parse_expression_function parser;
4545 unsigned infix_precedence;
4546 parse_expression_infix_function infix_parser;
4549 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4552 * Prints an error message if an expression was expected but not read
4554 static expression_t *expected_expression_error(void)
4556 /* skip the error message if the error token was read */
4557 if (token.type != T_ERROR) {
4558 errorf(HERE, "expected expression, got token '%K'", &token);
4562 return create_invalid_expression();
4566 * Parse a string constant.
4568 static expression_t *parse_string_const(void)
4571 if (token.type == T_STRING_LITERAL) {
4572 string_t res = token.v.string;
4574 while (token.type == T_STRING_LITERAL) {
4575 res = concat_strings(&res, &token.v.string);
4578 if (token.type != T_WIDE_STRING_LITERAL) {
4579 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4580 /* note: that we use type_char_ptr here, which is already the
4581 * automatic converted type. revert_automatic_type_conversion
4582 * will construct the array type */
4583 cnst->base.type = type_char_ptr;
4584 cnst->string.value = res;
4588 wres = concat_string_wide_string(&res, &token.v.wide_string);
4590 wres = token.v.wide_string;
4595 switch (token.type) {
4596 case T_WIDE_STRING_LITERAL:
4597 wres = concat_wide_strings(&wres, &token.v.wide_string);
4600 case T_STRING_LITERAL:
4601 wres = concat_wide_string_string(&wres, &token.v.string);
4605 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4606 cnst->base.type = type_wchar_t_ptr;
4607 cnst->wide_string.value = wres;
4616 * Parse an integer constant.
4618 static expression_t *parse_int_const(void)
4620 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4621 cnst->base.source_position = *HERE;
4622 cnst->base.type = token.datatype;
4623 cnst->conste.v.int_value = token.v.intvalue;
4631 * Parse a character constant.
4633 static expression_t *parse_character_constant(void)
4635 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4637 cnst->base.source_position = *HERE;
4638 cnst->base.type = token.datatype;
4639 cnst->conste.v.character = token.v.string;
4641 if (cnst->conste.v.character.size != 1) {
4642 if (warning.multichar && (c_mode & _GNUC)) {
4644 warningf(HERE, "multi-character character constant");
4646 errorf(HERE, "more than 1 characters in character constant");
4655 * Parse a wide character constant.
4657 static expression_t *parse_wide_character_constant(void)
4659 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4661 cnst->base.source_position = *HERE;
4662 cnst->base.type = token.datatype;
4663 cnst->conste.v.wide_character = token.v.wide_string;
4665 if (cnst->conste.v.wide_character.size != 1) {
4666 if (warning.multichar && (c_mode & _GNUC)) {
4668 warningf(HERE, "multi-character character constant");
4670 errorf(HERE, "more than 1 characters in character constant");
4679 * Parse a float constant.
4681 static expression_t *parse_float_const(void)
4683 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4684 cnst->base.type = token.datatype;
4685 cnst->conste.v.float_value = token.v.floatvalue;
4692 static declaration_t *create_implicit_function(symbol_t *symbol,
4693 const source_position_t *source_position)
4695 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4696 ntype->function.return_type = type_int;
4697 ntype->function.unspecified_parameters = true;
4699 type_t *type = typehash_insert(ntype);
4704 declaration_t *const declaration = allocate_declaration_zero();
4705 declaration->storage_class = STORAGE_CLASS_EXTERN;
4706 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4707 declaration->type = type;
4708 declaration->symbol = symbol;
4709 declaration->source_position = *source_position;
4710 declaration->parent_scope = global_scope;
4712 scope_t *old_scope = scope;
4713 set_scope(global_scope);
4715 environment_push(declaration);
4716 /* prepends the declaration to the global declarations list */
4717 declaration->next = scope->declarations;
4718 scope->declarations = declaration;
4720 assert(scope == global_scope);
4721 set_scope(old_scope);
4727 * Creates a return_type (func)(argument_type) function type if not
4730 * @param return_type the return type
4731 * @param argument_type the argument type
4733 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4735 function_parameter_t *parameter
4736 = obstack_alloc(type_obst, sizeof(parameter[0]));
4737 memset(parameter, 0, sizeof(parameter[0]));
4738 parameter->type = argument_type;
4740 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4741 type->function.return_type = return_type;
4742 type->function.parameters = parameter;
4744 type_t *result = typehash_insert(type);
4745 if(result != type) {
4753 * Creates a function type for some function like builtins.
4755 * @param symbol the symbol describing the builtin
4757 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4759 switch(symbol->ID) {
4760 case T___builtin_alloca:
4761 return make_function_1_type(type_void_ptr, type_size_t);
4762 case T___builtin_nan:
4763 return make_function_1_type(type_double, type_char_ptr);
4764 case T___builtin_nanf:
4765 return make_function_1_type(type_float, type_char_ptr);
4766 case T___builtin_nand:
4767 return make_function_1_type(type_long_double, type_char_ptr);
4768 case T___builtin_va_end:
4769 return make_function_1_type(type_void, type_valist);
4771 internal_errorf(HERE, "not implemented builtin symbol found");
4776 * Performs automatic type cast as described in § 6.3.2.1.
4778 * @param orig_type the original type
4780 static type_t *automatic_type_conversion(type_t *orig_type)
4782 type_t *type = skip_typeref(orig_type);
4783 if(is_type_array(type)) {
4784 array_type_t *array_type = &type->array;
4785 type_t *element_type = array_type->element_type;
4786 unsigned qualifiers = array_type->type.qualifiers;
4788 return make_pointer_type(element_type, qualifiers);
4791 if(is_type_function(type)) {
4792 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4799 * reverts the automatic casts of array to pointer types and function
4800 * to function-pointer types as defined § 6.3.2.1
4802 type_t *revert_automatic_type_conversion(const expression_t *expression)
4804 switch (expression->kind) {
4805 case EXPR_REFERENCE: return expression->reference.declaration->type;
4806 case EXPR_SELECT: return expression->select.compound_entry->type;
4808 case EXPR_UNARY_DEREFERENCE: {
4809 const expression_t *const value = expression->unary.value;
4810 type_t *const type = skip_typeref(value->base.type);
4811 assert(is_type_pointer(type));
4812 return type->pointer.points_to;
4815 case EXPR_BUILTIN_SYMBOL:
4816 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4818 case EXPR_ARRAY_ACCESS: {
4819 const expression_t *array_ref = expression->array_access.array_ref;
4820 type_t *type_left = skip_typeref(array_ref->base.type);
4821 if (!is_type_valid(type_left))
4823 assert(is_type_pointer(type_left));
4824 return type_left->pointer.points_to;
4827 case EXPR_STRING_LITERAL: {
4828 size_t size = expression->string.value.size;
4829 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4832 case EXPR_WIDE_STRING_LITERAL: {
4833 size_t size = expression->wide_string.value.size;
4834 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4837 case EXPR_COMPOUND_LITERAL:
4838 return expression->compound_literal.type;
4843 return expression->base.type;
4846 static expression_t *parse_reference(void)
4848 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4850 reference_expression_t *ref = &expression->reference;
4851 ref->symbol = token.v.symbol;
4853 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4855 source_position_t source_position = token.source_position;
4858 if(declaration == NULL) {
4859 if (! strict_mode && token.type == '(') {
4860 /* an implicitly defined function */
4861 if (warning.implicit_function_declaration) {
4862 warningf(HERE, "implicit declaration of function '%Y'",
4866 declaration = create_implicit_function(ref->symbol,
4869 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4870 return create_invalid_expression();
4874 type_t *type = declaration->type;
4876 /* we always do the auto-type conversions; the & and sizeof parser contains
4877 * code to revert this! */
4878 type = automatic_type_conversion(type);
4880 ref->declaration = declaration;
4881 ref->base.type = type;
4883 /* this declaration is used */
4884 declaration->used = true;
4886 /* check for deprecated functions */
4887 if(declaration->deprecated != 0) {
4888 const char *prefix = "";
4889 if (is_type_function(declaration->type))
4890 prefix = "function ";
4892 if (declaration->deprecated_string != NULL) {
4893 warningf(&source_position,
4894 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4895 declaration->deprecated_string);
4897 warningf(&source_position,
4898 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4905 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4909 /* TODO check if explicit cast is allowed and issue warnings/errors */
4912 static expression_t *parse_compound_literal(type_t *type)
4914 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4916 parse_initializer_env_t env;
4918 env.declaration = NULL;
4919 env.must_be_constant = false;
4920 initializer_t *initializer = parse_initializer(&env);
4923 expression->compound_literal.initializer = initializer;
4924 expression->compound_literal.type = type;
4925 expression->base.type = automatic_type_conversion(type);
4931 * Parse a cast expression.
4933 static expression_t *parse_cast(void)
4935 source_position_t source_position = token.source_position;
4937 type_t *type = parse_typename();
4939 /* matching add_anchor_token() is at call site */
4940 rem_anchor_token(')');
4943 if(token.type == '{') {
4944 return parse_compound_literal(type);
4947 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4948 cast->base.source_position = source_position;
4950 expression_t *value = parse_sub_expression(20);
4952 check_cast_allowed(value, type);
4954 cast->base.type = type;
4955 cast->unary.value = value;
4959 return create_invalid_expression();
4963 * Parse a statement expression.
4965 static expression_t *parse_statement_expression(void)
4967 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4969 statement_t *statement = parse_compound_statement();
4970 expression->statement.statement = statement;
4971 expression->base.source_position = statement->base.source_position;
4973 /* find last statement and use its type */
4974 type_t *type = type_void;
4975 const statement_t *stmt = statement->compound.statements;
4977 while (stmt->base.next != NULL)
4978 stmt = stmt->base.next;
4980 if (stmt->kind == STATEMENT_EXPRESSION) {
4981 type = stmt->expression.expression->base.type;
4984 warningf(&expression->base.source_position, "empty statement expression ({})");
4986 expression->base.type = type;
4992 return create_invalid_expression();
4996 * Parse a braced expression.
4998 static expression_t *parse_brace_expression(void)
5001 add_anchor_token(')');
5003 switch(token.type) {
5005 /* gcc extension: a statement expression */
5006 return parse_statement_expression();
5010 return parse_cast();
5012 if(is_typedef_symbol(token.v.symbol)) {
5013 return parse_cast();
5017 expression_t *result = parse_expression();
5018 rem_anchor_token(')');
5023 return create_invalid_expression();
5026 static expression_t *parse_function_keyword(void)
5031 if (current_function == NULL) {
5032 errorf(HERE, "'__func__' used outside of a function");
5035 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5036 expression->base.type = type_char_ptr;
5037 expression->funcname.kind = FUNCNAME_FUNCTION;
5042 static expression_t *parse_pretty_function_keyword(void)
5044 eat(T___PRETTY_FUNCTION__);
5046 if (current_function == NULL) {
5047 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5050 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5051 expression->base.type = type_char_ptr;
5052 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5057 static expression_t *parse_funcsig_keyword(void)
5061 if (current_function == NULL) {
5062 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5065 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5066 expression->base.type = type_char_ptr;
5067 expression->funcname.kind = FUNCNAME_FUNCSIG;
5072 static expression_t *parse_funcdname_keyword(void)
5074 eat(T___FUNCDNAME__);
5076 if (current_function == NULL) {
5077 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5080 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5081 expression->base.type = type_char_ptr;
5082 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5087 static designator_t *parse_designator(void)
5089 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5090 result->source_position = *HERE;
5092 if(token.type != T_IDENTIFIER) {
5093 parse_error_expected("while parsing member designator",
5097 result->symbol = token.v.symbol;
5100 designator_t *last_designator = result;
5102 if(token.type == '.') {
5104 if(token.type != T_IDENTIFIER) {
5105 parse_error_expected("while parsing member designator",
5109 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5110 designator->source_position = *HERE;
5111 designator->symbol = token.v.symbol;
5114 last_designator->next = designator;
5115 last_designator = designator;
5118 if(token.type == '[') {
5120 add_anchor_token(']');
5121 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5122 designator->source_position = *HERE;
5123 designator->array_index = parse_expression();
5124 rem_anchor_token(']');
5126 if(designator->array_index == NULL) {
5130 last_designator->next = designator;
5131 last_designator = designator;
5143 * Parse the __builtin_offsetof() expression.
5145 static expression_t *parse_offsetof(void)
5147 eat(T___builtin_offsetof);
5149 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5150 expression->base.type = type_size_t;
5153 add_anchor_token(',');
5154 type_t *type = parse_typename();
5155 rem_anchor_token(',');
5157 add_anchor_token(')');
5158 designator_t *designator = parse_designator();
5159 rem_anchor_token(')');
5162 expression->offsetofe.type = type;
5163 expression->offsetofe.designator = designator;
5166 memset(&path, 0, sizeof(path));
5167 path.top_type = type;
5168 path.path = NEW_ARR_F(type_path_entry_t, 0);
5170 descend_into_subtype(&path);
5172 if(!walk_designator(&path, designator, true)) {
5173 return create_invalid_expression();
5176 DEL_ARR_F(path.path);
5180 return create_invalid_expression();
5184 * Parses a _builtin_va_start() expression.
5186 static expression_t *parse_va_start(void)
5188 eat(T___builtin_va_start);
5190 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5193 add_anchor_token(',');
5194 expression->va_starte.ap = parse_assignment_expression();
5195 rem_anchor_token(',');
5197 expression_t *const expr = parse_assignment_expression();
5198 if (expr->kind == EXPR_REFERENCE) {
5199 declaration_t *const decl = expr->reference.declaration;
5201 return create_invalid_expression();
5202 if (decl->parent_scope == ¤t_function->scope &&
5203 decl->next == NULL) {
5204 expression->va_starte.parameter = decl;
5209 errorf(&expr->base.source_position,
5210 "second argument of 'va_start' must be last parameter of the current function");
5212 return create_invalid_expression();
5216 * Parses a _builtin_va_arg() expression.
5218 static expression_t *parse_va_arg(void)
5220 eat(T___builtin_va_arg);
5222 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5225 expression->va_arge.ap = parse_assignment_expression();
5227 expression->base.type = parse_typename();
5232 return create_invalid_expression();
5235 static expression_t *parse_builtin_symbol(void)
5237 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5239 symbol_t *symbol = token.v.symbol;
5241 expression->builtin_symbol.symbol = symbol;
5244 type_t *type = get_builtin_symbol_type(symbol);
5245 type = automatic_type_conversion(type);
5247 expression->base.type = type;
5252 * Parses a __builtin_constant() expression.
5254 static expression_t *parse_builtin_constant(void)
5256 eat(T___builtin_constant_p);
5258 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5261 add_anchor_token(')');
5262 expression->builtin_constant.value = parse_assignment_expression();
5263 rem_anchor_token(')');
5265 expression->base.type = type_int;
5269 return create_invalid_expression();
5273 * Parses a __builtin_prefetch() expression.
5275 static expression_t *parse_builtin_prefetch(void)
5277 eat(T___builtin_prefetch);
5279 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5282 add_anchor_token(')');
5283 expression->builtin_prefetch.adr = parse_assignment_expression();
5284 if (token.type == ',') {
5286 expression->builtin_prefetch.rw = parse_assignment_expression();
5288 if (token.type == ',') {
5290 expression->builtin_prefetch.locality = parse_assignment_expression();
5292 rem_anchor_token(')');
5294 expression->base.type = type_void;
5298 return create_invalid_expression();
5302 * Parses a __builtin_is_*() compare expression.
5304 static expression_t *parse_compare_builtin(void)
5306 expression_t *expression;
5308 switch(token.type) {
5309 case T___builtin_isgreater:
5310 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5312 case T___builtin_isgreaterequal:
5313 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5315 case T___builtin_isless:
5316 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5318 case T___builtin_islessequal:
5319 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5321 case T___builtin_islessgreater:
5322 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5324 case T___builtin_isunordered:
5325 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5328 internal_errorf(HERE, "invalid compare builtin found");
5331 expression->base.source_position = *HERE;
5335 expression->binary.left = parse_assignment_expression();
5337 expression->binary.right = parse_assignment_expression();
5340 type_t *const orig_type_left = expression->binary.left->base.type;
5341 type_t *const orig_type_right = expression->binary.right->base.type;
5343 type_t *const type_left = skip_typeref(orig_type_left);
5344 type_t *const type_right = skip_typeref(orig_type_right);
5345 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5346 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5347 type_error_incompatible("invalid operands in comparison",
5348 &expression->base.source_position, orig_type_left, orig_type_right);
5351 semantic_comparison(&expression->binary);
5356 return create_invalid_expression();
5360 * Parses a __builtin_expect() expression.
5362 static expression_t *parse_builtin_expect(void)
5364 eat(T___builtin_expect);
5366 expression_t *expression
5367 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5370 expression->binary.left = parse_assignment_expression();
5372 expression->binary.right = parse_constant_expression();
5375 expression->base.type = expression->binary.left->base.type;
5379 return create_invalid_expression();
5383 * Parses a MS assume() expression.
5385 static expression_t *parse_assume(void) {
5388 expression_t *expression
5389 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5392 add_anchor_token(')');
5393 expression->unary.value = parse_assignment_expression();
5394 rem_anchor_token(')');
5397 expression->base.type = type_void;
5400 return create_invalid_expression();
5404 * Parse a microsoft __noop expression.
5406 static expression_t *parse_noop_expression(void) {
5407 source_position_t source_position = *HERE;
5410 if (token.type == '(') {
5411 /* parse arguments */
5413 add_anchor_token(')');
5414 add_anchor_token(',');
5416 if(token.type != ')') {
5418 (void)parse_assignment_expression();
5419 if(token.type != ',')
5425 rem_anchor_token(',');
5426 rem_anchor_token(')');
5429 /* the result is a (int)0 */
5430 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5431 cnst->base.source_position = source_position;
5432 cnst->base.type = type_int;
5433 cnst->conste.v.int_value = 0;
5434 cnst->conste.is_ms_noop = true;
5439 return create_invalid_expression();
5443 * Parses a primary expression.
5445 static expression_t *parse_primary_expression(void)
5447 switch (token.type) {
5448 case T_INTEGER: return parse_int_const();
5449 case T_CHARACTER_CONSTANT: return parse_character_constant();
5450 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5451 case T_FLOATINGPOINT: return parse_float_const();
5452 case T_STRING_LITERAL:
5453 case T_WIDE_STRING_LITERAL: return parse_string_const();
5454 case T_IDENTIFIER: return parse_reference();
5455 case T___FUNCTION__:
5456 case T___func__: return parse_function_keyword();
5457 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5458 case T___FUNCSIG__: return parse_funcsig_keyword();
5459 case T___FUNCDNAME__: return parse_funcdname_keyword();
5460 case T___builtin_offsetof: return parse_offsetof();
5461 case T___builtin_va_start: return parse_va_start();
5462 case T___builtin_va_arg: return parse_va_arg();
5463 case T___builtin_expect: return parse_builtin_expect();
5464 case T___builtin_alloca:
5465 case T___builtin_nan:
5466 case T___builtin_nand:
5467 case T___builtin_nanf:
5468 case T___builtin_va_end: return parse_builtin_symbol();
5469 case T___builtin_isgreater:
5470 case T___builtin_isgreaterequal:
5471 case T___builtin_isless:
5472 case T___builtin_islessequal:
5473 case T___builtin_islessgreater:
5474 case T___builtin_isunordered: return parse_compare_builtin();
5475 case T___builtin_constant_p: return parse_builtin_constant();
5476 case T___builtin_prefetch: return parse_builtin_prefetch();
5477 case T__assume: return parse_assume();
5479 case '(': return parse_brace_expression();
5480 case T___noop: return parse_noop_expression();
5483 errorf(HERE, "unexpected token %K, expected an expression", &token);
5484 return create_invalid_expression();
5488 * Check if the expression has the character type and issue a warning then.
5490 static void check_for_char_index_type(const expression_t *expression) {
5491 type_t *const type = expression->base.type;
5492 const type_t *const base_type = skip_typeref(type);
5494 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5495 warning.char_subscripts) {
5496 warningf(&expression->base.source_position,
5497 "array subscript has type '%T'", type);
5501 static expression_t *parse_array_expression(unsigned precedence,
5507 add_anchor_token(']');
5509 expression_t *inside = parse_expression();
5511 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5513 array_access_expression_t *array_access = &expression->array_access;
5515 type_t *const orig_type_left = left->base.type;
5516 type_t *const orig_type_inside = inside->base.type;
5518 type_t *const type_left = skip_typeref(orig_type_left);
5519 type_t *const type_inside = skip_typeref(orig_type_inside);
5521 type_t *return_type;
5522 if (is_type_pointer(type_left)) {
5523 return_type = type_left->pointer.points_to;
5524 array_access->array_ref = left;
5525 array_access->index = inside;
5526 check_for_char_index_type(inside);
5527 } else if (is_type_pointer(type_inside)) {
5528 return_type = type_inside->pointer.points_to;
5529 array_access->array_ref = inside;
5530 array_access->index = left;
5531 array_access->flipped = true;
5532 check_for_char_index_type(left);
5534 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5536 "array access on object with non-pointer types '%T', '%T'",
5537 orig_type_left, orig_type_inside);
5539 return_type = type_error_type;
5540 array_access->array_ref = create_invalid_expression();
5543 rem_anchor_token(']');
5544 if(token.type != ']') {
5545 parse_error_expected("Problem while parsing array access", ']', 0);
5550 return_type = automatic_type_conversion(return_type);
5551 expression->base.type = return_type;
5556 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5558 expression_t *tp_expression = allocate_expression_zero(kind);
5559 tp_expression->base.type = type_size_t;
5561 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5563 add_anchor_token(')');
5564 tp_expression->typeprop.type = parse_typename();
5565 rem_anchor_token(')');
5568 expression_t *expression = parse_sub_expression(precedence);
5569 expression->base.type = revert_automatic_type_conversion(expression);
5571 tp_expression->typeprop.type = expression->base.type;
5572 tp_expression->typeprop.tp_expression = expression;
5575 return tp_expression;
5577 return create_invalid_expression();
5580 static expression_t *parse_sizeof(unsigned precedence)
5583 return parse_typeprop(EXPR_SIZEOF, precedence);
5586 static expression_t *parse_alignof(unsigned precedence)
5589 return parse_typeprop(EXPR_SIZEOF, precedence);
5592 static expression_t *parse_select_expression(unsigned precedence,
5593 expression_t *compound)
5596 assert(token.type == '.' || token.type == T_MINUSGREATER);
5598 bool is_pointer = (token.type == T_MINUSGREATER);
5601 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5602 select->select.compound = compound;
5604 if(token.type != T_IDENTIFIER) {
5605 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
5608 symbol_t *symbol = token.v.symbol;
5609 select->select.symbol = symbol;
5612 type_t *const orig_type = compound->base.type;
5613 type_t *const type = skip_typeref(orig_type);
5615 type_t *type_left = type;
5617 if (!is_type_pointer(type)) {
5618 if (is_type_valid(type)) {
5619 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5621 return create_invalid_expression();
5623 type_left = type->pointer.points_to;
5625 type_left = skip_typeref(type_left);
5627 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5628 type_left->kind != TYPE_COMPOUND_UNION) {
5629 if (is_type_valid(type_left)) {
5630 errorf(HERE, "request for member '%Y' in something not a struct or "
5631 "union, but '%T'", symbol, type_left);
5633 return create_invalid_expression();
5636 declaration_t *const declaration = type_left->compound.declaration;
5638 if(!declaration->init.is_defined) {
5639 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5641 return create_invalid_expression();
5644 declaration_t *iter = find_compound_entry(declaration, symbol);
5646 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5647 return create_invalid_expression();
5650 /* we always do the auto-type conversions; the & and sizeof parser contains
5651 * code to revert this! */
5652 type_t *expression_type = automatic_type_conversion(iter->type);
5654 select->select.compound_entry = iter;
5655 select->base.type = expression_type;
5657 if(expression_type->kind == TYPE_BITFIELD) {
5658 expression_t *extract
5659 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5660 extract->unary.value = select;
5661 extract->base.type = expression_type->bitfield.base;
5670 * Parse a call expression, ie. expression '( ... )'.
5672 * @param expression the function address
5674 static expression_t *parse_call_expression(unsigned precedence,
5675 expression_t *expression)
5678 expression_t *result = allocate_expression_zero(EXPR_CALL);
5679 result->base.source_position = expression->base.source_position;
5681 call_expression_t *call = &result->call;
5682 call->function = expression;
5684 type_t *const orig_type = expression->base.type;
5685 type_t *const type = skip_typeref(orig_type);
5687 function_type_t *function_type = NULL;
5688 if (is_type_pointer(type)) {
5689 type_t *const to_type = skip_typeref(type->pointer.points_to);
5691 if (is_type_function(to_type)) {
5692 function_type = &to_type->function;
5693 call->base.type = function_type->return_type;
5697 if (function_type == NULL && is_type_valid(type)) {
5698 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5701 /* parse arguments */
5703 add_anchor_token(')');
5704 add_anchor_token(',');
5706 if(token.type != ')') {
5707 call_argument_t *last_argument = NULL;
5710 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5712 argument->expression = parse_assignment_expression();
5713 if(last_argument == NULL) {
5714 call->arguments = argument;
5716 last_argument->next = argument;
5718 last_argument = argument;
5720 if(token.type != ',')
5725 rem_anchor_token(',');
5726 rem_anchor_token(')');
5729 if(function_type != NULL) {
5730 function_parameter_t *parameter = function_type->parameters;
5731 call_argument_t *argument = call->arguments;
5732 for( ; parameter != NULL && argument != NULL;
5733 parameter = parameter->next, argument = argument->next) {
5734 type_t *expected_type = parameter->type;
5735 /* TODO report scope in error messages */
5736 expression_t *const arg_expr = argument->expression;
5737 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5739 &arg_expr->base.source_position);
5740 if (res_type == NULL) {
5741 /* TODO improve error message */
5742 errorf(&arg_expr->base.source_position,
5743 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5744 arg_expr, arg_expr->base.type, expected_type);
5746 argument->expression = create_implicit_cast(argument->expression, expected_type);
5749 /* too few parameters */
5750 if(parameter != NULL) {
5751 errorf(HERE, "too few arguments to function '%E'", expression);
5752 } else if(argument != NULL) {
5753 /* too many parameters */
5754 if(!function_type->variadic
5755 && !function_type->unspecified_parameters) {
5756 errorf(HERE, "too many arguments to function '%E'", expression);
5758 /* do default promotion */
5759 for( ; argument != NULL; argument = argument->next) {
5760 type_t *type = argument->expression->base.type;
5762 type = skip_typeref(type);
5763 if(is_type_integer(type)) {
5764 type = promote_integer(type);
5765 } else if(type == type_float) {
5769 argument->expression
5770 = create_implicit_cast(argument->expression, type);
5773 check_format(&result->call);
5776 check_format(&result->call);
5782 return create_invalid_expression();
5785 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5787 static bool same_compound_type(const type_t *type1, const type_t *type2)
5790 is_type_compound(type1) &&
5791 type1->kind == type2->kind &&
5792 type1->compound.declaration == type2->compound.declaration;
5796 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5798 * @param expression the conditional expression
5800 static expression_t *parse_conditional_expression(unsigned precedence,
5801 expression_t *expression)
5804 add_anchor_token(':');
5806 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5808 conditional_expression_t *conditional = &result->conditional;
5809 conditional->condition = expression;
5812 type_t *const condition_type_orig = expression->base.type;
5813 type_t *const condition_type = skip_typeref(condition_type_orig);
5814 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5815 type_error("expected a scalar type in conditional condition",
5816 &expression->base.source_position, condition_type_orig);
5819 expression_t *true_expression = parse_expression();
5820 rem_anchor_token(':');
5822 expression_t *false_expression = parse_sub_expression(precedence);
5824 type_t *const orig_true_type = true_expression->base.type;
5825 type_t *const orig_false_type = false_expression->base.type;
5826 type_t *const true_type = skip_typeref(orig_true_type);
5827 type_t *const false_type = skip_typeref(orig_false_type);
5830 type_t *result_type;
5831 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5832 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5833 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5834 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5835 warningf(&expression->base.source_position,
5836 "ISO C forbids conditional expression with only one void side");
5838 result_type = type_void;
5839 } else if (is_type_arithmetic(true_type)
5840 && is_type_arithmetic(false_type)) {
5841 result_type = semantic_arithmetic(true_type, false_type);
5843 true_expression = create_implicit_cast(true_expression, result_type);
5844 false_expression = create_implicit_cast(false_expression, result_type);
5846 conditional->true_expression = true_expression;
5847 conditional->false_expression = false_expression;
5848 conditional->base.type = result_type;
5849 } else if (same_compound_type(true_type, false_type)) {
5850 /* just take 1 of the 2 types */
5851 result_type = true_type;
5852 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5853 type_t *pointer_type;
5855 expression_t *other_expression;
5856 if (is_type_pointer(true_type)) {
5857 pointer_type = true_type;
5858 other_type = false_type;
5859 other_expression = false_expression;
5861 pointer_type = false_type;
5862 other_type = true_type;
5863 other_expression = true_expression;
5866 if(is_type_pointer(other_type)) {
5867 if(!pointers_compatible(true_type, false_type)) {
5868 warningf(&expression->base.source_position,
5869 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5871 result_type = true_type;
5872 } else if(is_null_pointer_constant(other_expression)) {
5873 result_type = pointer_type;
5874 } else if(is_type_integer(other_type)) {
5875 warningf(&expression->base.source_position,
5876 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5877 result_type = pointer_type;
5879 type_error_incompatible("while parsing conditional",
5880 &expression->base.source_position, true_type, false_type);
5881 result_type = type_error_type;
5884 /* TODO: one pointer to void*, other some pointer */
5886 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5887 type_error_incompatible("while parsing conditional",
5888 &expression->base.source_position, true_type,
5891 result_type = type_error_type;
5894 conditional->true_expression
5895 = create_implicit_cast(true_expression, result_type);
5896 conditional->false_expression
5897 = create_implicit_cast(false_expression, result_type);
5898 conditional->base.type = result_type;
5901 return create_invalid_expression();
5905 * Parse an extension expression.
5907 static expression_t *parse_extension(unsigned precedence)
5909 eat(T___extension__);
5911 /* TODO enable extensions */
5912 expression_t *expression = parse_sub_expression(precedence);
5913 /* TODO disable extensions */
5918 * Parse a __builtin_classify_type() expression.
5920 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5922 eat(T___builtin_classify_type);
5924 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5925 result->base.type = type_int;
5928 add_anchor_token(')');
5929 expression_t *expression = parse_sub_expression(precedence);
5930 rem_anchor_token(')');
5932 result->classify_type.type_expression = expression;
5936 return create_invalid_expression();
5939 static void semantic_incdec(unary_expression_t *expression)
5941 type_t *const orig_type = expression->value->base.type;
5942 type_t *const type = skip_typeref(orig_type);
5943 /* TODO !is_type_real && !is_type_pointer */
5944 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5945 if (is_type_valid(type)) {
5946 /* TODO: improve error message */
5947 errorf(HERE, "operation needs an arithmetic or pointer type");
5952 expression->base.type = orig_type;
5955 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5957 type_t *const orig_type = expression->value->base.type;
5958 type_t *const type = skip_typeref(orig_type);
5959 if(!is_type_arithmetic(type)) {
5960 if (is_type_valid(type)) {
5961 /* TODO: improve error message */
5962 errorf(HERE, "operation needs an arithmetic type");
5967 expression->base.type = orig_type;
5970 static void semantic_unexpr_scalar(unary_expression_t *expression)
5972 type_t *const orig_type = expression->value->base.type;
5973 type_t *const type = skip_typeref(orig_type);
5974 if (!is_type_scalar(type)) {
5975 if (is_type_valid(type)) {
5976 errorf(HERE, "operand of ! must be of scalar type");
5981 expression->base.type = orig_type;
5984 static void semantic_unexpr_integer(unary_expression_t *expression)
5986 type_t *const orig_type = expression->value->base.type;
5987 type_t *const type = skip_typeref(orig_type);
5988 if (!is_type_integer(type)) {
5989 if (is_type_valid(type)) {
5990 errorf(HERE, "operand of ~ must be of integer type");
5995 expression->base.type = orig_type;
5998 static void semantic_dereference(unary_expression_t *expression)
6000 type_t *const orig_type = expression->value->base.type;
6001 type_t *const type = skip_typeref(orig_type);
6002 if(!is_type_pointer(type)) {
6003 if (is_type_valid(type)) {
6004 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6009 type_t *result_type = type->pointer.points_to;
6010 result_type = automatic_type_conversion(result_type);
6011 expression->base.type = result_type;
6015 * Check the semantic of the address taken expression.
6017 static void semantic_take_addr(unary_expression_t *expression)
6019 expression_t *value = expression->value;
6020 value->base.type = revert_automatic_type_conversion(value);
6022 type_t *orig_type = value->base.type;
6023 if(!is_type_valid(orig_type))
6026 if(value->kind == EXPR_REFERENCE) {
6027 declaration_t *const declaration = value->reference.declaration;
6028 if(declaration != NULL) {
6029 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6030 errorf(&expression->base.source_position,
6031 "address of register variable '%Y' requested",
6032 declaration->symbol);
6034 declaration->address_taken = 1;
6038 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6041 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6042 static expression_t *parse_##unexpression_type(unsigned precedence) \
6046 expression_t *unary_expression \
6047 = allocate_expression_zero(unexpression_type); \
6048 unary_expression->base.source_position = *HERE; \
6049 unary_expression->unary.value = parse_sub_expression(precedence); \
6051 sfunc(&unary_expression->unary); \
6053 return unary_expression; \
6056 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6057 semantic_unexpr_arithmetic)
6058 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6059 semantic_unexpr_arithmetic)
6060 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6061 semantic_unexpr_scalar)
6062 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6063 semantic_dereference)
6064 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6066 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6067 semantic_unexpr_integer)
6068 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6070 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6073 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6075 static expression_t *parse_##unexpression_type(unsigned precedence, \
6076 expression_t *left) \
6078 (void) precedence; \
6081 expression_t *unary_expression \
6082 = allocate_expression_zero(unexpression_type); \
6083 unary_expression->unary.value = left; \
6085 sfunc(&unary_expression->unary); \
6087 return unary_expression; \
6090 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6091 EXPR_UNARY_POSTFIX_INCREMENT,
6093 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6094 EXPR_UNARY_POSTFIX_DECREMENT,
6097 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6099 /* TODO: handle complex + imaginary types */
6101 /* § 6.3.1.8 Usual arithmetic conversions */
6102 if(type_left == type_long_double || type_right == type_long_double) {
6103 return type_long_double;
6104 } else if(type_left == type_double || type_right == type_double) {
6106 } else if(type_left == type_float || type_right == type_float) {
6110 type_right = promote_integer(type_right);
6111 type_left = promote_integer(type_left);
6113 if(type_left == type_right)
6116 bool signed_left = is_type_signed(type_left);
6117 bool signed_right = is_type_signed(type_right);
6118 int rank_left = get_rank(type_left);
6119 int rank_right = get_rank(type_right);
6120 if(rank_left < rank_right) {
6121 if(signed_left == signed_right || !signed_right) {
6127 if(signed_left == signed_right || !signed_left) {
6136 * Check the semantic restrictions for a binary expression.
6138 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6140 expression_t *const left = expression->left;
6141 expression_t *const right = expression->right;
6142 type_t *const orig_type_left = left->base.type;
6143 type_t *const orig_type_right = right->base.type;
6144 type_t *const type_left = skip_typeref(orig_type_left);
6145 type_t *const type_right = skip_typeref(orig_type_right);
6147 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6148 /* TODO: improve error message */
6149 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6150 errorf(HERE, "operation needs arithmetic types");
6155 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6156 expression->left = create_implicit_cast(left, arithmetic_type);
6157 expression->right = create_implicit_cast(right, arithmetic_type);
6158 expression->base.type = arithmetic_type;
6161 static void semantic_shift_op(binary_expression_t *expression)
6163 expression_t *const left = expression->left;
6164 expression_t *const right = expression->right;
6165 type_t *const orig_type_left = left->base.type;
6166 type_t *const orig_type_right = right->base.type;
6167 type_t * type_left = skip_typeref(orig_type_left);
6168 type_t * type_right = skip_typeref(orig_type_right);
6170 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6171 /* TODO: improve error message */
6172 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6173 errorf(HERE, "operation needs integer types");
6178 type_left = promote_integer(type_left);
6179 type_right = promote_integer(type_right);
6181 expression->left = create_implicit_cast(left, type_left);
6182 expression->right = create_implicit_cast(right, type_right);
6183 expression->base.type = type_left;
6186 static void semantic_add(binary_expression_t *expression)
6188 expression_t *const left = expression->left;
6189 expression_t *const right = expression->right;
6190 type_t *const orig_type_left = left->base.type;
6191 type_t *const orig_type_right = right->base.type;
6192 type_t *const type_left = skip_typeref(orig_type_left);
6193 type_t *const type_right = skip_typeref(orig_type_right);
6196 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6197 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6198 expression->left = create_implicit_cast(left, arithmetic_type);
6199 expression->right = create_implicit_cast(right, arithmetic_type);
6200 expression->base.type = arithmetic_type;
6202 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6203 expression->base.type = type_left;
6204 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6205 expression->base.type = type_right;
6206 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6207 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6211 static void semantic_sub(binary_expression_t *expression)
6213 expression_t *const left = expression->left;
6214 expression_t *const right = expression->right;
6215 type_t *const orig_type_left = left->base.type;
6216 type_t *const orig_type_right = right->base.type;
6217 type_t *const type_left = skip_typeref(orig_type_left);
6218 type_t *const type_right = skip_typeref(orig_type_right);
6221 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6222 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6223 expression->left = create_implicit_cast(left, arithmetic_type);
6224 expression->right = create_implicit_cast(right, arithmetic_type);
6225 expression->base.type = arithmetic_type;
6227 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6228 expression->base.type = type_left;
6229 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6230 if(!pointers_compatible(type_left, type_right)) {
6232 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6233 orig_type_left, orig_type_right);
6235 expression->base.type = type_ptrdiff_t;
6237 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6238 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6239 orig_type_left, orig_type_right);
6244 * Check the semantics of comparison expressions.
6246 * @param expression The expression to check.
6248 static void semantic_comparison(binary_expression_t *expression)
6250 expression_t *left = expression->left;
6251 expression_t *right = expression->right;
6252 type_t *orig_type_left = left->base.type;
6253 type_t *orig_type_right = right->base.type;
6255 type_t *type_left = skip_typeref(orig_type_left);
6256 type_t *type_right = skip_typeref(orig_type_right);
6258 /* TODO non-arithmetic types */
6259 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6260 if (warning.sign_compare &&
6261 (expression->base.kind != EXPR_BINARY_EQUAL &&
6262 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6263 (is_type_signed(type_left) != is_type_signed(type_right))) {
6264 warningf(&expression->base.source_position,
6265 "comparison between signed and unsigned");
6267 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6268 expression->left = create_implicit_cast(left, arithmetic_type);
6269 expression->right = create_implicit_cast(right, arithmetic_type);
6270 expression->base.type = arithmetic_type;
6271 if (warning.float_equal &&
6272 (expression->base.kind == EXPR_BINARY_EQUAL ||
6273 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6274 is_type_float(arithmetic_type)) {
6275 warningf(&expression->base.source_position,
6276 "comparing floating point with == or != is unsafe");
6278 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6279 /* TODO check compatibility */
6280 } else if (is_type_pointer(type_left)) {
6281 expression->right = create_implicit_cast(right, type_left);
6282 } else if (is_type_pointer(type_right)) {
6283 expression->left = create_implicit_cast(left, type_right);
6284 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6285 type_error_incompatible("invalid operands in comparison",
6286 &expression->base.source_position,
6287 type_left, type_right);
6289 expression->base.type = type_int;
6292 static void semantic_arithmetic_assign(binary_expression_t *expression)
6294 expression_t *left = expression->left;
6295 expression_t *right = expression->right;
6296 type_t *orig_type_left = left->base.type;
6297 type_t *orig_type_right = right->base.type;
6299 type_t *type_left = skip_typeref(orig_type_left);
6300 type_t *type_right = skip_typeref(orig_type_right);
6302 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6303 /* TODO: improve error message */
6304 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6305 errorf(HERE, "operation needs arithmetic types");
6310 /* combined instructions are tricky. We can't create an implicit cast on
6311 * the left side, because we need the uncasted form for the store.
6312 * The ast2firm pass has to know that left_type must be right_type
6313 * for the arithmetic operation and create a cast by itself */
6314 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6315 expression->right = create_implicit_cast(right, arithmetic_type);
6316 expression->base.type = type_left;
6319 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6321 expression_t *const left = expression->left;
6322 expression_t *const right = expression->right;
6323 type_t *const orig_type_left = left->base.type;
6324 type_t *const orig_type_right = right->base.type;
6325 type_t *const type_left = skip_typeref(orig_type_left);
6326 type_t *const type_right = skip_typeref(orig_type_right);
6328 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6329 /* combined instructions are tricky. We can't create an implicit cast on
6330 * the left side, because we need the uncasted form for the store.
6331 * The ast2firm pass has to know that left_type must be right_type
6332 * for the arithmetic operation and create a cast by itself */
6333 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6334 expression->right = create_implicit_cast(right, arithmetic_type);
6335 expression->base.type = type_left;
6336 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6337 expression->base.type = type_left;
6338 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6339 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6344 * Check the semantic restrictions of a logical expression.
6346 static void semantic_logical_op(binary_expression_t *expression)
6348 expression_t *const left = expression->left;
6349 expression_t *const right = expression->right;
6350 type_t *const orig_type_left = left->base.type;
6351 type_t *const orig_type_right = right->base.type;
6352 type_t *const type_left = skip_typeref(orig_type_left);
6353 type_t *const type_right = skip_typeref(orig_type_right);
6355 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6356 /* TODO: improve error message */
6357 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6358 errorf(HERE, "operation needs scalar types");
6363 expression->base.type = type_int;
6367 * Checks if a compound type has constant fields.
6369 static bool has_const_fields(const compound_type_t *type)
6371 const scope_t *scope = &type->declaration->scope;
6372 const declaration_t *declaration = scope->declarations;
6374 for (; declaration != NULL; declaration = declaration->next) {
6375 if (declaration->namespc != NAMESPACE_NORMAL)
6378 const type_t *decl_type = skip_typeref(declaration->type);
6379 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6387 * Check the semantic restrictions of a binary assign expression.
6389 static void semantic_binexpr_assign(binary_expression_t *expression)
6391 expression_t *left = expression->left;
6392 type_t *orig_type_left = left->base.type;
6394 type_t *type_left = revert_automatic_type_conversion(left);
6395 type_left = skip_typeref(orig_type_left);
6397 /* must be a modifiable lvalue */
6398 if (is_type_array(type_left)) {
6399 errorf(HERE, "cannot assign to arrays ('%E')", left);
6402 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6403 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6407 if(is_type_incomplete(type_left)) {
6409 "left-hand side of assignment '%E' has incomplete type '%T'",
6410 left, orig_type_left);
6413 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6414 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6415 left, orig_type_left);
6419 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6420 "assignment", &left->base.source_position);
6421 if (res_type == NULL) {
6422 errorf(&expression->base.source_position,
6423 "cannot assign to '%T' from '%T'",
6424 orig_type_left, expression->right->base.type);
6426 expression->right = create_implicit_cast(expression->right, res_type);
6429 expression->base.type = orig_type_left;
6433 * Determine if the outermost operation (or parts thereof) of the given
6434 * expression has no effect in order to generate a warning about this fact.
6435 * Therefore in some cases this only examines some of the operands of the
6436 * expression (see comments in the function and examples below).
6438 * f() + 23; // warning, because + has no effect
6439 * x || f(); // no warning, because x controls execution of f()
6440 * x ? y : f(); // warning, because y has no effect
6441 * (void)x; // no warning to be able to suppress the warning
6442 * This function can NOT be used for an "expression has definitely no effect"-
6444 static bool expression_has_effect(const expression_t *const expr)
6446 switch (expr->kind) {
6447 case EXPR_UNKNOWN: break;
6448 case EXPR_INVALID: return true; /* do NOT warn */
6449 case EXPR_REFERENCE: return false;
6450 /* suppress the warning for microsoft __noop operations */
6451 case EXPR_CONST: return expr->conste.is_ms_noop;
6452 case EXPR_CHARACTER_CONSTANT: return false;
6453 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6454 case EXPR_STRING_LITERAL: return false;
6455 case EXPR_WIDE_STRING_LITERAL: return false;
6458 const call_expression_t *const call = &expr->call;
6459 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6462 switch (call->function->builtin_symbol.symbol->ID) {
6463 case T___builtin_va_end: return true;
6464 default: return false;
6468 /* Generate the warning if either the left or right hand side of a
6469 * conditional expression has no effect */
6470 case EXPR_CONDITIONAL: {
6471 const conditional_expression_t *const cond = &expr->conditional;
6473 expression_has_effect(cond->true_expression) &&
6474 expression_has_effect(cond->false_expression);
6477 case EXPR_SELECT: return false;
6478 case EXPR_ARRAY_ACCESS: return false;
6479 case EXPR_SIZEOF: return false;
6480 case EXPR_CLASSIFY_TYPE: return false;
6481 case EXPR_ALIGNOF: return false;
6483 case EXPR_FUNCNAME: return false;
6484 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6485 case EXPR_BUILTIN_CONSTANT_P: return false;
6486 case EXPR_BUILTIN_PREFETCH: return true;
6487 case EXPR_OFFSETOF: return false;
6488 case EXPR_VA_START: return true;
6489 case EXPR_VA_ARG: return true;
6490 case EXPR_STATEMENT: return true; // TODO
6491 case EXPR_COMPOUND_LITERAL: return false;
6493 case EXPR_UNARY_NEGATE: return false;
6494 case EXPR_UNARY_PLUS: return false;
6495 case EXPR_UNARY_BITWISE_NEGATE: return false;
6496 case EXPR_UNARY_NOT: return false;
6497 case EXPR_UNARY_DEREFERENCE: return false;
6498 case EXPR_UNARY_TAKE_ADDRESS: return false;
6499 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6500 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6501 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6502 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6504 /* Treat void casts as if they have an effect in order to being able to
6505 * suppress the warning */
6506 case EXPR_UNARY_CAST: {
6507 type_t *const type = skip_typeref(expr->base.type);
6508 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6511 case EXPR_UNARY_CAST_IMPLICIT: return true;
6512 case EXPR_UNARY_ASSUME: return true;
6513 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6515 case EXPR_BINARY_ADD: return false;
6516 case EXPR_BINARY_SUB: return false;
6517 case EXPR_BINARY_MUL: return false;
6518 case EXPR_BINARY_DIV: return false;
6519 case EXPR_BINARY_MOD: return false;
6520 case EXPR_BINARY_EQUAL: return false;
6521 case EXPR_BINARY_NOTEQUAL: return false;
6522 case EXPR_BINARY_LESS: return false;
6523 case EXPR_BINARY_LESSEQUAL: return false;
6524 case EXPR_BINARY_GREATER: return false;
6525 case EXPR_BINARY_GREATEREQUAL: return false;
6526 case EXPR_BINARY_BITWISE_AND: return false;
6527 case EXPR_BINARY_BITWISE_OR: return false;
6528 case EXPR_BINARY_BITWISE_XOR: return false;
6529 case EXPR_BINARY_SHIFTLEFT: return false;
6530 case EXPR_BINARY_SHIFTRIGHT: return false;
6531 case EXPR_BINARY_ASSIGN: return true;
6532 case EXPR_BINARY_MUL_ASSIGN: return true;
6533 case EXPR_BINARY_DIV_ASSIGN: return true;
6534 case EXPR_BINARY_MOD_ASSIGN: return true;
6535 case EXPR_BINARY_ADD_ASSIGN: return true;
6536 case EXPR_BINARY_SUB_ASSIGN: return true;
6537 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6538 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6539 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6540 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6541 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6543 /* Only examine the right hand side of && and ||, because the left hand
6544 * side already has the effect of controlling the execution of the right
6546 case EXPR_BINARY_LOGICAL_AND:
6547 case EXPR_BINARY_LOGICAL_OR:
6548 /* Only examine the right hand side of a comma expression, because the left
6549 * hand side has a separate warning */
6550 case EXPR_BINARY_COMMA:
6551 return expression_has_effect(expr->binary.right);
6553 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6554 case EXPR_BINARY_ISGREATER: return false;
6555 case EXPR_BINARY_ISGREATEREQUAL: return false;
6556 case EXPR_BINARY_ISLESS: return false;
6557 case EXPR_BINARY_ISLESSEQUAL: return false;
6558 case EXPR_BINARY_ISLESSGREATER: return false;
6559 case EXPR_BINARY_ISUNORDERED: return false;
6562 internal_errorf(HERE, "unexpected expression");
6565 static void semantic_comma(binary_expression_t *expression)
6567 if (warning.unused_value) {
6568 const expression_t *const left = expression->left;
6569 if (!expression_has_effect(left)) {
6570 warningf(&left->base.source_position,
6571 "left-hand operand of comma expression has no effect");
6574 expression->base.type = expression->right->base.type;
6577 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6578 static expression_t *parse_##binexpression_type(unsigned precedence, \
6579 expression_t *left) \
6582 source_position_t pos = *HERE; \
6584 expression_t *right = parse_sub_expression(precedence + lr); \
6586 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6587 binexpr->base.source_position = pos; \
6588 binexpr->binary.left = left; \
6589 binexpr->binary.right = right; \
6590 sfunc(&binexpr->binary); \
6595 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6596 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6597 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6598 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6599 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6600 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6601 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6602 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6603 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6605 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6606 semantic_comparison, 1)
6607 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6608 semantic_comparison, 1)
6609 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6610 semantic_comparison, 1)
6611 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6612 semantic_comparison, 1)
6614 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6615 semantic_binexpr_arithmetic, 1)
6616 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6617 semantic_binexpr_arithmetic, 1)
6618 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6619 semantic_binexpr_arithmetic, 1)
6620 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6621 semantic_logical_op, 1)
6622 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6623 semantic_logical_op, 1)
6624 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6625 semantic_shift_op, 1)
6626 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6627 semantic_shift_op, 1)
6628 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6629 semantic_arithmetic_addsubb_assign, 0)
6630 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6631 semantic_arithmetic_addsubb_assign, 0)
6632 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6633 semantic_arithmetic_assign, 0)
6634 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6635 semantic_arithmetic_assign, 0)
6636 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6637 semantic_arithmetic_assign, 0)
6638 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6639 semantic_arithmetic_assign, 0)
6640 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6641 semantic_arithmetic_assign, 0)
6642 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6643 semantic_arithmetic_assign, 0)
6644 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6645 semantic_arithmetic_assign, 0)
6646 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6647 semantic_arithmetic_assign, 0)
6649 static expression_t *parse_sub_expression(unsigned precedence)
6651 if(token.type < 0) {
6652 return expected_expression_error();
6655 expression_parser_function_t *parser
6656 = &expression_parsers[token.type];
6657 source_position_t source_position = token.source_position;
6660 if(parser->parser != NULL) {
6661 left = parser->parser(parser->precedence);
6663 left = parse_primary_expression();
6665 assert(left != NULL);
6666 left->base.source_position = source_position;
6669 if(token.type < 0) {
6670 return expected_expression_error();
6673 parser = &expression_parsers[token.type];
6674 if(parser->infix_parser == NULL)
6676 if(parser->infix_precedence < precedence)
6679 left = parser->infix_parser(parser->infix_precedence, left);
6681 assert(left != NULL);
6682 assert(left->kind != EXPR_UNKNOWN);
6683 left->base.source_position = source_position;
6690 * Parse an expression.
6692 static expression_t *parse_expression(void)
6694 return parse_sub_expression(1);
6698 * Register a parser for a prefix-like operator with given precedence.
6700 * @param parser the parser function
6701 * @param token_type the token type of the prefix token
6702 * @param precedence the precedence of the operator
6704 static void register_expression_parser(parse_expression_function parser,
6705 int token_type, unsigned precedence)
6707 expression_parser_function_t *entry = &expression_parsers[token_type];
6709 if(entry->parser != NULL) {
6710 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6711 panic("trying to register multiple expression parsers for a token");
6713 entry->parser = parser;
6714 entry->precedence = precedence;
6718 * Register a parser for an infix operator with given precedence.
6720 * @param parser the parser function
6721 * @param token_type the token type of the infix operator
6722 * @param precedence the precedence of the operator
6724 static void register_infix_parser(parse_expression_infix_function parser,
6725 int token_type, unsigned precedence)
6727 expression_parser_function_t *entry = &expression_parsers[token_type];
6729 if(entry->infix_parser != NULL) {
6730 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6731 panic("trying to register multiple infix expression parsers for a "
6734 entry->infix_parser = parser;
6735 entry->infix_precedence = precedence;
6739 * Initialize the expression parsers.
6741 static void init_expression_parsers(void)
6743 memset(&expression_parsers, 0, sizeof(expression_parsers));
6745 register_infix_parser(parse_array_expression, '[', 30);
6746 register_infix_parser(parse_call_expression, '(', 30);
6747 register_infix_parser(parse_select_expression, '.', 30);
6748 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6749 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6751 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6754 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6755 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6756 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6757 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6758 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6759 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6760 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6761 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6762 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6763 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6764 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6765 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6766 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6767 T_EXCLAMATIONMARKEQUAL, 13);
6768 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6769 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6770 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6771 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6772 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6773 register_infix_parser(parse_conditional_expression, '?', 7);
6774 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6775 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6776 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6777 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6778 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6779 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6780 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6781 T_LESSLESSEQUAL, 2);
6782 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6783 T_GREATERGREATEREQUAL, 2);
6784 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6786 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6788 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6791 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6793 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6794 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6795 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6796 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6797 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6798 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6799 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6801 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6803 register_expression_parser(parse_sizeof, T_sizeof, 25);
6804 register_expression_parser(parse_alignof, T___alignof__, 25);
6805 register_expression_parser(parse_extension, T___extension__, 25);
6806 register_expression_parser(parse_builtin_classify_type,
6807 T___builtin_classify_type, 25);
6811 * Parse a asm statement constraints specification.
6813 static asm_constraint_t *parse_asm_constraints(void)
6815 asm_constraint_t *result = NULL;
6816 asm_constraint_t *last = NULL;
6818 while(token.type == T_STRING_LITERAL || token.type == '[') {
6819 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6820 memset(constraint, 0, sizeof(constraint[0]));
6822 if(token.type == '[') {
6824 if(token.type != T_IDENTIFIER) {
6825 parse_error_expected("while parsing asm constraint",
6829 constraint->symbol = token.v.symbol;
6834 constraint->constraints = parse_string_literals();
6836 constraint->expression = parse_expression();
6840 last->next = constraint;
6842 result = constraint;
6846 if(token.type != ',')
6857 * Parse a asm statement clobber specification.
6859 static asm_clobber_t *parse_asm_clobbers(void)
6861 asm_clobber_t *result = NULL;
6862 asm_clobber_t *last = NULL;
6864 while(token.type == T_STRING_LITERAL) {
6865 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6866 clobber->clobber = parse_string_literals();
6869 last->next = clobber;
6875 if(token.type != ',')
6884 * Parse an asm statement.
6886 static statement_t *parse_asm_statement(void)
6890 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6891 statement->base.source_position = token.source_position;
6893 asm_statement_t *asm_statement = &statement->asms;
6895 if(token.type == T_volatile) {
6897 asm_statement->is_volatile = true;
6901 add_anchor_token(')');
6902 add_anchor_token(':');
6903 asm_statement->asm_text = parse_string_literals();
6905 if(token.type != ':') {
6906 rem_anchor_token(':');
6911 asm_statement->inputs = parse_asm_constraints();
6912 if(token.type != ':') {
6913 rem_anchor_token(':');
6918 asm_statement->outputs = parse_asm_constraints();
6919 if(token.type != ':') {
6920 rem_anchor_token(':');
6923 rem_anchor_token(':');
6926 asm_statement->clobbers = parse_asm_clobbers();
6929 rem_anchor_token(')');
6934 return create_invalid_statement();
6938 * Parse a case statement.
6940 static statement_t *parse_case_statement(void)
6944 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6946 statement->base.source_position = token.source_position;
6947 statement->case_label.expression = parse_expression();
6949 if (c_mode & _GNUC) {
6950 if (token.type == T_DOTDOTDOT) {
6952 statement->case_label.end_range = parse_expression();
6958 if (! is_constant_expression(statement->case_label.expression)) {
6959 errorf(&statement->base.source_position,
6960 "case label does not reduce to an integer constant");
6962 /* TODO: check if the case label is already known */
6963 if (current_switch != NULL) {
6964 /* link all cases into the switch statement */
6965 if (current_switch->last_case == NULL) {
6966 current_switch->first_case =
6967 current_switch->last_case = &statement->case_label;
6969 current_switch->last_case->next = &statement->case_label;
6972 errorf(&statement->base.source_position,
6973 "case label not within a switch statement");
6976 statement->case_label.statement = parse_statement();
6980 return create_invalid_statement();
6984 * Finds an existing default label of a switch statement.
6986 static case_label_statement_t *
6987 find_default_label(const switch_statement_t *statement)
6989 case_label_statement_t *label = statement->first_case;
6990 for ( ; label != NULL; label = label->next) {
6991 if (label->expression == NULL)
6998 * Parse a default statement.
7000 static statement_t *parse_default_statement(void)
7004 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7006 statement->base.source_position = token.source_position;
7009 if (current_switch != NULL) {
7010 const case_label_statement_t *def_label = find_default_label(current_switch);
7011 if (def_label != NULL) {
7012 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7013 &def_label->base.source_position);
7015 /* link all cases into the switch statement */
7016 if (current_switch->last_case == NULL) {
7017 current_switch->first_case =
7018 current_switch->last_case = &statement->case_label;
7020 current_switch->last_case->next = &statement->case_label;
7024 errorf(&statement->base.source_position,
7025 "'default' label not within a switch statement");
7027 statement->case_label.statement = parse_statement();
7031 return create_invalid_statement();
7035 * Return the declaration for a given label symbol or create a new one.
7037 static declaration_t *get_label(symbol_t *symbol)
7039 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7040 assert(current_function != NULL);
7041 /* if we found a label in the same function, then we already created the
7043 if(candidate != NULL
7044 && candidate->parent_scope == ¤t_function->scope) {
7048 /* otherwise we need to create a new one */
7049 declaration_t *const declaration = allocate_declaration_zero();
7050 declaration->namespc = NAMESPACE_LABEL;
7051 declaration->symbol = symbol;
7053 label_push(declaration);
7059 * Parse a label statement.
7061 static statement_t *parse_label_statement(void)
7063 assert(token.type == T_IDENTIFIER);
7064 symbol_t *symbol = token.v.symbol;
7067 declaration_t *label = get_label(symbol);
7069 /* if source position is already set then the label is defined twice,
7070 * otherwise it was just mentioned in a goto so far */
7071 if(label->source_position.input_name != NULL) {
7072 errorf(HERE, "duplicate label '%Y' (declared %P)",
7073 symbol, &label->source_position);
7075 label->source_position = token.source_position;
7078 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7080 statement->base.source_position = token.source_position;
7081 statement->label.label = label;
7085 if(token.type == '}') {
7086 /* TODO only warn? */
7088 warningf(HERE, "label at end of compound statement");
7089 statement->label.statement = create_empty_statement();
7091 errorf(HERE, "label at end of compound statement");
7092 statement->label.statement = create_invalid_statement();
7096 if (token.type == ';') {
7097 /* eat an empty statement here, to avoid the warning about an empty
7098 * after a label. label:; is commonly used to have a label before
7100 statement->label.statement = create_empty_statement();
7103 statement->label.statement = parse_statement();
7107 /* remember the labels's in a list for later checking */
7108 if (label_last == NULL) {
7109 label_first = &statement->label;
7111 label_last->next = &statement->label;
7113 label_last = &statement->label;
7119 * Parse an if statement.
7121 static statement_t *parse_if(void)
7125 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7126 statement->base.source_position = token.source_position;
7129 add_anchor_token(')');
7130 statement->ifs.condition = parse_expression();
7131 rem_anchor_token(')');
7134 add_anchor_token(T_else);
7135 statement->ifs.true_statement = parse_statement();
7136 rem_anchor_token(T_else);
7138 if(token.type == T_else) {
7140 statement->ifs.false_statement = parse_statement();
7145 return create_invalid_statement();
7149 * Parse a switch statement.
7151 static statement_t *parse_switch(void)
7155 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7156 statement->base.source_position = token.source_position;
7159 expression_t *const expr = parse_expression();
7160 type_t * type = skip_typeref(expr->base.type);
7161 if (is_type_integer(type)) {
7162 type = promote_integer(type);
7163 } else if (is_type_valid(type)) {
7164 errorf(&expr->base.source_position,
7165 "switch quantity is not an integer, but '%T'", type);
7166 type = type_error_type;
7168 statement->switchs.expression = create_implicit_cast(expr, type);
7171 switch_statement_t *rem = current_switch;
7172 current_switch = &statement->switchs;
7173 statement->switchs.body = parse_statement();
7174 current_switch = rem;
7176 if(warning.switch_default &&
7177 find_default_label(&statement->switchs) == NULL) {
7178 warningf(&statement->base.source_position, "switch has no default case");
7183 return create_invalid_statement();
7186 static statement_t *parse_loop_body(statement_t *const loop)
7188 statement_t *const rem = current_loop;
7189 current_loop = loop;
7191 statement_t *const body = parse_statement();
7198 * Parse a while statement.
7200 static statement_t *parse_while(void)
7204 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7205 statement->base.source_position = token.source_position;
7208 add_anchor_token(')');
7209 statement->whiles.condition = parse_expression();
7210 rem_anchor_token(')');
7213 statement->whiles.body = parse_loop_body(statement);
7217 return create_invalid_statement();
7221 * Parse a do statement.
7223 static statement_t *parse_do(void)
7227 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7229 statement->base.source_position = token.source_position;
7231 add_anchor_token(T_while);
7232 statement->do_while.body = parse_loop_body(statement);
7233 rem_anchor_token(T_while);
7237 add_anchor_token(')');
7238 statement->do_while.condition = parse_expression();
7239 rem_anchor_token(')');
7245 return create_invalid_statement();
7249 * Parse a for statement.
7251 static statement_t *parse_for(void)
7255 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7256 statement->base.source_position = token.source_position;
7258 int top = environment_top();
7259 scope_t *last_scope = scope;
7260 set_scope(&statement->fors.scope);
7263 add_anchor_token(')');
7265 if(token.type != ';') {
7266 if(is_declaration_specifier(&token, false)) {
7267 parse_declaration(record_declaration);
7269 add_anchor_token(';');
7270 expression_t *const init = parse_expression();
7271 statement->fors.initialisation = init;
7272 if (warning.unused_value && !expression_has_effect(init)) {
7273 warningf(&init->base.source_position,
7274 "initialisation of 'for'-statement has no effect");
7276 rem_anchor_token(';');
7283 if(token.type != ';') {
7284 add_anchor_token(';');
7285 statement->fors.condition = parse_expression();
7286 rem_anchor_token(';');
7289 if(token.type != ')') {
7290 expression_t *const step = parse_expression();
7291 statement->fors.step = step;
7292 if (warning.unused_value && !expression_has_effect(step)) {
7293 warningf(&step->base.source_position,
7294 "step of 'for'-statement has no effect");
7297 rem_anchor_token(')');
7299 statement->fors.body = parse_loop_body(statement);
7301 assert(scope == &statement->fors.scope);
7302 set_scope(last_scope);
7303 environment_pop_to(top);
7308 rem_anchor_token(')');
7309 assert(scope == &statement->fors.scope);
7310 set_scope(last_scope);
7311 environment_pop_to(top);
7313 return create_invalid_statement();
7317 * Parse a goto statement.
7319 static statement_t *parse_goto(void)
7323 if(token.type != T_IDENTIFIER) {
7324 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
7328 symbol_t *symbol = token.v.symbol;
7331 declaration_t *label = get_label(symbol);
7333 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7334 statement->base.source_position = token.source_position;
7336 statement->gotos.label = label;
7338 /* remember the goto's in a list for later checking */
7339 if (goto_last == NULL) {
7340 goto_first = &statement->gotos;
7342 goto_last->next = &statement->gotos;
7344 goto_last = &statement->gotos;
7350 return create_invalid_statement();
7354 * Parse a continue statement.
7356 static statement_t *parse_continue(void)
7358 statement_t *statement;
7359 if (current_loop == NULL) {
7360 errorf(HERE, "continue statement not within loop");
7363 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7365 statement->base.source_position = token.source_position;
7373 return create_invalid_statement();
7377 * Parse a break statement.
7379 static statement_t *parse_break(void)
7381 statement_t *statement;
7382 if (current_switch == NULL && current_loop == NULL) {
7383 errorf(HERE, "break statement not within loop or switch");
7386 statement = allocate_statement_zero(STATEMENT_BREAK);
7388 statement->base.source_position = token.source_position;
7396 return create_invalid_statement();
7400 * Check if a given declaration represents a local variable.
7402 static bool is_local_var_declaration(const declaration_t *declaration) {
7403 switch ((storage_class_tag_t) declaration->storage_class) {
7404 case STORAGE_CLASS_AUTO:
7405 case STORAGE_CLASS_REGISTER: {
7406 const type_t *type = skip_typeref(declaration->type);
7407 if(is_type_function(type)) {
7419 * Check if a given declaration represents a variable.
7421 static bool is_var_declaration(const declaration_t *declaration) {
7422 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7425 const type_t *type = skip_typeref(declaration->type);
7426 return !is_type_function(type);
7430 * Check if a given expression represents a local variable.
7432 static bool is_local_variable(const expression_t *expression)
7434 if (expression->base.kind != EXPR_REFERENCE) {
7437 const declaration_t *declaration = expression->reference.declaration;
7438 return is_local_var_declaration(declaration);
7442 * Check if a given expression represents a local variable and
7443 * return its declaration then, else return NULL.
7445 declaration_t *expr_is_variable(const expression_t *expression)
7447 if (expression->base.kind != EXPR_REFERENCE) {
7450 declaration_t *declaration = expression->reference.declaration;
7451 if (is_var_declaration(declaration))
7457 * Parse a return statement.
7459 static statement_t *parse_return(void)
7461 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7462 statement->base.source_position = token.source_position;
7466 expression_t *return_value = NULL;
7467 if(token.type != ';') {
7468 return_value = parse_expression();
7472 const type_t *const func_type = current_function->type;
7473 assert(is_type_function(func_type));
7474 type_t *const return_type = skip_typeref(func_type->function.return_type);
7476 if(return_value != NULL) {
7477 type_t *return_value_type = skip_typeref(return_value->base.type);
7479 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7480 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7481 warningf(&statement->base.source_position,
7482 "'return' with a value, in function returning void");
7483 return_value = NULL;
7485 type_t *const res_type = semantic_assign(return_type,
7486 return_value, "'return'", &statement->base.source_position);
7487 if (res_type == NULL) {
7488 errorf(&statement->base.source_position,
7489 "cannot return something of type '%T' in function returning '%T'",
7490 return_value->base.type, return_type);
7492 return_value = create_implicit_cast(return_value, res_type);
7495 /* check for returning address of a local var */
7496 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7497 const expression_t *expression = return_value->unary.value;
7498 if (is_local_variable(expression)) {
7499 warningf(&statement->base.source_position,
7500 "function returns address of local variable");
7504 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7505 warningf(&statement->base.source_position,
7506 "'return' without value, in function returning non-void");
7509 statement->returns.value = return_value;
7513 return create_invalid_statement();
7517 * Parse a declaration statement.
7519 static statement_t *parse_declaration_statement(void)
7521 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7523 statement->base.source_position = token.source_position;
7525 declaration_t *before = last_declaration;
7526 parse_declaration(record_declaration);
7528 if(before == NULL) {
7529 statement->declaration.declarations_begin = scope->declarations;
7531 statement->declaration.declarations_begin = before->next;
7533 statement->declaration.declarations_end = last_declaration;
7539 * Parse an expression statement, ie. expr ';'.
7541 static statement_t *parse_expression_statement(void)
7543 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7545 statement->base.source_position = token.source_position;
7546 expression_t *const expr = parse_expression();
7547 statement->expression.expression = expr;
7549 if (warning.unused_value && !expression_has_effect(expr)) {
7550 warningf(&expr->base.source_position, "statement has no effect");
7557 return create_invalid_statement();
7561 * Parse a microsoft __try { } __finally { } or
7562 * __try{ } __except() { }
7564 static statement_t *parse_ms_try_statment(void) {
7565 statement_t *statement = allocate_statement_zero(STATEMENT_MS_TRY);
7567 statement->base.source_position = token.source_position;
7570 statement->ms_try.try_statement = parse_compound_statement();
7572 if(token.type == T___except) {
7575 add_anchor_token(')');
7576 expression_t *const expr = parse_expression();
7577 type_t * type = skip_typeref(expr->base.type);
7578 if (is_type_integer(type)) {
7579 type = promote_integer(type);
7580 } else if (is_type_valid(type)) {
7581 errorf(&expr->base.source_position,
7582 "__expect expression is not an integer, but '%T'", type);
7583 type = type_error_type;
7585 statement->ms_try.except_expression = create_implicit_cast(expr, type);
7586 rem_anchor_token(')');
7588 statement->ms_try.final_statement = parse_compound_statement();
7589 } else if(token.type == T__finally) {
7591 statement->ms_try.final_statement = parse_compound_statement();
7593 parse_error_expected("while parsing __try statement", T___except, T___finally, 0);
7594 return create_invalid_statement();
7598 return create_invalid_statement();
7602 * Parse a statement.
7604 static statement_t *parse_statement(void)
7606 statement_t *statement = NULL;
7608 /* declaration or statement */
7609 add_anchor_token(';');
7610 switch(token.type) {
7612 statement = parse_asm_statement();
7616 statement = parse_case_statement();
7620 statement = parse_default_statement();
7624 statement = parse_compound_statement();
7628 statement = parse_if();
7632 statement = parse_switch();
7636 statement = parse_while();
7640 statement = parse_do();
7644 statement = parse_for();
7648 statement = parse_goto();
7652 statement = parse_continue();
7656 statement = parse_break();
7660 statement = parse_return();
7664 if(warning.empty_statement) {
7665 warningf(HERE, "statement is empty");
7667 statement = create_empty_statement();
7672 if(look_ahead(1)->type == ':') {
7673 statement = parse_label_statement();
7677 if(is_typedef_symbol(token.v.symbol)) {
7678 statement = parse_declaration_statement();
7682 statement = parse_expression_statement();
7685 case T___extension__:
7686 /* this can be a prefix to a declaration or an expression statement */
7687 /* we simply eat it now and parse the rest with tail recursion */
7690 } while(token.type == T___extension__);
7691 statement = parse_statement();
7695 statement = parse_declaration_statement();
7699 statement = parse_ms_try_statment();
7703 statement = parse_expression_statement();
7706 rem_anchor_token(';');
7708 assert(statement != NULL
7709 && statement->base.source_position.input_name != NULL);
7715 * Parse a compound statement.
7717 static statement_t *parse_compound_statement(void)
7719 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7721 statement->base.source_position = token.source_position;
7724 add_anchor_token('}');
7726 int top = environment_top();
7727 scope_t *last_scope = scope;
7728 set_scope(&statement->compound.scope);
7730 statement_t *last_statement = NULL;
7732 while(token.type != '}' && token.type != T_EOF) {
7733 statement_t *sub_statement = parse_statement();
7734 if(is_invalid_statement(sub_statement)) {
7735 /* an error occurred. if we are at an anchor, return */
7741 if(last_statement != NULL) {
7742 last_statement->base.next = sub_statement;
7744 statement->compound.statements = sub_statement;
7747 while(sub_statement->base.next != NULL)
7748 sub_statement = sub_statement->base.next;
7750 last_statement = sub_statement;
7753 if(token.type == '}') {
7756 errorf(&statement->base.source_position,
7757 "end of file while looking for closing '}'");
7761 rem_anchor_token('}');
7762 assert(scope == &statement->compound.scope);
7763 set_scope(last_scope);
7764 environment_pop_to(top);
7770 * Initialize builtin types.
7772 static void initialize_builtin_types(void)
7774 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7775 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7776 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7777 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7778 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7779 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7780 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7781 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7783 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7784 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7785 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7786 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7790 * Check for unused global static functions and variables
7792 static void check_unused_globals(void)
7794 if (!warning.unused_function && !warning.unused_variable)
7797 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7798 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7801 type_t *const type = decl->type;
7803 if (is_type_function(skip_typeref(type))) {
7804 if (!warning.unused_function || decl->is_inline)
7807 s = (decl->init.statement != NULL ? "defined" : "declared");
7809 if (!warning.unused_variable)
7815 warningf(&decl->source_position, "'%#T' %s but not used",
7816 type, decl->symbol, s);
7821 * Parse a translation unit.
7823 static translation_unit_t *parse_translation_unit(void)
7825 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7827 assert(global_scope == NULL);
7828 global_scope = &unit->scope;
7830 assert(scope == NULL);
7831 set_scope(&unit->scope);
7833 initialize_builtin_types();
7835 while(token.type != T_EOF) {
7836 if (token.type == ';') {
7837 /* TODO error in strict mode */
7838 warningf(HERE, "stray ';' outside of function");
7841 parse_external_declaration();
7845 assert(scope == &unit->scope);
7847 last_declaration = NULL;
7849 assert(global_scope == &unit->scope);
7850 check_unused_globals();
7851 global_scope = NULL;
7859 * @return the translation unit or NULL if errors occurred.
7861 translation_unit_t *parse(void)
7863 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7864 label_stack = NEW_ARR_F(stack_entry_t, 0);
7865 diagnostic_count = 0;
7869 type_set_output(stderr);
7870 ast_set_output(stderr);
7872 lookahead_bufpos = 0;
7873 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7876 translation_unit_t *unit = parse_translation_unit();
7878 DEL_ARR_F(environment_stack);
7879 DEL_ARR_F(label_stack);
7885 * Initialize the parser.
7887 void init_parser(void)
7890 /* add predefined symbols for extended-decl-modifier */
7891 sym_align = symbol_table_insert("align");
7892 sym_allocate = symbol_table_insert("allocate");
7893 sym_dllimport = symbol_table_insert("dllimport");
7894 sym_dllexport = symbol_table_insert("dllexport");
7895 sym_naked = symbol_table_insert("naked");
7896 sym_noinline = symbol_table_insert("noinline");
7897 sym_noreturn = symbol_table_insert("noreturn");
7898 sym_nothrow = symbol_table_insert("nothrow");
7899 sym_novtable = symbol_table_insert("novtable");
7900 sym_property = symbol_table_insert("property");
7901 sym_get = symbol_table_insert("get");
7902 sym_put = symbol_table_insert("put");
7903 sym_selectany = symbol_table_insert("selectany");
7904 sym_thread = symbol_table_insert("thread");
7905 sym_uuid = symbol_table_insert("uuid");
7906 sym_deprecated = symbol_table_insert("deprecated");
7907 sym_restrict = symbol_table_insert("restrict");
7908 sym_noalias = symbol_table_insert("noalias");
7910 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7912 init_expression_parsers();
7913 obstack_init(&temp_obst);
7915 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7916 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7920 * Terminate the parser.
7922 void exit_parser(void)
7924 obstack_free(&temp_obst, NULL);