7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(declaration_t *compound_declaration);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
202 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
203 [EXPR_CALL] = sizeof(call_expression_t),
204 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
205 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
206 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
207 [EXPR_SELECT] = sizeof(select_expression_t),
208 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
209 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
210 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
211 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
212 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
213 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
214 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
215 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
216 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
217 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
218 [EXPR_VA_START] = sizeof(va_start_expression_t),
219 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
220 [EXPR_STATEMENT] = sizeof(statement_expression_t),
222 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
223 return sizes[EXPR_UNARY_FIRST];
225 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
226 return sizes[EXPR_BINARY_FIRST];
228 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
229 assert(sizes[kind] != 0);
234 * Allocate an expression node of given kind and initialize all
237 static expression_t *allocate_expression_zero(expression_kind_t kind)
239 size_t size = get_expression_struct_size(kind);
240 expression_t *res = allocate_ast_zero(size);
242 res->base.kind = kind;
243 res->base.type = type_error_type;
248 * Returns the size of a type node.
250 * @param kind the type kind
252 static size_t get_type_struct_size(type_kind_t kind)
254 static const size_t sizes[] = {
255 [TYPE_ATOMIC] = sizeof(atomic_type_t),
256 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
257 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
258 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
259 [TYPE_ENUM] = sizeof(enum_type_t),
260 [TYPE_FUNCTION] = sizeof(function_type_t),
261 [TYPE_POINTER] = sizeof(pointer_type_t),
262 [TYPE_ARRAY] = sizeof(array_type_t),
263 [TYPE_BUILTIN] = sizeof(builtin_type_t),
264 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
265 [TYPE_TYPEOF] = sizeof(typeof_type_t),
267 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
268 assert(kind <= TYPE_TYPEOF);
269 assert(sizes[kind] != 0);
274 * Allocate a type node of given kind and initialize all
277 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
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;
284 res->base.source_position = source_position;
289 * Returns the size of an initializer node.
291 * @param kind the initializer kind
293 static size_t get_initializer_size(initializer_kind_t kind)
295 static const size_t sizes[] = {
296 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
297 [INITIALIZER_STRING] = sizeof(initializer_string_t),
298 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
299 [INITIALIZER_LIST] = sizeof(initializer_list_t)
301 assert(kind < sizeof(sizes) / sizeof(*sizes));
302 assert(sizes[kind] != 0);
307 * Allocate an initializer node of given kind and initialize all
310 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
312 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
319 * Free a type from the type obstack.
321 static void free_type(void *type)
323 obstack_free(type_obst, type);
327 * Returns the index of the top element of the environment stack.
329 static size_t environment_top(void)
331 return ARR_LEN(environment_stack);
335 * Returns the index of the top element of the label stack.
337 static size_t label_top(void)
339 return ARR_LEN(label_stack);
344 * Return the next token.
346 static inline void next_token(void)
348 token = lookahead_buffer[lookahead_bufpos];
349 lookahead_buffer[lookahead_bufpos] = lexer_token;
352 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
355 print_token(stderr, &token);
356 fprintf(stderr, "\n");
361 * Return the next token with a given lookahead.
363 static inline const token_t *look_ahead(int num)
365 assert(num > 0 && num <= MAX_LOOKAHEAD);
366 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
367 return &lookahead_buffer[pos];
370 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
373 * Report a parse error because an expected token was not found.
375 static void parse_error_expected(const char *message, ...)
377 if(message != NULL) {
378 errorf(HERE, "%s", message);
381 va_start(ap, message);
382 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
387 * Report a type error.
389 static void type_error(const char *msg, const source_position_t source_position,
392 errorf(source_position, "%s, but found type '%T'", msg, type);
396 * Report an incompatible type.
398 static void type_error_incompatible(const char *msg,
399 const source_position_t source_position, type_t *type1, type_t *type2)
401 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
405 * Eat an complete block, ie. '{ ... }'.
407 static void eat_block(void)
409 if(token.type == '{')
412 while(token.type != '}') {
413 if(token.type == T_EOF)
415 if(token.type == '{') {
425 * Eat a statement until an ';' token.
427 static void eat_statement(void)
429 while(token.type != ';') {
430 if(token.type == T_EOF)
432 if(token.type == '}')
434 if(token.type == '{') {
444 * Eat a parenthesed term, ie. '( ... )'.
446 static void eat_paren(void)
448 if(token.type == '(')
451 while(token.type != ')') {
452 if(token.type == T_EOF)
454 if(token.type == ')' || token.type == ';' || token.type == '}') {
457 if(token.type == '(') {
461 if(token.type == '{') {
470 #define expect(expected) \
471 if(UNLIKELY(token.type != (expected))) { \
472 parse_error_expected(NULL, (expected), 0); \
478 #define expect_block(expected) \
479 if(UNLIKELY(token.type != (expected))) { \
480 parse_error_expected(NULL, (expected), 0); \
486 #define expect_void(expected) \
487 if(UNLIKELY(token.type != (expected))) { \
488 parse_error_expected(NULL, (expected), 0); \
494 static void set_scope(scope_t *new_scope)
498 last_declaration = new_scope->declarations;
499 if(last_declaration != NULL) {
500 while(last_declaration->next != NULL) {
501 last_declaration = last_declaration->next;
507 * Search a symbol in a given namespace and returns its declaration or
508 * NULL if this symbol was not found.
510 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
512 declaration_t *declaration = symbol->declaration;
513 for( ; declaration != NULL; declaration = declaration->symbol_next) {
514 if(declaration->namespc == namespc)
522 * pushs an environment_entry on the environment stack and links the
523 * corresponding symbol to the new entry
525 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
527 symbol_t *symbol = declaration->symbol;
528 namespace_t namespc = (namespace_t)declaration->namespc;
530 /* remember old declaration */
532 entry.symbol = symbol;
533 entry.old_declaration = symbol->declaration;
534 entry.namespc = (unsigned short) namespc;
535 ARR_APP1(stack_entry_t, *stack_ptr, entry);
537 /* replace/add declaration into declaration list of the symbol */
538 if(symbol->declaration == NULL) {
539 symbol->declaration = declaration;
541 declaration_t *iter_last = NULL;
542 declaration_t *iter = symbol->declaration;
543 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
544 /* replace an entry? */
545 if(iter->namespc == namespc) {
546 if(iter_last == NULL) {
547 symbol->declaration = declaration;
549 iter_last->symbol_next = declaration;
551 declaration->symbol_next = iter->symbol_next;
556 assert(iter_last->symbol_next == NULL);
557 iter_last->symbol_next = declaration;
562 static void environment_push(declaration_t *declaration)
564 assert(declaration->source_position.input_name != NULL);
565 assert(declaration->parent_scope != NULL);
566 stack_push(&environment_stack, declaration);
569 static void label_push(declaration_t *declaration)
571 declaration->parent_scope = ¤t_function->scope;
572 stack_push(&label_stack, declaration);
576 * pops symbols from the environment stack until @p new_top is the top element
578 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
580 stack_entry_t *stack = *stack_ptr;
581 size_t top = ARR_LEN(stack);
584 assert(new_top <= top);
588 for(i = top; i > new_top; --i) {
589 stack_entry_t *entry = &stack[i - 1];
591 declaration_t *old_declaration = entry->old_declaration;
592 symbol_t *symbol = entry->symbol;
593 namespace_t namespc = (namespace_t)entry->namespc;
595 /* replace/remove declaration */
596 declaration_t *declaration = symbol->declaration;
597 assert(declaration != NULL);
598 if(declaration->namespc == namespc) {
599 if(old_declaration == NULL) {
600 symbol->declaration = declaration->symbol_next;
602 symbol->declaration = old_declaration;
605 declaration_t *iter_last = declaration;
606 declaration_t *iter = declaration->symbol_next;
607 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
608 /* replace an entry? */
609 if(iter->namespc == namespc) {
610 assert(iter_last != NULL);
611 iter_last->symbol_next = old_declaration;
612 old_declaration->symbol_next = iter->symbol_next;
616 assert(iter != NULL);
620 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
623 static void environment_pop_to(size_t new_top)
625 stack_pop_to(&environment_stack, new_top);
628 static void label_pop_to(size_t new_top)
630 stack_pop_to(&label_stack, new_top);
634 static int get_rank(const type_t *type)
636 assert(!is_typeref(type));
637 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
638 * and esp. footnote 108). However we can't fold constants (yet), so we
639 * can't decide whether unsigned int is possible, while int always works.
640 * (unsigned int would be preferable when possible... for stuff like
641 * struct { enum { ... } bla : 4; } ) */
642 if(type->kind == TYPE_ENUM)
643 return ATOMIC_TYPE_INT;
645 assert(type->kind == TYPE_ATOMIC);
646 return type->atomic.akind;
649 static type_t *promote_integer(type_t *type)
651 if(type->kind == TYPE_BITFIELD)
652 type = type->bitfield.base;
654 if(get_rank(type) < ATOMIC_TYPE_INT)
661 * Create a cast expression.
663 * @param expression the expression to cast
664 * @param dest_type the destination type
666 static expression_t *create_cast_expression(expression_t *expression,
669 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
671 cast->unary.value = expression;
672 cast->base.type = dest_type;
678 * Check if a given expression represents the 0 pointer constant.
680 static bool is_null_pointer_constant(const expression_t *expression)
682 /* skip void* cast */
683 if(expression->kind == EXPR_UNARY_CAST
684 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
685 expression = expression->unary.value;
688 /* TODO: not correct yet, should be any constant integer expression
689 * which evaluates to 0 */
690 if (expression->kind != EXPR_CONST)
693 type_t *const type = skip_typeref(expression->base.type);
694 if (!is_type_integer(type))
697 return expression->conste.v.int_value == 0;
701 * Create an implicit cast expression.
703 * @param expression the expression to cast
704 * @param dest_type the destination type
706 static expression_t *create_implicit_cast(expression_t *expression,
709 type_t *const source_type = expression->base.type;
711 if (source_type == dest_type)
714 return create_cast_expression(expression, dest_type);
717 /** Implements the rules from § 6.5.16.1 */
718 static type_t *semantic_assign(type_t *orig_type_left,
719 const expression_t *const right,
722 type_t *const orig_type_right = right->base.type;
723 type_t *const type_left = skip_typeref(orig_type_left);
724 type_t *const type_right = skip_typeref(orig_type_right);
726 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
727 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
728 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
729 && is_type_pointer(type_right))) {
730 return orig_type_left;
733 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
734 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
735 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
737 /* the left type has all qualifiers from the right type */
738 unsigned missing_qualifiers
739 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
740 if(missing_qualifiers != 0) {
741 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
742 return orig_type_left;
745 points_to_left = get_unqualified_type(points_to_left);
746 points_to_right = get_unqualified_type(points_to_right);
748 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
749 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
750 return orig_type_left;
753 if (!types_compatible(points_to_left, points_to_right)) {
754 warningf(right->base.source_position,
755 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
756 orig_type_left, context, right, orig_type_right);
759 return orig_type_left;
762 if (is_type_compound(type_left) && is_type_compound(type_right)) {
763 type_t *const unqual_type_left = get_unqualified_type(type_left);
764 type_t *const unqual_type_right = get_unqualified_type(type_right);
765 if (types_compatible(unqual_type_left, unqual_type_right)) {
766 return orig_type_left;
770 if (!is_type_valid(type_left))
773 if (!is_type_valid(type_right))
774 return orig_type_right;
779 static expression_t *parse_constant_expression(void)
781 /* start parsing at precedence 7 (conditional expression) */
782 expression_t *result = parse_sub_expression(7);
784 if(!is_constant_expression(result)) {
785 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
791 static expression_t *parse_assignment_expression(void)
793 /* start parsing at precedence 2 (assignment expression) */
794 return parse_sub_expression(2);
797 static type_t *make_global_typedef(const char *name, type_t *type)
799 symbol_t *const symbol = symbol_table_insert(name);
801 declaration_t *const declaration = allocate_declaration_zero();
802 declaration->namespc = NAMESPACE_NORMAL;
803 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
804 declaration->type = type;
805 declaration->symbol = symbol;
806 declaration->source_position = builtin_source_position;
808 record_declaration(declaration);
810 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
811 typedef_type->typedeft.declaration = declaration;
816 static string_t parse_string_literals(void)
818 assert(token.type == T_STRING_LITERAL);
819 string_t result = token.v.string;
823 while (token.type == T_STRING_LITERAL) {
824 result = concat_strings(&result, &token.v.string);
831 static void parse_attributes(void)
835 case T___attribute__: {
843 errorf(HERE, "EOF while parsing attribute");
862 if(token.type != T_STRING_LITERAL) {
863 parse_error_expected("while parsing assembler attribute",
868 parse_string_literals();
873 goto attributes_finished;
882 static designator_t *parse_designation(void)
884 if(token.type != '[' && token.type != '.')
887 designator_t *result = NULL;
888 designator_t *last = NULL;
891 designator_t *designator;
894 designator = allocate_ast_zero(sizeof(designator[0]));
896 designator->array_access = parse_constant_expression();
900 designator = allocate_ast_zero(sizeof(designator[0]));
902 if(token.type != T_IDENTIFIER) {
903 parse_error_expected("while parsing designator",
907 designator->symbol = token.v.symbol;
915 assert(designator != NULL);
917 last->next = designator;
926 static initializer_t *initializer_from_string(array_type_t *type,
927 const string_t *const string)
929 /* TODO: check len vs. size of array type */
932 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
933 initializer->string.string = *string;
938 static initializer_t *initializer_from_wide_string(array_type_t *const type,
939 wide_string_t *const string)
941 /* TODO: check len vs. size of array type */
944 initializer_t *const initializer =
945 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
946 initializer->wide_string.string = *string;
951 static initializer_t *initializer_from_expression(type_t *type,
952 expression_t *expression)
954 /* TODO check that expression is a constant expression */
956 /* § 6.7.8.14/15 char array may be initialized by string literals */
957 type_t *const expr_type = expression->base.type;
958 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
959 array_type_t *const array_type = &type->array;
960 type_t *const element_type = skip_typeref(array_type->element_type);
962 if (element_type->kind == TYPE_ATOMIC) {
963 switch (expression->kind) {
964 case EXPR_STRING_LITERAL:
965 if (element_type->atomic.akind == ATOMIC_TYPE_CHAR) {
966 return initializer_from_string(array_type,
967 &expression->string.value);
970 case EXPR_WIDE_STRING_LITERAL: {
971 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
972 if (get_unqualified_type(element_type) == bare_wchar_type) {
973 return initializer_from_wide_string(array_type,
974 &expression->wide_string.value);
984 type_t *const res_type = semantic_assign(type, expression, "initializer");
985 if (res_type == NULL)
988 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
989 result->value.value = create_implicit_cast(expression, res_type);
994 static initializer_t *parse_sub_initializer(type_t *type,
995 expression_t *expression);
997 static initializer_t *parse_sub_initializer_elem(type_t *type)
999 if(token.type == '{') {
1000 return parse_sub_initializer(type, NULL);
1003 expression_t *expression = parse_assignment_expression();
1004 return parse_sub_initializer(type, expression);
1007 static bool had_initializer_brace_warning;
1009 static void skip_designator(void)
1012 if(token.type == '.') {
1014 if(token.type == T_IDENTIFIER)
1016 } else if(token.type == '[') {
1018 parse_constant_expression();
1019 if(token.type == ']')
1027 static initializer_t *parse_sub_initializer(type_t *type,
1028 expression_t *expression)
1030 if(is_type_scalar(type)) {
1031 /* there might be extra {} hierarchies */
1032 if(token.type == '{') {
1034 if(!had_initializer_brace_warning) {
1035 warningf(HERE, "braces around scalar initializer");
1036 had_initializer_brace_warning = true;
1038 initializer_t *result = parse_sub_initializer(type, NULL);
1039 if(token.type == ',') {
1041 /* TODO: warn about excessive elements */
1047 if(expression == NULL) {
1048 expression = parse_assignment_expression();
1050 return initializer_from_expression(type, expression);
1053 /* does the expression match the currently looked at object to initialize */
1054 if(expression != NULL) {
1055 initializer_t *result = initializer_from_expression(type, expression);
1060 bool read_paren = false;
1061 if(token.type == '{') {
1066 /* descend into subtype */
1067 initializer_t *result = NULL;
1068 initializer_t **elems;
1069 if(is_type_array(type)) {
1070 if(token.type == '.') {
1072 "compound designator in initializer for array type '%T'",
1077 type_t *const element_type = skip_typeref(type->array.element_type);
1080 had_initializer_brace_warning = false;
1082 if(token.type == '{') {
1083 return parse_sub_initializer(type, NULL);
1086 if(expression == NULL) {
1087 expression = parse_assignment_expression();
1089 /* 6.7.8.14 + 15: we can have an optional {} around the string
1091 if(read_paren && (expression->kind == EXPR_STRING_LITERAL
1092 || expression->kind == EXPR_WIDE_STRING_LITERAL)) {
1093 initializer_t *result
1094 = initializer_from_expression(type, expression);
1095 if(result != NULL) {
1102 sub = parse_sub_initializer(element_type, expression);
1104 /* didn't match the subtypes -> try the parent type */
1106 assert(!read_paren);
1110 elems = NEW_ARR_F(initializer_t*, 0);
1111 ARR_APP1(initializer_t*, elems, sub);
1114 if(token.type == '}')
1117 if(token.type == '}')
1120 sub = parse_sub_initializer_elem(element_type);
1122 /* TODO error, do nicer cleanup */
1123 errorf(HERE, "member initializer didn't match");
1127 ARR_APP1(initializer_t*, elems, sub);
1130 assert(is_type_compound(type));
1131 scope_t *const scope = &type->compound.declaration->scope;
1133 if(token.type == '[') {
1135 "array designator in initializer for compound type '%T'",
1140 declaration_t *first = scope->declarations;
1143 type_t *first_type = first->type;
1144 first_type = skip_typeref(first_type);
1147 had_initializer_brace_warning = false;
1148 if(expression == NULL) {
1149 sub = parse_sub_initializer_elem(first_type);
1151 sub = parse_sub_initializer(first_type, expression);
1154 /* didn't match the subtypes -> try our parent type */
1156 assert(!read_paren);
1160 elems = NEW_ARR_F(initializer_t*, 0);
1161 ARR_APP1(initializer_t*, elems, sub);
1163 declaration_t *iter = first->next;
1164 for( ; iter != NULL; iter = iter->next) {
1165 if(iter->symbol == NULL)
1167 if(iter->namespc != NAMESPACE_NORMAL)
1170 if(token.type == '}')
1173 if(token.type == '}')
1176 type_t *iter_type = iter->type;
1177 iter_type = skip_typeref(iter_type);
1179 sub = parse_sub_initializer_elem(iter_type);
1181 /* TODO error, do nicer cleanup */
1182 errorf(HERE, "member initializer didn't match");
1186 ARR_APP1(initializer_t*, elems, sub);
1190 int len = ARR_LEN(elems);
1191 size_t elems_size = sizeof(initializer_t*) * len;
1193 initializer_list_t *init = allocate_ast_zero(sizeof(init[0]) + elems_size);
1195 init->initializer.kind = INITIALIZER_LIST;
1197 memcpy(init->initializers, elems, elems_size);
1200 result = (initializer_t*) init;
1203 if(token.type == ',')
1210 static initializer_t *parse_initializer(type_t *const orig_type)
1212 initializer_t *result;
1214 type_t *const type = skip_typeref(orig_type);
1216 if(token.type != '{') {
1217 expression_t *expression = parse_assignment_expression();
1218 initializer_t *initializer = initializer_from_expression(type, expression);
1219 if(initializer == NULL) {
1221 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1222 expression, expression->base.type, orig_type);
1227 if(is_type_scalar(type)) {
1231 expression_t *expression = parse_assignment_expression();
1232 result = initializer_from_expression(type, expression);
1234 if(token.type == ',')
1240 result = parse_sub_initializer(type, NULL);
1246 static declaration_t *append_declaration(declaration_t *declaration);
1248 static declaration_t *parse_compound_type_specifier(bool is_struct)
1256 symbol_t *symbol = NULL;
1257 declaration_t *declaration = NULL;
1259 if (token.type == T___attribute__) {
1264 if(token.type == T_IDENTIFIER) {
1265 symbol = token.v.symbol;
1269 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1271 declaration = get_declaration(symbol, NAMESPACE_UNION);
1273 } else if(token.type != '{') {
1275 parse_error_expected("while parsing struct type specifier",
1276 T_IDENTIFIER, '{', 0);
1278 parse_error_expected("while parsing union type specifier",
1279 T_IDENTIFIER, '{', 0);
1285 if(declaration == NULL) {
1286 declaration = allocate_declaration_zero();
1287 declaration->namespc =
1288 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1289 declaration->source_position = token.source_position;
1290 declaration->symbol = symbol;
1291 declaration->parent_scope = scope;
1292 if (symbol != NULL) {
1293 environment_push(declaration);
1295 append_declaration(declaration);
1298 if(token.type == '{') {
1299 if(declaration->init.is_defined) {
1300 assert(symbol != NULL);
1301 errorf(HERE, "multiple definitions of '%s %Y'",
1302 is_struct ? "struct" : "union", symbol);
1303 declaration->scope.declarations = NULL;
1305 declaration->init.is_defined = true;
1307 parse_compound_type_entries(declaration);
1314 static void parse_enum_entries(type_t *const enum_type)
1318 if(token.type == '}') {
1320 errorf(HERE, "empty enum not allowed");
1325 if(token.type != T_IDENTIFIER) {
1326 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1331 declaration_t *const entry = allocate_declaration_zero();
1332 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1333 entry->type = enum_type;
1334 entry->symbol = token.v.symbol;
1335 entry->source_position = token.source_position;
1338 if(token.type == '=') {
1340 entry->init.enum_value = parse_constant_expression();
1345 record_declaration(entry);
1347 if(token.type != ',')
1350 } while(token.type != '}');
1355 static type_t *parse_enum_specifier(void)
1359 declaration_t *declaration;
1362 if(token.type == T_IDENTIFIER) {
1363 symbol = token.v.symbol;
1366 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1367 } else if(token.type != '{') {
1368 parse_error_expected("while parsing enum type specifier",
1369 T_IDENTIFIER, '{', 0);
1376 if(declaration == NULL) {
1377 declaration = allocate_declaration_zero();
1378 declaration->namespc = NAMESPACE_ENUM;
1379 declaration->source_position = token.source_position;
1380 declaration->symbol = symbol;
1381 declaration->parent_scope = scope;
1384 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1385 type->enumt.declaration = declaration;
1387 if(token.type == '{') {
1388 if(declaration->init.is_defined) {
1389 errorf(HERE, "multiple definitions of enum %Y", symbol);
1391 if (symbol != NULL) {
1392 environment_push(declaration);
1394 append_declaration(declaration);
1395 declaration->init.is_defined = 1;
1397 parse_enum_entries(type);
1405 * if a symbol is a typedef to another type, return true
1407 static bool is_typedef_symbol(symbol_t *symbol)
1409 const declaration_t *const declaration =
1410 get_declaration(symbol, NAMESPACE_NORMAL);
1412 declaration != NULL &&
1413 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1416 static type_t *parse_typeof(void)
1424 expression_t *expression = NULL;
1427 switch(token.type) {
1428 case T___extension__:
1429 /* this can be a prefix to a typename or an expression */
1430 /* we simply eat it now. */
1433 } while(token.type == T___extension__);
1437 if(is_typedef_symbol(token.v.symbol)) {
1438 type = parse_typename();
1440 expression = parse_expression();
1441 type = expression->base.type;
1446 type = parse_typename();
1450 expression = parse_expression();
1451 type = expression->base.type;
1457 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1458 typeof_type->typeoft.expression = expression;
1459 typeof_type->typeoft.typeof_type = type;
1465 SPECIFIER_SIGNED = 1 << 0,
1466 SPECIFIER_UNSIGNED = 1 << 1,
1467 SPECIFIER_LONG = 1 << 2,
1468 SPECIFIER_INT = 1 << 3,
1469 SPECIFIER_DOUBLE = 1 << 4,
1470 SPECIFIER_CHAR = 1 << 5,
1471 SPECIFIER_SHORT = 1 << 6,
1472 SPECIFIER_LONG_LONG = 1 << 7,
1473 SPECIFIER_FLOAT = 1 << 8,
1474 SPECIFIER_BOOL = 1 << 9,
1475 SPECIFIER_VOID = 1 << 10,
1476 #ifdef PROVIDE_COMPLEX
1477 SPECIFIER_COMPLEX = 1 << 11,
1478 SPECIFIER_IMAGINARY = 1 << 12,
1482 static type_t *create_builtin_type(symbol_t *const symbol,
1483 type_t *const real_type)
1485 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1486 type->builtin.symbol = symbol;
1487 type->builtin.real_type = real_type;
1489 type_t *result = typehash_insert(type);
1490 if (type != result) {
1497 static type_t *get_typedef_type(symbol_t *symbol)
1499 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1500 if(declaration == NULL
1501 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1504 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1505 type->typedeft.declaration = declaration;
1510 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1512 type_t *type = NULL;
1513 unsigned type_qualifiers = 0;
1514 unsigned type_specifiers = 0;
1517 specifiers->source_position = token.source_position;
1520 switch(token.type) {
1523 #define MATCH_STORAGE_CLASS(token, class) \
1525 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1526 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1528 specifiers->storage_class = class; \
1532 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1533 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1534 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1535 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1536 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1539 switch (specifiers->storage_class) {
1540 case STORAGE_CLASS_NONE:
1541 specifiers->storage_class = STORAGE_CLASS_THREAD;
1544 case STORAGE_CLASS_EXTERN:
1545 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1548 case STORAGE_CLASS_STATIC:
1549 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1553 errorf(HERE, "multiple storage classes in declaration specifiers");
1559 /* type qualifiers */
1560 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1562 type_qualifiers |= qualifier; \
1566 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1567 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1568 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1570 case T___extension__:
1575 /* type specifiers */
1576 #define MATCH_SPECIFIER(token, specifier, name) \
1579 if(type_specifiers & specifier) { \
1580 errorf(HERE, "multiple " name " type specifiers given"); \
1582 type_specifiers |= specifier; \
1586 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1587 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1588 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1589 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1590 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1591 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1592 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1593 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1594 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1595 #ifdef PROVIDE_COMPLEX
1596 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1597 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1600 /* only in microsoft mode */
1601 specifiers->decl_modifiers |= DM_FORCEINLINE;
1605 specifiers->is_inline = true;
1610 if(type_specifiers & SPECIFIER_LONG_LONG) {
1611 errorf(HERE, "multiple type specifiers given");
1612 } else if(type_specifiers & SPECIFIER_LONG) {
1613 type_specifiers |= SPECIFIER_LONG_LONG;
1615 type_specifiers |= SPECIFIER_LONG;
1620 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1622 type->compound.declaration = parse_compound_type_specifier(true);
1626 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1628 type->compound.declaration = parse_compound_type_specifier(false);
1632 type = parse_enum_specifier();
1635 type = parse_typeof();
1637 case T___builtin_va_list:
1638 type = duplicate_type(type_valist);
1642 case T___attribute__:
1646 case T_IDENTIFIER: {
1647 /* only parse identifier if we haven't found a type yet */
1648 if(type != NULL || type_specifiers != 0)
1649 goto finish_specifiers;
1651 type_t *typedef_type = get_typedef_type(token.v.symbol);
1653 if(typedef_type == NULL)
1654 goto finish_specifiers;
1657 type = typedef_type;
1661 /* function specifier */
1663 goto finish_specifiers;
1670 atomic_type_kind_t atomic_type;
1672 /* match valid basic types */
1673 switch(type_specifiers) {
1674 case SPECIFIER_VOID:
1675 atomic_type = ATOMIC_TYPE_VOID;
1677 case SPECIFIER_CHAR:
1678 atomic_type = ATOMIC_TYPE_CHAR;
1680 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1681 atomic_type = ATOMIC_TYPE_SCHAR;
1683 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1684 atomic_type = ATOMIC_TYPE_UCHAR;
1686 case SPECIFIER_SHORT:
1687 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1688 case SPECIFIER_SHORT | SPECIFIER_INT:
1689 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1690 atomic_type = ATOMIC_TYPE_SHORT;
1692 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1693 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1694 atomic_type = ATOMIC_TYPE_USHORT;
1697 case SPECIFIER_SIGNED:
1698 case SPECIFIER_SIGNED | SPECIFIER_INT:
1699 atomic_type = ATOMIC_TYPE_INT;
1701 case SPECIFIER_UNSIGNED:
1702 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1703 atomic_type = ATOMIC_TYPE_UINT;
1705 case SPECIFIER_LONG:
1706 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1707 case SPECIFIER_LONG | SPECIFIER_INT:
1708 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1709 atomic_type = ATOMIC_TYPE_LONG;
1711 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1712 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1713 atomic_type = ATOMIC_TYPE_ULONG;
1715 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1716 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1717 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1718 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1720 atomic_type = ATOMIC_TYPE_LONGLONG;
1722 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1723 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1725 atomic_type = ATOMIC_TYPE_ULONGLONG;
1727 case SPECIFIER_FLOAT:
1728 atomic_type = ATOMIC_TYPE_FLOAT;
1730 case SPECIFIER_DOUBLE:
1731 atomic_type = ATOMIC_TYPE_DOUBLE;
1733 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1734 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1736 case SPECIFIER_BOOL:
1737 atomic_type = ATOMIC_TYPE_BOOL;
1739 #ifdef PROVIDE_COMPLEX
1740 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1741 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1743 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1744 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1746 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1747 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1749 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1750 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1752 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1753 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1755 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1756 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1760 /* invalid specifier combination, give an error message */
1761 if(type_specifiers == 0) {
1762 if (! strict_mode) {
1763 if (warning.implicit_int) {
1764 warningf(HERE, "no type specifiers in declaration, using 'int'");
1766 atomic_type = ATOMIC_TYPE_INT;
1769 errorf(HERE, "no type specifiers given in declaration");
1771 } else if((type_specifiers & SPECIFIER_SIGNED) &&
1772 (type_specifiers & SPECIFIER_UNSIGNED)) {
1773 errorf(HERE, "signed and unsigned specifiers gives");
1774 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
1775 errorf(HERE, "only integer types can be signed or unsigned");
1777 errorf(HERE, "multiple datatypes in declaration");
1779 atomic_type = ATOMIC_TYPE_INVALID;
1782 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
1783 type->atomic.akind = atomic_type;
1786 if(type_specifiers != 0) {
1787 errorf(HERE, "multiple datatypes in declaration");
1791 type->base.qualifiers = type_qualifiers;
1793 type_t *result = typehash_insert(type);
1794 if(newtype && result != type) {
1798 specifiers->type = result;
1801 static type_qualifiers_t parse_type_qualifiers(void)
1803 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
1806 switch(token.type) {
1807 /* type qualifiers */
1808 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1809 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1810 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1813 return type_qualifiers;
1818 static declaration_t *parse_identifier_list(void)
1820 declaration_t *declarations = NULL;
1821 declaration_t *last_declaration = NULL;
1823 declaration_t *const declaration = allocate_declaration_zero();
1824 declaration->type = NULL; /* a K&R parameter list has no types, yet */
1825 declaration->source_position = token.source_position;
1826 declaration->symbol = token.v.symbol;
1829 if(last_declaration != NULL) {
1830 last_declaration->next = declaration;
1832 declarations = declaration;
1834 last_declaration = declaration;
1836 if(token.type != ',')
1839 } while(token.type == T_IDENTIFIER);
1841 return declarations;
1844 static void semantic_parameter(declaration_t *declaration)
1846 /* TODO: improve error messages */
1848 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
1849 errorf(HERE, "typedef not allowed in parameter list");
1850 } else if(declaration->storage_class != STORAGE_CLASS_NONE
1851 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
1852 errorf(HERE, "parameter may only have none or register storage class");
1855 type_t *const orig_type = declaration->type;
1856 type_t * type = skip_typeref(orig_type);
1858 /* Array as last part of a parameter type is just syntactic sugar. Turn it
1859 * into a pointer. § 6.7.5.3 (7) */
1860 if (is_type_array(type)) {
1861 type_t *const element_type = type->array.element_type;
1863 type = make_pointer_type(element_type, type->base.qualifiers);
1865 declaration->type = type;
1868 if(is_type_incomplete(type)) {
1869 errorf(HERE, "incomplete type ('%T') not allowed for parameter '%Y'",
1870 orig_type, declaration->symbol);
1874 static declaration_t *parse_parameter(void)
1876 declaration_specifiers_t specifiers;
1877 memset(&specifiers, 0, sizeof(specifiers));
1879 parse_declaration_specifiers(&specifiers);
1881 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
1883 semantic_parameter(declaration);
1888 static declaration_t *parse_parameters(function_type_t *type)
1890 if(token.type == T_IDENTIFIER) {
1891 symbol_t *symbol = token.v.symbol;
1892 if(!is_typedef_symbol(symbol)) {
1893 type->kr_style_parameters = true;
1894 return parse_identifier_list();
1898 if(token.type == ')') {
1899 type->unspecified_parameters = 1;
1902 if(token.type == T_void && look_ahead(1)->type == ')') {
1907 declaration_t *declarations = NULL;
1908 declaration_t *declaration;
1909 declaration_t *last_declaration = NULL;
1910 function_parameter_t *parameter;
1911 function_parameter_t *last_parameter = NULL;
1914 switch(token.type) {
1918 return declarations;
1921 case T___extension__:
1923 declaration = parse_parameter();
1925 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
1926 memset(parameter, 0, sizeof(parameter[0]));
1927 parameter->type = declaration->type;
1929 if(last_parameter != NULL) {
1930 last_declaration->next = declaration;
1931 last_parameter->next = parameter;
1933 type->parameters = parameter;
1934 declarations = declaration;
1936 last_parameter = parameter;
1937 last_declaration = declaration;
1941 return declarations;
1943 if(token.type != ',')
1944 return declarations;
1954 } construct_type_kind_t;
1956 typedef struct construct_type_t construct_type_t;
1957 struct construct_type_t {
1958 construct_type_kind_t kind;
1959 construct_type_t *next;
1962 typedef struct parsed_pointer_t parsed_pointer_t;
1963 struct parsed_pointer_t {
1964 construct_type_t construct_type;
1965 type_qualifiers_t type_qualifiers;
1968 typedef struct construct_function_type_t construct_function_type_t;
1969 struct construct_function_type_t {
1970 construct_type_t construct_type;
1971 type_t *function_type;
1974 typedef struct parsed_array_t parsed_array_t;
1975 struct parsed_array_t {
1976 construct_type_t construct_type;
1977 type_qualifiers_t type_qualifiers;
1983 typedef struct construct_base_type_t construct_base_type_t;
1984 struct construct_base_type_t {
1985 construct_type_t construct_type;
1989 static construct_type_t *parse_pointer_declarator(void)
1993 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
1994 memset(pointer, 0, sizeof(pointer[0]));
1995 pointer->construct_type.kind = CONSTRUCT_POINTER;
1996 pointer->type_qualifiers = parse_type_qualifiers();
1998 return (construct_type_t*) pointer;
2001 static construct_type_t *parse_array_declarator(void)
2005 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2006 memset(array, 0, sizeof(array[0]));
2007 array->construct_type.kind = CONSTRUCT_ARRAY;
2009 if(token.type == T_static) {
2010 array->is_static = true;
2014 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2015 if(type_qualifiers != 0) {
2016 if(token.type == T_static) {
2017 array->is_static = true;
2021 array->type_qualifiers = type_qualifiers;
2023 if(token.type == '*' && look_ahead(1)->type == ']') {
2024 array->is_variable = true;
2026 } else if(token.type != ']') {
2027 array->size = parse_assignment_expression();
2032 return (construct_type_t*) array;
2035 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2039 type_t *type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2041 declaration_t *parameters = parse_parameters(&type->function);
2042 if(declaration != NULL) {
2043 declaration->scope.declarations = parameters;
2046 construct_function_type_t *construct_function_type =
2047 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2048 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2049 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2050 construct_function_type->function_type = type;
2054 return (construct_type_t*) construct_function_type;
2057 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2058 bool may_be_abstract)
2060 /* construct a single linked list of construct_type_t's which describe
2061 * how to construct the final declarator type */
2062 construct_type_t *first = NULL;
2063 construct_type_t *last = NULL;
2066 while(token.type == '*') {
2067 construct_type_t *type = parse_pointer_declarator();
2078 /* TODO: find out if this is correct */
2081 construct_type_t *inner_types = NULL;
2083 switch(token.type) {
2085 if(declaration == NULL) {
2086 errorf(HERE, "no identifier expected in typename");
2088 declaration->symbol = token.v.symbol;
2089 declaration->source_position = token.source_position;
2095 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2101 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2102 /* avoid a loop in the outermost scope, because eat_statement doesn't
2104 if(token.type == '}' && current_function == NULL) {
2112 construct_type_t *p = last;
2115 construct_type_t *type;
2116 switch(token.type) {
2118 type = parse_function_declarator(declaration);
2121 type = parse_array_declarator();
2124 goto declarator_finished;
2127 /* insert in the middle of the list (behind p) */
2129 type->next = p->next;
2140 declarator_finished:
2143 /* append inner_types at the end of the list, we don't to set last anymore
2144 * as it's not needed anymore */
2146 assert(first == NULL);
2147 first = inner_types;
2149 last->next = inner_types;
2155 static type_t *construct_declarator_type(construct_type_t *construct_list,
2158 construct_type_t *iter = construct_list;
2159 for( ; iter != NULL; iter = iter->next) {
2160 switch(iter->kind) {
2161 case CONSTRUCT_INVALID:
2162 panic("invalid type construction found");
2163 case CONSTRUCT_FUNCTION: {
2164 construct_function_type_t *construct_function_type
2165 = (construct_function_type_t*) iter;
2167 type_t *function_type = construct_function_type->function_type;
2169 function_type->function.return_type = type;
2171 type_t *skipped_return_type = skip_typeref(type);
2172 if (is_type_function(skipped_return_type)) {
2173 errorf(HERE, "function returning function is not allowed");
2174 type = type_error_type;
2175 } else if (is_type_array(skipped_return_type)) {
2176 errorf(HERE, "function returning array is not allowed");
2177 type = type_error_type;
2179 type = function_type;
2184 case CONSTRUCT_POINTER: {
2185 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2186 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2187 pointer_type->pointer.points_to = type;
2188 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2190 type = pointer_type;
2194 case CONSTRUCT_ARRAY: {
2195 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2196 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2198 array_type->base.qualifiers = parsed_array->type_qualifiers;
2199 array_type->array.element_type = type;
2200 array_type->array.is_static = parsed_array->is_static;
2201 array_type->array.is_variable = parsed_array->is_variable;
2202 array_type->array.size = parsed_array->size;
2204 type_t *skipped_type = skip_typeref(type);
2205 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2206 errorf(HERE, "array of void is not allowed");
2207 type = type_error_type;
2215 type_t *hashed_type = typehash_insert(type);
2216 if(hashed_type != type) {
2217 /* the function type was constructed earlier freeing it here will
2218 * destroy other types... */
2219 if(iter->kind != CONSTRUCT_FUNCTION) {
2229 static declaration_t *parse_declarator(
2230 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2232 declaration_t *const declaration = allocate_declaration_zero();
2233 declaration->storage_class = specifiers->storage_class;
2234 declaration->modifiers = specifiers->decl_modifiers;
2235 declaration->is_inline = specifiers->is_inline;
2237 construct_type_t *construct_type
2238 = parse_inner_declarator(declaration, may_be_abstract);
2239 type_t *const type = specifiers->type;
2240 declaration->type = construct_declarator_type(construct_type, type);
2242 if(construct_type != NULL) {
2243 obstack_free(&temp_obst, construct_type);
2249 static type_t *parse_abstract_declarator(type_t *base_type)
2251 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2253 type_t *result = construct_declarator_type(construct_type, base_type);
2254 if(construct_type != NULL) {
2255 obstack_free(&temp_obst, construct_type);
2261 static declaration_t *append_declaration(declaration_t* const declaration)
2263 if (last_declaration != NULL) {
2264 last_declaration->next = declaration;
2266 scope->declarations = declaration;
2268 last_declaration = declaration;
2273 * Check if the declaration of main is suspicious. main should be a
2274 * function with external linkage, returning int, taking either zero
2275 * arguments, two, or three arguments of appropriate types, ie.
2277 * int main([ int argc, char **argv [, char **env ] ]).
2279 * @param decl the declaration to check
2280 * @param type the function type of the declaration
2282 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2284 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2285 warningf(decl->source_position, "'main' is normally a non-static function");
2287 if (skip_typeref(func_type->return_type) != type_int) {
2288 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2290 const function_parameter_t *parm = func_type->parameters;
2292 type_t *const first_type = parm->type;
2293 if (!types_compatible(skip_typeref(first_type), type_int)) {
2294 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2298 type_t *const second_type = parm->type;
2299 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2300 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2304 type_t *const third_type = parm->type;
2305 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2306 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2310 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2314 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2320 * Check if a symbol is the equal to "main".
2322 static bool is_sym_main(const symbol_t *const sym)
2324 return strcmp(sym->string, "main") == 0;
2327 static declaration_t *internal_record_declaration(
2328 declaration_t *const declaration,
2329 const bool is_function_definition)
2331 const symbol_t *const symbol = declaration->symbol;
2332 const namespace_t namespc = (namespace_t)declaration->namespc;
2334 type_t *const orig_type = declaration->type;
2335 type_t *const type = skip_typeref(orig_type);
2336 if (is_type_function(type) &&
2337 type->function.unspecified_parameters &&
2338 warning.strict_prototypes) {
2339 warningf(declaration->source_position,
2340 "function declaration '%#T' is not a prototype",
2341 orig_type, declaration->symbol);
2344 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2345 check_type_of_main(declaration, &type->function);
2348 assert(declaration->symbol != NULL);
2349 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2351 assert(declaration != previous_declaration);
2352 if (previous_declaration != NULL) {
2353 if (previous_declaration->parent_scope == scope) {
2354 /* can happen for K&R style declarations */
2355 if(previous_declaration->type == NULL) {
2356 previous_declaration->type = declaration->type;
2359 const type_t *prev_type = skip_typeref(previous_declaration->type);
2360 if (!types_compatible(type, prev_type)) {
2361 errorf(declaration->source_position,
2362 "declaration '%#T' is incompatible with "
2363 "previous declaration '%#T'",
2364 orig_type, symbol, previous_declaration->type, symbol);
2365 errorf(previous_declaration->source_position,
2366 "previous declaration of '%Y' was here", symbol);
2368 unsigned old_storage_class
2369 = previous_declaration->storage_class;
2370 unsigned new_storage_class = declaration->storage_class;
2372 if(is_type_incomplete(prev_type)) {
2373 previous_declaration->type = type;
2377 /* pretend no storage class means extern for function
2378 * declarations (except if the previous declaration is neither
2379 * none nor extern) */
2380 if (is_type_function(type)) {
2381 switch (old_storage_class) {
2382 case STORAGE_CLASS_NONE:
2383 old_storage_class = STORAGE_CLASS_EXTERN;
2385 case STORAGE_CLASS_EXTERN:
2386 if (is_function_definition) {
2387 if (warning.missing_prototypes &&
2388 prev_type->function.unspecified_parameters &&
2389 !is_sym_main(symbol)) {
2390 warningf(declaration->source_position,
2391 "no previous prototype for '%#T'",
2394 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2395 new_storage_class = STORAGE_CLASS_EXTERN;
2403 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2404 new_storage_class == STORAGE_CLASS_EXTERN) {
2405 warn_redundant_declaration:
2406 if (warning.redundant_decls) {
2407 warningf(declaration->source_position,
2408 "redundant declaration for '%Y'", symbol);
2409 warningf(previous_declaration->source_position,
2410 "previous declaration of '%Y' was here",
2413 } else if (current_function == NULL) {
2414 if (old_storage_class != STORAGE_CLASS_STATIC &&
2415 new_storage_class == STORAGE_CLASS_STATIC) {
2416 errorf(declaration->source_position,
2417 "static declaration of '%Y' follows non-static declaration",
2419 errorf(previous_declaration->source_position,
2420 "previous declaration of '%Y' was here", symbol);
2422 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2423 goto warn_redundant_declaration;
2425 if (new_storage_class == STORAGE_CLASS_NONE) {
2426 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2430 if (old_storage_class == new_storage_class) {
2431 errorf(declaration->source_position,
2432 "redeclaration of '%Y'", symbol);
2434 errorf(declaration->source_position,
2435 "redeclaration of '%Y' with different linkage",
2438 errorf(previous_declaration->source_position,
2439 "previous declaration of '%Y' was here", symbol);
2442 return previous_declaration;
2444 } else if (is_function_definition) {
2445 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2446 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2447 warningf(declaration->source_position,
2448 "no previous prototype for '%#T'", orig_type, symbol);
2449 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2450 warningf(declaration->source_position,
2451 "no previous declaration for '%#T'", orig_type,
2455 } else if (warning.missing_declarations &&
2456 scope == global_scope &&
2457 !is_type_function(type) && (
2458 declaration->storage_class == STORAGE_CLASS_NONE ||
2459 declaration->storage_class == STORAGE_CLASS_THREAD
2461 warningf(declaration->source_position,
2462 "no previous declaration for '%#T'", orig_type, symbol);
2465 assert(declaration->parent_scope == NULL);
2466 assert(scope != NULL);
2468 declaration->parent_scope = scope;
2470 environment_push(declaration);
2471 return append_declaration(declaration);
2474 static declaration_t *record_declaration(declaration_t *declaration)
2476 return internal_record_declaration(declaration, false);
2479 static declaration_t *record_function_definition(declaration_t *declaration)
2481 return internal_record_declaration(declaration, true);
2484 static void parser_error_multiple_definition(declaration_t *declaration,
2485 const source_position_t source_position)
2487 errorf(source_position, "multiple definition of symbol '%Y'",
2488 declaration->symbol);
2489 errorf(declaration->source_position,
2490 "this is the location of the previous definition.");
2493 static bool is_declaration_specifier(const token_t *token,
2494 bool only_type_specifiers)
2496 switch(token->type) {
2500 return is_typedef_symbol(token->v.symbol);
2502 case T___extension__:
2505 return !only_type_specifiers;
2512 static void parse_init_declarator_rest(declaration_t *declaration)
2516 type_t *orig_type = declaration->type;
2517 type_t *type = type = skip_typeref(orig_type);
2519 if(declaration->init.initializer != NULL) {
2520 parser_error_multiple_definition(declaration, token.source_position);
2523 initializer_t *initializer = parse_initializer(type);
2525 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2526 * the array type size */
2527 if(is_type_array(type) && initializer != NULL) {
2528 array_type_t *array_type = &type->array;
2530 if(array_type->size == NULL) {
2531 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2533 cnst->base.type = type_size_t;
2535 switch (initializer->kind) {
2536 case INITIALIZER_LIST: {
2537 cnst->conste.v.int_value = initializer->list.len;
2541 case INITIALIZER_STRING: {
2542 cnst->conste.v.int_value = initializer->string.string.size;
2546 case INITIALIZER_WIDE_STRING: {
2547 cnst->conste.v.int_value = initializer->wide_string.string.size;
2552 panic("invalid initializer type");
2555 array_type->size = cnst;
2556 array_type->has_implicit_size = true;
2560 if(is_type_function(type)) {
2561 errorf(declaration->source_position,
2562 "initializers not allowed for function types at declator '%Y' (type '%T')",
2563 declaration->symbol, orig_type);
2565 declaration->init.initializer = initializer;
2569 /* parse rest of a declaration without any declarator */
2570 static void parse_anonymous_declaration_rest(
2571 const declaration_specifiers_t *specifiers,
2572 parsed_declaration_func finished_declaration)
2576 declaration_t *const declaration = allocate_declaration_zero();
2577 declaration->type = specifiers->type;
2578 declaration->storage_class = specifiers->storage_class;
2579 declaration->source_position = specifiers->source_position;
2581 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2582 warningf(declaration->source_position, "useless storage class in empty declaration");
2585 type_t *type = declaration->type;
2586 switch (type->kind) {
2587 case TYPE_COMPOUND_STRUCT:
2588 case TYPE_COMPOUND_UNION: {
2589 if (type->compound.declaration->symbol == NULL) {
2590 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2599 warningf(declaration->source_position, "empty declaration");
2603 finished_declaration(declaration);
2606 static void parse_declaration_rest(declaration_t *ndeclaration,
2607 const declaration_specifiers_t *specifiers,
2608 parsed_declaration_func finished_declaration)
2611 declaration_t *declaration = finished_declaration(ndeclaration);
2613 type_t *orig_type = declaration->type;
2614 type_t *type = skip_typeref(orig_type);
2616 if (type->kind != TYPE_FUNCTION &&
2617 declaration->is_inline &&
2618 is_type_valid(type)) {
2619 warningf(declaration->source_position,
2620 "variable '%Y' declared 'inline'\n", declaration->symbol);
2623 if(token.type == '=') {
2624 parse_init_declarator_rest(declaration);
2627 if(token.type != ',')
2631 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2636 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2638 symbol_t *symbol = declaration->symbol;
2639 if(symbol == NULL) {
2640 errorf(HERE, "anonymous declaration not valid as function parameter");
2643 namespace_t namespc = (namespace_t) declaration->namespc;
2644 if(namespc != NAMESPACE_NORMAL) {
2645 return record_declaration(declaration);
2648 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2649 if(previous_declaration == NULL ||
2650 previous_declaration->parent_scope != scope) {
2651 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2656 if(previous_declaration->type == NULL) {
2657 previous_declaration->type = declaration->type;
2658 previous_declaration->storage_class = declaration->storage_class;
2659 previous_declaration->parent_scope = scope;
2660 return previous_declaration;
2662 return record_declaration(declaration);
2666 static void parse_declaration(parsed_declaration_func finished_declaration)
2668 declaration_specifiers_t specifiers;
2669 memset(&specifiers, 0, sizeof(specifiers));
2670 parse_declaration_specifiers(&specifiers);
2672 if(token.type == ';') {
2673 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2675 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2676 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2680 static void parse_kr_declaration_list(declaration_t *declaration)
2682 type_t *type = skip_typeref(declaration->type);
2683 if(!is_type_function(type))
2686 if(!type->function.kr_style_parameters)
2689 /* push function parameters */
2690 int top = environment_top();
2691 scope_t *last_scope = scope;
2692 set_scope(&declaration->scope);
2694 declaration_t *parameter = declaration->scope.declarations;
2695 for( ; parameter != NULL; parameter = parameter->next) {
2696 assert(parameter->parent_scope == NULL);
2697 parameter->parent_scope = scope;
2698 environment_push(parameter);
2701 /* parse declaration list */
2702 while(is_declaration_specifier(&token, false)) {
2703 parse_declaration(finished_kr_declaration);
2706 /* pop function parameters */
2707 assert(scope == &declaration->scope);
2708 set_scope(last_scope);
2709 environment_pop_to(top);
2711 /* update function type */
2712 type_t *new_type = duplicate_type(type);
2713 new_type->function.kr_style_parameters = false;
2715 function_parameter_t *parameters = NULL;
2716 function_parameter_t *last_parameter = NULL;
2718 declaration_t *parameter_declaration = declaration->scope.declarations;
2719 for( ; parameter_declaration != NULL;
2720 parameter_declaration = parameter_declaration->next) {
2721 type_t *parameter_type = parameter_declaration->type;
2722 if(parameter_type == NULL) {
2724 errorf(HERE, "no type specified for function parameter '%Y'",
2725 parameter_declaration->symbol);
2727 if (warning.implicit_int) {
2728 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2729 parameter_declaration->symbol);
2731 parameter_type = type_int;
2732 parameter_declaration->type = parameter_type;
2736 semantic_parameter(parameter_declaration);
2737 parameter_type = parameter_declaration->type;
2739 function_parameter_t *function_parameter
2740 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2741 memset(function_parameter, 0, sizeof(function_parameter[0]));
2743 function_parameter->type = parameter_type;
2744 if(last_parameter != NULL) {
2745 last_parameter->next = function_parameter;
2747 parameters = function_parameter;
2749 last_parameter = function_parameter;
2751 new_type->function.parameters = parameters;
2753 type = typehash_insert(new_type);
2754 if(type != new_type) {
2755 obstack_free(type_obst, new_type);
2758 declaration->type = type;
2761 static bool first_err = true;
2764 * When called with first_err set, prints the name of the current function,
2767 static void print_in_function(void) {
2770 diagnosticf("%s: In function '%Y':\n",
2771 current_function->source_position.input_name,
2772 current_function->symbol);
2777 * Check if all labels are defined in the current function.
2778 * Check if all labels are used in the current function.
2780 static void check_labels(void)
2782 for (const goto_statement_t *goto_statement = goto_first;
2783 goto_statement != NULL;
2784 goto_statement = goto_statement->next) {
2785 declaration_t *label = goto_statement->label;
2788 if (label->source_position.input_name == NULL) {
2789 print_in_function();
2790 errorf(goto_statement->base.source_position,
2791 "label '%Y' used but not defined", label->symbol);
2794 goto_first = goto_last = NULL;
2796 if (warning.unused_label) {
2797 for (const label_statement_t *label_statement = label_first;
2798 label_statement != NULL;
2799 label_statement = label_statement->next) {
2800 const declaration_t *label = label_statement->label;
2802 if (! label->used) {
2803 print_in_function();
2804 warningf(label_statement->base.source_position,
2805 "label '%Y' defined but not used", label->symbol);
2809 label_first = label_last = NULL;
2813 * Check declarations of current_function for unused entities.
2815 static void check_declarations(void)
2817 if (warning.unused_parameter) {
2818 const scope_t *scope = ¤t_function->scope;
2820 const declaration_t *parameter = scope->declarations;
2821 for (; parameter != NULL; parameter = parameter->next) {
2822 if (! parameter->used) {
2823 print_in_function();
2824 warningf(parameter->source_position,
2825 "unused parameter '%Y'", parameter->symbol);
2829 if (warning.unused_variable) {
2833 static void parse_external_declaration(void)
2835 /* function-definitions and declarations both start with declaration
2837 declaration_specifiers_t specifiers;
2838 memset(&specifiers, 0, sizeof(specifiers));
2839 parse_declaration_specifiers(&specifiers);
2841 /* must be a declaration */
2842 if(token.type == ';') {
2843 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2847 /* declarator is common to both function-definitions and declarations */
2848 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2850 /* must be a declaration */
2851 if(token.type == ',' || token.type == '=' || token.type == ';') {
2852 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
2856 /* must be a function definition */
2857 parse_kr_declaration_list(ndeclaration);
2859 if(token.type != '{') {
2860 parse_error_expected("while parsing function definition", '{', 0);
2865 type_t *type = ndeclaration->type;
2867 /* note that we don't skip typerefs: the standard doesn't allow them here
2868 * (so we can't use is_type_function here) */
2869 if(type->kind != TYPE_FUNCTION) {
2870 if (is_type_valid(type)) {
2871 errorf(HERE, "declarator '%#T' has a body but is not a function type",
2872 type, ndeclaration->symbol);
2878 /* § 6.7.5.3 (14) a function definition with () means no
2879 * parameters (and not unspecified parameters) */
2880 if(type->function.unspecified_parameters) {
2881 type_t *duplicate = duplicate_type(type);
2882 duplicate->function.unspecified_parameters = false;
2884 type = typehash_insert(duplicate);
2885 if(type != duplicate) {
2886 obstack_free(type_obst, duplicate);
2888 ndeclaration->type = type;
2891 declaration_t *const declaration = record_function_definition(ndeclaration);
2892 if(ndeclaration != declaration) {
2893 declaration->scope = ndeclaration->scope;
2895 type = skip_typeref(declaration->type);
2897 /* push function parameters and switch scope */
2898 int top = environment_top();
2899 scope_t *last_scope = scope;
2900 set_scope(&declaration->scope);
2902 declaration_t *parameter = declaration->scope.declarations;
2903 for( ; parameter != NULL; parameter = parameter->next) {
2904 if(parameter->parent_scope == &ndeclaration->scope) {
2905 parameter->parent_scope = scope;
2907 assert(parameter->parent_scope == NULL
2908 || parameter->parent_scope == scope);
2909 parameter->parent_scope = scope;
2910 environment_push(parameter);
2913 if(declaration->init.statement != NULL) {
2914 parser_error_multiple_definition(declaration, token.source_position);
2916 goto end_of_parse_external_declaration;
2918 /* parse function body */
2919 int label_stack_top = label_top();
2920 declaration_t *old_current_function = current_function;
2921 current_function = declaration;
2923 declaration->init.statement = parse_compound_statement();
2926 check_declarations();
2928 assert(current_function == declaration);
2929 current_function = old_current_function;
2930 label_pop_to(label_stack_top);
2933 end_of_parse_external_declaration:
2934 assert(scope == &declaration->scope);
2935 set_scope(last_scope);
2936 environment_pop_to(top);
2939 static type_t *make_bitfield_type(type_t *base, expression_t *size, source_position_t source_position)
2941 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
2942 type->bitfield.base = base;
2943 type->bitfield.size = size;
2948 static void parse_compound_declarators(declaration_t *struct_declaration,
2949 const declaration_specifiers_t *specifiers)
2951 declaration_t *last_declaration = struct_declaration->scope.declarations;
2952 if(last_declaration != NULL) {
2953 while(last_declaration->next != NULL) {
2954 last_declaration = last_declaration->next;
2959 declaration_t *declaration;
2961 if(token.type == ':') {
2962 source_position_t source_position = HERE;
2965 type_t *base_type = specifiers->type;
2966 expression_t *size = parse_constant_expression();
2968 if(!is_type_integer(skip_typeref(base_type))) {
2969 errorf(HERE, "bitfield base type '%T' is not an integer type",
2973 type_t *type = make_bitfield_type(base_type, size, source_position);
2975 declaration = allocate_declaration_zero();
2976 declaration->namespc = NAMESPACE_NORMAL;
2977 declaration->storage_class = STORAGE_CLASS_NONE;
2978 declaration->source_position = source_position;
2979 declaration->modifiers = specifiers->decl_modifiers;
2980 declaration->type = type;
2982 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
2984 type_t *orig_type = declaration->type;
2985 type_t *type = skip_typeref(orig_type);
2987 if(token.type == ':') {
2988 source_position_t source_position = HERE;
2990 expression_t *size = parse_constant_expression();
2992 if(!is_type_integer(type)) {
2993 errorf(HERE, "bitfield base type '%T' is not an "
2994 "integer type", orig_type);
2997 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
2998 declaration->type = bitfield_type;
3000 /* TODO we ignore arrays for now... what is missing is a check
3001 * that they're at the end of the struct */
3002 if(is_type_incomplete(type) && !is_type_array(type)) {
3004 "compound member '%Y' has incomplete type '%T'",
3005 declaration->symbol, orig_type);
3006 } else if(is_type_function(type)) {
3007 errorf(HERE, "compound member '%Y' must not have function "
3008 "type '%T'", declaration->symbol, orig_type);
3013 /* make sure we don't define a symbol multiple times */
3014 symbol_t *symbol = declaration->symbol;
3015 if(symbol != NULL) {
3016 declaration_t *iter = struct_declaration->scope.declarations;
3017 for( ; iter != NULL; iter = iter->next) {
3018 if(iter->symbol == symbol) {
3019 errorf(declaration->source_position,
3020 "multiple declarations of symbol '%Y'", symbol);
3021 errorf(iter->source_position,
3022 "previous declaration of '%Y' was here", symbol);
3028 /* append declaration */
3029 if(last_declaration != NULL) {
3030 last_declaration->next = declaration;
3032 struct_declaration->scope.declarations = declaration;
3034 last_declaration = declaration;
3036 if(token.type != ',')
3043 static void parse_compound_type_entries(declaration_t *compound_declaration)
3047 while(token.type != '}' && token.type != T_EOF) {
3048 declaration_specifiers_t specifiers;
3049 memset(&specifiers, 0, sizeof(specifiers));
3050 parse_declaration_specifiers(&specifiers);
3052 parse_compound_declarators(compound_declaration, &specifiers);
3054 if(token.type == T_EOF) {
3055 errorf(HERE, "EOF while parsing struct");
3060 static type_t *parse_typename(void)
3062 declaration_specifiers_t specifiers;
3063 memset(&specifiers, 0, sizeof(specifiers));
3064 parse_declaration_specifiers(&specifiers);
3065 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3066 /* TODO: improve error message, user does probably not know what a
3067 * storage class is...
3069 errorf(HERE, "typename may not have a storage class");
3072 type_t *result = parse_abstract_declarator(specifiers.type);
3080 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3081 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3082 expression_t *left);
3084 typedef struct expression_parser_function_t expression_parser_function_t;
3085 struct expression_parser_function_t {
3086 unsigned precedence;
3087 parse_expression_function parser;
3088 unsigned infix_precedence;
3089 parse_expression_infix_function infix_parser;
3092 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3095 * Creates a new invalid expression.
3097 static expression_t *create_invalid_expression(void)
3099 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3100 expression->base.source_position = token.source_position;
3105 * Prints an error message if an expression was expected but not read
3107 static expression_t *expected_expression_error(void)
3109 /* skip the error message if the error token was read */
3110 if (token.type != T_ERROR) {
3111 errorf(HERE, "expected expression, got token '%K'", &token);
3115 return create_invalid_expression();
3119 * Parse a string constant.
3121 static expression_t *parse_string_const(void)
3123 expression_t *cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3124 cnst->base.type = type_char_ptr;
3125 cnst->string.value = parse_string_literals();
3131 * Parse a wide string constant.
3133 static expression_t *parse_wide_string_const(void)
3135 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3136 cnst->base.type = type_wchar_t_ptr;
3137 cnst->wide_string.value = token.v.wide_string; /* TODO concatenate */
3143 * Parse an integer constant.
3145 static expression_t *parse_int_const(void)
3147 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3148 cnst->base.type = token.datatype;
3149 cnst->conste.v.int_value = token.v.intvalue;
3157 * Parse a float constant.
3159 static expression_t *parse_float_const(void)
3161 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3162 cnst->base.type = token.datatype;
3163 cnst->conste.v.float_value = token.v.floatvalue;
3170 static declaration_t *create_implicit_function(symbol_t *symbol,
3171 const source_position_t source_position)
3173 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3174 ntype->function.return_type = type_int;
3175 ntype->function.unspecified_parameters = true;
3177 type_t *type = typehash_insert(ntype);
3182 declaration_t *const declaration = allocate_declaration_zero();
3183 declaration->storage_class = STORAGE_CLASS_EXTERN;
3184 declaration->type = type;
3185 declaration->symbol = symbol;
3186 declaration->source_position = source_position;
3187 declaration->parent_scope = global_scope;
3189 scope_t *old_scope = scope;
3190 set_scope(global_scope);
3192 environment_push(declaration);
3193 /* prepends the declaration to the global declarations list */
3194 declaration->next = scope->declarations;
3195 scope->declarations = declaration;
3197 assert(scope == global_scope);
3198 set_scope(old_scope);
3204 * Creates a return_type (func)(argument_type) function type if not
3207 * @param return_type the return type
3208 * @param argument_type the argument type
3210 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3212 function_parameter_t *parameter
3213 = obstack_alloc(type_obst, sizeof(parameter[0]));
3214 memset(parameter, 0, sizeof(parameter[0]));
3215 parameter->type = argument_type;
3217 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3218 type->function.return_type = return_type;
3219 type->function.parameters = parameter;
3221 type_t *result = typehash_insert(type);
3222 if(result != type) {
3230 * Creates a function type for some function like builtins.
3232 * @param symbol the symbol describing the builtin
3234 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3236 switch(symbol->ID) {
3237 case T___builtin_alloca:
3238 return make_function_1_type(type_void_ptr, type_size_t);
3239 case T___builtin_nan:
3240 return make_function_1_type(type_double, type_char_ptr);
3241 case T___builtin_nanf:
3242 return make_function_1_type(type_float, type_char_ptr);
3243 case T___builtin_nand:
3244 return make_function_1_type(type_long_double, type_char_ptr);
3245 case T___builtin_va_end:
3246 return make_function_1_type(type_void, type_valist);
3248 panic("not implemented builtin symbol found");
3253 * Performs automatic type cast as described in § 6.3.2.1.
3255 * @param orig_type the original type
3257 static type_t *automatic_type_conversion(type_t *orig_type)
3259 type_t *type = skip_typeref(orig_type);
3260 if(is_type_array(type)) {
3261 array_type_t *array_type = &type->array;
3262 type_t *element_type = array_type->element_type;
3263 unsigned qualifiers = array_type->type.qualifiers;
3265 return make_pointer_type(element_type, qualifiers);
3268 if(is_type_function(type)) {
3269 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3276 * reverts the automatic casts of array to pointer types and function
3277 * to function-pointer types as defined § 6.3.2.1
3279 type_t *revert_automatic_type_conversion(const expression_t *expression)
3281 switch (expression->kind) {
3282 case EXPR_REFERENCE: return expression->reference.declaration->type;
3283 case EXPR_SELECT: return expression->select.compound_entry->type;
3285 case EXPR_UNARY_DEREFERENCE: {
3286 const expression_t *const value = expression->unary.value;
3287 type_t *const type = skip_typeref(value->base.type);
3288 assert(is_type_pointer(type));
3289 return type->pointer.points_to;
3292 case EXPR_BUILTIN_SYMBOL:
3293 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3295 case EXPR_ARRAY_ACCESS: {
3296 const expression_t *array_ref = expression->array_access.array_ref;
3297 type_t *type_left = skip_typeref(array_ref->base.type);
3298 if (!is_type_valid(type_left))
3300 assert(is_type_pointer(type_left));
3301 return type_left->pointer.points_to;
3307 return expression->base.type;
3310 static expression_t *parse_reference(void)
3312 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3314 reference_expression_t *ref = &expression->reference;
3315 ref->symbol = token.v.symbol;
3317 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3319 source_position_t source_position = token.source_position;
3322 if(declaration == NULL) {
3323 if (! strict_mode && token.type == '(') {
3324 /* an implicitly defined function */
3325 if (warning.implicit_function_declaration) {
3326 warningf(HERE, "implicit declaration of function '%Y'",
3330 declaration = create_implicit_function(ref->symbol,
3333 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3338 type_t *type = declaration->type;
3340 /* we always do the auto-type conversions; the & and sizeof parser contains
3341 * code to revert this! */
3342 type = automatic_type_conversion(type);
3344 ref->declaration = declaration;
3345 ref->base.type = type;
3347 /* this declaration is used */
3348 declaration->used = true;
3353 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3357 /* TODO check if explicit cast is allowed and issue warnings/errors */
3360 static expression_t *parse_cast(void)
3362 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3364 cast->base.source_position = token.source_position;
3366 type_t *type = parse_typename();
3369 expression_t *value = parse_sub_expression(20);
3371 check_cast_allowed(value, type);
3373 cast->base.type = type;
3374 cast->unary.value = value;
3379 static expression_t *parse_statement_expression(void)
3381 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3383 statement_t *statement = parse_compound_statement();
3384 expression->statement.statement = statement;
3385 expression->base.source_position = statement->base.source_position;
3387 /* find last statement and use its type */
3388 type_t *type = type_void;
3389 const statement_t *stmt = statement->compound.statements;
3391 while (stmt->base.next != NULL)
3392 stmt = stmt->base.next;
3394 if (stmt->kind == STATEMENT_EXPRESSION) {
3395 type = stmt->expression.expression->base.type;
3398 warningf(expression->base.source_position, "empty statement expression ({})");
3400 expression->base.type = type;
3407 static expression_t *parse_brace_expression(void)
3411 switch(token.type) {
3413 /* gcc extension: a statement expression */
3414 return parse_statement_expression();
3418 return parse_cast();
3420 if(is_typedef_symbol(token.v.symbol)) {
3421 return parse_cast();
3425 expression_t *result = parse_expression();
3431 static expression_t *parse_function_keyword(void)
3436 if (current_function == NULL) {
3437 errorf(HERE, "'__func__' used outside of a function");
3440 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3441 expression->base.type = type_char_ptr;
3446 static expression_t *parse_pretty_function_keyword(void)
3448 eat(T___PRETTY_FUNCTION__);
3451 if (current_function == NULL) {
3452 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3455 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3456 expression->base.type = type_char_ptr;
3461 static designator_t *parse_designator(void)
3463 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3465 if(token.type != T_IDENTIFIER) {
3466 parse_error_expected("while parsing member designator",
3471 result->symbol = token.v.symbol;
3474 designator_t *last_designator = result;
3476 if(token.type == '.') {
3478 if(token.type != T_IDENTIFIER) {
3479 parse_error_expected("while parsing member designator",
3484 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3485 designator->symbol = token.v.symbol;
3488 last_designator->next = designator;
3489 last_designator = designator;
3492 if(token.type == '[') {
3494 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3495 designator->array_access = parse_expression();
3496 if(designator->array_access == NULL) {
3502 last_designator->next = designator;
3503 last_designator = designator;
3512 static expression_t *parse_offsetof(void)
3514 eat(T___builtin_offsetof);
3516 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3517 expression->base.type = type_size_t;
3520 expression->offsetofe.type = parse_typename();
3522 expression->offsetofe.designator = parse_designator();
3528 static expression_t *parse_va_start(void)
3530 eat(T___builtin_va_start);
3532 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3535 expression->va_starte.ap = parse_assignment_expression();
3537 expression_t *const expr = parse_assignment_expression();
3538 if (expr->kind == EXPR_REFERENCE) {
3539 declaration_t *const decl = expr->reference.declaration;
3541 return create_invalid_expression();
3542 if (decl->parent_scope == ¤t_function->scope &&
3543 decl->next == NULL) {
3544 expression->va_starte.parameter = decl;
3549 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3551 return create_invalid_expression();
3554 static expression_t *parse_va_arg(void)
3556 eat(T___builtin_va_arg);
3558 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3561 expression->va_arge.ap = parse_assignment_expression();
3563 expression->base.type = parse_typename();
3569 static expression_t *parse_builtin_symbol(void)
3571 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3573 symbol_t *symbol = token.v.symbol;
3575 expression->builtin_symbol.symbol = symbol;
3578 type_t *type = get_builtin_symbol_type(symbol);
3579 type = automatic_type_conversion(type);
3581 expression->base.type = type;
3585 static expression_t *parse_builtin_constant(void)
3587 eat(T___builtin_constant_p);
3589 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3592 expression->builtin_constant.value = parse_assignment_expression();
3594 expression->base.type = type_int;
3599 static expression_t *parse_builtin_prefetch(void)
3601 eat(T___builtin_prefetch);
3603 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3606 expression->builtin_prefetch.adr = parse_assignment_expression();
3607 if (token.type == ',') {
3609 expression->builtin_prefetch.rw = parse_assignment_expression();
3611 if (token.type == ',') {
3613 expression->builtin_prefetch.locality = parse_assignment_expression();
3616 expression->base.type = type_void;
3621 static expression_t *parse_compare_builtin(void)
3623 expression_t *expression;
3625 switch(token.type) {
3626 case T___builtin_isgreater:
3627 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3629 case T___builtin_isgreaterequal:
3630 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3632 case T___builtin_isless:
3633 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
3635 case T___builtin_islessequal:
3636 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
3638 case T___builtin_islessgreater:
3639 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
3641 case T___builtin_isunordered:
3642 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
3645 panic("invalid compare builtin found");
3648 expression->base.source_position = HERE;
3652 expression->binary.left = parse_assignment_expression();
3654 expression->binary.right = parse_assignment_expression();
3657 type_t *const orig_type_left = expression->binary.left->base.type;
3658 type_t *const orig_type_right = expression->binary.right->base.type;
3660 type_t *const type_left = skip_typeref(orig_type_left);
3661 type_t *const type_right = skip_typeref(orig_type_right);
3662 if(!is_type_float(type_left) && !is_type_float(type_right)) {
3663 if (is_type_valid(type_left) && is_type_valid(type_right)) {
3664 type_error_incompatible("invalid operands in comparison",
3665 expression->base.source_position, orig_type_left, orig_type_right);
3668 semantic_comparison(&expression->binary);
3674 static expression_t *parse_builtin_expect(void)
3676 eat(T___builtin_expect);
3678 expression_t *expression
3679 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
3682 expression->binary.left = parse_assignment_expression();
3684 expression->binary.right = parse_constant_expression();
3687 expression->base.type = expression->binary.left->base.type;
3692 static expression_t *parse_assume(void) {
3695 expression_t *expression
3696 = allocate_expression_zero(EXPR_UNARY_ASSUME);
3699 expression->unary.value = parse_assignment_expression();
3702 expression->base.type = type_void;
3706 static expression_t *parse_primary_expression(void)
3708 switch(token.type) {
3710 return parse_int_const();
3711 case T_FLOATINGPOINT:
3712 return parse_float_const();
3713 case T_STRING_LITERAL:
3714 return parse_string_const();
3715 case T_WIDE_STRING_LITERAL:
3716 return parse_wide_string_const();
3718 return parse_reference();
3719 case T___FUNCTION__:
3721 return parse_function_keyword();
3722 case T___PRETTY_FUNCTION__:
3723 return parse_pretty_function_keyword();
3724 case T___builtin_offsetof:
3725 return parse_offsetof();
3726 case T___builtin_va_start:
3727 return parse_va_start();
3728 case T___builtin_va_arg:
3729 return parse_va_arg();
3730 case T___builtin_expect:
3731 return parse_builtin_expect();
3732 case T___builtin_alloca:
3733 case T___builtin_nan:
3734 case T___builtin_nand:
3735 case T___builtin_nanf:
3736 case T___builtin_va_end:
3737 return parse_builtin_symbol();
3738 case T___builtin_isgreater:
3739 case T___builtin_isgreaterequal:
3740 case T___builtin_isless:
3741 case T___builtin_islessequal:
3742 case T___builtin_islessgreater:
3743 case T___builtin_isunordered:
3744 return parse_compare_builtin();
3745 case T___builtin_constant_p:
3746 return parse_builtin_constant();
3747 case T___builtin_prefetch:
3748 return parse_builtin_prefetch();
3750 return parse_assume();
3753 return parse_brace_expression();
3756 errorf(HERE, "unexpected token %K", &token);
3759 return create_invalid_expression();
3763 * Check if the expression has the character type and issue a warning then.
3765 static void check_for_char_index_type(const expression_t *expression) {
3766 type_t *const type = expression->base.type;
3767 const type_t *const base_type = skip_typeref(type);
3769 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
3770 warning.char_subscripts) {
3771 warningf(expression->base.source_position,
3772 "array subscript has type '%T'", type);
3776 static expression_t *parse_array_expression(unsigned precedence,
3783 expression_t *inside = parse_expression();
3785 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
3787 array_access_expression_t *array_access = &expression->array_access;
3789 type_t *const orig_type_left = left->base.type;
3790 type_t *const orig_type_inside = inside->base.type;
3792 type_t *const type_left = skip_typeref(orig_type_left);
3793 type_t *const type_inside = skip_typeref(orig_type_inside);
3795 type_t *return_type;
3796 if (is_type_pointer(type_left)) {
3797 return_type = type_left->pointer.points_to;
3798 array_access->array_ref = left;
3799 array_access->index = inside;
3800 check_for_char_index_type(inside);
3801 } else if (is_type_pointer(type_inside)) {
3802 return_type = type_inside->pointer.points_to;
3803 array_access->array_ref = inside;
3804 array_access->index = left;
3805 array_access->flipped = true;
3806 check_for_char_index_type(left);
3808 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
3810 "array access on object with non-pointer types '%T', '%T'",
3811 orig_type_left, orig_type_inside);
3813 return_type = type_error_type;
3814 array_access->array_ref = create_invalid_expression();
3817 if(token.type != ']') {
3818 parse_error_expected("Problem while parsing array access", ']', 0);
3823 return_type = automatic_type_conversion(return_type);
3824 expression->base.type = return_type;
3829 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
3831 expression_t *tp_expression = allocate_expression_zero(kind);
3832 tp_expression->base.type = type_size_t;
3834 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
3836 tp_expression->typeprop.type = parse_typename();
3839 expression_t *expression = parse_sub_expression(precedence);
3840 expression->base.type = revert_automatic_type_conversion(expression);
3842 tp_expression->typeprop.type = expression->base.type;
3843 tp_expression->typeprop.tp_expression = expression;
3846 return tp_expression;
3849 static expression_t *parse_sizeof(unsigned precedence)
3852 return parse_typeprop(EXPR_SIZEOF, precedence);
3855 static expression_t *parse_alignof(unsigned precedence)
3858 return parse_typeprop(EXPR_SIZEOF, precedence);
3861 static expression_t *parse_select_expression(unsigned precedence,
3862 expression_t *compound)
3865 assert(token.type == '.' || token.type == T_MINUSGREATER);
3867 bool is_pointer = (token.type == T_MINUSGREATER);
3870 expression_t *select = allocate_expression_zero(EXPR_SELECT);
3871 select->select.compound = compound;
3873 if(token.type != T_IDENTIFIER) {
3874 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
3877 symbol_t *symbol = token.v.symbol;
3878 select->select.symbol = symbol;
3881 type_t *const orig_type = compound->base.type;
3882 type_t *const type = skip_typeref(orig_type);
3884 type_t *type_left = type;
3886 if (!is_type_pointer(type)) {
3887 if (is_type_valid(type)) {
3888 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
3890 return create_invalid_expression();
3892 type_left = type->pointer.points_to;
3894 type_left = skip_typeref(type_left);
3896 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
3897 type_left->kind != TYPE_COMPOUND_UNION) {
3898 if (is_type_valid(type_left)) {
3899 errorf(HERE, "request for member '%Y' in something not a struct or "
3900 "union, but '%T'", symbol, type_left);
3902 return create_invalid_expression();
3905 declaration_t *const declaration = type_left->compound.declaration;
3907 if(!declaration->init.is_defined) {
3908 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
3910 return create_invalid_expression();
3913 declaration_t *iter = declaration->scope.declarations;
3914 for( ; iter != NULL; iter = iter->next) {
3915 if(iter->symbol == symbol) {
3920 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
3921 return create_invalid_expression();
3924 /* we always do the auto-type conversions; the & and sizeof parser contains
3925 * code to revert this! */
3926 type_t *expression_type = automatic_type_conversion(iter->type);
3928 select->select.compound_entry = iter;
3929 select->base.type = expression_type;
3931 if(expression_type->kind == TYPE_BITFIELD) {
3932 expression_t *extract
3933 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
3934 extract->unary.value = select;
3935 extract->base.type = expression_type->bitfield.base;
3944 * Parse a call expression, ie. expression '( ... )'.
3946 * @param expression the function address
3948 static expression_t *parse_call_expression(unsigned precedence,
3949 expression_t *expression)
3952 expression_t *result = allocate_expression_zero(EXPR_CALL);
3954 call_expression_t *call = &result->call;
3955 call->function = expression;
3957 type_t *const orig_type = expression->base.type;
3958 type_t *const type = skip_typeref(orig_type);
3960 function_type_t *function_type = NULL;
3961 if (is_type_pointer(type)) {
3962 type_t *const to_type = skip_typeref(type->pointer.points_to);
3964 if (is_type_function(to_type)) {
3965 function_type = &to_type->function;
3966 call->base.type = function_type->return_type;
3970 if (function_type == NULL && is_type_valid(type)) {
3971 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
3974 /* parse arguments */
3977 if(token.type != ')') {
3978 call_argument_t *last_argument = NULL;
3981 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
3983 argument->expression = parse_assignment_expression();
3984 if(last_argument == NULL) {
3985 call->arguments = argument;
3987 last_argument->next = argument;
3989 last_argument = argument;
3991 if(token.type != ',')
3998 if(function_type != NULL) {
3999 function_parameter_t *parameter = function_type->parameters;
4000 call_argument_t *argument = call->arguments;
4001 for( ; parameter != NULL && argument != NULL;
4002 parameter = parameter->next, argument = argument->next) {
4003 type_t *expected_type = parameter->type;
4004 /* TODO report scope in error messages */
4005 expression_t *const arg_expr = argument->expression;
4006 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4007 if (res_type == NULL) {
4008 /* TODO improve error message */
4009 errorf(arg_expr->base.source_position,
4010 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4011 arg_expr, arg_expr->base.type, expected_type);
4013 argument->expression = create_implicit_cast(argument->expression, expected_type);
4016 /* too few parameters */
4017 if(parameter != NULL) {
4018 errorf(HERE, "too few arguments to function '%E'", expression);
4019 } else if(argument != NULL) {
4020 /* too many parameters */
4021 if(!function_type->variadic
4022 && !function_type->unspecified_parameters) {
4023 errorf(HERE, "too many arguments to function '%E'", expression);
4025 /* do default promotion */
4026 for( ; argument != NULL; argument = argument->next) {
4027 type_t *type = argument->expression->base.type;
4029 type = skip_typeref(type);
4030 if(is_type_integer(type)) {
4031 type = promote_integer(type);
4032 } else if(type == type_float) {
4036 argument->expression
4037 = create_implicit_cast(argument->expression, type);
4040 check_format(&result->call);
4043 check_format(&result->call);
4050 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4052 static bool same_compound_type(const type_t *type1, const type_t *type2)
4055 is_type_compound(type1) &&
4056 type1->kind == type2->kind &&
4057 type1->compound.declaration == type2->compound.declaration;
4061 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4063 * @param expression the conditional expression
4065 static expression_t *parse_conditional_expression(unsigned precedence,
4066 expression_t *expression)
4070 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4072 conditional_expression_t *conditional = &result->conditional;
4073 conditional->condition = expression;
4076 type_t *const condition_type_orig = expression->base.type;
4077 type_t *const condition_type = skip_typeref(condition_type_orig);
4078 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4079 type_error("expected a scalar type in conditional condition",
4080 expression->base.source_position, condition_type_orig);
4083 expression_t *true_expression = parse_expression();
4085 expression_t *false_expression = parse_sub_expression(precedence);
4087 conditional->true_expression = true_expression;
4088 conditional->false_expression = false_expression;
4090 type_t *const orig_true_type = true_expression->base.type;
4091 type_t *const orig_false_type = false_expression->base.type;
4092 type_t *const true_type = skip_typeref(orig_true_type);
4093 type_t *const false_type = skip_typeref(orig_false_type);
4096 type_t *result_type;
4097 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4098 result_type = semantic_arithmetic(true_type, false_type);
4100 true_expression = create_implicit_cast(true_expression, result_type);
4101 false_expression = create_implicit_cast(false_expression, result_type);
4103 conditional->true_expression = true_expression;
4104 conditional->false_expression = false_expression;
4105 conditional->base.type = result_type;
4106 } else if (same_compound_type(true_type, false_type) || (
4107 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4108 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4110 /* just take 1 of the 2 types */
4111 result_type = true_type;
4112 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4113 && pointers_compatible(true_type, false_type)) {
4115 result_type = true_type;
4118 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4119 type_error_incompatible("while parsing conditional",
4120 expression->base.source_position, true_type,
4123 result_type = type_error_type;
4126 conditional->base.type = result_type;
4131 * Parse an extension expression.
4133 static expression_t *parse_extension(unsigned precedence)
4135 eat(T___extension__);
4137 /* TODO enable extensions */
4138 expression_t *expression = parse_sub_expression(precedence);
4139 /* TODO disable extensions */
4143 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4145 eat(T___builtin_classify_type);
4147 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4148 result->base.type = type_int;
4151 expression_t *expression = parse_sub_expression(precedence);
4153 result->classify_type.type_expression = expression;
4158 static void semantic_incdec(unary_expression_t *expression)
4160 type_t *const orig_type = expression->value->base.type;
4161 type_t *const type = skip_typeref(orig_type);
4162 /* TODO !is_type_real && !is_type_pointer */
4163 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4164 if (is_type_valid(type)) {
4165 /* TODO: improve error message */
4166 errorf(HERE, "operation needs an arithmetic or pointer type");
4171 expression->base.type = orig_type;
4174 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4176 type_t *const orig_type = expression->value->base.type;
4177 type_t *const type = skip_typeref(orig_type);
4178 if(!is_type_arithmetic(type)) {
4179 if (is_type_valid(type)) {
4180 /* TODO: improve error message */
4181 errorf(HERE, "operation needs an arithmetic type");
4186 expression->base.type = orig_type;
4189 static void semantic_unexpr_scalar(unary_expression_t *expression)
4191 type_t *const orig_type = expression->value->base.type;
4192 type_t *const type = skip_typeref(orig_type);
4193 if (!is_type_scalar(type)) {
4194 if (is_type_valid(type)) {
4195 errorf(HERE, "operand of ! must be of scalar type");
4200 expression->base.type = orig_type;
4203 static void semantic_unexpr_integer(unary_expression_t *expression)
4205 type_t *const orig_type = expression->value->base.type;
4206 type_t *const type = skip_typeref(orig_type);
4207 if (!is_type_integer(type)) {
4208 if (is_type_valid(type)) {
4209 errorf(HERE, "operand of ~ must be of integer type");
4214 expression->base.type = orig_type;
4217 static void semantic_dereference(unary_expression_t *expression)
4219 type_t *const orig_type = expression->value->base.type;
4220 type_t *const type = skip_typeref(orig_type);
4221 if(!is_type_pointer(type)) {
4222 if (is_type_valid(type)) {
4223 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4228 type_t *result_type = type->pointer.points_to;
4229 result_type = automatic_type_conversion(result_type);
4230 expression->base.type = result_type;
4234 * Check the semantic of the address taken expression.
4236 static void semantic_take_addr(unary_expression_t *expression)
4238 expression_t *value = expression->value;
4239 value->base.type = revert_automatic_type_conversion(value);
4241 type_t *orig_type = value->base.type;
4242 if(!is_type_valid(orig_type))
4245 if(value->kind == EXPR_REFERENCE) {
4246 declaration_t *const declaration = value->reference.declaration;
4247 if(declaration != NULL) {
4248 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4249 errorf(expression->base.source_position,
4250 "address of register variable '%Y' requested",
4251 declaration->symbol);
4253 declaration->address_taken = 1;
4257 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4260 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4261 static expression_t *parse_##unexpression_type(unsigned precedence) \
4265 expression_t *unary_expression \
4266 = allocate_expression_zero(unexpression_type); \
4267 unary_expression->base.source_position = HERE; \
4268 unary_expression->unary.value = parse_sub_expression(precedence); \
4270 sfunc(&unary_expression->unary); \
4272 return unary_expression; \
4275 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4276 semantic_unexpr_arithmetic)
4277 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4278 semantic_unexpr_arithmetic)
4279 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4280 semantic_unexpr_scalar)
4281 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4282 semantic_dereference)
4283 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4285 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4286 semantic_unexpr_integer)
4287 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4289 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4292 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4294 static expression_t *parse_##unexpression_type(unsigned precedence, \
4295 expression_t *left) \
4297 (void) precedence; \
4300 expression_t *unary_expression \
4301 = allocate_expression_zero(unexpression_type); \
4302 unary_expression->unary.value = left; \
4304 sfunc(&unary_expression->unary); \
4306 return unary_expression; \
4309 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4310 EXPR_UNARY_POSTFIX_INCREMENT,
4312 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4313 EXPR_UNARY_POSTFIX_DECREMENT,
4316 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4318 /* TODO: handle complex + imaginary types */
4320 /* § 6.3.1.8 Usual arithmetic conversions */
4321 if(type_left == type_long_double || type_right == type_long_double) {
4322 return type_long_double;
4323 } else if(type_left == type_double || type_right == type_double) {
4325 } else if(type_left == type_float || type_right == type_float) {
4329 type_right = promote_integer(type_right);
4330 type_left = promote_integer(type_left);
4332 if(type_left == type_right)
4335 bool signed_left = is_type_signed(type_left);
4336 bool signed_right = is_type_signed(type_right);
4337 int rank_left = get_rank(type_left);
4338 int rank_right = get_rank(type_right);
4339 if(rank_left < rank_right) {
4340 if(signed_left == signed_right || !signed_right) {
4346 if(signed_left == signed_right || !signed_left) {
4355 * Check the semantic restrictions for a binary expression.
4357 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4359 expression_t *const left = expression->left;
4360 expression_t *const right = expression->right;
4361 type_t *const orig_type_left = left->base.type;
4362 type_t *const orig_type_right = right->base.type;
4363 type_t *const type_left = skip_typeref(orig_type_left);
4364 type_t *const type_right = skip_typeref(orig_type_right);
4366 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4367 /* TODO: improve error message */
4368 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4369 errorf(HERE, "operation needs arithmetic types");
4374 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4375 expression->left = create_implicit_cast(left, arithmetic_type);
4376 expression->right = create_implicit_cast(right, arithmetic_type);
4377 expression->base.type = arithmetic_type;
4380 static void semantic_shift_op(binary_expression_t *expression)
4382 expression_t *const left = expression->left;
4383 expression_t *const right = expression->right;
4384 type_t *const orig_type_left = left->base.type;
4385 type_t *const orig_type_right = right->base.type;
4386 type_t * type_left = skip_typeref(orig_type_left);
4387 type_t * type_right = skip_typeref(orig_type_right);
4389 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4390 /* TODO: improve error message */
4391 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4392 errorf(HERE, "operation needs integer types");
4397 type_left = promote_integer(type_left);
4398 type_right = promote_integer(type_right);
4400 expression->left = create_implicit_cast(left, type_left);
4401 expression->right = create_implicit_cast(right, type_right);
4402 expression->base.type = type_left;
4405 static void semantic_add(binary_expression_t *expression)
4407 expression_t *const left = expression->left;
4408 expression_t *const right = expression->right;
4409 type_t *const orig_type_left = left->base.type;
4410 type_t *const orig_type_right = right->base.type;
4411 type_t *const type_left = skip_typeref(orig_type_left);
4412 type_t *const type_right = skip_typeref(orig_type_right);
4415 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4416 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4417 expression->left = create_implicit_cast(left, arithmetic_type);
4418 expression->right = create_implicit_cast(right, arithmetic_type);
4419 expression->base.type = arithmetic_type;
4421 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4422 expression->base.type = type_left;
4423 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4424 expression->base.type = type_right;
4425 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4426 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4430 static void semantic_sub(binary_expression_t *expression)
4432 expression_t *const left = expression->left;
4433 expression_t *const right = expression->right;
4434 type_t *const orig_type_left = left->base.type;
4435 type_t *const orig_type_right = right->base.type;
4436 type_t *const type_left = skip_typeref(orig_type_left);
4437 type_t *const type_right = skip_typeref(orig_type_right);
4440 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4441 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4442 expression->left = create_implicit_cast(left, arithmetic_type);
4443 expression->right = create_implicit_cast(right, arithmetic_type);
4444 expression->base.type = arithmetic_type;
4446 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4447 expression->base.type = type_left;
4448 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4449 if(!pointers_compatible(type_left, type_right)) {
4451 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4452 orig_type_left, orig_type_right);
4454 expression->base.type = type_ptrdiff_t;
4456 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4457 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4458 orig_type_left, orig_type_right);
4463 * Check the semantics of comparison expressions.
4465 * @param expression The expression to check.
4467 static void semantic_comparison(binary_expression_t *expression)
4469 expression_t *left = expression->left;
4470 expression_t *right = expression->right;
4471 type_t *orig_type_left = left->base.type;
4472 type_t *orig_type_right = right->base.type;
4474 type_t *type_left = skip_typeref(orig_type_left);
4475 type_t *type_right = skip_typeref(orig_type_right);
4477 /* TODO non-arithmetic types */
4478 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4479 if (warning.sign_compare &&
4480 (expression->base.kind != EXPR_BINARY_EQUAL &&
4481 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4482 (is_type_signed(type_left) != is_type_signed(type_right))) {
4483 warningf(expression->base.source_position,
4484 "comparison between signed and unsigned");
4486 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4487 expression->left = create_implicit_cast(left, arithmetic_type);
4488 expression->right = create_implicit_cast(right, arithmetic_type);
4489 expression->base.type = arithmetic_type;
4490 if (warning.float_equal &&
4491 (expression->base.kind == EXPR_BINARY_EQUAL ||
4492 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4493 is_type_float(arithmetic_type)) {
4494 warningf(expression->base.source_position,
4495 "comparing floating point with == or != is unsafe");
4497 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4498 /* TODO check compatibility */
4499 } else if (is_type_pointer(type_left)) {
4500 expression->right = create_implicit_cast(right, type_left);
4501 } else if (is_type_pointer(type_right)) {
4502 expression->left = create_implicit_cast(left, type_right);
4503 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4504 type_error_incompatible("invalid operands in comparison",
4505 expression->base.source_position,
4506 type_left, type_right);
4508 expression->base.type = type_int;
4511 static void semantic_arithmetic_assign(binary_expression_t *expression)
4513 expression_t *left = expression->left;
4514 expression_t *right = expression->right;
4515 type_t *orig_type_left = left->base.type;
4516 type_t *orig_type_right = right->base.type;
4518 type_t *type_left = skip_typeref(orig_type_left);
4519 type_t *type_right = skip_typeref(orig_type_right);
4521 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4522 /* TODO: improve error message */
4523 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4524 errorf(HERE, "operation needs arithmetic types");
4529 /* combined instructions are tricky. We can't create an implicit cast on
4530 * the left side, because we need the uncasted form for the store.
4531 * The ast2firm pass has to know that left_type must be right_type
4532 * for the arithmetic operation and create a cast by itself */
4533 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4534 expression->right = create_implicit_cast(right, arithmetic_type);
4535 expression->base.type = type_left;
4538 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4540 expression_t *const left = expression->left;
4541 expression_t *const right = expression->right;
4542 type_t *const orig_type_left = left->base.type;
4543 type_t *const orig_type_right = right->base.type;
4544 type_t *const type_left = skip_typeref(orig_type_left);
4545 type_t *const type_right = skip_typeref(orig_type_right);
4547 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4548 /* combined instructions are tricky. We can't create an implicit cast on
4549 * the left side, because we need the uncasted form for the store.
4550 * The ast2firm pass has to know that left_type must be right_type
4551 * for the arithmetic operation and create a cast by itself */
4552 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4553 expression->right = create_implicit_cast(right, arithmetic_type);
4554 expression->base.type = type_left;
4555 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4556 expression->base.type = type_left;
4557 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4558 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4563 * Check the semantic restrictions of a logical expression.
4565 static void semantic_logical_op(binary_expression_t *expression)
4567 expression_t *const left = expression->left;
4568 expression_t *const right = expression->right;
4569 type_t *const orig_type_left = left->base.type;
4570 type_t *const orig_type_right = right->base.type;
4571 type_t *const type_left = skip_typeref(orig_type_left);
4572 type_t *const type_right = skip_typeref(orig_type_right);
4574 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4575 /* TODO: improve error message */
4576 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4577 errorf(HERE, "operation needs scalar types");
4582 expression->base.type = type_int;
4586 * Checks if a compound type has constant fields.
4588 static bool has_const_fields(const compound_type_t *type)
4590 const scope_t *scope = &type->declaration->scope;
4591 const declaration_t *declaration = scope->declarations;
4593 for (; declaration != NULL; declaration = declaration->next) {
4594 if (declaration->namespc != NAMESPACE_NORMAL)
4597 const type_t *decl_type = skip_typeref(declaration->type);
4598 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4606 * Check the semantic restrictions of a binary assign expression.
4608 static void semantic_binexpr_assign(binary_expression_t *expression)
4610 expression_t *left = expression->left;
4611 type_t *orig_type_left = left->base.type;
4613 type_t *type_left = revert_automatic_type_conversion(left);
4614 type_left = skip_typeref(orig_type_left);
4616 /* must be a modifiable lvalue */
4617 if (is_type_array(type_left)) {
4618 errorf(HERE, "cannot assign to arrays ('%E')", left);
4621 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4622 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4626 if(is_type_incomplete(type_left)) {
4628 "left-hand side of assignment '%E' has incomplete type '%T'",
4629 left, orig_type_left);
4632 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4633 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4634 left, orig_type_left);
4638 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4640 if (res_type == NULL) {
4641 errorf(expression->base.source_position,
4642 "cannot assign to '%T' from '%T'",
4643 orig_type_left, expression->right->base.type);
4645 expression->right = create_implicit_cast(expression->right, res_type);
4648 expression->base.type = orig_type_left;
4651 static bool expression_has_effect(const expression_t *const expr)
4653 switch (expr->kind) {
4654 case EXPR_UNKNOWN: break;
4655 case EXPR_INVALID: break;
4656 case EXPR_REFERENCE: return false;
4657 case EXPR_CONST: return false;
4658 case EXPR_STRING_LITERAL: return false;
4659 case EXPR_WIDE_STRING_LITERAL: return false;
4661 const call_expression_t *const call = &expr->call;
4662 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
4665 switch (call->function->builtin_symbol.symbol->ID) {
4666 case T___builtin_va_end: return true;
4667 default: return false;
4670 case EXPR_CONDITIONAL: {
4671 const conditional_expression_t *const cond = &expr->conditional;
4673 expression_has_effect(cond->true_expression) &&
4674 expression_has_effect(cond->false_expression);
4676 case EXPR_SELECT: return false;
4677 case EXPR_ARRAY_ACCESS: return false;
4678 case EXPR_SIZEOF: return false;
4679 case EXPR_CLASSIFY_TYPE: return false;
4680 case EXPR_ALIGNOF: return false;
4682 case EXPR_FUNCTION: return false;
4683 case EXPR_PRETTY_FUNCTION: return false;
4684 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
4685 case EXPR_BUILTIN_CONSTANT_P: return false;
4686 case EXPR_BUILTIN_PREFETCH: return true;
4687 case EXPR_OFFSETOF: return false;
4688 case EXPR_VA_START: return true;
4689 case EXPR_VA_ARG: return true;
4690 case EXPR_STATEMENT: return true; // TODO
4692 case EXPR_UNARY_NEGATE: return false;
4693 case EXPR_UNARY_PLUS: return false;
4694 case EXPR_UNARY_BITWISE_NEGATE: return false;
4695 case EXPR_UNARY_NOT: return false;
4696 case EXPR_UNARY_DEREFERENCE: return false;
4697 case EXPR_UNARY_TAKE_ADDRESS: return false;
4698 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
4699 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
4700 case EXPR_UNARY_PREFIX_INCREMENT: return true;
4701 case EXPR_UNARY_PREFIX_DECREMENT: return true;
4702 case EXPR_UNARY_CAST:
4703 return is_type_atomic(expr->base.type, ATOMIC_TYPE_VOID);
4704 case EXPR_UNARY_CAST_IMPLICIT: return true;
4705 case EXPR_UNARY_ASSUME: return true;
4706 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
4708 case EXPR_BINARY_ADD: return false;
4709 case EXPR_BINARY_SUB: return false;
4710 case EXPR_BINARY_MUL: return false;
4711 case EXPR_BINARY_DIV: return false;
4712 case EXPR_BINARY_MOD: return false;
4713 case EXPR_BINARY_EQUAL: return false;
4714 case EXPR_BINARY_NOTEQUAL: return false;
4715 case EXPR_BINARY_LESS: return false;
4716 case EXPR_BINARY_LESSEQUAL: return false;
4717 case EXPR_BINARY_GREATER: return false;
4718 case EXPR_BINARY_GREATEREQUAL: return false;
4719 case EXPR_BINARY_BITWISE_AND: return false;
4720 case EXPR_BINARY_BITWISE_OR: return false;
4721 case EXPR_BINARY_BITWISE_XOR: return false;
4722 case EXPR_BINARY_SHIFTLEFT: return false;
4723 case EXPR_BINARY_SHIFTRIGHT: return false;
4724 case EXPR_BINARY_ASSIGN: return true;
4725 case EXPR_BINARY_MUL_ASSIGN: return true;
4726 case EXPR_BINARY_DIV_ASSIGN: return true;
4727 case EXPR_BINARY_MOD_ASSIGN: return true;
4728 case EXPR_BINARY_ADD_ASSIGN: return true;
4729 case EXPR_BINARY_SUB_ASSIGN: return true;
4730 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
4731 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
4732 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
4733 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
4734 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
4735 case EXPR_BINARY_LOGICAL_AND:
4736 case EXPR_BINARY_LOGICAL_OR:
4737 case EXPR_BINARY_COMMA:
4738 return expression_has_effect(expr->binary.right);
4740 case EXPR_BINARY_BUILTIN_EXPECT: return true;
4741 case EXPR_BINARY_ISGREATER: return false;
4742 case EXPR_BINARY_ISGREATEREQUAL: return false;
4743 case EXPR_BINARY_ISLESS: return false;
4744 case EXPR_BINARY_ISLESSEQUAL: return false;
4745 case EXPR_BINARY_ISLESSGREATER: return false;
4746 case EXPR_BINARY_ISUNORDERED: return false;
4749 panic("unexpected statement");
4752 static void semantic_comma(binary_expression_t *expression)
4754 if (warning.unused_value) {
4755 const expression_t *const left = expression->left;
4756 if (!expression_has_effect(left)) {
4757 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
4760 expression->base.type = expression->right->base.type;
4763 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
4764 static expression_t *parse_##binexpression_type(unsigned precedence, \
4765 expression_t *left) \
4768 source_position_t pos = HERE; \
4770 expression_t *right = parse_sub_expression(precedence + lr); \
4772 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
4773 binexpr->base.source_position = pos; \
4774 binexpr->binary.left = left; \
4775 binexpr->binary.right = right; \
4776 sfunc(&binexpr->binary); \
4781 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
4782 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
4783 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
4784 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
4785 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
4786 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
4787 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
4788 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
4789 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
4791 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
4792 semantic_comparison, 1)
4793 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
4794 semantic_comparison, 1)
4795 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
4796 semantic_comparison, 1)
4797 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
4798 semantic_comparison, 1)
4800 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
4801 semantic_binexpr_arithmetic, 1)
4802 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
4803 semantic_binexpr_arithmetic, 1)
4804 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
4805 semantic_binexpr_arithmetic, 1)
4806 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
4807 semantic_logical_op, 1)
4808 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
4809 semantic_logical_op, 1)
4810 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
4811 semantic_shift_op, 1)
4812 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
4813 semantic_shift_op, 1)
4814 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
4815 semantic_arithmetic_addsubb_assign, 0)
4816 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
4817 semantic_arithmetic_addsubb_assign, 0)
4818 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
4819 semantic_arithmetic_assign, 0)
4820 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
4821 semantic_arithmetic_assign, 0)
4822 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
4823 semantic_arithmetic_assign, 0)
4824 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
4825 semantic_arithmetic_assign, 0)
4826 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4827 semantic_arithmetic_assign, 0)
4828 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
4829 semantic_arithmetic_assign, 0)
4830 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
4831 semantic_arithmetic_assign, 0)
4832 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
4833 semantic_arithmetic_assign, 0)
4835 static expression_t *parse_sub_expression(unsigned precedence)
4837 if(token.type < 0) {
4838 return expected_expression_error();
4841 expression_parser_function_t *parser
4842 = &expression_parsers[token.type];
4843 source_position_t source_position = token.source_position;
4846 if(parser->parser != NULL) {
4847 left = parser->parser(parser->precedence);
4849 left = parse_primary_expression();
4851 assert(left != NULL);
4852 left->base.source_position = source_position;
4855 if(token.type < 0) {
4856 return expected_expression_error();
4859 parser = &expression_parsers[token.type];
4860 if(parser->infix_parser == NULL)
4862 if(parser->infix_precedence < precedence)
4865 left = parser->infix_parser(parser->infix_precedence, left);
4867 assert(left != NULL);
4868 assert(left->kind != EXPR_UNKNOWN);
4869 left->base.source_position = source_position;
4876 * Parse an expression.
4878 static expression_t *parse_expression(void)
4880 return parse_sub_expression(1);
4884 * Register a parser for a prefix-like operator with given precedence.
4886 * @param parser the parser function
4887 * @param token_type the token type of the prefix token
4888 * @param precedence the precedence of the operator
4890 static void register_expression_parser(parse_expression_function parser,
4891 int token_type, unsigned precedence)
4893 expression_parser_function_t *entry = &expression_parsers[token_type];
4895 if(entry->parser != NULL) {
4896 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4897 panic("trying to register multiple expression parsers for a token");
4899 entry->parser = parser;
4900 entry->precedence = precedence;
4904 * Register a parser for an infix operator with given precedence.
4906 * @param parser the parser function
4907 * @param token_type the token type of the infix operator
4908 * @param precedence the precedence of the operator
4910 static void register_infix_parser(parse_expression_infix_function parser,
4911 int token_type, unsigned precedence)
4913 expression_parser_function_t *entry = &expression_parsers[token_type];
4915 if(entry->infix_parser != NULL) {
4916 diagnosticf("for token '%k'\n", (token_type_t)token_type);
4917 panic("trying to register multiple infix expression parsers for a "
4920 entry->infix_parser = parser;
4921 entry->infix_precedence = precedence;
4925 * Initialize the expression parsers.
4927 static void init_expression_parsers(void)
4929 memset(&expression_parsers, 0, sizeof(expression_parsers));
4931 register_infix_parser(parse_array_expression, '[', 30);
4932 register_infix_parser(parse_call_expression, '(', 30);
4933 register_infix_parser(parse_select_expression, '.', 30);
4934 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
4935 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
4937 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
4940 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
4941 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
4942 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
4943 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
4944 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
4945 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
4946 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
4947 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
4948 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
4949 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
4950 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
4951 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
4952 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
4953 T_EXCLAMATIONMARKEQUAL, 13);
4954 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
4955 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
4956 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
4957 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
4958 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
4959 register_infix_parser(parse_conditional_expression, '?', 7);
4960 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
4961 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
4962 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
4963 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
4964 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
4965 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
4966 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
4967 T_LESSLESSEQUAL, 2);
4968 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
4969 T_GREATERGREATEREQUAL, 2);
4970 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
4972 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
4974 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
4977 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
4979 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
4980 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
4981 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
4982 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
4983 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
4984 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
4985 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
4987 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
4989 register_expression_parser(parse_sizeof, T_sizeof, 25);
4990 register_expression_parser(parse_alignof, T___alignof__, 25);
4991 register_expression_parser(parse_extension, T___extension__, 25);
4992 register_expression_parser(parse_builtin_classify_type,
4993 T___builtin_classify_type, 25);
4997 * Parse a asm statement constraints specification.
4999 static asm_constraint_t *parse_asm_constraints(void)
5001 asm_constraint_t *result = NULL;
5002 asm_constraint_t *last = NULL;
5004 while(token.type == T_STRING_LITERAL || token.type == '[') {
5005 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5006 memset(constraint, 0, sizeof(constraint[0]));
5008 if(token.type == '[') {
5010 if(token.type != T_IDENTIFIER) {
5011 parse_error_expected("while parsing asm constraint",
5015 constraint->symbol = token.v.symbol;
5020 constraint->constraints = parse_string_literals();
5022 constraint->expression = parse_expression();
5026 last->next = constraint;
5028 result = constraint;
5032 if(token.type != ',')
5041 * Parse a asm statement clobber specification.
5043 static asm_clobber_t *parse_asm_clobbers(void)
5045 asm_clobber_t *result = NULL;
5046 asm_clobber_t *last = NULL;
5048 while(token.type == T_STRING_LITERAL) {
5049 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5050 clobber->clobber = parse_string_literals();
5053 last->next = clobber;
5059 if(token.type != ',')
5068 * Parse an asm statement.
5070 static statement_t *parse_asm_statement(void)
5074 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5075 statement->base.source_position = token.source_position;
5077 asm_statement_t *asm_statement = &statement->asms;
5079 if(token.type == T_volatile) {
5081 asm_statement->is_volatile = true;
5085 asm_statement->asm_text = parse_string_literals();
5087 if(token.type != ':')
5091 asm_statement->inputs = parse_asm_constraints();
5092 if(token.type != ':')
5096 asm_statement->outputs = parse_asm_constraints();
5097 if(token.type != ':')
5101 asm_statement->clobbers = parse_asm_clobbers();
5110 * Parse a case statement.
5112 static statement_t *parse_case_statement(void)
5116 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5118 statement->base.source_position = token.source_position;
5119 statement->case_label.expression = parse_expression();
5121 if (c_mode & _GNUC) {
5122 if (token.type == T_DOTDOTDOT) {
5124 statement->case_label.end_range = parse_expression();
5130 if (! is_constant_expression(statement->case_label.expression)) {
5131 errorf(statement->base.source_position,
5132 "case label does not reduce to an integer constant");
5134 /* TODO: check if the case label is already known */
5135 if (current_switch != NULL) {
5136 /* link all cases into the switch statement */
5137 if (current_switch->last_case == NULL) {
5138 current_switch->first_case =
5139 current_switch->last_case = &statement->case_label;
5141 current_switch->last_case->next = &statement->case_label;
5144 errorf(statement->base.source_position,
5145 "case label not within a switch statement");
5148 statement->case_label.statement = parse_statement();
5154 * Finds an existing default label of a switch statement.
5156 static case_label_statement_t *
5157 find_default_label(const switch_statement_t *statement)
5159 for (case_label_statement_t *label = statement->first_case;
5161 label = label->next) {
5162 if (label->expression == NULL)
5169 * Parse a default statement.
5171 static statement_t *parse_default_statement(void)
5175 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5177 statement->base.source_position = token.source_position;
5180 if (current_switch != NULL) {
5181 const case_label_statement_t *def_label = find_default_label(current_switch);
5182 if (def_label != NULL) {
5183 errorf(HERE, "multiple default labels in one switch");
5184 errorf(def_label->base.source_position,
5185 "this is the first default label");
5187 /* link all cases into the switch statement */
5188 if (current_switch->last_case == NULL) {
5189 current_switch->first_case =
5190 current_switch->last_case = &statement->case_label;
5192 current_switch->last_case->next = &statement->case_label;
5196 errorf(statement->base.source_position,
5197 "'default' label not within a switch statement");
5199 statement->label.statement = parse_statement();
5205 * Return the declaration for a given label symbol or create a new one.
5207 static declaration_t *get_label(symbol_t *symbol)
5209 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5210 assert(current_function != NULL);
5211 /* if we found a label in the same function, then we already created the
5213 if(candidate != NULL
5214 && candidate->parent_scope == ¤t_function->scope) {
5218 /* otherwise we need to create a new one */
5219 declaration_t *const declaration = allocate_declaration_zero();
5220 declaration->namespc = NAMESPACE_LABEL;
5221 declaration->symbol = symbol;
5223 label_push(declaration);
5229 * Parse a label statement.
5231 static statement_t *parse_label_statement(void)
5233 assert(token.type == T_IDENTIFIER);
5234 symbol_t *symbol = token.v.symbol;
5237 declaration_t *label = get_label(symbol);
5239 /* if source position is already set then the label is defined twice,
5240 * otherwise it was just mentioned in a goto so far */
5241 if(label->source_position.input_name != NULL) {
5242 errorf(HERE, "duplicate label '%Y'", symbol);
5243 errorf(label->source_position, "previous definition of '%Y' was here",
5246 label->source_position = token.source_position;
5249 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5251 statement->base.source_position = token.source_position;
5252 statement->label.label = label;
5256 if(token.type == '}') {
5257 /* TODO only warn? */
5258 errorf(HERE, "label at end of compound statement");
5261 if (token.type == ';') {
5262 /* eat an empty statement here, to avoid the warning about an empty
5263 * after a label. label:; is commonly used to have a label before
5267 statement->label.statement = parse_statement();
5271 /* remember the labels's in a list for later checking */
5272 if (label_last == NULL) {
5273 label_first = &statement->label;
5275 label_last->next = &statement->label;
5277 label_last = &statement->label;
5283 * Parse an if statement.
5285 static statement_t *parse_if(void)
5289 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5290 statement->base.source_position = token.source_position;
5293 statement->ifs.condition = parse_expression();
5296 statement->ifs.true_statement = parse_statement();
5297 if(token.type == T_else) {
5299 statement->ifs.false_statement = parse_statement();
5306 * Parse a switch statement.
5308 static statement_t *parse_switch(void)
5312 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5313 statement->base.source_position = token.source_position;
5316 expression_t *const expr = parse_expression();
5317 type_t * type = skip_typeref(expr->base.type);
5318 if (is_type_integer(type)) {
5319 type = promote_integer(type);
5320 } else if (is_type_valid(type)) {
5321 errorf(expr->base.source_position,
5322 "switch quantity is not an integer, but '%T'", type);
5323 type = type_error_type;
5325 statement->switchs.expression = create_implicit_cast(expr, type);
5328 switch_statement_t *rem = current_switch;
5329 current_switch = &statement->switchs;
5330 statement->switchs.body = parse_statement();
5331 current_switch = rem;
5333 if (warning.switch_default
5334 && find_default_label(&statement->switchs) == NULL) {
5335 warningf(statement->base.source_position, "switch has no default case");
5341 static statement_t *parse_loop_body(statement_t *const loop)
5343 statement_t *const rem = current_loop;
5344 current_loop = loop;
5346 statement_t *const body = parse_statement();
5353 * Parse a while statement.
5355 static statement_t *parse_while(void)
5359 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5360 statement->base.source_position = token.source_position;
5363 statement->whiles.condition = parse_expression();
5366 statement->whiles.body = parse_loop_body(statement);
5372 * Parse a do statement.
5374 static statement_t *parse_do(void)
5378 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5380 statement->base.source_position = token.source_position;
5382 statement->do_while.body = parse_loop_body(statement);
5386 statement->do_while.condition = parse_expression();
5394 * Parse a for statement.
5396 static statement_t *parse_for(void)
5400 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5401 statement->base.source_position = token.source_position;
5405 int top = environment_top();
5406 scope_t *last_scope = scope;
5407 set_scope(&statement->fors.scope);
5409 if(token.type != ';') {
5410 if(is_declaration_specifier(&token, false)) {
5411 parse_declaration(record_declaration);
5413 statement->fors.initialisation = parse_expression();
5420 if(token.type != ';') {
5421 statement->fors.condition = parse_expression();
5424 if(token.type != ')') {
5425 statement->fors.step = parse_expression();
5428 statement->fors.body = parse_loop_body(statement);
5430 assert(scope == &statement->fors.scope);
5431 set_scope(last_scope);
5432 environment_pop_to(top);
5438 * Parse a goto statement.
5440 static statement_t *parse_goto(void)
5444 if(token.type != T_IDENTIFIER) {
5445 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5449 symbol_t *symbol = token.v.symbol;
5452 declaration_t *label = get_label(symbol);
5454 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5455 statement->base.source_position = token.source_position;
5457 statement->gotos.label = label;
5459 /* remember the goto's in a list for later checking */
5460 if (goto_last == NULL) {
5461 goto_first = &statement->gotos;
5463 goto_last->next = &statement->gotos;
5465 goto_last = &statement->gotos;
5473 * Parse a continue statement.
5475 static statement_t *parse_continue(void)
5477 statement_t *statement;
5478 if (current_loop == NULL) {
5479 errorf(HERE, "continue statement not within loop");
5482 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5484 statement->base.source_position = token.source_position;
5494 * Parse a break statement.
5496 static statement_t *parse_break(void)
5498 statement_t *statement;
5499 if (current_switch == NULL && current_loop == NULL) {
5500 errorf(HERE, "break statement not within loop or switch");
5503 statement = allocate_statement_zero(STATEMENT_BREAK);
5505 statement->base.source_position = token.source_position;
5515 * Check if a given declaration represents a local variable.
5517 static bool is_local_var_declaration(const declaration_t *declaration) {
5518 switch ((storage_class_tag_t) declaration->storage_class) {
5519 case STORAGE_CLASS_NONE:
5520 case STORAGE_CLASS_AUTO:
5521 case STORAGE_CLASS_REGISTER: {
5522 const type_t *type = skip_typeref(declaration->type);
5523 if(is_type_function(type)) {
5535 * Check if a given declaration represents a variable.
5537 static bool is_var_declaration(const declaration_t *declaration) {
5538 switch ((storage_class_tag_t) declaration->storage_class) {
5539 case STORAGE_CLASS_NONE:
5540 case STORAGE_CLASS_EXTERN:
5541 case STORAGE_CLASS_STATIC:
5542 case STORAGE_CLASS_AUTO:
5543 case STORAGE_CLASS_REGISTER:
5544 case STORAGE_CLASS_THREAD:
5545 case STORAGE_CLASS_THREAD_EXTERN:
5546 case STORAGE_CLASS_THREAD_STATIC: {
5547 const type_t *type = skip_typeref(declaration->type);
5548 if(is_type_function(type)) {
5560 * Check if a given expression represents a local variable.
5562 static bool is_local_variable(const expression_t *expression)
5564 if (expression->base.kind != EXPR_REFERENCE) {
5567 const declaration_t *declaration = expression->reference.declaration;
5568 return is_local_var_declaration(declaration);
5572 * Check if a given expression represents a local variable and
5573 * return its declaration then, else return NULL.
5575 declaration_t *expr_is_variable(const expression_t *expression)
5577 if (expression->base.kind != EXPR_REFERENCE) {
5580 declaration_t *declaration = expression->reference.declaration;
5581 if (is_var_declaration(declaration))
5587 * Parse a return statement.
5589 static statement_t *parse_return(void)
5593 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5594 statement->base.source_position = token.source_position;
5596 expression_t *return_value = NULL;
5597 if(token.type != ';') {
5598 return_value = parse_expression();
5602 const type_t *const func_type = current_function->type;
5603 assert(is_type_function(func_type));
5604 type_t *const return_type = skip_typeref(func_type->function.return_type);
5606 if(return_value != NULL) {
5607 type_t *return_value_type = skip_typeref(return_value->base.type);
5609 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5610 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5611 warningf(statement->base.source_position,
5612 "'return' with a value, in function returning void");
5613 return_value = NULL;
5615 type_t *const res_type = semantic_assign(return_type,
5616 return_value, "'return'");
5617 if (res_type == NULL) {
5618 errorf(statement->base.source_position,
5619 "cannot return something of type '%T' in function returning '%T'",
5620 return_value->base.type, return_type);
5622 return_value = create_implicit_cast(return_value, res_type);
5625 /* check for returning address of a local var */
5626 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5627 const expression_t *expression = return_value->unary.value;
5628 if (is_local_variable(expression)) {
5629 warningf(statement->base.source_position,
5630 "function returns address of local variable");
5634 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
5635 warningf(statement->base.source_position,
5636 "'return' without value, in function returning non-void");
5639 statement->returns.value = return_value;
5645 * Parse a declaration statement.
5647 static statement_t *parse_declaration_statement(void)
5649 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
5651 statement->base.source_position = token.source_position;
5653 declaration_t *before = last_declaration;
5654 parse_declaration(record_declaration);
5656 if(before == NULL) {
5657 statement->declaration.declarations_begin = scope->declarations;
5659 statement->declaration.declarations_begin = before->next;
5661 statement->declaration.declarations_end = last_declaration;
5667 * Parse an expression statement, ie. expr ';'.
5669 static statement_t *parse_expression_statement(void)
5671 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
5673 statement->base.source_position = token.source_position;
5674 expression_t *const expr = parse_expression();
5675 statement->expression.expression = expr;
5677 if (warning.unused_value && !expression_has_effect(expr)) {
5678 warningf(expr->base.source_position, "statement has no effect");
5687 * Parse a statement.
5689 static statement_t *parse_statement(void)
5691 statement_t *statement = NULL;
5693 /* declaration or statement */
5694 switch(token.type) {
5696 statement = parse_asm_statement();
5700 statement = parse_case_statement();
5704 statement = parse_default_statement();
5708 statement = parse_compound_statement();
5712 statement = parse_if();
5716 statement = parse_switch();
5720 statement = parse_while();
5724 statement = parse_do();
5728 statement = parse_for();
5732 statement = parse_goto();
5736 statement = parse_continue();
5740 statement = parse_break();
5744 statement = parse_return();
5748 if (warning.empty_statement) {
5749 warningf(HERE, "statement is empty");
5756 if(look_ahead(1)->type == ':') {
5757 statement = parse_label_statement();
5761 if(is_typedef_symbol(token.v.symbol)) {
5762 statement = parse_declaration_statement();
5766 statement = parse_expression_statement();
5769 case T___extension__:
5770 /* this can be a prefix to a declaration or an expression statement */
5771 /* we simply eat it now and parse the rest with tail recursion */
5774 } while(token.type == T___extension__);
5775 statement = parse_statement();
5779 statement = parse_declaration_statement();
5783 statement = parse_expression_statement();
5787 assert(statement == NULL
5788 || statement->base.source_position.input_name != NULL);
5794 * Parse a compound statement.
5796 static statement_t *parse_compound_statement(void)
5798 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
5800 statement->base.source_position = token.source_position;
5804 int top = environment_top();
5805 scope_t *last_scope = scope;
5806 set_scope(&statement->compound.scope);
5808 statement_t *last_statement = NULL;
5810 while(token.type != '}' && token.type != T_EOF) {
5811 statement_t *sub_statement = parse_statement();
5812 if(sub_statement == NULL)
5815 if(last_statement != NULL) {
5816 last_statement->base.next = sub_statement;
5818 statement->compound.statements = sub_statement;
5821 while(sub_statement->base.next != NULL)
5822 sub_statement = sub_statement->base.next;
5824 last_statement = sub_statement;
5827 if(token.type == '}') {
5830 errorf(statement->base.source_position,
5831 "end of file while looking for closing '}'");
5834 assert(scope == &statement->compound.scope);
5835 set_scope(last_scope);
5836 environment_pop_to(top);
5842 * Initialize builtin types.
5844 static void initialize_builtin_types(void)
5846 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
5847 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
5848 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
5849 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
5850 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
5851 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
5852 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
5853 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
5855 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
5856 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
5857 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
5858 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
5862 * Check for unused global static functions and variables
5864 static void check_unused_globals(void)
5866 if (!warning.unused_function && !warning.unused_variable)
5869 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
5870 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
5873 type_t *const type = decl->type;
5875 if (is_type_function(skip_typeref(type))) {
5876 if (!warning.unused_function || decl->is_inline)
5879 s = (decl->init.statement != NULL ? "defined" : "declared");
5881 if (!warning.unused_variable)
5887 warningf(decl->source_position, "'%#T' %s but not used",
5888 type, decl->symbol, s);
5893 * Parse a translation unit.
5895 static translation_unit_t *parse_translation_unit(void)
5897 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
5899 assert(global_scope == NULL);
5900 global_scope = &unit->scope;
5902 assert(scope == NULL);
5903 set_scope(&unit->scope);
5905 initialize_builtin_types();
5907 while(token.type != T_EOF) {
5908 if (token.type == ';') {
5909 /* TODO error in strict mode */
5910 warningf(HERE, "stray ';' outside of function");
5913 parse_external_declaration();
5917 assert(scope == &unit->scope);
5919 last_declaration = NULL;
5921 assert(global_scope == &unit->scope);
5922 check_unused_globals();
5923 global_scope = NULL;
5931 * @return the translation unit or NULL if errors occurred.
5933 translation_unit_t *parse(void)
5935 environment_stack = NEW_ARR_F(stack_entry_t, 0);
5936 label_stack = NEW_ARR_F(stack_entry_t, 0);
5937 diagnostic_count = 0;
5941 type_set_output(stderr);
5942 ast_set_output(stderr);
5944 lookahead_bufpos = 0;
5945 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
5948 translation_unit_t *unit = parse_translation_unit();
5950 DEL_ARR_F(environment_stack);
5951 DEL_ARR_F(label_stack);
5960 * Initialize the parser.
5962 void init_parser(void)
5964 init_expression_parsers();
5965 obstack_init(&temp_obst);
5967 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
5968 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
5972 * Terminate the parser.
5974 void exit_parser(void)
5976 obstack_free(&temp_obst, NULL);