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(void);
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_STRING_LITERAL] = sizeof(string_literal_expression_t),
202 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
203 [EXPR_CALL] = sizeof(call_expression_t),
204 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
205 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
206 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
207 [EXPR_SELECT] = sizeof(select_expression_t),
208 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
209 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
210 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
211 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
212 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
213 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
214 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
215 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
216 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
217 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
218 [EXPR_VA_START] = sizeof(va_start_expression_t),
219 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
220 [EXPR_STATEMENT] = sizeof(statement_expression_t),
222 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
223 return sizes[EXPR_UNARY_FIRST];
225 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
226 return sizes[EXPR_BINARY_FIRST];
228 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
229 assert(sizes[kind] != 0);
234 * Allocate an expression node of given kind and initialize all
237 static expression_t *allocate_expression_zero(expression_kind_t kind)
239 size_t size = get_expression_struct_size(kind);
240 expression_t *res = allocate_ast_zero(size);
242 res->base.kind = kind;
243 res->base.type = type_error_type;
248 * Returns the size of a type node.
250 * @param kind the type kind
252 static size_t get_type_struct_size(type_kind_t kind)
254 static const size_t sizes[] = {
255 [TYPE_ATOMIC] = sizeof(atomic_type_t),
256 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
257 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
258 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
259 [TYPE_ENUM] = sizeof(enum_type_t),
260 [TYPE_FUNCTION] = sizeof(function_type_t),
261 [TYPE_POINTER] = sizeof(pointer_type_t),
262 [TYPE_ARRAY] = sizeof(array_type_t),
263 [TYPE_BUILTIN] = sizeof(builtin_type_t),
264 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
265 [TYPE_TYPEOF] = sizeof(typeof_type_t),
267 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
268 assert(kind <= TYPE_TYPEOF);
269 assert(sizes[kind] != 0);
274 * Allocate a type node of given kind and initialize all
277 static type_t *allocate_type_zero(type_kind_t kind)
279 size_t size = get_type_struct_size(kind);
280 type_t *res = obstack_alloc(type_obst, size);
281 memset(res, 0, size);
283 res->base.kind = kind;
288 * Returns the size of an initializer node.
290 * @param kind the initializer kind
292 static size_t get_initializer_size(initializer_kind_t kind)
294 static const size_t sizes[] = {
295 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
296 [INITIALIZER_STRING] = sizeof(initializer_string_t),
297 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
298 [INITIALIZER_LIST] = sizeof(initializer_list_t)
300 assert(kind < sizeof(sizes) / sizeof(*sizes));
301 assert(sizes[kind] != 0);
306 * Allocate an initializer node of given kind and initialize all
309 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
311 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
318 * Free a type from the type obstack.
320 static void free_type(void *type)
322 obstack_free(type_obst, type);
326 * Returns the index of the top element of the environment stack.
328 static size_t environment_top(void)
330 return ARR_LEN(environment_stack);
334 * Returns the index of the top element of the label stack.
336 static size_t label_top(void)
338 return ARR_LEN(label_stack);
343 * Return the next token.
345 static inline void next_token(void)
347 token = lookahead_buffer[lookahead_bufpos];
348 lookahead_buffer[lookahead_bufpos] = lexer_token;
351 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
354 print_token(stderr, &token);
355 fprintf(stderr, "\n");
360 * Return the next token with a given lookahead.
362 static inline const token_t *look_ahead(int num)
364 assert(num > 0 && num <= MAX_LOOKAHEAD);
365 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
366 return &lookahead_buffer[pos];
369 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
372 * Report a parse error because an expected token was not found.
374 static void parse_error_expected(const char *message, ...)
376 if(message != NULL) {
377 errorf(HERE, "%s", message);
380 va_start(ap, message);
381 errorf(HERE, "got '%K', expected %#k", &token, &ap, ", ");
386 * Report a type error.
388 static void type_error(const char *msg, const source_position_t source_position,
391 errorf(source_position, "%s, but found type '%T'", msg, type);
395 * Report an incompatible type.
397 static void type_error_incompatible(const char *msg,
398 const source_position_t source_position, type_t *type1, type_t *type2)
400 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
404 * Eat an complete block, ie. '{ ... }'.
406 static void eat_block(void)
408 if(token.type == '{')
411 while(token.type != '}') {
412 if(token.type == T_EOF)
414 if(token.type == '{') {
424 * Eat a statement until an ';' token.
426 static void eat_statement(void)
428 while(token.type != ';') {
429 if(token.type == T_EOF)
431 if(token.type == '}')
433 if(token.type == '{') {
443 * Eat a parenthesed term, ie. '( ... )'.
445 static void eat_paren(void)
447 if(token.type == '(')
450 while(token.type != ')') {
451 if(token.type == T_EOF)
453 if(token.type == ')' || token.type == ';' || token.type == '}') {
456 if(token.type == '(') {
460 if(token.type == '{') {
469 #define expect(expected) \
470 if(UNLIKELY(token.type != (expected))) { \
471 parse_error_expected(NULL, (expected), 0); \
477 #define expect_block(expected) \
478 if(UNLIKELY(token.type != (expected))) { \
479 parse_error_expected(NULL, (expected), 0); \
485 #define expect_void(expected) \
486 if(UNLIKELY(token.type != (expected))) { \
487 parse_error_expected(NULL, (expected), 0); \
493 static void set_scope(scope_t *new_scope)
497 last_declaration = new_scope->declarations;
498 if(last_declaration != NULL) {
499 while(last_declaration->next != NULL) {
500 last_declaration = last_declaration->next;
506 * Search a symbol in a given namespace and returns its declaration or
507 * NULL if this symbol was not found.
509 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
511 declaration_t *declaration = symbol->declaration;
512 for( ; declaration != NULL; declaration = declaration->symbol_next) {
513 if(declaration->namespc == namespc)
521 * pushs an environment_entry on the environment stack and links the
522 * corresponding symbol to the new entry
524 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
526 symbol_t *symbol = declaration->symbol;
527 namespace_t namespc = (namespace_t)declaration->namespc;
529 /* remember old declaration */
531 entry.symbol = symbol;
532 entry.old_declaration = symbol->declaration;
533 entry.namespc = (unsigned short) namespc;
534 ARR_APP1(stack_entry_t, *stack_ptr, entry);
536 /* replace/add declaration into declaration list of the symbol */
537 if(symbol->declaration == NULL) {
538 symbol->declaration = declaration;
540 declaration_t *iter_last = NULL;
541 declaration_t *iter = symbol->declaration;
542 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
543 /* replace an entry? */
544 if(iter->namespc == namespc) {
545 if(iter_last == NULL) {
546 symbol->declaration = declaration;
548 iter_last->symbol_next = declaration;
550 declaration->symbol_next = iter->symbol_next;
555 assert(iter_last->symbol_next == NULL);
556 iter_last->symbol_next = declaration;
561 static void environment_push(declaration_t *declaration)
563 assert(declaration->source_position.input_name != NULL);
564 assert(declaration->parent_scope != NULL);
565 stack_push(&environment_stack, declaration);
568 static void label_push(declaration_t *declaration)
570 declaration->parent_scope = ¤t_function->scope;
571 stack_push(&label_stack, declaration);
575 * pops symbols from the environment stack until @p new_top is the top element
577 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
579 stack_entry_t *stack = *stack_ptr;
580 size_t top = ARR_LEN(stack);
583 assert(new_top <= top);
587 for(i = top; i > new_top; --i) {
588 stack_entry_t *entry = &stack[i - 1];
590 declaration_t *old_declaration = entry->old_declaration;
591 symbol_t *symbol = entry->symbol;
592 namespace_t namespc = (namespace_t)entry->namespc;
594 /* replace/remove declaration */
595 declaration_t *declaration = symbol->declaration;
596 assert(declaration != NULL);
597 if(declaration->namespc == namespc) {
598 if(old_declaration == NULL) {
599 symbol->declaration = declaration->symbol_next;
601 symbol->declaration = old_declaration;
604 declaration_t *iter_last = declaration;
605 declaration_t *iter = declaration->symbol_next;
606 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
607 /* replace an entry? */
608 if(iter->namespc == namespc) {
609 assert(iter_last != NULL);
610 iter_last->symbol_next = old_declaration;
611 old_declaration->symbol_next = iter->symbol_next;
615 assert(iter != NULL);
619 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
622 static void environment_pop_to(size_t new_top)
624 stack_pop_to(&environment_stack, new_top);
627 static void label_pop_to(size_t new_top)
629 stack_pop_to(&label_stack, new_top);
633 static int get_rank(const type_t *type)
635 assert(!is_typeref(type));
636 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
637 * and esp. footnote 108). However we can't fold constants (yet), so we
638 * can't decide whether unsigned int is possible, while int always works.
639 * (unsigned int would be preferable when possible... for stuff like
640 * struct { enum { ... } bla : 4; } ) */
641 if(type->kind == TYPE_ENUM)
642 return ATOMIC_TYPE_INT;
644 assert(type->kind == TYPE_ATOMIC);
645 return type->atomic.akind;
648 static type_t *promote_integer(type_t *type)
650 if(type->kind == TYPE_BITFIELD)
651 type = type->bitfield.base;
653 if(get_rank(type) < ATOMIC_TYPE_INT)
660 * Create a cast expression.
662 * @param expression the expression to cast
663 * @param dest_type the destination type
665 static expression_t *create_cast_expression(expression_t *expression,
668 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
670 cast->unary.value = expression;
671 cast->base.type = dest_type;
677 * Check if a given expression represents the 0 pointer constant.
679 static bool is_null_pointer_constant(const expression_t *expression)
681 /* skip void* cast */
682 if(expression->kind == EXPR_UNARY_CAST
683 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
684 expression = expression->unary.value;
687 /* TODO: not correct yet, should be any constant integer expression
688 * which evaluates to 0 */
689 if (expression->kind != EXPR_CONST)
692 type_t *const type = skip_typeref(expression->base.type);
693 if (!is_type_integer(type))
696 return expression->conste.v.int_value == 0;
700 * Create an implicit cast expression.
702 * @param expression the expression to cast
703 * @param dest_type the destination type
705 static expression_t *create_implicit_cast(expression_t *expression,
708 type_t *const source_type = expression->base.type;
710 if (source_type == dest_type)
713 return create_cast_expression(expression, dest_type);
716 /** Implements the rules from § 6.5.16.1 */
717 static type_t *semantic_assign(type_t *orig_type_left,
718 const expression_t *const right,
721 type_t *const orig_type_right = right->base.type;
722 type_t *const type_left = skip_typeref(orig_type_left);
723 type_t *const type_right = skip_typeref(orig_type_right);
725 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
726 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
727 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
728 && is_type_pointer(type_right))) {
729 return orig_type_left;
732 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
733 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
734 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
736 /* the left type has all qualifiers from the right type */
737 unsigned missing_qualifiers
738 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
739 if(missing_qualifiers != 0) {
740 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
741 return orig_type_left;
744 points_to_left = get_unqualified_type(points_to_left);
745 points_to_right = get_unqualified_type(points_to_right);
747 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
748 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
749 return orig_type_left;
752 if (!types_compatible(points_to_left, points_to_right)) {
753 warningf(right->base.source_position,
754 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
755 orig_type_left, context, right, orig_type_right);
758 return orig_type_left;
761 if (is_type_compound(type_left) && is_type_compound(type_right)) {
762 type_t *const unqual_type_left = get_unqualified_type(type_left);
763 type_t *const unqual_type_right = get_unqualified_type(type_right);
764 if (types_compatible(unqual_type_left, unqual_type_right)) {
765 return orig_type_left;
769 if (!is_type_valid(type_left))
772 if (!is_type_valid(type_right))
773 return orig_type_right;
778 static expression_t *parse_constant_expression(void)
780 /* start parsing at precedence 7 (conditional expression) */
781 expression_t *result = parse_sub_expression(7);
783 if(!is_constant_expression(result)) {
784 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
790 static expression_t *parse_assignment_expression(void)
792 /* start parsing at precedence 2 (assignment expression) */
793 return parse_sub_expression(2);
796 static type_t *make_global_typedef(const char *name, type_t *type)
798 symbol_t *const symbol = symbol_table_insert(name);
800 declaration_t *const declaration = allocate_declaration_zero();
801 declaration->namespc = NAMESPACE_NORMAL;
802 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
803 declaration->type = type;
804 declaration->symbol = symbol;
805 declaration->source_position = builtin_source_position;
807 record_declaration(declaration);
809 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
810 typedef_type->typedeft.declaration = declaration;
815 static string_t parse_string_literals(void)
817 assert(token.type == T_STRING_LITERAL);
818 string_t result = token.v.string;
822 while (token.type == T_STRING_LITERAL) {
823 result = concat_strings(&result, &token.v.string);
830 static void parse_attributes(void)
834 case T___attribute__: {
842 errorf(HERE, "EOF while parsing attribute");
861 if(token.type != T_STRING_LITERAL) {
862 parse_error_expected("while parsing assembler attribute",
867 parse_string_literals();
872 goto attributes_finished;
881 static designator_t *parse_designation(void)
883 if(token.type != '[' && token.type != '.')
886 designator_t *result = NULL;
887 designator_t *last = NULL;
890 designator_t *designator;
893 designator = allocate_ast_zero(sizeof(designator[0]));
895 designator->array_access = parse_constant_expression();
899 designator = allocate_ast_zero(sizeof(designator[0]));
901 if(token.type != T_IDENTIFIER) {
902 parse_error_expected("while parsing designator",
906 designator->symbol = token.v.symbol;
914 assert(designator != NULL);
916 last->next = designator;
925 static initializer_t *initializer_from_string(array_type_t *type,
926 const string_t *const string)
928 /* TODO: check len vs. size of array type */
931 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
932 initializer->string.string = *string;
937 static initializer_t *initializer_from_wide_string(array_type_t *const type,
938 wide_string_t *const string)
940 /* TODO: check len vs. size of array type */
943 initializer_t *const initializer =
944 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
945 initializer->wide_string.string = *string;
950 static initializer_t *initializer_from_expression(type_t *type,
951 expression_t *expression)
953 /* TODO check that expression is a constant expression */
955 /* § 6.7.8.14/15 char array may be initialized by string literals */
956 type_t *const expr_type = expression->base.type;
957 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
958 array_type_t *const array_type = &type->array;
959 type_t *const element_type = skip_typeref(array_type->element_type);
961 if (element_type->kind == TYPE_ATOMIC) {
962 switch (expression->kind) {
963 case EXPR_STRING_LITERAL:
964 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
965 return initializer_from_string(array_type,
966 &expression->string.value);
969 case EXPR_WIDE_STRING_LITERAL: {
970 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
971 if (get_unqualified_type(element_type) == bare_wchar_type) {
972 return initializer_from_wide_string(array_type,
973 &expression->wide_string.value);
983 type_t *const res_type = semantic_assign(type, expression, "initializer");
984 if (res_type == NULL)
987 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
988 result->value.value = create_implicit_cast(expression, res_type);
993 static initializer_t *parse_sub_initializer(type_t *type,
994 expression_t *expression);
996 static initializer_t *parse_sub_initializer_elem(type_t *type)
998 if(token.type == '{') {
999 return parse_sub_initializer(type, NULL);
1002 expression_t *expression = parse_assignment_expression();
1003 return parse_sub_initializer(type, expression);
1006 static bool had_initializer_brace_warning;
1008 static void skip_designator(void)
1011 if(token.type == '.') {
1013 if(token.type == T_IDENTIFIER)
1015 } else if(token.type == '[') {
1017 parse_constant_expression();
1018 if(token.type == ']')
1026 static initializer_t *parse_sub_initializer(type_t *type,
1027 expression_t *expression)
1029 if(is_type_scalar(type)) {
1030 /* there might be extra {} hierarchies */
1031 if(token.type == '{') {
1033 if(!had_initializer_brace_warning) {
1034 warningf(HERE, "braces around scalar initializer");
1035 had_initializer_brace_warning = true;
1037 initializer_t *result = parse_sub_initializer(type, NULL);
1038 if(token.type == ',') {
1040 /* TODO: warn about excessive elements */
1046 if(expression == NULL) {
1047 expression = parse_assignment_expression();
1049 return initializer_from_expression(type, expression);
1052 /* does the expression match the currently looked at object to initialize */
1053 if(expression != NULL) {
1054 initializer_t *result = initializer_from_expression(type, expression);
1059 bool read_paren = false;
1060 if(token.type == '{') {
1065 /* descend into subtype */
1066 initializer_t *result = NULL;
1067 initializer_t **elems;
1068 if(is_type_array(type)) {
1069 if(token.type == '.') {
1071 "compound designator in initializer for array type '%T'",
1076 type_t *const element_type = skip_typeref(type->array.element_type);
1079 had_initializer_brace_warning = false;
1080 if(expression == NULL) {
1081 sub = parse_sub_initializer_elem(element_type);
1083 sub = parse_sub_initializer(element_type, expression);
1086 /* didn't match the subtypes -> try the parent type */
1088 assert(!read_paren);
1092 elems = NEW_ARR_F(initializer_t*, 0);
1093 ARR_APP1(initializer_t*, elems, sub);
1096 if(token.type == '}')
1099 if(token.type == '}')
1102 sub = parse_sub_initializer_elem(element_type);
1104 /* TODO error, do nicer cleanup */
1105 errorf(HERE, "member initializer didn't match");
1109 ARR_APP1(initializer_t*, elems, sub);
1112 assert(is_type_compound(type));
1113 scope_t *const scope = &type->compound.declaration->scope;
1115 if(token.type == '[') {
1117 "array designator in initializer for compound type '%T'",
1122 declaration_t *first = scope->declarations;
1125 type_t *first_type = first->type;
1126 first_type = skip_typeref(first_type);
1129 had_initializer_brace_warning = false;
1130 if(expression == NULL) {
1131 sub = parse_sub_initializer_elem(first_type);
1133 sub = parse_sub_initializer(first_type, expression);
1136 /* didn't match the subtypes -> try our parent type */
1138 assert(!read_paren);
1142 elems = NEW_ARR_F(initializer_t*, 0);
1143 ARR_APP1(initializer_t*, elems, sub);
1145 declaration_t *iter = first->next;
1146 for( ; iter != NULL; iter = iter->next) {
1147 if(iter->symbol == NULL)
1149 if(iter->namespc != NAMESPACE_NORMAL)
1152 if(token.type == '}')
1155 if(token.type == '}')
1158 type_t *iter_type = iter->type;
1159 iter_type = skip_typeref(iter_type);
1161 sub = parse_sub_initializer_elem(iter_type);
1163 /* TODO error, do nicer cleanup */
1164 errorf(HERE, "member initializer didn't match");
1168 ARR_APP1(initializer_t*, elems, sub);
1172 int len = ARR_LEN(elems);
1173 size_t elems_size = sizeof(initializer_t*) * len;
1175 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1177 init->initializer.kind = INITIALIZER_LIST;
1179 memcpy(init->initializers, elems, elems_size);
1182 result = (initializer_t*) init;
1185 if(token.type == ',')
1192 static initializer_t *parse_initializer(type_t *const orig_type)
1194 initializer_t *result;
1196 type_t *const type = skip_typeref(orig_type);
1198 if(token.type != '{') {
1199 expression_t *expression = parse_assignment_expression();
1200 initializer_t *initializer = initializer_from_expression(type, expression);
1201 if(initializer == NULL) {
1203 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1204 expression, expression->base.type, orig_type);
1209 if(is_type_scalar(type)) {
1213 expression_t *expression = parse_assignment_expression();
1214 result = initializer_from_expression(type, expression);
1216 if(token.type == ',')
1222 result = parse_sub_initializer(type, NULL);
1228 static declaration_t *append_declaration(declaration_t *declaration);
1230 static declaration_t *parse_compound_type_specifier(bool is_struct)
1238 symbol_t *symbol = NULL;
1239 declaration_t *declaration = NULL;
1241 if (token.type == T___attribute__) {
1246 if(token.type == T_IDENTIFIER) {
1247 symbol = token.v.symbol;
1251 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1253 declaration = get_declaration(symbol, NAMESPACE_UNION);
1255 } else if(token.type != '{') {
1257 parse_error_expected("while parsing struct type specifier",
1258 T_IDENTIFIER, '{', 0);
1260 parse_error_expected("while parsing union type specifier",
1261 T_IDENTIFIER, '{', 0);
1267 if(declaration == NULL) {
1268 declaration = allocate_declaration_zero();
1269 declaration->namespc =
1270 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1271 declaration->source_position = token.source_position;
1272 declaration->symbol = symbol;
1273 declaration->parent_scope = scope;
1274 if (symbol != NULL) {
1275 environment_push(declaration);
1277 append_declaration(declaration);
1280 if(token.type == '{') {
1281 if(declaration->init.is_defined) {
1282 assert(symbol != NULL);
1283 errorf(HERE, "multiple definition of '%s %Y'",
1284 is_struct ? "struct" : "union", symbol);
1285 declaration->scope.declarations = NULL;
1287 declaration->init.is_defined = true;
1289 int top = environment_top();
1290 scope_t *last_scope = scope;
1291 set_scope(&declaration->scope);
1293 parse_compound_type_entries();
1296 assert(scope == &declaration->scope);
1297 set_scope(last_scope);
1298 environment_pop_to(top);
1304 static void parse_enum_entries(type_t *const enum_type)
1308 if(token.type == '}') {
1310 errorf(HERE, "empty enum not allowed");
1315 if(token.type != T_IDENTIFIER) {
1316 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1321 declaration_t *const entry = allocate_declaration_zero();
1322 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1323 entry->type = enum_type;
1324 entry->symbol = token.v.symbol;
1325 entry->source_position = token.source_position;
1328 if(token.type == '=') {
1330 entry->init.enum_value = parse_constant_expression();
1335 record_declaration(entry);
1337 if(token.type != ',')
1340 } while(token.type != '}');
1345 static type_t *parse_enum_specifier(void)
1349 declaration_t *declaration;
1352 if(token.type == T_IDENTIFIER) {
1353 symbol = token.v.symbol;
1356 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1357 } else if(token.type != '{') {
1358 parse_error_expected("while parsing enum type specifier",
1359 T_IDENTIFIER, '{', 0);
1366 if(declaration == NULL) {
1367 declaration = allocate_declaration_zero();
1368 declaration->namespc = NAMESPACE_ENUM;
1369 declaration->source_position = token.source_position;
1370 declaration->symbol = symbol;
1371 declaration->parent_scope = scope;
1374 type_t *const type = allocate_type_zero(TYPE_ENUM);
1375 type->enumt.declaration = declaration;
1377 if(token.type == '{') {
1378 if(declaration->init.is_defined) {
1379 errorf(HERE, "multiple definitions of enum %Y", symbol);
1381 if (symbol != NULL) {
1382 environment_push(declaration);
1384 append_declaration(declaration);
1385 declaration->init.is_defined = 1;
1387 parse_enum_entries(type);
1395 * if a symbol is a typedef to another type, return true
1397 static bool is_typedef_symbol(symbol_t *symbol)
1399 const declaration_t *const declaration =
1400 get_declaration(symbol, NAMESPACE_NORMAL);
1402 declaration != NULL &&
1403 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1406 static type_t *parse_typeof(void)
1414 expression_t *expression = NULL;
1417 switch(token.type) {
1418 case T___extension__:
1419 /* this can be a prefix to a typename or an expression */
1420 /* we simply eat it now. */
1423 } while(token.type == T___extension__);
1427 if(is_typedef_symbol(token.v.symbol)) {
1428 type = parse_typename();
1430 expression = parse_expression();
1431 type = expression->base.type;
1436 type = parse_typename();
1440 expression = parse_expression();
1441 type = expression->base.type;
1447 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1448 typeof_type->typeoft.expression = expression;
1449 typeof_type->typeoft.typeof_type = type;
1455 SPECIFIER_SIGNED = 1 << 0,
1456 SPECIFIER_UNSIGNED = 1 << 1,
1457 SPECIFIER_LONG = 1 << 2,
1458 SPECIFIER_INT = 1 << 3,
1459 SPECIFIER_DOUBLE = 1 << 4,
1460 SPECIFIER_CHAR = 1 << 5,
1461 SPECIFIER_SHORT = 1 << 6,
1462 SPECIFIER_LONG_LONG = 1 << 7,
1463 SPECIFIER_FLOAT = 1 << 8,
1464 SPECIFIER_BOOL = 1 << 9,
1465 SPECIFIER_VOID = 1 << 10,
1466 #ifdef PROVIDE_COMPLEX
1467 SPECIFIER_COMPLEX = 1 << 11,
1468 SPECIFIER_IMAGINARY = 1 << 12,
1472 static type_t *create_builtin_type(symbol_t *const symbol,
1473 type_t *const real_type)
1475 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1476 type->builtin.symbol = symbol;
1477 type->builtin.real_type = real_type;
1479 type_t *result = typehash_insert(type);
1480 if (type != result) {
1487 static type_t *get_typedef_type(symbol_t *symbol)
1489 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1490 if(declaration == NULL
1491 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1494 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1495 type->typedeft.declaration = declaration;
1500 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1502 type_t *type = NULL;
1503 unsigned type_qualifiers = 0;
1504 unsigned type_specifiers = 0;
1507 specifiers->source_position = token.source_position;
1510 switch(token.type) {
1513 #define MATCH_STORAGE_CLASS(token, class) \
1515 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1516 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1518 specifiers->storage_class = class; \
1522 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1523 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1524 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1525 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1526 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1529 switch (specifiers->storage_class) {
1530 case STORAGE_CLASS_NONE:
1531 specifiers->storage_class = STORAGE_CLASS_THREAD;
1534 case STORAGE_CLASS_EXTERN:
1535 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1538 case STORAGE_CLASS_STATIC:
1539 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1543 errorf(HERE, "multiple storage classes in declaration specifiers");
1549 /* type qualifiers */
1550 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1552 type_qualifiers |= qualifier; \
1556 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1557 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1558 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1560 case T___extension__:
1565 /* type specifiers */
1566 #define MATCH_SPECIFIER(token, specifier, name) \
1569 if(type_specifiers & specifier) { \
1570 errorf(HERE, "multiple " name " type specifiers given"); \
1572 type_specifiers |= specifier; \
1576 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1577 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1578 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1579 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1580 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1581 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1582 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1583 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1584 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1585 #ifdef PROVIDE_COMPLEX
1586 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1587 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1590 /* only in microsoft mode */
1591 specifiers->decl_modifiers |= DM_FORCEINLINE;
1595 specifiers->is_inline = true;
1600 if(type_specifiers & SPECIFIER_LONG_LONG) {
1601 errorf(HERE, "multiple type specifiers given");
1602 } else if(type_specifiers & SPECIFIER_LONG) {
1603 type_specifiers |= SPECIFIER_LONG_LONG;
1605 type_specifiers |= SPECIFIER_LONG;
1609 /* TODO: if is_type_valid(type) for the following rules should issue
1612 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1614 type->compound.declaration = parse_compound_type_specifier(true);
1618 type = allocate_type_zero(TYPE_COMPOUND_UNION);
1620 type->compound.declaration = parse_compound_type_specifier(false);
1624 type = parse_enum_specifier();
1627 type = parse_typeof();
1629 case T___builtin_va_list:
1630 type = duplicate_type(type_valist);
1634 case T___attribute__:
1639 case T_IDENTIFIER: {
1640 type_t *typedef_type = get_typedef_type(token.v.symbol);
1642 if(typedef_type == NULL)
1643 goto finish_specifiers;
1646 type = typedef_type;
1650 /* function specifier */
1652 goto finish_specifiers;
1659 atomic_type_kind_t atomic_type;
1661 /* match valid basic types */
1662 switch(type_specifiers) {
1663 case SPECIFIER_VOID:
1664 atomic_type = ATOMIC_TYPE_VOID;
1666 case SPECIFIER_CHAR:
1667 atomic_type = ATOMIC_TYPE_CHAR;
1669 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1670 atomic_type = ATOMIC_TYPE_SCHAR;
1672 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1673 atomic_type = ATOMIC_TYPE_UCHAR;
1675 case SPECIFIER_SHORT:
1676 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1677 case SPECIFIER_SHORT | SPECIFIER_INT:
1678 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1679 atomic_type = ATOMIC_TYPE_SHORT;
1681 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1682 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1683 atomic_type = ATOMIC_TYPE_USHORT;
1686 case SPECIFIER_SIGNED:
1687 case SPECIFIER_SIGNED | SPECIFIER_INT:
1688 atomic_type = ATOMIC_TYPE_INT;
1690 case SPECIFIER_UNSIGNED:
1691 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1692 atomic_type = ATOMIC_TYPE_UINT;
1694 case SPECIFIER_LONG:
1695 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1696 case SPECIFIER_LONG | SPECIFIER_INT:
1697 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1698 atomic_type = ATOMIC_TYPE_LONG;
1700 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1701 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1702 atomic_type = ATOMIC_TYPE_ULONG;
1704 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1705 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1706 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1707 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1709 atomic_type = ATOMIC_TYPE_LONGLONG;
1711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1714 atomic_type = ATOMIC_TYPE_ULONGLONG;
1716 case SPECIFIER_FLOAT:
1717 atomic_type = ATOMIC_TYPE_FLOAT;
1719 case SPECIFIER_DOUBLE:
1720 atomic_type = ATOMIC_TYPE_DOUBLE;
1722 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1723 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1725 case SPECIFIER_BOOL:
1726 atomic_type = ATOMIC_TYPE_BOOL;
1728 #ifdef PROVIDE_COMPLEX
1729 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1730 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1732 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1733 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1735 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1736 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1738 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1739 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1741 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1742 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1744 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1745 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1749 /* invalid specifier combination, give an error message */
1750 if(type_specifiers == 0) {
1751 if (! strict_mode) {
1752 if (warning.implicit_int) {
1753 warningf(HERE, "no type specifiers in declaration, using 'int'");
1755 atomic_type = ATOMIC_TYPE_INT;
1758 errorf(HERE, "no type specifiers given in declaration");
1760 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1761 (type_specifiers & SPECIFIER_UNSIGNED)) {
1762 errorf(HERE, "signed and unsigned specifiers gives");
1763 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1764 errorf(HERE, "only integer types can be signed or unsigned");
1766 errorf(HERE, "multiple datatypes in declaration");
1768 atomic_type = ATOMIC_TYPE_INVALID;
1771 type = allocate_type_zero(TYPE_ATOMIC);
1772 type->atomic.akind = atomic_type;
1775 if(type_specifiers != 0) {
1776 errorf(HERE, "multiple datatypes in declaration");
1780 type->base.qualifiers = type_qualifiers;
1782 type_t *result = typehash_insert(type);
1783 if(newtype && result != type) {
1787 specifiers->type = result;
1790 static type_qualifiers_t parse_type_qualifiers(void)
1792 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1795 switch(token.type) {
1796 /* type qualifiers */
1797 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1798 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1799 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1802 return type_qualifiers;
1807 static declaration_t *parse_identifier_list(void)
1809 declaration_t *declarations = NULL;
1810 declaration_t *last_declaration = NULL;
1812 declaration_t *const declaration = allocate_declaration_zero();
1813 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1814 declaration->source_position = token.source_position;
1815 declaration->symbol = token.v.symbol;
1818 if(last_declaration != NULL) {
1819 last_declaration->next = declaration;
1821 declarations = declaration;
1823 last_declaration = declaration;
1825 if(token.type != ',')
1828 } while(token.type == T_IDENTIFIER);
1830 return declarations;
1833 static void semantic_parameter(declaration_t *declaration)
1835 /* TODO: improve error messages */
1837 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1838 errorf(HERE, "typedef not allowed in parameter list");
1839 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1840 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1841 errorf(HERE, "parameter may only have none or register storage class");
1844 type_t *const orig_type = declaration->type;
1845 type_t * type = skip_typeref(orig_type);
1847 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1848 * into a pointer. § 6.7.5.3 (7) */
1849 if (is_type_array(type)) {
1850 type_t *const element_type = type->array.element_type;
1852 type = make_pointer_type(element_type, type->base.qualifiers);
1854 declaration->type = type;
1857 if(is_type_incomplete(type)) {
1858 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1859 orig_type, declaration->symbol);
1863 static declaration_t *parse_parameter(void)
1865 declaration_specifiers_t specifiers;
1866 memset(&specifiers, 0, sizeof(specifiers));
1868 parse_declaration_specifiers(&specifiers);
1870 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1872 semantic_parameter(declaration);
1877 static declaration_t *parse_parameters(function_type_t *type)
1879 if(token.type == T_IDENTIFIER) {
1880 symbol_t *symbol = token.v.symbol;
1881 if(!is_typedef_symbol(symbol)) {
1882 type->kr_style_parameters = true;
1883 return parse_identifier_list();
1887 if(token.type == ')') {
1888 type->unspecified_parameters = 1;
1891 if(token.type == T_void && look_ahead(1)->type == ')') {
1896 declaration_t *declarations = NULL;
1897 declaration_t *declaration;
1898 declaration_t *last_declaration = NULL;
1899 function_parameter_t *parameter;
1900 function_parameter_t *last_parameter = NULL;
1903 switch(token.type) {
1907 return declarations;
1910 case T___extension__:
1912 declaration = parse_parameter();
1914 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1915 memset(parameter, 0, sizeof(parameter[0]));
1916 parameter->type = declaration->type;
1918 if(last_parameter != NULL) {
1919 last_declaration->next = declaration;
1920 last_parameter->next = parameter;
1922 type->parameters = parameter;
1923 declarations = declaration;
1925 last_parameter = parameter;
1926 last_declaration = declaration;
1930 return declarations;
1932 if(token.type != ',')
1933 return declarations;
1943 } construct_type_type_t;
1945 typedef struct construct_type_t construct_type_t;
1946 struct construct_type_t {
1947 construct_type_type_t type;
1948 construct_type_t *next;
1951 typedef struct parsed_pointer_t parsed_pointer_t;
1952 struct parsed_pointer_t {
1953 construct_type_t construct_type;
1954 type_qualifiers_t type_qualifiers;
1957 typedef struct construct_function_type_t construct_function_type_t;
1958 struct construct_function_type_t {
1959 construct_type_t construct_type;
1960 type_t *function_type;
1963 typedef struct parsed_array_t parsed_array_t;
1964 struct parsed_array_t {
1965 construct_type_t construct_type;
1966 type_qualifiers_t type_qualifiers;
1972 typedef struct construct_base_type_t construct_base_type_t;
1973 struct construct_base_type_t {
1974 construct_type_t construct_type;
1978 static construct_type_t *parse_pointer_declarator(void)
1982 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1983 memset(pointer, 0, sizeof(pointer[0]));
1984 pointer->construct_type.type = CONSTRUCT_POINTER;
1985 pointer->type_qualifiers = parse_type_qualifiers();
1987 return (construct_type_t*) pointer;
1990 static construct_type_t *parse_array_declarator(void)
1994 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1995 memset(array, 0, sizeof(array[0]));
1996 array->construct_type.type = CONSTRUCT_ARRAY;
1998 if(token.type == T_static) {
1999 array->is_static = true;
2003 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2004 if(type_qualifiers != 0) {
2005 if(token.type == T_static) {
2006 array->is_static = true;
2010 array->type_qualifiers = type_qualifiers;
2012 if(token.type == '*' && look_ahead(1)->type == ']') {
2013 array->is_variable = true;
2015 } else if(token.type != ']') {
2016 array->size = parse_assignment_expression();
2021 return (construct_type_t*) array;
2024 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2028 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2030 declaration_t *parameters = parse_parameters(&type->function);
2031 if(declaration != NULL) {
2032 declaration->scope.declarations = parameters;
2035 construct_function_type_t *construct_function_type =
2036 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2037 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2038 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2039 construct_function_type->function_type = type;
2043 return (construct_type_t*) construct_function_type;
2046 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2047 bool may_be_abstract)
2049 /* construct a single linked list of construct_type_t's which describe
2050 * how to construct the final declarator type */
2051 construct_type_t *first = NULL;
2052 construct_type_t *last = NULL;
2055 while(token.type == '*') {
2056 construct_type_t *type = parse_pointer_declarator();
2067 /* TODO: find out if this is correct */
2070 construct_type_t *inner_types = NULL;
2072 switch(token.type) {
2074 if(declaration == NULL) {
2075 errorf(HERE, "no identifier expected in typename");
2077 declaration->symbol = token.v.symbol;
2078 declaration->source_position = token.source_position;
2084 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2090 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2091 /* avoid a loop in the outermost scope, because eat_statement doesn't
2093 if(token.type == '}' && current_function == NULL) {
2101 construct_type_t *p = last;
2104 construct_type_t *type;
2105 switch(token.type) {
2107 type = parse_function_declarator(declaration);
2110 type = parse_array_declarator();
2113 goto declarator_finished;
2116 /* insert in the middle of the list (behind p) */
2118 type->next = p->next;
2129 declarator_finished:
2132 /* append inner_types at the end of the list, we don't to set last anymore
2133 * as it's not needed anymore */
2135 assert(first == NULL);
2136 first = inner_types;
2138 last->next = inner_types;
2144 static type_t *construct_declarator_type(construct_type_t *construct_list,
2147 construct_type_t *iter = construct_list;
2148 for( ; iter != NULL; iter = iter->next) {
2149 switch(iter->type) {
2150 case CONSTRUCT_INVALID:
2151 panic("invalid type construction found");
2152 case CONSTRUCT_FUNCTION: {
2153 construct_function_type_t *construct_function_type
2154 = (construct_function_type_t*) iter;
2156 type_t *function_type = construct_function_type->function_type;
2158 function_type->function.return_type = type;
2160 type_t *skipped_return_type = skip_typeref(type);
2161 if (is_type_function(skipped_return_type)) {
2162 errorf(HERE, "function returning function is not allowed");
2163 type = type_error_type;
2164 } else if (is_type_array(skipped_return_type)) {
2165 errorf(HERE, "function returning array is not allowed");
2166 type = type_error_type;
2168 type = function_type;
2173 case CONSTRUCT_POINTER: {
2174 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2175 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2176 pointer_type->pointer.points_to = type;
2177 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2179 type = pointer_type;
2183 case CONSTRUCT_ARRAY: {
2184 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2185 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2187 array_type->base.qualifiers = parsed_array->type_qualifiers;
2188 array_type->array.element_type = type;
2189 array_type->array.is_static = parsed_array->is_static;
2190 array_type->array.is_variable = parsed_array->is_variable;
2191 array_type->array.size = parsed_array->size;
2193 type_t *skipped_type = skip_typeref(type);
2194 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2195 errorf(HERE, "array of void is not allowed");
2196 type = type_error_type;
2204 type_t *hashed_type = typehash_insert(type);
2205 if(hashed_type != type) {
2206 /* the function type was constructed earlier freeing it here will
2207 * destroy other types... */
2208 if(iter->type != CONSTRUCT_FUNCTION) {
2218 static declaration_t *parse_declarator(
2219 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2221 declaration_t *const declaration = allocate_declaration_zero();
2222 declaration->storage_class = specifiers->storage_class;
2223 declaration->modifiers = specifiers->decl_modifiers;
2224 declaration->is_inline = specifiers->is_inline;
2226 construct_type_t *construct_type
2227 = parse_inner_declarator(declaration, may_be_abstract);
2228 type_t *const type = specifiers->type;
2229 declaration->type = construct_declarator_type(construct_type, type);
2231 if(construct_type != NULL) {
2232 obstack_free(&temp_obst, construct_type);
2238 static type_t *parse_abstract_declarator(type_t *base_type)
2240 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2242 type_t *result = construct_declarator_type(construct_type, base_type);
2243 if(construct_type != NULL) {
2244 obstack_free(&temp_obst, construct_type);
2250 static declaration_t *append_declaration(declaration_t* const declaration)
2252 if (last_declaration != NULL) {
2253 last_declaration->next = declaration;
2255 scope->declarations = declaration;
2257 last_declaration = declaration;
2262 * Check if the declaration of main is suspicious. main should be a
2263 * function with external linkage, returning int, taking either zero
2264 * arguments, two, or three arguments of appropriate types, ie.
2266 * int main([ int argc, char **argv [, char **env ] ]).
2268 * @param decl the declaration to check
2269 * @param type the function type of the declaration
2271 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2273 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2274 warningf(decl->source_position, "'main' is normally a non-static function");
2276 if (skip_typeref(func_type->return_type) != type_int) {
2277 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2279 const function_parameter_t *parm = func_type->parameters;
2281 type_t *const first_type = parm->type;
2282 if (!types_compatible(skip_typeref(first_type), type_int)) {
2283 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2287 type_t *const second_type = parm->type;
2288 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2289 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2293 type_t *const third_type = parm->type;
2294 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2295 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2299 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2303 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2309 * Check if a symbol is the equal to "main".
2311 static bool is_sym_main(const symbol_t *const sym)
2313 return strcmp(sym->string, "main") == 0;
2316 static declaration_t *internal_record_declaration(
2317 declaration_t *const declaration,
2318 const bool is_function_definition)
2320 const symbol_t *const symbol = declaration->symbol;
2321 const namespace_t namespc = (namespace_t)declaration->namespc;
2323 type_t *const orig_type = declaration->type;
2324 type_t *const type = skip_typeref(orig_type);
2325 if (is_type_function(type) &&
2326 type->function.unspecified_parameters &&
2327 warning.strict_prototypes) {
2328 warningf(declaration->source_position,
2329 "function declaration '%#T' is not a prototype",
2330 orig_type, declaration->symbol);
2333 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2334 check_type_of_main(declaration, &type->function);
2337 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2338 assert(declaration != previous_declaration);
2339 if (previous_declaration != NULL) {
2340 if (previous_declaration->parent_scope == scope) {
2341 /* can happen for K&R style declarations */
2342 if(previous_declaration->type == NULL) {
2343 previous_declaration->type = declaration->type;
2346 const type_t *prev_type = skip_typeref(previous_declaration->type);
2347 if (!types_compatible(type, prev_type)) {
2348 errorf(declaration->source_position,
2349 "declaration '%#T' is incompatible with "
2350 "previous declaration '%#T'",
2351 orig_type, symbol, previous_declaration->type, symbol);
2352 errorf(previous_declaration->source_position,
2353 "previous declaration of '%Y' was here", symbol);
2355 unsigned old_storage_class
2356 = previous_declaration->storage_class;
2357 unsigned new_storage_class = declaration->storage_class;
2359 if(is_type_incomplete(prev_type)) {
2360 previous_declaration->type = type;
2364 /* pretend no storage class means extern for function
2365 * declarations (except if the previous declaration is neither
2366 * none nor extern) */
2367 if (is_type_function(type)) {
2368 switch (old_storage_class) {
2369 case STORAGE_CLASS_NONE:
2370 old_storage_class = STORAGE_CLASS_EXTERN;
2372 case STORAGE_CLASS_EXTERN:
2373 if (is_function_definition) {
2374 if (warning.missing_prototypes &&
2375 prev_type->function.unspecified_parameters &&
2376 !is_sym_main(symbol)) {
2377 warningf(declaration->source_position,
2378 "no previous prototype for '%#T'",
2381 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2382 new_storage_class = STORAGE_CLASS_EXTERN;
2390 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2391 new_storage_class == STORAGE_CLASS_EXTERN) {
2392 warn_redundant_declaration:
2393 if (warning.redundant_decls) {
2394 warningf(declaration->source_position,
2395 "redundant declaration for '%Y'", symbol);
2396 warningf(previous_declaration->source_position,
2397 "previous declaration of '%Y' was here",
2400 } else if (current_function == NULL) {
2401 if (old_storage_class != STORAGE_CLASS_STATIC &&
2402 new_storage_class == STORAGE_CLASS_STATIC) {
2403 errorf(declaration->source_position,
2404 "static declaration of '%Y' follows non-static declaration",
2406 errorf(previous_declaration->source_position,
2407 "previous declaration of '%Y' was here", symbol);
2409 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2410 goto warn_redundant_declaration;
2412 if (new_storage_class == STORAGE_CLASS_NONE) {
2413 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2417 if (old_storage_class == new_storage_class) {
2418 errorf(declaration->source_position,
2419 "redeclaration of '%Y'", symbol);
2421 errorf(declaration->source_position,
2422 "redeclaration of '%Y' with different linkage",
2425 errorf(previous_declaration->source_position,
2426 "previous declaration of '%Y' was here", symbol);
2429 return previous_declaration;
2431 } else if (is_function_definition) {
2432 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2433 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2434 warningf(declaration->source_position,
2435 "no previous prototype for '%#T'", orig_type, symbol);
2436 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2437 warningf(declaration->source_position,
2438 "no previous declaration for '%#T'", orig_type,
2442 } else if (warning.missing_declarations &&
2443 scope == global_scope &&
2444 !is_type_function(type) && (
2445 declaration->storage_class == STORAGE_CLASS_NONE ||
2446 declaration->storage_class == STORAGE_CLASS_THREAD
2448 warningf(declaration->source_position,
2449 "no previous declaration for '%#T'", orig_type, symbol);
2452 assert(declaration->parent_scope == NULL);
2453 assert(declaration->symbol != NULL);
2454 assert(scope != NULL);
2456 declaration->parent_scope = scope;
2458 environment_push(declaration);
2459 return append_declaration(declaration);
2462 static declaration_t *record_declaration(declaration_t *declaration)
2464 return internal_record_declaration(declaration, false);
2467 static declaration_t *record_function_definition(declaration_t *declaration)
2469 return internal_record_declaration(declaration, true);
2472 static void parser_error_multiple_definition(declaration_t *declaration,
2473 const source_position_t source_position)
2475 errorf(source_position, "multiple definition of symbol '%Y'",
2476 declaration->symbol);
2477 errorf(declaration->source_position,
2478 "this is the location of the previous definition.");
2481 static bool is_declaration_specifier(const token_t *token,
2482 bool only_type_specifiers)
2484 switch(token->type) {
2488 return is_typedef_symbol(token->v.symbol);
2490 case T___extension__:
2493 return !only_type_specifiers;
2500 static void parse_init_declarator_rest(declaration_t *declaration)
2504 type_t *orig_type = declaration->type;
2505 type_t *type = type = skip_typeref(orig_type);
2507 if(declaration->init.initializer != NULL) {
2508 parser_error_multiple_definition(declaration, token.source_position);
2511 initializer_t *initializer = parse_initializer(type);
2513 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2514 * the array type size */
2515 if(is_type_array(type) && initializer != NULL) {
2516 array_type_t *array_type = &type->array;
2518 if(array_type->size == NULL) {
2519 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2521 cnst->base.type = type_size_t;
2523 switch (initializer->kind) {
2524 case INITIALIZER_LIST: {
2525 cnst->conste.v.int_value = initializer->list.len;
2529 case INITIALIZER_STRING: {
2530 cnst->conste.v.int_value = initializer->string.string.size;
2534 case INITIALIZER_WIDE_STRING: {
2535 cnst->conste.v.int_value = initializer->wide_string.string.size;
2540 panic("invalid initializer type");
2543 array_type->size = cnst;
2547 if(is_type_function(type)) {
2548 errorf(declaration->source_position,
2549 "initializers not allowed for function types at declator '%Y' (type '%T')",
2550 declaration->symbol, orig_type);
2552 declaration->init.initializer = initializer;
2556 /* parse rest of a declaration without any declarator */
2557 static void parse_anonymous_declaration_rest(
2558 const declaration_specifiers_t *specifiers,
2559 parsed_declaration_func finished_declaration)
2563 declaration_t *const declaration = allocate_declaration_zero();
2564 declaration->type = specifiers->type;
2565 declaration->storage_class = specifiers->storage_class;
2566 declaration->source_position = specifiers->source_position;
2568 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2569 warningf(declaration->source_position, "useless storage class in empty declaration");
2572 type_t *type = declaration->type;
2573 switch (type->kind) {
2574 case TYPE_COMPOUND_STRUCT:
2575 case TYPE_COMPOUND_UNION: {
2576 if (type->compound.declaration->symbol == NULL) {
2577 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2586 warningf(declaration->source_position, "empty declaration");
2590 finished_declaration(declaration);
2593 static void parse_declaration_rest(declaration_t *ndeclaration,
2594 const declaration_specifiers_t *specifiers,
2595 parsed_declaration_func finished_declaration)
2598 declaration_t *declaration = finished_declaration(ndeclaration);
2600 type_t *orig_type = declaration->type;
2601 type_t *type = skip_typeref(orig_type);
2603 if (type->kind != TYPE_FUNCTION &&
2604 declaration->is_inline &&
2605 is_type_valid(type)) {
2606 warningf(declaration->source_position,
2607 "variable '%Y' declared 'inline'\n", declaration->symbol);
2610 if(token.type == '=') {
2611 parse_init_declarator_rest(declaration);
2614 if(token.type != ',')
2618 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2623 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2625 symbol_t *symbol = declaration->symbol;
2626 if(symbol == NULL) {
2627 errorf(HERE, "anonymous declaration not valid as function parameter");
2630 namespace_t namespc = (namespace_t) declaration->namespc;
2631 if(namespc != NAMESPACE_NORMAL) {
2632 return record_declaration(declaration);
2635 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2636 if(previous_declaration == NULL ||
2637 previous_declaration->parent_scope != scope) {
2638 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2643 if(previous_declaration->type == NULL) {
2644 previous_declaration->type = declaration->type;
2645 previous_declaration->storage_class = declaration->storage_class;
2646 previous_declaration->parent_scope = scope;
2647 return previous_declaration;
2649 return record_declaration(declaration);
2653 static void parse_declaration(parsed_declaration_func finished_declaration)
2655 declaration_specifiers_t specifiers;
2656 memset(&specifiers, 0, sizeof(specifiers));
2657 parse_declaration_specifiers(&specifiers);
2659 if(token.type == ';') {
2660 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2662 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2663 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2667 static void parse_kr_declaration_list(declaration_t *declaration)
2669 type_t *type = skip_typeref(declaration->type);
2670 if(!is_type_function(type))
2673 if(!type->function.kr_style_parameters)
2676 /* push function parameters */
2677 int top = environment_top();
2678 scope_t *last_scope = scope;
2679 set_scope(&declaration->scope);
2681 declaration_t *parameter = declaration->scope.declarations;
2682 for( ; parameter != NULL; parameter = parameter->next) {
2683 assert(parameter->parent_scope == NULL);
2684 parameter->parent_scope = scope;
2685 environment_push(parameter);
2688 /* parse declaration list */
2689 while(is_declaration_specifier(&token, false)) {
2690 parse_declaration(finished_kr_declaration);
2693 /* pop function parameters */
2694 assert(scope == &declaration->scope);
2695 set_scope(last_scope);
2696 environment_pop_to(top);
2698 /* update function type */
2699 type_t *new_type = duplicate_type(type);
2700 new_type->function.kr_style_parameters = false;
2702 function_parameter_t *parameters = NULL;
2703 function_parameter_t *last_parameter = NULL;
2705 declaration_t *parameter_declaration = declaration->scope.declarations;
2706 for( ; parameter_declaration != NULL;
2707 parameter_declaration = parameter_declaration->next) {
2708 type_t *parameter_type = parameter_declaration->type;
2709 if(parameter_type == NULL) {
2711 errorf(HERE, "no type specified for function parameter '%Y'",
2712 parameter_declaration->symbol);
2714 if (warning.implicit_int) {
2715 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2716 parameter_declaration->symbol);
2718 parameter_type = type_int;
2719 parameter_declaration->type = parameter_type;
2723 semantic_parameter(parameter_declaration);
2724 parameter_type = parameter_declaration->type;
2726 function_parameter_t *function_parameter
2727 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2728 memset(function_parameter, 0, sizeof(function_parameter[0]));
2730 function_parameter->type = parameter_type;
2731 if(last_parameter != NULL) {
2732 last_parameter->next = function_parameter;
2734 parameters = function_parameter;
2736 last_parameter = function_parameter;
2738 new_type->function.parameters = parameters;
2740 type = typehash_insert(new_type);
2741 if(type != new_type) {
2742 obstack_free(type_obst, new_type);
2745 declaration->type = type;
2748 static bool first_err = true;
2751 * When called with first_err set, prints the name of the current function,
2754 static void print_in_function(void) {
2757 diagnosticf("%s: In function '%Y':\n",
2758 current_function->source_position.input_name,
2759 current_function->symbol);
2764 * Check if all labels are defined in the current function.
2765 * Check if all labels are used in the current function.
2767 static void check_labels(void)
2769 for (const goto_statement_t *goto_statement = goto_first;
2770 goto_statement != NULL;
2771 goto_statement = goto_statement->next) {
2772 declaration_t *label = goto_statement->label;
2775 if (label->source_position.input_name == NULL) {
2776 print_in_function();
2777 errorf(goto_statement->base.source_position,
2778 "label '%Y' used but not defined", label->symbol);
2781 goto_first = goto_last = NULL;
2783 if (warning.unused_label) {
2784 for (const label_statement_t *label_statement = label_first;
2785 label_statement != NULL;
2786 label_statement = label_statement->next) {
2787 const declaration_t *label = label_statement->label;
2789 if (! label->used) {
2790 print_in_function();
2791 warningf(label_statement->base.source_position,
2792 "label '%Y' defined but not used", label->symbol);
2796 label_first = label_last = NULL;
2800 * Check declarations of current_function for unused entities.
2802 static void check_declarations(void)
2804 if (warning.unused_parameter) {
2805 const scope_t *scope = ¤t_function->scope;
2807 const declaration_t *parameter = scope->declarations;
2808 for (; parameter != NULL; parameter = parameter->next) {
2809 if (! parameter->used) {
2810 print_in_function();
2811 warningf(parameter->source_position,
2812 "unused parameter '%Y'", parameter->symbol);
2816 if (warning.unused_variable) {
2820 static void parse_external_declaration(void)
2822 /* function-definitions and declarations both start with declaration
2824 declaration_specifiers_t specifiers;
2825 memset(&specifiers, 0, sizeof(specifiers));
2826 parse_declaration_specifiers(&specifiers);
2828 /* must be a declaration */
2829 if(token.type == ';') {
2830 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2834 /* declarator is common to both function-definitions and declarations */
2835 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2837 /* must be a declaration */
2838 if(token.type == ',' || token.type == '=' || token.type == ';') {
2839 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2843 /* must be a function definition */
2844 parse_kr_declaration_list(ndeclaration);
2846 if(token.type != '{') {
2847 parse_error_expected("while parsing function definition", '{', 0);
2852 type_t *type = ndeclaration->type;
2854 /* note that we don't skip typerefs: the standard doesn't allow them here
2855 * (so we can't use is_type_function here) */
2856 if(type->kind != TYPE_FUNCTION) {
2857 if (is_type_valid(type)) {
2858 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2859 type, ndeclaration->symbol);
2865 /* § 6.7.5.3 (14) a function definition with () means no
2866 * parameters (and not unspecified parameters) */
2867 if(type->function.unspecified_parameters) {
2868 type_t *duplicate = duplicate_type(type);
2869 duplicate->function.unspecified_parameters = false;
2871 type = typehash_insert(duplicate);
2872 if(type != duplicate) {
2873 obstack_free(type_obst, duplicate);
2875 ndeclaration->type = type;
2878 declaration_t *const declaration = record_function_definition(ndeclaration);
2879 if(ndeclaration != declaration) {
2880 declaration->scope = ndeclaration->scope;
2882 type = skip_typeref(declaration->type);
2884 /* push function parameters and switch scope */
2885 int top = environment_top();
2886 scope_t *last_scope = scope;
2887 set_scope(&declaration->scope);
2889 declaration_t *parameter = declaration->scope.declarations;
2890 for( ; parameter != NULL; parameter = parameter->next) {
2891 if(parameter->parent_scope == &ndeclaration->scope) {
2892 parameter->parent_scope = scope;
2894 assert(parameter->parent_scope == NULL
2895 || parameter->parent_scope == scope);
2896 parameter->parent_scope = scope;
2897 environment_push(parameter);
2900 if(declaration->init.statement != NULL) {
2901 parser_error_multiple_definition(declaration, token.source_position);
2903 goto end_of_parse_external_declaration;
2905 /* parse function body */
2906 int label_stack_top = label_top();
2907 declaration_t *old_current_function = current_function;
2908 current_function = declaration;
2910 declaration->init.statement = parse_compound_statement();
2913 check_declarations();
2915 assert(current_function == declaration);
2916 current_function = old_current_function;
2917 label_pop_to(label_stack_top);
2920 end_of_parse_external_declaration:
2921 assert(scope == &declaration->scope);
2922 set_scope(last_scope);
2923 environment_pop_to(top);
2926 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2928 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2929 type->bitfield.base = base;
2930 type->bitfield.size = size;
2935 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2937 /* TODO: check constraints for struct declarations (in specifiers) */
2939 declaration_t *declaration;
2941 if(token.type == ':') {
2944 type_t *base_type = specifiers->type;
2945 expression_t *size = parse_constant_expression();
2947 type_t *type = make_bitfield_type(base_type, size);
2949 declaration = allocate_declaration_zero();
2950 declaration->namespc = NAMESPACE_NORMAL;
2951 declaration->storage_class = STORAGE_CLASS_NONE;
2952 declaration->source_position = token.source_position;
2953 declaration->modifiers = specifiers->decl_modifiers;
2954 declaration->type = type;
2956 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2958 if(token.type == ':') {
2960 expression_t *size = parse_constant_expression();
2962 type_t *type = make_bitfield_type(declaration->type, size);
2963 declaration->type = type;
2966 record_declaration(declaration);
2968 if(token.type != ',')
2975 static void parse_compound_type_entries(void)
2979 while(token.type != '}' && token.type != T_EOF) {
2980 declaration_specifiers_t specifiers;
2981 memset(&specifiers, 0, sizeof(specifiers));
2982 parse_declaration_specifiers(&specifiers);
2984 parse_struct_declarators(&specifiers);
2986 if(token.type == T_EOF) {
2987 errorf(HERE, "EOF while parsing struct");
2992 static type_t *parse_typename(void)
2994 declaration_specifiers_t specifiers;
2995 memset(&specifiers, 0, sizeof(specifiers));
2996 parse_declaration_specifiers(&specifiers);
2997 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2998 /* TODO: improve error message, user does probably not know what a
2999 * storage class is...
3001 errorf(HERE, "typename may not have a storage class");
3004 type_t *result = parse_abstract_declarator(specifiers.type);
3012 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3013 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3014 expression_t *left);
3016 typedef struct expression_parser_function_t expression_parser_function_t;
3017 struct expression_parser_function_t {
3018 unsigned precedence;
3019 parse_expression_function parser;
3020 unsigned infix_precedence;
3021 parse_expression_infix_function infix_parser;
3024 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3027 * Creates a new invalid expression.
3029 static expression_t *create_invalid_expression(void)
3031 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3032 expression->base.source_position = token.source_position;
3037 * Prints an error message if an expression was expected but not read
3039 static expression_t *expected_expression_error(void)
3041 /* skip the error message if the error token was read */
3042 if (token.type != T_ERROR) {
3043 errorf(HERE, "expected expression, got token '%K'", &token);
3047 return create_invalid_expression();
3051 * Parse a string constant.
3053 static expression_t *parse_string_const(void)
3055 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3056 cnst->base.type = type_char_ptr;
3057 cnst->string.value = parse_string_literals();
3063 * Parse a wide string constant.
3065 static expression_t *parse_wide_string_const(void)
3067 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3068 cnst->base.type = type_wchar_t_ptr;
3069 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3075 * Parse an integer constant.
3077 static expression_t *parse_int_const(void)
3079 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3080 cnst->base.type = token.datatype;
3081 cnst->conste.v.int_value = token.v.intvalue;
3089 * Parse a float constant.
3091 static expression_t *parse_float_const(void)
3093 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3094 cnst->base.type = token.datatype;
3095 cnst->conste.v.float_value = token.v.floatvalue;
3102 static declaration_t *create_implicit_function(symbol_t *symbol,
3103 const source_position_t source_position)
3105 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3106 ntype->function.return_type = type_int;
3107 ntype->function.unspecified_parameters = true;
3109 type_t *type = typehash_insert(ntype);
3114 declaration_t *const declaration = allocate_declaration_zero();
3115 declaration->storage_class = STORAGE_CLASS_EXTERN;
3116 declaration->type = type;
3117 declaration->symbol = symbol;
3118 declaration->source_position = source_position;
3119 declaration->parent_scope = global_scope;
3121 scope_t *old_scope = scope;
3122 set_scope(global_scope);
3124 environment_push(declaration);
3125 /* prepends the declaration to the global declarations list */
3126 declaration->next = scope->declarations;
3127 scope->declarations = declaration;
3129 assert(scope == global_scope);
3130 set_scope(old_scope);
3136 * Creates a return_type (func)(argument_type) function type if not
3139 * @param return_type the return type
3140 * @param argument_type the argument type
3142 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3144 function_parameter_t *parameter
3145 = obstack_alloc(type_obst, sizeof(parameter[0]));
3146 memset(parameter, 0, sizeof(parameter[0]));
3147 parameter->type = argument_type;
3149 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3150 type->function.return_type = return_type;
3151 type->function.parameters = parameter;
3153 type_t *result = typehash_insert(type);
3154 if(result != type) {
3162 * Creates a function type for some function like builtins.
3164 * @param symbol the symbol describing the builtin
3166 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3168 switch(symbol->ID) {
3169 case T___builtin_alloca:
3170 return make_function_1_type(type_void_ptr, type_size_t);
3171 case T___builtin_nan:
3172 return make_function_1_type(type_double, type_char_ptr);
3173 case T___builtin_nanf:
3174 return make_function_1_type(type_float, type_char_ptr);
3175 case T___builtin_nand:
3176 return make_function_1_type(type_long_double, type_char_ptr);
3177 case T___builtin_va_end:
3178 return make_function_1_type(type_void, type_valist);
3180 panic("not implemented builtin symbol found");
3185 * Performs automatic type cast as described in § 6.3.2.1.
3187 * @param orig_type the original type
3189 static type_t *automatic_type_conversion(type_t *orig_type)
3191 type_t *type = skip_typeref(orig_type);
3192 if(is_type_array(type)) {
3193 array_type_t *array_type = &type->array;
3194 type_t *element_type = array_type->element_type;
3195 unsigned qualifiers = array_type->type.qualifiers;
3197 return make_pointer_type(element_type, qualifiers);
3200 if(is_type_function(type)) {
3201 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3208 * reverts the automatic casts of array to pointer types and function
3209 * to function-pointer types as defined § 6.3.2.1
3211 type_t *revert_automatic_type_conversion(const expression_t *expression)
3213 switch (expression->kind) {
3214 case EXPR_REFERENCE: return expression->reference.declaration->type;
3215 case EXPR_SELECT: return expression->select.compound_entry->type;
3217 case EXPR_UNARY_DEREFERENCE: {
3218 const expression_t *const value = expression->unary.value;
3219 type_t *const type = skip_typeref(value->base.type);
3220 assert(is_type_pointer(type));
3221 return type->pointer.points_to;
3224 case EXPR_BUILTIN_SYMBOL:
3225 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3227 case EXPR_ARRAY_ACCESS: {
3228 const expression_t *array_ref = expression->array_access.array_ref;
3229 type_t *type_left = skip_typeref(array_ref->base.type);
3230 if (!is_type_valid(type_left))
3232 assert(is_type_pointer(type_left));
3233 return type_left->pointer.points_to;
3239 return expression->base.type;
3242 static expression_t *parse_reference(void)
3244 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3246 reference_expression_t *ref = &expression->reference;
3247 ref->symbol = token.v.symbol;
3249 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3251 source_position_t source_position = token.source_position;
3254 if(declaration == NULL) {
3255 if (! strict_mode && token.type == '(') {
3256 /* an implicitly defined function */
3257 if (warning.implicit_function_declaration) {
3258 warningf(HERE, "implicit declaration of function '%Y'",
3262 declaration = create_implicit_function(ref->symbol,
3265 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3270 type_t *type = declaration->type;
3272 /* we always do the auto-type conversions; the & and sizeof parser contains
3273 * code to revert this! */
3274 type = automatic_type_conversion(type);
3276 ref->declaration = declaration;
3277 ref->base.type = type;
3279 /* this declaration is used */
3280 declaration->used = true;
3285 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3289 /* TODO check if explicit cast is allowed and issue warnings/errors */
3292 static expression_t *parse_cast(void)
3294 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3296 cast->base.source_position = token.source_position;
3298 type_t *type = parse_typename();
3301 expression_t *value = parse_sub_expression(20);
3303 check_cast_allowed(value, type);
3305 cast->base.type = type;
3306 cast->unary.value = value;
3311 static expression_t *parse_statement_expression(void)
3313 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3315 statement_t *statement = parse_compound_statement();
3316 expression->statement.statement = statement;
3317 expression->base.source_position = statement->base.source_position;
3319 /* find last statement and use its type */
3320 type_t *type = type_void;
3321 const statement_t *stmt = statement->compound.statements;
3323 while (stmt->base.next != NULL)
3324 stmt = stmt->base.next;
3326 if (stmt->kind == STATEMENT_EXPRESSION) {
3327 type = stmt->expression.expression->base.type;
3330 warningf(expression->base.source_position, "empty statement expression ({})");
3332 expression->base.type = type;
3339 static expression_t *parse_brace_expression(void)
3343 switch(token.type) {
3345 /* gcc extension: a statement expression */
3346 return parse_statement_expression();
3350 return parse_cast();
3352 if(is_typedef_symbol(token.v.symbol)) {
3353 return parse_cast();
3357 expression_t *result = parse_expression();
3363 static expression_t *parse_function_keyword(void)
3368 if (current_function == NULL) {
3369 errorf(HERE, "'__func__' used outside of a function");
3372 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3373 expression->base.type = type_char_ptr;
3378 static expression_t *parse_pretty_function_keyword(void)
3380 eat(T___PRETTY_FUNCTION__);
3383 if (current_function == NULL) {
3384 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3387 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3388 expression->base.type = type_char_ptr;
3393 static designator_t *parse_designator(void)
3395 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3397 if(token.type != T_IDENTIFIER) {
3398 parse_error_expected("while parsing member designator",
3403 result->symbol = token.v.symbol;
3406 designator_t *last_designator = result;
3408 if(token.type == '.') {
3410 if(token.type != T_IDENTIFIER) {
3411 parse_error_expected("while parsing member designator",
3416 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3417 designator->symbol = token.v.symbol;
3420 last_designator->next = designator;
3421 last_designator = designator;
3424 if(token.type == '[') {
3426 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3427 designator->array_access = parse_expression();
3428 if(designator->array_access == NULL) {
3434 last_designator->next = designator;
3435 last_designator = designator;
3444 static expression_t *parse_offsetof(void)
3446 eat(T___builtin_offsetof);
3448 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3449 expression->base.type = type_size_t;
3452 expression->offsetofe.type = parse_typename();
3454 expression->offsetofe.designator = parse_designator();
3460 static expression_t *parse_va_start(void)
3462 eat(T___builtin_va_start);
3464 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3467 expression->va_starte.ap = parse_assignment_expression();
3469 expression_t *const expr = parse_assignment_expression();
3470 if (expr->kind == EXPR_REFERENCE) {
3471 declaration_t *const decl = expr->reference.declaration;
3473 return create_invalid_expression();
3474 if (decl->parent_scope == ¤t_function->scope &&
3475 decl->next == NULL) {
3476 expression->va_starte.parameter = decl;
3481 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3483 return create_invalid_expression();
3486 static expression_t *parse_va_arg(void)
3488 eat(T___builtin_va_arg);
3490 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3493 expression->va_arge.ap = parse_assignment_expression();
3495 expression->base.type = parse_typename();
3501 static expression_t *parse_builtin_symbol(void)
3503 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3505 symbol_t *symbol = token.v.symbol;
3507 expression->builtin_symbol.symbol = symbol;
3510 type_t *type = get_builtin_symbol_type(symbol);
3511 type = automatic_type_conversion(type);
3513 expression->base.type = type;
3517 static expression_t *parse_builtin_constant(void)
3519 eat(T___builtin_constant_p);
3521 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3524 expression->builtin_constant.value = parse_assignment_expression();
3526 expression->base.type = type_int;
3531 static expression_t *parse_builtin_prefetch(void)
3533 eat(T___builtin_prefetch);
3535 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3538 expression->builtin_prefetch.adr = parse_assignment_expression();
3539 if (token.type == ',') {
3541 expression->builtin_prefetch.rw = parse_assignment_expression();
3543 if (token.type == ',') {
3545 expression->builtin_prefetch.locality = parse_assignment_expression();
3548 expression->base.type = type_void;
3553 static expression_t *parse_compare_builtin(void)
3555 expression_t *expression;
3557 switch(token.type) {
3558 case T___builtin_isgreater:
3559 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3561 case T___builtin_isgreaterequal:
3562 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3564 case T___builtin_isless:
3565 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3567 case T___builtin_islessequal:
3568 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3570 case T___builtin_islessgreater:
3571 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3573 case T___builtin_isunordered:
3574 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3577 panic("invalid compare builtin found");
3580 expression->base.source_position = HERE;
3584 expression->binary.left = parse_assignment_expression();
3586 expression->binary.right = parse_assignment_expression();
3589 type_t *const orig_type_left = expression->binary.left->base.type;
3590 type_t *const orig_type_right = expression->binary.right->base.type;
3592 type_t *const type_left = skip_typeref(orig_type_left);
3593 type_t *const type_right = skip_typeref(orig_type_right);
3594 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3595 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3596 type_error_incompatible("invalid operands in comparison",
3597 expression->base.source_position, orig_type_left, orig_type_right);
3600 semantic_comparison(&expression->binary);
3606 static expression_t *parse_builtin_expect(void)
3608 eat(T___builtin_expect);
3610 expression_t *expression
3611 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3614 expression->binary.left = parse_assignment_expression();
3616 expression->binary.right = parse_constant_expression();
3619 expression->base.type = expression->binary.left->base.type;
3624 static expression_t *parse_assume(void) {
3627 expression_t *expression
3628 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3631 expression->unary.value = parse_assignment_expression();
3634 expression->base.type = type_void;
3638 static expression_t *parse_primary_expression(void)
3640 switch(token.type) {
3642 return parse_int_const();
3643 case T_FLOATINGPOINT:
3644 return parse_float_const();
3645 case T_STRING_LITERAL:
3646 return parse_string_const();
3647 case T_WIDE_STRING_LITERAL:
3648 return parse_wide_string_const();
3650 return parse_reference();
3651 case T___FUNCTION__:
3653 return parse_function_keyword();
3654 case T___PRETTY_FUNCTION__:
3655 return parse_pretty_function_keyword();
3656 case T___builtin_offsetof:
3657 return parse_offsetof();
3658 case T___builtin_va_start:
3659 return parse_va_start();
3660 case T___builtin_va_arg:
3661 return parse_va_arg();
3662 case T___builtin_expect:
3663 return parse_builtin_expect();
3664 case T___builtin_alloca:
3665 case T___builtin_nan:
3666 case T___builtin_nand:
3667 case T___builtin_nanf:
3668 case T___builtin_va_end:
3669 return parse_builtin_symbol();
3670 case T___builtin_isgreater:
3671 case T___builtin_isgreaterequal:
3672 case T___builtin_isless:
3673 case T___builtin_islessequal:
3674 case T___builtin_islessgreater:
3675 case T___builtin_isunordered:
3676 return parse_compare_builtin();
3677 case T___builtin_constant_p:
3678 return parse_builtin_constant();
3679 case T___builtin_prefetch:
3680 return parse_builtin_prefetch();
3682 return parse_assume();
3685 return parse_brace_expression();
3688 errorf(HERE, "unexpected token '%K'", &token);
3691 return create_invalid_expression();
3695 * Check if the expression has the character type and issue a warning then.
3697 static void check_for_char_index_type(const expression_t *expression) {
3698 type_t *const type = expression->base.type;
3699 const type_t *const base_type = skip_typeref(type);
3701 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3702 warning.char_subscripts) {
3703 warningf(expression->base.source_position,
3704 "array subscript has type '%T'", type);
3708 static expression_t *parse_array_expression(unsigned precedence,
3715 expression_t *inside = parse_expression();
3717 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3719 array_access_expression_t *array_access = &expression->array_access;
3721 type_t *const orig_type_left = left->base.type;
3722 type_t *const orig_type_inside = inside->base.type;
3724 type_t *const type_left = skip_typeref(orig_type_left);
3725 type_t *const type_inside = skip_typeref(orig_type_inside);
3727 type_t *return_type;
3728 if (is_type_pointer(type_left)) {
3729 return_type = type_left->pointer.points_to;
3730 array_access->array_ref = left;
3731 array_access->index = inside;
3732 check_for_char_index_type(inside);
3733 } else if (is_type_pointer(type_inside)) {
3734 return_type = type_inside->pointer.points_to;
3735 array_access->array_ref = inside;
3736 array_access->index = left;
3737 array_access->flipped = true;
3738 check_for_char_index_type(left);
3740 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3742 "array access on object with non-pointer types '%T', '%T'",
3743 orig_type_left, orig_type_inside);
3745 return_type = type_error_type;
3746 array_access->array_ref = create_invalid_expression();
3749 if(token.type != ']') {
3750 parse_error_expected("Problem while parsing array access", ']', 0);
3755 return_type = automatic_type_conversion(return_type);
3756 expression->base.type = return_type;
3761 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3763 expression_t *tp_expression = allocate_expression_zero(kind);
3764 tp_expression->base.type = type_size_t;
3766 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3768 tp_expression->typeprop.type = parse_typename();
3771 expression_t *expression = parse_sub_expression(precedence);
3772 expression->base.type = revert_automatic_type_conversion(expression);
3774 tp_expression->typeprop.type = expression->base.type;
3775 tp_expression->typeprop.tp_expression = expression;
3778 return tp_expression;
3781 static expression_t *parse_sizeof(unsigned precedence)
3784 return parse_typeprop(EXPR_SIZEOF, precedence);
3787 static expression_t *parse_alignof(unsigned precedence)
3790 return parse_typeprop(EXPR_SIZEOF, precedence);
3793 static expression_t *parse_select_expression(unsigned precedence,
3794 expression_t *compound)
3797 assert(token.type == '.' || token.type == T_MINUSGREATER);
3799 bool is_pointer = (token.type == T_MINUSGREATER);
3802 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3803 select->select.compound = compound;
3805 if(token.type != T_IDENTIFIER) {
3806 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3809 symbol_t *symbol = token.v.symbol;
3810 select->select.symbol = symbol;
3813 type_t *const orig_type = compound->base.type;
3814 type_t *const type = skip_typeref(orig_type);
3816 type_t *type_left = type;
3818 if (!is_type_pointer(type)) {
3819 if (is_type_valid(type)) {
3820 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3822 return create_invalid_expression();
3824 type_left = type->pointer.points_to;
3826 type_left = skip_typeref(type_left);
3828 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3829 type_left->kind != TYPE_COMPOUND_UNION) {
3830 if (is_type_valid(type_left)) {
3831 errorf(HERE, "request for member '%Y' in something not a struct or "
3832 "union, but '%T'", symbol, type_left);
3834 return create_invalid_expression();
3837 declaration_t *const declaration = type_left->compound.declaration;
3839 if(!declaration->init.is_defined) {
3840 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3842 return create_invalid_expression();
3845 declaration_t *iter = declaration->scope.declarations;
3846 for( ; iter != NULL; iter = iter->next) {
3847 if(iter->symbol == symbol) {
3852 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3853 return create_invalid_expression();
3856 /* we always do the auto-type conversions; the & and sizeof parser contains
3857 * code to revert this! */
3858 type_t *expression_type = automatic_type_conversion(iter->type);
3860 select->select.compound_entry = iter;
3861 select->base.type = expression_type;
3863 if(expression_type->kind == TYPE_BITFIELD) {
3864 expression_t *extract
3865 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3866 extract->unary.value = select;
3867 extract->base.type = expression_type->bitfield.base;
3876 * Parse a call expression, ie. expression '( ... )'.
3878 * @param expression the function address
3880 static expression_t *parse_call_expression(unsigned precedence,
3881 expression_t *expression)
3884 expression_t *result = allocate_expression_zero(EXPR_CALL);
3886 call_expression_t *call = &result->call;
3887 call->function = expression;
3889 type_t *const orig_type = expression->base.type;
3890 type_t *const type = skip_typeref(orig_type);
3892 function_type_t *function_type = NULL;
3893 if (is_type_pointer(type)) {
3894 type_t *const to_type = skip_typeref(type->pointer.points_to);
3896 if (is_type_function(to_type)) {
3897 function_type = &to_type->function;
3898 call->base.type = function_type->return_type;
3902 if (function_type == NULL && is_type_valid(type)) {
3903 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3906 /* parse arguments */
3909 if(token.type != ')') {
3910 call_argument_t *last_argument = NULL;
3913 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3915 argument->expression = parse_assignment_expression();
3916 if(last_argument == NULL) {
3917 call->arguments = argument;
3919 last_argument->next = argument;
3921 last_argument = argument;
3923 if(token.type != ',')
3930 if(function_type != NULL) {
3931 function_parameter_t *parameter = function_type->parameters;
3932 call_argument_t *argument = call->arguments;
3933 for( ; parameter != NULL && argument != NULL;
3934 parameter = parameter->next, argument = argument->next) {
3935 type_t *expected_type = parameter->type;
3936 /* TODO report scope in error messages */
3937 expression_t *const arg_expr = argument->expression;
3938 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3939 if (res_type == NULL) {
3940 /* TODO improve error message */
3941 errorf(arg_expr->base.source_position,
3942 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3943 arg_expr, arg_expr->base.type, expected_type);
3945 argument->expression = create_implicit_cast(argument->expression, expected_type);
3948 /* too few parameters */
3949 if(parameter != NULL) {
3950 errorf(HERE, "too few arguments to function '%E'", expression);
3951 } else if(argument != NULL) {
3952 /* too many parameters */
3953 if(!function_type->variadic
3954 && !function_type->unspecified_parameters) {
3955 errorf(HERE, "too many arguments to function '%E'", expression);
3957 /* do default promotion */
3958 for( ; argument != NULL; argument = argument->next) {
3959 type_t *type = argument->expression->base.type;
3961 type = skip_typeref(type);
3962 if(is_type_integer(type)) {
3963 type = promote_integer(type);
3964 } else if(type == type_float) {
3968 argument->expression
3969 = create_implicit_cast(argument->expression, type);
3972 check_format(&result->call);
3975 check_format(&result->call);
3982 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3984 static bool same_compound_type(const type_t *type1, const type_t *type2)
3987 is_type_compound(type1) &&
3988 type1->kind == type2->kind &&
3989 type1->compound.declaration == type2->compound.declaration;
3993 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3995 * @param expression the conditional expression
3997 static expression_t *parse_conditional_expression(unsigned precedence,
3998 expression_t *expression)
4002 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4004 conditional_expression_t *conditional = &result->conditional;
4005 conditional->condition = expression;
4008 type_t *const condition_type_orig = expression->base.type;
4009 type_t *const condition_type = skip_typeref(condition_type_orig);
4010 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4011 type_error("expected a scalar type in conditional condition",
4012 expression->base.source_position, condition_type_orig);
4015 expression_t *true_expression = parse_expression();
4017 expression_t *false_expression = parse_sub_expression(precedence);
4019 conditional->true_expression = true_expression;
4020 conditional->false_expression = false_expression;
4022 type_t *const orig_true_type = true_expression->base.type;
4023 type_t *const orig_false_type = false_expression->base.type;
4024 type_t *const true_type = skip_typeref(orig_true_type);
4025 type_t *const false_type = skip_typeref(orig_false_type);
4028 type_t *result_type;
4029 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4030 result_type = semantic_arithmetic(true_type, false_type);
4032 true_expression = create_implicit_cast(true_expression, result_type);
4033 false_expression = create_implicit_cast(false_expression, result_type);
4035 conditional->true_expression = true_expression;
4036 conditional->false_expression = false_expression;
4037 conditional->base.type = result_type;
4038 } else if (same_compound_type(true_type, false_type) || (
4039 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4040 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4042 /* just take 1 of the 2 types */
4043 result_type = true_type;
4044 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4045 && pointers_compatible(true_type, false_type)) {
4047 result_type = true_type;
4050 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4051 type_error_incompatible("while parsing conditional",
4052 expression->base.source_position, true_type,
4055 result_type = type_error_type;
4058 conditional->base.type = result_type;
4063 * Parse an extension expression.
4065 static expression_t *parse_extension(unsigned precedence)
4067 eat(T___extension__);
4069 /* TODO enable extensions */
4070 expression_t *expression = parse_sub_expression(precedence);
4071 /* TODO disable extensions */
4075 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4077 eat(T___builtin_classify_type);
4079 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4080 result->base.type = type_int;
4083 expression_t *expression = parse_sub_expression(precedence);
4085 result->classify_type.type_expression = expression;
4090 static void semantic_incdec(unary_expression_t *expression)
4092 type_t *const orig_type = expression->value->base.type;
4093 type_t *const type = skip_typeref(orig_type);
4094 /* TODO !is_type_real && !is_type_pointer */
4095 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4096 if (is_type_valid(type)) {
4097 /* TODO: improve error message */
4098 errorf(HERE, "operation needs an arithmetic or pointer type");
4103 expression->base.type = orig_type;
4106 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4108 type_t *const orig_type = expression->value->base.type;
4109 type_t *const type = skip_typeref(orig_type);
4110 if(!is_type_arithmetic(type)) {
4111 if (is_type_valid(type)) {
4112 /* TODO: improve error message */
4113 errorf(HERE, "operation needs an arithmetic type");
4118 expression->base.type = orig_type;
4121 static void semantic_unexpr_scalar(unary_expression_t *expression)
4123 type_t *const orig_type = expression->value->base.type;
4124 type_t *const type = skip_typeref(orig_type);
4125 if (!is_type_scalar(type)) {
4126 if (is_type_valid(type)) {
4127 errorf(HERE, "operand of ! must be of scalar type");
4132 expression->base.type = orig_type;
4135 static void semantic_unexpr_integer(unary_expression_t *expression)
4137 type_t *const orig_type = expression->value->base.type;
4138 type_t *const type = skip_typeref(orig_type);
4139 if (!is_type_integer(type)) {
4140 if (is_type_valid(type)) {
4141 errorf(HERE, "operand of ~ must be of integer type");
4146 expression->base.type = orig_type;
4149 static void semantic_dereference(unary_expression_t *expression)
4151 type_t *const orig_type = expression->value->base.type;
4152 type_t *const type = skip_typeref(orig_type);
4153 if(!is_type_pointer(type)) {
4154 if (is_type_valid(type)) {
4155 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4160 type_t *result_type = type->pointer.points_to;
4161 result_type = automatic_type_conversion(result_type);
4162 expression->base.type = result_type;
4166 * Check the semantic of the address taken expression.
4168 static void semantic_take_addr(unary_expression_t *expression)
4170 expression_t *value = expression->value;
4171 value->base.type = revert_automatic_type_conversion(value);
4173 type_t *orig_type = value->base.type;
4174 if(!is_type_valid(orig_type))
4177 if(value->kind == EXPR_REFERENCE) {
4178 declaration_t *const declaration = value->reference.declaration;
4179 if(declaration != NULL) {
4180 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4181 errorf(expression->base.source_position,
4182 "address of register variable '%Y' requested",
4183 declaration->symbol);
4185 declaration->address_taken = 1;
4189 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4192 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4193 static expression_t *parse_##unexpression_type(unsigned precedence) \
4197 expression_t *unary_expression \
4198 = allocate_expression_zero(unexpression_type); \
4199 unary_expression->base.source_position = HERE; \
4200 unary_expression->unary.value = parse_sub_expression(precedence); \
4202 sfunc(&unary_expression->unary); \
4204 return unary_expression; \
4207 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4208 semantic_unexpr_arithmetic)
4209 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4210 semantic_unexpr_arithmetic)
4211 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4212 semantic_unexpr_scalar)
4213 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4214 semantic_dereference)
4215 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4217 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4218 semantic_unexpr_integer)
4219 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4221 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4224 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4226 static expression_t *parse_##unexpression_type(unsigned precedence, \
4227 expression_t *left) \
4229 (void) precedence; \
4232 expression_t *unary_expression \
4233 = allocate_expression_zero(unexpression_type); \
4234 unary_expression->unary.value = left; \
4236 sfunc(&unary_expression->unary); \
4238 return unary_expression; \
4241 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4242 EXPR_UNARY_POSTFIX_INCREMENT,
4244 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4245 EXPR_UNARY_POSTFIX_DECREMENT,
4248 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4250 /* TODO: handle complex + imaginary types */
4252 /* § 6.3.1.8 Usual arithmetic conversions */
4253 if(type_left == type_long_double || type_right == type_long_double) {
4254 return type_long_double;
4255 } else if(type_left == type_double || type_right == type_double) {
4257 } else if(type_left == type_float || type_right == type_float) {
4261 type_right = promote_integer(type_right);
4262 type_left = promote_integer(type_left);
4264 if(type_left == type_right)
4267 bool signed_left = is_type_signed(type_left);
4268 bool signed_right = is_type_signed(type_right);
4269 int rank_left = get_rank(type_left);
4270 int rank_right = get_rank(type_right);
4271 if(rank_left < rank_right) {
4272 if(signed_left == signed_right || !signed_right) {
4278 if(signed_left == signed_right || !signed_left) {
4287 * Check the semantic restrictions for a binary expression.
4289 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4291 expression_t *const left = expression->left;
4292 expression_t *const right = expression->right;
4293 type_t *const orig_type_left = left->base.type;
4294 type_t *const orig_type_right = right->base.type;
4295 type_t *const type_left = skip_typeref(orig_type_left);
4296 type_t *const type_right = skip_typeref(orig_type_right);
4298 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4299 /* TODO: improve error message */
4300 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4301 errorf(HERE, "operation needs arithmetic types");
4306 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4307 expression->left = create_implicit_cast(left, arithmetic_type);
4308 expression->right = create_implicit_cast(right, arithmetic_type);
4309 expression->base.type = arithmetic_type;
4312 static void semantic_shift_op(binary_expression_t *expression)
4314 expression_t *const left = expression->left;
4315 expression_t *const right = expression->right;
4316 type_t *const orig_type_left = left->base.type;
4317 type_t *const orig_type_right = right->base.type;
4318 type_t * type_left = skip_typeref(orig_type_left);
4319 type_t * type_right = skip_typeref(orig_type_right);
4321 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4322 /* TODO: improve error message */
4323 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4324 errorf(HERE, "operation needs integer types");
4329 type_left = promote_integer(type_left);
4330 type_right = promote_integer(type_right);
4332 expression->left = create_implicit_cast(left, type_left);
4333 expression->right = create_implicit_cast(right, type_right);
4334 expression->base.type = type_left;
4337 static void semantic_add(binary_expression_t *expression)
4339 expression_t *const left = expression->left;
4340 expression_t *const right = expression->right;
4341 type_t *const orig_type_left = left->base.type;
4342 type_t *const orig_type_right = right->base.type;
4343 type_t *const type_left = skip_typeref(orig_type_left);
4344 type_t *const type_right = skip_typeref(orig_type_right);
4347 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4348 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4349 expression->left = create_implicit_cast(left, arithmetic_type);
4350 expression->right = create_implicit_cast(right, arithmetic_type);
4351 expression->base.type = arithmetic_type;
4353 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4354 expression->base.type = type_left;
4355 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4356 expression->base.type = type_right;
4357 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4358 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4362 static void semantic_sub(binary_expression_t *expression)
4364 expression_t *const left = expression->left;
4365 expression_t *const right = expression->right;
4366 type_t *const orig_type_left = left->base.type;
4367 type_t *const orig_type_right = right->base.type;
4368 type_t *const type_left = skip_typeref(orig_type_left);
4369 type_t *const type_right = skip_typeref(orig_type_right);
4372 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4373 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4374 expression->left = create_implicit_cast(left, arithmetic_type);
4375 expression->right = create_implicit_cast(right, arithmetic_type);
4376 expression->base.type = arithmetic_type;
4378 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4379 expression->base.type = type_left;
4380 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4381 if(!pointers_compatible(type_left, type_right)) {
4383 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4384 orig_type_left, orig_type_right);
4386 expression->base.type = type_ptrdiff_t;
4388 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4389 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4390 orig_type_left, orig_type_right);
4395 * Check the semantics of comparison expressions.
4397 * @param expression The expression to check.
4399 static void semantic_comparison(binary_expression_t *expression)
4401 expression_t *left = expression->left;
4402 expression_t *right = expression->right;
4403 type_t *orig_type_left = left->base.type;
4404 type_t *orig_type_right = right->base.type;
4406 type_t *type_left = skip_typeref(orig_type_left);
4407 type_t *type_right = skip_typeref(orig_type_right);
4409 /* TODO non-arithmetic types */
4410 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4411 if (warning.sign_compare &&
4412 (expression->base.kind != EXPR_BINARY_EQUAL &&
4413 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4414 (is_type_signed(type_left) != is_type_signed(type_right))) {
4415 warningf(expression->base.source_position,
4416 "comparison between signed and unsigned");
4418 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4419 expression->left = create_implicit_cast(left, arithmetic_type);
4420 expression->right = create_implicit_cast(right, arithmetic_type);
4421 expression->base.type = arithmetic_type;
4422 if (warning.float_equal &&
4423 (expression->base.kind == EXPR_BINARY_EQUAL ||
4424 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4425 is_type_float(arithmetic_type)) {
4426 warningf(expression->base.source_position,
4427 "comparing floating point with == or != is unsafe");
4429 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4430 /* TODO check compatibility */
4431 } else if (is_type_pointer(type_left)) {
4432 expression->right = create_implicit_cast(right, type_left);
4433 } else if (is_type_pointer(type_right)) {
4434 expression->left = create_implicit_cast(left, type_right);
4435 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4436 type_error_incompatible("invalid operands in comparison",
4437 expression->base.source_position,
4438 type_left, type_right);
4440 expression->base.type = type_int;
4443 static void semantic_arithmetic_assign(binary_expression_t *expression)
4445 expression_t *left = expression->left;
4446 expression_t *right = expression->right;
4447 type_t *orig_type_left = left->base.type;
4448 type_t *orig_type_right = right->base.type;
4450 type_t *type_left = skip_typeref(orig_type_left);
4451 type_t *type_right = skip_typeref(orig_type_right);
4453 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4454 /* TODO: improve error message */
4455 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4456 errorf(HERE, "operation needs arithmetic types");
4461 /* combined instructions are tricky. We can't create an implicit cast on
4462 * the left side, because we need the uncasted form for the store.
4463 * The ast2firm pass has to know that left_type must be right_type
4464 * for the arithmetic operation and create a cast by itself */
4465 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4466 expression->right = create_implicit_cast(right, arithmetic_type);
4467 expression->base.type = type_left;
4470 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4472 expression_t *const left = expression->left;
4473 expression_t *const right = expression->right;
4474 type_t *const orig_type_left = left->base.type;
4475 type_t *const orig_type_right = right->base.type;
4476 type_t *const type_left = skip_typeref(orig_type_left);
4477 type_t *const type_right = skip_typeref(orig_type_right);
4479 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4480 /* combined instructions are tricky. We can't create an implicit cast on
4481 * the left side, because we need the uncasted form for the store.
4482 * The ast2firm pass has to know that left_type must be right_type
4483 * for the arithmetic operation and create a cast by itself */
4484 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4485 expression->right = create_implicit_cast(right, arithmetic_type);
4486 expression->base.type = type_left;
4487 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4488 expression->base.type = type_left;
4489 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4490 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4495 * Check the semantic restrictions of a logical expression.
4497 static void semantic_logical_op(binary_expression_t *expression)
4499 expression_t *const left = expression->left;
4500 expression_t *const right = expression->right;
4501 type_t *const orig_type_left = left->base.type;
4502 type_t *const orig_type_right = right->base.type;
4503 type_t *const type_left = skip_typeref(orig_type_left);
4504 type_t *const type_right = skip_typeref(orig_type_right);
4506 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4507 /* TODO: improve error message */
4508 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4509 errorf(HERE, "operation needs scalar types");
4514 expression->base.type = type_int;
4518 * Checks if a compound type has constant fields.
4520 static bool has_const_fields(const compound_type_t *type)
4522 const scope_t *scope = &type->declaration->scope;
4523 const declaration_t *declaration = scope->declarations;
4525 for (; declaration != NULL; declaration = declaration->next) {
4526 if (declaration->namespc != NAMESPACE_NORMAL)
4529 const type_t *decl_type = skip_typeref(declaration->type);
4530 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4538 * Check the semantic restrictions of a binary assign expression.
4540 static void semantic_binexpr_assign(binary_expression_t *expression)
4542 expression_t *left = expression->left;
4543 type_t *orig_type_left = left->base.type;
4545 type_t *type_left = revert_automatic_type_conversion(left);
4546 type_left = skip_typeref(orig_type_left);
4548 /* must be a modifiable lvalue */
4549 if (is_type_array(type_left)) {
4550 errorf(HERE, "cannot assign to arrays ('%E')", left);
4553 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4554 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4558 if(is_type_incomplete(type_left)) {
4560 "left-hand side of assignment '%E' has incomplete type '%T'",
4561 left, orig_type_left);
4564 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4565 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4566 left, orig_type_left);
4570 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4572 if (res_type == NULL) {
4573 errorf(expression->base.source_position,
4574 "cannot assign to '%T' from '%T'",
4575 orig_type_left, expression->right->base.type);
4577 expression->right = create_implicit_cast(expression->right, res_type);
4580 expression->base.type = orig_type_left;
4583 static bool expression_has_effect(const expression_t *const expr)
4585 switch (expr->kind) {
4586 case EXPR_UNKNOWN: break;
4587 case EXPR_INVALID: break;
4588 case EXPR_REFERENCE: return false;
4589 case EXPR_CONST: return false;
4590 case EXPR_STRING_LITERAL: return false;
4591 case EXPR_WIDE_STRING_LITERAL: return false;
4593 const call_expression_t *const call = &expr->call;
4594 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4597 switch (call->function->builtin_symbol.symbol->ID) {
4598 case T___builtin_va_end: return true;
4599 default: return false;
4602 case EXPR_CONDITIONAL: {
4603 const conditional_expression_t *const cond = &expr->conditional;
4605 expression_has_effect(cond->true_expression) &&
4606 expression_has_effect(cond->false_expression);
4608 case EXPR_SELECT: return false;
4609 case EXPR_ARRAY_ACCESS: return false;
4610 case EXPR_SIZEOF: return false;
4611 case EXPR_CLASSIFY_TYPE: return false;
4612 case EXPR_ALIGNOF: return false;
4614 case EXPR_FUNCTION: return false;
4615 case EXPR_PRETTY_FUNCTION: return false;
4616 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4617 case EXPR_BUILTIN_CONSTANT_P: return false;
4618 case EXPR_BUILTIN_PREFETCH: return true;
4619 case EXPR_OFFSETOF: return false;
4620 case EXPR_VA_START: return true;
4621 case EXPR_VA_ARG: return true;
4622 case EXPR_STATEMENT: return true; // TODO
4624 case EXPR_UNARY_NEGATE: return false;
4625 case EXPR_UNARY_PLUS: return false;
4626 case EXPR_UNARY_BITWISE_NEGATE: return false;
4627 case EXPR_UNARY_NOT: return false;
4628 case EXPR_UNARY_DEREFERENCE: return false;
4629 case EXPR_UNARY_TAKE_ADDRESS: return false;
4630 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4631 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4632 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4633 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4634 case EXPR_UNARY_CAST:
4635 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4636 case EXPR_UNARY_CAST_IMPLICIT: return true;
4637 case EXPR_UNARY_ASSUME: return true;
4638 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4640 case EXPR_BINARY_ADD: return false;
4641 case EXPR_BINARY_SUB: return false;
4642 case EXPR_BINARY_MUL: return false;
4643 case EXPR_BINARY_DIV: return false;
4644 case EXPR_BINARY_MOD: return false;
4645 case EXPR_BINARY_EQUAL: return false;
4646 case EXPR_BINARY_NOTEQUAL: return false;
4647 case EXPR_BINARY_LESS: return false;
4648 case EXPR_BINARY_LESSEQUAL: return false;
4649 case EXPR_BINARY_GREATER: return false;
4650 case EXPR_BINARY_GREATEREQUAL: return false;
4651 case EXPR_BINARY_BITWISE_AND: return false;
4652 case EXPR_BINARY_BITWISE_OR: return false;
4653 case EXPR_BINARY_BITWISE_XOR: return false;
4654 case EXPR_BINARY_SHIFTLEFT: return false;
4655 case EXPR_BINARY_SHIFTRIGHT: return false;
4656 case EXPR_BINARY_ASSIGN: return true;
4657 case EXPR_BINARY_MUL_ASSIGN: return true;
4658 case EXPR_BINARY_DIV_ASSIGN: return true;
4659 case EXPR_BINARY_MOD_ASSIGN: return true;
4660 case EXPR_BINARY_ADD_ASSIGN: return true;
4661 case EXPR_BINARY_SUB_ASSIGN: return true;
4662 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4663 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4664 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4665 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4666 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4667 case EXPR_BINARY_LOGICAL_AND:
4668 case EXPR_BINARY_LOGICAL_OR:
4669 case EXPR_BINARY_COMMA:
4670 return expression_has_effect(expr->binary.right);
4672 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4673 case EXPR_BINARY_ISGREATER: return false;
4674 case EXPR_BINARY_ISGREATEREQUAL: return false;
4675 case EXPR_BINARY_ISLESS: return false;
4676 case EXPR_BINARY_ISLESSEQUAL: return false;
4677 case EXPR_BINARY_ISLESSGREATER: return false;
4678 case EXPR_BINARY_ISUNORDERED: return false;
4681 panic("unexpected statement");
4684 static void semantic_comma(binary_expression_t *expression)
4686 if (warning.unused_value) {
4687 const expression_t *const left = expression->left;
4688 if (!expression_has_effect(left)) {
4689 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4692 expression->base.type = expression->right->base.type;
4695 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4696 static expression_t *parse_##binexpression_type(unsigned precedence, \
4697 expression_t *left) \
4700 source_position_t pos = HERE; \
4702 expression_t *right = parse_sub_expression(precedence + lr); \
4704 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4705 binexpr->base.source_position = pos; \
4706 binexpr->binary.left = left; \
4707 binexpr->binary.right = right; \
4708 sfunc(&binexpr->binary); \
4713 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4714 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4715 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4716 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4717 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4718 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4719 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4720 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4721 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4723 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4724 semantic_comparison, 1)
4725 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4726 semantic_comparison, 1)
4727 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4728 semantic_comparison, 1)
4729 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4730 semantic_comparison, 1)
4732 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4733 semantic_binexpr_arithmetic, 1)
4734 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4735 semantic_binexpr_arithmetic, 1)
4736 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4737 semantic_binexpr_arithmetic, 1)
4738 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4739 semantic_logical_op, 1)
4740 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4741 semantic_logical_op, 1)
4742 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4743 semantic_shift_op, 1)
4744 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4745 semantic_shift_op, 1)
4746 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4747 semantic_arithmetic_addsubb_assign, 0)
4748 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4749 semantic_arithmetic_addsubb_assign, 0)
4750 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4751 semantic_arithmetic_assign, 0)
4752 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4753 semantic_arithmetic_assign, 0)
4754 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4755 semantic_arithmetic_assign, 0)
4756 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4757 semantic_arithmetic_assign, 0)
4758 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4759 semantic_arithmetic_assign, 0)
4760 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4761 semantic_arithmetic_assign, 0)
4762 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4763 semantic_arithmetic_assign, 0)
4764 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4765 semantic_arithmetic_assign, 0)
4767 static expression_t *parse_sub_expression(unsigned precedence)
4769 if(token.type < 0) {
4770 return expected_expression_error();
4773 expression_parser_function_t *parser
4774 = &expression_parsers[token.type];
4775 source_position_t source_position = token.source_position;
4778 if(parser->parser != NULL) {
4779 left = parser->parser(parser->precedence);
4781 left = parse_primary_expression();
4783 assert(left != NULL);
4784 left->base.source_position = source_position;
4787 if(token.type < 0) {
4788 return expected_expression_error();
4791 parser = &expression_parsers[token.type];
4792 if(parser->infix_parser == NULL)
4794 if(parser->infix_precedence < precedence)
4797 left = parser->infix_parser(parser->infix_precedence, left);
4799 assert(left != NULL);
4800 assert(left->kind != EXPR_UNKNOWN);
4801 left->base.source_position = source_position;
4808 * Parse an expression.
4810 static expression_t *parse_expression(void)
4812 return parse_sub_expression(1);
4816 * Register a parser for a prefix-like operator with given precedence.
4818 * @param parser the parser function
4819 * @param token_type the token type of the prefix token
4820 * @param precedence the precedence of the operator
4822 static void register_expression_parser(parse_expression_function parser,
4823 int token_type, unsigned precedence)
4825 expression_parser_function_t *entry = &expression_parsers[token_type];
4827 if(entry->parser != NULL) {
4828 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4829 panic("trying to register multiple expression parsers for a token");
4831 entry->parser = parser;
4832 entry->precedence = precedence;
4836 * Register a parser for an infix operator with given precedence.
4838 * @param parser the parser function
4839 * @param token_type the token type of the infix operator
4840 * @param precedence the precedence of the operator
4842 static void register_infix_parser(parse_expression_infix_function parser,
4843 int token_type, unsigned precedence)
4845 expression_parser_function_t *entry = &expression_parsers[token_type];
4847 if(entry->infix_parser != NULL) {
4848 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4849 panic("trying to register multiple infix expression parsers for a "
4852 entry->infix_parser = parser;
4853 entry->infix_precedence = precedence;
4857 * Initialize the expression parsers.
4859 static void init_expression_parsers(void)
4861 memset(&expression_parsers, 0, sizeof(expression_parsers));
4863 register_infix_parser(parse_array_expression, '[', 30);
4864 register_infix_parser(parse_call_expression, '(', 30);
4865 register_infix_parser(parse_select_expression, '.', 30);
4866 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4867 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4869 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4872 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4873 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4874 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4875 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4876 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4877 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4878 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4879 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4880 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4881 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4882 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4883 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4884 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4885 T_EXCLAMATIONMARKEQUAL, 13);
4886 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4887 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4888 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4889 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4890 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4891 register_infix_parser(parse_conditional_expression, '?', 7);
4892 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4893 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4894 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4895 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4896 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4897 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4898 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4899 T_LESSLESSEQUAL, 2);
4900 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4901 T_GREATERGREATEREQUAL, 2);
4902 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4904 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4906 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4909 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4911 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4912 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4913 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4914 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4915 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4916 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4917 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4919 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4921 register_expression_parser(parse_sizeof, T_sizeof, 25);
4922 register_expression_parser(parse_alignof, T___alignof__, 25);
4923 register_expression_parser(parse_extension, T___extension__, 25);
4924 register_expression_parser(parse_builtin_classify_type,
4925 T___builtin_classify_type, 25);
4929 * Parse a asm statement constraints specification.
4931 static asm_constraint_t *parse_asm_constraints(void)
4933 asm_constraint_t *result = NULL;
4934 asm_constraint_t *last = NULL;
4936 while(token.type == T_STRING_LITERAL || token.type == '[') {
4937 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4938 memset(constraint, 0, sizeof(constraint[0]));
4940 if(token.type == '[') {
4942 if(token.type != T_IDENTIFIER) {
4943 parse_error_expected("while parsing asm constraint",
4947 constraint->symbol = token.v.symbol;
4952 constraint->constraints = parse_string_literals();
4954 constraint->expression = parse_expression();
4958 last->next = constraint;
4960 result = constraint;
4964 if(token.type != ',')
4973 * Parse a asm statement clobber specification.
4975 static asm_clobber_t *parse_asm_clobbers(void)
4977 asm_clobber_t *result = NULL;
4978 asm_clobber_t *last = NULL;
4980 while(token.type == T_STRING_LITERAL) {
4981 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4982 clobber->clobber = parse_string_literals();
4985 last->next = clobber;
4991 if(token.type != ',')
5000 * Parse an asm statement.
5002 static statement_t *parse_asm_statement(void)
5006 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5007 statement->base.source_position = token.source_position;
5009 asm_statement_t *asm_statement = &statement->asms;
5011 if(token.type == T_volatile) {
5013 asm_statement->is_volatile = true;
5017 asm_statement->asm_text = parse_string_literals();
5019 if(token.type != ':')
5023 asm_statement->inputs = parse_asm_constraints();
5024 if(token.type != ':')
5028 asm_statement->outputs = parse_asm_constraints();
5029 if(token.type != ':')
5033 asm_statement->clobbers = parse_asm_clobbers();
5042 * Parse a case statement.
5044 static statement_t *parse_case_statement(void)
5048 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5050 statement->base.source_position = token.source_position;
5051 statement->case_label.expression = parse_expression();
5055 if (! is_constant_expression(statement->case_label.expression)) {
5056 errorf(statement->base.source_position,
5057 "case label does not reduce to an integer constant");
5059 /* TODO: check if the case label is already known */
5060 if (current_switch != NULL) {
5061 /* link all cases into the switch statement */
5062 if (current_switch->last_case == NULL) {
5063 current_switch->first_case =
5064 current_switch->last_case = &statement->case_label;
5066 current_switch->last_case->next = &statement->case_label;
5069 errorf(statement->base.source_position,
5070 "case label not within a switch statement");
5073 statement->case_label.statement = parse_statement();
5079 * Finds an existing default label of a switch statement.
5081 static case_label_statement_t *
5082 find_default_label(const switch_statement_t *statement)
5084 for (case_label_statement_t *label = statement->first_case;
5086 label = label->next) {
5087 if (label->expression == NULL)
5094 * Parse a default statement.
5096 static statement_t *parse_default_statement(void)
5100 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5102 statement->base.source_position = token.source_position;
5105 if (current_switch != NULL) {
5106 const case_label_statement_t *def_label = find_default_label(current_switch);
5107 if (def_label != NULL) {
5108 errorf(HERE, "multiple default labels in one switch");
5109 errorf(def_label->base.source_position,
5110 "this is the first default label");
5112 /* link all cases into the switch statement */
5113 if (current_switch->last_case == NULL) {
5114 current_switch->first_case =
5115 current_switch->last_case = &statement->case_label;
5117 current_switch->last_case->next = &statement->case_label;
5121 errorf(statement->base.source_position,
5122 "'default' label not within a switch statement");
5124 statement->label.statement = parse_statement();
5130 * Return the declaration for a given label symbol or create a new one.
5132 static declaration_t *get_label(symbol_t *symbol)
5134 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5135 assert(current_function != NULL);
5136 /* if we found a label in the same function, then we already created the
5138 if(candidate != NULL
5139 && candidate->parent_scope == ¤t_function->scope) {
5143 /* otherwise we need to create a new one */
5144 declaration_t *const declaration = allocate_declaration_zero();
5145 declaration->namespc = NAMESPACE_LABEL;
5146 declaration->symbol = symbol;
5148 label_push(declaration);
5154 * Parse a label statement.
5156 static statement_t *parse_label_statement(void)
5158 assert(token.type == T_IDENTIFIER);
5159 symbol_t *symbol = token.v.symbol;
5162 declaration_t *label = get_label(symbol);
5164 /* if source position is already set then the label is defined twice,
5165 * otherwise it was just mentioned in a goto so far */
5166 if(label->source_position.input_name != NULL) {
5167 errorf(HERE, "duplicate label '%Y'", symbol);
5168 errorf(label->source_position, "previous definition of '%Y' was here",
5171 label->source_position = token.source_position;
5174 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5176 statement->base.source_position = token.source_position;
5177 statement->label.label = label;
5181 if(token.type == '}') {
5182 /* TODO only warn? */
5183 errorf(HERE, "label at end of compound statement");
5186 if (token.type == ';') {
5187 /* eat an empty statement here, to avoid the warning about an empty
5188 * after a label. label:; is commonly used to have a label before
5192 statement->label.statement = parse_statement();
5196 /* remember the labels's in a list for later checking */
5197 if (label_last == NULL) {
5198 label_first = &statement->label;
5200 label_last->next = &statement->label;
5202 label_last = &statement->label;
5208 * Parse an if statement.
5210 static statement_t *parse_if(void)
5214 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5215 statement->base.source_position = token.source_position;
5218 statement->ifs.condition = parse_expression();
5221 statement->ifs.true_statement = parse_statement();
5222 if(token.type == T_else) {
5224 statement->ifs.false_statement = parse_statement();
5231 * Parse a switch statement.
5233 static statement_t *parse_switch(void)
5237 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5238 statement->base.source_position = token.source_position;
5241 expression_t *const expr = parse_expression();
5242 type_t * type = skip_typeref(expr->base.type);
5243 if (is_type_integer(type)) {
5244 type = promote_integer(type);
5245 } else if (is_type_valid(type)) {
5246 errorf(expr->base.source_position,
5247 "switch quantity is not an integer, but '%T'", type);
5248 type = type_error_type;
5250 statement->switchs.expression = create_implicit_cast(expr, type);
5253 switch_statement_t *rem = current_switch;
5254 current_switch = &statement->switchs;
5255 statement->switchs.body = parse_statement();
5256 current_switch = rem;
5258 if (warning.switch_default
5259 && find_default_label(&statement->switchs) == NULL) {
5260 warningf(statement->base.source_position, "switch has no default case");
5266 static statement_t *parse_loop_body(statement_t *const loop)
5268 statement_t *const rem = current_loop;
5269 current_loop = loop;
5271 statement_t *const body = parse_statement();
5278 * Parse a while statement.
5280 static statement_t *parse_while(void)
5284 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5285 statement->base.source_position = token.source_position;
5288 statement->whiles.condition = parse_expression();
5291 statement->whiles.body = parse_loop_body(statement);
5297 * Parse a do statement.
5299 static statement_t *parse_do(void)
5303 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5305 statement->base.source_position = token.source_position;
5307 statement->do_while.body = parse_loop_body(statement);
5311 statement->do_while.condition = parse_expression();
5319 * Parse a for statement.
5321 static statement_t *parse_for(void)
5325 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5326 statement->base.source_position = token.source_position;
5330 int top = environment_top();
5331 scope_t *last_scope = scope;
5332 set_scope(&statement->fors.scope);
5334 if(token.type != ';') {
5335 if(is_declaration_specifier(&token, false)) {
5336 parse_declaration(record_declaration);
5338 statement->fors.initialisation = parse_expression();
5345 if(token.type != ';') {
5346 statement->fors.condition = parse_expression();
5349 if(token.type != ')') {
5350 statement->fors.step = parse_expression();
5353 statement->fors.body = parse_loop_body(statement);
5355 assert(scope == &statement->fors.scope);
5356 set_scope(last_scope);
5357 environment_pop_to(top);
5363 * Parse a goto statement.
5365 static statement_t *parse_goto(void)
5369 if(token.type != T_IDENTIFIER) {
5370 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5374 symbol_t *symbol = token.v.symbol;
5377 declaration_t *label = get_label(symbol);
5379 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5380 statement->base.source_position = token.source_position;
5382 statement->gotos.label = label;
5384 /* remember the goto's in a list for later checking */
5385 if (goto_last == NULL) {
5386 goto_first = &statement->gotos;
5388 goto_last->next = &statement->gotos;
5390 goto_last = &statement->gotos;
5398 * Parse a continue statement.
5400 static statement_t *parse_continue(void)
5402 statement_t *statement;
5403 if (current_loop == NULL) {
5404 errorf(HERE, "continue statement not within loop");
5407 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5409 statement->base.source_position = token.source_position;
5419 * Parse a break statement.
5421 static statement_t *parse_break(void)
5423 statement_t *statement;
5424 if (current_switch == NULL && current_loop == NULL) {
5425 errorf(HERE, "break statement not within loop or switch");
5428 statement = allocate_statement_zero(STATEMENT_BREAK);
5430 statement->base.source_position = token.source_position;
5440 * Check if a given declaration represents a local variable.
5442 static bool is_local_var_declaration(const declaration_t *declaration) {
5443 switch ((storage_class_tag_t) declaration->storage_class) {
5444 case STORAGE_CLASS_NONE:
5445 case STORAGE_CLASS_AUTO:
5446 case STORAGE_CLASS_REGISTER: {
5447 const type_t *type = skip_typeref(declaration->type);
5448 if(is_type_function(type)) {
5460 * Check if a given declaration represents a variable.
5462 static bool is_var_declaration(const declaration_t *declaration) {
5463 switch ((storage_class_tag_t) declaration->storage_class) {
5464 case STORAGE_CLASS_NONE:
5465 case STORAGE_CLASS_EXTERN:
5466 case STORAGE_CLASS_STATIC:
5467 case STORAGE_CLASS_AUTO:
5468 case STORAGE_CLASS_REGISTER:
5469 case STORAGE_CLASS_THREAD:
5470 case STORAGE_CLASS_THREAD_EXTERN:
5471 case STORAGE_CLASS_THREAD_STATIC: {
5472 const type_t *type = skip_typeref(declaration->type);
5473 if(is_type_function(type)) {
5485 * Check if a given expression represents a local variable.
5487 static bool is_local_variable(const expression_t *expression)
5489 if (expression->base.kind != EXPR_REFERENCE) {
5492 const declaration_t *declaration = expression->reference.declaration;
5493 return is_local_var_declaration(declaration);
5497 * Check if a given expression represents a local variable and
5498 * return its declaration then, else return NULL.
5500 declaration_t *expr_is_variable(const expression_t *expression)
5502 if (expression->base.kind != EXPR_REFERENCE) {
5505 declaration_t *declaration = expression->reference.declaration;
5506 if (is_var_declaration(declaration))
5512 * Parse a return statement.
5514 static statement_t *parse_return(void)
5518 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5519 statement->base.source_position = token.source_position;
5521 expression_t *return_value = NULL;
5522 if(token.type != ';') {
5523 return_value = parse_expression();
5527 const type_t *const func_type = current_function->type;
5528 assert(is_type_function(func_type));
5529 type_t *const return_type = skip_typeref(func_type->function.return_type);
5531 if(return_value != NULL) {
5532 type_t *return_value_type = skip_typeref(return_value->base.type);
5534 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5535 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5536 warningf(statement->base.source_position,
5537 "'return' with a value, in function returning void");
5538 return_value = NULL;
5540 type_t *const res_type = semantic_assign(return_type,
5541 return_value, "'return'");
5542 if (res_type == NULL) {
5543 errorf(statement->base.source_position,
5544 "cannot return something of type '%T' in function returning '%T'",
5545 return_value->base.type, return_type);
5547 return_value = create_implicit_cast(return_value, res_type);
5550 /* check for returning address of a local var */
5551 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5552 const expression_t *expression = return_value->unary.value;
5553 if (is_local_variable(expression)) {
5554 warningf(statement->base.source_position,
5555 "function returns address of local variable");
5559 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5560 warningf(statement->base.source_position,
5561 "'return' without value, in function returning non-void");
5564 statement->returns.value = return_value;
5570 * Parse a declaration statement.
5572 static statement_t *parse_declaration_statement(void)
5574 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5576 statement->base.source_position = token.source_position;
5578 declaration_t *before = last_declaration;
5579 parse_declaration(record_declaration);
5581 if(before == NULL) {
5582 statement->declaration.declarations_begin = scope->declarations;
5584 statement->declaration.declarations_begin = before->next;
5586 statement->declaration.declarations_end = last_declaration;
5592 * Parse an expression statement, ie. expr ';'.
5594 static statement_t *parse_expression_statement(void)
5596 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5598 statement->base.source_position = token.source_position;
5599 expression_t *const expr = parse_expression();
5600 statement->expression.expression = expr;
5602 if (warning.unused_value && !expression_has_effect(expr)) {
5603 warningf(expr->base.source_position, "statement has no effect");
5612 * Parse a statement.
5614 static statement_t *parse_statement(void)
5616 statement_t *statement = NULL;
5618 /* declaration or statement */
5619 switch(token.type) {
5621 statement = parse_asm_statement();
5625 statement = parse_case_statement();
5629 statement = parse_default_statement();
5633 statement = parse_compound_statement();
5637 statement = parse_if();
5641 statement = parse_switch();
5645 statement = parse_while();
5649 statement = parse_do();
5653 statement = parse_for();
5657 statement = parse_goto();
5661 statement = parse_continue();
5665 statement = parse_break();
5669 statement = parse_return();
5673 if (warning.empty_statement) {
5674 warningf(HERE, "statement is empty");
5681 if(look_ahead(1)->type == ':') {
5682 statement = parse_label_statement();
5686 if(is_typedef_symbol(token.v.symbol)) {
5687 statement = parse_declaration_statement();
5691 statement = parse_expression_statement();
5694 case T___extension__:
5695 /* this can be a prefix to a declaration or an expression statement */
5696 /* we simply eat it now and parse the rest with tail recursion */
5699 } while(token.type == T___extension__);
5700 statement = parse_statement();
5704 statement = parse_declaration_statement();
5708 statement = parse_expression_statement();
5712 assert(statement == NULL
5713 || statement->base.source_position.input_name != NULL);
5719 * Parse a compound statement.
5721 static statement_t *parse_compound_statement(void)
5723 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5725 statement->base.source_position = token.source_position;
5729 int top = environment_top();
5730 scope_t *last_scope = scope;
5731 set_scope(&statement->compound.scope);
5733 statement_t *last_statement = NULL;
5735 while(token.type != '}' && token.type != T_EOF) {
5736 statement_t *sub_statement = parse_statement();
5737 if(sub_statement == NULL)
5740 if(last_statement != NULL) {
5741 last_statement->base.next = sub_statement;
5743 statement->compound.statements = sub_statement;
5746 while(sub_statement->base.next != NULL)
5747 sub_statement = sub_statement->base.next;
5749 last_statement = sub_statement;
5752 if(token.type == '}') {
5755 errorf(statement->base.source_position,
5756 "end of file while looking for closing '}'");
5759 assert(scope == &statement->compound.scope);
5760 set_scope(last_scope);
5761 environment_pop_to(top);
5767 * Initialize builtin types.
5769 static void initialize_builtin_types(void)
5771 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5772 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5773 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5774 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5775 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5776 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5777 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5778 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5780 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5781 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5782 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5783 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5787 * Check for unused global static functions and variables
5789 static void check_unused_globals(void)
5791 if (!warning.unused_function && !warning.unused_variable)
5794 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5795 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5798 type_t *const type = decl->type;
5800 if (is_type_function(skip_typeref(type))) {
5801 if (!warning.unused_function || decl->is_inline)
5804 s = (decl->init.statement != NULL ? "defined" : "declared");
5806 if (!warning.unused_variable)
5812 warningf(decl->source_position, "'%#T' %s but not used",
5813 type, decl->symbol, s);
5818 * Parse a translation unit.
5820 static translation_unit_t *parse_translation_unit(void)
5822 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5824 assert(global_scope == NULL);
5825 global_scope = &unit->scope;
5827 assert(scope == NULL);
5828 set_scope(&unit->scope);
5830 initialize_builtin_types();
5832 while(token.type != T_EOF) {
5833 if (token.type == ';') {
5834 /* TODO error in strict mode */
5835 warningf(HERE, "stray ';' outside of function");
5838 parse_external_declaration();
5842 assert(scope == &unit->scope);
5844 last_declaration = NULL;
5846 assert(global_scope == &unit->scope);
5847 check_unused_globals();
5848 global_scope = NULL;
5856 * @return the translation unit or NULL if errors occurred.
5858 translation_unit_t *parse(void)
5860 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5861 label_stack = NEW_ARR_F(stack_entry_t, 0);
5862 diagnostic_count = 0;
5866 type_set_output(stderr);
5867 ast_set_output(stderr);
5869 lookahead_bufpos = 0;
5870 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5873 translation_unit_t *unit = parse_translation_unit();
5875 DEL_ARR_F(environment_stack);
5876 DEL_ARR_F(label_stack);
5885 * Initialize the parser.
5887 void init_parser(void)
5889 init_expression_parsers();
5890 obstack_init(&temp_obst);
5892 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5893 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5897 * Terminate the parser.
5899 void exit_parser(void)
5901 obstack_free(&temp_obst, NULL);