7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(declaration_t *compound_declaration);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_CHAR_CONST] = sizeof(const_expression_t),
202 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
203 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
204 [EXPR_CALL] = sizeof(call_expression_t),
205 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
206 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
207 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
208 [EXPR_SELECT] = sizeof(select_expression_t),
209 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
210 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
211 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
212 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
213 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
214 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
215 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
216 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
217 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
218 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
219 [EXPR_VA_START] = sizeof(va_start_expression_t),
220 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
221 [EXPR_STATEMENT] = sizeof(statement_expression_t),
223 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
224 return sizes[EXPR_UNARY_FIRST];
226 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
227 return sizes[EXPR_BINARY_FIRST];
229 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
230 assert(sizes[kind] != 0);
235 * Allocate an expression node of given kind and initialize all
238 static expression_t *allocate_expression_zero(expression_kind_t kind)
240 size_t size = get_expression_struct_size(kind);
241 expression_t *res = allocate_ast_zero(size);
243 res->base.kind = kind;
244 res->base.type = type_error_type;
249 * Returns the size of a type node.
251 * @param kind the type kind
253 static size_t get_type_struct_size(type_kind_t kind)
255 static const size_t sizes[] = {
256 [TYPE_ATOMIC] = sizeof(atomic_type_t),
257 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
258 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
259 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
260 [TYPE_ENUM] = sizeof(enum_type_t),
261 [TYPE_FUNCTION] = sizeof(function_type_t),
262 [TYPE_POINTER] = sizeof(pointer_type_t),
263 [TYPE_ARRAY] = sizeof(array_type_t),
264 [TYPE_BUILTIN] = sizeof(builtin_type_t),
265 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
266 [TYPE_TYPEOF] = sizeof(typeof_type_t),
268 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
269 assert(kind <= TYPE_TYPEOF);
270 assert(sizes[kind] != 0);
275 * Allocate a type node of given kind and initialize all
278 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
280 size_t size = get_type_struct_size(kind);
281 type_t *res = obstack_alloc(type_obst, size);
282 memset(res, 0, size);
284 res->base.kind = kind;
285 res->base.source_position = source_position;
290 * Returns the size of an initializer node.
292 * @param kind the initializer kind
294 static size_t get_initializer_size(initializer_kind_t kind)
296 static const size_t sizes[] = {
297 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
298 [INITIALIZER_STRING] = sizeof(initializer_string_t),
299 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
300 [INITIALIZER_LIST] = sizeof(initializer_list_t)
302 assert(kind < sizeof(sizes) / sizeof(*sizes));
303 assert(sizes[kind] != 0);
308 * Allocate an initializer node of given kind and initialize all
311 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
313 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
320 * Free a type from the type obstack.
322 static void free_type(void *type)
324 obstack_free(type_obst, type);
328 * Returns the index of the top element of the environment stack.
330 static size_t environment_top(void)
332 return ARR_LEN(environment_stack);
336 * Returns the index of the top element of the label stack.
338 static size_t label_top(void)
340 return ARR_LEN(label_stack);
345 * Return the next token.
347 static inline void next_token(void)
349 token = lookahead_buffer[lookahead_bufpos];
350 lookahead_buffer[lookahead_bufpos] = lexer_token;
353 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
356 print_token(stderr, &token);
357 fprintf(stderr, "\n");
362 * Return the next token with a given lookahead.
364 static inline const token_t *look_ahead(int num)
366 assert(num > 0 && num <= MAX_LOOKAHEAD);
367 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
368 return &lookahead_buffer[pos];
371 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
374 * Report a parse error because an expected token was not found.
376 static void parse_error_expected(const char *message, ...)
378 if(message != NULL) {
379 errorf(HERE, "%s", message);
382 va_start(ap, message);
383 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
388 * Report a type error.
390 static void type_error(const char *msg, const source_position_t source_position,
393 errorf(source_position, "%s, but found type '%T'", msg, type);
397 * Report an incompatible type.
399 static void type_error_incompatible(const char *msg,
400 const source_position_t source_position, type_t *type1, type_t *type2)
402 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
406 * Eat an complete block, ie. '{ ... }'.
408 static void eat_block(void)
410 if(token.type == '{')
413 while(token.type != '}') {
414 if(token.type == T_EOF)
416 if(token.type == '{') {
426 * Eat a statement until an ';' token.
428 static void eat_statement(void)
430 while(token.type != ';') {
431 if(token.type == T_EOF)
433 if(token.type == '}')
435 if(token.type == '{') {
445 * Eat a parenthesed term, ie. '( ... )'.
447 static void eat_paren(void)
449 if(token.type == '(')
452 while(token.type != ')') {
453 if(token.type == T_EOF)
455 if(token.type == ')' || token.type == ';' || token.type == '}') {
458 if(token.type == '(') {
462 if(token.type == '{') {
471 #define expect(expected) \
472 if(UNLIKELY(token.type != (expected))) { \
473 parse_error_expected(NULL, (expected), 0); \
479 #define expect_block(expected) \
480 if(UNLIKELY(token.type != (expected))) { \
481 parse_error_expected(NULL, (expected), 0); \
487 #define expect_void(expected) \
488 if(UNLIKELY(token.type != (expected))) { \
489 parse_error_expected(NULL, (expected), 0); \
495 static void set_scope(scope_t *new_scope)
499 last_declaration = new_scope->declarations;
500 if(last_declaration != NULL) {
501 while(last_declaration->next != NULL) {
502 last_declaration = last_declaration->next;
508 * Search a symbol in a given namespace and returns its declaration or
509 * NULL if this symbol was not found.
511 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
513 declaration_t *declaration = symbol->declaration;
514 for( ; declaration != NULL; declaration = declaration->symbol_next) {
515 if(declaration->namespc == namespc)
523 * pushs an environment_entry on the environment stack and links the
524 * corresponding symbol to the new entry
526 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
528 symbol_t *symbol = declaration->symbol;
529 namespace_t namespc = (namespace_t) declaration->namespc;
531 /* replace/add declaration into declaration list of the symbol */
532 declaration_t *iter = symbol->declaration;
534 symbol->declaration = declaration;
536 declaration_t *iter_last = NULL;
537 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
538 /* replace an entry? */
539 if(iter->namespc == namespc) {
540 if(iter_last == NULL) {
541 symbol->declaration = declaration;
543 iter_last->symbol_next = declaration;
545 declaration->symbol_next = iter->symbol_next;
550 assert(iter_last->symbol_next == NULL);
551 iter_last->symbol_next = declaration;
555 /* remember old declaration */
557 entry.symbol = symbol;
558 entry.old_declaration = iter;
559 entry.namespc = (unsigned short) namespc;
560 ARR_APP1(stack_entry_t, *stack_ptr, entry);
563 static void environment_push(declaration_t *declaration)
565 assert(declaration->source_position.input_name != NULL);
566 assert(declaration->parent_scope != NULL);
567 stack_push(&environment_stack, declaration);
570 static void label_push(declaration_t *declaration)
572 declaration->parent_scope = ¤t_function->scope;
573 stack_push(&label_stack, declaration);
577 * pops symbols from the environment stack until @p new_top is the top element
579 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
581 stack_entry_t *stack = *stack_ptr;
582 size_t top = ARR_LEN(stack);
585 assert(new_top <= top);
589 for(i = top; i > new_top; --i) {
590 stack_entry_t *entry = &stack[i - 1];
592 declaration_t *old_declaration = entry->old_declaration;
593 symbol_t *symbol = entry->symbol;
594 namespace_t namespc = (namespace_t)entry->namespc;
596 /* replace/remove declaration */
597 declaration_t *declaration = symbol->declaration;
598 assert(declaration != NULL);
599 if(declaration->namespc == namespc) {
600 if(old_declaration == NULL) {
601 symbol->declaration = declaration->symbol_next;
603 symbol->declaration = old_declaration;
606 declaration_t *iter_last = declaration;
607 declaration_t *iter = declaration->symbol_next;
608 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
609 /* replace an entry? */
610 if(iter->namespc == namespc) {
611 assert(iter_last != NULL);
612 iter_last->symbol_next = old_declaration;
613 if(old_declaration != NULL) {
614 old_declaration->symbol_next = iter->symbol_next;
619 assert(iter != NULL);
623 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
626 static void environment_pop_to(size_t new_top)
628 stack_pop_to(&environment_stack, new_top);
631 static void label_pop_to(size_t new_top)
633 stack_pop_to(&label_stack, new_top);
637 static int get_rank(const type_t *type)
639 assert(!is_typeref(type));
640 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
641 * and esp. footnote 108). However we can't fold constants (yet), so we
642 * can't decide whether unsigned int is possible, while int always works.
643 * (unsigned int would be preferable when possible... for stuff like
644 * struct { enum { ... } bla : 4; } ) */
645 if(type->kind == TYPE_ENUM)
646 return ATOMIC_TYPE_INT;
648 assert(type->kind == TYPE_ATOMIC);
649 return type->atomic.akind;
652 static type_t *promote_integer(type_t *type)
654 if(type->kind == TYPE_BITFIELD)
655 type = type->bitfield.base;
657 if(get_rank(type) < ATOMIC_TYPE_INT)
664 * Create a cast expression.
666 * @param expression the expression to cast
667 * @param dest_type the destination type
669 static expression_t *create_cast_expression(expression_t *expression,
672 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
674 cast->unary.value = expression;
675 cast->base.type = dest_type;
681 * Check if a given expression represents the 0 pointer constant.
683 static bool is_null_pointer_constant(const expression_t *expression)
685 /* skip void* cast */
686 if(expression->kind == EXPR_UNARY_CAST
687 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
688 expression = expression->unary.value;
691 /* TODO: not correct yet, should be any constant integer expression
692 * which evaluates to 0 */
693 if (expression->kind != EXPR_CONST)
696 type_t *const type = skip_typeref(expression->base.type);
697 if (!is_type_integer(type))
700 return expression->conste.v.int_value == 0;
704 * Create an implicit cast expression.
706 * @param expression the expression to cast
707 * @param dest_type the destination type
709 static expression_t *create_implicit_cast(expression_t *expression,
712 type_t *const source_type = expression->base.type;
714 if (source_type == dest_type)
717 return create_cast_expression(expression, dest_type);
720 /** Implements the rules from § 6.5.16.1 */
721 static type_t *semantic_assign(type_t *orig_type_left,
722 const expression_t *const right,
725 type_t *const orig_type_right = right->base.type;
726 type_t *const type_left = skip_typeref(orig_type_left);
727 type_t *const type_right = skip_typeref(orig_type_right);
729 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
730 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
731 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
732 && is_type_pointer(type_right))) {
733 return orig_type_left;
736 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
737 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
738 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
740 /* the left type has all qualifiers from the right type */
741 unsigned missing_qualifiers
742 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
743 if(missing_qualifiers != 0) {
744 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
745 return orig_type_left;
748 points_to_left = get_unqualified_type(points_to_left);
749 points_to_right = get_unqualified_type(points_to_right);
751 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
752 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
753 return orig_type_left;
756 if (!types_compatible(points_to_left, points_to_right)) {
757 warningf(right->base.source_position,
758 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
759 orig_type_left, context, right, orig_type_right);
762 return orig_type_left;
765 if (is_type_compound(type_left) && is_type_compound(type_right)) {
766 type_t *const unqual_type_left = get_unqualified_type(type_left);
767 type_t *const unqual_type_right = get_unqualified_type(type_right);
768 if (types_compatible(unqual_type_left, unqual_type_right)) {
769 return orig_type_left;
773 if (!is_type_valid(type_left))
776 if (!is_type_valid(type_right))
777 return orig_type_right;
782 static expression_t *parse_constant_expression(void)
784 /* start parsing at precedence 7 (conditional expression) */
785 expression_t *result = parse_sub_expression(7);
787 if(!is_constant_expression(result)) {
788 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
794 static expression_t *parse_assignment_expression(void)
796 /* start parsing at precedence 2 (assignment expression) */
797 return parse_sub_expression(2);
800 static type_t *make_global_typedef(const char *name, type_t *type)
802 symbol_t *const symbol = symbol_table_insert(name);
804 declaration_t *const declaration = allocate_declaration_zero();
805 declaration->namespc = NAMESPACE_NORMAL;
806 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
807 declaration->type = type;
808 declaration->symbol = symbol;
809 declaration->source_position = builtin_source_position;
811 record_declaration(declaration);
813 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
814 typedef_type->typedeft.declaration = declaration;
819 static string_t parse_string_literals(void)
821 assert(token.type == T_STRING_LITERAL);
822 string_t result = token.v.string;
826 while (token.type == T_STRING_LITERAL) {
827 result = concat_strings(&result, &token.v.string);
834 static void parse_attributes(void)
838 case T___attribute__: {
846 errorf(HERE, "EOF while parsing attribute");
865 if(token.type != T_STRING_LITERAL) {
866 parse_error_expected("while parsing assembler attribute",
871 parse_string_literals();
876 goto attributes_finished;
885 static designator_t *parse_designation(void)
887 if(token.type != '[' && token.type != '.')
890 designator_t *result = NULL;
891 designator_t *last = NULL;
894 designator_t *designator;
897 designator = allocate_ast_zero(sizeof(designator[0]));
899 designator->array_access = parse_constant_expression();
903 designator = allocate_ast_zero(sizeof(designator[0]));
905 if(token.type != T_IDENTIFIER) {
906 parse_error_expected("while parsing designator",
910 designator->symbol = token.v.symbol;
918 assert(designator != NULL);
920 last->next = designator;
929 static initializer_t *initializer_from_string(array_type_t *type,
930 const string_t *const string)
932 /* TODO: check len vs. size of array type */
935 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
936 initializer->string.string = *string;
941 static initializer_t *initializer_from_wide_string(array_type_t *const type,
942 wide_string_t *const string)
944 /* TODO: check len vs. size of array type */
947 initializer_t *const initializer =
948 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
949 initializer->wide_string.string = *string;
954 static initializer_t *initializer_from_expression(type_t *type,
955 expression_t *expression)
957 /* TODO check that expression is a constant expression */
959 /* § 6.7.8.14/15 char array may be initialized by string literals */
960 type_t *const expr_type = expression->base.type;
961 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
962 array_type_t *const array_type = &type->array;
963 type_t *const element_type = skip_typeref(array_type->element_type);
965 if (element_type->kind == TYPE_ATOMIC) {
966 switch (expression->kind) {
967 case EXPR_STRING_LITERAL:
968 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
969 return initializer_from_string(array_type,
970 &expression->string.value);
973 case EXPR_WIDE_STRING_LITERAL: {
974 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
975 if (get_unqualified_type(element_type) == bare_wchar_type) {
976 return initializer_from_wide_string(array_type,
977 &expression->wide_string.value);
987 type_t *const res_type = semantic_assign(type, expression, "initializer");
988 if (res_type == NULL)
991 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
992 result->value.value = create_implicit_cast(expression, res_type);
997 static initializer_t *parse_sub_initializer(type_t *type,
998 expression_t *expression);
1000 static initializer_t *parse_sub_initializer_elem(type_t *type)
1002 if(token.type == '{') {
1003 return parse_sub_initializer(type, NULL);
1006 expression_t *expression = parse_assignment_expression();
1007 return parse_sub_initializer(type, expression);
1010 static bool had_initializer_brace_warning;
1012 static void skip_designator(void)
1015 if(token.type == '.') {
1017 if(token.type == T_IDENTIFIER)
1019 } else if(token.type == '[') {
1021 parse_constant_expression();
1022 if(token.type == ']')
1030 static initializer_t *parse_sub_initializer(type_t *type,
1031 expression_t *expression)
1033 if(is_type_scalar(type)) {
1034 /* there might be extra {} hierarchies */
1035 if(token.type == '{') {
1037 if(!had_initializer_brace_warning) {
1038 warningf(HERE, "braces around scalar initializer");
1039 had_initializer_brace_warning = true;
1041 initializer_t *result = parse_sub_initializer(type, NULL);
1042 if(token.type == ',') {
1044 /* TODO: warn about excessive elements */
1050 if(expression == NULL) {
1051 expression = parse_assignment_expression();
1053 return initializer_from_expression(type, expression);
1056 /* does the expression match the currently looked at object to initialize */
1057 if(expression != NULL) {
1058 initializer_t *result = initializer_from_expression(type, expression);
1063 bool read_paren = false;
1064 if(token.type == '{') {
1069 /* descend into subtype */
1070 initializer_t *result = NULL;
1071 initializer_t **elems;
1072 if(is_type_array(type)) {
1073 if(token.type == '.') {
1075 "compound designator in initializer for array type '%T'",
1080 type_t *const element_type = skip_typeref(type->array.element_type);
1083 had_initializer_brace_warning = false;
1085 if(token.type == '{') {
1086 sub = parse_sub_initializer(element_type, NULL);
1088 if(expression == NULL) {
1089 expression = parse_assignment_expression();
1091 /* 6.7.8.14 + 15: we can have an optional {} around the string
1093 if(read_paren && (expression->kind == EXPR_STRING_LITERAL
1094 || expression->kind == EXPR_WIDE_STRING_LITERAL)) {
1095 initializer_t *result
1096 = initializer_from_expression(type, expression);
1097 if(result != NULL) {
1104 sub = parse_sub_initializer(element_type, expression);
1107 /* didn't match the subtypes -> try the parent type */
1109 assert(!read_paren);
1113 elems = NEW_ARR_F(initializer_t*, 0);
1114 ARR_APP1(initializer_t*, elems, sub);
1117 if(token.type == '}')
1120 if(token.type == '}')
1123 sub = parse_sub_initializer_elem(element_type);
1125 /* TODO error, do nicer cleanup */
1126 errorf(HERE, "member initializer didn't match");
1130 ARR_APP1(initializer_t*, elems, sub);
1133 assert(is_type_compound(type));
1134 scope_t *const scope = &type->compound.declaration->scope;
1136 if(token.type == '[') {
1138 "array designator in initializer for compound type '%T'",
1143 declaration_t *first = scope->declarations;
1146 type_t *first_type = first->type;
1147 first_type = skip_typeref(first_type);
1150 had_initializer_brace_warning = false;
1151 if(expression == NULL) {
1152 sub = parse_sub_initializer_elem(first_type);
1154 sub = parse_sub_initializer(first_type, expression);
1157 /* didn't match the subtypes -> try our parent type */
1159 assert(!read_paren);
1163 elems = NEW_ARR_F(initializer_t*, 0);
1164 ARR_APP1(initializer_t*, elems, sub);
1166 declaration_t *iter = first->next;
1167 for( ; iter != NULL; iter = iter->next) {
1168 if(iter->symbol == NULL)
1170 if(iter->namespc != NAMESPACE_NORMAL)
1173 if(token.type == '}')
1176 if(token.type == '}')
1179 type_t *iter_type = iter->type;
1180 iter_type = skip_typeref(iter_type);
1182 sub = parse_sub_initializer_elem(iter_type);
1184 /* TODO error, do nicer cleanup */
1185 errorf(HERE, "member initializer didn't match");
1189 ARR_APP1(initializer_t*, elems, sub);
1193 int len = ARR_LEN(elems);
1194 size_t elems_size = sizeof(initializer_t*) * len;
1196 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1198 init->initializer.kind = INITIALIZER_LIST;
1200 memcpy(init->initializers, elems, elems_size);
1203 result = (initializer_t*) init;
1206 if(token.type == ',')
1213 static initializer_t *parse_initializer(type_t *const orig_type)
1215 initializer_t *result;
1217 type_t *const type = skip_typeref(orig_type);
1219 if(token.type != '{') {
1220 expression_t *expression = parse_assignment_expression();
1221 initializer_t *initializer = initializer_from_expression(type, expression);
1222 if(initializer == NULL) {
1224 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1225 expression, expression->base.type, orig_type);
1230 if(is_type_scalar(type)) {
1234 expression_t *expression = parse_assignment_expression();
1235 result = initializer_from_expression(type, expression);
1237 if(token.type == ',')
1243 result = parse_sub_initializer(type, NULL);
1249 static declaration_t *append_declaration(declaration_t *declaration);
1251 static declaration_t *parse_compound_type_specifier(bool is_struct)
1259 symbol_t *symbol = NULL;
1260 declaration_t *declaration = NULL;
1262 if (token.type == T___attribute__) {
1267 if(token.type == T_IDENTIFIER) {
1268 symbol = token.v.symbol;
1272 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1274 declaration = get_declaration(symbol, NAMESPACE_UNION);
1276 } else if(token.type != '{') {
1278 parse_error_expected("while parsing struct type specifier",
1279 T_IDENTIFIER, '{', 0);
1281 parse_error_expected("while parsing union type specifier",
1282 T_IDENTIFIER, '{', 0);
1288 if(declaration == NULL) {
1289 declaration = allocate_declaration_zero();
1290 declaration->namespc =
1291 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1292 declaration->source_position = token.source_position;
1293 declaration->symbol = symbol;
1294 declaration->parent_scope = scope;
1295 if (symbol != NULL) {
1296 environment_push(declaration);
1298 append_declaration(declaration);
1301 if(token.type == '{') {
1302 if(declaration->init.is_defined) {
1303 assert(symbol != NULL);
1304 errorf(HERE, "multiple definitions of '%s %Y'",
1305 is_struct ? "struct" : "union", symbol);
1306 declaration->scope.declarations = NULL;
1308 declaration->init.is_defined = true;
1310 parse_compound_type_entries(declaration);
1317 static void parse_enum_entries(type_t *const enum_type)
1321 if(token.type == '}') {
1323 errorf(HERE, "empty enum not allowed");
1328 if(token.type != T_IDENTIFIER) {
1329 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1334 declaration_t *const entry = allocate_declaration_zero();
1335 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1336 entry->type = enum_type;
1337 entry->symbol = token.v.symbol;
1338 entry->source_position = token.source_position;
1341 if(token.type == '=') {
1343 entry->init.enum_value = parse_constant_expression();
1348 record_declaration(entry);
1350 if(token.type != ',')
1353 } while(token.type != '}');
1358 static type_t *parse_enum_specifier(void)
1362 declaration_t *declaration;
1365 if(token.type == T_IDENTIFIER) {
1366 symbol = token.v.symbol;
1369 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1370 } else if(token.type != '{') {
1371 parse_error_expected("while parsing enum type specifier",
1372 T_IDENTIFIER, '{', 0);
1379 if(declaration == NULL) {
1380 declaration = allocate_declaration_zero();
1381 declaration->namespc = NAMESPACE_ENUM;
1382 declaration->source_position = token.source_position;
1383 declaration->symbol = symbol;
1384 declaration->parent_scope = scope;
1387 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1388 type->enumt.declaration = declaration;
1390 if(token.type == '{') {
1391 if(declaration->init.is_defined) {
1392 errorf(HERE, "multiple definitions of enum %Y", symbol);
1394 if (symbol != NULL) {
1395 environment_push(declaration);
1397 append_declaration(declaration);
1398 declaration->init.is_defined = 1;
1400 parse_enum_entries(type);
1408 * if a symbol is a typedef to another type, return true
1410 static bool is_typedef_symbol(symbol_t *symbol)
1412 const declaration_t *const declaration =
1413 get_declaration(symbol, NAMESPACE_NORMAL);
1415 declaration != NULL &&
1416 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1419 static type_t *parse_typeof(void)
1427 expression_t *expression = NULL;
1430 switch(token.type) {
1431 case T___extension__:
1432 /* this can be a prefix to a typename or an expression */
1433 /* we simply eat it now. */
1436 } while(token.type == T___extension__);
1440 if(is_typedef_symbol(token.v.symbol)) {
1441 type = parse_typename();
1443 expression = parse_expression();
1444 type = expression->base.type;
1449 type = parse_typename();
1453 expression = parse_expression();
1454 type = expression->base.type;
1460 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1461 typeof_type->typeoft.expression = expression;
1462 typeof_type->typeoft.typeof_type = type;
1468 SPECIFIER_SIGNED = 1 << 0,
1469 SPECIFIER_UNSIGNED = 1 << 1,
1470 SPECIFIER_LONG = 1 << 2,
1471 SPECIFIER_INT = 1 << 3,
1472 SPECIFIER_DOUBLE = 1 << 4,
1473 SPECIFIER_CHAR = 1 << 5,
1474 SPECIFIER_SHORT = 1 << 6,
1475 SPECIFIER_LONG_LONG = 1 << 7,
1476 SPECIFIER_FLOAT = 1 << 8,
1477 SPECIFIER_BOOL = 1 << 9,
1478 SPECIFIER_VOID = 1 << 10,
1479 #ifdef PROVIDE_COMPLEX
1480 SPECIFIER_COMPLEX = 1 << 11,
1481 SPECIFIER_IMAGINARY = 1 << 12,
1485 static type_t *create_builtin_type(symbol_t *const symbol,
1486 type_t *const real_type)
1488 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1489 type->builtin.symbol = symbol;
1490 type->builtin.real_type = real_type;
1492 type_t *result = typehash_insert(type);
1493 if (type != result) {
1500 static type_t *get_typedef_type(symbol_t *symbol)
1502 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1503 if(declaration == NULL
1504 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1507 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1508 type->typedeft.declaration = declaration;
1513 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1515 type_t *type = NULL;
1516 unsigned type_qualifiers = 0;
1517 unsigned type_specifiers = 0;
1520 specifiers->source_position = token.source_position;
1523 switch(token.type) {
1526 #define MATCH_STORAGE_CLASS(token, class) \
1528 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1529 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1531 specifiers->storage_class = class; \
1535 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1536 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1537 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1538 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1539 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1542 switch (specifiers->storage_class) {
1543 case STORAGE_CLASS_NONE:
1544 specifiers->storage_class = STORAGE_CLASS_THREAD;
1547 case STORAGE_CLASS_EXTERN:
1548 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1551 case STORAGE_CLASS_STATIC:
1552 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1556 errorf(HERE, "multiple storage classes in declaration specifiers");
1562 /* type qualifiers */
1563 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1565 type_qualifiers |= qualifier; \
1569 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1570 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1571 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1573 case T___extension__:
1578 /* type specifiers */
1579 #define MATCH_SPECIFIER(token, specifier, name) \
1582 if(type_specifiers & specifier) { \
1583 errorf(HERE, "multiple " name " type specifiers given"); \
1585 type_specifiers |= specifier; \
1589 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1590 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1591 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1592 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1593 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1594 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1595 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1596 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1597 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1598 #ifdef PROVIDE_COMPLEX
1599 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1600 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1603 /* only in microsoft mode */
1604 specifiers->decl_modifiers |= DM_FORCEINLINE;
1608 specifiers->is_inline = true;
1613 if(type_specifiers & SPECIFIER_LONG_LONG) {
1614 errorf(HERE, "multiple type specifiers given");
1615 } else if(type_specifiers & SPECIFIER_LONG) {
1616 type_specifiers |= SPECIFIER_LONG_LONG;
1618 type_specifiers |= SPECIFIER_LONG;
1623 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1625 type->compound.declaration = parse_compound_type_specifier(true);
1629 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1631 type->compound.declaration = parse_compound_type_specifier(false);
1635 type = parse_enum_specifier();
1638 type = parse_typeof();
1640 case T___builtin_va_list:
1641 type = duplicate_type(type_valist);
1645 case T___attribute__:
1649 case T_IDENTIFIER: {
1650 /* only parse identifier if we haven't found a type yet */
1651 if(type != NULL || type_specifiers != 0)
1652 goto finish_specifiers;
1654 type_t *typedef_type = get_typedef_type(token.v.symbol);
1656 if(typedef_type == NULL)
1657 goto finish_specifiers;
1660 type = typedef_type;
1664 /* function specifier */
1666 goto finish_specifiers;
1673 atomic_type_kind_t atomic_type;
1675 /* match valid basic types */
1676 switch(type_specifiers) {
1677 case SPECIFIER_VOID:
1678 atomic_type = ATOMIC_TYPE_VOID;
1680 case SPECIFIER_CHAR:
1681 atomic_type = ATOMIC_TYPE_CHAR;
1683 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1684 atomic_type = ATOMIC_TYPE_SCHAR;
1686 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1687 atomic_type = ATOMIC_TYPE_UCHAR;
1689 case SPECIFIER_SHORT:
1690 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1691 case SPECIFIER_SHORT | SPECIFIER_INT:
1692 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1693 atomic_type = ATOMIC_TYPE_SHORT;
1695 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1696 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1697 atomic_type = ATOMIC_TYPE_USHORT;
1700 case SPECIFIER_SIGNED:
1701 case SPECIFIER_SIGNED | SPECIFIER_INT:
1702 atomic_type = ATOMIC_TYPE_INT;
1704 case SPECIFIER_UNSIGNED:
1705 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1706 atomic_type = ATOMIC_TYPE_UINT;
1708 case SPECIFIER_LONG:
1709 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1710 case SPECIFIER_LONG | SPECIFIER_INT:
1711 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1712 atomic_type = ATOMIC_TYPE_LONG;
1714 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1715 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1716 atomic_type = ATOMIC_TYPE_ULONG;
1718 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1719 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1720 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1721 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1723 atomic_type = ATOMIC_TYPE_LONGLONG;
1725 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1726 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1728 atomic_type = ATOMIC_TYPE_ULONGLONG;
1730 case SPECIFIER_FLOAT:
1731 atomic_type = ATOMIC_TYPE_FLOAT;
1733 case SPECIFIER_DOUBLE:
1734 atomic_type = ATOMIC_TYPE_DOUBLE;
1736 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1737 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1739 case SPECIFIER_BOOL:
1740 atomic_type = ATOMIC_TYPE_BOOL;
1742 #ifdef PROVIDE_COMPLEX
1743 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1744 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1746 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1747 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1749 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1750 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1752 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1753 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1755 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1756 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1758 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1759 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1763 /* invalid specifier combination, give an error message */
1764 if(type_specifiers == 0) {
1765 if (! strict_mode) {
1766 if (warning.implicit_int) {
1767 warningf(HERE, "no type specifiers in declaration, using 'int'");
1769 atomic_type = ATOMIC_TYPE_INT;
1772 errorf(HERE, "no type specifiers given in declaration");
1774 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1775 (type_specifiers & SPECIFIER_UNSIGNED)) {
1776 errorf(HERE, "signed and unsigned specifiers gives");
1777 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1778 errorf(HERE, "only integer types can be signed or unsigned");
1780 errorf(HERE, "multiple datatypes in declaration");
1782 atomic_type = ATOMIC_TYPE_INVALID;
1785 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
1786 type->atomic.akind = atomic_type;
1789 if(type_specifiers != 0) {
1790 errorf(HERE, "multiple datatypes in declaration");
1794 type->base.qualifiers = type_qualifiers;
1796 type_t *result = typehash_insert(type);
1797 if(newtype && result != type) {
1801 specifiers->type = result;
1804 static type_qualifiers_t parse_type_qualifiers(void)
1806 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1809 switch(token.type) {
1810 /* type qualifiers */
1811 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1812 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1813 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1816 return type_qualifiers;
1821 static declaration_t *parse_identifier_list(void)
1823 declaration_t *declarations = NULL;
1824 declaration_t *last_declaration = NULL;
1826 declaration_t *const declaration = allocate_declaration_zero();
1827 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1828 declaration->source_position = token.source_position;
1829 declaration->symbol = token.v.symbol;
1832 if(last_declaration != NULL) {
1833 last_declaration->next = declaration;
1835 declarations = declaration;
1837 last_declaration = declaration;
1839 if(token.type != ',')
1842 } while(token.type == T_IDENTIFIER);
1844 return declarations;
1847 static void semantic_parameter(declaration_t *declaration)
1849 /* TODO: improve error messages */
1851 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1852 errorf(HERE, "typedef not allowed in parameter list");
1853 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1854 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1855 errorf(HERE, "parameter may only have none or register storage class");
1858 type_t *const orig_type = declaration->type;
1859 type_t * type = skip_typeref(orig_type);
1861 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1862 * into a pointer. § 6.7.5.3 (7) */
1863 if (is_type_array(type)) {
1864 type_t *const element_type = type->array.element_type;
1866 type = make_pointer_type(element_type, type->base.qualifiers);
1868 declaration->type = type;
1871 if(is_type_incomplete(type)) {
1872 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
1873 orig_type, declaration->symbol);
1877 static declaration_t *parse_parameter(void)
1879 declaration_specifiers_t specifiers;
1880 memset(&specifiers, 0, sizeof(specifiers));
1882 parse_declaration_specifiers(&specifiers);
1884 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1886 semantic_parameter(declaration);
1891 static declaration_t *parse_parameters(function_type_t *type)
1893 if(token.type == T_IDENTIFIER) {
1894 symbol_t *symbol = token.v.symbol;
1895 if(!is_typedef_symbol(symbol)) {
1896 type->kr_style_parameters = true;
1897 return parse_identifier_list();
1901 if(token.type == ')') {
1902 type->unspecified_parameters = 1;
1905 if(token.type == T_void && look_ahead(1)->type == ')') {
1910 declaration_t *declarations = NULL;
1911 declaration_t *declaration;
1912 declaration_t *last_declaration = NULL;
1913 function_parameter_t *parameter;
1914 function_parameter_t *last_parameter = NULL;
1917 switch(token.type) {
1921 return declarations;
1924 case T___extension__:
1926 declaration = parse_parameter();
1928 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1929 memset(parameter, 0, sizeof(parameter[0]));
1930 parameter->type = declaration->type;
1932 if(last_parameter != NULL) {
1933 last_declaration->next = declaration;
1934 last_parameter->next = parameter;
1936 type->parameters = parameter;
1937 declarations = declaration;
1939 last_parameter = parameter;
1940 last_declaration = declaration;
1944 return declarations;
1946 if(token.type != ',')
1947 return declarations;
1957 } construct_type_kind_t;
1959 typedef struct construct_type_t construct_type_t;
1960 struct construct_type_t {
1961 construct_type_kind_t kind;
1962 construct_type_t *next;
1965 typedef struct parsed_pointer_t parsed_pointer_t;
1966 struct parsed_pointer_t {
1967 construct_type_t construct_type;
1968 type_qualifiers_t type_qualifiers;
1971 typedef struct construct_function_type_t construct_function_type_t;
1972 struct construct_function_type_t {
1973 construct_type_t construct_type;
1974 type_t *function_type;
1977 typedef struct parsed_array_t parsed_array_t;
1978 struct parsed_array_t {
1979 construct_type_t construct_type;
1980 type_qualifiers_t type_qualifiers;
1986 typedef struct construct_base_type_t construct_base_type_t;
1987 struct construct_base_type_t {
1988 construct_type_t construct_type;
1992 static construct_type_t *parse_pointer_declarator(void)
1996 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1997 memset(pointer, 0, sizeof(pointer[0]));
1998 pointer->construct_type.kind = CONSTRUCT_POINTER;
1999 pointer->type_qualifiers = parse_type_qualifiers();
2001 return (construct_type_t*) pointer;
2004 static construct_type_t *parse_array_declarator(void)
2008 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2009 memset(array, 0, sizeof(array[0]));
2010 array->construct_type.kind = CONSTRUCT_ARRAY;
2012 if(token.type == T_static) {
2013 array->is_static = true;
2017 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2018 if(type_qualifiers != 0) {
2019 if(token.type == T_static) {
2020 array->is_static = true;
2024 array->type_qualifiers = type_qualifiers;
2026 if(token.type == '*' && look_ahead(1)->type == ']') {
2027 array->is_variable = true;
2029 } else if(token.type != ']') {
2030 array->size = parse_assignment_expression();
2035 return (construct_type_t*) array;
2038 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2043 if(declaration != NULL) {
2044 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2046 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2049 declaration_t *parameters = parse_parameters(&type->function);
2050 if(declaration != NULL) {
2051 declaration->scope.declarations = parameters;
2054 construct_function_type_t *construct_function_type =
2055 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2056 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2057 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2058 construct_function_type->function_type = type;
2062 return (construct_type_t*) construct_function_type;
2065 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2066 bool may_be_abstract)
2068 /* construct a single linked list of construct_type_t's which describe
2069 * how to construct the final declarator type */
2070 construct_type_t *first = NULL;
2071 construct_type_t *last = NULL;
2074 while(token.type == '*') {
2075 construct_type_t *type = parse_pointer_declarator();
2086 /* TODO: find out if this is correct */
2089 construct_type_t *inner_types = NULL;
2091 switch(token.type) {
2093 if(declaration == NULL) {
2094 errorf(HERE, "no identifier expected in typename");
2096 declaration->symbol = token.v.symbol;
2097 declaration->source_position = token.source_position;
2103 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2109 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2110 /* avoid a loop in the outermost scope, because eat_statement doesn't
2112 if(token.type == '}' && current_function == NULL) {
2120 construct_type_t *p = last;
2123 construct_type_t *type;
2124 switch(token.type) {
2126 type = parse_function_declarator(declaration);
2129 type = parse_array_declarator();
2132 goto declarator_finished;
2135 /* insert in the middle of the list (behind p) */
2137 type->next = p->next;
2148 declarator_finished:
2151 /* append inner_types at the end of the list, we don't to set last anymore
2152 * as it's not needed anymore */
2154 assert(first == NULL);
2155 first = inner_types;
2157 last->next = inner_types;
2163 static type_t *construct_declarator_type(construct_type_t *construct_list,
2166 construct_type_t *iter = construct_list;
2167 for( ; iter != NULL; iter = iter->next) {
2168 switch(iter->kind) {
2169 case CONSTRUCT_INVALID:
2170 panic("invalid type construction found");
2171 case CONSTRUCT_FUNCTION: {
2172 construct_function_type_t *construct_function_type
2173 = (construct_function_type_t*) iter;
2175 type_t *function_type = construct_function_type->function_type;
2177 function_type->function.return_type = type;
2179 type_t *skipped_return_type = skip_typeref(type);
2180 if (is_type_function(skipped_return_type)) {
2181 errorf(HERE, "function returning function is not allowed");
2182 type = type_error_type;
2183 } else if (is_type_array(skipped_return_type)) {
2184 errorf(HERE, "function returning array is not allowed");
2185 type = type_error_type;
2187 type = function_type;
2192 case CONSTRUCT_POINTER: {
2193 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2194 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2195 pointer_type->pointer.points_to = type;
2196 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2198 type = pointer_type;
2202 case CONSTRUCT_ARRAY: {
2203 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2204 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2206 array_type->base.qualifiers = parsed_array->type_qualifiers;
2207 array_type->array.element_type = type;
2208 array_type->array.is_static = parsed_array->is_static;
2209 array_type->array.is_variable = parsed_array->is_variable;
2210 array_type->array.size = parsed_array->size;
2212 type_t *skipped_type = skip_typeref(type);
2213 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2214 errorf(HERE, "array of void is not allowed");
2215 type = type_error_type;
2223 type_t *hashed_type = typehash_insert(type);
2224 if(hashed_type != type) {
2225 /* the function type was constructed earlier freeing it here will
2226 * destroy other types... */
2227 if(iter->kind != CONSTRUCT_FUNCTION) {
2237 static declaration_t *parse_declarator(
2238 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2240 declaration_t *const declaration = allocate_declaration_zero();
2241 declaration->storage_class = specifiers->storage_class;
2242 declaration->modifiers = specifiers->decl_modifiers;
2243 declaration->is_inline = specifiers->is_inline;
2245 construct_type_t *construct_type
2246 = parse_inner_declarator(declaration, may_be_abstract);
2247 type_t *const type = specifiers->type;
2248 declaration->type = construct_declarator_type(construct_type, type);
2250 if(construct_type != NULL) {
2251 obstack_free(&temp_obst, construct_type);
2257 static type_t *parse_abstract_declarator(type_t *base_type)
2259 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2261 type_t *result = construct_declarator_type(construct_type, base_type);
2262 if(construct_type != NULL) {
2263 obstack_free(&temp_obst, construct_type);
2269 static declaration_t *append_declaration(declaration_t* const declaration)
2271 if (last_declaration != NULL) {
2272 last_declaration->next = declaration;
2274 scope->declarations = declaration;
2276 last_declaration = declaration;
2281 * Check if the declaration of main is suspicious. main should be a
2282 * function with external linkage, returning int, taking either zero
2283 * arguments, two, or three arguments of appropriate types, ie.
2285 * int main([ int argc, char **argv [, char **env ] ]).
2287 * @param decl the declaration to check
2288 * @param type the function type of the declaration
2290 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2292 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2293 warningf(decl->source_position, "'main' is normally a non-static function");
2295 if (skip_typeref(func_type->return_type) != type_int) {
2296 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2298 const function_parameter_t *parm = func_type->parameters;
2300 type_t *const first_type = parm->type;
2301 if (!types_compatible(skip_typeref(first_type), type_int)) {
2302 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2306 type_t *const second_type = parm->type;
2307 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2308 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2312 type_t *const third_type = parm->type;
2313 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2314 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2318 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2322 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2328 * Check if a symbol is the equal to "main".
2330 static bool is_sym_main(const symbol_t *const sym)
2332 return strcmp(sym->string, "main") == 0;
2335 static declaration_t *internal_record_declaration(
2336 declaration_t *const declaration,
2337 const bool is_function_definition)
2339 const symbol_t *const symbol = declaration->symbol;
2340 const namespace_t namespc = (namespace_t)declaration->namespc;
2342 type_t *const orig_type = declaration->type;
2343 type_t *const type = skip_typeref(orig_type);
2344 if (is_type_function(type) &&
2345 type->function.unspecified_parameters &&
2346 warning.strict_prototypes) {
2347 warningf(declaration->source_position,
2348 "function declaration '%#T' is not a prototype",
2349 orig_type, declaration->symbol);
2352 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2353 check_type_of_main(declaration, &type->function);
2356 assert(declaration->symbol != NULL);
2357 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2359 assert(declaration != previous_declaration);
2360 if (previous_declaration != NULL) {
2361 if (previous_declaration->parent_scope == scope) {
2362 /* can happen for K&R style declarations */
2363 if(previous_declaration->type == NULL) {
2364 previous_declaration->type = declaration->type;
2367 const type_t *prev_type = skip_typeref(previous_declaration->type);
2368 if (!types_compatible(type, prev_type)) {
2369 errorf(declaration->source_position,
2370 "declaration '%#T' is incompatible with "
2371 "previous declaration '%#T'",
2372 orig_type, symbol, previous_declaration->type, symbol);
2373 errorf(previous_declaration->source_position,
2374 "previous declaration of '%Y' was here", symbol);
2376 unsigned old_storage_class
2377 = previous_declaration->storage_class;
2378 unsigned new_storage_class = declaration->storage_class;
2380 if(is_type_incomplete(prev_type)) {
2381 previous_declaration->type = type;
2385 /* pretend no storage class means extern for function
2386 * declarations (except if the previous declaration is neither
2387 * none nor extern) */
2388 if (is_type_function(type)) {
2389 switch (old_storage_class) {
2390 case STORAGE_CLASS_NONE:
2391 old_storage_class = STORAGE_CLASS_EXTERN;
2393 case STORAGE_CLASS_EXTERN:
2394 if (is_function_definition) {
2395 if (warning.missing_prototypes &&
2396 prev_type->function.unspecified_parameters &&
2397 !is_sym_main(symbol)) {
2398 warningf(declaration->source_position,
2399 "no previous prototype for '%#T'",
2402 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2403 new_storage_class = STORAGE_CLASS_EXTERN;
2411 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2412 new_storage_class == STORAGE_CLASS_EXTERN) {
2413 warn_redundant_declaration:
2414 if (warning.redundant_decls) {
2415 warningf(declaration->source_position,
2416 "redundant declaration for '%Y'", symbol);
2417 warningf(previous_declaration->source_position,
2418 "previous declaration of '%Y' was here",
2421 } else if (current_function == NULL) {
2422 if (old_storage_class != STORAGE_CLASS_STATIC &&
2423 new_storage_class == STORAGE_CLASS_STATIC) {
2424 errorf(declaration->source_position,
2425 "static declaration of '%Y' follows non-static declaration",
2427 errorf(previous_declaration->source_position,
2428 "previous declaration of '%Y' was here", symbol);
2430 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2431 goto warn_redundant_declaration;
2433 if (new_storage_class == STORAGE_CLASS_NONE) {
2434 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2438 if (old_storage_class == new_storage_class) {
2439 errorf(declaration->source_position,
2440 "redeclaration of '%Y'", symbol);
2442 errorf(declaration->source_position,
2443 "redeclaration of '%Y' with different linkage",
2446 errorf(previous_declaration->source_position,
2447 "previous declaration of '%Y' was here", symbol);
2450 return previous_declaration;
2452 } else if (is_function_definition) {
2453 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2454 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2455 warningf(declaration->source_position,
2456 "no previous prototype for '%#T'", orig_type, symbol);
2457 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2458 warningf(declaration->source_position,
2459 "no previous declaration for '%#T'", orig_type,
2463 } else if (warning.missing_declarations &&
2464 scope == global_scope &&
2465 !is_type_function(type) && (
2466 declaration->storage_class == STORAGE_CLASS_NONE ||
2467 declaration->storage_class == STORAGE_CLASS_THREAD
2469 warningf(declaration->source_position,
2470 "no previous declaration for '%#T'", orig_type, symbol);
2473 assert(declaration->parent_scope == NULL);
2474 assert(scope != NULL);
2476 declaration->parent_scope = scope;
2478 environment_push(declaration);
2479 return append_declaration(declaration);
2482 static declaration_t *record_declaration(declaration_t *declaration)
2484 return internal_record_declaration(declaration, false);
2487 static declaration_t *record_function_definition(declaration_t *declaration)
2489 return internal_record_declaration(declaration, true);
2492 static void parser_error_multiple_definition(declaration_t *declaration,
2493 const source_position_t source_position)
2495 errorf(source_position, "multiple definition of symbol '%Y'",
2496 declaration->symbol);
2497 errorf(declaration->source_position,
2498 "this is the location of the previous definition.");
2501 static bool is_declaration_specifier(const token_t *token,
2502 bool only_type_specifiers)
2504 switch(token->type) {
2508 return is_typedef_symbol(token->v.symbol);
2510 case T___extension__:
2513 return !only_type_specifiers;
2520 static void parse_init_declarator_rest(declaration_t *declaration)
2524 type_t *orig_type = declaration->type;
2525 type_t *type = type = skip_typeref(orig_type);
2527 if(declaration->init.initializer != NULL) {
2528 parser_error_multiple_definition(declaration, token.source_position);
2531 initializer_t *initializer = parse_initializer(type);
2533 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2534 * the array type size */
2535 if(is_type_array(type) && initializer != NULL) {
2536 array_type_t *array_type = &type->array;
2538 if(array_type->size == NULL) {
2539 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2541 cnst->base.type = type_size_t;
2543 switch (initializer->kind) {
2544 case INITIALIZER_LIST: {
2545 cnst->conste.v.int_value = initializer->list.len;
2549 case INITIALIZER_STRING: {
2550 cnst->conste.v.int_value = initializer->string.string.size;
2554 case INITIALIZER_WIDE_STRING: {
2555 cnst->conste.v.int_value = initializer->wide_string.string.size;
2560 panic("invalid initializer type");
2563 array_type->size = cnst;
2564 array_type->has_implicit_size = true;
2568 if(is_type_function(type)) {
2569 errorf(declaration->source_position,
2570 "initializers not allowed for function types at declator '%Y' (type '%T')",
2571 declaration->symbol, orig_type);
2573 declaration->init.initializer = initializer;
2577 /* parse rest of a declaration without any declarator */
2578 static void parse_anonymous_declaration_rest(
2579 const declaration_specifiers_t *specifiers,
2580 parsed_declaration_func finished_declaration)
2584 declaration_t *const declaration = allocate_declaration_zero();
2585 declaration->type = specifiers->type;
2586 declaration->storage_class = specifiers->storage_class;
2587 declaration->source_position = specifiers->source_position;
2589 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2590 warningf(declaration->source_position, "useless storage class in empty declaration");
2593 type_t *type = declaration->type;
2594 switch (type->kind) {
2595 case TYPE_COMPOUND_STRUCT:
2596 case TYPE_COMPOUND_UNION: {
2597 if (type->compound.declaration->symbol == NULL) {
2598 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2607 warningf(declaration->source_position, "empty declaration");
2611 finished_declaration(declaration);
2614 static void parse_declaration_rest(declaration_t *ndeclaration,
2615 const declaration_specifiers_t *specifiers,
2616 parsed_declaration_func finished_declaration)
2619 declaration_t *declaration = finished_declaration(ndeclaration);
2621 type_t *orig_type = declaration->type;
2622 type_t *type = skip_typeref(orig_type);
2624 if (type->kind != TYPE_FUNCTION &&
2625 declaration->is_inline &&
2626 is_type_valid(type)) {
2627 warningf(declaration->source_position,
2628 "variable '%Y' declared 'inline'\n", declaration->symbol);
2631 if(token.type == '=') {
2632 parse_init_declarator_rest(declaration);
2635 if(token.type != ',')
2639 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2644 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2646 symbol_t *symbol = declaration->symbol;
2647 if(symbol == NULL) {
2648 errorf(HERE, "anonymous declaration not valid as function parameter");
2651 namespace_t namespc = (namespace_t) declaration->namespc;
2652 if(namespc != NAMESPACE_NORMAL) {
2653 return record_declaration(declaration);
2656 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2657 if(previous_declaration == NULL ||
2658 previous_declaration->parent_scope != scope) {
2659 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2664 if(previous_declaration->type == NULL) {
2665 previous_declaration->type = declaration->type;
2666 previous_declaration->storage_class = declaration->storage_class;
2667 previous_declaration->parent_scope = scope;
2668 return previous_declaration;
2670 return record_declaration(declaration);
2674 static void parse_declaration(parsed_declaration_func finished_declaration)
2676 declaration_specifiers_t specifiers;
2677 memset(&specifiers, 0, sizeof(specifiers));
2678 parse_declaration_specifiers(&specifiers);
2680 if(token.type == ';') {
2681 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2683 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2684 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2688 static void parse_kr_declaration_list(declaration_t *declaration)
2690 type_t *type = skip_typeref(declaration->type);
2691 if(!is_type_function(type))
2694 if(!type->function.kr_style_parameters)
2697 /* push function parameters */
2698 int top = environment_top();
2699 scope_t *last_scope = scope;
2700 set_scope(&declaration->scope);
2702 declaration_t *parameter = declaration->scope.declarations;
2703 for( ; parameter != NULL; parameter = parameter->next) {
2704 assert(parameter->parent_scope == NULL);
2705 parameter->parent_scope = scope;
2706 environment_push(parameter);
2709 /* parse declaration list */
2710 while(is_declaration_specifier(&token, false)) {
2711 parse_declaration(finished_kr_declaration);
2714 /* pop function parameters */
2715 assert(scope == &declaration->scope);
2716 set_scope(last_scope);
2717 environment_pop_to(top);
2719 /* update function type */
2720 type_t *new_type = duplicate_type(type);
2721 new_type->function.kr_style_parameters = false;
2723 function_parameter_t *parameters = NULL;
2724 function_parameter_t *last_parameter = NULL;
2726 declaration_t *parameter_declaration = declaration->scope.declarations;
2727 for( ; parameter_declaration != NULL;
2728 parameter_declaration = parameter_declaration->next) {
2729 type_t *parameter_type = parameter_declaration->type;
2730 if(parameter_type == NULL) {
2732 errorf(HERE, "no type specified for function parameter '%Y'",
2733 parameter_declaration->symbol);
2735 if (warning.implicit_int) {
2736 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2737 parameter_declaration->symbol);
2739 parameter_type = type_int;
2740 parameter_declaration->type = parameter_type;
2744 semantic_parameter(parameter_declaration);
2745 parameter_type = parameter_declaration->type;
2747 function_parameter_t *function_parameter
2748 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2749 memset(function_parameter, 0, sizeof(function_parameter[0]));
2751 function_parameter->type = parameter_type;
2752 if(last_parameter != NULL) {
2753 last_parameter->next = function_parameter;
2755 parameters = function_parameter;
2757 last_parameter = function_parameter;
2759 new_type->function.parameters = parameters;
2761 type = typehash_insert(new_type);
2762 if(type != new_type) {
2763 obstack_free(type_obst, new_type);
2766 declaration->type = type;
2769 static bool first_err = true;
2772 * When called with first_err set, prints the name of the current function,
2775 static void print_in_function(void) {
2778 diagnosticf("%s: In function '%Y':\n",
2779 current_function->source_position.input_name,
2780 current_function->symbol);
2785 * Check if all labels are defined in the current function.
2786 * Check if all labels are used in the current function.
2788 static void check_labels(void)
2790 for (const goto_statement_t *goto_statement = goto_first;
2791 goto_statement != NULL;
2792 goto_statement = goto_statement->next) {
2793 declaration_t *label = goto_statement->label;
2796 if (label->source_position.input_name == NULL) {
2797 print_in_function();
2798 errorf(goto_statement->base.source_position,
2799 "label '%Y' used but not defined", label->symbol);
2802 goto_first = goto_last = NULL;
2804 if (warning.unused_label) {
2805 for (const label_statement_t *label_statement = label_first;
2806 label_statement != NULL;
2807 label_statement = label_statement->next) {
2808 const declaration_t *label = label_statement->label;
2810 if (! label->used) {
2811 print_in_function();
2812 warningf(label_statement->base.source_position,
2813 "label '%Y' defined but not used", label->symbol);
2817 label_first = label_last = NULL;
2821 * Check declarations of current_function for unused entities.
2823 static void check_declarations(void)
2825 if (warning.unused_parameter) {
2826 const scope_t *scope = ¤t_function->scope;
2828 const declaration_t *parameter = scope->declarations;
2829 for (; parameter != NULL; parameter = parameter->next) {
2830 if (! parameter->used) {
2831 print_in_function();
2832 warningf(parameter->source_position,
2833 "unused parameter '%Y'", parameter->symbol);
2837 if (warning.unused_variable) {
2841 static void parse_external_declaration(void)
2843 /* function-definitions and declarations both start with declaration
2845 declaration_specifiers_t specifiers;
2846 memset(&specifiers, 0, sizeof(specifiers));
2847 parse_declaration_specifiers(&specifiers);
2849 /* must be a declaration */
2850 if(token.type == ';') {
2851 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2855 /* declarator is common to both function-definitions and declarations */
2856 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2858 /* must be a declaration */
2859 if(token.type == ',' || token.type == '=' || token.type == ';') {
2860 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2864 /* must be a function definition */
2865 parse_kr_declaration_list(ndeclaration);
2867 if(token.type != '{') {
2868 parse_error_expected("while parsing function definition", '{', 0);
2873 type_t *type = ndeclaration->type;
2875 /* note that we don't skip typerefs: the standard doesn't allow them here
2876 * (so we can't use is_type_function here) */
2877 if(type->kind != TYPE_FUNCTION) {
2878 if (is_type_valid(type)) {
2879 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2880 type, ndeclaration->symbol);
2886 /* § 6.7.5.3 (14) a function definition with () means no
2887 * parameters (and not unspecified parameters) */
2888 if(type->function.unspecified_parameters) {
2889 type_t *duplicate = duplicate_type(type);
2890 duplicate->function.unspecified_parameters = false;
2892 type = typehash_insert(duplicate);
2893 if(type != duplicate) {
2894 obstack_free(type_obst, duplicate);
2896 ndeclaration->type = type;
2899 declaration_t *const declaration = record_function_definition(ndeclaration);
2900 if(ndeclaration != declaration) {
2901 declaration->scope = ndeclaration->scope;
2903 type = skip_typeref(declaration->type);
2905 /* push function parameters and switch scope */
2906 int top = environment_top();
2907 scope_t *last_scope = scope;
2908 set_scope(&declaration->scope);
2910 declaration_t *parameter = declaration->scope.declarations;
2911 for( ; parameter != NULL; parameter = parameter->next) {
2912 if(parameter->parent_scope == &ndeclaration->scope) {
2913 parameter->parent_scope = scope;
2915 assert(parameter->parent_scope == NULL
2916 || parameter->parent_scope == scope);
2917 parameter->parent_scope = scope;
2918 environment_push(parameter);
2921 if(declaration->init.statement != NULL) {
2922 parser_error_multiple_definition(declaration, token.source_position);
2924 goto end_of_parse_external_declaration;
2926 /* parse function body */
2927 int label_stack_top = label_top();
2928 declaration_t *old_current_function = current_function;
2929 current_function = declaration;
2931 declaration->init.statement = parse_compound_statement();
2934 check_declarations();
2936 assert(current_function == declaration);
2937 current_function = old_current_function;
2938 label_pop_to(label_stack_top);
2941 end_of_parse_external_declaration:
2942 assert(scope == &declaration->scope);
2943 set_scope(last_scope);
2944 environment_pop_to(top);
2947 static type_t *make_bitfield_type(type_t *base, expression_t *size,
2948 source_position_t source_position)
2950 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
2951 type->bitfield.base = base;
2952 type->bitfield.size = size;
2957 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
2960 declaration_t *iter = compound_declaration->scope.declarations;
2961 for( ; iter != NULL; iter = iter->next) {
2962 if(iter->namespc != NAMESPACE_NORMAL)
2965 if(iter->symbol == NULL) {
2966 type_t *type = skip_typeref(iter->type);
2967 if(is_type_compound(type)) {
2968 declaration_t *result
2969 = find_compound_entry(type->compound.declaration, symbol);
2976 if(iter->symbol == symbol) {
2984 static void parse_compound_declarators(declaration_t *struct_declaration,
2985 const declaration_specifiers_t *specifiers)
2987 declaration_t *last_declaration = struct_declaration->scope.declarations;
2988 if(last_declaration != NULL) {
2989 while(last_declaration->next != NULL) {
2990 last_declaration = last_declaration->next;
2995 declaration_t *declaration;
2997 if(token.type == ':') {
2998 source_position_t source_position = HERE;
3001 type_t *base_type = specifiers->type;
3002 expression_t *size = parse_constant_expression();
3004 if(!is_type_integer(skip_typeref(base_type))) {
3005 errorf(HERE, "bitfield base type '%T' is not an integer type",
3009 type_t *type = make_bitfield_type(base_type, size, source_position);
3011 declaration = allocate_declaration_zero();
3012 declaration->namespc = NAMESPACE_NORMAL;
3013 declaration->storage_class = STORAGE_CLASS_NONE;
3014 declaration->source_position = source_position;
3015 declaration->modifiers = specifiers->decl_modifiers;
3016 declaration->type = type;
3018 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3020 type_t *orig_type = declaration->type;
3021 type_t *type = skip_typeref(orig_type);
3023 if(token.type == ':') {
3024 source_position_t source_position = HERE;
3026 expression_t *size = parse_constant_expression();
3028 if(!is_type_integer(type)) {
3029 errorf(HERE, "bitfield base type '%T' is not an "
3030 "integer type", orig_type);
3033 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3034 declaration->type = bitfield_type;
3036 /* TODO we ignore arrays for now... what is missing is a check
3037 * that they're at the end of the struct */
3038 if(is_type_incomplete(type) && !is_type_array(type)) {
3040 "compound member '%Y' has incomplete type '%T'",
3041 declaration->symbol, orig_type);
3042 } else if(is_type_function(type)) {
3043 errorf(HERE, "compound member '%Y' must not have function "
3044 "type '%T'", declaration->symbol, orig_type);
3049 /* make sure we don't define a symbol multiple times */
3050 symbol_t *symbol = declaration->symbol;
3051 if(symbol != NULL) {
3052 declaration_t *prev_decl
3053 = find_compound_entry(struct_declaration, symbol);
3055 if(prev_decl != NULL) {
3056 assert(prev_decl->symbol == symbol);
3057 errorf(declaration->source_position,
3058 "multiple declarations of symbol '%Y'", symbol);
3059 errorf(prev_decl->source_position,
3060 "previous declaration of '%Y' was here", symbol);
3064 /* append declaration */
3065 if(last_declaration != NULL) {
3066 last_declaration->next = declaration;
3068 struct_declaration->scope.declarations = declaration;
3070 last_declaration = declaration;
3072 if(token.type != ',')
3079 static void parse_compound_type_entries(declaration_t *compound_declaration)
3083 while(token.type != '}' && token.type != T_EOF) {
3084 declaration_specifiers_t specifiers;
3085 memset(&specifiers, 0, sizeof(specifiers));
3086 parse_declaration_specifiers(&specifiers);
3088 parse_compound_declarators(compound_declaration, &specifiers);
3090 if(token.type == T_EOF) {
3091 errorf(HERE, "EOF while parsing struct");
3096 static type_t *parse_typename(void)
3098 declaration_specifiers_t specifiers;
3099 memset(&specifiers, 0, sizeof(specifiers));
3100 parse_declaration_specifiers(&specifiers);
3101 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3102 /* TODO: improve error message, user does probably not know what a
3103 * storage class is...
3105 errorf(HERE, "typename may not have a storage class");
3108 type_t *result = parse_abstract_declarator(specifiers.type);
3116 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3117 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3118 expression_t *left);
3120 typedef struct expression_parser_function_t expression_parser_function_t;
3121 struct expression_parser_function_t {
3122 unsigned precedence;
3123 parse_expression_function parser;
3124 unsigned infix_precedence;
3125 parse_expression_infix_function infix_parser;
3128 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3131 * Creates a new invalid expression.
3133 static expression_t *create_invalid_expression(void)
3135 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3136 expression->base.source_position = token.source_position;
3141 * Prints an error message if an expression was expected but not read
3143 static expression_t *expected_expression_error(void)
3145 /* skip the error message if the error token was read */
3146 if (token.type != T_ERROR) {
3147 errorf(HERE, "expected expression, got token '%K'", &token);
3151 return create_invalid_expression();
3155 * Parse a string constant.
3157 static expression_t *parse_string_const(void)
3160 if (token.type == T_STRING_LITERAL) {
3161 string_t res = token.v.string;
3163 while (token.type == T_STRING_LITERAL) {
3164 res = concat_strings(&res, &token.v.string);
3167 if (token.type != T_WIDE_STRING_LITERAL) {
3168 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3169 cnst->base.type = type_char_ptr;
3170 cnst->string.value = res;
3174 wres = concat_string_wide_string(&res, &token.v.wide_string);
3176 wres = token.v.wide_string;
3181 switch (token.type) {
3182 case T_WIDE_STRING_LITERAL:
3183 wres = concat_wide_strings(&wres, &token.v.wide_string);
3186 case T_STRING_LITERAL:
3187 wres = concat_wide_string_string(&wres, &token.v.string);
3191 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3192 cnst->base.type = type_wchar_t_ptr;
3193 cnst->wide_string.value = wres;
3202 * Parse an integer constant.
3204 static expression_t *parse_int_const(void)
3206 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3207 cnst->base.source_position = HERE;
3208 cnst->base.type = token.datatype;
3209 cnst->conste.v.int_value = token.v.intvalue;
3217 * Parse a character constant.
3219 static expression_t *parse_char_const(void)
3221 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3222 cnst->base.source_position = HERE;
3223 cnst->base.type = token.datatype;
3224 cnst->conste.v.chars.begin = token.v.string.begin;
3225 cnst->conste.v.chars.size = token.v.string.size;
3227 if (cnst->conste.v.chars.size != 1) {
3228 if (warning.multichar && (c_mode & _GNUC)) {
3230 warningf(HERE, "multi-character character constant");
3232 errorf(HERE, "more than 1 characters in character constant");
3241 * Parse a float constant.
3243 static expression_t *parse_float_const(void)
3245 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3246 cnst->base.type = token.datatype;
3247 cnst->conste.v.float_value = token.v.floatvalue;
3254 static declaration_t *create_implicit_function(symbol_t *symbol,
3255 const source_position_t source_position)
3257 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3258 ntype->function.return_type = type_int;
3259 ntype->function.unspecified_parameters = true;
3261 type_t *type = typehash_insert(ntype);
3266 declaration_t *const declaration = allocate_declaration_zero();
3267 declaration->storage_class = STORAGE_CLASS_EXTERN;
3268 declaration->type = type;
3269 declaration->symbol = symbol;
3270 declaration->source_position = source_position;
3271 declaration->parent_scope = global_scope;
3273 scope_t *old_scope = scope;
3274 set_scope(global_scope);
3276 environment_push(declaration);
3277 /* prepends the declaration to the global declarations list */
3278 declaration->next = scope->declarations;
3279 scope->declarations = declaration;
3281 assert(scope == global_scope);
3282 set_scope(old_scope);
3288 * Creates a return_type (func)(argument_type) function type if not
3291 * @param return_type the return type
3292 * @param argument_type the argument type
3294 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3296 function_parameter_t *parameter
3297 = obstack_alloc(type_obst, sizeof(parameter[0]));
3298 memset(parameter, 0, sizeof(parameter[0]));
3299 parameter->type = argument_type;
3301 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3302 type->function.return_type = return_type;
3303 type->function.parameters = parameter;
3305 type_t *result = typehash_insert(type);
3306 if(result != type) {
3314 * Creates a function type for some function like builtins.
3316 * @param symbol the symbol describing the builtin
3318 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3320 switch(symbol->ID) {
3321 case T___builtin_alloca:
3322 return make_function_1_type(type_void_ptr, type_size_t);
3323 case T___builtin_nan:
3324 return make_function_1_type(type_double, type_char_ptr);
3325 case T___builtin_nanf:
3326 return make_function_1_type(type_float, type_char_ptr);
3327 case T___builtin_nand:
3328 return make_function_1_type(type_long_double, type_char_ptr);
3329 case T___builtin_va_end:
3330 return make_function_1_type(type_void, type_valist);
3332 panic("not implemented builtin symbol found");
3337 * Performs automatic type cast as described in § 6.3.2.1.
3339 * @param orig_type the original type
3341 static type_t *automatic_type_conversion(type_t *orig_type)
3343 type_t *type = skip_typeref(orig_type);
3344 if(is_type_array(type)) {
3345 array_type_t *array_type = &type->array;
3346 type_t *element_type = array_type->element_type;
3347 unsigned qualifiers = array_type->type.qualifiers;
3349 return make_pointer_type(element_type, qualifiers);
3352 if(is_type_function(type)) {
3353 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3360 * reverts the automatic casts of array to pointer types and function
3361 * to function-pointer types as defined § 6.3.2.1
3363 type_t *revert_automatic_type_conversion(const expression_t *expression)
3365 switch (expression->kind) {
3366 case EXPR_REFERENCE: return expression->reference.declaration->type;
3367 case EXPR_SELECT: return expression->select.compound_entry->type;
3369 case EXPR_UNARY_DEREFERENCE: {
3370 const expression_t *const value = expression->unary.value;
3371 type_t *const type = skip_typeref(value->base.type);
3372 assert(is_type_pointer(type));
3373 return type->pointer.points_to;
3376 case EXPR_BUILTIN_SYMBOL:
3377 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3379 case EXPR_ARRAY_ACCESS: {
3380 const expression_t *array_ref = expression->array_access.array_ref;
3381 type_t *type_left = skip_typeref(array_ref->base.type);
3382 if (!is_type_valid(type_left))
3384 assert(is_type_pointer(type_left));
3385 return type_left->pointer.points_to;
3391 return expression->base.type;
3394 static expression_t *parse_reference(void)
3396 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3398 reference_expression_t *ref = &expression->reference;
3399 ref->symbol = token.v.symbol;
3401 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3403 source_position_t source_position = token.source_position;
3406 if(declaration == NULL) {
3407 if (! strict_mode && token.type == '(') {
3408 /* an implicitly defined function */
3409 if (warning.implicit_function_declaration) {
3410 warningf(HERE, "implicit declaration of function '%Y'",
3414 declaration = create_implicit_function(ref->symbol,
3417 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3422 type_t *type = declaration->type;
3424 /* we always do the auto-type conversions; the & and sizeof parser contains
3425 * code to revert this! */
3426 type = automatic_type_conversion(type);
3428 ref->declaration = declaration;
3429 ref->base.type = type;
3431 /* this declaration is used */
3432 declaration->used = true;
3437 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3441 /* TODO check if explicit cast is allowed and issue warnings/errors */
3444 static expression_t *parse_cast(void)
3446 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3448 cast->base.source_position = token.source_position;
3450 type_t *type = parse_typename();
3453 expression_t *value = parse_sub_expression(20);
3455 check_cast_allowed(value, type);
3457 cast->base.type = type;
3458 cast->unary.value = value;
3463 static expression_t *parse_statement_expression(void)
3465 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3467 statement_t *statement = parse_compound_statement();
3468 expression->statement.statement = statement;
3469 expression->base.source_position = statement->base.source_position;
3471 /* find last statement and use its type */
3472 type_t *type = type_void;
3473 const statement_t *stmt = statement->compound.statements;
3475 while (stmt->base.next != NULL)
3476 stmt = stmt->base.next;
3478 if (stmt->kind == STATEMENT_EXPRESSION) {
3479 type = stmt->expression.expression->base.type;
3482 warningf(expression->base.source_position, "empty statement expression ({})");
3484 expression->base.type = type;
3491 static expression_t *parse_brace_expression(void)
3495 switch(token.type) {
3497 /* gcc extension: a statement expression */
3498 return parse_statement_expression();
3502 return parse_cast();
3504 if(is_typedef_symbol(token.v.symbol)) {
3505 return parse_cast();
3509 expression_t *result = parse_expression();
3515 static expression_t *parse_function_keyword(void)
3520 if (current_function == NULL) {
3521 errorf(HERE, "'__func__' used outside of a function");
3524 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3525 expression->base.type = type_char_ptr;
3530 static expression_t *parse_pretty_function_keyword(void)
3532 eat(T___PRETTY_FUNCTION__);
3535 if (current_function == NULL) {
3536 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3539 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3540 expression->base.type = type_char_ptr;
3545 static designator_t *parse_designator(void)
3547 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3549 if(token.type != T_IDENTIFIER) {
3550 parse_error_expected("while parsing member designator",
3555 result->symbol = token.v.symbol;
3558 designator_t *last_designator = result;
3560 if(token.type == '.') {
3562 if(token.type != T_IDENTIFIER) {
3563 parse_error_expected("while parsing member designator",
3568 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3569 designator->symbol = token.v.symbol;
3572 last_designator->next = designator;
3573 last_designator = designator;
3576 if(token.type == '[') {
3578 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3579 designator->array_access = parse_expression();
3580 if(designator->array_access == NULL) {
3586 last_designator->next = designator;
3587 last_designator = designator;
3596 static expression_t *parse_offsetof(void)
3598 eat(T___builtin_offsetof);
3600 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3601 expression->base.type = type_size_t;
3604 expression->offsetofe.type = parse_typename();
3606 expression->offsetofe.designator = parse_designator();
3612 static expression_t *parse_va_start(void)
3614 eat(T___builtin_va_start);
3616 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3619 expression->va_starte.ap = parse_assignment_expression();
3621 expression_t *const expr = parse_assignment_expression();
3622 if (expr->kind == EXPR_REFERENCE) {
3623 declaration_t *const decl = expr->reference.declaration;
3625 return create_invalid_expression();
3626 if (decl->parent_scope == ¤t_function->scope &&
3627 decl->next == NULL) {
3628 expression->va_starte.parameter = decl;
3633 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3635 return create_invalid_expression();
3638 static expression_t *parse_va_arg(void)
3640 eat(T___builtin_va_arg);
3642 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3645 expression->va_arge.ap = parse_assignment_expression();
3647 expression->base.type = parse_typename();
3653 static expression_t *parse_builtin_symbol(void)
3655 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3657 symbol_t *symbol = token.v.symbol;
3659 expression->builtin_symbol.symbol = symbol;
3662 type_t *type = get_builtin_symbol_type(symbol);
3663 type = automatic_type_conversion(type);
3665 expression->base.type = type;
3669 static expression_t *parse_builtin_constant(void)
3671 eat(T___builtin_constant_p);
3673 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3676 expression->builtin_constant.value = parse_assignment_expression();
3678 expression->base.type = type_int;
3683 static expression_t *parse_builtin_prefetch(void)
3685 eat(T___builtin_prefetch);
3687 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3690 expression->builtin_prefetch.adr = parse_assignment_expression();
3691 if (token.type == ',') {
3693 expression->builtin_prefetch.rw = parse_assignment_expression();
3695 if (token.type == ',') {
3697 expression->builtin_prefetch.locality = parse_assignment_expression();
3700 expression->base.type = type_void;
3705 static expression_t *parse_compare_builtin(void)
3707 expression_t *expression;
3709 switch(token.type) {
3710 case T___builtin_isgreater:
3711 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3713 case T___builtin_isgreaterequal:
3714 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3716 case T___builtin_isless:
3717 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3719 case T___builtin_islessequal:
3720 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3722 case T___builtin_islessgreater:
3723 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3725 case T___builtin_isunordered:
3726 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3729 panic("invalid compare builtin found");
3732 expression->base.source_position = HERE;
3736 expression->binary.left = parse_assignment_expression();
3738 expression->binary.right = parse_assignment_expression();
3741 type_t *const orig_type_left = expression->binary.left->base.type;
3742 type_t *const orig_type_right = expression->binary.right->base.type;
3744 type_t *const type_left = skip_typeref(orig_type_left);
3745 type_t *const type_right = skip_typeref(orig_type_right);
3746 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3747 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3748 type_error_incompatible("invalid operands in comparison",
3749 expression->base.source_position, orig_type_left, orig_type_right);
3752 semantic_comparison(&expression->binary);
3758 static expression_t *parse_builtin_expect(void)
3760 eat(T___builtin_expect);
3762 expression_t *expression
3763 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3766 expression->binary.left = parse_assignment_expression();
3768 expression->binary.right = parse_constant_expression();
3771 expression->base.type = expression->binary.left->base.type;
3776 static expression_t *parse_assume(void) {
3779 expression_t *expression
3780 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3783 expression->unary.value = parse_assignment_expression();
3786 expression->base.type = type_void;
3790 static expression_t *parse_primary_expression(void)
3792 switch(token.type) {
3794 return parse_int_const();
3796 return parse_char_const();
3797 case T_FLOATINGPOINT:
3798 return parse_float_const();
3799 case T_STRING_LITERAL:
3800 case T_WIDE_STRING_LITERAL:
3801 return parse_string_const();
3803 return parse_reference();
3804 case T___FUNCTION__:
3806 return parse_function_keyword();
3807 case T___PRETTY_FUNCTION__:
3808 return parse_pretty_function_keyword();
3809 case T___builtin_offsetof:
3810 return parse_offsetof();
3811 case T___builtin_va_start:
3812 return parse_va_start();
3813 case T___builtin_va_arg:
3814 return parse_va_arg();
3815 case T___builtin_expect:
3816 return parse_builtin_expect();
3817 case T___builtin_alloca:
3818 case T___builtin_nan:
3819 case T___builtin_nand:
3820 case T___builtin_nanf:
3821 case T___builtin_va_end:
3822 return parse_builtin_symbol();
3823 case T___builtin_isgreater:
3824 case T___builtin_isgreaterequal:
3825 case T___builtin_isless:
3826 case T___builtin_islessequal:
3827 case T___builtin_islessgreater:
3828 case T___builtin_isunordered:
3829 return parse_compare_builtin();
3830 case T___builtin_constant_p:
3831 return parse_builtin_constant();
3832 case T___builtin_prefetch:
3833 return parse_builtin_prefetch();
3835 return parse_assume();
3838 return parse_brace_expression();
3841 errorf(HERE, "unexpected token %K", &token);
3844 return create_invalid_expression();
3848 * Check if the expression has the character type and issue a warning then.
3850 static void check_for_char_index_type(const expression_t *expression) {
3851 type_t *const type = expression->base.type;
3852 const type_t *const base_type = skip_typeref(type);
3854 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3855 warning.char_subscripts) {
3856 warningf(expression->base.source_position,
3857 "array subscript has type '%T'", type);
3861 static expression_t *parse_array_expression(unsigned precedence,
3868 expression_t *inside = parse_expression();
3870 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3872 array_access_expression_t *array_access = &expression->array_access;
3874 type_t *const orig_type_left = left->base.type;
3875 type_t *const orig_type_inside = inside->base.type;
3877 type_t *const type_left = skip_typeref(orig_type_left);
3878 type_t *const type_inside = skip_typeref(orig_type_inside);
3880 type_t *return_type;
3881 if (is_type_pointer(type_left)) {
3882 return_type = type_left->pointer.points_to;
3883 array_access->array_ref = left;
3884 array_access->index = inside;
3885 check_for_char_index_type(inside);
3886 } else if (is_type_pointer(type_inside)) {
3887 return_type = type_inside->pointer.points_to;
3888 array_access->array_ref = inside;
3889 array_access->index = left;
3890 array_access->flipped = true;
3891 check_for_char_index_type(left);
3893 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3895 "array access on object with non-pointer types '%T', '%T'",
3896 orig_type_left, orig_type_inside);
3898 return_type = type_error_type;
3899 array_access->array_ref = create_invalid_expression();
3902 if(token.type != ']') {
3903 parse_error_expected("Problem while parsing array access", ']', 0);
3908 return_type = automatic_type_conversion(return_type);
3909 expression->base.type = return_type;
3914 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3916 expression_t *tp_expression = allocate_expression_zero(kind);
3917 tp_expression->base.type = type_size_t;
3919 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3921 tp_expression->typeprop.type = parse_typename();
3924 expression_t *expression = parse_sub_expression(precedence);
3925 expression->base.type = revert_automatic_type_conversion(expression);
3927 tp_expression->typeprop.type = expression->base.type;
3928 tp_expression->typeprop.tp_expression = expression;
3931 return tp_expression;
3934 static expression_t *parse_sizeof(unsigned precedence)
3937 return parse_typeprop(EXPR_SIZEOF, precedence);
3940 static expression_t *parse_alignof(unsigned precedence)
3943 return parse_typeprop(EXPR_SIZEOF, precedence);
3946 static expression_t *parse_select_expression(unsigned precedence,
3947 expression_t *compound)
3950 assert(token.type == '.' || token.type == T_MINUSGREATER);
3952 bool is_pointer = (token.type == T_MINUSGREATER);
3955 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3956 select->select.compound = compound;
3958 if(token.type != T_IDENTIFIER) {
3959 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3962 symbol_t *symbol = token.v.symbol;
3963 select->select.symbol = symbol;
3966 type_t *const orig_type = compound->base.type;
3967 type_t *const type = skip_typeref(orig_type);
3969 type_t *type_left = type;
3971 if (!is_type_pointer(type)) {
3972 if (is_type_valid(type)) {
3973 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3975 return create_invalid_expression();
3977 type_left = type->pointer.points_to;
3979 type_left = skip_typeref(type_left);
3981 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3982 type_left->kind != TYPE_COMPOUND_UNION) {
3983 if (is_type_valid(type_left)) {
3984 errorf(HERE, "request for member '%Y' in something not a struct or "
3985 "union, but '%T'", symbol, type_left);
3987 return create_invalid_expression();
3990 declaration_t *const declaration = type_left->compound.declaration;
3992 if(!declaration->init.is_defined) {
3993 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3995 return create_invalid_expression();
3998 declaration_t *iter = find_compound_entry(declaration, symbol);
4000 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4001 return create_invalid_expression();
4004 /* we always do the auto-type conversions; the & and sizeof parser contains
4005 * code to revert this! */
4006 type_t *expression_type = automatic_type_conversion(iter->type);
4008 select->select.compound_entry = iter;
4009 select->base.type = expression_type;
4011 if(expression_type->kind == TYPE_BITFIELD) {
4012 expression_t *extract
4013 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4014 extract->unary.value = select;
4015 extract->base.type = expression_type->bitfield.base;
4024 * Parse a call expression, ie. expression '( ... )'.
4026 * @param expression the function address
4028 static expression_t *parse_call_expression(unsigned precedence,
4029 expression_t *expression)
4032 expression_t *result = allocate_expression_zero(EXPR_CALL);
4034 call_expression_t *call = &result->call;
4035 call->function = expression;
4037 type_t *const orig_type = expression->base.type;
4038 type_t *const type = skip_typeref(orig_type);
4040 function_type_t *function_type = NULL;
4041 if (is_type_pointer(type)) {
4042 type_t *const to_type = skip_typeref(type->pointer.points_to);
4044 if (is_type_function(to_type)) {
4045 function_type = &to_type->function;
4046 call->base.type = function_type->return_type;
4050 if (function_type == NULL && is_type_valid(type)) {
4051 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4054 /* parse arguments */
4057 if(token.type != ')') {
4058 call_argument_t *last_argument = NULL;
4061 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4063 argument->expression = parse_assignment_expression();
4064 if(last_argument == NULL) {
4065 call->arguments = argument;
4067 last_argument->next = argument;
4069 last_argument = argument;
4071 if(token.type != ',')
4078 if(function_type != NULL) {
4079 function_parameter_t *parameter = function_type->parameters;
4080 call_argument_t *argument = call->arguments;
4081 for( ; parameter != NULL && argument != NULL;
4082 parameter = parameter->next, argument = argument->next) {
4083 type_t *expected_type = parameter->type;
4084 /* TODO report scope in error messages */
4085 expression_t *const arg_expr = argument->expression;
4086 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4087 if (res_type == NULL) {
4088 /* TODO improve error message */
4089 errorf(arg_expr->base.source_position,
4090 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4091 arg_expr, arg_expr->base.type, expected_type);
4093 argument->expression = create_implicit_cast(argument->expression, expected_type);
4096 /* too few parameters */
4097 if(parameter != NULL) {
4098 errorf(HERE, "too few arguments to function '%E'", expression);
4099 } else if(argument != NULL) {
4100 /* too many parameters */
4101 if(!function_type->variadic
4102 && !function_type->unspecified_parameters) {
4103 errorf(HERE, "too many arguments to function '%E'", expression);
4105 /* do default promotion */
4106 for( ; argument != NULL; argument = argument->next) {
4107 type_t *type = argument->expression->base.type;
4109 type = skip_typeref(type);
4110 if(is_type_integer(type)) {
4111 type = promote_integer(type);
4112 } else if(type == type_float) {
4116 argument->expression
4117 = create_implicit_cast(argument->expression, type);
4120 check_format(&result->call);
4123 check_format(&result->call);
4130 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4132 static bool same_compound_type(const type_t *type1, const type_t *type2)
4135 is_type_compound(type1) &&
4136 type1->kind == type2->kind &&
4137 type1->compound.declaration == type2->compound.declaration;
4141 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4143 * @param expression the conditional expression
4145 static expression_t *parse_conditional_expression(unsigned precedence,
4146 expression_t *expression)
4150 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4152 conditional_expression_t *conditional = &result->conditional;
4153 conditional->condition = expression;
4156 type_t *const condition_type_orig = expression->base.type;
4157 type_t *const condition_type = skip_typeref(condition_type_orig);
4158 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4159 type_error("expected a scalar type in conditional condition",
4160 expression->base.source_position, condition_type_orig);
4163 expression_t *true_expression = parse_expression();
4165 expression_t *false_expression = parse_sub_expression(precedence);
4167 conditional->true_expression = true_expression;
4168 conditional->false_expression = false_expression;
4170 type_t *const orig_true_type = true_expression->base.type;
4171 type_t *const orig_false_type = false_expression->base.type;
4172 type_t *const true_type = skip_typeref(orig_true_type);
4173 type_t *const false_type = skip_typeref(orig_false_type);
4176 type_t *result_type;
4177 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4178 result_type = semantic_arithmetic(true_type, false_type);
4180 true_expression = create_implicit_cast(true_expression, result_type);
4181 false_expression = create_implicit_cast(false_expression, result_type);
4183 conditional->true_expression = true_expression;
4184 conditional->false_expression = false_expression;
4185 conditional->base.type = result_type;
4186 } else if (same_compound_type(true_type, false_type) || (
4187 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4188 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4190 /* just take 1 of the 2 types */
4191 result_type = true_type;
4192 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4193 && pointers_compatible(true_type, false_type)) {
4195 result_type = true_type;
4196 } else if (is_type_pointer(true_type)
4197 && is_null_pointer_constant(false_expression)) {
4198 result_type = true_type;
4199 } else if (is_type_pointer(false_type)
4200 && is_null_pointer_constant(true_expression)) {
4201 result_type = false_type;
4203 /* TODO: one pointer to void*, other some pointer */
4205 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4206 type_error_incompatible("while parsing conditional",
4207 expression->base.source_position, true_type,
4210 result_type = type_error_type;
4213 conditional->base.type = result_type;
4218 * Parse an extension expression.
4220 static expression_t *parse_extension(unsigned precedence)
4222 eat(T___extension__);
4224 /* TODO enable extensions */
4225 expression_t *expression = parse_sub_expression(precedence);
4226 /* TODO disable extensions */
4230 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4232 eat(T___builtin_classify_type);
4234 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4235 result->base.type = type_int;
4238 expression_t *expression = parse_sub_expression(precedence);
4240 result->classify_type.type_expression = expression;
4245 static void semantic_incdec(unary_expression_t *expression)
4247 type_t *const orig_type = expression->value->base.type;
4248 type_t *const type = skip_typeref(orig_type);
4249 /* TODO !is_type_real && !is_type_pointer */
4250 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4251 if (is_type_valid(type)) {
4252 /* TODO: improve error message */
4253 errorf(HERE, "operation needs an arithmetic or pointer type");
4258 expression->base.type = orig_type;
4261 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4263 type_t *const orig_type = expression->value->base.type;
4264 type_t *const type = skip_typeref(orig_type);
4265 if(!is_type_arithmetic(type)) {
4266 if (is_type_valid(type)) {
4267 /* TODO: improve error message */
4268 errorf(HERE, "operation needs an arithmetic type");
4273 expression->base.type = orig_type;
4276 static void semantic_unexpr_scalar(unary_expression_t *expression)
4278 type_t *const orig_type = expression->value->base.type;
4279 type_t *const type = skip_typeref(orig_type);
4280 if (!is_type_scalar(type)) {
4281 if (is_type_valid(type)) {
4282 errorf(HERE, "operand of ! must be of scalar type");
4287 expression->base.type = orig_type;
4290 static void semantic_unexpr_integer(unary_expression_t *expression)
4292 type_t *const orig_type = expression->value->base.type;
4293 type_t *const type = skip_typeref(orig_type);
4294 if (!is_type_integer(type)) {
4295 if (is_type_valid(type)) {
4296 errorf(HERE, "operand of ~ must be of integer type");
4301 expression->base.type = orig_type;
4304 static void semantic_dereference(unary_expression_t *expression)
4306 type_t *const orig_type = expression->value->base.type;
4307 type_t *const type = skip_typeref(orig_type);
4308 if(!is_type_pointer(type)) {
4309 if (is_type_valid(type)) {
4310 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4315 type_t *result_type = type->pointer.points_to;
4316 result_type = automatic_type_conversion(result_type);
4317 expression->base.type = result_type;
4321 * Check the semantic of the address taken expression.
4323 static void semantic_take_addr(unary_expression_t *expression)
4325 expression_t *value = expression->value;
4326 value->base.type = revert_automatic_type_conversion(value);
4328 type_t *orig_type = value->base.type;
4329 if(!is_type_valid(orig_type))
4332 if(value->kind == EXPR_REFERENCE) {
4333 declaration_t *const declaration = value->reference.declaration;
4334 if(declaration != NULL) {
4335 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4336 errorf(expression->base.source_position,
4337 "address of register variable '%Y' requested",
4338 declaration->symbol);
4340 declaration->address_taken = 1;
4344 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4347 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4348 static expression_t *parse_##unexpression_type(unsigned precedence) \
4352 expression_t *unary_expression \
4353 = allocate_expression_zero(unexpression_type); \
4354 unary_expression->base.source_position = HERE; \
4355 unary_expression->unary.value = parse_sub_expression(precedence); \
4357 sfunc(&unary_expression->unary); \
4359 return unary_expression; \
4362 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4363 semantic_unexpr_arithmetic)
4364 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4365 semantic_unexpr_arithmetic)
4366 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4367 semantic_unexpr_scalar)
4368 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4369 semantic_dereference)
4370 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4372 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4373 semantic_unexpr_integer)
4374 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4376 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4379 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4381 static expression_t *parse_##unexpression_type(unsigned precedence, \
4382 expression_t *left) \
4384 (void) precedence; \
4387 expression_t *unary_expression \
4388 = allocate_expression_zero(unexpression_type); \
4389 unary_expression->unary.value = left; \
4391 sfunc(&unary_expression->unary); \
4393 return unary_expression; \
4396 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4397 EXPR_UNARY_POSTFIX_INCREMENT,
4399 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4400 EXPR_UNARY_POSTFIX_DECREMENT,
4403 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4405 /* TODO: handle complex + imaginary types */
4407 /* § 6.3.1.8 Usual arithmetic conversions */
4408 if(type_left == type_long_double || type_right == type_long_double) {
4409 return type_long_double;
4410 } else if(type_left == type_double || type_right == type_double) {
4412 } else if(type_left == type_float || type_right == type_float) {
4416 type_right = promote_integer(type_right);
4417 type_left = promote_integer(type_left);
4419 if(type_left == type_right)
4422 bool signed_left = is_type_signed(type_left);
4423 bool signed_right = is_type_signed(type_right);
4424 int rank_left = get_rank(type_left);
4425 int rank_right = get_rank(type_right);
4426 if(rank_left < rank_right) {
4427 if(signed_left == signed_right || !signed_right) {
4433 if(signed_left == signed_right || !signed_left) {
4442 * Check the semantic restrictions for a binary expression.
4444 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4446 expression_t *const left = expression->left;
4447 expression_t *const right = expression->right;
4448 type_t *const orig_type_left = left->base.type;
4449 type_t *const orig_type_right = right->base.type;
4450 type_t *const type_left = skip_typeref(orig_type_left);
4451 type_t *const type_right = skip_typeref(orig_type_right);
4453 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4454 /* TODO: improve error message */
4455 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4456 errorf(HERE, "operation needs arithmetic types");
4461 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4462 expression->left = create_implicit_cast(left, arithmetic_type);
4463 expression->right = create_implicit_cast(right, arithmetic_type);
4464 expression->base.type = arithmetic_type;
4467 static void semantic_shift_op(binary_expression_t *expression)
4469 expression_t *const left = expression->left;
4470 expression_t *const right = expression->right;
4471 type_t *const orig_type_left = left->base.type;
4472 type_t *const orig_type_right = right->base.type;
4473 type_t * type_left = skip_typeref(orig_type_left);
4474 type_t * type_right = skip_typeref(orig_type_right);
4476 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4477 /* TODO: improve error message */
4478 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4479 errorf(HERE, "operation needs integer types");
4484 type_left = promote_integer(type_left);
4485 type_right = promote_integer(type_right);
4487 expression->left = create_implicit_cast(left, type_left);
4488 expression->right = create_implicit_cast(right, type_right);
4489 expression->base.type = type_left;
4492 static void semantic_add(binary_expression_t *expression)
4494 expression_t *const left = expression->left;
4495 expression_t *const right = expression->right;
4496 type_t *const orig_type_left = left->base.type;
4497 type_t *const orig_type_right = right->base.type;
4498 type_t *const type_left = skip_typeref(orig_type_left);
4499 type_t *const type_right = skip_typeref(orig_type_right);
4502 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4503 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4504 expression->left = create_implicit_cast(left, arithmetic_type);
4505 expression->right = create_implicit_cast(right, arithmetic_type);
4506 expression->base.type = arithmetic_type;
4508 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4509 expression->base.type = type_left;
4510 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4511 expression->base.type = type_right;
4512 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4513 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4517 static void semantic_sub(binary_expression_t *expression)
4519 expression_t *const left = expression->left;
4520 expression_t *const right = expression->right;
4521 type_t *const orig_type_left = left->base.type;
4522 type_t *const orig_type_right = right->base.type;
4523 type_t *const type_left = skip_typeref(orig_type_left);
4524 type_t *const type_right = skip_typeref(orig_type_right);
4527 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4528 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4529 expression->left = create_implicit_cast(left, arithmetic_type);
4530 expression->right = create_implicit_cast(right, arithmetic_type);
4531 expression->base.type = arithmetic_type;
4533 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4534 expression->base.type = type_left;
4535 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4536 if(!pointers_compatible(type_left, type_right)) {
4538 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4539 orig_type_left, orig_type_right);
4541 expression->base.type = type_ptrdiff_t;
4543 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4544 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4545 orig_type_left, orig_type_right);
4550 * Check the semantics of comparison expressions.
4552 * @param expression The expression to check.
4554 static void semantic_comparison(binary_expression_t *expression)
4556 expression_t *left = expression->left;
4557 expression_t *right = expression->right;
4558 type_t *orig_type_left = left->base.type;
4559 type_t *orig_type_right = right->base.type;
4561 type_t *type_left = skip_typeref(orig_type_left);
4562 type_t *type_right = skip_typeref(orig_type_right);
4564 /* TODO non-arithmetic types */
4565 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4566 if (warning.sign_compare &&
4567 (expression->base.kind != EXPR_BINARY_EQUAL &&
4568 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4569 (is_type_signed(type_left) != is_type_signed(type_right))) {
4570 warningf(expression->base.source_position,
4571 "comparison between signed and unsigned");
4573 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4574 expression->left = create_implicit_cast(left, arithmetic_type);
4575 expression->right = create_implicit_cast(right, arithmetic_type);
4576 expression->base.type = arithmetic_type;
4577 if (warning.float_equal &&
4578 (expression->base.kind == EXPR_BINARY_EQUAL ||
4579 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4580 is_type_float(arithmetic_type)) {
4581 warningf(expression->base.source_position,
4582 "comparing floating point with == or != is unsafe");
4584 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4585 /* TODO check compatibility */
4586 } else if (is_type_pointer(type_left)) {
4587 expression->right = create_implicit_cast(right, type_left);
4588 } else if (is_type_pointer(type_right)) {
4589 expression->left = create_implicit_cast(left, type_right);
4590 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4591 type_error_incompatible("invalid operands in comparison",
4592 expression->base.source_position,
4593 type_left, type_right);
4595 expression->base.type = type_int;
4598 static void semantic_arithmetic_assign(binary_expression_t *expression)
4600 expression_t *left = expression->left;
4601 expression_t *right = expression->right;
4602 type_t *orig_type_left = left->base.type;
4603 type_t *orig_type_right = right->base.type;
4605 type_t *type_left = skip_typeref(orig_type_left);
4606 type_t *type_right = skip_typeref(orig_type_right);
4608 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4609 /* TODO: improve error message */
4610 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4611 errorf(HERE, "operation needs arithmetic types");
4616 /* combined instructions are tricky. We can't create an implicit cast on
4617 * the left side, because we need the uncasted form for the store.
4618 * The ast2firm pass has to know that left_type must be right_type
4619 * for the arithmetic operation and create a cast by itself */
4620 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4621 expression->right = create_implicit_cast(right, arithmetic_type);
4622 expression->base.type = type_left;
4625 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4627 expression_t *const left = expression->left;
4628 expression_t *const right = expression->right;
4629 type_t *const orig_type_left = left->base.type;
4630 type_t *const orig_type_right = right->base.type;
4631 type_t *const type_left = skip_typeref(orig_type_left);
4632 type_t *const type_right = skip_typeref(orig_type_right);
4634 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4635 /* combined instructions are tricky. We can't create an implicit cast on
4636 * the left side, because we need the uncasted form for the store.
4637 * The ast2firm pass has to know that left_type must be right_type
4638 * for the arithmetic operation and create a cast by itself */
4639 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4640 expression->right = create_implicit_cast(right, arithmetic_type);
4641 expression->base.type = type_left;
4642 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4643 expression->base.type = type_left;
4644 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4645 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4650 * Check the semantic restrictions of a logical expression.
4652 static void semantic_logical_op(binary_expression_t *expression)
4654 expression_t *const left = expression->left;
4655 expression_t *const right = expression->right;
4656 type_t *const orig_type_left = left->base.type;
4657 type_t *const orig_type_right = right->base.type;
4658 type_t *const type_left = skip_typeref(orig_type_left);
4659 type_t *const type_right = skip_typeref(orig_type_right);
4661 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4662 /* TODO: improve error message */
4663 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4664 errorf(HERE, "operation needs scalar types");
4669 expression->base.type = type_int;
4673 * Checks if a compound type has constant fields.
4675 static bool has_const_fields(const compound_type_t *type)
4677 const scope_t *scope = &type->declaration->scope;
4678 const declaration_t *declaration = scope->declarations;
4680 for (; declaration != NULL; declaration = declaration->next) {
4681 if (declaration->namespc != NAMESPACE_NORMAL)
4684 const type_t *decl_type = skip_typeref(declaration->type);
4685 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4693 * Check the semantic restrictions of a binary assign expression.
4695 static void semantic_binexpr_assign(binary_expression_t *expression)
4697 expression_t *left = expression->left;
4698 type_t *orig_type_left = left->base.type;
4700 type_t *type_left = revert_automatic_type_conversion(left);
4701 type_left = skip_typeref(orig_type_left);
4703 /* must be a modifiable lvalue */
4704 if (is_type_array(type_left)) {
4705 errorf(HERE, "cannot assign to arrays ('%E')", left);
4708 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4709 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4713 if(is_type_incomplete(type_left)) {
4715 "left-hand side of assignment '%E' has incomplete type '%T'",
4716 left, orig_type_left);
4719 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4720 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4721 left, orig_type_left);
4725 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4727 if (res_type == NULL) {
4728 errorf(expression->base.source_position,
4729 "cannot assign to '%T' from '%T'",
4730 orig_type_left, expression->right->base.type);
4732 expression->right = create_implicit_cast(expression->right, res_type);
4735 expression->base.type = orig_type_left;
4738 static bool expression_has_effect(const expression_t *const expr)
4740 switch (expr->kind) {
4741 case EXPR_UNKNOWN: break;
4742 case EXPR_INVALID: break;
4743 case EXPR_REFERENCE: return false;
4744 case EXPR_CONST: return false;
4745 case EXPR_CHAR_CONST: return false;
4746 case EXPR_STRING_LITERAL: return false;
4747 case EXPR_WIDE_STRING_LITERAL: return false;
4749 const call_expression_t *const call = &expr->call;
4750 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4753 switch (call->function->builtin_symbol.symbol->ID) {
4754 case T___builtin_va_end: return true;
4755 default: return false;
4758 case EXPR_CONDITIONAL: {
4759 const conditional_expression_t *const cond = &expr->conditional;
4761 expression_has_effect(cond->true_expression) &&
4762 expression_has_effect(cond->false_expression);
4764 case EXPR_SELECT: return false;
4765 case EXPR_ARRAY_ACCESS: return false;
4766 case EXPR_SIZEOF: return false;
4767 case EXPR_CLASSIFY_TYPE: return false;
4768 case EXPR_ALIGNOF: return false;
4770 case EXPR_FUNCTION: return false;
4771 case EXPR_PRETTY_FUNCTION: return false;
4772 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4773 case EXPR_BUILTIN_CONSTANT_P: return false;
4774 case EXPR_BUILTIN_PREFETCH: return true;
4775 case EXPR_OFFSETOF: return false;
4776 case EXPR_VA_START: return true;
4777 case EXPR_VA_ARG: return true;
4778 case EXPR_STATEMENT: return true; // TODO
4780 case EXPR_UNARY_NEGATE: return false;
4781 case EXPR_UNARY_PLUS: return false;
4782 case EXPR_UNARY_BITWISE_NEGATE: return false;
4783 case EXPR_UNARY_NOT: return false;
4784 case EXPR_UNARY_DEREFERENCE: return false;
4785 case EXPR_UNARY_TAKE_ADDRESS: return false;
4786 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4787 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4788 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4789 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4790 case EXPR_UNARY_CAST:
4791 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4792 case EXPR_UNARY_CAST_IMPLICIT: return true;
4793 case EXPR_UNARY_ASSUME: return true;
4794 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4796 case EXPR_BINARY_ADD: return false;
4797 case EXPR_BINARY_SUB: return false;
4798 case EXPR_BINARY_MUL: return false;
4799 case EXPR_BINARY_DIV: return false;
4800 case EXPR_BINARY_MOD: return false;
4801 case EXPR_BINARY_EQUAL: return false;
4802 case EXPR_BINARY_NOTEQUAL: return false;
4803 case EXPR_BINARY_LESS: return false;
4804 case EXPR_BINARY_LESSEQUAL: return false;
4805 case EXPR_BINARY_GREATER: return false;
4806 case EXPR_BINARY_GREATEREQUAL: return false;
4807 case EXPR_BINARY_BITWISE_AND: return false;
4808 case EXPR_BINARY_BITWISE_OR: return false;
4809 case EXPR_BINARY_BITWISE_XOR: return false;
4810 case EXPR_BINARY_SHIFTLEFT: return false;
4811 case EXPR_BINARY_SHIFTRIGHT: return false;
4812 case EXPR_BINARY_ASSIGN: return true;
4813 case EXPR_BINARY_MUL_ASSIGN: return true;
4814 case EXPR_BINARY_DIV_ASSIGN: return true;
4815 case EXPR_BINARY_MOD_ASSIGN: return true;
4816 case EXPR_BINARY_ADD_ASSIGN: return true;
4817 case EXPR_BINARY_SUB_ASSIGN: return true;
4818 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4819 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4820 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4821 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4822 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4823 case EXPR_BINARY_LOGICAL_AND:
4824 case EXPR_BINARY_LOGICAL_OR:
4825 case EXPR_BINARY_COMMA:
4826 return expression_has_effect(expr->binary.right);
4828 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4829 case EXPR_BINARY_ISGREATER: return false;
4830 case EXPR_BINARY_ISGREATEREQUAL: return false;
4831 case EXPR_BINARY_ISLESS: return false;
4832 case EXPR_BINARY_ISLESSEQUAL: return false;
4833 case EXPR_BINARY_ISLESSGREATER: return false;
4834 case EXPR_BINARY_ISUNORDERED: return false;
4837 panic("unexpected statement");
4840 static void semantic_comma(binary_expression_t *expression)
4842 if (warning.unused_value) {
4843 const expression_t *const left = expression->left;
4844 if (!expression_has_effect(left)) {
4845 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4848 expression->base.type = expression->right->base.type;
4851 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4852 static expression_t *parse_##binexpression_type(unsigned precedence, \
4853 expression_t *left) \
4856 source_position_t pos = HERE; \
4858 expression_t *right = parse_sub_expression(precedence + lr); \
4860 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4861 binexpr->base.source_position = pos; \
4862 binexpr->binary.left = left; \
4863 binexpr->binary.right = right; \
4864 sfunc(&binexpr->binary); \
4869 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4870 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4871 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4872 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4873 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4874 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4875 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4876 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4877 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4879 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4880 semantic_comparison, 1)
4881 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4882 semantic_comparison, 1)
4883 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4884 semantic_comparison, 1)
4885 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4886 semantic_comparison, 1)
4888 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4889 semantic_binexpr_arithmetic, 1)
4890 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4891 semantic_binexpr_arithmetic, 1)
4892 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4893 semantic_binexpr_arithmetic, 1)
4894 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4895 semantic_logical_op, 1)
4896 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4897 semantic_logical_op, 1)
4898 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4899 semantic_shift_op, 1)
4900 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4901 semantic_shift_op, 1)
4902 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4903 semantic_arithmetic_addsubb_assign, 0)
4904 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4905 semantic_arithmetic_addsubb_assign, 0)
4906 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4907 semantic_arithmetic_assign, 0)
4908 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4909 semantic_arithmetic_assign, 0)
4910 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4911 semantic_arithmetic_assign, 0)
4912 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4913 semantic_arithmetic_assign, 0)
4914 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4915 semantic_arithmetic_assign, 0)
4916 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4917 semantic_arithmetic_assign, 0)
4918 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4919 semantic_arithmetic_assign, 0)
4920 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4921 semantic_arithmetic_assign, 0)
4923 static expression_t *parse_sub_expression(unsigned precedence)
4925 if(token.type < 0) {
4926 return expected_expression_error();
4929 expression_parser_function_t *parser
4930 = &expression_parsers[token.type];
4931 source_position_t source_position = token.source_position;
4934 if(parser->parser != NULL) {
4935 left = parser->parser(parser->precedence);
4937 left = parse_primary_expression();
4939 assert(left != NULL);
4940 left->base.source_position = source_position;
4943 if(token.type < 0) {
4944 return expected_expression_error();
4947 parser = &expression_parsers[token.type];
4948 if(parser->infix_parser == NULL)
4950 if(parser->infix_precedence < precedence)
4953 left = parser->infix_parser(parser->infix_precedence, left);
4955 assert(left != NULL);
4956 assert(left->kind != EXPR_UNKNOWN);
4957 left->base.source_position = source_position;
4964 * Parse an expression.
4966 static expression_t *parse_expression(void)
4968 return parse_sub_expression(1);
4972 * Register a parser for a prefix-like operator with given precedence.
4974 * @param parser the parser function
4975 * @param token_type the token type of the prefix token
4976 * @param precedence the precedence of the operator
4978 static void register_expression_parser(parse_expression_function parser,
4979 int token_type, unsigned precedence)
4981 expression_parser_function_t *entry = &expression_parsers[token_type];
4983 if(entry->parser != NULL) {
4984 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4985 panic("trying to register multiple expression parsers for a token");
4987 entry->parser = parser;
4988 entry->precedence = precedence;
4992 * Register a parser for an infix operator with given precedence.
4994 * @param parser the parser function
4995 * @param token_type the token type of the infix operator
4996 * @param precedence the precedence of the operator
4998 static void register_infix_parser(parse_expression_infix_function parser,
4999 int token_type, unsigned precedence)
5001 expression_parser_function_t *entry = &expression_parsers[token_type];
5003 if(entry->infix_parser != NULL) {
5004 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5005 panic("trying to register multiple infix expression parsers for a "
5008 entry->infix_parser = parser;
5009 entry->infix_precedence = precedence;
5013 * Initialize the expression parsers.
5015 static void init_expression_parsers(void)
5017 memset(&expression_parsers, 0, sizeof(expression_parsers));
5019 register_infix_parser(parse_array_expression, '[', 30);
5020 register_infix_parser(parse_call_expression, '(', 30);
5021 register_infix_parser(parse_select_expression, '.', 30);
5022 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5023 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5025 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5028 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5029 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5030 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5031 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5032 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5033 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5034 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5035 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5036 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5037 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5038 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5039 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5040 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5041 T_EXCLAMATIONMARKEQUAL, 13);
5042 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5043 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5044 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5045 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5046 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5047 register_infix_parser(parse_conditional_expression, '?', 7);
5048 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5049 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5050 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5051 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5052 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5053 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5054 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5055 T_LESSLESSEQUAL, 2);
5056 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5057 T_GREATERGREATEREQUAL, 2);
5058 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5060 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5062 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5065 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5067 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5068 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5069 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5070 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5071 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5072 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5073 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5075 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5077 register_expression_parser(parse_sizeof, T_sizeof, 25);
5078 register_expression_parser(parse_alignof, T___alignof__, 25);
5079 register_expression_parser(parse_extension, T___extension__, 25);
5080 register_expression_parser(parse_builtin_classify_type,
5081 T___builtin_classify_type, 25);
5085 * Parse a asm statement constraints specification.
5087 static asm_constraint_t *parse_asm_constraints(void)
5089 asm_constraint_t *result = NULL;
5090 asm_constraint_t *last = NULL;
5092 while(token.type == T_STRING_LITERAL || token.type == '[') {
5093 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5094 memset(constraint, 0, sizeof(constraint[0]));
5096 if(token.type == '[') {
5098 if(token.type != T_IDENTIFIER) {
5099 parse_error_expected("while parsing asm constraint",
5103 constraint->symbol = token.v.symbol;
5108 constraint->constraints = parse_string_literals();
5110 constraint->expression = parse_expression();
5114 last->next = constraint;
5116 result = constraint;
5120 if(token.type != ',')
5129 * Parse a asm statement clobber specification.
5131 static asm_clobber_t *parse_asm_clobbers(void)
5133 asm_clobber_t *result = NULL;
5134 asm_clobber_t *last = NULL;
5136 while(token.type == T_STRING_LITERAL) {
5137 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5138 clobber->clobber = parse_string_literals();
5141 last->next = clobber;
5147 if(token.type != ',')
5156 * Parse an asm statement.
5158 static statement_t *parse_asm_statement(void)
5162 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5163 statement->base.source_position = token.source_position;
5165 asm_statement_t *asm_statement = &statement->asms;
5167 if(token.type == T_volatile) {
5169 asm_statement->is_volatile = true;
5173 asm_statement->asm_text = parse_string_literals();
5175 if(token.type != ':')
5179 asm_statement->inputs = parse_asm_constraints();
5180 if(token.type != ':')
5184 asm_statement->outputs = parse_asm_constraints();
5185 if(token.type != ':')
5189 asm_statement->clobbers = parse_asm_clobbers();
5198 * Parse a case statement.
5200 static statement_t *parse_case_statement(void)
5204 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5206 statement->base.source_position = token.source_position;
5207 statement->case_label.expression = parse_expression();
5209 if (c_mode & _GNUC) {
5210 if (token.type == T_DOTDOTDOT) {
5212 statement->case_label.end_range = parse_expression();
5218 if (! is_constant_expression(statement->case_label.expression)) {
5219 errorf(statement->base.source_position,
5220 "case label does not reduce to an integer constant");
5222 /* TODO: check if the case label is already known */
5223 if (current_switch != NULL) {
5224 /* link all cases into the switch statement */
5225 if (current_switch->last_case == NULL) {
5226 current_switch->first_case =
5227 current_switch->last_case = &statement->case_label;
5229 current_switch->last_case->next = &statement->case_label;
5232 errorf(statement->base.source_position,
5233 "case label not within a switch statement");
5236 statement->case_label.statement = parse_statement();
5242 * Finds an existing default label of a switch statement.
5244 static case_label_statement_t *
5245 find_default_label(const switch_statement_t *statement)
5247 case_label_statement_t *label = statement->first_case;
5248 for ( ; label != NULL; label = label->next) {
5249 if (label->expression == NULL)
5256 * Parse a default statement.
5258 static statement_t *parse_default_statement(void)
5262 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5264 statement->base.source_position = token.source_position;
5267 if (current_switch != NULL) {
5268 const case_label_statement_t *def_label = find_default_label(current_switch);
5269 if (def_label != NULL) {
5270 errorf(HERE, "multiple default labels in one switch");
5271 errorf(def_label->base.source_position,
5272 "this is the first default label");
5274 /* link all cases into the switch statement */
5275 if (current_switch->last_case == NULL) {
5276 current_switch->first_case =
5277 current_switch->last_case = &statement->case_label;
5279 current_switch->last_case->next = &statement->case_label;
5283 errorf(statement->base.source_position,
5284 "'default' label not within a switch statement");
5286 statement->case_label.statement = parse_statement();
5292 * Return the declaration for a given label symbol or create a new one.
5294 static declaration_t *get_label(symbol_t *symbol)
5296 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5297 assert(current_function != NULL);
5298 /* if we found a label in the same function, then we already created the
5300 if(candidate != NULL
5301 && candidate->parent_scope == ¤t_function->scope) {
5305 /* otherwise we need to create a new one */
5306 declaration_t *const declaration = allocate_declaration_zero();
5307 declaration->namespc = NAMESPACE_LABEL;
5308 declaration->symbol = symbol;
5310 label_push(declaration);
5316 * Parse a label statement.
5318 static statement_t *parse_label_statement(void)
5320 assert(token.type == T_IDENTIFIER);
5321 symbol_t *symbol = token.v.symbol;
5324 declaration_t *label = get_label(symbol);
5326 /* if source position is already set then the label is defined twice,
5327 * otherwise it was just mentioned in a goto so far */
5328 if(label->source_position.input_name != NULL) {
5329 errorf(HERE, "duplicate label '%Y'", symbol);
5330 errorf(label->source_position, "previous definition of '%Y' was here",
5333 label->source_position = token.source_position;
5336 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5338 statement->base.source_position = token.source_position;
5339 statement->label.label = label;
5343 if(token.type == '}') {
5344 /* TODO only warn? */
5345 errorf(HERE, "label at end of compound statement");
5348 if (token.type == ';') {
5349 /* eat an empty statement here, to avoid the warning about an empty
5350 * after a label. label:; is commonly used to have a label before
5354 statement->label.statement = parse_statement();
5358 /* remember the labels's in a list for later checking */
5359 if (label_last == NULL) {
5360 label_first = &statement->label;
5362 label_last->next = &statement->label;
5364 label_last = &statement->label;
5370 * Parse an if statement.
5372 static statement_t *parse_if(void)
5376 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5377 statement->base.source_position = token.source_position;
5380 statement->ifs.condition = parse_expression();
5383 statement->ifs.true_statement = parse_statement();
5384 if(token.type == T_else) {
5386 statement->ifs.false_statement = parse_statement();
5393 * Parse a switch statement.
5395 static statement_t *parse_switch(void)
5399 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5400 statement->base.source_position = token.source_position;
5403 expression_t *const expr = parse_expression();
5404 type_t * type = skip_typeref(expr->base.type);
5405 if (is_type_integer(type)) {
5406 type = promote_integer(type);
5407 } else if (is_type_valid(type)) {
5408 errorf(expr->base.source_position,
5409 "switch quantity is not an integer, but '%T'", type);
5410 type = type_error_type;
5412 statement->switchs.expression = create_implicit_cast(expr, type);
5415 switch_statement_t *rem = current_switch;
5416 current_switch = &statement->switchs;
5417 statement->switchs.body = parse_statement();
5418 current_switch = rem;
5420 if (warning.switch_default
5421 && find_default_label(&statement->switchs) == NULL) {
5422 warningf(statement->base.source_position, "switch has no default case");
5428 static statement_t *parse_loop_body(statement_t *const loop)
5430 statement_t *const rem = current_loop;
5431 current_loop = loop;
5433 statement_t *const body = parse_statement();
5440 * Parse a while statement.
5442 static statement_t *parse_while(void)
5446 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5447 statement->base.source_position = token.source_position;
5450 statement->whiles.condition = parse_expression();
5453 statement->whiles.body = parse_loop_body(statement);
5459 * Parse a do statement.
5461 static statement_t *parse_do(void)
5465 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5467 statement->base.source_position = token.source_position;
5469 statement->do_while.body = parse_loop_body(statement);
5473 statement->do_while.condition = parse_expression();
5481 * Parse a for statement.
5483 static statement_t *parse_for(void)
5487 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5488 statement->base.source_position = token.source_position;
5492 int top = environment_top();
5493 scope_t *last_scope = scope;
5494 set_scope(&statement->fors.scope);
5496 if(token.type != ';') {
5497 if(is_declaration_specifier(&token, false)) {
5498 parse_declaration(record_declaration);
5500 expression_t *const init = parse_expression();
5501 statement->fors.initialisation = init;
5502 if (warning.unused_value && !expression_has_effect(init)) {
5503 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5511 if(token.type != ';') {
5512 statement->fors.condition = parse_expression();
5515 if(token.type != ')') {
5516 expression_t *const step = parse_expression();
5517 statement->fors.step = step;
5518 if (warning.unused_value && !expression_has_effect(step)) {
5519 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5523 statement->fors.body = parse_loop_body(statement);
5525 assert(scope == &statement->fors.scope);
5526 set_scope(last_scope);
5527 environment_pop_to(top);
5533 * Parse a goto statement.
5535 static statement_t *parse_goto(void)
5539 if(token.type != T_IDENTIFIER) {
5540 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5544 symbol_t *symbol = token.v.symbol;
5547 declaration_t *label = get_label(symbol);
5549 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5550 statement->base.source_position = token.source_position;
5552 statement->gotos.label = label;
5554 /* remember the goto's in a list for later checking */
5555 if (goto_last == NULL) {
5556 goto_first = &statement->gotos;
5558 goto_last->next = &statement->gotos;
5560 goto_last = &statement->gotos;
5568 * Parse a continue statement.
5570 static statement_t *parse_continue(void)
5572 statement_t *statement;
5573 if (current_loop == NULL) {
5574 errorf(HERE, "continue statement not within loop");
5577 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5579 statement->base.source_position = token.source_position;
5589 * Parse a break statement.
5591 static statement_t *parse_break(void)
5593 statement_t *statement;
5594 if (current_switch == NULL && current_loop == NULL) {
5595 errorf(HERE, "break statement not within loop or switch");
5598 statement = allocate_statement_zero(STATEMENT_BREAK);
5600 statement->base.source_position = token.source_position;
5610 * Check if a given declaration represents a local variable.
5612 static bool is_local_var_declaration(const declaration_t *declaration) {
5613 switch ((storage_class_tag_t) declaration->storage_class) {
5614 case STORAGE_CLASS_NONE:
5615 case STORAGE_CLASS_AUTO:
5616 case STORAGE_CLASS_REGISTER: {
5617 const type_t *type = skip_typeref(declaration->type);
5618 if(is_type_function(type)) {
5630 * Check if a given declaration represents a variable.
5632 static bool is_var_declaration(const declaration_t *declaration) {
5633 switch ((storage_class_tag_t) declaration->storage_class) {
5634 case STORAGE_CLASS_NONE:
5635 case STORAGE_CLASS_EXTERN:
5636 case STORAGE_CLASS_STATIC:
5637 case STORAGE_CLASS_AUTO:
5638 case STORAGE_CLASS_REGISTER:
5639 case STORAGE_CLASS_THREAD:
5640 case STORAGE_CLASS_THREAD_EXTERN:
5641 case STORAGE_CLASS_THREAD_STATIC: {
5642 const type_t *type = skip_typeref(declaration->type);
5643 if(is_type_function(type)) {
5655 * Check if a given expression represents a local variable.
5657 static bool is_local_variable(const expression_t *expression)
5659 if (expression->base.kind != EXPR_REFERENCE) {
5662 const declaration_t *declaration = expression->reference.declaration;
5663 return is_local_var_declaration(declaration);
5667 * Check if a given expression represents a local variable and
5668 * return its declaration then, else return NULL.
5670 declaration_t *expr_is_variable(const expression_t *expression)
5672 if (expression->base.kind != EXPR_REFERENCE) {
5675 declaration_t *declaration = expression->reference.declaration;
5676 if (is_var_declaration(declaration))
5682 * Parse a return statement.
5684 static statement_t *parse_return(void)
5688 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5689 statement->base.source_position = token.source_position;
5691 expression_t *return_value = NULL;
5692 if(token.type != ';') {
5693 return_value = parse_expression();
5697 const type_t *const func_type = current_function->type;
5698 assert(is_type_function(func_type));
5699 type_t *const return_type = skip_typeref(func_type->function.return_type);
5701 if(return_value != NULL) {
5702 type_t *return_value_type = skip_typeref(return_value->base.type);
5704 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5705 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5706 warningf(statement->base.source_position,
5707 "'return' with a value, in function returning void");
5708 return_value = NULL;
5710 type_t *const res_type = semantic_assign(return_type,
5711 return_value, "'return'");
5712 if (res_type == NULL) {
5713 errorf(statement->base.source_position,
5714 "cannot return something of type '%T' in function returning '%T'",
5715 return_value->base.type, return_type);
5717 return_value = create_implicit_cast(return_value, res_type);
5720 /* check for returning address of a local var */
5721 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5722 const expression_t *expression = return_value->unary.value;
5723 if (is_local_variable(expression)) {
5724 warningf(statement->base.source_position,
5725 "function returns address of local variable");
5729 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5730 warningf(statement->base.source_position,
5731 "'return' without value, in function returning non-void");
5734 statement->returns.value = return_value;
5740 * Parse a declaration statement.
5742 static statement_t *parse_declaration_statement(void)
5744 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5746 statement->base.source_position = token.source_position;
5748 declaration_t *before = last_declaration;
5749 parse_declaration(record_declaration);
5751 if(before == NULL) {
5752 statement->declaration.declarations_begin = scope->declarations;
5754 statement->declaration.declarations_begin = before->next;
5756 statement->declaration.declarations_end = last_declaration;
5762 * Parse an expression statement, ie. expr ';'.
5764 static statement_t *parse_expression_statement(void)
5766 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5768 statement->base.source_position = token.source_position;
5769 expression_t *const expr = parse_expression();
5770 statement->expression.expression = expr;
5772 if (warning.unused_value && !expression_has_effect(expr)) {
5773 warningf(expr->base.source_position, "statement has no effect");
5782 * Parse a statement.
5784 static statement_t *parse_statement(void)
5786 statement_t *statement = NULL;
5788 /* declaration or statement */
5789 switch(token.type) {
5791 statement = parse_asm_statement();
5795 statement = parse_case_statement();
5799 statement = parse_default_statement();
5803 statement = parse_compound_statement();
5807 statement = parse_if();
5811 statement = parse_switch();
5815 statement = parse_while();
5819 statement = parse_do();
5823 statement = parse_for();
5827 statement = parse_goto();
5831 statement = parse_continue();
5835 statement = parse_break();
5839 statement = parse_return();
5843 if (warning.empty_statement) {
5844 warningf(HERE, "statement is empty");
5851 if(look_ahead(1)->type == ':') {
5852 statement = parse_label_statement();
5856 if(is_typedef_symbol(token.v.symbol)) {
5857 statement = parse_declaration_statement();
5861 statement = parse_expression_statement();
5864 case T___extension__:
5865 /* this can be a prefix to a declaration or an expression statement */
5866 /* we simply eat it now and parse the rest with tail recursion */
5869 } while(token.type == T___extension__);
5870 statement = parse_statement();
5874 statement = parse_declaration_statement();
5878 statement = parse_expression_statement();
5882 assert(statement == NULL
5883 || statement->base.source_position.input_name != NULL);
5889 * Parse a compound statement.
5891 static statement_t *parse_compound_statement(void)
5893 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5895 statement->base.source_position = token.source_position;
5899 int top = environment_top();
5900 scope_t *last_scope = scope;
5901 set_scope(&statement->compound.scope);
5903 statement_t *last_statement = NULL;
5905 while(token.type != '}' && token.type != T_EOF) {
5906 statement_t *sub_statement = parse_statement();
5907 if(sub_statement == NULL)
5910 if(last_statement != NULL) {
5911 last_statement->base.next = sub_statement;
5913 statement->compound.statements = sub_statement;
5916 while(sub_statement->base.next != NULL)
5917 sub_statement = sub_statement->base.next;
5919 last_statement = sub_statement;
5922 if(token.type == '}') {
5925 errorf(statement->base.source_position,
5926 "end of file while looking for closing '}'");
5929 assert(scope == &statement->compound.scope);
5930 set_scope(last_scope);
5931 environment_pop_to(top);
5937 * Initialize builtin types.
5939 static void initialize_builtin_types(void)
5941 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5942 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5943 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5944 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5945 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5946 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5947 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5948 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5950 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5951 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5952 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5953 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5957 * Check for unused global static functions and variables
5959 static void check_unused_globals(void)
5961 if (!warning.unused_function && !warning.unused_variable)
5964 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5965 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5968 type_t *const type = decl->type;
5970 if (is_type_function(skip_typeref(type))) {
5971 if (!warning.unused_function || decl->is_inline)
5974 s = (decl->init.statement != NULL ? "defined" : "declared");
5976 if (!warning.unused_variable)
5982 warningf(decl->source_position, "'%#T' %s but not used",
5983 type, decl->symbol, s);
5988 * Parse a translation unit.
5990 static translation_unit_t *parse_translation_unit(void)
5992 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5994 assert(global_scope == NULL);
5995 global_scope = &unit->scope;
5997 assert(scope == NULL);
5998 set_scope(&unit->scope);
6000 initialize_builtin_types();
6002 while(token.type != T_EOF) {
6003 if (token.type == ';') {
6004 /* TODO error in strict mode */
6005 warningf(HERE, "stray ';' outside of function");
6008 parse_external_declaration();
6012 assert(scope == &unit->scope);
6014 last_declaration = NULL;
6016 assert(global_scope == &unit->scope);
6017 check_unused_globals();
6018 global_scope = NULL;
6026 * @return the translation unit or NULL if errors occurred.
6028 translation_unit_t *parse(void)
6030 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6031 label_stack = NEW_ARR_F(stack_entry_t, 0);
6032 diagnostic_count = 0;
6036 type_set_output(stderr);
6037 ast_set_output(stderr);
6039 lookahead_bufpos = 0;
6040 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6043 translation_unit_t *unit = parse_translation_unit();
6045 DEL_ARR_F(environment_stack);
6046 DEL_ARR_F(label_stack);
6055 * Initialize the parser.
6057 void init_parser(void)
6059 init_expression_parsers();
6060 obstack_init(&temp_obst);
6062 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6063 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6067 * Terminate the parser.
6069 void exit_parser(void)
6071 obstack_free(&temp_obst, NULL);