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;
1076 * parse one constant expression argument.
1078 static void parse_gnu_attribute_const_arg(gnu_attribute_t *attribute) {
1079 expression_t *expression;
1080 add_anchor_token(')');
1081 expression = parse_constant_expression();
1082 rem_anchor_token(')');
1087 attribute->invalid = true;
1091 * parse a list of constant expressions arguments.
1093 static void parse_gnu_attribute_const_arg_list(gnu_attribute_t *attribute) {
1094 expression_t *expression;
1095 add_anchor_token(')');
1096 add_anchor_token(',');
1098 expression = parse_constant_expression();
1099 if(token.type != ',')
1103 rem_anchor_token(',');
1104 rem_anchor_token(')');
1109 attribute->invalid = true;
1113 * parse one string literal argument.
1115 static void parse_gnu_attribute_string_arg(gnu_attribute_t *attribute, string_t *string) {
1116 add_anchor_token('(');
1117 if(token.type != T_STRING_LITERAL) {
1118 parse_error_expected("while parsing attribute directive", T_STRING_LITERAL);
1121 *string = parse_string_literals();
1122 rem_anchor_token('(');
1126 attribute->invalid = true;
1130 * parse one tls model.
1132 static void parse_gnu_attribute_tls_model_arg(gnu_attribute_t *attribute) {
1133 static const char *tls_models[] = {
1139 string_t string = { NULL, 0 };
1140 parse_gnu_attribute_string_arg(attribute, &string);
1141 if(string.begin != NULL) {
1142 for(size_t i = 0; i < 4; ++i) {
1143 if(strcmp(tls_models[i], string.begin) == 0) {
1144 attribute->u.value = i;
1149 errorf(HERE, "'%s' is an unrecognized tls model", string.begin);
1150 attribute->invalid = true;
1154 * parse one tls model.
1156 static void parse_gnu_attribute_visibility_arg(gnu_attribute_t *attribute) {
1157 static const char *visibilities[] = {
1163 string_t string = { NULL, 0 };
1164 parse_gnu_attribute_string_arg(attribute, &string);
1165 if(string.begin != NULL) {
1166 for(size_t i = 0; i < 4; ++i) {
1167 if(strcmp(visibilities[i], string.begin) == 0) {
1168 attribute->u.value = i;
1173 errorf(HERE, "'%s' is an unrecognized visibility", string.begin);
1174 attribute->invalid = true;
1178 * parse one (code) model.
1180 static void parse_gnu_attribute_model_arg(gnu_attribute_t *attribute) {
1181 static const char *visibilities[] = {
1186 string_t string = { NULL, 0 };
1187 parse_gnu_attribute_string_arg(attribute, &string);
1188 if(string.begin != NULL) {
1189 for(int i = 0; i < 3; ++i) {
1190 if(strcmp(visibilities[i], string.begin) == 0) {
1191 attribute->u.value = i;
1196 errorf(HERE, "'%s' is an unrecognized model", string.begin);
1197 attribute->invalid = true;
1201 * parse one interrupt argument.
1203 static void parse_gnu_attribute_interrupt_arg(gnu_attribute_t *attribute) {
1204 static const char *interrupts[] = {
1211 string_t string = { NULL, 0 };
1212 parse_gnu_attribute_string_arg(attribute, &string);
1213 if(string.begin != NULL) {
1214 for(size_t i = 0; i < 5; ++i) {
1215 if(strcmp(interrupts[i], string.begin) == 0) {
1216 attribute->u.value = i;
1221 errorf(HERE, "'%s' is an interrupt", string.begin);
1222 attribute->invalid = true;
1226 * parse ( identifier, const expression, const expression )
1228 static void parse_gnu_attribute_format_args(gnu_attribute_t *attribute) {
1229 static const char *format_names[] = {
1237 if(token.type != T_IDENTIFIER) {
1238 parse_error_expected("while parsing format attribute directive", T_IDENTIFIER);
1241 const char *name = token.v.symbol->string;
1242 for(i = 0; i < 4; ++i) {
1243 if(strcmp_underscore(format_names[i], name) == 0)
1247 if(warning.attribute)
1248 warningf(HERE, "'%s' is an unrecognized format function type", name);
1253 add_anchor_token(')');
1254 add_anchor_token(',');
1255 parse_constant_expression();
1256 rem_anchor_token(',');
1257 rem_anchor_token('(');
1260 add_anchor_token(')');
1261 parse_constant_expression();
1262 rem_anchor_token('(');
1266 attribute->u.value = true;
1270 * Parse one GNU attribute.
1272 * Note that attribute names can be specified WITH or WITHOUT
1273 * double underscores, ie const or __const__.
1275 * The following attributes are parsed without arguments
1300 * no_instrument_function
1301 * warn_unused_result
1318 * externally_visible
1326 * The following attributes are parsed with arguments
1327 * aligned( const expression )
1328 * alias( string literal )
1329 * section( string literal )
1330 * format( identifier, const expression, const expression )
1331 * format_arg( const expression )
1332 * tls_model( string literal )
1333 * visibility( string literal )
1334 * regparm( const expression )
1335 * model( string leteral )
1336 * trap_exit( const expression )
1337 * sp_switch( string literal )
1339 * The following attributes might have arguments
1340 * weak_ref( string literal )
1341 * non_null( const expression // ',' )
1342 * interrupt( string literal )
1343 * sentinel( constant expression )
1345 static void parse_gnu_attribute(gnu_attribute_t **attributes)
1347 gnu_attribute_t *head = *attributes;
1348 gnu_attribute_t *last = *attributes;
1349 gnu_attribute_t *attribute;
1351 eat(T___attribute__);
1355 if(token.type != ')') {
1356 /* find the end of the list */
1358 while(last->next != NULL)
1362 /* non-empty attribute list */
1365 if(token.type == T_const) {
1367 } else if(token.type == T_volatile) {
1369 } else if(token.type == T_cdecl) {
1370 /* __attribute__((cdecl)), WITH ms mode */
1372 } else if(token.type != T_IDENTIFIER) {
1373 parse_error_expected("while parsing GNU attribute", T_IDENTIFIER);
1376 const symbol_t *sym = token.v.symbol;
1381 for(i = 0; i < GNU_AK_LAST; ++i) {
1382 if(strcmp_underscore(gnu_attribute_names[i], name) == 0)
1385 gnu_attribute_kind_t kind = (gnu_attribute_kind_t)i;
1388 if(kind == GNU_AK_LAST) {
1389 if(warning.attribute)
1390 warningf(HERE, "'%s' attribute directive ignored", name);
1392 /* skip possible arguments */
1393 if(token.type == '(') {
1394 eat_until_matching_token(')');
1397 /* check for arguments */
1398 attribute = allocate_gnu_attribute(kind);
1399 if(token.type == '(') {
1401 if(token.type == ')') {
1402 /* empty args are allowed */
1405 attribute->have_arguments = true;
1410 case GNU_AK_VOLATILE:
1412 case GNU_AK_STDCALL:
1413 case GNU_AK_FASTCALL:
1414 case GNU_AK_DEPRECATED:
1415 case GNU_AK_NOINLINE:
1416 case GNU_AK_NORETURN:
1419 case GNU_AK_ALWAYS_INLINE:
1422 case GNU_AK_CONSTRUCTOR:
1423 case GNU_AK_DESTRUCTOR:
1424 case GNU_AK_NOTHROW:
1425 case GNU_AK_TRANSPARENT_UNION:
1427 case GNU_AK_NOCOMMON:
1430 case GNU_AK_NOTSHARED:
1433 case GNU_AK_NO_INSTRUMENT_FUNCTION:
1434 case GNU_AK_WARN_UNUSED_RESULT:
1435 case GNU_AK_LONGCALL:
1436 case GNU_AK_SHORTCALL:
1437 case GNU_AK_LONG_CALL:
1438 case GNU_AK_SHORT_CALL:
1439 case GNU_AK_FUNCTION_VECTOR:
1440 case GNU_AK_INTERRUPT_HANDLER:
1441 case GNU_AK_NMI_HANDLER:
1442 case GNU_AK_NESTING:
1446 case GNU_AK_EIGTHBIT_DATA:
1447 case GNU_AK_TINY_DATA:
1448 case GNU_AK_SAVEALL:
1449 case GNU_AK_FLATTEN:
1450 case GNU_AK_SSEREGPARM:
1451 case GNU_AK_EXTERNALLY_VISIBLE:
1452 case GNU_AK_RETURN_TWICE:
1453 case GNU_AK_MAY_ALIAS:
1454 case GNU_AK_MS_STRUCT:
1455 case GNU_AK_GCC_STRUCT:
1456 case GNU_AK_DLLIMPORT:
1457 case GNU_AK_DLLEXPORT:
1458 if(attribute->have_arguments) {
1459 /* should have no arguments */
1460 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1461 eat_until_matching_token('(');
1462 /* we have already consumed '(', so we stop before ')', eat it */
1464 attribute->invalid = true;
1468 case GNU_AK_ALIGNED:
1469 case GNU_AK_FORMAT_ARG:
1470 case GNU_AK_REGPARM:
1471 case GNU_AK_TRAP_EXIT:
1472 if(!attribute->have_arguments) {
1473 /* should have arguments */
1474 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1475 attribute->invalid = true;
1477 parse_gnu_attribute_const_arg(attribute);
1480 case GNU_AK_SECTION:
1481 case GNU_AK_SP_SWITCH:
1482 if(!attribute->have_arguments) {
1483 /* should have arguments */
1484 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1485 attribute->invalid = true;
1487 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1490 if(!attribute->have_arguments) {
1491 /* should have arguments */
1492 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1493 attribute->invalid = true;
1495 parse_gnu_attribute_format_args(attribute);
1497 case GNU_AK_WEAKREF:
1498 /* may have one string argument */
1499 if(attribute->have_arguments)
1500 parse_gnu_attribute_string_arg(attribute, &attribute->u.string);
1502 case GNU_AK_NONNULL:
1503 if(attribute->have_arguments)
1504 parse_gnu_attribute_const_arg_list(attribute);
1506 case GNU_AK_TLS_MODEL:
1507 if(!attribute->have_arguments) {
1508 /* should have arguments */
1509 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1511 parse_gnu_attribute_tls_model_arg(attribute);
1513 case GNU_AK_VISIBILITY:
1514 if(!attribute->have_arguments) {
1515 /* should have arguments */
1516 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1518 parse_gnu_attribute_visibility_arg(attribute);
1521 if(!attribute->have_arguments) {
1522 /* should have arguments */
1523 errorf(HERE, "wrong number of arguments specified for '%s' attribute", name);
1525 parse_gnu_attribute_model_arg(attribute);
1526 case GNU_AK_INTERRUPT:
1527 /* may have one string argument */
1528 if(attribute->have_arguments)
1529 parse_gnu_attribute_interrupt_arg(attribute);
1531 case GNU_AK_SENTINEL:
1532 /* may have one string argument */
1533 if(attribute->have_arguments)
1534 parse_gnu_attribute_const_arg(attribute);
1537 /* already handled */
1541 if(attribute != NULL) {
1543 last->next = attribute;
1546 head = last = attribute;
1550 if(token.type != ',')
1562 * Parse GNU attributes.
1564 static void parse_attributes(gnu_attribute_t **attributes)
1567 switch(token.type) {
1568 case T___attribute__: {
1569 parse_gnu_attribute(attributes);
1575 if(token.type != T_STRING_LITERAL) {
1576 parse_error_expected("while parsing assembler attribute",
1578 eat_until_matching_token('(');
1581 parse_string_literals();
1586 goto attributes_finished;
1590 attributes_finished:
1595 static designator_t *parse_designation(void)
1597 designator_t *result = NULL;
1598 designator_t *last = NULL;
1601 designator_t *designator;
1602 switch(token.type) {
1604 designator = allocate_ast_zero(sizeof(designator[0]));
1605 designator->source_position = token.source_position;
1607 add_anchor_token(']');
1608 designator->array_index = parse_constant_expression();
1609 rem_anchor_token(']');
1613 designator = allocate_ast_zero(sizeof(designator[0]));
1614 designator->source_position = token.source_position;
1616 if(token.type != T_IDENTIFIER) {
1617 parse_error_expected("while parsing designator",
1621 designator->symbol = token.v.symbol;
1629 assert(designator != NULL);
1631 last->next = designator;
1633 result = designator;
1641 static initializer_t *initializer_from_string(array_type_t *type,
1642 const string_t *const string)
1644 /* TODO: check len vs. size of array type */
1647 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1648 initializer->string.string = *string;
1653 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1654 wide_string_t *const string)
1656 /* TODO: check len vs. size of array type */
1659 initializer_t *const initializer =
1660 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1661 initializer->wide_string.string = *string;
1667 * Build an initializer from a given expression.
1669 static initializer_t *initializer_from_expression(type_t *orig_type,
1670 expression_t *expression)
1672 /* TODO check that expression is a constant expression */
1674 /* § 6.7.8.14/15 char array may be initialized by string literals */
1675 type_t *type = skip_typeref(orig_type);
1676 type_t *expr_type_orig = expression->base.type;
1677 type_t *expr_type = skip_typeref(expr_type_orig);
1678 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1679 array_type_t *const array_type = &type->array;
1680 type_t *const element_type = skip_typeref(array_type->element_type);
1682 if (element_type->kind == TYPE_ATOMIC) {
1683 atomic_type_kind_t akind = element_type->atomic.akind;
1684 switch (expression->kind) {
1685 case EXPR_STRING_LITERAL:
1686 if (akind == ATOMIC_TYPE_CHAR
1687 || akind == ATOMIC_TYPE_SCHAR
1688 || akind == ATOMIC_TYPE_UCHAR) {
1689 return initializer_from_string(array_type,
1690 &expression->string.value);
1693 case EXPR_WIDE_STRING_LITERAL: {
1694 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1695 if (get_unqualified_type(element_type) == bare_wchar_type) {
1696 return initializer_from_wide_string(array_type,
1697 &expression->wide_string.value);
1707 type_t *const res_type = semantic_assign(type, expression, "initializer",
1708 &expression->base.source_position);
1709 if (res_type == NULL)
1712 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1713 result->value.value = create_implicit_cast(expression, res_type);
1719 * Checks if a given expression can be used as an constant initializer.
1721 static bool is_initializer_constant(const expression_t *expression)
1723 return is_constant_expression(expression)
1724 || is_address_constant(expression);
1728 * Parses an scalar initializer.
1730 * § 6.7.8.11; eat {} without warning
1732 static initializer_t *parse_scalar_initializer(type_t *type,
1733 bool must_be_constant)
1735 /* there might be extra {} hierarchies */
1737 while(token.type == '{') {
1740 warningf(HERE, "extra curly braces around scalar initializer");
1745 expression_t *expression = parse_assignment_expression();
1746 if(must_be_constant && !is_initializer_constant(expression)) {
1747 errorf(&expression->base.source_position,
1748 "Initialisation expression '%E' is not constant\n",
1752 initializer_t *initializer = initializer_from_expression(type, expression);
1754 if(initializer == NULL) {
1755 errorf(&expression->base.source_position,
1756 "expression '%E' (type '%T') doesn't match expected type '%T'",
1757 expression, expression->base.type, type);
1762 bool additional_warning_displayed = false;
1764 if(token.type == ',') {
1767 if(token.type != '}') {
1768 if(!additional_warning_displayed) {
1769 warningf(HERE, "additional elements in scalar initializer");
1770 additional_warning_displayed = true;
1781 * An entry in the type path.
1783 typedef struct type_path_entry_t type_path_entry_t;
1784 struct type_path_entry_t {
1785 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1787 size_t index; /**< For array types: the current index. */
1788 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1793 * A type path expression a position inside compound or array types.
1795 typedef struct type_path_t type_path_t;
1796 struct type_path_t {
1797 type_path_entry_t *path; /**< An flexible array containing the current path. */
1798 type_t *top_type; /**< type of the element the path points */
1799 size_t max_index; /**< largest index in outermost array */
1803 * Prints a type path for debugging.
1805 static __attribute__((unused)) void debug_print_type_path(
1806 const type_path_t *path)
1808 size_t len = ARR_LEN(path->path);
1810 for(size_t i = 0; i < len; ++i) {
1811 const type_path_entry_t *entry = & path->path[i];
1813 type_t *type = skip_typeref(entry->type);
1814 if(is_type_compound(type)) {
1815 /* in gcc mode structs can have no members */
1816 if(entry->v.compound_entry == NULL) {
1820 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1821 } else if(is_type_array(type)) {
1822 fprintf(stderr, "[%zd]", entry->v.index);
1824 fprintf(stderr, "-INVALID-");
1827 if(path->top_type != NULL) {
1828 fprintf(stderr, " (");
1829 print_type(path->top_type);
1830 fprintf(stderr, ")");
1835 * Return the top type path entry, ie. in a path
1836 * (type).a.b returns the b.
1838 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1840 size_t len = ARR_LEN(path->path);
1842 return &path->path[len-1];
1846 * Enlarge the type path by an (empty) element.
1848 static type_path_entry_t *append_to_type_path(type_path_t *path)
1850 size_t len = ARR_LEN(path->path);
1851 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1853 type_path_entry_t *result = & path->path[len];
1854 memset(result, 0, sizeof(result[0]));
1859 * Descending into a sub-type. Enter the scope of the current
1862 static void descend_into_subtype(type_path_t *path)
1864 type_t *orig_top_type = path->top_type;
1865 type_t *top_type = skip_typeref(orig_top_type);
1867 assert(is_type_compound(top_type) || is_type_array(top_type));
1869 type_path_entry_t *top = append_to_type_path(path);
1870 top->type = top_type;
1872 if(is_type_compound(top_type)) {
1873 declaration_t *declaration = top_type->compound.declaration;
1874 declaration_t *entry = declaration->scope.declarations;
1875 top->v.compound_entry = entry;
1878 path->top_type = entry->type;
1880 path->top_type = NULL;
1883 assert(is_type_array(top_type));
1886 path->top_type = top_type->array.element_type;
1891 * Pop an entry from the given type path, ie. returning from
1892 * (type).a.b to (type).a
1894 static void ascend_from_subtype(type_path_t *path)
1896 type_path_entry_t *top = get_type_path_top(path);
1898 path->top_type = top->type;
1900 size_t len = ARR_LEN(path->path);
1901 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1905 * Pop entries from the given type path until the given
1906 * path level is reached.
1908 static void ascend_to(type_path_t *path, size_t top_path_level)
1910 size_t len = ARR_LEN(path->path);
1912 while(len > top_path_level) {
1913 ascend_from_subtype(path);
1914 len = ARR_LEN(path->path);
1918 static bool walk_designator(type_path_t *path, const designator_t *designator,
1919 bool used_in_offsetof)
1921 for( ; designator != NULL; designator = designator->next) {
1922 type_path_entry_t *top = get_type_path_top(path);
1923 type_t *orig_type = top->type;
1925 type_t *type = skip_typeref(orig_type);
1927 if(designator->symbol != NULL) {
1928 symbol_t *symbol = designator->symbol;
1929 if(!is_type_compound(type)) {
1930 if(is_type_valid(type)) {
1931 errorf(&designator->source_position,
1932 "'.%Y' designator used for non-compound type '%T'",
1938 declaration_t *declaration = type->compound.declaration;
1939 declaration_t *iter = declaration->scope.declarations;
1940 for( ; iter != NULL; iter = iter->next) {
1941 if(iter->symbol == symbol) {
1946 errorf(&designator->source_position,
1947 "'%T' has no member named '%Y'", orig_type, symbol);
1950 if(used_in_offsetof) {
1951 type_t *real_type = skip_typeref(iter->type);
1952 if(real_type->kind == TYPE_BITFIELD) {
1953 errorf(&designator->source_position,
1954 "offsetof designator '%Y' may not specify bitfield",
1960 top->type = orig_type;
1961 top->v.compound_entry = iter;
1962 orig_type = iter->type;
1964 expression_t *array_index = designator->array_index;
1965 assert(designator->array_index != NULL);
1967 if(!is_type_array(type)) {
1968 if(is_type_valid(type)) {
1969 errorf(&designator->source_position,
1970 "[%E] designator used for non-array type '%T'",
1971 array_index, orig_type);
1975 if(!is_type_valid(array_index->base.type)) {
1979 long index = fold_constant(array_index);
1980 if(!used_in_offsetof) {
1982 errorf(&designator->source_position,
1983 "array index [%E] must be positive", array_index);
1986 if(type->array.size_constant == true) {
1987 long array_size = type->array.size;
1988 if(index >= array_size) {
1989 errorf(&designator->source_position,
1990 "designator [%E] (%d) exceeds array size %d",
1991 array_index, index, array_size);
1997 top->type = orig_type;
1998 top->v.index = (size_t) index;
1999 orig_type = type->array.element_type;
2001 path->top_type = orig_type;
2003 if(designator->next != NULL) {
2004 descend_into_subtype(path);
2013 static void advance_current_object(type_path_t *path, size_t top_path_level)
2015 type_path_entry_t *top = get_type_path_top(path);
2017 type_t *type = skip_typeref(top->type);
2018 if(is_type_union(type)) {
2019 /* in unions only the first element is initialized */
2020 top->v.compound_entry = NULL;
2021 } else if(is_type_struct(type)) {
2022 declaration_t *entry = top->v.compound_entry;
2024 entry = entry->next;
2025 top->v.compound_entry = entry;
2027 path->top_type = entry->type;
2031 assert(is_type_array(type));
2035 if(!type->array.size_constant || top->v.index < type->array.size) {
2040 /* we're past the last member of the current sub-aggregate, try if we
2041 * can ascend in the type hierarchy and continue with another subobject */
2042 size_t len = ARR_LEN(path->path);
2044 if(len > top_path_level) {
2045 ascend_from_subtype(path);
2046 advance_current_object(path, top_path_level);
2048 path->top_type = NULL;
2053 * skip until token is found.
2055 static void skip_until(int type) {
2056 while(token.type != type) {
2057 if(token.type == T_EOF)
2064 * skip any {...} blocks until a closing braket is reached.
2066 static void skip_initializers(void)
2068 if(token.type == '{')
2071 while(token.type != '}') {
2072 if(token.type == T_EOF)
2074 if(token.type == '{') {
2082 static initializer_t *create_empty_initializer(void)
2084 static initializer_t empty_initializer
2085 = { .list = { { INITIALIZER_LIST }, 0 } };
2086 return &empty_initializer;
2090 * Parse a part of an initialiser for a struct or union,
2092 static initializer_t *parse_sub_initializer(type_path_t *path,
2093 type_t *outer_type, size_t top_path_level,
2094 parse_initializer_env_t *env)
2096 if(token.type == '}') {
2097 /* empty initializer */
2098 return create_empty_initializer();
2101 type_t *orig_type = path->top_type;
2102 type_t *type = NULL;
2104 if (orig_type == NULL) {
2105 /* We are initializing an empty compound. */
2107 type = skip_typeref(orig_type);
2109 /* we can't do usefull stuff if we didn't even parse the type. Skip the
2110 * initializers in this case. */
2111 if(!is_type_valid(type)) {
2112 skip_initializers();
2113 return create_empty_initializer();
2117 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
2120 designator_t *designator = NULL;
2121 if(token.type == '.' || token.type == '[') {
2122 designator = parse_designation();
2124 /* reset path to toplevel, evaluate designator from there */
2125 ascend_to(path, top_path_level);
2126 if(!walk_designator(path, designator, false)) {
2127 /* can't continue after designation error */
2131 initializer_t *designator_initializer
2132 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
2133 designator_initializer->designator.designator = designator;
2134 ARR_APP1(initializer_t*, initializers, designator_initializer);
2139 if(token.type == '{') {
2140 if(type != NULL && is_type_scalar(type)) {
2141 sub = parse_scalar_initializer(type, env->must_be_constant);
2145 if (env->declaration != NULL)
2146 errorf(HERE, "extra brace group at end of initializer for '%Y'",
2147 env->declaration->symbol);
2149 errorf(HERE, "extra brace group at end of initializer");
2151 descend_into_subtype(path);
2153 add_anchor_token('}');
2154 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
2156 rem_anchor_token('}');
2159 ascend_from_subtype(path);
2163 goto error_parse_next;
2167 /* must be an expression */
2168 expression_t *expression = parse_assignment_expression();
2170 if(env->must_be_constant && !is_initializer_constant(expression)) {
2171 errorf(&expression->base.source_position,
2172 "Initialisation expression '%E' is not constant\n",
2177 /* we are already outside, ... */
2181 /* handle { "string" } special case */
2182 if((expression->kind == EXPR_STRING_LITERAL
2183 || expression->kind == EXPR_WIDE_STRING_LITERAL)
2184 && outer_type != NULL) {
2185 sub = initializer_from_expression(outer_type, expression);
2187 if(token.type == ',') {
2190 if(token.type != '}') {
2191 warningf(HERE, "excessive elements in initializer for type '%T'",
2194 /* TODO: eat , ... */
2199 /* descend into subtypes until expression matches type */
2201 orig_type = path->top_type;
2202 type = skip_typeref(orig_type);
2204 sub = initializer_from_expression(orig_type, expression);
2208 if(!is_type_valid(type)) {
2211 if(is_type_scalar(type)) {
2212 errorf(&expression->base.source_position,
2213 "expression '%E' doesn't match expected type '%T'",
2214 expression, orig_type);
2218 descend_into_subtype(path);
2222 /* update largest index of top array */
2223 const type_path_entry_t *first = &path->path[0];
2224 type_t *first_type = first->type;
2225 first_type = skip_typeref(first_type);
2226 if(is_type_array(first_type)) {
2227 size_t index = first->v.index;
2228 if(index > path->max_index)
2229 path->max_index = index;
2233 /* append to initializers list */
2234 ARR_APP1(initializer_t*, initializers, sub);
2237 if(env->declaration != NULL)
2238 warningf(HERE, "excess elements in struct initializer for '%Y'",
2239 env->declaration->symbol);
2241 warningf(HERE, "excess elements in struct initializer");
2245 if(token.type == '}') {
2249 if(token.type == '}') {
2254 /* advance to the next declaration if we are not at the end */
2255 advance_current_object(path, top_path_level);
2256 orig_type = path->top_type;
2257 if(orig_type != NULL)
2258 type = skip_typeref(orig_type);
2264 size_t len = ARR_LEN(initializers);
2265 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
2266 initializer_t *result = allocate_ast_zero(size);
2267 result->kind = INITIALIZER_LIST;
2268 result->list.len = len;
2269 memcpy(&result->list.initializers, initializers,
2270 len * sizeof(initializers[0]));
2272 DEL_ARR_F(initializers);
2273 ascend_to(path, top_path_level);
2278 skip_initializers();
2279 DEL_ARR_F(initializers);
2280 ascend_to(path, top_path_level);
2285 * Parses an initializer. Parsers either a compound literal
2286 * (env->declaration == NULL) or an initializer of a declaration.
2288 static initializer_t *parse_initializer(parse_initializer_env_t *env)
2290 type_t *type = skip_typeref(env->type);
2291 initializer_t *result = NULL;
2294 if(is_type_scalar(type)) {
2295 result = parse_scalar_initializer(type, env->must_be_constant);
2296 } else if(token.type == '{') {
2300 memset(&path, 0, sizeof(path));
2301 path.top_type = env->type;
2302 path.path = NEW_ARR_F(type_path_entry_t, 0);
2304 descend_into_subtype(&path);
2306 add_anchor_token('}');
2307 result = parse_sub_initializer(&path, env->type, 1, env);
2308 rem_anchor_token('}');
2310 max_index = path.max_index;
2311 DEL_ARR_F(path.path);
2315 /* parse_scalar_initializer() also works in this case: we simply
2316 * have an expression without {} around it */
2317 result = parse_scalar_initializer(type, env->must_be_constant);
2320 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2321 * the array type size */
2322 if(is_type_array(type) && type->array.size_expression == NULL
2323 && result != NULL) {
2325 switch (result->kind) {
2326 case INITIALIZER_LIST:
2327 size = max_index + 1;
2330 case INITIALIZER_STRING:
2331 size = result->string.string.size;
2334 case INITIALIZER_WIDE_STRING:
2335 size = result->wide_string.string.size;
2339 internal_errorf(HERE, "invalid initializer type");
2342 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2343 cnst->base.type = type_size_t;
2344 cnst->conste.v.int_value = size;
2346 type_t *new_type = duplicate_type(type);
2348 new_type->array.size_expression = cnst;
2349 new_type->array.size_constant = true;
2350 new_type->array.size = size;
2351 env->type = new_type;
2359 static declaration_t *append_declaration(declaration_t *declaration);
2361 static declaration_t *parse_compound_type_specifier(bool is_struct)
2363 gnu_attribute_t *attributes = NULL;
2370 symbol_t *symbol = NULL;
2371 declaration_t *declaration = NULL;
2373 if (token.type == T___attribute__) {
2374 parse_attributes(&attributes);
2377 if(token.type == T_IDENTIFIER) {
2378 symbol = token.v.symbol;
2382 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
2384 declaration = get_declaration(symbol, NAMESPACE_UNION);
2386 } else if(token.type != '{') {
2388 parse_error_expected("while parsing struct type specifier",
2389 T_IDENTIFIER, '{', 0);
2391 parse_error_expected("while parsing union type specifier",
2392 T_IDENTIFIER, '{', 0);
2398 if(declaration == NULL) {
2399 declaration = allocate_declaration_zero();
2400 declaration->namespc =
2401 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
2402 declaration->source_position = token.source_position;
2403 declaration->symbol = symbol;
2404 declaration->parent_scope = scope;
2405 if (symbol != NULL) {
2406 environment_push(declaration);
2408 append_declaration(declaration);
2411 if(token.type == '{') {
2412 if(declaration->init.is_defined) {
2413 assert(symbol != NULL);
2414 errorf(HERE, "multiple definitions of '%s %Y'",
2415 is_struct ? "struct" : "union", symbol);
2416 declaration->scope.declarations = NULL;
2418 declaration->init.is_defined = true;
2420 parse_compound_type_entries(declaration);
2421 parse_attributes(&attributes);
2427 static void parse_enum_entries(type_t *const enum_type)
2431 if(token.type == '}') {
2433 errorf(HERE, "empty enum not allowed");
2437 add_anchor_token('}');
2439 if(token.type != T_IDENTIFIER) {
2440 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
2442 rem_anchor_token('}');
2446 declaration_t *const entry = allocate_declaration_zero();
2447 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
2448 entry->type = enum_type;
2449 entry->symbol = token.v.symbol;
2450 entry->source_position = token.source_position;
2453 if(token.type == '=') {
2455 expression_t *value = parse_constant_expression();
2457 value = create_implicit_cast(value, enum_type);
2458 entry->init.enum_value = value;
2463 record_declaration(entry);
2465 if(token.type != ',')
2468 } while(token.type != '}');
2469 rem_anchor_token('}');
2477 static type_t *parse_enum_specifier(void)
2479 gnu_attribute_t *attributes = NULL;
2480 declaration_t *declaration;
2484 if(token.type == T_IDENTIFIER) {
2485 symbol = token.v.symbol;
2488 declaration = get_declaration(symbol, NAMESPACE_ENUM);
2489 } else if(token.type != '{') {
2490 parse_error_expected("while parsing enum type specifier",
2491 T_IDENTIFIER, '{', 0);
2498 if(declaration == NULL) {
2499 declaration = allocate_declaration_zero();
2500 declaration->namespc = NAMESPACE_ENUM;
2501 declaration->source_position = token.source_position;
2502 declaration->symbol = symbol;
2503 declaration->parent_scope = scope;
2506 type_t *const type = allocate_type_zero(TYPE_ENUM, &declaration->source_position);
2507 type->enumt.declaration = declaration;
2509 if(token.type == '{') {
2510 if(declaration->init.is_defined) {
2511 errorf(HERE, "multiple definitions of enum %Y", symbol);
2513 if (symbol != NULL) {
2514 environment_push(declaration);
2516 append_declaration(declaration);
2517 declaration->init.is_defined = 1;
2519 parse_enum_entries(type);
2520 parse_attributes(&attributes);
2527 * if a symbol is a typedef to another type, return true
2529 static bool is_typedef_symbol(symbol_t *symbol)
2531 const declaration_t *const declaration =
2532 get_declaration(symbol, NAMESPACE_NORMAL);
2534 declaration != NULL &&
2535 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
2538 static type_t *parse_typeof(void)
2545 add_anchor_token(')');
2547 expression_t *expression = NULL;
2550 switch(token.type) {
2551 case T___extension__:
2552 /* this can be a prefix to a typename or an expression */
2553 /* we simply eat it now. */
2556 } while(token.type == T___extension__);
2560 if(is_typedef_symbol(token.v.symbol)) {
2561 type = parse_typename();
2563 expression = parse_expression();
2564 type = expression->base.type;
2569 type = parse_typename();
2573 expression = parse_expression();
2574 type = expression->base.type;
2578 rem_anchor_token(')');
2581 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, &expression->base.source_position);
2582 typeof_type->typeoft.expression = expression;
2583 typeof_type->typeoft.typeof_type = type;
2591 SPECIFIER_SIGNED = 1 << 0,
2592 SPECIFIER_UNSIGNED = 1 << 1,
2593 SPECIFIER_LONG = 1 << 2,
2594 SPECIFIER_INT = 1 << 3,
2595 SPECIFIER_DOUBLE = 1 << 4,
2596 SPECIFIER_CHAR = 1 << 5,
2597 SPECIFIER_SHORT = 1 << 6,
2598 SPECIFIER_LONG_LONG = 1 << 7,
2599 SPECIFIER_FLOAT = 1 << 8,
2600 SPECIFIER_BOOL = 1 << 9,
2601 SPECIFIER_VOID = 1 << 10,
2602 SPECIFIER_INT8 = 1 << 11,
2603 SPECIFIER_INT16 = 1 << 12,
2604 SPECIFIER_INT32 = 1 << 13,
2605 SPECIFIER_INT64 = 1 << 14,
2606 SPECIFIER_INT128 = 1 << 15,
2607 #ifdef PROVIDE_COMPLEX
2608 SPECIFIER_COMPLEX = 1 << 16,
2609 SPECIFIER_IMAGINARY = 1 << 17,
2613 static type_t *create_builtin_type(symbol_t *const symbol,
2614 type_t *const real_type)
2616 type_t *type = allocate_type_zero(TYPE_BUILTIN, &builtin_source_position);
2617 type->builtin.symbol = symbol;
2618 type->builtin.real_type = real_type;
2620 type_t *result = typehash_insert(type);
2621 if (type != result) {
2628 static type_t *get_typedef_type(symbol_t *symbol)
2630 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2631 if(declaration == NULL
2632 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2635 type_t *type = allocate_type_zero(TYPE_TYPEDEF, &declaration->source_position);
2636 type->typedeft.declaration = declaration;
2642 * check for the allowed MS alignment values.
2644 static bool check_elignment_value(long long intvalue) {
2645 if(intvalue < 1 || intvalue > 8192) {
2646 errorf(HERE, "illegal alignment value");
2649 unsigned v = (unsigned)intvalue;
2650 for(unsigned i = 1; i <= 8192; i += i) {
2654 errorf(HERE, "alignment must be power of two");
2658 #define DET_MOD(name, tag) do { \
2659 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2660 *modifiers |= tag; \
2663 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2665 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2668 if(token.type == T_restrict) {
2670 DET_MOD(restrict, DM_RESTRICT);
2672 } else if(token.type != T_IDENTIFIER)
2674 symbol_t *symbol = token.v.symbol;
2675 if(symbol == sym_align) {
2678 if(token.type != T_INTEGER)
2680 if(check_elignment_value(token.v.intvalue)) {
2681 if(specifiers->alignment != 0)
2682 warningf(HERE, "align used more than once");
2683 specifiers->alignment = (unsigned char)token.v.intvalue;
2687 } else if(symbol == sym_allocate) {
2690 if(token.type != T_IDENTIFIER)
2692 (void)token.v.symbol;
2694 } else if(symbol == sym_dllimport) {
2696 DET_MOD(dllimport, DM_DLLIMPORT);
2697 } else if(symbol == sym_dllexport) {
2699 DET_MOD(dllexport, DM_DLLEXPORT);
2700 } else if(symbol == sym_thread) {
2702 DET_MOD(thread, DM_THREAD);
2703 } else if(symbol == sym_naked) {
2705 DET_MOD(naked, DM_NAKED);
2706 } else if(symbol == sym_noinline) {
2708 DET_MOD(noinline, DM_NOINLINE);
2709 } else if(symbol == sym_noreturn) {
2711 DET_MOD(noreturn, DM_NORETURN);
2712 } else if(symbol == sym_nothrow) {
2714 DET_MOD(nothrow, DM_NOTHROW);
2715 } else if(symbol == sym_novtable) {
2717 DET_MOD(novtable, DM_NOVTABLE);
2718 } else if(symbol == sym_property) {
2722 bool is_get = false;
2723 if(token.type != T_IDENTIFIER)
2725 if(token.v.symbol == sym_get) {
2727 } else if(token.v.symbol == sym_put) {
2729 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2734 if(token.type != T_IDENTIFIER)
2737 if(specifiers->get_property_sym != NULL) {
2738 errorf(HERE, "get property name already specified");
2740 specifiers->get_property_sym = token.v.symbol;
2743 if(specifiers->put_property_sym != NULL) {
2744 errorf(HERE, "put property name already specified");
2746 specifiers->put_property_sym = token.v.symbol;
2750 if(token.type == ',') {
2757 } else if(symbol == sym_selectany) {
2759 DET_MOD(selectany, DM_SELECTANY);
2760 } else if(symbol == sym_uuid) {
2763 if(token.type != T_STRING_LITERAL)
2767 } else if(symbol == sym_deprecated) {
2769 if(specifiers->deprecated != 0)
2770 warningf(HERE, "deprecated used more than once");
2771 specifiers->deprecated = 1;
2772 if(token.type == '(') {
2774 if(token.type == T_STRING_LITERAL) {
2775 specifiers->deprecated_string = token.v.string.begin;
2778 errorf(HERE, "string literal expected");
2782 } else if(symbol == sym_noalias) {
2784 DET_MOD(noalias, DM_NOALIAS);
2786 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2788 if(token.type == '(')
2792 if (token.type == ',')
2799 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2801 type_t *type = NULL;
2802 unsigned type_qualifiers = 0;
2803 unsigned type_specifiers = 0;
2806 specifiers->source_position = token.source_position;
2809 switch(token.type) {
2812 #define MATCH_STORAGE_CLASS(token, class) \
2814 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2815 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2817 specifiers->declared_storage_class = class; \
2821 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2822 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2823 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2824 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2825 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2830 add_anchor_token(')');
2831 parse_microsoft_extended_decl_modifier(specifiers);
2832 rem_anchor_token(')');
2837 switch (specifiers->declared_storage_class) {
2838 case STORAGE_CLASS_NONE:
2839 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2842 case STORAGE_CLASS_EXTERN:
2843 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2846 case STORAGE_CLASS_STATIC:
2847 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2851 errorf(HERE, "multiple storage classes in declaration specifiers");
2857 /* type qualifiers */
2858 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2860 type_qualifiers |= qualifier; \
2864 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2865 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2866 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2867 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
2868 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
2869 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
2870 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
2871 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
2873 case T___extension__:
2878 /* type specifiers */
2879 #define MATCH_SPECIFIER(token, specifier, name) \
2882 if(type_specifiers & specifier) { \
2883 errorf(HERE, "multiple " name " type specifiers given"); \
2885 type_specifiers |= specifier; \
2889 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2890 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2891 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2892 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2893 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2894 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2895 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2896 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2897 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2898 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2899 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2900 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2901 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2902 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2903 #ifdef PROVIDE_COMPLEX
2904 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2905 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2907 case T__forceinline:
2908 /* only in microsoft mode */
2909 specifiers->decl_modifiers |= DM_FORCEINLINE;
2913 specifiers->is_inline = true;
2918 if(type_specifiers & SPECIFIER_LONG_LONG) {
2919 errorf(HERE, "multiple type specifiers given");
2920 } else if(type_specifiers & SPECIFIER_LONG) {
2921 type_specifiers |= SPECIFIER_LONG_LONG;
2923 type_specifiers |= SPECIFIER_LONG;
2928 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2930 type->compound.declaration = parse_compound_type_specifier(true);
2934 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2936 type->compound.declaration = parse_compound_type_specifier(false);
2940 type = parse_enum_specifier();
2943 type = parse_typeof();
2945 case T___builtin_va_list:
2946 type = duplicate_type(type_valist);
2950 case T___attribute__:
2951 parse_attributes(&specifiers->gnu_attributes);
2954 case T_IDENTIFIER: {
2955 /* only parse identifier if we haven't found a type yet */
2956 if(type != NULL || type_specifiers != 0)
2957 goto finish_specifiers;
2959 type_t *typedef_type = get_typedef_type(token.v.symbol);
2961 if(typedef_type == NULL)
2962 goto finish_specifiers;
2965 type = typedef_type;
2969 /* function specifier */
2971 goto finish_specifiers;
2978 atomic_type_kind_t atomic_type;
2980 /* match valid basic types */
2981 switch(type_specifiers) {
2982 case SPECIFIER_VOID:
2983 atomic_type = ATOMIC_TYPE_VOID;
2985 case SPECIFIER_CHAR:
2986 atomic_type = ATOMIC_TYPE_CHAR;
2988 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2989 atomic_type = ATOMIC_TYPE_SCHAR;
2991 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2992 atomic_type = ATOMIC_TYPE_UCHAR;
2994 case SPECIFIER_SHORT:
2995 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2996 case SPECIFIER_SHORT | SPECIFIER_INT:
2997 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2998 atomic_type = ATOMIC_TYPE_SHORT;
3000 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
3001 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
3002 atomic_type = ATOMIC_TYPE_USHORT;
3005 case SPECIFIER_SIGNED:
3006 case SPECIFIER_SIGNED | SPECIFIER_INT:
3007 atomic_type = ATOMIC_TYPE_INT;
3009 case SPECIFIER_UNSIGNED:
3010 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
3011 atomic_type = ATOMIC_TYPE_UINT;
3013 case SPECIFIER_LONG:
3014 case SPECIFIER_SIGNED | SPECIFIER_LONG:
3015 case SPECIFIER_LONG | SPECIFIER_INT:
3016 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3017 atomic_type = ATOMIC_TYPE_LONG;
3019 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
3020 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
3021 atomic_type = ATOMIC_TYPE_ULONG;
3023 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3024 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3025 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
3026 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3028 atomic_type = ATOMIC_TYPE_LONGLONG;
3030 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
3031 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
3033 atomic_type = ATOMIC_TYPE_ULONGLONG;
3036 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
3037 atomic_type = unsigned_int8_type_kind;
3040 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
3041 atomic_type = unsigned_int16_type_kind;
3044 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
3045 atomic_type = unsigned_int32_type_kind;
3048 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
3049 atomic_type = unsigned_int64_type_kind;
3052 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
3053 atomic_type = unsigned_int128_type_kind;
3056 case SPECIFIER_INT8:
3057 case SPECIFIER_SIGNED | SPECIFIER_INT8:
3058 atomic_type = int8_type_kind;
3061 case SPECIFIER_INT16:
3062 case SPECIFIER_SIGNED | SPECIFIER_INT16:
3063 atomic_type = int16_type_kind;
3066 case SPECIFIER_INT32:
3067 case SPECIFIER_SIGNED | SPECIFIER_INT32:
3068 atomic_type = int32_type_kind;
3071 case SPECIFIER_INT64:
3072 case SPECIFIER_SIGNED | SPECIFIER_INT64:
3073 atomic_type = int64_type_kind;
3076 case SPECIFIER_INT128:
3077 case SPECIFIER_SIGNED | SPECIFIER_INT128:
3078 atomic_type = int128_type_kind;
3081 case SPECIFIER_FLOAT:
3082 atomic_type = ATOMIC_TYPE_FLOAT;
3084 case SPECIFIER_DOUBLE:
3085 atomic_type = ATOMIC_TYPE_DOUBLE;
3087 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
3088 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
3090 case SPECIFIER_BOOL:
3091 atomic_type = ATOMIC_TYPE_BOOL;
3093 #ifdef PROVIDE_COMPLEX
3094 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
3095 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
3097 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3098 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
3100 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
3101 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
3103 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
3104 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
3106 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3107 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
3109 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
3110 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
3114 /* invalid specifier combination, give an error message */
3115 if(type_specifiers == 0) {
3116 if (! strict_mode) {
3117 if (warning.implicit_int) {
3118 warningf(HERE, "no type specifiers in declaration, using 'int'");
3120 atomic_type = ATOMIC_TYPE_INT;
3123 errorf(HERE, "no type specifiers given in declaration");
3125 } else if((type_specifiers & SPECIFIER_SIGNED) &&
3126 (type_specifiers & SPECIFIER_UNSIGNED)) {
3127 errorf(HERE, "signed and unsigned specifiers gives");
3128 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
3129 errorf(HERE, "only integer types can be signed or unsigned");
3131 errorf(HERE, "multiple datatypes in declaration");
3133 atomic_type = ATOMIC_TYPE_INVALID;
3136 type = allocate_type_zero(TYPE_ATOMIC, &builtin_source_position);
3137 type->atomic.akind = atomic_type;
3140 if(type_specifiers != 0) {
3141 errorf(HERE, "multiple datatypes in declaration");
3145 type->base.qualifiers = type_qualifiers;
3146 /* FIXME: check type qualifiers here */
3148 type_t *result = typehash_insert(type);
3149 if(newtype && result != type) {
3153 specifiers->type = result;
3158 static type_qualifiers_t parse_type_qualifiers(void)
3160 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
3163 switch(token.type) {
3164 /* type qualifiers */
3165 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
3166 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
3167 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
3168 /* microsoft extended type modifiers */
3169 MATCH_TYPE_QUALIFIER(T__w64, TYPE_QUALIFIER_W64);
3170 MATCH_TYPE_QUALIFIER(T___ptr32, TYPE_QUALIFIER_PTR32);
3171 MATCH_TYPE_QUALIFIER(T___ptr64, TYPE_QUALIFIER_PTR64);
3172 MATCH_TYPE_QUALIFIER(T___uptr, TYPE_QUALIFIER_UPTR);
3173 MATCH_TYPE_QUALIFIER(T___sptr, TYPE_QUALIFIER_SPTR);
3176 return type_qualifiers;
3181 static declaration_t *parse_identifier_list(void)
3183 declaration_t *declarations = NULL;
3184 declaration_t *last_declaration = NULL;
3186 declaration_t *const declaration = allocate_declaration_zero();
3187 declaration->type = NULL; /* a K&R parameter list has no types, yet */
3188 declaration->source_position = token.source_position;
3189 declaration->symbol = token.v.symbol;
3192 if(last_declaration != NULL) {
3193 last_declaration->next = declaration;
3195 declarations = declaration;
3197 last_declaration = declaration;
3199 if(token.type != ',')
3202 } while(token.type == T_IDENTIFIER);
3204 return declarations;
3207 static void semantic_parameter(declaration_t *declaration)
3209 /* TODO: improve error messages */
3211 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
3212 errorf(HERE, "typedef not allowed in parameter list");
3213 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
3214 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
3215 errorf(HERE, "parameter may only have none or register storage class");
3218 type_t *const orig_type = declaration->type;
3219 type_t * type = skip_typeref(orig_type);
3221 /* Array as last part of a parameter type is just syntactic sugar. Turn it
3222 * into a pointer. § 6.7.5.3 (7) */
3223 if (is_type_array(type)) {
3224 type_t *const element_type = type->array.element_type;
3226 type = make_pointer_type(element_type, type->base.qualifiers);
3228 declaration->type = type;
3231 if(is_type_incomplete(type)) {
3232 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
3233 orig_type, declaration->symbol);
3237 static declaration_t *parse_parameter(void)
3239 declaration_specifiers_t specifiers;
3240 memset(&specifiers, 0, sizeof(specifiers));
3242 parse_declaration_specifiers(&specifiers);
3244 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
3246 semantic_parameter(declaration);
3251 static declaration_t *parse_parameters(function_type_t *type)
3253 if(token.type == T_IDENTIFIER) {
3254 symbol_t *symbol = token.v.symbol;
3255 if(!is_typedef_symbol(symbol)) {
3256 type->kr_style_parameters = true;
3257 return parse_identifier_list();
3261 if(token.type == ')') {
3262 type->unspecified_parameters = 1;
3265 if(token.type == T_void && look_ahead(1)->type == ')') {
3270 declaration_t *declarations = NULL;
3271 declaration_t *declaration;
3272 declaration_t *last_declaration = NULL;
3273 function_parameter_t *parameter;
3274 function_parameter_t *last_parameter = NULL;
3277 switch(token.type) {
3281 return declarations;
3284 case T___extension__:
3286 declaration = parse_parameter();
3288 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
3289 memset(parameter, 0, sizeof(parameter[0]));
3290 parameter->type = declaration->type;
3292 if(last_parameter != NULL) {
3293 last_declaration->next = declaration;
3294 last_parameter->next = parameter;
3296 type->parameters = parameter;
3297 declarations = declaration;
3299 last_parameter = parameter;
3300 last_declaration = declaration;
3304 return declarations;
3306 if(token.type != ',')
3307 return declarations;
3317 } construct_type_kind_t;
3319 typedef struct construct_type_t construct_type_t;
3320 struct construct_type_t {
3321 construct_type_kind_t kind;
3322 construct_type_t *next;
3325 typedef struct parsed_pointer_t parsed_pointer_t;
3326 struct parsed_pointer_t {
3327 construct_type_t construct_type;
3328 type_qualifiers_t type_qualifiers;
3331 typedef struct construct_function_type_t construct_function_type_t;
3332 struct construct_function_type_t {
3333 construct_type_t construct_type;
3334 type_t *function_type;
3337 typedef struct parsed_array_t parsed_array_t;
3338 struct parsed_array_t {
3339 construct_type_t construct_type;
3340 type_qualifiers_t type_qualifiers;
3346 typedef struct construct_base_type_t construct_base_type_t;
3347 struct construct_base_type_t {
3348 construct_type_t construct_type;
3352 static construct_type_t *parse_pointer_declarator(void)
3356 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
3357 memset(pointer, 0, sizeof(pointer[0]));
3358 pointer->construct_type.kind = CONSTRUCT_POINTER;
3359 pointer->type_qualifiers = parse_type_qualifiers();
3361 return (construct_type_t*) pointer;
3364 static construct_type_t *parse_array_declarator(void)
3367 add_anchor_token(']');
3369 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
3370 memset(array, 0, sizeof(array[0]));
3371 array->construct_type.kind = CONSTRUCT_ARRAY;
3373 if(token.type == T_static) {
3374 array->is_static = true;
3378 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
3379 if(type_qualifiers != 0) {
3380 if(token.type == T_static) {
3381 array->is_static = true;
3385 array->type_qualifiers = type_qualifiers;
3387 if(token.type == '*' && look_ahead(1)->type == ']') {
3388 array->is_variable = true;
3390 } else if(token.type != ']') {
3391 array->size = parse_assignment_expression();
3394 rem_anchor_token(']');
3397 return (construct_type_t*) array;
3402 static construct_type_t *parse_function_declarator(declaration_t *declaration)
3405 add_anchor_token(')');
3408 if(declaration != NULL) {
3409 type = allocate_type_zero(TYPE_FUNCTION, &declaration->source_position);
3411 type = allocate_type_zero(TYPE_FUNCTION, HERE);
3414 declaration_t *parameters = parse_parameters(&type->function);
3415 if(declaration != NULL) {
3416 declaration->scope.declarations = parameters;
3419 construct_function_type_t *construct_function_type =
3420 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
3421 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
3422 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
3423 construct_function_type->function_type = type;
3425 rem_anchor_token(')');
3429 return (construct_type_t*) construct_function_type;
3432 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
3433 bool may_be_abstract)
3435 /* construct a single linked list of construct_type_t's which describe
3436 * how to construct the final declarator type */
3437 construct_type_t *first = NULL;
3438 construct_type_t *last = NULL;
3439 gnu_attribute_t *attributes = NULL;
3442 while(token.type == '*') {
3443 construct_type_t *type = parse_pointer_declarator();
3454 /* TODO: find out if this is correct */
3455 parse_attributes(&attributes);
3457 construct_type_t *inner_types = NULL;
3459 switch(token.type) {
3461 if(declaration == NULL) {
3462 errorf(HERE, "no identifier expected in typename");
3464 declaration->symbol = token.v.symbol;
3465 declaration->source_position = token.source_position;
3471 add_anchor_token(')');
3472 inner_types = parse_inner_declarator(declaration, may_be_abstract);
3473 rem_anchor_token(')');
3479 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
3480 /* avoid a loop in the outermost scope, because eat_statement doesn't
3482 if(token.type == '}' && current_function == NULL) {
3490 construct_type_t *p = last;
3493 construct_type_t *type;
3494 switch(token.type) {
3496 type = parse_function_declarator(declaration);
3499 type = parse_array_declarator();
3502 goto declarator_finished;
3505 /* insert in the middle of the list (behind p) */
3507 type->next = p->next;
3518 declarator_finished:
3519 parse_attributes(&attributes);
3521 /* append inner_types at the end of the list, we don't to set last anymore
3522 * as it's not needed anymore */
3524 assert(first == NULL);
3525 first = inner_types;
3527 last->next = inner_types;
3535 static type_t *construct_declarator_type(construct_type_t *construct_list,
3538 construct_type_t *iter = construct_list;
3539 for( ; iter != NULL; iter = iter->next) {
3540 switch(iter->kind) {
3541 case CONSTRUCT_INVALID:
3542 internal_errorf(HERE, "invalid type construction found");
3543 case CONSTRUCT_FUNCTION: {
3544 construct_function_type_t *construct_function_type
3545 = (construct_function_type_t*) iter;
3547 type_t *function_type = construct_function_type->function_type;
3549 function_type->function.return_type = type;
3551 type_t *skipped_return_type = skip_typeref(type);
3552 if (is_type_function(skipped_return_type)) {
3553 errorf(HERE, "function returning function is not allowed");
3554 type = type_error_type;
3555 } else if (is_type_array(skipped_return_type)) {
3556 errorf(HERE, "function returning array is not allowed");
3557 type = type_error_type;
3559 type = function_type;
3564 case CONSTRUCT_POINTER: {
3565 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
3566 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, &null_position);
3567 pointer_type->pointer.points_to = type;
3568 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
3570 type = pointer_type;
3574 case CONSTRUCT_ARRAY: {
3575 parsed_array_t *parsed_array = (parsed_array_t*) iter;
3576 type_t *array_type = allocate_type_zero(TYPE_ARRAY, &null_position);
3578 expression_t *size_expression = parsed_array->size;
3579 if(size_expression != NULL) {
3581 = create_implicit_cast(size_expression, type_size_t);
3584 array_type->base.qualifiers = parsed_array->type_qualifiers;
3585 array_type->array.element_type = type;
3586 array_type->array.is_static = parsed_array->is_static;
3587 array_type->array.is_variable = parsed_array->is_variable;
3588 array_type->array.size_expression = size_expression;
3590 if(size_expression != NULL) {
3591 if(is_constant_expression(size_expression)) {
3592 array_type->array.size_constant = true;
3593 array_type->array.size
3594 = fold_constant(size_expression);
3596 array_type->array.is_vla = true;
3600 type_t *skipped_type = skip_typeref(type);
3601 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
3602 errorf(HERE, "array of void is not allowed");
3603 type = type_error_type;
3611 type_t *hashed_type = typehash_insert(type);
3612 if(hashed_type != type) {
3613 /* the function type was constructed earlier freeing it here will
3614 * destroy other types... */
3615 if(iter->kind != CONSTRUCT_FUNCTION) {
3625 static declaration_t *parse_declarator(
3626 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3628 declaration_t *const declaration = allocate_declaration_zero();
3629 declaration->declared_storage_class = specifiers->declared_storage_class;
3630 declaration->modifiers = specifiers->decl_modifiers;
3631 declaration->deprecated = specifiers->deprecated;
3632 declaration->deprecated_string = specifiers->deprecated_string;
3633 declaration->get_property_sym = specifiers->get_property_sym;
3634 declaration->put_property_sym = specifiers->put_property_sym;
3635 declaration->is_inline = specifiers->is_inline;
3637 declaration->storage_class = specifiers->declared_storage_class;
3638 if(declaration->storage_class == STORAGE_CLASS_NONE
3639 && scope != global_scope) {
3640 declaration->storage_class = STORAGE_CLASS_AUTO;
3643 if(specifiers->alignment != 0) {
3644 /* TODO: add checks here */
3645 declaration->alignment = specifiers->alignment;
3648 construct_type_t *construct_type
3649 = parse_inner_declarator(declaration, may_be_abstract);
3650 type_t *const type = specifiers->type;
3651 declaration->type = construct_declarator_type(construct_type, type);
3653 if(construct_type != NULL) {
3654 obstack_free(&temp_obst, construct_type);
3660 static type_t *parse_abstract_declarator(type_t *base_type)
3662 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3664 type_t *result = construct_declarator_type(construct_type, base_type);
3665 if(construct_type != NULL) {
3666 obstack_free(&temp_obst, construct_type);
3672 static declaration_t *append_declaration(declaration_t* const declaration)
3674 if (last_declaration != NULL) {
3675 last_declaration->next = declaration;
3677 scope->declarations = declaration;
3679 last_declaration = declaration;
3684 * Check if the declaration of main is suspicious. main should be a
3685 * function with external linkage, returning int, taking either zero
3686 * arguments, two, or three arguments of appropriate types, ie.
3688 * int main([ int argc, char **argv [, char **env ] ]).
3690 * @param decl the declaration to check
3691 * @param type the function type of the declaration
3693 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3695 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3696 warningf(&decl->source_position,
3697 "'main' is normally a non-static function");
3699 if (skip_typeref(func_type->return_type) != type_int) {
3700 warningf(&decl->source_position,
3701 "return type of 'main' should be 'int', but is '%T'",
3702 func_type->return_type);
3704 const function_parameter_t *parm = func_type->parameters;
3706 type_t *const first_type = parm->type;
3707 if (!types_compatible(skip_typeref(first_type), type_int)) {
3708 warningf(&decl->source_position,
3709 "first argument of 'main' should be 'int', but is '%T'", first_type);
3713 type_t *const second_type = parm->type;
3714 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3715 warningf(&decl->source_position,
3716 "second argument of 'main' should be 'char**', but is '%T'", second_type);
3720 type_t *const third_type = parm->type;
3721 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3722 warningf(&decl->source_position,
3723 "third argument of 'main' should be 'char**', but is '%T'", third_type);
3727 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3731 warningf(&decl->source_position, "'main' takes only zero, two or three arguments");
3737 * Check if a symbol is the equal to "main".
3739 static bool is_sym_main(const symbol_t *const sym)
3741 return strcmp(sym->string, "main") == 0;
3744 static declaration_t *internal_record_declaration(
3745 declaration_t *const declaration,
3746 const bool is_function_definition)
3748 const symbol_t *const symbol = declaration->symbol;
3749 const namespace_t namespc = (namespace_t)declaration->namespc;
3751 type_t *const orig_type = declaration->type;
3752 type_t *const type = skip_typeref(orig_type);
3753 if (is_type_function(type) &&
3754 type->function.unspecified_parameters &&
3755 warning.strict_prototypes) {
3756 warningf(&declaration->source_position,
3757 "function declaration '%#T' is not a prototype",
3758 orig_type, declaration->symbol);
3761 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3762 check_type_of_main(declaration, &type->function);
3765 assert(declaration->symbol != NULL);
3766 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3768 assert(declaration != previous_declaration);
3769 if (previous_declaration != NULL) {
3770 if (previous_declaration->parent_scope == scope) {
3771 /* can happen for K&R style declarations */
3772 if(previous_declaration->type == NULL) {
3773 previous_declaration->type = declaration->type;
3776 const type_t *prev_type = skip_typeref(previous_declaration->type);
3777 if (!types_compatible(type, prev_type)) {
3778 errorf(&declaration->source_position,
3779 "declaration '%#T' is incompatible with '%#T' (declared %P)",
3780 orig_type, symbol, previous_declaration->type, symbol,
3781 &previous_declaration->source_position);
3783 unsigned old_storage_class = previous_declaration->storage_class;
3784 if(old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3785 errorf(&declaration->source_position,
3786 "redeclaration of enum entry '%Y' (declared %P)",
3787 symbol, &previous_declaration->source_position);
3788 return previous_declaration;
3791 unsigned new_storage_class = declaration->storage_class;
3793 if(is_type_incomplete(prev_type)) {
3794 previous_declaration->type = type;
3798 /* pretend no storage class means extern for function
3799 * declarations (except if the previous declaration is neither
3800 * none nor extern) */
3801 if (is_type_function(type)) {
3802 switch (old_storage_class) {
3803 case STORAGE_CLASS_NONE:
3804 old_storage_class = STORAGE_CLASS_EXTERN;
3806 case STORAGE_CLASS_EXTERN:
3807 if (is_function_definition) {
3808 if (warning.missing_prototypes &&
3809 prev_type->function.unspecified_parameters &&
3810 !is_sym_main(symbol)) {
3811 warningf(&declaration->source_position,
3812 "no previous prototype for '%#T'",
3815 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3816 new_storage_class = STORAGE_CLASS_EXTERN;
3824 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3825 new_storage_class == STORAGE_CLASS_EXTERN) {
3826 warn_redundant_declaration:
3827 if (warning.redundant_decls) {
3828 warningf(&declaration->source_position,
3829 "redundant declaration for '%Y' (declared %P)",
3830 symbol, &previous_declaration->source_position);
3832 } else if (current_function == NULL) {
3833 if (old_storage_class != STORAGE_CLASS_STATIC &&
3834 new_storage_class == STORAGE_CLASS_STATIC) {
3835 errorf(&declaration->source_position,
3836 "static declaration of '%Y' follows non-static declaration (declared %P)",
3837 symbol, &previous_declaration->source_position);
3839 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3840 goto warn_redundant_declaration;
3842 if (new_storage_class == STORAGE_CLASS_NONE) {
3843 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3844 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3848 if (old_storage_class == new_storage_class) {
3849 errorf(&declaration->source_position,
3850 "redeclaration of '%Y' (declared %P)",
3851 symbol, &previous_declaration->source_position);
3853 errorf(&declaration->source_position,
3854 "redeclaration of '%Y' with different linkage (declared %P)",
3855 symbol, &previous_declaration->source_position);
3859 return previous_declaration;
3861 } else if (is_function_definition) {
3862 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3863 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3864 warningf(&declaration->source_position,
3865 "no previous prototype for '%#T'", orig_type, symbol);
3866 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3867 warningf(&declaration->source_position,
3868 "no previous declaration for '%#T'", orig_type,
3872 } else if (warning.missing_declarations &&
3873 scope == global_scope &&
3874 !is_type_function(type) && (
3875 declaration->storage_class == STORAGE_CLASS_NONE ||
3876 declaration->storage_class == STORAGE_CLASS_THREAD
3878 warningf(&declaration->source_position,
3879 "no previous declaration for '%#T'", orig_type, symbol);
3882 assert(declaration->parent_scope == NULL);
3883 assert(scope != NULL);
3885 declaration->parent_scope = scope;
3887 environment_push(declaration);
3888 return append_declaration(declaration);
3891 static declaration_t *record_declaration(declaration_t *declaration)
3893 return internal_record_declaration(declaration, false);
3896 static declaration_t *record_function_definition(declaration_t *declaration)
3898 return internal_record_declaration(declaration, true);
3901 static void parser_error_multiple_definition(declaration_t *declaration,
3902 const source_position_t *source_position)
3904 errorf(source_position, "multiple definition of symbol '%Y' (declared %P)",
3905 declaration->symbol, &declaration->source_position);
3908 static bool is_declaration_specifier(const token_t *token,
3909 bool only_type_specifiers)
3911 switch(token->type) {
3915 return is_typedef_symbol(token->v.symbol);
3917 case T___extension__:
3920 return !only_type_specifiers;
3927 static void parse_init_declarator_rest(declaration_t *declaration)
3931 type_t *orig_type = declaration->type;
3932 type_t *type = skip_typeref(orig_type);
3934 if(declaration->init.initializer != NULL) {
3935 parser_error_multiple_definition(declaration, HERE);
3938 bool must_be_constant = false;
3939 if(declaration->storage_class == STORAGE_CLASS_STATIC
3940 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3941 || declaration->parent_scope == global_scope) {
3942 must_be_constant = true;
3945 parse_initializer_env_t env;
3946 env.type = orig_type;
3947 env.must_be_constant = must_be_constant;
3948 env.declaration = declaration;
3950 initializer_t *initializer = parse_initializer(&env);
3952 if(env.type != orig_type) {
3953 orig_type = env.type;
3954 type = skip_typeref(orig_type);
3955 declaration->type = env.type;
3958 if(is_type_function(type)) {
3959 errorf(&declaration->source_position,
3960 "initializers not allowed for function types at declator '%Y' (type '%T')",
3961 declaration->symbol, orig_type);
3963 declaration->init.initializer = initializer;
3967 /* parse rest of a declaration without any declarator */
3968 static void parse_anonymous_declaration_rest(
3969 const declaration_specifiers_t *specifiers,
3970 parsed_declaration_func finished_declaration)
3974 declaration_t *const declaration = allocate_declaration_zero();
3975 declaration->type = specifiers->type;
3976 declaration->declared_storage_class = specifiers->declared_storage_class;
3977 declaration->source_position = specifiers->source_position;
3978 declaration->modifiers = specifiers->decl_modifiers;
3980 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3981 warningf(&declaration->source_position,
3982 "useless storage class in empty declaration");
3984 declaration->storage_class = STORAGE_CLASS_NONE;
3986 type_t *type = declaration->type;
3987 switch (type->kind) {
3988 case TYPE_COMPOUND_STRUCT:
3989 case TYPE_COMPOUND_UNION: {
3990 if (type->compound.declaration->symbol == NULL) {
3991 warningf(&declaration->source_position,
3992 "unnamed struct/union that defines no instances");
4001 warningf(&declaration->source_position, "empty declaration");
4005 finished_declaration(declaration);
4008 static void parse_declaration_rest(declaration_t *ndeclaration,
4009 const declaration_specifiers_t *specifiers,
4010 parsed_declaration_func finished_declaration)
4012 add_anchor_token(';');
4013 add_anchor_token('=');
4014 add_anchor_token(',');
4016 declaration_t *declaration = finished_declaration(ndeclaration);
4018 type_t *orig_type = declaration->type;
4019 type_t *type = skip_typeref(orig_type);
4021 if (type->kind != TYPE_FUNCTION &&
4022 declaration->is_inline &&
4023 is_type_valid(type)) {
4024 warningf(&declaration->source_position,
4025 "variable '%Y' declared 'inline'\n", declaration->symbol);
4028 if(token.type == '=') {
4029 parse_init_declarator_rest(declaration);
4032 if(token.type != ',')
4036 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
4041 rem_anchor_token(';');
4042 rem_anchor_token('=');
4043 rem_anchor_token(',');
4046 static declaration_t *finished_kr_declaration(declaration_t *declaration)
4048 symbol_t *symbol = declaration->symbol;
4049 if(symbol == NULL) {
4050 errorf(HERE, "anonymous declaration not valid as function parameter");
4053 namespace_t namespc = (namespace_t) declaration->namespc;
4054 if(namespc != NAMESPACE_NORMAL) {
4055 return record_declaration(declaration);
4058 declaration_t *previous_declaration = get_declaration(symbol, namespc);
4059 if(previous_declaration == NULL ||
4060 previous_declaration->parent_scope != scope) {
4061 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
4066 if(previous_declaration->type == NULL) {
4067 previous_declaration->type = declaration->type;
4068 previous_declaration->declared_storage_class = declaration->declared_storage_class;
4069 previous_declaration->storage_class = declaration->storage_class;
4070 previous_declaration->parent_scope = scope;
4071 return previous_declaration;
4073 return record_declaration(declaration);
4077 static void parse_declaration(parsed_declaration_func finished_declaration)
4079 declaration_specifiers_t specifiers;
4080 memset(&specifiers, 0, sizeof(specifiers));
4081 parse_declaration_specifiers(&specifiers);
4083 if(token.type == ';') {
4084 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4086 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4087 parse_declaration_rest(declaration, &specifiers, finished_declaration);
4091 static void parse_kr_declaration_list(declaration_t *declaration)
4093 type_t *type = skip_typeref(declaration->type);
4094 if(!is_type_function(type))
4097 if(!type->function.kr_style_parameters)
4100 /* push function parameters */
4101 int top = environment_top();
4102 scope_t *last_scope = scope;
4103 set_scope(&declaration->scope);
4105 declaration_t *parameter = declaration->scope.declarations;
4106 for( ; parameter != NULL; parameter = parameter->next) {
4107 assert(parameter->parent_scope == NULL);
4108 parameter->parent_scope = scope;
4109 environment_push(parameter);
4112 /* parse declaration list */
4113 while(is_declaration_specifier(&token, false)) {
4114 parse_declaration(finished_kr_declaration);
4117 /* pop function parameters */
4118 assert(scope == &declaration->scope);
4119 set_scope(last_scope);
4120 environment_pop_to(top);
4122 /* update function type */
4123 type_t *new_type = duplicate_type(type);
4124 new_type->function.kr_style_parameters = false;
4126 function_parameter_t *parameters = NULL;
4127 function_parameter_t *last_parameter = NULL;
4129 declaration_t *parameter_declaration = declaration->scope.declarations;
4130 for( ; parameter_declaration != NULL;
4131 parameter_declaration = parameter_declaration->next) {
4132 type_t *parameter_type = parameter_declaration->type;
4133 if(parameter_type == NULL) {
4135 errorf(HERE, "no type specified for function parameter '%Y'",
4136 parameter_declaration->symbol);
4138 if (warning.implicit_int) {
4139 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
4140 parameter_declaration->symbol);
4142 parameter_type = type_int;
4143 parameter_declaration->type = parameter_type;
4147 semantic_parameter(parameter_declaration);
4148 parameter_type = parameter_declaration->type;
4150 function_parameter_t *function_parameter
4151 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
4152 memset(function_parameter, 0, sizeof(function_parameter[0]));
4154 function_parameter->type = parameter_type;
4155 if(last_parameter != NULL) {
4156 last_parameter->next = function_parameter;
4158 parameters = function_parameter;
4160 last_parameter = function_parameter;
4162 new_type->function.parameters = parameters;
4164 type = typehash_insert(new_type);
4165 if(type != new_type) {
4166 obstack_free(type_obst, new_type);
4169 declaration->type = type;
4172 static bool first_err = true;
4175 * When called with first_err set, prints the name of the current function,
4178 static void print_in_function(void) {
4181 diagnosticf("%s: In function '%Y':\n",
4182 current_function->source_position.input_name,
4183 current_function->symbol);
4188 * Check if all labels are defined in the current function.
4189 * Check if all labels are used in the current function.
4191 static void check_labels(void)
4193 for (const goto_statement_t *goto_statement = goto_first;
4194 goto_statement != NULL;
4195 goto_statement = goto_statement->next) {
4196 declaration_t *label = goto_statement->label;
4199 if (label->source_position.input_name == NULL) {
4200 print_in_function();
4201 errorf(&goto_statement->base.source_position,
4202 "label '%Y' used but not defined", label->symbol);
4205 goto_first = goto_last = NULL;
4207 if (warning.unused_label) {
4208 for (const label_statement_t *label_statement = label_first;
4209 label_statement != NULL;
4210 label_statement = label_statement->next) {
4211 const declaration_t *label = label_statement->label;
4213 if (! label->used) {
4214 print_in_function();
4215 warningf(&label_statement->base.source_position,
4216 "label '%Y' defined but not used", label->symbol);
4220 label_first = label_last = NULL;
4224 * Check declarations of current_function for unused entities.
4226 static void check_declarations(void)
4228 if (warning.unused_parameter) {
4229 const scope_t *scope = ¤t_function->scope;
4231 const declaration_t *parameter = scope->declarations;
4232 for (; parameter != NULL; parameter = parameter->next) {
4233 if (! parameter->used) {
4234 print_in_function();
4235 warningf(¶meter->source_position,
4236 "unused parameter '%Y'", parameter->symbol);
4240 if (warning.unused_variable) {
4244 static void parse_external_declaration(void)
4246 /* function-definitions and declarations both start with declaration
4248 declaration_specifiers_t specifiers;
4249 memset(&specifiers, 0, sizeof(specifiers));
4251 add_anchor_token(';');
4252 parse_declaration_specifiers(&specifiers);
4253 rem_anchor_token(';');
4255 /* must be a declaration */
4256 if(token.type == ';') {
4257 parse_anonymous_declaration_rest(&specifiers, append_declaration);
4261 add_anchor_token(',');
4262 add_anchor_token('=');
4263 rem_anchor_token(';');
4265 /* declarator is common to both function-definitions and declarations */
4266 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
4268 rem_anchor_token(',');
4269 rem_anchor_token('=');
4270 rem_anchor_token(';');
4272 /* must be a declaration */
4273 if(token.type == ',' || token.type == '=' || token.type == ';') {
4274 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
4278 /* must be a function definition */
4279 parse_kr_declaration_list(ndeclaration);
4281 if(token.type != '{') {
4282 parse_error_expected("while parsing function definition", '{', 0);
4283 eat_until_matching_token(';');
4287 type_t *type = ndeclaration->type;
4289 /* note that we don't skip typerefs: the standard doesn't allow them here
4290 * (so we can't use is_type_function here) */
4291 if(type->kind != TYPE_FUNCTION) {
4292 if (is_type_valid(type)) {
4293 errorf(HERE, "declarator '%#T' has a body but is not a function type",
4294 type, ndeclaration->symbol);
4300 /* § 6.7.5.3 (14) a function definition with () means no
4301 * parameters (and not unspecified parameters) */
4302 if(type->function.unspecified_parameters) {
4303 type_t *duplicate = duplicate_type(type);
4304 duplicate->function.unspecified_parameters = false;
4306 type = typehash_insert(duplicate);
4307 if(type != duplicate) {
4308 obstack_free(type_obst, duplicate);
4310 ndeclaration->type = type;
4313 declaration_t *const declaration = record_function_definition(ndeclaration);
4314 if(ndeclaration != declaration) {
4315 declaration->scope = ndeclaration->scope;
4317 type = skip_typeref(declaration->type);
4319 /* push function parameters and switch scope */
4320 int top = environment_top();
4321 scope_t *last_scope = scope;
4322 set_scope(&declaration->scope);
4324 declaration_t *parameter = declaration->scope.declarations;
4325 for( ; parameter != NULL; parameter = parameter->next) {
4326 if(parameter->parent_scope == &ndeclaration->scope) {
4327 parameter->parent_scope = scope;
4329 assert(parameter->parent_scope == NULL
4330 || parameter->parent_scope == scope);
4331 parameter->parent_scope = scope;
4332 environment_push(parameter);
4335 if(declaration->init.statement != NULL) {
4336 parser_error_multiple_definition(declaration, HERE);
4338 goto end_of_parse_external_declaration;
4340 /* parse function body */
4341 int label_stack_top = label_top();
4342 declaration_t *old_current_function = current_function;
4343 current_function = declaration;
4345 declaration->init.statement = parse_compound_statement();
4348 check_declarations();
4350 assert(current_function == declaration);
4351 current_function = old_current_function;
4352 label_pop_to(label_stack_top);
4355 end_of_parse_external_declaration:
4356 assert(scope == &declaration->scope);
4357 set_scope(last_scope);
4358 environment_pop_to(top);
4361 static type_t *make_bitfield_type(type_t *base, expression_t *size,
4362 source_position_t *source_position)
4364 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
4365 type->bitfield.base = base;
4366 type->bitfield.size = size;
4371 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
4374 declaration_t *iter = compound_declaration->scope.declarations;
4375 for( ; iter != NULL; iter = iter->next) {
4376 if(iter->namespc != NAMESPACE_NORMAL)
4379 if(iter->symbol == NULL) {
4380 type_t *type = skip_typeref(iter->type);
4381 if(is_type_compound(type)) {
4382 declaration_t *result
4383 = find_compound_entry(type->compound.declaration, symbol);
4390 if(iter->symbol == symbol) {
4398 static void parse_compound_declarators(declaration_t *struct_declaration,
4399 const declaration_specifiers_t *specifiers)
4401 declaration_t *last_declaration = struct_declaration->scope.declarations;
4402 if(last_declaration != NULL) {
4403 while(last_declaration->next != NULL) {
4404 last_declaration = last_declaration->next;
4409 declaration_t *declaration;
4411 if(token.type == ':') {
4412 source_position_t source_position = *HERE;
4415 type_t *base_type = specifiers->type;
4416 expression_t *size = parse_constant_expression();
4418 if(!is_type_integer(skip_typeref(base_type))) {
4419 errorf(HERE, "bitfield base type '%T' is not an integer type",
4423 type_t *type = make_bitfield_type(base_type, size, &source_position);
4425 declaration = allocate_declaration_zero();
4426 declaration->namespc = NAMESPACE_NORMAL;
4427 declaration->declared_storage_class = STORAGE_CLASS_NONE;
4428 declaration->storage_class = STORAGE_CLASS_NONE;
4429 declaration->source_position = source_position;
4430 declaration->modifiers = specifiers->decl_modifiers;
4431 declaration->type = type;
4433 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
4435 type_t *orig_type = declaration->type;
4436 type_t *type = skip_typeref(orig_type);
4438 if(token.type == ':') {
4439 source_position_t source_position = *HERE;
4441 expression_t *size = parse_constant_expression();
4443 if(!is_type_integer(type)) {
4444 errorf(HERE, "bitfield base type '%T' is not an "
4445 "integer type", orig_type);
4448 type_t *bitfield_type = make_bitfield_type(orig_type, size, &source_position);
4449 declaration->type = bitfield_type;
4451 /* TODO we ignore arrays for now... what is missing is a check
4452 * that they're at the end of the struct */
4453 if(is_type_incomplete(type) && !is_type_array(type)) {
4455 "compound member '%Y' has incomplete type '%T'",
4456 declaration->symbol, orig_type);
4457 } else if(is_type_function(type)) {
4458 errorf(HERE, "compound member '%Y' must not have function "
4459 "type '%T'", declaration->symbol, orig_type);
4464 /* make sure we don't define a symbol multiple times */
4465 symbol_t *symbol = declaration->symbol;
4466 if(symbol != NULL) {
4467 declaration_t *prev_decl
4468 = find_compound_entry(struct_declaration, symbol);
4470 if(prev_decl != NULL) {
4471 assert(prev_decl->symbol == symbol);
4472 errorf(&declaration->source_position,
4473 "multiple declarations of symbol '%Y' (declared %P)",
4474 symbol, &prev_decl->source_position);
4478 /* append declaration */
4479 if(last_declaration != NULL) {
4480 last_declaration->next = declaration;
4482 struct_declaration->scope.declarations = declaration;
4484 last_declaration = declaration;
4486 if(token.type != ',')
4496 static void parse_compound_type_entries(declaration_t *compound_declaration)
4499 add_anchor_token('}');
4501 while(token.type != '}' && token.type != T_EOF) {
4502 declaration_specifiers_t specifiers;
4503 memset(&specifiers, 0, sizeof(specifiers));
4504 parse_declaration_specifiers(&specifiers);
4506 parse_compound_declarators(compound_declaration, &specifiers);
4508 rem_anchor_token('}');
4510 if(token.type == T_EOF) {
4511 errorf(HERE, "EOF while parsing struct");
4516 static type_t *parse_typename(void)
4518 declaration_specifiers_t specifiers;
4519 memset(&specifiers, 0, sizeof(specifiers));
4520 parse_declaration_specifiers(&specifiers);
4521 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
4522 /* TODO: improve error message, user does probably not know what a
4523 * storage class is...
4525 errorf(HERE, "typename may not have a storage class");
4528 type_t *result = parse_abstract_declarator(specifiers.type);
4536 typedef expression_t* (*parse_expression_function) (unsigned precedence);
4537 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
4538 expression_t *left);
4540 typedef struct expression_parser_function_t expression_parser_function_t;
4541 struct expression_parser_function_t {
4542 unsigned precedence;
4543 parse_expression_function parser;
4544 unsigned infix_precedence;
4545 parse_expression_infix_function infix_parser;
4548 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
4551 * Prints an error message if an expression was expected but not read
4553 static expression_t *expected_expression_error(void)
4555 /* skip the error message if the error token was read */
4556 if (token.type != T_ERROR) {
4557 errorf(HERE, "expected expression, got token '%K'", &token);
4561 return create_invalid_expression();
4565 * Parse a string constant.
4567 static expression_t *parse_string_const(void)
4570 if (token.type == T_STRING_LITERAL) {
4571 string_t res = token.v.string;
4573 while (token.type == T_STRING_LITERAL) {
4574 res = concat_strings(&res, &token.v.string);
4577 if (token.type != T_WIDE_STRING_LITERAL) {
4578 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
4579 /* note: that we use type_char_ptr here, which is already the
4580 * automatic converted type. revert_automatic_type_conversion
4581 * will construct the array type */
4582 cnst->base.type = type_char_ptr;
4583 cnst->string.value = res;
4587 wres = concat_string_wide_string(&res, &token.v.wide_string);
4589 wres = token.v.wide_string;
4594 switch (token.type) {
4595 case T_WIDE_STRING_LITERAL:
4596 wres = concat_wide_strings(&wres, &token.v.wide_string);
4599 case T_STRING_LITERAL:
4600 wres = concat_wide_string_string(&wres, &token.v.string);
4604 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
4605 cnst->base.type = type_wchar_t_ptr;
4606 cnst->wide_string.value = wres;
4615 * Parse an integer constant.
4617 static expression_t *parse_int_const(void)
4619 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4620 cnst->base.source_position = *HERE;
4621 cnst->base.type = token.datatype;
4622 cnst->conste.v.int_value = token.v.intvalue;
4630 * Parse a character constant.
4632 static expression_t *parse_character_constant(void)
4634 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4636 cnst->base.source_position = *HERE;
4637 cnst->base.type = token.datatype;
4638 cnst->conste.v.character = token.v.string;
4640 if (cnst->conste.v.character.size != 1) {
4641 if (warning.multichar && (c_mode & _GNUC)) {
4643 warningf(HERE, "multi-character character constant");
4645 errorf(HERE, "more than 1 characters in character constant");
4654 * Parse a wide character constant.
4656 static expression_t *parse_wide_character_constant(void)
4658 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4660 cnst->base.source_position = *HERE;
4661 cnst->base.type = token.datatype;
4662 cnst->conste.v.wide_character = token.v.wide_string;
4664 if (cnst->conste.v.wide_character.size != 1) {
4665 if (warning.multichar && (c_mode & _GNUC)) {
4667 warningf(HERE, "multi-character character constant");
4669 errorf(HERE, "more than 1 characters in character constant");
4678 * Parse a float constant.
4680 static expression_t *parse_float_const(void)
4682 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4683 cnst->base.type = token.datatype;
4684 cnst->conste.v.float_value = token.v.floatvalue;
4691 static declaration_t *create_implicit_function(symbol_t *symbol,
4692 const source_position_t *source_position)
4694 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4695 ntype->function.return_type = type_int;
4696 ntype->function.unspecified_parameters = true;
4698 type_t *type = typehash_insert(ntype);
4703 declaration_t *const declaration = allocate_declaration_zero();
4704 declaration->storage_class = STORAGE_CLASS_EXTERN;
4705 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4706 declaration->type = type;
4707 declaration->symbol = symbol;
4708 declaration->source_position = *source_position;
4709 declaration->parent_scope = global_scope;
4711 scope_t *old_scope = scope;
4712 set_scope(global_scope);
4714 environment_push(declaration);
4715 /* prepends the declaration to the global declarations list */
4716 declaration->next = scope->declarations;
4717 scope->declarations = declaration;
4719 assert(scope == global_scope);
4720 set_scope(old_scope);
4726 * Creates a return_type (func)(argument_type) function type if not
4729 * @param return_type the return type
4730 * @param argument_type the argument type
4732 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4734 function_parameter_t *parameter
4735 = obstack_alloc(type_obst, sizeof(parameter[0]));
4736 memset(parameter, 0, sizeof(parameter[0]));
4737 parameter->type = argument_type;
4739 type_t *type = allocate_type_zero(TYPE_FUNCTION, &builtin_source_position);
4740 type->function.return_type = return_type;
4741 type->function.parameters = parameter;
4743 type_t *result = typehash_insert(type);
4744 if(result != type) {
4752 * Creates a function type for some function like builtins.
4754 * @param symbol the symbol describing the builtin
4756 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4758 switch(symbol->ID) {
4759 case T___builtin_alloca:
4760 return make_function_1_type(type_void_ptr, type_size_t);
4761 case T___builtin_nan:
4762 return make_function_1_type(type_double, type_char_ptr);
4763 case T___builtin_nanf:
4764 return make_function_1_type(type_float, type_char_ptr);
4765 case T___builtin_nand:
4766 return make_function_1_type(type_long_double, type_char_ptr);
4767 case T___builtin_va_end:
4768 return make_function_1_type(type_void, type_valist);
4770 internal_errorf(HERE, "not implemented builtin symbol found");
4775 * Performs automatic type cast as described in § 6.3.2.1.
4777 * @param orig_type the original type
4779 static type_t *automatic_type_conversion(type_t *orig_type)
4781 type_t *type = skip_typeref(orig_type);
4782 if(is_type_array(type)) {
4783 array_type_t *array_type = &type->array;
4784 type_t *element_type = array_type->element_type;
4785 unsigned qualifiers = array_type->type.qualifiers;
4787 return make_pointer_type(element_type, qualifiers);
4790 if(is_type_function(type)) {
4791 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4798 * reverts the automatic casts of array to pointer types and function
4799 * to function-pointer types as defined § 6.3.2.1
4801 type_t *revert_automatic_type_conversion(const expression_t *expression)
4803 switch (expression->kind) {
4804 case EXPR_REFERENCE: return expression->reference.declaration->type;
4805 case EXPR_SELECT: return expression->select.compound_entry->type;
4807 case EXPR_UNARY_DEREFERENCE: {
4808 const expression_t *const value = expression->unary.value;
4809 type_t *const type = skip_typeref(value->base.type);
4810 assert(is_type_pointer(type));
4811 return type->pointer.points_to;
4814 case EXPR_BUILTIN_SYMBOL:
4815 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4817 case EXPR_ARRAY_ACCESS: {
4818 const expression_t *array_ref = expression->array_access.array_ref;
4819 type_t *type_left = skip_typeref(array_ref->base.type);
4820 if (!is_type_valid(type_left))
4822 assert(is_type_pointer(type_left));
4823 return type_left->pointer.points_to;
4826 case EXPR_STRING_LITERAL: {
4827 size_t size = expression->string.value.size;
4828 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4831 case EXPR_WIDE_STRING_LITERAL: {
4832 size_t size = expression->wide_string.value.size;
4833 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4836 case EXPR_COMPOUND_LITERAL:
4837 return expression->compound_literal.type;
4842 return expression->base.type;
4845 static expression_t *parse_reference(void)
4847 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4849 reference_expression_t *ref = &expression->reference;
4850 ref->symbol = token.v.symbol;
4852 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4854 source_position_t source_position = token.source_position;
4857 if(declaration == NULL) {
4858 if (! strict_mode && token.type == '(') {
4859 /* an implicitly defined function */
4860 if (warning.implicit_function_declaration) {
4861 warningf(HERE, "implicit declaration of function '%Y'",
4865 declaration = create_implicit_function(ref->symbol,
4868 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4869 return create_invalid_expression();
4873 type_t *type = declaration->type;
4875 /* we always do the auto-type conversions; the & and sizeof parser contains
4876 * code to revert this! */
4877 type = automatic_type_conversion(type);
4879 ref->declaration = declaration;
4880 ref->base.type = type;
4882 /* this declaration is used */
4883 declaration->used = true;
4885 /* check for deprecated functions */
4886 if(declaration->deprecated != 0) {
4887 const char *prefix = "";
4888 if (is_type_function(declaration->type))
4889 prefix = "function ";
4891 if (declaration->deprecated_string != NULL) {
4892 warningf(&source_position,
4893 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4894 declaration->deprecated_string);
4896 warningf(&source_position,
4897 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4904 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4908 /* TODO check if explicit cast is allowed and issue warnings/errors */
4911 static expression_t *parse_compound_literal(type_t *type)
4913 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4915 parse_initializer_env_t env;
4917 env.declaration = NULL;
4918 env.must_be_constant = false;
4919 initializer_t *initializer = parse_initializer(&env);
4922 expression->compound_literal.initializer = initializer;
4923 expression->compound_literal.type = type;
4924 expression->base.type = automatic_type_conversion(type);
4930 * Parse a cast expression.
4932 static expression_t *parse_cast(void)
4934 source_position_t source_position = token.source_position;
4936 type_t *type = parse_typename();
4938 /* matching add_anchor_token() is at call site */
4939 rem_anchor_token(')');
4942 if(token.type == '{') {
4943 return parse_compound_literal(type);
4946 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4947 cast->base.source_position = source_position;
4949 expression_t *value = parse_sub_expression(20);
4951 check_cast_allowed(value, type);
4953 cast->base.type = type;
4954 cast->unary.value = value;
4958 return create_invalid_expression();
4962 * Parse a statement expression.
4964 static expression_t *parse_statement_expression(void)
4966 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4968 statement_t *statement = parse_compound_statement();
4969 expression->statement.statement = statement;
4970 expression->base.source_position = statement->base.source_position;
4972 /* find last statement and use its type */
4973 type_t *type = type_void;
4974 const statement_t *stmt = statement->compound.statements;
4976 while (stmt->base.next != NULL)
4977 stmt = stmt->base.next;
4979 if (stmt->kind == STATEMENT_EXPRESSION) {
4980 type = stmt->expression.expression->base.type;
4983 warningf(&expression->base.source_position, "empty statement expression ({})");
4985 expression->base.type = type;
4991 return create_invalid_expression();
4995 * Parse a braced expression.
4997 static expression_t *parse_brace_expression(void)
5000 add_anchor_token(')');
5002 switch(token.type) {
5004 /* gcc extension: a statement expression */
5005 return parse_statement_expression();
5009 return parse_cast();
5011 if(is_typedef_symbol(token.v.symbol)) {
5012 return parse_cast();
5016 expression_t *result = parse_expression();
5017 rem_anchor_token(')');
5022 return create_invalid_expression();
5025 static expression_t *parse_function_keyword(void)
5030 if (current_function == NULL) {
5031 errorf(HERE, "'__func__' used outside of a function");
5034 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5035 expression->base.type = type_char_ptr;
5036 expression->funcname.kind = FUNCNAME_FUNCTION;
5041 static expression_t *parse_pretty_function_keyword(void)
5043 eat(T___PRETTY_FUNCTION__);
5045 if (current_function == NULL) {
5046 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
5049 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5050 expression->base.type = type_char_ptr;
5051 expression->funcname.kind = FUNCNAME_PRETTY_FUNCTION;
5056 static expression_t *parse_funcsig_keyword(void)
5060 if (current_function == NULL) {
5061 errorf(HERE, "'__FUNCSIG__' used outside of a function");
5064 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5065 expression->base.type = type_char_ptr;
5066 expression->funcname.kind = FUNCNAME_FUNCSIG;
5071 static expression_t *parse_funcdname_keyword(void)
5073 eat(T___FUNCDNAME__);
5075 if (current_function == NULL) {
5076 errorf(HERE, "'__FUNCDNAME__' used outside of a function");
5079 expression_t *expression = allocate_expression_zero(EXPR_FUNCNAME);
5080 expression->base.type = type_char_ptr;
5081 expression->funcname.kind = FUNCNAME_FUNCDNAME;
5086 static designator_t *parse_designator(void)
5088 designator_t *result = allocate_ast_zero(sizeof(result[0]));
5089 result->source_position = *HERE;
5091 if(token.type != T_IDENTIFIER) {
5092 parse_error_expected("while parsing member designator",
5096 result->symbol = token.v.symbol;
5099 designator_t *last_designator = result;
5101 if(token.type == '.') {
5103 if(token.type != T_IDENTIFIER) {
5104 parse_error_expected("while parsing member designator",
5108 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5109 designator->source_position = *HERE;
5110 designator->symbol = token.v.symbol;
5113 last_designator->next = designator;
5114 last_designator = designator;
5117 if(token.type == '[') {
5119 add_anchor_token(']');
5120 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
5121 designator->source_position = *HERE;
5122 designator->array_index = parse_expression();
5123 rem_anchor_token(']');
5125 if(designator->array_index == NULL) {
5129 last_designator->next = designator;
5130 last_designator = designator;
5142 * Parse the __builtin_offsetof() expression.
5144 static expression_t *parse_offsetof(void)
5146 eat(T___builtin_offsetof);
5148 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
5149 expression->base.type = type_size_t;
5152 add_anchor_token(',');
5153 type_t *type = parse_typename();
5154 rem_anchor_token(',');
5156 add_anchor_token(')');
5157 designator_t *designator = parse_designator();
5158 rem_anchor_token(')');
5161 expression->offsetofe.type = type;
5162 expression->offsetofe.designator = designator;
5165 memset(&path, 0, sizeof(path));
5166 path.top_type = type;
5167 path.path = NEW_ARR_F(type_path_entry_t, 0);
5169 descend_into_subtype(&path);
5171 if(!walk_designator(&path, designator, true)) {
5172 return create_invalid_expression();
5175 DEL_ARR_F(path.path);
5179 return create_invalid_expression();
5183 * Parses a _builtin_va_start() expression.
5185 static expression_t *parse_va_start(void)
5187 eat(T___builtin_va_start);
5189 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
5192 add_anchor_token(',');
5193 expression->va_starte.ap = parse_assignment_expression();
5194 rem_anchor_token(',');
5196 expression_t *const expr = parse_assignment_expression();
5197 if (expr->kind == EXPR_REFERENCE) {
5198 declaration_t *const decl = expr->reference.declaration;
5200 return create_invalid_expression();
5201 if (decl->parent_scope == ¤t_function->scope &&
5202 decl->next == NULL) {
5203 expression->va_starte.parameter = decl;
5208 errorf(&expr->base.source_position,
5209 "second argument of 'va_start' must be last parameter of the current function");
5211 return create_invalid_expression();
5215 * Parses a _builtin_va_arg() expression.
5217 static expression_t *parse_va_arg(void)
5219 eat(T___builtin_va_arg);
5221 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
5224 expression->va_arge.ap = parse_assignment_expression();
5226 expression->base.type = parse_typename();
5231 return create_invalid_expression();
5234 static expression_t *parse_builtin_symbol(void)
5236 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
5238 symbol_t *symbol = token.v.symbol;
5240 expression->builtin_symbol.symbol = symbol;
5243 type_t *type = get_builtin_symbol_type(symbol);
5244 type = automatic_type_conversion(type);
5246 expression->base.type = type;
5251 * Parses a __builtin_constant() expression.
5253 static expression_t *parse_builtin_constant(void)
5255 eat(T___builtin_constant_p);
5257 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
5260 add_anchor_token(')');
5261 expression->builtin_constant.value = parse_assignment_expression();
5262 rem_anchor_token(')');
5264 expression->base.type = type_int;
5268 return create_invalid_expression();
5272 * Parses a __builtin_prefetch() expression.
5274 static expression_t *parse_builtin_prefetch(void)
5276 eat(T___builtin_prefetch);
5278 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
5281 add_anchor_token(')');
5282 expression->builtin_prefetch.adr = parse_assignment_expression();
5283 if (token.type == ',') {
5285 expression->builtin_prefetch.rw = parse_assignment_expression();
5287 if (token.type == ',') {
5289 expression->builtin_prefetch.locality = parse_assignment_expression();
5291 rem_anchor_token(')');
5293 expression->base.type = type_void;
5297 return create_invalid_expression();
5301 * Parses a __builtin_is_*() compare expression.
5303 static expression_t *parse_compare_builtin(void)
5305 expression_t *expression;
5307 switch(token.type) {
5308 case T___builtin_isgreater:
5309 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
5311 case T___builtin_isgreaterequal:
5312 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
5314 case T___builtin_isless:
5315 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
5317 case T___builtin_islessequal:
5318 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
5320 case T___builtin_islessgreater:
5321 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
5323 case T___builtin_isunordered:
5324 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
5327 internal_errorf(HERE, "invalid compare builtin found");
5330 expression->base.source_position = *HERE;
5334 expression->binary.left = parse_assignment_expression();
5336 expression->binary.right = parse_assignment_expression();
5339 type_t *const orig_type_left = expression->binary.left->base.type;
5340 type_t *const orig_type_right = expression->binary.right->base.type;
5342 type_t *const type_left = skip_typeref(orig_type_left);
5343 type_t *const type_right = skip_typeref(orig_type_right);
5344 if(!is_type_float(type_left) && !is_type_float(type_right)) {
5345 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5346 type_error_incompatible("invalid operands in comparison",
5347 &expression->base.source_position, orig_type_left, orig_type_right);
5350 semantic_comparison(&expression->binary);
5355 return create_invalid_expression();
5359 * Parses a __builtin_expect() expression.
5361 static expression_t *parse_builtin_expect(void)
5363 eat(T___builtin_expect);
5365 expression_t *expression
5366 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
5369 expression->binary.left = parse_assignment_expression();
5371 expression->binary.right = parse_constant_expression();
5374 expression->base.type = expression->binary.left->base.type;
5378 return create_invalid_expression();
5382 * Parses a MS assume() expression.
5384 static expression_t *parse_assume(void) {
5387 expression_t *expression
5388 = allocate_expression_zero(EXPR_UNARY_ASSUME);
5391 add_anchor_token(')');
5392 expression->unary.value = parse_assignment_expression();
5393 rem_anchor_token(')');
5396 expression->base.type = type_void;
5399 return create_invalid_expression();
5403 * Parse a microsoft __noop expression.
5405 static expression_t *parse_noop_expression(void) {
5406 source_position_t source_position = *HERE;
5409 if (token.type == '(') {
5410 /* parse arguments */
5412 add_anchor_token(')');
5413 add_anchor_token(',');
5415 if(token.type != ')') {
5417 (void)parse_assignment_expression();
5418 if(token.type != ',')
5424 rem_anchor_token(',');
5425 rem_anchor_token(')');
5428 /* the result is a (int)0 */
5429 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
5430 cnst->base.source_position = source_position;
5431 cnst->base.type = type_int;
5432 cnst->conste.v.int_value = 0;
5433 cnst->conste.is_ms_noop = true;
5438 return create_invalid_expression();
5442 * Parses a primary expression.
5444 static expression_t *parse_primary_expression(void)
5446 switch (token.type) {
5447 case T_INTEGER: return parse_int_const();
5448 case T_CHARACTER_CONSTANT: return parse_character_constant();
5449 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
5450 case T_FLOATINGPOINT: return parse_float_const();
5451 case T_STRING_LITERAL:
5452 case T_WIDE_STRING_LITERAL: return parse_string_const();
5453 case T_IDENTIFIER: return parse_reference();
5454 case T___FUNCTION__:
5455 case T___func__: return parse_function_keyword();
5456 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
5457 case T___FUNCSIG__: return parse_funcsig_keyword();
5458 case T___FUNCDNAME__: return parse_funcdname_keyword();
5459 case T___builtin_offsetof: return parse_offsetof();
5460 case T___builtin_va_start: return parse_va_start();
5461 case T___builtin_va_arg: return parse_va_arg();
5462 case T___builtin_expect: return parse_builtin_expect();
5463 case T___builtin_alloca:
5464 case T___builtin_nan:
5465 case T___builtin_nand:
5466 case T___builtin_nanf:
5467 case T___builtin_va_end: return parse_builtin_symbol();
5468 case T___builtin_isgreater:
5469 case T___builtin_isgreaterequal:
5470 case T___builtin_isless:
5471 case T___builtin_islessequal:
5472 case T___builtin_islessgreater:
5473 case T___builtin_isunordered: return parse_compare_builtin();
5474 case T___builtin_constant_p: return parse_builtin_constant();
5475 case T___builtin_prefetch: return parse_builtin_prefetch();
5476 case T__assume: return parse_assume();
5478 case '(': return parse_brace_expression();
5479 case T___noop: return parse_noop_expression();
5482 errorf(HERE, "unexpected token %K, expected an expression", &token);
5483 return create_invalid_expression();
5487 * Check if the expression has the character type and issue a warning then.
5489 static void check_for_char_index_type(const expression_t *expression) {
5490 type_t *const type = expression->base.type;
5491 const type_t *const base_type = skip_typeref(type);
5493 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
5494 warning.char_subscripts) {
5495 warningf(&expression->base.source_position,
5496 "array subscript has type '%T'", type);
5500 static expression_t *parse_array_expression(unsigned precedence,
5506 add_anchor_token(']');
5508 expression_t *inside = parse_expression();
5510 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
5512 array_access_expression_t *array_access = &expression->array_access;
5514 type_t *const orig_type_left = left->base.type;
5515 type_t *const orig_type_inside = inside->base.type;
5517 type_t *const type_left = skip_typeref(orig_type_left);
5518 type_t *const type_inside = skip_typeref(orig_type_inside);
5520 type_t *return_type;
5521 if (is_type_pointer(type_left)) {
5522 return_type = type_left->pointer.points_to;
5523 array_access->array_ref = left;
5524 array_access->index = inside;
5525 check_for_char_index_type(inside);
5526 } else if (is_type_pointer(type_inside)) {
5527 return_type = type_inside->pointer.points_to;
5528 array_access->array_ref = inside;
5529 array_access->index = left;
5530 array_access->flipped = true;
5531 check_for_char_index_type(left);
5533 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
5535 "array access on object with non-pointer types '%T', '%T'",
5536 orig_type_left, orig_type_inside);
5538 return_type = type_error_type;
5539 array_access->array_ref = create_invalid_expression();
5542 rem_anchor_token(']');
5543 if(token.type != ']') {
5544 parse_error_expected("Problem while parsing array access", ']', 0);
5549 return_type = automatic_type_conversion(return_type);
5550 expression->base.type = return_type;
5555 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
5557 expression_t *tp_expression = allocate_expression_zero(kind);
5558 tp_expression->base.type = type_size_t;
5560 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
5562 add_anchor_token(')');
5563 tp_expression->typeprop.type = parse_typename();
5564 rem_anchor_token(')');
5567 expression_t *expression = parse_sub_expression(precedence);
5568 expression->base.type = revert_automatic_type_conversion(expression);
5570 tp_expression->typeprop.type = expression->base.type;
5571 tp_expression->typeprop.tp_expression = expression;
5574 return tp_expression;
5576 return create_invalid_expression();
5579 static expression_t *parse_sizeof(unsigned precedence)
5582 return parse_typeprop(EXPR_SIZEOF, precedence);
5585 static expression_t *parse_alignof(unsigned precedence)
5588 return parse_typeprop(EXPR_SIZEOF, precedence);
5591 static expression_t *parse_select_expression(unsigned precedence,
5592 expression_t *compound)
5595 assert(token.type == '.' || token.type == T_MINUSGREATER);
5597 bool is_pointer = (token.type == T_MINUSGREATER);
5600 expression_t *select = allocate_expression_zero(EXPR_SELECT);
5601 select->select.compound = compound;
5603 if(token.type != T_IDENTIFIER) {
5604 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
5607 symbol_t *symbol = token.v.symbol;
5608 select->select.symbol = symbol;
5611 type_t *const orig_type = compound->base.type;
5612 type_t *const type = skip_typeref(orig_type);
5614 type_t *type_left = type;
5616 if (!is_type_pointer(type)) {
5617 if (is_type_valid(type)) {
5618 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
5620 return create_invalid_expression();
5622 type_left = type->pointer.points_to;
5624 type_left = skip_typeref(type_left);
5626 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
5627 type_left->kind != TYPE_COMPOUND_UNION) {
5628 if (is_type_valid(type_left)) {
5629 errorf(HERE, "request for member '%Y' in something not a struct or "
5630 "union, but '%T'", symbol, type_left);
5632 return create_invalid_expression();
5635 declaration_t *const declaration = type_left->compound.declaration;
5637 if(!declaration->init.is_defined) {
5638 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
5640 return create_invalid_expression();
5643 declaration_t *iter = find_compound_entry(declaration, symbol);
5645 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
5646 return create_invalid_expression();
5649 /* we always do the auto-type conversions; the & and sizeof parser contains
5650 * code to revert this! */
5651 type_t *expression_type = automatic_type_conversion(iter->type);
5653 select->select.compound_entry = iter;
5654 select->base.type = expression_type;
5656 if(expression_type->kind == TYPE_BITFIELD) {
5657 expression_t *extract
5658 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
5659 extract->unary.value = select;
5660 extract->base.type = expression_type->bitfield.base;
5669 * Parse a call expression, ie. expression '( ... )'.
5671 * @param expression the function address
5673 static expression_t *parse_call_expression(unsigned precedence,
5674 expression_t *expression)
5677 expression_t *result = allocate_expression_zero(EXPR_CALL);
5678 result->base.source_position = expression->base.source_position;
5680 call_expression_t *call = &result->call;
5681 call->function = expression;
5683 type_t *const orig_type = expression->base.type;
5684 type_t *const type = skip_typeref(orig_type);
5686 function_type_t *function_type = NULL;
5687 if (is_type_pointer(type)) {
5688 type_t *const to_type = skip_typeref(type->pointer.points_to);
5690 if (is_type_function(to_type)) {
5691 function_type = &to_type->function;
5692 call->base.type = function_type->return_type;
5696 if (function_type == NULL && is_type_valid(type)) {
5697 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5700 /* parse arguments */
5702 add_anchor_token(')');
5703 add_anchor_token(',');
5705 if(token.type != ')') {
5706 call_argument_t *last_argument = NULL;
5709 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5711 argument->expression = parse_assignment_expression();
5712 if(last_argument == NULL) {
5713 call->arguments = argument;
5715 last_argument->next = argument;
5717 last_argument = argument;
5719 if(token.type != ',')
5724 rem_anchor_token(',');
5725 rem_anchor_token(')');
5728 if(function_type != NULL) {
5729 function_parameter_t *parameter = function_type->parameters;
5730 call_argument_t *argument = call->arguments;
5731 for( ; parameter != NULL && argument != NULL;
5732 parameter = parameter->next, argument = argument->next) {
5733 type_t *expected_type = parameter->type;
5734 /* TODO report scope in error messages */
5735 expression_t *const arg_expr = argument->expression;
5736 type_t *const res_type = semantic_assign(expected_type, arg_expr,
5738 &arg_expr->base.source_position);
5739 if (res_type == NULL) {
5740 /* TODO improve error message */
5741 errorf(&arg_expr->base.source_position,
5742 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5743 arg_expr, arg_expr->base.type, expected_type);
5745 argument->expression = create_implicit_cast(argument->expression, expected_type);
5748 /* too few parameters */
5749 if(parameter != NULL) {
5750 errorf(HERE, "too few arguments to function '%E'", expression);
5751 } else if(argument != NULL) {
5752 /* too many parameters */
5753 if(!function_type->variadic
5754 && !function_type->unspecified_parameters) {
5755 errorf(HERE, "too many arguments to function '%E'", expression);
5757 /* do default promotion */
5758 for( ; argument != NULL; argument = argument->next) {
5759 type_t *type = argument->expression->base.type;
5761 type = skip_typeref(type);
5762 if(is_type_integer(type)) {
5763 type = promote_integer(type);
5764 } else if(type == type_float) {
5768 argument->expression
5769 = create_implicit_cast(argument->expression, type);
5772 check_format(&result->call);
5775 check_format(&result->call);
5781 return create_invalid_expression();
5784 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5786 static bool same_compound_type(const type_t *type1, const type_t *type2)
5789 is_type_compound(type1) &&
5790 type1->kind == type2->kind &&
5791 type1->compound.declaration == type2->compound.declaration;
5795 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5797 * @param expression the conditional expression
5799 static expression_t *parse_conditional_expression(unsigned precedence,
5800 expression_t *expression)
5803 add_anchor_token(':');
5805 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5807 conditional_expression_t *conditional = &result->conditional;
5808 conditional->condition = expression;
5811 type_t *const condition_type_orig = expression->base.type;
5812 type_t *const condition_type = skip_typeref(condition_type_orig);
5813 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5814 type_error("expected a scalar type in conditional condition",
5815 &expression->base.source_position, condition_type_orig);
5818 expression_t *true_expression = parse_expression();
5819 rem_anchor_token(':');
5821 expression_t *false_expression = parse_sub_expression(precedence);
5823 type_t *const orig_true_type = true_expression->base.type;
5824 type_t *const orig_false_type = false_expression->base.type;
5825 type_t *const true_type = skip_typeref(orig_true_type);
5826 type_t *const false_type = skip_typeref(orig_false_type);
5829 type_t *result_type;
5830 if(is_type_atomic(true_type, ATOMIC_TYPE_VOID) ||
5831 is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5832 if (!is_type_atomic(true_type, ATOMIC_TYPE_VOID)
5833 || !is_type_atomic(false_type, ATOMIC_TYPE_VOID)) {
5834 warningf(&expression->base.source_position,
5835 "ISO C forbids conditional expression with only one void side");
5837 result_type = type_void;
5838 } else if (is_type_arithmetic(true_type)
5839 && is_type_arithmetic(false_type)) {
5840 result_type = semantic_arithmetic(true_type, false_type);
5842 true_expression = create_implicit_cast(true_expression, result_type);
5843 false_expression = create_implicit_cast(false_expression, result_type);
5845 conditional->true_expression = true_expression;
5846 conditional->false_expression = false_expression;
5847 conditional->base.type = result_type;
5848 } else if (same_compound_type(true_type, false_type)) {
5849 /* just take 1 of the 2 types */
5850 result_type = true_type;
5851 } else if (is_type_pointer(true_type) || is_type_pointer(false_type)) {
5852 type_t *pointer_type;
5854 expression_t *other_expression;
5855 if (is_type_pointer(true_type)) {
5856 pointer_type = true_type;
5857 other_type = false_type;
5858 other_expression = false_expression;
5860 pointer_type = false_type;
5861 other_type = true_type;
5862 other_expression = true_expression;
5865 if(is_type_pointer(other_type)) {
5866 if(!pointers_compatible(true_type, false_type)) {
5867 warningf(&expression->base.source_position,
5868 "pointer types '%T' and '%T' in conditional expression are incompatible", true_type, false_type);
5870 result_type = true_type;
5871 } else if(is_null_pointer_constant(other_expression)) {
5872 result_type = pointer_type;
5873 } else if(is_type_integer(other_type)) {
5874 warningf(&expression->base.source_position,
5875 "pointer/integer type mismatch in conditional expression ('%T' and '%T')", true_type, false_type);
5876 result_type = pointer_type;
5878 type_error_incompatible("while parsing conditional",
5879 &expression->base.source_position, true_type, false_type);
5880 result_type = type_error_type;
5883 /* TODO: one pointer to void*, other some pointer */
5885 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5886 type_error_incompatible("while parsing conditional",
5887 &expression->base.source_position, true_type,
5890 result_type = type_error_type;
5893 conditional->true_expression
5894 = create_implicit_cast(true_expression, result_type);
5895 conditional->false_expression
5896 = create_implicit_cast(false_expression, result_type);
5897 conditional->base.type = result_type;
5900 return create_invalid_expression();
5904 * Parse an extension expression.
5906 static expression_t *parse_extension(unsigned precedence)
5908 eat(T___extension__);
5910 /* TODO enable extensions */
5911 expression_t *expression = parse_sub_expression(precedence);
5912 /* TODO disable extensions */
5917 * Parse a __builtin_classify_type() expression.
5919 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5921 eat(T___builtin_classify_type);
5923 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5924 result->base.type = type_int;
5927 add_anchor_token(')');
5928 expression_t *expression = parse_sub_expression(precedence);
5929 rem_anchor_token(')');
5931 result->classify_type.type_expression = expression;
5935 return create_invalid_expression();
5938 static void semantic_incdec(unary_expression_t *expression)
5940 type_t *const orig_type = expression->value->base.type;
5941 type_t *const type = skip_typeref(orig_type);
5942 /* TODO !is_type_real && !is_type_pointer */
5943 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5944 if (is_type_valid(type)) {
5945 /* TODO: improve error message */
5946 errorf(HERE, "operation needs an arithmetic or pointer type");
5951 expression->base.type = orig_type;
5954 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5956 type_t *const orig_type = expression->value->base.type;
5957 type_t *const type = skip_typeref(orig_type);
5958 if(!is_type_arithmetic(type)) {
5959 if (is_type_valid(type)) {
5960 /* TODO: improve error message */
5961 errorf(HERE, "operation needs an arithmetic type");
5966 expression->base.type = orig_type;
5969 static void semantic_unexpr_scalar(unary_expression_t *expression)
5971 type_t *const orig_type = expression->value->base.type;
5972 type_t *const type = skip_typeref(orig_type);
5973 if (!is_type_scalar(type)) {
5974 if (is_type_valid(type)) {
5975 errorf(HERE, "operand of ! must be of scalar type");
5980 expression->base.type = orig_type;
5983 static void semantic_unexpr_integer(unary_expression_t *expression)
5985 type_t *const orig_type = expression->value->base.type;
5986 type_t *const type = skip_typeref(orig_type);
5987 if (!is_type_integer(type)) {
5988 if (is_type_valid(type)) {
5989 errorf(HERE, "operand of ~ must be of integer type");
5994 expression->base.type = orig_type;
5997 static void semantic_dereference(unary_expression_t *expression)
5999 type_t *const orig_type = expression->value->base.type;
6000 type_t *const type = skip_typeref(orig_type);
6001 if(!is_type_pointer(type)) {
6002 if (is_type_valid(type)) {
6003 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
6008 type_t *result_type = type->pointer.points_to;
6009 result_type = automatic_type_conversion(result_type);
6010 expression->base.type = result_type;
6014 * Check the semantic of the address taken expression.
6016 static void semantic_take_addr(unary_expression_t *expression)
6018 expression_t *value = expression->value;
6019 value->base.type = revert_automatic_type_conversion(value);
6021 type_t *orig_type = value->base.type;
6022 if(!is_type_valid(orig_type))
6025 if(value->kind == EXPR_REFERENCE) {
6026 declaration_t *const declaration = value->reference.declaration;
6027 if(declaration != NULL) {
6028 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
6029 errorf(&expression->base.source_position,
6030 "address of register variable '%Y' requested",
6031 declaration->symbol);
6033 declaration->address_taken = 1;
6037 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
6040 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
6041 static expression_t *parse_##unexpression_type(unsigned precedence) \
6045 expression_t *unary_expression \
6046 = allocate_expression_zero(unexpression_type); \
6047 unary_expression->base.source_position = *HERE; \
6048 unary_expression->unary.value = parse_sub_expression(precedence); \
6050 sfunc(&unary_expression->unary); \
6052 return unary_expression; \
6055 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
6056 semantic_unexpr_arithmetic)
6057 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
6058 semantic_unexpr_arithmetic)
6059 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
6060 semantic_unexpr_scalar)
6061 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
6062 semantic_dereference)
6063 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
6065 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
6066 semantic_unexpr_integer)
6067 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
6069 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
6072 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
6074 static expression_t *parse_##unexpression_type(unsigned precedence, \
6075 expression_t *left) \
6077 (void) precedence; \
6080 expression_t *unary_expression \
6081 = allocate_expression_zero(unexpression_type); \
6082 unary_expression->unary.value = left; \
6084 sfunc(&unary_expression->unary); \
6086 return unary_expression; \
6089 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
6090 EXPR_UNARY_POSTFIX_INCREMENT,
6092 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
6093 EXPR_UNARY_POSTFIX_DECREMENT,
6096 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
6098 /* TODO: handle complex + imaginary types */
6100 /* § 6.3.1.8 Usual arithmetic conversions */
6101 if(type_left == type_long_double || type_right == type_long_double) {
6102 return type_long_double;
6103 } else if(type_left == type_double || type_right == type_double) {
6105 } else if(type_left == type_float || type_right == type_float) {
6109 type_right = promote_integer(type_right);
6110 type_left = promote_integer(type_left);
6112 if(type_left == type_right)
6115 bool signed_left = is_type_signed(type_left);
6116 bool signed_right = is_type_signed(type_right);
6117 int rank_left = get_rank(type_left);
6118 int rank_right = get_rank(type_right);
6119 if(rank_left < rank_right) {
6120 if(signed_left == signed_right || !signed_right) {
6126 if(signed_left == signed_right || !signed_left) {
6135 * Check the semantic restrictions for a binary expression.
6137 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
6139 expression_t *const left = expression->left;
6140 expression_t *const right = expression->right;
6141 type_t *const orig_type_left = left->base.type;
6142 type_t *const orig_type_right = right->base.type;
6143 type_t *const type_left = skip_typeref(orig_type_left);
6144 type_t *const type_right = skip_typeref(orig_type_right);
6146 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6147 /* TODO: improve error message */
6148 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6149 errorf(HERE, "operation needs arithmetic types");
6154 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6155 expression->left = create_implicit_cast(left, arithmetic_type);
6156 expression->right = create_implicit_cast(right, arithmetic_type);
6157 expression->base.type = arithmetic_type;
6160 static void semantic_shift_op(binary_expression_t *expression)
6162 expression_t *const left = expression->left;
6163 expression_t *const right = expression->right;
6164 type_t *const orig_type_left = left->base.type;
6165 type_t *const orig_type_right = right->base.type;
6166 type_t * type_left = skip_typeref(orig_type_left);
6167 type_t * type_right = skip_typeref(orig_type_right);
6169 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
6170 /* TODO: improve error message */
6171 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6172 errorf(HERE, "operation needs integer types");
6177 type_left = promote_integer(type_left);
6178 type_right = promote_integer(type_right);
6180 expression->left = create_implicit_cast(left, type_left);
6181 expression->right = create_implicit_cast(right, type_right);
6182 expression->base.type = type_left;
6185 static void semantic_add(binary_expression_t *expression)
6187 expression_t *const left = expression->left;
6188 expression_t *const right = expression->right;
6189 type_t *const orig_type_left = left->base.type;
6190 type_t *const orig_type_right = right->base.type;
6191 type_t *const type_left = skip_typeref(orig_type_left);
6192 type_t *const type_right = skip_typeref(orig_type_right);
6195 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6196 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6197 expression->left = create_implicit_cast(left, arithmetic_type);
6198 expression->right = create_implicit_cast(right, arithmetic_type);
6199 expression->base.type = arithmetic_type;
6201 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6202 expression->base.type = type_left;
6203 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
6204 expression->base.type = type_right;
6205 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6206 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
6210 static void semantic_sub(binary_expression_t *expression)
6212 expression_t *const left = expression->left;
6213 expression_t *const right = expression->right;
6214 type_t *const orig_type_left = left->base.type;
6215 type_t *const orig_type_right = right->base.type;
6216 type_t *const type_left = skip_typeref(orig_type_left);
6217 type_t *const type_right = skip_typeref(orig_type_right);
6220 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6221 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6222 expression->left = create_implicit_cast(left, arithmetic_type);
6223 expression->right = create_implicit_cast(right, arithmetic_type);
6224 expression->base.type = arithmetic_type;
6226 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
6227 expression->base.type = type_left;
6228 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
6229 if(!pointers_compatible(type_left, type_right)) {
6231 "pointers to incompatible objects to binary '-' ('%T', '%T')",
6232 orig_type_left, orig_type_right);
6234 expression->base.type = type_ptrdiff_t;
6236 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6237 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
6238 orig_type_left, orig_type_right);
6243 * Check the semantics of comparison expressions.
6245 * @param expression The expression to check.
6247 static void semantic_comparison(binary_expression_t *expression)
6249 expression_t *left = expression->left;
6250 expression_t *right = expression->right;
6251 type_t *orig_type_left = left->base.type;
6252 type_t *orig_type_right = right->base.type;
6254 type_t *type_left = skip_typeref(orig_type_left);
6255 type_t *type_right = skip_typeref(orig_type_right);
6257 /* TODO non-arithmetic types */
6258 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6259 if (warning.sign_compare &&
6260 (expression->base.kind != EXPR_BINARY_EQUAL &&
6261 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
6262 (is_type_signed(type_left) != is_type_signed(type_right))) {
6263 warningf(&expression->base.source_position,
6264 "comparison between signed and unsigned");
6266 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6267 expression->left = create_implicit_cast(left, arithmetic_type);
6268 expression->right = create_implicit_cast(right, arithmetic_type);
6269 expression->base.type = arithmetic_type;
6270 if (warning.float_equal &&
6271 (expression->base.kind == EXPR_BINARY_EQUAL ||
6272 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
6273 is_type_float(arithmetic_type)) {
6274 warningf(&expression->base.source_position,
6275 "comparing floating point with == or != is unsafe");
6277 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
6278 /* TODO check compatibility */
6279 } else if (is_type_pointer(type_left)) {
6280 expression->right = create_implicit_cast(right, type_left);
6281 } else if (is_type_pointer(type_right)) {
6282 expression->left = create_implicit_cast(left, type_right);
6283 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6284 type_error_incompatible("invalid operands in comparison",
6285 &expression->base.source_position,
6286 type_left, type_right);
6288 expression->base.type = type_int;
6291 static void semantic_arithmetic_assign(binary_expression_t *expression)
6293 expression_t *left = expression->left;
6294 expression_t *right = expression->right;
6295 type_t *orig_type_left = left->base.type;
6296 type_t *orig_type_right = right->base.type;
6298 type_t *type_left = skip_typeref(orig_type_left);
6299 type_t *type_right = skip_typeref(orig_type_right);
6301 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
6302 /* TODO: improve error message */
6303 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6304 errorf(HERE, "operation needs arithmetic types");
6309 /* combined instructions are tricky. We can't create an implicit cast on
6310 * the left side, because we need the uncasted form for the store.
6311 * The ast2firm pass has to know that left_type must be right_type
6312 * for the arithmetic operation and create a cast by itself */
6313 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
6314 expression->right = create_implicit_cast(right, arithmetic_type);
6315 expression->base.type = type_left;
6318 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
6320 expression_t *const left = expression->left;
6321 expression_t *const right = expression->right;
6322 type_t *const orig_type_left = left->base.type;
6323 type_t *const orig_type_right = right->base.type;
6324 type_t *const type_left = skip_typeref(orig_type_left);
6325 type_t *const type_right = skip_typeref(orig_type_right);
6327 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
6328 /* combined instructions are tricky. We can't create an implicit cast on
6329 * the left side, because we need the uncasted form for the store.
6330 * The ast2firm pass has to know that left_type must be right_type
6331 * for the arithmetic operation and create a cast by itself */
6332 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
6333 expression->right = create_implicit_cast(right, arithmetic_type);
6334 expression->base.type = type_left;
6335 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
6336 expression->base.type = type_left;
6337 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
6338 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
6343 * Check the semantic restrictions of a logical expression.
6345 static void semantic_logical_op(binary_expression_t *expression)
6347 expression_t *const left = expression->left;
6348 expression_t *const right = expression->right;
6349 type_t *const orig_type_left = left->base.type;
6350 type_t *const orig_type_right = right->base.type;
6351 type_t *const type_left = skip_typeref(orig_type_left);
6352 type_t *const type_right = skip_typeref(orig_type_right);
6354 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
6355 /* TODO: improve error message */
6356 if (is_type_valid(type_left) && is_type_valid(type_right)) {
6357 errorf(HERE, "operation needs scalar types");
6362 expression->base.type = type_int;
6366 * Checks if a compound type has constant fields.
6368 static bool has_const_fields(const compound_type_t *type)
6370 const scope_t *scope = &type->declaration->scope;
6371 const declaration_t *declaration = scope->declarations;
6373 for (; declaration != NULL; declaration = declaration->next) {
6374 if (declaration->namespc != NAMESPACE_NORMAL)
6377 const type_t *decl_type = skip_typeref(declaration->type);
6378 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
6386 * Check the semantic restrictions of a binary assign expression.
6388 static void semantic_binexpr_assign(binary_expression_t *expression)
6390 expression_t *left = expression->left;
6391 type_t *orig_type_left = left->base.type;
6393 type_t *type_left = revert_automatic_type_conversion(left);
6394 type_left = skip_typeref(orig_type_left);
6396 /* must be a modifiable lvalue */
6397 if (is_type_array(type_left)) {
6398 errorf(HERE, "cannot assign to arrays ('%E')", left);
6401 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
6402 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
6406 if(is_type_incomplete(type_left)) {
6408 "left-hand side of assignment '%E' has incomplete type '%T'",
6409 left, orig_type_left);
6412 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
6413 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
6414 left, orig_type_left);
6418 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
6419 "assignment", &left->base.source_position);
6420 if (res_type == NULL) {
6421 errorf(&expression->base.source_position,
6422 "cannot assign to '%T' from '%T'",
6423 orig_type_left, expression->right->base.type);
6425 expression->right = create_implicit_cast(expression->right, res_type);
6428 expression->base.type = orig_type_left;
6432 * Determine if the outermost operation (or parts thereof) of the given
6433 * expression has no effect in order to generate a warning about this fact.
6434 * Therefore in some cases this only examines some of the operands of the
6435 * expression (see comments in the function and examples below).
6437 * f() + 23; // warning, because + has no effect
6438 * x || f(); // no warning, because x controls execution of f()
6439 * x ? y : f(); // warning, because y has no effect
6440 * (void)x; // no warning to be able to suppress the warning
6441 * This function can NOT be used for an "expression has definitely no effect"-
6443 static bool expression_has_effect(const expression_t *const expr)
6445 switch (expr->kind) {
6446 case EXPR_UNKNOWN: break;
6447 case EXPR_INVALID: return true; /* do NOT warn */
6448 case EXPR_REFERENCE: return false;
6449 /* suppress the warning for microsoft __noop operations */
6450 case EXPR_CONST: return expr->conste.is_ms_noop;
6451 case EXPR_CHARACTER_CONSTANT: return false;
6452 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
6453 case EXPR_STRING_LITERAL: return false;
6454 case EXPR_WIDE_STRING_LITERAL: return false;
6457 const call_expression_t *const call = &expr->call;
6458 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
6461 switch (call->function->builtin_symbol.symbol->ID) {
6462 case T___builtin_va_end: return true;
6463 default: return false;
6467 /* Generate the warning if either the left or right hand side of a
6468 * conditional expression has no effect */
6469 case EXPR_CONDITIONAL: {
6470 const conditional_expression_t *const cond = &expr->conditional;
6472 expression_has_effect(cond->true_expression) &&
6473 expression_has_effect(cond->false_expression);
6476 case EXPR_SELECT: return false;
6477 case EXPR_ARRAY_ACCESS: return false;
6478 case EXPR_SIZEOF: return false;
6479 case EXPR_CLASSIFY_TYPE: return false;
6480 case EXPR_ALIGNOF: return false;
6482 case EXPR_FUNCNAME: return false;
6483 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
6484 case EXPR_BUILTIN_CONSTANT_P: return false;
6485 case EXPR_BUILTIN_PREFETCH: return true;
6486 case EXPR_OFFSETOF: return false;
6487 case EXPR_VA_START: return true;
6488 case EXPR_VA_ARG: return true;
6489 case EXPR_STATEMENT: return true; // TODO
6490 case EXPR_COMPOUND_LITERAL: return false;
6492 case EXPR_UNARY_NEGATE: return false;
6493 case EXPR_UNARY_PLUS: return false;
6494 case EXPR_UNARY_BITWISE_NEGATE: return false;
6495 case EXPR_UNARY_NOT: return false;
6496 case EXPR_UNARY_DEREFERENCE: return false;
6497 case EXPR_UNARY_TAKE_ADDRESS: return false;
6498 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
6499 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
6500 case EXPR_UNARY_PREFIX_INCREMENT: return true;
6501 case EXPR_UNARY_PREFIX_DECREMENT: return true;
6503 /* Treat void casts as if they have an effect in order to being able to
6504 * suppress the warning */
6505 case EXPR_UNARY_CAST: {
6506 type_t *const type = skip_typeref(expr->base.type);
6507 return is_type_atomic(type, ATOMIC_TYPE_VOID);
6510 case EXPR_UNARY_CAST_IMPLICIT: return true;
6511 case EXPR_UNARY_ASSUME: return true;
6512 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
6514 case EXPR_BINARY_ADD: return false;
6515 case EXPR_BINARY_SUB: return false;
6516 case EXPR_BINARY_MUL: return false;
6517 case EXPR_BINARY_DIV: return false;
6518 case EXPR_BINARY_MOD: return false;
6519 case EXPR_BINARY_EQUAL: return false;
6520 case EXPR_BINARY_NOTEQUAL: return false;
6521 case EXPR_BINARY_LESS: return false;
6522 case EXPR_BINARY_LESSEQUAL: return false;
6523 case EXPR_BINARY_GREATER: return false;
6524 case EXPR_BINARY_GREATEREQUAL: return false;
6525 case EXPR_BINARY_BITWISE_AND: return false;
6526 case EXPR_BINARY_BITWISE_OR: return false;
6527 case EXPR_BINARY_BITWISE_XOR: return false;
6528 case EXPR_BINARY_SHIFTLEFT: return false;
6529 case EXPR_BINARY_SHIFTRIGHT: return false;
6530 case EXPR_BINARY_ASSIGN: return true;
6531 case EXPR_BINARY_MUL_ASSIGN: return true;
6532 case EXPR_BINARY_DIV_ASSIGN: return true;
6533 case EXPR_BINARY_MOD_ASSIGN: return true;
6534 case EXPR_BINARY_ADD_ASSIGN: return true;
6535 case EXPR_BINARY_SUB_ASSIGN: return true;
6536 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
6537 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
6538 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
6539 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
6540 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
6542 /* Only examine the right hand side of && and ||, because the left hand
6543 * side already has the effect of controlling the execution of the right
6545 case EXPR_BINARY_LOGICAL_AND:
6546 case EXPR_BINARY_LOGICAL_OR:
6547 /* Only examine the right hand side of a comma expression, because the left
6548 * hand side has a separate warning */
6549 case EXPR_BINARY_COMMA:
6550 return expression_has_effect(expr->binary.right);
6552 case EXPR_BINARY_BUILTIN_EXPECT: return true;
6553 case EXPR_BINARY_ISGREATER: return false;
6554 case EXPR_BINARY_ISGREATEREQUAL: return false;
6555 case EXPR_BINARY_ISLESS: return false;
6556 case EXPR_BINARY_ISLESSEQUAL: return false;
6557 case EXPR_BINARY_ISLESSGREATER: return false;
6558 case EXPR_BINARY_ISUNORDERED: return false;
6561 internal_errorf(HERE, "unexpected expression");
6564 static void semantic_comma(binary_expression_t *expression)
6566 if (warning.unused_value) {
6567 const expression_t *const left = expression->left;
6568 if (!expression_has_effect(left)) {
6569 warningf(&left->base.source_position,
6570 "left-hand operand of comma expression has no effect");
6573 expression->base.type = expression->right->base.type;
6576 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
6577 static expression_t *parse_##binexpression_type(unsigned precedence, \
6578 expression_t *left) \
6581 source_position_t pos = *HERE; \
6583 expression_t *right = parse_sub_expression(precedence + lr); \
6585 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
6586 binexpr->base.source_position = pos; \
6587 binexpr->binary.left = left; \
6588 binexpr->binary.right = right; \
6589 sfunc(&binexpr->binary); \
6594 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
6595 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
6596 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
6597 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
6598 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
6599 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
6600 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
6601 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
6602 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
6604 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
6605 semantic_comparison, 1)
6606 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
6607 semantic_comparison, 1)
6608 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
6609 semantic_comparison, 1)
6610 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
6611 semantic_comparison, 1)
6613 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
6614 semantic_binexpr_arithmetic, 1)
6615 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
6616 semantic_binexpr_arithmetic, 1)
6617 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
6618 semantic_binexpr_arithmetic, 1)
6619 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
6620 semantic_logical_op, 1)
6621 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
6622 semantic_logical_op, 1)
6623 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
6624 semantic_shift_op, 1)
6625 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
6626 semantic_shift_op, 1)
6627 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
6628 semantic_arithmetic_addsubb_assign, 0)
6629 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
6630 semantic_arithmetic_addsubb_assign, 0)
6631 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
6632 semantic_arithmetic_assign, 0)
6633 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
6634 semantic_arithmetic_assign, 0)
6635 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
6636 semantic_arithmetic_assign, 0)
6637 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
6638 semantic_arithmetic_assign, 0)
6639 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6640 semantic_arithmetic_assign, 0)
6641 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
6642 semantic_arithmetic_assign, 0)
6643 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
6644 semantic_arithmetic_assign, 0)
6645 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
6646 semantic_arithmetic_assign, 0)
6648 static expression_t *parse_sub_expression(unsigned precedence)
6650 if(token.type < 0) {
6651 return expected_expression_error();
6654 expression_parser_function_t *parser
6655 = &expression_parsers[token.type];
6656 source_position_t source_position = token.source_position;
6659 if(parser->parser != NULL) {
6660 left = parser->parser(parser->precedence);
6662 left = parse_primary_expression();
6664 assert(left != NULL);
6665 left->base.source_position = source_position;
6668 if(token.type < 0) {
6669 return expected_expression_error();
6672 parser = &expression_parsers[token.type];
6673 if(parser->infix_parser == NULL)
6675 if(parser->infix_precedence < precedence)
6678 left = parser->infix_parser(parser->infix_precedence, left);
6680 assert(left != NULL);
6681 assert(left->kind != EXPR_UNKNOWN);
6682 left->base.source_position = source_position;
6689 * Parse an expression.
6691 static expression_t *parse_expression(void)
6693 return parse_sub_expression(1);
6697 * Register a parser for a prefix-like operator with given precedence.
6699 * @param parser the parser function
6700 * @param token_type the token type of the prefix token
6701 * @param precedence the precedence of the operator
6703 static void register_expression_parser(parse_expression_function parser,
6704 int token_type, unsigned precedence)
6706 expression_parser_function_t *entry = &expression_parsers[token_type];
6708 if(entry->parser != NULL) {
6709 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6710 panic("trying to register multiple expression parsers for a token");
6712 entry->parser = parser;
6713 entry->precedence = precedence;
6717 * Register a parser for an infix operator with given precedence.
6719 * @param parser the parser function
6720 * @param token_type the token type of the infix operator
6721 * @param precedence the precedence of the operator
6723 static void register_infix_parser(parse_expression_infix_function parser,
6724 int token_type, unsigned precedence)
6726 expression_parser_function_t *entry = &expression_parsers[token_type];
6728 if(entry->infix_parser != NULL) {
6729 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6730 panic("trying to register multiple infix expression parsers for a "
6733 entry->infix_parser = parser;
6734 entry->infix_precedence = precedence;
6738 * Initialize the expression parsers.
6740 static void init_expression_parsers(void)
6742 memset(&expression_parsers, 0, sizeof(expression_parsers));
6744 register_infix_parser(parse_array_expression, '[', 30);
6745 register_infix_parser(parse_call_expression, '(', 30);
6746 register_infix_parser(parse_select_expression, '.', 30);
6747 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6748 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6750 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6753 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6754 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6755 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6756 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6757 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6758 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6759 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6760 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6761 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6762 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6763 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6764 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6765 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6766 T_EXCLAMATIONMARKEQUAL, 13);
6767 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6768 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6769 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6770 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6771 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6772 register_infix_parser(parse_conditional_expression, '?', 7);
6773 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6774 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6775 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6776 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6777 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6778 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6779 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6780 T_LESSLESSEQUAL, 2);
6781 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6782 T_GREATERGREATEREQUAL, 2);
6783 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6785 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6787 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6790 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6792 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6793 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6794 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6795 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6796 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6797 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6798 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6800 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6802 register_expression_parser(parse_sizeof, T_sizeof, 25);
6803 register_expression_parser(parse_alignof, T___alignof__, 25);
6804 register_expression_parser(parse_extension, T___extension__, 25);
6805 register_expression_parser(parse_builtin_classify_type,
6806 T___builtin_classify_type, 25);
6810 * Parse a asm statement constraints specification.
6812 static asm_constraint_t *parse_asm_constraints(void)
6814 asm_constraint_t *result = NULL;
6815 asm_constraint_t *last = NULL;
6817 while(token.type == T_STRING_LITERAL || token.type == '[') {
6818 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6819 memset(constraint, 0, sizeof(constraint[0]));
6821 if(token.type == '[') {
6823 if(token.type != T_IDENTIFIER) {
6824 parse_error_expected("while parsing asm constraint",
6828 constraint->symbol = token.v.symbol;
6833 constraint->constraints = parse_string_literals();
6835 constraint->expression = parse_expression();
6839 last->next = constraint;
6841 result = constraint;
6845 if(token.type != ',')
6856 * Parse a asm statement clobber specification.
6858 static asm_clobber_t *parse_asm_clobbers(void)
6860 asm_clobber_t *result = NULL;
6861 asm_clobber_t *last = NULL;
6863 while(token.type == T_STRING_LITERAL) {
6864 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6865 clobber->clobber = parse_string_literals();
6868 last->next = clobber;
6874 if(token.type != ',')
6883 * Parse an asm statement.
6885 static statement_t *parse_asm_statement(void)
6889 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6890 statement->base.source_position = token.source_position;
6892 asm_statement_t *asm_statement = &statement->asms;
6894 if(token.type == T_volatile) {
6896 asm_statement->is_volatile = true;
6900 add_anchor_token(')');
6901 add_anchor_token(':');
6902 asm_statement->asm_text = parse_string_literals();
6904 if(token.type != ':') {
6905 rem_anchor_token(':');
6910 asm_statement->inputs = parse_asm_constraints();
6911 if(token.type != ':') {
6912 rem_anchor_token(':');
6917 asm_statement->outputs = parse_asm_constraints();
6918 if(token.type != ':') {
6919 rem_anchor_token(':');
6922 rem_anchor_token(':');
6925 asm_statement->clobbers = parse_asm_clobbers();
6928 rem_anchor_token(')');
6933 return create_invalid_statement();
6937 * Parse a case statement.
6939 static statement_t *parse_case_statement(void)
6943 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6945 statement->base.source_position = token.source_position;
6946 statement->case_label.expression = parse_expression();
6948 if (c_mode & _GNUC) {
6949 if (token.type == T_DOTDOTDOT) {
6951 statement->case_label.end_range = parse_expression();
6957 if (! is_constant_expression(statement->case_label.expression)) {
6958 errorf(&statement->base.source_position,
6959 "case label does not reduce to an integer constant");
6961 /* TODO: check if the case label is already known */
6962 if (current_switch != NULL) {
6963 /* link all cases into the switch statement */
6964 if (current_switch->last_case == NULL) {
6965 current_switch->first_case =
6966 current_switch->last_case = &statement->case_label;
6968 current_switch->last_case->next = &statement->case_label;
6971 errorf(&statement->base.source_position,
6972 "case label not within a switch statement");
6975 statement->case_label.statement = parse_statement();
6979 return create_invalid_statement();
6983 * Finds an existing default label of a switch statement.
6985 static case_label_statement_t *
6986 find_default_label(const switch_statement_t *statement)
6988 case_label_statement_t *label = statement->first_case;
6989 for ( ; label != NULL; label = label->next) {
6990 if (label->expression == NULL)
6997 * Parse a default statement.
6999 static statement_t *parse_default_statement(void)
7003 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
7005 statement->base.source_position = token.source_position;
7008 if (current_switch != NULL) {
7009 const case_label_statement_t *def_label = find_default_label(current_switch);
7010 if (def_label != NULL) {
7011 errorf(HERE, "multiple default labels in one switch (previous declared %P)",
7012 &def_label->base.source_position);
7014 /* link all cases into the switch statement */
7015 if (current_switch->last_case == NULL) {
7016 current_switch->first_case =
7017 current_switch->last_case = &statement->case_label;
7019 current_switch->last_case->next = &statement->case_label;
7023 errorf(&statement->base.source_position,
7024 "'default' label not within a switch statement");
7026 statement->case_label.statement = parse_statement();
7030 return create_invalid_statement();
7034 * Return the declaration for a given label symbol or create a new one.
7036 static declaration_t *get_label(symbol_t *symbol)
7038 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
7039 assert(current_function != NULL);
7040 /* if we found a label in the same function, then we already created the
7042 if(candidate != NULL
7043 && candidate->parent_scope == ¤t_function->scope) {
7047 /* otherwise we need to create a new one */
7048 declaration_t *const declaration = allocate_declaration_zero();
7049 declaration->namespc = NAMESPACE_LABEL;
7050 declaration->symbol = symbol;
7052 label_push(declaration);
7058 * Parse a label statement.
7060 static statement_t *parse_label_statement(void)
7062 assert(token.type == T_IDENTIFIER);
7063 symbol_t *symbol = token.v.symbol;
7066 declaration_t *label = get_label(symbol);
7068 /* if source position is already set then the label is defined twice,
7069 * otherwise it was just mentioned in a goto so far */
7070 if(label->source_position.input_name != NULL) {
7071 errorf(HERE, "duplicate label '%Y' (declared %P)",
7072 symbol, &label->source_position);
7074 label->source_position = token.source_position;
7077 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
7079 statement->base.source_position = token.source_position;
7080 statement->label.label = label;
7084 if(token.type == '}') {
7085 /* TODO only warn? */
7087 warningf(HERE, "label at end of compound statement");
7088 statement->label.statement = create_empty_statement();
7090 errorf(HERE, "label at end of compound statement");
7091 statement->label.statement = create_invalid_statement();
7095 if (token.type == ';') {
7096 /* eat an empty statement here, to avoid the warning about an empty
7097 * after a label. label:; is commonly used to have a label before
7099 statement->label.statement = create_empty_statement();
7102 statement->label.statement = parse_statement();
7106 /* remember the labels's in a list for later checking */
7107 if (label_last == NULL) {
7108 label_first = &statement->label;
7110 label_last->next = &statement->label;
7112 label_last = &statement->label;
7118 * Parse an if statement.
7120 static statement_t *parse_if(void)
7124 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
7125 statement->base.source_position = token.source_position;
7128 add_anchor_token(')');
7129 statement->ifs.condition = parse_expression();
7130 rem_anchor_token(')');
7133 add_anchor_token(T_else);
7134 statement->ifs.true_statement = parse_statement();
7135 rem_anchor_token(T_else);
7137 if(token.type == T_else) {
7139 statement->ifs.false_statement = parse_statement();
7144 return create_invalid_statement();
7148 * Parse a switch statement.
7150 static statement_t *parse_switch(void)
7154 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
7155 statement->base.source_position = token.source_position;
7158 expression_t *const expr = parse_expression();
7159 type_t * type = skip_typeref(expr->base.type);
7160 if (is_type_integer(type)) {
7161 type = promote_integer(type);
7162 } else if (is_type_valid(type)) {
7163 errorf(&expr->base.source_position,
7164 "switch quantity is not an integer, but '%T'", type);
7165 type = type_error_type;
7167 statement->switchs.expression = create_implicit_cast(expr, type);
7170 switch_statement_t *rem = current_switch;
7171 current_switch = &statement->switchs;
7172 statement->switchs.body = parse_statement();
7173 current_switch = rem;
7175 if(warning.switch_default &&
7176 find_default_label(&statement->switchs) == NULL) {
7177 warningf(&statement->base.source_position, "switch has no default case");
7182 return create_invalid_statement();
7185 static statement_t *parse_loop_body(statement_t *const loop)
7187 statement_t *const rem = current_loop;
7188 current_loop = loop;
7190 statement_t *const body = parse_statement();
7197 * Parse a while statement.
7199 static statement_t *parse_while(void)
7203 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
7204 statement->base.source_position = token.source_position;
7207 add_anchor_token(')');
7208 statement->whiles.condition = parse_expression();
7209 rem_anchor_token(')');
7212 statement->whiles.body = parse_loop_body(statement);
7216 return create_invalid_statement();
7220 * Parse a do statement.
7222 static statement_t *parse_do(void)
7226 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
7228 statement->base.source_position = token.source_position;
7230 add_anchor_token(T_while);
7231 statement->do_while.body = parse_loop_body(statement);
7232 rem_anchor_token(T_while);
7236 add_anchor_token(')');
7237 statement->do_while.condition = parse_expression();
7238 rem_anchor_token(')');
7244 return create_invalid_statement();
7248 * Parse a for statement.
7250 static statement_t *parse_for(void)
7254 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
7255 statement->base.source_position = token.source_position;
7257 int top = environment_top();
7258 scope_t *last_scope = scope;
7259 set_scope(&statement->fors.scope);
7262 add_anchor_token(')');
7264 if(token.type != ';') {
7265 if(is_declaration_specifier(&token, false)) {
7266 parse_declaration(record_declaration);
7268 add_anchor_token(';');
7269 expression_t *const init = parse_expression();
7270 statement->fors.initialisation = init;
7271 if (warning.unused_value && !expression_has_effect(init)) {
7272 warningf(&init->base.source_position,
7273 "initialisation of 'for'-statement has no effect");
7275 rem_anchor_token(';');
7282 if(token.type != ';') {
7283 add_anchor_token(';');
7284 statement->fors.condition = parse_expression();
7285 rem_anchor_token(';');
7288 if(token.type != ')') {
7289 expression_t *const step = parse_expression();
7290 statement->fors.step = step;
7291 if (warning.unused_value && !expression_has_effect(step)) {
7292 warningf(&step->base.source_position,
7293 "step of 'for'-statement has no effect");
7296 rem_anchor_token(')');
7298 statement->fors.body = parse_loop_body(statement);
7300 assert(scope == &statement->fors.scope);
7301 set_scope(last_scope);
7302 environment_pop_to(top);
7307 rem_anchor_token(')');
7308 assert(scope == &statement->fors.scope);
7309 set_scope(last_scope);
7310 environment_pop_to(top);
7312 return create_invalid_statement();
7316 * Parse a goto statement.
7318 static statement_t *parse_goto(void)
7322 if(token.type != T_IDENTIFIER) {
7323 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
7327 symbol_t *symbol = token.v.symbol;
7330 declaration_t *label = get_label(symbol);
7332 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
7333 statement->base.source_position = token.source_position;
7335 statement->gotos.label = label;
7337 /* remember the goto's in a list for later checking */
7338 if (goto_last == NULL) {
7339 goto_first = &statement->gotos;
7341 goto_last->next = &statement->gotos;
7343 goto_last = &statement->gotos;
7349 return create_invalid_statement();
7353 * Parse a continue statement.
7355 static statement_t *parse_continue(void)
7357 statement_t *statement;
7358 if (current_loop == NULL) {
7359 errorf(HERE, "continue statement not within loop");
7362 statement = allocate_statement_zero(STATEMENT_CONTINUE);
7364 statement->base.source_position = token.source_position;
7372 return create_invalid_statement();
7376 * Parse a break statement.
7378 static statement_t *parse_break(void)
7380 statement_t *statement;
7381 if (current_switch == NULL && current_loop == NULL) {
7382 errorf(HERE, "break statement not within loop or switch");
7385 statement = allocate_statement_zero(STATEMENT_BREAK);
7387 statement->base.source_position = token.source_position;
7395 return create_invalid_statement();
7399 * Check if a given declaration represents a local variable.
7401 static bool is_local_var_declaration(const declaration_t *declaration) {
7402 switch ((storage_class_tag_t) declaration->storage_class) {
7403 case STORAGE_CLASS_AUTO:
7404 case STORAGE_CLASS_REGISTER: {
7405 const type_t *type = skip_typeref(declaration->type);
7406 if(is_type_function(type)) {
7418 * Check if a given declaration represents a variable.
7420 static bool is_var_declaration(const declaration_t *declaration) {
7421 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
7424 const type_t *type = skip_typeref(declaration->type);
7425 return !is_type_function(type);
7429 * Check if a given expression represents a local variable.
7431 static bool is_local_variable(const expression_t *expression)
7433 if (expression->base.kind != EXPR_REFERENCE) {
7436 const declaration_t *declaration = expression->reference.declaration;
7437 return is_local_var_declaration(declaration);
7441 * Check if a given expression represents a local variable and
7442 * return its declaration then, else return NULL.
7444 declaration_t *expr_is_variable(const expression_t *expression)
7446 if (expression->base.kind != EXPR_REFERENCE) {
7449 declaration_t *declaration = expression->reference.declaration;
7450 if (is_var_declaration(declaration))
7456 * Parse a return statement.
7458 static statement_t *parse_return(void)
7460 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
7461 statement->base.source_position = token.source_position;
7465 expression_t *return_value = NULL;
7466 if(token.type != ';') {
7467 return_value = parse_expression();
7471 const type_t *const func_type = current_function->type;
7472 assert(is_type_function(func_type));
7473 type_t *const return_type = skip_typeref(func_type->function.return_type);
7475 if(return_value != NULL) {
7476 type_t *return_value_type = skip_typeref(return_value->base.type);
7478 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
7479 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
7480 warningf(&statement->base.source_position,
7481 "'return' with a value, in function returning void");
7482 return_value = NULL;
7484 type_t *const res_type = semantic_assign(return_type,
7485 return_value, "'return'", &statement->base.source_position);
7486 if (res_type == NULL) {
7487 errorf(&statement->base.source_position,
7488 "cannot return something of type '%T' in function returning '%T'",
7489 return_value->base.type, return_type);
7491 return_value = create_implicit_cast(return_value, res_type);
7494 /* check for returning address of a local var */
7495 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
7496 const expression_t *expression = return_value->unary.value;
7497 if (is_local_variable(expression)) {
7498 warningf(&statement->base.source_position,
7499 "function returns address of local variable");
7503 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
7504 warningf(&statement->base.source_position,
7505 "'return' without value, in function returning non-void");
7508 statement->returns.value = return_value;
7512 return create_invalid_statement();
7516 * Parse a declaration statement.
7518 static statement_t *parse_declaration_statement(void)
7520 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
7522 statement->base.source_position = token.source_position;
7524 declaration_t *before = last_declaration;
7525 parse_declaration(record_declaration);
7527 if(before == NULL) {
7528 statement->declaration.declarations_begin = scope->declarations;
7530 statement->declaration.declarations_begin = before->next;
7532 statement->declaration.declarations_end = last_declaration;
7538 * Parse an expression statement, ie. expr ';'.
7540 static statement_t *parse_expression_statement(void)
7542 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
7544 statement->base.source_position = token.source_position;
7545 expression_t *const expr = parse_expression();
7546 statement->expression.expression = expr;
7548 if (warning.unused_value && !expression_has_effect(expr)) {
7549 warningf(&expr->base.source_position, "statement has no effect");
7556 return create_invalid_statement();
7560 * Parse a statement.
7562 static statement_t *parse_statement(void)
7564 statement_t *statement = NULL;
7566 /* declaration or statement */
7567 add_anchor_token(';');
7568 switch(token.type) {
7570 statement = parse_asm_statement();
7574 statement = parse_case_statement();
7578 statement = parse_default_statement();
7582 statement = parse_compound_statement();
7586 statement = parse_if();
7590 statement = parse_switch();
7594 statement = parse_while();
7598 statement = parse_do();
7602 statement = parse_for();
7606 statement = parse_goto();
7610 statement = parse_continue();
7614 statement = parse_break();
7618 statement = parse_return();
7622 if(warning.empty_statement) {
7623 warningf(HERE, "statement is empty");
7625 statement = create_empty_statement();
7630 if(look_ahead(1)->type == ':') {
7631 statement = parse_label_statement();
7635 if(is_typedef_symbol(token.v.symbol)) {
7636 statement = parse_declaration_statement();
7640 statement = parse_expression_statement();
7643 case T___extension__:
7644 /* this can be a prefix to a declaration or an expression statement */
7645 /* we simply eat it now and parse the rest with tail recursion */
7648 } while(token.type == T___extension__);
7649 statement = parse_statement();
7653 statement = parse_declaration_statement();
7657 statement = parse_expression_statement();
7660 rem_anchor_token(';');
7662 assert(statement != NULL
7663 && statement->base.source_position.input_name != NULL);
7669 * Parse a compound statement.
7671 static statement_t *parse_compound_statement(void)
7673 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
7675 statement->base.source_position = token.source_position;
7678 add_anchor_token('}');
7680 int top = environment_top();
7681 scope_t *last_scope = scope;
7682 set_scope(&statement->compound.scope);
7684 statement_t *last_statement = NULL;
7686 while(token.type != '}' && token.type != T_EOF) {
7687 statement_t *sub_statement = parse_statement();
7688 if(is_invalid_statement(sub_statement)) {
7689 /* an error occurred. if we are at an anchor, return */
7695 if(last_statement != NULL) {
7696 last_statement->base.next = sub_statement;
7698 statement->compound.statements = sub_statement;
7701 while(sub_statement->base.next != NULL)
7702 sub_statement = sub_statement->base.next;
7704 last_statement = sub_statement;
7707 if(token.type == '}') {
7710 errorf(&statement->base.source_position,
7711 "end of file while looking for closing '}'");
7715 rem_anchor_token('}');
7716 assert(scope == &statement->compound.scope);
7717 set_scope(last_scope);
7718 environment_pop_to(top);
7724 * Initialize builtin types.
7726 static void initialize_builtin_types(void)
7728 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7729 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7730 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7731 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7732 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7733 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7734 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7735 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7737 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7738 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7739 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7740 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7744 * Check for unused global static functions and variables
7746 static void check_unused_globals(void)
7748 if (!warning.unused_function && !warning.unused_variable)
7751 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7752 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7755 type_t *const type = decl->type;
7757 if (is_type_function(skip_typeref(type))) {
7758 if (!warning.unused_function || decl->is_inline)
7761 s = (decl->init.statement != NULL ? "defined" : "declared");
7763 if (!warning.unused_variable)
7769 warningf(&decl->source_position, "'%#T' %s but not used",
7770 type, decl->symbol, s);
7775 * Parse a translation unit.
7777 static translation_unit_t *parse_translation_unit(void)
7779 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7781 assert(global_scope == NULL);
7782 global_scope = &unit->scope;
7784 assert(scope == NULL);
7785 set_scope(&unit->scope);
7787 initialize_builtin_types();
7789 while(token.type != T_EOF) {
7790 if (token.type == ';') {
7791 /* TODO error in strict mode */
7792 warningf(HERE, "stray ';' outside of function");
7795 parse_external_declaration();
7799 assert(scope == &unit->scope);
7801 last_declaration = NULL;
7803 assert(global_scope == &unit->scope);
7804 check_unused_globals();
7805 global_scope = NULL;
7813 * @return the translation unit or NULL if errors occurred.
7815 translation_unit_t *parse(void)
7817 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7818 label_stack = NEW_ARR_F(stack_entry_t, 0);
7819 diagnostic_count = 0;
7823 type_set_output(stderr);
7824 ast_set_output(stderr);
7826 lookahead_bufpos = 0;
7827 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7830 translation_unit_t *unit = parse_translation_unit();
7832 DEL_ARR_F(environment_stack);
7833 DEL_ARR_F(label_stack);
7839 * Initialize the parser.
7841 void init_parser(void)
7844 /* add predefined symbols for extended-decl-modifier */
7845 sym_align = symbol_table_insert("align");
7846 sym_allocate = symbol_table_insert("allocate");
7847 sym_dllimport = symbol_table_insert("dllimport");
7848 sym_dllexport = symbol_table_insert("dllexport");
7849 sym_naked = symbol_table_insert("naked");
7850 sym_noinline = symbol_table_insert("noinline");
7851 sym_noreturn = symbol_table_insert("noreturn");
7852 sym_nothrow = symbol_table_insert("nothrow");
7853 sym_novtable = symbol_table_insert("novtable");
7854 sym_property = symbol_table_insert("property");
7855 sym_get = symbol_table_insert("get");
7856 sym_put = symbol_table_insert("put");
7857 sym_selectany = symbol_table_insert("selectany");
7858 sym_thread = symbol_table_insert("thread");
7859 sym_uuid = symbol_table_insert("uuid");
7860 sym_deprecated = symbol_table_insert("deprecated");
7861 sym_restrict = symbol_table_insert("restrict");
7862 sym_noalias = symbol_table_insert("noalias");
7864 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7866 init_expression_parsers();
7867 obstack_init(&temp_obst);
7869 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7870 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7874 * Terminate the parser.
7876 void exit_parser(void)
7878 obstack_free(&temp_obst, NULL);