7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(declaration_t *compound_declaration);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_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 definitions of '%s %Y'",
1284 is_struct ? "struct" : "union", symbol);
1285 declaration->scope.declarations = NULL;
1287 declaration->init.is_defined = true;
1289 parse_compound_type_entries(declaration);
1296 static void parse_enum_entries(type_t *const enum_type)
1300 if(token.type == '}') {
1302 errorf(HERE, "empty enum not allowed");
1307 if(token.type != T_IDENTIFIER) {
1308 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1313 declaration_t *const entry = allocate_declaration_zero();
1314 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1315 entry->type = enum_type;
1316 entry->symbol = token.v.symbol;
1317 entry->source_position = token.source_position;
1320 if(token.type == '=') {
1322 entry->init.enum_value = parse_constant_expression();
1327 record_declaration(entry);
1329 if(token.type != ',')
1332 } while(token.type != '}');
1337 static type_t *parse_enum_specifier(void)
1341 declaration_t *declaration;
1344 if(token.type == T_IDENTIFIER) {
1345 symbol = token.v.symbol;
1348 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1349 } else if(token.type != '{') {
1350 parse_error_expected("while parsing enum type specifier",
1351 T_IDENTIFIER, '{', 0);
1358 if(declaration == NULL) {
1359 declaration = allocate_declaration_zero();
1360 declaration->namespc = NAMESPACE_ENUM;
1361 declaration->source_position = token.source_position;
1362 declaration->symbol = symbol;
1363 declaration->parent_scope = scope;
1366 type_t *const type = allocate_type_zero(TYPE_ENUM);
1367 type->enumt.declaration = declaration;
1369 if(token.type == '{') {
1370 if(declaration->init.is_defined) {
1371 errorf(HERE, "multiple definitions of enum %Y", symbol);
1373 if (symbol != NULL) {
1374 environment_push(declaration);
1376 append_declaration(declaration);
1377 declaration->init.is_defined = 1;
1379 parse_enum_entries(type);
1387 * if a symbol is a typedef to another type, return true
1389 static bool is_typedef_symbol(symbol_t *symbol)
1391 const declaration_t *const declaration =
1392 get_declaration(symbol, NAMESPACE_NORMAL);
1394 declaration != NULL &&
1395 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1398 static type_t *parse_typeof(void)
1406 expression_t *expression = NULL;
1409 switch(token.type) {
1410 case T___extension__:
1411 /* this can be a prefix to a typename or an expression */
1412 /* we simply eat it now. */
1415 } while(token.type == T___extension__);
1419 if(is_typedef_symbol(token.v.symbol)) {
1420 type = parse_typename();
1422 expression = parse_expression();
1423 type = expression->base.type;
1428 type = parse_typename();
1432 expression = parse_expression();
1433 type = expression->base.type;
1439 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1440 typeof_type->typeoft.expression = expression;
1441 typeof_type->typeoft.typeof_type = type;
1447 SPECIFIER_SIGNED = 1 << 0,
1448 SPECIFIER_UNSIGNED = 1 << 1,
1449 SPECIFIER_LONG = 1 << 2,
1450 SPECIFIER_INT = 1 << 3,
1451 SPECIFIER_DOUBLE = 1 << 4,
1452 SPECIFIER_CHAR = 1 << 5,
1453 SPECIFIER_SHORT = 1 << 6,
1454 SPECIFIER_LONG_LONG = 1 << 7,
1455 SPECIFIER_FLOAT = 1 << 8,
1456 SPECIFIER_BOOL = 1 << 9,
1457 SPECIFIER_VOID = 1 << 10,
1458 #ifdef PROVIDE_COMPLEX
1459 SPECIFIER_COMPLEX = 1 << 11,
1460 SPECIFIER_IMAGINARY = 1 << 12,
1464 static type_t *create_builtin_type(symbol_t *const symbol,
1465 type_t *const real_type)
1467 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1468 type->builtin.symbol = symbol;
1469 type->builtin.real_type = real_type;
1471 type_t *result = typehash_insert(type);
1472 if (type != result) {
1479 static type_t *get_typedef_type(symbol_t *symbol)
1481 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1482 if(declaration == NULL
1483 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1486 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1487 type->typedeft.declaration = declaration;
1492 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1494 type_t *type = NULL;
1495 unsigned type_qualifiers = 0;
1496 unsigned type_specifiers = 0;
1499 specifiers->source_position = token.source_position;
1502 switch(token.type) {
1505 #define MATCH_STORAGE_CLASS(token, class) \
1507 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1508 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1510 specifiers->storage_class = class; \
1514 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1515 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1516 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1517 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1518 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1521 switch (specifiers->storage_class) {
1522 case STORAGE_CLASS_NONE:
1523 specifiers->storage_class = STORAGE_CLASS_THREAD;
1526 case STORAGE_CLASS_EXTERN:
1527 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1530 case STORAGE_CLASS_STATIC:
1531 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1535 errorf(HERE, "multiple storage classes in declaration specifiers");
1541 /* type qualifiers */
1542 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1544 type_qualifiers |= qualifier; \
1548 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1549 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1550 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1552 case T___extension__:
1557 /* type specifiers */
1558 #define MATCH_SPECIFIER(token, specifier, name) \
1561 if(type_specifiers & specifier) { \
1562 errorf(HERE, "multiple " name " type specifiers given"); \
1564 type_specifiers |= specifier; \
1568 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1569 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1570 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1571 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1572 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1573 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1574 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1575 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1576 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1577 #ifdef PROVIDE_COMPLEX
1578 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1579 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1582 /* only in microsoft mode */
1583 specifiers->decl_modifiers |= DM_FORCEINLINE;
1587 specifiers->is_inline = true;
1592 if(type_specifiers & SPECIFIER_LONG_LONG) {
1593 errorf(HERE, "multiple type specifiers given");
1594 } else if(type_specifiers & SPECIFIER_LONG) {
1595 type_specifiers |= SPECIFIER_LONG_LONG;
1597 type_specifiers |= SPECIFIER_LONG;
1601 /* TODO: if is_type_valid(type) for the following rules should issue
1604 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1606 type->compound.declaration = parse_compound_type_specifier(true);
1610 type = allocate_type_zero(TYPE_COMPOUND_UNION);
1612 type->compound.declaration = parse_compound_type_specifier(false);
1616 type = parse_enum_specifier();
1619 type = parse_typeof();
1621 case T___builtin_va_list:
1622 type = duplicate_type(type_valist);
1626 case T___attribute__:
1631 case T_IDENTIFIER: {
1632 type_t *typedef_type = get_typedef_type(token.v.symbol);
1634 if(typedef_type == NULL)
1635 goto finish_specifiers;
1638 type = typedef_type;
1642 /* function specifier */
1644 goto finish_specifiers;
1651 atomic_type_kind_t atomic_type;
1653 /* match valid basic types */
1654 switch(type_specifiers) {
1655 case SPECIFIER_VOID:
1656 atomic_type = ATOMIC_TYPE_VOID;
1658 case SPECIFIER_CHAR:
1659 atomic_type = ATOMIC_TYPE_CHAR;
1661 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1662 atomic_type = ATOMIC_TYPE_SCHAR;
1664 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1665 atomic_type = ATOMIC_TYPE_UCHAR;
1667 case SPECIFIER_SHORT:
1668 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1669 case SPECIFIER_SHORT | SPECIFIER_INT:
1670 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1671 atomic_type = ATOMIC_TYPE_SHORT;
1673 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1674 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1675 atomic_type = ATOMIC_TYPE_USHORT;
1678 case SPECIFIER_SIGNED:
1679 case SPECIFIER_SIGNED | SPECIFIER_INT:
1680 atomic_type = ATOMIC_TYPE_INT;
1682 case SPECIFIER_UNSIGNED:
1683 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1684 atomic_type = ATOMIC_TYPE_UINT;
1686 case SPECIFIER_LONG:
1687 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1688 case SPECIFIER_LONG | SPECIFIER_INT:
1689 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1690 atomic_type = ATOMIC_TYPE_LONG;
1692 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1693 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1694 atomic_type = ATOMIC_TYPE_ULONG;
1696 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1697 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1698 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1699 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1701 atomic_type = ATOMIC_TYPE_LONGLONG;
1703 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1704 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1706 atomic_type = ATOMIC_TYPE_ULONGLONG;
1708 case SPECIFIER_FLOAT:
1709 atomic_type = ATOMIC_TYPE_FLOAT;
1711 case SPECIFIER_DOUBLE:
1712 atomic_type = ATOMIC_TYPE_DOUBLE;
1714 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1715 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1717 case SPECIFIER_BOOL:
1718 atomic_type = ATOMIC_TYPE_BOOL;
1720 #ifdef PROVIDE_COMPLEX
1721 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1722 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1724 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1725 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1727 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1728 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1730 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1731 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1733 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1734 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1736 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1737 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1741 /* invalid specifier combination, give an error message */
1742 if(type_specifiers == 0) {
1743 if (! strict_mode) {
1744 if (warning.implicit_int) {
1745 warningf(HERE, "no type specifiers in declaration, using 'int'");
1747 atomic_type = ATOMIC_TYPE_INT;
1750 errorf(HERE, "no type specifiers given in declaration");
1752 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1753 (type_specifiers & SPECIFIER_UNSIGNED)) {
1754 errorf(HERE, "signed and unsigned specifiers gives");
1755 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1756 errorf(HERE, "only integer types can be signed or unsigned");
1758 errorf(HERE, "multiple datatypes in declaration");
1760 atomic_type = ATOMIC_TYPE_INVALID;
1763 type = allocate_type_zero(TYPE_ATOMIC);
1764 type->atomic.akind = atomic_type;
1767 if(type_specifiers != 0) {
1768 errorf(HERE, "multiple datatypes in declaration");
1772 type->base.qualifiers = type_qualifiers;
1774 type_t *result = typehash_insert(type);
1775 if(newtype && result != type) {
1779 specifiers->type = result;
1782 static type_qualifiers_t parse_type_qualifiers(void)
1784 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1787 switch(token.type) {
1788 /* type qualifiers */
1789 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1790 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1791 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1794 return type_qualifiers;
1799 static declaration_t *parse_identifier_list(void)
1801 declaration_t *declarations = NULL;
1802 declaration_t *last_declaration = NULL;
1804 declaration_t *const declaration = allocate_declaration_zero();
1805 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1806 declaration->source_position = token.source_position;
1807 declaration->symbol = token.v.symbol;
1810 if(last_declaration != NULL) {
1811 last_declaration->next = declaration;
1813 declarations = declaration;
1815 last_declaration = declaration;
1817 if(token.type != ',')
1820 } while(token.type == T_IDENTIFIER);
1822 return declarations;
1825 static void semantic_parameter(declaration_t *declaration)
1827 /* TODO: improve error messages */
1829 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1830 errorf(HERE, "typedef not allowed in parameter list");
1831 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1832 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1833 errorf(HERE, "parameter may only have none or register storage class");
1836 type_t *const orig_type = declaration->type;
1837 type_t * type = skip_typeref(orig_type);
1839 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1840 * into a pointer. § 6.7.5.3 (7) */
1841 if (is_type_array(type)) {
1842 type_t *const element_type = type->array.element_type;
1844 type = make_pointer_type(element_type, type->base.qualifiers);
1846 declaration->type = type;
1849 if(is_type_incomplete(type)) {
1850 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1851 orig_type, declaration->symbol);
1855 static declaration_t *parse_parameter(void)
1857 declaration_specifiers_t specifiers;
1858 memset(&specifiers, 0, sizeof(specifiers));
1860 parse_declaration_specifiers(&specifiers);
1862 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1864 semantic_parameter(declaration);
1869 static declaration_t *parse_parameters(function_type_t *type)
1871 if(token.type == T_IDENTIFIER) {
1872 symbol_t *symbol = token.v.symbol;
1873 if(!is_typedef_symbol(symbol)) {
1874 type->kr_style_parameters = true;
1875 return parse_identifier_list();
1879 if(token.type == ')') {
1880 type->unspecified_parameters = 1;
1883 if(token.type == T_void && look_ahead(1)->type == ')') {
1888 declaration_t *declarations = NULL;
1889 declaration_t *declaration;
1890 declaration_t *last_declaration = NULL;
1891 function_parameter_t *parameter;
1892 function_parameter_t *last_parameter = NULL;
1895 switch(token.type) {
1899 return declarations;
1902 case T___extension__:
1904 declaration = parse_parameter();
1906 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1907 memset(parameter, 0, sizeof(parameter[0]));
1908 parameter->type = declaration->type;
1910 if(last_parameter != NULL) {
1911 last_declaration->next = declaration;
1912 last_parameter->next = parameter;
1914 type->parameters = parameter;
1915 declarations = declaration;
1917 last_parameter = parameter;
1918 last_declaration = declaration;
1922 return declarations;
1924 if(token.type != ',')
1925 return declarations;
1935 } construct_type_type_t;
1937 typedef struct construct_type_t construct_type_t;
1938 struct construct_type_t {
1939 construct_type_type_t type;
1940 construct_type_t *next;
1943 typedef struct parsed_pointer_t parsed_pointer_t;
1944 struct parsed_pointer_t {
1945 construct_type_t construct_type;
1946 type_qualifiers_t type_qualifiers;
1949 typedef struct construct_function_type_t construct_function_type_t;
1950 struct construct_function_type_t {
1951 construct_type_t construct_type;
1952 type_t *function_type;
1955 typedef struct parsed_array_t parsed_array_t;
1956 struct parsed_array_t {
1957 construct_type_t construct_type;
1958 type_qualifiers_t type_qualifiers;
1964 typedef struct construct_base_type_t construct_base_type_t;
1965 struct construct_base_type_t {
1966 construct_type_t construct_type;
1970 static construct_type_t *parse_pointer_declarator(void)
1974 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1975 memset(pointer, 0, sizeof(pointer[0]));
1976 pointer->construct_type.type = CONSTRUCT_POINTER;
1977 pointer->type_qualifiers = parse_type_qualifiers();
1979 return (construct_type_t*) pointer;
1982 static construct_type_t *parse_array_declarator(void)
1986 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1987 memset(array, 0, sizeof(array[0]));
1988 array->construct_type.type = CONSTRUCT_ARRAY;
1990 if(token.type == T_static) {
1991 array->is_static = true;
1995 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1996 if(type_qualifiers != 0) {
1997 if(token.type == T_static) {
1998 array->is_static = true;
2002 array->type_qualifiers = type_qualifiers;
2004 if(token.type == '*' && look_ahead(1)->type == ']') {
2005 array->is_variable = true;
2007 } else if(token.type != ']') {
2008 array->size = parse_assignment_expression();
2013 return (construct_type_t*) array;
2016 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2020 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2022 declaration_t *parameters = parse_parameters(&type->function);
2023 if(declaration != NULL) {
2024 declaration->scope.declarations = parameters;
2027 construct_function_type_t *construct_function_type =
2028 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2029 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2030 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2031 construct_function_type->function_type = type;
2035 return (construct_type_t*) construct_function_type;
2038 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2039 bool may_be_abstract)
2041 /* construct a single linked list of construct_type_t's which describe
2042 * how to construct the final declarator type */
2043 construct_type_t *first = NULL;
2044 construct_type_t *last = NULL;
2047 while(token.type == '*') {
2048 construct_type_t *type = parse_pointer_declarator();
2059 /* TODO: find out if this is correct */
2062 construct_type_t *inner_types = NULL;
2064 switch(token.type) {
2066 if(declaration == NULL) {
2067 errorf(HERE, "no identifier expected in typename");
2069 declaration->symbol = token.v.symbol;
2070 declaration->source_position = token.source_position;
2076 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2082 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2083 /* avoid a loop in the outermost scope, because eat_statement doesn't
2085 if(token.type == '}' && current_function == NULL) {
2093 construct_type_t *p = last;
2096 construct_type_t *type;
2097 switch(token.type) {
2099 type = parse_function_declarator(declaration);
2102 type = parse_array_declarator();
2105 goto declarator_finished;
2108 /* insert in the middle of the list (behind p) */
2110 type->next = p->next;
2121 declarator_finished:
2124 /* append inner_types at the end of the list, we don't to set last anymore
2125 * as it's not needed anymore */
2127 assert(first == NULL);
2128 first = inner_types;
2130 last->next = inner_types;
2136 static type_t *construct_declarator_type(construct_type_t *construct_list,
2139 construct_type_t *iter = construct_list;
2140 for( ; iter != NULL; iter = iter->next) {
2141 switch(iter->type) {
2142 case CONSTRUCT_INVALID:
2143 panic("invalid type construction found");
2144 case CONSTRUCT_FUNCTION: {
2145 construct_function_type_t *construct_function_type
2146 = (construct_function_type_t*) iter;
2148 type_t *function_type = construct_function_type->function_type;
2150 function_type->function.return_type = type;
2152 type_t *skipped_return_type = skip_typeref(type);
2153 if (is_type_function(skipped_return_type)) {
2154 errorf(HERE, "function returning function is not allowed");
2155 type = type_error_type;
2156 } else if (is_type_array(skipped_return_type)) {
2157 errorf(HERE, "function returning array is not allowed");
2158 type = type_error_type;
2160 type = function_type;
2165 case CONSTRUCT_POINTER: {
2166 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2167 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2168 pointer_type->pointer.points_to = type;
2169 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2171 type = pointer_type;
2175 case CONSTRUCT_ARRAY: {
2176 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2177 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2179 array_type->base.qualifiers = parsed_array->type_qualifiers;
2180 array_type->array.element_type = type;
2181 array_type->array.is_static = parsed_array->is_static;
2182 array_type->array.is_variable = parsed_array->is_variable;
2183 array_type->array.size = parsed_array->size;
2185 type_t *skipped_type = skip_typeref(type);
2186 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2187 errorf(HERE, "array of void is not allowed");
2188 type = type_error_type;
2196 type_t *hashed_type = typehash_insert(type);
2197 if(hashed_type != type) {
2198 /* the function type was constructed earlier freeing it here will
2199 * destroy other types... */
2200 if(iter->type != CONSTRUCT_FUNCTION) {
2210 static declaration_t *parse_declarator(
2211 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2213 declaration_t *const declaration = allocate_declaration_zero();
2214 declaration->storage_class = specifiers->storage_class;
2215 declaration->modifiers = specifiers->decl_modifiers;
2216 declaration->is_inline = specifiers->is_inline;
2218 construct_type_t *construct_type
2219 = parse_inner_declarator(declaration, may_be_abstract);
2220 type_t *const type = specifiers->type;
2221 declaration->type = construct_declarator_type(construct_type, type);
2223 if(construct_type != NULL) {
2224 obstack_free(&temp_obst, construct_type);
2230 static type_t *parse_abstract_declarator(type_t *base_type)
2232 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2234 type_t *result = construct_declarator_type(construct_type, base_type);
2235 if(construct_type != NULL) {
2236 obstack_free(&temp_obst, construct_type);
2242 static declaration_t *append_declaration(declaration_t* const declaration)
2244 if (last_declaration != NULL) {
2245 last_declaration->next = declaration;
2247 scope->declarations = declaration;
2249 last_declaration = declaration;
2254 * Check if the declaration of main is suspicious. main should be a
2255 * function with external linkage, returning int, taking either zero
2256 * arguments, two, or three arguments of appropriate types, ie.
2258 * int main([ int argc, char **argv [, char **env ] ]).
2260 * @param decl the declaration to check
2261 * @param type the function type of the declaration
2263 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2265 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2266 warningf(decl->source_position, "'main' is normally a non-static function");
2268 if (skip_typeref(func_type->return_type) != type_int) {
2269 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2271 const function_parameter_t *parm = func_type->parameters;
2273 type_t *const first_type = parm->type;
2274 if (!types_compatible(skip_typeref(first_type), type_int)) {
2275 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2279 type_t *const second_type = parm->type;
2280 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2281 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2285 type_t *const third_type = parm->type;
2286 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2287 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2291 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2295 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2301 * Check if a symbol is the equal to "main".
2303 static bool is_sym_main(const symbol_t *const sym)
2305 return strcmp(sym->string, "main") == 0;
2308 static declaration_t *internal_record_declaration(
2309 declaration_t *const declaration,
2310 const bool is_function_definition)
2312 const symbol_t *const symbol = declaration->symbol;
2313 const namespace_t namespc = (namespace_t)declaration->namespc;
2315 type_t *const orig_type = declaration->type;
2316 type_t *const type = skip_typeref(orig_type);
2317 if (is_type_function(type) &&
2318 type->function.unspecified_parameters &&
2319 warning.strict_prototypes) {
2320 warningf(declaration->source_position,
2321 "function declaration '%#T' is not a prototype",
2322 orig_type, declaration->symbol);
2325 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2326 check_type_of_main(declaration, &type->function);
2329 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2330 assert(declaration != previous_declaration);
2331 if (previous_declaration != NULL) {
2332 if (previous_declaration->parent_scope == scope) {
2333 /* can happen for K&R style declarations */
2334 if(previous_declaration->type == NULL) {
2335 previous_declaration->type = declaration->type;
2338 const type_t *prev_type = skip_typeref(previous_declaration->type);
2339 if (!types_compatible(type, prev_type)) {
2340 errorf(declaration->source_position,
2341 "declaration '%#T' is incompatible with "
2342 "previous declaration '%#T'",
2343 orig_type, symbol, previous_declaration->type, symbol);
2344 errorf(previous_declaration->source_position,
2345 "previous declaration of '%Y' was here", symbol);
2347 unsigned old_storage_class
2348 = previous_declaration->storage_class;
2349 unsigned new_storage_class = declaration->storage_class;
2351 if(is_type_incomplete(prev_type)) {
2352 previous_declaration->type = type;
2356 /* pretend no storage class means extern for function
2357 * declarations (except if the previous declaration is neither
2358 * none nor extern) */
2359 if (is_type_function(type)) {
2360 switch (old_storage_class) {
2361 case STORAGE_CLASS_NONE:
2362 old_storage_class = STORAGE_CLASS_EXTERN;
2364 case STORAGE_CLASS_EXTERN:
2365 if (is_function_definition) {
2366 if (warning.missing_prototypes &&
2367 prev_type->function.unspecified_parameters &&
2368 !is_sym_main(symbol)) {
2369 warningf(declaration->source_position,
2370 "no previous prototype for '%#T'",
2373 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2374 new_storage_class = STORAGE_CLASS_EXTERN;
2382 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2383 new_storage_class == STORAGE_CLASS_EXTERN) {
2384 warn_redundant_declaration:
2385 if (warning.redundant_decls) {
2386 warningf(declaration->source_position,
2387 "redundant declaration for '%Y'", symbol);
2388 warningf(previous_declaration->source_position,
2389 "previous declaration of '%Y' was here",
2392 } else if (current_function == NULL) {
2393 if (old_storage_class != STORAGE_CLASS_STATIC &&
2394 new_storage_class == STORAGE_CLASS_STATIC) {
2395 errorf(declaration->source_position,
2396 "static declaration of '%Y' follows non-static declaration",
2398 errorf(previous_declaration->source_position,
2399 "previous declaration of '%Y' was here", symbol);
2401 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2402 goto warn_redundant_declaration;
2404 if (new_storage_class == STORAGE_CLASS_NONE) {
2405 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2409 if (old_storage_class == new_storage_class) {
2410 errorf(declaration->source_position,
2411 "redeclaration of '%Y'", symbol);
2413 errorf(declaration->source_position,
2414 "redeclaration of '%Y' with different linkage",
2417 errorf(previous_declaration->source_position,
2418 "previous declaration of '%Y' was here", symbol);
2421 return previous_declaration;
2423 } else if (is_function_definition) {
2424 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2425 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2426 warningf(declaration->source_position,
2427 "no previous prototype for '%#T'", orig_type, symbol);
2428 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2429 warningf(declaration->source_position,
2430 "no previous declaration for '%#T'", orig_type,
2434 } else if (warning.missing_declarations &&
2435 scope == global_scope &&
2436 !is_type_function(type) && (
2437 declaration->storage_class == STORAGE_CLASS_NONE ||
2438 declaration->storage_class == STORAGE_CLASS_THREAD
2440 warningf(declaration->source_position,
2441 "no previous declaration for '%#T'", orig_type, symbol);
2444 assert(declaration->parent_scope == NULL);
2445 assert(declaration->symbol != NULL);
2446 assert(scope != NULL);
2448 declaration->parent_scope = scope;
2450 environment_push(declaration);
2451 return append_declaration(declaration);
2454 static declaration_t *record_declaration(declaration_t *declaration)
2456 return internal_record_declaration(declaration, false);
2459 static declaration_t *record_function_definition(declaration_t *declaration)
2461 return internal_record_declaration(declaration, true);
2464 static void parser_error_multiple_definition(declaration_t *declaration,
2465 const source_position_t source_position)
2467 errorf(source_position, "multiple definition of symbol '%Y'",
2468 declaration->symbol);
2469 errorf(declaration->source_position,
2470 "this is the location of the previous definition.");
2473 static bool is_declaration_specifier(const token_t *token,
2474 bool only_type_specifiers)
2476 switch(token->type) {
2480 return is_typedef_symbol(token->v.symbol);
2482 case T___extension__:
2485 return !only_type_specifiers;
2492 static void parse_init_declarator_rest(declaration_t *declaration)
2496 type_t *orig_type = declaration->type;
2497 type_t *type = type = skip_typeref(orig_type);
2499 if(declaration->init.initializer != NULL) {
2500 parser_error_multiple_definition(declaration, token.source_position);
2503 initializer_t *initializer = parse_initializer(type);
2505 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2506 * the array type size */
2507 if(is_type_array(type) && initializer != NULL) {
2508 array_type_t *array_type = &type->array;
2510 if(array_type->size == NULL) {
2511 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2513 cnst->base.type = type_size_t;
2515 switch (initializer->kind) {
2516 case INITIALIZER_LIST: {
2517 cnst->conste.v.int_value = initializer->list.len;
2521 case INITIALIZER_STRING: {
2522 cnst->conste.v.int_value = initializer->string.string.size;
2526 case INITIALIZER_WIDE_STRING: {
2527 cnst->conste.v.int_value = initializer->wide_string.string.size;
2532 panic("invalid initializer type");
2535 array_type->size = cnst;
2539 if(is_type_function(type)) {
2540 errorf(declaration->source_position,
2541 "initializers not allowed for function types at declator '%Y' (type '%T')",
2542 declaration->symbol, orig_type);
2544 declaration->init.initializer = initializer;
2548 /* parse rest of a declaration without any declarator */
2549 static void parse_anonymous_declaration_rest(
2550 const declaration_specifiers_t *specifiers,
2551 parsed_declaration_func finished_declaration)
2555 declaration_t *const declaration = allocate_declaration_zero();
2556 declaration->type = specifiers->type;
2557 declaration->storage_class = specifiers->storage_class;
2558 declaration->source_position = specifiers->source_position;
2560 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2561 warningf(declaration->source_position, "useless storage class in empty declaration");
2564 type_t *type = declaration->type;
2565 switch (type->kind) {
2566 case TYPE_COMPOUND_STRUCT:
2567 case TYPE_COMPOUND_UNION: {
2568 if (type->compound.declaration->symbol == NULL) {
2569 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2578 warningf(declaration->source_position, "empty declaration");
2582 finished_declaration(declaration);
2585 static void parse_declaration_rest(declaration_t *ndeclaration,
2586 const declaration_specifiers_t *specifiers,
2587 parsed_declaration_func finished_declaration)
2590 declaration_t *declaration = finished_declaration(ndeclaration);
2592 type_t *orig_type = declaration->type;
2593 type_t *type = skip_typeref(orig_type);
2595 if (type->kind != TYPE_FUNCTION &&
2596 declaration->is_inline &&
2597 is_type_valid(type)) {
2598 warningf(declaration->source_position,
2599 "variable '%Y' declared 'inline'\n", declaration->symbol);
2602 if(token.type == '=') {
2603 parse_init_declarator_rest(declaration);
2606 if(token.type != ',')
2610 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2615 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2617 symbol_t *symbol = declaration->symbol;
2618 if(symbol == NULL) {
2619 errorf(HERE, "anonymous declaration not valid as function parameter");
2622 namespace_t namespc = (namespace_t) declaration->namespc;
2623 if(namespc != NAMESPACE_NORMAL) {
2624 return record_declaration(declaration);
2627 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2628 if(previous_declaration == NULL ||
2629 previous_declaration->parent_scope != scope) {
2630 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2635 if(previous_declaration->type == NULL) {
2636 previous_declaration->type = declaration->type;
2637 previous_declaration->storage_class = declaration->storage_class;
2638 previous_declaration->parent_scope = scope;
2639 return previous_declaration;
2641 return record_declaration(declaration);
2645 static void parse_declaration(parsed_declaration_func finished_declaration)
2647 declaration_specifiers_t specifiers;
2648 memset(&specifiers, 0, sizeof(specifiers));
2649 parse_declaration_specifiers(&specifiers);
2651 if(token.type == ';') {
2652 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2654 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2655 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2659 static void parse_kr_declaration_list(declaration_t *declaration)
2661 type_t *type = skip_typeref(declaration->type);
2662 if(!is_type_function(type))
2665 if(!type->function.kr_style_parameters)
2668 /* push function parameters */
2669 int top = environment_top();
2670 scope_t *last_scope = scope;
2671 set_scope(&declaration->scope);
2673 declaration_t *parameter = declaration->scope.declarations;
2674 for( ; parameter != NULL; parameter = parameter->next) {
2675 assert(parameter->parent_scope == NULL);
2676 parameter->parent_scope = scope;
2677 environment_push(parameter);
2680 /* parse declaration list */
2681 while(is_declaration_specifier(&token, false)) {
2682 parse_declaration(finished_kr_declaration);
2685 /* pop function parameters */
2686 assert(scope == &declaration->scope);
2687 set_scope(last_scope);
2688 environment_pop_to(top);
2690 /* update function type */
2691 type_t *new_type = duplicate_type(type);
2692 new_type->function.kr_style_parameters = false;
2694 function_parameter_t *parameters = NULL;
2695 function_parameter_t *last_parameter = NULL;
2697 declaration_t *parameter_declaration = declaration->scope.declarations;
2698 for( ; parameter_declaration != NULL;
2699 parameter_declaration = parameter_declaration->next) {
2700 type_t *parameter_type = parameter_declaration->type;
2701 if(parameter_type == NULL) {
2703 errorf(HERE, "no type specified for function parameter '%Y'",
2704 parameter_declaration->symbol);
2706 if (warning.implicit_int) {
2707 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2708 parameter_declaration->symbol);
2710 parameter_type = type_int;
2711 parameter_declaration->type = parameter_type;
2715 semantic_parameter(parameter_declaration);
2716 parameter_type = parameter_declaration->type;
2718 function_parameter_t *function_parameter
2719 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2720 memset(function_parameter, 0, sizeof(function_parameter[0]));
2722 function_parameter->type = parameter_type;
2723 if(last_parameter != NULL) {
2724 last_parameter->next = function_parameter;
2726 parameters = function_parameter;
2728 last_parameter = function_parameter;
2730 new_type->function.parameters = parameters;
2732 type = typehash_insert(new_type);
2733 if(type != new_type) {
2734 obstack_free(type_obst, new_type);
2737 declaration->type = type;
2740 static bool first_err = true;
2743 * When called with first_err set, prints the name of the current function,
2746 static void print_in_function(void) {
2749 diagnosticf("%s: In function '%Y':\n",
2750 current_function->source_position.input_name,
2751 current_function->symbol);
2756 * Check if all labels are defined in the current function.
2757 * Check if all labels are used in the current function.
2759 static void check_labels(void)
2761 for (const goto_statement_t *goto_statement = goto_first;
2762 goto_statement != NULL;
2763 goto_statement = goto_statement->next) {
2764 declaration_t *label = goto_statement->label;
2767 if (label->source_position.input_name == NULL) {
2768 print_in_function();
2769 errorf(goto_statement->base.source_position,
2770 "label '%Y' used but not defined", label->symbol);
2773 goto_first = goto_last = NULL;
2775 if (warning.unused_label) {
2776 for (const label_statement_t *label_statement = label_first;
2777 label_statement != NULL;
2778 label_statement = label_statement->next) {
2779 const declaration_t *label = label_statement->label;
2781 if (! label->used) {
2782 print_in_function();
2783 warningf(label_statement->base.source_position,
2784 "label '%Y' defined but not used", label->symbol);
2788 label_first = label_last = NULL;
2792 * Check declarations of current_function for unused entities.
2794 static void check_declarations(void)
2796 if (warning.unused_parameter) {
2797 const scope_t *scope = ¤t_function->scope;
2799 const declaration_t *parameter = scope->declarations;
2800 for (; parameter != NULL; parameter = parameter->next) {
2801 if (! parameter->used) {
2802 print_in_function();
2803 warningf(parameter->source_position,
2804 "unused parameter '%Y'", parameter->symbol);
2808 if (warning.unused_variable) {
2812 static void parse_external_declaration(void)
2814 /* function-definitions and declarations both start with declaration
2816 declaration_specifiers_t specifiers;
2817 memset(&specifiers, 0, sizeof(specifiers));
2818 parse_declaration_specifiers(&specifiers);
2820 /* must be a declaration */
2821 if(token.type == ';') {
2822 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2826 /* declarator is common to both function-definitions and declarations */
2827 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2829 /* must be a declaration */
2830 if(token.type == ',' || token.type == '=' || token.type == ';') {
2831 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2835 /* must be a function definition */
2836 parse_kr_declaration_list(ndeclaration);
2838 if(token.type != '{') {
2839 parse_error_expected("while parsing function definition", '{', 0);
2844 type_t *type = ndeclaration->type;
2846 /* note that we don't skip typerefs: the standard doesn't allow them here
2847 * (so we can't use is_type_function here) */
2848 if(type->kind != TYPE_FUNCTION) {
2849 if (is_type_valid(type)) {
2850 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2851 type, ndeclaration->symbol);
2857 /* § 6.7.5.3 (14) a function definition with () means no
2858 * parameters (and not unspecified parameters) */
2859 if(type->function.unspecified_parameters) {
2860 type_t *duplicate = duplicate_type(type);
2861 duplicate->function.unspecified_parameters = false;
2863 type = typehash_insert(duplicate);
2864 if(type != duplicate) {
2865 obstack_free(type_obst, duplicate);
2867 ndeclaration->type = type;
2870 declaration_t *const declaration = record_function_definition(ndeclaration);
2871 if(ndeclaration != declaration) {
2872 declaration->scope = ndeclaration->scope;
2874 type = skip_typeref(declaration->type);
2876 /* push function parameters and switch scope */
2877 int top = environment_top();
2878 scope_t *last_scope = scope;
2879 set_scope(&declaration->scope);
2881 declaration_t *parameter = declaration->scope.declarations;
2882 for( ; parameter != NULL; parameter = parameter->next) {
2883 if(parameter->parent_scope == &ndeclaration->scope) {
2884 parameter->parent_scope = scope;
2886 assert(parameter->parent_scope == NULL
2887 || parameter->parent_scope == scope);
2888 parameter->parent_scope = scope;
2889 environment_push(parameter);
2892 if(declaration->init.statement != NULL) {
2893 parser_error_multiple_definition(declaration, token.source_position);
2895 goto end_of_parse_external_declaration;
2897 /* parse function body */
2898 int label_stack_top = label_top();
2899 declaration_t *old_current_function = current_function;
2900 current_function = declaration;
2902 declaration->init.statement = parse_compound_statement();
2905 check_declarations();
2907 assert(current_function == declaration);
2908 current_function = old_current_function;
2909 label_pop_to(label_stack_top);
2912 end_of_parse_external_declaration:
2913 assert(scope == &declaration->scope);
2914 set_scope(last_scope);
2915 environment_pop_to(top);
2918 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2920 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2921 type->bitfield.base = base;
2922 type->bitfield.size = size;
2927 static void parse_compound_declarators(declaration_t *struct_declaration,
2928 const declaration_specifiers_t *specifiers)
2930 declaration_t *last_declaration = struct_declaration->scope.declarations;
2931 if(last_declaration != NULL) {
2932 while(last_declaration->next != NULL) {
2933 last_declaration = last_declaration->next;
2938 declaration_t *declaration;
2940 if(token.type == ':') {
2943 type_t *base_type = specifiers->type;
2944 expression_t *size = parse_constant_expression();
2946 if(!is_type_integer(skip_typeref(base_type))) {
2947 errorf(HERE, "bitfield base type '%T' is not an integer type",
2951 type_t *type = make_bitfield_type(base_type, size);
2953 declaration = allocate_declaration_zero();
2954 declaration->namespc = NAMESPACE_NORMAL;
2955 declaration->storage_class = STORAGE_CLASS_NONE;
2956 declaration->source_position = token.source_position;
2957 declaration->modifiers = specifiers->decl_modifiers;
2958 declaration->type = type;
2960 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2962 type_t *orig_type = declaration->type;
2963 type_t *type = skip_typeref(orig_type);
2965 if(token.type == ':') {
2967 expression_t *size = parse_constant_expression();
2969 if(!is_type_integer(type)) {
2970 errorf(HERE, "bitfield base type '%T' is not an "
2971 "integer type", orig_type);
2974 type_t *bitfield_type = make_bitfield_type(orig_type, size);
2975 declaration->type = bitfield_type;
2977 /* TODO we ignore arrays for now... what is missing is a check
2978 * that they're at the end of the struct */
2979 if(is_type_incomplete(type) && !is_type_array(type)) {
2981 "compound member '%Y' has incomplete type '%T'",
2982 declaration->symbol, orig_type);
2983 } else if(is_type_function(type)) {
2984 errorf(HERE, "compound member '%Y' must not have function "
2985 "type '%T'", declaration->symbol, orig_type);
2990 /* make sure we don't define a symbol multiple times */
2991 symbol_t *symbol = declaration->symbol;
2992 if(symbol != NULL) {
2993 declaration_t *iter = struct_declaration->scope.declarations;
2994 for( ; iter != NULL; iter = iter->next) {
2995 if(iter->symbol == symbol) {
2996 errorf(declaration->source_position,
2997 "multiple declarations of symbol '%Y'", symbol);
2998 errorf(iter->source_position,
2999 "previous declaration of '%Y' was here", symbol);
3005 /* append declaration */
3006 if(last_declaration != NULL) {
3007 last_declaration->next = declaration;
3009 struct_declaration->scope.declarations = declaration;
3011 last_declaration = declaration;
3013 if(token.type != ',')
3020 static void parse_compound_type_entries(declaration_t *compound_declaration)
3024 while(token.type != '}' && token.type != T_EOF) {
3025 declaration_specifiers_t specifiers;
3026 memset(&specifiers, 0, sizeof(specifiers));
3027 parse_declaration_specifiers(&specifiers);
3029 parse_compound_declarators(compound_declaration, &specifiers);
3031 if(token.type == T_EOF) {
3032 errorf(HERE, "EOF while parsing struct");
3037 static type_t *parse_typename(void)
3039 declaration_specifiers_t specifiers;
3040 memset(&specifiers, 0, sizeof(specifiers));
3041 parse_declaration_specifiers(&specifiers);
3042 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3043 /* TODO: improve error message, user does probably not know what a
3044 * storage class is...
3046 errorf(HERE, "typename may not have a storage class");
3049 type_t *result = parse_abstract_declarator(specifiers.type);
3057 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3058 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3059 expression_t *left);
3061 typedef struct expression_parser_function_t expression_parser_function_t;
3062 struct expression_parser_function_t {
3063 unsigned precedence;
3064 parse_expression_function parser;
3065 unsigned infix_precedence;
3066 parse_expression_infix_function infix_parser;
3069 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3072 * Creates a new invalid expression.
3074 static expression_t *create_invalid_expression(void)
3076 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3077 expression->base.source_position = token.source_position;
3082 * Prints an error message if an expression was expected but not read
3084 static expression_t *expected_expression_error(void)
3086 /* skip the error message if the error token was read */
3087 if (token.type != T_ERROR) {
3088 errorf(HERE, "expected expression, got token '%K'", &token);
3092 return create_invalid_expression();
3096 * Parse a string constant.
3098 static expression_t *parse_string_const(void)
3100 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3101 cnst->base.type = type_char_ptr;
3102 cnst->string.value = parse_string_literals();
3108 * Parse a wide string constant.
3110 static expression_t *parse_wide_string_const(void)
3112 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3113 cnst->base.type = type_wchar_t_ptr;
3114 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3120 * Parse an integer constant.
3122 static expression_t *parse_int_const(void)
3124 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3125 cnst->base.type = token.datatype;
3126 cnst->conste.v.int_value = token.v.intvalue;
3134 * Parse a float constant.
3136 static expression_t *parse_float_const(void)
3138 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3139 cnst->base.type = token.datatype;
3140 cnst->conste.v.float_value = token.v.floatvalue;
3147 static declaration_t *create_implicit_function(symbol_t *symbol,
3148 const source_position_t source_position)
3150 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3151 ntype->function.return_type = type_int;
3152 ntype->function.unspecified_parameters = true;
3154 type_t *type = typehash_insert(ntype);
3159 declaration_t *const declaration = allocate_declaration_zero();
3160 declaration->storage_class = STORAGE_CLASS_EXTERN;
3161 declaration->type = type;
3162 declaration->symbol = symbol;
3163 declaration->source_position = source_position;
3164 declaration->parent_scope = global_scope;
3166 scope_t *old_scope = scope;
3167 set_scope(global_scope);
3169 environment_push(declaration);
3170 /* prepends the declaration to the global declarations list */
3171 declaration->next = scope->declarations;
3172 scope->declarations = declaration;
3174 assert(scope == global_scope);
3175 set_scope(old_scope);
3181 * Creates a return_type (func)(argument_type) function type if not
3184 * @param return_type the return type
3185 * @param argument_type the argument type
3187 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3189 function_parameter_t *parameter
3190 = obstack_alloc(type_obst, sizeof(parameter[0]));
3191 memset(parameter, 0, sizeof(parameter[0]));
3192 parameter->type = argument_type;
3194 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3195 type->function.return_type = return_type;
3196 type->function.parameters = parameter;
3198 type_t *result = typehash_insert(type);
3199 if(result != type) {
3207 * Creates a function type for some function like builtins.
3209 * @param symbol the symbol describing the builtin
3211 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3213 switch(symbol->ID) {
3214 case T___builtin_alloca:
3215 return make_function_1_type(type_void_ptr, type_size_t);
3216 case T___builtin_nan:
3217 return make_function_1_type(type_double, type_char_ptr);
3218 case T___builtin_nanf:
3219 return make_function_1_type(type_float, type_char_ptr);
3220 case T___builtin_nand:
3221 return make_function_1_type(type_long_double, type_char_ptr);
3222 case T___builtin_va_end:
3223 return make_function_1_type(type_void, type_valist);
3225 panic("not implemented builtin symbol found");
3230 * Performs automatic type cast as described in § 6.3.2.1.
3232 * @param orig_type the original type
3234 static type_t *automatic_type_conversion(type_t *orig_type)
3236 type_t *type = skip_typeref(orig_type);
3237 if(is_type_array(type)) {
3238 array_type_t *array_type = &type->array;
3239 type_t *element_type = array_type->element_type;
3240 unsigned qualifiers = array_type->type.qualifiers;
3242 return make_pointer_type(element_type, qualifiers);
3245 if(is_type_function(type)) {
3246 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3253 * reverts the automatic casts of array to pointer types and function
3254 * to function-pointer types as defined § 6.3.2.1
3256 type_t *revert_automatic_type_conversion(const expression_t *expression)
3258 switch (expression->kind) {
3259 case EXPR_REFERENCE: return expression->reference.declaration->type;
3260 case EXPR_SELECT: return expression->select.compound_entry->type;
3262 case EXPR_UNARY_DEREFERENCE: {
3263 const expression_t *const value = expression->unary.value;
3264 type_t *const type = skip_typeref(value->base.type);
3265 assert(is_type_pointer(type));
3266 return type->pointer.points_to;
3269 case EXPR_BUILTIN_SYMBOL:
3270 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3272 case EXPR_ARRAY_ACCESS: {
3273 const expression_t *array_ref = expression->array_access.array_ref;
3274 type_t *type_left = skip_typeref(array_ref->base.type);
3275 if (!is_type_valid(type_left))
3277 assert(is_type_pointer(type_left));
3278 return type_left->pointer.points_to;
3284 return expression->base.type;
3287 static expression_t *parse_reference(void)
3289 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3291 reference_expression_t *ref = &expression->reference;
3292 ref->symbol = token.v.symbol;
3294 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3296 source_position_t source_position = token.source_position;
3299 if(declaration == NULL) {
3300 if (! strict_mode && token.type == '(') {
3301 /* an implicitly defined function */
3302 if (warning.implicit_function_declaration) {
3303 warningf(HERE, "implicit declaration of function '%Y'",
3307 declaration = create_implicit_function(ref->symbol,
3310 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3315 type_t *type = declaration->type;
3317 /* we always do the auto-type conversions; the & and sizeof parser contains
3318 * code to revert this! */
3319 type = automatic_type_conversion(type);
3321 ref->declaration = declaration;
3322 ref->base.type = type;
3324 /* this declaration is used */
3325 declaration->used = true;
3330 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3334 /* TODO check if explicit cast is allowed and issue warnings/errors */
3337 static expression_t *parse_cast(void)
3339 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3341 cast->base.source_position = token.source_position;
3343 type_t *type = parse_typename();
3346 expression_t *value = parse_sub_expression(20);
3348 check_cast_allowed(value, type);
3350 cast->base.type = type;
3351 cast->unary.value = value;
3356 static expression_t *parse_statement_expression(void)
3358 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3360 statement_t *statement = parse_compound_statement();
3361 expression->statement.statement = statement;
3362 expression->base.source_position = statement->base.source_position;
3364 /* find last statement and use its type */
3365 type_t *type = type_void;
3366 const statement_t *stmt = statement->compound.statements;
3368 while (stmt->base.next != NULL)
3369 stmt = stmt->base.next;
3371 if (stmt->kind == STATEMENT_EXPRESSION) {
3372 type = stmt->expression.expression->base.type;
3375 warningf(expression->base.source_position, "empty statement expression ({})");
3377 expression->base.type = type;
3384 static expression_t *parse_brace_expression(void)
3388 switch(token.type) {
3390 /* gcc extension: a statement expression */
3391 return parse_statement_expression();
3395 return parse_cast();
3397 if(is_typedef_symbol(token.v.symbol)) {
3398 return parse_cast();
3402 expression_t *result = parse_expression();
3408 static expression_t *parse_function_keyword(void)
3413 if (current_function == NULL) {
3414 errorf(HERE, "'__func__' used outside of a function");
3417 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3418 expression->base.type = type_char_ptr;
3423 static expression_t *parse_pretty_function_keyword(void)
3425 eat(T___PRETTY_FUNCTION__);
3428 if (current_function == NULL) {
3429 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3432 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3433 expression->base.type = type_char_ptr;
3438 static designator_t *parse_designator(void)
3440 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3442 if(token.type != T_IDENTIFIER) {
3443 parse_error_expected("while parsing member designator",
3448 result->symbol = token.v.symbol;
3451 designator_t *last_designator = result;
3453 if(token.type == '.') {
3455 if(token.type != T_IDENTIFIER) {
3456 parse_error_expected("while parsing member designator",
3461 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3462 designator->symbol = token.v.symbol;
3465 last_designator->next = designator;
3466 last_designator = designator;
3469 if(token.type == '[') {
3471 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3472 designator->array_access = parse_expression();
3473 if(designator->array_access == NULL) {
3479 last_designator->next = designator;
3480 last_designator = designator;
3489 static expression_t *parse_offsetof(void)
3491 eat(T___builtin_offsetof);
3493 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3494 expression->base.type = type_size_t;
3497 expression->offsetofe.type = parse_typename();
3499 expression->offsetofe.designator = parse_designator();
3505 static expression_t *parse_va_start(void)
3507 eat(T___builtin_va_start);
3509 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3512 expression->va_starte.ap = parse_assignment_expression();
3514 expression_t *const expr = parse_assignment_expression();
3515 if (expr->kind == EXPR_REFERENCE) {
3516 declaration_t *const decl = expr->reference.declaration;
3518 return create_invalid_expression();
3519 if (decl->parent_scope == ¤t_function->scope &&
3520 decl->next == NULL) {
3521 expression->va_starte.parameter = decl;
3526 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3528 return create_invalid_expression();
3531 static expression_t *parse_va_arg(void)
3533 eat(T___builtin_va_arg);
3535 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3538 expression->va_arge.ap = parse_assignment_expression();
3540 expression->base.type = parse_typename();
3546 static expression_t *parse_builtin_symbol(void)
3548 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3550 symbol_t *symbol = token.v.symbol;
3552 expression->builtin_symbol.symbol = symbol;
3555 type_t *type = get_builtin_symbol_type(symbol);
3556 type = automatic_type_conversion(type);
3558 expression->base.type = type;
3562 static expression_t *parse_builtin_constant(void)
3564 eat(T___builtin_constant_p);
3566 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3569 expression->builtin_constant.value = parse_assignment_expression();
3571 expression->base.type = type_int;
3576 static expression_t *parse_builtin_prefetch(void)
3578 eat(T___builtin_prefetch);
3580 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3583 expression->builtin_prefetch.adr = parse_assignment_expression();
3584 if (token.type == ',') {
3586 expression->builtin_prefetch.rw = parse_assignment_expression();
3588 if (token.type == ',') {
3590 expression->builtin_prefetch.locality = parse_assignment_expression();
3593 expression->base.type = type_void;
3598 static expression_t *parse_compare_builtin(void)
3600 expression_t *expression;
3602 switch(token.type) {
3603 case T___builtin_isgreater:
3604 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3606 case T___builtin_isgreaterequal:
3607 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3609 case T___builtin_isless:
3610 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3612 case T___builtin_islessequal:
3613 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3615 case T___builtin_islessgreater:
3616 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3618 case T___builtin_isunordered:
3619 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3622 panic("invalid compare builtin found");
3625 expression->base.source_position = HERE;
3629 expression->binary.left = parse_assignment_expression();
3631 expression->binary.right = parse_assignment_expression();
3634 type_t *const orig_type_left = expression->binary.left->base.type;
3635 type_t *const orig_type_right = expression->binary.right->base.type;
3637 type_t *const type_left = skip_typeref(orig_type_left);
3638 type_t *const type_right = skip_typeref(orig_type_right);
3639 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3640 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3641 type_error_incompatible("invalid operands in comparison",
3642 expression->base.source_position, orig_type_left, orig_type_right);
3645 semantic_comparison(&expression->binary);
3651 static expression_t *parse_builtin_expect(void)
3653 eat(T___builtin_expect);
3655 expression_t *expression
3656 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3659 expression->binary.left = parse_assignment_expression();
3661 expression->binary.right = parse_constant_expression();
3664 expression->base.type = expression->binary.left->base.type;
3669 static expression_t *parse_assume(void) {
3672 expression_t *expression
3673 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3676 expression->unary.value = parse_assignment_expression();
3679 expression->base.type = type_void;
3683 static expression_t *parse_primary_expression(void)
3685 switch(token.type) {
3687 return parse_int_const();
3688 case T_FLOATINGPOINT:
3689 return parse_float_const();
3690 case T_STRING_LITERAL:
3691 return parse_string_const();
3692 case T_WIDE_STRING_LITERAL:
3693 return parse_wide_string_const();
3695 return parse_reference();
3696 case T___FUNCTION__:
3698 return parse_function_keyword();
3699 case T___PRETTY_FUNCTION__:
3700 return parse_pretty_function_keyword();
3701 case T___builtin_offsetof:
3702 return parse_offsetof();
3703 case T___builtin_va_start:
3704 return parse_va_start();
3705 case T___builtin_va_arg:
3706 return parse_va_arg();
3707 case T___builtin_expect:
3708 return parse_builtin_expect();
3709 case T___builtin_alloca:
3710 case T___builtin_nan:
3711 case T___builtin_nand:
3712 case T___builtin_nanf:
3713 case T___builtin_va_end:
3714 return parse_builtin_symbol();
3715 case T___builtin_isgreater:
3716 case T___builtin_isgreaterequal:
3717 case T___builtin_isless:
3718 case T___builtin_islessequal:
3719 case T___builtin_islessgreater:
3720 case T___builtin_isunordered:
3721 return parse_compare_builtin();
3722 case T___builtin_constant_p:
3723 return parse_builtin_constant();
3724 case T___builtin_prefetch:
3725 return parse_builtin_prefetch();
3727 return parse_assume();
3730 return parse_brace_expression();
3733 errorf(HERE, "unexpected token '%K'", &token);
3736 return create_invalid_expression();
3740 * Check if the expression has the character type and issue a warning then.
3742 static void check_for_char_index_type(const expression_t *expression) {
3743 type_t *const type = expression->base.type;
3744 const type_t *const base_type = skip_typeref(type);
3746 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3747 warning.char_subscripts) {
3748 warningf(expression->base.source_position,
3749 "array subscript has type '%T'", type);
3753 static expression_t *parse_array_expression(unsigned precedence,
3760 expression_t *inside = parse_expression();
3762 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3764 array_access_expression_t *array_access = &expression->array_access;
3766 type_t *const orig_type_left = left->base.type;
3767 type_t *const orig_type_inside = inside->base.type;
3769 type_t *const type_left = skip_typeref(orig_type_left);
3770 type_t *const type_inside = skip_typeref(orig_type_inside);
3772 type_t *return_type;
3773 if (is_type_pointer(type_left)) {
3774 return_type = type_left->pointer.points_to;
3775 array_access->array_ref = left;
3776 array_access->index = inside;
3777 check_for_char_index_type(inside);
3778 } else if (is_type_pointer(type_inside)) {
3779 return_type = type_inside->pointer.points_to;
3780 array_access->array_ref = inside;
3781 array_access->index = left;
3782 array_access->flipped = true;
3783 check_for_char_index_type(left);
3785 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3787 "array access on object with non-pointer types '%T', '%T'",
3788 orig_type_left, orig_type_inside);
3790 return_type = type_error_type;
3791 array_access->array_ref = create_invalid_expression();
3794 if(token.type != ']') {
3795 parse_error_expected("Problem while parsing array access", ']', 0);
3800 return_type = automatic_type_conversion(return_type);
3801 expression->base.type = return_type;
3806 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3808 expression_t *tp_expression = allocate_expression_zero(kind);
3809 tp_expression->base.type = type_size_t;
3811 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3813 tp_expression->typeprop.type = parse_typename();
3816 expression_t *expression = parse_sub_expression(precedence);
3817 expression->base.type = revert_automatic_type_conversion(expression);
3819 tp_expression->typeprop.type = expression->base.type;
3820 tp_expression->typeprop.tp_expression = expression;
3823 return tp_expression;
3826 static expression_t *parse_sizeof(unsigned precedence)
3829 return parse_typeprop(EXPR_SIZEOF, precedence);
3832 static expression_t *parse_alignof(unsigned precedence)
3835 return parse_typeprop(EXPR_SIZEOF, precedence);
3838 static expression_t *parse_select_expression(unsigned precedence,
3839 expression_t *compound)
3842 assert(token.type == '.' || token.type == T_MINUSGREATER);
3844 bool is_pointer = (token.type == T_MINUSGREATER);
3847 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3848 select->select.compound = compound;
3850 if(token.type != T_IDENTIFIER) {
3851 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3854 symbol_t *symbol = token.v.symbol;
3855 select->select.symbol = symbol;
3858 type_t *const orig_type = compound->base.type;
3859 type_t *const type = skip_typeref(orig_type);
3861 type_t *type_left = type;
3863 if (!is_type_pointer(type)) {
3864 if (is_type_valid(type)) {
3865 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3867 return create_invalid_expression();
3869 type_left = type->pointer.points_to;
3871 type_left = skip_typeref(type_left);
3873 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3874 type_left->kind != TYPE_COMPOUND_UNION) {
3875 if (is_type_valid(type_left)) {
3876 errorf(HERE, "request for member '%Y' in something not a struct or "
3877 "union, but '%T'", symbol, type_left);
3879 return create_invalid_expression();
3882 declaration_t *const declaration = type_left->compound.declaration;
3884 if(!declaration->init.is_defined) {
3885 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3887 return create_invalid_expression();
3890 declaration_t *iter = declaration->scope.declarations;
3891 for( ; iter != NULL; iter = iter->next) {
3892 if(iter->symbol == symbol) {
3897 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3898 return create_invalid_expression();
3901 /* we always do the auto-type conversions; the & and sizeof parser contains
3902 * code to revert this! */
3903 type_t *expression_type = automatic_type_conversion(iter->type);
3905 select->select.compound_entry = iter;
3906 select->base.type = expression_type;
3908 if(expression_type->kind == TYPE_BITFIELD) {
3909 expression_t *extract
3910 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3911 extract->unary.value = select;
3912 extract->base.type = expression_type->bitfield.base;
3921 * Parse a call expression, ie. expression '( ... )'.
3923 * @param expression the function address
3925 static expression_t *parse_call_expression(unsigned precedence,
3926 expression_t *expression)
3929 expression_t *result = allocate_expression_zero(EXPR_CALL);
3931 call_expression_t *call = &result->call;
3932 call->function = expression;
3934 type_t *const orig_type = expression->base.type;
3935 type_t *const type = skip_typeref(orig_type);
3937 function_type_t *function_type = NULL;
3938 if (is_type_pointer(type)) {
3939 type_t *const to_type = skip_typeref(type->pointer.points_to);
3941 if (is_type_function(to_type)) {
3942 function_type = &to_type->function;
3943 call->base.type = function_type->return_type;
3947 if (function_type == NULL && is_type_valid(type)) {
3948 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3951 /* parse arguments */
3954 if(token.type != ')') {
3955 call_argument_t *last_argument = NULL;
3958 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3960 argument->expression = parse_assignment_expression();
3961 if(last_argument == NULL) {
3962 call->arguments = argument;
3964 last_argument->next = argument;
3966 last_argument = argument;
3968 if(token.type != ',')
3975 if(function_type != NULL) {
3976 function_parameter_t *parameter = function_type->parameters;
3977 call_argument_t *argument = call->arguments;
3978 for( ; parameter != NULL && argument != NULL;
3979 parameter = parameter->next, argument = argument->next) {
3980 type_t *expected_type = parameter->type;
3981 /* TODO report scope in error messages */
3982 expression_t *const arg_expr = argument->expression;
3983 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3984 if (res_type == NULL) {
3985 /* TODO improve error message */
3986 errorf(arg_expr->base.source_position,
3987 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3988 arg_expr, arg_expr->base.type, expected_type);
3990 argument->expression = create_implicit_cast(argument->expression, expected_type);
3993 /* too few parameters */
3994 if(parameter != NULL) {
3995 errorf(HERE, "too few arguments to function '%E'", expression);
3996 } else if(argument != NULL) {
3997 /* too many parameters */
3998 if(!function_type->variadic
3999 && !function_type->unspecified_parameters) {
4000 errorf(HERE, "too many arguments to function '%E'", expression);
4002 /* do default promotion */
4003 for( ; argument != NULL; argument = argument->next) {
4004 type_t *type = argument->expression->base.type;
4006 type = skip_typeref(type);
4007 if(is_type_integer(type)) {
4008 type = promote_integer(type);
4009 } else if(type == type_float) {
4013 argument->expression
4014 = create_implicit_cast(argument->expression, type);
4017 check_format(&result->call);
4020 check_format(&result->call);
4027 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4029 static bool same_compound_type(const type_t *type1, const type_t *type2)
4032 is_type_compound(type1) &&
4033 type1->kind == type2->kind &&
4034 type1->compound.declaration == type2->compound.declaration;
4038 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4040 * @param expression the conditional expression
4042 static expression_t *parse_conditional_expression(unsigned precedence,
4043 expression_t *expression)
4047 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4049 conditional_expression_t *conditional = &result->conditional;
4050 conditional->condition = expression;
4053 type_t *const condition_type_orig = expression->base.type;
4054 type_t *const condition_type = skip_typeref(condition_type_orig);
4055 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4056 type_error("expected a scalar type in conditional condition",
4057 expression->base.source_position, condition_type_orig);
4060 expression_t *true_expression = parse_expression();
4062 expression_t *false_expression = parse_sub_expression(precedence);
4064 conditional->true_expression = true_expression;
4065 conditional->false_expression = false_expression;
4067 type_t *const orig_true_type = true_expression->base.type;
4068 type_t *const orig_false_type = false_expression->base.type;
4069 type_t *const true_type = skip_typeref(orig_true_type);
4070 type_t *const false_type = skip_typeref(orig_false_type);
4073 type_t *result_type;
4074 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4075 result_type = semantic_arithmetic(true_type, false_type);
4077 true_expression = create_implicit_cast(true_expression, result_type);
4078 false_expression = create_implicit_cast(false_expression, result_type);
4080 conditional->true_expression = true_expression;
4081 conditional->false_expression = false_expression;
4082 conditional->base.type = result_type;
4083 } else if (same_compound_type(true_type, false_type) || (
4084 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4085 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4087 /* just take 1 of the 2 types */
4088 result_type = true_type;
4089 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4090 && pointers_compatible(true_type, false_type)) {
4092 result_type = true_type;
4095 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4096 type_error_incompatible("while parsing conditional",
4097 expression->base.source_position, true_type,
4100 result_type = type_error_type;
4103 conditional->base.type = result_type;
4108 * Parse an extension expression.
4110 static expression_t *parse_extension(unsigned precedence)
4112 eat(T___extension__);
4114 /* TODO enable extensions */
4115 expression_t *expression = parse_sub_expression(precedence);
4116 /* TODO disable extensions */
4120 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4122 eat(T___builtin_classify_type);
4124 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4125 result->base.type = type_int;
4128 expression_t *expression = parse_sub_expression(precedence);
4130 result->classify_type.type_expression = expression;
4135 static void semantic_incdec(unary_expression_t *expression)
4137 type_t *const orig_type = expression->value->base.type;
4138 type_t *const type = skip_typeref(orig_type);
4139 /* TODO !is_type_real && !is_type_pointer */
4140 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4141 if (is_type_valid(type)) {
4142 /* TODO: improve error message */
4143 errorf(HERE, "operation needs an arithmetic or pointer type");
4148 expression->base.type = orig_type;
4151 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4153 type_t *const orig_type = expression->value->base.type;
4154 type_t *const type = skip_typeref(orig_type);
4155 if(!is_type_arithmetic(type)) {
4156 if (is_type_valid(type)) {
4157 /* TODO: improve error message */
4158 errorf(HERE, "operation needs an arithmetic type");
4163 expression->base.type = orig_type;
4166 static void semantic_unexpr_scalar(unary_expression_t *expression)
4168 type_t *const orig_type = expression->value->base.type;
4169 type_t *const type = skip_typeref(orig_type);
4170 if (!is_type_scalar(type)) {
4171 if (is_type_valid(type)) {
4172 errorf(HERE, "operand of ! must be of scalar type");
4177 expression->base.type = orig_type;
4180 static void semantic_unexpr_integer(unary_expression_t *expression)
4182 type_t *const orig_type = expression->value->base.type;
4183 type_t *const type = skip_typeref(orig_type);
4184 if (!is_type_integer(type)) {
4185 if (is_type_valid(type)) {
4186 errorf(HERE, "operand of ~ must be of integer type");
4191 expression->base.type = orig_type;
4194 static void semantic_dereference(unary_expression_t *expression)
4196 type_t *const orig_type = expression->value->base.type;
4197 type_t *const type = skip_typeref(orig_type);
4198 if(!is_type_pointer(type)) {
4199 if (is_type_valid(type)) {
4200 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4205 type_t *result_type = type->pointer.points_to;
4206 result_type = automatic_type_conversion(result_type);
4207 expression->base.type = result_type;
4211 * Check the semantic of the address taken expression.
4213 static void semantic_take_addr(unary_expression_t *expression)
4215 expression_t *value = expression->value;
4216 value->base.type = revert_automatic_type_conversion(value);
4218 type_t *orig_type = value->base.type;
4219 if(!is_type_valid(orig_type))
4222 if(value->kind == EXPR_REFERENCE) {
4223 declaration_t *const declaration = value->reference.declaration;
4224 if(declaration != NULL) {
4225 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4226 errorf(expression->base.source_position,
4227 "address of register variable '%Y' requested",
4228 declaration->symbol);
4230 declaration->address_taken = 1;
4234 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4237 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4238 static expression_t *parse_##unexpression_type(unsigned precedence) \
4242 expression_t *unary_expression \
4243 = allocate_expression_zero(unexpression_type); \
4244 unary_expression->base.source_position = HERE; \
4245 unary_expression->unary.value = parse_sub_expression(precedence); \
4247 sfunc(&unary_expression->unary); \
4249 return unary_expression; \
4252 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4253 semantic_unexpr_arithmetic)
4254 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4255 semantic_unexpr_arithmetic)
4256 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4257 semantic_unexpr_scalar)
4258 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4259 semantic_dereference)
4260 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4262 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4263 semantic_unexpr_integer)
4264 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4266 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4269 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4271 static expression_t *parse_##unexpression_type(unsigned precedence, \
4272 expression_t *left) \
4274 (void) precedence; \
4277 expression_t *unary_expression \
4278 = allocate_expression_zero(unexpression_type); \
4279 unary_expression->unary.value = left; \
4281 sfunc(&unary_expression->unary); \
4283 return unary_expression; \
4286 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4287 EXPR_UNARY_POSTFIX_INCREMENT,
4289 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4290 EXPR_UNARY_POSTFIX_DECREMENT,
4293 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4295 /* TODO: handle complex + imaginary types */
4297 /* § 6.3.1.8 Usual arithmetic conversions */
4298 if(type_left == type_long_double || type_right == type_long_double) {
4299 return type_long_double;
4300 } else if(type_left == type_double || type_right == type_double) {
4302 } else if(type_left == type_float || type_right == type_float) {
4306 type_right = promote_integer(type_right);
4307 type_left = promote_integer(type_left);
4309 if(type_left == type_right)
4312 bool signed_left = is_type_signed(type_left);
4313 bool signed_right = is_type_signed(type_right);
4314 int rank_left = get_rank(type_left);
4315 int rank_right = get_rank(type_right);
4316 if(rank_left < rank_right) {
4317 if(signed_left == signed_right || !signed_right) {
4323 if(signed_left == signed_right || !signed_left) {
4332 * Check the semantic restrictions for a binary expression.
4334 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4336 expression_t *const left = expression->left;
4337 expression_t *const right = expression->right;
4338 type_t *const orig_type_left = left->base.type;
4339 type_t *const orig_type_right = right->base.type;
4340 type_t *const type_left = skip_typeref(orig_type_left);
4341 type_t *const type_right = skip_typeref(orig_type_right);
4343 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4344 /* TODO: improve error message */
4345 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4346 errorf(HERE, "operation needs arithmetic types");
4351 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4352 expression->left = create_implicit_cast(left, arithmetic_type);
4353 expression->right = create_implicit_cast(right, arithmetic_type);
4354 expression->base.type = arithmetic_type;
4357 static void semantic_shift_op(binary_expression_t *expression)
4359 expression_t *const left = expression->left;
4360 expression_t *const right = expression->right;
4361 type_t *const orig_type_left = left->base.type;
4362 type_t *const orig_type_right = right->base.type;
4363 type_t * type_left = skip_typeref(orig_type_left);
4364 type_t * type_right = skip_typeref(orig_type_right);
4366 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4367 /* TODO: improve error message */
4368 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4369 errorf(HERE, "operation needs integer types");
4374 type_left = promote_integer(type_left);
4375 type_right = promote_integer(type_right);
4377 expression->left = create_implicit_cast(left, type_left);
4378 expression->right = create_implicit_cast(right, type_right);
4379 expression->base.type = type_left;
4382 static void semantic_add(binary_expression_t *expression)
4384 expression_t *const left = expression->left;
4385 expression_t *const right = expression->right;
4386 type_t *const orig_type_left = left->base.type;
4387 type_t *const orig_type_right = right->base.type;
4388 type_t *const type_left = skip_typeref(orig_type_left);
4389 type_t *const type_right = skip_typeref(orig_type_right);
4392 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4393 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4394 expression->left = create_implicit_cast(left, arithmetic_type);
4395 expression->right = create_implicit_cast(right, arithmetic_type);
4396 expression->base.type = arithmetic_type;
4398 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4399 expression->base.type = type_left;
4400 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4401 expression->base.type = type_right;
4402 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4403 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4407 static void semantic_sub(binary_expression_t *expression)
4409 expression_t *const left = expression->left;
4410 expression_t *const right = expression->right;
4411 type_t *const orig_type_left = left->base.type;
4412 type_t *const orig_type_right = right->base.type;
4413 type_t *const type_left = skip_typeref(orig_type_left);
4414 type_t *const type_right = skip_typeref(orig_type_right);
4417 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
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;
4423 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4424 expression->base.type = type_left;
4425 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4426 if(!pointers_compatible(type_left, type_right)) {
4428 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4429 orig_type_left, orig_type_right);
4431 expression->base.type = type_ptrdiff_t;
4433 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4434 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4435 orig_type_left, orig_type_right);
4440 * Check the semantics of comparison expressions.
4442 * @param expression The expression to check.
4444 static void semantic_comparison(binary_expression_t *expression)
4446 expression_t *left = expression->left;
4447 expression_t *right = expression->right;
4448 type_t *orig_type_left = left->base.type;
4449 type_t *orig_type_right = right->base.type;
4451 type_t *type_left = skip_typeref(orig_type_left);
4452 type_t *type_right = skip_typeref(orig_type_right);
4454 /* TODO non-arithmetic types */
4455 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4456 if (warning.sign_compare &&
4457 (expression->base.kind != EXPR_BINARY_EQUAL &&
4458 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4459 (is_type_signed(type_left) != is_type_signed(type_right))) {
4460 warningf(expression->base.source_position,
4461 "comparison between signed and unsigned");
4463 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4464 expression->left = create_implicit_cast(left, arithmetic_type);
4465 expression->right = create_implicit_cast(right, arithmetic_type);
4466 expression->base.type = arithmetic_type;
4467 if (warning.float_equal &&
4468 (expression->base.kind == EXPR_BINARY_EQUAL ||
4469 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4470 is_type_float(arithmetic_type)) {
4471 warningf(expression->base.source_position,
4472 "comparing floating point with == or != is unsafe");
4474 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4475 /* TODO check compatibility */
4476 } else if (is_type_pointer(type_left)) {
4477 expression->right = create_implicit_cast(right, type_left);
4478 } else if (is_type_pointer(type_right)) {
4479 expression->left = create_implicit_cast(left, type_right);
4480 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4481 type_error_incompatible("invalid operands in comparison",
4482 expression->base.source_position,
4483 type_left, type_right);
4485 expression->base.type = type_int;
4488 static void semantic_arithmetic_assign(binary_expression_t *expression)
4490 expression_t *left = expression->left;
4491 expression_t *right = expression->right;
4492 type_t *orig_type_left = left->base.type;
4493 type_t *orig_type_right = right->base.type;
4495 type_t *type_left = skip_typeref(orig_type_left);
4496 type_t *type_right = skip_typeref(orig_type_right);
4498 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4499 /* TODO: improve error message */
4500 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4501 errorf(HERE, "operation needs arithmetic types");
4506 /* combined instructions are tricky. We can't create an implicit cast on
4507 * the left side, because we need the uncasted form for the store.
4508 * The ast2firm pass has to know that left_type must be right_type
4509 * for the arithmetic operation and create a cast by itself */
4510 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4511 expression->right = create_implicit_cast(right, arithmetic_type);
4512 expression->base.type = type_left;
4515 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4517 expression_t *const left = expression->left;
4518 expression_t *const right = expression->right;
4519 type_t *const orig_type_left = left->base.type;
4520 type_t *const orig_type_right = right->base.type;
4521 type_t *const type_left = skip_typeref(orig_type_left);
4522 type_t *const type_right = skip_typeref(orig_type_right);
4524 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4525 /* combined instructions are tricky. We can't create an implicit cast on
4526 * the left side, because we need the uncasted form for the store.
4527 * The ast2firm pass has to know that left_type must be right_type
4528 * for the arithmetic operation and create a cast by itself */
4529 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4530 expression->right = create_implicit_cast(right, arithmetic_type);
4531 expression->base.type = type_left;
4532 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4533 expression->base.type = type_left;
4534 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4535 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4540 * Check the semantic restrictions of a logical expression.
4542 static void semantic_logical_op(binary_expression_t *expression)
4544 expression_t *const left = expression->left;
4545 expression_t *const right = expression->right;
4546 type_t *const orig_type_left = left->base.type;
4547 type_t *const orig_type_right = right->base.type;
4548 type_t *const type_left = skip_typeref(orig_type_left);
4549 type_t *const type_right = skip_typeref(orig_type_right);
4551 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4552 /* TODO: improve error message */
4553 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4554 errorf(HERE, "operation needs scalar types");
4559 expression->base.type = type_int;
4563 * Checks if a compound type has constant fields.
4565 static bool has_const_fields(const compound_type_t *type)
4567 const scope_t *scope = &type->declaration->scope;
4568 const declaration_t *declaration = scope->declarations;
4570 for (; declaration != NULL; declaration = declaration->next) {
4571 if (declaration->namespc != NAMESPACE_NORMAL)
4574 const type_t *decl_type = skip_typeref(declaration->type);
4575 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4583 * Check the semantic restrictions of a binary assign expression.
4585 static void semantic_binexpr_assign(binary_expression_t *expression)
4587 expression_t *left = expression->left;
4588 type_t *orig_type_left = left->base.type;
4590 type_t *type_left = revert_automatic_type_conversion(left);
4591 type_left = skip_typeref(orig_type_left);
4593 /* must be a modifiable lvalue */
4594 if (is_type_array(type_left)) {
4595 errorf(HERE, "cannot assign to arrays ('%E')", left);
4598 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4599 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4603 if(is_type_incomplete(type_left)) {
4605 "left-hand side of assignment '%E' has incomplete type '%T'",
4606 left, orig_type_left);
4609 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4610 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4611 left, orig_type_left);
4615 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4617 if (res_type == NULL) {
4618 errorf(expression->base.source_position,
4619 "cannot assign to '%T' from '%T'",
4620 orig_type_left, expression->right->base.type);
4622 expression->right = create_implicit_cast(expression->right, res_type);
4625 expression->base.type = orig_type_left;
4628 static bool expression_has_effect(const expression_t *const expr)
4630 switch (expr->kind) {
4631 case EXPR_UNKNOWN: break;
4632 case EXPR_INVALID: break;
4633 case EXPR_REFERENCE: return false;
4634 case EXPR_CONST: return false;
4635 case EXPR_STRING_LITERAL: return false;
4636 case EXPR_WIDE_STRING_LITERAL: return false;
4638 const call_expression_t *const call = &expr->call;
4639 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4642 switch (call->function->builtin_symbol.symbol->ID) {
4643 case T___builtin_va_end: return true;
4644 default: return false;
4647 case EXPR_CONDITIONAL: {
4648 const conditional_expression_t *const cond = &expr->conditional;
4650 expression_has_effect(cond->true_expression) &&
4651 expression_has_effect(cond->false_expression);
4653 case EXPR_SELECT: return false;
4654 case EXPR_ARRAY_ACCESS: return false;
4655 case EXPR_SIZEOF: return false;
4656 case EXPR_CLASSIFY_TYPE: return false;
4657 case EXPR_ALIGNOF: return false;
4659 case EXPR_FUNCTION: return false;
4660 case EXPR_PRETTY_FUNCTION: return false;
4661 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4662 case EXPR_BUILTIN_CONSTANT_P: return false;
4663 case EXPR_BUILTIN_PREFETCH: return true;
4664 case EXPR_OFFSETOF: return false;
4665 case EXPR_VA_START: return true;
4666 case EXPR_VA_ARG: return true;
4667 case EXPR_STATEMENT: return true; // TODO
4669 case EXPR_UNARY_NEGATE: return false;
4670 case EXPR_UNARY_PLUS: return false;
4671 case EXPR_UNARY_BITWISE_NEGATE: return false;
4672 case EXPR_UNARY_NOT: return false;
4673 case EXPR_UNARY_DEREFERENCE: return false;
4674 case EXPR_UNARY_TAKE_ADDRESS: return false;
4675 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4676 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4677 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4678 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4679 case EXPR_UNARY_CAST:
4680 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4681 case EXPR_UNARY_CAST_IMPLICIT: return true;
4682 case EXPR_UNARY_ASSUME: return true;
4683 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4685 case EXPR_BINARY_ADD: return false;
4686 case EXPR_BINARY_SUB: return false;
4687 case EXPR_BINARY_MUL: return false;
4688 case EXPR_BINARY_DIV: return false;
4689 case EXPR_BINARY_MOD: return false;
4690 case EXPR_BINARY_EQUAL: return false;
4691 case EXPR_BINARY_NOTEQUAL: return false;
4692 case EXPR_BINARY_LESS: return false;
4693 case EXPR_BINARY_LESSEQUAL: return false;
4694 case EXPR_BINARY_GREATER: return false;
4695 case EXPR_BINARY_GREATEREQUAL: return false;
4696 case EXPR_BINARY_BITWISE_AND: return false;
4697 case EXPR_BINARY_BITWISE_OR: return false;
4698 case EXPR_BINARY_BITWISE_XOR: return false;
4699 case EXPR_BINARY_SHIFTLEFT: return false;
4700 case EXPR_BINARY_SHIFTRIGHT: return false;
4701 case EXPR_BINARY_ASSIGN: return true;
4702 case EXPR_BINARY_MUL_ASSIGN: return true;
4703 case EXPR_BINARY_DIV_ASSIGN: return true;
4704 case EXPR_BINARY_MOD_ASSIGN: return true;
4705 case EXPR_BINARY_ADD_ASSIGN: return true;
4706 case EXPR_BINARY_SUB_ASSIGN: return true;
4707 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4708 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4709 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4710 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4711 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4712 case EXPR_BINARY_LOGICAL_AND:
4713 case EXPR_BINARY_LOGICAL_OR:
4714 case EXPR_BINARY_COMMA:
4715 return expression_has_effect(expr->binary.right);
4717 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4718 case EXPR_BINARY_ISGREATER: return false;
4719 case EXPR_BINARY_ISGREATEREQUAL: return false;
4720 case EXPR_BINARY_ISLESS: return false;
4721 case EXPR_BINARY_ISLESSEQUAL: return false;
4722 case EXPR_BINARY_ISLESSGREATER: return false;
4723 case EXPR_BINARY_ISUNORDERED: return false;
4726 panic("unexpected statement");
4729 static void semantic_comma(binary_expression_t *expression)
4731 if (warning.unused_value) {
4732 const expression_t *const left = expression->left;
4733 if (!expression_has_effect(left)) {
4734 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4737 expression->base.type = expression->right->base.type;
4740 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4741 static expression_t *parse_##binexpression_type(unsigned precedence, \
4742 expression_t *left) \
4745 source_position_t pos = HERE; \
4747 expression_t *right = parse_sub_expression(precedence + lr); \
4749 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4750 binexpr->base.source_position = pos; \
4751 binexpr->binary.left = left; \
4752 binexpr->binary.right = right; \
4753 sfunc(&binexpr->binary); \
4758 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4759 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4760 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4761 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4762 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4763 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4764 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4765 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4766 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4768 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4769 semantic_comparison, 1)
4770 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4771 semantic_comparison, 1)
4772 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4773 semantic_comparison, 1)
4774 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4775 semantic_comparison, 1)
4777 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4778 semantic_binexpr_arithmetic, 1)
4779 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4780 semantic_binexpr_arithmetic, 1)
4781 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4782 semantic_binexpr_arithmetic, 1)
4783 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4784 semantic_logical_op, 1)
4785 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4786 semantic_logical_op, 1)
4787 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4788 semantic_shift_op, 1)
4789 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4790 semantic_shift_op, 1)
4791 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4792 semantic_arithmetic_addsubb_assign, 0)
4793 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4794 semantic_arithmetic_addsubb_assign, 0)
4795 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4796 semantic_arithmetic_assign, 0)
4797 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4798 semantic_arithmetic_assign, 0)
4799 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4800 semantic_arithmetic_assign, 0)
4801 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4802 semantic_arithmetic_assign, 0)
4803 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4804 semantic_arithmetic_assign, 0)
4805 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4806 semantic_arithmetic_assign, 0)
4807 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4808 semantic_arithmetic_assign, 0)
4809 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4810 semantic_arithmetic_assign, 0)
4812 static expression_t *parse_sub_expression(unsigned precedence)
4814 if(token.type < 0) {
4815 return expected_expression_error();
4818 expression_parser_function_t *parser
4819 = &expression_parsers[token.type];
4820 source_position_t source_position = token.source_position;
4823 if(parser->parser != NULL) {
4824 left = parser->parser(parser->precedence);
4826 left = parse_primary_expression();
4828 assert(left != NULL);
4829 left->base.source_position = source_position;
4832 if(token.type < 0) {
4833 return expected_expression_error();
4836 parser = &expression_parsers[token.type];
4837 if(parser->infix_parser == NULL)
4839 if(parser->infix_precedence < precedence)
4842 left = parser->infix_parser(parser->infix_precedence, left);
4844 assert(left != NULL);
4845 assert(left->kind != EXPR_UNKNOWN);
4846 left->base.source_position = source_position;
4853 * Parse an expression.
4855 static expression_t *parse_expression(void)
4857 return parse_sub_expression(1);
4861 * Register a parser for a prefix-like operator with given precedence.
4863 * @param parser the parser function
4864 * @param token_type the token type of the prefix token
4865 * @param precedence the precedence of the operator
4867 static void register_expression_parser(parse_expression_function parser,
4868 int token_type, unsigned precedence)
4870 expression_parser_function_t *entry = &expression_parsers[token_type];
4872 if(entry->parser != NULL) {
4873 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4874 panic("trying to register multiple expression parsers for a token");
4876 entry->parser = parser;
4877 entry->precedence = precedence;
4881 * Register a parser for an infix operator with given precedence.
4883 * @param parser the parser function
4884 * @param token_type the token type of the infix operator
4885 * @param precedence the precedence of the operator
4887 static void register_infix_parser(parse_expression_infix_function parser,
4888 int token_type, unsigned precedence)
4890 expression_parser_function_t *entry = &expression_parsers[token_type];
4892 if(entry->infix_parser != NULL) {
4893 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4894 panic("trying to register multiple infix expression parsers for a "
4897 entry->infix_parser = parser;
4898 entry->infix_precedence = precedence;
4902 * Initialize the expression parsers.
4904 static void init_expression_parsers(void)
4906 memset(&expression_parsers, 0, sizeof(expression_parsers));
4908 register_infix_parser(parse_array_expression, '[', 30);
4909 register_infix_parser(parse_call_expression, '(', 30);
4910 register_infix_parser(parse_select_expression, '.', 30);
4911 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4912 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4914 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4917 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4918 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4919 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4920 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4921 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4922 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4923 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4924 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4925 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4926 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4927 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4928 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4929 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4930 T_EXCLAMATIONMARKEQUAL, 13);
4931 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4932 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4933 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4934 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4935 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4936 register_infix_parser(parse_conditional_expression, '?', 7);
4937 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4938 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4939 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4940 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4941 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4942 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4943 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4944 T_LESSLESSEQUAL, 2);
4945 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4946 T_GREATERGREATEREQUAL, 2);
4947 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4949 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4951 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4954 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4956 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4957 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4958 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4959 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4960 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4961 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4962 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4964 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4966 register_expression_parser(parse_sizeof, T_sizeof, 25);
4967 register_expression_parser(parse_alignof, T___alignof__, 25);
4968 register_expression_parser(parse_extension, T___extension__, 25);
4969 register_expression_parser(parse_builtin_classify_type,
4970 T___builtin_classify_type, 25);
4974 * Parse a asm statement constraints specification.
4976 static asm_constraint_t *parse_asm_constraints(void)
4978 asm_constraint_t *result = NULL;
4979 asm_constraint_t *last = NULL;
4981 while(token.type == T_STRING_LITERAL || token.type == '[') {
4982 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4983 memset(constraint, 0, sizeof(constraint[0]));
4985 if(token.type == '[') {
4987 if(token.type != T_IDENTIFIER) {
4988 parse_error_expected("while parsing asm constraint",
4992 constraint->symbol = token.v.symbol;
4997 constraint->constraints = parse_string_literals();
4999 constraint->expression = parse_expression();
5003 last->next = constraint;
5005 result = constraint;
5009 if(token.type != ',')
5018 * Parse a asm statement clobber specification.
5020 static asm_clobber_t *parse_asm_clobbers(void)
5022 asm_clobber_t *result = NULL;
5023 asm_clobber_t *last = NULL;
5025 while(token.type == T_STRING_LITERAL) {
5026 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5027 clobber->clobber = parse_string_literals();
5030 last->next = clobber;
5036 if(token.type != ',')
5045 * Parse an asm statement.
5047 static statement_t *parse_asm_statement(void)
5051 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5052 statement->base.source_position = token.source_position;
5054 asm_statement_t *asm_statement = &statement->asms;
5056 if(token.type == T_volatile) {
5058 asm_statement->is_volatile = true;
5062 asm_statement->asm_text = parse_string_literals();
5064 if(token.type != ':')
5068 asm_statement->inputs = parse_asm_constraints();
5069 if(token.type != ':')
5073 asm_statement->outputs = parse_asm_constraints();
5074 if(token.type != ':')
5078 asm_statement->clobbers = parse_asm_clobbers();
5087 * Parse a case statement.
5089 static statement_t *parse_case_statement(void)
5093 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5095 statement->base.source_position = token.source_position;
5096 statement->case_label.expression = parse_expression();
5100 if (! is_constant_expression(statement->case_label.expression)) {
5101 errorf(statement->base.source_position,
5102 "case label does not reduce to an integer constant");
5104 /* TODO: check if the case label is already known */
5105 if (current_switch != NULL) {
5106 /* link all cases into the switch statement */
5107 if (current_switch->last_case == NULL) {
5108 current_switch->first_case =
5109 current_switch->last_case = &statement->case_label;
5111 current_switch->last_case->next = &statement->case_label;
5114 errorf(statement->base.source_position,
5115 "case label not within a switch statement");
5118 statement->case_label.statement = parse_statement();
5124 * Finds an existing default label of a switch statement.
5126 static case_label_statement_t *
5127 find_default_label(const switch_statement_t *statement)
5129 for (case_label_statement_t *label = statement->first_case;
5131 label = label->next) {
5132 if (label->expression == NULL)
5139 * Parse a default statement.
5141 static statement_t *parse_default_statement(void)
5145 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5147 statement->base.source_position = token.source_position;
5150 if (current_switch != NULL) {
5151 const case_label_statement_t *def_label = find_default_label(current_switch);
5152 if (def_label != NULL) {
5153 errorf(HERE, "multiple default labels in one switch");
5154 errorf(def_label->base.source_position,
5155 "this is the first default label");
5157 /* link all cases into the switch statement */
5158 if (current_switch->last_case == NULL) {
5159 current_switch->first_case =
5160 current_switch->last_case = &statement->case_label;
5162 current_switch->last_case->next = &statement->case_label;
5166 errorf(statement->base.source_position,
5167 "'default' label not within a switch statement");
5169 statement->label.statement = parse_statement();
5175 * Return the declaration for a given label symbol or create a new one.
5177 static declaration_t *get_label(symbol_t *symbol)
5179 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5180 assert(current_function != NULL);
5181 /* if we found a label in the same function, then we already created the
5183 if(candidate != NULL
5184 && candidate->parent_scope == ¤t_function->scope) {
5188 /* otherwise we need to create a new one */
5189 declaration_t *const declaration = allocate_declaration_zero();
5190 declaration->namespc = NAMESPACE_LABEL;
5191 declaration->symbol = symbol;
5193 label_push(declaration);
5199 * Parse a label statement.
5201 static statement_t *parse_label_statement(void)
5203 assert(token.type == T_IDENTIFIER);
5204 symbol_t *symbol = token.v.symbol;
5207 declaration_t *label = get_label(symbol);
5209 /* if source position is already set then the label is defined twice,
5210 * otherwise it was just mentioned in a goto so far */
5211 if(label->source_position.input_name != NULL) {
5212 errorf(HERE, "duplicate label '%Y'", symbol);
5213 errorf(label->source_position, "previous definition of '%Y' was here",
5216 label->source_position = token.source_position;
5219 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5221 statement->base.source_position = token.source_position;
5222 statement->label.label = label;
5226 if(token.type == '}') {
5227 /* TODO only warn? */
5228 errorf(HERE, "label at end of compound statement");
5231 if (token.type == ';') {
5232 /* eat an empty statement here, to avoid the warning about an empty
5233 * after a label. label:; is commonly used to have a label before
5237 statement->label.statement = parse_statement();
5241 /* remember the labels's in a list for later checking */
5242 if (label_last == NULL) {
5243 label_first = &statement->label;
5245 label_last->next = &statement->label;
5247 label_last = &statement->label;
5253 * Parse an if statement.
5255 static statement_t *parse_if(void)
5259 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5260 statement->base.source_position = token.source_position;
5263 statement->ifs.condition = parse_expression();
5266 statement->ifs.true_statement = parse_statement();
5267 if(token.type == T_else) {
5269 statement->ifs.false_statement = parse_statement();
5276 * Parse a switch statement.
5278 static statement_t *parse_switch(void)
5282 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5283 statement->base.source_position = token.source_position;
5286 expression_t *const expr = parse_expression();
5287 type_t * type = skip_typeref(expr->base.type);
5288 if (is_type_integer(type)) {
5289 type = promote_integer(type);
5290 } else if (is_type_valid(type)) {
5291 errorf(expr->base.source_position,
5292 "switch quantity is not an integer, but '%T'", type);
5293 type = type_error_type;
5295 statement->switchs.expression = create_implicit_cast(expr, type);
5298 switch_statement_t *rem = current_switch;
5299 current_switch = &statement->switchs;
5300 statement->switchs.body = parse_statement();
5301 current_switch = rem;
5303 if (warning.switch_default
5304 && find_default_label(&statement->switchs) == NULL) {
5305 warningf(statement->base.source_position, "switch has no default case");
5311 static statement_t *parse_loop_body(statement_t *const loop)
5313 statement_t *const rem = current_loop;
5314 current_loop = loop;
5316 statement_t *const body = parse_statement();
5323 * Parse a while statement.
5325 static statement_t *parse_while(void)
5329 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5330 statement->base.source_position = token.source_position;
5333 statement->whiles.condition = parse_expression();
5336 statement->whiles.body = parse_loop_body(statement);
5342 * Parse a do statement.
5344 static statement_t *parse_do(void)
5348 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5350 statement->base.source_position = token.source_position;
5352 statement->do_while.body = parse_loop_body(statement);
5356 statement->do_while.condition = parse_expression();
5364 * Parse a for statement.
5366 static statement_t *parse_for(void)
5370 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5371 statement->base.source_position = token.source_position;
5375 int top = environment_top();
5376 scope_t *last_scope = scope;
5377 set_scope(&statement->fors.scope);
5379 if(token.type != ';') {
5380 if(is_declaration_specifier(&token, false)) {
5381 parse_declaration(record_declaration);
5383 statement->fors.initialisation = parse_expression();
5390 if(token.type != ';') {
5391 statement->fors.condition = parse_expression();
5394 if(token.type != ')') {
5395 statement->fors.step = parse_expression();
5398 statement->fors.body = parse_loop_body(statement);
5400 assert(scope == &statement->fors.scope);
5401 set_scope(last_scope);
5402 environment_pop_to(top);
5408 * Parse a goto statement.
5410 static statement_t *parse_goto(void)
5414 if(token.type != T_IDENTIFIER) {
5415 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5419 symbol_t *symbol = token.v.symbol;
5422 declaration_t *label = get_label(symbol);
5424 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5425 statement->base.source_position = token.source_position;
5427 statement->gotos.label = label;
5429 /* remember the goto's in a list for later checking */
5430 if (goto_last == NULL) {
5431 goto_first = &statement->gotos;
5433 goto_last->next = &statement->gotos;
5435 goto_last = &statement->gotos;
5443 * Parse a continue statement.
5445 static statement_t *parse_continue(void)
5447 statement_t *statement;
5448 if (current_loop == NULL) {
5449 errorf(HERE, "continue statement not within loop");
5452 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5454 statement->base.source_position = token.source_position;
5464 * Parse a break statement.
5466 static statement_t *parse_break(void)
5468 statement_t *statement;
5469 if (current_switch == NULL && current_loop == NULL) {
5470 errorf(HERE, "break statement not within loop or switch");
5473 statement = allocate_statement_zero(STATEMENT_BREAK);
5475 statement->base.source_position = token.source_position;
5485 * Check if a given declaration represents a local variable.
5487 static bool is_local_var_declaration(const declaration_t *declaration) {
5488 switch ((storage_class_tag_t) declaration->storage_class) {
5489 case STORAGE_CLASS_NONE:
5490 case STORAGE_CLASS_AUTO:
5491 case STORAGE_CLASS_REGISTER: {
5492 const type_t *type = skip_typeref(declaration->type);
5493 if(is_type_function(type)) {
5505 * Check if a given declaration represents a variable.
5507 static bool is_var_declaration(const declaration_t *declaration) {
5508 switch ((storage_class_tag_t) declaration->storage_class) {
5509 case STORAGE_CLASS_NONE:
5510 case STORAGE_CLASS_EXTERN:
5511 case STORAGE_CLASS_STATIC:
5512 case STORAGE_CLASS_AUTO:
5513 case STORAGE_CLASS_REGISTER:
5514 case STORAGE_CLASS_THREAD:
5515 case STORAGE_CLASS_THREAD_EXTERN:
5516 case STORAGE_CLASS_THREAD_STATIC: {
5517 const type_t *type = skip_typeref(declaration->type);
5518 if(is_type_function(type)) {
5530 * Check if a given expression represents a local variable.
5532 static bool is_local_variable(const expression_t *expression)
5534 if (expression->base.kind != EXPR_REFERENCE) {
5537 const declaration_t *declaration = expression->reference.declaration;
5538 return is_local_var_declaration(declaration);
5542 * Check if a given expression represents a local variable and
5543 * return its declaration then, else return NULL.
5545 declaration_t *expr_is_variable(const expression_t *expression)
5547 if (expression->base.kind != EXPR_REFERENCE) {
5550 declaration_t *declaration = expression->reference.declaration;
5551 if (is_var_declaration(declaration))
5557 * Parse a return statement.
5559 static statement_t *parse_return(void)
5563 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5564 statement->base.source_position = token.source_position;
5566 expression_t *return_value = NULL;
5567 if(token.type != ';') {
5568 return_value = parse_expression();
5572 const type_t *const func_type = current_function->type;
5573 assert(is_type_function(func_type));
5574 type_t *const return_type = skip_typeref(func_type->function.return_type);
5576 if(return_value != NULL) {
5577 type_t *return_value_type = skip_typeref(return_value->base.type);
5579 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5580 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5581 warningf(statement->base.source_position,
5582 "'return' with a value, in function returning void");
5583 return_value = NULL;
5585 type_t *const res_type = semantic_assign(return_type,
5586 return_value, "'return'");
5587 if (res_type == NULL) {
5588 errorf(statement->base.source_position,
5589 "cannot return something of type '%T' in function returning '%T'",
5590 return_value->base.type, return_type);
5592 return_value = create_implicit_cast(return_value, res_type);
5595 /* check for returning address of a local var */
5596 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5597 const expression_t *expression = return_value->unary.value;
5598 if (is_local_variable(expression)) {
5599 warningf(statement->base.source_position,
5600 "function returns address of local variable");
5604 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5605 warningf(statement->base.source_position,
5606 "'return' without value, in function returning non-void");
5609 statement->returns.value = return_value;
5615 * Parse a declaration statement.
5617 static statement_t *parse_declaration_statement(void)
5619 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5621 statement->base.source_position = token.source_position;
5623 declaration_t *before = last_declaration;
5624 parse_declaration(record_declaration);
5626 if(before == NULL) {
5627 statement->declaration.declarations_begin = scope->declarations;
5629 statement->declaration.declarations_begin = before->next;
5631 statement->declaration.declarations_end = last_declaration;
5637 * Parse an expression statement, ie. expr ';'.
5639 static statement_t *parse_expression_statement(void)
5641 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5643 statement->base.source_position = token.source_position;
5644 expression_t *const expr = parse_expression();
5645 statement->expression.expression = expr;
5647 if (warning.unused_value && !expression_has_effect(expr)) {
5648 warningf(expr->base.source_position, "statement has no effect");
5657 * Parse a statement.
5659 static statement_t *parse_statement(void)
5661 statement_t *statement = NULL;
5663 /* declaration or statement */
5664 switch(token.type) {
5666 statement = parse_asm_statement();
5670 statement = parse_case_statement();
5674 statement = parse_default_statement();
5678 statement = parse_compound_statement();
5682 statement = parse_if();
5686 statement = parse_switch();
5690 statement = parse_while();
5694 statement = parse_do();
5698 statement = parse_for();
5702 statement = parse_goto();
5706 statement = parse_continue();
5710 statement = parse_break();
5714 statement = parse_return();
5718 if (warning.empty_statement) {
5719 warningf(HERE, "statement is empty");
5726 if(look_ahead(1)->type == ':') {
5727 statement = parse_label_statement();
5731 if(is_typedef_symbol(token.v.symbol)) {
5732 statement = parse_declaration_statement();
5736 statement = parse_expression_statement();
5739 case T___extension__:
5740 /* this can be a prefix to a declaration or an expression statement */
5741 /* we simply eat it now and parse the rest with tail recursion */
5744 } while(token.type == T___extension__);
5745 statement = parse_statement();
5749 statement = parse_declaration_statement();
5753 statement = parse_expression_statement();
5757 assert(statement == NULL
5758 || statement->base.source_position.input_name != NULL);
5764 * Parse a compound statement.
5766 static statement_t *parse_compound_statement(void)
5768 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5770 statement->base.source_position = token.source_position;
5774 int top = environment_top();
5775 scope_t *last_scope = scope;
5776 set_scope(&statement->compound.scope);
5778 statement_t *last_statement = NULL;
5780 while(token.type != '}' && token.type != T_EOF) {
5781 statement_t *sub_statement = parse_statement();
5782 if(sub_statement == NULL)
5785 if(last_statement != NULL) {
5786 last_statement->base.next = sub_statement;
5788 statement->compound.statements = sub_statement;
5791 while(sub_statement->base.next != NULL)
5792 sub_statement = sub_statement->base.next;
5794 last_statement = sub_statement;
5797 if(token.type == '}') {
5800 errorf(statement->base.source_position,
5801 "end of file while looking for closing '}'");
5804 assert(scope == &statement->compound.scope);
5805 set_scope(last_scope);
5806 environment_pop_to(top);
5812 * Initialize builtin types.
5814 static void initialize_builtin_types(void)
5816 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5817 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5818 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5819 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5820 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5821 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5822 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5823 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5825 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5826 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5827 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5828 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5832 * Check for unused global static functions and variables
5834 static void check_unused_globals(void)
5836 if (!warning.unused_function && !warning.unused_variable)
5839 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5840 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5843 type_t *const type = decl->type;
5845 if (is_type_function(skip_typeref(type))) {
5846 if (!warning.unused_function || decl->is_inline)
5849 s = (decl->init.statement != NULL ? "defined" : "declared");
5851 if (!warning.unused_variable)
5857 warningf(decl->source_position, "'%#T' %s but not used",
5858 type, decl->symbol, s);
5863 * Parse a translation unit.
5865 static translation_unit_t *parse_translation_unit(void)
5867 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5869 assert(global_scope == NULL);
5870 global_scope = &unit->scope;
5872 assert(scope == NULL);
5873 set_scope(&unit->scope);
5875 initialize_builtin_types();
5877 while(token.type != T_EOF) {
5878 if (token.type == ';') {
5879 /* TODO error in strict mode */
5880 warningf(HERE, "stray ';' outside of function");
5883 parse_external_declaration();
5887 assert(scope == &unit->scope);
5889 last_declaration = NULL;
5891 assert(global_scope == &unit->scope);
5892 check_unused_globals();
5893 global_scope = NULL;
5901 * @return the translation unit or NULL if errors occurred.
5903 translation_unit_t *parse(void)
5905 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5906 label_stack = NEW_ARR_F(stack_entry_t, 0);
5907 diagnostic_count = 0;
5911 type_set_output(stderr);
5912 ast_set_output(stderr);
5914 lookahead_bufpos = 0;
5915 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5918 translation_unit_t *unit = parse_translation_unit();
5920 DEL_ARR_F(environment_stack);
5921 DEL_ARR_F(label_stack);
5930 * Initialize the parser.
5932 void init_parser(void)
5934 init_expression_parsers();
5935 obstack_init(&temp_obst);
5937 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5938 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5942 * Terminate the parser.
5944 void exit_parser(void)
5946 obstack_free(&temp_obst, NULL);