7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(void);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(*allocate_declaration_zero()));
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.datatype = 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.datatype = 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.datatype);
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.datatype;
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.datatype;
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 && !types_compatible(points_to_left, points_to_right)) {
753 return orig_type_left;
756 if (is_type_compound(type_left) && is_type_compound(type_right)) {
757 type_t *const unqual_type_left = get_unqualified_type(type_left);
758 type_t *const unqual_type_right = get_unqualified_type(type_right);
759 if (types_compatible(unqual_type_left, unqual_type_right)) {
760 return orig_type_left;
764 if (!is_type_valid(type_left))
767 if (!is_type_valid(type_right))
768 return orig_type_right;
773 static expression_t *parse_constant_expression(void)
775 /* start parsing at precedence 7 (conditional expression) */
776 expression_t *result = parse_sub_expression(7);
778 if(!is_constant_expression(result)) {
779 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
785 static expression_t *parse_assignment_expression(void)
787 /* start parsing at precedence 2 (assignment expression) */
788 return parse_sub_expression(2);
791 static type_t *make_global_typedef(const char *name, type_t *type)
793 symbol_t *const symbol = symbol_table_insert(name);
795 declaration_t *const declaration = allocate_declaration_zero();
796 declaration->namespc = NAMESPACE_NORMAL;
797 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
798 declaration->type = type;
799 declaration->symbol = symbol;
800 declaration->source_position = builtin_source_position;
802 record_declaration(declaration);
804 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF);
805 typedef_type->typedeft.declaration = declaration;
810 static string_t parse_string_literals(void)
812 assert(token.type == T_STRING_LITERAL);
813 string_t result = token.v.string;
817 while (token.type == T_STRING_LITERAL) {
818 result = concat_strings(&result, &token.v.string);
825 static void parse_attributes(void)
829 case T___attribute__: {
837 errorf(HERE, "EOF while parsing attribute");
856 if(token.type != T_STRING_LITERAL) {
857 parse_error_expected("while parsing assembler attribute",
862 parse_string_literals();
867 goto attributes_finished;
876 static designator_t *parse_designation(void)
878 if(token.type != '[' && token.type != '.')
881 designator_t *result = NULL;
882 designator_t *last = NULL;
885 designator_t *designator;
888 designator = allocate_ast_zero(sizeof(designator[0]));
890 designator->array_access = parse_constant_expression();
894 designator = allocate_ast_zero(sizeof(designator[0]));
896 if(token.type != T_IDENTIFIER) {
897 parse_error_expected("while parsing designator",
901 designator->symbol = token.v.symbol;
909 assert(designator != NULL);
911 last->next = designator;
920 static initializer_t *initializer_from_string(array_type_t *type,
921 const string_t *const string)
923 /* TODO: check len vs. size of array type */
926 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
927 initializer->string.string = *string;
932 static initializer_t *initializer_from_wide_string(array_type_t *const type,
933 wide_string_t *const string)
935 /* TODO: check len vs. size of array type */
938 initializer_t *const initializer =
939 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
940 initializer->wide_string.string = *string;
945 static initializer_t *initializer_from_expression(type_t *type,
946 expression_t *expression)
948 /* TODO check that expression is a constant expression */
950 /* § 6.7.8.14/15 char array may be initialized by string literals */
951 type_t *const expr_type = expression->base.datatype;
952 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
953 array_type_t *const array_type = &type->array;
954 type_t *const element_type = skip_typeref(array_type->element_type);
956 if (element_type->kind == TYPE_ATOMIC) {
957 switch (expression->kind) {
958 case EXPR_STRING_LITERAL:
959 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
960 return initializer_from_string(array_type,
961 &expression->string.value);
964 case EXPR_WIDE_STRING_LITERAL: {
965 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
966 if (get_unqualified_type(element_type) == bare_wchar_type) {
967 return initializer_from_wide_string(array_type,
968 &expression->wide_string.value);
978 type_t *const res_type = semantic_assign(type, expression, "initializer");
979 if (res_type == NULL)
982 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
983 result->value.value = create_implicit_cast(expression, res_type);
988 static initializer_t *parse_sub_initializer(type_t *type,
989 expression_t *expression);
991 static initializer_t *parse_sub_initializer_elem(type_t *type)
993 if(token.type == '{') {
994 return parse_sub_initializer(type, NULL);
997 expression_t *expression = parse_assignment_expression();
998 return parse_sub_initializer(type, expression);
1001 static bool had_initializer_brace_warning;
1003 static void skip_designator(void)
1006 if(token.type == '.') {
1008 if(token.type == T_IDENTIFIER)
1010 } else if(token.type == '[') {
1012 parse_constant_expression();
1013 if(token.type == ']')
1021 static initializer_t *parse_sub_initializer(type_t *type,
1022 expression_t *expression)
1024 if(is_type_scalar(type)) {
1025 /* there might be extra {} hierarchies */
1026 if(token.type == '{') {
1028 if(!had_initializer_brace_warning) {
1029 warningf(HERE, "braces around scalar initializer");
1030 had_initializer_brace_warning = true;
1032 initializer_t *result = parse_sub_initializer(type, NULL);
1033 if(token.type == ',') {
1035 /* TODO: warn about excessive elements */
1041 if(expression == NULL) {
1042 expression = parse_assignment_expression();
1044 return initializer_from_expression(type, expression);
1047 /* does the expression match the currently looked at object to initialize */
1048 if(expression != NULL) {
1049 initializer_t *result = initializer_from_expression(type, expression);
1054 bool read_paren = false;
1055 if(token.type == '{') {
1060 /* descend into subtype */
1061 initializer_t *result = NULL;
1062 initializer_t **elems;
1063 if(is_type_array(type)) {
1064 if(token.type == '.') {
1066 "compound designator in initializer for array type '%T'",
1071 type_t *const element_type = skip_typeref(type->array.element_type);
1074 had_initializer_brace_warning = false;
1075 if(expression == NULL) {
1076 sub = parse_sub_initializer_elem(element_type);
1078 sub = parse_sub_initializer(element_type, expression);
1081 /* didn't match the subtypes -> try the parent type */
1083 assert(!read_paren);
1087 elems = NEW_ARR_F(initializer_t*, 0);
1088 ARR_APP1(initializer_t*, elems, sub);
1091 if(token.type == '}')
1094 if(token.type == '}')
1097 sub = parse_sub_initializer_elem(element_type);
1099 /* TODO error, do nicer cleanup */
1100 errorf(HERE, "member initializer didn't match");
1104 ARR_APP1(initializer_t*, elems, sub);
1107 assert(is_type_compound(type));
1108 scope_t *const scope = &type->compound.declaration->scope;
1110 if(token.type == '[') {
1112 "array designator in initializer for compound type '%T'",
1117 declaration_t *first = scope->declarations;
1120 type_t *first_type = first->type;
1121 first_type = skip_typeref(first_type);
1124 had_initializer_brace_warning = false;
1125 if(expression == NULL) {
1126 sub = parse_sub_initializer_elem(first_type);
1128 sub = parse_sub_initializer(first_type, expression);
1131 /* didn't match the subtypes -> try our parent type */
1133 assert(!read_paren);
1137 elems = NEW_ARR_F(initializer_t*, 0);
1138 ARR_APP1(initializer_t*, elems, sub);
1140 declaration_t *iter = first->next;
1141 for( ; iter != NULL; iter = iter->next) {
1142 if(iter->symbol == NULL)
1144 if(iter->namespc != NAMESPACE_NORMAL)
1147 if(token.type == '}')
1150 if(token.type == '}')
1153 type_t *iter_type = iter->type;
1154 iter_type = skip_typeref(iter_type);
1156 sub = parse_sub_initializer_elem(iter_type);
1158 /* TODO error, do nicer cleanup */
1159 errorf(HERE, "member initializer didn't match");
1163 ARR_APP1(initializer_t*, elems, sub);
1167 int len = ARR_LEN(elems);
1168 size_t elems_size = sizeof(initializer_t*) * len;
1170 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1172 init->initializer.kind = INITIALIZER_LIST;
1174 memcpy(init->initializers, elems, elems_size);
1177 result = (initializer_t*) init;
1180 if(token.type == ',')
1187 static initializer_t *parse_initializer(type_t *const orig_type)
1189 initializer_t *result;
1191 type_t *const type = skip_typeref(orig_type);
1193 if(token.type != '{') {
1194 expression_t *expression = parse_assignment_expression();
1195 initializer_t *initializer = initializer_from_expression(type, expression);
1196 if(initializer == NULL) {
1198 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1199 expression, expression->base.datatype, orig_type);
1204 if(is_type_scalar(type)) {
1208 expression_t *expression = parse_assignment_expression();
1209 result = initializer_from_expression(type, expression);
1211 if(token.type == ',')
1217 result = parse_sub_initializer(type, NULL);
1223 static declaration_t *append_declaration(declaration_t *declaration);
1225 static declaration_t *parse_compound_type_specifier(bool is_struct)
1233 symbol_t *symbol = NULL;
1234 declaration_t *declaration = NULL;
1236 if (token.type == T___attribute__) {
1241 if(token.type == T_IDENTIFIER) {
1242 symbol = token.v.symbol;
1246 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1248 declaration = get_declaration(symbol, NAMESPACE_UNION);
1250 } else if(token.type != '{') {
1252 parse_error_expected("while parsing struct type specifier",
1253 T_IDENTIFIER, '{', 0);
1255 parse_error_expected("while parsing union type specifier",
1256 T_IDENTIFIER, '{', 0);
1262 if(declaration == NULL) {
1263 declaration = allocate_declaration_zero();
1264 declaration->namespc =
1265 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1266 declaration->source_position = token.source_position;
1267 declaration->symbol = symbol;
1268 declaration->parent_scope = scope;
1269 if (symbol != NULL) {
1270 environment_push(declaration);
1272 append_declaration(declaration);
1275 if(token.type == '{') {
1276 if(declaration->init.is_defined) {
1277 assert(symbol != NULL);
1278 errorf(HERE, "multiple definition of '%s %Y'",
1279 is_struct ? "struct" : "union", symbol);
1280 declaration->scope.declarations = NULL;
1282 declaration->init.is_defined = true;
1284 int top = environment_top();
1285 scope_t *last_scope = scope;
1286 set_scope(&declaration->scope);
1288 parse_compound_type_entries();
1291 assert(scope == &declaration->scope);
1292 set_scope(last_scope);
1293 environment_pop_to(top);
1299 static void parse_enum_entries(type_t *const enum_type)
1303 if(token.type == '}') {
1305 errorf(HERE, "empty enum not allowed");
1310 if(token.type != T_IDENTIFIER) {
1311 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1316 declaration_t *const entry = allocate_declaration_zero();
1317 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1318 entry->type = enum_type;
1319 entry->symbol = token.v.symbol;
1320 entry->source_position = token.source_position;
1323 if(token.type == '=') {
1325 entry->init.enum_value = parse_constant_expression();
1330 record_declaration(entry);
1332 if(token.type != ',')
1335 } while(token.type != '}');
1340 static type_t *parse_enum_specifier(void)
1344 declaration_t *declaration;
1347 if(token.type == T_IDENTIFIER) {
1348 symbol = token.v.symbol;
1351 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1352 } else if(token.type != '{') {
1353 parse_error_expected("while parsing enum type specifier",
1354 T_IDENTIFIER, '{', 0);
1361 if(declaration == NULL) {
1362 declaration = allocate_declaration_zero();
1363 declaration->namespc = NAMESPACE_ENUM;
1364 declaration->source_position = token.source_position;
1365 declaration->symbol = symbol;
1366 declaration->parent_scope = scope;
1369 type_t *const type = allocate_type_zero(TYPE_ENUM);
1370 type->enumt.declaration = declaration;
1372 if(token.type == '{') {
1373 if(declaration->init.is_defined) {
1374 errorf(HERE, "multiple definitions of enum %Y", symbol);
1376 if (symbol != NULL) {
1377 environment_push(declaration);
1379 append_declaration(declaration);
1380 declaration->init.is_defined = 1;
1382 parse_enum_entries(type);
1390 * if a symbol is a typedef to another type, return true
1392 static bool is_typedef_symbol(symbol_t *symbol)
1394 const declaration_t *const declaration =
1395 get_declaration(symbol, NAMESPACE_NORMAL);
1397 declaration != NULL &&
1398 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1401 static type_t *parse_typeof(void)
1409 expression_t *expression = NULL;
1412 switch(token.type) {
1413 case T___extension__:
1414 /* this can be a prefix to a typename or an expression */
1415 /* we simply eat it now. */
1418 } while(token.type == T___extension__);
1422 if(is_typedef_symbol(token.v.symbol)) {
1423 type = parse_typename();
1425 expression = parse_expression();
1426 type = expression->base.datatype;
1431 type = parse_typename();
1435 expression = parse_expression();
1436 type = expression->base.datatype;
1442 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF);
1443 typeof_type->typeoft.expression = expression;
1444 typeof_type->typeoft.typeof_type = type;
1450 SPECIFIER_SIGNED = 1 << 0,
1451 SPECIFIER_UNSIGNED = 1 << 1,
1452 SPECIFIER_LONG = 1 << 2,
1453 SPECIFIER_INT = 1 << 3,
1454 SPECIFIER_DOUBLE = 1 << 4,
1455 SPECIFIER_CHAR = 1 << 5,
1456 SPECIFIER_SHORT = 1 << 6,
1457 SPECIFIER_LONG_LONG = 1 << 7,
1458 SPECIFIER_FLOAT = 1 << 8,
1459 SPECIFIER_BOOL = 1 << 9,
1460 SPECIFIER_VOID = 1 << 10,
1461 #ifdef PROVIDE_COMPLEX
1462 SPECIFIER_COMPLEX = 1 << 11,
1463 SPECIFIER_IMAGINARY = 1 << 12,
1467 static type_t *create_builtin_type(symbol_t *const symbol,
1468 type_t *const real_type)
1470 type_t *type = allocate_type_zero(TYPE_BUILTIN);
1471 type->builtin.symbol = symbol;
1472 type->builtin.real_type = real_type;
1474 type_t *result = typehash_insert(type);
1475 if (type != result) {
1482 static type_t *get_typedef_type(symbol_t *symbol)
1484 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1485 if(declaration == NULL
1486 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1489 type_t *type = allocate_type_zero(TYPE_TYPEDEF);
1490 type->typedeft.declaration = declaration;
1495 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1497 type_t *type = NULL;
1498 unsigned type_qualifiers = 0;
1499 unsigned type_specifiers = 0;
1502 specifiers->source_position = token.source_position;
1505 switch(token.type) {
1508 #define MATCH_STORAGE_CLASS(token, class) \
1510 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1511 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1513 specifiers->storage_class = class; \
1517 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1518 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1519 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1520 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1521 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1524 switch (specifiers->storage_class) {
1525 case STORAGE_CLASS_NONE:
1526 specifiers->storage_class = STORAGE_CLASS_THREAD;
1529 case STORAGE_CLASS_EXTERN:
1530 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1533 case STORAGE_CLASS_STATIC:
1534 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1538 errorf(HERE, "multiple storage classes in declaration specifiers");
1544 /* type qualifiers */
1545 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1547 type_qualifiers |= qualifier; \
1551 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1552 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1553 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1555 case T___extension__:
1560 /* type specifiers */
1561 #define MATCH_SPECIFIER(token, specifier, name) \
1564 if(type_specifiers & specifier) { \
1565 errorf(HERE, "multiple " name " type specifiers given"); \
1567 type_specifiers |= specifier; \
1571 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1572 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1573 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1574 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1575 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1576 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1577 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1578 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1579 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1580 #ifdef PROVIDE_COMPLEX
1581 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1582 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1585 /* only in microsoft mode */
1586 specifiers->decl_modifiers |= DM_FORCEINLINE;
1590 specifiers->is_inline = true;
1595 if(type_specifiers & SPECIFIER_LONG_LONG) {
1596 errorf(HERE, "multiple type specifiers given");
1597 } else if(type_specifiers & SPECIFIER_LONG) {
1598 type_specifiers |= SPECIFIER_LONG_LONG;
1600 type_specifiers |= SPECIFIER_LONG;
1604 /* TODO: if is_type_valid(type) for the following rules should issue
1607 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1609 type->compound.declaration = parse_compound_type_specifier(true);
1613 type = allocate_type_zero(TYPE_COMPOUND_STRUCT);
1615 type->compound.declaration = parse_compound_type_specifier(false);
1619 type = parse_enum_specifier();
1622 type = parse_typeof();
1624 case T___builtin_va_list:
1625 type = duplicate_type(type_valist);
1629 case T___attribute__:
1634 case T_IDENTIFIER: {
1635 type_t *typedef_type = get_typedef_type(token.v.symbol);
1637 if(typedef_type == NULL)
1638 goto finish_specifiers;
1641 type = typedef_type;
1645 /* function specifier */
1647 goto finish_specifiers;
1654 atomic_type_kind_t atomic_type;
1656 /* match valid basic types */
1657 switch(type_specifiers) {
1658 case SPECIFIER_VOID:
1659 atomic_type = ATOMIC_TYPE_VOID;
1661 case SPECIFIER_CHAR:
1662 atomic_type = ATOMIC_TYPE_CHAR;
1664 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1665 atomic_type = ATOMIC_TYPE_SCHAR;
1667 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1668 atomic_type = ATOMIC_TYPE_UCHAR;
1670 case SPECIFIER_SHORT:
1671 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1672 case SPECIFIER_SHORT | SPECIFIER_INT:
1673 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1674 atomic_type = ATOMIC_TYPE_SHORT;
1676 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1677 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1678 atomic_type = ATOMIC_TYPE_USHORT;
1681 case SPECIFIER_SIGNED:
1682 case SPECIFIER_SIGNED | SPECIFIER_INT:
1683 atomic_type = ATOMIC_TYPE_INT;
1685 case SPECIFIER_UNSIGNED:
1686 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1687 atomic_type = ATOMIC_TYPE_UINT;
1689 case SPECIFIER_LONG:
1690 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1691 case SPECIFIER_LONG | SPECIFIER_INT:
1692 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1693 atomic_type = ATOMIC_TYPE_LONG;
1695 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1696 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1697 atomic_type = ATOMIC_TYPE_ULONG;
1699 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1700 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1701 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1702 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1704 atomic_type = ATOMIC_TYPE_LONGLONG;
1706 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1707 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1709 atomic_type = ATOMIC_TYPE_ULONGLONG;
1711 case SPECIFIER_FLOAT:
1712 atomic_type = ATOMIC_TYPE_FLOAT;
1714 case SPECIFIER_DOUBLE:
1715 atomic_type = ATOMIC_TYPE_DOUBLE;
1717 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1718 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1720 case SPECIFIER_BOOL:
1721 atomic_type = ATOMIC_TYPE_BOOL;
1723 #ifdef PROVIDE_COMPLEX
1724 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1725 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1727 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1728 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1730 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1731 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1733 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1734 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1736 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1737 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1739 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1740 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1744 /* invalid specifier combination, give an error message */
1745 if(type_specifiers == 0) {
1746 if (! strict_mode) {
1747 if (warning.implicit_int) {
1748 warningf(HERE, "no type specifiers in declaration, using 'int'");
1750 atomic_type = ATOMIC_TYPE_INT;
1753 errorf(HERE, "no type specifiers given in declaration");
1755 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1756 (type_specifiers & SPECIFIER_UNSIGNED)) {
1757 errorf(HERE, "signed and unsigned specifiers gives");
1758 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1759 errorf(HERE, "only integer types can be signed or unsigned");
1761 errorf(HERE, "multiple datatypes in declaration");
1763 atomic_type = ATOMIC_TYPE_INVALID;
1766 type = allocate_type_zero(TYPE_ATOMIC);
1767 type->atomic.akind = atomic_type;
1770 if(type_specifiers != 0) {
1771 errorf(HERE, "multiple datatypes in declaration");
1775 type->base.qualifiers = type_qualifiers;
1777 type_t *result = typehash_insert(type);
1778 if(newtype && result != type) {
1782 specifiers->type = result;
1785 static type_qualifiers_t parse_type_qualifiers(void)
1787 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1790 switch(token.type) {
1791 /* type qualifiers */
1792 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1793 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1794 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1797 return type_qualifiers;
1802 static declaration_t *parse_identifier_list(void)
1804 declaration_t *declarations = NULL;
1805 declaration_t *last_declaration = NULL;
1807 declaration_t *const declaration = allocate_declaration_zero();
1808 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1809 declaration->source_position = token.source_position;
1810 declaration->symbol = token.v.symbol;
1813 if(last_declaration != NULL) {
1814 last_declaration->next = declaration;
1816 declarations = declaration;
1818 last_declaration = declaration;
1820 if(token.type != ',')
1823 } while(token.type == T_IDENTIFIER);
1825 return declarations;
1828 static void semantic_parameter(declaration_t *declaration)
1830 /* TODO: improve error messages */
1832 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1833 errorf(HERE, "typedef not allowed in parameter list");
1834 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1835 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1836 errorf(HERE, "parameter may only have none or register storage class");
1839 type_t *const orig_type = declaration->type;
1840 type_t * type = skip_typeref(orig_type);
1842 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1843 * into a pointer. § 6.7.5.3 (7) */
1844 if (is_type_array(type)) {
1845 type_t *const element_type = type->array.element_type;
1847 type = make_pointer_type(element_type, type->base.qualifiers);
1849 declaration->type = type;
1852 if(is_type_incomplete(type)) {
1853 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1854 orig_type, declaration->symbol);
1858 static declaration_t *parse_parameter(void)
1860 declaration_specifiers_t specifiers;
1861 memset(&specifiers, 0, sizeof(specifiers));
1863 parse_declaration_specifiers(&specifiers);
1865 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1867 semantic_parameter(declaration);
1872 static declaration_t *parse_parameters(function_type_t *type)
1874 if(token.type == T_IDENTIFIER) {
1875 symbol_t *symbol = token.v.symbol;
1876 if(!is_typedef_symbol(symbol)) {
1877 type->kr_style_parameters = true;
1878 return parse_identifier_list();
1882 if(token.type == ')') {
1883 type->unspecified_parameters = 1;
1886 if(token.type == T_void && look_ahead(1)->type == ')') {
1891 declaration_t *declarations = NULL;
1892 declaration_t *declaration;
1893 declaration_t *last_declaration = NULL;
1894 function_parameter_t *parameter;
1895 function_parameter_t *last_parameter = NULL;
1898 switch(token.type) {
1902 return declarations;
1905 case T___extension__:
1907 declaration = parse_parameter();
1909 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1910 memset(parameter, 0, sizeof(parameter[0]));
1911 parameter->type = declaration->type;
1913 if(last_parameter != NULL) {
1914 last_declaration->next = declaration;
1915 last_parameter->next = parameter;
1917 type->parameters = parameter;
1918 declarations = declaration;
1920 last_parameter = parameter;
1921 last_declaration = declaration;
1925 return declarations;
1927 if(token.type != ',')
1928 return declarations;
1938 } construct_type_type_t;
1940 typedef struct construct_type_t construct_type_t;
1941 struct construct_type_t {
1942 construct_type_type_t type;
1943 construct_type_t *next;
1946 typedef struct parsed_pointer_t parsed_pointer_t;
1947 struct parsed_pointer_t {
1948 construct_type_t construct_type;
1949 type_qualifiers_t type_qualifiers;
1952 typedef struct construct_function_type_t construct_function_type_t;
1953 struct construct_function_type_t {
1954 construct_type_t construct_type;
1955 type_t *function_type;
1958 typedef struct parsed_array_t parsed_array_t;
1959 struct parsed_array_t {
1960 construct_type_t construct_type;
1961 type_qualifiers_t type_qualifiers;
1967 typedef struct construct_base_type_t construct_base_type_t;
1968 struct construct_base_type_t {
1969 construct_type_t construct_type;
1973 static construct_type_t *parse_pointer_declarator(void)
1977 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1978 memset(pointer, 0, sizeof(pointer[0]));
1979 pointer->construct_type.type = CONSTRUCT_POINTER;
1980 pointer->type_qualifiers = parse_type_qualifiers();
1982 return (construct_type_t*) pointer;
1985 static construct_type_t *parse_array_declarator(void)
1989 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
1990 memset(array, 0, sizeof(array[0]));
1991 array->construct_type.type = CONSTRUCT_ARRAY;
1993 if(token.type == T_static) {
1994 array->is_static = true;
1998 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
1999 if(type_qualifiers != 0) {
2000 if(token.type == T_static) {
2001 array->is_static = true;
2005 array->type_qualifiers = type_qualifiers;
2007 if(token.type == '*' && look_ahead(1)->type == ']') {
2008 array->is_variable = true;
2010 } else if(token.type != ']') {
2011 array->size = parse_assignment_expression();
2016 return (construct_type_t*) array;
2019 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2023 type_t *type = allocate_type_zero(TYPE_FUNCTION);
2025 declaration_t *parameters = parse_parameters(&type->function);
2026 if(declaration != NULL) {
2027 declaration->scope.declarations = parameters;
2030 construct_function_type_t *construct_function_type =
2031 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2032 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2033 construct_function_type->construct_type.type = CONSTRUCT_FUNCTION;
2034 construct_function_type->function_type = type;
2038 return (construct_type_t*) construct_function_type;
2041 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2042 bool may_be_abstract)
2044 /* construct a single linked list of construct_type_t's which describe
2045 * how to construct the final declarator type */
2046 construct_type_t *first = NULL;
2047 construct_type_t *last = NULL;
2050 while(token.type == '*') {
2051 construct_type_t *type = parse_pointer_declarator();
2062 /* TODO: find out if this is correct */
2065 construct_type_t *inner_types = NULL;
2067 switch(token.type) {
2069 if(declaration == NULL) {
2070 errorf(HERE, "no identifier expected in typename");
2072 declaration->symbol = token.v.symbol;
2073 declaration->source_position = token.source_position;
2079 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2085 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2086 /* avoid a loop in the outermost scope, because eat_statement doesn't
2088 if(token.type == '}' && current_function == NULL) {
2096 construct_type_t *p = last;
2099 construct_type_t *type;
2100 switch(token.type) {
2102 type = parse_function_declarator(declaration);
2105 type = parse_array_declarator();
2108 goto declarator_finished;
2111 /* insert in the middle of the list (behind p) */
2113 type->next = p->next;
2124 declarator_finished:
2127 /* append inner_types at the end of the list, we don't to set last anymore
2128 * as it's not needed anymore */
2130 assert(first == NULL);
2131 first = inner_types;
2133 last->next = inner_types;
2139 static type_t *construct_declarator_type(construct_type_t *construct_list,
2142 construct_type_t *iter = construct_list;
2143 for( ; iter != NULL; iter = iter->next) {
2144 switch(iter->type) {
2145 case CONSTRUCT_INVALID:
2146 panic("invalid type construction found");
2147 case CONSTRUCT_FUNCTION: {
2148 construct_function_type_t *construct_function_type
2149 = (construct_function_type_t*) iter;
2151 type_t *function_type = construct_function_type->function_type;
2153 function_type->function.return_type = type;
2155 type_t *skipped_return_type = skip_typeref(type);
2156 if (is_type_function(skipped_return_type)) {
2157 errorf(HERE, "function returning function is not allowed");
2158 type = type_error_type;
2159 } else if (is_type_array(skipped_return_type)) {
2160 errorf(HERE, "function returning array is not allowed");
2161 type = type_error_type;
2163 type = function_type;
2168 case CONSTRUCT_POINTER: {
2169 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2170 type_t *pointer_type = allocate_type_zero(TYPE_POINTER);
2171 pointer_type->pointer.points_to = type;
2172 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2174 type = pointer_type;
2178 case CONSTRUCT_ARRAY: {
2179 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2180 type_t *array_type = allocate_type_zero(TYPE_ARRAY);
2182 array_type->base.qualifiers = parsed_array->type_qualifiers;
2183 array_type->array.element_type = type;
2184 array_type->array.is_static = parsed_array->is_static;
2185 array_type->array.is_variable = parsed_array->is_variable;
2186 array_type->array.size = parsed_array->size;
2188 type_t *skipped_type = skip_typeref(type);
2189 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2190 errorf(HERE, "array of void is not allowed");
2191 type = type_error_type;
2199 type_t *hashed_type = typehash_insert(type);
2200 if(hashed_type != type) {
2201 /* the function type was constructed earlier freeing it here will
2202 * destroy other types... */
2203 if(iter->type != CONSTRUCT_FUNCTION) {
2213 static declaration_t *parse_declarator(
2214 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2216 declaration_t *const declaration = allocate_declaration_zero();
2217 declaration->storage_class = specifiers->storage_class;
2218 declaration->modifiers = specifiers->decl_modifiers;
2219 declaration->is_inline = specifiers->is_inline;
2221 construct_type_t *construct_type
2222 = parse_inner_declarator(declaration, may_be_abstract);
2223 type_t *const type = specifiers->type;
2224 declaration->type = construct_declarator_type(construct_type, type);
2226 if(construct_type != NULL) {
2227 obstack_free(&temp_obst, construct_type);
2233 static type_t *parse_abstract_declarator(type_t *base_type)
2235 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2237 type_t *result = construct_declarator_type(construct_type, base_type);
2238 if(construct_type != NULL) {
2239 obstack_free(&temp_obst, construct_type);
2245 static declaration_t *append_declaration(declaration_t* const declaration)
2247 if (last_declaration != NULL) {
2248 last_declaration->next = declaration;
2250 scope->declarations = declaration;
2252 last_declaration = declaration;
2257 * Check if the declaration of main is suspicious. main should be a
2258 * function with external linkage, returning int, taking either zero
2259 * arguments, two, or three arguments of appropriate types, ie.
2261 * int main([ int argc, char **argv [, char **env ] ]).
2263 * @param decl the declaration to check
2264 * @param type the function type of the declaration
2266 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2268 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2269 warningf(decl->source_position, "'main' is normally a non-static function");
2271 if (skip_typeref(func_type->return_type) != type_int) {
2272 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2274 const function_parameter_t *parm = func_type->parameters;
2276 type_t *const first_type = parm->type;
2277 if (!types_compatible(skip_typeref(first_type), type_int)) {
2278 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2282 type_t *const second_type = parm->type;
2283 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2284 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2288 type_t *const third_type = parm->type;
2289 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2290 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2294 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2298 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2304 * Check if a symbol is the equal to "main".
2306 static bool is_sym_main(const symbol_t *const sym)
2308 return strcmp(sym->string, "main") == 0;
2311 static declaration_t *internal_record_declaration(
2312 declaration_t *const declaration,
2313 const bool is_function_definition)
2315 const symbol_t *const symbol = declaration->symbol;
2316 const namespace_t namespc = (namespace_t)declaration->namespc;
2318 type_t *const orig_type = declaration->type;
2319 const type_t *const type = skip_typeref(orig_type);
2320 if (is_type_function(type) &&
2321 type->function.unspecified_parameters &&
2322 warning.strict_prototypes) {
2323 warningf(declaration->source_position,
2324 "function declaration '%#T' is not a prototype",
2325 orig_type, declaration->symbol);
2328 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2329 check_type_of_main(declaration, &type->function);
2332 declaration_t *const previous_declaration = get_declaration(symbol, namespc);
2333 assert(declaration != previous_declaration);
2334 if (previous_declaration != NULL) {
2335 if (previous_declaration->parent_scope == scope) {
2336 /* can happen for K&R style declarations */
2337 if(previous_declaration->type == NULL) {
2338 previous_declaration->type = declaration->type;
2341 const type_t *const prev_type = skip_typeref(previous_declaration->type);
2342 if (!types_compatible(type, prev_type)) {
2343 errorf(declaration->source_position,
2344 "declaration '%#T' is incompatible with previous declaration '%#T'",
2345 orig_type, symbol, previous_declaration->type, symbol);
2346 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2348 unsigned old_storage_class = previous_declaration->storage_class;
2349 unsigned new_storage_class = declaration->storage_class;
2351 /* pretend no storage class means extern for function declarations
2352 * (except if the previous declaration is neither none nor extern) */
2353 if (is_type_function(type)) {
2354 switch (old_storage_class) {
2355 case STORAGE_CLASS_NONE:
2356 old_storage_class = STORAGE_CLASS_EXTERN;
2358 case STORAGE_CLASS_EXTERN:
2359 if (is_function_definition) {
2360 if (warning.missing_prototypes &&
2361 prev_type->function.unspecified_parameters &&
2362 !is_sym_main(symbol)) {
2363 warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol);
2365 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2366 new_storage_class = STORAGE_CLASS_EXTERN;
2374 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2375 new_storage_class == STORAGE_CLASS_EXTERN) {
2376 warn_redundant_declaration:
2377 if (warning.redundant_decls) {
2378 warningf(declaration->source_position, "redundant declaration for '%Y'", symbol);
2379 warningf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2381 } else if (current_function == NULL) {
2382 if (old_storage_class != STORAGE_CLASS_STATIC &&
2383 new_storage_class == STORAGE_CLASS_STATIC) {
2384 errorf(declaration->source_position, "static declaration of '%Y' follows non-static declaration", symbol);
2385 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2387 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2388 goto warn_redundant_declaration;
2390 if (new_storage_class == STORAGE_CLASS_NONE) {
2391 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2395 if (old_storage_class == new_storage_class) {
2396 errorf(declaration->source_position, "redeclaration of '%Y'", symbol);
2398 errorf(declaration->source_position, "redeclaration of '%Y' with different linkage", symbol);
2400 errorf(previous_declaration->source_position, "previous declaration of '%Y' was here", symbol);
2403 return previous_declaration;
2405 } else if (is_function_definition) {
2406 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2407 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2408 warningf(declaration->source_position, "no previous prototype for '%#T'", orig_type, symbol);
2409 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2410 warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol);
2413 } else if (warning.missing_declarations &&
2414 scope == global_scope &&
2415 !is_type_function(type) && (
2416 declaration->storage_class == STORAGE_CLASS_NONE ||
2417 declaration->storage_class == STORAGE_CLASS_THREAD
2419 warningf(declaration->source_position, "no previous declaration for '%#T'", orig_type, symbol);
2422 assert(declaration->parent_scope == NULL);
2423 assert(declaration->symbol != NULL);
2424 assert(scope != NULL);
2426 declaration->parent_scope = scope;
2428 environment_push(declaration);
2429 return append_declaration(declaration);
2432 static declaration_t *record_declaration(declaration_t *declaration)
2434 return internal_record_declaration(declaration, false);
2437 static declaration_t *record_function_definition(declaration_t *declaration)
2439 return internal_record_declaration(declaration, true);
2442 static void parser_error_multiple_definition(declaration_t *declaration,
2443 const source_position_t source_position)
2445 errorf(source_position, "multiple definition of symbol '%Y'",
2446 declaration->symbol);
2447 errorf(declaration->source_position,
2448 "this is the location of the previous definition.");
2451 static bool is_declaration_specifier(const token_t *token,
2452 bool only_type_specifiers)
2454 switch(token->type) {
2458 return is_typedef_symbol(token->v.symbol);
2460 case T___extension__:
2463 return !only_type_specifiers;
2470 static void parse_init_declarator_rest(declaration_t *declaration)
2474 type_t *orig_type = declaration->type;
2475 type_t *type = type = skip_typeref(orig_type);
2477 if(declaration->init.initializer != NULL) {
2478 parser_error_multiple_definition(declaration, token.source_position);
2481 initializer_t *initializer = parse_initializer(type);
2483 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2484 * the array type size */
2485 if(is_type_array(type) && initializer != NULL) {
2486 array_type_t *array_type = &type->array;
2488 if(array_type->size == NULL) {
2489 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2491 cnst->base.datatype = type_size_t;
2493 switch (initializer->kind) {
2494 case INITIALIZER_LIST: {
2495 cnst->conste.v.int_value = initializer->list.len;
2499 case INITIALIZER_STRING: {
2500 cnst->conste.v.int_value = initializer->string.string.size;
2504 case INITIALIZER_WIDE_STRING: {
2505 cnst->conste.v.int_value = initializer->wide_string.string.size;
2510 panic("invalid initializer type");
2513 array_type->size = cnst;
2517 if(is_type_function(type)) {
2518 errorf(declaration->source_position,
2519 "initializers not allowed for function types at declator '%Y' (type '%T')",
2520 declaration->symbol, orig_type);
2522 declaration->init.initializer = initializer;
2526 /* parse rest of a declaration without any declarator */
2527 static void parse_anonymous_declaration_rest(
2528 const declaration_specifiers_t *specifiers,
2529 parsed_declaration_func finished_declaration)
2533 declaration_t *const declaration = allocate_declaration_zero();
2534 declaration->type = specifiers->type;
2535 declaration->storage_class = specifiers->storage_class;
2536 declaration->source_position = specifiers->source_position;
2538 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2539 warningf(declaration->source_position, "useless storage class in empty declaration");
2542 type_t *type = declaration->type;
2543 switch (type->kind) {
2544 case TYPE_COMPOUND_STRUCT:
2545 case TYPE_COMPOUND_UNION: {
2546 if (type->compound.declaration->symbol == NULL) {
2547 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2556 warningf(declaration->source_position, "empty declaration");
2560 finished_declaration(declaration);
2563 static void parse_declaration_rest(declaration_t *ndeclaration,
2564 const declaration_specifiers_t *specifiers,
2565 parsed_declaration_func finished_declaration)
2568 declaration_t *declaration = finished_declaration(ndeclaration);
2570 type_t *orig_type = declaration->type;
2571 type_t *type = skip_typeref(orig_type);
2573 if (type->kind != TYPE_FUNCTION &&
2574 declaration->is_inline &&
2575 is_type_valid(type)) {
2576 warningf(declaration->source_position,
2577 "variable '%Y' declared 'inline'\n", declaration->symbol);
2580 if(token.type == '=') {
2581 parse_init_declarator_rest(declaration);
2584 if(token.type != ',')
2588 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2593 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2595 symbol_t *symbol = declaration->symbol;
2596 if(symbol == NULL) {
2597 errorf(HERE, "anonymous declaration not valid as function parameter");
2600 namespace_t namespc = (namespace_t) declaration->namespc;
2601 if(namespc != NAMESPACE_NORMAL) {
2602 return record_declaration(declaration);
2605 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2606 if(previous_declaration == NULL ||
2607 previous_declaration->parent_scope != scope) {
2608 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2613 if(previous_declaration->type == NULL) {
2614 previous_declaration->type = declaration->type;
2615 previous_declaration->storage_class = declaration->storage_class;
2616 previous_declaration->parent_scope = scope;
2617 return previous_declaration;
2619 return record_declaration(declaration);
2623 static void parse_declaration(parsed_declaration_func finished_declaration)
2625 declaration_specifiers_t specifiers;
2626 memset(&specifiers, 0, sizeof(specifiers));
2627 parse_declaration_specifiers(&specifiers);
2629 if(token.type == ';') {
2630 parse_anonymous_declaration_rest(&specifiers, finished_declaration);
2632 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2633 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2637 static void parse_kr_declaration_list(declaration_t *declaration)
2639 type_t *type = skip_typeref(declaration->type);
2640 if(!is_type_function(type))
2643 if(!type->function.kr_style_parameters)
2646 /* push function parameters */
2647 int top = environment_top();
2648 scope_t *last_scope = scope;
2649 set_scope(&declaration->scope);
2651 declaration_t *parameter = declaration->scope.declarations;
2652 for( ; parameter != NULL; parameter = parameter->next) {
2653 assert(parameter->parent_scope == NULL);
2654 parameter->parent_scope = scope;
2655 environment_push(parameter);
2658 /* parse declaration list */
2659 while(is_declaration_specifier(&token, false)) {
2660 parse_declaration(finished_kr_declaration);
2663 /* pop function parameters */
2664 assert(scope == &declaration->scope);
2665 set_scope(last_scope);
2666 environment_pop_to(top);
2668 /* update function type */
2669 type_t *new_type = duplicate_type(type);
2670 new_type->function.kr_style_parameters = false;
2672 function_parameter_t *parameters = NULL;
2673 function_parameter_t *last_parameter = NULL;
2675 declaration_t *parameter_declaration = declaration->scope.declarations;
2676 for( ; parameter_declaration != NULL;
2677 parameter_declaration = parameter_declaration->next) {
2678 type_t *parameter_type = parameter_declaration->type;
2679 if(parameter_type == NULL) {
2681 errorf(HERE, "no type specified for function parameter '%Y'",
2682 parameter_declaration->symbol);
2684 if (warning.implicit_int) {
2685 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2686 parameter_declaration->symbol);
2688 parameter_type = type_int;
2689 parameter_declaration->type = parameter_type;
2693 semantic_parameter(parameter_declaration);
2694 parameter_type = parameter_declaration->type;
2696 function_parameter_t *function_parameter
2697 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2698 memset(function_parameter, 0, sizeof(function_parameter[0]));
2700 function_parameter->type = parameter_type;
2701 if(last_parameter != NULL) {
2702 last_parameter->next = function_parameter;
2704 parameters = function_parameter;
2706 last_parameter = function_parameter;
2708 new_type->function.parameters = parameters;
2710 type = typehash_insert(new_type);
2711 if(type != new_type) {
2712 obstack_free(type_obst, new_type);
2715 declaration->type = type;
2718 static bool first_err = true;
2721 * When called with first_err set, prints the name of the current function,
2724 static void print_in_function(void) {
2727 diagnosticf("%s: In function '%Y':\n",
2728 current_function->source_position.input_name,
2729 current_function->symbol);
2734 * Check if all labels are defined in the current function.
2735 * Check if all labels are used in the current function.
2737 static void check_labels(void)
2739 for (const goto_statement_t *goto_statement = goto_first;
2740 goto_statement != NULL;
2741 goto_statement = goto_statement->next) {
2742 declaration_t *label = goto_statement->label;
2745 if (label->source_position.input_name == NULL) {
2746 print_in_function();
2747 errorf(goto_statement->statement.source_position,
2748 "label '%Y' used but not defined", label->symbol);
2751 goto_first = goto_last = NULL;
2753 if (warning.unused_label) {
2754 for (const label_statement_t *label_statement = label_first;
2755 label_statement != NULL;
2756 label_statement = label_statement->next) {
2757 const declaration_t *label = label_statement->label;
2759 if (! label->used) {
2760 print_in_function();
2761 warningf(label_statement->statement.source_position,
2762 "label '%Y' defined but not used", label->symbol);
2766 label_first = label_last = NULL;
2770 * Check declarations of current_function for unused entities.
2772 static void check_declarations(void)
2774 if (warning.unused_parameter) {
2775 const scope_t *scope = ¤t_function->scope;
2777 const declaration_t *parameter = scope->declarations;
2778 for (; parameter != NULL; parameter = parameter->next) {
2779 if (! parameter->used) {
2780 print_in_function();
2781 warningf(parameter->source_position,
2782 "unused parameter '%Y'", parameter->symbol);
2786 if (warning.unused_variable) {
2790 static void parse_external_declaration(void)
2792 /* function-definitions and declarations both start with declaration
2794 declaration_specifiers_t specifiers;
2795 memset(&specifiers, 0, sizeof(specifiers));
2796 parse_declaration_specifiers(&specifiers);
2798 /* must be a declaration */
2799 if(token.type == ';') {
2800 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2804 /* declarator is common to both function-definitions and declarations */
2805 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2807 /* must be a declaration */
2808 if(token.type == ',' || token.type == '=' || token.type == ';') {
2809 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2813 /* must be a function definition */
2814 parse_kr_declaration_list(ndeclaration);
2816 if(token.type != '{') {
2817 parse_error_expected("while parsing function definition", '{', 0);
2822 type_t *type = ndeclaration->type;
2824 /* note that we don't skip typerefs: the standard doesn't allow them here
2825 * (so we can't use is_type_function here) */
2826 if(type->kind != TYPE_FUNCTION) {
2827 if (is_type_valid(type)) {
2828 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2829 type, ndeclaration->symbol);
2835 /* § 6.7.5.3 (14) a function definition with () means no
2836 * parameters (and not unspecified parameters) */
2837 if(type->function.unspecified_parameters) {
2838 type_t *duplicate = duplicate_type(type);
2839 duplicate->function.unspecified_parameters = false;
2841 type = typehash_insert(duplicate);
2842 if(type != duplicate) {
2843 obstack_free(type_obst, duplicate);
2845 ndeclaration->type = type;
2848 declaration_t *const declaration = record_function_definition(ndeclaration);
2849 if(ndeclaration != declaration) {
2850 declaration->scope = ndeclaration->scope;
2852 type = skip_typeref(declaration->type);
2854 /* push function parameters and switch scope */
2855 int top = environment_top();
2856 scope_t *last_scope = scope;
2857 set_scope(&declaration->scope);
2859 declaration_t *parameter = declaration->scope.declarations;
2860 for( ; parameter != NULL; parameter = parameter->next) {
2861 if(parameter->parent_scope == &ndeclaration->scope) {
2862 parameter->parent_scope = scope;
2864 assert(parameter->parent_scope == NULL
2865 || parameter->parent_scope == scope);
2866 parameter->parent_scope = scope;
2867 environment_push(parameter);
2870 if(declaration->init.statement != NULL) {
2871 parser_error_multiple_definition(declaration, token.source_position);
2873 goto end_of_parse_external_declaration;
2875 /* parse function body */
2876 int label_stack_top = label_top();
2877 declaration_t *old_current_function = current_function;
2878 current_function = declaration;
2880 declaration->init.statement = parse_compound_statement();
2883 check_declarations();
2885 assert(current_function == declaration);
2886 current_function = old_current_function;
2887 label_pop_to(label_stack_top);
2890 end_of_parse_external_declaration:
2891 assert(scope == &declaration->scope);
2892 set_scope(last_scope);
2893 environment_pop_to(top);
2896 static type_t *make_bitfield_type(type_t *base, expression_t *size)
2898 type_t *type = allocate_type_zero(TYPE_BITFIELD);
2899 type->bitfield.base = base;
2900 type->bitfield.size = size;
2905 static void parse_struct_declarators(const declaration_specifiers_t *specifiers)
2907 /* TODO: check constraints for struct declarations (in specifiers) */
2909 declaration_t *declaration;
2911 if(token.type == ':') {
2914 type_t *base_type = specifiers->type;
2915 expression_t *size = parse_constant_expression();
2917 type_t *type = make_bitfield_type(base_type, size);
2919 declaration = allocate_declaration_zero();
2920 declaration->namespc = NAMESPACE_NORMAL;
2921 declaration->storage_class = STORAGE_CLASS_NONE;
2922 declaration->source_position = token.source_position;
2923 declaration->modifiers = specifiers->decl_modifiers;
2924 declaration->type = type;
2926 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2928 if(token.type == ':') {
2930 expression_t *size = parse_constant_expression();
2932 type_t *type = make_bitfield_type(declaration->type, size);
2933 declaration->type = type;
2936 record_declaration(declaration);
2938 if(token.type != ',')
2945 static void parse_compound_type_entries(void)
2949 while(token.type != '}' && token.type != T_EOF) {
2950 declaration_specifiers_t specifiers;
2951 memset(&specifiers, 0, sizeof(specifiers));
2952 parse_declaration_specifiers(&specifiers);
2954 parse_struct_declarators(&specifiers);
2956 if(token.type == T_EOF) {
2957 errorf(HERE, "EOF while parsing struct");
2962 static type_t *parse_typename(void)
2964 declaration_specifiers_t specifiers;
2965 memset(&specifiers, 0, sizeof(specifiers));
2966 parse_declaration_specifiers(&specifiers);
2967 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
2968 /* TODO: improve error message, user does probably not know what a
2969 * storage class is...
2971 errorf(HERE, "typename may not have a storage class");
2974 type_t *result = parse_abstract_declarator(specifiers.type);
2982 typedef expression_t* (*parse_expression_function) (unsigned precedence);
2983 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
2984 expression_t *left);
2986 typedef struct expression_parser_function_t expression_parser_function_t;
2987 struct expression_parser_function_t {
2988 unsigned precedence;
2989 parse_expression_function parser;
2990 unsigned infix_precedence;
2991 parse_expression_infix_function infix_parser;
2994 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
2997 * Creates a new invalid expression.
2999 static expression_t *create_invalid_expression(void)
3001 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3002 expression->base.source_position = token.source_position;
3007 * Prints an error message if an expression was expected but not read
3009 static expression_t *expected_expression_error(void)
3011 /* skip the error message if the error token was read */
3012 if (token.type != T_ERROR) {
3013 errorf(HERE, "expected expression, got token '%K'", &token);
3017 return create_invalid_expression();
3021 * Parse a string constant.
3023 static expression_t *parse_string_const(void)
3025 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3026 cnst->base.datatype = type_string;
3027 cnst->string.value = parse_string_literals();
3033 * Parse a wide string constant.
3035 static expression_t *parse_wide_string_const(void)
3037 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3038 cnst->base.datatype = type_wchar_t_ptr;
3039 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3045 * Parse an integer constant.
3047 static expression_t *parse_int_const(void)
3049 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3050 cnst->base.datatype = token.datatype;
3051 cnst->conste.v.int_value = token.v.intvalue;
3059 * Parse a float constant.
3061 static expression_t *parse_float_const(void)
3063 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3064 cnst->base.datatype = token.datatype;
3065 cnst->conste.v.float_value = token.v.floatvalue;
3072 static declaration_t *create_implicit_function(symbol_t *symbol,
3073 const source_position_t source_position)
3075 type_t *ntype = allocate_type_zero(TYPE_FUNCTION);
3076 ntype->function.return_type = type_int;
3077 ntype->function.unspecified_parameters = true;
3079 type_t *type = typehash_insert(ntype);
3084 declaration_t *const declaration = allocate_declaration_zero();
3085 declaration->storage_class = STORAGE_CLASS_EXTERN;
3086 declaration->type = type;
3087 declaration->symbol = symbol;
3088 declaration->source_position = source_position;
3089 declaration->parent_scope = global_scope;
3091 scope_t *old_scope = scope;
3092 set_scope(global_scope);
3094 environment_push(declaration);
3095 /* prepends the declaration to the global declarations list */
3096 declaration->next = scope->declarations;
3097 scope->declarations = declaration;
3099 assert(scope == global_scope);
3100 set_scope(old_scope);
3106 * Creates a return_type (func)(argument_type) function type if not
3109 * @param return_type the return type
3110 * @param argument_type the argument type
3112 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3114 function_parameter_t *parameter
3115 = obstack_alloc(type_obst, sizeof(parameter[0]));
3116 memset(parameter, 0, sizeof(parameter[0]));
3117 parameter->type = argument_type;
3119 type_t *type = allocate_type_zero(TYPE_FUNCTION);
3120 type->function.return_type = return_type;
3121 type->function.parameters = parameter;
3123 type_t *result = typehash_insert(type);
3124 if(result != type) {
3132 * Creates a function type for some function like builtins.
3134 * @param symbol the symbol describing the builtin
3136 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3138 switch(symbol->ID) {
3139 case T___builtin_alloca:
3140 return make_function_1_type(type_void_ptr, type_size_t);
3141 case T___builtin_nan:
3142 return make_function_1_type(type_double, type_string);
3143 case T___builtin_nanf:
3144 return make_function_1_type(type_float, type_string);
3145 case T___builtin_nand:
3146 return make_function_1_type(type_long_double, type_string);
3147 case T___builtin_va_end:
3148 return make_function_1_type(type_void, type_valist);
3150 panic("not implemented builtin symbol found");
3155 * Performs automatic type cast as described in § 6.3.2.1.
3157 * @param orig_type the original type
3159 static type_t *automatic_type_conversion(type_t *orig_type)
3161 type_t *type = skip_typeref(orig_type);
3162 if(is_type_array(type)) {
3163 array_type_t *array_type = &type->array;
3164 type_t *element_type = array_type->element_type;
3165 unsigned qualifiers = array_type->type.qualifiers;
3167 return make_pointer_type(element_type, qualifiers);
3170 if(is_type_function(type)) {
3171 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3178 * reverts the automatic casts of array to pointer types and function
3179 * to function-pointer types as defined § 6.3.2.1
3181 type_t *revert_automatic_type_conversion(const expression_t *expression)
3183 switch (expression->kind) {
3184 case EXPR_REFERENCE: return expression->reference.declaration->type;
3185 case EXPR_SELECT: return expression->select.compound_entry->type;
3187 case EXPR_UNARY_DEREFERENCE: {
3188 const expression_t *const value = expression->unary.value;
3189 type_t *const type = skip_typeref(value->base.datatype);
3190 assert(is_type_pointer(type));
3191 return type->pointer.points_to;
3194 case EXPR_BUILTIN_SYMBOL:
3195 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3197 case EXPR_ARRAY_ACCESS: {
3198 const expression_t *const array_ref = expression->array_access.array_ref;
3199 type_t *const type_left = skip_typeref(array_ref->base.datatype);
3200 if (!is_type_valid(type_left))
3202 assert(is_type_pointer(type_left));
3203 return type_left->pointer.points_to;
3209 return expression->base.datatype;
3212 static expression_t *parse_reference(void)
3214 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3216 reference_expression_t *ref = &expression->reference;
3217 ref->symbol = token.v.symbol;
3219 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3221 source_position_t source_position = token.source_position;
3224 if(declaration == NULL) {
3225 if (! strict_mode && token.type == '(') {
3226 /* an implicitly defined function */
3227 if (warning.implicit_function_declaration) {
3228 warningf(HERE, "implicit declaration of function '%Y'",
3232 declaration = create_implicit_function(ref->symbol,
3235 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3240 type_t *type = declaration->type;
3242 /* we always do the auto-type conversions; the & and sizeof parser contains
3243 * code to revert this! */
3244 type = automatic_type_conversion(type);
3246 ref->declaration = declaration;
3247 ref->expression.datatype = type;
3249 /* this declaration is used */
3250 declaration->used = true;
3255 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3259 /* TODO check if explicit cast is allowed and issue warnings/errors */
3262 static expression_t *parse_cast(void)
3264 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3266 cast->base.source_position = token.source_position;
3268 type_t *type = parse_typename();
3271 expression_t *value = parse_sub_expression(20);
3273 check_cast_allowed(value, type);
3275 cast->base.datatype = type;
3276 cast->unary.value = value;
3281 static expression_t *parse_statement_expression(void)
3283 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3285 statement_t *statement = parse_compound_statement();
3286 expression->statement.statement = statement;
3287 expression->base.source_position = statement->base.source_position;
3289 /* find last statement and use its type */
3290 type_t *type = type_void;
3291 const statement_t *stmt = statement->compound.statements;
3293 while (stmt->base.next != NULL)
3294 stmt = stmt->base.next;
3296 if (stmt->kind == STATEMENT_EXPRESSION) {
3297 type = stmt->expression.expression->base.datatype;
3300 warningf(expression->base.source_position, "empty statement expression ({})");
3302 expression->base.datatype = type;
3309 static expression_t *parse_brace_expression(void)
3313 switch(token.type) {
3315 /* gcc extension: a statement expression */
3316 return parse_statement_expression();
3320 return parse_cast();
3322 if(is_typedef_symbol(token.v.symbol)) {
3323 return parse_cast();
3327 expression_t *result = parse_expression();
3333 static expression_t *parse_function_keyword(void)
3338 if (current_function == NULL) {
3339 errorf(HERE, "'__func__' used outside of a function");
3342 string_literal_expression_t *expression
3343 = allocate_ast_zero(sizeof(expression[0]));
3345 expression->expression.kind = EXPR_FUNCTION;
3346 expression->expression.datatype = type_string;
3348 return (expression_t*) expression;
3351 static expression_t *parse_pretty_function_keyword(void)
3353 eat(T___PRETTY_FUNCTION__);
3356 if (current_function == NULL) {
3357 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3360 string_literal_expression_t *expression
3361 = allocate_ast_zero(sizeof(expression[0]));
3363 expression->expression.kind = EXPR_PRETTY_FUNCTION;
3364 expression->expression.datatype = type_string;
3366 return (expression_t*) expression;
3369 static designator_t *parse_designator(void)
3371 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3373 if(token.type != T_IDENTIFIER) {
3374 parse_error_expected("while parsing member designator",
3379 result->symbol = token.v.symbol;
3382 designator_t *last_designator = result;
3384 if(token.type == '.') {
3386 if(token.type != T_IDENTIFIER) {
3387 parse_error_expected("while parsing member designator",
3392 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3393 designator->symbol = token.v.symbol;
3396 last_designator->next = designator;
3397 last_designator = designator;
3400 if(token.type == '[') {
3402 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3403 designator->array_access = parse_expression();
3404 if(designator->array_access == NULL) {
3410 last_designator->next = designator;
3411 last_designator = designator;
3420 static expression_t *parse_offsetof(void)
3422 eat(T___builtin_offsetof);
3424 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3425 expression->base.datatype = type_size_t;
3428 expression->offsetofe.type = parse_typename();
3430 expression->offsetofe.designator = parse_designator();
3436 static expression_t *parse_va_start(void)
3438 eat(T___builtin_va_start);
3440 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3443 expression->va_starte.ap = parse_assignment_expression();
3445 expression_t *const expr = parse_assignment_expression();
3446 if (expr->kind == EXPR_REFERENCE) {
3447 declaration_t *const decl = expr->reference.declaration;
3449 return create_invalid_expression();
3450 if (decl->parent_scope == ¤t_function->scope &&
3451 decl->next == NULL) {
3452 expression->va_starte.parameter = decl;
3457 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3459 return create_invalid_expression();
3462 static expression_t *parse_va_arg(void)
3464 eat(T___builtin_va_arg);
3466 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3469 expression->va_arge.ap = parse_assignment_expression();
3471 expression->base.datatype = parse_typename();
3477 static expression_t *parse_builtin_symbol(void)
3479 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3481 symbol_t *symbol = token.v.symbol;
3483 expression->builtin_symbol.symbol = symbol;
3486 type_t *type = get_builtin_symbol_type(symbol);
3487 type = automatic_type_conversion(type);
3489 expression->base.datatype = type;
3493 static expression_t *parse_builtin_constant(void)
3495 eat(T___builtin_constant_p);
3497 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3500 expression->builtin_constant.value = parse_assignment_expression();
3502 expression->base.datatype = type_int;
3507 static expression_t *parse_builtin_prefetch(void)
3509 eat(T___builtin_prefetch);
3511 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3514 expression->builtin_prefetch.adr = parse_assignment_expression();
3515 if (token.type == ',') {
3517 expression->builtin_prefetch.rw = parse_assignment_expression();
3519 if (token.type == ',') {
3521 expression->builtin_prefetch.locality = parse_assignment_expression();
3524 expression->base.datatype = type_void;
3529 static expression_t *parse_compare_builtin(void)
3531 expression_t *expression;
3533 switch(token.type) {
3534 case T___builtin_isgreater:
3535 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3537 case T___builtin_isgreaterequal:
3538 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3540 case T___builtin_isless:
3541 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3543 case T___builtin_islessequal:
3544 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3546 case T___builtin_islessgreater:
3547 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3549 case T___builtin_isunordered:
3550 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3553 panic("invalid compare builtin found");
3556 expression->base.source_position = HERE;
3560 expression->binary.left = parse_assignment_expression();
3562 expression->binary.right = parse_assignment_expression();
3565 type_t *const orig_type_left = expression->binary.left->base.datatype;
3566 type_t *const orig_type_right = expression->binary.right->base.datatype;
3568 type_t *const type_left = skip_typeref(orig_type_left);
3569 type_t *const type_right = skip_typeref(orig_type_right);
3570 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3571 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3572 type_error_incompatible("invalid operands in comparison",
3573 expression->base.source_position, orig_type_left, orig_type_right);
3576 semantic_comparison(&expression->binary);
3582 static expression_t *parse_builtin_expect(void)
3584 eat(T___builtin_expect);
3586 expression_t *expression
3587 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3590 expression->binary.left = parse_assignment_expression();
3592 expression->binary.right = parse_constant_expression();
3595 expression->base.datatype = expression->binary.left->base.datatype;
3600 static expression_t *parse_assume(void) {
3603 expression_t *expression
3604 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3607 expression->unary.value = parse_assignment_expression();
3610 expression->base.datatype = type_void;
3614 static expression_t *parse_primary_expression(void)
3616 switch(token.type) {
3618 return parse_int_const();
3619 case T_FLOATINGPOINT:
3620 return parse_float_const();
3621 case T_STRING_LITERAL:
3622 return parse_string_const();
3623 case T_WIDE_STRING_LITERAL:
3624 return parse_wide_string_const();
3626 return parse_reference();
3627 case T___FUNCTION__:
3629 return parse_function_keyword();
3630 case T___PRETTY_FUNCTION__:
3631 return parse_pretty_function_keyword();
3632 case T___builtin_offsetof:
3633 return parse_offsetof();
3634 case T___builtin_va_start:
3635 return parse_va_start();
3636 case T___builtin_va_arg:
3637 return parse_va_arg();
3638 case T___builtin_expect:
3639 return parse_builtin_expect();
3640 case T___builtin_nanf:
3641 case T___builtin_alloca:
3642 case T___builtin_va_end:
3643 return parse_builtin_symbol();
3644 case T___builtin_isgreater:
3645 case T___builtin_isgreaterequal:
3646 case T___builtin_isless:
3647 case T___builtin_islessequal:
3648 case T___builtin_islessgreater:
3649 case T___builtin_isunordered:
3650 return parse_compare_builtin();
3651 case T___builtin_constant_p:
3652 return parse_builtin_constant();
3653 case T___builtin_prefetch:
3654 return parse_builtin_prefetch();
3656 return parse_assume();
3659 return parse_brace_expression();
3662 errorf(HERE, "unexpected token '%K'", &token);
3665 return create_invalid_expression();
3669 * Check if the expression has the character type and issue a warning then.
3671 static void check_for_char_index_type(const expression_t *expression) {
3672 type_t *const type = expression->base.datatype;
3673 const type_t *const base_type = skip_typeref(type);
3675 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3676 warning.char_subscripts) {
3677 warningf(expression->base.source_position,
3678 "array subscript has type '%T'", type);
3682 static expression_t *parse_array_expression(unsigned precedence,
3689 expression_t *inside = parse_expression();
3691 array_access_expression_t *array_access
3692 = allocate_ast_zero(sizeof(array_access[0]));
3694 array_access->expression.kind = EXPR_ARRAY_ACCESS;
3696 type_t *const orig_type_left = left->base.datatype;
3697 type_t *const orig_type_inside = inside->base.datatype;
3699 type_t *const type_left = skip_typeref(orig_type_left);
3700 type_t *const type_inside = skip_typeref(orig_type_inside);
3702 type_t *return_type;
3703 if (is_type_pointer(type_left)) {
3704 return_type = type_left->pointer.points_to;
3705 array_access->array_ref = left;
3706 array_access->index = inside;
3707 check_for_char_index_type(inside);
3708 } else if (is_type_pointer(type_inside)) {
3709 return_type = type_inside->pointer.points_to;
3710 array_access->array_ref = inside;
3711 array_access->index = left;
3712 array_access->flipped = true;
3713 check_for_char_index_type(left);
3715 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3717 "array access on object with non-pointer types '%T', '%T'",
3718 orig_type_left, orig_type_inside);
3720 return_type = type_error_type;
3721 array_access->array_ref = create_invalid_expression();
3724 if(token.type != ']') {
3725 parse_error_expected("Problem while parsing array access", ']', 0);
3726 return (expression_t*) array_access;
3730 return_type = automatic_type_conversion(return_type);
3731 array_access->expression.datatype = return_type;
3733 return (expression_t*) array_access;
3736 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3738 expression_t *tp_expression
3739 = allocate_expression_zero(kind);
3740 tp_expression->base.datatype = type_size_t;
3742 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3744 tp_expression->typeprop.type = parse_typename();
3747 expression_t *expression = parse_sub_expression(precedence);
3748 expression->base.datatype = revert_automatic_type_conversion(expression);
3750 tp_expression->typeprop.type = expression->base.datatype;
3751 tp_expression->typeprop.tp_expression = expression;
3754 return tp_expression;
3757 static expression_t *parse_sizeof(unsigned precedence)
3760 return parse_typeprop(EXPR_SIZEOF, precedence);
3763 static expression_t *parse_alignof(unsigned precedence)
3766 return parse_typeprop(EXPR_SIZEOF, precedence);
3769 static expression_t *parse_select_expression(unsigned precedence,
3770 expression_t *compound)
3773 assert(token.type == '.' || token.type == T_MINUSGREATER);
3775 bool is_pointer = (token.type == T_MINUSGREATER);
3778 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3779 select->select.compound = compound;
3781 if(token.type != T_IDENTIFIER) {
3782 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3785 symbol_t *symbol = token.v.symbol;
3786 select->select.symbol = symbol;
3789 type_t *const orig_type = compound->base.datatype;
3790 type_t *const type = skip_typeref(orig_type);
3792 type_t *type_left = type;
3794 if (!is_type_pointer(type)) {
3795 if (is_type_valid(type)) {
3796 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3798 return create_invalid_expression();
3800 type_left = type->pointer.points_to;
3802 type_left = skip_typeref(type_left);
3804 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3805 type_left->kind != TYPE_COMPOUND_UNION) {
3806 if (is_type_valid(type_left)) {
3807 errorf(HERE, "request for member '%Y' in something not a struct or "
3808 "union, but '%T'", symbol, type_left);
3810 return create_invalid_expression();
3813 declaration_t *const declaration = type_left->compound.declaration;
3815 if(!declaration->init.is_defined) {
3816 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3818 return create_invalid_expression();
3821 declaration_t *iter = declaration->scope.declarations;
3822 for( ; iter != NULL; iter = iter->next) {
3823 if(iter->symbol == symbol) {
3828 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3829 return create_invalid_expression();
3832 /* we always do the auto-type conversions; the & and sizeof parser contains
3833 * code to revert this! */
3834 type_t *expression_type = automatic_type_conversion(iter->type);
3836 select->select.compound_entry = iter;
3837 select->base.datatype = expression_type;
3839 if(expression_type->kind == TYPE_BITFIELD) {
3840 expression_t *extract
3841 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3842 extract->unary.value = select;
3843 extract->base.datatype = expression_type->bitfield.base;
3852 * Parse a call expression, ie. expression '( ... )'.
3854 * @param expression the function address
3856 static expression_t *parse_call_expression(unsigned precedence,
3857 expression_t *expression)
3860 expression_t *result = allocate_expression_zero(EXPR_CALL);
3862 call_expression_t *call = &result->call;
3863 call->function = expression;
3865 type_t *const orig_type = expression->base.datatype;
3866 type_t *const type = skip_typeref(orig_type);
3868 function_type_t *function_type = NULL;
3869 if (is_type_pointer(type)) {
3870 type_t *const to_type = skip_typeref(type->pointer.points_to);
3872 if (is_type_function(to_type)) {
3873 function_type = &to_type->function;
3874 call->expression.datatype = function_type->return_type;
3878 if (function_type == NULL && is_type_valid(type)) {
3879 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3882 /* parse arguments */
3885 if(token.type != ')') {
3886 call_argument_t *last_argument = NULL;
3889 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3891 argument->expression = parse_assignment_expression();
3892 if(last_argument == NULL) {
3893 call->arguments = argument;
3895 last_argument->next = argument;
3897 last_argument = argument;
3899 if(token.type != ',')
3906 if(function_type != NULL) {
3907 function_parameter_t *parameter = function_type->parameters;
3908 call_argument_t *argument = call->arguments;
3909 for( ; parameter != NULL && argument != NULL;
3910 parameter = parameter->next, argument = argument->next) {
3911 type_t *expected_type = parameter->type;
3912 /* TODO report scope in error messages */
3913 expression_t *const arg_expr = argument->expression;
3914 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
3915 if (res_type == NULL) {
3916 /* TODO improve error message */
3917 errorf(arg_expr->base.source_position,
3918 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
3919 arg_expr, arg_expr->base.datatype, expected_type);
3921 argument->expression = create_implicit_cast(argument->expression, expected_type);
3924 /* too few parameters */
3925 if(parameter != NULL) {
3926 errorf(HERE, "too few arguments to function '%E'", expression);
3927 } else if(argument != NULL) {
3928 /* too many parameters */
3929 if(!function_type->variadic
3930 && !function_type->unspecified_parameters) {
3931 errorf(HERE, "too many arguments to function '%E'", expression);
3933 /* do default promotion */
3934 for( ; argument != NULL; argument = argument->next) {
3935 type_t *type = argument->expression->base.datatype;
3937 type = skip_typeref(type);
3938 if(is_type_integer(type)) {
3939 type = promote_integer(type);
3940 } else if(type == type_float) {
3944 argument->expression
3945 = create_implicit_cast(argument->expression, type);
3948 check_format(&result->call);
3951 check_format(&result->call);
3958 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
3960 static bool same_compound_type(const type_t *type1, const type_t *type2)
3963 is_type_compound(type1) &&
3964 type1->kind == type2->kind &&
3965 type1->compound.declaration == type2->compound.declaration;
3969 * Parse a conditional expression, ie. 'expression ? ... : ...'.
3971 * @param expression the conditional expression
3973 static expression_t *parse_conditional_expression(unsigned precedence,
3974 expression_t *expression)
3978 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
3980 conditional_expression_t *conditional = &result->conditional;
3981 conditional->condition = expression;
3984 type_t *const condition_type_orig = expression->base.datatype;
3985 type_t *const condition_type = skip_typeref(condition_type_orig);
3986 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
3987 type_error("expected a scalar type in conditional condition",
3988 expression->base.source_position, condition_type_orig);
3991 expression_t *true_expression = parse_expression();
3993 expression_t *false_expression = parse_sub_expression(precedence);
3995 conditional->true_expression = true_expression;
3996 conditional->false_expression = false_expression;
3998 type_t *const orig_true_type = true_expression->base.datatype;
3999 type_t *const orig_false_type = false_expression->base.datatype;
4000 type_t *const true_type = skip_typeref(orig_true_type);
4001 type_t *const false_type = skip_typeref(orig_false_type);
4004 type_t *result_type;
4005 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4006 result_type = semantic_arithmetic(true_type, false_type);
4008 true_expression = create_implicit_cast(true_expression, result_type);
4009 false_expression = create_implicit_cast(false_expression, result_type);
4011 conditional->true_expression = true_expression;
4012 conditional->false_expression = false_expression;
4013 conditional->expression.datatype = result_type;
4014 } else if (same_compound_type(true_type, false_type) || (
4015 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4016 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4018 /* just take 1 of the 2 types */
4019 result_type = true_type;
4020 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4021 && pointers_compatible(true_type, false_type)) {
4023 result_type = true_type;
4026 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4027 type_error_incompatible("while parsing conditional",
4028 expression->base.source_position, true_type,
4031 result_type = type_error_type;
4034 conditional->expression.datatype = result_type;
4039 * Parse an extension expression.
4041 static expression_t *parse_extension(unsigned precedence)
4043 eat(T___extension__);
4045 /* TODO enable extensions */
4046 expression_t *expression = parse_sub_expression(precedence);
4047 /* TODO disable extensions */
4051 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4053 eat(T___builtin_classify_type);
4055 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4056 result->base.datatype = type_int;
4059 expression_t *expression = parse_sub_expression(precedence);
4061 result->classify_type.type_expression = expression;
4066 static void semantic_incdec(unary_expression_t *expression)
4068 type_t *const orig_type = expression->value->base.datatype;
4069 type_t *const type = skip_typeref(orig_type);
4070 /* TODO !is_type_real && !is_type_pointer */
4071 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4072 if (is_type_valid(type)) {
4073 /* TODO: improve error message */
4074 errorf(HERE, "operation needs an arithmetic or pointer type");
4079 expression->expression.datatype = orig_type;
4082 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4084 type_t *const orig_type = expression->value->base.datatype;
4085 type_t *const type = skip_typeref(orig_type);
4086 if(!is_type_arithmetic(type)) {
4087 if (is_type_valid(type)) {
4088 /* TODO: improve error message */
4089 errorf(HERE, "operation needs an arithmetic type");
4094 expression->expression.datatype = orig_type;
4097 static void semantic_unexpr_scalar(unary_expression_t *expression)
4099 type_t *const orig_type = expression->value->base.datatype;
4100 type_t *const type = skip_typeref(orig_type);
4101 if (!is_type_scalar(type)) {
4102 if (is_type_valid(type)) {
4103 errorf(HERE, "operand of ! must be of scalar type");
4108 expression->expression.datatype = orig_type;
4111 static void semantic_unexpr_integer(unary_expression_t *expression)
4113 type_t *const orig_type = expression->value->base.datatype;
4114 type_t *const type = skip_typeref(orig_type);
4115 if (!is_type_integer(type)) {
4116 if (is_type_valid(type)) {
4117 errorf(HERE, "operand of ~ must be of integer type");
4122 expression->expression.datatype = orig_type;
4125 static void semantic_dereference(unary_expression_t *expression)
4127 type_t *const orig_type = expression->value->base.datatype;
4128 type_t *const type = skip_typeref(orig_type);
4129 if(!is_type_pointer(type)) {
4130 if (is_type_valid(type)) {
4131 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4136 type_t *result_type = type->pointer.points_to;
4137 result_type = automatic_type_conversion(result_type);
4138 expression->expression.datatype = result_type;
4142 * Check the semantic of the address taken expression.
4144 static void semantic_take_addr(unary_expression_t *expression)
4146 expression_t *value = expression->value;
4147 value->base.datatype = revert_automatic_type_conversion(value);
4149 type_t *orig_type = value->base.datatype;
4150 if(!is_type_valid(orig_type))
4153 if(value->kind == EXPR_REFERENCE) {
4154 declaration_t *const declaration = value->reference.declaration;
4155 if(declaration != NULL) {
4156 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4157 errorf(expression->expression.source_position,
4158 "address of register variable '%Y' requested",
4159 declaration->symbol);
4161 declaration->address_taken = 1;
4165 expression->expression.datatype = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4168 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4169 static expression_t *parse_##unexpression_type(unsigned precedence) \
4173 expression_t *unary_expression \
4174 = allocate_expression_zero(unexpression_type); \
4175 unary_expression->base.source_position = HERE; \
4176 unary_expression->unary.value = parse_sub_expression(precedence); \
4178 sfunc(&unary_expression->unary); \
4180 return unary_expression; \
4183 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4184 semantic_unexpr_arithmetic)
4185 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4186 semantic_unexpr_arithmetic)
4187 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4188 semantic_unexpr_scalar)
4189 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4190 semantic_dereference)
4191 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4193 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4194 semantic_unexpr_integer)
4195 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4197 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4200 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4202 static expression_t *parse_##unexpression_type(unsigned precedence, \
4203 expression_t *left) \
4205 (void) precedence; \
4208 expression_t *unary_expression \
4209 = allocate_expression_zero(unexpression_type); \
4210 unary_expression->unary.value = left; \
4212 sfunc(&unary_expression->unary); \
4214 return unary_expression; \
4217 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4218 EXPR_UNARY_POSTFIX_INCREMENT,
4220 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4221 EXPR_UNARY_POSTFIX_DECREMENT,
4224 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4226 /* TODO: handle complex + imaginary types */
4228 /* § 6.3.1.8 Usual arithmetic conversions */
4229 if(type_left == type_long_double || type_right == type_long_double) {
4230 return type_long_double;
4231 } else if(type_left == type_double || type_right == type_double) {
4233 } else if(type_left == type_float || type_right == type_float) {
4237 type_right = promote_integer(type_right);
4238 type_left = promote_integer(type_left);
4240 if(type_left == type_right)
4243 bool signed_left = is_type_signed(type_left);
4244 bool signed_right = is_type_signed(type_right);
4245 int rank_left = get_rank(type_left);
4246 int rank_right = get_rank(type_right);
4247 if(rank_left < rank_right) {
4248 if(signed_left == signed_right || !signed_right) {
4254 if(signed_left == signed_right || !signed_left) {
4263 * Check the semantic restrictions for a binary expression.
4265 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4267 expression_t *const left = expression->left;
4268 expression_t *const right = expression->right;
4269 type_t *const orig_type_left = left->base.datatype;
4270 type_t *const orig_type_right = right->base.datatype;
4271 type_t *const type_left = skip_typeref(orig_type_left);
4272 type_t *const type_right = skip_typeref(orig_type_right);
4274 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4275 /* TODO: improve error message */
4276 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4277 errorf(HERE, "operation needs arithmetic types");
4282 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4283 expression->left = create_implicit_cast(left, arithmetic_type);
4284 expression->right = create_implicit_cast(right, arithmetic_type);
4285 expression->expression.datatype = arithmetic_type;
4288 static void semantic_shift_op(binary_expression_t *expression)
4290 expression_t *const left = expression->left;
4291 expression_t *const right = expression->right;
4292 type_t *const orig_type_left = left->base.datatype;
4293 type_t *const orig_type_right = right->base.datatype;
4294 type_t * type_left = skip_typeref(orig_type_left);
4295 type_t * type_right = skip_typeref(orig_type_right);
4297 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4298 /* TODO: improve error message */
4299 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4300 errorf(HERE, "operation needs integer types");
4305 type_left = promote_integer(type_left);
4306 type_right = promote_integer(type_right);
4308 expression->left = create_implicit_cast(left, type_left);
4309 expression->right = create_implicit_cast(right, type_right);
4310 expression->expression.datatype = type_left;
4313 static void semantic_add(binary_expression_t *expression)
4315 expression_t *const left = expression->left;
4316 expression_t *const right = expression->right;
4317 type_t *const orig_type_left = left->base.datatype;
4318 type_t *const orig_type_right = right->base.datatype;
4319 type_t *const type_left = skip_typeref(orig_type_left);
4320 type_t *const type_right = skip_typeref(orig_type_right);
4323 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4324 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4325 expression->left = create_implicit_cast(left, arithmetic_type);
4326 expression->right = create_implicit_cast(right, arithmetic_type);
4327 expression->expression.datatype = arithmetic_type;
4329 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4330 expression->expression.datatype = type_left;
4331 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4332 expression->expression.datatype = type_right;
4333 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4334 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4338 static void semantic_sub(binary_expression_t *expression)
4340 expression_t *const left = expression->left;
4341 expression_t *const right = expression->right;
4342 type_t *const orig_type_left = left->base.datatype;
4343 type_t *const orig_type_right = right->base.datatype;
4344 type_t *const type_left = skip_typeref(orig_type_left);
4345 type_t *const type_right = skip_typeref(orig_type_right);
4348 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4349 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4350 expression->left = create_implicit_cast(left, arithmetic_type);
4351 expression->right = create_implicit_cast(right, arithmetic_type);
4352 expression->expression.datatype = arithmetic_type;
4354 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4355 expression->expression.datatype = type_left;
4356 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4357 if(!pointers_compatible(type_left, type_right)) {
4358 errorf(HERE, "pointers to incompatible objects to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4360 expression->expression.datatype = type_ptrdiff_t;
4362 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4363 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')", orig_type_left, orig_type_right);
4368 * Check the semantics of comparison expressions.
4370 * @param expression The expression to check.
4372 static void semantic_comparison(binary_expression_t *expression)
4374 expression_t *left = expression->left;
4375 expression_t *right = expression->right;
4376 type_t *orig_type_left = left->base.datatype;
4377 type_t *orig_type_right = right->base.datatype;
4379 type_t *type_left = skip_typeref(orig_type_left);
4380 type_t *type_right = skip_typeref(orig_type_right);
4382 /* TODO non-arithmetic types */
4383 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4384 if (warning.sign_compare &&
4385 (expression->expression.kind != EXPR_BINARY_EQUAL &&
4386 expression->expression.kind != EXPR_BINARY_NOTEQUAL) &&
4387 (is_type_signed(type_left) != is_type_signed(type_right))) {
4388 warningf(expression->expression.source_position,
4389 "comparison between signed and unsigned");
4391 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4392 expression->left = create_implicit_cast(left, arithmetic_type);
4393 expression->right = create_implicit_cast(right, arithmetic_type);
4394 expression->expression.datatype = arithmetic_type;
4395 if (warning.float_equal &&
4396 (expression->expression.kind == EXPR_BINARY_EQUAL ||
4397 expression->expression.kind == EXPR_BINARY_NOTEQUAL) &&
4398 is_type_float(arithmetic_type)) {
4399 warningf(expression->expression.source_position,
4400 "comparing floating point with == or != is unsafe");
4402 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4403 /* TODO check compatibility */
4404 } else if (is_type_pointer(type_left)) {
4405 expression->right = create_implicit_cast(right, type_left);
4406 } else if (is_type_pointer(type_right)) {
4407 expression->left = create_implicit_cast(left, type_right);
4408 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4409 type_error_incompatible("invalid operands in comparison",
4410 expression->expression.source_position,
4411 type_left, type_right);
4413 expression->expression.datatype = type_int;
4416 static void semantic_arithmetic_assign(binary_expression_t *expression)
4418 expression_t *left = expression->left;
4419 expression_t *right = expression->right;
4420 type_t *orig_type_left = left->base.datatype;
4421 type_t *orig_type_right = right->base.datatype;
4423 type_t *type_left = skip_typeref(orig_type_left);
4424 type_t *type_right = skip_typeref(orig_type_right);
4426 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4427 /* TODO: improve error message */
4428 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4429 errorf(HERE, "operation needs arithmetic types");
4434 /* combined instructions are tricky. We can't create an implicit cast on
4435 * the left side, because we need the uncasted form for the store.
4436 * The ast2firm pass has to know that left_type must be right_type
4437 * for the arithmetic operation and create a cast by itself */
4438 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4439 expression->right = create_implicit_cast(right, arithmetic_type);
4440 expression->expression.datatype = type_left;
4443 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4445 expression_t *const left = expression->left;
4446 expression_t *const right = expression->right;
4447 type_t *const orig_type_left = left->base.datatype;
4448 type_t *const orig_type_right = right->base.datatype;
4449 type_t *const type_left = skip_typeref(orig_type_left);
4450 type_t *const type_right = skip_typeref(orig_type_right);
4452 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4453 /* combined instructions are tricky. We can't create an implicit cast on
4454 * the left side, because we need the uncasted form for the store.
4455 * The ast2firm pass has to know that left_type must be right_type
4456 * for the arithmetic operation and create a cast by itself */
4457 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4458 expression->right = create_implicit_cast(right, arithmetic_type);
4459 expression->expression.datatype = type_left;
4460 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4461 expression->expression.datatype = type_left;
4462 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4463 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4468 * Check the semantic restrictions of a logical expression.
4470 static void semantic_logical_op(binary_expression_t *expression)
4472 expression_t *const left = expression->left;
4473 expression_t *const right = expression->right;
4474 type_t *const orig_type_left = left->base.datatype;
4475 type_t *const orig_type_right = right->base.datatype;
4476 type_t *const type_left = skip_typeref(orig_type_left);
4477 type_t *const type_right = skip_typeref(orig_type_right);
4479 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4480 /* TODO: improve error message */
4481 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4482 errorf(HERE, "operation needs scalar types");
4487 expression->expression.datatype = type_int;
4491 * Checks if a compound type has constant fields.
4493 static bool has_const_fields(const compound_type_t *type)
4495 const scope_t *scope = &type->declaration->scope;
4496 const declaration_t *declaration = scope->declarations;
4498 for (; declaration != NULL; declaration = declaration->next) {
4499 if (declaration->namespc != NAMESPACE_NORMAL)
4502 const type_t *decl_type = skip_typeref(declaration->type);
4503 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4511 * Check the semantic restrictions of a binary assign expression.
4513 static void semantic_binexpr_assign(binary_expression_t *expression)
4515 expression_t *left = expression->left;
4516 type_t *orig_type_left = left->base.datatype;
4518 type_t *type_left = revert_automatic_type_conversion(left);
4519 type_left = skip_typeref(orig_type_left);
4521 /* must be a modifiable lvalue */
4522 if (is_type_array(type_left)) {
4523 errorf(HERE, "cannot assign to arrays ('%E')", left);
4526 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4527 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4531 if(is_type_incomplete(type_left)) {
4533 "left-hand side of assignment '%E' has incomplete type '%T'",
4534 left, orig_type_left);
4537 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4538 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4539 left, orig_type_left);
4543 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4545 if (res_type == NULL) {
4546 errorf(expression->expression.source_position,
4547 "cannot assign to '%T' from '%T'",
4548 orig_type_left, expression->right->base.datatype);
4550 expression->right = create_implicit_cast(expression->right, res_type);
4553 expression->expression.datatype = orig_type_left;
4556 static bool expression_has_effect(const expression_t *const expr)
4558 switch (expr->kind) {
4559 case EXPR_UNKNOWN: break;
4560 case EXPR_INVALID: break;
4561 case EXPR_REFERENCE: return false;
4562 case EXPR_CONST: return false;
4563 case EXPR_STRING_LITERAL: return false;
4564 case EXPR_WIDE_STRING_LITERAL: return false;
4566 const call_expression_t *const call = &expr->call;
4567 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4570 switch (call->function->builtin_symbol.symbol->ID) {
4571 case T___builtin_va_end: return true;
4572 default: return false;
4575 case EXPR_CONDITIONAL: {
4576 const conditional_expression_t *const cond = &expr->conditional;
4578 expression_has_effect(cond->true_expression) &&
4579 expression_has_effect(cond->false_expression);
4581 case EXPR_SELECT: return false;
4582 case EXPR_ARRAY_ACCESS: return false;
4583 case EXPR_SIZEOF: return false;
4584 case EXPR_CLASSIFY_TYPE: return false;
4585 case EXPR_ALIGNOF: return false;
4587 case EXPR_FUNCTION: return false;
4588 case EXPR_PRETTY_FUNCTION: return false;
4589 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4590 case EXPR_BUILTIN_CONSTANT_P: return false;
4591 case EXPR_BUILTIN_PREFETCH: return true;
4592 case EXPR_OFFSETOF: return false;
4593 case EXPR_VA_START: return true;
4594 case EXPR_VA_ARG: return true;
4595 case EXPR_STATEMENT: return true; // TODO
4597 case EXPR_UNARY_NEGATE: return false;
4598 case EXPR_UNARY_PLUS: return false;
4599 case EXPR_UNARY_BITWISE_NEGATE: return false;
4600 case EXPR_UNARY_NOT: return false;
4601 case EXPR_UNARY_DEREFERENCE: return false;
4602 case EXPR_UNARY_TAKE_ADDRESS: return false;
4603 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4604 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4605 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4606 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4607 case EXPR_UNARY_CAST:
4608 return is_type_atomic(expr->base.datatype, ATOMIC_TYPE_VOID);
4609 case EXPR_UNARY_CAST_IMPLICIT: return true;
4610 case EXPR_UNARY_ASSUME: return true;
4611 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4613 case EXPR_BINARY_ADD: return false;
4614 case EXPR_BINARY_SUB: return false;
4615 case EXPR_BINARY_MUL: return false;
4616 case EXPR_BINARY_DIV: return false;
4617 case EXPR_BINARY_MOD: return false;
4618 case EXPR_BINARY_EQUAL: return false;
4619 case EXPR_BINARY_NOTEQUAL: return false;
4620 case EXPR_BINARY_LESS: return false;
4621 case EXPR_BINARY_LESSEQUAL: return false;
4622 case EXPR_BINARY_GREATER: return false;
4623 case EXPR_BINARY_GREATEREQUAL: return false;
4624 case EXPR_BINARY_BITWISE_AND: return false;
4625 case EXPR_BINARY_BITWISE_OR: return false;
4626 case EXPR_BINARY_BITWISE_XOR: return false;
4627 case EXPR_BINARY_SHIFTLEFT: return false;
4628 case EXPR_BINARY_SHIFTRIGHT: return false;
4629 case EXPR_BINARY_ASSIGN: return true;
4630 case EXPR_BINARY_MUL_ASSIGN: return true;
4631 case EXPR_BINARY_DIV_ASSIGN: return true;
4632 case EXPR_BINARY_MOD_ASSIGN: return true;
4633 case EXPR_BINARY_ADD_ASSIGN: return true;
4634 case EXPR_BINARY_SUB_ASSIGN: return true;
4635 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4636 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4637 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4638 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4639 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4640 case EXPR_BINARY_LOGICAL_AND:
4641 case EXPR_BINARY_LOGICAL_OR:
4642 case EXPR_BINARY_COMMA:
4643 return expression_has_effect(expr->binary.right);
4645 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4646 case EXPR_BINARY_ISGREATER: return false;
4647 case EXPR_BINARY_ISGREATEREQUAL: return false;
4648 case EXPR_BINARY_ISLESS: return false;
4649 case EXPR_BINARY_ISLESSEQUAL: return false;
4650 case EXPR_BINARY_ISLESSGREATER: return false;
4651 case EXPR_BINARY_ISUNORDERED: return false;
4654 panic("unexpected statement");
4657 static void semantic_comma(binary_expression_t *expression)
4659 if (warning.unused_value) {
4660 const expression_t *const left = expression->left;
4661 if (!expression_has_effect(left)) {
4662 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4665 expression->expression.datatype = expression->right->base.datatype;
4668 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4669 static expression_t *parse_##binexpression_type(unsigned precedence, \
4670 expression_t *left) \
4673 source_position_t pos = HERE; \
4675 expression_t *right = parse_sub_expression(precedence + lr); \
4677 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4678 binexpr->base.source_position = pos; \
4679 binexpr->binary.left = left; \
4680 binexpr->binary.right = right; \
4681 sfunc(&binexpr->binary); \
4686 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4687 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4688 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4689 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4690 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4691 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4692 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4693 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4694 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4696 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4697 semantic_comparison, 1)
4698 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4699 semantic_comparison, 1)
4700 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4701 semantic_comparison, 1)
4702 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4703 semantic_comparison, 1)
4705 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4706 semantic_binexpr_arithmetic, 1)
4707 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4708 semantic_binexpr_arithmetic, 1)
4709 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4710 semantic_binexpr_arithmetic, 1)
4711 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4712 semantic_logical_op, 1)
4713 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4714 semantic_logical_op, 1)
4715 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4716 semantic_shift_op, 1)
4717 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4718 semantic_shift_op, 1)
4719 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4720 semantic_arithmetic_addsubb_assign, 0)
4721 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4722 semantic_arithmetic_addsubb_assign, 0)
4723 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4724 semantic_arithmetic_assign, 0)
4725 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4726 semantic_arithmetic_assign, 0)
4727 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4728 semantic_arithmetic_assign, 0)
4729 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4730 semantic_arithmetic_assign, 0)
4731 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4732 semantic_arithmetic_assign, 0)
4733 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4734 semantic_arithmetic_assign, 0)
4735 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4736 semantic_arithmetic_assign, 0)
4737 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4738 semantic_arithmetic_assign, 0)
4740 static expression_t *parse_sub_expression(unsigned precedence)
4742 if(token.type < 0) {
4743 return expected_expression_error();
4746 expression_parser_function_t *parser
4747 = &expression_parsers[token.type];
4748 source_position_t source_position = token.source_position;
4751 if(parser->parser != NULL) {
4752 left = parser->parser(parser->precedence);
4754 left = parse_primary_expression();
4756 assert(left != NULL);
4757 left->base.source_position = source_position;
4760 if(token.type < 0) {
4761 return expected_expression_error();
4764 parser = &expression_parsers[token.type];
4765 if(parser->infix_parser == NULL)
4767 if(parser->infix_precedence < precedence)
4770 left = parser->infix_parser(parser->infix_precedence, left);
4772 assert(left != NULL);
4773 assert(left->kind != EXPR_UNKNOWN);
4774 left->base.source_position = source_position;
4781 * Parse an expression.
4783 static expression_t *parse_expression(void)
4785 return parse_sub_expression(1);
4789 * Register a parser for a prefix-like operator with given precedence.
4791 * @param parser the parser function
4792 * @param token_type the token type of the prefix token
4793 * @param precedence the precedence of the operator
4795 static void register_expression_parser(parse_expression_function parser,
4796 int token_type, unsigned precedence)
4798 expression_parser_function_t *entry = &expression_parsers[token_type];
4800 if(entry->parser != NULL) {
4801 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4802 panic("trying to register multiple expression parsers for a token");
4804 entry->parser = parser;
4805 entry->precedence = precedence;
4809 * Register a parser for an infix operator with given precedence.
4811 * @param parser the parser function
4812 * @param token_type the token type of the infix operator
4813 * @param precedence the precedence of the operator
4815 static void register_infix_parser(parse_expression_infix_function parser,
4816 int token_type, unsigned precedence)
4818 expression_parser_function_t *entry = &expression_parsers[token_type];
4820 if(entry->infix_parser != NULL) {
4821 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4822 panic("trying to register multiple infix expression parsers for a "
4825 entry->infix_parser = parser;
4826 entry->infix_precedence = precedence;
4830 * Initialize the expression parsers.
4832 static void init_expression_parsers(void)
4834 memset(&expression_parsers, 0, sizeof(expression_parsers));
4836 register_infix_parser(parse_array_expression, '[', 30);
4837 register_infix_parser(parse_call_expression, '(', 30);
4838 register_infix_parser(parse_select_expression, '.', 30);
4839 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4840 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4842 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4845 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4846 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4847 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4848 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4849 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4850 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4851 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4852 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4853 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4854 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4855 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4856 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4857 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4858 T_EXCLAMATIONMARKEQUAL, 13);
4859 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4860 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4861 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4862 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4863 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4864 register_infix_parser(parse_conditional_expression, '?', 7);
4865 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4866 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4867 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4868 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4869 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4870 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4871 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4872 T_LESSLESSEQUAL, 2);
4873 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4874 T_GREATERGREATEREQUAL, 2);
4875 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4877 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4879 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4882 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4884 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4885 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4886 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4887 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4888 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4889 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4890 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4892 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4894 register_expression_parser(parse_sizeof, T_sizeof, 25);
4895 register_expression_parser(parse_alignof, T___alignof__, 25);
4896 register_expression_parser(parse_extension, T___extension__, 25);
4897 register_expression_parser(parse_builtin_classify_type,
4898 T___builtin_classify_type, 25);
4902 * Parse a asm statement constraints specification.
4904 static asm_constraint_t *parse_asm_constraints(void)
4906 asm_constraint_t *result = NULL;
4907 asm_constraint_t *last = NULL;
4909 while(token.type == T_STRING_LITERAL || token.type == '[') {
4910 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
4911 memset(constraint, 0, sizeof(constraint[0]));
4913 if(token.type == '[') {
4915 if(token.type != T_IDENTIFIER) {
4916 parse_error_expected("while parsing asm constraint",
4920 constraint->symbol = token.v.symbol;
4925 constraint->constraints = parse_string_literals();
4927 constraint->expression = parse_expression();
4931 last->next = constraint;
4933 result = constraint;
4937 if(token.type != ',')
4946 * Parse a asm statement clobber specification.
4948 static asm_clobber_t *parse_asm_clobbers(void)
4950 asm_clobber_t *result = NULL;
4951 asm_clobber_t *last = NULL;
4953 while(token.type == T_STRING_LITERAL) {
4954 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
4955 clobber->clobber = parse_string_literals();
4958 last->next = clobber;
4964 if(token.type != ',')
4973 * Parse an asm statement.
4975 static statement_t *parse_asm_statement(void)
4979 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
4980 statement->base.source_position = token.source_position;
4982 asm_statement_t *asm_statement = &statement->asms;
4984 if(token.type == T_volatile) {
4986 asm_statement->is_volatile = true;
4990 asm_statement->asm_text = parse_string_literals();
4992 if(token.type != ':')
4996 asm_statement->inputs = parse_asm_constraints();
4997 if(token.type != ':')
5001 asm_statement->outputs = parse_asm_constraints();
5002 if(token.type != ':')
5006 asm_statement->clobbers = parse_asm_clobbers();
5015 * Parse a case statement.
5017 static statement_t *parse_case_statement(void)
5021 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5023 statement->base.source_position = token.source_position;
5024 statement->case_label.expression = parse_expression();
5028 if (! is_constant_expression(statement->case_label.expression)) {
5029 errorf(statement->base.source_position,
5030 "case label does not reduce to an integer constant");
5032 /* TODO: check if the case label is already known */
5033 if (current_switch != NULL) {
5034 /* link all cases into the switch statement */
5035 if (current_switch->last_case == NULL) {
5036 current_switch->first_case =
5037 current_switch->last_case = &statement->case_label;
5039 current_switch->last_case->next = &statement->case_label;
5042 errorf(statement->base.source_position,
5043 "case label not within a switch statement");
5046 statement->case_label.label_statement = parse_statement();
5052 * Finds an existing default label of a switch statement.
5054 static case_label_statement_t *
5055 find_default_label(const switch_statement_t *statement)
5057 for (case_label_statement_t *label = statement->first_case;
5059 label = label->next) {
5060 if (label->expression == NULL)
5067 * Parse a default statement.
5069 static statement_t *parse_default_statement(void)
5073 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5075 statement->base.source_position = token.source_position;
5078 if (current_switch != NULL) {
5079 const case_label_statement_t *def_label = find_default_label(current_switch);
5080 if (def_label != NULL) {
5081 errorf(HERE, "multiple default labels in one switch");
5082 errorf(def_label->statement.source_position,
5083 "this is the first default label");
5085 /* link all cases into the switch statement */
5086 if (current_switch->last_case == NULL) {
5087 current_switch->first_case =
5088 current_switch->last_case = &statement->case_label;
5090 current_switch->last_case->next = &statement->case_label;
5094 errorf(statement->base.source_position,
5095 "'default' label not within a switch statement");
5097 statement->label.label_statement = parse_statement();
5103 * Return the declaration for a given label symbol or create a new one.
5105 static declaration_t *get_label(symbol_t *symbol)
5107 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5108 assert(current_function != NULL);
5109 /* if we found a label in the same function, then we already created the
5111 if(candidate != NULL
5112 && candidate->parent_scope == ¤t_function->scope) {
5116 /* otherwise we need to create a new one */
5117 declaration_t *const declaration = allocate_declaration_zero();
5118 declaration->namespc = NAMESPACE_LABEL;
5119 declaration->symbol = symbol;
5121 label_push(declaration);
5127 * Parse a label statement.
5129 static statement_t *parse_label_statement(void)
5131 assert(token.type == T_IDENTIFIER);
5132 symbol_t *symbol = token.v.symbol;
5135 declaration_t *label = get_label(symbol);
5137 /* if source position is already set then the label is defined twice,
5138 * otherwise it was just mentioned in a goto so far */
5139 if(label->source_position.input_name != NULL) {
5140 errorf(HERE, "duplicate label '%Y'", symbol);
5141 errorf(label->source_position, "previous definition of '%Y' was here",
5144 label->source_position = token.source_position;
5147 label_statement_t *label_statement = allocate_ast_zero(sizeof(label[0]));
5149 label_statement->statement.kind = STATEMENT_LABEL;
5150 label_statement->statement.source_position = token.source_position;
5151 label_statement->label = label;
5155 if(token.type == '}') {
5156 /* TODO only warn? */
5157 errorf(HERE, "label at end of compound statement");
5158 return (statement_t*) label_statement;
5160 if (token.type == ';') {
5161 /* eat an empty statement here, to avoid the warning about an empty
5162 * after a label. label:; is commonly used to have a label before
5166 label_statement->label_statement = parse_statement();
5170 /* remember the labels's in a list for later checking */
5171 if (label_last == NULL) {
5172 label_first = label_statement;
5174 label_last->next = label_statement;
5176 label_last = label_statement;
5178 return (statement_t*) label_statement;
5182 * Parse an if statement.
5184 static statement_t *parse_if(void)
5188 if_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5189 statement->statement.kind = STATEMENT_IF;
5190 statement->statement.source_position = token.source_position;
5193 statement->condition = parse_expression();
5196 statement->true_statement = parse_statement();
5197 if(token.type == T_else) {
5199 statement->false_statement = parse_statement();
5202 return (statement_t*) statement;
5206 * Parse a switch statement.
5208 static statement_t *parse_switch(void)
5212 switch_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5213 statement->statement.kind = STATEMENT_SWITCH;
5214 statement->statement.source_position = token.source_position;
5217 expression_t *const expr = parse_expression();
5218 type_t * type = skip_typeref(expr->base.datatype);
5219 if (is_type_integer(type)) {
5220 type = promote_integer(type);
5221 } else if (is_type_valid(type)) {
5222 errorf(expr->base.source_position, "switch quantity is not an integer, but '%T'", type);
5223 type = type_error_type;
5225 statement->expression = create_implicit_cast(expr, type);
5228 switch_statement_t *rem = current_switch;
5229 current_switch = statement;
5230 statement->body = parse_statement();
5231 current_switch = rem;
5233 if (warning.switch_default && find_default_label(statement) == NULL) {
5234 warningf(statement->statement.source_position, "switch has no default case");
5237 return (statement_t*) statement;
5240 static statement_t *parse_loop_body(statement_t *const loop)
5242 statement_t *const rem = current_loop;
5243 current_loop = loop;
5244 statement_t *const body = parse_statement();
5250 * Parse a while statement.
5252 static statement_t *parse_while(void)
5256 while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5257 statement->statement.kind = STATEMENT_WHILE;
5258 statement->statement.source_position = token.source_position;
5261 statement->condition = parse_expression();
5264 statement->body = parse_loop_body((statement_t*)statement);
5266 return (statement_t*) statement;
5270 * Parse a do statement.
5272 static statement_t *parse_do(void)
5276 do_while_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5277 statement->statement.kind = STATEMENT_DO_WHILE;
5278 statement->statement.source_position = token.source_position;
5280 statement->body = parse_loop_body((statement_t*)statement);
5283 statement->condition = parse_expression();
5287 return (statement_t*) statement;
5291 * Parse a for statement.
5293 static statement_t *parse_for(void)
5297 for_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5298 statement->statement.kind = STATEMENT_FOR;
5299 statement->statement.source_position = token.source_position;
5303 int top = environment_top();
5304 scope_t *last_scope = scope;
5305 set_scope(&statement->scope);
5307 if(token.type != ';') {
5308 if(is_declaration_specifier(&token, false)) {
5309 parse_declaration(record_declaration);
5311 statement->initialisation = parse_expression();
5318 if(token.type != ';') {
5319 statement->condition = parse_expression();
5322 if(token.type != ')') {
5323 statement->step = parse_expression();
5326 statement->body = parse_loop_body((statement_t*)statement);
5328 assert(scope == &statement->scope);
5329 set_scope(last_scope);
5330 environment_pop_to(top);
5332 return (statement_t*) statement;
5336 * Parse a goto statement.
5338 static statement_t *parse_goto(void)
5342 if(token.type != T_IDENTIFIER) {
5343 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5347 symbol_t *symbol = token.v.symbol;
5350 declaration_t *label = get_label(symbol);
5352 goto_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5354 statement->statement.kind = STATEMENT_GOTO;
5355 statement->statement.source_position = token.source_position;
5357 statement->label = label;
5359 /* remember the goto's in a list for later checking */
5360 if (goto_last == NULL) {
5361 goto_first = statement;
5363 goto_last->next = statement;
5365 goto_last = statement;
5369 return (statement_t*) statement;
5373 * Parse a continue statement.
5375 static statement_t *parse_continue(void)
5377 statement_t *statement;
5378 if (current_loop == NULL) {
5379 errorf(HERE, "continue statement not within loop");
5382 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5384 statement->base.source_position = token.source_position;
5394 * Parse a break statement.
5396 static statement_t *parse_break(void)
5398 statement_t *statement;
5399 if (current_switch == NULL && current_loop == NULL) {
5400 errorf(HERE, "break statement not within loop or switch");
5403 statement = allocate_statement_zero(STATEMENT_BREAK);
5405 statement->base.source_position = token.source_position;
5415 * Check if a given declaration represents a local variable.
5417 static bool is_local_var_declaration(const declaration_t *declaration) {
5418 switch ((storage_class_tag_t) declaration->storage_class) {
5419 case STORAGE_CLASS_NONE:
5420 case STORAGE_CLASS_AUTO:
5421 case STORAGE_CLASS_REGISTER: {
5422 const type_t *type = skip_typeref(declaration->type);
5423 if(is_type_function(type)) {
5435 * Check if a given declaration represents a variable.
5437 static bool is_var_declaration(const declaration_t *declaration) {
5438 switch ((storage_class_tag_t) declaration->storage_class) {
5439 case STORAGE_CLASS_NONE:
5440 case STORAGE_CLASS_EXTERN:
5441 case STORAGE_CLASS_STATIC:
5442 case STORAGE_CLASS_AUTO:
5443 case STORAGE_CLASS_REGISTER:
5444 case STORAGE_CLASS_THREAD:
5445 case STORAGE_CLASS_THREAD_EXTERN:
5446 case STORAGE_CLASS_THREAD_STATIC: {
5447 const type_t *type = skip_typeref(declaration->type);
5448 if(is_type_function(type)) {
5460 * Check if a given expression represents a local variable.
5462 static bool is_local_variable(const expression_t *expression)
5464 if (expression->base.kind != EXPR_REFERENCE) {
5467 const declaration_t *declaration = expression->reference.declaration;
5468 return is_local_var_declaration(declaration);
5472 * Check if a given expression represents a local variable and
5473 * return its declaration then, else return NULL.
5475 declaration_t *expr_is_variable(const expression_t *expression)
5477 if (expression->base.kind != EXPR_REFERENCE) {
5480 declaration_t *declaration = expression->reference.declaration;
5481 if (is_var_declaration(declaration))
5487 * Parse a return statement.
5489 static statement_t *parse_return(void)
5493 return_statement_t *statement = allocate_ast_zero(sizeof(statement[0]));
5495 statement->statement.kind = STATEMENT_RETURN;
5496 statement->statement.source_position = token.source_position;
5498 expression_t *return_value = NULL;
5499 if(token.type != ';') {
5500 return_value = parse_expression();
5504 const type_t *const func_type = current_function->type;
5505 assert(is_type_function(func_type));
5506 type_t *const return_type = skip_typeref(func_type->function.return_type);
5508 if(return_value != NULL) {
5509 type_t *return_value_type = skip_typeref(return_value->base.datatype);
5511 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5512 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5513 warningf(statement->statement.source_position,
5514 "'return' with a value, in function returning void");
5515 return_value = NULL;
5517 type_t *const res_type = semantic_assign(return_type,
5518 return_value, "'return'");
5519 if (res_type == NULL) {
5520 errorf(statement->statement.source_position,
5521 "cannot return something of type '%T' in function returning '%T'",
5522 return_value->base.datatype, return_type);
5524 return_value = create_implicit_cast(return_value, res_type);
5527 /* check for returning address of a local var */
5528 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5529 const expression_t *expression = return_value->unary.value;
5530 if (is_local_variable(expression)) {
5531 warningf(statement->statement.source_position,
5532 "function returns address of local variable");
5536 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5537 warningf(statement->statement.source_position,
5538 "'return' without value, in function returning non-void");
5541 statement->return_value = return_value;
5543 return (statement_t*) statement;
5547 * Parse a declaration statement.
5549 static statement_t *parse_declaration_statement(void)
5551 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5553 statement->base.source_position = token.source_position;
5555 declaration_t *before = last_declaration;
5556 parse_declaration(record_declaration);
5558 if(before == NULL) {
5559 statement->declaration.declarations_begin = scope->declarations;
5561 statement->declaration.declarations_begin = before->next;
5563 statement->declaration.declarations_end = last_declaration;
5569 * Parse an expression statement, ie. expr ';'.
5571 static statement_t *parse_expression_statement(void)
5573 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5575 statement->base.source_position = token.source_position;
5576 expression_t *const expr = parse_expression();
5577 statement->expression.expression = expr;
5579 if (warning.unused_value && !expression_has_effect(expr)) {
5580 warningf(expr->base.source_position, "statement has no effect");
5589 * Parse a statement.
5591 static statement_t *parse_statement(void)
5593 statement_t *statement = NULL;
5595 /* declaration or statement */
5596 switch(token.type) {
5598 statement = parse_asm_statement();
5602 statement = parse_case_statement();
5606 statement = parse_default_statement();
5610 statement = parse_compound_statement();
5614 statement = parse_if();
5618 statement = parse_switch();
5622 statement = parse_while();
5626 statement = parse_do();
5630 statement = parse_for();
5634 statement = parse_goto();
5638 statement = parse_continue();
5642 statement = parse_break();
5646 statement = parse_return();
5650 if (warning.empty_statement) {
5651 warningf(HERE, "statement is empty");
5658 if(look_ahead(1)->type == ':') {
5659 statement = parse_label_statement();
5663 if(is_typedef_symbol(token.v.symbol)) {
5664 statement = parse_declaration_statement();
5668 statement = parse_expression_statement();
5671 case T___extension__:
5672 /* this can be a prefix to a declaration or an expression statement */
5673 /* we simply eat it now and parse the rest with tail recursion */
5676 } while(token.type == T___extension__);
5677 statement = parse_statement();
5681 statement = parse_declaration_statement();
5685 statement = parse_expression_statement();
5689 assert(statement == NULL
5690 || statement->base.source_position.input_name != NULL);
5696 * Parse a compound statement.
5698 static statement_t *parse_compound_statement(void)
5700 compound_statement_t *const compound_statement
5701 = allocate_ast_zero(sizeof(compound_statement[0]));
5702 compound_statement->statement.kind = STATEMENT_COMPOUND;
5703 compound_statement->statement.source_position = token.source_position;
5707 int top = environment_top();
5708 scope_t *last_scope = scope;
5709 set_scope(&compound_statement->scope);
5711 statement_t *last_statement = NULL;
5713 while(token.type != '}' && token.type != T_EOF) {
5714 statement_t *statement = parse_statement();
5715 if(statement == NULL)
5718 if(last_statement != NULL) {
5719 last_statement->base.next = statement;
5721 compound_statement->statements = statement;
5724 while(statement->base.next != NULL)
5725 statement = statement->base.next;
5727 last_statement = statement;
5730 if(token.type == '}') {
5733 errorf(compound_statement->statement.source_position, "end of file while looking for closing '}'");
5736 assert(scope == &compound_statement->scope);
5737 set_scope(last_scope);
5738 environment_pop_to(top);
5740 return (statement_t*) compound_statement;
5744 * Initialize builtin types.
5746 static void initialize_builtin_types(void)
5748 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5749 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5750 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5751 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5752 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5753 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5754 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5755 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5757 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5758 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5759 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5760 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5764 * Check for unused global static functions and variables
5766 static void check_unused_globals(void)
5768 if (!warning.unused_function && !warning.unused_variable)
5771 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5772 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5775 type_t *const type = decl->type;
5777 if (is_type_function(skip_typeref(type))) {
5778 if (!warning.unused_function || decl->is_inline)
5781 s = (decl->init.statement != NULL ? "defined" : "declared");
5783 if (!warning.unused_variable)
5789 warningf(decl->source_position, "'%#T' %s but not used",
5790 type, decl->symbol, s);
5795 * Parse a translation unit.
5797 static translation_unit_t *parse_translation_unit(void)
5799 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5801 assert(global_scope == NULL);
5802 global_scope = &unit->scope;
5804 assert(scope == NULL);
5805 set_scope(&unit->scope);
5807 initialize_builtin_types();
5809 while(token.type != T_EOF) {
5810 if (token.type == ';') {
5811 /* TODO error in strict mode */
5812 warningf(HERE, "stray ';' outside of function");
5815 parse_external_declaration();
5819 assert(scope == &unit->scope);
5821 last_declaration = NULL;
5823 assert(global_scope == &unit->scope);
5824 check_unused_globals();
5825 global_scope = NULL;
5833 * @return the translation unit or NULL if errors occurred.
5835 translation_unit_t *parse(void)
5837 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5838 label_stack = NEW_ARR_F(stack_entry_t, 0);
5839 diagnostic_count = 0;
5843 type_set_output(stderr);
5844 ast_set_output(stderr);
5846 lookahead_bufpos = 0;
5847 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5850 translation_unit_t *unit = parse_translation_unit();
5852 DEL_ARR_F(environment_stack);
5853 DEL_ARR_F(label_stack);
5862 * Initialize the parser.
5864 void init_parser(void)
5866 init_expression_parsers();
5867 obstack_init(&temp_obst);
5869 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5870 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5874 * Terminate the parser.
5876 void exit_parser(void)
5878 obstack_free(&temp_obst, NULL);