2 * This file is part of cparser.
3 * Copyright (C) 2007-2008 Matthias Braun <matze@braunis.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
27 #include "diagnostic.h"
28 #include "format_check.h"
34 #include "type_hash.h"
36 #include "lang_features.h"
38 #include "adt/bitfiddle.h"
39 #include "adt/error.h"
40 #include "adt/array.h"
42 //#define PRINT_TOKENS
43 #define MAX_LOOKAHEAD 2
46 declaration_t *old_declaration;
48 unsigned short namespc;
51 typedef struct declaration_specifiers_t declaration_specifiers_t;
52 struct declaration_specifiers_t {
53 source_position_t source_position;
54 unsigned char declared_storage_class;
55 unsigned char alignment; /**< Alignment, 0 if not set. */
57 decl_modifiers_t decl_modifiers; /**< MS __declspec extended modifier mask */
58 const char *deprecated_string; /**< can be set if declaration was marked deprecated. */
59 symbol_t *get_property_sym; /**< the name of the get property if set. */
60 symbol_t *put_property_sym; /**< the name of the put property if set. */
65 * An environment for parsing initializers (and compound literals).
67 typedef struct parse_initializer_env_t {
68 type_t *type; /**< the type of the initializer. In case of an
69 array type with unspecified size this gets
70 adjusted to the actual size. */
71 declaration_t *declaration; /**< the declaration that is initialized if any */
72 bool must_be_constant;
73 } parse_initializer_env_t;
75 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
78 static token_t lookahead_buffer[MAX_LOOKAHEAD];
79 static int lookahead_bufpos;
80 static stack_entry_t *environment_stack = NULL;
81 static stack_entry_t *label_stack = NULL;
82 static scope_t *global_scope = NULL;
83 static scope_t *scope = NULL;
84 static declaration_t *last_declaration = NULL;
85 static declaration_t *current_function = NULL;
86 static switch_statement_t *current_switch = NULL;
87 static statement_t *current_loop = NULL;
88 static goto_statement_t *goto_first = NULL;
89 static goto_statement_t *goto_last = NULL;
90 static label_statement_t *label_first = NULL;
91 static label_statement_t *label_last = NULL;
92 static struct obstack temp_obst;
94 /* symbols for Microsoft extended-decl-modifier */
95 static const symbol_t *sym_align = NULL;
96 static const symbol_t *sym_allocate = NULL;
97 static const symbol_t *sym_dllimport = NULL;
98 static const symbol_t *sym_dllexport = NULL;
99 static const symbol_t *sym_naked = NULL;
100 static const symbol_t *sym_noinline = NULL;
101 static const symbol_t *sym_noreturn = NULL;
102 static const symbol_t *sym_nothrow = NULL;
103 static const symbol_t *sym_novtable = NULL;
104 static const symbol_t *sym_property = NULL;
105 static const symbol_t *sym_get = NULL;
106 static const symbol_t *sym_put = NULL;
107 static const symbol_t *sym_selectany = NULL;
108 static const symbol_t *sym_thread = NULL;
109 static const symbol_t *sym_uuid = NULL;
110 static const symbol_t *sym_deprecated = NULL;
111 static const symbol_t *sym_restrict = NULL;
112 static const symbol_t *sym_noalias = NULL;
114 /** The token anchor set */
115 static unsigned char token_anchor_set[T_LAST_TOKEN];
117 /** The current source position. */
118 #define HERE token.source_position
120 static type_t *type_valist;
122 static statement_t *parse_compound_statement(void);
123 static statement_t *parse_statement(void);
125 static expression_t *parse_sub_expression(unsigned precedence);
126 static expression_t *parse_expression(void);
127 static type_t *parse_typename(void);
129 static void parse_compound_type_entries(declaration_t *compound_declaration);
130 static declaration_t *parse_declarator(
131 const declaration_specifiers_t *specifiers, bool may_be_abstract);
132 static declaration_t *record_declaration(declaration_t *declaration);
134 static void semantic_comparison(binary_expression_t *expression);
136 #define STORAGE_CLASSES \
143 #define TYPE_QUALIFIERS \
150 #ifdef PROVIDE_COMPLEX
151 #define COMPLEX_SPECIFIERS \
153 #define IMAGINARY_SPECIFIERS \
156 #define COMPLEX_SPECIFIERS
157 #define IMAGINARY_SPECIFIERS
160 #define TYPE_SPECIFIERS \
175 case T___builtin_va_list: \
180 #define DECLARATION_START \
185 #define TYPENAME_START \
190 * Allocate an AST node with given size and
191 * initialize all fields with zero.
193 static void *allocate_ast_zero(size_t size)
195 void *res = allocate_ast(size);
196 memset(res, 0, size);
200 static declaration_t *allocate_declaration_zero(void)
202 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
203 declaration->type = type_error_type;
204 declaration->alignment = 0;
209 * Returns the size of a statement node.
211 * @param kind the statement kind
213 static size_t get_statement_struct_size(statement_kind_t kind)
215 static const size_t sizes[] = {
216 [STATEMENT_INVALID] = sizeof(invalid_statement_t),
217 [STATEMENT_EMPTY] = sizeof(empty_statement_t),
218 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
219 [STATEMENT_RETURN] = sizeof(return_statement_t),
220 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
221 [STATEMENT_IF] = sizeof(if_statement_t),
222 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
223 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
224 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
225 [STATEMENT_BREAK] = sizeof(statement_base_t),
226 [STATEMENT_GOTO] = sizeof(goto_statement_t),
227 [STATEMENT_LABEL] = sizeof(label_statement_t),
228 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
229 [STATEMENT_WHILE] = sizeof(while_statement_t),
230 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
231 [STATEMENT_FOR] = sizeof(for_statement_t),
232 [STATEMENT_ASM] = sizeof(asm_statement_t)
234 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
235 assert(sizes[kind] != 0);
240 * Allocate a statement node of given kind and initialize all
243 static statement_t *allocate_statement_zero(statement_kind_t kind)
245 size_t size = get_statement_struct_size(kind);
246 statement_t *res = allocate_ast_zero(size);
248 res->base.kind = kind;
253 * Creates a new invalid statement.
255 static statement_t *create_invalid_statement(void)
257 statement_t *statement = allocate_statement_zero(STATEMENT_INVALID);
258 statement->base.source_position = token.source_position;
263 * Allocate a new empty statement.
265 static statement_t *create_empty_statement(void)
267 statement_t *statement = allocate_statement_zero(STATEMENT_EMPTY);
268 statement->base.source_position = token.source_position;
273 * Returns the size of an expression node.
275 * @param kind the expression kind
277 static size_t get_expression_struct_size(expression_kind_t kind)
279 static const size_t sizes[] = {
280 [EXPR_INVALID] = sizeof(expression_base_t),
281 [EXPR_REFERENCE] = sizeof(reference_expression_t),
282 [EXPR_CONST] = sizeof(const_expression_t),
283 [EXPR_CHARACTER_CONSTANT] = sizeof(const_expression_t),
284 [EXPR_WIDE_CHARACTER_CONSTANT] = sizeof(const_expression_t),
285 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
286 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
287 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
288 [EXPR_CALL] = sizeof(call_expression_t),
289 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
290 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
291 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
292 [EXPR_SELECT] = sizeof(select_expression_t),
293 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
294 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
295 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
296 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
297 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
298 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
299 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
300 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
301 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
302 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
303 [EXPR_VA_START] = sizeof(va_start_expression_t),
304 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
305 [EXPR_STATEMENT] = sizeof(statement_expression_t),
307 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
308 return sizes[EXPR_UNARY_FIRST];
310 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
311 return sizes[EXPR_BINARY_FIRST];
313 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
314 assert(sizes[kind] != 0);
319 * Allocate an expression node of given kind and initialize all
322 static expression_t *allocate_expression_zero(expression_kind_t kind)
324 size_t size = get_expression_struct_size(kind);
325 expression_t *res = allocate_ast_zero(size);
327 res->base.kind = kind;
328 res->base.type = type_error_type;
333 * Returns the size of a type node.
335 * @param kind the type kind
337 static size_t get_type_struct_size(type_kind_t kind)
339 static const size_t sizes[] = {
340 [TYPE_ATOMIC] = sizeof(atomic_type_t),
341 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
342 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
343 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
344 [TYPE_ENUM] = sizeof(enum_type_t),
345 [TYPE_FUNCTION] = sizeof(function_type_t),
346 [TYPE_POINTER] = sizeof(pointer_type_t),
347 [TYPE_ARRAY] = sizeof(array_type_t),
348 [TYPE_BUILTIN] = sizeof(builtin_type_t),
349 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
350 [TYPE_TYPEOF] = sizeof(typeof_type_t),
352 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
353 assert(kind <= TYPE_TYPEOF);
354 assert(sizes[kind] != 0);
359 * Allocate a type node of given kind and initialize all
362 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
364 size_t size = get_type_struct_size(kind);
365 type_t *res = obstack_alloc(type_obst, size);
366 memset(res, 0, size);
368 res->base.kind = kind;
369 res->base.source_position = source_position;
374 * Returns the size of an initializer node.
376 * @param kind the initializer kind
378 static size_t get_initializer_size(initializer_kind_t kind)
380 static const size_t sizes[] = {
381 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
382 [INITIALIZER_STRING] = sizeof(initializer_string_t),
383 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
384 [INITIALIZER_LIST] = sizeof(initializer_list_t),
385 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
387 assert(kind < sizeof(sizes) / sizeof(*sizes));
388 assert(sizes[kind] != 0);
393 * Allocate an initializer node of given kind and initialize all
396 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
398 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
405 * Free a type from the type obstack.
407 static void free_type(void *type)
409 obstack_free(type_obst, type);
413 * Returns the index of the top element of the environment stack.
415 static size_t environment_top(void)
417 return ARR_LEN(environment_stack);
421 * Returns the index of the top element of the label stack.
423 static size_t label_top(void)
425 return ARR_LEN(label_stack);
429 * Return the next token.
431 static inline void next_token(void)
433 token = lookahead_buffer[lookahead_bufpos];
434 lookahead_buffer[lookahead_bufpos] = lexer_token;
437 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
440 print_token(stderr, &token);
441 fprintf(stderr, "\n");
446 * Return the next token with a given lookahead.
448 static inline const token_t *look_ahead(int num)
450 assert(num > 0 && num <= MAX_LOOKAHEAD);
451 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
452 return &lookahead_buffer[pos];
456 * Adds a token to the token anchor set (a multi-set).
458 static void add_anchor_token(int token_type) {
459 assert(0 <= token_type && token_type < T_LAST_TOKEN);
460 ++token_anchor_set[token_type];
464 * Remove a token from the token anchor set (a multi-set).
466 static void rem_anchor_token(int token_type) {
467 assert(0 <= token_type && token_type < T_LAST_TOKEN);
468 --token_anchor_set[token_type];
471 static bool at_anchor(void) {
474 return token_anchor_set[token.type];
478 * Eat tokens until a matching token is found.
480 static void eat_until_matching_token(int type) {
481 unsigned parenthesis_count = 0;
482 unsigned brace_count = 0;
483 unsigned bracket_count = 0;
484 int end_token = type;
493 while(token.type != end_token ||
494 (parenthesis_count > 0 || brace_count > 0 || bracket_count > 0)) {
498 case '(': ++parenthesis_count; break;
499 case '{': ++brace_count; break;
500 case '[': ++bracket_count; break;
502 if(parenthesis_count > 0)
510 if(bracket_count > 0)
521 * Eat input tokens until an anchor is found.
523 static void eat_until_anchor(void) {
524 if(token.type == T_EOF)
526 while(token_anchor_set[token.type] == 0) {
527 if(token.type == '(' || token.type == '{' || token.type == '[')
528 eat_until_matching_token(token.type);
529 if(token.type == T_EOF)
535 static void eat_block(void) {
536 eat_until_matching_token('{');
537 if(token.type == '}')
542 * eat all token until a ';' is reached
543 * or a stop token is found.
545 static void eat_statement(void) {
546 eat_until_matching_token(';');
547 if(token.type == ';')
551 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
554 * Report a parse error because an expected token was not found.
556 static void parse_error_expected(const char *message, ...)
558 if(message != NULL) {
559 errorf(HERE, "%s", message);
562 va_start(ap, message);
563 errorf(HERE, "got %K, expected %#k", &token, &ap, "a ");
568 * Report a type error.
570 static void type_error(const char *msg, const source_position_t source_position,
573 errorf(source_position, "%s, but found type '%T'", msg, type);
577 * Report an incompatible type.
579 static void type_error_incompatible(const char *msg,
580 const source_position_t source_position, type_t *type1, type_t *type2)
582 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
586 * Expect the the current token is the expected token.
587 * If not, generate an error, eat the current statement,
588 * and goto the end_error label.
590 #define expect(expected) \
592 if(UNLIKELY(token.type != (expected))) { \
593 parse_error_expected(NULL, (expected), 0); \
594 add_anchor_token(expected); \
595 eat_until_anchor(); \
596 rem_anchor_token(expected); \
602 static void set_scope(scope_t *new_scope)
605 scope->last_declaration = last_declaration;
609 last_declaration = new_scope->last_declaration;
613 * Search a symbol in a given namespace and returns its declaration or
614 * NULL if this symbol was not found.
616 static declaration_t *get_declaration(const symbol_t *const symbol,
617 const namespace_t namespc)
619 declaration_t *declaration = symbol->declaration;
620 for( ; declaration != NULL; declaration = declaration->symbol_next) {
621 if(declaration->namespc == namespc)
629 * pushs an environment_entry on the environment stack and links the
630 * corresponding symbol to the new entry
632 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
634 symbol_t *symbol = declaration->symbol;
635 namespace_t namespc = (namespace_t) declaration->namespc;
637 /* replace/add declaration into declaration list of the symbol */
638 declaration_t *iter = symbol->declaration;
640 symbol->declaration = declaration;
642 declaration_t *iter_last = NULL;
643 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
644 /* replace an entry? */
645 if(iter->namespc == namespc) {
646 if(iter_last == NULL) {
647 symbol->declaration = declaration;
649 iter_last->symbol_next = declaration;
651 declaration->symbol_next = iter->symbol_next;
656 assert(iter_last->symbol_next == NULL);
657 iter_last->symbol_next = declaration;
661 /* remember old declaration */
663 entry.symbol = symbol;
664 entry.old_declaration = iter;
665 entry.namespc = (unsigned short) namespc;
666 ARR_APP1(stack_entry_t, *stack_ptr, entry);
669 static void environment_push(declaration_t *declaration)
671 assert(declaration->source_position.input_name != NULL);
672 assert(declaration->parent_scope != NULL);
673 stack_push(&environment_stack, declaration);
676 static void label_push(declaration_t *declaration)
678 declaration->parent_scope = ¤t_function->scope;
679 stack_push(&label_stack, declaration);
683 * pops symbols from the environment stack until @p new_top is the top element
685 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
687 stack_entry_t *stack = *stack_ptr;
688 size_t top = ARR_LEN(stack);
691 assert(new_top <= top);
695 for(i = top; i > new_top; --i) {
696 stack_entry_t *entry = &stack[i - 1];
698 declaration_t *old_declaration = entry->old_declaration;
699 symbol_t *symbol = entry->symbol;
700 namespace_t namespc = (namespace_t)entry->namespc;
702 /* replace/remove declaration */
703 declaration_t *declaration = symbol->declaration;
704 assert(declaration != NULL);
705 if(declaration->namespc == namespc) {
706 if(old_declaration == NULL) {
707 symbol->declaration = declaration->symbol_next;
709 symbol->declaration = old_declaration;
712 declaration_t *iter_last = declaration;
713 declaration_t *iter = declaration->symbol_next;
714 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
715 /* replace an entry? */
716 if(iter->namespc == namespc) {
717 assert(iter_last != NULL);
718 iter_last->symbol_next = old_declaration;
719 if(old_declaration != NULL) {
720 old_declaration->symbol_next = iter->symbol_next;
725 assert(iter != NULL);
729 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
732 static void environment_pop_to(size_t new_top)
734 stack_pop_to(&environment_stack, new_top);
737 static void label_pop_to(size_t new_top)
739 stack_pop_to(&label_stack, new_top);
743 static int get_rank(const type_t *type)
745 assert(!is_typeref(type));
746 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
747 * and esp. footnote 108). However we can't fold constants (yet), so we
748 * can't decide whether unsigned int is possible, while int always works.
749 * (unsigned int would be preferable when possible... for stuff like
750 * struct { enum { ... } bla : 4; } ) */
751 if(type->kind == TYPE_ENUM)
752 return ATOMIC_TYPE_INT;
754 assert(type->kind == TYPE_ATOMIC);
755 return type->atomic.akind;
758 static type_t *promote_integer(type_t *type)
760 if(type->kind == TYPE_BITFIELD)
761 type = type->bitfield.base;
763 if(get_rank(type) < ATOMIC_TYPE_INT)
770 * Create a cast expression.
772 * @param expression the expression to cast
773 * @param dest_type the destination type
775 static expression_t *create_cast_expression(expression_t *expression,
778 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
780 cast->unary.value = expression;
781 cast->base.type = dest_type;
787 * Check if a given expression represents the 0 pointer constant.
789 static bool is_null_pointer_constant(const expression_t *expression)
791 /* skip void* cast */
792 if(expression->kind == EXPR_UNARY_CAST
793 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
794 expression = expression->unary.value;
797 /* TODO: not correct yet, should be any constant integer expression
798 * which evaluates to 0 */
799 if (expression->kind != EXPR_CONST)
802 type_t *const type = skip_typeref(expression->base.type);
803 if (!is_type_integer(type))
806 return expression->conste.v.int_value == 0;
810 * Create an implicit cast expression.
812 * @param expression the expression to cast
813 * @param dest_type the destination type
815 static expression_t *create_implicit_cast(expression_t *expression,
818 type_t *const source_type = expression->base.type;
820 if (source_type == dest_type)
823 return create_cast_expression(expression, dest_type);
826 /** Implements the rules from § 6.5.16.1 */
827 static type_t *semantic_assign(type_t *orig_type_left,
828 const expression_t *const right,
831 type_t *const orig_type_right = right->base.type;
832 type_t *const type_left = skip_typeref(orig_type_left);
833 type_t *const type_right = skip_typeref(orig_type_right);
835 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
836 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
837 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
838 && is_type_pointer(type_right))) {
839 return orig_type_left;
842 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
843 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
844 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
846 /* the left type has all qualifiers from the right type */
847 unsigned missing_qualifiers
848 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
849 if(missing_qualifiers != 0) {
850 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
851 return orig_type_left;
854 points_to_left = get_unqualified_type(points_to_left);
855 points_to_right = get_unqualified_type(points_to_right);
857 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
858 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
859 return orig_type_left;
862 if (!types_compatible(points_to_left, points_to_right)) {
863 warningf(right->base.source_position,
864 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
865 orig_type_left, context, right, orig_type_right);
868 return orig_type_left;
871 if ((is_type_compound(type_left) && is_type_compound(type_right))
872 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
873 type_t *const unqual_type_left = get_unqualified_type(type_left);
874 type_t *const unqual_type_right = get_unqualified_type(type_right);
875 if (types_compatible(unqual_type_left, unqual_type_right)) {
876 return orig_type_left;
880 if (!is_type_valid(type_left))
883 if (!is_type_valid(type_right))
884 return orig_type_right;
889 static expression_t *parse_constant_expression(void)
891 /* start parsing at precedence 7 (conditional expression) */
892 expression_t *result = parse_sub_expression(7);
894 if(!is_constant_expression(result)) {
895 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
901 static expression_t *parse_assignment_expression(void)
903 /* start parsing at precedence 2 (assignment expression) */
904 return parse_sub_expression(2);
907 static type_t *make_global_typedef(const char *name, type_t *type)
909 symbol_t *const symbol = symbol_table_insert(name);
911 declaration_t *const declaration = allocate_declaration_zero();
912 declaration->namespc = NAMESPACE_NORMAL;
913 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
914 declaration->declared_storage_class = STORAGE_CLASS_TYPEDEF;
915 declaration->type = type;
916 declaration->symbol = symbol;
917 declaration->source_position = builtin_source_position;
919 record_declaration(declaration);
921 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
922 typedef_type->typedeft.declaration = declaration;
927 static string_t parse_string_literals(void)
929 assert(token.type == T_STRING_LITERAL);
930 string_t result = token.v.string;
934 while (token.type == T_STRING_LITERAL) {
935 result = concat_strings(&result, &token.v.string);
942 static void parse_attributes(void)
946 case T___attribute__: {
954 errorf(HERE, "EOF while parsing attribute");
973 if(token.type != T_STRING_LITERAL) {
974 parse_error_expected("while parsing assembler attribute",
976 eat_until_matching_token('(');
979 parse_string_literals();
984 goto attributes_finished;
993 static designator_t *parse_designation(void)
995 designator_t *result = NULL;
996 designator_t *last = NULL;
999 designator_t *designator;
1000 switch(token.type) {
1002 designator = allocate_ast_zero(sizeof(designator[0]));
1003 designator->source_position = token.source_position;
1005 add_anchor_token(']');
1006 designator->array_index = parse_constant_expression();
1007 rem_anchor_token(']');
1011 designator = allocate_ast_zero(sizeof(designator[0]));
1012 designator->source_position = token.source_position;
1014 if(token.type != T_IDENTIFIER) {
1015 parse_error_expected("while parsing designator",
1019 designator->symbol = token.v.symbol;
1027 assert(designator != NULL);
1029 last->next = designator;
1031 result = designator;
1039 static initializer_t *initializer_from_string(array_type_t *type,
1040 const string_t *const string)
1042 /* TODO: check len vs. size of array type */
1045 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
1046 initializer->string.string = *string;
1051 static initializer_t *initializer_from_wide_string(array_type_t *const type,
1052 wide_string_t *const string)
1054 /* TODO: check len vs. size of array type */
1057 initializer_t *const initializer =
1058 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
1059 initializer->wide_string.string = *string;
1065 * Build an initializer from a given expression.
1067 static initializer_t *initializer_from_expression(type_t *orig_type,
1068 expression_t *expression)
1070 /* TODO check that expression is a constant expression */
1072 /* § 6.7.8.14/15 char array may be initialized by string literals */
1073 type_t *type = skip_typeref(orig_type);
1074 type_t *expr_type_orig = expression->base.type;
1075 type_t *expr_type = skip_typeref(expr_type_orig);
1076 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
1077 array_type_t *const array_type = &type->array;
1078 type_t *const element_type = skip_typeref(array_type->element_type);
1080 if (element_type->kind == TYPE_ATOMIC) {
1081 atomic_type_kind_t akind = element_type->atomic.akind;
1082 switch (expression->kind) {
1083 case EXPR_STRING_LITERAL:
1084 if (akind == ATOMIC_TYPE_CHAR
1085 || akind == ATOMIC_TYPE_SCHAR
1086 || akind == ATOMIC_TYPE_UCHAR) {
1087 return initializer_from_string(array_type,
1088 &expression->string.value);
1091 case EXPR_WIDE_STRING_LITERAL: {
1092 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
1093 if (get_unqualified_type(element_type) == bare_wchar_type) {
1094 return initializer_from_wide_string(array_type,
1095 &expression->wide_string.value);
1105 type_t *const res_type = semantic_assign(type, expression, "initializer");
1106 if (res_type == NULL)
1109 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1110 result->value.value = create_implicit_cast(expression, res_type);
1116 * Checks if a given expression can be used as an constant initializer.
1118 static bool is_initializer_constant(const expression_t *expression)
1120 return is_constant_expression(expression)
1121 || is_address_constant(expression);
1125 * Parses an scalar initializer.
1127 * § 6.7.8.11; eat {} without warning
1129 static initializer_t *parse_scalar_initializer(type_t *type,
1130 bool must_be_constant)
1132 /* there might be extra {} hierarchies */
1134 while(token.type == '{') {
1137 warningf(HERE, "extra curly braces around scalar initializer");
1142 expression_t *expression = parse_assignment_expression();
1143 if(must_be_constant && !is_initializer_constant(expression)) {
1144 errorf(expression->base.source_position,
1145 "Initialisation expression '%E' is not constant\n",
1149 initializer_t *initializer = initializer_from_expression(type, expression);
1151 if(initializer == NULL) {
1152 errorf(expression->base.source_position,
1153 "expression '%E' doesn't match expected type '%T'",
1159 bool additional_warning_displayed = false;
1161 if(token.type == ',') {
1164 if(token.type != '}') {
1165 if(!additional_warning_displayed) {
1166 warningf(HERE, "additional elements in scalar initializer");
1167 additional_warning_displayed = true;
1178 * An entry in the type path.
1180 typedef struct type_path_entry_t type_path_entry_t;
1181 struct type_path_entry_t {
1182 type_t *type; /**< the upper top type. restored to path->top_tye if this entry is popped. */
1184 size_t index; /**< For array types: the current index. */
1185 declaration_t *compound_entry; /**< For compound types: the current declaration. */
1190 * A type path expression a position inside compound or array types.
1192 typedef struct type_path_t type_path_t;
1193 struct type_path_t {
1194 type_path_entry_t *path; /**< An flexible array containing the current path. */
1195 type_t *top_type; /**< type of the element the path points */
1196 size_t max_index; /**< largest index in outermost array */
1200 * Prints a type path for debugging.
1202 static __attribute__((unused)) void debug_print_type_path(
1203 const type_path_t *path)
1205 size_t len = ARR_LEN(path->path);
1207 for(size_t i = 0; i < len; ++i) {
1208 const type_path_entry_t *entry = & path->path[i];
1210 type_t *type = skip_typeref(entry->type);
1211 if(is_type_compound(type)) {
1212 /* in gcc mode structs can have no members */
1213 if(entry->v.compound_entry == NULL) {
1217 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1218 } else if(is_type_array(type)) {
1219 fprintf(stderr, "[%u]", entry->v.index);
1221 fprintf(stderr, "-INVALID-");
1224 if(path->top_type != NULL) {
1225 fprintf(stderr, " (");
1226 print_type(path->top_type);
1227 fprintf(stderr, ")");
1232 * Return the top type path entry, ie. in a path
1233 * (type).a.b returns the b.
1235 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1237 size_t len = ARR_LEN(path->path);
1239 return &path->path[len-1];
1243 * Enlarge the type path by an (empty) element.
1245 static type_path_entry_t *append_to_type_path(type_path_t *path)
1247 size_t len = ARR_LEN(path->path);
1248 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1250 type_path_entry_t *result = & path->path[len];
1251 memset(result, 0, sizeof(result[0]));
1256 * Descending into a sub-type. Enter the scope of the current
1259 static void descend_into_subtype(type_path_t *path)
1261 type_t *orig_top_type = path->top_type;
1262 type_t *top_type = skip_typeref(orig_top_type);
1264 assert(is_type_compound(top_type) || is_type_array(top_type));
1266 type_path_entry_t *top = append_to_type_path(path);
1267 top->type = top_type;
1269 if(is_type_compound(top_type)) {
1270 declaration_t *declaration = top_type->compound.declaration;
1271 declaration_t *entry = declaration->scope.declarations;
1272 top->v.compound_entry = entry;
1275 path->top_type = entry->type;
1277 path->top_type = NULL;
1280 assert(is_type_array(top_type));
1283 path->top_type = top_type->array.element_type;
1288 * Pop an entry from the given type path, ie. returning from
1289 * (type).a.b to (type).a
1291 static void ascend_from_subtype(type_path_t *path)
1293 type_path_entry_t *top = get_type_path_top(path);
1295 path->top_type = top->type;
1297 size_t len = ARR_LEN(path->path);
1298 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1302 * Pop entries from the given type path until the given
1303 * path level is reached.
1305 static void ascend_to(type_path_t *path, size_t top_path_level)
1307 size_t len = ARR_LEN(path->path);
1309 while(len > top_path_level) {
1310 ascend_from_subtype(path);
1311 len = ARR_LEN(path->path);
1315 static bool walk_designator(type_path_t *path, const designator_t *designator,
1316 bool used_in_offsetof)
1318 for( ; designator != NULL; designator = designator->next) {
1319 type_path_entry_t *top = get_type_path_top(path);
1320 type_t *orig_type = top->type;
1322 type_t *type = skip_typeref(orig_type);
1324 if(designator->symbol != NULL) {
1325 symbol_t *symbol = designator->symbol;
1326 if(!is_type_compound(type)) {
1327 if(is_type_valid(type)) {
1328 errorf(designator->source_position,
1329 "'.%Y' designator used for non-compound type '%T'",
1335 declaration_t *declaration = type->compound.declaration;
1336 declaration_t *iter = declaration->scope.declarations;
1337 for( ; iter != NULL; iter = iter->next) {
1338 if(iter->symbol == symbol) {
1343 errorf(designator->source_position,
1344 "'%T' has no member named '%Y'", orig_type, symbol);
1347 if(used_in_offsetof) {
1348 type_t *real_type = skip_typeref(iter->type);
1349 if(real_type->kind == TYPE_BITFIELD) {
1350 errorf(designator->source_position,
1351 "offsetof designator '%Y' may not specify bitfield",
1357 top->type = orig_type;
1358 top->v.compound_entry = iter;
1359 orig_type = iter->type;
1361 expression_t *array_index = designator->array_index;
1362 assert(designator->array_index != NULL);
1364 if(!is_type_array(type)) {
1365 if(is_type_valid(type)) {
1366 errorf(designator->source_position,
1367 "[%E] designator used for non-array type '%T'",
1368 array_index, orig_type);
1372 if(!is_type_valid(array_index->base.type)) {
1376 long index = fold_constant(array_index);
1377 if(!used_in_offsetof) {
1379 errorf(designator->source_position,
1380 "array index [%E] must be positive", array_index);
1383 if(type->array.size_constant == true) {
1384 long array_size = type->array.size;
1385 if(index >= array_size) {
1386 errorf(designator->source_position,
1387 "designator [%E] (%d) exceeds array size %d",
1388 array_index, index, array_size);
1394 top->type = orig_type;
1395 top->v.index = (size_t) index;
1396 orig_type = type->array.element_type;
1398 path->top_type = orig_type;
1400 if(designator->next != NULL) {
1401 descend_into_subtype(path);
1410 static void advance_current_object(type_path_t *path, size_t top_path_level)
1412 type_path_entry_t *top = get_type_path_top(path);
1414 type_t *type = skip_typeref(top->type);
1415 if(is_type_union(type)) {
1416 /* in unions only the first element is initialized */
1417 top->v.compound_entry = NULL;
1418 } else if(is_type_struct(type)) {
1419 declaration_t *entry = top->v.compound_entry;
1421 entry = entry->next;
1422 top->v.compound_entry = entry;
1424 path->top_type = entry->type;
1428 assert(is_type_array(type));
1432 if(!type->array.size_constant || top->v.index < type->array.size) {
1437 /* we're past the last member of the current sub-aggregate, try if we
1438 * can ascend in the type hierarchy and continue with another subobject */
1439 size_t len = ARR_LEN(path->path);
1441 if(len > top_path_level) {
1442 ascend_from_subtype(path);
1443 advance_current_object(path, top_path_level);
1445 path->top_type = NULL;
1450 * skip until token is found.
1452 static void skip_until(int type) {
1453 while(token.type != type) {
1454 if(token.type == T_EOF)
1461 * skip any {...} blocks until a closing braket is reached.
1463 static void skip_initializers(void)
1465 if(token.type == '{')
1468 while(token.type != '}') {
1469 if(token.type == T_EOF)
1471 if(token.type == '{') {
1479 static initializer_t *create_empty_initializer(void)
1481 static initializer_t empty_initializer
1482 = { .list = { { INITIALIZER_LIST }, 0 } };
1483 return &empty_initializer;
1487 * Parse a part of an initialiser for a struct or union,
1489 static initializer_t *parse_sub_initializer(type_path_t *path,
1490 type_t *outer_type, size_t top_path_level,
1491 parse_initializer_env_t *env)
1493 if(token.type == '}') {
1494 /* empty initializer */
1495 return create_empty_initializer();
1498 type_t *orig_type = path->top_type;
1499 type_t *type = NULL;
1501 if (orig_type == NULL) {
1502 /* We are initializing an empty compound. */
1504 type = skip_typeref(orig_type);
1506 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1507 * initializers in this case. */
1508 if(!is_type_valid(type)) {
1509 skip_initializers();
1510 return create_empty_initializer();
1514 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1517 designator_t *designator = NULL;
1518 if(token.type == '.' || token.type == '[') {
1519 designator = parse_designation();
1521 /* reset path to toplevel, evaluate designator from there */
1522 ascend_to(path, top_path_level);
1523 if(!walk_designator(path, designator, false)) {
1524 /* can't continue after designation error */
1528 initializer_t *designator_initializer
1529 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1530 designator_initializer->designator.designator = designator;
1531 ARR_APP1(initializer_t*, initializers, designator_initializer);
1536 if(token.type == '{') {
1537 if(type != NULL && is_type_scalar(type)) {
1538 sub = parse_scalar_initializer(type, env->must_be_constant);
1542 if (env->declaration != NULL)
1543 errorf(HERE, "extra brace group at end of initializer for '%Y'",
1544 env->declaration->symbol);
1546 errorf(HERE, "extra brace group at end of initializer");
1548 descend_into_subtype(path);
1550 add_anchor_token('}');
1551 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1553 rem_anchor_token('}');
1556 ascend_from_subtype(path);
1560 goto error_parse_next;
1564 /* must be an expression */
1565 expression_t *expression = parse_assignment_expression();
1567 if(env->must_be_constant && !is_initializer_constant(expression)) {
1568 errorf(expression->base.source_position,
1569 "Initialisation expression '%E' is not constant\n",
1574 /* we are already outside, ... */
1578 /* handle { "string" } special case */
1579 if((expression->kind == EXPR_STRING_LITERAL
1580 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1581 && outer_type != NULL) {
1582 sub = initializer_from_expression(outer_type, expression);
1584 if(token.type == ',') {
1587 if(token.type != '}') {
1588 warningf(HERE, "excessive elements in initializer for type '%T'",
1591 /* TODO: eat , ... */
1596 /* descend into subtypes until expression matches type */
1598 orig_type = path->top_type;
1599 type = skip_typeref(orig_type);
1601 sub = initializer_from_expression(orig_type, expression);
1605 if(!is_type_valid(type)) {
1608 if(is_type_scalar(type)) {
1609 errorf(expression->base.source_position,
1610 "expression '%E' doesn't match expected type '%T'",
1611 expression, orig_type);
1615 descend_into_subtype(path);
1619 /* update largest index of top array */
1620 const type_path_entry_t *first = &path->path[0];
1621 type_t *first_type = first->type;
1622 first_type = skip_typeref(first_type);
1623 if(is_type_array(first_type)) {
1624 size_t index = first->v.index;
1625 if(index > path->max_index)
1626 path->max_index = index;
1630 /* append to initializers list */
1631 ARR_APP1(initializer_t*, initializers, sub);
1634 if(env->declaration != NULL)
1635 warningf(HERE, "excess elements in struct initializer for '%Y'",
1636 env->declaration->symbol);
1638 warningf(HERE, "excess elements in struct initializer");
1642 if(token.type == '}') {
1646 if(token.type == '}') {
1651 /* advance to the next declaration if we are not at the end */
1652 advance_current_object(path, top_path_level);
1653 orig_type = path->top_type;
1654 if(orig_type != NULL)
1655 type = skip_typeref(orig_type);
1661 size_t len = ARR_LEN(initializers);
1662 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1663 initializer_t *result = allocate_ast_zero(size);
1664 result->kind = INITIALIZER_LIST;
1665 result->list.len = len;
1666 memcpy(&result->list.initializers, initializers,
1667 len * sizeof(initializers[0]));
1669 DEL_ARR_F(initializers);
1670 ascend_to(path, top_path_level);
1675 skip_initializers();
1676 DEL_ARR_F(initializers);
1677 ascend_to(path, top_path_level);
1682 * Parses an initializer. Parsers either a compound literal
1683 * (env->declaration == NULL) or an initializer of a declaration.
1685 static initializer_t *parse_initializer(parse_initializer_env_t *env)
1687 type_t *type = skip_typeref(env->type);
1688 initializer_t *result = NULL;
1691 if(is_type_scalar(type)) {
1692 result = parse_scalar_initializer(type, env->must_be_constant);
1693 } else if(token.type == '{') {
1697 memset(&path, 0, sizeof(path));
1698 path.top_type = env->type;
1699 path.path = NEW_ARR_F(type_path_entry_t, 0);
1701 descend_into_subtype(&path);
1703 add_anchor_token('}');
1704 result = parse_sub_initializer(&path, env->type, 1, env);
1705 rem_anchor_token('}');
1707 max_index = path.max_index;
1708 DEL_ARR_F(path.path);
1712 /* parse_scalar_initializer() also works in this case: we simply
1713 * have an expression without {} around it */
1714 result = parse_scalar_initializer(type, env->must_be_constant);
1717 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1718 * the array type size */
1719 if(is_type_array(type) && type->array.size_expression == NULL
1720 && result != NULL) {
1722 switch (result->kind) {
1723 case INITIALIZER_LIST:
1724 size = max_index + 1;
1727 case INITIALIZER_STRING:
1728 size = result->string.string.size;
1731 case INITIALIZER_WIDE_STRING:
1732 size = result->wide_string.string.size;
1736 internal_errorf(HERE, "invalid initializer type");
1739 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1740 cnst->base.type = type_size_t;
1741 cnst->conste.v.int_value = size;
1743 type_t *new_type = duplicate_type(type);
1745 new_type->array.size_expression = cnst;
1746 new_type->array.size_constant = true;
1747 new_type->array.size = size;
1748 env->type = new_type;
1756 static declaration_t *append_declaration(declaration_t *declaration);
1758 static declaration_t *parse_compound_type_specifier(bool is_struct)
1766 symbol_t *symbol = NULL;
1767 declaration_t *declaration = NULL;
1769 if (token.type == T___attribute__) {
1774 if(token.type == T_IDENTIFIER) {
1775 symbol = token.v.symbol;
1779 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1781 declaration = get_declaration(symbol, NAMESPACE_UNION);
1783 } else if(token.type != '{') {
1785 parse_error_expected("while parsing struct type specifier",
1786 T_IDENTIFIER, '{', 0);
1788 parse_error_expected("while parsing union type specifier",
1789 T_IDENTIFIER, '{', 0);
1795 if(declaration == NULL) {
1796 declaration = allocate_declaration_zero();
1797 declaration->namespc =
1798 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1799 declaration->source_position = token.source_position;
1800 declaration->symbol = symbol;
1801 declaration->parent_scope = scope;
1802 if (symbol != NULL) {
1803 environment_push(declaration);
1805 append_declaration(declaration);
1808 if(token.type == '{') {
1809 if(declaration->init.is_defined) {
1810 assert(symbol != NULL);
1811 errorf(HERE, "multiple definitions of '%s %Y'",
1812 is_struct ? "struct" : "union", symbol);
1813 declaration->scope.declarations = NULL;
1815 declaration->init.is_defined = true;
1817 parse_compound_type_entries(declaration);
1824 static void parse_enum_entries(type_t *const enum_type)
1828 if(token.type == '}') {
1830 errorf(HERE, "empty enum not allowed");
1834 add_anchor_token('}');
1836 if(token.type != T_IDENTIFIER) {
1837 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1839 rem_anchor_token('}');
1843 declaration_t *const entry = allocate_declaration_zero();
1844 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1845 entry->type = enum_type;
1846 entry->symbol = token.v.symbol;
1847 entry->source_position = token.source_position;
1850 if(token.type == '=') {
1852 expression_t *value = parse_constant_expression();
1854 value = create_implicit_cast(value, enum_type);
1855 entry->init.enum_value = value;
1860 record_declaration(entry);
1862 if(token.type != ',')
1865 } while(token.type != '}');
1866 rem_anchor_token('}');
1874 static type_t *parse_enum_specifier(void)
1878 declaration_t *declaration;
1881 if(token.type == T_IDENTIFIER) {
1882 symbol = token.v.symbol;
1885 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1886 } else if(token.type != '{') {
1887 parse_error_expected("while parsing enum type specifier",
1888 T_IDENTIFIER, '{', 0);
1895 if(declaration == NULL) {
1896 declaration = allocate_declaration_zero();
1897 declaration->namespc = NAMESPACE_ENUM;
1898 declaration->source_position = token.source_position;
1899 declaration->symbol = symbol;
1900 declaration->parent_scope = scope;
1903 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1904 type->enumt.declaration = declaration;
1906 if(token.type == '{') {
1907 if(declaration->init.is_defined) {
1908 errorf(HERE, "multiple definitions of enum %Y", symbol);
1910 if (symbol != NULL) {
1911 environment_push(declaration);
1913 append_declaration(declaration);
1914 declaration->init.is_defined = 1;
1916 parse_enum_entries(type);
1924 * if a symbol is a typedef to another type, return true
1926 static bool is_typedef_symbol(symbol_t *symbol)
1928 const declaration_t *const declaration =
1929 get_declaration(symbol, NAMESPACE_NORMAL);
1931 declaration != NULL &&
1932 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1935 static type_t *parse_typeof(void)
1942 add_anchor_token(')');
1944 expression_t *expression = NULL;
1947 switch(token.type) {
1948 case T___extension__:
1949 /* this can be a prefix to a typename or an expression */
1950 /* we simply eat it now. */
1953 } while(token.type == T___extension__);
1957 if(is_typedef_symbol(token.v.symbol)) {
1958 type = parse_typename();
1960 expression = parse_expression();
1961 type = expression->base.type;
1966 type = parse_typename();
1970 expression = parse_expression();
1971 type = expression->base.type;
1975 rem_anchor_token(')');
1978 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1979 typeof_type->typeoft.expression = expression;
1980 typeof_type->typeoft.typeof_type = type;
1988 SPECIFIER_SIGNED = 1 << 0,
1989 SPECIFIER_UNSIGNED = 1 << 1,
1990 SPECIFIER_LONG = 1 << 2,
1991 SPECIFIER_INT = 1 << 3,
1992 SPECIFIER_DOUBLE = 1 << 4,
1993 SPECIFIER_CHAR = 1 << 5,
1994 SPECIFIER_SHORT = 1 << 6,
1995 SPECIFIER_LONG_LONG = 1 << 7,
1996 SPECIFIER_FLOAT = 1 << 8,
1997 SPECIFIER_BOOL = 1 << 9,
1998 SPECIFIER_VOID = 1 << 10,
1999 SPECIFIER_INT8 = 1 << 11,
2000 SPECIFIER_INT16 = 1 << 12,
2001 SPECIFIER_INT32 = 1 << 13,
2002 SPECIFIER_INT64 = 1 << 14,
2003 SPECIFIER_INT128 = 1 << 15,
2004 #ifdef PROVIDE_COMPLEX
2005 SPECIFIER_COMPLEX = 1 << 16,
2006 SPECIFIER_IMAGINARY = 1 << 17,
2010 static type_t *create_builtin_type(symbol_t *const symbol,
2011 type_t *const real_type)
2013 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
2014 type->builtin.symbol = symbol;
2015 type->builtin.real_type = real_type;
2017 type_t *result = typehash_insert(type);
2018 if (type != result) {
2025 static type_t *get_typedef_type(symbol_t *symbol)
2027 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
2028 if(declaration == NULL
2029 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
2032 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
2033 type->typedeft.declaration = declaration;
2039 * check for the allowed MS alignment values.
2041 static bool check_elignment_value(long long intvalue) {
2042 if(intvalue < 1 || intvalue > 8192) {
2043 errorf(HERE, "illegal alignment value");
2046 unsigned v = (unsigned)intvalue;
2047 for(unsigned i = 1; i <= 8192; i += i) {
2051 errorf(HERE, "alignment must be power of two");
2055 #define DET_MOD(name, tag) do { \
2056 if(*modifiers & tag) warningf(HERE, #name " used more than once"); \
2057 *modifiers |= tag; \
2060 static void parse_microsoft_extended_decl_modifier(declaration_specifiers_t *specifiers)
2062 decl_modifiers_t *modifiers = &specifiers->decl_modifiers;
2065 if(token.type == T_restrict) {
2067 DET_MOD(restrict, DM_RESTRICT);
2069 } else if(token.type != T_IDENTIFIER)
2071 symbol_t *symbol = token.v.symbol;
2072 if(symbol == sym_align) {
2075 if(token.type != T_INTEGER)
2077 if(check_elignment_value(token.v.intvalue)) {
2078 if(specifiers->alignment != 0)
2079 warningf(HERE, "align used more than once");
2080 specifiers->alignment = (unsigned char)token.v.intvalue;
2084 } else if(symbol == sym_allocate) {
2087 if(token.type != T_IDENTIFIER)
2089 (void)token.v.symbol;
2091 } else if(symbol == sym_dllimport) {
2093 DET_MOD(dllimport, DM_DLLIMPORT);
2094 } else if(symbol == sym_dllexport) {
2096 DET_MOD(dllexport, DM_DLLEXPORT);
2097 } else if(symbol == sym_thread) {
2099 DET_MOD(thread, DM_THREAD);
2100 } else if(symbol == sym_naked) {
2102 DET_MOD(naked, DM_NAKED);
2103 } else if(symbol == sym_noinline) {
2105 DET_MOD(noinline, DM_NOINLINE);
2106 } else if(symbol == sym_noreturn) {
2108 DET_MOD(noreturn, DM_NORETURN);
2109 } else if(symbol == sym_nothrow) {
2111 DET_MOD(nothrow, DM_NOTHROW);
2112 } else if(symbol == sym_novtable) {
2114 DET_MOD(novtable, DM_NOVTABLE);
2115 } else if(symbol == sym_property) {
2119 bool is_get = false;
2120 if(token.type != T_IDENTIFIER)
2122 if(token.v.symbol == sym_get) {
2124 } else if(token.v.symbol == sym_put) {
2126 errorf(HERE, "Bad property name '%Y'", token.v.symbol);
2131 if(token.type != T_IDENTIFIER)
2134 if(specifiers->get_property_sym != NULL) {
2135 errorf(HERE, "get property name already specified");
2137 specifiers->get_property_sym = token.v.symbol;
2140 if(specifiers->put_property_sym != NULL) {
2141 errorf(HERE, "put property name already specified");
2143 specifiers->put_property_sym = token.v.symbol;
2147 if(token.type == ',') {
2154 } else if(symbol == sym_selectany) {
2156 DET_MOD(selectany, DM_SELECTANY);
2157 } else if(symbol == sym_uuid) {
2160 if(token.type != T_STRING_LITERAL)
2164 } else if(symbol == sym_deprecated) {
2166 DET_MOD(deprecated, DM_DEPRECATED);
2167 if(token.type == '(') {
2169 if(token.type == T_STRING_LITERAL) {
2170 specifiers->deprecated_string = token.v.string.begin;
2173 errorf(HERE, "string literal expected");
2177 } else if(symbol == sym_noalias) {
2179 DET_MOD(noalias, DM_NOALIAS);
2181 warningf(HERE, "Unknown modifier %Y ignored", token.v.symbol);
2183 if(token.type == '(')
2187 if (token.type == ',')
2194 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
2196 type_t *type = NULL;
2197 unsigned type_qualifiers = 0;
2198 unsigned type_specifiers = 0;
2201 specifiers->source_position = token.source_position;
2204 switch(token.type) {
2207 #define MATCH_STORAGE_CLASS(token, class) \
2209 if(specifiers->declared_storage_class != STORAGE_CLASS_NONE) { \
2210 errorf(HERE, "multiple storage classes in declaration specifiers"); \
2212 specifiers->declared_storage_class = class; \
2216 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
2217 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
2218 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
2219 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
2220 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
2225 add_anchor_token(')');
2226 parse_microsoft_extended_decl_modifier(specifiers);
2227 rem_anchor_token(')');
2232 switch (specifiers->declared_storage_class) {
2233 case STORAGE_CLASS_NONE:
2234 specifiers->declared_storage_class = STORAGE_CLASS_THREAD;
2237 case STORAGE_CLASS_EXTERN:
2238 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_EXTERN;
2241 case STORAGE_CLASS_STATIC:
2242 specifiers->declared_storage_class = STORAGE_CLASS_THREAD_STATIC;
2246 errorf(HERE, "multiple storage classes in declaration specifiers");
2252 /* type qualifiers */
2253 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
2255 type_qualifiers |= qualifier; \
2259 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2260 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2261 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2263 case T___extension__:
2268 /* type specifiers */
2269 #define MATCH_SPECIFIER(token, specifier, name) \
2272 if(type_specifiers & specifier) { \
2273 errorf(HERE, "multiple " name " type specifiers given"); \
2275 type_specifiers |= specifier; \
2279 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
2280 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
2281 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
2282 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
2283 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
2284 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
2285 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
2286 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
2287 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
2288 MATCH_SPECIFIER(T__int8, SPECIFIER_INT8, "_int8")
2289 MATCH_SPECIFIER(T__int16, SPECIFIER_INT16, "_int16")
2290 MATCH_SPECIFIER(T__int32, SPECIFIER_INT32, "_int32")
2291 MATCH_SPECIFIER(T__int64, SPECIFIER_INT64, "_int64")
2292 MATCH_SPECIFIER(T__int128, SPECIFIER_INT128, "_int128")
2293 #ifdef PROVIDE_COMPLEX
2294 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
2295 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
2297 case T__forceinline:
2298 /* only in microsoft mode */
2299 specifiers->decl_modifiers |= DM_FORCEINLINE;
2303 specifiers->is_inline = true;
2308 if(type_specifiers & SPECIFIER_LONG_LONG) {
2309 errorf(HERE, "multiple type specifiers given");
2310 } else if(type_specifiers & SPECIFIER_LONG) {
2311 type_specifiers |= SPECIFIER_LONG_LONG;
2313 type_specifiers |= SPECIFIER_LONG;
2318 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
2320 type->compound.declaration = parse_compound_type_specifier(true);
2324 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
2326 type->compound.declaration = parse_compound_type_specifier(false);
2330 type = parse_enum_specifier();
2333 type = parse_typeof();
2335 case T___builtin_va_list:
2336 type = duplicate_type(type_valist);
2340 case T___attribute__:
2344 case T_IDENTIFIER: {
2345 /* only parse identifier if we haven't found a type yet */
2346 if(type != NULL || type_specifiers != 0)
2347 goto finish_specifiers;
2349 type_t *typedef_type = get_typedef_type(token.v.symbol);
2351 if(typedef_type == NULL)
2352 goto finish_specifiers;
2355 type = typedef_type;
2359 /* function specifier */
2361 goto finish_specifiers;
2368 atomic_type_kind_t atomic_type;
2370 /* match valid basic types */
2371 switch(type_specifiers) {
2372 case SPECIFIER_VOID:
2373 atomic_type = ATOMIC_TYPE_VOID;
2375 case SPECIFIER_CHAR:
2376 atomic_type = ATOMIC_TYPE_CHAR;
2378 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
2379 atomic_type = ATOMIC_TYPE_SCHAR;
2381 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
2382 atomic_type = ATOMIC_TYPE_UCHAR;
2384 case SPECIFIER_SHORT:
2385 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
2386 case SPECIFIER_SHORT | SPECIFIER_INT:
2387 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2388 atomic_type = ATOMIC_TYPE_SHORT;
2390 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
2391 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
2392 atomic_type = ATOMIC_TYPE_USHORT;
2395 case SPECIFIER_SIGNED:
2396 case SPECIFIER_SIGNED | SPECIFIER_INT:
2397 atomic_type = ATOMIC_TYPE_INT;
2399 case SPECIFIER_UNSIGNED:
2400 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2401 atomic_type = ATOMIC_TYPE_UINT;
2403 case SPECIFIER_LONG:
2404 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2405 case SPECIFIER_LONG | SPECIFIER_INT:
2406 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2407 atomic_type = ATOMIC_TYPE_LONG;
2409 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2410 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2411 atomic_type = ATOMIC_TYPE_ULONG;
2413 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2414 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2415 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2416 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2418 atomic_type = ATOMIC_TYPE_LONGLONG;
2420 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2421 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2423 atomic_type = ATOMIC_TYPE_ULONGLONG;
2426 case SPECIFIER_UNSIGNED | SPECIFIER_INT8:
2427 atomic_type = unsigned_int8_type_kind;
2430 case SPECIFIER_UNSIGNED | SPECIFIER_INT16:
2431 atomic_type = unsigned_int16_type_kind;
2434 case SPECIFIER_UNSIGNED | SPECIFIER_INT32:
2435 atomic_type = unsigned_int32_type_kind;
2438 case SPECIFIER_UNSIGNED | SPECIFIER_INT64:
2439 atomic_type = unsigned_int64_type_kind;
2442 case SPECIFIER_UNSIGNED | SPECIFIER_INT128:
2443 atomic_type = unsigned_int128_type_kind;
2446 case SPECIFIER_INT8:
2447 case SPECIFIER_SIGNED | SPECIFIER_INT8:
2448 atomic_type = int8_type_kind;
2451 case SPECIFIER_INT16:
2452 case SPECIFIER_SIGNED | SPECIFIER_INT16:
2453 atomic_type = int16_type_kind;
2456 case SPECIFIER_INT32:
2457 case SPECIFIER_SIGNED | SPECIFIER_INT32:
2458 atomic_type = int32_type_kind;
2461 case SPECIFIER_INT64:
2462 case SPECIFIER_SIGNED | SPECIFIER_INT64:
2463 atomic_type = int64_type_kind;
2466 case SPECIFIER_INT128:
2467 case SPECIFIER_SIGNED | SPECIFIER_INT128:
2468 atomic_type = int128_type_kind;
2471 case SPECIFIER_FLOAT:
2472 atomic_type = ATOMIC_TYPE_FLOAT;
2474 case SPECIFIER_DOUBLE:
2475 atomic_type = ATOMIC_TYPE_DOUBLE;
2477 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2478 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2480 case SPECIFIER_BOOL:
2481 atomic_type = ATOMIC_TYPE_BOOL;
2483 #ifdef PROVIDE_COMPLEX
2484 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2485 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2487 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2488 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2490 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2491 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2493 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2494 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2496 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2497 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2499 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2500 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2504 /* invalid specifier combination, give an error message */
2505 if(type_specifiers == 0) {
2506 if (! strict_mode) {
2507 if (warning.implicit_int) {
2508 warningf(HERE, "no type specifiers in declaration, using 'int'");
2510 atomic_type = ATOMIC_TYPE_INT;
2513 errorf(HERE, "no type specifiers given in declaration");
2515 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2516 (type_specifiers & SPECIFIER_UNSIGNED)) {
2517 errorf(HERE, "signed and unsigned specifiers gives");
2518 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2519 errorf(HERE, "only integer types can be signed or unsigned");
2521 errorf(HERE, "multiple datatypes in declaration");
2523 atomic_type = ATOMIC_TYPE_INVALID;
2526 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2527 type->atomic.akind = atomic_type;
2530 if(type_specifiers != 0) {
2531 errorf(HERE, "multiple datatypes in declaration");
2535 type->base.qualifiers = type_qualifiers;
2537 type_t *result = typehash_insert(type);
2538 if(newtype && result != type) {
2542 specifiers->type = result;
2547 static type_qualifiers_t parse_type_qualifiers(void)
2549 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2552 switch(token.type) {
2553 /* type qualifiers */
2554 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2555 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2556 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2559 return type_qualifiers;
2564 static declaration_t *parse_identifier_list(void)
2566 declaration_t *declarations = NULL;
2567 declaration_t *last_declaration = NULL;
2569 declaration_t *const declaration = allocate_declaration_zero();
2570 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2571 declaration->source_position = token.source_position;
2572 declaration->symbol = token.v.symbol;
2575 if(last_declaration != NULL) {
2576 last_declaration->next = declaration;
2578 declarations = declaration;
2580 last_declaration = declaration;
2582 if(token.type != ',')
2585 } while(token.type == T_IDENTIFIER);
2587 return declarations;
2590 static void semantic_parameter(declaration_t *declaration)
2592 /* TODO: improve error messages */
2594 if(declaration->declared_storage_class == STORAGE_CLASS_TYPEDEF) {
2595 errorf(HERE, "typedef not allowed in parameter list");
2596 } else if(declaration->declared_storage_class != STORAGE_CLASS_NONE
2597 && declaration->declared_storage_class != STORAGE_CLASS_REGISTER) {
2598 errorf(HERE, "parameter may only have none or register storage class");
2601 type_t *const orig_type = declaration->type;
2602 type_t * type = skip_typeref(orig_type);
2604 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2605 * into a pointer. § 6.7.5.3 (7) */
2606 if (is_type_array(type)) {
2607 type_t *const element_type = type->array.element_type;
2609 type = make_pointer_type(element_type, type->base.qualifiers);
2611 declaration->type = type;
2614 if(is_type_incomplete(type)) {
2615 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2616 orig_type, declaration->symbol);
2620 static declaration_t *parse_parameter(void)
2622 declaration_specifiers_t specifiers;
2623 memset(&specifiers, 0, sizeof(specifiers));
2625 parse_declaration_specifiers(&specifiers);
2627 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2629 semantic_parameter(declaration);
2634 static declaration_t *parse_parameters(function_type_t *type)
2636 if(token.type == T_IDENTIFIER) {
2637 symbol_t *symbol = token.v.symbol;
2638 if(!is_typedef_symbol(symbol)) {
2639 type->kr_style_parameters = true;
2640 return parse_identifier_list();
2644 if(token.type == ')') {
2645 type->unspecified_parameters = 1;
2648 if(token.type == T_void && look_ahead(1)->type == ')') {
2653 declaration_t *declarations = NULL;
2654 declaration_t *declaration;
2655 declaration_t *last_declaration = NULL;
2656 function_parameter_t *parameter;
2657 function_parameter_t *last_parameter = NULL;
2660 switch(token.type) {
2664 return declarations;
2667 case T___extension__:
2669 declaration = parse_parameter();
2671 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2672 memset(parameter, 0, sizeof(parameter[0]));
2673 parameter->type = declaration->type;
2675 if(last_parameter != NULL) {
2676 last_declaration->next = declaration;
2677 last_parameter->next = parameter;
2679 type->parameters = parameter;
2680 declarations = declaration;
2682 last_parameter = parameter;
2683 last_declaration = declaration;
2687 return declarations;
2689 if(token.type != ',')
2690 return declarations;
2700 } construct_type_kind_t;
2702 typedef struct construct_type_t construct_type_t;
2703 struct construct_type_t {
2704 construct_type_kind_t kind;
2705 construct_type_t *next;
2708 typedef struct parsed_pointer_t parsed_pointer_t;
2709 struct parsed_pointer_t {
2710 construct_type_t construct_type;
2711 type_qualifiers_t type_qualifiers;
2714 typedef struct construct_function_type_t construct_function_type_t;
2715 struct construct_function_type_t {
2716 construct_type_t construct_type;
2717 type_t *function_type;
2720 typedef struct parsed_array_t parsed_array_t;
2721 struct parsed_array_t {
2722 construct_type_t construct_type;
2723 type_qualifiers_t type_qualifiers;
2729 typedef struct construct_base_type_t construct_base_type_t;
2730 struct construct_base_type_t {
2731 construct_type_t construct_type;
2735 static construct_type_t *parse_pointer_declarator(void)
2739 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2740 memset(pointer, 0, sizeof(pointer[0]));
2741 pointer->construct_type.kind = CONSTRUCT_POINTER;
2742 pointer->type_qualifiers = parse_type_qualifiers();
2744 return (construct_type_t*) pointer;
2747 static construct_type_t *parse_array_declarator(void)
2750 add_anchor_token(']');
2752 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2753 memset(array, 0, sizeof(array[0]));
2754 array->construct_type.kind = CONSTRUCT_ARRAY;
2756 if(token.type == T_static) {
2757 array->is_static = true;
2761 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2762 if(type_qualifiers != 0) {
2763 if(token.type == T_static) {
2764 array->is_static = true;
2768 array->type_qualifiers = type_qualifiers;
2770 if(token.type == '*' && look_ahead(1)->type == ']') {
2771 array->is_variable = true;
2773 } else if(token.type != ']') {
2774 array->size = parse_assignment_expression();
2777 rem_anchor_token(']');
2780 return (construct_type_t*) array;
2785 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2788 add_anchor_token(')');
2791 if(declaration != NULL) {
2792 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2794 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2797 declaration_t *parameters = parse_parameters(&type->function);
2798 if(declaration != NULL) {
2799 declaration->scope.declarations = parameters;
2802 construct_function_type_t *construct_function_type =
2803 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2804 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2805 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2806 construct_function_type->function_type = type;
2808 rem_anchor_token(')');
2812 return (construct_type_t*) construct_function_type;
2815 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2816 bool may_be_abstract)
2818 /* construct a single linked list of construct_type_t's which describe
2819 * how to construct the final declarator type */
2820 construct_type_t *first = NULL;
2821 construct_type_t *last = NULL;
2824 while(token.type == '*') {
2825 construct_type_t *type = parse_pointer_declarator();
2836 /* TODO: find out if this is correct */
2839 construct_type_t *inner_types = NULL;
2841 switch(token.type) {
2843 if(declaration == NULL) {
2844 errorf(HERE, "no identifier expected in typename");
2846 declaration->symbol = token.v.symbol;
2847 declaration->source_position = token.source_position;
2853 add_anchor_token(')');
2854 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2855 rem_anchor_token(')');
2861 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2862 /* avoid a loop in the outermost scope, because eat_statement doesn't
2864 if(token.type == '}' && current_function == NULL) {
2872 construct_type_t *p = last;
2875 construct_type_t *type;
2876 switch(token.type) {
2878 type = parse_function_declarator(declaration);
2881 type = parse_array_declarator();
2884 goto declarator_finished;
2887 /* insert in the middle of the list (behind p) */
2889 type->next = p->next;
2900 declarator_finished:
2903 /* append inner_types at the end of the list, we don't to set last anymore
2904 * as it's not needed anymore */
2906 assert(first == NULL);
2907 first = inner_types;
2909 last->next = inner_types;
2917 static type_t *construct_declarator_type(construct_type_t *construct_list,
2920 construct_type_t *iter = construct_list;
2921 for( ; iter != NULL; iter = iter->next) {
2922 switch(iter->kind) {
2923 case CONSTRUCT_INVALID:
2924 internal_errorf(HERE, "invalid type construction found");
2925 case CONSTRUCT_FUNCTION: {
2926 construct_function_type_t *construct_function_type
2927 = (construct_function_type_t*) iter;
2929 type_t *function_type = construct_function_type->function_type;
2931 function_type->function.return_type = type;
2933 type_t *skipped_return_type = skip_typeref(type);
2934 if (is_type_function(skipped_return_type)) {
2935 errorf(HERE, "function returning function is not allowed");
2936 type = type_error_type;
2937 } else if (is_type_array(skipped_return_type)) {
2938 errorf(HERE, "function returning array is not allowed");
2939 type = type_error_type;
2941 type = function_type;
2946 case CONSTRUCT_POINTER: {
2947 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2948 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2949 pointer_type->pointer.points_to = type;
2950 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2952 type = pointer_type;
2956 case CONSTRUCT_ARRAY: {
2957 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2958 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2960 expression_t *size_expression = parsed_array->size;
2961 if(size_expression != NULL) {
2963 = create_implicit_cast(size_expression, type_size_t);
2966 array_type->base.qualifiers = parsed_array->type_qualifiers;
2967 array_type->array.element_type = type;
2968 array_type->array.is_static = parsed_array->is_static;
2969 array_type->array.is_variable = parsed_array->is_variable;
2970 array_type->array.size_expression = size_expression;
2972 if(size_expression != NULL) {
2973 if(is_constant_expression(size_expression)) {
2974 array_type->array.size_constant = true;
2975 array_type->array.size
2976 = fold_constant(size_expression);
2978 array_type->array.is_vla = true;
2982 type_t *skipped_type = skip_typeref(type);
2983 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2984 errorf(HERE, "array of void is not allowed");
2985 type = type_error_type;
2993 type_t *hashed_type = typehash_insert(type);
2994 if(hashed_type != type) {
2995 /* the function type was constructed earlier freeing it here will
2996 * destroy other types... */
2997 if(iter->kind != CONSTRUCT_FUNCTION) {
3007 static declaration_t *parse_declarator(
3008 const declaration_specifiers_t *specifiers, bool may_be_abstract)
3010 declaration_t *const declaration = allocate_declaration_zero();
3011 declaration->declared_storage_class = specifiers->declared_storage_class;
3012 declaration->modifiers = specifiers->decl_modifiers;
3013 declaration->deprecated_string = specifiers->deprecated_string;
3014 declaration->get_property_sym = specifiers->get_property_sym;
3015 declaration->put_property_sym = specifiers->put_property_sym;
3016 declaration->is_inline = specifiers->is_inline;
3018 declaration->storage_class = specifiers->declared_storage_class;
3019 if(declaration->storage_class == STORAGE_CLASS_NONE
3020 && scope != global_scope) {
3021 declaration->storage_class = STORAGE_CLASS_AUTO;
3024 if(specifiers->alignment != 0) {
3025 /* TODO: add checks here */
3026 declaration->alignment = specifiers->alignment;
3029 construct_type_t *construct_type
3030 = parse_inner_declarator(declaration, may_be_abstract);
3031 type_t *const type = specifiers->type;
3032 declaration->type = construct_declarator_type(construct_type, type);
3034 if(construct_type != NULL) {
3035 obstack_free(&temp_obst, construct_type);
3041 static type_t *parse_abstract_declarator(type_t *base_type)
3043 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
3045 type_t *result = construct_declarator_type(construct_type, base_type);
3046 if(construct_type != NULL) {
3047 obstack_free(&temp_obst, construct_type);
3053 static declaration_t *append_declaration(declaration_t* const declaration)
3055 if (last_declaration != NULL) {
3056 last_declaration->next = declaration;
3058 scope->declarations = declaration;
3060 last_declaration = declaration;
3065 * Check if the declaration of main is suspicious. main should be a
3066 * function with external linkage, returning int, taking either zero
3067 * arguments, two, or three arguments of appropriate types, ie.
3069 * int main([ int argc, char **argv [, char **env ] ]).
3071 * @param decl the declaration to check
3072 * @param type the function type of the declaration
3074 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
3076 if (decl->storage_class == STORAGE_CLASS_STATIC) {
3077 warningf(decl->source_position, "'main' is normally a non-static function");
3079 if (skip_typeref(func_type->return_type) != type_int) {
3080 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
3082 const function_parameter_t *parm = func_type->parameters;
3084 type_t *const first_type = parm->type;
3085 if (!types_compatible(skip_typeref(first_type), type_int)) {
3086 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
3090 type_t *const second_type = parm->type;
3091 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
3092 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
3096 type_t *const third_type = parm->type;
3097 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
3098 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
3102 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3106 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
3112 * Check if a symbol is the equal to "main".
3114 static bool is_sym_main(const symbol_t *const sym)
3116 return strcmp(sym->string, "main") == 0;
3119 static declaration_t *internal_record_declaration(
3120 declaration_t *const declaration,
3121 const bool is_function_definition)
3123 const symbol_t *const symbol = declaration->symbol;
3124 const namespace_t namespc = (namespace_t)declaration->namespc;
3126 type_t *const orig_type = declaration->type;
3127 type_t *const type = skip_typeref(orig_type);
3128 if (is_type_function(type) &&
3129 type->function.unspecified_parameters &&
3130 warning.strict_prototypes) {
3131 warningf(declaration->source_position,
3132 "function declaration '%#T' is not a prototype",
3133 orig_type, declaration->symbol);
3136 if (is_function_definition && warning.main && is_sym_main(symbol)) {
3137 check_type_of_main(declaration, &type->function);
3140 assert(declaration->symbol != NULL);
3141 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3143 assert(declaration != previous_declaration);
3144 if (previous_declaration != NULL) {
3145 if (previous_declaration->parent_scope == scope) {
3146 /* can happen for K&R style declarations */
3147 if(previous_declaration->type == NULL) {
3148 previous_declaration->type = declaration->type;
3151 const type_t *prev_type = skip_typeref(previous_declaration->type);
3152 if (!types_compatible(type, prev_type)) {
3153 errorf(declaration->source_position,
3154 "declaration '%#T' is incompatible with "
3155 "previous declaration '%#T'",
3156 orig_type, symbol, previous_declaration->type, symbol);
3157 errorf(previous_declaration->source_position,
3158 "previous declaration of '%Y' was here", symbol);
3160 unsigned old_storage_class = previous_declaration->storage_class;
3161 if (old_storage_class == STORAGE_CLASS_ENUM_ENTRY) {
3162 errorf(declaration->source_position, "redeclaration of enum entry '%Y'", symbol);
3163 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
3164 return previous_declaration;
3167 unsigned new_storage_class = declaration->storage_class;
3169 if(is_type_incomplete(prev_type)) {
3170 previous_declaration->type = type;
3174 /* pretend no storage class means extern for function
3175 * declarations (except if the previous declaration is neither
3176 * none nor extern) */
3177 if (is_type_function(type)) {
3178 switch (old_storage_class) {
3179 case STORAGE_CLASS_NONE:
3180 old_storage_class = STORAGE_CLASS_EXTERN;
3182 case STORAGE_CLASS_EXTERN:
3183 if (is_function_definition) {
3184 if (warning.missing_prototypes &&
3185 prev_type->function.unspecified_parameters &&
3186 !is_sym_main(symbol)) {
3187 warningf(declaration->source_position,
3188 "no previous prototype for '%#T'",
3191 } else if (new_storage_class == STORAGE_CLASS_NONE) {
3192 new_storage_class = STORAGE_CLASS_EXTERN;
3200 if (old_storage_class == STORAGE_CLASS_EXTERN &&
3201 new_storage_class == STORAGE_CLASS_EXTERN) {
3202 warn_redundant_declaration:
3203 if (warning.redundant_decls) {
3204 warningf(declaration->source_position,
3205 "redundant declaration for '%Y'", symbol);
3206 warningf(previous_declaration->source_position,
3207 "previous declaration of '%Y' was here",
3210 } else if (current_function == NULL) {
3211 if (old_storage_class != STORAGE_CLASS_STATIC &&
3212 new_storage_class == STORAGE_CLASS_STATIC) {
3213 errorf(declaration->source_position,
3214 "static declaration of '%Y' follows non-static declaration",
3216 errorf(previous_declaration->source_position,
3217 "previous declaration of '%Y' was here", symbol);
3219 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
3220 goto warn_redundant_declaration;
3222 if (new_storage_class == STORAGE_CLASS_NONE) {
3223 previous_declaration->storage_class = STORAGE_CLASS_NONE;
3224 previous_declaration->declared_storage_class = STORAGE_CLASS_NONE;
3228 if (old_storage_class == new_storage_class) {
3229 errorf(declaration->source_position,
3230 "redeclaration of '%Y'", symbol);
3232 errorf(declaration->source_position,
3233 "redeclaration of '%Y' with different linkage",
3236 errorf(previous_declaration->source_position,
3237 "previous declaration of '%Y' was here", symbol);
3240 return previous_declaration;
3242 } else if (is_function_definition) {
3243 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
3244 if (warning.missing_prototypes && !is_sym_main(symbol)) {
3245 warningf(declaration->source_position,
3246 "no previous prototype for '%#T'", orig_type, symbol);
3247 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
3248 warningf(declaration->source_position,
3249 "no previous declaration for '%#T'", orig_type,
3253 } else if (warning.missing_declarations &&
3254 scope == global_scope &&
3255 !is_type_function(type) && (
3256 declaration->storage_class == STORAGE_CLASS_NONE ||
3257 declaration->storage_class == STORAGE_CLASS_THREAD
3259 warningf(declaration->source_position,
3260 "no previous declaration for '%#T'", orig_type, symbol);
3263 assert(declaration->parent_scope == NULL);
3264 assert(scope != NULL);
3266 declaration->parent_scope = scope;
3268 environment_push(declaration);
3269 return append_declaration(declaration);
3272 static declaration_t *record_declaration(declaration_t *declaration)
3274 return internal_record_declaration(declaration, false);
3277 static declaration_t *record_function_definition(declaration_t *declaration)
3279 return internal_record_declaration(declaration, true);
3282 static void parser_error_multiple_definition(declaration_t *declaration,
3283 const source_position_t source_position)
3285 errorf(source_position, "multiple definition of symbol '%Y'",
3286 declaration->symbol);
3287 errorf(declaration->source_position,
3288 "this is the location of the previous definition.");
3291 static bool is_declaration_specifier(const token_t *token,
3292 bool only_type_specifiers)
3294 switch(token->type) {
3298 return is_typedef_symbol(token->v.symbol);
3300 case T___extension__:
3303 return !only_type_specifiers;
3310 static void parse_init_declarator_rest(declaration_t *declaration)
3314 type_t *orig_type = declaration->type;
3315 type_t *type = skip_typeref(orig_type);
3317 if(declaration->init.initializer != NULL) {
3318 parser_error_multiple_definition(declaration, token.source_position);
3321 bool must_be_constant = false;
3322 if(declaration->storage_class == STORAGE_CLASS_STATIC
3323 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
3324 || declaration->parent_scope == global_scope) {
3325 must_be_constant = true;
3328 parse_initializer_env_t env;
3329 env.type = orig_type;
3330 env.must_be_constant = must_be_constant;
3331 env.declaration = declaration;
3333 initializer_t *initializer = parse_initializer(&env);
3335 if(env.type != orig_type) {
3336 orig_type = env.type;
3337 type = skip_typeref(orig_type);
3338 declaration->type = env.type;
3341 if(is_type_function(type)) {
3342 errorf(declaration->source_position,
3343 "initializers not allowed for function types at declator '%Y' (type '%T')",
3344 declaration->symbol, orig_type);
3346 declaration->init.initializer = initializer;
3350 /* parse rest of a declaration without any declarator */
3351 static void parse_anonymous_declaration_rest(
3352 const declaration_specifiers_t *specifiers,
3353 parsed_declaration_func finished_declaration)
3357 declaration_t *const declaration = allocate_declaration_zero();
3358 declaration->type = specifiers->type;
3359 declaration->declared_storage_class = specifiers->declared_storage_class;
3360 declaration->source_position = specifiers->source_position;
3361 declaration->modifiers = specifiers->decl_modifiers;
3363 if (declaration->declared_storage_class != STORAGE_CLASS_NONE) {
3364 warningf(declaration->source_position, "useless storage class in empty declaration");
3366 declaration->storage_class = STORAGE_CLASS_NONE;
3368 type_t *type = declaration->type;
3369 switch (type->kind) {
3370 case TYPE_COMPOUND_STRUCT:
3371 case TYPE_COMPOUND_UNION: {
3372 if (type->compound.declaration->symbol == NULL) {
3373 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
3382 warningf(declaration->source_position, "empty declaration");
3386 finished_declaration(declaration);
3389 static void parse_declaration_rest(declaration_t *ndeclaration,
3390 const declaration_specifiers_t *specifiers,
3391 parsed_declaration_func finished_declaration)
3393 add_anchor_token(';');
3394 add_anchor_token('=');
3395 add_anchor_token(',');
3397 declaration_t *declaration = finished_declaration(ndeclaration);
3399 type_t *orig_type = declaration->type;
3400 type_t *type = skip_typeref(orig_type);
3402 if (type->kind != TYPE_FUNCTION &&
3403 declaration->is_inline &&
3404 is_type_valid(type)) {
3405 warningf(declaration->source_position,
3406 "variable '%Y' declared 'inline'\n", declaration->symbol);
3409 if(token.type == '=') {
3410 parse_init_declarator_rest(declaration);
3413 if(token.type != ',')
3417 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
3422 rem_anchor_token(';');
3423 rem_anchor_token('=');
3424 rem_anchor_token(',');
3427 static declaration_t *finished_kr_declaration(declaration_t *declaration)
3429 symbol_t *symbol = declaration->symbol;
3430 if(symbol == NULL) {
3431 errorf(HERE, "anonymous declaration not valid as function parameter");
3434 namespace_t namespc = (namespace_t) declaration->namespc;
3435 if(namespc != NAMESPACE_NORMAL) {
3436 return record_declaration(declaration);
3439 declaration_t *previous_declaration = get_declaration(symbol, namespc);
3440 if(previous_declaration == NULL ||
3441 previous_declaration->parent_scope != scope) {
3442 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
3447 if(previous_declaration->type == NULL) {
3448 previous_declaration->type = declaration->type;
3449 previous_declaration->declared_storage_class = declaration->declared_storage_class;
3450 previous_declaration->storage_class = declaration->storage_class;
3451 previous_declaration->parent_scope = scope;
3452 return previous_declaration;
3454 return record_declaration(declaration);
3458 static void parse_declaration(parsed_declaration_func finished_declaration)
3460 declaration_specifiers_t specifiers;
3461 memset(&specifiers, 0, sizeof(specifiers));
3462 parse_declaration_specifiers(&specifiers);
3464 if(token.type == ';') {
3465 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3467 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3468 parse_declaration_rest(declaration, &specifiers, finished_declaration);
3472 static void parse_kr_declaration_list(declaration_t *declaration)
3474 type_t *type = skip_typeref(declaration->type);
3475 if(!is_type_function(type))
3478 if(!type->function.kr_style_parameters)
3481 /* push function parameters */
3482 int top = environment_top();
3483 scope_t *last_scope = scope;
3484 set_scope(&declaration->scope);
3486 declaration_t *parameter = declaration->scope.declarations;
3487 for( ; parameter != NULL; parameter = parameter->next) {
3488 assert(parameter->parent_scope == NULL);
3489 parameter->parent_scope = scope;
3490 environment_push(parameter);
3493 /* parse declaration list */
3494 while(is_declaration_specifier(&token, false)) {
3495 parse_declaration(finished_kr_declaration);
3498 /* pop function parameters */
3499 assert(scope == &declaration->scope);
3500 set_scope(last_scope);
3501 environment_pop_to(top);
3503 /* update function type */
3504 type_t *new_type = duplicate_type(type);
3505 new_type->function.kr_style_parameters = false;
3507 function_parameter_t *parameters = NULL;
3508 function_parameter_t *last_parameter = NULL;
3510 declaration_t *parameter_declaration = declaration->scope.declarations;
3511 for( ; parameter_declaration != NULL;
3512 parameter_declaration = parameter_declaration->next) {
3513 type_t *parameter_type = parameter_declaration->type;
3514 if(parameter_type == NULL) {
3516 errorf(HERE, "no type specified for function parameter '%Y'",
3517 parameter_declaration->symbol);
3519 if (warning.implicit_int) {
3520 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3521 parameter_declaration->symbol);
3523 parameter_type = type_int;
3524 parameter_declaration->type = parameter_type;
3528 semantic_parameter(parameter_declaration);
3529 parameter_type = parameter_declaration->type;
3531 function_parameter_t *function_parameter
3532 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3533 memset(function_parameter, 0, sizeof(function_parameter[0]));
3535 function_parameter->type = parameter_type;
3536 if(last_parameter != NULL) {
3537 last_parameter->next = function_parameter;
3539 parameters = function_parameter;
3541 last_parameter = function_parameter;
3543 new_type->function.parameters = parameters;
3545 type = typehash_insert(new_type);
3546 if(type != new_type) {
3547 obstack_free(type_obst, new_type);
3550 declaration->type = type;
3553 static bool first_err = true;
3556 * When called with first_err set, prints the name of the current function,
3559 static void print_in_function(void) {
3562 diagnosticf("%s: In function '%Y':\n",
3563 current_function->source_position.input_name,
3564 current_function->symbol);
3569 * Check if all labels are defined in the current function.
3570 * Check if all labels are used in the current function.
3572 static void check_labels(void)
3574 for (const goto_statement_t *goto_statement = goto_first;
3575 goto_statement != NULL;
3576 goto_statement = goto_statement->next) {
3577 declaration_t *label = goto_statement->label;
3580 if (label->source_position.input_name == NULL) {
3581 print_in_function();
3582 errorf(goto_statement->base.source_position,
3583 "label '%Y' used but not defined", label->symbol);
3586 goto_first = goto_last = NULL;
3588 if (warning.unused_label) {
3589 for (const label_statement_t *label_statement = label_first;
3590 label_statement != NULL;
3591 label_statement = label_statement->next) {
3592 const declaration_t *label = label_statement->label;
3594 if (! label->used) {
3595 print_in_function();
3596 warningf(label_statement->base.source_position,
3597 "label '%Y' defined but not used", label->symbol);
3601 label_first = label_last = NULL;
3605 * Check declarations of current_function for unused entities.
3607 static void check_declarations(void)
3609 if (warning.unused_parameter) {
3610 const scope_t *scope = ¤t_function->scope;
3612 const declaration_t *parameter = scope->declarations;
3613 for (; parameter != NULL; parameter = parameter->next) {
3614 if (! parameter->used) {
3615 print_in_function();
3616 warningf(parameter->source_position,
3617 "unused parameter '%Y'", parameter->symbol);
3621 if (warning.unused_variable) {
3625 static void parse_external_declaration(void)
3627 /* function-definitions and declarations both start with declaration
3629 declaration_specifiers_t specifiers;
3630 memset(&specifiers, 0, sizeof(specifiers));
3632 add_anchor_token(';');
3633 parse_declaration_specifiers(&specifiers);
3634 rem_anchor_token(';');
3636 /* must be a declaration */
3637 if(token.type == ';') {
3638 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3642 add_anchor_token(',');
3643 add_anchor_token('=');
3644 rem_anchor_token(';');
3646 /* declarator is common to both function-definitions and declarations */
3647 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3649 rem_anchor_token(',');
3650 rem_anchor_token('=');
3651 rem_anchor_token(';');
3653 /* must be a declaration */
3654 if(token.type == ',' || token.type == '=' || token.type == ';') {
3655 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3659 /* must be a function definition */
3660 parse_kr_declaration_list(ndeclaration);
3662 if(token.type != '{') {
3663 parse_error_expected("while parsing function definition", '{', 0);
3664 eat_until_matching_token(';');
3668 type_t *type = ndeclaration->type;
3670 /* note that we don't skip typerefs: the standard doesn't allow them here
3671 * (so we can't use is_type_function here) */
3672 if(type->kind != TYPE_FUNCTION) {
3673 if (is_type_valid(type)) {
3674 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3675 type, ndeclaration->symbol);
3681 /* § 6.7.5.3 (14) a function definition with () means no
3682 * parameters (and not unspecified parameters) */
3683 if(type->function.unspecified_parameters) {
3684 type_t *duplicate = duplicate_type(type);
3685 duplicate->function.unspecified_parameters = false;
3687 type = typehash_insert(duplicate);
3688 if(type != duplicate) {
3689 obstack_free(type_obst, duplicate);
3691 ndeclaration->type = type;
3694 declaration_t *const declaration = record_function_definition(ndeclaration);
3695 if(ndeclaration != declaration) {
3696 declaration->scope = ndeclaration->scope;
3698 type = skip_typeref(declaration->type);
3700 /* push function parameters and switch scope */
3701 int top = environment_top();
3702 scope_t *last_scope = scope;
3703 set_scope(&declaration->scope);
3705 declaration_t *parameter = declaration->scope.declarations;
3706 for( ; parameter != NULL; parameter = parameter->next) {
3707 if(parameter->parent_scope == &ndeclaration->scope) {
3708 parameter->parent_scope = scope;
3710 assert(parameter->parent_scope == NULL
3711 || parameter->parent_scope == scope);
3712 parameter->parent_scope = scope;
3713 environment_push(parameter);
3716 if(declaration->init.statement != NULL) {
3717 parser_error_multiple_definition(declaration, token.source_position);
3719 goto end_of_parse_external_declaration;
3721 /* parse function body */
3722 int label_stack_top = label_top();
3723 declaration_t *old_current_function = current_function;
3724 current_function = declaration;
3726 declaration->init.statement = parse_compound_statement();
3729 check_declarations();
3731 assert(current_function == declaration);
3732 current_function = old_current_function;
3733 label_pop_to(label_stack_top);
3736 end_of_parse_external_declaration:
3737 assert(scope == &declaration->scope);
3738 set_scope(last_scope);
3739 environment_pop_to(top);
3742 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3743 source_position_t source_position)
3745 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3746 type->bitfield.base = base;
3747 type->bitfield.size = size;
3752 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3755 declaration_t *iter = compound_declaration->scope.declarations;
3756 for( ; iter != NULL; iter = iter->next) {
3757 if(iter->namespc != NAMESPACE_NORMAL)
3760 if(iter->symbol == NULL) {
3761 type_t *type = skip_typeref(iter->type);
3762 if(is_type_compound(type)) {
3763 declaration_t *result
3764 = find_compound_entry(type->compound.declaration, symbol);
3771 if(iter->symbol == symbol) {
3779 static void parse_compound_declarators(declaration_t *struct_declaration,
3780 const declaration_specifiers_t *specifiers)
3782 declaration_t *last_declaration = struct_declaration->scope.declarations;
3783 if(last_declaration != NULL) {
3784 while(last_declaration->next != NULL) {
3785 last_declaration = last_declaration->next;
3790 declaration_t *declaration;
3792 if(token.type == ':') {
3793 source_position_t source_position = HERE;
3796 type_t *base_type = specifiers->type;
3797 expression_t *size = parse_constant_expression();
3799 if(!is_type_integer(skip_typeref(base_type))) {
3800 errorf(HERE, "bitfield base type '%T' is not an integer type",
3804 type_t *type = make_bitfield_type(base_type, size, source_position);
3806 declaration = allocate_declaration_zero();
3807 declaration->namespc = NAMESPACE_NORMAL;
3808 declaration->declared_storage_class = STORAGE_CLASS_NONE;
3809 declaration->storage_class = STORAGE_CLASS_NONE;
3810 declaration->source_position = source_position;
3811 declaration->modifiers = specifiers->decl_modifiers;
3812 declaration->type = type;
3814 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3816 type_t *orig_type = declaration->type;
3817 type_t *type = skip_typeref(orig_type);
3819 if(token.type == ':') {
3820 source_position_t source_position = HERE;
3822 expression_t *size = parse_constant_expression();
3824 if(!is_type_integer(type)) {
3825 errorf(HERE, "bitfield base type '%T' is not an "
3826 "integer type", orig_type);
3829 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3830 declaration->type = bitfield_type;
3832 /* TODO we ignore arrays for now... what is missing is a check
3833 * that they're at the end of the struct */
3834 if(is_type_incomplete(type) && !is_type_array(type)) {
3836 "compound member '%Y' has incomplete type '%T'",
3837 declaration->symbol, orig_type);
3838 } else if(is_type_function(type)) {
3839 errorf(HERE, "compound member '%Y' must not have function "
3840 "type '%T'", declaration->symbol, orig_type);
3845 /* make sure we don't define a symbol multiple times */
3846 symbol_t *symbol = declaration->symbol;
3847 if(symbol != NULL) {
3848 declaration_t *prev_decl
3849 = find_compound_entry(struct_declaration, symbol);
3851 if(prev_decl != NULL) {
3852 assert(prev_decl->symbol == symbol);
3853 errorf(declaration->source_position,
3854 "multiple declarations of symbol '%Y'", symbol);
3855 errorf(prev_decl->source_position,
3856 "previous declaration of '%Y' was here", symbol);
3860 /* append declaration */
3861 if(last_declaration != NULL) {
3862 last_declaration->next = declaration;
3864 struct_declaration->scope.declarations = declaration;
3866 last_declaration = declaration;
3868 if(token.type != ',')
3878 static void parse_compound_type_entries(declaration_t *compound_declaration)
3881 add_anchor_token('}');
3883 while(token.type != '}' && token.type != T_EOF) {
3884 declaration_specifiers_t specifiers;
3885 memset(&specifiers, 0, sizeof(specifiers));
3886 parse_declaration_specifiers(&specifiers);
3888 parse_compound_declarators(compound_declaration, &specifiers);
3890 rem_anchor_token('}');
3892 if(token.type == T_EOF) {
3893 errorf(HERE, "EOF while parsing struct");
3898 static type_t *parse_typename(void)
3900 declaration_specifiers_t specifiers;
3901 memset(&specifiers, 0, sizeof(specifiers));
3902 parse_declaration_specifiers(&specifiers);
3903 if(specifiers.declared_storage_class != STORAGE_CLASS_NONE) {
3904 /* TODO: improve error message, user does probably not know what a
3905 * storage class is...
3907 errorf(HERE, "typename may not have a storage class");
3910 type_t *result = parse_abstract_declarator(specifiers.type);
3918 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3919 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3920 expression_t *left);
3922 typedef struct expression_parser_function_t expression_parser_function_t;
3923 struct expression_parser_function_t {
3924 unsigned precedence;
3925 parse_expression_function parser;
3926 unsigned infix_precedence;
3927 parse_expression_infix_function infix_parser;
3930 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3933 * Creates a new invalid expression.
3935 static expression_t *create_invalid_expression(void)
3937 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3938 expression->base.source_position = token.source_position;
3943 * Prints an error message if an expression was expected but not read
3945 static expression_t *expected_expression_error(void)
3947 /* skip the error message if the error token was read */
3948 if (token.type != T_ERROR) {
3949 errorf(HERE, "expected expression, got token '%K'", &token);
3953 return create_invalid_expression();
3957 * Parse a string constant.
3959 static expression_t *parse_string_const(void)
3962 if (token.type == T_STRING_LITERAL) {
3963 string_t res = token.v.string;
3965 while (token.type == T_STRING_LITERAL) {
3966 res = concat_strings(&res, &token.v.string);
3969 if (token.type != T_WIDE_STRING_LITERAL) {
3970 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3971 /* note: that we use type_char_ptr here, which is already the
3972 * automatic converted type. revert_automatic_type_conversion
3973 * will construct the array type */
3974 cnst->base.type = type_char_ptr;
3975 cnst->string.value = res;
3979 wres = concat_string_wide_string(&res, &token.v.wide_string);
3981 wres = token.v.wide_string;
3986 switch (token.type) {
3987 case T_WIDE_STRING_LITERAL:
3988 wres = concat_wide_strings(&wres, &token.v.wide_string);
3991 case T_STRING_LITERAL:
3992 wres = concat_wide_string_string(&wres, &token.v.string);
3996 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3997 cnst->base.type = type_wchar_t_ptr;
3998 cnst->wide_string.value = wres;
4007 * Parse an integer constant.
4009 static expression_t *parse_int_const(void)
4011 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4012 cnst->base.source_position = HERE;
4013 cnst->base.type = token.datatype;
4014 cnst->conste.v.int_value = token.v.intvalue;
4022 * Parse a character constant.
4024 static expression_t *parse_character_constant(void)
4026 expression_t *cnst = allocate_expression_zero(EXPR_CHARACTER_CONSTANT);
4028 cnst->base.source_position = HERE;
4029 cnst->base.type = token.datatype;
4030 cnst->conste.v.character = token.v.string;
4032 if (cnst->conste.v.character.size != 1) {
4033 if (warning.multichar && (c_mode & _GNUC)) {
4035 warningf(HERE, "multi-character character constant");
4037 errorf(HERE, "more than 1 characters in character constant");
4046 * Parse a wide character constant.
4048 static expression_t *parse_wide_character_constant(void)
4050 expression_t *cnst = allocate_expression_zero(EXPR_WIDE_CHARACTER_CONSTANT);
4052 cnst->base.source_position = HERE;
4053 cnst->base.type = token.datatype;
4054 cnst->conste.v.wide_character = token.v.wide_string;
4056 if (cnst->conste.v.wide_character.size != 1) {
4057 if (warning.multichar && (c_mode & _GNUC)) {
4059 warningf(HERE, "multi-character character constant");
4061 errorf(HERE, "more than 1 characters in character constant");
4070 * Parse a float constant.
4072 static expression_t *parse_float_const(void)
4074 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
4075 cnst->base.type = token.datatype;
4076 cnst->conste.v.float_value = token.v.floatvalue;
4083 static declaration_t *create_implicit_function(symbol_t *symbol,
4084 const source_position_t source_position)
4086 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
4087 ntype->function.return_type = type_int;
4088 ntype->function.unspecified_parameters = true;
4090 type_t *type = typehash_insert(ntype);
4095 declaration_t *const declaration = allocate_declaration_zero();
4096 declaration->storage_class = STORAGE_CLASS_EXTERN;
4097 declaration->declared_storage_class = STORAGE_CLASS_EXTERN;
4098 declaration->type = type;
4099 declaration->symbol = symbol;
4100 declaration->source_position = source_position;
4101 declaration->parent_scope = global_scope;
4103 scope_t *old_scope = scope;
4104 set_scope(global_scope);
4106 environment_push(declaration);
4107 /* prepends the declaration to the global declarations list */
4108 declaration->next = scope->declarations;
4109 scope->declarations = declaration;
4111 assert(scope == global_scope);
4112 set_scope(old_scope);
4118 * Creates a return_type (func)(argument_type) function type if not
4121 * @param return_type the return type
4122 * @param argument_type the argument type
4124 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
4126 function_parameter_t *parameter
4127 = obstack_alloc(type_obst, sizeof(parameter[0]));
4128 memset(parameter, 0, sizeof(parameter[0]));
4129 parameter->type = argument_type;
4131 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
4132 type->function.return_type = return_type;
4133 type->function.parameters = parameter;
4135 type_t *result = typehash_insert(type);
4136 if(result != type) {
4144 * Creates a function type for some function like builtins.
4146 * @param symbol the symbol describing the builtin
4148 static type_t *get_builtin_symbol_type(symbol_t *symbol)
4150 switch(symbol->ID) {
4151 case T___builtin_alloca:
4152 return make_function_1_type(type_void_ptr, type_size_t);
4153 case T___builtin_nan:
4154 return make_function_1_type(type_double, type_char_ptr);
4155 case T___builtin_nanf:
4156 return make_function_1_type(type_float, type_char_ptr);
4157 case T___builtin_nand:
4158 return make_function_1_type(type_long_double, type_char_ptr);
4159 case T___builtin_va_end:
4160 return make_function_1_type(type_void, type_valist);
4162 internal_errorf(HERE, "not implemented builtin symbol found");
4167 * Performs automatic type cast as described in § 6.3.2.1.
4169 * @param orig_type the original type
4171 static type_t *automatic_type_conversion(type_t *orig_type)
4173 type_t *type = skip_typeref(orig_type);
4174 if(is_type_array(type)) {
4175 array_type_t *array_type = &type->array;
4176 type_t *element_type = array_type->element_type;
4177 unsigned qualifiers = array_type->type.qualifiers;
4179 return make_pointer_type(element_type, qualifiers);
4182 if(is_type_function(type)) {
4183 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4190 * reverts the automatic casts of array to pointer types and function
4191 * to function-pointer types as defined § 6.3.2.1
4193 type_t *revert_automatic_type_conversion(const expression_t *expression)
4195 switch (expression->kind) {
4196 case EXPR_REFERENCE: return expression->reference.declaration->type;
4197 case EXPR_SELECT: return expression->select.compound_entry->type;
4199 case EXPR_UNARY_DEREFERENCE: {
4200 const expression_t *const value = expression->unary.value;
4201 type_t *const type = skip_typeref(value->base.type);
4202 assert(is_type_pointer(type));
4203 return type->pointer.points_to;
4206 case EXPR_BUILTIN_SYMBOL:
4207 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
4209 case EXPR_ARRAY_ACCESS: {
4210 const expression_t *array_ref = expression->array_access.array_ref;
4211 type_t *type_left = skip_typeref(array_ref->base.type);
4212 if (!is_type_valid(type_left))
4214 assert(is_type_pointer(type_left));
4215 return type_left->pointer.points_to;
4218 case EXPR_STRING_LITERAL: {
4219 size_t size = expression->string.value.size;
4220 return make_array_type(type_char, size, TYPE_QUALIFIER_NONE);
4223 case EXPR_WIDE_STRING_LITERAL: {
4224 size_t size = expression->wide_string.value.size;
4225 return make_array_type(type_wchar_t, size, TYPE_QUALIFIER_NONE);
4228 case EXPR_COMPOUND_LITERAL:
4229 return expression->compound_literal.type;
4234 return expression->base.type;
4237 static expression_t *parse_reference(void)
4239 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
4241 reference_expression_t *ref = &expression->reference;
4242 ref->symbol = token.v.symbol;
4244 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
4246 source_position_t source_position = token.source_position;
4249 if(declaration == NULL) {
4250 if (! strict_mode && token.type == '(') {
4251 /* an implicitly defined function */
4252 if (warning.implicit_function_declaration) {
4253 warningf(HERE, "implicit declaration of function '%Y'",
4257 declaration = create_implicit_function(ref->symbol,
4260 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
4261 return create_invalid_expression();
4265 type_t *type = declaration->type;
4267 /* we always do the auto-type conversions; the & and sizeof parser contains
4268 * code to revert this! */
4269 type = automatic_type_conversion(type);
4271 ref->declaration = declaration;
4272 ref->base.type = type;
4274 /* this declaration is used */
4275 declaration->used = true;
4277 /* check for deprecated functions */
4278 if(declaration->modifiers & DM_DEPRECATED) {
4279 const char *prefix = "";
4280 if (is_type_function(declaration->type))
4281 prefix = "function ";
4283 if (declaration->deprecated_string != NULL) {
4284 warningf(source_position,
4285 "%s'%Y' was declared 'deprecated(\"%s\")'", prefix, declaration->symbol,
4286 declaration->deprecated_string);
4288 warningf(source_position,
4289 "%s'%Y' was declared 'deprecated'", prefix, declaration->symbol);
4296 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
4300 /* TODO check if explicit cast is allowed and issue warnings/errors */
4303 static expression_t *parse_compound_literal(type_t *type)
4305 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
4307 parse_initializer_env_t env;
4309 env.declaration = NULL;
4310 env.must_be_constant = false;
4311 initializer_t *initializer = parse_initializer(&env);
4314 expression->compound_literal.initializer = initializer;
4315 expression->compound_literal.type = type;
4316 expression->base.type = automatic_type_conversion(type);
4322 * Parse a cast expression.
4324 static expression_t *parse_cast(void)
4326 source_position_t source_position = token.source_position;
4328 type_t *type = parse_typename();
4330 /* matching add_anchor_token() is at call site */
4331 rem_anchor_token(')');
4334 if(token.type == '{') {
4335 return parse_compound_literal(type);
4338 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
4339 cast->base.source_position = source_position;
4341 expression_t *value = parse_sub_expression(20);
4343 check_cast_allowed(value, type);
4345 cast->base.type = type;
4346 cast->unary.value = value;
4350 return create_invalid_expression();
4354 * Parse a statement expression.
4356 static expression_t *parse_statement_expression(void)
4358 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
4360 statement_t *statement = parse_compound_statement();
4361 expression->statement.statement = statement;
4362 expression->base.source_position = statement->base.source_position;
4364 /* find last statement and use its type */
4365 type_t *type = type_void;
4366 const statement_t *stmt = statement->compound.statements;
4368 while (stmt->base.next != NULL)
4369 stmt = stmt->base.next;
4371 if (stmt->kind == STATEMENT_EXPRESSION) {
4372 type = stmt->expression.expression->base.type;
4375 warningf(expression->base.source_position, "empty statement expression ({})");
4377 expression->base.type = type;
4383 return create_invalid_expression();
4387 * Parse a braced expression.
4389 static expression_t *parse_brace_expression(void)
4392 add_anchor_token(')');
4394 switch(token.type) {
4396 /* gcc extension: a statement expression */
4397 return parse_statement_expression();
4401 return parse_cast();
4403 if(is_typedef_symbol(token.v.symbol)) {
4404 return parse_cast();
4408 expression_t *result = parse_expression();
4409 rem_anchor_token(')');
4414 return create_invalid_expression();
4417 static expression_t *parse_function_keyword(void)
4422 if (current_function == NULL) {
4423 errorf(HERE, "'__func__' used outside of a function");
4426 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
4427 expression->base.type = type_char_ptr;
4432 static expression_t *parse_pretty_function_keyword(void)
4434 eat(T___PRETTY_FUNCTION__);
4437 if (current_function == NULL) {
4438 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
4441 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
4442 expression->base.type = type_char_ptr;
4447 static designator_t *parse_designator(void)
4449 designator_t *result = allocate_ast_zero(sizeof(result[0]));
4450 result->source_position = HERE;
4452 if(token.type != T_IDENTIFIER) {
4453 parse_error_expected("while parsing member designator",
4457 result->symbol = token.v.symbol;
4460 designator_t *last_designator = result;
4462 if(token.type == '.') {
4464 if(token.type != T_IDENTIFIER) {
4465 parse_error_expected("while parsing member designator",
4469 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4470 designator->source_position = HERE;
4471 designator->symbol = token.v.symbol;
4474 last_designator->next = designator;
4475 last_designator = designator;
4478 if(token.type == '[') {
4480 add_anchor_token(']');
4481 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
4482 designator->source_position = HERE;
4483 designator->array_index = parse_expression();
4484 rem_anchor_token(']');
4486 if(designator->array_index == NULL) {
4490 last_designator->next = designator;
4491 last_designator = designator;
4503 * Parse the __builtin_offsetof() expression.
4505 static expression_t *parse_offsetof(void)
4507 eat(T___builtin_offsetof);
4509 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
4510 expression->base.type = type_size_t;
4513 add_anchor_token(',');
4514 type_t *type = parse_typename();
4515 rem_anchor_token(',');
4517 add_anchor_token(')');
4518 designator_t *designator = parse_designator();
4519 rem_anchor_token(')');
4522 expression->offsetofe.type = type;
4523 expression->offsetofe.designator = designator;
4526 memset(&path, 0, sizeof(path));
4527 path.top_type = type;
4528 path.path = NEW_ARR_F(type_path_entry_t, 0);
4530 descend_into_subtype(&path);
4532 if(!walk_designator(&path, designator, true)) {
4533 return create_invalid_expression();
4536 DEL_ARR_F(path.path);
4540 return create_invalid_expression();
4544 * Parses a _builtin_va_start() expression.
4546 static expression_t *parse_va_start(void)
4548 eat(T___builtin_va_start);
4550 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
4553 add_anchor_token(',');
4554 expression->va_starte.ap = parse_assignment_expression();
4555 rem_anchor_token(',');
4557 expression_t *const expr = parse_assignment_expression();
4558 if (expr->kind == EXPR_REFERENCE) {
4559 declaration_t *const decl = expr->reference.declaration;
4561 return create_invalid_expression();
4562 if (decl->parent_scope == ¤t_function->scope &&
4563 decl->next == NULL) {
4564 expression->va_starte.parameter = decl;
4569 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
4571 return create_invalid_expression();
4575 * Parses a _builtin_va_arg() expression.
4577 static expression_t *parse_va_arg(void)
4579 eat(T___builtin_va_arg);
4581 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
4584 expression->va_arge.ap = parse_assignment_expression();
4586 expression->base.type = parse_typename();
4591 return create_invalid_expression();
4594 static expression_t *parse_builtin_symbol(void)
4596 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
4598 symbol_t *symbol = token.v.symbol;
4600 expression->builtin_symbol.symbol = symbol;
4603 type_t *type = get_builtin_symbol_type(symbol);
4604 type = automatic_type_conversion(type);
4606 expression->base.type = type;
4611 * Parses a __builtin_constant() expression.
4613 static expression_t *parse_builtin_constant(void)
4615 eat(T___builtin_constant_p);
4617 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4620 add_anchor_token(')');
4621 expression->builtin_constant.value = parse_assignment_expression();
4622 rem_anchor_token(')');
4624 expression->base.type = type_int;
4628 return create_invalid_expression();
4632 * Parses a __builtin_prefetch() expression.
4634 static expression_t *parse_builtin_prefetch(void)
4636 eat(T___builtin_prefetch);
4638 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4641 add_anchor_token(')');
4642 expression->builtin_prefetch.adr = parse_assignment_expression();
4643 if (token.type == ',') {
4645 expression->builtin_prefetch.rw = parse_assignment_expression();
4647 if (token.type == ',') {
4649 expression->builtin_prefetch.locality = parse_assignment_expression();
4651 rem_anchor_token(')');
4653 expression->base.type = type_void;
4657 return create_invalid_expression();
4661 * Parses a __builtin_is_*() compare expression.
4663 static expression_t *parse_compare_builtin(void)
4665 expression_t *expression;
4667 switch(token.type) {
4668 case T___builtin_isgreater:
4669 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4671 case T___builtin_isgreaterequal:
4672 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4674 case T___builtin_isless:
4675 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4677 case T___builtin_islessequal:
4678 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4680 case T___builtin_islessgreater:
4681 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4683 case T___builtin_isunordered:
4684 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4687 internal_errorf(HERE, "invalid compare builtin found");
4690 expression->base.source_position = HERE;
4694 expression->binary.left = parse_assignment_expression();
4696 expression->binary.right = parse_assignment_expression();
4699 type_t *const orig_type_left = expression->binary.left->base.type;
4700 type_t *const orig_type_right = expression->binary.right->base.type;
4702 type_t *const type_left = skip_typeref(orig_type_left);
4703 type_t *const type_right = skip_typeref(orig_type_right);
4704 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4705 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4706 type_error_incompatible("invalid operands in comparison",
4707 expression->base.source_position, orig_type_left, orig_type_right);
4710 semantic_comparison(&expression->binary);
4715 return create_invalid_expression();
4719 * Parses a __builtin_expect() expression.
4721 static expression_t *parse_builtin_expect(void)
4723 eat(T___builtin_expect);
4725 expression_t *expression
4726 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4729 expression->binary.left = parse_assignment_expression();
4731 expression->binary.right = parse_constant_expression();
4734 expression->base.type = expression->binary.left->base.type;
4738 return create_invalid_expression();
4742 * Parses a MS assume() expression.
4744 static expression_t *parse_assume(void) {
4747 expression_t *expression
4748 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4751 add_anchor_token(')');
4752 expression->unary.value = parse_assignment_expression();
4753 rem_anchor_token(')');
4756 expression->base.type = type_void;
4759 return create_invalid_expression();
4763 * Parses a primary expression.
4765 static expression_t *parse_primary_expression(void)
4767 switch (token.type) {
4768 case T_INTEGER: return parse_int_const();
4769 case T_CHARACTER_CONSTANT: return parse_character_constant();
4770 case T_WIDE_CHARACTER_CONSTANT: return parse_wide_character_constant();
4771 case T_FLOATINGPOINT: return parse_float_const();
4772 case T_STRING_LITERAL:
4773 case T_WIDE_STRING_LITERAL: return parse_string_const();
4774 case T_IDENTIFIER: return parse_reference();
4775 case T___FUNCTION__:
4776 case T___func__: return parse_function_keyword();
4777 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4778 case T___builtin_offsetof: return parse_offsetof();
4779 case T___builtin_va_start: return parse_va_start();
4780 case T___builtin_va_arg: return parse_va_arg();
4781 case T___builtin_expect: return parse_builtin_expect();
4782 case T___builtin_alloca:
4783 case T___builtin_nan:
4784 case T___builtin_nand:
4785 case T___builtin_nanf:
4786 case T___builtin_va_end: return parse_builtin_symbol();
4787 case T___builtin_isgreater:
4788 case T___builtin_isgreaterequal:
4789 case T___builtin_isless:
4790 case T___builtin_islessequal:
4791 case T___builtin_islessgreater:
4792 case T___builtin_isunordered: return parse_compare_builtin();
4793 case T___builtin_constant_p: return parse_builtin_constant();
4794 case T___builtin_prefetch: return parse_builtin_prefetch();
4795 case T__assume: return parse_assume();
4797 case '(': return parse_brace_expression();
4800 errorf(HERE, "unexpected token %K, expected an expression", &token);
4801 return create_invalid_expression();
4805 * Check if the expression has the character type and issue a warning then.
4807 static void check_for_char_index_type(const expression_t *expression) {
4808 type_t *const type = expression->base.type;
4809 const type_t *const base_type = skip_typeref(type);
4811 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4812 warning.char_subscripts) {
4813 warningf(expression->base.source_position,
4814 "array subscript has type '%T'", type);
4818 static expression_t *parse_array_expression(unsigned precedence,
4824 add_anchor_token(']');
4826 expression_t *inside = parse_expression();
4828 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4830 array_access_expression_t *array_access = &expression->array_access;
4832 type_t *const orig_type_left = left->base.type;
4833 type_t *const orig_type_inside = inside->base.type;
4835 type_t *const type_left = skip_typeref(orig_type_left);
4836 type_t *const type_inside = skip_typeref(orig_type_inside);
4838 type_t *return_type;
4839 if (is_type_pointer(type_left)) {
4840 return_type = type_left->pointer.points_to;
4841 array_access->array_ref = left;
4842 array_access->index = inside;
4843 check_for_char_index_type(inside);
4844 } else if (is_type_pointer(type_inside)) {
4845 return_type = type_inside->pointer.points_to;
4846 array_access->array_ref = inside;
4847 array_access->index = left;
4848 array_access->flipped = true;
4849 check_for_char_index_type(left);
4851 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4853 "array access on object with non-pointer types '%T', '%T'",
4854 orig_type_left, orig_type_inside);
4856 return_type = type_error_type;
4857 array_access->array_ref = create_invalid_expression();
4860 rem_anchor_token(']');
4861 if(token.type != ']') {
4862 parse_error_expected("Problem while parsing array access", ']', 0);
4867 return_type = automatic_type_conversion(return_type);
4868 expression->base.type = return_type;
4873 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4875 expression_t *tp_expression = allocate_expression_zero(kind);
4876 tp_expression->base.type = type_size_t;
4878 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4880 add_anchor_token(')');
4881 tp_expression->typeprop.type = parse_typename();
4882 rem_anchor_token(')');
4885 expression_t *expression = parse_sub_expression(precedence);
4886 expression->base.type = revert_automatic_type_conversion(expression);
4888 tp_expression->typeprop.type = expression->base.type;
4889 tp_expression->typeprop.tp_expression = expression;
4892 return tp_expression;
4894 return create_invalid_expression();
4897 static expression_t *parse_sizeof(unsigned precedence)
4900 return parse_typeprop(EXPR_SIZEOF, precedence);
4903 static expression_t *parse_alignof(unsigned precedence)
4906 return parse_typeprop(EXPR_SIZEOF, precedence);
4909 static expression_t *parse_select_expression(unsigned precedence,
4910 expression_t *compound)
4913 assert(token.type == '.' || token.type == T_MINUSGREATER);
4915 bool is_pointer = (token.type == T_MINUSGREATER);
4918 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4919 select->select.compound = compound;
4921 if(token.type != T_IDENTIFIER) {
4922 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4925 symbol_t *symbol = token.v.symbol;
4926 select->select.symbol = symbol;
4929 type_t *const orig_type = compound->base.type;
4930 type_t *const type = skip_typeref(orig_type);
4932 type_t *type_left = type;
4934 if (!is_type_pointer(type)) {
4935 if (is_type_valid(type)) {
4936 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4938 return create_invalid_expression();
4940 type_left = type->pointer.points_to;
4942 type_left = skip_typeref(type_left);
4944 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4945 type_left->kind != TYPE_COMPOUND_UNION) {
4946 if (is_type_valid(type_left)) {
4947 errorf(HERE, "request for member '%Y' in something not a struct or "
4948 "union, but '%T'", symbol, type_left);
4950 return create_invalid_expression();
4953 declaration_t *const declaration = type_left->compound.declaration;
4955 if(!declaration->init.is_defined) {
4956 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4958 return create_invalid_expression();
4961 declaration_t *iter = find_compound_entry(declaration, symbol);
4963 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4964 return create_invalid_expression();
4967 /* we always do the auto-type conversions; the & and sizeof parser contains
4968 * code to revert this! */
4969 type_t *expression_type = automatic_type_conversion(iter->type);
4971 select->select.compound_entry = iter;
4972 select->base.type = expression_type;
4974 if(expression_type->kind == TYPE_BITFIELD) {
4975 expression_t *extract
4976 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4977 extract->unary.value = select;
4978 extract->base.type = expression_type->bitfield.base;
4987 * Parse a call expression, ie. expression '( ... )'.
4989 * @param expression the function address
4991 static expression_t *parse_call_expression(unsigned precedence,
4992 expression_t *expression)
4995 expression_t *result = allocate_expression_zero(EXPR_CALL);
4996 result->base.source_position = expression->base.source_position;
4998 call_expression_t *call = &result->call;
4999 call->function = expression;
5001 type_t *const orig_type = expression->base.type;
5002 type_t *const type = skip_typeref(orig_type);
5004 function_type_t *function_type = NULL;
5005 if (is_type_pointer(type)) {
5006 type_t *const to_type = skip_typeref(type->pointer.points_to);
5008 if (is_type_function(to_type)) {
5009 function_type = &to_type->function;
5010 call->base.type = function_type->return_type;
5014 if (function_type == NULL && is_type_valid(type)) {
5015 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
5018 /* parse arguments */
5020 add_anchor_token(')');
5021 add_anchor_token(',');
5023 if(token.type != ')') {
5024 call_argument_t *last_argument = NULL;
5027 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
5029 argument->expression = parse_assignment_expression();
5030 if(last_argument == NULL) {
5031 call->arguments = argument;
5033 last_argument->next = argument;
5035 last_argument = argument;
5037 if(token.type != ',')
5042 rem_anchor_token(',');
5043 rem_anchor_token(')');
5046 if(function_type != NULL) {
5047 function_parameter_t *parameter = function_type->parameters;
5048 call_argument_t *argument = call->arguments;
5049 for( ; parameter != NULL && argument != NULL;
5050 parameter = parameter->next, argument = argument->next) {
5051 type_t *expected_type = parameter->type;
5052 /* TODO report scope in error messages */
5053 expression_t *const arg_expr = argument->expression;
5054 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
5055 if (res_type == NULL) {
5056 /* TODO improve error message */
5057 errorf(arg_expr->base.source_position,
5058 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
5059 arg_expr, arg_expr->base.type, expected_type);
5061 argument->expression = create_implicit_cast(argument->expression, expected_type);
5064 /* too few parameters */
5065 if(parameter != NULL) {
5066 errorf(HERE, "too few arguments to function '%E'", expression);
5067 } else if(argument != NULL) {
5068 /* too many parameters */
5069 if(!function_type->variadic
5070 && !function_type->unspecified_parameters) {
5071 errorf(HERE, "too many arguments to function '%E'", expression);
5073 /* do default promotion */
5074 for( ; argument != NULL; argument = argument->next) {
5075 type_t *type = argument->expression->base.type;
5077 type = skip_typeref(type);
5078 if(is_type_integer(type)) {
5079 type = promote_integer(type);
5080 } else if(type == type_float) {
5084 argument->expression
5085 = create_implicit_cast(argument->expression, type);
5088 check_format(&result->call);
5091 check_format(&result->call);
5097 return create_invalid_expression();
5100 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
5102 static bool same_compound_type(const type_t *type1, const type_t *type2)
5105 is_type_compound(type1) &&
5106 type1->kind == type2->kind &&
5107 type1->compound.declaration == type2->compound.declaration;
5111 * Parse a conditional expression, ie. 'expression ? ... : ...'.
5113 * @param expression the conditional expression
5115 static expression_t *parse_conditional_expression(unsigned precedence,
5116 expression_t *expression)
5119 add_anchor_token(':');
5121 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
5123 conditional_expression_t *conditional = &result->conditional;
5124 conditional->condition = expression;
5127 type_t *const condition_type_orig = expression->base.type;
5128 type_t *const condition_type = skip_typeref(condition_type_orig);
5129 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
5130 type_error("expected a scalar type in conditional condition",
5131 expression->base.source_position, condition_type_orig);
5134 expression_t *true_expression = parse_expression();
5135 rem_anchor_token(':');
5137 expression_t *false_expression = parse_sub_expression(precedence);
5139 type_t *const orig_true_type = true_expression->base.type;
5140 type_t *const orig_false_type = false_expression->base.type;
5141 type_t *const true_type = skip_typeref(orig_true_type);
5142 type_t *const false_type = skip_typeref(orig_false_type);
5145 type_t *result_type;
5146 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
5147 result_type = semantic_arithmetic(true_type, false_type);
5149 true_expression = create_implicit_cast(true_expression, result_type);
5150 false_expression = create_implicit_cast(false_expression, result_type);
5152 conditional->true_expression = true_expression;
5153 conditional->false_expression = false_expression;
5154 conditional->base.type = result_type;
5155 } else if (same_compound_type(true_type, false_type) || (
5156 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
5157 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
5159 /* just take 1 of the 2 types */
5160 result_type = true_type;
5161 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
5162 && pointers_compatible(true_type, false_type)) {
5164 result_type = true_type;
5165 } else if (is_type_pointer(true_type)
5166 && is_null_pointer_constant(false_expression)) {
5167 result_type = true_type;
5168 } else if (is_type_pointer(false_type)
5169 && is_null_pointer_constant(true_expression)) {
5170 result_type = false_type;
5172 /* TODO: one pointer to void*, other some pointer */
5174 if (is_type_valid(true_type) && is_type_valid(false_type)) {
5175 type_error_incompatible("while parsing conditional",
5176 expression->base.source_position, true_type,
5179 result_type = type_error_type;
5182 conditional->true_expression
5183 = create_implicit_cast(true_expression, result_type);
5184 conditional->false_expression
5185 = create_implicit_cast(false_expression, result_type);
5186 conditional->base.type = result_type;
5189 return create_invalid_expression();
5193 * Parse an extension expression.
5195 static expression_t *parse_extension(unsigned precedence)
5197 eat(T___extension__);
5199 /* TODO enable extensions */
5200 expression_t *expression = parse_sub_expression(precedence);
5201 /* TODO disable extensions */
5206 * Parse a __builtin_classify_type() expression.
5208 static expression_t *parse_builtin_classify_type(const unsigned precedence)
5210 eat(T___builtin_classify_type);
5212 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
5213 result->base.type = type_int;
5216 add_anchor_token(')');
5217 expression_t *expression = parse_sub_expression(precedence);
5218 rem_anchor_token(')');
5220 result->classify_type.type_expression = expression;
5224 return create_invalid_expression();
5227 static void semantic_incdec(unary_expression_t *expression)
5229 type_t *const orig_type = expression->value->base.type;
5230 type_t *const type = skip_typeref(orig_type);
5231 /* TODO !is_type_real && !is_type_pointer */
5232 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
5233 if (is_type_valid(type)) {
5234 /* TODO: improve error message */
5235 errorf(HERE, "operation needs an arithmetic or pointer type");
5240 expression->base.type = orig_type;
5243 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
5245 type_t *const orig_type = expression->value->base.type;
5246 type_t *const type = skip_typeref(orig_type);
5247 if(!is_type_arithmetic(type)) {
5248 if (is_type_valid(type)) {
5249 /* TODO: improve error message */
5250 errorf(HERE, "operation needs an arithmetic type");
5255 expression->base.type = orig_type;
5258 static void semantic_unexpr_scalar(unary_expression_t *expression)
5260 type_t *const orig_type = expression->value->base.type;
5261 type_t *const type = skip_typeref(orig_type);
5262 if (!is_type_scalar(type)) {
5263 if (is_type_valid(type)) {
5264 errorf(HERE, "operand of ! must be of scalar type");
5269 expression->base.type = orig_type;
5272 static void semantic_unexpr_integer(unary_expression_t *expression)
5274 type_t *const orig_type = expression->value->base.type;
5275 type_t *const type = skip_typeref(orig_type);
5276 if (!is_type_integer(type)) {
5277 if (is_type_valid(type)) {
5278 errorf(HERE, "operand of ~ must be of integer type");
5283 expression->base.type = orig_type;
5286 static void semantic_dereference(unary_expression_t *expression)
5288 type_t *const orig_type = expression->value->base.type;
5289 type_t *const type = skip_typeref(orig_type);
5290 if(!is_type_pointer(type)) {
5291 if (is_type_valid(type)) {
5292 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
5297 type_t *result_type = type->pointer.points_to;
5298 result_type = automatic_type_conversion(result_type);
5299 expression->base.type = result_type;
5303 * Check the semantic of the address taken expression.
5305 static void semantic_take_addr(unary_expression_t *expression)
5307 expression_t *value = expression->value;
5308 value->base.type = revert_automatic_type_conversion(value);
5310 type_t *orig_type = value->base.type;
5311 if(!is_type_valid(orig_type))
5314 if(value->kind == EXPR_REFERENCE) {
5315 declaration_t *const declaration = value->reference.declaration;
5316 if(declaration != NULL) {
5317 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
5318 errorf(expression->base.source_position,
5319 "address of register variable '%Y' requested",
5320 declaration->symbol);
5322 declaration->address_taken = 1;
5326 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
5329 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
5330 static expression_t *parse_##unexpression_type(unsigned precedence) \
5334 expression_t *unary_expression \
5335 = allocate_expression_zero(unexpression_type); \
5336 unary_expression->base.source_position = HERE; \
5337 unary_expression->unary.value = parse_sub_expression(precedence); \
5339 sfunc(&unary_expression->unary); \
5341 return unary_expression; \
5344 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
5345 semantic_unexpr_arithmetic)
5346 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
5347 semantic_unexpr_arithmetic)
5348 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
5349 semantic_unexpr_scalar)
5350 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
5351 semantic_dereference)
5352 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
5354 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
5355 semantic_unexpr_integer)
5356 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
5358 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
5361 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
5363 static expression_t *parse_##unexpression_type(unsigned precedence, \
5364 expression_t *left) \
5366 (void) precedence; \
5369 expression_t *unary_expression \
5370 = allocate_expression_zero(unexpression_type); \
5371 unary_expression->unary.value = left; \
5373 sfunc(&unary_expression->unary); \
5375 return unary_expression; \
5378 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
5379 EXPR_UNARY_POSTFIX_INCREMENT,
5381 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
5382 EXPR_UNARY_POSTFIX_DECREMENT,
5385 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
5387 /* TODO: handle complex + imaginary types */
5389 /* § 6.3.1.8 Usual arithmetic conversions */
5390 if(type_left == type_long_double || type_right == type_long_double) {
5391 return type_long_double;
5392 } else if(type_left == type_double || type_right == type_double) {
5394 } else if(type_left == type_float || type_right == type_float) {
5398 type_right = promote_integer(type_right);
5399 type_left = promote_integer(type_left);
5401 if(type_left == type_right)
5404 bool signed_left = is_type_signed(type_left);
5405 bool signed_right = is_type_signed(type_right);
5406 int rank_left = get_rank(type_left);
5407 int rank_right = get_rank(type_right);
5408 if(rank_left < rank_right) {
5409 if(signed_left == signed_right || !signed_right) {
5415 if(signed_left == signed_right || !signed_left) {
5424 * Check the semantic restrictions for a binary expression.
5426 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
5428 expression_t *const left = expression->left;
5429 expression_t *const right = expression->right;
5430 type_t *const orig_type_left = left->base.type;
5431 type_t *const orig_type_right = right->base.type;
5432 type_t *const type_left = skip_typeref(orig_type_left);
5433 type_t *const type_right = skip_typeref(orig_type_right);
5435 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5436 /* TODO: improve error message */
5437 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5438 errorf(HERE, "operation needs arithmetic types");
5443 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5444 expression->left = create_implicit_cast(left, arithmetic_type);
5445 expression->right = create_implicit_cast(right, arithmetic_type);
5446 expression->base.type = arithmetic_type;
5449 static void semantic_shift_op(binary_expression_t *expression)
5451 expression_t *const left = expression->left;
5452 expression_t *const right = expression->right;
5453 type_t *const orig_type_left = left->base.type;
5454 type_t *const orig_type_right = right->base.type;
5455 type_t * type_left = skip_typeref(orig_type_left);
5456 type_t * type_right = skip_typeref(orig_type_right);
5458 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
5459 /* TODO: improve error message */
5460 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5461 errorf(HERE, "operation needs integer types");
5466 type_left = promote_integer(type_left);
5467 type_right = promote_integer(type_right);
5469 expression->left = create_implicit_cast(left, type_left);
5470 expression->right = create_implicit_cast(right, type_right);
5471 expression->base.type = type_left;
5474 static void semantic_add(binary_expression_t *expression)
5476 expression_t *const left = expression->left;
5477 expression_t *const right = expression->right;
5478 type_t *const orig_type_left = left->base.type;
5479 type_t *const orig_type_right = right->base.type;
5480 type_t *const type_left = skip_typeref(orig_type_left);
5481 type_t *const type_right = skip_typeref(orig_type_right);
5484 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5485 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5486 expression->left = create_implicit_cast(left, arithmetic_type);
5487 expression->right = create_implicit_cast(right, arithmetic_type);
5488 expression->base.type = arithmetic_type;
5490 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5491 expression->base.type = type_left;
5492 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
5493 expression->base.type = type_right;
5494 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5495 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
5499 static void semantic_sub(binary_expression_t *expression)
5501 expression_t *const left = expression->left;
5502 expression_t *const right = expression->right;
5503 type_t *const orig_type_left = left->base.type;
5504 type_t *const orig_type_right = right->base.type;
5505 type_t *const type_left = skip_typeref(orig_type_left);
5506 type_t *const type_right = skip_typeref(orig_type_right);
5509 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5510 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5511 expression->left = create_implicit_cast(left, arithmetic_type);
5512 expression->right = create_implicit_cast(right, arithmetic_type);
5513 expression->base.type = arithmetic_type;
5515 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
5516 expression->base.type = type_left;
5517 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
5518 if(!pointers_compatible(type_left, type_right)) {
5520 "pointers to incompatible objects to binary '-' ('%T', '%T')",
5521 orig_type_left, orig_type_right);
5523 expression->base.type = type_ptrdiff_t;
5525 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5526 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
5527 orig_type_left, orig_type_right);
5532 * Check the semantics of comparison expressions.
5534 * @param expression The expression to check.
5536 static void semantic_comparison(binary_expression_t *expression)
5538 expression_t *left = expression->left;
5539 expression_t *right = expression->right;
5540 type_t *orig_type_left = left->base.type;
5541 type_t *orig_type_right = right->base.type;
5543 type_t *type_left = skip_typeref(orig_type_left);
5544 type_t *type_right = skip_typeref(orig_type_right);
5546 /* TODO non-arithmetic types */
5547 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5548 if (warning.sign_compare &&
5549 (expression->base.kind != EXPR_BINARY_EQUAL &&
5550 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
5551 (is_type_signed(type_left) != is_type_signed(type_right))) {
5552 warningf(expression->base.source_position,
5553 "comparison between signed and unsigned");
5555 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5556 expression->left = create_implicit_cast(left, arithmetic_type);
5557 expression->right = create_implicit_cast(right, arithmetic_type);
5558 expression->base.type = arithmetic_type;
5559 if (warning.float_equal &&
5560 (expression->base.kind == EXPR_BINARY_EQUAL ||
5561 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
5562 is_type_float(arithmetic_type)) {
5563 warningf(expression->base.source_position,
5564 "comparing floating point with == or != is unsafe");
5566 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
5567 /* TODO check compatibility */
5568 } else if (is_type_pointer(type_left)) {
5569 expression->right = create_implicit_cast(right, type_left);
5570 } else if (is_type_pointer(type_right)) {
5571 expression->left = create_implicit_cast(left, type_right);
5572 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5573 type_error_incompatible("invalid operands in comparison",
5574 expression->base.source_position,
5575 type_left, type_right);
5577 expression->base.type = type_int;
5580 static void semantic_arithmetic_assign(binary_expression_t *expression)
5582 expression_t *left = expression->left;
5583 expression_t *right = expression->right;
5584 type_t *orig_type_left = left->base.type;
5585 type_t *orig_type_right = right->base.type;
5587 type_t *type_left = skip_typeref(orig_type_left);
5588 type_t *type_right = skip_typeref(orig_type_right);
5590 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
5591 /* TODO: improve error message */
5592 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5593 errorf(HERE, "operation needs arithmetic types");
5598 /* combined instructions are tricky. We can't create an implicit cast on
5599 * the left side, because we need the uncasted form for the store.
5600 * The ast2firm pass has to know that left_type must be right_type
5601 * for the arithmetic operation and create a cast by itself */
5602 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
5603 expression->right = create_implicit_cast(right, arithmetic_type);
5604 expression->base.type = type_left;
5607 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
5609 expression_t *const left = expression->left;
5610 expression_t *const right = expression->right;
5611 type_t *const orig_type_left = left->base.type;
5612 type_t *const orig_type_right = right->base.type;
5613 type_t *const type_left = skip_typeref(orig_type_left);
5614 type_t *const type_right = skip_typeref(orig_type_right);
5616 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
5617 /* combined instructions are tricky. We can't create an implicit cast on
5618 * the left side, because we need the uncasted form for the store.
5619 * The ast2firm pass has to know that left_type must be right_type
5620 * for the arithmetic operation and create a cast by itself */
5621 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
5622 expression->right = create_implicit_cast(right, arithmetic_type);
5623 expression->base.type = type_left;
5624 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
5625 expression->base.type = type_left;
5626 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
5627 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
5632 * Check the semantic restrictions of a logical expression.
5634 static void semantic_logical_op(binary_expression_t *expression)
5636 expression_t *const left = expression->left;
5637 expression_t *const right = expression->right;
5638 type_t *const orig_type_left = left->base.type;
5639 type_t *const orig_type_right = right->base.type;
5640 type_t *const type_left = skip_typeref(orig_type_left);
5641 type_t *const type_right = skip_typeref(orig_type_right);
5643 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
5644 /* TODO: improve error message */
5645 if (is_type_valid(type_left) && is_type_valid(type_right)) {
5646 errorf(HERE, "operation needs scalar types");
5651 expression->base.type = type_int;
5655 * Checks if a compound type has constant fields.
5657 static bool has_const_fields(const compound_type_t *type)
5659 const scope_t *scope = &type->declaration->scope;
5660 const declaration_t *declaration = scope->declarations;
5662 for (; declaration != NULL; declaration = declaration->next) {
5663 if (declaration->namespc != NAMESPACE_NORMAL)
5666 const type_t *decl_type = skip_typeref(declaration->type);
5667 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5675 * Check the semantic restrictions of a binary assign expression.
5677 static void semantic_binexpr_assign(binary_expression_t *expression)
5679 expression_t *left = expression->left;
5680 type_t *orig_type_left = left->base.type;
5682 type_t *type_left = revert_automatic_type_conversion(left);
5683 type_left = skip_typeref(orig_type_left);
5685 /* must be a modifiable lvalue */
5686 if (is_type_array(type_left)) {
5687 errorf(HERE, "cannot assign to arrays ('%E')", left);
5690 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5691 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5695 if(is_type_incomplete(type_left)) {
5697 "left-hand side of assignment '%E' has incomplete type '%T'",
5698 left, orig_type_left);
5701 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5702 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5703 left, orig_type_left);
5707 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5709 if (res_type == NULL) {
5710 errorf(expression->base.source_position,
5711 "cannot assign to '%T' from '%T'",
5712 orig_type_left, expression->right->base.type);
5714 expression->right = create_implicit_cast(expression->right, res_type);
5717 expression->base.type = orig_type_left;
5721 * Determine if the outermost operation (or parts thereof) of the given
5722 * expression has no effect in order to generate a warning about this fact.
5723 * Therefore in some cases this only examines some of the operands of the
5724 * expression (see comments in the function and examples below).
5726 * f() + 23; // warning, because + has no effect
5727 * x || f(); // no warning, because x controls execution of f()
5728 * x ? y : f(); // warning, because y has no effect
5729 * (void)x; // no warning to be able to suppress the warning
5730 * This function can NOT be used for an "expression has definitely no effect"-
5732 static bool expression_has_effect(const expression_t *const expr)
5734 switch (expr->kind) {
5735 case EXPR_UNKNOWN: break;
5736 case EXPR_INVALID: return true; /* do NOT warn */
5737 case EXPR_REFERENCE: return false;
5738 case EXPR_CONST: return false;
5739 case EXPR_CHARACTER_CONSTANT: return false;
5740 case EXPR_WIDE_CHARACTER_CONSTANT: return false;
5741 case EXPR_STRING_LITERAL: return false;
5742 case EXPR_WIDE_STRING_LITERAL: return false;
5745 const call_expression_t *const call = &expr->call;
5746 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5749 switch (call->function->builtin_symbol.symbol->ID) {
5750 case T___builtin_va_end: return true;
5751 default: return false;
5755 /* Generate the warning if either the left or right hand side of a
5756 * conditional expression has no effect */
5757 case EXPR_CONDITIONAL: {
5758 const conditional_expression_t *const cond = &expr->conditional;
5760 expression_has_effect(cond->true_expression) &&
5761 expression_has_effect(cond->false_expression);
5764 case EXPR_SELECT: return false;
5765 case EXPR_ARRAY_ACCESS: return false;
5766 case EXPR_SIZEOF: return false;
5767 case EXPR_CLASSIFY_TYPE: return false;
5768 case EXPR_ALIGNOF: return false;
5770 case EXPR_FUNCTION: return false;
5771 case EXPR_PRETTY_FUNCTION: return false;
5772 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5773 case EXPR_BUILTIN_CONSTANT_P: return false;
5774 case EXPR_BUILTIN_PREFETCH: return true;
5775 case EXPR_OFFSETOF: return false;
5776 case EXPR_VA_START: return true;
5777 case EXPR_VA_ARG: return true;
5778 case EXPR_STATEMENT: return true; // TODO
5779 case EXPR_COMPOUND_LITERAL: return false;
5781 case EXPR_UNARY_NEGATE: return false;
5782 case EXPR_UNARY_PLUS: return false;
5783 case EXPR_UNARY_BITWISE_NEGATE: return false;
5784 case EXPR_UNARY_NOT: return false;
5785 case EXPR_UNARY_DEREFERENCE: return false;
5786 case EXPR_UNARY_TAKE_ADDRESS: return false;
5787 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5788 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5789 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5790 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5792 /* Treat void casts as if they have an effect in order to being able to
5793 * suppress the warning */
5794 case EXPR_UNARY_CAST: {
5795 type_t *const type = skip_typeref(expr->base.type);
5796 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5799 case EXPR_UNARY_CAST_IMPLICIT: return true;
5800 case EXPR_UNARY_ASSUME: return true;
5801 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5803 case EXPR_BINARY_ADD: return false;
5804 case EXPR_BINARY_SUB: return false;
5805 case EXPR_BINARY_MUL: return false;
5806 case EXPR_BINARY_DIV: return false;
5807 case EXPR_BINARY_MOD: return false;
5808 case EXPR_BINARY_EQUAL: return false;
5809 case EXPR_BINARY_NOTEQUAL: return false;
5810 case EXPR_BINARY_LESS: return false;
5811 case EXPR_BINARY_LESSEQUAL: return false;
5812 case EXPR_BINARY_GREATER: return false;
5813 case EXPR_BINARY_GREATEREQUAL: return false;
5814 case EXPR_BINARY_BITWISE_AND: return false;
5815 case EXPR_BINARY_BITWISE_OR: return false;
5816 case EXPR_BINARY_BITWISE_XOR: return false;
5817 case EXPR_BINARY_SHIFTLEFT: return false;
5818 case EXPR_BINARY_SHIFTRIGHT: return false;
5819 case EXPR_BINARY_ASSIGN: return true;
5820 case EXPR_BINARY_MUL_ASSIGN: return true;
5821 case EXPR_BINARY_DIV_ASSIGN: return true;
5822 case EXPR_BINARY_MOD_ASSIGN: return true;
5823 case EXPR_BINARY_ADD_ASSIGN: return true;
5824 case EXPR_BINARY_SUB_ASSIGN: return true;
5825 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5826 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5827 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5828 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5829 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5831 /* Only examine the right hand side of && and ||, because the left hand
5832 * side already has the effect of controlling the execution of the right
5834 case EXPR_BINARY_LOGICAL_AND:
5835 case EXPR_BINARY_LOGICAL_OR:
5836 /* Only examine the right hand side of a comma expression, because the left
5837 * hand side has a separate warning */
5838 case EXPR_BINARY_COMMA:
5839 return expression_has_effect(expr->binary.right);
5841 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5842 case EXPR_BINARY_ISGREATER: return false;
5843 case EXPR_BINARY_ISGREATEREQUAL: return false;
5844 case EXPR_BINARY_ISLESS: return false;
5845 case EXPR_BINARY_ISLESSEQUAL: return false;
5846 case EXPR_BINARY_ISLESSGREATER: return false;
5847 case EXPR_BINARY_ISUNORDERED: return false;
5850 internal_errorf(HERE, "unexpected expression");
5853 static void semantic_comma(binary_expression_t *expression)
5855 if (warning.unused_value) {
5856 const expression_t *const left = expression->left;
5857 if (!expression_has_effect(left)) {
5858 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5861 expression->base.type = expression->right->base.type;
5864 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5865 static expression_t *parse_##binexpression_type(unsigned precedence, \
5866 expression_t *left) \
5869 source_position_t pos = HERE; \
5871 expression_t *right = parse_sub_expression(precedence + lr); \
5873 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5874 binexpr->base.source_position = pos; \
5875 binexpr->binary.left = left; \
5876 binexpr->binary.right = right; \
5877 sfunc(&binexpr->binary); \
5882 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5883 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5884 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5885 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5886 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5887 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5888 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5889 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5890 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5892 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5893 semantic_comparison, 1)
5894 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5895 semantic_comparison, 1)
5896 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5897 semantic_comparison, 1)
5898 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5899 semantic_comparison, 1)
5901 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5902 semantic_binexpr_arithmetic, 1)
5903 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5904 semantic_binexpr_arithmetic, 1)
5905 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5906 semantic_binexpr_arithmetic, 1)
5907 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5908 semantic_logical_op, 1)
5909 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5910 semantic_logical_op, 1)
5911 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5912 semantic_shift_op, 1)
5913 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5914 semantic_shift_op, 1)
5915 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5916 semantic_arithmetic_addsubb_assign, 0)
5917 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5918 semantic_arithmetic_addsubb_assign, 0)
5919 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5920 semantic_arithmetic_assign, 0)
5921 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5922 semantic_arithmetic_assign, 0)
5923 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5924 semantic_arithmetic_assign, 0)
5925 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5926 semantic_arithmetic_assign, 0)
5927 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5928 semantic_arithmetic_assign, 0)
5929 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5930 semantic_arithmetic_assign, 0)
5931 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5932 semantic_arithmetic_assign, 0)
5933 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5934 semantic_arithmetic_assign, 0)
5936 static expression_t *parse_sub_expression(unsigned precedence)
5938 if(token.type < 0) {
5939 return expected_expression_error();
5942 expression_parser_function_t *parser
5943 = &expression_parsers[token.type];
5944 source_position_t source_position = token.source_position;
5947 if(parser->parser != NULL) {
5948 left = parser->parser(parser->precedence);
5950 left = parse_primary_expression();
5952 assert(left != NULL);
5953 left->base.source_position = source_position;
5956 if(token.type < 0) {
5957 return expected_expression_error();
5960 parser = &expression_parsers[token.type];
5961 if(parser->infix_parser == NULL)
5963 if(parser->infix_precedence < precedence)
5966 left = parser->infix_parser(parser->infix_precedence, left);
5968 assert(left != NULL);
5969 assert(left->kind != EXPR_UNKNOWN);
5970 left->base.source_position = source_position;
5977 * Parse an expression.
5979 static expression_t *parse_expression(void)
5981 return parse_sub_expression(1);
5985 * Register a parser for a prefix-like operator with given precedence.
5987 * @param parser the parser function
5988 * @param token_type the token type of the prefix token
5989 * @param precedence the precedence of the operator
5991 static void register_expression_parser(parse_expression_function parser,
5992 int token_type, unsigned precedence)
5994 expression_parser_function_t *entry = &expression_parsers[token_type];
5996 if(entry->parser != NULL) {
5997 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5998 panic("trying to register multiple expression parsers for a token");
6000 entry->parser = parser;
6001 entry->precedence = precedence;
6005 * Register a parser for an infix operator with given precedence.
6007 * @param parser the parser function
6008 * @param token_type the token type of the infix operator
6009 * @param precedence the precedence of the operator
6011 static void register_infix_parser(parse_expression_infix_function parser,
6012 int token_type, unsigned precedence)
6014 expression_parser_function_t *entry = &expression_parsers[token_type];
6016 if(entry->infix_parser != NULL) {
6017 diagnosticf("for token '%k'\n", (token_type_t)token_type);
6018 panic("trying to register multiple infix expression parsers for a "
6021 entry->infix_parser = parser;
6022 entry->infix_precedence = precedence;
6026 * Initialize the expression parsers.
6028 static void init_expression_parsers(void)
6030 memset(&expression_parsers, 0, sizeof(expression_parsers));
6032 register_infix_parser(parse_array_expression, '[', 30);
6033 register_infix_parser(parse_call_expression, '(', 30);
6034 register_infix_parser(parse_select_expression, '.', 30);
6035 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
6036 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
6038 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
6041 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
6042 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
6043 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
6044 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
6045 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
6046 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
6047 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
6048 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
6049 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
6050 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
6051 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
6052 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
6053 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
6054 T_EXCLAMATIONMARKEQUAL, 13);
6055 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
6056 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
6057 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
6058 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
6059 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
6060 register_infix_parser(parse_conditional_expression, '?', 7);
6061 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
6062 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
6063 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
6064 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
6065 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
6066 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
6067 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
6068 T_LESSLESSEQUAL, 2);
6069 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
6070 T_GREATERGREATEREQUAL, 2);
6071 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
6073 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
6075 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
6078 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
6080 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
6081 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
6082 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
6083 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
6084 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
6085 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
6086 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
6088 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
6090 register_expression_parser(parse_sizeof, T_sizeof, 25);
6091 register_expression_parser(parse_alignof, T___alignof__, 25);
6092 register_expression_parser(parse_extension, T___extension__, 25);
6093 register_expression_parser(parse_builtin_classify_type,
6094 T___builtin_classify_type, 25);
6098 * Parse a asm statement constraints specification.
6100 static asm_constraint_t *parse_asm_constraints(void)
6102 asm_constraint_t *result = NULL;
6103 asm_constraint_t *last = NULL;
6105 while(token.type == T_STRING_LITERAL || token.type == '[') {
6106 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
6107 memset(constraint, 0, sizeof(constraint[0]));
6109 if(token.type == '[') {
6111 if(token.type != T_IDENTIFIER) {
6112 parse_error_expected("while parsing asm constraint",
6116 constraint->symbol = token.v.symbol;
6121 constraint->constraints = parse_string_literals();
6123 constraint->expression = parse_expression();
6127 last->next = constraint;
6129 result = constraint;
6133 if(token.type != ',')
6144 * Parse a asm statement clobber specification.
6146 static asm_clobber_t *parse_asm_clobbers(void)
6148 asm_clobber_t *result = NULL;
6149 asm_clobber_t *last = NULL;
6151 while(token.type == T_STRING_LITERAL) {
6152 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
6153 clobber->clobber = parse_string_literals();
6156 last->next = clobber;
6162 if(token.type != ',')
6171 * Parse an asm statement.
6173 static statement_t *parse_asm_statement(void)
6177 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
6178 statement->base.source_position = token.source_position;
6180 asm_statement_t *asm_statement = &statement->asms;
6182 if(token.type == T_volatile) {
6184 asm_statement->is_volatile = true;
6188 add_anchor_token(')');
6189 add_anchor_token(':');
6190 asm_statement->asm_text = parse_string_literals();
6192 if(token.type != ':') {
6193 rem_anchor_token(':');
6198 asm_statement->inputs = parse_asm_constraints();
6199 if(token.type != ':') {
6200 rem_anchor_token(':');
6205 asm_statement->outputs = parse_asm_constraints();
6206 if(token.type != ':') {
6207 rem_anchor_token(':');
6210 rem_anchor_token(':');
6213 asm_statement->clobbers = parse_asm_clobbers();
6216 rem_anchor_token(')');
6221 return create_invalid_statement();
6225 * Parse a case statement.
6227 static statement_t *parse_case_statement(void)
6231 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6233 statement->base.source_position = token.source_position;
6234 statement->case_label.expression = parse_expression();
6236 if (c_mode & _GNUC) {
6237 if (token.type == T_DOTDOTDOT) {
6239 statement->case_label.end_range = parse_expression();
6245 if (! is_constant_expression(statement->case_label.expression)) {
6246 errorf(statement->base.source_position,
6247 "case label does not reduce to an integer constant");
6249 /* TODO: check if the case label is already known */
6250 if (current_switch != NULL) {
6251 /* link all cases into the switch statement */
6252 if (current_switch->last_case == NULL) {
6253 current_switch->first_case =
6254 current_switch->last_case = &statement->case_label;
6256 current_switch->last_case->next = &statement->case_label;
6259 errorf(statement->base.source_position,
6260 "case label not within a switch statement");
6263 statement->case_label.statement = parse_statement();
6267 return create_invalid_statement();
6271 * Finds an existing default label of a switch statement.
6273 static case_label_statement_t *
6274 find_default_label(const switch_statement_t *statement)
6276 case_label_statement_t *label = statement->first_case;
6277 for ( ; label != NULL; label = label->next) {
6278 if (label->expression == NULL)
6285 * Parse a default statement.
6287 static statement_t *parse_default_statement(void)
6291 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
6293 statement->base.source_position = token.source_position;
6296 if (current_switch != NULL) {
6297 const case_label_statement_t *def_label = find_default_label(current_switch);
6298 if (def_label != NULL) {
6299 errorf(HERE, "multiple default labels in one switch");
6300 errorf(def_label->base.source_position,
6301 "this is the first default label");
6303 /* link all cases into the switch statement */
6304 if (current_switch->last_case == NULL) {
6305 current_switch->first_case =
6306 current_switch->last_case = &statement->case_label;
6308 current_switch->last_case->next = &statement->case_label;
6312 errorf(statement->base.source_position,
6313 "'default' label not within a switch statement");
6315 statement->case_label.statement = parse_statement();
6319 return create_invalid_statement();
6323 * Return the declaration for a given label symbol or create a new one.
6325 static declaration_t *get_label(symbol_t *symbol)
6327 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
6328 assert(current_function != NULL);
6329 /* if we found a label in the same function, then we already created the
6331 if(candidate != NULL
6332 && candidate->parent_scope == ¤t_function->scope) {
6336 /* otherwise we need to create a new one */
6337 declaration_t *const declaration = allocate_declaration_zero();
6338 declaration->namespc = NAMESPACE_LABEL;
6339 declaration->symbol = symbol;
6341 label_push(declaration);
6347 * Parse a label statement.
6349 static statement_t *parse_label_statement(void)
6351 assert(token.type == T_IDENTIFIER);
6352 symbol_t *symbol = token.v.symbol;
6355 declaration_t *label = get_label(symbol);
6357 /* if source position is already set then the label is defined twice,
6358 * otherwise it was just mentioned in a goto so far */
6359 if(label->source_position.input_name != NULL) {
6360 errorf(HERE, "duplicate label '%Y'", symbol);
6361 errorf(label->source_position, "previous definition of '%Y' was here",
6364 label->source_position = token.source_position;
6367 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
6369 statement->base.source_position = token.source_position;
6370 statement->label.label = label;
6374 if(token.type == '}') {
6375 /* TODO only warn? */
6377 warningf(HERE, "label at end of compound statement");
6378 statement->label.statement = create_empty_statement();
6380 errorf(HERE, "label at end of compound statement");
6381 statement->label.statement = create_invalid_statement();
6385 if (token.type == ';') {
6386 /* eat an empty statement here, to avoid the warning about an empty
6387 * after a label. label:; is commonly used to have a label before
6389 statement->label.statement = create_empty_statement();
6392 statement->label.statement = parse_statement();
6396 /* remember the labels's in a list for later checking */
6397 if (label_last == NULL) {
6398 label_first = &statement->label;
6400 label_last->next = &statement->label;
6402 label_last = &statement->label;
6408 * Parse an if statement.
6410 static statement_t *parse_if(void)
6414 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
6415 statement->base.source_position = token.source_position;
6418 add_anchor_token(')');
6419 statement->ifs.condition = parse_expression();
6420 rem_anchor_token(')');
6423 add_anchor_token(T_else);
6424 statement->ifs.true_statement = parse_statement();
6425 rem_anchor_token(T_else);
6427 if(token.type == T_else) {
6429 statement->ifs.false_statement = parse_statement();
6434 return create_invalid_statement();
6438 * Parse a switch statement.
6440 static statement_t *parse_switch(void)
6444 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
6445 statement->base.source_position = token.source_position;
6448 expression_t *const expr = parse_expression();
6449 type_t * type = skip_typeref(expr->base.type);
6450 if (is_type_integer(type)) {
6451 type = promote_integer(type);
6452 } else if (is_type_valid(type)) {
6453 errorf(expr->base.source_position,
6454 "switch quantity is not an integer, but '%T'", type);
6455 type = type_error_type;
6457 statement->switchs.expression = create_implicit_cast(expr, type);
6460 switch_statement_t *rem = current_switch;
6461 current_switch = &statement->switchs;
6462 statement->switchs.body = parse_statement();
6463 current_switch = rem;
6465 if (warning.switch_default
6466 && find_default_label(&statement->switchs) == NULL) {
6467 warningf(statement->base.source_position, "switch has no default case");
6472 return create_invalid_statement();
6475 static statement_t *parse_loop_body(statement_t *const loop)
6477 statement_t *const rem = current_loop;
6478 current_loop = loop;
6480 statement_t *const body = parse_statement();
6487 * Parse a while statement.
6489 static statement_t *parse_while(void)
6493 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
6494 statement->base.source_position = token.source_position;
6497 add_anchor_token(')');
6498 statement->whiles.condition = parse_expression();
6499 rem_anchor_token(')');
6502 statement->whiles.body = parse_loop_body(statement);
6506 return create_invalid_statement();
6510 * Parse a do statement.
6512 static statement_t *parse_do(void)
6516 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
6518 statement->base.source_position = token.source_position;
6520 add_anchor_token(T_while);
6521 statement->do_while.body = parse_loop_body(statement);
6522 rem_anchor_token(T_while);
6526 add_anchor_token(')');
6527 statement->do_while.condition = parse_expression();
6528 rem_anchor_token(')');
6534 return create_invalid_statement();
6538 * Parse a for statement.
6540 static statement_t *parse_for(void)
6544 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
6545 statement->base.source_position = token.source_position;
6547 int top = environment_top();
6548 scope_t *last_scope = scope;
6549 set_scope(&statement->fors.scope);
6552 add_anchor_token(')');
6554 if(token.type != ';') {
6555 if(is_declaration_specifier(&token, false)) {
6556 parse_declaration(record_declaration);
6558 expression_t *const init = parse_expression();
6559 statement->fors.initialisation = init;
6560 if (warning.unused_value && !expression_has_effect(init)) {
6561 warningf(init->base.source_position,
6562 "initialisation of 'for'-statement has no effect");
6570 if(token.type != ';') {
6571 statement->fors.condition = parse_expression();
6574 if(token.type != ')') {
6575 expression_t *const step = parse_expression();
6576 statement->fors.step = step;
6577 if (warning.unused_value && !expression_has_effect(step)) {
6578 warningf(step->base.source_position,
6579 "step of 'for'-statement has no effect");
6582 rem_anchor_token(')');
6584 statement->fors.body = parse_loop_body(statement);
6586 assert(scope == &statement->fors.scope);
6587 set_scope(last_scope);
6588 environment_pop_to(top);
6593 rem_anchor_token(')');
6594 assert(scope == &statement->fors.scope);
6595 set_scope(last_scope);
6596 environment_pop_to(top);
6598 return create_invalid_statement();
6602 * Parse a goto statement.
6604 static statement_t *parse_goto(void)
6608 if(token.type != T_IDENTIFIER) {
6609 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
6613 symbol_t *symbol = token.v.symbol;
6616 declaration_t *label = get_label(symbol);
6618 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
6619 statement->base.source_position = token.source_position;
6621 statement->gotos.label = label;
6623 /* remember the goto's in a list for later checking */
6624 if (goto_last == NULL) {
6625 goto_first = &statement->gotos;
6627 goto_last->next = &statement->gotos;
6629 goto_last = &statement->gotos;
6635 return create_invalid_statement();
6639 * Parse a continue statement.
6641 static statement_t *parse_continue(void)
6643 statement_t *statement;
6644 if (current_loop == NULL) {
6645 errorf(HERE, "continue statement not within loop");
6648 statement = allocate_statement_zero(STATEMENT_CONTINUE);
6650 statement->base.source_position = token.source_position;
6658 return create_invalid_statement();
6662 * Parse a break statement.
6664 static statement_t *parse_break(void)
6666 statement_t *statement;
6667 if (current_switch == NULL && current_loop == NULL) {
6668 errorf(HERE, "break statement not within loop or switch");
6671 statement = allocate_statement_zero(STATEMENT_BREAK);
6673 statement->base.source_position = token.source_position;
6681 return create_invalid_statement();
6685 * Check if a given declaration represents a local variable.
6687 static bool is_local_var_declaration(const declaration_t *declaration) {
6688 switch ((storage_class_tag_t) declaration->storage_class) {
6689 case STORAGE_CLASS_AUTO:
6690 case STORAGE_CLASS_REGISTER: {
6691 const type_t *type = skip_typeref(declaration->type);
6692 if(is_type_function(type)) {
6704 * Check if a given declaration represents a variable.
6706 static bool is_var_declaration(const declaration_t *declaration) {
6707 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF)
6710 const type_t *type = skip_typeref(declaration->type);
6711 return !is_type_function(type);
6715 * Check if a given expression represents a local variable.
6717 static bool is_local_variable(const expression_t *expression)
6719 if (expression->base.kind != EXPR_REFERENCE) {
6722 const declaration_t *declaration = expression->reference.declaration;
6723 return is_local_var_declaration(declaration);
6727 * Check if a given expression represents a local variable and
6728 * return its declaration then, else return NULL.
6730 declaration_t *expr_is_variable(const expression_t *expression)
6732 if (expression->base.kind != EXPR_REFERENCE) {
6735 declaration_t *declaration = expression->reference.declaration;
6736 if (is_var_declaration(declaration))
6742 * Parse a return statement.
6744 static statement_t *parse_return(void)
6748 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6749 statement->base.source_position = token.source_position;
6751 expression_t *return_value = NULL;
6752 if(token.type != ';') {
6753 return_value = parse_expression();
6757 const type_t *const func_type = current_function->type;
6758 assert(is_type_function(func_type));
6759 type_t *const return_type = skip_typeref(func_type->function.return_type);
6761 if(return_value != NULL) {
6762 type_t *return_value_type = skip_typeref(return_value->base.type);
6764 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6765 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6766 warningf(statement->base.source_position,
6767 "'return' with a value, in function returning void");
6768 return_value = NULL;
6770 type_t *const res_type = semantic_assign(return_type,
6771 return_value, "'return'");
6772 if (res_type == NULL) {
6773 errorf(statement->base.source_position,
6774 "cannot return something of type '%T' in function returning '%T'",
6775 return_value->base.type, return_type);
6777 return_value = create_implicit_cast(return_value, res_type);
6780 /* check for returning address of a local var */
6781 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6782 const expression_t *expression = return_value->unary.value;
6783 if (is_local_variable(expression)) {
6784 warningf(statement->base.source_position,
6785 "function returns address of local variable");
6789 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6790 warningf(statement->base.source_position,
6791 "'return' without value, in function returning non-void");
6794 statement->returns.value = return_value;
6798 return create_invalid_statement();
6802 * Parse a declaration statement.
6804 static statement_t *parse_declaration_statement(void)
6806 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6808 statement->base.source_position = token.source_position;
6810 declaration_t *before = last_declaration;
6811 parse_declaration(record_declaration);
6813 if(before == NULL) {
6814 statement->declaration.declarations_begin = scope->declarations;
6816 statement->declaration.declarations_begin = before->next;
6818 statement->declaration.declarations_end = last_declaration;
6824 * Parse an expression statement, ie. expr ';'.
6826 static statement_t *parse_expression_statement(void)
6828 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6830 statement->base.source_position = token.source_position;
6831 expression_t *const expr = parse_expression();
6832 statement->expression.expression = expr;
6834 if (warning.unused_value && !expression_has_effect(expr)) {
6835 warningf(expr->base.source_position, "statement has no effect");
6842 return create_invalid_statement();
6846 * Parse a statement.
6848 static statement_t *parse_statement(void)
6850 statement_t *statement = NULL;
6852 /* declaration or statement */
6853 add_anchor_token(';');
6854 switch(token.type) {
6856 statement = parse_asm_statement();
6860 statement = parse_case_statement();
6864 statement = parse_default_statement();
6868 statement = parse_compound_statement();
6872 statement = parse_if();
6876 statement = parse_switch();
6880 statement = parse_while();
6884 statement = parse_do();
6888 statement = parse_for();
6892 statement = parse_goto();
6896 statement = parse_continue();
6900 statement = parse_break();
6904 statement = parse_return();
6908 if(warning.empty_statement) {
6909 warningf(HERE, "statement is empty");
6911 statement = create_empty_statement();
6916 if(look_ahead(1)->type == ':') {
6917 statement = parse_label_statement();
6921 if(is_typedef_symbol(token.v.symbol)) {
6922 statement = parse_declaration_statement();
6926 statement = parse_expression_statement();
6929 case T___extension__:
6930 /* this can be a prefix to a declaration or an expression statement */
6931 /* we simply eat it now and parse the rest with tail recursion */
6934 } while(token.type == T___extension__);
6935 statement = parse_statement();
6939 statement = parse_declaration_statement();
6943 statement = parse_expression_statement();
6946 rem_anchor_token(';');
6948 assert(statement != NULL
6949 && statement->base.source_position.input_name != NULL);
6955 * Parse a compound statement.
6957 static statement_t *parse_compound_statement(void)
6959 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6961 statement->base.source_position = token.source_position;
6964 add_anchor_token('}');
6966 int top = environment_top();
6967 scope_t *last_scope = scope;
6968 set_scope(&statement->compound.scope);
6970 statement_t *last_statement = NULL;
6972 while(token.type != '}' && token.type != T_EOF) {
6973 statement_t *sub_statement = parse_statement();
6974 if(is_invalid_statement(sub_statement)) {
6975 /* an error occurred. if we are at an anchor, return */
6981 if(last_statement != NULL) {
6982 last_statement->base.next = sub_statement;
6984 statement->compound.statements = sub_statement;
6987 while(sub_statement->base.next != NULL)
6988 sub_statement = sub_statement->base.next;
6990 last_statement = sub_statement;
6993 if(token.type == '}') {
6996 errorf(statement->base.source_position,
6997 "end of file while looking for closing '}'");
7001 rem_anchor_token('}');
7002 assert(scope == &statement->compound.scope);
7003 set_scope(last_scope);
7004 environment_pop_to(top);
7010 * Initialize builtin types.
7012 static void initialize_builtin_types(void)
7014 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
7015 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
7016 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
7017 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
7018 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
7019 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
7020 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
7021 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
7023 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
7024 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
7025 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
7026 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
7030 * Check for unused global static functions and variables
7032 static void check_unused_globals(void)
7034 if (!warning.unused_function && !warning.unused_variable)
7037 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
7038 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
7041 type_t *const type = decl->type;
7043 if (is_type_function(skip_typeref(type))) {
7044 if (!warning.unused_function || decl->is_inline)
7047 s = (decl->init.statement != NULL ? "defined" : "declared");
7049 if (!warning.unused_variable)
7055 warningf(decl->source_position, "'%#T' %s but not used",
7056 type, decl->symbol, s);
7061 * Parse a translation unit.
7063 static translation_unit_t *parse_translation_unit(void)
7065 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
7067 assert(global_scope == NULL);
7068 global_scope = &unit->scope;
7070 assert(scope == NULL);
7071 set_scope(&unit->scope);
7073 initialize_builtin_types();
7075 while(token.type != T_EOF) {
7076 if (token.type == ';') {
7077 /* TODO error in strict mode */
7078 warningf(HERE, "stray ';' outside of function");
7081 parse_external_declaration();
7085 assert(scope == &unit->scope);
7087 last_declaration = NULL;
7089 assert(global_scope == &unit->scope);
7090 check_unused_globals();
7091 global_scope = NULL;
7099 * @return the translation unit or NULL if errors occurred.
7101 translation_unit_t *parse(void)
7103 environment_stack = NEW_ARR_F(stack_entry_t, 0);
7104 label_stack = NEW_ARR_F(stack_entry_t, 0);
7105 diagnostic_count = 0;
7109 type_set_output(stderr);
7110 ast_set_output(stderr);
7112 lookahead_bufpos = 0;
7113 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
7116 translation_unit_t *unit = parse_translation_unit();
7118 DEL_ARR_F(environment_stack);
7119 DEL_ARR_F(label_stack);
7125 * Initialize the parser.
7127 void init_parser(void)
7130 /* add predefined symbols for extended-decl-modifier */
7131 sym_align = symbol_table_insert("align");
7132 sym_allocate = symbol_table_insert("allocate");
7133 sym_dllimport = symbol_table_insert("dllimport");
7134 sym_dllexport = symbol_table_insert("dllexport");
7135 sym_naked = symbol_table_insert("naked");
7136 sym_noinline = symbol_table_insert("noinline");
7137 sym_noreturn = symbol_table_insert("noreturn");
7138 sym_nothrow = symbol_table_insert("nothrow");
7139 sym_novtable = symbol_table_insert("novtable");
7140 sym_property = symbol_table_insert("property");
7141 sym_get = symbol_table_insert("get");
7142 sym_put = symbol_table_insert("put");
7143 sym_selectany = symbol_table_insert("selectany");
7144 sym_thread = symbol_table_insert("thread");
7145 sym_uuid = symbol_table_insert("uuid");
7146 sym_deprecated = symbol_table_insert("deprecated");
7147 sym_restrict = symbol_table_insert("restrict");
7148 sym_noalias = symbol_table_insert("noalias");
7150 memset(token_anchor_set, 0, sizeof(token_anchor_set));
7152 init_expression_parsers();
7153 obstack_init(&temp_obst);
7155 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
7156 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
7160 * Terminate the parser.
7162 void exit_parser(void)
7164 obstack_free(&temp_obst, NULL);