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 /* remember old declaration */
533 entry.symbol = symbol;
534 entry.old_declaration = symbol->declaration;
535 entry.namespc = (unsigned short) namespc;
536 ARR_APP1(stack_entry_t, *stack_ptr, entry);
538 /* replace/add declaration into declaration list of the symbol */
539 if(symbol->declaration == NULL) {
540 symbol->declaration = declaration;
542 declaration_t *iter_last = NULL;
543 declaration_t *iter = symbol->declaration;
544 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
545 /* replace an entry? */
546 if(iter->namespc == namespc) {
547 if(iter_last == NULL) {
548 symbol->declaration = declaration;
550 iter_last->symbol_next = declaration;
552 declaration->symbol_next = iter->symbol_next;
557 assert(iter_last->symbol_next == NULL);
558 iter_last->symbol_next = declaration;
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 old_declaration->symbol_next = iter->symbol_next;
617 assert(iter != NULL);
621 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
624 static void environment_pop_to(size_t new_top)
626 stack_pop_to(&environment_stack, new_top);
629 static void label_pop_to(size_t new_top)
631 stack_pop_to(&label_stack, new_top);
635 static int get_rank(const type_t *type)
637 assert(!is_typeref(type));
638 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
639 * and esp. footnote 108). However we can't fold constants (yet), so we
640 * can't decide whether unsigned int is possible, while int always works.
641 * (unsigned int would be preferable when possible... for stuff like
642 * struct { enum { ... } bla : 4; } ) */
643 if(type->kind == TYPE_ENUM)
644 return ATOMIC_TYPE_INT;
646 assert(type->kind == TYPE_ATOMIC);
647 return type->atomic.akind;
650 static type_t *promote_integer(type_t *type)
652 if(type->kind == TYPE_BITFIELD)
653 type = type->bitfield.base;
655 if(get_rank(type) < ATOMIC_TYPE_INT)
662 * Create a cast expression.
664 * @param expression the expression to cast
665 * @param dest_type the destination type
667 static expression_t *create_cast_expression(expression_t *expression,
670 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
672 cast->unary.value = expression;
673 cast->base.type = dest_type;
679 * Check if a given expression represents the 0 pointer constant.
681 static bool is_null_pointer_constant(const expression_t *expression)
683 /* skip void* cast */
684 if(expression->kind == EXPR_UNARY_CAST
685 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
686 expression = expression->unary.value;
689 /* TODO: not correct yet, should be any constant integer expression
690 * which evaluates to 0 */
691 if (expression->kind != EXPR_CONST)
694 type_t *const type = skip_typeref(expression->base.type);
695 if (!is_type_integer(type))
698 return expression->conste.v.int_value == 0;
702 * Create an implicit cast expression.
704 * @param expression the expression to cast
705 * @param dest_type the destination type
707 static expression_t *create_implicit_cast(expression_t *expression,
710 type_t *const source_type = expression->base.type;
712 if (source_type == dest_type)
715 return create_cast_expression(expression, dest_type);
718 /** Implements the rules from § 6.5.16.1 */
719 static type_t *semantic_assign(type_t *orig_type_left,
720 const expression_t *const right,
723 type_t *const orig_type_right = right->base.type;
724 type_t *const type_left = skip_typeref(orig_type_left);
725 type_t *const type_right = skip_typeref(orig_type_right);
727 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
728 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
729 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
730 && is_type_pointer(type_right))) {
731 return orig_type_left;
734 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
735 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
736 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
738 /* the left type has all qualifiers from the right type */
739 unsigned missing_qualifiers
740 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
741 if(missing_qualifiers != 0) {
742 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
743 return orig_type_left;
746 points_to_left = get_unqualified_type(points_to_left);
747 points_to_right = get_unqualified_type(points_to_right);
749 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
750 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
751 return orig_type_left;
754 if (!types_compatible(points_to_left, points_to_right)) {
755 warningf(right->base.source_position,
756 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
757 orig_type_left, context, right, orig_type_right);
760 return orig_type_left;
763 if (is_type_compound(type_left) && is_type_compound(type_right)) {
764 type_t *const unqual_type_left = get_unqualified_type(type_left);
765 type_t *const unqual_type_right = get_unqualified_type(type_right);
766 if (types_compatible(unqual_type_left, unqual_type_right)) {
767 return orig_type_left;
771 if (!is_type_valid(type_left))
774 if (!is_type_valid(type_right))
775 return orig_type_right;
780 static expression_t *parse_constant_expression(void)
782 /* start parsing at precedence 7 (conditional expression) */
783 expression_t *result = parse_sub_expression(7);
785 if(!is_constant_expression(result)) {
786 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
792 static expression_t *parse_assignment_expression(void)
794 /* start parsing at precedence 2 (assignment expression) */
795 return parse_sub_expression(2);
798 static type_t *make_global_typedef(const char *name, type_t *type)
800 symbol_t *const symbol = symbol_table_insert(name);
802 declaration_t *const declaration = allocate_declaration_zero();
803 declaration->namespc = NAMESPACE_NORMAL;
804 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
805 declaration->type = type;
806 declaration->symbol = symbol;
807 declaration->source_position = builtin_source_position;
809 record_declaration(declaration);
811 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
812 typedef_type->typedeft.declaration = declaration;
817 static string_t parse_string_literals(void)
819 assert(token.type == T_STRING_LITERAL);
820 string_t result = token.v.string;
824 while (token.type == T_STRING_LITERAL) {
825 result = concat_strings(&result, &token.v.string);
832 static void parse_attributes(void)
836 case T___attribute__: {
844 errorf(HERE, "EOF while parsing attribute");
863 if(token.type != T_STRING_LITERAL) {
864 parse_error_expected("while parsing assembler attribute",
869 parse_string_literals();
874 goto attributes_finished;
883 static designator_t *parse_designation(void)
885 if(token.type != '[' && token.type != '.')
888 designator_t *result = NULL;
889 designator_t *last = NULL;
892 designator_t *designator;
895 designator = allocate_ast_zero(sizeof(designator[0]));
897 designator->array_access = parse_constant_expression();
901 designator = allocate_ast_zero(sizeof(designator[0]));
903 if(token.type != T_IDENTIFIER) {
904 parse_error_expected("while parsing designator",
908 designator->symbol = token.v.symbol;
916 assert(designator != NULL);
918 last->next = designator;
927 static initializer_t *initializer_from_string(array_type_t *type,
928 const string_t *const string)
930 /* TODO: check len vs. size of array type */
933 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
934 initializer->string.string = *string;
939 static initializer_t *initializer_from_wide_string(array_type_t *const type,
940 wide_string_t *const string)
942 /* TODO: check len vs. size of array type */
945 initializer_t *const initializer =
946 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
947 initializer->wide_string.string = *string;
952 static initializer_t *initializer_from_expression(type_t *type,
953 expression_t *expression)
955 /* TODO check that expression is a constant expression */
957 /* § 6.7.8.14/15 char array may be initialized by string literals */
958 type_t *const expr_type = expression->base.type;
959 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
960 array_type_t *const array_type = &type->array;
961 type_t *const element_type = skip_typeref(array_type->element_type);
963 if (element_type->kind == TYPE_ATOMIC) {
964 switch (expression->kind) {
965 case EXPR_STRING_LITERAL:
966 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
967 return initializer_from_string(array_type,
968 &expression->string.value);
971 case EXPR_WIDE_STRING_LITERAL: {
972 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
973 if (get_unqualified_type(element_type) == bare_wchar_type) {
974 return initializer_from_wide_string(array_type,
975 &expression->wide_string.value);
985 type_t *const res_type = semantic_assign(type, expression, "initializer");
986 if (res_type == NULL)
989 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
990 result->value.value = create_implicit_cast(expression, res_type);
995 static initializer_t *parse_sub_initializer(type_t *type,
996 expression_t *expression);
998 static initializer_t *parse_sub_initializer_elem(type_t *type)
1000 if(token.type == '{') {
1001 return parse_sub_initializer(type, NULL);
1004 expression_t *expression = parse_assignment_expression();
1005 return parse_sub_initializer(type, expression);
1008 static bool had_initializer_brace_warning;
1010 static void skip_designator(void)
1013 if(token.type == '.') {
1015 if(token.type == T_IDENTIFIER)
1017 } else if(token.type == '[') {
1019 parse_constant_expression();
1020 if(token.type == ']')
1028 static initializer_t *parse_sub_initializer(type_t *type,
1029 expression_t *expression)
1031 if(is_type_scalar(type)) {
1032 /* there might be extra {} hierarchies */
1033 if(token.type == '{') {
1035 if(!had_initializer_brace_warning) {
1036 warningf(HERE, "braces around scalar initializer");
1037 had_initializer_brace_warning = true;
1039 initializer_t *result = parse_sub_initializer(type, NULL);
1040 if(token.type == ',') {
1042 /* TODO: warn about excessive elements */
1048 if(expression == NULL) {
1049 expression = parse_assignment_expression();
1051 return initializer_from_expression(type, expression);
1054 /* does the expression match the currently looked at object to initialize */
1055 if(expression != NULL) {
1056 initializer_t *result = initializer_from_expression(type, expression);
1061 bool read_paren = false;
1062 if(token.type == '{') {
1067 /* descend into subtype */
1068 initializer_t *result = NULL;
1069 initializer_t **elems;
1070 if(is_type_array(type)) {
1071 if(token.type == '.') {
1073 "compound designator in initializer for array type '%T'",
1078 type_t *const element_type = skip_typeref(type->array.element_type);
1081 had_initializer_brace_warning = false;
1083 if(token.type == '{') {
1084 return parse_sub_initializer(type, NULL);
1087 if(expression == NULL) {
1088 expression = parse_assignment_expression();
1090 /* 6.7.8.14 + 15: we can have an optional {} around the string
1092 if(read_paren && (expression->kind == EXPR_STRING_LITERAL
1093 || expression->kind == EXPR_WIDE_STRING_LITERAL)) {
1094 initializer_t *result
1095 = initializer_from_expression(type, expression);
1096 if(result != NULL) {
1103 sub = parse_sub_initializer(element_type, expression);
1105 /* didn't match the subtypes -> try the parent type */
1107 assert(!read_paren);
1111 elems = NEW_ARR_F(initializer_t*, 0);
1112 ARR_APP1(initializer_t*, elems, sub);
1115 if(token.type == '}')
1118 if(token.type == '}')
1121 sub = parse_sub_initializer_elem(element_type);
1123 /* TODO error, do nicer cleanup */
1124 errorf(HERE, "member initializer didn't match");
1128 ARR_APP1(initializer_t*, elems, sub);
1131 assert(is_type_compound(type));
1132 scope_t *const scope = &type->compound.declaration->scope;
1134 if(token.type == '[') {
1136 "array designator in initializer for compound type '%T'",
1141 declaration_t *first = scope->declarations;
1144 type_t *first_type = first->type;
1145 first_type = skip_typeref(first_type);
1148 had_initializer_brace_warning = false;
1149 if(expression == NULL) {
1150 sub = parse_sub_initializer_elem(first_type);
1152 sub = parse_sub_initializer(first_type, expression);
1155 /* didn't match the subtypes -> try our parent type */
1157 assert(!read_paren);
1161 elems = NEW_ARR_F(initializer_t*, 0);
1162 ARR_APP1(initializer_t*, elems, sub);
1164 declaration_t *iter = first->next;
1165 for( ; iter != NULL; iter = iter->next) {
1166 if(iter->symbol == NULL)
1168 if(iter->namespc != NAMESPACE_NORMAL)
1171 if(token.type == '}')
1174 if(token.type == '}')
1177 type_t *iter_type = iter->type;
1178 iter_type = skip_typeref(iter_type);
1180 sub = parse_sub_initializer_elem(iter_type);
1182 /* TODO error, do nicer cleanup */
1183 errorf(HERE, "member initializer didn't match");
1187 ARR_APP1(initializer_t*, elems, sub);
1191 int len = ARR_LEN(elems);
1192 size_t elems_size = sizeof(initializer_t*) * len;
1194 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1196 init->initializer.kind = INITIALIZER_LIST;
1198 memcpy(init->initializers, elems, elems_size);
1201 result = (initializer_t*) init;
1204 if(token.type == ',')
1211 static initializer_t *parse_initializer(type_t *const orig_type)
1213 initializer_t *result;
1215 type_t *const type = skip_typeref(orig_type);
1217 if(token.type != '{') {
1218 expression_t *expression = parse_assignment_expression();
1219 initializer_t *initializer = initializer_from_expression(type, expression);
1220 if(initializer == NULL) {
1222 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1223 expression, expression->base.type, orig_type);
1228 if(is_type_scalar(type)) {
1232 expression_t *expression = parse_assignment_expression();
1233 result = initializer_from_expression(type, expression);
1235 if(token.type == ',')
1241 result = parse_sub_initializer(type, NULL);
1247 static declaration_t *append_declaration(declaration_t *declaration);
1249 static declaration_t *parse_compound_type_specifier(bool is_struct)
1257 symbol_t *symbol = NULL;
1258 declaration_t *declaration = NULL;
1260 if (token.type == T___attribute__) {
1265 if(token.type == T_IDENTIFIER) {
1266 symbol = token.v.symbol;
1270 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1272 declaration = get_declaration(symbol, NAMESPACE_UNION);
1274 } else if(token.type != '{') {
1276 parse_error_expected("while parsing struct type specifier",
1277 T_IDENTIFIER, '{', 0);
1279 parse_error_expected("while parsing union type specifier",
1280 T_IDENTIFIER, '{', 0);
1286 if(declaration == NULL) {
1287 declaration = allocate_declaration_zero();
1288 declaration->namespc =
1289 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1290 declaration->source_position = token.source_position;
1291 declaration->symbol = symbol;
1292 declaration->parent_scope = scope;
1293 if (symbol != NULL) {
1294 environment_push(declaration);
1296 append_declaration(declaration);
1299 if(token.type == '{') {
1300 if(declaration->init.is_defined) {
1301 assert(symbol != NULL);
1302 errorf(HERE, "multiple definitions of '%s %Y'",
1303 is_struct ? "struct" : "union", symbol);
1304 declaration->scope.declarations = NULL;
1306 declaration->init.is_defined = true;
1308 parse_compound_type_entries(declaration);
1315 static void parse_enum_entries(type_t *const enum_type)
1319 if(token.type == '}') {
1321 errorf(HERE, "empty enum not allowed");
1326 if(token.type != T_IDENTIFIER) {
1327 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1332 declaration_t *const entry = allocate_declaration_zero();
1333 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1334 entry->type = enum_type;
1335 entry->symbol = token.v.symbol;
1336 entry->source_position = token.source_position;
1339 if(token.type == '=') {
1341 entry->init.enum_value = parse_constant_expression();
1346 record_declaration(entry);
1348 if(token.type != ',')
1351 } while(token.type != '}');
1356 static type_t *parse_enum_specifier(void)
1360 declaration_t *declaration;
1363 if(token.type == T_IDENTIFIER) {
1364 symbol = token.v.symbol;
1367 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1368 } else if(token.type != '{') {
1369 parse_error_expected("while parsing enum type specifier",
1370 T_IDENTIFIER, '{', 0);
1377 if(declaration == NULL) {
1378 declaration = allocate_declaration_zero();
1379 declaration->namespc = NAMESPACE_ENUM;
1380 declaration->source_position = token.source_position;
1381 declaration->symbol = symbol;
1382 declaration->parent_scope = scope;
1385 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1386 type->enumt.declaration = declaration;
1388 if(token.type == '{') {
1389 if(declaration->init.is_defined) {
1390 errorf(HERE, "multiple definitions of enum %Y", symbol);
1392 if (symbol != NULL) {
1393 environment_push(declaration);
1395 append_declaration(declaration);
1396 declaration->init.is_defined = 1;
1398 parse_enum_entries(type);
1406 * if a symbol is a typedef to another type, return true
1408 static bool is_typedef_symbol(symbol_t *symbol)
1410 const declaration_t *const declaration =
1411 get_declaration(symbol, NAMESPACE_NORMAL);
1413 declaration != NULL &&
1414 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1417 static type_t *parse_typeof(void)
1425 expression_t *expression = NULL;
1428 switch(token.type) {
1429 case T___extension__:
1430 /* this can be a prefix to a typename or an expression */
1431 /* we simply eat it now. */
1434 } while(token.type == T___extension__);
1438 if(is_typedef_symbol(token.v.symbol)) {
1439 type = parse_typename();
1441 expression = parse_expression();
1442 type = expression->base.type;
1447 type = parse_typename();
1451 expression = parse_expression();
1452 type = expression->base.type;
1458 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1459 typeof_type->typeoft.expression = expression;
1460 typeof_type->typeoft.typeof_type = type;
1466 SPECIFIER_SIGNED = 1 << 0,
1467 SPECIFIER_UNSIGNED = 1 << 1,
1468 SPECIFIER_LONG = 1 << 2,
1469 SPECIFIER_INT = 1 << 3,
1470 SPECIFIER_DOUBLE = 1 << 4,
1471 SPECIFIER_CHAR = 1 << 5,
1472 SPECIFIER_SHORT = 1 << 6,
1473 SPECIFIER_LONG_LONG = 1 << 7,
1474 SPECIFIER_FLOAT = 1 << 8,
1475 SPECIFIER_BOOL = 1 << 9,
1476 SPECIFIER_VOID = 1 << 10,
1477 #ifdef PROVIDE_COMPLEX
1478 SPECIFIER_COMPLEX = 1 << 11,
1479 SPECIFIER_IMAGINARY = 1 << 12,
1483 static type_t *create_builtin_type(symbol_t *const symbol,
1484 type_t *const real_type)
1486 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1487 type->builtin.symbol = symbol;
1488 type->builtin.real_type = real_type;
1490 type_t *result = typehash_insert(type);
1491 if (type != result) {
1498 static type_t *get_typedef_type(symbol_t *symbol)
1500 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1501 if(declaration == NULL
1502 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1505 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1506 type->typedeft.declaration = declaration;
1511 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1513 type_t *type = NULL;
1514 unsigned type_qualifiers = 0;
1515 unsigned type_specifiers = 0;
1518 specifiers->source_position = token.source_position;
1521 switch(token.type) {
1524 #define MATCH_STORAGE_CLASS(token, class) \
1526 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1527 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1529 specifiers->storage_class = class; \
1533 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1534 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1535 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1536 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1537 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1540 switch (specifiers->storage_class) {
1541 case STORAGE_CLASS_NONE:
1542 specifiers->storage_class = STORAGE_CLASS_THREAD;
1545 case STORAGE_CLASS_EXTERN:
1546 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1549 case STORAGE_CLASS_STATIC:
1550 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1554 errorf(HERE, "multiple storage classes in declaration specifiers");
1560 /* type qualifiers */
1561 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1563 type_qualifiers |= qualifier; \
1567 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1568 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1569 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1571 case T___extension__:
1576 /* type specifiers */
1577 #define MATCH_SPECIFIER(token, specifier, name) \
1580 if(type_specifiers & specifier) { \
1581 errorf(HERE, "multiple " name " type specifiers given"); \
1583 type_specifiers |= specifier; \
1587 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1588 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1589 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1590 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1591 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1592 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1593 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1594 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1595 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1596 #ifdef PROVIDE_COMPLEX
1597 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1598 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1601 /* only in microsoft mode */
1602 specifiers->decl_modifiers |= DM_FORCEINLINE;
1606 specifiers->is_inline = true;
1611 if(type_specifiers & SPECIFIER_LONG_LONG) {
1612 errorf(HERE, "multiple type specifiers given");
1613 } else if(type_specifiers & SPECIFIER_LONG) {
1614 type_specifiers |= SPECIFIER_LONG_LONG;
1616 type_specifiers |= SPECIFIER_LONG;
1621 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1623 type->compound.declaration = parse_compound_type_specifier(true);
1627 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1629 type->compound.declaration = parse_compound_type_specifier(false);
1633 type = parse_enum_specifier();
1636 type = parse_typeof();
1638 case T___builtin_va_list:
1639 type = duplicate_type(type_valist);
1643 case T___attribute__:
1647 case T_IDENTIFIER: {
1648 /* only parse identifier if we haven't found a type yet */
1649 if(type != NULL || type_specifiers != 0)
1650 goto finish_specifiers;
1652 type_t *typedef_type = get_typedef_type(token.v.symbol);
1654 if(typedef_type == NULL)
1655 goto finish_specifiers;
1658 type = typedef_type;
1662 /* function specifier */
1664 goto finish_specifiers;
1671 atomic_type_kind_t atomic_type;
1673 /* match valid basic types */
1674 switch(type_specifiers) {
1675 case SPECIFIER_VOID:
1676 atomic_type = ATOMIC_TYPE_VOID;
1678 case SPECIFIER_CHAR:
1679 atomic_type = ATOMIC_TYPE_CHAR;
1681 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1682 atomic_type = ATOMIC_TYPE_SCHAR;
1684 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1685 atomic_type = ATOMIC_TYPE_UCHAR;
1687 case SPECIFIER_SHORT:
1688 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1689 case SPECIFIER_SHORT | SPECIFIER_INT:
1690 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1691 atomic_type = ATOMIC_TYPE_SHORT;
1693 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1694 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1695 atomic_type = ATOMIC_TYPE_USHORT;
1698 case SPECIFIER_SIGNED:
1699 case SPECIFIER_SIGNED | SPECIFIER_INT:
1700 atomic_type = ATOMIC_TYPE_INT;
1702 case SPECIFIER_UNSIGNED:
1703 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1704 atomic_type = ATOMIC_TYPE_UINT;
1706 case SPECIFIER_LONG:
1707 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1708 case SPECIFIER_LONG | SPECIFIER_INT:
1709 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1710 atomic_type = ATOMIC_TYPE_LONG;
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1713 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1714 atomic_type = ATOMIC_TYPE_ULONG;
1716 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1717 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1718 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1719 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1721 atomic_type = ATOMIC_TYPE_LONGLONG;
1723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1724 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1726 atomic_type = ATOMIC_TYPE_ULONGLONG;
1728 case SPECIFIER_FLOAT:
1729 atomic_type = ATOMIC_TYPE_FLOAT;
1731 case SPECIFIER_DOUBLE:
1732 atomic_type = ATOMIC_TYPE_DOUBLE;
1734 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1735 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1737 case SPECIFIER_BOOL:
1738 atomic_type = ATOMIC_TYPE_BOOL;
1740 #ifdef PROVIDE_COMPLEX
1741 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1742 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1744 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1745 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1747 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1748 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1750 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1751 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1753 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1754 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1756 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1757 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1761 /* invalid specifier combination, give an error message */
1762 if(type_specifiers == 0) {
1763 if (! strict_mode) {
1764 if (warning.implicit_int) {
1765 warningf(HERE, "no type specifiers in declaration, using 'int'");
1767 atomic_type = ATOMIC_TYPE_INT;
1770 errorf(HERE, "no type specifiers given in declaration");
1772 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1773 (type_specifiers & SPECIFIER_UNSIGNED)) {
1774 errorf(HERE, "signed and unsigned specifiers gives");
1775 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1776 errorf(HERE, "only integer types can be signed or unsigned");
1778 errorf(HERE, "multiple datatypes in declaration");
1780 atomic_type = ATOMIC_TYPE_INVALID;
1783 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
1784 type->atomic.akind = atomic_type;
1787 if(type_specifiers != 0) {
1788 errorf(HERE, "multiple datatypes in declaration");
1792 type->base.qualifiers = type_qualifiers;
1794 type_t *result = typehash_insert(type);
1795 if(newtype && result != type) {
1799 specifiers->type = result;
1802 static type_qualifiers_t parse_type_qualifiers(void)
1804 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1807 switch(token.type) {
1808 /* type qualifiers */
1809 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1810 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1811 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1814 return type_qualifiers;
1819 static declaration_t *parse_identifier_list(void)
1821 declaration_t *declarations = NULL;
1822 declaration_t *last_declaration = NULL;
1824 declaration_t *const declaration = allocate_declaration_zero();
1825 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1826 declaration->source_position = token.source_position;
1827 declaration->symbol = token.v.symbol;
1830 if(last_declaration != NULL) {
1831 last_declaration->next = declaration;
1833 declarations = declaration;
1835 last_declaration = declaration;
1837 if(token.type != ',')
1840 } while(token.type == T_IDENTIFIER);
1842 return declarations;
1845 static void semantic_parameter(declaration_t *declaration)
1847 /* TODO: improve error messages */
1849 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1850 errorf(HERE, "typedef not allowed in parameter list");
1851 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1852 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1853 errorf(HERE, "parameter may only have none or register storage class");
1856 type_t *const orig_type = declaration->type;
1857 type_t * type = skip_typeref(orig_type);
1859 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1860 * into a pointer. § 6.7.5.3 (7) */
1861 if (is_type_array(type)) {
1862 type_t *const element_type = type->array.element_type;
1864 type = make_pointer_type(element_type, type->base.qualifiers);
1866 declaration->type = type;
1869 if(is_type_incomplete(type)) {
1870 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1871 orig_type, declaration->symbol);
1875 static declaration_t *parse_parameter(void)
1877 declaration_specifiers_t specifiers;
1878 memset(&specifiers, 0, sizeof(specifiers));
1880 parse_declaration_specifiers(&specifiers);
1882 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1884 semantic_parameter(declaration);
1889 static declaration_t *parse_parameters(function_type_t *type)
1891 if(token.type == T_IDENTIFIER) {
1892 symbol_t *symbol = token.v.symbol;
1893 if(!is_typedef_symbol(symbol)) {
1894 type->kr_style_parameters = true;
1895 return parse_identifier_list();
1899 if(token.type == ')') {
1900 type->unspecified_parameters = 1;
1903 if(token.type == T_void && look_ahead(1)->type == ')') {
1908 declaration_t *declarations = NULL;
1909 declaration_t *declaration;
1910 declaration_t *last_declaration = NULL;
1911 function_parameter_t *parameter;
1912 function_parameter_t *last_parameter = NULL;
1915 switch(token.type) {
1919 return declarations;
1922 case T___extension__:
1924 declaration = parse_parameter();
1926 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1927 memset(parameter, 0, sizeof(parameter[0]));
1928 parameter->type = declaration->type;
1930 if(last_parameter != NULL) {
1931 last_declaration->next = declaration;
1932 last_parameter->next = parameter;
1934 type->parameters = parameter;
1935 declarations = declaration;
1937 last_parameter = parameter;
1938 last_declaration = declaration;
1942 return declarations;
1944 if(token.type != ',')
1945 return declarations;
1955 } construct_type_kind_t;
1957 typedef struct construct_type_t construct_type_t;
1958 struct construct_type_t {
1959 construct_type_kind_t kind;
1960 construct_type_t *next;
1963 typedef struct parsed_pointer_t parsed_pointer_t;
1964 struct parsed_pointer_t {
1965 construct_type_t construct_type;
1966 type_qualifiers_t type_qualifiers;
1969 typedef struct construct_function_type_t construct_function_type_t;
1970 struct construct_function_type_t {
1971 construct_type_t construct_type;
1972 type_t *function_type;
1975 typedef struct parsed_array_t parsed_array_t;
1976 struct parsed_array_t {
1977 construct_type_t construct_type;
1978 type_qualifiers_t type_qualifiers;
1984 typedef struct construct_base_type_t construct_base_type_t;
1985 struct construct_base_type_t {
1986 construct_type_t construct_type;
1990 static construct_type_t *parse_pointer_declarator(void)
1994 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1995 memset(pointer, 0, sizeof(pointer[0]));
1996 pointer->construct_type.kind = CONSTRUCT_POINTER;
1997 pointer->type_qualifiers = parse_type_qualifiers();
1999 return (construct_type_t*) pointer;
2002 static construct_type_t *parse_array_declarator(void)
2006 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2007 memset(array, 0, sizeof(array[0]));
2008 array->construct_type.kind = CONSTRUCT_ARRAY;
2010 if(token.type == T_static) {
2011 array->is_static = true;
2015 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2016 if(type_qualifiers != 0) {
2017 if(token.type == T_static) {
2018 array->is_static = true;
2022 array->type_qualifiers = type_qualifiers;
2024 if(token.type == '*' && look_ahead(1)->type == ']') {
2025 array->is_variable = true;
2027 } else if(token.type != ']') {
2028 array->size = parse_assignment_expression();
2033 return (construct_type_t*) array;
2036 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2040 type_t *type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2042 declaration_t *parameters = parse_parameters(&type->function);
2043 if(declaration != NULL) {
2044 declaration->scope.declarations = parameters;
2047 construct_function_type_t *construct_function_type =
2048 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2049 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2050 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2051 construct_function_type->function_type = type;
2055 return (construct_type_t*) construct_function_type;
2058 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2059 bool may_be_abstract)
2061 /* construct a single linked list of construct_type_t's which describe
2062 * how to construct the final declarator type */
2063 construct_type_t *first = NULL;
2064 construct_type_t *last = NULL;
2067 while(token.type == '*') {
2068 construct_type_t *type = parse_pointer_declarator();
2079 /* TODO: find out if this is correct */
2082 construct_type_t *inner_types = NULL;
2084 switch(token.type) {
2086 if(declaration == NULL) {
2087 errorf(HERE, "no identifier expected in typename");
2089 declaration->symbol = token.v.symbol;
2090 declaration->source_position = token.source_position;
2096 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2102 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2103 /* avoid a loop in the outermost scope, because eat_statement doesn't
2105 if(token.type == '}' && current_function == NULL) {
2113 construct_type_t *p = last;
2116 construct_type_t *type;
2117 switch(token.type) {
2119 type = parse_function_declarator(declaration);
2122 type = parse_array_declarator();
2125 goto declarator_finished;
2128 /* insert in the middle of the list (behind p) */
2130 type->next = p->next;
2141 declarator_finished:
2144 /* append inner_types at the end of the list, we don't to set last anymore
2145 * as it's not needed anymore */
2147 assert(first == NULL);
2148 first = inner_types;
2150 last->next = inner_types;
2156 static type_t *construct_declarator_type(construct_type_t *construct_list,
2159 construct_type_t *iter = construct_list;
2160 for( ; iter != NULL; iter = iter->next) {
2161 switch(iter->kind) {
2162 case CONSTRUCT_INVALID:
2163 panic("invalid type construction found");
2164 case CONSTRUCT_FUNCTION: {
2165 construct_function_type_t *construct_function_type
2166 = (construct_function_type_t*) iter;
2168 type_t *function_type = construct_function_type->function_type;
2170 function_type->function.return_type = type;
2172 type_t *skipped_return_type = skip_typeref(type);
2173 if (is_type_function(skipped_return_type)) {
2174 errorf(HERE, "function returning function is not allowed");
2175 type = type_error_type;
2176 } else if (is_type_array(skipped_return_type)) {
2177 errorf(HERE, "function returning array is not allowed");
2178 type = type_error_type;
2180 type = function_type;
2185 case CONSTRUCT_POINTER: {
2186 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2187 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2188 pointer_type->pointer.points_to = type;
2189 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2191 type = pointer_type;
2195 case CONSTRUCT_ARRAY: {
2196 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2197 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2199 array_type->base.qualifiers = parsed_array->type_qualifiers;
2200 array_type->array.element_type = type;
2201 array_type->array.is_static = parsed_array->is_static;
2202 array_type->array.is_variable = parsed_array->is_variable;
2203 array_type->array.size = parsed_array->size;
2205 type_t *skipped_type = skip_typeref(type);
2206 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2207 errorf(HERE, "array of void is not allowed");
2208 type = type_error_type;
2216 type_t *hashed_type = typehash_insert(type);
2217 if(hashed_type != type) {
2218 /* the function type was constructed earlier freeing it here will
2219 * destroy other types... */
2220 if(iter->kind != CONSTRUCT_FUNCTION) {
2230 static declaration_t *parse_declarator(
2231 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2233 declaration_t *const declaration = allocate_declaration_zero();
2234 declaration->storage_class = specifiers->storage_class;
2235 declaration->modifiers = specifiers->decl_modifiers;
2236 declaration->is_inline = specifiers->is_inline;
2238 construct_type_t *construct_type
2239 = parse_inner_declarator(declaration, may_be_abstract);
2240 type_t *const type = specifiers->type;
2241 declaration->type = construct_declarator_type(construct_type, type);
2243 if(construct_type != NULL) {
2244 obstack_free(&temp_obst, construct_type);
2250 static type_t *parse_abstract_declarator(type_t *base_type)
2252 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2254 type_t *result = construct_declarator_type(construct_type, base_type);
2255 if(construct_type != NULL) {
2256 obstack_free(&temp_obst, construct_type);
2262 static declaration_t *append_declaration(declaration_t* const declaration)
2264 if (last_declaration != NULL) {
2265 last_declaration->next = declaration;
2267 scope->declarations = declaration;
2269 last_declaration = declaration;
2274 * Check if the declaration of main is suspicious. main should be a
2275 * function with external linkage, returning int, taking either zero
2276 * arguments, two, or three arguments of appropriate types, ie.
2278 * int main([ int argc, char **argv [, char **env ] ]).
2280 * @param decl the declaration to check
2281 * @param type the function type of the declaration
2283 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2285 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2286 warningf(decl->source_position, "'main' is normally a non-static function");
2288 if (skip_typeref(func_type->return_type) != type_int) {
2289 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2291 const function_parameter_t *parm = func_type->parameters;
2293 type_t *const first_type = parm->type;
2294 if (!types_compatible(skip_typeref(first_type), type_int)) {
2295 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2299 type_t *const second_type = parm->type;
2300 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2301 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2305 type_t *const third_type = parm->type;
2306 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2307 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2311 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2315 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2321 * Check if a symbol is the equal to "main".
2323 static bool is_sym_main(const symbol_t *const sym)
2325 return strcmp(sym->string, "main") == 0;
2328 static declaration_t *internal_record_declaration(
2329 declaration_t *const declaration,
2330 const bool is_function_definition)
2332 const symbol_t *const symbol = declaration->symbol;
2333 const namespace_t namespc = (namespace_t)declaration->namespc;
2335 type_t *const orig_type = declaration->type;
2336 type_t *const type = skip_typeref(orig_type);
2337 if (is_type_function(type) &&
2338 type->function.unspecified_parameters &&
2339 warning.strict_prototypes) {
2340 warningf(declaration->source_position,
2341 "function declaration '%#T' is not a prototype",
2342 orig_type, declaration->symbol);
2345 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2346 check_type_of_main(declaration, &type->function);
2349 assert(declaration->symbol != NULL);
2350 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2352 assert(declaration != previous_declaration);
2353 if (previous_declaration != NULL) {
2354 if (previous_declaration->parent_scope == scope) {
2355 /* can happen for K&R style declarations */
2356 if(previous_declaration->type == NULL) {
2357 previous_declaration->type = declaration->type;
2360 const type_t *prev_type = skip_typeref(previous_declaration->type);
2361 if (!types_compatible(type, prev_type)) {
2362 errorf(declaration->source_position,
2363 "declaration '%#T' is incompatible with "
2364 "previous declaration '%#T'",
2365 orig_type, symbol, previous_declaration->type, symbol);
2366 errorf(previous_declaration->source_position,
2367 "previous declaration of '%Y' was here", symbol);
2369 unsigned old_storage_class
2370 = previous_declaration->storage_class;
2371 unsigned new_storage_class = declaration->storage_class;
2373 if(is_type_incomplete(prev_type)) {
2374 previous_declaration->type = type;
2378 /* pretend no storage class means extern for function
2379 * declarations (except if the previous declaration is neither
2380 * none nor extern) */
2381 if (is_type_function(type)) {
2382 switch (old_storage_class) {
2383 case STORAGE_CLASS_NONE:
2384 old_storage_class = STORAGE_CLASS_EXTERN;
2386 case STORAGE_CLASS_EXTERN:
2387 if (is_function_definition) {
2388 if (warning.missing_prototypes &&
2389 prev_type->function.unspecified_parameters &&
2390 !is_sym_main(symbol)) {
2391 warningf(declaration->source_position,
2392 "no previous prototype for '%#T'",
2395 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2396 new_storage_class = STORAGE_CLASS_EXTERN;
2404 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2405 new_storage_class == STORAGE_CLASS_EXTERN) {
2406 warn_redundant_declaration:
2407 if (warning.redundant_decls) {
2408 warningf(declaration->source_position,
2409 "redundant declaration for '%Y'", symbol);
2410 warningf(previous_declaration->source_position,
2411 "previous declaration of '%Y' was here",
2414 } else if (current_function == NULL) {
2415 if (old_storage_class != STORAGE_CLASS_STATIC &&
2416 new_storage_class == STORAGE_CLASS_STATIC) {
2417 errorf(declaration->source_position,
2418 "static declaration of '%Y' follows non-static declaration",
2420 errorf(previous_declaration->source_position,
2421 "previous declaration of '%Y' was here", symbol);
2423 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2424 goto warn_redundant_declaration;
2426 if (new_storage_class == STORAGE_CLASS_NONE) {
2427 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2431 if (old_storage_class == new_storage_class) {
2432 errorf(declaration->source_position,
2433 "redeclaration of '%Y'", symbol);
2435 errorf(declaration->source_position,
2436 "redeclaration of '%Y' with different linkage",
2439 errorf(previous_declaration->source_position,
2440 "previous declaration of '%Y' was here", symbol);
2443 return previous_declaration;
2445 } else if (is_function_definition) {
2446 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2447 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2448 warningf(declaration->source_position,
2449 "no previous prototype for '%#T'", orig_type, symbol);
2450 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2451 warningf(declaration->source_position,
2452 "no previous declaration for '%#T'", orig_type,
2456 } else if (warning.missing_declarations &&
2457 scope == global_scope &&
2458 !is_type_function(type) && (
2459 declaration->storage_class == STORAGE_CLASS_NONE ||
2460 declaration->storage_class == STORAGE_CLASS_THREAD
2462 warningf(declaration->source_position,
2463 "no previous declaration for '%#T'", orig_type, symbol);
2466 assert(declaration->parent_scope == NULL);
2467 assert(scope != NULL);
2469 declaration->parent_scope = scope;
2471 environment_push(declaration);
2472 return append_declaration(declaration);
2475 static declaration_t *record_declaration(declaration_t *declaration)
2477 return internal_record_declaration(declaration, false);
2480 static declaration_t *record_function_definition(declaration_t *declaration)
2482 return internal_record_declaration(declaration, true);
2485 static void parser_error_multiple_definition(declaration_t *declaration,
2486 const source_position_t source_position)
2488 errorf(source_position, "multiple definition of symbol '%Y'",
2489 declaration->symbol);
2490 errorf(declaration->source_position,
2491 "this is the location of the previous definition.");
2494 static bool is_declaration_specifier(const token_t *token,
2495 bool only_type_specifiers)
2497 switch(token->type) {
2501 return is_typedef_symbol(token->v.symbol);
2503 case T___extension__:
2506 return !only_type_specifiers;
2513 static void parse_init_declarator_rest(declaration_t *declaration)
2517 type_t *orig_type = declaration->type;
2518 type_t *type = type = skip_typeref(orig_type);
2520 if(declaration->init.initializer != NULL) {
2521 parser_error_multiple_definition(declaration, token.source_position);
2524 initializer_t *initializer = parse_initializer(type);
2526 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2527 * the array type size */
2528 if(is_type_array(type) && initializer != NULL) {
2529 array_type_t *array_type = &type->array;
2531 if(array_type->size == NULL) {
2532 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2534 cnst->base.type = type_size_t;
2536 switch (initializer->kind) {
2537 case INITIALIZER_LIST: {
2538 cnst->conste.v.int_value = initializer->list.len;
2542 case INITIALIZER_STRING: {
2543 cnst->conste.v.int_value = initializer->string.string.size;
2547 case INITIALIZER_WIDE_STRING: {
2548 cnst->conste.v.int_value = initializer->wide_string.string.size;
2553 panic("invalid initializer type");
2556 array_type->size = cnst;
2557 array_type->has_implicit_size = true;
2561 if(is_type_function(type)) {
2562 errorf(declaration->source_position,
2563 "initializers not allowed for function types at declator '%Y' (type '%T')",
2564 declaration->symbol, orig_type);
2566 declaration->init.initializer = initializer;
2570 /* parse rest of a declaration without any declarator */
2571 static void parse_anonymous_declaration_rest(
2572 const declaration_specifiers_t *specifiers,
2573 parsed_declaration_func finished_declaration)
2577 declaration_t *const declaration = allocate_declaration_zero();
2578 declaration->type = specifiers->type;
2579 declaration->storage_class = specifiers->storage_class;
2580 declaration->source_position = specifiers->source_position;
2582 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2583 warningf(declaration->source_position, "useless storage class in empty declaration");
2586 type_t *type = declaration->type;
2587 switch (type->kind) {
2588 case TYPE_COMPOUND_STRUCT:
2589 case TYPE_COMPOUND_UNION: {
2590 if (type->compound.declaration->symbol == NULL) {
2591 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2600 warningf(declaration->source_position, "empty declaration");
2604 finished_declaration(declaration);
2607 static void parse_declaration_rest(declaration_t *ndeclaration,
2608 const declaration_specifiers_t *specifiers,
2609 parsed_declaration_func finished_declaration)
2612 declaration_t *declaration = finished_declaration(ndeclaration);
2614 type_t *orig_type = declaration->type;
2615 type_t *type = skip_typeref(orig_type);
2617 if (type->kind != TYPE_FUNCTION &&
2618 declaration->is_inline &&
2619 is_type_valid(type)) {
2620 warningf(declaration->source_position,
2621 "variable '%Y' declared 'inline'\n", declaration->symbol);
2624 if(token.type == '=') {
2625 parse_init_declarator_rest(declaration);
2628 if(token.type != ',')
2632 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2637 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2639 symbol_t *symbol = declaration->symbol;
2640 if(symbol == NULL) {
2641 errorf(HERE, "anonymous declaration not valid as function parameter");
2644 namespace_t namespc = (namespace_t) declaration->namespc;
2645 if(namespc != NAMESPACE_NORMAL) {
2646 return record_declaration(declaration);
2649 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2650 if(previous_declaration == NULL ||
2651 previous_declaration->parent_scope != scope) {
2652 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2657 if(previous_declaration->type == NULL) {
2658 previous_declaration->type = declaration->type;
2659 previous_declaration->storage_class = declaration->storage_class;
2660 previous_declaration->parent_scope = scope;
2661 return previous_declaration;
2663 return record_declaration(declaration);
2667 static void parse_declaration(parsed_declaration_func finished_declaration)
2669 declaration_specifiers_t specifiers;
2670 memset(&specifiers, 0, sizeof(specifiers));
2671 parse_declaration_specifiers(&specifiers);
2673 if(token.type == ';') {
2674 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2676 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2677 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2681 static void parse_kr_declaration_list(declaration_t *declaration)
2683 type_t *type = skip_typeref(declaration->type);
2684 if(!is_type_function(type))
2687 if(!type->function.kr_style_parameters)
2690 /* push function parameters */
2691 int top = environment_top();
2692 scope_t *last_scope = scope;
2693 set_scope(&declaration->scope);
2695 declaration_t *parameter = declaration->scope.declarations;
2696 for( ; parameter != NULL; parameter = parameter->next) {
2697 assert(parameter->parent_scope == NULL);
2698 parameter->parent_scope = scope;
2699 environment_push(parameter);
2702 /* parse declaration list */
2703 while(is_declaration_specifier(&token, false)) {
2704 parse_declaration(finished_kr_declaration);
2707 /* pop function parameters */
2708 assert(scope == &declaration->scope);
2709 set_scope(last_scope);
2710 environment_pop_to(top);
2712 /* update function type */
2713 type_t *new_type = duplicate_type(type);
2714 new_type->function.kr_style_parameters = false;
2716 function_parameter_t *parameters = NULL;
2717 function_parameter_t *last_parameter = NULL;
2719 declaration_t *parameter_declaration = declaration->scope.declarations;
2720 for( ; parameter_declaration != NULL;
2721 parameter_declaration = parameter_declaration->next) {
2722 type_t *parameter_type = parameter_declaration->type;
2723 if(parameter_type == NULL) {
2725 errorf(HERE, "no type specified for function parameter '%Y'",
2726 parameter_declaration->symbol);
2728 if (warning.implicit_int) {
2729 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2730 parameter_declaration->symbol);
2732 parameter_type = type_int;
2733 parameter_declaration->type = parameter_type;
2737 semantic_parameter(parameter_declaration);
2738 parameter_type = parameter_declaration->type;
2740 function_parameter_t *function_parameter
2741 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2742 memset(function_parameter, 0, sizeof(function_parameter[0]));
2744 function_parameter->type = parameter_type;
2745 if(last_parameter != NULL) {
2746 last_parameter->next = function_parameter;
2748 parameters = function_parameter;
2750 last_parameter = function_parameter;
2752 new_type->function.parameters = parameters;
2754 type = typehash_insert(new_type);
2755 if(type != new_type) {
2756 obstack_free(type_obst, new_type);
2759 declaration->type = type;
2762 static bool first_err = true;
2765 * When called with first_err set, prints the name of the current function,
2768 static void print_in_function(void) {
2771 diagnosticf("%s: In function '%Y':\n",
2772 current_function->source_position.input_name,
2773 current_function->symbol);
2778 * Check if all labels are defined in the current function.
2779 * Check if all labels are used in the current function.
2781 static void check_labels(void)
2783 for (const goto_statement_t *goto_statement = goto_first;
2784 goto_statement != NULL;
2785 goto_statement = goto_statement->next) {
2786 declaration_t *label = goto_statement->label;
2789 if (label->source_position.input_name == NULL) {
2790 print_in_function();
2791 errorf(goto_statement->base.source_position,
2792 "label '%Y' used but not defined", label->symbol);
2795 goto_first = goto_last = NULL;
2797 if (warning.unused_label) {
2798 for (const label_statement_t *label_statement = label_first;
2799 label_statement != NULL;
2800 label_statement = label_statement->next) {
2801 const declaration_t *label = label_statement->label;
2803 if (! label->used) {
2804 print_in_function();
2805 warningf(label_statement->base.source_position,
2806 "label '%Y' defined but not used", label->symbol);
2810 label_first = label_last = NULL;
2814 * Check declarations of current_function for unused entities.
2816 static void check_declarations(void)
2818 if (warning.unused_parameter) {
2819 const scope_t *scope = ¤t_function->scope;
2821 const declaration_t *parameter = scope->declarations;
2822 for (; parameter != NULL; parameter = parameter->next) {
2823 if (! parameter->used) {
2824 print_in_function();
2825 warningf(parameter->source_position,
2826 "unused parameter '%Y'", parameter->symbol);
2830 if (warning.unused_variable) {
2834 static void parse_external_declaration(void)
2836 /* function-definitions and declarations both start with declaration
2838 declaration_specifiers_t specifiers;
2839 memset(&specifiers, 0, sizeof(specifiers));
2840 parse_declaration_specifiers(&specifiers);
2842 /* must be a declaration */
2843 if(token.type == ';') {
2844 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2848 /* declarator is common to both function-definitions and declarations */
2849 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2851 /* must be a declaration */
2852 if(token.type == ',' || token.type == '=' || token.type == ';') {
2853 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2857 /* must be a function definition */
2858 parse_kr_declaration_list(ndeclaration);
2860 if(token.type != '{') {
2861 parse_error_expected("while parsing function definition", '{', 0);
2866 type_t *type = ndeclaration->type;
2868 /* note that we don't skip typerefs: the standard doesn't allow them here
2869 * (so we can't use is_type_function here) */
2870 if(type->kind != TYPE_FUNCTION) {
2871 if (is_type_valid(type)) {
2872 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2873 type, ndeclaration->symbol);
2879 /* § 6.7.5.3 (14) a function definition with () means no
2880 * parameters (and not unspecified parameters) */
2881 if(type->function.unspecified_parameters) {
2882 type_t *duplicate = duplicate_type(type);
2883 duplicate->function.unspecified_parameters = false;
2885 type = typehash_insert(duplicate);
2886 if(type != duplicate) {
2887 obstack_free(type_obst, duplicate);
2889 ndeclaration->type = type;
2892 declaration_t *const declaration = record_function_definition(ndeclaration);
2893 if(ndeclaration != declaration) {
2894 declaration->scope = ndeclaration->scope;
2896 type = skip_typeref(declaration->type);
2898 /* push function parameters and switch scope */
2899 int top = environment_top();
2900 scope_t *last_scope = scope;
2901 set_scope(&declaration->scope);
2903 declaration_t *parameter = declaration->scope.declarations;
2904 for( ; parameter != NULL; parameter = parameter->next) {
2905 if(parameter->parent_scope == &ndeclaration->scope) {
2906 parameter->parent_scope = scope;
2908 assert(parameter->parent_scope == NULL
2909 || parameter->parent_scope == scope);
2910 parameter->parent_scope = scope;
2911 environment_push(parameter);
2914 if(declaration->init.statement != NULL) {
2915 parser_error_multiple_definition(declaration, token.source_position);
2917 goto end_of_parse_external_declaration;
2919 /* parse function body */
2920 int label_stack_top = label_top();
2921 declaration_t *old_current_function = current_function;
2922 current_function = declaration;
2924 declaration->init.statement = parse_compound_statement();
2927 check_declarations();
2929 assert(current_function == declaration);
2930 current_function = old_current_function;
2931 label_pop_to(label_stack_top);
2934 end_of_parse_external_declaration:
2935 assert(scope == &declaration->scope);
2936 set_scope(last_scope);
2937 environment_pop_to(top);
2940 static type_t *make_bitfield_type(type_t *base, expression_t *size, source_position_t source_position)
2942 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
2943 type->bitfield.base = base;
2944 type->bitfield.size = size;
2949 static void parse_compound_declarators(declaration_t *struct_declaration,
2950 const declaration_specifiers_t *specifiers)
2952 declaration_t *last_declaration = struct_declaration->scope.declarations;
2953 if(last_declaration != NULL) {
2954 while(last_declaration->next != NULL) {
2955 last_declaration = last_declaration->next;
2960 declaration_t *declaration;
2962 if(token.type == ':') {
2963 source_position_t source_position = HERE;
2966 type_t *base_type = specifiers->type;
2967 expression_t *size = parse_constant_expression();
2969 if(!is_type_integer(skip_typeref(base_type))) {
2970 errorf(HERE, "bitfield base type '%T' is not an integer type",
2974 type_t *type = make_bitfield_type(base_type, size, source_position);
2976 declaration = allocate_declaration_zero();
2977 declaration->namespc = NAMESPACE_NORMAL;
2978 declaration->storage_class = STORAGE_CLASS_NONE;
2979 declaration->source_position = source_position;
2980 declaration->modifiers = specifiers->decl_modifiers;
2981 declaration->type = type;
2983 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2985 type_t *orig_type = declaration->type;
2986 type_t *type = skip_typeref(orig_type);
2988 if(token.type == ':') {
2989 source_position_t source_position = HERE;
2991 expression_t *size = parse_constant_expression();
2993 if(!is_type_integer(type)) {
2994 errorf(HERE, "bitfield base type '%T' is not an "
2995 "integer type", orig_type);
2998 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
2999 declaration->type = bitfield_type;
3001 /* TODO we ignore arrays for now... what is missing is a check
3002 * that they're at the end of the struct */
3003 if(is_type_incomplete(type) && !is_type_array(type)) {
3005 "compound member '%Y' has incomplete type '%T'",
3006 declaration->symbol, orig_type);
3007 } else if(is_type_function(type)) {
3008 errorf(HERE, "compound member '%Y' must not have function "
3009 "type '%T'", declaration->symbol, orig_type);
3014 /* make sure we don't define a symbol multiple times */
3015 symbol_t *symbol = declaration->symbol;
3016 if(symbol != NULL) {
3017 declaration_t *iter = struct_declaration->scope.declarations;
3018 for( ; iter != NULL; iter = iter->next) {
3019 if(iter->symbol == symbol) {
3020 errorf(declaration->source_position,
3021 "multiple declarations of symbol '%Y'", symbol);
3022 errorf(iter->source_position,
3023 "previous declaration of '%Y' was here", symbol);
3029 /* append declaration */
3030 if(last_declaration != NULL) {
3031 last_declaration->next = declaration;
3033 struct_declaration->scope.declarations = declaration;
3035 last_declaration = declaration;
3037 if(token.type != ',')
3044 static void parse_compound_type_entries(declaration_t *compound_declaration)
3048 while(token.type != '}' && token.type != T_EOF) {
3049 declaration_specifiers_t specifiers;
3050 memset(&specifiers, 0, sizeof(specifiers));
3051 parse_declaration_specifiers(&specifiers);
3053 parse_compound_declarators(compound_declaration, &specifiers);
3055 if(token.type == T_EOF) {
3056 errorf(HERE, "EOF while parsing struct");
3061 static type_t *parse_typename(void)
3063 declaration_specifiers_t specifiers;
3064 memset(&specifiers, 0, sizeof(specifiers));
3065 parse_declaration_specifiers(&specifiers);
3066 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3067 /* TODO: improve error message, user does probably not know what a
3068 * storage class is...
3070 errorf(HERE, "typename may not have a storage class");
3073 type_t *result = parse_abstract_declarator(specifiers.type);
3081 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3082 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3083 expression_t *left);
3085 typedef struct expression_parser_function_t expression_parser_function_t;
3086 struct expression_parser_function_t {
3087 unsigned precedence;
3088 parse_expression_function parser;
3089 unsigned infix_precedence;
3090 parse_expression_infix_function infix_parser;
3093 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3096 * Creates a new invalid expression.
3098 static expression_t *create_invalid_expression(void)
3100 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3101 expression->base.source_position = token.source_position;
3106 * Prints an error message if an expression was expected but not read
3108 static expression_t *expected_expression_error(void)
3110 /* skip the error message if the error token was read */
3111 if (token.type != T_ERROR) {
3112 errorf(HERE, "expected expression, got token '%K'", &token);
3116 return create_invalid_expression();
3120 * Parse a string constant.
3122 static expression_t *parse_string_const(void)
3125 if (token.type == T_STRING_LITERAL) {
3126 string_t res = token.v.string;
3128 while (token.type == T_STRING_LITERAL) {
3129 res = concat_strings(&res, &token.v.string);
3132 if (token.type != T_WIDE_STRING_LITERAL) {
3133 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3134 cnst->base.type = type_char_ptr;
3135 cnst->string.value = res;
3139 wres = concat_string_wide_string(&res, &token.v.wide_string);
3141 wres = token.v.wide_string;
3146 switch (token.type) {
3147 case T_WIDE_STRING_LITERAL:
3148 wres = concat_wide_strings(&wres, &token.v.wide_string);
3151 case T_STRING_LITERAL:
3152 wres = concat_wide_string_string(&wres, &token.v.string);
3156 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3157 cnst->base.type = type_wchar_t_ptr;
3158 cnst->wide_string.value = wres;
3167 * Parse an integer constant.
3169 static expression_t *parse_int_const(void)
3171 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3172 cnst->base.source_position = HERE;
3173 cnst->base.type = token.datatype;
3174 cnst->conste.v.int_value = token.v.intvalue;
3182 * Parse a character constant.
3184 static expression_t *parse_char_const(void)
3186 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3187 cnst->base.source_position = HERE;
3188 cnst->base.type = token.datatype;
3189 cnst->conste.v.chars.begin = token.v.string.begin;
3190 cnst->conste.v.chars.size = token.v.string.size;
3192 if (cnst->conste.v.chars.size != 1) {
3193 if (warning.multichar && (c_mode & _GNUC)) {
3195 warningf(HERE, "multi-character character constant");
3197 errorf(HERE, "more than 1 characters in character constant");
3206 * Parse a float constant.
3208 static expression_t *parse_float_const(void)
3210 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3211 cnst->base.type = token.datatype;
3212 cnst->conste.v.float_value = token.v.floatvalue;
3219 static declaration_t *create_implicit_function(symbol_t *symbol,
3220 const source_position_t source_position)
3222 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3223 ntype->function.return_type = type_int;
3224 ntype->function.unspecified_parameters = true;
3226 type_t *type = typehash_insert(ntype);
3231 declaration_t *const declaration = allocate_declaration_zero();
3232 declaration->storage_class = STORAGE_CLASS_EXTERN;
3233 declaration->type = type;
3234 declaration->symbol = symbol;
3235 declaration->source_position = source_position;
3236 declaration->parent_scope = global_scope;
3238 scope_t *old_scope = scope;
3239 set_scope(global_scope);
3241 environment_push(declaration);
3242 /* prepends the declaration to the global declarations list */
3243 declaration->next = scope->declarations;
3244 scope->declarations = declaration;
3246 assert(scope == global_scope);
3247 set_scope(old_scope);
3253 * Creates a return_type (func)(argument_type) function type if not
3256 * @param return_type the return type
3257 * @param argument_type the argument type
3259 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3261 function_parameter_t *parameter
3262 = obstack_alloc(type_obst, sizeof(parameter[0]));
3263 memset(parameter, 0, sizeof(parameter[0]));
3264 parameter->type = argument_type;
3266 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3267 type->function.return_type = return_type;
3268 type->function.parameters = parameter;
3270 type_t *result = typehash_insert(type);
3271 if(result != type) {
3279 * Creates a function type for some function like builtins.
3281 * @param symbol the symbol describing the builtin
3283 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3285 switch(symbol->ID) {
3286 case T___builtin_alloca:
3287 return make_function_1_type(type_void_ptr, type_size_t);
3288 case T___builtin_nan:
3289 return make_function_1_type(type_double, type_char_ptr);
3290 case T___builtin_nanf:
3291 return make_function_1_type(type_float, type_char_ptr);
3292 case T___builtin_nand:
3293 return make_function_1_type(type_long_double, type_char_ptr);
3294 case T___builtin_va_end:
3295 return make_function_1_type(type_void, type_valist);
3297 panic("not implemented builtin symbol found");
3302 * Performs automatic type cast as described in § 6.3.2.1.
3304 * @param orig_type the original type
3306 static type_t *automatic_type_conversion(type_t *orig_type)
3308 type_t *type = skip_typeref(orig_type);
3309 if(is_type_array(type)) {
3310 array_type_t *array_type = &type->array;
3311 type_t *element_type = array_type->element_type;
3312 unsigned qualifiers = array_type->type.qualifiers;
3314 return make_pointer_type(element_type, qualifiers);
3317 if(is_type_function(type)) {
3318 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3325 * reverts the automatic casts of array to pointer types and function
3326 * to function-pointer types as defined § 6.3.2.1
3328 type_t *revert_automatic_type_conversion(const expression_t *expression)
3330 switch (expression->kind) {
3331 case EXPR_REFERENCE: return expression->reference.declaration->type;
3332 case EXPR_SELECT: return expression->select.compound_entry->type;
3334 case EXPR_UNARY_DEREFERENCE: {
3335 const expression_t *const value = expression->unary.value;
3336 type_t *const type = skip_typeref(value->base.type);
3337 assert(is_type_pointer(type));
3338 return type->pointer.points_to;
3341 case EXPR_BUILTIN_SYMBOL:
3342 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3344 case EXPR_ARRAY_ACCESS: {
3345 const expression_t *array_ref = expression->array_access.array_ref;
3346 type_t *type_left = skip_typeref(array_ref->base.type);
3347 if (!is_type_valid(type_left))
3349 assert(is_type_pointer(type_left));
3350 return type_left->pointer.points_to;
3356 return expression->base.type;
3359 static expression_t *parse_reference(void)
3361 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3363 reference_expression_t *ref = &expression->reference;
3364 ref->symbol = token.v.symbol;
3366 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3368 source_position_t source_position = token.source_position;
3371 if(declaration == NULL) {
3372 if (! strict_mode && token.type == '(') {
3373 /* an implicitly defined function */
3374 if (warning.implicit_function_declaration) {
3375 warningf(HERE, "implicit declaration of function '%Y'",
3379 declaration = create_implicit_function(ref->symbol,
3382 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3387 type_t *type = declaration->type;
3389 /* we always do the auto-type conversions; the & and sizeof parser contains
3390 * code to revert this! */
3391 type = automatic_type_conversion(type);
3393 ref->declaration = declaration;
3394 ref->base.type = type;
3396 /* this declaration is used */
3397 declaration->used = true;
3402 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3406 /* TODO check if explicit cast is allowed and issue warnings/errors */
3409 static expression_t *parse_cast(void)
3411 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3413 cast->base.source_position = token.source_position;
3415 type_t *type = parse_typename();
3418 expression_t *value = parse_sub_expression(20);
3420 check_cast_allowed(value, type);
3422 cast->base.type = type;
3423 cast->unary.value = value;
3428 static expression_t *parse_statement_expression(void)
3430 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3432 statement_t *statement = parse_compound_statement();
3433 expression->statement.statement = statement;
3434 expression->base.source_position = statement->base.source_position;
3436 /* find last statement and use its type */
3437 type_t *type = type_void;
3438 const statement_t *stmt = statement->compound.statements;
3440 while (stmt->base.next != NULL)
3441 stmt = stmt->base.next;
3443 if (stmt->kind == STATEMENT_EXPRESSION) {
3444 type = stmt->expression.expression->base.type;
3447 warningf(expression->base.source_position, "empty statement expression ({})");
3449 expression->base.type = type;
3456 static expression_t *parse_brace_expression(void)
3460 switch(token.type) {
3462 /* gcc extension: a statement expression */
3463 return parse_statement_expression();
3467 return parse_cast();
3469 if(is_typedef_symbol(token.v.symbol)) {
3470 return parse_cast();
3474 expression_t *result = parse_expression();
3480 static expression_t *parse_function_keyword(void)
3485 if (current_function == NULL) {
3486 errorf(HERE, "'__func__' used outside of a function");
3489 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3490 expression->base.type = type_char_ptr;
3495 static expression_t *parse_pretty_function_keyword(void)
3497 eat(T___PRETTY_FUNCTION__);
3500 if (current_function == NULL) {
3501 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3504 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3505 expression->base.type = type_char_ptr;
3510 static designator_t *parse_designator(void)
3512 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3514 if(token.type != T_IDENTIFIER) {
3515 parse_error_expected("while parsing member designator",
3520 result->symbol = token.v.symbol;
3523 designator_t *last_designator = result;
3525 if(token.type == '.') {
3527 if(token.type != T_IDENTIFIER) {
3528 parse_error_expected("while parsing member designator",
3533 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3534 designator->symbol = token.v.symbol;
3537 last_designator->next = designator;
3538 last_designator = designator;
3541 if(token.type == '[') {
3543 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3544 designator->array_access = parse_expression();
3545 if(designator->array_access == NULL) {
3551 last_designator->next = designator;
3552 last_designator = designator;
3561 static expression_t *parse_offsetof(void)
3563 eat(T___builtin_offsetof);
3565 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3566 expression->base.type = type_size_t;
3569 expression->offsetofe.type = parse_typename();
3571 expression->offsetofe.designator = parse_designator();
3577 static expression_t *parse_va_start(void)
3579 eat(T___builtin_va_start);
3581 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3584 expression->va_starte.ap = parse_assignment_expression();
3586 expression_t *const expr = parse_assignment_expression();
3587 if (expr->kind == EXPR_REFERENCE) {
3588 declaration_t *const decl = expr->reference.declaration;
3590 return create_invalid_expression();
3591 if (decl->parent_scope == ¤t_function->scope &&
3592 decl->next == NULL) {
3593 expression->va_starte.parameter = decl;
3598 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3600 return create_invalid_expression();
3603 static expression_t *parse_va_arg(void)
3605 eat(T___builtin_va_arg);
3607 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3610 expression->va_arge.ap = parse_assignment_expression();
3612 expression->base.type = parse_typename();
3618 static expression_t *parse_builtin_symbol(void)
3620 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3622 symbol_t *symbol = token.v.symbol;
3624 expression->builtin_symbol.symbol = symbol;
3627 type_t *type = get_builtin_symbol_type(symbol);
3628 type = automatic_type_conversion(type);
3630 expression->base.type = type;
3634 static expression_t *parse_builtin_constant(void)
3636 eat(T___builtin_constant_p);
3638 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3641 expression->builtin_constant.value = parse_assignment_expression();
3643 expression->base.type = type_int;
3648 static expression_t *parse_builtin_prefetch(void)
3650 eat(T___builtin_prefetch);
3652 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3655 expression->builtin_prefetch.adr = parse_assignment_expression();
3656 if (token.type == ',') {
3658 expression->builtin_prefetch.rw = parse_assignment_expression();
3660 if (token.type == ',') {
3662 expression->builtin_prefetch.locality = parse_assignment_expression();
3665 expression->base.type = type_void;
3670 static expression_t *parse_compare_builtin(void)
3672 expression_t *expression;
3674 switch(token.type) {
3675 case T___builtin_isgreater:
3676 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3678 case T___builtin_isgreaterequal:
3679 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3681 case T___builtin_isless:
3682 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3684 case T___builtin_islessequal:
3685 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3687 case T___builtin_islessgreater:
3688 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3690 case T___builtin_isunordered:
3691 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3694 panic("invalid compare builtin found");
3697 expression->base.source_position = HERE;
3701 expression->binary.left = parse_assignment_expression();
3703 expression->binary.right = parse_assignment_expression();
3706 type_t *const orig_type_left = expression->binary.left->base.type;
3707 type_t *const orig_type_right = expression->binary.right->base.type;
3709 type_t *const type_left = skip_typeref(orig_type_left);
3710 type_t *const type_right = skip_typeref(orig_type_right);
3711 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3712 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3713 type_error_incompatible("invalid operands in comparison",
3714 expression->base.source_position, orig_type_left, orig_type_right);
3717 semantic_comparison(&expression->binary);
3723 static expression_t *parse_builtin_expect(void)
3725 eat(T___builtin_expect);
3727 expression_t *expression
3728 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3731 expression->binary.left = parse_assignment_expression();
3733 expression->binary.right = parse_constant_expression();
3736 expression->base.type = expression->binary.left->base.type;
3741 static expression_t *parse_assume(void) {
3744 expression_t *expression
3745 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3748 expression->unary.value = parse_assignment_expression();
3751 expression->base.type = type_void;
3755 static expression_t *parse_primary_expression(void)
3757 switch(token.type) {
3759 return parse_int_const();
3761 return parse_char_const();
3762 case T_FLOATINGPOINT:
3763 return parse_float_const();
3764 case T_STRING_LITERAL:
3765 case T_WIDE_STRING_LITERAL:
3766 return parse_string_const();
3768 return parse_reference();
3769 case T___FUNCTION__:
3771 return parse_function_keyword();
3772 case T___PRETTY_FUNCTION__:
3773 return parse_pretty_function_keyword();
3774 case T___builtin_offsetof:
3775 return parse_offsetof();
3776 case T___builtin_va_start:
3777 return parse_va_start();
3778 case T___builtin_va_arg:
3779 return parse_va_arg();
3780 case T___builtin_expect:
3781 return parse_builtin_expect();
3782 case T___builtin_alloca:
3783 case T___builtin_nan:
3784 case T___builtin_nand:
3785 case T___builtin_nanf:
3786 case T___builtin_va_end:
3787 return parse_builtin_symbol();
3788 case T___builtin_isgreater:
3789 case T___builtin_isgreaterequal:
3790 case T___builtin_isless:
3791 case T___builtin_islessequal:
3792 case T___builtin_islessgreater:
3793 case T___builtin_isunordered:
3794 return parse_compare_builtin();
3795 case T___builtin_constant_p:
3796 return parse_builtin_constant();
3797 case T___builtin_prefetch:
3798 return parse_builtin_prefetch();
3800 return parse_assume();
3803 return parse_brace_expression();
3806 errorf(HERE, "unexpected token %K", &token);
3809 return create_invalid_expression();
3813 * Check if the expression has the character type and issue a warning then.
3815 static void check_for_char_index_type(const expression_t *expression) {
3816 type_t *const type = expression->base.type;
3817 const type_t *const base_type = skip_typeref(type);
3819 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3820 warning.char_subscripts) {
3821 warningf(expression->base.source_position,
3822 "array subscript has type '%T'", type);
3826 static expression_t *parse_array_expression(unsigned precedence,
3833 expression_t *inside = parse_expression();
3835 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3837 array_access_expression_t *array_access = &expression->array_access;
3839 type_t *const orig_type_left = left->base.type;
3840 type_t *const orig_type_inside = inside->base.type;
3842 type_t *const type_left = skip_typeref(orig_type_left);
3843 type_t *const type_inside = skip_typeref(orig_type_inside);
3845 type_t *return_type;
3846 if (is_type_pointer(type_left)) {
3847 return_type = type_left->pointer.points_to;
3848 array_access->array_ref = left;
3849 array_access->index = inside;
3850 check_for_char_index_type(inside);
3851 } else if (is_type_pointer(type_inside)) {
3852 return_type = type_inside->pointer.points_to;
3853 array_access->array_ref = inside;
3854 array_access->index = left;
3855 array_access->flipped = true;
3856 check_for_char_index_type(left);
3858 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3860 "array access on object with non-pointer types '%T', '%T'",
3861 orig_type_left, orig_type_inside);
3863 return_type = type_error_type;
3864 array_access->array_ref = create_invalid_expression();
3867 if(token.type != ']') {
3868 parse_error_expected("Problem while parsing array access", ']', 0);
3873 return_type = automatic_type_conversion(return_type);
3874 expression->base.type = return_type;
3879 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3881 expression_t *tp_expression = allocate_expression_zero(kind);
3882 tp_expression->base.type = type_size_t;
3884 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3886 tp_expression->typeprop.type = parse_typename();
3889 expression_t *expression = parse_sub_expression(precedence);
3890 expression->base.type = revert_automatic_type_conversion(expression);
3892 tp_expression->typeprop.type = expression->base.type;
3893 tp_expression->typeprop.tp_expression = expression;
3896 return tp_expression;
3899 static expression_t *parse_sizeof(unsigned precedence)
3902 return parse_typeprop(EXPR_SIZEOF, precedence);
3905 static expression_t *parse_alignof(unsigned precedence)
3908 return parse_typeprop(EXPR_SIZEOF, precedence);
3911 static expression_t *parse_select_expression(unsigned precedence,
3912 expression_t *compound)
3915 assert(token.type == '.' || token.type == T_MINUSGREATER);
3917 bool is_pointer = (token.type == T_MINUSGREATER);
3920 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3921 select->select.compound = compound;
3923 if(token.type != T_IDENTIFIER) {
3924 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3927 symbol_t *symbol = token.v.symbol;
3928 select->select.symbol = symbol;
3931 type_t *const orig_type = compound->base.type;
3932 type_t *const type = skip_typeref(orig_type);
3934 type_t *type_left = type;
3936 if (!is_type_pointer(type)) {
3937 if (is_type_valid(type)) {
3938 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3940 return create_invalid_expression();
3942 type_left = type->pointer.points_to;
3944 type_left = skip_typeref(type_left);
3946 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3947 type_left->kind != TYPE_COMPOUND_UNION) {
3948 if (is_type_valid(type_left)) {
3949 errorf(HERE, "request for member '%Y' in something not a struct or "
3950 "union, but '%T'", symbol, type_left);
3952 return create_invalid_expression();
3955 declaration_t *const declaration = type_left->compound.declaration;
3957 if(!declaration->init.is_defined) {
3958 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3960 return create_invalid_expression();
3963 declaration_t *iter = declaration->scope.declarations;
3964 for( ; iter != NULL; iter = iter->next) {
3965 if(iter->symbol == symbol) {
3970 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3971 return create_invalid_expression();
3974 /* we always do the auto-type conversions; the & and sizeof parser contains
3975 * code to revert this! */
3976 type_t *expression_type = automatic_type_conversion(iter->type);
3978 select->select.compound_entry = iter;
3979 select->base.type = expression_type;
3981 if(expression_type->kind == TYPE_BITFIELD) {
3982 expression_t *extract
3983 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3984 extract->unary.value = select;
3985 extract->base.type = expression_type->bitfield.base;
3994 * Parse a call expression, ie. expression '( ... )'.
3996 * @param expression the function address
3998 static expression_t *parse_call_expression(unsigned precedence,
3999 expression_t *expression)
4002 expression_t *result = allocate_expression_zero(EXPR_CALL);
4004 call_expression_t *call = &result->call;
4005 call->function = expression;
4007 type_t *const orig_type = expression->base.type;
4008 type_t *const type = skip_typeref(orig_type);
4010 function_type_t *function_type = NULL;
4011 if (is_type_pointer(type)) {
4012 type_t *const to_type = skip_typeref(type->pointer.points_to);
4014 if (is_type_function(to_type)) {
4015 function_type = &to_type->function;
4016 call->base.type = function_type->return_type;
4020 if (function_type == NULL && is_type_valid(type)) {
4021 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4024 /* parse arguments */
4027 if(token.type != ')') {
4028 call_argument_t *last_argument = NULL;
4031 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4033 argument->expression = parse_assignment_expression();
4034 if(last_argument == NULL) {
4035 call->arguments = argument;
4037 last_argument->next = argument;
4039 last_argument = argument;
4041 if(token.type != ',')
4048 if(function_type != NULL) {
4049 function_parameter_t *parameter = function_type->parameters;
4050 call_argument_t *argument = call->arguments;
4051 for( ; parameter != NULL && argument != NULL;
4052 parameter = parameter->next, argument = argument->next) {
4053 type_t *expected_type = parameter->type;
4054 /* TODO report scope in error messages */
4055 expression_t *const arg_expr = argument->expression;
4056 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4057 if (res_type == NULL) {
4058 /* TODO improve error message */
4059 errorf(arg_expr->base.source_position,
4060 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4061 arg_expr, arg_expr->base.type, expected_type);
4063 argument->expression = create_implicit_cast(argument->expression, expected_type);
4066 /* too few parameters */
4067 if(parameter != NULL) {
4068 errorf(HERE, "too few arguments to function '%E'", expression);
4069 } else if(argument != NULL) {
4070 /* too many parameters */
4071 if(!function_type->variadic
4072 && !function_type->unspecified_parameters) {
4073 errorf(HERE, "too many arguments to function '%E'", expression);
4075 /* do default promotion */
4076 for( ; argument != NULL; argument = argument->next) {
4077 type_t *type = argument->expression->base.type;
4079 type = skip_typeref(type);
4080 if(is_type_integer(type)) {
4081 type = promote_integer(type);
4082 } else if(type == type_float) {
4086 argument->expression
4087 = create_implicit_cast(argument->expression, type);
4090 check_format(&result->call);
4093 check_format(&result->call);
4100 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4102 static bool same_compound_type(const type_t *type1, const type_t *type2)
4105 is_type_compound(type1) &&
4106 type1->kind == type2->kind &&
4107 type1->compound.declaration == type2->compound.declaration;
4111 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4113 * @param expression the conditional expression
4115 static expression_t *parse_conditional_expression(unsigned precedence,
4116 expression_t *expression)
4120 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4122 conditional_expression_t *conditional = &result->conditional;
4123 conditional->condition = expression;
4126 type_t *const condition_type_orig = expression->base.type;
4127 type_t *const condition_type = skip_typeref(condition_type_orig);
4128 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4129 type_error("expected a scalar type in conditional condition",
4130 expression->base.source_position, condition_type_orig);
4133 expression_t *true_expression = parse_expression();
4135 expression_t *false_expression = parse_sub_expression(precedence);
4137 conditional->true_expression = true_expression;
4138 conditional->false_expression = false_expression;
4140 type_t *const orig_true_type = true_expression->base.type;
4141 type_t *const orig_false_type = false_expression->base.type;
4142 type_t *const true_type = skip_typeref(orig_true_type);
4143 type_t *const false_type = skip_typeref(orig_false_type);
4146 type_t *result_type;
4147 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4148 result_type = semantic_arithmetic(true_type, false_type);
4150 true_expression = create_implicit_cast(true_expression, result_type);
4151 false_expression = create_implicit_cast(false_expression, result_type);
4153 conditional->true_expression = true_expression;
4154 conditional->false_expression = false_expression;
4155 conditional->base.type = result_type;
4156 } else if (same_compound_type(true_type, false_type) || (
4157 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4158 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4160 /* just take 1 of the 2 types */
4161 result_type = true_type;
4162 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4163 && pointers_compatible(true_type, false_type)) {
4165 result_type = true_type;
4168 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4169 type_error_incompatible("while parsing conditional",
4170 expression->base.source_position, true_type,
4173 result_type = type_error_type;
4176 conditional->base.type = result_type;
4181 * Parse an extension expression.
4183 static expression_t *parse_extension(unsigned precedence)
4185 eat(T___extension__);
4187 /* TODO enable extensions */
4188 expression_t *expression = parse_sub_expression(precedence);
4189 /* TODO disable extensions */
4193 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4195 eat(T___builtin_classify_type);
4197 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4198 result->base.type = type_int;
4201 expression_t *expression = parse_sub_expression(precedence);
4203 result->classify_type.type_expression = expression;
4208 static void semantic_incdec(unary_expression_t *expression)
4210 type_t *const orig_type = expression->value->base.type;
4211 type_t *const type = skip_typeref(orig_type);
4212 /* TODO !is_type_real && !is_type_pointer */
4213 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4214 if (is_type_valid(type)) {
4215 /* TODO: improve error message */
4216 errorf(HERE, "operation needs an arithmetic or pointer type");
4221 expression->base.type = orig_type;
4224 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4226 type_t *const orig_type = expression->value->base.type;
4227 type_t *const type = skip_typeref(orig_type);
4228 if(!is_type_arithmetic(type)) {
4229 if (is_type_valid(type)) {
4230 /* TODO: improve error message */
4231 errorf(HERE, "operation needs an arithmetic type");
4236 expression->base.type = orig_type;
4239 static void semantic_unexpr_scalar(unary_expression_t *expression)
4241 type_t *const orig_type = expression->value->base.type;
4242 type_t *const type = skip_typeref(orig_type);
4243 if (!is_type_scalar(type)) {
4244 if (is_type_valid(type)) {
4245 errorf(HERE, "operand of ! must be of scalar type");
4250 expression->base.type = orig_type;
4253 static void semantic_unexpr_integer(unary_expression_t *expression)
4255 type_t *const orig_type = expression->value->base.type;
4256 type_t *const type = skip_typeref(orig_type);
4257 if (!is_type_integer(type)) {
4258 if (is_type_valid(type)) {
4259 errorf(HERE, "operand of ~ must be of integer type");
4264 expression->base.type = orig_type;
4267 static void semantic_dereference(unary_expression_t *expression)
4269 type_t *const orig_type = expression->value->base.type;
4270 type_t *const type = skip_typeref(orig_type);
4271 if(!is_type_pointer(type)) {
4272 if (is_type_valid(type)) {
4273 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4278 type_t *result_type = type->pointer.points_to;
4279 result_type = automatic_type_conversion(result_type);
4280 expression->base.type = result_type;
4284 * Check the semantic of the address taken expression.
4286 static void semantic_take_addr(unary_expression_t *expression)
4288 expression_t *value = expression->value;
4289 value->base.type = revert_automatic_type_conversion(value);
4291 type_t *orig_type = value->base.type;
4292 if(!is_type_valid(orig_type))
4295 if(value->kind == EXPR_REFERENCE) {
4296 declaration_t *const declaration = value->reference.declaration;
4297 if(declaration != NULL) {
4298 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4299 errorf(expression->base.source_position,
4300 "address of register variable '%Y' requested",
4301 declaration->symbol);
4303 declaration->address_taken = 1;
4307 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4310 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4311 static expression_t *parse_##unexpression_type(unsigned precedence) \
4315 expression_t *unary_expression \
4316 = allocate_expression_zero(unexpression_type); \
4317 unary_expression->base.source_position = HERE; \
4318 unary_expression->unary.value = parse_sub_expression(precedence); \
4320 sfunc(&unary_expression->unary); \
4322 return unary_expression; \
4325 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4326 semantic_unexpr_arithmetic)
4327 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4328 semantic_unexpr_arithmetic)
4329 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4330 semantic_unexpr_scalar)
4331 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4332 semantic_dereference)
4333 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4335 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4336 semantic_unexpr_integer)
4337 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4339 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4342 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4344 static expression_t *parse_##unexpression_type(unsigned precedence, \
4345 expression_t *left) \
4347 (void) precedence; \
4350 expression_t *unary_expression \
4351 = allocate_expression_zero(unexpression_type); \
4352 unary_expression->unary.value = left; \
4354 sfunc(&unary_expression->unary); \
4356 return unary_expression; \
4359 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4360 EXPR_UNARY_POSTFIX_INCREMENT,
4362 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4363 EXPR_UNARY_POSTFIX_DECREMENT,
4366 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4368 /* TODO: handle complex + imaginary types */
4370 /* § 6.3.1.8 Usual arithmetic conversions */
4371 if(type_left == type_long_double || type_right == type_long_double) {
4372 return type_long_double;
4373 } else if(type_left == type_double || type_right == type_double) {
4375 } else if(type_left == type_float || type_right == type_float) {
4379 type_right = promote_integer(type_right);
4380 type_left = promote_integer(type_left);
4382 if(type_left == type_right)
4385 bool signed_left = is_type_signed(type_left);
4386 bool signed_right = is_type_signed(type_right);
4387 int rank_left = get_rank(type_left);
4388 int rank_right = get_rank(type_right);
4389 if(rank_left < rank_right) {
4390 if(signed_left == signed_right || !signed_right) {
4396 if(signed_left == signed_right || !signed_left) {
4405 * Check the semantic restrictions for a binary expression.
4407 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4409 expression_t *const left = expression->left;
4410 expression_t *const right = expression->right;
4411 type_t *const orig_type_left = left->base.type;
4412 type_t *const orig_type_right = right->base.type;
4413 type_t *const type_left = skip_typeref(orig_type_left);
4414 type_t *const type_right = skip_typeref(orig_type_right);
4416 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4417 /* TODO: improve error message */
4418 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4419 errorf(HERE, "operation needs arithmetic types");
4424 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4425 expression->left = create_implicit_cast(left, arithmetic_type);
4426 expression->right = create_implicit_cast(right, arithmetic_type);
4427 expression->base.type = arithmetic_type;
4430 static void semantic_shift_op(binary_expression_t *expression)
4432 expression_t *const left = expression->left;
4433 expression_t *const right = expression->right;
4434 type_t *const orig_type_left = left->base.type;
4435 type_t *const orig_type_right = right->base.type;
4436 type_t * type_left = skip_typeref(orig_type_left);
4437 type_t * type_right = skip_typeref(orig_type_right);
4439 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4440 /* TODO: improve error message */
4441 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4442 errorf(HERE, "operation needs integer types");
4447 type_left = promote_integer(type_left);
4448 type_right = promote_integer(type_right);
4450 expression->left = create_implicit_cast(left, type_left);
4451 expression->right = create_implicit_cast(right, type_right);
4452 expression->base.type = type_left;
4455 static void semantic_add(binary_expression_t *expression)
4457 expression_t *const left = expression->left;
4458 expression_t *const right = expression->right;
4459 type_t *const orig_type_left = left->base.type;
4460 type_t *const orig_type_right = right->base.type;
4461 type_t *const type_left = skip_typeref(orig_type_left);
4462 type_t *const type_right = skip_typeref(orig_type_right);
4465 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4466 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4467 expression->left = create_implicit_cast(left, arithmetic_type);
4468 expression->right = create_implicit_cast(right, arithmetic_type);
4469 expression->base.type = arithmetic_type;
4471 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4472 expression->base.type = type_left;
4473 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4474 expression->base.type = type_right;
4475 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4476 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4480 static void semantic_sub(binary_expression_t *expression)
4482 expression_t *const left = expression->left;
4483 expression_t *const right = expression->right;
4484 type_t *const orig_type_left = left->base.type;
4485 type_t *const orig_type_right = right->base.type;
4486 type_t *const type_left = skip_typeref(orig_type_left);
4487 type_t *const type_right = skip_typeref(orig_type_right);
4490 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4491 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4492 expression->left = create_implicit_cast(left, arithmetic_type);
4493 expression->right = create_implicit_cast(right, arithmetic_type);
4494 expression->base.type = arithmetic_type;
4496 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4497 expression->base.type = type_left;
4498 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4499 if(!pointers_compatible(type_left, type_right)) {
4501 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4502 orig_type_left, orig_type_right);
4504 expression->base.type = type_ptrdiff_t;
4506 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4507 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4508 orig_type_left, orig_type_right);
4513 * Check the semantics of comparison expressions.
4515 * @param expression The expression to check.
4517 static void semantic_comparison(binary_expression_t *expression)
4519 expression_t *left = expression->left;
4520 expression_t *right = expression->right;
4521 type_t *orig_type_left = left->base.type;
4522 type_t *orig_type_right = right->base.type;
4524 type_t *type_left = skip_typeref(orig_type_left);
4525 type_t *type_right = skip_typeref(orig_type_right);
4527 /* TODO non-arithmetic types */
4528 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4529 if (warning.sign_compare &&
4530 (expression->base.kind != EXPR_BINARY_EQUAL &&
4531 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4532 (is_type_signed(type_left) != is_type_signed(type_right))) {
4533 warningf(expression->base.source_position,
4534 "comparison between signed and unsigned");
4536 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4537 expression->left = create_implicit_cast(left, arithmetic_type);
4538 expression->right = create_implicit_cast(right, arithmetic_type);
4539 expression->base.type = arithmetic_type;
4540 if (warning.float_equal &&
4541 (expression->base.kind == EXPR_BINARY_EQUAL ||
4542 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4543 is_type_float(arithmetic_type)) {
4544 warningf(expression->base.source_position,
4545 "comparing floating point with == or != is unsafe");
4547 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4548 /* TODO check compatibility */
4549 } else if (is_type_pointer(type_left)) {
4550 expression->right = create_implicit_cast(right, type_left);
4551 } else if (is_type_pointer(type_right)) {
4552 expression->left = create_implicit_cast(left, type_right);
4553 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4554 type_error_incompatible("invalid operands in comparison",
4555 expression->base.source_position,
4556 type_left, type_right);
4558 expression->base.type = type_int;
4561 static void semantic_arithmetic_assign(binary_expression_t *expression)
4563 expression_t *left = expression->left;
4564 expression_t *right = expression->right;
4565 type_t *orig_type_left = left->base.type;
4566 type_t *orig_type_right = right->base.type;
4568 type_t *type_left = skip_typeref(orig_type_left);
4569 type_t *type_right = skip_typeref(orig_type_right);
4571 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4572 /* TODO: improve error message */
4573 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4574 errorf(HERE, "operation needs arithmetic types");
4579 /* combined instructions are tricky. We can't create an implicit cast on
4580 * the left side, because we need the uncasted form for the store.
4581 * The ast2firm pass has to know that left_type must be right_type
4582 * for the arithmetic operation and create a cast by itself */
4583 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4584 expression->right = create_implicit_cast(right, arithmetic_type);
4585 expression->base.type = type_left;
4588 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4590 expression_t *const left = expression->left;
4591 expression_t *const right = expression->right;
4592 type_t *const orig_type_left = left->base.type;
4593 type_t *const orig_type_right = right->base.type;
4594 type_t *const type_left = skip_typeref(orig_type_left);
4595 type_t *const type_right = skip_typeref(orig_type_right);
4597 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4598 /* combined instructions are tricky. We can't create an implicit cast on
4599 * the left side, because we need the uncasted form for the store.
4600 * The ast2firm pass has to know that left_type must be right_type
4601 * for the arithmetic operation and create a cast by itself */
4602 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4603 expression->right = create_implicit_cast(right, arithmetic_type);
4604 expression->base.type = type_left;
4605 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4606 expression->base.type = type_left;
4607 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4608 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4613 * Check the semantic restrictions of a logical expression.
4615 static void semantic_logical_op(binary_expression_t *expression)
4617 expression_t *const left = expression->left;
4618 expression_t *const right = expression->right;
4619 type_t *const orig_type_left = left->base.type;
4620 type_t *const orig_type_right = right->base.type;
4621 type_t *const type_left = skip_typeref(orig_type_left);
4622 type_t *const type_right = skip_typeref(orig_type_right);
4624 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4625 /* TODO: improve error message */
4626 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4627 errorf(HERE, "operation needs scalar types");
4632 expression->base.type = type_int;
4636 * Checks if a compound type has constant fields.
4638 static bool has_const_fields(const compound_type_t *type)
4640 const scope_t *scope = &type->declaration->scope;
4641 const declaration_t *declaration = scope->declarations;
4643 for (; declaration != NULL; declaration = declaration->next) {
4644 if (declaration->namespc != NAMESPACE_NORMAL)
4647 const type_t *decl_type = skip_typeref(declaration->type);
4648 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4656 * Check the semantic restrictions of a binary assign expression.
4658 static void semantic_binexpr_assign(binary_expression_t *expression)
4660 expression_t *left = expression->left;
4661 type_t *orig_type_left = left->base.type;
4663 type_t *type_left = revert_automatic_type_conversion(left);
4664 type_left = skip_typeref(orig_type_left);
4666 /* must be a modifiable lvalue */
4667 if (is_type_array(type_left)) {
4668 errorf(HERE, "cannot assign to arrays ('%E')", left);
4671 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4672 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4676 if(is_type_incomplete(type_left)) {
4678 "left-hand side of assignment '%E' has incomplete type '%T'",
4679 left, orig_type_left);
4682 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4683 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4684 left, orig_type_left);
4688 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4690 if (res_type == NULL) {
4691 errorf(expression->base.source_position,
4692 "cannot assign to '%T' from '%T'",
4693 orig_type_left, expression->right->base.type);
4695 expression->right = create_implicit_cast(expression->right, res_type);
4698 expression->base.type = orig_type_left;
4701 static bool expression_has_effect(const expression_t *const expr)
4703 switch (expr->kind) {
4704 case EXPR_UNKNOWN: break;
4705 case EXPR_INVALID: break;
4706 case EXPR_REFERENCE: return false;
4707 case EXPR_CONST: return false;
4708 case EXPR_CHAR_CONST: return false;
4709 case EXPR_STRING_LITERAL: return false;
4710 case EXPR_WIDE_STRING_LITERAL: return false;
4712 const call_expression_t *const call = &expr->call;
4713 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4716 switch (call->function->builtin_symbol.symbol->ID) {
4717 case T___builtin_va_end: return true;
4718 default: return false;
4721 case EXPR_CONDITIONAL: {
4722 const conditional_expression_t *const cond = &expr->conditional;
4724 expression_has_effect(cond->true_expression) &&
4725 expression_has_effect(cond->false_expression);
4727 case EXPR_SELECT: return false;
4728 case EXPR_ARRAY_ACCESS: return false;
4729 case EXPR_SIZEOF: return false;
4730 case EXPR_CLASSIFY_TYPE: return false;
4731 case EXPR_ALIGNOF: return false;
4733 case EXPR_FUNCTION: return false;
4734 case EXPR_PRETTY_FUNCTION: return false;
4735 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4736 case EXPR_BUILTIN_CONSTANT_P: return false;
4737 case EXPR_BUILTIN_PREFETCH: return true;
4738 case EXPR_OFFSETOF: return false;
4739 case EXPR_VA_START: return true;
4740 case EXPR_VA_ARG: return true;
4741 case EXPR_STATEMENT: return true; // TODO
4743 case EXPR_UNARY_NEGATE: return false;
4744 case EXPR_UNARY_PLUS: return false;
4745 case EXPR_UNARY_BITWISE_NEGATE: return false;
4746 case EXPR_UNARY_NOT: return false;
4747 case EXPR_UNARY_DEREFERENCE: return false;
4748 case EXPR_UNARY_TAKE_ADDRESS: return false;
4749 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4750 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4751 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4752 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4753 case EXPR_UNARY_CAST:
4754 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4755 case EXPR_UNARY_CAST_IMPLICIT: return true;
4756 case EXPR_UNARY_ASSUME: return true;
4757 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4759 case EXPR_BINARY_ADD: return false;
4760 case EXPR_BINARY_SUB: return false;
4761 case EXPR_BINARY_MUL: return false;
4762 case EXPR_BINARY_DIV: return false;
4763 case EXPR_BINARY_MOD: return false;
4764 case EXPR_BINARY_EQUAL: return false;
4765 case EXPR_BINARY_NOTEQUAL: return false;
4766 case EXPR_BINARY_LESS: return false;
4767 case EXPR_BINARY_LESSEQUAL: return false;
4768 case EXPR_BINARY_GREATER: return false;
4769 case EXPR_BINARY_GREATEREQUAL: return false;
4770 case EXPR_BINARY_BITWISE_AND: return false;
4771 case EXPR_BINARY_BITWISE_OR: return false;
4772 case EXPR_BINARY_BITWISE_XOR: return false;
4773 case EXPR_BINARY_SHIFTLEFT: return false;
4774 case EXPR_BINARY_SHIFTRIGHT: return false;
4775 case EXPR_BINARY_ASSIGN: return true;
4776 case EXPR_BINARY_MUL_ASSIGN: return true;
4777 case EXPR_BINARY_DIV_ASSIGN: return true;
4778 case EXPR_BINARY_MOD_ASSIGN: return true;
4779 case EXPR_BINARY_ADD_ASSIGN: return true;
4780 case EXPR_BINARY_SUB_ASSIGN: return true;
4781 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4782 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4783 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4784 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4785 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4786 case EXPR_BINARY_LOGICAL_AND:
4787 case EXPR_BINARY_LOGICAL_OR:
4788 case EXPR_BINARY_COMMA:
4789 return expression_has_effect(expr->binary.right);
4791 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4792 case EXPR_BINARY_ISGREATER: return false;
4793 case EXPR_BINARY_ISGREATEREQUAL: return false;
4794 case EXPR_BINARY_ISLESS: return false;
4795 case EXPR_BINARY_ISLESSEQUAL: return false;
4796 case EXPR_BINARY_ISLESSGREATER: return false;
4797 case EXPR_BINARY_ISUNORDERED: return false;
4800 panic("unexpected statement");
4803 static void semantic_comma(binary_expression_t *expression)
4805 if (warning.unused_value) {
4806 const expression_t *const left = expression->left;
4807 if (!expression_has_effect(left)) {
4808 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4811 expression->base.type = expression->right->base.type;
4814 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4815 static expression_t *parse_##binexpression_type(unsigned precedence, \
4816 expression_t *left) \
4819 source_position_t pos = HERE; \
4821 expression_t *right = parse_sub_expression(precedence + lr); \
4823 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4824 binexpr->base.source_position = pos; \
4825 binexpr->binary.left = left; \
4826 binexpr->binary.right = right; \
4827 sfunc(&binexpr->binary); \
4832 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4833 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4834 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4835 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4836 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4837 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4838 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4839 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4840 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4842 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4843 semantic_comparison, 1)
4844 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4845 semantic_comparison, 1)
4846 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4847 semantic_comparison, 1)
4848 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4849 semantic_comparison, 1)
4851 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4852 semantic_binexpr_arithmetic, 1)
4853 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4854 semantic_binexpr_arithmetic, 1)
4855 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4856 semantic_binexpr_arithmetic, 1)
4857 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4858 semantic_logical_op, 1)
4859 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4860 semantic_logical_op, 1)
4861 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4862 semantic_shift_op, 1)
4863 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4864 semantic_shift_op, 1)
4865 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4866 semantic_arithmetic_addsubb_assign, 0)
4867 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4868 semantic_arithmetic_addsubb_assign, 0)
4869 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4870 semantic_arithmetic_assign, 0)
4871 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4872 semantic_arithmetic_assign, 0)
4873 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4874 semantic_arithmetic_assign, 0)
4875 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4876 semantic_arithmetic_assign, 0)
4877 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4878 semantic_arithmetic_assign, 0)
4879 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4880 semantic_arithmetic_assign, 0)
4881 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4882 semantic_arithmetic_assign, 0)
4883 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4884 semantic_arithmetic_assign, 0)
4886 static expression_t *parse_sub_expression(unsigned precedence)
4888 if(token.type < 0) {
4889 return expected_expression_error();
4892 expression_parser_function_t *parser
4893 = &expression_parsers[token.type];
4894 source_position_t source_position = token.source_position;
4897 if(parser->parser != NULL) {
4898 left = parser->parser(parser->precedence);
4900 left = parse_primary_expression();
4902 assert(left != NULL);
4903 left->base.source_position = source_position;
4906 if(token.type < 0) {
4907 return expected_expression_error();
4910 parser = &expression_parsers[token.type];
4911 if(parser->infix_parser == NULL)
4913 if(parser->infix_precedence < precedence)
4916 left = parser->infix_parser(parser->infix_precedence, left);
4918 assert(left != NULL);
4919 assert(left->kind != EXPR_UNKNOWN);
4920 left->base.source_position = source_position;
4927 * Parse an expression.
4929 static expression_t *parse_expression(void)
4931 return parse_sub_expression(1);
4935 * Register a parser for a prefix-like operator with given precedence.
4937 * @param parser the parser function
4938 * @param token_type the token type of the prefix token
4939 * @param precedence the precedence of the operator
4941 static void register_expression_parser(parse_expression_function parser,
4942 int token_type, unsigned precedence)
4944 expression_parser_function_t *entry = &expression_parsers[token_type];
4946 if(entry->parser != NULL) {
4947 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4948 panic("trying to register multiple expression parsers for a token");
4950 entry->parser = parser;
4951 entry->precedence = precedence;
4955 * Register a parser for an infix operator with given precedence.
4957 * @param parser the parser function
4958 * @param token_type the token type of the infix operator
4959 * @param precedence the precedence of the operator
4961 static void register_infix_parser(parse_expression_infix_function parser,
4962 int token_type, unsigned precedence)
4964 expression_parser_function_t *entry = &expression_parsers[token_type];
4966 if(entry->infix_parser != NULL) {
4967 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4968 panic("trying to register multiple infix expression parsers for a "
4971 entry->infix_parser = parser;
4972 entry->infix_precedence = precedence;
4976 * Initialize the expression parsers.
4978 static void init_expression_parsers(void)
4980 memset(&expression_parsers, 0, sizeof(expression_parsers));
4982 register_infix_parser(parse_array_expression, '[', 30);
4983 register_infix_parser(parse_call_expression, '(', 30);
4984 register_infix_parser(parse_select_expression, '.', 30);
4985 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4986 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4988 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4991 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4992 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4993 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4994 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4995 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4996 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4997 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4998 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4999 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5000 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5001 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5002 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5003 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5004 T_EXCLAMATIONMARKEQUAL, 13);
5005 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5006 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5007 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5008 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5009 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5010 register_infix_parser(parse_conditional_expression, '?', 7);
5011 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5012 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5013 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5014 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5015 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5016 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5017 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5018 T_LESSLESSEQUAL, 2);
5019 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5020 T_GREATERGREATEREQUAL, 2);
5021 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5023 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5025 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5028 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5030 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5031 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5032 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5033 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5034 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5035 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5036 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5038 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5040 register_expression_parser(parse_sizeof, T_sizeof, 25);
5041 register_expression_parser(parse_alignof, T___alignof__, 25);
5042 register_expression_parser(parse_extension, T___extension__, 25);
5043 register_expression_parser(parse_builtin_classify_type,
5044 T___builtin_classify_type, 25);
5048 * Parse a asm statement constraints specification.
5050 static asm_constraint_t *parse_asm_constraints(void)
5052 asm_constraint_t *result = NULL;
5053 asm_constraint_t *last = NULL;
5055 while(token.type == T_STRING_LITERAL || token.type == '[') {
5056 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5057 memset(constraint, 0, sizeof(constraint[0]));
5059 if(token.type == '[') {
5061 if(token.type != T_IDENTIFIER) {
5062 parse_error_expected("while parsing asm constraint",
5066 constraint->symbol = token.v.symbol;
5071 constraint->constraints = parse_string_literals();
5073 constraint->expression = parse_expression();
5077 last->next = constraint;
5079 result = constraint;
5083 if(token.type != ',')
5092 * Parse a asm statement clobber specification.
5094 static asm_clobber_t *parse_asm_clobbers(void)
5096 asm_clobber_t *result = NULL;
5097 asm_clobber_t *last = NULL;
5099 while(token.type == T_STRING_LITERAL) {
5100 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5101 clobber->clobber = parse_string_literals();
5104 last->next = clobber;
5110 if(token.type != ',')
5119 * Parse an asm statement.
5121 static statement_t *parse_asm_statement(void)
5125 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5126 statement->base.source_position = token.source_position;
5128 asm_statement_t *asm_statement = &statement->asms;
5130 if(token.type == T_volatile) {
5132 asm_statement->is_volatile = true;
5136 asm_statement->asm_text = parse_string_literals();
5138 if(token.type != ':')
5142 asm_statement->inputs = parse_asm_constraints();
5143 if(token.type != ':')
5147 asm_statement->outputs = parse_asm_constraints();
5148 if(token.type != ':')
5152 asm_statement->clobbers = parse_asm_clobbers();
5161 * Parse a case statement.
5163 static statement_t *parse_case_statement(void)
5167 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5169 statement->base.source_position = token.source_position;
5170 statement->case_label.expression = parse_expression();
5172 if (c_mode & _GNUC) {
5173 if (token.type == T_DOTDOTDOT) {
5175 statement->case_label.end_range = parse_expression();
5181 if (! is_constant_expression(statement->case_label.expression)) {
5182 errorf(statement->base.source_position,
5183 "case label does not reduce to an integer constant");
5185 /* TODO: check if the case label is already known */
5186 if (current_switch != NULL) {
5187 /* link all cases into the switch statement */
5188 if (current_switch->last_case == NULL) {
5189 current_switch->first_case =
5190 current_switch->last_case = &statement->case_label;
5192 current_switch->last_case->next = &statement->case_label;
5195 errorf(statement->base.source_position,
5196 "case label not within a switch statement");
5199 statement->case_label.statement = parse_statement();
5205 * Finds an existing default label of a switch statement.
5207 static case_label_statement_t *
5208 find_default_label(const switch_statement_t *statement)
5210 for (case_label_statement_t *label = statement->first_case;
5212 label = label->next) {
5213 if (label->expression == NULL)
5220 * Parse a default statement.
5222 static statement_t *parse_default_statement(void)
5226 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5228 statement->base.source_position = token.source_position;
5231 if (current_switch != NULL) {
5232 const case_label_statement_t *def_label = find_default_label(current_switch);
5233 if (def_label != NULL) {
5234 errorf(HERE, "multiple default labels in one switch");
5235 errorf(def_label->base.source_position,
5236 "this is the first default label");
5238 /* link all cases into the switch statement */
5239 if (current_switch->last_case == NULL) {
5240 current_switch->first_case =
5241 current_switch->last_case = &statement->case_label;
5243 current_switch->last_case->next = &statement->case_label;
5247 errorf(statement->base.source_position,
5248 "'default' label not within a switch statement");
5250 statement->label.statement = parse_statement();
5256 * Return the declaration for a given label symbol or create a new one.
5258 static declaration_t *get_label(symbol_t *symbol)
5260 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5261 assert(current_function != NULL);
5262 /* if we found a label in the same function, then we already created the
5264 if(candidate != NULL
5265 && candidate->parent_scope == ¤t_function->scope) {
5269 /* otherwise we need to create a new one */
5270 declaration_t *const declaration = allocate_declaration_zero();
5271 declaration->namespc = NAMESPACE_LABEL;
5272 declaration->symbol = symbol;
5274 label_push(declaration);
5280 * Parse a label statement.
5282 static statement_t *parse_label_statement(void)
5284 assert(token.type == T_IDENTIFIER);
5285 symbol_t *symbol = token.v.symbol;
5288 declaration_t *label = get_label(symbol);
5290 /* if source position is already set then the label is defined twice,
5291 * otherwise it was just mentioned in a goto so far */
5292 if(label->source_position.input_name != NULL) {
5293 errorf(HERE, "duplicate label '%Y'", symbol);
5294 errorf(label->source_position, "previous definition of '%Y' was here",
5297 label->source_position = token.source_position;
5300 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5302 statement->base.source_position = token.source_position;
5303 statement->label.label = label;
5307 if(token.type == '}') {
5308 /* TODO only warn? */
5309 errorf(HERE, "label at end of compound statement");
5312 if (token.type == ';') {
5313 /* eat an empty statement here, to avoid the warning about an empty
5314 * after a label. label:; is commonly used to have a label before
5318 statement->label.statement = parse_statement();
5322 /* remember the labels's in a list for later checking */
5323 if (label_last == NULL) {
5324 label_first = &statement->label;
5326 label_last->next = &statement->label;
5328 label_last = &statement->label;
5334 * Parse an if statement.
5336 static statement_t *parse_if(void)
5340 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5341 statement->base.source_position = token.source_position;
5344 statement->ifs.condition = parse_expression();
5347 statement->ifs.true_statement = parse_statement();
5348 if(token.type == T_else) {
5350 statement->ifs.false_statement = parse_statement();
5357 * Parse a switch statement.
5359 static statement_t *parse_switch(void)
5363 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5364 statement->base.source_position = token.source_position;
5367 expression_t *const expr = parse_expression();
5368 type_t * type = skip_typeref(expr->base.type);
5369 if (is_type_integer(type)) {
5370 type = promote_integer(type);
5371 } else if (is_type_valid(type)) {
5372 errorf(expr->base.source_position,
5373 "switch quantity is not an integer, but '%T'", type);
5374 type = type_error_type;
5376 statement->switchs.expression = create_implicit_cast(expr, type);
5379 switch_statement_t *rem = current_switch;
5380 current_switch = &statement->switchs;
5381 statement->switchs.body = parse_statement();
5382 current_switch = rem;
5384 if (warning.switch_default
5385 && find_default_label(&statement->switchs) == NULL) {
5386 warningf(statement->base.source_position, "switch has no default case");
5392 static statement_t *parse_loop_body(statement_t *const loop)
5394 statement_t *const rem = current_loop;
5395 current_loop = loop;
5397 statement_t *const body = parse_statement();
5404 * Parse a while statement.
5406 static statement_t *parse_while(void)
5410 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5411 statement->base.source_position = token.source_position;
5414 statement->whiles.condition = parse_expression();
5417 statement->whiles.body = parse_loop_body(statement);
5423 * Parse a do statement.
5425 static statement_t *parse_do(void)
5429 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5431 statement->base.source_position = token.source_position;
5433 statement->do_while.body = parse_loop_body(statement);
5437 statement->do_while.condition = parse_expression();
5445 * Parse a for statement.
5447 static statement_t *parse_for(void)
5451 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5452 statement->base.source_position = token.source_position;
5456 int top = environment_top();
5457 scope_t *last_scope = scope;
5458 set_scope(&statement->fors.scope);
5460 if(token.type != ';') {
5461 if(is_declaration_specifier(&token, false)) {
5462 parse_declaration(record_declaration);
5464 expression_t *const init = parse_expression();
5465 statement->fors.initialisation = init;
5466 if (warning.unused_value && !expression_has_effect(init)) {
5467 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5475 if(token.type != ';') {
5476 statement->fors.condition = parse_expression();
5479 if(token.type != ')') {
5480 expression_t *const step = parse_expression();
5481 statement->fors.step = step;
5482 if (warning.unused_value && !expression_has_effect(step)) {
5483 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5487 statement->fors.body = parse_loop_body(statement);
5489 assert(scope == &statement->fors.scope);
5490 set_scope(last_scope);
5491 environment_pop_to(top);
5497 * Parse a goto statement.
5499 static statement_t *parse_goto(void)
5503 if(token.type != T_IDENTIFIER) {
5504 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5508 symbol_t *symbol = token.v.symbol;
5511 declaration_t *label = get_label(symbol);
5513 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5514 statement->base.source_position = token.source_position;
5516 statement->gotos.label = label;
5518 /* remember the goto's in a list for later checking */
5519 if (goto_last == NULL) {
5520 goto_first = &statement->gotos;
5522 goto_last->next = &statement->gotos;
5524 goto_last = &statement->gotos;
5532 * Parse a continue statement.
5534 static statement_t *parse_continue(void)
5536 statement_t *statement;
5537 if (current_loop == NULL) {
5538 errorf(HERE, "continue statement not within loop");
5541 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5543 statement->base.source_position = token.source_position;
5553 * Parse a break statement.
5555 static statement_t *parse_break(void)
5557 statement_t *statement;
5558 if (current_switch == NULL && current_loop == NULL) {
5559 errorf(HERE, "break statement not within loop or switch");
5562 statement = allocate_statement_zero(STATEMENT_BREAK);
5564 statement->base.source_position = token.source_position;
5574 * Check if a given declaration represents a local variable.
5576 static bool is_local_var_declaration(const declaration_t *declaration) {
5577 switch ((storage_class_tag_t) declaration->storage_class) {
5578 case STORAGE_CLASS_NONE:
5579 case STORAGE_CLASS_AUTO:
5580 case STORAGE_CLASS_REGISTER: {
5581 const type_t *type = skip_typeref(declaration->type);
5582 if(is_type_function(type)) {
5594 * Check if a given declaration represents a variable.
5596 static bool is_var_declaration(const declaration_t *declaration) {
5597 switch ((storage_class_tag_t) declaration->storage_class) {
5598 case STORAGE_CLASS_NONE:
5599 case STORAGE_CLASS_EXTERN:
5600 case STORAGE_CLASS_STATIC:
5601 case STORAGE_CLASS_AUTO:
5602 case STORAGE_CLASS_REGISTER:
5603 case STORAGE_CLASS_THREAD:
5604 case STORAGE_CLASS_THREAD_EXTERN:
5605 case STORAGE_CLASS_THREAD_STATIC: {
5606 const type_t *type = skip_typeref(declaration->type);
5607 if(is_type_function(type)) {
5619 * Check if a given expression represents a local variable.
5621 static bool is_local_variable(const expression_t *expression)
5623 if (expression->base.kind != EXPR_REFERENCE) {
5626 const declaration_t *declaration = expression->reference.declaration;
5627 return is_local_var_declaration(declaration);
5631 * Check if a given expression represents a local variable and
5632 * return its declaration then, else return NULL.
5634 declaration_t *expr_is_variable(const expression_t *expression)
5636 if (expression->base.kind != EXPR_REFERENCE) {
5639 declaration_t *declaration = expression->reference.declaration;
5640 if (is_var_declaration(declaration))
5646 * Parse a return statement.
5648 static statement_t *parse_return(void)
5652 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5653 statement->base.source_position = token.source_position;
5655 expression_t *return_value = NULL;
5656 if(token.type != ';') {
5657 return_value = parse_expression();
5661 const type_t *const func_type = current_function->type;
5662 assert(is_type_function(func_type));
5663 type_t *const return_type = skip_typeref(func_type->function.return_type);
5665 if(return_value != NULL) {
5666 type_t *return_value_type = skip_typeref(return_value->base.type);
5668 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5669 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5670 warningf(statement->base.source_position,
5671 "'return' with a value, in function returning void");
5672 return_value = NULL;
5674 type_t *const res_type = semantic_assign(return_type,
5675 return_value, "'return'");
5676 if (res_type == NULL) {
5677 errorf(statement->base.source_position,
5678 "cannot return something of type '%T' in function returning '%T'",
5679 return_value->base.type, return_type);
5681 return_value = create_implicit_cast(return_value, res_type);
5684 /* check for returning address of a local var */
5685 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5686 const expression_t *expression = return_value->unary.value;
5687 if (is_local_variable(expression)) {
5688 warningf(statement->base.source_position,
5689 "function returns address of local variable");
5693 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5694 warningf(statement->base.source_position,
5695 "'return' without value, in function returning non-void");
5698 statement->returns.value = return_value;
5704 * Parse a declaration statement.
5706 static statement_t *parse_declaration_statement(void)
5708 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5710 statement->base.source_position = token.source_position;
5712 declaration_t *before = last_declaration;
5713 parse_declaration(record_declaration);
5715 if(before == NULL) {
5716 statement->declaration.declarations_begin = scope->declarations;
5718 statement->declaration.declarations_begin = before->next;
5720 statement->declaration.declarations_end = last_declaration;
5726 * Parse an expression statement, ie. expr ';'.
5728 static statement_t *parse_expression_statement(void)
5730 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5732 statement->base.source_position = token.source_position;
5733 expression_t *const expr = parse_expression();
5734 statement->expression.expression = expr;
5736 if (warning.unused_value && !expression_has_effect(expr)) {
5737 warningf(expr->base.source_position, "statement has no effect");
5746 * Parse a statement.
5748 static statement_t *parse_statement(void)
5750 statement_t *statement = NULL;
5752 /* declaration or statement */
5753 switch(token.type) {
5755 statement = parse_asm_statement();
5759 statement = parse_case_statement();
5763 statement = parse_default_statement();
5767 statement = parse_compound_statement();
5771 statement = parse_if();
5775 statement = parse_switch();
5779 statement = parse_while();
5783 statement = parse_do();
5787 statement = parse_for();
5791 statement = parse_goto();
5795 statement = parse_continue();
5799 statement = parse_break();
5803 statement = parse_return();
5807 if (warning.empty_statement) {
5808 warningf(HERE, "statement is empty");
5815 if(look_ahead(1)->type == ':') {
5816 statement = parse_label_statement();
5820 if(is_typedef_symbol(token.v.symbol)) {
5821 statement = parse_declaration_statement();
5825 statement = parse_expression_statement();
5828 case T___extension__:
5829 /* this can be a prefix to a declaration or an expression statement */
5830 /* we simply eat it now and parse the rest with tail recursion */
5833 } while(token.type == T___extension__);
5834 statement = parse_statement();
5838 statement = parse_declaration_statement();
5842 statement = parse_expression_statement();
5846 assert(statement == NULL
5847 || statement->base.source_position.input_name != NULL);
5853 * Parse a compound statement.
5855 static statement_t *parse_compound_statement(void)
5857 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5859 statement->base.source_position = token.source_position;
5863 int top = environment_top();
5864 scope_t *last_scope = scope;
5865 set_scope(&statement->compound.scope);
5867 statement_t *last_statement = NULL;
5869 while(token.type != '}' && token.type != T_EOF) {
5870 statement_t *sub_statement = parse_statement();
5871 if(sub_statement == NULL)
5874 if(last_statement != NULL) {
5875 last_statement->base.next = sub_statement;
5877 statement->compound.statements = sub_statement;
5880 while(sub_statement->base.next != NULL)
5881 sub_statement = sub_statement->base.next;
5883 last_statement = sub_statement;
5886 if(token.type == '}') {
5889 errorf(statement->base.source_position,
5890 "end of file while looking for closing '}'");
5893 assert(scope == &statement->compound.scope);
5894 set_scope(last_scope);
5895 environment_pop_to(top);
5901 * Initialize builtin types.
5903 static void initialize_builtin_types(void)
5905 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5906 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5907 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5908 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5909 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5910 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5911 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5912 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5914 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5915 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5916 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5917 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5921 * Check for unused global static functions and variables
5923 static void check_unused_globals(void)
5925 if (!warning.unused_function && !warning.unused_variable)
5928 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5929 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5932 type_t *const type = decl->type;
5934 if (is_type_function(skip_typeref(type))) {
5935 if (!warning.unused_function || decl->is_inline)
5938 s = (decl->init.statement != NULL ? "defined" : "declared");
5940 if (!warning.unused_variable)
5946 warningf(decl->source_position, "'%#T' %s but not used",
5947 type, decl->symbol, s);
5952 * Parse a translation unit.
5954 static translation_unit_t *parse_translation_unit(void)
5956 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5958 assert(global_scope == NULL);
5959 global_scope = &unit->scope;
5961 assert(scope == NULL);
5962 set_scope(&unit->scope);
5964 initialize_builtin_types();
5966 while(token.type != T_EOF) {
5967 if (token.type == ';') {
5968 /* TODO error in strict mode */
5969 warningf(HERE, "stray ';' outside of function");
5972 parse_external_declaration();
5976 assert(scope == &unit->scope);
5978 last_declaration = NULL;
5980 assert(global_scope == &unit->scope);
5981 check_unused_globals();
5982 global_scope = NULL;
5990 * @return the translation unit or NULL if errors occurred.
5992 translation_unit_t *parse(void)
5994 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5995 label_stack = NEW_ARR_F(stack_entry_t, 0);
5996 diagnostic_count = 0;
6000 type_set_output(stderr);
6001 ast_set_output(stderr);
6003 lookahead_bufpos = 0;
6004 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6007 translation_unit_t *unit = parse_translation_unit();
6009 DEL_ARR_F(environment_stack);
6010 DEL_ARR_F(label_stack);
6019 * Initialize the parser.
6021 void init_parser(void)
6023 init_expression_parsers();
6024 obstack_init(&temp_obst);
6026 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6027 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6031 * Terminate the parser.
6033 void exit_parser(void)
6035 obstack_free(&temp_obst, NULL);