7 #include "diagnostic.h"
8 #include "format_check.h"
14 #include "type_hash.h"
16 #include "lang_features.h"
18 #include "adt/bitfiddle.h"
19 #include "adt/error.h"
20 #include "adt/array.h"
22 //#define PRINT_TOKENS
23 #define MAX_LOOKAHEAD 2
26 declaration_t *old_declaration;
28 unsigned short namespc;
31 typedef struct declaration_specifiers_t declaration_specifiers_t;
32 struct declaration_specifiers_t {
33 source_position_t source_position;
34 unsigned char storage_class;
36 decl_modifiers_t decl_modifiers;
40 typedef declaration_t* (*parsed_declaration_func) (declaration_t *declaration);
43 static token_t lookahead_buffer[MAX_LOOKAHEAD];
44 static int lookahead_bufpos;
45 static stack_entry_t *environment_stack = NULL;
46 static stack_entry_t *label_stack = NULL;
47 static scope_t *global_scope = NULL;
48 static scope_t *scope = NULL;
49 static declaration_t *last_declaration = NULL;
50 static declaration_t *current_function = NULL;
51 static switch_statement_t *current_switch = NULL;
52 static statement_t *current_loop = NULL;
53 static goto_statement_t *goto_first = NULL;
54 static goto_statement_t *goto_last = NULL;
55 static label_statement_t *label_first = NULL;
56 static label_statement_t *label_last = NULL;
57 static struct obstack temp_obst;
59 /** The current source position. */
60 #define HERE token.source_position
62 static type_t *type_valist;
64 static statement_t *parse_compound_statement(void);
65 static statement_t *parse_statement(void);
67 static expression_t *parse_sub_expression(unsigned precedence);
68 static expression_t *parse_expression(void);
69 static type_t *parse_typename(void);
71 static void parse_compound_type_entries(declaration_t *compound_declaration);
72 static declaration_t *parse_declarator(
73 const declaration_specifiers_t *specifiers, bool may_be_abstract);
74 static declaration_t *record_declaration(declaration_t *declaration);
76 static void semantic_comparison(binary_expression_t *expression);
78 #define STORAGE_CLASSES \
85 #define TYPE_QUALIFIERS \
92 #ifdef PROVIDE_COMPLEX
93 #define COMPLEX_SPECIFIERS \
95 #define IMAGINARY_SPECIFIERS \
98 #define COMPLEX_SPECIFIERS
99 #define IMAGINARY_SPECIFIERS
102 #define TYPE_SPECIFIERS \
117 case T___builtin_va_list: \
121 #define DECLARATION_START \
126 #define TYPENAME_START \
131 * Allocate an AST node with given size and
132 * initialize all fields with zero.
134 static void *allocate_ast_zero(size_t size)
136 void *res = allocate_ast(size);
137 memset(res, 0, size);
141 static declaration_t *allocate_declaration_zero(void)
143 declaration_t *declaration = allocate_ast_zero(sizeof(declaration_t));
144 declaration->type = type_error_type;
149 * Returns the size of a statement node.
151 * @param kind the statement kind
153 static size_t get_statement_struct_size(statement_kind_t kind)
155 static const size_t sizes[] = {
156 [STATEMENT_COMPOUND] = sizeof(compound_statement_t),
157 [STATEMENT_RETURN] = sizeof(return_statement_t),
158 [STATEMENT_DECLARATION] = sizeof(declaration_statement_t),
159 [STATEMENT_IF] = sizeof(if_statement_t),
160 [STATEMENT_SWITCH] = sizeof(switch_statement_t),
161 [STATEMENT_EXPRESSION] = sizeof(expression_statement_t),
162 [STATEMENT_CONTINUE] = sizeof(statement_base_t),
163 [STATEMENT_BREAK] = sizeof(statement_base_t),
164 [STATEMENT_GOTO] = sizeof(goto_statement_t),
165 [STATEMENT_LABEL] = sizeof(label_statement_t),
166 [STATEMENT_CASE_LABEL] = sizeof(case_label_statement_t),
167 [STATEMENT_WHILE] = sizeof(while_statement_t),
168 [STATEMENT_DO_WHILE] = sizeof(do_while_statement_t),
169 [STATEMENT_FOR] = sizeof(for_statement_t),
170 [STATEMENT_ASM] = sizeof(asm_statement_t)
172 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
173 assert(sizes[kind] != 0);
178 * Allocate a statement node of given kind and initialize all
181 static statement_t *allocate_statement_zero(statement_kind_t kind)
183 size_t size = get_statement_struct_size(kind);
184 statement_t *res = allocate_ast_zero(size);
186 res->base.kind = kind;
191 * Returns the size of an expression node.
193 * @param kind the expression kind
195 static size_t get_expression_struct_size(expression_kind_t kind)
197 static const size_t sizes[] = {
198 [EXPR_INVALID] = sizeof(expression_base_t),
199 [EXPR_REFERENCE] = sizeof(reference_expression_t),
200 [EXPR_CONST] = sizeof(const_expression_t),
201 [EXPR_CHAR_CONST] = sizeof(const_expression_t),
202 [EXPR_STRING_LITERAL] = sizeof(string_literal_expression_t),
203 [EXPR_WIDE_STRING_LITERAL] = sizeof(wide_string_literal_expression_t),
204 [EXPR_COMPOUND_LITERAL] = sizeof(compound_literal_expression_t),
205 [EXPR_CALL] = sizeof(call_expression_t),
206 [EXPR_UNARY_FIRST] = sizeof(unary_expression_t),
207 [EXPR_BINARY_FIRST] = sizeof(binary_expression_t),
208 [EXPR_CONDITIONAL] = sizeof(conditional_expression_t),
209 [EXPR_SELECT] = sizeof(select_expression_t),
210 [EXPR_ARRAY_ACCESS] = sizeof(array_access_expression_t),
211 [EXPR_SIZEOF] = sizeof(typeprop_expression_t),
212 [EXPR_ALIGNOF] = sizeof(typeprop_expression_t),
213 [EXPR_CLASSIFY_TYPE] = sizeof(classify_type_expression_t),
214 [EXPR_FUNCTION] = sizeof(string_literal_expression_t),
215 [EXPR_PRETTY_FUNCTION] = sizeof(string_literal_expression_t),
216 [EXPR_BUILTIN_SYMBOL] = sizeof(builtin_symbol_expression_t),
217 [EXPR_BUILTIN_CONSTANT_P] = sizeof(builtin_constant_expression_t),
218 [EXPR_BUILTIN_PREFETCH] = sizeof(builtin_prefetch_expression_t),
219 [EXPR_OFFSETOF] = sizeof(offsetof_expression_t),
220 [EXPR_VA_START] = sizeof(va_start_expression_t),
221 [EXPR_VA_ARG] = sizeof(va_arg_expression_t),
222 [EXPR_STATEMENT] = sizeof(statement_expression_t),
224 if(kind >= EXPR_UNARY_FIRST && kind <= EXPR_UNARY_LAST) {
225 return sizes[EXPR_UNARY_FIRST];
227 if(kind >= EXPR_BINARY_FIRST && kind <= EXPR_BINARY_LAST) {
228 return sizes[EXPR_BINARY_FIRST];
230 assert(kind <= sizeof(sizes) / sizeof(sizes[0]));
231 assert(sizes[kind] != 0);
236 * Allocate an expression node of given kind and initialize all
239 static expression_t *allocate_expression_zero(expression_kind_t kind)
241 size_t size = get_expression_struct_size(kind);
242 expression_t *res = allocate_ast_zero(size);
244 res->base.kind = kind;
245 res->base.type = type_error_type;
250 * Returns the size of a type node.
252 * @param kind the type kind
254 static size_t get_type_struct_size(type_kind_t kind)
256 static const size_t sizes[] = {
257 [TYPE_ATOMIC] = sizeof(atomic_type_t),
258 [TYPE_BITFIELD] = sizeof(bitfield_type_t),
259 [TYPE_COMPOUND_STRUCT] = sizeof(compound_type_t),
260 [TYPE_COMPOUND_UNION] = sizeof(compound_type_t),
261 [TYPE_ENUM] = sizeof(enum_type_t),
262 [TYPE_FUNCTION] = sizeof(function_type_t),
263 [TYPE_POINTER] = sizeof(pointer_type_t),
264 [TYPE_ARRAY] = sizeof(array_type_t),
265 [TYPE_BUILTIN] = sizeof(builtin_type_t),
266 [TYPE_TYPEDEF] = sizeof(typedef_type_t),
267 [TYPE_TYPEOF] = sizeof(typeof_type_t),
269 assert(sizeof(sizes) / sizeof(sizes[0]) == (int) TYPE_TYPEOF + 1);
270 assert(kind <= TYPE_TYPEOF);
271 assert(sizes[kind] != 0);
276 * Allocate a type node of given kind and initialize all
279 static type_t *allocate_type_zero(type_kind_t kind, source_position_t source_position)
281 size_t size = get_type_struct_size(kind);
282 type_t *res = obstack_alloc(type_obst, size);
283 memset(res, 0, size);
285 res->base.kind = kind;
286 res->base.source_position = source_position;
291 * Returns the size of an initializer node.
293 * @param kind the initializer kind
295 static size_t get_initializer_size(initializer_kind_t kind)
297 static const size_t sizes[] = {
298 [INITIALIZER_VALUE] = sizeof(initializer_value_t),
299 [INITIALIZER_STRING] = sizeof(initializer_string_t),
300 [INITIALIZER_WIDE_STRING] = sizeof(initializer_wide_string_t),
301 [INITIALIZER_LIST] = sizeof(initializer_list_t),
302 [INITIALIZER_DESIGNATOR] = sizeof(initializer_designator_t)
304 assert(kind < sizeof(sizes) / sizeof(*sizes));
305 assert(sizes[kind] != 0);
310 * Allocate an initializer node of given kind and initialize all
313 static initializer_t *allocate_initializer_zero(initializer_kind_t kind)
315 initializer_t *result = allocate_ast_zero(get_initializer_size(kind));
322 * Free a type from the type obstack.
324 static void free_type(void *type)
326 obstack_free(type_obst, type);
330 * Returns the index of the top element of the environment stack.
332 static size_t environment_top(void)
334 return ARR_LEN(environment_stack);
338 * Returns the index of the top element of the label stack.
340 static size_t label_top(void)
342 return ARR_LEN(label_stack);
347 * Return the next token.
349 static inline void next_token(void)
351 token = lookahead_buffer[lookahead_bufpos];
352 lookahead_buffer[lookahead_bufpos] = lexer_token;
355 lookahead_bufpos = (lookahead_bufpos+1) % MAX_LOOKAHEAD;
358 print_token(stderr, &token);
359 fprintf(stderr, "\n");
364 * Return the next token with a given lookahead.
366 static inline const token_t *look_ahead(int num)
368 assert(num > 0 && num <= MAX_LOOKAHEAD);
369 int pos = (lookahead_bufpos+num-1) % MAX_LOOKAHEAD;
370 return &lookahead_buffer[pos];
373 #define eat(token_type) do { assert(token.type == token_type); next_token(); } while(0)
376 * Report a parse error because an expected token was not found.
378 static void parse_error_expected(const char *message, ...)
380 if(message != NULL) {
381 errorf(HERE, "%s", message);
384 va_start(ap, message);
385 errorf(HERE, "got %K, expected %#k", &token, &ap, ", ");
390 * Report a type error.
392 static void type_error(const char *msg, const source_position_t source_position,
395 errorf(source_position, "%s, but found type '%T'", msg, type);
399 * Report an incompatible type.
401 static void type_error_incompatible(const char *msg,
402 const source_position_t source_position, type_t *type1, type_t *type2)
404 errorf(source_position, "%s, incompatible types: '%T' - '%T'", msg, type1, type2);
408 * Eat an complete block, ie. '{ ... }'.
410 static void eat_block(void)
412 if(token.type == '{')
415 while(token.type != '}') {
416 if(token.type == T_EOF)
418 if(token.type == '{') {
428 * Eat a statement until an ';' token.
430 static void eat_statement(void)
432 while(token.type != ';') {
433 if(token.type == T_EOF)
435 if(token.type == '}')
437 if(token.type == '{') {
447 * Eat a parenthesed term, ie. '( ... )'.
449 static void eat_paren(void)
451 if(token.type == '(')
454 while(token.type != ')') {
455 if(token.type == T_EOF)
457 if(token.type == ')' || token.type == ';' || token.type == '}') {
460 if(token.type == '(') {
464 if(token.type == '{') {
473 #define expect(expected) \
475 if(UNLIKELY(token.type != (expected))) { \
476 parse_error_expected(NULL, (expected), 0); \
483 #define expect_block(expected) \
485 if(UNLIKELY(token.type != (expected))) { \
486 parse_error_expected(NULL, (expected), 0); \
493 #define expect_void(expected) \
495 if(UNLIKELY(token.type != (expected))) { \
496 parse_error_expected(NULL, (expected), 0); \
503 static void set_scope(scope_t *new_scope)
506 scope->last_declaration = last_declaration;
510 last_declaration = new_scope->last_declaration;
514 * Search a symbol in a given namespace and returns its declaration or
515 * NULL if this symbol was not found.
517 static declaration_t *get_declaration(const symbol_t *const symbol, const namespace_t namespc)
519 declaration_t *declaration = symbol->declaration;
520 for( ; declaration != NULL; declaration = declaration->symbol_next) {
521 if(declaration->namespc == namespc)
529 * pushs an environment_entry on the environment stack and links the
530 * corresponding symbol to the new entry
532 static void stack_push(stack_entry_t **stack_ptr, declaration_t *declaration)
534 symbol_t *symbol = declaration->symbol;
535 namespace_t namespc = (namespace_t) declaration->namespc;
537 /* replace/add declaration into declaration list of the symbol */
538 declaration_t *iter = symbol->declaration;
540 symbol->declaration = declaration;
542 declaration_t *iter_last = NULL;
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;
561 /* remember old declaration */
563 entry.symbol = symbol;
564 entry.old_declaration = iter;
565 entry.namespc = (unsigned short) namespc;
566 ARR_APP1(stack_entry_t, *stack_ptr, entry);
569 static void environment_push(declaration_t *declaration)
571 assert(declaration->source_position.input_name != NULL);
572 assert(declaration->parent_scope != NULL);
573 stack_push(&environment_stack, declaration);
576 static void label_push(declaration_t *declaration)
578 declaration->parent_scope = ¤t_function->scope;
579 stack_push(&label_stack, declaration);
583 * pops symbols from the environment stack until @p new_top is the top element
585 static void stack_pop_to(stack_entry_t **stack_ptr, size_t new_top)
587 stack_entry_t *stack = *stack_ptr;
588 size_t top = ARR_LEN(stack);
591 assert(new_top <= top);
595 for(i = top; i > new_top; --i) {
596 stack_entry_t *entry = &stack[i - 1];
598 declaration_t *old_declaration = entry->old_declaration;
599 symbol_t *symbol = entry->symbol;
600 namespace_t namespc = (namespace_t)entry->namespc;
602 /* replace/remove declaration */
603 declaration_t *declaration = symbol->declaration;
604 assert(declaration != NULL);
605 if(declaration->namespc == namespc) {
606 if(old_declaration == NULL) {
607 symbol->declaration = declaration->symbol_next;
609 symbol->declaration = old_declaration;
612 declaration_t *iter_last = declaration;
613 declaration_t *iter = declaration->symbol_next;
614 for( ; iter != NULL; iter_last = iter, iter = iter->symbol_next) {
615 /* replace an entry? */
616 if(iter->namespc == namespc) {
617 assert(iter_last != NULL);
618 iter_last->symbol_next = old_declaration;
619 if(old_declaration != NULL) {
620 old_declaration->symbol_next = iter->symbol_next;
625 assert(iter != NULL);
629 ARR_SHRINKLEN(*stack_ptr, (int) new_top);
632 static void environment_pop_to(size_t new_top)
634 stack_pop_to(&environment_stack, new_top);
637 static void label_pop_to(size_t new_top)
639 stack_pop_to(&label_stack, new_top);
643 static int get_rank(const type_t *type)
645 assert(!is_typeref(type));
646 /* The C-standard allows promoting to int or unsigned int (see § 7.2.2
647 * and esp. footnote 108). However we can't fold constants (yet), so we
648 * can't decide whether unsigned int is possible, while int always works.
649 * (unsigned int would be preferable when possible... for stuff like
650 * struct { enum { ... } bla : 4; } ) */
651 if(type->kind == TYPE_ENUM)
652 return ATOMIC_TYPE_INT;
654 assert(type->kind == TYPE_ATOMIC);
655 return type->atomic.akind;
658 static type_t *promote_integer(type_t *type)
660 if(type->kind == TYPE_BITFIELD)
661 type = type->bitfield.base;
663 if(get_rank(type) < ATOMIC_TYPE_INT)
670 * Create a cast expression.
672 * @param expression the expression to cast
673 * @param dest_type the destination type
675 static expression_t *create_cast_expression(expression_t *expression,
678 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST_IMPLICIT);
680 cast->unary.value = expression;
681 cast->base.type = dest_type;
687 * Check if a given expression represents the 0 pointer constant.
689 static bool is_null_pointer_constant(const expression_t *expression)
691 /* skip void* cast */
692 if(expression->kind == EXPR_UNARY_CAST
693 || expression->kind == EXPR_UNARY_CAST_IMPLICIT) {
694 expression = expression->unary.value;
697 /* TODO: not correct yet, should be any constant integer expression
698 * which evaluates to 0 */
699 if (expression->kind != EXPR_CONST)
702 type_t *const type = skip_typeref(expression->base.type);
703 if (!is_type_integer(type))
706 return expression->conste.v.int_value == 0;
710 * Create an implicit cast expression.
712 * @param expression the expression to cast
713 * @param dest_type the destination type
715 static expression_t *create_implicit_cast(expression_t *expression,
718 type_t *const source_type = expression->base.type;
720 if (source_type == dest_type)
723 return create_cast_expression(expression, dest_type);
726 /** Implements the rules from § 6.5.16.1 */
727 static type_t *semantic_assign(type_t *orig_type_left,
728 const expression_t *const right,
731 type_t *const orig_type_right = right->base.type;
732 type_t *const type_left = skip_typeref(orig_type_left);
733 type_t *const type_right = skip_typeref(orig_type_right);
735 if ((is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) ||
736 (is_type_pointer(type_left) && is_null_pointer_constant(right)) ||
737 (is_type_atomic(type_left, ATOMIC_TYPE_BOOL)
738 && is_type_pointer(type_right))) {
739 return orig_type_left;
742 if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
743 type_t *points_to_left = skip_typeref(type_left->pointer.points_to);
744 type_t *points_to_right = skip_typeref(type_right->pointer.points_to);
746 /* the left type has all qualifiers from the right type */
747 unsigned missing_qualifiers
748 = points_to_right->base.qualifiers & ~points_to_left->base.qualifiers;
749 if(missing_qualifiers != 0) {
750 errorf(HERE, "destination type '%T' in %s from type '%T' lacks qualifiers '%Q' in pointed-to type", type_left, context, type_right, missing_qualifiers);
751 return orig_type_left;
754 points_to_left = get_unqualified_type(points_to_left);
755 points_to_right = get_unqualified_type(points_to_right);
757 if (is_type_atomic(points_to_left, ATOMIC_TYPE_VOID) ||
758 is_type_atomic(points_to_right, ATOMIC_TYPE_VOID)) {
759 return orig_type_left;
762 if (!types_compatible(points_to_left, points_to_right)) {
763 warningf(right->base.source_position,
764 "destination type '%T' in %s is incompatible with '%E' of type '%T'",
765 orig_type_left, context, right, orig_type_right);
768 return orig_type_left;
771 if ((is_type_compound(type_left) && is_type_compound(type_right))
772 || (is_type_builtin(type_left) && is_type_builtin(type_right))) {
773 type_t *const unqual_type_left = get_unqualified_type(type_left);
774 type_t *const unqual_type_right = get_unqualified_type(type_right);
775 if (types_compatible(unqual_type_left, unqual_type_right)) {
776 return orig_type_left;
780 if (!is_type_valid(type_left))
783 if (!is_type_valid(type_right))
784 return orig_type_right;
789 static expression_t *parse_constant_expression(void)
791 /* start parsing at precedence 7 (conditional expression) */
792 expression_t *result = parse_sub_expression(7);
794 if(!is_constant_expression(result)) {
795 errorf(result->base.source_position, "expression '%E' is not constant\n", result);
801 static expression_t *parse_assignment_expression(void)
803 /* start parsing at precedence 2 (assignment expression) */
804 return parse_sub_expression(2);
807 static type_t *make_global_typedef(const char *name, type_t *type)
809 symbol_t *const symbol = symbol_table_insert(name);
811 declaration_t *const declaration = allocate_declaration_zero();
812 declaration->namespc = NAMESPACE_NORMAL;
813 declaration->storage_class = STORAGE_CLASS_TYPEDEF;
814 declaration->type = type;
815 declaration->symbol = symbol;
816 declaration->source_position = builtin_source_position;
818 record_declaration(declaration);
820 type_t *typedef_type = allocate_type_zero(TYPE_TYPEDEF, builtin_source_position);
821 typedef_type->typedeft.declaration = declaration;
826 static string_t parse_string_literals(void)
828 assert(token.type == T_STRING_LITERAL);
829 string_t result = token.v.string;
833 while (token.type == T_STRING_LITERAL) {
834 result = concat_strings(&result, &token.v.string);
841 static void parse_attributes(void)
845 case T___attribute__: {
853 errorf(HERE, "EOF while parsing attribute");
872 if(token.type != T_STRING_LITERAL) {
873 parse_error_expected("while parsing assembler attribute",
878 parse_string_literals();
883 goto attributes_finished;
891 static designator_t *parse_designation(void)
893 designator_t *result = NULL;
894 designator_t *last = NULL;
897 designator_t *designator;
900 designator = allocate_ast_zero(sizeof(designator[0]));
901 designator->source_position = token.source_position;
903 designator->array_index = parse_constant_expression();
907 designator = allocate_ast_zero(sizeof(designator[0]));
908 designator->source_position = token.source_position;
910 if(token.type != T_IDENTIFIER) {
911 parse_error_expected("while parsing designator",
915 designator->symbol = token.v.symbol;
923 assert(designator != NULL);
925 last->next = designator;
933 static initializer_t *initializer_from_string(array_type_t *type,
934 const string_t *const string)
936 /* TODO: check len vs. size of array type */
939 initializer_t *initializer = allocate_initializer_zero(INITIALIZER_STRING);
940 initializer->string.string = *string;
945 static initializer_t *initializer_from_wide_string(array_type_t *const type,
946 wide_string_t *const string)
948 /* TODO: check len vs. size of array type */
951 initializer_t *const initializer =
952 allocate_initializer_zero(INITIALIZER_WIDE_STRING);
953 initializer->wide_string.string = *string;
958 static initializer_t *initializer_from_expression(type_t *orig_type,
959 expression_t *expression)
961 /* TODO check that expression is a constant expression */
963 /* § 6.7.8.14/15 char array may be initialized by string literals */
964 type_t *type = skip_typeref(orig_type);
965 type_t *expr_type_orig = expression->base.type;
966 type_t *expr_type = skip_typeref(expr_type_orig);
967 if (is_type_array(type) && expr_type->kind == TYPE_POINTER) {
968 array_type_t *const array_type = &type->array;
969 type_t *const element_type = skip_typeref(array_type->element_type);
971 if (element_type->kind == TYPE_ATOMIC) {
972 atomic_type_kind_t akind = element_type->atomic.akind;
973 switch (expression->kind) {
974 case EXPR_STRING_LITERAL:
975 if (akind == ATOMIC_TYPE_CHAR
976 || akind == ATOMIC_TYPE_SCHAR
977 || akind == ATOMIC_TYPE_UCHAR) {
978 return initializer_from_string(array_type,
979 &expression->string.value);
982 case EXPR_WIDE_STRING_LITERAL: {
983 type_t *bare_wchar_type = skip_typeref(type_wchar_t);
984 if (get_unqualified_type(element_type) == bare_wchar_type) {
985 return initializer_from_wide_string(array_type,
986 &expression->wide_string.value);
996 type_t *const res_type = semantic_assign(type, expression, "initializer");
997 if (res_type == NULL)
1000 initializer_t *const result = allocate_initializer_zero(INITIALIZER_VALUE);
1001 result->value.value = create_implicit_cast(expression, res_type);
1006 static initializer_t *parse_scalar_initializer(type_t *type)
1008 /* there might be extra {} hierarchies */
1010 while(token.type == '{') {
1013 warningf(HERE, "extra curly braces around scalar initializer");
1018 expression_t *expression = parse_assignment_expression();
1019 initializer_t *initializer = initializer_from_expression(type, expression);
1021 if(initializer == NULL) {
1022 errorf(expression->base.source_position,
1023 "expression '%E' doesn't match expected type '%T'",
1029 bool additional_warning_displayed = false;
1031 if(token.type == ',') {
1034 if(token.type != '}') {
1035 if(!additional_warning_displayed) {
1036 warningf(HERE, "additional elements in scalar initializer");
1037 additional_warning_displayed = true;
1047 typedef struct type_path_entry_t type_path_entry_t;
1048 struct type_path_entry_t {
1052 declaration_t *compound_entry;
1056 typedef struct type_path_t type_path_t;
1057 struct type_path_t {
1058 type_path_entry_t *path;
1063 static __attribute__((unused)) void debug_print_type_path(const type_path_t *path)
1065 size_t len = ARR_LEN(path->path);
1068 fprintf(stderr, "invalid path");
1072 for(size_t i = 0; i < len; ++i) {
1073 const type_path_entry_t *entry = & path->path[i];
1075 type_t *type = skip_typeref(entry->type);
1076 if(is_type_compound(type)) {
1077 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1078 } else if(is_type_array(type)) {
1079 fprintf(stderr, "[%u]", entry->v.index);
1081 fprintf(stderr, "-INVALID-");
1084 fprintf(stderr, " (");
1085 print_type(path->top_type);
1086 fprintf(stderr, ")");
1089 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1091 size_t len = ARR_LEN(path->path);
1093 return & path->path[len-1];
1096 static type_path_entry_t *append_to_type_path(type_path_t *path)
1098 size_t len = ARR_LEN(path->path);
1099 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1101 type_path_entry_t *result = & path->path[len];
1102 memset(result, 0, sizeof(result[0]));
1106 static void descend_into_subtype(type_path_t *path)
1108 type_t *orig_top_type = path->top_type;
1109 type_t *top_type = skip_typeref(orig_top_type);
1111 assert(is_type_compound(top_type) || is_type_array(top_type));
1113 type_path_entry_t *top = append_to_type_path(path);
1114 top->type = top_type;
1116 if(is_type_compound(top_type)) {
1117 declaration_t *declaration = top_type->compound.declaration;
1118 declaration_t *entry = declaration->scope.declarations;
1120 top->v.compound_entry = entry;
1121 path->top_type = entry->type;
1123 assert(is_type_array(top_type));
1126 path->top_type = top_type->array.element_type;
1130 static void ascend_from_subtype(type_path_t *path)
1132 type_path_entry_t *top = get_type_path_top(path);
1134 path->top_type = top->type;
1136 size_t len = ARR_LEN(path->path);
1137 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1140 static void ascend_to(type_path_t *path, size_t top_path_level)
1142 size_t len = ARR_LEN(path->path);
1143 assert(len >= top_path_level);
1145 while(len > top_path_level) {
1146 ascend_from_subtype(path);
1147 len = ARR_LEN(path->path);
1151 static bool walk_designator(type_path_t *path, const designator_t *designator,
1152 bool used_in_offsetof)
1154 for( ; designator != NULL; designator = designator->next) {
1155 type_path_entry_t *top = get_type_path_top(path);
1156 type_t *orig_type = top->type;
1158 type_t *type = skip_typeref(orig_type);
1160 if(designator->symbol != NULL) {
1161 symbol_t *symbol = designator->symbol;
1162 if(!is_type_compound(type)) {
1163 if(is_type_valid(type)) {
1164 errorf(designator->source_position,
1165 "'.%Y' designator used for non-compound type '%T'",
1171 declaration_t *declaration = type->compound.declaration;
1172 declaration_t *iter = declaration->scope.declarations;
1173 for( ; iter != NULL; iter = iter->next) {
1174 if(iter->symbol == symbol) {
1179 errorf(designator->source_position,
1180 "'%T' has no member named '%Y'", orig_type, symbol);
1183 if(used_in_offsetof) {
1184 type_t *real_type = skip_typeref(iter->type);
1185 if(real_type->kind == TYPE_BITFIELD) {
1186 errorf(designator->source_position,
1187 "offsetof designator '%Y' may not specify bitfield",
1193 top->type = orig_type;
1194 top->v.compound_entry = iter;
1195 orig_type = iter->type;
1197 expression_t *array_index = designator->array_index;
1198 assert(designator->array_index != NULL);
1200 if(!is_type_array(type)) {
1201 if(is_type_valid(type)) {
1202 errorf(designator->source_position,
1203 "[%E] designator used for non-array type '%T'",
1204 array_index, orig_type);
1208 if(!is_type_valid(array_index->base.type)) {
1212 long index = fold_constant(array_index);
1213 if(!used_in_offsetof) {
1215 errorf(designator->source_position,
1216 "array index [%E] must be positive", array_index);
1219 if(type->array.size_constant == true) {
1220 long array_size = type->array.size;
1221 if(index >= array_size) {
1222 errorf(designator->source_position,
1223 "designator [%E] (%d) exceeds array size %d",
1224 array_index, index, array_size);
1230 top->type = orig_type;
1231 top->v.index = (size_t) index;
1232 orig_type = type->array.element_type;
1234 path->top_type = orig_type;
1236 if(designator->next != NULL) {
1237 descend_into_subtype(path);
1241 path->invalid = false;
1248 static void advance_current_object(type_path_t *path, size_t top_path_level)
1253 type_path_entry_t *top = get_type_path_top(path);
1255 type_t *type = skip_typeref(top->type);
1256 if(is_type_union(type)) {
1257 /* in unions only the first element is initialized */
1258 top->v.compound_entry = NULL;
1259 } else if(is_type_struct(type)) {
1260 declaration_t *entry = top->v.compound_entry;
1262 entry = entry->next;
1263 top->v.compound_entry = entry;
1265 path->top_type = entry->type;
1269 assert(is_type_array(type));
1273 if(!type->array.size_constant || top->v.index < type->array.size) {
1278 /* we're past the last member of the current sub-aggregate, try if we
1279 * can ascend in the type hierarchy and continue with another subobject */
1280 size_t len = ARR_LEN(path->path);
1282 if(len > top_path_level) {
1283 ascend_from_subtype(path);
1284 advance_current_object(path, top_path_level);
1286 path->invalid = true;
1290 static void skip_initializers(void)
1292 if(token.type == '{')
1295 while(token.type != '}') {
1296 if(token.type == T_EOF)
1298 if(token.type == '{') {
1306 static initializer_t *parse_sub_initializer(type_path_t *path,
1307 type_t *outer_type, size_t top_path_level)
1309 type_t *orig_type = path->top_type;
1310 type_t *type = skip_typeref(orig_type);
1312 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1313 * initializers in this case. */
1314 if(!is_type_valid(type)) {
1315 skip_initializers();
1319 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1322 designator_t *designator = NULL;
1323 if(token.type == '.' || token.type == '[') {
1324 designator = parse_designation();
1326 /* reset path to toplevel, evaluate designator from there */
1327 ascend_to(path, top_path_level);
1328 if(!walk_designator(path, designator, false)) {
1329 /* can't continue after designation error */
1333 initializer_t *designator_initializer
1334 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1335 designator_initializer->designator.designator = designator;
1336 ARR_APP1(initializer_t*, initializers, designator_initializer);
1341 if(token.type == '{') {
1342 if(is_type_scalar(type)) {
1343 sub = parse_scalar_initializer(type);
1346 descend_into_subtype(path);
1348 sub = parse_sub_initializer(path, orig_type, top_path_level+1);
1350 ascend_from_subtype(path);
1355 /* must be an expression */
1356 expression_t *expression = parse_assignment_expression();
1358 /* handle { "string" } special case */
1359 if((expression->kind == EXPR_STRING_LITERAL
1360 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1361 && outer_type != NULL) {
1362 sub = initializer_from_expression(outer_type, expression);
1364 if(token.type == ',') {
1367 if(token.type != '}') {
1368 warningf(HERE, "excessive elements in initializer for type '%T'",
1371 /* TODO: eat , ... */
1376 /* descend into subtypes until expression matches type */
1378 orig_type = path->top_type;
1379 type = skip_typeref(orig_type);
1381 sub = initializer_from_expression(orig_type, expression);
1385 if(!is_type_valid(type)) {
1388 if(is_type_scalar(type)) {
1389 errorf(expression->base.source_position,
1390 "expression '%E' doesn't match expected type '%T'",
1391 expression, orig_type);
1395 descend_into_subtype(path);
1398 ARR_APP1(initializer_t*, initializers, sub);
1400 if(token.type == '}') {
1404 if(token.type == '}') {
1407 advance_current_object(path, top_path_level);
1410 size_t len = ARR_LEN(initializers);
1411 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1412 initializer_t *result = allocate_ast_zero(size);
1413 result->kind = INITIALIZER_LIST;
1414 result->list.len = len;
1415 memcpy(&result->list.initializers, initializers,
1416 len * sizeof(initializers[0]));
1418 ascend_to(path, top_path_level);
1420 /* TODO: if(is_global && !is_constant(...)) { error } */
1425 skip_initializers();
1426 DEL_ARR_F(initializers);
1427 ascend_to(path, top_path_level);
1431 static initializer_t *parse_initializer(type_t *const orig_type)
1433 initializer_t *result;
1435 type_t *const type = skip_typeref(orig_type);
1437 if(token.type != '{') {
1438 expression_t *expression = parse_assignment_expression();
1439 initializer_t *initializer = initializer_from_expression(type, expression);
1440 if(initializer == NULL) {
1442 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1443 expression, expression->base.type, orig_type);
1448 if(is_type_scalar(type)) {
1449 /* TODO: § 6.7.8.11; eat {} without warning */
1451 result = parse_scalar_initializer(type);
1453 if(token.type == ',')
1457 } else if(token.type == '{') {
1461 memset(&path, 0, sizeof(path));
1462 path.top_type = orig_type;
1463 path.path = NEW_ARR_F(type_path_entry_t, 0);
1465 descend_into_subtype(&path);
1467 result = parse_sub_initializer(&path, orig_type, 1);
1469 DEL_ARR_F(path.path);
1479 static declaration_t *append_declaration(declaration_t *declaration);
1481 static declaration_t *parse_compound_type_specifier(bool is_struct)
1489 symbol_t *symbol = NULL;
1490 declaration_t *declaration = NULL;
1492 if (token.type == T___attribute__) {
1497 if(token.type == T_IDENTIFIER) {
1498 symbol = token.v.symbol;
1502 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1504 declaration = get_declaration(symbol, NAMESPACE_UNION);
1506 } else if(token.type != '{') {
1508 parse_error_expected("while parsing struct type specifier",
1509 T_IDENTIFIER, '{', 0);
1511 parse_error_expected("while parsing union type specifier",
1512 T_IDENTIFIER, '{', 0);
1518 if(declaration == NULL) {
1519 declaration = allocate_declaration_zero();
1520 declaration->namespc =
1521 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1522 declaration->source_position = token.source_position;
1523 declaration->symbol = symbol;
1524 declaration->parent_scope = scope;
1525 if (symbol != NULL) {
1526 environment_push(declaration);
1528 append_declaration(declaration);
1531 if(token.type == '{') {
1532 if(declaration->init.is_defined) {
1533 assert(symbol != NULL);
1534 errorf(HERE, "multiple definitions of '%s %Y'",
1535 is_struct ? "struct" : "union", symbol);
1536 declaration->scope.declarations = NULL;
1538 declaration->init.is_defined = true;
1540 parse_compound_type_entries(declaration);
1547 static void parse_enum_entries(type_t *const enum_type)
1551 if(token.type == '}') {
1553 errorf(HERE, "empty enum not allowed");
1558 if(token.type != T_IDENTIFIER) {
1559 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1564 declaration_t *const entry = allocate_declaration_zero();
1565 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1566 entry->type = enum_type;
1567 entry->symbol = token.v.symbol;
1568 entry->source_position = token.source_position;
1571 if(token.type == '=') {
1573 expression_t *value = parse_constant_expression();
1575 value = create_implicit_cast(value, enum_type);
1576 entry->init.enum_value = value;
1581 record_declaration(entry);
1583 if(token.type != ',')
1586 } while(token.type != '}');
1591 static type_t *parse_enum_specifier(void)
1595 declaration_t *declaration;
1598 if(token.type == T_IDENTIFIER) {
1599 symbol = token.v.symbol;
1602 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1603 } else if(token.type != '{') {
1604 parse_error_expected("while parsing enum type specifier",
1605 T_IDENTIFIER, '{', 0);
1612 if(declaration == NULL) {
1613 declaration = allocate_declaration_zero();
1614 declaration->namespc = NAMESPACE_ENUM;
1615 declaration->source_position = token.source_position;
1616 declaration->symbol = symbol;
1617 declaration->parent_scope = scope;
1620 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1621 type->enumt.declaration = declaration;
1623 if(token.type == '{') {
1624 if(declaration->init.is_defined) {
1625 errorf(HERE, "multiple definitions of enum %Y", symbol);
1627 if (symbol != NULL) {
1628 environment_push(declaration);
1630 append_declaration(declaration);
1631 declaration->init.is_defined = 1;
1633 parse_enum_entries(type);
1641 * if a symbol is a typedef to another type, return true
1643 static bool is_typedef_symbol(symbol_t *symbol)
1645 const declaration_t *const declaration =
1646 get_declaration(symbol, NAMESPACE_NORMAL);
1648 declaration != NULL &&
1649 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1652 static type_t *parse_typeof(void)
1660 expression_t *expression = NULL;
1663 switch(token.type) {
1664 case T___extension__:
1665 /* this can be a prefix to a typename or an expression */
1666 /* we simply eat it now. */
1669 } while(token.type == T___extension__);
1673 if(is_typedef_symbol(token.v.symbol)) {
1674 type = parse_typename();
1676 expression = parse_expression();
1677 type = expression->base.type;
1682 type = parse_typename();
1686 expression = parse_expression();
1687 type = expression->base.type;
1693 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1694 typeof_type->typeoft.expression = expression;
1695 typeof_type->typeoft.typeof_type = type;
1701 SPECIFIER_SIGNED = 1 << 0,
1702 SPECIFIER_UNSIGNED = 1 << 1,
1703 SPECIFIER_LONG = 1 << 2,
1704 SPECIFIER_INT = 1 << 3,
1705 SPECIFIER_DOUBLE = 1 << 4,
1706 SPECIFIER_CHAR = 1 << 5,
1707 SPECIFIER_SHORT = 1 << 6,
1708 SPECIFIER_LONG_LONG = 1 << 7,
1709 SPECIFIER_FLOAT = 1 << 8,
1710 SPECIFIER_BOOL = 1 << 9,
1711 SPECIFIER_VOID = 1 << 10,
1712 #ifdef PROVIDE_COMPLEX
1713 SPECIFIER_COMPLEX = 1 << 11,
1714 SPECIFIER_IMAGINARY = 1 << 12,
1718 static type_t *create_builtin_type(symbol_t *const symbol,
1719 type_t *const real_type)
1721 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1722 type->builtin.symbol = symbol;
1723 type->builtin.real_type = real_type;
1725 type_t *result = typehash_insert(type);
1726 if (type != result) {
1733 static type_t *get_typedef_type(symbol_t *symbol)
1735 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1736 if(declaration == NULL
1737 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1740 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1741 type->typedeft.declaration = declaration;
1746 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1748 type_t *type = NULL;
1749 unsigned type_qualifiers = 0;
1750 unsigned type_specifiers = 0;
1753 specifiers->source_position = token.source_position;
1756 switch(token.type) {
1759 #define MATCH_STORAGE_CLASS(token, class) \
1761 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1762 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1764 specifiers->storage_class = class; \
1768 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1769 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1770 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1771 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1772 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1775 switch (specifiers->storage_class) {
1776 case STORAGE_CLASS_NONE:
1777 specifiers->storage_class = STORAGE_CLASS_THREAD;
1780 case STORAGE_CLASS_EXTERN:
1781 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1784 case STORAGE_CLASS_STATIC:
1785 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1789 errorf(HERE, "multiple storage classes in declaration specifiers");
1795 /* type qualifiers */
1796 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1798 type_qualifiers |= qualifier; \
1802 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1803 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1804 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1806 case T___extension__:
1811 /* type specifiers */
1812 #define MATCH_SPECIFIER(token, specifier, name) \
1815 if(type_specifiers & specifier) { \
1816 errorf(HERE, "multiple " name " type specifiers given"); \
1818 type_specifiers |= specifier; \
1822 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1823 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1824 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1825 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1826 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1827 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1828 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1829 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1830 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1831 #ifdef PROVIDE_COMPLEX
1832 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1833 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1836 /* only in microsoft mode */
1837 specifiers->decl_modifiers |= DM_FORCEINLINE;
1841 specifiers->is_inline = true;
1846 if(type_specifiers & SPECIFIER_LONG_LONG) {
1847 errorf(HERE, "multiple type specifiers given");
1848 } else if(type_specifiers & SPECIFIER_LONG) {
1849 type_specifiers |= SPECIFIER_LONG_LONG;
1851 type_specifiers |= SPECIFIER_LONG;
1856 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1858 type->compound.declaration = parse_compound_type_specifier(true);
1862 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1864 type->compound.declaration = parse_compound_type_specifier(false);
1868 type = parse_enum_specifier();
1871 type = parse_typeof();
1873 case T___builtin_va_list:
1874 type = duplicate_type(type_valist);
1878 case T___attribute__:
1882 case T_IDENTIFIER: {
1883 /* only parse identifier if we haven't found a type yet */
1884 if(type != NULL || type_specifiers != 0)
1885 goto finish_specifiers;
1887 type_t *typedef_type = get_typedef_type(token.v.symbol);
1889 if(typedef_type == NULL)
1890 goto finish_specifiers;
1893 type = typedef_type;
1897 /* function specifier */
1899 goto finish_specifiers;
1906 atomic_type_kind_t atomic_type;
1908 /* match valid basic types */
1909 switch(type_specifiers) {
1910 case SPECIFIER_VOID:
1911 atomic_type = ATOMIC_TYPE_VOID;
1913 case SPECIFIER_CHAR:
1914 atomic_type = ATOMIC_TYPE_CHAR;
1916 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1917 atomic_type = ATOMIC_TYPE_SCHAR;
1919 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1920 atomic_type = ATOMIC_TYPE_UCHAR;
1922 case SPECIFIER_SHORT:
1923 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1924 case SPECIFIER_SHORT | SPECIFIER_INT:
1925 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1926 atomic_type = ATOMIC_TYPE_SHORT;
1928 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1929 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1930 atomic_type = ATOMIC_TYPE_USHORT;
1933 case SPECIFIER_SIGNED:
1934 case SPECIFIER_SIGNED | SPECIFIER_INT:
1935 atomic_type = ATOMIC_TYPE_INT;
1937 case SPECIFIER_UNSIGNED:
1938 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
1939 atomic_type = ATOMIC_TYPE_UINT;
1941 case SPECIFIER_LONG:
1942 case SPECIFIER_SIGNED | SPECIFIER_LONG:
1943 case SPECIFIER_LONG | SPECIFIER_INT:
1944 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1945 atomic_type = ATOMIC_TYPE_LONG;
1947 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
1948 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
1949 atomic_type = ATOMIC_TYPE_ULONG;
1951 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1952 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1953 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
1954 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1956 atomic_type = ATOMIC_TYPE_LONGLONG;
1958 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
1959 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
1961 atomic_type = ATOMIC_TYPE_ULONGLONG;
1963 case SPECIFIER_FLOAT:
1964 atomic_type = ATOMIC_TYPE_FLOAT;
1966 case SPECIFIER_DOUBLE:
1967 atomic_type = ATOMIC_TYPE_DOUBLE;
1969 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
1970 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
1972 case SPECIFIER_BOOL:
1973 atomic_type = ATOMIC_TYPE_BOOL;
1975 #ifdef PROVIDE_COMPLEX
1976 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
1977 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
1979 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1980 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
1982 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
1983 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
1985 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
1986 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
1988 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1989 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
1991 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
1992 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
1996 /* invalid specifier combination, give an error message */
1997 if(type_specifiers == 0) {
1998 if (! strict_mode) {
1999 if (warning.implicit_int) {
2000 warningf(HERE, "no type specifiers in declaration, using 'int'");
2002 atomic_type = ATOMIC_TYPE_INT;
2005 errorf(HERE, "no type specifiers given in declaration");
2007 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2008 (type_specifiers & SPECIFIER_UNSIGNED)) {
2009 errorf(HERE, "signed and unsigned specifiers gives");
2010 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2011 errorf(HERE, "only integer types can be signed or unsigned");
2013 errorf(HERE, "multiple datatypes in declaration");
2015 atomic_type = ATOMIC_TYPE_INVALID;
2018 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2019 type->atomic.akind = atomic_type;
2022 if(type_specifiers != 0) {
2023 errorf(HERE, "multiple datatypes in declaration");
2027 type->base.qualifiers = type_qualifiers;
2029 type_t *result = typehash_insert(type);
2030 if(newtype && result != type) {
2034 specifiers->type = result;
2037 static type_qualifiers_t parse_type_qualifiers(void)
2039 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2042 switch(token.type) {
2043 /* type qualifiers */
2044 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2045 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2046 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2049 return type_qualifiers;
2054 static declaration_t *parse_identifier_list(void)
2056 declaration_t *declarations = NULL;
2057 declaration_t *last_declaration = NULL;
2059 declaration_t *const declaration = allocate_declaration_zero();
2060 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2061 declaration->source_position = token.source_position;
2062 declaration->symbol = token.v.symbol;
2065 if(last_declaration != NULL) {
2066 last_declaration->next = declaration;
2068 declarations = declaration;
2070 last_declaration = declaration;
2072 if(token.type != ',')
2075 } while(token.type == T_IDENTIFIER);
2077 return declarations;
2080 static void semantic_parameter(declaration_t *declaration)
2082 /* TODO: improve error messages */
2084 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2085 errorf(HERE, "typedef not allowed in parameter list");
2086 } else if(declaration->storage_class != STORAGE_CLASS_NONE
2087 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
2088 errorf(HERE, "parameter may only have none or register storage class");
2091 type_t *const orig_type = declaration->type;
2092 type_t * type = skip_typeref(orig_type);
2094 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2095 * into a pointer. § 6.7.5.3 (7) */
2096 if (is_type_array(type)) {
2097 type_t *const element_type = type->array.element_type;
2099 type = make_pointer_type(element_type, type->base.qualifiers);
2101 declaration->type = type;
2104 if(is_type_incomplete(type)) {
2105 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2106 orig_type, declaration->symbol);
2110 static declaration_t *parse_parameter(void)
2112 declaration_specifiers_t specifiers;
2113 memset(&specifiers, 0, sizeof(specifiers));
2115 parse_declaration_specifiers(&specifiers);
2117 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2119 semantic_parameter(declaration);
2124 static declaration_t *parse_parameters(function_type_t *type)
2126 if(token.type == T_IDENTIFIER) {
2127 symbol_t *symbol = token.v.symbol;
2128 if(!is_typedef_symbol(symbol)) {
2129 type->kr_style_parameters = true;
2130 return parse_identifier_list();
2134 if(token.type == ')') {
2135 type->unspecified_parameters = 1;
2138 if(token.type == T_void && look_ahead(1)->type == ')') {
2143 declaration_t *declarations = NULL;
2144 declaration_t *declaration;
2145 declaration_t *last_declaration = NULL;
2146 function_parameter_t *parameter;
2147 function_parameter_t *last_parameter = NULL;
2150 switch(token.type) {
2154 return declarations;
2157 case T___extension__:
2159 declaration = parse_parameter();
2161 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2162 memset(parameter, 0, sizeof(parameter[0]));
2163 parameter->type = declaration->type;
2165 if(last_parameter != NULL) {
2166 last_declaration->next = declaration;
2167 last_parameter->next = parameter;
2169 type->parameters = parameter;
2170 declarations = declaration;
2172 last_parameter = parameter;
2173 last_declaration = declaration;
2177 return declarations;
2179 if(token.type != ',')
2180 return declarations;
2190 } construct_type_kind_t;
2192 typedef struct construct_type_t construct_type_t;
2193 struct construct_type_t {
2194 construct_type_kind_t kind;
2195 construct_type_t *next;
2198 typedef struct parsed_pointer_t parsed_pointer_t;
2199 struct parsed_pointer_t {
2200 construct_type_t construct_type;
2201 type_qualifiers_t type_qualifiers;
2204 typedef struct construct_function_type_t construct_function_type_t;
2205 struct construct_function_type_t {
2206 construct_type_t construct_type;
2207 type_t *function_type;
2210 typedef struct parsed_array_t parsed_array_t;
2211 struct parsed_array_t {
2212 construct_type_t construct_type;
2213 type_qualifiers_t type_qualifiers;
2219 typedef struct construct_base_type_t construct_base_type_t;
2220 struct construct_base_type_t {
2221 construct_type_t construct_type;
2225 static construct_type_t *parse_pointer_declarator(void)
2229 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2230 memset(pointer, 0, sizeof(pointer[0]));
2231 pointer->construct_type.kind = CONSTRUCT_POINTER;
2232 pointer->type_qualifiers = parse_type_qualifiers();
2234 return (construct_type_t*) pointer;
2237 static construct_type_t *parse_array_declarator(void)
2241 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2242 memset(array, 0, sizeof(array[0]));
2243 array->construct_type.kind = CONSTRUCT_ARRAY;
2245 if(token.type == T_static) {
2246 array->is_static = true;
2250 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2251 if(type_qualifiers != 0) {
2252 if(token.type == T_static) {
2253 array->is_static = true;
2257 array->type_qualifiers = type_qualifiers;
2259 if(token.type == '*' && look_ahead(1)->type == ']') {
2260 array->is_variable = true;
2262 } else if(token.type != ']') {
2263 array->size = parse_assignment_expression();
2268 return (construct_type_t*) array;
2271 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2276 if(declaration != NULL) {
2277 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2279 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2282 declaration_t *parameters = parse_parameters(&type->function);
2283 if(declaration != NULL) {
2284 declaration->scope.declarations = parameters;
2287 construct_function_type_t *construct_function_type =
2288 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2289 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2290 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2291 construct_function_type->function_type = type;
2295 return (construct_type_t*) construct_function_type;
2298 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2299 bool may_be_abstract)
2301 /* construct a single linked list of construct_type_t's which describe
2302 * how to construct the final declarator type */
2303 construct_type_t *first = NULL;
2304 construct_type_t *last = NULL;
2307 while(token.type == '*') {
2308 construct_type_t *type = parse_pointer_declarator();
2319 /* TODO: find out if this is correct */
2322 construct_type_t *inner_types = NULL;
2324 switch(token.type) {
2326 if(declaration == NULL) {
2327 errorf(HERE, "no identifier expected in typename");
2329 declaration->symbol = token.v.symbol;
2330 declaration->source_position = token.source_position;
2336 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2342 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2343 /* avoid a loop in the outermost scope, because eat_statement doesn't
2345 if(token.type == '}' && current_function == NULL) {
2353 construct_type_t *p = last;
2356 construct_type_t *type;
2357 switch(token.type) {
2359 type = parse_function_declarator(declaration);
2362 type = parse_array_declarator();
2365 goto declarator_finished;
2368 /* insert in the middle of the list (behind p) */
2370 type->next = p->next;
2381 declarator_finished:
2384 /* append inner_types at the end of the list, we don't to set last anymore
2385 * as it's not needed anymore */
2387 assert(first == NULL);
2388 first = inner_types;
2390 last->next = inner_types;
2396 static type_t *construct_declarator_type(construct_type_t *construct_list,
2399 construct_type_t *iter = construct_list;
2400 for( ; iter != NULL; iter = iter->next) {
2401 switch(iter->kind) {
2402 case CONSTRUCT_INVALID:
2403 panic("invalid type construction found");
2404 case CONSTRUCT_FUNCTION: {
2405 construct_function_type_t *construct_function_type
2406 = (construct_function_type_t*) iter;
2408 type_t *function_type = construct_function_type->function_type;
2410 function_type->function.return_type = type;
2412 type_t *skipped_return_type = skip_typeref(type);
2413 if (is_type_function(skipped_return_type)) {
2414 errorf(HERE, "function returning function is not allowed");
2415 type = type_error_type;
2416 } else if (is_type_array(skipped_return_type)) {
2417 errorf(HERE, "function returning array is not allowed");
2418 type = type_error_type;
2420 type = function_type;
2425 case CONSTRUCT_POINTER: {
2426 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2427 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2428 pointer_type->pointer.points_to = type;
2429 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2431 type = pointer_type;
2435 case CONSTRUCT_ARRAY: {
2436 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2437 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2439 expression_t *size_expression = parsed_array->size;
2441 array_type->base.qualifiers = parsed_array->type_qualifiers;
2442 array_type->array.element_type = type;
2443 array_type->array.is_static = parsed_array->is_static;
2444 array_type->array.is_variable = parsed_array->is_variable;
2445 array_type->array.size_expression = size_expression;
2447 if(size_expression != NULL &&
2448 is_constant_expression(size_expression)) {
2449 array_type->array.size_constant = true;
2450 array_type->array.size
2451 = fold_constant(size_expression);
2454 type_t *skipped_type = skip_typeref(type);
2455 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2456 errorf(HERE, "array of void is not allowed");
2457 type = type_error_type;
2465 type_t *hashed_type = typehash_insert(type);
2466 if(hashed_type != type) {
2467 /* the function type was constructed earlier freeing it here will
2468 * destroy other types... */
2469 if(iter->kind != CONSTRUCT_FUNCTION) {
2479 static declaration_t *parse_declarator(
2480 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2482 declaration_t *const declaration = allocate_declaration_zero();
2483 declaration->storage_class = specifiers->storage_class;
2484 declaration->modifiers = specifiers->decl_modifiers;
2485 declaration->is_inline = specifiers->is_inline;
2487 construct_type_t *construct_type
2488 = parse_inner_declarator(declaration, may_be_abstract);
2489 type_t *const type = specifiers->type;
2490 declaration->type = construct_declarator_type(construct_type, type);
2492 if(construct_type != NULL) {
2493 obstack_free(&temp_obst, construct_type);
2499 static type_t *parse_abstract_declarator(type_t *base_type)
2501 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2503 type_t *result = construct_declarator_type(construct_type, base_type);
2504 if(construct_type != NULL) {
2505 obstack_free(&temp_obst, construct_type);
2511 static declaration_t *append_declaration(declaration_t* const declaration)
2513 if (last_declaration != NULL) {
2514 last_declaration->next = declaration;
2516 scope->declarations = declaration;
2518 last_declaration = declaration;
2523 * Check if the declaration of main is suspicious. main should be a
2524 * function with external linkage, returning int, taking either zero
2525 * arguments, two, or three arguments of appropriate types, ie.
2527 * int main([ int argc, char **argv [, char **env ] ]).
2529 * @param decl the declaration to check
2530 * @param type the function type of the declaration
2532 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2534 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2535 warningf(decl->source_position, "'main' is normally a non-static function");
2537 if (skip_typeref(func_type->return_type) != type_int) {
2538 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2540 const function_parameter_t *parm = func_type->parameters;
2542 type_t *const first_type = parm->type;
2543 if (!types_compatible(skip_typeref(first_type), type_int)) {
2544 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2548 type_t *const second_type = parm->type;
2549 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2550 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2554 type_t *const third_type = parm->type;
2555 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2556 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2560 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2564 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2570 * Check if a symbol is the equal to "main".
2572 static bool is_sym_main(const symbol_t *const sym)
2574 return strcmp(sym->string, "main") == 0;
2577 static declaration_t *internal_record_declaration(
2578 declaration_t *const declaration,
2579 const bool is_function_definition)
2581 const symbol_t *const symbol = declaration->symbol;
2582 const namespace_t namespc = (namespace_t)declaration->namespc;
2584 type_t *const orig_type = declaration->type;
2585 type_t *const type = skip_typeref(orig_type);
2586 if (is_type_function(type) &&
2587 type->function.unspecified_parameters &&
2588 warning.strict_prototypes) {
2589 warningf(declaration->source_position,
2590 "function declaration '%#T' is not a prototype",
2591 orig_type, declaration->symbol);
2594 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2595 check_type_of_main(declaration, &type->function);
2598 assert(declaration->symbol != NULL);
2599 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2601 assert(declaration != previous_declaration);
2602 if (previous_declaration != NULL) {
2603 if (previous_declaration->parent_scope == scope) {
2604 /* can happen for K&R style declarations */
2605 if(previous_declaration->type == NULL) {
2606 previous_declaration->type = declaration->type;
2609 const type_t *prev_type = skip_typeref(previous_declaration->type);
2610 if (!types_compatible(type, prev_type)) {
2611 errorf(declaration->source_position,
2612 "declaration '%#T' is incompatible with "
2613 "previous declaration '%#T'",
2614 orig_type, symbol, previous_declaration->type, symbol);
2615 errorf(previous_declaration->source_position,
2616 "previous declaration of '%Y' was here", symbol);
2618 unsigned old_storage_class
2619 = previous_declaration->storage_class;
2620 unsigned new_storage_class = declaration->storage_class;
2622 if(is_type_incomplete(prev_type)) {
2623 previous_declaration->type = type;
2627 /* pretend no storage class means extern for function
2628 * declarations (except if the previous declaration is neither
2629 * none nor extern) */
2630 if (is_type_function(type)) {
2631 switch (old_storage_class) {
2632 case STORAGE_CLASS_NONE:
2633 old_storage_class = STORAGE_CLASS_EXTERN;
2635 case STORAGE_CLASS_EXTERN:
2636 if (is_function_definition) {
2637 if (warning.missing_prototypes &&
2638 prev_type->function.unspecified_parameters &&
2639 !is_sym_main(symbol)) {
2640 warningf(declaration->source_position,
2641 "no previous prototype for '%#T'",
2644 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2645 new_storage_class = STORAGE_CLASS_EXTERN;
2653 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2654 new_storage_class == STORAGE_CLASS_EXTERN) {
2655 warn_redundant_declaration:
2656 if (warning.redundant_decls) {
2657 warningf(declaration->source_position,
2658 "redundant declaration for '%Y'", symbol);
2659 warningf(previous_declaration->source_position,
2660 "previous declaration of '%Y' was here",
2663 } else if (current_function == NULL) {
2664 if (old_storage_class != STORAGE_CLASS_STATIC &&
2665 new_storage_class == STORAGE_CLASS_STATIC) {
2666 errorf(declaration->source_position,
2667 "static declaration of '%Y' follows non-static declaration",
2669 errorf(previous_declaration->source_position,
2670 "previous declaration of '%Y' was here", symbol);
2672 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2673 goto warn_redundant_declaration;
2675 if (new_storage_class == STORAGE_CLASS_NONE) {
2676 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2680 if (old_storage_class == new_storage_class) {
2681 errorf(declaration->source_position,
2682 "redeclaration of '%Y'", symbol);
2684 errorf(declaration->source_position,
2685 "redeclaration of '%Y' with different linkage",
2688 errorf(previous_declaration->source_position,
2689 "previous declaration of '%Y' was here", symbol);
2692 return previous_declaration;
2694 } else if (is_function_definition) {
2695 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2696 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2697 warningf(declaration->source_position,
2698 "no previous prototype for '%#T'", orig_type, symbol);
2699 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2700 warningf(declaration->source_position,
2701 "no previous declaration for '%#T'", orig_type,
2705 } else if (warning.missing_declarations &&
2706 scope == global_scope &&
2707 !is_type_function(type) && (
2708 declaration->storage_class == STORAGE_CLASS_NONE ||
2709 declaration->storage_class == STORAGE_CLASS_THREAD
2711 warningf(declaration->source_position,
2712 "no previous declaration for '%#T'", orig_type, symbol);
2715 assert(declaration->parent_scope == NULL);
2716 assert(scope != NULL);
2718 declaration->parent_scope = scope;
2720 environment_push(declaration);
2721 return append_declaration(declaration);
2724 static declaration_t *record_declaration(declaration_t *declaration)
2726 return internal_record_declaration(declaration, false);
2729 static declaration_t *record_function_definition(declaration_t *declaration)
2731 return internal_record_declaration(declaration, true);
2734 static void parser_error_multiple_definition(declaration_t *declaration,
2735 const source_position_t source_position)
2737 errorf(source_position, "multiple definition of symbol '%Y'",
2738 declaration->symbol);
2739 errorf(declaration->source_position,
2740 "this is the location of the previous definition.");
2743 static bool is_declaration_specifier(const token_t *token,
2744 bool only_type_specifiers)
2746 switch(token->type) {
2750 return is_typedef_symbol(token->v.symbol);
2752 case T___extension__:
2755 return !only_type_specifiers;
2762 static void parse_init_declarator_rest(declaration_t *declaration)
2766 type_t *orig_type = declaration->type;
2767 type_t *type = type = skip_typeref(orig_type);
2769 if(declaration->init.initializer != NULL) {
2770 parser_error_multiple_definition(declaration, token.source_position);
2773 initializer_t *initializer = parse_initializer(type);
2775 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
2776 * the array type size */
2777 if(is_type_array(type) && initializer != NULL) {
2778 array_type_t *array_type = &type->array;
2780 if(array_type->size_expression == NULL) {
2782 switch (initializer->kind) {
2783 case INITIALIZER_LIST: {
2785 size = initializer->list.len;
2789 case INITIALIZER_STRING: {
2790 size = initializer->string.string.size;
2794 case INITIALIZER_WIDE_STRING: {
2795 size = initializer->wide_string.string.size;
2800 panic("invalid initializer type");
2805 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
2806 cnst->base.type = type_size_t;
2807 cnst->conste.v.int_value = size;
2809 array_type->size_expression = cnst;
2810 array_type->size_constant = true;
2811 array_type->size = size;
2815 if(is_type_function(type)) {
2816 errorf(declaration->source_position,
2817 "initializers not allowed for function types at declator '%Y' (type '%T')",
2818 declaration->symbol, orig_type);
2820 declaration->init.initializer = initializer;
2824 /* parse rest of a declaration without any declarator */
2825 static void parse_anonymous_declaration_rest(
2826 const declaration_specifiers_t *specifiers,
2827 parsed_declaration_func finished_declaration)
2831 declaration_t *const declaration = allocate_declaration_zero();
2832 declaration->type = specifiers->type;
2833 declaration->storage_class = specifiers->storage_class;
2834 declaration->source_position = specifiers->source_position;
2836 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2837 warningf(declaration->source_position, "useless storage class in empty declaration");
2840 type_t *type = declaration->type;
2841 switch (type->kind) {
2842 case TYPE_COMPOUND_STRUCT:
2843 case TYPE_COMPOUND_UNION: {
2844 if (type->compound.declaration->symbol == NULL) {
2845 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2854 warningf(declaration->source_position, "empty declaration");
2858 finished_declaration(declaration);
2861 static void parse_declaration_rest(declaration_t *ndeclaration,
2862 const declaration_specifiers_t *specifiers,
2863 parsed_declaration_func finished_declaration)
2866 declaration_t *declaration = finished_declaration(ndeclaration);
2868 type_t *orig_type = declaration->type;
2869 type_t *type = skip_typeref(orig_type);
2871 if (type->kind != TYPE_FUNCTION &&
2872 declaration->is_inline &&
2873 is_type_valid(type)) {
2874 warningf(declaration->source_position,
2875 "variable '%Y' declared 'inline'\n", declaration->symbol);
2878 if(token.type == '=') {
2879 parse_init_declarator_rest(declaration);
2882 if(token.type != ',')
2886 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2891 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2893 symbol_t *symbol = declaration->symbol;
2894 if(symbol == NULL) {
2895 errorf(HERE, "anonymous declaration not valid as function parameter");
2898 namespace_t namespc = (namespace_t) declaration->namespc;
2899 if(namespc != NAMESPACE_NORMAL) {
2900 return record_declaration(declaration);
2903 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2904 if(previous_declaration == NULL ||
2905 previous_declaration->parent_scope != scope) {
2906 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2911 if(previous_declaration->type == NULL) {
2912 previous_declaration->type = declaration->type;
2913 previous_declaration->storage_class = declaration->storage_class;
2914 previous_declaration->parent_scope = scope;
2915 return previous_declaration;
2917 return record_declaration(declaration);
2921 static void parse_declaration(parsed_declaration_func finished_declaration)
2923 declaration_specifiers_t specifiers;
2924 memset(&specifiers, 0, sizeof(specifiers));
2925 parse_declaration_specifiers(&specifiers);
2927 if(token.type == ';') {
2928 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2930 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2931 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2935 static void parse_kr_declaration_list(declaration_t *declaration)
2937 type_t *type = skip_typeref(declaration->type);
2938 if(!is_type_function(type))
2941 if(!type->function.kr_style_parameters)
2944 /* push function parameters */
2945 int top = environment_top();
2946 scope_t *last_scope = scope;
2947 set_scope(&declaration->scope);
2949 declaration_t *parameter = declaration->scope.declarations;
2950 for( ; parameter != NULL; parameter = parameter->next) {
2951 assert(parameter->parent_scope == NULL);
2952 parameter->parent_scope = scope;
2953 environment_push(parameter);
2956 /* parse declaration list */
2957 while(is_declaration_specifier(&token, false)) {
2958 parse_declaration(finished_kr_declaration);
2961 /* pop function parameters */
2962 assert(scope == &declaration->scope);
2963 set_scope(last_scope);
2964 environment_pop_to(top);
2966 /* update function type */
2967 type_t *new_type = duplicate_type(type);
2968 new_type->function.kr_style_parameters = false;
2970 function_parameter_t *parameters = NULL;
2971 function_parameter_t *last_parameter = NULL;
2973 declaration_t *parameter_declaration = declaration->scope.declarations;
2974 for( ; parameter_declaration != NULL;
2975 parameter_declaration = parameter_declaration->next) {
2976 type_t *parameter_type = parameter_declaration->type;
2977 if(parameter_type == NULL) {
2979 errorf(HERE, "no type specified for function parameter '%Y'",
2980 parameter_declaration->symbol);
2982 if (warning.implicit_int) {
2983 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
2984 parameter_declaration->symbol);
2986 parameter_type = type_int;
2987 parameter_declaration->type = parameter_type;
2991 semantic_parameter(parameter_declaration);
2992 parameter_type = parameter_declaration->type;
2994 function_parameter_t *function_parameter
2995 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
2996 memset(function_parameter, 0, sizeof(function_parameter[0]));
2998 function_parameter->type = parameter_type;
2999 if(last_parameter != NULL) {
3000 last_parameter->next = function_parameter;
3002 parameters = function_parameter;
3004 last_parameter = function_parameter;
3006 new_type->function.parameters = parameters;
3008 type = typehash_insert(new_type);
3009 if(type != new_type) {
3010 obstack_free(type_obst, new_type);
3013 declaration->type = type;
3016 static bool first_err = true;
3019 * When called with first_err set, prints the name of the current function,
3022 static void print_in_function(void) {
3025 diagnosticf("%s: In function '%Y':\n",
3026 current_function->source_position.input_name,
3027 current_function->symbol);
3032 * Check if all labels are defined in the current function.
3033 * Check if all labels are used in the current function.
3035 static void check_labels(void)
3037 for (const goto_statement_t *goto_statement = goto_first;
3038 goto_statement != NULL;
3039 goto_statement = goto_statement->next) {
3040 declaration_t *label = goto_statement->label;
3043 if (label->source_position.input_name == NULL) {
3044 print_in_function();
3045 errorf(goto_statement->base.source_position,
3046 "label '%Y' used but not defined", label->symbol);
3049 goto_first = goto_last = NULL;
3051 if (warning.unused_label) {
3052 for (const label_statement_t *label_statement = label_first;
3053 label_statement != NULL;
3054 label_statement = label_statement->next) {
3055 const declaration_t *label = label_statement->label;
3057 if (! label->used) {
3058 print_in_function();
3059 warningf(label_statement->base.source_position,
3060 "label '%Y' defined but not used", label->symbol);
3064 label_first = label_last = NULL;
3068 * Check declarations of current_function for unused entities.
3070 static void check_declarations(void)
3072 if (warning.unused_parameter) {
3073 const scope_t *scope = ¤t_function->scope;
3075 const declaration_t *parameter = scope->declarations;
3076 for (; parameter != NULL; parameter = parameter->next) {
3077 if (! parameter->used) {
3078 print_in_function();
3079 warningf(parameter->source_position,
3080 "unused parameter '%Y'", parameter->symbol);
3084 if (warning.unused_variable) {
3088 static void parse_external_declaration(void)
3090 /* function-definitions and declarations both start with declaration
3092 declaration_specifiers_t specifiers;
3093 memset(&specifiers, 0, sizeof(specifiers));
3094 parse_declaration_specifiers(&specifiers);
3096 /* must be a declaration */
3097 if(token.type == ';') {
3098 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3102 /* declarator is common to both function-definitions and declarations */
3103 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3105 /* must be a declaration */
3106 if(token.type == ',' || token.type == '=' || token.type == ';') {
3107 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3111 /* must be a function definition */
3112 parse_kr_declaration_list(ndeclaration);
3114 if(token.type != '{') {
3115 parse_error_expected("while parsing function definition", '{', 0);
3120 type_t *type = ndeclaration->type;
3122 /* note that we don't skip typerefs: the standard doesn't allow them here
3123 * (so we can't use is_type_function here) */
3124 if(type->kind != TYPE_FUNCTION) {
3125 if (is_type_valid(type)) {
3126 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3127 type, ndeclaration->symbol);
3133 /* § 6.7.5.3 (14) a function definition with () means no
3134 * parameters (and not unspecified parameters) */
3135 if(type->function.unspecified_parameters) {
3136 type_t *duplicate = duplicate_type(type);
3137 duplicate->function.unspecified_parameters = false;
3139 type = typehash_insert(duplicate);
3140 if(type != duplicate) {
3141 obstack_free(type_obst, duplicate);
3143 ndeclaration->type = type;
3146 declaration_t *const declaration = record_function_definition(ndeclaration);
3147 if(ndeclaration != declaration) {
3148 declaration->scope = ndeclaration->scope;
3150 type = skip_typeref(declaration->type);
3152 /* push function parameters and switch scope */
3153 int top = environment_top();
3154 scope_t *last_scope = scope;
3155 set_scope(&declaration->scope);
3157 declaration_t *parameter = declaration->scope.declarations;
3158 for( ; parameter != NULL; parameter = parameter->next) {
3159 if(parameter->parent_scope == &ndeclaration->scope) {
3160 parameter->parent_scope = scope;
3162 assert(parameter->parent_scope == NULL
3163 || parameter->parent_scope == scope);
3164 parameter->parent_scope = scope;
3165 environment_push(parameter);
3168 if(declaration->init.statement != NULL) {
3169 parser_error_multiple_definition(declaration, token.source_position);
3171 goto end_of_parse_external_declaration;
3173 /* parse function body */
3174 int label_stack_top = label_top();
3175 declaration_t *old_current_function = current_function;
3176 current_function = declaration;
3178 declaration->init.statement = parse_compound_statement();
3181 check_declarations();
3183 assert(current_function == declaration);
3184 current_function = old_current_function;
3185 label_pop_to(label_stack_top);
3188 end_of_parse_external_declaration:
3189 assert(scope == &declaration->scope);
3190 set_scope(last_scope);
3191 environment_pop_to(top);
3194 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3195 source_position_t source_position)
3197 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3198 type->bitfield.base = base;
3199 type->bitfield.size = size;
3204 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3207 declaration_t *iter = compound_declaration->scope.declarations;
3208 for( ; iter != NULL; iter = iter->next) {
3209 if(iter->namespc != NAMESPACE_NORMAL)
3212 if(iter->symbol == NULL) {
3213 type_t *type = skip_typeref(iter->type);
3214 if(is_type_compound(type)) {
3215 declaration_t *result
3216 = find_compound_entry(type->compound.declaration, symbol);
3223 if(iter->symbol == symbol) {
3231 static void parse_compound_declarators(declaration_t *struct_declaration,
3232 const declaration_specifiers_t *specifiers)
3234 declaration_t *last_declaration = struct_declaration->scope.declarations;
3235 if(last_declaration != NULL) {
3236 while(last_declaration->next != NULL) {
3237 last_declaration = last_declaration->next;
3242 declaration_t *declaration;
3244 if(token.type == ':') {
3245 source_position_t source_position = HERE;
3248 type_t *base_type = specifiers->type;
3249 expression_t *size = parse_constant_expression();
3251 if(!is_type_integer(skip_typeref(base_type))) {
3252 errorf(HERE, "bitfield base type '%T' is not an integer type",
3256 type_t *type = make_bitfield_type(base_type, size, source_position);
3258 declaration = allocate_declaration_zero();
3259 declaration->namespc = NAMESPACE_NORMAL;
3260 declaration->storage_class = STORAGE_CLASS_NONE;
3261 declaration->source_position = source_position;
3262 declaration->modifiers = specifiers->decl_modifiers;
3263 declaration->type = type;
3265 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3267 type_t *orig_type = declaration->type;
3268 type_t *type = skip_typeref(orig_type);
3270 if(token.type == ':') {
3271 source_position_t source_position = HERE;
3273 expression_t *size = parse_constant_expression();
3275 if(!is_type_integer(type)) {
3276 errorf(HERE, "bitfield base type '%T' is not an "
3277 "integer type", orig_type);
3280 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3281 declaration->type = bitfield_type;
3283 /* TODO we ignore arrays for now... what is missing is a check
3284 * that they're at the end of the struct */
3285 if(is_type_incomplete(type) && !is_type_array(type)) {
3287 "compound member '%Y' has incomplete type '%T'",
3288 declaration->symbol, orig_type);
3289 } else if(is_type_function(type)) {
3290 errorf(HERE, "compound member '%Y' must not have function "
3291 "type '%T'", declaration->symbol, orig_type);
3296 /* make sure we don't define a symbol multiple times */
3297 symbol_t *symbol = declaration->symbol;
3298 if(symbol != NULL) {
3299 declaration_t *prev_decl
3300 = find_compound_entry(struct_declaration, symbol);
3302 if(prev_decl != NULL) {
3303 assert(prev_decl->symbol == symbol);
3304 errorf(declaration->source_position,
3305 "multiple declarations of symbol '%Y'", symbol);
3306 errorf(prev_decl->source_position,
3307 "previous declaration of '%Y' was here", symbol);
3311 /* append declaration */
3312 if(last_declaration != NULL) {
3313 last_declaration->next = declaration;
3315 struct_declaration->scope.declarations = declaration;
3317 last_declaration = declaration;
3319 if(token.type != ',')
3326 static void parse_compound_type_entries(declaration_t *compound_declaration)
3330 while(token.type != '}' && token.type != T_EOF) {
3331 declaration_specifiers_t specifiers;
3332 memset(&specifiers, 0, sizeof(specifiers));
3333 parse_declaration_specifiers(&specifiers);
3335 parse_compound_declarators(compound_declaration, &specifiers);
3337 if(token.type == T_EOF) {
3338 errorf(HERE, "EOF while parsing struct");
3343 static type_t *parse_typename(void)
3345 declaration_specifiers_t specifiers;
3346 memset(&specifiers, 0, sizeof(specifiers));
3347 parse_declaration_specifiers(&specifiers);
3348 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3349 /* TODO: improve error message, user does probably not know what a
3350 * storage class is...
3352 errorf(HERE, "typename may not have a storage class");
3355 type_t *result = parse_abstract_declarator(specifiers.type);
3363 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3364 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3365 expression_t *left);
3367 typedef struct expression_parser_function_t expression_parser_function_t;
3368 struct expression_parser_function_t {
3369 unsigned precedence;
3370 parse_expression_function parser;
3371 unsigned infix_precedence;
3372 parse_expression_infix_function infix_parser;
3375 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3378 * Creates a new invalid expression.
3380 static expression_t *create_invalid_expression(void)
3382 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3383 expression->base.source_position = token.source_position;
3388 * Prints an error message if an expression was expected but not read
3390 static expression_t *expected_expression_error(void)
3392 /* skip the error message if the error token was read */
3393 if (token.type != T_ERROR) {
3394 errorf(HERE, "expected expression, got token '%K'", &token);
3398 return create_invalid_expression();
3402 * Parse a string constant.
3404 static expression_t *parse_string_const(void)
3407 if (token.type == T_STRING_LITERAL) {
3408 string_t res = token.v.string;
3410 while (token.type == T_STRING_LITERAL) {
3411 res = concat_strings(&res, &token.v.string);
3414 if (token.type != T_WIDE_STRING_LITERAL) {
3415 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3416 cnst->base.type = type_char_ptr;
3417 cnst->string.value = res;
3421 wres = concat_string_wide_string(&res, &token.v.wide_string);
3423 wres = token.v.wide_string;
3428 switch (token.type) {
3429 case T_WIDE_STRING_LITERAL:
3430 wres = concat_wide_strings(&wres, &token.v.wide_string);
3433 case T_STRING_LITERAL:
3434 wres = concat_wide_string_string(&wres, &token.v.string);
3438 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3439 cnst->base.type = type_wchar_t_ptr;
3440 cnst->wide_string.value = wres;
3449 * Parse an integer constant.
3451 static expression_t *parse_int_const(void)
3453 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3454 cnst->base.source_position = HERE;
3455 cnst->base.type = token.datatype;
3456 cnst->conste.v.int_value = token.v.intvalue;
3464 * Parse a character constant.
3466 static expression_t *parse_char_const(void)
3468 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3469 cnst->base.source_position = HERE;
3470 cnst->base.type = token.datatype;
3471 cnst->conste.v.chars.begin = token.v.string.begin;
3472 cnst->conste.v.chars.size = token.v.string.size;
3474 if (cnst->conste.v.chars.size != 1) {
3475 if (warning.multichar && (c_mode & _GNUC)) {
3477 warningf(HERE, "multi-character character constant");
3479 errorf(HERE, "more than 1 characters in character constant");
3488 * Parse a float constant.
3490 static expression_t *parse_float_const(void)
3492 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3493 cnst->base.type = token.datatype;
3494 cnst->conste.v.float_value = token.v.floatvalue;
3501 static declaration_t *create_implicit_function(symbol_t *symbol,
3502 const source_position_t source_position)
3504 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3505 ntype->function.return_type = type_int;
3506 ntype->function.unspecified_parameters = true;
3508 type_t *type = typehash_insert(ntype);
3513 declaration_t *const declaration = allocate_declaration_zero();
3514 declaration->storage_class = STORAGE_CLASS_EXTERN;
3515 declaration->type = type;
3516 declaration->symbol = symbol;
3517 declaration->source_position = source_position;
3518 declaration->parent_scope = global_scope;
3520 scope_t *old_scope = scope;
3521 set_scope(global_scope);
3523 environment_push(declaration);
3524 /* prepends the declaration to the global declarations list */
3525 declaration->next = scope->declarations;
3526 scope->declarations = declaration;
3528 assert(scope == global_scope);
3529 set_scope(old_scope);
3535 * Creates a return_type (func)(argument_type) function type if not
3538 * @param return_type the return type
3539 * @param argument_type the argument type
3541 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3543 function_parameter_t *parameter
3544 = obstack_alloc(type_obst, sizeof(parameter[0]));
3545 memset(parameter, 0, sizeof(parameter[0]));
3546 parameter->type = argument_type;
3548 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3549 type->function.return_type = return_type;
3550 type->function.parameters = parameter;
3552 type_t *result = typehash_insert(type);
3553 if(result != type) {
3561 * Creates a function type for some function like builtins.
3563 * @param symbol the symbol describing the builtin
3565 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3567 switch(symbol->ID) {
3568 case T___builtin_alloca:
3569 return make_function_1_type(type_void_ptr, type_size_t);
3570 case T___builtin_nan:
3571 return make_function_1_type(type_double, type_char_ptr);
3572 case T___builtin_nanf:
3573 return make_function_1_type(type_float, type_char_ptr);
3574 case T___builtin_nand:
3575 return make_function_1_type(type_long_double, type_char_ptr);
3576 case T___builtin_va_end:
3577 return make_function_1_type(type_void, type_valist);
3579 panic("not implemented builtin symbol found");
3584 * Performs automatic type cast as described in § 6.3.2.1.
3586 * @param orig_type the original type
3588 static type_t *automatic_type_conversion(type_t *orig_type)
3590 type_t *type = skip_typeref(orig_type);
3591 if(is_type_array(type)) {
3592 array_type_t *array_type = &type->array;
3593 type_t *element_type = array_type->element_type;
3594 unsigned qualifiers = array_type->type.qualifiers;
3596 return make_pointer_type(element_type, qualifiers);
3599 if(is_type_function(type)) {
3600 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3607 * reverts the automatic casts of array to pointer types and function
3608 * to function-pointer types as defined § 6.3.2.1
3610 type_t *revert_automatic_type_conversion(const expression_t *expression)
3612 switch (expression->kind) {
3613 case EXPR_REFERENCE: return expression->reference.declaration->type;
3614 case EXPR_SELECT: return expression->select.compound_entry->type;
3616 case EXPR_UNARY_DEREFERENCE: {
3617 const expression_t *const value = expression->unary.value;
3618 type_t *const type = skip_typeref(value->base.type);
3619 assert(is_type_pointer(type));
3620 return type->pointer.points_to;
3623 case EXPR_BUILTIN_SYMBOL:
3624 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3626 case EXPR_ARRAY_ACCESS: {
3627 const expression_t *array_ref = expression->array_access.array_ref;
3628 type_t *type_left = skip_typeref(array_ref->base.type);
3629 if (!is_type_valid(type_left))
3631 assert(is_type_pointer(type_left));
3632 return type_left->pointer.points_to;
3638 return expression->base.type;
3641 static expression_t *parse_reference(void)
3643 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3645 reference_expression_t *ref = &expression->reference;
3646 ref->symbol = token.v.symbol;
3648 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3650 source_position_t source_position = token.source_position;
3653 if(declaration == NULL) {
3654 if (! strict_mode && token.type == '(') {
3655 /* an implicitly defined function */
3656 if (warning.implicit_function_declaration) {
3657 warningf(HERE, "implicit declaration of function '%Y'",
3661 declaration = create_implicit_function(ref->symbol,
3664 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3665 return create_invalid_expression();
3669 type_t *type = declaration->type;
3671 /* we always do the auto-type conversions; the & and sizeof parser contains
3672 * code to revert this! */
3673 type = automatic_type_conversion(type);
3675 ref->declaration = declaration;
3676 ref->base.type = type;
3678 /* this declaration is used */
3679 declaration->used = true;
3684 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3688 /* TODO check if explicit cast is allowed and issue warnings/errors */
3691 static expression_t *parse_compound_literal(type_t *type)
3693 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3695 expression->compound_literal.type = type;
3696 expression->compound_literal.initializer = parse_initializer(type);
3697 expression->base.type = automatic_type_conversion(type);
3702 static expression_t *parse_cast(void)
3704 source_position_t source_position = token.source_position;
3706 type_t *type = parse_typename();
3710 if(token.type == '{') {
3711 return parse_compound_literal(type);
3714 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3715 cast->base.source_position = source_position;
3717 expression_t *value = parse_sub_expression(20);
3719 check_cast_allowed(value, type);
3721 cast->base.type = type;
3722 cast->unary.value = value;
3727 static expression_t *parse_statement_expression(void)
3729 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3731 statement_t *statement = parse_compound_statement();
3732 expression->statement.statement = statement;
3733 expression->base.source_position = statement->base.source_position;
3735 /* find last statement and use its type */
3736 type_t *type = type_void;
3737 const statement_t *stmt = statement->compound.statements;
3739 while (stmt->base.next != NULL)
3740 stmt = stmt->base.next;
3742 if (stmt->kind == STATEMENT_EXPRESSION) {
3743 type = stmt->expression.expression->base.type;
3746 warningf(expression->base.source_position, "empty statement expression ({})");
3748 expression->base.type = type;
3755 static expression_t *parse_brace_expression(void)
3759 switch(token.type) {
3761 /* gcc extension: a statement expression */
3762 return parse_statement_expression();
3766 return parse_cast();
3768 if(is_typedef_symbol(token.v.symbol)) {
3769 return parse_cast();
3773 expression_t *result = parse_expression();
3779 static expression_t *parse_function_keyword(void)
3784 if (current_function == NULL) {
3785 errorf(HERE, "'__func__' used outside of a function");
3788 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3789 expression->base.type = type_char_ptr;
3794 static expression_t *parse_pretty_function_keyword(void)
3796 eat(T___PRETTY_FUNCTION__);
3799 if (current_function == NULL) {
3800 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3803 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3804 expression->base.type = type_char_ptr;
3809 static designator_t *parse_designator(void)
3811 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3812 result->source_position = HERE;
3814 if(token.type != T_IDENTIFIER) {
3815 parse_error_expected("while parsing member designator",
3820 result->symbol = token.v.symbol;
3823 designator_t *last_designator = result;
3825 if(token.type == '.') {
3827 if(token.type != T_IDENTIFIER) {
3828 parse_error_expected("while parsing member designator",
3833 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3834 designator->source_position = HERE;
3835 designator->symbol = token.v.symbol;
3838 last_designator->next = designator;
3839 last_designator = designator;
3842 if(token.type == '[') {
3844 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3845 designator->source_position = HERE;
3846 designator->array_index = parse_expression();
3847 if(designator->array_index == NULL) {
3853 last_designator->next = designator;
3854 last_designator = designator;
3863 static expression_t *parse_offsetof(void)
3865 eat(T___builtin_offsetof);
3867 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3868 expression->base.type = type_size_t;
3871 type_t *type = parse_typename();
3873 designator_t *designator = parse_designator();
3876 expression->offsetofe.type = type;
3877 expression->offsetofe.designator = designator;
3880 memset(&path, 0, sizeof(path));
3881 path.top_type = type;
3882 path.path = NEW_ARR_F(type_path_entry_t, 0);
3884 descend_into_subtype(&path);
3886 if(!walk_designator(&path, designator, true)) {
3887 return create_invalid_expression();
3890 DEL_ARR_F(path.path);
3895 static expression_t *parse_va_start(void)
3897 eat(T___builtin_va_start);
3899 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3902 expression->va_starte.ap = parse_assignment_expression();
3904 expression_t *const expr = parse_assignment_expression();
3905 if (expr->kind == EXPR_REFERENCE) {
3906 declaration_t *const decl = expr->reference.declaration;
3908 return create_invalid_expression();
3909 if (decl->parent_scope == ¤t_function->scope &&
3910 decl->next == NULL) {
3911 expression->va_starte.parameter = decl;
3916 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3918 return create_invalid_expression();
3921 static expression_t *parse_va_arg(void)
3923 eat(T___builtin_va_arg);
3925 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3928 expression->va_arge.ap = parse_assignment_expression();
3930 expression->base.type = parse_typename();
3936 static expression_t *parse_builtin_symbol(void)
3938 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3940 symbol_t *symbol = token.v.symbol;
3942 expression->builtin_symbol.symbol = symbol;
3945 type_t *type = get_builtin_symbol_type(symbol);
3946 type = automatic_type_conversion(type);
3948 expression->base.type = type;
3952 static expression_t *parse_builtin_constant(void)
3954 eat(T___builtin_constant_p);
3956 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
3959 expression->builtin_constant.value = parse_assignment_expression();
3961 expression->base.type = type_int;
3966 static expression_t *parse_builtin_prefetch(void)
3968 eat(T___builtin_prefetch);
3970 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
3973 expression->builtin_prefetch.adr = parse_assignment_expression();
3974 if (token.type == ',') {
3976 expression->builtin_prefetch.rw = parse_assignment_expression();
3978 if (token.type == ',') {
3980 expression->builtin_prefetch.locality = parse_assignment_expression();
3983 expression->base.type = type_void;
3988 static expression_t *parse_compare_builtin(void)
3990 expression_t *expression;
3992 switch(token.type) {
3993 case T___builtin_isgreater:
3994 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
3996 case T___builtin_isgreaterequal:
3997 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
3999 case T___builtin_isless:
4000 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4002 case T___builtin_islessequal:
4003 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4005 case T___builtin_islessgreater:
4006 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4008 case T___builtin_isunordered:
4009 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4012 panic("invalid compare builtin found");
4015 expression->base.source_position = HERE;
4019 expression->binary.left = parse_assignment_expression();
4021 expression->binary.right = parse_assignment_expression();
4024 type_t *const orig_type_left = expression->binary.left->base.type;
4025 type_t *const orig_type_right = expression->binary.right->base.type;
4027 type_t *const type_left = skip_typeref(orig_type_left);
4028 type_t *const type_right = skip_typeref(orig_type_right);
4029 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4030 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4031 type_error_incompatible("invalid operands in comparison",
4032 expression->base.source_position, orig_type_left, orig_type_right);
4035 semantic_comparison(&expression->binary);
4041 static expression_t *parse_builtin_expect(void)
4043 eat(T___builtin_expect);
4045 expression_t *expression
4046 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4049 expression->binary.left = parse_assignment_expression();
4051 expression->binary.right = parse_constant_expression();
4054 expression->base.type = expression->binary.left->base.type;
4059 static expression_t *parse_assume(void) {
4062 expression_t *expression
4063 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4066 expression->unary.value = parse_assignment_expression();
4069 expression->base.type = type_void;
4073 static expression_t *parse_primary_expression(void)
4075 switch (token.type) {
4076 case T_INTEGER: return parse_int_const();
4077 case T_CHARS: return parse_char_const();
4078 case T_FLOATINGPOINT: return parse_float_const();
4079 case T_STRING_LITERAL:
4080 case T_WIDE_STRING_LITERAL: return parse_string_const();
4081 case T_IDENTIFIER: return parse_reference();
4082 case T___FUNCTION__:
4083 case T___func__: return parse_function_keyword();
4084 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4085 case T___builtin_offsetof: return parse_offsetof();
4086 case T___builtin_va_start: return parse_va_start();
4087 case T___builtin_va_arg: return parse_va_arg();
4088 case T___builtin_expect: return parse_builtin_expect();
4089 case T___builtin_alloca:
4090 case T___builtin_nan:
4091 case T___builtin_nand:
4092 case T___builtin_nanf:
4093 case T___builtin_va_end: return parse_builtin_symbol();
4094 case T___builtin_isgreater:
4095 case T___builtin_isgreaterequal:
4096 case T___builtin_isless:
4097 case T___builtin_islessequal:
4098 case T___builtin_islessgreater:
4099 case T___builtin_isunordered: return parse_compare_builtin();
4100 case T___builtin_constant_p: return parse_builtin_constant();
4101 case T___builtin_prefetch: return parse_builtin_prefetch();
4102 case T_assume: return parse_assume();
4104 case '(': return parse_brace_expression();
4107 errorf(HERE, "unexpected token %K, expected an expression", &token);
4110 return create_invalid_expression();
4114 * Check if the expression has the character type and issue a warning then.
4116 static void check_for_char_index_type(const expression_t *expression) {
4117 type_t *const type = expression->base.type;
4118 const type_t *const base_type = skip_typeref(type);
4120 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4121 warning.char_subscripts) {
4122 warningf(expression->base.source_position,
4123 "array subscript has type '%T'", type);
4127 static expression_t *parse_array_expression(unsigned precedence,
4134 expression_t *inside = parse_expression();
4136 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4138 array_access_expression_t *array_access = &expression->array_access;
4140 type_t *const orig_type_left = left->base.type;
4141 type_t *const orig_type_inside = inside->base.type;
4143 type_t *const type_left = skip_typeref(orig_type_left);
4144 type_t *const type_inside = skip_typeref(orig_type_inside);
4146 type_t *return_type;
4147 if (is_type_pointer(type_left)) {
4148 return_type = type_left->pointer.points_to;
4149 array_access->array_ref = left;
4150 array_access->index = inside;
4151 check_for_char_index_type(inside);
4152 } else if (is_type_pointer(type_inside)) {
4153 return_type = type_inside->pointer.points_to;
4154 array_access->array_ref = inside;
4155 array_access->index = left;
4156 array_access->flipped = true;
4157 check_for_char_index_type(left);
4159 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4161 "array access on object with non-pointer types '%T', '%T'",
4162 orig_type_left, orig_type_inside);
4164 return_type = type_error_type;
4165 array_access->array_ref = create_invalid_expression();
4168 if(token.type != ']') {
4169 parse_error_expected("Problem while parsing array access", ']', 0);
4174 return_type = automatic_type_conversion(return_type);
4175 expression->base.type = return_type;
4180 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4182 expression_t *tp_expression = allocate_expression_zero(kind);
4183 tp_expression->base.type = type_size_t;
4185 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4187 tp_expression->typeprop.type = parse_typename();
4190 expression_t *expression = parse_sub_expression(precedence);
4191 expression->base.type = revert_automatic_type_conversion(expression);
4193 tp_expression->typeprop.type = expression->base.type;
4194 tp_expression->typeprop.tp_expression = expression;
4197 return tp_expression;
4200 static expression_t *parse_sizeof(unsigned precedence)
4203 return parse_typeprop(EXPR_SIZEOF, precedence);
4206 static expression_t *parse_alignof(unsigned precedence)
4209 return parse_typeprop(EXPR_SIZEOF, precedence);
4212 static expression_t *parse_select_expression(unsigned precedence,
4213 expression_t *compound)
4216 assert(token.type == '.' || token.type == T_MINUSGREATER);
4218 bool is_pointer = (token.type == T_MINUSGREATER);
4221 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4222 select->select.compound = compound;
4224 if(token.type != T_IDENTIFIER) {
4225 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4228 symbol_t *symbol = token.v.symbol;
4229 select->select.symbol = symbol;
4232 type_t *const orig_type = compound->base.type;
4233 type_t *const type = skip_typeref(orig_type);
4235 type_t *type_left = type;
4237 if (!is_type_pointer(type)) {
4238 if (is_type_valid(type)) {
4239 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4241 return create_invalid_expression();
4243 type_left = type->pointer.points_to;
4245 type_left = skip_typeref(type_left);
4247 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4248 type_left->kind != TYPE_COMPOUND_UNION) {
4249 if (is_type_valid(type_left)) {
4250 errorf(HERE, "request for member '%Y' in something not a struct or "
4251 "union, but '%T'", symbol, type_left);
4253 return create_invalid_expression();
4256 declaration_t *const declaration = type_left->compound.declaration;
4258 if(!declaration->init.is_defined) {
4259 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4261 return create_invalid_expression();
4264 declaration_t *iter = find_compound_entry(declaration, symbol);
4266 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4267 return create_invalid_expression();
4270 /* we always do the auto-type conversions; the & and sizeof parser contains
4271 * code to revert this! */
4272 type_t *expression_type = automatic_type_conversion(iter->type);
4274 select->select.compound_entry = iter;
4275 select->base.type = expression_type;
4277 if(expression_type->kind == TYPE_BITFIELD) {
4278 expression_t *extract
4279 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4280 extract->unary.value = select;
4281 extract->base.type = expression_type->bitfield.base;
4290 * Parse a call expression, ie. expression '( ... )'.
4292 * @param expression the function address
4294 static expression_t *parse_call_expression(unsigned precedence,
4295 expression_t *expression)
4298 expression_t *result = allocate_expression_zero(EXPR_CALL);
4300 call_expression_t *call = &result->call;
4301 call->function = expression;
4303 type_t *const orig_type = expression->base.type;
4304 type_t *const type = skip_typeref(orig_type);
4306 function_type_t *function_type = NULL;
4307 if (is_type_pointer(type)) {
4308 type_t *const to_type = skip_typeref(type->pointer.points_to);
4310 if (is_type_function(to_type)) {
4311 function_type = &to_type->function;
4312 call->base.type = function_type->return_type;
4316 if (function_type == NULL && is_type_valid(type)) {
4317 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4320 /* parse arguments */
4323 if(token.type != ')') {
4324 call_argument_t *last_argument = NULL;
4327 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4329 argument->expression = parse_assignment_expression();
4330 if(last_argument == NULL) {
4331 call->arguments = argument;
4333 last_argument->next = argument;
4335 last_argument = argument;
4337 if(token.type != ',')
4344 if(function_type != NULL) {
4345 function_parameter_t *parameter = function_type->parameters;
4346 call_argument_t *argument = call->arguments;
4347 for( ; parameter != NULL && argument != NULL;
4348 parameter = parameter->next, argument = argument->next) {
4349 type_t *expected_type = parameter->type;
4350 /* TODO report scope in error messages */
4351 expression_t *const arg_expr = argument->expression;
4352 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4353 if (res_type == NULL) {
4354 /* TODO improve error message */
4355 errorf(arg_expr->base.source_position,
4356 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4357 arg_expr, arg_expr->base.type, expected_type);
4359 argument->expression = create_implicit_cast(argument->expression, expected_type);
4362 /* too few parameters */
4363 if(parameter != NULL) {
4364 errorf(HERE, "too few arguments to function '%E'", expression);
4365 } else if(argument != NULL) {
4366 /* too many parameters */
4367 if(!function_type->variadic
4368 && !function_type->unspecified_parameters) {
4369 errorf(HERE, "too many arguments to function '%E'", expression);
4371 /* do default promotion */
4372 for( ; argument != NULL; argument = argument->next) {
4373 type_t *type = argument->expression->base.type;
4375 type = skip_typeref(type);
4376 if(is_type_integer(type)) {
4377 type = promote_integer(type);
4378 } else if(type == type_float) {
4382 argument->expression
4383 = create_implicit_cast(argument->expression, type);
4386 check_format(&result->call);
4389 check_format(&result->call);
4396 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4398 static bool same_compound_type(const type_t *type1, const type_t *type2)
4401 is_type_compound(type1) &&
4402 type1->kind == type2->kind &&
4403 type1->compound.declaration == type2->compound.declaration;
4407 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4409 * @param expression the conditional expression
4411 static expression_t *parse_conditional_expression(unsigned precedence,
4412 expression_t *expression)
4416 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4418 conditional_expression_t *conditional = &result->conditional;
4419 conditional->condition = expression;
4422 type_t *const condition_type_orig = expression->base.type;
4423 type_t *const condition_type = skip_typeref(condition_type_orig);
4424 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4425 type_error("expected a scalar type in conditional condition",
4426 expression->base.source_position, condition_type_orig);
4429 expression_t *true_expression = parse_expression();
4431 expression_t *false_expression = parse_sub_expression(precedence);
4433 type_t *const orig_true_type = true_expression->base.type;
4434 type_t *const orig_false_type = false_expression->base.type;
4435 type_t *const true_type = skip_typeref(orig_true_type);
4436 type_t *const false_type = skip_typeref(orig_false_type);
4439 type_t *result_type;
4440 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4441 result_type = semantic_arithmetic(true_type, false_type);
4443 true_expression = create_implicit_cast(true_expression, result_type);
4444 false_expression = create_implicit_cast(false_expression, result_type);
4446 conditional->true_expression = true_expression;
4447 conditional->false_expression = false_expression;
4448 conditional->base.type = result_type;
4449 } else if (same_compound_type(true_type, false_type) || (
4450 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4451 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4453 /* just take 1 of the 2 types */
4454 result_type = true_type;
4455 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4456 && pointers_compatible(true_type, false_type)) {
4458 result_type = true_type;
4459 } else if (is_type_pointer(true_type)
4460 && is_null_pointer_constant(false_expression)) {
4461 result_type = true_type;
4462 } else if (is_type_pointer(false_type)
4463 && is_null_pointer_constant(true_expression)) {
4464 result_type = false_type;
4466 /* TODO: one pointer to void*, other some pointer */
4468 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4469 type_error_incompatible("while parsing conditional",
4470 expression->base.source_position, true_type,
4473 result_type = type_error_type;
4476 conditional->true_expression
4477 = create_implicit_cast(true_expression, result_type);
4478 conditional->false_expression
4479 = create_implicit_cast(false_expression, result_type);
4480 conditional->base.type = result_type;
4485 * Parse an extension expression.
4487 static expression_t *parse_extension(unsigned precedence)
4489 eat(T___extension__);
4491 /* TODO enable extensions */
4492 expression_t *expression = parse_sub_expression(precedence);
4493 /* TODO disable extensions */
4497 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4499 eat(T___builtin_classify_type);
4501 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4502 result->base.type = type_int;
4505 expression_t *expression = parse_sub_expression(precedence);
4507 result->classify_type.type_expression = expression;
4512 static void semantic_incdec(unary_expression_t *expression)
4514 type_t *const orig_type = expression->value->base.type;
4515 type_t *const type = skip_typeref(orig_type);
4516 /* TODO !is_type_real && !is_type_pointer */
4517 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4518 if (is_type_valid(type)) {
4519 /* TODO: improve error message */
4520 errorf(HERE, "operation needs an arithmetic or pointer type");
4525 expression->base.type = orig_type;
4528 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4530 type_t *const orig_type = expression->value->base.type;
4531 type_t *const type = skip_typeref(orig_type);
4532 if(!is_type_arithmetic(type)) {
4533 if (is_type_valid(type)) {
4534 /* TODO: improve error message */
4535 errorf(HERE, "operation needs an arithmetic type");
4540 expression->base.type = orig_type;
4543 static void semantic_unexpr_scalar(unary_expression_t *expression)
4545 type_t *const orig_type = expression->value->base.type;
4546 type_t *const type = skip_typeref(orig_type);
4547 if (!is_type_scalar(type)) {
4548 if (is_type_valid(type)) {
4549 errorf(HERE, "operand of ! must be of scalar type");
4554 expression->base.type = orig_type;
4557 static void semantic_unexpr_integer(unary_expression_t *expression)
4559 type_t *const orig_type = expression->value->base.type;
4560 type_t *const type = skip_typeref(orig_type);
4561 if (!is_type_integer(type)) {
4562 if (is_type_valid(type)) {
4563 errorf(HERE, "operand of ~ must be of integer type");
4568 expression->base.type = orig_type;
4571 static void semantic_dereference(unary_expression_t *expression)
4573 type_t *const orig_type = expression->value->base.type;
4574 type_t *const type = skip_typeref(orig_type);
4575 if(!is_type_pointer(type)) {
4576 if (is_type_valid(type)) {
4577 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4582 type_t *result_type = type->pointer.points_to;
4583 result_type = automatic_type_conversion(result_type);
4584 expression->base.type = result_type;
4588 * Check the semantic of the address taken expression.
4590 static void semantic_take_addr(unary_expression_t *expression)
4592 expression_t *value = expression->value;
4593 value->base.type = revert_automatic_type_conversion(value);
4595 type_t *orig_type = value->base.type;
4596 if(!is_type_valid(orig_type))
4599 if(value->kind == EXPR_REFERENCE) {
4600 declaration_t *const declaration = value->reference.declaration;
4601 if(declaration != NULL) {
4602 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4603 errorf(expression->base.source_position,
4604 "address of register variable '%Y' requested",
4605 declaration->symbol);
4607 declaration->address_taken = 1;
4611 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4614 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4615 static expression_t *parse_##unexpression_type(unsigned precedence) \
4619 expression_t *unary_expression \
4620 = allocate_expression_zero(unexpression_type); \
4621 unary_expression->base.source_position = HERE; \
4622 unary_expression->unary.value = parse_sub_expression(precedence); \
4624 sfunc(&unary_expression->unary); \
4626 return unary_expression; \
4629 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4630 semantic_unexpr_arithmetic)
4631 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4632 semantic_unexpr_arithmetic)
4633 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4634 semantic_unexpr_scalar)
4635 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4636 semantic_dereference)
4637 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4639 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4640 semantic_unexpr_integer)
4641 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4643 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4646 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4648 static expression_t *parse_##unexpression_type(unsigned precedence, \
4649 expression_t *left) \
4651 (void) precedence; \
4654 expression_t *unary_expression \
4655 = allocate_expression_zero(unexpression_type); \
4656 unary_expression->unary.value = left; \
4658 sfunc(&unary_expression->unary); \
4660 return unary_expression; \
4663 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4664 EXPR_UNARY_POSTFIX_INCREMENT,
4666 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4667 EXPR_UNARY_POSTFIX_DECREMENT,
4670 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4672 /* TODO: handle complex + imaginary types */
4674 /* § 6.3.1.8 Usual arithmetic conversions */
4675 if(type_left == type_long_double || type_right == type_long_double) {
4676 return type_long_double;
4677 } else if(type_left == type_double || type_right == type_double) {
4679 } else if(type_left == type_float || type_right == type_float) {
4683 type_right = promote_integer(type_right);
4684 type_left = promote_integer(type_left);
4686 if(type_left == type_right)
4689 bool signed_left = is_type_signed(type_left);
4690 bool signed_right = is_type_signed(type_right);
4691 int rank_left = get_rank(type_left);
4692 int rank_right = get_rank(type_right);
4693 if(rank_left < rank_right) {
4694 if(signed_left == signed_right || !signed_right) {
4700 if(signed_left == signed_right || !signed_left) {
4709 * Check the semantic restrictions for a binary expression.
4711 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4713 expression_t *const left = expression->left;
4714 expression_t *const right = expression->right;
4715 type_t *const orig_type_left = left->base.type;
4716 type_t *const orig_type_right = right->base.type;
4717 type_t *const type_left = skip_typeref(orig_type_left);
4718 type_t *const type_right = skip_typeref(orig_type_right);
4720 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4721 /* TODO: improve error message */
4722 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4723 errorf(HERE, "operation needs arithmetic types");
4728 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4729 expression->left = create_implicit_cast(left, arithmetic_type);
4730 expression->right = create_implicit_cast(right, arithmetic_type);
4731 expression->base.type = arithmetic_type;
4734 static void semantic_shift_op(binary_expression_t *expression)
4736 expression_t *const left = expression->left;
4737 expression_t *const right = expression->right;
4738 type_t *const orig_type_left = left->base.type;
4739 type_t *const orig_type_right = right->base.type;
4740 type_t * type_left = skip_typeref(orig_type_left);
4741 type_t * type_right = skip_typeref(orig_type_right);
4743 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4744 /* TODO: improve error message */
4745 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4746 errorf(HERE, "operation needs integer types");
4751 type_left = promote_integer(type_left);
4752 type_right = promote_integer(type_right);
4754 expression->left = create_implicit_cast(left, type_left);
4755 expression->right = create_implicit_cast(right, type_right);
4756 expression->base.type = type_left;
4759 static void semantic_add(binary_expression_t *expression)
4761 expression_t *const left = expression->left;
4762 expression_t *const right = expression->right;
4763 type_t *const orig_type_left = left->base.type;
4764 type_t *const orig_type_right = right->base.type;
4765 type_t *const type_left = skip_typeref(orig_type_left);
4766 type_t *const type_right = skip_typeref(orig_type_right);
4769 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4770 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4771 expression->left = create_implicit_cast(left, arithmetic_type);
4772 expression->right = create_implicit_cast(right, arithmetic_type);
4773 expression->base.type = arithmetic_type;
4775 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4776 expression->base.type = type_left;
4777 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4778 expression->base.type = type_right;
4779 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4780 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4784 static void semantic_sub(binary_expression_t *expression)
4786 expression_t *const left = expression->left;
4787 expression_t *const right = expression->right;
4788 type_t *const orig_type_left = left->base.type;
4789 type_t *const orig_type_right = right->base.type;
4790 type_t *const type_left = skip_typeref(orig_type_left);
4791 type_t *const type_right = skip_typeref(orig_type_right);
4794 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4795 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4796 expression->left = create_implicit_cast(left, arithmetic_type);
4797 expression->right = create_implicit_cast(right, arithmetic_type);
4798 expression->base.type = arithmetic_type;
4800 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4801 expression->base.type = type_left;
4802 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4803 if(!pointers_compatible(type_left, type_right)) {
4805 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4806 orig_type_left, orig_type_right);
4808 expression->base.type = type_ptrdiff_t;
4810 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4811 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4812 orig_type_left, orig_type_right);
4817 * Check the semantics of comparison expressions.
4819 * @param expression The expression to check.
4821 static void semantic_comparison(binary_expression_t *expression)
4823 expression_t *left = expression->left;
4824 expression_t *right = expression->right;
4825 type_t *orig_type_left = left->base.type;
4826 type_t *orig_type_right = right->base.type;
4828 type_t *type_left = skip_typeref(orig_type_left);
4829 type_t *type_right = skip_typeref(orig_type_right);
4831 /* TODO non-arithmetic types */
4832 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4833 if (warning.sign_compare &&
4834 (expression->base.kind != EXPR_BINARY_EQUAL &&
4835 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4836 (is_type_signed(type_left) != is_type_signed(type_right))) {
4837 warningf(expression->base.source_position,
4838 "comparison between signed and unsigned");
4840 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4841 expression->left = create_implicit_cast(left, arithmetic_type);
4842 expression->right = create_implicit_cast(right, arithmetic_type);
4843 expression->base.type = arithmetic_type;
4844 if (warning.float_equal &&
4845 (expression->base.kind == EXPR_BINARY_EQUAL ||
4846 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4847 is_type_float(arithmetic_type)) {
4848 warningf(expression->base.source_position,
4849 "comparing floating point with == or != is unsafe");
4851 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4852 /* TODO check compatibility */
4853 } else if (is_type_pointer(type_left)) {
4854 expression->right = create_implicit_cast(right, type_left);
4855 } else if (is_type_pointer(type_right)) {
4856 expression->left = create_implicit_cast(left, type_right);
4857 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4858 type_error_incompatible("invalid operands in comparison",
4859 expression->base.source_position,
4860 type_left, type_right);
4862 expression->base.type = type_int;
4865 static void semantic_arithmetic_assign(binary_expression_t *expression)
4867 expression_t *left = expression->left;
4868 expression_t *right = expression->right;
4869 type_t *orig_type_left = left->base.type;
4870 type_t *orig_type_right = right->base.type;
4872 type_t *type_left = skip_typeref(orig_type_left);
4873 type_t *type_right = skip_typeref(orig_type_right);
4875 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4876 /* TODO: improve error message */
4877 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4878 errorf(HERE, "operation needs arithmetic types");
4883 /* combined instructions are tricky. We can't create an implicit cast on
4884 * the left side, because we need the uncasted form for the store.
4885 * The ast2firm pass has to know that left_type must be right_type
4886 * for the arithmetic operation and create a cast by itself */
4887 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4888 expression->right = create_implicit_cast(right, arithmetic_type);
4889 expression->base.type = type_left;
4892 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4894 expression_t *const left = expression->left;
4895 expression_t *const right = expression->right;
4896 type_t *const orig_type_left = left->base.type;
4897 type_t *const orig_type_right = right->base.type;
4898 type_t *const type_left = skip_typeref(orig_type_left);
4899 type_t *const type_right = skip_typeref(orig_type_right);
4901 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4902 /* combined instructions are tricky. We can't create an implicit cast on
4903 * the left side, because we need the uncasted form for the store.
4904 * The ast2firm pass has to know that left_type must be right_type
4905 * for the arithmetic operation and create a cast by itself */
4906 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4907 expression->right = create_implicit_cast(right, arithmetic_type);
4908 expression->base.type = type_left;
4909 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4910 expression->base.type = type_left;
4911 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4912 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4917 * Check the semantic restrictions of a logical expression.
4919 static void semantic_logical_op(binary_expression_t *expression)
4921 expression_t *const left = expression->left;
4922 expression_t *const right = expression->right;
4923 type_t *const orig_type_left = left->base.type;
4924 type_t *const orig_type_right = right->base.type;
4925 type_t *const type_left = skip_typeref(orig_type_left);
4926 type_t *const type_right = skip_typeref(orig_type_right);
4928 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4929 /* TODO: improve error message */
4930 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4931 errorf(HERE, "operation needs scalar types");
4936 expression->base.type = type_int;
4940 * Checks if a compound type has constant fields.
4942 static bool has_const_fields(const compound_type_t *type)
4944 const scope_t *scope = &type->declaration->scope;
4945 const declaration_t *declaration = scope->declarations;
4947 for (; declaration != NULL; declaration = declaration->next) {
4948 if (declaration->namespc != NAMESPACE_NORMAL)
4951 const type_t *decl_type = skip_typeref(declaration->type);
4952 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
4960 * Check the semantic restrictions of a binary assign expression.
4962 static void semantic_binexpr_assign(binary_expression_t *expression)
4964 expression_t *left = expression->left;
4965 type_t *orig_type_left = left->base.type;
4967 type_t *type_left = revert_automatic_type_conversion(left);
4968 type_left = skip_typeref(orig_type_left);
4970 /* must be a modifiable lvalue */
4971 if (is_type_array(type_left)) {
4972 errorf(HERE, "cannot assign to arrays ('%E')", left);
4975 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
4976 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
4980 if(is_type_incomplete(type_left)) {
4982 "left-hand side of assignment '%E' has incomplete type '%T'",
4983 left, orig_type_left);
4986 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
4987 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
4988 left, orig_type_left);
4992 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
4994 if (res_type == NULL) {
4995 errorf(expression->base.source_position,
4996 "cannot assign to '%T' from '%T'",
4997 orig_type_left, expression->right->base.type);
4999 expression->right = create_implicit_cast(expression->right, res_type);
5002 expression->base.type = orig_type_left;
5005 static bool expression_has_effect(const expression_t *const expr)
5007 switch (expr->kind) {
5008 case EXPR_UNKNOWN: break;
5009 case EXPR_INVALID: break;
5010 case EXPR_REFERENCE: return false;
5011 case EXPR_CONST: return false;
5012 case EXPR_CHAR_CONST: return false;
5013 case EXPR_STRING_LITERAL: return false;
5014 case EXPR_WIDE_STRING_LITERAL: return false;
5016 const call_expression_t *const call = &expr->call;
5017 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5020 switch (call->function->builtin_symbol.symbol->ID) {
5021 case T___builtin_va_end: return true;
5022 default: return false;
5025 case EXPR_CONDITIONAL: {
5026 const conditional_expression_t *const cond = &expr->conditional;
5028 expression_has_effect(cond->true_expression) &&
5029 expression_has_effect(cond->false_expression);
5031 case EXPR_SELECT: return false;
5032 case EXPR_ARRAY_ACCESS: return false;
5033 case EXPR_SIZEOF: return false;
5034 case EXPR_CLASSIFY_TYPE: return false;
5035 case EXPR_ALIGNOF: return false;
5037 case EXPR_FUNCTION: return false;
5038 case EXPR_PRETTY_FUNCTION: return false;
5039 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5040 case EXPR_BUILTIN_CONSTANT_P: return false;
5041 case EXPR_BUILTIN_PREFETCH: return true;
5042 case EXPR_OFFSETOF: return false;
5043 case EXPR_VA_START: return true;
5044 case EXPR_VA_ARG: return true;
5045 case EXPR_STATEMENT: return true; // TODO
5046 case EXPR_COMPOUND_LITERAL: return false;
5048 case EXPR_UNARY_NEGATE: return false;
5049 case EXPR_UNARY_PLUS: return false;
5050 case EXPR_UNARY_BITWISE_NEGATE: return false;
5051 case EXPR_UNARY_NOT: return false;
5052 case EXPR_UNARY_DEREFERENCE: return false;
5053 case EXPR_UNARY_TAKE_ADDRESS: return false;
5054 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5055 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5056 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5057 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5058 case EXPR_UNARY_CAST: {
5059 type_t *type = skip_typeref(expr->base.type);
5060 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5062 case EXPR_UNARY_CAST_IMPLICIT: return true;
5063 case EXPR_UNARY_ASSUME: return true;
5064 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5066 case EXPR_BINARY_ADD: return false;
5067 case EXPR_BINARY_SUB: return false;
5068 case EXPR_BINARY_MUL: return false;
5069 case EXPR_BINARY_DIV: return false;
5070 case EXPR_BINARY_MOD: return false;
5071 case EXPR_BINARY_EQUAL: return false;
5072 case EXPR_BINARY_NOTEQUAL: return false;
5073 case EXPR_BINARY_LESS: return false;
5074 case EXPR_BINARY_LESSEQUAL: return false;
5075 case EXPR_BINARY_GREATER: return false;
5076 case EXPR_BINARY_GREATEREQUAL: return false;
5077 case EXPR_BINARY_BITWISE_AND: return false;
5078 case EXPR_BINARY_BITWISE_OR: return false;
5079 case EXPR_BINARY_BITWISE_XOR: return false;
5080 case EXPR_BINARY_SHIFTLEFT: return false;
5081 case EXPR_BINARY_SHIFTRIGHT: return false;
5082 case EXPR_BINARY_ASSIGN: return true;
5083 case EXPR_BINARY_MUL_ASSIGN: return true;
5084 case EXPR_BINARY_DIV_ASSIGN: return true;
5085 case EXPR_BINARY_MOD_ASSIGN: return true;
5086 case EXPR_BINARY_ADD_ASSIGN: return true;
5087 case EXPR_BINARY_SUB_ASSIGN: return true;
5088 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5089 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5090 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5091 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5092 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5093 case EXPR_BINARY_LOGICAL_AND:
5094 case EXPR_BINARY_LOGICAL_OR:
5095 case EXPR_BINARY_COMMA:
5096 return expression_has_effect(expr->binary.right);
5098 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5099 case EXPR_BINARY_ISGREATER: return false;
5100 case EXPR_BINARY_ISGREATEREQUAL: return false;
5101 case EXPR_BINARY_ISLESS: return false;
5102 case EXPR_BINARY_ISLESSEQUAL: return false;
5103 case EXPR_BINARY_ISLESSGREATER: return false;
5104 case EXPR_BINARY_ISUNORDERED: return false;
5107 panic("unexpected statement");
5110 static void semantic_comma(binary_expression_t *expression)
5112 if (warning.unused_value) {
5113 const expression_t *const left = expression->left;
5114 if (!expression_has_effect(left)) {
5115 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5118 expression->base.type = expression->right->base.type;
5121 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5122 static expression_t *parse_##binexpression_type(unsigned precedence, \
5123 expression_t *left) \
5126 source_position_t pos = HERE; \
5128 expression_t *right = parse_sub_expression(precedence + lr); \
5130 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5131 binexpr->base.source_position = pos; \
5132 binexpr->binary.left = left; \
5133 binexpr->binary.right = right; \
5134 sfunc(&binexpr->binary); \
5139 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5140 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5141 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5142 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5143 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5144 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5145 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5146 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5147 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5149 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5150 semantic_comparison, 1)
5151 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5152 semantic_comparison, 1)
5153 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5154 semantic_comparison, 1)
5155 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5156 semantic_comparison, 1)
5158 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5159 semantic_binexpr_arithmetic, 1)
5160 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5161 semantic_binexpr_arithmetic, 1)
5162 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5163 semantic_binexpr_arithmetic, 1)
5164 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5165 semantic_logical_op, 1)
5166 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5167 semantic_logical_op, 1)
5168 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5169 semantic_shift_op, 1)
5170 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5171 semantic_shift_op, 1)
5172 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5173 semantic_arithmetic_addsubb_assign, 0)
5174 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5175 semantic_arithmetic_addsubb_assign, 0)
5176 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5177 semantic_arithmetic_assign, 0)
5178 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5179 semantic_arithmetic_assign, 0)
5180 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5181 semantic_arithmetic_assign, 0)
5182 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5183 semantic_arithmetic_assign, 0)
5184 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5185 semantic_arithmetic_assign, 0)
5186 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5187 semantic_arithmetic_assign, 0)
5188 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5189 semantic_arithmetic_assign, 0)
5190 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5191 semantic_arithmetic_assign, 0)
5193 static expression_t *parse_sub_expression(unsigned precedence)
5195 if(token.type < 0) {
5196 return expected_expression_error();
5199 expression_parser_function_t *parser
5200 = &expression_parsers[token.type];
5201 source_position_t source_position = token.source_position;
5204 if(parser->parser != NULL) {
5205 left = parser->parser(parser->precedence);
5207 left = parse_primary_expression();
5209 assert(left != NULL);
5210 left->base.source_position = source_position;
5213 if(token.type < 0) {
5214 return expected_expression_error();
5217 parser = &expression_parsers[token.type];
5218 if(parser->infix_parser == NULL)
5220 if(parser->infix_precedence < precedence)
5223 left = parser->infix_parser(parser->infix_precedence, left);
5225 assert(left != NULL);
5226 assert(left->kind != EXPR_UNKNOWN);
5227 left->base.source_position = source_position;
5234 * Parse an expression.
5236 static expression_t *parse_expression(void)
5238 return parse_sub_expression(1);
5242 * Register a parser for a prefix-like operator with given precedence.
5244 * @param parser the parser function
5245 * @param token_type the token type of the prefix token
5246 * @param precedence the precedence of the operator
5248 static void register_expression_parser(parse_expression_function parser,
5249 int token_type, unsigned precedence)
5251 expression_parser_function_t *entry = &expression_parsers[token_type];
5253 if(entry->parser != NULL) {
5254 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5255 panic("trying to register multiple expression parsers for a token");
5257 entry->parser = parser;
5258 entry->precedence = precedence;
5262 * Register a parser for an infix operator with given precedence.
5264 * @param parser the parser function
5265 * @param token_type the token type of the infix operator
5266 * @param precedence the precedence of the operator
5268 static void register_infix_parser(parse_expression_infix_function parser,
5269 int token_type, unsigned precedence)
5271 expression_parser_function_t *entry = &expression_parsers[token_type];
5273 if(entry->infix_parser != NULL) {
5274 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5275 panic("trying to register multiple infix expression parsers for a "
5278 entry->infix_parser = parser;
5279 entry->infix_precedence = precedence;
5283 * Initialize the expression parsers.
5285 static void init_expression_parsers(void)
5287 memset(&expression_parsers, 0, sizeof(expression_parsers));
5289 register_infix_parser(parse_array_expression, '[', 30);
5290 register_infix_parser(parse_call_expression, '(', 30);
5291 register_infix_parser(parse_select_expression, '.', 30);
5292 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5293 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5295 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5298 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5299 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5300 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5301 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5302 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5303 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5304 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5305 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5306 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5307 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5308 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5309 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5310 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5311 T_EXCLAMATIONMARKEQUAL, 13);
5312 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5313 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5314 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5315 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5316 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5317 register_infix_parser(parse_conditional_expression, '?', 7);
5318 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5319 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5320 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5321 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5322 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5323 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5324 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5325 T_LESSLESSEQUAL, 2);
5326 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5327 T_GREATERGREATEREQUAL, 2);
5328 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5330 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5332 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5335 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5337 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5338 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5339 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5340 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5341 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5342 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5343 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5345 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5347 register_expression_parser(parse_sizeof, T_sizeof, 25);
5348 register_expression_parser(parse_alignof, T___alignof__, 25);
5349 register_expression_parser(parse_extension, T___extension__, 25);
5350 register_expression_parser(parse_builtin_classify_type,
5351 T___builtin_classify_type, 25);
5355 * Parse a asm statement constraints specification.
5357 static asm_constraint_t *parse_asm_constraints(void)
5359 asm_constraint_t *result = NULL;
5360 asm_constraint_t *last = NULL;
5362 while(token.type == T_STRING_LITERAL || token.type == '[') {
5363 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5364 memset(constraint, 0, sizeof(constraint[0]));
5366 if(token.type == '[') {
5368 if(token.type != T_IDENTIFIER) {
5369 parse_error_expected("while parsing asm constraint",
5373 constraint->symbol = token.v.symbol;
5378 constraint->constraints = parse_string_literals();
5380 constraint->expression = parse_expression();
5384 last->next = constraint;
5386 result = constraint;
5390 if(token.type != ',')
5399 * Parse a asm statement clobber specification.
5401 static asm_clobber_t *parse_asm_clobbers(void)
5403 asm_clobber_t *result = NULL;
5404 asm_clobber_t *last = NULL;
5406 while(token.type == T_STRING_LITERAL) {
5407 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5408 clobber->clobber = parse_string_literals();
5411 last->next = clobber;
5417 if(token.type != ',')
5426 * Parse an asm statement.
5428 static statement_t *parse_asm_statement(void)
5432 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5433 statement->base.source_position = token.source_position;
5435 asm_statement_t *asm_statement = &statement->asms;
5437 if(token.type == T_volatile) {
5439 asm_statement->is_volatile = true;
5443 asm_statement->asm_text = parse_string_literals();
5445 if(token.type != ':')
5449 asm_statement->inputs = parse_asm_constraints();
5450 if(token.type != ':')
5454 asm_statement->outputs = parse_asm_constraints();
5455 if(token.type != ':')
5459 asm_statement->clobbers = parse_asm_clobbers();
5468 * Parse a case statement.
5470 static statement_t *parse_case_statement(void)
5474 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5476 statement->base.source_position = token.source_position;
5477 statement->case_label.expression = parse_expression();
5479 if (c_mode & _GNUC) {
5480 if (token.type == T_DOTDOTDOT) {
5482 statement->case_label.end_range = parse_expression();
5488 if (! is_constant_expression(statement->case_label.expression)) {
5489 errorf(statement->base.source_position,
5490 "case label does not reduce to an integer constant");
5492 /* TODO: check if the case label is already known */
5493 if (current_switch != NULL) {
5494 /* link all cases into the switch statement */
5495 if (current_switch->last_case == NULL) {
5496 current_switch->first_case =
5497 current_switch->last_case = &statement->case_label;
5499 current_switch->last_case->next = &statement->case_label;
5502 errorf(statement->base.source_position,
5503 "case label not within a switch statement");
5506 statement->case_label.statement = parse_statement();
5512 * Finds an existing default label of a switch statement.
5514 static case_label_statement_t *
5515 find_default_label(const switch_statement_t *statement)
5517 case_label_statement_t *label = statement->first_case;
5518 for ( ; label != NULL; label = label->next) {
5519 if (label->expression == NULL)
5526 * Parse a default statement.
5528 static statement_t *parse_default_statement(void)
5532 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5534 statement->base.source_position = token.source_position;
5537 if (current_switch != NULL) {
5538 const case_label_statement_t *def_label = find_default_label(current_switch);
5539 if (def_label != NULL) {
5540 errorf(HERE, "multiple default labels in one switch");
5541 errorf(def_label->base.source_position,
5542 "this is the first default label");
5544 /* link all cases into the switch statement */
5545 if (current_switch->last_case == NULL) {
5546 current_switch->first_case =
5547 current_switch->last_case = &statement->case_label;
5549 current_switch->last_case->next = &statement->case_label;
5553 errorf(statement->base.source_position,
5554 "'default' label not within a switch statement");
5556 statement->case_label.statement = parse_statement();
5562 * Return the declaration for a given label symbol or create a new one.
5564 static declaration_t *get_label(symbol_t *symbol)
5566 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5567 assert(current_function != NULL);
5568 /* if we found a label in the same function, then we already created the
5570 if(candidate != NULL
5571 && candidate->parent_scope == ¤t_function->scope) {
5575 /* otherwise we need to create a new one */
5576 declaration_t *const declaration = allocate_declaration_zero();
5577 declaration->namespc = NAMESPACE_LABEL;
5578 declaration->symbol = symbol;
5580 label_push(declaration);
5586 * Parse a label statement.
5588 static statement_t *parse_label_statement(void)
5590 assert(token.type == T_IDENTIFIER);
5591 symbol_t *symbol = token.v.symbol;
5594 declaration_t *label = get_label(symbol);
5596 /* if source position is already set then the label is defined twice,
5597 * otherwise it was just mentioned in a goto so far */
5598 if(label->source_position.input_name != NULL) {
5599 errorf(HERE, "duplicate label '%Y'", symbol);
5600 errorf(label->source_position, "previous definition of '%Y' was here",
5603 label->source_position = token.source_position;
5606 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5608 statement->base.source_position = token.source_position;
5609 statement->label.label = label;
5613 if(token.type == '}') {
5614 /* TODO only warn? */
5615 errorf(HERE, "label at end of compound statement");
5618 if (token.type == ';') {
5619 /* eat an empty statement here, to avoid the warning about an empty
5620 * after a label. label:; is commonly used to have a label before
5624 statement->label.statement = parse_statement();
5628 /* remember the labels's in a list for later checking */
5629 if (label_last == NULL) {
5630 label_first = &statement->label;
5632 label_last->next = &statement->label;
5634 label_last = &statement->label;
5640 * Parse an if statement.
5642 static statement_t *parse_if(void)
5646 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5647 statement->base.source_position = token.source_position;
5650 statement->ifs.condition = parse_expression();
5653 statement->ifs.true_statement = parse_statement();
5654 if(token.type == T_else) {
5656 statement->ifs.false_statement = parse_statement();
5663 * Parse a switch statement.
5665 static statement_t *parse_switch(void)
5669 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5670 statement->base.source_position = token.source_position;
5673 expression_t *const expr = parse_expression();
5674 type_t * type = skip_typeref(expr->base.type);
5675 if (is_type_integer(type)) {
5676 type = promote_integer(type);
5677 } else if (is_type_valid(type)) {
5678 errorf(expr->base.source_position,
5679 "switch quantity is not an integer, but '%T'", type);
5680 type = type_error_type;
5682 statement->switchs.expression = create_implicit_cast(expr, type);
5685 switch_statement_t *rem = current_switch;
5686 current_switch = &statement->switchs;
5687 statement->switchs.body = parse_statement();
5688 current_switch = rem;
5690 if (warning.switch_default
5691 && find_default_label(&statement->switchs) == NULL) {
5692 warningf(statement->base.source_position, "switch has no default case");
5698 static statement_t *parse_loop_body(statement_t *const loop)
5700 statement_t *const rem = current_loop;
5701 current_loop = loop;
5703 statement_t *const body = parse_statement();
5710 * Parse a while statement.
5712 static statement_t *parse_while(void)
5716 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5717 statement->base.source_position = token.source_position;
5720 statement->whiles.condition = parse_expression();
5723 statement->whiles.body = parse_loop_body(statement);
5729 * Parse a do statement.
5731 static statement_t *parse_do(void)
5735 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5737 statement->base.source_position = token.source_position;
5739 statement->do_while.body = parse_loop_body(statement);
5743 statement->do_while.condition = parse_expression();
5751 * Parse a for statement.
5753 static statement_t *parse_for(void)
5757 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5758 statement->base.source_position = token.source_position;
5762 int top = environment_top();
5763 scope_t *last_scope = scope;
5764 set_scope(&statement->fors.scope);
5766 if(token.type != ';') {
5767 if(is_declaration_specifier(&token, false)) {
5768 parse_declaration(record_declaration);
5770 expression_t *const init = parse_expression();
5771 statement->fors.initialisation = init;
5772 if (warning.unused_value && !expression_has_effect(init)) {
5773 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5781 if(token.type != ';') {
5782 statement->fors.condition = parse_expression();
5785 if(token.type != ')') {
5786 expression_t *const step = parse_expression();
5787 statement->fors.step = step;
5788 if (warning.unused_value && !expression_has_effect(step)) {
5789 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5793 statement->fors.body = parse_loop_body(statement);
5795 assert(scope == &statement->fors.scope);
5796 set_scope(last_scope);
5797 environment_pop_to(top);
5803 * Parse a goto statement.
5805 static statement_t *parse_goto(void)
5809 if(token.type != T_IDENTIFIER) {
5810 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5814 symbol_t *symbol = token.v.symbol;
5817 declaration_t *label = get_label(symbol);
5819 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5820 statement->base.source_position = token.source_position;
5822 statement->gotos.label = label;
5824 /* remember the goto's in a list for later checking */
5825 if (goto_last == NULL) {
5826 goto_first = &statement->gotos;
5828 goto_last->next = &statement->gotos;
5830 goto_last = &statement->gotos;
5838 * Parse a continue statement.
5840 static statement_t *parse_continue(void)
5842 statement_t *statement;
5843 if (current_loop == NULL) {
5844 errorf(HERE, "continue statement not within loop");
5847 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5849 statement->base.source_position = token.source_position;
5859 * Parse a break statement.
5861 static statement_t *parse_break(void)
5863 statement_t *statement;
5864 if (current_switch == NULL && current_loop == NULL) {
5865 errorf(HERE, "break statement not within loop or switch");
5868 statement = allocate_statement_zero(STATEMENT_BREAK);
5870 statement->base.source_position = token.source_position;
5880 * Check if a given declaration represents a local variable.
5882 static bool is_local_var_declaration(const declaration_t *declaration) {
5883 switch ((storage_class_tag_t) declaration->storage_class) {
5884 case STORAGE_CLASS_NONE:
5885 case STORAGE_CLASS_AUTO:
5886 case STORAGE_CLASS_REGISTER: {
5887 const type_t *type = skip_typeref(declaration->type);
5888 if(is_type_function(type)) {
5900 * Check if a given declaration represents a variable.
5902 static bool is_var_declaration(const declaration_t *declaration) {
5903 switch ((storage_class_tag_t) declaration->storage_class) {
5904 case STORAGE_CLASS_NONE:
5905 case STORAGE_CLASS_EXTERN:
5906 case STORAGE_CLASS_STATIC:
5907 case STORAGE_CLASS_AUTO:
5908 case STORAGE_CLASS_REGISTER:
5909 case STORAGE_CLASS_THREAD:
5910 case STORAGE_CLASS_THREAD_EXTERN:
5911 case STORAGE_CLASS_THREAD_STATIC: {
5912 const type_t *type = skip_typeref(declaration->type);
5913 if(is_type_function(type)) {
5925 * Check if a given expression represents a local variable.
5927 static bool is_local_variable(const expression_t *expression)
5929 if (expression->base.kind != EXPR_REFERENCE) {
5932 const declaration_t *declaration = expression->reference.declaration;
5933 return is_local_var_declaration(declaration);
5937 * Check if a given expression represents a local variable and
5938 * return its declaration then, else return NULL.
5940 declaration_t *expr_is_variable(const expression_t *expression)
5942 if (expression->base.kind != EXPR_REFERENCE) {
5945 declaration_t *declaration = expression->reference.declaration;
5946 if (is_var_declaration(declaration))
5952 * Parse a return statement.
5954 static statement_t *parse_return(void)
5958 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
5959 statement->base.source_position = token.source_position;
5961 expression_t *return_value = NULL;
5962 if(token.type != ';') {
5963 return_value = parse_expression();
5967 const type_t *const func_type = current_function->type;
5968 assert(is_type_function(func_type));
5969 type_t *const return_type = skip_typeref(func_type->function.return_type);
5971 if(return_value != NULL) {
5972 type_t *return_value_type = skip_typeref(return_value->base.type);
5974 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
5975 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
5976 warningf(statement->base.source_position,
5977 "'return' with a value, in function returning void");
5978 return_value = NULL;
5980 type_t *const res_type = semantic_assign(return_type,
5981 return_value, "'return'");
5982 if (res_type == NULL) {
5983 errorf(statement->base.source_position,
5984 "cannot return something of type '%T' in function returning '%T'",
5985 return_value->base.type, return_type);
5987 return_value = create_implicit_cast(return_value, res_type);
5990 /* check for returning address of a local var */
5991 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
5992 const expression_t *expression = return_value->unary.value;
5993 if (is_local_variable(expression)) {
5994 warningf(statement->base.source_position,
5995 "function returns address of local variable");
5999 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6000 warningf(statement->base.source_position,
6001 "'return' without value, in function returning non-void");
6004 statement->returns.value = return_value;
6010 * Parse a declaration statement.
6012 static statement_t *parse_declaration_statement(void)
6014 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6016 statement->base.source_position = token.source_position;
6018 declaration_t *before = last_declaration;
6019 parse_declaration(record_declaration);
6021 if(before == NULL) {
6022 statement->declaration.declarations_begin = scope->declarations;
6024 statement->declaration.declarations_begin = before->next;
6026 statement->declaration.declarations_end = last_declaration;
6032 * Parse an expression statement, ie. expr ';'.
6034 static statement_t *parse_expression_statement(void)
6036 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6038 statement->base.source_position = token.source_position;
6039 expression_t *const expr = parse_expression();
6040 statement->expression.expression = expr;
6042 if (warning.unused_value && !expression_has_effect(expr)) {
6043 warningf(expr->base.source_position, "statement has no effect");
6052 * Parse a statement.
6054 static statement_t *parse_statement(void)
6056 statement_t *statement = NULL;
6058 /* declaration or statement */
6059 switch(token.type) {
6061 statement = parse_asm_statement();
6065 statement = parse_case_statement();
6069 statement = parse_default_statement();
6073 statement = parse_compound_statement();
6077 statement = parse_if();
6081 statement = parse_switch();
6085 statement = parse_while();
6089 statement = parse_do();
6093 statement = parse_for();
6097 statement = parse_goto();
6101 statement = parse_continue();
6105 statement = parse_break();
6109 statement = parse_return();
6113 if (warning.empty_statement) {
6114 warningf(HERE, "statement is empty");
6121 if(look_ahead(1)->type == ':') {
6122 statement = parse_label_statement();
6126 if(is_typedef_symbol(token.v.symbol)) {
6127 statement = parse_declaration_statement();
6131 statement = parse_expression_statement();
6134 case T___extension__:
6135 /* this can be a prefix to a declaration or an expression statement */
6136 /* we simply eat it now and parse the rest with tail recursion */
6139 } while(token.type == T___extension__);
6140 statement = parse_statement();
6144 statement = parse_declaration_statement();
6148 statement = parse_expression_statement();
6152 assert(statement == NULL
6153 || statement->base.source_position.input_name != NULL);
6159 * Parse a compound statement.
6161 static statement_t *parse_compound_statement(void)
6163 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6165 statement->base.source_position = token.source_position;
6169 int top = environment_top();
6170 scope_t *last_scope = scope;
6171 set_scope(&statement->compound.scope);
6173 statement_t *last_statement = NULL;
6175 while(token.type != '}' && token.type != T_EOF) {
6176 statement_t *sub_statement = parse_statement();
6177 if(sub_statement == NULL)
6180 if(last_statement != NULL) {
6181 last_statement->base.next = sub_statement;
6183 statement->compound.statements = sub_statement;
6186 while(sub_statement->base.next != NULL)
6187 sub_statement = sub_statement->base.next;
6189 last_statement = sub_statement;
6192 if(token.type == '}') {
6195 errorf(statement->base.source_position,
6196 "end of file while looking for closing '}'");
6199 assert(scope == &statement->compound.scope);
6200 set_scope(last_scope);
6201 environment_pop_to(top);
6207 * Initialize builtin types.
6209 static void initialize_builtin_types(void)
6211 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6212 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6213 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6214 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6215 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6216 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6217 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6218 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6220 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6221 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6222 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6223 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6227 * Check for unused global static functions and variables
6229 static void check_unused_globals(void)
6231 if (!warning.unused_function && !warning.unused_variable)
6234 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6235 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6238 type_t *const type = decl->type;
6240 if (is_type_function(skip_typeref(type))) {
6241 if (!warning.unused_function || decl->is_inline)
6244 s = (decl->init.statement != NULL ? "defined" : "declared");
6246 if (!warning.unused_variable)
6252 warningf(decl->source_position, "'%#T' %s but not used",
6253 type, decl->symbol, s);
6258 * Parse a translation unit.
6260 static translation_unit_t *parse_translation_unit(void)
6262 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6264 assert(global_scope == NULL);
6265 global_scope = &unit->scope;
6267 assert(scope == NULL);
6268 set_scope(&unit->scope);
6270 initialize_builtin_types();
6272 while(token.type != T_EOF) {
6273 if (token.type == ';') {
6274 /* TODO error in strict mode */
6275 warningf(HERE, "stray ';' outside of function");
6278 parse_external_declaration();
6282 assert(scope == &unit->scope);
6284 last_declaration = NULL;
6286 assert(global_scope == &unit->scope);
6287 check_unused_globals();
6288 global_scope = NULL;
6296 * @return the translation unit or NULL if errors occurred.
6298 translation_unit_t *parse(void)
6300 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6301 label_stack = NEW_ARR_F(stack_entry_t, 0);
6302 diagnostic_count = 0;
6306 type_set_output(stderr);
6307 ast_set_output(stderr);
6309 lookahead_bufpos = 0;
6310 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6313 translation_unit_t *unit = parse_translation_unit();
6315 DEL_ARR_F(environment_stack);
6316 DEL_ARR_F(label_stack);
6325 * Initialize the parser.
6327 void init_parser(void)
6329 init_expression_parsers();
6330 obstack_init(&temp_obst);
6332 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6333 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6337 * Terminate the parser.
6339 void exit_parser(void)
6341 obstack_free(&temp_obst, NULL);