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,
1007 bool must_be_constant)
1009 /* there might be extra {} hierarchies */
1011 while(token.type == '{') {
1014 warningf(HERE, "extra curly braces around scalar initializer");
1019 expression_t *expression = parse_assignment_expression();
1020 if(must_be_constant && !is_constant_expression(expression)) {
1021 errorf(expression->base.source_position,
1022 "Initialisation expression '%E' is not constant\n",
1026 initializer_t *initializer = initializer_from_expression(type, expression);
1028 if(initializer == NULL) {
1029 errorf(expression->base.source_position,
1030 "expression '%E' doesn't match expected type '%T'",
1036 bool additional_warning_displayed = false;
1038 if(token.type == ',') {
1041 if(token.type != '}') {
1042 if(!additional_warning_displayed) {
1043 warningf(HERE, "additional elements in scalar initializer");
1044 additional_warning_displayed = true;
1054 typedef struct type_path_entry_t type_path_entry_t;
1055 struct type_path_entry_t {
1059 declaration_t *compound_entry;
1063 typedef struct type_path_t type_path_t;
1064 struct type_path_t {
1065 type_path_entry_t *path;
1066 type_t *top_type; /**< type of the element the path points */
1067 size_t max_index; /**< largest index in outermost array */
1071 static __attribute__((unused)) void debug_print_type_path(
1072 const type_path_t *path)
1074 size_t len = ARR_LEN(path->path);
1077 fprintf(stderr, "invalid path");
1081 for(size_t i = 0; i < len; ++i) {
1082 const type_path_entry_t *entry = & path->path[i];
1084 type_t *type = skip_typeref(entry->type);
1085 if(is_type_compound(type)) {
1086 fprintf(stderr, ".%s", entry->v.compound_entry->symbol->string);
1087 } else if(is_type_array(type)) {
1088 fprintf(stderr, "[%u]", entry->v.index);
1090 fprintf(stderr, "-INVALID-");
1093 fprintf(stderr, " (");
1094 print_type(path->top_type);
1095 fprintf(stderr, ")");
1098 static type_path_entry_t *get_type_path_top(const type_path_t *path)
1100 size_t len = ARR_LEN(path->path);
1102 return & path->path[len-1];
1105 static type_path_entry_t *append_to_type_path(type_path_t *path)
1107 size_t len = ARR_LEN(path->path);
1108 ARR_RESIZE(type_path_entry_t, path->path, len+1);
1110 type_path_entry_t *result = & path->path[len];
1111 memset(result, 0, sizeof(result[0]));
1115 static void descend_into_subtype(type_path_t *path)
1117 type_t *orig_top_type = path->top_type;
1118 type_t *top_type = skip_typeref(orig_top_type);
1120 assert(is_type_compound(top_type) || is_type_array(top_type));
1122 type_path_entry_t *top = append_to_type_path(path);
1123 top->type = top_type;
1125 if(is_type_compound(top_type)) {
1126 declaration_t *declaration = top_type->compound.declaration;
1127 declaration_t *entry = declaration->scope.declarations;
1129 top->v.compound_entry = entry;
1130 path->top_type = entry->type;
1132 assert(is_type_array(top_type));
1135 path->top_type = top_type->array.element_type;
1139 static void ascend_from_subtype(type_path_t *path)
1141 type_path_entry_t *top = get_type_path_top(path);
1143 path->top_type = top->type;
1145 size_t len = ARR_LEN(path->path);
1146 ARR_RESIZE(type_path_entry_t, path->path, len-1);
1149 static void ascend_to(type_path_t *path, size_t top_path_level)
1151 size_t len = ARR_LEN(path->path);
1152 assert(len >= top_path_level);
1154 while(len > top_path_level) {
1155 ascend_from_subtype(path);
1156 len = ARR_LEN(path->path);
1160 static bool walk_designator(type_path_t *path, const designator_t *designator,
1161 bool used_in_offsetof)
1163 for( ; designator != NULL; designator = designator->next) {
1164 type_path_entry_t *top = get_type_path_top(path);
1165 type_t *orig_type = top->type;
1167 type_t *type = skip_typeref(orig_type);
1169 if(designator->symbol != NULL) {
1170 symbol_t *symbol = designator->symbol;
1171 if(!is_type_compound(type)) {
1172 if(is_type_valid(type)) {
1173 errorf(designator->source_position,
1174 "'.%Y' designator used for non-compound type '%T'",
1180 declaration_t *declaration = type->compound.declaration;
1181 declaration_t *iter = declaration->scope.declarations;
1182 for( ; iter != NULL; iter = iter->next) {
1183 if(iter->symbol == symbol) {
1188 errorf(designator->source_position,
1189 "'%T' has no member named '%Y'", orig_type, symbol);
1192 if(used_in_offsetof) {
1193 type_t *real_type = skip_typeref(iter->type);
1194 if(real_type->kind == TYPE_BITFIELD) {
1195 errorf(designator->source_position,
1196 "offsetof designator '%Y' may not specify bitfield",
1202 top->type = orig_type;
1203 top->v.compound_entry = iter;
1204 orig_type = iter->type;
1206 expression_t *array_index = designator->array_index;
1207 assert(designator->array_index != NULL);
1209 if(!is_type_array(type)) {
1210 if(is_type_valid(type)) {
1211 errorf(designator->source_position,
1212 "[%E] designator used for non-array type '%T'",
1213 array_index, orig_type);
1217 if(!is_type_valid(array_index->base.type)) {
1221 long index = fold_constant(array_index);
1222 if(!used_in_offsetof) {
1224 errorf(designator->source_position,
1225 "array index [%E] must be positive", array_index);
1228 if(type->array.size_constant == true) {
1229 long array_size = type->array.size;
1230 if(index >= array_size) {
1231 errorf(designator->source_position,
1232 "designator [%E] (%d) exceeds array size %d",
1233 array_index, index, array_size);
1239 top->type = orig_type;
1240 top->v.index = (size_t) index;
1241 orig_type = type->array.element_type;
1243 path->top_type = orig_type;
1245 if(designator->next != NULL) {
1246 descend_into_subtype(path);
1250 path->invalid = false;
1257 static void advance_current_object(type_path_t *path, size_t top_path_level)
1262 type_path_entry_t *top = get_type_path_top(path);
1264 type_t *type = skip_typeref(top->type);
1265 if(is_type_union(type)) {
1266 /* in unions only the first element is initialized */
1267 top->v.compound_entry = NULL;
1268 } else if(is_type_struct(type)) {
1269 declaration_t *entry = top->v.compound_entry;
1271 entry = entry->next;
1272 top->v.compound_entry = entry;
1274 path->top_type = entry->type;
1278 assert(is_type_array(type));
1282 if(!type->array.size_constant || top->v.index < type->array.size) {
1287 /* we're past the last member of the current sub-aggregate, try if we
1288 * can ascend in the type hierarchy and continue with another subobject */
1289 size_t len = ARR_LEN(path->path);
1291 if(len > top_path_level) {
1292 ascend_from_subtype(path);
1293 advance_current_object(path, top_path_level);
1295 path->invalid = true;
1299 static void skip_initializers(void)
1301 if(token.type == '{')
1304 while(token.type != '}') {
1305 if(token.type == T_EOF)
1307 if(token.type == '{') {
1315 static initializer_t *parse_sub_initializer(type_path_t *path,
1316 type_t *outer_type, size_t top_path_level, bool must_be_constant)
1318 type_t *orig_type = path->top_type;
1319 type_t *type = skip_typeref(orig_type);
1321 /* we can't do usefull stuff if we didn't even parse the type. Skip the
1322 * initializers in this case. */
1323 if(!is_type_valid(type)) {
1324 skip_initializers();
1328 initializer_t **initializers = NEW_ARR_F(initializer_t*, 0);
1331 designator_t *designator = NULL;
1332 if(token.type == '.' || token.type == '[') {
1333 designator = parse_designation();
1335 /* reset path to toplevel, evaluate designator from there */
1336 ascend_to(path, top_path_level);
1337 if(!walk_designator(path, designator, false)) {
1338 /* can't continue after designation error */
1342 initializer_t *designator_initializer
1343 = allocate_initializer_zero(INITIALIZER_DESIGNATOR);
1344 designator_initializer->designator.designator = designator;
1345 ARR_APP1(initializer_t*, initializers, designator_initializer);
1350 if(token.type == '{') {
1351 if(is_type_scalar(type)) {
1352 sub = parse_scalar_initializer(type, must_be_constant);
1355 descend_into_subtype(path);
1357 sub = parse_sub_initializer(path, orig_type, top_path_level+1,
1360 ascend_from_subtype(path);
1365 /* must be an expression */
1366 expression_t *expression = parse_assignment_expression();
1368 if(must_be_constant && !is_constant_expression(expression)) {
1369 errorf(expression->base.source_position,
1370 "Initialisation expression '%E' is not constant\n",
1374 /* handle { "string" } special case */
1375 if((expression->kind == EXPR_STRING_LITERAL
1376 || expression->kind == EXPR_WIDE_STRING_LITERAL)
1377 && outer_type != NULL) {
1378 sub = initializer_from_expression(outer_type, expression);
1380 if(token.type == ',') {
1383 if(token.type != '}') {
1384 warningf(HERE, "excessive elements in initializer for type '%T'",
1387 /* TODO: eat , ... */
1392 /* descend into subtypes until expression matches type */
1394 orig_type = path->top_type;
1395 type = skip_typeref(orig_type);
1397 sub = initializer_from_expression(orig_type, expression);
1401 if(!is_type_valid(type)) {
1404 if(is_type_scalar(type)) {
1405 errorf(expression->base.source_position,
1406 "expression '%E' doesn't match expected type '%T'",
1407 expression, orig_type);
1411 descend_into_subtype(path);
1415 /* update largest index of top array */
1416 const type_path_entry_t *first = &path->path[0];
1417 type_t *first_type = first->type;
1418 first_type = skip_typeref(first_type);
1419 if(is_type_array(first_type)) {
1420 size_t index = first->v.index;
1421 if(index > path->max_index)
1422 path->max_index = index;
1425 /* append to initializers list */
1426 ARR_APP1(initializer_t*, initializers, sub);
1428 if(token.type == '}') {
1432 if(token.type == '}') {
1436 advance_current_object(path, top_path_level);
1437 orig_type = path->top_type;
1438 type = skip_typeref(orig_type);
1441 size_t len = ARR_LEN(initializers);
1442 size_t size = sizeof(initializer_list_t) + len * sizeof(initializers[0]);
1443 initializer_t *result = allocate_ast_zero(size);
1444 result->kind = INITIALIZER_LIST;
1445 result->list.len = len;
1446 memcpy(&result->list.initializers, initializers,
1447 len * sizeof(initializers[0]));
1449 ascend_to(path, top_path_level);
1454 skip_initializers();
1455 DEL_ARR_F(initializers);
1456 ascend_to(path, top_path_level);
1460 typedef struct parse_initializer_env_t {
1461 type_t *type; /* the type of the initializer. In case of an
1462 array type with unspecified size this gets
1463 adjusted to the actual size. */
1464 initializer_t *initializer; /* initializer will be filled in here */
1465 bool must_be_constant;
1466 } parse_initializer_env_t;
1468 static void parse_initializer(parse_initializer_env_t *env)
1470 type_t *type = skip_typeref(env->type);
1471 initializer_t *result = NULL;
1474 if(token.type != '{') {
1475 expression_t *expression = parse_assignment_expression();
1477 result = initializer_from_expression(type, expression);
1478 if(result == NULL) {
1480 "initializer expression '%E' of type '%T' is incompatible with type '%T'",
1481 expression, expression->base.type, env->type);
1483 } else if(is_type_scalar(type)) {
1484 /* TODO: § 6.7.8.11; eat {} without warning */
1485 result = parse_scalar_initializer(type, env->must_be_constant);
1487 if(token.type == ',')
1489 } else if(token.type == '{') {
1493 memset(&path, 0, sizeof(path));
1494 path.top_type = env->type;
1495 path.path = NEW_ARR_F(type_path_entry_t, 0);
1497 descend_into_subtype(&path);
1499 result = parse_sub_initializer(&path, env->type, 1,
1500 env->must_be_constant);
1502 max_index = path.max_index;
1503 DEL_ARR_F(path.path);
1507 /* TODO: can this even happen? */
1511 /* § 6.7.5 (22) array initializers for arrays with unknown size determine
1512 * the array type size */
1513 if(is_type_array(type) && type->array.size_expression == NULL
1514 && result != NULL) {
1516 switch (result->kind) {
1517 case INITIALIZER_LIST:
1518 size = max_index + 1;
1521 case INITIALIZER_STRING:
1522 size = result->string.string.size;
1525 case INITIALIZER_WIDE_STRING:
1526 size = result->wide_string.string.size;
1530 panic("invalid initializer type");
1533 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
1534 cnst->base.type = type_size_t;
1535 cnst->conste.v.int_value = size;
1537 type_t *new_type = duplicate_type(type);
1539 new_type->array.size_expression = cnst;
1540 new_type->array.size_constant = true;
1541 new_type->array.size = size;
1542 env->type = new_type;
1545 env->initializer = result;
1548 static declaration_t *append_declaration(declaration_t *declaration);
1550 static declaration_t *parse_compound_type_specifier(bool is_struct)
1558 symbol_t *symbol = NULL;
1559 declaration_t *declaration = NULL;
1561 if (token.type == T___attribute__) {
1566 if(token.type == T_IDENTIFIER) {
1567 symbol = token.v.symbol;
1571 declaration = get_declaration(symbol, NAMESPACE_STRUCT);
1573 declaration = get_declaration(symbol, NAMESPACE_UNION);
1575 } else if(token.type != '{') {
1577 parse_error_expected("while parsing struct type specifier",
1578 T_IDENTIFIER, '{', 0);
1580 parse_error_expected("while parsing union type specifier",
1581 T_IDENTIFIER, '{', 0);
1587 if(declaration == NULL) {
1588 declaration = allocate_declaration_zero();
1589 declaration->namespc =
1590 (is_struct ? NAMESPACE_STRUCT : NAMESPACE_UNION);
1591 declaration->source_position = token.source_position;
1592 declaration->symbol = symbol;
1593 declaration->parent_scope = scope;
1594 if (symbol != NULL) {
1595 environment_push(declaration);
1597 append_declaration(declaration);
1600 if(token.type == '{') {
1601 if(declaration->init.is_defined) {
1602 assert(symbol != NULL);
1603 errorf(HERE, "multiple definitions of '%s %Y'",
1604 is_struct ? "struct" : "union", symbol);
1605 declaration->scope.declarations = NULL;
1607 declaration->init.is_defined = true;
1609 parse_compound_type_entries(declaration);
1616 static void parse_enum_entries(type_t *const enum_type)
1620 if(token.type == '}') {
1622 errorf(HERE, "empty enum not allowed");
1627 if(token.type != T_IDENTIFIER) {
1628 parse_error_expected("while parsing enum entry", T_IDENTIFIER, 0);
1633 declaration_t *const entry = allocate_declaration_zero();
1634 entry->storage_class = STORAGE_CLASS_ENUM_ENTRY;
1635 entry->type = enum_type;
1636 entry->symbol = token.v.symbol;
1637 entry->source_position = token.source_position;
1640 if(token.type == '=') {
1642 expression_t *value = parse_constant_expression();
1644 value = create_implicit_cast(value, enum_type);
1645 entry->init.enum_value = value;
1650 record_declaration(entry);
1652 if(token.type != ',')
1655 } while(token.type != '}');
1660 static type_t *parse_enum_specifier(void)
1664 declaration_t *declaration;
1667 if(token.type == T_IDENTIFIER) {
1668 symbol = token.v.symbol;
1671 declaration = get_declaration(symbol, NAMESPACE_ENUM);
1672 } else if(token.type != '{') {
1673 parse_error_expected("while parsing enum type specifier",
1674 T_IDENTIFIER, '{', 0);
1681 if(declaration == NULL) {
1682 declaration = allocate_declaration_zero();
1683 declaration->namespc = NAMESPACE_ENUM;
1684 declaration->source_position = token.source_position;
1685 declaration->symbol = symbol;
1686 declaration->parent_scope = scope;
1689 type_t *const type = allocate_type_zero(TYPE_ENUM, declaration->source_position);
1690 type->enumt.declaration = declaration;
1692 if(token.type == '{') {
1693 if(declaration->init.is_defined) {
1694 errorf(HERE, "multiple definitions of enum %Y", symbol);
1696 if (symbol != NULL) {
1697 environment_push(declaration);
1699 append_declaration(declaration);
1700 declaration->init.is_defined = 1;
1702 parse_enum_entries(type);
1710 * if a symbol is a typedef to another type, return true
1712 static bool is_typedef_symbol(symbol_t *symbol)
1714 const declaration_t *const declaration =
1715 get_declaration(symbol, NAMESPACE_NORMAL);
1717 declaration != NULL &&
1718 declaration->storage_class == STORAGE_CLASS_TYPEDEF;
1721 static type_t *parse_typeof(void)
1729 expression_t *expression = NULL;
1732 switch(token.type) {
1733 case T___extension__:
1734 /* this can be a prefix to a typename or an expression */
1735 /* we simply eat it now. */
1738 } while(token.type == T___extension__);
1742 if(is_typedef_symbol(token.v.symbol)) {
1743 type = parse_typename();
1745 expression = parse_expression();
1746 type = expression->base.type;
1751 type = parse_typename();
1755 expression = parse_expression();
1756 type = expression->base.type;
1762 type_t *typeof_type = allocate_type_zero(TYPE_TYPEOF, expression->base.source_position);
1763 typeof_type->typeoft.expression = expression;
1764 typeof_type->typeoft.typeof_type = type;
1770 SPECIFIER_SIGNED = 1 << 0,
1771 SPECIFIER_UNSIGNED = 1 << 1,
1772 SPECIFIER_LONG = 1 << 2,
1773 SPECIFIER_INT = 1 << 3,
1774 SPECIFIER_DOUBLE = 1 << 4,
1775 SPECIFIER_CHAR = 1 << 5,
1776 SPECIFIER_SHORT = 1 << 6,
1777 SPECIFIER_LONG_LONG = 1 << 7,
1778 SPECIFIER_FLOAT = 1 << 8,
1779 SPECIFIER_BOOL = 1 << 9,
1780 SPECIFIER_VOID = 1 << 10,
1781 #ifdef PROVIDE_COMPLEX
1782 SPECIFIER_COMPLEX = 1 << 11,
1783 SPECIFIER_IMAGINARY = 1 << 12,
1787 static type_t *create_builtin_type(symbol_t *const symbol,
1788 type_t *const real_type)
1790 type_t *type = allocate_type_zero(TYPE_BUILTIN, builtin_source_position);
1791 type->builtin.symbol = symbol;
1792 type->builtin.real_type = real_type;
1794 type_t *result = typehash_insert(type);
1795 if (type != result) {
1802 static type_t *get_typedef_type(symbol_t *symbol)
1804 declaration_t *declaration = get_declaration(symbol, NAMESPACE_NORMAL);
1805 if(declaration == NULL
1806 || declaration->storage_class != STORAGE_CLASS_TYPEDEF)
1809 type_t *type = allocate_type_zero(TYPE_TYPEDEF, declaration->source_position);
1810 type->typedeft.declaration = declaration;
1815 static void parse_declaration_specifiers(declaration_specifiers_t *specifiers)
1817 type_t *type = NULL;
1818 unsigned type_qualifiers = 0;
1819 unsigned type_specifiers = 0;
1822 specifiers->source_position = token.source_position;
1825 switch(token.type) {
1828 #define MATCH_STORAGE_CLASS(token, class) \
1830 if(specifiers->storage_class != STORAGE_CLASS_NONE) { \
1831 errorf(HERE, "multiple storage classes in declaration specifiers"); \
1833 specifiers->storage_class = class; \
1837 MATCH_STORAGE_CLASS(T_typedef, STORAGE_CLASS_TYPEDEF)
1838 MATCH_STORAGE_CLASS(T_extern, STORAGE_CLASS_EXTERN)
1839 MATCH_STORAGE_CLASS(T_static, STORAGE_CLASS_STATIC)
1840 MATCH_STORAGE_CLASS(T_auto, STORAGE_CLASS_AUTO)
1841 MATCH_STORAGE_CLASS(T_register, STORAGE_CLASS_REGISTER)
1844 switch (specifiers->storage_class) {
1845 case STORAGE_CLASS_NONE:
1846 specifiers->storage_class = STORAGE_CLASS_THREAD;
1849 case STORAGE_CLASS_EXTERN:
1850 specifiers->storage_class = STORAGE_CLASS_THREAD_EXTERN;
1853 case STORAGE_CLASS_STATIC:
1854 specifiers->storage_class = STORAGE_CLASS_THREAD_STATIC;
1858 errorf(HERE, "multiple storage classes in declaration specifiers");
1864 /* type qualifiers */
1865 #define MATCH_TYPE_QUALIFIER(token, qualifier) \
1867 type_qualifiers |= qualifier; \
1871 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
1872 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
1873 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
1875 case T___extension__:
1880 /* type specifiers */
1881 #define MATCH_SPECIFIER(token, specifier, name) \
1884 if(type_specifiers & specifier) { \
1885 errorf(HERE, "multiple " name " type specifiers given"); \
1887 type_specifiers |= specifier; \
1891 MATCH_SPECIFIER(T_void, SPECIFIER_VOID, "void")
1892 MATCH_SPECIFIER(T_char, SPECIFIER_CHAR, "char")
1893 MATCH_SPECIFIER(T_short, SPECIFIER_SHORT, "short")
1894 MATCH_SPECIFIER(T_int, SPECIFIER_INT, "int")
1895 MATCH_SPECIFIER(T_float, SPECIFIER_FLOAT, "float")
1896 MATCH_SPECIFIER(T_double, SPECIFIER_DOUBLE, "double")
1897 MATCH_SPECIFIER(T_signed, SPECIFIER_SIGNED, "signed")
1898 MATCH_SPECIFIER(T_unsigned, SPECIFIER_UNSIGNED, "unsigned")
1899 MATCH_SPECIFIER(T__Bool, SPECIFIER_BOOL, "_Bool")
1900 #ifdef PROVIDE_COMPLEX
1901 MATCH_SPECIFIER(T__Complex, SPECIFIER_COMPLEX, "_Complex")
1902 MATCH_SPECIFIER(T__Imaginary, SPECIFIER_IMAGINARY, "_Imaginary")
1905 /* only in microsoft mode */
1906 specifiers->decl_modifiers |= DM_FORCEINLINE;
1910 specifiers->is_inline = true;
1915 if(type_specifiers & SPECIFIER_LONG_LONG) {
1916 errorf(HERE, "multiple type specifiers given");
1917 } else if(type_specifiers & SPECIFIER_LONG) {
1918 type_specifiers |= SPECIFIER_LONG_LONG;
1920 type_specifiers |= SPECIFIER_LONG;
1925 type = allocate_type_zero(TYPE_COMPOUND_STRUCT, HERE);
1927 type->compound.declaration = parse_compound_type_specifier(true);
1931 type = allocate_type_zero(TYPE_COMPOUND_UNION, HERE);
1933 type->compound.declaration = parse_compound_type_specifier(false);
1937 type = parse_enum_specifier();
1940 type = parse_typeof();
1942 case T___builtin_va_list:
1943 type = duplicate_type(type_valist);
1947 case T___attribute__:
1951 case T_IDENTIFIER: {
1952 /* only parse identifier if we haven't found a type yet */
1953 if(type != NULL || type_specifiers != 0)
1954 goto finish_specifiers;
1956 type_t *typedef_type = get_typedef_type(token.v.symbol);
1958 if(typedef_type == NULL)
1959 goto finish_specifiers;
1962 type = typedef_type;
1966 /* function specifier */
1968 goto finish_specifiers;
1975 atomic_type_kind_t atomic_type;
1977 /* match valid basic types */
1978 switch(type_specifiers) {
1979 case SPECIFIER_VOID:
1980 atomic_type = ATOMIC_TYPE_VOID;
1982 case SPECIFIER_CHAR:
1983 atomic_type = ATOMIC_TYPE_CHAR;
1985 case SPECIFIER_SIGNED | SPECIFIER_CHAR:
1986 atomic_type = ATOMIC_TYPE_SCHAR;
1988 case SPECIFIER_UNSIGNED | SPECIFIER_CHAR:
1989 atomic_type = ATOMIC_TYPE_UCHAR;
1991 case SPECIFIER_SHORT:
1992 case SPECIFIER_SIGNED | SPECIFIER_SHORT:
1993 case SPECIFIER_SHORT | SPECIFIER_INT:
1994 case SPECIFIER_SIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1995 atomic_type = ATOMIC_TYPE_SHORT;
1997 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT:
1998 case SPECIFIER_UNSIGNED | SPECIFIER_SHORT | SPECIFIER_INT:
1999 atomic_type = ATOMIC_TYPE_USHORT;
2002 case SPECIFIER_SIGNED:
2003 case SPECIFIER_SIGNED | SPECIFIER_INT:
2004 atomic_type = ATOMIC_TYPE_INT;
2006 case SPECIFIER_UNSIGNED:
2007 case SPECIFIER_UNSIGNED | SPECIFIER_INT:
2008 atomic_type = ATOMIC_TYPE_UINT;
2010 case SPECIFIER_LONG:
2011 case SPECIFIER_SIGNED | SPECIFIER_LONG:
2012 case SPECIFIER_LONG | SPECIFIER_INT:
2013 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2014 atomic_type = ATOMIC_TYPE_LONG;
2016 case SPECIFIER_UNSIGNED | SPECIFIER_LONG:
2017 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_INT:
2018 atomic_type = ATOMIC_TYPE_ULONG;
2020 case SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2021 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2022 case SPECIFIER_LONG | SPECIFIER_LONG_LONG | SPECIFIER_INT:
2023 case SPECIFIER_SIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2025 atomic_type = ATOMIC_TYPE_LONGLONG;
2027 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG:
2028 case SPECIFIER_UNSIGNED | SPECIFIER_LONG | SPECIFIER_LONG_LONG
2030 atomic_type = ATOMIC_TYPE_ULONGLONG;
2032 case SPECIFIER_FLOAT:
2033 atomic_type = ATOMIC_TYPE_FLOAT;
2035 case SPECIFIER_DOUBLE:
2036 atomic_type = ATOMIC_TYPE_DOUBLE;
2038 case SPECIFIER_LONG | SPECIFIER_DOUBLE:
2039 atomic_type = ATOMIC_TYPE_LONG_DOUBLE;
2041 case SPECIFIER_BOOL:
2042 atomic_type = ATOMIC_TYPE_BOOL;
2044 #ifdef PROVIDE_COMPLEX
2045 case SPECIFIER_FLOAT | SPECIFIER_COMPLEX:
2046 atomic_type = ATOMIC_TYPE_FLOAT_COMPLEX;
2048 case SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2049 atomic_type = ATOMIC_TYPE_DOUBLE_COMPLEX;
2051 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_COMPLEX:
2052 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_COMPLEX;
2054 case SPECIFIER_FLOAT | SPECIFIER_IMAGINARY:
2055 atomic_type = ATOMIC_TYPE_FLOAT_IMAGINARY;
2057 case SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2058 atomic_type = ATOMIC_TYPE_DOUBLE_IMAGINARY;
2060 case SPECIFIER_LONG | SPECIFIER_DOUBLE | SPECIFIER_IMAGINARY:
2061 atomic_type = ATOMIC_TYPE_LONG_DOUBLE_IMAGINARY;
2065 /* invalid specifier combination, give an error message */
2066 if(type_specifiers == 0) {
2067 if (! strict_mode) {
2068 if (warning.implicit_int) {
2069 warningf(HERE, "no type specifiers in declaration, using 'int'");
2071 atomic_type = ATOMIC_TYPE_INT;
2074 errorf(HERE, "no type specifiers given in declaration");
2076 } else if((type_specifiers & SPECIFIER_SIGNED) &&
2077 (type_specifiers & SPECIFIER_UNSIGNED)) {
2078 errorf(HERE, "signed and unsigned specifiers gives");
2079 } else if(type_specifiers & (SPECIFIER_SIGNED | SPECIFIER_UNSIGNED)) {
2080 errorf(HERE, "only integer types can be signed or unsigned");
2082 errorf(HERE, "multiple datatypes in declaration");
2084 atomic_type = ATOMIC_TYPE_INVALID;
2087 type = allocate_type_zero(TYPE_ATOMIC, builtin_source_position);
2088 type->atomic.akind = atomic_type;
2091 if(type_specifiers != 0) {
2092 errorf(HERE, "multiple datatypes in declaration");
2096 type->base.qualifiers = type_qualifiers;
2098 type_t *result = typehash_insert(type);
2099 if(newtype && result != type) {
2103 specifiers->type = result;
2106 static type_qualifiers_t parse_type_qualifiers(void)
2108 type_qualifiers_t type_qualifiers = TYPE_QUALIFIER_NONE;
2111 switch(token.type) {
2112 /* type qualifiers */
2113 MATCH_TYPE_QUALIFIER(T_const, TYPE_QUALIFIER_CONST);
2114 MATCH_TYPE_QUALIFIER(T_restrict, TYPE_QUALIFIER_RESTRICT);
2115 MATCH_TYPE_QUALIFIER(T_volatile, TYPE_QUALIFIER_VOLATILE);
2118 return type_qualifiers;
2123 static declaration_t *parse_identifier_list(void)
2125 declaration_t *declarations = NULL;
2126 declaration_t *last_declaration = NULL;
2128 declaration_t *const declaration = allocate_declaration_zero();
2129 declaration->type = NULL; /* a K&R parameter list has no types, yet */
2130 declaration->source_position = token.source_position;
2131 declaration->symbol = token.v.symbol;
2134 if(last_declaration != NULL) {
2135 last_declaration->next = declaration;
2137 declarations = declaration;
2139 last_declaration = declaration;
2141 if(token.type != ',')
2144 } while(token.type == T_IDENTIFIER);
2146 return declarations;
2149 static void semantic_parameter(declaration_t *declaration)
2151 /* TODO: improve error messages */
2153 if(declaration->storage_class == STORAGE_CLASS_TYPEDEF) {
2154 errorf(HERE, "typedef not allowed in parameter list");
2155 } else if(declaration->storage_class != STORAGE_CLASS_NONE
2156 && declaration->storage_class != STORAGE_CLASS_REGISTER) {
2157 errorf(HERE, "parameter may only have none or register storage class");
2160 type_t *const orig_type = declaration->type;
2161 type_t * type = skip_typeref(orig_type);
2163 /* Array as last part of a parameter type is just syntactic sugar. Turn it
2164 * into a pointer. § 6.7.5.3 (7) */
2165 if (is_type_array(type)) {
2166 type_t *const element_type = type->array.element_type;
2168 type = make_pointer_type(element_type, type->base.qualifiers);
2170 declaration->type = type;
2173 if(is_type_incomplete(type)) {
2174 errorf(HERE, "incomplete type '%T' not allowed for parameter '%Y'",
2175 orig_type, declaration->symbol);
2179 static declaration_t *parse_parameter(void)
2181 declaration_specifiers_t specifiers;
2182 memset(&specifiers, 0, sizeof(specifiers));
2184 parse_declaration_specifiers(&specifiers);
2186 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/true);
2188 semantic_parameter(declaration);
2193 static declaration_t *parse_parameters(function_type_t *type)
2195 if(token.type == T_IDENTIFIER) {
2196 symbol_t *symbol = token.v.symbol;
2197 if(!is_typedef_symbol(symbol)) {
2198 type->kr_style_parameters = true;
2199 return parse_identifier_list();
2203 if(token.type == ')') {
2204 type->unspecified_parameters = 1;
2207 if(token.type == T_void && look_ahead(1)->type == ')') {
2212 declaration_t *declarations = NULL;
2213 declaration_t *declaration;
2214 declaration_t *last_declaration = NULL;
2215 function_parameter_t *parameter;
2216 function_parameter_t *last_parameter = NULL;
2219 switch(token.type) {
2223 return declarations;
2226 case T___extension__:
2228 declaration = parse_parameter();
2230 parameter = obstack_alloc(type_obst, sizeof(parameter[0]));
2231 memset(parameter, 0, sizeof(parameter[0]));
2232 parameter->type = declaration->type;
2234 if(last_parameter != NULL) {
2235 last_declaration->next = declaration;
2236 last_parameter->next = parameter;
2238 type->parameters = parameter;
2239 declarations = declaration;
2241 last_parameter = parameter;
2242 last_declaration = declaration;
2246 return declarations;
2248 if(token.type != ',')
2249 return declarations;
2259 } construct_type_kind_t;
2261 typedef struct construct_type_t construct_type_t;
2262 struct construct_type_t {
2263 construct_type_kind_t kind;
2264 construct_type_t *next;
2267 typedef struct parsed_pointer_t parsed_pointer_t;
2268 struct parsed_pointer_t {
2269 construct_type_t construct_type;
2270 type_qualifiers_t type_qualifiers;
2273 typedef struct construct_function_type_t construct_function_type_t;
2274 struct construct_function_type_t {
2275 construct_type_t construct_type;
2276 type_t *function_type;
2279 typedef struct parsed_array_t parsed_array_t;
2280 struct parsed_array_t {
2281 construct_type_t construct_type;
2282 type_qualifiers_t type_qualifiers;
2288 typedef struct construct_base_type_t construct_base_type_t;
2289 struct construct_base_type_t {
2290 construct_type_t construct_type;
2294 static construct_type_t *parse_pointer_declarator(void)
2298 parsed_pointer_t *pointer = obstack_alloc(&temp_obst, sizeof(pointer[0]));
2299 memset(pointer, 0, sizeof(pointer[0]));
2300 pointer->construct_type.kind = CONSTRUCT_POINTER;
2301 pointer->type_qualifiers = parse_type_qualifiers();
2303 return (construct_type_t*) pointer;
2306 static construct_type_t *parse_array_declarator(void)
2310 parsed_array_t *array = obstack_alloc(&temp_obst, sizeof(array[0]));
2311 memset(array, 0, sizeof(array[0]));
2312 array->construct_type.kind = CONSTRUCT_ARRAY;
2314 if(token.type == T_static) {
2315 array->is_static = true;
2319 type_qualifiers_t type_qualifiers = parse_type_qualifiers();
2320 if(type_qualifiers != 0) {
2321 if(token.type == T_static) {
2322 array->is_static = true;
2326 array->type_qualifiers = type_qualifiers;
2328 if(token.type == '*' && look_ahead(1)->type == ']') {
2329 array->is_variable = true;
2331 } else if(token.type != ']') {
2332 array->size = parse_assignment_expression();
2337 return (construct_type_t*) array;
2340 static construct_type_t *parse_function_declarator(declaration_t *declaration)
2345 if(declaration != NULL) {
2346 type = allocate_type_zero(TYPE_FUNCTION, declaration->source_position);
2348 type = allocate_type_zero(TYPE_FUNCTION, token.source_position);
2351 declaration_t *parameters = parse_parameters(&type->function);
2352 if(declaration != NULL) {
2353 declaration->scope.declarations = parameters;
2356 construct_function_type_t *construct_function_type =
2357 obstack_alloc(&temp_obst, sizeof(construct_function_type[0]));
2358 memset(construct_function_type, 0, sizeof(construct_function_type[0]));
2359 construct_function_type->construct_type.kind = CONSTRUCT_FUNCTION;
2360 construct_function_type->function_type = type;
2364 return (construct_type_t*) construct_function_type;
2367 static construct_type_t *parse_inner_declarator(declaration_t *declaration,
2368 bool may_be_abstract)
2370 /* construct a single linked list of construct_type_t's which describe
2371 * how to construct the final declarator type */
2372 construct_type_t *first = NULL;
2373 construct_type_t *last = NULL;
2376 while(token.type == '*') {
2377 construct_type_t *type = parse_pointer_declarator();
2388 /* TODO: find out if this is correct */
2391 construct_type_t *inner_types = NULL;
2393 switch(token.type) {
2395 if(declaration == NULL) {
2396 errorf(HERE, "no identifier expected in typename");
2398 declaration->symbol = token.v.symbol;
2399 declaration->source_position = token.source_position;
2405 inner_types = parse_inner_declarator(declaration, may_be_abstract);
2411 parse_error_expected("while parsing declarator", T_IDENTIFIER, '(', 0);
2412 /* avoid a loop in the outermost scope, because eat_statement doesn't
2414 if(token.type == '}' && current_function == NULL) {
2422 construct_type_t *p = last;
2425 construct_type_t *type;
2426 switch(token.type) {
2428 type = parse_function_declarator(declaration);
2431 type = parse_array_declarator();
2434 goto declarator_finished;
2437 /* insert in the middle of the list (behind p) */
2439 type->next = p->next;
2450 declarator_finished:
2453 /* append inner_types at the end of the list, we don't to set last anymore
2454 * as it's not needed anymore */
2456 assert(first == NULL);
2457 first = inner_types;
2459 last->next = inner_types;
2465 static type_t *construct_declarator_type(construct_type_t *construct_list,
2468 construct_type_t *iter = construct_list;
2469 for( ; iter != NULL; iter = iter->next) {
2470 switch(iter->kind) {
2471 case CONSTRUCT_INVALID:
2472 panic("invalid type construction found");
2473 case CONSTRUCT_FUNCTION: {
2474 construct_function_type_t *construct_function_type
2475 = (construct_function_type_t*) iter;
2477 type_t *function_type = construct_function_type->function_type;
2479 function_type->function.return_type = type;
2481 type_t *skipped_return_type = skip_typeref(type);
2482 if (is_type_function(skipped_return_type)) {
2483 errorf(HERE, "function returning function is not allowed");
2484 type = type_error_type;
2485 } else if (is_type_array(skipped_return_type)) {
2486 errorf(HERE, "function returning array is not allowed");
2487 type = type_error_type;
2489 type = function_type;
2494 case CONSTRUCT_POINTER: {
2495 parsed_pointer_t *parsed_pointer = (parsed_pointer_t*) iter;
2496 type_t *pointer_type = allocate_type_zero(TYPE_POINTER, (source_position_t){NULL, 0});
2497 pointer_type->pointer.points_to = type;
2498 pointer_type->base.qualifiers = parsed_pointer->type_qualifiers;
2500 type = pointer_type;
2504 case CONSTRUCT_ARRAY: {
2505 parsed_array_t *parsed_array = (parsed_array_t*) iter;
2506 type_t *array_type = allocate_type_zero(TYPE_ARRAY, (source_position_t){NULL, 0});
2508 expression_t *size_expression = parsed_array->size;
2510 array_type->base.qualifiers = parsed_array->type_qualifiers;
2511 array_type->array.element_type = type;
2512 array_type->array.is_static = parsed_array->is_static;
2513 array_type->array.is_variable = parsed_array->is_variable;
2514 array_type->array.size_expression = size_expression;
2516 if(size_expression != NULL &&
2517 is_constant_expression(size_expression)) {
2518 array_type->array.size_constant = true;
2519 array_type->array.size
2520 = fold_constant(size_expression);
2523 type_t *skipped_type = skip_typeref(type);
2524 if (is_type_atomic(skipped_type, ATOMIC_TYPE_VOID)) {
2525 errorf(HERE, "array of void is not allowed");
2526 type = type_error_type;
2534 type_t *hashed_type = typehash_insert(type);
2535 if(hashed_type != type) {
2536 /* the function type was constructed earlier freeing it here will
2537 * destroy other types... */
2538 if(iter->kind != CONSTRUCT_FUNCTION) {
2548 static declaration_t *parse_declarator(
2549 const declaration_specifiers_t *specifiers, bool may_be_abstract)
2551 declaration_t *const declaration = allocate_declaration_zero();
2552 declaration->storage_class = specifiers->storage_class;
2553 declaration->modifiers = specifiers->decl_modifiers;
2554 declaration->is_inline = specifiers->is_inline;
2556 construct_type_t *construct_type
2557 = parse_inner_declarator(declaration, may_be_abstract);
2558 type_t *const type = specifiers->type;
2559 declaration->type = construct_declarator_type(construct_type, type);
2561 if(construct_type != NULL) {
2562 obstack_free(&temp_obst, construct_type);
2568 static type_t *parse_abstract_declarator(type_t *base_type)
2570 construct_type_t *construct_type = parse_inner_declarator(NULL, 1);
2572 type_t *result = construct_declarator_type(construct_type, base_type);
2573 if(construct_type != NULL) {
2574 obstack_free(&temp_obst, construct_type);
2580 static declaration_t *append_declaration(declaration_t* const declaration)
2582 if (last_declaration != NULL) {
2583 last_declaration->next = declaration;
2585 scope->declarations = declaration;
2587 last_declaration = declaration;
2592 * Check if the declaration of main is suspicious. main should be a
2593 * function with external linkage, returning int, taking either zero
2594 * arguments, two, or three arguments of appropriate types, ie.
2596 * int main([ int argc, char **argv [, char **env ] ]).
2598 * @param decl the declaration to check
2599 * @param type the function type of the declaration
2601 static void check_type_of_main(const declaration_t *const decl, const function_type_t *const func_type)
2603 if (decl->storage_class == STORAGE_CLASS_STATIC) {
2604 warningf(decl->source_position, "'main' is normally a non-static function");
2606 if (skip_typeref(func_type->return_type) != type_int) {
2607 warningf(decl->source_position, "return type of 'main' should be 'int', but is '%T'", func_type->return_type);
2609 const function_parameter_t *parm = func_type->parameters;
2611 type_t *const first_type = parm->type;
2612 if (!types_compatible(skip_typeref(first_type), type_int)) {
2613 warningf(decl->source_position, "first argument of 'main' should be 'int', but is '%T'", first_type);
2617 type_t *const second_type = parm->type;
2618 if (!types_compatible(skip_typeref(second_type), type_char_ptr_ptr)) {
2619 warningf(decl->source_position, "second argument of 'main' should be 'char**', but is '%T'", second_type);
2623 type_t *const third_type = parm->type;
2624 if (!types_compatible(skip_typeref(third_type), type_char_ptr_ptr)) {
2625 warningf(decl->source_position, "third argument of 'main' should be 'char**', but is '%T'", third_type);
2629 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2633 warningf(decl->source_position, "'main' takes only zero, two or three arguments");
2639 * Check if a symbol is the equal to "main".
2641 static bool is_sym_main(const symbol_t *const sym)
2643 return strcmp(sym->string, "main") == 0;
2646 static declaration_t *internal_record_declaration(
2647 declaration_t *const declaration,
2648 const bool is_function_definition)
2650 const symbol_t *const symbol = declaration->symbol;
2651 const namespace_t namespc = (namespace_t)declaration->namespc;
2653 type_t *const orig_type = declaration->type;
2654 type_t *const type = skip_typeref(orig_type);
2655 if (is_type_function(type) &&
2656 type->function.unspecified_parameters &&
2657 warning.strict_prototypes) {
2658 warningf(declaration->source_position,
2659 "function declaration '%#T' is not a prototype",
2660 orig_type, declaration->symbol);
2663 if (is_function_definition && warning.main && is_sym_main(symbol)) {
2664 check_type_of_main(declaration, &type->function);
2667 assert(declaration->symbol != NULL);
2668 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2670 assert(declaration != previous_declaration);
2671 if (previous_declaration != NULL) {
2672 if (previous_declaration->parent_scope == scope) {
2673 /* can happen for K&R style declarations */
2674 if(previous_declaration->type == NULL) {
2675 previous_declaration->type = declaration->type;
2678 const type_t *prev_type = skip_typeref(previous_declaration->type);
2679 if (!types_compatible(type, prev_type)) {
2680 errorf(declaration->source_position,
2681 "declaration '%#T' is incompatible with "
2682 "previous declaration '%#T'",
2683 orig_type, symbol, previous_declaration->type, symbol);
2684 errorf(previous_declaration->source_position,
2685 "previous declaration of '%Y' was here", symbol);
2687 unsigned old_storage_class
2688 = previous_declaration->storage_class;
2689 unsigned new_storage_class = declaration->storage_class;
2691 if(is_type_incomplete(prev_type)) {
2692 previous_declaration->type = type;
2696 /* pretend no storage class means extern for function
2697 * declarations (except if the previous declaration is neither
2698 * none nor extern) */
2699 if (is_type_function(type)) {
2700 switch (old_storage_class) {
2701 case STORAGE_CLASS_NONE:
2702 old_storage_class = STORAGE_CLASS_EXTERN;
2704 case STORAGE_CLASS_EXTERN:
2705 if (is_function_definition) {
2706 if (warning.missing_prototypes &&
2707 prev_type->function.unspecified_parameters &&
2708 !is_sym_main(symbol)) {
2709 warningf(declaration->source_position,
2710 "no previous prototype for '%#T'",
2713 } else if (new_storage_class == STORAGE_CLASS_NONE) {
2714 new_storage_class = STORAGE_CLASS_EXTERN;
2722 if (old_storage_class == STORAGE_CLASS_EXTERN &&
2723 new_storage_class == STORAGE_CLASS_EXTERN) {
2724 warn_redundant_declaration:
2725 if (warning.redundant_decls) {
2726 warningf(declaration->source_position,
2727 "redundant declaration for '%Y'", symbol);
2728 warningf(previous_declaration->source_position,
2729 "previous declaration of '%Y' was here",
2732 } else if (current_function == NULL) {
2733 if (old_storage_class != STORAGE_CLASS_STATIC &&
2734 new_storage_class == STORAGE_CLASS_STATIC) {
2735 errorf(declaration->source_position,
2736 "static declaration of '%Y' follows non-static declaration",
2738 errorf(previous_declaration->source_position,
2739 "previous declaration of '%Y' was here", symbol);
2741 if (old_storage_class != STORAGE_CLASS_EXTERN && !is_function_definition) {
2742 goto warn_redundant_declaration;
2744 if (new_storage_class == STORAGE_CLASS_NONE) {
2745 previous_declaration->storage_class = STORAGE_CLASS_NONE;
2749 if (old_storage_class == new_storage_class) {
2750 errorf(declaration->source_position,
2751 "redeclaration of '%Y'", symbol);
2753 errorf(declaration->source_position,
2754 "redeclaration of '%Y' with different linkage",
2757 errorf(previous_declaration->source_position,
2758 "previous declaration of '%Y' was here", symbol);
2761 return previous_declaration;
2763 } else if (is_function_definition) {
2764 if (declaration->storage_class != STORAGE_CLASS_STATIC) {
2765 if (warning.missing_prototypes && !is_sym_main(symbol)) {
2766 warningf(declaration->source_position,
2767 "no previous prototype for '%#T'", orig_type, symbol);
2768 } else if (warning.missing_declarations && !is_sym_main(symbol)) {
2769 warningf(declaration->source_position,
2770 "no previous declaration for '%#T'", orig_type,
2774 } else if (warning.missing_declarations &&
2775 scope == global_scope &&
2776 !is_type_function(type) && (
2777 declaration->storage_class == STORAGE_CLASS_NONE ||
2778 declaration->storage_class == STORAGE_CLASS_THREAD
2780 warningf(declaration->source_position,
2781 "no previous declaration for '%#T'", orig_type, symbol);
2784 assert(declaration->parent_scope == NULL);
2785 assert(scope != NULL);
2787 declaration->parent_scope = scope;
2789 environment_push(declaration);
2790 return append_declaration(declaration);
2793 static declaration_t *record_declaration(declaration_t *declaration)
2795 return internal_record_declaration(declaration, false);
2798 static declaration_t *record_function_definition(declaration_t *declaration)
2800 return internal_record_declaration(declaration, true);
2803 static void parser_error_multiple_definition(declaration_t *declaration,
2804 const source_position_t source_position)
2806 errorf(source_position, "multiple definition of symbol '%Y'",
2807 declaration->symbol);
2808 errorf(declaration->source_position,
2809 "this is the location of the previous definition.");
2812 static bool is_declaration_specifier(const token_t *token,
2813 bool only_type_specifiers)
2815 switch(token->type) {
2819 return is_typedef_symbol(token->v.symbol);
2821 case T___extension__:
2824 return !only_type_specifiers;
2831 static void parse_init_declarator_rest(declaration_t *declaration)
2835 type_t *orig_type = declaration->type;
2836 type_t *type = skip_typeref(orig_type);
2838 if(declaration->init.initializer != NULL) {
2839 parser_error_multiple_definition(declaration, token.source_position);
2842 bool must_be_constant = false;
2843 if(declaration->storage_class == STORAGE_CLASS_STATIC
2844 || declaration->storage_class == STORAGE_CLASS_THREAD_STATIC
2845 || declaration->parent_scope == global_scope) {
2846 must_be_constant = true;
2849 parse_initializer_env_t env;
2850 env.type = orig_type;
2851 env.must_be_constant = must_be_constant;
2852 parse_initializer(&env);
2854 if(env.type != orig_type) {
2855 orig_type = env.type;
2856 type = skip_typeref(orig_type);
2857 declaration->type = env.type;
2860 if(is_type_function(type)) {
2861 errorf(declaration->source_position,
2862 "initializers not allowed for function types at declator '%Y' (type '%T')",
2863 declaration->symbol, orig_type);
2865 declaration->init.initializer = env.initializer;
2869 /* parse rest of a declaration without any declarator */
2870 static void parse_anonymous_declaration_rest(
2871 const declaration_specifiers_t *specifiers,
2872 parsed_declaration_func finished_declaration)
2876 declaration_t *const declaration = allocate_declaration_zero();
2877 declaration->type = specifiers->type;
2878 declaration->storage_class = specifiers->storage_class;
2879 declaration->source_position = specifiers->source_position;
2881 if (declaration->storage_class != STORAGE_CLASS_NONE) {
2882 warningf(declaration->source_position, "useless storage class in empty declaration");
2885 type_t *type = declaration->type;
2886 switch (type->kind) {
2887 case TYPE_COMPOUND_STRUCT:
2888 case TYPE_COMPOUND_UNION: {
2889 if (type->compound.declaration->symbol == NULL) {
2890 warningf(declaration->source_position, "unnamed struct/union that defines no instances");
2899 warningf(declaration->source_position, "empty declaration");
2903 finished_declaration(declaration);
2906 static void parse_declaration_rest(declaration_t *ndeclaration,
2907 const declaration_specifiers_t *specifiers,
2908 parsed_declaration_func finished_declaration)
2911 declaration_t *declaration = finished_declaration(ndeclaration);
2913 type_t *orig_type = declaration->type;
2914 type_t *type = skip_typeref(orig_type);
2916 if (type->kind != TYPE_FUNCTION &&
2917 declaration->is_inline &&
2918 is_type_valid(type)) {
2919 warningf(declaration->source_position,
2920 "variable '%Y' declared 'inline'\n", declaration->symbol);
2923 if(token.type == '=') {
2924 parse_init_declarator_rest(declaration);
2927 if(token.type != ',')
2931 ndeclaration = parse_declarator(specifiers, /*may_be_abstract=*/false);
2936 static declaration_t *finished_kr_declaration(declaration_t *declaration)
2938 symbol_t *symbol = declaration->symbol;
2939 if(symbol == NULL) {
2940 errorf(HERE, "anonymous declaration not valid as function parameter");
2943 namespace_t namespc = (namespace_t) declaration->namespc;
2944 if(namespc != NAMESPACE_NORMAL) {
2945 return record_declaration(declaration);
2948 declaration_t *previous_declaration = get_declaration(symbol, namespc);
2949 if(previous_declaration == NULL ||
2950 previous_declaration->parent_scope != scope) {
2951 errorf(HERE, "expected declaration of a function parameter, found '%Y'",
2956 if(previous_declaration->type == NULL) {
2957 previous_declaration->type = declaration->type;
2958 previous_declaration->storage_class = declaration->storage_class;
2959 previous_declaration->parent_scope = scope;
2960 return previous_declaration;
2962 return record_declaration(declaration);
2966 static void parse_declaration(parsed_declaration_func finished_declaration)
2968 declaration_specifiers_t specifiers;
2969 memset(&specifiers, 0, sizeof(specifiers));
2970 parse_declaration_specifiers(&specifiers);
2972 if(token.type == ';') {
2973 parse_anonymous_declaration_rest(&specifiers, append_declaration);
2975 declaration_t *declaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
2976 parse_declaration_rest(declaration, &specifiers, finished_declaration);
2980 static void parse_kr_declaration_list(declaration_t *declaration)
2982 type_t *type = skip_typeref(declaration->type);
2983 if(!is_type_function(type))
2986 if(!type->function.kr_style_parameters)
2989 /* push function parameters */
2990 int top = environment_top();
2991 scope_t *last_scope = scope;
2992 set_scope(&declaration->scope);
2994 declaration_t *parameter = declaration->scope.declarations;
2995 for( ; parameter != NULL; parameter = parameter->next) {
2996 assert(parameter->parent_scope == NULL);
2997 parameter->parent_scope = scope;
2998 environment_push(parameter);
3001 /* parse declaration list */
3002 while(is_declaration_specifier(&token, false)) {
3003 parse_declaration(finished_kr_declaration);
3006 /* pop function parameters */
3007 assert(scope == &declaration->scope);
3008 set_scope(last_scope);
3009 environment_pop_to(top);
3011 /* update function type */
3012 type_t *new_type = duplicate_type(type);
3013 new_type->function.kr_style_parameters = false;
3015 function_parameter_t *parameters = NULL;
3016 function_parameter_t *last_parameter = NULL;
3018 declaration_t *parameter_declaration = declaration->scope.declarations;
3019 for( ; parameter_declaration != NULL;
3020 parameter_declaration = parameter_declaration->next) {
3021 type_t *parameter_type = parameter_declaration->type;
3022 if(parameter_type == NULL) {
3024 errorf(HERE, "no type specified for function parameter '%Y'",
3025 parameter_declaration->symbol);
3027 if (warning.implicit_int) {
3028 warningf(HERE, "no type specified for function parameter '%Y', using 'int'",
3029 parameter_declaration->symbol);
3031 parameter_type = type_int;
3032 parameter_declaration->type = parameter_type;
3036 semantic_parameter(parameter_declaration);
3037 parameter_type = parameter_declaration->type;
3039 function_parameter_t *function_parameter
3040 = obstack_alloc(type_obst, sizeof(function_parameter[0]));
3041 memset(function_parameter, 0, sizeof(function_parameter[0]));
3043 function_parameter->type = parameter_type;
3044 if(last_parameter != NULL) {
3045 last_parameter->next = function_parameter;
3047 parameters = function_parameter;
3049 last_parameter = function_parameter;
3051 new_type->function.parameters = parameters;
3053 type = typehash_insert(new_type);
3054 if(type != new_type) {
3055 obstack_free(type_obst, new_type);
3058 declaration->type = type;
3061 static bool first_err = true;
3064 * When called with first_err set, prints the name of the current function,
3067 static void print_in_function(void) {
3070 diagnosticf("%s: In function '%Y':\n",
3071 current_function->source_position.input_name,
3072 current_function->symbol);
3077 * Check if all labels are defined in the current function.
3078 * Check if all labels are used in the current function.
3080 static void check_labels(void)
3082 for (const goto_statement_t *goto_statement = goto_first;
3083 goto_statement != NULL;
3084 goto_statement = goto_statement->next) {
3085 declaration_t *label = goto_statement->label;
3088 if (label->source_position.input_name == NULL) {
3089 print_in_function();
3090 errorf(goto_statement->base.source_position,
3091 "label '%Y' used but not defined", label->symbol);
3094 goto_first = goto_last = NULL;
3096 if (warning.unused_label) {
3097 for (const label_statement_t *label_statement = label_first;
3098 label_statement != NULL;
3099 label_statement = label_statement->next) {
3100 const declaration_t *label = label_statement->label;
3102 if (! label->used) {
3103 print_in_function();
3104 warningf(label_statement->base.source_position,
3105 "label '%Y' defined but not used", label->symbol);
3109 label_first = label_last = NULL;
3113 * Check declarations of current_function for unused entities.
3115 static void check_declarations(void)
3117 if (warning.unused_parameter) {
3118 const scope_t *scope = ¤t_function->scope;
3120 const declaration_t *parameter = scope->declarations;
3121 for (; parameter != NULL; parameter = parameter->next) {
3122 if (! parameter->used) {
3123 print_in_function();
3124 warningf(parameter->source_position,
3125 "unused parameter '%Y'", parameter->symbol);
3129 if (warning.unused_variable) {
3133 static void parse_external_declaration(void)
3135 /* function-definitions and declarations both start with declaration
3137 declaration_specifiers_t specifiers;
3138 memset(&specifiers, 0, sizeof(specifiers));
3139 parse_declaration_specifiers(&specifiers);
3141 /* must be a declaration */
3142 if(token.type == ';') {
3143 parse_anonymous_declaration_rest(&specifiers, append_declaration);
3147 /* declarator is common to both function-definitions and declarations */
3148 declaration_t *ndeclaration = parse_declarator(&specifiers, /*may_be_abstract=*/false);
3150 /* must be a declaration */
3151 if(token.type == ',' || token.type == '=' || token.type == ';') {
3152 parse_declaration_rest(ndeclaration, &specifiers, record_declaration);
3156 /* must be a function definition */
3157 parse_kr_declaration_list(ndeclaration);
3159 if(token.type != '{') {
3160 parse_error_expected("while parsing function definition", '{', 0);
3165 type_t *type = ndeclaration->type;
3167 /* note that we don't skip typerefs: the standard doesn't allow them here
3168 * (so we can't use is_type_function here) */
3169 if(type->kind != TYPE_FUNCTION) {
3170 if (is_type_valid(type)) {
3171 errorf(HERE, "declarator '%#T' has a body but is not a function type",
3172 type, ndeclaration->symbol);
3178 /* § 6.7.5.3 (14) a function definition with () means no
3179 * parameters (and not unspecified parameters) */
3180 if(type->function.unspecified_parameters) {
3181 type_t *duplicate = duplicate_type(type);
3182 duplicate->function.unspecified_parameters = false;
3184 type = typehash_insert(duplicate);
3185 if(type != duplicate) {
3186 obstack_free(type_obst, duplicate);
3188 ndeclaration->type = type;
3191 declaration_t *const declaration = record_function_definition(ndeclaration);
3192 if(ndeclaration != declaration) {
3193 declaration->scope = ndeclaration->scope;
3195 type = skip_typeref(declaration->type);
3197 /* push function parameters and switch scope */
3198 int top = environment_top();
3199 scope_t *last_scope = scope;
3200 set_scope(&declaration->scope);
3202 declaration_t *parameter = declaration->scope.declarations;
3203 for( ; parameter != NULL; parameter = parameter->next) {
3204 if(parameter->parent_scope == &ndeclaration->scope) {
3205 parameter->parent_scope = scope;
3207 assert(parameter->parent_scope == NULL
3208 || parameter->parent_scope == scope);
3209 parameter->parent_scope = scope;
3210 environment_push(parameter);
3213 if(declaration->init.statement != NULL) {
3214 parser_error_multiple_definition(declaration, token.source_position);
3216 goto end_of_parse_external_declaration;
3218 /* parse function body */
3219 int label_stack_top = label_top();
3220 declaration_t *old_current_function = current_function;
3221 current_function = declaration;
3223 declaration->init.statement = parse_compound_statement();
3226 check_declarations();
3228 assert(current_function == declaration);
3229 current_function = old_current_function;
3230 label_pop_to(label_stack_top);
3233 end_of_parse_external_declaration:
3234 assert(scope == &declaration->scope);
3235 set_scope(last_scope);
3236 environment_pop_to(top);
3239 static type_t *make_bitfield_type(type_t *base, expression_t *size,
3240 source_position_t source_position)
3242 type_t *type = allocate_type_zero(TYPE_BITFIELD, source_position);
3243 type->bitfield.base = base;
3244 type->bitfield.size = size;
3249 static declaration_t *find_compound_entry(declaration_t *compound_declaration,
3252 declaration_t *iter = compound_declaration->scope.declarations;
3253 for( ; iter != NULL; iter = iter->next) {
3254 if(iter->namespc != NAMESPACE_NORMAL)
3257 if(iter->symbol == NULL) {
3258 type_t *type = skip_typeref(iter->type);
3259 if(is_type_compound(type)) {
3260 declaration_t *result
3261 = find_compound_entry(type->compound.declaration, symbol);
3268 if(iter->symbol == symbol) {
3276 static void parse_compound_declarators(declaration_t *struct_declaration,
3277 const declaration_specifiers_t *specifiers)
3279 declaration_t *last_declaration = struct_declaration->scope.declarations;
3280 if(last_declaration != NULL) {
3281 while(last_declaration->next != NULL) {
3282 last_declaration = last_declaration->next;
3287 declaration_t *declaration;
3289 if(token.type == ':') {
3290 source_position_t source_position = HERE;
3293 type_t *base_type = specifiers->type;
3294 expression_t *size = parse_constant_expression();
3296 if(!is_type_integer(skip_typeref(base_type))) {
3297 errorf(HERE, "bitfield base type '%T' is not an integer type",
3301 type_t *type = make_bitfield_type(base_type, size, source_position);
3303 declaration = allocate_declaration_zero();
3304 declaration->namespc = NAMESPACE_NORMAL;
3305 declaration->storage_class = STORAGE_CLASS_NONE;
3306 declaration->source_position = source_position;
3307 declaration->modifiers = specifiers->decl_modifiers;
3308 declaration->type = type;
3310 declaration = parse_declarator(specifiers,/*may_be_abstract=*/true);
3312 type_t *orig_type = declaration->type;
3313 type_t *type = skip_typeref(orig_type);
3315 if(token.type == ':') {
3316 source_position_t source_position = HERE;
3318 expression_t *size = parse_constant_expression();
3320 if(!is_type_integer(type)) {
3321 errorf(HERE, "bitfield base type '%T' is not an "
3322 "integer type", orig_type);
3325 type_t *bitfield_type = make_bitfield_type(orig_type, size, source_position);
3326 declaration->type = bitfield_type;
3328 /* TODO we ignore arrays for now... what is missing is a check
3329 * that they're at the end of the struct */
3330 if(is_type_incomplete(type) && !is_type_array(type)) {
3332 "compound member '%Y' has incomplete type '%T'",
3333 declaration->symbol, orig_type);
3334 } else if(is_type_function(type)) {
3335 errorf(HERE, "compound member '%Y' must not have function "
3336 "type '%T'", declaration->symbol, orig_type);
3341 /* make sure we don't define a symbol multiple times */
3342 symbol_t *symbol = declaration->symbol;
3343 if(symbol != NULL) {
3344 declaration_t *prev_decl
3345 = find_compound_entry(struct_declaration, symbol);
3347 if(prev_decl != NULL) {
3348 assert(prev_decl->symbol == symbol);
3349 errorf(declaration->source_position,
3350 "multiple declarations of symbol '%Y'", symbol);
3351 errorf(prev_decl->source_position,
3352 "previous declaration of '%Y' was here", symbol);
3356 /* append declaration */
3357 if(last_declaration != NULL) {
3358 last_declaration->next = declaration;
3360 struct_declaration->scope.declarations = declaration;
3362 last_declaration = declaration;
3364 if(token.type != ',')
3371 static void parse_compound_type_entries(declaration_t *compound_declaration)
3375 while(token.type != '}' && token.type != T_EOF) {
3376 declaration_specifiers_t specifiers;
3377 memset(&specifiers, 0, sizeof(specifiers));
3378 parse_declaration_specifiers(&specifiers);
3380 parse_compound_declarators(compound_declaration, &specifiers);
3382 if(token.type == T_EOF) {
3383 errorf(HERE, "EOF while parsing struct");
3388 static type_t *parse_typename(void)
3390 declaration_specifiers_t specifiers;
3391 memset(&specifiers, 0, sizeof(specifiers));
3392 parse_declaration_specifiers(&specifiers);
3393 if(specifiers.storage_class != STORAGE_CLASS_NONE) {
3394 /* TODO: improve error message, user does probably not know what a
3395 * storage class is...
3397 errorf(HERE, "typename may not have a storage class");
3400 type_t *result = parse_abstract_declarator(specifiers.type);
3408 typedef expression_t* (*parse_expression_function) (unsigned precedence);
3409 typedef expression_t* (*parse_expression_infix_function) (unsigned precedence,
3410 expression_t *left);
3412 typedef struct expression_parser_function_t expression_parser_function_t;
3413 struct expression_parser_function_t {
3414 unsigned precedence;
3415 parse_expression_function parser;
3416 unsigned infix_precedence;
3417 parse_expression_infix_function infix_parser;
3420 expression_parser_function_t expression_parsers[T_LAST_TOKEN];
3423 * Creates a new invalid expression.
3425 static expression_t *create_invalid_expression(void)
3427 expression_t *expression = allocate_expression_zero(EXPR_INVALID);
3428 expression->base.source_position = token.source_position;
3433 * Prints an error message if an expression was expected but not read
3435 static expression_t *expected_expression_error(void)
3437 /* skip the error message if the error token was read */
3438 if (token.type != T_ERROR) {
3439 errorf(HERE, "expected expression, got token '%K'", &token);
3443 return create_invalid_expression();
3447 * Parse a string constant.
3449 static expression_t *parse_string_const(void)
3452 if (token.type == T_STRING_LITERAL) {
3453 string_t res = token.v.string;
3455 while (token.type == T_STRING_LITERAL) {
3456 res = concat_strings(&res, &token.v.string);
3459 if (token.type != T_WIDE_STRING_LITERAL) {
3460 expression_t *const cnst = allocate_expression_zero(EXPR_STRING_LITERAL);
3461 cnst->base.type = type_char_ptr;
3462 cnst->string.value = res;
3466 wres = concat_string_wide_string(&res, &token.v.wide_string);
3468 wres = token.v.wide_string;
3473 switch (token.type) {
3474 case T_WIDE_STRING_LITERAL:
3475 wres = concat_wide_strings(&wres, &token.v.wide_string);
3478 case T_STRING_LITERAL:
3479 wres = concat_wide_string_string(&wres, &token.v.string);
3483 expression_t *const cnst = allocate_expression_zero(EXPR_WIDE_STRING_LITERAL);
3484 cnst->base.type = type_wchar_t_ptr;
3485 cnst->wide_string.value = wres;
3494 * Parse an integer constant.
3496 static expression_t *parse_int_const(void)
3498 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3499 cnst->base.source_position = HERE;
3500 cnst->base.type = token.datatype;
3501 cnst->conste.v.int_value = token.v.intvalue;
3509 * Parse a character constant.
3511 static expression_t *parse_char_const(void)
3513 expression_t *cnst = allocate_expression_zero(EXPR_CHAR_CONST);
3514 cnst->base.source_position = HERE;
3515 cnst->base.type = token.datatype;
3516 cnst->conste.v.chars.begin = token.v.string.begin;
3517 cnst->conste.v.chars.size = token.v.string.size;
3519 if (cnst->conste.v.chars.size != 1) {
3520 if (warning.multichar && (c_mode & _GNUC)) {
3522 warningf(HERE, "multi-character character constant");
3524 errorf(HERE, "more than 1 characters in character constant");
3533 * Parse a float constant.
3535 static expression_t *parse_float_const(void)
3537 expression_t *cnst = allocate_expression_zero(EXPR_CONST);
3538 cnst->base.type = token.datatype;
3539 cnst->conste.v.float_value = token.v.floatvalue;
3546 static declaration_t *create_implicit_function(symbol_t *symbol,
3547 const source_position_t source_position)
3549 type_t *ntype = allocate_type_zero(TYPE_FUNCTION, source_position);
3550 ntype->function.return_type = type_int;
3551 ntype->function.unspecified_parameters = true;
3553 type_t *type = typehash_insert(ntype);
3558 declaration_t *const declaration = allocate_declaration_zero();
3559 declaration->storage_class = STORAGE_CLASS_EXTERN;
3560 declaration->type = type;
3561 declaration->symbol = symbol;
3562 declaration->source_position = source_position;
3563 declaration->parent_scope = global_scope;
3565 scope_t *old_scope = scope;
3566 set_scope(global_scope);
3568 environment_push(declaration);
3569 /* prepends the declaration to the global declarations list */
3570 declaration->next = scope->declarations;
3571 scope->declarations = declaration;
3573 assert(scope == global_scope);
3574 set_scope(old_scope);
3580 * Creates a return_type (func)(argument_type) function type if not
3583 * @param return_type the return type
3584 * @param argument_type the argument type
3586 static type_t *make_function_1_type(type_t *return_type, type_t *argument_type)
3588 function_parameter_t *parameter
3589 = obstack_alloc(type_obst, sizeof(parameter[0]));
3590 memset(parameter, 0, sizeof(parameter[0]));
3591 parameter->type = argument_type;
3593 type_t *type = allocate_type_zero(TYPE_FUNCTION, builtin_source_position);
3594 type->function.return_type = return_type;
3595 type->function.parameters = parameter;
3597 type_t *result = typehash_insert(type);
3598 if(result != type) {
3606 * Creates a function type for some function like builtins.
3608 * @param symbol the symbol describing the builtin
3610 static type_t *get_builtin_symbol_type(symbol_t *symbol)
3612 switch(symbol->ID) {
3613 case T___builtin_alloca:
3614 return make_function_1_type(type_void_ptr, type_size_t);
3615 case T___builtin_nan:
3616 return make_function_1_type(type_double, type_char_ptr);
3617 case T___builtin_nanf:
3618 return make_function_1_type(type_float, type_char_ptr);
3619 case T___builtin_nand:
3620 return make_function_1_type(type_long_double, type_char_ptr);
3621 case T___builtin_va_end:
3622 return make_function_1_type(type_void, type_valist);
3624 panic("not implemented builtin symbol found");
3629 * Performs automatic type cast as described in § 6.3.2.1.
3631 * @param orig_type the original type
3633 static type_t *automatic_type_conversion(type_t *orig_type)
3635 type_t *type = skip_typeref(orig_type);
3636 if(is_type_array(type)) {
3637 array_type_t *array_type = &type->array;
3638 type_t *element_type = array_type->element_type;
3639 unsigned qualifiers = array_type->type.qualifiers;
3641 return make_pointer_type(element_type, qualifiers);
3644 if(is_type_function(type)) {
3645 return make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
3652 * reverts the automatic casts of array to pointer types and function
3653 * to function-pointer types as defined § 6.3.2.1
3655 type_t *revert_automatic_type_conversion(const expression_t *expression)
3657 switch (expression->kind) {
3658 case EXPR_REFERENCE: return expression->reference.declaration->type;
3659 case EXPR_SELECT: return expression->select.compound_entry->type;
3661 case EXPR_UNARY_DEREFERENCE: {
3662 const expression_t *const value = expression->unary.value;
3663 type_t *const type = skip_typeref(value->base.type);
3664 assert(is_type_pointer(type));
3665 return type->pointer.points_to;
3668 case EXPR_BUILTIN_SYMBOL:
3669 return get_builtin_symbol_type(expression->builtin_symbol.symbol);
3671 case EXPR_ARRAY_ACCESS: {
3672 const expression_t *array_ref = expression->array_access.array_ref;
3673 type_t *type_left = skip_typeref(array_ref->base.type);
3674 if (!is_type_valid(type_left))
3676 assert(is_type_pointer(type_left));
3677 return type_left->pointer.points_to;
3680 case EXPR_COMPOUND_LITERAL:
3681 return expression->compound_literal.type;
3686 return expression->base.type;
3689 static expression_t *parse_reference(void)
3691 expression_t *expression = allocate_expression_zero(EXPR_REFERENCE);
3693 reference_expression_t *ref = &expression->reference;
3694 ref->symbol = token.v.symbol;
3696 declaration_t *declaration = get_declaration(ref->symbol, NAMESPACE_NORMAL);
3698 source_position_t source_position = token.source_position;
3701 if(declaration == NULL) {
3702 if (! strict_mode && token.type == '(') {
3703 /* an implicitly defined function */
3704 if (warning.implicit_function_declaration) {
3705 warningf(HERE, "implicit declaration of function '%Y'",
3709 declaration = create_implicit_function(ref->symbol,
3712 errorf(HERE, "unknown symbol '%Y' found.", ref->symbol);
3713 return create_invalid_expression();
3717 type_t *type = declaration->type;
3719 /* we always do the auto-type conversions; the & and sizeof parser contains
3720 * code to revert this! */
3721 type = automatic_type_conversion(type);
3723 ref->declaration = declaration;
3724 ref->base.type = type;
3726 /* this declaration is used */
3727 declaration->used = true;
3732 static void check_cast_allowed(expression_t *expression, type_t *dest_type)
3736 /* TODO check if explicit cast is allowed and issue warnings/errors */
3739 static expression_t *parse_compound_literal(type_t *type)
3741 expression_t *expression = allocate_expression_zero(EXPR_COMPOUND_LITERAL);
3743 parse_initializer_env_t env;
3745 env.must_be_constant = false;
3746 parse_initializer(&env);
3749 expression->compound_literal.type = type;
3750 expression->compound_literal.initializer = env.initializer;
3751 expression->base.type = automatic_type_conversion(type);
3756 static expression_t *parse_cast(void)
3758 source_position_t source_position = token.source_position;
3760 type_t *type = parse_typename();
3764 if(token.type == '{') {
3765 return parse_compound_literal(type);
3768 expression_t *cast = allocate_expression_zero(EXPR_UNARY_CAST);
3769 cast->base.source_position = source_position;
3771 expression_t *value = parse_sub_expression(20);
3773 check_cast_allowed(value, type);
3775 cast->base.type = type;
3776 cast->unary.value = value;
3781 static expression_t *parse_statement_expression(void)
3783 expression_t *expression = allocate_expression_zero(EXPR_STATEMENT);
3785 statement_t *statement = parse_compound_statement();
3786 expression->statement.statement = statement;
3787 expression->base.source_position = statement->base.source_position;
3789 /* find last statement and use its type */
3790 type_t *type = type_void;
3791 const statement_t *stmt = statement->compound.statements;
3793 while (stmt->base.next != NULL)
3794 stmt = stmt->base.next;
3796 if (stmt->kind == STATEMENT_EXPRESSION) {
3797 type = stmt->expression.expression->base.type;
3800 warningf(expression->base.source_position, "empty statement expression ({})");
3802 expression->base.type = type;
3809 static expression_t *parse_brace_expression(void)
3813 switch(token.type) {
3815 /* gcc extension: a statement expression */
3816 return parse_statement_expression();
3820 return parse_cast();
3822 if(is_typedef_symbol(token.v.symbol)) {
3823 return parse_cast();
3827 expression_t *result = parse_expression();
3833 static expression_t *parse_function_keyword(void)
3838 if (current_function == NULL) {
3839 errorf(HERE, "'__func__' used outside of a function");
3842 expression_t *expression = allocate_expression_zero(EXPR_FUNCTION);
3843 expression->base.type = type_char_ptr;
3848 static expression_t *parse_pretty_function_keyword(void)
3850 eat(T___PRETTY_FUNCTION__);
3853 if (current_function == NULL) {
3854 errorf(HERE, "'__PRETTY_FUNCTION__' used outside of a function");
3857 expression_t *expression = allocate_expression_zero(EXPR_PRETTY_FUNCTION);
3858 expression->base.type = type_char_ptr;
3863 static designator_t *parse_designator(void)
3865 designator_t *result = allocate_ast_zero(sizeof(result[0]));
3866 result->source_position = HERE;
3868 if(token.type != T_IDENTIFIER) {
3869 parse_error_expected("while parsing member designator",
3874 result->symbol = token.v.symbol;
3877 designator_t *last_designator = result;
3879 if(token.type == '.') {
3881 if(token.type != T_IDENTIFIER) {
3882 parse_error_expected("while parsing member designator",
3887 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3888 designator->source_position = HERE;
3889 designator->symbol = token.v.symbol;
3892 last_designator->next = designator;
3893 last_designator = designator;
3896 if(token.type == '[') {
3898 designator_t *designator = allocate_ast_zero(sizeof(result[0]));
3899 designator->source_position = HERE;
3900 designator->array_index = parse_expression();
3901 if(designator->array_index == NULL) {
3907 last_designator->next = designator;
3908 last_designator = designator;
3917 static expression_t *parse_offsetof(void)
3919 eat(T___builtin_offsetof);
3921 expression_t *expression = allocate_expression_zero(EXPR_OFFSETOF);
3922 expression->base.type = type_size_t;
3925 type_t *type = parse_typename();
3927 designator_t *designator = parse_designator();
3930 expression->offsetofe.type = type;
3931 expression->offsetofe.designator = designator;
3934 memset(&path, 0, sizeof(path));
3935 path.top_type = type;
3936 path.path = NEW_ARR_F(type_path_entry_t, 0);
3938 descend_into_subtype(&path);
3940 if(!walk_designator(&path, designator, true)) {
3941 return create_invalid_expression();
3944 DEL_ARR_F(path.path);
3949 static expression_t *parse_va_start(void)
3951 eat(T___builtin_va_start);
3953 expression_t *expression = allocate_expression_zero(EXPR_VA_START);
3956 expression->va_starte.ap = parse_assignment_expression();
3958 expression_t *const expr = parse_assignment_expression();
3959 if (expr->kind == EXPR_REFERENCE) {
3960 declaration_t *const decl = expr->reference.declaration;
3962 return create_invalid_expression();
3963 if (decl->parent_scope == ¤t_function->scope &&
3964 decl->next == NULL) {
3965 expression->va_starte.parameter = decl;
3970 errorf(expr->base.source_position, "second argument of 'va_start' must be last parameter of the current function");
3972 return create_invalid_expression();
3975 static expression_t *parse_va_arg(void)
3977 eat(T___builtin_va_arg);
3979 expression_t *expression = allocate_expression_zero(EXPR_VA_ARG);
3982 expression->va_arge.ap = parse_assignment_expression();
3984 expression->base.type = parse_typename();
3990 static expression_t *parse_builtin_symbol(void)
3992 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_SYMBOL);
3994 symbol_t *symbol = token.v.symbol;
3996 expression->builtin_symbol.symbol = symbol;
3999 type_t *type = get_builtin_symbol_type(symbol);
4000 type = automatic_type_conversion(type);
4002 expression->base.type = type;
4006 static expression_t *parse_builtin_constant(void)
4008 eat(T___builtin_constant_p);
4010 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_CONSTANT_P);
4013 expression->builtin_constant.value = parse_assignment_expression();
4015 expression->base.type = type_int;
4020 static expression_t *parse_builtin_prefetch(void)
4022 eat(T___builtin_prefetch);
4024 expression_t *expression = allocate_expression_zero(EXPR_BUILTIN_PREFETCH);
4027 expression->builtin_prefetch.adr = parse_assignment_expression();
4028 if (token.type == ',') {
4030 expression->builtin_prefetch.rw = parse_assignment_expression();
4032 if (token.type == ',') {
4034 expression->builtin_prefetch.locality = parse_assignment_expression();
4037 expression->base.type = type_void;
4042 static expression_t *parse_compare_builtin(void)
4044 expression_t *expression;
4046 switch(token.type) {
4047 case T___builtin_isgreater:
4048 expression = allocate_expression_zero(EXPR_BINARY_ISGREATER);
4050 case T___builtin_isgreaterequal:
4051 expression = allocate_expression_zero(EXPR_BINARY_ISGREATEREQUAL);
4053 case T___builtin_isless:
4054 expression = allocate_expression_zero(EXPR_BINARY_ISLESS);
4056 case T___builtin_islessequal:
4057 expression = allocate_expression_zero(EXPR_BINARY_ISLESSEQUAL);
4059 case T___builtin_islessgreater:
4060 expression = allocate_expression_zero(EXPR_BINARY_ISLESSGREATER);
4062 case T___builtin_isunordered:
4063 expression = allocate_expression_zero(EXPR_BINARY_ISUNORDERED);
4066 panic("invalid compare builtin found");
4069 expression->base.source_position = HERE;
4073 expression->binary.left = parse_assignment_expression();
4075 expression->binary.right = parse_assignment_expression();
4078 type_t *const orig_type_left = expression->binary.left->base.type;
4079 type_t *const orig_type_right = expression->binary.right->base.type;
4081 type_t *const type_left = skip_typeref(orig_type_left);
4082 type_t *const type_right = skip_typeref(orig_type_right);
4083 if(!is_type_float(type_left) && !is_type_float(type_right)) {
4084 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4085 type_error_incompatible("invalid operands in comparison",
4086 expression->base.source_position, orig_type_left, orig_type_right);
4089 semantic_comparison(&expression->binary);
4095 static expression_t *parse_builtin_expect(void)
4097 eat(T___builtin_expect);
4099 expression_t *expression
4100 = allocate_expression_zero(EXPR_BINARY_BUILTIN_EXPECT);
4103 expression->binary.left = parse_assignment_expression();
4105 expression->binary.right = parse_constant_expression();
4108 expression->base.type = expression->binary.left->base.type;
4113 static expression_t *parse_assume(void) {
4116 expression_t *expression
4117 = allocate_expression_zero(EXPR_UNARY_ASSUME);
4120 expression->unary.value = parse_assignment_expression();
4123 expression->base.type = type_void;
4127 static expression_t *parse_primary_expression(void)
4129 switch (token.type) {
4130 case T_INTEGER: return parse_int_const();
4131 case T_CHARS: return parse_char_const();
4132 case T_FLOATINGPOINT: return parse_float_const();
4133 case T_STRING_LITERAL:
4134 case T_WIDE_STRING_LITERAL: return parse_string_const();
4135 case T_IDENTIFIER: return parse_reference();
4136 case T___FUNCTION__:
4137 case T___func__: return parse_function_keyword();
4138 case T___PRETTY_FUNCTION__: return parse_pretty_function_keyword();
4139 case T___builtin_offsetof: return parse_offsetof();
4140 case T___builtin_va_start: return parse_va_start();
4141 case T___builtin_va_arg: return parse_va_arg();
4142 case T___builtin_expect: return parse_builtin_expect();
4143 case T___builtin_alloca:
4144 case T___builtin_nan:
4145 case T___builtin_nand:
4146 case T___builtin_nanf:
4147 case T___builtin_va_end: return parse_builtin_symbol();
4148 case T___builtin_isgreater:
4149 case T___builtin_isgreaterequal:
4150 case T___builtin_isless:
4151 case T___builtin_islessequal:
4152 case T___builtin_islessgreater:
4153 case T___builtin_isunordered: return parse_compare_builtin();
4154 case T___builtin_constant_p: return parse_builtin_constant();
4155 case T___builtin_prefetch: return parse_builtin_prefetch();
4156 case T_assume: return parse_assume();
4158 case '(': return parse_brace_expression();
4161 errorf(HERE, "unexpected token %K, expected an expression", &token);
4164 return create_invalid_expression();
4168 * Check if the expression has the character type and issue a warning then.
4170 static void check_for_char_index_type(const expression_t *expression) {
4171 type_t *const type = expression->base.type;
4172 const type_t *const base_type = skip_typeref(type);
4174 if (is_type_atomic(base_type, ATOMIC_TYPE_CHAR) &&
4175 warning.char_subscripts) {
4176 warningf(expression->base.source_position,
4177 "array subscript has type '%T'", type);
4181 static expression_t *parse_array_expression(unsigned precedence,
4188 expression_t *inside = parse_expression();
4190 expression_t *expression = allocate_expression_zero(EXPR_ARRAY_ACCESS);
4192 array_access_expression_t *array_access = &expression->array_access;
4194 type_t *const orig_type_left = left->base.type;
4195 type_t *const orig_type_inside = inside->base.type;
4197 type_t *const type_left = skip_typeref(orig_type_left);
4198 type_t *const type_inside = skip_typeref(orig_type_inside);
4200 type_t *return_type;
4201 if (is_type_pointer(type_left)) {
4202 return_type = type_left->pointer.points_to;
4203 array_access->array_ref = left;
4204 array_access->index = inside;
4205 check_for_char_index_type(inside);
4206 } else if (is_type_pointer(type_inside)) {
4207 return_type = type_inside->pointer.points_to;
4208 array_access->array_ref = inside;
4209 array_access->index = left;
4210 array_access->flipped = true;
4211 check_for_char_index_type(left);
4213 if (is_type_valid(type_left) && is_type_valid(type_inside)) {
4215 "array access on object with non-pointer types '%T', '%T'",
4216 orig_type_left, orig_type_inside);
4218 return_type = type_error_type;
4219 array_access->array_ref = create_invalid_expression();
4222 if(token.type != ']') {
4223 parse_error_expected("Problem while parsing array access", ']', 0);
4228 return_type = automatic_type_conversion(return_type);
4229 expression->base.type = return_type;
4234 static expression_t *parse_typeprop(expression_kind_t kind, unsigned precedence)
4236 expression_t *tp_expression = allocate_expression_zero(kind);
4237 tp_expression->base.type = type_size_t;
4239 if(token.type == '(' && is_declaration_specifier(look_ahead(1), true)) {
4241 tp_expression->typeprop.type = parse_typename();
4244 expression_t *expression = parse_sub_expression(precedence);
4245 expression->base.type = revert_automatic_type_conversion(expression);
4247 tp_expression->typeprop.type = expression->base.type;
4248 tp_expression->typeprop.tp_expression = expression;
4251 return tp_expression;
4254 static expression_t *parse_sizeof(unsigned precedence)
4257 return parse_typeprop(EXPR_SIZEOF, precedence);
4260 static expression_t *parse_alignof(unsigned precedence)
4263 return parse_typeprop(EXPR_SIZEOF, precedence);
4266 static expression_t *parse_select_expression(unsigned precedence,
4267 expression_t *compound)
4270 assert(token.type == '.' || token.type == T_MINUSGREATER);
4272 bool is_pointer = (token.type == T_MINUSGREATER);
4275 expression_t *select = allocate_expression_zero(EXPR_SELECT);
4276 select->select.compound = compound;
4278 if(token.type != T_IDENTIFIER) {
4279 parse_error_expected("while parsing select", T_IDENTIFIER, 0);
4282 symbol_t *symbol = token.v.symbol;
4283 select->select.symbol = symbol;
4286 type_t *const orig_type = compound->base.type;
4287 type_t *const type = skip_typeref(orig_type);
4289 type_t *type_left = type;
4291 if (!is_type_pointer(type)) {
4292 if (is_type_valid(type)) {
4293 errorf(HERE, "left hand side of '->' is not a pointer, but '%T'", orig_type);
4295 return create_invalid_expression();
4297 type_left = type->pointer.points_to;
4299 type_left = skip_typeref(type_left);
4301 if (type_left->kind != TYPE_COMPOUND_STRUCT &&
4302 type_left->kind != TYPE_COMPOUND_UNION) {
4303 if (is_type_valid(type_left)) {
4304 errorf(HERE, "request for member '%Y' in something not a struct or "
4305 "union, but '%T'", symbol, type_left);
4307 return create_invalid_expression();
4310 declaration_t *const declaration = type_left->compound.declaration;
4312 if(!declaration->init.is_defined) {
4313 errorf(HERE, "request for member '%Y' of incomplete type '%T'",
4315 return create_invalid_expression();
4318 declaration_t *iter = find_compound_entry(declaration, symbol);
4320 errorf(HERE, "'%T' has no member named '%Y'", orig_type, symbol);
4321 return create_invalid_expression();
4324 /* we always do the auto-type conversions; the & and sizeof parser contains
4325 * code to revert this! */
4326 type_t *expression_type = automatic_type_conversion(iter->type);
4328 select->select.compound_entry = iter;
4329 select->base.type = expression_type;
4331 if(expression_type->kind == TYPE_BITFIELD) {
4332 expression_t *extract
4333 = allocate_expression_zero(EXPR_UNARY_BITFIELD_EXTRACT);
4334 extract->unary.value = select;
4335 extract->base.type = expression_type->bitfield.base;
4344 * Parse a call expression, ie. expression '( ... )'.
4346 * @param expression the function address
4348 static expression_t *parse_call_expression(unsigned precedence,
4349 expression_t *expression)
4352 expression_t *result = allocate_expression_zero(EXPR_CALL);
4354 call_expression_t *call = &result->call;
4355 call->function = expression;
4357 type_t *const orig_type = expression->base.type;
4358 type_t *const type = skip_typeref(orig_type);
4360 function_type_t *function_type = NULL;
4361 if (is_type_pointer(type)) {
4362 type_t *const to_type = skip_typeref(type->pointer.points_to);
4364 if (is_type_function(to_type)) {
4365 function_type = &to_type->function;
4366 call->base.type = function_type->return_type;
4370 if (function_type == NULL && is_type_valid(type)) {
4371 errorf(HERE, "called object '%E' (type '%T') is not a pointer to a function", expression, orig_type);
4374 /* parse arguments */
4377 if(token.type != ')') {
4378 call_argument_t *last_argument = NULL;
4381 call_argument_t *argument = allocate_ast_zero(sizeof(argument[0]));
4383 argument->expression = parse_assignment_expression();
4384 if(last_argument == NULL) {
4385 call->arguments = argument;
4387 last_argument->next = argument;
4389 last_argument = argument;
4391 if(token.type != ',')
4398 if(function_type != NULL) {
4399 function_parameter_t *parameter = function_type->parameters;
4400 call_argument_t *argument = call->arguments;
4401 for( ; parameter != NULL && argument != NULL;
4402 parameter = parameter->next, argument = argument->next) {
4403 type_t *expected_type = parameter->type;
4404 /* TODO report scope in error messages */
4405 expression_t *const arg_expr = argument->expression;
4406 type_t *const res_type = semantic_assign(expected_type, arg_expr, "function call");
4407 if (res_type == NULL) {
4408 /* TODO improve error message */
4409 errorf(arg_expr->base.source_position,
4410 "Cannot call function with argument '%E' of type '%T' where type '%T' is expected",
4411 arg_expr, arg_expr->base.type, expected_type);
4413 argument->expression = create_implicit_cast(argument->expression, expected_type);
4416 /* too few parameters */
4417 if(parameter != NULL) {
4418 errorf(HERE, "too few arguments to function '%E'", expression);
4419 } else if(argument != NULL) {
4420 /* too many parameters */
4421 if(!function_type->variadic
4422 && !function_type->unspecified_parameters) {
4423 errorf(HERE, "too many arguments to function '%E'", expression);
4425 /* do default promotion */
4426 for( ; argument != NULL; argument = argument->next) {
4427 type_t *type = argument->expression->base.type;
4429 type = skip_typeref(type);
4430 if(is_type_integer(type)) {
4431 type = promote_integer(type);
4432 } else if(type == type_float) {
4436 argument->expression
4437 = create_implicit_cast(argument->expression, type);
4440 check_format(&result->call);
4443 check_format(&result->call);
4450 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right);
4452 static bool same_compound_type(const type_t *type1, const type_t *type2)
4455 is_type_compound(type1) &&
4456 type1->kind == type2->kind &&
4457 type1->compound.declaration == type2->compound.declaration;
4461 * Parse a conditional expression, ie. 'expression ? ... : ...'.
4463 * @param expression the conditional expression
4465 static expression_t *parse_conditional_expression(unsigned precedence,
4466 expression_t *expression)
4470 expression_t *result = allocate_expression_zero(EXPR_CONDITIONAL);
4472 conditional_expression_t *conditional = &result->conditional;
4473 conditional->condition = expression;
4476 type_t *const condition_type_orig = expression->base.type;
4477 type_t *const condition_type = skip_typeref(condition_type_orig);
4478 if (!is_type_scalar(condition_type) && is_type_valid(condition_type)) {
4479 type_error("expected a scalar type in conditional condition",
4480 expression->base.source_position, condition_type_orig);
4483 expression_t *true_expression = parse_expression();
4485 expression_t *false_expression = parse_sub_expression(precedence);
4487 type_t *const orig_true_type = true_expression->base.type;
4488 type_t *const orig_false_type = false_expression->base.type;
4489 type_t *const true_type = skip_typeref(orig_true_type);
4490 type_t *const false_type = skip_typeref(orig_false_type);
4493 type_t *result_type;
4494 if (is_type_arithmetic(true_type) && is_type_arithmetic(false_type)) {
4495 result_type = semantic_arithmetic(true_type, false_type);
4497 true_expression = create_implicit_cast(true_expression, result_type);
4498 false_expression = create_implicit_cast(false_expression, result_type);
4500 conditional->true_expression = true_expression;
4501 conditional->false_expression = false_expression;
4502 conditional->base.type = result_type;
4503 } else if (same_compound_type(true_type, false_type) || (
4504 is_type_atomic(true_type, ATOMIC_TYPE_VOID) &&
4505 is_type_atomic(false_type, ATOMIC_TYPE_VOID)
4507 /* just take 1 of the 2 types */
4508 result_type = true_type;
4509 } else if (is_type_pointer(true_type) && is_type_pointer(false_type)
4510 && pointers_compatible(true_type, false_type)) {
4512 result_type = true_type;
4513 } else if (is_type_pointer(true_type)
4514 && is_null_pointer_constant(false_expression)) {
4515 result_type = true_type;
4516 } else if (is_type_pointer(false_type)
4517 && is_null_pointer_constant(true_expression)) {
4518 result_type = false_type;
4520 /* TODO: one pointer to void*, other some pointer */
4522 if (is_type_valid(true_type) && is_type_valid(false_type)) {
4523 type_error_incompatible("while parsing conditional",
4524 expression->base.source_position, true_type,
4527 result_type = type_error_type;
4530 conditional->true_expression
4531 = create_implicit_cast(true_expression, result_type);
4532 conditional->false_expression
4533 = create_implicit_cast(false_expression, result_type);
4534 conditional->base.type = result_type;
4539 * Parse an extension expression.
4541 static expression_t *parse_extension(unsigned precedence)
4543 eat(T___extension__);
4545 /* TODO enable extensions */
4546 expression_t *expression = parse_sub_expression(precedence);
4547 /* TODO disable extensions */
4551 static expression_t *parse_builtin_classify_type(const unsigned precedence)
4553 eat(T___builtin_classify_type);
4555 expression_t *result = allocate_expression_zero(EXPR_CLASSIFY_TYPE);
4556 result->base.type = type_int;
4559 expression_t *expression = parse_sub_expression(precedence);
4561 result->classify_type.type_expression = expression;
4566 static void semantic_incdec(unary_expression_t *expression)
4568 type_t *const orig_type = expression->value->base.type;
4569 type_t *const type = skip_typeref(orig_type);
4570 /* TODO !is_type_real && !is_type_pointer */
4571 if(!is_type_arithmetic(type) && type->kind != TYPE_POINTER) {
4572 if (is_type_valid(type)) {
4573 /* TODO: improve error message */
4574 errorf(HERE, "operation needs an arithmetic or pointer type");
4579 expression->base.type = orig_type;
4582 static void semantic_unexpr_arithmetic(unary_expression_t *expression)
4584 type_t *const orig_type = expression->value->base.type;
4585 type_t *const type = skip_typeref(orig_type);
4586 if(!is_type_arithmetic(type)) {
4587 if (is_type_valid(type)) {
4588 /* TODO: improve error message */
4589 errorf(HERE, "operation needs an arithmetic type");
4594 expression->base.type = orig_type;
4597 static void semantic_unexpr_scalar(unary_expression_t *expression)
4599 type_t *const orig_type = expression->value->base.type;
4600 type_t *const type = skip_typeref(orig_type);
4601 if (!is_type_scalar(type)) {
4602 if (is_type_valid(type)) {
4603 errorf(HERE, "operand of ! must be of scalar type");
4608 expression->base.type = orig_type;
4611 static void semantic_unexpr_integer(unary_expression_t *expression)
4613 type_t *const orig_type = expression->value->base.type;
4614 type_t *const type = skip_typeref(orig_type);
4615 if (!is_type_integer(type)) {
4616 if (is_type_valid(type)) {
4617 errorf(HERE, "operand of ~ must be of integer type");
4622 expression->base.type = orig_type;
4625 static void semantic_dereference(unary_expression_t *expression)
4627 type_t *const orig_type = expression->value->base.type;
4628 type_t *const type = skip_typeref(orig_type);
4629 if(!is_type_pointer(type)) {
4630 if (is_type_valid(type)) {
4631 errorf(HERE, "Unary '*' needs pointer or arrray type, but type '%T' given", orig_type);
4636 type_t *result_type = type->pointer.points_to;
4637 result_type = automatic_type_conversion(result_type);
4638 expression->base.type = result_type;
4642 * Check the semantic of the address taken expression.
4644 static void semantic_take_addr(unary_expression_t *expression)
4646 expression_t *value = expression->value;
4647 value->base.type = revert_automatic_type_conversion(value);
4649 type_t *orig_type = value->base.type;
4650 if(!is_type_valid(orig_type))
4653 if(value->kind == EXPR_REFERENCE) {
4654 declaration_t *const declaration = value->reference.declaration;
4655 if(declaration != NULL) {
4656 if (declaration->storage_class == STORAGE_CLASS_REGISTER) {
4657 errorf(expression->base.source_position,
4658 "address of register variable '%Y' requested",
4659 declaration->symbol);
4661 declaration->address_taken = 1;
4665 expression->base.type = make_pointer_type(orig_type, TYPE_QUALIFIER_NONE);
4668 #define CREATE_UNARY_EXPRESSION_PARSER(token_type, unexpression_type, sfunc) \
4669 static expression_t *parse_##unexpression_type(unsigned precedence) \
4673 expression_t *unary_expression \
4674 = allocate_expression_zero(unexpression_type); \
4675 unary_expression->base.source_position = HERE; \
4676 unary_expression->unary.value = parse_sub_expression(precedence); \
4678 sfunc(&unary_expression->unary); \
4680 return unary_expression; \
4683 CREATE_UNARY_EXPRESSION_PARSER('-', EXPR_UNARY_NEGATE,
4684 semantic_unexpr_arithmetic)
4685 CREATE_UNARY_EXPRESSION_PARSER('+', EXPR_UNARY_PLUS,
4686 semantic_unexpr_arithmetic)
4687 CREATE_UNARY_EXPRESSION_PARSER('!', EXPR_UNARY_NOT,
4688 semantic_unexpr_scalar)
4689 CREATE_UNARY_EXPRESSION_PARSER('*', EXPR_UNARY_DEREFERENCE,
4690 semantic_dereference)
4691 CREATE_UNARY_EXPRESSION_PARSER('&', EXPR_UNARY_TAKE_ADDRESS,
4693 CREATE_UNARY_EXPRESSION_PARSER('~', EXPR_UNARY_BITWISE_NEGATE,
4694 semantic_unexpr_integer)
4695 CREATE_UNARY_EXPRESSION_PARSER(T_PLUSPLUS, EXPR_UNARY_PREFIX_INCREMENT,
4697 CREATE_UNARY_EXPRESSION_PARSER(T_MINUSMINUS, EXPR_UNARY_PREFIX_DECREMENT,
4700 #define CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(token_type, unexpression_type, \
4702 static expression_t *parse_##unexpression_type(unsigned precedence, \
4703 expression_t *left) \
4705 (void) precedence; \
4708 expression_t *unary_expression \
4709 = allocate_expression_zero(unexpression_type); \
4710 unary_expression->unary.value = left; \
4712 sfunc(&unary_expression->unary); \
4714 return unary_expression; \
4717 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_PLUSPLUS,
4718 EXPR_UNARY_POSTFIX_INCREMENT,
4720 CREATE_UNARY_POSTFIX_EXPRESSION_PARSER(T_MINUSMINUS,
4721 EXPR_UNARY_POSTFIX_DECREMENT,
4724 static type_t *semantic_arithmetic(type_t *type_left, type_t *type_right)
4726 /* TODO: handle complex + imaginary types */
4728 /* § 6.3.1.8 Usual arithmetic conversions */
4729 if(type_left == type_long_double || type_right == type_long_double) {
4730 return type_long_double;
4731 } else if(type_left == type_double || type_right == type_double) {
4733 } else if(type_left == type_float || type_right == type_float) {
4737 type_right = promote_integer(type_right);
4738 type_left = promote_integer(type_left);
4740 if(type_left == type_right)
4743 bool signed_left = is_type_signed(type_left);
4744 bool signed_right = is_type_signed(type_right);
4745 int rank_left = get_rank(type_left);
4746 int rank_right = get_rank(type_right);
4747 if(rank_left < rank_right) {
4748 if(signed_left == signed_right || !signed_right) {
4754 if(signed_left == signed_right || !signed_left) {
4763 * Check the semantic restrictions for a binary expression.
4765 static void semantic_binexpr_arithmetic(binary_expression_t *expression)
4767 expression_t *const left = expression->left;
4768 expression_t *const right = expression->right;
4769 type_t *const orig_type_left = left->base.type;
4770 type_t *const orig_type_right = right->base.type;
4771 type_t *const type_left = skip_typeref(orig_type_left);
4772 type_t *const type_right = skip_typeref(orig_type_right);
4774 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4775 /* TODO: improve error message */
4776 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4777 errorf(HERE, "operation needs arithmetic types");
4782 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4783 expression->left = create_implicit_cast(left, arithmetic_type);
4784 expression->right = create_implicit_cast(right, arithmetic_type);
4785 expression->base.type = arithmetic_type;
4788 static void semantic_shift_op(binary_expression_t *expression)
4790 expression_t *const left = expression->left;
4791 expression_t *const right = expression->right;
4792 type_t *const orig_type_left = left->base.type;
4793 type_t *const orig_type_right = right->base.type;
4794 type_t * type_left = skip_typeref(orig_type_left);
4795 type_t * type_right = skip_typeref(orig_type_right);
4797 if(!is_type_integer(type_left) || !is_type_integer(type_right)) {
4798 /* TODO: improve error message */
4799 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4800 errorf(HERE, "operation needs integer types");
4805 type_left = promote_integer(type_left);
4806 type_right = promote_integer(type_right);
4808 expression->left = create_implicit_cast(left, type_left);
4809 expression->right = create_implicit_cast(right, type_right);
4810 expression->base.type = type_left;
4813 static void semantic_add(binary_expression_t *expression)
4815 expression_t *const left = expression->left;
4816 expression_t *const right = expression->right;
4817 type_t *const orig_type_left = left->base.type;
4818 type_t *const orig_type_right = right->base.type;
4819 type_t *const type_left = skip_typeref(orig_type_left);
4820 type_t *const type_right = skip_typeref(orig_type_right);
4823 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4824 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4825 expression->left = create_implicit_cast(left, arithmetic_type);
4826 expression->right = create_implicit_cast(right, arithmetic_type);
4827 expression->base.type = arithmetic_type;
4829 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4830 expression->base.type = type_left;
4831 } else if(is_type_pointer(type_right) && is_type_integer(type_left)) {
4832 expression->base.type = type_right;
4833 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4834 errorf(HERE, "invalid operands to binary + ('%T', '%T')", orig_type_left, orig_type_right);
4838 static void semantic_sub(binary_expression_t *expression)
4840 expression_t *const left = expression->left;
4841 expression_t *const right = expression->right;
4842 type_t *const orig_type_left = left->base.type;
4843 type_t *const orig_type_right = right->base.type;
4844 type_t *const type_left = skip_typeref(orig_type_left);
4845 type_t *const type_right = skip_typeref(orig_type_right);
4848 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4849 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4850 expression->left = create_implicit_cast(left, arithmetic_type);
4851 expression->right = create_implicit_cast(right, arithmetic_type);
4852 expression->base.type = arithmetic_type;
4854 } else if(is_type_pointer(type_left) && is_type_integer(type_right)) {
4855 expression->base.type = type_left;
4856 } else if(is_type_pointer(type_left) && is_type_pointer(type_right)) {
4857 if(!pointers_compatible(type_left, type_right)) {
4859 "pointers to incompatible objects to binary '-' ('%T', '%T')",
4860 orig_type_left, orig_type_right);
4862 expression->base.type = type_ptrdiff_t;
4864 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4865 errorf(HERE, "invalid operands to binary '-' ('%T', '%T')",
4866 orig_type_left, orig_type_right);
4871 * Check the semantics of comparison expressions.
4873 * @param expression The expression to check.
4875 static void semantic_comparison(binary_expression_t *expression)
4877 expression_t *left = expression->left;
4878 expression_t *right = expression->right;
4879 type_t *orig_type_left = left->base.type;
4880 type_t *orig_type_right = right->base.type;
4882 type_t *type_left = skip_typeref(orig_type_left);
4883 type_t *type_right = skip_typeref(orig_type_right);
4885 /* TODO non-arithmetic types */
4886 if(is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4887 if (warning.sign_compare &&
4888 (expression->base.kind != EXPR_BINARY_EQUAL &&
4889 expression->base.kind != EXPR_BINARY_NOTEQUAL) &&
4890 (is_type_signed(type_left) != is_type_signed(type_right))) {
4891 warningf(expression->base.source_position,
4892 "comparison between signed and unsigned");
4894 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4895 expression->left = create_implicit_cast(left, arithmetic_type);
4896 expression->right = create_implicit_cast(right, arithmetic_type);
4897 expression->base.type = arithmetic_type;
4898 if (warning.float_equal &&
4899 (expression->base.kind == EXPR_BINARY_EQUAL ||
4900 expression->base.kind == EXPR_BINARY_NOTEQUAL) &&
4901 is_type_float(arithmetic_type)) {
4902 warningf(expression->base.source_position,
4903 "comparing floating point with == or != is unsafe");
4905 } else if (is_type_pointer(type_left) && is_type_pointer(type_right)) {
4906 /* TODO check compatibility */
4907 } else if (is_type_pointer(type_left)) {
4908 expression->right = create_implicit_cast(right, type_left);
4909 } else if (is_type_pointer(type_right)) {
4910 expression->left = create_implicit_cast(left, type_right);
4911 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4912 type_error_incompatible("invalid operands in comparison",
4913 expression->base.source_position,
4914 type_left, type_right);
4916 expression->base.type = type_int;
4919 static void semantic_arithmetic_assign(binary_expression_t *expression)
4921 expression_t *left = expression->left;
4922 expression_t *right = expression->right;
4923 type_t *orig_type_left = left->base.type;
4924 type_t *orig_type_right = right->base.type;
4926 type_t *type_left = skip_typeref(orig_type_left);
4927 type_t *type_right = skip_typeref(orig_type_right);
4929 if(!is_type_arithmetic(type_left) || !is_type_arithmetic(type_right)) {
4930 /* TODO: improve error message */
4931 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4932 errorf(HERE, "operation needs arithmetic types");
4937 /* combined instructions are tricky. We can't create an implicit cast on
4938 * the left side, because we need the uncasted form for the store.
4939 * The ast2firm pass has to know that left_type must be right_type
4940 * for the arithmetic operation and create a cast by itself */
4941 type_t *arithmetic_type = semantic_arithmetic(type_left, type_right);
4942 expression->right = create_implicit_cast(right, arithmetic_type);
4943 expression->base.type = type_left;
4946 static void semantic_arithmetic_addsubb_assign(binary_expression_t *expression)
4948 expression_t *const left = expression->left;
4949 expression_t *const right = expression->right;
4950 type_t *const orig_type_left = left->base.type;
4951 type_t *const orig_type_right = right->base.type;
4952 type_t *const type_left = skip_typeref(orig_type_left);
4953 type_t *const type_right = skip_typeref(orig_type_right);
4955 if (is_type_arithmetic(type_left) && is_type_arithmetic(type_right)) {
4956 /* combined instructions are tricky. We can't create an implicit cast on
4957 * the left side, because we need the uncasted form for the store.
4958 * The ast2firm pass has to know that left_type must be right_type
4959 * for the arithmetic operation and create a cast by itself */
4960 type_t *const arithmetic_type = semantic_arithmetic(type_left, type_right);
4961 expression->right = create_implicit_cast(right, arithmetic_type);
4962 expression->base.type = type_left;
4963 } else if (is_type_pointer(type_left) && is_type_integer(type_right)) {
4964 expression->base.type = type_left;
4965 } else if (is_type_valid(type_left) && is_type_valid(type_right)) {
4966 errorf(HERE, "incompatible types '%T' and '%T' in assignment", orig_type_left, orig_type_right);
4971 * Check the semantic restrictions of a logical expression.
4973 static void semantic_logical_op(binary_expression_t *expression)
4975 expression_t *const left = expression->left;
4976 expression_t *const right = expression->right;
4977 type_t *const orig_type_left = left->base.type;
4978 type_t *const orig_type_right = right->base.type;
4979 type_t *const type_left = skip_typeref(orig_type_left);
4980 type_t *const type_right = skip_typeref(orig_type_right);
4982 if (!is_type_scalar(type_left) || !is_type_scalar(type_right)) {
4983 /* TODO: improve error message */
4984 if (is_type_valid(type_left) && is_type_valid(type_right)) {
4985 errorf(HERE, "operation needs scalar types");
4990 expression->base.type = type_int;
4994 * Checks if a compound type has constant fields.
4996 static bool has_const_fields(const compound_type_t *type)
4998 const scope_t *scope = &type->declaration->scope;
4999 const declaration_t *declaration = scope->declarations;
5001 for (; declaration != NULL; declaration = declaration->next) {
5002 if (declaration->namespc != NAMESPACE_NORMAL)
5005 const type_t *decl_type = skip_typeref(declaration->type);
5006 if (decl_type->base.qualifiers & TYPE_QUALIFIER_CONST)
5014 * Check the semantic restrictions of a binary assign expression.
5016 static void semantic_binexpr_assign(binary_expression_t *expression)
5018 expression_t *left = expression->left;
5019 type_t *orig_type_left = left->base.type;
5021 type_t *type_left = revert_automatic_type_conversion(left);
5022 type_left = skip_typeref(orig_type_left);
5024 /* must be a modifiable lvalue */
5025 if (is_type_array(type_left)) {
5026 errorf(HERE, "cannot assign to arrays ('%E')", left);
5029 if(type_left->base.qualifiers & TYPE_QUALIFIER_CONST) {
5030 errorf(HERE, "assignment to readonly location '%E' (type '%T')", left,
5034 if(is_type_incomplete(type_left)) {
5036 "left-hand side of assignment '%E' has incomplete type '%T'",
5037 left, orig_type_left);
5040 if(is_type_compound(type_left) && has_const_fields(&type_left->compound)) {
5041 errorf(HERE, "cannot assign to '%E' because compound type '%T' has readonly fields",
5042 left, orig_type_left);
5046 type_t *const res_type = semantic_assign(orig_type_left, expression->right,
5048 if (res_type == NULL) {
5049 errorf(expression->base.source_position,
5050 "cannot assign to '%T' from '%T'",
5051 orig_type_left, expression->right->base.type);
5053 expression->right = create_implicit_cast(expression->right, res_type);
5056 expression->base.type = orig_type_left;
5059 static bool expression_has_effect(const expression_t *const expr)
5061 switch (expr->kind) {
5062 case EXPR_UNKNOWN: break;
5063 case EXPR_INVALID: break;
5064 case EXPR_REFERENCE: return false;
5065 case EXPR_CONST: return false;
5066 case EXPR_CHAR_CONST: return false;
5067 case EXPR_STRING_LITERAL: return false;
5068 case EXPR_WIDE_STRING_LITERAL: return false;
5070 const call_expression_t *const call = &expr->call;
5071 if (call->function->kind != EXPR_BUILTIN_SYMBOL)
5074 switch (call->function->builtin_symbol.symbol->ID) {
5075 case T___builtin_va_end: return true;
5076 default: return false;
5079 case EXPR_CONDITIONAL: {
5080 const conditional_expression_t *const cond = &expr->conditional;
5082 expression_has_effect(cond->true_expression) &&
5083 expression_has_effect(cond->false_expression);
5085 case EXPR_SELECT: return false;
5086 case EXPR_ARRAY_ACCESS: return false;
5087 case EXPR_SIZEOF: return false;
5088 case EXPR_CLASSIFY_TYPE: return false;
5089 case EXPR_ALIGNOF: return false;
5091 case EXPR_FUNCTION: return false;
5092 case EXPR_PRETTY_FUNCTION: return false;
5093 case EXPR_BUILTIN_SYMBOL: break; /* handled in EXPR_CALL */
5094 case EXPR_BUILTIN_CONSTANT_P: return false;
5095 case EXPR_BUILTIN_PREFETCH: return true;
5096 case EXPR_OFFSETOF: return false;
5097 case EXPR_VA_START: return true;
5098 case EXPR_VA_ARG: return true;
5099 case EXPR_STATEMENT: return true; // TODO
5100 case EXPR_COMPOUND_LITERAL: return false;
5102 case EXPR_UNARY_NEGATE: return false;
5103 case EXPR_UNARY_PLUS: return false;
5104 case EXPR_UNARY_BITWISE_NEGATE: return false;
5105 case EXPR_UNARY_NOT: return false;
5106 case EXPR_UNARY_DEREFERENCE: return false;
5107 case EXPR_UNARY_TAKE_ADDRESS: return false;
5108 case EXPR_UNARY_POSTFIX_INCREMENT: return true;
5109 case EXPR_UNARY_POSTFIX_DECREMENT: return true;
5110 case EXPR_UNARY_PREFIX_INCREMENT: return true;
5111 case EXPR_UNARY_PREFIX_DECREMENT: return true;
5112 case EXPR_UNARY_CAST: {
5113 type_t *type = skip_typeref(expr->base.type);
5114 return is_type_atomic(type, ATOMIC_TYPE_VOID);
5116 case EXPR_UNARY_CAST_IMPLICIT: return true;
5117 case EXPR_UNARY_ASSUME: return true;
5118 case EXPR_UNARY_BITFIELD_EXTRACT: return false;
5120 case EXPR_BINARY_ADD: return false;
5121 case EXPR_BINARY_SUB: return false;
5122 case EXPR_BINARY_MUL: return false;
5123 case EXPR_BINARY_DIV: return false;
5124 case EXPR_BINARY_MOD: return false;
5125 case EXPR_BINARY_EQUAL: return false;
5126 case EXPR_BINARY_NOTEQUAL: return false;
5127 case EXPR_BINARY_LESS: return false;
5128 case EXPR_BINARY_LESSEQUAL: return false;
5129 case EXPR_BINARY_GREATER: return false;
5130 case EXPR_BINARY_GREATEREQUAL: return false;
5131 case EXPR_BINARY_BITWISE_AND: return false;
5132 case EXPR_BINARY_BITWISE_OR: return false;
5133 case EXPR_BINARY_BITWISE_XOR: return false;
5134 case EXPR_BINARY_SHIFTLEFT: return false;
5135 case EXPR_BINARY_SHIFTRIGHT: return false;
5136 case EXPR_BINARY_ASSIGN: return true;
5137 case EXPR_BINARY_MUL_ASSIGN: return true;
5138 case EXPR_BINARY_DIV_ASSIGN: return true;
5139 case EXPR_BINARY_MOD_ASSIGN: return true;
5140 case EXPR_BINARY_ADD_ASSIGN: return true;
5141 case EXPR_BINARY_SUB_ASSIGN: return true;
5142 case EXPR_BINARY_SHIFTLEFT_ASSIGN: return true;
5143 case EXPR_BINARY_SHIFTRIGHT_ASSIGN: return true;
5144 case EXPR_BINARY_BITWISE_AND_ASSIGN: return true;
5145 case EXPR_BINARY_BITWISE_XOR_ASSIGN: return true;
5146 case EXPR_BINARY_BITWISE_OR_ASSIGN: return true;
5147 case EXPR_BINARY_LOGICAL_AND:
5148 case EXPR_BINARY_LOGICAL_OR:
5149 case EXPR_BINARY_COMMA:
5150 return expression_has_effect(expr->binary.right);
5152 case EXPR_BINARY_BUILTIN_EXPECT: return true;
5153 case EXPR_BINARY_ISGREATER: return false;
5154 case EXPR_BINARY_ISGREATEREQUAL: return false;
5155 case EXPR_BINARY_ISLESS: return false;
5156 case EXPR_BINARY_ISLESSEQUAL: return false;
5157 case EXPR_BINARY_ISLESSGREATER: return false;
5158 case EXPR_BINARY_ISUNORDERED: return false;
5161 panic("unexpected statement");
5164 static void semantic_comma(binary_expression_t *expression)
5166 if (warning.unused_value) {
5167 const expression_t *const left = expression->left;
5168 if (!expression_has_effect(left)) {
5169 warningf(left->base.source_position, "left-hand operand of comma expression has no effect");
5172 expression->base.type = expression->right->base.type;
5175 #define CREATE_BINEXPR_PARSER(token_type, binexpression_type, sfunc, lr) \
5176 static expression_t *parse_##binexpression_type(unsigned precedence, \
5177 expression_t *left) \
5180 source_position_t pos = HERE; \
5182 expression_t *right = parse_sub_expression(precedence + lr); \
5184 expression_t *binexpr = allocate_expression_zero(binexpression_type); \
5185 binexpr->base.source_position = pos; \
5186 binexpr->binary.left = left; \
5187 binexpr->binary.right = right; \
5188 sfunc(&binexpr->binary); \
5193 CREATE_BINEXPR_PARSER(',', EXPR_BINARY_COMMA, semantic_comma, 1)
5194 CREATE_BINEXPR_PARSER('*', EXPR_BINARY_MUL, semantic_binexpr_arithmetic, 1)
5195 CREATE_BINEXPR_PARSER('/', EXPR_BINARY_DIV, semantic_binexpr_arithmetic, 1)
5196 CREATE_BINEXPR_PARSER('%', EXPR_BINARY_MOD, semantic_binexpr_arithmetic, 1)
5197 CREATE_BINEXPR_PARSER('+', EXPR_BINARY_ADD, semantic_add, 1)
5198 CREATE_BINEXPR_PARSER('-', EXPR_BINARY_SUB, semantic_sub, 1)
5199 CREATE_BINEXPR_PARSER('<', EXPR_BINARY_LESS, semantic_comparison, 1)
5200 CREATE_BINEXPR_PARSER('>', EXPR_BINARY_GREATER, semantic_comparison, 1)
5201 CREATE_BINEXPR_PARSER('=', EXPR_BINARY_ASSIGN, semantic_binexpr_assign, 0)
5203 CREATE_BINEXPR_PARSER(T_EQUALEQUAL, EXPR_BINARY_EQUAL,
5204 semantic_comparison, 1)
5205 CREATE_BINEXPR_PARSER(T_EXCLAMATIONMARKEQUAL, EXPR_BINARY_NOTEQUAL,
5206 semantic_comparison, 1)
5207 CREATE_BINEXPR_PARSER(T_LESSEQUAL, EXPR_BINARY_LESSEQUAL,
5208 semantic_comparison, 1)
5209 CREATE_BINEXPR_PARSER(T_GREATEREQUAL, EXPR_BINARY_GREATEREQUAL,
5210 semantic_comparison, 1)
5212 CREATE_BINEXPR_PARSER('&', EXPR_BINARY_BITWISE_AND,
5213 semantic_binexpr_arithmetic, 1)
5214 CREATE_BINEXPR_PARSER('|', EXPR_BINARY_BITWISE_OR,
5215 semantic_binexpr_arithmetic, 1)
5216 CREATE_BINEXPR_PARSER('^', EXPR_BINARY_BITWISE_XOR,
5217 semantic_binexpr_arithmetic, 1)
5218 CREATE_BINEXPR_PARSER(T_ANDAND, EXPR_BINARY_LOGICAL_AND,
5219 semantic_logical_op, 1)
5220 CREATE_BINEXPR_PARSER(T_PIPEPIPE, EXPR_BINARY_LOGICAL_OR,
5221 semantic_logical_op, 1)
5222 CREATE_BINEXPR_PARSER(T_LESSLESS, EXPR_BINARY_SHIFTLEFT,
5223 semantic_shift_op, 1)
5224 CREATE_BINEXPR_PARSER(T_GREATERGREATER, EXPR_BINARY_SHIFTRIGHT,
5225 semantic_shift_op, 1)
5226 CREATE_BINEXPR_PARSER(T_PLUSEQUAL, EXPR_BINARY_ADD_ASSIGN,
5227 semantic_arithmetic_addsubb_assign, 0)
5228 CREATE_BINEXPR_PARSER(T_MINUSEQUAL, EXPR_BINARY_SUB_ASSIGN,
5229 semantic_arithmetic_addsubb_assign, 0)
5230 CREATE_BINEXPR_PARSER(T_ASTERISKEQUAL, EXPR_BINARY_MUL_ASSIGN,
5231 semantic_arithmetic_assign, 0)
5232 CREATE_BINEXPR_PARSER(T_SLASHEQUAL, EXPR_BINARY_DIV_ASSIGN,
5233 semantic_arithmetic_assign, 0)
5234 CREATE_BINEXPR_PARSER(T_PERCENTEQUAL, EXPR_BINARY_MOD_ASSIGN,
5235 semantic_arithmetic_assign, 0)
5236 CREATE_BINEXPR_PARSER(T_LESSLESSEQUAL, EXPR_BINARY_SHIFTLEFT_ASSIGN,
5237 semantic_arithmetic_assign, 0)
5238 CREATE_BINEXPR_PARSER(T_GREATERGREATEREQUAL, EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5239 semantic_arithmetic_assign, 0)
5240 CREATE_BINEXPR_PARSER(T_ANDEQUAL, EXPR_BINARY_BITWISE_AND_ASSIGN,
5241 semantic_arithmetic_assign, 0)
5242 CREATE_BINEXPR_PARSER(T_PIPEEQUAL, EXPR_BINARY_BITWISE_OR_ASSIGN,
5243 semantic_arithmetic_assign, 0)
5244 CREATE_BINEXPR_PARSER(T_CARETEQUAL, EXPR_BINARY_BITWISE_XOR_ASSIGN,
5245 semantic_arithmetic_assign, 0)
5247 static expression_t *parse_sub_expression(unsigned precedence)
5249 if(token.type < 0) {
5250 return expected_expression_error();
5253 expression_parser_function_t *parser
5254 = &expression_parsers[token.type];
5255 source_position_t source_position = token.source_position;
5258 if(parser->parser != NULL) {
5259 left = parser->parser(parser->precedence);
5261 left = parse_primary_expression();
5263 assert(left != NULL);
5264 left->base.source_position = source_position;
5267 if(token.type < 0) {
5268 return expected_expression_error();
5271 parser = &expression_parsers[token.type];
5272 if(parser->infix_parser == NULL)
5274 if(parser->infix_precedence < precedence)
5277 left = parser->infix_parser(parser->infix_precedence, left);
5279 assert(left != NULL);
5280 assert(left->kind != EXPR_UNKNOWN);
5281 left->base.source_position = source_position;
5288 * Parse an expression.
5290 static expression_t *parse_expression(void)
5292 return parse_sub_expression(1);
5296 * Register a parser for a prefix-like operator with given precedence.
5298 * @param parser the parser function
5299 * @param token_type the token type of the prefix token
5300 * @param precedence the precedence of the operator
5302 static void register_expression_parser(parse_expression_function parser,
5303 int token_type, unsigned precedence)
5305 expression_parser_function_t *entry = &expression_parsers[token_type];
5307 if(entry->parser != NULL) {
5308 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5309 panic("trying to register multiple expression parsers for a token");
5311 entry->parser = parser;
5312 entry->precedence = precedence;
5316 * Register a parser for an infix operator with given precedence.
5318 * @param parser the parser function
5319 * @param token_type the token type of the infix operator
5320 * @param precedence the precedence of the operator
5322 static void register_infix_parser(parse_expression_infix_function parser,
5323 int token_type, unsigned precedence)
5325 expression_parser_function_t *entry = &expression_parsers[token_type];
5327 if(entry->infix_parser != NULL) {
5328 diagnosticf("for token '%k'\n", (token_type_t)token_type);
5329 panic("trying to register multiple infix expression parsers for a "
5332 entry->infix_parser = parser;
5333 entry->infix_precedence = precedence;
5337 * Initialize the expression parsers.
5339 static void init_expression_parsers(void)
5341 memset(&expression_parsers, 0, sizeof(expression_parsers));
5343 register_infix_parser(parse_array_expression, '[', 30);
5344 register_infix_parser(parse_call_expression, '(', 30);
5345 register_infix_parser(parse_select_expression, '.', 30);
5346 register_infix_parser(parse_select_expression, T_MINUSGREATER, 30);
5347 register_infix_parser(parse_EXPR_UNARY_POSTFIX_INCREMENT,
5349 register_infix_parser(parse_EXPR_UNARY_POSTFIX_DECREMENT,
5352 register_infix_parser(parse_EXPR_BINARY_MUL, '*', 16);
5353 register_infix_parser(parse_EXPR_BINARY_DIV, '/', 16);
5354 register_infix_parser(parse_EXPR_BINARY_MOD, '%', 16);
5355 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT, T_LESSLESS, 16);
5356 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT, T_GREATERGREATER, 16);
5357 register_infix_parser(parse_EXPR_BINARY_ADD, '+', 15);
5358 register_infix_parser(parse_EXPR_BINARY_SUB, '-', 15);
5359 register_infix_parser(parse_EXPR_BINARY_LESS, '<', 14);
5360 register_infix_parser(parse_EXPR_BINARY_GREATER, '>', 14);
5361 register_infix_parser(parse_EXPR_BINARY_LESSEQUAL, T_LESSEQUAL, 14);
5362 register_infix_parser(parse_EXPR_BINARY_GREATEREQUAL, T_GREATEREQUAL, 14);
5363 register_infix_parser(parse_EXPR_BINARY_EQUAL, T_EQUALEQUAL, 13);
5364 register_infix_parser(parse_EXPR_BINARY_NOTEQUAL,
5365 T_EXCLAMATIONMARKEQUAL, 13);
5366 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND, '&', 12);
5367 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR, '^', 11);
5368 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR, '|', 10);
5369 register_infix_parser(parse_EXPR_BINARY_LOGICAL_AND, T_ANDAND, 9);
5370 register_infix_parser(parse_EXPR_BINARY_LOGICAL_OR, T_PIPEPIPE, 8);
5371 register_infix_parser(parse_conditional_expression, '?', 7);
5372 register_infix_parser(parse_EXPR_BINARY_ASSIGN, '=', 2);
5373 register_infix_parser(parse_EXPR_BINARY_ADD_ASSIGN, T_PLUSEQUAL, 2);
5374 register_infix_parser(parse_EXPR_BINARY_SUB_ASSIGN, T_MINUSEQUAL, 2);
5375 register_infix_parser(parse_EXPR_BINARY_MUL_ASSIGN, T_ASTERISKEQUAL, 2);
5376 register_infix_parser(parse_EXPR_BINARY_DIV_ASSIGN, T_SLASHEQUAL, 2);
5377 register_infix_parser(parse_EXPR_BINARY_MOD_ASSIGN, T_PERCENTEQUAL, 2);
5378 register_infix_parser(parse_EXPR_BINARY_SHIFTLEFT_ASSIGN,
5379 T_LESSLESSEQUAL, 2);
5380 register_infix_parser(parse_EXPR_BINARY_SHIFTRIGHT_ASSIGN,
5381 T_GREATERGREATEREQUAL, 2);
5382 register_infix_parser(parse_EXPR_BINARY_BITWISE_AND_ASSIGN,
5384 register_infix_parser(parse_EXPR_BINARY_BITWISE_OR_ASSIGN,
5386 register_infix_parser(parse_EXPR_BINARY_BITWISE_XOR_ASSIGN,
5389 register_infix_parser(parse_EXPR_BINARY_COMMA, ',', 1);
5391 register_expression_parser(parse_EXPR_UNARY_NEGATE, '-', 25);
5392 register_expression_parser(parse_EXPR_UNARY_PLUS, '+', 25);
5393 register_expression_parser(parse_EXPR_UNARY_NOT, '!', 25);
5394 register_expression_parser(parse_EXPR_UNARY_BITWISE_NEGATE, '~', 25);
5395 register_expression_parser(parse_EXPR_UNARY_DEREFERENCE, '*', 25);
5396 register_expression_parser(parse_EXPR_UNARY_TAKE_ADDRESS, '&', 25);
5397 register_expression_parser(parse_EXPR_UNARY_PREFIX_INCREMENT,
5399 register_expression_parser(parse_EXPR_UNARY_PREFIX_DECREMENT,
5401 register_expression_parser(parse_sizeof, T_sizeof, 25);
5402 register_expression_parser(parse_alignof, T___alignof__, 25);
5403 register_expression_parser(parse_extension, T___extension__, 25);
5404 register_expression_parser(parse_builtin_classify_type,
5405 T___builtin_classify_type, 25);
5409 * Parse a asm statement constraints specification.
5411 static asm_constraint_t *parse_asm_constraints(void)
5413 asm_constraint_t *result = NULL;
5414 asm_constraint_t *last = NULL;
5416 while(token.type == T_STRING_LITERAL || token.type == '[') {
5417 asm_constraint_t *constraint = allocate_ast_zero(sizeof(constraint[0]));
5418 memset(constraint, 0, sizeof(constraint[0]));
5420 if(token.type == '[') {
5422 if(token.type != T_IDENTIFIER) {
5423 parse_error_expected("while parsing asm constraint",
5427 constraint->symbol = token.v.symbol;
5432 constraint->constraints = parse_string_literals();
5434 constraint->expression = parse_expression();
5438 last->next = constraint;
5440 result = constraint;
5444 if(token.type != ',')
5453 * Parse a asm statement clobber specification.
5455 static asm_clobber_t *parse_asm_clobbers(void)
5457 asm_clobber_t *result = NULL;
5458 asm_clobber_t *last = NULL;
5460 while(token.type == T_STRING_LITERAL) {
5461 asm_clobber_t *clobber = allocate_ast_zero(sizeof(clobber[0]));
5462 clobber->clobber = parse_string_literals();
5465 last->next = clobber;
5471 if(token.type != ',')
5480 * Parse an asm statement.
5482 static statement_t *parse_asm_statement(void)
5486 statement_t *statement = allocate_statement_zero(STATEMENT_ASM);
5487 statement->base.source_position = token.source_position;
5489 asm_statement_t *asm_statement = &statement->asms;
5491 if(token.type == T_volatile) {
5493 asm_statement->is_volatile = true;
5497 asm_statement->asm_text = parse_string_literals();
5499 if(token.type != ':')
5503 asm_statement->inputs = parse_asm_constraints();
5504 if(token.type != ':')
5508 asm_statement->outputs = parse_asm_constraints();
5509 if(token.type != ':')
5513 asm_statement->clobbers = parse_asm_clobbers();
5522 * Parse a case statement.
5524 static statement_t *parse_case_statement(void)
5528 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5530 statement->base.source_position = token.source_position;
5531 statement->case_label.expression = parse_expression();
5533 if (c_mode & _GNUC) {
5534 if (token.type == T_DOTDOTDOT) {
5536 statement->case_label.end_range = parse_expression();
5542 if (! is_constant_expression(statement->case_label.expression)) {
5543 errorf(statement->base.source_position,
5544 "case label does not reduce to an integer constant");
5546 /* TODO: check if the case label is already known */
5547 if (current_switch != NULL) {
5548 /* link all cases into the switch statement */
5549 if (current_switch->last_case == NULL) {
5550 current_switch->first_case =
5551 current_switch->last_case = &statement->case_label;
5553 current_switch->last_case->next = &statement->case_label;
5556 errorf(statement->base.source_position,
5557 "case label not within a switch statement");
5560 statement->case_label.statement = parse_statement();
5566 * Finds an existing default label of a switch statement.
5568 static case_label_statement_t *
5569 find_default_label(const switch_statement_t *statement)
5571 case_label_statement_t *label = statement->first_case;
5572 for ( ; label != NULL; label = label->next) {
5573 if (label->expression == NULL)
5580 * Parse a default statement.
5582 static statement_t *parse_default_statement(void)
5586 statement_t *statement = allocate_statement_zero(STATEMENT_CASE_LABEL);
5588 statement->base.source_position = token.source_position;
5591 if (current_switch != NULL) {
5592 const case_label_statement_t *def_label = find_default_label(current_switch);
5593 if (def_label != NULL) {
5594 errorf(HERE, "multiple default labels in one switch");
5595 errorf(def_label->base.source_position,
5596 "this is the first default label");
5598 /* link all cases into the switch statement */
5599 if (current_switch->last_case == NULL) {
5600 current_switch->first_case =
5601 current_switch->last_case = &statement->case_label;
5603 current_switch->last_case->next = &statement->case_label;
5607 errorf(statement->base.source_position,
5608 "'default' label not within a switch statement");
5610 statement->case_label.statement = parse_statement();
5616 * Return the declaration for a given label symbol or create a new one.
5618 static declaration_t *get_label(symbol_t *symbol)
5620 declaration_t *candidate = get_declaration(symbol, NAMESPACE_LABEL);
5621 assert(current_function != NULL);
5622 /* if we found a label in the same function, then we already created the
5624 if(candidate != NULL
5625 && candidate->parent_scope == ¤t_function->scope) {
5629 /* otherwise we need to create a new one */
5630 declaration_t *const declaration = allocate_declaration_zero();
5631 declaration->namespc = NAMESPACE_LABEL;
5632 declaration->symbol = symbol;
5634 label_push(declaration);
5640 * Parse a label statement.
5642 static statement_t *parse_label_statement(void)
5644 assert(token.type == T_IDENTIFIER);
5645 symbol_t *symbol = token.v.symbol;
5648 declaration_t *label = get_label(symbol);
5650 /* if source position is already set then the label is defined twice,
5651 * otherwise it was just mentioned in a goto so far */
5652 if(label->source_position.input_name != NULL) {
5653 errorf(HERE, "duplicate label '%Y'", symbol);
5654 errorf(label->source_position, "previous definition of '%Y' was here",
5657 label->source_position = token.source_position;
5660 statement_t *statement = allocate_statement_zero(STATEMENT_LABEL);
5662 statement->base.source_position = token.source_position;
5663 statement->label.label = label;
5667 if(token.type == '}') {
5668 /* TODO only warn? */
5669 errorf(HERE, "label at end of compound statement");
5672 if (token.type == ';') {
5673 /* eat an empty statement here, to avoid the warning about an empty
5674 * after a label. label:; is commonly used to have a label before
5678 statement->label.statement = parse_statement();
5682 /* remember the labels's in a list for later checking */
5683 if (label_last == NULL) {
5684 label_first = &statement->label;
5686 label_last->next = &statement->label;
5688 label_last = &statement->label;
5694 * Parse an if statement.
5696 static statement_t *parse_if(void)
5700 statement_t *statement = allocate_statement_zero(STATEMENT_IF);
5701 statement->base.source_position = token.source_position;
5704 statement->ifs.condition = parse_expression();
5707 statement->ifs.true_statement = parse_statement();
5708 if(token.type == T_else) {
5710 statement->ifs.false_statement = parse_statement();
5717 * Parse a switch statement.
5719 static statement_t *parse_switch(void)
5723 statement_t *statement = allocate_statement_zero(STATEMENT_SWITCH);
5724 statement->base.source_position = token.source_position;
5727 expression_t *const expr = parse_expression();
5728 type_t * type = skip_typeref(expr->base.type);
5729 if (is_type_integer(type)) {
5730 type = promote_integer(type);
5731 } else if (is_type_valid(type)) {
5732 errorf(expr->base.source_position,
5733 "switch quantity is not an integer, but '%T'", type);
5734 type = type_error_type;
5736 statement->switchs.expression = create_implicit_cast(expr, type);
5739 switch_statement_t *rem = current_switch;
5740 current_switch = &statement->switchs;
5741 statement->switchs.body = parse_statement();
5742 current_switch = rem;
5744 if (warning.switch_default
5745 && find_default_label(&statement->switchs) == NULL) {
5746 warningf(statement->base.source_position, "switch has no default case");
5752 static statement_t *parse_loop_body(statement_t *const loop)
5754 statement_t *const rem = current_loop;
5755 current_loop = loop;
5757 statement_t *const body = parse_statement();
5764 * Parse a while statement.
5766 static statement_t *parse_while(void)
5770 statement_t *statement = allocate_statement_zero(STATEMENT_WHILE);
5771 statement->base.source_position = token.source_position;
5774 statement->whiles.condition = parse_expression();
5777 statement->whiles.body = parse_loop_body(statement);
5783 * Parse a do statement.
5785 static statement_t *parse_do(void)
5789 statement_t *statement = allocate_statement_zero(STATEMENT_DO_WHILE);
5791 statement->base.source_position = token.source_position;
5793 statement->do_while.body = parse_loop_body(statement);
5797 statement->do_while.condition = parse_expression();
5805 * Parse a for statement.
5807 static statement_t *parse_for(void)
5811 statement_t *statement = allocate_statement_zero(STATEMENT_FOR);
5812 statement->base.source_position = token.source_position;
5816 int top = environment_top();
5817 scope_t *last_scope = scope;
5818 set_scope(&statement->fors.scope);
5820 if(token.type != ';') {
5821 if(is_declaration_specifier(&token, false)) {
5822 parse_declaration(record_declaration);
5824 expression_t *const init = parse_expression();
5825 statement->fors.initialisation = init;
5826 if (warning.unused_value && !expression_has_effect(init)) {
5827 warningf(init->base.source_position, "initialisation of 'for'-statement has no effect");
5835 if(token.type != ';') {
5836 statement->fors.condition = parse_expression();
5839 if(token.type != ')') {
5840 expression_t *const step = parse_expression();
5841 statement->fors.step = step;
5842 if (warning.unused_value && !expression_has_effect(step)) {
5843 warningf(step->base.source_position, "step of 'for'-statement has no effect");
5847 statement->fors.body = parse_loop_body(statement);
5849 assert(scope == &statement->fors.scope);
5850 set_scope(last_scope);
5851 environment_pop_to(top);
5857 * Parse a goto statement.
5859 static statement_t *parse_goto(void)
5863 if(token.type != T_IDENTIFIER) {
5864 parse_error_expected("while parsing goto", T_IDENTIFIER, 0);
5868 symbol_t *symbol = token.v.symbol;
5871 declaration_t *label = get_label(symbol);
5873 statement_t *statement = allocate_statement_zero(STATEMENT_GOTO);
5874 statement->base.source_position = token.source_position;
5876 statement->gotos.label = label;
5878 /* remember the goto's in a list for later checking */
5879 if (goto_last == NULL) {
5880 goto_first = &statement->gotos;
5882 goto_last->next = &statement->gotos;
5884 goto_last = &statement->gotos;
5892 * Parse a continue statement.
5894 static statement_t *parse_continue(void)
5896 statement_t *statement;
5897 if (current_loop == NULL) {
5898 errorf(HERE, "continue statement not within loop");
5901 statement = allocate_statement_zero(STATEMENT_CONTINUE);
5903 statement->base.source_position = token.source_position;
5913 * Parse a break statement.
5915 static statement_t *parse_break(void)
5917 statement_t *statement;
5918 if (current_switch == NULL && current_loop == NULL) {
5919 errorf(HERE, "break statement not within loop or switch");
5922 statement = allocate_statement_zero(STATEMENT_BREAK);
5924 statement->base.source_position = token.source_position;
5934 * Check if a given declaration represents a local variable.
5936 static bool is_local_var_declaration(const declaration_t *declaration) {
5937 switch ((storage_class_tag_t) declaration->storage_class) {
5938 case STORAGE_CLASS_NONE:
5939 case STORAGE_CLASS_AUTO:
5940 case STORAGE_CLASS_REGISTER: {
5941 const type_t *type = skip_typeref(declaration->type);
5942 if(is_type_function(type)) {
5954 * Check if a given declaration represents a variable.
5956 static bool is_var_declaration(const declaration_t *declaration) {
5957 switch ((storage_class_tag_t) declaration->storage_class) {
5958 case STORAGE_CLASS_NONE:
5959 case STORAGE_CLASS_EXTERN:
5960 case STORAGE_CLASS_STATIC:
5961 case STORAGE_CLASS_AUTO:
5962 case STORAGE_CLASS_REGISTER:
5963 case STORAGE_CLASS_THREAD:
5964 case STORAGE_CLASS_THREAD_EXTERN:
5965 case STORAGE_CLASS_THREAD_STATIC: {
5966 const type_t *type = skip_typeref(declaration->type);
5967 if(is_type_function(type)) {
5979 * Check if a given expression represents a local variable.
5981 static bool is_local_variable(const expression_t *expression)
5983 if (expression->base.kind != EXPR_REFERENCE) {
5986 const declaration_t *declaration = expression->reference.declaration;
5987 return is_local_var_declaration(declaration);
5991 * Check if a given expression represents a local variable and
5992 * return its declaration then, else return NULL.
5994 declaration_t *expr_is_variable(const expression_t *expression)
5996 if (expression->base.kind != EXPR_REFERENCE) {
5999 declaration_t *declaration = expression->reference.declaration;
6000 if (is_var_declaration(declaration))
6006 * Parse a return statement.
6008 static statement_t *parse_return(void)
6012 statement_t *statement = allocate_statement_zero(STATEMENT_RETURN);
6013 statement->base.source_position = token.source_position;
6015 expression_t *return_value = NULL;
6016 if(token.type != ';') {
6017 return_value = parse_expression();
6021 const type_t *const func_type = current_function->type;
6022 assert(is_type_function(func_type));
6023 type_t *const return_type = skip_typeref(func_type->function.return_type);
6025 if(return_value != NULL) {
6026 type_t *return_value_type = skip_typeref(return_value->base.type);
6028 if(is_type_atomic(return_type, ATOMIC_TYPE_VOID)
6029 && !is_type_atomic(return_value_type, ATOMIC_TYPE_VOID)) {
6030 warningf(statement->base.source_position,
6031 "'return' with a value, in function returning void");
6032 return_value = NULL;
6034 type_t *const res_type = semantic_assign(return_type,
6035 return_value, "'return'");
6036 if (res_type == NULL) {
6037 errorf(statement->base.source_position,
6038 "cannot return something of type '%T' in function returning '%T'",
6039 return_value->base.type, return_type);
6041 return_value = create_implicit_cast(return_value, res_type);
6044 /* check for returning address of a local var */
6045 if (return_value->base.kind == EXPR_UNARY_TAKE_ADDRESS) {
6046 const expression_t *expression = return_value->unary.value;
6047 if (is_local_variable(expression)) {
6048 warningf(statement->base.source_position,
6049 "function returns address of local variable");
6053 if(!is_type_atomic(return_type, ATOMIC_TYPE_VOID)) {
6054 warningf(statement->base.source_position,
6055 "'return' without value, in function returning non-void");
6058 statement->returns.value = return_value;
6064 * Parse a declaration statement.
6066 static statement_t *parse_declaration_statement(void)
6068 statement_t *statement = allocate_statement_zero(STATEMENT_DECLARATION);
6070 statement->base.source_position = token.source_position;
6072 declaration_t *before = last_declaration;
6073 parse_declaration(record_declaration);
6075 if(before == NULL) {
6076 statement->declaration.declarations_begin = scope->declarations;
6078 statement->declaration.declarations_begin = before->next;
6080 statement->declaration.declarations_end = last_declaration;
6086 * Parse an expression statement, ie. expr ';'.
6088 static statement_t *parse_expression_statement(void)
6090 statement_t *statement = allocate_statement_zero(STATEMENT_EXPRESSION);
6092 statement->base.source_position = token.source_position;
6093 expression_t *const expr = parse_expression();
6094 statement->expression.expression = expr;
6096 if (warning.unused_value && !expression_has_effect(expr)) {
6097 warningf(expr->base.source_position, "statement has no effect");
6106 * Parse a statement.
6108 static statement_t *parse_statement(void)
6110 statement_t *statement = NULL;
6112 /* declaration or statement */
6113 switch(token.type) {
6115 statement = parse_asm_statement();
6119 statement = parse_case_statement();
6123 statement = parse_default_statement();
6127 statement = parse_compound_statement();
6131 statement = parse_if();
6135 statement = parse_switch();
6139 statement = parse_while();
6143 statement = parse_do();
6147 statement = parse_for();
6151 statement = parse_goto();
6155 statement = parse_continue();
6159 statement = parse_break();
6163 statement = parse_return();
6167 if (warning.empty_statement) {
6168 warningf(HERE, "statement is empty");
6175 if(look_ahead(1)->type == ':') {
6176 statement = parse_label_statement();
6180 if(is_typedef_symbol(token.v.symbol)) {
6181 statement = parse_declaration_statement();
6185 statement = parse_expression_statement();
6188 case T___extension__:
6189 /* this can be a prefix to a declaration or an expression statement */
6190 /* we simply eat it now and parse the rest with tail recursion */
6193 } while(token.type == T___extension__);
6194 statement = parse_statement();
6198 statement = parse_declaration_statement();
6202 statement = parse_expression_statement();
6206 assert(statement == NULL
6207 || statement->base.source_position.input_name != NULL);
6213 * Parse a compound statement.
6215 static statement_t *parse_compound_statement(void)
6217 statement_t *statement = allocate_statement_zero(STATEMENT_COMPOUND);
6219 statement->base.source_position = token.source_position;
6223 int top = environment_top();
6224 scope_t *last_scope = scope;
6225 set_scope(&statement->compound.scope);
6227 statement_t *last_statement = NULL;
6229 while(token.type != '}' && token.type != T_EOF) {
6230 statement_t *sub_statement = parse_statement();
6231 if(sub_statement == NULL)
6234 if(last_statement != NULL) {
6235 last_statement->base.next = sub_statement;
6237 statement->compound.statements = sub_statement;
6240 while(sub_statement->base.next != NULL)
6241 sub_statement = sub_statement->base.next;
6243 last_statement = sub_statement;
6246 if(token.type == '}') {
6249 errorf(statement->base.source_position,
6250 "end of file while looking for closing '}'");
6253 assert(scope == &statement->compound.scope);
6254 set_scope(last_scope);
6255 environment_pop_to(top);
6261 * Initialize builtin types.
6263 static void initialize_builtin_types(void)
6265 type_intmax_t = make_global_typedef("__intmax_t__", type_long_long);
6266 type_size_t = make_global_typedef("__SIZE_TYPE__", type_unsigned_long);
6267 type_ssize_t = make_global_typedef("__SSIZE_TYPE__", type_long);
6268 type_ptrdiff_t = make_global_typedef("__PTRDIFF_TYPE__", type_long);
6269 type_uintmax_t = make_global_typedef("__uintmax_t__", type_unsigned_long_long);
6270 type_uptrdiff_t = make_global_typedef("__UPTRDIFF_TYPE__", type_unsigned_long);
6271 type_wchar_t = make_global_typedef("__WCHAR_TYPE__", type_int);
6272 type_wint_t = make_global_typedef("__WINT_TYPE__", type_int);
6274 type_intmax_t_ptr = make_pointer_type(type_intmax_t, TYPE_QUALIFIER_NONE);
6275 type_ptrdiff_t_ptr = make_pointer_type(type_ptrdiff_t, TYPE_QUALIFIER_NONE);
6276 type_ssize_t_ptr = make_pointer_type(type_ssize_t, TYPE_QUALIFIER_NONE);
6277 type_wchar_t_ptr = make_pointer_type(type_wchar_t, TYPE_QUALIFIER_NONE);
6281 * Check for unused global static functions and variables
6283 static void check_unused_globals(void)
6285 if (!warning.unused_function && !warning.unused_variable)
6288 for (const declaration_t *decl = global_scope->declarations; decl != NULL; decl = decl->next) {
6289 if (decl->used || decl->storage_class != STORAGE_CLASS_STATIC)
6292 type_t *const type = decl->type;
6294 if (is_type_function(skip_typeref(type))) {
6295 if (!warning.unused_function || decl->is_inline)
6298 s = (decl->init.statement != NULL ? "defined" : "declared");
6300 if (!warning.unused_variable)
6306 warningf(decl->source_position, "'%#T' %s but not used",
6307 type, decl->symbol, s);
6312 * Parse a translation unit.
6314 static translation_unit_t *parse_translation_unit(void)
6316 translation_unit_t *unit = allocate_ast_zero(sizeof(unit[0]));
6318 assert(global_scope == NULL);
6319 global_scope = &unit->scope;
6321 assert(scope == NULL);
6322 set_scope(&unit->scope);
6324 initialize_builtin_types();
6326 while(token.type != T_EOF) {
6327 if (token.type == ';') {
6328 /* TODO error in strict mode */
6329 warningf(HERE, "stray ';' outside of function");
6332 parse_external_declaration();
6336 assert(scope == &unit->scope);
6338 last_declaration = NULL;
6340 assert(global_scope == &unit->scope);
6341 check_unused_globals();
6342 global_scope = NULL;
6350 * @return the translation unit or NULL if errors occurred.
6352 translation_unit_t *parse(void)
6354 environment_stack = NEW_ARR_F(stack_entry_t, 0);
6355 label_stack = NEW_ARR_F(stack_entry_t, 0);
6356 diagnostic_count = 0;
6360 type_set_output(stderr);
6361 ast_set_output(stderr);
6363 lookahead_bufpos = 0;
6364 for(int i = 0; i < MAX_LOOKAHEAD + 2; ++i) {
6367 translation_unit_t *unit = parse_translation_unit();
6369 DEL_ARR_F(environment_stack);
6370 DEL_ARR_F(label_stack);
6379 * Initialize the parser.
6381 void init_parser(void)
6383 init_expression_parsers();
6384 obstack_init(&temp_obst);
6386 symbol_t *const va_list_sym = symbol_table_insert("__builtin_va_list");
6387 type_valist = create_builtin_type(va_list_sym, type_void_ptr);
6391 * Terminate the parser.
6393 void exit_parser(void)
6395 obstack_free(&temp_obst, NULL);