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)
250 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
251 assert(sizes[kind] != 0);
256 * Returns the size of an expression node.
258 * @param kind the expression kind
260 static size_t get_expression_struct_size(expression_kind_t kind)
262 static const size_t sizes[] = {
263 [EXPR_INVALID] = sizeof(expression_base_t),
264 [EXPR_REFERENCE] = sizeof(reference_expression_t),
265 [EXPR_CONST] = sizeof(const_expression_t),
266 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
267 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
268 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
269 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
270 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
271 [EXPR_CALL] = sizeof(call_expression_t),
272 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
273 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
274 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
275 [EXPR_SELECT] = sizeof(select_expression_t),
276 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
277 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
278 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
279 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
280 [EXPR_FUNCNAME] = sizeof(funcname_expression_t),
281 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
282 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
283 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
284 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
285 [EXPR_VA_START] = sizeof(va_start_expression_t),
286 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
287 [EXPR_STATEMENT] = sizeof(statement_expression_t),
289 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
290 return sizes[EXPR_UNARY_FIRST];
292 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
293 return sizes[EXPR_BINARY_FIRST];
295 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
296 assert(sizes[kind] != 0);
301 * Allocate a statement node of given kind and initialize all
304 static statement_t *allocate_statement_zero(statement_kind_t kind)
306 size_t size = get_statement_struct_size(kind);
307 statement_t *res = allocate_ast_zero(size);
309 res->base.kind = kind;
314 * Allocate an expression node of given kind and initialize all
317 static expression_t *allocate_expression_zero(expression_kind_t kind)
319 size_t size = get_expression_struct_size(kind);
320 expression_t *res = allocate_ast_zero(size);
322 res->base.kind = kind;
323 res->base.type = type_error_type;
328 * Creates a new invalid expression.
330 static expression_t *create_invalid_expression(void)
332 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
333 expression->base.source_position = token.source_position;
338 * Creates a new invalid statement.
340 static statement_t *create_invalid_statement(void)
342 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
343 statement->base.source_position = token.source_position;
348 * Allocate a new empty statement.
350 static statement_t *create_empty_statement(void)
352 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
353 statement->base.source_position = token.source_position;
358 * Returns the size of a type node.
360 * @param kind the type kind
362 static size_t get_type_struct_size(type_kind_t kind)
364 static const size_t sizes[] = {
365 [TYPE_ATOMIC] = sizeof(atomic_type_t),
366 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
367 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
368 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
369 [TYPE_ENUM] = sizeof(enum_type_t),
370 [TYPE_FUNCTION] = sizeof(function_type_t),
371 [TYPE_POINTER] = sizeof(pointer_type_t),
372 [TYPE_ARRAY] = sizeof(array_type_t),
373 [TYPE_BUILTIN] = sizeof(builtin_type_t),
374 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
375 [TYPE_TYPEOF] = sizeof(typeof_type_t),
377 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
378 assert(kind <= TYPE_TYPEOF);
379 assert(sizes[kind] != 0);
384 * Allocate a type node of given kind and initialize all
387 static type_t *allocate_type_zero(type_kind_t kind, const source_position_t *source_position)
389 size_t size = get_type_struct_size(kind);
390 type_t *res = obstack_alloc(type_obst, size);
391 memset(res, 0, size);
393 res->base.kind = kind;
394 res->base.source_position = *source_position;
399 * Returns the size of an initializer node.
401 * @param kind the initializer kind
403 static size_t get_initializer_size(initializer_kind_t kind)
405 static const size_t sizes[] = {
406 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
407 [INITIALIZER_STRING] = sizeof(initializer_string_t),
408 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
409 [INITIALIZER_LIST] = sizeof(initializer_list_t),
410 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
412 assert(kind < sizeof(sizes) / sizeof(*sizes));
413 assert(sizes[kind] != 0);
418 * Allocate an initializer node of given kind and initialize all
421 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
423 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
430 * Free a type from the type obstack.
432 static void free_type(void *type)
434 obstack_free(type_obst, type);
438 * Returns the index of the top element of the environment stack.
440 static size_t environment_top(void)
442 return ARR_LEN(environment_stack);
446 * Returns the index of the top element of the label stack.
448 static size_t label_top(void)
450 return ARR_LEN(label_stack);
454 * Return the next token.
456 static inline void next_token(void)
458 token = lookahead_buffer[lookahead_bufpos];
459 lookahead_buffer[lookahead_bufpos] = lexer_token;
462 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
465 print_token(stderr, &token);
466 fprintf(stderr, "\n");
471 * Return the next token with a given lookahead.
473 static inline const token_t *look_ahead(int num)
475 assert(num > 0 && num <= MAX_LOOKAHEAD);
476 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
477 return &lookahead_buffer[pos];
481 * Adds a token to the token anchor set (a multi-set).
483 static void add_anchor_token(int token_type) {
484 assert(0 <= token_type && token_type < T_LAST_TOKEN);
485 ++token_anchor_set[token_type];
489 * Remove a token from the token anchor set (a multi-set).
491 static void rem_anchor_token(int token_type) {
492 assert(0 <= token_type && token_type < T_LAST_TOKEN);
493 --token_anchor_set[token_type];
496 static bool at_anchor(void) {
499 return token_anchor_set[token.type];
503 * Eat tokens until a matching token is found.
505 static void eat_until_matching_token(int type) {
506 unsigned parenthesis_count = 0;
507 unsigned brace_count = 0;
508 unsigned bracket_count = 0;
509 int end_token = type;
518 while(token.type != end_token ||
519 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
523 case '(': ++parenthesis_count; break;
524 case '{': ++brace_count; break;
525 case '[': ++bracket_count; break;
527 if(parenthesis_count > 0)
535 if(bracket_count > 0)
546 * Eat input tokens until an anchor is found.
548 static void eat_until_anchor(void) {
549 if(token.type == T_EOF)
551 while(token_anchor_set[token.type] == 0) {
552 if(token.type == '(' || token.type == '{' || token.type == '[')
553 eat_until_matching_token(token.type);
554 if(token.type == T_EOF)
560 static void eat_block(void) {
561 eat_until_matching_token('{');
562 if(token.type == '}')
567 * eat all token until a ';' is reached
568 * or a stop token is found.
570 static void eat_statement(void) {
571 eat_until_matching_token(';');
572 if(token.type == ';')
576 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
579 * Report a parse error because an expected token was not found.
581 static void parse_error_expected(const char *message, ...)
583 if(message != NULL) {
584 errorf(HERE, "%s", message);
587 va_start(ap, message);
588 errorf(HERE, "got %K, expected %#k", &token, &ap, "a ");
593 * Report a type error.
595 static void type_error(const char *msg, const source_position_t *source_position,
598 errorf(source_position, "%s, but found type '%T'", msg, type);
602 * Report an incompatible type.
604 static void type_error_incompatible(const char *msg,
605 const source_position_t *source_position, type_t *type1, type_t *type2)
607 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
611 * Expect the the current token is the expected token.
612 * If not, generate an error, eat the current statement,
613 * and goto the end_error label.
615 #define expect(expected) \
617 if(UNLIKELY(token.type != (expected))) { \
618 parse_error_expected(NULL, (expected), 0); \
619 add_anchor_token(expected); \
620 eat_until_anchor(); \
621 rem_anchor_token(expected); \
627 static void set_scope(scope_t *new_scope)
630 scope->last_declaration = last_declaration;
634 last_declaration = new_scope->last_declaration;
638 * Search a symbol in a given namespace and returns its declaration or
639 * NULL if this symbol was not found.
641 static declaration_t *get_declaration(const symbol_t *const symbol,
642 const namespace_t namespc)
644 declaration_t *declaration = symbol->declaration;
645 for( ; declaration != NULL; declaration = declaration->symbol_next) {
646 if(declaration->namespc == namespc)
654 * pushs an environment_entry on the environment stack and links the
655 * corresponding symbol to the new entry
657 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
659 symbol_t *symbol = declaration->symbol;
660 namespace_t namespc = (namespace_t) declaration->namespc;
662 /* replace/add declaration into declaration list of the symbol */
663 declaration_t *iter = symbol->declaration;
665 symbol->declaration = declaration;
667 declaration_t *iter_last = NULL;
668 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
669 /* replace an entry? */
670 if(iter->namespc == namespc) {
671 if(iter_last == NULL) {
672 symbol->declaration = declaration;
674 iter_last->symbol_next = declaration;
676 declaration->symbol_next = iter->symbol_next;
681 assert(iter_last->symbol_next == NULL);
682 iter_last->symbol_next = declaration;
686 /* remember old declaration */
688 entry.symbol = symbol;
689 entry.old_declaration = iter;
690 entry.namespc = (unsigned short) namespc;
691 ARR_APP1(stack_entry_t, *stack_ptr, entry);
694 static void environment_push(declaration_t *declaration)
696 assert(declaration->source_position.input_name != NULL);
697 assert(declaration->parent_scope != NULL);
698 stack_push(&environment_stack, declaration);
701 static void label_push(declaration_t *declaration)
703 declaration->parent_scope = ¤t_function->scope;
704 stack_push(&label_stack, declaration);
708 * pops symbols from the environment stack until @p new_top is the top element
710 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
712 stack_entry_t *stack = *stack_ptr;
713 size_t top = ARR_LEN(stack);
716 assert(new_top <= top);
720 for(i = top; i > new_top; --i) {
721 stack_entry_t *entry = &stack[i - 1];
723 declaration_t *old_declaration = entry->old_declaration;
724 symbol_t *symbol = entry->symbol;
725 namespace_t namespc = (namespace_t)entry->namespc;
727 /* replace/remove declaration */
728 declaration_t *declaration = symbol->declaration;
729 assert(declaration != NULL);
730 if(declaration->namespc == namespc) {
731 if(old_declaration == NULL) {
732 symbol->declaration = declaration->symbol_next;
734 symbol->declaration = old_declaration;
737 declaration_t *iter_last = declaration;
738 declaration_t *iter = declaration->symbol_next;
739 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
740 /* replace an entry? */
741 if(iter->namespc == namespc) {
742 assert(iter_last != NULL);
743 iter_last->symbol_next = old_declaration;
744 if(old_declaration != NULL) {
745 old_declaration->symbol_next = iter->symbol_next;
750 assert(iter != NULL);
754 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
757 static void environment_pop_to(size_t new_top)
759 stack_pop_to(&environment_stack, new_top);
762 static void label_pop_to(size_t new_top)
764 stack_pop_to(&label_stack, new_top);
768 static int get_rank(const type_t *type)
770 assert(!is_typeref(type));
771 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
772 * and esp. footnote 108). However we can't fold constants (yet), so we
773 * can't decide whether unsigned int is possible, while int always works.
774 * (unsigned int would be preferable when possible... for stuff like
775 * struct { enum { ... } bla : 4; } ) */
776 if(type->kind == TYPE_ENUM)
777 return ATOMIC_TYPE_INT;
779 assert(type->kind == TYPE_ATOMIC);
780 return type->atomic.akind;
783 static type_t *promote_integer(type_t *type)
785 if(type->kind == TYPE_BITFIELD)
786 type = type->bitfield.base;
788 if(get_rank(type) < ATOMIC_TYPE_INT)
795 * Create a cast expression.
797 * @param expression the expression to cast
798 * @param dest_type the destination type
800 static expression_t *create_cast_expression(expression_t *expression,
803 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
805 cast->unary.value = expression;
806 cast->base.type = dest_type;
812 * Check if a given expression represents the 0 pointer constant.
814 static bool is_null_pointer_constant(const expression_t *expression)
816 /* skip void* cast */
817 if(expression->kind == EXPR_UNARY_CAST
818 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
819 expression = expression->unary.value;
822 /* TODO: not correct yet, should be any constant integer expression
823 * which evaluates to 0 */
824 if (expression->kind != EXPR_CONST)
827 type_t *const type = skip_typeref(expression->base.type);
828 if (!is_type_integer(type))
831 return expression->conste.v.int_value == 0;
835 * Create an implicit cast expression.
837 * @param expression the expression to cast
838 * @param dest_type the destination type
840 static expression_t *create_implicit_cast(expression_t *expression,
843 type_t *const source_type = expression->base.type;
845 if (source_type == dest_type)
848 return create_cast_expression(expression, dest_type);
851 /** Implements the rules from § 6.5.16.1 */
852 static type_t *semantic_assign(type_t *orig_type_left,
853 const expression_t *const right,
855 const source_position_t *source_position)
857 type_t *const orig_type_right = right->base.type;
858 type_t *const type_left = skip_typeref(orig_type_left);
859 type_t *const type_right = skip_typeref(orig_type_right);
861 if(is_type_pointer(type_left)) {
862 if(is_null_pointer_constant(right)) {
863 return orig_type_left;
864 } else if(is_type_pointer(type_right)) {
865 type_t *points_to_left
866 = skip_typeref(type_left->pointer.points_to);
867 type_t *points_to_right
868 = skip_typeref(type_right->pointer.points_to);
870 /* the left type has all qualifiers from the right type */
871 unsigned missing_qualifiers
872 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
873 if(missing_qualifiers != 0) {
874 errorf(source_position,
875 "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
876 return orig_type_left;
879 points_to_left = get_unqualified_type(points_to_left);
880 points_to_right = get_unqualified_type(points_to_right);
882 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
883 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
884 return orig_type_left;
887 if (!types_compatible(points_to_left, points_to_right)) {
888 warningf(source_position,
889 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
890 orig_type_left, context, right, orig_type_right);
893 return orig_type_left;
894 } else if(is_type_integer(type_right)) {
895 warningf(source_position,
896 "%s makes pointer '%T' from integer '%T' without a cast",
897 context, orig_type_left, orig_type_right);
898 return orig_type_left;
900 } else if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
901 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
902 && is_type_pointer(type_right))) {
903 return orig_type_left;
904 } else if ((is_type_compound(type_left) && is_type_compound(type_right))
905 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
906 type_t *const unqual_type_left = get_unqualified_type(type_left);
907 type_t *const unqual_type_right = get_unqualified_type(type_right);
908 if (types_compatible(unqual_type_left, unqual_type_right)) {
909 return orig_type_left;
911 } else if (is_type_integer(type_left) && is_type_pointer(type_right)) {
912 warningf(source_position,
913 "%s makes integer '%T' from pointer '%T' without a cast",
914 context, orig_type_left, orig_type_right);
915 return orig_type_left;
918 if (!is_type_valid(type_left))
921 if (!is_type_valid(type_right))
922 return orig_type_right;
927 static expression_t *parse_constant_expression(void)
929 /* start parsing at precedence 7 (conditional expression) */
930 expression_t *result = parse_sub_expression(7);
932 if(!is_constant_expression(result)) {
933 errorf(&result->base.source_position, "expression '%E' is not constant\n", result);
939 static expression_t *parse_assignment_expression(void)
941 /* start parsing at precedence 2 (assignment expression) */
942 return parse_sub_expression(2);
945 static type_t *make_global_typedef(const char *name, type_t *type)
947 symbol_t *const symbol = symbol_table_insert(name);
949 declaration_t *const declaration = allocate_declaration_zero();
950 declaration->namespc = NAMESPACE_NORMAL;
951 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
952 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
953 declaration->type = type;
954 declaration->symbol = symbol;
955 declaration->source_position = builtin_source_position;
957 record_declaration(declaration);
959 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, &builtin_source_position);
960 typedef_type->typedeft.declaration = declaration;
965 static string_t parse_string_literals(void)
967 assert(token.type == T_STRING_LITERAL);
968 string_t result = token.v.string;
972 while (token.type == T_STRING_LITERAL) {
973 result = concat_strings(&result, &token.v.string);
980 static const char *gnu_attribute_names[GNU_AK_LAST] = {
981 [GNU_AK_CONST] = "const",
982 [GNU_AK_VOLATILE] = "volatile",
983 [GNU_AK_CDECL] = "cdecl",
984 [GNU_AK_STDCALL] = "stdcall",
985 [GNU_AK_FASTCALL] = "fastcall",
986 [GNU_AK_DEPRECATED] = "deprecated",
987 [GNU_AK_NOINLINE] = "noinline",
988 [GNU_AK_NORETURN] = "noreturn",
989 [GNU_AK_NAKED] = "naked",
990 [GNU_AK_PURE] = "pure",
991 [GNU_AK_ALWAYS_INLINE] = "always_inline",
992 [GNU_AK_MALLOC] = "malloc",
993 [GNU_AK_WEAK] = "weak",
994 [GNU_AK_CONSTRUCTOR] = "constructor",
995 [GNU_AK_DESTRUCTOR] = "destructor",
996 [GNU_AK_NOTHROW] = "nothrow",
997 [GNU_AK_TRANSPARENT_UNION] = "transparent_union",
998 [GNU_AK_COMMON] = "coommon",
999 [GNU_AK_NOCOMMON] = "nocommon",
1000 [GNU_AK_PACKED] = "packed",
1001 [GNU_AK_SHARED] = "shared",
1002 [GNU_AK_NOTSHARED] = "notshared",
1003 [GNU_AK_USED] = "used",
1004 [GNU_AK_UNUSED] = "unused",
1005 [GNU_AK_NO_INSTRUMENT_FUNCTION] = "no_instrument_function",
1006 [GNU_AK_WARN_UNUSED_RESULT] = "warn_unused_result",
1007 [GNU_AK_LONGCALL] = "longcall",
1008 [GNU_AK_SHORTCALL] = "shortcall",
1009 [GNU_AK_LONG_CALL] = "long_call",
1010 [GNU_AK_SHORT_CALL] = "short_call",
1011 [GNU_AK_FUNCTION_VECTOR] = "function_vector",
1012 [GNU_AK_INTERRUPT] = "interrupt",
1013 [GNU_AK_INTERRUPT_HANDLER] = "interrupt_handler",
1014 [GNU_AK_NMI_HANDLER] = "nmi_handler",
1015 [GNU_AK_NESTING] = "nesting",
1016 [GNU_AK_NEAR] = "near",
1017 [GNU_AK_FAR] = "far",
1018 [GNU_AK_SIGNAL] = "signal",
1019 [GNU_AK_EIGTHBIT_DATA] = "eightbit_data",
1020 [GNU_AK_TINY_DATA] = "tiny_data",
1021 [GNU_AK_SAVEALL] = "saveall",
1022 [GNU_AK_FLATTEN] = "flatten",
1023 [GNU_AK_SSEREGPARM] = "sseregparm",
1024 [GNU_AK_EXTERNALLY_VISIBLE] = "externally_visible",
1025 [GNU_AK_RETURN_TWICE] = "return_twice",
1026 [GNU_AK_MAY_ALIAS] = "may_alias",
1027 [GNU_AK_MS_STRUCT] = "ms_struct",
1028 [GNU_AK_GCC_STRUCT] = "gcc_struct",
1029 [GNU_AK_DLLIMPORT] = "dllimport",
1030 [GNU_AK_DLLEXPORT] = "dllexport",
1031 [GNU_AK_ALIGNED] = "aligned",
1032 [GNU_AK_ALIAS] = "alias",
1033 [GNU_AK_SECTION] = "section",
1034 [GNU_AK_FORMAT] = "format",
1035 [GNU_AK_FORMAT_ARG] = "format_arg",
1036 [GNU_AK_WEAKREF] = "weakref",
1037 [GNU_AK_NONNULL] = "nonnull",
1038 [GNU_AK_TLS_MODEL] = "tls_model",
1039 [GNU_AK_VISIBILITY] = "visibility",
1040 [GNU_AK_REGPARM] = "regparm",
1041 [GNU_AK_MODEL] = "model",
1042 [GNU_AK_TRAP_EXIT] = "trap_exit",
1043 [GNU_AK_SP_SWITCH] = "sp_switch",
1044 [GNU_AK_SENTINEL] = "sentinel"
1048 * compare two string, ignoring double underscores on the second.
1050 static int strcmp_underscore(const char *s1, const char *s2) {
1051 if(s2[0] == '_' && s2[1] == '_') {
1053 size_t l1 = strlen(s1);
1054 if(l1 + 2 != strlen(s2)) {
1058 return strncmp(s1, s2, l1);
1060 return strcmp(s1, s2);
1064 * Allocate a new gnu temporal attribute.
1066 static gnu_attribute_t *allocate_gnu_attribute(gnu_attribute_kind_t kind) {
1067 gnu_attribute_t *attribute = obstack_alloc(&temp_obst, sizeof(*attribute));
1068 attribute->kind = kind;
1069 attribute->next = NULL;
1070 attribute->invalid = false;
1071 attribute->have_arguments = false;
1075 * parse one constant expression argument.
1077 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1078 expression_t *expression;
1079 add_anchor_token(')');
1080 expression = parse_constant_expression();
1081 rem_anchor_token(')');
1086 attribute->invalid = true;
1090 * parse a list of constant expressions arguments.
1092 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1093 expression_t *expression;
1094 add_anchor_token(')');
1095 add_anchor_token(',');
1097 expression = parse_constant_expression();
1098 if(token.type != ',')
1102 rem_anchor_token(',');
1103 rem_anchor_token(')');
1108 attribute->invalid = true;
1112 * parse one string literal argument.
1114 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute, string_t *string) {
1115 add_anchor_token('(');
1116 if(token.type != T_STRING_LITERAL) {
1117 parse_error_expected("while parsing attribute directive", T_STRING_LITERAL);
1120 *string = parse_string_literals();
1121 rem_anchor_token('(');
1125 attribute->invalid = true;
1129 * parse one tls model.
1131 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1132 static const char *tls_models[] = {
1138 string_t string = { NULL, 0 };
1139 parse_gnu_attribute_string_arg(attribute, &string);
1140 if(string.begin != NULL) {
1141 for(size_t i = 0; i < 4; ++i) {
1142 if(strcmp(tls_models[i], string.begin) == 0) {
1143 attribute->u.value = i;
1148 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1149 attribute->invalid = true;
1153 * parse one tls model.
1155 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1156 static const char *visibilities[] = {
1162 string_t string = { NULL, 0 };
1163 parse_gnu_attribute_string_arg(attribute, &string);
1164 if(string.begin != NULL) {
1165 for(size_t i = 0; i < 4; ++i) {
1166 if(strcmp(visibilities[i], string.begin) == 0) {
1167 attribute->u.value = i;
1172 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1173 attribute->invalid = true;
1177 * parse one (code) model.
1179 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1180 static const char *visibilities[] = {
1185 string_t string = { NULL, 0 };
1186 parse_gnu_attribute_string_arg(attribute, &string);
1187 if(string.begin != NULL) {
1188 for(int i = 0; i < 3; ++i) {
1189 if(strcmp(visibilities[i], string.begin) == 0) {
1190 attribute->u.value = i;
1195 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1196 attribute->invalid = true;
1200 * parse one interrupt argument.
1202 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1203 static const char *interrupts[] = {
1210 string_t string = { NULL, 0 };
1211 parse_gnu_attribute_string_arg(attribute, &string);
1212 if(string.begin != NULL) {
1213 for(size_t i = 0; i < 5; ++i) {
1214 if(strcmp(interrupts[i], string.begin) == 0) {
1215 attribute->u.value = i;
1220 errorf(HERE, "'%s' is an interrupt", string.begin);
1221 attribute->invalid = true;
1225 * parse ( identifier, const expression, const expression )
1227 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1228 static const char *format_names[] = {
1236 if(token.type != T_IDENTIFIER) {
1237 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER);
1240 const char *name = token.v.symbol->string;
1241 for(i = 0; i < 4; ++i) {
1242 if(strcmp_underscore(format_names[i], name) == 0)
1246 if(warning.attribute)
1247 warningf(HERE, "'%s' is an unrecognized format function type", name);
1252 add_anchor_token(')');
1253 add_anchor_token(',');
1254 parse_constant_expression();
1255 rem_anchor_token(',');
1256 rem_anchor_token('(');
1259 add_anchor_token(')');
1260 parse_constant_expression();
1261 rem_anchor_token('(');
1265 attribute->u.value = true;
1269 * Parse one GNU attribute.
1271 * Note that attribute names can be specified WITH or WITHOUT
1272 * double underscores, ie const or __const__.
1274 * The following attributes are parsed without arguments
1299 * no_instrument_function
1300 * warn_unused_result
1317 * externally_visible
1325 * The following attributes are parsed with arguments
1326 * aligned( const expression )
1327 * alias( string literal )
1328 * section( string literal )
1329 * format( identifier, const expression, const expression )
1330 * format_arg( const expression )
1331 * tls_model( string literal )
1332 * visibility( string literal )
1333 * regparm( const expression )
1334 * model( string leteral )
1335 * trap_exit( const expression )
1336 * sp_switch( string literal )
1338 * The following attributes might have arguments
1339 * weak_ref( string literal )
1340 * non_null( const expression // ',' )
1341 * interrupt( string literal )
1342 * sentinel( constant expression )
1344 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1346 gnu_attribute_t *head = *attributes;
1347 gnu_attribute_t *last = *attributes;
1348 gnu_attribute_t *attribute;
1350 eat(T___attribute__);
1354 if(token.type != ')') {
1355 /* find the end of the list */
1357 while(last->next != NULL)
1361 /* non-empty attribute list */
1364 if(token.type == T_const) {
1366 } else if(token.type == T_volatile) {
1368 } else if(token.type == T_cdecl) {
1369 /* __attribute__((cdecl)), WITH ms mode */
1371 } else if(token.type != T_IDENTIFIER) {
1372 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER);
1375 const symbol_t *sym = token.v.symbol;
1380 for(i = 0; i < GNU_AK_LAST; ++i) {
1381 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1384 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1387 if(kind == GNU_AK_LAST) {
1388 if(warning.attribute)
1389 warningf(HERE, "'%s' attribute directive ignored", name);
1391 /* skip possible arguments */
1392 if(token.type == '(') {
1393 eat_until_matching_token(')');
1396 /* check for arguments */
1397 attribute = allocate_gnu_attribute(kind);
1398 if(token.type == '(') {
1400 if(token.type == ')') {
1401 /* empty args are allowed */
1404 attribute->have_arguments = true;
1409 case GNU_AK_VOLATILE:
1411 case GNU_AK_STDCALL:
1412 case GNU_AK_FASTCALL:
1413 case GNU_AK_DEPRECATED:
1414 case GNU_AK_NOINLINE:
1415 case GNU_AK_NORETURN:
1418 case GNU_AK_ALWAYS_INLINE:
1421 case GNU_AK_CONSTRUCTOR:
1422 case GNU_AK_DESTRUCTOR:
1423 case GNU_AK_NOTHROW:
1424 case GNU_AK_TRANSPARENT_UNION:
1426 case GNU_AK_NOCOMMON:
1429 case GNU_AK_NOTSHARED:
1432 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1433 case GNU_AK_WARN_UNUSED_RESULT:
1434 case GNU_AK_LONGCALL:
1435 case GNU_AK_SHORTCALL:
1436 case GNU_AK_LONG_CALL:
1437 case GNU_AK_SHORT_CALL:
1438 case GNU_AK_FUNCTION_VECTOR:
1439 case GNU_AK_INTERRUPT_HANDLER:
1440 case GNU_AK_NMI_HANDLER:
1441 case GNU_AK_NESTING:
1445 case GNU_AK_EIGTHBIT_DATA:
1446 case GNU_AK_TINY_DATA:
1447 case GNU_AK_SAVEALL:
1448 case GNU_AK_FLATTEN:
1449 case GNU_AK_SSEREGPARM:
1450 case GNU_AK_EXTERNALLY_VISIBLE:
1451 case GNU_AK_RETURN_TWICE:
1452 case GNU_AK_MAY_ALIAS:
1453 case GNU_AK_MS_STRUCT:
1454 case GNU_AK_GCC_STRUCT:
1455 case GNU_AK_DLLIMPORT:
1456 case GNU_AK_DLLEXPORT:
1457 if(attribute->have_arguments) {
1458 /* should have no arguments */
1459 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1460 eat_until_matching_token('(');
1461 /* we have already consumed '(', so we stop before ')', eat it */
1463 attribute->invalid = true;
1467 case GNU_AK_ALIGNED:
1468 case GNU_AK_FORMAT_ARG:
1469 case GNU_AK_REGPARM:
1470 case GNU_AK_TRAP_EXIT:
1471 if(!attribute->have_arguments) {
1472 /* should have arguments */
1473 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1474 attribute->invalid = true;
1476 parse_gnu_attribute_const_arg(attribute);
1479 case GNU_AK_SECTION:
1480 case GNU_AK_SP_SWITCH:
1481 if(!attribute->have_arguments) {
1482 /* should have arguments */
1483 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1484 attribute->invalid = true;
1486 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1489 if(!attribute->have_arguments) {
1490 /* should have arguments */
1491 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1492 attribute->invalid = true;
1494 parse_gnu_attribute_format_args(attribute);
1496 case GNU_AK_WEAKREF:
1497 /* may have one string argument */
1498 if(attribute->have_arguments)
1499 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1501 case GNU_AK_NONNULL:
1502 if(attribute->have_arguments)
1503 parse_gnu_attribute_const_arg_list(attribute);
1505 case GNU_AK_TLS_MODEL:
1506 if(!attribute->have_arguments) {
1507 /* should have arguments */
1508 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1510 parse_gnu_attribute_tls_model_arg(attribute);
1512 case GNU_AK_VISIBILITY:
1513 if(!attribute->have_arguments) {
1514 /* should have arguments */
1515 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1517 parse_gnu_attribute_visibility_arg(attribute);
1520 if(!attribute->have_arguments) {
1521 /* should have arguments */
1522 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1524 parse_gnu_attribute_model_arg(attribute);
1525 case GNU_AK_INTERRUPT:
1526 /* may have one string argument */
1527 if(attribute->have_arguments)
1528 parse_gnu_attribute_interrupt_arg(attribute);
1530 case GNU_AK_SENTINEL:
1531 /* may have one string argument */
1532 if(attribute->have_arguments)
1533 parse_gnu_attribute_const_arg(attribute);
1536 /* already handled */
1540 if(attribute != NULL) {
1542 last->next = attribute;
1545 head = last = attribute;
1549 if(token.type != ',')
1561 * Parse GNU attributes.
1563 static void parse_attributes(gnu_attribute_t **attributes)
1566 switch(token.type) {
1567 case T___attribute__: {
1568 parse_gnu_attribute(attributes);
1574 if(token.type != T_STRING_LITERAL) {
1575 parse_error_expected("while parsing assembler attribute",
1577 eat_until_matching_token('(');
1580 parse_string_literals();
1585 goto attributes_finished;
1589 attributes_finished:
1594 static designator_t *parse_designation(void)
1596 designator_t *result = NULL;
1597 designator_t *last = NULL;
1600 designator_t *designator;
1601 switch(token.type) {
1603 designator = allocate_ast_zero(sizeof(designator[0]));
1604 designator->source_position = token.source_position;
1606 add_anchor_token(']');
1607 designator->array_index = parse_constant_expression();
1608 rem_anchor_token(']');
1612 designator = allocate_ast_zero(sizeof(designator[0]));
1613 designator->source_position = token.source_position;
1615 if(token.type != T_IDENTIFIER) {
1616 parse_error_expected("while parsing designator",
1620 designator->symbol = token.v.symbol;
1628 assert(designator != NULL);
1630 last->next = designator;
1632 result = designator;
1640 static initializer_t *initializer_from_string(array_type_t *type,
1641 const string_t *const string)
1643 /* TODO: check len vs. size of array type */
1646 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1647 initializer->string.string = *string;
1652 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1653 wide_string_t *const string)
1655 /* TODO: check len vs. size of array type */
1658 initializer_t *const initializer =
1659 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1660 initializer->wide_string.string = *string;
1666 * Build an initializer from a given expression.
1668 static initializer_t *initializer_from_expression(type_t *orig_type,
1669 expression_t *expression)
1671 /* TODO check that expression is a constant expression */
1673 /* § 6.7.8.14/15 char array may be initialized by string literals */
1674 type_t *type = skip_typeref(orig_type);
1675 type_t *expr_type_orig = expression->base.type;
1676 type_t *expr_type = skip_typeref(expr_type_orig);
1677 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1678 array_type_t *const array_type = &type->array;
1679 type_t *const element_type = skip_typeref(array_type->element_type);
1681 if (element_type->kind == TYPE_ATOMIC) {
1682 atomic_type_kind_t akind = element_type->atomic.akind;
1683 switch (expression->kind) {
1684 case EXPR_STRING_LITERAL:
1685 if (akind == ATOMIC_TYPE_CHAR
1686 || akind == ATOMIC_TYPE_SCHAR
1687 || akind == ATOMIC_TYPE_UCHAR) {
1688 return initializer_from_string(array_type,
1689 &expression->string.value);
1692 case EXPR_WIDE_STRING_LITERAL: {
1693 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1694 if (get_unqualified_type(element_type) == bare_wchar_type) {
1695 return initializer_from_wide_string(array_type,
1696 &expression->wide_string.value);
1706 type_t *const res_type = semantic_assign(type, expression, "initializer",
1707 &expression->base.source_position);
1708 if (res_type == NULL)
1711 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1712 result->value.value = create_implicit_cast(expression, res_type);
1718 * Checks if a given expression can be used as an constant initializer.
1720 static bool is_initializer_constant(const expression_t *expression)
1722 return is_constant_expression(expression)
1723 || is_address_constant(expression);
1727 * Parses an scalar initializer.
1729 * § 6.7.8.11; eat {} without warning
1731 static initializer_t *parse_scalar_initializer(type_t *type,
1732 bool must_be_constant)
1734 /* there might be extra {} hierarchies */
1736 while(token.type == '{') {
1739 warningf(HERE, "extra curly braces around scalar initializer");
1744 expression_t *expression = parse_assignment_expression();
1745 if(must_be_constant && !is_initializer_constant(expression)) {
1746 errorf(&expression->base.source_position,
1747 "Initialisation expression '%E' is not constant\n",
1751 initializer_t *initializer = initializer_from_expression(type, expression);
1753 if(initializer == NULL) {
1754 errorf(&expression->base.source_position,
1755 "expression '%E' (type '%T') doesn't match expected type '%T'",
1756 expression, expression->base.type, type);
1761 bool additional_warning_displayed = false;
1763 if(token.type == ',') {
1766 if(token.type != '}') {
1767 if(!additional_warning_displayed) {
1768 warningf(HERE, "additional elements in scalar initializer");
1769 additional_warning_displayed = true;
1780 * An entry in the type path.
1782 typedef struct type_path_entry_t type_path_entry_t;
1783 struct type_path_entry_t {
1784 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1786 size_t index; /**< For array types: the current index. */
1787 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1792 * A type path expression a position inside compound or array types.
1794 typedef struct type_path_t type_path_t;
1795 struct type_path_t {
1796 type_path_entry_t *path; /**< An flexible array containing the current path. */
1797 type_t *top_type; /**< type of the element the path points */
1798 size_t max_index; /**< largest index in outermost array */
1802 * Prints a type path for debugging.
1804 static __attribute__((unused)) void debug_print_type_path(
1805 const type_path_t *path)
1807 size_t len = ARR_LEN(path->path);
1809 for(size_t i = 0; i < len; ++i) {
1810 const type_path_entry_t *entry = & path->path[i];
1812 type_t *type = skip_typeref(entry->type);
1813 if(is_type_compound(type)) {
1814 /* in gcc mode structs can have no members */
1815 if(entry->v.compound_entry == NULL) {
1819 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1820 } else if(is_type_array(type)) {
1821 fprintf(stderr, "[%zd]", entry->v.index);
1823 fprintf(stderr, "-INVALID-");
1826 if(path->top_type != NULL) {
1827 fprintf(stderr, " (");
1828 print_type(path->top_type);
1829 fprintf(stderr, ")");
1834 * Return the top type path entry, ie. in a path
1835 * (type).a.b returns the b.
1837 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1839 size_t len = ARR_LEN(path->path);
1841 return &path->path[len-1];
1845 * Enlarge the type path by an (empty) element.
1847 static type_path_entry_t *append_to_type_path(type_path_t *path)
1849 size_t len = ARR_LEN(path->path);
1850 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1852 type_path_entry_t *result = & path->path[len];
1853 memset(result, 0, sizeof(result[0]));
1858 * Descending into a sub-type. Enter the scope of the current
1861 static void descend_into_subtype(type_path_t *path)
1863 type_t *orig_top_type = path->top_type;
1864 type_t *top_type = skip_typeref(orig_top_type);
1866 assert(is_type_compound(top_type) || is_type_array(top_type));
1868 type_path_entry_t *top = append_to_type_path(path);
1869 top->type = top_type;
1871 if(is_type_compound(top_type)) {
1872 declaration_t *declaration = top_type->compound.declaration;
1873 declaration_t *entry = declaration->scope.declarations;
1874 top->v.compound_entry = entry;
1877 path->top_type = entry->type;
1879 path->top_type = NULL;
1882 assert(is_type_array(top_type));
1885 path->top_type = top_type->array.element_type;
1890 * Pop an entry from the given type path, ie. returning from
1891 * (type).a.b to (type).a
1893 static void ascend_from_subtype(type_path_t *path)
1895 type_path_entry_t *top = get_type_path_top(path);
1897 path->top_type = top->type;
1899 size_t len = ARR_LEN(path->path);
1900 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1904 * Pop entries from the given type path until the given
1905 * path level is reached.
1907 static void ascend_to(type_path_t *path, size_t top_path_level)
1909 size_t len = ARR_LEN(path->path);
1911 while(len > top_path_level) {
1912 ascend_from_subtype(path);
1913 len = ARR_LEN(path->path);
1917 static bool walk_designator(type_path_t *path, const designator_t *designator,
1918 bool used_in_offsetof)
1920 for( ; designator != NULL; designator = designator->next) {
1921 type_path_entry_t *top = get_type_path_top(path);
1922 type_t *orig_type = top->type;
1924 type_t *type = skip_typeref(orig_type);
1926 if(designator->symbol != NULL) {
1927 symbol_t *symbol = designator->symbol;
1928 if(!is_type_compound(type)) {
1929 if(is_type_valid(type)) {
1930 errorf(&designator->source_position,
1931 "'.%Y' designator used for non-compound type '%T'",
1937 declaration_t *declaration = type->compound.declaration;
1938 declaration_t *iter = declaration->scope.declarations;
1939 for( ; iter != NULL; iter = iter->next) {
1940 if(iter->symbol == symbol) {
1945 errorf(&designator->source_position,
1946 "'%T' has no member named '%Y'", orig_type, symbol);
1949 if(used_in_offsetof) {
1950 type_t *real_type = skip_typeref(iter->type);
1951 if(real_type->kind == TYPE_BITFIELD) {
1952 errorf(&designator->source_position,
1953 "offsetof designator '%Y' may not specify bitfield",
1959 top->type = orig_type;
1960 top->v.compound_entry = iter;
1961 orig_type = iter->type;
1963 expression_t *array_index = designator->array_index;
1964 assert(designator->array_index != NULL);
1966 if(!is_type_array(type)) {
1967 if(is_type_valid(type)) {
1968 errorf(&designator->source_position,
1969 "[%E] designator used for non-array type '%T'",
1970 array_index, orig_type);
1974 if(!is_type_valid(array_index->base.type)) {
1978 long index = fold_constant(array_index);
1979 if(!used_in_offsetof) {
1981 errorf(&designator->source_position,
1982 "array index [%E] must be positive", array_index);
1985 if(type->array.size_constant == true) {
1986 long array_size = type->array.size;
1987 if(index >= array_size) {
1988 errorf(&designator->source_position,
1989 "designator [%E] (%d) exceeds array size %d",
1990 array_index, index, array_size);
1996 top->type = orig_type;
1997 top->v.index = (size_t) index;
1998 orig_type = type->array.element_type;
2000 path->top_type = orig_type;
2002 if(designator->next != NULL) {
2003 descend_into_subtype(path);
2012 static void advance_current_object(type_path_t *path, size_t top_path_level)
2014 type_path_entry_t *top = get_type_path_top(path);
2016 type_t *type = skip_typeref(top->type);
2017 if(is_type_union(type)) {
2018 /* in unions only the first element is initialized */
2019 top->v.compound_entry = NULL;
2020 } else if(is_type_struct(type)) {
2021 declaration_t *entry = top->v.compound_entry;
2023 entry = entry->next;
2024 top->v.compound_entry = entry;
2026 path->top_type = entry->type;
2030 assert(is_type_array(type));
2034 if(!type->array.size_constant || top->v.index < type->array.size) {
2039 /* we're past the last member of the current sub-aggregate, try if we
2040 * can ascend in the type hierarchy and continue with another subobject */
2041 size_t len = ARR_LEN(path->path);
2043 if(len > top_path_level) {
2044 ascend_from_subtype(path);
2045 advance_current_object(path, top_path_level);
2047 path->top_type = NULL;
2052 * skip until token is found.
2054 static void skip_until(int type) {
2055 while(token.type != type) {
2056 if(token.type == T_EOF)
2063 * skip any {...} blocks until a closing braket is reached.
2065 static void skip_initializers(void)
2067 if(token.type == '{')
2070 while(token.type != '}') {
2071 if(token.type == T_EOF)
2073 if(token.type == '{') {
2081 static initializer_t *create_empty_initializer(void)
2083 static initializer_t empty_initializer
2084 = { .list = { { INITIALIZER_LIST }, 0 } };
2085 return &empty_initializer;
2089 * Parse a part of an initialiser for a struct or union,
2091 static initializer_t *parse_sub_initializer(type_path_t *path,
2092 type_t *outer_type, size_t top_path_level,
2093 parse_initializer_env_t *env)
2095 if(token.type == '}') {
2096 /* empty initializer */
2097 return create_empty_initializer();
2100 type_t *orig_type = path->top_type;
2101 type_t *type = NULL;
2103 if (orig_type == NULL) {
2104 /* We are initializing an empty compound. */
2106 type = skip_typeref(orig_type);
2108 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2109 * initializers in this case. */
2110 if(!is_type_valid(type)) {
2111 skip_initializers();
2112 return create_empty_initializer();
2116 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2119 designator_t *designator = NULL;
2120 if(token.type == '.' || token.type == '[') {
2121 designator = parse_designation();
2123 /* reset path to toplevel, evaluate designator from there */
2124 ascend_to(path, top_path_level);
2125 if(!walk_designator(path, designator, false)) {
2126 /* can't continue after designation error */
2130 initializer_t *designator_initializer
2131 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2132 designator_initializer->designator.designator = designator;
2133 ARR_APP1(initializer_t*, initializers, designator_initializer);
2138 if(token.type == '{') {
2139 if(type != NULL && is_type_scalar(type)) {
2140 sub = parse_scalar_initializer(type, env->must_be_constant);
2144 if (env->declaration != NULL)
2145 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2146 env->declaration->symbol);
2148 errorf(HERE, "extra brace group at end of initializer");
2150 descend_into_subtype(path);
2152 add_anchor_token('}');
2153 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2155 rem_anchor_token('}');
2158 ascend_from_subtype(path);
2162 goto error_parse_next;
2166 /* must be an expression */
2167 expression_t *expression = parse_assignment_expression();
2169 if(env->must_be_constant && !is_initializer_constant(expression)) {
2170 errorf(&expression->base.source_position,
2171 "Initialisation expression '%E' is not constant\n",
2176 /* we are already outside, ... */
2180 /* handle { "string" } special case */
2181 if((expression->kind == EXPR_STRING_LITERAL
2182 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2183 && outer_type != NULL) {
2184 sub = initializer_from_expression(outer_type, expression);
2186 if(token.type == ',') {
2189 if(token.type != '}') {
2190 warningf(HERE, "excessive elements in initializer for type '%T'",
2193 /* TODO: eat , ... */
2198 /* descend into subtypes until expression matches type */
2200 orig_type = path->top_type;
2201 type = skip_typeref(orig_type);
2203 sub = initializer_from_expression(orig_type, expression);
2207 if(!is_type_valid(type)) {
2210 if(is_type_scalar(type)) {
2211 errorf(&expression->base.source_position,
2212 "expression '%E' doesn't match expected type '%T'",
2213 expression, orig_type);
2217 descend_into_subtype(path);
2221 /* update largest index of top array */
2222 const type_path_entry_t *first = &path->path[0];
2223 type_t *first_type = first->type;
2224 first_type = skip_typeref(first_type);
2225 if(is_type_array(first_type)) {
2226 size_t index = first->v.index;
2227 if(index > path->max_index)
2228 path->max_index = index;
2232 /* append to initializers list */
2233 ARR_APP1(initializer_t*, initializers, sub);
2236 if(env->declaration != NULL)
2237 warningf(HERE, "excess elements in struct initializer for '%Y'",
2238 env->declaration->symbol);
2240 warningf(HERE, "excess elements in struct initializer");
2244 if(token.type == '}') {
2248 if(token.type == '}') {
2253 /* advance to the next declaration if we are not at the end */
2254 advance_current_object(path, top_path_level);
2255 orig_type = path->top_type;
2256 if(orig_type != NULL)
2257 type = skip_typeref(orig_type);
2263 size_t len = ARR_LEN(initializers);
2264 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2265 initializer_t *result = allocate_ast_zero(size);
2266 result->kind = INITIALIZER_LIST;
2267 result->list.len = len;
2268 memcpy(&result->list.initializers, initializers,
2269 len * sizeof(initializers[0]));
2271 DEL_ARR_F(initializers);
2272 ascend_to(path, top_path_level);
2277 skip_initializers();
2278 DEL_ARR_F(initializers);
2279 ascend_to(path, top_path_level);
2284 * Parses an initializer. Parsers either a compound literal
2285 * (env->declaration == NULL) or an initializer of a declaration.
2287 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2289 type_t *type = skip_typeref(env->type);
2290 initializer_t *result = NULL;
2293 if(is_type_scalar(type)) {
2294 result = parse_scalar_initializer(type, env->must_be_constant);
2295 } else if(token.type == '{') {
2299 memset(&path, 0, sizeof(path));
2300 path.top_type = env->type;
2301 path.path = NEW_ARR_F(type_path_entry_t, 0);
2303 descend_into_subtype(&path);
2305 add_anchor_token('}');
2306 result = parse_sub_initializer(&path, env->type, 1, env);
2307 rem_anchor_token('}');
2309 max_index = path.max_index;
2310 DEL_ARR_F(path.path);
2314 /* parse_scalar_initializer() also works in this case: we simply
2315 * have an expression without {} around it */
2316 result = parse_scalar_initializer(type, env->must_be_constant);
2319 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2320 * the array type size */
2321 if(is_type_array(type) && type->array.size_expression == NULL
2322 && result != NULL) {
2324 switch (result->kind) {
2325 case INITIALIZER_LIST:
2326 size = max_index + 1;
2329 case INITIALIZER_STRING:
2330 size = result->string.string.size;
2333 case INITIALIZER_WIDE_STRING:
2334 size = result->wide_string.string.size;
2338 internal_errorf(HERE, "invalid initializer type");
2341 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2342 cnst->base.type = type_size_t;
2343 cnst->conste.v.int_value = size;
2345 type_t *new_type = duplicate_type(type);
2347 new_type->array.size_expression = cnst;
2348 new_type->array.size_constant = true;
2349 new_type->array.size = size;
2350 env->type = new_type;
2358 static declaration_t *append_declaration(declaration_t *declaration);
2360 static declaration_t *parse_compound_type_specifier(bool is_struct)
2362 gnu_attribute_t *attributes = NULL;
2369 symbol_t *symbol = NULL;
2370 declaration_t *declaration = NULL;
2372 if (token.type == T___attribute__) {
2373 parse_attributes(&attributes);
2376 if(token.type == T_IDENTIFIER) {
2377 symbol = token.v.symbol;
2381 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2383 declaration = get_declaration(symbol, NAMESPACE_UNION);
2385 } else if(token.type != '{') {
2387 parse_error_expected("while parsing struct type specifier",
2388 T_IDENTIFIER, '{', 0);
2390 parse_error_expected("while parsing union type specifier",
2391 T_IDENTIFIER, '{', 0);
2397 if(declaration == NULL) {
2398 declaration = allocate_declaration_zero();
2399 declaration->namespc =
2400 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2401 declaration->source_position = token.source_position;
2402 declaration->symbol = symbol;
2403 declaration->parent_scope = scope;
2404 if (symbol != NULL) {
2405 environment_push(declaration);
2407 append_declaration(declaration);
2410 if(token.type == '{') {
2411 if(declaration->init.is_defined) {
2412 assert(symbol != NULL);
2413 errorf(HERE, "multiple definitions of '%s %Y'",
2414 is_struct ? "struct" : "union", symbol);
2415 declaration->scope.declarations = NULL;
2417 declaration->init.is_defined = true;
2419 parse_compound_type_entries(declaration);
2420 parse_attributes(&attributes);
2426 static void parse_enum_entries(type_t *const enum_type)
2430 if(token.type == '}') {
2432 errorf(HERE, "empty enum not allowed");
2436 add_anchor_token('}');
2438 if(token.type != T_IDENTIFIER) {
2439 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
2441 rem_anchor_token('}');
2445 declaration_t *const entry = allocate_declaration_zero();
2446 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2447 entry->type = enum_type;
2448 entry->symbol = token.v.symbol;
2449 entry->source_position = token.source_position;
2452 if(token.type == '=') {
2454 expression_t *value = parse_constant_expression();
2456 value = create_implicit_cast(value, enum_type);
2457 entry->init.enum_value = value;
2462 record_declaration(entry);
2464 if(token.type != ',')
2467 } while(token.type != '}');
2468 rem_anchor_token('}');
2476 static type_t *parse_enum_specifier(void)
2478 gnu_attribute_t *attributes = NULL;
2479 declaration_t *declaration;
2483 if(token.type == T_IDENTIFIER) {
2484 symbol = token.v.symbol;
2487 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2488 } else if(token.type != '{') {
2489 parse_error_expected("while parsing enum type specifier",
2490 T_IDENTIFIER, '{', 0);
2497 if(declaration == NULL) {
2498 declaration = allocate_declaration_zero();
2499 declaration->namespc = NAMESPACE_ENUM;
2500 declaration->source_position = token.source_position;
2501 declaration->symbol = symbol;
2502 declaration->parent_scope = scope;
2505 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2506 type->enumt.declaration = declaration;
2508 if(token.type == '{') {
2509 if(declaration->init.is_defined) {
2510 errorf(HERE, "multiple definitions of enum %Y", symbol);
2512 if (symbol != NULL) {
2513 environment_push(declaration);
2515 append_declaration(declaration);
2516 declaration->init.is_defined = 1;
2518 parse_enum_entries(type);
2519 parse_attributes(&attributes);
2526 * if a symbol is a typedef to another type, return true
2528 static bool is_typedef_symbol(symbol_t *symbol)
2530 const declaration_t *const declaration =
2531 get_declaration(symbol, NAMESPACE_NORMAL);
2533 declaration != NULL &&
2534 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2537 static type_t *parse_typeof(void)
2544 add_anchor_token(')');
2546 expression_t *expression = NULL;
2549 switch(token.type) {
2550 case T___extension__:
2551 /* this can be a prefix to a typename or an expression */
2552 /* we simply eat it now. */
2555 } while(token.type == T___extension__);
2559 if(is_typedef_symbol(token.v.symbol)) {
2560 type = parse_typename();
2562 expression = parse_expression();
2563 type = expression->base.type;
2568 type = parse_typename();
2572 expression = parse_expression();
2573 type = expression->base.type;
2577 rem_anchor_token(')');
2580 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2581 typeof_type->typeoft.expression = expression;
2582 typeof_type->typeoft.typeof_type = type;
2590 SPECIFIER_SIGNED = 1 << 0,
2591 SPECIFIER_UNSIGNED = 1 << 1,
2592 SPECIFIER_LONG = 1 << 2,
2593 SPECIFIER_INT = 1 << 3,
2594 SPECIFIER_DOUBLE = 1 << 4,
2595 SPECIFIER_CHAR = 1 << 5,
2596 SPECIFIER_SHORT = 1 << 6,
2597 SPECIFIER_LONG_LONG = 1 << 7,
2598 SPECIFIER_FLOAT = 1 << 8,
2599 SPECIFIER_BOOL = 1 << 9,
2600 SPECIFIER_VOID = 1 << 10,
2601 SPECIFIER_INT8 = 1 << 11,
2602 SPECIFIER_INT16 = 1 << 12,
2603 SPECIFIER_INT32 = 1 << 13,
2604 SPECIFIER_INT64 = 1 << 14,
2605 SPECIFIER_INT128 = 1 << 15,
2606 #ifdef PROVIDE_COMPLEX
2607 SPECIFIER_COMPLEX = 1 << 16,
2608 SPECIFIER_IMAGINARY = 1 << 17,
2612 static type_t *create_builtin_type(symbol_t *const symbol,
2613 type_t *const real_type)
2615 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2616 type->builtin.symbol = symbol;
2617 type->builtin.real_type = real_type;
2619 type_t *result = typehash_insert(type);
2620 if (type != result) {
2627 static type_t *get_typedef_type(symbol_t *symbol)
2629 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2630 if(declaration == NULL
2631 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2634 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2635 type->typedeft.declaration = declaration;
2641 * check for the allowed MS alignment values.
2643 static bool check_elignment_value(long long intvalue) {
2644 if(intvalue < 1 || intvalue > 8192) {
2645 errorf(HERE, "illegal alignment value");
2648 unsigned v = (unsigned)intvalue;
2649 for(unsigned i = 1; i <= 8192; i += i) {
2653 errorf(HERE, "alignment must be power of two");
2657 #define DET_MOD(name, tag) do { \
2658 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2659 *modifiers |= tag; \
2662 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2664 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2667 if(token.type == T_restrict) {
2669 DET_MOD(restrict, DM_RESTRICT);
2671 } else if(token.type != T_IDENTIFIER)
2673 symbol_t *symbol = token.v.symbol;
2674 if(symbol == sym_align) {
2677 if(token.type != T_INTEGER)
2679 if(check_elignment_value(token.v.intvalue)) {
2680 if(specifiers->alignment != 0)
2681 warningf(HERE, "align used more than once");
2682 specifiers->alignment = (unsigned char)token.v.intvalue;
2686 } else if(symbol == sym_allocate) {
2689 if(token.type != T_IDENTIFIER)
2691 (void)token.v.symbol;
2693 } else if(symbol == sym_dllimport) {
2695 DET_MOD(dllimport, DM_DLLIMPORT);
2696 } else if(symbol == sym_dllexport) {
2698 DET_MOD(dllexport, DM_DLLEXPORT);
2699 } else if(symbol == sym_thread) {
2701 DET_MOD(thread, DM_THREAD);
2702 } else if(symbol == sym_naked) {
2704 DET_MOD(naked, DM_NAKED);
2705 } else if(symbol == sym_noinline) {
2707 DET_MOD(noinline, DM_NOINLINE);
2708 } else if(symbol == sym_noreturn) {
2710 DET_MOD(noreturn, DM_NORETURN);
2711 } else if(symbol == sym_nothrow) {
2713 DET_MOD(nothrow, DM_NOTHROW);
2714 } else if(symbol == sym_novtable) {
2716 DET_MOD(novtable, DM_NOVTABLE);
2717 } else if(symbol == sym_property) {
2721 bool is_get = false;
2722 if(token.type != T_IDENTIFIER)
2724 if(token.v.symbol == sym_get) {
2726 } else if(token.v.symbol == sym_put) {
2728 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2733 if(token.type != T_IDENTIFIER)
2736 if(specifiers->get_property_sym != NULL) {
2737 errorf(HERE, "get property name already specified");
2739 specifiers->get_property_sym = token.v.symbol;
2742 if(specifiers->put_property_sym != NULL) {
2743 errorf(HERE, "put property name already specified");
2745 specifiers->put_property_sym = token.v.symbol;
2749 if(token.type == ',') {
2756 } else if(symbol == sym_selectany) {
2758 DET_MOD(selectany, DM_SELECTANY);
2759 } else if(symbol == sym_uuid) {
2762 if(token.type != T_STRING_LITERAL)
2766 } else if(symbol == sym_deprecated) {
2768 if(specifiers->deprecated != 0)
2769 warningf(HERE, "deprecated used more than once");
2770 specifiers->deprecated = 1;
2771 if(token.type == '(') {
2773 if(token.type == T_STRING_LITERAL) {
2774 specifiers->deprecated_string = token.v.string.begin;
2777 errorf(HERE, "string literal expected");
2781 } else if(symbol == sym_noalias) {
2783 DET_MOD(noalias, DM_NOALIAS);
2785 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2787 if(token.type == '(')
2791 if (token.type == ',')
2798 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2800 type_t *type = NULL;
2801 unsigned type_qualifiers = 0;
2802 unsigned type_specifiers = 0;
2805 specifiers->source_position = token.source_position;
2808 switch(token.type) {
2811 #define MATCH_STORAGE_CLASS(token, class) \
2813 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2814 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2816 specifiers->declared_storage_class = class; \
2820 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2821 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2822 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2823 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2824 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2829 add_anchor_token(')');
2830 parse_microsoft_extended_decl_modifier(specifiers);
2831 rem_anchor_token(')');
2836 switch (specifiers->declared_storage_class) {
2837 case STORAGE_CLASS_NONE:
2838 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2841 case STORAGE_CLASS_EXTERN:
2842 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2845 case STORAGE_CLASS_STATIC:
2846 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2850 errorf(HERE, "multiple storage classes in declaration specifiers");
2856 /* type qualifiers */
2857 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2859 type_qualifiers |= qualifier; \
2863 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2864 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2865 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2866 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2867 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2868 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2869 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2870 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2872 case T___extension__:
2877 /* type specifiers */
2878 #define MATCH_SPECIFIER(token, specifier, name) \
2881 if(type_specifiers & specifier) { \
2882 errorf(HERE, "multiple " name " type specifiers given"); \
2884 type_specifiers |= specifier; \
2888 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2889 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2890 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2891 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2892 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2893 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2894 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2895 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2896 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2897 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2898 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2899 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2900 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2901 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2902 #ifdef PROVIDE_COMPLEX
2903 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2904 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2906 case T__forceinline:
2907 /* only in microsoft mode */
2908 specifiers->decl_modifiers |= DM_FORCEINLINE;
2912 specifiers->is_inline = true;
2917 if(type_specifiers & SPECIFIER_LONG_LONG) {
2918 errorf(HERE, "multiple type specifiers given");
2919 } else if(type_specifiers & SPECIFIER_LONG) {
2920 type_specifiers |= SPECIFIER_LONG_LONG;
2922 type_specifiers |= SPECIFIER_LONG;
2927 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2929 type->compound.declaration = parse_compound_type_specifier(true);
2933 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2935 type->compound.declaration = parse_compound_type_specifier(false);
2939 type = parse_enum_specifier();
2942 type = parse_typeof();
2944 case T___builtin_va_list:
2945 type = duplicate_type(type_valist);
2949 case T___attribute__:
2950 parse_attributes(&specifiers->gnu_attributes);
2953 case T_IDENTIFIER: {
2954 /* only parse identifier if we haven't found a type yet */
2955 if(type != NULL || type_specifiers != 0)
2956 goto finish_specifiers;
2958 type_t *typedef_type = get_typedef_type(token.v.symbol);
2960 if(typedef_type == NULL)
2961 goto finish_specifiers;
2964 type = typedef_type;
2968 /* function specifier */
2970 goto finish_specifiers;
2977 atomic_type_kind_t atomic_type;
2979 /* match valid basic types */
2980 switch(type_specifiers) {
2981 case SPECIFIER_VOID:
2982 atomic_type = ATOMIC_TYPE_VOID;
2984 case SPECIFIER_CHAR:
2985 atomic_type = ATOMIC_TYPE_CHAR;
2987 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2988 atomic_type = ATOMIC_TYPE_SCHAR;
2990 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2991 atomic_type = ATOMIC_TYPE_UCHAR;
2993 case SPECIFIER_SHORT:
2994 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2995 case SPECIFIER_SHORT | SPECIFIER_INT:
2996 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2997 atomic_type = ATOMIC_TYPE_SHORT;
2999 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3000 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3001 atomic_type = ATOMIC_TYPE_USHORT;
3004 case SPECIFIER_SIGNED:
3005 case SPECIFIER_SIGNED | SPECIFIER_INT:
3006 atomic_type = ATOMIC_TYPE_INT;
3008 case SPECIFIER_UNSIGNED:
3009 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3010 atomic_type = ATOMIC_TYPE_UINT;
3012 case SPECIFIER_LONG:
3013 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3014 case SPECIFIER_LONG | SPECIFIER_INT:
3015 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3016 atomic_type = ATOMIC_TYPE_LONG;
3018 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3019 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3020 atomic_type = ATOMIC_TYPE_ULONG;
3022 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3023 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3024 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3025 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3027 atomic_type = ATOMIC_TYPE_LONGLONG;
3029 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3030 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3032 atomic_type = ATOMIC_TYPE_ULONGLONG;
3035 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3036 atomic_type = unsigned_int8_type_kind;
3039 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3040 atomic_type = unsigned_int16_type_kind;
3043 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3044 atomic_type = unsigned_int32_type_kind;
3047 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3048 atomic_type = unsigned_int64_type_kind;
3051 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3052 atomic_type = unsigned_int128_type_kind;
3055 case SPECIFIER_INT8:
3056 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3057 atomic_type = int8_type_kind;
3060 case SPECIFIER_INT16:
3061 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3062 atomic_type = int16_type_kind;
3065 case SPECIFIER_INT32:
3066 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3067 atomic_type = int32_type_kind;
3070 case SPECIFIER_INT64:
3071 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3072 atomic_type = int64_type_kind;
3075 case SPECIFIER_INT128:
3076 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3077 atomic_type = int128_type_kind;
3080 case SPECIFIER_FLOAT:
3081 atomic_type = ATOMIC_TYPE_FLOAT;
3083 case SPECIFIER_DOUBLE:
3084 atomic_type = ATOMIC_TYPE_DOUBLE;
3086 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3087 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3089 case SPECIFIER_BOOL:
3090 atomic_type = ATOMIC_TYPE_BOOL;
3092 #ifdef PROVIDE_COMPLEX
3093 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3094 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
3096 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3097 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
3099 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3100 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
3102 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3103 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
3105 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3106 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
3108 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3109 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
3113 /* invalid specifier combination, give an error message */
3114 if(type_specifiers == 0) {
3115 if (! strict_mode) {
3116 if (warning.implicit_int) {
3117 warningf(HERE, "no type specifiers in declaration, using 'int'");
3119 atomic_type = ATOMIC_TYPE_INT;
3122 errorf(HERE, "no type specifiers given in declaration");
3124 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3125 (type_specifiers & SPECIFIER_UNSIGNED)) {
3126 errorf(HERE, "signed and unsigned specifiers gives");
3127 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3128 errorf(HERE, "only integer types can be signed or unsigned");
3130 errorf(HERE, "multiple datatypes in declaration");
3132 atomic_type = ATOMIC_TYPE_INVALID;
3135 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3136 type->atomic.akind = atomic_type;
3139 if(type_specifiers != 0) {
3140 errorf(HERE, "multiple datatypes in declaration");
3144 type->base.qualifiers = type_qualifiers;
3145 /* FIXME: check type qualifiers here */
3147 type_t *result = typehash_insert(type);
3148 if(newtype && result != type) {
3152 specifiers->type = result;
3157 static type_qualifiers_t parse_type_qualifiers(void)
3159 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3162 switch(token.type) {
3163 /* type qualifiers */
3164 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3165 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3166 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3167 /* microsoft extended type modifiers */
3168 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3169 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3170 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3171 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3172 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3175 return type_qualifiers;
3180 static declaration_t *parse_identifier_list(void)
3182 declaration_t *declarations = NULL;
3183 declaration_t *last_declaration = NULL;
3185 declaration_t *const declaration = allocate_declaration_zero();
3186 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3187 declaration->source_position = token.source_position;
3188 declaration->symbol = token.v.symbol;
3191 if(last_declaration != NULL) {
3192 last_declaration->next = declaration;
3194 declarations = declaration;
3196 last_declaration = declaration;
3198 if(token.type != ',')
3201 } while(token.type == T_IDENTIFIER);
3203 return declarations;
3206 static void semantic_parameter(declaration_t *declaration)
3208 /* TODO: improve error messages */
3210 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3211 errorf(HERE, "typedef not allowed in parameter list");
3212 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3213 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3214 errorf(HERE, "parameter may only have none or register storage class");
3217 type_t *const orig_type = declaration->type;
3218 type_t * type = skip_typeref(orig_type);
3220 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3221 * into a pointer. § 6.7.5.3 (7) */
3222 if (is_type_array(type)) {
3223 type_t *const element_type = type->array.element_type;
3225 type = make_pointer_type(element_type, type->base.qualifiers);
3227 declaration->type = type;
3230 if(is_type_incomplete(type)) {
3231 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3232 orig_type, declaration->symbol);
3236 static declaration_t *parse_parameter(void)
3238 declaration_specifiers_t specifiers;
3239 memset(&specifiers, 0, sizeof(specifiers));
3241 parse_declaration_specifiers(&specifiers);
3243 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3245 semantic_parameter(declaration);
3250 static declaration_t *parse_parameters(function_type_t *type)
3252 if(token.type == T_IDENTIFIER) {
3253 symbol_t *symbol = token.v.symbol;
3254 if(!is_typedef_symbol(symbol)) {
3255 type->kr_style_parameters = true;
3256 return parse_identifier_list();
3260 if(token.type == ')') {
3261 type->unspecified_parameters = 1;
3264 if(token.type == T_void && look_ahead(1)->type == ')') {
3269 declaration_t *declarations = NULL;
3270 declaration_t *declaration;
3271 declaration_t *last_declaration = NULL;
3272 function_parameter_t *parameter;
3273 function_parameter_t *last_parameter = NULL;
3276 switch(token.type) {
3280 return declarations;
3283 case T___extension__:
3285 declaration = parse_parameter();
3287 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3288 memset(parameter, 0, sizeof(parameter[0]));
3289 parameter->type = declaration->type;
3291 if(last_parameter != NULL) {
3292 last_declaration->next = declaration;
3293 last_parameter->next = parameter;
3295 type->parameters = parameter;
3296 declarations = declaration;
3298 last_parameter = parameter;
3299 last_declaration = declaration;
3303 return declarations;
3305 if(token.type != ',')
3306 return declarations;
3316 } construct_type_kind_t;
3318 typedef struct construct_type_t construct_type_t;
3319 struct construct_type_t {
3320 construct_type_kind_t kind;
3321 construct_type_t *next;
3324 typedef struct parsed_pointer_t parsed_pointer_t;
3325 struct parsed_pointer_t {
3326 construct_type_t construct_type;
3327 type_qualifiers_t type_qualifiers;
3330 typedef struct construct_function_type_t construct_function_type_t;
3331 struct construct_function_type_t {
3332 construct_type_t construct_type;
3333 type_t *function_type;
3336 typedef struct parsed_array_t parsed_array_t;
3337 struct parsed_array_t {
3338 construct_type_t construct_type;
3339 type_qualifiers_t type_qualifiers;
3345 typedef struct construct_base_type_t construct_base_type_t;
3346 struct construct_base_type_t {
3347 construct_type_t construct_type;
3351 static construct_type_t *parse_pointer_declarator(void)
3355 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3356 memset(pointer, 0, sizeof(pointer[0]));
3357 pointer->construct_type.kind = CONSTRUCT_POINTER;
3358 pointer->type_qualifiers = parse_type_qualifiers();
3360 return (construct_type_t*) pointer;
3363 static construct_type_t *parse_array_declarator(void)
3366 add_anchor_token(']');
3368 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3369 memset(array, 0, sizeof(array[0]));
3370 array->construct_type.kind = CONSTRUCT_ARRAY;
3372 if(token.type == T_static) {
3373 array->is_static = true;
3377 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3378 if(type_qualifiers != 0) {
3379 if(token.type == T_static) {
3380 array->is_static = true;
3384 array->type_qualifiers = type_qualifiers;
3386 if(token.type == '*' && look_ahead(1)->type == ']') {
3387 array->is_variable = true;
3389 } else if(token.type != ']') {
3390 array->size = parse_assignment_expression();
3393 rem_anchor_token(']');
3396 return (construct_type_t*) array;
3401 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3404 add_anchor_token(')');
3407 if(declaration != NULL) {
3408 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3410 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3413 declaration_t *parameters = parse_parameters(&type->function);
3414 if(declaration != NULL) {
3415 declaration->scope.declarations = parameters;
3418 construct_function_type_t *construct_function_type =
3419 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3420 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3421 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3422 construct_function_type->function_type = type;
3424 rem_anchor_token(')');
3428 return (construct_type_t*) construct_function_type;
3431 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3432 bool may_be_abstract)
3434 /* construct a single linked list of construct_type_t's which describe
3435 * how to construct the final declarator type */
3436 construct_type_t *first = NULL;
3437 construct_type_t *last = NULL;
3438 gnu_attribute_t *attributes = NULL;
3441 while(token.type == '*') {
3442 construct_type_t *type = parse_pointer_declarator();
3453 /* TODO: find out if this is correct */
3454 parse_attributes(&attributes);
3456 construct_type_t *inner_types = NULL;
3458 switch(token.type) {
3460 if(declaration == NULL) {
3461 errorf(HERE, "no identifier expected in typename");
3463 declaration->symbol = token.v.symbol;
3464 declaration->source_position = token.source_position;
3470 add_anchor_token(')');
3471 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3472 rem_anchor_token(')');
3478 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
3479 /* avoid a loop in the outermost scope, because eat_statement doesn't
3481 if(token.type == '}' && current_function == NULL) {
3489 construct_type_t *p = last;
3492 construct_type_t *type;
3493 switch(token.type) {
3495 type = parse_function_declarator(declaration);
3498 type = parse_array_declarator();
3501 goto declarator_finished;
3504 /* insert in the middle of the list (behind p) */
3506 type->next = p->next;
3517 declarator_finished:
3518 parse_attributes(&attributes);
3520 /* append inner_types at the end of the list, we don't to set last anymore
3521 * as it's not needed anymore */
3523 assert(first == NULL);
3524 first = inner_types;
3526 last->next = inner_types;
3534 static type_t *construct_declarator_type(construct_type_t *construct_list,
3537 construct_type_t *iter = construct_list;
3538 for( ; iter != NULL; iter = iter->next) {
3539 switch(iter->kind) {
3540 case CONSTRUCT_INVALID:
3541 internal_errorf(HERE, "invalid type construction found");
3542 case CONSTRUCT_FUNCTION: {
3543 construct_function_type_t *construct_function_type
3544 = (construct_function_type_t*) iter;
3546 type_t *function_type = construct_function_type->function_type;
3548 function_type->function.return_type = type;
3550 type_t *skipped_return_type = skip_typeref(type);
3551 if (is_type_function(skipped_return_type)) {
3552 errorf(HERE, "function returning function is not allowed");
3553 type = type_error_type;
3554 } else if (is_type_array(skipped_return_type)) {
3555 errorf(HERE, "function returning array is not allowed");
3556 type = type_error_type;
3558 type = function_type;
3563 case CONSTRUCT_POINTER: {
3564 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3565 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3566 pointer_type->pointer.points_to = type;
3567 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3569 type = pointer_type;
3573 case CONSTRUCT_ARRAY: {
3574 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3575 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3577 expression_t *size_expression = parsed_array->size;
3578 if(size_expression != NULL) {
3580 = create_implicit_cast(size_expression, type_size_t);
3583 array_type->base.qualifiers = parsed_array->type_qualifiers;
3584 array_type->array.element_type = type;
3585 array_type->array.is_static = parsed_array->is_static;
3586 array_type->array.is_variable = parsed_array->is_variable;
3587 array_type->array.size_expression = size_expression;
3589 if(size_expression != NULL) {
3590 if(is_constant_expression(size_expression)) {
3591 array_type->array.size_constant = true;
3592 array_type->array.size
3593 = fold_constant(size_expression);
3595 array_type->array.is_vla = true;
3599 type_t *skipped_type = skip_typeref(type);
3600 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3601 errorf(HERE, "array of void is not allowed");
3602 type = type_error_type;
3610 type_t *hashed_type = typehash_insert(type);
3611 if(hashed_type != type) {
3612 /* the function type was constructed earlier freeing it here will
3613 * destroy other types... */
3614 if(iter->kind != CONSTRUCT_FUNCTION) {
3624 static declaration_t *parse_declarator(
3625 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3627 declaration_t *const declaration = allocate_declaration_zero();
3628 declaration->declared_storage_class = specifiers->declared_storage_class;
3629 declaration->modifiers = specifiers->decl_modifiers;
3630 declaration->deprecated = specifiers->deprecated;
3631 declaration->deprecated_string = specifiers->deprecated_string;
3632 declaration->get_property_sym = specifiers->get_property_sym;
3633 declaration->put_property_sym = specifiers->put_property_sym;
3634 declaration->is_inline = specifiers->is_inline;
3636 declaration->storage_class = specifiers->declared_storage_class;
3637 if(declaration->storage_class == STORAGE_CLASS_NONE
3638 && scope != global_scope) {
3639 declaration->storage_class = STORAGE_CLASS_AUTO;
3642 if(specifiers->alignment != 0) {
3643 /* TODO: add checks here */
3644 declaration->alignment = specifiers->alignment;
3647 construct_type_t *construct_type
3648 = parse_inner_declarator(declaration, may_be_abstract);
3649 type_t *const type = specifiers->type;
3650 declaration->type = construct_declarator_type(construct_type, type);
3652 if(construct_type != NULL) {
3653 obstack_free(&temp_obst, construct_type);
3659 static type_t *parse_abstract_declarator(type_t *base_type)
3661 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3663 type_t *result = construct_declarator_type(construct_type, base_type);
3664 if(construct_type != NULL) {
3665 obstack_free(&temp_obst, construct_type);
3671 static declaration_t *append_declaration(declaration_t* const declaration)
3673 if (last_declaration != NULL) {
3674 last_declaration->next = declaration;
3676 scope->declarations = declaration;
3678 last_declaration = declaration;
3683 * Check if the declaration of main is suspicious. main should be a
3684 * function with external linkage, returning int, taking either zero
3685 * arguments, two, or three arguments of appropriate types, ie.
3687 * int main([ int argc, char **argv [, char **env ] ]).
3689 * @param decl the declaration to check
3690 * @param type the function type of the declaration
3692 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3694 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3695 warningf(&decl->source_position,
3696 "'main' is normally a non-static function");
3698 if (skip_typeref(func_type->return_type) != type_int) {
3699 warningf(&decl->source_position,
3700 "return type of 'main' should be 'int', but is '%T'",
3701 func_type->return_type);
3703 const function_parameter_t *parm = func_type->parameters;
3705 type_t *const first_type = parm->type;
3706 if (!types_compatible(skip_typeref(first_type), type_int)) {
3707 warningf(&decl->source_position,
3708 "first argument of 'main' should be 'int', but is '%T'", first_type);
3712 type_t *const second_type = parm->type;
3713 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3714 warningf(&decl->source_position,
3715 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3719 type_t *const third_type = parm->type;
3720 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3721 warningf(&decl->source_position,
3722 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3726 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3730 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3736 * Check if a symbol is the equal to "main".
3738 static bool is_sym_main(const symbol_t *const sym)
3740 return strcmp(sym->string, "main") == 0;
3743 static declaration_t *internal_record_declaration(
3744 declaration_t *const declaration,
3745 const bool is_function_definition)
3747 const symbol_t *const symbol = declaration->symbol;
3748 const namespace_t namespc = (namespace_t)declaration->namespc;
3750 type_t *const orig_type = declaration->type;
3751 type_t *const type = skip_typeref(orig_type);
3752 if (is_type_function(type) &&
3753 type->function.unspecified_parameters &&
3754 warning.strict_prototypes) {
3755 warningf(&declaration->source_position,
3756 "function declaration '%#T' is not a prototype",
3757 orig_type, declaration->symbol);
3760 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3761 check_type_of_main(declaration, &type->function);
3764 assert(declaration->symbol != NULL);
3765 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3767 assert(declaration != previous_declaration);
3768 if (previous_declaration != NULL) {
3769 if (previous_declaration->parent_scope == scope) {
3770 /* can happen for K&R style declarations */
3771 if(previous_declaration->type == NULL) {
3772 previous_declaration->type = declaration->type;
3775 const type_t *prev_type = skip_typeref(previous_declaration->type);
3776 if (!types_compatible(type, prev_type)) {
3777 errorf(&declaration->source_position,
3778 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3779 orig_type, symbol, previous_declaration->type, symbol,
3780 &previous_declaration->source_position);
3782 unsigned old_storage_class = previous_declaration->storage_class;
3783 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3784 errorf(&declaration->source_position,
3785 "redeclaration of enum entry '%Y' (declared %P)",
3786 symbol, &previous_declaration->source_position);
3787 return previous_declaration;
3790 unsigned new_storage_class = declaration->storage_class;
3792 if(is_type_incomplete(prev_type)) {
3793 previous_declaration->type = type;
3797 /* pretend no storage class means extern for function
3798 * declarations (except if the previous declaration is neither
3799 * none nor extern) */
3800 if (is_type_function(type)) {
3801 switch (old_storage_class) {
3802 case STORAGE_CLASS_NONE:
3803 old_storage_class = STORAGE_CLASS_EXTERN;
3805 case STORAGE_CLASS_EXTERN:
3806 if (is_function_definition) {
3807 if (warning.missing_prototypes &&
3808 prev_type->function.unspecified_parameters &&
3809 !is_sym_main(symbol)) {
3810 warningf(&declaration->source_position,
3811 "no previous prototype for '%#T'",
3814 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3815 new_storage_class = STORAGE_CLASS_EXTERN;
3823 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3824 new_storage_class == STORAGE_CLASS_EXTERN) {
3825 warn_redundant_declaration:
3826 if (warning.redundant_decls) {
3827 warningf(&declaration->source_position,
3828 "redundant declaration for '%Y' (declared %P)",
3829 symbol, &previous_declaration->source_position);
3831 } else if (current_function == NULL) {
3832 if (old_storage_class != STORAGE_CLASS_STATIC &&
3833 new_storage_class == STORAGE_CLASS_STATIC) {
3834 errorf(&declaration->source_position,
3835 "static declaration of '%Y' follows non-static declaration (declared %P)",
3836 symbol, &previous_declaration->source_position);
3838 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3839 goto warn_redundant_declaration;
3841 if (new_storage_class == STORAGE_CLASS_NONE) {
3842 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3843 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3847 if (old_storage_class == new_storage_class) {
3848 errorf(&declaration->source_position,
3849 "redeclaration of '%Y' (declared %P)",
3850 symbol, &previous_declaration->source_position);
3852 errorf(&declaration->source_position,
3853 "redeclaration of '%Y' with different linkage (declared %P)",
3854 symbol, &previous_declaration->source_position);
3858 return previous_declaration;
3860 } else if (is_function_definition) {
3861 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3862 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3863 warningf(&declaration->source_position,
3864 "no previous prototype for '%#T'", orig_type, symbol);
3865 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3866 warningf(&declaration->source_position,
3867 "no previous declaration for '%#T'", orig_type,
3871 } else if (warning.missing_declarations &&
3872 scope == global_scope &&
3873 !is_type_function(type) && (
3874 declaration->storage_class == STORAGE_CLASS_NONE ||
3875 declaration->storage_class == STORAGE_CLASS_THREAD
3877 warningf(&declaration->source_position,
3878 "no previous declaration for '%#T'", orig_type, symbol);
3881 assert(declaration->parent_scope == NULL);
3882 assert(scope != NULL);
3884 declaration->parent_scope = scope;
3886 environment_push(declaration);
3887 return append_declaration(declaration);
3890 static declaration_t *record_declaration(declaration_t *declaration)
3892 return internal_record_declaration(declaration, false);
3895 static declaration_t *record_function_definition(declaration_t *declaration)
3897 return internal_record_declaration(declaration, true);
3900 static void parser_error_multiple_definition(declaration_t *declaration,
3901 const source_position_t *source_position)
3903 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3904 declaration->symbol, &declaration->source_position);
3907 static bool is_declaration_specifier(const token_t *token,
3908 bool only_type_specifiers)
3910 switch(token->type) {
3914 return is_typedef_symbol(token->v.symbol);
3916 case T___extension__:
3919 return !only_type_specifiers;
3926 static void parse_init_declarator_rest(declaration_t *declaration)
3930 type_t *orig_type = declaration->type;
3931 type_t *type = skip_typeref(orig_type);
3933 if(declaration->init.initializer != NULL) {
3934 parser_error_multiple_definition(declaration, HERE);
3937 bool must_be_constant = false;
3938 if(declaration->storage_class == STORAGE_CLASS_STATIC
3939 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3940 || declaration->parent_scope == global_scope) {
3941 must_be_constant = true;
3944 parse_initializer_env_t env;
3945 env.type = orig_type;
3946 env.must_be_constant = must_be_constant;
3947 env.declaration = declaration;
3949 initializer_t *initializer = parse_initializer(&env);
3951 if(env.type != orig_type) {
3952 orig_type = env.type;
3953 type = skip_typeref(orig_type);
3954 declaration->type = env.type;
3957 if(is_type_function(type)) {
3958 errorf(&declaration->source_position,
3959 "initializers not allowed for function types at declator '%Y' (type '%T')",
3960 declaration->symbol, orig_type);
3962 declaration->init.initializer = initializer;
3966 /* parse rest of a declaration without any declarator */
3967 static void parse_anonymous_declaration_rest(
3968 const declaration_specifiers_t *specifiers,
3969 parsed_declaration_func finished_declaration)
3973 declaration_t *const declaration = allocate_declaration_zero();
3974 declaration->type = specifiers->type;
3975 declaration->declared_storage_class = specifiers->declared_storage_class;
3976 declaration->source_position = specifiers->source_position;
3977 declaration->modifiers = specifiers->decl_modifiers;
3979 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3980 warningf(&declaration->source_position,
3981 "useless storage class in empty declaration");
3983 declaration->storage_class = STORAGE_CLASS_NONE;
3985 type_t *type = declaration->type;
3986 switch (type->kind) {
3987 case TYPE_COMPOUND_STRUCT:
3988 case TYPE_COMPOUND_UNION: {
3989 if (type->compound.declaration->symbol == NULL) {
3990 warningf(&declaration->source_position,
3991 "unnamed struct/union that defines no instances");
4000 warningf(&declaration->source_position, "empty declaration");
4004 finished_declaration(declaration);
4007 static void parse_declaration_rest(declaration_t *ndeclaration,
4008 const declaration_specifiers_t *specifiers,
4009 parsed_declaration_func finished_declaration)
4011 add_anchor_token(';');
4012 add_anchor_token('=');
4013 add_anchor_token(',');
4015 declaration_t *declaration = finished_declaration(ndeclaration);
4017 type_t *orig_type = declaration->type;
4018 type_t *type = skip_typeref(orig_type);
4020 if (type->kind != TYPE_FUNCTION &&
4021 declaration->is_inline &&
4022 is_type_valid(type)) {
4023 warningf(&declaration->source_position,
4024 "variable '%Y' declared 'inline'\n", declaration->symbol);
4027 if(token.type == '=') {
4028 parse_init_declarator_rest(declaration);
4031 if(token.type != ',')
4035 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4040 rem_anchor_token(';');
4041 rem_anchor_token('=');
4042 rem_anchor_token(',');
4045 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4047 symbol_t *symbol = declaration->symbol;
4048 if(symbol == NULL) {
4049 errorf(HERE, "anonymous declaration not valid as function parameter");
4052 namespace_t namespc = (namespace_t) declaration->namespc;
4053 if(namespc != NAMESPACE_NORMAL) {
4054 return record_declaration(declaration);
4057 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4058 if(previous_declaration == NULL ||
4059 previous_declaration->parent_scope != scope) {
4060 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4065 if(previous_declaration->type == NULL) {
4066 previous_declaration->type = declaration->type;
4067 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4068 previous_declaration->storage_class = declaration->storage_class;
4069 previous_declaration->parent_scope = scope;
4070 return previous_declaration;
4072 return record_declaration(declaration);
4076 static void parse_declaration(parsed_declaration_func finished_declaration)
4078 declaration_specifiers_t specifiers;
4079 memset(&specifiers, 0, sizeof(specifiers));
4080 parse_declaration_specifiers(&specifiers);
4082 if(token.type == ';') {
4083 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4085 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4086 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4090 static void parse_kr_declaration_list(declaration_t *declaration)
4092 type_t *type = skip_typeref(declaration->type);
4093 if(!is_type_function(type))
4096 if(!type->function.kr_style_parameters)
4099 /* push function parameters */
4100 int top = environment_top();
4101 scope_t *last_scope = scope;
4102 set_scope(&declaration->scope);
4104 declaration_t *parameter = declaration->scope.declarations;
4105 for( ; parameter != NULL; parameter = parameter->next) {
4106 assert(parameter->parent_scope == NULL);
4107 parameter->parent_scope = scope;
4108 environment_push(parameter);
4111 /* parse declaration list */
4112 while(is_declaration_specifier(&token, false)) {
4113 parse_declaration(finished_kr_declaration);
4116 /* pop function parameters */
4117 assert(scope == &declaration->scope);
4118 set_scope(last_scope);
4119 environment_pop_to(top);
4121 /* update function type */
4122 type_t *new_type = duplicate_type(type);
4123 new_type->function.kr_style_parameters = false;
4125 function_parameter_t *parameters = NULL;
4126 function_parameter_t *last_parameter = NULL;
4128 declaration_t *parameter_declaration = declaration->scope.declarations;
4129 for( ; parameter_declaration != NULL;
4130 parameter_declaration = parameter_declaration->next) {
4131 type_t *parameter_type = parameter_declaration->type;
4132 if(parameter_type == NULL) {
4134 errorf(HERE, "no type specified for function parameter '%Y'",
4135 parameter_declaration->symbol);
4137 if (warning.implicit_int) {
4138 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4139 parameter_declaration->symbol);
4141 parameter_type = type_int;
4142 parameter_declaration->type = parameter_type;
4146 semantic_parameter(parameter_declaration);
4147 parameter_type = parameter_declaration->type;
4149 function_parameter_t *function_parameter
4150 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4151 memset(function_parameter, 0, sizeof(function_parameter[0]));
4153 function_parameter->type = parameter_type;
4154 if(last_parameter != NULL) {
4155 last_parameter->next = function_parameter;
4157 parameters = function_parameter;
4159 last_parameter = function_parameter;
4161 new_type->function.parameters = parameters;
4163 type = typehash_insert(new_type);
4164 if(type != new_type) {
4165 obstack_free(type_obst, new_type);
4168 declaration->type = type;
4171 static bool first_err = true;
4174 * When called with first_err set, prints the name of the current function,
4177 static void print_in_function(void) {
4180 diagnosticf("%s: In function '%Y':\n",
4181 current_function->source_position.input_name,
4182 current_function->symbol);
4187 * Check if all labels are defined in the current function.
4188 * Check if all labels are used in the current function.
4190 static void check_labels(void)
4192 for (const goto_statement_t *goto_statement = goto_first;
4193 goto_statement != NULL;
4194 goto_statement = goto_statement->next) {
4195 declaration_t *label = goto_statement->label;
4198 if (label->source_position.input_name == NULL) {
4199 print_in_function();
4200 errorf(&goto_statement->base.source_position,
4201 "label '%Y' used but not defined", label->symbol);
4204 goto_first = goto_last = NULL;
4206 if (warning.unused_label) {
4207 for (const label_statement_t *label_statement = label_first;
4208 label_statement != NULL;
4209 label_statement = label_statement->next) {
4210 const declaration_t *label = label_statement->label;
4212 if (! label->used) {
4213 print_in_function();
4214 warningf(&label_statement->base.source_position,
4215 "label '%Y' defined but not used", label->symbol);
4219 label_first = label_last = NULL;
4223 * Check declarations of current_function for unused entities.
4225 static void check_declarations(void)
4227 if (warning.unused_parameter) {
4228 const scope_t *scope = ¤t_function->scope;
4230 const declaration_t *parameter = scope->declarations;
4231 for (; parameter != NULL; parameter = parameter->next) {
4232 if (! parameter->used) {
4233 print_in_function();
4234 warningf(¶meter->source_position,
4235 "unused parameter '%Y'", parameter->symbol);
4239 if (warning.unused_variable) {
4243 static void parse_external_declaration(void)
4245 /* function-definitions and declarations both start with declaration
4247 declaration_specifiers_t specifiers;
4248 memset(&specifiers, 0, sizeof(specifiers));
4250 add_anchor_token(';');
4251 parse_declaration_specifiers(&specifiers);
4252 rem_anchor_token(';');
4254 /* must be a declaration */
4255 if(token.type == ';') {
4256 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4260 add_anchor_token(',');
4261 add_anchor_token('=');
4262 rem_anchor_token(';');
4264 /* declarator is common to both function-definitions and declarations */
4265 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4267 rem_anchor_token(',');
4268 rem_anchor_token('=');
4269 rem_anchor_token(';');
4271 /* must be a declaration */
4272 if(token.type == ',' || token.type == '=' || token.type == ';') {
4273 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4277 /* must be a function definition */
4278 parse_kr_declaration_list(ndeclaration);
4280 if(token.type != '{') {
4281 parse_error_expected("while parsing function definition", '{', 0);
4282 eat_until_matching_token(';');
4286 type_t *type = ndeclaration->type;
4288 /* note that we don't skip typerefs: the standard doesn't allow them here
4289 * (so we can't use is_type_function here) */
4290 if(type->kind != TYPE_FUNCTION) {
4291 if (is_type_valid(type)) {
4292 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4293 type, ndeclaration->symbol);
4299 /* § 6.7.5.3 (14) a function definition with () means no
4300 * parameters (and not unspecified parameters) */
4301 if(type->function.unspecified_parameters) {
4302 type_t *duplicate = duplicate_type(type);
4303 duplicate->function.unspecified_parameters = false;
4305 type = typehash_insert(duplicate);
4306 if(type != duplicate) {
4307 obstack_free(type_obst, duplicate);
4309 ndeclaration->type = type;
4312 declaration_t *const declaration = record_function_definition(ndeclaration);
4313 if(ndeclaration != declaration) {
4314 declaration->scope = ndeclaration->scope;
4316 type = skip_typeref(declaration->type);
4318 /* push function parameters and switch scope */
4319 int top = environment_top();
4320 scope_t *last_scope = scope;
4321 set_scope(&declaration->scope);
4323 declaration_t *parameter = declaration->scope.declarations;
4324 for( ; parameter != NULL; parameter = parameter->next) {
4325 if(parameter->parent_scope == &ndeclaration->scope) {
4326 parameter->parent_scope = scope;
4328 assert(parameter->parent_scope == NULL
4329 || parameter->parent_scope == scope);
4330 parameter->parent_scope = scope;
4331 environment_push(parameter);
4334 if(declaration->init.statement != NULL) {
4335 parser_error_multiple_definition(declaration, HERE);
4337 goto end_of_parse_external_declaration;
4339 /* parse function body */
4340 int label_stack_top = label_top();
4341 declaration_t *old_current_function = current_function;
4342 current_function = declaration;
4344 declaration->init.statement = parse_compound_statement();
4347 check_declarations();
4349 assert(current_function == declaration);
4350 current_function = old_current_function;
4351 label_pop_to(label_stack_top);
4354 end_of_parse_external_declaration:
4355 assert(scope == &declaration->scope);
4356 set_scope(last_scope);
4357 environment_pop_to(top);
4360 static type_t *make_bitfield_type(type_t *base, expression_t *size,
4361 source_position_t *source_position)
4363 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4364 type->bitfield.base = base;
4365 type->bitfield.size = size;
4370 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4373 declaration_t *iter = compound_declaration->scope.declarations;
4374 for( ; iter != NULL; iter = iter->next) {
4375 if(iter->namespc != NAMESPACE_NORMAL)
4378 if(iter->symbol == NULL) {
4379 type_t *type = skip_typeref(iter->type);
4380 if(is_type_compound(type)) {
4381 declaration_t *result
4382 = find_compound_entry(type->compound.declaration, symbol);
4389 if(iter->symbol == symbol) {
4397 static void parse_compound_declarators(declaration_t *struct_declaration,
4398 const declaration_specifiers_t *specifiers)
4400 declaration_t *last_declaration = struct_declaration->scope.declarations;
4401 if(last_declaration != NULL) {
4402 while(last_declaration->next != NULL) {
4403 last_declaration = last_declaration->next;
4408 declaration_t *declaration;
4410 if(token.type == ':') {
4411 source_position_t source_position = *HERE;
4414 type_t *base_type = specifiers->type;
4415 expression_t *size = parse_constant_expression();
4417 if(!is_type_integer(skip_typeref(base_type))) {
4418 errorf(HERE, "bitfield base type '%T' is not an integer type",
4422 type_t *type = make_bitfield_type(base_type, size, &source_position);
4424 declaration = allocate_declaration_zero();
4425 declaration->namespc = NAMESPACE_NORMAL;
4426 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4427 declaration->storage_class = STORAGE_CLASS_NONE;
4428 declaration->source_position = source_position;
4429 declaration->modifiers = specifiers->decl_modifiers;
4430 declaration->type = type;
4432 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4434 type_t *orig_type = declaration->type;
4435 type_t *type = skip_typeref(orig_type);
4437 if(token.type == ':') {
4438 source_position_t source_position = *HERE;
4440 expression_t *size = parse_constant_expression();
4442 if(!is_type_integer(type)) {
4443 errorf(HERE, "bitfield base type '%T' is not an "
4444 "integer type", orig_type);
4447 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4448 declaration->type = bitfield_type;
4450 /* TODO we ignore arrays for now... what is missing is a check
4451 * that they're at the end of the struct */
4452 if(is_type_incomplete(type) && !is_type_array(type)) {
4454 "compound member '%Y' has incomplete type '%T'",
4455 declaration->symbol, orig_type);
4456 } else if(is_type_function(type)) {
4457 errorf(HERE, "compound member '%Y' must not have function "
4458 "type '%T'", declaration->symbol, orig_type);
4463 /* make sure we don't define a symbol multiple times */
4464 symbol_t *symbol = declaration->symbol;
4465 if(symbol != NULL) {
4466 declaration_t *prev_decl
4467 = find_compound_entry(struct_declaration, symbol);
4469 if(prev_decl != NULL) {
4470 assert(prev_decl->symbol == symbol);
4471 errorf(&declaration->source_position,
4472 "multiple declarations of symbol '%Y' (declared %P)",
4473 symbol, &prev_decl->source_position);
4477 /* append declaration */
4478 if(last_declaration != NULL) {
4479 last_declaration->next = declaration;
4481 struct_declaration->scope.declarations = declaration;
4483 last_declaration = declaration;
4485 if(token.type != ',')
4495 static void parse_compound_type_entries(declaration_t *compound_declaration)
4498 add_anchor_token('}');
4500 while(token.type != '}' && token.type != T_EOF) {
4501 declaration_specifiers_t specifiers;
4502 memset(&specifiers, 0, sizeof(specifiers));
4503 parse_declaration_specifiers(&specifiers);
4505 parse_compound_declarators(compound_declaration, &specifiers);
4507 rem_anchor_token('}');
4509 if(token.type == T_EOF) {
4510 errorf(HERE, "EOF while parsing struct");
4515 static type_t *parse_typename(void)
4517 declaration_specifiers_t specifiers;
4518 memset(&specifiers, 0, sizeof(specifiers));
4519 parse_declaration_specifiers(&specifiers);
4520 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4521 /* TODO: improve error message, user does probably not know what a
4522 * storage class is...
4524 errorf(HERE, "typename may not have a storage class");
4527 type_t *result = parse_abstract_declarator(specifiers.type);
4535 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4536 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4537 expression_t *left);
4539 typedef struct expression_parser_function_t expression_parser_function_t;
4540 struct expression_parser_function_t {
4541 unsigned precedence;
4542 parse_expression_function parser;
4543 unsigned infix_precedence;
4544 parse_expression_infix_function infix_parser;
4547 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4550 * Prints an error message if an expression was expected but not read
4552 static expression_t *expected_expression_error(void)
4554 /* skip the error message if the error token was read */
4555 if (token.type != T_ERROR) {
4556 errorf(HERE, "expected expression, got token '%K'", &token);
4560 return create_invalid_expression();
4564 * Parse a string constant.
4566 static expression_t *parse_string_const(void)
4569 if (token.type == T_STRING_LITERAL) {
4570 string_t res = token.v.string;
4572 while (token.type == T_STRING_LITERAL) {
4573 res = concat_strings(&res, &token.v.string);
4576 if (token.type != T_WIDE_STRING_LITERAL) {
4577 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4578 /* note: that we use type_char_ptr here, which is already the
4579 * automatic converted type. revert_automatic_type_conversion
4580 * will construct the array type */
4581 cnst->base.type = type_char_ptr;
4582 cnst->string.value = res;
4586 wres = concat_string_wide_string(&res, &token.v.wide_string);
4588 wres = token.v.wide_string;
4593 switch (token.type) {
4594 case T_WIDE_STRING_LITERAL:
4595 wres = concat_wide_strings(&wres, &token.v.wide_string);
4598 case T_STRING_LITERAL:
4599 wres = concat_wide_string_string(&wres, &token.v.string);
4603 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4604 cnst->base.type = type_wchar_t_ptr;
4605 cnst->wide_string.value = wres;
4614 * Parse an integer constant.
4616 static expression_t *parse_int_const(void)
4618 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4619 cnst->base.source_position = *HERE;
4620 cnst->base.type = token.datatype;
4621 cnst->conste.v.int_value = token.v.intvalue;
4629 * Parse a character constant.
4631 static expression_t *parse_character_constant(void)
4633 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4635 cnst->base.source_position = *HERE;
4636 cnst->base.type = token.datatype;
4637 cnst->conste.v.character = token.v.string;
4639 if (cnst->conste.v.character.size != 1) {
4640 if (warning.multichar && (c_mode & _GNUC)) {
4642 warningf(HERE, "multi-character character constant");
4644 errorf(HERE, "more than 1 characters in character constant");
4653 * Parse a wide character constant.
4655 static expression_t *parse_wide_character_constant(void)
4657 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4659 cnst->base.source_position = *HERE;
4660 cnst->base.type = token.datatype;
4661 cnst->conste.v.wide_character = token.v.wide_string;
4663 if (cnst->conste.v.wide_character.size != 1) {
4664 if (warning.multichar && (c_mode & _GNUC)) {
4666 warningf(HERE, "multi-character character constant");
4668 errorf(HERE, "more than 1 characters in character constant");
4677 * Parse a float constant.
4679 static expression_t *parse_float_const(void)
4681 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4682 cnst->base.type = token.datatype;
4683 cnst->conste.v.float_value = token.v.floatvalue;
4690 static declaration_t *create_implicit_function(symbol_t *symbol,
4691 const source_position_t *source_position)
4693 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4694 ntype->function.return_type = type_int;
4695 ntype->function.unspecified_parameters = true;
4697 type_t *type = typehash_insert(ntype);
4702 declaration_t *const declaration = allocate_declaration_zero();
4703 declaration->storage_class = STORAGE_CLASS_EXTERN;
4704 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4705 declaration->type = type;
4706 declaration->symbol = symbol;
4707 declaration->source_position = *source_position;
4708 declaration->parent_scope = global_scope;
4710 scope_t *old_scope = scope;
4711 set_scope(global_scope);
4713 environment_push(declaration);
4714 /* prepends the declaration to the global declarations list */
4715 declaration->next = scope->declarations;
4716 scope->declarations = declaration;
4718 assert(scope == global_scope);
4719 set_scope(old_scope);
4725 * Creates a return_type (func)(argument_type) function type if not
4728 * @param return_type the return type
4729 * @param argument_type the argument type
4731 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4733 function_parameter_t *parameter
4734 = obstack_alloc(type_obst, sizeof(parameter[0]));
4735 memset(parameter, 0, sizeof(parameter[0]));
4736 parameter->type = argument_type;
4738 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4739 type->function.return_type = return_type;
4740 type->function.parameters = parameter;
4742 type_t *result = typehash_insert(type);
4743 if(result != type) {
4751 * Creates a function type for some function like builtins.
4753 * @param symbol the symbol describing the builtin
4755 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4757 switch(symbol->ID) {
4758 case T___builtin_alloca:
4759 return make_function_1_type(type_void_ptr, type_size_t);
4760 case T___builtin_nan:
4761 return make_function_1_type(type_double, type_char_ptr);
4762 case T___builtin_nanf:
4763 return make_function_1_type(type_float, type_char_ptr);
4764 case T___builtin_nand:
4765 return make_function_1_type(type_long_double, type_char_ptr);
4766 case T___builtin_va_end:
4767 return make_function_1_type(type_void, type_valist);
4769 internal_errorf(HERE, "not implemented builtin symbol found");
4774 * Performs automatic type cast as described in § 6.3.2.1.
4776 * @param orig_type the original type
4778 static type_t *automatic_type_conversion(type_t *orig_type)
4780 type_t *type = skip_typeref(orig_type);
4781 if(is_type_array(type)) {
4782 array_type_t *array_type = &type->array;
4783 type_t *element_type = array_type->element_type;
4784 unsigned qualifiers = array_type->type.qualifiers;
4786 return make_pointer_type(element_type, qualifiers);
4789 if(is_type_function(type)) {
4790 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4797 * reverts the automatic casts of array to pointer types and function
4798 * to function-pointer types as defined § 6.3.2.1
4800 type_t *revert_automatic_type_conversion(const expression_t *expression)
4802 switch (expression->kind) {
4803 case EXPR_REFERENCE: return expression->reference.declaration->type;
4804 case EXPR_SELECT: return expression->select.compound_entry->type;
4806 case EXPR_UNARY_DEREFERENCE: {
4807 const expression_t *const value = expression->unary.value;
4808 type_t *const type = skip_typeref(value->base.type);
4809 assert(is_type_pointer(type));
4810 return type->pointer.points_to;
4813 case EXPR_BUILTIN_SYMBOL:
4814 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4816 case EXPR_ARRAY_ACCESS: {
4817 const expression_t *array_ref = expression->array_access.array_ref;
4818 type_t *type_left = skip_typeref(array_ref->base.type);
4819 if (!is_type_valid(type_left))
4821 assert(is_type_pointer(type_left));
4822 return type_left->pointer.points_to;
4825 case EXPR_STRING_LITERAL: {
4826 size_t size = expression->string.value.size;
4827 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4830 case EXPR_WIDE_STRING_LITERAL: {
4831 size_t size = expression->wide_string.value.size;
4832 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4835 case EXPR_COMPOUND_LITERAL:
4836 return expression->compound_literal.type;
4841 return expression->base.type;
4844 static expression_t *parse_reference(void)
4846 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4848 reference_expression_t *ref = &expression->reference;
4849 ref->symbol = token.v.symbol;
4851 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4853 source_position_t source_position = token.source_position;
4856 if(declaration == NULL) {
4857 if (! strict_mode && token.type == '(') {
4858 /* an implicitly defined function */
4859 if (warning.implicit_function_declaration) {
4860 warningf(HERE, "implicit declaration of function '%Y'",
4864 declaration = create_implicit_function(ref->symbol,
4867 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4868 return create_invalid_expression();
4872 type_t *type = declaration->type;
4874 /* we always do the auto-type conversions; the & and sizeof parser contains
4875 * code to revert this! */
4876 type = automatic_type_conversion(type);
4878 ref->declaration = declaration;
4879 ref->base.type = type;
4881 /* this declaration is used */
4882 declaration->used = true;
4884 /* check for deprecated functions */
4885 if(declaration->deprecated != 0) {
4886 const char *prefix = "";
4887 if (is_type_function(declaration->type))
4888 prefix = "function ";
4890 if (declaration->deprecated_string != NULL) {
4891 warningf(&source_position,
4892 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4893 declaration->deprecated_string);
4895 warningf(&source_position,
4896 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4903 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4907 /* TODO check if explicit cast is allowed and issue warnings/errors */
4910 static expression_t *parse_compound_literal(type_t *type)
4912 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4914 parse_initializer_env_t env;
4916 env.declaration = NULL;
4917 env.must_be_constant = false;
4918 initializer_t *initializer = parse_initializer(&env);
4921 expression->compound_literal.initializer = initializer;
4922 expression->compound_literal.type = type;
4923 expression->base.type = automatic_type_conversion(type);
4929 * Parse a cast expression.
4931 static expression_t *parse_cast(void)
4933 source_position_t source_position = token.source_position;
4935 type_t *type = parse_typename();
4937 /* matching add_anchor_token() is at call site */
4938 rem_anchor_token(')');
4941 if(token.type == '{') {
4942 return parse_compound_literal(type);
4945 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4946 cast->base.source_position = source_position;
4948 expression_t *value = parse_sub_expression(20);
4950 check_cast_allowed(value, type);
4952 cast->base.type = type;
4953 cast->unary.value = value;
4957 return create_invalid_expression();
4961 * Parse a statement expression.
4963 static expression_t *parse_statement_expression(void)
4965 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4967 statement_t *statement = parse_compound_statement();
4968 expression->statement.statement = statement;
4969 expression->base.source_position = statement->base.source_position;
4971 /* find last statement and use its type */
4972 type_t *type = type_void;
4973 const statement_t *stmt = statement->compound.statements;
4975 while (stmt->base.next != NULL)
4976 stmt = stmt->base.next;
4978 if (stmt->kind == STATEMENT_EXPRESSION) {
4979 type = stmt->expression.expression->base.type;
4982 warningf(&expression->base.source_position, "empty statement expression ({})");
4984 expression->base.type = type;
4990 return create_invalid_expression();
4994 * Parse a braced expression.
4996 static expression_t *parse_brace_expression(void)
4999 add_anchor_token(')');
5001 switch(token.type) {
5003 /* gcc extension: a statement expression */
5004 return parse_statement_expression();
5008 return parse_cast();
5010 if(is_typedef_symbol(token.v.symbol)) {
5011 return parse_cast();
5015 expression_t *result = parse_expression();
5016 rem_anchor_token(')');
5021 return create_invalid_expression();
5024 static expression_t *parse_function_keyword(void)
5029 if (current_function == NULL) {
5030 errorf(HERE, "'__func__' used outside of a function");
5033 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5034 expression->base.type = type_char_ptr;
5035 expression->funcname.kind = FUNCNAME_FUNCTION;
5040 static expression_t *parse_pretty_function_keyword(void)
5042 eat(T___PRETTY_FUNCTION__);
5044 if (current_function == NULL) {
5045 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5048 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5049 expression->base.type = type_char_ptr;
5050 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5055 static expression_t *parse_funcsig_keyword(void)
5059 if (current_function == NULL) {
5060 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5063 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5064 expression->base.type = type_char_ptr;
5065 expression->funcname.kind = FUNCNAME_FUNCSIG;
5070 static expression_t *parse_funcdname_keyword(void)
5072 eat(T___FUNCDNAME__);
5074 if (current_function == NULL) {
5075 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5078 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5079 expression->base.type = type_char_ptr;
5080 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5085 static designator_t *parse_designator(void)
5087 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5088 result->source_position = *HERE;
5090 if(token.type != T_IDENTIFIER) {
5091 parse_error_expected("while parsing member designator",
5095 result->symbol = token.v.symbol;
5098 designator_t *last_designator = result;
5100 if(token.type == '.') {
5102 if(token.type != T_IDENTIFIER) {
5103 parse_error_expected("while parsing member designator",
5107 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5108 designator->source_position = *HERE;
5109 designator->symbol = token.v.symbol;
5112 last_designator->next = designator;
5113 last_designator = designator;
5116 if(token.type == '[') {
5118 add_anchor_token(']');
5119 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5120 designator->source_position = *HERE;
5121 designator->array_index = parse_expression();
5122 rem_anchor_token(']');
5124 if(designator->array_index == NULL) {
5128 last_designator->next = designator;
5129 last_designator = designator;
5141 * Parse the __builtin_offsetof() expression.
5143 static expression_t *parse_offsetof(void)
5145 eat(T___builtin_offsetof);
5147 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5148 expression->base.type = type_size_t;
5151 add_anchor_token(',');
5152 type_t *type = parse_typename();
5153 rem_anchor_token(',');
5155 add_anchor_token(')');
5156 designator_t *designator = parse_designator();
5157 rem_anchor_token(')');
5160 expression->offsetofe.type = type;
5161 expression->offsetofe.designator = designator;
5164 memset(&path, 0, sizeof(path));
5165 path.top_type = type;
5166 path.path = NEW_ARR_F(type_path_entry_t, 0);
5168 descend_into_subtype(&path);
5170 if(!walk_designator(&path, designator, true)) {
5171 return create_invalid_expression();
5174 DEL_ARR_F(path.path);
5178 return create_invalid_expression();
5182 * Parses a _builtin_va_start() expression.
5184 static expression_t *parse_va_start(void)
5186 eat(T___builtin_va_start);
5188 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5191 add_anchor_token(',');
5192 expression->va_starte.ap = parse_assignment_expression();
5193 rem_anchor_token(',');
5195 expression_t *const expr = parse_assignment_expression();
5196 if (expr->kind == EXPR_REFERENCE) {
5197 declaration_t *const decl = expr->reference.declaration;
5199 return create_invalid_expression();
5200 if (decl->parent_scope == ¤t_function->scope &&
5201 decl->next == NULL) {
5202 expression->va_starte.parameter = decl;
5207 errorf(&expr->base.source_position,
5208 "second argument of 'va_start' must be last parameter of the current function");
5210 return create_invalid_expression();
5214 * Parses a _builtin_va_arg() expression.
5216 static expression_t *parse_va_arg(void)
5218 eat(T___builtin_va_arg);
5220 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5223 expression->va_arge.ap = parse_assignment_expression();
5225 expression->base.type = parse_typename();
5230 return create_invalid_expression();
5233 static expression_t *parse_builtin_symbol(void)
5235 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5237 symbol_t *symbol = token.v.symbol;
5239 expression->builtin_symbol.symbol = symbol;
5242 type_t *type = get_builtin_symbol_type(symbol);
5243 type = automatic_type_conversion(type);
5245 expression->base.type = type;
5250 * Parses a __builtin_constant() expression.
5252 static expression_t *parse_builtin_constant(void)
5254 eat(T___builtin_constant_p);
5256 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5259 add_anchor_token(')');
5260 expression->builtin_constant.value = parse_assignment_expression();
5261 rem_anchor_token(')');
5263 expression->base.type = type_int;
5267 return create_invalid_expression();
5271 * Parses a __builtin_prefetch() expression.
5273 static expression_t *parse_builtin_prefetch(void)
5275 eat(T___builtin_prefetch);
5277 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5280 add_anchor_token(')');
5281 expression->builtin_prefetch.adr = parse_assignment_expression();
5282 if (token.type == ',') {
5284 expression->builtin_prefetch.rw = parse_assignment_expression();
5286 if (token.type == ',') {
5288 expression->builtin_prefetch.locality = parse_assignment_expression();
5290 rem_anchor_token(')');
5292 expression->base.type = type_void;
5296 return create_invalid_expression();
5300 * Parses a __builtin_is_*() compare expression.
5302 static expression_t *parse_compare_builtin(void)
5304 expression_t *expression;
5306 switch(token.type) {
5307 case T___builtin_isgreater:
5308 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5310 case T___builtin_isgreaterequal:
5311 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5313 case T___builtin_isless:
5314 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5316 case T___builtin_islessequal:
5317 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5319 case T___builtin_islessgreater:
5320 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5322 case T___builtin_isunordered:
5323 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5326 internal_errorf(HERE, "invalid compare builtin found");
5329 expression->base.source_position = *HERE;
5333 expression->binary.left = parse_assignment_expression();
5335 expression->binary.right = parse_assignment_expression();
5338 type_t *const orig_type_left = expression->binary.left->base.type;
5339 type_t *const orig_type_right = expression->binary.right->base.type;
5341 type_t *const type_left = skip_typeref(orig_type_left);
5342 type_t *const type_right = skip_typeref(orig_type_right);
5343 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5344 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5345 type_error_incompatible("invalid operands in comparison",
5346 &expression->base.source_position, orig_type_left, orig_type_right);
5349 semantic_comparison(&expression->binary);
5354 return create_invalid_expression();
5358 * Parses a __builtin_expect() expression.
5360 static expression_t *parse_builtin_expect(void)
5362 eat(T___builtin_expect);
5364 expression_t *expression
5365 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5368 expression->binary.left = parse_assignment_expression();
5370 expression->binary.right = parse_constant_expression();
5373 expression->base.type = expression->binary.left->base.type;
5377 return create_invalid_expression();
5381 * Parses a MS assume() expression.
5383 static expression_t *parse_assume(void) {
5386 expression_t *expression
5387 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5390 add_anchor_token(')');
5391 expression->unary.value = parse_assignment_expression();
5392 rem_anchor_token(')');
5395 expression->base.type = type_void;
5398 return create_invalid_expression();
5402 * Parse a microsoft __noop expression.
5404 static expression_t *parse_noop_expression(void) {
5405 source_position_t source_position = *HERE;
5408 if (token.type == '(') {
5409 /* parse arguments */
5411 add_anchor_token(')');
5412 add_anchor_token(',');
5414 if(token.type != ')') {
5416 (void)parse_assignment_expression();
5417 if(token.type != ',')
5423 rem_anchor_token(',');
5424 rem_anchor_token(')');
5427 /* the result is a (int)0 */
5428 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5429 cnst->base.source_position = source_position;
5430 cnst->base.type = type_int;
5431 cnst->conste.v.int_value = 0;
5432 cnst->conste.is_ms_noop = true;
5437 return create_invalid_expression();
5441 * Parses a primary expression.
5443 static expression_t *parse_primary_expression(void)
5445 switch (token.type) {
5446 case T_INTEGER: return parse_int_const();
5447 case T_CHARACTER_CONSTANT: return parse_character_constant();
5448 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5449 case T_FLOATINGPOINT: return parse_float_const();
5450 case T_STRING_LITERAL:
5451 case T_WIDE_STRING_LITERAL: return parse_string_const();
5452 case T_IDENTIFIER: return parse_reference();
5453 case T___FUNCTION__:
5454 case T___func__: return parse_function_keyword();
5455 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5456 case T___FUNCSIG__: return parse_funcsig_keyword();
5457 case T___FUNCDNAME__: return parse_funcdname_keyword();
5458 case T___builtin_offsetof: return parse_offsetof();
5459 case T___builtin_va_start: return parse_va_start();
5460 case T___builtin_va_arg: return parse_va_arg();
5461 case T___builtin_expect: return parse_builtin_expect();
5462 case T___builtin_alloca:
5463 case T___builtin_nan:
5464 case T___builtin_nand:
5465 case T___builtin_nanf:
5466 case T___builtin_va_end: return parse_builtin_symbol();
5467 case T___builtin_isgreater:
5468 case T___builtin_isgreaterequal:
5469 case T___builtin_isless:
5470 case T___builtin_islessequal:
5471 case T___builtin_islessgreater:
5472 case T___builtin_isunordered: return parse_compare_builtin();
5473 case T___builtin_constant_p: return parse_builtin_constant();
5474 case T___builtin_prefetch: return parse_builtin_prefetch();
5475 case T__assume: return parse_assume();
5477 case '(': return parse_brace_expression();
5478 case T___noop: return parse_noop_expression();
5481 errorf(HERE, "unexpected token %K, expected an expression", &token);
5482 return create_invalid_expression();
5486 * Check if the expression has the character type and issue a warning then.
5488 static void check_for_char_index_type(const expression_t *expression) {
5489 type_t *const type = expression->base.type;
5490 const type_t *const base_type = skip_typeref(type);
5492 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5493 warning.char_subscripts) {
5494 warningf(&expression->base.source_position,
5495 "array subscript has type '%T'", type);
5499 static expression_t *parse_array_expression(unsigned precedence,
5505 add_anchor_token(']');
5507 expression_t *inside = parse_expression();
5509 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5511 array_access_expression_t *array_access = &expression->array_access;
5513 type_t *const orig_type_left = left->base.type;
5514 type_t *const orig_type_inside = inside->base.type;
5516 type_t *const type_left = skip_typeref(orig_type_left);
5517 type_t *const type_inside = skip_typeref(orig_type_inside);
5519 type_t *return_type;
5520 if (is_type_pointer(type_left)) {
5521 return_type = type_left->pointer.points_to;
5522 array_access->array_ref = left;
5523 array_access->index = inside;
5524 check_for_char_index_type(inside);
5525 } else if (is_type_pointer(type_inside)) {
5526 return_type = type_inside->pointer.points_to;
5527 array_access->array_ref = inside;
5528 array_access->index = left;
5529 array_access->flipped = true;
5530 check_for_char_index_type(left);
5532 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5534 "array access on object with non-pointer types '%T', '%T'",
5535 orig_type_left, orig_type_inside);
5537 return_type = type_error_type;
5538 array_access->array_ref = create_invalid_expression();
5541 rem_anchor_token(']');
5542 if(token.type != ']') {
5543 parse_error_expected("Problem while parsing array access", ']', 0);
5548 return_type = automatic_type_conversion(return_type);
5549 expression->base.type = return_type;
5554 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5556 expression_t *tp_expression = allocate_expression_zero(kind);
5557 tp_expression->base.type = type_size_t;
5559 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5561 add_anchor_token(')');
5562 tp_expression->typeprop.type = parse_typename();
5563 rem_anchor_token(')');
5566 expression_t *expression = parse_sub_expression(precedence);
5567 expression->base.type = revert_automatic_type_conversion(expression);
5569 tp_expression->typeprop.type = expression->base.type;
5570 tp_expression->typeprop.tp_expression = expression;
5573 return tp_expression;
5575 return create_invalid_expression();
5578 static expression_t *parse_sizeof(unsigned precedence)
5581 return parse_typeprop(EXPR_SIZEOF, precedence);
5584 static expression_t *parse_alignof(unsigned precedence)
5587 return parse_typeprop(EXPR_SIZEOF, precedence);
5590 static expression_t *parse_select_expression(unsigned precedence,
5591 expression_t *compound)
5594 assert(token.type == '.' || token.type == T_MINUSGREATER);
5596 bool is_pointer = (token.type == T_MINUSGREATER);
5599 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5600 select->select.compound = compound;
5602 if(token.type != T_IDENTIFIER) {
5603 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
5606 symbol_t *symbol = token.v.symbol;
5607 select->select.symbol = symbol;
5610 type_t *const orig_type = compound->base.type;
5611 type_t *const type = skip_typeref(orig_type);
5613 type_t *type_left = type;
5615 if (!is_type_pointer(type)) {
5616 if (is_type_valid(type)) {
5617 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5619 return create_invalid_expression();
5621 type_left = type->pointer.points_to;
5623 type_left = skip_typeref(type_left);
5625 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5626 type_left->kind != TYPE_COMPOUND_UNION) {
5627 if (is_type_valid(type_left)) {
5628 errorf(HERE, "request for member '%Y' in something not a struct or "
5629 "union, but '%T'", symbol, type_left);
5631 return create_invalid_expression();
5634 declaration_t *const declaration = type_left->compound.declaration;
5636 if(!declaration->init.is_defined) {
5637 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5639 return create_invalid_expression();
5642 declaration_t *iter = find_compound_entry(declaration, symbol);
5644 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5645 return create_invalid_expression();
5648 /* we always do the auto-type conversions; the & and sizeof parser contains
5649 * code to revert this! */
5650 type_t *expression_type = automatic_type_conversion(iter->type);
5652 select->select.compound_entry = iter;
5653 select->base.type = expression_type;
5655 if(expression_type->kind == TYPE_BITFIELD) {
5656 expression_t *extract
5657 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5658 extract->unary.value = select;
5659 extract->base.type = expression_type->bitfield.base;
5668 * Parse a call expression, ie. expression '( ... )'.
5670 * @param expression the function address
5672 static expression_t *parse_call_expression(unsigned precedence,
5673 expression_t *expression)
5676 expression_t *result = allocate_expression_zero(EXPR_CALL);
5677 result->base.source_position = expression->base.source_position;
5679 call_expression_t *call = &result->call;
5680 call->function = expression;
5682 type_t *const orig_type = expression->base.type;
5683 type_t *const type = skip_typeref(orig_type);
5685 function_type_t *function_type = NULL;
5686 if (is_type_pointer(type)) {
5687 type_t *const to_type = skip_typeref(type->pointer.points_to);
5689 if (is_type_function(to_type)) {
5690 function_type = &to_type->function;
5691 call->base.type = function_type->return_type;
5695 if (function_type == NULL && is_type_valid(type)) {
5696 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5699 /* parse arguments */
5701 add_anchor_token(')');
5702 add_anchor_token(',');
5704 if(token.type != ')') {
5705 call_argument_t *last_argument = NULL;
5708 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5710 argument->expression = parse_assignment_expression();
5711 if(last_argument == NULL) {
5712 call->arguments = argument;
5714 last_argument->next = argument;
5716 last_argument = argument;
5718 if(token.type != ',')
5723 rem_anchor_token(',');
5724 rem_anchor_token(')');
5727 if(function_type != NULL) {
5728 function_parameter_t *parameter = function_type->parameters;
5729 call_argument_t *argument = call->arguments;
5730 for( ; parameter != NULL && argument != NULL;
5731 parameter = parameter->next, argument = argument->next) {
5732 type_t *expected_type = parameter->type;
5733 /* TODO report scope in error messages */
5734 expression_t *const arg_expr = argument->expression;
5735 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5737 &arg_expr->base.source_position);
5738 if (res_type == NULL) {
5739 /* TODO improve error message */
5740 errorf(&arg_expr->base.source_position,
5741 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5742 arg_expr, arg_expr->base.type, expected_type);
5744 argument->expression = create_implicit_cast(argument->expression, expected_type);
5747 /* too few parameters */
5748 if(parameter != NULL) {
5749 errorf(HERE, "too few arguments to function '%E'", expression);
5750 } else if(argument != NULL) {
5751 /* too many parameters */
5752 if(!function_type->variadic
5753 && !function_type->unspecified_parameters) {
5754 errorf(HERE, "too many arguments to function '%E'", expression);
5756 /* do default promotion */
5757 for( ; argument != NULL; argument = argument->next) {
5758 type_t *type = argument->expression->base.type;
5760 type = skip_typeref(type);
5761 if(is_type_integer(type)) {
5762 type = promote_integer(type);
5763 } else if(type == type_float) {
5767 argument->expression
5768 = create_implicit_cast(argument->expression, type);
5771 check_format(&result->call);
5774 check_format(&result->call);
5780 return create_invalid_expression();
5783 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5785 static bool same_compound_type(const type_t *type1, const type_t *type2)
5788 is_type_compound(type1) &&
5789 type1->kind == type2->kind &&
5790 type1->compound.declaration == type2->compound.declaration;
5794 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5796 * @param expression the conditional expression
5798 static expression_t *parse_conditional_expression(unsigned precedence,
5799 expression_t *expression)
5802 add_anchor_token(':');
5804 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5806 conditional_expression_t *conditional = &result->conditional;
5807 conditional->condition = expression;
5810 type_t *const condition_type_orig = expression->base.type;
5811 type_t *const condition_type = skip_typeref(condition_type_orig);
5812 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5813 type_error("expected a scalar type in conditional condition",
5814 &expression->base.source_position, condition_type_orig);
5817 expression_t *true_expression = parse_expression();
5818 rem_anchor_token(':');
5820 expression_t *false_expression = parse_sub_expression(precedence);
5822 type_t *const orig_true_type = true_expression->base.type;
5823 type_t *const orig_false_type = false_expression->base.type;
5824 type_t *const true_type = skip_typeref(orig_true_type);
5825 type_t *const false_type = skip_typeref(orig_false_type);
5828 type_t *result_type;
5829 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5830 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5831 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5832 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5833 warningf(&expression->base.source_position,
5834 "ISO C forbids conditional expression with only one void side");
5836 result_type = type_void;
5837 } else if (is_type_arithmetic(true_type)
5838 && is_type_arithmetic(false_type)) {
5839 result_type = semantic_arithmetic(true_type, false_type);
5841 true_expression = create_implicit_cast(true_expression, result_type);
5842 false_expression = create_implicit_cast(false_expression, result_type);
5844 conditional->true_expression = true_expression;
5845 conditional->false_expression = false_expression;
5846 conditional->base.type = result_type;
5847 } else if (same_compound_type(true_type, false_type)) {
5848 /* just take 1 of the 2 types */
5849 result_type = true_type;
5850 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5851 type_t *pointer_type;
5853 expression_t *other_expression;
5854 if (is_type_pointer(true_type)) {
5855 pointer_type = true_type;
5856 other_type = false_type;
5857 other_expression = false_expression;
5859 pointer_type = false_type;
5860 other_type = true_type;
5861 other_expression = true_expression;
5864 if(is_type_pointer(other_type)) {
5865 if(!pointers_compatible(true_type, false_type)) {
5866 warningf(&expression->base.source_position,
5867 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5869 result_type = true_type;
5870 } else if(is_null_pointer_constant(other_expression)) {
5871 result_type = pointer_type;
5872 } else if(is_type_integer(other_type)) {
5873 warningf(&expression->base.source_position,
5874 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5875 result_type = pointer_type;
5877 type_error_incompatible("while parsing conditional",
5878 &expression->base.source_position, true_type, false_type);
5879 result_type = type_error_type;
5882 /* TODO: one pointer to void*, other some pointer */
5884 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5885 type_error_incompatible("while parsing conditional",
5886 &expression->base.source_position, true_type,
5889 result_type = type_error_type;
5892 conditional->true_expression
5893 = create_implicit_cast(true_expression, result_type);
5894 conditional->false_expression
5895 = create_implicit_cast(false_expression, result_type);
5896 conditional->base.type = result_type;
5899 return create_invalid_expression();
5903 * Parse an extension expression.
5905 static expression_t *parse_extension(unsigned precedence)
5907 eat(T___extension__);
5909 /* TODO enable extensions */
5910 expression_t *expression = parse_sub_expression(precedence);
5911 /* TODO disable extensions */
5916 * Parse a __builtin_classify_type() expression.
5918 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5920 eat(T___builtin_classify_type);
5922 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5923 result->base.type = type_int;
5926 add_anchor_token(')');
5927 expression_t *expression = parse_sub_expression(precedence);
5928 rem_anchor_token(')');
5930 result->classify_type.type_expression = expression;
5934 return create_invalid_expression();
5937 static void semantic_incdec(unary_expression_t *expression)
5939 type_t *const orig_type = expression->value->base.type;
5940 type_t *const type = skip_typeref(orig_type);
5941 /* TODO !is_type_real && !is_type_pointer */
5942 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5943 if (is_type_valid(type)) {
5944 /* TODO: improve error message */
5945 errorf(HERE, "operation needs an arithmetic or pointer type");
5950 expression->base.type = orig_type;
5953 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5955 type_t *const orig_type = expression->value->base.type;
5956 type_t *const type = skip_typeref(orig_type);
5957 if(!is_type_arithmetic(type)) {
5958 if (is_type_valid(type)) {
5959 /* TODO: improve error message */
5960 errorf(HERE, "operation needs an arithmetic type");
5965 expression->base.type = orig_type;
5968 static void semantic_unexpr_scalar(unary_expression_t *expression)
5970 type_t *const orig_type = expression->value->base.type;
5971 type_t *const type = skip_typeref(orig_type);
5972 if (!is_type_scalar(type)) {
5973 if (is_type_valid(type)) {
5974 errorf(HERE, "operand of ! must be of scalar type");
5979 expression->base.type = orig_type;
5982 static void semantic_unexpr_integer(unary_expression_t *expression)
5984 type_t *const orig_type = expression->value->base.type;
5985 type_t *const type = skip_typeref(orig_type);
5986 if (!is_type_integer(type)) {
5987 if (is_type_valid(type)) {
5988 errorf(HERE, "operand of ~ must be of integer type");
5993 expression->base.type = orig_type;
5996 static void semantic_dereference(unary_expression_t *expression)
5998 type_t *const orig_type = expression->value->base.type;
5999 type_t *const type = skip_typeref(orig_type);
6000 if(!is_type_pointer(type)) {
6001 if (is_type_valid(type)) {
6002 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6007 type_t *result_type = type->pointer.points_to;
6008 result_type = automatic_type_conversion(result_type);
6009 expression->base.type = result_type;
6013 * Check the semantic of the address taken expression.
6015 static void semantic_take_addr(unary_expression_t *expression)
6017 expression_t *value = expression->value;
6018 value->base.type = revert_automatic_type_conversion(value);
6020 type_t *orig_type = value->base.type;
6021 if(!is_type_valid(orig_type))
6024 if(value->kind == EXPR_REFERENCE) {
6025 declaration_t *const declaration = value->reference.declaration;
6026 if(declaration != NULL) {
6027 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6028 errorf(&expression->base.source_position,
6029 "address of register variable '%Y' requested",
6030 declaration->symbol);
6032 declaration->address_taken = 1;
6036 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6039 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6040 static expression_t *parse_##unexpression_type(unsigned precedence) \
6044 expression_t *unary_expression \
6045 = allocate_expression_zero(unexpression_type); \
6046 unary_expression->base.source_position = *HERE; \
6047 unary_expression->unary.value = parse_sub_expression(precedence); \
6049 sfunc(&unary_expression->unary); \
6051 return unary_expression; \
6054 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6055 semantic_unexpr_arithmetic)
6056 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6057 semantic_unexpr_arithmetic)
6058 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6059 semantic_unexpr_scalar)
6060 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6061 semantic_dereference)
6062 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6064 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6065 semantic_unexpr_integer)
6066 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6068 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6071 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6073 static expression_t *parse_##unexpression_type(unsigned precedence, \
6074 expression_t *left) \
6076 (void) precedence; \
6079 expression_t *unary_expression \
6080 = allocate_expression_zero(unexpression_type); \
6081 unary_expression->unary.value = left; \
6083 sfunc(&unary_expression->unary); \
6085 return unary_expression; \
6088 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6089 EXPR_UNARY_POSTFIX_INCREMENT,
6091 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6092 EXPR_UNARY_POSTFIX_DECREMENT,
6095 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6097 /* TODO: handle complex + imaginary types */
6099 /* § 6.3.1.8 Usual arithmetic conversions */
6100 if(type_left == type_long_double || type_right == type_long_double) {
6101 return type_long_double;
6102 } else if(type_left == type_double || type_right == type_double) {
6104 } else if(type_left == type_float || type_right == type_float) {
6108 type_right = promote_integer(type_right);
6109 type_left = promote_integer(type_left);
6111 if(type_left == type_right)
6114 bool signed_left = is_type_signed(type_left);
6115 bool signed_right = is_type_signed(type_right);
6116 int rank_left = get_rank(type_left);
6117 int rank_right = get_rank(type_right);
6118 if(rank_left < rank_right) {
6119 if(signed_left == signed_right || !signed_right) {
6125 if(signed_left == signed_right || !signed_left) {
6134 * Check the semantic restrictions for a binary expression.
6136 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6138 expression_t *const left = expression->left;
6139 expression_t *const right = expression->right;
6140 type_t *const orig_type_left = left->base.type;
6141 type_t *const orig_type_right = right->base.type;
6142 type_t *const type_left = skip_typeref(orig_type_left);
6143 type_t *const type_right = skip_typeref(orig_type_right);
6145 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6146 /* TODO: improve error message */
6147 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6148 errorf(HERE, "operation needs arithmetic types");
6153 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6154 expression->left = create_implicit_cast(left, arithmetic_type);
6155 expression->right = create_implicit_cast(right, arithmetic_type);
6156 expression->base.type = arithmetic_type;
6159 static void semantic_shift_op(binary_expression_t *expression)
6161 expression_t *const left = expression->left;
6162 expression_t *const right = expression->right;
6163 type_t *const orig_type_left = left->base.type;
6164 type_t *const orig_type_right = right->base.type;
6165 type_t * type_left = skip_typeref(orig_type_left);
6166 type_t * type_right = skip_typeref(orig_type_right);
6168 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6169 /* TODO: improve error message */
6170 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6171 errorf(HERE, "operation needs integer types");
6176 type_left = promote_integer(type_left);
6177 type_right = promote_integer(type_right);
6179 expression->left = create_implicit_cast(left, type_left);
6180 expression->right = create_implicit_cast(right, type_right);
6181 expression->base.type = type_left;
6184 static void semantic_add(binary_expression_t *expression)
6186 expression_t *const left = expression->left;
6187 expression_t *const right = expression->right;
6188 type_t *const orig_type_left = left->base.type;
6189 type_t *const orig_type_right = right->base.type;
6190 type_t *const type_left = skip_typeref(orig_type_left);
6191 type_t *const type_right = skip_typeref(orig_type_right);
6194 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6195 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6196 expression->left = create_implicit_cast(left, arithmetic_type);
6197 expression->right = create_implicit_cast(right, arithmetic_type);
6198 expression->base.type = arithmetic_type;
6200 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6201 expression->base.type = type_left;
6202 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6203 expression->base.type = type_right;
6204 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6205 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6209 static void semantic_sub(binary_expression_t *expression)
6211 expression_t *const left = expression->left;
6212 expression_t *const right = expression->right;
6213 type_t *const orig_type_left = left->base.type;
6214 type_t *const orig_type_right = right->base.type;
6215 type_t *const type_left = skip_typeref(orig_type_left);
6216 type_t *const type_right = skip_typeref(orig_type_right);
6219 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6220 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6221 expression->left = create_implicit_cast(left, arithmetic_type);
6222 expression->right = create_implicit_cast(right, arithmetic_type);
6223 expression->base.type = arithmetic_type;
6225 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6226 expression->base.type = type_left;
6227 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6228 if(!pointers_compatible(type_left, type_right)) {
6230 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6231 orig_type_left, orig_type_right);
6233 expression->base.type = type_ptrdiff_t;
6235 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6236 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6237 orig_type_left, orig_type_right);
6242 * Check the semantics of comparison expressions.
6244 * @param expression The expression to check.
6246 static void semantic_comparison(binary_expression_t *expression)
6248 expression_t *left = expression->left;
6249 expression_t *right = expression->right;
6250 type_t *orig_type_left = left->base.type;
6251 type_t *orig_type_right = right->base.type;
6253 type_t *type_left = skip_typeref(orig_type_left);
6254 type_t *type_right = skip_typeref(orig_type_right);
6256 /* TODO non-arithmetic types */
6257 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6258 if (warning.sign_compare &&
6259 (expression->base.kind != EXPR_BINARY_EQUAL &&
6260 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6261 (is_type_signed(type_left) != is_type_signed(type_right))) {
6262 warningf(&expression->base.source_position,
6263 "comparison between signed and unsigned");
6265 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6266 expression->left = create_implicit_cast(left, arithmetic_type);
6267 expression->right = create_implicit_cast(right, arithmetic_type);
6268 expression->base.type = arithmetic_type;
6269 if (warning.float_equal &&
6270 (expression->base.kind == EXPR_BINARY_EQUAL ||
6271 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6272 is_type_float(arithmetic_type)) {
6273 warningf(&expression->base.source_position,
6274 "comparing floating point with == or != is unsafe");
6276 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6277 /* TODO check compatibility */
6278 } else if (is_type_pointer(type_left)) {
6279 expression->right = create_implicit_cast(right, type_left);
6280 } else if (is_type_pointer(type_right)) {
6281 expression->left = create_implicit_cast(left, type_right);
6282 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6283 type_error_incompatible("invalid operands in comparison",
6284 &expression->base.source_position,
6285 type_left, type_right);
6287 expression->base.type = type_int;
6290 static void semantic_arithmetic_assign(binary_expression_t *expression)
6292 expression_t *left = expression->left;
6293 expression_t *right = expression->right;
6294 type_t *orig_type_left = left->base.type;
6295 type_t *orig_type_right = right->base.type;
6297 type_t *type_left = skip_typeref(orig_type_left);
6298 type_t *type_right = skip_typeref(orig_type_right);
6300 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6301 /* TODO: improve error message */
6302 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6303 errorf(HERE, "operation needs arithmetic types");
6308 /* combined instructions are tricky. We can't create an implicit cast on
6309 * the left side, because we need the uncasted form for the store.
6310 * The ast2firm pass has to know that left_type must be right_type
6311 * for the arithmetic operation and create a cast by itself */
6312 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6313 expression->right = create_implicit_cast(right, arithmetic_type);
6314 expression->base.type = type_left;
6317 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6319 expression_t *const left = expression->left;
6320 expression_t *const right = expression->right;
6321 type_t *const orig_type_left = left->base.type;
6322 type_t *const orig_type_right = right->base.type;
6323 type_t *const type_left = skip_typeref(orig_type_left);
6324 type_t *const type_right = skip_typeref(orig_type_right);
6326 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6327 /* combined instructions are tricky. We can't create an implicit cast on
6328 * the left side, because we need the uncasted form for the store.
6329 * The ast2firm pass has to know that left_type must be right_type
6330 * for the arithmetic operation and create a cast by itself */
6331 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6332 expression->right = create_implicit_cast(right, arithmetic_type);
6333 expression->base.type = type_left;
6334 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6335 expression->base.type = type_left;
6336 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6337 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6342 * Check the semantic restrictions of a logical expression.
6344 static void semantic_logical_op(binary_expression_t *expression)
6346 expression_t *const left = expression->left;
6347 expression_t *const right = expression->right;
6348 type_t *const orig_type_left = left->base.type;
6349 type_t *const orig_type_right = right->base.type;
6350 type_t *const type_left = skip_typeref(orig_type_left);
6351 type_t *const type_right = skip_typeref(orig_type_right);
6353 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6354 /* TODO: improve error message */
6355 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6356 errorf(HERE, "operation needs scalar types");
6361 expression->base.type = type_int;
6365 * Checks if a compound type has constant fields.
6367 static bool has_const_fields(const compound_type_t *type)
6369 const scope_t *scope = &type->declaration->scope;
6370 const declaration_t *declaration = scope->declarations;
6372 for (; declaration != NULL; declaration = declaration->next) {
6373 if (declaration->namespc != NAMESPACE_NORMAL)
6376 const type_t *decl_type = skip_typeref(declaration->type);
6377 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6385 * Check the semantic restrictions of a binary assign expression.
6387 static void semantic_binexpr_assign(binary_expression_t *expression)
6389 expression_t *left = expression->left;
6390 type_t *orig_type_left = left->base.type;
6392 type_t *type_left = revert_automatic_type_conversion(left);
6393 type_left = skip_typeref(orig_type_left);
6395 /* must be a modifiable lvalue */
6396 if (is_type_array(type_left)) {
6397 errorf(HERE, "cannot assign to arrays ('%E')", left);
6400 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6401 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6405 if(is_type_incomplete(type_left)) {
6407 "left-hand side of assignment '%E' has incomplete type '%T'",
6408 left, orig_type_left);
6411 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6412 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6413 left, orig_type_left);
6417 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6418 "assignment", &left->base.source_position);
6419 if (res_type == NULL) {
6420 errorf(&expression->base.source_position,
6421 "cannot assign to '%T' from '%T'",
6422 orig_type_left, expression->right->base.type);
6424 expression->right = create_implicit_cast(expression->right, res_type);
6427 expression->base.type = orig_type_left;
6431 * Determine if the outermost operation (or parts thereof) of the given
6432 * expression has no effect in order to generate a warning about this fact.
6433 * Therefore in some cases this only examines some of the operands of the
6434 * expression (see comments in the function and examples below).
6436 * f() + 23; // warning, because + has no effect
6437 * x || f(); // no warning, because x controls execution of f()
6438 * x ? y : f(); // warning, because y has no effect
6439 * (void)x; // no warning to be able to suppress the warning
6440 * This function can NOT be used for an "expression has definitely no effect"-
6442 static bool expression_has_effect(const expression_t *const expr)
6444 switch (expr->kind) {
6445 case EXPR_UNKNOWN: break;
6446 case EXPR_INVALID: return true; /* do NOT warn */
6447 case EXPR_REFERENCE: return false;
6448 /* suppress the warning for microsoft __noop operations */
6449 case EXPR_CONST: return expr->conste.is_ms_noop;
6450 case EXPR_CHARACTER_CONSTANT: return false;
6451 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6452 case EXPR_STRING_LITERAL: return false;
6453 case EXPR_WIDE_STRING_LITERAL: return false;
6456 const call_expression_t *const call = &expr->call;
6457 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6460 switch (call->function->builtin_symbol.symbol->ID) {
6461 case T___builtin_va_end: return true;
6462 default: return false;
6466 /* Generate the warning if either the left or right hand side of a
6467 * conditional expression has no effect */
6468 case EXPR_CONDITIONAL: {
6469 const conditional_expression_t *const cond = &expr->conditional;
6471 expression_has_effect(cond->true_expression) &&
6472 expression_has_effect(cond->false_expression);
6475 case EXPR_SELECT: return false;
6476 case EXPR_ARRAY_ACCESS: return false;
6477 case EXPR_SIZEOF: return false;
6478 case EXPR_CLASSIFY_TYPE: return false;
6479 case EXPR_ALIGNOF: return false;
6481 case EXPR_FUNCNAME: return false;
6482 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6483 case EXPR_BUILTIN_CONSTANT_P: return false;
6484 case EXPR_BUILTIN_PREFETCH: return true;
6485 case EXPR_OFFSETOF: return false;
6486 case EXPR_VA_START: return true;
6487 case EXPR_VA_ARG: return true;
6488 case EXPR_STATEMENT: return true; // TODO
6489 case EXPR_COMPOUND_LITERAL: return false;
6491 case EXPR_UNARY_NEGATE: return false;
6492 case EXPR_UNARY_PLUS: return false;
6493 case EXPR_UNARY_BITWISE_NEGATE: return false;
6494 case EXPR_UNARY_NOT: return false;
6495 case EXPR_UNARY_DEREFERENCE: return false;
6496 case EXPR_UNARY_TAKE_ADDRESS: return false;
6497 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6498 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6499 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6500 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6502 /* Treat void casts as if they have an effect in order to being able to
6503 * suppress the warning */
6504 case EXPR_UNARY_CAST: {
6505 type_t *const type = skip_typeref(expr->base.type);
6506 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6509 case EXPR_UNARY_CAST_IMPLICIT: return true;
6510 case EXPR_UNARY_ASSUME: return true;
6511 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6513 case EXPR_BINARY_ADD: return false;
6514 case EXPR_BINARY_SUB: return false;
6515 case EXPR_BINARY_MUL: return false;
6516 case EXPR_BINARY_DIV: return false;
6517 case EXPR_BINARY_MOD: return false;
6518 case EXPR_BINARY_EQUAL: return false;
6519 case EXPR_BINARY_NOTEQUAL: return false;
6520 case EXPR_BINARY_LESS: return false;
6521 case EXPR_BINARY_LESSEQUAL: return false;
6522 case EXPR_BINARY_GREATER: return false;
6523 case EXPR_BINARY_GREATEREQUAL: return false;
6524 case EXPR_BINARY_BITWISE_AND: return false;
6525 case EXPR_BINARY_BITWISE_OR: return false;
6526 case EXPR_BINARY_BITWISE_XOR: return false;
6527 case EXPR_BINARY_SHIFTLEFT: return false;
6528 case EXPR_BINARY_SHIFTRIGHT: return false;
6529 case EXPR_BINARY_ASSIGN: return true;
6530 case EXPR_BINARY_MUL_ASSIGN: return true;
6531 case EXPR_BINARY_DIV_ASSIGN: return true;
6532 case EXPR_BINARY_MOD_ASSIGN: return true;
6533 case EXPR_BINARY_ADD_ASSIGN: return true;
6534 case EXPR_BINARY_SUB_ASSIGN: return true;
6535 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6536 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6537 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6538 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6539 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6541 /* Only examine the right hand side of && and ||, because the left hand
6542 * side already has the effect of controlling the execution of the right
6544 case EXPR_BINARY_LOGICAL_AND:
6545 case EXPR_BINARY_LOGICAL_OR:
6546 /* Only examine the right hand side of a comma expression, because the left
6547 * hand side has a separate warning */
6548 case EXPR_BINARY_COMMA:
6549 return expression_has_effect(expr->binary.right);
6551 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6552 case EXPR_BINARY_ISGREATER: return false;
6553 case EXPR_BINARY_ISGREATEREQUAL: return false;
6554 case EXPR_BINARY_ISLESS: return false;
6555 case EXPR_BINARY_ISLESSEQUAL: return false;
6556 case EXPR_BINARY_ISLESSGREATER: return false;
6557 case EXPR_BINARY_ISUNORDERED: return false;
6560 internal_errorf(HERE, "unexpected expression");
6563 static void semantic_comma(binary_expression_t *expression)
6565 if (warning.unused_value) {
6566 const expression_t *const left = expression->left;
6567 if (!expression_has_effect(left)) {
6568 warningf(&left->base.source_position,
6569 "left-hand operand of comma expression has no effect");
6572 expression->base.type = expression->right->base.type;
6575 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6576 static expression_t *parse_##binexpression_type(unsigned precedence, \
6577 expression_t *left) \
6580 source_position_t pos = *HERE; \
6582 expression_t *right = parse_sub_expression(precedence + lr); \
6584 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6585 binexpr->base.source_position = pos; \
6586 binexpr->binary.left = left; \
6587 binexpr->binary.right = right; \
6588 sfunc(&binexpr->binary); \
6593 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6594 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6595 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6596 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6597 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6598 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6599 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6600 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6601 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6603 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6604 semantic_comparison, 1)
6605 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6606 semantic_comparison, 1)
6607 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6608 semantic_comparison, 1)
6609 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6610 semantic_comparison, 1)
6612 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6613 semantic_binexpr_arithmetic, 1)
6614 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6615 semantic_binexpr_arithmetic, 1)
6616 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6617 semantic_binexpr_arithmetic, 1)
6618 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6619 semantic_logical_op, 1)
6620 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6621 semantic_logical_op, 1)
6622 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6623 semantic_shift_op, 1)
6624 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6625 semantic_shift_op, 1)
6626 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6627 semantic_arithmetic_addsubb_assign, 0)
6628 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6629 semantic_arithmetic_addsubb_assign, 0)
6630 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6631 semantic_arithmetic_assign, 0)
6632 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6633 semantic_arithmetic_assign, 0)
6634 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6635 semantic_arithmetic_assign, 0)
6636 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6637 semantic_arithmetic_assign, 0)
6638 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6639 semantic_arithmetic_assign, 0)
6640 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6641 semantic_arithmetic_assign, 0)
6642 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6643 semantic_arithmetic_assign, 0)
6644 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6645 semantic_arithmetic_assign, 0)
6647 static expression_t *parse_sub_expression(unsigned precedence)
6649 if(token.type < 0) {
6650 return expected_expression_error();
6653 expression_parser_function_t *parser
6654 = &expression_parsers[token.type];
6655 source_position_t source_position = token.source_position;
6658 if(parser->parser != NULL) {
6659 left = parser->parser(parser->precedence);
6661 left = parse_primary_expression();
6663 assert(left != NULL);
6664 left->base.source_position = source_position;
6667 if(token.type < 0) {
6668 return expected_expression_error();
6671 parser = &expression_parsers[token.type];
6672 if(parser->infix_parser == NULL)
6674 if(parser->infix_precedence < precedence)
6677 left = parser->infix_parser(parser->infix_precedence, left);
6679 assert(left != NULL);
6680 assert(left->kind != EXPR_UNKNOWN);
6681 left->base.source_position = source_position;
6688 * Parse an expression.
6690 static expression_t *parse_expression(void)
6692 return parse_sub_expression(1);
6696 * Register a parser for a prefix-like operator with given precedence.
6698 * @param parser the parser function
6699 * @param token_type the token type of the prefix token
6700 * @param precedence the precedence of the operator
6702 static void register_expression_parser(parse_expression_function parser,
6703 int token_type, unsigned precedence)
6705 expression_parser_function_t *entry = &expression_parsers[token_type];
6707 if(entry->parser != NULL) {
6708 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6709 panic("trying to register multiple expression parsers for a token");
6711 entry->parser = parser;
6712 entry->precedence = precedence;
6716 * Register a parser for an infix operator with given precedence.
6718 * @param parser the parser function
6719 * @param token_type the token type of the infix operator
6720 * @param precedence the precedence of the operator
6722 static void register_infix_parser(parse_expression_infix_function parser,
6723 int token_type, unsigned precedence)
6725 expression_parser_function_t *entry = &expression_parsers[token_type];
6727 if(entry->infix_parser != NULL) {
6728 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6729 panic("trying to register multiple infix expression parsers for a "
6732 entry->infix_parser = parser;
6733 entry->infix_precedence = precedence;
6737 * Initialize the expression parsers.
6739 static void init_expression_parsers(void)
6741 memset(&expression_parsers, 0, sizeof(expression_parsers));
6743 register_infix_parser(parse_array_expression, '[', 30);
6744 register_infix_parser(parse_call_expression, '(', 30);
6745 register_infix_parser(parse_select_expression, '.', 30);
6746 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6747 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6749 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6752 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6753 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6754 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6755 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6756 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6757 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6758 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6759 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6760 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6761 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6762 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6763 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6764 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6765 T_EXCLAMATIONMARKEQUAL, 13);
6766 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6767 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6768 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6769 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6770 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6771 register_infix_parser(parse_conditional_expression, '?', 7);
6772 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6773 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6774 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6775 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6776 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6777 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6778 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6779 T_LESSLESSEQUAL, 2);
6780 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6781 T_GREATERGREATEREQUAL, 2);
6782 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6784 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6786 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6789 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6791 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6792 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6793 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6794 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6795 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6796 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6797 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6799 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6801 register_expression_parser(parse_sizeof, T_sizeof, 25);
6802 register_expression_parser(parse_alignof, T___alignof__, 25);
6803 register_expression_parser(parse_extension, T___extension__, 25);
6804 register_expression_parser(parse_builtin_classify_type,
6805 T___builtin_classify_type, 25);
6809 * Parse a asm statement constraints specification.
6811 static asm_constraint_t *parse_asm_constraints(void)
6813 asm_constraint_t *result = NULL;
6814 asm_constraint_t *last = NULL;
6816 while(token.type == T_STRING_LITERAL || token.type == '[') {
6817 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6818 memset(constraint, 0, sizeof(constraint[0]));
6820 if(token.type == '[') {
6822 if(token.type != T_IDENTIFIER) {
6823 parse_error_expected("while parsing asm constraint",
6827 constraint->symbol = token.v.symbol;
6832 constraint->constraints = parse_string_literals();
6834 constraint->expression = parse_expression();
6838 last->next = constraint;
6840 result = constraint;
6844 if(token.type != ',')
6855 * Parse a asm statement clobber specification.
6857 static asm_clobber_t *parse_asm_clobbers(void)
6859 asm_clobber_t *result = NULL;
6860 asm_clobber_t *last = NULL;
6862 while(token.type == T_STRING_LITERAL) {
6863 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6864 clobber->clobber = parse_string_literals();
6867 last->next = clobber;
6873 if(token.type != ',')
6882 * Parse an asm statement.
6884 static statement_t *parse_asm_statement(void)
6888 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6889 statement->base.source_position = token.source_position;
6891 asm_statement_t *asm_statement = &statement->asms;
6893 if(token.type == T_volatile) {
6895 asm_statement->is_volatile = true;
6899 add_anchor_token(')');
6900 add_anchor_token(':');
6901 asm_statement->asm_text = parse_string_literals();
6903 if(token.type != ':') {
6904 rem_anchor_token(':');
6909 asm_statement->inputs = parse_asm_constraints();
6910 if(token.type != ':') {
6911 rem_anchor_token(':');
6916 asm_statement->outputs = parse_asm_constraints();
6917 if(token.type != ':') {
6918 rem_anchor_token(':');
6921 rem_anchor_token(':');
6924 asm_statement->clobbers = parse_asm_clobbers();
6927 rem_anchor_token(')');
6932 return create_invalid_statement();
6936 * Parse a case statement.
6938 static statement_t *parse_case_statement(void)
6942 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6944 statement->base.source_position = token.source_position;
6945 statement->case_label.expression = parse_expression();
6947 if (c_mode & _GNUC) {
6948 if (token.type == T_DOTDOTDOT) {
6950 statement->case_label.end_range = parse_expression();
6956 if (! is_constant_expression(statement->case_label.expression)) {
6957 errorf(&statement->base.source_position,
6958 "case label does not reduce to an integer constant");
6960 /* TODO: check if the case label is already known */
6961 if (current_switch != NULL) {
6962 /* link all cases into the switch statement */
6963 if (current_switch->last_case == NULL) {
6964 current_switch->first_case =
6965 current_switch->last_case = &statement->case_label;
6967 current_switch->last_case->next = &statement->case_label;
6970 errorf(&statement->base.source_position,
6971 "case label not within a switch statement");
6974 statement->case_label.statement = parse_statement();
6978 return create_invalid_statement();
6982 * Finds an existing default label of a switch statement.
6984 static case_label_statement_t *
6985 find_default_label(const switch_statement_t *statement)
6987 case_label_statement_t *label = statement->first_case;
6988 for ( ; label != NULL; label = label->next) {
6989 if (label->expression == NULL)
6996 * Parse a default statement.
6998 static statement_t *parse_default_statement(void)
7002 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7004 statement->base.source_position = token.source_position;
7007 if (current_switch != NULL) {
7008 const case_label_statement_t *def_label = find_default_label(current_switch);
7009 if (def_label != NULL) {
7010 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7011 &def_label->base.source_position);
7013 /* link all cases into the switch statement */
7014 if (current_switch->last_case == NULL) {
7015 current_switch->first_case =
7016 current_switch->last_case = &statement->case_label;
7018 current_switch->last_case->next = &statement->case_label;
7022 errorf(&statement->base.source_position,
7023 "'default' label not within a switch statement");
7025 statement->case_label.statement = parse_statement();
7029 return create_invalid_statement();
7033 * Return the declaration for a given label symbol or create a new one.
7035 static declaration_t *get_label(symbol_t *symbol)
7037 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7038 assert(current_function != NULL);
7039 /* if we found a label in the same function, then we already created the
7041 if(candidate != NULL
7042 && candidate->parent_scope == ¤t_function->scope) {
7046 /* otherwise we need to create a new one */
7047 declaration_t *const declaration = allocate_declaration_zero();
7048 declaration->namespc = NAMESPACE_LABEL;
7049 declaration->symbol = symbol;
7051 label_push(declaration);
7057 * Parse a label statement.
7059 static statement_t *parse_label_statement(void)
7061 assert(token.type == T_IDENTIFIER);
7062 symbol_t *symbol = token.v.symbol;
7065 declaration_t *label = get_label(symbol);
7067 /* if source position is already set then the label is defined twice,
7068 * otherwise it was just mentioned in a goto so far */
7069 if(label->source_position.input_name != NULL) {
7070 errorf(HERE, "duplicate label '%Y' (declared %P)",
7071 symbol, &label->source_position);
7073 label->source_position = token.source_position;
7076 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7078 statement->base.source_position = token.source_position;
7079 statement->label.label = label;
7083 if(token.type == '}') {
7084 /* TODO only warn? */
7086 warningf(HERE, "label at end of compound statement");
7087 statement->label.statement = create_empty_statement();
7089 errorf(HERE, "label at end of compound statement");
7090 statement->label.statement = create_invalid_statement();
7094 if (token.type == ';') {
7095 /* eat an empty statement here, to avoid the warning about an empty
7096 * after a label. label:; is commonly used to have a label before
7098 statement->label.statement = create_empty_statement();
7101 statement->label.statement = parse_statement();
7105 /* remember the labels's in a list for later checking */
7106 if (label_last == NULL) {
7107 label_first = &statement->label;
7109 label_last->next = &statement->label;
7111 label_last = &statement->label;
7117 * Parse an if statement.
7119 static statement_t *parse_if(void)
7123 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7124 statement->base.source_position = token.source_position;
7127 add_anchor_token(')');
7128 statement->ifs.condition = parse_expression();
7129 rem_anchor_token(')');
7132 add_anchor_token(T_else);
7133 statement->ifs.true_statement = parse_statement();
7134 rem_anchor_token(T_else);
7136 if(token.type == T_else) {
7138 statement->ifs.false_statement = parse_statement();
7143 return create_invalid_statement();
7147 * Parse a switch statement.
7149 static statement_t *parse_switch(void)
7153 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7154 statement->base.source_position = token.source_position;
7157 expression_t *const expr = parse_expression();
7158 type_t * type = skip_typeref(expr->base.type);
7159 if (is_type_integer(type)) {
7160 type = promote_integer(type);
7161 } else if (is_type_valid(type)) {
7162 errorf(&expr->base.source_position,
7163 "switch quantity is not an integer, but '%T'", type);
7164 type = type_error_type;
7166 statement->switchs.expression = create_implicit_cast(expr, type);
7169 switch_statement_t *rem = current_switch;
7170 current_switch = &statement->switchs;
7171 statement->switchs.body = parse_statement();
7172 current_switch = rem;
7174 if(warning.switch_default &&
7175 find_default_label(&statement->switchs) == NULL) {
7176 warningf(&statement->base.source_position, "switch has no default case");
7181 return create_invalid_statement();
7184 static statement_t *parse_loop_body(statement_t *const loop)
7186 statement_t *const rem = current_loop;
7187 current_loop = loop;
7189 statement_t *const body = parse_statement();
7196 * Parse a while statement.
7198 static statement_t *parse_while(void)
7202 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7203 statement->base.source_position = token.source_position;
7206 add_anchor_token(')');
7207 statement->whiles.condition = parse_expression();
7208 rem_anchor_token(')');
7211 statement->whiles.body = parse_loop_body(statement);
7215 return create_invalid_statement();
7219 * Parse a do statement.
7221 static statement_t *parse_do(void)
7225 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7227 statement->base.source_position = token.source_position;
7229 add_anchor_token(T_while);
7230 statement->do_while.body = parse_loop_body(statement);
7231 rem_anchor_token(T_while);
7235 add_anchor_token(')');
7236 statement->do_while.condition = parse_expression();
7237 rem_anchor_token(')');
7243 return create_invalid_statement();
7247 * Parse a for statement.
7249 static statement_t *parse_for(void)
7253 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7254 statement->base.source_position = token.source_position;
7256 int top = environment_top();
7257 scope_t *last_scope = scope;
7258 set_scope(&statement->fors.scope);
7261 add_anchor_token(')');
7263 if(token.type != ';') {
7264 if(is_declaration_specifier(&token, false)) {
7265 parse_declaration(record_declaration);
7267 add_anchor_token(';');
7268 expression_t *const init = parse_expression();
7269 statement->fors.initialisation = init;
7270 if (warning.unused_value && !expression_has_effect(init)) {
7271 warningf(&init->base.source_position,
7272 "initialisation of 'for'-statement has no effect");
7274 rem_anchor_token(';');
7281 if(token.type != ';') {
7282 add_anchor_token(';');
7283 statement->fors.condition = parse_expression();
7284 rem_anchor_token(';');
7287 if(token.type != ')') {
7288 expression_t *const step = parse_expression();
7289 statement->fors.step = step;
7290 if (warning.unused_value && !expression_has_effect(step)) {
7291 warningf(&step->base.source_position,
7292 "step of 'for'-statement has no effect");
7295 rem_anchor_token(')');
7297 statement->fors.body = parse_loop_body(statement);
7299 assert(scope == &statement->fors.scope);
7300 set_scope(last_scope);
7301 environment_pop_to(top);
7306 rem_anchor_token(')');
7307 assert(scope == &statement->fors.scope);
7308 set_scope(last_scope);
7309 environment_pop_to(top);
7311 return create_invalid_statement();
7315 * Parse a goto statement.
7317 static statement_t *parse_goto(void)
7321 if(token.type != T_IDENTIFIER) {
7322 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
7326 symbol_t *symbol = token.v.symbol;
7329 declaration_t *label = get_label(symbol);
7331 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7332 statement->base.source_position = token.source_position;
7334 statement->gotos.label = label;
7336 /* remember the goto's in a list for later checking */
7337 if (goto_last == NULL) {
7338 goto_first = &statement->gotos;
7340 goto_last->next = &statement->gotos;
7342 goto_last = &statement->gotos;
7348 return create_invalid_statement();
7352 * Parse a continue statement.
7354 static statement_t *parse_continue(void)
7356 statement_t *statement;
7357 if (current_loop == NULL) {
7358 errorf(HERE, "continue statement not within loop");
7361 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7363 statement->base.source_position = token.source_position;
7371 return create_invalid_statement();
7375 * Parse a break statement.
7377 static statement_t *parse_break(void)
7379 statement_t *statement;
7380 if (current_switch == NULL && current_loop == NULL) {
7381 errorf(HERE, "break statement not within loop or switch");
7384 statement = allocate_statement_zero(STATEMENT_BREAK);
7386 statement->base.source_position = token.source_position;
7394 return create_invalid_statement();
7398 * Check if a given declaration represents a local variable.
7400 static bool is_local_var_declaration(const declaration_t *declaration) {
7401 switch ((storage_class_tag_t) declaration->storage_class) {
7402 case STORAGE_CLASS_AUTO:
7403 case STORAGE_CLASS_REGISTER: {
7404 const type_t *type = skip_typeref(declaration->type);
7405 if(is_type_function(type)) {
7417 * Check if a given declaration represents a variable.
7419 static bool is_var_declaration(const declaration_t *declaration) {
7420 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7423 const type_t *type = skip_typeref(declaration->type);
7424 return !is_type_function(type);
7428 * Check if a given expression represents a local variable.
7430 static bool is_local_variable(const expression_t *expression)
7432 if (expression->base.kind != EXPR_REFERENCE) {
7435 const declaration_t *declaration = expression->reference.declaration;
7436 return is_local_var_declaration(declaration);
7440 * Check if a given expression represents a local variable and
7441 * return its declaration then, else return NULL.
7443 declaration_t *expr_is_variable(const expression_t *expression)
7445 if (expression->base.kind != EXPR_REFERENCE) {
7448 declaration_t *declaration = expression->reference.declaration;
7449 if (is_var_declaration(declaration))
7455 * Parse a return statement.
7457 static statement_t *parse_return(void)
7459 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7460 statement->base.source_position = token.source_position;
7464 expression_t *return_value = NULL;
7465 if(token.type != ';') {
7466 return_value = parse_expression();
7470 const type_t *const func_type = current_function->type;
7471 assert(is_type_function(func_type));
7472 type_t *const return_type = skip_typeref(func_type->function.return_type);
7474 if(return_value != NULL) {
7475 type_t *return_value_type = skip_typeref(return_value->base.type);
7477 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7478 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7479 warningf(&statement->base.source_position,
7480 "'return' with a value, in function returning void");
7481 return_value = NULL;
7483 type_t *const res_type = semantic_assign(return_type,
7484 return_value, "'return'", &statement->base.source_position);
7485 if (res_type == NULL) {
7486 errorf(&statement->base.source_position,
7487 "cannot return something of type '%T' in function returning '%T'",
7488 return_value->base.type, return_type);
7490 return_value = create_implicit_cast(return_value, res_type);
7493 /* check for returning address of a local var */
7494 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7495 const expression_t *expression = return_value->unary.value;
7496 if (is_local_variable(expression)) {
7497 warningf(&statement->base.source_position,
7498 "function returns address of local variable");
7502 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7503 warningf(&statement->base.source_position,
7504 "'return' without value, in function returning non-void");
7507 statement->returns.value = return_value;
7511 return create_invalid_statement();
7515 * Parse a declaration statement.
7517 static statement_t *parse_declaration_statement(void)
7519 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7521 statement->base.source_position = token.source_position;
7523 declaration_t *before = last_declaration;
7524 parse_declaration(record_declaration);
7526 if(before == NULL) {
7527 statement->declaration.declarations_begin = scope->declarations;
7529 statement->declaration.declarations_begin = before->next;
7531 statement->declaration.declarations_end = last_declaration;
7537 * Parse an expression statement, ie. expr ';'.
7539 static statement_t *parse_expression_statement(void)
7541 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7543 statement->base.source_position = token.source_position;
7544 expression_t *const expr = parse_expression();
7545 statement->expression.expression = expr;
7547 if (warning.unused_value && !expression_has_effect(expr)) {
7548 warningf(&expr->base.source_position, "statement has no effect");
7555 return create_invalid_statement();
7559 * Parse a statement.
7561 static statement_t *parse_statement(void)
7563 statement_t *statement = NULL;
7565 /* declaration or statement */
7566 add_anchor_token(';');
7567 switch(token.type) {
7569 statement = parse_asm_statement();
7573 statement = parse_case_statement();
7577 statement = parse_default_statement();
7581 statement = parse_compound_statement();
7585 statement = parse_if();
7589 statement = parse_switch();
7593 statement = parse_while();
7597 statement = parse_do();
7601 statement = parse_for();
7605 statement = parse_goto();
7609 statement = parse_continue();
7613 statement = parse_break();
7617 statement = parse_return();
7621 if(warning.empty_statement) {
7622 warningf(HERE, "statement is empty");
7624 statement = create_empty_statement();
7629 if(look_ahead(1)->type == ':') {
7630 statement = parse_label_statement();
7634 if(is_typedef_symbol(token.v.symbol)) {
7635 statement = parse_declaration_statement();
7639 statement = parse_expression_statement();
7642 case T___extension__:
7643 /* this can be a prefix to a declaration or an expression statement */
7644 /* we simply eat it now and parse the rest with tail recursion */
7647 } while(token.type == T___extension__);
7648 statement = parse_statement();
7652 statement = parse_declaration_statement();
7656 statement = parse_expression_statement();
7659 rem_anchor_token(';');
7661 assert(statement != NULL
7662 && statement->base.source_position.input_name != NULL);
7668 * Parse a compound statement.
7670 static statement_t *parse_compound_statement(void)
7672 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7674 statement->base.source_position = token.source_position;
7677 add_anchor_token('}');
7679 int top = environment_top();
7680 scope_t *last_scope = scope;
7681 set_scope(&statement->compound.scope);
7683 statement_t *last_statement = NULL;
7685 while(token.type != '}' && token.type != T_EOF) {
7686 statement_t *sub_statement = parse_statement();
7687 if(is_invalid_statement(sub_statement)) {
7688 /* an error occurred. if we are at an anchor, return */
7694 if(last_statement != NULL) {
7695 last_statement->base.next = sub_statement;
7697 statement->compound.statements = sub_statement;
7700 while(sub_statement->base.next != NULL)
7701 sub_statement = sub_statement->base.next;
7703 last_statement = sub_statement;
7706 if(token.type == '}') {
7709 errorf(&statement->base.source_position,
7710 "end of file while looking for closing '}'");
7714 rem_anchor_token('}');
7715 assert(scope == &statement->compound.scope);
7716 set_scope(last_scope);
7717 environment_pop_to(top);
7723 * Initialize builtin types.
7725 static void initialize_builtin_types(void)
7727 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7728 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7729 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7730 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7731 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7732 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7733 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7734 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7736 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7737 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7738 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7739 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7743 * Check for unused global static functions and variables
7745 static void check_unused_globals(void)
7747 if (!warning.unused_function && !warning.unused_variable)
7750 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7751 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7754 type_t *const type = decl->type;
7756 if (is_type_function(skip_typeref(type))) {
7757 if (!warning.unused_function || decl->is_inline)
7760 s = (decl->init.statement != NULL ? "defined" : "declared");
7762 if (!warning.unused_variable)
7768 warningf(&decl->source_position, "'%#T' %s but not used",
7769 type, decl->symbol, s);
7774 * Parse a translation unit.
7776 static translation_unit_t *parse_translation_unit(void)
7778 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7780 assert(global_scope == NULL);
7781 global_scope = &unit->scope;
7783 assert(scope == NULL);
7784 set_scope(&unit->scope);
7786 initialize_builtin_types();
7788 while(token.type != T_EOF) {
7789 if (token.type == ';') {
7790 /* TODO error in strict mode */
7791 warningf(HERE, "stray ';' outside of function");
7794 parse_external_declaration();
7798 assert(scope == &unit->scope);
7800 last_declaration = NULL;
7802 assert(global_scope == &unit->scope);
7803 check_unused_globals();
7804 global_scope = NULL;
7812 * @return the translation unit or NULL if errors occurred.
7814 translation_unit_t *parse(void)
7816 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7817 label_stack = NEW_ARR_F(stack_entry_t, 0);
7818 diagnostic_count = 0;
7822 type_set_output(stderr);
7823 ast_set_output(stderr);
7825 lookahead_bufpos = 0;
7826 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7829 translation_unit_t *unit = parse_translation_unit();
7831 DEL_ARR_F(environment_stack);
7832 DEL_ARR_F(label_stack);
7838 * Initialize the parser.
7840 void init_parser(void)
7843 /* add predefined symbols for extended-decl-modifier */
7844 sym_align = symbol_table_insert("align");
7845 sym_allocate = symbol_table_insert("allocate");
7846 sym_dllimport = symbol_table_insert("dllimport");
7847 sym_dllexport = symbol_table_insert("dllexport");
7848 sym_naked = symbol_table_insert("naked");
7849 sym_noinline = symbol_table_insert("noinline");
7850 sym_noreturn = symbol_table_insert("noreturn");
7851 sym_nothrow = symbol_table_insert("nothrow");
7852 sym_novtable = symbol_table_insert("novtable");
7853 sym_property = symbol_table_insert("property");
7854 sym_get = symbol_table_insert("get");
7855 sym_put = symbol_table_insert("put");
7856 sym_selectany = symbol_table_insert("selectany");
7857 sym_thread = symbol_table_insert("thread");
7858 sym_uuid = symbol_table_insert("uuid");
7859 sym_deprecated = symbol_table_insert("deprecated");
7860 sym_restrict = symbol_table_insert("restrict");
7861 sym_noalias = symbol_table_insert("noalias");
7863 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7865 init_expression_parsers();
7866 obstack_init(&temp_obst);
7868 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7869 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7873 * Terminate the parser.
7875 void exit_parser(void)
7877 obstack_free(&temp_obst, NULL);